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 (!is_valid_fd(fd
)) {
824 ret
= CloseHandle(hand
) ? 0 : -1;
826 WARN(":failed-last error (%d)\n",GetLastError());
827 _dosmaperr(GetLastError());
835 /*********************************************************************
838 int CDECL
_commit(int fd
)
840 HANDLE hand
= fdtoh(fd
);
842 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
843 if (hand
== INVALID_HANDLE_VALUE
)
846 if (!FlushFileBuffers(hand
))
848 if (GetLastError() == ERROR_INVALID_HANDLE
)
850 /* FlushFileBuffers fails for console handles
851 * so we ignore this error.
855 TRACE(":failed-last error (%d)\n",GetLastError());
856 _dosmaperr(GetLastError());
863 /*********************************************************************
866 * MSDN isn't clear on this point, but the remarks for _pipe
867 * indicate file descriptors duplicated with _dup and _dup2 are always
870 int CDECL
_dup2(int od
, int nd
)
874 TRACE("(od=%d, nd=%d)\n", od
, nd
);
876 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
880 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
881 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
883 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
887 ret
= alloc_fd_from(handle
, wxflag
, nd
);
895 /* _dup2 returns 0, not nd, on success */
902 _dosmaperr(GetLastError());
914 /*********************************************************************
917 int CDECL
_dup(int od
)
923 if (_dup2(od
, fd
) == 0)
931 /*********************************************************************
934 int CDECL
_eof(int fd
)
937 LONG hcurpos
,hendpos
;
938 HANDLE hand
= fdtoh(fd
);
940 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
942 if (hand
== INVALID_HANDLE_VALUE
)
945 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
947 /* Otherwise we do it the hard way */
948 hcurpos
= hendpos
= 0;
949 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
950 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
952 if (curpos
== endpos
&& hcurpos
== hendpos
)
954 /* FIXME: shouldn't WX_ATEOF be set here? */
958 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
962 /*********************************************************************
963 * _fcloseall (MSVCRT.@)
965 int CDECL
_fcloseall(void)
967 int num_closed
= 0, i
;
971 for (i
= 3; i
< stream_idx
; i
++) {
974 if (file
->_flag
&& !fclose(file
))
979 TRACE(":closed (%d) handles\n",num_closed
);
983 /* free everything on process exit */
984 void msvcrt_free_io(void)
989 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
990 * stdout, and stderr (unlike GNU), so we need to fclose() them here
991 * or they won't get flushed.
997 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1000 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
1003 file_cs
.DebugInfo
->Spare
[0] = 0;
1004 DeleteCriticalSection(&file_cs
);
1007 /*********************************************************************
1008 * _lseeki64 (MSVCRT.@)
1010 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1012 HANDLE hand
= fdtoh(fd
);
1015 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1016 if (hand
== INVALID_HANDLE_VALUE
)
1019 if (whence
< 0 || whence
> 2)
1025 TRACE(":fd (%d) to %s pos %s\n",
1026 fd
,wine_dbgstr_longlong(offset
),
1027 (whence
==SEEK_SET
)?"SEEK_SET":
1028 (whence
==SEEK_CUR
)?"SEEK_CUR":
1029 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1031 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1032 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1033 ofs
.QuadPart
= offset
;
1034 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1035 GetLastError() == ERROR_SUCCESS
)
1037 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1038 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1040 return ofs
.QuadPart
;
1042 TRACE(":error-last error (%d)\n",GetLastError());
1043 _dosmaperr(GetLastError());
1047 /*********************************************************************
1050 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1052 return (LONG
)_lseeki64(fd
, offset
, whence
);
1055 /*********************************************************************
1056 * _lock_file (MSVCRT.@)
1058 void CDECL
_lock_file(FILE *file
)
1060 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1061 _lock(_STREAM_LOCKS
+(file
-_iob
));
1062 /* ReactOS: string streams dont need to be locked */
1063 else if(!(file
->_flag
& _IOSTRG
))
1064 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1067 /*********************************************************************
1068 * _unlock_file (MSVCRT.@)
1070 void CDECL
_unlock_file(FILE *file
)
1072 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1073 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1074 /* ReactOS: string streams dont need to be locked */
1075 else if(!(file
->_flag
& _IOSTRG
))
1076 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1080 /*********************************************************************
1081 * _locking (MSVCRT.@)
1083 * This is untested; the underlying LockFile doesn't work yet.
1085 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1089 HANDLE hand
= fdtoh(fd
);
1091 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1092 if (hand
== INVALID_HANDLE_VALUE
)
1095 if (mode
< 0 || mode
> 4)
1101 TRACE(":fd (%d) by 0x%08x mode %s\n",
1102 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1103 (mode
==_LK_LOCK
)?"_LK_LOCK":
1104 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1105 (mode
==_LK_RLCK
)?"_LK_RLCK":
1106 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1109 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1111 FIXME ("Seek failed\n");
1112 *_errno() = EINVAL
; /* FIXME */
1115 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1118 ret
= 1; /* just to satisfy gcc */
1121 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1126 else if (mode
== _LK_UNLCK
)
1127 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1129 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1130 /* FIXME - what about error settings? */
1131 return ret
? 0 : -1;
1134 /*********************************************************************
1135 * _fseeki64 (MSVCRT.@)
1137 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1142 /* Flush output if needed */
1143 if(file
->_flag
& _IOWRT
)
1146 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1147 offset
-= file
->_cnt
;
1148 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1149 /* Black magic correction for CR removal */
1151 for (i
=0; i
<file
->_cnt
; i
++) {
1152 if (file
->_ptr
[i
] == '\n')
1155 /* Black magic when reading CR at buffer boundary*/
1156 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1160 /* Discard buffered input */
1162 file
->_ptr
= file
->_base
;
1163 /* Reset direction of i/o */
1164 if(file
->_flag
& _IORW
) {
1165 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1167 /* Clear end of file flag */
1168 file
->_flag
&= ~_IOEOF
;
1169 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1175 /*********************************************************************
1178 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1180 return _fseeki64( file
, offset
, whence
);
1183 /*********************************************************************
1184 * _chsize (MSVCRT.@)
1186 int CDECL
_chsize(int fd
, long size
)
1192 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1197 if (handle
!= INVALID_HANDLE_VALUE
)
1199 /* save the current file pointer */
1200 cur
= _lseek(fd
, 0, SEEK_CUR
);
1203 pos
= _lseek(fd
, size
, SEEK_SET
);
1206 ret
= SetEndOfFile(handle
);
1207 if (!ret
) _dosmaperr(GetLastError());
1210 /* restore the file pointer */
1211 _lseek(fd
, cur
, SEEK_SET
);
1216 return ret
? 0 : -1;
1219 /*********************************************************************
1220 * clearerr (MSVCRT.@)
1222 void CDECL
clearerr(FILE* file
)
1224 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1227 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1231 /*********************************************************************
1234 void CDECL
rewind(FILE* file
)
1236 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1239 fseek(file
, 0L, SEEK_SET
);
1244 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1246 int plus
= strchrW(mode
, '+') != NULL
;
1248 while(*mode
== ' ') mode
++;
1253 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1254 *stream_flags
= plus
? _IORW
: _IOREAD
;
1257 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1258 *stream_flags
= plus
? _IORW
: _IOWRT
;
1261 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1262 *stream_flags
= plus
? _IORW
: _IOWRT
;
1265 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1271 /* *stream_flags |= MSVCRT__commode; */
1273 while (*mode
&& *mode
!=',')
1277 *open_flags
|= _O_BINARY
;
1278 *open_flags
&= ~_O_TEXT
;
1281 *open_flags
|= _O_TEXT
;
1282 *open_flags
&= ~_O_BINARY
;
1285 *open_flags
|= _O_TEMPORARY
;
1288 *open_flags
|= _O_SHORT_LIVED
;
1292 *stream_flags |= _IOCOMMIT;
1295 *stream_flags &= ~_IOCOMMIT;
1299 *open_flags
|= _O_NOINHERIT
;
1308 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1311 ERR("incorrect mode flag: %c\n", mode
[-1]);
1317 static const WCHAR ccs
[] = {'c','c','s'};
1318 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1319 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1320 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1323 while(*mode
== ' ') mode
++;
1324 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1326 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1327 while(*mode
== ' ') mode
++;
1328 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1331 while(*mode
== ' ') mode
++;
1333 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1335 *open_flags
|= _O_U8TEXT
;
1336 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1338 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1340 *open_flags
|= _O_U16TEXT
;
1341 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1343 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1345 *open_flags
|= _O_WTEXT
;
1346 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1350 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1355 while(*mode
== ' ') mode
++;
1358 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1364 /*********************************************************************
1365 * _fdopen (MSVCRT.@)
1367 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1370 wchar_t *modeW
= NULL
;
1372 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1374 ret
= _wfdopen(fd
, modeW
);
1380 /*********************************************************************
1381 * _wfdopen (MSVCRT.@)
1383 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1385 int open_flags
, stream_flags
;
1388 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1391 if (!(file
= alloc_fp()))
1393 else if (init_fp(file
, fd
, stream_flags
) == -1)
1398 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1404 /*********************************************************************
1405 * _filelength (MSVCRT.@)
1407 LONG CDECL
_filelength(int fd
)
1409 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1412 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1415 if (endPos
!= curPos
)
1416 _lseek(fd
, curPos
, SEEK_SET
);
1423 /*********************************************************************
1424 * _filelengthi64 (MSVCRT.@)
1426 __int64 CDECL
_filelengthi64(int fd
)
1428 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1431 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1434 if (endPos
!= curPos
)
1435 _lseeki64(fd
, curPos
, SEEK_SET
);
1442 /*********************************************************************
1443 * _fileno (MSVCRT.@)
1445 int CDECL
_fileno(FILE* file
)
1447 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1451 /*********************************************************************
1452 * _get_osfhandle (MSVCRT.@)
1454 intptr_t CDECL
_get_osfhandle(int fd
)
1456 HANDLE hand
= fdtoh(fd
);
1457 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1459 return (intptr_t)hand
;
1462 /*********************************************************************
1463 * _isatty (MSVCRT.@)
1465 int CDECL
_isatty(int fd
)
1467 HANDLE hand
= fdtoh(fd
);
1469 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1470 if (hand
== INVALID_HANDLE_VALUE
)
1473 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1476 /*********************************************************************
1477 * _mktemp (MSVCRT.@)
1479 char * CDECL
_mktemp(char *pattern
)
1482 char *retVal
= pattern
;
1487 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1491 id
= GetCurrentProcessId();
1495 int tempNum
= id
/ 10;
1496 *pattern
-- = id
- (tempNum
* 10) + '0';
1502 *pattern
= letter
++;
1503 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1504 GetLastError() == ERROR_FILE_NOT_FOUND
)
1506 } while(letter
<= 'z');
1510 /*********************************************************************
1511 * _wmktemp (MSVCRT.@)
1513 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1516 wchar_t *retVal
= pattern
;
1518 wchar_t letter
= 'a';
1521 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1525 id
= GetCurrentProcessId();
1529 int tempNum
= id
/ 10;
1530 *pattern
-- = id
- (tempNum
* 10) + '0';
1536 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1537 GetLastError() == ERROR_FILE_NOT_FOUND
)
1539 *pattern
= letter
++;
1540 } while(letter
!= '|');
1544 /*static*/ unsigned split_oflags(unsigned oflags
)
1547 unsigned unsupp
; /* until we support everything */
1549 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1550 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1551 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1552 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1553 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1554 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1555 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1556 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1557 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1559 if ((unsupp
= oflags
& ~(
1560 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1561 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1562 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1564 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1565 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1567 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1572 /*********************************************************************
1575 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1578 SECURITY_ATTRIBUTES sa
;
1579 HANDLE readHandle
, writeHandle
;
1587 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1588 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1589 sa
.lpSecurityDescriptor
= NULL
;
1590 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1592 unsigned int wxflags
= split_oflags(textmode
);
1596 fd
= alloc_fd(readHandle
, wxflags
);
1600 fd
= alloc_fd(writeHandle
, wxflags
);
1609 CloseHandle(writeHandle
);
1615 CloseHandle(readHandle
);
1616 CloseHandle(writeHandle
);
1622 _dosmaperr(GetLastError());
1627 /*********************************************************************
1628 * _sopen_s (MSVCRT.@)
1630 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1632 DWORD access
= 0, creation
= 0, attrib
;
1636 SECURITY_ATTRIBUTES sa
;
1638 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1639 fd
, path
, oflags
, shflags
, pmode
);
1643 MSVCRT_INVALID_PMT("null out fd pointer");
1649 wxflag
= split_oflags(oflags
);
1650 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1652 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1653 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1654 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1657 if (oflags
& _O_CREAT
)
1659 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1660 FIXME(": pmode 0x%04x ignored\n", pmode
);
1662 WARN(": pmode 0x%04x ignored\n", pmode
);
1664 if (oflags
& _O_EXCL
)
1665 creation
= CREATE_NEW
;
1666 else if (oflags
& _O_TRUNC
)
1667 creation
= CREATE_ALWAYS
;
1669 creation
= OPEN_ALWAYS
;
1671 else /* no _O_CREAT */
1673 if (oflags
& _O_TRUNC
)
1674 creation
= TRUNCATE_EXISTING
;
1676 creation
= OPEN_EXISTING
;
1685 sharing
= FILE_SHARE_READ
;
1688 sharing
= FILE_SHARE_WRITE
;
1691 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1694 ERR( "Unhandled shflags 0x%x\n", shflags
);
1697 attrib
= FILE_ATTRIBUTE_NORMAL
;
1699 if (oflags
& _O_TEMPORARY
)
1701 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1703 sharing
|= FILE_SHARE_DELETE
;
1706 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1707 sa
.lpSecurityDescriptor
= NULL
;
1708 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1710 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1711 if (hand
== INVALID_HANDLE_VALUE
) {
1712 WARN(":failed-last error (%d)\n", GetLastError());
1713 _dosmaperr(GetLastError());
1717 *fd
= alloc_fd(hand
, wxflag
);
1721 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1725 /*********************************************************************
1728 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1733 if (oflags
& _O_CREAT
)
1737 va_start(ap
, shflags
);
1738 pmode
= va_arg(ap
, int);
1744 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1748 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1750 char bom
[sizeof(utf8_bom
)];
1753 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1755 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1758 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1759 oflags
|= _O_U8TEXT
;
1760 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1762 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1763 oflags
|= _O_U16TEXT
;
1765 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1771 /*********************************************************************
1772 * _wsopen_s (MSVCRT.@)
1774 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1776 DWORD access
= 0, creation
= 0, attrib
;
1777 SECURITY_ATTRIBUTES sa
;
1782 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1783 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1785 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1788 wxflag
= split_oflags(oflags
);
1789 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1791 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1792 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1793 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1796 if (oflags
& _O_CREAT
)
1798 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1799 FIXME(": pmode 0x%04x ignored\n", pmode
);
1801 WARN(": pmode 0x%04x ignored\n", pmode
);
1803 if (oflags
& _O_EXCL
)
1804 creation
= CREATE_NEW
;
1805 else if (oflags
& _O_TRUNC
)
1806 creation
= CREATE_ALWAYS
;
1808 creation
= OPEN_ALWAYS
;
1810 else /* no _O_CREAT */
1812 if (oflags
& _O_TRUNC
)
1813 creation
= TRUNCATE_EXISTING
;
1815 creation
= OPEN_EXISTING
;
1824 sharing
= FILE_SHARE_READ
;
1827 sharing
= FILE_SHARE_WRITE
;
1830 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1833 ERR( "Unhandled shflags 0x%x\n", shflags
);
1836 attrib
= FILE_ATTRIBUTE_NORMAL
;
1838 if (oflags
& _O_TEMPORARY
)
1840 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1842 sharing
|= FILE_SHARE_DELETE
;
1845 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1846 sa
.lpSecurityDescriptor
= NULL
;
1847 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1849 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1850 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1851 && !(access
&GENERIC_READ
))
1853 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1854 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1855 if (hand
!= INVALID_HANDLE_VALUE
)
1857 oflags
= check_bom(hand
, oflags
, FALSE
);
1861 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1864 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1865 if (hand
== INVALID_HANDLE_VALUE
) {
1866 WARN(":failed-last error (%d)\n",GetLastError());
1867 _dosmaperr(GetLastError());
1871 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1873 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1874 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1875 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1877 if (oflags
& _O_U8TEXT
)
1879 DWORD written
= 0, tmp
;
1881 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1882 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1884 if (written
!= sizeof(utf8_bom
)) {
1885 WARN("error writing BOM\n");
1887 _dosmaperr(GetLastError());
1893 DWORD written
= 0, tmp
;
1895 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1896 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1898 if (written
!= sizeof(utf16_bom
))
1900 WARN("error writing BOM\n");
1902 _dosmaperr(GetLastError());
1907 else if (access
& GENERIC_READ
)
1908 oflags
= check_bom(hand
, oflags
, TRUE
);
1911 *fd
= alloc_fd(hand
, wxflag
);
1915 if (oflags
& _O_WTEXT
)
1916 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1917 else if (oflags
& _O_U16TEXT
)
1918 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1919 else if (oflags
& _O_U8TEXT
)
1920 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1922 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1926 /*********************************************************************
1927 * _wsopen (MSVCRT.@)
1929 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1934 if (oflags
& _O_CREAT
)
1938 va_start(ap
, shflags
);
1939 pmode
= va_arg(ap
, int);
1945 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1949 /*********************************************************************
1952 int CDECL
_open( const char *path
, int flags
, ... )
1956 if (flags
& _O_CREAT
)
1959 va_start(ap
, flags
);
1960 pmode
= va_arg(ap
, int);
1962 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1965 return _sopen( path
, flags
, _SH_DENYNO
);
1968 /*********************************************************************
1971 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1975 if (flags
& _O_CREAT
)
1978 va_start(ap
, flags
);
1979 pmode
= va_arg(ap
, int);
1981 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1984 return _wsopen( path
, flags
, _SH_DENYNO
);
1987 /*********************************************************************
1990 int CDECL
_creat(const char *path
, int flags
)
1992 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1993 return _open(path
, usedFlags
);
1996 /*********************************************************************
1997 * _wcreat (MSVCRT.@)
1999 int CDECL
_wcreat(const wchar_t *path
, int flags
)
2001 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2002 return _wopen(path
, usedFlags
);
2005 /*********************************************************************
2006 * _open_osfhandle (MSVCRT.@)
2008 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2012 /* _O_RDONLY (0) always matches, so set the read flag
2013 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2014 * file, so set the write flag. It also only sets _O_TEXT if it wants
2015 * text - it never sets _O_BINARY.
2017 /* don't let split_oflags() decide the mode if no mode is passed */
2018 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2019 oflags
|= _O_BINARY
;
2021 fd
= alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
2022 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
2026 /*********************************************************************
2029 int CDECL
_rmtmp(void)
2031 int num_removed
= 0, i
;
2035 for (i
= 3; i
< stream_idx
; i
++) {
2038 if (file
->_tmpfname
)
2047 TRACE(":removed (%d) temp files\n",num_removed
);
2051 /*********************************************************************
2054 * When reading \r as last character in text mode, read() positions
2055 * the file pointer on the \r character while getc() goes on to
2058 static int read_i(int fd
, void *buf
, unsigned int count
)
2061 char *bufstart
= buf
;
2062 HANDLE hand
= fdtoh(fd
);
2063 ioinfo
*fdinfo
= get_ioinfo(fd
);
2068 if (fdinfo
->wxflag
& WX_READEOF
) {
2069 fdinfo
->wxflag
|= WX_ATEOF
;
2070 TRACE("already at EOF, returning 0\n");
2073 /* Don't trace small reads, it gets *very* annoying */
2075 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2076 if (hand
== INVALID_HANDLE_VALUE
)
2079 /* Reading single bytes in O_TEXT mode makes things slow
2080 * So read big chunks
2082 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2084 if (count
!= 0 && num_read
== 0)
2086 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2087 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2089 else if (fdinfo
->wxflag
& WX_TEXT
)
2092 if (bufstart
[num_read
-1] == '\r')
2096 fdinfo
->wxflag
&= ~WX_READCR
;
2097 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
2101 fdinfo
->wxflag
|= WX_READCR
;
2106 fdinfo
->wxflag
&= ~WX_READCR
;
2107 for (i
=0, j
=0; i
<num_read
; i
++)
2109 /* in text mode, a ctrl-z signals EOF */
2110 if (bufstart
[i
] == 0x1a)
2112 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2113 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2116 /* in text mode, strip \r if followed by \n.
2117 * BUG: should save state across calls somehow, so CR LF that
2118 * straddles buffer boundary gets recognized properly?
2120 if ((bufstart
[i
] != '\r')
2121 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
2122 bufstart
[j
++] = bufstart
[i
];
2129 if (GetLastError() == ERROR_BROKEN_PIPE
)
2131 TRACE(":end-of-pipe\n");
2132 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2137 TRACE(":failed-last error (%d)\n",GetLastError());
2143 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2147 /*********************************************************************
2150 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2153 num_read
= read_i(fd
, buf
, count
);
2157 /*********************************************************************
2158 * _setmode (MSVCRT.@)
2160 int CDECL
_setmode(int fd
,int mode
)
2162 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2163 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
2164 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
2165 if ((mode
& _O_TEXT
) == _O_TEXT
)
2166 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2168 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2172 /*********************************************************************
2175 long CDECL
_tell(int fd
)
2177 return _lseek(fd
, 0, SEEK_CUR
);
2180 /*********************************************************************
2181 * _telli64 (MSVCRT.@)
2183 __int64 CDECL
_telli64(int fd
)
2185 return _lseeki64(fd
, 0, SEEK_CUR
);
2188 /*********************************************************************
2189 * _tempnam (MSVCRT.@)
2191 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2193 char tmpbuf
[MAX_PATH
];
2194 const char *tmp_dir
= getenv("TMP");
2196 if (tmp_dir
) dir
= tmp_dir
;
2198 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2199 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2201 TRACE("got name (%s)\n",tmpbuf
);
2202 DeleteFileA(tmpbuf
);
2203 return _strdup(tmpbuf
);
2205 TRACE("failed (%d)\n",GetLastError());
2209 /*********************************************************************
2210 * _wtempnam (MSVCRT.@)
2212 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2214 wchar_t tmpbuf
[MAX_PATH
];
2216 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2217 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2219 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2220 DeleteFileW(tmpbuf
);
2221 return _wcsdup(tmpbuf
);
2223 TRACE("failed (%d)\n",GetLastError());
2227 /*********************************************************************
2230 int CDECL
_umask(int umask
)
2232 int old_umask
= umask
;
2233 TRACE("(%d)\n",umask
);
2234 MSVCRT_umask
= umask
;
2238 /*********************************************************************
2241 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2244 HANDLE hand
= fdtoh(fd
);
2246 /* Don't trace small writes, it gets *very* annoying */
2249 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2251 if (hand
== INVALID_HANDLE_VALUE
)
2257 /* If appending, go to EOF */
2258 if (get_ioinfo(fd
)->wxflag
& WX_APPEND
)
2259 _lseek(fd
, 0, FILE_END
);
2261 if (!(get_ioinfo(fd
)->wxflag
& WX_TEXT
))
2263 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2264 && (num_written
== count
))
2266 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2267 hand
, GetLastError());
2272 unsigned int i
, j
, nr_lf
;
2275 const char *s
= buf
, *buf_start
= buf
;
2276 /* find number of \n ( without preceding \r ) */
2277 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2282 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2287 if ((q
= p
= malloc(count
+ nr_lf
)))
2289 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2294 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2301 FIXME("Malloc failed\n");
2309 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2311 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2312 fd
, hand
, GetLastError(), num_written
);
2316 return s
- buf_start
;
2328 /*********************************************************************
2331 int CDECL
_putw(int val
, FILE* file
)
2336 len
= _write(file
->_file
, &val
, sizeof(val
));
2337 if (len
== sizeof(val
)) {
2342 file
->_flag
|= _IOERR
;
2347 /*********************************************************************
2350 int CDECL
fclose(FILE* file
)
2356 free(file
->_tmpfname
);
2357 file
->_tmpfname
= NULL
;
2358 /* flush stdio buffers */
2359 if(file
->_flag
& _IOWRT
)
2361 if(file
->_flag
& _IOMYBUF
)
2364 r
=_close(file
->_file
);
2368 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2369 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2371 if(file
== get_file(stream_idx
-1)) {
2372 while(stream_idx
>3 && !file
->_flag
) {
2374 file
= get_file(stream_idx
-1);
2378 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2381 /*********************************************************************
2384 int CDECL
feof(FILE* file
)
2389 ret
= file
->_flag
& _IOEOF
;
2395 /*********************************************************************
2398 int CDECL
ferror(FILE* file
)
2403 ret
= file
->_flag
& _IOERR
;
2409 /*********************************************************************
2410 * _filbuf (MSVCRT.@)
2412 int CDECL
_filbuf(FILE* file
)
2417 /* Allocate buffer if needed */
2418 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2421 if(!(file
->_flag
& _IOREAD
)) {
2422 if(file
->_flag
& _IORW
)
2423 file
->_flag
|= _IOREAD
;
2430 if(file
->_flag
& _IONBF
) {
2432 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2433 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2441 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2443 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2450 file
->_ptr
= file
->_base
+1;
2451 c
= *(unsigned char *)file
->_base
;
2457 /*********************************************************************
2460 int CDECL
fgetc(FILE* file
)
2468 i
= (unsigned char *)file
->_ptr
++;
2477 /*********************************************************************
2478 * _fgetchar (MSVCRT.@)
2480 int CDECL
_fgetchar(void)
2482 return fgetc(stdin
);
2485 /*********************************************************************
2488 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2491 char * buf_start
= s
;
2493 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2494 file
,file
->_file
,s
,size
);
2498 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2503 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2505 TRACE(":nothing read\n");
2509 if ((cc
!= EOF
) && (size
> 1))
2512 TRACE(":got %s\n", debugstr_a(buf_start
));
2517 /*********************************************************************
2520 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2521 * the CR from CR/LF combinations
2523 wint_t CDECL
fgetwc(FILE* file
)
2528 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2535 for(i
=0; i
<sizeof(wc
); i
++)
2548 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2563 if ((__mb_cur_max
> 1) && isleadbyte(c
))
2565 FIXME("Treat Multibyte characters\n");
2575 /*********************************************************************
2578 int CDECL
_getw(FILE* file
)
2586 for (j
=0; j
<sizeof(int); j
++) {
2589 file
->_flag
|= _IOEOF
;
2600 /*********************************************************************
2603 wint_t CDECL
getwc(FILE* file
)
2605 return fgetwc(file
);
2608 /*********************************************************************
2609 * _fgetwchar (MSVCRT.@)
2611 wint_t CDECL
_fgetwchar(void)
2613 return fgetwc(stdin
);
2616 /*********************************************************************
2617 * getwchar (MSVCRT.@)
2619 wint_t CDECL
getwchar(void)
2621 return _fgetwchar();
2624 /*********************************************************************
2627 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2630 wchar_t * buf_start
= s
;
2632 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2633 file
,file
->_file
,s
,size
);
2637 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2642 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2644 TRACE(":nothing read\n");
2648 if ((cc
!= WEOF
) && (size
> 1))
2651 TRACE(":got %s\n", debugstr_w(buf_start
));
2656 /*********************************************************************
2659 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2661 size_t wrcnt
=size
* nmemb
;
2668 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2669 memcpy(file
->_ptr
, ptr
, pcnt
);
2674 ptr
= (const char*)ptr
+ pcnt
;
2675 } else if(!(file
->_flag
& _IOWRT
)) {
2676 if(file
->_flag
& _IORW
) {
2677 file
->_flag
|= _IOWRT
;
2685 int res
=flush_buffer(file
);
2687 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2690 file
->_flag
|= _IOERR
;
2693 written
+= pwritten
;
2698 return written
/ size
;
2701 /*********************************************************************
2703 * FORKED for ReactOS, don't sync with Wine!
2705 * - http://jira.reactos.org/browse/CORE-6495
2706 * - http://bugs.winehq.org/show_bug.cgi?id=8598
2708 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2710 /* If this is a real file stream (and not some temporary one for
2711 sprintf-like functions), check whether it is opened in text mode.
2712 In this case, we have to perform an implicit conversion to ANSI. */
2713 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
2715 /* Convert to multibyte in text mode */
2716 char mbc
[MB_LEN_MAX
];
2719 mb_return
= wctomb(mbc
, c
);
2724 /* Output all characters */
2725 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2730 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2737 /*********************************************************************
2738 * _fputwchar (MSVCRT.@)
2740 wint_t CDECL
_fputwchar(wint_t wc
)
2742 return fputwc(wc
, stdout
);
2745 /*********************************************************************
2746 * _wfsopen (MSVCRT.@)
2748 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2751 int open_flags
, stream_flags
, fd
;
2753 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2755 /* map mode string to open() flags. "man fopen" for possibilities. */
2756 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2760 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2763 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2765 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2772 TRACE(":got (%p)\n",file
);
2773 if (fd
>= 0 && !file
)
2779 /*********************************************************************
2780 * _fsopen (MSVCRT.@)
2782 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2785 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2787 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2788 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2792 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2795 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2800 ret
= _wfsopen(pathW
, modeW
, share
);
2807 /*********************************************************************
2810 FILE * CDECL
fopen(const char *path
, const char *mode
)
2812 return _fsopen( path
, mode
, _SH_DENYNO
);
2815 /*********************************************************************
2816 * fopen_s (MSVCRT.@)
2818 int CDECL
fopen_s(FILE** pFile
,
2819 const char *filename
, const char *mode
)
2821 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2822 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2827 *pFile
= fopen(filename
, mode
);
2834 /*********************************************************************
2835 * _wfopen (MSVCRT.@)
2837 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2839 return _wfsopen( path
, mode
, _SH_DENYNO
);
2842 /*********************************************************************
2843 * _wfopen_s (MSVCRT.@)
2845 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
2846 const wchar_t *mode
)
2848 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2849 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2854 *pFile
= _wfopen(filename
, mode
);
2861 /* fputc calls _flsbuf which calls fputc */
2862 int CDECL
_flsbuf(int c
, FILE* file
);
2864 /*********************************************************************
2867 int CDECL
fputc(int c
, FILE* file
)
2877 res
= flush_buffer(file
);
2879 return res
? res
: c
;
2886 res
= _flsbuf(c
, file
);
2892 /*********************************************************************
2893 * _fputchar (MSVCRT.@)
2895 int CDECL
_fputchar(int c
)
2897 return fputc(c
, stdout
);
2900 /*********************************************************************
2903 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2905 size_t rcnt
=size
* nmemb
;
2914 /* first buffered data */
2916 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
2917 memcpy(ptr
, file
->_ptr
, pcnt
);
2922 ptr
= (char*)ptr
+ pcnt
;
2923 } else if(!(file
->_flag
& _IOREAD
)) {
2924 if(file
->_flag
& _IORW
) {
2925 file
->_flag
|= _IOREAD
;
2934 /* Fill the buffer on small reads.
2935 * TODO: Use a better buffering strategy.
2937 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2938 if (file
->_bufsiz
== 0) {
2941 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2942 file
->_ptr
= file
->_base
;
2943 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2944 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2945 if (i
> 0 && i
< file
->_cnt
) {
2946 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
2947 file
->_flag
&= ~_IOEOF
;
2950 memcpy(ptr
, file
->_ptr
, i
);
2955 i
= _read(file
->_file
,ptr
, rcnt
);
2959 ptr
= (char *)ptr
+i
;
2960 /* expose feof condition in the flags
2961 * MFC tests file->_flag for feof, and doesn't call feof())
2963 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
2964 file
->_flag
|= _IOEOF
;
2967 file
->_flag
|= _IOERR
;
2978 /*********************************************************************
2979 * _wfreopen (MSVCRT.@)
2982 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
2984 int open_flags
, stream_flags
, fd
;
2986 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2989 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2994 /* map mode string to open() flags. "man fopen" for possibilities. */
2995 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2999 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3002 else if (init_fp(file
, fd
, stream_flags
) == -1)
3005 WARN(":failed-last error (%d)\n",GetLastError());
3006 _dosmaperr(GetLastError());
3015 /*********************************************************************
3016 * freopen (MSVCRT.@)
3019 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3022 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3024 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3025 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3031 ret
= _wfreopen(pathW
, modeW
, file
);
3038 /*********************************************************************
3039 * fsetpos (MSVCRT.@)
3041 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3046 /* Note that all this has been lifted 'as is' from fseek */
3047 if(file
->_flag
& _IOWRT
)
3050 /* Discard buffered input */
3052 file
->_ptr
= file
->_base
;
3054 /* Reset direction of i/o */
3055 if(file
->_flag
& _IORW
) {
3056 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3059 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3064 /*********************************************************************
3065 * _ftelli64 (MSVCRT.@)
3067 __int64 CDECL
_ftelli64(FILE* file
)
3069 /* TODO: just call fgetpos and return lower half of result */
3074 pos
= _telli64(file
->_file
);
3080 if( file
->_flag
& _IOWRT
) {
3081 off
= file
->_ptr
- file
->_base
;
3084 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3085 /* Black magic correction for CR removal */
3087 for (i
=0; i
<file
->_cnt
; i
++) {
3088 if (file
->_ptr
[i
] == '\n')
3091 /* Black magic when reading CR at buffer boundary*/
3092 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3102 /*********************************************************************
3105 LONG CDECL
ftell(FILE* file
)
3107 return (LONG
)_ftelli64(file
);
3110 /*********************************************************************
3111 * fgetpos (MSVCRT.@)
3113 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3118 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
3124 if( file
->_flag
& _IOWRT
) {
3125 off
= file
->_ptr
- file
->_base
;
3128 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3129 /* Black magic correction for CR removal */
3131 for (i
=0; i
<file
->_cnt
; i
++) {
3132 if (file
->_ptr
[i
] == '\n')
3135 /* Black magic when reading CR at buffer boundary*/
3136 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3146 /*********************************************************************
3149 int CDECL
fputs(const char *s
, FILE* file
)
3151 size_t i
, len
= strlen(s
);
3155 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3156 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3160 for (i
=0; i
<len
; i
++)
3161 if (fputc(s
[i
], file
) == EOF
) {
3170 /*********************************************************************
3173 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3175 size_t i
, len
= strlenW(s
);
3179 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3180 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3184 for (i
=0; i
<len
; i
++) {
3185 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
3186 || fputwc(s
[i
], file
) == WEOF
) {
3196 /*********************************************************************
3197 * getchar (MSVCRT.@)
3199 int CDECL
getchar(void)
3201 return fgetc(stdin
);
3204 /*********************************************************************
3207 int CDECL
getc(FILE* file
)
3212 /*********************************************************************
3215 char * CDECL
gets(char *buf
)
3218 char * buf_start
= buf
;
3221 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3223 if(cc
!= '\r') *buf
++ = (char)cc
;
3227 TRACE("got '%s'\n", buf_start
);
3228 _unlock_file(stdin
);
3232 /*********************************************************************
3235 wchar_t* CDECL
_getws(wchar_t* buf
)
3241 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3245 *buf
++ = (wchar_t)cc
;
3249 TRACE("got %s\n", debugstr_w(ws
));
3250 _unlock_file(stdin
);
3254 /*********************************************************************
3257 int CDECL
putc(int c
, FILE* file
)
3259 return fputc(c
, file
);
3262 /*********************************************************************
3263 * putchar (MSVCRT.@)
3265 int CDECL
putchar(int c
)
3267 return fputc(c
, stdout
);
3270 /*********************************************************************
3271 * _putwch (MSVCRT.@)
3273 wint_t CDECL
_putwch(wchar_t c
)
3275 return fputwc(c
, stdout
);
3278 /*********************************************************************
3281 int CDECL
puts(const char *s
)
3283 size_t len
= strlen(s
);
3287 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3288 _unlock_file(stdout
);
3292 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3293 _unlock_file(stdout
);
3297 /*********************************************************************
3300 int CDECL
_putws(const wchar_t *s
)
3302 static const wchar_t nl
= '\n';
3303 size_t len
= strlenW(s
);
3307 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3308 _unlock_file(stdout
);
3312 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3313 _unlock_file(stdout
);
3317 /*********************************************************************
3320 int CDECL
remove(const char *path
)
3322 TRACE("(%s)\n",path
);
3323 if (DeleteFileA(path
))
3325 TRACE(":failed (%d)\n",GetLastError());
3326 _dosmaperr(GetLastError());
3330 /*********************************************************************
3331 * _wremove (MSVCRT.@)
3333 int CDECL
_wremove(const wchar_t *path
)
3335 TRACE("(%s)\n",debugstr_w(path
));
3336 if (DeleteFileW(path
))
3338 TRACE(":failed (%d)\n",GetLastError());
3339 _dosmaperr(GetLastError());
3343 /*********************************************************************
3346 int CDECL
rename(const char *oldpath
,const char *newpath
)
3348 TRACE(":from %s to %s\n",oldpath
,newpath
);
3349 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3351 TRACE(":failed (%d)\n",GetLastError());
3352 _dosmaperr(GetLastError());
3356 /*********************************************************************
3357 * _wrename (MSVCRT.@)
3359 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3361 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3362 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3364 TRACE(":failed (%d)\n",GetLastError());
3365 _dosmaperr(GetLastError());
3369 /*********************************************************************
3370 * setvbuf (MSVCRT.@)
3372 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3380 if(mode
== _IOFBF
) {
3381 file
->_flag
&= ~_IONBF
;
3382 file
->_base
= file
->_ptr
= buf
;
3384 file
->_bufsiz
= size
;
3387 file
->_flag
|= _IONBF
;
3393 /*********************************************************************
3396 void CDECL
setbuf(FILE* file
, char *buf
)
3398 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3401 /*********************************************************************
3404 char * CDECL
tmpnam(char *s
)
3411 thread_data_t
*data
= msvcrt_get_thread_data();
3413 if(!data
->tmpnam_buffer
)
3414 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3416 s
= data
->tmpnam_buffer
;
3419 int_to_base32(GetCurrentProcessId(), tmpstr
);
3420 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3421 for (count
= 0; count
< TMP_MAX
; count
++)
3423 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3424 memcpy(p
, tmpstr
, size
);
3425 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3426 GetLastError() == ERROR_FILE_NOT_FOUND
)
3432 /*********************************************************************
3433 * _wtmpnam (MSVCRT.@)
3435 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3437 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3442 thread_data_t
*data
= msvcrt_get_thread_data();
3444 if(!data
->wtmpnam_buffer
)
3445 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3447 s
= data
->wtmpnam_buffer
;
3450 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3451 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3452 for (count
= 0; count
< TMP_MAX
; count
++)
3454 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3455 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3456 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3457 GetLastError() == ERROR_FILE_NOT_FOUND
)
3463 /*********************************************************************
3464 * tmpfile (MSVCRT.@)
3466 FILE* CDECL
tmpfile(void)
3468 char *filename
= tmpnam(NULL
);
3473 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
3474 if (fd
!= -1 && (file
= alloc_fp()))
3476 if (init_fp(file
, fd
, _O_RDWR
) == -1)
3481 else file
->_tmpfname
= _strdup(filename
);
3487 /*********************************************************************
3490 int CDECL
ungetc(int c
, FILE * file
)
3496 if(file
->_bufsiz
== 0) {
3500 if(file
->_ptr
>file
->_base
) {
3513 /*********************************************************************
3514 * ungetwc (MSVCRT.@)
3516 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3519 char * pp
= (char *)&mwc
;
3523 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3524 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3536 /*********************************************************************
3537 * _getmaxstdio (MSVCRT.@)
3539 int CDECL
_getmaxstdio(void)
3544 /*********************************************************************
3545 * _setmaxstdio (MSVCRT.@)
3547 int CDECL
_setmaxstdio(int newmax
)
3549 TRACE("%d\n", newmax
);
3551 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3554 max_streams
= newmax
;