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 MSVCRT_MAX_FILES 2048
111 #define MSVCRT_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
[MSVCRT_MAX_FILES
/MSVCRT_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
[MSVCRT_MAX_FILES
/MSVCRT_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
<MSVCRT_MAX_FILES
)
175 ret
= __pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
179 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
182 static inline ioinfo
* get_ioinfo(int fd
)
185 if(fd
< MSVCRT_MAX_FILES
)
186 ret
= __pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
190 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
193 static inline FILE* msvcrt_get_file(int i
)
197 if(i
>= MSVCRT_max_streams
)
203 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
205 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
206 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
207 ERR("out of memory\n");
212 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
214 ret
+= i
%MSVCRT_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
)
287 if (fd
>= MSVCRT_MAX_FILES
)
289 WARN(":files exhausted!\n");
294 fdinfo
= get_ioinfo(fd
);
295 if(fdinfo
== &__badioinfo
) {
298 __pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
299 if(!__pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
300 WARN(":out of memory!\n");
305 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
306 __pioinfo
[fd
/MSVCRT_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
, MSVCRT_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
;
532 #ifdef __REACTOS__ /* CORE-11949 */
533 file
->_ptr
=file
->_base
;
538 #ifndef __REACTOS__ /* CORE-11949 */
539 file
->_ptr
=file
->_base
;
545 /*********************************************************************
548 int CDECL
_isatty(int fd
)
550 TRACE(":fd (%d)\n",fd
);
552 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
555 /* INTERNAL: Allocate stdio file buffer */
556 /*static*/ BOOL
msvcrt_alloc_buffer(FILE* file
)
558 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
559 && _isatty(file
->_file
))
562 file
->_base
= calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
564 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
565 file
->_flag
|= _IOMYBUF
;
567 file
->_base
= (char*)(&file
->_charbuf
);
569 file
->_flag
|= _IONBF
;
571 file
->_ptr
= file
->_base
;
576 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
577 static BOOL
add_std_buffer(FILE *file
)
579 static char buffers
[2][BUFSIZ
];
581 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
582 || (file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
583 || !_isatty(file
->_file
))
586 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
587 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
588 file
->_flag
|= _USERBUF
;
592 /* INTERNAL: Removes temporary buffer from stdout or stderr */
593 /* Only call this function when add_std_buffer returned TRUE */
594 static void remove_std_buffer(FILE *file
)
596 msvcrt_flush_buffer(file
);
597 file
->_ptr
= file
->_base
= NULL
;
598 file
->_bufsiz
= file
->_cnt
= 0;
599 file
->_flag
&= ~_USERBUF
;
602 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
603 static int msvcrt_int_to_base32(int num
, char *str
)
618 *p
= (num
& 31) + '0';
620 *p
+= ('a' - '0' - 10);
627 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
628 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
643 *p
= (num
& 31) + '0';
645 *p
+= ('a' - '0' - 10);
652 /* INTERNAL: Create a wide string from an ascii string */
653 wchar_t *msvcrt_wstrdupa(const char *str
)
655 const unsigned int len
= strlen(str
) + 1 ;
656 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
659 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
663 /*********************************************************************
664 * __iob_func(MSVCRT.@)
666 FILE * CDECL
__iob_func(void)
671 /*********************************************************************
674 int CDECL
_access(const char *filename
, int mode
)
676 DWORD attr
= GetFileAttributesA(filename
);
678 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
680 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
682 _dosmaperr(GetLastError());
685 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
687 _set_errno(ERROR_ACCESS_DENIED
);
693 /*********************************************************************
694 * _access_s (MSVCRT.@)
696 int CDECL
_access_s(const char *filename
, int mode
)
698 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
699 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
705 return _access(filename
, mode
);
708 /*********************************************************************
709 * _waccess (MSVCRT.@)
711 int CDECL
_waccess(const wchar_t *filename
, int mode
)
713 DWORD attr
= GetFileAttributesW(filename
);
715 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
717 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
719 _dosmaperr(GetLastError());
722 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
724 _set_errno(ERROR_ACCESS_DENIED
);
730 /*********************************************************************
731 * _waccess_s (MSVCRT.@)
733 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
735 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
736 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
742 return _waccess(filename
, mode
);
745 /*********************************************************************
748 int CDECL
_chmod(const char *path
, int flags
)
750 DWORD oldFlags
= GetFileAttributesA(path
);
752 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
754 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
755 oldFlags
| FILE_ATTRIBUTE_READONLY
;
757 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
760 _dosmaperr(GetLastError());
764 /*********************************************************************
767 int CDECL
_wchmod(const wchar_t *path
, int flags
)
769 DWORD oldFlags
= GetFileAttributesW(path
);
771 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
773 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
774 oldFlags
| FILE_ATTRIBUTE_READONLY
;
776 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
779 _dosmaperr(GetLastError());
783 /*********************************************************************
786 int CDECL
_unlink(const char *path
)
788 TRACE("%s\n",debugstr_a(path
));
789 if(DeleteFileA(path
))
791 TRACE("failed (%d)\n",GetLastError());
792 _dosmaperr(GetLastError());
796 /*********************************************************************
797 * _wunlink (MSVCRT.@)
799 int CDECL
_wunlink(const wchar_t *path
)
801 TRACE("(%s)\n",debugstr_w(path
));
802 if(DeleteFileW(path
))
804 TRACE("failed (%d)\n",GetLastError());
805 _dosmaperr(GetLastError());
809 /*********************************************************************
812 int CDECL
_commit(int fd
)
814 HANDLE hand
= fdtoh(fd
);
816 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
817 if (hand
== INVALID_HANDLE_VALUE
)
820 if (!FlushFileBuffers(hand
))
822 if (GetLastError() == ERROR_INVALID_HANDLE
)
824 /* FlushFileBuffers fails for console handles
825 * so we ignore this error.
829 TRACE(":failed-last error (%d)\n",GetLastError());
830 _dosmaperr(GetLastError());
837 /* _flushall calls fflush which calls _flushall */
838 int CDECL
fflush(FILE* file
);
840 /* INTERNAL: Flush all stream buffer */
841 static int msvcrt_flush_all_buffers(int mask
)
843 int i
, num_flushed
= 0;
847 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
848 file
= msvcrt_get_file(i
);
852 if(file
->_flag
& mask
) {
860 TRACE(":flushed (%d) handles\n",num_flushed
);
864 /*********************************************************************
865 * _flushall (MSVCRT.@)
867 int CDECL
_flushall(void)
869 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
872 /*********************************************************************
875 int CDECL
fflush(FILE* file
)
878 msvcrt_flush_all_buffers(_IOWRT
);
879 } else if(file
->_flag
& _IOWRT
) {
883 res
= msvcrt_flush_buffer(file
);
885 if(!res && (file->_flag & _IOCOMMIT))
886 res = _commit(file->_file) ? EOF : 0;
891 } else if(file
->_flag
& _IOREAD
) {
894 file
->_ptr
= file
->_base
;
902 /*********************************************************************
905 int CDECL
_close(int fd
)
912 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
913 if (hand
== INVALID_HANDLE_VALUE
)
915 else if (!CloseHandle(hand
))
917 WARN(":failed-last error (%d)\n",GetLastError());
918 _dosmaperr(GetLastError());
931 /*********************************************************************
934 * MSDN isn't clear on this point, but the remarks for _pipe
935 * indicate file descriptors duplicated with _dup and _dup2 are always
938 int CDECL
_dup2(int od
, int nd
)
942 TRACE("(od=%d, nd=%d)\n", od
, nd
);
944 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
948 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
949 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
951 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
955 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
963 /* _dup2 returns 0, not nd, on success */
970 _dosmaperr(GetLastError());
982 /*********************************************************************
985 int CDECL
_dup(int od
)
991 if (_dup2(od
, fd
) == 0)
999 /*********************************************************************
1002 int CDECL
_eof(int fd
)
1004 DWORD curpos
,endpos
;
1005 LONG hcurpos
,hendpos
;
1006 HANDLE hand
= fdtoh(fd
);
1008 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1010 if (hand
== INVALID_HANDLE_VALUE
)
1013 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1015 /* Otherwise we do it the hard way */
1016 hcurpos
= hendpos
= 0;
1017 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1018 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1020 if (curpos
== endpos
&& hcurpos
== hendpos
)
1022 /* FIXME: shouldn't WX_ATEOF be set here? */
1026 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1030 /*********************************************************************
1031 * _fcloseall (MSVCRT.@)
1033 int CDECL
_fcloseall(void)
1035 int num_closed
= 0, i
;
1039 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1040 file
= msvcrt_get_file(i
);
1042 if (file
->_flag
&& !fclose(file
))
1047 TRACE(":closed (%d) handles\n",num_closed
);
1051 /* free everything on process exit */
1052 void msvcrt_free_io(void)
1060 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1063 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1065 FILE *file
= msvcrt_get_file(j
);
1066 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1068 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1069 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1073 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1074 free(MSVCRT_fstream
[i
]);
1077 /*********************************************************************
1078 * _lseeki64 (MSVCRT.@)
1080 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1082 HANDLE hand
= fdtoh(fd
);
1085 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1086 if (hand
== INVALID_HANDLE_VALUE
)
1089 if (whence
< 0 || whence
> 2)
1095 TRACE(":fd (%d) to %s pos %s\n",
1096 fd
,wine_dbgstr_longlong(offset
),
1097 (whence
==SEEK_SET
)?"SEEK_SET":
1098 (whence
==SEEK_CUR
)?"SEEK_CUR":
1099 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1101 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1102 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1103 ofs
.QuadPart
= offset
;
1104 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1105 GetLastError() == ERROR_SUCCESS
)
1107 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1108 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1110 return ofs
.QuadPart
;
1112 TRACE(":error-last error (%d)\n",GetLastError());
1113 _dosmaperr(GetLastError());
1117 /*********************************************************************
1120 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1122 return (LONG
)_lseeki64(fd
, offset
, whence
);
1125 /*********************************************************************
1126 * _lock_file (MSVCRT.@)
1128 void CDECL
_lock_file(FILE *file
)
1130 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1131 _lock(_STREAM_LOCKS
+(file
-_iob
));
1132 /* ReactOS: string streams dont need to be locked */
1133 else if(!(file
->_flag
& _IOSTRG
))
1134 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1137 /*********************************************************************
1138 * _unlock_file (MSVCRT.@)
1140 void CDECL
_unlock_file(FILE *file
)
1142 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1143 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1144 /* ReactOS: string streams dont need to be locked */
1145 else if(!(file
->_flag
& _IOSTRG
))
1146 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1150 /*********************************************************************
1151 * _locking (MSVCRT.@)
1153 * This is untested; the underlying LockFile doesn't work yet.
1155 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1159 HANDLE hand
= fdtoh(fd
);
1161 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1162 if (hand
== INVALID_HANDLE_VALUE
)
1165 if (mode
< 0 || mode
> 4)
1171 TRACE(":fd (%d) by 0x%08x mode %s\n",
1172 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1173 (mode
==_LK_LOCK
)?"_LK_LOCK":
1174 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1175 (mode
==_LK_RLCK
)?"_LK_RLCK":
1176 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1179 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1181 FIXME ("Seek failed\n");
1182 *_errno() = EINVAL
; /* FIXME */
1185 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1188 ret
= 1; /* just to satisfy gcc */
1191 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1196 else if (mode
== _LK_UNLCK
)
1197 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1199 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1200 /* FIXME - what about error settings? */
1201 return ret
? 0 : -1;
1204 /*********************************************************************
1205 * _fseeki64 (MSVCRT.@)
1207 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1212 /* Flush output if needed */
1213 if(file
->_flag
& _IOWRT
)
1214 msvcrt_flush_buffer(file
);
1216 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1218 offset
+= _ftelli64(file
);
1221 /* Discard buffered input */
1223 file
->_ptr
= file
->_base
;
1224 /* Reset direction of i/o */
1225 if(file
->_flag
& _IORW
) {
1226 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1228 /* Clear end of file flag */
1229 file
->_flag
&= ~_IOEOF
;
1230 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1236 /*********************************************************************
1239 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1241 return _fseeki64( file
, offset
, whence
);
1244 /*********************************************************************
1245 * _chsize_s (MSVCRT.@)
1247 int CDECL
_chsize_s(int fd
, __int64 size
)
1253 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1255 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1260 if (handle
!= INVALID_HANDLE_VALUE
)
1262 /* save the current file pointer */
1263 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1266 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1269 ret
= SetEndOfFile(handle
);
1270 if (!ret
) _dosmaperr(GetLastError());
1273 /* restore the file pointer */
1274 _lseeki64(fd
, cur
, SEEK_SET
);
1279 return ret
? 0 : *_errno();
1282 /*********************************************************************
1283 * _chsize (MSVCRT.@)
1285 int CDECL
_chsize(int fd
, long size
)
1287 /* _chsize_s returns errno on failure but _chsize should return -1 */
1288 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1291 /*********************************************************************
1292 * clearerr (MSVCRT.@)
1294 void CDECL
clearerr(FILE* file
)
1296 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1299 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1303 /*********************************************************************
1306 void CDECL
rewind(FILE* file
)
1308 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1311 fseek(file
, 0L, SEEK_SET
);
1316 static int msvcrt_get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1318 int plus
= strchrW(mode
, '+') != NULL
;
1320 TRACE("%s\n", debugstr_w(mode
));
1322 while(*mode
== ' ') mode
++;
1327 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1328 *stream_flags
= plus
? _IORW
: _IOREAD
;
1331 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1332 *stream_flags
= plus
? _IORW
: _IOWRT
;
1335 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1336 *stream_flags
= plus
? _IORW
: _IOWRT
;
1339 MSVCRT_INVALID_PMT(0, EINVAL
);
1343 *stream_flags
|= _commode
;
1345 while (*mode
&& *mode
!=',')
1349 *open_flags
|= _O_BINARY
;
1350 *open_flags
&= ~_O_TEXT
;
1353 *open_flags
|= _O_TEXT
;
1354 *open_flags
&= ~_O_BINARY
;
1357 *open_flags
|= _O_TEMPORARY
;
1360 *open_flags
|= _O_SHORT_LIVED
;
1363 *stream_flags
|= _IOCOMMIT
;
1366 *stream_flags
&= ~_IOCOMMIT
;
1369 *open_flags
|= _O_NOINHERIT
;
1378 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1381 ERR("incorrect mode flag: %c\n", mode
[-1]);
1387 static const WCHAR ccs
[] = {'c','c','s'};
1388 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1389 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1390 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1393 while(*mode
== ' ') mode
++;
1394 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1396 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1397 while(*mode
== ' ') mode
++;
1398 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1401 while(*mode
== ' ') mode
++;
1403 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1405 *open_flags
|= _O_U8TEXT
;
1406 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1408 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1410 *open_flags
|= _O_U16TEXT
;
1411 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1413 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1415 *open_flags
|= _O_WTEXT
;
1416 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1420 MSVCRT_INVALID_PMT(0, EINVAL
);
1424 while(*mode
== ' ') mode
++;
1427 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1432 /*********************************************************************
1433 * _fdopen (MSVCRT.@)
1435 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1438 wchar_t *modeW
= NULL
;
1440 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1442 ret
= _wfdopen(fd
, modeW
);
1448 /*********************************************************************
1449 * _wfdopen (MSVCRT.@)
1451 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1453 int open_flags
, stream_flags
;
1456 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1459 if (!(file
= msvcrt_alloc_fp()))
1461 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1466 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1472 /*********************************************************************
1473 * _filelength (MSVCRT.@)
1475 LONG CDECL
_filelength(int fd
)
1477 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1480 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1483 if (endPos
!= curPos
)
1484 _lseek(fd
, curPos
, SEEK_SET
);
1491 /*********************************************************************
1492 * _filelengthi64 (MSVCRT.@)
1494 __int64 CDECL
_filelengthi64(int fd
)
1496 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1499 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1502 if (endPos
!= curPos
)
1503 _lseeki64(fd
, curPos
, SEEK_SET
);
1510 /*********************************************************************
1511 * _fileno (MSVCRT.@)
1513 int CDECL
_fileno(FILE* file
)
1515 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1519 /*********************************************************************
1520 * _get_osfhandle (MSVCRT.@)
1522 intptr_t CDECL
_get_osfhandle(int fd
)
1524 HANDLE hand
= fdtoh(fd
);
1525 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1527 return (intptr_t)hand
;
1530 /*********************************************************************
1531 * _mktemp (MSVCRT.@)
1533 char * CDECL
_mktemp(char *pattern
)
1536 char *retVal
= pattern
;
1544 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1548 id
= GetCurrentProcessId();
1552 int tempNum
= id
/ 10;
1553 *pattern
-- = id
- (tempNum
* 10) + '0';
1559 *pattern
= letter
++;
1560 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1562 } while(letter
<= 'z');
1566 /*********************************************************************
1567 * _wmktemp (MSVCRT.@)
1569 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1572 wchar_t *retVal
= pattern
;
1574 wchar_t letter
= 'a';
1577 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1581 id
= GetCurrentProcessId();
1585 int tempNum
= id
/ 10;
1586 *pattern
-- = id
- (tempNum
* 10) + '0';
1592 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1593 GetLastError() == ERROR_FILE_NOT_FOUND
)
1595 *pattern
= letter
++;
1596 } while(letter
!= '|');
1600 /*static*/ unsigned split_oflags(unsigned oflags
)
1603 unsigned unsupp
; /* until we support everything */
1605 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1606 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1607 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1608 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1609 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1610 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1611 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1612 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1613 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1615 if ((unsupp
= oflags
& ~(
1616 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1617 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1618 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1620 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1621 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1623 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1628 /*********************************************************************
1631 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1634 SECURITY_ATTRIBUTES sa
;
1635 HANDLE readHandle
, writeHandle
;
1643 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1644 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1645 sa
.lpSecurityDescriptor
= NULL
;
1646 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1648 unsigned int wxflags
= split_oflags(textmode
);
1652 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1656 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1665 CloseHandle(writeHandle
);
1671 CloseHandle(readHandle
);
1672 CloseHandle(writeHandle
);
1678 _dosmaperr(GetLastError());
1683 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1685 char bom
[sizeof(utf8_bom
)];
1688 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1690 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1693 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1694 oflags
|= _O_U8TEXT
;
1695 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1697 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1698 oflags
|= _O_U16TEXT
;
1700 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1706 /*********************************************************************
1707 * _wsopen_s (MSVCRT.@)
1709 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1711 DWORD access
= 0, creation
= 0, attrib
;
1712 SECURITY_ATTRIBUTES sa
;
1713 DWORD sharing
, type
;
1717 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1718 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1720 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1723 wxflag
= split_oflags(oflags
);
1724 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1726 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1727 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1728 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1731 if (oflags
& _O_CREAT
)
1733 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1734 FIXME(": pmode 0x%04x ignored\n", pmode
);
1736 WARN(": pmode 0x%04x ignored\n", pmode
);
1738 if (oflags
& _O_EXCL
)
1739 creation
= CREATE_NEW
;
1740 else if (oflags
& _O_TRUNC
)
1741 creation
= CREATE_ALWAYS
;
1743 creation
= OPEN_ALWAYS
;
1745 else /* no _O_CREAT */
1747 if (oflags
& _O_TRUNC
)
1748 creation
= TRUNCATE_EXISTING
;
1750 creation
= OPEN_EXISTING
;
1759 sharing
= FILE_SHARE_READ
;
1762 sharing
= FILE_SHARE_WRITE
;
1765 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1768 ERR( "Unhandled shflags 0x%x\n", shflags
);
1771 attrib
= FILE_ATTRIBUTE_NORMAL
;
1773 if (oflags
& _O_TEMPORARY
)
1775 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1777 sharing
|= FILE_SHARE_DELETE
;
1780 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1781 sa
.lpSecurityDescriptor
= NULL
;
1782 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1784 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1785 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1786 && !(access
&GENERIC_READ
))
1788 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1789 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1790 if (hand
!= INVALID_HANDLE_VALUE
)
1792 oflags
= check_bom(hand
, oflags
, FALSE
);
1796 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1799 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1800 if (hand
== INVALID_HANDLE_VALUE
) {
1801 WARN(":failed-last error (%d)\n",GetLastError());
1802 _dosmaperr(GetLastError());
1806 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1808 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1809 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1810 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1812 if (oflags
& _O_U8TEXT
)
1814 DWORD written
= 0, tmp
;
1816 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1817 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1819 if (written
!= sizeof(utf8_bom
)) {
1820 WARN("error writing BOM\n");
1822 _dosmaperr(GetLastError());
1828 DWORD written
= 0, tmp
;
1830 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1831 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1833 if (written
!= sizeof(utf16_bom
))
1835 WARN("error writing BOM\n");
1837 _dosmaperr(GetLastError());
1842 else if (access
& GENERIC_READ
)
1843 oflags
= check_bom(hand
, oflags
, TRUE
);
1846 type
= GetFileType(hand
);
1847 if (type
== FILE_TYPE_CHAR
)
1849 else if (type
== FILE_TYPE_PIPE
)
1852 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1856 if (oflags
& _O_WTEXT
)
1857 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1858 else if (oflags
& _O_U16TEXT
)
1859 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1860 else if (oflags
& _O_U8TEXT
)
1861 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1863 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1867 /*********************************************************************
1868 * _wsopen (MSVCRT.@)
1870 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1875 if (oflags
& _O_CREAT
)
1879 __ms_va_start(ap
, shflags
);
1880 pmode
= va_arg(ap
, int);
1886 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1890 /*********************************************************************
1891 * _sopen_s (MSVCRT.@)
1893 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1898 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1901 ret
= _wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1906 /*********************************************************************
1909 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1914 if (oflags
& _O_CREAT
)
1918 va_start(ap
, shflags
);
1919 pmode
= va_arg(ap
, int);
1925 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1929 /*********************************************************************
1932 int CDECL
_open( const char *path
, int flags
, ... )
1936 if (flags
& _O_CREAT
)
1939 va_start(ap
, flags
);
1940 pmode
= va_arg(ap
, int);
1942 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1945 return _sopen( path
, flags
, _SH_DENYNO
);
1948 /*********************************************************************
1951 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1955 if (flags
& _O_CREAT
)
1958 va_start(ap
, flags
);
1959 pmode
= va_arg(ap
, int);
1961 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1964 return _wsopen( path
, flags
, _SH_DENYNO
);
1967 /*********************************************************************
1970 int CDECL
_creat(const char *path
, int flags
)
1972 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1973 return _open(path
, usedFlags
);
1976 /*********************************************************************
1977 * _wcreat (MSVCRT.@)
1979 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1981 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1982 return _wopen(path
, usedFlags
);
1985 /*********************************************************************
1986 * _open_osfhandle (MSVCRT.@)
1988 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1993 /* _O_RDONLY (0) always matches, so set the read flag
1994 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1995 * file, so set the write flag. It also only sets _O_TEXT if it wants
1996 * text - it never sets _O_BINARY.
1998 /* don't let split_oflags() decide the mode if no mode is passed */
1999 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2000 oflags
|= _O_BINARY
;
2002 flags
= GetFileType((HANDLE
)handle
);
2003 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2005 _dosmaperr(GetLastError());
2009 if (flags
== FILE_TYPE_CHAR
)
2011 else if (flags
== FILE_TYPE_PIPE
)
2015 flags
|= split_oflags(oflags
);
2017 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2018 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2022 /*********************************************************************
2025 int CDECL
_rmtmp(void)
2027 int num_removed
= 0, i
;
2031 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2032 file
= msvcrt_get_file(i
);
2034 if (file
->_tmpfname
)
2043 TRACE(":removed (%d) temp files\n",num_removed
);
2047 static inline int get_utf8_char_len(char ch
)
2049 if((ch
&0xf8) == 0xf0)
2051 else if((ch
&0xf0) == 0xe0)
2053 else if((ch
&0xe0) == 0xc0)
2058 /*********************************************************************
2059 * (internal) read_utf8
2061 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2063 ioinfo
*fdinfo
= get_ioinfo(fd
);
2064 HANDLE hand
= fdinfo
->handle
;
2065 char min_buf
[4], *readbuf
, lookahead
;
2066 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2068 /* make the buffer big enough to hold at least one character */
2069 /* read bytes have to fit to output and lookahead buffers */
2071 readbuf_size
= count
< 4 ? 4 : count
;
2072 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2077 if(fdinfo
->lookahead
[0] != '\n') {
2078 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2079 fdinfo
->lookahead
[0] = '\n';
2081 if(fdinfo
->lookahead
[1] != '\n') {
2082 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2083 fdinfo
->lookahead
[1] = '\n';
2085 if(fdinfo
->lookahead
[2] != '\n') {
2086 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2087 fdinfo
->lookahead
[2] = '\n';
2092 /* NOTE: this case is broken in native dll, reading
2093 * sometimes fails when small buffer is passed
2096 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2097 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2098 fdinfo
->wxflag
|= WX_ATEOF
;
2101 _dosmaperr(GetLastError());
2104 }else if(!num_read
) {
2105 fdinfo
->wxflag
|= WX_ATEOF
;
2111 char_len
= get_utf8_char_len(readbuf
[0]);
2113 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2117 if(readbuf
[0] == '\n')
2118 fdinfo
->wxflag
|= WX_READNL
;
2120 fdinfo
->wxflag
&= ~WX_READNL
;
2122 if(readbuf
[0] == 0x1a) {
2123 fdinfo
->wxflag
|= WX_ATEOF
;
2127 if(readbuf
[0] == '\r') {
2128 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2130 else if(lookahead
== '\n')
2134 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2135 fdinfo
->lookahead
[0] = lookahead
;
2137 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2142 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2143 _dosmaperr(GetLastError());
2150 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2153 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2154 fdinfo
->wxflag
|= WX_ATEOF
;
2155 if (readbuf
!= min_buf
) free(readbuf
);
2158 _dosmaperr(GetLastError());
2159 if (readbuf
!= min_buf
) free(readbuf
);
2162 }else if(!pos
&& !num_read
) {
2163 fdinfo
->wxflag
|= WX_ATEOF
;
2164 if (readbuf
!= min_buf
) free(readbuf
);
2169 if(readbuf
[0] == '\n')
2170 fdinfo
->wxflag
|= WX_READNL
;
2172 fdinfo
->wxflag
&= ~WX_READNL
;
2174 /* Find first byte of last character (may be incomplete) */
2175 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2176 if((readbuf
[i
]&0xc0) != 0x80)
2178 char_len
= get_utf8_char_len(readbuf
[i
]);
2179 if(char_len
+i
<= pos
)
2182 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2184 fdinfo
->lookahead
[0] = readbuf
[i
];
2186 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2188 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2190 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2194 for(i
=0, j
=0; i
<pos
; i
++) {
2195 if(readbuf
[i
] == 0x1a) {
2196 fdinfo
->wxflag
|= WX_ATEOF
;
2200 /* strip '\r' if followed by '\n' */
2201 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2202 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2203 readbuf
[j
++] = '\r';
2204 }else if(lookahead
== '\n' && j
==0) {
2205 readbuf
[j
++] = '\n';
2207 if(lookahead
!= '\n')
2208 readbuf
[j
++] = '\r';
2210 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2211 fdinfo
->lookahead
[0] = lookahead
;
2213 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2215 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2216 readbuf
[j
++] = readbuf
[i
];
2221 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2222 _dosmaperr(GetLastError());
2223 if (readbuf
!= min_buf
) free(readbuf
);
2227 if (readbuf
!= min_buf
) free(readbuf
);
2231 /*********************************************************************
2234 * When reading \r as last character in text mode, read() positions
2235 * the file pointer on the \r character while getc() goes on to
2238 static int read_i(int fd
, void *buf
, unsigned int count
)
2240 DWORD num_read
, utf16
;
2241 char *bufstart
= buf
;
2242 HANDLE hand
= fdtoh(fd
);
2243 ioinfo
*fdinfo
= get_ioinfo(fd
);
2248 if (fdinfo
->wxflag
& WX_ATEOF
) {
2249 TRACE("already at EOF, returning 0\n");
2252 /* Don't trace small reads, it gets *very* annoying */
2254 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2255 if (hand
== INVALID_HANDLE_VALUE
)
2261 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2262 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2268 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2269 return read_utf8(fd
, buf
, count
);
2271 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2273 if (fdinfo
->lookahead
[0] != '\n')
2275 bufstart
[0] = fdinfo
->lookahead
[0];
2276 fdinfo
->lookahead
[0] = '\n';
2280 bufstart
[1] = fdinfo
->lookahead
[1];
2281 fdinfo
->lookahead
[1] = '\n';
2284 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2285 num_read
+= 1+utf16
;
2290 if(utf16
&& (num_read
&1))
2292 /* msvcr90 uses uninitialized value from the buffer in this case */
2293 /* msvcrt ignores additional data */
2294 ERR("got odd number of bytes in UTF16 mode\n");
2298 if (count
!= 0 && num_read
== 0)
2300 fdinfo
->wxflag
|= WX_ATEOF
;
2301 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2303 else if (fdinfo
->wxflag
& WX_TEXT
)
2307 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2308 fdinfo
->wxflag
|= WX_READNL
;
2310 fdinfo
->wxflag
&= ~WX_READNL
;
2312 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2314 /* in text mode, a ctrl-z signals EOF */
2315 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2317 fdinfo
->wxflag
|= WX_ATEOF
;
2318 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2322 /* in text mode, strip \r if followed by \n */
2323 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2328 lookahead
[1] = '\n';
2329 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2331 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2333 bufstart
[j
++] = '\n';
2334 if(utf16
) bufstart
[j
++] = 0;
2338 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2340 bufstart
[j
++] = '\r';
2341 if(utf16
) bufstart
[j
++] = 0;
2344 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2346 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2348 bufstart
[j
++] = '\n';
2349 if (utf16
) bufstart
[j
++] = 0;
2353 fdinfo
->lookahead
[0] = lookahead
[0];
2354 fdinfo
->lookahead
[1] = lookahead
[1];
2358 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2363 bufstart
[j
++] = '\r';
2364 if(utf16
) bufstart
[j
++] = 0;
2367 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2368 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2370 bufstart
[j
++] = bufstart
[i
];
2371 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2379 if (GetLastError() == ERROR_BROKEN_PIPE
)
2381 TRACE(":end-of-pipe\n");
2382 fdinfo
->wxflag
|= WX_ATEOF
;
2387 TRACE(":failed-last error (%d)\n",GetLastError());
2393 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2397 /*********************************************************************
2400 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2403 num_read
= read_i(fd
, buf
, count
);
2407 /*********************************************************************
2408 * _setmode (MSVCRT.@)
2410 int CDECL
_setmode(int fd
,int mode
)
2412 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2413 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2416 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2417 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2422 if(mode
== _O_BINARY
) {
2423 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2424 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2428 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2430 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2431 else if(mode
== _O_U8TEXT
)
2432 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2434 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2440 /*********************************************************************
2443 long CDECL
_tell(int fd
)
2445 return _lseek(fd
, 0, SEEK_CUR
);
2448 /*********************************************************************
2449 * _telli64 (MSVCRT.@)
2451 __int64 CDECL
_telli64(int fd
)
2453 return _lseeki64(fd
, 0, SEEK_CUR
);
2456 /*********************************************************************
2457 * _tempnam (MSVCRT.@)
2459 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2461 char tmpbuf
[MAX_PATH
];
2462 const char *tmp_dir
= getenv("TMP");
2464 if (tmp_dir
) dir
= tmp_dir
;
2466 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2467 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2469 TRACE("got name (%s)\n",tmpbuf
);
2470 DeleteFileA(tmpbuf
);
2471 return _strdup(tmpbuf
);
2473 TRACE("failed (%d)\n",GetLastError());
2477 /*********************************************************************
2478 * _wtempnam (MSVCRT.@)
2480 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2482 wchar_t tmpbuf
[MAX_PATH
];
2484 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2485 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2487 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2488 DeleteFileW(tmpbuf
);
2489 return _wcsdup(tmpbuf
);
2491 TRACE("failed (%d)\n",GetLastError());
2495 /*********************************************************************
2498 int CDECL
_umask(int umask
)
2500 int old_umask
= MSVCRT_umask
;
2501 TRACE("(%d)\n",umask
);
2502 MSVCRT_umask
= umask
;
2506 /*********************************************************************
2509 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2512 ioinfo
*info
= get_ioinfo(fd
);
2513 HANDLE hand
= info
->handle
;
2515 /* Don't trace small writes, it gets *very* annoying */
2518 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2520 if (hand
== INVALID_HANDLE_VALUE
)
2526 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2532 /* If appending, go to EOF */
2533 if (info
->wxflag
& WX_APPEND
)
2534 _lseek(fd
, 0, FILE_END
);
2536 if (!(info
->wxflag
& WX_TEXT
))
2538 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2539 && (num_written
== count
))
2541 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2542 hand
, GetLastError());
2547 unsigned int i
, j
, nr_lf
, size
;
2550 const char *s
= buf
, *buf_start
= buf
;
2552 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2554 /* find number of \n */
2555 for (nr_lf
=0, i
=0; i
<count
; i
++)
2561 if ((q
= p
= malloc(size
)))
2563 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2572 FIXME("Malloc failed\n");
2584 else if (info
->exflag
& EF_UTF16
)
2586 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2587 if (s
[i
]=='\n' && s
[i
+1]==0)
2592 if ((q
= p
= malloc(size
)))
2594 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2596 if (s
[i
]=='\n' && s
[i
+1]==0)
2607 FIXME("Malloc failed\n");
2623 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2624 if (s
[i
]=='\n' && s
[i
+1]==0)
2627 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2629 _dosmaperr(GetLastError());
2634 size
= conv_len
+nr_lf
;
2635 if((p
= malloc(count
+nr_lf
*2+size
)))
2637 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2639 if (s
[i
]=='\n' && s
[i
+1]==0)
2647 q
= p
+count
+nr_lf
*2;
2648 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2649 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2653 FIXME("Malloc failed\n");
2660 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2664 if (num_written
!= size
)
2666 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2667 fd
, hand
, GetLastError(), num_written
);
2669 return s
- buf_start
;
2677 /*********************************************************************
2680 int CDECL
_putw(int val
, FILE* file
)
2685 len
= _write(file
->_file
, &val
, sizeof(val
));
2686 if (len
== sizeof(val
)) {
2691 file
->_flag
|= _IOERR
;
2696 /*********************************************************************
2699 int CDECL
fclose(FILE* file
)
2705 free(file
->_tmpfname
);
2706 file
->_tmpfname
= NULL
;
2707 /* flush stdio buffers */
2708 if(file
->_flag
& _IOWRT
)
2710 if(file
->_flag
& _IOMYBUF
)
2713 r
=_close(file
->_file
);
2717 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2718 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2720 if(file
== msvcrt_get_file(MSVCRT_stream_idx
-1)) {
2721 while(MSVCRT_stream_idx
>3 && !file
->_flag
) {
2722 MSVCRT_stream_idx
--;
2723 file
= msvcrt_get_file(MSVCRT_stream_idx
-1);
2727 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2730 /*********************************************************************
2733 int CDECL
feof(FILE* file
)
2735 return file
->_flag
& _IOEOF
;
2738 /*********************************************************************
2741 int CDECL
ferror(FILE* file
)
2743 return file
->_flag
& _IOERR
;
2746 /*********************************************************************
2747 * _filbuf (MSVCRT.@)
2749 int CDECL
_filbuf(FILE* file
)
2754 if(file
->_flag
& _IOSTRG
) {
2759 /* Allocate buffer if needed */
2760 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
2761 msvcrt_alloc_buffer(file
);
2763 if(!(file
->_flag
& _IOREAD
)) {
2764 if(file
->_flag
& _IORW
)
2765 file
->_flag
|= _IOREAD
;
2772 if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
2774 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2775 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2783 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2785 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2792 file
->_ptr
= file
->_base
+1;
2793 c
= *(unsigned char *)file
->_base
;
2799 /*********************************************************************
2802 int CDECL
fgetc(FILE* file
)
2810 i
= (unsigned char *)file
->_ptr
++;
2819 /*********************************************************************
2820 * _fgetchar (MSVCRT.@)
2822 int CDECL
_fgetchar(void)
2824 return fgetc(stdin
);
2827 /*********************************************************************
2830 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2833 char * buf_start
= s
;
2835 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2836 file
,file
->_file
,s
,size
);
2840 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2845 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2847 TRACE(":nothing read\n");
2851 if ((cc
!= EOF
) && (size
> 1))
2854 TRACE(":got %s\n", debugstr_a(buf_start
));
2859 /*********************************************************************
2862 wint_t CDECL
fgetwc(FILE* file
)
2869 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2870 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2873 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2882 char mbs
[MB_LEN_MAX
];
2888 if(isleadbyte((unsigned char)mbs
[0])) {
2899 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2907 /*********************************************************************
2910 int CDECL
_getw(FILE* file
)
2918 for (j
=0; j
<sizeof(int); j
++) {
2921 file
->_flag
|= _IOEOF
;
2932 /*********************************************************************
2935 wint_t CDECL
getwc(FILE* file
)
2937 return fgetwc(file
);
2940 /*********************************************************************
2941 * _fgetwchar (MSVCRT.@)
2943 wint_t CDECL
_fgetwchar(void)
2945 return fgetwc(stdin
);
2948 /*********************************************************************
2949 * getwchar (MSVCRT.@)
2951 wint_t CDECL
getwchar(void)
2953 return _fgetwchar();
2956 /*********************************************************************
2959 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2962 wchar_t * buf_start
= s
;
2964 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2965 file
,file
->_file
,s
,size
);
2969 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2974 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2976 TRACE(":nothing read\n");
2980 if ((cc
!= WEOF
) && (size
> 1))
2983 TRACE(":got %s\n", debugstr_w(buf_start
));
2988 /*********************************************************************
2991 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2993 size_t wrcnt
=size
* nmemb
;
3002 if(file
->_cnt
< 0) {
3003 WARN("negative file->_cnt value in %p\n", file
);
3004 file
->_flag
|= MSVCRT__IOERR
;
3009 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3010 memcpy(file
->_ptr
, ptr
, pcnt
);
3015 ptr
= (const char*)ptr
+ pcnt
;
3016 } else if((file
->_flag
& _IONBF
)
3017 || ((file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3018 || (!(file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3022 if(file
->_flag
& _IONBF
)
3024 else if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
)))
3025 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3027 bufsiz
= file
->_bufsiz
;
3029 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3031 if(msvcrt_flush_buffer(file
) == EOF
)
3034 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
3035 file
->_flag
|= _IOERR
;
3040 ptr
= (const char*)ptr
+ pcnt
;
3042 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3046 ptr
= (const char*)ptr
+ 1;
3051 return written
/ size
;
3054 /*********************************************************************
3056 * FORKED for ReactOS, don't sync with Wine!
3058 * - http://jira.reactos.org/browse/CORE-6495
3059 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3061 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3063 /* If this is a real file stream (and not some temporary one for
3064 sprintf-like functions), check whether it is opened in text mode.
3065 In this case, we have to perform an implicit conversion to ANSI. */
3066 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
3068 /* Convert to multibyte in text mode */
3069 char mbc
[MB_LEN_MAX
];
3072 mb_return
= wctomb(mbc
, c
);
3077 /* Output all characters */
3078 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3083 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3090 /*********************************************************************
3091 * _fputwchar (MSVCRT.@)
3093 wint_t CDECL
_fputwchar(wint_t wc
)
3095 return fputwc(wc
, stdout
);
3098 /*********************************************************************
3099 * _wfsopen (MSVCRT.@)
3101 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3104 int open_flags
, stream_flags
, fd
;
3106 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3108 /* map mode string to open() flags. "man fopen" for possibilities. */
3109 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3113 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3116 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3118 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3125 TRACE(":got (%p)\n",file
);
3126 if (fd
>= 0 && !file
)
3132 /*********************************************************************
3133 * _fsopen (MSVCRT.@)
3135 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3138 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3140 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3141 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3145 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3148 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3153 ret
= _wfsopen(pathW
, modeW
, share
);
3160 /*********************************************************************
3163 FILE * CDECL
fopen(const char *path
, const char *mode
)
3165 return _fsopen( path
, mode
, _SH_DENYNO
);
3168 /*********************************************************************
3169 * fopen_s (MSVCRT.@)
3171 int CDECL
fopen_s(FILE** pFile
,
3172 const char *filename
, const char *mode
)
3174 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
3175 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
3176 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
3178 *pFile
= fopen(filename
, mode
);
3185 /*********************************************************************
3186 * _wfopen (MSVCRT.@)
3188 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3190 return _wfsopen( path
, mode
, _SH_DENYNO
);
3193 /*********************************************************************
3194 * _wfopen_s (MSVCRT.@)
3196 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3197 const wchar_t *mode
)
3199 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3200 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3205 *pFile
= _wfopen(filename
, mode
);
3212 /* fputc calls _flsbuf which calls fputc */
3213 int CDECL
_flsbuf(int c
, FILE* file
);
3215 /*********************************************************************
3218 int CDECL
fputc(int c
, FILE* file
)
3228 res
= msvcrt_flush_buffer(file
);
3230 return res
? res
: c
;
3237 res
= _flsbuf(c
, file
);
3243 /*********************************************************************
3244 * _fputchar (MSVCRT.@)
3246 int CDECL
_fputchar(int c
)
3248 return fputc(c
, stdout
);
3251 /*********************************************************************
3254 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3256 size_t rcnt
=size
* nmemb
;
3265 /* first buffered data */
3267 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3268 memcpy(ptr
, file
->_ptr
, pcnt
);
3273 ptr
= (char*)ptr
+ pcnt
;
3274 } else if(!(file
->_flag
& _IOREAD
)) {
3275 if(file
->_flag
& _IORW
) {
3276 file
->_flag
|= _IOREAD
;
3283 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
3284 msvcrt_alloc_buffer(file
);
3289 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& (file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
3290 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3291 file
->_ptr
= file
->_base
;
3292 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3293 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3294 if (i
> 0 && i
< file
->_cnt
) {
3295 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3296 file
->_flag
&= ~_IOEOF
;
3299 memcpy(ptr
, file
->_ptr
, i
);
3303 } else if (rcnt
> INT_MAX
) {
3304 i
= _read(file
->_file
, ptr
, INT_MAX
);
3305 } else if (rcnt
< BUFSIZ
) {
3306 i
= _read(file
->_file
, ptr
, rcnt
);
3308 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3312 ptr
= (char *)ptr
+i
;
3313 /* expose feof condition in the flags
3314 * MFC tests file->_flag for feof, and doesn't call feof())
3316 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3317 file
->_flag
|= _IOEOF
;
3320 file
->_flag
|= _IOERR
;
3331 /*********************************************************************
3332 * _wfreopen (MSVCRT.@)
3335 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3337 int open_flags
, stream_flags
, fd
;
3339 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3342 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3347 /* map mode string to open() flags. "man fopen" for possibilities. */
3348 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3352 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3355 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
3358 WARN(":failed-last error (%d)\n",GetLastError());
3359 _dosmaperr(GetLastError());
3368 /*********************************************************************
3369 * freopen (MSVCRT.@)
3372 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3375 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3377 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3378 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3384 ret
= _wfreopen(pathW
, modeW
, file
);
3391 /*********************************************************************
3392 * fsetpos (MSVCRT.@)
3394 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3399 /* Note that all this has been lifted 'as is' from fseek */
3400 if(file
->_flag
& _IOWRT
)
3401 msvcrt_flush_buffer(file
);
3403 /* Discard buffered input */
3405 file
->_ptr
= file
->_base
;
3407 /* Reset direction of i/o */
3408 if(file
->_flag
& _IORW
) {
3409 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3412 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3417 /*********************************************************************
3418 * _ftelli64 (MSVCRT.@)
3420 __int64 CDECL
_ftelli64(FILE* file
)
3425 pos
= _telli64(file
->_file
);
3430 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
3431 if(file
->_flag
& _IOWRT
) {
3432 pos
+= file
->_ptr
- file
->_base
;
3434 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3437 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3441 } else if(!file
->_cnt
) { /* nothing to do */
3442 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3446 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3447 for(i
=0; i
<file
->_cnt
; i
++)
3448 if(file
->_ptr
[i
] == '\n')
3454 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3459 pos
-= file
->_bufsiz
;
3460 pos
+= file
->_ptr
- file
->_base
;
3462 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3463 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3466 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3477 /*********************************************************************
3480 LONG CDECL
ftell(FILE* file
)
3482 return (LONG
)_ftelli64(file
);
3485 /*********************************************************************
3486 * fgetpos (MSVCRT.@)
3488 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3490 *pos
= _ftelli64(file
);
3496 /*********************************************************************
3499 int CDECL
fputs(const char *s
, FILE* file
)
3501 size_t len
= strlen(s
);
3505 ret
= fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
3510 /*********************************************************************
3513 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3515 size_t i
, len
= strlenW(s
);
3520 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3521 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3526 tmp_buf
= add_std_buffer(file
);
3527 for (i
=0; i
<len
; i
++) {
3528 if(fputwc(s
[i
], file
) == WEOF
) {
3529 if(tmp_buf
) remove_std_buffer(file
);
3535 if(tmp_buf
) remove_std_buffer(file
);
3540 /*********************************************************************
3541 * getchar (MSVCRT.@)
3543 int CDECL
getchar(void)
3545 return fgetc(stdin
);
3548 /*********************************************************************
3551 int CDECL
getc(FILE* file
)
3556 /*********************************************************************
3559 char * CDECL
gets(char *buf
)
3562 char * buf_start
= buf
;
3565 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3567 if(cc
!= '\r') *buf
++ = (char)cc
;
3571 TRACE("got '%s'\n", buf_start
);
3572 _unlock_file(stdin
);
3576 /*********************************************************************
3579 wchar_t* CDECL
_getws(wchar_t* buf
)
3585 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3589 *buf
++ = (wchar_t)cc
;
3593 TRACE("got %s\n", debugstr_w(ws
));
3594 _unlock_file(stdin
);
3598 /*********************************************************************
3601 int CDECL
putc(int c
, FILE* file
)
3603 return fputc(c
, file
);
3606 /*********************************************************************
3607 * putchar (MSVCRT.@)
3609 int CDECL
putchar(int c
)
3611 return fputc(c
, stdout
);
3614 /*********************************************************************
3615 * _putwch (MSVCRT.@)
3617 wint_t CDECL
_putwch(wchar_t c
)
3619 return fputwc(c
, stdout
);
3622 /*********************************************************************
3625 int CDECL
puts(const char *s
)
3627 size_t len
= strlen(s
);
3631 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3632 _unlock_file(stdout
);
3636 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3637 _unlock_file(stdout
);
3641 /*********************************************************************
3644 int CDECL
_putws(const wchar_t *s
)
3646 static const wchar_t nl
= '\n';
3647 size_t len
= strlenW(s
);
3651 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3652 _unlock_file(stdout
);
3656 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3657 _unlock_file(stdout
);
3661 /*********************************************************************
3664 int CDECL
remove(const char *path
)
3666 TRACE("(%s)\n",path
);
3667 if (DeleteFileA(path
))
3669 TRACE(":failed (%d)\n",GetLastError());
3670 _dosmaperr(GetLastError());
3674 /*********************************************************************
3675 * _wremove (MSVCRT.@)
3677 int CDECL
_wremove(const wchar_t *path
)
3679 TRACE("(%s)\n",debugstr_w(path
));
3680 if (DeleteFileW(path
))
3682 TRACE(":failed (%d)\n",GetLastError());
3683 _dosmaperr(GetLastError());
3687 /*********************************************************************
3690 int CDECL
rename(const char *oldpath
,const char *newpath
)
3692 TRACE(":from %s to %s\n",oldpath
,newpath
);
3693 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3695 TRACE(":failed (%d)\n",GetLastError());
3696 _dosmaperr(GetLastError());
3700 /*********************************************************************
3701 * _wrename (MSVCRT.@)
3703 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3705 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3706 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3708 TRACE(":failed (%d)\n",GetLastError());
3709 _dosmaperr(GetLastError());
3713 /*********************************************************************
3714 * setvbuf (MSVCRT.@)
3716 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3718 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
3719 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
3720 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
3725 if(file
->_flag
& _IOMYBUF
)
3727 file
->_flag
&= ~(_IONBF
| _IOMYBUF
| _USERBUF
);
3730 if(mode
== _IONBF
) {
3731 file
->_flag
|= _IONBF
;
3732 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
3735 file
->_base
= file
->_ptr
= buf
;
3736 file
->_flag
|= _USERBUF
;
3737 file
->_bufsiz
= size
;
3739 file
->_base
= file
->_ptr
= malloc(size
);
3746 file
->_flag
|= _IOMYBUF
;
3747 file
->_bufsiz
= size
;
3753 /*********************************************************************
3756 void CDECL
setbuf(FILE* file
, char *buf
)
3758 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3761 /*********************************************************************
3764 char * CDECL
tmpnam(char *s
)
3771 thread_data_t
*data
= msvcrt_get_thread_data();
3773 if(!data
->tmpnam_buffer
)
3774 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3776 s
= data
->tmpnam_buffer
;
3779 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3780 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3781 for (count
= 0; count
< TMP_MAX
; count
++)
3783 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
3784 memcpy(p
, tmpstr
, size
);
3786 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3787 GetLastError() == ERROR_FILE_NOT_FOUND
)
3793 /*********************************************************************
3794 * _wtmpnam (MSVCRT.@)
3796 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3798 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3803 thread_data_t
*data
= msvcrt_get_thread_data();
3805 if(!data
->wtmpnam_buffer
)
3806 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3808 s
= data
->wtmpnam_buffer
;
3811 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3812 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3813 for (count
= 0; count
< TMP_MAX
; count
++)
3815 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
3816 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3818 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3819 GetLastError() == ERROR_FILE_NOT_FOUND
)
3825 /*********************************************************************
3826 * tmpfile (MSVCRT.@)
3828 FILE* CDECL
tmpfile(void)
3830 char *filename
= tmpnam(NULL
);
3835 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3836 _S_IREAD
| _S_IWRITE
);
3837 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3839 if (msvcrt_init_fp(file
, fd
, _IORW
) == -1)
3844 else file
->_tmpfname
= _strdup(filename
);
3847 if(fd
!= -1 && !file
)
3853 /*********************************************************************
3856 int CDECL
ungetc(int c
, FILE * file
)
3858 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3860 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
3861 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
3865 if((!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
3866 && msvcrt_alloc_buffer(file
))
3867 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
3870 if(file
->_ptr
>file
->_base
) {
3872 if(file
->_flag
& _IOSTRG
) {
3873 if(*file
->_ptr
!= c
) {
3883 file
->_flag
|= _IOREAD
;
3892 /*********************************************************************
3893 * ungetwc (MSVCRT.@)
3895 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3904 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3905 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3906 unsigned char * pp
= (unsigned char *)&mwc
;
3909 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3910 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3916 char mbs
[MB_LEN_MAX
];
3919 len
= wctomb(mbs
, mwc
);
3925 for(len
--; len
>=0; len
--) {
3926 if(mbs
[len
] != ungetc(mbs
[len
], file
)) {
3939 /*********************************************************************
3940 * _getmaxstdio (MSVCRT.@)
3942 int CDECL
_getmaxstdio(void)
3944 return MSVCRT_max_streams
;
3947 /*********************************************************************
3948 * _setmaxstdio (MSVCRT.@)
3950 int CDECL
_setmaxstdio(int newmax
)
3952 TRACE("%d\n", newmax
);
3954 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
3957 MSVCRT_max_streams
= newmax
;
3958 return MSVCRT_max_streams
;