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 #define MSVCRT_INTERNAL_BUFSIZ 4096
115 /* ioinfo structure size is different in msvcrXX.dll's */
118 unsigned char wxflag
;
121 CRITICAL_SECTION crit
;
124 /*********************************************************************
125 * __pioinfo (MSVCRT.@)
126 * array of pointers to ioinfo arrays [64]
128 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
130 /*********************************************************************
131 * __badioinfo (MSVCRT.@)
133 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
135 static int fdstart
= 3; /* first unallocated fd */
136 static int fdend
= 3; /* highest allocated fd */
140 CRITICAL_SECTION crit
;
143 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
144 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
145 static int max_streams
= 512, stream_idx
;
147 /* INTERNAL: process umask */
148 static int MSVCRT_umask
= 0;
150 /* INTERNAL: static data for tmpnam and _wtmpname functions */
151 static int tmpnam_unique
;
153 /* This critical section protects the tables __pioinfo and fstreams,
154 * and their related indexes, fdstart, fdend,
155 * and stream_idx, from race conditions.
156 * It doesn't protect against race conditions manipulating the underlying files
157 * or flags; doing so would probably be better accomplished with per-file
158 * protection, rather than locking the whole table for every change.
160 static CRITICAL_SECTION file_cs
;
161 static CRITICAL_SECTION_DEBUG file_cs_debug
=
164 { &file_cs_debug
.ProcessLocksList
, &file_cs_debug
.ProcessLocksList
},
165 0, 0, { (DWORD_PTR
)(__FILE__
": file_cs") }
167 static CRITICAL_SECTION file_cs
= { &file_cs_debug
, -1, 0, 0, 0, 0 };
168 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
169 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
171 static inline ioinfo
* get_ioinfo(int fd
)
175 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
179 return ret
+ (fd
%FD_BLOCK_SIZE
);
182 static inline FILE* get_file(int i
)
192 ret
= fstream
[i
/FD_BLOCK_SIZE
];
194 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
195 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
196 ERR("out of memory\n");
201 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
203 ret
+= i
%FD_BLOCK_SIZE
;
208 static inline BOOL
is_valid_fd(int fd
)
210 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
213 /* INTERNAL: Get the HANDLE for a fd
214 * This doesn't lock the table, because a failure will result in
215 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
216 * it returns a valid handle which is about to be closed, a subsequent call
217 * will fail, most likely in a sane way.
219 /*static*/ HANDLE
fdtoh(int fd
)
221 if (!is_valid_fd(fd
))
223 WARN(":fd (%d) - no handle!\n",fd
);
226 return INVALID_HANDLE_VALUE
;
228 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
229 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
230 return get_ioinfo(fd
)->handle
;
233 /* INTERNAL: free a file entry fd */
234 static void free_fd(int fd
)
239 fdinfo
= get_ioinfo(fd
);
240 if(fdinfo
!= &__badioinfo
)
242 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
245 TRACE(":fd (%d) freed\n",fd
);
252 SetStdHandle(STD_INPUT_HANDLE
, 0);
255 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
258 SetStdHandle(STD_ERROR_HANDLE
, 0);
270 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
271 /* caller must hold the files lock */
272 static int set_fd(HANDLE hand
, int flag
, int fd
)
278 WARN(":files exhausted!\n");
283 fdinfo
= get_ioinfo(fd
);
284 if(fdinfo
== &__badioinfo
) {
287 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
288 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
289 WARN(":out of memory!\n");
294 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
295 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
297 fdinfo
= get_ioinfo(fd
);
300 fdinfo
->handle
= hand
;
301 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_NOSEEK
));
302 fdinfo
->lookahead
[0] = '\n';
303 fdinfo
->lookahead
[1] = '\n';
304 fdinfo
->lookahead
[2] = '\n';
307 /* locate next free slot */
308 if (fd
== fdstart
&& fd
== fdend
)
311 while (fdstart
< fdend
&&
312 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
314 /* update last fd in use */
317 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
321 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
322 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
323 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
329 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
330 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
335 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
336 ret
= set_fd(hand
, flag
, fdstart
);
341 /* INTERNAL: Allocate a FILE* for an fd slot */
342 /* caller must hold the files lock */
343 static FILE* alloc_fp(void)
348 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
354 if (file
->_flag
== 0)
356 if (i
== stream_idx
) stream_idx
++;
364 /* INTERNAL: initialize a FILE* from an open fd */
365 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
367 TRACE(":fd (%d) allocating FILE*\n",fd
);
368 if (!is_valid_fd(fd
))
370 WARN(":invalid fd %d\n",fd
);
375 file
->_ptr
= file
->_base
= NULL
;
378 file
->_flag
= stream_flags
;
379 file
->_tmpfname
= NULL
;
381 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
382 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
384 TRACE(":got FILE* (%p)\n",file
);
388 /* INTERNAL: Create an inheritance data block (for spawned process)
389 * The inheritance block is made of:
390 * 00 int nb of file descriptor (NBFD)
391 * 04 char file flags (wxflag): repeated for each fd
392 * 4+NBFD HANDLE file handle: repeated for each fd
394 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
401 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
402 *block
= calloc(*size
, 1);
408 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
409 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
411 *(unsigned*)*block
= fdend
;
412 for (fd
= 0; fd
< fdend
; fd
++)
414 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
415 fdinfo
= get_ioinfo(fd
);
416 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
418 *wxflag_ptr
= fdinfo
->wxflag
;
419 *handle_ptr
= fdinfo
->handle
;
424 *handle_ptr
= INVALID_HANDLE_VALUE
;
426 wxflag_ptr
++; handle_ptr
++;
431 /* INTERNAL: Set up all file descriptors,
432 * as well as default streams (stdin, stderr and stdout)
434 void msvcrt_init_io(void)
440 GetStartupInfoA(&si
);
441 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
447 count
= *(unsigned*)si
.lpReserved2
;
448 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
449 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
451 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
452 count
= min(count
, MAX_FILES
);
453 for (i
= 0; i
< count
; i
++)
455 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
456 set_fd(*handle_ptr
, *wxflag_ptr
, i
);
458 wxflag_ptr
++; handle_ptr
++;
460 fdend
= max( 3, count
);
461 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
462 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
465 fdinfo
= get_ioinfo(STDIN_FILENO
);
466 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
467 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
468 DWORD type
= GetFileType(h
);
470 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
471 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDIN_FILENO
);
474 fdinfo
= get_ioinfo(STDOUT_FILENO
);
475 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
476 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
477 DWORD type
= GetFileType(h
);
479 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
480 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDOUT_FILENO
);
483 fdinfo
= get_ioinfo(STDERR_FILENO
);
484 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
485 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
486 DWORD type
= GetFileType(h
);
488 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
489 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDERR_FILENO
);
492 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(STDIN_FILENO
)->handle
,
493 get_ioinfo(STDOUT_FILENO
)->handle
,
494 get_ioinfo(STDERR_FILENO
)->handle
);
496 memset(_iob
,0,3*sizeof(FILE));
497 for (i
= 0; i
< 3; i
++)
499 /* FILE structs for stdin/out/err are static and never deleted */
501 _iob
[i
]._tmpfname
= NULL
;
502 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
507 /* INTERNAL: Flush stdio file buffer */
508 static int flush_buffer(FILE* file
)
510 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
511 int cnt
=file
->_ptr
-file
->_base
;
512 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
513 file
->_flag
|= _IOERR
;
516 file
->_ptr
=file
->_base
;
522 /*********************************************************************
525 int CDECL
_isatty(int fd
)
527 HANDLE hand
= fdtoh(fd
);
529 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
530 if (hand
== INVALID_HANDLE_VALUE
)
533 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
536 /* INTERNAL: Allocate stdio file buffer */
537 /*static*/ BOOL
alloc_buffer(FILE* file
)
539 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
540 && _isatty(file
->_file
))
543 file
->_base
= calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
545 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
546 file
->_flag
|= _IOMYBUF
;
548 file
->_base
= (char*)(&file
->_charbuf
);
550 file
->_flag
|= _IONBF
;
552 file
->_ptr
= file
->_base
;
557 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
558 static BOOL
add_std_buffer(FILE *file
)
560 static char buffers
[2][BUFSIZ
];
562 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
563 || !_isatty(file
->_file
) || file
->_bufsiz
)
566 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
567 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
571 /* INTERNAL: Removes temporary buffer from stdout or stderr */
572 /* Only call this function when add_std_buffer returned TRUE */
573 static void remove_std_buffer(FILE *file
)
576 file
->_ptr
= file
->_base
= NULL
;
577 file
->_bufsiz
= file
->_cnt
= 0;
580 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
581 static int int_to_base32(int num
, char *str
)
596 *p
= (num
& 31) + '0';
598 *p
+= ('a' - '0' - 10);
605 /* INTERNAL: wide character version of int_to_base32 */
606 static int int_to_base32_w(int num
, wchar_t *str
)
621 *p
= (num
& 31) + '0';
623 *p
+= ('a' - '0' - 10);
630 /* INTERNAL: Create a wide string from an ascii string */
631 wchar_t *msvcrt_wstrdupa(const char *str
)
633 const unsigned int len
= strlen(str
) + 1 ;
634 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
637 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
641 /*********************************************************************
642 * __iob_func(MSVCRT.@)
644 FILE * CDECL
__iob_func(void)
649 /*********************************************************************
652 int CDECL
_access(const char *filename
, int mode
)
654 DWORD attr
= GetFileAttributesA(filename
);
656 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
658 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
660 _dosmaperr(GetLastError());
663 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
665 _set_errno(ERROR_ACCESS_DENIED
);
671 /*********************************************************************
672 * _access_s (MSVCRT.@)
674 int CDECL
_access_s(const char *filename
, int mode
)
676 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
677 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
683 return _access(filename
, mode
);
686 /*********************************************************************
687 * _waccess (MSVCRT.@)
689 int CDECL
_waccess(const wchar_t *filename
, int mode
)
691 DWORD attr
= GetFileAttributesW(filename
);
693 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
695 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
697 _dosmaperr(GetLastError());
700 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
702 _set_errno(ERROR_ACCESS_DENIED
);
708 /*********************************************************************
709 * _waccess_s (MSVCRT.@)
711 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
713 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
714 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
720 return _waccess(filename
, mode
);
723 /*********************************************************************
726 int CDECL
_chmod(const char *path
, int flags
)
728 DWORD oldFlags
= GetFileAttributesA(path
);
730 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
732 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
733 oldFlags
| FILE_ATTRIBUTE_READONLY
;
735 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
738 _dosmaperr(GetLastError());
742 /*********************************************************************
745 int CDECL
_wchmod(const wchar_t *path
, int flags
)
747 DWORD oldFlags
= GetFileAttributesW(path
);
749 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
751 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
752 oldFlags
| FILE_ATTRIBUTE_READONLY
;
754 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
757 _dosmaperr(GetLastError());
761 /*********************************************************************
764 int CDECL
_unlink(const char *path
)
766 TRACE("%s\n",debugstr_a(path
));
767 if(DeleteFileA(path
))
769 TRACE("failed (%d)\n",GetLastError());
770 _dosmaperr(GetLastError());
774 /*********************************************************************
775 * _wunlink (MSVCRT.@)
777 int CDECL
_wunlink(const wchar_t *path
)
779 TRACE("(%s)\n",debugstr_w(path
));
780 if(DeleteFileW(path
))
782 TRACE("failed (%d)\n",GetLastError());
783 _dosmaperr(GetLastError());
787 /* _flushall calls fflush which calls _flushall */
788 int CDECL
fflush(FILE* file
);
790 /* INTERNAL: Flush all stream buffer */
791 static int flush_all_buffers(int mask
)
793 int i
, num_flushed
= 0;
797 for (i
= 0; i
< stream_idx
; i
++) {
802 if(file
->_flag
& mask
) {
810 TRACE(":flushed (%d) handles\n",num_flushed
);
814 /*********************************************************************
815 * _flushall (MSVCRT.@)
817 int CDECL
_flushall(void)
819 return flush_all_buffers(_IOWRT
| _IOREAD
);
822 /*********************************************************************
825 int CDECL
fflush(FILE* file
)
828 flush_all_buffers(_IOWRT
);
829 } else if(file
->_flag
& _IOWRT
) {
833 res
= flush_buffer(file
);
835 if(!res && (file->_flag & _IOCOMMIT))
836 res = _commit(file->_file) ? EOF : 0;
841 } else if(file
->_flag
& _IOREAD
) {
844 file
->_ptr
= file
->_base
;
852 /*********************************************************************
855 int CDECL
_close(int fd
)
862 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
863 if (hand
== INVALID_HANDLE_VALUE
)
865 else if (!CloseHandle(hand
))
867 WARN(":failed-last error (%d)\n",GetLastError());
868 _dosmaperr(GetLastError());
881 /*********************************************************************
884 int CDECL
_commit(int fd
)
886 HANDLE hand
= fdtoh(fd
);
888 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
889 if (hand
== INVALID_HANDLE_VALUE
)
892 if (!FlushFileBuffers(hand
))
894 if (GetLastError() == ERROR_INVALID_HANDLE
)
896 /* FlushFileBuffers fails for console handles
897 * so we ignore this error.
901 TRACE(":failed-last error (%d)\n",GetLastError());
902 _dosmaperr(GetLastError());
909 /*********************************************************************
912 * MSDN isn't clear on this point, but the remarks for _pipe
913 * indicate file descriptors duplicated with _dup and _dup2 are always
916 int CDECL
_dup2(int od
, int nd
)
920 TRACE("(od=%d, nd=%d)\n", od
, nd
);
922 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
926 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
927 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
929 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
933 ret
= set_fd(handle
, wxflag
, nd
);
941 /* _dup2 returns 0, not nd, on success */
948 _dosmaperr(GetLastError());
960 /*********************************************************************
963 int CDECL
_dup(int od
)
969 if (_dup2(od
, fd
) == 0)
977 /*********************************************************************
980 int CDECL
_eof(int fd
)
983 LONG hcurpos
,hendpos
;
984 HANDLE hand
= fdtoh(fd
);
986 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
988 if (hand
== INVALID_HANDLE_VALUE
)
991 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
993 /* Otherwise we do it the hard way */
994 hcurpos
= hendpos
= 0;
995 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
996 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
998 if (curpos
== endpos
&& hcurpos
== hendpos
)
1000 /* FIXME: shouldn't WX_ATEOF be set here? */
1004 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1008 /*********************************************************************
1009 * _fcloseall (MSVCRT.@)
1011 int CDECL
_fcloseall(void)
1013 int num_closed
= 0, i
;
1017 for (i
= 3; i
< stream_idx
; i
++) {
1020 if (file
->_flag
&& !fclose(file
))
1025 TRACE(":closed (%d) handles\n",num_closed
);
1029 /* free everything on process exit */
1030 void msvcrt_free_io(void)
1038 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1041 for(j
=0; j
<stream_idx
; j
++)
1043 FILE *file
= get_file(j
);
1044 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1046 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1047 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1051 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
1054 DeleteCriticalSection(&file_cs
);
1057 /*********************************************************************
1058 * _lseeki64 (MSVCRT.@)
1060 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1062 HANDLE hand
= fdtoh(fd
);
1065 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1066 if (hand
== INVALID_HANDLE_VALUE
)
1069 if (whence
< 0 || whence
> 2)
1075 TRACE(":fd (%d) to %s pos %s\n",
1076 fd
,wine_dbgstr_longlong(offset
),
1077 (whence
==SEEK_SET
)?"SEEK_SET":
1078 (whence
==SEEK_CUR
)?"SEEK_CUR":
1079 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1081 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1082 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1083 ofs
.QuadPart
= offset
;
1084 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1085 GetLastError() == ERROR_SUCCESS
)
1087 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1088 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1090 return ofs
.QuadPart
;
1092 TRACE(":error-last error (%d)\n",GetLastError());
1093 _dosmaperr(GetLastError());
1097 /*********************************************************************
1100 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1102 return (LONG
)_lseeki64(fd
, offset
, whence
);
1105 /*********************************************************************
1106 * _lock_file (MSVCRT.@)
1108 void CDECL
_lock_file(FILE *file
)
1110 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1111 _lock(_STREAM_LOCKS
+(file
-_iob
));
1112 /* ReactOS: string streams dont need to be locked */
1113 else if(!(file
->_flag
& _IOSTRG
))
1114 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1117 /*********************************************************************
1118 * _unlock_file (MSVCRT.@)
1120 void CDECL
_unlock_file(FILE *file
)
1122 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1123 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1124 /* ReactOS: string streams dont need to be locked */
1125 else if(!(file
->_flag
& _IOSTRG
))
1126 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1130 /*********************************************************************
1131 * _locking (MSVCRT.@)
1133 * This is untested; the underlying LockFile doesn't work yet.
1135 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1139 HANDLE hand
= fdtoh(fd
);
1141 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1142 if (hand
== INVALID_HANDLE_VALUE
)
1145 if (mode
< 0 || mode
> 4)
1151 TRACE(":fd (%d) by 0x%08x mode %s\n",
1152 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1153 (mode
==_LK_LOCK
)?"_LK_LOCK":
1154 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1155 (mode
==_LK_RLCK
)?"_LK_RLCK":
1156 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1159 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1161 FIXME ("Seek failed\n");
1162 *_errno() = EINVAL
; /* FIXME */
1165 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1168 ret
= 1; /* just to satisfy gcc */
1171 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1176 else if (mode
== _LK_UNLCK
)
1177 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1179 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1180 /* FIXME - what about error settings? */
1181 return ret
? 0 : -1;
1184 /*********************************************************************
1185 * _fseeki64 (MSVCRT.@)
1187 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1192 /* Flush output if needed */
1193 if(file
->_flag
& _IOWRT
)
1196 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1198 offset
+= _ftelli64(file
);
1201 /* Discard buffered input */
1203 file
->_ptr
= file
->_base
;
1204 /* Reset direction of i/o */
1205 if(file
->_flag
& _IORW
) {
1206 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1208 /* Clear end of file flag */
1209 file
->_flag
&= ~_IOEOF
;
1210 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1216 /*********************************************************************
1219 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1221 return _fseeki64( file
, offset
, whence
);
1224 /*********************************************************************
1225 * _chsize_s (MSVCRT.@)
1227 int CDECL
_chsize_s(int fd
, __int64 size
)
1233 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1235 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1240 if (handle
!= INVALID_HANDLE_VALUE
)
1242 /* save the current file pointer */
1243 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1246 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1249 ret
= SetEndOfFile(handle
);
1250 if (!ret
) _dosmaperr(GetLastError());
1253 /* restore the file pointer */
1254 _lseeki64(fd
, cur
, SEEK_SET
);
1259 return ret
? 0 : *_errno();
1262 /*********************************************************************
1263 * _chsize (MSVCRT.@)
1265 int CDECL
_chsize(int fd
, long size
)
1267 /* _chsize_s returns errno on failure but _chsize should return -1 */
1268 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1271 /*********************************************************************
1272 * clearerr (MSVCRT.@)
1274 void CDECL
clearerr(FILE* file
)
1276 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1279 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1283 /*********************************************************************
1286 void CDECL
rewind(FILE* file
)
1288 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1291 fseek(file
, 0L, SEEK_SET
);
1296 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1298 int plus
= strchrW(mode
, '+') != NULL
;
1300 TRACE("%s\n", debugstr_w(mode
));
1302 while(*mode
== ' ') mode
++;
1307 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1308 *stream_flags
= plus
? _IORW
: _IOREAD
;
1311 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1312 *stream_flags
= plus
? _IORW
: _IOWRT
;
1315 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1316 *stream_flags
= plus
? _IORW
: _IOWRT
;
1319 MSVCRT_INVALID_PMT(0, EINVAL
);
1323 *stream_flags
|= _commode
;
1325 while (*mode
&& *mode
!=',')
1329 *open_flags
|= _O_BINARY
;
1330 *open_flags
&= ~_O_TEXT
;
1333 *open_flags
|= _O_TEXT
;
1334 *open_flags
&= ~_O_BINARY
;
1337 *open_flags
|= _O_TEMPORARY
;
1340 *open_flags
|= _O_SHORT_LIVED
;
1343 *stream_flags
|= _IOCOMMIT
;
1346 *stream_flags
&= ~_IOCOMMIT
;
1349 *open_flags
|= _O_NOINHERIT
;
1358 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1361 ERR("incorrect mode flag: %c\n", mode
[-1]);
1367 static const WCHAR ccs
[] = {'c','c','s'};
1368 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1369 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1370 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1373 while(*mode
== ' ') mode
++;
1374 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1376 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1377 while(*mode
== ' ') mode
++;
1378 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1381 while(*mode
== ' ') mode
++;
1383 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1385 *open_flags
|= _O_U8TEXT
;
1386 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1388 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1390 *open_flags
|= _O_U16TEXT
;
1391 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1393 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1395 *open_flags
|= _O_WTEXT
;
1396 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1400 MSVCRT_INVALID_PMT(0, EINVAL
);
1404 while(*mode
== ' ') mode
++;
1407 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1412 /*********************************************************************
1413 * _fdopen (MSVCRT.@)
1415 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1418 wchar_t *modeW
= NULL
;
1420 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1422 ret
= _wfdopen(fd
, modeW
);
1428 /*********************************************************************
1429 * _wfdopen (MSVCRT.@)
1431 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1433 int open_flags
, stream_flags
;
1436 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1439 if (!(file
= alloc_fp()))
1441 else if (init_fp(file
, fd
, stream_flags
) == -1)
1446 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1452 /*********************************************************************
1453 * _filelength (MSVCRT.@)
1455 LONG CDECL
_filelength(int fd
)
1457 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1460 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1463 if (endPos
!= curPos
)
1464 _lseek(fd
, curPos
, SEEK_SET
);
1471 /*********************************************************************
1472 * _filelengthi64 (MSVCRT.@)
1474 __int64 CDECL
_filelengthi64(int fd
)
1476 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1479 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1482 if (endPos
!= curPos
)
1483 _lseeki64(fd
, curPos
, SEEK_SET
);
1490 /*********************************************************************
1491 * _fileno (MSVCRT.@)
1493 int CDECL
_fileno(FILE* file
)
1495 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1499 /*********************************************************************
1500 * _get_osfhandle (MSVCRT.@)
1502 intptr_t CDECL
_get_osfhandle(int fd
)
1504 HANDLE hand
= fdtoh(fd
);
1505 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1507 return (intptr_t)hand
;
1510 /*********************************************************************
1511 * _mktemp (MSVCRT.@)
1513 char * CDECL
_mktemp(char *pattern
)
1516 char *retVal
= pattern
;
1524 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1528 id
= GetCurrentProcessId();
1532 int tempNum
= id
/ 10;
1533 *pattern
-- = id
- (tempNum
* 10) + '0';
1539 *pattern
= letter
++;
1540 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1542 } while(letter
<= 'z');
1546 /*********************************************************************
1547 * _wmktemp (MSVCRT.@)
1549 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1552 wchar_t *retVal
= pattern
;
1554 wchar_t letter
= 'a';
1557 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1561 id
= GetCurrentProcessId();
1565 int tempNum
= id
/ 10;
1566 *pattern
-- = id
- (tempNum
* 10) + '0';
1572 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1573 GetLastError() == ERROR_FILE_NOT_FOUND
)
1575 *pattern
= letter
++;
1576 } while(letter
!= '|');
1580 /*static*/ unsigned split_oflags(unsigned oflags
)
1583 unsigned unsupp
; /* until we support everything */
1585 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1586 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1587 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1588 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1589 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1590 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1591 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1592 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1593 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1595 if ((unsupp
= oflags
& ~(
1596 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1597 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1598 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1600 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1601 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1603 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1608 /*********************************************************************
1611 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1614 SECURITY_ATTRIBUTES sa
;
1615 HANDLE readHandle
, writeHandle
;
1623 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1624 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1625 sa
.lpSecurityDescriptor
= NULL
;
1626 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1628 unsigned int wxflags
= split_oflags(textmode
);
1632 fd
= alloc_fd(readHandle
, wxflags
);
1636 fd
= alloc_fd(writeHandle
, wxflags
);
1645 CloseHandle(writeHandle
);
1651 CloseHandle(readHandle
);
1652 CloseHandle(writeHandle
);
1658 _dosmaperr(GetLastError());
1663 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1665 char bom
[sizeof(utf8_bom
)];
1668 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1670 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1673 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1674 oflags
|= _O_U8TEXT
;
1675 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1677 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1678 oflags
|= _O_U16TEXT
;
1680 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1686 /*********************************************************************
1687 * _wsopen_s (MSVCRT.@)
1689 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1691 DWORD access
= 0, creation
= 0, attrib
;
1692 SECURITY_ATTRIBUTES sa
;
1697 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1698 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1700 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1703 wxflag
= split_oflags(oflags
);
1704 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1706 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1707 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1708 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1711 if (oflags
& _O_CREAT
)
1713 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1714 FIXME(": pmode 0x%04x ignored\n", pmode
);
1716 WARN(": pmode 0x%04x ignored\n", pmode
);
1718 if (oflags
& _O_EXCL
)
1719 creation
= CREATE_NEW
;
1720 else if (oflags
& _O_TRUNC
)
1721 creation
= CREATE_ALWAYS
;
1723 creation
= OPEN_ALWAYS
;
1725 else /* no _O_CREAT */
1727 if (oflags
& _O_TRUNC
)
1728 creation
= TRUNCATE_EXISTING
;
1730 creation
= OPEN_EXISTING
;
1739 sharing
= FILE_SHARE_READ
;
1742 sharing
= FILE_SHARE_WRITE
;
1745 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1748 ERR( "Unhandled shflags 0x%x\n", shflags
);
1751 attrib
= FILE_ATTRIBUTE_NORMAL
;
1753 if (oflags
& _O_TEMPORARY
)
1755 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1757 sharing
|= FILE_SHARE_DELETE
;
1760 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1761 sa
.lpSecurityDescriptor
= NULL
;
1762 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1764 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1765 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1766 && !(access
&GENERIC_READ
))
1768 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1769 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1770 if (hand
!= INVALID_HANDLE_VALUE
)
1772 oflags
= check_bom(hand
, oflags
, FALSE
);
1776 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1779 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1780 if (hand
== INVALID_HANDLE_VALUE
) {
1781 WARN(":failed-last error (%d)\n",GetLastError());
1782 _dosmaperr(GetLastError());
1786 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1788 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1789 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1790 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1792 if (oflags
& _O_U8TEXT
)
1794 DWORD written
= 0, tmp
;
1796 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1797 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1799 if (written
!= sizeof(utf8_bom
)) {
1800 WARN("error writing BOM\n");
1802 _dosmaperr(GetLastError());
1808 DWORD written
= 0, tmp
;
1810 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1811 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1813 if (written
!= sizeof(utf16_bom
))
1815 WARN("error writing BOM\n");
1817 _dosmaperr(GetLastError());
1822 else if (access
& GENERIC_READ
)
1823 oflags
= check_bom(hand
, oflags
, TRUE
);
1826 *fd
= alloc_fd(hand
, wxflag
);
1830 if (oflags
& _O_WTEXT
)
1831 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1832 else if (oflags
& _O_U16TEXT
)
1833 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1834 else if (oflags
& _O_U8TEXT
)
1835 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1837 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1841 /*********************************************************************
1842 * _wsopen (MSVCRT.@)
1844 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1849 if (oflags
& _O_CREAT
)
1853 __ms_va_start(ap
, shflags
);
1854 pmode
= va_arg(ap
, int);
1860 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1864 /*********************************************************************
1865 * _sopen_s (MSVCRT.@)
1867 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1872 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1875 ret
= _wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1880 /*********************************************************************
1883 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1888 if (oflags
& _O_CREAT
)
1892 va_start(ap
, shflags
);
1893 pmode
= va_arg(ap
, int);
1899 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1903 /*********************************************************************
1906 int CDECL
_open( const char *path
, int flags
, ... )
1910 if (flags
& _O_CREAT
)
1913 va_start(ap
, flags
);
1914 pmode
= va_arg(ap
, int);
1916 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1919 return _sopen( path
, flags
, _SH_DENYNO
);
1922 /*********************************************************************
1925 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1929 if (flags
& _O_CREAT
)
1932 va_start(ap
, flags
);
1933 pmode
= va_arg(ap
, int);
1935 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1938 return _wsopen( path
, flags
, _SH_DENYNO
);
1941 /*********************************************************************
1944 int CDECL
_creat(const char *path
, int flags
)
1946 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1947 return _open(path
, usedFlags
);
1950 /*********************************************************************
1951 * _wcreat (MSVCRT.@)
1953 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1955 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1956 return _wopen(path
, usedFlags
);
1959 /*********************************************************************
1960 * _open_osfhandle (MSVCRT.@)
1962 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1967 /* _O_RDONLY (0) always matches, so set the read flag
1968 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1969 * file, so set the write flag. It also only sets _O_TEXT if it wants
1970 * text - it never sets _O_BINARY.
1972 /* don't let split_oflags() decide the mode if no mode is passed */
1973 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1974 oflags
|= _O_BINARY
;
1976 flags
= GetFileType((HANDLE
)handle
);
1977 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
1979 _dosmaperr(GetLastError());
1983 if (flags
== FILE_TYPE_CHAR
)
1985 else if (flags
== FILE_TYPE_PIPE
)
1989 flags
|= split_oflags(oflags
);
1991 fd
= alloc_fd((HANDLE
)handle
, flags
);
1992 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
1996 /*********************************************************************
1999 int CDECL
_rmtmp(void)
2001 int num_removed
= 0, i
;
2005 for (i
= 3; i
< stream_idx
; i
++) {
2008 if (file
->_tmpfname
)
2017 TRACE(":removed (%d) temp files\n",num_removed
);
2021 static inline int get_utf8_char_len(char ch
)
2023 if((ch
&0xf8) == 0xf0)
2025 else if((ch
&0xf0) == 0xe0)
2027 else if((ch
&0xe0) == 0xc0)
2032 /*********************************************************************
2033 * (internal) read_utf8
2035 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2037 ioinfo
*fdinfo
= get_ioinfo(fd
);
2038 HANDLE hand
= fdinfo
->handle
;
2039 char min_buf
[4], *readbuf
, lookahead
;
2040 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2042 /* make the buffer big enough to hold at least one character */
2043 /* read bytes have to fit to output and lookahead buffers */
2045 readbuf_size
= count
< 4 ? 4 : count
;
2046 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2051 if(fdinfo
->lookahead
[0] != '\n') {
2052 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2053 fdinfo
->lookahead
[0] = '\n';
2055 if(fdinfo
->lookahead
[1] != '\n') {
2056 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2057 fdinfo
->lookahead
[1] = '\n';
2059 if(fdinfo
->lookahead
[2] != '\n') {
2060 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2061 fdinfo
->lookahead
[2] = '\n';
2066 /* NOTE: this case is broken in native dll, reading
2067 * sometimes fails when small buffer is passed
2070 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2071 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2072 fdinfo
->wxflag
|= WX_ATEOF
;
2075 _dosmaperr(GetLastError());
2078 }else if(!num_read
) {
2079 fdinfo
->wxflag
|= WX_ATEOF
;
2085 char_len
= get_utf8_char_len(readbuf
[0]);
2087 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2091 if(readbuf
[0] == '\n')
2092 fdinfo
->wxflag
|= WX_READNL
;
2094 fdinfo
->wxflag
&= ~WX_READNL
;
2096 if(readbuf
[0] == 0x1a) {
2097 fdinfo
->wxflag
|= WX_ATEOF
;
2101 if(readbuf
[0] == '\r') {
2102 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2104 else if(lookahead
== '\n')
2108 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2109 fdinfo
->lookahead
[0] = lookahead
;
2111 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2116 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2117 _dosmaperr(GetLastError());
2124 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2127 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2128 fdinfo
->wxflag
|= WX_ATEOF
;
2129 if (readbuf
!= min_buf
) free(readbuf
);
2132 _dosmaperr(GetLastError());
2133 if (readbuf
!= min_buf
) free(readbuf
);
2136 }else if(!pos
&& !num_read
) {
2137 fdinfo
->wxflag
|= WX_ATEOF
;
2138 if (readbuf
!= min_buf
) free(readbuf
);
2143 if(readbuf
[0] == '\n')
2144 fdinfo
->wxflag
|= WX_READNL
;
2146 fdinfo
->wxflag
&= ~WX_READNL
;
2148 /* Find first byte of last character (may be incomplete) */
2149 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2150 if((readbuf
[i
]&0xc0) != 0x80)
2152 char_len
= get_utf8_char_len(readbuf
[i
]);
2153 if(char_len
+i
<= pos
)
2156 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
)) {
2158 fdinfo
->lookahead
[0] = readbuf
[i
];
2160 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2162 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2164 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2168 for(i
=0, j
=0; i
<pos
; i
++) {
2169 if(readbuf
[i
] == 0x1a) {
2170 fdinfo
->wxflag
|= WX_ATEOF
;
2174 /* strip '\r' if followed by '\n' */
2175 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2176 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2177 readbuf
[j
++] = '\r';
2178 }else if(lookahead
== '\n' && j
==0) {
2179 readbuf
[j
++] = '\n';
2181 if(lookahead
!= '\n')
2182 readbuf
[j
++] = '\r';
2184 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2185 fdinfo
->lookahead
[0] = lookahead
;
2187 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2189 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2190 readbuf
[j
++] = readbuf
[i
];
2195 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2196 _dosmaperr(GetLastError());
2197 if (readbuf
!= min_buf
) free(readbuf
);
2201 if (readbuf
!= min_buf
) free(readbuf
);
2205 /*********************************************************************
2208 * When reading \r as last character in text mode, read() positions
2209 * the file pointer on the \r character while getc() goes on to
2212 static int read_i(int fd
, void *buf
, unsigned int count
)
2214 DWORD num_read
, utf16
;
2215 char *bufstart
= buf
;
2216 HANDLE hand
= fdtoh(fd
);
2217 ioinfo
*fdinfo
= get_ioinfo(fd
);
2222 if (fdinfo
->wxflag
& WX_ATEOF
) {
2223 TRACE("already at EOF, returning 0\n");
2226 /* Don't trace small reads, it gets *very* annoying */
2228 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2229 if (hand
== INVALID_HANDLE_VALUE
)
2235 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2236 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2242 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2243 return read_utf8(fd
, buf
, count
);
2245 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2247 if (fdinfo
->lookahead
[0] != '\n')
2249 bufstart
[0] = fdinfo
->lookahead
[0];
2250 fdinfo
->lookahead
[0] = '\n';
2254 bufstart
[1] = fdinfo
->lookahead
[1];
2255 fdinfo
->lookahead
[1] = '\n';
2258 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2259 num_read
+= 1+utf16
;
2264 if(utf16
&& (num_read
&1))
2266 /* msvcr90 uses uninitialized value from the buffer in this case */
2267 /* msvcrt ignores additional data */
2268 ERR("got odd number of bytes in UTF16 mode\n");
2272 if (count
!= 0 && num_read
== 0)
2274 fdinfo
->wxflag
|= WX_ATEOF
;
2275 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2277 else if (fdinfo
->wxflag
& WX_TEXT
)
2281 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2282 fdinfo
->wxflag
|= WX_READNL
;
2284 fdinfo
->wxflag
&= ~WX_READNL
;
2286 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2288 /* in text mode, a ctrl-z signals EOF */
2289 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2291 fdinfo
->wxflag
|= WX_ATEOF
;
2292 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2296 /* in text mode, strip \r if followed by \n */
2297 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2302 lookahead
[1] = '\n';
2303 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2305 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2307 bufstart
[j
++] = '\n';
2308 if(utf16
) bufstart
[j
++] = 0;
2312 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2314 bufstart
[j
++] = '\r';
2315 if(utf16
) bufstart
[j
++] = 0;
2318 if (fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2320 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2322 bufstart
[j
++] = '\n';
2323 if (utf16
) bufstart
[j
++] = 0;
2327 fdinfo
->lookahead
[0] = lookahead
[0];
2328 fdinfo
->lookahead
[1] = lookahead
[1];
2332 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2337 bufstart
[j
++] = '\r';
2338 if(utf16
) bufstart
[j
++] = 0;
2341 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2342 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2344 bufstart
[j
++] = bufstart
[i
];
2345 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2353 if (GetLastError() == ERROR_BROKEN_PIPE
)
2355 TRACE(":end-of-pipe\n");
2356 fdinfo
->wxflag
|= WX_ATEOF
;
2361 TRACE(":failed-last error (%d)\n",GetLastError());
2367 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2371 /*********************************************************************
2374 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2377 num_read
= read_i(fd
, buf
, count
);
2381 /*********************************************************************
2382 * _setmode (MSVCRT.@)
2384 int CDECL
_setmode(int fd
,int mode
)
2386 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2387 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2390 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2391 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2396 if(mode
== _O_BINARY
) {
2397 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2398 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2402 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2404 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2405 else if(mode
== _O_U8TEXT
)
2406 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2408 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2414 /*********************************************************************
2417 long CDECL
_tell(int fd
)
2419 return _lseek(fd
, 0, SEEK_CUR
);
2422 /*********************************************************************
2423 * _telli64 (MSVCRT.@)
2425 __int64 CDECL
_telli64(int fd
)
2427 return _lseeki64(fd
, 0, SEEK_CUR
);
2430 /*********************************************************************
2431 * _tempnam (MSVCRT.@)
2433 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2435 char tmpbuf
[MAX_PATH
];
2436 const char *tmp_dir
= getenv("TMP");
2438 if (tmp_dir
) dir
= tmp_dir
;
2440 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2441 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2443 TRACE("got name (%s)\n",tmpbuf
);
2444 DeleteFileA(tmpbuf
);
2445 return _strdup(tmpbuf
);
2447 TRACE("failed (%d)\n",GetLastError());
2451 /*********************************************************************
2452 * _wtempnam (MSVCRT.@)
2454 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2456 wchar_t tmpbuf
[MAX_PATH
];
2458 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2459 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2461 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2462 DeleteFileW(tmpbuf
);
2463 return _wcsdup(tmpbuf
);
2465 TRACE("failed (%d)\n",GetLastError());
2469 /*********************************************************************
2472 int CDECL
_umask(int umask
)
2474 int old_umask
= umask
;
2475 TRACE("(%d)\n",umask
);
2476 MSVCRT_umask
= umask
;
2480 /*********************************************************************
2483 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2486 ioinfo
*info
= get_ioinfo(fd
);
2487 HANDLE hand
= info
->handle
;
2489 /* Don't trace small writes, it gets *very* annoying */
2492 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2494 if (hand
== INVALID_HANDLE_VALUE
)
2500 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2506 /* If appending, go to EOF */
2507 if (info
->wxflag
& WX_APPEND
)
2508 _lseek(fd
, 0, FILE_END
);
2510 if (!(info
->wxflag
& WX_TEXT
))
2512 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2513 && (num_written
== count
))
2515 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2516 hand
, GetLastError());
2521 unsigned int i
, j
, nr_lf
, size
;
2524 const char *s
= buf
, *buf_start
= buf
;
2526 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2528 /* find number of \n */
2529 for (nr_lf
=0, i
=0; i
<count
; i
++)
2535 if ((q
= p
= malloc(size
)))
2537 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2546 FIXME("Malloc failed\n");
2558 else if (info
->exflag
& EF_UTF16
)
2560 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2561 if (s
[i
]=='\n' && s
[i
+1]==0)
2566 if ((q
= p
= malloc(size
)))
2568 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2570 if (s
[i
]=='\n' && s
[i
+1]==0)
2581 FIXME("Malloc failed\n");
2597 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2598 if (s
[i
]=='\n' && s
[i
+1]==0)
2601 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2603 _dosmaperr(GetLastError());
2608 size
= conv_len
+nr_lf
;
2609 if((p
= malloc(count
+nr_lf
*2+size
)))
2611 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2613 if (s
[i
]=='\n' && s
[i
+1]==0)
2621 q
= p
+count
+nr_lf
*2;
2622 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2623 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2627 FIXME("Malloc failed\n");
2634 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2638 if (num_written
!= size
)
2640 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2641 fd
, hand
, GetLastError(), num_written
);
2643 return s
- buf_start
;
2651 /*********************************************************************
2654 int CDECL
_putw(int val
, FILE* file
)
2659 len
= _write(file
->_file
, &val
, sizeof(val
));
2660 if (len
== sizeof(val
)) {
2665 file
->_flag
|= _IOERR
;
2670 /*********************************************************************
2673 int CDECL
fclose(FILE* file
)
2679 free(file
->_tmpfname
);
2680 file
->_tmpfname
= NULL
;
2681 /* flush stdio buffers */
2682 if(file
->_flag
& _IOWRT
)
2684 if(file
->_flag
& _IOMYBUF
)
2687 r
=_close(file
->_file
);
2691 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2692 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2694 if(file
== get_file(stream_idx
-1)) {
2695 while(stream_idx
>3 && !file
->_flag
) {
2697 file
= get_file(stream_idx
-1);
2701 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2704 /*********************************************************************
2707 int CDECL
feof(FILE* file
)
2709 return file
->_flag
& _IOEOF
;
2712 /*********************************************************************
2715 int CDECL
ferror(FILE* file
)
2717 return file
->_flag
& _IOERR
;
2720 /*********************************************************************
2721 * _filbuf (MSVCRT.@)
2723 int CDECL
_filbuf(FILE* file
)
2728 if(file
->_flag
& _IOSTRG
) {
2733 /* Allocate buffer if needed */
2734 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
2737 if(!(file
->_flag
& _IOREAD
)) {
2738 if(file
->_flag
& _IORW
)
2739 file
->_flag
|= _IOREAD
;
2746 if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
2748 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2749 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2757 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2759 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2766 file
->_ptr
= file
->_base
+1;
2767 c
= *(unsigned char *)file
->_base
;
2773 /*********************************************************************
2776 int CDECL
fgetc(FILE* file
)
2784 i
= (unsigned char *)file
->_ptr
++;
2793 /*********************************************************************
2794 * _fgetchar (MSVCRT.@)
2796 int CDECL
_fgetchar(void)
2798 return fgetc(stdin
);
2801 /*********************************************************************
2804 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2807 char * buf_start
= s
;
2809 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2810 file
,file
->_file
,s
,size
);
2814 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2819 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2821 TRACE(":nothing read\n");
2825 if ((cc
!= EOF
) && (size
> 1))
2828 TRACE(":got %s\n", debugstr_a(buf_start
));
2833 /*********************************************************************
2836 wint_t CDECL
fgetwc(FILE* file
)
2843 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2844 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2847 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2856 char mbs
[MB_LEN_MAX
];
2862 if(isleadbyte((unsigned char)mbs
[0])) {
2873 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2881 /*********************************************************************
2884 int CDECL
_getw(FILE* file
)
2892 for (j
=0; j
<sizeof(int); j
++) {
2895 file
->_flag
|= _IOEOF
;
2906 /*********************************************************************
2909 wint_t CDECL
getwc(FILE* file
)
2911 return fgetwc(file
);
2914 /*********************************************************************
2915 * _fgetwchar (MSVCRT.@)
2917 wint_t CDECL
_fgetwchar(void)
2919 return fgetwc(stdin
);
2922 /*********************************************************************
2923 * getwchar (MSVCRT.@)
2925 wint_t CDECL
getwchar(void)
2927 return _fgetwchar();
2930 /*********************************************************************
2933 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2936 wchar_t * buf_start
= s
;
2938 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2939 file
,file
->_file
,s
,size
);
2943 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2948 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2950 TRACE(":nothing read\n");
2954 if ((cc
!= WEOF
) && (size
> 1))
2957 TRACE(":got %s\n", debugstr_w(buf_start
));
2962 /*********************************************************************
2965 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2967 size_t wrcnt
=size
* nmemb
;
2976 if(file
->_cnt
< 0) {
2977 WARN("negative file->_cnt value in %p\n", file
);
2978 file
->_flag
|= MSVCRT__IOERR
;
2983 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2984 memcpy(file
->_ptr
, ptr
, pcnt
);
2989 ptr
= (const char*)ptr
+ pcnt
;
2990 } else if((file
->_flag
& _IONBF
)
2991 || ((file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= file
->_bufsiz
)
2992 || (!(file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
2996 if(file
->_flag
& _IONBF
)
2998 else if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
)))
2999 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3001 bufsiz
= file
->_bufsiz
;
3003 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3005 if(flush_buffer(file
) == EOF
)
3008 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
3009 file
->_flag
|= _IOERR
;
3014 ptr
= (const char*)ptr
+ pcnt
;
3016 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3020 ptr
= (const char*)ptr
+ 1;
3025 return written
/ size
;
3028 /*********************************************************************
3030 * FORKED for ReactOS, don't sync with Wine!
3032 * - http://jira.reactos.org/browse/CORE-6495
3033 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3035 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3037 /* If this is a real file stream (and not some temporary one for
3038 sprintf-like functions), check whether it is opened in text mode.
3039 In this case, we have to perform an implicit conversion to ANSI. */
3040 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
3042 /* Convert to multibyte in text mode */
3043 char mbc
[MB_LEN_MAX
];
3046 mb_return
= wctomb(mbc
, c
);
3051 /* Output all characters */
3052 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3057 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3064 /*********************************************************************
3065 * _fputwchar (MSVCRT.@)
3067 wint_t CDECL
_fputwchar(wint_t wc
)
3069 return fputwc(wc
, stdout
);
3072 /*********************************************************************
3073 * _wfsopen (MSVCRT.@)
3075 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3078 int open_flags
, stream_flags
, fd
;
3080 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3082 /* map mode string to open() flags. "man fopen" for possibilities. */
3083 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3087 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3090 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
3092 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3099 TRACE(":got (%p)\n",file
);
3100 if (fd
>= 0 && !file
)
3106 /*********************************************************************
3107 * _fsopen (MSVCRT.@)
3109 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3112 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3114 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3115 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3119 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3122 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3127 ret
= _wfsopen(pathW
, modeW
, share
);
3134 /*********************************************************************
3137 FILE * CDECL
fopen(const char *path
, const char *mode
)
3139 return _fsopen( path
, mode
, _SH_DENYNO
);
3142 /*********************************************************************
3143 * fopen_s (MSVCRT.@)
3145 int CDECL
fopen_s(FILE** pFile
,
3146 const char *filename
, const char *mode
)
3148 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
3149 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
3150 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
3152 *pFile
= fopen(filename
, mode
);
3159 /*********************************************************************
3160 * _wfopen (MSVCRT.@)
3162 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3164 return _wfsopen( path
, mode
, _SH_DENYNO
);
3167 /*********************************************************************
3168 * _wfopen_s (MSVCRT.@)
3170 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3171 const wchar_t *mode
)
3173 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3174 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3179 *pFile
= _wfopen(filename
, mode
);
3186 /* fputc calls _flsbuf which calls fputc */
3187 int CDECL
_flsbuf(int c
, FILE* file
);
3189 /*********************************************************************
3192 int CDECL
fputc(int c
, FILE* file
)
3202 res
= flush_buffer(file
);
3204 return res
? res
: c
;
3211 res
= _flsbuf(c
, file
);
3217 /*********************************************************************
3218 * _fputchar (MSVCRT.@)
3220 int CDECL
_fputchar(int c
)
3222 return fputc(c
, stdout
);
3225 /*********************************************************************
3228 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3230 size_t rcnt
=size
* nmemb
;
3239 /* first buffered data */
3241 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3242 memcpy(ptr
, file
->_ptr
, pcnt
);
3247 ptr
= (char*)ptr
+ pcnt
;
3248 } else if(!(file
->_flag
& _IOREAD
)) {
3249 if(file
->_flag
& _IORW
) {
3250 file
->_flag
|= _IOREAD
;
3257 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
3263 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& (file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
3264 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3265 file
->_ptr
= file
->_base
;
3266 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3267 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3268 if (i
> 0 && i
< file
->_cnt
) {
3269 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3270 file
->_flag
&= ~_IOEOF
;
3273 memcpy(ptr
, file
->_ptr
, i
);
3277 } else if (rcnt
> INT_MAX
) {
3278 i
= _read(file
->_file
, ptr
, INT_MAX
);
3279 } else if (rcnt
< BUFSIZ
) {
3280 i
= _read(file
->_file
, ptr
, rcnt
);
3282 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3286 ptr
= (char *)ptr
+i
;
3287 /* expose feof condition in the flags
3288 * MFC tests file->_flag for feof, and doesn't call feof())
3290 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3291 file
->_flag
|= _IOEOF
;
3294 file
->_flag
|= _IOERR
;
3305 /*********************************************************************
3306 * _wfreopen (MSVCRT.@)
3309 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3311 int open_flags
, stream_flags
, fd
;
3313 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3316 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3321 /* map mode string to open() flags. "man fopen" for possibilities. */
3322 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3326 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3329 else if (init_fp(file
, fd
, stream_flags
) == -1)
3332 WARN(":failed-last error (%d)\n",GetLastError());
3333 _dosmaperr(GetLastError());
3342 /*********************************************************************
3343 * freopen (MSVCRT.@)
3346 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3349 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3351 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3352 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3358 ret
= _wfreopen(pathW
, modeW
, file
);
3365 /*********************************************************************
3366 * fsetpos (MSVCRT.@)
3368 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3373 /* Note that all this has been lifted 'as is' from fseek */
3374 if(file
->_flag
& _IOWRT
)
3377 /* Discard buffered input */
3379 file
->_ptr
= file
->_base
;
3381 /* Reset direction of i/o */
3382 if(file
->_flag
& _IORW
) {
3383 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3386 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3391 /*********************************************************************
3392 * _ftelli64 (MSVCRT.@)
3394 __int64 CDECL
_ftelli64(FILE* file
)
3399 pos
= _telli64(file
->_file
);
3404 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
3405 if(file
->_flag
& _IOWRT
) {
3406 pos
+= file
->_ptr
- file
->_base
;
3408 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3411 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3415 } else if(!file
->_cnt
) { /* nothing to do */
3416 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3420 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3421 for(i
=0; i
<file
->_cnt
; i
++)
3422 if(file
->_ptr
[i
] == '\n')
3428 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3433 pos
-= file
->_bufsiz
;
3434 pos
+= file
->_ptr
- file
->_base
;
3436 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3437 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3440 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3451 /*********************************************************************
3454 LONG CDECL
ftell(FILE* file
)
3456 return (LONG
)_ftelli64(file
);
3459 /*********************************************************************
3460 * fgetpos (MSVCRT.@)
3462 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3464 *pos
= _ftelli64(file
);
3470 /*********************************************************************
3473 int CDECL
fputs(const char *s
, FILE* file
)
3475 size_t len
= strlen(s
);
3479 ret
= fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
3484 /*********************************************************************
3487 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3489 size_t i
, len
= strlenW(s
);
3494 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3495 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3500 tmp_buf
= add_std_buffer(file
);
3501 for (i
=0; i
<len
; i
++) {
3502 if(fputwc(s
[i
], file
) == WEOF
) {
3503 if(tmp_buf
) remove_std_buffer(file
);
3509 if(tmp_buf
) remove_std_buffer(file
);
3514 /*********************************************************************
3515 * getchar (MSVCRT.@)
3517 int CDECL
getchar(void)
3519 return fgetc(stdin
);
3522 /*********************************************************************
3525 int CDECL
getc(FILE* file
)
3530 /*********************************************************************
3533 char * CDECL
gets(char *buf
)
3536 char * buf_start
= buf
;
3539 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3541 if(cc
!= '\r') *buf
++ = (char)cc
;
3545 TRACE("got '%s'\n", buf_start
);
3546 _unlock_file(stdin
);
3550 /*********************************************************************
3553 wchar_t* CDECL
_getws(wchar_t* buf
)
3559 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3563 *buf
++ = (wchar_t)cc
;
3567 TRACE("got %s\n", debugstr_w(ws
));
3568 _unlock_file(stdin
);
3572 /*********************************************************************
3575 int CDECL
putc(int c
, FILE* file
)
3577 return fputc(c
, file
);
3580 /*********************************************************************
3581 * putchar (MSVCRT.@)
3583 int CDECL
putchar(int c
)
3585 return fputc(c
, stdout
);
3588 /*********************************************************************
3589 * _putwch (MSVCRT.@)
3591 wint_t CDECL
_putwch(wchar_t c
)
3593 return fputwc(c
, stdout
);
3596 /*********************************************************************
3599 int CDECL
puts(const char *s
)
3601 size_t len
= strlen(s
);
3605 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3606 _unlock_file(stdout
);
3610 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3611 _unlock_file(stdout
);
3615 /*********************************************************************
3618 int CDECL
_putws(const wchar_t *s
)
3620 static const wchar_t nl
= '\n';
3621 size_t len
= strlenW(s
);
3625 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3626 _unlock_file(stdout
);
3630 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3631 _unlock_file(stdout
);
3635 /*********************************************************************
3638 int CDECL
remove(const char *path
)
3640 TRACE("(%s)\n",path
);
3641 if (DeleteFileA(path
))
3643 TRACE(":failed (%d)\n",GetLastError());
3644 _dosmaperr(GetLastError());
3648 /*********************************************************************
3649 * _wremove (MSVCRT.@)
3651 int CDECL
_wremove(const wchar_t *path
)
3653 TRACE("(%s)\n",debugstr_w(path
));
3654 if (DeleteFileW(path
))
3656 TRACE(":failed (%d)\n",GetLastError());
3657 _dosmaperr(GetLastError());
3661 /*********************************************************************
3664 int CDECL
rename(const char *oldpath
,const char *newpath
)
3666 TRACE(":from %s to %s\n",oldpath
,newpath
);
3667 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3669 TRACE(":failed (%d)\n",GetLastError());
3670 _dosmaperr(GetLastError());
3674 /*********************************************************************
3675 * _wrename (MSVCRT.@)
3677 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3679 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3680 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3682 TRACE(":failed (%d)\n",GetLastError());
3683 _dosmaperr(GetLastError());
3687 /*********************************************************************
3688 * setvbuf (MSVCRT.@)
3690 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3692 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
3693 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
3694 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
3699 if(file
->_flag
& _IOMYBUF
)
3701 file
->_flag
&= ~(_IONBF
| _IOMYBUF
| _USERBUF
);
3704 if(mode
== _IONBF
) {
3705 file
->_flag
|= _IONBF
;
3706 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
3709 file
->_base
= file
->_ptr
= buf
;
3710 file
->_flag
|= _USERBUF
;
3711 file
->_bufsiz
= size
;
3713 file
->_base
= file
->_ptr
= malloc(size
);
3720 file
->_flag
|= _IOMYBUF
;
3721 file
->_bufsiz
= size
;
3727 /*********************************************************************
3730 void CDECL
setbuf(FILE* file
, char *buf
)
3732 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3735 /*********************************************************************
3738 char * CDECL
tmpnam(char *s
)
3745 thread_data_t
*data
= msvcrt_get_thread_data();
3747 if(!data
->tmpnam_buffer
)
3748 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3750 s
= data
->tmpnam_buffer
;
3753 int_to_base32(GetCurrentProcessId(), tmpstr
);
3754 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3755 for (count
= 0; count
< TMP_MAX
; count
++)
3757 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3758 memcpy(p
, tmpstr
, size
);
3760 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3761 GetLastError() == ERROR_FILE_NOT_FOUND
)
3767 /*********************************************************************
3768 * _wtmpnam (MSVCRT.@)
3770 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3772 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3777 thread_data_t
*data
= msvcrt_get_thread_data();
3779 if(!data
->wtmpnam_buffer
)
3780 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3782 s
= data
->wtmpnam_buffer
;
3785 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3786 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3787 for (count
= 0; count
< TMP_MAX
; count
++)
3789 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3790 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3792 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3793 GetLastError() == ERROR_FILE_NOT_FOUND
)
3799 /*********************************************************************
3800 * tmpfile (MSVCRT.@)
3802 FILE* CDECL
tmpfile(void)
3804 char *filename
= tmpnam(NULL
);
3809 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3810 _S_IREAD
| _S_IWRITE
);
3811 if (fd
!= -1 && (file
= alloc_fp()))
3813 if (init_fp(file
, fd
, _IORW
) == -1)
3818 else file
->_tmpfname
= _strdup(filename
);
3821 if(fd
!= -1 && !file
)
3827 /*********************************************************************
3830 int CDECL
ungetc(int c
, FILE * file
)
3832 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3834 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
3835 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
3839 if((!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
3840 && alloc_buffer(file
))
3841 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
3844 if(file
->_ptr
>file
->_base
) {
3846 if(file
->_flag
& _IOSTRG
) {
3847 if(*file
->_ptr
!= c
) {
3857 file
->_flag
|= _IOREAD
;
3866 /*********************************************************************
3867 * ungetwc (MSVCRT.@)
3869 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3878 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3879 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3880 unsigned char * pp
= (unsigned char *)&mwc
;
3883 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3884 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3890 char mbs
[MB_LEN_MAX
];
3893 len
= wctomb(mbs
, mwc
);
3899 for(len
--; len
>=0; len
--) {
3900 if(mbs
[len
] != ungetc(mbs
[len
], file
)) {
3913 /*********************************************************************
3914 * _getmaxstdio (MSVCRT.@)
3916 int CDECL
_getmaxstdio(void)
3921 /*********************************************************************
3922 * _setmaxstdio (MSVCRT.@)
3924 int CDECL
_setmaxstdio(int newmax
)
3926 TRACE("%d\n", newmax
);
3928 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3931 max_streams
= newmax
;