kernelbase: Ignore URL_PARTFLAG_KEEPSCHEME when used with URL_PART_SCHEME or URL_PART...
[wine.git] / dlls / msvcrt / file.c
blob86c6786838196c6f7bfdf687c15029e871a4c8a7
1 /*
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
27 #include <direct.h>
28 #include <fcntl.h>
29 #include <io.h>
30 #include <share.h>
31 #include <stdarg.h>
32 #include <sys/locking.h>
33 #include <sys/types.h>
34 #include <sys/utime.h>
35 #include <time.h>
36 #include <limits.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wincon.h"
41 #include "winternl.h"
42 #include "winnls.h"
43 #include "msvcrt.h"
44 #include "mtdll.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
50 #undef _fstat
51 #undef _fstati64
52 #undef _stat
53 #undef _stati64
54 #undef _wstat
55 #undef _wstati64
56 #undef _fstat32
57 #undef _fstat32i64
58 #undef _stat32i64
59 #undef _stat32
60 #undef _wstat32
61 #undef _wstat32i64
62 #undef _fstat64i32
63 #undef _fstat64
64 #undef _stat64
65 #undef _stat64i32
66 #undef _wstat64i32
67 #undef _wstat64
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 */
79 #define WX_OPEN 0x01
80 #define WX_ATEOF 0x02
81 #define WX_READNL 0x04 /* read started with \n */
82 #define WX_PIPE 0x08
83 #define WX_DONTINHERIT 0x10
84 #define WX_APPEND 0x20
85 #define WX_TTY 0x40
86 #define WX_TEXT 0x80
88 /* values for exflag - it's used differently in msvcr90.dll*/
89 #define EF_UTF8 0x01
90 #define EF_UTF16 0x02
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 */
104 typedef struct {
105 HANDLE handle;
106 unsigned char wxflag;
107 char lookahead[3];
108 int exflag;
109 CRITICAL_SECTION crit;
110 #if _MSVCR_VER >= 80
111 char textmode : 7;
112 char unicode : 1;
113 char pipech2[2];
114 __int64 startpos;
115 BOOL utf8translations;
116 #endif
117 #if _MSVCR_VER >= 90
118 char dbcsBuffer;
119 BOOL dbcsBufferUsed;
120 #endif
121 } ioinfo;
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 };
134 typedef struct {
135 FILE file;
136 CRITICAL_SECTION crit;
137 } file_crit;
139 FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
140 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
141 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
143 /* INTERNAL: process umask */
144 static int MSVCRT_umask = 0;
146 /* INTERNAL: static data for tmpnam and _wtmpname functions */
147 static LONG tmpnam_unique;
148 static LONG tmpnam_s_unique;
150 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
151 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
152 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
153 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
155 #define TOUL(x) (ULONGLONG)(x)
156 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
157 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
158 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
159 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
161 /* This critical section protects the MSVCRT_fstreams table
162 * and MSVCRT_stream_idx from race conditions. It also
163 * protects fd critical sections creation code.
165 static CRITICAL_SECTION MSVCRT_file_cs;
166 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
168 0, 0, &MSVCRT_file_cs,
169 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
170 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
172 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
173 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
174 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
176 static void msvcrt_stat64_to_stat(const struct _stat64 *buf64, struct _stat *buf)
178 buf->st_dev = buf64->st_dev;
179 buf->st_ino = buf64->st_ino;
180 buf->st_mode = buf64->st_mode;
181 buf->st_nlink = buf64->st_nlink;
182 buf->st_uid = buf64->st_uid;
183 buf->st_gid = buf64->st_gid;
184 buf->st_rdev = buf64->st_rdev;
185 buf->st_size = buf64->st_size;
186 buf->st_atime = buf64->st_atime;
187 buf->st_mtime = buf64->st_mtime;
188 buf->st_ctime = buf64->st_ctime;
191 static void msvcrt_stat64_to_stati64(const struct _stat64 *buf64, struct _stati64 *buf)
193 buf->st_dev = buf64->st_dev;
194 buf->st_ino = buf64->st_ino;
195 buf->st_mode = buf64->st_mode;
196 buf->st_nlink = buf64->st_nlink;
197 buf->st_uid = buf64->st_uid;
198 buf->st_gid = buf64->st_gid;
199 buf->st_rdev = buf64->st_rdev;
200 buf->st_size = buf64->st_size;
201 buf->st_atime = buf64->st_atime;
202 buf->st_mtime = buf64->st_mtime;
203 buf->st_ctime = buf64->st_ctime;
206 static void msvcrt_stat64_to_stat32(const struct _stat64 *buf64, struct _stat32 *buf)
208 buf->st_dev = buf64->st_dev;
209 buf->st_ino = buf64->st_ino;
210 buf->st_mode = buf64->st_mode;
211 buf->st_nlink = buf64->st_nlink;
212 buf->st_uid = buf64->st_uid;
213 buf->st_gid = buf64->st_gid;
214 buf->st_rdev = buf64->st_rdev;
215 buf->st_size = buf64->st_size;
216 buf->st_atime = buf64->st_atime;
217 buf->st_mtime = buf64->st_mtime;
218 buf->st_ctime = buf64->st_ctime;
221 static void msvcrt_stat64_to_stat64i32(const struct _stat64 *buf64, struct _stat64i32 *buf)
223 buf->st_dev = buf64->st_dev;
224 buf->st_ino = buf64->st_ino;
225 buf->st_mode = buf64->st_mode;
226 buf->st_nlink = buf64->st_nlink;
227 buf->st_uid = buf64->st_uid;
228 buf->st_gid = buf64->st_gid;
229 buf->st_rdev = buf64->st_rdev;
230 buf->st_size = buf64->st_size;
231 buf->st_atime = buf64->st_atime;
232 buf->st_mtime = buf64->st_mtime;
233 buf->st_ctime = buf64->st_ctime;
236 static void msvcrt_stat64_to_stat32i64(const struct _stat64 *buf64, struct _stat32i64 *buf)
238 buf->st_dev = buf64->st_dev;
239 buf->st_ino = buf64->st_ino;
240 buf->st_mode = buf64->st_mode;
241 buf->st_nlink = buf64->st_nlink;
242 buf->st_uid = buf64->st_uid;
243 buf->st_gid = buf64->st_gid;
244 buf->st_rdev = buf64->st_rdev;
245 buf->st_size = buf64->st_size;
246 buf->st_atime = buf64->st_atime;
247 buf->st_mtime = buf64->st_mtime;
248 buf->st_ctime = buf64->st_ctime;
251 static void time_to_filetime( __time64_t time, FILETIME *ft )
253 /* 1601 to 1970 is 369 years plus 89 leap days */
254 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
256 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
257 ft->dwHighDateTime = ticks >> 32;
258 ft->dwLowDateTime = ticks;
261 static inline ioinfo* get_ioinfo_nolock(int fd)
263 ioinfo *ret = NULL;
264 if(fd>=0 && fd<MSVCRT_MAX_FILES)
265 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
266 if(!ret)
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)) {
275 LOCK_FILES();
276 if(!(info->exflag & EF_CRIT_INIT)) {
277 InitializeCriticalSection(&info->crit);
278 info->exflag |= EF_CRIT_INIT;
280 UNLOCK_FILES();
284 static inline ioinfo* get_ioinfo(int fd)
286 ioinfo *ret = get_ioinfo_nolock(fd);
287 if(ret == &MSVCRT___badioinfo)
288 return ret;
289 init_ioinfo_cs(ret);
290 EnterCriticalSection(&ret->crit);
291 return ret;
294 static inline BOOL alloc_pioinfo_block(int fd)
296 ioinfo *block;
297 int i;
299 if(fd<0 || fd>=MSVCRT_MAX_FILES)
301 *_errno() = ENFILE;
302 return FALSE;
305 block = calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
306 if(!block)
308 WARN(":out of memory!\n");
309 *_errno() = ENOMEM;
310 return FALSE;
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))
315 free(block);
316 return TRUE;
319 static inline ioinfo* get_ioinfo_alloc_fd(int fd)
321 ioinfo *ret;
323 ret = get_ioinfo(fd);
324 if(ret != &MSVCRT___badioinfo)
325 return ret;
327 if(!alloc_pioinfo_block(fd))
328 return &MSVCRT___badioinfo;
330 return get_ioinfo(fd);
333 static inline ioinfo* get_ioinfo_alloc(int *fd)
335 int i;
337 *fd = -1;
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)
354 *fd = i;
355 return info;
357 LeaveCriticalSection(&info->crit);
361 WARN(":files exhausted!\n");
362 *_errno() = ENFILE;
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)
374 file_crit *ret;
376 if(i >= MSVCRT_max_streams)
377 return NULL;
379 if(i < _IOB_ENTRIES)
380 return &MSVCRT__iob[i];
382 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
383 if(!ret) {
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");
387 *_errno() = ENOMEM;
388 return NULL;
391 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
392 } else
393 ret += i%MSVCRT_FD_BLOCK_SIZE;
395 return &ret->file;
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;
406 fdinfo->wxflag = 0;
408 TRACE(":fd (%d) freed\n",fd);
410 if (fd < 3)
412 switch (fd)
414 case 0:
415 SetStdHandle(STD_INPUT_HANDLE, 0);
416 break;
417 case 1:
418 SetStdHandle(STD_OUTPUT_HANDLE, 0);
419 break;
420 case 2:
421 SetStdHandle(STD_ERROR_HANDLE, 0);
422 break;
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)
449 int fd;
450 ioinfo *info = get_ioinfo_alloc(&fd);
452 TRACE(":handle (%p) allocating fd (%d)\n", hand, fd);
454 if(info == &MSVCRT___badioinfo)
455 return -1;
457 msvcrt_set_fd(info, hand, flag);
458 release_ioinfo(info);
459 return fd;
462 /* INTERNAL: Allocate a FILE* for an fd slot */
463 /* caller must hold the files lock */
464 static FILE* msvcrt_alloc_fp(void)
466 int i;
467 FILE *file;
469 for (i = 3; i < MSVCRT_max_streams; i++)
471 file = msvcrt_get_file(i);
472 if (!file)
473 return NULL;
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");
484 MSVCRT_stream_idx++;
486 return file;
490 return NULL;
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);
500 *__doserrno() = 0;
501 *_errno() = EBADF;
502 return -1;
504 file->_ptr = file->_base = NULL;
505 file->_cnt = 0;
506 file->_file = fd;
507 file->_flag = stream_flags;
508 file->_tmpfname = NULL;
510 TRACE(":got FILE* (%p)\n",file);
511 return 0;
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)
522 int fd, last_fd;
523 char* wxflag_ptr;
524 HANDLE* handle_ptr;
525 ioinfo* fdinfo;
527 for (last_fd=MSVCRT_MAX_FILES-1; last_fd>=0; last_fd--)
528 if (get_ioinfo_nolock(last_fd)->handle != INVALID_HANDLE_VALUE)
529 break;
530 last_fd++;
532 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * last_fd;
533 *block = calloc(1, *size);
534 if (!*block)
536 *size = 0;
537 return FALSE;
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;
552 else
554 *wxflag_ptr = 0;
555 *handle_ptr = INVALID_HANDLE_VALUE;
557 wxflag_ptr++; handle_ptr++;
559 return TRUE;
562 /* INTERNAL: Set up all file descriptors,
563 * as well as default streams (stdin, stderr and stdout)
565 void msvcrt_init_io(void)
567 STARTUPINFOA si;
568 int i;
569 ioinfo *fdinfo;
571 GetStartupInfoA(&si);
572 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
574 BYTE* wxflag_ptr;
575 HANDLE* handle_ptr;
576 unsigned int count;
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;
606 flags |= WX_TTY;
607 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
608 flags |= WX_TTY;
609 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
610 flags |= WX_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;
625 flags |= WX_TTY;
626 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
627 flags |= WX_TTY;
628 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
629 flags |= WX_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;
644 flags |= WX_TTY;
645 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
646 flags |= WX_TTY;
647 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
648 flags |= WX_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)
674 int ret = 0;
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;
681 ret = EOF;
682 } else if(file->_flag & _IORW) {
683 file->_flag &= ~_IOWRT;
687 file->_ptr=file->_base;
688 file->_cnt=0;
689 return ret;
692 /*********************************************************************
693 * _isatty (MSVCRT.@)
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))
707 return FALSE;
709 file->_base = calloc(1, MSVCRT_INTERNAL_BUFSIZ);
710 if(file->_base) {
711 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
712 file->_flag |= _IOMYBUF;
713 } else {
714 file->_base = (char*)(&file->_charbuf);
715 file->_bufsiz = 2;
716 file->_flag |= _IONBF;
718 file->_ptr = file->_base;
719 file->_cnt = 0;
720 return TRUE;
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))
731 return FALSE;
733 file->_ptr = file->_base = buffers[file->_file == STDOUT_FILENO ? 0 : 1];
734 file->_bufsiz = file->_cnt = BUFSIZ;
735 file->_flag |= MSVCRT__USERBUF;
736 return TRUE;
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)
752 char *p;
753 int n = num;
754 int digits = 0;
756 while (n != 0)
758 n >>= 5;
759 digits++;
761 p = str + digits;
762 *p = 0;
763 while (--p >= str)
765 *p = (num & 31) + '0';
766 if (*p > '9')
767 *p += ('a' - '0' - 10);
768 num >>= 5;
771 return digits;
774 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
775 static int msvcrt_int_to_base32_w(int num, wchar_t *str)
777 wchar_t *p;
778 int n = num;
779 int digits = 0;
781 while (n != 0)
783 n >>= 5;
784 digits++;
786 p = str + digits;
787 *p = 0;
788 while (--p >= str)
790 *p = (num & 31) + '0';
791 if (*p > '9')
792 *p += ('a' - '0' - 10);
793 num >>= 5;
796 return digits;
799 /*********************************************************************
800 * __iob_func (MSVCRT.@)
802 #undef __iob_func
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];
816 #endif
818 /*********************************************************************
819 * _access (MSVCRT.@)
821 int CDECL _access(const char *filename, int mode)
823 DWORD attr = GetFileAttributesA(filename);
825 TRACE("(%s,%d) %ld\n", filename, mode, attr);
827 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
829 msvcrt_set_errno(GetLastError());
830 return -1;
832 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
834 msvcrt_set_errno(ERROR_ACCESS_DENIED);
835 return -1;
837 return 0;
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)
849 return *_errno();
850 return 0;
853 /*********************************************************************
854 * _waccess (MSVCRT.@)
856 int CDECL _waccess(const wchar_t *filename, int mode)
858 DWORD attr = GetFileAttributesW(filename);
860 TRACE("(%s,%d) %ld\n", debugstr_w(filename), mode, attr);
862 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
864 msvcrt_set_errno(GetLastError());
865 return -1;
867 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
869 msvcrt_set_errno(ERROR_ACCESS_DENIED);
870 return -1;
872 return 0;
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)
884 return *_errno();
885 return 0;
888 /*********************************************************************
889 * _chmod (MSVCRT.@)
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))
901 return 0;
903 msvcrt_set_errno(GetLastError());
904 return -1;
907 /*********************************************************************
908 * _wchmod (MSVCRT.@)
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))
920 return 0;
922 msvcrt_set_errno(GetLastError());
923 return -1;
926 /*********************************************************************
927 * _unlink (MSVCRT.@)
929 int CDECL _unlink(const char *path)
931 TRACE("%s\n", debugstr_a(path));
932 if(DeleteFileA(path))
933 return 0;
934 TRACE("failed (%ld)\n", GetLastError());
935 msvcrt_set_errno(GetLastError());
936 return -1;
939 /*********************************************************************
940 * _wunlink (MSVCRT.@)
942 int CDECL _wunlink(const wchar_t *path)
944 TRACE("(%s)\n", debugstr_w(path));
945 if(DeleteFileW(path))
946 return 0;
947 TRACE("failed (%ld)\n", GetLastError());
948 msvcrt_set_errno(GetLastError());
949 return -1;
952 /*********************************************************************
953 * _commit (MSVCRT.@)
955 int CDECL _commit(int fd)
957 ioinfo *info = get_ioinfo(fd);
958 int ret;
960 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
962 if (info->handle == INVALID_HANDLE_VALUE)
963 ret = -1;
964 else if (!FlushFileBuffers(info->handle))
966 if (GetLastError() == ERROR_INVALID_HANDLE)
968 /* FlushFileBuffers fails for console handles
969 * so we ignore this error.
971 ret = 0;
973 else
975 TRACE(":failed-last error (%ld)\n", GetLastError());
976 msvcrt_set_errno(GetLastError());
977 ret = -1;
980 else
982 TRACE(":ok\n");
983 ret = 0;
986 release_ioinfo(info);
987 return ret;
990 /* INTERNAL: Flush all stream buffer */
991 static int msvcrt_flush_all_buffers(int mask)
993 int i, num_flushed = 0;
994 FILE *file;
996 LOCK_FILES();
997 for (i = 0; i < MSVCRT_stream_idx; i++) {
998 file = msvcrt_get_file(i);
1000 if (file->_flag)
1002 if(file->_flag & mask) {
1003 fflush(file);
1004 num_flushed++;
1008 UNLOCK_FILES();
1010 TRACE(":flushed (%d) handles\n",num_flushed);
1011 return num_flushed;
1014 /*********************************************************************
1015 * _flushall (MSVCRT.@)
1017 int CDECL _flushall(void)
1019 return msvcrt_flush_all_buffers(_IOWRT | _IOREAD);
1022 /*********************************************************************
1023 * fflush (MSVCRT.@)
1025 int CDECL fflush(FILE* file)
1027 int ret;
1029 if(!file) {
1030 msvcrt_flush_all_buffers(_IOWRT);
1031 ret = 0;
1032 } else {
1033 _lock_file(file);
1034 ret = _fflush_nolock(file);
1035 _unlock_file(file);
1038 return ret;
1041 /*********************************************************************
1042 * _fflush_nolock (MSVCRT.@)
1044 int CDECL _fflush_nolock(FILE* file)
1046 int res;
1048 if(!file) {
1049 msvcrt_flush_all_buffers(_IOWRT);
1050 return 0;
1053 res = msvcrt_flush_buffer(file);
1054 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
1055 res = _commit(file->_file) ? EOF : 0;
1056 return res;
1059 /*********************************************************************
1060 * _close (MSVCRT.@)
1062 int CDECL _close(int fd)
1064 ioinfo *info = get_ioinfo(fd);
1065 int ret;
1067 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1069 if (fd == MSVCRT_NO_CONSOLE_FD) {
1070 *_errno() = EBADF;
1071 ret = -1;
1072 } else if (!MSVCRT_CHECK_PMT_ERR(info->wxflag & WX_OPEN, EBADF)) {
1073 ret = -1;
1074 } else if (fd == STDOUT_FILENO &&
1075 info->handle == get_ioinfo_nolock(STDERR_FILENO)->handle) {
1076 msvcrt_free_fd(fd);
1077 ret = 0;
1078 } else if (fd == STDERR_FILENO &&
1079 info->handle == get_ioinfo_nolock(STDOUT_FILENO)->handle) {
1080 msvcrt_free_fd(fd);
1081 ret = 0;
1082 } else {
1083 ret = CloseHandle(info->handle) ? 0 : -1;
1084 msvcrt_free_fd(fd);
1085 if (ret) {
1086 WARN(":failed-last error (%ld)\n", GetLastError());
1087 msvcrt_set_errno(GetLastError());
1090 release_ioinfo(info);
1091 return ret;
1094 /*********************************************************************
1095 * _dup2 (MSVCRT.@)
1096 * NOTES
1097 * MSDN isn't clear on this point, but the remarks for _pipe
1098 * indicate file descriptors duplicated with _dup and _dup2 are always
1099 * inheritable.
1101 int CDECL _dup2(int od, int nd)
1103 ioinfo *info_od, *info_nd;
1104 int ret;
1106 TRACE("(od=%d, nd=%d)\n", od, nd);
1108 if (od < nd)
1110 info_od = get_ioinfo(od);
1111 info_nd = get_ioinfo_alloc_fd(nd);
1113 else
1115 info_nd = get_ioinfo_alloc_fd(nd);
1116 info_od = get_ioinfo(od);
1119 if (info_nd == &MSVCRT___badioinfo)
1121 ret = -1;
1123 else if (info_od->wxflag & WX_OPEN)
1125 HANDLE handle;
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)
1133 _close(nd);
1135 msvcrt_set_fd(info_nd, handle, wxflag);
1136 /* _dup2 returns 0, not nd, on success */
1137 ret = 0;
1139 else
1141 ret = -1;
1142 msvcrt_set_errno(GetLastError());
1145 else
1147 *_errno() = EBADF;
1148 ret = -1;
1151 release_ioinfo(info_od);
1152 release_ioinfo(info_nd);
1153 return ret;
1156 /*********************************************************************
1157 * _dup (MSVCRT.@)
1159 int CDECL _dup(int od)
1161 int fd, ret;
1162 ioinfo *info = get_ioinfo_alloc(&fd);
1164 if (_dup2(od, fd) == 0)
1165 ret = fd;
1166 else
1167 ret = -1;
1168 release_ioinfo(info);
1169 return ret;
1172 /*********************************************************************
1173 * _eof (MSVCRT.@)
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);
1186 return -1;
1189 if (info->wxflag & WX_ATEOF)
1191 release_ioinfo(info);
1192 return TRUE;
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);
1204 return TRUE;
1207 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1208 release_ioinfo(info);
1209 return FALSE;
1212 /*********************************************************************
1213 * _fcloseall (MSVCRT.@)
1215 int CDECL _fcloseall(void)
1217 int num_closed = 0, i;
1218 FILE *file;
1220 LOCK_FILES();
1221 for (i = 3; i < MSVCRT_stream_idx; i++) {
1222 file = msvcrt_get_file(i);
1224 if (file->_flag && !fclose(file))
1225 num_closed++;
1227 UNLOCK_FILES();
1229 TRACE(":closed (%d) handles\n",num_closed);
1230 return num_closed;
1233 /* free everything on process exit */
1234 void msvcrt_free_io(void)
1236 unsigned int i;
1237 int j;
1239 _flushall();
1240 _fcloseall();
1242 for(i=0; i<ARRAY_SIZE(MSVCRT___pioinfo); i++)
1244 if(!MSVCRT___pioinfo[i])
1245 continue;
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);
1275 LARGE_INTEGER ofs;
1277 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1279 if (info->handle == INVALID_HANDLE_VALUE)
1281 *_errno() = EBADF;
1282 release_ioinfo(info);
1283 return -1;
1286 if (whence < 0 || whence > 2)
1288 release_ioinfo(info);
1289 *_errno() = EINVAL;
1290 return -1;
1293 TRACE(":fd (%d) to %#I64x pos %s\n",
1294 fd, offset, (whence == SEEK_SET) ? "SEEK_SET" :
1295 (whence == SEEK_CUR) ? "SEEK_CUR" :
1296 (whence == SEEK_END) ? "SEEK_END" : "UNKNOWN");
1298 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1299 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1300 ofs.QuadPart = offset;
1301 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1302 GetLastError() == ERROR_SUCCESS)
1304 info->wxflag &= ~WX_ATEOF;
1305 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1307 release_ioinfo(info);
1308 return ofs.QuadPart;
1310 release_ioinfo(info);
1311 TRACE(":error-last error (%ld)\n", GetLastError());
1312 msvcrt_set_errno(GetLastError());
1313 return -1;
1316 /*********************************************************************
1317 * _lseek (MSVCRT.@)
1319 __msvcrt_long CDECL _lseek(int fd, __msvcrt_long offset, int whence)
1321 return _lseeki64(fd, offset, whence);
1324 /*********************************************************************
1325 * _lock_file (MSVCRT.@)
1327 void CDECL _lock_file(FILE *file)
1329 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1330 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1331 else
1332 EnterCriticalSection(&((file_crit*)file)->crit);
1335 /*********************************************************************
1336 * _unlock_file (MSVCRT.@)
1338 void CDECL _unlock_file(FILE *file)
1340 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1341 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1342 else
1343 LeaveCriticalSection(&((file_crit*)file)->crit);
1346 /*********************************************************************
1347 * _locking (MSVCRT.@)
1349 * This is untested; the underlying LockFile doesn't work yet.
1351 int CDECL _locking(int fd, int mode, __msvcrt_long nbytes)
1353 ioinfo *info = get_ioinfo(fd);
1354 BOOL ret;
1355 DWORD cur_locn;
1357 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1358 if (info->handle == INVALID_HANDLE_VALUE)
1360 release_ioinfo(info);
1361 return -1;
1364 if (mode < 0 || mode > 4)
1366 release_ioinfo(info);
1367 *_errno() = EINVAL;
1368 return -1;
1371 TRACE(":fd (%d) by %#lx mode %s\n",
1372 fd, nbytes, (mode == _LK_UNLCK) ? "_LK_UNLCK" :
1373 (mode == _LK_LOCK) ? "_LK_LOCK" :
1374 (mode == _LK_NBLCK) ? "_LK_NBLCK" :
1375 (mode == _LK_RLCK) ? "_LK_RLCK" :
1376 (mode == _LK_NBRLCK) ? "_LK_NBRLCK" :
1377 "UNKNOWN");
1379 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1381 release_ioinfo(info);
1382 FIXME("Seek failed\n");
1383 *_errno() = EINVAL; /* FIXME */
1384 return -1;
1386 if (mode == _LK_LOCK || mode == _LK_RLCK)
1388 int nretry = 10;
1389 ret = 1; /* just to satisfy gcc */
1390 while (nretry--)
1392 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1393 if (ret) break;
1394 Sleep(1);
1397 else if (mode == _LK_UNLCK)
1398 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1399 else
1400 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1401 /* FIXME - what about error settings? */
1402 release_ioinfo(info);
1403 return ret ? 0 : -1;
1406 /*********************************************************************
1407 * _fseeki64 (MSVCRT.@)
1409 int CDECL _fseeki64(FILE* file, __int64 offset, int whence)
1411 int ret;
1413 _lock_file(file);
1414 ret = _fseeki64_nolock(file, offset, whence);
1415 _unlock_file(file);
1417 return ret;
1420 /*********************************************************************
1421 * _fseeki64_nolock (MSVCRT.@)
1423 int CDECL _fseeki64_nolock(FILE* file, __int64 offset, int whence)
1425 int ret;
1427 if(whence == SEEK_CUR && file->_flag & _IOREAD ) {
1428 whence = SEEK_SET;
1429 offset += _ftelli64_nolock(file);
1432 /* Flush output if needed */
1433 msvcrt_flush_buffer(file);
1434 /* Reset direction of i/o */
1435 if(file->_flag & _IORW) {
1436 file->_flag &= ~(_IOREAD|_IOWRT);
1438 /* Clear end of file flag */
1439 file->_flag &= ~_IOEOF;
1440 ret = (_lseeki64(file->_file,offset,whence) == -1)?-1:0;
1442 return ret;
1445 /*********************************************************************
1446 * fseek (MSVCRT.@)
1448 int CDECL fseek(FILE* file, __msvcrt_long offset, int whence)
1450 return _fseeki64( file, offset, whence );
1453 /*********************************************************************
1454 * _fseek_nolock (MSVCRT.@)
1456 int CDECL _fseek_nolock(FILE* file, __msvcrt_long offset, int whence)
1458 return _fseeki64_nolock( file, offset, whence );
1461 /*********************************************************************
1462 * _chsize_s (MSVCRT.@)
1464 int CDECL _chsize_s(int fd, __int64 size)
1466 ioinfo *info;
1467 __int64 cur, pos;
1468 BOOL ret = FALSE;
1470 TRACE("(fd=%d, size=%#I64x)\n", fd, size);
1472 if (!MSVCRT_CHECK_PMT(size >= 0)) return EINVAL;
1475 info = get_ioinfo(fd);
1476 if (info->handle != INVALID_HANDLE_VALUE)
1478 /* save the current file pointer */
1479 cur = _lseeki64(fd, 0, SEEK_CUR);
1480 if (cur >= 0)
1482 pos = _lseeki64(fd, size, SEEK_SET);
1483 if (pos >= 0)
1485 ret = SetEndOfFile(info->handle);
1486 if (!ret) msvcrt_set_errno(GetLastError());
1489 /* restore the file pointer */
1490 _lseeki64(fd, cur, SEEK_SET);
1494 release_ioinfo(info);
1495 return ret ? 0 : *_errno();
1498 /*********************************************************************
1499 * _chsize (MSVCRT.@)
1501 int CDECL _chsize(int fd, __msvcrt_long size)
1503 /* _chsize_s returns errno on failure but _chsize should return -1 */
1504 return _chsize_s( fd, size ) == 0 ? 0 : -1;
1507 /*********************************************************************
1508 * clearerr (MSVCRT.@)
1510 void CDECL clearerr(FILE* file)
1512 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1514 _lock_file(file);
1515 file->_flag &= ~(_IOERR | _IOEOF);
1516 _unlock_file(file);
1519 /*********************************************************************
1520 * clearerr_s (MSVCRT.@)
1522 int CDECL clearerr_s(FILE* file)
1524 TRACE(":file (%p)\n",file);
1526 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
1528 _lock_file(file);
1529 file->_flag &= ~(_IOERR | _IOEOF);
1530 _unlock_file(file);
1531 return 0;
1534 /*********************************************************************
1535 * rewind (MSVCRT.@)
1537 void CDECL rewind(FILE* file)
1539 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1541 _lock_file(file);
1542 _fseek_nolock(file, 0L, SEEK_SET);
1543 clearerr(file);
1544 _unlock_file(file);
1547 static int msvcrt_get_flags(const wchar_t* mode, int *open_flags, int* stream_flags)
1549 int plus = wcschr(mode, '+') != NULL;
1551 TRACE("%s\n", debugstr_w(mode));
1553 while(*mode == ' ') mode++;
1555 switch(*mode++)
1557 case 'R': case 'r':
1558 *open_flags = plus ? _O_RDWR : _O_RDONLY;
1559 *stream_flags = plus ? _IORW : _IOREAD;
1560 break;
1561 case 'W': case 'w':
1562 *open_flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1563 *stream_flags = plus ? _IORW : _IOWRT;
1564 break;
1565 case 'A': case 'a':
1566 *open_flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1567 *stream_flags = plus ? _IORW : _IOWRT;
1568 break;
1569 default:
1570 MSVCRT_INVALID_PMT(0, EINVAL);
1571 return -1;
1574 *stream_flags |= MSVCRT__commode;
1576 while (*mode && *mode!=',')
1577 switch (*mode++)
1579 case 'B': case 'b':
1580 *open_flags |= _O_BINARY;
1581 *open_flags &= ~_O_TEXT;
1582 break;
1583 case 't':
1584 *open_flags |= _O_TEXT;
1585 *open_flags &= ~_O_BINARY;
1586 break;
1587 #if _MSVCR_VER>=140
1588 case 'x':
1589 if(!MSVCRT_CHECK_PMT((*open_flags & (_O_CREAT | _O_APPEND)) == _O_CREAT))
1590 return -1;
1591 *open_flags |= _O_EXCL;
1592 break;
1593 #endif
1594 case 'D':
1595 *open_flags |= _O_TEMPORARY;
1596 break;
1597 case 'T':
1598 *open_flags |= _O_SHORT_LIVED;
1599 break;
1600 case 'c':
1601 *stream_flags |= MSVCRT__IOCOMMIT;
1602 break;
1603 case 'n':
1604 *stream_flags &= ~MSVCRT__IOCOMMIT;
1605 break;
1606 case 'N':
1607 *open_flags |= _O_NOINHERIT;
1608 break;
1609 case '+':
1610 case ' ':
1611 case 'a':
1612 case 'w':
1613 break;
1614 case 'S':
1615 if (!(*open_flags & _O_RANDOM))
1616 *open_flags |= _O_SEQUENTIAL;
1617 break;
1618 case 'R':
1619 if (!(*open_flags & _O_SEQUENTIAL))
1620 *open_flags |= _O_RANDOM;
1621 break;
1622 default:
1623 ERR("incorrect mode flag: %c\n", mode[-1]);
1624 break;
1627 if(*mode == ',')
1629 mode++;
1630 while(*mode == ' ') mode++;
1631 if(!MSVCRT_CHECK_PMT(!wcsncmp(L"ccs", mode, 3)))
1632 return -1;
1633 mode += 3;
1634 while(*mode == ' ') mode++;
1635 if(!MSVCRT_CHECK_PMT(*mode == '='))
1636 return -1;
1637 mode++;
1638 while(*mode == ' ') mode++;
1640 if(!_wcsnicmp(L"utf-8", mode, 5))
1642 *open_flags |= _O_U8TEXT;
1643 mode += 5;
1645 else if(!_wcsnicmp(L"utf-16le", mode, 8))
1647 *open_flags |= _O_U16TEXT;
1648 mode += 8;
1650 else if(!_wcsnicmp(L"unicode", mode, 7))
1652 *open_flags |= _O_WTEXT;
1653 mode += 7;
1655 else
1657 MSVCRT_INVALID_PMT(0, EINVAL);
1658 return -1;
1661 while(*mode == ' ') mode++;
1664 if(!MSVCRT_CHECK_PMT(*mode == 0))
1665 return -1;
1666 return 0;
1669 /*********************************************************************
1670 * _fdopen (MSVCRT.@)
1672 FILE* CDECL _fdopen(int fd, const char *mode)
1674 FILE *ret;
1675 wchar_t *modeW = NULL;
1677 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1679 ret = _wfdopen(fd, modeW);
1681 free(modeW);
1682 return ret;
1685 /*********************************************************************
1686 * _wfdopen (MSVCRT.@)
1688 FILE* CDECL _wfdopen(int fd, const wchar_t *mode)
1690 int open_flags, stream_flags;
1691 FILE* file;
1693 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1695 LOCK_FILES();
1696 if (!(file = msvcrt_alloc_fp()))
1697 file = NULL;
1698 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1700 file->_flag = 0;
1701 file = NULL;
1703 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1704 UNLOCK_FILES();
1706 return file;
1709 /*********************************************************************
1710 * _filelength (MSVCRT.@)
1712 __msvcrt_long CDECL _filelength(int fd)
1714 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1715 if (curPos != -1)
1717 LONG endPos = _lseek(fd, 0, SEEK_END);
1718 if (endPos != -1)
1720 if (endPos != curPos)
1721 _lseek(fd, curPos, SEEK_SET);
1722 return endPos;
1725 return -1;
1728 /*********************************************************************
1729 * _filelengthi64 (MSVCRT.@)
1731 __int64 CDECL _filelengthi64(int fd)
1733 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1734 if (curPos != -1)
1736 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1737 if (endPos != -1)
1739 if (endPos != curPos)
1740 _lseeki64(fd, curPos, SEEK_SET);
1741 return endPos;
1744 return -1;
1747 /*********************************************************************
1748 * _fileno (MSVCRT.@)
1750 int CDECL _fileno(FILE* file)
1752 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1753 return file->_file;
1756 /*********************************************************************
1757 * _fstat64 (MSVCRT.@)
1759 int CDECL _fstat64(int fd, struct _stat64* buf)
1761 ioinfo *info = get_ioinfo(fd);
1762 DWORD dw;
1763 DWORD type;
1765 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1766 if (info->handle == INVALID_HANDLE_VALUE)
1768 release_ioinfo(info);
1769 return -1;
1772 if (!buf)
1774 WARN(":failed-NULL buf\n");
1775 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1776 release_ioinfo(info);
1777 return -1;
1780 memset(buf, 0, sizeof(struct _stat64));
1781 type = GetFileType(info->handle);
1782 if (type == FILE_TYPE_PIPE)
1784 buf->st_dev = buf->st_rdev = fd;
1785 buf->st_mode = _S_IFIFO;
1786 buf->st_nlink = 1;
1788 else if (type == FILE_TYPE_CHAR)
1790 buf->st_dev = buf->st_rdev = fd;
1791 buf->st_mode = _S_IFCHR;
1792 buf->st_nlink = 1;
1794 else /* FILE_TYPE_DISK etc. */
1796 FILE_BASIC_INFORMATION basic_info;
1797 FILE_STANDARD_INFORMATION std_info;
1798 IO_STATUS_BLOCK io;
1799 NTSTATUS status;
1801 if ((status = NtQueryInformationFile( info->handle, &io, &basic_info, sizeof(basic_info), FileBasicInformation )) ||
1802 (status = NtQueryInformationFile( info->handle, &io, &std_info, sizeof(std_info), FileStandardInformation )))
1804 WARN(":failed-error %lx\n", status);
1805 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1806 release_ioinfo(info);
1807 return -1;
1809 buf->st_mode = _S_IFREG | 0444;
1810 if (!(basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY))
1811 buf->st_mode |= 0222;
1812 buf->st_size = std_info.EndOfFile.QuadPart;
1813 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&basic_info.LastAccessTime, &dw);
1814 buf->st_atime = dw;
1815 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&basic_info.LastWriteTime, &dw);
1816 buf->st_mtime = buf->st_ctime = dw;
1817 buf->st_nlink = std_info.NumberOfLinks;
1818 TRACE(":dwFileAttributes = %#lx, mode set to %#x\n",
1819 basic_info.FileAttributes, buf->st_mode);
1821 release_ioinfo(info);
1822 return 0;
1825 /*********************************************************************
1826 * _fstati64 (MSVCRT.@)
1828 int CDECL _fstati64(int fd, struct _stati64* buf)
1830 int ret;
1831 struct _stat64 buf64;
1833 ret = _fstat64(fd, &buf64);
1834 if (!ret)
1835 msvcrt_stat64_to_stati64(&buf64, buf);
1836 return ret;
1839 /*********************************************************************
1840 * _fstat (MSVCRT.@)
1842 int CDECL _fstat(int fd, struct _stat* buf)
1843 { int ret;
1844 struct _stat64 buf64;
1846 ret = _fstat64(fd, &buf64);
1847 if (!ret)
1848 msvcrt_stat64_to_stat(&buf64, buf);
1849 return ret;
1852 /*********************************************************************
1853 * _fstat32 (MSVCR80.@)
1855 int CDECL _fstat32(int fd, struct _stat32* buf)
1857 int ret;
1858 struct _stat64 buf64;
1860 ret = _fstat64(fd, &buf64);
1861 if (!ret)
1862 msvcrt_stat64_to_stat32(&buf64, buf);
1863 return ret;
1866 /*********************************************************************
1867 * _fstat32i64 (MSVCR80.@)
1869 int CDECL _fstat32i64(int fd, struct _stat32i64* buf)
1871 int ret;
1872 struct _stat64 buf64;
1874 ret = _fstat64(fd, &buf64);
1875 if (!ret)
1876 msvcrt_stat64_to_stat32i64(&buf64, buf);
1877 return ret;
1880 /*********************************************************************
1881 * _fstat64i32 (MSVCR80.@)
1883 int CDECL _fstat64i32(int fd, struct _stat64i32* buf)
1885 int ret;
1886 struct _stat64 buf64;
1888 ret = _fstat64(fd, &buf64);
1889 if (!ret)
1890 msvcrt_stat64_to_stat64i32(&buf64, buf);
1891 return ret;
1894 /*********************************************************************
1895 * _futime64 (MSVCRT.@)
1897 int CDECL _futime64(int fd, struct __utimbuf64 *t)
1899 ioinfo *info = get_ioinfo(fd);
1900 FILETIME at, wt;
1902 if (!t)
1904 time_to_filetime( _time64(NULL), &at );
1905 wt = at;
1907 else
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());
1917 return -1 ;
1919 release_ioinfo(info);
1920 return 0;
1923 /*********************************************************************
1924 * _futime32 (MSVCRT.@)
1926 int CDECL _futime32(int fd, struct __utimbuf32 *t)
1928 if (t)
1930 struct __utimbuf64 t64;
1931 t64.actime = t->actime;
1932 t64.modtime = t->modtime;
1933 return _futime64( fd, &t64 );
1935 else
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)
1948 *_errno() = EBADF;
1949 return (intptr_t)hand;
1952 /*********************************************************************
1953 * _mktemp_s (MSVCRT.@)
1955 int CDECL _mktemp_s(char *pattern, size_t size)
1957 DWORD len, xno, id;
1959 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1960 return EINVAL;
1962 for(len=0; len<size; len++)
1963 if(!pattern[len])
1964 break;
1965 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1966 if(size)
1967 pattern[0] = 0;
1968 return EINVAL;
1971 for(xno=1; xno<=6; xno++)
1972 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1973 return EINVAL;
1975 id = GetCurrentProcessId();
1976 for(xno=1; xno<6; xno++) {
1977 pattern[len-xno] = id%10 + '0';
1978 id /= 10;
1981 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1982 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1983 return 0;
1986 pattern[0] = 0;
1987 *_errno() = EEXIST;
1988 return EEXIST;
1991 /*********************************************************************
1992 * _mktemp (MSVCRT.@)
1994 char * CDECL _mktemp(char *pattern)
1996 int numX = 0;
1997 char *retVal = pattern;
1998 int id;
1999 char letter = 'a';
2001 if(!pattern)
2002 return NULL;
2004 while(*pattern)
2005 numX = (*pattern++ == 'X')? numX + 1 : 0;
2006 if (numX < 6)
2007 return NULL;
2008 pattern--;
2009 id = GetCurrentProcessId();
2010 numX = 6;
2011 while(numX--)
2013 int tempNum = id / 10;
2014 *pattern-- = id - (tempNum * 10) + '0';
2015 id = tempNum;
2017 pattern++;
2020 *pattern = letter++;
2021 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
2022 return retVal;
2023 } while(letter <= 'z');
2024 return NULL;
2027 /*********************************************************************
2028 * _wmktemp_s (MSVCRT.@)
2030 int CDECL _wmktemp_s(wchar_t *pattern, size_t size)
2032 DWORD len, xno, id;
2034 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
2035 return EINVAL;
2037 for(len=0; len<size; len++)
2038 if(!pattern[len])
2039 break;
2040 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
2041 if(size)
2042 pattern[0] = 0;
2043 return EINVAL;
2046 for(xno=1; xno<=6; xno++)
2047 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
2048 return EINVAL;
2050 id = GetCurrentProcessId();
2051 for(xno=1; xno<6; xno++) {
2052 pattern[len-xno] = id%10 + '0';
2053 id /= 10;
2056 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
2057 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
2058 return 0;
2061 pattern[0] = 0;
2062 *_errno() = EEXIST;
2063 return EEXIST;
2066 /*********************************************************************
2067 * _wmktemp (MSVCRT.@)
2069 wchar_t * CDECL _wmktemp(wchar_t *pattern)
2071 int numX = 0;
2072 wchar_t *retVal = pattern;
2073 int id;
2074 wchar_t letter = 'a';
2076 if(!pattern)
2077 return NULL;
2079 while(*pattern)
2080 numX = (*pattern++ == 'X')? numX + 1 : 0;
2081 if (numX < 6)
2082 return NULL;
2083 pattern--;
2084 id = GetCurrentProcessId();
2085 numX = 6;
2086 while(numX--)
2088 int tempNum = id / 10;
2089 *pattern-- = id - (tempNum * 10) + '0';
2090 id = tempNum;
2092 pattern++;
2095 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
2096 return retVal;
2097 *pattern = letter++;
2098 } while(letter != '|');
2099 return NULL;
2102 static unsigned split_oflags(unsigned oflags)
2104 int wxflags = 0;
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;
2113 else
2115 int fmode;
2116 _get_fmode(&fmode);
2117 if (!(fmode & _O_BINARY)) wxflags |= WX_TEXT; /* default to TEXT*/
2119 if (oflags & _O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2121 if ((unsupp = oflags & ~(_O_BINARY | _O_TEXT | _O_APPEND | _O_TRUNC | _O_EXCL | _O_CREAT |
2122 _O_RDWR | _O_WRONLY | _O_TEMPORARY | _O_NOINHERIT | _O_SEQUENTIAL |
2123 _O_RANDOM | _O_SHORT_LIVED | _O_WTEXT | _O_U16TEXT | _O_U8TEXT)))
2124 ERR(":unsupported oflags %#x\n",unsupp);
2126 return wxflags;
2129 /*********************************************************************
2130 * _pipe (MSVCRT.@)
2132 int CDECL _pipe(int *pfds, unsigned int psize, int textmode)
2134 int ret = -1;
2135 SECURITY_ATTRIBUTES sa;
2136 HANDLE readHandle, writeHandle;
2138 if (!pfds)
2140 *_errno() = EINVAL;
2141 return -1;
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);
2150 int fd;
2152 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2153 if (fd != -1)
2155 pfds[0] = fd;
2156 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2157 if (fd != -1)
2159 pfds[1] = fd;
2160 ret = 0;
2162 else
2164 _close(pfds[0]);
2165 CloseHandle(writeHandle);
2166 *_errno() = EMFILE;
2169 else
2171 CloseHandle(readHandle);
2172 CloseHandle(writeHandle);
2173 *_errno() = EMFILE;
2176 else
2177 msvcrt_set_errno(GetLastError());
2179 return ret;
2182 static int check_bom(HANDLE h, int oflags, BOOL seek)
2184 char bom[sizeof(utf8_bom)];
2185 DWORD r;
2187 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2188 return oflags;
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))) {
2193 if (seek && r>2)
2194 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2195 oflags = (oflags & ~(_O_WTEXT | _O_U8TEXT)) | _O_U16TEXT;
2196 }else if (seek) {
2197 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2200 return oflags;
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;
2212 int wxflag;
2213 HANDLE hand;
2215 TRACE("path: (%s) oflags: %#x shflags: %#x pmode: %#x fd*: %p secure: %d\n",
2216 debugstr_w(path), oflags, shflags, pmode, fd, secure);
2218 if (!MSVCRT_CHECK_PMT( fd != NULL )) return EINVAL;
2220 *fd = -1;
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))))
2232 return EINVAL;
2234 if (oflags & _O_EXCL)
2235 creation = CREATE_NEW;
2236 else if (oflags & _O_TRUNC)
2237 creation = CREATE_ALWAYS;
2238 else
2239 creation = OPEN_ALWAYS;
2241 else /* no _O_CREAT */
2243 if (oflags & _O_TRUNC)
2244 creation = TRUNCATE_EXISTING;
2245 else
2246 creation = OPEN_EXISTING;
2249 switch( shflags )
2251 case _SH_DENYRW:
2252 sharing = 0L;
2253 break;
2254 case _SH_DENYWR:
2255 sharing = FILE_SHARE_READ;
2256 break;
2257 case _SH_DENYRD:
2258 sharing = FILE_SHARE_WRITE;
2259 break;
2260 case _SH_DENYNO:
2261 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2262 break;
2263 default:
2264 ERR( "Unhandled shflags %#x\n", shflags );
2265 return EINVAL;
2268 if (!(pmode & ~MSVCRT_umask & _S_IWRITE))
2269 attrib = FILE_ATTRIBUTE_READONLY;
2270 else
2271 attrib = FILE_ATTRIBUTE_NORMAL;
2273 if (oflags & _O_TEMPORARY)
2275 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2276 access |= DELETE;
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);
2300 CloseHandle(hand);
2304 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2305 if (hand == INVALID_HANDLE_VALUE) {
2306 WARN(":failed-last error (%ld)\n", GetLastError());
2307 msvcrt_set_errno(GetLastError());
2308 return *_errno();
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))
2328 written += tmp;
2329 if (written != sizeof(utf8_bom)) {
2330 WARN("error writing BOM\n");
2331 CloseHandle(hand);
2332 msvcrt_set_errno(GetLastError());
2333 return *_errno();
2336 else
2338 DWORD written = 0, tmp;
2340 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2341 sizeof(utf16_bom)-written, &tmp, NULL))
2342 written += tmp;
2343 if (written != sizeof(utf16_bom))
2345 WARN("error writing BOM\n");
2346 CloseHandle(hand);
2347 msvcrt_set_errno(GetLastError());
2348 return *_errno();
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)
2359 wxflag |= WX_TTY;
2360 else if (type == FILE_TYPE_PIPE)
2361 wxflag |= WX_PIPE;
2363 *fd = msvcrt_alloc_fd(hand, wxflag);
2364 if (*fd == -1)
2365 return *_errno();
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);
2376 return 0;
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, ... )
2393 int pmode;
2394 int fd;
2396 if (oflags & _O_CREAT)
2398 va_list ap;
2400 va_start(ap, shflags);
2401 pmode = va_arg(ap, int);
2402 va_end(ap);
2404 else
2405 pmode = 0;
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)
2417 wchar_t *pathW;
2418 int ret;
2420 if (!MSVCRT_CHECK_PMT(fd != NULL))
2421 return EINVAL;
2422 *fd = -1;
2423 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2424 return EINVAL;
2426 ret = _wsopen_dispatch(pathW, oflags, shflags, pmode, fd, secure);
2427 free(pathW);
2428 return ret;
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 /*********************************************************************
2440 * _sopen (MSVCRT.@)
2442 int WINAPIV _sopen( const char *path, int oflags, int shflags, ... )
2444 int pmode;
2445 int fd;
2447 if (oflags & _O_CREAT)
2449 va_list ap;
2451 va_start(ap, shflags);
2452 pmode = va_arg(ap, int);
2453 va_end(ap);
2455 else
2456 pmode = 0;
2458 return _sopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2461 /*********************************************************************
2462 * _open (MSVCRT.@)
2464 int WINAPIV _open( const char *path, int flags, ... )
2466 va_list ap;
2468 if (flags & _O_CREAT)
2470 int pmode;
2471 va_start(ap, flags);
2472 pmode = va_arg(ap, int);
2473 va_end(ap);
2474 return _sopen( path, flags, _SH_DENYNO, pmode );
2476 else
2477 return _sopen( path, flags, _SH_DENYNO);
2480 /*********************************************************************
2481 * _wopen (MSVCRT.@)
2483 int WINAPIV _wopen(const wchar_t *path,int flags,...)
2485 va_list ap;
2487 if (flags & _O_CREAT)
2489 int pmode;
2490 va_start(ap, flags);
2491 pmode = va_arg(ap, int);
2492 va_end(ap);
2493 return _wsopen( path, flags, _SH_DENYNO, pmode );
2495 else
2496 return _wsopen( path, flags, _SH_DENYNO);
2499 /*********************************************************************
2500 * _creat (MSVCRT.@)
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)
2522 DWORD flags;
2523 int fd;
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());
2538 return -1;
2541 if (flags == FILE_TYPE_CHAR)
2542 flags = WX_TTY;
2543 else if (flags == FILE_TYPE_PIPE)
2544 flags = WX_PIPE;
2545 else
2546 flags = 0;
2547 flags |= split_oflags(oflags);
2549 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2550 TRACE(":handle (%Iu) fd (%d) flags %#lx\n", handle, fd, flags);
2551 return fd;
2554 /*********************************************************************
2555 * _rmtmp (MSVCRT.@)
2557 int CDECL _rmtmp(void)
2559 int num_removed = 0, i;
2560 FILE *file;
2562 LOCK_FILES();
2563 for (i = 3; i < MSVCRT_stream_idx; i++) {
2564 file = msvcrt_get_file(i);
2566 if (file->_tmpfname)
2568 fclose(file);
2569 num_removed++;
2572 UNLOCK_FILES();
2574 if (num_removed)
2575 TRACE(":removed (%d) temp files\n",num_removed);
2576 return num_removed;
2579 static inline int get_utf8_char_len(char ch)
2581 if((ch&0xf8) == 0xf0)
2582 return 4;
2583 else if((ch&0xf0) == 0xe0)
2584 return 3;
2585 else if((ch&0xe0) == 0xc0)
2586 return 2;
2587 return 1;
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 */
2601 count /= 2;
2602 readbuf_size = count < 4 ? 4 : count;
2603 if(readbuf_size<=4 || !(readbuf = malloc(readbuf_size))) {
2604 readbuf_size = 4;
2605 readbuf = min_buf;
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
2626 if(count < 4) {
2627 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2628 if (GetLastError() == ERROR_BROKEN_PIPE) {
2629 fdinfo->wxflag |= WX_ATEOF;
2630 return 0;
2631 }else {
2632 msvcrt_set_errno(GetLastError());
2633 return -1;
2635 }else if(!num_read) {
2636 fdinfo->wxflag |= WX_ATEOF;
2637 return 0;
2638 }else {
2639 pos++;
2642 char_len = get_utf8_char_len(readbuf[0]);
2643 if(char_len>pos) {
2644 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2645 pos += num_read;
2648 if(readbuf[0] == '\n')
2649 fdinfo->wxflag |= WX_READNL;
2650 else
2651 fdinfo->wxflag &= ~WX_READNL;
2653 if(readbuf[0] == 0x1a) {
2654 fdinfo->wxflag |= WX_ATEOF;
2655 return 0;
2658 if(readbuf[0] == '\r') {
2659 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2660 buf[0] = '\r';
2661 else if(lookahead == '\n')
2662 buf[0] = '\n';
2663 else {
2664 buf[0] = '\r';
2665 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2666 fdinfo->lookahead[0] = lookahead;
2667 else
2668 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2670 return 2;
2673 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2674 msvcrt_set_errno(GetLastError());
2675 return -1;
2678 return num_read*2;
2681 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2682 if(pos) {
2683 num_read = 0;
2684 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2685 fdinfo->wxflag |= WX_ATEOF;
2686 if (readbuf != min_buf) free(readbuf);
2687 return 0;
2688 }else {
2689 msvcrt_set_errno(GetLastError());
2690 if (readbuf != min_buf) free(readbuf);
2691 return -1;
2693 }else if(!pos && !num_read) {
2694 fdinfo->wxflag |= WX_ATEOF;
2695 if (readbuf != min_buf) free(readbuf);
2696 return 0;
2699 pos += num_read;
2700 if(readbuf[0] == '\n')
2701 fdinfo->wxflag |= WX_READNL;
2702 else
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)
2708 break;
2709 char_len = get_utf8_char_len(readbuf[i]);
2710 if(char_len+i <= pos)
2711 i += char_len;
2713 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2714 if(i < pos)
2715 fdinfo->lookahead[0] = readbuf[i];
2716 if(i+1 < pos)
2717 fdinfo->lookahead[1] = readbuf[i+1];
2718 if(i+2 < pos)
2719 fdinfo->lookahead[2] = readbuf[i+2];
2720 }else if(i < pos) {
2721 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2723 pos = i;
2725 for(i=0, j=0; i<pos; i++) {
2726 if(readbuf[i] == 0x1a) {
2727 fdinfo->wxflag |= WX_ATEOF;
2728 break;
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';
2737 }else {
2738 if(lookahead != '\n')
2739 readbuf[j++] = '\r';
2741 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2742 fdinfo->lookahead[0] = lookahead;
2743 else
2744 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2746 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2747 readbuf[j++] = readbuf[i];
2750 pos = j;
2752 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2753 msvcrt_set_errno(GetLastError());
2754 if (readbuf != min_buf) free(readbuf);
2755 return -1;
2758 if (readbuf != min_buf) free(readbuf);
2759 return num_read*2;
2762 /*********************************************************************
2763 * (internal) read_i
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
2767 * the following \n
2769 static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
2771 DWORD num_read, utf16;
2772 char *bufstart = buf;
2774 if (count == 0)
2775 return 0;
2777 if (fdinfo->wxflag & WX_ATEOF) {
2778 TRACE("already at EOF, returning 0\n");
2779 return 0;
2781 /* Don't trace small reads, it gets *very* annoying */
2782 if (count > 4)
2783 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2784 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2786 *_errno() = EBADF;
2787 return -1;
2790 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2791 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2793 *_errno() = EINVAL;
2794 return -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';
2807 if (utf16)
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;
2815 else
2816 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");
2824 num_read--;
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)
2834 DWORD i, j;
2836 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2837 fdinfo->wxflag |= WX_READNL;
2838 else
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));
2848 break;
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)
2854 char lookahead[2];
2855 DWORD len;
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;
2865 else
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;
2880 else
2882 fdinfo->lookahead[0] = lookahead[0];
2883 fdinfo->lookahead[1] = lookahead[1];
2886 else
2887 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2890 else
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];
2903 num_read = j;
2906 else
2908 if (GetLastError() == ERROR_BROKEN_PIPE)
2910 TRACE(":end-of-pipe\n");
2911 fdinfo->wxflag |= WX_ATEOF;
2912 return 0;
2914 else
2916 TRACE(":failed-last error (%ld)\n", GetLastError());
2917 msvcrt_set_errno(GetLastError());
2918 return -1;
2922 if (count > 4)
2923 TRACE("(%lu), %s\n", num_read, debugstr_an(buf, num_read));
2924 return num_read;
2927 /*********************************************************************
2928 * _read (MSVCRT.@)
2930 int CDECL _read(int fd, void *buf, unsigned int count)
2932 ioinfo *info;
2933 int num_read;
2935 if(fd == MSVCRT_NO_CONSOLE_FD) {
2936 *_errno() = EBADF;
2937 return -1;
2940 info = get_ioinfo(fd);
2941 num_read = read_i(fd, info, buf, count);
2942 release_ioinfo(info);
2943 return num_read;
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)))
2954 ret = _O_WTEXT;
2956 if(mode!=_O_TEXT && mode!=_O_BINARY && mode!=_O_WTEXT
2957 && mode!=_O_U16TEXT && mode!=_O_U8TEXT) {
2958 *_errno() = EINVAL;
2959 release_ioinfo(info);
2960 return -1;
2963 if(info == &MSVCRT___badioinfo) {
2964 *_errno() = EBADF;
2965 return EOF;
2968 if(mode == _O_BINARY) {
2969 info->wxflag &= ~WX_TEXT;
2970 info->exflag &= ~(EF_UTF8|EF_UTF16);
2971 release_ioinfo(info);
2972 return ret;
2975 info->wxflag |= WX_TEXT;
2976 if(mode == _O_TEXT)
2977 info->exflag &= ~(EF_UTF8|EF_UTF16);
2978 else if(mode == _O_U8TEXT)
2979 info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
2980 else
2981 info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
2983 release_ioinfo(info);
2984 return ret;
2987 /*********************************************************************
2988 * _stat64 (MSVCRT.@)
2990 int CDECL _stat64(const char* path, struct _stat64 * buf)
2992 DWORD dw;
2993 WIN32_FILE_ATTRIBUTE_DATA hfi;
2994 unsigned short mode = ALL_S_IREAD;
2995 int plen;
2997 TRACE(":file (%s) buf(%p)\n", path, buf);
2999 plen = strlen(path);
3000 while (plen && path[plen-1]==' ')
3001 plen--;
3003 if (plen==2 && path[1]==':')
3005 *_errno() = ENOENT;
3006 return -1;
3009 #if _MSVCR_VER<140
3010 if (plen>=2 && path[plen-2]!=':' && (path[plen-1]=='\\' || path[plen-1]=='/'))
3012 *_errno() = ENOENT;
3013 return -1;
3015 #endif
3017 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
3019 TRACE("failed (%ld)\n", GetLastError());
3020 *_errno() = ENOENT;
3021 return -1;
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
3029 as a drive letter
3031 if (isalpha(*path)&& (*(path+1)==':'))
3032 buf->st_dev = buf->st_rdev = _toupper_l(*path, NULL) - 'A'; /* drive num */
3033 else
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);
3039 else
3041 mode |= _S_IFREG;
3042 /* executable? */
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;
3057 buf->st_nlink = 1;
3058 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3059 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3060 buf->st_atime = dw;
3061 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3062 buf->st_mtime = buf->st_ctime = dw;
3063 TRACE("%d %d %#I64x %I64d %I64d %I64d\n", buf->st_mode, buf->st_nlink,
3064 buf->st_size, buf->st_atime, buf->st_mtime, buf->st_ctime);
3065 return 0;
3068 /*********************************************************************
3069 * _stati64 (MSVCRT.@)
3071 int CDECL _stati64(const char* path, struct _stati64 * buf)
3073 int ret;
3074 struct _stat64 buf64;
3076 ret = _stat64(path, &buf64);
3077 if (!ret)
3078 msvcrt_stat64_to_stati64(&buf64, buf);
3079 return ret;
3082 /*********************************************************************
3083 * _stat (MSVCRT.@)
3085 int CDECL _stat(const char* path, struct _stat * buf)
3087 int ret;
3088 struct _stat64 buf64;
3090 ret = _stat64( path, &buf64);
3091 if (!ret)
3092 msvcrt_stat64_to_stat(&buf64, buf);
3093 return ret;
3096 #if _MSVCR_VER >= 80
3098 /*********************************************************************
3099 * _stat32 (MSVCR80.@)
3101 int CDECL _stat32(const char *path, struct _stat32 *buf)
3103 int ret;
3104 struct _stat64 buf64;
3106 ret = _stat64(path, &buf64);
3107 if (!ret)
3108 msvcrt_stat64_to_stat32(&buf64, buf);
3109 return ret;
3112 /*********************************************************************
3113 * _stat32i64 (MSVCR80.@)
3115 int CDECL _stat32i64(const char *path, struct _stat32i64 *buf)
3117 int ret;
3118 struct _stat64 buf64;
3120 ret = _stat64(path, &buf64);
3121 if (!ret)
3122 msvcrt_stat64_to_stat32i64(&buf64, buf);
3123 return ret;
3126 /*********************************************************************
3127 * _stat64i32 (MSVCR80.@)
3129 int CDECL _stat64i32(const char* path, struct _stat64i32 *buf)
3131 int ret;
3132 struct _stat64 buf64;
3134 ret = _stat64(path, &buf64);
3135 if (!ret)
3136 msvcrt_stat64_to_stat64i32(&buf64, buf);
3137 return ret;
3140 #endif /* _MSVCR_VER >= 80 */
3142 /*********************************************************************
3143 * _wstat64 (MSVCRT.@)
3145 int CDECL _wstat64(const wchar_t* path, struct _stat64 * buf)
3147 DWORD dw;
3148 WIN32_FILE_ATTRIBUTE_DATA hfi;
3149 unsigned short mode = ALL_S_IREAD;
3150 int plen;
3152 TRACE(":file (%s) buf(%p)\n", debugstr_w(path), buf);
3154 plen = wcslen(path);
3155 while (plen && path[plen-1]==' ')
3156 plen--;
3158 if (plen==2 && path[1]==':')
3160 *_errno() = ENOENT;
3161 return -1;
3164 #if _MSVCR_VER<140
3165 if (plen>=2 && path[plen-2]!=':' && (path[plen-1]=='\\' || path[plen-1]=='/'))
3167 *_errno() = ENOENT;
3168 return -1;
3170 #endif
3172 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
3174 TRACE("failed (%ld)\n", GetLastError());
3175 *_errno() = ENOENT;
3176 return -1;
3179 memset(buf,0,sizeof(struct _stat64));
3181 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3182 if (iswalpha(*path) && path[1] == ':')
3183 buf->st_dev = buf->st_rdev = towupper(*path) - 'A'; /* drive num */
3184 else
3185 buf->st_dev = buf->st_rdev = _getdrive() - 1;
3187 /* Dir, or regular file? */
3188 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3189 mode |= (_S_IFDIR | ALL_S_IEXEC);
3190 else
3192 mode |= _S_IFREG;
3193 /* executable? */
3194 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3196 ULONGLONG ext = towlower(path[plen-1]) | (towlower(path[plen-2]) << 16) |
3197 ((ULONGLONG)towlower(path[plen-3]) << 32);
3198 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3199 mode |= ALL_S_IEXEC;
3203 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3204 mode |= ALL_S_IWRITE;
3206 buf->st_mode = mode;
3207 buf->st_nlink = 1;
3208 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3209 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3210 buf->st_atime = dw;
3211 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3212 buf->st_mtime = buf->st_ctime = dw;
3213 TRACE("%d %d %#I64x %I64d %I64d %I64d\n", buf->st_mode, buf->st_nlink,
3214 buf->st_size, buf->st_atime, buf->st_mtime, buf->st_ctime);
3215 return 0;
3218 /*********************************************************************
3219 * _wstati64 (MSVCRT.@)
3221 int CDECL _wstati64(const wchar_t* path, struct _stati64 * buf)
3223 int ret;
3224 struct _stat64 buf64;
3226 ret = _wstat64(path, &buf64);
3227 if (!ret)
3228 msvcrt_stat64_to_stati64(&buf64, buf);
3229 return ret;
3232 /*********************************************************************
3233 * _wstat (MSVCRT.@)
3235 int CDECL _wstat(const wchar_t* path, struct _stat * buf)
3237 int ret;
3238 struct _stat64 buf64;
3240 ret = _wstat64( path, &buf64 );
3241 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3242 return ret;
3245 #if _MSVCR_VER >= 80
3247 /*********************************************************************
3248 * _wstat32 (MSVCR80.@)
3250 int CDECL _wstat32(const wchar_t *path, struct _stat32 *buf)
3252 int ret;
3253 struct _stat64 buf64;
3255 ret = _wstat64(path, &buf64);
3256 if (!ret)
3257 msvcrt_stat64_to_stat32(&buf64, buf);
3258 return ret;
3261 /*********************************************************************
3262 * _wstat32i64 (MSVCR80.@)
3264 int CDECL _wstat32i64(const wchar_t *path, struct _stat32i64 *buf)
3266 int ret;
3267 struct _stat64 buf64;
3269 ret = _wstat64(path, &buf64);
3270 if (!ret)
3271 msvcrt_stat64_to_stat32i64(&buf64, buf);
3272 return ret;
3275 /*********************************************************************
3276 * _wstat64i32 (MSVCR80.@)
3278 int CDECL _wstat64i32(const wchar_t *path, struct _stat64i32 *buf)
3280 int ret;
3281 struct _stat64 buf64;
3283 ret = _wstat64(path, &buf64);
3284 if (!ret)
3285 msvcrt_stat64_to_stat64i32(&buf64, buf);
3286 return ret;
3289 #endif /* _MSVCR_VER >= 80 */
3291 /*********************************************************************
3292 * _tell (MSVCRT.@)
3294 __msvcrt_long CDECL _tell(int fd)
3296 return _lseek(fd, 0, SEEK_CUR);
3299 /*********************************************************************
3300 * _telli64 (MSVCRT.@)
3302 __int64 CDECL _telli64(int fd)
3304 return _lseeki64(fd, 0, SEEK_CUR);
3307 /*********************************************************************
3308 * _tempnam (MSVCRT.@)
3310 char * CDECL _tempnam(const char *dir, const char *prefix)
3312 char tmpbuf[MAX_PATH];
3313 const char *tmp_dir = getenv("TMP");
3315 if (tmp_dir) dir = tmp_dir;
3317 TRACE("dir (%s) prefix (%s)\n", dir, prefix);
3318 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3320 TRACE("got name (%s)\n", tmpbuf);
3321 DeleteFileA(tmpbuf);
3322 return _strdup(tmpbuf);
3324 TRACE("failed (%ld)\n", GetLastError());
3325 return NULL;
3328 /*********************************************************************
3329 * _wtempnam (MSVCRT.@)
3331 wchar_t * CDECL _wtempnam(const wchar_t *dir, const wchar_t *prefix)
3333 wchar_t tmpbuf[MAX_PATH];
3334 const wchar_t *tmp_dir = _wgetenv(L"TMP");
3336 if (tmp_dir) dir = tmp_dir;
3338 TRACE("dir (%s) prefix (%s)\n", debugstr_w(dir), debugstr_w(prefix));
3339 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3341 TRACE("got name (%s)\n", debugstr_w(tmpbuf));
3342 DeleteFileW(tmpbuf);
3343 return _wcsdup(tmpbuf);
3345 TRACE("failed (%ld)\n", GetLastError());
3346 return NULL;
3349 /*********************************************************************
3350 * _umask (MSVCRT.@)
3352 int CDECL _umask(int umask)
3354 int old_umask = MSVCRT_umask;
3355 TRACE("(%d)\n",umask);
3356 MSVCRT_umask = umask;
3357 return old_umask;
3360 /*********************************************************************
3361 * _utime64 (MSVCRT.@)
3363 int CDECL _utime64(const char* path, struct __utimbuf64 *t)
3365 int fd = _open(path, _O_WRONLY | _O_BINARY);
3367 if (fd > 0)
3369 int retVal = _futime64(fd, t);
3370 _close(fd);
3371 return retVal;
3373 return -1;
3376 /*********************************************************************
3377 * _utime32 (MSVCRT.@)
3379 int CDECL _utime32(const char* path, struct __utimbuf32 *t)
3381 if (t)
3383 struct __utimbuf64 t64;
3384 t64.actime = t->actime;
3385 t64.modtime = t->modtime;
3386 return _utime64( path, &t64 );
3388 else
3389 return _utime64( path, NULL );
3392 /*********************************************************************
3393 * _wutime64 (MSVCRT.@)
3395 int CDECL _wutime64(const wchar_t* path, struct __utimbuf64 *t)
3397 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
3399 if (fd > 0)
3401 int retVal = _futime64(fd, t);
3402 _close(fd);
3403 return retVal;
3405 return -1;
3408 /*********************************************************************
3409 * _wutime32 (MSVCRT.@)
3411 int CDECL _wutime32(const wchar_t* path, struct __utimbuf32 *t)
3413 if (t)
3415 struct __utimbuf64 t64;
3416 t64.actime = t->actime;
3417 t64.modtime = t->modtime;
3418 return _wutime64( path, &t64 );
3420 else
3421 return _wutime64( path, NULL );
3424 /*********************************************************************
3425 * _write (MSVCRT.@)
3427 int CDECL _write(int fd, const void* buf, unsigned int count)
3429 ioinfo *info = get_ioinfo(fd);
3430 HANDLE hand = info->handle;
3431 DWORD num_written, i;
3432 BOOL console = FALSE;
3434 if (hand == INVALID_HANDLE_VALUE || fd == MSVCRT_NO_CONSOLE_FD)
3436 *_errno() = EBADF;
3437 release_ioinfo(info);
3438 return -1;
3441 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3443 *_errno() = EINVAL;
3444 release_ioinfo(info);
3445 return -1;
3448 /* If appending, go to EOF */
3449 if (info->wxflag & WX_APPEND)
3450 _lseek(fd, 0, FILE_END);
3452 if (!(info->wxflag & WX_TEXT))
3454 if (!WriteFile(hand, buf, count, &num_written, NULL)
3455 || num_written != count)
3457 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd,
3458 hand, GetLastError());
3459 msvcrt_set_errno(GetLastError());
3460 num_written = -1;
3463 release_ioinfo(info);
3464 return num_written;
3467 if (_isatty(fd)) console = VerifyConsoleIoHandle(hand);
3468 for (i = 0; i < count;)
3470 const char *s = buf;
3471 char lfbuf[2048];
3472 DWORD j = 0;
3474 if (!(info->exflag & (EF_UTF8|EF_UTF16)) && console)
3476 char conv[sizeof(lfbuf)];
3477 size_t len = 0;
3479 #if _MSVCR_VER >= 90
3480 if (info->dbcsBufferUsed)
3482 conv[j++] = info->dbcsBuffer;
3483 info->dbcsBufferUsed = FALSE;
3484 conv[j++] = s[i++];
3485 len++;
3487 #endif
3489 for (; i < count && j < sizeof(conv)-1 &&
3490 len < (sizeof(lfbuf) - 1) / sizeof(WCHAR); i++, j++, len++)
3492 if (isleadbyte((unsigned char)s[i]))
3494 conv[j++] = s[i++];
3496 if (i == count)
3498 #if _MSVCR_VER >= 90
3499 info->dbcsBuffer = conv[j-1];
3500 info->dbcsBufferUsed = TRUE;
3501 break;
3502 #else
3503 *_errno() = EINVAL;
3504 release_ioinfo(info);
3505 return -1;
3506 #endif
3509 else if (s[i] == '\n')
3511 conv[j++] = '\r';
3512 len++;
3514 conv[j] = s[i];
3517 len = mbstowcs((WCHAR*)lfbuf, conv, len);
3518 if (len == -1)
3520 msvcrt_set_errno(GetLastError());
3521 release_ioinfo(info);
3522 return -1;
3524 j = len * 2;
3526 else if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3528 for (j = 0; i < count && j < sizeof(lfbuf)-1; i++, j++)
3530 if (s[i] == '\n')
3531 lfbuf[j++] = '\r';
3532 lfbuf[j] = s[i];
3535 else if (info->exflag & EF_UTF16 || console)
3537 for (j = 0; i < count && j < sizeof(lfbuf)-3; i++, j++)
3539 if (s[i] == '\n' && !s[i+1])
3541 lfbuf[j++] = '\r';
3542 lfbuf[j++] = 0;
3544 lfbuf[j++] = s[i++];
3545 lfbuf[j] = s[i];
3548 else
3550 char conv[sizeof(lfbuf)/4];
3552 for (j = 0; i < count && j < sizeof(conv)-3; i++, j++)
3554 if (s[i] == '\n' && !s[i+1])
3556 conv[j++] = '\r';
3557 conv[j++] = 0;
3559 conv[j++] = s[i++];
3560 conv[j] = s[i];
3563 j = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)conv, j/2, lfbuf, sizeof(lfbuf), NULL, NULL);
3564 if (!j)
3566 msvcrt_set_errno(GetLastError());
3567 release_ioinfo(info);
3568 return -1;
3572 if (console)
3574 j = j/2;
3575 if (!WriteConsoleW(hand, lfbuf, j, &num_written, NULL))
3576 num_written = -1;
3578 else if (!WriteFile(hand, lfbuf, j, &num_written, NULL))
3580 num_written = -1;
3583 if (num_written != j)
3585 TRACE("WriteFile/WriteConsoleW (fd %d, hand %p) failed-last error (%ld)\n", fd,
3586 hand, GetLastError());
3587 msvcrt_set_errno(GetLastError());
3588 release_ioinfo(info);
3589 return -1;
3593 release_ioinfo(info);
3594 return count;
3597 /*********************************************************************
3598 * _putw (MSVCRT.@)
3600 int CDECL _putw(int val, FILE* file)
3602 int len;
3604 _lock_file(file);
3605 len = _write(file->_file, &val, sizeof(val));
3606 if (len == sizeof(val)) {
3607 _unlock_file(file);
3608 return val;
3611 file->_flag |= _IOERR;
3612 _unlock_file(file);
3613 return EOF;
3616 /*********************************************************************
3617 * fclose (MSVCRT.@)
3619 int CDECL fclose(FILE* file)
3621 int ret;
3623 if (!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
3625 _lock_file(file);
3626 ret = _fclose_nolock(file);
3627 _unlock_file(file);
3629 return ret;
3632 /*********************************************************************
3633 * _fclose_nolock (MSVCRT.@)
3635 int CDECL _fclose_nolock(FILE* file)
3637 int r, flag;
3639 if (!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
3641 if(!(file->_flag & (_IOREAD | _IOWRT | _IORW)))
3643 file->_flag = 0;
3644 return EOF;
3647 flag = file->_flag;
3648 free(file->_tmpfname);
3649 file->_tmpfname = NULL;
3650 /* flush stdio buffers */
3651 if(file->_flag & _IOWRT)
3652 _fflush_nolock(file);
3653 if(file->_flag & _IOMYBUF)
3654 free(file->_base);
3656 r=_close(file->_file);
3657 file->_flag = 0;
3659 return ((r == -1) || (flag & _IOERR) ? EOF : 0);
3662 /*********************************************************************
3663 * feof (MSVCRT.@)
3665 int CDECL feof(FILE* file)
3667 return file->_flag & _IOEOF;
3670 /*********************************************************************
3671 * ferror (MSVCRT.@)
3673 int CDECL ferror(FILE* file)
3675 return file->_flag & _IOERR;
3678 /*********************************************************************
3679 * _filbuf (MSVCRT.@)
3681 int CDECL _filbuf(FILE* file)
3683 unsigned char c;
3685 if(file->_flag & _IOSTRG)
3686 return EOF;
3688 /* Allocate buffer if needed */
3689 if(!(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF)))
3690 msvcrt_alloc_buffer(file);
3692 if(!(file->_flag & _IOREAD)) {
3693 if(file->_flag & _IORW)
3694 file->_flag |= _IOREAD;
3695 else
3696 return EOF;
3699 if(!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF))) {
3700 int r;
3701 if ((r = _read(file->_file,&c,1)) != 1) {
3702 file->_flag |= (r == 0) ? _IOEOF : _IOERR;
3703 return EOF;
3706 return c;
3707 } else {
3708 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
3709 if(file->_cnt<=0) {
3710 file->_flag |= (file->_cnt == 0) ? _IOEOF : _IOERR;
3711 file->_cnt = 0;
3712 return EOF;
3715 file->_cnt--;
3716 file->_ptr = file->_base+1;
3717 c = *(unsigned char *)file->_base;
3718 return c;
3722 /*********************************************************************
3723 * fgetc (MSVCRT.@)
3725 int CDECL fgetc(FILE* file)
3727 int ret;
3729 _lock_file(file);
3730 ret = _fgetc_nolock(file);
3731 _unlock_file(file);
3733 return ret;
3736 /*********************************************************************
3737 * _fgetc_nolock (MSVCRT.@)
3739 int CDECL _fgetc_nolock(FILE* file)
3741 unsigned char *i;
3742 unsigned int j;
3744 if (file->_cnt>0) {
3745 file->_cnt--;
3746 i = (unsigned char *)file->_ptr++;
3747 j = *i;
3748 } else
3749 j = _filbuf(file);
3751 return j;
3754 /*********************************************************************
3755 * _fgetchar (MSVCRT.@)
3757 int CDECL _fgetchar(void)
3759 return fgetc(MSVCRT_stdin);
3762 /*********************************************************************
3763 * fgets (MSVCRT.@)
3765 char * CDECL fgets(char *s, int size, FILE* file)
3767 int cc = EOF;
3768 char * buf_start = s;
3770 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3771 file,file->_file,s,size);
3773 _lock_file(file);
3775 while ((size >1) && (cc = _fgetc_nolock(file)) != EOF && cc != '\n')
3777 *s++ = (char)cc;
3778 size --;
3780 if ((cc == EOF) && (s == buf_start)) /* If nothing read, return 0*/
3782 TRACE(":nothing read\n");
3783 _unlock_file(file);
3784 return NULL;
3786 if ((cc != EOF) && (size > 1))
3787 *s++ = cc;
3788 *s = '\0';
3789 TRACE(":got %s\n", debugstr_a(buf_start));
3790 _unlock_file(file);
3791 return buf_start;
3794 /*********************************************************************
3795 * fgetwc (MSVCRT.@)
3797 wint_t CDECL fgetwc(FILE* file)
3799 wint_t ret;
3801 _lock_file(file);
3802 ret = _fgetwc_nolock(file);
3803 _unlock_file(file);
3805 return ret;
3808 /*********************************************************************
3809 * _fgetwc_nolock (MSVCRT.@)
3811 wint_t CDECL _fgetwc_nolock(FILE* file)
3813 wint_t ret;
3814 int ch;
3816 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3817 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3818 char *p;
3820 for(p=(char*)&ret; (wint_t*)p<&ret+1; p++) {
3821 ch = _fgetc_nolock(file);
3822 if(ch == EOF) {
3823 ret = WEOF;
3824 break;
3826 *p = (char)ch;
3828 }else {
3829 char mbs[MB_LEN_MAX];
3830 int len = 0;
3832 ch = _fgetc_nolock(file);
3833 if(ch != EOF) {
3834 mbs[0] = (char)ch;
3835 if(isleadbyte((unsigned char)mbs[0])) {
3836 ch = _fgetc_nolock(file);
3837 if(ch != EOF) {
3838 mbs[1] = (char)ch;
3839 len = 2;
3841 }else {
3842 len = 1;
3846 if(!len || mbtowc(&ret, mbs, len)==-1)
3847 ret = WEOF;
3850 return ret;
3853 /*********************************************************************
3854 * _getw (MSVCRT.@)
3856 int CDECL _getw(FILE* file)
3858 char *ch;
3859 int i, k;
3860 unsigned int j;
3861 ch = (char *)&i;
3863 _lock_file(file);
3864 for (j=0; j<sizeof(int); j++) {
3865 k = _fgetc_nolock(file);
3866 if (k == EOF) {
3867 file->_flag |= _IOEOF;
3868 _unlock_file(file);
3869 return EOF;
3871 ch[j] = k;
3874 _unlock_file(file);
3875 return i;
3878 /*********************************************************************
3879 * getwc (MSVCRT.@)
3881 wint_t CDECL getwc(FILE* file)
3883 return fgetwc(file);
3886 /*********************************************************************
3887 * _fgetwchar (MSVCRT.@)
3889 wint_t CDECL _fgetwchar(void)
3891 return fgetwc(MSVCRT_stdin);
3894 /*********************************************************************
3895 * getwchar (MSVCRT.@)
3897 wint_t CDECL getwchar(void)
3899 return _fgetwchar();
3902 /*********************************************************************
3903 * fgetws (MSVCRT.@)
3905 wchar_t * CDECL fgetws(wchar_t *s, int size, FILE* file)
3907 wint_t cc = WEOF;
3908 wchar_t * buf_start = s;
3910 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3911 file,file->_file,s,size);
3913 _lock_file(file);
3915 while ((size >1) && (cc = _fgetwc_nolock(file)) != WEOF && cc != '\n')
3917 *s++ = cc;
3918 size --;
3920 if ((cc == WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3922 TRACE(":nothing read\n");
3923 _unlock_file(file);
3924 return NULL;
3926 if ((cc != WEOF) && (size > 1))
3927 *s++ = cc;
3928 *s = 0;
3929 TRACE(":got %s\n", debugstr_w(buf_start));
3930 _unlock_file(file);
3931 return buf_start;
3934 /*********************************************************************
3935 * _flsbuf (MSVCRT.@)
3937 int CDECL _flsbuf(int c, FILE* file)
3939 /* Flush output buffer */
3940 if(!(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF))) {
3941 msvcrt_alloc_buffer(file);
3944 if(!(file->_flag & _IOWRT)) {
3945 if(!(file->_flag & _IORW)) {
3946 file->_flag |= _IOERR;
3947 *_errno() = EBADF;
3948 return EOF;
3950 file->_flag |= _IOWRT;
3952 if(file->_flag & _IOREAD) {
3953 if(!(file->_flag & _IOEOF)) {
3954 file->_flag |= _IOERR;
3955 return EOF;
3957 file->_cnt = 0;
3958 file->_ptr = file->_base;
3959 file->_flag &= ~(_IOREAD | _IOEOF);
3962 if(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) {
3963 int res = 0;
3965 if(file->_cnt <= 0) {
3966 res = msvcrt_flush_buffer(file);
3967 if(res)
3968 return res;
3969 file->_flag |= _IOWRT;
3970 file->_cnt=file->_bufsiz;
3972 *file->_ptr++ = c;
3973 file->_cnt--;
3974 return c&0xff;
3975 } else {
3976 unsigned char cc=c;
3977 int len;
3978 /* set _cnt to 0 for unbuffered FILEs */
3979 file->_cnt = 0;
3980 len = _write(file->_file, &cc, 1);
3981 if (len == 1)
3982 return c & 0xff;
3983 file->_flag |= _IOERR;
3984 return EOF;
3988 /*********************************************************************
3989 * fwrite (MSVCRT.@)
3991 size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE* file)
3993 size_t ret;
3995 _lock_file(file);
3996 ret = _fwrite_nolock(ptr, size, nmemb, file);
3997 _unlock_file(file);
3999 return ret;
4002 /*********************************************************************
4003 * _fwrite_nolock (MSVCRT.@)
4005 size_t CDECL _fwrite_nolock(const void *ptr, size_t size, size_t nmemb, FILE* file)
4007 size_t wrcnt=size * nmemb;
4008 int written = 0;
4009 if (size == 0)
4010 return 0;
4012 while(wrcnt) {
4013 if(file->_cnt < 0) {
4014 WARN("negative file->_cnt value in %p\n", file);
4015 file->_flag |= _IOERR;
4016 break;
4017 } else if(file->_cnt) {
4018 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
4019 memcpy(file->_ptr, ptr, pcnt);
4020 file->_cnt -= pcnt;
4021 file->_ptr += pcnt;
4022 written += pcnt;
4023 wrcnt -= pcnt;
4024 ptr = (const char*)ptr + pcnt;
4025 } else if((file->_flag & _IONBF)
4026 || ((file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
4027 || (!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
4028 size_t pcnt;
4029 int bufsiz;
4031 if(file->_flag & _IONBF)
4032 bufsiz = 1;
4033 else if(!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)))
4034 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
4035 else
4036 bufsiz = file->_bufsiz;
4038 pcnt = (wrcnt / bufsiz) * bufsiz;
4040 if(msvcrt_flush_buffer(file) == EOF)
4041 break;
4043 if(_write(file->_file, ptr, pcnt) <= 0) {
4044 file->_flag |= _IOERR;
4045 break;
4047 written += pcnt;
4048 wrcnt -= pcnt;
4049 ptr = (const char*)ptr + pcnt;
4050 } else {
4051 if(_flsbuf(*(const char*)ptr, file) == EOF)
4052 break;
4053 written++;
4054 wrcnt--;
4055 ptr = (const char*)ptr + 1;
4059 return written / size;
4062 /*********************************************************************
4063 * fputwc (MSVCRT.@)
4065 wint_t CDECL fputwc(wint_t wc, FILE* file)
4067 wint_t ret;
4069 _lock_file(file);
4070 ret = _fputwc_nolock(wc, file);
4071 _unlock_file(file);
4073 return ret;
4076 /*********************************************************************
4077 * _fputwc_nolock (MSVCRT.@)
4079 wint_t CDECL _fputwc_nolock(wint_t wc, FILE* file)
4081 wchar_t mwc=wc;
4082 ioinfo *fdinfo;
4083 wint_t ret;
4085 fdinfo = get_ioinfo_nolock(file->_file);
4087 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
4088 char buf[MB_LEN_MAX];
4089 int char_len;
4091 char_len = wctomb(buf, mwc);
4092 if(char_len!=-1 && _fwrite_nolock(buf, char_len, 1, file)==1)
4093 ret = wc;
4094 else
4095 ret = WEOF;
4096 }else if(_fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
4097 ret = wc;
4098 }else {
4099 ret = WEOF;
4102 return ret;
4105 /*********************************************************************
4106 * _fputwchar (MSVCRT.@)
4108 wint_t CDECL _fputwchar(wint_t wc)
4110 return fputwc(wc, MSVCRT_stdout);
4113 /*********************************************************************
4114 * _wfsopen (MSVCRT.@)
4116 FILE * CDECL _wfsopen(const wchar_t *path, const wchar_t *mode, int share)
4118 FILE* file;
4119 int open_flags, stream_flags, fd;
4121 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
4123 /* map mode string to open() flags. "man fopen" for possibilities. */
4124 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4125 return NULL;
4127 LOCK_FILES();
4128 fd = _wsopen(path, open_flags, share, _S_IREAD | _S_IWRITE);
4129 if (fd < 0)
4130 file = NULL;
4131 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
4132 != -1)
4133 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
4134 else if (file)
4136 file->_flag = 0;
4137 file = NULL;
4140 TRACE(":got (%p)\n",file);
4141 if (fd >= 0 && !file)
4142 _close(fd);
4143 UNLOCK_FILES();
4144 return file;
4147 /*********************************************************************
4148 * _fsopen (MSVCRT.@)
4150 FILE * CDECL _fsopen(const char *path, const char *mode, int share)
4152 FILE *ret;
4153 wchar_t *pathW = NULL, *modeW = NULL;
4155 if (path && !(pathW = msvcrt_wstrdupa(path))) {
4156 _invalid_parameter(NULL, NULL, NULL, 0, 0);
4157 *_errno() = EINVAL;
4158 return NULL;
4160 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4162 free(pathW);
4163 _invalid_parameter(NULL, NULL, NULL, 0, 0);
4164 *_errno() = EINVAL;
4165 return NULL;
4168 ret = _wfsopen(pathW, modeW, share);
4170 free(pathW);
4171 free(modeW);
4172 return ret;
4175 /*********************************************************************
4176 * fopen (MSVCRT.@)
4178 FILE * CDECL fopen(const char *path, const char *mode)
4180 return _fsopen( path, mode, _SH_DENYNO );
4183 /*********************************************************************
4184 * fopen_s (MSVCRT.@)
4186 int CDECL fopen_s(FILE** pFile,
4187 const char *filename, const char *mode)
4189 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4190 if (!MSVCRT_CHECK_PMT(filename != NULL)) return EINVAL;
4191 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4193 *pFile = fopen(filename, mode);
4195 if(!*pFile)
4196 return *_errno();
4197 return 0;
4200 /*********************************************************************
4201 * _wfopen (MSVCRT.@)
4203 FILE * CDECL _wfopen(const wchar_t *path, const wchar_t *mode)
4205 return _wfsopen( path, mode, _SH_DENYNO );
4208 /*********************************************************************
4209 * _wfopen_s (MSVCRT.@)
4211 int CDECL _wfopen_s(FILE** pFile, const wchar_t *filename,
4212 const wchar_t *mode)
4214 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4215 if (!MSVCRT_CHECK_PMT(filename != NULL)) return EINVAL;
4216 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4218 *pFile = _wfopen(filename, mode);
4220 if(!*pFile)
4221 return *_errno();
4222 return 0;
4225 /*********************************************************************
4226 * fputc (MSVCRT.@)
4228 int CDECL fputc(int c, FILE* file)
4230 int ret;
4232 _lock_file(file);
4233 ret = _fputc_nolock(c, file);
4234 _unlock_file(file);
4236 return ret;
4239 /*********************************************************************
4240 * _fputc_nolock (MSVCRT.@)
4242 int CDECL _fputc_nolock(int c, FILE* file)
4244 int res;
4246 if(file->_cnt>0) {
4247 *file->_ptr++=c;
4248 file->_cnt--;
4249 if (c == '\n')
4251 res = msvcrt_flush_buffer(file);
4252 return res ? res : c;
4254 else {
4255 return c & 0xff;
4257 } else {
4258 res = _flsbuf(c, file);
4259 return res;
4263 /*********************************************************************
4264 * _fputchar (MSVCRT.@)
4266 int CDECL _fputchar(int c)
4268 return fputc(c, MSVCRT_stdout);
4271 /*********************************************************************
4272 * fread (MSVCRT.@)
4274 size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file)
4276 size_t ret;
4278 _lock_file(file);
4279 ret = _fread_nolock(ptr, size, nmemb, file);
4280 _unlock_file(file);
4282 return ret;
4285 /*********************************************************************
4286 * _fread_nolock (MSVCRT.@)
4288 size_t CDECL _fread_nolock(void *ptr, size_t size, size_t nmemb, FILE* file)
4290 size_t rcnt=size * nmemb;
4291 size_t read=0;
4292 size_t pread=0;
4294 if(!rcnt)
4295 return 0;
4297 /* first buffered data */
4298 if(file->_cnt>0) {
4299 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4300 memcpy(ptr, file->_ptr, pcnt);
4301 file->_cnt -= pcnt;
4302 file->_ptr += pcnt;
4303 read += pcnt ;
4304 rcnt -= pcnt ;
4305 ptr = (char*)ptr + pcnt;
4306 } else if(!(file->_flag & _IOREAD )) {
4307 if(file->_flag & _IORW) {
4308 file->_flag |= _IOREAD;
4309 } else {
4310 return 0;
4314 if(rcnt>0 && !(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF)))
4315 msvcrt_alloc_buffer(file);
4317 while(rcnt>0)
4319 int i;
4320 if (!file->_cnt && rcnt<file->_bufsiz && (file->_flag & (_IOMYBUF | MSVCRT__USERBUF))) {
4321 i = _read(file->_file, file->_base, file->_bufsiz);
4322 file->_ptr = file->_base;
4323 if (i != -1) {
4324 file->_cnt = i;
4325 if (i > rcnt) i = rcnt;
4327 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4328 if (i > 0 && i < file->_cnt) {
4329 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4330 file->_flag &= ~_IOEOF;
4332 if (i > 0) {
4333 memcpy(ptr, file->_ptr, i);
4334 file->_cnt -= i;
4335 file->_ptr += i;
4337 } else if (rcnt > INT_MAX) {
4338 i = _read(file->_file, ptr, INT_MAX);
4339 } else if (rcnt < (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ)) {
4340 i = _read(file->_file, ptr, rcnt);
4341 } else {
4342 i = _read(file->_file, ptr, rcnt - rcnt % (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ));
4344 pread += i;
4345 rcnt -= i;
4346 ptr = (char *)ptr+i;
4347 /* expose feof condition in the flags
4348 * MFC tests file->_flag for feof, and doesn't call feof())
4350 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4351 file->_flag |= _IOEOF;
4352 else if (i == -1)
4354 file->_flag |= _IOERR;
4355 pread = 0;
4356 rcnt = 0;
4358 if (i < 1) break;
4360 read+=pread;
4361 return read / size;
4364 #if _MSVCR_VER >= 80
4366 /*********************************************************************
4367 * fread_s (MSVCR80.@)
4369 size_t CDECL fread_s(void *buf, size_t buf_size, size_t elem_size,
4370 size_t count, FILE *stream)
4372 size_t ret;
4374 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4375 if(buf && buf_size)
4376 memset(buf, 0, buf_size);
4377 return 0;
4379 if(!elem_size || !count) return 0;
4381 _lock_file(stream);
4382 ret = _fread_nolock_s(buf, buf_size, elem_size, count, stream);
4383 _unlock_file(stream);
4385 return ret;
4388 /*********************************************************************
4389 * _fread_nolock_s (MSVCR80.@)
4391 size_t CDECL _fread_nolock_s(void *buf, size_t buf_size, size_t elem_size,
4392 size_t count, FILE *stream)
4394 size_t bytes_left, buf_pos;
4396 TRACE("(%p %Iu %Iu %Iu %p)\n", buf, buf_size, elem_size, count, stream);
4398 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4399 if(buf && buf_size)
4400 memset(buf, 0, buf_size);
4401 return 0;
4403 if(!elem_size || !count) return 0;
4404 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4405 if(!MSVCRT_CHECK_PMT(SIZE_MAX/count >= elem_size)) return 0;
4407 bytes_left = elem_size*count;
4408 buf_pos = 0;
4409 while(bytes_left) {
4410 if(stream->_cnt > 0) {
4411 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4413 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, ERANGE)) {
4414 memset(buf, 0, buf_size);
4415 return 0;
4418 _fread_nolock((char*)buf+buf_pos, 1, size, stream);
4419 buf_pos += size;
4420 bytes_left -= size;
4421 }else {
4422 int c = _filbuf(stream);
4424 if(c == EOF)
4425 break;
4427 if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, ERANGE)) {
4428 memset(buf, 0, buf_size);
4429 return 0;
4432 ((char*)buf)[buf_pos++] = c;
4433 bytes_left--;
4437 return buf_pos/elem_size;
4440 #endif /* _MSVCR_VER >= 80 */
4442 /*********************************************************************
4443 * _wfreopen (MSVCRT.@)
4446 FILE* CDECL _wfreopen(const wchar_t *path, const wchar_t *mode, FILE* file)
4448 int open_flags, stream_flags, fd;
4450 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4452 LOCK_FILES();
4453 if (!file || ((fd = file->_file) < 0))
4454 file = NULL;
4455 else
4457 fclose(file);
4458 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4459 file = NULL;
4460 else if((fd = _wopen(path, open_flags, _S_IREAD | _S_IWRITE)) < 0)
4461 file = NULL;
4462 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4464 file->_flag = 0;
4465 file = NULL;
4468 UNLOCK_FILES();
4469 return file;
4472 /*********************************************************************
4473 * _wfreopen_s (MSVCRT.@)
4475 int CDECL _wfreopen_s(FILE** pFile,
4476 const wchar_t *path, const wchar_t *mode, FILE* file)
4478 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4479 if (!MSVCRT_CHECK_PMT(path != NULL)) return EINVAL;
4480 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4481 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
4483 *pFile = _wfreopen(path, mode, file);
4485 if(!*pFile)
4486 return *_errno();
4487 return 0;
4490 /*********************************************************************
4491 * freopen (MSVCRT.@)
4494 FILE* CDECL freopen(const char *path, const char *mode, FILE* file)
4496 FILE *ret;
4497 wchar_t *pathW = NULL, *modeW = NULL;
4499 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4500 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4502 free(pathW);
4503 return NULL;
4506 ret = _wfreopen(pathW, modeW, file);
4508 free(pathW);
4509 free(modeW);
4510 return ret;
4513 /*********************************************************************
4514 * freopen_s (MSVCRT.@)
4516 int CDECL freopen_s(FILE** pFile,
4517 const char *path, const char *mode, FILE* file)
4519 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4520 if (!MSVCRT_CHECK_PMT(path != NULL)) return EINVAL;
4521 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4522 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
4524 *pFile = freopen(path, mode, file);
4526 if(!*pFile)
4527 return *_errno();
4528 return 0;
4531 /*********************************************************************
4532 * fsetpos (MSVCRT.@)
4534 int CDECL fsetpos(FILE* file, fpos_t *pos)
4536 int ret;
4538 _lock_file(file);
4539 msvcrt_flush_buffer(file);
4541 /* Reset direction of i/o */
4542 if(file->_flag & _IORW) {
4543 file->_flag &= ~(_IOREAD|_IOWRT);
4546 ret = (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
4547 _unlock_file(file);
4548 return ret;
4551 /*********************************************************************
4552 * _ftelli64 (MSVCRT.@)
4554 __int64 CDECL _ftelli64(FILE* file)
4556 __int64 ret;
4558 _lock_file(file);
4559 ret = _ftelli64_nolock(file);
4560 _unlock_file(file);
4562 return ret;
4565 /*********************************************************************
4566 * _ftelli64_nolock (MSVCRT.@)
4568 __int64 CDECL _ftelli64_nolock(FILE* file)
4570 __int64 pos;
4572 pos = _telli64(file->_file);
4573 if(pos == -1)
4574 return -1;
4575 if(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) {
4576 if(file->_flag & _IOWRT) {
4577 pos += file->_ptr - file->_base;
4579 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4580 char *p;
4582 for(p=file->_base; p<file->_ptr; p++)
4583 if(*p == '\n')
4584 pos++;
4586 } else if(!file->_cnt) { /* nothing to do */
4587 } else if(_lseeki64(file->_file, 0, SEEK_END)==pos) {
4588 int i;
4590 pos -= file->_cnt;
4591 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4592 for(i=0; i<file->_cnt; i++)
4593 if(file->_ptr[i] == '\n')
4594 pos--;
4596 } else {
4597 char *p;
4599 if(_lseeki64(file->_file, pos, SEEK_SET) != pos)
4600 return -1;
4602 pos -= file->_bufsiz;
4603 pos += file->_ptr - file->_base;
4605 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4606 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4607 pos--;
4609 for(p=file->_base; p<file->_ptr; p++)
4610 if(*p == '\n')
4611 pos++;
4616 return pos;
4619 /*********************************************************************
4620 * ftell (MSVCRT.@)
4622 __msvcrt_long CDECL ftell(FILE* file)
4624 return _ftelli64(file);
4627 #if _MSVCR_VER >= 80
4628 /*********************************************************************
4629 * _ftell_nolock (MSVCR80.@)
4631 __msvcrt_long CDECL _ftell_nolock(FILE* file)
4633 return _ftelli64_nolock(file);
4635 #endif
4637 /*********************************************************************
4638 * fgetpos (MSVCRT.@)
4640 int CDECL fgetpos(FILE* file, fpos_t *pos)
4642 *pos = _ftelli64(file);
4643 if(*pos == -1)
4644 return -1;
4645 return 0;
4648 /*********************************************************************
4649 * fputs (MSVCRT.@)
4651 int CDECL fputs(const char *s, FILE* file)
4653 size_t len = strlen(s);
4654 int ret;
4656 _lock_file(file);
4657 ret = _fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : EOF;
4658 _unlock_file(file);
4659 return ret;
4662 /*********************************************************************
4663 * fputws (MSVCRT.@)
4665 int CDECL fputws(const wchar_t *s, FILE* file)
4667 size_t i, len = wcslen(s);
4668 BOOL tmp_buf;
4669 int ret;
4671 _lock_file(file);
4672 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4673 ret = _fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : EOF;
4674 _unlock_file(file);
4675 return ret;
4678 tmp_buf = add_std_buffer(file);
4679 for (i=0; i<len; i++) {
4680 if(_fputwc_nolock(s[i], file) == WEOF) {
4681 if(tmp_buf) remove_std_buffer(file);
4682 _unlock_file(file);
4683 return WEOF;
4687 if(tmp_buf) remove_std_buffer(file);
4688 _unlock_file(file);
4689 return 0;
4692 /*********************************************************************
4693 * getchar (MSVCRT.@)
4695 int CDECL getchar(void)
4697 return fgetc(MSVCRT_stdin);
4700 /*********************************************************************
4701 * getc (MSVCRT.@)
4703 int CDECL getc(FILE* file)
4705 return fgetc(file);
4708 /*********************************************************************
4709 * gets_s (MSVCR80.@)
4711 char * CDECL gets_s(char *buf, size_t len)
4713 char *buf_start = buf;
4714 int cc;
4716 if (!MSVCRT_CHECK_PMT(buf != NULL)) return NULL;
4717 if (!MSVCRT_CHECK_PMT(len != 0)) return NULL;
4719 _lock_file(MSVCRT_stdin);
4720 for(cc = _fgetc_nolock(MSVCRT_stdin);
4721 len != 0 && cc != EOF && cc != '\n';
4722 cc = _fgetc_nolock(MSVCRT_stdin))
4724 if (cc != '\r')
4726 *buf++ = (char)cc;
4727 len--;
4730 _unlock_file(MSVCRT_stdin);
4732 if (!len)
4734 *buf_start = 0;
4735 _invalid_parameter(NULL, NULL, NULL, 0, 0);
4736 return NULL;
4739 if ((cc == EOF) && (buf_start == buf))
4741 TRACE(":nothing read\n");
4742 return NULL;
4744 *buf = '\0';
4746 TRACE("got '%s'\n", buf_start);
4747 return buf_start;
4750 /*********************************************************************
4751 * gets (MSVCRT.@)
4753 char * CDECL gets(char *buf)
4755 return gets_s(buf, -1);
4758 /*********************************************************************
4759 * _getws (MSVCRT.@)
4761 wchar_t* CDECL _getws(wchar_t* buf)
4763 wint_t cc;
4764 wchar_t* ws = buf;
4766 _lock_file(MSVCRT_stdin);
4767 for (cc = _fgetwc_nolock(MSVCRT_stdin); cc != WEOF && cc != '\n';
4768 cc = _fgetwc_nolock(MSVCRT_stdin))
4770 if (cc != '\r')
4771 *buf++ = (wchar_t)cc;
4773 _unlock_file(MSVCRT_stdin);
4775 if ((cc == WEOF) && (ws == buf))
4777 TRACE(":nothing read\n");
4778 return NULL;
4780 *buf = '\0';
4782 TRACE("got %s\n", debugstr_w(ws));
4783 return ws;
4786 /*********************************************************************
4787 * putc (MSVCRT.@)
4789 int CDECL putc(int c, FILE* file)
4791 return fputc(c, file);
4794 /*********************************************************************
4795 * putchar (MSVCRT.@)
4797 int CDECL putchar(int c)
4799 return fputc(c, MSVCRT_stdout);
4802 /*********************************************************************
4803 * puts (MSVCRT.@)
4805 int CDECL puts(const char *s)
4807 size_t len = strlen(s);
4808 int ret;
4810 _lock_file(MSVCRT_stdout);
4811 if(_fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4812 _unlock_file(MSVCRT_stdout);
4813 return EOF;
4816 ret = _fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : EOF;
4817 _unlock_file(MSVCRT_stdout);
4818 return ret;
4821 /*********************************************************************
4822 * _putws (MSVCRT.@)
4824 int CDECL _putws(const wchar_t *s)
4826 int ret;
4828 _lock_file(MSVCRT_stdout);
4829 ret = fputws(s, MSVCRT_stdout);
4830 if(ret >= 0)
4831 ret = _fputwc_nolock('\n', MSVCRT_stdout);
4832 _unlock_file(MSVCRT_stdout);
4833 return ret >= 0 ? 0 : WEOF;
4836 /*********************************************************************
4837 * remove (MSVCRT.@)
4839 int CDECL remove(const char *path)
4841 TRACE("(%s)\n", path);
4842 if (DeleteFileA(path))
4843 return 0;
4844 TRACE(":failed (%ld)\n", GetLastError());
4845 msvcrt_set_errno(GetLastError());
4846 return -1;
4849 /*********************************************************************
4850 * _wremove (MSVCRT.@)
4852 int CDECL _wremove(const wchar_t *path)
4854 TRACE("(%s)\n", debugstr_w(path));
4855 if (DeleteFileW(path))
4856 return 0;
4857 TRACE(":failed (%ld)\n", GetLastError());
4858 msvcrt_set_errno(GetLastError());
4859 return -1;
4862 /*********************************************************************
4863 * rename (MSVCRT.@)
4865 int CDECL rename(const char *oldpath,const char *newpath)
4867 TRACE(":from %s to %s\n", oldpath, newpath);
4868 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4869 return 0;
4870 TRACE(":failed (%ld)\n", GetLastError());
4871 msvcrt_set_errno(GetLastError());
4872 return -1;
4875 /*********************************************************************
4876 * _wrename (MSVCRT.@)
4878 int CDECL _wrename(const wchar_t *oldpath,const wchar_t *newpath)
4880 TRACE(":from %s to %s\n", debugstr_w(oldpath), debugstr_w(newpath));
4881 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4882 return 0;
4883 TRACE(":failed (%ld)\n", GetLastError());
4884 msvcrt_set_errno(GetLastError());
4885 return -1;
4888 /*********************************************************************
4889 * setvbuf (MSVCRT.@)
4891 int CDECL setvbuf(FILE* file, char *buf, int mode, size_t size)
4893 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4894 if(!MSVCRT_CHECK_PMT(mode==_IONBF || mode==_IOFBF || mode==_IOLBF)) return -1;
4895 if(!MSVCRT_CHECK_PMT(mode==_IONBF || (size>=2 && size<=INT_MAX))) return -1;
4897 _lock_file(file);
4899 _fflush_nolock(file);
4900 if(file->_flag & _IOMYBUF)
4901 free(file->_base);
4902 file->_flag &= ~(_IONBF | _IOMYBUF | MSVCRT__USERBUF);
4903 file->_cnt = 0;
4905 if(mode == _IONBF) {
4906 file->_flag |= _IONBF;
4907 file->_base = file->_ptr = (char*)&file->_charbuf;
4908 file->_bufsiz = 2;
4909 }else if(buf) {
4910 file->_base = file->_ptr = buf;
4911 file->_flag |= MSVCRT__USERBUF;
4912 file->_bufsiz = size;
4913 }else {
4914 file->_base = file->_ptr = malloc(size);
4915 if(!file->_base) {
4916 file->_bufsiz = 0;
4917 _unlock_file(file);
4918 return -1;
4921 file->_flag |= _IOMYBUF;
4922 file->_bufsiz = size;
4924 _unlock_file(file);
4925 return 0;
4928 /*********************************************************************
4929 * setbuf (MSVCRT.@)
4931 void CDECL setbuf(FILE* file, char *buf)
4933 setvbuf(file, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
4936 static int tmpnam_helper(char *s, size_t size, LONG *tmpnam_unique, int tmp_max)
4938 char tmpstr[8];
4939 char *p = s;
4940 int digits;
4942 if (!MSVCRT_CHECK_PMT(s != NULL)) return EINVAL;
4944 if (size < 3) {
4945 if (size) *s = 0;
4946 *_errno() = ERANGE;
4947 return ERANGE;
4949 *p++ = '\\';
4950 *p++ = 's';
4951 size -= 2;
4952 digits = msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4953 if (digits+1 > size) {
4954 *s = 0;
4955 *_errno() = ERANGE;
4956 return ERANGE;
4958 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4959 p += digits;
4960 *p++ = '.';
4961 size -= digits+1;
4963 while(1) {
4964 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4965 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4966 break;
4969 digits = msvcrt_int_to_base32(digits, tmpstr);
4970 if (digits+1 > size) {
4971 *s = 0;
4972 *_errno() = ERANGE;
4973 return ERANGE;
4975 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4976 p[digits] = 0;
4978 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4979 GetLastError() == ERROR_FILE_NOT_FOUND)
4980 break;
4982 return 0;
4985 int CDECL tmpnam_s(char *s, size_t size)
4987 return tmpnam_helper(s, size, &tmpnam_s_unique, TMP_MAX_S);
4990 /*********************************************************************
4991 * tmpnam (MSVCRT.@)
4993 char * CDECL tmpnam(char *s)
4995 if (!s) {
4996 thread_data_t *data = msvcrt_get_thread_data();
4998 if(!data->tmpnam_buffer)
4999 data->tmpnam_buffer = malloc(MAX_PATH);
5001 s = data->tmpnam_buffer;
5004 return tmpnam_helper(s, -1, &tmpnam_unique, TMP_MAX) ? NULL : s;
5007 static int wtmpnam_helper(wchar_t *s, size_t size, LONG *tmpnam_unique, int tmp_max)
5009 wchar_t tmpstr[8];
5010 wchar_t *p = s;
5011 int digits;
5013 if (!MSVCRT_CHECK_PMT(s != NULL)) return EINVAL;
5015 if (size < 3) {
5016 if (size) *s = 0;
5017 *_errno() = ERANGE;
5018 return ERANGE;
5020 *p++ = '\\';
5021 *p++ = 's';
5022 size -= 2;
5023 digits = msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
5024 if (digits+1 > size) {
5025 *s = 0;
5026 *_errno() = ERANGE;
5027 return ERANGE;
5029 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5030 p += digits;
5031 *p++ = '.';
5032 size -= digits+1;
5034 while(1) {
5035 while ((digits = *tmpnam_unique)+1 < tmp_max) {
5036 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
5037 break;
5040 digits = msvcrt_int_to_base32_w(digits, tmpstr);
5041 if (digits+1 > size) {
5042 *s = 0;
5043 *_errno() = ERANGE;
5044 return ERANGE;
5046 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5047 p[digits] = 0;
5049 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
5050 GetLastError() == ERROR_FILE_NOT_FOUND)
5051 break;
5053 return 0;
5056 /*********************************************************************
5057 * _wtmpnam_s (MSVCRT.@)
5059 int CDECL _wtmpnam_s(wchar_t *s, size_t size)
5061 return wtmpnam_helper(s, size, &tmpnam_s_unique, TMP_MAX_S);
5064 /*********************************************************************
5065 * _wtmpnam (MSVCRT.@)
5067 wchar_t * CDECL _wtmpnam(wchar_t *s)
5069 if (!s) {
5070 thread_data_t *data = msvcrt_get_thread_data();
5072 if(!data->wtmpnam_buffer)
5073 data->wtmpnam_buffer = malloc(sizeof(wchar_t[MAX_PATH]));
5075 s = data->wtmpnam_buffer;
5078 return wtmpnam_helper(s, -1, &tmpnam_unique, TMP_MAX) ? NULL : s;
5081 /*********************************************************************
5082 * tmpfile (MSVCRT.@)
5084 FILE* CDECL tmpfile(void)
5086 char *filename = _tempnam(",", "t");
5087 int fd;
5088 FILE* file = NULL;
5090 LOCK_FILES();
5091 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY,
5092 _S_IREAD | _S_IWRITE);
5093 if (fd != -1 && (file = msvcrt_alloc_fp()))
5095 if (msvcrt_init_fp(file, fd, _IORW) == -1)
5097 file->_flag = 0;
5098 file = NULL;
5100 else file->_tmpfname = _strdup(filename);
5103 if(fd != -1 && !file)
5104 _close(fd);
5105 free(filename);
5106 UNLOCK_FILES();
5107 return file;
5110 /*********************************************************************
5111 * tmpfile_s (MSVCRT.@)
5113 int CDECL tmpfile_s(FILE** file)
5115 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
5117 *file = tmpfile();
5118 return 0;
5121 static int puts_clbk_file_a(void *file, int len, const char *str)
5123 return fwrite(str, sizeof(char), len, file);
5126 static int puts_clbk_file_w(void *file, int len, const wchar_t *str)
5128 int i, ret;
5130 _lock_file(file);
5132 if(!(get_ioinfo_nolock(((FILE*)file)->_file)->wxflag & WX_TEXT)) {
5133 ret = _fwrite_nolock(str, sizeof(wchar_t), len, file);
5134 _unlock_file(file);
5135 return ret;
5138 for(i=0; i<len; i++) {
5139 if(_fputwc_nolock(str[i], file) == WEOF) {
5140 _unlock_file(file);
5141 return -1;
5145 _unlock_file(file);
5146 return len;
5149 static int vfprintf_helper(DWORD options, FILE* file, const char *format,
5150 _locale_t locale, va_list valist)
5152 printf_arg args_ctx[_ARGMAX+1];
5153 BOOL tmp_buf;
5154 int ret;
5156 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5157 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5159 if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
5160 memset(args_ctx, 0, sizeof(args_ctx));
5161 ret = create_positional_ctx_a(args_ctx, format, valist);
5162 if(ret < 0) {
5163 _invalid_parameter(NULL, NULL, NULL, 0, 0);
5164 *_errno() = EINVAL;
5165 return ret;
5166 } else if(!ret)
5167 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5170 _lock_file(file);
5171 tmp_buf = add_std_buffer(file);
5172 ret = pf_printf_a(puts_clbk_file_a, file, format, locale, options,
5173 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
5174 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
5175 if(tmp_buf) remove_std_buffer(file);
5176 _unlock_file(file);
5178 return ret;
5181 static int vfwprintf_helper(DWORD options, FILE* file, const wchar_t *format,
5182 _locale_t locale, va_list valist)
5184 printf_arg args_ctx[_ARGMAX+1];
5185 BOOL tmp_buf;
5186 int ret;
5188 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5189 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5191 if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
5192 memset(args_ctx, 0, sizeof(args_ctx));
5193 ret = create_positional_ctx_w(args_ctx, format, valist);
5194 if(ret < 0) {
5195 _invalid_parameter(NULL, NULL, NULL, 0, 0);
5196 *_errno() = EINVAL;
5197 return ret;
5198 } else if(!ret)
5199 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5202 _lock_file(file);
5203 tmp_buf = add_std_buffer(file);
5204 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, options,
5205 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
5206 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
5207 if(tmp_buf) remove_std_buffer(file);
5208 _unlock_file(file);
5210 return ret;
5213 /*********************************************************************
5214 * _vfprintf_s_l (MSVCRT.@)
5216 int CDECL _vfprintf_s_l(FILE* file, const char *format,
5217 _locale_t locale, va_list valist)
5219 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5222 /*********************************************************************
5223 * _vfwprintf_s_l (MSVCRT.@)
5225 int CDECL _vfwprintf_s_l(FILE* file, const wchar_t *format,
5226 _locale_t locale, va_list valist)
5228 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5231 /*********************************************************************
5232 * vfprintf (MSVCRT.@)
5234 int CDECL vfprintf(FILE* file, const char *format, va_list valist)
5236 return vfprintf_helper(0, file, format, NULL, valist);
5239 /*********************************************************************
5240 * vfprintf_s (MSVCRT.@)
5242 int CDECL vfprintf_s(FILE* file, const char *format, va_list valist)
5244 return _vfprintf_s_l(file, format, NULL, valist);
5247 /*********************************************************************
5248 * vfwprintf (MSVCRT.@)
5250 int CDECL vfwprintf(FILE* file, const wchar_t *format, va_list valist)
5252 return vfwprintf_helper(0, file, format, NULL, valist);
5255 /*********************************************************************
5256 * vfwprintf_s (MSVCRT.@)
5258 int CDECL vfwprintf_s(FILE* file, const wchar_t *format, va_list valist)
5260 return _vfwprintf_s_l(file, format, NULL, valist);
5263 #if _MSVCR_VER >= 140
5265 /*********************************************************************
5266 * __stdio_common_vfprintf (UCRTBASE.@)
5268 int CDECL _stdio_common_vfprintf(unsigned __int64 options, FILE *file, const char *format,
5269 _locale_t locale, va_list valist)
5271 if (options & ~UCRTBASE_PRINTF_MASK)
5272 FIXME("options %#I64x not handled\n", options);
5274 return vfprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
5277 /*********************************************************************
5278 * __stdio_common_vfprintf_p (UCRTBASE.@)
5280 int CDECL __stdio_common_vfprintf_p(unsigned __int64 options, FILE *file, const char *format,
5281 _locale_t locale, va_list valist)
5283 if (options & ~UCRTBASE_PRINTF_MASK)
5284 FIXME("options %#I64x not handled\n", options);
5286 return vfprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_POSITIONAL_PARAMS
5287 | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5291 /*********************************************************************
5292 * __stdio_common_vfprintf_s (UCRTBASE.@)
5294 int CDECL __stdio_common_vfprintf_s(unsigned __int64 options, FILE *file, const char *format,
5295 _locale_t locale, va_list valist)
5297 if (options & ~UCRTBASE_PRINTF_MASK)
5298 FIXME("options %#I64x not handled\n", options);
5300 return vfprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5301 file, format, locale, valist);
5304 /*********************************************************************
5305 * __stdio_common_vfwprintf (UCRTBASE.@)
5307 int CDECL __stdio_common_vfwprintf(unsigned __int64 options, FILE *file, const wchar_t *format,
5308 _locale_t locale, va_list valist)
5310 if (options & ~UCRTBASE_PRINTF_MASK)
5311 FIXME("options %#I64x not handled\n", options);
5313 return vfwprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
5316 /*********************************************************************
5317 * __stdio_common_vfwprintf_p (UCRTBASE.@)
5319 int CDECL __stdio_common_vfwprintf_p(unsigned __int64 options, FILE *file, const wchar_t *format,
5320 _locale_t locale, va_list valist)
5322 if (options & ~UCRTBASE_PRINTF_MASK)
5323 FIXME("options %#I64x not handled\n", options);
5325 return vfwprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_POSITIONAL_PARAMS
5326 | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5330 /*********************************************************************
5331 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5333 int CDECL __stdio_common_vfwprintf_s(unsigned __int64 options, FILE *file, const wchar_t *format,
5334 _locale_t locale, va_list valist)
5336 if (options & ~UCRTBASE_PRINTF_MASK)
5337 FIXME("options %#I64x not handled\n", options);
5339 return vfwprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5340 file, format, locale, valist);
5343 #endif /* _MSVCR_VER >= 140 */
5345 /*********************************************************************
5346 * _vfprintf_l (MSVCRT.@)
5348 int CDECL _vfprintf_l(FILE* file, const char *format,
5349 _locale_t locale, va_list valist)
5351 return vfprintf_helper(0, file, format, locale, valist);
5354 /*********************************************************************
5355 * _vfwprintf_l (MSVCRT.@)
5357 int CDECL _vfwprintf_l(FILE* file, const wchar_t *format,
5358 _locale_t locale, va_list valist)
5360 return vfwprintf_helper(0, file, format, locale, valist);
5363 /*********************************************************************
5364 * _vfprintf_p_l (MSVCRT.@)
5366 int CDECL _vfprintf_p_l(FILE* file, const char *format,
5367 _locale_t locale, va_list valist)
5369 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5370 file, format, locale, valist);
5373 /*********************************************************************
5374 * _vfprintf_p (MSVCRT.@)
5376 int CDECL _vfprintf_p(FILE* file, const char *format, va_list valist)
5378 return _vfprintf_p_l(file, format, NULL, valist);
5381 /*********************************************************************
5382 * _vfwprintf_p_l (MSVCRT.@)
5384 int CDECL _vfwprintf_p_l(FILE* file, const wchar_t *format,
5385 _locale_t locale, va_list valist)
5387 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5388 file, format, locale, valist);
5391 /*********************************************************************
5392 * _vfwprintf_p (MSVCRT.@)
5394 int CDECL _vfwprintf_p(FILE* file, const wchar_t *format, va_list valist)
5396 return _vfwprintf_p_l(file, format, NULL, valist);
5399 /*********************************************************************
5400 * vprintf (MSVCRT.@)
5402 int CDECL vprintf(const char *format, va_list valist)
5404 return vfprintf(MSVCRT_stdout,format,valist);
5407 /*********************************************************************
5408 * vprintf_s (MSVCRT.@)
5410 int CDECL vprintf_s(const char *format, va_list valist)
5412 return vfprintf_s(MSVCRT_stdout,format,valist);
5415 /*********************************************************************
5416 * vwprintf (MSVCRT.@)
5418 int CDECL vwprintf(const wchar_t *format, va_list valist)
5420 return vfwprintf(MSVCRT_stdout,format,valist);
5423 /*********************************************************************
5424 * vwprintf_s (MSVCRT.@)
5426 int CDECL vwprintf_s(const wchar_t *format, va_list valist)
5428 return vfwprintf_s(MSVCRT_stdout,format,valist);
5431 /*********************************************************************
5432 * fprintf (MSVCRT.@)
5434 int WINAPIV fprintf(FILE* file, const char *format, ...)
5436 va_list valist;
5437 int res;
5438 va_start(valist, format);
5439 res = vfprintf(file, format, valist);
5440 va_end(valist);
5441 return res;
5444 /*********************************************************************
5445 * fprintf_s (MSVCRT.@)
5447 int WINAPIV fprintf_s(FILE* file, const char *format, ...)
5449 va_list valist;
5450 int res;
5451 va_start(valist, format);
5452 res = vfprintf_s(file, format, valist);
5453 va_end(valist);
5454 return res;
5457 /*********************************************************************
5458 * fwprintf (MSVCRT.@)
5460 int WINAPIV fwprintf(FILE* file, const wchar_t *format, ...)
5462 va_list valist;
5463 int res;
5464 va_start(valist, format);
5465 res = vfwprintf(file, format, valist);
5466 va_end(valist);
5467 return res;
5470 /*********************************************************************
5471 * fwprintf_s (MSVCRT.@)
5473 int WINAPIV fwprintf_s(FILE* file, const wchar_t *format, ...)
5475 va_list valist;
5476 int res;
5477 va_start(valist, format);
5478 res = vfwprintf_s(file, format, valist);
5479 va_end(valist);
5480 return res;
5483 /*********************************************************************
5484 * _fwprintf_l (MSVCRT.@)
5486 int WINAPIV _fwprintf_l(FILE* file, const wchar_t *format, _locale_t locale, ...)
5488 va_list valist;
5489 int res;
5490 va_start(valist, locale);
5491 res = _vfwprintf_l(file, format, locale, valist);
5492 va_end(valist);
5493 return res;
5496 /*********************************************************************
5497 * printf (MSVCRT.@)
5499 int WINAPIV printf(const char *format, ...)
5501 va_list valist;
5502 int res;
5503 va_start(valist, format);
5504 res = vfprintf(MSVCRT_stdout, format, valist);
5505 va_end(valist);
5506 return res;
5509 /*********************************************************************
5510 * printf_s (MSVCRT.@)
5512 int WINAPIV printf_s(const char *format, ...)
5514 va_list valist;
5515 int res;
5516 va_start(valist, format);
5517 res = vprintf_s(format, valist);
5518 va_end(valist);
5519 return res;
5522 /*********************************************************************
5523 * ungetc (MSVCRT.@)
5525 int CDECL ungetc(int c, FILE * file)
5527 int ret;
5529 if(!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
5531 _lock_file(file);
5532 ret = _ungetc_nolock(c, file);
5533 _unlock_file(file);
5535 return ret;
5538 /*********************************************************************
5539 * _ungetc_nolock (MSVCRT.@)
5541 int CDECL _ungetc_nolock(int c, FILE * file)
5543 if(!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
5545 if (c == EOF || !(file->_flag&_IOREAD ||
5546 (file->_flag&_IORW && !(file->_flag&_IOWRT))))
5547 return EOF;
5549 if((!(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF))
5550 && msvcrt_alloc_buffer(file))
5551 || (!file->_cnt && file->_ptr==file->_base))
5552 file->_ptr++;
5554 if(file->_ptr>file->_base) {
5555 file->_ptr--;
5556 if(file->_flag & _IOSTRG) {
5557 if(*file->_ptr != c) {
5558 file->_ptr++;
5559 return EOF;
5561 }else {
5562 *file->_ptr = c;
5564 file->_cnt++;
5565 file->_flag &= ~(_IOERR | _IOEOF);
5566 file->_flag |= _IOREAD;
5567 return c;
5570 return EOF;
5573 /*********************************************************************
5574 * ungetwc (MSVCRT.@)
5576 wint_t CDECL ungetwc(wint_t wc, FILE * file)
5578 wint_t ret;
5580 if(!MSVCRT_CHECK_PMT(file != NULL)) return WEOF;
5582 _lock_file(file);
5583 ret = _ungetwc_nolock(wc, file);
5584 _unlock_file(file);
5586 return ret;
5589 /*********************************************************************
5590 * _ungetwc_nolock (MSVCRT.@)
5592 wint_t CDECL _ungetwc_nolock(wint_t wc, FILE * file)
5594 wchar_t mwc = wc;
5596 if(!MSVCRT_CHECK_PMT(file != NULL)) return WEOF;
5597 if (wc == WEOF)
5598 return WEOF;
5600 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5601 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5602 unsigned char * pp = (unsigned char *)&mwc;
5603 int i;
5605 for(i=sizeof(wchar_t)-1;i>=0;i--) {
5606 if(pp[i] != _ungetc_nolock(pp[i],file))
5607 return WEOF;
5609 }else {
5610 char mbs[MB_LEN_MAX];
5611 int len;
5613 len = wctomb(mbs, mwc);
5614 if(len == -1)
5615 return WEOF;
5617 for(len--; len>=0; len--) {
5618 if(mbs[len] != _ungetc_nolock(mbs[len], file))
5619 return WEOF;
5623 return mwc;
5626 /*********************************************************************
5627 * wprintf (MSVCRT.@)
5629 int WINAPIV wprintf(const wchar_t *format, ...)
5631 va_list valist;
5632 int res;
5633 va_start(valist, format);
5634 res = vwprintf(format, valist);
5635 va_end(valist);
5636 return res;
5639 /*********************************************************************
5640 * wprintf_s (MSVCRT.@)
5642 int WINAPIV wprintf_s(const wchar_t *format, ...)
5644 va_list valist;
5645 int res;
5646 va_start(valist, format);
5647 res = vwprintf_s(format, valist);
5648 va_end(valist);
5649 return res;
5652 /*********************************************************************
5653 * _getmaxstdio (MSVCRT.@)
5655 int CDECL _getmaxstdio(void)
5657 return MSVCRT_max_streams;
5660 /*********************************************************************
5661 * _setmaxstdio (MSVCRT.@)
5663 int CDECL _setmaxstdio(int newmax)
5665 TRACE("%d\n", newmax);
5667 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5668 return -1;
5670 MSVCRT_max_streams = newmax;
5671 return MSVCRT_max_streams;
5674 #if _MSVCR_VER >= 140
5675 /*********************************************************************
5676 * _get_stream_buffer_pointers (UCRTBASE.@)
5678 int CDECL _get_stream_buffer_pointers(FILE *file, char*** base,
5679 char*** ptr, int** count)
5681 if (base)
5682 *base = &file->_base;
5683 if (ptr)
5684 *ptr = &file->_ptr;
5685 if (count)
5686 *count = &file->_cnt;
5687 return 0;
5689 #endif