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
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
* MSVCRT_fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
145 static int MSVCRT_max_streams
= 512, MSVCRT_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 MSVCRT_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 MSVCRT_file_cs
;
161 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
163 0, 0, &MSVCRT_file_cs
,
164 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
165 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
167 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
168 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
169 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
171 static inline ioinfo
* get_ioinfo_nolock(int fd
)
174 if(fd
>=0 && fd
<MAX_FILES
)
175 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
179 return ret
+ (fd
%FD_BLOCK_SIZE
);
182 static inline ioinfo
* get_ioinfo(int fd
)
186 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
190 return ret
+ (fd
%FD_BLOCK_SIZE
);
193 static inline FILE* msvcrt_get_file(int i
)
197 if(i
>= MSVCRT_max_streams
)
203 ret
= MSVCRT_fstream
[i
/FD_BLOCK_SIZE
];
205 MSVCRT_fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
206 if(!MSVCRT_fstream
[i
/FD_BLOCK_SIZE
]) {
207 ERR("out of memory\n");
212 ret
= MSVCRT_fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
214 ret
+= i
%FD_BLOCK_SIZE
;
219 static inline BOOL
is_valid_fd(int fd
)
221 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
224 /* INTERNAL: Get the HANDLE for a fd
225 * This doesn't lock the table, because a failure will result in
226 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
227 * it returns a valid handle which is about to be closed, a subsequent call
228 * will fail, most likely in a sane way.
230 /*static*/ HANDLE
fdtoh(int fd
)
232 if (!is_valid_fd(fd
))
234 WARN(":fd (%d) - no handle!\n",fd
);
237 return INVALID_HANDLE_VALUE
;
239 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
240 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
241 return get_ioinfo(fd
)->handle
;
244 /* INTERNAL: free a file entry fd */
245 static void msvcrt_free_fd(int fd
)
250 fdinfo
= get_ioinfo(fd
);
251 if(fdinfo
!= &__badioinfo
)
253 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
256 TRACE(":fd (%d) freed\n",fd
);
263 SetStdHandle(STD_INPUT_HANDLE
, 0);
266 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
269 SetStdHandle(STD_ERROR_HANDLE
, 0);
281 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
282 /* caller must hold the files lock */
283 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
289 WARN(":files exhausted!\n");
294 fdinfo
= get_ioinfo(fd
);
295 if(fdinfo
== &__badioinfo
) {
298 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
299 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
300 WARN(":out of memory!\n");
305 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
306 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
308 fdinfo
= get_ioinfo(fd
);
311 fdinfo
->handle
= hand
;
312 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
313 fdinfo
->lookahead
[0] = '\n';
314 fdinfo
->lookahead
[1] = '\n';
315 fdinfo
->lookahead
[2] = '\n';
318 /* locate next free slot */
319 if (fd
== fdstart
&& fd
== fdend
)
322 while (fdstart
< fdend
&&
323 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
325 /* update last fd in use */
328 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
332 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
333 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
334 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
340 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
341 /*static*/ int msvcrt_alloc_fd(HANDLE hand
, int flag
)
346 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
347 ret
= msvcrt_set_fd(hand
, flag
, fdstart
);
352 /* INTERNAL: Allocate a FILE* for an fd slot */
353 /* caller must hold the files lock */
354 static FILE* msvcrt_alloc_fp(void)
359 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
361 file
= msvcrt_get_file(i
);
365 if (file
->_flag
== 0)
367 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
375 /* INTERNAL: initialize a FILE* from an open fd */
376 static int msvcrt_init_fp(FILE* file
, int fd
, unsigned stream_flags
)
378 TRACE(":fd (%d) allocating FILE*\n",fd
);
379 if (!is_valid_fd(fd
))
381 WARN(":invalid fd %d\n",fd
);
386 file
->_ptr
= file
->_base
= NULL
;
389 file
->_flag
= stream_flags
;
390 file
->_tmpfname
= NULL
;
392 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
393 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
395 TRACE(":got FILE* (%p)\n",file
);
399 /* INTERNAL: Create an inheritance data block (for spawned process)
400 * The inheritance block is made of:
401 * 00 int nb of file descriptor (NBFD)
402 * 04 char file flags (wxflag): repeated for each fd
403 * 4+NBFD HANDLE file handle: repeated for each fd
405 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
412 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
413 *block
= calloc(*size
, 1);
419 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
420 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
422 *(unsigned*)*block
= fdend
;
423 for (fd
= 0; fd
< fdend
; fd
++)
425 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
426 fdinfo
= get_ioinfo(fd
);
427 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
429 *wxflag_ptr
= fdinfo
->wxflag
;
430 *handle_ptr
= fdinfo
->handle
;
435 *handle_ptr
= INVALID_HANDLE_VALUE
;
437 wxflag_ptr
++; handle_ptr
++;
442 /* INTERNAL: Set up all file descriptors,
443 * as well as default streams (stdin, stderr and stdout)
445 void msvcrt_init_io(void)
451 GetStartupInfoA(&si
);
452 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
458 count
= *(unsigned*)si
.lpReserved2
;
459 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
460 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
462 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
463 count
= min(count
, MAX_FILES
);
464 for (i
= 0; i
< count
; i
++)
466 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
467 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
469 wxflag_ptr
++; handle_ptr
++;
471 fdend
= max( 3, count
);
472 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
473 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
476 fdinfo
= get_ioinfo(STDIN_FILENO
);
477 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
478 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
479 DWORD type
= GetFileType(h
);
481 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
482 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDIN_FILENO
);
485 fdinfo
= get_ioinfo(STDOUT_FILENO
);
486 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
487 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
488 DWORD type
= GetFileType(h
);
490 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
491 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDOUT_FILENO
);
494 fdinfo
= get_ioinfo(STDERR_FILENO
);
495 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
496 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
497 DWORD type
= GetFileType(h
);
499 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
500 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDERR_FILENO
);
503 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(STDIN_FILENO
)->handle
,
504 get_ioinfo(STDOUT_FILENO
)->handle
,
505 get_ioinfo(STDERR_FILENO
)->handle
);
507 memset(_iob
,0,3*sizeof(FILE));
508 for (i
= 0; i
< 3; i
++)
510 /* FILE structs for stdin/out/err are static and never deleted */
512 _iob
[i
]._tmpfname
= NULL
;
513 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
515 MSVCRT_stream_idx
= 3;
518 /* INTERNAL: Flush stdio file buffer */
519 static int msvcrt_flush_buffer(FILE* file
)
521 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
522 int cnt
=file
->_ptr
-file
->_base
;
523 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
524 file
->_flag
|= _IOERR
;
527 file
->_ptr
=file
->_base
;
533 /*********************************************************************
536 int CDECL
_isatty(int fd
)
538 TRACE(":fd (%d)\n",fd
);
540 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
543 /* INTERNAL: Allocate stdio file buffer */
544 /*static*/ BOOL
alloc_buffer(FILE* file
)
546 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
547 && _isatty(file
->_file
))
550 file
->_base
= calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
552 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
553 file
->_flag
|= _IOMYBUF
;
555 file
->_base
= (char*)(&file
->_charbuf
);
557 file
->_flag
|= _IONBF
;
559 file
->_ptr
= file
->_base
;
564 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
565 static BOOL
add_std_buffer(FILE *file
)
567 static char buffers
[2][BUFSIZ
];
569 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
570 || !_isatty(file
->_file
) || file
->_bufsiz
)
573 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
574 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
578 /* INTERNAL: Removes temporary buffer from stdout or stderr */
579 /* Only call this function when add_std_buffer returned TRUE */
580 static void remove_std_buffer(FILE *file
)
582 msvcrt_flush_buffer(file
);
583 file
->_ptr
= file
->_base
= NULL
;
584 file
->_bufsiz
= file
->_cnt
= 0;
587 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
588 static int msvcrt_int_to_base32(int num
, char *str
)
603 *p
= (num
& 31) + '0';
605 *p
+= ('a' - '0' - 10);
612 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
613 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
628 *p
= (num
& 31) + '0';
630 *p
+= ('a' - '0' - 10);
637 /* INTERNAL: Create a wide string from an ascii string */
638 wchar_t *msvcrt_wstrdupa(const char *str
)
640 const unsigned int len
= strlen(str
) + 1 ;
641 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
644 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
648 /*********************************************************************
649 * __iob_func(MSVCRT.@)
651 FILE * CDECL
__iob_func(void)
656 /*********************************************************************
659 int CDECL
_access(const char *filename
, int mode
)
661 DWORD attr
= GetFileAttributesA(filename
);
663 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
665 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
667 _dosmaperr(GetLastError());
670 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
672 _set_errno(ERROR_ACCESS_DENIED
);
678 /*********************************************************************
679 * _access_s (MSVCRT.@)
681 int CDECL
_access_s(const char *filename
, int mode
)
683 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
684 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
690 return _access(filename
, mode
);
693 /*********************************************************************
694 * _waccess (MSVCRT.@)
696 int CDECL
_waccess(const wchar_t *filename
, int mode
)
698 DWORD attr
= GetFileAttributesW(filename
);
700 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
702 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
704 _dosmaperr(GetLastError());
707 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
709 _set_errno(ERROR_ACCESS_DENIED
);
715 /*********************************************************************
716 * _waccess_s (MSVCRT.@)
718 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
720 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
721 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
727 return _waccess(filename
, mode
);
730 /*********************************************************************
733 int CDECL
_chmod(const char *path
, int flags
)
735 DWORD oldFlags
= GetFileAttributesA(path
);
737 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
739 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
740 oldFlags
| FILE_ATTRIBUTE_READONLY
;
742 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
745 _dosmaperr(GetLastError());
749 /*********************************************************************
752 int CDECL
_wchmod(const wchar_t *path
, int flags
)
754 DWORD oldFlags
= GetFileAttributesW(path
);
756 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
758 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
759 oldFlags
| FILE_ATTRIBUTE_READONLY
;
761 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
764 _dosmaperr(GetLastError());
768 /*********************************************************************
771 int CDECL
_unlink(const char *path
)
773 TRACE("%s\n",debugstr_a(path
));
774 if(DeleteFileA(path
))
776 TRACE("failed (%d)\n",GetLastError());
777 _dosmaperr(GetLastError());
781 /*********************************************************************
782 * _wunlink (MSVCRT.@)
784 int CDECL
_wunlink(const wchar_t *path
)
786 TRACE("(%s)\n",debugstr_w(path
));
787 if(DeleteFileW(path
))
789 TRACE("failed (%d)\n",GetLastError());
790 _dosmaperr(GetLastError());
794 /*********************************************************************
797 int CDECL
_commit(int fd
)
799 HANDLE hand
= fdtoh(fd
);
801 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
802 if (hand
== INVALID_HANDLE_VALUE
)
805 if (!FlushFileBuffers(hand
))
807 if (GetLastError() == ERROR_INVALID_HANDLE
)
809 /* FlushFileBuffers fails for console handles
810 * so we ignore this error.
814 TRACE(":failed-last error (%d)\n",GetLastError());
815 _dosmaperr(GetLastError());
822 /* _flushall calls fflush which calls _flushall */
823 int CDECL
fflush(FILE* file
);
825 /* INTERNAL: Flush all stream buffer */
826 static int msvcrt_flush_all_buffers(int mask
)
828 int i
, num_flushed
= 0;
832 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
833 file
= msvcrt_get_file(i
);
837 if(file
->_flag
& mask
) {
845 TRACE(":flushed (%d) handles\n",num_flushed
);
849 /*********************************************************************
850 * _flushall (MSVCRT.@)
852 int CDECL
_flushall(void)
854 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
857 /*********************************************************************
860 int CDECL
fflush(FILE* file
)
863 msvcrt_flush_all_buffers(_IOWRT
);
864 } else if(file
->_flag
& _IOWRT
) {
868 res
= msvcrt_flush_buffer(file
);
870 if(!res && (file->_flag & _IOCOMMIT))
871 res = _commit(file->_file) ? EOF : 0;
876 } else if(file
->_flag
& _IOREAD
) {
879 file
->_ptr
= file
->_base
;
887 /*********************************************************************
890 int CDECL
_close(int fd
)
897 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
898 if (hand
== INVALID_HANDLE_VALUE
)
900 else if (!CloseHandle(hand
))
902 WARN(":failed-last error (%d)\n",GetLastError());
903 _dosmaperr(GetLastError());
916 /*********************************************************************
919 * MSDN isn't clear on this point, but the remarks for _pipe
920 * indicate file descriptors duplicated with _dup and _dup2 are always
923 int CDECL
_dup2(int od
, int nd
)
927 TRACE("(od=%d, nd=%d)\n", od
, nd
);
929 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
933 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
934 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
936 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
940 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
948 /* _dup2 returns 0, not nd, on success */
955 _dosmaperr(GetLastError());
967 /*********************************************************************
970 int CDECL
_dup(int od
)
976 if (_dup2(od
, fd
) == 0)
984 /*********************************************************************
987 int CDECL
_eof(int fd
)
990 LONG hcurpos
,hendpos
;
991 HANDLE hand
= fdtoh(fd
);
993 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
995 if (hand
== INVALID_HANDLE_VALUE
)
998 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1000 /* Otherwise we do it the hard way */
1001 hcurpos
= hendpos
= 0;
1002 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1003 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1005 if (curpos
== endpos
&& hcurpos
== hendpos
)
1007 /* FIXME: shouldn't WX_ATEOF be set here? */
1011 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1015 /*********************************************************************
1016 * _fcloseall (MSVCRT.@)
1018 int CDECL
_fcloseall(void)
1020 int num_closed
= 0, i
;
1024 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1025 file
= msvcrt_get_file(i
);
1027 if (file
->_flag
&& !fclose(file
))
1032 TRACE(":closed (%d) handles\n",num_closed
);
1036 /* free everything on process exit */
1037 void msvcrt_free_io(void)
1045 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1048 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1050 FILE *file
= msvcrt_get_file(j
);
1051 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1053 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1054 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1058 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1059 free(MSVCRT_fstream
[i
]);
1062 /*********************************************************************
1063 * _lseeki64 (MSVCRT.@)
1065 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1067 HANDLE hand
= fdtoh(fd
);
1070 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1071 if (hand
== INVALID_HANDLE_VALUE
)
1074 if (whence
< 0 || whence
> 2)
1080 TRACE(":fd (%d) to %s pos %s\n",
1081 fd
,wine_dbgstr_longlong(offset
),
1082 (whence
==SEEK_SET
)?"SEEK_SET":
1083 (whence
==SEEK_CUR
)?"SEEK_CUR":
1084 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1086 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1087 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1088 ofs
.QuadPart
= offset
;
1089 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1090 GetLastError() == ERROR_SUCCESS
)
1092 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1093 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1095 return ofs
.QuadPart
;
1097 TRACE(":error-last error (%d)\n",GetLastError());
1098 _dosmaperr(GetLastError());
1102 /*********************************************************************
1105 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1107 return (LONG
)_lseeki64(fd
, offset
, whence
);
1110 /*********************************************************************
1111 * _lock_file (MSVCRT.@)
1113 void CDECL
_lock_file(FILE *file
)
1115 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1116 _lock(_STREAM_LOCKS
+(file
-_iob
));
1117 /* ReactOS: string streams dont need to be locked */
1118 else if(!(file
->_flag
& _IOSTRG
))
1119 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1122 /*********************************************************************
1123 * _unlock_file (MSVCRT.@)
1125 void CDECL
_unlock_file(FILE *file
)
1127 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1128 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1129 /* ReactOS: string streams dont need to be locked */
1130 else if(!(file
->_flag
& _IOSTRG
))
1131 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1135 /*********************************************************************
1136 * _locking (MSVCRT.@)
1138 * This is untested; the underlying LockFile doesn't work yet.
1140 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1144 HANDLE hand
= fdtoh(fd
);
1146 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1147 if (hand
== INVALID_HANDLE_VALUE
)
1150 if (mode
< 0 || mode
> 4)
1156 TRACE(":fd (%d) by 0x%08x mode %s\n",
1157 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1158 (mode
==_LK_LOCK
)?"_LK_LOCK":
1159 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1160 (mode
==_LK_RLCK
)?"_LK_RLCK":
1161 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1164 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1166 FIXME ("Seek failed\n");
1167 *_errno() = EINVAL
; /* FIXME */
1170 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1173 ret
= 1; /* just to satisfy gcc */
1176 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1181 else if (mode
== _LK_UNLCK
)
1182 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1184 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1185 /* FIXME - what about error settings? */
1186 return ret
? 0 : -1;
1189 /*********************************************************************
1190 * _fseeki64 (MSVCRT.@)
1192 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1197 /* Flush output if needed */
1198 if(file
->_flag
& _IOWRT
)
1199 msvcrt_flush_buffer(file
);
1201 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1203 offset
+= _ftelli64(file
);
1206 /* Discard buffered input */
1208 file
->_ptr
= file
->_base
;
1209 /* Reset direction of i/o */
1210 if(file
->_flag
& _IORW
) {
1211 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1213 /* Clear end of file flag */
1214 file
->_flag
&= ~_IOEOF
;
1215 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1221 /*********************************************************************
1224 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1226 return _fseeki64( file
, offset
, whence
);
1229 /*********************************************************************
1230 * _chsize_s (MSVCRT.@)
1232 int CDECL
_chsize_s(int fd
, __int64 size
)
1238 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1240 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1245 if (handle
!= INVALID_HANDLE_VALUE
)
1247 /* save the current file pointer */
1248 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1251 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1254 ret
= SetEndOfFile(handle
);
1255 if (!ret
) _dosmaperr(GetLastError());
1258 /* restore the file pointer */
1259 _lseeki64(fd
, cur
, SEEK_SET
);
1264 return ret
? 0 : *_errno();
1267 /*********************************************************************
1268 * _chsize (MSVCRT.@)
1270 int CDECL
_chsize(int fd
, long size
)
1272 /* _chsize_s returns errno on failure but _chsize should return -1 */
1273 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1276 /*********************************************************************
1277 * clearerr (MSVCRT.@)
1279 void CDECL
clearerr(FILE* file
)
1281 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1284 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1288 /*********************************************************************
1291 void CDECL
rewind(FILE* file
)
1293 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1296 fseek(file
, 0L, SEEK_SET
);
1301 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1303 int plus
= strchrW(mode
, '+') != NULL
;
1305 TRACE("%s\n", debugstr_w(mode
));
1307 while(*mode
== ' ') mode
++;
1312 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1313 *stream_flags
= plus
? _IORW
: _IOREAD
;
1316 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1317 *stream_flags
= plus
? _IORW
: _IOWRT
;
1320 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1321 *stream_flags
= plus
? _IORW
: _IOWRT
;
1324 MSVCRT_INVALID_PMT(0, EINVAL
);
1328 *stream_flags
|= _commode
;
1330 while (*mode
&& *mode
!=',')
1334 *open_flags
|= _O_BINARY
;
1335 *open_flags
&= ~_O_TEXT
;
1338 *open_flags
|= _O_TEXT
;
1339 *open_flags
&= ~_O_BINARY
;
1342 *open_flags
|= _O_TEMPORARY
;
1345 *open_flags
|= _O_SHORT_LIVED
;
1348 *stream_flags
|= _IOCOMMIT
;
1351 *stream_flags
&= ~_IOCOMMIT
;
1354 *open_flags
|= _O_NOINHERIT
;
1363 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1366 ERR("incorrect mode flag: %c\n", mode
[-1]);
1372 static const WCHAR ccs
[] = {'c','c','s'};
1373 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1374 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1375 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1378 while(*mode
== ' ') mode
++;
1379 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1381 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1382 while(*mode
== ' ') mode
++;
1383 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1386 while(*mode
== ' ') mode
++;
1388 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1390 *open_flags
|= _O_U8TEXT
;
1391 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1393 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1395 *open_flags
|= _O_U16TEXT
;
1396 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1398 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1400 *open_flags
|= _O_WTEXT
;
1401 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1405 MSVCRT_INVALID_PMT(0, EINVAL
);
1409 while(*mode
== ' ') mode
++;
1412 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1417 /*********************************************************************
1418 * _fdopen (MSVCRT.@)
1420 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1423 wchar_t *modeW
= NULL
;
1425 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1427 ret
= _wfdopen(fd
, modeW
);
1433 /*********************************************************************
1434 * _wfdopen (MSVCRT.@)
1436 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1438 int open_flags
, stream_flags
;
1441 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1444 if (!(file
= msvcrt_alloc_fp()))
1446 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1451 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1457 /*********************************************************************
1458 * _filelength (MSVCRT.@)
1460 LONG CDECL
_filelength(int fd
)
1462 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1465 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1468 if (endPos
!= curPos
)
1469 _lseek(fd
, curPos
, SEEK_SET
);
1476 /*********************************************************************
1477 * _filelengthi64 (MSVCRT.@)
1479 __int64 CDECL
_filelengthi64(int fd
)
1481 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1484 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1487 if (endPos
!= curPos
)
1488 _lseeki64(fd
, curPos
, SEEK_SET
);
1495 /*********************************************************************
1496 * _fileno (MSVCRT.@)
1498 int CDECL
_fileno(FILE* file
)
1500 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1504 /*********************************************************************
1505 * _get_osfhandle (MSVCRT.@)
1507 intptr_t CDECL
_get_osfhandle(int fd
)
1509 HANDLE hand
= fdtoh(fd
);
1510 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1512 return (intptr_t)hand
;
1515 /*********************************************************************
1516 * _mktemp (MSVCRT.@)
1518 char * CDECL
_mktemp(char *pattern
)
1521 char *retVal
= pattern
;
1529 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1533 id
= GetCurrentProcessId();
1537 int tempNum
= id
/ 10;
1538 *pattern
-- = id
- (tempNum
* 10) + '0';
1544 *pattern
= letter
++;
1545 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1547 } while(letter
<= 'z');
1551 /*********************************************************************
1552 * _wmktemp (MSVCRT.@)
1554 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1557 wchar_t *retVal
= pattern
;
1559 wchar_t letter
= 'a';
1562 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1566 id
= GetCurrentProcessId();
1570 int tempNum
= id
/ 10;
1571 *pattern
-- = id
- (tempNum
* 10) + '0';
1577 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1578 GetLastError() == ERROR_FILE_NOT_FOUND
)
1580 *pattern
= letter
++;
1581 } while(letter
!= '|');
1585 /*static*/ unsigned split_oflags(unsigned oflags
)
1588 unsigned unsupp
; /* until we support everything */
1590 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1591 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1592 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1593 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1594 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1595 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1596 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1597 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1598 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1600 if ((unsupp
= oflags
& ~(
1601 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1602 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1603 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1605 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1606 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1608 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1613 /*********************************************************************
1616 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1619 SECURITY_ATTRIBUTES sa
;
1620 HANDLE readHandle
, writeHandle
;
1628 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1629 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1630 sa
.lpSecurityDescriptor
= NULL
;
1631 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1633 unsigned int wxflags
= split_oflags(textmode
);
1637 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1641 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1650 CloseHandle(writeHandle
);
1656 CloseHandle(readHandle
);
1657 CloseHandle(writeHandle
);
1663 _dosmaperr(GetLastError());
1668 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1670 char bom
[sizeof(utf8_bom
)];
1673 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1675 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1678 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1679 oflags
|= _O_U8TEXT
;
1680 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1682 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1683 oflags
|= _O_U16TEXT
;
1685 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1691 /*********************************************************************
1692 * _wsopen_s (MSVCRT.@)
1694 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1696 DWORD access
= 0, creation
= 0, attrib
;
1697 SECURITY_ATTRIBUTES sa
;
1698 DWORD sharing
, type
;
1702 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1703 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1705 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1708 wxflag
= split_oflags(oflags
);
1709 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1711 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1712 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1713 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1716 if (oflags
& _O_CREAT
)
1718 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1719 FIXME(": pmode 0x%04x ignored\n", pmode
);
1721 WARN(": pmode 0x%04x ignored\n", pmode
);
1723 if (oflags
& _O_EXCL
)
1724 creation
= CREATE_NEW
;
1725 else if (oflags
& _O_TRUNC
)
1726 creation
= CREATE_ALWAYS
;
1728 creation
= OPEN_ALWAYS
;
1730 else /* no _O_CREAT */
1732 if (oflags
& _O_TRUNC
)
1733 creation
= TRUNCATE_EXISTING
;
1735 creation
= OPEN_EXISTING
;
1744 sharing
= FILE_SHARE_READ
;
1747 sharing
= FILE_SHARE_WRITE
;
1750 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1753 ERR( "Unhandled shflags 0x%x\n", shflags
);
1756 attrib
= FILE_ATTRIBUTE_NORMAL
;
1758 if (oflags
& _O_TEMPORARY
)
1760 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1762 sharing
|= FILE_SHARE_DELETE
;
1765 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1766 sa
.lpSecurityDescriptor
= NULL
;
1767 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1769 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1770 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1771 && !(access
&GENERIC_READ
))
1773 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1774 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1775 if (hand
!= INVALID_HANDLE_VALUE
)
1777 oflags
= check_bom(hand
, oflags
, FALSE
);
1781 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1784 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1785 if (hand
== INVALID_HANDLE_VALUE
) {
1786 WARN(":failed-last error (%d)\n",GetLastError());
1787 _dosmaperr(GetLastError());
1791 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1793 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1794 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1795 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1797 if (oflags
& _O_U8TEXT
)
1799 DWORD written
= 0, tmp
;
1801 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1802 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1804 if (written
!= sizeof(utf8_bom
)) {
1805 WARN("error writing BOM\n");
1807 _dosmaperr(GetLastError());
1813 DWORD written
= 0, tmp
;
1815 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1816 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1818 if (written
!= sizeof(utf16_bom
))
1820 WARN("error writing BOM\n");
1822 _dosmaperr(GetLastError());
1827 else if (access
& GENERIC_READ
)
1828 oflags
= check_bom(hand
, oflags
, TRUE
);
1830 type
= GetFileType(hand
);
1831 if (type
== FILE_TYPE_CHAR
)
1833 else if (type
== FILE_TYPE_PIPE
)
1836 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1840 if (oflags
& _O_WTEXT
)
1841 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1842 else if (oflags
& _O_U16TEXT
)
1843 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1844 else if (oflags
& _O_U8TEXT
)
1845 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1847 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1851 /*********************************************************************
1852 * _wsopen (MSVCRT.@)
1854 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1859 if (oflags
& _O_CREAT
)
1863 __ms_va_start(ap
, shflags
);
1864 pmode
= va_arg(ap
, int);
1870 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1874 /*********************************************************************
1875 * _sopen_s (MSVCRT.@)
1877 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1882 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1885 ret
= _wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1890 /*********************************************************************
1893 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1898 if (oflags
& _O_CREAT
)
1902 va_start(ap
, shflags
);
1903 pmode
= va_arg(ap
, int);
1909 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1913 /*********************************************************************
1916 int CDECL
_open( const char *path
, int flags
, ... )
1920 if (flags
& _O_CREAT
)
1923 va_start(ap
, flags
);
1924 pmode
= va_arg(ap
, int);
1926 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1929 return _sopen( path
, flags
, _SH_DENYNO
);
1932 /*********************************************************************
1935 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1939 if (flags
& _O_CREAT
)
1942 va_start(ap
, flags
);
1943 pmode
= va_arg(ap
, int);
1945 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1948 return _wsopen( path
, flags
, _SH_DENYNO
);
1951 /*********************************************************************
1954 int CDECL
_creat(const char *path
, int flags
)
1956 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1957 return _open(path
, usedFlags
);
1960 /*********************************************************************
1961 * _wcreat (MSVCRT.@)
1963 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1965 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1966 return _wopen(path
, usedFlags
);
1969 /*********************************************************************
1970 * _open_osfhandle (MSVCRT.@)
1972 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1977 /* _O_RDONLY (0) always matches, so set the read flag
1978 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1979 * file, so set the write flag. It also only sets _O_TEXT if it wants
1980 * text - it never sets _O_BINARY.
1982 /* don't let split_oflags() decide the mode if no mode is passed */
1983 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1984 oflags
|= _O_BINARY
;
1986 flags
= GetFileType((HANDLE
)handle
);
1987 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
1989 _dosmaperr(GetLastError());
1993 if (flags
== FILE_TYPE_CHAR
)
1995 else if (flags
== FILE_TYPE_PIPE
)
1999 flags
|= split_oflags(oflags
);
2001 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2002 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2006 /*********************************************************************
2009 int CDECL
_rmtmp(void)
2011 int num_removed
= 0, i
;
2015 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2016 file
= msvcrt_get_file(i
);
2018 if (file
->_tmpfname
)
2027 TRACE(":removed (%d) temp files\n",num_removed
);
2031 static inline int get_utf8_char_len(char ch
)
2033 if((ch
&0xf8) == 0xf0)
2035 else if((ch
&0xf0) == 0xe0)
2037 else if((ch
&0xe0) == 0xc0)
2042 /*********************************************************************
2043 * (internal) read_utf8
2045 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2047 ioinfo
*fdinfo
= get_ioinfo(fd
);
2048 HANDLE hand
= fdinfo
->handle
;
2049 char min_buf
[4], *readbuf
, lookahead
;
2050 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2052 /* make the buffer big enough to hold at least one character */
2053 /* read bytes have to fit to output and lookahead buffers */
2055 readbuf_size
= count
< 4 ? 4 : count
;
2056 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2061 if(fdinfo
->lookahead
[0] != '\n') {
2062 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2063 fdinfo
->lookahead
[0] = '\n';
2065 if(fdinfo
->lookahead
[1] != '\n') {
2066 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2067 fdinfo
->lookahead
[1] = '\n';
2069 if(fdinfo
->lookahead
[2] != '\n') {
2070 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2071 fdinfo
->lookahead
[2] = '\n';
2076 /* NOTE: this case is broken in native dll, reading
2077 * sometimes fails when small buffer is passed
2080 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2081 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2082 fdinfo
->wxflag
|= WX_ATEOF
;
2085 _dosmaperr(GetLastError());
2088 }else if(!num_read
) {
2089 fdinfo
->wxflag
|= WX_ATEOF
;
2095 char_len
= get_utf8_char_len(readbuf
[0]);
2097 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2101 if(readbuf
[0] == '\n')
2102 fdinfo
->wxflag
|= WX_READNL
;
2104 fdinfo
->wxflag
&= ~WX_READNL
;
2106 if(readbuf
[0] == 0x1a) {
2107 fdinfo
->wxflag
|= WX_ATEOF
;
2111 if(readbuf
[0] == '\r') {
2112 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2114 else if(lookahead
== '\n')
2118 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2119 fdinfo
->lookahead
[0] = lookahead
;
2121 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2126 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2127 _dosmaperr(GetLastError());
2134 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2137 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2138 fdinfo
->wxflag
|= WX_ATEOF
;
2139 if (readbuf
!= min_buf
) free(readbuf
);
2142 _dosmaperr(GetLastError());
2143 if (readbuf
!= min_buf
) free(readbuf
);
2146 }else if(!pos
&& !num_read
) {
2147 fdinfo
->wxflag
|= WX_ATEOF
;
2148 if (readbuf
!= min_buf
) free(readbuf
);
2153 if(readbuf
[0] == '\n')
2154 fdinfo
->wxflag
|= WX_READNL
;
2156 fdinfo
->wxflag
&= ~WX_READNL
;
2158 /* Find first byte of last character (may be incomplete) */
2159 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2160 if((readbuf
[i
]&0xc0) != 0x80)
2162 char_len
= get_utf8_char_len(readbuf
[i
]);
2163 if(char_len
+i
<= pos
)
2166 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2168 fdinfo
->lookahead
[0] = readbuf
[i
];
2170 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2172 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2174 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2178 for(i
=0, j
=0; i
<pos
; i
++) {
2179 if(readbuf
[i
] == 0x1a) {
2180 fdinfo
->wxflag
|= WX_ATEOF
;
2184 /* strip '\r' if followed by '\n' */
2185 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2186 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2187 readbuf
[j
++] = '\r';
2188 }else if(lookahead
== '\n' && j
==0) {
2189 readbuf
[j
++] = '\n';
2191 if(lookahead
!= '\n')
2192 readbuf
[j
++] = '\r';
2194 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2195 fdinfo
->lookahead
[0] = lookahead
;
2197 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2199 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2200 readbuf
[j
++] = readbuf
[i
];
2205 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2206 _dosmaperr(GetLastError());
2207 if (readbuf
!= min_buf
) free(readbuf
);
2211 if (readbuf
!= min_buf
) free(readbuf
);
2215 /*********************************************************************
2218 * When reading \r as last character in text mode, read() positions
2219 * the file pointer on the \r character while getc() goes on to
2222 static int read_i(int fd
, void *buf
, unsigned int count
)
2224 DWORD num_read
, utf16
;
2225 char *bufstart
= buf
;
2226 HANDLE hand
= fdtoh(fd
);
2227 ioinfo
*fdinfo
= get_ioinfo(fd
);
2232 if (fdinfo
->wxflag
& WX_ATEOF
) {
2233 TRACE("already at EOF, returning 0\n");
2236 /* Don't trace small reads, it gets *very* annoying */
2238 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2239 if (hand
== INVALID_HANDLE_VALUE
)
2245 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2246 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2252 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2253 return read_utf8(fd
, buf
, count
);
2255 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2257 if (fdinfo
->lookahead
[0] != '\n')
2259 bufstart
[0] = fdinfo
->lookahead
[0];
2260 fdinfo
->lookahead
[0] = '\n';
2264 bufstart
[1] = fdinfo
->lookahead
[1];
2265 fdinfo
->lookahead
[1] = '\n';
2268 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2269 num_read
+= 1+utf16
;
2274 if(utf16
&& (num_read
&1))
2276 /* msvcr90 uses uninitialized value from the buffer in this case */
2277 /* msvcrt ignores additional data */
2278 ERR("got odd number of bytes in UTF16 mode\n");
2282 if (count
!= 0 && num_read
== 0)
2284 fdinfo
->wxflag
|= WX_ATEOF
;
2285 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2287 else if (fdinfo
->wxflag
& WX_TEXT
)
2291 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2292 fdinfo
->wxflag
|= WX_READNL
;
2294 fdinfo
->wxflag
&= ~WX_READNL
;
2296 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2298 /* in text mode, a ctrl-z signals EOF */
2299 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2301 fdinfo
->wxflag
|= WX_ATEOF
;
2302 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2306 /* in text mode, strip \r if followed by \n */
2307 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2312 lookahead
[1] = '\n';
2313 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2315 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2317 bufstart
[j
++] = '\n';
2318 if(utf16
) bufstart
[j
++] = 0;
2322 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2324 bufstart
[j
++] = '\r';
2325 if(utf16
) bufstart
[j
++] = 0;
2328 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2330 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2332 bufstart
[j
++] = '\n';
2333 if (utf16
) bufstart
[j
++] = 0;
2337 fdinfo
->lookahead
[0] = lookahead
[0];
2338 fdinfo
->lookahead
[1] = lookahead
[1];
2342 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2347 bufstart
[j
++] = '\r';
2348 if(utf16
) bufstart
[j
++] = 0;
2351 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2352 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2354 bufstart
[j
++] = bufstart
[i
];
2355 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2363 if (GetLastError() == ERROR_BROKEN_PIPE
)
2365 TRACE(":end-of-pipe\n");
2366 fdinfo
->wxflag
|= WX_ATEOF
;
2371 TRACE(":failed-last error (%d)\n",GetLastError());
2377 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2381 /*********************************************************************
2384 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2387 num_read
= read_i(fd
, buf
, count
);
2391 /*********************************************************************
2392 * _setmode (MSVCRT.@)
2394 int CDECL
_setmode(int fd
,int mode
)
2396 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2397 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2400 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2401 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2406 if(mode
== _O_BINARY
) {
2407 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2408 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2412 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2414 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2415 else if(mode
== _O_U8TEXT
)
2416 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2418 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2424 /*********************************************************************
2427 long CDECL
_tell(int fd
)
2429 return _lseek(fd
, 0, SEEK_CUR
);
2432 /*********************************************************************
2433 * _telli64 (MSVCRT.@)
2435 __int64 CDECL
_telli64(int fd
)
2437 return _lseeki64(fd
, 0, SEEK_CUR
);
2440 /*********************************************************************
2441 * _tempnam (MSVCRT.@)
2443 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2445 char tmpbuf
[MAX_PATH
];
2446 const char *tmp_dir
= getenv("TMP");
2448 if (tmp_dir
) dir
= tmp_dir
;
2450 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2451 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2453 TRACE("got name (%s)\n",tmpbuf
);
2454 DeleteFileA(tmpbuf
);
2455 return _strdup(tmpbuf
);
2457 TRACE("failed (%d)\n",GetLastError());
2461 /*********************************************************************
2462 * _wtempnam (MSVCRT.@)
2464 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2466 wchar_t tmpbuf
[MAX_PATH
];
2468 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2469 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2471 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2472 DeleteFileW(tmpbuf
);
2473 return _wcsdup(tmpbuf
);
2475 TRACE("failed (%d)\n",GetLastError());
2479 /*********************************************************************
2482 int CDECL
_umask(int umask
)
2484 int old_umask
= MSVCRT_umask
;
2485 TRACE("(%d)\n",umask
);
2486 MSVCRT_umask
= umask
;
2490 /*********************************************************************
2493 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2496 ioinfo
*info
= get_ioinfo(fd
);
2497 HANDLE hand
= info
->handle
;
2499 /* Don't trace small writes, it gets *very* annoying */
2502 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2504 if (hand
== INVALID_HANDLE_VALUE
)
2510 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2516 /* If appending, go to EOF */
2517 if (info
->wxflag
& WX_APPEND
)
2518 _lseek(fd
, 0, FILE_END
);
2520 if (!(info
->wxflag
& WX_TEXT
))
2522 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2523 && (num_written
== count
))
2525 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2526 hand
, GetLastError());
2531 unsigned int i
, j
, nr_lf
, size
;
2534 const char *s
= buf
, *buf_start
= buf
;
2536 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2538 /* find number of \n */
2539 for (nr_lf
=0, i
=0; i
<count
; i
++)
2545 if ((q
= p
= malloc(size
)))
2547 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2556 FIXME("Malloc failed\n");
2568 else if (info
->exflag
& EF_UTF16
)
2570 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2571 if (s
[i
]=='\n' && s
[i
+1]==0)
2576 if ((q
= p
= malloc(size
)))
2578 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2580 if (s
[i
]=='\n' && s
[i
+1]==0)
2591 FIXME("Malloc failed\n");
2607 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2608 if (s
[i
]=='\n' && s
[i
+1]==0)
2611 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2613 _dosmaperr(GetLastError());
2618 size
= conv_len
+nr_lf
;
2619 if((p
= malloc(count
+nr_lf
*2+size
)))
2621 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2623 if (s
[i
]=='\n' && s
[i
+1]==0)
2631 q
= p
+count
+nr_lf
*2;
2632 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2633 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2637 FIXME("Malloc failed\n");
2644 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2648 if (num_written
!= size
)
2650 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2651 fd
, hand
, GetLastError(), num_written
);
2653 return s
- buf_start
;
2661 /*********************************************************************
2664 int CDECL
_putw(int val
, FILE* file
)
2669 len
= _write(file
->_file
, &val
, sizeof(val
));
2670 if (len
== sizeof(val
)) {
2675 file
->_flag
|= _IOERR
;
2680 /*********************************************************************
2683 int CDECL
fclose(FILE* file
)
2689 free(file
->_tmpfname
);
2690 file
->_tmpfname
= NULL
;
2691 /* flush stdio buffers */
2692 if(file
->_flag
& _IOWRT
)
2694 if(file
->_flag
& _IOMYBUF
)
2697 r
=_close(file
->_file
);
2701 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2702 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2704 if(file
== msvcrt_get_file(MSVCRT_stream_idx
-1)) {
2705 while(MSVCRT_stream_idx
>3 && !file
->_flag
) {
2706 MSVCRT_stream_idx
--;
2707 file
= msvcrt_get_file(MSVCRT_stream_idx
-1);
2711 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2714 /*********************************************************************
2717 int CDECL
feof(FILE* file
)
2719 return file
->_flag
& _IOEOF
;
2722 /*********************************************************************
2725 int CDECL
ferror(FILE* file
)
2727 return file
->_flag
& _IOERR
;
2730 /*********************************************************************
2731 * _filbuf (MSVCRT.@)
2733 int CDECL
_filbuf(FILE* file
)
2738 if(file
->_flag
& _IOSTRG
) {
2743 /* Allocate buffer if needed */
2744 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
2747 if(!(file
->_flag
& _IOREAD
)) {
2748 if(file
->_flag
& _IORW
)
2749 file
->_flag
|= _IOREAD
;
2756 if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
2758 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2759 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2767 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2769 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2776 file
->_ptr
= file
->_base
+1;
2777 c
= *(unsigned char *)file
->_base
;
2783 /*********************************************************************
2786 int CDECL
fgetc(FILE* file
)
2794 i
= (unsigned char *)file
->_ptr
++;
2803 /*********************************************************************
2804 * _fgetchar (MSVCRT.@)
2806 int CDECL
_fgetchar(void)
2808 return fgetc(stdin
);
2811 /*********************************************************************
2814 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2817 char * buf_start
= s
;
2819 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2820 file
,file
->_file
,s
,size
);
2824 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2829 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2831 TRACE(":nothing read\n");
2835 if ((cc
!= EOF
) && (size
> 1))
2838 TRACE(":got %s\n", debugstr_a(buf_start
));
2843 /*********************************************************************
2846 wint_t CDECL
fgetwc(FILE* file
)
2853 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2854 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2857 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2866 char mbs
[MB_LEN_MAX
];
2872 if(isleadbyte((unsigned char)mbs
[0])) {
2883 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2891 /*********************************************************************
2894 int CDECL
_getw(FILE* file
)
2902 for (j
=0; j
<sizeof(int); j
++) {
2905 file
->_flag
|= _IOEOF
;
2916 /*********************************************************************
2919 wint_t CDECL
getwc(FILE* file
)
2921 return fgetwc(file
);
2924 /*********************************************************************
2925 * _fgetwchar (MSVCRT.@)
2927 wint_t CDECL
_fgetwchar(void)
2929 return fgetwc(stdin
);
2932 /*********************************************************************
2933 * getwchar (MSVCRT.@)
2935 wint_t CDECL
getwchar(void)
2937 return _fgetwchar();
2940 /*********************************************************************
2943 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2946 wchar_t * buf_start
= s
;
2948 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2949 file
,file
->_file
,s
,size
);
2953 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2958 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2960 TRACE(":nothing read\n");
2964 if ((cc
!= WEOF
) && (size
> 1))
2967 TRACE(":got %s\n", debugstr_w(buf_start
));
2972 /*********************************************************************
2975 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2977 size_t wrcnt
=size
* nmemb
;
2986 if(file
->_cnt
< 0) {
2987 WARN("negative file->_cnt value in %p\n", file
);
2988 file
->_flag
|= MSVCRT__IOERR
;
2993 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2994 memcpy(file
->_ptr
, ptr
, pcnt
);
2999 ptr
= (const char*)ptr
+ pcnt
;
3000 } else if((file
->_flag
& _IONBF
)
3001 || ((file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3002 || (!(file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3006 if(file
->_flag
& _IONBF
)
3008 else if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
)))
3009 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3011 bufsiz
= file
->_bufsiz
;
3013 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3015 if(msvcrt_flush_buffer(file
) == EOF
)
3018 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
3019 file
->_flag
|= _IOERR
;
3024 ptr
= (const char*)ptr
+ pcnt
;
3026 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3030 ptr
= (const char*)ptr
+ 1;
3035 return written
/ size
;
3038 /*********************************************************************
3040 * FORKED for ReactOS, don't sync with Wine!
3042 * - http://jira.reactos.org/browse/CORE-6495
3043 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3045 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3047 /* If this is a real file stream (and not some temporary one for
3048 sprintf-like functions), check whether it is opened in text mode.
3049 In this case, we have to perform an implicit conversion to ANSI. */
3050 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
3052 /* Convert to multibyte in text mode */
3053 char mbc
[MB_LEN_MAX
];
3056 mb_return
= wctomb(mbc
, c
);
3061 /* Output all characters */
3062 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3067 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3074 /*********************************************************************
3075 * _fputwchar (MSVCRT.@)
3077 wint_t CDECL
_fputwchar(wint_t wc
)
3079 return fputwc(wc
, stdout
);
3082 /*********************************************************************
3083 * _wfsopen (MSVCRT.@)
3085 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3088 int open_flags
, stream_flags
, fd
;
3090 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3092 /* map mode string to open() flags. "man fopen" for possibilities. */
3093 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3097 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3100 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3102 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3109 TRACE(":got (%p)\n",file
);
3110 if (fd
>= 0 && !file
)
3116 /*********************************************************************
3117 * _fsopen (MSVCRT.@)
3119 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3122 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3124 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3125 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3129 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3132 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3137 ret
= _wfsopen(pathW
, modeW
, share
);
3144 /*********************************************************************
3147 FILE * CDECL
fopen(const char *path
, const char *mode
)
3149 return _fsopen( path
, mode
, _SH_DENYNO
);
3152 /*********************************************************************
3153 * fopen_s (MSVCRT.@)
3155 int CDECL
fopen_s(FILE** pFile
,
3156 const char *filename
, const char *mode
)
3158 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
3159 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
3160 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
3162 *pFile
= fopen(filename
, mode
);
3169 /*********************************************************************
3170 * _wfopen (MSVCRT.@)
3172 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3174 return _wfsopen( path
, mode
, _SH_DENYNO
);
3177 /*********************************************************************
3178 * _wfopen_s (MSVCRT.@)
3180 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3181 const wchar_t *mode
)
3183 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3184 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3189 *pFile
= _wfopen(filename
, mode
);
3196 /* fputc calls _flsbuf which calls fputc */
3197 int CDECL
_flsbuf(int c
, FILE* file
);
3199 /*********************************************************************
3202 int CDECL
fputc(int c
, FILE* file
)
3212 res
= msvcrt_flush_buffer(file
);
3214 return res
? res
: c
;
3221 res
= _flsbuf(c
, file
);
3227 /*********************************************************************
3228 * _fputchar (MSVCRT.@)
3230 int CDECL
_fputchar(int c
)
3232 return fputc(c
, stdout
);
3235 /*********************************************************************
3238 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3240 size_t rcnt
=size
* nmemb
;
3249 /* first buffered data */
3251 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3252 memcpy(ptr
, file
->_ptr
, pcnt
);
3257 ptr
= (char*)ptr
+ pcnt
;
3258 } else if(!(file
->_flag
& _IOREAD
)) {
3259 if(file
->_flag
& _IORW
) {
3260 file
->_flag
|= _IOREAD
;
3267 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
3273 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& (file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
3274 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3275 file
->_ptr
= file
->_base
;
3276 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3277 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3278 if (i
> 0 && i
< file
->_cnt
) {
3279 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3280 file
->_flag
&= ~_IOEOF
;
3283 memcpy(ptr
, file
->_ptr
, i
);
3287 } else if (rcnt
> INT_MAX
) {
3288 i
= _read(file
->_file
, ptr
, INT_MAX
);
3289 } else if (rcnt
< BUFSIZ
) {
3290 i
= _read(file
->_file
, ptr
, rcnt
);
3292 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3296 ptr
= (char *)ptr
+i
;
3297 /* expose feof condition in the flags
3298 * MFC tests file->_flag for feof, and doesn't call feof())
3300 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3301 file
->_flag
|= _IOEOF
;
3304 file
->_flag
|= _IOERR
;
3315 /*********************************************************************
3316 * _wfreopen (MSVCRT.@)
3319 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3321 int open_flags
, stream_flags
, fd
;
3323 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3326 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3331 /* map mode string to open() flags. "man fopen" for possibilities. */
3332 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3336 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3339 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
3342 WARN(":failed-last error (%d)\n",GetLastError());
3343 _dosmaperr(GetLastError());
3352 /*********************************************************************
3353 * freopen (MSVCRT.@)
3356 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3359 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3361 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3362 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3368 ret
= _wfreopen(pathW
, modeW
, file
);
3375 /*********************************************************************
3376 * fsetpos (MSVCRT.@)
3378 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3383 /* Note that all this has been lifted 'as is' from fseek */
3384 if(file
->_flag
& _IOWRT
)
3385 msvcrt_flush_buffer(file
);
3387 /* Discard buffered input */
3389 file
->_ptr
= file
->_base
;
3391 /* Reset direction of i/o */
3392 if(file
->_flag
& _IORW
) {
3393 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3396 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3401 /*********************************************************************
3402 * _ftelli64 (MSVCRT.@)
3404 __int64 CDECL
_ftelli64(FILE* file
)
3409 pos
= _telli64(file
->_file
);
3414 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
3415 if(file
->_flag
& _IOWRT
) {
3416 pos
+= file
->_ptr
- file
->_base
;
3418 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3421 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3425 } else if(!file
->_cnt
) { /* nothing to do */
3426 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3430 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3431 for(i
=0; i
<file
->_cnt
; i
++)
3432 if(file
->_ptr
[i
] == '\n')
3438 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3443 pos
-= file
->_bufsiz
;
3444 pos
+= file
->_ptr
- file
->_base
;
3446 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3447 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3450 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3461 /*********************************************************************
3464 LONG CDECL
ftell(FILE* file
)
3466 return (LONG
)_ftelli64(file
);
3469 /*********************************************************************
3470 * fgetpos (MSVCRT.@)
3472 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3474 *pos
= _ftelli64(file
);
3480 /*********************************************************************
3483 int CDECL
fputs(const char *s
, FILE* file
)
3485 size_t len
= strlen(s
);
3489 ret
= fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
3494 /*********************************************************************
3497 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3499 size_t i
, len
= strlenW(s
);
3504 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3505 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3510 tmp_buf
= add_std_buffer(file
);
3511 for (i
=0; i
<len
; i
++) {
3512 if(fputwc(s
[i
], file
) == WEOF
) {
3513 if(tmp_buf
) remove_std_buffer(file
);
3519 if(tmp_buf
) remove_std_buffer(file
);
3524 /*********************************************************************
3525 * getchar (MSVCRT.@)
3527 int CDECL
getchar(void)
3529 return fgetc(stdin
);
3532 /*********************************************************************
3535 int CDECL
getc(FILE* file
)
3540 /*********************************************************************
3543 char * CDECL
gets(char *buf
)
3546 char * buf_start
= buf
;
3549 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3551 if(cc
!= '\r') *buf
++ = (char)cc
;
3555 TRACE("got '%s'\n", buf_start
);
3556 _unlock_file(stdin
);
3560 /*********************************************************************
3563 wchar_t* CDECL
_getws(wchar_t* buf
)
3569 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3573 *buf
++ = (wchar_t)cc
;
3577 TRACE("got %s\n", debugstr_w(ws
));
3578 _unlock_file(stdin
);
3582 /*********************************************************************
3585 int CDECL
putc(int c
, FILE* file
)
3587 return fputc(c
, file
);
3590 /*********************************************************************
3591 * putchar (MSVCRT.@)
3593 int CDECL
putchar(int c
)
3595 return fputc(c
, stdout
);
3598 /*********************************************************************
3599 * _putwch (MSVCRT.@)
3601 wint_t CDECL
_putwch(wchar_t c
)
3603 return fputwc(c
, stdout
);
3606 /*********************************************************************
3609 int CDECL
puts(const char *s
)
3611 size_t len
= strlen(s
);
3615 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3616 _unlock_file(stdout
);
3620 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3621 _unlock_file(stdout
);
3625 /*********************************************************************
3628 int CDECL
_putws(const wchar_t *s
)
3630 static const wchar_t nl
= '\n';
3631 size_t len
= strlenW(s
);
3635 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3636 _unlock_file(stdout
);
3640 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3641 _unlock_file(stdout
);
3645 /*********************************************************************
3648 int CDECL
remove(const char *path
)
3650 TRACE("(%s)\n",path
);
3651 if (DeleteFileA(path
))
3653 TRACE(":failed (%d)\n",GetLastError());
3654 _dosmaperr(GetLastError());
3658 /*********************************************************************
3659 * _wremove (MSVCRT.@)
3661 int CDECL
_wremove(const wchar_t *path
)
3663 TRACE("(%s)\n",debugstr_w(path
));
3664 if (DeleteFileW(path
))
3666 TRACE(":failed (%d)\n",GetLastError());
3667 _dosmaperr(GetLastError());
3671 /*********************************************************************
3674 int CDECL
rename(const char *oldpath
,const char *newpath
)
3676 TRACE(":from %s to %s\n",oldpath
,newpath
);
3677 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3679 TRACE(":failed (%d)\n",GetLastError());
3680 _dosmaperr(GetLastError());
3684 /*********************************************************************
3685 * _wrename (MSVCRT.@)
3687 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3689 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3690 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3692 TRACE(":failed (%d)\n",GetLastError());
3693 _dosmaperr(GetLastError());
3697 /*********************************************************************
3698 * setvbuf (MSVCRT.@)
3700 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3702 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
3703 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
3704 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
3709 if(file
->_flag
& _IOMYBUF
)
3711 file
->_flag
&= ~(_IONBF
| _IOMYBUF
| _USERBUF
);
3714 if(mode
== _IONBF
) {
3715 file
->_flag
|= _IONBF
;
3716 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
3719 file
->_base
= file
->_ptr
= buf
;
3720 file
->_flag
|= _USERBUF
;
3721 file
->_bufsiz
= size
;
3723 file
->_base
= file
->_ptr
= malloc(size
);
3730 file
->_flag
|= _IOMYBUF
;
3731 file
->_bufsiz
= size
;
3737 /*********************************************************************
3740 void CDECL
setbuf(FILE* file
, char *buf
)
3742 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3745 /*********************************************************************
3748 char * CDECL
tmpnam(char *s
)
3755 thread_data_t
*data
= msvcrt_get_thread_data();
3757 if(!data
->tmpnam_buffer
)
3758 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3760 s
= data
->tmpnam_buffer
;
3763 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3764 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3765 for (count
= 0; count
< TMP_MAX
; count
++)
3767 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
3768 memcpy(p
, tmpstr
, size
);
3770 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3771 GetLastError() == ERROR_FILE_NOT_FOUND
)
3777 /*********************************************************************
3778 * _wtmpnam (MSVCRT.@)
3780 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3782 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3787 thread_data_t
*data
= msvcrt_get_thread_data();
3789 if(!data
->wtmpnam_buffer
)
3790 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3792 s
= data
->wtmpnam_buffer
;
3795 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3796 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3797 for (count
= 0; count
< TMP_MAX
; count
++)
3799 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
3800 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3802 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3803 GetLastError() == ERROR_FILE_NOT_FOUND
)
3809 /*********************************************************************
3810 * tmpfile (MSVCRT.@)
3812 FILE* CDECL
tmpfile(void)
3814 char *filename
= tmpnam(NULL
);
3819 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3820 _S_IREAD
| _S_IWRITE
);
3821 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3823 if (msvcrt_init_fp(file
, fd
, _IORW
) == -1)
3828 else file
->_tmpfname
= _strdup(filename
);
3831 if(fd
!= -1 && !file
)
3837 /*********************************************************************
3840 int CDECL
ungetc(int c
, FILE * file
)
3842 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3844 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
3845 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
3849 if((!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
3850 && alloc_buffer(file
))
3851 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
3854 if(file
->_ptr
>file
->_base
) {
3856 if(file
->_flag
& _IOSTRG
) {
3857 if(*file
->_ptr
!= c
) {
3867 file
->_flag
|= _IOREAD
;
3876 /*********************************************************************
3877 * ungetwc (MSVCRT.@)
3879 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3888 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3889 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3890 unsigned char * pp
= (unsigned char *)&mwc
;
3893 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3894 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3900 char mbs
[MB_LEN_MAX
];
3903 len
= wctomb(mbs
, mwc
);
3909 for(len
--; len
>=0; len
--) {
3910 if(mbs
[len
] != ungetc(mbs
[len
], file
)) {
3923 /*********************************************************************
3924 * _getmaxstdio (MSVCRT.@)
3926 int CDECL
_getmaxstdio(void)
3928 return MSVCRT_max_streams
;
3931 /*********************************************************************
3932 * _setmaxstdio (MSVCRT.@)
3934 int CDECL
_setmaxstdio(int newmax
)
3936 TRACE("%d\n", newmax
);
3938 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
3941 MSVCRT_max_streams
= newmax
;
3942 return MSVCRT_max_streams
;