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 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
89 static char utf16_bom
[2] = { 0xff, 0xfe };
91 #define MSVCRT_INTERNAL_BUFSIZ 4096
100 #if _MSVCR_VER >= 140
102 #define MSVCRT_MAX_FILES 8192
103 #define MSVCRT_FD_BLOCK_SIZE 64
106 CRITICAL_SECTION crit
;
109 unsigned char wxflag
;
112 unsigned int unicode
: 1;
113 unsigned int utf8translations
: 1;
114 unsigned int dbcsBufferUsed
: 1;
115 char dbcsBuffer
[MB_LEN_MAX
];
118 /*********************************************************************
119 * __badioinfo (MSVCRT.@)
121 ioinfo MSVCRT___badioinfo
= { {0}, INVALID_HANDLE_VALUE
, 0, WX_TEXT
};
124 #define MSVCRT_MAX_FILES 2048
125 #define MSVCRT_FD_BLOCK_SIZE 32
129 unsigned char wxflag
;
132 CRITICAL_SECTION crit
;
138 BOOL utf8translations
;
144 /*********************************************************************
145 * __badioinfo (MSVCRT.@)
147 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
150 /*********************************************************************
151 * __pioinfo (MSVCRT.@)
152 * array of pointers to ioinfo arrays [32]
154 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
158 #if _MSVCR_VER >= 140
159 static inline BOOL
ioinfo_is_crit_init(ioinfo
*info
)
164 static inline void ioinfo_set_crit_init(ioinfo
*info
)
168 static inline BOOL
ioinfo_is_crit_init(ioinfo
*info
)
170 return info
->exflag
& 1;
173 static inline void ioinfo_set_crit_init(ioinfo
*info
)
179 static inline enum textmode
ioinfo_get_textmode(ioinfo
*info
)
181 return info
->textmode
;
184 static inline void ioinfo_set_textmode(ioinfo
*info
, enum textmode mode
)
186 info
->textmode
= mode
;
189 static inline void ioinfo_set_unicode(ioinfo
*info
, BOOL unicode
)
191 info
->unicode
= !!unicode
;
196 #define EF_UTF16 0x02
197 #define EF_CRIT_INIT 0x04
198 #define EF_UNK_UNICODE 0x08
200 static inline BOOL
ioinfo_is_crit_init(ioinfo
*info
)
202 return info
->exflag
& EF_CRIT_INIT
;
205 static inline void ioinfo_set_crit_init(ioinfo
*info
)
207 info
->exflag
|= EF_CRIT_INIT
;
210 static inline enum textmode
ioinfo_get_textmode(ioinfo
*info
)
212 if (info
->exflag
& EF_UTF8
)
213 return TEXTMODE_UTF8
;
214 if (info
->exflag
& EF_UTF16
)
215 return TEXTMODE_UTF16LE
;
216 return TEXTMODE_ANSI
;
219 static inline void ioinfo_set_textmode(ioinfo
*info
, enum textmode mode
)
221 info
->exflag
&= EF_CRIT_INIT
| EF_UNK_UNICODE
;
227 info
->exflag
|= EF_UTF8
;
229 case TEXTMODE_UTF16LE
:
230 info
->exflag
|= EF_UTF16
;
235 static inline void ioinfo_set_unicode(ioinfo
*info
, BOOL unicode
)
238 info
->exflag
|= EF_UNK_UNICODE
;
240 info
->exflag
&= ~EF_UNK_UNICODE
;
246 CRITICAL_SECTION crit
;
249 #if _MSVCR_VER >= 140
250 file_crit MSVCRT__iob
[_IOB_ENTRIES
] = { 0 };
252 static FILE* iob_get_file(int i
)
254 return &MSVCRT__iob
[i
].file
;
257 static CRITICAL_SECTION
* file_get_cs(FILE *f
)
259 return &((file_crit
*)f
)->crit
;
262 FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
264 static FILE* iob_get_file(int i
)
266 return &MSVCRT__iob
[i
];
269 static CRITICAL_SECTION
* file_get_cs(FILE *f
)
271 if (f
< iob_get_file(0) || f
>= iob_get_file(_IOB_ENTRIES
))
272 return &((file_crit
*)f
)->crit
;
277 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
278 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
280 /* INTERNAL: process umask */
281 static int MSVCRT_umask
= 0;
283 /* INTERNAL: static data for tmpnam and _wtmpname functions */
284 static LONG tmpnam_unique
;
285 static LONG tmpnam_s_unique
;
287 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
288 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
289 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
290 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
292 #define TOUL(x) (ULONGLONG)(x)
293 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
294 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
295 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
296 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
298 /* This critical section protects the MSVCRT_fstreams table
299 * and MSVCRT_stream_idx from race conditions. It also
300 * protects fd critical sections creation code.
302 static CRITICAL_SECTION MSVCRT_file_cs
;
303 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
305 0, 0, &MSVCRT_file_cs
,
306 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
307 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
309 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
310 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
311 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
313 static void msvcrt_stat64_to_stat(const struct _stat64
*buf64
, struct _stat
*buf
)
315 buf
->st_dev
= buf64
->st_dev
;
316 buf
->st_ino
= buf64
->st_ino
;
317 buf
->st_mode
= buf64
->st_mode
;
318 buf
->st_nlink
= buf64
->st_nlink
;
319 buf
->st_uid
= buf64
->st_uid
;
320 buf
->st_gid
= buf64
->st_gid
;
321 buf
->st_rdev
= buf64
->st_rdev
;
322 buf
->st_size
= buf64
->st_size
;
323 buf
->st_atime
= buf64
->st_atime
;
324 buf
->st_mtime
= buf64
->st_mtime
;
325 buf
->st_ctime
= buf64
->st_ctime
;
328 static void msvcrt_stat64_to_stati64(const struct _stat64
*buf64
, struct _stati64
*buf
)
330 buf
->st_dev
= buf64
->st_dev
;
331 buf
->st_ino
= buf64
->st_ino
;
332 buf
->st_mode
= buf64
->st_mode
;
333 buf
->st_nlink
= buf64
->st_nlink
;
334 buf
->st_uid
= buf64
->st_uid
;
335 buf
->st_gid
= buf64
->st_gid
;
336 buf
->st_rdev
= buf64
->st_rdev
;
337 buf
->st_size
= buf64
->st_size
;
338 buf
->st_atime
= buf64
->st_atime
;
339 buf
->st_mtime
= buf64
->st_mtime
;
340 buf
->st_ctime
= buf64
->st_ctime
;
343 static void msvcrt_stat64_to_stat32(const struct _stat64
*buf64
, struct _stat32
*buf
)
345 buf
->st_dev
= buf64
->st_dev
;
346 buf
->st_ino
= buf64
->st_ino
;
347 buf
->st_mode
= buf64
->st_mode
;
348 buf
->st_nlink
= buf64
->st_nlink
;
349 buf
->st_uid
= buf64
->st_uid
;
350 buf
->st_gid
= buf64
->st_gid
;
351 buf
->st_rdev
= buf64
->st_rdev
;
352 buf
->st_size
= buf64
->st_size
;
353 buf
->st_atime
= buf64
->st_atime
;
354 buf
->st_mtime
= buf64
->st_mtime
;
355 buf
->st_ctime
= buf64
->st_ctime
;
358 static void msvcrt_stat64_to_stat64i32(const struct _stat64
*buf64
, struct _stat64i32
*buf
)
360 buf
->st_dev
= buf64
->st_dev
;
361 buf
->st_ino
= buf64
->st_ino
;
362 buf
->st_mode
= buf64
->st_mode
;
363 buf
->st_nlink
= buf64
->st_nlink
;
364 buf
->st_uid
= buf64
->st_uid
;
365 buf
->st_gid
= buf64
->st_gid
;
366 buf
->st_rdev
= buf64
->st_rdev
;
367 buf
->st_size
= buf64
->st_size
;
368 buf
->st_atime
= buf64
->st_atime
;
369 buf
->st_mtime
= buf64
->st_mtime
;
370 buf
->st_ctime
= buf64
->st_ctime
;
373 static void msvcrt_stat64_to_stat32i64(const struct _stat64
*buf64
, struct _stat32i64
*buf
)
375 buf
->st_dev
= buf64
->st_dev
;
376 buf
->st_ino
= buf64
->st_ino
;
377 buf
->st_mode
= buf64
->st_mode
;
378 buf
->st_nlink
= buf64
->st_nlink
;
379 buf
->st_uid
= buf64
->st_uid
;
380 buf
->st_gid
= buf64
->st_gid
;
381 buf
->st_rdev
= buf64
->st_rdev
;
382 buf
->st_size
= buf64
->st_size
;
383 buf
->st_atime
= buf64
->st_atime
;
384 buf
->st_mtime
= buf64
->st_mtime
;
385 buf
->st_ctime
= buf64
->st_ctime
;
388 static void time_to_filetime( __time64_t time
, FILETIME
*ft
)
390 /* 1601 to 1970 is 369 years plus 89 leap days */
391 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
393 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
394 ft
->dwHighDateTime
= ticks
>> 32;
395 ft
->dwLowDateTime
= ticks
;
398 static inline ioinfo
* get_ioinfo_nolock(int fd
)
401 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
402 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
404 return &MSVCRT___badioinfo
;
406 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
409 static inline void init_ioinfo_cs(ioinfo
*info
)
411 if(!ioinfo_is_crit_init(info
)) {
413 if(!ioinfo_is_crit_init(info
)) {
414 InitializeCriticalSection(&info
->crit
);
415 ioinfo_set_crit_init(info
);
421 static inline ioinfo
* get_ioinfo(int fd
)
423 ioinfo
*ret
= get_ioinfo_nolock(fd
);
424 if(ret
== &MSVCRT___badioinfo
)
427 EnterCriticalSection(&ret
->crit
);
431 static inline BOOL
alloc_pioinfo_block(int fd
)
436 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
442 block
= calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
445 WARN(":out of memory!\n");
449 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
451 block
[i
].handle
= INVALID_HANDLE_VALUE
;
452 if (ioinfo_is_crit_init(&block
[i
]))
454 /* Initialize crit section on block allocation for _MSVC_VER >= 140,
455 * ioinfo_is_crit_init() is always TRUE. */
456 InitializeCriticalSection(&block
[i
].crit
);
459 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
461 if (ioinfo_is_crit_init(&block
[0]))
463 for(i
= 0; i
< MSVCRT_FD_BLOCK_SIZE
; ++i
)
464 DeleteCriticalSection(&block
[i
].crit
);
471 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
475 ret
= get_ioinfo(fd
);
476 if(ret
!= &MSVCRT___badioinfo
)
479 if(!alloc_pioinfo_block(fd
))
480 return &MSVCRT___badioinfo
;
482 return get_ioinfo(fd
);
485 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
490 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
492 ioinfo
*info
= get_ioinfo_nolock(i
);
494 if(info
== &MSVCRT___badioinfo
)
496 if(!alloc_pioinfo_block(i
))
497 return &MSVCRT___badioinfo
;
498 info
= get_ioinfo_nolock(i
);
501 init_ioinfo_cs(info
);
502 if(TryEnterCriticalSection(&info
->crit
))
504 if(info
->handle
== INVALID_HANDLE_VALUE
)
509 LeaveCriticalSection(&info
->crit
);
513 WARN(":files exhausted!\n");
515 return &MSVCRT___badioinfo
;
518 static inline void release_ioinfo(ioinfo
*info
)
520 if(info
!=&MSVCRT___badioinfo
&& ioinfo_is_crit_init(info
))
521 LeaveCriticalSection(&info
->crit
);
524 static inline FILE* msvcrt_get_file(int i
)
528 if(i
>= MSVCRT_max_streams
)
532 return iob_get_file(i
);
534 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
536 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
537 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
538 ERR("out of memory\n");
543 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
545 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
550 /* INTERNAL: free a file entry fd */
551 static void msvcrt_free_fd(int fd
)
553 ioinfo
*fdinfo
= get_ioinfo(fd
);
555 if(fdinfo
!= &MSVCRT___badioinfo
)
557 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
560 TRACE(":fd (%d) freed\n",fd
);
567 SetStdHandle(STD_INPUT_HANDLE
, 0);
570 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
573 SetStdHandle(STD_ERROR_HANDLE
, 0);
577 release_ioinfo(fdinfo
);
580 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
582 fdinfo
->handle
= hand
;
583 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
584 fdinfo
->lookahead
[0] = '\n';
585 fdinfo
->lookahead
[1] = '\n';
586 fdinfo
->lookahead
[2] = '\n';
587 ioinfo_set_unicode(fdinfo
, FALSE
);
588 ioinfo_set_textmode(fdinfo
, TEXTMODE_ANSI
);
590 if (hand
!= MSVCRT_NO_CONSOLE
)
592 switch (fdinfo
-MSVCRT___pioinfo
[0])
594 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
595 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
596 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
601 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
602 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
605 ioinfo
*info
= get_ioinfo_alloc(&fd
);
607 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
609 if(info
== &MSVCRT___badioinfo
)
612 msvcrt_set_fd(info
, hand
, flag
);
613 release_ioinfo(info
);
617 /* INTERNAL: Allocate a FILE* for an fd slot */
618 /* caller must hold the files lock */
619 static FILE* msvcrt_alloc_fp(void)
624 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
626 file
= msvcrt_get_file(i
);
630 if (file
->_flag
== 0)
632 if (i
== MSVCRT_stream_idx
)
634 CRITICAL_SECTION
*cs
= file_get_cs(file
);
637 InitializeCriticalSectionEx(cs
, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO
);
638 cs
->DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
649 /* INTERNAL: initialize a FILE* from an open fd */
650 static int msvcrt_init_fp(FILE* file
, int fd
, unsigned stream_flags
)
652 TRACE(":fd (%d) allocating FILE*\n",fd
);
653 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
655 WARN(":invalid fd %d\n",fd
);
660 file
->_ptr
= file
->_base
= NULL
;
663 file
->_flag
= stream_flags
;
664 file
->_tmpfname
= NULL
;
666 TRACE(":got FILE* (%p)\n",file
);
670 /* INTERNAL: Create an inheritance data block (for spawned process)
671 * The inheritance block is made of:
672 * 00 int nb of file descriptor (NBFD)
673 * 04 char file flags (wxflag): repeated for each fd
674 * 4+NBFD HANDLE file handle: repeated for each fd
676 BOOL
msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
683 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
684 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
688 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
689 *block
= calloc(1, *size
);
695 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
696 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
698 *(unsigned*)*block
= last_fd
;
699 for (fd
= 0; fd
< last_fd
; fd
++)
701 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
702 fdinfo
= get_ioinfo_nolock(fd
);
703 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
705 *wxflag_ptr
= fdinfo
->wxflag
;
706 *handle_ptr
= fdinfo
->handle
;
711 *handle_ptr
= INVALID_HANDLE_VALUE
;
713 wxflag_ptr
++; handle_ptr
++;
718 /* INTERNAL: Set up all file descriptors,
719 * as well as default streams (stdin, stderr and stdout)
721 void msvcrt_init_io(void)
727 GetStartupInfoA(&si
);
728 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
734 count
= *(unsigned*)si
.lpReserved2
;
735 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
736 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
738 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
739 count
= min(count
, MSVCRT_MAX_FILES
);
740 for (i
= 0; i
< count
; i
++)
742 if ((*wxflag_ptr
& WX_OPEN
) && GetFileType(*handle_ptr
) != FILE_TYPE_UNKNOWN
)
744 fdinfo
= get_ioinfo_alloc_fd(i
);
745 if(fdinfo
!= &MSVCRT___badioinfo
)
746 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
747 release_ioinfo(fdinfo
);
750 wxflag_ptr
++; handle_ptr
++;
754 fdinfo
= get_ioinfo_alloc_fd(STDIN_FILENO
);
755 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
756 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
757 DWORD flags
= WX_OPEN
| WX_TEXT
;
758 DWORD type
= GetFileType(h
);
760 if (type
== FILE_TYPE_UNKNOWN
) {
761 h
= MSVCRT_NO_CONSOLE
;
763 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
765 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
769 msvcrt_set_fd(fdinfo
, h
, flags
);
771 release_ioinfo(fdinfo
);
773 fdinfo
= get_ioinfo_alloc_fd(STDOUT_FILENO
);
774 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
775 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
776 DWORD flags
= WX_OPEN
| WX_TEXT
;
777 DWORD type
= GetFileType(h
);
779 if (type
== FILE_TYPE_UNKNOWN
) {
780 h
= MSVCRT_NO_CONSOLE
;
782 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
784 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
788 msvcrt_set_fd(fdinfo
, h
, flags
);
790 release_ioinfo(fdinfo
);
792 fdinfo
= get_ioinfo_alloc_fd(STDERR_FILENO
);
793 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
794 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
795 DWORD flags
= WX_OPEN
| WX_TEXT
;
796 DWORD type
= GetFileType(h
);
798 if (type
== FILE_TYPE_UNKNOWN
) {
799 h
= MSVCRT_NO_CONSOLE
;
801 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
803 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
807 msvcrt_set_fd(fdinfo
, h
, flags
);
809 release_ioinfo(fdinfo
);
811 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO
)->handle
,
812 get_ioinfo_nolock(STDOUT_FILENO
)->handle
,
813 get_ioinfo_nolock(STDERR_FILENO
)->handle
);
815 for (i
= 0; i
< 3; i
++)
817 FILE *f
= iob_get_file(i
);
818 CRITICAL_SECTION
*cs
= file_get_cs(f
);
820 /* FILE structs for stdin/out/err are static and never deleted */
821 f
->_file
= get_ioinfo_nolock(i
)->handle
== MSVCRT_NO_CONSOLE
?
822 MSVCRT_NO_CONSOLE_FD
: i
;
824 f
->_flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
828 InitializeCriticalSectionEx(cs
, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO
);
829 cs
->DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
832 MSVCRT_stream_idx
= 3;
835 /* INTERNAL: Flush stdio file buffer */
836 static int msvcrt_flush_buffer(FILE* file
)
840 if((file
->_flag
& (_IOREAD
|_IOWRT
)) == _IOWRT
&&
841 file
->_flag
& (_IOMYBUF
|MSVCRT__USERBUF
)) {
842 int cnt
=file
->_ptr
-file
->_base
;
843 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
844 file
->_flag
|= _IOERR
;
846 } else if(file
->_flag
& _IORW
) {
847 file
->_flag
&= ~_IOWRT
;
851 file
->_ptr
=file
->_base
;
856 /*********************************************************************
859 int CDECL
_isatty(int fd
)
861 TRACE(":fd (%d)\n",fd
);
863 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
866 /* INTERNAL: Allocate stdio file buffer */
867 static BOOL
msvcrt_alloc_buffer(FILE* file
)
869 #if _MSVCR_VER >= 140
870 if((file
->_file
==STDOUT_FILENO
&& _isatty(file
->_file
))
871 || file
->_file
== STDERR_FILENO
)
874 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
875 && _isatty(file
->_file
))
879 file
->_base
= calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
881 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
882 file
->_flag
|= _IOMYBUF
;
884 file
->_base
= (char*)(&file
->_charbuf
);
886 file
->_flag
|= MSVCRT__NOBUF
;
888 file
->_ptr
= file
->_base
;
893 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
894 static BOOL
add_std_buffer(FILE *file
)
896 static char buffers
[2][BUFSIZ
];
898 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
899 || (file
->_flag
& (MSVCRT__NOBUF
| _IOMYBUF
| MSVCRT__USERBUF
))
900 || !_isatty(file
->_file
))
903 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
904 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
905 file
->_flag
|= MSVCRT__USERBUF
;
909 /* INTERNAL: Removes temporary buffer from stdout or stderr */
910 /* Only call this function when add_std_buffer returned TRUE */
911 static void remove_std_buffer(FILE *file
)
913 msvcrt_flush_buffer(file
);
914 file
->_ptr
= file
->_base
= NULL
;
915 file
->_bufsiz
= file
->_cnt
= 0;
916 file
->_flag
&= ~MSVCRT__USERBUF
;
919 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
920 static int msvcrt_int_to_base32(int num
, char *str
)
935 *p
= (num
& 31) + '0';
937 *p
+= ('a' - '0' - 10);
944 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
945 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
960 *p
= (num
& 31) + '0';
962 *p
+= ('a' - '0' - 10);
969 /*********************************************************************
970 * __iob_func (MSVCRT.@)
973 FILE * CDECL
__iob_func(void)
975 return iob_get_file(0);
978 #if _MSVCR_VER >= 140
979 /*********************************************************************
980 * __acrt_iob_func(UCRTBASE.@)
982 FILE * CDECL
__acrt_iob_func(unsigned idx
)
984 return iob_get_file(idx
);
988 /*********************************************************************
991 int CDECL
_access(const char *filename
, int mode
)
993 DWORD attr
= GetFileAttributesA(filename
);
995 TRACE("(%s,%d) %ld\n", filename
, mode
, attr
);
997 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
999 msvcrt_set_errno(GetLastError());
1002 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
1004 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
1010 /*********************************************************************
1011 * _access_s (MSVCRT.@)
1013 int CDECL
_access_s(const char *filename
, int mode
)
1015 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *_errno();
1016 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *_errno();
1018 if (_access(filename
, mode
) == -1)
1023 /*********************************************************************
1024 * _waccess (MSVCRT.@)
1026 int CDECL
_waccess(const wchar_t *filename
, int mode
)
1028 DWORD attr
= GetFileAttributesW(filename
);
1030 TRACE("(%s,%d) %ld\n", debugstr_w(filename
), mode
, attr
);
1032 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
1034 msvcrt_set_errno(GetLastError());
1037 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
1039 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
1045 /*********************************************************************
1046 * _waccess_s (MSVCRT.@)
1048 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
1050 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *_errno();
1051 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *_errno();
1053 if (_waccess(filename
, mode
) == -1)
1058 /*********************************************************************
1061 int CDECL
_chmod(const char *path
, int flags
)
1063 DWORD oldFlags
= GetFileAttributesA(path
);
1065 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
1067 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
1068 oldFlags
| FILE_ATTRIBUTE_READONLY
;
1070 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
1073 msvcrt_set_errno(GetLastError());
1077 /*********************************************************************
1078 * _wchmod (MSVCRT.@)
1080 int CDECL
_wchmod(const wchar_t *path
, int flags
)
1082 DWORD oldFlags
= GetFileAttributesW(path
);
1084 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
1086 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
1087 oldFlags
| FILE_ATTRIBUTE_READONLY
;
1089 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
1092 msvcrt_set_errno(GetLastError());
1096 /*********************************************************************
1097 * _unlink (MSVCRT.@)
1099 int CDECL
_unlink(const char *path
)
1101 TRACE("%s\n", debugstr_a(path
));
1102 if(DeleteFileA(path
))
1104 TRACE("failed (%ld)\n", GetLastError());
1105 msvcrt_set_errno(GetLastError());
1109 /*********************************************************************
1110 * _wunlink (MSVCRT.@)
1112 int CDECL
_wunlink(const wchar_t *path
)
1114 TRACE("(%s)\n", debugstr_w(path
));
1115 if(DeleteFileW(path
))
1117 TRACE("failed (%ld)\n", GetLastError());
1118 msvcrt_set_errno(GetLastError());
1122 /*********************************************************************
1123 * _commit (MSVCRT.@)
1125 int CDECL
_commit(int fd
)
1127 ioinfo
*info
= get_ioinfo(fd
);
1130 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1132 if (info
->handle
== INVALID_HANDLE_VALUE
)
1134 else if (!FlushFileBuffers(info
->handle
))
1136 if (GetLastError() == ERROR_INVALID_HANDLE
)
1138 /* FlushFileBuffers fails for console handles
1139 * so we ignore this error.
1145 TRACE(":failed-last error (%ld)\n", GetLastError());
1146 msvcrt_set_errno(GetLastError());
1156 release_ioinfo(info
);
1160 /* INTERNAL: Flush all stream buffer */
1161 static int msvcrt_flush_all_buffers(int mask
)
1163 int i
, num_flushed
= 0;
1167 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
1168 file
= msvcrt_get_file(i
);
1172 if(file
->_flag
& mask
) {
1180 TRACE(":flushed (%d) handles\n",num_flushed
);
1184 /*********************************************************************
1185 * _flushall (MSVCRT.@)
1187 int CDECL
_flushall(void)
1189 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
1192 /*********************************************************************
1195 int CDECL
fflush(FILE* file
)
1200 msvcrt_flush_all_buffers(_IOWRT
);
1204 ret
= _fflush_nolock(file
);
1211 /*********************************************************************
1212 * _fflush_nolock (MSVCRT.@)
1214 int CDECL
_fflush_nolock(FILE* file
)
1219 msvcrt_flush_all_buffers(_IOWRT
);
1223 res
= msvcrt_flush_buffer(file
);
1224 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1225 res
= _commit(file
->_file
) ? EOF
: 0;
1229 /*********************************************************************
1232 int CDECL
_close(int fd
)
1234 ioinfo
*info
= get_ioinfo(fd
);
1237 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1239 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1242 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, EBADF
)) {
1244 } else if (fd
== STDOUT_FILENO
&&
1245 info
->handle
== get_ioinfo_nolock(STDERR_FILENO
)->handle
) {
1248 } else if (fd
== STDERR_FILENO
&&
1249 info
->handle
== get_ioinfo_nolock(STDOUT_FILENO
)->handle
) {
1253 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1256 WARN(":failed-last error (%ld)\n", GetLastError());
1257 msvcrt_set_errno(GetLastError());
1260 release_ioinfo(info
);
1264 /*********************************************************************
1267 * MSDN isn't clear on this point, but the remarks for _pipe
1268 * indicate file descriptors duplicated with _dup and _dup2 are always
1271 int CDECL
_dup2(int od
, int nd
)
1273 ioinfo
*info_od
, *info_nd
;
1276 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1280 info_od
= get_ioinfo(od
);
1281 info_nd
= get_ioinfo_alloc_fd(nd
);
1285 info_nd
= get_ioinfo_alloc_fd(nd
);
1286 info_od
= get_ioinfo(od
);
1289 if (info_nd
== &MSVCRT___badioinfo
)
1294 else if (info_od
->wxflag
& WX_OPEN
)
1298 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1299 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1301 int wxflag
= info_od
->wxflag
& ~WX_DONTINHERIT
;
1303 if (info_nd
->wxflag
& WX_OPEN
)
1306 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1307 /* _dup2 returns 0, not nd, on success */
1313 msvcrt_set_errno(GetLastError());
1322 release_ioinfo(info_od
);
1323 release_ioinfo(info_nd
);
1327 /*********************************************************************
1330 int CDECL
_dup(int od
)
1333 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1335 if (_dup2(od
, fd
) == 0)
1339 release_ioinfo(info
);
1343 /*********************************************************************
1346 int CDECL
_eof(int fd
)
1348 ioinfo
*info
= get_ioinfo(fd
);
1349 DWORD curpos
,endpos
;
1350 LONG hcurpos
,hendpos
;
1352 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1354 if (info
->handle
== INVALID_HANDLE_VALUE
)
1356 release_ioinfo(info
);
1360 if (info
->wxflag
& WX_ATEOF
)
1362 release_ioinfo(info
);
1366 /* Otherwise we do it the hard way */
1367 hcurpos
= hendpos
= 0;
1368 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1369 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1371 if (curpos
== endpos
&& hcurpos
== hendpos
)
1373 /* FIXME: shouldn't WX_ATEOF be set here? */
1374 release_ioinfo(info
);
1378 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1379 release_ioinfo(info
);
1383 /*********************************************************************
1384 * _fcloseall (MSVCRT.@)
1386 int CDECL
_fcloseall(void)
1388 int num_closed
= 0, i
;
1392 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1393 file
= msvcrt_get_file(i
);
1395 if (file
->_flag
&& !fclose(file
))
1400 TRACE(":closed (%d) handles\n",num_closed
);
1404 /* free everything on process exit */
1405 void msvcrt_free_io(void)
1413 for(i
=0; i
<ARRAY_SIZE(MSVCRT___pioinfo
); i
++)
1415 if(!MSVCRT___pioinfo
[i
])
1418 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1420 if(ioinfo_is_crit_init(&MSVCRT___pioinfo
[i
][j
]))
1421 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1423 free(MSVCRT___pioinfo
[i
]);
1426 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1428 FILE *file
= msvcrt_get_file(j
);
1429 CRITICAL_SECTION
*cs
= file_get_cs(file
);
1433 cs
->DebugInfo
->Spare
[0] = 0;
1434 DeleteCriticalSection(cs
);
1438 for(i
=0; i
<ARRAY_SIZE(MSVCRT_fstream
); i
++)
1439 free(MSVCRT_fstream
[i
]);
1442 /*********************************************************************
1443 * _lseeki64 (MSVCRT.@)
1445 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1447 ioinfo
*info
= get_ioinfo(fd
);
1450 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1452 if (info
->handle
== INVALID_HANDLE_VALUE
)
1455 release_ioinfo(info
);
1459 if (whence
< 0 || whence
> 2)
1461 release_ioinfo(info
);
1466 TRACE(":fd (%d) to %#I64x pos %s\n",
1467 fd
, offset
, (whence
== SEEK_SET
) ? "SEEK_SET" :
1468 (whence
== SEEK_CUR
) ? "SEEK_CUR" :
1469 (whence
== SEEK_END
) ? "SEEK_END" : "UNKNOWN");
1471 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1472 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1473 ofs
.QuadPart
= offset
;
1474 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1475 GetLastError() == ERROR_SUCCESS
)
1477 info
->wxflag
&= ~WX_ATEOF
;
1478 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1480 release_ioinfo(info
);
1481 return ofs
.QuadPart
;
1483 release_ioinfo(info
);
1484 TRACE(":error-last error (%ld)\n", GetLastError());
1485 msvcrt_set_errno(GetLastError());
1489 /*********************************************************************
1492 __msvcrt_long CDECL
_lseek(int fd
, __msvcrt_long offset
, int whence
)
1494 return _lseeki64(fd
, offset
, whence
);
1497 /*********************************************************************
1498 * _lock_file (MSVCRT.@)
1500 void CDECL
_lock_file(FILE *file
)
1502 CRITICAL_SECTION
*cs
= file_get_cs(file
);
1504 _lock(_STREAM_LOCKS
+ (file
- iob_get_file(0)));
1506 EnterCriticalSection(cs
);
1509 /*********************************************************************
1510 * _unlock_file (MSVCRT.@)
1512 void CDECL
_unlock_file(FILE *file
)
1514 CRITICAL_SECTION
*cs
= file_get_cs(file
);
1516 _unlock(_STREAM_LOCKS
+ (file
- iob_get_file(0)));
1518 LeaveCriticalSection(cs
);
1521 /*********************************************************************
1522 * _locking (MSVCRT.@)
1524 * This is untested; the underlying LockFile doesn't work yet.
1526 int CDECL
_locking(int fd
, int mode
, __msvcrt_long nbytes
)
1528 ioinfo
*info
= get_ioinfo(fd
);
1532 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1533 if (info
->handle
== INVALID_HANDLE_VALUE
)
1535 release_ioinfo(info
);
1539 if (mode
< 0 || mode
> 4)
1541 release_ioinfo(info
);
1546 TRACE(":fd (%d) by %#lx mode %s\n",
1547 fd
, nbytes
, (mode
== _LK_UNLCK
) ? "_LK_UNLCK" :
1548 (mode
== _LK_LOCK
) ? "_LK_LOCK" :
1549 (mode
== _LK_NBLCK
) ? "_LK_NBLCK" :
1550 (mode
== _LK_RLCK
) ? "_LK_RLCK" :
1551 (mode
== _LK_NBRLCK
) ? "_LK_NBRLCK" :
1554 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1556 release_ioinfo(info
);
1557 FIXME("Seek failed\n");
1558 *_errno() = EINVAL
; /* FIXME */
1561 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1564 ret
= 1; /* just to satisfy gcc */
1567 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1572 else if (mode
== _LK_UNLCK
)
1573 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1575 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1576 /* FIXME - what about error settings? */
1577 release_ioinfo(info
);
1578 return ret
? 0 : -1;
1581 /*********************************************************************
1582 * _fseeki64 (MSVCRT.@)
1584 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1589 ret
= _fseeki64_nolock(file
, offset
, whence
);
1595 /*********************************************************************
1596 * _fseeki64_nolock (MSVCRT.@)
1598 int CDECL
_fseeki64_nolock(FILE* file
, __int64 offset
, int whence
)
1602 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1604 offset
+= _ftelli64_nolock(file
);
1607 /* Flush output if needed */
1608 msvcrt_flush_buffer(file
);
1609 /* Reset direction of i/o */
1610 if(file
->_flag
& _IORW
) {
1611 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1613 /* Clear end of file flag */
1614 file
->_flag
&= ~_IOEOF
;
1615 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1620 /*********************************************************************
1623 int CDECL
fseek(FILE* file
, __msvcrt_long offset
, int whence
)
1625 return _fseeki64( file
, offset
, whence
);
1628 /*********************************************************************
1629 * _fseek_nolock (MSVCRT.@)
1631 int CDECL
_fseek_nolock(FILE* file
, __msvcrt_long offset
, int whence
)
1633 return _fseeki64_nolock( file
, offset
, whence
);
1636 /*********************************************************************
1637 * _chsize_s (MSVCRT.@)
1639 int CDECL
_chsize_s(int fd
, __int64 size
)
1645 TRACE("(fd=%d, size=%#I64x)\n", fd
, size
);
1647 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1650 info
= get_ioinfo(fd
);
1651 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1653 /* save the current file pointer */
1654 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1657 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1660 ret
= SetEndOfFile(info
->handle
);
1661 if (!ret
) msvcrt_set_errno(GetLastError());
1664 /* restore the file pointer */
1665 _lseeki64(fd
, cur
, SEEK_SET
);
1669 release_ioinfo(info
);
1670 return ret
? 0 : *_errno();
1673 /*********************************************************************
1674 * _chsize (MSVCRT.@)
1676 int CDECL
_chsize(int fd
, __msvcrt_long size
)
1678 /* _chsize_s returns errno on failure but _chsize should return -1 */
1679 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1682 /*********************************************************************
1683 * clearerr (MSVCRT.@)
1685 void CDECL
clearerr(FILE* file
)
1687 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1690 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1694 /*********************************************************************
1695 * clearerr_s (MSVCRT.@)
1697 int CDECL
clearerr_s(FILE* file
)
1699 TRACE(":file (%p)\n",file
);
1701 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
1704 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1709 #if defined(__i386__)
1710 /* Stack preserving thunk for rewind
1711 * needed for the UIO mod for Fallout: New Vegas
1713 __ASM_GLOBAL_FUNC(rewind_preserve_stack
,
1715 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1716 "call "__ASM_NAME("rewind") "\n\t"
1718 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1722 /*********************************************************************
1725 void CDECL
rewind(FILE* file
)
1727 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1730 _fseek_nolock(file
, 0L, SEEK_SET
);
1735 static int msvcrt_get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1737 int plus
= wcschr(mode
, '+') != NULL
;
1739 TRACE("%s\n", debugstr_w(mode
));
1741 while(*mode
== ' ') mode
++;
1746 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1747 *stream_flags
= plus
? _IORW
: _IOREAD
;
1750 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1751 *stream_flags
= plus
? _IORW
: _IOWRT
;
1754 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1755 *stream_flags
= plus
? _IORW
: _IOWRT
;
1758 MSVCRT_INVALID_PMT(0, EINVAL
);
1762 *stream_flags
|= MSVCRT__commode
;
1764 while (*mode
&& *mode
!=',')
1768 *open_flags
|= _O_BINARY
;
1769 *open_flags
&= ~_O_TEXT
;
1772 *open_flags
|= _O_TEXT
;
1773 *open_flags
&= ~_O_BINARY
;
1777 if(!MSVCRT_CHECK_PMT((*open_flags
& (_O_CREAT
| _O_APPEND
)) == _O_CREAT
))
1779 *open_flags
|= _O_EXCL
;
1783 *open_flags
|= _O_TEMPORARY
;
1786 *open_flags
|= _O_SHORT_LIVED
;
1789 *stream_flags
|= MSVCRT__IOCOMMIT
;
1792 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1795 *open_flags
|= _O_NOINHERIT
;
1803 if (!(*open_flags
& _O_RANDOM
))
1804 *open_flags
|= _O_SEQUENTIAL
;
1807 if (!(*open_flags
& _O_SEQUENTIAL
))
1808 *open_flags
|= _O_RANDOM
;
1811 ERR("incorrect mode flag: %c\n", mode
[-1]);
1818 while(*mode
== ' ') mode
++;
1819 if(!MSVCRT_CHECK_PMT(!wcsncmp(L
"ccs", mode
, 3)))
1822 while(*mode
== ' ') mode
++;
1823 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1826 while(*mode
== ' ') mode
++;
1828 if(!_wcsnicmp(L
"utf-8", mode
, 5))
1830 *open_flags
|= _O_U8TEXT
;
1833 else if(!_wcsnicmp(L
"utf-16le", mode
, 8))
1835 *open_flags
|= _O_U16TEXT
;
1838 else if(!_wcsnicmp(L
"unicode", mode
, 7))
1840 *open_flags
|= _O_WTEXT
;
1845 MSVCRT_INVALID_PMT(0, EINVAL
);
1849 while(*mode
== ' ') mode
++;
1852 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1857 /*********************************************************************
1858 * _fdopen (MSVCRT.@)
1860 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1863 wchar_t *modeW
= NULL
;
1865 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1867 ret
= _wfdopen(fd
, modeW
);
1873 /*********************************************************************
1874 * _wfdopen (MSVCRT.@)
1876 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1878 int open_flags
, stream_flags
;
1881 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1884 if (!(file
= msvcrt_alloc_fp()))
1886 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1891 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1897 /*********************************************************************
1898 * _filelength (MSVCRT.@)
1900 __msvcrt_long CDECL
_filelength(int fd
)
1902 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1905 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1908 if (endPos
!= curPos
)
1909 _lseek(fd
, curPos
, SEEK_SET
);
1916 /*********************************************************************
1917 * _filelengthi64 (MSVCRT.@)
1919 __int64 CDECL
_filelengthi64(int fd
)
1921 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1924 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1927 if (endPos
!= curPos
)
1928 _lseeki64(fd
, curPos
, SEEK_SET
);
1935 /*********************************************************************
1936 * _fileno (MSVCRT.@)
1938 int CDECL
_fileno(FILE* file
)
1940 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1944 /*********************************************************************
1945 * _fstat64 (MSVCRT.@)
1947 int CDECL
_fstat64(int fd
, struct _stat64
* buf
)
1949 ioinfo
*info
= get_ioinfo(fd
);
1953 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1954 if (info
->handle
== INVALID_HANDLE_VALUE
)
1956 release_ioinfo(info
);
1962 WARN(":failed-NULL buf\n");
1963 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1964 release_ioinfo(info
);
1968 memset(buf
, 0, sizeof(struct _stat64
));
1969 type
= GetFileType(info
->handle
);
1970 if (type
== FILE_TYPE_PIPE
)
1972 buf
->st_dev
= buf
->st_rdev
= fd
;
1973 buf
->st_mode
= _S_IFIFO
;
1976 else if (type
== FILE_TYPE_CHAR
)
1978 buf
->st_dev
= buf
->st_rdev
= fd
;
1979 buf
->st_mode
= _S_IFCHR
;
1982 else /* FILE_TYPE_DISK etc. */
1984 FILE_BASIC_INFORMATION basic_info
;
1985 FILE_STANDARD_INFORMATION std_info
;
1989 if ((status
= NtQueryInformationFile( info
->handle
, &io
, &basic_info
, sizeof(basic_info
), FileBasicInformation
)) ||
1990 (status
= NtQueryInformationFile( info
->handle
, &io
, &std_info
, sizeof(std_info
), FileStandardInformation
)))
1992 WARN(":failed-error %lx\n", status
);
1993 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1994 release_ioinfo(info
);
1997 buf
->st_mode
= _S_IFREG
| 0444;
1998 if (!(basic_info
.FileAttributes
& FILE_ATTRIBUTE_READONLY
))
1999 buf
->st_mode
|= 0222;
2000 buf
->st_size
= std_info
.EndOfFile
.QuadPart
;
2001 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&basic_info
.LastAccessTime
, &dw
);
2003 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&basic_info
.LastWriteTime
, &dw
);
2004 buf
->st_mtime
= buf
->st_ctime
= dw
;
2005 buf
->st_nlink
= std_info
.NumberOfLinks
;
2006 TRACE(":dwFileAttributes = %#lx, mode set to %#x\n",
2007 basic_info
.FileAttributes
, buf
->st_mode
);
2009 release_ioinfo(info
);
2013 /*********************************************************************
2014 * _fstati64 (MSVCRT.@)
2016 int CDECL
_fstati64(int fd
, struct _stati64
* buf
)
2019 struct _stat64 buf64
;
2021 ret
= _fstat64(fd
, &buf64
);
2023 msvcrt_stat64_to_stati64(&buf64
, buf
);
2027 /*********************************************************************
2030 int CDECL
_fstat(int fd
, struct _stat
* buf
)
2032 struct _stat64 buf64
;
2034 ret
= _fstat64(fd
, &buf64
);
2036 msvcrt_stat64_to_stat(&buf64
, buf
);
2040 /*********************************************************************
2041 * _fstat32 (MSVCR80.@)
2043 int CDECL
_fstat32(int fd
, struct _stat32
* buf
)
2046 struct _stat64 buf64
;
2048 ret
= _fstat64(fd
, &buf64
);
2050 msvcrt_stat64_to_stat32(&buf64
, buf
);
2054 /*********************************************************************
2055 * _fstat32i64 (MSVCR80.@)
2057 int CDECL
_fstat32i64(int fd
, struct _stat32i64
* buf
)
2060 struct _stat64 buf64
;
2062 ret
= _fstat64(fd
, &buf64
);
2064 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2068 /*********************************************************************
2069 * _fstat64i32 (MSVCR80.@)
2071 int CDECL
_fstat64i32(int fd
, struct _stat64i32
* buf
)
2074 struct _stat64 buf64
;
2076 ret
= _fstat64(fd
, &buf64
);
2078 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2082 /*********************************************************************
2083 * _futime64 (MSVCRT.@)
2085 int CDECL
_futime64(int fd
, struct __utimbuf64
*t
)
2087 ioinfo
*info
= get_ioinfo(fd
);
2092 time_to_filetime( _time64(NULL
), &at
);
2097 time_to_filetime( t
->actime
, &at
);
2098 time_to_filetime( t
->modtime
, &wt
);
2101 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
2103 release_ioinfo(info
);
2104 msvcrt_set_errno(GetLastError());
2107 release_ioinfo(info
);
2111 /*********************************************************************
2112 * _futime32 (MSVCRT.@)
2114 int CDECL
_futime32(int fd
, struct __utimbuf32
*t
)
2118 struct __utimbuf64 t64
;
2119 t64
.actime
= t
->actime
;
2120 t64
.modtime
= t
->modtime
;
2121 return _futime64( fd
, &t64
);
2124 return _futime64( fd
, NULL
);
2127 /*********************************************************************
2128 * _get_osfhandle (MSVCRT.@)
2130 intptr_t CDECL
_get_osfhandle(int fd
)
2132 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
2133 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
2135 if(hand
== INVALID_HANDLE_VALUE
)
2137 return (intptr_t)hand
;
2140 /*********************************************************************
2141 * _mktemp_s (MSVCRT.@)
2143 int CDECL
_mktemp_s(char *pattern
, size_t size
)
2147 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2150 for(len
=0; len
<size
; len
++)
2153 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2159 for(xno
=1; xno
<=6; xno
++)
2160 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2163 id
= GetCurrentProcessId();
2164 for(xno
=1; xno
<6; xno
++) {
2165 pattern
[len
-xno
] = id
%10 + '0';
2169 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2170 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
2179 /*********************************************************************
2180 * _mktemp (MSVCRT.@)
2182 char * CDECL
_mktemp(char *pattern
)
2185 char *retVal
= pattern
;
2193 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2197 id
= GetCurrentProcessId();
2201 int tempNum
= id
/ 10;
2202 *pattern
-- = id
- (tempNum
* 10) + '0';
2208 *pattern
= letter
++;
2209 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2211 } while(letter
<= 'z');
2215 /*********************************************************************
2216 * _wmktemp_s (MSVCRT.@)
2218 int CDECL
_wmktemp_s(wchar_t *pattern
, size_t size
)
2222 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2225 for(len
=0; len
<size
; len
++)
2228 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2234 for(xno
=1; xno
<=6; xno
++)
2235 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2238 id
= GetCurrentProcessId();
2239 for(xno
=1; xno
<6; xno
++) {
2240 pattern
[len
-xno
] = id
%10 + '0';
2244 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2245 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2254 /*********************************************************************
2255 * _wmktemp (MSVCRT.@)
2257 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
2260 wchar_t *retVal
= pattern
;
2262 wchar_t letter
= 'a';
2268 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2272 id
= GetCurrentProcessId();
2276 int tempNum
= id
/ 10;
2277 *pattern
-- = id
- (tempNum
* 10) + '0';
2283 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2285 *pattern
= letter
++;
2286 } while(letter
!= '|');
2290 static unsigned split_oflags(unsigned oflags
)
2293 unsigned unsupp
; /* until we support everything */
2295 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
2296 if (oflags
& _O_BINARY
) {/* Nothing to do */}
2297 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
2298 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
2299 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
2300 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
2305 if (!(fmode
& _O_BINARY
)) wxflags
|= WX_TEXT
; /* default to TEXT*/
2307 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2309 if ((unsupp
= oflags
& ~(_O_BINARY
| _O_TEXT
| _O_APPEND
| _O_TRUNC
| _O_EXCL
| _O_CREAT
|
2310 _O_RDWR
| _O_WRONLY
| _O_TEMPORARY
| _O_NOINHERIT
| _O_SEQUENTIAL
|
2311 _O_RANDOM
| _O_SHORT_LIVED
| _O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
)))
2312 ERR(":unsupported oflags %#x\n",unsupp
);
2317 /*********************************************************************
2320 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
2323 SECURITY_ATTRIBUTES sa
;
2324 HANDLE readHandle
, writeHandle
;
2332 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2333 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
2334 sa
.lpSecurityDescriptor
= NULL
;
2335 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2337 unsigned int wxflags
= split_oflags(textmode
);
2340 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2344 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2353 CloseHandle(writeHandle
);
2359 CloseHandle(readHandle
);
2360 CloseHandle(writeHandle
);
2365 msvcrt_set_errno(GetLastError());
2370 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2372 char bom
[sizeof(utf8_bom
)];
2375 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2378 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2379 oflags
= (oflags
& ~(_O_WTEXT
| _O_U16TEXT
)) | _O_U8TEXT
;
2380 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2382 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2383 oflags
= (oflags
& ~(_O_WTEXT
| _O_U8TEXT
)) | _O_U16TEXT
;
2385 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2391 /*********************************************************************
2392 * _wsopen_dispatch (UCRTBASE.@)
2394 int CDECL
_wsopen_dispatch( const wchar_t* path
, int oflags
, int shflags
, int pmode
,
2395 int *fd
, int secure
)
2397 DWORD access
= 0, creation
= 0, attrib
;
2398 SECURITY_ATTRIBUTES sa
;
2399 DWORD sharing
, type
;
2403 TRACE("path: (%s) oflags: %#x shflags: %#x pmode: %#x fd*: %p secure: %d\n",
2404 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2406 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
2409 wxflag
= split_oflags(oflags
);
2410 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
2412 case _O_RDONLY
: access
|= GENERIC_READ
; break;
2413 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
2414 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2417 if (oflags
& _O_CREAT
)
2419 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(_S_IREAD
| _S_IWRITE
))))
2422 if (oflags
& _O_EXCL
)
2423 creation
= CREATE_NEW
;
2424 else if (oflags
& _O_TRUNC
)
2425 creation
= CREATE_ALWAYS
;
2427 creation
= OPEN_ALWAYS
;
2429 else /* no _O_CREAT */
2431 if (oflags
& _O_TRUNC
)
2432 creation
= TRUNCATE_EXISTING
;
2434 creation
= OPEN_EXISTING
;
2443 sharing
= FILE_SHARE_READ
;
2446 sharing
= FILE_SHARE_WRITE
;
2449 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2452 ERR( "Unhandled shflags %#x\n", shflags
);
2456 if (!(pmode
& ~MSVCRT_umask
& _S_IWRITE
))
2457 attrib
= FILE_ATTRIBUTE_READONLY
;
2459 attrib
= FILE_ATTRIBUTE_NORMAL
;
2461 if (oflags
& _O_TEMPORARY
)
2463 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2465 sharing
|= FILE_SHARE_DELETE
;
2468 if (oflags
& _O_RANDOM
)
2469 attrib
|= FILE_FLAG_RANDOM_ACCESS
;
2470 if (oflags
& _O_SEQUENTIAL
)
2471 attrib
|= FILE_FLAG_SEQUENTIAL_SCAN
;
2472 if (oflags
& _O_SHORT_LIVED
)
2473 attrib
|= FILE_ATTRIBUTE_TEMPORARY
;
2475 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2476 sa
.lpSecurityDescriptor
= NULL
;
2477 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
2479 if ((oflags
& (_O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
))
2480 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2481 && !(access
&GENERIC_READ
))
2483 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2484 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2485 if (hand
!= INVALID_HANDLE_VALUE
)
2487 oflags
= check_bom(hand
, oflags
, FALSE
);
2492 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2493 if (hand
== INVALID_HANDLE_VALUE
) {
2494 WARN(":failed-last error (%ld)\n", GetLastError());
2495 msvcrt_set_errno(GetLastError());
2499 if (oflags
& (_O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
))
2501 LARGE_INTEGER size
= {{0}};
2503 if ((access
& GENERIC_WRITE
) && (creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
))
2504 GetFileSizeEx(hand
, &size
);
2506 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2507 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2508 || ((creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
) && !size
.QuadPart
)))
2510 if (oflags
& _O_U8TEXT
)
2512 DWORD written
= 0, tmp
;
2514 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2515 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2517 if (written
!= sizeof(utf8_bom
)) {
2518 WARN("error writing BOM\n");
2520 msvcrt_set_errno(GetLastError());
2526 DWORD written
= 0, tmp
;
2528 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2529 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2531 if (written
!= sizeof(utf16_bom
))
2533 WARN("error writing BOM\n");
2535 msvcrt_set_errno(GetLastError());
2538 oflags
|= _O_U16TEXT
;
2541 else if (access
& GENERIC_READ
)
2542 oflags
= check_bom(hand
, oflags
, TRUE
);
2545 type
= GetFileType(hand
);
2546 if (type
== FILE_TYPE_CHAR
)
2548 else if (type
== FILE_TYPE_PIPE
)
2551 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2555 if (oflags
& _O_WTEXT
)
2556 ioinfo_set_unicode(get_ioinfo_nolock(*fd
), TRUE
);
2558 if (oflags
& _O_U16TEXT
)
2559 ioinfo_set_textmode(get_ioinfo_nolock(*fd
), TEXTMODE_UTF16LE
);
2560 else if (oflags
& _O_U8TEXT
)
2561 ioinfo_set_textmode(get_ioinfo_nolock(*fd
), TEXTMODE_UTF8
);
2563 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2568 /*********************************************************************
2569 * _wsopen_s (MSVCRT.@)
2571 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
2573 return _wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2576 /*********************************************************************
2577 * _wsopen (MSVCRT.@)
2579 int WINAPIV
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
2584 if (oflags
& _O_CREAT
)
2588 va_start(ap
, shflags
);
2589 pmode
= va_arg(ap
, int);
2595 return _wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2599 /*********************************************************************
2600 * _sopen_dispatch (UCRTBASE.@)
2602 int CDECL
_sopen_dispatch( const char *path
, int oflags
, int shflags
,
2603 int pmode
, int *fd
, int secure
)
2608 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2611 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2614 ret
= _wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2619 /*********************************************************************
2620 * _sopen_s (MSVCRT.@)
2622 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2624 return _sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2627 /*********************************************************************
2630 int WINAPIV
_sopen( const char *path
, int oflags
, int shflags
, ... )
2635 if (oflags
& _O_CREAT
)
2639 va_start(ap
, shflags
);
2640 pmode
= va_arg(ap
, int);
2646 return _sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2649 /*********************************************************************
2652 int WINAPIV
_open( const char *path
, int flags
, ... )
2656 if (flags
& _O_CREAT
)
2659 va_start(ap
, flags
);
2660 pmode
= va_arg(ap
, int);
2662 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
2665 return _sopen( path
, flags
, _SH_DENYNO
);
2668 /*********************************************************************
2671 int WINAPIV
_wopen(const wchar_t *path
,int flags
,...)
2675 if (flags
& _O_CREAT
)
2678 va_start(ap
, flags
);
2679 pmode
= va_arg(ap
, int);
2681 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
2684 return _wsopen( path
, flags
, _SH_DENYNO
);
2687 /*********************************************************************
2690 int CDECL
_creat(const char *path
, int pmode
)
2692 int flags
= _O_CREAT
| _O_TRUNC
| _O_RDWR
;
2693 return _open(path
, flags
, pmode
);
2696 /*********************************************************************
2697 * _wcreat (MSVCRT.@)
2699 int CDECL
_wcreat(const wchar_t *path
, int pmode
)
2701 int flags
= _O_CREAT
| _O_TRUNC
| _O_RDWR
;
2702 return _wopen(path
, flags
, pmode
);
2705 /*********************************************************************
2706 * _open_osfhandle (MSVCRT.@)
2708 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2713 /* _O_RDONLY (0) always matches, so set the read flag
2714 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2715 * file, so set the write flag. It also only sets _O_TEXT if it wants
2716 * text - it never sets _O_BINARY.
2718 /* don't let split_oflags() decide the mode if no mode is passed */
2719 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2720 oflags
|= _O_BINARY
;
2722 flags
= GetFileType((HANDLE
)handle
);
2723 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2725 msvcrt_set_errno(GetLastError());
2729 if (flags
== FILE_TYPE_CHAR
)
2731 else if (flags
== FILE_TYPE_PIPE
)
2735 flags
|= split_oflags(oflags
);
2737 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2738 TRACE(":handle (%Iu) fd (%d) flags %#lx\n", handle
, fd
, flags
);
2742 /*********************************************************************
2745 int CDECL
_rmtmp(void)
2747 int num_removed
= 0, i
;
2751 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2752 file
= msvcrt_get_file(i
);
2754 if (file
->_tmpfname
)
2763 TRACE(":removed (%d) temp files\n",num_removed
);
2767 static inline int get_utf8_char_len(char ch
)
2769 if((ch
&0xf8) == 0xf0)
2771 else if((ch
&0xf0) == 0xe0)
2773 else if((ch
&0xe0) == 0xc0)
2778 /*********************************************************************
2779 * (internal) read_utf8
2781 static int read_utf8(ioinfo
*fdinfo
, wchar_t *buf
, unsigned int count
)
2783 HANDLE hand
= fdinfo
->handle
;
2784 char min_buf
[4], *readbuf
, lookahead
;
2785 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2787 /* make the buffer big enough to hold at least one character */
2788 /* read bytes have to fit to output and lookahead buffers */
2790 readbuf_size
= count
< 4 ? 4 : count
;
2791 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2796 if(fdinfo
->lookahead
[0] != '\n') {
2797 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2798 fdinfo
->lookahead
[0] = '\n';
2800 if(fdinfo
->lookahead
[1] != '\n') {
2801 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2802 fdinfo
->lookahead
[1] = '\n';
2804 if(fdinfo
->lookahead
[2] != '\n') {
2805 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2806 fdinfo
->lookahead
[2] = '\n';
2811 /* NOTE: this case is broken in native dll, reading
2812 * sometimes fails when small buffer is passed
2815 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2816 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2817 fdinfo
->wxflag
|= WX_ATEOF
;
2820 msvcrt_set_errno(GetLastError());
2821 if (GetLastError() == ERROR_ACCESS_DENIED
)
2825 }else if(!num_read
) {
2826 fdinfo
->wxflag
|= WX_ATEOF
;
2832 char_len
= get_utf8_char_len(readbuf
[0]);
2834 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2838 if(readbuf
[0] == '\n')
2839 fdinfo
->wxflag
|= WX_READNL
;
2841 fdinfo
->wxflag
&= ~WX_READNL
;
2843 if(readbuf
[0] == 0x1a) {
2844 fdinfo
->wxflag
|= WX_ATEOF
;
2848 if(readbuf
[0] == '\r') {
2849 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2851 else if(lookahead
== '\n')
2855 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2856 fdinfo
->lookahead
[0] = lookahead
;
2858 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2863 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2864 msvcrt_set_errno(GetLastError());
2871 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2874 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2875 fdinfo
->wxflag
|= WX_ATEOF
;
2876 if (readbuf
!= min_buf
) free(readbuf
);
2879 msvcrt_set_errno(GetLastError());
2880 if (GetLastError() == ERROR_ACCESS_DENIED
)
2882 if (readbuf
!= min_buf
) free(readbuf
);
2885 }else if(!pos
&& !num_read
) {
2886 fdinfo
->wxflag
|= WX_ATEOF
;
2887 if (readbuf
!= min_buf
) free(readbuf
);
2892 if(readbuf
[0] == '\n')
2893 fdinfo
->wxflag
|= WX_READNL
;
2895 fdinfo
->wxflag
&= ~WX_READNL
;
2897 /* Find first byte of last character (may be incomplete) */
2898 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2899 if((readbuf
[i
]&0xc0) != 0x80)
2901 char_len
= get_utf8_char_len(readbuf
[i
]);
2902 if(char_len
+i
<= pos
)
2905 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2907 fdinfo
->lookahead
[0] = readbuf
[i
];
2909 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2911 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2913 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2917 for(i
=0, j
=0; i
<pos
; i
++) {
2918 if(readbuf
[i
] == 0x1a) {
2919 fdinfo
->wxflag
|= WX_ATEOF
;
2923 /* strip '\r' if followed by '\n' */
2924 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2925 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2926 readbuf
[j
++] = '\r';
2927 }else if(lookahead
== '\n' && j
==0) {
2928 readbuf
[j
++] = '\n';
2930 if(lookahead
!= '\n')
2931 readbuf
[j
++] = '\r';
2933 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2934 fdinfo
->lookahead
[0] = lookahead
;
2936 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2938 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2939 readbuf
[j
++] = readbuf
[i
];
2944 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2945 msvcrt_set_errno(GetLastError());
2946 if (readbuf
!= min_buf
) free(readbuf
);
2950 if (readbuf
!= min_buf
) free(readbuf
);
2954 /*********************************************************************
2957 * When reading \r as last character in text mode, read() positions
2958 * the file pointer on the \r character while getc() goes on to
2961 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2963 DWORD num_read
, utf16
;
2964 char *bufstart
= buf
;
2969 if (fdinfo
->wxflag
& WX_ATEOF
) {
2970 TRACE("already at EOF, returning 0\n");
2973 /* Don't trace small reads, it gets *very* annoying */
2975 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2976 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2982 utf16
= ioinfo_get_textmode(fdinfo
) == TEXTMODE_UTF16LE
;
2983 if (ioinfo_get_textmode(fdinfo
) != TEXTMODE_ANSI
&& count
&1)
2989 if((fdinfo
->wxflag
&WX_TEXT
) && ioinfo_get_textmode(fdinfo
) == TEXTMODE_UTF8
)
2990 return read_utf8(fdinfo
, buf
, count
);
2992 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2994 if (fdinfo
->lookahead
[0] != '\n')
2996 bufstart
[0] = fdinfo
->lookahead
[0];
2997 fdinfo
->lookahead
[0] = '\n';
3001 bufstart
[1] = fdinfo
->lookahead
[1];
3002 fdinfo
->lookahead
[1] = '\n';
3005 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
3006 num_read
+= 1+utf16
;
3011 if(utf16
&& (num_read
&1))
3013 /* msvcr90 uses uninitialized value from the buffer in this case */
3014 /* msvcrt ignores additional data */
3015 ERR("got odd number of bytes in UTF16 mode\n");
3019 if (count
!= 0 && num_read
== 0)
3021 fdinfo
->wxflag
|= WX_ATEOF
;
3022 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
3024 else if (fdinfo
->wxflag
& WX_TEXT
)
3028 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
3029 fdinfo
->wxflag
|= WX_READNL
;
3031 fdinfo
->wxflag
&= ~WX_READNL
;
3033 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
3035 /* in text mode, a ctrl-z signals EOF */
3036 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
3038 fdinfo
->wxflag
|= WX_ATEOF
;
3039 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
3043 /* in text mode, strip \r if followed by \n */
3044 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
3049 lookahead
[1] = '\n';
3050 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
3052 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
3054 bufstart
[j
++] = '\n';
3055 if(utf16
) bufstart
[j
++] = 0;
3059 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
3061 bufstart
[j
++] = '\r';
3062 if(utf16
) bufstart
[j
++] = 0;
3065 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
3067 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
3069 bufstart
[j
++] = '\n';
3070 if (utf16
) bufstart
[j
++] = 0;
3074 fdinfo
->lookahead
[0] = lookahead
[0];
3075 fdinfo
->lookahead
[1] = lookahead
[1];
3079 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
3084 bufstart
[j
++] = '\r';
3085 if(utf16
) bufstart
[j
++] = 0;
3088 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
3089 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
3091 bufstart
[j
++] = bufstart
[i
];
3092 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
3100 if (GetLastError() == ERROR_BROKEN_PIPE
)
3102 TRACE(":end-of-pipe\n");
3103 fdinfo
->wxflag
|= WX_ATEOF
;
3108 TRACE(":failed-last error (%ld)\n", GetLastError());
3109 msvcrt_set_errno(GetLastError());
3110 if (GetLastError() == ERROR_ACCESS_DENIED
)
3117 TRACE("(%lu), %s\n", num_read
, debugstr_an(buf
, num_read
));
3121 /*********************************************************************
3124 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
3129 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
3134 info
= get_ioinfo(fd
);
3135 num_read
= read_i(fd
, info
, buf
, count
);
3136 release_ioinfo(info
);
3140 /*********************************************************************
3141 * _setmode (MSVCRT.@)
3143 int CDECL
_setmode(int fd
,int mode
)
3145 ioinfo
*info
= get_ioinfo(fd
);
3146 int ret
= info
->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
3148 if(ret
==_O_TEXT
&& ioinfo_get_textmode(info
) != TEXTMODE_ANSI
)
3151 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
3152 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
3154 release_ioinfo(info
);
3158 if(info
== &MSVCRT___badioinfo
) {
3163 if(mode
== _O_BINARY
) {
3164 info
->wxflag
&= ~WX_TEXT
;
3165 ioinfo_set_textmode(info
, TEXTMODE_ANSI
);
3166 release_ioinfo(info
);
3170 info
->wxflag
|= WX_TEXT
;
3172 ioinfo_set_textmode(info
, TEXTMODE_ANSI
);
3173 else if(mode
== _O_U8TEXT
)
3174 ioinfo_set_textmode(info
, TEXTMODE_UTF8
);
3176 ioinfo_set_textmode(info
, TEXTMODE_UTF16LE
);
3178 release_ioinfo(info
);
3182 /*********************************************************************
3183 * _stat64 (MSVCRT.@)
3185 int CDECL
_stat64(const char* path
, struct _stat64
* buf
)
3188 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3189 unsigned short mode
= ALL_S_IREAD
;
3192 TRACE(":file (%s) buf(%p)\n", path
, buf
);
3194 plen
= strlen(path
);
3195 while (plen
&& path
[plen
-1]==' ')
3198 if (plen
==2 && path
[1]==':')
3205 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3212 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
3214 TRACE("failed (%ld)\n", GetLastError());
3219 memset(buf
,0,sizeof(struct _stat64
));
3221 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3222 Bon 011120: This FIXME seems incorrect
3223 Also a letter as first char isn't enough to be classified
3226 if (isalpha(*path
)&& (*(path
+1)==':'))
3227 buf
->st_dev
= buf
->st_rdev
= _toupper_l(*path
, NULL
) - 'A'; /* drive num */
3229 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
3231 /* Dir, or regular file? */
3232 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3233 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
3238 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3240 unsigned int ext
= _tolower_l(path
[plen
-1], NULL
) |
3241 (_tolower_l(path
[plen
-2], NULL
) << 8) |
3242 (_tolower_l(path
[plen
-3], NULL
) << 16);
3243 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3244 mode
|= ALL_S_IEXEC
;
3248 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3249 mode
|= ALL_S_IWRITE
;
3251 buf
->st_mode
= mode
;
3253 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3254 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3256 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3257 buf
->st_mtime
= buf
->st_ctime
= dw
;
3258 TRACE("%d %d %#I64x %I64d %I64d %I64d\n", buf
->st_mode
, buf
->st_nlink
,
3259 buf
->st_size
, buf
->st_atime
, buf
->st_mtime
, buf
->st_ctime
);
3263 /*********************************************************************
3264 * _stati64 (MSVCRT.@)
3266 int CDECL
_stati64(const char* path
, struct _stati64
* buf
)
3269 struct _stat64 buf64
;
3271 ret
= _stat64(path
, &buf64
);
3273 msvcrt_stat64_to_stati64(&buf64
, buf
);
3277 /*********************************************************************
3280 int CDECL
_stat(const char* path
, struct _stat
* buf
)
3283 struct _stat64 buf64
;
3285 ret
= _stat64( path
, &buf64
);
3287 msvcrt_stat64_to_stat(&buf64
, buf
);
3291 #if _MSVCR_VER >= 80
3293 /*********************************************************************
3294 * _stat32 (MSVCR80.@)
3296 int CDECL
_stat32(const char *path
, struct _stat32
*buf
)
3299 struct _stat64 buf64
;
3301 ret
= _stat64(path
, &buf64
);
3303 msvcrt_stat64_to_stat32(&buf64
, buf
);
3307 /*********************************************************************
3308 * _stat32i64 (MSVCR80.@)
3310 int CDECL
_stat32i64(const char *path
, struct _stat32i64
*buf
)
3313 struct _stat64 buf64
;
3315 ret
= _stat64(path
, &buf64
);
3317 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3321 /*********************************************************************
3322 * _stat64i32 (MSVCR80.@)
3324 int CDECL
_stat64i32(const char* path
, struct _stat64i32
*buf
)
3327 struct _stat64 buf64
;
3329 ret
= _stat64(path
, &buf64
);
3331 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3335 #endif /* _MSVCR_VER >= 80 */
3337 /*********************************************************************
3338 * _wstat64 (MSVCRT.@)
3340 int CDECL
_wstat64(const wchar_t* path
, struct _stat64
* buf
)
3343 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3344 unsigned short mode
= ALL_S_IREAD
;
3347 TRACE(":file (%s) buf(%p)\n", debugstr_w(path
), buf
);
3349 plen
= wcslen(path
);
3350 while (plen
&& path
[plen
-1]==' ')
3353 if (plen
==2 && path
[1]==':')
3360 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3367 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3369 TRACE("failed (%ld)\n", GetLastError());
3374 memset(buf
,0,sizeof(struct _stat64
));
3376 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3377 if (iswalpha(*path
) && path
[1] == ':')
3378 buf
->st_dev
= buf
->st_rdev
= towupper(*path
) - 'A'; /* drive num */
3380 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
3382 /* Dir, or regular file? */
3383 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3384 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
3389 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3391 ULONGLONG ext
= towlower(path
[plen
-1]) | (towlower(path
[plen
-2]) << 16) |
3392 ((ULONGLONG
)towlower(path
[plen
-3]) << 32);
3393 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3394 mode
|= ALL_S_IEXEC
;
3398 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3399 mode
|= ALL_S_IWRITE
;
3401 buf
->st_mode
= mode
;
3403 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3404 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3406 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3407 buf
->st_mtime
= buf
->st_ctime
= dw
;
3408 TRACE("%d %d %#I64x %I64d %I64d %I64d\n", buf
->st_mode
, buf
->st_nlink
,
3409 buf
->st_size
, buf
->st_atime
, buf
->st_mtime
, buf
->st_ctime
);
3413 /*********************************************************************
3414 * _wstati64 (MSVCRT.@)
3416 int CDECL
_wstati64(const wchar_t* path
, struct _stati64
* buf
)
3419 struct _stat64 buf64
;
3421 ret
= _wstat64(path
, &buf64
);
3423 msvcrt_stat64_to_stati64(&buf64
, buf
);
3427 /*********************************************************************
3430 int CDECL
_wstat(const wchar_t* path
, struct _stat
* buf
)
3433 struct _stat64 buf64
;
3435 ret
= _wstat64( path
, &buf64
);
3436 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3440 #if _MSVCR_VER >= 80
3442 /*********************************************************************
3443 * _wstat32 (MSVCR80.@)
3445 int CDECL
_wstat32(const wchar_t *path
, struct _stat32
*buf
)
3448 struct _stat64 buf64
;
3450 ret
= _wstat64(path
, &buf64
);
3452 msvcrt_stat64_to_stat32(&buf64
, buf
);
3456 /*********************************************************************
3457 * _wstat32i64 (MSVCR80.@)
3459 int CDECL
_wstat32i64(const wchar_t *path
, struct _stat32i64
*buf
)
3462 struct _stat64 buf64
;
3464 ret
= _wstat64(path
, &buf64
);
3466 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3470 /*********************************************************************
3471 * _wstat64i32 (MSVCR80.@)
3473 int CDECL
_wstat64i32(const wchar_t *path
, struct _stat64i32
*buf
)
3476 struct _stat64 buf64
;
3478 ret
= _wstat64(path
, &buf64
);
3480 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3484 #endif /* _MSVCR_VER >= 80 */
3486 /*********************************************************************
3489 __msvcrt_long CDECL
_tell(int fd
)
3491 return _lseek(fd
, 0, SEEK_CUR
);
3494 /*********************************************************************
3495 * _telli64 (MSVCRT.@)
3497 __int64 CDECL
_telli64(int fd
)
3499 return _lseeki64(fd
, 0, SEEK_CUR
);
3502 /*********************************************************************
3503 * _tempnam (MSVCRT.@)
3505 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
3507 char tmpbuf
[MAX_PATH
];
3508 const char *tmp_dir
= getenv("TMP");
3510 if (tmp_dir
) dir
= tmp_dir
;
3512 TRACE("dir (%s) prefix (%s)\n", dir
, prefix
);
3513 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3515 TRACE("got name (%s)\n", tmpbuf
);
3516 DeleteFileA(tmpbuf
);
3517 return _strdup(tmpbuf
);
3519 TRACE("failed (%ld)\n", GetLastError());
3523 /*********************************************************************
3524 * _wtempnam (MSVCRT.@)
3526 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
3528 wchar_t tmpbuf
[MAX_PATH
];
3529 const wchar_t *tmp_dir
= _wgetenv(L
"TMP");
3531 if (tmp_dir
) dir
= tmp_dir
;
3533 TRACE("dir (%s) prefix (%s)\n", debugstr_w(dir
), debugstr_w(prefix
));
3534 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3536 TRACE("got name (%s)\n", debugstr_w(tmpbuf
));
3537 DeleteFileW(tmpbuf
);
3538 return _wcsdup(tmpbuf
);
3540 TRACE("failed (%ld)\n", GetLastError());
3544 /*********************************************************************
3547 int CDECL
_umask(int umask
)
3549 int old_umask
= MSVCRT_umask
;
3550 TRACE("(%d)\n",umask
);
3551 MSVCRT_umask
= umask
;
3555 /*********************************************************************
3556 * _utime64 (MSVCRT.@)
3558 int CDECL
_utime64(const char* path
, struct __utimbuf64
*t
)
3560 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
3564 int retVal
= _futime64(fd
, t
);
3571 /*********************************************************************
3572 * _utime32 (MSVCRT.@)
3574 int CDECL
_utime32(const char* path
, struct __utimbuf32
*t
)
3578 struct __utimbuf64 t64
;
3579 t64
.actime
= t
->actime
;
3580 t64
.modtime
= t
->modtime
;
3581 return _utime64( path
, &t64
);
3584 return _utime64( path
, NULL
);
3587 /*********************************************************************
3588 * _wutime64 (MSVCRT.@)
3590 int CDECL
_wutime64(const wchar_t* path
, struct __utimbuf64
*t
)
3592 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
3596 int retVal
= _futime64(fd
, t
);
3603 /*********************************************************************
3604 * _wutime32 (MSVCRT.@)
3606 int CDECL
_wutime32(const wchar_t* path
, struct __utimbuf32
*t
)
3610 struct __utimbuf64 t64
;
3611 t64
.actime
= t
->actime
;
3612 t64
.modtime
= t
->modtime
;
3613 return _wutime64( path
, &t64
);
3616 return _wutime64( path
, NULL
);
3619 /*********************************************************************
3622 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
3624 ioinfo
*info
= get_ioinfo(fd
);
3625 HANDLE hand
= info
->handle
;
3626 DWORD num_written
, i
;
3627 BOOL console
= FALSE
;
3629 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3632 release_ioinfo(info
);
3636 if (ioinfo_get_textmode(info
) != TEXTMODE_ANSI
&& count
&1)
3639 release_ioinfo(info
);
3643 /* If appending, go to EOF */
3644 if (info
->wxflag
& WX_APPEND
)
3645 _lseek(fd
, 0, FILE_END
);
3647 if (!(info
->wxflag
& WX_TEXT
))
3649 if (!WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3650 || num_written
!= count
)
3652 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd
,
3653 hand
, GetLastError());
3654 msvcrt_set_errno(GetLastError());
3655 if (GetLastError() == ERROR_ACCESS_DENIED
)
3660 release_ioinfo(info
);
3664 if (_isatty(fd
)) console
= VerifyConsoleIoHandle(hand
);
3665 for (i
= 0; i
< count
;)
3667 const char *s
= buf
;
3671 if (ioinfo_get_textmode(info
) == TEXTMODE_ANSI
&& console
)
3673 char conv
[sizeof(lfbuf
)];
3676 #if _MSVCR_VER >= 80
3677 if (info
->dbcsBufferUsed
)
3679 conv
[j
++] = info
->dbcsBuffer
[0];
3680 info
->dbcsBufferUsed
= FALSE
;
3686 for (; i
< count
&& j
< sizeof(conv
)-1 &&
3687 len
< (sizeof(lfbuf
) - 1) / sizeof(WCHAR
); i
++, j
++, len
++)
3689 if (isleadbyte((unsigned char)s
[i
]))
3695 #if _MSVCR_VER >= 80
3696 info
->dbcsBuffer
[0] = conv
[j
-1];
3697 info
->dbcsBufferUsed
= TRUE
;
3701 release_ioinfo(info
);
3706 else if (s
[i
] == '\n')
3714 len
= mbstowcs((WCHAR
*)lfbuf
, conv
, len
);
3717 msvcrt_set_errno(GetLastError());
3718 release_ioinfo(info
);
3723 else if (ioinfo_get_textmode(info
) == TEXTMODE_ANSI
)
3725 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-1; i
++, j
++)
3732 else if (ioinfo_get_textmode(info
) == TEXTMODE_UTF16LE
|| console
)
3734 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-3; i
++, j
++)
3736 if (s
[i
] == '\n' && !s
[i
+1])
3741 lfbuf
[j
++] = s
[i
++];
3747 char conv
[sizeof(lfbuf
)/4];
3749 for (j
= 0; i
< count
&& j
< sizeof(conv
)-3; i
++, j
++)
3751 if (s
[i
] == '\n' && !s
[i
+1])
3760 j
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)conv
, j
/2, lfbuf
, sizeof(lfbuf
), NULL
, NULL
);
3763 msvcrt_set_errno(GetLastError());
3764 release_ioinfo(info
);
3772 if (!WriteConsoleW(hand
, lfbuf
, j
, &num_written
, NULL
))
3775 else if (!WriteFile(hand
, lfbuf
, j
, &num_written
, NULL
))
3780 if (num_written
!= j
)
3782 TRACE("WriteFile/WriteConsoleW (fd %d, hand %p) failed-last error (%ld)\n", fd
,
3783 hand
, GetLastError());
3784 msvcrt_set_errno(GetLastError());
3785 if (GetLastError() == ERROR_ACCESS_DENIED
)
3787 release_ioinfo(info
);
3792 release_ioinfo(info
);
3796 /*********************************************************************
3799 int CDECL
_putw(int val
, FILE* file
)
3804 len
= _write(file
->_file
, &val
, sizeof(val
));
3805 if (len
== sizeof(val
)) {
3810 file
->_flag
|= _IOERR
;
3815 /*********************************************************************
3818 int CDECL
fclose(FILE* file
)
3822 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3825 ret
= _fclose_nolock(file
);
3831 /*********************************************************************
3832 * _fclose_nolock (MSVCRT.@)
3834 int CDECL
_fclose_nolock(FILE* file
)
3838 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3840 if(!(file
->_flag
& (_IOREAD
| _IOWRT
| _IORW
)))
3847 free(file
->_tmpfname
);
3848 file
->_tmpfname
= NULL
;
3849 /* flush stdio buffers */
3850 if(file
->_flag
& _IOWRT
)
3851 _fflush_nolock(file
);
3852 if(file
->_flag
& _IOMYBUF
)
3855 r
=_close(file
->_file
);
3858 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
3861 /*********************************************************************
3864 int CDECL
feof(FILE* file
)
3866 return file
->_flag
& _IOEOF
;
3869 /*********************************************************************
3872 int CDECL
ferror(FILE* file
)
3874 return file
->_flag
& _IOERR
;
3877 /*********************************************************************
3878 * _filbuf (MSVCRT.@)
3880 int CDECL
_filbuf(FILE* file
)
3884 if(file
->_flag
& _IOSTRG
)
3887 /* Allocate buffer if needed */
3888 if(!(file
->_flag
& (MSVCRT__NOBUF
| _IOMYBUF
| MSVCRT__USERBUF
)))
3889 msvcrt_alloc_buffer(file
);
3891 if(!(file
->_flag
& _IOREAD
)) {
3892 if(file
->_flag
& _IORW
)
3893 file
->_flag
|= _IOREAD
;
3898 if(!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
))) {
3900 if ((r
= _read(file
->_file
,&c
,1)) != 1) {
3901 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
3907 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3909 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
3915 file
->_ptr
= file
->_base
+1;
3916 c
= *(unsigned char *)file
->_base
;
3921 /*********************************************************************
3924 int CDECL
fgetc(FILE* file
)
3929 ret
= _fgetc_nolock(file
);
3935 /*********************************************************************
3936 * _fgetc_nolock (MSVCRT.@)
3938 int CDECL
_fgetc_nolock(FILE* file
)
3945 i
= (unsigned char *)file
->_ptr
++;
3953 /*********************************************************************
3954 * _fgetchar (MSVCRT.@)
3956 int CDECL
_fgetchar(void)
3958 return fgetc(stdin
);
3961 /*********************************************************************
3964 char * CDECL
fgets(char *s
, int size
, FILE* file
)
3967 char * buf_start
= s
;
3969 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3970 file
,file
->_file
,s
,size
);
3974 while ((size
>1) && (cc
= _fgetc_nolock(file
)) != EOF
&& cc
!= '\n')
3979 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3981 TRACE(":nothing read\n");
3985 if ((cc
!= EOF
) && (size
> 1))
3988 TRACE(":got %s\n", debugstr_a(buf_start
));
3993 /*********************************************************************
3996 wint_t CDECL
fgetwc(FILE* file
)
4001 ret
= _fgetwc_nolock(file
);
4007 /*********************************************************************
4008 * _fgetwc_nolock (MSVCRT.@)
4010 wint_t CDECL
_fgetwc_nolock(FILE* file
)
4015 if(ioinfo_get_textmode(get_ioinfo_nolock(file
->_file
)) != TEXTMODE_ANSI
4016 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4019 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
4020 ch
= _fgetc_nolock(file
);
4028 char mbs
[MB_LEN_MAX
];
4031 ch
= _fgetc_nolock(file
);
4034 if(isleadbyte((unsigned char)mbs
[0])) {
4035 ch
= _fgetc_nolock(file
);
4045 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
4052 /*********************************************************************
4055 int CDECL
_getw(FILE* file
)
4063 for (j
=0; j
<sizeof(int); j
++) {
4064 k
= _fgetc_nolock(file
);
4066 file
->_flag
|= _IOEOF
;
4077 /*********************************************************************
4080 wint_t CDECL
getwc(FILE* file
)
4082 return fgetwc(file
);
4085 /*********************************************************************
4086 * _fgetwchar (MSVCRT.@)
4088 wint_t CDECL
_fgetwchar(void)
4090 return fgetwc(stdin
);
4093 /*********************************************************************
4094 * getwchar (MSVCRT.@)
4096 wint_t CDECL
getwchar(void)
4098 return _fgetwchar();
4101 /*********************************************************************
4104 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
4107 wchar_t * buf_start
= s
;
4109 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
4110 file
,file
->_file
,s
,size
);
4114 while ((size
>1) && (cc
= _fgetwc_nolock(file
)) != WEOF
&& cc
!= '\n')
4119 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
4121 TRACE(":nothing read\n");
4125 if ((cc
!= WEOF
) && (size
> 1))
4128 TRACE(":got %s\n", debugstr_w(buf_start
));
4133 /*********************************************************************
4134 * _flsbuf (MSVCRT.@)
4136 int CDECL
_flsbuf(int c
, FILE* file
)
4138 /* Flush output buffer */
4139 if(!(file
->_flag
& (MSVCRT__NOBUF
| _IOMYBUF
| MSVCRT__USERBUF
))) {
4140 msvcrt_alloc_buffer(file
);
4143 if(!(file
->_flag
& _IOWRT
)) {
4144 if(!(file
->_flag
& _IORW
)) {
4145 file
->_flag
|= _IOERR
;
4149 file
->_flag
|= _IOWRT
;
4151 if(file
->_flag
& _IOREAD
) {
4152 if(!(file
->_flag
& _IOEOF
)) {
4153 file
->_flag
|= _IOERR
;
4157 file
->_ptr
= file
->_base
;
4158 file
->_flag
&= ~(_IOREAD
| _IOEOF
);
4161 if(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) {
4164 if(file
->_cnt
<= 0) {
4165 res
= msvcrt_flush_buffer(file
);
4168 file
->_flag
|= _IOWRT
;
4169 file
->_cnt
=file
->_bufsiz
;
4177 /* set _cnt to 0 for unbuffered FILEs */
4179 len
= _write(file
->_file
, &cc
, 1);
4182 file
->_flag
|= _IOERR
;
4187 /*********************************************************************
4190 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4195 ret
= _fwrite_nolock(ptr
, size
, nmemb
, file
);
4201 /*********************************************************************
4202 * _fwrite_nolock (MSVCRT.@)
4204 size_t CDECL
_fwrite_nolock(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4206 size_t wrcnt
=size
* nmemb
;
4212 if(file
->_cnt
< 0) {
4213 WARN("negative file->_cnt value in %p\n", file
);
4214 file
->_flag
|= _IOERR
;
4216 } else if(file
->_cnt
) {
4217 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
4218 memcpy(file
->_ptr
, ptr
, pcnt
);
4223 ptr
= (const char*)ptr
+ pcnt
;
4224 } else if((file
->_flag
& MSVCRT__NOBUF
)
4225 || ((file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4226 || (!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4230 if(file
->_flag
& MSVCRT__NOBUF
)
4232 else if(!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)))
4233 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4235 bufsiz
= file
->_bufsiz
;
4237 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4239 if(msvcrt_flush_buffer(file
) == EOF
)
4242 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
4243 file
->_flag
|= _IOERR
;
4248 ptr
= (const char*)ptr
+ pcnt
;
4250 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
4254 ptr
= (const char*)ptr
+ 1;
4258 return written
/ size
;
4261 /*********************************************************************
4264 wint_t CDECL
fputwc(wint_t wc
, FILE* file
)
4269 ret
= _fputwc_nolock(wc
, file
);
4275 /*********************************************************************
4276 * _fputwc_nolock (MSVCRT.@)
4278 wint_t CDECL
_fputwc_nolock(wint_t wc
, FILE* file
)
4284 fdinfo
= get_ioinfo_nolock(file
->_file
);
4286 if((fdinfo
->wxflag
&WX_TEXT
) && ioinfo_get_textmode(fdinfo
) == TEXTMODE_ANSI
) {
4287 char buf
[MB_LEN_MAX
];
4290 char_len
= wctomb(buf
, mwc
);
4291 if(char_len
!=-1 && _fwrite_nolock(buf
, char_len
, 1, file
)==1)
4295 }else if(_fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4304 /*********************************************************************
4305 * _fputwchar (MSVCRT.@)
4307 wint_t CDECL
_fputwchar(wint_t wc
)
4309 return fputwc(wc
, stdout
);
4312 /*********************************************************************
4313 * _wfsopen (MSVCRT.@)
4315 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
4318 int open_flags
, stream_flags
, fd
;
4320 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4322 /* map mode string to open() flags. "man fopen" for possibilities. */
4323 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4327 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
4330 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4332 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4339 TRACE(":got (%p)\n",file
);
4340 if (fd
>= 0 && !file
)
4346 /*********************************************************************
4347 * _fsopen (MSVCRT.@)
4349 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
4352 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4354 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4355 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4359 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4362 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4367 ret
= _wfsopen(pathW
, modeW
, share
);
4374 /*********************************************************************
4377 FILE * CDECL
fopen(const char *path
, const char *mode
)
4379 return _fsopen( path
, mode
, _SH_DENYNO
);
4382 /*********************************************************************
4383 * fopen_s (MSVCRT.@)
4385 int CDECL
fopen_s(FILE** pFile
,
4386 const char *filename
, const char *mode
)
4388 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4389 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
4390 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4392 *pFile
= fopen(filename
, mode
);
4399 /*********************************************************************
4400 * _wfopen (MSVCRT.@)
4402 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
4404 return _wfsopen( path
, mode
, _SH_DENYNO
);
4407 /*********************************************************************
4408 * _wfopen_s (MSVCRT.@)
4410 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
4411 const wchar_t *mode
)
4413 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4414 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
4415 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4417 *pFile
= _wfopen(filename
, mode
);
4424 /*********************************************************************
4427 int CDECL
fputc(int c
, FILE* file
)
4432 ret
= _fputc_nolock(c
, file
);
4438 /*********************************************************************
4439 * _fputc_nolock (MSVCRT.@)
4441 int CDECL
_fputc_nolock(int c
, FILE* file
)
4450 res
= msvcrt_flush_buffer(file
);
4451 return res
? res
: c
;
4457 res
= _flsbuf(c
, file
);
4462 /*********************************************************************
4463 * _fputchar (MSVCRT.@)
4465 int CDECL
_fputchar(int c
)
4467 return fputc(c
, stdout
);
4470 /*********************************************************************
4473 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4478 ret
= _fread_nolock(ptr
, size
, nmemb
, file
);
4484 /*********************************************************************
4485 * _fread_nolock (MSVCRT.@)
4487 size_t CDECL
_fread_nolock(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4489 size_t rcnt
=size
* nmemb
;
4496 /* first buffered data */
4498 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4499 memcpy(ptr
, file
->_ptr
, pcnt
);
4504 ptr
= (char*)ptr
+ pcnt
;
4505 } else if(!(file
->_flag
& _IOREAD
)) {
4506 if(file
->_flag
& _IORW
) {
4507 file
->_flag
|= _IOREAD
;
4513 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__NOBUF
| _IOMYBUF
| MSVCRT__USERBUF
)))
4514 msvcrt_alloc_buffer(file
);
4519 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
))) {
4520 i
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
4521 file
->_ptr
= file
->_base
;
4524 if (i
> rcnt
) i
= rcnt
;
4526 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4527 if (i
> 0 && i
< file
->_cnt
) {
4528 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4529 file
->_flag
&= ~_IOEOF
;
4532 memcpy(ptr
, file
->_ptr
, i
);
4536 } else if (rcnt
> INT_MAX
) {
4537 i
= _read(file
->_file
, ptr
, INT_MAX
);
4538 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4539 i
= _read(file
->_file
, ptr
, rcnt
);
4541 i
= _read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4545 ptr
= (char *)ptr
+i
;
4546 /* expose feof condition in the flags
4547 * MFC tests file->_flag for feof, and doesn't call feof())
4549 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4550 file
->_flag
|= _IOEOF
;
4553 file
->_flag
|= _IOERR
;
4563 #if _MSVCR_VER >= 80
4565 /*********************************************************************
4566 * fread_s (MSVCR80.@)
4568 size_t CDECL
fread_s(void *buf
, size_t buf_size
, size_t elem_size
,
4569 size_t count
, FILE *stream
)
4573 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4575 memset(buf
, 0, buf_size
);
4578 if(!elem_size
|| !count
) return 0;
4581 ret
= _fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4582 _unlock_file(stream
);
4587 /*********************************************************************
4588 * _fread_nolock_s (MSVCR80.@)
4590 size_t CDECL
_fread_nolock_s(void *buf
, size_t buf_size
, size_t elem_size
,
4591 size_t count
, FILE *stream
)
4593 size_t bytes_left
, buf_pos
;
4595 TRACE("(%p %Iu %Iu %Iu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4597 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4599 memset(buf
, 0, buf_size
);
4602 if(!elem_size
|| !count
) return 0;
4603 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4604 if(!MSVCRT_CHECK_PMT(SIZE_MAX
/count
>= elem_size
)) return 0;
4606 bytes_left
= elem_size
*count
;
4609 if(stream
->_cnt
> 0) {
4610 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4612 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, ERANGE
)) {
4613 memset(buf
, 0, buf_size
);
4617 _fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4621 int c
= _filbuf(stream
);
4626 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, ERANGE
)) {
4627 memset(buf
, 0, buf_size
);
4631 ((char*)buf
)[buf_pos
++] = c
;
4636 return buf_pos
/elem_size
;
4639 #endif /* _MSVCR_VER >= 80 */
4641 /*********************************************************************
4642 * _wfreopen (MSVCRT.@)
4645 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
4647 int open_flags
, stream_flags
, fd
;
4649 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4655 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4657 else if((fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
)) < 0)
4659 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4669 /*********************************************************************
4670 * _wfreopen_s (MSVCRT.@)
4672 int CDECL
_wfreopen_s(FILE** pFile
,
4673 const wchar_t *path
, const wchar_t *mode
, FILE* file
)
4675 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4676 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return EINVAL
;
4677 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4678 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
4680 *pFile
= _wfreopen(path
, mode
, file
);
4687 /*********************************************************************
4688 * freopen (MSVCRT.@)
4691 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
4694 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4696 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4697 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4703 ret
= _wfreopen(pathW
, modeW
, file
);
4710 /*********************************************************************
4711 * freopen_s (MSVCRT.@)
4713 errno_t CDECL
freopen_s(FILE** pFile
,
4714 const char *path
, const char *mode
, FILE* file
)
4716 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4717 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return EINVAL
;
4718 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4719 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
4721 *pFile
= freopen(path
, mode
, file
);
4728 /*********************************************************************
4729 * fsetpos (MSVCRT.@)
4731 int CDECL
fsetpos(FILE* file
, fpos_t *pos
)
4733 return _fseeki64(file
,*pos
,SEEK_SET
);
4736 /*********************************************************************
4737 * _ftelli64 (MSVCRT.@)
4739 __int64 CDECL
_ftelli64(FILE* file
)
4744 ret
= _ftelli64_nolock(file
);
4750 /*********************************************************************
4751 * _ftelli64_nolock (MSVCRT.@)
4753 __int64 CDECL
_ftelli64_nolock(FILE* file
)
4757 pos
= _telli64(file
->_file
);
4760 if(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) {
4761 if(file
->_flag
& _IOWRT
) {
4762 pos
+= file
->_ptr
- file
->_base
;
4764 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4767 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4771 } else if(!file
->_cnt
) { /* nothing to do */
4772 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4776 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4777 for(i
=0; i
<file
->_cnt
; i
++)
4778 if(file
->_ptr
[i
] == '\n')
4784 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4787 pos
-= file
->_bufsiz
;
4788 pos
+= file
->_ptr
- file
->_base
;
4790 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4791 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4794 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4804 /*********************************************************************
4807 __msvcrt_long CDECL
ftell(FILE* file
)
4809 return _ftelli64(file
);
4812 #if _MSVCR_VER >= 80
4813 /*********************************************************************
4814 * _ftell_nolock (MSVCR80.@)
4816 __msvcrt_long CDECL
_ftell_nolock(FILE* file
)
4818 return _ftelli64_nolock(file
);
4822 /*********************************************************************
4823 * fgetpos (MSVCRT.@)
4825 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
4827 *pos
= _ftelli64(file
);
4833 /*********************************************************************
4836 int CDECL
fputs(const char *s
, FILE* file
)
4838 size_t len
= strlen(s
);
4842 ret
= _fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
4847 /*********************************************************************
4850 int CDECL
fputws(const wchar_t *s
, FILE* file
)
4852 size_t i
, len
= wcslen(s
);
4857 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4858 ret
= _fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
4863 tmp_buf
= add_std_buffer(file
);
4864 for (i
=0; i
<len
; i
++) {
4865 if(_fputwc_nolock(s
[i
], file
) == WEOF
) {
4866 if(tmp_buf
) remove_std_buffer(file
);
4872 if(tmp_buf
) remove_std_buffer(file
);
4877 /*********************************************************************
4878 * getchar (MSVCRT.@)
4880 int CDECL
getchar(void)
4882 return fgetc(stdin
);
4885 /*********************************************************************
4888 int CDECL
getc(FILE* file
)
4893 /*********************************************************************
4894 * gets_s (MSVCR80.@)
4896 char * CDECL
gets_s(char *buf
, size_t len
)
4898 char *buf_start
= buf
;
4901 if (!MSVCRT_CHECK_PMT(buf
!= NULL
)) return NULL
;
4902 if (!MSVCRT_CHECK_PMT(len
!= 0)) return NULL
;
4905 for(cc
= _fgetc_nolock(stdin
);
4906 len
!= 0 && cc
!= EOF
&& cc
!= '\n';
4907 cc
= _fgetc_nolock(stdin
))
4915 _unlock_file(stdin
);
4920 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4924 if ((cc
== EOF
) && (buf_start
== buf
))
4926 TRACE(":nothing read\n");
4931 TRACE("got '%s'\n", buf_start
);
4935 /*********************************************************************
4938 char * CDECL
gets(char *buf
)
4940 return gets_s(buf
, -1);
4943 /*********************************************************************
4946 wchar_t* CDECL
_getws(wchar_t* buf
)
4952 for (cc
= _fgetwc_nolock(stdin
); cc
!= WEOF
&& cc
!= '\n';
4953 cc
= _fgetwc_nolock(stdin
))
4956 *buf
++ = (wchar_t)cc
;
4958 _unlock_file(stdin
);
4960 if ((cc
== WEOF
) && (ws
== buf
))
4962 TRACE(":nothing read\n");
4967 TRACE("got %s\n", debugstr_w(ws
));
4971 /*********************************************************************
4974 int CDECL
putc(int c
, FILE* file
)
4976 return fputc(c
, file
);
4979 /*********************************************************************
4980 * putchar (MSVCRT.@)
4982 int CDECL
putchar(int c
)
4984 return fputc(c
, stdout
);
4987 /*********************************************************************
4990 int CDECL
puts(const char *s
)
4992 size_t len
= strlen(s
);
4996 if(_fwrite_nolock(s
, sizeof(*s
), len
, stdout
) != len
) {
4997 _unlock_file(stdout
);
5001 ret
= _fwrite_nolock("\n",1,1,stdout
) == 1 ? 0 : EOF
;
5002 _unlock_file(stdout
);
5006 /*********************************************************************
5009 int CDECL
_putws(const wchar_t *s
)
5014 ret
= fputws(s
, stdout
);
5016 ret
= _fputwc_nolock('\n', stdout
);
5017 _unlock_file(stdout
);
5018 return ret
>= 0 ? 0 : WEOF
;
5021 /*********************************************************************
5024 int CDECL
remove(const char *path
)
5026 TRACE("(%s)\n", path
);
5027 if (DeleteFileA(path
))
5029 TRACE(":failed (%ld)\n", GetLastError());
5030 msvcrt_set_errno(GetLastError());
5034 /*********************************************************************
5035 * _wremove (MSVCRT.@)
5037 int CDECL
_wremove(const wchar_t *path
)
5039 TRACE("(%s)\n", debugstr_w(path
));
5040 if (DeleteFileW(path
))
5042 TRACE(":failed (%ld)\n", GetLastError());
5043 msvcrt_set_errno(GetLastError());
5047 /*********************************************************************
5050 int CDECL
rename(const char *oldpath
,const char *newpath
)
5052 TRACE(":from %s to %s\n", oldpath
, newpath
);
5053 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
5055 TRACE(":failed (%ld)\n", GetLastError());
5056 msvcrt_set_errno(GetLastError());
5060 /*********************************************************************
5061 * _wrename (MSVCRT.@)
5063 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
5065 TRACE(":from %s to %s\n", debugstr_w(oldpath
), debugstr_w(newpath
));
5066 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
5068 TRACE(":failed (%ld)\n", GetLastError());
5069 msvcrt_set_errno(GetLastError());
5073 /*********************************************************************
5074 * setvbuf (MSVCRT.@)
5076 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
5078 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
5079 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
5080 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
5084 _fflush_nolock(file
);
5085 if(file
->_flag
& _IOMYBUF
)
5087 file
->_flag
&= ~(MSVCRT__NOBUF
| _IOMYBUF
| MSVCRT__USERBUF
);
5090 if(mode
== _IONBF
) {
5091 file
->_flag
|= MSVCRT__NOBUF
;
5092 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
5095 file
->_base
= file
->_ptr
= buf
;
5096 file
->_flag
|= MSVCRT__USERBUF
;
5097 file
->_bufsiz
= size
;
5099 file
->_base
= file
->_ptr
= malloc(size
);
5106 file
->_flag
|= _IOMYBUF
;
5107 file
->_bufsiz
= size
;
5113 /*********************************************************************
5116 void CDECL
setbuf(FILE* file
, char *buf
)
5118 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
5121 static int tmpnam_helper(char *s
, size_t size
, LONG
*tmpnam_unique
, int tmp_max
)
5127 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return EINVAL
;
5137 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
5138 if (digits
+1 > size
) {
5143 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5149 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5150 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5154 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
5155 if (digits
+1 > size
) {
5160 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5163 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
5164 GetLastError() == ERROR_FILE_NOT_FOUND
)
5170 int CDECL
tmpnam_s(char *s
, size_t size
)
5172 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, TMP_MAX_S
);
5175 /*********************************************************************
5178 char * CDECL
tmpnam(char *s
)
5181 thread_data_t
*data
= msvcrt_get_thread_data();
5183 if(!data
->tmpnam_buffer
)
5184 data
->tmpnam_buffer
= malloc(MAX_PATH
);
5186 s
= data
->tmpnam_buffer
;
5189 return tmpnam_helper(s
, -1, &tmpnam_unique
, TMP_MAX
) ? NULL
: s
;
5192 static int wtmpnam_helper(wchar_t *s
, size_t size
, LONG
*tmpnam_unique
, int tmp_max
)
5198 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return EINVAL
;
5208 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5209 if (digits
+1 > size
) {
5214 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5220 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5221 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5225 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5226 if (digits
+1 > size
) {
5231 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5234 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5235 GetLastError() == ERROR_FILE_NOT_FOUND
)
5241 /*********************************************************************
5242 * _wtmpnam_s (MSVCRT.@)
5244 int CDECL
_wtmpnam_s(wchar_t *s
, size_t size
)
5246 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, TMP_MAX_S
);
5249 /*********************************************************************
5250 * _wtmpnam (MSVCRT.@)
5252 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
5255 thread_data_t
*data
= msvcrt_get_thread_data();
5257 if(!data
->wtmpnam_buffer
)
5258 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
5260 s
= data
->wtmpnam_buffer
;
5263 return wtmpnam_helper(s
, -1, &tmpnam_unique
, TMP_MAX
) ? NULL
: s
;
5266 /*********************************************************************
5267 * tmpfile (MSVCRT.@)
5269 FILE* CDECL
tmpfile(void)
5271 char *filename
= _tempnam(",", "t");
5276 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
5277 _S_IREAD
| _S_IWRITE
);
5278 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5280 if (msvcrt_init_fp(file
, fd
, _IORW
) == -1)
5285 else file
->_tmpfname
= _strdup(filename
);
5288 if(fd
!= -1 && !file
)
5295 /*********************************************************************
5296 * tmpfile_s (MSVCRT.@)
5298 int CDECL
tmpfile_s(FILE** file
)
5300 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
5306 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5308 return fwrite(str
, sizeof(char), len
, file
);
5311 static int puts_clbk_file_w(void *file
, int len
, const wchar_t *str
)
5317 if(!(get_ioinfo_nolock(((FILE*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5318 ret
= _fwrite_nolock(str
, sizeof(wchar_t), len
, file
);
5323 for(i
=0; i
<len
; i
++) {
5324 if(_fputwc_nolock(str
[i
], file
) == WEOF
) {
5334 static int vfprintf_helper(DWORD options
, FILE* file
, const char *format
,
5335 _locale_t locale
, va_list valist
)
5337 printf_arg args_ctx
[_ARGMAX
+1];
5341 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5342 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5344 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5345 memset(args_ctx
, 0, sizeof(args_ctx
));
5346 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5348 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5352 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5356 tmp_buf
= add_std_buffer(file
);
5357 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5358 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5359 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5360 if(tmp_buf
) remove_std_buffer(file
);
5366 static int vfwprintf_helper(DWORD options
, FILE* file
, const wchar_t *format
,
5367 _locale_t locale
, va_list valist
)
5369 printf_arg args_ctx
[_ARGMAX
+1];
5373 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5374 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5376 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5377 memset(args_ctx
, 0, sizeof(args_ctx
));
5378 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5380 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5384 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5388 tmp_buf
= add_std_buffer(file
);
5389 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5390 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5391 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5392 if(tmp_buf
) remove_std_buffer(file
);
5398 /*********************************************************************
5399 * _vfprintf_s_l (MSVCRT.@)
5401 int CDECL
_vfprintf_s_l(FILE* file
, const char *format
,
5402 _locale_t locale
, va_list valist
)
5404 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5407 /*********************************************************************
5408 * _vfwprintf_s_l (MSVCRT.@)
5410 int CDECL
_vfwprintf_s_l(FILE* file
, const wchar_t *format
,
5411 _locale_t locale
, va_list valist
)
5413 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5416 /*********************************************************************
5417 * vfprintf (MSVCRT.@)
5419 int CDECL
vfprintf(FILE* file
, const char *format
, va_list valist
)
5421 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5424 /*********************************************************************
5425 * vfprintf_s (MSVCRT.@)
5427 int CDECL
vfprintf_s(FILE* file
, const char *format
, va_list valist
)
5429 return _vfprintf_s_l(file
, format
, NULL
, valist
);
5432 /*********************************************************************
5433 * vfwprintf (MSVCRT.@)
5435 int CDECL
vfwprintf(FILE* file
, const wchar_t *format
, va_list valist
)
5437 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5440 /*********************************************************************
5441 * vfwprintf_s (MSVCRT.@)
5443 int CDECL
vfwprintf_s(FILE* file
, const wchar_t *format
, va_list valist
)
5445 return _vfwprintf_s_l(file
, format
, NULL
, valist
);
5448 #if _MSVCR_VER >= 140
5450 /*********************************************************************
5451 * __stdio_common_vfprintf (UCRTBASE.@)
5453 int CDECL
_stdio_common_vfprintf(unsigned __int64 options
, FILE *file
, const char *format
,
5454 _locale_t locale
, va_list valist
)
5456 if (options
& ~UCRTBASE_PRINTF_MASK
)
5457 FIXME("options %#I64x not handled\n", options
);
5459 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5462 /*********************************************************************
5463 * __stdio_common_vfprintf_p (UCRTBASE.@)
5465 int CDECL
__stdio_common_vfprintf_p(unsigned __int64 options
, FILE *file
, const char *format
,
5466 _locale_t locale
, va_list valist
)
5468 if (options
& ~UCRTBASE_PRINTF_MASK
)
5469 FIXME("options %#I64x not handled\n", options
);
5471 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_POSITIONAL_PARAMS
5472 | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5476 /*********************************************************************
5477 * __stdio_common_vfprintf_s (UCRTBASE.@)
5479 int CDECL
__stdio_common_vfprintf_s(unsigned __int64 options
, FILE *file
, const char *format
,
5480 _locale_t locale
, va_list valist
)
5482 if (options
& ~UCRTBASE_PRINTF_MASK
)
5483 FIXME("options %#I64x not handled\n", options
);
5485 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5486 file
, format
, locale
, valist
);
5489 /*********************************************************************
5490 * __stdio_common_vfwprintf (UCRTBASE.@)
5492 int CDECL
__stdio_common_vfwprintf(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5493 _locale_t locale
, va_list valist
)
5495 if (options
& ~UCRTBASE_PRINTF_MASK
)
5496 FIXME("options %#I64x not handled\n", options
);
5498 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5501 /*********************************************************************
5502 * __stdio_common_vfwprintf_p (UCRTBASE.@)
5504 int CDECL
__stdio_common_vfwprintf_p(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5505 _locale_t locale
, va_list valist
)
5507 if (options
& ~UCRTBASE_PRINTF_MASK
)
5508 FIXME("options %#I64x not handled\n", options
);
5510 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_POSITIONAL_PARAMS
5511 | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5515 /*********************************************************************
5516 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5518 int CDECL
__stdio_common_vfwprintf_s(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5519 _locale_t locale
, va_list valist
)
5521 if (options
& ~UCRTBASE_PRINTF_MASK
)
5522 FIXME("options %#I64x not handled\n", options
);
5524 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5525 file
, format
, locale
, valist
);
5528 #endif /* _MSVCR_VER >= 140 */
5530 /*********************************************************************
5531 * _vfprintf_l (MSVCRT.@)
5533 int CDECL
_vfprintf_l(FILE* file
, const char *format
,
5534 _locale_t locale
, va_list valist
)
5536 return vfprintf_helper(0, file
, format
, locale
, valist
);
5539 /*********************************************************************
5540 * _vfwprintf_l (MSVCRT.@)
5542 int CDECL
_vfwprintf_l(FILE* file
, const wchar_t *format
,
5543 _locale_t locale
, va_list valist
)
5545 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5548 /*********************************************************************
5549 * _vfprintf_p_l (MSVCRT.@)
5551 int CDECL
_vfprintf_p_l(FILE* file
, const char *format
,
5552 _locale_t locale
, va_list valist
)
5554 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5555 file
, format
, locale
, valist
);
5558 /*********************************************************************
5559 * _vfprintf_p (MSVCRT.@)
5561 int CDECL
_vfprintf_p(FILE* file
, const char *format
, va_list valist
)
5563 return _vfprintf_p_l(file
, format
, NULL
, valist
);
5566 /*********************************************************************
5567 * _vfwprintf_p_l (MSVCRT.@)
5569 int CDECL
_vfwprintf_p_l(FILE* file
, const wchar_t *format
,
5570 _locale_t locale
, va_list valist
)
5572 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5573 file
, format
, locale
, valist
);
5576 /*********************************************************************
5577 * _vfwprintf_p (MSVCRT.@)
5579 int CDECL
_vfwprintf_p(FILE* file
, const wchar_t *format
, va_list valist
)
5581 return _vfwprintf_p_l(file
, format
, NULL
, valist
);
5584 /*********************************************************************
5585 * vprintf (MSVCRT.@)
5587 int CDECL
vprintf(const char *format
, va_list valist
)
5589 return vfprintf(stdout
,format
,valist
);
5592 /*********************************************************************
5593 * vprintf_s (MSVCRT.@)
5595 int CDECL
vprintf_s(const char *format
, va_list valist
)
5597 return vfprintf_s(stdout
,format
,valist
);
5600 /*********************************************************************
5601 * vwprintf (MSVCRT.@)
5603 int CDECL
vwprintf(const wchar_t *format
, va_list valist
)
5605 return vfwprintf(stdout
,format
,valist
);
5608 /*********************************************************************
5609 * vwprintf_s (MSVCRT.@)
5611 int CDECL
vwprintf_s(const wchar_t *format
, va_list valist
)
5613 return vfwprintf_s(stdout
,format
,valist
);
5616 /*********************************************************************
5617 * fprintf (MSVCRT.@)
5619 int WINAPIV
fprintf(FILE* file
, const char *format
, ...)
5623 va_start(valist
, format
);
5624 res
= vfprintf(file
, format
, valist
);
5629 /*********************************************************************
5630 * fprintf_s (MSVCRT.@)
5632 int WINAPIV
fprintf_s(FILE* file
, const char *format
, ...)
5636 va_start(valist
, format
);
5637 res
= vfprintf_s(file
, format
, valist
);
5642 /*********************************************************************
5643 * _fprintf_l (MSVCRT.@)
5645 int WINAPIV
_fprintf_l(FILE* file
, const char *format
, _locale_t locale
, ...)
5649 va_start(valist
, locale
);
5650 res
= _vfprintf_l(file
, format
, locale
, valist
);
5656 /*********************************************************************
5657 * _fprintf_p (MSVCRT.@)
5659 int WINAPIV
_fprintf_p(FILE* file
, const char *format
, ...)
5663 va_start(valist
, format
);
5664 res
= _vfprintf_p_l(file
, format
, NULL
, valist
);
5669 /*********************************************************************
5670 * _fprintf_p_l (MSVCRT.@)
5672 int WINAPIV
_fprintf_p_l(FILE* file
, const char *format
, _locale_t locale
, ...)
5676 va_start(valist
, locale
);
5677 res
= _vfprintf_p_l(file
, format
, locale
, valist
);
5682 /*********************************************************************
5683 * _fprintf_s_l (MSVCRT.@)
5685 int WINAPIV
_fprintf_s_l(FILE* file
, const char *format
, _locale_t locale
, ...)
5689 va_start(valist
, locale
);
5690 res
= _vfprintf_s_l(file
, format
, locale
, valist
);
5695 /*********************************************************************
5696 * fwprintf (MSVCRT.@)
5698 int WINAPIV
fwprintf(FILE* file
, const wchar_t *format
, ...)
5702 va_start(valist
, format
);
5703 res
= vfwprintf(file
, format
, valist
);
5708 /*********************************************************************
5709 * fwprintf_s (MSVCRT.@)
5711 int WINAPIV
fwprintf_s(FILE* file
, const wchar_t *format
, ...)
5715 va_start(valist
, format
);
5716 res
= vfwprintf_s(file
, format
, valist
);
5721 /*********************************************************************
5722 * _fwprintf_l (MSVCRT.@)
5724 int WINAPIV
_fwprintf_l(FILE* file
, const wchar_t *format
, _locale_t locale
, ...)
5728 va_start(valist
, locale
);
5729 res
= _vfwprintf_l(file
, format
, locale
, valist
);
5734 /*********************************************************************
5735 * _fwprintf_p (MSVCRT.@)
5737 int WINAPIV
_fwprintf_p(FILE* file
, const wchar_t *format
, ...)
5741 va_start(valist
, format
);
5742 res
= _vfwprintf_p_l(file
, format
, NULL
, valist
);
5747 /*********************************************************************
5748 * _fwprintf_p_l (MSVCRT.@)
5750 int WINAPIV
_fwprintf_p_l(FILE* file
, const wchar_t *format
, _locale_t locale
, ...)
5754 va_start(valist
, locale
);
5755 res
= _vfwprintf_p_l(file
, format
, locale
, valist
);
5760 /*********************************************************************
5761 * _fwprintf_s_l (MSVCRT.@)
5763 int WINAPIV
_fwprintf_s_l(FILE* file
, const wchar_t *format
, _locale_t locale
, ...)
5767 va_start(valist
, locale
);
5768 res
= _vfwprintf_s_l(file
, format
, locale
, valist
);
5773 /*********************************************************************
5776 int WINAPIV
printf(const char *format
, ...)
5780 va_start(valist
, format
);
5781 res
= vfprintf(stdout
, format
, valist
);
5786 /*********************************************************************
5787 * printf_s (MSVCRT.@)
5789 int WINAPIV
printf_s(const char *format
, ...)
5793 va_start(valist
, format
);
5794 res
= vprintf_s(format
, valist
);
5799 /*********************************************************************
5802 int CDECL
ungetc(int c
, FILE * file
)
5806 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
5809 ret
= _ungetc_nolock(c
, file
);
5815 /*********************************************************************
5816 * _ungetc_nolock (MSVCRT.@)
5818 int CDECL
_ungetc_nolock(int c
, FILE * file
)
5820 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
5822 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
5823 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
5826 if((!(file
->_flag
& (MSVCRT__NOBUF
| _IOMYBUF
| MSVCRT__USERBUF
))
5827 && msvcrt_alloc_buffer(file
))
5828 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5831 if(file
->_ptr
>file
->_base
) {
5833 if(file
->_flag
& _IOSTRG
) {
5834 if(*file
->_ptr
!= c
) {
5842 file
->_flag
&= ~(_IOERR
| _IOEOF
);
5843 file
->_flag
|= _IOREAD
;
5850 /*********************************************************************
5851 * ungetwc (MSVCRT.@)
5853 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
5857 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5860 ret
= _ungetwc_nolock(wc
, file
);
5866 /*********************************************************************
5867 * _ungetwc_nolock (MSVCRT.@)
5869 wint_t CDECL
_ungetwc_nolock(wint_t wc
, FILE * file
)
5873 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5877 if(ioinfo_get_textmode(get_ioinfo_nolock(file
->_file
)) != TEXTMODE_ANSI
5878 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5879 unsigned char * pp
= (unsigned char *)&mwc
;
5882 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
5883 if(pp
[i
] != _ungetc_nolock(pp
[i
],file
))
5887 char mbs
[MB_LEN_MAX
];
5890 len
= wctomb(mbs
, mwc
);
5894 for(len
--; len
>=0; len
--) {
5895 if(mbs
[len
] != _ungetc_nolock(mbs
[len
], file
))
5903 /*********************************************************************
5904 * wprintf (MSVCRT.@)
5906 int WINAPIV
wprintf(const wchar_t *format
, ...)
5910 va_start(valist
, format
);
5911 res
= vwprintf(format
, valist
);
5916 /*********************************************************************
5917 * wprintf_s (MSVCRT.@)
5919 int WINAPIV
wprintf_s(const wchar_t *format
, ...)
5923 va_start(valist
, format
);
5924 res
= vwprintf_s(format
, valist
);
5929 /*********************************************************************
5930 * _getmaxstdio (MSVCRT.@)
5932 int CDECL
_getmaxstdio(void)
5934 return MSVCRT_max_streams
;
5937 /*********************************************************************
5938 * _setmaxstdio (MSVCRT.@)
5940 int CDECL
_setmaxstdio(int newmax
)
5942 TRACE("%d\n", newmax
);
5944 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5947 MSVCRT_max_streams
= newmax
;
5948 return MSVCRT_max_streams
;
5951 #if _MSVCR_VER >= 140
5952 /*********************************************************************
5953 * _get_stream_buffer_pointers (UCRTBASE.@)
5955 int CDECL
_get_stream_buffer_pointers(FILE *file
, char*** base
,
5956 char*** ptr
, int** count
)
5959 *base
= &file
->_base
;
5963 *count
= &file
->_cnt
;