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
32 #include <sys/locking.h>
33 #include <sys/types.h>
34 #include <sys/utime.h>
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
69 /* for stat mode, permissions apply to all,owner and group */
70 #define ALL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
71 #define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
72 #define ALL_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
74 /* _access() bit flags FIXME: incomplete */
75 #define MSVCRT_W_OK 0x02
76 #define MSVCRT_R_OK 0x04
78 /* values for wxflag in file descriptor */
81 #define WX_READNL 0x04 /* read started with \n */
83 #define WX_DONTINHERIT 0x10
84 #define WX_APPEND 0x20
88 /* values for exflag - it's used differently in msvcr90.dll*/
91 #define EF_CRIT_INIT 0x04
92 #define EF_UNK_UNICODE 0x08
94 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
95 static char utf16_bom
[2] = { 0xff, 0xfe };
97 /* FIXME: this should be allocated dynamically */
98 #define MSVCRT_MAX_FILES 2048
99 #define MSVCRT_FD_BLOCK_SIZE 32
101 #define MSVCRT_INTERNAL_BUFSIZ 4096
103 /* ioinfo structure size is different in msvcrXX.dll's */
106 unsigned char wxflag
;
109 CRITICAL_SECTION crit
;
115 BOOL utf8translations
;
123 /*********************************************************************
124 * __pioinfo (MSVCRT.@)
125 * array of pointers to ioinfo arrays [32]
127 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
129 /*********************************************************************
130 * __badioinfo (MSVCRT.@)
132 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
136 CRITICAL_SECTION crit
;
139 FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
140 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
141 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
143 /* INTERNAL: process umask */
144 static int MSVCRT_umask
= 0;
146 /* INTERNAL: static data for tmpnam and _wtmpname functions */
147 static LONG tmpnam_unique
;
148 static LONG tmpnam_s_unique
;
150 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
151 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
152 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
153 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
155 #define TOUL(x) (ULONGLONG)(x)
156 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
157 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
158 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
159 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
161 /* This critical section protects the MSVCRT_fstreams table
162 * and MSVCRT_stream_idx from race conditions. It also
163 * protects fd critical sections creation code.
165 static CRITICAL_SECTION MSVCRT_file_cs
;
166 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
168 0, 0, &MSVCRT_file_cs
,
169 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
170 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
172 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
173 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
174 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
176 static void msvcrt_stat64_to_stat(const struct _stat64
*buf64
, struct _stat
*buf
)
178 buf
->st_dev
= buf64
->st_dev
;
179 buf
->st_ino
= buf64
->st_ino
;
180 buf
->st_mode
= buf64
->st_mode
;
181 buf
->st_nlink
= buf64
->st_nlink
;
182 buf
->st_uid
= buf64
->st_uid
;
183 buf
->st_gid
= buf64
->st_gid
;
184 buf
->st_rdev
= buf64
->st_rdev
;
185 buf
->st_size
= buf64
->st_size
;
186 buf
->st_atime
= buf64
->st_atime
;
187 buf
->st_mtime
= buf64
->st_mtime
;
188 buf
->st_ctime
= buf64
->st_ctime
;
191 static void msvcrt_stat64_to_stati64(const struct _stat64
*buf64
, struct _stati64
*buf
)
193 buf
->st_dev
= buf64
->st_dev
;
194 buf
->st_ino
= buf64
->st_ino
;
195 buf
->st_mode
= buf64
->st_mode
;
196 buf
->st_nlink
= buf64
->st_nlink
;
197 buf
->st_uid
= buf64
->st_uid
;
198 buf
->st_gid
= buf64
->st_gid
;
199 buf
->st_rdev
= buf64
->st_rdev
;
200 buf
->st_size
= buf64
->st_size
;
201 buf
->st_atime
= buf64
->st_atime
;
202 buf
->st_mtime
= buf64
->st_mtime
;
203 buf
->st_ctime
= buf64
->st_ctime
;
206 static void msvcrt_stat64_to_stat32(const struct _stat64
*buf64
, struct _stat32
*buf
)
208 buf
->st_dev
= buf64
->st_dev
;
209 buf
->st_ino
= buf64
->st_ino
;
210 buf
->st_mode
= buf64
->st_mode
;
211 buf
->st_nlink
= buf64
->st_nlink
;
212 buf
->st_uid
= buf64
->st_uid
;
213 buf
->st_gid
= buf64
->st_gid
;
214 buf
->st_rdev
= buf64
->st_rdev
;
215 buf
->st_size
= buf64
->st_size
;
216 buf
->st_atime
= buf64
->st_atime
;
217 buf
->st_mtime
= buf64
->st_mtime
;
218 buf
->st_ctime
= buf64
->st_ctime
;
221 static void msvcrt_stat64_to_stat64i32(const struct _stat64
*buf64
, struct _stat64i32
*buf
)
223 buf
->st_dev
= buf64
->st_dev
;
224 buf
->st_ino
= buf64
->st_ino
;
225 buf
->st_mode
= buf64
->st_mode
;
226 buf
->st_nlink
= buf64
->st_nlink
;
227 buf
->st_uid
= buf64
->st_uid
;
228 buf
->st_gid
= buf64
->st_gid
;
229 buf
->st_rdev
= buf64
->st_rdev
;
230 buf
->st_size
= buf64
->st_size
;
231 buf
->st_atime
= buf64
->st_atime
;
232 buf
->st_mtime
= buf64
->st_mtime
;
233 buf
->st_ctime
= buf64
->st_ctime
;
236 static void msvcrt_stat64_to_stat32i64(const struct _stat64
*buf64
, struct _stat32i64
*buf
)
238 buf
->st_dev
= buf64
->st_dev
;
239 buf
->st_ino
= buf64
->st_ino
;
240 buf
->st_mode
= buf64
->st_mode
;
241 buf
->st_nlink
= buf64
->st_nlink
;
242 buf
->st_uid
= buf64
->st_uid
;
243 buf
->st_gid
= buf64
->st_gid
;
244 buf
->st_rdev
= buf64
->st_rdev
;
245 buf
->st_size
= buf64
->st_size
;
246 buf
->st_atime
= buf64
->st_atime
;
247 buf
->st_mtime
= buf64
->st_mtime
;
248 buf
->st_ctime
= buf64
->st_ctime
;
251 static void time_to_filetime( __time64_t time
, FILETIME
*ft
)
253 /* 1601 to 1970 is 369 years plus 89 leap days */
254 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
256 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
257 ft
->dwHighDateTime
= ticks
>> 32;
258 ft
->dwLowDateTime
= ticks
;
261 static inline ioinfo
* get_ioinfo_nolock(int fd
)
264 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
265 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
267 return &MSVCRT___badioinfo
;
269 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
272 static inline void init_ioinfo_cs(ioinfo
*info
)
274 if(!(info
->exflag
& EF_CRIT_INIT
)) {
276 if(!(info
->exflag
& EF_CRIT_INIT
)) {
277 InitializeCriticalSection(&info
->crit
);
278 info
->exflag
|= EF_CRIT_INIT
;
284 static inline ioinfo
* get_ioinfo(int fd
)
286 ioinfo
*ret
= get_ioinfo_nolock(fd
);
287 if(ret
== &MSVCRT___badioinfo
)
290 EnterCriticalSection(&ret
->crit
);
294 static inline BOOL
alloc_pioinfo_block(int fd
)
299 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
305 block
= calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
308 WARN(":out of memory!\n");
312 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
313 block
[i
].handle
= INVALID_HANDLE_VALUE
;
314 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
319 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
323 ret
= get_ioinfo(fd
);
324 if(ret
!= &MSVCRT___badioinfo
)
327 if(!alloc_pioinfo_block(fd
))
328 return &MSVCRT___badioinfo
;
330 return get_ioinfo(fd
);
333 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
338 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
340 ioinfo
*info
= get_ioinfo_nolock(i
);
342 if(info
== &MSVCRT___badioinfo
)
344 if(!alloc_pioinfo_block(i
))
345 return &MSVCRT___badioinfo
;
346 info
= get_ioinfo_nolock(i
);
349 init_ioinfo_cs(info
);
350 if(TryEnterCriticalSection(&info
->crit
))
352 if(info
->handle
== INVALID_HANDLE_VALUE
)
357 LeaveCriticalSection(&info
->crit
);
361 WARN(":files exhausted!\n");
363 return &MSVCRT___badioinfo
;
366 static inline void release_ioinfo(ioinfo
*info
)
368 if(info
!=&MSVCRT___badioinfo
&& info
->exflag
& EF_CRIT_INIT
)
369 LeaveCriticalSection(&info
->crit
);
372 static inline FILE* msvcrt_get_file(int i
)
376 if(i
>= MSVCRT_max_streams
)
380 return &MSVCRT__iob
[i
];
382 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
384 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
385 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
386 ERR("out of memory\n");
391 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
393 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
398 /* INTERNAL: free a file entry fd */
399 static void msvcrt_free_fd(int fd
)
401 ioinfo
*fdinfo
= get_ioinfo(fd
);
403 if(fdinfo
!= &MSVCRT___badioinfo
)
405 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
408 TRACE(":fd (%d) freed\n",fd
);
415 SetStdHandle(STD_INPUT_HANDLE
, 0);
418 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
421 SetStdHandle(STD_ERROR_HANDLE
, 0);
425 release_ioinfo(fdinfo
);
428 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
430 fdinfo
->handle
= hand
;
431 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
432 fdinfo
->lookahead
[0] = '\n';
433 fdinfo
->lookahead
[1] = '\n';
434 fdinfo
->lookahead
[2] = '\n';
435 fdinfo
->exflag
&= EF_CRIT_INIT
;
437 if (hand
== MSVCRT_NO_CONSOLE
) hand
= 0;
438 switch (fdinfo
-MSVCRT___pioinfo
[0])
440 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
441 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
442 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
446 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
447 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
450 ioinfo
*info
= get_ioinfo_alloc(&fd
);
452 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
454 if(info
== &MSVCRT___badioinfo
)
457 msvcrt_set_fd(info
, hand
, flag
);
458 release_ioinfo(info
);
462 /* INTERNAL: Allocate a FILE* for an fd slot */
463 /* caller must hold the files lock */
464 static FILE* msvcrt_alloc_fp(void)
469 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
471 file
= msvcrt_get_file(i
);
475 if (file
->_flag
== 0)
477 if (i
== MSVCRT_stream_idx
)
479 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
481 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
482 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
493 /* INTERNAL: initialize a FILE* from an open fd */
494 static int msvcrt_init_fp(FILE* file
, int fd
, unsigned stream_flags
)
496 TRACE(":fd (%d) allocating FILE*\n",fd
);
497 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
499 WARN(":invalid fd %d\n",fd
);
504 file
->_ptr
= file
->_base
= NULL
;
507 file
->_flag
= stream_flags
;
508 file
->_tmpfname
= NULL
;
510 TRACE(":got FILE* (%p)\n",file
);
514 /* INTERNAL: Create an inheritance data block (for spawned process)
515 * The inheritance block is made of:
516 * 00 int nb of file descriptor (NBFD)
517 * 04 char file flags (wxflag): repeated for each fd
518 * 4+NBFD HANDLE file handle: repeated for each fd
520 BOOL
msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
527 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
528 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
532 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
533 *block
= calloc(1, *size
);
539 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
540 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
542 *(unsigned*)*block
= last_fd
;
543 for (fd
= 0; fd
< last_fd
; fd
++)
545 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
546 fdinfo
= get_ioinfo_nolock(fd
);
547 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
549 *wxflag_ptr
= fdinfo
->wxflag
;
550 *handle_ptr
= fdinfo
->handle
;
555 *handle_ptr
= INVALID_HANDLE_VALUE
;
557 wxflag_ptr
++; handle_ptr
++;
562 /* INTERNAL: Set up all file descriptors,
563 * as well as default streams (stdin, stderr and stdout)
565 void msvcrt_init_io(void)
571 GetStartupInfoA(&si
);
572 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
578 count
= *(unsigned*)si
.lpReserved2
;
579 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
580 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
582 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
583 count
= min(count
, MSVCRT_MAX_FILES
);
584 for (i
= 0; i
< count
; i
++)
586 if ((*wxflag_ptr
& WX_OPEN
) && GetFileType(*handle_ptr
) != FILE_TYPE_UNKNOWN
)
588 fdinfo
= get_ioinfo_alloc_fd(i
);
589 if(fdinfo
!= &MSVCRT___badioinfo
)
590 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
591 release_ioinfo(fdinfo
);
594 wxflag_ptr
++; handle_ptr
++;
598 fdinfo
= get_ioinfo_alloc_fd(STDIN_FILENO
);
599 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
600 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
601 DWORD flags
= WX_OPEN
| WX_TEXT
;
602 DWORD type
= GetFileType(h
);
604 if (type
== FILE_TYPE_UNKNOWN
) {
605 h
= MSVCRT_NO_CONSOLE
;
607 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
609 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
613 msvcrt_set_fd(fdinfo
, h
, flags
);
615 release_ioinfo(fdinfo
);
617 fdinfo
= get_ioinfo_alloc_fd(STDOUT_FILENO
);
618 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
619 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
620 DWORD flags
= WX_OPEN
| WX_TEXT
;
621 DWORD type
= GetFileType(h
);
623 if (type
== FILE_TYPE_UNKNOWN
) {
624 h
= MSVCRT_NO_CONSOLE
;
626 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
628 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
632 msvcrt_set_fd(fdinfo
, h
, flags
);
634 release_ioinfo(fdinfo
);
636 fdinfo
= get_ioinfo_alloc_fd(STDERR_FILENO
);
637 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
638 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
639 DWORD flags
= WX_OPEN
| WX_TEXT
;
640 DWORD type
= GetFileType(h
);
642 if (type
== FILE_TYPE_UNKNOWN
) {
643 h
= MSVCRT_NO_CONSOLE
;
645 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
647 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
651 msvcrt_set_fd(fdinfo
, h
, flags
);
653 release_ioinfo(fdinfo
);
655 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO
)->handle
,
656 get_ioinfo_nolock(STDOUT_FILENO
)->handle
,
657 get_ioinfo_nolock(STDERR_FILENO
)->handle
);
659 memset(MSVCRT__iob
,0,3*sizeof(FILE));
660 for (i
= 0; i
< 3; i
++)
662 /* FILE structs for stdin/out/err are static and never deleted */
663 MSVCRT__iob
[i
]._file
= get_ioinfo_nolock(i
)->handle
== MSVCRT_NO_CONSOLE
?
664 MSVCRT_NO_CONSOLE_FD
: i
;
665 MSVCRT__iob
[i
]._tmpfname
= NULL
;
666 MSVCRT__iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
668 MSVCRT_stream_idx
= 3;
671 /* INTERNAL: Flush stdio file buffer */
672 static int msvcrt_flush_buffer(FILE* file
)
676 if((file
->_flag
& (_IOREAD
|_IOWRT
)) == _IOWRT
&&
677 file
->_flag
& (_IOMYBUF
|MSVCRT__USERBUF
)) {
678 int cnt
=file
->_ptr
-file
->_base
;
679 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
680 file
->_flag
|= _IOERR
;
682 } else if(file
->_flag
& _IORW
) {
683 file
->_flag
&= ~_IOWRT
;
687 file
->_ptr
=file
->_base
;
692 /*********************************************************************
695 int CDECL
_isatty(int fd
)
697 TRACE(":fd (%d)\n",fd
);
699 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
702 /* INTERNAL: Allocate stdio file buffer */
703 static BOOL
msvcrt_alloc_buffer(FILE* file
)
705 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
706 && _isatty(file
->_file
))
709 file
->_base
= calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
711 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
712 file
->_flag
|= _IOMYBUF
;
714 file
->_base
= (char*)(&file
->_charbuf
);
716 file
->_flag
|= _IONBF
;
718 file
->_ptr
= file
->_base
;
723 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
724 static BOOL
add_std_buffer(FILE *file
)
726 static char buffers
[2][BUFSIZ
];
728 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
729 || (file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
))
730 || !_isatty(file
->_file
))
733 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
734 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
735 file
->_flag
|= MSVCRT__USERBUF
;
739 /* INTERNAL: Removes temporary buffer from stdout or stderr */
740 /* Only call this function when add_std_buffer returned TRUE */
741 static void remove_std_buffer(FILE *file
)
743 msvcrt_flush_buffer(file
);
744 file
->_ptr
= file
->_base
= NULL
;
745 file
->_bufsiz
= file
->_cnt
= 0;
746 file
->_flag
&= ~MSVCRT__USERBUF
;
749 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
750 static int msvcrt_int_to_base32(int num
, char *str
)
765 *p
= (num
& 31) + '0';
767 *p
+= ('a' - '0' - 10);
774 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
775 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
790 *p
= (num
& 31) + '0';
792 *p
+= ('a' - '0' - 10);
799 /*********************************************************************
800 * __iob_func (MSVCRT.@)
803 FILE * CDECL
__iob_func(void)
805 return &MSVCRT__iob
[0];
808 #if _MSVCR_VER >= 140
809 /*********************************************************************
810 * __acrt_iob_func(UCRTBASE.@)
812 FILE * CDECL
__acrt_iob_func(unsigned idx
)
814 return &MSVCRT__iob
[idx
];
818 /*********************************************************************
821 int CDECL
_access(const char *filename
, int mode
)
823 DWORD attr
= GetFileAttributesA(filename
);
825 TRACE("(%s,%d) %ld\n", filename
, mode
, attr
);
827 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
829 msvcrt_set_errno(GetLastError());
832 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
834 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
840 /*********************************************************************
841 * _access_s (MSVCRT.@)
843 int CDECL
_access_s(const char *filename
, int mode
)
845 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *_errno();
846 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *_errno();
848 if (_access(filename
, mode
) == -1)
853 /*********************************************************************
854 * _waccess (MSVCRT.@)
856 int CDECL
_waccess(const wchar_t *filename
, int mode
)
858 DWORD attr
= GetFileAttributesW(filename
);
860 TRACE("(%s,%d) %ld\n", debugstr_w(filename
), mode
, attr
);
862 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
864 msvcrt_set_errno(GetLastError());
867 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
869 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
875 /*********************************************************************
876 * _waccess_s (MSVCRT.@)
878 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
880 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *_errno();
881 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *_errno();
883 if (_waccess(filename
, mode
) == -1)
888 /*********************************************************************
891 int CDECL
_chmod(const char *path
, int flags
)
893 DWORD oldFlags
= GetFileAttributesA(path
);
895 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
897 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
898 oldFlags
| FILE_ATTRIBUTE_READONLY
;
900 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
903 msvcrt_set_errno(GetLastError());
907 /*********************************************************************
910 int CDECL
_wchmod(const wchar_t *path
, int flags
)
912 DWORD oldFlags
= GetFileAttributesW(path
);
914 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
916 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
917 oldFlags
| FILE_ATTRIBUTE_READONLY
;
919 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
922 msvcrt_set_errno(GetLastError());
926 /*********************************************************************
929 int CDECL
_unlink(const char *path
)
931 TRACE("%s\n", debugstr_a(path
));
932 if(DeleteFileA(path
))
934 TRACE("failed (%ld)\n", GetLastError());
935 msvcrt_set_errno(GetLastError());
939 /*********************************************************************
940 * _wunlink (MSVCRT.@)
942 int CDECL
_wunlink(const wchar_t *path
)
944 TRACE("(%s)\n", debugstr_w(path
));
945 if(DeleteFileW(path
))
947 TRACE("failed (%ld)\n", GetLastError());
948 msvcrt_set_errno(GetLastError());
952 /*********************************************************************
955 int CDECL
_commit(int fd
)
957 ioinfo
*info
= get_ioinfo(fd
);
960 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
962 if (info
->handle
== INVALID_HANDLE_VALUE
)
964 else if (!FlushFileBuffers(info
->handle
))
966 if (GetLastError() == ERROR_INVALID_HANDLE
)
968 /* FlushFileBuffers fails for console handles
969 * so we ignore this error.
975 TRACE(":failed-last error (%ld)\n", GetLastError());
976 msvcrt_set_errno(GetLastError());
986 release_ioinfo(info
);
990 /* INTERNAL: Flush all stream buffer */
991 static int msvcrt_flush_all_buffers(int mask
)
993 int i
, num_flushed
= 0;
997 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
998 file
= msvcrt_get_file(i
);
1002 if(file
->_flag
& mask
) {
1010 TRACE(":flushed (%d) handles\n",num_flushed
);
1014 /*********************************************************************
1015 * _flushall (MSVCRT.@)
1017 int CDECL
_flushall(void)
1019 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
1022 /*********************************************************************
1025 int CDECL
fflush(FILE* file
)
1030 msvcrt_flush_all_buffers(_IOWRT
);
1034 ret
= _fflush_nolock(file
);
1041 /*********************************************************************
1042 * _fflush_nolock (MSVCRT.@)
1044 int CDECL
_fflush_nolock(FILE* file
)
1049 msvcrt_flush_all_buffers(_IOWRT
);
1053 res
= msvcrt_flush_buffer(file
);
1054 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1055 res
= _commit(file
->_file
) ? EOF
: 0;
1059 /*********************************************************************
1062 int CDECL
_close(int fd
)
1064 ioinfo
*info
= get_ioinfo(fd
);
1067 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1069 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1072 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, EBADF
)) {
1074 } else if (fd
== STDOUT_FILENO
&&
1075 info
->handle
== get_ioinfo_nolock(STDERR_FILENO
)->handle
) {
1078 } else if (fd
== STDERR_FILENO
&&
1079 info
->handle
== get_ioinfo_nolock(STDOUT_FILENO
)->handle
) {
1083 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1086 WARN(":failed-last error (%ld)\n", GetLastError());
1087 msvcrt_set_errno(GetLastError());
1090 release_ioinfo(info
);
1094 /*********************************************************************
1097 * MSDN isn't clear on this point, but the remarks for _pipe
1098 * indicate file descriptors duplicated with _dup and _dup2 are always
1101 int CDECL
_dup2(int od
, int nd
)
1103 ioinfo
*info_od
, *info_nd
;
1106 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1110 info_od
= get_ioinfo(od
);
1111 info_nd
= get_ioinfo_alloc_fd(nd
);
1115 info_nd
= get_ioinfo_alloc_fd(nd
);
1116 info_od
= get_ioinfo(od
);
1119 if (info_nd
== &MSVCRT___badioinfo
)
1123 else if (info_od
->wxflag
& WX_OPEN
)
1127 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1128 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1130 int wxflag
= info_od
->wxflag
& ~WX_DONTINHERIT
;
1132 if (info_nd
->wxflag
& WX_OPEN
)
1135 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1136 /* _dup2 returns 0, not nd, on success */
1142 msvcrt_set_errno(GetLastError());
1151 release_ioinfo(info_od
);
1152 release_ioinfo(info_nd
);
1156 /*********************************************************************
1159 int CDECL
_dup(int od
)
1162 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1164 if (_dup2(od
, fd
) == 0)
1168 release_ioinfo(info
);
1172 /*********************************************************************
1175 int CDECL
_eof(int fd
)
1177 ioinfo
*info
= get_ioinfo(fd
);
1178 DWORD curpos
,endpos
;
1179 LONG hcurpos
,hendpos
;
1181 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1183 if (info
->handle
== INVALID_HANDLE_VALUE
)
1185 release_ioinfo(info
);
1189 if (info
->wxflag
& WX_ATEOF
)
1191 release_ioinfo(info
);
1195 /* Otherwise we do it the hard way */
1196 hcurpos
= hendpos
= 0;
1197 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1198 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1200 if (curpos
== endpos
&& hcurpos
== hendpos
)
1202 /* FIXME: shouldn't WX_ATEOF be set here? */
1203 release_ioinfo(info
);
1207 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1208 release_ioinfo(info
);
1212 /*********************************************************************
1213 * _fcloseall (MSVCRT.@)
1215 int CDECL
_fcloseall(void)
1217 int num_closed
= 0, i
;
1221 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1222 file
= msvcrt_get_file(i
);
1224 if (file
->_flag
&& !fclose(file
))
1229 TRACE(":closed (%d) handles\n",num_closed
);
1233 /* free everything on process exit */
1234 void msvcrt_free_io(void)
1242 for(i
=0; i
<ARRAY_SIZE(MSVCRT___pioinfo
); i
++)
1244 if(!MSVCRT___pioinfo
[i
])
1247 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1249 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1250 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1252 free(MSVCRT___pioinfo
[i
]);
1255 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1257 FILE *file
= msvcrt_get_file(j
);
1258 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1260 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1261 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1265 for(i
=0; i
<ARRAY_SIZE(MSVCRT_fstream
); i
++)
1266 free(MSVCRT_fstream
[i
]);
1269 /*********************************************************************
1270 * _lseeki64 (MSVCRT.@)
1272 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1274 ioinfo
*info
= get_ioinfo(fd
);
1277 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1279 if (info
->handle
== INVALID_HANDLE_VALUE
)
1282 release_ioinfo(info
);
1286 if (whence
< 0 || whence
> 2)
1288 release_ioinfo(info
);
1293 TRACE(":fd (%d) to %#I64x pos %s\n",
1294 fd
, offset
, (whence
== SEEK_SET
) ? "SEEK_SET" :
1295 (whence
== SEEK_CUR
) ? "SEEK_CUR" :
1296 (whence
== SEEK_END
) ? "SEEK_END" : "UNKNOWN");
1298 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1299 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1300 ofs
.QuadPart
= offset
;
1301 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1302 GetLastError() == ERROR_SUCCESS
)
1304 info
->wxflag
&= ~WX_ATEOF
;
1305 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1307 release_ioinfo(info
);
1308 return ofs
.QuadPart
;
1310 release_ioinfo(info
);
1311 TRACE(":error-last error (%ld)\n", GetLastError());
1312 msvcrt_set_errno(GetLastError());
1316 /*********************************************************************
1319 __msvcrt_long CDECL
_lseek(int fd
, __msvcrt_long offset
, int whence
)
1321 return _lseeki64(fd
, offset
, whence
);
1324 /*********************************************************************
1325 * _lock_file (MSVCRT.@)
1327 void CDECL
_lock_file(FILE *file
)
1329 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1330 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1332 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1335 /*********************************************************************
1336 * _unlock_file (MSVCRT.@)
1338 void CDECL
_unlock_file(FILE *file
)
1340 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1341 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1343 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1346 /*********************************************************************
1347 * _locking (MSVCRT.@)
1349 * This is untested; the underlying LockFile doesn't work yet.
1351 int CDECL
_locking(int fd
, int mode
, __msvcrt_long nbytes
)
1353 ioinfo
*info
= get_ioinfo(fd
);
1357 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1358 if (info
->handle
== INVALID_HANDLE_VALUE
)
1360 release_ioinfo(info
);
1364 if (mode
< 0 || mode
> 4)
1366 release_ioinfo(info
);
1371 TRACE(":fd (%d) by %#lx mode %s\n",
1372 fd
, nbytes
, (mode
== _LK_UNLCK
) ? "_LK_UNLCK" :
1373 (mode
== _LK_LOCK
) ? "_LK_LOCK" :
1374 (mode
== _LK_NBLCK
) ? "_LK_NBLCK" :
1375 (mode
== _LK_RLCK
) ? "_LK_RLCK" :
1376 (mode
== _LK_NBRLCK
) ? "_LK_NBRLCK" :
1379 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1381 release_ioinfo(info
);
1382 FIXME("Seek failed\n");
1383 *_errno() = EINVAL
; /* FIXME */
1386 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1389 ret
= 1; /* just to satisfy gcc */
1392 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1397 else if (mode
== _LK_UNLCK
)
1398 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1400 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1401 /* FIXME - what about error settings? */
1402 release_ioinfo(info
);
1403 return ret
? 0 : -1;
1406 /*********************************************************************
1407 * _fseeki64 (MSVCRT.@)
1409 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1414 ret
= _fseeki64_nolock(file
, offset
, whence
);
1420 /*********************************************************************
1421 * _fseeki64_nolock (MSVCRT.@)
1423 int CDECL
_fseeki64_nolock(FILE* file
, __int64 offset
, int whence
)
1427 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1429 offset
+= _ftelli64_nolock(file
);
1432 /* Flush output if needed */
1433 msvcrt_flush_buffer(file
);
1434 /* Reset direction of i/o */
1435 if(file
->_flag
& _IORW
) {
1436 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1438 /* Clear end of file flag */
1439 file
->_flag
&= ~_IOEOF
;
1440 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1445 /*********************************************************************
1448 int CDECL
fseek(FILE* file
, __msvcrt_long offset
, int whence
)
1450 return _fseeki64( file
, offset
, whence
);
1453 /*********************************************************************
1454 * _fseek_nolock (MSVCRT.@)
1456 int CDECL
_fseek_nolock(FILE* file
, __msvcrt_long offset
, int whence
)
1458 return _fseeki64_nolock( file
, offset
, whence
);
1461 /*********************************************************************
1462 * _chsize_s (MSVCRT.@)
1464 int CDECL
_chsize_s(int fd
, __int64 size
)
1470 TRACE("(fd=%d, size=%#I64x)\n", fd
, size
);
1472 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1475 info
= get_ioinfo(fd
);
1476 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1478 /* save the current file pointer */
1479 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1482 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1485 ret
= SetEndOfFile(info
->handle
);
1486 if (!ret
) msvcrt_set_errno(GetLastError());
1489 /* restore the file pointer */
1490 _lseeki64(fd
, cur
, SEEK_SET
);
1494 release_ioinfo(info
);
1495 return ret
? 0 : *_errno();
1498 /*********************************************************************
1499 * _chsize (MSVCRT.@)
1501 int CDECL
_chsize(int fd
, __msvcrt_long size
)
1503 /* _chsize_s returns errno on failure but _chsize should return -1 */
1504 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1507 /*********************************************************************
1508 * clearerr (MSVCRT.@)
1510 void CDECL
clearerr(FILE* file
)
1512 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1515 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1519 /*********************************************************************
1520 * clearerr_s (MSVCRT.@)
1522 int CDECL
clearerr_s(FILE* file
)
1524 TRACE(":file (%p)\n",file
);
1526 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
1529 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1534 /*********************************************************************
1537 void CDECL
rewind(FILE* file
)
1539 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1542 _fseek_nolock(file
, 0L, SEEK_SET
);
1547 static int msvcrt_get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1549 int plus
= wcschr(mode
, '+') != NULL
;
1551 TRACE("%s\n", debugstr_w(mode
));
1553 while(*mode
== ' ') mode
++;
1558 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1559 *stream_flags
= plus
? _IORW
: _IOREAD
;
1562 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1563 *stream_flags
= plus
? _IORW
: _IOWRT
;
1566 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1567 *stream_flags
= plus
? _IORW
: _IOWRT
;
1570 MSVCRT_INVALID_PMT(0, EINVAL
);
1574 *stream_flags
|= MSVCRT__commode
;
1576 while (*mode
&& *mode
!=',')
1580 *open_flags
|= _O_BINARY
;
1581 *open_flags
&= ~_O_TEXT
;
1584 *open_flags
|= _O_TEXT
;
1585 *open_flags
&= ~_O_BINARY
;
1589 if(!MSVCRT_CHECK_PMT((*open_flags
& (_O_CREAT
| _O_APPEND
)) == _O_CREAT
))
1591 *open_flags
|= _O_EXCL
;
1595 *open_flags
|= _O_TEMPORARY
;
1598 *open_flags
|= _O_SHORT_LIVED
;
1601 *stream_flags
|= MSVCRT__IOCOMMIT
;
1604 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1607 *open_flags
|= _O_NOINHERIT
;
1615 if (!(*open_flags
& _O_RANDOM
))
1616 *open_flags
|= _O_SEQUENTIAL
;
1619 if (!(*open_flags
& _O_SEQUENTIAL
))
1620 *open_flags
|= _O_RANDOM
;
1623 ERR("incorrect mode flag: %c\n", mode
[-1]);
1630 while(*mode
== ' ') mode
++;
1631 if(!MSVCRT_CHECK_PMT(!wcsncmp(L
"ccs", mode
, 3)))
1634 while(*mode
== ' ') mode
++;
1635 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1638 while(*mode
== ' ') mode
++;
1640 if(!_wcsnicmp(L
"utf-8", mode
, 5))
1642 *open_flags
|= _O_U8TEXT
;
1645 else if(!_wcsnicmp(L
"utf-16le", mode
, 8))
1647 *open_flags
|= _O_U16TEXT
;
1650 else if(!_wcsnicmp(L
"unicode", mode
, 7))
1652 *open_flags
|= _O_WTEXT
;
1657 MSVCRT_INVALID_PMT(0, EINVAL
);
1661 while(*mode
== ' ') mode
++;
1664 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1669 /*********************************************************************
1670 * _fdopen (MSVCRT.@)
1672 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1675 wchar_t *modeW
= NULL
;
1677 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1679 ret
= _wfdopen(fd
, modeW
);
1685 /*********************************************************************
1686 * _wfdopen (MSVCRT.@)
1688 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1690 int open_flags
, stream_flags
;
1693 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1696 if (!(file
= msvcrt_alloc_fp()))
1698 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1703 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1709 /*********************************************************************
1710 * _filelength (MSVCRT.@)
1712 __msvcrt_long CDECL
_filelength(int fd
)
1714 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1717 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1720 if (endPos
!= curPos
)
1721 _lseek(fd
, curPos
, SEEK_SET
);
1728 /*********************************************************************
1729 * _filelengthi64 (MSVCRT.@)
1731 __int64 CDECL
_filelengthi64(int fd
)
1733 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1736 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1739 if (endPos
!= curPos
)
1740 _lseeki64(fd
, curPos
, SEEK_SET
);
1747 /*********************************************************************
1748 * _fileno (MSVCRT.@)
1750 int CDECL
_fileno(FILE* file
)
1752 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1756 /*********************************************************************
1757 * _fstat64 (MSVCRT.@)
1759 int CDECL
_fstat64(int fd
, struct _stat64
* buf
)
1761 ioinfo
*info
= get_ioinfo(fd
);
1765 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1766 if (info
->handle
== INVALID_HANDLE_VALUE
)
1768 release_ioinfo(info
);
1774 WARN(":failed-NULL buf\n");
1775 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1776 release_ioinfo(info
);
1780 memset(buf
, 0, sizeof(struct _stat64
));
1781 type
= GetFileType(info
->handle
);
1782 if (type
== FILE_TYPE_PIPE
)
1784 buf
->st_dev
= buf
->st_rdev
= fd
;
1785 buf
->st_mode
= _S_IFIFO
;
1788 else if (type
== FILE_TYPE_CHAR
)
1790 buf
->st_dev
= buf
->st_rdev
= fd
;
1791 buf
->st_mode
= _S_IFCHR
;
1794 else /* FILE_TYPE_DISK etc. */
1796 FILE_BASIC_INFORMATION basic_info
;
1797 FILE_STANDARD_INFORMATION std_info
;
1801 if ((status
= NtQueryInformationFile( info
->handle
, &io
, &basic_info
, sizeof(basic_info
), FileBasicInformation
)) ||
1802 (status
= NtQueryInformationFile( info
->handle
, &io
, &std_info
, sizeof(std_info
), FileStandardInformation
)))
1804 WARN(":failed-error %lx\n", status
);
1805 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1806 release_ioinfo(info
);
1809 buf
->st_mode
= _S_IFREG
| 0444;
1810 if (!(basic_info
.FileAttributes
& FILE_ATTRIBUTE_READONLY
))
1811 buf
->st_mode
|= 0222;
1812 buf
->st_size
= std_info
.EndOfFile
.QuadPart
;
1813 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&basic_info
.LastAccessTime
, &dw
);
1815 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&basic_info
.LastWriteTime
, &dw
);
1816 buf
->st_mtime
= buf
->st_ctime
= dw
;
1817 buf
->st_nlink
= std_info
.NumberOfLinks
;
1818 TRACE(":dwFileAttributes = %#lx, mode set to %#x\n",
1819 basic_info
.FileAttributes
, buf
->st_mode
);
1821 release_ioinfo(info
);
1825 /*********************************************************************
1826 * _fstati64 (MSVCRT.@)
1828 int CDECL
_fstati64(int fd
, struct _stati64
* buf
)
1831 struct _stat64 buf64
;
1833 ret
= _fstat64(fd
, &buf64
);
1835 msvcrt_stat64_to_stati64(&buf64
, buf
);
1839 /*********************************************************************
1842 int CDECL
_fstat(int fd
, struct _stat
* buf
)
1844 struct _stat64 buf64
;
1846 ret
= _fstat64(fd
, &buf64
);
1848 msvcrt_stat64_to_stat(&buf64
, buf
);
1852 /*********************************************************************
1853 * _fstat32 (MSVCR80.@)
1855 int CDECL
_fstat32(int fd
, struct _stat32
* buf
)
1858 struct _stat64 buf64
;
1860 ret
= _fstat64(fd
, &buf64
);
1862 msvcrt_stat64_to_stat32(&buf64
, buf
);
1866 /*********************************************************************
1867 * _fstat32i64 (MSVCR80.@)
1869 int CDECL
_fstat32i64(int fd
, struct _stat32i64
* buf
)
1872 struct _stat64 buf64
;
1874 ret
= _fstat64(fd
, &buf64
);
1876 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1880 /*********************************************************************
1881 * _fstat64i32 (MSVCR80.@)
1883 int CDECL
_fstat64i32(int fd
, struct _stat64i32
* buf
)
1886 struct _stat64 buf64
;
1888 ret
= _fstat64(fd
, &buf64
);
1890 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1894 /*********************************************************************
1895 * _futime64 (MSVCRT.@)
1897 int CDECL
_futime64(int fd
, struct __utimbuf64
*t
)
1899 ioinfo
*info
= get_ioinfo(fd
);
1904 time_to_filetime( _time64(NULL
), &at
);
1909 time_to_filetime( t
->actime
, &at
);
1910 time_to_filetime( t
->modtime
, &wt
);
1913 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1915 release_ioinfo(info
);
1916 msvcrt_set_errno(GetLastError());
1919 release_ioinfo(info
);
1923 /*********************************************************************
1924 * _futime32 (MSVCRT.@)
1926 int CDECL
_futime32(int fd
, struct __utimbuf32
*t
)
1930 struct __utimbuf64 t64
;
1931 t64
.actime
= t
->actime
;
1932 t64
.modtime
= t
->modtime
;
1933 return _futime64( fd
, &t64
);
1936 return _futime64( fd
, NULL
);
1939 /*********************************************************************
1940 * _get_osfhandle (MSVCRT.@)
1942 intptr_t CDECL
_get_osfhandle(int fd
)
1944 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1945 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1947 if(hand
== INVALID_HANDLE_VALUE
)
1949 return (intptr_t)hand
;
1952 /*********************************************************************
1953 * _mktemp_s (MSVCRT.@)
1955 int CDECL
_mktemp_s(char *pattern
, size_t size
)
1959 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1962 for(len
=0; len
<size
; len
++)
1965 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1971 for(xno
=1; xno
<=6; xno
++)
1972 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1975 id
= GetCurrentProcessId();
1976 for(xno
=1; xno
<6; xno
++) {
1977 pattern
[len
-xno
] = id
%10 + '0';
1981 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1982 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1991 /*********************************************************************
1992 * _mktemp (MSVCRT.@)
1994 char * CDECL
_mktemp(char *pattern
)
1997 char *retVal
= pattern
;
2005 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2009 id
= GetCurrentProcessId();
2013 int tempNum
= id
/ 10;
2014 *pattern
-- = id
- (tempNum
* 10) + '0';
2020 *pattern
= letter
++;
2021 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2023 } while(letter
<= 'z');
2027 /*********************************************************************
2028 * _wmktemp_s (MSVCRT.@)
2030 int CDECL
_wmktemp_s(wchar_t *pattern
, size_t size
)
2034 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2037 for(len
=0; len
<size
; len
++)
2040 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2046 for(xno
=1; xno
<=6; xno
++)
2047 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2050 id
= GetCurrentProcessId();
2051 for(xno
=1; xno
<6; xno
++) {
2052 pattern
[len
-xno
] = id
%10 + '0';
2056 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2057 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2066 /*********************************************************************
2067 * _wmktemp (MSVCRT.@)
2069 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
2072 wchar_t *retVal
= pattern
;
2074 wchar_t letter
= 'a';
2080 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2084 id
= GetCurrentProcessId();
2088 int tempNum
= id
/ 10;
2089 *pattern
-- = id
- (tempNum
* 10) + '0';
2095 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2097 *pattern
= letter
++;
2098 } while(letter
!= '|');
2102 static unsigned split_oflags(unsigned oflags
)
2105 unsigned unsupp
; /* until we support everything */
2107 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
2108 if (oflags
& _O_BINARY
) {/* Nothing to do */}
2109 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
2110 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
2111 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
2112 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
2117 if (!(fmode
& _O_BINARY
)) wxflags
|= WX_TEXT
; /* default to TEXT*/
2119 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2121 if ((unsupp
= oflags
& ~(_O_BINARY
| _O_TEXT
| _O_APPEND
| _O_TRUNC
| _O_EXCL
| _O_CREAT
|
2122 _O_RDWR
| _O_WRONLY
| _O_TEMPORARY
| _O_NOINHERIT
| _O_SEQUENTIAL
|
2123 _O_RANDOM
| _O_SHORT_LIVED
| _O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
)))
2124 ERR(":unsupported oflags %#x\n",unsupp
);
2129 /*********************************************************************
2132 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
2135 SECURITY_ATTRIBUTES sa
;
2136 HANDLE readHandle
, writeHandle
;
2144 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2145 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
2146 sa
.lpSecurityDescriptor
= NULL
;
2147 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2149 unsigned int wxflags
= split_oflags(textmode
);
2152 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2156 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2165 CloseHandle(writeHandle
);
2171 CloseHandle(readHandle
);
2172 CloseHandle(writeHandle
);
2177 msvcrt_set_errno(GetLastError());
2182 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2184 char bom
[sizeof(utf8_bom
)];
2187 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2190 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2191 oflags
= (oflags
& ~(_O_WTEXT
| _O_U16TEXT
)) | _O_U8TEXT
;
2192 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2194 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2195 oflags
= (oflags
& ~(_O_WTEXT
| _O_U8TEXT
)) | _O_U16TEXT
;
2197 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2203 /*********************************************************************
2204 * _wsopen_dispatch (UCRTBASE.@)
2206 int CDECL
_wsopen_dispatch( const wchar_t* path
, int oflags
, int shflags
, int pmode
,
2207 int *fd
, int secure
)
2209 DWORD access
= 0, creation
= 0, attrib
;
2210 SECURITY_ATTRIBUTES sa
;
2211 DWORD sharing
, type
;
2215 TRACE("path: (%s) oflags: %#x shflags: %#x pmode: %#x fd*: %p secure: %d\n",
2216 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2218 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
2221 wxflag
= split_oflags(oflags
);
2222 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
2224 case _O_RDONLY
: access
|= GENERIC_READ
; break;
2225 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
2226 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2229 if (oflags
& _O_CREAT
)
2231 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(_S_IREAD
| _S_IWRITE
))))
2234 if (oflags
& _O_EXCL
)
2235 creation
= CREATE_NEW
;
2236 else if (oflags
& _O_TRUNC
)
2237 creation
= CREATE_ALWAYS
;
2239 creation
= OPEN_ALWAYS
;
2241 else /* no _O_CREAT */
2243 if (oflags
& _O_TRUNC
)
2244 creation
= TRUNCATE_EXISTING
;
2246 creation
= OPEN_EXISTING
;
2255 sharing
= FILE_SHARE_READ
;
2258 sharing
= FILE_SHARE_WRITE
;
2261 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2264 ERR( "Unhandled shflags %#x\n", shflags
);
2268 if (!(pmode
& ~MSVCRT_umask
& _S_IWRITE
))
2269 attrib
= FILE_ATTRIBUTE_READONLY
;
2271 attrib
= FILE_ATTRIBUTE_NORMAL
;
2273 if (oflags
& _O_TEMPORARY
)
2275 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2277 sharing
|= FILE_SHARE_DELETE
;
2280 if (oflags
& _O_RANDOM
)
2281 attrib
|= FILE_FLAG_RANDOM_ACCESS
;
2282 if (oflags
& _O_SEQUENTIAL
)
2283 attrib
|= FILE_FLAG_SEQUENTIAL_SCAN
;
2284 if (oflags
& _O_SHORT_LIVED
)
2285 attrib
|= FILE_ATTRIBUTE_TEMPORARY
;
2287 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2288 sa
.lpSecurityDescriptor
= NULL
;
2289 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
2291 if ((oflags
& (_O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
))
2292 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2293 && !(access
&GENERIC_READ
))
2295 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2296 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2297 if (hand
!= INVALID_HANDLE_VALUE
)
2299 oflags
= check_bom(hand
, oflags
, FALSE
);
2304 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2305 if (hand
== INVALID_HANDLE_VALUE
) {
2306 WARN(":failed-last error (%ld)\n", GetLastError());
2307 msvcrt_set_errno(GetLastError());
2311 if (oflags
& (_O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
))
2313 LARGE_INTEGER size
= {{0}};
2315 if ((access
& GENERIC_WRITE
) && (creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
))
2316 GetFileSizeEx(hand
, &size
);
2318 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2319 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2320 || ((creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
) && !size
.QuadPart
)))
2322 if (oflags
& _O_U8TEXT
)
2324 DWORD written
= 0, tmp
;
2326 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2327 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2329 if (written
!= sizeof(utf8_bom
)) {
2330 WARN("error writing BOM\n");
2332 msvcrt_set_errno(GetLastError());
2338 DWORD written
= 0, tmp
;
2340 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2341 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2343 if (written
!= sizeof(utf16_bom
))
2345 WARN("error writing BOM\n");
2347 msvcrt_set_errno(GetLastError());
2350 oflags
|= _O_U16TEXT
;
2353 else if (access
& GENERIC_READ
)
2354 oflags
= check_bom(hand
, oflags
, TRUE
);
2357 type
= GetFileType(hand
);
2358 if (type
== FILE_TYPE_CHAR
)
2360 else if (type
== FILE_TYPE_PIPE
)
2363 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2367 if (oflags
& _O_WTEXT
)
2368 get_ioinfo_nolock(*fd
)->exflag
|= EF_UNK_UNICODE
;
2370 if (oflags
& _O_U16TEXT
)
2371 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2372 else if (oflags
& _O_U8TEXT
)
2373 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2375 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2380 /*********************************************************************
2381 * _wsopen_s (MSVCRT.@)
2383 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
2385 return _wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2388 /*********************************************************************
2389 * _wsopen (MSVCRT.@)
2391 int WINAPIV
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
2396 if (oflags
& _O_CREAT
)
2400 va_start(ap
, shflags
);
2401 pmode
= va_arg(ap
, int);
2407 return _wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2411 /*********************************************************************
2412 * _sopen_dispatch (UCRTBASE.@)
2414 int CDECL
_sopen_dispatch( const char *path
, int oflags
, int shflags
,
2415 int pmode
, int *fd
, int secure
)
2420 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2423 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2426 ret
= _wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2431 /*********************************************************************
2432 * _sopen_s (MSVCRT.@)
2434 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2436 return _sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2439 /*********************************************************************
2442 int WINAPIV
_sopen( const char *path
, int oflags
, int shflags
, ... )
2447 if (oflags
& _O_CREAT
)
2451 va_start(ap
, shflags
);
2452 pmode
= va_arg(ap
, int);
2458 return _sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2461 /*********************************************************************
2464 int WINAPIV
_open( const char *path
, int flags
, ... )
2468 if (flags
& _O_CREAT
)
2471 va_start(ap
, flags
);
2472 pmode
= va_arg(ap
, int);
2474 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
2477 return _sopen( path
, flags
, _SH_DENYNO
);
2480 /*********************************************************************
2483 int WINAPIV
_wopen(const wchar_t *path
,int flags
,...)
2487 if (flags
& _O_CREAT
)
2490 va_start(ap
, flags
);
2491 pmode
= va_arg(ap
, int);
2493 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
2496 return _wsopen( path
, flags
, _SH_DENYNO
);
2499 /*********************************************************************
2502 int CDECL
_creat(const char *path
, int pmode
)
2504 int flags
= _O_CREAT
| _O_TRUNC
| _O_RDWR
;
2505 return _open(path
, flags
, pmode
);
2508 /*********************************************************************
2509 * _wcreat (MSVCRT.@)
2511 int CDECL
_wcreat(const wchar_t *path
, int pmode
)
2513 int flags
= _O_CREAT
| _O_TRUNC
| _O_RDWR
;
2514 return _wopen(path
, flags
, pmode
);
2517 /*********************************************************************
2518 * _open_osfhandle (MSVCRT.@)
2520 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2525 /* _O_RDONLY (0) always matches, so set the read flag
2526 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2527 * file, so set the write flag. It also only sets _O_TEXT if it wants
2528 * text - it never sets _O_BINARY.
2530 /* don't let split_oflags() decide the mode if no mode is passed */
2531 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2532 oflags
|= _O_BINARY
;
2534 flags
= GetFileType((HANDLE
)handle
);
2535 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2537 msvcrt_set_errno(GetLastError());
2541 if (flags
== FILE_TYPE_CHAR
)
2543 else if (flags
== FILE_TYPE_PIPE
)
2547 flags
|= split_oflags(oflags
);
2549 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2550 TRACE(":handle (%Iu) fd (%d) flags %#lx\n", handle
, fd
, flags
);
2554 /*********************************************************************
2557 int CDECL
_rmtmp(void)
2559 int num_removed
= 0, i
;
2563 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2564 file
= msvcrt_get_file(i
);
2566 if (file
->_tmpfname
)
2575 TRACE(":removed (%d) temp files\n",num_removed
);
2579 static inline int get_utf8_char_len(char ch
)
2581 if((ch
&0xf8) == 0xf0)
2583 else if((ch
&0xf0) == 0xe0)
2585 else if((ch
&0xe0) == 0xc0)
2590 /*********************************************************************
2591 * (internal) read_utf8
2593 static int read_utf8(ioinfo
*fdinfo
, wchar_t *buf
, unsigned int count
)
2595 HANDLE hand
= fdinfo
->handle
;
2596 char min_buf
[4], *readbuf
, lookahead
;
2597 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2599 /* make the buffer big enough to hold at least one character */
2600 /* read bytes have to fit to output and lookahead buffers */
2602 readbuf_size
= count
< 4 ? 4 : count
;
2603 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2608 if(fdinfo
->lookahead
[0] != '\n') {
2609 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2610 fdinfo
->lookahead
[0] = '\n';
2612 if(fdinfo
->lookahead
[1] != '\n') {
2613 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2614 fdinfo
->lookahead
[1] = '\n';
2616 if(fdinfo
->lookahead
[2] != '\n') {
2617 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2618 fdinfo
->lookahead
[2] = '\n';
2623 /* NOTE: this case is broken in native dll, reading
2624 * sometimes fails when small buffer is passed
2627 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2628 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2629 fdinfo
->wxflag
|= WX_ATEOF
;
2632 msvcrt_set_errno(GetLastError());
2635 }else if(!num_read
) {
2636 fdinfo
->wxflag
|= WX_ATEOF
;
2642 char_len
= get_utf8_char_len(readbuf
[0]);
2644 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2648 if(readbuf
[0] == '\n')
2649 fdinfo
->wxflag
|= WX_READNL
;
2651 fdinfo
->wxflag
&= ~WX_READNL
;
2653 if(readbuf
[0] == 0x1a) {
2654 fdinfo
->wxflag
|= WX_ATEOF
;
2658 if(readbuf
[0] == '\r') {
2659 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2661 else if(lookahead
== '\n')
2665 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2666 fdinfo
->lookahead
[0] = lookahead
;
2668 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2673 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2674 msvcrt_set_errno(GetLastError());
2681 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2684 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2685 fdinfo
->wxflag
|= WX_ATEOF
;
2686 if (readbuf
!= min_buf
) free(readbuf
);
2689 msvcrt_set_errno(GetLastError());
2690 if (readbuf
!= min_buf
) free(readbuf
);
2693 }else if(!pos
&& !num_read
) {
2694 fdinfo
->wxflag
|= WX_ATEOF
;
2695 if (readbuf
!= min_buf
) free(readbuf
);
2700 if(readbuf
[0] == '\n')
2701 fdinfo
->wxflag
|= WX_READNL
;
2703 fdinfo
->wxflag
&= ~WX_READNL
;
2705 /* Find first byte of last character (may be incomplete) */
2706 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2707 if((readbuf
[i
]&0xc0) != 0x80)
2709 char_len
= get_utf8_char_len(readbuf
[i
]);
2710 if(char_len
+i
<= pos
)
2713 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2715 fdinfo
->lookahead
[0] = readbuf
[i
];
2717 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2719 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2721 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2725 for(i
=0, j
=0; i
<pos
; i
++) {
2726 if(readbuf
[i
] == 0x1a) {
2727 fdinfo
->wxflag
|= WX_ATEOF
;
2731 /* strip '\r' if followed by '\n' */
2732 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2733 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2734 readbuf
[j
++] = '\r';
2735 }else if(lookahead
== '\n' && j
==0) {
2736 readbuf
[j
++] = '\n';
2738 if(lookahead
!= '\n')
2739 readbuf
[j
++] = '\r';
2741 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2742 fdinfo
->lookahead
[0] = lookahead
;
2744 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2746 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2747 readbuf
[j
++] = readbuf
[i
];
2752 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2753 msvcrt_set_errno(GetLastError());
2754 if (readbuf
!= min_buf
) free(readbuf
);
2758 if (readbuf
!= min_buf
) free(readbuf
);
2762 /*********************************************************************
2765 * When reading \r as last character in text mode, read() positions
2766 * the file pointer on the \r character while getc() goes on to
2769 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2771 DWORD num_read
, utf16
;
2772 char *bufstart
= buf
;
2777 if (fdinfo
->wxflag
& WX_ATEOF
) {
2778 TRACE("already at EOF, returning 0\n");
2781 /* Don't trace small reads, it gets *very* annoying */
2783 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2784 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2790 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2791 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2797 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2798 return read_utf8(fdinfo
, buf
, count
);
2800 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2802 if (fdinfo
->lookahead
[0] != '\n')
2804 bufstart
[0] = fdinfo
->lookahead
[0];
2805 fdinfo
->lookahead
[0] = '\n';
2809 bufstart
[1] = fdinfo
->lookahead
[1];
2810 fdinfo
->lookahead
[1] = '\n';
2813 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2814 num_read
+= 1+utf16
;
2819 if(utf16
&& (num_read
&1))
2821 /* msvcr90 uses uninitialized value from the buffer in this case */
2822 /* msvcrt ignores additional data */
2823 ERR("got odd number of bytes in UTF16 mode\n");
2827 if (count
!= 0 && num_read
== 0)
2829 fdinfo
->wxflag
|= WX_ATEOF
;
2830 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2832 else if (fdinfo
->wxflag
& WX_TEXT
)
2836 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2837 fdinfo
->wxflag
|= WX_READNL
;
2839 fdinfo
->wxflag
&= ~WX_READNL
;
2841 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2843 /* in text mode, a ctrl-z signals EOF */
2844 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2846 fdinfo
->wxflag
|= WX_ATEOF
;
2847 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2851 /* in text mode, strip \r if followed by \n */
2852 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2857 lookahead
[1] = '\n';
2858 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2860 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2862 bufstart
[j
++] = '\n';
2863 if(utf16
) bufstart
[j
++] = 0;
2867 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2869 bufstart
[j
++] = '\r';
2870 if(utf16
) bufstart
[j
++] = 0;
2873 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2875 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2877 bufstart
[j
++] = '\n';
2878 if (utf16
) bufstart
[j
++] = 0;
2882 fdinfo
->lookahead
[0] = lookahead
[0];
2883 fdinfo
->lookahead
[1] = lookahead
[1];
2887 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2892 bufstart
[j
++] = '\r';
2893 if(utf16
) bufstart
[j
++] = 0;
2896 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2897 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2899 bufstart
[j
++] = bufstart
[i
];
2900 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2908 if (GetLastError() == ERROR_BROKEN_PIPE
)
2910 TRACE(":end-of-pipe\n");
2911 fdinfo
->wxflag
|= WX_ATEOF
;
2916 TRACE(":failed-last error (%ld)\n", GetLastError());
2917 msvcrt_set_errno(GetLastError());
2923 TRACE("(%lu), %s\n", num_read
, debugstr_an(buf
, num_read
));
2927 /*********************************************************************
2930 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2935 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
2940 info
= get_ioinfo(fd
);
2941 num_read
= read_i(fd
, info
, buf
, count
);
2942 release_ioinfo(info
);
2946 /*********************************************************************
2947 * _setmode (MSVCRT.@)
2949 int CDECL
_setmode(int fd
,int mode
)
2951 ioinfo
*info
= get_ioinfo(fd
);
2952 int ret
= info
->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2953 if(ret
==_O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2956 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2957 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2959 release_ioinfo(info
);
2963 if(info
== &MSVCRT___badioinfo
) {
2968 if(mode
== _O_BINARY
) {
2969 info
->wxflag
&= ~WX_TEXT
;
2970 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2971 release_ioinfo(info
);
2975 info
->wxflag
|= WX_TEXT
;
2977 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2978 else if(mode
== _O_U8TEXT
)
2979 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2981 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2983 release_ioinfo(info
);
2987 /*********************************************************************
2988 * _stat64 (MSVCRT.@)
2990 int CDECL
_stat64(const char* path
, struct _stat64
* buf
)
2993 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2994 unsigned short mode
= ALL_S_IREAD
;
2997 TRACE(":file (%s) buf(%p)\n", path
, buf
);
2999 plen
= strlen(path
);
3000 while (plen
&& path
[plen
-1]==' ')
3003 if (plen
==2 && path
[1]==':')
3010 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3017 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
3019 TRACE("failed (%ld)\n", GetLastError());
3024 memset(buf
,0,sizeof(struct _stat64
));
3026 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3027 Bon 011120: This FIXME seems incorrect
3028 Also a letter as first char isn't enough to be classified
3031 if (isalpha(*path
)&& (*(path
+1)==':'))
3032 buf
->st_dev
= buf
->st_rdev
= _toupper_l(*path
, NULL
) - 'A'; /* drive num */
3034 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
3036 /* Dir, or regular file? */
3037 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3038 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
3043 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3045 unsigned int ext
= _tolower_l(path
[plen
-1], NULL
) |
3046 (_tolower_l(path
[plen
-2], NULL
) << 8) |
3047 (_tolower_l(path
[plen
-3], NULL
) << 16);
3048 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3049 mode
|= ALL_S_IEXEC
;
3053 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3054 mode
|= ALL_S_IWRITE
;
3056 buf
->st_mode
= mode
;
3058 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3059 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3061 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3062 buf
->st_mtime
= buf
->st_ctime
= dw
;
3063 TRACE("%d %d %#I64x %I64d %I64d %I64d\n", buf
->st_mode
, buf
->st_nlink
,
3064 buf
->st_size
, buf
->st_atime
, buf
->st_mtime
, buf
->st_ctime
);
3068 /*********************************************************************
3069 * _stati64 (MSVCRT.@)
3071 int CDECL
_stati64(const char* path
, struct _stati64
* buf
)
3074 struct _stat64 buf64
;
3076 ret
= _stat64(path
, &buf64
);
3078 msvcrt_stat64_to_stati64(&buf64
, buf
);
3082 /*********************************************************************
3085 int CDECL
_stat(const char* path
, struct _stat
* buf
)
3088 struct _stat64 buf64
;
3090 ret
= _stat64( path
, &buf64
);
3092 msvcrt_stat64_to_stat(&buf64
, buf
);
3096 #if _MSVCR_VER >= 80
3098 /*********************************************************************
3099 * _stat32 (MSVCR80.@)
3101 int CDECL
_stat32(const char *path
, struct _stat32
*buf
)
3104 struct _stat64 buf64
;
3106 ret
= _stat64(path
, &buf64
);
3108 msvcrt_stat64_to_stat32(&buf64
, buf
);
3112 /*********************************************************************
3113 * _stat32i64 (MSVCR80.@)
3115 int CDECL
_stat32i64(const char *path
, struct _stat32i64
*buf
)
3118 struct _stat64 buf64
;
3120 ret
= _stat64(path
, &buf64
);
3122 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3126 /*********************************************************************
3127 * _stat64i32 (MSVCR80.@)
3129 int CDECL
_stat64i32(const char* path
, struct _stat64i32
*buf
)
3132 struct _stat64 buf64
;
3134 ret
= _stat64(path
, &buf64
);
3136 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3140 #endif /* _MSVCR_VER >= 80 */
3142 /*********************************************************************
3143 * _wstat64 (MSVCRT.@)
3145 int CDECL
_wstat64(const wchar_t* path
, struct _stat64
* buf
)
3148 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3149 unsigned short mode
= ALL_S_IREAD
;
3152 TRACE(":file (%s) buf(%p)\n", debugstr_w(path
), buf
);
3154 plen
= wcslen(path
);
3155 while (plen
&& path
[plen
-1]==' ')
3158 if (plen
==2 && path
[1]==':')
3165 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3172 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3174 TRACE("failed (%ld)\n", GetLastError());
3179 memset(buf
,0,sizeof(struct _stat64
));
3181 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3182 if (iswalpha(*path
) && path
[1] == ':')
3183 buf
->st_dev
= buf
->st_rdev
= towupper(*path
) - 'A'; /* drive num */
3185 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
3187 /* Dir, or regular file? */
3188 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3189 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
3194 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3196 ULONGLONG ext
= towlower(path
[plen
-1]) | (towlower(path
[plen
-2]) << 16) |
3197 ((ULONGLONG
)towlower(path
[plen
-3]) << 32);
3198 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3199 mode
|= ALL_S_IEXEC
;
3203 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3204 mode
|= ALL_S_IWRITE
;
3206 buf
->st_mode
= mode
;
3208 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3209 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3211 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3212 buf
->st_mtime
= buf
->st_ctime
= dw
;
3213 TRACE("%d %d %#I64x %I64d %I64d %I64d\n", buf
->st_mode
, buf
->st_nlink
,
3214 buf
->st_size
, buf
->st_atime
, buf
->st_mtime
, buf
->st_ctime
);
3218 /*********************************************************************
3219 * _wstati64 (MSVCRT.@)
3221 int CDECL
_wstati64(const wchar_t* path
, struct _stati64
* buf
)
3224 struct _stat64 buf64
;
3226 ret
= _wstat64(path
, &buf64
);
3228 msvcrt_stat64_to_stati64(&buf64
, buf
);
3232 /*********************************************************************
3235 int CDECL
_wstat(const wchar_t* path
, struct _stat
* buf
)
3238 struct _stat64 buf64
;
3240 ret
= _wstat64( path
, &buf64
);
3241 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3245 #if _MSVCR_VER >= 80
3247 /*********************************************************************
3248 * _wstat32 (MSVCR80.@)
3250 int CDECL
_wstat32(const wchar_t *path
, struct _stat32
*buf
)
3253 struct _stat64 buf64
;
3255 ret
= _wstat64(path
, &buf64
);
3257 msvcrt_stat64_to_stat32(&buf64
, buf
);
3261 /*********************************************************************
3262 * _wstat32i64 (MSVCR80.@)
3264 int CDECL
_wstat32i64(const wchar_t *path
, struct _stat32i64
*buf
)
3267 struct _stat64 buf64
;
3269 ret
= _wstat64(path
, &buf64
);
3271 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3275 /*********************************************************************
3276 * _wstat64i32 (MSVCR80.@)
3278 int CDECL
_wstat64i32(const wchar_t *path
, struct _stat64i32
*buf
)
3281 struct _stat64 buf64
;
3283 ret
= _wstat64(path
, &buf64
);
3285 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3289 #endif /* _MSVCR_VER >= 80 */
3291 /*********************************************************************
3294 __msvcrt_long CDECL
_tell(int fd
)
3296 return _lseek(fd
, 0, SEEK_CUR
);
3299 /*********************************************************************
3300 * _telli64 (MSVCRT.@)
3302 __int64 CDECL
_telli64(int fd
)
3304 return _lseeki64(fd
, 0, SEEK_CUR
);
3307 /*********************************************************************
3308 * _tempnam (MSVCRT.@)
3310 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
3312 char tmpbuf
[MAX_PATH
];
3313 const char *tmp_dir
= getenv("TMP");
3315 if (tmp_dir
) dir
= tmp_dir
;
3317 TRACE("dir (%s) prefix (%s)\n", dir
, prefix
);
3318 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3320 TRACE("got name (%s)\n", tmpbuf
);
3321 DeleteFileA(tmpbuf
);
3322 return _strdup(tmpbuf
);
3324 TRACE("failed (%ld)\n", GetLastError());
3328 /*********************************************************************
3329 * _wtempnam (MSVCRT.@)
3331 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
3333 wchar_t tmpbuf
[MAX_PATH
];
3334 const wchar_t *tmp_dir
= _wgetenv(L
"TMP");
3336 if (tmp_dir
) dir
= tmp_dir
;
3338 TRACE("dir (%s) prefix (%s)\n", debugstr_w(dir
), debugstr_w(prefix
));
3339 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3341 TRACE("got name (%s)\n", debugstr_w(tmpbuf
));
3342 DeleteFileW(tmpbuf
);
3343 return _wcsdup(tmpbuf
);
3345 TRACE("failed (%ld)\n", GetLastError());
3349 /*********************************************************************
3352 int CDECL
_umask(int umask
)
3354 int old_umask
= MSVCRT_umask
;
3355 TRACE("(%d)\n",umask
);
3356 MSVCRT_umask
= umask
;
3360 /*********************************************************************
3361 * _utime64 (MSVCRT.@)
3363 int CDECL
_utime64(const char* path
, struct __utimbuf64
*t
)
3365 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
3369 int retVal
= _futime64(fd
, t
);
3376 /*********************************************************************
3377 * _utime32 (MSVCRT.@)
3379 int CDECL
_utime32(const char* path
, struct __utimbuf32
*t
)
3383 struct __utimbuf64 t64
;
3384 t64
.actime
= t
->actime
;
3385 t64
.modtime
= t
->modtime
;
3386 return _utime64( path
, &t64
);
3389 return _utime64( path
, NULL
);
3392 /*********************************************************************
3393 * _wutime64 (MSVCRT.@)
3395 int CDECL
_wutime64(const wchar_t* path
, struct __utimbuf64
*t
)
3397 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
3401 int retVal
= _futime64(fd
, t
);
3408 /*********************************************************************
3409 * _wutime32 (MSVCRT.@)
3411 int CDECL
_wutime32(const wchar_t* path
, struct __utimbuf32
*t
)
3415 struct __utimbuf64 t64
;
3416 t64
.actime
= t
->actime
;
3417 t64
.modtime
= t
->modtime
;
3418 return _wutime64( path
, &t64
);
3421 return _wutime64( path
, NULL
);
3424 /*********************************************************************
3427 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
3429 ioinfo
*info
= get_ioinfo(fd
);
3430 HANDLE hand
= info
->handle
;
3431 DWORD num_written
, i
;
3432 BOOL console
= FALSE
;
3434 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3437 release_ioinfo(info
);
3441 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3444 release_ioinfo(info
);
3448 /* If appending, go to EOF */
3449 if (info
->wxflag
& WX_APPEND
)
3450 _lseek(fd
, 0, FILE_END
);
3452 if (!(info
->wxflag
& WX_TEXT
))
3454 if (!WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3455 || num_written
!= count
)
3457 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd
,
3458 hand
, GetLastError());
3459 msvcrt_set_errno(GetLastError());
3463 release_ioinfo(info
);
3467 if (_isatty(fd
)) console
= VerifyConsoleIoHandle(hand
);
3468 for (i
= 0; i
< count
;)
3470 const char *s
= buf
;
3474 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)) && console
)
3476 char conv
[sizeof(lfbuf
)];
3479 #if _MSVCR_VER >= 90
3480 if (info
->dbcsBufferUsed
)
3482 conv
[j
++] = info
->dbcsBuffer
;
3483 info
->dbcsBufferUsed
= FALSE
;
3489 for (; i
< count
&& j
< sizeof(conv
)-1 &&
3490 len
< (sizeof(lfbuf
) - 1) / sizeof(WCHAR
); i
++, j
++, len
++)
3492 if (isleadbyte((unsigned char)s
[i
]))
3498 #if _MSVCR_VER >= 90
3499 info
->dbcsBuffer
= conv
[j
-1];
3500 info
->dbcsBufferUsed
= TRUE
;
3504 release_ioinfo(info
);
3509 else if (s
[i
] == '\n')
3517 len
= mbstowcs((WCHAR
*)lfbuf
, conv
, len
);
3520 msvcrt_set_errno(GetLastError());
3521 release_ioinfo(info
);
3526 else if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3528 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-1; i
++, j
++)
3535 else if (info
->exflag
& EF_UTF16
|| console
)
3537 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-3; i
++, j
++)
3539 if (s
[i
] == '\n' && !s
[i
+1])
3544 lfbuf
[j
++] = s
[i
++];
3550 char conv
[sizeof(lfbuf
)/4];
3552 for (j
= 0; i
< count
&& j
< sizeof(conv
)-3; i
++, j
++)
3554 if (s
[i
] == '\n' && !s
[i
+1])
3563 j
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)conv
, j
/2, lfbuf
, sizeof(lfbuf
), NULL
, NULL
);
3566 msvcrt_set_errno(GetLastError());
3567 release_ioinfo(info
);
3575 if (!WriteConsoleW(hand
, lfbuf
, j
, &num_written
, NULL
))
3578 else if (!WriteFile(hand
, lfbuf
, j
, &num_written
, NULL
))
3583 if (num_written
!= j
)
3585 TRACE("WriteFile/WriteConsoleW (fd %d, hand %p) failed-last error (%ld)\n", fd
,
3586 hand
, GetLastError());
3587 msvcrt_set_errno(GetLastError());
3588 release_ioinfo(info
);
3593 release_ioinfo(info
);
3597 /*********************************************************************
3600 int CDECL
_putw(int val
, FILE* file
)
3605 len
= _write(file
->_file
, &val
, sizeof(val
));
3606 if (len
== sizeof(val
)) {
3611 file
->_flag
|= _IOERR
;
3616 /*********************************************************************
3619 int CDECL
fclose(FILE* file
)
3623 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3626 ret
= _fclose_nolock(file
);
3632 /*********************************************************************
3633 * _fclose_nolock (MSVCRT.@)
3635 int CDECL
_fclose_nolock(FILE* file
)
3639 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3641 if(!(file
->_flag
& (_IOREAD
| _IOWRT
| _IORW
)))
3648 free(file
->_tmpfname
);
3649 file
->_tmpfname
= NULL
;
3650 /* flush stdio buffers */
3651 if(file
->_flag
& _IOWRT
)
3652 _fflush_nolock(file
);
3653 if(file
->_flag
& _IOMYBUF
)
3656 r
=_close(file
->_file
);
3659 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
3662 /*********************************************************************
3665 int CDECL
feof(FILE* file
)
3667 return file
->_flag
& _IOEOF
;
3670 /*********************************************************************
3673 int CDECL
ferror(FILE* file
)
3675 return file
->_flag
& _IOERR
;
3678 /*********************************************************************
3679 * _filbuf (MSVCRT.@)
3681 int CDECL
_filbuf(FILE* file
)
3685 if(file
->_flag
& _IOSTRG
)
3688 /* Allocate buffer if needed */
3689 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
)))
3690 msvcrt_alloc_buffer(file
);
3692 if(!(file
->_flag
& _IOREAD
)) {
3693 if(file
->_flag
& _IORW
)
3694 file
->_flag
|= _IOREAD
;
3699 if(!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
))) {
3701 if ((r
= _read(file
->_file
,&c
,1)) != 1) {
3702 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
3708 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3710 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
3716 file
->_ptr
= file
->_base
+1;
3717 c
= *(unsigned char *)file
->_base
;
3722 /*********************************************************************
3725 int CDECL
fgetc(FILE* file
)
3730 ret
= _fgetc_nolock(file
);
3736 /*********************************************************************
3737 * _fgetc_nolock (MSVCRT.@)
3739 int CDECL
_fgetc_nolock(FILE* file
)
3746 i
= (unsigned char *)file
->_ptr
++;
3754 /*********************************************************************
3755 * _fgetchar (MSVCRT.@)
3757 int CDECL
_fgetchar(void)
3759 return fgetc(MSVCRT_stdin
);
3762 /*********************************************************************
3765 char * CDECL
fgets(char *s
, int size
, FILE* file
)
3768 char * buf_start
= s
;
3770 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3771 file
,file
->_file
,s
,size
);
3775 while ((size
>1) && (cc
= _fgetc_nolock(file
)) != EOF
&& cc
!= '\n')
3780 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3782 TRACE(":nothing read\n");
3786 if ((cc
!= EOF
) && (size
> 1))
3789 TRACE(":got %s\n", debugstr_a(buf_start
));
3794 /*********************************************************************
3797 wint_t CDECL
fgetwc(FILE* file
)
3802 ret
= _fgetwc_nolock(file
);
3808 /*********************************************************************
3809 * _fgetwc_nolock (MSVCRT.@)
3811 wint_t CDECL
_fgetwc_nolock(FILE* file
)
3816 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3817 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3820 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
3821 ch
= _fgetc_nolock(file
);
3829 char mbs
[MB_LEN_MAX
];
3832 ch
= _fgetc_nolock(file
);
3835 if(isleadbyte((unsigned char)mbs
[0])) {
3836 ch
= _fgetc_nolock(file
);
3846 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
3853 /*********************************************************************
3856 int CDECL
_getw(FILE* file
)
3864 for (j
=0; j
<sizeof(int); j
++) {
3865 k
= _fgetc_nolock(file
);
3867 file
->_flag
|= _IOEOF
;
3878 /*********************************************************************
3881 wint_t CDECL
getwc(FILE* file
)
3883 return fgetwc(file
);
3886 /*********************************************************************
3887 * _fgetwchar (MSVCRT.@)
3889 wint_t CDECL
_fgetwchar(void)
3891 return fgetwc(MSVCRT_stdin
);
3894 /*********************************************************************
3895 * getwchar (MSVCRT.@)
3897 wint_t CDECL
getwchar(void)
3899 return _fgetwchar();
3902 /*********************************************************************
3905 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
3908 wchar_t * buf_start
= s
;
3910 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3911 file
,file
->_file
,s
,size
);
3915 while ((size
>1) && (cc
= _fgetwc_nolock(file
)) != WEOF
&& cc
!= '\n')
3920 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3922 TRACE(":nothing read\n");
3926 if ((cc
!= WEOF
) && (size
> 1))
3929 TRACE(":got %s\n", debugstr_w(buf_start
));
3934 /*********************************************************************
3935 * _flsbuf (MSVCRT.@)
3937 int CDECL
_flsbuf(int c
, FILE* file
)
3939 /* Flush output buffer */
3940 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
))) {
3941 msvcrt_alloc_buffer(file
);
3944 if(!(file
->_flag
& _IOWRT
)) {
3945 if(!(file
->_flag
& _IORW
)) {
3946 file
->_flag
|= _IOERR
;
3950 file
->_flag
|= _IOWRT
;
3952 if(file
->_flag
& _IOREAD
) {
3953 if(!(file
->_flag
& _IOEOF
)) {
3954 file
->_flag
|= _IOERR
;
3958 file
->_ptr
= file
->_base
;
3959 file
->_flag
&= ~(_IOREAD
| _IOEOF
);
3962 if(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) {
3965 if(file
->_cnt
<= 0) {
3966 res
= msvcrt_flush_buffer(file
);
3969 file
->_flag
|= _IOWRT
;
3970 file
->_cnt
=file
->_bufsiz
;
3978 /* set _cnt to 0 for unbuffered FILEs */
3980 len
= _write(file
->_file
, &cc
, 1);
3983 file
->_flag
|= _IOERR
;
3988 /*********************************************************************
3991 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3996 ret
= _fwrite_nolock(ptr
, size
, nmemb
, file
);
4002 /*********************************************************************
4003 * _fwrite_nolock (MSVCRT.@)
4005 size_t CDECL
_fwrite_nolock(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4007 size_t wrcnt
=size
* nmemb
;
4013 if(file
->_cnt
< 0) {
4014 WARN("negative file->_cnt value in %p\n", file
);
4015 file
->_flag
|= _IOERR
;
4017 } else if(file
->_cnt
) {
4018 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
4019 memcpy(file
->_ptr
, ptr
, pcnt
);
4024 ptr
= (const char*)ptr
+ pcnt
;
4025 } else if((file
->_flag
& _IONBF
)
4026 || ((file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4027 || (!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4031 if(file
->_flag
& _IONBF
)
4033 else if(!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)))
4034 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4036 bufsiz
= file
->_bufsiz
;
4038 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4040 if(msvcrt_flush_buffer(file
) == EOF
)
4043 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
4044 file
->_flag
|= _IOERR
;
4049 ptr
= (const char*)ptr
+ pcnt
;
4051 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
4055 ptr
= (const char*)ptr
+ 1;
4059 return written
/ size
;
4062 /*********************************************************************
4065 wint_t CDECL
fputwc(wint_t wc
, FILE* file
)
4070 ret
= _fputwc_nolock(wc
, file
);
4076 /*********************************************************************
4077 * _fputwc_nolock (MSVCRT.@)
4079 wint_t CDECL
_fputwc_nolock(wint_t wc
, FILE* file
)
4085 fdinfo
= get_ioinfo_nolock(file
->_file
);
4087 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
4088 char buf
[MB_LEN_MAX
];
4091 char_len
= wctomb(buf
, mwc
);
4092 if(char_len
!=-1 && _fwrite_nolock(buf
, char_len
, 1, file
)==1)
4096 }else if(_fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4105 /*********************************************************************
4106 * _fputwchar (MSVCRT.@)
4108 wint_t CDECL
_fputwchar(wint_t wc
)
4110 return fputwc(wc
, MSVCRT_stdout
);
4113 /*********************************************************************
4114 * _wfsopen (MSVCRT.@)
4116 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
4119 int open_flags
, stream_flags
, fd
;
4121 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4123 /* map mode string to open() flags. "man fopen" for possibilities. */
4124 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4128 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
4131 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4133 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4140 TRACE(":got (%p)\n",file
);
4141 if (fd
>= 0 && !file
)
4147 /*********************************************************************
4148 * _fsopen (MSVCRT.@)
4150 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
4153 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4155 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4156 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4160 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4163 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4168 ret
= _wfsopen(pathW
, modeW
, share
);
4175 /*********************************************************************
4178 FILE * CDECL
fopen(const char *path
, const char *mode
)
4180 return _fsopen( path
, mode
, _SH_DENYNO
);
4183 /*********************************************************************
4184 * fopen_s (MSVCRT.@)
4186 int CDECL
fopen_s(FILE** pFile
,
4187 const char *filename
, const char *mode
)
4189 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4190 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
4191 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4193 *pFile
= fopen(filename
, mode
);
4200 /*********************************************************************
4201 * _wfopen (MSVCRT.@)
4203 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
4205 return _wfsopen( path
, mode
, _SH_DENYNO
);
4208 /*********************************************************************
4209 * _wfopen_s (MSVCRT.@)
4211 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
4212 const wchar_t *mode
)
4214 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4215 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
4216 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4218 *pFile
= _wfopen(filename
, mode
);
4225 /*********************************************************************
4228 int CDECL
fputc(int c
, FILE* file
)
4233 ret
= _fputc_nolock(c
, file
);
4239 /*********************************************************************
4240 * _fputc_nolock (MSVCRT.@)
4242 int CDECL
_fputc_nolock(int c
, FILE* file
)
4251 res
= msvcrt_flush_buffer(file
);
4252 return res
? res
: c
;
4258 res
= _flsbuf(c
, file
);
4263 /*********************************************************************
4264 * _fputchar (MSVCRT.@)
4266 int CDECL
_fputchar(int c
)
4268 return fputc(c
, MSVCRT_stdout
);
4271 /*********************************************************************
4274 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4279 ret
= _fread_nolock(ptr
, size
, nmemb
, file
);
4285 /*********************************************************************
4286 * _fread_nolock (MSVCRT.@)
4288 size_t CDECL
_fread_nolock(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4290 size_t rcnt
=size
* nmemb
;
4297 /* first buffered data */
4299 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4300 memcpy(ptr
, file
->_ptr
, pcnt
);
4305 ptr
= (char*)ptr
+ pcnt
;
4306 } else if(!(file
->_flag
& _IOREAD
)) {
4307 if(file
->_flag
& _IORW
) {
4308 file
->_flag
|= _IOREAD
;
4314 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
)))
4315 msvcrt_alloc_buffer(file
);
4320 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
))) {
4321 i
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
4322 file
->_ptr
= file
->_base
;
4325 if (i
> rcnt
) i
= rcnt
;
4327 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4328 if (i
> 0 && i
< file
->_cnt
) {
4329 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4330 file
->_flag
&= ~_IOEOF
;
4333 memcpy(ptr
, file
->_ptr
, i
);
4337 } else if (rcnt
> INT_MAX
) {
4338 i
= _read(file
->_file
, ptr
, INT_MAX
);
4339 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4340 i
= _read(file
->_file
, ptr
, rcnt
);
4342 i
= _read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4346 ptr
= (char *)ptr
+i
;
4347 /* expose feof condition in the flags
4348 * MFC tests file->_flag for feof, and doesn't call feof())
4350 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4351 file
->_flag
|= _IOEOF
;
4354 file
->_flag
|= _IOERR
;
4364 #if _MSVCR_VER >= 80
4366 /*********************************************************************
4367 * fread_s (MSVCR80.@)
4369 size_t CDECL
fread_s(void *buf
, size_t buf_size
, size_t elem_size
,
4370 size_t count
, FILE *stream
)
4374 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4376 memset(buf
, 0, buf_size
);
4379 if(!elem_size
|| !count
) return 0;
4382 ret
= _fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4383 _unlock_file(stream
);
4388 /*********************************************************************
4389 * _fread_nolock_s (MSVCR80.@)
4391 size_t CDECL
_fread_nolock_s(void *buf
, size_t buf_size
, size_t elem_size
,
4392 size_t count
, FILE *stream
)
4394 size_t bytes_left
, buf_pos
;
4396 TRACE("(%p %Iu %Iu %Iu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4398 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4400 memset(buf
, 0, buf_size
);
4403 if(!elem_size
|| !count
) return 0;
4404 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4405 if(!MSVCRT_CHECK_PMT(SIZE_MAX
/count
>= elem_size
)) return 0;
4407 bytes_left
= elem_size
*count
;
4410 if(stream
->_cnt
> 0) {
4411 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4413 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, ERANGE
)) {
4414 memset(buf
, 0, buf_size
);
4418 _fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4422 int c
= _filbuf(stream
);
4427 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, ERANGE
)) {
4428 memset(buf
, 0, buf_size
);
4432 ((char*)buf
)[buf_pos
++] = c
;
4437 return buf_pos
/elem_size
;
4440 #endif /* _MSVCR_VER >= 80 */
4442 /*********************************************************************
4443 * _wfreopen (MSVCRT.@)
4446 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
4448 int open_flags
, stream_flags
, fd
;
4450 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4453 if (!file
|| ((fd
= file
->_file
) < 0))
4458 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4460 else if((fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
)) < 0)
4462 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4472 /*********************************************************************
4473 * _wfreopen_s (MSVCRT.@)
4475 int CDECL
_wfreopen_s(FILE** pFile
,
4476 const wchar_t *path
, const wchar_t *mode
, FILE* file
)
4478 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4479 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return EINVAL
;
4480 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4481 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
4483 *pFile
= _wfreopen(path
, mode
, file
);
4490 /*********************************************************************
4491 * freopen (MSVCRT.@)
4494 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
4497 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4499 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4500 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4506 ret
= _wfreopen(pathW
, modeW
, file
);
4513 /*********************************************************************
4514 * freopen_s (MSVCRT.@)
4516 int CDECL
freopen_s(FILE** pFile
,
4517 const char *path
, const char *mode
, FILE* file
)
4519 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4520 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return EINVAL
;
4521 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4522 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
4524 *pFile
= freopen(path
, mode
, file
);
4531 /*********************************************************************
4532 * fsetpos (MSVCRT.@)
4534 int CDECL
fsetpos(FILE* file
, fpos_t *pos
)
4539 msvcrt_flush_buffer(file
);
4541 /* Reset direction of i/o */
4542 if(file
->_flag
& _IORW
) {
4543 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
4546 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4551 /*********************************************************************
4552 * _ftelli64 (MSVCRT.@)
4554 __int64 CDECL
_ftelli64(FILE* file
)
4559 ret
= _ftelli64_nolock(file
);
4565 /*********************************************************************
4566 * _ftelli64_nolock (MSVCRT.@)
4568 __int64 CDECL
_ftelli64_nolock(FILE* file
)
4572 pos
= _telli64(file
->_file
);
4575 if(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) {
4576 if(file
->_flag
& _IOWRT
) {
4577 pos
+= file
->_ptr
- file
->_base
;
4579 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4582 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4586 } else if(!file
->_cnt
) { /* nothing to do */
4587 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4591 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4592 for(i
=0; i
<file
->_cnt
; i
++)
4593 if(file
->_ptr
[i
] == '\n')
4599 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4602 pos
-= file
->_bufsiz
;
4603 pos
+= file
->_ptr
- file
->_base
;
4605 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4606 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4609 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4619 /*********************************************************************
4622 __msvcrt_long CDECL
ftell(FILE* file
)
4624 return _ftelli64(file
);
4627 #if _MSVCR_VER >= 80
4628 /*********************************************************************
4629 * _ftell_nolock (MSVCR80.@)
4631 __msvcrt_long CDECL
_ftell_nolock(FILE* file
)
4633 return _ftelli64_nolock(file
);
4637 /*********************************************************************
4638 * fgetpos (MSVCRT.@)
4640 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
4642 *pos
= _ftelli64(file
);
4648 /*********************************************************************
4651 int CDECL
fputs(const char *s
, FILE* file
)
4653 size_t len
= strlen(s
);
4657 ret
= _fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
4662 /*********************************************************************
4665 int CDECL
fputws(const wchar_t *s
, FILE* file
)
4667 size_t i
, len
= wcslen(s
);
4672 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4673 ret
= _fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
4678 tmp_buf
= add_std_buffer(file
);
4679 for (i
=0; i
<len
; i
++) {
4680 if(_fputwc_nolock(s
[i
], file
) == WEOF
) {
4681 if(tmp_buf
) remove_std_buffer(file
);
4687 if(tmp_buf
) remove_std_buffer(file
);
4692 /*********************************************************************
4693 * getchar (MSVCRT.@)
4695 int CDECL
getchar(void)
4697 return fgetc(MSVCRT_stdin
);
4700 /*********************************************************************
4703 int CDECL
getc(FILE* file
)
4708 /*********************************************************************
4709 * gets_s (MSVCR80.@)
4711 char * CDECL
gets_s(char *buf
, size_t len
)
4713 char *buf_start
= buf
;
4716 if (!MSVCRT_CHECK_PMT(buf
!= NULL
)) return NULL
;
4717 if (!MSVCRT_CHECK_PMT(len
!= 0)) return NULL
;
4719 _lock_file(MSVCRT_stdin
);
4720 for(cc
= _fgetc_nolock(MSVCRT_stdin
);
4721 len
!= 0 && cc
!= EOF
&& cc
!= '\n';
4722 cc
= _fgetc_nolock(MSVCRT_stdin
))
4730 _unlock_file(MSVCRT_stdin
);
4735 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4739 if ((cc
== EOF
) && (buf_start
== buf
))
4741 TRACE(":nothing read\n");
4746 TRACE("got '%s'\n", buf_start
);
4750 /*********************************************************************
4753 char * CDECL
gets(char *buf
)
4755 return gets_s(buf
, -1);
4758 /*********************************************************************
4761 wchar_t* CDECL
_getws(wchar_t* buf
)
4766 _lock_file(MSVCRT_stdin
);
4767 for (cc
= _fgetwc_nolock(MSVCRT_stdin
); cc
!= WEOF
&& cc
!= '\n';
4768 cc
= _fgetwc_nolock(MSVCRT_stdin
))
4771 *buf
++ = (wchar_t)cc
;
4773 _unlock_file(MSVCRT_stdin
);
4775 if ((cc
== WEOF
) && (ws
== buf
))
4777 TRACE(":nothing read\n");
4782 TRACE("got %s\n", debugstr_w(ws
));
4786 /*********************************************************************
4789 int CDECL
putc(int c
, FILE* file
)
4791 return fputc(c
, file
);
4794 /*********************************************************************
4795 * putchar (MSVCRT.@)
4797 int CDECL
putchar(int c
)
4799 return fputc(c
, MSVCRT_stdout
);
4802 /*********************************************************************
4805 int CDECL
puts(const char *s
)
4807 size_t len
= strlen(s
);
4810 _lock_file(MSVCRT_stdout
);
4811 if(_fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4812 _unlock_file(MSVCRT_stdout
);
4816 ret
= _fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : EOF
;
4817 _unlock_file(MSVCRT_stdout
);
4821 /*********************************************************************
4824 int CDECL
_putws(const wchar_t *s
)
4828 _lock_file(MSVCRT_stdout
);
4829 ret
= fputws(s
, MSVCRT_stdout
);
4831 ret
= _fputwc_nolock('\n', MSVCRT_stdout
);
4832 _unlock_file(MSVCRT_stdout
);
4833 return ret
>= 0 ? 0 : WEOF
;
4836 /*********************************************************************
4839 int CDECL
remove(const char *path
)
4841 TRACE("(%s)\n", path
);
4842 if (DeleteFileA(path
))
4844 TRACE(":failed (%ld)\n", GetLastError());
4845 msvcrt_set_errno(GetLastError());
4849 /*********************************************************************
4850 * _wremove (MSVCRT.@)
4852 int CDECL
_wremove(const wchar_t *path
)
4854 TRACE("(%s)\n", debugstr_w(path
));
4855 if (DeleteFileW(path
))
4857 TRACE(":failed (%ld)\n", GetLastError());
4858 msvcrt_set_errno(GetLastError());
4862 /*********************************************************************
4865 int CDECL
rename(const char *oldpath
,const char *newpath
)
4867 TRACE(":from %s to %s\n", oldpath
, newpath
);
4868 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4870 TRACE(":failed (%ld)\n", GetLastError());
4871 msvcrt_set_errno(GetLastError());
4875 /*********************************************************************
4876 * _wrename (MSVCRT.@)
4878 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
4880 TRACE(":from %s to %s\n", debugstr_w(oldpath
), debugstr_w(newpath
));
4881 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4883 TRACE(":failed (%ld)\n", GetLastError());
4884 msvcrt_set_errno(GetLastError());
4888 /*********************************************************************
4889 * setvbuf (MSVCRT.@)
4891 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
4893 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4894 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
4895 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4899 _fflush_nolock(file
);
4900 if(file
->_flag
& _IOMYBUF
)
4902 file
->_flag
&= ~(_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
);
4905 if(mode
== _IONBF
) {
4906 file
->_flag
|= _IONBF
;
4907 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4910 file
->_base
= file
->_ptr
= buf
;
4911 file
->_flag
|= MSVCRT__USERBUF
;
4912 file
->_bufsiz
= size
;
4914 file
->_base
= file
->_ptr
= malloc(size
);
4921 file
->_flag
|= _IOMYBUF
;
4922 file
->_bufsiz
= size
;
4928 /*********************************************************************
4931 void CDECL
setbuf(FILE* file
, char *buf
)
4933 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
4936 static int tmpnam_helper(char *s
, size_t size
, LONG
*tmpnam_unique
, int tmp_max
)
4942 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return EINVAL
;
4952 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4953 if (digits
+1 > size
) {
4958 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4964 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4965 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4969 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4970 if (digits
+1 > size
) {
4975 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4978 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4979 GetLastError() == ERROR_FILE_NOT_FOUND
)
4985 int CDECL
tmpnam_s(char *s
, size_t size
)
4987 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, TMP_MAX_S
);
4990 /*********************************************************************
4993 char * CDECL
tmpnam(char *s
)
4996 thread_data_t
*data
= msvcrt_get_thread_data();
4998 if(!data
->tmpnam_buffer
)
4999 data
->tmpnam_buffer
= malloc(MAX_PATH
);
5001 s
= data
->tmpnam_buffer
;
5004 return tmpnam_helper(s
, -1, &tmpnam_unique
, TMP_MAX
) ? NULL
: s
;
5007 static int wtmpnam_helper(wchar_t *s
, size_t size
, LONG
*tmpnam_unique
, int tmp_max
)
5013 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return EINVAL
;
5023 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5024 if (digits
+1 > size
) {
5029 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5035 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5036 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5040 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5041 if (digits
+1 > size
) {
5046 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5049 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5050 GetLastError() == ERROR_FILE_NOT_FOUND
)
5056 /*********************************************************************
5057 * _wtmpnam_s (MSVCRT.@)
5059 int CDECL
_wtmpnam_s(wchar_t *s
, size_t size
)
5061 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, TMP_MAX_S
);
5064 /*********************************************************************
5065 * _wtmpnam (MSVCRT.@)
5067 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
5070 thread_data_t
*data
= msvcrt_get_thread_data();
5072 if(!data
->wtmpnam_buffer
)
5073 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
5075 s
= data
->wtmpnam_buffer
;
5078 return wtmpnam_helper(s
, -1, &tmpnam_unique
, TMP_MAX
) ? NULL
: s
;
5081 /*********************************************************************
5082 * tmpfile (MSVCRT.@)
5084 FILE* CDECL
tmpfile(void)
5086 char *filename
= _tempnam(",", "t");
5091 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
5092 _S_IREAD
| _S_IWRITE
);
5093 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5095 if (msvcrt_init_fp(file
, fd
, _IORW
) == -1)
5100 else file
->_tmpfname
= _strdup(filename
);
5103 if(fd
!= -1 && !file
)
5110 /*********************************************************************
5111 * tmpfile_s (MSVCRT.@)
5113 int CDECL
tmpfile_s(FILE** file
)
5115 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
5121 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5123 return fwrite(str
, sizeof(char), len
, file
);
5126 static int puts_clbk_file_w(void *file
, int len
, const wchar_t *str
)
5132 if(!(get_ioinfo_nolock(((FILE*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5133 ret
= _fwrite_nolock(str
, sizeof(wchar_t), len
, file
);
5138 for(i
=0; i
<len
; i
++) {
5139 if(_fputwc_nolock(str
[i
], file
) == WEOF
) {
5149 static int vfprintf_helper(DWORD options
, FILE* file
, const char *format
,
5150 _locale_t locale
, va_list valist
)
5152 printf_arg args_ctx
[_ARGMAX
+1];
5156 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5157 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5159 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5160 memset(args_ctx
, 0, sizeof(args_ctx
));
5161 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5163 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5167 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5171 tmp_buf
= add_std_buffer(file
);
5172 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5173 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5174 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5175 if(tmp_buf
) remove_std_buffer(file
);
5181 static int vfwprintf_helper(DWORD options
, FILE* file
, const wchar_t *format
,
5182 _locale_t locale
, va_list valist
)
5184 printf_arg args_ctx
[_ARGMAX
+1];
5188 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5189 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5191 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5192 memset(args_ctx
, 0, sizeof(args_ctx
));
5193 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5195 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5199 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5203 tmp_buf
= add_std_buffer(file
);
5204 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5205 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5206 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5207 if(tmp_buf
) remove_std_buffer(file
);
5213 /*********************************************************************
5214 * _vfprintf_s_l (MSVCRT.@)
5216 int CDECL
_vfprintf_s_l(FILE* file
, const char *format
,
5217 _locale_t locale
, va_list valist
)
5219 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5222 /*********************************************************************
5223 * _vfwprintf_s_l (MSVCRT.@)
5225 int CDECL
_vfwprintf_s_l(FILE* file
, const wchar_t *format
,
5226 _locale_t locale
, va_list valist
)
5228 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5231 /*********************************************************************
5232 * vfprintf (MSVCRT.@)
5234 int CDECL
vfprintf(FILE* file
, const char *format
, va_list valist
)
5236 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5239 /*********************************************************************
5240 * vfprintf_s (MSVCRT.@)
5242 int CDECL
vfprintf_s(FILE* file
, const char *format
, va_list valist
)
5244 return _vfprintf_s_l(file
, format
, NULL
, valist
);
5247 /*********************************************************************
5248 * vfwprintf (MSVCRT.@)
5250 int CDECL
vfwprintf(FILE* file
, const wchar_t *format
, va_list valist
)
5252 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5255 /*********************************************************************
5256 * vfwprintf_s (MSVCRT.@)
5258 int CDECL
vfwprintf_s(FILE* file
, const wchar_t *format
, va_list valist
)
5260 return _vfwprintf_s_l(file
, format
, NULL
, valist
);
5263 #if _MSVCR_VER >= 140
5265 /*********************************************************************
5266 * __stdio_common_vfprintf (UCRTBASE.@)
5268 int CDECL
_stdio_common_vfprintf(unsigned __int64 options
, FILE *file
, const char *format
,
5269 _locale_t locale
, va_list valist
)
5271 if (options
& ~UCRTBASE_PRINTF_MASK
)
5272 FIXME("options %#I64x not handled\n", options
);
5274 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5277 /*********************************************************************
5278 * __stdio_common_vfprintf_p (UCRTBASE.@)
5280 int CDECL
__stdio_common_vfprintf_p(unsigned __int64 options
, FILE *file
, const char *format
,
5281 _locale_t locale
, va_list valist
)
5283 if (options
& ~UCRTBASE_PRINTF_MASK
)
5284 FIXME("options %#I64x not handled\n", options
);
5286 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_POSITIONAL_PARAMS
5287 | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5291 /*********************************************************************
5292 * __stdio_common_vfprintf_s (UCRTBASE.@)
5294 int CDECL
__stdio_common_vfprintf_s(unsigned __int64 options
, FILE *file
, const char *format
,
5295 _locale_t locale
, va_list valist
)
5297 if (options
& ~UCRTBASE_PRINTF_MASK
)
5298 FIXME("options %#I64x not handled\n", options
);
5300 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5301 file
, format
, locale
, valist
);
5304 /*********************************************************************
5305 * __stdio_common_vfwprintf (UCRTBASE.@)
5307 int CDECL
__stdio_common_vfwprintf(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5308 _locale_t locale
, va_list valist
)
5310 if (options
& ~UCRTBASE_PRINTF_MASK
)
5311 FIXME("options %#I64x not handled\n", options
);
5313 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5316 /*********************************************************************
5317 * __stdio_common_vfwprintf_p (UCRTBASE.@)
5319 int CDECL
__stdio_common_vfwprintf_p(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5320 _locale_t locale
, va_list valist
)
5322 if (options
& ~UCRTBASE_PRINTF_MASK
)
5323 FIXME("options %#I64x not handled\n", options
);
5325 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_POSITIONAL_PARAMS
5326 | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5330 /*********************************************************************
5331 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5333 int CDECL
__stdio_common_vfwprintf_s(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5334 _locale_t locale
, va_list valist
)
5336 if (options
& ~UCRTBASE_PRINTF_MASK
)
5337 FIXME("options %#I64x not handled\n", options
);
5339 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5340 file
, format
, locale
, valist
);
5343 #endif /* _MSVCR_VER >= 140 */
5345 /*********************************************************************
5346 * _vfprintf_l (MSVCRT.@)
5348 int CDECL
_vfprintf_l(FILE* file
, const char *format
,
5349 _locale_t locale
, va_list valist
)
5351 return vfprintf_helper(0, file
, format
, locale
, valist
);
5354 /*********************************************************************
5355 * _vfwprintf_l (MSVCRT.@)
5357 int CDECL
_vfwprintf_l(FILE* file
, const wchar_t *format
,
5358 _locale_t locale
, va_list valist
)
5360 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5363 /*********************************************************************
5364 * _vfprintf_p_l (MSVCRT.@)
5366 int CDECL
_vfprintf_p_l(FILE* file
, const char *format
,
5367 _locale_t locale
, va_list valist
)
5369 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5370 file
, format
, locale
, valist
);
5373 /*********************************************************************
5374 * _vfprintf_p (MSVCRT.@)
5376 int CDECL
_vfprintf_p(FILE* file
, const char *format
, va_list valist
)
5378 return _vfprintf_p_l(file
, format
, NULL
, valist
);
5381 /*********************************************************************
5382 * _vfwprintf_p_l (MSVCRT.@)
5384 int CDECL
_vfwprintf_p_l(FILE* file
, const wchar_t *format
,
5385 _locale_t locale
, va_list valist
)
5387 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5388 file
, format
, locale
, valist
);
5391 /*********************************************************************
5392 * _vfwprintf_p (MSVCRT.@)
5394 int CDECL
_vfwprintf_p(FILE* file
, const wchar_t *format
, va_list valist
)
5396 return _vfwprintf_p_l(file
, format
, NULL
, valist
);
5399 /*********************************************************************
5400 * vprintf (MSVCRT.@)
5402 int CDECL
vprintf(const char *format
, va_list valist
)
5404 return vfprintf(MSVCRT_stdout
,format
,valist
);
5407 /*********************************************************************
5408 * vprintf_s (MSVCRT.@)
5410 int CDECL
vprintf_s(const char *format
, va_list valist
)
5412 return vfprintf_s(MSVCRT_stdout
,format
,valist
);
5415 /*********************************************************************
5416 * vwprintf (MSVCRT.@)
5418 int CDECL
vwprintf(const wchar_t *format
, va_list valist
)
5420 return vfwprintf(MSVCRT_stdout
,format
,valist
);
5423 /*********************************************************************
5424 * vwprintf_s (MSVCRT.@)
5426 int CDECL
vwprintf_s(const wchar_t *format
, va_list valist
)
5428 return vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5431 /*********************************************************************
5432 * fprintf (MSVCRT.@)
5434 int WINAPIV
fprintf(FILE* file
, const char *format
, ...)
5438 va_start(valist
, format
);
5439 res
= vfprintf(file
, format
, valist
);
5444 /*********************************************************************
5445 * fprintf_s (MSVCRT.@)
5447 int WINAPIV
fprintf_s(FILE* file
, const char *format
, ...)
5451 va_start(valist
, format
);
5452 res
= vfprintf_s(file
, format
, valist
);
5457 /*********************************************************************
5458 * fwprintf (MSVCRT.@)
5460 int WINAPIV
fwprintf(FILE* file
, const wchar_t *format
, ...)
5464 va_start(valist
, format
);
5465 res
= vfwprintf(file
, format
, valist
);
5470 /*********************************************************************
5471 * fwprintf_s (MSVCRT.@)
5473 int WINAPIV
fwprintf_s(FILE* file
, const wchar_t *format
, ...)
5477 va_start(valist
, format
);
5478 res
= vfwprintf_s(file
, format
, valist
);
5483 /*********************************************************************
5484 * _fwprintf_l (MSVCRT.@)
5486 int WINAPIV
_fwprintf_l(FILE* file
, const wchar_t *format
, _locale_t locale
, ...)
5490 va_start(valist
, locale
);
5491 res
= _vfwprintf_l(file
, format
, locale
, valist
);
5496 /*********************************************************************
5499 int WINAPIV
printf(const char *format
, ...)
5503 va_start(valist
, format
);
5504 res
= vfprintf(MSVCRT_stdout
, format
, valist
);
5509 /*********************************************************************
5510 * printf_s (MSVCRT.@)
5512 int WINAPIV
printf_s(const char *format
, ...)
5516 va_start(valist
, format
);
5517 res
= vprintf_s(format
, valist
);
5522 /*********************************************************************
5525 int CDECL
ungetc(int c
, FILE * file
)
5529 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
5532 ret
= _ungetc_nolock(c
, file
);
5538 /*********************************************************************
5539 * _ungetc_nolock (MSVCRT.@)
5541 int CDECL
_ungetc_nolock(int c
, FILE * file
)
5543 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
5545 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
5546 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
5549 if((!(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
))
5550 && msvcrt_alloc_buffer(file
))
5551 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5554 if(file
->_ptr
>file
->_base
) {
5556 if(file
->_flag
& _IOSTRG
) {
5557 if(*file
->_ptr
!= c
) {
5565 file
->_flag
&= ~(_IOERR
| _IOEOF
);
5566 file
->_flag
|= _IOREAD
;
5573 /*********************************************************************
5574 * ungetwc (MSVCRT.@)
5576 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
5580 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5583 ret
= _ungetwc_nolock(wc
, file
);
5589 /*********************************************************************
5590 * _ungetwc_nolock (MSVCRT.@)
5592 wint_t CDECL
_ungetwc_nolock(wint_t wc
, FILE * file
)
5596 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5600 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5601 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5602 unsigned char * pp
= (unsigned char *)&mwc
;
5605 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
5606 if(pp
[i
] != _ungetc_nolock(pp
[i
],file
))
5610 char mbs
[MB_LEN_MAX
];
5613 len
= wctomb(mbs
, mwc
);
5617 for(len
--; len
>=0; len
--) {
5618 if(mbs
[len
] != _ungetc_nolock(mbs
[len
], file
))
5626 /*********************************************************************
5627 * wprintf (MSVCRT.@)
5629 int WINAPIV
wprintf(const wchar_t *format
, ...)
5633 va_start(valist
, format
);
5634 res
= vwprintf(format
, valist
);
5639 /*********************************************************************
5640 * wprintf_s (MSVCRT.@)
5642 int WINAPIV
wprintf_s(const wchar_t *format
, ...)
5646 va_start(valist
, format
);
5647 res
= vwprintf_s(format
, valist
);
5652 /*********************************************************************
5653 * _getmaxstdio (MSVCRT.@)
5655 int CDECL
_getmaxstdio(void)
5657 return MSVCRT_max_streams
;
5660 /*********************************************************************
5661 * _setmaxstdio (MSVCRT.@)
5663 int CDECL
_setmaxstdio(int newmax
)
5665 TRACE("%d\n", newmax
);
5667 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5670 MSVCRT_max_streams
= newmax
;
5671 return MSVCRT_max_streams
;
5674 #if _MSVCR_VER >= 140
5675 /*********************************************************************
5676 * _get_stream_buffer_pointers (UCRTBASE.@)
5678 int CDECL
_get_stream_buffer_pointers(FILE *file
, char*** base
,
5679 char*** ptr
, int** count
)
5682 *base
= &file
->_base
;
5686 *count
= &file
->_cnt
;