2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include <sys/locking.h>
33 #include <sys/types.h>
34 #include <sys/utime.h>
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
69 /* for stat mode, permissions apply to all,owner and group */
70 #define ALL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
71 #define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
72 #define ALL_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
74 /* _access() bit flags FIXME: incomplete */
75 #define MSVCRT_W_OK 0x02
76 #define MSVCRT_R_OK 0x04
78 /* values for wxflag in file descriptor */
81 #define WX_READNL 0x04 /* read started with \n */
83 #define WX_DONTINHERIT 0x10
84 #define WX_APPEND 0x20
88 /* values for exflag - it's used differently in msvcr90.dll*/
91 #define EF_CRIT_INIT 0x04
92 #define EF_UNK_UNICODE 0x08
94 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
95 static char utf16_bom
[2] = { 0xff, 0xfe };
97 /* FIXME: this should be allocated dynamically */
98 #define MSVCRT_MAX_FILES 2048
99 #define MSVCRT_FD_BLOCK_SIZE 32
101 #define MSVCRT_INTERNAL_BUFSIZ 4096
103 /* ioinfo structure size is different in msvcrXX.dll's */
106 unsigned char wxflag
;
109 CRITICAL_SECTION crit
;
115 BOOL utf8translations
;
123 /*********************************************************************
124 * __pioinfo (MSVCRT.@)
125 * array of pointers to ioinfo arrays [32]
127 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
129 /*********************************************************************
130 * __badioinfo (MSVCRT.@)
132 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
136 CRITICAL_SECTION crit
;
139 FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
140 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
141 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
143 /* INTERNAL: process umask */
144 static int MSVCRT_umask
= 0;
146 /* INTERNAL: static data for tmpnam and _wtmpname functions */
147 static int tmpnam_unique
;
148 static int tmpnam_s_unique
;
150 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
151 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
152 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
153 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
155 #define TOUL(x) (ULONGLONG)(x)
156 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
157 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
158 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
159 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
161 /* This critical section protects the MSVCRT_fstreams table
162 * and MSVCRT_stream_idx from race conditions. It also
163 * protects fd critical sections creation code.
165 static CRITICAL_SECTION MSVCRT_file_cs
;
166 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
168 0, 0, &MSVCRT_file_cs
,
169 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
170 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
172 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
173 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
174 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
176 static void msvcrt_stat64_to_stat(const struct _stat64
*buf64
, struct _stat
*buf
)
178 buf
->st_dev
= buf64
->st_dev
;
179 buf
->st_ino
= buf64
->st_ino
;
180 buf
->st_mode
= buf64
->st_mode
;
181 buf
->st_nlink
= buf64
->st_nlink
;
182 buf
->st_uid
= buf64
->st_uid
;
183 buf
->st_gid
= buf64
->st_gid
;
184 buf
->st_rdev
= buf64
->st_rdev
;
185 buf
->st_size
= buf64
->st_size
;
186 buf
->st_atime
= buf64
->st_atime
;
187 buf
->st_mtime
= buf64
->st_mtime
;
188 buf
->st_ctime
= buf64
->st_ctime
;
191 static void msvcrt_stat64_to_stati64(const struct _stat64
*buf64
, struct _stati64
*buf
)
193 buf
->st_dev
= buf64
->st_dev
;
194 buf
->st_ino
= buf64
->st_ino
;
195 buf
->st_mode
= buf64
->st_mode
;
196 buf
->st_nlink
= buf64
->st_nlink
;
197 buf
->st_uid
= buf64
->st_uid
;
198 buf
->st_gid
= buf64
->st_gid
;
199 buf
->st_rdev
= buf64
->st_rdev
;
200 buf
->st_size
= buf64
->st_size
;
201 buf
->st_atime
= buf64
->st_atime
;
202 buf
->st_mtime
= buf64
->st_mtime
;
203 buf
->st_ctime
= buf64
->st_ctime
;
206 static void msvcrt_stat64_to_stat32(const struct _stat64
*buf64
, struct _stat32
*buf
)
208 buf
->st_dev
= buf64
->st_dev
;
209 buf
->st_ino
= buf64
->st_ino
;
210 buf
->st_mode
= buf64
->st_mode
;
211 buf
->st_nlink
= buf64
->st_nlink
;
212 buf
->st_uid
= buf64
->st_uid
;
213 buf
->st_gid
= buf64
->st_gid
;
214 buf
->st_rdev
= buf64
->st_rdev
;
215 buf
->st_size
= buf64
->st_size
;
216 buf
->st_atime
= buf64
->st_atime
;
217 buf
->st_mtime
= buf64
->st_mtime
;
218 buf
->st_ctime
= buf64
->st_ctime
;
221 static void msvcrt_stat64_to_stat64i32(const struct _stat64
*buf64
, struct _stat64i32
*buf
)
223 buf
->st_dev
= buf64
->st_dev
;
224 buf
->st_ino
= buf64
->st_ino
;
225 buf
->st_mode
= buf64
->st_mode
;
226 buf
->st_nlink
= buf64
->st_nlink
;
227 buf
->st_uid
= buf64
->st_uid
;
228 buf
->st_gid
= buf64
->st_gid
;
229 buf
->st_rdev
= buf64
->st_rdev
;
230 buf
->st_size
= buf64
->st_size
;
231 buf
->st_atime
= buf64
->st_atime
;
232 buf
->st_mtime
= buf64
->st_mtime
;
233 buf
->st_ctime
= buf64
->st_ctime
;
236 static void msvcrt_stat64_to_stat32i64(const struct _stat64
*buf64
, struct _stat32i64
*buf
)
238 buf
->st_dev
= buf64
->st_dev
;
239 buf
->st_ino
= buf64
->st_ino
;
240 buf
->st_mode
= buf64
->st_mode
;
241 buf
->st_nlink
= buf64
->st_nlink
;
242 buf
->st_uid
= buf64
->st_uid
;
243 buf
->st_gid
= buf64
->st_gid
;
244 buf
->st_rdev
= buf64
->st_rdev
;
245 buf
->st_size
= buf64
->st_size
;
246 buf
->st_atime
= buf64
->st_atime
;
247 buf
->st_mtime
= buf64
->st_mtime
;
248 buf
->st_ctime
= buf64
->st_ctime
;
251 static void time_to_filetime( __time64_t time
, FILETIME
*ft
)
253 /* 1601 to 1970 is 369 years plus 89 leap days */
254 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
256 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
257 ft
->dwHighDateTime
= ticks
>> 32;
258 ft
->dwLowDateTime
= ticks
;
261 static inline ioinfo
* get_ioinfo_nolock(int fd
)
264 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
265 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
267 return &MSVCRT___badioinfo
;
269 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
272 static inline void init_ioinfo_cs(ioinfo
*info
)
274 if(!(info
->exflag
& EF_CRIT_INIT
)) {
276 if(!(info
->exflag
& EF_CRIT_INIT
)) {
277 InitializeCriticalSection(&info
->crit
);
278 info
->exflag
|= EF_CRIT_INIT
;
284 static inline ioinfo
* get_ioinfo(int fd
)
286 ioinfo
*ret
= get_ioinfo_nolock(fd
);
287 if(ret
== &MSVCRT___badioinfo
)
290 EnterCriticalSection(&ret
->crit
);
294 static inline BOOL
alloc_pioinfo_block(int fd
)
299 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
305 block
= calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
308 WARN(":out of memory!\n");
312 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
313 block
[i
].handle
= INVALID_HANDLE_VALUE
;
314 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
319 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
323 ret
= get_ioinfo(fd
);
324 if(ret
!= &MSVCRT___badioinfo
)
327 if(!alloc_pioinfo_block(fd
))
328 return &MSVCRT___badioinfo
;
330 return get_ioinfo(fd
);
333 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
338 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
340 ioinfo
*info
= get_ioinfo_nolock(i
);
342 if(info
== &MSVCRT___badioinfo
)
344 if(!alloc_pioinfo_block(i
))
345 return &MSVCRT___badioinfo
;
346 info
= get_ioinfo_nolock(i
);
349 init_ioinfo_cs(info
);
350 if(TryEnterCriticalSection(&info
->crit
))
352 if(info
->handle
== INVALID_HANDLE_VALUE
)
357 LeaveCriticalSection(&info
->crit
);
361 WARN(":files exhausted!\n");
363 return &MSVCRT___badioinfo
;
366 static inline void release_ioinfo(ioinfo
*info
)
368 if(info
!=&MSVCRT___badioinfo
&& info
->exflag
& EF_CRIT_INIT
)
369 LeaveCriticalSection(&info
->crit
);
372 static inline FILE* msvcrt_get_file(int i
)
376 if(i
>= MSVCRT_max_streams
)
380 return &MSVCRT__iob
[i
];
382 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
384 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
385 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
386 ERR("out of memory\n");
391 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
393 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
398 /* INTERNAL: free a file entry fd */
399 static void msvcrt_free_fd(int fd
)
401 ioinfo
*fdinfo
= get_ioinfo(fd
);
403 if(fdinfo
!= &MSVCRT___badioinfo
)
405 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
408 TRACE(":fd (%d) freed\n",fd
);
415 SetStdHandle(STD_INPUT_HANDLE
, 0);
418 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
421 SetStdHandle(STD_ERROR_HANDLE
, 0);
425 release_ioinfo(fdinfo
);
428 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
430 fdinfo
->handle
= hand
;
431 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
432 fdinfo
->lookahead
[0] = '\n';
433 fdinfo
->lookahead
[1] = '\n';
434 fdinfo
->lookahead
[2] = '\n';
435 fdinfo
->exflag
&= EF_CRIT_INIT
;
437 if (hand
== MSVCRT_NO_CONSOLE
) hand
= 0;
438 switch (fdinfo
-MSVCRT___pioinfo
[0])
440 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
441 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
442 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
446 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
447 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
450 ioinfo
*info
= get_ioinfo_alloc(&fd
);
452 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
454 if(info
== &MSVCRT___badioinfo
)
457 msvcrt_set_fd(info
, hand
, flag
);
458 release_ioinfo(info
);
462 /* INTERNAL: Allocate a FILE* for an fd slot */
463 /* caller must hold the files lock */
464 static FILE* msvcrt_alloc_fp(void)
469 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
471 file
= msvcrt_get_file(i
);
475 if (file
->_flag
== 0)
477 if (i
== MSVCRT_stream_idx
)
479 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
481 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
482 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
493 /* INTERNAL: initialize a FILE* from an open fd */
494 static int msvcrt_init_fp(FILE* file
, int fd
, unsigned stream_flags
)
496 TRACE(":fd (%d) allocating FILE*\n",fd
);
497 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
499 WARN(":invalid fd %d\n",fd
);
504 file
->_ptr
= file
->_base
= NULL
;
507 file
->_flag
= stream_flags
;
508 file
->_tmpfname
= NULL
;
510 TRACE(":got FILE* (%p)\n",file
);
514 /* INTERNAL: Create an inheritance data block (for spawned process)
515 * The inheritance block is made of:
516 * 00 int nb of file descriptor (NBFD)
517 * 04 char file flags (wxflag): repeated for each fd
518 * 4+NBFD HANDLE file handle: repeated for each fd
520 BOOL
msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
527 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
528 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
532 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
533 *block
= calloc(1, *size
);
539 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
540 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
542 *(unsigned*)*block
= last_fd
;
543 for (fd
= 0; fd
< last_fd
; fd
++)
545 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
546 fdinfo
= get_ioinfo_nolock(fd
);
547 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
549 *wxflag_ptr
= fdinfo
->wxflag
;
550 *handle_ptr
= fdinfo
->handle
;
555 *handle_ptr
= INVALID_HANDLE_VALUE
;
557 wxflag_ptr
++; handle_ptr
++;
562 /* INTERNAL: Set up all file descriptors,
563 * as well as default streams (stdin, stderr and stdout)
565 void msvcrt_init_io(void)
571 GetStartupInfoA(&si
);
572 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
578 count
= *(unsigned*)si
.lpReserved2
;
579 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
580 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
582 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
583 count
= min(count
, MSVCRT_MAX_FILES
);
584 for (i
= 0; i
< count
; i
++)
586 if ((*wxflag_ptr
& WX_OPEN
) && GetFileType(*handle_ptr
) != FILE_TYPE_UNKNOWN
)
588 fdinfo
= get_ioinfo_alloc_fd(i
);
589 if(fdinfo
!= &MSVCRT___badioinfo
)
590 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
591 release_ioinfo(fdinfo
);
594 wxflag_ptr
++; handle_ptr
++;
598 fdinfo
= get_ioinfo_alloc_fd(STDIN_FILENO
);
599 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
600 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
601 DWORD flags
= WX_OPEN
| WX_TEXT
;
602 DWORD type
= GetFileType(h
);
604 if (type
== FILE_TYPE_UNKNOWN
) {
605 h
= MSVCRT_NO_CONSOLE
;
607 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
609 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
613 msvcrt_set_fd(fdinfo
, h
, flags
);
615 release_ioinfo(fdinfo
);
617 fdinfo
= get_ioinfo_alloc_fd(STDOUT_FILENO
);
618 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
619 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
620 DWORD flags
= WX_OPEN
| WX_TEXT
;
621 DWORD type
= GetFileType(h
);
623 if (type
== FILE_TYPE_UNKNOWN
) {
624 h
= MSVCRT_NO_CONSOLE
;
626 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
628 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
632 msvcrt_set_fd(fdinfo
, h
, flags
);
634 release_ioinfo(fdinfo
);
636 fdinfo
= get_ioinfo_alloc_fd(STDERR_FILENO
);
637 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
638 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
639 DWORD flags
= WX_OPEN
| WX_TEXT
;
640 DWORD type
= GetFileType(h
);
642 if (type
== FILE_TYPE_UNKNOWN
) {
643 h
= MSVCRT_NO_CONSOLE
;
645 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
647 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
651 msvcrt_set_fd(fdinfo
, h
, flags
);
653 release_ioinfo(fdinfo
);
655 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO
)->handle
,
656 get_ioinfo_nolock(STDOUT_FILENO
)->handle
,
657 get_ioinfo_nolock(STDERR_FILENO
)->handle
);
659 memset(MSVCRT__iob
,0,3*sizeof(FILE));
660 for (i
= 0; i
< 3; i
++)
662 /* FILE structs for stdin/out/err are static and never deleted */
663 MSVCRT__iob
[i
]._file
= get_ioinfo_nolock(i
)->handle
== MSVCRT_NO_CONSOLE
?
664 MSVCRT_NO_CONSOLE_FD
: i
;
665 MSVCRT__iob
[i
]._tmpfname
= NULL
;
666 MSVCRT__iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
668 MSVCRT_stream_idx
= 3;
671 /* INTERNAL: Flush stdio file buffer */
672 static int msvcrt_flush_buffer(FILE* file
)
676 if((file
->_flag
& (_IOREAD
|_IOWRT
)) == _IOWRT
&&
677 file
->_flag
& (_IOMYBUF
|MSVCRT__USERBUF
)) {
678 int cnt
=file
->_ptr
-file
->_base
;
679 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
680 file
->_flag
|= _IOERR
;
682 } else if(file
->_flag
& _IORW
) {
683 file
->_flag
&= ~_IOWRT
;
687 file
->_ptr
=file
->_base
;
692 /*********************************************************************
695 int CDECL
_isatty(int fd
)
697 TRACE(":fd (%d)\n",fd
);
699 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
702 /* INTERNAL: Allocate stdio file buffer */
703 static BOOL
msvcrt_alloc_buffer(FILE* file
)
705 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
706 && _isatty(file
->_file
))
709 file
->_base
= calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
711 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
712 file
->_flag
|= _IOMYBUF
;
714 file
->_base
= (char*)(&file
->_charbuf
);
716 file
->_flag
|= _IONBF
;
718 file
->_ptr
= file
->_base
;
723 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
724 static BOOL
add_std_buffer(FILE *file
)
726 static char buffers
[2][BUFSIZ
];
728 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
729 || (file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
))
730 || !_isatty(file
->_file
))
733 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
734 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
735 file
->_flag
|= MSVCRT__USERBUF
;
739 /* INTERNAL: Removes temporary buffer from stdout or stderr */
740 /* Only call this function when add_std_buffer returned TRUE */
741 static void remove_std_buffer(FILE *file
)
743 msvcrt_flush_buffer(file
);
744 file
->_ptr
= file
->_base
= NULL
;
745 file
->_bufsiz
= file
->_cnt
= 0;
746 file
->_flag
&= ~MSVCRT__USERBUF
;
749 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
750 static int msvcrt_int_to_base32(int num
, char *str
)
765 *p
= (num
& 31) + '0';
767 *p
+= ('a' - '0' - 10);
774 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
775 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
790 *p
= (num
& 31) + '0';
792 *p
+= ('a' - '0' - 10);
799 /*********************************************************************
800 * __iob_func (MSVCRT.@)
803 FILE * CDECL
__iob_func(void)
805 return &MSVCRT__iob
[0];
808 #if _MSVCR_VER >= 140
809 /*********************************************************************
810 * __acrt_iob_func(UCRTBASE.@)
812 FILE * CDECL
__acrt_iob_func(unsigned idx
)
814 return &MSVCRT__iob
[idx
];
818 /*********************************************************************
821 int CDECL
_access(const char *filename
, int mode
)
823 DWORD attr
= GetFileAttributesA(filename
);
825 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
827 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
829 msvcrt_set_errno(GetLastError());
832 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
834 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
840 /*********************************************************************
841 * _access_s (MSVCRT.@)
843 int CDECL
_access_s(const char *filename
, int mode
)
845 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *_errno();
846 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *_errno();
848 if (_access(filename
, mode
) == -1)
853 /*********************************************************************
854 * _waccess (MSVCRT.@)
856 int CDECL
_waccess(const wchar_t *filename
, int mode
)
858 DWORD attr
= GetFileAttributesW(filename
);
860 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
862 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
864 msvcrt_set_errno(GetLastError());
867 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
869 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
875 /*********************************************************************
876 * _waccess_s (MSVCRT.@)
878 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
880 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *_errno();
881 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *_errno();
883 if (_waccess(filename
, mode
) == -1)
888 /*********************************************************************
891 int CDECL
_chmod(const char *path
, int flags
)
893 DWORD oldFlags
= GetFileAttributesA(path
);
895 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
897 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
898 oldFlags
| FILE_ATTRIBUTE_READONLY
;
900 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
903 msvcrt_set_errno(GetLastError());
907 /*********************************************************************
910 int CDECL
_wchmod(const wchar_t *path
, int flags
)
912 DWORD oldFlags
= GetFileAttributesW(path
);
914 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
916 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
917 oldFlags
| FILE_ATTRIBUTE_READONLY
;
919 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
922 msvcrt_set_errno(GetLastError());
926 /*********************************************************************
929 int CDECL
_unlink(const char *path
)
931 TRACE("%s\n",debugstr_a(path
));
932 if(DeleteFileA(path
))
934 TRACE("failed (%d)\n",GetLastError());
935 msvcrt_set_errno(GetLastError());
939 /*********************************************************************
940 * _wunlink (MSVCRT.@)
942 int CDECL
_wunlink(const wchar_t *path
)
944 TRACE("(%s)\n",debugstr_w(path
));
945 if(DeleteFileW(path
))
947 TRACE("failed (%d)\n",GetLastError());
948 msvcrt_set_errno(GetLastError());
952 /*********************************************************************
955 int CDECL
_commit(int fd
)
957 ioinfo
*info
= get_ioinfo(fd
);
960 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
962 if (info
->handle
== INVALID_HANDLE_VALUE
)
964 else if (!FlushFileBuffers(info
->handle
))
966 if (GetLastError() == ERROR_INVALID_HANDLE
)
968 /* FlushFileBuffers fails for console handles
969 * so we ignore this error.
975 TRACE(":failed-last error (%d)\n",GetLastError());
976 msvcrt_set_errno(GetLastError());
986 release_ioinfo(info
);
990 /* INTERNAL: Flush all stream buffer */
991 static int msvcrt_flush_all_buffers(int mask
)
993 int i
, num_flushed
= 0;
997 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
998 file
= msvcrt_get_file(i
);
1002 if(file
->_flag
& mask
) {
1010 TRACE(":flushed (%d) handles\n",num_flushed
);
1014 /*********************************************************************
1015 * _flushall (MSVCRT.@)
1017 int CDECL
_flushall(void)
1019 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
1022 /*********************************************************************
1025 int CDECL
fflush(FILE* file
)
1030 msvcrt_flush_all_buffers(_IOWRT
);
1034 ret
= _fflush_nolock(file
);
1041 /*********************************************************************
1042 * _fflush_nolock (MSVCRT.@)
1044 int CDECL
_fflush_nolock(FILE* file
)
1049 msvcrt_flush_all_buffers(_IOWRT
);
1053 res
= msvcrt_flush_buffer(file
);
1054 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1055 res
= _commit(file
->_file
) ? EOF
: 0;
1059 /*********************************************************************
1062 int CDECL
_close(int fd
)
1064 ioinfo
*info
= get_ioinfo(fd
);
1067 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1069 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1072 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, EBADF
)) {
1074 } else if (fd
== STDOUT_FILENO
&&
1075 info
->handle
== get_ioinfo_nolock(STDERR_FILENO
)->handle
) {
1078 } else if (fd
== STDERR_FILENO
&&
1079 info
->handle
== get_ioinfo_nolock(STDOUT_FILENO
)->handle
) {
1083 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1086 WARN(":failed-last error (%d)\n",GetLastError());
1087 msvcrt_set_errno(GetLastError());
1090 release_ioinfo(info
);
1094 /*********************************************************************
1097 * MSDN isn't clear on this point, but the remarks for _pipe
1098 * indicate file descriptors duplicated with _dup and _dup2 are always
1101 int CDECL
_dup2(int od
, int nd
)
1103 ioinfo
*info_od
, *info_nd
;
1106 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1110 info_od
= get_ioinfo(od
);
1111 info_nd
= get_ioinfo_alloc_fd(nd
);
1115 info_nd
= get_ioinfo_alloc_fd(nd
);
1116 info_od
= get_ioinfo(od
);
1119 if (info_nd
== &MSVCRT___badioinfo
)
1123 else if (info_od
->wxflag
& WX_OPEN
)
1127 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1128 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1130 int wxflag
= info_od
->wxflag
& ~WX_DONTINHERIT
;
1132 if (info_nd
->wxflag
& WX_OPEN
)
1135 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1136 /* _dup2 returns 0, not nd, on success */
1142 msvcrt_set_errno(GetLastError());
1151 release_ioinfo(info_od
);
1152 release_ioinfo(info_nd
);
1156 /*********************************************************************
1159 int CDECL
_dup(int od
)
1162 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1164 if (_dup2(od
, fd
) == 0)
1168 release_ioinfo(info
);
1172 /*********************************************************************
1175 int CDECL
_eof(int fd
)
1177 ioinfo
*info
= get_ioinfo(fd
);
1178 DWORD curpos
,endpos
;
1179 LONG hcurpos
,hendpos
;
1181 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1183 if (info
->handle
== INVALID_HANDLE_VALUE
)
1185 release_ioinfo(info
);
1189 if (info
->wxflag
& WX_ATEOF
)
1191 release_ioinfo(info
);
1195 /* Otherwise we do it the hard way */
1196 hcurpos
= hendpos
= 0;
1197 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1198 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1200 if (curpos
== endpos
&& hcurpos
== hendpos
)
1202 /* FIXME: shouldn't WX_ATEOF be set here? */
1203 release_ioinfo(info
);
1207 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1208 release_ioinfo(info
);
1212 /*********************************************************************
1213 * _fcloseall (MSVCRT.@)
1215 int CDECL
_fcloseall(void)
1217 int num_closed
= 0, i
;
1221 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1222 file
= msvcrt_get_file(i
);
1224 if (file
->_flag
&& !fclose(file
))
1229 TRACE(":closed (%d) handles\n",num_closed
);
1233 /* free everything on process exit */
1234 void msvcrt_free_io(void)
1242 for(i
=0; i
<ARRAY_SIZE(MSVCRT___pioinfo
); i
++)
1244 if(!MSVCRT___pioinfo
[i
])
1247 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1249 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1250 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1252 free(MSVCRT___pioinfo
[i
]);
1255 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1257 FILE *file
= msvcrt_get_file(j
);
1258 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1260 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1261 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1265 for(i
=0; i
<ARRAY_SIZE(MSVCRT_fstream
); i
++)
1266 free(MSVCRT_fstream
[i
]);
1269 /*********************************************************************
1270 * _lseeki64 (MSVCRT.@)
1272 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1274 ioinfo
*info
= get_ioinfo(fd
);
1277 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1279 if (info
->handle
== INVALID_HANDLE_VALUE
)
1282 release_ioinfo(info
);
1286 if (whence
< 0 || whence
> 2)
1288 release_ioinfo(info
);
1293 TRACE(":fd (%d) to %s pos %s\n",
1294 fd
,wine_dbgstr_longlong(offset
),
1295 (whence
==SEEK_SET
)?"SEEK_SET":
1296 (whence
==SEEK_CUR
)?"SEEK_CUR":
1297 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1299 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1300 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1301 ofs
.QuadPart
= offset
;
1302 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1303 GetLastError() == ERROR_SUCCESS
)
1305 info
->wxflag
&= ~WX_ATEOF
;
1306 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1308 release_ioinfo(info
);
1309 return ofs
.QuadPart
;
1311 release_ioinfo(info
);
1312 TRACE(":error-last error (%d)\n",GetLastError());
1313 msvcrt_set_errno(GetLastError());
1317 /*********************************************************************
1320 __msvcrt_long CDECL
_lseek(int fd
, __msvcrt_long offset
, int whence
)
1322 return _lseeki64(fd
, offset
, whence
);
1325 /*********************************************************************
1326 * _lock_file (MSVCRT.@)
1328 void CDECL
_lock_file(FILE *file
)
1330 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1331 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1333 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1336 /*********************************************************************
1337 * _unlock_file (MSVCRT.@)
1339 void CDECL
_unlock_file(FILE *file
)
1341 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1342 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1344 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1347 /*********************************************************************
1348 * _locking (MSVCRT.@)
1350 * This is untested; the underlying LockFile doesn't work yet.
1352 int CDECL
_locking(int fd
, int mode
, __msvcrt_long nbytes
)
1354 ioinfo
*info
= get_ioinfo(fd
);
1358 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1359 if (info
->handle
== INVALID_HANDLE_VALUE
)
1361 release_ioinfo(info
);
1365 if (mode
< 0 || mode
> 4)
1367 release_ioinfo(info
);
1372 TRACE(":fd (%d) by 0x%08Ix mode %s\n",
1373 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1374 (mode
==_LK_LOCK
)?"_LK_LOCK":
1375 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1376 (mode
==_LK_RLCK
)?"_LK_RLCK":
1377 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1380 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1382 release_ioinfo(info
);
1383 FIXME ("Seek failed\n");
1384 *_errno() = EINVAL
; /* FIXME */
1387 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1390 ret
= 1; /* just to satisfy gcc */
1393 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1398 else if (mode
== _LK_UNLCK
)
1399 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1401 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1402 /* FIXME - what about error settings? */
1403 release_ioinfo(info
);
1404 return ret
? 0 : -1;
1407 /*********************************************************************
1408 * _fseeki64 (MSVCRT.@)
1410 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1415 ret
= _fseeki64_nolock(file
, offset
, whence
);
1421 /*********************************************************************
1422 * _fseeki64_nolock (MSVCRT.@)
1424 int CDECL
_fseeki64_nolock(FILE* file
, __int64 offset
, int whence
)
1428 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1430 offset
+= _ftelli64_nolock(file
);
1433 /* Flush output if needed */
1434 msvcrt_flush_buffer(file
);
1435 /* Reset direction of i/o */
1436 if(file
->_flag
& _IORW
) {
1437 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1439 /* Clear end of file flag */
1440 file
->_flag
&= ~_IOEOF
;
1441 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1446 /*********************************************************************
1449 int CDECL
fseek(FILE* file
, __msvcrt_long offset
, int whence
)
1451 return _fseeki64( file
, offset
, whence
);
1454 /*********************************************************************
1455 * _fseek_nolock (MSVCRT.@)
1457 int CDECL
_fseek_nolock(FILE* file
, __msvcrt_long offset
, int whence
)
1459 return _fseeki64_nolock( file
, offset
, whence
);
1462 /*********************************************************************
1463 * _chsize_s (MSVCRT.@)
1465 int CDECL
_chsize_s(int fd
, __int64 size
)
1471 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1473 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1476 info
= get_ioinfo(fd
);
1477 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1479 /* save the current file pointer */
1480 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1483 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1486 ret
= SetEndOfFile(info
->handle
);
1487 if (!ret
) msvcrt_set_errno(GetLastError());
1490 /* restore the file pointer */
1491 _lseeki64(fd
, cur
, SEEK_SET
);
1495 release_ioinfo(info
);
1496 return ret
? 0 : *_errno();
1499 /*********************************************************************
1500 * _chsize (MSVCRT.@)
1502 int CDECL
_chsize(int fd
, __msvcrt_long size
)
1504 /* _chsize_s returns errno on failure but _chsize should return -1 */
1505 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1508 /*********************************************************************
1509 * clearerr (MSVCRT.@)
1511 void CDECL
clearerr(FILE* file
)
1513 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1516 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1520 /*********************************************************************
1521 * clearerr_s (MSVCRT.@)
1523 int CDECL
clearerr_s(FILE* file
)
1525 TRACE(":file (%p)\n",file
);
1527 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
1530 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1535 /*********************************************************************
1538 void CDECL
rewind(FILE* file
)
1540 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1543 _fseek_nolock(file
, 0L, SEEK_SET
);
1548 static int msvcrt_get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1550 int plus
= wcschr(mode
, '+') != NULL
;
1552 TRACE("%s\n", debugstr_w(mode
));
1554 while(*mode
== ' ') mode
++;
1559 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1560 *stream_flags
= plus
? _IORW
: _IOREAD
;
1563 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1564 *stream_flags
= plus
? _IORW
: _IOWRT
;
1567 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1568 *stream_flags
= plus
? _IORW
: _IOWRT
;
1571 MSVCRT_INVALID_PMT(0, EINVAL
);
1575 *stream_flags
|= MSVCRT__commode
;
1577 while (*mode
&& *mode
!=',')
1581 *open_flags
|= _O_BINARY
;
1582 *open_flags
&= ~_O_TEXT
;
1585 *open_flags
|= _O_TEXT
;
1586 *open_flags
&= ~_O_BINARY
;
1590 if(!MSVCRT_CHECK_PMT((*open_flags
& (_O_CREAT
| _O_APPEND
)) == _O_CREAT
))
1592 *open_flags
|= _O_EXCL
;
1596 *open_flags
|= _O_TEMPORARY
;
1599 *open_flags
|= _O_SHORT_LIVED
;
1602 *stream_flags
|= MSVCRT__IOCOMMIT
;
1605 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1608 *open_flags
|= _O_NOINHERIT
;
1616 if (!(*open_flags
& _O_RANDOM
))
1617 *open_flags
|= _O_SEQUENTIAL
;
1620 if (!(*open_flags
& _O_SEQUENTIAL
))
1621 *open_flags
|= _O_RANDOM
;
1624 ERR("incorrect mode flag: %c\n", mode
[-1]);
1631 while(*mode
== ' ') mode
++;
1632 if(!MSVCRT_CHECK_PMT(!wcsncmp(L
"ccs", mode
, 3)))
1635 while(*mode
== ' ') mode
++;
1636 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1639 while(*mode
== ' ') mode
++;
1641 if(!_wcsnicmp(L
"utf-8", mode
, 5))
1643 *open_flags
|= _O_U8TEXT
;
1646 else if(!_wcsnicmp(L
"utf-16le", mode
, 8))
1648 *open_flags
|= _O_U16TEXT
;
1651 else if(!_wcsnicmp(L
"unicode", mode
, 7))
1653 *open_flags
|= _O_WTEXT
;
1658 MSVCRT_INVALID_PMT(0, EINVAL
);
1662 while(*mode
== ' ') mode
++;
1665 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1670 /*********************************************************************
1671 * _fdopen (MSVCRT.@)
1673 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1676 wchar_t *modeW
= NULL
;
1678 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1680 ret
= _wfdopen(fd
, modeW
);
1686 /*********************************************************************
1687 * _wfdopen (MSVCRT.@)
1689 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1691 int open_flags
, stream_flags
;
1694 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1697 if (!(file
= msvcrt_alloc_fp()))
1699 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1704 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1710 /*********************************************************************
1711 * _filelength (MSVCRT.@)
1713 __msvcrt_long CDECL
_filelength(int fd
)
1715 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1718 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1721 if (endPos
!= curPos
)
1722 _lseek(fd
, curPos
, SEEK_SET
);
1729 /*********************************************************************
1730 * _filelengthi64 (MSVCRT.@)
1732 __int64 CDECL
_filelengthi64(int fd
)
1734 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1737 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1740 if (endPos
!= curPos
)
1741 _lseeki64(fd
, curPos
, SEEK_SET
);
1748 /*********************************************************************
1749 * _fileno (MSVCRT.@)
1751 int CDECL
_fileno(FILE* file
)
1753 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1757 /*********************************************************************
1758 * _fstat64 (MSVCRT.@)
1760 int CDECL
_fstat64(int fd
, struct _stat64
* buf
)
1762 ioinfo
*info
= get_ioinfo(fd
);
1766 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1767 if (info
->handle
== INVALID_HANDLE_VALUE
)
1769 release_ioinfo(info
);
1775 WARN(":failed-NULL buf\n");
1776 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1777 release_ioinfo(info
);
1781 memset(buf
, 0, sizeof(struct _stat64
));
1782 type
= GetFileType(info
->handle
);
1783 if (type
== FILE_TYPE_PIPE
)
1785 buf
->st_dev
= buf
->st_rdev
= fd
;
1786 buf
->st_mode
= _S_IFIFO
;
1789 else if (type
== FILE_TYPE_CHAR
)
1791 buf
->st_dev
= buf
->st_rdev
= fd
;
1792 buf
->st_mode
= _S_IFCHR
;
1795 else /* FILE_TYPE_DISK etc. */
1797 FILE_BASIC_INFORMATION basic_info
;
1798 FILE_STANDARD_INFORMATION std_info
;
1802 if ((status
= NtQueryInformationFile( info
->handle
, &io
, &basic_info
, sizeof(basic_info
), FileBasicInformation
)) ||
1803 (status
= NtQueryInformationFile( info
->handle
, &io
, &std_info
, sizeof(std_info
), FileStandardInformation
)))
1805 WARN(":failed-error %x\n",status
);
1806 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1807 release_ioinfo(info
);
1810 buf
->st_mode
= _S_IFREG
| 0444;
1811 if (!(basic_info
.FileAttributes
& FILE_ATTRIBUTE_READONLY
))
1812 buf
->st_mode
|= 0222;
1813 buf
->st_size
= std_info
.EndOfFile
.QuadPart
;
1814 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&basic_info
.LastAccessTime
, &dw
);
1816 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&basic_info
.LastWriteTime
, &dw
);
1817 buf
->st_mtime
= buf
->st_ctime
= dw
;
1818 buf
->st_nlink
= std_info
.NumberOfLinks
;
1819 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",basic_info
.FileAttributes
, buf
->st_mode
);
1821 release_ioinfo(info
);
1825 /*********************************************************************
1826 * _fstati64 (MSVCRT.@)
1828 int CDECL
_fstati64(int fd
, struct _stati64
* buf
)
1831 struct _stat64 buf64
;
1833 ret
= _fstat64(fd
, &buf64
);
1835 msvcrt_stat64_to_stati64(&buf64
, buf
);
1839 /*********************************************************************
1842 int CDECL
_fstat(int fd
, struct _stat
* buf
)
1844 struct _stat64 buf64
;
1846 ret
= _fstat64(fd
, &buf64
);
1848 msvcrt_stat64_to_stat(&buf64
, buf
);
1852 /*********************************************************************
1853 * _fstat32 (MSVCR80.@)
1855 int CDECL
_fstat32(int fd
, struct _stat32
* buf
)
1858 struct _stat64 buf64
;
1860 ret
= _fstat64(fd
, &buf64
);
1862 msvcrt_stat64_to_stat32(&buf64
, buf
);
1866 /*********************************************************************
1867 * _fstat32i64 (MSVCR80.@)
1869 int CDECL
_fstat32i64(int fd
, struct _stat32i64
* buf
)
1872 struct _stat64 buf64
;
1874 ret
= _fstat64(fd
, &buf64
);
1876 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1880 /*********************************************************************
1881 * _fstat64i32 (MSVCR80.@)
1883 int CDECL
_fstat64i32(int fd
, struct _stat64i32
* buf
)
1886 struct _stat64 buf64
;
1888 ret
= _fstat64(fd
, &buf64
);
1890 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1894 /*********************************************************************
1895 * _futime64 (MSVCRT.@)
1897 int CDECL
_futime64(int fd
, struct __utimbuf64
*t
)
1899 ioinfo
*info
= get_ioinfo(fd
);
1904 time_to_filetime( _time64(NULL
), &at
);
1909 time_to_filetime( t
->actime
, &at
);
1910 time_to_filetime( t
->modtime
, &wt
);
1913 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1915 release_ioinfo(info
);
1916 msvcrt_set_errno(GetLastError());
1919 release_ioinfo(info
);
1923 /*********************************************************************
1924 * _futime32 (MSVCRT.@)
1926 int CDECL
_futime32(int fd
, struct __utimbuf32
*t
)
1930 struct __utimbuf64 t64
;
1931 t64
.actime
= t
->actime
;
1932 t64
.modtime
= t
->modtime
;
1933 return _futime64( fd
, &t64
);
1936 return _futime64( fd
, NULL
);
1939 /*********************************************************************
1940 * _get_osfhandle (MSVCRT.@)
1942 intptr_t CDECL
_get_osfhandle(int fd
)
1944 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1945 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1947 if(hand
== INVALID_HANDLE_VALUE
)
1949 return (intptr_t)hand
;
1952 /*********************************************************************
1953 * _mktemp_s (MSVCRT.@)
1955 int CDECL
_mktemp_s(char *pattern
, size_t size
)
1959 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1962 for(len
=0; len
<size
; len
++)
1965 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1971 for(xno
=1; xno
<=6; xno
++)
1972 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1975 id
= GetCurrentProcessId();
1976 for(xno
=1; xno
<6; xno
++) {
1977 pattern
[len
-xno
] = id
%10 + '0';
1981 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1982 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1991 /*********************************************************************
1992 * _mktemp (MSVCRT.@)
1994 char * CDECL
_mktemp(char *pattern
)
1997 char *retVal
= pattern
;
2005 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2009 id
= GetCurrentProcessId();
2013 int tempNum
= id
/ 10;
2014 *pattern
-- = id
- (tempNum
* 10) + '0';
2020 *pattern
= letter
++;
2021 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2023 } while(letter
<= 'z');
2027 /*********************************************************************
2028 * _wmktemp_s (MSVCRT.@)
2030 int CDECL
_wmktemp_s(wchar_t *pattern
, size_t size
)
2034 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2037 for(len
=0; len
<size
; len
++)
2040 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2046 for(xno
=1; xno
<=6; xno
++)
2047 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2050 id
= GetCurrentProcessId();
2051 for(xno
=1; xno
<6; xno
++) {
2052 pattern
[len
-xno
] = id
%10 + '0';
2056 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2057 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2066 /*********************************************************************
2067 * _wmktemp (MSVCRT.@)
2069 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
2072 wchar_t *retVal
= pattern
;
2074 wchar_t letter
= 'a';
2080 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2084 id
= GetCurrentProcessId();
2088 int tempNum
= id
/ 10;
2089 *pattern
-- = id
- (tempNum
* 10) + '0';
2095 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2097 *pattern
= letter
++;
2098 } while(letter
!= '|');
2102 static unsigned split_oflags(unsigned oflags
)
2105 unsigned unsupp
; /* until we support everything */
2107 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
2108 if (oflags
& _O_BINARY
) {/* Nothing to do */}
2109 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
2110 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
2111 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
2112 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
2117 if (!(fmode
& _O_BINARY
)) wxflags
|= WX_TEXT
; /* default to TEXT*/
2119 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2121 if ((unsupp
= oflags
& ~(_O_BINARY
| _O_TEXT
| _O_APPEND
| _O_TRUNC
| _O_EXCL
| _O_CREAT
|
2122 _O_RDWR
| _O_WRONLY
| _O_TEMPORARY
| _O_NOINHERIT
| _O_SEQUENTIAL
|
2123 _O_RANDOM
| _O_SHORT_LIVED
| _O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
)))
2124 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2129 /*********************************************************************
2132 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
2135 SECURITY_ATTRIBUTES sa
;
2136 HANDLE readHandle
, writeHandle
;
2144 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2145 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
2146 sa
.lpSecurityDescriptor
= NULL
;
2147 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2149 unsigned int wxflags
= split_oflags(textmode
);
2152 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2156 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2165 CloseHandle(writeHandle
);
2171 CloseHandle(readHandle
);
2172 CloseHandle(writeHandle
);
2177 msvcrt_set_errno(GetLastError());
2182 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2184 char bom
[sizeof(utf8_bom
)];
2187 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2190 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2191 oflags
= (oflags
& ~(_O_WTEXT
| _O_U16TEXT
)) | _O_U8TEXT
;
2192 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2194 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2195 oflags
= (oflags
& ~(_O_WTEXT
| _O_U8TEXT
)) | _O_U16TEXT
;
2197 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2203 /*********************************************************************
2204 * _wsopen_dispatch (UCRTBASE.@)
2206 int CDECL
_wsopen_dispatch( const wchar_t* path
, int oflags
, int shflags
, int pmode
,
2207 int *fd
, int secure
)
2209 DWORD access
= 0, creation
= 0, attrib
;
2210 SECURITY_ATTRIBUTES sa
;
2211 DWORD sharing
, type
;
2215 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2216 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2218 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
2221 wxflag
= split_oflags(oflags
);
2222 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
2224 case _O_RDONLY
: access
|= GENERIC_READ
; break;
2225 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
2226 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2229 if (oflags
& _O_CREAT
)
2231 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(_S_IREAD
| _S_IWRITE
))))
2234 if (oflags
& _O_EXCL
)
2235 creation
= CREATE_NEW
;
2236 else if (oflags
& _O_TRUNC
)
2237 creation
= CREATE_ALWAYS
;
2239 creation
= OPEN_ALWAYS
;
2241 else /* no _O_CREAT */
2243 if (oflags
& _O_TRUNC
)
2244 creation
= TRUNCATE_EXISTING
;
2246 creation
= OPEN_EXISTING
;
2255 sharing
= FILE_SHARE_READ
;
2258 sharing
= FILE_SHARE_WRITE
;
2261 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2264 ERR( "Unhandled shflags 0x%x\n", shflags
);
2268 if (!(pmode
& ~MSVCRT_umask
& _S_IWRITE
))
2269 attrib
= FILE_ATTRIBUTE_READONLY
;
2271 attrib
= FILE_ATTRIBUTE_NORMAL
;
2273 if (oflags
& _O_TEMPORARY
)
2275 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2277 sharing
|= FILE_SHARE_DELETE
;
2280 if (oflags
& _O_RANDOM
)
2281 attrib
|= FILE_FLAG_RANDOM_ACCESS
;
2282 if (oflags
& _O_SEQUENTIAL
)
2283 attrib
|= FILE_FLAG_SEQUENTIAL_SCAN
;
2284 if (oflags
& _O_SHORT_LIVED
)
2285 attrib
|= FILE_ATTRIBUTE_TEMPORARY
;
2287 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2288 sa
.lpSecurityDescriptor
= NULL
;
2289 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
2291 if ((oflags
& (_O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
))
2292 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2293 && !(access
&GENERIC_READ
))
2295 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2296 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2297 if (hand
!= INVALID_HANDLE_VALUE
)
2299 oflags
= check_bom(hand
, oflags
, FALSE
);
2304 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2305 if (hand
== INVALID_HANDLE_VALUE
) {
2306 WARN(":failed-last error (%d)\n",GetLastError());
2307 msvcrt_set_errno(GetLastError());
2311 if (oflags
& (_O_WTEXT
| _O_U16TEXT
| _O_U8TEXT
))
2313 LARGE_INTEGER size
= {{0}};
2315 if ((access
& GENERIC_WRITE
) && (creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
))
2316 GetFileSizeEx(hand
, &size
);
2318 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2319 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2320 || ((creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
) && !size
.QuadPart
)))
2322 if (oflags
& _O_U8TEXT
)
2324 DWORD written
= 0, tmp
;
2326 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2327 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2329 if (written
!= sizeof(utf8_bom
)) {
2330 WARN("error writing BOM\n");
2332 msvcrt_set_errno(GetLastError());
2338 DWORD written
= 0, tmp
;
2340 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2341 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2343 if (written
!= sizeof(utf16_bom
))
2345 WARN("error writing BOM\n");
2347 msvcrt_set_errno(GetLastError());
2350 oflags
|= _O_U16TEXT
;
2353 else if (access
& GENERIC_READ
)
2354 oflags
= check_bom(hand
, oflags
, TRUE
);
2357 type
= GetFileType(hand
);
2358 if (type
== FILE_TYPE_CHAR
)
2360 else if (type
== FILE_TYPE_PIPE
)
2363 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2367 if (oflags
& _O_WTEXT
)
2368 get_ioinfo_nolock(*fd
)->exflag
|= EF_UNK_UNICODE
;
2370 if (oflags
& _O_U16TEXT
)
2371 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2372 else if (oflags
& _O_U8TEXT
)
2373 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2375 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2380 /*********************************************************************
2381 * _wsopen_s (MSVCRT.@)
2383 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
2385 return _wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2388 /*********************************************************************
2389 * _wsopen (MSVCRT.@)
2391 int WINAPIV
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
2396 if (oflags
& _O_CREAT
)
2400 va_start(ap
, shflags
);
2401 pmode
= va_arg(ap
, int);
2407 return _wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2411 /*********************************************************************
2412 * _sopen_dispatch (UCRTBASE.@)
2414 int CDECL
_sopen_dispatch( const char *path
, int oflags
, int shflags
,
2415 int pmode
, int *fd
, int secure
)
2420 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2423 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2426 ret
= _wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2431 /*********************************************************************
2432 * _sopen_s (MSVCRT.@)
2434 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2436 return _sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2439 /*********************************************************************
2442 int WINAPIV
_sopen( const char *path
, int oflags
, int shflags
, ... )
2447 if (oflags
& _O_CREAT
)
2451 va_start(ap
, shflags
);
2452 pmode
= va_arg(ap
, int);
2458 return _sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2461 /*********************************************************************
2464 int WINAPIV
_open( const char *path
, int flags
, ... )
2468 if (flags
& _O_CREAT
)
2471 va_start(ap
, flags
);
2472 pmode
= va_arg(ap
, int);
2474 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
2477 return _sopen( path
, flags
, _SH_DENYNO
);
2480 /*********************************************************************
2483 int WINAPIV
_wopen(const wchar_t *path
,int flags
,...)
2487 if (flags
& _O_CREAT
)
2490 va_start(ap
, flags
);
2491 pmode
= va_arg(ap
, int);
2493 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
2496 return _wsopen( path
, flags
, _SH_DENYNO
);
2499 /*********************************************************************
2502 int CDECL
_creat(const char *path
, int pmode
)
2504 int flags
= _O_CREAT
| _O_TRUNC
| _O_RDWR
;
2505 return _open(path
, flags
, pmode
);
2508 /*********************************************************************
2509 * _wcreat (MSVCRT.@)
2511 int CDECL
_wcreat(const wchar_t *path
, int pmode
)
2513 int flags
= _O_CREAT
| _O_TRUNC
| _O_RDWR
;
2514 return _wopen(path
, flags
, pmode
);
2517 /*********************************************************************
2518 * _open_osfhandle (MSVCRT.@)
2520 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2525 /* _O_RDONLY (0) always matches, so set the read flag
2526 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2527 * file, so set the write flag. It also only sets _O_TEXT if it wants
2528 * text - it never sets _O_BINARY.
2530 /* don't let split_oflags() decide the mode if no mode is passed */
2531 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2532 oflags
|= _O_BINARY
;
2534 flags
= GetFileType((HANDLE
)handle
);
2535 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2537 msvcrt_set_errno(GetLastError());
2541 if (flags
== FILE_TYPE_CHAR
)
2543 else if (flags
== FILE_TYPE_PIPE
)
2547 flags
|= split_oflags(oflags
);
2549 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2550 TRACE(":handle (%Iu) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2554 /*********************************************************************
2557 int CDECL
_rmtmp(void)
2559 int num_removed
= 0, i
;
2563 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2564 file
= msvcrt_get_file(i
);
2566 if (file
->_tmpfname
)
2575 TRACE(":removed (%d) temp files\n",num_removed
);
2579 static inline int get_utf8_char_len(char ch
)
2581 if((ch
&0xf8) == 0xf0)
2583 else if((ch
&0xf0) == 0xe0)
2585 else if((ch
&0xe0) == 0xc0)
2590 /*********************************************************************
2591 * (internal) read_utf8
2593 static int read_utf8(ioinfo
*fdinfo
, wchar_t *buf
, unsigned int count
)
2595 HANDLE hand
= fdinfo
->handle
;
2596 char min_buf
[4], *readbuf
, lookahead
;
2597 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2599 /* make the buffer big enough to hold at least one character */
2600 /* read bytes have to fit to output and lookahead buffers */
2602 readbuf_size
= count
< 4 ? 4 : count
;
2603 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2608 if(fdinfo
->lookahead
[0] != '\n') {
2609 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2610 fdinfo
->lookahead
[0] = '\n';
2612 if(fdinfo
->lookahead
[1] != '\n') {
2613 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2614 fdinfo
->lookahead
[1] = '\n';
2616 if(fdinfo
->lookahead
[2] != '\n') {
2617 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2618 fdinfo
->lookahead
[2] = '\n';
2623 /* NOTE: this case is broken in native dll, reading
2624 * sometimes fails when small buffer is passed
2627 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2628 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2629 fdinfo
->wxflag
|= WX_ATEOF
;
2632 msvcrt_set_errno(GetLastError());
2635 }else if(!num_read
) {
2636 fdinfo
->wxflag
|= WX_ATEOF
;
2642 char_len
= get_utf8_char_len(readbuf
[0]);
2644 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2648 if(readbuf
[0] == '\n')
2649 fdinfo
->wxflag
|= WX_READNL
;
2651 fdinfo
->wxflag
&= ~WX_READNL
;
2653 if(readbuf
[0] == 0x1a) {
2654 fdinfo
->wxflag
|= WX_ATEOF
;
2658 if(readbuf
[0] == '\r') {
2659 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2661 else if(lookahead
== '\n')
2665 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2666 fdinfo
->lookahead
[0] = lookahead
;
2668 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2673 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2674 msvcrt_set_errno(GetLastError());
2681 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2684 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2685 fdinfo
->wxflag
|= WX_ATEOF
;
2686 if (readbuf
!= min_buf
) free(readbuf
);
2689 msvcrt_set_errno(GetLastError());
2690 if (readbuf
!= min_buf
) free(readbuf
);
2693 }else if(!pos
&& !num_read
) {
2694 fdinfo
->wxflag
|= WX_ATEOF
;
2695 if (readbuf
!= min_buf
) free(readbuf
);
2700 if(readbuf
[0] == '\n')
2701 fdinfo
->wxflag
|= WX_READNL
;
2703 fdinfo
->wxflag
&= ~WX_READNL
;
2705 /* Find first byte of last character (may be incomplete) */
2706 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2707 if((readbuf
[i
]&0xc0) != 0x80)
2709 char_len
= get_utf8_char_len(readbuf
[i
]);
2710 if(char_len
+i
<= pos
)
2713 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2715 fdinfo
->lookahead
[0] = readbuf
[i
];
2717 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2719 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2721 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2725 for(i
=0, j
=0; i
<pos
; i
++) {
2726 if(readbuf
[i
] == 0x1a) {
2727 fdinfo
->wxflag
|= WX_ATEOF
;
2731 /* strip '\r' if followed by '\n' */
2732 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2733 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2734 readbuf
[j
++] = '\r';
2735 }else if(lookahead
== '\n' && j
==0) {
2736 readbuf
[j
++] = '\n';
2738 if(lookahead
!= '\n')
2739 readbuf
[j
++] = '\r';
2741 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2742 fdinfo
->lookahead
[0] = lookahead
;
2744 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2746 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2747 readbuf
[j
++] = readbuf
[i
];
2752 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2753 msvcrt_set_errno(GetLastError());
2754 if (readbuf
!= min_buf
) free(readbuf
);
2758 if (readbuf
!= min_buf
) free(readbuf
);
2762 /*********************************************************************
2765 * When reading \r as last character in text mode, read() positions
2766 * the file pointer on the \r character while getc() goes on to
2769 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2771 DWORD num_read
, utf16
;
2772 char *bufstart
= buf
;
2777 if (fdinfo
->wxflag
& WX_ATEOF
) {
2778 TRACE("already at EOF, returning 0\n");
2781 /* Don't trace small reads, it gets *very* annoying */
2783 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2784 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2790 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2791 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2797 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2798 return read_utf8(fdinfo
, buf
, count
);
2800 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2802 if (fdinfo
->lookahead
[0] != '\n')
2804 bufstart
[0] = fdinfo
->lookahead
[0];
2805 fdinfo
->lookahead
[0] = '\n';
2809 bufstart
[1] = fdinfo
->lookahead
[1];
2810 fdinfo
->lookahead
[1] = '\n';
2813 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2814 num_read
+= 1+utf16
;
2819 if(utf16
&& (num_read
&1))
2821 /* msvcr90 uses uninitialized value from the buffer in this case */
2822 /* msvcrt ignores additional data */
2823 ERR("got odd number of bytes in UTF16 mode\n");
2827 if (count
!= 0 && num_read
== 0)
2829 fdinfo
->wxflag
|= WX_ATEOF
;
2830 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2832 else if (fdinfo
->wxflag
& WX_TEXT
)
2836 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2837 fdinfo
->wxflag
|= WX_READNL
;
2839 fdinfo
->wxflag
&= ~WX_READNL
;
2841 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2843 /* in text mode, a ctrl-z signals EOF */
2844 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2846 fdinfo
->wxflag
|= WX_ATEOF
;
2847 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2851 /* in text mode, strip \r if followed by \n */
2852 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2857 lookahead
[1] = '\n';
2858 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2860 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2862 bufstart
[j
++] = '\n';
2863 if(utf16
) bufstart
[j
++] = 0;
2867 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2869 bufstart
[j
++] = '\r';
2870 if(utf16
) bufstart
[j
++] = 0;
2873 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2875 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2877 bufstart
[j
++] = '\n';
2878 if (utf16
) bufstart
[j
++] = 0;
2882 fdinfo
->lookahead
[0] = lookahead
[0];
2883 fdinfo
->lookahead
[1] = lookahead
[1];
2887 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2892 bufstart
[j
++] = '\r';
2893 if(utf16
) bufstart
[j
++] = 0;
2896 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2897 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2899 bufstart
[j
++] = bufstart
[i
];
2900 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2908 if (GetLastError() == ERROR_BROKEN_PIPE
)
2910 TRACE(":end-of-pipe\n");
2911 fdinfo
->wxflag
|= WX_ATEOF
;
2916 TRACE(":failed-last error (%d)\n",GetLastError());
2917 msvcrt_set_errno(GetLastError());
2923 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2927 /*********************************************************************
2930 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2935 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
2940 info
= get_ioinfo(fd
);
2941 num_read
= read_i(fd
, info
, buf
, count
);
2942 release_ioinfo(info
);
2946 /*********************************************************************
2947 * _setmode (MSVCRT.@)
2949 int CDECL
_setmode(int fd
,int mode
)
2951 ioinfo
*info
= get_ioinfo(fd
);
2952 int ret
= info
->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2953 if(ret
==_O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2956 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2957 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2959 release_ioinfo(info
);
2963 if(info
== &MSVCRT___badioinfo
) {
2968 if(mode
== _O_BINARY
) {
2969 info
->wxflag
&= ~WX_TEXT
;
2970 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2971 release_ioinfo(info
);
2975 info
->wxflag
|= WX_TEXT
;
2977 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2978 else if(mode
== _O_U8TEXT
)
2979 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2981 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2983 release_ioinfo(info
);
2987 /*********************************************************************
2988 * _stat64 (MSVCRT.@)
2990 int CDECL
_stat64(const char* path
, struct _stat64
* buf
)
2993 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2994 unsigned short mode
= ALL_S_IREAD
;
2997 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2999 plen
= strlen(path
);
3000 while (plen
&& path
[plen
-1]==' ')
3003 if (plen
==2 && path
[1]==':')
3010 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3017 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
3019 TRACE("failed (%d)\n",GetLastError());
3024 memset(buf
,0,sizeof(struct _stat64
));
3026 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3027 Bon 011120: This FIXME seems incorrect
3028 Also a letter as first char isn't enough to be classified
3031 if (isalpha(*path
)&& (*(path
+1)==':'))
3032 buf
->st_dev
= buf
->st_rdev
= _toupper_l(*path
, NULL
) - 'A'; /* drive num */
3034 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
3036 /* Dir, or regular file? */
3037 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3038 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
3043 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3045 unsigned int ext
= _tolower_l(path
[plen
-1], NULL
) |
3046 (_tolower_l(path
[plen
-2], NULL
) << 8) |
3047 (_tolower_l(path
[plen
-3], NULL
) << 16);
3048 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3049 mode
|= ALL_S_IEXEC
;
3053 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3054 mode
|= ALL_S_IWRITE
;
3056 buf
->st_mode
= mode
;
3058 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3059 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3061 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3062 buf
->st_mtime
= buf
->st_ctime
= dw
;
3063 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3064 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3065 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3069 /*********************************************************************
3070 * _stati64 (MSVCRT.@)
3072 int CDECL
_stati64(const char* path
, struct _stati64
* buf
)
3075 struct _stat64 buf64
;
3077 ret
= _stat64(path
, &buf64
);
3079 msvcrt_stat64_to_stati64(&buf64
, buf
);
3083 /*********************************************************************
3086 int CDECL
_stat(const char* path
, struct _stat
* buf
)
3089 struct _stat64 buf64
;
3091 ret
= _stat64( path
, &buf64
);
3093 msvcrt_stat64_to_stat(&buf64
, buf
);
3097 #if _MSVCR_VER >= 80
3099 /*********************************************************************
3100 * _stat32 (MSVCR80.@)
3102 int CDECL
_stat32(const char *path
, struct _stat32
*buf
)
3105 struct _stat64 buf64
;
3107 ret
= _stat64(path
, &buf64
);
3109 msvcrt_stat64_to_stat32(&buf64
, buf
);
3113 /*********************************************************************
3114 * _stat32i64 (MSVCR80.@)
3116 int CDECL
_stat32i64(const char *path
, struct _stat32i64
*buf
)
3119 struct _stat64 buf64
;
3121 ret
= _stat64(path
, &buf64
);
3123 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3127 /*********************************************************************
3128 * _stat64i32 (MSVCR80.@)
3130 int CDECL
_stat64i32(const char* path
, struct _stat64i32
*buf
)
3133 struct _stat64 buf64
;
3135 ret
= _stat64(path
, &buf64
);
3137 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3141 #endif /* _MSVCR_VER >= 80 */
3143 /*********************************************************************
3144 * _wstat64 (MSVCRT.@)
3146 int CDECL
_wstat64(const wchar_t* path
, struct _stat64
* buf
)
3149 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3150 unsigned short mode
= ALL_S_IREAD
;
3153 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3155 plen
= wcslen(path
);
3156 while (plen
&& path
[plen
-1]==' ')
3159 if (plen
==2 && path
[1]==':')
3166 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3173 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3175 TRACE("failed (%d)\n",GetLastError());
3180 memset(buf
,0,sizeof(struct _stat64
));
3182 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3183 if (iswalpha(*path
) && path
[1] == ':')
3184 buf
->st_dev
= buf
->st_rdev
= towupper(*path
) - 'A'; /* drive num */
3186 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
3188 /* Dir, or regular file? */
3189 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3190 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
3195 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3197 ULONGLONG ext
= towlower(path
[plen
-1]) | (towlower(path
[plen
-2]) << 16) |
3198 ((ULONGLONG
)towlower(path
[plen
-3]) << 32);
3199 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3200 mode
|= ALL_S_IEXEC
;
3204 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3205 mode
|= ALL_S_IWRITE
;
3207 buf
->st_mode
= mode
;
3209 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3210 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3212 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3213 buf
->st_mtime
= buf
->st_ctime
= dw
;
3214 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3215 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3216 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3220 /*********************************************************************
3221 * _wstati64 (MSVCRT.@)
3223 int CDECL
_wstati64(const wchar_t* path
, struct _stati64
* buf
)
3226 struct _stat64 buf64
;
3228 ret
= _wstat64(path
, &buf64
);
3230 msvcrt_stat64_to_stati64(&buf64
, buf
);
3234 /*********************************************************************
3237 int CDECL
_wstat(const wchar_t* path
, struct _stat
* buf
)
3240 struct _stat64 buf64
;
3242 ret
= _wstat64( path
, &buf64
);
3243 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3247 #if _MSVCR_VER >= 80
3249 /*********************************************************************
3250 * _wstat32 (MSVCR80.@)
3252 int CDECL
_wstat32(const wchar_t *path
, struct _stat32
*buf
)
3255 struct _stat64 buf64
;
3257 ret
= _wstat64(path
, &buf64
);
3259 msvcrt_stat64_to_stat32(&buf64
, buf
);
3263 /*********************************************************************
3264 * _wstat32i64 (MSVCR80.@)
3266 int CDECL
_wstat32i64(const wchar_t *path
, struct _stat32i64
*buf
)
3269 struct _stat64 buf64
;
3271 ret
= _wstat64(path
, &buf64
);
3273 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3277 /*********************************************************************
3278 * _wstat64i32 (MSVCR80.@)
3280 int CDECL
_wstat64i32(const wchar_t *path
, struct _stat64i32
*buf
)
3283 struct _stat64 buf64
;
3285 ret
= _wstat64(path
, &buf64
);
3287 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3291 #endif /* _MSVCR_VER >= 80 */
3293 /*********************************************************************
3296 __msvcrt_long CDECL
_tell(int fd
)
3298 return _lseek(fd
, 0, SEEK_CUR
);
3301 /*********************************************************************
3302 * _telli64 (MSVCRT.@)
3304 __int64 CDECL
_telli64(int fd
)
3306 return _lseeki64(fd
, 0, SEEK_CUR
);
3309 /*********************************************************************
3310 * _tempnam (MSVCRT.@)
3312 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
3314 char tmpbuf
[MAX_PATH
];
3315 const char *tmp_dir
= getenv("TMP");
3317 if (tmp_dir
) dir
= tmp_dir
;
3319 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3320 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3322 TRACE("got name (%s)\n",tmpbuf
);
3323 DeleteFileA(tmpbuf
);
3324 return _strdup(tmpbuf
);
3326 TRACE("failed (%d)\n",GetLastError());
3330 /*********************************************************************
3331 * _wtempnam (MSVCRT.@)
3333 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
3335 wchar_t tmpbuf
[MAX_PATH
];
3336 const wchar_t *tmp_dir
= _wgetenv(L
"TMP");
3338 if (tmp_dir
) dir
= tmp_dir
;
3340 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3341 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3343 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3344 DeleteFileW(tmpbuf
);
3345 return _wcsdup(tmpbuf
);
3347 TRACE("failed (%d)\n",GetLastError());
3351 /*********************************************************************
3354 int CDECL
_umask(int umask
)
3356 int old_umask
= MSVCRT_umask
;
3357 TRACE("(%d)\n",umask
);
3358 MSVCRT_umask
= umask
;
3362 /*********************************************************************
3363 * _utime64 (MSVCRT.@)
3365 int CDECL
_utime64(const char* path
, struct __utimbuf64
*t
)
3367 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
3371 int retVal
= _futime64(fd
, t
);
3378 /*********************************************************************
3379 * _utime32 (MSVCRT.@)
3381 int CDECL
_utime32(const char* path
, struct __utimbuf32
*t
)
3385 struct __utimbuf64 t64
;
3386 t64
.actime
= t
->actime
;
3387 t64
.modtime
= t
->modtime
;
3388 return _utime64( path
, &t64
);
3391 return _utime64( path
, NULL
);
3394 /*********************************************************************
3395 * _wutime64 (MSVCRT.@)
3397 int CDECL
_wutime64(const wchar_t* path
, struct __utimbuf64
*t
)
3399 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
3403 int retVal
= _futime64(fd
, t
);
3410 /*********************************************************************
3411 * _wutime32 (MSVCRT.@)
3413 int CDECL
_wutime32(const wchar_t* path
, struct __utimbuf32
*t
)
3417 struct __utimbuf64 t64
;
3418 t64
.actime
= t
->actime
;
3419 t64
.modtime
= t
->modtime
;
3420 return _wutime64( path
, &t64
);
3423 return _wutime64( path
, NULL
);
3426 /*********************************************************************
3429 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
3431 ioinfo
*info
= get_ioinfo(fd
);
3432 HANDLE hand
= info
->handle
;
3433 DWORD num_written
, i
;
3434 BOOL console
= FALSE
;
3436 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3439 release_ioinfo(info
);
3443 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3446 release_ioinfo(info
);
3450 /* If appending, go to EOF */
3451 if (info
->wxflag
& WX_APPEND
)
3452 _lseek(fd
, 0, FILE_END
);
3454 if (!(info
->wxflag
& WX_TEXT
))
3456 if (!WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3457 || num_written
!= count
)
3459 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3460 hand
, GetLastError());
3461 msvcrt_set_errno(GetLastError());
3465 release_ioinfo(info
);
3469 if (_isatty(fd
)) console
= VerifyConsoleIoHandle(hand
);
3470 for (i
= 0; i
< count
;)
3472 const char *s
= buf
;
3476 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)) && console
)
3478 char conv
[sizeof(lfbuf
)];
3481 #if _MSVCR_VER >= 90
3482 if (info
->dbcsBufferUsed
)
3484 conv
[j
++] = info
->dbcsBuffer
;
3485 info
->dbcsBufferUsed
= FALSE
;
3491 for (; i
< count
&& j
< sizeof(conv
)-1 &&
3492 len
< (sizeof(lfbuf
) - 1) / sizeof(WCHAR
); i
++, j
++, len
++)
3494 if (isleadbyte((unsigned char)s
[i
]))
3500 #if _MSVCR_VER >= 90
3501 info
->dbcsBuffer
= conv
[j
-1];
3502 info
->dbcsBufferUsed
= TRUE
;
3506 release_ioinfo(info
);
3511 else if (s
[i
] == '\n')
3519 len
= mbstowcs((WCHAR
*)lfbuf
, conv
, len
);
3522 msvcrt_set_errno(GetLastError());
3523 release_ioinfo(info
);
3528 else if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3530 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-1; i
++, j
++)
3537 else if (info
->exflag
& EF_UTF16
|| console
)
3539 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-3; i
++, j
++)
3541 if (s
[i
] == '\n' && !s
[i
+1])
3546 lfbuf
[j
++] = s
[i
++];
3552 char conv
[sizeof(lfbuf
)/4];
3554 for (j
= 0; i
< count
&& j
< sizeof(conv
)-3; i
++, j
++)
3556 if (s
[i
] == '\n' && !s
[i
+1])
3565 j
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)conv
, j
/2, lfbuf
, sizeof(lfbuf
), NULL
, NULL
);
3568 msvcrt_set_errno(GetLastError());
3569 release_ioinfo(info
);
3577 if (!WriteConsoleW(hand
, lfbuf
, j
, &num_written
, NULL
))
3580 else if (!WriteFile(hand
, lfbuf
, j
, &num_written
, NULL
))
3585 if (num_written
!= j
)
3587 TRACE("WriteFile/WriteConsoleW (fd %d, hand %p) failed-last error (%d)\n", fd
,
3588 hand
, GetLastError());
3589 msvcrt_set_errno(GetLastError());
3590 release_ioinfo(info
);
3595 release_ioinfo(info
);
3599 /*********************************************************************
3602 int CDECL
_putw(int val
, FILE* file
)
3607 len
= _write(file
->_file
, &val
, sizeof(val
));
3608 if (len
== sizeof(val
)) {
3613 file
->_flag
|= _IOERR
;
3618 /*********************************************************************
3621 int CDECL
fclose(FILE* file
)
3625 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3628 ret
= _fclose_nolock(file
);
3634 /*********************************************************************
3635 * _fclose_nolock (MSVCRT.@)
3637 int CDECL
_fclose_nolock(FILE* file
)
3641 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3643 if(!(file
->_flag
& (_IOREAD
| _IOWRT
| _IORW
)))
3650 free(file
->_tmpfname
);
3651 file
->_tmpfname
= NULL
;
3652 /* flush stdio buffers */
3653 if(file
->_flag
& _IOWRT
)
3654 _fflush_nolock(file
);
3655 if(file
->_flag
& _IOMYBUF
)
3658 r
=_close(file
->_file
);
3661 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
3664 /*********************************************************************
3667 int CDECL
feof(FILE* file
)
3669 return file
->_flag
& _IOEOF
;
3672 /*********************************************************************
3675 int CDECL
ferror(FILE* file
)
3677 return file
->_flag
& _IOERR
;
3680 /*********************************************************************
3681 * _filbuf (MSVCRT.@)
3683 int CDECL
_filbuf(FILE* file
)
3687 if(file
->_flag
& _IOSTRG
)
3690 /* Allocate buffer if needed */
3691 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
)))
3692 msvcrt_alloc_buffer(file
);
3694 if(!(file
->_flag
& _IOREAD
)) {
3695 if(file
->_flag
& _IORW
)
3696 file
->_flag
|= _IOREAD
;
3701 if(!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
))) {
3703 if ((r
= _read(file
->_file
,&c
,1)) != 1) {
3704 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
3710 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3712 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
3718 file
->_ptr
= file
->_base
+1;
3719 c
= *(unsigned char *)file
->_base
;
3724 /*********************************************************************
3727 int CDECL
fgetc(FILE* file
)
3732 ret
= _fgetc_nolock(file
);
3738 /*********************************************************************
3739 * _fgetc_nolock (MSVCRT.@)
3741 int CDECL
_fgetc_nolock(FILE* file
)
3748 i
= (unsigned char *)file
->_ptr
++;
3756 /*********************************************************************
3757 * _fgetchar (MSVCRT.@)
3759 int CDECL
_fgetchar(void)
3761 return fgetc(MSVCRT_stdin
);
3764 /*********************************************************************
3767 char * CDECL
fgets(char *s
, int size
, FILE* file
)
3770 char * buf_start
= s
;
3772 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3773 file
,file
->_file
,s
,size
);
3777 while ((size
>1) && (cc
= _fgetc_nolock(file
)) != EOF
&& cc
!= '\n')
3782 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3784 TRACE(":nothing read\n");
3788 if ((cc
!= EOF
) && (size
> 1))
3791 TRACE(":got %s\n", debugstr_a(buf_start
));
3796 /*********************************************************************
3799 wint_t CDECL
fgetwc(FILE* file
)
3804 ret
= _fgetwc_nolock(file
);
3810 /*********************************************************************
3811 * _fgetwc_nolock (MSVCRT.@)
3813 wint_t CDECL
_fgetwc_nolock(FILE* file
)
3818 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3819 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3822 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
3823 ch
= _fgetc_nolock(file
);
3831 char mbs
[MB_LEN_MAX
];
3834 ch
= _fgetc_nolock(file
);
3837 if(isleadbyte((unsigned char)mbs
[0])) {
3838 ch
= _fgetc_nolock(file
);
3848 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
3855 /*********************************************************************
3858 int CDECL
_getw(FILE* file
)
3866 for (j
=0; j
<sizeof(int); j
++) {
3867 k
= _fgetc_nolock(file
);
3869 file
->_flag
|= _IOEOF
;
3880 /*********************************************************************
3883 wint_t CDECL
getwc(FILE* file
)
3885 return fgetwc(file
);
3888 /*********************************************************************
3889 * _fgetwchar (MSVCRT.@)
3891 wint_t CDECL
_fgetwchar(void)
3893 return fgetwc(MSVCRT_stdin
);
3896 /*********************************************************************
3897 * getwchar (MSVCRT.@)
3899 wint_t CDECL
getwchar(void)
3901 return _fgetwchar();
3904 /*********************************************************************
3907 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
3910 wchar_t * buf_start
= s
;
3912 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3913 file
,file
->_file
,s
,size
);
3917 while ((size
>1) && (cc
= _fgetwc_nolock(file
)) != WEOF
&& cc
!= '\n')
3922 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3924 TRACE(":nothing read\n");
3928 if ((cc
!= WEOF
) && (size
> 1))
3931 TRACE(":got %s\n", debugstr_w(buf_start
));
3936 /*********************************************************************
3937 * _flsbuf (MSVCRT.@)
3939 int CDECL
_flsbuf(int c
, FILE* file
)
3941 /* Flush output buffer */
3942 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
))) {
3943 msvcrt_alloc_buffer(file
);
3946 if(!(file
->_flag
& _IOWRT
)) {
3947 if(!(file
->_flag
& _IORW
)) {
3948 file
->_flag
|= _IOERR
;
3952 file
->_flag
|= _IOWRT
;
3954 if(file
->_flag
& _IOREAD
) {
3955 if(!(file
->_flag
& _IOEOF
)) {
3956 file
->_flag
|= _IOERR
;
3960 file
->_ptr
= file
->_base
;
3961 file
->_flag
&= ~(_IOREAD
| _IOEOF
);
3964 if(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) {
3967 if(file
->_cnt
<= 0) {
3968 res
= msvcrt_flush_buffer(file
);
3971 file
->_flag
|= _IOWRT
;
3972 file
->_cnt
=file
->_bufsiz
;
3980 /* set _cnt to 0 for unbuffered FILEs */
3982 len
= _write(file
->_file
, &cc
, 1);
3985 file
->_flag
|= _IOERR
;
3990 /*********************************************************************
3993 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3998 ret
= _fwrite_nolock(ptr
, size
, nmemb
, file
);
4004 /*********************************************************************
4005 * _fwrite_nolock (MSVCRT.@)
4007 size_t CDECL
_fwrite_nolock(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4009 size_t wrcnt
=size
* nmemb
;
4015 if(file
->_cnt
< 0) {
4016 WARN("negative file->_cnt value in %p\n", file
);
4017 file
->_flag
|= _IOERR
;
4019 } else if(file
->_cnt
) {
4020 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
4021 memcpy(file
->_ptr
, ptr
, pcnt
);
4026 ptr
= (const char*)ptr
+ pcnt
;
4027 } else if((file
->_flag
& _IONBF
)
4028 || ((file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4029 || (!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4033 if(file
->_flag
& _IONBF
)
4035 else if(!(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)))
4036 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4038 bufsiz
= file
->_bufsiz
;
4040 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4042 if(msvcrt_flush_buffer(file
) == EOF
)
4045 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
4046 file
->_flag
|= _IOERR
;
4051 ptr
= (const char*)ptr
+ pcnt
;
4053 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
4057 ptr
= (const char*)ptr
+ 1;
4061 return written
/ size
;
4064 /*********************************************************************
4067 wint_t CDECL
fputwc(wint_t wc
, FILE* file
)
4072 ret
= _fputwc_nolock(wc
, file
);
4078 /*********************************************************************
4079 * _fputwc_nolock (MSVCRT.@)
4081 wint_t CDECL
_fputwc_nolock(wint_t wc
, FILE* file
)
4087 fdinfo
= get_ioinfo_nolock(file
->_file
);
4089 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
4090 char buf
[MB_LEN_MAX
];
4093 char_len
= wctomb(buf
, mwc
);
4094 if(char_len
!=-1 && _fwrite_nolock(buf
, char_len
, 1, file
)==1)
4098 }else if(_fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4107 /*********************************************************************
4108 * _fputwchar (MSVCRT.@)
4110 wint_t CDECL
_fputwchar(wint_t wc
)
4112 return fputwc(wc
, MSVCRT_stdout
);
4115 /*********************************************************************
4116 * _wfsopen (MSVCRT.@)
4118 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
4121 int open_flags
, stream_flags
, fd
;
4123 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4125 /* map mode string to open() flags. "man fopen" for possibilities. */
4126 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4130 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
4133 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4135 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4142 TRACE(":got (%p)\n",file
);
4143 if (fd
>= 0 && !file
)
4149 /*********************************************************************
4150 * _fsopen (MSVCRT.@)
4152 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
4155 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4157 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4158 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4162 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4165 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4170 ret
= _wfsopen(pathW
, modeW
, share
);
4177 /*********************************************************************
4180 FILE * CDECL
fopen(const char *path
, const char *mode
)
4182 return _fsopen( path
, mode
, _SH_DENYNO
);
4185 /*********************************************************************
4186 * fopen_s (MSVCRT.@)
4188 int CDECL
fopen_s(FILE** pFile
,
4189 const char *filename
, const char *mode
)
4191 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4192 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
4193 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4195 *pFile
= fopen(filename
, mode
);
4202 /*********************************************************************
4203 * _wfopen (MSVCRT.@)
4205 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
4207 return _wfsopen( path
, mode
, _SH_DENYNO
);
4210 /*********************************************************************
4211 * _wfopen_s (MSVCRT.@)
4213 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
4214 const wchar_t *mode
)
4216 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4217 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
4218 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4220 *pFile
= _wfopen(filename
, mode
);
4227 /*********************************************************************
4230 int CDECL
fputc(int c
, FILE* file
)
4235 ret
= _fputc_nolock(c
, file
);
4241 /*********************************************************************
4242 * _fputc_nolock (MSVCRT.@)
4244 int CDECL
_fputc_nolock(int c
, FILE* file
)
4253 res
= msvcrt_flush_buffer(file
);
4254 return res
? res
: c
;
4260 res
= _flsbuf(c
, file
);
4265 /*********************************************************************
4266 * _fputchar (MSVCRT.@)
4268 int CDECL
_fputchar(int c
)
4270 return fputc(c
, MSVCRT_stdout
);
4273 /*********************************************************************
4276 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4281 ret
= _fread_nolock(ptr
, size
, nmemb
, file
);
4287 /*********************************************************************
4288 * _fread_nolock (MSVCRT.@)
4290 size_t CDECL
_fread_nolock(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
4292 size_t rcnt
=size
* nmemb
;
4299 /* first buffered data */
4301 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4302 memcpy(ptr
, file
->_ptr
, pcnt
);
4307 ptr
= (char*)ptr
+ pcnt
;
4308 } else if(!(file
->_flag
& _IOREAD
)) {
4309 if(file
->_flag
& _IORW
) {
4310 file
->_flag
|= _IOREAD
;
4316 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
)))
4317 msvcrt_alloc_buffer(file
);
4322 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
))) {
4323 i
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
4324 file
->_ptr
= file
->_base
;
4327 if (i
> rcnt
) i
= rcnt
;
4329 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4330 if (i
> 0 && i
< file
->_cnt
) {
4331 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4332 file
->_flag
&= ~_IOEOF
;
4335 memcpy(ptr
, file
->_ptr
, i
);
4339 } else if (rcnt
> INT_MAX
) {
4340 i
= _read(file
->_file
, ptr
, INT_MAX
);
4341 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4342 i
= _read(file
->_file
, ptr
, rcnt
);
4344 i
= _read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4348 ptr
= (char *)ptr
+i
;
4349 /* expose feof condition in the flags
4350 * MFC tests file->_flag for feof, and doesn't call feof())
4352 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4353 file
->_flag
|= _IOEOF
;
4356 file
->_flag
|= _IOERR
;
4366 #if _MSVCR_VER >= 80
4368 /*********************************************************************
4369 * fread_s (MSVCR80.@)
4371 size_t CDECL
fread_s(void *buf
, size_t buf_size
, size_t elem_size
,
4372 size_t count
, FILE *stream
)
4376 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4378 memset(buf
, 0, buf_size
);
4381 if(!elem_size
|| !count
) return 0;
4384 ret
= _fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4385 _unlock_file(stream
);
4390 /*********************************************************************
4391 * _fread_nolock_s (MSVCR80.@)
4393 size_t CDECL
_fread_nolock_s(void *buf
, size_t buf_size
, size_t elem_size
,
4394 size_t count
, FILE *stream
)
4396 size_t bytes_left
, buf_pos
;
4398 TRACE("(%p %Iu %Iu %Iu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4400 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4402 memset(buf
, 0, buf_size
);
4405 if(!elem_size
|| !count
) return 0;
4406 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4407 if(!MSVCRT_CHECK_PMT(SIZE_MAX
/count
>= elem_size
)) return 0;
4409 bytes_left
= elem_size
*count
;
4412 if(stream
->_cnt
> 0) {
4413 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4415 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, ERANGE
)) {
4416 memset(buf
, 0, buf_size
);
4420 _fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4424 int c
= _filbuf(stream
);
4429 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, ERANGE
)) {
4430 memset(buf
, 0, buf_size
);
4434 ((char*)buf
)[buf_pos
++] = c
;
4439 return buf_pos
/elem_size
;
4442 #endif /* _MSVCR_VER >= 80 */
4444 /*********************************************************************
4445 * _wfreopen (MSVCRT.@)
4448 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
4450 int open_flags
, stream_flags
, fd
;
4452 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4455 if (!file
|| ((fd
= file
->_file
) < 0))
4460 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4462 else if((fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
)) < 0)
4464 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4474 /*********************************************************************
4475 * _wfreopen_s (MSVCRT.@)
4477 int CDECL
_wfreopen_s(FILE** pFile
,
4478 const wchar_t *path
, const wchar_t *mode
, FILE* file
)
4480 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4481 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return EINVAL
;
4482 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4483 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
4485 *pFile
= _wfreopen(path
, mode
, file
);
4492 /*********************************************************************
4493 * freopen (MSVCRT.@)
4496 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
4499 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4501 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4502 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4508 ret
= _wfreopen(pathW
, modeW
, file
);
4515 /*********************************************************************
4516 * freopen_s (MSVCRT.@)
4518 int CDECL
freopen_s(FILE** pFile
,
4519 const char *path
, const char *mode
, FILE* file
)
4521 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
4522 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return EINVAL
;
4523 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
4524 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
4526 *pFile
= freopen(path
, mode
, file
);
4533 /*********************************************************************
4534 * fsetpos (MSVCRT.@)
4536 int CDECL
fsetpos(FILE* file
, fpos_t *pos
)
4541 msvcrt_flush_buffer(file
);
4543 /* Reset direction of i/o */
4544 if(file
->_flag
& _IORW
) {
4545 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
4548 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4553 /*********************************************************************
4554 * _ftelli64 (MSVCRT.@)
4556 __int64 CDECL
_ftelli64(FILE* file
)
4561 ret
= _ftelli64_nolock(file
);
4567 /*********************************************************************
4568 * _ftelli64_nolock (MSVCRT.@)
4570 __int64 CDECL
_ftelli64_nolock(FILE* file
)
4574 pos
= _telli64(file
->_file
);
4577 if(file
->_flag
& (_IOMYBUF
| MSVCRT__USERBUF
)) {
4578 if(file
->_flag
& _IOWRT
) {
4579 pos
+= file
->_ptr
- file
->_base
;
4581 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4584 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4588 } else if(!file
->_cnt
) { /* nothing to do */
4589 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4593 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4594 for(i
=0; i
<file
->_cnt
; i
++)
4595 if(file
->_ptr
[i
] == '\n')
4601 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4604 pos
-= file
->_bufsiz
;
4605 pos
+= file
->_ptr
- file
->_base
;
4607 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4608 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4611 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4621 /*********************************************************************
4624 __msvcrt_long CDECL
ftell(FILE* file
)
4626 return _ftelli64(file
);
4629 #if _MSVCR_VER >= 80
4630 /*********************************************************************
4631 * _ftell_nolock (MSVCR80.@)
4633 __msvcrt_long CDECL
_ftell_nolock(FILE* file
)
4635 return _ftelli64_nolock(file
);
4639 /*********************************************************************
4640 * fgetpos (MSVCRT.@)
4642 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
4644 *pos
= _ftelli64(file
);
4650 /*********************************************************************
4653 int CDECL
fputs(const char *s
, FILE* file
)
4655 size_t len
= strlen(s
);
4659 ret
= _fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
4664 /*********************************************************************
4667 int CDECL
fputws(const wchar_t *s
, FILE* file
)
4669 size_t i
, len
= wcslen(s
);
4674 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4675 ret
= _fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
4680 tmp_buf
= add_std_buffer(file
);
4681 for (i
=0; i
<len
; i
++) {
4682 if(_fputwc_nolock(s
[i
], file
) == WEOF
) {
4683 if(tmp_buf
) remove_std_buffer(file
);
4689 if(tmp_buf
) remove_std_buffer(file
);
4694 /*********************************************************************
4695 * getchar (MSVCRT.@)
4697 int CDECL
getchar(void)
4699 return fgetc(MSVCRT_stdin
);
4702 /*********************************************************************
4705 int CDECL
getc(FILE* file
)
4710 /*********************************************************************
4711 * gets_s (MSVCR80.@)
4713 char * CDECL
gets_s(char *buf
, size_t len
)
4715 char *buf_start
= buf
;
4718 if (!MSVCRT_CHECK_PMT(buf
!= NULL
)) return NULL
;
4719 if (!MSVCRT_CHECK_PMT(len
!= 0)) return NULL
;
4721 _lock_file(MSVCRT_stdin
);
4722 for(cc
= _fgetc_nolock(MSVCRT_stdin
);
4723 len
!= 0 && cc
!= EOF
&& cc
!= '\n';
4724 cc
= _fgetc_nolock(MSVCRT_stdin
))
4732 _unlock_file(MSVCRT_stdin
);
4737 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4741 if ((cc
== EOF
) && (buf_start
== buf
))
4743 TRACE(":nothing read\n");
4748 TRACE("got '%s'\n", buf_start
);
4752 /*********************************************************************
4755 char * CDECL
gets(char *buf
)
4757 return gets_s(buf
, -1);
4760 /*********************************************************************
4763 wchar_t* CDECL
_getws(wchar_t* buf
)
4768 _lock_file(MSVCRT_stdin
);
4769 for (cc
= _fgetwc_nolock(MSVCRT_stdin
); cc
!= WEOF
&& cc
!= '\n';
4770 cc
= _fgetwc_nolock(MSVCRT_stdin
))
4773 *buf
++ = (wchar_t)cc
;
4775 _unlock_file(MSVCRT_stdin
);
4777 if ((cc
== WEOF
) && (ws
== buf
))
4779 TRACE(":nothing read\n");
4784 TRACE("got %s\n", debugstr_w(ws
));
4788 /*********************************************************************
4791 int CDECL
putc(int c
, FILE* file
)
4793 return fputc(c
, file
);
4796 /*********************************************************************
4797 * putchar (MSVCRT.@)
4799 int CDECL
putchar(int c
)
4801 return fputc(c
, MSVCRT_stdout
);
4804 /*********************************************************************
4807 int CDECL
puts(const char *s
)
4809 size_t len
= strlen(s
);
4812 _lock_file(MSVCRT_stdout
);
4813 if(_fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4814 _unlock_file(MSVCRT_stdout
);
4818 ret
= _fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : EOF
;
4819 _unlock_file(MSVCRT_stdout
);
4823 /*********************************************************************
4826 int CDECL
_putws(const wchar_t *s
)
4830 _lock_file(MSVCRT_stdout
);
4831 ret
= fputws(s
, MSVCRT_stdout
);
4833 ret
= _fputwc_nolock('\n', MSVCRT_stdout
);
4834 _unlock_file(MSVCRT_stdout
);
4835 return ret
>= 0 ? 0 : WEOF
;
4838 /*********************************************************************
4841 int CDECL
remove(const char *path
)
4843 TRACE("(%s)\n",path
);
4844 if (DeleteFileA(path
))
4846 TRACE(":failed (%d)\n",GetLastError());
4847 msvcrt_set_errno(GetLastError());
4851 /*********************************************************************
4852 * _wremove (MSVCRT.@)
4854 int CDECL
_wremove(const wchar_t *path
)
4856 TRACE("(%s)\n",debugstr_w(path
));
4857 if (DeleteFileW(path
))
4859 TRACE(":failed (%d)\n",GetLastError());
4860 msvcrt_set_errno(GetLastError());
4864 /*********************************************************************
4867 int CDECL
rename(const char *oldpath
,const char *newpath
)
4869 TRACE(":from %s to %s\n",oldpath
,newpath
);
4870 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4872 TRACE(":failed (%d)\n",GetLastError());
4873 msvcrt_set_errno(GetLastError());
4877 /*********************************************************************
4878 * _wrename (MSVCRT.@)
4880 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
4882 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4883 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4885 TRACE(":failed (%d)\n",GetLastError());
4886 msvcrt_set_errno(GetLastError());
4890 /*********************************************************************
4891 * setvbuf (MSVCRT.@)
4893 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
4895 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4896 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
4897 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4901 _fflush_nolock(file
);
4902 if(file
->_flag
& _IOMYBUF
)
4904 file
->_flag
&= ~(_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
);
4907 if(mode
== _IONBF
) {
4908 file
->_flag
|= _IONBF
;
4909 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4912 file
->_base
= file
->_ptr
= buf
;
4913 file
->_flag
|= MSVCRT__USERBUF
;
4914 file
->_bufsiz
= size
;
4916 file
->_base
= file
->_ptr
= malloc(size
);
4923 file
->_flag
|= _IOMYBUF
;
4924 file
->_bufsiz
= size
;
4930 /*********************************************************************
4933 void CDECL
setbuf(FILE* file
, char *buf
)
4935 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
4938 static int tmpnam_helper(char *s
, size_t size
, int *tmpnam_unique
, int tmp_max
)
4944 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return EINVAL
;
4954 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4955 if (digits
+1 > size
) {
4960 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4966 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4967 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4971 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4972 if (digits
+1 > size
) {
4977 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4980 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4981 GetLastError() == ERROR_FILE_NOT_FOUND
)
4987 int CDECL
tmpnam_s(char *s
, size_t size
)
4989 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, TMP_MAX_S
);
4992 /*********************************************************************
4995 char * CDECL
tmpnam(char *s
)
4998 thread_data_t
*data
= msvcrt_get_thread_data();
5000 if(!data
->tmpnam_buffer
)
5001 data
->tmpnam_buffer
= malloc(MAX_PATH
);
5003 s
= data
->tmpnam_buffer
;
5006 return tmpnam_helper(s
, -1, &tmpnam_unique
, TMP_MAX
) ? NULL
: s
;
5009 static int wtmpnam_helper(wchar_t *s
, size_t size
, int *tmpnam_unique
, int tmp_max
)
5015 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return EINVAL
;
5025 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5026 if (digits
+1 > size
) {
5031 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5037 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5038 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5042 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5043 if (digits
+1 > size
) {
5048 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5051 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5052 GetLastError() == ERROR_FILE_NOT_FOUND
)
5058 /*********************************************************************
5059 * _wtmpnam_s (MSVCRT.@)
5061 int CDECL
_wtmpnam_s(wchar_t *s
, size_t size
)
5063 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, TMP_MAX_S
);
5066 /*********************************************************************
5067 * _wtmpnam (MSVCRT.@)
5069 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
5072 thread_data_t
*data
= msvcrt_get_thread_data();
5074 if(!data
->wtmpnam_buffer
)
5075 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
5077 s
= data
->wtmpnam_buffer
;
5080 return wtmpnam_helper(s
, -1, &tmpnam_unique
, TMP_MAX
) ? NULL
: s
;
5083 /*********************************************************************
5084 * tmpfile (MSVCRT.@)
5086 FILE* CDECL
tmpfile(void)
5088 char *filename
= _tempnam(",", "t");
5093 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
5094 _S_IREAD
| _S_IWRITE
);
5095 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5097 if (msvcrt_init_fp(file
, fd
, _IORW
) == -1)
5102 else file
->_tmpfname
= _strdup(filename
);
5105 if(fd
!= -1 && !file
)
5112 /*********************************************************************
5113 * tmpfile_s (MSVCRT.@)
5115 int CDECL
tmpfile_s(FILE** file
)
5117 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return EINVAL
;
5123 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5125 return fwrite(str
, sizeof(char), len
, file
);
5128 static int puts_clbk_file_w(void *file
, int len
, const wchar_t *str
)
5134 if(!(get_ioinfo_nolock(((FILE*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5135 ret
= _fwrite_nolock(str
, sizeof(wchar_t), len
, file
);
5140 for(i
=0; i
<len
; i
++) {
5141 if(_fputwc_nolock(str
[i
], file
) == WEOF
) {
5151 static int vfprintf_helper(DWORD options
, FILE* file
, const char *format
,
5152 _locale_t locale
, va_list valist
)
5154 printf_arg args_ctx
[_ARGMAX
+1];
5158 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5159 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5161 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5162 memset(args_ctx
, 0, sizeof(args_ctx
));
5163 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5165 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5169 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5173 tmp_buf
= add_std_buffer(file
);
5174 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5175 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5176 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5177 if(tmp_buf
) remove_std_buffer(file
);
5183 static int vfwprintf_helper(DWORD options
, FILE* file
, const wchar_t *format
,
5184 _locale_t locale
, va_list valist
)
5186 printf_arg args_ctx
[_ARGMAX
+1];
5190 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5191 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5193 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5194 memset(args_ctx
, 0, sizeof(args_ctx
));
5195 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5197 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5201 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5205 tmp_buf
= add_std_buffer(file
);
5206 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5207 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5208 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5209 if(tmp_buf
) remove_std_buffer(file
);
5215 /*********************************************************************
5216 * _vfprintf_s_l (MSVCRT.@)
5218 int CDECL
_vfprintf_s_l(FILE* file
, const char *format
,
5219 _locale_t locale
, va_list valist
)
5221 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5224 /*********************************************************************
5225 * _vfwprintf_s_l (MSVCRT.@)
5227 int CDECL
_vfwprintf_s_l(FILE* file
, const wchar_t *format
,
5228 _locale_t locale
, va_list valist
)
5230 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5233 /*********************************************************************
5234 * vfprintf (MSVCRT.@)
5236 int CDECL
vfprintf(FILE* file
, const char *format
, va_list valist
)
5238 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5241 /*********************************************************************
5242 * vfprintf_s (MSVCRT.@)
5244 int CDECL
vfprintf_s(FILE* file
, const char *format
, va_list valist
)
5246 return _vfprintf_s_l(file
, format
, NULL
, valist
);
5249 /*********************************************************************
5250 * vfwprintf (MSVCRT.@)
5252 int CDECL
vfwprintf(FILE* file
, const wchar_t *format
, va_list valist
)
5254 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5257 /*********************************************************************
5258 * vfwprintf_s (MSVCRT.@)
5260 int CDECL
vfwprintf_s(FILE* file
, const wchar_t *format
, va_list valist
)
5262 return _vfwprintf_s_l(file
, format
, NULL
, valist
);
5265 #if _MSVCR_VER >= 140
5267 /*********************************************************************
5268 * __stdio_common_vfprintf (UCRTBASE.@)
5270 int CDECL
_stdio_common_vfprintf(unsigned __int64 options
, FILE *file
, const char *format
,
5271 _locale_t locale
, va_list valist
)
5273 if (options
& ~UCRTBASE_PRINTF_MASK
)
5274 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5276 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5279 /*********************************************************************
5280 * __stdio_common_vfprintf_p (UCRTBASE.@)
5282 int CDECL
__stdio_common_vfprintf_p(unsigned __int64 options
, FILE *file
, const char *format
,
5283 _locale_t locale
, va_list valist
)
5285 if (options
& ~UCRTBASE_PRINTF_MASK
)
5286 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5288 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_POSITIONAL_PARAMS
5289 | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5293 /*********************************************************************
5294 * __stdio_common_vfprintf_s (UCRTBASE.@)
5296 int CDECL
__stdio_common_vfprintf_s(unsigned __int64 options
, FILE *file
, const char *format
,
5297 _locale_t locale
, va_list valist
)
5299 if (options
& ~UCRTBASE_PRINTF_MASK
)
5300 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5302 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5303 file
, format
, locale
, valist
);
5306 /*********************************************************************
5307 * __stdio_common_vfwprintf (UCRTBASE.@)
5309 int CDECL
__stdio_common_vfwprintf(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5310 _locale_t locale
, va_list valist
)
5312 if (options
& ~UCRTBASE_PRINTF_MASK
)
5313 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5315 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5318 /*********************************************************************
5319 * __stdio_common_vfwprintf_p (UCRTBASE.@)
5321 int CDECL
__stdio_common_vfwprintf_p(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5322 _locale_t locale
, va_list valist
)
5324 if (options
& ~UCRTBASE_PRINTF_MASK
)
5325 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5327 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_POSITIONAL_PARAMS
5328 | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5332 /*********************************************************************
5333 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5335 int CDECL
__stdio_common_vfwprintf_s(unsigned __int64 options
, FILE *file
, const wchar_t *format
,
5336 _locale_t locale
, va_list valist
)
5338 if (options
& ~UCRTBASE_PRINTF_MASK
)
5339 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5341 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5342 file
, format
, locale
, valist
);
5345 #endif /* _MSVCR_VER >= 140 */
5347 /*********************************************************************
5348 * _vfprintf_l (MSVCRT.@)
5350 int CDECL
_vfprintf_l(FILE* file
, const char *format
,
5351 _locale_t locale
, va_list valist
)
5353 return vfprintf_helper(0, file
, format
, locale
, valist
);
5356 /*********************************************************************
5357 * _vfwprintf_l (MSVCRT.@)
5359 int CDECL
_vfwprintf_l(FILE* file
, const wchar_t *format
,
5360 _locale_t locale
, va_list valist
)
5362 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5365 /*********************************************************************
5366 * _vfprintf_p_l (MSVCRT.@)
5368 int CDECL
_vfprintf_p_l(FILE* file
, const char *format
,
5369 _locale_t locale
, va_list valist
)
5371 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5372 file
, format
, locale
, valist
);
5375 /*********************************************************************
5376 * _vfprintf_p (MSVCRT.@)
5378 int CDECL
_vfprintf_p(FILE* file
, const char *format
, va_list valist
)
5380 return _vfprintf_p_l(file
, format
, NULL
, valist
);
5383 /*********************************************************************
5384 * _vfwprintf_p_l (MSVCRT.@)
5386 int CDECL
_vfwprintf_p_l(FILE* file
, const wchar_t *format
,
5387 _locale_t locale
, va_list valist
)
5389 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5390 file
, format
, locale
, valist
);
5393 /*********************************************************************
5394 * _vfwprintf_p (MSVCRT.@)
5396 int CDECL
_vfwprintf_p(FILE* file
, const wchar_t *format
, va_list valist
)
5398 return _vfwprintf_p_l(file
, format
, NULL
, valist
);
5401 /*********************************************************************
5402 * vprintf (MSVCRT.@)
5404 int CDECL
vprintf(const char *format
, va_list valist
)
5406 return vfprintf(MSVCRT_stdout
,format
,valist
);
5409 /*********************************************************************
5410 * vprintf_s (MSVCRT.@)
5412 int CDECL
vprintf_s(const char *format
, va_list valist
)
5414 return vfprintf_s(MSVCRT_stdout
,format
,valist
);
5417 /*********************************************************************
5418 * vwprintf (MSVCRT.@)
5420 int CDECL
vwprintf(const wchar_t *format
, va_list valist
)
5422 return vfwprintf(MSVCRT_stdout
,format
,valist
);
5425 /*********************************************************************
5426 * vwprintf_s (MSVCRT.@)
5428 int CDECL
vwprintf_s(const wchar_t *format
, va_list valist
)
5430 return vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5433 /*********************************************************************
5434 * fprintf (MSVCRT.@)
5436 int WINAPIV
fprintf(FILE* file
, const char *format
, ...)
5440 va_start(valist
, format
);
5441 res
= vfprintf(file
, format
, valist
);
5446 /*********************************************************************
5447 * fprintf_s (MSVCRT.@)
5449 int WINAPIV
fprintf_s(FILE* file
, const char *format
, ...)
5453 va_start(valist
, format
);
5454 res
= vfprintf_s(file
, format
, valist
);
5459 /*********************************************************************
5460 * fwprintf (MSVCRT.@)
5462 int WINAPIV
fwprintf(FILE* file
, const wchar_t *format
, ...)
5466 va_start(valist
, format
);
5467 res
= vfwprintf(file
, format
, valist
);
5472 /*********************************************************************
5473 * fwprintf_s (MSVCRT.@)
5475 int WINAPIV
fwprintf_s(FILE* file
, const wchar_t *format
, ...)
5479 va_start(valist
, format
);
5480 res
= vfwprintf_s(file
, format
, valist
);
5485 /*********************************************************************
5486 * _fwprintf_l (MSVCRT.@)
5488 int WINAPIV
_fwprintf_l(FILE* file
, const wchar_t *format
, _locale_t locale
, ...)
5492 va_start(valist
, locale
);
5493 res
= _vfwprintf_l(file
, format
, locale
, valist
);
5498 /*********************************************************************
5501 int WINAPIV
printf(const char *format
, ...)
5505 va_start(valist
, format
);
5506 res
= vfprintf(MSVCRT_stdout
, format
, valist
);
5511 /*********************************************************************
5512 * printf_s (MSVCRT.@)
5514 int WINAPIV
printf_s(const char *format
, ...)
5518 va_start(valist
, format
);
5519 res
= vprintf_s(format
, valist
);
5524 /*********************************************************************
5527 int CDECL
ungetc(int c
, FILE * file
)
5531 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
5534 ret
= _ungetc_nolock(c
, file
);
5540 /*********************************************************************
5541 * _ungetc_nolock (MSVCRT.@)
5543 int CDECL
_ungetc_nolock(int c
, FILE * file
)
5545 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
5547 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
5548 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
5551 if((!(file
->_flag
& (_IONBF
| _IOMYBUF
| MSVCRT__USERBUF
))
5552 && msvcrt_alloc_buffer(file
))
5553 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5556 if(file
->_ptr
>file
->_base
) {
5558 if(file
->_flag
& _IOSTRG
) {
5559 if(*file
->_ptr
!= c
) {
5567 file
->_flag
&= ~(_IOERR
| _IOEOF
);
5568 file
->_flag
|= _IOREAD
;
5575 /*********************************************************************
5576 * ungetwc (MSVCRT.@)
5578 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
5582 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5585 ret
= _ungetwc_nolock(wc
, file
);
5591 /*********************************************************************
5592 * _ungetwc_nolock (MSVCRT.@)
5594 wint_t CDECL
_ungetwc_nolock(wint_t wc
, FILE * file
)
5598 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5602 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5603 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5604 unsigned char * pp
= (unsigned char *)&mwc
;
5607 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
5608 if(pp
[i
] != _ungetc_nolock(pp
[i
],file
))
5612 char mbs
[MB_LEN_MAX
];
5615 len
= wctomb(mbs
, mwc
);
5619 for(len
--; len
>=0; len
--) {
5620 if(mbs
[len
] != _ungetc_nolock(mbs
[len
], file
))
5628 /*********************************************************************
5629 * wprintf (MSVCRT.@)
5631 int WINAPIV
wprintf(const wchar_t *format
, ...)
5635 va_start(valist
, format
);
5636 res
= vwprintf(format
, valist
);
5641 /*********************************************************************
5642 * wprintf_s (MSVCRT.@)
5644 int WINAPIV
wprintf_s(const wchar_t *format
, ...)
5648 va_start(valist
, format
);
5649 res
= vwprintf_s(format
, valist
);
5654 /*********************************************************************
5655 * _getmaxstdio (MSVCRT.@)
5657 int CDECL
_getmaxstdio(void)
5659 return MSVCRT_max_streams
;
5662 /*********************************************************************
5663 * _setmaxstdio (MSVCRT.@)
5665 int CDECL
_setmaxstdio(int newmax
)
5667 TRACE("%d\n", newmax
);
5669 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5672 MSVCRT_max_streams
= newmax
;
5673 return MSVCRT_max_streams
;
5676 #if _MSVCR_VER >= 140
5677 /*********************************************************************
5678 * _get_stream_buffer_pointers (UCRTBASE.@)
5680 int CDECL
_get_stream_buffer_pointers(FILE *file
, char*** base
,
5681 char*** ptr
, int** count
)
5684 *base
= &file
->_base
;
5688 *count
= &file
->_cnt
;