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(1, *size
);
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
& (_IOREAD
|_IOWRT
)) == _IOWRT
&&
522 file
->_flag
& (_IOMYBUF
|_USERBUF
)) {
523 int cnt
=file
->_ptr
-file
->_base
;
524 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
525 file
->_flag
|= _IOERR
;
529 if(file
->_flag
& _IORW
)
530 file
->_flag
&= ~_IOWRT
;
533 file
->_ptr
=file
->_base
;
538 /*********************************************************************
541 int CDECL
_isatty(int fd
)
543 TRACE(":fd (%d)\n",fd
);
545 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
548 /* INTERNAL: Allocate stdio file buffer */
549 /*static*/ BOOL
alloc_buffer(FILE* file
)
551 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
552 && _isatty(file
->_file
))
555 file
->_base
= calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
557 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
558 file
->_flag
|= _IOMYBUF
;
560 file
->_base
= (char*)(&file
->_charbuf
);
562 file
->_flag
|= _IONBF
;
564 file
->_ptr
= file
->_base
;
569 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
570 static BOOL
add_std_buffer(FILE *file
)
572 static char buffers
[2][BUFSIZ
];
574 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
575 || (file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
576 || !_isatty(file
->_file
))
579 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
580 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
581 file
->_flag
|= _USERBUF
;
585 /* INTERNAL: Removes temporary buffer from stdout or stderr */
586 /* Only call this function when add_std_buffer returned TRUE */
587 static void remove_std_buffer(FILE *file
)
589 msvcrt_flush_buffer(file
);
590 file
->_ptr
= file
->_base
= NULL
;
591 file
->_bufsiz
= file
->_cnt
= 0;
592 file
->_flag
&= ~_USERBUF
;
595 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
596 static int msvcrt_int_to_base32(int num
, char *str
)
611 *p
= (num
& 31) + '0';
613 *p
+= ('a' - '0' - 10);
620 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
621 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
636 *p
= (num
& 31) + '0';
638 *p
+= ('a' - '0' - 10);
645 /* INTERNAL: Create a wide string from an ascii string */
646 wchar_t *msvcrt_wstrdupa(const char *str
)
648 const unsigned int len
= strlen(str
) + 1 ;
649 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
652 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
656 /*********************************************************************
657 * __iob_func(MSVCRT.@)
659 FILE * CDECL
__iob_func(void)
664 /*********************************************************************
667 int CDECL
_access(const char *filename
, int mode
)
669 DWORD attr
= GetFileAttributesA(filename
);
671 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
673 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
675 _dosmaperr(GetLastError());
678 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
680 _set_errno(ERROR_ACCESS_DENIED
);
686 /*********************************************************************
687 * _access_s (MSVCRT.@)
689 int CDECL
_access_s(const char *filename
, int mode
)
691 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
692 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
698 return _access(filename
, mode
);
701 /*********************************************************************
702 * _waccess (MSVCRT.@)
704 int CDECL
_waccess(const wchar_t *filename
, int mode
)
706 DWORD attr
= GetFileAttributesW(filename
);
708 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
710 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
712 _dosmaperr(GetLastError());
715 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
717 _set_errno(ERROR_ACCESS_DENIED
);
723 /*********************************************************************
724 * _waccess_s (MSVCRT.@)
726 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
728 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
729 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
735 return _waccess(filename
, mode
);
738 /*********************************************************************
741 int CDECL
_chmod(const char *path
, int flags
)
743 DWORD oldFlags
= GetFileAttributesA(path
);
745 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
747 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
748 oldFlags
| FILE_ATTRIBUTE_READONLY
;
750 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
753 _dosmaperr(GetLastError());
757 /*********************************************************************
760 int CDECL
_wchmod(const wchar_t *path
, int flags
)
762 DWORD oldFlags
= GetFileAttributesW(path
);
764 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
766 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
767 oldFlags
| FILE_ATTRIBUTE_READONLY
;
769 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
772 _dosmaperr(GetLastError());
776 /*********************************************************************
779 int CDECL
_unlink(const char *path
)
781 TRACE("%s\n",debugstr_a(path
));
782 if(DeleteFileA(path
))
784 TRACE("failed (%d)\n",GetLastError());
785 _dosmaperr(GetLastError());
789 /*********************************************************************
790 * _wunlink (MSVCRT.@)
792 int CDECL
_wunlink(const wchar_t *path
)
794 TRACE("(%s)\n",debugstr_w(path
));
795 if(DeleteFileW(path
))
797 TRACE("failed (%d)\n",GetLastError());
798 _dosmaperr(GetLastError());
802 /*********************************************************************
805 int CDECL
_commit(int fd
)
807 HANDLE hand
= fdtoh(fd
);
809 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
810 if (hand
== INVALID_HANDLE_VALUE
)
813 if (!FlushFileBuffers(hand
))
815 if (GetLastError() == ERROR_INVALID_HANDLE
)
817 /* FlushFileBuffers fails for console handles
818 * so we ignore this error.
822 TRACE(":failed-last error (%d)\n",GetLastError());
823 _dosmaperr(GetLastError());
830 /* _flushall calls fflush which calls _flushall */
831 int CDECL
fflush(FILE* file
);
833 /* INTERNAL: Flush all stream buffer */
834 static int msvcrt_flush_all_buffers(int mask
)
836 int i
, num_flushed
= 0;
840 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
841 file
= msvcrt_get_file(i
);
845 if(file
->_flag
& mask
) {
853 TRACE(":flushed (%d) handles\n",num_flushed
);
857 /*********************************************************************
858 * _flushall (MSVCRT.@)
860 int CDECL
_flushall(void)
862 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
865 /*********************************************************************
868 int CDECL
fflush(FILE* file
)
871 msvcrt_flush_all_buffers(_IOWRT
);
872 } else if(file
->_flag
& _IOWRT
) {
876 res
= msvcrt_flush_buffer(file
);
878 if(!res && (file->_flag & _IOCOMMIT))
879 res = _commit(file->_file) ? EOF : 0;
884 } else if(file
->_flag
& _IOREAD
) {
887 file
->_ptr
= file
->_base
;
895 /*********************************************************************
898 int CDECL
_close(int fd
)
905 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
906 if (hand
== INVALID_HANDLE_VALUE
)
908 else if (!CloseHandle(hand
))
910 WARN(":failed-last error (%d)\n",GetLastError());
911 _dosmaperr(GetLastError());
924 /*********************************************************************
927 * MSDN isn't clear on this point, but the remarks for _pipe
928 * indicate file descriptors duplicated with _dup and _dup2 are always
931 int CDECL
_dup2(int od
, int nd
)
935 TRACE("(od=%d, nd=%d)\n", od
, nd
);
937 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
941 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
942 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
944 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
948 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
956 /* _dup2 returns 0, not nd, on success */
963 _dosmaperr(GetLastError());
975 /*********************************************************************
978 int CDECL
_dup(int od
)
984 if (_dup2(od
, fd
) == 0)
992 /*********************************************************************
995 int CDECL
_eof(int fd
)
998 LONG hcurpos
,hendpos
;
999 HANDLE hand
= fdtoh(fd
);
1001 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1003 if (hand
== INVALID_HANDLE_VALUE
)
1006 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1008 /* Otherwise we do it the hard way */
1009 hcurpos
= hendpos
= 0;
1010 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1011 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1013 if (curpos
== endpos
&& hcurpos
== hendpos
)
1015 /* FIXME: shouldn't WX_ATEOF be set here? */
1019 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1023 /*********************************************************************
1024 * _fcloseall (MSVCRT.@)
1026 int CDECL
_fcloseall(void)
1028 int num_closed
= 0, i
;
1032 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1033 file
= msvcrt_get_file(i
);
1035 if (file
->_flag
&& !fclose(file
))
1040 TRACE(":closed (%d) handles\n",num_closed
);
1044 /* free everything on process exit */
1045 void msvcrt_free_io(void)
1053 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1056 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1058 FILE *file
= msvcrt_get_file(j
);
1059 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1061 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1062 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1066 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1067 free(MSVCRT_fstream
[i
]);
1070 /*********************************************************************
1071 * _lseeki64 (MSVCRT.@)
1073 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1075 HANDLE hand
= fdtoh(fd
);
1078 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1079 if (hand
== INVALID_HANDLE_VALUE
)
1082 if (whence
< 0 || whence
> 2)
1088 TRACE(":fd (%d) to %s pos %s\n",
1089 fd
,wine_dbgstr_longlong(offset
),
1090 (whence
==SEEK_SET
)?"SEEK_SET":
1091 (whence
==SEEK_CUR
)?"SEEK_CUR":
1092 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1094 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1095 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1096 ofs
.QuadPart
= offset
;
1097 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1098 GetLastError() == ERROR_SUCCESS
)
1100 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1101 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1103 return ofs
.QuadPart
;
1105 TRACE(":error-last error (%d)\n",GetLastError());
1106 _dosmaperr(GetLastError());
1110 /*********************************************************************
1113 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1115 return (LONG
)_lseeki64(fd
, offset
, whence
);
1118 /*********************************************************************
1119 * _lock_file (MSVCRT.@)
1121 void CDECL
_lock_file(FILE *file
)
1123 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1124 _lock(_STREAM_LOCKS
+(file
-_iob
));
1125 /* ReactOS: string streams dont need to be locked */
1126 else if(!(file
->_flag
& _IOSTRG
))
1127 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1130 /*********************************************************************
1131 * _unlock_file (MSVCRT.@)
1133 void CDECL
_unlock_file(FILE *file
)
1135 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1136 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1137 /* ReactOS: string streams dont need to be locked */
1138 else if(!(file
->_flag
& _IOSTRG
))
1139 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1143 /*********************************************************************
1144 * _locking (MSVCRT.@)
1146 * This is untested; the underlying LockFile doesn't work yet.
1148 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1152 HANDLE hand
= fdtoh(fd
);
1154 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1155 if (hand
== INVALID_HANDLE_VALUE
)
1158 if (mode
< 0 || mode
> 4)
1164 TRACE(":fd (%d) by 0x%08x mode %s\n",
1165 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1166 (mode
==_LK_LOCK
)?"_LK_LOCK":
1167 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1168 (mode
==_LK_RLCK
)?"_LK_RLCK":
1169 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1172 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1174 FIXME ("Seek failed\n");
1175 *_errno() = EINVAL
; /* FIXME */
1178 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1181 ret
= 1; /* just to satisfy gcc */
1184 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1189 else if (mode
== _LK_UNLCK
)
1190 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1192 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1193 /* FIXME - what about error settings? */
1194 return ret
? 0 : -1;
1197 /*********************************************************************
1198 * _fseeki64 (MSVCRT.@)
1200 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1205 /* Flush output if needed */
1206 if(file
->_flag
& _IOWRT
)
1207 msvcrt_flush_buffer(file
);
1209 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1211 offset
+= _ftelli64(file
);
1214 /* Discard buffered input */
1216 file
->_ptr
= file
->_base
;
1217 /* Reset direction of i/o */
1218 if(file
->_flag
& _IORW
) {
1219 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1221 /* Clear end of file flag */
1222 file
->_flag
&= ~_IOEOF
;
1223 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1229 /*********************************************************************
1232 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1234 return _fseeki64( file
, offset
, whence
);
1237 /*********************************************************************
1238 * _chsize_s (MSVCRT.@)
1240 int CDECL
_chsize_s(int fd
, __int64 size
)
1246 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1248 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1253 if (handle
!= INVALID_HANDLE_VALUE
)
1255 /* save the current file pointer */
1256 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1259 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1262 ret
= SetEndOfFile(handle
);
1263 if (!ret
) _dosmaperr(GetLastError());
1266 /* restore the file pointer */
1267 _lseeki64(fd
, cur
, SEEK_SET
);
1272 return ret
? 0 : *_errno();
1275 /*********************************************************************
1276 * _chsize (MSVCRT.@)
1278 int CDECL
_chsize(int fd
, long size
)
1280 /* _chsize_s returns errno on failure but _chsize should return -1 */
1281 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1284 /*********************************************************************
1285 * clearerr (MSVCRT.@)
1287 void CDECL
clearerr(FILE* file
)
1289 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1292 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1296 /*********************************************************************
1299 void CDECL
rewind(FILE* file
)
1301 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1304 fseek(file
, 0L, SEEK_SET
);
1309 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1311 int plus
= strchrW(mode
, '+') != NULL
;
1313 TRACE("%s\n", debugstr_w(mode
));
1315 while(*mode
== ' ') mode
++;
1320 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1321 *stream_flags
= plus
? _IORW
: _IOREAD
;
1324 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1325 *stream_flags
= plus
? _IORW
: _IOWRT
;
1328 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1329 *stream_flags
= plus
? _IORW
: _IOWRT
;
1332 MSVCRT_INVALID_PMT(0, EINVAL
);
1336 *stream_flags
|= _commode
;
1338 while (*mode
&& *mode
!=',')
1342 *open_flags
|= _O_BINARY
;
1343 *open_flags
&= ~_O_TEXT
;
1346 *open_flags
|= _O_TEXT
;
1347 *open_flags
&= ~_O_BINARY
;
1350 *open_flags
|= _O_TEMPORARY
;
1353 *open_flags
|= _O_SHORT_LIVED
;
1356 *stream_flags
|= _IOCOMMIT
;
1359 *stream_flags
&= ~_IOCOMMIT
;
1362 *open_flags
|= _O_NOINHERIT
;
1371 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1374 ERR("incorrect mode flag: %c\n", mode
[-1]);
1380 static const WCHAR ccs
[] = {'c','c','s'};
1381 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1382 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1383 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1386 while(*mode
== ' ') mode
++;
1387 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1389 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1390 while(*mode
== ' ') mode
++;
1391 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1394 while(*mode
== ' ') mode
++;
1396 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1398 *open_flags
|= _O_U8TEXT
;
1399 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1401 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1403 *open_flags
|= _O_U16TEXT
;
1404 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1406 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1408 *open_flags
|= _O_WTEXT
;
1409 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1413 MSVCRT_INVALID_PMT(0, EINVAL
);
1417 while(*mode
== ' ') mode
++;
1420 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1425 /*********************************************************************
1426 * _fdopen (MSVCRT.@)
1428 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1431 wchar_t *modeW
= NULL
;
1433 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1435 ret
= _wfdopen(fd
, modeW
);
1441 /*********************************************************************
1442 * _wfdopen (MSVCRT.@)
1444 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1446 int open_flags
, stream_flags
;
1449 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1452 if (!(file
= msvcrt_alloc_fp()))
1454 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1459 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1465 /*********************************************************************
1466 * _filelength (MSVCRT.@)
1468 LONG CDECL
_filelength(int fd
)
1470 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1473 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1476 if (endPos
!= curPos
)
1477 _lseek(fd
, curPos
, SEEK_SET
);
1484 /*********************************************************************
1485 * _filelengthi64 (MSVCRT.@)
1487 __int64 CDECL
_filelengthi64(int fd
)
1489 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1492 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1495 if (endPos
!= curPos
)
1496 _lseeki64(fd
, curPos
, SEEK_SET
);
1503 /*********************************************************************
1504 * _fileno (MSVCRT.@)
1506 int CDECL
_fileno(FILE* file
)
1508 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1512 /*********************************************************************
1513 * _get_osfhandle (MSVCRT.@)
1515 intptr_t CDECL
_get_osfhandle(int fd
)
1517 HANDLE hand
= fdtoh(fd
);
1518 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1520 return (intptr_t)hand
;
1523 /*********************************************************************
1524 * _mktemp (MSVCRT.@)
1526 char * CDECL
_mktemp(char *pattern
)
1529 char *retVal
= pattern
;
1537 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1541 id
= GetCurrentProcessId();
1545 int tempNum
= id
/ 10;
1546 *pattern
-- = id
- (tempNum
* 10) + '0';
1552 *pattern
= letter
++;
1553 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1555 } while(letter
<= 'z');
1559 /*********************************************************************
1560 * _wmktemp (MSVCRT.@)
1562 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1565 wchar_t *retVal
= pattern
;
1567 wchar_t letter
= 'a';
1570 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1574 id
= GetCurrentProcessId();
1578 int tempNum
= id
/ 10;
1579 *pattern
-- = id
- (tempNum
* 10) + '0';
1585 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1586 GetLastError() == ERROR_FILE_NOT_FOUND
)
1588 *pattern
= letter
++;
1589 } while(letter
!= '|');
1593 /*static*/ unsigned split_oflags(unsigned oflags
)
1596 unsigned unsupp
; /* until we support everything */
1598 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1599 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1600 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1601 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1602 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1603 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1604 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1605 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1606 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1608 if ((unsupp
= oflags
& ~(
1609 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1610 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1611 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1613 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1614 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1616 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1621 /*********************************************************************
1624 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1627 SECURITY_ATTRIBUTES sa
;
1628 HANDLE readHandle
, writeHandle
;
1636 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1637 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1638 sa
.lpSecurityDescriptor
= NULL
;
1639 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1641 unsigned int wxflags
= split_oflags(textmode
);
1645 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1649 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1658 CloseHandle(writeHandle
);
1664 CloseHandle(readHandle
);
1665 CloseHandle(writeHandle
);
1671 _dosmaperr(GetLastError());
1676 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1678 char bom
[sizeof(utf8_bom
)];
1681 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1683 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1686 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1687 oflags
|= _O_U8TEXT
;
1688 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1690 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1691 oflags
|= _O_U16TEXT
;
1693 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1699 /*********************************************************************
1700 * _wsopen_s (MSVCRT.@)
1702 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1704 DWORD access
= 0, creation
= 0, attrib
;
1705 SECURITY_ATTRIBUTES sa
;
1706 DWORD sharing
, type
;
1710 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1711 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1713 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1716 wxflag
= split_oflags(oflags
);
1717 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1719 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1720 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1721 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1724 if (oflags
& _O_CREAT
)
1726 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1727 FIXME(": pmode 0x%04x ignored\n", pmode
);
1729 WARN(": pmode 0x%04x ignored\n", pmode
);
1731 if (oflags
& _O_EXCL
)
1732 creation
= CREATE_NEW
;
1733 else if (oflags
& _O_TRUNC
)
1734 creation
= CREATE_ALWAYS
;
1736 creation
= OPEN_ALWAYS
;
1738 else /* no _O_CREAT */
1740 if (oflags
& _O_TRUNC
)
1741 creation
= TRUNCATE_EXISTING
;
1743 creation
= OPEN_EXISTING
;
1752 sharing
= FILE_SHARE_READ
;
1755 sharing
= FILE_SHARE_WRITE
;
1758 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1761 ERR( "Unhandled shflags 0x%x\n", shflags
);
1764 attrib
= FILE_ATTRIBUTE_NORMAL
;
1766 if (oflags
& _O_TEMPORARY
)
1768 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1770 sharing
|= FILE_SHARE_DELETE
;
1773 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1774 sa
.lpSecurityDescriptor
= NULL
;
1775 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1777 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1778 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1779 && !(access
&GENERIC_READ
))
1781 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1782 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1783 if (hand
!= INVALID_HANDLE_VALUE
)
1785 oflags
= check_bom(hand
, oflags
, FALSE
);
1789 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1792 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1793 if (hand
== INVALID_HANDLE_VALUE
) {
1794 WARN(":failed-last error (%d)\n",GetLastError());
1795 _dosmaperr(GetLastError());
1799 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1801 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1802 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1803 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1805 if (oflags
& _O_U8TEXT
)
1807 DWORD written
= 0, tmp
;
1809 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1810 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1812 if (written
!= sizeof(utf8_bom
)) {
1813 WARN("error writing BOM\n");
1815 _dosmaperr(GetLastError());
1821 DWORD written
= 0, tmp
;
1823 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1824 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1826 if (written
!= sizeof(utf16_bom
))
1828 WARN("error writing BOM\n");
1830 _dosmaperr(GetLastError());
1835 else if (access
& GENERIC_READ
)
1836 oflags
= check_bom(hand
, oflags
, TRUE
);
1838 type
= GetFileType(hand
);
1839 if (type
== FILE_TYPE_CHAR
)
1841 else if (type
== FILE_TYPE_PIPE
)
1844 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1848 if (oflags
& _O_WTEXT
)
1849 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1850 else if (oflags
& _O_U16TEXT
)
1851 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1852 else if (oflags
& _O_U8TEXT
)
1853 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1855 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1859 /*********************************************************************
1860 * _wsopen (MSVCRT.@)
1862 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1867 if (oflags
& _O_CREAT
)
1871 __ms_va_start(ap
, shflags
);
1872 pmode
= va_arg(ap
, int);
1878 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1882 /*********************************************************************
1883 * _sopen_s (MSVCRT.@)
1885 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1890 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1893 ret
= _wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1898 /*********************************************************************
1901 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1906 if (oflags
& _O_CREAT
)
1910 va_start(ap
, shflags
);
1911 pmode
= va_arg(ap
, int);
1917 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1921 /*********************************************************************
1924 int CDECL
_open( const char *path
, int flags
, ... )
1928 if (flags
& _O_CREAT
)
1931 va_start(ap
, flags
);
1932 pmode
= va_arg(ap
, int);
1934 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1937 return _sopen( path
, flags
, _SH_DENYNO
);
1940 /*********************************************************************
1943 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1947 if (flags
& _O_CREAT
)
1950 va_start(ap
, flags
);
1951 pmode
= va_arg(ap
, int);
1953 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1956 return _wsopen( path
, flags
, _SH_DENYNO
);
1959 /*********************************************************************
1962 int CDECL
_creat(const char *path
, int flags
)
1964 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1965 return _open(path
, usedFlags
);
1968 /*********************************************************************
1969 * _wcreat (MSVCRT.@)
1971 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1973 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1974 return _wopen(path
, usedFlags
);
1977 /*********************************************************************
1978 * _open_osfhandle (MSVCRT.@)
1980 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1985 /* _O_RDONLY (0) always matches, so set the read flag
1986 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1987 * file, so set the write flag. It also only sets _O_TEXT if it wants
1988 * text - it never sets _O_BINARY.
1990 /* don't let split_oflags() decide the mode if no mode is passed */
1991 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1992 oflags
|= _O_BINARY
;
1994 flags
= GetFileType((HANDLE
)handle
);
1995 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
1997 _dosmaperr(GetLastError());
2001 if (flags
== FILE_TYPE_CHAR
)
2003 else if (flags
== FILE_TYPE_PIPE
)
2007 flags
|= split_oflags(oflags
);
2009 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2010 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2014 /*********************************************************************
2017 int CDECL
_rmtmp(void)
2019 int num_removed
= 0, i
;
2023 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2024 file
= msvcrt_get_file(i
);
2026 if (file
->_tmpfname
)
2035 TRACE(":removed (%d) temp files\n",num_removed
);
2039 static inline int get_utf8_char_len(char ch
)
2041 if((ch
&0xf8) == 0xf0)
2043 else if((ch
&0xf0) == 0xe0)
2045 else if((ch
&0xe0) == 0xc0)
2050 /*********************************************************************
2051 * (internal) read_utf8
2053 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2055 ioinfo
*fdinfo
= get_ioinfo(fd
);
2056 HANDLE hand
= fdinfo
->handle
;
2057 char min_buf
[4], *readbuf
, lookahead
;
2058 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2060 /* make the buffer big enough to hold at least one character */
2061 /* read bytes have to fit to output and lookahead buffers */
2063 readbuf_size
= count
< 4 ? 4 : count
;
2064 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2069 if(fdinfo
->lookahead
[0] != '\n') {
2070 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2071 fdinfo
->lookahead
[0] = '\n';
2073 if(fdinfo
->lookahead
[1] != '\n') {
2074 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2075 fdinfo
->lookahead
[1] = '\n';
2077 if(fdinfo
->lookahead
[2] != '\n') {
2078 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2079 fdinfo
->lookahead
[2] = '\n';
2084 /* NOTE: this case is broken in native dll, reading
2085 * sometimes fails when small buffer is passed
2088 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2089 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2090 fdinfo
->wxflag
|= WX_ATEOF
;
2093 _dosmaperr(GetLastError());
2096 }else if(!num_read
) {
2097 fdinfo
->wxflag
|= WX_ATEOF
;
2103 char_len
= get_utf8_char_len(readbuf
[0]);
2105 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2109 if(readbuf
[0] == '\n')
2110 fdinfo
->wxflag
|= WX_READNL
;
2112 fdinfo
->wxflag
&= ~WX_READNL
;
2114 if(readbuf
[0] == 0x1a) {
2115 fdinfo
->wxflag
|= WX_ATEOF
;
2119 if(readbuf
[0] == '\r') {
2120 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2122 else if(lookahead
== '\n')
2126 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2127 fdinfo
->lookahead
[0] = lookahead
;
2129 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2134 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2135 _dosmaperr(GetLastError());
2142 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2145 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2146 fdinfo
->wxflag
|= WX_ATEOF
;
2147 if (readbuf
!= min_buf
) free(readbuf
);
2150 _dosmaperr(GetLastError());
2151 if (readbuf
!= min_buf
) free(readbuf
);
2154 }else if(!pos
&& !num_read
) {
2155 fdinfo
->wxflag
|= WX_ATEOF
;
2156 if (readbuf
!= min_buf
) free(readbuf
);
2161 if(readbuf
[0] == '\n')
2162 fdinfo
->wxflag
|= WX_READNL
;
2164 fdinfo
->wxflag
&= ~WX_READNL
;
2166 /* Find first byte of last character (may be incomplete) */
2167 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2168 if((readbuf
[i
]&0xc0) != 0x80)
2170 char_len
= get_utf8_char_len(readbuf
[i
]);
2171 if(char_len
+i
<= pos
)
2174 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2176 fdinfo
->lookahead
[0] = readbuf
[i
];
2178 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2180 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2182 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2186 for(i
=0, j
=0; i
<pos
; i
++) {
2187 if(readbuf
[i
] == 0x1a) {
2188 fdinfo
->wxflag
|= WX_ATEOF
;
2192 /* strip '\r' if followed by '\n' */
2193 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2194 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2195 readbuf
[j
++] = '\r';
2196 }else if(lookahead
== '\n' && j
==0) {
2197 readbuf
[j
++] = '\n';
2199 if(lookahead
!= '\n')
2200 readbuf
[j
++] = '\r';
2202 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2203 fdinfo
->lookahead
[0] = lookahead
;
2205 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2207 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2208 readbuf
[j
++] = readbuf
[i
];
2213 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2214 _dosmaperr(GetLastError());
2215 if (readbuf
!= min_buf
) free(readbuf
);
2219 if (readbuf
!= min_buf
) free(readbuf
);
2223 /*********************************************************************
2226 * When reading \r as last character in text mode, read() positions
2227 * the file pointer on the \r character while getc() goes on to
2230 static int read_i(int fd
, void *buf
, unsigned int count
)
2232 DWORD num_read
, utf16
;
2233 char *bufstart
= buf
;
2234 HANDLE hand
= fdtoh(fd
);
2235 ioinfo
*fdinfo
= get_ioinfo(fd
);
2240 if (fdinfo
->wxflag
& WX_ATEOF
) {
2241 TRACE("already at EOF, returning 0\n");
2244 /* Don't trace small reads, it gets *very* annoying */
2246 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2247 if (hand
== INVALID_HANDLE_VALUE
)
2253 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2254 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2260 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2261 return read_utf8(fd
, buf
, count
);
2263 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2265 if (fdinfo
->lookahead
[0] != '\n')
2267 bufstart
[0] = fdinfo
->lookahead
[0];
2268 fdinfo
->lookahead
[0] = '\n';
2272 bufstart
[1] = fdinfo
->lookahead
[1];
2273 fdinfo
->lookahead
[1] = '\n';
2276 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2277 num_read
+= 1+utf16
;
2282 if(utf16
&& (num_read
&1))
2284 /* msvcr90 uses uninitialized value from the buffer in this case */
2285 /* msvcrt ignores additional data */
2286 ERR("got odd number of bytes in UTF16 mode\n");
2290 if (count
!= 0 && num_read
== 0)
2292 fdinfo
->wxflag
|= WX_ATEOF
;
2293 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2295 else if (fdinfo
->wxflag
& WX_TEXT
)
2299 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2300 fdinfo
->wxflag
|= WX_READNL
;
2302 fdinfo
->wxflag
&= ~WX_READNL
;
2304 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2306 /* in text mode, a ctrl-z signals EOF */
2307 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2309 fdinfo
->wxflag
|= WX_ATEOF
;
2310 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2314 /* in text mode, strip \r if followed by \n */
2315 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2320 lookahead
[1] = '\n';
2321 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2323 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2325 bufstart
[j
++] = '\n';
2326 if(utf16
) bufstart
[j
++] = 0;
2330 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2332 bufstart
[j
++] = '\r';
2333 if(utf16
) bufstart
[j
++] = 0;
2336 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2338 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2340 bufstart
[j
++] = '\n';
2341 if (utf16
) bufstart
[j
++] = 0;
2345 fdinfo
->lookahead
[0] = lookahead
[0];
2346 fdinfo
->lookahead
[1] = lookahead
[1];
2350 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2355 bufstart
[j
++] = '\r';
2356 if(utf16
) bufstart
[j
++] = 0;
2359 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2360 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2362 bufstart
[j
++] = bufstart
[i
];
2363 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2371 if (GetLastError() == ERROR_BROKEN_PIPE
)
2373 TRACE(":end-of-pipe\n");
2374 fdinfo
->wxflag
|= WX_ATEOF
;
2379 TRACE(":failed-last error (%d)\n",GetLastError());
2385 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2389 /*********************************************************************
2392 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2395 num_read
= read_i(fd
, buf
, count
);
2399 /*********************************************************************
2400 * _setmode (MSVCRT.@)
2402 int CDECL
_setmode(int fd
,int mode
)
2404 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2405 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2408 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2409 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2414 if(mode
== _O_BINARY
) {
2415 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2416 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2420 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2422 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2423 else if(mode
== _O_U8TEXT
)
2424 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2426 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2432 /*********************************************************************
2435 long CDECL
_tell(int fd
)
2437 return _lseek(fd
, 0, SEEK_CUR
);
2440 /*********************************************************************
2441 * _telli64 (MSVCRT.@)
2443 __int64 CDECL
_telli64(int fd
)
2445 return _lseeki64(fd
, 0, SEEK_CUR
);
2448 /*********************************************************************
2449 * _tempnam (MSVCRT.@)
2451 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2453 char tmpbuf
[MAX_PATH
];
2454 const char *tmp_dir
= getenv("TMP");
2456 if (tmp_dir
) dir
= tmp_dir
;
2458 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2459 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2461 TRACE("got name (%s)\n",tmpbuf
);
2462 DeleteFileA(tmpbuf
);
2463 return _strdup(tmpbuf
);
2465 TRACE("failed (%d)\n",GetLastError());
2469 /*********************************************************************
2470 * _wtempnam (MSVCRT.@)
2472 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2474 wchar_t tmpbuf
[MAX_PATH
];
2476 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2477 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2479 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2480 DeleteFileW(tmpbuf
);
2481 return _wcsdup(tmpbuf
);
2483 TRACE("failed (%d)\n",GetLastError());
2487 /*********************************************************************
2490 int CDECL
_umask(int umask
)
2492 int old_umask
= MSVCRT_umask
;
2493 TRACE("(%d)\n",umask
);
2494 MSVCRT_umask
= umask
;
2498 /*********************************************************************
2501 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2504 ioinfo
*info
= get_ioinfo(fd
);
2505 HANDLE hand
= info
->handle
;
2507 /* Don't trace small writes, it gets *very* annoying */
2510 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2512 if (hand
== INVALID_HANDLE_VALUE
)
2518 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2524 /* If appending, go to EOF */
2525 if (info
->wxflag
& WX_APPEND
)
2526 _lseek(fd
, 0, FILE_END
);
2528 if (!(info
->wxflag
& WX_TEXT
))
2530 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2531 && (num_written
== count
))
2533 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2534 hand
, GetLastError());
2539 unsigned int i
, j
, nr_lf
, size
;
2542 const char *s
= buf
, *buf_start
= buf
;
2544 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2546 /* find number of \n */
2547 for (nr_lf
=0, i
=0; i
<count
; i
++)
2553 if ((q
= p
= malloc(size
)))
2555 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2564 FIXME("Malloc failed\n");
2576 else if (info
->exflag
& EF_UTF16
)
2578 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2579 if (s
[i
]=='\n' && s
[i
+1]==0)
2584 if ((q
= p
= malloc(size
)))
2586 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2588 if (s
[i
]=='\n' && s
[i
+1]==0)
2599 FIXME("Malloc failed\n");
2615 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2616 if (s
[i
]=='\n' && s
[i
+1]==0)
2619 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2621 _dosmaperr(GetLastError());
2626 size
= conv_len
+nr_lf
;
2627 if((p
= malloc(count
+nr_lf
*2+size
)))
2629 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2631 if (s
[i
]=='\n' && s
[i
+1]==0)
2639 q
= p
+count
+nr_lf
*2;
2640 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2641 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2645 FIXME("Malloc failed\n");
2652 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2656 if (num_written
!= size
)
2658 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2659 fd
, hand
, GetLastError(), num_written
);
2661 return s
- buf_start
;
2669 /*********************************************************************
2672 int CDECL
_putw(int val
, FILE* file
)
2677 len
= _write(file
->_file
, &val
, sizeof(val
));
2678 if (len
== sizeof(val
)) {
2683 file
->_flag
|= _IOERR
;
2688 /*********************************************************************
2691 int CDECL
fclose(FILE* file
)
2697 free(file
->_tmpfname
);
2698 file
->_tmpfname
= NULL
;
2699 /* flush stdio buffers */
2700 if(file
->_flag
& _IOWRT
)
2702 if(file
->_flag
& _IOMYBUF
)
2705 r
=_close(file
->_file
);
2709 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2710 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2712 if(file
== msvcrt_get_file(MSVCRT_stream_idx
-1)) {
2713 while(MSVCRT_stream_idx
>3 && !file
->_flag
) {
2714 MSVCRT_stream_idx
--;
2715 file
= msvcrt_get_file(MSVCRT_stream_idx
-1);
2719 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2722 /*********************************************************************
2725 int CDECL
feof(FILE* file
)
2727 return file
->_flag
& _IOEOF
;
2730 /*********************************************************************
2733 int CDECL
ferror(FILE* file
)
2735 return file
->_flag
& _IOERR
;
2738 /*********************************************************************
2739 * _filbuf (MSVCRT.@)
2741 int CDECL
_filbuf(FILE* file
)
2746 if(file
->_flag
& _IOSTRG
) {
2751 /* Allocate buffer if needed */
2752 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
2755 if(!(file
->_flag
& _IOREAD
)) {
2756 if(file
->_flag
& _IORW
)
2757 file
->_flag
|= _IOREAD
;
2764 if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
2766 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2767 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2775 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2777 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2784 file
->_ptr
= file
->_base
+1;
2785 c
= *(unsigned char *)file
->_base
;
2791 /*********************************************************************
2794 int CDECL
fgetc(FILE* file
)
2802 i
= (unsigned char *)file
->_ptr
++;
2811 /*********************************************************************
2812 * _fgetchar (MSVCRT.@)
2814 int CDECL
_fgetchar(void)
2816 return fgetc(stdin
);
2819 /*********************************************************************
2822 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2825 char * buf_start
= s
;
2827 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2828 file
,file
->_file
,s
,size
);
2832 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2837 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2839 TRACE(":nothing read\n");
2843 if ((cc
!= EOF
) && (size
> 1))
2846 TRACE(":got %s\n", debugstr_a(buf_start
));
2851 /*********************************************************************
2854 wint_t CDECL
fgetwc(FILE* file
)
2861 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2862 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2865 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2874 char mbs
[MB_LEN_MAX
];
2880 if(isleadbyte((unsigned char)mbs
[0])) {
2891 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2899 /*********************************************************************
2902 int CDECL
_getw(FILE* file
)
2910 for (j
=0; j
<sizeof(int); j
++) {
2913 file
->_flag
|= _IOEOF
;
2924 /*********************************************************************
2927 wint_t CDECL
getwc(FILE* file
)
2929 return fgetwc(file
);
2932 /*********************************************************************
2933 * _fgetwchar (MSVCRT.@)
2935 wint_t CDECL
_fgetwchar(void)
2937 return fgetwc(stdin
);
2940 /*********************************************************************
2941 * getwchar (MSVCRT.@)
2943 wint_t CDECL
getwchar(void)
2945 return _fgetwchar();
2948 /*********************************************************************
2951 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2954 wchar_t * buf_start
= s
;
2956 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2957 file
,file
->_file
,s
,size
);
2961 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2966 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2968 TRACE(":nothing read\n");
2972 if ((cc
!= WEOF
) && (size
> 1))
2975 TRACE(":got %s\n", debugstr_w(buf_start
));
2980 /*********************************************************************
2983 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2985 size_t wrcnt
=size
* nmemb
;
2994 if(file
->_cnt
< 0) {
2995 WARN("negative file->_cnt value in %p\n", file
);
2996 file
->_flag
|= MSVCRT__IOERR
;
3001 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3002 memcpy(file
->_ptr
, ptr
, pcnt
);
3007 ptr
= (const char*)ptr
+ pcnt
;
3008 } else if((file
->_flag
& _IONBF
)
3009 || ((file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3010 || (!(file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3014 if(file
->_flag
& _IONBF
)
3016 else if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
)))
3017 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3019 bufsiz
= file
->_bufsiz
;
3021 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3023 if(msvcrt_flush_buffer(file
) == EOF
)
3026 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
3027 file
->_flag
|= _IOERR
;
3032 ptr
= (const char*)ptr
+ pcnt
;
3034 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3038 ptr
= (const char*)ptr
+ 1;
3043 return written
/ size
;
3046 /*********************************************************************
3048 * FORKED for ReactOS, don't sync with Wine!
3050 * - http://jira.reactos.org/browse/CORE-6495
3051 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3053 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3055 /* If this is a real file stream (and not some temporary one for
3056 sprintf-like functions), check whether it is opened in text mode.
3057 In this case, we have to perform an implicit conversion to ANSI. */
3058 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
3060 /* Convert to multibyte in text mode */
3061 char mbc
[MB_LEN_MAX
];
3064 mb_return
= wctomb(mbc
, c
);
3069 /* Output all characters */
3070 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3075 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3082 /*********************************************************************
3083 * _fputwchar (MSVCRT.@)
3085 wint_t CDECL
_fputwchar(wint_t wc
)
3087 return fputwc(wc
, stdout
);
3090 /*********************************************************************
3091 * _wfsopen (MSVCRT.@)
3093 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3096 int open_flags
, stream_flags
, fd
;
3098 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3100 /* map mode string to open() flags. "man fopen" for possibilities. */
3101 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3105 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3108 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3110 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3117 TRACE(":got (%p)\n",file
);
3118 if (fd
>= 0 && !file
)
3124 /*********************************************************************
3125 * _fsopen (MSVCRT.@)
3127 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3130 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3132 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3133 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3137 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3140 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3145 ret
= _wfsopen(pathW
, modeW
, share
);
3152 /*********************************************************************
3155 FILE * CDECL
fopen(const char *path
, const char *mode
)
3157 return _fsopen( path
, mode
, _SH_DENYNO
);
3160 /*********************************************************************
3161 * fopen_s (MSVCRT.@)
3163 int CDECL
fopen_s(FILE** pFile
,
3164 const char *filename
, const char *mode
)
3166 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
3167 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
3168 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
3170 *pFile
= fopen(filename
, mode
);
3177 /*********************************************************************
3178 * _wfopen (MSVCRT.@)
3180 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3182 return _wfsopen( path
, mode
, _SH_DENYNO
);
3185 /*********************************************************************
3186 * _wfopen_s (MSVCRT.@)
3188 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3189 const wchar_t *mode
)
3191 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3192 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3197 *pFile
= _wfopen(filename
, mode
);
3204 /* fputc calls _flsbuf which calls fputc */
3205 int CDECL
_flsbuf(int c
, FILE* file
);
3207 /*********************************************************************
3210 int CDECL
fputc(int c
, FILE* file
)
3220 res
= msvcrt_flush_buffer(file
);
3222 return res
? res
: c
;
3229 res
= _flsbuf(c
, file
);
3235 /*********************************************************************
3236 * _fputchar (MSVCRT.@)
3238 int CDECL
_fputchar(int c
)
3240 return fputc(c
, stdout
);
3243 /*********************************************************************
3246 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3248 size_t rcnt
=size
* nmemb
;
3257 /* first buffered data */
3259 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3260 memcpy(ptr
, file
->_ptr
, pcnt
);
3265 ptr
= (char*)ptr
+ pcnt
;
3266 } else if(!(file
->_flag
& _IOREAD
)) {
3267 if(file
->_flag
& _IORW
) {
3268 file
->_flag
|= _IOREAD
;
3275 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
3281 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& (file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
3282 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3283 file
->_ptr
= file
->_base
;
3284 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3285 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3286 if (i
> 0 && i
< file
->_cnt
) {
3287 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3288 file
->_flag
&= ~_IOEOF
;
3291 memcpy(ptr
, file
->_ptr
, i
);
3295 } else if (rcnt
> INT_MAX
) {
3296 i
= _read(file
->_file
, ptr
, INT_MAX
);
3297 } else if (rcnt
< BUFSIZ
) {
3298 i
= _read(file
->_file
, ptr
, rcnt
);
3300 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3304 ptr
= (char *)ptr
+i
;
3305 /* expose feof condition in the flags
3306 * MFC tests file->_flag for feof, and doesn't call feof())
3308 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3309 file
->_flag
|= _IOEOF
;
3312 file
->_flag
|= _IOERR
;
3323 /*********************************************************************
3324 * _wfreopen (MSVCRT.@)
3327 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3329 int open_flags
, stream_flags
, fd
;
3331 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3334 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3339 /* map mode string to open() flags. "man fopen" for possibilities. */
3340 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3344 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3347 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
3350 WARN(":failed-last error (%d)\n",GetLastError());
3351 _dosmaperr(GetLastError());
3360 /*********************************************************************
3361 * freopen (MSVCRT.@)
3364 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3367 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3369 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3370 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3376 ret
= _wfreopen(pathW
, modeW
, file
);
3383 /*********************************************************************
3384 * fsetpos (MSVCRT.@)
3386 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3391 /* Note that all this has been lifted 'as is' from fseek */
3392 if(file
->_flag
& _IOWRT
)
3393 msvcrt_flush_buffer(file
);
3395 /* Discard buffered input */
3397 file
->_ptr
= file
->_base
;
3399 /* Reset direction of i/o */
3400 if(file
->_flag
& _IORW
) {
3401 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3404 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3409 /*********************************************************************
3410 * _ftelli64 (MSVCRT.@)
3412 __int64 CDECL
_ftelli64(FILE* file
)
3417 pos
= _telli64(file
->_file
);
3422 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
3423 if(file
->_flag
& _IOWRT
) {
3424 pos
+= file
->_ptr
- file
->_base
;
3426 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3429 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3433 } else if(!file
->_cnt
) { /* nothing to do */
3434 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3438 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3439 for(i
=0; i
<file
->_cnt
; i
++)
3440 if(file
->_ptr
[i
] == '\n')
3446 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3451 pos
-= file
->_bufsiz
;
3452 pos
+= file
->_ptr
- file
->_base
;
3454 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3455 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3458 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3469 /*********************************************************************
3472 LONG CDECL
ftell(FILE* file
)
3474 return (LONG
)_ftelli64(file
);
3477 /*********************************************************************
3478 * fgetpos (MSVCRT.@)
3480 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3482 *pos
= _ftelli64(file
);
3488 /*********************************************************************
3491 int CDECL
fputs(const char *s
, FILE* file
)
3493 size_t len
= strlen(s
);
3497 ret
= fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
3502 /*********************************************************************
3505 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3507 size_t i
, len
= strlenW(s
);
3512 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3513 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3518 tmp_buf
= add_std_buffer(file
);
3519 for (i
=0; i
<len
; i
++) {
3520 if(fputwc(s
[i
], file
) == WEOF
) {
3521 if(tmp_buf
) remove_std_buffer(file
);
3527 if(tmp_buf
) remove_std_buffer(file
);
3532 /*********************************************************************
3533 * getchar (MSVCRT.@)
3535 int CDECL
getchar(void)
3537 return fgetc(stdin
);
3540 /*********************************************************************
3543 int CDECL
getc(FILE* file
)
3548 /*********************************************************************
3551 char * CDECL
gets(char *buf
)
3554 char * buf_start
= buf
;
3557 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3559 if(cc
!= '\r') *buf
++ = (char)cc
;
3563 TRACE("got '%s'\n", buf_start
);
3564 _unlock_file(stdin
);
3568 /*********************************************************************
3571 wchar_t* CDECL
_getws(wchar_t* buf
)
3577 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3581 *buf
++ = (wchar_t)cc
;
3585 TRACE("got %s\n", debugstr_w(ws
));
3586 _unlock_file(stdin
);
3590 /*********************************************************************
3593 int CDECL
putc(int c
, FILE* file
)
3595 return fputc(c
, file
);
3598 /*********************************************************************
3599 * putchar (MSVCRT.@)
3601 int CDECL
putchar(int c
)
3603 return fputc(c
, stdout
);
3606 /*********************************************************************
3607 * _putwch (MSVCRT.@)
3609 wint_t CDECL
_putwch(wchar_t c
)
3611 return fputwc(c
, stdout
);
3614 /*********************************************************************
3617 int CDECL
puts(const char *s
)
3619 size_t len
= strlen(s
);
3623 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3624 _unlock_file(stdout
);
3628 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3629 _unlock_file(stdout
);
3633 /*********************************************************************
3636 int CDECL
_putws(const wchar_t *s
)
3638 static const wchar_t nl
= '\n';
3639 size_t len
= strlenW(s
);
3643 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3644 _unlock_file(stdout
);
3648 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3649 _unlock_file(stdout
);
3653 /*********************************************************************
3656 int CDECL
remove(const char *path
)
3658 TRACE("(%s)\n",path
);
3659 if (DeleteFileA(path
))
3661 TRACE(":failed (%d)\n",GetLastError());
3662 _dosmaperr(GetLastError());
3666 /*********************************************************************
3667 * _wremove (MSVCRT.@)
3669 int CDECL
_wremove(const wchar_t *path
)
3671 TRACE("(%s)\n",debugstr_w(path
));
3672 if (DeleteFileW(path
))
3674 TRACE(":failed (%d)\n",GetLastError());
3675 _dosmaperr(GetLastError());
3679 /*********************************************************************
3682 int CDECL
rename(const char *oldpath
,const char *newpath
)
3684 TRACE(":from %s to %s\n",oldpath
,newpath
);
3685 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3687 TRACE(":failed (%d)\n",GetLastError());
3688 _dosmaperr(GetLastError());
3692 /*********************************************************************
3693 * _wrename (MSVCRT.@)
3695 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3697 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3698 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3700 TRACE(":failed (%d)\n",GetLastError());
3701 _dosmaperr(GetLastError());
3705 /*********************************************************************
3706 * setvbuf (MSVCRT.@)
3708 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3710 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
3711 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
3712 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
3717 if(file
->_flag
& _IOMYBUF
)
3719 file
->_flag
&= ~(_IONBF
| _IOMYBUF
| _USERBUF
);
3722 if(mode
== _IONBF
) {
3723 file
->_flag
|= _IONBF
;
3724 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
3727 file
->_base
= file
->_ptr
= buf
;
3728 file
->_flag
|= _USERBUF
;
3729 file
->_bufsiz
= size
;
3731 file
->_base
= file
->_ptr
= malloc(size
);
3738 file
->_flag
|= _IOMYBUF
;
3739 file
->_bufsiz
= size
;
3745 /*********************************************************************
3748 void CDECL
setbuf(FILE* file
, char *buf
)
3750 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3753 /*********************************************************************
3756 char * CDECL
tmpnam(char *s
)
3763 thread_data_t
*data
= msvcrt_get_thread_data();
3765 if(!data
->tmpnam_buffer
)
3766 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3768 s
= data
->tmpnam_buffer
;
3771 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3772 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3773 for (count
= 0; count
< TMP_MAX
; count
++)
3775 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
3776 memcpy(p
, tmpstr
, size
);
3778 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3779 GetLastError() == ERROR_FILE_NOT_FOUND
)
3785 /*********************************************************************
3786 * _wtmpnam (MSVCRT.@)
3788 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3790 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3795 thread_data_t
*data
= msvcrt_get_thread_data();
3797 if(!data
->wtmpnam_buffer
)
3798 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3800 s
= data
->wtmpnam_buffer
;
3803 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3804 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3805 for (count
= 0; count
< TMP_MAX
; count
++)
3807 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
3808 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3810 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3811 GetLastError() == ERROR_FILE_NOT_FOUND
)
3817 /*********************************************************************
3818 * tmpfile (MSVCRT.@)
3820 FILE* CDECL
tmpfile(void)
3822 char *filename
= tmpnam(NULL
);
3827 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3828 _S_IREAD
| _S_IWRITE
);
3829 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3831 if (msvcrt_init_fp(file
, fd
, _IORW
) == -1)
3836 else file
->_tmpfname
= _strdup(filename
);
3839 if(fd
!= -1 && !file
)
3845 /*********************************************************************
3848 int CDECL
ungetc(int c
, FILE * file
)
3850 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3852 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
3853 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
3857 if((!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
3858 && alloc_buffer(file
))
3859 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
3862 if(file
->_ptr
>file
->_base
) {
3864 if(file
->_flag
& _IOSTRG
) {
3865 if(*file
->_ptr
!= c
) {
3875 file
->_flag
|= _IOREAD
;
3884 /*********************************************************************
3885 * ungetwc (MSVCRT.@)
3887 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3896 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3897 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3898 unsigned char * pp
= (unsigned char *)&mwc
;
3901 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3902 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3908 char mbs
[MB_LEN_MAX
];
3911 len
= wctomb(mbs
, mwc
);
3917 for(len
--; len
>=0; len
--) {
3918 if(mbs
[len
] != ungetc(mbs
[len
], file
)) {
3931 /*********************************************************************
3932 * _getmaxstdio (MSVCRT.@)
3934 int CDECL
_getmaxstdio(void)
3936 return MSVCRT_max_streams
;
3939 /*********************************************************************
3940 * _setmaxstdio (MSVCRT.@)
3942 int CDECL
_setmaxstdio(int newmax
)
3944 TRACE("%d\n", newmax
);
3946 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
3949 MSVCRT_max_streams
= newmax
;
3950 return MSVCRT_max_streams
;