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
) {
1173 offset
-= file
->_cnt
;
1174 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1175 /* Black magic correction for CR removal */
1177 for (i
=0; i
<file
->_cnt
; i
++) {
1178 if (file
->_ptr
[i
] == '\n')
1181 /* Black magic when reading CR at buffer boundary*/
1182 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1186 /* Discard buffered input */
1188 file
->_ptr
= file
->_base
;
1189 /* Reset direction of i/o */
1190 if(file
->_flag
& _IORW
) {
1191 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1193 /* Clear end of file flag */
1194 file
->_flag
&= ~_IOEOF
;
1195 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1201 /*********************************************************************
1204 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1206 return _fseeki64( file
, offset
, whence
);
1209 /*********************************************************************
1210 * _chsize (MSVCRT.@)
1212 int CDECL
_chsize(int fd
, long size
)
1218 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1223 if (handle
!= INVALID_HANDLE_VALUE
)
1225 /* save the current file pointer */
1226 cur
= _lseek(fd
, 0, SEEK_CUR
);
1229 pos
= _lseek(fd
, size
, SEEK_SET
);
1232 ret
= SetEndOfFile(handle
);
1233 if (!ret
) _dosmaperr(GetLastError());
1236 /* restore the file pointer */
1237 _lseek(fd
, cur
, SEEK_SET
);
1242 return ret
? 0 : -1;
1245 /*********************************************************************
1246 * clearerr (MSVCRT.@)
1248 void CDECL
clearerr(FILE* file
)
1250 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1253 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1257 /*********************************************************************
1260 void CDECL
rewind(FILE* file
)
1262 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1265 fseek(file
, 0L, SEEK_SET
);
1270 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1272 int plus
= strchrW(mode
, '+') != NULL
;
1274 TRACE("%s\n", debugstr_w(mode
));
1276 while(*mode
== ' ') mode
++;
1281 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1282 *stream_flags
= plus
? _IORW
: _IOREAD
;
1285 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1286 *stream_flags
= plus
? _IORW
: _IOWRT
;
1289 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1290 *stream_flags
= plus
? _IORW
: _IOWRT
;
1293 MSVCRT_INVALID_PMT(0, EINVAL
);
1297 *stream_flags
|= _commode
;
1299 while (*mode
&& *mode
!=',')
1303 *open_flags
|= _O_BINARY
;
1304 *open_flags
&= ~_O_TEXT
;
1307 *open_flags
|= _O_TEXT
;
1308 *open_flags
&= ~_O_BINARY
;
1311 *open_flags
|= _O_TEMPORARY
;
1314 *open_flags
|= _O_SHORT_LIVED
;
1317 *stream_flags
|= _IOCOMMIT
;
1320 *stream_flags
&= ~_IOCOMMIT
;
1323 *open_flags
|= _O_NOINHERIT
;
1332 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1335 ERR("incorrect mode flag: %c\n", mode
[-1]);
1341 static const WCHAR ccs
[] = {'c','c','s'};
1342 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1343 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1344 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1347 while(*mode
== ' ') mode
++;
1348 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1350 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1351 while(*mode
== ' ') mode
++;
1352 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1355 while(*mode
== ' ') mode
++;
1357 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1359 *open_flags
|= _O_U8TEXT
;
1360 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1362 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1364 *open_flags
|= _O_U16TEXT
;
1365 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1367 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1369 *open_flags
|= _O_WTEXT
;
1370 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1374 MSVCRT_INVALID_PMT(0, EINVAL
);
1378 while(*mode
== ' ') mode
++;
1381 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1386 /*********************************************************************
1387 * _fdopen (MSVCRT.@)
1389 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1392 wchar_t *modeW
= NULL
;
1394 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1396 ret
= _wfdopen(fd
, modeW
);
1402 /*********************************************************************
1403 * _wfdopen (MSVCRT.@)
1405 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1407 int open_flags
, stream_flags
;
1410 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1413 if (!(file
= alloc_fp()))
1415 else if (init_fp(file
, fd
, stream_flags
) == -1)
1420 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1426 /*********************************************************************
1427 * _filelength (MSVCRT.@)
1429 LONG CDECL
_filelength(int fd
)
1431 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1434 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1437 if (endPos
!= curPos
)
1438 _lseek(fd
, curPos
, SEEK_SET
);
1445 /*********************************************************************
1446 * _filelengthi64 (MSVCRT.@)
1448 __int64 CDECL
_filelengthi64(int fd
)
1450 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1453 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1456 if (endPos
!= curPos
)
1457 _lseeki64(fd
, curPos
, SEEK_SET
);
1464 /*********************************************************************
1465 * _fileno (MSVCRT.@)
1467 int CDECL
_fileno(FILE* file
)
1469 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1473 /*********************************************************************
1474 * _get_osfhandle (MSVCRT.@)
1476 intptr_t CDECL
_get_osfhandle(int fd
)
1478 HANDLE hand
= fdtoh(fd
);
1479 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1481 return (intptr_t)hand
;
1484 /*********************************************************************
1485 * _mktemp (MSVCRT.@)
1487 char * CDECL
_mktemp(char *pattern
)
1490 char *retVal
= pattern
;
1498 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1502 id
= GetCurrentProcessId();
1506 int tempNum
= id
/ 10;
1507 *pattern
-- = id
- (tempNum
* 10) + '0';
1513 *pattern
= letter
++;
1514 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1516 } while(letter
<= 'z');
1520 /*********************************************************************
1521 * _wmktemp (MSVCRT.@)
1523 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1526 wchar_t *retVal
= pattern
;
1528 wchar_t letter
= 'a';
1531 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1535 id
= GetCurrentProcessId();
1539 int tempNum
= id
/ 10;
1540 *pattern
-- = id
- (tempNum
* 10) + '0';
1546 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1547 GetLastError() == ERROR_FILE_NOT_FOUND
)
1549 *pattern
= letter
++;
1550 } while(letter
!= '|');
1554 /*static*/ unsigned split_oflags(unsigned oflags
)
1557 unsigned unsupp
; /* until we support everything */
1559 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1560 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1561 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1562 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1563 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1564 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1565 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1566 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1567 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1569 if ((unsupp
= oflags
& ~(
1570 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1571 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1572 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1574 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1575 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1577 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1582 /*********************************************************************
1585 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1588 SECURITY_ATTRIBUTES sa
;
1589 HANDLE readHandle
, writeHandle
;
1597 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1598 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1599 sa
.lpSecurityDescriptor
= NULL
;
1600 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1602 unsigned int wxflags
= split_oflags(textmode
);
1606 fd
= alloc_fd(readHandle
, wxflags
);
1610 fd
= alloc_fd(writeHandle
, wxflags
);
1619 CloseHandle(writeHandle
);
1625 CloseHandle(readHandle
);
1626 CloseHandle(writeHandle
);
1632 _dosmaperr(GetLastError());
1637 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1639 char bom
[sizeof(utf8_bom
)];
1642 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1644 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1647 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1648 oflags
|= _O_U8TEXT
;
1649 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1651 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1652 oflags
|= _O_U16TEXT
;
1654 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1660 /*********************************************************************
1661 * _wsopen_s (MSVCRT.@)
1663 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1665 DWORD access
= 0, creation
= 0, attrib
;
1666 SECURITY_ATTRIBUTES sa
;
1671 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1672 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1674 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1677 wxflag
= split_oflags(oflags
);
1678 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1680 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1681 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1682 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1685 if (oflags
& _O_CREAT
)
1687 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1688 FIXME(": pmode 0x%04x ignored\n", pmode
);
1690 WARN(": pmode 0x%04x ignored\n", pmode
);
1692 if (oflags
& _O_EXCL
)
1693 creation
= CREATE_NEW
;
1694 else if (oflags
& _O_TRUNC
)
1695 creation
= CREATE_ALWAYS
;
1697 creation
= OPEN_ALWAYS
;
1699 else /* no _O_CREAT */
1701 if (oflags
& _O_TRUNC
)
1702 creation
= TRUNCATE_EXISTING
;
1704 creation
= OPEN_EXISTING
;
1713 sharing
= FILE_SHARE_READ
;
1716 sharing
= FILE_SHARE_WRITE
;
1719 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1722 ERR( "Unhandled shflags 0x%x\n", shflags
);
1725 attrib
= FILE_ATTRIBUTE_NORMAL
;
1727 if (oflags
& _O_TEMPORARY
)
1729 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1731 sharing
|= FILE_SHARE_DELETE
;
1734 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1735 sa
.lpSecurityDescriptor
= NULL
;
1736 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1738 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1739 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1740 && !(access
&GENERIC_READ
))
1742 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1743 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1744 if (hand
!= INVALID_HANDLE_VALUE
)
1746 oflags
= check_bom(hand
, oflags
, FALSE
);
1750 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1753 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1754 if (hand
== INVALID_HANDLE_VALUE
) {
1755 WARN(":failed-last error (%d)\n",GetLastError());
1756 _dosmaperr(GetLastError());
1760 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1762 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1763 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1764 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1766 if (oflags
& _O_U8TEXT
)
1768 DWORD written
= 0, tmp
;
1770 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1771 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1773 if (written
!= sizeof(utf8_bom
)) {
1774 WARN("error writing BOM\n");
1776 _dosmaperr(GetLastError());
1782 DWORD written
= 0, tmp
;
1784 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1785 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1787 if (written
!= sizeof(utf16_bom
))
1789 WARN("error writing BOM\n");
1791 _dosmaperr(GetLastError());
1796 else if (access
& GENERIC_READ
)
1797 oflags
= check_bom(hand
, oflags
, TRUE
);
1800 *fd
= alloc_fd(hand
, wxflag
);
1804 if (oflags
& _O_WTEXT
)
1805 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1806 else if (oflags
& _O_U16TEXT
)
1807 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1808 else if (oflags
& _O_U8TEXT
)
1809 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1811 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1815 /*********************************************************************
1816 * _wsopen (MSVCRT.@)
1818 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1823 if (oflags
& _O_CREAT
)
1827 va_start(ap
, shflags
);
1828 pmode
= va_arg(ap
, int);
1834 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1838 /*********************************************************************
1839 * _sopen_s (MSVCRT.@)
1841 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1843 DWORD access
= 0, creation
= 0, attrib
;
1847 SECURITY_ATTRIBUTES sa
;
1849 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1850 fd
, path
, oflags
, shflags
, pmode
);
1854 MSVCRT_INVALID_PMT("null out fd pointer", EINVAL
);
1859 wxflag
= split_oflags(oflags
);
1860 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1862 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1863 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1864 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1867 if (oflags
& _O_CREAT
)
1869 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1870 FIXME(": pmode 0x%04x ignored\n", pmode
);
1872 WARN(": pmode 0x%04x ignored\n", pmode
);
1874 if (oflags
& _O_EXCL
)
1875 creation
= CREATE_NEW
;
1876 else if (oflags
& _O_TRUNC
)
1877 creation
= CREATE_ALWAYS
;
1879 creation
= OPEN_ALWAYS
;
1881 else /* no _O_CREAT */
1883 if (oflags
& _O_TRUNC
)
1884 creation
= TRUNCATE_EXISTING
;
1886 creation
= OPEN_EXISTING
;
1895 sharing
= FILE_SHARE_READ
;
1898 sharing
= FILE_SHARE_WRITE
;
1901 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1904 ERR( "Unhandled shflags 0x%x\n", shflags
);
1907 attrib
= FILE_ATTRIBUTE_NORMAL
;
1909 if (oflags
& _O_TEMPORARY
)
1911 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1913 sharing
|= FILE_SHARE_DELETE
;
1916 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1917 sa
.lpSecurityDescriptor
= NULL
;
1918 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1920 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1921 if (hand
== INVALID_HANDLE_VALUE
) {
1922 WARN(":failed-last error (%d)\n", GetLastError());
1923 _dosmaperr(GetLastError());
1927 *fd
= alloc_fd(hand
, wxflag
);
1929 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1933 /*********************************************************************
1936 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1941 if (oflags
& _O_CREAT
)
1945 va_start(ap
, shflags
);
1946 pmode
= va_arg(ap
, int);
1952 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1956 /*********************************************************************
1959 int CDECL
_open( const char *path
, int flags
, ... )
1963 if (flags
& _O_CREAT
)
1966 va_start(ap
, flags
);
1967 pmode
= va_arg(ap
, int);
1969 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1972 return _sopen( path
, flags
, _SH_DENYNO
);
1975 /*********************************************************************
1978 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1982 if (flags
& _O_CREAT
)
1985 va_start(ap
, flags
);
1986 pmode
= va_arg(ap
, int);
1988 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1991 return _wsopen( path
, flags
, _SH_DENYNO
);
1994 /*********************************************************************
1997 int CDECL
_creat(const char *path
, int flags
)
1999 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2000 return _open(path
, usedFlags
);
2003 /*********************************************************************
2004 * _wcreat (MSVCRT.@)
2006 int CDECL
_wcreat(const wchar_t *path
, int flags
)
2008 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2009 return _wopen(path
, usedFlags
);
2012 /*********************************************************************
2013 * _open_osfhandle (MSVCRT.@)
2015 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2020 /* _O_RDONLY (0) always matches, so set the read flag
2021 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2022 * file, so set the write flag. It also only sets _O_TEXT if it wants
2023 * text - it never sets _O_BINARY.
2025 /* don't let split_oflags() decide the mode if no mode is passed */
2026 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2027 oflags
|= _O_BINARY
;
2029 flags
= GetFileType((HANDLE
)handle
);
2030 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2032 _dosmaperr(GetLastError());
2036 if (flags
== FILE_TYPE_CHAR
)
2038 else if (flags
== FILE_TYPE_PIPE
)
2042 flags
|= split_oflags(oflags
);
2044 fd
= alloc_fd((HANDLE
)handle
, flags
);
2045 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2049 /*********************************************************************
2052 int CDECL
_rmtmp(void)
2054 int num_removed
= 0, i
;
2058 for (i
= 3; i
< stream_idx
; i
++) {
2061 if (file
->_tmpfname
)
2070 TRACE(":removed (%d) temp files\n",num_removed
);
2074 static inline int get_utf8_char_len(char ch
)
2076 if((ch
&0xf8) == 0xf0)
2078 else if((ch
&0xf0) == 0xe0)
2080 else if((ch
&0xe0) == 0xc0)
2085 /*********************************************************************
2086 * (internal) read_utf8
2088 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2090 ioinfo
*fdinfo
= get_ioinfo(fd
);
2091 HANDLE hand
= fdinfo
->handle
;
2092 char min_buf
[4], *readbuf
, lookahead
;
2093 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2095 /* make the buffer big enough to hold at least one character */
2096 /* read bytes have to fit to output and lookahead buffers */
2098 readbuf_size
= count
< 4 ? 4 : count
;
2099 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2104 if(fdinfo
->lookahead
[0] != '\n') {
2105 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2106 fdinfo
->lookahead
[0] = '\n';
2108 if(fdinfo
->lookahead
[1] != '\n') {
2109 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2110 fdinfo
->lookahead
[1] = '\n';
2112 if(fdinfo
->lookahead
[2] != '\n') {
2113 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2114 fdinfo
->lookahead
[2] = '\n';
2119 /* NOTE: this case is broken in native dll, reading
2120 * sometimes fails when small buffer is passed
2123 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2124 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2125 fdinfo
->wxflag
|= WX_ATEOF
;
2128 _dosmaperr(GetLastError());
2131 }else if(!num_read
) {
2132 fdinfo
->wxflag
|= WX_ATEOF
;
2138 char_len
= get_utf8_char_len(readbuf
[0]);
2140 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2144 if(readbuf
[0] == '\n')
2145 fdinfo
->wxflag
|= WX_READNL
;
2147 fdinfo
->wxflag
&= ~WX_READNL
;
2149 if(readbuf
[0] == 0x1a) {
2150 fdinfo
->wxflag
|= WX_ATEOF
;
2154 if(readbuf
[0] == '\r') {
2155 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2157 else if(lookahead
== '\n')
2161 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2162 fdinfo
->lookahead
[0] = lookahead
;
2164 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2169 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2170 _dosmaperr(GetLastError());
2177 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2180 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2181 fdinfo
->wxflag
|= WX_ATEOF
;
2182 if (readbuf
!= min_buf
) free(readbuf
);
2185 _dosmaperr(GetLastError());
2186 if (readbuf
!= min_buf
) free(readbuf
);
2189 }else if(!pos
&& !num_read
) {
2190 fdinfo
->wxflag
|= WX_ATEOF
;
2191 if (readbuf
!= min_buf
) free(readbuf
);
2196 if(readbuf
[0] == '\n')
2197 fdinfo
->wxflag
|= WX_READNL
;
2199 fdinfo
->wxflag
&= ~WX_READNL
;
2201 /* Find first byte of last character (may be incomplete) */
2202 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2203 if((readbuf
[i
]&0xc0) != 0x80)
2205 char_len
= get_utf8_char_len(readbuf
[i
]);
2206 if(char_len
+i
<= pos
)
2209 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
)) {
2211 fdinfo
->lookahead
[0] = readbuf
[i
];
2213 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2215 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2217 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2221 for(i
=0, j
=0; i
<pos
; i
++) {
2222 if(readbuf
[i
] == 0x1a) {
2223 fdinfo
->wxflag
|= WX_ATEOF
;
2227 /* strip '\r' if followed by '\n' */
2228 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2229 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2230 readbuf
[j
++] = '\r';
2231 }else if(lookahead
== '\n' && j
==0) {
2232 readbuf
[j
++] = '\n';
2234 if(lookahead
!= '\n')
2235 readbuf
[j
++] = '\r';
2237 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2238 fdinfo
->lookahead
[0] = lookahead
;
2240 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2242 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2243 readbuf
[j
++] = readbuf
[i
];
2248 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2249 _dosmaperr(GetLastError());
2250 if (readbuf
!= min_buf
) free(readbuf
);
2254 if (readbuf
!= min_buf
) free(readbuf
);
2258 /*********************************************************************
2261 * When reading \r as last character in text mode, read() positions
2262 * the file pointer on the \r character while getc() goes on to
2265 static int read_i(int fd
, void *buf
, unsigned int count
)
2267 DWORD num_read
, utf16
;
2268 char *bufstart
= buf
;
2269 HANDLE hand
= fdtoh(fd
);
2270 ioinfo
*fdinfo
= get_ioinfo(fd
);
2275 if (fdinfo
->wxflag
& WX_ATEOF
) {
2276 TRACE("already at EOF, returning 0\n");
2279 /* Don't trace small reads, it gets *very* annoying */
2281 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2282 if (hand
== INVALID_HANDLE_VALUE
)
2288 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2289 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2295 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2296 return read_utf8(fd
, buf
, count
);
2298 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2300 if (fdinfo
->lookahead
[0] != '\n')
2302 bufstart
[0] = fdinfo
->lookahead
[0];
2303 fdinfo
->lookahead
[0] = '\n';
2307 bufstart
[1] = fdinfo
->lookahead
[1];
2308 fdinfo
->lookahead
[1] = '\n';
2311 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2312 num_read
+= 1+utf16
;
2317 if(utf16
&& (num_read
&1))
2319 /* msvcr90 uses uninitialized value from the buffer in this case */
2320 /* msvcrt ignores additional data */
2321 ERR("got odd number of bytes in UTF16 mode\n");
2325 if (count
!= 0 && num_read
== 0)
2327 fdinfo
->wxflag
|= WX_ATEOF
;
2328 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2330 else if (fdinfo
->wxflag
& WX_TEXT
)
2334 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2335 fdinfo
->wxflag
|= WX_READNL
;
2337 fdinfo
->wxflag
&= ~WX_READNL
;
2339 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2341 /* in text mode, a ctrl-z signals EOF */
2342 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2344 fdinfo
->wxflag
|= WX_ATEOF
;
2345 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2349 /* in text mode, strip \r if followed by \n */
2350 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2355 lookahead
[1] = '\n';
2356 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2358 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2360 bufstart
[j
++] = '\n';
2361 if(utf16
) bufstart
[j
++] = 0;
2365 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2367 bufstart
[j
++] = '\r';
2368 if(utf16
) bufstart
[j
++] = 0;
2371 if (fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2373 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2375 bufstart
[j
++] = '\n';
2376 if (utf16
) bufstart
[j
++] = 0;
2380 fdinfo
->lookahead
[0] = lookahead
[0];
2381 fdinfo
->lookahead
[1] = lookahead
[1];
2385 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2390 bufstart
[j
++] = '\r';
2391 if(utf16
) bufstart
[j
++] = 0;
2394 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2395 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2397 bufstart
[j
++] = bufstart
[i
];
2398 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2406 if (GetLastError() == ERROR_BROKEN_PIPE
)
2408 TRACE(":end-of-pipe\n");
2409 fdinfo
->wxflag
|= WX_ATEOF
;
2414 TRACE(":failed-last error (%d)\n",GetLastError());
2420 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2424 /*********************************************************************
2427 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2430 num_read
= read_i(fd
, buf
, count
);
2434 /*********************************************************************
2435 * _setmode (MSVCRT.@)
2437 int CDECL
_setmode(int fd
,int mode
)
2439 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2440 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2443 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2444 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2449 if(mode
== _O_BINARY
) {
2450 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2451 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2455 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2457 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2458 else if(mode
== _O_U8TEXT
)
2459 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2461 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2467 /*********************************************************************
2470 long CDECL
_tell(int fd
)
2472 return _lseek(fd
, 0, SEEK_CUR
);
2475 /*********************************************************************
2476 * _telli64 (MSVCRT.@)
2478 __int64 CDECL
_telli64(int fd
)
2480 return _lseeki64(fd
, 0, SEEK_CUR
);
2483 /*********************************************************************
2484 * _tempnam (MSVCRT.@)
2486 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2488 char tmpbuf
[MAX_PATH
];
2489 const char *tmp_dir
= getenv("TMP");
2491 if (tmp_dir
) dir
= tmp_dir
;
2493 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2494 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2496 TRACE("got name (%s)\n",tmpbuf
);
2497 DeleteFileA(tmpbuf
);
2498 return _strdup(tmpbuf
);
2500 TRACE("failed (%d)\n",GetLastError());
2504 /*********************************************************************
2505 * _wtempnam (MSVCRT.@)
2507 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2509 wchar_t tmpbuf
[MAX_PATH
];
2511 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2512 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2514 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2515 DeleteFileW(tmpbuf
);
2516 return _wcsdup(tmpbuf
);
2518 TRACE("failed (%d)\n",GetLastError());
2522 /*********************************************************************
2525 int CDECL
_umask(int umask
)
2527 int old_umask
= umask
;
2528 TRACE("(%d)\n",umask
);
2529 MSVCRT_umask
= umask
;
2533 /*********************************************************************
2536 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2539 ioinfo
*info
= get_ioinfo(fd
);
2540 HANDLE hand
= info
->handle
;
2542 /* Don't trace small writes, it gets *very* annoying */
2545 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2547 if (hand
== INVALID_HANDLE_VALUE
)
2553 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2559 /* If appending, go to EOF */
2560 if (info
->wxflag
& WX_APPEND
)
2561 _lseek(fd
, 0, FILE_END
);
2563 if (!(info
->wxflag
& WX_TEXT
))
2565 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2566 && (num_written
== count
))
2568 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2569 hand
, GetLastError());
2574 unsigned int i
, j
, nr_lf
, size
;
2577 const char *s
= buf
, *buf_start
= buf
;
2579 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2581 /* find number of \n */
2582 for (nr_lf
=0, i
=0; i
<count
; i
++)
2588 if ((q
= p
= malloc(size
)))
2590 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2599 FIXME("Malloc failed\n");
2611 else if (info
->exflag
& EF_UTF16
)
2613 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2614 if (s
[i
]=='\n' && s
[i
+1]==0)
2619 if ((q
= p
= malloc(size
)))
2621 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2623 if (s
[i
]=='\n' && s
[i
+1]==0)
2634 FIXME("Malloc failed\n");
2650 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2651 if (s
[i
]=='\n' && s
[i
+1]==0)
2654 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2656 _dosmaperr(GetLastError());
2661 size
= conv_len
+nr_lf
;
2662 if((p
= malloc(count
+nr_lf
*2+size
)))
2664 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2666 if (s
[i
]=='\n' && s
[i
+1]==0)
2674 q
= p
+count
+nr_lf
*2;
2675 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2676 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2680 FIXME("Malloc failed\n");
2687 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2691 if (num_written
!= size
)
2693 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2694 fd
, hand
, GetLastError(), num_written
);
2696 return s
- buf_start
;
2704 /*********************************************************************
2707 int CDECL
_putw(int val
, FILE* file
)
2712 len
= _write(file
->_file
, &val
, sizeof(val
));
2713 if (len
== sizeof(val
)) {
2718 file
->_flag
|= _IOERR
;
2723 /*********************************************************************
2726 int CDECL
fclose(FILE* file
)
2732 free(file
->_tmpfname
);
2733 file
->_tmpfname
= NULL
;
2734 /* flush stdio buffers */
2735 if(file
->_flag
& _IOWRT
)
2737 if(file
->_flag
& _IOMYBUF
)
2740 r
=_close(file
->_file
);
2744 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2745 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2747 if(file
== get_file(stream_idx
-1)) {
2748 while(stream_idx
>3 && !file
->_flag
) {
2750 file
= get_file(stream_idx
-1);
2754 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2757 /*********************************************************************
2760 int CDECL
feof(FILE* file
)
2762 return file
->_flag
& _IOEOF
;
2765 /*********************************************************************
2768 int CDECL
ferror(FILE* file
)
2770 return file
->_flag
& _IOERR
;
2773 /*********************************************************************
2774 * _filbuf (MSVCRT.@)
2776 int CDECL
_filbuf(FILE* file
)
2781 if(file
->_flag
& _IOSTRG
) {
2786 /* Allocate buffer if needed */
2787 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2790 if(!(file
->_flag
& _IOREAD
)) {
2791 if(file
->_flag
& _IORW
)
2792 file
->_flag
|= _IOREAD
;
2799 if(file
->_flag
& _IONBF
) {
2801 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2802 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2810 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2812 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2819 file
->_ptr
= file
->_base
+1;
2820 c
= *(unsigned char *)file
->_base
;
2826 /*********************************************************************
2829 int CDECL
fgetc(FILE* file
)
2837 i
= (unsigned char *)file
->_ptr
++;
2846 /*********************************************************************
2847 * _fgetchar (MSVCRT.@)
2849 int CDECL
_fgetchar(void)
2851 return fgetc(stdin
);
2854 /*********************************************************************
2857 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2860 char * buf_start
= s
;
2862 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2863 file
,file
->_file
,s
,size
);
2867 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2872 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2874 TRACE(":nothing read\n");
2878 if ((cc
!= EOF
) && (size
> 1))
2881 TRACE(":got %s\n", debugstr_a(buf_start
));
2886 /*********************************************************************
2889 wint_t CDECL
fgetwc(FILE* file
)
2896 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2897 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2900 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2909 char mbs
[MB_LEN_MAX
];
2915 if(isleadbyte((unsigned char)mbs
[0])) {
2926 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2934 /*********************************************************************
2937 int CDECL
_getw(FILE* file
)
2945 for (j
=0; j
<sizeof(int); j
++) {
2948 file
->_flag
|= _IOEOF
;
2959 /*********************************************************************
2962 wint_t CDECL
getwc(FILE* file
)
2964 return fgetwc(file
);
2967 /*********************************************************************
2968 * _fgetwchar (MSVCRT.@)
2970 wint_t CDECL
_fgetwchar(void)
2972 return fgetwc(stdin
);
2975 /*********************************************************************
2976 * getwchar (MSVCRT.@)
2978 wint_t CDECL
getwchar(void)
2980 return _fgetwchar();
2983 /*********************************************************************
2986 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2989 wchar_t * buf_start
= s
;
2991 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2992 file
,file
->_file
,s
,size
);
2996 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
3001 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3003 TRACE(":nothing read\n");
3007 if ((cc
!= WEOF
) && (size
> 1))
3010 TRACE(":got %s\n", debugstr_w(buf_start
));
3015 /*********************************************************************
3018 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3020 size_t wrcnt
=size
* nmemb
;
3029 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3030 memcpy(file
->_ptr
, ptr
, pcnt
);
3035 ptr
= (const char*)ptr
+ pcnt
;
3036 } else if(!file
->_bufsiz
&& (file
->_flag
& _IONBF
)) {
3037 if(!(file
->_flag
& _IOWRT
)) {
3038 if(file
->_flag
& _IORW
)
3039 file
->_flag
|= _IOWRT
;
3044 if(_write(file
->_file
, ptr
, wrcnt
) <= 0) {
3045 file
->_flag
|= _IOERR
;
3051 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3055 ptr
= (const char*)ptr
+ 1;
3060 return written
/ size
;
3063 /*********************************************************************
3065 * FORKED for ReactOS, don't sync with Wine!
3067 * - http://jira.reactos.org/browse/CORE-6495
3068 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3070 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3072 /* If this is a real file stream (and not some temporary one for
3073 sprintf-like functions), check whether it is opened in text mode.
3074 In this case, we have to perform an implicit conversion to ANSI. */
3075 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
3077 /* Convert to multibyte in text mode */
3078 char mbc
[MB_LEN_MAX
];
3081 mb_return
= wctomb(mbc
, c
);
3086 /* Output all characters */
3087 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3092 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3099 /*********************************************************************
3100 * _fputwchar (MSVCRT.@)
3102 wint_t CDECL
_fputwchar(wint_t wc
)
3104 return fputwc(wc
, stdout
);
3107 /*********************************************************************
3108 * _wfsopen (MSVCRT.@)
3110 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3113 int open_flags
, stream_flags
, fd
;
3115 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3117 /* map mode string to open() flags. "man fopen" for possibilities. */
3118 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3122 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3125 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
3127 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3134 TRACE(":got (%p)\n",file
);
3135 if (fd
>= 0 && !file
)
3141 /*********************************************************************
3142 * _fsopen (MSVCRT.@)
3144 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3147 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3149 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3150 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3154 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3157 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3162 ret
= _wfsopen(pathW
, modeW
, share
);
3169 /*********************************************************************
3172 FILE * CDECL
fopen(const char *path
, const char *mode
)
3174 return _fsopen( path
, mode
, _SH_DENYNO
);
3177 /*********************************************************************
3178 * fopen_s (MSVCRT.@)
3180 int CDECL
fopen_s(FILE** pFile
,
3181 const char *filename
, const char *mode
)
3183 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3184 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3189 *pFile
= fopen(filename
, mode
);
3196 /*********************************************************************
3197 * _wfopen (MSVCRT.@)
3199 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3201 return _wfsopen( path
, mode
, _SH_DENYNO
);
3204 /*********************************************************************
3205 * _wfopen_s (MSVCRT.@)
3207 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3208 const wchar_t *mode
)
3210 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3211 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3216 *pFile
= _wfopen(filename
, mode
);
3223 /* fputc calls _flsbuf which calls fputc */
3224 int CDECL
_flsbuf(int c
, FILE* file
);
3226 /*********************************************************************
3229 int CDECL
fputc(int c
, FILE* file
)
3239 res
= flush_buffer(file
);
3241 return res
? res
: c
;
3248 res
= _flsbuf(c
, file
);
3254 /*********************************************************************
3255 * _fputchar (MSVCRT.@)
3257 int CDECL
_fputchar(int c
)
3259 return fputc(c
, stdout
);
3262 /*********************************************************************
3265 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3267 size_t rcnt
=size
* nmemb
;
3276 /* first buffered data */
3278 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
3279 memcpy(ptr
, file
->_ptr
, pcnt
);
3284 ptr
= (char*)ptr
+ pcnt
;
3285 } else if(!(file
->_flag
& _IOREAD
)) {
3286 if(file
->_flag
& _IORW
) {
3287 file
->_flag
|= _IOREAD
;
3296 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& !(file
->_flag
& _IONBF
)
3297 && (file
->_bufsiz
!= 0 || alloc_buffer(file
))) {
3298 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3299 file
->_ptr
= file
->_base
;
3300 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3301 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3302 if (i
> 0 && i
< file
->_cnt
) {
3303 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3304 file
->_flag
&= ~_IOEOF
;
3307 memcpy(ptr
, file
->_ptr
, i
);
3311 } else if (rcnt
> UINT_MAX
) {
3312 i
= _read(file
->_file
, ptr
, UINT_MAX
);
3313 } else if (rcnt
< BUFSIZ
) {
3314 i
= _read(file
->_file
, ptr
, rcnt
);
3316 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3320 ptr
= (char *)ptr
+i
;
3321 /* expose feof condition in the flags
3322 * MFC tests file->_flag for feof, and doesn't call feof())
3324 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3325 file
->_flag
|= _IOEOF
;
3328 file
->_flag
|= _IOERR
;
3339 /*********************************************************************
3340 * _wfreopen (MSVCRT.@)
3343 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3345 int open_flags
, stream_flags
, fd
;
3347 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3350 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3355 /* map mode string to open() flags. "man fopen" for possibilities. */
3356 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3360 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3363 else if (init_fp(file
, fd
, stream_flags
) == -1)
3366 WARN(":failed-last error (%d)\n",GetLastError());
3367 _dosmaperr(GetLastError());
3376 /*********************************************************************
3377 * freopen (MSVCRT.@)
3380 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3383 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3385 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3386 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3392 ret
= _wfreopen(pathW
, modeW
, file
);
3399 /*********************************************************************
3400 * fsetpos (MSVCRT.@)
3402 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3407 /* Note that all this has been lifted 'as is' from fseek */
3408 if(file
->_flag
& _IOWRT
)
3411 /* Discard buffered input */
3413 file
->_ptr
= file
->_base
;
3415 /* Reset direction of i/o */
3416 if(file
->_flag
& _IORW
) {
3417 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3420 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3425 /*********************************************************************
3426 * _ftelli64 (MSVCRT.@)
3428 __int64 CDECL
_ftelli64(FILE* file
)
3433 pos
= _telli64(file
->_file
);
3439 if(file
->_flag
& _IOWRT
) {
3440 pos
+= file
->_ptr
- file
->_base
;
3442 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3445 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3449 } else if(!file
->_cnt
) { /* nothing to do */
3450 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3454 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3455 for(i
=0; i
<file
->_cnt
; i
++)
3456 if(file
->_ptr
[i
] == '\n')
3462 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3467 pos
-= file
->_bufsiz
;
3468 pos
+= file
->_ptr
- file
->_base
;
3470 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3471 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3474 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3485 /*********************************************************************
3488 LONG CDECL
ftell(FILE* file
)
3490 return (LONG
)_ftelli64(file
);
3493 /*********************************************************************
3494 * fgetpos (MSVCRT.@)
3496 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3501 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
3507 if( file
->_flag
& _IOWRT
) {
3508 off
= file
->_ptr
- file
->_base
;
3511 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3512 /* Black magic correction for CR removal */
3514 for (i
=0; i
<file
->_cnt
; i
++) {
3515 if (file
->_ptr
[i
] == '\n')
3518 /* Black magic when reading CR at buffer boundary*/
3519 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3529 /*********************************************************************
3532 int CDECL
fputs(const char *s
, FILE* file
)
3534 size_t i
, len
= strlen(s
);
3538 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3539 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3543 for (i
=0; i
<len
; i
++)
3544 if (fputc(s
[i
], file
) == EOF
) {
3553 /*********************************************************************
3556 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3558 size_t i
, len
= strlenW(s
);
3562 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3563 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3567 for (i
=0; i
<len
; i
++) {
3568 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
3569 || fputwc(s
[i
], file
) == WEOF
) {
3579 /*********************************************************************
3580 * getchar (MSVCRT.@)
3582 int CDECL
getchar(void)
3584 return fgetc(stdin
);
3587 /*********************************************************************
3590 int CDECL
getc(FILE* file
)
3595 /*********************************************************************
3598 char * CDECL
gets(char *buf
)
3601 char * buf_start
= buf
;
3604 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3606 if(cc
!= '\r') *buf
++ = (char)cc
;
3610 TRACE("got '%s'\n", buf_start
);
3611 _unlock_file(stdin
);
3615 /*********************************************************************
3618 wchar_t* CDECL
_getws(wchar_t* buf
)
3624 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3628 *buf
++ = (wchar_t)cc
;
3632 TRACE("got %s\n", debugstr_w(ws
));
3633 _unlock_file(stdin
);
3637 /*********************************************************************
3640 int CDECL
putc(int c
, FILE* file
)
3642 return fputc(c
, file
);
3645 /*********************************************************************
3646 * putchar (MSVCRT.@)
3648 int CDECL
putchar(int c
)
3650 return fputc(c
, stdout
);
3653 /*********************************************************************
3654 * _putwch (MSVCRT.@)
3656 wint_t CDECL
_putwch(wchar_t c
)
3658 return fputwc(c
, stdout
);
3661 /*********************************************************************
3664 int CDECL
puts(const char *s
)
3666 size_t len
= strlen(s
);
3670 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3671 _unlock_file(stdout
);
3675 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3676 _unlock_file(stdout
);
3680 /*********************************************************************
3683 int CDECL
_putws(const wchar_t *s
)
3685 static const wchar_t nl
= '\n';
3686 size_t len
= strlenW(s
);
3690 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3691 _unlock_file(stdout
);
3695 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3696 _unlock_file(stdout
);
3700 /*********************************************************************
3703 int CDECL
remove(const char *path
)
3705 TRACE("(%s)\n",path
);
3706 if (DeleteFileA(path
))
3708 TRACE(":failed (%d)\n",GetLastError());
3709 _dosmaperr(GetLastError());
3713 /*********************************************************************
3714 * _wremove (MSVCRT.@)
3716 int CDECL
_wremove(const wchar_t *path
)
3718 TRACE("(%s)\n",debugstr_w(path
));
3719 if (DeleteFileW(path
))
3721 TRACE(":failed (%d)\n",GetLastError());
3722 _dosmaperr(GetLastError());
3726 /*********************************************************************
3729 int CDECL
rename(const char *oldpath
,const char *newpath
)
3731 TRACE(":from %s to %s\n",oldpath
,newpath
);
3732 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3734 TRACE(":failed (%d)\n",GetLastError());
3735 _dosmaperr(GetLastError());
3739 /*********************************************************************
3740 * _wrename (MSVCRT.@)
3742 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3744 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3745 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3747 TRACE(":failed (%d)\n",GetLastError());
3748 _dosmaperr(GetLastError());
3752 /*********************************************************************
3753 * setvbuf (MSVCRT.@)
3755 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3763 if(mode
== _IOFBF
) {
3764 file
->_flag
&= ~_IONBF
;
3765 file
->_base
= file
->_ptr
= buf
;
3767 file
->_bufsiz
= size
;
3770 file
->_flag
|= _IONBF
;
3776 /*********************************************************************
3779 void CDECL
setbuf(FILE* file
, char *buf
)
3781 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3784 /*********************************************************************
3787 char * CDECL
tmpnam(char *s
)
3794 thread_data_t
*data
= msvcrt_get_thread_data();
3796 if(!data
->tmpnam_buffer
)
3797 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3799 s
= data
->tmpnam_buffer
;
3802 int_to_base32(GetCurrentProcessId(), tmpstr
);
3803 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3804 for (count
= 0; count
< TMP_MAX
; count
++)
3806 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3807 memcpy(p
, tmpstr
, size
);
3808 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3809 GetLastError() == ERROR_FILE_NOT_FOUND
)
3815 /*********************************************************************
3816 * _wtmpnam (MSVCRT.@)
3818 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3820 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3825 thread_data_t
*data
= msvcrt_get_thread_data();
3827 if(!data
->wtmpnam_buffer
)
3828 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3830 s
= data
->wtmpnam_buffer
;
3833 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3834 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3835 for (count
= 0; count
< TMP_MAX
; count
++)
3837 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3838 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3839 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3840 GetLastError() == ERROR_FILE_NOT_FOUND
)
3846 /*********************************************************************
3847 * tmpfile (MSVCRT.@)
3849 FILE* CDECL
tmpfile(void)
3851 char *filename
= tmpnam(NULL
);
3856 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3857 _S_IREAD
| _S_IWRITE
);
3858 if (fd
!= -1 && (file
= alloc_fp()))
3860 if (init_fp(file
, fd
, _IORW
) == -1)
3865 else file
->_tmpfname
= _strdup(filename
);
3868 if(fd
!= -1 && !file
)
3874 /*********************************************************************
3877 int CDECL
ungetc(int c
, FILE * file
)
3883 if(file
->_bufsiz
== 0) {
3887 if(file
->_ptr
>file
->_base
) {
3900 /*********************************************************************
3901 * ungetwc (MSVCRT.@)
3903 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3906 char * pp
= (char *)&mwc
;
3910 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3911 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3923 /*********************************************************************
3924 * _getmaxstdio (MSVCRT.@)
3926 int CDECL
_getmaxstdio(void)
3931 /*********************************************************************
3932 * _setmaxstdio (MSVCRT.@)
3934 int CDECL
_setmaxstdio(int newmax
)
3936 TRACE("%d\n", newmax
);
3938 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3941 max_streams
= newmax
;