2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
34 #include <sys/locking.h>
35 #include <sys/types.h>
36 #include <sys/utime.h>
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
71 /* for stat mode, permissions apply to all,owner and group */
72 #define ALL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
73 #define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
74 #define ALL_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
76 /* _access() bit flags FIXME: incomplete */
77 #define MSVCRT_W_OK 0x02
78 #define MSVCRT_R_OK 0x04
80 /* values for wxflag in file descriptor */
83 #define WX_READNL 0x04 /* read started with \n */
85 #define WX_DONTINHERIT 0x10
86 #define WX_APPEND 0x20
90 /* values for exflag - it's used differently in msvcr90.dll*/
93 #define EF_CRIT_INIT 0x04
94 #define EF_UNK_UNICODE 0x08
96 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
97 static char utf16_bom
[2] = { 0xff, 0xfe };
99 /* FIXME: this should be allocated dynamically */
100 #define MSVCRT_MAX_FILES 2048
101 #define MSVCRT_FD_BLOCK_SIZE 32
103 #define MSVCRT_INTERNAL_BUFSIZ 4096
105 /* ioinfo structure size is different in msvcrXX.dll's */
108 unsigned char wxflag
;
111 CRITICAL_SECTION crit
;
117 BOOL utf8translations
;
125 /*********************************************************************
126 * __pioinfo (MSVCRT.@)
127 * array of pointers to ioinfo arrays [32]
129 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
131 /*********************************************************************
132 * __badioinfo (MSVCRT.@)
134 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
138 CRITICAL_SECTION crit
;
141 FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
142 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
143 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
145 /* INTERNAL: process umask */
146 static int MSVCRT_umask
= 0;
148 /* INTERNAL: static data for tmpnam and _wtmpname functions */
149 static int tmpnam_unique
;
150 static int tmpnam_s_unique
;
152 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
153 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
154 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
155 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
157 #define TOUL(x) (ULONGLONG)(x)
158 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
159 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
160 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
161 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
163 /* This critical section protects the MSVCRT_fstreams table
164 * and MSVCRT_stream_idx from race conditions. It also
165 * protects fd critical sections creation code.
167 static CRITICAL_SECTION MSVCRT_file_cs
;
168 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
170 0, 0, &MSVCRT_file_cs
,
171 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
172 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
174 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
175 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
176 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
178 static void msvcrt_stat64_to_stat(const struct _stat64
*buf64
, struct _stat
*buf
)
180 buf
->st_dev
= buf64
->st_dev
;
181 buf
->st_ino
= buf64
->st_ino
;
182 buf
->st_mode
= buf64
->st_mode
;
183 buf
->st_nlink
= buf64
->st_nlink
;
184 buf
->st_uid
= buf64
->st_uid
;
185 buf
->st_gid
= buf64
->st_gid
;
186 buf
->st_rdev
= buf64
->st_rdev
;
187 buf
->st_size
= buf64
->st_size
;
188 buf
->st_atime
= buf64
->st_atime
;
189 buf
->st_mtime
= buf64
->st_mtime
;
190 buf
->st_ctime
= buf64
->st_ctime
;
193 static void msvcrt_stat64_to_stati64(const struct _stat64
*buf64
, struct _stati64
*buf
)
195 buf
->st_dev
= buf64
->st_dev
;
196 buf
->st_ino
= buf64
->st_ino
;
197 buf
->st_mode
= buf64
->st_mode
;
198 buf
->st_nlink
= buf64
->st_nlink
;
199 buf
->st_uid
= buf64
->st_uid
;
200 buf
->st_gid
= buf64
->st_gid
;
201 buf
->st_rdev
= buf64
->st_rdev
;
202 buf
->st_size
= buf64
->st_size
;
203 buf
->st_atime
= buf64
->st_atime
;
204 buf
->st_mtime
= buf64
->st_mtime
;
205 buf
->st_ctime
= buf64
->st_ctime
;
208 static void msvcrt_stat64_to_stat32(const struct _stat64
*buf64
, struct _stat32
*buf
)
210 buf
->st_dev
= buf64
->st_dev
;
211 buf
->st_ino
= buf64
->st_ino
;
212 buf
->st_mode
= buf64
->st_mode
;
213 buf
->st_nlink
= buf64
->st_nlink
;
214 buf
->st_uid
= buf64
->st_uid
;
215 buf
->st_gid
= buf64
->st_gid
;
216 buf
->st_rdev
= buf64
->st_rdev
;
217 buf
->st_size
= buf64
->st_size
;
218 buf
->st_atime
= buf64
->st_atime
;
219 buf
->st_mtime
= buf64
->st_mtime
;
220 buf
->st_ctime
= buf64
->st_ctime
;
223 static void msvcrt_stat64_to_stat64i32(const struct _stat64
*buf64
, struct _stat64i32
*buf
)
225 buf
->st_dev
= buf64
->st_dev
;
226 buf
->st_ino
= buf64
->st_ino
;
227 buf
->st_mode
= buf64
->st_mode
;
228 buf
->st_nlink
= buf64
->st_nlink
;
229 buf
->st_uid
= buf64
->st_uid
;
230 buf
->st_gid
= buf64
->st_gid
;
231 buf
->st_rdev
= buf64
->st_rdev
;
232 buf
->st_size
= buf64
->st_size
;
233 buf
->st_atime
= buf64
->st_atime
;
234 buf
->st_mtime
= buf64
->st_mtime
;
235 buf
->st_ctime
= buf64
->st_ctime
;
238 static void msvcrt_stat64_to_stat32i64(const struct _stat64
*buf64
, struct _stat32i64
*buf
)
240 buf
->st_dev
= buf64
->st_dev
;
241 buf
->st_ino
= buf64
->st_ino
;
242 buf
->st_mode
= buf64
->st_mode
;
243 buf
->st_nlink
= buf64
->st_nlink
;
244 buf
->st_uid
= buf64
->st_uid
;
245 buf
->st_gid
= buf64
->st_gid
;
246 buf
->st_rdev
= buf64
->st_rdev
;
247 buf
->st_size
= buf64
->st_size
;
248 buf
->st_atime
= buf64
->st_atime
;
249 buf
->st_mtime
= buf64
->st_mtime
;
250 buf
->st_ctime
= buf64
->st_ctime
;
253 static void time_to_filetime( __time64_t time
, FILETIME
*ft
)
255 /* 1601 to 1970 is 369 years plus 89 leap days */
256 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
258 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
259 ft
->dwHighDateTime
= ticks
>> 32;
260 ft
->dwLowDateTime
= ticks
;
263 static inline ioinfo
* get_ioinfo_nolock(int fd
)
266 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
267 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
269 return &MSVCRT___badioinfo
;
271 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
274 static inline void init_ioinfo_cs(ioinfo
*info
)
276 if(!(info
->exflag
& EF_CRIT_INIT
)) {
278 if(!(info
->exflag
& EF_CRIT_INIT
)) {
279 InitializeCriticalSection(&info
->crit
);
280 info
->exflag
|= EF_CRIT_INIT
;
286 static inline ioinfo
* get_ioinfo(int fd
)
288 ioinfo
*ret
= get_ioinfo_nolock(fd
);
289 if(ret
== &MSVCRT___badioinfo
)
292 EnterCriticalSection(&ret
->crit
);
296 static inline BOOL
alloc_pioinfo_block(int fd
)
301 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
307 block
= calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
310 WARN(":out of memory!\n");
314 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
315 block
[i
].handle
= INVALID_HANDLE_VALUE
;
316 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
321 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
325 ret
= get_ioinfo(fd
);
326 if(ret
!= &MSVCRT___badioinfo
)
329 if(!alloc_pioinfo_block(fd
))
330 return &MSVCRT___badioinfo
;
332 return get_ioinfo(fd
);
335 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
340 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
342 ioinfo
*info
= get_ioinfo_nolock(i
);
344 if(info
== &MSVCRT___badioinfo
)
346 if(!alloc_pioinfo_block(i
))
347 return &MSVCRT___badioinfo
;
348 info
= get_ioinfo_nolock(i
);
351 init_ioinfo_cs(info
);
352 if(TryEnterCriticalSection(&info
->crit
))
354 if(info
->handle
== INVALID_HANDLE_VALUE
)
359 LeaveCriticalSection(&info
->crit
);
363 WARN(":files exhausted!\n");
365 return &MSVCRT___badioinfo
;
368 static inline void release_ioinfo(ioinfo
*info
)
370 if(info
!=&MSVCRT___badioinfo
&& info
->exflag
& EF_CRIT_INIT
)
371 LeaveCriticalSection(&info
->crit
);
374 static inline FILE* msvcrt_get_file(int i
)
378 if(i
>= MSVCRT_max_streams
)
382 return &MSVCRT__iob
[i
];
384 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
386 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
387 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
388 ERR("out of memory\n");
393 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
395 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
400 /* INTERNAL: free a file entry fd */
401 static void msvcrt_free_fd(int fd
)
403 ioinfo
*fdinfo
= get_ioinfo(fd
);
405 if(fdinfo
!= &MSVCRT___badioinfo
)
407 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
410 TRACE(":fd (%d) freed\n",fd
);
417 SetStdHandle(STD_INPUT_HANDLE
, 0);
420 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
423 SetStdHandle(STD_ERROR_HANDLE
, 0);
427 release_ioinfo(fdinfo
);
430 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
432 fdinfo
->handle
= hand
;
433 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
434 fdinfo
->lookahead
[0] = '\n';
435 fdinfo
->lookahead
[1] = '\n';
436 fdinfo
->lookahead
[2] = '\n';
437 fdinfo
->exflag
&= EF_CRIT_INIT
;
439 if (hand
== MSVCRT_NO_CONSOLE
) hand
= 0;
440 switch (fdinfo
-MSVCRT___pioinfo
[0])
442 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
443 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
444 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
448 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
449 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
452 ioinfo
*info
= get_ioinfo_alloc(&fd
);
454 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
456 if(info
== &MSVCRT___badioinfo
)
459 msvcrt_set_fd(info
, hand
, flag
);
460 release_ioinfo(info
);
464 /* INTERNAL: Allocate a FILE* for an fd slot */
465 /* caller must hold the files lock */
466 static FILE* msvcrt_alloc_fp(void)
471 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
473 file
= msvcrt_get_file(i
);
477 if (file
->_flag
== 0)
479 if (i
== MSVCRT_stream_idx
)
481 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
483 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
484 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
495 /* INTERNAL: initialize a FILE* from an open fd */
496 static int msvcrt_init_fp(FILE* file
, int fd
, unsigned stream_flags
)
498 TRACE(":fd (%d) allocating FILE*\n",fd
);
499 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
501 WARN(":invalid fd %d\n",fd
);
506 file
->_ptr
= file
->_base
= NULL
;
509 file
->_flag
= stream_flags
;
510 file
->_tmpfname
= NULL
;
512 TRACE(":got FILE* (%p)\n",file
);
516 /* INTERNAL: Create an inheritance data block (for spawned process)
517 * The inheritance block is made of:
518 * 00 int nb of file descriptor (NBFD)
519 * 04 char file flags (wxflag): repeated for each fd
520 * 4+NBFD HANDLE file handle: repeated for each fd
522 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
529 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
530 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
534 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
535 *block
= calloc(1, *size
);
541 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
542 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
544 *(unsigned*)*block
= last_fd
;
545 for (fd
= 0; fd
< last_fd
; fd
++)
547 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
548 fdinfo
= get_ioinfo(fd
);
549 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
551 *wxflag_ptr
= fdinfo
->wxflag
;
552 *handle_ptr
= fdinfo
->handle
;
557 *handle_ptr
= INVALID_HANDLE_VALUE
;
559 release_ioinfo(fdinfo
);
560 wxflag_ptr
++; handle_ptr
++;
565 /* INTERNAL: Set up all file descriptors,
566 * as well as default streams (stdin, stderr and stdout)
568 void msvcrt_init_io(void)
574 GetStartupInfoA(&si
);
575 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
581 count
= *(unsigned*)si
.lpReserved2
;
582 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
583 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
585 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
586 count
= min(count
, MSVCRT_MAX_FILES
);
587 for (i
= 0; i
< count
; i
++)
589 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
591 fdinfo
= get_ioinfo_alloc_fd(i
);
592 if(fdinfo
!= &MSVCRT___badioinfo
)
593 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
594 release_ioinfo(fdinfo
);
597 wxflag_ptr
++; handle_ptr
++;
601 fdinfo
= get_ioinfo_alloc_fd(STDIN_FILENO
);
602 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
603 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
604 DWORD flags
= WX_OPEN
| WX_TEXT
;
605 DWORD type
= GetFileType(h
);
607 if (type
== FILE_TYPE_UNKNOWN
) {
608 h
= MSVCRT_NO_CONSOLE
;
610 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
612 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
616 msvcrt_set_fd(fdinfo
, h
, flags
);
618 release_ioinfo(fdinfo
);
620 fdinfo
= get_ioinfo_alloc_fd(STDOUT_FILENO
);
621 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
622 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
623 DWORD flags
= WX_OPEN
| WX_TEXT
;
624 DWORD type
= GetFileType(h
);
626 if (type
== FILE_TYPE_UNKNOWN
) {
627 h
= MSVCRT_NO_CONSOLE
;
629 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
631 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
635 msvcrt_set_fd(fdinfo
, h
, flags
);
637 release_ioinfo(fdinfo
);
639 fdinfo
= get_ioinfo_alloc_fd(STDERR_FILENO
);
640 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
641 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
642 DWORD flags
= WX_OPEN
| WX_TEXT
;
643 DWORD type
= GetFileType(h
);
645 if (type
== FILE_TYPE_UNKNOWN
) {
646 h
= MSVCRT_NO_CONSOLE
;
648 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
650 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
654 msvcrt_set_fd(fdinfo
, h
, flags
);
656 release_ioinfo(fdinfo
);
658 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO
)->handle
,
659 get_ioinfo_nolock(STDOUT_FILENO
)->handle
,
660 get_ioinfo_nolock(STDERR_FILENO
)->handle
);
662 memset(MSVCRT__iob
,0,3*sizeof(FILE));
663 for (i
= 0; i
< 3; i
++)
665 /* FILE structs for stdin/out/err are static and never deleted */
666 MSVCRT__iob
[i
]._file
= get_ioinfo_nolock(i
)->handle
== MSVCRT_NO_CONSOLE
?
667 MSVCRT_NO_CONSOLE_FD
: i
;
668 MSVCRT__iob
[i
]._tmpfname
= NULL
;
669 MSVCRT__iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
671 MSVCRT_stream_idx
= 3;
674 /* INTERNAL: Flush stdio file buffer */
675 static int msvcrt_flush_buffer(FILE* file
)
679 if((file
->_flag
& (_IOREAD
|_IOWRT
)) == _IOWRT
&&
680 file
->_flag
& (_IOMYBUF
|MSVCRT__USERBUF
)) {
681 int cnt
=file
->_ptr
-file
->_base
;
682 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
683 file
->_flag
|= _IOERR
;
685 } else if(file
->_flag
& _IORW
) {
686 file
->_flag
&= ~_IOWRT
;
690 file
->_ptr
=file
->_base
;
695 /*********************************************************************
698 int CDECL
_isatty(int fd
)
700 TRACE(":fd (%d)\n",fd
);
702 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
705 /* INTERNAL: Allocate stdio file buffer */
706 static BOOL
msvcrt_alloc_buffer(FILE* file
)
708 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
709 && _isatty(file
->_file
))
712 file
->_base
= calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
714 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
715 file
->_flag
|= _IOMYBUF
;
717 file
->_base
= (char*)(&file
->_charbuf
);
719 file
->_flag
|= _IONBF
;
721 file
->_ptr
= file
->_base
;
726 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
727 static BOOL
add_std_buffer(FILE *file
)
729 static char buffers
[2][BUFSIZ
];
731 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
732 || (file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
))
733 || !_isatty(file
->_file
))
736 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
737 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
738 file
->_flag
|= MSVCRT__USERBUF
;
742 /* INTERNAL: Removes temporary buffer from stdout or stderr */
743 /* Only call this function when add_std_buffer returned TRUE */
744 static void remove_std_buffer(FILE *file
)
746 msvcrt_flush_buffer(file
);
747 file
->_ptr
= file
->_base
= NULL
;
748 file
->_bufsiz
= file
->_cnt
= 0;
749 file
->_flag
&= ~MSVCRT__USERBUF
;
752 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
753 static int msvcrt_int_to_base32(int num
, char *str
)
768 *p
= (num
& 31) + '0';
770 *p
+= ('a' - '0' - 10);
777 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
778 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
793 *p
= (num
& 31) + '0';
795 *p
+= ('a' - '0' - 10);
802 /*********************************************************************
803 * __iob_func (MSVCRT.@)
806 FILE * CDECL
__iob_func(void)
808 return &MSVCRT__iob
[0];
811 #if _MSVCR_VER >= 140
812 /*********************************************************************
813 * __acrt_iob_func(UCRTBASE.@)
815 FILE * CDECL
__acrt_iob_func(unsigned idx
)
817 return &MSVCRT__iob
[idx
];
821 /*********************************************************************
824 int CDECL
_access(const char *filename
, int mode
)
826 DWORD attr
= GetFileAttributesA(filename
);
828 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
830 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
832 msvcrt_set_errno(GetLastError());
835 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
837 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
843 /*********************************************************************
844 * _access_s (MSVCRT.@)
846 int CDECL
_access_s(const char *filename
, int mode
)
848 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *_errno();
849 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *_errno();
851 if (_access(filename
, mode
) == -1)
856 /*********************************************************************
857 * _waccess (MSVCRT.@)
859 int CDECL
_waccess(const wchar_t *filename
, int mode
)
861 DWORD attr
= GetFileAttributesW(filename
);
863 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
865 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
867 msvcrt_set_errno(GetLastError());
870 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
872 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
878 /*********************************************************************
879 * _waccess_s (MSVCRT.@)
881 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
883 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *_errno();
884 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *_errno();
886 if (_waccess(filename
, mode
) == -1)
891 /*********************************************************************
894 int CDECL
_chmod(const char *path
, int flags
)
896 DWORD oldFlags
= GetFileAttributesA(path
);
898 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
900 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
901 oldFlags
| FILE_ATTRIBUTE_READONLY
;
903 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
906 msvcrt_set_errno(GetLastError());
910 /*********************************************************************
913 int CDECL
_wchmod(const wchar_t *path
, int flags
)
915 DWORD oldFlags
= GetFileAttributesW(path
);
917 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
919 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
920 oldFlags
| FILE_ATTRIBUTE_READONLY
;
922 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
925 msvcrt_set_errno(GetLastError());
929 /*********************************************************************
932 int CDECL
_unlink(const char *path
)
934 TRACE("%s\n",debugstr_a(path
));
935 if(DeleteFileA(path
))
937 TRACE("failed (%d)\n",GetLastError());
938 msvcrt_set_errno(GetLastError());
942 /*********************************************************************
943 * _wunlink (MSVCRT.@)
945 int CDECL
_wunlink(const wchar_t *path
)
947 TRACE("(%s)\n",debugstr_w(path
));
948 if(DeleteFileW(path
))
950 TRACE("failed (%d)\n",GetLastError());
951 msvcrt_set_errno(GetLastError());
955 /*********************************************************************
958 int CDECL
_commit(int fd
)
960 ioinfo
*info
= get_ioinfo(fd
);
963 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
965 if (info
->handle
== INVALID_HANDLE_VALUE
)
967 else if (!FlushFileBuffers(info
->handle
))
969 if (GetLastError() == ERROR_INVALID_HANDLE
)
971 /* FlushFileBuffers fails for console handles
972 * so we ignore this error.
978 TRACE(":failed-last error (%d)\n",GetLastError());
979 msvcrt_set_errno(GetLastError());
989 release_ioinfo(info
);
993 /* INTERNAL: Flush all stream buffer */
994 static int msvcrt_flush_all_buffers(int mask
)
996 int i
, num_flushed
= 0;
1000 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
1001 file
= msvcrt_get_file(i
);
1005 if(file
->_flag
& mask
) {
1013 TRACE(":flushed (%d) handles\n",num_flushed
);
1017 /*********************************************************************
1018 * _flushall (MSVCRT.@)
1020 int CDECL
_flushall(void)
1022 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
1025 /*********************************************************************
1028 int CDECL
fflush(FILE* file
)
1033 msvcrt_flush_all_buffers(_IOWRT
);
1037 ret
= _fflush_nolock(file
);
1044 /*********************************************************************
1045 * _fflush_nolock (MSVCRT.@)
1047 int CDECL
_fflush_nolock(FILE* file
)
1052 msvcrt_flush_all_buffers(_IOWRT
);
1056 res
= msvcrt_flush_buffer(file
);
1057 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1058 res
= _commit(file
->_file
) ? EOF
: 0;
1062 /*********************************************************************
1065 int CDECL
_close(int fd
)
1067 ioinfo
*info
= get_ioinfo(fd
);
1070 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1072 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1075 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, EBADF
)) {
1077 } else if (fd
== STDOUT_FILENO
&&
1078 info
->handle
== get_ioinfo_nolock(STDERR_FILENO
)->handle
) {
1081 } else if (fd
== STDERR_FILENO
&&
1082 info
->handle
== get_ioinfo_nolock(STDOUT_FILENO
)->handle
) {
1086 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1089 WARN(":failed-last error (%d)\n",GetLastError());
1090 msvcrt_set_errno(GetLastError());
1093 release_ioinfo(info
);
1097 /*********************************************************************
1100 * MSDN isn't clear on this point, but the remarks for _pipe
1101 * indicate file descriptors duplicated with _dup and _dup2 are always
1104 int CDECL
_dup2(int od
, int nd
)
1106 ioinfo
*info_od
, *info_nd
;
1109 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1113 info_od
= get_ioinfo(od
);
1114 info_nd
= get_ioinfo_alloc_fd(nd
);
1118 info_nd
= get_ioinfo_alloc_fd(nd
);
1119 info_od
= get_ioinfo(od
);
1122 if (info_nd
== &MSVCRT___badioinfo
)
1126 else if (info_od
->wxflag
& WX_OPEN
)
1130 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1131 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1133 int wxflag
= info_od
->wxflag
& ~_O_NOINHERIT
;
1135 if (info_nd
->wxflag
& WX_OPEN
)
1138 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1139 /* _dup2 returns 0, not nd, on success */
1145 msvcrt_set_errno(GetLastError());
1154 release_ioinfo(info_od
);
1155 release_ioinfo(info_nd
);
1159 /*********************************************************************
1162 int CDECL
_dup(int od
)
1165 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1167 if (_dup2(od
, fd
) == 0)
1171 release_ioinfo(info
);
1175 /*********************************************************************
1178 int CDECL
_eof(int fd
)
1180 ioinfo
*info
= get_ioinfo(fd
);
1181 DWORD curpos
,endpos
;
1182 LONG hcurpos
,hendpos
;
1184 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1186 if (info
->handle
== INVALID_HANDLE_VALUE
)
1188 release_ioinfo(info
);
1192 if (info
->wxflag
& WX_ATEOF
)
1194 release_ioinfo(info
);
1198 /* Otherwise we do it the hard way */
1199 hcurpos
= hendpos
= 0;
1200 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1201 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1203 if (curpos
== endpos
&& hcurpos
== hendpos
)
1205 /* FIXME: shouldn't WX_ATEOF be set here? */
1206 release_ioinfo(info
);
1210 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1211 release_ioinfo(info
);
1215 /*********************************************************************
1216 * _fcloseall (MSVCRT.@)
1218 int CDECL
_fcloseall(void)
1220 int num_closed
= 0, i
;
1224 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1225 file
= msvcrt_get_file(i
);
1227 if (file
->_flag
&& !fclose(file
))
1232 TRACE(":closed (%d) handles\n",num_closed
);
1236 /* free everything on process exit */
1237 void msvcrt_free_io(void)
1245 for(i
=0; i
<ARRAY_SIZE(MSVCRT___pioinfo
); i
++)
1247 if(!MSVCRT___pioinfo
[i
])
1250 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1252 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1253 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1255 free(MSVCRT___pioinfo
[i
]);
1258 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1260 FILE *file
= msvcrt_get_file(j
);
1261 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1263 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1264 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1268 for(i
=0; i
<ARRAY_SIZE(MSVCRT_fstream
); i
++)
1269 free(MSVCRT_fstream
[i
]);
1272 /*********************************************************************
1273 * _lseeki64 (MSVCRT.@)
1275 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1277 ioinfo
*info
= get_ioinfo(fd
);
1280 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1282 if (info
->handle
== INVALID_HANDLE_VALUE
)
1285 release_ioinfo(info
);
1289 if (whence
< 0 || whence
> 2)
1291 release_ioinfo(info
);
1296 TRACE(":fd (%d) to %s pos %s\n",
1297 fd
,wine_dbgstr_longlong(offset
),
1298 (whence
==SEEK_SET
)?"SEEK_SET":
1299 (whence
==SEEK_CUR
)?"SEEK_CUR":
1300 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1302 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1303 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1304 ofs
.QuadPart
= offset
;
1305 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1306 GetLastError() == ERROR_SUCCESS
)
1308 info
->wxflag
&= ~WX_ATEOF
;
1309 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1311 release_ioinfo(info
);
1312 return ofs
.QuadPart
;
1314 release_ioinfo(info
);
1315 TRACE(":error-last error (%d)\n",GetLastError());
1316 msvcrt_set_errno(GetLastError());
1320 /*********************************************************************
1323 __msvcrt_long CDECL
_lseek(int fd
, __msvcrt_long offset
, int whence
)
1325 return _lseeki64(fd
, offset
, whence
);
1328 /*********************************************************************
1329 * _lock_file (MSVCRT.@)
1331 void CDECL
_lock_file(FILE *file
)
1333 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1334 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1336 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1339 /*********************************************************************
1340 * _unlock_file (MSVCRT.@)
1342 void CDECL
_unlock_file(FILE *file
)
1344 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1345 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1347 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1350 /*********************************************************************
1351 * _locking (MSVCRT.@)
1353 * This is untested; the underlying LockFile doesn't work yet.
1355 int CDECL
_locking(int fd
, int mode
, __msvcrt_long nbytes
)
1357 ioinfo
*info
= get_ioinfo(fd
);
1361 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1362 if (info
->handle
== INVALID_HANDLE_VALUE
)
1364 release_ioinfo(info
);
1368 if (mode
< 0 || mode
> 4)
1370 release_ioinfo(info
);
1375 TRACE(":fd (%d) by 0x%08Ix mode %s\n",
1376 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1377 (mode
==_LK_LOCK
)?"_LK_LOCK":
1378 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1379 (mode
==_LK_RLCK
)?"_LK_RLCK":
1380 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1383 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1385 release_ioinfo(info
);
1386 FIXME ("Seek failed\n");
1387 *_errno() = EINVAL
; /* FIXME */
1390 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1393 ret
= 1; /* just to satisfy gcc */
1396 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1401 else if (mode
== _LK_UNLCK
)
1402 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1404 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1405 /* FIXME - what about error settings? */
1406 release_ioinfo(info
);
1407 return ret
? 0 : -1;
1410 /*********************************************************************
1411 * _fseeki64 (MSVCRT.@)
1413 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1418 ret
= _fseeki64_nolock(file
, offset
, whence
);
1424 /*********************************************************************
1425 * _fseeki64_nolock (MSVCRT.@)
1427 int CDECL
_fseeki64_nolock(FILE* file
, __int64 offset
, int whence
)
1431 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1433 offset
+= _ftelli64_nolock(file
);
1436 /* Flush output if needed */
1437 msvcrt_flush_buffer(file
);
1438 /* Reset direction of i/o */
1439 if(file
->_flag
& _IORW
) {
1440 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1442 /* Clear end of file flag */
1443 file
->_flag
&= ~_IOEOF
;
1444 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1449 /*********************************************************************
1452 int CDECL
fseek(FILE* file
, __msvcrt_long offset
, int whence
)
1454 return _fseeki64( file
, offset
, whence
);
1457 /*********************************************************************
1458 * _fseek_nolock (MSVCRT.@)
1460 int CDECL
_fseek_nolock(FILE* file
, __msvcrt_long offset
, int whence
)
1462 return _fseeki64_nolock( file
, offset
, whence
);
1465 /*********************************************************************
1466 * _chsize_s (MSVCRT.@)
1468 int CDECL
_chsize_s(int fd
, __int64 size
)
1474 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1476 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1479 info
= get_ioinfo(fd
);
1480 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1482 /* save the current file pointer */
1483 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1486 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1489 ret
= SetEndOfFile(info
->handle
);
1490 if (!ret
) msvcrt_set_errno(GetLastError());
1493 /* restore the file pointer */
1494 _lseeki64(fd
, cur
, SEEK_SET
);
1498 release_ioinfo(info
);
1499 return ret
? 0 : *_errno();
1502 /*********************************************************************
1503 * _chsize (MSVCRT.@)
1505 int CDECL
_chsize(int fd
, __msvcrt_long size
)
1507 /* _chsize_s returns errno on failure but _chsize should return -1 */
1508 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1511 /*********************************************************************
1512 * clearerr (MSVCRT.@)
1514 void CDECL
clearerr(FILE* file
)
1516 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1519 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1523 /*********************************************************************
1524 * clearerr_s (MSVCRT.@)
1526 int CDECL
clearerr_s(FILE* file
)
1528 TRACE(":file (%p)\n",file
);
1530 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
1533 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1538 /*********************************************************************
1541 void CDECL
rewind(FILE* file
)
1543 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1546 _fseek_nolock(file
, 0L, SEEK_SET
);
1551 static int msvcrt_get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1553 int plus
= wcschr(mode
, '+') != NULL
;
1555 TRACE("%s\n", debugstr_w(mode
));
1557 while(*mode
== ' ') mode
++;
1562 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1563 *stream_flags
= plus
? _IORW
: _IOREAD
;
1566 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1567 *stream_flags
= plus
? _IORW
: _IOWRT
;
1570 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1571 *stream_flags
= plus
? _IORW
: _IOWRT
;
1574 MSVCRT_INVALID_PMT(0, EINVAL
);
1578 *stream_flags
|= MSVCRT__commode
;
1580 while (*mode
&& *mode
!=',')
1584 *open_flags
|= _O_BINARY
;
1585 *open_flags
&= ~_O_TEXT
;
1588 *open_flags
|= _O_TEXT
;
1589 *open_flags
&= ~_O_BINARY
;
1592 *open_flags
|= _O_TEMPORARY
;
1595 *open_flags
|= _O_SHORT_LIVED
;
1598 *stream_flags
|= MSVCRT__IOCOMMIT
;
1601 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1604 *open_flags
|= _O_NOINHERIT
;
1613 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1616 ERR("incorrect mode flag: %c\n", mode
[-1]);
1623 while(*mode
== ' ') mode
++;
1624 if(!MSVCRT_CHECK_PMT(!wcsncmp(L
"ccs", mode
, 3)))
1627 while(*mode
== ' ') mode
++;
1628 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1631 while(*mode
== ' ') mode
++;
1633 if(!_wcsnicmp(L
"utf-8", mode
, 5))
1635 *open_flags
|= _O_U8TEXT
;
1638 else if(!_wcsnicmp(L
"utf-16le", mode
, 8))
1640 *open_flags
|= _O_U16TEXT
;
1643 else if(!_wcsnicmp(L
"unicode", mode
, 7))
1645 *open_flags
|= _O_WTEXT
;
1650 MSVCRT_INVALID_PMT(0, EINVAL
);
1654 while(*mode
== ' ') mode
++;
1657 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1662 /*********************************************************************
1663 * _fdopen (MSVCRT.@)
1665 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1668 wchar_t *modeW
= NULL
;
1670 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1672 ret
= _wfdopen(fd
, modeW
);
1678 /*********************************************************************
1679 * _wfdopen (MSVCRT.@)
1681 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1683 int open_flags
, stream_flags
;
1686 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1689 if (!(file
= msvcrt_alloc_fp()))
1691 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1696 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1702 /*********************************************************************
1703 * _filelength (MSVCRT.@)
1705 __msvcrt_long CDECL
_filelength(int fd
)
1707 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1710 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1713 if (endPos
!= curPos
)
1714 _lseek(fd
, curPos
, SEEK_SET
);
1721 /*********************************************************************
1722 * _filelengthi64 (MSVCRT.@)
1724 __int64 CDECL
_filelengthi64(int fd
)
1726 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1729 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1732 if (endPos
!= curPos
)
1733 _lseeki64(fd
, curPos
, SEEK_SET
);
1740 /*********************************************************************
1741 * _fileno (MSVCRT.@)
1743 int CDECL
_fileno(FILE* file
)
1745 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1749 /*********************************************************************
1750 * _fstat64 (MSVCRT.@)
1752 int CDECL
_fstat64(int fd
, struct _stat64
* buf
)
1754 ioinfo
*info
= get_ioinfo(fd
);
1757 BY_HANDLE_FILE_INFORMATION hfi
;
1759 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1760 if (info
->handle
== INVALID_HANDLE_VALUE
)
1762 release_ioinfo(info
);
1768 WARN(":failed-NULL buf\n");
1769 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1770 release_ioinfo(info
);
1774 memset(&hfi
, 0, sizeof(hfi
));
1775 memset(buf
, 0, sizeof(struct _stat64
));
1776 type
= GetFileType(info
->handle
);
1777 if (type
== FILE_TYPE_PIPE
)
1779 buf
->st_dev
= buf
->st_rdev
= fd
;
1780 buf
->st_mode
= _S_IFIFO
;
1783 else if (type
== FILE_TYPE_CHAR
)
1785 buf
->st_dev
= buf
->st_rdev
= fd
;
1786 buf
->st_mode
= _S_IFCHR
;
1789 else /* FILE_TYPE_DISK etc. */
1791 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1793 WARN(":failed-last error (%d)\n",GetLastError());
1794 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1795 release_ioinfo(info
);
1798 buf
->st_mode
= _S_IFREG
| 0444;
1799 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1800 buf
->st_mode
|= 0222;
1801 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1802 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1804 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1805 buf
->st_mtime
= buf
->st_ctime
= dw
;
1806 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1808 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1810 release_ioinfo(info
);
1814 /*********************************************************************
1815 * _fstati64 (MSVCRT.@)
1817 int CDECL
_fstati64(int fd
, struct _stati64
* buf
)
1820 struct _stat64 buf64
;
1822 ret
= _fstat64(fd
, &buf64
);
1824 msvcrt_stat64_to_stati64(&buf64
, buf
);
1828 /*********************************************************************
1831 int CDECL
_fstat(int fd
, struct _stat
* buf
)
1833 struct _stat64 buf64
;
1835 ret
= _fstat64(fd
, &buf64
);
1837 msvcrt_stat64_to_stat(&buf64
, buf
);
1841 /*********************************************************************
1842 * _fstat32 (MSVCR80.@)
1844 int CDECL
_fstat32(int fd
, struct _stat32
* buf
)
1847 struct _stat64 buf64
;
1849 ret
= _fstat64(fd
, &buf64
);
1851 msvcrt_stat64_to_stat32(&buf64
, buf
);
1855 /*********************************************************************
1856 * _fstat32i64 (MSVCR80.@)
1858 int CDECL
_fstat32i64(int fd
, struct _stat32i64
* buf
)
1861 struct _stat64 buf64
;
1863 ret
= _fstat64(fd
, &buf64
);
1865 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1869 /*********************************************************************
1870 * _fstat64i32 (MSVCR80.@)
1872 int CDECL
_fstat64i32(int fd
, struct _stat64i32
* buf
)
1875 struct _stat64 buf64
;
1877 ret
= _fstat64(fd
, &buf64
);
1879 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1883 /*********************************************************************
1884 * _futime64 (MSVCRT.@)
1886 int CDECL
_futime64(int fd
, struct __utimbuf64
*t
)
1888 ioinfo
*info
= get_ioinfo(fd
);
1893 time_to_filetime( _time64(NULL
), &at
);
1898 time_to_filetime( t
->actime
, &at
);
1899 time_to_filetime( t
->modtime
, &wt
);
1902 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1904 release_ioinfo(info
);
1905 msvcrt_set_errno(GetLastError());
1908 release_ioinfo(info
);
1912 /*********************************************************************
1913 * _futime32 (MSVCRT.@)
1915 int CDECL
_futime32(int fd
, struct __utimbuf32
*t
)
1919 struct __utimbuf64 t64
;
1920 t64
.actime
= t
->actime
;
1921 t64
.modtime
= t
->modtime
;
1922 return _futime64( fd
, &t64
);
1925 return _futime64( fd
, NULL
);
1928 /*********************************************************************
1929 * _get_osfhandle (MSVCRT.@)
1931 intptr_t CDECL
_get_osfhandle(int fd
)
1933 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1934 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1936 if(hand
== INVALID_HANDLE_VALUE
)
1938 return (intptr_t)hand
;
1941 /*********************************************************************
1942 * _mktemp_s (MSVCRT.@)
1944 int CDECL
_mktemp_s(char *pattern
, size_t size
)
1948 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1951 for(len
=0; len
<size
; len
++)
1954 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1960 for(xno
=1; xno
<=6; xno
++)
1961 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1964 id
= GetCurrentProcessId();
1965 for(xno
=1; xno
<6; xno
++) {
1966 pattern
[len
-xno
] = id
%10 + '0';
1970 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1971 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1980 /*********************************************************************
1981 * _mktemp (MSVCRT.@)
1983 char * CDECL
_mktemp(char *pattern
)
1986 char *retVal
= pattern
;
1994 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1998 id
= GetCurrentProcessId();
2002 int tempNum
= id
/ 10;
2003 *pattern
-- = id
- (tempNum
* 10) + '0';
2009 *pattern
= letter
++;
2010 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2012 } while(letter
<= 'z');
2016 /*********************************************************************
2017 * _wmktemp_s (MSVCRT.@)
2019 int CDECL
_wmktemp_s(wchar_t *pattern
, size_t size
)
2023 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2026 for(len
=0; len
<size
; len
++)
2029 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2035 for(xno
=1; xno
<=6; xno
++)
2036 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2039 id
= GetCurrentProcessId();
2040 for(xno
=1; xno
<6; xno
++) {
2041 pattern
[len
-xno
] = id
%10 + '0';
2045 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2046 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2055 /*********************************************************************
2056 * _wmktemp (MSVCRT.@)
2058 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
2061 wchar_t *retVal
= pattern
;
2063 wchar_t letter
= 'a';
2069 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2073 id
= GetCurrentProcessId();
2077 int tempNum
= id
/ 10;
2078 *pattern
-- = id
- (tempNum
* 10) + '0';
2084 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2086 *pattern
= letter
++;
2087 } while(letter
!= '|');
2091 static unsigned split_oflags(unsigned oflags
)
2094 unsigned unsupp
; /* until we support everything */
2096 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
2097 if (oflags
& _O_BINARY
) {/* Nothing to do */}
2098 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
2099 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
2100 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
2101 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
2106 if (!(fmode
& _O_BINARY
)) wxflags
|= WX_TEXT
; /* default to TEXT*/
2108 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2110 if ((unsupp
= oflags
& ~(_O_BINARY
| _O_TEXT
| _O_APPEND
| _O_TRUNC
| _O_EXCL
| _O_CREAT
|
2111 _O_RDWR
| _O_WRONLY
| _O_TEMPORARY
| _O_NOINHERIT
| _O_SEQUENTIAL
|
2112 _O_RANDOM
| _O_SHORT_LIVED
| _O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
)))
2113 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2118 /*********************************************************************
2121 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
2124 SECURITY_ATTRIBUTES sa
;
2125 HANDLE readHandle
, writeHandle
;
2133 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2134 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
2135 sa
.lpSecurityDescriptor
= NULL
;
2136 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2138 unsigned int wxflags
= split_oflags(textmode
);
2141 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2145 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2154 CloseHandle(writeHandle
);
2160 CloseHandle(readHandle
);
2161 CloseHandle(writeHandle
);
2166 msvcrt_set_errno(GetLastError());
2171 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2173 char bom
[sizeof(utf8_bom
)];
2176 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2179 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2180 oflags
= (oflags
& ~(_O_WTEXT
| _O_U16TEXT
)) | _O_U8TEXT
;
2181 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2183 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2184 oflags
= (oflags
& ~(_O_WTEXT
| _O_U8TEXT
)) | _O_U16TEXT
;
2186 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2192 /*********************************************************************
2193 * _wsopen_dispatch (UCRTBASE.@)
2195 int CDECL
_wsopen_dispatch( const wchar_t* path
, int oflags
, int shflags
, int pmode
,
2196 int *fd
, int secure
)
2198 DWORD access
= 0, creation
= 0, attrib
;
2199 SECURITY_ATTRIBUTES sa
;
2200 DWORD sharing
, type
;
2204 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2205 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2207 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
2210 wxflag
= split_oflags(oflags
);
2211 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
2213 case _O_RDONLY
: access
|= GENERIC_READ
; break;
2214 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
2215 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2218 if (oflags
& _O_CREAT
)
2220 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(_S_IREAD
| _S_IWRITE
))))
2223 if (oflags
& _O_EXCL
)
2224 creation
= CREATE_NEW
;
2225 else if (oflags
& _O_TRUNC
)
2226 creation
= CREATE_ALWAYS
;
2228 creation
= OPEN_ALWAYS
;
2230 else /* no _O_CREAT */
2232 if (oflags
& _O_TRUNC
)
2233 creation
= TRUNCATE_EXISTING
;
2235 creation
= OPEN_EXISTING
;
2244 sharing
= FILE_SHARE_READ
;
2247 sharing
= FILE_SHARE_WRITE
;
2250 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2253 ERR( "Unhandled shflags 0x%x\n", shflags
);
2257 if (!(pmode
& ~MSVCRT_umask
& _S_IWRITE
))
2258 attrib
= FILE_ATTRIBUTE_READONLY
;
2260 attrib
= FILE_ATTRIBUTE_NORMAL
;
2262 if (oflags
& _O_TEMPORARY
)
2264 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2266 sharing
|= FILE_SHARE_DELETE
;
2269 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2270 sa
.lpSecurityDescriptor
= NULL
;
2271 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
2273 if ((oflags
& (_O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
))
2274 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2275 && !(access
&GENERIC_READ
))
2277 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2278 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2279 if (hand
!= INVALID_HANDLE_VALUE
)
2281 oflags
= check_bom(hand
, oflags
, FALSE
);
2286 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2287 if (hand
== INVALID_HANDLE_VALUE
) {
2288 WARN(":failed-last error (%d)\n",GetLastError());
2289 msvcrt_set_errno(GetLastError());
2293 if (oflags
& (_O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
))
2295 LARGE_INTEGER size
= {{0}};
2297 if ((access
& GENERIC_WRITE
) && (creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
))
2298 GetFileSizeEx(hand
, &size
);
2300 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2301 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2302 || ((creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
) && !size
.QuadPart
)))
2304 if (oflags
& _O_U8TEXT
)
2306 DWORD written
= 0, tmp
;
2308 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2309 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2311 if (written
!= sizeof(utf8_bom
)) {
2312 WARN("error writing BOM\n");
2314 msvcrt_set_errno(GetLastError());
2320 DWORD written
= 0, tmp
;
2322 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2323 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2325 if (written
!= sizeof(utf16_bom
))
2327 WARN("error writing BOM\n");
2329 msvcrt_set_errno(GetLastError());
2332 oflags
|= _O_U16TEXT
;
2335 else if (access
& GENERIC_READ
)
2336 oflags
= check_bom(hand
, oflags
, TRUE
);
2339 type
= GetFileType(hand
);
2340 if (type
== FILE_TYPE_CHAR
)
2342 else if (type
== FILE_TYPE_PIPE
)
2345 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2349 if (oflags
& _O_WTEXT
)
2350 get_ioinfo_nolock(*fd
)->exflag
|= EF_UNK_UNICODE
;
2352 if (oflags
& _O_U16TEXT
)
2353 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2354 else if (oflags
& _O_U8TEXT
)
2355 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2357 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2362 /*********************************************************************
2363 * _wsopen_s (MSVCRT.@)
2365 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
2367 return _wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2370 /*********************************************************************
2371 * _wsopen (MSVCRT.@)
2373 int WINAPIV
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
2378 if (oflags
& _O_CREAT
)
2382 __ms_va_start(ap
, shflags
);
2383 pmode
= va_arg(ap
, int);
2389 return _wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2393 /*********************************************************************
2394 * _sopen_dispatch (UCRTBASE.@)
2396 int CDECL
_sopen_dispatch( const char *path
, int oflags
, int shflags
,
2397 int pmode
, int *fd
, int secure
)
2402 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2405 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2408 ret
= _wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2413 /*********************************************************************
2414 * _sopen_s (MSVCRT.@)
2416 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2418 return _sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2421 /*********************************************************************
2424 int WINAPIV
_sopen( const char *path
, int oflags
, int shflags
, ... )
2429 if (oflags
& _O_CREAT
)
2433 __ms_va_start(ap
, shflags
);
2434 pmode
= va_arg(ap
, int);
2440 return _sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2443 /*********************************************************************
2446 int WINAPIV
_open( const char *path
, int flags
, ... )
2450 if (flags
& _O_CREAT
)
2453 __ms_va_start(ap
, flags
);
2454 pmode
= va_arg(ap
, int);
2456 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
2459 return _sopen( path
, flags
, _SH_DENYNO
);
2462 /*********************************************************************
2465 int WINAPIV
_wopen(const wchar_t *path
,int flags
,...)
2469 if (flags
& _O_CREAT
)
2472 __ms_va_start(ap
, flags
);
2473 pmode
= va_arg(ap
, int);
2475 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
2478 return _wsopen( path
, flags
, _SH_DENYNO
);
2481 /*********************************************************************
2484 int CDECL
_creat(const char *path
, int pmode
)
2486 int flags
= _O_CREAT
| _O_TRUNC
| _O_RDWR
;
2487 return _open(path
, flags
, pmode
);
2490 /*********************************************************************
2491 * _wcreat (MSVCRT.@)
2493 int CDECL
_wcreat(const wchar_t *path
, int pmode
)
2495 int flags
= _O_CREAT
| _O_TRUNC
| _O_RDWR
;
2496 return _wopen(path
, flags
, pmode
);
2499 /*********************************************************************
2500 * _open_osfhandle (MSVCRT.@)
2502 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2507 /* _O_RDONLY (0) always matches, so set the read flag
2508 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2509 * file, so set the write flag. It also only sets _O_TEXT if it wants
2510 * text - it never sets _O_BINARY.
2512 /* don't let split_oflags() decide the mode if no mode is passed */
2513 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2514 oflags
|= _O_BINARY
;
2516 flags
= GetFileType((HANDLE
)handle
);
2517 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2519 msvcrt_set_errno(GetLastError());
2523 if (flags
== FILE_TYPE_CHAR
)
2525 else if (flags
== FILE_TYPE_PIPE
)
2529 flags
|= split_oflags(oflags
);
2531 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2532 TRACE(":handle (%Iu) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2536 /*********************************************************************
2539 int CDECL
_rmtmp(void)
2541 int num_removed
= 0, i
;
2545 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2546 file
= msvcrt_get_file(i
);
2548 if (file
->_tmpfname
)
2557 TRACE(":removed (%d) temp files\n",num_removed
);
2561 static inline int get_utf8_char_len(char ch
)
2563 if((ch
&0xf8) == 0xf0)
2565 else if((ch
&0xf0) == 0xe0)
2567 else if((ch
&0xe0) == 0xc0)
2572 /*********************************************************************
2573 * (internal) read_utf8
2575 static int read_utf8(ioinfo
*fdinfo
, wchar_t *buf
, unsigned int count
)
2577 HANDLE hand
= fdinfo
->handle
;
2578 char min_buf
[4], *readbuf
, lookahead
;
2579 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2581 /* make the buffer big enough to hold at least one character */
2582 /* read bytes have to fit to output and lookahead buffers */
2584 readbuf_size
= count
< 4 ? 4 : count
;
2585 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2590 if(fdinfo
->lookahead
[0] != '\n') {
2591 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2592 fdinfo
->lookahead
[0] = '\n';
2594 if(fdinfo
->lookahead
[1] != '\n') {
2595 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2596 fdinfo
->lookahead
[1] = '\n';
2598 if(fdinfo
->lookahead
[2] != '\n') {
2599 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2600 fdinfo
->lookahead
[2] = '\n';
2605 /* NOTE: this case is broken in native dll, reading
2606 * sometimes fails when small buffer is passed
2609 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2610 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2611 fdinfo
->wxflag
|= WX_ATEOF
;
2614 msvcrt_set_errno(GetLastError());
2617 }else if(!num_read
) {
2618 fdinfo
->wxflag
|= WX_ATEOF
;
2624 char_len
= get_utf8_char_len(readbuf
[0]);
2626 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2630 if(readbuf
[0] == '\n')
2631 fdinfo
->wxflag
|= WX_READNL
;
2633 fdinfo
->wxflag
&= ~WX_READNL
;
2635 if(readbuf
[0] == 0x1a) {
2636 fdinfo
->wxflag
|= WX_ATEOF
;
2640 if(readbuf
[0] == '\r') {
2641 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2643 else if(lookahead
== '\n')
2647 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2648 fdinfo
->lookahead
[0] = lookahead
;
2650 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2655 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2656 msvcrt_set_errno(GetLastError());
2663 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2666 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2667 fdinfo
->wxflag
|= WX_ATEOF
;
2668 if (readbuf
!= min_buf
) free(readbuf
);
2671 msvcrt_set_errno(GetLastError());
2672 if (readbuf
!= min_buf
) free(readbuf
);
2675 }else if(!pos
&& !num_read
) {
2676 fdinfo
->wxflag
|= WX_ATEOF
;
2677 if (readbuf
!= min_buf
) free(readbuf
);
2682 if(readbuf
[0] == '\n')
2683 fdinfo
->wxflag
|= WX_READNL
;
2685 fdinfo
->wxflag
&= ~WX_READNL
;
2687 /* Find first byte of last character (may be incomplete) */
2688 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2689 if((readbuf
[i
]&0xc0) != 0x80)
2691 char_len
= get_utf8_char_len(readbuf
[i
]);
2692 if(char_len
+i
<= pos
)
2695 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2697 fdinfo
->lookahead
[0] = readbuf
[i
];
2699 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2701 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2703 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2707 for(i
=0, j
=0; i
<pos
; i
++) {
2708 if(readbuf
[i
] == 0x1a) {
2709 fdinfo
->wxflag
|= WX_ATEOF
;
2713 /* strip '\r' if followed by '\n' */
2714 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2715 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2716 readbuf
[j
++] = '\r';
2717 }else if(lookahead
== '\n' && j
==0) {
2718 readbuf
[j
++] = '\n';
2720 if(lookahead
!= '\n')
2721 readbuf
[j
++] = '\r';
2723 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2724 fdinfo
->lookahead
[0] = lookahead
;
2726 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2728 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2729 readbuf
[j
++] = readbuf
[i
];
2734 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2735 msvcrt_set_errno(GetLastError());
2736 if (readbuf
!= min_buf
) free(readbuf
);
2740 if (readbuf
!= min_buf
) free(readbuf
);
2744 /*********************************************************************
2747 * When reading \r as last character in text mode, read() positions
2748 * the file pointer on the \r character while getc() goes on to
2751 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2753 DWORD num_read
, utf16
;
2754 char *bufstart
= buf
;
2759 if (fdinfo
->wxflag
& WX_ATEOF
) {
2760 TRACE("already at EOF, returning 0\n");
2763 /* Don't trace small reads, it gets *very* annoying */
2765 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2766 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2772 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2773 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2779 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2780 return read_utf8(fdinfo
, buf
, count
);
2782 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2784 if (fdinfo
->lookahead
[0] != '\n')
2786 bufstart
[0] = fdinfo
->lookahead
[0];
2787 fdinfo
->lookahead
[0] = '\n';
2791 bufstart
[1] = fdinfo
->lookahead
[1];
2792 fdinfo
->lookahead
[1] = '\n';
2795 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2796 num_read
+= 1+utf16
;
2801 if(utf16
&& (num_read
&1))
2803 /* msvcr90 uses uninitialized value from the buffer in this case */
2804 /* msvcrt ignores additional data */
2805 ERR("got odd number of bytes in UTF16 mode\n");
2809 if (count
!= 0 && num_read
== 0)
2811 fdinfo
->wxflag
|= WX_ATEOF
;
2812 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2814 else if (fdinfo
->wxflag
& WX_TEXT
)
2818 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2819 fdinfo
->wxflag
|= WX_READNL
;
2821 fdinfo
->wxflag
&= ~WX_READNL
;
2823 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2825 /* in text mode, a ctrl-z signals EOF */
2826 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2828 fdinfo
->wxflag
|= WX_ATEOF
;
2829 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2833 /* in text mode, strip \r if followed by \n */
2834 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2839 lookahead
[1] = '\n';
2840 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2842 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2844 bufstart
[j
++] = '\n';
2845 if(utf16
) bufstart
[j
++] = 0;
2849 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2851 bufstart
[j
++] = '\r';
2852 if(utf16
) bufstart
[j
++] = 0;
2855 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2857 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2859 bufstart
[j
++] = '\n';
2860 if (utf16
) bufstart
[j
++] = 0;
2864 fdinfo
->lookahead
[0] = lookahead
[0];
2865 fdinfo
->lookahead
[1] = lookahead
[1];
2869 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2874 bufstart
[j
++] = '\r';
2875 if(utf16
) bufstart
[j
++] = 0;
2878 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2879 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2881 bufstart
[j
++] = bufstart
[i
];
2882 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2890 if (GetLastError() == ERROR_BROKEN_PIPE
)
2892 TRACE(":end-of-pipe\n");
2893 fdinfo
->wxflag
|= WX_ATEOF
;
2898 TRACE(":failed-last error (%d)\n",GetLastError());
2899 msvcrt_set_errno(GetLastError());
2905 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2909 /*********************************************************************
2912 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2917 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
2922 info
= get_ioinfo(fd
);
2923 num_read
= read_i(fd
, info
, buf
, count
);
2924 release_ioinfo(info
);
2928 /*********************************************************************
2929 * _setmode (MSVCRT.@)
2931 int CDECL
_setmode(int fd
,int mode
)
2933 ioinfo
*info
= get_ioinfo(fd
);
2934 int ret
= info
->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2935 if(ret
==_O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2938 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2939 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2941 release_ioinfo(info
);
2945 if(info
== &MSVCRT___badioinfo
) {
2950 if(mode
== _O_BINARY
) {
2951 info
->wxflag
&= ~WX_TEXT
;
2952 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2953 release_ioinfo(info
);
2957 info
->wxflag
|= WX_TEXT
;
2959 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2960 else if(mode
== _O_U8TEXT
)
2961 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2963 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2965 release_ioinfo(info
);
2969 /*********************************************************************
2970 * _stat64 (MSVCRT.@)
2972 int CDECL
_stat64(const char* path
, struct _stat64
* buf
)
2975 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2976 unsigned short mode
= ALL_S_IREAD
;
2979 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2981 plen
= strlen(path
);
2982 while (plen
&& path
[plen
-1]==' ')
2985 if (plen
==2 && path
[1]==':')
2992 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2999 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
3001 TRACE("failed (%d)\n",GetLastError());
3006 memset(buf
,0,sizeof(struct _stat64
));
3008 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3009 Bon 011120: This FIXME seems incorrect
3010 Also a letter as first char isn't enough to be classified
3013 if (isalpha(*path
)&& (*(path
+1)==':'))
3014 buf
->st_dev
= buf
->st_rdev
= _toupper_l(*path
, NULL
) - 'A'; /* drive num */
3016 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
3018 /* Dir, or regular file? */
3019 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3020 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
3025 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3027 unsigned int ext
= _tolower_l(path
[plen
-1], NULL
) |
3028 (_tolower_l(path
[plen
-2], NULL
) << 8) |
3029 (_tolower_l(path
[plen
-3], NULL
) << 16);
3030 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3031 mode
|= ALL_S_IEXEC
;
3035 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3036 mode
|= ALL_S_IWRITE
;
3038 buf
->st_mode
= mode
;
3040 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3041 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3043 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3044 buf
->st_mtime
= buf
->st_ctime
= dw
;
3045 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3046 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3047 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3051 /*********************************************************************
3052 * _stati64 (MSVCRT.@)
3054 int CDECL
_stati64(const char* path
, struct _stati64
* buf
)
3057 struct _stat64 buf64
;
3059 ret
= _stat64(path
, &buf64
);
3061 msvcrt_stat64_to_stati64(&buf64
, buf
);
3065 /*********************************************************************
3068 int CDECL
_stat(const char* path
, struct _stat
* buf
)
3071 struct _stat64 buf64
;
3073 ret
= _stat64( path
, &buf64
);
3075 msvcrt_stat64_to_stat(&buf64
, buf
);
3079 #if _MSVCR_VER >= 80
3081 /*********************************************************************
3082 * _stat32 (MSVCR80.@)
3084 int CDECL
_stat32(const char *path
, struct _stat32
*buf
)
3087 struct _stat64 buf64
;
3089 ret
= _stat64(path
, &buf64
);
3091 msvcrt_stat64_to_stat32(&buf64
, buf
);
3095 /*********************************************************************
3096 * _stat32i64 (MSVCR80.@)
3098 int CDECL
_stat32i64(const char *path
, struct _stat32i64
*buf
)
3101 struct _stat64 buf64
;
3103 ret
= _stat64(path
, &buf64
);
3105 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3109 /*********************************************************************
3110 * _stat64i32 (MSVCR80.@)
3112 int CDECL
_stat64i32(const char* path
, struct _stat64i32
*buf
)
3115 struct _stat64 buf64
;
3117 ret
= _stat64(path
, &buf64
);
3119 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3123 #endif /* _MSVCR_VER >= 80 */
3125 /*********************************************************************
3126 * _wstat64 (MSVCRT.@)
3128 int CDECL
_wstat64(const wchar_t* path
, struct _stat64
* buf
)
3131 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3132 unsigned short mode
= ALL_S_IREAD
;
3135 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3137 plen
= wcslen(path
);
3138 while (plen
&& path
[plen
-1]==' ')
3141 if (plen
==2 && path
[1]==':')
3148 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3155 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3157 TRACE("failed (%d)\n",GetLastError());
3162 memset(buf
,0,sizeof(struct _stat64
));
3164 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3165 if (iswalpha(*path
) && path
[1] == ':')
3166 buf
->st_dev
= buf
->st_rdev
= towupper(*path
) - 'A'; /* drive num */
3168 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
3170 /* Dir, or regular file? */
3171 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3172 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
3177 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3179 ULONGLONG ext
= towlower(path
[plen
-1]) | (towlower(path
[plen
-2]) << 16) |
3180 ((ULONGLONG
)towlower(path
[plen
-3]) << 32);
3181 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3182 mode
|= ALL_S_IEXEC
;
3186 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3187 mode
|= ALL_S_IWRITE
;
3189 buf
->st_mode
= mode
;
3191 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3192 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3194 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3195 buf
->st_mtime
= buf
->st_ctime
= dw
;
3196 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3197 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3198 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3202 /*********************************************************************
3203 * _wstati64 (MSVCRT.@)
3205 int CDECL
_wstati64(const wchar_t* path
, struct _stati64
* buf
)
3208 struct _stat64 buf64
;
3210 ret
= _wstat64(path
, &buf64
);
3212 msvcrt_stat64_to_stati64(&buf64
, buf
);
3216 /*********************************************************************
3219 int CDECL
_wstat(const wchar_t* path
, struct _stat
* buf
)
3222 struct _stat64 buf64
;
3224 ret
= _wstat64( path
, &buf64
);
3225 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3229 #if _MSVCR_VER >= 80
3231 /*********************************************************************
3232 * _wstat32 (MSVCR80.@)
3234 int CDECL
_wstat32(const wchar_t *path
, struct _stat32
*buf
)
3237 struct _stat64 buf64
;
3239 ret
= _wstat64(path
, &buf64
);
3241 msvcrt_stat64_to_stat32(&buf64
, buf
);
3245 /*********************************************************************
3246 * _wstat32i64 (MSVCR80.@)
3248 int CDECL
_wstat32i64(const wchar_t *path
, struct _stat32i64
*buf
)
3251 struct _stat64 buf64
;
3253 ret
= _wstat64(path
, &buf64
);
3255 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3259 /*********************************************************************
3260 * _wstat64i32 (MSVCR80.@)
3262 int CDECL
_wstat64i32(const wchar_t *path
, struct _stat64i32
*buf
)
3265 struct _stat64 buf64
;
3267 ret
= _wstat64(path
, &buf64
);
3269 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3273 #endif /* _MSVCR_VER >= 80 */
3275 /*********************************************************************
3278 __msvcrt_long CDECL
_tell(int fd
)
3280 return _lseek(fd
, 0, SEEK_CUR
);
3283 /*********************************************************************
3284 * _telli64 (MSVCRT.@)
3286 __int64 CDECL
_telli64(int fd
)
3288 return _lseeki64(fd
, 0, SEEK_CUR
);
3291 /*********************************************************************
3292 * _tempnam (MSVCRT.@)
3294 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
3296 char tmpbuf
[MAX_PATH
];
3297 const char *tmp_dir
= getenv("TMP");
3299 if (tmp_dir
) dir
= tmp_dir
;
3301 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3302 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3304 TRACE("got name (%s)\n",tmpbuf
);
3305 DeleteFileA(tmpbuf
);
3306 return _strdup(tmpbuf
);
3308 TRACE("failed (%d)\n",GetLastError());
3312 /*********************************************************************
3313 * _wtempnam (MSVCRT.@)
3315 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
3317 wchar_t tmpbuf
[MAX_PATH
];
3318 const wchar_t *tmp_dir
= _wgetenv(L
"TMP");
3320 if (tmp_dir
) dir
= tmp_dir
;
3322 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3323 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3325 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3326 DeleteFileW(tmpbuf
);
3327 return _wcsdup(tmpbuf
);
3329 TRACE("failed (%d)\n",GetLastError());
3333 /*********************************************************************
3336 int CDECL
_umask(int umask
)
3338 int old_umask
= MSVCRT_umask
;
3339 TRACE("(%d)\n",umask
);
3340 MSVCRT_umask
= umask
;
3344 /*********************************************************************
3345 * _utime64 (MSVCRT.@)
3347 int CDECL
_utime64(const char* path
, struct __utimbuf64
*t
)
3349 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
3353 int retVal
= _futime64(fd
, t
);
3360 /*********************************************************************
3361 * _utime32 (MSVCRT.@)
3363 int CDECL
_utime32(const char* path
, struct __utimbuf32
*t
)
3367 struct __utimbuf64 t64
;
3368 t64
.actime
= t
->actime
;
3369 t64
.modtime
= t
->modtime
;
3370 return _utime64( path
, &t64
);
3373 return _utime64( path
, NULL
);
3376 /*********************************************************************
3377 * _wutime64 (MSVCRT.@)
3379 int CDECL
_wutime64(const wchar_t* path
, struct __utimbuf64
*t
)
3381 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
3385 int retVal
= _futime64(fd
, t
);
3392 /*********************************************************************
3393 * _wutime32 (MSVCRT.@)
3395 int CDECL
_wutime32(const wchar_t* path
, struct __utimbuf32
*t
)
3399 struct __utimbuf64 t64
;
3400 t64
.actime
= t
->actime
;
3401 t64
.modtime
= t
->modtime
;
3402 return _wutime64( path
, &t64
);
3405 return _wutime64( path
, NULL
);
3408 /*********************************************************************
3411 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
3413 ioinfo
*info
= get_ioinfo(fd
);
3414 HANDLE hand
= info
->handle
;
3415 DWORD num_written
, i
;
3416 BOOL console
= FALSE
;
3418 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3421 release_ioinfo(info
);
3425 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3428 release_ioinfo(info
);
3432 /* If appending, go to EOF */
3433 if (info
->wxflag
& WX_APPEND
)
3434 _lseek(fd
, 0, FILE_END
);
3436 if (!(info
->wxflag
& WX_TEXT
))
3438 if (!WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3439 || num_written
!= count
)
3441 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3442 hand
, GetLastError());
3443 msvcrt_set_errno(GetLastError());
3447 release_ioinfo(info
);
3451 if (_isatty(fd
)) console
= VerifyConsoleIoHandle(hand
);
3452 for (i
= 0; i
< count
;)
3454 const char *s
= buf
;
3458 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)) && console
)
3460 char conv
[sizeof(lfbuf
)];
3463 #if _MSVCR_VER >= 90
3464 if (info
->dbcsBufferUsed
)
3466 conv
[j
++] = info
->dbcsBuffer
;
3467 info
->dbcsBufferUsed
= FALSE
;
3473 for (; i
< count
&& j
< sizeof(conv
)-1 &&
3474 len
< (sizeof(lfbuf
) - 1) / sizeof(WCHAR
); i
++, j
++, len
++)
3476 if (isleadbyte((unsigned char)s
[i
]))
3482 #if _MSVCR_VER >= 90
3483 info
->dbcsBuffer
= conv
[j
-1];
3484 info
->dbcsBufferUsed
= TRUE
;
3488 release_ioinfo(info
);
3493 else if (s
[i
] == '\n')
3501 len
= mbstowcs((WCHAR
*)lfbuf
, conv
, len
);
3504 msvcrt_set_errno(GetLastError());
3505 release_ioinfo(info
);
3510 else if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3512 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-1; i
++, j
++)
3519 else if (info
->exflag
& EF_UTF16
|| console
)
3521 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-3; i
++, j
++)
3523 if (s
[i
] == '\n' && !s
[i
+1])
3528 lfbuf
[j
++] = s
[i
++];
3534 char conv
[sizeof(lfbuf
)/4];
3536 for (j
= 0; i
< count
&& j
< sizeof(conv
)-3; i
++, j
++)
3538 if (s
[i
] == '\n' && !s
[i
+1])
3547 j
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)conv
, j
/2, lfbuf
, sizeof(lfbuf
), NULL
, NULL
);
3550 msvcrt_set_errno(GetLastError());
3551 release_ioinfo(info
);
3559 if (!WriteConsoleW(hand
, lfbuf
, j
, &num_written
, NULL
))
3562 else if (!WriteFile(hand
, lfbuf
, j
, &num_written
, NULL
))
3567 if (num_written
!= j
)
3569 TRACE("WriteFile/WriteConsoleW (fd %d, hand %p) failed-last error (%d)\n", fd
,
3570 hand
, GetLastError());
3571 msvcrt_set_errno(GetLastError());
3572 release_ioinfo(info
);
3577 release_ioinfo(info
);
3581 /*********************************************************************
3584 int CDECL
_putw(int val
, FILE* file
)
3589 len
= _write(file
->_file
, &val
, sizeof(val
));
3590 if (len
== sizeof(val
)) {
3595 file
->_flag
|= _IOERR
;
3600 /*********************************************************************
3603 int CDECL
fclose(FILE* file
)
3608 ret
= _fclose_nolock(file
);
3614 /*********************************************************************
3615 * _fclose_nolock (MSVCRT.@)
3617 int CDECL
_fclose_nolock(FILE* file
)
3621 if(!(file
->_flag
& (_IOREAD
| _IOWRT
| _IORW
)))
3628 free(file
->_tmpfname
);
3629 file
->_tmpfname
= NULL
;
3630 /* flush stdio buffers */
3631 if(file
->_flag
& _IOWRT
)
3632 _fflush_nolock(file
);
3633 if(file
->_flag
& _IOMYBUF
)
3636 r
=_close(file
->_file
);
3639 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
3642 /*********************************************************************
3645 int CDECL
feof(FILE* file
)
3647 return file
->_flag
& _IOEOF
;
3650 /*********************************************************************
3653 int CDECL
ferror(FILE* file
)
3655 return file
->_flag
& _IOERR
;
3658 /*********************************************************************
3659 * _filbuf (MSVCRT.@)
3661 int CDECL
_filbuf(FILE* file
)
3665 if(file
->_flag
& _IOSTRG
)
3668 /* Allocate buffer if needed */
3669 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
)))
3670 msvcrt_alloc_buffer(file
);
3672 if(!(file
->_flag
& _IOREAD
)) {
3673 if(file
->_flag
& _IORW
)
3674 file
->_flag
|= _IOREAD
;
3679 if(!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
))) {
3681 if ((r
= _read(file
->_file
,&c
,1)) != 1) {
3682 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
3688 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3690 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
3696 file
->_ptr
= file
->_base
+1;
3697 c
= *(unsigned char *)file
->_base
;
3702 /*********************************************************************
3705 int CDECL
fgetc(FILE* file
)
3710 ret
= _fgetc_nolock(file
);
3716 /*********************************************************************
3717 * _fgetc_nolock (MSVCRT.@)
3719 int CDECL
_fgetc_nolock(FILE* file
)
3726 i
= (unsigned char *)file
->_ptr
++;
3734 /*********************************************************************
3735 * _fgetchar (MSVCRT.@)
3737 int CDECL
_fgetchar(void)
3739 return fgetc(MSVCRT_stdin
);
3742 /*********************************************************************
3745 char * CDECL
fgets(char *s
, int size
, FILE* file
)
3748 char * buf_start
= s
;
3750 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3751 file
,file
->_file
,s
,size
);
3755 while ((size
>1) && (cc
= _fgetc_nolock(file
)) != EOF
&& cc
!= '\n')
3760 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3762 TRACE(":nothing read\n");
3766 if ((cc
!= EOF
) && (size
> 1))
3769 TRACE(":got %s\n", debugstr_a(buf_start
));
3774 /*********************************************************************
3777 wint_t CDECL
fgetwc(FILE* file
)
3782 ret
= _fgetwc_nolock(file
);
3788 /*********************************************************************
3789 * _fgetwc_nolock (MSVCRT.@)
3791 wint_t CDECL
_fgetwc_nolock(FILE* file
)
3796 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3797 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3800 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
3801 ch
= _fgetc_nolock(file
);
3809 char mbs
[MB_LEN_MAX
];
3812 ch
= _fgetc_nolock(file
);
3815 if(isleadbyte((unsigned char)mbs
[0])) {
3816 ch
= _fgetc_nolock(file
);
3826 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
3833 /*********************************************************************
3836 int CDECL
_getw(FILE* file
)
3844 for (j
=0; j
<sizeof(int); j
++) {
3845 k
= _fgetc_nolock(file
);
3847 file
->_flag
|= _IOEOF
;
3858 /*********************************************************************
3861 wint_t CDECL
getwc(FILE* file
)
3863 return fgetwc(file
);
3866 /*********************************************************************
3867 * _fgetwchar (MSVCRT.@)
3869 wint_t CDECL
_fgetwchar(void)
3871 return fgetwc(MSVCRT_stdin
);
3874 /*********************************************************************
3875 * getwchar (MSVCRT.@)
3877 wint_t CDECL
getwchar(void)
3879 return _fgetwchar();
3882 /*********************************************************************
3885 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
3888 wchar_t * buf_start
= s
;
3890 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3891 file
,file
->_file
,s
,size
);
3895 while ((size
>1) && (cc
= _fgetwc_nolock(file
)) != WEOF
&& cc
!= '\n')
3900 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3902 TRACE(":nothing read\n");
3906 if ((cc
!= WEOF
) && (size
> 1))
3909 TRACE(":got %s\n", debugstr_w(buf_start
));
3914 /*********************************************************************
3915 * _flsbuf (MSVCRT.@)
3917 int CDECL
_flsbuf(int c
, FILE* file
)
3919 /* Flush output buffer */
3920 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
))) {
3921 msvcrt_alloc_buffer(file
);
3924 if(!(file
->_flag
& _IOWRT
)) {
3925 if(!(file
->_flag
& _IORW
)) {
3926 file
->_flag
|= _IOERR
;
3930 file
->_flag
|= _IOWRT
;
3932 if(file
->_flag
& _IOREAD
) {
3933 if(!(file
->_flag
& _IOEOF
)) {
3934 file
->_flag
|= _IOERR
;
3938 file
->_ptr
= file
->_base
;
3939 file
->_flag
&= ~(_IOREAD
| _IOEOF
);
3942 if(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) {
3945 if(file
->_cnt
<= 0) {
3946 res
= msvcrt_flush_buffer(file
);
3949 file
->_flag
|= _IOWRT
;
3950 file
->_cnt
=file
->_bufsiz
;
3958 /* set _cnt to 0 for unbuffered FILEs */
3960 len
= _write(file
->_file
, &cc
, 1);
3963 file
->_flag
|= _IOERR
;
3968 /*********************************************************************
3971 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3976 ret
= _fwrite_nolock(ptr
, size
, nmemb
, file
);
3982 /*********************************************************************
3983 * _fwrite_nolock (MSVCRT.@)
3985 size_t CDECL
_fwrite_nolock(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3987 size_t wrcnt
=size
* nmemb
;
3993 if(file
->_cnt
< 0) {
3994 WARN("negative file->_cnt value in %p\n", file
);
3995 file
->_flag
|= _IOERR
;
3997 } else if(file
->_cnt
) {
3998 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3999 memcpy(file
->_ptr
, ptr
, pcnt
);
4004 ptr
= (const char*)ptr
+ pcnt
;
4005 } else if((file
->_flag
& _IONBF
)
4006 || ((file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4007 || (!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4011 if(file
->_flag
& _IONBF
)
4013 else if(!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)))
4014 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4016 bufsiz
= file
->_bufsiz
;
4018 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4020 if(msvcrt_flush_buffer(file
) == EOF
)
4023 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
4024 file
->_flag
|= _IOERR
;
4029 ptr
= (const char*)ptr
+ pcnt
;
4031 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
4035 ptr
= (const char*)ptr
+ 1;
4039 return written
/ size
;
4042 /*********************************************************************
4045 wint_t CDECL
fputwc(wint_t wc
, FILE* file
)
4050 ret
= _fputwc_nolock(wc
, file
);
4056 /*********************************************************************
4057 * _fputwc_nolock (MSVCRT.@)
4059 wint_t CDECL
_fputwc_nolock(wint_t wc
, FILE* file
)
4065 fdinfo
= get_ioinfo_nolock(file
->_file
);
4067 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
4068 char buf
[MB_LEN_MAX
];
4071 char_len
= wctomb(buf
, mwc
);
4072 if(char_len
!=-1 && _fwrite_nolock(buf
, char_len
, 1, file
)==1)
4076 }else if(_fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4085 /*********************************************************************
4086 * _fputwchar (MSVCRT.@)
4088 wint_t CDECL
_fputwchar(wint_t wc
)
4090 return fputwc(wc
, MSVCRT_stdout
);
4093 /*********************************************************************
4094 * _wfsopen (MSVCRT.@)
4096 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
4099 int open_flags
, stream_flags
, fd
;
4101 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4103 /* map mode string to open() flags. "man fopen" for possibilities. */
4104 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4108 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
4111 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4113 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4120 TRACE(":got (%p)\n",file
);
4121 if (fd
>= 0 && !file
)
4127 /*********************************************************************
4128 * _fsopen (MSVCRT.@)
4130 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
4133 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4135 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4136 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4140 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4143 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4148 ret
= _wfsopen(pathW
, modeW
, share
);
4155 /*********************************************************************
4158 FILE * CDECL
fopen(const char *path
, const char *mode
)
4160 return _fsopen( path
, mode
, _SH_DENYNO
);
4163 /*********************************************************************
4164 * fopen_s (MSVCRT.@)
4166 int CDECL
fopen_s(FILE** pFile
,
4167 const char *filename
, const char *mode
)
4169 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4170 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
4171 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4173 *pFile
= fopen(filename
, mode
);
4180 /*********************************************************************
4181 * _wfopen (MSVCRT.@)
4183 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
4185 return _wfsopen( path
, mode
, _SH_DENYNO
);
4188 /*********************************************************************
4189 * _wfopen_s (MSVCRT.@)
4191 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
4192 const wchar_t *mode
)
4194 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4195 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
4196 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4198 *pFile
= _wfopen(filename
, mode
);
4205 /*********************************************************************
4208 int CDECL
fputc(int c
, FILE* file
)
4213 ret
= _fputc_nolock(c
, file
);
4219 /*********************************************************************
4220 * _fputc_nolock (MSVCRT.@)
4222 int CDECL
_fputc_nolock(int c
, FILE* file
)
4231 res
= msvcrt_flush_buffer(file
);
4232 return res
? res
: c
;
4238 res
= _flsbuf(c
, file
);
4243 /*********************************************************************
4244 * _fputchar (MSVCRT.@)
4246 int CDECL
_fputchar(int c
)
4248 return fputc(c
, MSVCRT_stdout
);
4251 /*********************************************************************
4254 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4259 ret
= _fread_nolock(ptr
, size
, nmemb
, file
);
4265 /*********************************************************************
4266 * _fread_nolock (MSVCRT.@)
4268 size_t CDECL
_fread_nolock(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4270 size_t rcnt
=size
* nmemb
;
4277 /* first buffered data */
4279 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4280 memcpy(ptr
, file
->_ptr
, pcnt
);
4285 ptr
= (char*)ptr
+ pcnt
;
4286 } else if(!(file
->_flag
& _IOREAD
)) {
4287 if(file
->_flag
& _IORW
) {
4288 file
->_flag
|= _IOREAD
;
4294 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
)))
4295 msvcrt_alloc_buffer(file
);
4300 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
))) {
4301 i
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
4302 file
->_ptr
= file
->_base
;
4305 if (i
> rcnt
) i
= rcnt
;
4307 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4308 if (i
> 0 && i
< file
->_cnt
) {
4309 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4310 file
->_flag
&= ~_IOEOF
;
4313 memcpy(ptr
, file
->_ptr
, i
);
4317 } else if (rcnt
> INT_MAX
) {
4318 i
= _read(file
->_file
, ptr
, INT_MAX
);
4319 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4320 i
= _read(file
->_file
, ptr
, rcnt
);
4322 i
= _read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4326 ptr
= (char *)ptr
+i
;
4327 /* expose feof condition in the flags
4328 * MFC tests file->_flag for feof, and doesn't call feof())
4330 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4331 file
->_flag
|= _IOEOF
;
4334 file
->_flag
|= _IOERR
;
4344 #if _MSVCR_VER >= 80
4346 /*********************************************************************
4347 * fread_s (MSVCR80.@)
4349 size_t CDECL
fread_s(void *buf
, size_t buf_size
, size_t elem_size
,
4350 size_t count
, FILE *stream
)
4354 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4356 memset(buf
, 0, buf_size
);
4359 if(!elem_size
|| !count
) return 0;
4362 ret
= _fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4363 _unlock_file(stream
);
4368 /*********************************************************************
4369 * _fread_nolock_s (MSVCR80.@)
4371 size_t CDECL
_fread_nolock_s(void *buf
, size_t buf_size
, size_t elem_size
,
4372 size_t count
, FILE *stream
)
4374 size_t bytes_left
, buf_pos
;
4376 TRACE("(%p %Iu %Iu %Iu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4378 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4380 memset(buf
, 0, buf_size
);
4383 if(!elem_size
|| !count
) return 0;
4384 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4385 if(!MSVCRT_CHECK_PMT(SIZE_MAX
/count
>= elem_size
)) return 0;
4387 bytes_left
= elem_size
*count
;
4390 if(stream
->_cnt
> 0) {
4391 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4393 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, ERANGE
)) {
4394 memset(buf
, 0, buf_size
);
4398 _fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4402 int c
= _filbuf(stream
);
4407 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, ERANGE
)) {
4408 memset(buf
, 0, buf_size
);
4412 ((char*)buf
)[buf_pos
++] = c
;
4417 return buf_pos
/elem_size
;
4420 #endif /* _MSVCR_VER >= 80 */
4422 /*********************************************************************
4423 * _wfreopen (MSVCRT.@)
4426 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
4428 int open_flags
, stream_flags
, fd
;
4430 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4433 if (!file
|| ((fd
= file
->_file
) < 0))
4438 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4440 else if((fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
)) < 0)
4442 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4452 /*********************************************************************
4453 * _wfreopen_s (MSVCRT.@)
4455 int CDECL
_wfreopen_s(FILE** pFile
,
4456 const wchar_t *path
, const wchar_t *mode
, FILE* file
)
4458 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4459 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return EINVAL
;
4460 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4461 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
4463 *pFile
= _wfreopen(path
, mode
, file
);
4470 /*********************************************************************
4471 * freopen (MSVCRT.@)
4474 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
4477 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4479 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4480 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4486 ret
= _wfreopen(pathW
, modeW
, file
);
4493 /*********************************************************************
4494 * freopen_s (MSVCRT.@)
4496 int CDECL
freopen_s(FILE** pFile
,
4497 const char *path
, const char *mode
, FILE* file
)
4499 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4500 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return EINVAL
;
4501 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4502 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
4504 *pFile
= freopen(path
, mode
, file
);
4511 /*********************************************************************
4512 * fsetpos (MSVCRT.@)
4514 int CDECL
fsetpos(FILE* file
, fpos_t *pos
)
4519 msvcrt_flush_buffer(file
);
4521 /* Reset direction of i/o */
4522 if(file
->_flag
& _IORW
) {
4523 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
4526 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4531 /*********************************************************************
4532 * _ftelli64 (MSVCRT.@)
4534 __int64 CDECL
_ftelli64(FILE* file
)
4539 ret
= _ftelli64_nolock(file
);
4545 /*********************************************************************
4546 * _ftelli64_nolock (MSVCRT.@)
4548 __int64 CDECL
_ftelli64_nolock(FILE* file
)
4552 pos
= _telli64(file
->_file
);
4555 if(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) {
4556 if(file
->_flag
& _IOWRT
) {
4557 pos
+= file
->_ptr
- file
->_base
;
4559 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4562 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4566 } else if(!file
->_cnt
) { /* nothing to do */
4567 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4571 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4572 for(i
=0; i
<file
->_cnt
; i
++)
4573 if(file
->_ptr
[i
] == '\n')
4579 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4582 pos
-= file
->_bufsiz
;
4583 pos
+= file
->_ptr
- file
->_base
;
4585 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4586 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4589 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4599 /*********************************************************************
4602 __msvcrt_long CDECL
ftell(FILE* file
)
4604 return _ftelli64(file
);
4607 #if _MSVCR_VER >= 80
4608 /*********************************************************************
4609 * _ftell_nolock (MSVCR80.@)
4611 __msvcrt_long CDECL
_ftell_nolock(FILE* file
)
4613 return _ftelli64_nolock(file
);
4617 /*********************************************************************
4618 * fgetpos (MSVCRT.@)
4620 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
4622 *pos
= _ftelli64(file
);
4628 /*********************************************************************
4631 int CDECL
fputs(const char *s
, FILE* file
)
4633 size_t len
= strlen(s
);
4637 ret
= _fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
4642 /*********************************************************************
4645 int CDECL
fputws(const wchar_t *s
, FILE* file
)
4647 size_t i
, len
= wcslen(s
);
4652 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4653 ret
= _fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
4658 tmp_buf
= add_std_buffer(file
);
4659 for (i
=0; i
<len
; i
++) {
4660 if(_fputwc_nolock(s
[i
], file
) == WEOF
) {
4661 if(tmp_buf
) remove_std_buffer(file
);
4667 if(tmp_buf
) remove_std_buffer(file
);
4672 /*********************************************************************
4673 * getchar (MSVCRT.@)
4675 int CDECL
getchar(void)
4677 return fgetc(MSVCRT_stdin
);
4680 /*********************************************************************
4683 int CDECL
getc(FILE* file
)
4688 /*********************************************************************
4689 * gets_s (MSVCR80.@)
4691 char * CDECL
gets_s(char *buf
, size_t len
)
4693 char *buf_start
= buf
;
4696 if (!MSVCRT_CHECK_PMT(buf
!= NULL
)) return NULL
;
4697 if (!MSVCRT_CHECK_PMT(len
!= 0)) return NULL
;
4699 _lock_file(MSVCRT_stdin
);
4700 for(cc
= _fgetc_nolock(MSVCRT_stdin
);
4701 len
!= 0 && cc
!= EOF
&& cc
!= '\n';
4702 cc
= _fgetc_nolock(MSVCRT_stdin
))
4710 _unlock_file(MSVCRT_stdin
);
4715 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4719 if ((cc
== EOF
) && (buf_start
== buf
))
4721 TRACE(":nothing read\n");
4726 TRACE("got '%s'\n", buf_start
);
4730 /*********************************************************************
4733 char * CDECL
gets(char *buf
)
4735 return gets_s(buf
, -1);
4738 /*********************************************************************
4741 wchar_t* CDECL
_getws(wchar_t* buf
)
4746 _lock_file(MSVCRT_stdin
);
4747 for (cc
= _fgetwc_nolock(MSVCRT_stdin
); cc
!= WEOF
&& cc
!= '\n';
4748 cc
= _fgetwc_nolock(MSVCRT_stdin
))
4751 *buf
++ = (wchar_t)cc
;
4753 _unlock_file(MSVCRT_stdin
);
4755 if ((cc
== WEOF
) && (ws
== buf
))
4757 TRACE(":nothing read\n");
4762 TRACE("got %s\n", debugstr_w(ws
));
4766 /*********************************************************************
4769 int CDECL
putc(int c
, FILE* file
)
4771 return fputc(c
, file
);
4774 /*********************************************************************
4775 * putchar (MSVCRT.@)
4777 int CDECL
putchar(int c
)
4779 return fputc(c
, MSVCRT_stdout
);
4782 /*********************************************************************
4785 int CDECL
puts(const char *s
)
4787 size_t len
= strlen(s
);
4790 _lock_file(MSVCRT_stdout
);
4791 if(_fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4792 _unlock_file(MSVCRT_stdout
);
4796 ret
= _fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : EOF
;
4797 _unlock_file(MSVCRT_stdout
);
4801 /*********************************************************************
4804 int CDECL
_putws(const wchar_t *s
)
4808 _lock_file(MSVCRT_stdout
);
4809 ret
= fputws(s
, MSVCRT_stdout
);
4811 ret
= _fputwc_nolock('\n', MSVCRT_stdout
);
4812 _unlock_file(MSVCRT_stdout
);
4813 return ret
>= 0 ? 0 : WEOF
;
4816 /*********************************************************************
4819 int CDECL
remove(const char *path
)
4821 TRACE("(%s)\n",path
);
4822 if (DeleteFileA(path
))
4824 TRACE(":failed (%d)\n",GetLastError());
4825 msvcrt_set_errno(GetLastError());
4829 /*********************************************************************
4830 * _wremove (MSVCRT.@)
4832 int CDECL
_wremove(const wchar_t *path
)
4834 TRACE("(%s)\n",debugstr_w(path
));
4835 if (DeleteFileW(path
))
4837 TRACE(":failed (%d)\n",GetLastError());
4838 msvcrt_set_errno(GetLastError());
4842 /*********************************************************************
4845 int CDECL
rename(const char *oldpath
,const char *newpath
)
4847 TRACE(":from %s to %s\n",oldpath
,newpath
);
4848 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4850 TRACE(":failed (%d)\n",GetLastError());
4851 msvcrt_set_errno(GetLastError());
4855 /*********************************************************************
4856 * _wrename (MSVCRT.@)
4858 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
4860 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4861 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4863 TRACE(":failed (%d)\n",GetLastError());
4864 msvcrt_set_errno(GetLastError());
4868 /*********************************************************************
4869 * setvbuf (MSVCRT.@)
4871 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
4873 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4874 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
4875 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4879 _fflush_nolock(file
);
4880 if(file
->_flag
& _IOMYBUF
)
4882 file
->_flag
&= ~(_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
);
4885 if(mode
== _IONBF
) {
4886 file
->_flag
|= _IONBF
;
4887 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4890 file
->_base
= file
->_ptr
= buf
;
4891 file
->_flag
|= MSVCRT__USERBUF
;
4892 file
->_bufsiz
= size
;
4894 file
->_base
= file
->_ptr
= malloc(size
);
4901 file
->_flag
|= _IOMYBUF
;
4902 file
->_bufsiz
= size
;
4908 /*********************************************************************
4911 void CDECL
setbuf(FILE* file
, char *buf
)
4913 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
4916 static int tmpnam_helper(char *s
, size_t size
, int *tmpnam_unique
, int tmp_max
)
4922 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return EINVAL
;
4932 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4933 if (digits
+1 > size
) {
4938 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4944 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4945 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4949 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4950 if (digits
+1 > size
) {
4955 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4958 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4959 GetLastError() == ERROR_FILE_NOT_FOUND
)
4965 int CDECL
tmpnam_s(char *s
, size_t size
)
4967 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, TMP_MAX_S
);
4970 /*********************************************************************
4973 char * CDECL
tmpnam(char *s
)
4976 thread_data_t
*data
= msvcrt_get_thread_data();
4978 if(!data
->tmpnam_buffer
)
4979 data
->tmpnam_buffer
= malloc(MAX_PATH
);
4981 s
= data
->tmpnam_buffer
;
4984 return tmpnam_helper(s
, -1, &tmpnam_unique
, TMP_MAX
) ? NULL
: s
;
4987 static int wtmpnam_helper(wchar_t *s
, size_t size
, int *tmpnam_unique
, int tmp_max
)
4993 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return EINVAL
;
5003 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5004 if (digits
+1 > size
) {
5009 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5015 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5016 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5020 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5021 if (digits
+1 > size
) {
5026 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5029 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5030 GetLastError() == ERROR_FILE_NOT_FOUND
)
5036 /*********************************************************************
5037 * _wtmpnam_s (MSVCRT.@)
5039 int CDECL
_wtmpnam_s(wchar_t *s
, size_t size
)
5041 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, TMP_MAX_S
);
5044 /*********************************************************************
5045 * _wtmpnam (MSVCRT.@)
5047 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
5050 thread_data_t
*data
= msvcrt_get_thread_data();
5052 if(!data
->wtmpnam_buffer
)
5053 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
5055 s
= data
->wtmpnam_buffer
;
5058 return wtmpnam_helper(s
, -1, &tmpnam_unique
, TMP_MAX
) ? NULL
: s
;
5061 /*********************************************************************
5062 * tmpfile (MSVCRT.@)
5064 FILE* CDECL
tmpfile(void)
5066 char *filename
= _tempnam(",", "t");
5071 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
5072 _S_IREAD
| _S_IWRITE
);
5073 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5075 if (msvcrt_init_fp(file
, fd
, _IORW
) == -1)
5080 else file
->_tmpfname
= _strdup(filename
);
5083 if(fd
!= -1 && !file
)
5090 /*********************************************************************
5091 * tmpfile_s (MSVCRT.@)
5093 int CDECL
tmpfile_s(FILE** file
)
5095 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
5101 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5103 return fwrite(str
, sizeof(char), len
, file
);
5106 static int puts_clbk_file_w(void *file
, int len
, const wchar_t *str
)
5112 if(!(get_ioinfo_nolock(((FILE*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5113 ret
= _fwrite_nolock(str
, sizeof(wchar_t), len
, file
);
5118 for(i
=0; i
<len
; i
++) {
5119 if(_fputwc_nolock(str
[i
], file
) == WEOF
) {
5129 static int vfprintf_helper(DWORD options
, FILE* file
, const char *format
,
5130 _locale_t locale
, __ms_va_list valist
)
5132 printf_arg args_ctx
[_ARGMAX
+1];
5136 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5137 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5139 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5140 memset(args_ctx
, 0, sizeof(args_ctx
));
5141 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5143 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5147 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5151 tmp_buf
= add_std_buffer(file
);
5152 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5153 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5154 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5155 if(tmp_buf
) remove_std_buffer(file
);
5161 static int vfwprintf_helper(DWORD options
, FILE* file
, const wchar_t *format
,
5162 _locale_t locale
, __ms_va_list valist
)
5164 printf_arg args_ctx
[_ARGMAX
+1];
5168 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5169 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5171 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5172 memset(args_ctx
, 0, sizeof(args_ctx
));
5173 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5175 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5179 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5183 tmp_buf
= add_std_buffer(file
);
5184 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5185 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5186 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5187 if(tmp_buf
) remove_std_buffer(file
);
5193 /*********************************************************************
5194 * _vfprintf_s_l (MSVCRT.@)
5196 int CDECL
_vfprintf_s_l(FILE* file
, const char *format
,
5197 _locale_t locale
, __ms_va_list valist
)
5199 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5202 /*********************************************************************
5203 * _vfwprintf_s_l (MSVCRT.@)
5205 int CDECL
_vfwprintf_s_l(FILE* file
, const wchar_t *format
,
5206 _locale_t locale
, __ms_va_list valist
)
5208 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5211 /*********************************************************************
5212 * vfprintf (MSVCRT.@)
5214 int CDECL
vfprintf(FILE* file
, const char *format
, __ms_va_list valist
)
5216 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5219 /*********************************************************************
5220 * vfprintf_s (MSVCRT.@)
5222 int CDECL
vfprintf_s(FILE* file
, const char *format
, __ms_va_list valist
)
5224 return _vfprintf_s_l(file
, format
, NULL
, valist
);
5227 /*********************************************************************
5228 * vfwprintf (MSVCRT.@)
5230 int CDECL
vfwprintf(FILE* file
, const wchar_t *format
, __ms_va_list valist
)
5232 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5235 /*********************************************************************
5236 * vfwprintf_s (MSVCRT.@)
5238 int CDECL
vfwprintf_s(FILE* file
, const wchar_t *format
, __ms_va_list valist
)
5240 return _vfwprintf_s_l(file
, format
, NULL
, valist
);
5243 #if _MSVCR_VER >= 140
5245 /*********************************************************************
5246 * __stdio_common_vfprintf (UCRTBASE.@)
5248 int CDECL
_stdio_common_vfprintf(unsigned __int64 options
, FILE *file
, const char *format
,
5249 _locale_t locale
, __ms_va_list valist
)
5251 if (options
& ~UCRTBASE_PRINTF_MASK
)
5252 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5254 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5257 /*********************************************************************
5258 * __stdio_common_vfprintf_s (UCRTBASE.@)
5260 int CDECL
__stdio_common_vfprintf_s(unsigned __int64 options
, FILE *file
, const char *format
,
5261 _locale_t locale
, __ms_va_list valist
)
5263 if (options
& ~UCRTBASE_PRINTF_MASK
)
5264 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5266 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5267 file
, format
, locale
, valist
);
5270 /*********************************************************************
5271 * __stdio_common_vfwprintf (UCRTBASE.@)
5273 int CDECL
__stdio_common_vfwprintf(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5274 _locale_t locale
, __ms_va_list valist
)
5276 if (options
& ~UCRTBASE_PRINTF_MASK
)
5277 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5279 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5282 /*********************************************************************
5283 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5285 int CDECL
__stdio_common_vfwprintf_s(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5286 _locale_t locale
, __ms_va_list valist
)
5288 if (options
& ~UCRTBASE_PRINTF_MASK
)
5289 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5291 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5292 file
, format
, locale
, valist
);
5295 #endif /* _MSVCR_VER >= 140 */
5297 /*********************************************************************
5298 * _vfprintf_l (MSVCRT.@)
5300 int CDECL
_vfprintf_l(FILE* file
, const char *format
,
5301 _locale_t locale
, __ms_va_list valist
)
5303 return vfprintf_helper(0, file
, format
, locale
, valist
);
5306 /*********************************************************************
5307 * _vfwprintf_l (MSVCRT.@)
5309 int CDECL
_vfwprintf_l(FILE* file
, const wchar_t *format
,
5310 _locale_t locale
, __ms_va_list valist
)
5312 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5315 /*********************************************************************
5316 * _vfprintf_p_l (MSVCRT.@)
5318 int CDECL
_vfprintf_p_l(FILE* file
, const char *format
,
5319 _locale_t locale
, __ms_va_list valist
)
5321 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5322 file
, format
, locale
, valist
);
5325 /*********************************************************************
5326 * _vfprintf_p (MSVCRT.@)
5328 int CDECL
_vfprintf_p(FILE* file
, const char *format
, __ms_va_list valist
)
5330 return _vfprintf_p_l(file
, format
, NULL
, valist
);
5333 /*********************************************************************
5334 * _vfwprintf_p_l (MSVCRT.@)
5336 int CDECL
_vfwprintf_p_l(FILE* file
, const wchar_t *format
,
5337 _locale_t locale
, __ms_va_list valist
)
5339 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5340 file
, format
, locale
, valist
);
5343 /*********************************************************************
5344 * _vfwprintf_p (MSVCRT.@)
5346 int CDECL
_vfwprintf_p(FILE* file
, const wchar_t *format
, __ms_va_list valist
)
5348 return _vfwprintf_p_l(file
, format
, NULL
, valist
);
5351 /*********************************************************************
5352 * vprintf (MSVCRT.@)
5354 int CDECL
vprintf(const char *format
, __ms_va_list valist
)
5356 return vfprintf(MSVCRT_stdout
,format
,valist
);
5359 /*********************************************************************
5360 * vprintf_s (MSVCRT.@)
5362 int CDECL
vprintf_s(const char *format
, __ms_va_list valist
)
5364 return vfprintf_s(MSVCRT_stdout
,format
,valist
);
5367 /*********************************************************************
5368 * vwprintf (MSVCRT.@)
5370 int CDECL
vwprintf(const wchar_t *format
, __ms_va_list valist
)
5372 return vfwprintf(MSVCRT_stdout
,format
,valist
);
5375 /*********************************************************************
5376 * vwprintf_s (MSVCRT.@)
5378 int CDECL
vwprintf_s(const wchar_t *format
, __ms_va_list valist
)
5380 return vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5383 /*********************************************************************
5384 * fprintf (MSVCRT.@)
5386 int WINAPIV
fprintf(FILE* file
, const char *format
, ...)
5388 __ms_va_list valist
;
5390 __ms_va_start(valist
, format
);
5391 res
= vfprintf(file
, format
, valist
);
5392 __ms_va_end(valist
);
5396 /*********************************************************************
5397 * fprintf_s (MSVCRT.@)
5399 int WINAPIV
fprintf_s(FILE* file
, const char *format
, ...)
5401 __ms_va_list valist
;
5403 __ms_va_start(valist
, format
);
5404 res
= vfprintf_s(file
, format
, valist
);
5405 __ms_va_end(valist
);
5409 /*********************************************************************
5410 * fwprintf (MSVCRT.@)
5412 int WINAPIV
fwprintf(FILE* file
, const wchar_t *format
, ...)
5414 __ms_va_list valist
;
5416 __ms_va_start(valist
, format
);
5417 res
= vfwprintf(file
, format
, valist
);
5418 __ms_va_end(valist
);
5422 /*********************************************************************
5423 * fwprintf_s (MSVCRT.@)
5425 int WINAPIV
fwprintf_s(FILE* file
, const wchar_t *format
, ...)
5427 __ms_va_list valist
;
5429 __ms_va_start(valist
, format
);
5430 res
= vfwprintf_s(file
, format
, valist
);
5431 __ms_va_end(valist
);
5435 /*********************************************************************
5436 * _fwprintf_l (MSVCRT.@)
5438 int WINAPIV
_fwprintf_l(FILE* file
, const wchar_t *format
, _locale_t locale
, ...)
5440 __ms_va_list valist
;
5442 __ms_va_start(valist
, locale
);
5443 res
= _vfwprintf_l(file
, format
, locale
, valist
);
5444 __ms_va_end(valist
);
5448 /*********************************************************************
5451 int WINAPIV
printf(const char *format
, ...)
5453 __ms_va_list valist
;
5455 __ms_va_start(valist
, format
);
5456 res
= vfprintf(MSVCRT_stdout
, format
, valist
);
5457 __ms_va_end(valist
);
5461 /*********************************************************************
5462 * printf_s (MSVCRT.@)
5464 int WINAPIV
printf_s(const char *format
, ...)
5466 __ms_va_list valist
;
5468 __ms_va_start(valist
, format
);
5469 res
= vprintf_s(format
, valist
);
5470 __ms_va_end(valist
);
5474 /*********************************************************************
5477 int CDECL
ungetc(int c
, FILE * file
)
5481 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
5484 ret
= _ungetc_nolock(c
, file
);
5490 /*********************************************************************
5491 * _ungetc_nolock (MSVCRT.@)
5493 int CDECL
_ungetc_nolock(int c
, FILE * file
)
5495 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
5497 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
5498 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
5501 if((!(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
))
5502 && msvcrt_alloc_buffer(file
))
5503 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5506 if(file
->_ptr
>file
->_base
) {
5508 if(file
->_flag
& _IOSTRG
) {
5509 if(*file
->_ptr
!= c
) {
5517 file
->_flag
&= ~(_IOERR
| _IOEOF
);
5518 file
->_flag
|= _IOREAD
;
5525 /*********************************************************************
5526 * ungetwc (MSVCRT.@)
5528 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
5532 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5535 ret
= _ungetwc_nolock(wc
, file
);
5541 /*********************************************************************
5542 * _ungetwc_nolock (MSVCRT.@)
5544 wint_t CDECL
_ungetwc_nolock(wint_t wc
, FILE * file
)
5548 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5552 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5553 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5554 unsigned char * pp
= (unsigned char *)&mwc
;
5557 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
5558 if(pp
[i
] != _ungetc_nolock(pp
[i
],file
))
5562 char mbs
[MB_LEN_MAX
];
5565 len
= wctomb(mbs
, mwc
);
5569 for(len
--; len
>=0; len
--) {
5570 if(mbs
[len
] != _ungetc_nolock(mbs
[len
], file
))
5578 /*********************************************************************
5579 * wprintf (MSVCRT.@)
5581 int WINAPIV
wprintf(const wchar_t *format
, ...)
5583 __ms_va_list valist
;
5585 __ms_va_start(valist
, format
);
5586 res
= vwprintf(format
, valist
);
5587 __ms_va_end(valist
);
5591 /*********************************************************************
5592 * wprintf_s (MSVCRT.@)
5594 int WINAPIV
wprintf_s(const wchar_t *format
, ...)
5596 __ms_va_list valist
;
5598 __ms_va_start(valist
, format
);
5599 res
= vwprintf_s(format
, valist
);
5600 __ms_va_end(valist
);
5604 /*********************************************************************
5605 * _getmaxstdio (MSVCRT.@)
5607 int CDECL
_getmaxstdio(void)
5609 return MSVCRT_max_streams
;
5612 /*********************************************************************
5613 * _setmaxstdio (MSVCRT.@)
5615 int CDECL
_setmaxstdio(int newmax
)
5617 TRACE("%d\n", newmax
);
5619 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5622 MSVCRT_max_streams
= newmax
;
5623 return MSVCRT_max_streams
;
5626 #if _MSVCR_VER >= 140
5627 /*********************************************************************
5628 * _get_stream_buffer_pointers (UCRTBASE.@)
5630 int CDECL
_get_stream_buffer_pointers(FILE *file
, char*** base
,
5631 char*** ptr
, int** count
)
5634 *base
= &file
->_base
;
5638 *count
= &file
->_cnt
;