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);
80 /* _access() bit flags FIXME: incomplete */
81 /* defined in crt/io.h */
83 /* values for wxflag in file descriptor */
86 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
87 #define WX_READCR 0x08 /* underlying file is at \r */
88 #define WX_DONTINHERIT 0x10
89 #define WX_APPEND 0x20
92 /* values for exflag - it's used differently in msvcr90.dll*/
95 #define EF_CRIT_INIT 0x04
96 #define EF_UNK_UNICODE 0x08
98 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
99 static char utf16_bom
[2] = { 0xff, 0xfe };
101 /* FIXME: this should be allocated dynamically */
102 #define MAX_FILES 2048
103 #define FD_BLOCK_SIZE 64
105 /* ioinfo structure size is different in msvcrXX.dll's */
108 unsigned char wxflag
;
111 CRITICAL_SECTION crit
;
114 /*********************************************************************
115 * __pioinfo (MSVCRT.@)
116 * array of pointers to ioinfo arrays [64]
118 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
120 /*********************************************************************
121 * __badioinfo (MSVCRT.@)
123 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
125 static int fdstart
= 3; /* first unallocated fd */
126 static int fdend
= 3; /* highest allocated fd */
130 CRITICAL_SECTION crit
;
133 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
134 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
135 static int max_streams
= 512, stream_idx
;
137 /* INTERNAL: process umask */
138 static int MSVCRT_umask
= 0;
140 /* INTERNAL: static data for tmpnam and _wtmpname functions */
141 static int tmpnam_unique
;
143 /* This critical section protects the tables __pioinfo and fstreams,
144 * and their related indexes, fdstart, fdend,
145 * and stream_idx, from race conditions.
146 * It doesn't protect against race conditions manipulating the underlying files
147 * or flags; doing so would probably be better accomplished with per-file
148 * protection, rather than locking the whole table for every change.
150 static CRITICAL_SECTION file_cs
;
151 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
152 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
154 static inline ioinfo
* get_ioinfo(int fd
)
158 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
162 return ret
+ (fd
%FD_BLOCK_SIZE
);
165 static inline FILE* get_file(int i
)
175 ret
= fstream
[i
/FD_BLOCK_SIZE
];
177 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
178 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
179 ERR("out of memory\n");
184 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
186 ret
+= i
%FD_BLOCK_SIZE
;
191 static inline BOOL
is_valid_fd(int fd
)
193 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
196 /* INTERNAL: Get the HANDLE for a fd
197 * This doesn't lock the table, because a failure will result in
198 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
199 * it returns a valid handle which is about to be closed, a subsequent call
200 * will fail, most likely in a sane way.
202 /*static*/ HANDLE
fdtoh(int fd
)
204 if (!is_valid_fd(fd
))
206 WARN(":fd (%d) - no handle!\n",fd
);
209 return INVALID_HANDLE_VALUE
;
211 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
212 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
213 return get_ioinfo(fd
)->handle
;
216 /* INTERNAL: free a file entry fd */
217 static void free_fd(int fd
)
223 fdinfo
= get_ioinfo(fd
);
224 old_handle
= fdinfo
->handle
;
225 if(fdinfo
!= &__badioinfo
)
227 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
230 TRACE(":fd (%d) freed\n",fd
);
231 if (fd
< 3) /* don't use 0,1,2 for user files */
236 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
239 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
242 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
256 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
257 /* caller must hold the files lock */
258 static int alloc_fd_from(HANDLE hand
, int flag
, int fd
)
264 WARN(":files exhausted!\n");
269 fdinfo
= get_ioinfo(fd
);
270 if(fdinfo
== &__badioinfo
) {
273 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
274 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
275 WARN(":out of memory!\n");
280 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
281 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
283 fdinfo
= get_ioinfo(fd
);
286 fdinfo
->handle
= hand
;
287 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
289 /* locate next free slot */
290 if (fd
== fdstart
&& fd
== fdend
)
293 while (fdstart
< fdend
&&
294 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
296 /* update last fd in use */
299 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
303 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
304 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
305 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
311 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
312 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
317 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
318 ret
= alloc_fd_from(hand
, flag
, fdstart
);
323 /* INTERNAL: Allocate a FILE* for an fd slot */
324 /* caller must hold the files lock */
325 static FILE* alloc_fp(void)
330 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
336 if (file
->_flag
== 0)
338 if (i
== stream_idx
) stream_idx
++;
346 /* INTERNAL: initialize a FILE* from an open fd */
347 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
349 TRACE(":fd (%d) allocating FILE*\n",fd
);
350 if (!is_valid_fd(fd
))
352 WARN(":invalid fd %d\n",fd
);
357 memset(file
, 0, sizeof(*file
));
359 file
->_flag
= stream_flags
;
361 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
362 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
364 TRACE(":got FILE* (%p)\n",file
);
368 /* INTERNAL: Create an inheritance data block (for spawned process)
369 * The inheritance block is made of:
370 * 00 int nb of file descriptor (NBFD)
371 * 04 char file flags (wxflag): repeated for each fd
372 * 4+NBFD HANDLE file handle: repeated for each fd
374 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
381 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
382 *block
= calloc(*size
, 1);
388 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
389 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
391 *(unsigned*)*block
= fdend
;
392 for (fd
= 0; fd
< fdend
; fd
++)
394 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
395 fdinfo
= get_ioinfo(fd
);
396 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
398 *wxflag_ptr
= fdinfo
->wxflag
;
399 *handle_ptr
= fdinfo
->handle
;
404 *handle_ptr
= INVALID_HANDLE_VALUE
;
406 wxflag_ptr
++; handle_ptr
++;
411 /* INTERNAL: Set up all file descriptors,
412 * as well as default streams (stdin, stderr and stdout)
414 void msvcrt_init_io(void)
420 InitializeCriticalSection(&file_cs
);
421 file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_cs");
422 GetStartupInfoA(&si
);
423 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
429 count
= *(unsigned*)si
.lpReserved2
;
430 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
431 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
433 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
434 count
= min(count
, MAX_FILES
);
435 for (i
= 0; i
< count
; i
++)
437 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
438 alloc_fd_from(*handle_ptr
, *wxflag_ptr
, i
);
440 wxflag_ptr
++; handle_ptr
++;
442 fdend
= max( 3, count
);
443 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
444 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
448 alloc_fd_from(INVALID_HANDLE_VALUE
, 0, 3);
450 fdinfo
= get_ioinfo(0);
451 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
453 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
455 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
456 GetCurrentProcess(), &fdinfo
->handle
,
457 0, TRUE
, DUPLICATE_SAME_ACCESS
))
459 fdinfo
->handle
= std
;
461 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
464 fdinfo
= get_ioinfo(1);
465 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
467 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
469 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
470 GetCurrentProcess(), &fdinfo
->handle
,
471 0, TRUE
, DUPLICATE_SAME_ACCESS
))
473 fdinfo
->handle
= std
;
475 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
478 fdinfo
= get_ioinfo(2);
479 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
481 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
483 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
484 GetCurrentProcess(), &fdinfo
->handle
,
485 0, TRUE
, DUPLICATE_SAME_ACCESS
))
487 fdinfo
->handle
= std
;
489 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
492 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(0)->handle
,
493 get_ioinfo(1)->handle
, get_ioinfo(2)->handle
);
495 memset(_iob
,0,3*sizeof(FILE));
496 for (i
= 0; i
< 3; i
++)
498 /* FILE structs for stdin/out/err are static and never deleted */
500 _iob
[i
]._tmpfname
= NULL
;
501 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
506 /* INTERNAL: Flush stdio file buffer */
507 static int flush_buffer(FILE* file
)
510 int cnt
=file
->_ptr
-file
->_base
;
511 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
512 file
->_flag
|= _IOERR
;
515 file
->_ptr
=file
->_base
;
516 file
->_cnt
=file
->_bufsiz
;
521 /* INTERNAL: Allocate stdio file buffer */
522 /*static*/ void alloc_buffer(FILE* file
)
524 file
->_base
= calloc(BUFSIZ
,1);
526 file
->_bufsiz
= BUFSIZ
;
527 file
->_flag
|= _IOMYBUF
;
529 file
->_base
= (char*)(&file
->_charbuf
);
531 file
->_bufsiz
= sizeof(file
->_charbuf
);
533 file
->_ptr
= file
->_base
;
537 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
538 static int int_to_base32(int num
, char *str
)
553 *p
= (num
& 31) + '0';
555 *p
+= ('a' - '0' - 10);
562 /* INTERNAL: wide character version of int_to_base32 */
563 static int int_to_base32_w(int num
, wchar_t *str
)
578 *p
= (num
& 31) + '0';
580 *p
+= ('a' - '0' - 10);
587 /* INTERNAL: Create a wide string from an ascii string */
588 wchar_t *msvcrt_wstrdupa(const char *str
)
590 const unsigned int len
= strlen(str
) + 1 ;
591 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
594 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
598 /*********************************************************************
599 * __iob_func(MSVCRT.@)
601 FILE * CDECL
__iob_func(void)
606 /*********************************************************************
609 int CDECL
_access(const char *filename
, int mode
)
611 DWORD attr
= GetFileAttributesA(filename
);
613 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
615 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
617 _dosmaperr(GetLastError());
620 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
622 _set_errno(ERROR_ACCESS_DENIED
);
628 /*********************************************************************
629 * _access_s (MSVCRT.@)
631 int CDECL
_access_s(const char *filename
, int mode
)
633 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
634 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
640 return _access(filename
, mode
);
643 /*********************************************************************
644 * _waccess (MSVCRT.@)
646 int CDECL
_waccess(const wchar_t *filename
, int mode
)
648 DWORD attr
= GetFileAttributesW(filename
);
650 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
652 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
654 _dosmaperr(GetLastError());
657 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
659 _set_errno(ERROR_ACCESS_DENIED
);
665 /*********************************************************************
666 * _waccess_s (MSVCRT.@)
668 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
670 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
671 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
677 return _waccess(filename
, mode
);
680 /*********************************************************************
683 int CDECL
_chmod(const char *path
, int flags
)
685 DWORD oldFlags
= GetFileAttributesA(path
);
687 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
689 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
690 oldFlags
| FILE_ATTRIBUTE_READONLY
;
692 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
695 _dosmaperr(GetLastError());
699 /*********************************************************************
702 int CDECL
_wchmod(const wchar_t *path
, int flags
)
704 DWORD oldFlags
= GetFileAttributesW(path
);
706 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
708 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
709 oldFlags
| FILE_ATTRIBUTE_READONLY
;
711 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
714 _dosmaperr(GetLastError());
718 /*********************************************************************
721 int CDECL
_unlink(const char *path
)
723 TRACE("%s\n",debugstr_a(path
));
724 if(DeleteFileA(path
))
726 TRACE("failed (%d)\n",GetLastError());
727 _dosmaperr(GetLastError());
731 /*********************************************************************
732 * _wunlink (MSVCRT.@)
734 int CDECL
_wunlink(const wchar_t *path
)
736 TRACE("(%s)\n",debugstr_w(path
));
737 if(DeleteFileW(path
))
739 TRACE("failed (%d)\n",GetLastError());
740 _dosmaperr(GetLastError());
744 /* _flushall calls fflush which calls _flushall */
745 int CDECL
fflush(FILE* file
);
747 /* INTERNAL: Flush all stream buffer */
748 static int flush_all_buffers(int mask
)
750 int i
, num_flushed
= 0;
754 for (i
= 3; i
< stream_idx
; i
++) {
759 if(file
->_flag
& mask
) {
767 TRACE(":flushed (%d) handles\n",num_flushed
);
771 /*********************************************************************
772 * _flushall (MSVCRT.@)
774 int CDECL
_flushall(void)
776 return flush_all_buffers(_IOWRT
| _IOREAD
);
779 /*********************************************************************
782 int CDECL
fflush(FILE* file
)
785 flush_all_buffers(_IOWRT
);
786 } else if(file
->_flag
& _IOWRT
) {
790 res
= flush_buffer(file
);
792 if(!res && (file->_flag & _IOCOMMIT))
793 res = _commit(file->_file) ? EOF : 0;
798 } else if(file
->_flag
& _IOREAD
) {
801 file
->_ptr
= file
->_base
;
809 /*********************************************************************
812 int CDECL
_close(int fd
)
819 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
820 if (hand
== INVALID_HANDLE_VALUE
)
822 else if (!CloseHandle(hand
))
824 WARN(":failed-last error (%d)\n",GetLastError());
825 _dosmaperr(GetLastError());
838 /*********************************************************************
841 int CDECL
_commit(int fd
)
843 HANDLE hand
= fdtoh(fd
);
845 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
846 if (hand
== INVALID_HANDLE_VALUE
)
849 if (!FlushFileBuffers(hand
))
851 if (GetLastError() == ERROR_INVALID_HANDLE
)
853 /* FlushFileBuffers fails for console handles
854 * so we ignore this error.
858 TRACE(":failed-last error (%d)\n",GetLastError());
859 _dosmaperr(GetLastError());
866 /*********************************************************************
869 * MSDN isn't clear on this point, but the remarks for _pipe
870 * indicate file descriptors duplicated with _dup and _dup2 are always
873 int CDECL
_dup2(int od
, int nd
)
877 TRACE("(od=%d, nd=%d)\n", od
, nd
);
879 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
883 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
884 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
886 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
890 ret
= alloc_fd_from(handle
, wxflag
, nd
);
898 /* _dup2 returns 0, not nd, on success */
905 _dosmaperr(GetLastError());
917 /*********************************************************************
920 int CDECL
_dup(int od
)
926 if (_dup2(od
, fd
) == 0)
934 /*********************************************************************
937 int CDECL
_eof(int fd
)
940 LONG hcurpos
,hendpos
;
941 HANDLE hand
= fdtoh(fd
);
943 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
945 if (hand
== INVALID_HANDLE_VALUE
)
948 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
950 /* Otherwise we do it the hard way */
951 hcurpos
= hendpos
= 0;
952 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
953 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
955 if (curpos
== endpos
&& hcurpos
== hendpos
)
957 /* FIXME: shouldn't WX_ATEOF be set here? */
961 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
965 /*********************************************************************
966 * _fcloseall (MSVCRT.@)
968 int CDECL
_fcloseall(void)
970 int num_closed
= 0, i
;
974 for (i
= 3; i
< stream_idx
; i
++) {
977 if (file
->_flag
&& !fclose(file
))
982 TRACE(":closed (%d) handles\n",num_closed
);
986 /* free everything on process exit */
987 void msvcrt_free_io(void)
992 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
993 * stdout, and stderr (unlike GNU), so we need to fclose() them here
994 * or they won't get flushed.
1000 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1003 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
1006 file_cs
.DebugInfo
->Spare
[0] = 0;
1007 DeleteCriticalSection(&file_cs
);
1010 /*********************************************************************
1011 * _lseeki64 (MSVCRT.@)
1013 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1015 HANDLE hand
= fdtoh(fd
);
1018 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1019 if (hand
== INVALID_HANDLE_VALUE
)
1022 if (whence
< 0 || whence
> 2)
1028 TRACE(":fd (%d) to %s pos %s\n",
1029 fd
,wine_dbgstr_longlong(offset
),
1030 (whence
==SEEK_SET
)?"SEEK_SET":
1031 (whence
==SEEK_CUR
)?"SEEK_CUR":
1032 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1034 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1035 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1036 ofs
.QuadPart
= offset
;
1037 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1038 GetLastError() == ERROR_SUCCESS
)
1040 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1041 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1043 return ofs
.QuadPart
;
1045 TRACE(":error-last error (%d)\n",GetLastError());
1046 _dosmaperr(GetLastError());
1050 /*********************************************************************
1053 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1055 return (LONG
)_lseeki64(fd
, offset
, whence
);
1058 /*********************************************************************
1059 * _lock_file (MSVCRT.@)
1061 void CDECL
_lock_file(FILE *file
)
1063 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1064 _lock(_STREAM_LOCKS
+(file
-_iob
));
1065 /* ReactOS: string streams dont need to be locked */
1066 else if(!(file
->_flag
& _IOSTRG
))
1067 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1070 /*********************************************************************
1071 * _unlock_file (MSVCRT.@)
1073 void CDECL
_unlock_file(FILE *file
)
1075 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1076 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1077 /* ReactOS: string streams dont need to be locked */
1078 else if(!(file
->_flag
& _IOSTRG
))
1079 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1083 /*********************************************************************
1084 * _locking (MSVCRT.@)
1086 * This is untested; the underlying LockFile doesn't work yet.
1088 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1092 HANDLE hand
= fdtoh(fd
);
1094 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1095 if (hand
== INVALID_HANDLE_VALUE
)
1098 if (mode
< 0 || mode
> 4)
1104 TRACE(":fd (%d) by 0x%08x mode %s\n",
1105 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1106 (mode
==_LK_LOCK
)?"_LK_LOCK":
1107 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1108 (mode
==_LK_RLCK
)?"_LK_RLCK":
1109 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1112 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1114 FIXME ("Seek failed\n");
1115 *_errno() = EINVAL
; /* FIXME */
1118 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1121 ret
= 1; /* just to satisfy gcc */
1124 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1129 else if (mode
== _LK_UNLCK
)
1130 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1132 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1133 /* FIXME - what about error settings? */
1134 return ret
? 0 : -1;
1137 /*********************************************************************
1138 * _fseeki64 (MSVCRT.@)
1140 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1145 /* Flush output if needed */
1146 if(file
->_flag
& _IOWRT
)
1149 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1150 offset
-= file
->_cnt
;
1151 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1152 /* Black magic correction for CR removal */
1154 for (i
=0; i
<file
->_cnt
; i
++) {
1155 if (file
->_ptr
[i
] == '\n')
1158 /* Black magic when reading CR at buffer boundary*/
1159 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1163 /* Discard buffered input */
1165 file
->_ptr
= file
->_base
;
1166 /* Reset direction of i/o */
1167 if(file
->_flag
& _IORW
) {
1168 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1170 /* Clear end of file flag */
1171 file
->_flag
&= ~_IOEOF
;
1172 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1178 /*********************************************************************
1181 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1183 return _fseeki64( file
, offset
, whence
);
1186 /*********************************************************************
1187 * _chsize (MSVCRT.@)
1189 int CDECL
_chsize(int fd
, long size
)
1195 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1200 if (handle
!= INVALID_HANDLE_VALUE
)
1202 /* save the current file pointer */
1203 cur
= _lseek(fd
, 0, SEEK_CUR
);
1206 pos
= _lseek(fd
, size
, SEEK_SET
);
1209 ret
= SetEndOfFile(handle
);
1210 if (!ret
) _dosmaperr(GetLastError());
1213 /* restore the file pointer */
1214 _lseek(fd
, cur
, SEEK_SET
);
1219 return ret
? 0 : -1;
1222 /*********************************************************************
1223 * clearerr (MSVCRT.@)
1225 void CDECL
clearerr(FILE* file
)
1227 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1230 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1234 /*********************************************************************
1237 void CDECL
rewind(FILE* file
)
1239 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1242 fseek(file
, 0L, SEEK_SET
);
1247 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1249 int plus
= strchrW(mode
, '+') != NULL
;
1251 while(*mode
== ' ') mode
++;
1256 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1257 *stream_flags
= plus
? _IORW
: _IOREAD
;
1260 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1261 *stream_flags
= plus
? _IORW
: _IOWRT
;
1264 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1265 *stream_flags
= plus
? _IORW
: _IOWRT
;
1268 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1274 /* *stream_flags |= MSVCRT__commode; */
1276 while (*mode
&& *mode
!=',')
1280 *open_flags
|= _O_BINARY
;
1281 *open_flags
&= ~_O_TEXT
;
1284 *open_flags
|= _O_TEXT
;
1285 *open_flags
&= ~_O_BINARY
;
1288 *open_flags
|= _O_TEMPORARY
;
1291 *open_flags
|= _O_SHORT_LIVED
;
1295 *stream_flags |= _IOCOMMIT;
1298 *stream_flags &= ~_IOCOMMIT;
1302 *open_flags
|= _O_NOINHERIT
;
1311 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1314 ERR("incorrect mode flag: %c\n", mode
[-1]);
1320 static const WCHAR ccs
[] = {'c','c','s'};
1321 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1322 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1323 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1326 while(*mode
== ' ') mode
++;
1327 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1329 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1330 while(*mode
== ' ') mode
++;
1331 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1334 while(*mode
== ' ') mode
++;
1336 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1338 *open_flags
|= _O_U8TEXT
;
1339 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1341 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1343 *open_flags
|= _O_U16TEXT
;
1344 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1346 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1348 *open_flags
|= _O_WTEXT
;
1349 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1353 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1358 while(*mode
== ' ') mode
++;
1361 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1367 /*********************************************************************
1368 * _fdopen (MSVCRT.@)
1370 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1373 wchar_t *modeW
= NULL
;
1375 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1377 ret
= _wfdopen(fd
, modeW
);
1383 /*********************************************************************
1384 * _wfdopen (MSVCRT.@)
1386 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1388 int open_flags
, stream_flags
;
1391 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1394 if (!(file
= alloc_fp()))
1396 else if (init_fp(file
, fd
, stream_flags
) == -1)
1401 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1407 /*********************************************************************
1408 * _filelength (MSVCRT.@)
1410 LONG CDECL
_filelength(int fd
)
1412 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1415 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1418 if (endPos
!= curPos
)
1419 _lseek(fd
, curPos
, SEEK_SET
);
1426 /*********************************************************************
1427 * _filelengthi64 (MSVCRT.@)
1429 __int64 CDECL
_filelengthi64(int fd
)
1431 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1434 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1437 if (endPos
!= curPos
)
1438 _lseeki64(fd
, curPos
, SEEK_SET
);
1445 /*********************************************************************
1446 * _fileno (MSVCRT.@)
1448 int CDECL
_fileno(FILE* file
)
1450 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1454 /*********************************************************************
1455 * _get_osfhandle (MSVCRT.@)
1457 intptr_t CDECL
_get_osfhandle(int fd
)
1459 HANDLE hand
= fdtoh(fd
);
1460 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1462 return (intptr_t)hand
;
1465 /*********************************************************************
1466 * _isatty (MSVCRT.@)
1468 int CDECL
_isatty(int fd
)
1470 HANDLE hand
= fdtoh(fd
);
1472 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1473 if (hand
== INVALID_HANDLE_VALUE
)
1476 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1479 /*********************************************************************
1480 * _mktemp (MSVCRT.@)
1482 char * CDECL
_mktemp(char *pattern
)
1485 char *retVal
= pattern
;
1490 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1494 id
= GetCurrentProcessId();
1498 int tempNum
= id
/ 10;
1499 *pattern
-- = id
- (tempNum
* 10) + '0';
1505 *pattern
= letter
++;
1506 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1507 GetLastError() == ERROR_FILE_NOT_FOUND
)
1509 } while(letter
<= 'z');
1513 /*********************************************************************
1514 * _wmktemp (MSVCRT.@)
1516 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1519 wchar_t *retVal
= pattern
;
1521 wchar_t letter
= 'a';
1524 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1528 id
= GetCurrentProcessId();
1532 int tempNum
= id
/ 10;
1533 *pattern
-- = id
- (tempNum
* 10) + '0';
1539 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1540 GetLastError() == ERROR_FILE_NOT_FOUND
)
1542 *pattern
= letter
++;
1543 } while(letter
!= '|');
1547 /*static*/ unsigned split_oflags(unsigned oflags
)
1550 unsigned unsupp
; /* until we support everything */
1552 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1553 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1554 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1555 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1556 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1557 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1558 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1559 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1560 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1562 if ((unsupp
= oflags
& ~(
1563 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1564 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1565 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1567 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1568 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1570 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1575 /*********************************************************************
1578 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1581 SECURITY_ATTRIBUTES sa
;
1582 HANDLE readHandle
, writeHandle
;
1590 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1591 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1592 sa
.lpSecurityDescriptor
= NULL
;
1593 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1595 unsigned int wxflags
= split_oflags(textmode
);
1599 fd
= alloc_fd(readHandle
, wxflags
);
1603 fd
= alloc_fd(writeHandle
, wxflags
);
1612 CloseHandle(writeHandle
);
1618 CloseHandle(readHandle
);
1619 CloseHandle(writeHandle
);
1625 _dosmaperr(GetLastError());
1630 /*********************************************************************
1631 * _sopen_s (MSVCRT.@)
1633 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1635 DWORD access
= 0, creation
= 0, attrib
;
1639 SECURITY_ATTRIBUTES sa
;
1641 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1642 fd
, path
, oflags
, shflags
, pmode
);
1646 MSVCRT_INVALID_PMT("null out fd pointer");
1652 wxflag
= split_oflags(oflags
);
1653 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1655 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1656 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1657 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1660 if (oflags
& _O_CREAT
)
1662 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1663 FIXME(": pmode 0x%04x ignored\n", pmode
);
1665 WARN(": pmode 0x%04x ignored\n", pmode
);
1667 if (oflags
& _O_EXCL
)
1668 creation
= CREATE_NEW
;
1669 else if (oflags
& _O_TRUNC
)
1670 creation
= CREATE_ALWAYS
;
1672 creation
= OPEN_ALWAYS
;
1674 else /* no _O_CREAT */
1676 if (oflags
& _O_TRUNC
)
1677 creation
= TRUNCATE_EXISTING
;
1679 creation
= OPEN_EXISTING
;
1688 sharing
= FILE_SHARE_READ
;
1691 sharing
= FILE_SHARE_WRITE
;
1694 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1697 ERR( "Unhandled shflags 0x%x\n", shflags
);
1700 attrib
= FILE_ATTRIBUTE_NORMAL
;
1702 if (oflags
& _O_TEMPORARY
)
1704 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1706 sharing
|= FILE_SHARE_DELETE
;
1709 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1710 sa
.lpSecurityDescriptor
= NULL
;
1711 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1713 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1714 if (hand
== INVALID_HANDLE_VALUE
) {
1715 WARN(":failed-last error (%d)\n", GetLastError());
1716 _dosmaperr(GetLastError());
1720 *fd
= alloc_fd(hand
, wxflag
);
1722 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1726 /*********************************************************************
1729 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1734 if (oflags
& _O_CREAT
)
1738 va_start(ap
, shflags
);
1739 pmode
= va_arg(ap
, int);
1745 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1749 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1751 char bom
[sizeof(utf8_bom
)];
1754 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1756 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1759 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1760 oflags
|= _O_U8TEXT
;
1761 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1763 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1764 oflags
|= _O_U16TEXT
;
1766 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1772 /*********************************************************************
1773 * _wsopen_s (MSVCRT.@)
1775 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1777 DWORD access
= 0, creation
= 0, attrib
;
1778 SECURITY_ATTRIBUTES sa
;
1783 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1784 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1788 MSVCRT_INVALID_PMT("null out fd pointer");
1794 wxflag
= split_oflags(oflags
);
1795 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1797 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1798 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1799 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1802 if (oflags
& _O_CREAT
)
1804 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1805 FIXME(": pmode 0x%04x ignored\n", pmode
);
1807 WARN(": pmode 0x%04x ignored\n", pmode
);
1809 if (oflags
& _O_EXCL
)
1810 creation
= CREATE_NEW
;
1811 else if (oflags
& _O_TRUNC
)
1812 creation
= CREATE_ALWAYS
;
1814 creation
= OPEN_ALWAYS
;
1816 else /* no _O_CREAT */
1818 if (oflags
& _O_TRUNC
)
1819 creation
= TRUNCATE_EXISTING
;
1821 creation
= OPEN_EXISTING
;
1830 sharing
= FILE_SHARE_READ
;
1833 sharing
= FILE_SHARE_WRITE
;
1836 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1839 ERR( "Unhandled shflags 0x%x\n", shflags
);
1842 attrib
= FILE_ATTRIBUTE_NORMAL
;
1844 if (oflags
& _O_TEMPORARY
)
1846 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1848 sharing
|= FILE_SHARE_DELETE
;
1851 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1852 sa
.lpSecurityDescriptor
= NULL
;
1853 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1855 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1856 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1857 && !(access
&GENERIC_READ
))
1859 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1860 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1861 if (hand
!= INVALID_HANDLE_VALUE
)
1863 oflags
= check_bom(hand
, oflags
, FALSE
);
1867 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1870 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1872 if (hand
== INVALID_HANDLE_VALUE
) {
1873 WARN(":failed-last error (%d)\n",GetLastError());
1874 _dosmaperr(GetLastError());
1878 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1880 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1881 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1882 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1884 if (oflags
& _O_U8TEXT
)
1886 DWORD written
= 0, tmp
;
1888 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1889 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1891 if (written
!= sizeof(utf8_bom
)) {
1892 WARN("error writing BOM\n");
1894 *_errno() = GetLastError();
1900 DWORD written
= 0, tmp
;
1902 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1903 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1905 if (written
!= sizeof(utf16_bom
))
1907 WARN("error writing BOM\n");
1909 *_errno() = GetLastError();
1914 else if (access
& GENERIC_READ
)
1915 oflags
= check_bom(hand
, oflags
, TRUE
);
1918 *fd
= alloc_fd(hand
, wxflag
);
1922 if (oflags
& _O_WTEXT
)
1923 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1924 else if (oflags
& _O_U16TEXT
)
1925 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1926 else if (oflags
& _O_U8TEXT
)
1927 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1929 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1933 /*********************************************************************
1934 * _wsopen (MSVCRT.@)
1936 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1941 if (oflags
& _O_CREAT
)
1945 va_start(ap
, shflags
);
1946 pmode
= va_arg(ap
, int);
1952 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1956 /*********************************************************************
1959 int CDECL
_open( const char *path
, int flags
, ... )
1963 if (flags
& _O_CREAT
)
1966 va_start(ap
, flags
);
1967 pmode
= va_arg(ap
, int);
1969 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1972 return _sopen( path
, flags
, _SH_DENYNO
);
1975 /*********************************************************************
1978 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1982 if (flags
& _O_CREAT
)
1985 va_start(ap
, flags
);
1986 pmode
= va_arg(ap
, int);
1988 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1991 return _wsopen( path
, flags
, _SH_DENYNO
);
1994 /*********************************************************************
1997 int CDECL
_creat(const char *path
, int flags
)
1999 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2000 return _open(path
, usedFlags
);
2003 /*********************************************************************
2004 * _wcreat (MSVCRT.@)
2006 int CDECL
_wcreat(const wchar_t *path
, int flags
)
2008 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2009 return _wopen(path
, usedFlags
);
2012 /*********************************************************************
2013 * _open_osfhandle (MSVCRT.@)
2015 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2019 /* _O_RDONLY (0) always matches, so set the read flag
2020 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2021 * file, so set the write flag. It also only sets _O_TEXT if it wants
2022 * text - it never sets _O_BINARY.
2024 /* don't let split_oflags() decide the mode if no mode is passed */
2025 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2026 oflags
|= _O_BINARY
;
2028 fd
= alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
2029 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
2033 /*********************************************************************
2036 int CDECL
_rmtmp(void)
2038 int num_removed
= 0, i
;
2042 for (i
= 3; i
< stream_idx
; i
++) {
2045 if (file
->_tmpfname
)
2054 TRACE(":removed (%d) temp files\n",num_removed
);
2058 /*********************************************************************
2061 * When reading \r as last character in text mode, read() positions
2062 * the file pointer on the \r character while getc() goes on to
2065 static int read_i(int fd
, void *buf
, unsigned int count
)
2068 char *bufstart
= buf
;
2069 HANDLE hand
= fdtoh(fd
);
2070 ioinfo
*fdinfo
= get_ioinfo(fd
);
2075 if (fdinfo
->wxflag
& WX_READEOF
) {
2076 fdinfo
->wxflag
|= WX_ATEOF
;
2077 TRACE("already at EOF, returning 0\n");
2080 /* Don't trace small reads, it gets *very* annoying */
2082 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2083 if (hand
== INVALID_HANDLE_VALUE
)
2086 /* Reading single bytes in O_TEXT mode makes things slow
2087 * So read big chunks
2089 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2091 if (count
!= 0 && num_read
== 0)
2093 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2094 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2096 else if (fdinfo
->wxflag
& WX_TEXT
)
2099 if (bufstart
[num_read
-1] == '\r')
2103 fdinfo
->wxflag
&= ~WX_READCR
;
2104 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
2108 fdinfo
->wxflag
|= WX_READCR
;
2113 fdinfo
->wxflag
&= ~WX_READCR
;
2114 for (i
=0, j
=0; i
<num_read
; i
++)
2116 /* in text mode, a ctrl-z signals EOF */
2117 if (bufstart
[i
] == 0x1a)
2119 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2120 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2123 /* in text mode, strip \r if followed by \n.
2124 * BUG: should save state across calls somehow, so CR LF that
2125 * straddles buffer boundary gets recognized properly?
2127 if ((bufstart
[i
] != '\r')
2128 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
2129 bufstart
[j
++] = bufstart
[i
];
2136 if (GetLastError() == ERROR_BROKEN_PIPE
)
2138 TRACE(":end-of-pipe\n");
2139 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2144 TRACE(":failed-last error (%d)\n",GetLastError());
2150 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2154 /*********************************************************************
2157 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2160 num_read
= read_i(fd
, buf
, count
);
2164 /*********************************************************************
2165 * _setmode (MSVCRT.@)
2167 int CDECL
_setmode(int fd
,int mode
)
2169 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2170 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
2171 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
2172 if ((mode
& _O_TEXT
) == _O_TEXT
)
2173 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2175 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2179 /*********************************************************************
2182 long CDECL
_tell(int fd
)
2184 return _lseek(fd
, 0, SEEK_CUR
);
2187 /*********************************************************************
2188 * _telli64 (MSVCRT.@)
2190 __int64 CDECL
_telli64(int fd
)
2192 return _lseeki64(fd
, 0, SEEK_CUR
);
2195 /*********************************************************************
2196 * _tempnam (MSVCRT.@)
2198 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2200 char tmpbuf
[MAX_PATH
];
2201 const char *tmp_dir
= getenv("TMP");
2203 if (tmp_dir
) dir
= tmp_dir
;
2205 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2206 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2208 TRACE("got name (%s)\n",tmpbuf
);
2209 DeleteFileA(tmpbuf
);
2210 return _strdup(tmpbuf
);
2212 TRACE("failed (%d)\n",GetLastError());
2216 /*********************************************************************
2217 * _wtempnam (MSVCRT.@)
2219 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2221 wchar_t tmpbuf
[MAX_PATH
];
2223 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2224 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2226 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2227 DeleteFileW(tmpbuf
);
2228 return _wcsdup(tmpbuf
);
2230 TRACE("failed (%d)\n",GetLastError());
2234 /*********************************************************************
2237 int CDECL
_umask(int umask
)
2239 int old_umask
= umask
;
2240 TRACE("(%d)\n",umask
);
2241 MSVCRT_umask
= umask
;
2245 /*********************************************************************
2248 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2251 HANDLE hand
= fdtoh(fd
);
2253 /* Don't trace small writes, it gets *very* annoying */
2256 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2258 if (hand
== INVALID_HANDLE_VALUE
)
2264 /* If appending, go to EOF */
2265 if (get_ioinfo(fd
)->wxflag
& WX_APPEND
)
2266 _lseek(fd
, 0, FILE_END
);
2268 if (!(get_ioinfo(fd
)->wxflag
& WX_TEXT
))
2270 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2271 && (num_written
== count
))
2273 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2274 hand
, GetLastError());
2279 unsigned int i
, j
, nr_lf
;
2282 const char *s
= buf
, *buf_start
= buf
;
2283 /* find number of \n ( without preceding \r ) */
2284 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2289 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2294 if ((q
= p
= malloc(count
+ nr_lf
)))
2296 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2301 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2308 FIXME("Malloc failed\n");
2316 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2318 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2319 fd
, hand
, GetLastError(), num_written
);
2323 return s
- buf_start
;
2335 /*********************************************************************
2338 int CDECL
_putw(int val
, FILE* file
)
2343 len
= _write(file
->_file
, &val
, sizeof(val
));
2344 if (len
== sizeof(val
)) {
2349 file
->_flag
|= _IOERR
;
2354 /*********************************************************************
2357 int CDECL
fclose(FILE* file
)
2363 free(file
->_tmpfname
);
2364 file
->_tmpfname
= NULL
;
2365 /* flush stdio buffers */
2366 if(file
->_flag
& _IOWRT
)
2368 if(file
->_flag
& _IOMYBUF
)
2371 r
=_close(file
->_file
);
2375 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2376 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2378 if(file
== get_file(stream_idx
-1)) {
2379 while(stream_idx
>3 && !file
->_flag
) {
2381 file
= get_file(stream_idx
-1);
2385 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2388 /*********************************************************************
2391 int CDECL
feof(FILE* file
)
2396 ret
= file
->_flag
& _IOEOF
;
2402 /*********************************************************************
2405 int CDECL
ferror(FILE* file
)
2410 ret
= file
->_flag
& _IOERR
;
2416 /*********************************************************************
2417 * _filbuf (MSVCRT.@)
2419 int CDECL
_filbuf(FILE* file
)
2424 /* Allocate buffer if needed */
2425 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2428 if(!(file
->_flag
& _IOREAD
)) {
2429 if(file
->_flag
& _IORW
)
2430 file
->_flag
|= _IOREAD
;
2437 if(file
->_flag
& _IONBF
) {
2439 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2440 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2448 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2450 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2457 file
->_ptr
= file
->_base
+1;
2458 c
= *(unsigned char *)file
->_base
;
2464 /*********************************************************************
2467 int CDECL
fgetc(FILE* file
)
2475 i
= (unsigned char *)file
->_ptr
++;
2484 /*********************************************************************
2485 * _fgetchar (MSVCRT.@)
2487 int CDECL
_fgetchar(void)
2489 return fgetc(stdin
);
2492 /*********************************************************************
2495 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2498 char * buf_start
= s
;
2500 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2501 file
,file
->_file
,s
,size
);
2505 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2510 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2512 TRACE(":nothing read\n");
2516 if ((cc
!= EOF
) && (size
> 1))
2519 TRACE(":got %s\n", debugstr_a(buf_start
));
2524 /*********************************************************************
2527 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2528 * the CR from CR/LF combinations
2530 wint_t CDECL
fgetwc(FILE* file
)
2535 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2542 for(i
=0; i
<sizeof(wc
); i
++)
2555 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2570 if ((__mb_cur_max
> 1) && isleadbyte(c
))
2572 FIXME("Treat Multibyte characters\n");
2582 /*********************************************************************
2585 int CDECL
_getw(FILE* file
)
2593 for (j
=0; j
<sizeof(int); j
++) {
2596 file
->_flag
|= _IOEOF
;
2607 /*********************************************************************
2610 wint_t CDECL
getwc(FILE* file
)
2612 return fgetwc(file
);
2615 /*********************************************************************
2616 * _fgetwchar (MSVCRT.@)
2618 wint_t CDECL
_fgetwchar(void)
2620 return fgetwc(stdin
);
2623 /*********************************************************************
2624 * getwchar (MSVCRT.@)
2626 wint_t CDECL
getwchar(void)
2628 return _fgetwchar();
2631 /*********************************************************************
2634 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2637 wchar_t * buf_start
= s
;
2639 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2640 file
,file
->_file
,s
,size
);
2644 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2649 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2651 TRACE(":nothing read\n");
2655 if ((cc
!= WEOF
) && (size
> 1))
2658 TRACE(":got %s\n", debugstr_w(buf_start
));
2663 /*********************************************************************
2666 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2668 size_t wrcnt
=size
* nmemb
;
2675 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2676 memcpy(file
->_ptr
, ptr
, pcnt
);
2681 ptr
= (const char*)ptr
+ pcnt
;
2682 } else if(!(file
->_flag
& _IOWRT
)) {
2683 if(file
->_flag
& _IORW
) {
2684 file
->_flag
|= _IOWRT
;
2692 int res
=flush_buffer(file
);
2694 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2697 file
->_flag
|= _IOERR
;
2700 written
+= pwritten
;
2705 return written
/ size
;
2708 /*********************************************************************
2710 * FORKED for ReactOS, don't sync with Wine!
2712 * - http://jira.reactos.org/browse/CORE-6495
2713 * - http://bugs.winehq.org/show_bug.cgi?id=8598
2715 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2717 /* If this is a real file stream (and not some temporary one for
2718 sprintf-like functions), check whether it is opened in text mode.
2719 In this case, we have to perform an implicit conversion to ANSI. */
2720 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
2722 /* Convert to multibyte in text mode */
2723 char mbc
[MB_LEN_MAX
];
2726 mb_return
= wctomb(mbc
, c
);
2731 /* Output all characters */
2732 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2737 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2744 /*********************************************************************
2745 * _fputwchar (MSVCRT.@)
2747 wint_t CDECL
_fputwchar(wint_t wc
)
2749 return fputwc(wc
, stdout
);
2752 /*********************************************************************
2753 * _wfsopen (MSVCRT.@)
2755 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2758 int open_flags
, stream_flags
, fd
;
2760 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2762 /* map mode string to open() flags. "man fopen" for possibilities. */
2763 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2767 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2770 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2772 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2779 TRACE(":got (%p)\n",file
);
2780 if (fd
>= 0 && !file
)
2786 /*********************************************************************
2787 * _fsopen (MSVCRT.@)
2789 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2792 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2794 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2795 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2799 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2802 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2807 ret
= _wfsopen(pathW
, modeW
, share
);
2814 /*********************************************************************
2817 FILE * CDECL
fopen(const char *path
, const char *mode
)
2819 return _fsopen( path
, mode
, _SH_DENYNO
);
2822 /*********************************************************************
2823 * fopen_s (MSVCRT.@)
2825 int CDECL
fopen_s(FILE** pFile
,
2826 const char *filename
, const char *mode
)
2828 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2829 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2834 *pFile
= fopen(filename
, mode
);
2841 /*********************************************************************
2842 * _wfopen (MSVCRT.@)
2844 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2846 return _wfsopen( path
, mode
, _SH_DENYNO
);
2849 /*********************************************************************
2850 * _wfopen_s (MSVCRT.@)
2852 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
2853 const wchar_t *mode
)
2855 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2856 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2861 *pFile
= _wfopen(filename
, mode
);
2868 /* fputc calls _flsbuf which calls fputc */
2869 int CDECL
_flsbuf(int c
, FILE* file
);
2871 /*********************************************************************
2874 int CDECL
fputc(int c
, FILE* file
)
2884 res
= flush_buffer(file
);
2886 return res
? res
: c
;
2893 res
= _flsbuf(c
, file
);
2899 /*********************************************************************
2900 * _fputchar (MSVCRT.@)
2902 int CDECL
_fputchar(int c
)
2904 return fputc(c
, stdout
);
2907 /*********************************************************************
2910 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2912 size_t rcnt
=size
* nmemb
;
2921 /* first buffered data */
2923 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
2924 memcpy(ptr
, file
->_ptr
, pcnt
);
2929 ptr
= (char*)ptr
+ pcnt
;
2930 } else if(!(file
->_flag
& _IOREAD
)) {
2931 if(file
->_flag
& _IORW
) {
2932 file
->_flag
|= _IOREAD
;
2941 /* Fill the buffer on small reads.
2942 * TODO: Use a better buffering strategy.
2944 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2945 if (file
->_bufsiz
== 0) {
2948 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2949 file
->_ptr
= file
->_base
;
2950 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2951 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2952 if (i
> 0 && i
< file
->_cnt
) {
2953 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
2954 file
->_flag
&= ~_IOEOF
;
2957 memcpy(ptr
, file
->_ptr
, i
);
2962 i
= _read(file
->_file
,ptr
, rcnt
);
2966 ptr
= (char *)ptr
+i
;
2967 /* expose feof condition in the flags
2968 * MFC tests file->_flag for feof, and doesn't call feof())
2970 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
2971 file
->_flag
|= _IOEOF
;
2974 file
->_flag
|= _IOERR
;
2985 /*********************************************************************
2986 * _wfreopen (MSVCRT.@)
2989 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
2991 int open_flags
, stream_flags
, fd
;
2993 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2996 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3001 /* map mode string to open() flags. "man fopen" for possibilities. */
3002 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3006 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3009 else if (init_fp(file
, fd
, stream_flags
) == -1)
3012 WARN(":failed-last error (%d)\n",GetLastError());
3013 _dosmaperr(GetLastError());
3022 /*********************************************************************
3023 * freopen (MSVCRT.@)
3026 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3029 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3031 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3032 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3038 ret
= _wfreopen(pathW
, modeW
, file
);
3045 /*********************************************************************
3046 * fsetpos (MSVCRT.@)
3048 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3053 /* Note that all this has been lifted 'as is' from fseek */
3054 if(file
->_flag
& _IOWRT
)
3057 /* Discard buffered input */
3059 file
->_ptr
= file
->_base
;
3061 /* Reset direction of i/o */
3062 if(file
->_flag
& _IORW
) {
3063 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3066 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3071 /*********************************************************************
3072 * _ftelli64 (MSVCRT.@)
3074 __int64 CDECL
_ftelli64(FILE* file
)
3076 /* TODO: just call fgetpos and return lower half of result */
3081 pos
= _telli64(file
->_file
);
3087 if( file
->_flag
& _IOWRT
) {
3088 off
= file
->_ptr
- file
->_base
;
3091 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3092 /* Black magic correction for CR removal */
3094 for (i
=0; i
<file
->_cnt
; i
++) {
3095 if (file
->_ptr
[i
] == '\n')
3098 /* Black magic when reading CR at buffer boundary*/
3099 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3109 /*********************************************************************
3112 LONG CDECL
ftell(FILE* file
)
3114 return (LONG
)_ftelli64(file
);
3117 /*********************************************************************
3118 * fgetpos (MSVCRT.@)
3120 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3125 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
3131 if( file
->_flag
& _IOWRT
) {
3132 off
= file
->_ptr
- file
->_base
;
3135 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3136 /* Black magic correction for CR removal */
3138 for (i
=0; i
<file
->_cnt
; i
++) {
3139 if (file
->_ptr
[i
] == '\n')
3142 /* Black magic when reading CR at buffer boundary*/
3143 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3153 /*********************************************************************
3156 int CDECL
fputs(const char *s
, FILE* file
)
3158 size_t i
, len
= strlen(s
);
3162 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3163 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3167 for (i
=0; i
<len
; i
++)
3168 if (fputc(s
[i
], file
) == EOF
) {
3177 /*********************************************************************
3180 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3182 size_t i
, len
= strlenW(s
);
3186 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3187 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3191 for (i
=0; i
<len
; i
++) {
3192 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
3193 || fputwc(s
[i
], file
) == WEOF
) {
3203 /*********************************************************************
3204 * getchar (MSVCRT.@)
3206 int CDECL
getchar(void)
3208 return fgetc(stdin
);
3211 /*********************************************************************
3214 int CDECL
getc(FILE* file
)
3219 /*********************************************************************
3222 char * CDECL
gets(char *buf
)
3225 char * buf_start
= buf
;
3228 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3230 if(cc
!= '\r') *buf
++ = (char)cc
;
3234 TRACE("got '%s'\n", buf_start
);
3235 _unlock_file(stdin
);
3239 /*********************************************************************
3242 wchar_t* CDECL
_getws(wchar_t* buf
)
3248 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3252 *buf
++ = (wchar_t)cc
;
3256 TRACE("got %s\n", debugstr_w(ws
));
3257 _unlock_file(stdin
);
3261 /*********************************************************************
3264 int CDECL
putc(int c
, FILE* file
)
3266 return fputc(c
, file
);
3269 /*********************************************************************
3270 * putchar (MSVCRT.@)
3272 int CDECL
putchar(int c
)
3274 return fputc(c
, stdout
);
3277 /*********************************************************************
3278 * _putwch (MSVCRT.@)
3280 wint_t CDECL
_putwch(wchar_t c
)
3282 return fputwc(c
, stdout
);
3285 /*********************************************************************
3288 int CDECL
puts(const char *s
)
3290 size_t len
= strlen(s
);
3294 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3295 _unlock_file(stdout
);
3299 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3300 _unlock_file(stdout
);
3304 /*********************************************************************
3307 int CDECL
_putws(const wchar_t *s
)
3309 static const wchar_t nl
= '\n';
3310 size_t len
= strlenW(s
);
3314 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3315 _unlock_file(stdout
);
3319 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3320 _unlock_file(stdout
);
3324 /*********************************************************************
3327 int CDECL
remove(const char *path
)
3329 TRACE("(%s)\n",path
);
3330 if (DeleteFileA(path
))
3332 TRACE(":failed (%d)\n",GetLastError());
3333 _dosmaperr(GetLastError());
3337 /*********************************************************************
3338 * _wremove (MSVCRT.@)
3340 int CDECL
_wremove(const wchar_t *path
)
3342 TRACE("(%s)\n",debugstr_w(path
));
3343 if (DeleteFileW(path
))
3345 TRACE(":failed (%d)\n",GetLastError());
3346 _dosmaperr(GetLastError());
3350 /*********************************************************************
3353 int CDECL
rename(const char *oldpath
,const char *newpath
)
3355 TRACE(":from %s to %s\n",oldpath
,newpath
);
3356 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3358 TRACE(":failed (%d)\n",GetLastError());
3359 _dosmaperr(GetLastError());
3363 /*********************************************************************
3364 * _wrename (MSVCRT.@)
3366 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3368 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3369 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3371 TRACE(":failed (%d)\n",GetLastError());
3372 _dosmaperr(GetLastError());
3376 /*********************************************************************
3377 * setvbuf (MSVCRT.@)
3379 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3387 if(mode
== _IOFBF
) {
3388 file
->_flag
&= ~_IONBF
;
3389 file
->_base
= file
->_ptr
= buf
;
3391 file
->_bufsiz
= size
;
3394 file
->_flag
|= _IONBF
;
3400 /*********************************************************************
3403 void CDECL
setbuf(FILE* file
, char *buf
)
3405 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3408 /*********************************************************************
3411 char * CDECL
tmpnam(char *s
)
3418 thread_data_t
*data
= msvcrt_get_thread_data();
3420 if(!data
->tmpnam_buffer
)
3421 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3423 s
= data
->tmpnam_buffer
;
3426 int_to_base32(GetCurrentProcessId(), tmpstr
);
3427 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3428 for (count
= 0; count
< TMP_MAX
; count
++)
3430 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3431 memcpy(p
, tmpstr
, size
);
3432 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3433 GetLastError() == ERROR_FILE_NOT_FOUND
)
3439 /*********************************************************************
3440 * _wtmpnam (MSVCRT.@)
3442 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3444 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3449 thread_data_t
*data
= msvcrt_get_thread_data();
3451 if(!data
->wtmpnam_buffer
)
3452 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3454 s
= data
->wtmpnam_buffer
;
3457 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3458 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3459 for (count
= 0; count
< TMP_MAX
; count
++)
3461 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3462 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3463 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3464 GetLastError() == ERROR_FILE_NOT_FOUND
)
3470 /*********************************************************************
3471 * tmpfile (MSVCRT.@)
3473 FILE* CDECL
tmpfile(void)
3475 char *filename
= tmpnam(NULL
);
3480 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
3481 if (fd
!= -1 && (file
= alloc_fp()))
3483 if (init_fp(file
, fd
, _O_RDWR
) == -1)
3488 else file
->_tmpfname
= _strdup(filename
);
3494 /*********************************************************************
3497 int CDECL
ungetc(int c
, FILE * file
)
3503 if(file
->_bufsiz
== 0) {
3507 if(file
->_ptr
>file
->_base
) {
3520 /*********************************************************************
3521 * ungetwc (MSVCRT.@)
3523 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3526 char * pp
= (char *)&mwc
;
3530 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3531 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3543 /*********************************************************************
3544 * _getmaxstdio (MSVCRT.@)
3546 int CDECL
_getmaxstdio(void)
3551 /*********************************************************************
3552 * _setmaxstdio (MSVCRT.@)
3554 int CDECL
_setmaxstdio(int newmax
)
3556 TRACE("%d\n", newmax
);
3558 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3561 max_streams
= newmax
;