2 * PROJECT: ReactOS CRT library
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: lib/sdk/crt/stdio/file.c
5 * PURPOSE: File CRT functions
6 * PROGRAMMERS: Wine team
7 * Ported to ReactOS by Aleksey Bragin (aleksey@reactos.org)
10 /*********************************************
11 * This file contains ReactOS changes!!
12 * Don't blindly sync it with Wine code!
14 * If you break Unicode output on the console again, please update this counter:
15 * int hours_wasted_on_this = 42;
16 *********************************************/
19 * msvcrt.dll file functions
21 * Copyright 1996,1998 Marcus Meissner
22 * Copyright 1996 Jukka Iivonen
23 * Copyright 1997,2000 Uwe Bonnes
24 * Copyright 2000 Jon Griffiths
25 * Copyright 2004 Eric Pouech
26 * Copyright 2004 Juan Lang
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Lesser General Public
30 * License as published by the Free Software Foundation; either
31 * version 2.1 of the License, or (at your option) any later version.
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Lesser General Public License for more details.
38 * You should have received a copy of the GNU Lesser General Public
39 * License along with this library; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
43 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
47 #include "wine/unicode.h"
49 #include <sys/utime.h>
52 int *__p__fmode(void);
53 int *__p___mb_cur_max(void);
58 #define _IOCOMMIT 0x4000
86 /* _access() bit flags FIXME: incomplete */
87 /* defined in crt/io.h */
89 /* values for wxflag in file descriptor */
92 #define WX_READNL 0x04 /* read started with \n */
93 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
95 #define WX_READCR 0x08 /* underlying file is at \r */
96 #define WX_DONTINHERIT 0x10
97 #define WX_APPEND 0x20
98 #define WX_NOSEEK 0x40
101 /* values for exflag - it's used differently in msvcr90.dll*/
103 #define EF_UTF16 0x02
104 #define EF_UNK_UNICODE 0x08
106 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
107 static char utf16_bom
[2] = { 0xff, 0xfe };
109 /* FIXME: this should be allocated dynamically */
110 #define MAX_FILES 2048
111 #define FD_BLOCK_SIZE 64
113 /* ioinfo structure size is different in msvcrXX.dll's */
116 unsigned char wxflag
;
119 CRITICAL_SECTION crit
;
122 /*********************************************************************
123 * __pioinfo (MSVCRT.@)
124 * array of pointers to ioinfo arrays [64]
126 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
128 /*********************************************************************
129 * __badioinfo (MSVCRT.@)
131 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
133 static int fdstart
= 3; /* first unallocated fd */
134 static int fdend
= 3; /* highest allocated fd */
138 CRITICAL_SECTION crit
;
141 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
142 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
143 static int max_streams
= 512, stream_idx
;
145 /* INTERNAL: process umask */
146 static int MSVCRT_umask
= 0;
148 /* INTERNAL: static data for tmpnam and _wtmpname functions */
149 static int tmpnam_unique
;
151 /* This critical section protects the tables __pioinfo and fstreams,
152 * and their related indexes, fdstart, fdend,
153 * and stream_idx, from race conditions.
154 * It doesn't protect against race conditions manipulating the underlying files
155 * or flags; doing so would probably be better accomplished with per-file
156 * protection, rather than locking the whole table for every change.
158 static CRITICAL_SECTION file_cs
;
159 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
160 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
162 static inline ioinfo
* get_ioinfo(int fd
)
166 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
170 return ret
+ (fd
%FD_BLOCK_SIZE
);
173 static inline FILE* get_file(int i
)
183 ret
= fstream
[i
/FD_BLOCK_SIZE
];
185 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
186 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
187 ERR("out of memory\n");
192 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
194 ret
+= i
%FD_BLOCK_SIZE
;
199 static inline BOOL
is_valid_fd(int fd
)
201 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
204 /* INTERNAL: Get the HANDLE for a fd
205 * This doesn't lock the table, because a failure will result in
206 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
207 * it returns a valid handle which is about to be closed, a subsequent call
208 * will fail, most likely in a sane way.
210 /*static*/ HANDLE
fdtoh(int fd
)
212 if (!is_valid_fd(fd
))
214 WARN(":fd (%d) - no handle!\n",fd
);
217 return INVALID_HANDLE_VALUE
;
219 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
220 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
221 return get_ioinfo(fd
)->handle
;
224 /* INTERNAL: free a file entry fd */
225 static void free_fd(int fd
)
231 fdinfo
= get_ioinfo(fd
);
232 old_handle
= fdinfo
->handle
;
233 if(fdinfo
!= &__badioinfo
)
235 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
238 TRACE(":fd (%d) freed\n",fd
);
239 if (fd
< 3) /* don't use 0,1,2 for user files */
244 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
247 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
250 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
264 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
265 /* caller must hold the files lock */
266 static int set_fd(HANDLE hand
, int flag
, int fd
)
272 WARN(":files exhausted!\n");
277 fdinfo
= get_ioinfo(fd
);
278 if(fdinfo
== &__badioinfo
) {
281 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
282 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
283 WARN(":out of memory!\n");
288 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
289 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
291 fdinfo
= get_ioinfo(fd
);
294 fdinfo
->handle
= hand
;
295 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_NOSEEK
));
296 fdinfo
->lookahead
[0] = '\n';
297 fdinfo
->lookahead
[1] = '\n';
298 fdinfo
->lookahead
[2] = '\n';
301 /* locate next free slot */
302 if (fd
== fdstart
&& fd
== fdend
)
305 while (fdstart
< fdend
&&
306 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
308 /* update last fd in use */
311 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
315 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
316 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
317 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
323 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
324 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
329 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
330 ret
= set_fd(hand
, flag
, fdstart
);
335 /* INTERNAL: Allocate a FILE* for an fd slot */
336 /* caller must hold the files lock */
337 static FILE* alloc_fp(void)
342 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
348 if (file
->_flag
== 0)
350 if (i
== stream_idx
) stream_idx
++;
358 /* INTERNAL: initialize a FILE* from an open fd */
359 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
361 TRACE(":fd (%d) allocating FILE*\n",fd
);
362 if (!is_valid_fd(fd
))
364 WARN(":invalid fd %d\n",fd
);
369 memset(file
, 0, sizeof(*file
));
371 file
->_flag
= stream_flags
;
373 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
374 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
376 TRACE(":got FILE* (%p)\n",file
);
380 /* INTERNAL: Create an inheritance data block (for spawned process)
381 * The inheritance block is made of:
382 * 00 int nb of file descriptor (NBFD)
383 * 04 char file flags (wxflag): repeated for each fd
384 * 4+NBFD HANDLE file handle: repeated for each fd
386 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
393 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
394 *block
= calloc(*size
, 1);
400 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
401 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
403 *(unsigned*)*block
= fdend
;
404 for (fd
= 0; fd
< fdend
; fd
++)
406 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
407 fdinfo
= get_ioinfo(fd
);
408 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
410 *wxflag_ptr
= fdinfo
->wxflag
;
411 *handle_ptr
= fdinfo
->handle
;
416 *handle_ptr
= INVALID_HANDLE_VALUE
;
418 wxflag_ptr
++; handle_ptr
++;
423 /* INTERNAL: Set up all file descriptors,
424 * as well as default streams (stdin, stderr and stdout)
426 void msvcrt_init_io(void)
432 InitializeCriticalSection(&file_cs
);
433 file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_cs");
434 GetStartupInfoA(&si
);
435 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
441 count
= *(unsigned*)si
.lpReserved2
;
442 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
443 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
445 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
446 count
= min(count
, MAX_FILES
);
447 for (i
= 0; i
< count
; i
++)
449 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
450 set_fd(*handle_ptr
, *wxflag_ptr
, i
);
452 wxflag_ptr
++; handle_ptr
++;
454 fdend
= max( 3, count
);
455 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
456 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
460 set_fd(INVALID_HANDLE_VALUE
, 0, 3);
462 fdinfo
= get_ioinfo(0);
463 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
465 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
467 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
468 GetCurrentProcess(), &fdinfo
->handle
,
469 0, TRUE
, DUPLICATE_SAME_ACCESS
))
471 fdinfo
->handle
= std
;
473 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
476 fdinfo
= get_ioinfo(1);
477 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
479 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
481 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
482 GetCurrentProcess(), &fdinfo
->handle
,
483 0, TRUE
, DUPLICATE_SAME_ACCESS
))
485 fdinfo
->handle
= std
;
487 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
490 fdinfo
= get_ioinfo(2);
491 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
493 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
495 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
496 GetCurrentProcess(), &fdinfo
->handle
,
497 0, TRUE
, DUPLICATE_SAME_ACCESS
))
499 fdinfo
->handle
= std
;
501 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
504 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(0)->handle
,
505 get_ioinfo(1)->handle
, get_ioinfo(2)->handle
);
507 memset(_iob
,0,3*sizeof(FILE));
508 for (i
= 0; i
< 3; i
++)
510 /* FILE structs for stdin/out/err are static and never deleted */
512 _iob
[i
]._tmpfname
= NULL
;
513 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
518 /* INTERNAL: Flush stdio file buffer */
519 static int flush_buffer(FILE* file
)
522 int cnt
=file
->_ptr
-file
->_base
;
523 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
524 file
->_flag
|= _IOERR
;
527 file
->_ptr
=file
->_base
;
528 file
->_cnt
=file
->_bufsiz
;
533 /* INTERNAL: Allocate stdio file buffer */
534 /*static*/ void alloc_buffer(FILE* file
)
536 file
->_base
= calloc(BUFSIZ
,1);
538 file
->_bufsiz
= BUFSIZ
;
539 file
->_flag
|= _IOMYBUF
;
541 file
->_base
= (char*)(&file
->_charbuf
);
543 file
->_bufsiz
= sizeof(file
->_charbuf
);
545 file
->_ptr
= file
->_base
;
549 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
550 static int int_to_base32(int num
, char *str
)
565 *p
= (num
& 31) + '0';
567 *p
+= ('a' - '0' - 10);
574 /* INTERNAL: wide character version of int_to_base32 */
575 static int int_to_base32_w(int num
, wchar_t *str
)
590 *p
= (num
& 31) + '0';
592 *p
+= ('a' - '0' - 10);
599 /* INTERNAL: Create a wide string from an ascii string */
600 wchar_t *msvcrt_wstrdupa(const char *str
)
602 const unsigned int len
= strlen(str
) + 1 ;
603 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
606 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
610 /*********************************************************************
611 * __iob_func(MSVCRT.@)
613 FILE * CDECL
__iob_func(void)
618 /*********************************************************************
621 int CDECL
_access(const char *filename
, int mode
)
623 DWORD attr
= GetFileAttributesA(filename
);
625 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
627 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
629 _dosmaperr(GetLastError());
632 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
634 _set_errno(ERROR_ACCESS_DENIED
);
640 /*********************************************************************
641 * _access_s (MSVCRT.@)
643 int CDECL
_access_s(const char *filename
, int mode
)
645 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
646 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
652 return _access(filename
, mode
);
655 /*********************************************************************
656 * _waccess (MSVCRT.@)
658 int CDECL
_waccess(const wchar_t *filename
, int mode
)
660 DWORD attr
= GetFileAttributesW(filename
);
662 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
664 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
666 _dosmaperr(GetLastError());
669 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
671 _set_errno(ERROR_ACCESS_DENIED
);
677 /*********************************************************************
678 * _waccess_s (MSVCRT.@)
680 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
682 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
683 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
689 return _waccess(filename
, mode
);
692 /*********************************************************************
695 int CDECL
_chmod(const char *path
, int flags
)
697 DWORD oldFlags
= GetFileAttributesA(path
);
699 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
701 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
702 oldFlags
| FILE_ATTRIBUTE_READONLY
;
704 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
707 _dosmaperr(GetLastError());
711 /*********************************************************************
714 int CDECL
_wchmod(const wchar_t *path
, int flags
)
716 DWORD oldFlags
= GetFileAttributesW(path
);
718 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
720 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
721 oldFlags
| FILE_ATTRIBUTE_READONLY
;
723 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
726 _dosmaperr(GetLastError());
730 /*********************************************************************
733 int CDECL
_unlink(const char *path
)
735 TRACE("%s\n",debugstr_a(path
));
736 if(DeleteFileA(path
))
738 TRACE("failed (%d)\n",GetLastError());
739 _dosmaperr(GetLastError());
743 /*********************************************************************
744 * _wunlink (MSVCRT.@)
746 int CDECL
_wunlink(const wchar_t *path
)
748 TRACE("(%s)\n",debugstr_w(path
));
749 if(DeleteFileW(path
))
751 TRACE("failed (%d)\n",GetLastError());
752 _dosmaperr(GetLastError());
756 /* _flushall calls fflush which calls _flushall */
757 int CDECL
fflush(FILE* file
);
759 /* INTERNAL: Flush all stream buffer */
760 static int flush_all_buffers(int mask
)
762 int i
, num_flushed
= 0;
766 for (i
= 3; i
< stream_idx
; i
++) {
771 if(file
->_flag
& mask
) {
779 TRACE(":flushed (%d) handles\n",num_flushed
);
783 /*********************************************************************
784 * _flushall (MSVCRT.@)
786 int CDECL
_flushall(void)
788 return flush_all_buffers(_IOWRT
| _IOREAD
);
791 /*********************************************************************
794 int CDECL
fflush(FILE* file
)
797 flush_all_buffers(_IOWRT
);
798 } else if(file
->_flag
& _IOWRT
) {
802 res
= flush_buffer(file
);
804 if(!res && (file->_flag & _IOCOMMIT))
805 res = _commit(file->_file) ? EOF : 0;
810 } else if(file
->_flag
& _IOREAD
) {
813 file
->_ptr
= file
->_base
;
821 /*********************************************************************
824 int CDECL
_close(int fd
)
831 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
832 if (hand
== INVALID_HANDLE_VALUE
)
834 else if (!CloseHandle(hand
))
836 WARN(":failed-last error (%d)\n",GetLastError());
837 _dosmaperr(GetLastError());
850 /*********************************************************************
853 int CDECL
_commit(int fd
)
855 HANDLE hand
= fdtoh(fd
);
857 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
858 if (hand
== INVALID_HANDLE_VALUE
)
861 if (!FlushFileBuffers(hand
))
863 if (GetLastError() == ERROR_INVALID_HANDLE
)
865 /* FlushFileBuffers fails for console handles
866 * so we ignore this error.
870 TRACE(":failed-last error (%d)\n",GetLastError());
871 _dosmaperr(GetLastError());
878 /*********************************************************************
881 * MSDN isn't clear on this point, but the remarks for _pipe
882 * indicate file descriptors duplicated with _dup and _dup2 are always
885 int CDECL
_dup2(int od
, int nd
)
889 TRACE("(od=%d, nd=%d)\n", od
, nd
);
891 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
895 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
896 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
898 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
902 ret
= set_fd(handle
, wxflag
, nd
);
910 /* _dup2 returns 0, not nd, on success */
917 _dosmaperr(GetLastError());
929 /*********************************************************************
932 int CDECL
_dup(int od
)
938 if (_dup2(od
, fd
) == 0)
946 /*********************************************************************
949 int CDECL
_eof(int fd
)
952 LONG hcurpos
,hendpos
;
953 HANDLE hand
= fdtoh(fd
);
955 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
957 if (hand
== INVALID_HANDLE_VALUE
)
960 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
962 /* Otherwise we do it the hard way */
963 hcurpos
= hendpos
= 0;
964 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
965 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
967 if (curpos
== endpos
&& hcurpos
== hendpos
)
969 /* FIXME: shouldn't WX_ATEOF be set here? */
973 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
977 /*********************************************************************
978 * _fcloseall (MSVCRT.@)
980 int CDECL
_fcloseall(void)
982 int num_closed
= 0, i
;
986 for (i
= 3; i
< stream_idx
; i
++) {
989 if (file
->_flag
&& !fclose(file
))
994 TRACE(":closed (%d) handles\n",num_closed
);
998 /* free everything on process exit */
999 void msvcrt_free_io(void)
1004 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
1005 * stdout, and stderr (unlike GNU), so we need to fclose() them here
1006 * or they won't get flushed.
1012 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1015 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
1018 file_cs
.DebugInfo
->Spare
[0] = 0;
1019 DeleteCriticalSection(&file_cs
);
1022 /*********************************************************************
1023 * _lseeki64 (MSVCRT.@)
1025 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1027 HANDLE hand
= fdtoh(fd
);
1030 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1031 if (hand
== INVALID_HANDLE_VALUE
)
1034 if (whence
< 0 || whence
> 2)
1040 TRACE(":fd (%d) to %s pos %s\n",
1041 fd
,wine_dbgstr_longlong(offset
),
1042 (whence
==SEEK_SET
)?"SEEK_SET":
1043 (whence
==SEEK_CUR
)?"SEEK_CUR":
1044 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1046 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1047 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1048 ofs
.QuadPart
= offset
;
1049 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1050 GetLastError() == ERROR_SUCCESS
)
1052 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1053 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1055 return ofs
.QuadPart
;
1057 TRACE(":error-last error (%d)\n",GetLastError());
1058 _dosmaperr(GetLastError());
1062 /*********************************************************************
1065 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1067 return (LONG
)_lseeki64(fd
, offset
, whence
);
1070 /*********************************************************************
1071 * _lock_file (MSVCRT.@)
1073 void CDECL
_lock_file(FILE *file
)
1075 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1076 _lock(_STREAM_LOCKS
+(file
-_iob
));
1077 /* ReactOS: string streams dont need to be locked */
1078 else if(!(file
->_flag
& _IOSTRG
))
1079 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1082 /*********************************************************************
1083 * _unlock_file (MSVCRT.@)
1085 void CDECL
_unlock_file(FILE *file
)
1087 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1088 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1089 /* ReactOS: string streams dont need to be locked */
1090 else if(!(file
->_flag
& _IOSTRG
))
1091 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1095 /*********************************************************************
1096 * _locking (MSVCRT.@)
1098 * This is untested; the underlying LockFile doesn't work yet.
1100 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1104 HANDLE hand
= fdtoh(fd
);
1106 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1107 if (hand
== INVALID_HANDLE_VALUE
)
1110 if (mode
< 0 || mode
> 4)
1116 TRACE(":fd (%d) by 0x%08x mode %s\n",
1117 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1118 (mode
==_LK_LOCK
)?"_LK_LOCK":
1119 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1120 (mode
==_LK_RLCK
)?"_LK_RLCK":
1121 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1124 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1126 FIXME ("Seek failed\n");
1127 *_errno() = EINVAL
; /* FIXME */
1130 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1133 ret
= 1; /* just to satisfy gcc */
1136 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1141 else if (mode
== _LK_UNLCK
)
1142 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1144 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1145 /* FIXME - what about error settings? */
1146 return ret
? 0 : -1;
1149 /*********************************************************************
1150 * _fseeki64 (MSVCRT.@)
1152 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1157 /* Flush output if needed */
1158 if(file
->_flag
& _IOWRT
)
1161 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1162 offset
-= file
->_cnt
;
1163 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1164 /* Black magic correction for CR removal */
1166 for (i
=0; i
<file
->_cnt
; i
++) {
1167 if (file
->_ptr
[i
] == '\n')
1170 /* Black magic when reading CR at buffer boundary*/
1171 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1175 /* Discard buffered input */
1177 file
->_ptr
= file
->_base
;
1178 /* Reset direction of i/o */
1179 if(file
->_flag
& _IORW
) {
1180 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1182 /* Clear end of file flag */
1183 file
->_flag
&= ~_IOEOF
;
1184 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1190 /*********************************************************************
1193 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1195 return _fseeki64( file
, offset
, whence
);
1198 /*********************************************************************
1199 * _chsize (MSVCRT.@)
1201 int CDECL
_chsize(int fd
, long size
)
1207 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1212 if (handle
!= INVALID_HANDLE_VALUE
)
1214 /* save the current file pointer */
1215 cur
= _lseek(fd
, 0, SEEK_CUR
);
1218 pos
= _lseek(fd
, size
, SEEK_SET
);
1221 ret
= SetEndOfFile(handle
);
1222 if (!ret
) _dosmaperr(GetLastError());
1225 /* restore the file pointer */
1226 _lseek(fd
, cur
, SEEK_SET
);
1231 return ret
? 0 : -1;
1234 /*********************************************************************
1235 * clearerr (MSVCRT.@)
1237 void CDECL
clearerr(FILE* file
)
1239 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1242 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1246 /*********************************************************************
1249 void CDECL
rewind(FILE* file
)
1251 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1254 fseek(file
, 0L, SEEK_SET
);
1259 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1261 int plus
= strchrW(mode
, '+') != NULL
;
1263 TRACE("%s\n", debugstr_w(mode
));
1265 while(*mode
== ' ') mode
++;
1270 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1271 *stream_flags
= plus
? _IORW
: _IOREAD
;
1274 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1275 *stream_flags
= plus
? _IORW
: _IOWRT
;
1278 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1279 *stream_flags
= plus
? _IORW
: _IOWRT
;
1282 MSVCRT_INVALID_PMT(0, EINVAL
);
1286 *stream_flags
|= _commode
;
1288 while (*mode
&& *mode
!=',')
1292 *open_flags
|= _O_BINARY
;
1293 *open_flags
&= ~_O_TEXT
;
1296 *open_flags
|= _O_TEXT
;
1297 *open_flags
&= ~_O_BINARY
;
1300 *open_flags
|= _O_TEMPORARY
;
1303 *open_flags
|= _O_SHORT_LIVED
;
1306 *stream_flags
|= _IOCOMMIT
;
1309 *stream_flags
&= ~_IOCOMMIT
;
1312 *open_flags
|= _O_NOINHERIT
;
1321 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1324 ERR("incorrect mode flag: %c\n", mode
[-1]);
1330 static const WCHAR ccs
[] = {'c','c','s'};
1331 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1332 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1333 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1336 while(*mode
== ' ') mode
++;
1337 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1339 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1340 while(*mode
== ' ') mode
++;
1341 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1344 while(*mode
== ' ') mode
++;
1346 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1348 *open_flags
|= _O_U8TEXT
;
1349 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1351 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1353 *open_flags
|= _O_U16TEXT
;
1354 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1356 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1358 *open_flags
|= _O_WTEXT
;
1359 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1363 MSVCRT_INVALID_PMT(0, EINVAL
);
1367 while(*mode
== ' ') mode
++;
1370 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1375 /*********************************************************************
1376 * _fdopen (MSVCRT.@)
1378 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1381 wchar_t *modeW
= NULL
;
1383 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1385 ret
= _wfdopen(fd
, modeW
);
1391 /*********************************************************************
1392 * _wfdopen (MSVCRT.@)
1394 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1396 int open_flags
, stream_flags
;
1399 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1402 if (!(file
= alloc_fp()))
1404 else if (init_fp(file
, fd
, stream_flags
) == -1)
1409 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1415 /*********************************************************************
1416 * _filelength (MSVCRT.@)
1418 LONG CDECL
_filelength(int fd
)
1420 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1423 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1426 if (endPos
!= curPos
)
1427 _lseek(fd
, curPos
, SEEK_SET
);
1434 /*********************************************************************
1435 * _filelengthi64 (MSVCRT.@)
1437 __int64 CDECL
_filelengthi64(int fd
)
1439 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1442 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1445 if (endPos
!= curPos
)
1446 _lseeki64(fd
, curPos
, SEEK_SET
);
1453 /*********************************************************************
1454 * _fileno (MSVCRT.@)
1456 int CDECL
_fileno(FILE* file
)
1458 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1462 /*********************************************************************
1463 * _get_osfhandle (MSVCRT.@)
1465 intptr_t CDECL
_get_osfhandle(int fd
)
1467 HANDLE hand
= fdtoh(fd
);
1468 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1470 return (intptr_t)hand
;
1473 /*********************************************************************
1474 * _isatty (MSVCRT.@)
1476 int CDECL
_isatty(int fd
)
1478 HANDLE hand
= fdtoh(fd
);
1480 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1481 if (hand
== INVALID_HANDLE_VALUE
)
1484 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1487 /*********************************************************************
1488 * _mktemp (MSVCRT.@)
1490 char * CDECL
_mktemp(char *pattern
)
1493 char *retVal
= pattern
;
1501 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1505 id
= GetCurrentProcessId();
1509 int tempNum
= id
/ 10;
1510 *pattern
-- = id
- (tempNum
* 10) + '0';
1516 *pattern
= letter
++;
1517 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1519 } while(letter
<= 'z');
1523 /*********************************************************************
1524 * _wmktemp (MSVCRT.@)
1526 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1529 wchar_t *retVal
= pattern
;
1531 wchar_t letter
= 'a';
1534 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1538 id
= GetCurrentProcessId();
1542 int tempNum
= id
/ 10;
1543 *pattern
-- = id
- (tempNum
* 10) + '0';
1549 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1550 GetLastError() == ERROR_FILE_NOT_FOUND
)
1552 *pattern
= letter
++;
1553 } while(letter
!= '|');
1557 /*static*/ unsigned split_oflags(unsigned oflags
)
1560 unsigned unsupp
; /* until we support everything */
1562 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1563 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1564 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1565 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1566 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1567 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1568 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1569 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1570 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1572 if ((unsupp
= oflags
& ~(
1573 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1574 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1575 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1577 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1578 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1580 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1585 /*********************************************************************
1588 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1591 SECURITY_ATTRIBUTES sa
;
1592 HANDLE readHandle
, writeHandle
;
1600 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1601 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1602 sa
.lpSecurityDescriptor
= NULL
;
1603 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1605 unsigned int wxflags
= split_oflags(textmode
);
1609 fd
= alloc_fd(readHandle
, wxflags
);
1613 fd
= alloc_fd(writeHandle
, wxflags
);
1622 CloseHandle(writeHandle
);
1628 CloseHandle(readHandle
);
1629 CloseHandle(writeHandle
);
1635 _dosmaperr(GetLastError());
1640 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1642 char bom
[sizeof(utf8_bom
)];
1645 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1647 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1650 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1651 oflags
|= _O_U8TEXT
;
1652 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1654 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1655 oflags
|= _O_U16TEXT
;
1657 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1663 /*********************************************************************
1664 * _wsopen_s (MSVCRT.@)
1666 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1668 DWORD access
= 0, creation
= 0, attrib
;
1669 SECURITY_ATTRIBUTES sa
;
1674 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1675 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1677 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1680 wxflag
= split_oflags(oflags
);
1681 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1683 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1684 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1685 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1688 if (oflags
& _O_CREAT
)
1690 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1691 FIXME(": pmode 0x%04x ignored\n", pmode
);
1693 WARN(": pmode 0x%04x ignored\n", pmode
);
1695 if (oflags
& _O_EXCL
)
1696 creation
= CREATE_NEW
;
1697 else if (oflags
& _O_TRUNC
)
1698 creation
= CREATE_ALWAYS
;
1700 creation
= OPEN_ALWAYS
;
1702 else /* no _O_CREAT */
1704 if (oflags
& _O_TRUNC
)
1705 creation
= TRUNCATE_EXISTING
;
1707 creation
= OPEN_EXISTING
;
1716 sharing
= FILE_SHARE_READ
;
1719 sharing
= FILE_SHARE_WRITE
;
1722 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1725 ERR( "Unhandled shflags 0x%x\n", shflags
);
1728 attrib
= FILE_ATTRIBUTE_NORMAL
;
1730 if (oflags
& _O_TEMPORARY
)
1732 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1734 sharing
|= FILE_SHARE_DELETE
;
1737 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1738 sa
.lpSecurityDescriptor
= NULL
;
1739 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1741 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1742 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1743 && !(access
&GENERIC_READ
))
1745 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1746 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1747 if (hand
!= INVALID_HANDLE_VALUE
)
1749 oflags
= check_bom(hand
, oflags
, FALSE
);
1753 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1756 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1757 if (hand
== INVALID_HANDLE_VALUE
) {
1758 WARN(":failed-last error (%d)\n",GetLastError());
1759 _dosmaperr(GetLastError());
1763 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1765 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1766 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1767 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1769 if (oflags
& _O_U8TEXT
)
1771 DWORD written
= 0, tmp
;
1773 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1774 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1776 if (written
!= sizeof(utf8_bom
)) {
1777 WARN("error writing BOM\n");
1779 _dosmaperr(GetLastError());
1785 DWORD written
= 0, tmp
;
1787 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1788 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1790 if (written
!= sizeof(utf16_bom
))
1792 WARN("error writing BOM\n");
1794 _dosmaperr(GetLastError());
1799 else if (access
& GENERIC_READ
)
1800 oflags
= check_bom(hand
, oflags
, TRUE
);
1803 *fd
= alloc_fd(hand
, wxflag
);
1807 if (oflags
& _O_WTEXT
)
1808 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1809 else if (oflags
& _O_U16TEXT
)
1810 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1811 else if (oflags
& _O_U8TEXT
)
1812 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1814 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1818 /*********************************************************************
1819 * _wsopen (MSVCRT.@)
1821 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1826 if (oflags
& _O_CREAT
)
1830 va_start(ap
, shflags
);
1831 pmode
= va_arg(ap
, int);
1837 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1841 /*********************************************************************
1842 * _sopen_s (MSVCRT.@)
1844 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1846 DWORD access
= 0, creation
= 0, attrib
;
1850 SECURITY_ATTRIBUTES sa
;
1852 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1853 fd
, path
, oflags
, shflags
, pmode
);
1857 MSVCRT_INVALID_PMT("null out fd pointer", EINVAL
);
1862 wxflag
= split_oflags(oflags
);
1863 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1865 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1866 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1867 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1870 if (oflags
& _O_CREAT
)
1872 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1873 FIXME(": pmode 0x%04x ignored\n", pmode
);
1875 WARN(": pmode 0x%04x ignored\n", pmode
);
1877 if (oflags
& _O_EXCL
)
1878 creation
= CREATE_NEW
;
1879 else if (oflags
& _O_TRUNC
)
1880 creation
= CREATE_ALWAYS
;
1882 creation
= OPEN_ALWAYS
;
1884 else /* no _O_CREAT */
1886 if (oflags
& _O_TRUNC
)
1887 creation
= TRUNCATE_EXISTING
;
1889 creation
= OPEN_EXISTING
;
1898 sharing
= FILE_SHARE_READ
;
1901 sharing
= FILE_SHARE_WRITE
;
1904 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1907 ERR( "Unhandled shflags 0x%x\n", shflags
);
1910 attrib
= FILE_ATTRIBUTE_NORMAL
;
1912 if (oflags
& _O_TEMPORARY
)
1914 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1916 sharing
|= FILE_SHARE_DELETE
;
1919 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1920 sa
.lpSecurityDescriptor
= NULL
;
1921 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1923 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1924 if (hand
== INVALID_HANDLE_VALUE
) {
1925 WARN(":failed-last error (%d)\n", GetLastError());
1926 _dosmaperr(GetLastError());
1930 *fd
= alloc_fd(hand
, wxflag
);
1932 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1936 /*********************************************************************
1939 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1944 if (oflags
& _O_CREAT
)
1948 va_start(ap
, shflags
);
1949 pmode
= va_arg(ap
, int);
1955 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1959 /*********************************************************************
1962 int CDECL
_open( const char *path
, int flags
, ... )
1966 if (flags
& _O_CREAT
)
1969 va_start(ap
, flags
);
1970 pmode
= va_arg(ap
, int);
1972 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1975 return _sopen( path
, flags
, _SH_DENYNO
);
1978 /*********************************************************************
1981 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1985 if (flags
& _O_CREAT
)
1988 va_start(ap
, flags
);
1989 pmode
= va_arg(ap
, int);
1991 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1994 return _wsopen( path
, flags
, _SH_DENYNO
);
1997 /*********************************************************************
2000 int CDECL
_creat(const char *path
, int flags
)
2002 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2003 return _open(path
, usedFlags
);
2006 /*********************************************************************
2007 * _wcreat (MSVCRT.@)
2009 int CDECL
_wcreat(const wchar_t *path
, int flags
)
2011 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2012 return _wopen(path
, usedFlags
);
2015 /*********************************************************************
2016 * _open_osfhandle (MSVCRT.@)
2018 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2023 /* _O_RDONLY (0) always matches, so set the read flag
2024 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2025 * file, so set the write flag. It also only sets _O_TEXT if it wants
2026 * text - it never sets _O_BINARY.
2028 /* don't let split_oflags() decide the mode if no mode is passed */
2029 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2030 oflags
|= _O_BINARY
;
2032 flags
= GetFileType((HANDLE
)handle
);
2033 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2035 _dosmaperr(GetLastError());
2039 if (flags
== FILE_TYPE_CHAR
)
2041 else if (flags
== FILE_TYPE_PIPE
)
2045 flags
|= split_oflags(oflags
);
2047 fd
= alloc_fd((HANDLE
)handle
, flags
);
2048 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2052 /*********************************************************************
2055 int CDECL
_rmtmp(void)
2057 int num_removed
= 0, i
;
2061 for (i
= 3; i
< stream_idx
; i
++) {
2064 if (file
->_tmpfname
)
2073 TRACE(":removed (%d) temp files\n",num_removed
);
2077 /*********************************************************************
2080 * When reading \r as last character in text mode, read() positions
2081 * the file pointer on the \r character while getc() goes on to
2084 static int read_i(int fd
, void *buf
, unsigned int count
)
2087 char *bufstart
= buf
;
2088 HANDLE hand
= fdtoh(fd
);
2089 ioinfo
*fdinfo
= get_ioinfo(fd
);
2094 if (fdinfo
->wxflag
& WX_READEOF
) {
2095 fdinfo
->wxflag
|= WX_ATEOF
;
2096 TRACE("already at EOF, returning 0\n");
2099 /* Don't trace small reads, it gets *very* annoying */
2101 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2102 if (hand
== INVALID_HANDLE_VALUE
)
2105 /* Reading single bytes in O_TEXT mode makes things slow
2106 * So read big chunks
2108 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2110 if (count
!= 0 && num_read
== 0)
2112 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2113 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2115 else if (fdinfo
->wxflag
& WX_TEXT
)
2118 if (bufstart
[num_read
-1] == '\r')
2122 fdinfo
->wxflag
&= ~WX_READCR
;
2123 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
2127 fdinfo
->wxflag
|= WX_READCR
;
2132 fdinfo
->wxflag
&= ~WX_READCR
;
2133 for (i
=0, j
=0; i
<num_read
; i
++)
2135 /* in text mode, a ctrl-z signals EOF */
2136 if (bufstart
[i
] == 0x1a)
2138 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2139 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2142 /* in text mode, strip \r if followed by \n.
2143 * BUG: should save state across calls somehow, so CR LF that
2144 * straddles buffer boundary gets recognized properly?
2146 if ((bufstart
[i
] != '\r')
2147 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
2148 bufstart
[j
++] = bufstart
[i
];
2155 if (GetLastError() == ERROR_BROKEN_PIPE
)
2157 TRACE(":end-of-pipe\n");
2158 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2163 TRACE(":failed-last error (%d)\n",GetLastError());
2169 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2173 /*********************************************************************
2176 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2179 num_read
= read_i(fd
, buf
, count
);
2183 /*********************************************************************
2184 * _setmode (MSVCRT.@)
2186 int CDECL
_setmode(int fd
,int mode
)
2188 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2189 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2192 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2193 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2198 if(mode
== _O_BINARY
) {
2199 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2200 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2204 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2206 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2207 else if(mode
== _O_U8TEXT
)
2208 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2210 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2216 /*********************************************************************
2219 long CDECL
_tell(int fd
)
2221 return _lseek(fd
, 0, SEEK_CUR
);
2224 /*********************************************************************
2225 * _telli64 (MSVCRT.@)
2227 __int64 CDECL
_telli64(int fd
)
2229 return _lseeki64(fd
, 0, SEEK_CUR
);
2232 /*********************************************************************
2233 * _tempnam (MSVCRT.@)
2235 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2237 char tmpbuf
[MAX_PATH
];
2238 const char *tmp_dir
= getenv("TMP");
2240 if (tmp_dir
) dir
= tmp_dir
;
2242 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2243 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2245 TRACE("got name (%s)\n",tmpbuf
);
2246 DeleteFileA(tmpbuf
);
2247 return _strdup(tmpbuf
);
2249 TRACE("failed (%d)\n",GetLastError());
2253 /*********************************************************************
2254 * _wtempnam (MSVCRT.@)
2256 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2258 wchar_t tmpbuf
[MAX_PATH
];
2260 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2261 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2263 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2264 DeleteFileW(tmpbuf
);
2265 return _wcsdup(tmpbuf
);
2267 TRACE("failed (%d)\n",GetLastError());
2271 /*********************************************************************
2274 int CDECL
_umask(int umask
)
2276 int old_umask
= umask
;
2277 TRACE("(%d)\n",umask
);
2278 MSVCRT_umask
= umask
;
2282 /*********************************************************************
2285 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2288 ioinfo
*info
= get_ioinfo(fd
);
2289 HANDLE hand
= info
->handle
;
2291 /* Don't trace small writes, it gets *very* annoying */
2294 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2296 if (hand
== INVALID_HANDLE_VALUE
)
2302 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2308 /* If appending, go to EOF */
2309 if (info
->wxflag
& WX_APPEND
)
2310 _lseek(fd
, 0, FILE_END
);
2312 if (!(info
->wxflag
& WX_TEXT
))
2314 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2315 && (num_written
== count
))
2317 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2318 hand
, GetLastError());
2323 unsigned int i
, j
, nr_lf
, size
;
2326 const char *s
= buf
, *buf_start
= buf
;
2328 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2330 /* find number of \n */
2331 for (nr_lf
=0, i
=0; i
<count
; i
++)
2337 if ((q
= p
= malloc(size
)))
2339 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2348 FIXME("Malloc failed\n");
2360 else if (info
->exflag
& EF_UTF16
)
2362 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2363 if (s
[i
]=='\n' && s
[i
+1]==0)
2368 if ((q
= p
= malloc(size
)))
2370 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2372 if (s
[i
]=='\n' && s
[i
+1]==0)
2383 FIXME("Malloc failed\n");
2399 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2400 if (s
[i
]=='\n' && s
[i
+1]==0)
2403 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2405 _dosmaperr(GetLastError());
2410 size
= conv_len
+nr_lf
;
2411 if((p
= malloc(count
+nr_lf
*2+size
)))
2413 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2415 if (s
[i
]=='\n' && s
[i
+1]==0)
2423 q
= p
+count
+nr_lf
*2;
2424 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2425 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2429 FIXME("Malloc failed\n");
2436 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2440 if (num_written
!= size
)
2442 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2443 fd
, hand
, GetLastError(), num_written
);
2445 return s
- buf_start
;
2453 /*********************************************************************
2456 int CDECL
_putw(int val
, FILE* file
)
2461 len
= _write(file
->_file
, &val
, sizeof(val
));
2462 if (len
== sizeof(val
)) {
2467 file
->_flag
|= _IOERR
;
2472 /*********************************************************************
2475 int CDECL
fclose(FILE* file
)
2481 free(file
->_tmpfname
);
2482 file
->_tmpfname
= NULL
;
2483 /* flush stdio buffers */
2484 if(file
->_flag
& _IOWRT
)
2486 if(file
->_flag
& _IOMYBUF
)
2489 r
=_close(file
->_file
);
2493 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2494 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2496 if(file
== get_file(stream_idx
-1)) {
2497 while(stream_idx
>3 && !file
->_flag
) {
2499 file
= get_file(stream_idx
-1);
2503 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2506 /*********************************************************************
2509 int CDECL
feof(FILE* file
)
2511 return file
->_flag
& _IOEOF
;
2514 /*********************************************************************
2517 int CDECL
ferror(FILE* file
)
2519 return file
->_flag
& _IOERR
;
2522 /*********************************************************************
2523 * _filbuf (MSVCRT.@)
2525 int CDECL
_filbuf(FILE* file
)
2530 if(file
->_flag
& _IOSTRG
) {
2535 /* Allocate buffer if needed */
2536 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2539 if(!(file
->_flag
& _IOREAD
)) {
2540 if(file
->_flag
& _IORW
)
2541 file
->_flag
|= _IOREAD
;
2548 if(file
->_flag
& _IONBF
) {
2550 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2551 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2559 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2561 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2568 file
->_ptr
= file
->_base
+1;
2569 c
= *(unsigned char *)file
->_base
;
2575 /*********************************************************************
2578 int CDECL
fgetc(FILE* file
)
2586 i
= (unsigned char *)file
->_ptr
++;
2595 /*********************************************************************
2596 * _fgetchar (MSVCRT.@)
2598 int CDECL
_fgetchar(void)
2600 return fgetc(stdin
);
2603 /*********************************************************************
2606 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2609 char * buf_start
= s
;
2611 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2612 file
,file
->_file
,s
,size
);
2616 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2621 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2623 TRACE(":nothing read\n");
2627 if ((cc
!= EOF
) && (size
> 1))
2630 TRACE(":got %s\n", debugstr_a(buf_start
));
2635 /*********************************************************************
2638 wint_t CDECL
fgetwc(FILE* file
)
2645 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2646 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2649 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2658 char mbs
[MB_LEN_MAX
];
2664 if(isleadbyte((unsigned char)mbs
[0])) {
2675 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2683 /*********************************************************************
2686 int CDECL
_getw(FILE* file
)
2694 for (j
=0; j
<sizeof(int); j
++) {
2697 file
->_flag
|= _IOEOF
;
2708 /*********************************************************************
2711 wint_t CDECL
getwc(FILE* file
)
2713 return fgetwc(file
);
2716 /*********************************************************************
2717 * _fgetwchar (MSVCRT.@)
2719 wint_t CDECL
_fgetwchar(void)
2721 return fgetwc(stdin
);
2724 /*********************************************************************
2725 * getwchar (MSVCRT.@)
2727 wint_t CDECL
getwchar(void)
2729 return _fgetwchar();
2732 /*********************************************************************
2735 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2738 wchar_t * buf_start
= s
;
2740 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2741 file
,file
->_file
,s
,size
);
2745 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2750 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2752 TRACE(":nothing read\n");
2756 if ((cc
!= WEOF
) && (size
> 1))
2759 TRACE(":got %s\n", debugstr_w(buf_start
));
2764 /*********************************************************************
2767 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2769 size_t wrcnt
=size
* nmemb
;
2778 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2779 memcpy(file
->_ptr
, ptr
, pcnt
);
2784 ptr
= (const char*)ptr
+ pcnt
;
2785 } else if(!file
->_bufsiz
&& (file
->_flag
& _IONBF
)) {
2786 if(!(file
->_flag
& _IOWRT
)) {
2787 if(file
->_flag
& _IORW
)
2788 file
->_flag
|= _IOWRT
;
2793 if(_write(file
->_file
, ptr
, wrcnt
) <= 0) {
2794 file
->_flag
|= _IOERR
;
2800 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
2804 ptr
= (const char*)ptr
+ 1;
2809 return written
/ size
;
2812 /*********************************************************************
2814 * FORKED for ReactOS, don't sync with Wine!
2816 * - http://jira.reactos.org/browse/CORE-6495
2817 * - http://bugs.winehq.org/show_bug.cgi?id=8598
2819 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2821 /* If this is a real file stream (and not some temporary one for
2822 sprintf-like functions), check whether it is opened in text mode.
2823 In this case, we have to perform an implicit conversion to ANSI. */
2824 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
2826 /* Convert to multibyte in text mode */
2827 char mbc
[MB_LEN_MAX
];
2830 mb_return
= wctomb(mbc
, c
);
2835 /* Output all characters */
2836 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2841 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2848 /*********************************************************************
2849 * _fputwchar (MSVCRT.@)
2851 wint_t CDECL
_fputwchar(wint_t wc
)
2853 return fputwc(wc
, stdout
);
2856 /*********************************************************************
2857 * _wfsopen (MSVCRT.@)
2859 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2862 int open_flags
, stream_flags
, fd
;
2864 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2866 /* map mode string to open() flags. "man fopen" for possibilities. */
2867 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2871 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2874 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2876 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2883 TRACE(":got (%p)\n",file
);
2884 if (fd
>= 0 && !file
)
2890 /*********************************************************************
2891 * _fsopen (MSVCRT.@)
2893 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2896 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2898 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2899 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2903 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2906 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2911 ret
= _wfsopen(pathW
, modeW
, share
);
2918 /*********************************************************************
2921 FILE * CDECL
fopen(const char *path
, const char *mode
)
2923 return _fsopen( path
, mode
, _SH_DENYNO
);
2926 /*********************************************************************
2927 * fopen_s (MSVCRT.@)
2929 int CDECL
fopen_s(FILE** pFile
,
2930 const char *filename
, const char *mode
)
2932 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2933 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2938 *pFile
= fopen(filename
, mode
);
2945 /*********************************************************************
2946 * _wfopen (MSVCRT.@)
2948 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2950 return _wfsopen( path
, mode
, _SH_DENYNO
);
2953 /*********************************************************************
2954 * _wfopen_s (MSVCRT.@)
2956 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
2957 const wchar_t *mode
)
2959 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2960 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2965 *pFile
= _wfopen(filename
, mode
);
2972 /* fputc calls _flsbuf which calls fputc */
2973 int CDECL
_flsbuf(int c
, FILE* file
);
2975 /*********************************************************************
2978 int CDECL
fputc(int c
, FILE* file
)
2988 res
= flush_buffer(file
);
2990 return res
? res
: c
;
2997 res
= _flsbuf(c
, file
);
3003 /*********************************************************************
3004 * _fputchar (MSVCRT.@)
3006 int CDECL
_fputchar(int c
)
3008 return fputc(c
, stdout
);
3011 /*********************************************************************
3014 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3016 size_t rcnt
=size
* nmemb
;
3025 /* first buffered data */
3027 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
3028 memcpy(ptr
, file
->_ptr
, pcnt
);
3033 ptr
= (char*)ptr
+ pcnt
;
3034 } else if(!(file
->_flag
& _IOREAD
)) {
3035 if(file
->_flag
& _IORW
) {
3036 file
->_flag
|= _IOREAD
;
3045 /* Fill the buffer on small reads.
3046 * TODO: Use a better buffering strategy.
3048 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
3049 if (file
->_bufsiz
== 0) {
3052 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3053 file
->_ptr
= file
->_base
;
3054 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3055 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3056 if (i
> 0 && i
< file
->_cnt
) {
3057 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3058 file
->_flag
&= ~_IOEOF
;
3061 memcpy(ptr
, file
->_ptr
, i
);
3066 i
= _read(file
->_file
,ptr
, rcnt
);
3070 ptr
= (char *)ptr
+i
;
3071 /* expose feof condition in the flags
3072 * MFC tests file->_flag for feof, and doesn't call feof())
3074 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3075 file
->_flag
|= _IOEOF
;
3078 file
->_flag
|= _IOERR
;
3089 /*********************************************************************
3090 * _wfreopen (MSVCRT.@)
3093 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3095 int open_flags
, stream_flags
, fd
;
3097 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3100 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3105 /* map mode string to open() flags. "man fopen" for possibilities. */
3106 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3110 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3113 else if (init_fp(file
, fd
, stream_flags
) == -1)
3116 WARN(":failed-last error (%d)\n",GetLastError());
3117 _dosmaperr(GetLastError());
3126 /*********************************************************************
3127 * freopen (MSVCRT.@)
3130 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3133 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3135 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3136 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3142 ret
= _wfreopen(pathW
, modeW
, file
);
3149 /*********************************************************************
3150 * fsetpos (MSVCRT.@)
3152 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3157 /* Note that all this has been lifted 'as is' from fseek */
3158 if(file
->_flag
& _IOWRT
)
3161 /* Discard buffered input */
3163 file
->_ptr
= file
->_base
;
3165 /* Reset direction of i/o */
3166 if(file
->_flag
& _IORW
) {
3167 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3170 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3175 /*********************************************************************
3176 * _ftelli64 (MSVCRT.@)
3178 __int64 CDECL
_ftelli64(FILE* file
)
3183 pos
= _telli64(file
->_file
);
3189 if(file
->_flag
& _IOWRT
) {
3190 pos
+= file
->_ptr
- file
->_base
;
3192 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3195 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3199 } else if(!file
->_cnt
) { /* nothing to do */
3200 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3204 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3205 for(i
=0; i
<file
->_cnt
; i
++)
3206 if(file
->_ptr
[i
] == '\n')
3212 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3217 pos
-= file
->_bufsiz
;
3218 pos
+= file
->_ptr
- file
->_base
;
3220 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3221 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3224 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3235 /*********************************************************************
3238 LONG CDECL
ftell(FILE* file
)
3240 return (LONG
)_ftelli64(file
);
3243 /*********************************************************************
3244 * fgetpos (MSVCRT.@)
3246 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3251 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
3257 if( file
->_flag
& _IOWRT
) {
3258 off
= file
->_ptr
- file
->_base
;
3261 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3262 /* Black magic correction for CR removal */
3264 for (i
=0; i
<file
->_cnt
; i
++) {
3265 if (file
->_ptr
[i
] == '\n')
3268 /* Black magic when reading CR at buffer boundary*/
3269 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3279 /*********************************************************************
3282 int CDECL
fputs(const char *s
, FILE* file
)
3284 size_t i
, len
= strlen(s
);
3288 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3289 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3293 for (i
=0; i
<len
; i
++)
3294 if (fputc(s
[i
], file
) == EOF
) {
3303 /*********************************************************************
3306 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3308 size_t i
, len
= strlenW(s
);
3312 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3313 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3317 for (i
=0; i
<len
; i
++) {
3318 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
3319 || fputwc(s
[i
], file
) == WEOF
) {
3329 /*********************************************************************
3330 * getchar (MSVCRT.@)
3332 int CDECL
getchar(void)
3334 return fgetc(stdin
);
3337 /*********************************************************************
3340 int CDECL
getc(FILE* file
)
3345 /*********************************************************************
3348 char * CDECL
gets(char *buf
)
3351 char * buf_start
= buf
;
3354 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3356 if(cc
!= '\r') *buf
++ = (char)cc
;
3360 TRACE("got '%s'\n", buf_start
);
3361 _unlock_file(stdin
);
3365 /*********************************************************************
3368 wchar_t* CDECL
_getws(wchar_t* buf
)
3374 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3378 *buf
++ = (wchar_t)cc
;
3382 TRACE("got %s\n", debugstr_w(ws
));
3383 _unlock_file(stdin
);
3387 /*********************************************************************
3390 int CDECL
putc(int c
, FILE* file
)
3392 return fputc(c
, file
);
3395 /*********************************************************************
3396 * putchar (MSVCRT.@)
3398 int CDECL
putchar(int c
)
3400 return fputc(c
, stdout
);
3403 /*********************************************************************
3404 * _putwch (MSVCRT.@)
3406 wint_t CDECL
_putwch(wchar_t c
)
3408 return fputwc(c
, stdout
);
3411 /*********************************************************************
3414 int CDECL
puts(const char *s
)
3416 size_t len
= strlen(s
);
3420 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3421 _unlock_file(stdout
);
3425 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3426 _unlock_file(stdout
);
3430 /*********************************************************************
3433 int CDECL
_putws(const wchar_t *s
)
3435 static const wchar_t nl
= '\n';
3436 size_t len
= strlenW(s
);
3440 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3441 _unlock_file(stdout
);
3445 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3446 _unlock_file(stdout
);
3450 /*********************************************************************
3453 int CDECL
remove(const char *path
)
3455 TRACE("(%s)\n",path
);
3456 if (DeleteFileA(path
))
3458 TRACE(":failed (%d)\n",GetLastError());
3459 _dosmaperr(GetLastError());
3463 /*********************************************************************
3464 * _wremove (MSVCRT.@)
3466 int CDECL
_wremove(const wchar_t *path
)
3468 TRACE("(%s)\n",debugstr_w(path
));
3469 if (DeleteFileW(path
))
3471 TRACE(":failed (%d)\n",GetLastError());
3472 _dosmaperr(GetLastError());
3476 /*********************************************************************
3479 int CDECL
rename(const char *oldpath
,const char *newpath
)
3481 TRACE(":from %s to %s\n",oldpath
,newpath
);
3482 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3484 TRACE(":failed (%d)\n",GetLastError());
3485 _dosmaperr(GetLastError());
3489 /*********************************************************************
3490 * _wrename (MSVCRT.@)
3492 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3494 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3495 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3497 TRACE(":failed (%d)\n",GetLastError());
3498 _dosmaperr(GetLastError());
3502 /*********************************************************************
3503 * setvbuf (MSVCRT.@)
3505 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3513 if(mode
== _IOFBF
) {
3514 file
->_flag
&= ~_IONBF
;
3515 file
->_base
= file
->_ptr
= buf
;
3517 file
->_bufsiz
= size
;
3520 file
->_flag
|= _IONBF
;
3526 /*********************************************************************
3529 void CDECL
setbuf(FILE* file
, char *buf
)
3531 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3534 /*********************************************************************
3537 char * CDECL
tmpnam(char *s
)
3544 thread_data_t
*data
= msvcrt_get_thread_data();
3546 if(!data
->tmpnam_buffer
)
3547 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3549 s
= data
->tmpnam_buffer
;
3552 int_to_base32(GetCurrentProcessId(), tmpstr
);
3553 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3554 for (count
= 0; count
< TMP_MAX
; count
++)
3556 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3557 memcpy(p
, tmpstr
, size
);
3558 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3559 GetLastError() == ERROR_FILE_NOT_FOUND
)
3565 /*********************************************************************
3566 * _wtmpnam (MSVCRT.@)
3568 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3570 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3575 thread_data_t
*data
= msvcrt_get_thread_data();
3577 if(!data
->wtmpnam_buffer
)
3578 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3580 s
= data
->wtmpnam_buffer
;
3583 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3584 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3585 for (count
= 0; count
< TMP_MAX
; count
++)
3587 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3588 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3589 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3590 GetLastError() == ERROR_FILE_NOT_FOUND
)
3596 /*********************************************************************
3597 * tmpfile (MSVCRT.@)
3599 FILE* CDECL
tmpfile(void)
3601 char *filename
= tmpnam(NULL
);
3606 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3607 _S_IREAD
| _S_IWRITE
);
3608 if (fd
!= -1 && (file
= alloc_fp()))
3610 if (init_fp(file
, fd
, _IORW
) == -1)
3615 else file
->_tmpfname
= _strdup(filename
);
3618 if(fd
!= -1 && !file
)
3624 /*********************************************************************
3627 int CDECL
ungetc(int c
, FILE * file
)
3633 if(file
->_bufsiz
== 0) {
3637 if(file
->_ptr
>file
->_base
) {
3650 /*********************************************************************
3651 * ungetwc (MSVCRT.@)
3653 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3656 char * pp
= (char *)&mwc
;
3660 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3661 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3673 /*********************************************************************
3674 * _getmaxstdio (MSVCRT.@)
3676 int CDECL
_getmaxstdio(void)
3681 /*********************************************************************
3682 * _setmaxstdio (MSVCRT.@)
3684 int CDECL
_setmaxstdio(int newmax
)
3686 TRACE("%d\n", newmax
);
3688 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3691 max_streams
= newmax
;