2 * PROJECT: ReactOS CRT library
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: lib/sdk/crt/stdio/file.c
5 * PURPOSE: File CRT functions
6 * PROGRAMMERS: Wine team
7 * Ported to ReactOS by Aleksey Bragin (aleksey@reactos.org)
10 /*********************************************
11 * This file contains ReactOS changes!!
12 * Don't blindly sync it with Wine code!
14 * If you break Unicode output on the console again, please update this counter:
15 * int hours_wasted_on_this = 42;
16 *********************************************/
19 * msvcrt.dll file functions
21 * Copyright 1996,1998 Marcus Meissner
22 * Copyright 1996 Jukka Iivonen
23 * Copyright 1997,2000 Uwe Bonnes
24 * Copyright 2000 Jon Griffiths
25 * Copyright 2004 Eric Pouech
26 * Copyright 2004 Juan Lang
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Lesser General Public
30 * License as published by the Free Software Foundation; either
31 * version 2.1 of the License, or (at your option) any later version.
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Lesser General Public License for more details.
38 * You should have received a copy of the GNU Lesser General Public
39 * License along with this library; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
43 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
47 #include "wine/unicode.h"
49 #include <sys/utime.h>
52 int *__p__fmode(void);
53 int *__p___mb_cur_max(void);
58 #define _IOCOMMIT 0x4000
86 /* _access() bit flags FIXME: incomplete */
87 /* defined in crt/io.h */
89 /* values for wxflag in file descriptor */
92 #define WX_READNL 0x04 /* read started with \n */
93 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
95 #define WX_READCR 0x08 /* underlying file is at \r */
96 #define WX_DONTINHERIT 0x10
97 #define WX_APPEND 0x20
98 #define WX_NOSEEK 0x40
101 /* values for exflag - it's used differently in msvcr90.dll*/
103 #define EF_UTF16 0x02
104 #define EF_UNK_UNICODE 0x08
106 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
107 static char utf16_bom
[2] = { 0xff, 0xfe };
109 /* FIXME: this should be allocated dynamically */
110 #define MAX_FILES 2048
111 #define FD_BLOCK_SIZE 64
113 /* ioinfo structure size is different in msvcrXX.dll's */
116 unsigned char wxflag
;
119 CRITICAL_SECTION crit
;
122 /*********************************************************************
123 * __pioinfo (MSVCRT.@)
124 * array of pointers to ioinfo arrays [64]
126 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
128 /*********************************************************************
129 * __badioinfo (MSVCRT.@)
131 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
133 static int fdstart
= 3; /* first unallocated fd */
134 static int fdend
= 3; /* highest allocated fd */
138 CRITICAL_SECTION crit
;
141 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
142 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
143 static int max_streams
= 512, stream_idx
;
145 /* INTERNAL: process umask */
146 static int MSVCRT_umask
= 0;
148 /* INTERNAL: static data for tmpnam and _wtmpname functions */
149 static int tmpnam_unique
;
151 /* This critical section protects the tables __pioinfo and fstreams,
152 * and their related indexes, fdstart, fdend,
153 * and stream_idx, from race conditions.
154 * It doesn't protect against race conditions manipulating the underlying files
155 * or flags; doing so would probably be better accomplished with per-file
156 * protection, rather than locking the whole table for every change.
158 static CRITICAL_SECTION file_cs
;
159 static CRITICAL_SECTION_DEBUG file_cs_debug
=
162 { &file_cs_debug
.ProcessLocksList
, &file_cs_debug
.ProcessLocksList
},
163 0, 0, { (DWORD_PTR
)(__FILE__
": file_cs") }
165 static CRITICAL_SECTION file_cs
= { &file_cs_debug
, -1, 0, 0, 0, 0 };
166 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
167 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
169 static inline ioinfo
* get_ioinfo(int fd
)
173 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
177 return ret
+ (fd
%FD_BLOCK_SIZE
);
180 static inline FILE* get_file(int i
)
190 ret
= fstream
[i
/FD_BLOCK_SIZE
];
192 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
193 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
194 ERR("out of memory\n");
199 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
201 ret
+= i
%FD_BLOCK_SIZE
;
206 static inline BOOL
is_valid_fd(int fd
)
208 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
211 /* INTERNAL: Get the HANDLE for a fd
212 * This doesn't lock the table, because a failure will result in
213 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
214 * it returns a valid handle which is about to be closed, a subsequent call
215 * will fail, most likely in a sane way.
217 /*static*/ HANDLE
fdtoh(int fd
)
219 if (!is_valid_fd(fd
))
221 WARN(":fd (%d) - no handle!\n",fd
);
224 return INVALID_HANDLE_VALUE
;
226 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
227 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
228 return get_ioinfo(fd
)->handle
;
231 /* INTERNAL: free a file entry fd */
232 static void free_fd(int fd
)
238 fdinfo
= get_ioinfo(fd
);
239 old_handle
= fdinfo
->handle
;
240 if(fdinfo
!= &__badioinfo
)
242 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
245 TRACE(":fd (%d) freed\n",fd
);
246 if (fd
< 3) /* don't use 0,1,2 for user files */
251 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
254 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
257 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
271 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
272 /* caller must hold the files lock */
273 static int set_fd(HANDLE hand
, int flag
, int fd
)
279 WARN(":files exhausted!\n");
284 fdinfo
= get_ioinfo(fd
);
285 if(fdinfo
== &__badioinfo
) {
288 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
289 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
290 WARN(":out of memory!\n");
295 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
296 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
298 fdinfo
= get_ioinfo(fd
);
301 fdinfo
->handle
= hand
;
302 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_NOSEEK
));
303 fdinfo
->lookahead
[0] = '\n';
304 fdinfo
->lookahead
[1] = '\n';
305 fdinfo
->lookahead
[2] = '\n';
308 /* locate next free slot */
309 if (fd
== fdstart
&& fd
== fdend
)
312 while (fdstart
< fdend
&&
313 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
315 /* update last fd in use */
318 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
322 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
323 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
324 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
330 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
331 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
336 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
337 ret
= set_fd(hand
, flag
, fdstart
);
342 /* INTERNAL: Allocate a FILE* for an fd slot */
343 /* caller must hold the files lock */
344 static FILE* alloc_fp(void)
349 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
355 if (file
->_flag
== 0)
357 if (i
== stream_idx
) stream_idx
++;
365 /* INTERNAL: initialize a FILE* from an open fd */
366 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
368 TRACE(":fd (%d) allocating FILE*\n",fd
);
369 if (!is_valid_fd(fd
))
371 WARN(":invalid fd %d\n",fd
);
376 memset(file
, 0, sizeof(*file
));
378 file
->_flag
= stream_flags
;
380 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
381 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
383 TRACE(":got FILE* (%p)\n",file
);
387 /* INTERNAL: Create an inheritance data block (for spawned process)
388 * The inheritance block is made of:
389 * 00 int nb of file descriptor (NBFD)
390 * 04 char file flags (wxflag): repeated for each fd
391 * 4+NBFD HANDLE file handle: repeated for each fd
393 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
400 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
401 *block
= calloc(*size
, 1);
407 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
408 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
410 *(unsigned*)*block
= fdend
;
411 for (fd
= 0; fd
< fdend
; fd
++)
413 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
414 fdinfo
= get_ioinfo(fd
);
415 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
417 *wxflag_ptr
= fdinfo
->wxflag
;
418 *handle_ptr
= fdinfo
->handle
;
423 *handle_ptr
= INVALID_HANDLE_VALUE
;
425 wxflag_ptr
++; handle_ptr
++;
430 /* INTERNAL: Set up all file descriptors,
431 * as well as default streams (stdin, stderr and stdout)
433 void msvcrt_init_io(void)
439 GetStartupInfoA(&si
);
440 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
446 count
= *(unsigned*)si
.lpReserved2
;
447 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
448 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
450 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
451 count
= min(count
, MAX_FILES
);
452 for (i
= 0; i
< count
; i
++)
454 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
455 set_fd(*handle_ptr
, *wxflag_ptr
, i
);
457 wxflag_ptr
++; handle_ptr
++;
459 fdend
= max( 3, count
);
460 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
461 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
464 fdinfo
= get_ioinfo(STDIN_FILENO
);
465 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
466 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
467 DWORD type
= GetFileType(h
);
469 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
470 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDIN_FILENO
);
473 fdinfo
= get_ioinfo(STDOUT_FILENO
);
474 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
475 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
476 DWORD type
= GetFileType(h
);
478 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
479 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDOUT_FILENO
);
482 fdinfo
= get_ioinfo(STDERR_FILENO
);
483 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
484 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
485 DWORD type
= GetFileType(h
);
487 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
488 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDERR_FILENO
);
491 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(STDIN_FILENO
)->handle
,
492 get_ioinfo(STDOUT_FILENO
)->handle
,
493 get_ioinfo(STDERR_FILENO
)->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 /*********************************************************************
524 int CDECL
_isatty(int fd
)
526 HANDLE hand
= fdtoh(fd
);
528 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
529 if (hand
== INVALID_HANDLE_VALUE
)
532 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
535 /* INTERNAL: Allocate stdio file buffer */
536 /*static*/ BOOL
alloc_buffer(FILE* file
)
538 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
539 && _isatty(file
->_file
))
542 file
->_base
= calloc(BUFSIZ
,1);
544 file
->_bufsiz
= BUFSIZ
;
545 file
->_flag
|= _IOMYBUF
;
547 file
->_base
= (char*)(&file
->_charbuf
);
549 file
->_bufsiz
= sizeof(file
->_charbuf
);
551 file
->_ptr
= file
->_base
;
556 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
557 static int int_to_base32(int num
, char *str
)
572 *p
= (num
& 31) + '0';
574 *p
+= ('a' - '0' - 10);
581 /* INTERNAL: wide character version of int_to_base32 */
582 static int int_to_base32_w(int num
, wchar_t *str
)
597 *p
= (num
& 31) + '0';
599 *p
+= ('a' - '0' - 10);
606 /* INTERNAL: Create a wide string from an ascii string */
607 wchar_t *msvcrt_wstrdupa(const char *str
)
609 const unsigned int len
= strlen(str
) + 1 ;
610 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
613 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
617 /*********************************************************************
618 * __iob_func(MSVCRT.@)
620 FILE * CDECL
__iob_func(void)
625 /*********************************************************************
628 int CDECL
_access(const char *filename
, int mode
)
630 DWORD attr
= GetFileAttributesA(filename
);
632 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
634 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
636 _dosmaperr(GetLastError());
639 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
641 _set_errno(ERROR_ACCESS_DENIED
);
647 /*********************************************************************
648 * _access_s (MSVCRT.@)
650 int CDECL
_access_s(const char *filename
, int mode
)
652 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
653 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
659 return _access(filename
, mode
);
662 /*********************************************************************
663 * _waccess (MSVCRT.@)
665 int CDECL
_waccess(const wchar_t *filename
, int mode
)
667 DWORD attr
= GetFileAttributesW(filename
);
669 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
671 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
673 _dosmaperr(GetLastError());
676 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
678 _set_errno(ERROR_ACCESS_DENIED
);
684 /*********************************************************************
685 * _waccess_s (MSVCRT.@)
687 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
689 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
690 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
696 return _waccess(filename
, mode
);
699 /*********************************************************************
702 int CDECL
_chmod(const char *path
, int flags
)
704 DWORD oldFlags
= GetFileAttributesA(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
|| SetFileAttributesA(path
, newFlags
))
714 _dosmaperr(GetLastError());
718 /*********************************************************************
721 int CDECL
_wchmod(const wchar_t *path
, int flags
)
723 DWORD oldFlags
= GetFileAttributesW(path
);
725 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
727 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
728 oldFlags
| FILE_ATTRIBUTE_READONLY
;
730 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
733 _dosmaperr(GetLastError());
737 /*********************************************************************
740 int CDECL
_unlink(const char *path
)
742 TRACE("%s\n",debugstr_a(path
));
743 if(DeleteFileA(path
))
745 TRACE("failed (%d)\n",GetLastError());
746 _dosmaperr(GetLastError());
750 /*********************************************************************
751 * _wunlink (MSVCRT.@)
753 int CDECL
_wunlink(const wchar_t *path
)
755 TRACE("(%s)\n",debugstr_w(path
));
756 if(DeleteFileW(path
))
758 TRACE("failed (%d)\n",GetLastError());
759 _dosmaperr(GetLastError());
763 /* _flushall calls fflush which calls _flushall */
764 int CDECL
fflush(FILE* file
);
766 /* INTERNAL: Flush all stream buffer */
767 static int flush_all_buffers(int mask
)
769 int i
, num_flushed
= 0;
773 for (i
= 3; i
< stream_idx
; i
++) {
778 if(file
->_flag
& mask
) {
786 TRACE(":flushed (%d) handles\n",num_flushed
);
790 /*********************************************************************
791 * _flushall (MSVCRT.@)
793 int CDECL
_flushall(void)
795 return flush_all_buffers(_IOWRT
| _IOREAD
);
798 /*********************************************************************
801 int CDECL
fflush(FILE* file
)
804 flush_all_buffers(_IOWRT
);
805 } else if(file
->_flag
& _IOWRT
) {
809 res
= flush_buffer(file
);
811 if(!res && (file->_flag & _IOCOMMIT))
812 res = _commit(file->_file) ? EOF : 0;
817 } else if(file
->_flag
& _IOREAD
) {
820 file
->_ptr
= file
->_base
;
828 /*********************************************************************
831 int CDECL
_close(int fd
)
838 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
839 if (hand
== INVALID_HANDLE_VALUE
)
841 else if (!CloseHandle(hand
))
843 WARN(":failed-last error (%d)\n",GetLastError());
844 _dosmaperr(GetLastError());
857 /*********************************************************************
860 int CDECL
_commit(int fd
)
862 HANDLE hand
= fdtoh(fd
);
864 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
865 if (hand
== INVALID_HANDLE_VALUE
)
868 if (!FlushFileBuffers(hand
))
870 if (GetLastError() == ERROR_INVALID_HANDLE
)
872 /* FlushFileBuffers fails for console handles
873 * so we ignore this error.
877 TRACE(":failed-last error (%d)\n",GetLastError());
878 _dosmaperr(GetLastError());
885 /*********************************************************************
888 * MSDN isn't clear on this point, but the remarks for _pipe
889 * indicate file descriptors duplicated with _dup and _dup2 are always
892 int CDECL
_dup2(int od
, int nd
)
896 TRACE("(od=%d, nd=%d)\n", od
, nd
);
898 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
902 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
903 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
905 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
909 ret
= set_fd(handle
, wxflag
, nd
);
917 /* _dup2 returns 0, not nd, on success */
924 _dosmaperr(GetLastError());
936 /*********************************************************************
939 int CDECL
_dup(int od
)
945 if (_dup2(od
, fd
) == 0)
953 /*********************************************************************
956 int CDECL
_eof(int fd
)
959 LONG hcurpos
,hendpos
;
960 HANDLE hand
= fdtoh(fd
);
962 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
964 if (hand
== INVALID_HANDLE_VALUE
)
967 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
969 /* Otherwise we do it the hard way */
970 hcurpos
= hendpos
= 0;
971 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
972 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
974 if (curpos
== endpos
&& hcurpos
== hendpos
)
976 /* FIXME: shouldn't WX_ATEOF be set here? */
980 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
984 /*********************************************************************
985 * _fcloseall (MSVCRT.@)
987 int CDECL
_fcloseall(void)
989 int num_closed
= 0, i
;
993 for (i
= 3; i
< stream_idx
; i
++) {
996 if (file
->_flag
&& !fclose(file
))
1001 TRACE(":closed (%d) handles\n",num_closed
);
1005 /* free everything on process exit */
1006 void msvcrt_free_io(void)
1014 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1017 for(j
=0; j
<stream_idx
; j
++)
1019 FILE *file
= get_file(j
);
1020 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1022 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1023 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1027 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
1030 DeleteCriticalSection(&file_cs
);
1033 /*********************************************************************
1034 * _lseeki64 (MSVCRT.@)
1036 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1038 HANDLE hand
= fdtoh(fd
);
1041 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1042 if (hand
== INVALID_HANDLE_VALUE
)
1045 if (whence
< 0 || whence
> 2)
1051 TRACE(":fd (%d) to %s pos %s\n",
1052 fd
,wine_dbgstr_longlong(offset
),
1053 (whence
==SEEK_SET
)?"SEEK_SET":
1054 (whence
==SEEK_CUR
)?"SEEK_CUR":
1055 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1057 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1058 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1059 ofs
.QuadPart
= offset
;
1060 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1061 GetLastError() == ERROR_SUCCESS
)
1063 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1064 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1066 return ofs
.QuadPart
;
1068 TRACE(":error-last error (%d)\n",GetLastError());
1069 _dosmaperr(GetLastError());
1073 /*********************************************************************
1076 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1078 return (LONG
)_lseeki64(fd
, offset
, whence
);
1081 /*********************************************************************
1082 * _lock_file (MSVCRT.@)
1084 void CDECL
_lock_file(FILE *file
)
1086 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1087 _lock(_STREAM_LOCKS
+(file
-_iob
));
1088 /* ReactOS: string streams dont need to be locked */
1089 else if(!(file
->_flag
& _IOSTRG
))
1090 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1093 /*********************************************************************
1094 * _unlock_file (MSVCRT.@)
1096 void CDECL
_unlock_file(FILE *file
)
1098 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1099 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1100 /* ReactOS: string streams dont need to be locked */
1101 else if(!(file
->_flag
& _IOSTRG
))
1102 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1106 /*********************************************************************
1107 * _locking (MSVCRT.@)
1109 * This is untested; the underlying LockFile doesn't work yet.
1111 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1115 HANDLE hand
= fdtoh(fd
);
1117 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1118 if (hand
== INVALID_HANDLE_VALUE
)
1121 if (mode
< 0 || mode
> 4)
1127 TRACE(":fd (%d) by 0x%08x mode %s\n",
1128 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1129 (mode
==_LK_LOCK
)?"_LK_LOCK":
1130 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1131 (mode
==_LK_RLCK
)?"_LK_RLCK":
1132 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1135 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1137 FIXME ("Seek failed\n");
1138 *_errno() = EINVAL
; /* FIXME */
1141 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1144 ret
= 1; /* just to satisfy gcc */
1147 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1152 else if (mode
== _LK_UNLCK
)
1153 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1155 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1156 /* FIXME - what about error settings? */
1157 return ret
? 0 : -1;
1160 /*********************************************************************
1161 * _fseeki64 (MSVCRT.@)
1163 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1168 /* Flush output if needed */
1169 if(file
->_flag
& _IOWRT
)
1172 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1174 offset
+= _ftelli64(file
);
1177 /* Discard buffered input */
1179 file
->_ptr
= file
->_base
;
1180 /* Reset direction of i/o */
1181 if(file
->_flag
& _IORW
) {
1182 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1184 /* Clear end of file flag */
1185 file
->_flag
&= ~_IOEOF
;
1186 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1192 /*********************************************************************
1195 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1197 return _fseeki64( file
, offset
, whence
);
1200 /*********************************************************************
1201 * _chsize (MSVCRT.@)
1203 int CDECL
_chsize(int fd
, long size
)
1209 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1214 if (handle
!= INVALID_HANDLE_VALUE
)
1216 /* save the current file pointer */
1217 cur
= _lseek(fd
, 0, SEEK_CUR
);
1220 pos
= _lseek(fd
, size
, SEEK_SET
);
1223 ret
= SetEndOfFile(handle
);
1224 if (!ret
) _dosmaperr(GetLastError());
1227 /* restore the file pointer */
1228 _lseek(fd
, cur
, SEEK_SET
);
1233 return ret
? 0 : -1;
1236 /*********************************************************************
1237 * clearerr (MSVCRT.@)
1239 void CDECL
clearerr(FILE* file
)
1241 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1244 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1248 /*********************************************************************
1251 void CDECL
rewind(FILE* file
)
1253 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1256 fseek(file
, 0L, SEEK_SET
);
1261 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1263 int plus
= strchrW(mode
, '+') != NULL
;
1265 TRACE("%s\n", debugstr_w(mode
));
1267 while(*mode
== ' ') mode
++;
1272 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1273 *stream_flags
= plus
? _IORW
: _IOREAD
;
1276 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1277 *stream_flags
= plus
? _IORW
: _IOWRT
;
1280 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1281 *stream_flags
= plus
? _IORW
: _IOWRT
;
1284 MSVCRT_INVALID_PMT(0, EINVAL
);
1288 *stream_flags
|= _commode
;
1290 while (*mode
&& *mode
!=',')
1294 *open_flags
|= _O_BINARY
;
1295 *open_flags
&= ~_O_TEXT
;
1298 *open_flags
|= _O_TEXT
;
1299 *open_flags
&= ~_O_BINARY
;
1302 *open_flags
|= _O_TEMPORARY
;
1305 *open_flags
|= _O_SHORT_LIVED
;
1308 *stream_flags
|= _IOCOMMIT
;
1311 *stream_flags
&= ~_IOCOMMIT
;
1314 *open_flags
|= _O_NOINHERIT
;
1323 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1326 ERR("incorrect mode flag: %c\n", mode
[-1]);
1332 static const WCHAR ccs
[] = {'c','c','s'};
1333 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1334 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1335 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1338 while(*mode
== ' ') mode
++;
1339 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1341 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1342 while(*mode
== ' ') mode
++;
1343 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1346 while(*mode
== ' ') mode
++;
1348 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1350 *open_flags
|= _O_U8TEXT
;
1351 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1353 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1355 *open_flags
|= _O_U16TEXT
;
1356 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1358 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1360 *open_flags
|= _O_WTEXT
;
1361 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1365 MSVCRT_INVALID_PMT(0, EINVAL
);
1369 while(*mode
== ' ') mode
++;
1372 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1377 /*********************************************************************
1378 * _fdopen (MSVCRT.@)
1380 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1383 wchar_t *modeW
= NULL
;
1385 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1387 ret
= _wfdopen(fd
, modeW
);
1393 /*********************************************************************
1394 * _wfdopen (MSVCRT.@)
1396 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1398 int open_flags
, stream_flags
;
1401 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1404 if (!(file
= alloc_fp()))
1406 else if (init_fp(file
, fd
, stream_flags
) == -1)
1411 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1417 /*********************************************************************
1418 * _filelength (MSVCRT.@)
1420 LONG CDECL
_filelength(int fd
)
1422 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1425 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1428 if (endPos
!= curPos
)
1429 _lseek(fd
, curPos
, SEEK_SET
);
1436 /*********************************************************************
1437 * _filelengthi64 (MSVCRT.@)
1439 __int64 CDECL
_filelengthi64(int fd
)
1441 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1444 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1447 if (endPos
!= curPos
)
1448 _lseeki64(fd
, curPos
, SEEK_SET
);
1455 /*********************************************************************
1456 * _fileno (MSVCRT.@)
1458 int CDECL
_fileno(FILE* file
)
1460 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1464 /*********************************************************************
1465 * _get_osfhandle (MSVCRT.@)
1467 intptr_t CDECL
_get_osfhandle(int fd
)
1469 HANDLE hand
= fdtoh(fd
);
1470 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1472 return (intptr_t)hand
;
1475 /*********************************************************************
1476 * _mktemp (MSVCRT.@)
1478 char * CDECL
_mktemp(char *pattern
)
1481 char *retVal
= pattern
;
1489 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1493 id
= GetCurrentProcessId();
1497 int tempNum
= id
/ 10;
1498 *pattern
-- = id
- (tempNum
* 10) + '0';
1504 *pattern
= letter
++;
1505 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1507 } while(letter
<= 'z');
1511 /*********************************************************************
1512 * _wmktemp (MSVCRT.@)
1514 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1517 wchar_t *retVal
= pattern
;
1519 wchar_t letter
= 'a';
1522 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1526 id
= GetCurrentProcessId();
1530 int tempNum
= id
/ 10;
1531 *pattern
-- = id
- (tempNum
* 10) + '0';
1537 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1538 GetLastError() == ERROR_FILE_NOT_FOUND
)
1540 *pattern
= letter
++;
1541 } while(letter
!= '|');
1545 /*static*/ unsigned split_oflags(unsigned oflags
)
1548 unsigned unsupp
; /* until we support everything */
1550 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1551 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1552 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1553 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1554 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1555 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1556 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1557 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1558 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1560 if ((unsupp
= oflags
& ~(
1561 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1562 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1563 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1565 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1566 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1568 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1573 /*********************************************************************
1576 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1579 SECURITY_ATTRIBUTES sa
;
1580 HANDLE readHandle
, writeHandle
;
1588 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1589 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1590 sa
.lpSecurityDescriptor
= NULL
;
1591 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1593 unsigned int wxflags
= split_oflags(textmode
);
1597 fd
= alloc_fd(readHandle
, wxflags
);
1601 fd
= alloc_fd(writeHandle
, wxflags
);
1610 CloseHandle(writeHandle
);
1616 CloseHandle(readHandle
);
1617 CloseHandle(writeHandle
);
1623 _dosmaperr(GetLastError());
1628 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1630 char bom
[sizeof(utf8_bom
)];
1633 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1635 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1638 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1639 oflags
|= _O_U8TEXT
;
1640 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1642 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1643 oflags
|= _O_U16TEXT
;
1645 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1651 /*********************************************************************
1652 * _wsopen_s (MSVCRT.@)
1654 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1656 DWORD access
= 0, creation
= 0, attrib
;
1657 SECURITY_ATTRIBUTES sa
;
1662 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1663 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1665 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1668 wxflag
= split_oflags(oflags
);
1669 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1671 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1672 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1673 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1676 if (oflags
& _O_CREAT
)
1678 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1679 FIXME(": pmode 0x%04x ignored\n", pmode
);
1681 WARN(": pmode 0x%04x ignored\n", pmode
);
1683 if (oflags
& _O_EXCL
)
1684 creation
= CREATE_NEW
;
1685 else if (oflags
& _O_TRUNC
)
1686 creation
= CREATE_ALWAYS
;
1688 creation
= OPEN_ALWAYS
;
1690 else /* no _O_CREAT */
1692 if (oflags
& _O_TRUNC
)
1693 creation
= TRUNCATE_EXISTING
;
1695 creation
= OPEN_EXISTING
;
1704 sharing
= FILE_SHARE_READ
;
1707 sharing
= FILE_SHARE_WRITE
;
1710 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1713 ERR( "Unhandled shflags 0x%x\n", shflags
);
1716 attrib
= FILE_ATTRIBUTE_NORMAL
;
1718 if (oflags
& _O_TEMPORARY
)
1720 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1722 sharing
|= FILE_SHARE_DELETE
;
1725 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1726 sa
.lpSecurityDescriptor
= NULL
;
1727 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1729 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1730 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1731 && !(access
&GENERIC_READ
))
1733 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1734 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1735 if (hand
!= INVALID_HANDLE_VALUE
)
1737 oflags
= check_bom(hand
, oflags
, FALSE
);
1741 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1744 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1745 if (hand
== INVALID_HANDLE_VALUE
) {
1746 WARN(":failed-last error (%d)\n",GetLastError());
1747 _dosmaperr(GetLastError());
1751 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1753 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1754 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1755 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1757 if (oflags
& _O_U8TEXT
)
1759 DWORD written
= 0, tmp
;
1761 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1762 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1764 if (written
!= sizeof(utf8_bom
)) {
1765 WARN("error writing BOM\n");
1767 _dosmaperr(GetLastError());
1773 DWORD written
= 0, tmp
;
1775 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1776 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1778 if (written
!= sizeof(utf16_bom
))
1780 WARN("error writing BOM\n");
1782 _dosmaperr(GetLastError());
1787 else if (access
& GENERIC_READ
)
1788 oflags
= check_bom(hand
, oflags
, TRUE
);
1791 *fd
= alloc_fd(hand
, wxflag
);
1795 if (oflags
& _O_WTEXT
)
1796 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1797 else if (oflags
& _O_U16TEXT
)
1798 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1799 else if (oflags
& _O_U8TEXT
)
1800 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1802 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1806 /*********************************************************************
1807 * _wsopen (MSVCRT.@)
1809 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1814 if (oflags
& _O_CREAT
)
1818 __ms_va_start(ap
, shflags
);
1819 pmode
= va_arg(ap
, int);
1825 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1829 /*********************************************************************
1830 * _sopen_s (MSVCRT.@)
1832 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1834 DWORD access
= 0, creation
= 0, attrib
;
1838 SECURITY_ATTRIBUTES sa
;
1840 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1841 fd
, path
, oflags
, shflags
, pmode
);
1845 MSVCRT_INVALID_PMT("null out fd pointer", EINVAL
);
1850 wxflag
= split_oflags(oflags
);
1851 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1853 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1854 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1855 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1858 if (oflags
& _O_CREAT
)
1860 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1861 FIXME(": pmode 0x%04x ignored\n", pmode
);
1863 WARN(": pmode 0x%04x ignored\n", pmode
);
1865 if (oflags
& _O_EXCL
)
1866 creation
= CREATE_NEW
;
1867 else if (oflags
& _O_TRUNC
)
1868 creation
= CREATE_ALWAYS
;
1870 creation
= OPEN_ALWAYS
;
1872 else /* no _O_CREAT */
1874 if (oflags
& _O_TRUNC
)
1875 creation
= TRUNCATE_EXISTING
;
1877 creation
= OPEN_EXISTING
;
1886 sharing
= FILE_SHARE_READ
;
1889 sharing
= FILE_SHARE_WRITE
;
1892 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1895 ERR( "Unhandled shflags 0x%x\n", shflags
);
1898 attrib
= FILE_ATTRIBUTE_NORMAL
;
1900 if (oflags
& _O_TEMPORARY
)
1902 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1904 sharing
|= FILE_SHARE_DELETE
;
1907 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1908 sa
.lpSecurityDescriptor
= NULL
;
1909 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1911 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1912 if (hand
== INVALID_HANDLE_VALUE
) {
1913 WARN(":failed-last error (%d)\n", GetLastError());
1914 _dosmaperr(GetLastError());
1918 *fd
= alloc_fd(hand
, wxflag
);
1920 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1924 /*********************************************************************
1927 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1932 if (oflags
& _O_CREAT
)
1936 va_start(ap
, shflags
);
1937 pmode
= va_arg(ap
, int);
1943 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1947 /*********************************************************************
1950 int CDECL
_open( const char *path
, int flags
, ... )
1954 if (flags
& _O_CREAT
)
1957 va_start(ap
, flags
);
1958 pmode
= va_arg(ap
, int);
1960 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1963 return _sopen( path
, flags
, _SH_DENYNO
);
1966 /*********************************************************************
1969 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1973 if (flags
& _O_CREAT
)
1976 va_start(ap
, flags
);
1977 pmode
= va_arg(ap
, int);
1979 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1982 return _wsopen( path
, flags
, _SH_DENYNO
);
1985 /*********************************************************************
1988 int CDECL
_creat(const char *path
, int flags
)
1990 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1991 return _open(path
, usedFlags
);
1994 /*********************************************************************
1995 * _wcreat (MSVCRT.@)
1997 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1999 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2000 return _wopen(path
, usedFlags
);
2003 /*********************************************************************
2004 * _open_osfhandle (MSVCRT.@)
2006 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2011 /* _O_RDONLY (0) always matches, so set the read flag
2012 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2013 * file, so set the write flag. It also only sets _O_TEXT if it wants
2014 * text - it never sets _O_BINARY.
2016 /* don't let split_oflags() decide the mode if no mode is passed */
2017 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2018 oflags
|= _O_BINARY
;
2020 flags
= GetFileType((HANDLE
)handle
);
2021 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2023 _dosmaperr(GetLastError());
2027 if (flags
== FILE_TYPE_CHAR
)
2029 else if (flags
== FILE_TYPE_PIPE
)
2033 flags
|= split_oflags(oflags
);
2035 fd
= alloc_fd((HANDLE
)handle
, flags
);
2036 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2040 /*********************************************************************
2043 int CDECL
_rmtmp(void)
2045 int num_removed
= 0, i
;
2049 for (i
= 3; i
< stream_idx
; i
++) {
2052 if (file
->_tmpfname
)
2061 TRACE(":removed (%d) temp files\n",num_removed
);
2065 static inline int get_utf8_char_len(char ch
)
2067 if((ch
&0xf8) == 0xf0)
2069 else if((ch
&0xf0) == 0xe0)
2071 else if((ch
&0xe0) == 0xc0)
2076 /*********************************************************************
2077 * (internal) read_utf8
2079 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2081 ioinfo
*fdinfo
= get_ioinfo(fd
);
2082 HANDLE hand
= fdinfo
->handle
;
2083 char min_buf
[4], *readbuf
, lookahead
;
2084 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2086 /* make the buffer big enough to hold at least one character */
2087 /* read bytes have to fit to output and lookahead buffers */
2089 readbuf_size
= count
< 4 ? 4 : count
;
2090 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2095 if(fdinfo
->lookahead
[0] != '\n') {
2096 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2097 fdinfo
->lookahead
[0] = '\n';
2099 if(fdinfo
->lookahead
[1] != '\n') {
2100 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2101 fdinfo
->lookahead
[1] = '\n';
2103 if(fdinfo
->lookahead
[2] != '\n') {
2104 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2105 fdinfo
->lookahead
[2] = '\n';
2110 /* NOTE: this case is broken in native dll, reading
2111 * sometimes fails when small buffer is passed
2114 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2115 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2116 fdinfo
->wxflag
|= WX_ATEOF
;
2119 _dosmaperr(GetLastError());
2122 }else if(!num_read
) {
2123 fdinfo
->wxflag
|= WX_ATEOF
;
2129 char_len
= get_utf8_char_len(readbuf
[0]);
2131 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2135 if(readbuf
[0] == '\n')
2136 fdinfo
->wxflag
|= WX_READNL
;
2138 fdinfo
->wxflag
&= ~WX_READNL
;
2140 if(readbuf
[0] == 0x1a) {
2141 fdinfo
->wxflag
|= WX_ATEOF
;
2145 if(readbuf
[0] == '\r') {
2146 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2148 else if(lookahead
== '\n')
2152 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2153 fdinfo
->lookahead
[0] = lookahead
;
2155 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2160 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2161 _dosmaperr(GetLastError());
2168 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2171 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2172 fdinfo
->wxflag
|= WX_ATEOF
;
2173 if (readbuf
!= min_buf
) free(readbuf
);
2176 _dosmaperr(GetLastError());
2177 if (readbuf
!= min_buf
) free(readbuf
);
2180 }else if(!pos
&& !num_read
) {
2181 fdinfo
->wxflag
|= WX_ATEOF
;
2182 if (readbuf
!= min_buf
) free(readbuf
);
2187 if(readbuf
[0] == '\n')
2188 fdinfo
->wxflag
|= WX_READNL
;
2190 fdinfo
->wxflag
&= ~WX_READNL
;
2192 /* Find first byte of last character (may be incomplete) */
2193 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2194 if((readbuf
[i
]&0xc0) != 0x80)
2196 char_len
= get_utf8_char_len(readbuf
[i
]);
2197 if(char_len
+i
<= pos
)
2200 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
)) {
2202 fdinfo
->lookahead
[0] = readbuf
[i
];
2204 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2206 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2208 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2212 for(i
=0, j
=0; i
<pos
; i
++) {
2213 if(readbuf
[i
] == 0x1a) {
2214 fdinfo
->wxflag
|= WX_ATEOF
;
2218 /* strip '\r' if followed by '\n' */
2219 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2220 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2221 readbuf
[j
++] = '\r';
2222 }else if(lookahead
== '\n' && j
==0) {
2223 readbuf
[j
++] = '\n';
2225 if(lookahead
!= '\n')
2226 readbuf
[j
++] = '\r';
2228 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2229 fdinfo
->lookahead
[0] = lookahead
;
2231 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2233 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2234 readbuf
[j
++] = readbuf
[i
];
2239 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2240 _dosmaperr(GetLastError());
2241 if (readbuf
!= min_buf
) free(readbuf
);
2245 if (readbuf
!= min_buf
) free(readbuf
);
2249 /*********************************************************************
2252 * When reading \r as last character in text mode, read() positions
2253 * the file pointer on the \r character while getc() goes on to
2256 static int read_i(int fd
, void *buf
, unsigned int count
)
2258 DWORD num_read
, utf16
;
2259 char *bufstart
= buf
;
2260 HANDLE hand
= fdtoh(fd
);
2261 ioinfo
*fdinfo
= get_ioinfo(fd
);
2266 if (fdinfo
->wxflag
& WX_ATEOF
) {
2267 TRACE("already at EOF, returning 0\n");
2270 /* Don't trace small reads, it gets *very* annoying */
2272 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2273 if (hand
== INVALID_HANDLE_VALUE
)
2279 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2280 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2286 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2287 return read_utf8(fd
, buf
, count
);
2289 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2291 if (fdinfo
->lookahead
[0] != '\n')
2293 bufstart
[0] = fdinfo
->lookahead
[0];
2294 fdinfo
->lookahead
[0] = '\n';
2298 bufstart
[1] = fdinfo
->lookahead
[1];
2299 fdinfo
->lookahead
[1] = '\n';
2302 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2303 num_read
+= 1+utf16
;
2308 if(utf16
&& (num_read
&1))
2310 /* msvcr90 uses uninitialized value from the buffer in this case */
2311 /* msvcrt ignores additional data */
2312 ERR("got odd number of bytes in UTF16 mode\n");
2316 if (count
!= 0 && num_read
== 0)
2318 fdinfo
->wxflag
|= WX_ATEOF
;
2319 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2321 else if (fdinfo
->wxflag
& WX_TEXT
)
2325 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2326 fdinfo
->wxflag
|= WX_READNL
;
2328 fdinfo
->wxflag
&= ~WX_READNL
;
2330 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2332 /* in text mode, a ctrl-z signals EOF */
2333 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2335 fdinfo
->wxflag
|= WX_ATEOF
;
2336 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2340 /* in text mode, strip \r if followed by \n */
2341 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2346 lookahead
[1] = '\n';
2347 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2349 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2351 bufstart
[j
++] = '\n';
2352 if(utf16
) bufstart
[j
++] = 0;
2356 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2358 bufstart
[j
++] = '\r';
2359 if(utf16
) bufstart
[j
++] = 0;
2362 if (fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2364 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2366 bufstart
[j
++] = '\n';
2367 if (utf16
) bufstart
[j
++] = 0;
2371 fdinfo
->lookahead
[0] = lookahead
[0];
2372 fdinfo
->lookahead
[1] = lookahead
[1];
2376 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2381 bufstart
[j
++] = '\r';
2382 if(utf16
) bufstart
[j
++] = 0;
2385 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2386 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2388 bufstart
[j
++] = bufstart
[i
];
2389 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2397 if (GetLastError() == ERROR_BROKEN_PIPE
)
2399 TRACE(":end-of-pipe\n");
2400 fdinfo
->wxflag
|= WX_ATEOF
;
2405 TRACE(":failed-last error (%d)\n",GetLastError());
2411 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2415 /*********************************************************************
2418 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2421 num_read
= read_i(fd
, buf
, count
);
2425 /*********************************************************************
2426 * _setmode (MSVCRT.@)
2428 int CDECL
_setmode(int fd
,int mode
)
2430 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2431 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2434 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2435 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2440 if(mode
== _O_BINARY
) {
2441 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2442 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2446 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2448 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2449 else if(mode
== _O_U8TEXT
)
2450 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2452 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2458 /*********************************************************************
2461 long CDECL
_tell(int fd
)
2463 return _lseek(fd
, 0, SEEK_CUR
);
2466 /*********************************************************************
2467 * _telli64 (MSVCRT.@)
2469 __int64 CDECL
_telli64(int fd
)
2471 return _lseeki64(fd
, 0, SEEK_CUR
);
2474 /*********************************************************************
2475 * _tempnam (MSVCRT.@)
2477 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2479 char tmpbuf
[MAX_PATH
];
2480 const char *tmp_dir
= getenv("TMP");
2482 if (tmp_dir
) dir
= tmp_dir
;
2484 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2485 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2487 TRACE("got name (%s)\n",tmpbuf
);
2488 DeleteFileA(tmpbuf
);
2489 return _strdup(tmpbuf
);
2491 TRACE("failed (%d)\n",GetLastError());
2495 /*********************************************************************
2496 * _wtempnam (MSVCRT.@)
2498 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2500 wchar_t tmpbuf
[MAX_PATH
];
2502 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2503 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2505 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2506 DeleteFileW(tmpbuf
);
2507 return _wcsdup(tmpbuf
);
2509 TRACE("failed (%d)\n",GetLastError());
2513 /*********************************************************************
2516 int CDECL
_umask(int umask
)
2518 int old_umask
= umask
;
2519 TRACE("(%d)\n",umask
);
2520 MSVCRT_umask
= umask
;
2524 /*********************************************************************
2527 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2530 ioinfo
*info
= get_ioinfo(fd
);
2531 HANDLE hand
= info
->handle
;
2533 /* Don't trace small writes, it gets *very* annoying */
2536 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2538 if (hand
== INVALID_HANDLE_VALUE
)
2544 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2550 /* If appending, go to EOF */
2551 if (info
->wxflag
& WX_APPEND
)
2552 _lseek(fd
, 0, FILE_END
);
2554 if (!(info
->wxflag
& WX_TEXT
))
2556 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2557 && (num_written
== count
))
2559 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2560 hand
, GetLastError());
2565 unsigned int i
, j
, nr_lf
, size
;
2568 const char *s
= buf
, *buf_start
= buf
;
2570 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2572 /* find number of \n */
2573 for (nr_lf
=0, i
=0; i
<count
; i
++)
2579 if ((q
= p
= malloc(size
)))
2581 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2590 FIXME("Malloc failed\n");
2602 else if (info
->exflag
& EF_UTF16
)
2604 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2605 if (s
[i
]=='\n' && s
[i
+1]==0)
2610 if ((q
= p
= malloc(size
)))
2612 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2614 if (s
[i
]=='\n' && s
[i
+1]==0)
2625 FIXME("Malloc failed\n");
2641 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2642 if (s
[i
]=='\n' && s
[i
+1]==0)
2645 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2647 _dosmaperr(GetLastError());
2652 size
= conv_len
+nr_lf
;
2653 if((p
= malloc(count
+nr_lf
*2+size
)))
2655 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2657 if (s
[i
]=='\n' && s
[i
+1]==0)
2665 q
= p
+count
+nr_lf
*2;
2666 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2667 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2671 FIXME("Malloc failed\n");
2678 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2682 if (num_written
!= size
)
2684 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2685 fd
, hand
, GetLastError(), num_written
);
2687 return s
- buf_start
;
2695 /*********************************************************************
2698 int CDECL
_putw(int val
, FILE* file
)
2703 len
= _write(file
->_file
, &val
, sizeof(val
));
2704 if (len
== sizeof(val
)) {
2709 file
->_flag
|= _IOERR
;
2714 /*********************************************************************
2717 int CDECL
fclose(FILE* file
)
2723 free(file
->_tmpfname
);
2724 file
->_tmpfname
= NULL
;
2725 /* flush stdio buffers */
2726 if(file
->_flag
& _IOWRT
)
2728 if(file
->_flag
& _IOMYBUF
)
2731 r
=_close(file
->_file
);
2735 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2736 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2738 if(file
== get_file(stream_idx
-1)) {
2739 while(stream_idx
>3 && !file
->_flag
) {
2741 file
= get_file(stream_idx
-1);
2745 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2748 /*********************************************************************
2751 int CDECL
feof(FILE* file
)
2753 return file
->_flag
& _IOEOF
;
2756 /*********************************************************************
2759 int CDECL
ferror(FILE* file
)
2761 return file
->_flag
& _IOERR
;
2764 /*********************************************************************
2765 * _filbuf (MSVCRT.@)
2767 int CDECL
_filbuf(FILE* file
)
2772 if(file
->_flag
& _IOSTRG
) {
2777 /* Allocate buffer if needed */
2778 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2781 if(!(file
->_flag
& _IOREAD
)) {
2782 if(file
->_flag
& _IORW
)
2783 file
->_flag
|= _IOREAD
;
2790 if(file
->_flag
& _IONBF
) {
2792 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2793 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2801 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2803 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2810 file
->_ptr
= file
->_base
+1;
2811 c
= *(unsigned char *)file
->_base
;
2817 /*********************************************************************
2820 int CDECL
fgetc(FILE* file
)
2828 i
= (unsigned char *)file
->_ptr
++;
2837 /*********************************************************************
2838 * _fgetchar (MSVCRT.@)
2840 int CDECL
_fgetchar(void)
2842 return fgetc(stdin
);
2845 /*********************************************************************
2848 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2851 char * buf_start
= s
;
2853 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2854 file
,file
->_file
,s
,size
);
2858 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2863 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2865 TRACE(":nothing read\n");
2869 if ((cc
!= EOF
) && (size
> 1))
2872 TRACE(":got %s\n", debugstr_a(buf_start
));
2877 /*********************************************************************
2880 wint_t CDECL
fgetwc(FILE* file
)
2887 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2888 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2891 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2900 char mbs
[MB_LEN_MAX
];
2906 if(isleadbyte((unsigned char)mbs
[0])) {
2917 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2925 /*********************************************************************
2928 int CDECL
_getw(FILE* file
)
2936 for (j
=0; j
<sizeof(int); j
++) {
2939 file
->_flag
|= _IOEOF
;
2950 /*********************************************************************
2953 wint_t CDECL
getwc(FILE* file
)
2955 return fgetwc(file
);
2958 /*********************************************************************
2959 * _fgetwchar (MSVCRT.@)
2961 wint_t CDECL
_fgetwchar(void)
2963 return fgetwc(stdin
);
2966 /*********************************************************************
2967 * getwchar (MSVCRT.@)
2969 wint_t CDECL
getwchar(void)
2971 return _fgetwchar();
2974 /*********************************************************************
2977 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2980 wchar_t * buf_start
= s
;
2982 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2983 file
,file
->_file
,s
,size
);
2987 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2992 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2994 TRACE(":nothing read\n");
2998 if ((cc
!= WEOF
) && (size
> 1))
3001 TRACE(":got %s\n", debugstr_w(buf_start
));
3006 /*********************************************************************
3009 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3011 size_t wrcnt
=size
* nmemb
;
3020 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3021 memcpy(file
->_ptr
, ptr
, pcnt
);
3026 ptr
= (const char*)ptr
+ pcnt
;
3027 } else if(!file
->_bufsiz
&& (file
->_flag
& _IONBF
)) {
3028 if(!(file
->_flag
& _IOWRT
)) {
3029 if(file
->_flag
& _IORW
)
3030 file
->_flag
|= _IOWRT
;
3035 if(_write(file
->_file
, ptr
, wrcnt
) <= 0) {
3036 file
->_flag
|= _IOERR
;
3042 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3046 ptr
= (const char*)ptr
+ 1;
3051 return written
/ size
;
3054 /*********************************************************************
3056 * FORKED for ReactOS, don't sync with Wine!
3058 * - http://jira.reactos.org/browse/CORE-6495
3059 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3061 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3063 /* If this is a real file stream (and not some temporary one for
3064 sprintf-like functions), check whether it is opened in text mode.
3065 In this case, we have to perform an implicit conversion to ANSI. */
3066 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
3068 /* Convert to multibyte in text mode */
3069 char mbc
[MB_LEN_MAX
];
3072 mb_return
= wctomb(mbc
, c
);
3077 /* Output all characters */
3078 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3083 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3090 /*********************************************************************
3091 * _fputwchar (MSVCRT.@)
3093 wint_t CDECL
_fputwchar(wint_t wc
)
3095 return fputwc(wc
, stdout
);
3098 /*********************************************************************
3099 * _wfsopen (MSVCRT.@)
3101 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3104 int open_flags
, stream_flags
, fd
;
3106 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3108 /* map mode string to open() flags. "man fopen" for possibilities. */
3109 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3113 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3116 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
3118 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3125 TRACE(":got (%p)\n",file
);
3126 if (fd
>= 0 && !file
)
3132 /*********************************************************************
3133 * _fsopen (MSVCRT.@)
3135 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3138 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3140 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3141 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3145 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3148 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3153 ret
= _wfsopen(pathW
, modeW
, share
);
3160 /*********************************************************************
3163 FILE * CDECL
fopen(const char *path
, const char *mode
)
3165 return _fsopen( path
, mode
, _SH_DENYNO
);
3168 /*********************************************************************
3169 * fopen_s (MSVCRT.@)
3171 int CDECL
fopen_s(FILE** pFile
,
3172 const char *filename
, const char *mode
)
3174 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
3175 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
3176 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
3178 *pFile
= fopen(filename
, mode
);
3185 /*********************************************************************
3186 * _wfopen (MSVCRT.@)
3188 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3190 return _wfsopen( path
, mode
, _SH_DENYNO
);
3193 /*********************************************************************
3194 * _wfopen_s (MSVCRT.@)
3196 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3197 const wchar_t *mode
)
3199 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3200 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3205 *pFile
= _wfopen(filename
, mode
);
3212 /* fputc calls _flsbuf which calls fputc */
3213 int CDECL
_flsbuf(int c
, FILE* file
);
3215 /*********************************************************************
3218 int CDECL
fputc(int c
, FILE* file
)
3228 res
= flush_buffer(file
);
3230 return res
? res
: c
;
3237 res
= _flsbuf(c
, file
);
3243 /*********************************************************************
3244 * _fputchar (MSVCRT.@)
3246 int CDECL
_fputchar(int c
)
3248 return fputc(c
, stdout
);
3251 /*********************************************************************
3254 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3256 size_t rcnt
=size
* nmemb
;
3265 /* first buffered data */
3267 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
3268 memcpy(ptr
, file
->_ptr
, pcnt
);
3273 ptr
= (char*)ptr
+ pcnt
;
3274 } else if(!(file
->_flag
& _IOREAD
)) {
3275 if(file
->_flag
& _IORW
) {
3276 file
->_flag
|= _IOREAD
;
3285 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& !(file
->_flag
& _IONBF
)
3286 && (file
->_bufsiz
!= 0 || alloc_buffer(file
))) {
3287 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3288 file
->_ptr
= file
->_base
;
3289 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3290 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3291 if (i
> 0 && i
< file
->_cnt
) {
3292 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3293 file
->_flag
&= ~_IOEOF
;
3296 memcpy(ptr
, file
->_ptr
, i
);
3300 } else if (rcnt
> UINT_MAX
) {
3301 i
= _read(file
->_file
, ptr
, UINT_MAX
);
3302 } else if (rcnt
< BUFSIZ
) {
3303 i
= _read(file
->_file
, ptr
, rcnt
);
3305 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3309 ptr
= (char *)ptr
+i
;
3310 /* expose feof condition in the flags
3311 * MFC tests file->_flag for feof, and doesn't call feof())
3313 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3314 file
->_flag
|= _IOEOF
;
3317 file
->_flag
|= _IOERR
;
3328 /*********************************************************************
3329 * _wfreopen (MSVCRT.@)
3332 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3334 int open_flags
, stream_flags
, fd
;
3336 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3339 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3344 /* map mode string to open() flags. "man fopen" for possibilities. */
3345 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3349 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3352 else if (init_fp(file
, fd
, stream_flags
) == -1)
3355 WARN(":failed-last error (%d)\n",GetLastError());
3356 _dosmaperr(GetLastError());
3365 /*********************************************************************
3366 * freopen (MSVCRT.@)
3369 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3372 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3374 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3375 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3381 ret
= _wfreopen(pathW
, modeW
, file
);
3388 /*********************************************************************
3389 * fsetpos (MSVCRT.@)
3391 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3396 /* Note that all this has been lifted 'as is' from fseek */
3397 if(file
->_flag
& _IOWRT
)
3400 /* Discard buffered input */
3402 file
->_ptr
= file
->_base
;
3404 /* Reset direction of i/o */
3405 if(file
->_flag
& _IORW
) {
3406 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3409 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3414 /*********************************************************************
3415 * _ftelli64 (MSVCRT.@)
3417 __int64 CDECL
_ftelli64(FILE* file
)
3422 pos
= _telli64(file
->_file
);
3428 if(file
->_flag
& _IOWRT
) {
3429 pos
+= file
->_ptr
- file
->_base
;
3431 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3434 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3438 } else if(!file
->_cnt
) { /* nothing to do */
3439 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3443 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3444 for(i
=0; i
<file
->_cnt
; i
++)
3445 if(file
->_ptr
[i
] == '\n')
3451 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3456 pos
-= file
->_bufsiz
;
3457 pos
+= file
->_ptr
- file
->_base
;
3459 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3460 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3463 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3474 /*********************************************************************
3477 LONG CDECL
ftell(FILE* file
)
3479 return (LONG
)_ftelli64(file
);
3482 /*********************************************************************
3483 * fgetpos (MSVCRT.@)
3485 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3490 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
3496 if( file
->_flag
& _IOWRT
) {
3497 off
= file
->_ptr
- file
->_base
;
3500 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3501 /* Black magic correction for CR removal */
3503 for (i
=0; i
<file
->_cnt
; i
++) {
3504 if (file
->_ptr
[i
] == '\n')
3507 /* Black magic when reading CR at buffer boundary*/
3508 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3518 /*********************************************************************
3521 int CDECL
fputs(const char *s
, FILE* file
)
3523 size_t i
, len
= strlen(s
);
3527 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3528 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3532 for (i
=0; i
<len
; i
++)
3533 if (fputc(s
[i
], file
) == EOF
) {
3542 /*********************************************************************
3545 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3547 size_t i
, len
= strlenW(s
);
3551 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3552 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3556 for (i
=0; i
<len
; i
++) {
3557 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
3558 || fputwc(s
[i
], file
) == WEOF
) {
3568 /*********************************************************************
3569 * getchar (MSVCRT.@)
3571 int CDECL
getchar(void)
3573 return fgetc(stdin
);
3576 /*********************************************************************
3579 int CDECL
getc(FILE* file
)
3584 /*********************************************************************
3587 char * CDECL
gets(char *buf
)
3590 char * buf_start
= buf
;
3593 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3595 if(cc
!= '\r') *buf
++ = (char)cc
;
3599 TRACE("got '%s'\n", buf_start
);
3600 _unlock_file(stdin
);
3604 /*********************************************************************
3607 wchar_t* CDECL
_getws(wchar_t* buf
)
3613 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3617 *buf
++ = (wchar_t)cc
;
3621 TRACE("got %s\n", debugstr_w(ws
));
3622 _unlock_file(stdin
);
3626 /*********************************************************************
3629 int CDECL
putc(int c
, FILE* file
)
3631 return fputc(c
, file
);
3634 /*********************************************************************
3635 * putchar (MSVCRT.@)
3637 int CDECL
putchar(int c
)
3639 return fputc(c
, stdout
);
3642 /*********************************************************************
3643 * _putwch (MSVCRT.@)
3645 wint_t CDECL
_putwch(wchar_t c
)
3647 return fputwc(c
, stdout
);
3650 /*********************************************************************
3653 int CDECL
puts(const char *s
)
3655 size_t len
= strlen(s
);
3659 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3660 _unlock_file(stdout
);
3664 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3665 _unlock_file(stdout
);
3669 /*********************************************************************
3672 int CDECL
_putws(const wchar_t *s
)
3674 static const wchar_t nl
= '\n';
3675 size_t len
= strlenW(s
);
3679 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3680 _unlock_file(stdout
);
3684 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3685 _unlock_file(stdout
);
3689 /*********************************************************************
3692 int CDECL
remove(const char *path
)
3694 TRACE("(%s)\n",path
);
3695 if (DeleteFileA(path
))
3697 TRACE(":failed (%d)\n",GetLastError());
3698 _dosmaperr(GetLastError());
3702 /*********************************************************************
3703 * _wremove (MSVCRT.@)
3705 int CDECL
_wremove(const wchar_t *path
)
3707 TRACE("(%s)\n",debugstr_w(path
));
3708 if (DeleteFileW(path
))
3710 TRACE(":failed (%d)\n",GetLastError());
3711 _dosmaperr(GetLastError());
3715 /*********************************************************************
3718 int CDECL
rename(const char *oldpath
,const char *newpath
)
3720 TRACE(":from %s to %s\n",oldpath
,newpath
);
3721 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3723 TRACE(":failed (%d)\n",GetLastError());
3724 _dosmaperr(GetLastError());
3728 /*********************************************************************
3729 * _wrename (MSVCRT.@)
3731 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3733 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3734 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3736 TRACE(":failed (%d)\n",GetLastError());
3737 _dosmaperr(GetLastError());
3741 /*********************************************************************
3742 * setvbuf (MSVCRT.@)
3744 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3752 if(mode
== _IOFBF
) {
3753 file
->_flag
&= ~_IONBF
;
3754 file
->_base
= file
->_ptr
= buf
;
3756 file
->_bufsiz
= size
;
3759 file
->_flag
|= _IONBF
;
3765 /*********************************************************************
3768 void CDECL
setbuf(FILE* file
, char *buf
)
3770 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3773 /*********************************************************************
3776 char * CDECL
tmpnam(char *s
)
3783 thread_data_t
*data
= msvcrt_get_thread_data();
3785 if(!data
->tmpnam_buffer
)
3786 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3788 s
= data
->tmpnam_buffer
;
3791 int_to_base32(GetCurrentProcessId(), tmpstr
);
3792 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3793 for (count
= 0; count
< TMP_MAX
; count
++)
3795 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3796 memcpy(p
, tmpstr
, size
);
3797 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3798 GetLastError() == ERROR_FILE_NOT_FOUND
)
3804 /*********************************************************************
3805 * _wtmpnam (MSVCRT.@)
3807 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3809 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3814 thread_data_t
*data
= msvcrt_get_thread_data();
3816 if(!data
->wtmpnam_buffer
)
3817 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3819 s
= data
->wtmpnam_buffer
;
3822 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3823 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3824 for (count
= 0; count
< TMP_MAX
; count
++)
3826 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3827 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3828 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3829 GetLastError() == ERROR_FILE_NOT_FOUND
)
3835 /*********************************************************************
3836 * tmpfile (MSVCRT.@)
3838 FILE* CDECL
tmpfile(void)
3840 char *filename
= tmpnam(NULL
);
3845 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3846 _S_IREAD
| _S_IWRITE
);
3847 if (fd
!= -1 && (file
= alloc_fp()))
3849 if (init_fp(file
, fd
, _IORW
) == -1)
3854 else file
->_tmpfname
= _strdup(filename
);
3857 if(fd
!= -1 && !file
)
3863 /*********************************************************************
3866 int CDECL
ungetc(int c
, FILE * file
)
3872 if(file
->_bufsiz
== 0 && alloc_buffer(file
))
3874 if(file
->_ptr
>file
->_base
) {
3876 if(file
->_flag
& _IOSTRG
) {
3877 if(*file
->_ptr
!= c
) {
3895 /*********************************************************************
3896 * ungetwc (MSVCRT.@)
3898 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3901 char * pp
= (char *)&mwc
;
3905 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3906 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3918 /*********************************************************************
3919 * _getmaxstdio (MSVCRT.@)
3921 int CDECL
_getmaxstdio(void)
3926 /*********************************************************************
3927 * _setmaxstdio (MSVCRT.@)
3929 int CDECL
_setmaxstdio(int newmax
)
3931 TRACE("%d\n", newmax
);
3933 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3936 max_streams
= newmax
;