ntdll: Allow NtQueryVolumeInformationFile to make async volume information queries.
[wine/zf.git] / dlls / msvcrt / file.c
blob73e1a82476f7a2b08384a0ce9222e06ac5e4d48f
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
25 * TODO
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
29 #include <direct.h>
30 #include <fcntl.h>
31 #include <io.h>
32 #include <share.h>
33 #include <stdarg.h>
34 #include <sys/locking.h>
35 #include <sys/types.h>
36 #include <sys/utime.h>
37 #include <time.h>
38 #include <limits.h>
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wincon.h"
43 #include "winternl.h"
44 #include "winnls.h"
45 #include "msvcrt.h"
46 #include "mtdll.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
52 #undef _fstat
53 #undef _fstati64
54 #undef _stat
55 #undef _stati64
56 #undef _wstat
57 #undef _wstati64
58 #undef _fstat32
59 #undef _fstat32i64
60 #undef _stat32i64
61 #undef _stat32
62 #undef _wstat32
63 #undef _wstat32i64
64 #undef _fstat64i32
65 #undef _fstat64
66 #undef _stat64
67 #undef _stat64i32
68 #undef _wstat64i32
69 #undef _wstat64
71 /* for stat mode, permissions apply to all,owner and group */
72 #define ALL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
73 #define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
74 #define ALL_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
76 /* _access() bit flags FIXME: incomplete */
77 #define MSVCRT_W_OK 0x02
78 #define MSVCRT_R_OK 0x04
80 /* values for wxflag in file descriptor */
81 #define WX_OPEN 0x01
82 #define WX_ATEOF 0x02
83 #define WX_READNL 0x04 /* read started with \n */
84 #define WX_PIPE 0x08
85 #define WX_DONTINHERIT 0x10
86 #define WX_APPEND 0x20
87 #define WX_TTY 0x40
88 #define WX_TEXT 0x80
90 /* values for exflag - it's used differently in msvcr90.dll*/
91 #define EF_UTF8 0x01
92 #define EF_UTF16 0x02
93 #define EF_CRIT_INIT 0x04
94 #define EF_UNK_UNICODE 0x08
96 static char utf8_bom[3] = { 0xef, 0xbb, 0xbf };
97 static char utf16_bom[2] = { 0xff, 0xfe };
99 /* FIXME: this should be allocated dynamically */
100 #define MSVCRT_MAX_FILES 2048
101 #define MSVCRT_FD_BLOCK_SIZE 32
103 #define MSVCRT_INTERNAL_BUFSIZ 4096
105 /* ioinfo structure size is different in msvcrXX.dll's */
106 typedef struct {
107 HANDLE handle;
108 unsigned char wxflag;
109 char lookahead[3];
110 int exflag;
111 CRITICAL_SECTION crit;
112 #if _MSVCR_VER >= 80
113 char textmode : 7;
114 char unicode : 1;
115 char pipech2[2];
116 __int64 startpos;
117 BOOL utf8translations;
118 #endif
119 #if _MSVCR_VER >= 90
120 char dbcsBuffer;
121 BOOL dbcsBufferUsed;
122 #endif
123 } ioinfo;
125 /*********************************************************************
126 * __pioinfo (MSVCRT.@)
127 * array of pointers to ioinfo arrays [32]
129 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
131 /*********************************************************************
132 * __badioinfo (MSVCRT.@)
134 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
136 typedef struct {
137 FILE file;
138 CRITICAL_SECTION crit;
139 } file_crit;
141 FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
142 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
143 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
145 /* INTERNAL: process umask */
146 static int MSVCRT_umask = 0;
148 /* INTERNAL: static data for tmpnam and _wtmpname functions */
149 static int tmpnam_unique;
150 static int tmpnam_s_unique;
152 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
153 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
154 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
155 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
157 #define TOUL(x) (ULONGLONG)(x)
158 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
159 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
160 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
161 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
163 /* This critical section protects the MSVCRT_fstreams table
164 * and MSVCRT_stream_idx from race conditions. It also
165 * protects fd critical sections creation code.
167 static CRITICAL_SECTION MSVCRT_file_cs;
168 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
170 0, 0, &MSVCRT_file_cs,
171 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
172 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
174 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
175 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
176 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
178 static void msvcrt_stat64_to_stat(const struct _stat64 *buf64, struct _stat *buf)
180 buf->st_dev = buf64->st_dev;
181 buf->st_ino = buf64->st_ino;
182 buf->st_mode = buf64->st_mode;
183 buf->st_nlink = buf64->st_nlink;
184 buf->st_uid = buf64->st_uid;
185 buf->st_gid = buf64->st_gid;
186 buf->st_rdev = buf64->st_rdev;
187 buf->st_size = buf64->st_size;
188 buf->st_atime = buf64->st_atime;
189 buf->st_mtime = buf64->st_mtime;
190 buf->st_ctime = buf64->st_ctime;
193 static void msvcrt_stat64_to_stati64(const struct _stat64 *buf64, struct _stati64 *buf)
195 buf->st_dev = buf64->st_dev;
196 buf->st_ino = buf64->st_ino;
197 buf->st_mode = buf64->st_mode;
198 buf->st_nlink = buf64->st_nlink;
199 buf->st_uid = buf64->st_uid;
200 buf->st_gid = buf64->st_gid;
201 buf->st_rdev = buf64->st_rdev;
202 buf->st_size = buf64->st_size;
203 buf->st_atime = buf64->st_atime;
204 buf->st_mtime = buf64->st_mtime;
205 buf->st_ctime = buf64->st_ctime;
208 static void msvcrt_stat64_to_stat32(const struct _stat64 *buf64, struct _stat32 *buf)
210 buf->st_dev = buf64->st_dev;
211 buf->st_ino = buf64->st_ino;
212 buf->st_mode = buf64->st_mode;
213 buf->st_nlink = buf64->st_nlink;
214 buf->st_uid = buf64->st_uid;
215 buf->st_gid = buf64->st_gid;
216 buf->st_rdev = buf64->st_rdev;
217 buf->st_size = buf64->st_size;
218 buf->st_atime = buf64->st_atime;
219 buf->st_mtime = buf64->st_mtime;
220 buf->st_ctime = buf64->st_ctime;
223 static void msvcrt_stat64_to_stat64i32(const struct _stat64 *buf64, struct _stat64i32 *buf)
225 buf->st_dev = buf64->st_dev;
226 buf->st_ino = buf64->st_ino;
227 buf->st_mode = buf64->st_mode;
228 buf->st_nlink = buf64->st_nlink;
229 buf->st_uid = buf64->st_uid;
230 buf->st_gid = buf64->st_gid;
231 buf->st_rdev = buf64->st_rdev;
232 buf->st_size = buf64->st_size;
233 buf->st_atime = buf64->st_atime;
234 buf->st_mtime = buf64->st_mtime;
235 buf->st_ctime = buf64->st_ctime;
238 static void msvcrt_stat64_to_stat32i64(const struct _stat64 *buf64, struct _stat32i64 *buf)
240 buf->st_dev = buf64->st_dev;
241 buf->st_ino = buf64->st_ino;
242 buf->st_mode = buf64->st_mode;
243 buf->st_nlink = buf64->st_nlink;
244 buf->st_uid = buf64->st_uid;
245 buf->st_gid = buf64->st_gid;
246 buf->st_rdev = buf64->st_rdev;
247 buf->st_size = buf64->st_size;
248 buf->st_atime = buf64->st_atime;
249 buf->st_mtime = buf64->st_mtime;
250 buf->st_ctime = buf64->st_ctime;
253 static void time_to_filetime( __time64_t time, FILETIME *ft )
255 /* 1601 to 1970 is 369 years plus 89 leap days */
256 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
258 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
259 ft->dwHighDateTime = ticks >> 32;
260 ft->dwLowDateTime = ticks;
263 static inline ioinfo* get_ioinfo_nolock(int fd)
265 ioinfo *ret = NULL;
266 if(fd>=0 && fd<MSVCRT_MAX_FILES)
267 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
268 if(!ret)
269 return &MSVCRT___badioinfo;
271 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
274 static inline void init_ioinfo_cs(ioinfo *info)
276 if(!(info->exflag & EF_CRIT_INIT)) {
277 LOCK_FILES();
278 if(!(info->exflag & EF_CRIT_INIT)) {
279 InitializeCriticalSection(&info->crit);
280 info->exflag |= EF_CRIT_INIT;
282 UNLOCK_FILES();
286 static inline ioinfo* get_ioinfo(int fd)
288 ioinfo *ret = get_ioinfo_nolock(fd);
289 if(ret == &MSVCRT___badioinfo)
290 return ret;
291 init_ioinfo_cs(ret);
292 EnterCriticalSection(&ret->crit);
293 return ret;
296 static inline BOOL alloc_pioinfo_block(int fd)
298 ioinfo *block;
299 int i;
301 if(fd<0 || fd>=MSVCRT_MAX_FILES)
303 *_errno() = ENFILE;
304 return FALSE;
307 block = calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
308 if(!block)
310 WARN(":out of memory!\n");
311 *_errno() = ENOMEM;
312 return FALSE;
314 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
315 block[i].handle = INVALID_HANDLE_VALUE;
316 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE], block, NULL))
317 free(block);
318 return TRUE;
321 static inline ioinfo* get_ioinfo_alloc_fd(int fd)
323 ioinfo *ret;
325 ret = get_ioinfo(fd);
326 if(ret != &MSVCRT___badioinfo)
327 return ret;
329 if(!alloc_pioinfo_block(fd))
330 return &MSVCRT___badioinfo;
332 return get_ioinfo(fd);
335 static inline ioinfo* get_ioinfo_alloc(int *fd)
337 int i;
339 *fd = -1;
340 for(i=0; i<MSVCRT_MAX_FILES; i++)
342 ioinfo *info = get_ioinfo_nolock(i);
344 if(info == &MSVCRT___badioinfo)
346 if(!alloc_pioinfo_block(i))
347 return &MSVCRT___badioinfo;
348 info = get_ioinfo_nolock(i);
351 init_ioinfo_cs(info);
352 if(TryEnterCriticalSection(&info->crit))
354 if(info->handle == INVALID_HANDLE_VALUE)
356 *fd = i;
357 return info;
359 LeaveCriticalSection(&info->crit);
363 WARN(":files exhausted!\n");
364 *_errno() = ENFILE;
365 return &MSVCRT___badioinfo;
368 static inline void release_ioinfo(ioinfo *info)
370 if(info!=&MSVCRT___badioinfo && info->exflag & EF_CRIT_INIT)
371 LeaveCriticalSection(&info->crit);
374 static inline FILE* msvcrt_get_file(int i)
376 file_crit *ret;
378 if(i >= MSVCRT_max_streams)
379 return NULL;
381 if(i < _IOB_ENTRIES)
382 return &MSVCRT__iob[i];
384 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
385 if(!ret) {
386 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
387 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
388 ERR("out of memory\n");
389 *_errno() = ENOMEM;
390 return NULL;
393 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
394 } else
395 ret += i%MSVCRT_FD_BLOCK_SIZE;
397 return &ret->file;
400 /* INTERNAL: free a file entry fd */
401 static void msvcrt_free_fd(int fd)
403 ioinfo *fdinfo = get_ioinfo(fd);
405 if(fdinfo != &MSVCRT___badioinfo)
407 fdinfo->handle = INVALID_HANDLE_VALUE;
408 fdinfo->wxflag = 0;
410 TRACE(":fd (%d) freed\n",fd);
412 if (fd < 3)
414 switch (fd)
416 case 0:
417 SetStdHandle(STD_INPUT_HANDLE, 0);
418 break;
419 case 1:
420 SetStdHandle(STD_OUTPUT_HANDLE, 0);
421 break;
422 case 2:
423 SetStdHandle(STD_ERROR_HANDLE, 0);
424 break;
427 release_ioinfo(fdinfo);
430 static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag)
432 fdinfo->handle = hand;
433 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_TTY));
434 fdinfo->lookahead[0] = '\n';
435 fdinfo->lookahead[1] = '\n';
436 fdinfo->lookahead[2] = '\n';
437 fdinfo->exflag &= EF_CRIT_INIT;
439 if (hand == MSVCRT_NO_CONSOLE) hand = 0;
440 switch (fdinfo-MSVCRT___pioinfo[0])
442 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
443 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
444 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
448 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
449 static int msvcrt_alloc_fd(HANDLE hand, int flag)
451 int fd;
452 ioinfo *info = get_ioinfo_alloc(&fd);
454 TRACE(":handle (%p) allocating fd (%d)\n", hand, fd);
456 if(info == &MSVCRT___badioinfo)
457 return -1;
459 msvcrt_set_fd(info, hand, flag);
460 release_ioinfo(info);
461 return fd;
464 /* INTERNAL: Allocate a FILE* for an fd slot */
465 /* caller must hold the files lock */
466 static FILE* msvcrt_alloc_fp(void)
468 int i;
469 FILE *file;
471 for (i = 3; i < MSVCRT_max_streams; i++)
473 file = msvcrt_get_file(i);
474 if (!file)
475 return NULL;
477 if (file->_flag == 0)
479 if (i == MSVCRT_stream_idx)
481 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
483 InitializeCriticalSection(&((file_crit*)file)->crit);
484 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
486 MSVCRT_stream_idx++;
488 return file;
492 return NULL;
495 /* INTERNAL: initialize a FILE* from an open fd */
496 static int msvcrt_init_fp(FILE* file, int fd, unsigned stream_flags)
498 TRACE(":fd (%d) allocating FILE*\n",fd);
499 if (!(get_ioinfo_nolock(fd)->wxflag & WX_OPEN))
501 WARN(":invalid fd %d\n",fd);
502 *__doserrno() = 0;
503 *_errno() = EBADF;
504 return -1;
506 file->_ptr = file->_base = NULL;
507 file->_cnt = 0;
508 file->_file = fd;
509 file->_flag = stream_flags;
510 file->_tmpfname = NULL;
512 TRACE(":got FILE* (%p)\n",file);
513 return 0;
516 /* INTERNAL: Create an inheritance data block (for spawned process)
517 * The inheritance block is made of:
518 * 00 int nb of file descriptor (NBFD)
519 * 04 char file flags (wxflag): repeated for each fd
520 * 4+NBFD HANDLE file handle: repeated for each fd
522 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
524 int fd, last_fd;
525 char* wxflag_ptr;
526 HANDLE* handle_ptr;
527 ioinfo* fdinfo;
529 for (last_fd=MSVCRT_MAX_FILES-1; last_fd>=0; last_fd--)
530 if (get_ioinfo_nolock(last_fd)->handle != INVALID_HANDLE_VALUE)
531 break;
532 last_fd++;
534 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * last_fd;
535 *block = calloc(1, *size);
536 if (!*block)
538 *size = 0;
539 return FALSE;
541 wxflag_ptr = (char*)*block + sizeof(unsigned);
542 handle_ptr = (HANDLE*)(wxflag_ptr + last_fd);
544 *(unsigned*)*block = last_fd;
545 for (fd = 0; fd < last_fd; fd++)
547 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
548 fdinfo = get_ioinfo(fd);
549 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
551 *wxflag_ptr = fdinfo->wxflag;
552 *handle_ptr = fdinfo->handle;
554 else
556 *wxflag_ptr = 0;
557 *handle_ptr = INVALID_HANDLE_VALUE;
559 release_ioinfo(fdinfo);
560 wxflag_ptr++; handle_ptr++;
562 return TRUE;
565 /* INTERNAL: Set up all file descriptors,
566 * as well as default streams (stdin, stderr and stdout)
568 void msvcrt_init_io(void)
570 STARTUPINFOA si;
571 int i;
572 ioinfo *fdinfo;
574 GetStartupInfoA(&si);
575 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
577 BYTE* wxflag_ptr;
578 HANDLE* handle_ptr;
579 unsigned int count;
581 count = *(unsigned*)si.lpReserved2;
582 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
583 handle_ptr = (HANDLE*)(wxflag_ptr + count);
585 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
586 count = min(count, MSVCRT_MAX_FILES);
587 for (i = 0; i < count; i++)
589 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
591 fdinfo = get_ioinfo_alloc_fd(i);
592 if(fdinfo != &MSVCRT___badioinfo)
593 msvcrt_set_fd(fdinfo, *handle_ptr, *wxflag_ptr);
594 release_ioinfo(fdinfo);
597 wxflag_ptr++; handle_ptr++;
601 fdinfo = get_ioinfo_alloc_fd(STDIN_FILENO);
602 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
603 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
604 DWORD flags = WX_OPEN | WX_TEXT;
605 DWORD type = GetFileType(h);
607 if (type == FILE_TYPE_UNKNOWN) {
608 h = MSVCRT_NO_CONSOLE;
609 flags |= WX_TTY;
610 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
611 flags |= WX_TTY;
612 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
613 flags |= WX_PIPE;
616 msvcrt_set_fd(fdinfo, h, flags);
618 release_ioinfo(fdinfo);
620 fdinfo = get_ioinfo_alloc_fd(STDOUT_FILENO);
621 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
622 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
623 DWORD flags = WX_OPEN | WX_TEXT;
624 DWORD type = GetFileType(h);
626 if (type == FILE_TYPE_UNKNOWN) {
627 h = MSVCRT_NO_CONSOLE;
628 flags |= WX_TTY;
629 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
630 flags |= WX_TTY;
631 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
632 flags |= WX_PIPE;
635 msvcrt_set_fd(fdinfo, h, flags);
637 release_ioinfo(fdinfo);
639 fdinfo = get_ioinfo_alloc_fd(STDERR_FILENO);
640 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
641 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
642 DWORD flags = WX_OPEN | WX_TEXT;
643 DWORD type = GetFileType(h);
645 if (type == FILE_TYPE_UNKNOWN) {
646 h = MSVCRT_NO_CONSOLE;
647 flags |= WX_TTY;
648 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
649 flags |= WX_TTY;
650 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
651 flags |= WX_PIPE;
654 msvcrt_set_fd(fdinfo, h, flags);
656 release_ioinfo(fdinfo);
658 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO)->handle,
659 get_ioinfo_nolock(STDOUT_FILENO)->handle,
660 get_ioinfo_nolock(STDERR_FILENO)->handle);
662 memset(MSVCRT__iob,0,3*sizeof(FILE));
663 for (i = 0; i < 3; i++)
665 /* FILE structs for stdin/out/err are static and never deleted */
666 MSVCRT__iob[i]._file = get_ioinfo_nolock(i)->handle == MSVCRT_NO_CONSOLE ?
667 MSVCRT_NO_CONSOLE_FD : i;
668 MSVCRT__iob[i]._tmpfname = NULL;
669 MSVCRT__iob[i]._flag = (i == 0) ? _IOREAD : _IOWRT;
671 MSVCRT_stream_idx = 3;
674 /* INTERNAL: Flush stdio file buffer */
675 static int msvcrt_flush_buffer(FILE* file)
677 int ret = 0;
679 if((file->_flag & (_IOREAD|_IOWRT)) == _IOWRT &&
680 file->_flag & (_IOMYBUF|MSVCRT__USERBUF)) {
681 int cnt=file->_ptr-file->_base;
682 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
683 file->_flag |= _IOERR;
684 ret = EOF;
685 } else if(file->_flag & _IORW) {
686 file->_flag &= ~_IOWRT;
690 file->_ptr=file->_base;
691 file->_cnt=0;
692 return ret;
695 /*********************************************************************
696 * _isatty (MSVCRT.@)
698 int CDECL _isatty(int fd)
700 TRACE(":fd (%d)\n",fd);
702 return get_ioinfo_nolock(fd)->wxflag & WX_TTY;
705 /* INTERNAL: Allocate stdio file buffer */
706 static BOOL msvcrt_alloc_buffer(FILE* file)
708 if((file->_file==STDOUT_FILENO || file->_file==STDERR_FILENO)
709 && _isatty(file->_file))
710 return FALSE;
712 file->_base = calloc(1, MSVCRT_INTERNAL_BUFSIZ);
713 if(file->_base) {
714 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
715 file->_flag |= _IOMYBUF;
716 } else {
717 file->_base = (char*)(&file->_charbuf);
718 file->_bufsiz = 2;
719 file->_flag |= _IONBF;
721 file->_ptr = file->_base;
722 file->_cnt = 0;
723 return TRUE;
726 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
727 static BOOL add_std_buffer(FILE *file)
729 static char buffers[2][BUFSIZ];
731 if((file->_file!=STDOUT_FILENO && file->_file!=STDERR_FILENO)
732 || (file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF))
733 || !_isatty(file->_file))
734 return FALSE;
736 file->_ptr = file->_base = buffers[file->_file == STDOUT_FILENO ? 0 : 1];
737 file->_bufsiz = file->_cnt = BUFSIZ;
738 file->_flag |= MSVCRT__USERBUF;
739 return TRUE;
742 /* INTERNAL: Removes temporary buffer from stdout or stderr */
743 /* Only call this function when add_std_buffer returned TRUE */
744 static void remove_std_buffer(FILE *file)
746 msvcrt_flush_buffer(file);
747 file->_ptr = file->_base = NULL;
748 file->_bufsiz = file->_cnt = 0;
749 file->_flag &= ~MSVCRT__USERBUF;
752 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
753 static int msvcrt_int_to_base32(int num, char *str)
755 char *p;
756 int n = num;
757 int digits = 0;
759 while (n != 0)
761 n >>= 5;
762 digits++;
764 p = str + digits;
765 *p = 0;
766 while (--p >= str)
768 *p = (num & 31) + '0';
769 if (*p > '9')
770 *p += ('a' - '0' - 10);
771 num >>= 5;
774 return digits;
777 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
778 static int msvcrt_int_to_base32_w(int num, wchar_t *str)
780 wchar_t *p;
781 int n = num;
782 int digits = 0;
784 while (n != 0)
786 n >>= 5;
787 digits++;
789 p = str + digits;
790 *p = 0;
791 while (--p >= str)
793 *p = (num & 31) + '0';
794 if (*p > '9')
795 *p += ('a' - '0' - 10);
796 num >>= 5;
799 return digits;
802 /*********************************************************************
803 * __iob_func (MSVCRT.@)
805 #undef __iob_func
806 FILE * CDECL __iob_func(void)
808 return &MSVCRT__iob[0];
811 #if _MSVCR_VER >= 140
812 /*********************************************************************
813 * __acrt_iob_func(UCRTBASE.@)
815 FILE * CDECL __acrt_iob_func(unsigned idx)
817 return &MSVCRT__iob[idx];
819 #endif
821 /*********************************************************************
822 * _access (MSVCRT.@)
824 int CDECL _access(const char *filename, int mode)
826 DWORD attr = GetFileAttributesA(filename);
828 TRACE("(%s,%d) %d\n",filename,mode,attr);
830 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
832 msvcrt_set_errno(GetLastError());
833 return -1;
835 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
837 msvcrt_set_errno(ERROR_ACCESS_DENIED);
838 return -1;
840 return 0;
843 /*********************************************************************
844 * _access_s (MSVCRT.@)
846 int CDECL _access_s(const char *filename, int mode)
848 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *_errno();
849 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *_errno();
851 if (_access(filename, mode) == -1)
852 return *_errno();
853 return 0;
856 /*********************************************************************
857 * _waccess (MSVCRT.@)
859 int CDECL _waccess(const wchar_t *filename, int mode)
861 DWORD attr = GetFileAttributesW(filename);
863 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
865 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
867 msvcrt_set_errno(GetLastError());
868 return -1;
870 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
872 msvcrt_set_errno(ERROR_ACCESS_DENIED);
873 return -1;
875 return 0;
878 /*********************************************************************
879 * _waccess_s (MSVCRT.@)
881 int CDECL _waccess_s(const wchar_t *filename, int mode)
883 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *_errno();
884 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *_errno();
886 if (_waccess(filename, mode) == -1)
887 return *_errno();
888 return 0;
891 /*********************************************************************
892 * _chmod (MSVCRT.@)
894 int CDECL _chmod(const char *path, int flags)
896 DWORD oldFlags = GetFileAttributesA(path);
898 if (oldFlags != INVALID_FILE_ATTRIBUTES)
900 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
901 oldFlags | FILE_ATTRIBUTE_READONLY;
903 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
904 return 0;
906 msvcrt_set_errno(GetLastError());
907 return -1;
910 /*********************************************************************
911 * _wchmod (MSVCRT.@)
913 int CDECL _wchmod(const wchar_t *path, int flags)
915 DWORD oldFlags = GetFileAttributesW(path);
917 if (oldFlags != INVALID_FILE_ATTRIBUTES)
919 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
920 oldFlags | FILE_ATTRIBUTE_READONLY;
922 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
923 return 0;
925 msvcrt_set_errno(GetLastError());
926 return -1;
929 /*********************************************************************
930 * _unlink (MSVCRT.@)
932 int CDECL _unlink(const char *path)
934 TRACE("%s\n",debugstr_a(path));
935 if(DeleteFileA(path))
936 return 0;
937 TRACE("failed (%d)\n",GetLastError());
938 msvcrt_set_errno(GetLastError());
939 return -1;
942 /*********************************************************************
943 * _wunlink (MSVCRT.@)
945 int CDECL _wunlink(const wchar_t *path)
947 TRACE("(%s)\n",debugstr_w(path));
948 if(DeleteFileW(path))
949 return 0;
950 TRACE("failed (%d)\n",GetLastError());
951 msvcrt_set_errno(GetLastError());
952 return -1;
955 /*********************************************************************
956 * _commit (MSVCRT.@)
958 int CDECL _commit(int fd)
960 ioinfo *info = get_ioinfo(fd);
961 int ret;
963 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
965 if (info->handle == INVALID_HANDLE_VALUE)
966 ret = -1;
967 else if (!FlushFileBuffers(info->handle))
969 if (GetLastError() == ERROR_INVALID_HANDLE)
971 /* FlushFileBuffers fails for console handles
972 * so we ignore this error.
974 ret = 0;
976 else
978 TRACE(":failed-last error (%d)\n",GetLastError());
979 msvcrt_set_errno(GetLastError());
980 ret = -1;
983 else
985 TRACE(":ok\n");
986 ret = 0;
989 release_ioinfo(info);
990 return ret;
993 /* INTERNAL: Flush all stream buffer */
994 static int msvcrt_flush_all_buffers(int mask)
996 int i, num_flushed = 0;
997 FILE *file;
999 LOCK_FILES();
1000 for (i = 0; i < MSVCRT_stream_idx; i++) {
1001 file = msvcrt_get_file(i);
1003 if (file->_flag)
1005 if(file->_flag & mask) {
1006 fflush(file);
1007 num_flushed++;
1011 UNLOCK_FILES();
1013 TRACE(":flushed (%d) handles\n",num_flushed);
1014 return num_flushed;
1017 /*********************************************************************
1018 * _flushall (MSVCRT.@)
1020 int CDECL _flushall(void)
1022 return msvcrt_flush_all_buffers(_IOWRT | _IOREAD);
1025 /*********************************************************************
1026 * fflush (MSVCRT.@)
1028 int CDECL fflush(FILE* file)
1030 int ret;
1032 if(!file) {
1033 msvcrt_flush_all_buffers(_IOWRT);
1034 ret = 0;
1035 } else {
1036 _lock_file(file);
1037 ret = _fflush_nolock(file);
1038 _unlock_file(file);
1041 return ret;
1044 /*********************************************************************
1045 * _fflush_nolock (MSVCRT.@)
1047 int CDECL _fflush_nolock(FILE* file)
1049 int res;
1051 if(!file) {
1052 msvcrt_flush_all_buffers(_IOWRT);
1053 return 0;
1056 res = msvcrt_flush_buffer(file);
1057 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
1058 res = _commit(file->_file) ? EOF : 0;
1059 return res;
1062 /*********************************************************************
1063 * _close (MSVCRT.@)
1065 int CDECL _close(int fd)
1067 ioinfo *info = get_ioinfo(fd);
1068 int ret;
1070 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1072 if (fd == MSVCRT_NO_CONSOLE_FD) {
1073 *_errno() = EBADF;
1074 ret = -1;
1075 } else if (!MSVCRT_CHECK_PMT_ERR(info->wxflag & WX_OPEN, EBADF)) {
1076 ret = -1;
1077 } else if (fd == STDOUT_FILENO &&
1078 info->handle == get_ioinfo_nolock(STDERR_FILENO)->handle) {
1079 msvcrt_free_fd(fd);
1080 ret = 0;
1081 } else if (fd == STDERR_FILENO &&
1082 info->handle == get_ioinfo_nolock(STDOUT_FILENO)->handle) {
1083 msvcrt_free_fd(fd);
1084 ret = 0;
1085 } else {
1086 ret = CloseHandle(info->handle) ? 0 : -1;
1087 msvcrt_free_fd(fd);
1088 if (ret) {
1089 WARN(":failed-last error (%d)\n",GetLastError());
1090 msvcrt_set_errno(GetLastError());
1093 release_ioinfo(info);
1094 return ret;
1097 /*********************************************************************
1098 * _dup2 (MSVCRT.@)
1099 * NOTES
1100 * MSDN isn't clear on this point, but the remarks for _pipe
1101 * indicate file descriptors duplicated with _dup and _dup2 are always
1102 * inheritable.
1104 int CDECL _dup2(int od, int nd)
1106 ioinfo *info_od, *info_nd;
1107 int ret;
1109 TRACE("(od=%d, nd=%d)\n", od, nd);
1111 if (od < nd)
1113 info_od = get_ioinfo(od);
1114 info_nd = get_ioinfo_alloc_fd(nd);
1116 else
1118 info_nd = get_ioinfo_alloc_fd(nd);
1119 info_od = get_ioinfo(od);
1122 if (info_nd == &MSVCRT___badioinfo)
1124 ret = -1;
1126 else if (info_od->wxflag & WX_OPEN)
1128 HANDLE handle;
1130 if (DuplicateHandle(GetCurrentProcess(), info_od->handle,
1131 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1133 int wxflag = info_od->wxflag & ~_O_NOINHERIT;
1135 if (info_nd->wxflag & WX_OPEN)
1136 _close(nd);
1138 msvcrt_set_fd(info_nd, handle, wxflag);
1139 /* _dup2 returns 0, not nd, on success */
1140 ret = 0;
1142 else
1144 ret = -1;
1145 msvcrt_set_errno(GetLastError());
1148 else
1150 *_errno() = EBADF;
1151 ret = -1;
1154 release_ioinfo(info_od);
1155 release_ioinfo(info_nd);
1156 return ret;
1159 /*********************************************************************
1160 * _dup (MSVCRT.@)
1162 int CDECL _dup(int od)
1164 int fd, ret;
1165 ioinfo *info = get_ioinfo_alloc(&fd);
1167 if (_dup2(od, fd) == 0)
1168 ret = fd;
1169 else
1170 ret = -1;
1171 release_ioinfo(info);
1172 return ret;
1175 /*********************************************************************
1176 * _eof (MSVCRT.@)
1178 int CDECL _eof(int fd)
1180 ioinfo *info = get_ioinfo(fd);
1181 DWORD curpos,endpos;
1182 LONG hcurpos,hendpos;
1184 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1186 if (info->handle == INVALID_HANDLE_VALUE)
1188 release_ioinfo(info);
1189 return -1;
1192 if (info->wxflag & WX_ATEOF)
1194 release_ioinfo(info);
1195 return TRUE;
1198 /* Otherwise we do it the hard way */
1199 hcurpos = hendpos = 0;
1200 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1201 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1203 if (curpos == endpos && hcurpos == hendpos)
1205 /* FIXME: shouldn't WX_ATEOF be set here? */
1206 release_ioinfo(info);
1207 return TRUE;
1210 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1211 release_ioinfo(info);
1212 return FALSE;
1215 /*********************************************************************
1216 * _fcloseall (MSVCRT.@)
1218 int CDECL _fcloseall(void)
1220 int num_closed = 0, i;
1221 FILE *file;
1223 LOCK_FILES();
1224 for (i = 3; i < MSVCRT_stream_idx; i++) {
1225 file = msvcrt_get_file(i);
1227 if (file->_flag && !fclose(file))
1228 num_closed++;
1230 UNLOCK_FILES();
1232 TRACE(":closed (%d) handles\n",num_closed);
1233 return num_closed;
1236 /* free everything on process exit */
1237 void msvcrt_free_io(void)
1239 unsigned int i;
1240 int j;
1242 _flushall();
1243 _fcloseall();
1245 for(i=0; i<ARRAY_SIZE(MSVCRT___pioinfo); i++)
1247 if(!MSVCRT___pioinfo[i])
1248 continue;
1250 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1252 if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
1253 DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
1255 free(MSVCRT___pioinfo[i]);
1258 for(j=0; j<MSVCRT_stream_idx; j++)
1260 FILE *file = msvcrt_get_file(j);
1261 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1263 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1264 DeleteCriticalSection(&((file_crit*)file)->crit);
1268 for(i=0; i<ARRAY_SIZE(MSVCRT_fstream); i++)
1269 free(MSVCRT_fstream[i]);
1272 /*********************************************************************
1273 * _lseeki64 (MSVCRT.@)
1275 __int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
1277 ioinfo *info = get_ioinfo(fd);
1278 LARGE_INTEGER ofs;
1280 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1282 if (info->handle == INVALID_HANDLE_VALUE)
1284 *_errno() = EBADF;
1285 release_ioinfo(info);
1286 return -1;
1289 if (whence < 0 || whence > 2)
1291 release_ioinfo(info);
1292 *_errno() = EINVAL;
1293 return -1;
1296 TRACE(":fd (%d) to %s pos %s\n",
1297 fd,wine_dbgstr_longlong(offset),
1298 (whence==SEEK_SET)?"SEEK_SET":
1299 (whence==SEEK_CUR)?"SEEK_CUR":
1300 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1302 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1303 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1304 ofs.QuadPart = offset;
1305 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1306 GetLastError() == ERROR_SUCCESS)
1308 info->wxflag &= ~WX_ATEOF;
1309 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1311 release_ioinfo(info);
1312 return ofs.QuadPart;
1314 release_ioinfo(info);
1315 TRACE(":error-last error (%d)\n",GetLastError());
1316 msvcrt_set_errno(GetLastError());
1317 return -1;
1320 /*********************************************************************
1321 * _lseek (MSVCRT.@)
1323 __msvcrt_long CDECL _lseek(int fd, __msvcrt_long offset, int whence)
1325 return _lseeki64(fd, offset, whence);
1328 /*********************************************************************
1329 * _lock_file (MSVCRT.@)
1331 void CDECL _lock_file(FILE *file)
1333 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1334 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1335 else
1336 EnterCriticalSection(&((file_crit*)file)->crit);
1339 /*********************************************************************
1340 * _unlock_file (MSVCRT.@)
1342 void CDECL _unlock_file(FILE *file)
1344 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1345 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1346 else
1347 LeaveCriticalSection(&((file_crit*)file)->crit);
1350 /*********************************************************************
1351 * _locking (MSVCRT.@)
1353 * This is untested; the underlying LockFile doesn't work yet.
1355 int CDECL _locking(int fd, int mode, __msvcrt_long nbytes)
1357 ioinfo *info = get_ioinfo(fd);
1358 BOOL ret;
1359 DWORD cur_locn;
1361 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1362 if (info->handle == INVALID_HANDLE_VALUE)
1364 release_ioinfo(info);
1365 return -1;
1368 if (mode < 0 || mode > 4)
1370 release_ioinfo(info);
1371 *_errno() = EINVAL;
1372 return -1;
1375 TRACE(":fd (%d) by 0x%08Ix mode %s\n",
1376 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
1377 (mode==_LK_LOCK)?"_LK_LOCK":
1378 (mode==_LK_NBLCK)?"_LK_NBLCK":
1379 (mode==_LK_RLCK)?"_LK_RLCK":
1380 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
1381 "UNKNOWN");
1383 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1385 release_ioinfo(info);
1386 FIXME ("Seek failed\n");
1387 *_errno() = EINVAL; /* FIXME */
1388 return -1;
1390 if (mode == _LK_LOCK || mode == _LK_RLCK)
1392 int nretry = 10;
1393 ret = 1; /* just to satisfy gcc */
1394 while (nretry--)
1396 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1397 if (ret) break;
1398 Sleep(1);
1401 else if (mode == _LK_UNLCK)
1402 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1403 else
1404 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1405 /* FIXME - what about error settings? */
1406 release_ioinfo(info);
1407 return ret ? 0 : -1;
1410 /*********************************************************************
1411 * _fseeki64 (MSVCRT.@)
1413 int CDECL _fseeki64(FILE* file, __int64 offset, int whence)
1415 int ret;
1417 _lock_file(file);
1418 ret = _fseeki64_nolock(file, offset, whence);
1419 _unlock_file(file);
1421 return ret;
1424 /*********************************************************************
1425 * _fseeki64_nolock (MSVCRT.@)
1427 int CDECL _fseeki64_nolock(FILE* file, __int64 offset, int whence)
1429 int ret;
1431 if(whence == SEEK_CUR && file->_flag & _IOREAD ) {
1432 whence = SEEK_SET;
1433 offset += _ftelli64_nolock(file);
1436 /* Flush output if needed */
1437 msvcrt_flush_buffer(file);
1438 /* Reset direction of i/o */
1439 if(file->_flag & _IORW) {
1440 file->_flag &= ~(_IOREAD|_IOWRT);
1442 /* Clear end of file flag */
1443 file->_flag &= ~_IOEOF;
1444 ret = (_lseeki64(file->_file,offset,whence) == -1)?-1:0;
1446 return ret;
1449 /*********************************************************************
1450 * fseek (MSVCRT.@)
1452 int CDECL fseek(FILE* file, __msvcrt_long offset, int whence)
1454 return _fseeki64( file, offset, whence );
1457 /*********************************************************************
1458 * _fseek_nolock (MSVCRT.@)
1460 int CDECL _fseek_nolock(FILE* file, __msvcrt_long offset, int whence)
1462 return _fseeki64_nolock( file, offset, whence );
1465 /*********************************************************************
1466 * _chsize_s (MSVCRT.@)
1468 int CDECL _chsize_s(int fd, __int64 size)
1470 ioinfo *info;
1471 __int64 cur, pos;
1472 BOOL ret = FALSE;
1474 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1476 if (!MSVCRT_CHECK_PMT(size >= 0)) return EINVAL;
1479 info = get_ioinfo(fd);
1480 if (info->handle != INVALID_HANDLE_VALUE)
1482 /* save the current file pointer */
1483 cur = _lseeki64(fd, 0, SEEK_CUR);
1484 if (cur >= 0)
1486 pos = _lseeki64(fd, size, SEEK_SET);
1487 if (pos >= 0)
1489 ret = SetEndOfFile(info->handle);
1490 if (!ret) msvcrt_set_errno(GetLastError());
1493 /* restore the file pointer */
1494 _lseeki64(fd, cur, SEEK_SET);
1498 release_ioinfo(info);
1499 return ret ? 0 : *_errno();
1502 /*********************************************************************
1503 * _chsize (MSVCRT.@)
1505 int CDECL _chsize(int fd, __msvcrt_long size)
1507 /* _chsize_s returns errno on failure but _chsize should return -1 */
1508 return _chsize_s( fd, size ) == 0 ? 0 : -1;
1511 /*********************************************************************
1512 * clearerr (MSVCRT.@)
1514 void CDECL clearerr(FILE* file)
1516 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1518 _lock_file(file);
1519 file->_flag &= ~(_IOERR | _IOEOF);
1520 _unlock_file(file);
1523 /*********************************************************************
1524 * clearerr_s (MSVCRT.@)
1526 int CDECL clearerr_s(FILE* file)
1528 TRACE(":file (%p)\n",file);
1530 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
1532 _lock_file(file);
1533 file->_flag &= ~(_IOERR | _IOEOF);
1534 _unlock_file(file);
1535 return 0;
1538 /*********************************************************************
1539 * rewind (MSVCRT.@)
1541 void CDECL rewind(FILE* file)
1543 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1545 _lock_file(file);
1546 _fseek_nolock(file, 0L, SEEK_SET);
1547 clearerr(file);
1548 _unlock_file(file);
1551 static int msvcrt_get_flags(const wchar_t* mode, int *open_flags, int* stream_flags)
1553 int plus = wcschr(mode, '+') != NULL;
1555 TRACE("%s\n", debugstr_w(mode));
1557 while(*mode == ' ') mode++;
1559 switch(*mode++)
1561 case 'R': case 'r':
1562 *open_flags = plus ? _O_RDWR : _O_RDONLY;
1563 *stream_flags = plus ? _IORW : _IOREAD;
1564 break;
1565 case 'W': case 'w':
1566 *open_flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1567 *stream_flags = plus ? _IORW : _IOWRT;
1568 break;
1569 case 'A': case 'a':
1570 *open_flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1571 *stream_flags = plus ? _IORW : _IOWRT;
1572 break;
1573 default:
1574 MSVCRT_INVALID_PMT(0, EINVAL);
1575 return -1;
1578 *stream_flags |= MSVCRT__commode;
1580 while (*mode && *mode!=',')
1581 switch (*mode++)
1583 case 'B': case 'b':
1584 *open_flags |= _O_BINARY;
1585 *open_flags &= ~_O_TEXT;
1586 break;
1587 case 't':
1588 *open_flags |= _O_TEXT;
1589 *open_flags &= ~_O_BINARY;
1590 break;
1591 case 'D':
1592 *open_flags |= _O_TEMPORARY;
1593 break;
1594 case 'T':
1595 *open_flags |= _O_SHORT_LIVED;
1596 break;
1597 case 'c':
1598 *stream_flags |= MSVCRT__IOCOMMIT;
1599 break;
1600 case 'n':
1601 *stream_flags &= ~MSVCRT__IOCOMMIT;
1602 break;
1603 case 'N':
1604 *open_flags |= _O_NOINHERIT;
1605 break;
1606 case '+':
1607 case ' ':
1608 case 'a':
1609 case 'w':
1610 break;
1611 case 'S':
1612 case 'R':
1613 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1614 break;
1615 default:
1616 ERR("incorrect mode flag: %c\n", mode[-1]);
1617 break;
1620 if(*mode == ',')
1622 mode++;
1623 while(*mode == ' ') mode++;
1624 if(!MSVCRT_CHECK_PMT(!wcsncmp(L"ccs", mode, 3)))
1625 return -1;
1626 mode += 3;
1627 while(*mode == ' ') mode++;
1628 if(!MSVCRT_CHECK_PMT(*mode == '='))
1629 return -1;
1630 mode++;
1631 while(*mode == ' ') mode++;
1633 if(!_wcsnicmp(L"utf-8", mode, 5))
1635 *open_flags |= _O_U8TEXT;
1636 mode += 5;
1638 else if(!_wcsnicmp(L"utf-16le", mode, 8))
1640 *open_flags |= _O_U16TEXT;
1641 mode += 8;
1643 else if(!_wcsnicmp(L"unicode", mode, 7))
1645 *open_flags |= _O_WTEXT;
1646 mode += 7;
1648 else
1650 MSVCRT_INVALID_PMT(0, EINVAL);
1651 return -1;
1654 while(*mode == ' ') mode++;
1657 if(!MSVCRT_CHECK_PMT(*mode == 0))
1658 return -1;
1659 return 0;
1662 /*********************************************************************
1663 * _fdopen (MSVCRT.@)
1665 FILE* CDECL _fdopen(int fd, const char *mode)
1667 FILE *ret;
1668 wchar_t *modeW = NULL;
1670 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1672 ret = _wfdopen(fd, modeW);
1674 free(modeW);
1675 return ret;
1678 /*********************************************************************
1679 * _wfdopen (MSVCRT.@)
1681 FILE* CDECL _wfdopen(int fd, const wchar_t *mode)
1683 int open_flags, stream_flags;
1684 FILE* file;
1686 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1688 LOCK_FILES();
1689 if (!(file = msvcrt_alloc_fp()))
1690 file = NULL;
1691 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1693 file->_flag = 0;
1694 file = NULL;
1696 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1697 UNLOCK_FILES();
1699 return file;
1702 /*********************************************************************
1703 * _filelength (MSVCRT.@)
1705 __msvcrt_long CDECL _filelength(int fd)
1707 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1708 if (curPos != -1)
1710 LONG endPos = _lseek(fd, 0, SEEK_END);
1711 if (endPos != -1)
1713 if (endPos != curPos)
1714 _lseek(fd, curPos, SEEK_SET);
1715 return endPos;
1718 return -1;
1721 /*********************************************************************
1722 * _filelengthi64 (MSVCRT.@)
1724 __int64 CDECL _filelengthi64(int fd)
1726 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1727 if (curPos != -1)
1729 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1730 if (endPos != -1)
1732 if (endPos != curPos)
1733 _lseeki64(fd, curPos, SEEK_SET);
1734 return endPos;
1737 return -1;
1740 /*********************************************************************
1741 * _fileno (MSVCRT.@)
1743 int CDECL _fileno(FILE* file)
1745 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1746 return file->_file;
1749 /*********************************************************************
1750 * _fstat64 (MSVCRT.@)
1752 int CDECL _fstat64(int fd, struct _stat64* buf)
1754 ioinfo *info = get_ioinfo(fd);
1755 DWORD dw;
1756 DWORD type;
1757 BY_HANDLE_FILE_INFORMATION hfi;
1759 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1760 if (info->handle == INVALID_HANDLE_VALUE)
1762 release_ioinfo(info);
1763 return -1;
1766 if (!buf)
1768 WARN(":failed-NULL buf\n");
1769 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1770 release_ioinfo(info);
1771 return -1;
1774 memset(&hfi, 0, sizeof(hfi));
1775 memset(buf, 0, sizeof(struct _stat64));
1776 type = GetFileType(info->handle);
1777 if (type == FILE_TYPE_PIPE)
1779 buf->st_dev = buf->st_rdev = fd;
1780 buf->st_mode = _S_IFIFO;
1781 buf->st_nlink = 1;
1783 else if (type == FILE_TYPE_CHAR)
1785 buf->st_dev = buf->st_rdev = fd;
1786 buf->st_mode = _S_IFCHR;
1787 buf->st_nlink = 1;
1789 else /* FILE_TYPE_DISK etc. */
1791 if (!GetFileInformationByHandle(info->handle, &hfi))
1793 WARN(":failed-last error (%d)\n",GetLastError());
1794 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1795 release_ioinfo(info);
1796 return -1;
1798 buf->st_mode = _S_IFREG | 0444;
1799 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1800 buf->st_mode |= 0222;
1801 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1802 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1803 buf->st_atime = dw;
1804 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1805 buf->st_mtime = buf->st_ctime = dw;
1806 buf->st_nlink = hfi.nNumberOfLinks;
1808 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1809 buf->st_mode);
1810 release_ioinfo(info);
1811 return 0;
1814 /*********************************************************************
1815 * _fstati64 (MSVCRT.@)
1817 int CDECL _fstati64(int fd, struct _stati64* buf)
1819 int ret;
1820 struct _stat64 buf64;
1822 ret = _fstat64(fd, &buf64);
1823 if (!ret)
1824 msvcrt_stat64_to_stati64(&buf64, buf);
1825 return ret;
1828 /*********************************************************************
1829 * _fstat (MSVCRT.@)
1831 int CDECL _fstat(int fd, struct _stat* buf)
1832 { int ret;
1833 struct _stat64 buf64;
1835 ret = _fstat64(fd, &buf64);
1836 if (!ret)
1837 msvcrt_stat64_to_stat(&buf64, buf);
1838 return ret;
1841 /*********************************************************************
1842 * _fstat32 (MSVCR80.@)
1844 int CDECL _fstat32(int fd, struct _stat32* buf)
1846 int ret;
1847 struct _stat64 buf64;
1849 ret = _fstat64(fd, &buf64);
1850 if (!ret)
1851 msvcrt_stat64_to_stat32(&buf64, buf);
1852 return ret;
1855 /*********************************************************************
1856 * _fstat32i64 (MSVCR80.@)
1858 int CDECL _fstat32i64(int fd, struct _stat32i64* buf)
1860 int ret;
1861 struct _stat64 buf64;
1863 ret = _fstat64(fd, &buf64);
1864 if (!ret)
1865 msvcrt_stat64_to_stat32i64(&buf64, buf);
1866 return ret;
1869 /*********************************************************************
1870 * _fstat64i32 (MSVCR80.@)
1872 int CDECL _fstat64i32(int fd, struct _stat64i32* buf)
1874 int ret;
1875 struct _stat64 buf64;
1877 ret = _fstat64(fd, &buf64);
1878 if (!ret)
1879 msvcrt_stat64_to_stat64i32(&buf64, buf);
1880 return ret;
1883 /*********************************************************************
1884 * _futime64 (MSVCRT.@)
1886 int CDECL _futime64(int fd, struct __utimbuf64 *t)
1888 ioinfo *info = get_ioinfo(fd);
1889 FILETIME at, wt;
1891 if (!t)
1893 time_to_filetime( _time64(NULL), &at );
1894 wt = at;
1896 else
1898 time_to_filetime( t->actime, &at );
1899 time_to_filetime( t->modtime, &wt );
1902 if (!SetFileTime(info->handle, NULL, &at, &wt))
1904 release_ioinfo(info);
1905 msvcrt_set_errno(GetLastError());
1906 return -1 ;
1908 release_ioinfo(info);
1909 return 0;
1912 /*********************************************************************
1913 * _futime32 (MSVCRT.@)
1915 int CDECL _futime32(int fd, struct __utimbuf32 *t)
1917 if (t)
1919 struct __utimbuf64 t64;
1920 t64.actime = t->actime;
1921 t64.modtime = t->modtime;
1922 return _futime64( fd, &t64 );
1924 else
1925 return _futime64( fd, NULL );
1928 /*********************************************************************
1929 * _get_osfhandle (MSVCRT.@)
1931 intptr_t CDECL _get_osfhandle(int fd)
1933 HANDLE hand = get_ioinfo_nolock(fd)->handle;
1934 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1936 if(hand == INVALID_HANDLE_VALUE)
1937 *_errno() = EBADF;
1938 return (intptr_t)hand;
1941 /*********************************************************************
1942 * _mktemp_s (MSVCRT.@)
1944 int CDECL _mktemp_s(char *pattern, size_t size)
1946 DWORD len, xno, id;
1948 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1949 return EINVAL;
1951 for(len=0; len<size; len++)
1952 if(!pattern[len])
1953 break;
1954 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1955 if(size)
1956 pattern[0] = 0;
1957 return EINVAL;
1960 for(xno=1; xno<=6; xno++)
1961 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1962 return EINVAL;
1964 id = GetCurrentProcessId();
1965 for(xno=1; xno<6; xno++) {
1966 pattern[len-xno] = id%10 + '0';
1967 id /= 10;
1970 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1971 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1972 return 0;
1975 pattern[0] = 0;
1976 *_errno() = EEXIST;
1977 return EEXIST;
1980 /*********************************************************************
1981 * _mktemp (MSVCRT.@)
1983 char * CDECL _mktemp(char *pattern)
1985 int numX = 0;
1986 char *retVal = pattern;
1987 int id;
1988 char letter = 'a';
1990 if(!pattern)
1991 return NULL;
1993 while(*pattern)
1994 numX = (*pattern++ == 'X')? numX + 1 : 0;
1995 if (numX < 6)
1996 return NULL;
1997 pattern--;
1998 id = GetCurrentProcessId();
1999 numX = 6;
2000 while(numX--)
2002 int tempNum = id / 10;
2003 *pattern-- = id - (tempNum * 10) + '0';
2004 id = tempNum;
2006 pattern++;
2009 *pattern = letter++;
2010 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
2011 return retVal;
2012 } while(letter <= 'z');
2013 return NULL;
2016 /*********************************************************************
2017 * _wmktemp_s (MSVCRT.@)
2019 int CDECL _wmktemp_s(wchar_t *pattern, size_t size)
2021 DWORD len, xno, id;
2023 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
2024 return EINVAL;
2026 for(len=0; len<size; len++)
2027 if(!pattern[len])
2028 break;
2029 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
2030 if(size)
2031 pattern[0] = 0;
2032 return EINVAL;
2035 for(xno=1; xno<=6; xno++)
2036 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
2037 return EINVAL;
2039 id = GetCurrentProcessId();
2040 for(xno=1; xno<6; xno++) {
2041 pattern[len-xno] = id%10 + '0';
2042 id /= 10;
2045 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
2046 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
2047 return 0;
2050 pattern[0] = 0;
2051 *_errno() = EEXIST;
2052 return EEXIST;
2055 /*********************************************************************
2056 * _wmktemp (MSVCRT.@)
2058 wchar_t * CDECL _wmktemp(wchar_t *pattern)
2060 int numX = 0;
2061 wchar_t *retVal = pattern;
2062 int id;
2063 wchar_t letter = 'a';
2065 if(!pattern)
2066 return NULL;
2068 while(*pattern)
2069 numX = (*pattern++ == 'X')? numX + 1 : 0;
2070 if (numX < 6)
2071 return NULL;
2072 pattern--;
2073 id = GetCurrentProcessId();
2074 numX = 6;
2075 while(numX--)
2077 int tempNum = id / 10;
2078 *pattern-- = id - (tempNum * 10) + '0';
2079 id = tempNum;
2081 pattern++;
2084 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
2085 return retVal;
2086 *pattern = letter++;
2087 } while(letter != '|');
2088 return NULL;
2091 static unsigned split_oflags(unsigned oflags)
2093 int wxflags = 0;
2094 unsigned unsupp; /* until we support everything */
2096 if (oflags & _O_APPEND) wxflags |= WX_APPEND;
2097 if (oflags & _O_BINARY) {/* Nothing to do */}
2098 else if (oflags & _O_TEXT) wxflags |= WX_TEXT;
2099 else if (oflags & _O_WTEXT) wxflags |= WX_TEXT;
2100 else if (oflags & _O_U16TEXT) wxflags |= WX_TEXT;
2101 else if (oflags & _O_U8TEXT) wxflags |= WX_TEXT;
2102 else
2104 int fmode;
2105 _get_fmode(&fmode);
2106 if (!(fmode & _O_BINARY)) wxflags |= WX_TEXT; /* default to TEXT*/
2108 if (oflags & _O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2110 if ((unsupp = oflags & ~(_O_BINARY | _O_TEXT | _O_APPEND | _O_TRUNC | _O_EXCL | _O_CREAT |
2111 _O_RDWR | _O_WRONLY | _O_TEMPORARY | _O_NOINHERIT | _O_SEQUENTIAL |
2112 _O_RANDOM | _O_SHORT_LIVED | _O_WTEXT | _O_U16TEXT | _O_U8TEXT)))
2113 ERR(":unsupported oflags 0x%04x\n",unsupp);
2115 return wxflags;
2118 /*********************************************************************
2119 * _pipe (MSVCRT.@)
2121 int CDECL _pipe(int *pfds, unsigned int psize, int textmode)
2123 int ret = -1;
2124 SECURITY_ATTRIBUTES sa;
2125 HANDLE readHandle, writeHandle;
2127 if (!pfds)
2129 *_errno() = EINVAL;
2130 return -1;
2133 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2134 sa.bInheritHandle = !(textmode & _O_NOINHERIT);
2135 sa.lpSecurityDescriptor = NULL;
2136 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2138 unsigned int wxflags = split_oflags(textmode);
2139 int fd;
2141 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2142 if (fd != -1)
2144 pfds[0] = fd;
2145 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2146 if (fd != -1)
2148 pfds[1] = fd;
2149 ret = 0;
2151 else
2153 _close(pfds[0]);
2154 CloseHandle(writeHandle);
2155 *_errno() = EMFILE;
2158 else
2160 CloseHandle(readHandle);
2161 CloseHandle(writeHandle);
2162 *_errno() = EMFILE;
2165 else
2166 msvcrt_set_errno(GetLastError());
2168 return ret;
2171 static int check_bom(HANDLE h, int oflags, BOOL seek)
2173 char bom[sizeof(utf8_bom)];
2174 DWORD r;
2176 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2177 return oflags;
2179 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2180 oflags = (oflags & ~(_O_WTEXT | _O_U16TEXT)) | _O_U8TEXT;
2181 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2182 if (seek && r>2)
2183 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2184 oflags = (oflags & ~(_O_WTEXT | _O_U8TEXT)) | _O_U16TEXT;
2185 }else if (seek) {
2186 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2189 return oflags;
2192 /*********************************************************************
2193 * _wsopen_dispatch (UCRTBASE.@)
2195 int CDECL _wsopen_dispatch( const wchar_t* path, int oflags, int shflags, int pmode,
2196 int *fd, int secure )
2198 DWORD access = 0, creation = 0, attrib;
2199 SECURITY_ATTRIBUTES sa;
2200 DWORD sharing, type;
2201 int wxflag;
2202 HANDLE hand;
2204 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2205 debugstr_w(path), oflags, shflags, pmode, fd, secure);
2207 if (!MSVCRT_CHECK_PMT( fd != NULL )) return EINVAL;
2209 *fd = -1;
2210 wxflag = split_oflags(oflags);
2211 switch (oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
2213 case _O_RDONLY: access |= GENERIC_READ; break;
2214 case _O_WRONLY: access |= GENERIC_WRITE; break;
2215 case _O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2218 if (oflags & _O_CREAT)
2220 if (secure && !MSVCRT_CHECK_PMT(!(pmode & ~(_S_IREAD | _S_IWRITE))))
2221 return EINVAL;
2223 if (oflags & _O_EXCL)
2224 creation = CREATE_NEW;
2225 else if (oflags & _O_TRUNC)
2226 creation = CREATE_ALWAYS;
2227 else
2228 creation = OPEN_ALWAYS;
2230 else /* no _O_CREAT */
2232 if (oflags & _O_TRUNC)
2233 creation = TRUNCATE_EXISTING;
2234 else
2235 creation = OPEN_EXISTING;
2238 switch( shflags )
2240 case _SH_DENYRW:
2241 sharing = 0L;
2242 break;
2243 case _SH_DENYWR:
2244 sharing = FILE_SHARE_READ;
2245 break;
2246 case _SH_DENYRD:
2247 sharing = FILE_SHARE_WRITE;
2248 break;
2249 case _SH_DENYNO:
2250 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2251 break;
2252 default:
2253 ERR( "Unhandled shflags 0x%x\n", shflags );
2254 return EINVAL;
2257 if (!(pmode & ~MSVCRT_umask & _S_IWRITE))
2258 attrib = FILE_ATTRIBUTE_READONLY;
2259 else
2260 attrib = FILE_ATTRIBUTE_NORMAL;
2262 if (oflags & _O_TEMPORARY)
2264 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2265 access |= DELETE;
2266 sharing |= FILE_SHARE_DELETE;
2269 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2270 sa.lpSecurityDescriptor = NULL;
2271 sa.bInheritHandle = !(oflags & _O_NOINHERIT);
2273 if ((oflags & (_O_WTEXT | _O_U16TEXT | _O_U8TEXT))
2274 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2275 && !(access&GENERIC_READ))
2277 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2278 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2279 if (hand != INVALID_HANDLE_VALUE)
2281 oflags = check_bom(hand, oflags, FALSE);
2282 CloseHandle(hand);
2286 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2287 if (hand == INVALID_HANDLE_VALUE) {
2288 WARN(":failed-last error (%d)\n",GetLastError());
2289 msvcrt_set_errno(GetLastError());
2290 return *_errno();
2293 if (oflags & (_O_WTEXT | _O_U16TEXT | _O_U8TEXT))
2295 LARGE_INTEGER size = {{0}};
2297 if ((access & GENERIC_WRITE) && (creation==OPEN_EXISTING || creation==OPEN_ALWAYS))
2298 GetFileSizeEx(hand, &size);
2300 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2301 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2302 || ((creation==OPEN_EXISTING || creation==OPEN_ALWAYS) && !size.QuadPart)))
2304 if (oflags & _O_U8TEXT)
2306 DWORD written = 0, tmp;
2308 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2309 sizeof(utf8_bom)-written, &tmp, NULL))
2310 written += tmp;
2311 if (written != sizeof(utf8_bom)) {
2312 WARN("error writing BOM\n");
2313 CloseHandle(hand);
2314 msvcrt_set_errno(GetLastError());
2315 return *_errno();
2318 else
2320 DWORD written = 0, tmp;
2322 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2323 sizeof(utf16_bom)-written, &tmp, NULL))
2324 written += tmp;
2325 if (written != sizeof(utf16_bom))
2327 WARN("error writing BOM\n");
2328 CloseHandle(hand);
2329 msvcrt_set_errno(GetLastError());
2330 return *_errno();
2332 oflags |= _O_U16TEXT;
2335 else if (access & GENERIC_READ)
2336 oflags = check_bom(hand, oflags, TRUE);
2339 type = GetFileType(hand);
2340 if (type == FILE_TYPE_CHAR)
2341 wxflag |= WX_TTY;
2342 else if (type == FILE_TYPE_PIPE)
2343 wxflag |= WX_PIPE;
2345 *fd = msvcrt_alloc_fd(hand, wxflag);
2346 if (*fd == -1)
2347 return *_errno();
2349 if (oflags & _O_WTEXT)
2350 get_ioinfo_nolock(*fd)->exflag |= EF_UNK_UNICODE;
2352 if (oflags & _O_U16TEXT)
2353 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
2354 else if (oflags & _O_U8TEXT)
2355 get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
2357 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2358 return 0;
2362 /*********************************************************************
2363 * _wsopen_s (MSVCRT.@)
2365 int CDECL _wsopen_s( int *fd, const wchar_t* path, int oflags, int shflags, int pmode )
2367 return _wsopen_dispatch( path, oflags, shflags, pmode, fd, 1 );
2370 /*********************************************************************
2371 * _wsopen (MSVCRT.@)
2373 int WINAPIV _wsopen( const wchar_t *path, int oflags, int shflags, ... )
2375 int pmode;
2376 int fd;
2378 if (oflags & _O_CREAT)
2380 __ms_va_list ap;
2382 __ms_va_start(ap, shflags);
2383 pmode = va_arg(ap, int);
2384 __ms_va_end(ap);
2386 else
2387 pmode = 0;
2389 return _wsopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2393 /*********************************************************************
2394 * _sopen_dispatch (UCRTBASE.@)
2396 int CDECL _sopen_dispatch( const char *path, int oflags, int shflags,
2397 int pmode, int *fd, int secure)
2399 wchar_t *pathW;
2400 int ret;
2402 if (!MSVCRT_CHECK_PMT(fd != NULL))
2403 return EINVAL;
2404 *fd = -1;
2405 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2406 return EINVAL;
2408 ret = _wsopen_dispatch(pathW, oflags, shflags, pmode, fd, secure);
2409 free(pathW);
2410 return ret;
2413 /*********************************************************************
2414 * _sopen_s (MSVCRT.@)
2416 int CDECL _sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2418 return _sopen_dispatch(path, oflags, shflags, pmode, fd, 1);
2421 /*********************************************************************
2422 * _sopen (MSVCRT.@)
2424 int WINAPIV _sopen( const char *path, int oflags, int shflags, ... )
2426 int pmode;
2427 int fd;
2429 if (oflags & _O_CREAT)
2431 __ms_va_list ap;
2433 __ms_va_start(ap, shflags);
2434 pmode = va_arg(ap, int);
2435 __ms_va_end(ap);
2437 else
2438 pmode = 0;
2440 return _sopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2443 /*********************************************************************
2444 * _open (MSVCRT.@)
2446 int WINAPIV _open( const char *path, int flags, ... )
2448 __ms_va_list ap;
2450 if (flags & _O_CREAT)
2452 int pmode;
2453 __ms_va_start(ap, flags);
2454 pmode = va_arg(ap, int);
2455 __ms_va_end(ap);
2456 return _sopen( path, flags, _SH_DENYNO, pmode );
2458 else
2459 return _sopen( path, flags, _SH_DENYNO);
2462 /*********************************************************************
2463 * _wopen (MSVCRT.@)
2465 int WINAPIV _wopen(const wchar_t *path,int flags,...)
2467 __ms_va_list ap;
2469 if (flags & _O_CREAT)
2471 int pmode;
2472 __ms_va_start(ap, flags);
2473 pmode = va_arg(ap, int);
2474 __ms_va_end(ap);
2475 return _wsopen( path, flags, _SH_DENYNO, pmode );
2477 else
2478 return _wsopen( path, flags, _SH_DENYNO);
2481 /*********************************************************************
2482 * _creat (MSVCRT.@)
2484 int CDECL _creat(const char *path, int pmode)
2486 int flags = _O_CREAT | _O_TRUNC | _O_RDWR;
2487 return _open(path, flags, pmode);
2490 /*********************************************************************
2491 * _wcreat (MSVCRT.@)
2493 int CDECL _wcreat(const wchar_t *path, int pmode)
2495 int flags = _O_CREAT | _O_TRUNC | _O_RDWR;
2496 return _wopen(path, flags, pmode);
2499 /*********************************************************************
2500 * _open_osfhandle (MSVCRT.@)
2502 int CDECL _open_osfhandle(intptr_t handle, int oflags)
2504 DWORD flags;
2505 int fd;
2507 /* _O_RDONLY (0) always matches, so set the read flag
2508 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2509 * file, so set the write flag. It also only sets _O_TEXT if it wants
2510 * text - it never sets _O_BINARY.
2512 /* don't let split_oflags() decide the mode if no mode is passed */
2513 if (!(oflags & (_O_BINARY | _O_TEXT)))
2514 oflags |= _O_BINARY;
2516 flags = GetFileType((HANDLE)handle);
2517 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2519 msvcrt_set_errno(GetLastError());
2520 return -1;
2523 if (flags == FILE_TYPE_CHAR)
2524 flags = WX_TTY;
2525 else if (flags == FILE_TYPE_PIPE)
2526 flags = WX_PIPE;
2527 else
2528 flags = 0;
2529 flags |= split_oflags(oflags);
2531 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2532 TRACE(":handle (%Iu) fd (%d) flags 0x%08x\n", handle, fd, flags);
2533 return fd;
2536 /*********************************************************************
2537 * _rmtmp (MSVCRT.@)
2539 int CDECL _rmtmp(void)
2541 int num_removed = 0, i;
2542 FILE *file;
2544 LOCK_FILES();
2545 for (i = 3; i < MSVCRT_stream_idx; i++) {
2546 file = msvcrt_get_file(i);
2548 if (file->_tmpfname)
2550 fclose(file);
2551 num_removed++;
2554 UNLOCK_FILES();
2556 if (num_removed)
2557 TRACE(":removed (%d) temp files\n",num_removed);
2558 return num_removed;
2561 static inline int get_utf8_char_len(char ch)
2563 if((ch&0xf8) == 0xf0)
2564 return 4;
2565 else if((ch&0xf0) == 0xe0)
2566 return 3;
2567 else if((ch&0xe0) == 0xc0)
2568 return 2;
2569 return 1;
2572 /*********************************************************************
2573 * (internal) read_utf8
2575 static int read_utf8(ioinfo *fdinfo, wchar_t *buf, unsigned int count)
2577 HANDLE hand = fdinfo->handle;
2578 char min_buf[4], *readbuf, lookahead;
2579 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2581 /* make the buffer big enough to hold at least one character */
2582 /* read bytes have to fit to output and lookahead buffers */
2583 count /= 2;
2584 readbuf_size = count < 4 ? 4 : count;
2585 if(readbuf_size<=4 || !(readbuf = malloc(readbuf_size))) {
2586 readbuf_size = 4;
2587 readbuf = min_buf;
2590 if(fdinfo->lookahead[0] != '\n') {
2591 readbuf[pos++] = fdinfo->lookahead[0];
2592 fdinfo->lookahead[0] = '\n';
2594 if(fdinfo->lookahead[1] != '\n') {
2595 readbuf[pos++] = fdinfo->lookahead[1];
2596 fdinfo->lookahead[1] = '\n';
2598 if(fdinfo->lookahead[2] != '\n') {
2599 readbuf[pos++] = fdinfo->lookahead[2];
2600 fdinfo->lookahead[2] = '\n';
2605 /* NOTE: this case is broken in native dll, reading
2606 * sometimes fails when small buffer is passed
2608 if(count < 4) {
2609 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2610 if (GetLastError() == ERROR_BROKEN_PIPE) {
2611 fdinfo->wxflag |= WX_ATEOF;
2612 return 0;
2613 }else {
2614 msvcrt_set_errno(GetLastError());
2615 return -1;
2617 }else if(!num_read) {
2618 fdinfo->wxflag |= WX_ATEOF;
2619 return 0;
2620 }else {
2621 pos++;
2624 char_len = get_utf8_char_len(readbuf[0]);
2625 if(char_len>pos) {
2626 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2627 pos += num_read;
2630 if(readbuf[0] == '\n')
2631 fdinfo->wxflag |= WX_READNL;
2632 else
2633 fdinfo->wxflag &= ~WX_READNL;
2635 if(readbuf[0] == 0x1a) {
2636 fdinfo->wxflag |= WX_ATEOF;
2637 return 0;
2640 if(readbuf[0] == '\r') {
2641 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2642 buf[0] = '\r';
2643 else if(lookahead == '\n')
2644 buf[0] = '\n';
2645 else {
2646 buf[0] = '\r';
2647 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2648 fdinfo->lookahead[0] = lookahead;
2649 else
2650 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2652 return 2;
2655 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2656 msvcrt_set_errno(GetLastError());
2657 return -1;
2660 return num_read*2;
2663 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2664 if(pos) {
2665 num_read = 0;
2666 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2667 fdinfo->wxflag |= WX_ATEOF;
2668 if (readbuf != min_buf) free(readbuf);
2669 return 0;
2670 }else {
2671 msvcrt_set_errno(GetLastError());
2672 if (readbuf != min_buf) free(readbuf);
2673 return -1;
2675 }else if(!pos && !num_read) {
2676 fdinfo->wxflag |= WX_ATEOF;
2677 if (readbuf != min_buf) free(readbuf);
2678 return 0;
2681 pos += num_read;
2682 if(readbuf[0] == '\n')
2683 fdinfo->wxflag |= WX_READNL;
2684 else
2685 fdinfo->wxflag &= ~WX_READNL;
2687 /* Find first byte of last character (may be incomplete) */
2688 for(i=pos-1; i>0 && i>pos-4; i--)
2689 if((readbuf[i]&0xc0) != 0x80)
2690 break;
2691 char_len = get_utf8_char_len(readbuf[i]);
2692 if(char_len+i <= pos)
2693 i += char_len;
2695 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2696 if(i < pos)
2697 fdinfo->lookahead[0] = readbuf[i];
2698 if(i+1 < pos)
2699 fdinfo->lookahead[1] = readbuf[i+1];
2700 if(i+2 < pos)
2701 fdinfo->lookahead[2] = readbuf[i+2];
2702 }else if(i < pos) {
2703 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2705 pos = i;
2707 for(i=0, j=0; i<pos; i++) {
2708 if(readbuf[i] == 0x1a) {
2709 fdinfo->wxflag |= WX_ATEOF;
2710 break;
2713 /* strip '\r' if followed by '\n' */
2714 if(readbuf[i] == '\r' && i+1==pos) {
2715 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2716 readbuf[j++] = '\r';
2717 }else if(lookahead == '\n' && j==0) {
2718 readbuf[j++] = '\n';
2719 }else {
2720 if(lookahead != '\n')
2721 readbuf[j++] = '\r';
2723 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2724 fdinfo->lookahead[0] = lookahead;
2725 else
2726 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2728 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2729 readbuf[j++] = readbuf[i];
2732 pos = j;
2734 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2735 msvcrt_set_errno(GetLastError());
2736 if (readbuf != min_buf) free(readbuf);
2737 return -1;
2740 if (readbuf != min_buf) free(readbuf);
2741 return num_read*2;
2744 /*********************************************************************
2745 * (internal) read_i
2747 * When reading \r as last character in text mode, read() positions
2748 * the file pointer on the \r character while getc() goes on to
2749 * the following \n
2751 static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
2753 DWORD num_read, utf16;
2754 char *bufstart = buf;
2756 if (count == 0)
2757 return 0;
2759 if (fdinfo->wxflag & WX_ATEOF) {
2760 TRACE("already at EOF, returning 0\n");
2761 return 0;
2763 /* Don't trace small reads, it gets *very* annoying */
2764 if (count > 4)
2765 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2766 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2768 *_errno() = EBADF;
2769 return -1;
2772 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2773 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2775 *_errno() = EINVAL;
2776 return -1;
2779 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2780 return read_utf8(fdinfo, buf, count);
2782 if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
2784 if (fdinfo->lookahead[0] != '\n')
2786 bufstart[0] = fdinfo->lookahead[0];
2787 fdinfo->lookahead[0] = '\n';
2789 if (utf16)
2791 bufstart[1] = fdinfo->lookahead[1];
2792 fdinfo->lookahead[1] = '\n';
2795 if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2796 num_read += 1+utf16;
2797 else
2798 num_read = 1+utf16;
2801 if(utf16 && (num_read&1))
2803 /* msvcr90 uses uninitialized value from the buffer in this case */
2804 /* msvcrt ignores additional data */
2805 ERR("got odd number of bytes in UTF16 mode\n");
2806 num_read--;
2809 if (count != 0 && num_read == 0)
2811 fdinfo->wxflag |= WX_ATEOF;
2812 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2814 else if (fdinfo->wxflag & WX_TEXT)
2816 DWORD i, j;
2818 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2819 fdinfo->wxflag |= WX_READNL;
2820 else
2821 fdinfo->wxflag &= ~WX_READNL;
2823 for (i=0, j=0; i<num_read; i+=1+utf16)
2825 /* in text mode, a ctrl-z signals EOF */
2826 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2828 fdinfo->wxflag |= WX_ATEOF;
2829 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2830 break;
2833 /* in text mode, strip \r if followed by \n */
2834 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2836 char lookahead[2];
2837 DWORD len;
2839 lookahead[1] = '\n';
2840 if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
2842 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2844 bufstart[j++] = '\n';
2845 if(utf16) bufstart[j++] = 0;
2847 else
2849 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2851 bufstart[j++] = '\r';
2852 if(utf16) bufstart[j++] = 0;
2855 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2857 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2859 bufstart[j++] = '\n';
2860 if (utf16) bufstart[j++] = 0;
2862 else
2864 fdinfo->lookahead[0] = lookahead[0];
2865 fdinfo->lookahead[1] = lookahead[1];
2868 else
2869 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2872 else
2874 bufstart[j++] = '\r';
2875 if(utf16) bufstart[j++] = 0;
2878 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2879 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2881 bufstart[j++] = bufstart[i];
2882 if(utf16) bufstart[j++] = bufstart[i+1];
2885 num_read = j;
2888 else
2890 if (GetLastError() == ERROR_BROKEN_PIPE)
2892 TRACE(":end-of-pipe\n");
2893 fdinfo->wxflag |= WX_ATEOF;
2894 return 0;
2896 else
2898 TRACE(":failed-last error (%d)\n",GetLastError());
2899 msvcrt_set_errno(GetLastError());
2900 return -1;
2904 if (count > 4)
2905 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2906 return num_read;
2909 /*********************************************************************
2910 * _read (MSVCRT.@)
2912 int CDECL _read(int fd, void *buf, unsigned int count)
2914 ioinfo *info;
2915 int num_read;
2917 if(fd == MSVCRT_NO_CONSOLE_FD) {
2918 *_errno() = EBADF;
2919 return -1;
2922 info = get_ioinfo(fd);
2923 num_read = read_i(fd, info, buf, count);
2924 release_ioinfo(info);
2925 return num_read;
2928 /*********************************************************************
2929 * _setmode (MSVCRT.@)
2931 int CDECL _setmode(int fd,int mode)
2933 ioinfo *info = get_ioinfo(fd);
2934 int ret = info->wxflag & WX_TEXT ? _O_TEXT : _O_BINARY;
2935 if(ret==_O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16)))
2936 ret = _O_WTEXT;
2938 if(mode!=_O_TEXT && mode!=_O_BINARY && mode!=_O_WTEXT
2939 && mode!=_O_U16TEXT && mode!=_O_U8TEXT) {
2940 *_errno() = EINVAL;
2941 release_ioinfo(info);
2942 return -1;
2945 if(info == &MSVCRT___badioinfo) {
2946 *_errno() = EBADF;
2947 return EOF;
2950 if(mode == _O_BINARY) {
2951 info->wxflag &= ~WX_TEXT;
2952 info->exflag &= ~(EF_UTF8|EF_UTF16);
2953 release_ioinfo(info);
2954 return ret;
2957 info->wxflag |= WX_TEXT;
2958 if(mode == _O_TEXT)
2959 info->exflag &= ~(EF_UTF8|EF_UTF16);
2960 else if(mode == _O_U8TEXT)
2961 info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
2962 else
2963 info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
2965 release_ioinfo(info);
2966 return ret;
2969 /*********************************************************************
2970 * _stat64 (MSVCRT.@)
2972 int CDECL _stat64(const char* path, struct _stat64 * buf)
2974 DWORD dw;
2975 WIN32_FILE_ATTRIBUTE_DATA hfi;
2976 unsigned short mode = ALL_S_IREAD;
2977 int plen;
2979 TRACE(":file (%s) buf(%p)\n",path,buf);
2981 plen = strlen(path);
2982 while (plen && path[plen-1]==' ')
2983 plen--;
2985 if (plen==2 && path[1]==':')
2987 *_errno() = ENOENT;
2988 return -1;
2991 #if _MSVCR_VER<140
2992 if (plen>=2 && path[plen-2]!=':' && (path[plen-1]=='\\' || path[plen-1]=='/'))
2994 *_errno() = ENOENT;
2995 return -1;
2997 #endif
2999 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
3001 TRACE("failed (%d)\n",GetLastError());
3002 *_errno() = ENOENT;
3003 return -1;
3006 memset(buf,0,sizeof(struct _stat64));
3008 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3009 Bon 011120: This FIXME seems incorrect
3010 Also a letter as first char isn't enough to be classified
3011 as a drive letter
3013 if (isalpha(*path)&& (*(path+1)==':'))
3014 buf->st_dev = buf->st_rdev = _toupper_l(*path, NULL) - 'A'; /* drive num */
3015 else
3016 buf->st_dev = buf->st_rdev = _getdrive() - 1;
3018 /* Dir, or regular file? */
3019 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3020 mode |= (_S_IFDIR | ALL_S_IEXEC);
3021 else
3023 mode |= _S_IFREG;
3024 /* executable? */
3025 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3027 unsigned int ext = _tolower_l(path[plen-1], NULL) |
3028 (_tolower_l(path[plen-2], NULL) << 8) |
3029 (_tolower_l(path[plen-3], NULL) << 16);
3030 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
3031 mode |= ALL_S_IEXEC;
3035 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3036 mode |= ALL_S_IWRITE;
3038 buf->st_mode = mode;
3039 buf->st_nlink = 1;
3040 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3041 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3042 buf->st_atime = dw;
3043 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3044 buf->st_mtime = buf->st_ctime = dw;
3045 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3046 (int)(buf->st_size >> 32),(int)buf->st_size,
3047 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3048 return 0;
3051 /*********************************************************************
3052 * _stati64 (MSVCRT.@)
3054 int CDECL _stati64(const char* path, struct _stati64 * buf)
3056 int ret;
3057 struct _stat64 buf64;
3059 ret = _stat64(path, &buf64);
3060 if (!ret)
3061 msvcrt_stat64_to_stati64(&buf64, buf);
3062 return ret;
3065 /*********************************************************************
3066 * _stat (MSVCRT.@)
3068 int CDECL _stat(const char* path, struct _stat * buf)
3070 int ret;
3071 struct _stat64 buf64;
3073 ret = _stat64( path, &buf64);
3074 if (!ret)
3075 msvcrt_stat64_to_stat(&buf64, buf);
3076 return ret;
3079 #if _MSVCR_VER >= 80
3081 /*********************************************************************
3082 * _stat32 (MSVCR80.@)
3084 int CDECL _stat32(const char *path, struct _stat32 *buf)
3086 int ret;
3087 struct _stat64 buf64;
3089 ret = _stat64(path, &buf64);
3090 if (!ret)
3091 msvcrt_stat64_to_stat32(&buf64, buf);
3092 return ret;
3095 /*********************************************************************
3096 * _stat32i64 (MSVCR80.@)
3098 int CDECL _stat32i64(const char *path, struct _stat32i64 *buf)
3100 int ret;
3101 struct _stat64 buf64;
3103 ret = _stat64(path, &buf64);
3104 if (!ret)
3105 msvcrt_stat64_to_stat32i64(&buf64, buf);
3106 return ret;
3109 /*********************************************************************
3110 * _stat64i32 (MSVCR80.@)
3112 int CDECL _stat64i32(const char* path, struct _stat64i32 *buf)
3114 int ret;
3115 struct _stat64 buf64;
3117 ret = _stat64(path, &buf64);
3118 if (!ret)
3119 msvcrt_stat64_to_stat64i32(&buf64, buf);
3120 return ret;
3123 #endif /* _MSVCR_VER >= 80 */
3125 /*********************************************************************
3126 * _wstat64 (MSVCRT.@)
3128 int CDECL _wstat64(const wchar_t* path, struct _stat64 * buf)
3130 DWORD dw;
3131 WIN32_FILE_ATTRIBUTE_DATA hfi;
3132 unsigned short mode = ALL_S_IREAD;
3133 int plen;
3135 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
3137 plen = wcslen(path);
3138 while (plen && path[plen-1]==' ')
3139 plen--;
3141 if (plen==2 && path[1]==':')
3143 *_errno() = ENOENT;
3144 return -1;
3147 #if _MSVCR_VER<140
3148 if (plen>=2 && path[plen-2]!=':' && (path[plen-1]=='\\' || path[plen-1]=='/'))
3150 *_errno() = ENOENT;
3151 return -1;
3153 #endif
3155 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
3157 TRACE("failed (%d)\n",GetLastError());
3158 *_errno() = ENOENT;
3159 return -1;
3162 memset(buf,0,sizeof(struct _stat64));
3164 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3165 if (iswalpha(*path) && path[1] == ':')
3166 buf->st_dev = buf->st_rdev = towupper(*path) - 'A'; /* drive num */
3167 else
3168 buf->st_dev = buf->st_rdev = _getdrive() - 1;
3170 /* Dir, or regular file? */
3171 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3172 mode |= (_S_IFDIR | ALL_S_IEXEC);
3173 else
3175 mode |= _S_IFREG;
3176 /* executable? */
3177 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3179 ULONGLONG ext = towlower(path[plen-1]) | (towlower(path[plen-2]) << 16) |
3180 ((ULONGLONG)towlower(path[plen-3]) << 32);
3181 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3182 mode |= ALL_S_IEXEC;
3186 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3187 mode |= ALL_S_IWRITE;
3189 buf->st_mode = mode;
3190 buf->st_nlink = 1;
3191 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3192 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3193 buf->st_atime = dw;
3194 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3195 buf->st_mtime = buf->st_ctime = dw;
3196 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3197 (int)(buf->st_size >> 32),(int)buf->st_size,
3198 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3199 return 0;
3202 /*********************************************************************
3203 * _wstati64 (MSVCRT.@)
3205 int CDECL _wstati64(const wchar_t* path, struct _stati64 * buf)
3207 int ret;
3208 struct _stat64 buf64;
3210 ret = _wstat64(path, &buf64);
3211 if (!ret)
3212 msvcrt_stat64_to_stati64(&buf64, buf);
3213 return ret;
3216 /*********************************************************************
3217 * _wstat (MSVCRT.@)
3219 int CDECL _wstat(const wchar_t* path, struct _stat * buf)
3221 int ret;
3222 struct _stat64 buf64;
3224 ret = _wstat64( path, &buf64 );
3225 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3226 return ret;
3229 #if _MSVCR_VER >= 80
3231 /*********************************************************************
3232 * _wstat32 (MSVCR80.@)
3234 int CDECL _wstat32(const wchar_t *path, struct _stat32 *buf)
3236 int ret;
3237 struct _stat64 buf64;
3239 ret = _wstat64(path, &buf64);
3240 if (!ret)
3241 msvcrt_stat64_to_stat32(&buf64, buf);
3242 return ret;
3245 /*********************************************************************
3246 * _wstat32i64 (MSVCR80.@)
3248 int CDECL _wstat32i64(const wchar_t *path, struct _stat32i64 *buf)
3250 int ret;
3251 struct _stat64 buf64;
3253 ret = _wstat64(path, &buf64);
3254 if (!ret)
3255 msvcrt_stat64_to_stat32i64(&buf64, buf);
3256 return ret;
3259 /*********************************************************************
3260 * _wstat64i32 (MSVCR80.@)
3262 int CDECL _wstat64i32(const wchar_t *path, struct _stat64i32 *buf)
3264 int ret;
3265 struct _stat64 buf64;
3267 ret = _wstat64(path, &buf64);
3268 if (!ret)
3269 msvcrt_stat64_to_stat64i32(&buf64, buf);
3270 return ret;
3273 #endif /* _MSVCR_VER >= 80 */
3275 /*********************************************************************
3276 * _tell (MSVCRT.@)
3278 __msvcrt_long CDECL _tell(int fd)
3280 return _lseek(fd, 0, SEEK_CUR);
3283 /*********************************************************************
3284 * _telli64 (MSVCRT.@)
3286 __int64 CDECL _telli64(int fd)
3288 return _lseeki64(fd, 0, SEEK_CUR);
3291 /*********************************************************************
3292 * _tempnam (MSVCRT.@)
3294 char * CDECL _tempnam(const char *dir, const char *prefix)
3296 char tmpbuf[MAX_PATH];
3297 const char *tmp_dir = getenv("TMP");
3299 if (tmp_dir) dir = tmp_dir;
3301 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3302 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3304 TRACE("got name (%s)\n",tmpbuf);
3305 DeleteFileA(tmpbuf);
3306 return _strdup(tmpbuf);
3308 TRACE("failed (%d)\n",GetLastError());
3309 return NULL;
3312 /*********************************************************************
3313 * _wtempnam (MSVCRT.@)
3315 wchar_t * CDECL _wtempnam(const wchar_t *dir, const wchar_t *prefix)
3317 wchar_t tmpbuf[MAX_PATH];
3318 const wchar_t *tmp_dir = _wgetenv(L"TMP");
3320 if (tmp_dir) dir = tmp_dir;
3322 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3323 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3325 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3326 DeleteFileW(tmpbuf);
3327 return _wcsdup(tmpbuf);
3329 TRACE("failed (%d)\n",GetLastError());
3330 return NULL;
3333 /*********************************************************************
3334 * _umask (MSVCRT.@)
3336 int CDECL _umask(int umask)
3338 int old_umask = MSVCRT_umask;
3339 TRACE("(%d)\n",umask);
3340 MSVCRT_umask = umask;
3341 return old_umask;
3344 /*********************************************************************
3345 * _utime64 (MSVCRT.@)
3347 int CDECL _utime64(const char* path, struct __utimbuf64 *t)
3349 int fd = _open(path, _O_WRONLY | _O_BINARY);
3351 if (fd > 0)
3353 int retVal = _futime64(fd, t);
3354 _close(fd);
3355 return retVal;
3357 return -1;
3360 /*********************************************************************
3361 * _utime32 (MSVCRT.@)
3363 int CDECL _utime32(const char* path, struct __utimbuf32 *t)
3365 if (t)
3367 struct __utimbuf64 t64;
3368 t64.actime = t->actime;
3369 t64.modtime = t->modtime;
3370 return _utime64( path, &t64 );
3372 else
3373 return _utime64( path, NULL );
3376 /*********************************************************************
3377 * _wutime64 (MSVCRT.@)
3379 int CDECL _wutime64(const wchar_t* path, struct __utimbuf64 *t)
3381 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
3383 if (fd > 0)
3385 int retVal = _futime64(fd, t);
3386 _close(fd);
3387 return retVal;
3389 return -1;
3392 /*********************************************************************
3393 * _wutime32 (MSVCRT.@)
3395 int CDECL _wutime32(const wchar_t* path, struct __utimbuf32 *t)
3397 if (t)
3399 struct __utimbuf64 t64;
3400 t64.actime = t->actime;
3401 t64.modtime = t->modtime;
3402 return _wutime64( path, &t64 );
3404 else
3405 return _wutime64( path, NULL );
3408 /*********************************************************************
3409 * _write (MSVCRT.@)
3411 int CDECL _write(int fd, const void* buf, unsigned int count)
3413 ioinfo *info = get_ioinfo(fd);
3414 HANDLE hand = info->handle;
3415 DWORD num_written, i;
3416 BOOL console = FALSE;
3418 if (hand == INVALID_HANDLE_VALUE || fd == MSVCRT_NO_CONSOLE_FD)
3420 *_errno() = EBADF;
3421 release_ioinfo(info);
3422 return -1;
3425 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3427 *_errno() = EINVAL;
3428 release_ioinfo(info);
3429 return -1;
3432 /* If appending, go to EOF */
3433 if (info->wxflag & WX_APPEND)
3434 _lseek(fd, 0, FILE_END);
3436 if (!(info->wxflag & WX_TEXT))
3438 if (!WriteFile(hand, buf, count, &num_written, NULL)
3439 || num_written != count)
3441 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3442 hand, GetLastError());
3443 msvcrt_set_errno(GetLastError());
3444 num_written = -1;
3447 release_ioinfo(info);
3448 return num_written;
3451 if (_isatty(fd)) console = VerifyConsoleIoHandle(hand);
3452 for (i = 0; i < count;)
3454 const char *s = buf;
3455 char lfbuf[2048];
3456 DWORD j = 0;
3458 if (!(info->exflag & (EF_UTF8|EF_UTF16)) && console)
3460 char conv[sizeof(lfbuf)];
3461 size_t len = 0;
3463 #if _MSVCR_VER >= 90
3464 if (info->dbcsBufferUsed)
3466 conv[j++] = info->dbcsBuffer;
3467 info->dbcsBufferUsed = FALSE;
3468 conv[j++] = s[i++];
3469 len++;
3471 #endif
3473 for (; i < count && j < sizeof(conv)-1 &&
3474 len < (sizeof(lfbuf) - 1) / sizeof(WCHAR); i++, j++, len++)
3476 if (isleadbyte((unsigned char)s[i]))
3478 conv[j++] = s[i++];
3480 if (i == count)
3482 #if _MSVCR_VER >= 90
3483 info->dbcsBuffer = conv[j-1];
3484 info->dbcsBufferUsed = TRUE;
3485 break;
3486 #else
3487 *_errno() = EINVAL;
3488 release_ioinfo(info);
3489 return -1;
3490 #endif
3493 else if (s[i] == '\n')
3495 conv[j++] = '\r';
3496 len++;
3498 conv[j] = s[i];
3501 len = mbstowcs((WCHAR*)lfbuf, conv, len);
3502 if (len == -1)
3504 msvcrt_set_errno(GetLastError());
3505 release_ioinfo(info);
3506 return -1;
3508 j = len * 2;
3510 else if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3512 for (j = 0; i < count && j < sizeof(lfbuf)-1; i++, j++)
3514 if (s[i] == '\n')
3515 lfbuf[j++] = '\r';
3516 lfbuf[j] = s[i];
3519 else if (info->exflag & EF_UTF16 || console)
3521 for (j = 0; i < count && j < sizeof(lfbuf)-3; i++, j++)
3523 if (s[i] == '\n' && !s[i+1])
3525 lfbuf[j++] = '\r';
3526 lfbuf[j++] = 0;
3528 lfbuf[j++] = s[i++];
3529 lfbuf[j] = s[i];
3532 else
3534 char conv[sizeof(lfbuf)/4];
3536 for (j = 0; i < count && j < sizeof(conv)-3; i++, j++)
3538 if (s[i] == '\n' && !s[i+1])
3540 conv[j++] = '\r';
3541 conv[j++] = 0;
3543 conv[j++] = s[i++];
3544 conv[j] = s[i];
3547 j = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)conv, j/2, lfbuf, sizeof(lfbuf), NULL, NULL);
3548 if (!j)
3550 msvcrt_set_errno(GetLastError());
3551 release_ioinfo(info);
3552 return -1;
3556 if (console)
3558 j = j/2;
3559 if (!WriteConsoleW(hand, lfbuf, j, &num_written, NULL))
3560 num_written = -1;
3562 else if (!WriteFile(hand, lfbuf, j, &num_written, NULL))
3564 num_written = -1;
3567 if (num_written != j)
3569 TRACE("WriteFile/WriteConsoleW (fd %d, hand %p) failed-last error (%d)\n", fd,
3570 hand, GetLastError());
3571 msvcrt_set_errno(GetLastError());
3572 release_ioinfo(info);
3573 return -1;
3577 release_ioinfo(info);
3578 return count;
3581 /*********************************************************************
3582 * _putw (MSVCRT.@)
3584 int CDECL _putw(int val, FILE* file)
3586 int len;
3588 _lock_file(file);
3589 len = _write(file->_file, &val, sizeof(val));
3590 if (len == sizeof(val)) {
3591 _unlock_file(file);
3592 return val;
3595 file->_flag |= _IOERR;
3596 _unlock_file(file);
3597 return EOF;
3600 /*********************************************************************
3601 * fclose (MSVCRT.@)
3603 int CDECL fclose(FILE* file)
3605 int ret;
3607 _lock_file(file);
3608 ret = _fclose_nolock(file);
3609 _unlock_file(file);
3611 return ret;
3614 /*********************************************************************
3615 * _fclose_nolock (MSVCRT.@)
3617 int CDECL _fclose_nolock(FILE* file)
3619 int r, flag;
3621 if(!(file->_flag & (_IOREAD | _IOWRT | _IORW)))
3623 file->_flag = 0;
3624 return EOF;
3627 flag = file->_flag;
3628 free(file->_tmpfname);
3629 file->_tmpfname = NULL;
3630 /* flush stdio buffers */
3631 if(file->_flag & _IOWRT)
3632 _fflush_nolock(file);
3633 if(file->_flag & _IOMYBUF)
3634 free(file->_base);
3636 r=_close(file->_file);
3637 file->_flag = 0;
3639 return ((r == -1) || (flag & _IOERR) ? EOF : 0);
3642 /*********************************************************************
3643 * feof (MSVCRT.@)
3645 int CDECL feof(FILE* file)
3647 return file->_flag & _IOEOF;
3650 /*********************************************************************
3651 * ferror (MSVCRT.@)
3653 int CDECL ferror(FILE* file)
3655 return file->_flag & _IOERR;
3658 /*********************************************************************
3659 * _filbuf (MSVCRT.@)
3661 int CDECL _filbuf(FILE* file)
3663 unsigned char c;
3665 if(file->_flag & _IOSTRG)
3666 return EOF;
3668 /* Allocate buffer if needed */
3669 if(!(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF)))
3670 msvcrt_alloc_buffer(file);
3672 if(!(file->_flag & _IOREAD)) {
3673 if(file->_flag & _IORW)
3674 file->_flag |= _IOREAD;
3675 else
3676 return EOF;
3679 if(!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF))) {
3680 int r;
3681 if ((r = _read(file->_file,&c,1)) != 1) {
3682 file->_flag |= (r == 0) ? _IOEOF : _IOERR;
3683 return EOF;
3686 return c;
3687 } else {
3688 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
3689 if(file->_cnt<=0) {
3690 file->_flag |= (file->_cnt == 0) ? _IOEOF : _IOERR;
3691 file->_cnt = 0;
3692 return EOF;
3695 file->_cnt--;
3696 file->_ptr = file->_base+1;
3697 c = *(unsigned char *)file->_base;
3698 return c;
3702 /*********************************************************************
3703 * fgetc (MSVCRT.@)
3705 int CDECL fgetc(FILE* file)
3707 int ret;
3709 _lock_file(file);
3710 ret = _fgetc_nolock(file);
3711 _unlock_file(file);
3713 return ret;
3716 /*********************************************************************
3717 * _fgetc_nolock (MSVCRT.@)
3719 int CDECL _fgetc_nolock(FILE* file)
3721 unsigned char *i;
3722 unsigned int j;
3724 if (file->_cnt>0) {
3725 file->_cnt--;
3726 i = (unsigned char *)file->_ptr++;
3727 j = *i;
3728 } else
3729 j = _filbuf(file);
3731 return j;
3734 /*********************************************************************
3735 * _fgetchar (MSVCRT.@)
3737 int CDECL _fgetchar(void)
3739 return fgetc(MSVCRT_stdin);
3742 /*********************************************************************
3743 * fgets (MSVCRT.@)
3745 char * CDECL fgets(char *s, int size, FILE* file)
3747 int cc = EOF;
3748 char * buf_start = s;
3750 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3751 file,file->_file,s,size);
3753 _lock_file(file);
3755 while ((size >1) && (cc = _fgetc_nolock(file)) != EOF && cc != '\n')
3757 *s++ = (char)cc;
3758 size --;
3760 if ((cc == EOF) && (s == buf_start)) /* If nothing read, return 0*/
3762 TRACE(":nothing read\n");
3763 _unlock_file(file);
3764 return NULL;
3766 if ((cc != EOF) && (size > 1))
3767 *s++ = cc;
3768 *s = '\0';
3769 TRACE(":got %s\n", debugstr_a(buf_start));
3770 _unlock_file(file);
3771 return buf_start;
3774 /*********************************************************************
3775 * fgetwc (MSVCRT.@)
3777 wint_t CDECL fgetwc(FILE* file)
3779 wint_t ret;
3781 _lock_file(file);
3782 ret = _fgetwc_nolock(file);
3783 _unlock_file(file);
3785 return ret;
3788 /*********************************************************************
3789 * _fgetwc_nolock (MSVCRT.@)
3791 wint_t CDECL _fgetwc_nolock(FILE* file)
3793 wint_t ret;
3794 int ch;
3796 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3797 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3798 char *p;
3800 for(p=(char*)&ret; (wint_t*)p<&ret+1; p++) {
3801 ch = _fgetc_nolock(file);
3802 if(ch == EOF) {
3803 ret = WEOF;
3804 break;
3806 *p = (char)ch;
3808 }else {
3809 char mbs[MB_LEN_MAX];
3810 int len = 0;
3812 ch = _fgetc_nolock(file);
3813 if(ch != EOF) {
3814 mbs[0] = (char)ch;
3815 if(isleadbyte((unsigned char)mbs[0])) {
3816 ch = _fgetc_nolock(file);
3817 if(ch != EOF) {
3818 mbs[1] = (char)ch;
3819 len = 2;
3821 }else {
3822 len = 1;
3826 if(!len || mbtowc(&ret, mbs, len)==-1)
3827 ret = WEOF;
3830 return ret;
3833 /*********************************************************************
3834 * _getw (MSVCRT.@)
3836 int CDECL _getw(FILE* file)
3838 char *ch;
3839 int i, k;
3840 unsigned int j;
3841 ch = (char *)&i;
3843 _lock_file(file);
3844 for (j=0; j<sizeof(int); j++) {
3845 k = _fgetc_nolock(file);
3846 if (k == EOF) {
3847 file->_flag |= _IOEOF;
3848 _unlock_file(file);
3849 return EOF;
3851 ch[j] = k;
3854 _unlock_file(file);
3855 return i;
3858 /*********************************************************************
3859 * getwc (MSVCRT.@)
3861 wint_t CDECL getwc(FILE* file)
3863 return fgetwc(file);
3866 /*********************************************************************
3867 * _fgetwchar (MSVCRT.@)
3869 wint_t CDECL _fgetwchar(void)
3871 return fgetwc(MSVCRT_stdin);
3874 /*********************************************************************
3875 * getwchar (MSVCRT.@)
3877 wint_t CDECL getwchar(void)
3879 return _fgetwchar();
3882 /*********************************************************************
3883 * fgetws (MSVCRT.@)
3885 wchar_t * CDECL fgetws(wchar_t *s, int size, FILE* file)
3887 wint_t cc = WEOF;
3888 wchar_t * buf_start = s;
3890 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3891 file,file->_file,s,size);
3893 _lock_file(file);
3895 while ((size >1) && (cc = _fgetwc_nolock(file)) != WEOF && cc != '\n')
3897 *s++ = cc;
3898 size --;
3900 if ((cc == WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3902 TRACE(":nothing read\n");
3903 _unlock_file(file);
3904 return NULL;
3906 if ((cc != WEOF) && (size > 1))
3907 *s++ = cc;
3908 *s = 0;
3909 TRACE(":got %s\n", debugstr_w(buf_start));
3910 _unlock_file(file);
3911 return buf_start;
3914 /*********************************************************************
3915 * _flsbuf (MSVCRT.@)
3917 int CDECL _flsbuf(int c, FILE* file)
3919 /* Flush output buffer */
3920 if(!(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF))) {
3921 msvcrt_alloc_buffer(file);
3924 if(!(file->_flag & _IOWRT)) {
3925 if(!(file->_flag & _IORW)) {
3926 file->_flag |= _IOERR;
3927 *_errno() = EBADF;
3928 return EOF;
3930 file->_flag |= _IOWRT;
3932 if(file->_flag & _IOREAD) {
3933 if(!(file->_flag & _IOEOF)) {
3934 file->_flag |= _IOERR;
3935 return EOF;
3937 file->_cnt = 0;
3938 file->_ptr = file->_base;
3939 file->_flag &= ~(_IOREAD | _IOEOF);
3942 if(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) {
3943 int res = 0;
3945 if(file->_cnt <= 0) {
3946 res = msvcrt_flush_buffer(file);
3947 if(res)
3948 return res;
3949 file->_flag |= _IOWRT;
3950 file->_cnt=file->_bufsiz;
3952 *file->_ptr++ = c;
3953 file->_cnt--;
3954 return c&0xff;
3955 } else {
3956 unsigned char cc=c;
3957 int len;
3958 /* set _cnt to 0 for unbuffered FILEs */
3959 file->_cnt = 0;
3960 len = _write(file->_file, &cc, 1);
3961 if (len == 1)
3962 return c & 0xff;
3963 file->_flag |= _IOERR;
3964 return EOF;
3968 /*********************************************************************
3969 * fwrite (MSVCRT.@)
3971 size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE* file)
3973 size_t ret;
3975 _lock_file(file);
3976 ret = _fwrite_nolock(ptr, size, nmemb, file);
3977 _unlock_file(file);
3979 return ret;
3982 /*********************************************************************
3983 * _fwrite_nolock (MSVCRT.@)
3985 size_t CDECL _fwrite_nolock(const void *ptr, size_t size, size_t nmemb, FILE* file)
3987 size_t wrcnt=size * nmemb;
3988 int written = 0;
3989 if (size == 0)
3990 return 0;
3992 while(wrcnt) {
3993 if(file->_cnt < 0) {
3994 WARN("negative file->_cnt value in %p\n", file);
3995 file->_flag |= _IOERR;
3996 break;
3997 } else if(file->_cnt) {
3998 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3999 memcpy(file->_ptr, ptr, pcnt);
4000 file->_cnt -= pcnt;
4001 file->_ptr += pcnt;
4002 written += pcnt;
4003 wrcnt -= pcnt;
4004 ptr = (const char*)ptr + pcnt;
4005 } else if((file->_flag & _IONBF)
4006 || ((file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
4007 || (!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
4008 size_t pcnt;
4009 int bufsiz;
4011 if(file->_flag & _IONBF)
4012 bufsiz = 1;
4013 else if(!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)))
4014 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
4015 else
4016 bufsiz = file->_bufsiz;
4018 pcnt = (wrcnt / bufsiz) * bufsiz;
4020 if(msvcrt_flush_buffer(file) == EOF)
4021 break;
4023 if(_write(file->_file, ptr, pcnt) <= 0) {
4024 file->_flag |= _IOERR;
4025 break;
4027 written += pcnt;
4028 wrcnt -= pcnt;
4029 ptr = (const char*)ptr + pcnt;
4030 } else {
4031 if(_flsbuf(*(const char*)ptr, file) == EOF)
4032 break;
4033 written++;
4034 wrcnt--;
4035 ptr = (const char*)ptr + 1;
4039 return written / size;
4042 /*********************************************************************
4043 * fputwc (MSVCRT.@)
4045 wint_t CDECL fputwc(wint_t wc, FILE* file)
4047 wint_t ret;
4049 _lock_file(file);
4050 ret = _fputwc_nolock(wc, file);
4051 _unlock_file(file);
4053 return ret;
4056 /*********************************************************************
4057 * _fputwc_nolock (MSVCRT.@)
4059 wint_t CDECL _fputwc_nolock(wint_t wc, FILE* file)
4061 wchar_t mwc=wc;
4062 ioinfo *fdinfo;
4063 wint_t ret;
4065 fdinfo = get_ioinfo_nolock(file->_file);
4067 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
4068 char buf[MB_LEN_MAX];
4069 int char_len;
4071 char_len = wctomb(buf, mwc);
4072 if(char_len!=-1 && _fwrite_nolock(buf, char_len, 1, file)==1)
4073 ret = wc;
4074 else
4075 ret = WEOF;
4076 }else if(_fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
4077 ret = wc;
4078 }else {
4079 ret = WEOF;
4082 return ret;
4085 /*********************************************************************
4086 * _fputwchar (MSVCRT.@)
4088 wint_t CDECL _fputwchar(wint_t wc)
4090 return fputwc(wc, MSVCRT_stdout);
4093 /*********************************************************************
4094 * _wfsopen (MSVCRT.@)
4096 FILE * CDECL _wfsopen(const wchar_t *path, const wchar_t *mode, int share)
4098 FILE* file;
4099 int open_flags, stream_flags, fd;
4101 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
4103 /* map mode string to open() flags. "man fopen" for possibilities. */
4104 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4105 return NULL;
4107 LOCK_FILES();
4108 fd = _wsopen(path, open_flags, share, _S_IREAD | _S_IWRITE);
4109 if (fd < 0)
4110 file = NULL;
4111 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
4112 != -1)
4113 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
4114 else if (file)
4116 file->_flag = 0;
4117 file = NULL;
4120 TRACE(":got (%p)\n",file);
4121 if (fd >= 0 && !file)
4122 _close(fd);
4123 UNLOCK_FILES();
4124 return file;
4127 /*********************************************************************
4128 * _fsopen (MSVCRT.@)
4130 FILE * CDECL _fsopen(const char *path, const char *mode, int share)
4132 FILE *ret;
4133 wchar_t *pathW = NULL, *modeW = NULL;
4135 if (path && !(pathW = msvcrt_wstrdupa(path))) {
4136 _invalid_parameter(NULL, NULL, NULL, 0, 0);
4137 *_errno() = EINVAL;
4138 return NULL;
4140 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4142 free(pathW);
4143 _invalid_parameter(NULL, NULL, NULL, 0, 0);
4144 *_errno() = EINVAL;
4145 return NULL;
4148 ret = _wfsopen(pathW, modeW, share);
4150 free(pathW);
4151 free(modeW);
4152 return ret;
4155 /*********************************************************************
4156 * fopen (MSVCRT.@)
4158 FILE * CDECL fopen(const char *path, const char *mode)
4160 return _fsopen( path, mode, _SH_DENYNO );
4163 /*********************************************************************
4164 * fopen_s (MSVCRT.@)
4166 int CDECL fopen_s(FILE** pFile,
4167 const char *filename, const char *mode)
4169 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4170 if (!MSVCRT_CHECK_PMT(filename != NULL)) return EINVAL;
4171 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4173 *pFile = fopen(filename, mode);
4175 if(!*pFile)
4176 return *_errno();
4177 return 0;
4180 /*********************************************************************
4181 * _wfopen (MSVCRT.@)
4183 FILE * CDECL _wfopen(const wchar_t *path, const wchar_t *mode)
4185 return _wfsopen( path, mode, _SH_DENYNO );
4188 /*********************************************************************
4189 * _wfopen_s (MSVCRT.@)
4191 int CDECL _wfopen_s(FILE** pFile, const wchar_t *filename,
4192 const wchar_t *mode)
4194 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4195 if (!MSVCRT_CHECK_PMT(filename != NULL)) return EINVAL;
4196 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4198 *pFile = _wfopen(filename, mode);
4200 if(!*pFile)
4201 return *_errno();
4202 return 0;
4205 /*********************************************************************
4206 * fputc (MSVCRT.@)
4208 int CDECL fputc(int c, FILE* file)
4210 int ret;
4212 _lock_file(file);
4213 ret = _fputc_nolock(c, file);
4214 _unlock_file(file);
4216 return ret;
4219 /*********************************************************************
4220 * _fputc_nolock (MSVCRT.@)
4222 int CDECL _fputc_nolock(int c, FILE* file)
4224 int res;
4226 if(file->_cnt>0) {
4227 *file->_ptr++=c;
4228 file->_cnt--;
4229 if (c == '\n')
4231 res = msvcrt_flush_buffer(file);
4232 return res ? res : c;
4234 else {
4235 return c & 0xff;
4237 } else {
4238 res = _flsbuf(c, file);
4239 return res;
4243 /*********************************************************************
4244 * _fputchar (MSVCRT.@)
4246 int CDECL _fputchar(int c)
4248 return fputc(c, MSVCRT_stdout);
4251 /*********************************************************************
4252 * fread (MSVCRT.@)
4254 size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file)
4256 size_t ret;
4258 _lock_file(file);
4259 ret = _fread_nolock(ptr, size, nmemb, file);
4260 _unlock_file(file);
4262 return ret;
4265 /*********************************************************************
4266 * _fread_nolock (MSVCRT.@)
4268 size_t CDECL _fread_nolock(void *ptr, size_t size, size_t nmemb, FILE* file)
4270 size_t rcnt=size * nmemb;
4271 size_t read=0;
4272 size_t pread=0;
4274 if(!rcnt)
4275 return 0;
4277 /* first buffered data */
4278 if(file->_cnt>0) {
4279 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4280 memcpy(ptr, file->_ptr, pcnt);
4281 file->_cnt -= pcnt;
4282 file->_ptr += pcnt;
4283 read += pcnt ;
4284 rcnt -= pcnt ;
4285 ptr = (char*)ptr + pcnt;
4286 } else if(!(file->_flag & _IOREAD )) {
4287 if(file->_flag & _IORW) {
4288 file->_flag |= _IOREAD;
4289 } else {
4290 return 0;
4294 if(rcnt>0 && !(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF)))
4295 msvcrt_alloc_buffer(file);
4297 while(rcnt>0)
4299 int i;
4300 if (!file->_cnt && rcnt<file->_bufsiz && (file->_flag & (_IOMYBUF | MSVCRT__USERBUF))) {
4301 i = _read(file->_file, file->_base, file->_bufsiz);
4302 file->_ptr = file->_base;
4303 if (i != -1) {
4304 file->_cnt = i;
4305 if (i > rcnt) i = rcnt;
4307 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4308 if (i > 0 && i < file->_cnt) {
4309 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4310 file->_flag &= ~_IOEOF;
4312 if (i > 0) {
4313 memcpy(ptr, file->_ptr, i);
4314 file->_cnt -= i;
4315 file->_ptr += i;
4317 } else if (rcnt > INT_MAX) {
4318 i = _read(file->_file, ptr, INT_MAX);
4319 } else if (rcnt < (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ)) {
4320 i = _read(file->_file, ptr, rcnt);
4321 } else {
4322 i = _read(file->_file, ptr, rcnt - rcnt % (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ));
4324 pread += i;
4325 rcnt -= i;
4326 ptr = (char *)ptr+i;
4327 /* expose feof condition in the flags
4328 * MFC tests file->_flag for feof, and doesn't call feof())
4330 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4331 file->_flag |= _IOEOF;
4332 else if (i == -1)
4334 file->_flag |= _IOERR;
4335 pread = 0;
4336 rcnt = 0;
4338 if (i < 1) break;
4340 read+=pread;
4341 return read / size;
4344 #if _MSVCR_VER >= 80
4346 /*********************************************************************
4347 * fread_s (MSVCR80.@)
4349 size_t CDECL fread_s(void *buf, size_t buf_size, size_t elem_size,
4350 size_t count, FILE *stream)
4352 size_t ret;
4354 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4355 if(buf && buf_size)
4356 memset(buf, 0, buf_size);
4357 return 0;
4359 if(!elem_size || !count) return 0;
4361 _lock_file(stream);
4362 ret = _fread_nolock_s(buf, buf_size, elem_size, count, stream);
4363 _unlock_file(stream);
4365 return ret;
4368 /*********************************************************************
4369 * _fread_nolock_s (MSVCR80.@)
4371 size_t CDECL _fread_nolock_s(void *buf, size_t buf_size, size_t elem_size,
4372 size_t count, FILE *stream)
4374 size_t bytes_left, buf_pos;
4376 TRACE("(%p %Iu %Iu %Iu %p)\n", buf, buf_size, elem_size, count, stream);
4378 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4379 if(buf && buf_size)
4380 memset(buf, 0, buf_size);
4381 return 0;
4383 if(!elem_size || !count) return 0;
4384 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4385 if(!MSVCRT_CHECK_PMT(SIZE_MAX/count >= elem_size)) return 0;
4387 bytes_left = elem_size*count;
4388 buf_pos = 0;
4389 while(bytes_left) {
4390 if(stream->_cnt > 0) {
4391 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4393 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, ERANGE)) {
4394 memset(buf, 0, buf_size);
4395 return 0;
4398 _fread_nolock((char*)buf+buf_pos, 1, size, stream);
4399 buf_pos += size;
4400 bytes_left -= size;
4401 }else {
4402 int c = _filbuf(stream);
4404 if(c == EOF)
4405 break;
4407 if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, ERANGE)) {
4408 memset(buf, 0, buf_size);
4409 return 0;
4412 ((char*)buf)[buf_pos++] = c;
4413 bytes_left--;
4417 return buf_pos/elem_size;
4420 #endif /* _MSVCR_VER >= 80 */
4422 /*********************************************************************
4423 * _wfreopen (MSVCRT.@)
4426 FILE* CDECL _wfreopen(const wchar_t *path, const wchar_t *mode, FILE* file)
4428 int open_flags, stream_flags, fd;
4430 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4432 LOCK_FILES();
4433 if (!file || ((fd = file->_file) < 0))
4434 file = NULL;
4435 else
4437 fclose(file);
4438 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4439 file = NULL;
4440 else if((fd = _wopen(path, open_flags, _S_IREAD | _S_IWRITE)) < 0)
4441 file = NULL;
4442 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4444 file->_flag = 0;
4445 file = NULL;
4448 UNLOCK_FILES();
4449 return file;
4452 /*********************************************************************
4453 * _wfreopen_s (MSVCRT.@)
4455 int CDECL _wfreopen_s(FILE** pFile,
4456 const wchar_t *path, const wchar_t *mode, FILE* file)
4458 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4459 if (!MSVCRT_CHECK_PMT(path != NULL)) return EINVAL;
4460 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4461 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
4463 *pFile = _wfreopen(path, mode, file);
4465 if(!*pFile)
4466 return *_errno();
4467 return 0;
4470 /*********************************************************************
4471 * freopen (MSVCRT.@)
4474 FILE* CDECL freopen(const char *path, const char *mode, FILE* file)
4476 FILE *ret;
4477 wchar_t *pathW = NULL, *modeW = NULL;
4479 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4480 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4482 free(pathW);
4483 return NULL;
4486 ret = _wfreopen(pathW, modeW, file);
4488 free(pathW);
4489 free(modeW);
4490 return ret;
4493 /*********************************************************************
4494 * freopen_s (MSVCRT.@)
4496 int CDECL freopen_s(FILE** pFile,
4497 const char *path, const char *mode, FILE* file)
4499 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4500 if (!MSVCRT_CHECK_PMT(path != NULL)) return EINVAL;
4501 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4502 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
4504 *pFile = freopen(path, mode, file);
4506 if(!*pFile)
4507 return *_errno();
4508 return 0;
4511 /*********************************************************************
4512 * fsetpos (MSVCRT.@)
4514 int CDECL fsetpos(FILE* file, fpos_t *pos)
4516 int ret;
4518 _lock_file(file);
4519 msvcrt_flush_buffer(file);
4521 /* Reset direction of i/o */
4522 if(file->_flag & _IORW) {
4523 file->_flag &= ~(_IOREAD|_IOWRT);
4526 ret = (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
4527 _unlock_file(file);
4528 return ret;
4531 /*********************************************************************
4532 * _ftelli64 (MSVCRT.@)
4534 __int64 CDECL _ftelli64(FILE* file)
4536 __int64 ret;
4538 _lock_file(file);
4539 ret = _ftelli64_nolock(file);
4540 _unlock_file(file);
4542 return ret;
4545 /*********************************************************************
4546 * _ftelli64_nolock (MSVCRT.@)
4548 __int64 CDECL _ftelli64_nolock(FILE* file)
4550 __int64 pos;
4552 pos = _telli64(file->_file);
4553 if(pos == -1)
4554 return -1;
4555 if(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) {
4556 if(file->_flag & _IOWRT) {
4557 pos += file->_ptr - file->_base;
4559 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4560 char *p;
4562 for(p=file->_base; p<file->_ptr; p++)
4563 if(*p == '\n')
4564 pos++;
4566 } else if(!file->_cnt) { /* nothing to do */
4567 } else if(_lseeki64(file->_file, 0, SEEK_END)==pos) {
4568 int i;
4570 pos -= file->_cnt;
4571 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4572 for(i=0; i<file->_cnt; i++)
4573 if(file->_ptr[i] == '\n')
4574 pos--;
4576 } else {
4577 char *p;
4579 if(_lseeki64(file->_file, pos, SEEK_SET) != pos)
4580 return -1;
4582 pos -= file->_bufsiz;
4583 pos += file->_ptr - file->_base;
4585 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4586 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4587 pos--;
4589 for(p=file->_base; p<file->_ptr; p++)
4590 if(*p == '\n')
4591 pos++;
4596 return pos;
4599 /*********************************************************************
4600 * ftell (MSVCRT.@)
4602 __msvcrt_long CDECL ftell(FILE* file)
4604 return _ftelli64(file);
4607 #if _MSVCR_VER >= 80
4608 /*********************************************************************
4609 * _ftell_nolock (MSVCR80.@)
4611 __msvcrt_long CDECL _ftell_nolock(FILE* file)
4613 return _ftelli64_nolock(file);
4615 #endif
4617 /*********************************************************************
4618 * fgetpos (MSVCRT.@)
4620 int CDECL fgetpos(FILE* file, fpos_t *pos)
4622 *pos = _ftelli64(file);
4623 if(*pos == -1)
4624 return -1;
4625 return 0;
4628 /*********************************************************************
4629 * fputs (MSVCRT.@)
4631 int CDECL fputs(const char *s, FILE* file)
4633 size_t len = strlen(s);
4634 int ret;
4636 _lock_file(file);
4637 ret = _fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : EOF;
4638 _unlock_file(file);
4639 return ret;
4642 /*********************************************************************
4643 * fputws (MSVCRT.@)
4645 int CDECL fputws(const wchar_t *s, FILE* file)
4647 size_t i, len = wcslen(s);
4648 BOOL tmp_buf;
4649 int ret;
4651 _lock_file(file);
4652 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4653 ret = _fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : EOF;
4654 _unlock_file(file);
4655 return ret;
4658 tmp_buf = add_std_buffer(file);
4659 for (i=0; i<len; i++) {
4660 if(_fputwc_nolock(s[i], file) == WEOF) {
4661 if(tmp_buf) remove_std_buffer(file);
4662 _unlock_file(file);
4663 return WEOF;
4667 if(tmp_buf) remove_std_buffer(file);
4668 _unlock_file(file);
4669 return 0;
4672 /*********************************************************************
4673 * getchar (MSVCRT.@)
4675 int CDECL getchar(void)
4677 return fgetc(MSVCRT_stdin);
4680 /*********************************************************************
4681 * getc (MSVCRT.@)
4683 int CDECL getc(FILE* file)
4685 return fgetc(file);
4688 /*********************************************************************
4689 * gets_s (MSVCR80.@)
4691 char * CDECL gets_s(char *buf, size_t len)
4693 char *buf_start = buf;
4694 int cc;
4696 if (!MSVCRT_CHECK_PMT(buf != NULL)) return NULL;
4697 if (!MSVCRT_CHECK_PMT(len != 0)) return NULL;
4699 _lock_file(MSVCRT_stdin);
4700 for(cc = _fgetc_nolock(MSVCRT_stdin);
4701 len != 0 && cc != EOF && cc != '\n';
4702 cc = _fgetc_nolock(MSVCRT_stdin))
4704 if (cc != '\r')
4706 *buf++ = (char)cc;
4707 len--;
4710 _unlock_file(MSVCRT_stdin);
4712 if (!len)
4714 *buf_start = 0;
4715 _invalid_parameter(NULL, NULL, NULL, 0, 0);
4716 return NULL;
4719 if ((cc == EOF) && (buf_start == buf))
4721 TRACE(":nothing read\n");
4722 return NULL;
4724 *buf = '\0';
4726 TRACE("got '%s'\n", buf_start);
4727 return buf_start;
4730 /*********************************************************************
4731 * gets (MSVCRT.@)
4733 char * CDECL gets(char *buf)
4735 return gets_s(buf, -1);
4738 /*********************************************************************
4739 * _getws (MSVCRT.@)
4741 wchar_t* CDECL _getws(wchar_t* buf)
4743 wint_t cc;
4744 wchar_t* ws = buf;
4746 _lock_file(MSVCRT_stdin);
4747 for (cc = _fgetwc_nolock(MSVCRT_stdin); cc != WEOF && cc != '\n';
4748 cc = _fgetwc_nolock(MSVCRT_stdin))
4750 if (cc != '\r')
4751 *buf++ = (wchar_t)cc;
4753 _unlock_file(MSVCRT_stdin);
4755 if ((cc == WEOF) && (ws == buf))
4757 TRACE(":nothing read\n");
4758 return NULL;
4760 *buf = '\0';
4762 TRACE("got %s\n", debugstr_w(ws));
4763 return ws;
4766 /*********************************************************************
4767 * putc (MSVCRT.@)
4769 int CDECL putc(int c, FILE* file)
4771 return fputc(c, file);
4774 /*********************************************************************
4775 * putchar (MSVCRT.@)
4777 int CDECL putchar(int c)
4779 return fputc(c, MSVCRT_stdout);
4782 /*********************************************************************
4783 * puts (MSVCRT.@)
4785 int CDECL puts(const char *s)
4787 size_t len = strlen(s);
4788 int ret;
4790 _lock_file(MSVCRT_stdout);
4791 if(_fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4792 _unlock_file(MSVCRT_stdout);
4793 return EOF;
4796 ret = _fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : EOF;
4797 _unlock_file(MSVCRT_stdout);
4798 return ret;
4801 /*********************************************************************
4802 * _putws (MSVCRT.@)
4804 int CDECL _putws(const wchar_t *s)
4806 int ret;
4808 _lock_file(MSVCRT_stdout);
4809 ret = fputws(s, MSVCRT_stdout);
4810 if(ret >= 0)
4811 ret = _fputwc_nolock('\n', MSVCRT_stdout);
4812 _unlock_file(MSVCRT_stdout);
4813 return ret >= 0 ? 0 : WEOF;
4816 /*********************************************************************
4817 * remove (MSVCRT.@)
4819 int CDECL remove(const char *path)
4821 TRACE("(%s)\n",path);
4822 if (DeleteFileA(path))
4823 return 0;
4824 TRACE(":failed (%d)\n",GetLastError());
4825 msvcrt_set_errno(GetLastError());
4826 return -1;
4829 /*********************************************************************
4830 * _wremove (MSVCRT.@)
4832 int CDECL _wremove(const wchar_t *path)
4834 TRACE("(%s)\n",debugstr_w(path));
4835 if (DeleteFileW(path))
4836 return 0;
4837 TRACE(":failed (%d)\n",GetLastError());
4838 msvcrt_set_errno(GetLastError());
4839 return -1;
4842 /*********************************************************************
4843 * rename (MSVCRT.@)
4845 int CDECL rename(const char *oldpath,const char *newpath)
4847 TRACE(":from %s to %s\n",oldpath,newpath);
4848 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4849 return 0;
4850 TRACE(":failed (%d)\n",GetLastError());
4851 msvcrt_set_errno(GetLastError());
4852 return -1;
4855 /*********************************************************************
4856 * _wrename (MSVCRT.@)
4858 int CDECL _wrename(const wchar_t *oldpath,const wchar_t *newpath)
4860 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4861 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4862 return 0;
4863 TRACE(":failed (%d)\n",GetLastError());
4864 msvcrt_set_errno(GetLastError());
4865 return -1;
4868 /*********************************************************************
4869 * setvbuf (MSVCRT.@)
4871 int CDECL setvbuf(FILE* file, char *buf, int mode, size_t size)
4873 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4874 if(!MSVCRT_CHECK_PMT(mode==_IONBF || mode==_IOFBF || mode==_IOLBF)) return -1;
4875 if(!MSVCRT_CHECK_PMT(mode==_IONBF || (size>=2 && size<=INT_MAX))) return -1;
4877 _lock_file(file);
4879 _fflush_nolock(file);
4880 if(file->_flag & _IOMYBUF)
4881 free(file->_base);
4882 file->_flag &= ~(_IONBF | _IOMYBUF | MSVCRT__USERBUF);
4883 file->_cnt = 0;
4885 if(mode == _IONBF) {
4886 file->_flag |= _IONBF;
4887 file->_base = file->_ptr = (char*)&file->_charbuf;
4888 file->_bufsiz = 2;
4889 }else if(buf) {
4890 file->_base = file->_ptr = buf;
4891 file->_flag |= MSVCRT__USERBUF;
4892 file->_bufsiz = size;
4893 }else {
4894 file->_base = file->_ptr = malloc(size);
4895 if(!file->_base) {
4896 file->_bufsiz = 0;
4897 _unlock_file(file);
4898 return -1;
4901 file->_flag |= _IOMYBUF;
4902 file->_bufsiz = size;
4904 _unlock_file(file);
4905 return 0;
4908 /*********************************************************************
4909 * setbuf (MSVCRT.@)
4911 void CDECL setbuf(FILE* file, char *buf)
4913 setvbuf(file, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
4916 static int tmpnam_helper(char *s, size_t size, int *tmpnam_unique, int tmp_max)
4918 char tmpstr[8];
4919 char *p = s;
4920 int digits;
4922 if (!MSVCRT_CHECK_PMT(s != NULL)) return EINVAL;
4924 if (size < 3) {
4925 if (size) *s = 0;
4926 *_errno() = ERANGE;
4927 return ERANGE;
4929 *p++ = '\\';
4930 *p++ = 's';
4931 size -= 2;
4932 digits = msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4933 if (digits+1 > size) {
4934 *s = 0;
4935 *_errno() = ERANGE;
4936 return ERANGE;
4938 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4939 p += digits;
4940 *p++ = '.';
4941 size -= digits+1;
4943 while(1) {
4944 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4945 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4946 break;
4949 digits = msvcrt_int_to_base32(digits, tmpstr);
4950 if (digits+1 > size) {
4951 *s = 0;
4952 *_errno() = ERANGE;
4953 return ERANGE;
4955 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4956 p[digits] = 0;
4958 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4959 GetLastError() == ERROR_FILE_NOT_FOUND)
4960 break;
4962 return 0;
4965 int CDECL tmpnam_s(char *s, size_t size)
4967 return tmpnam_helper(s, size, &tmpnam_s_unique, TMP_MAX_S);
4970 /*********************************************************************
4971 * tmpnam (MSVCRT.@)
4973 char * CDECL tmpnam(char *s)
4975 if (!s) {
4976 thread_data_t *data = msvcrt_get_thread_data();
4978 if(!data->tmpnam_buffer)
4979 data->tmpnam_buffer = malloc(MAX_PATH);
4981 s = data->tmpnam_buffer;
4984 return tmpnam_helper(s, -1, &tmpnam_unique, TMP_MAX) ? NULL : s;
4987 static int wtmpnam_helper(wchar_t *s, size_t size, int *tmpnam_unique, int tmp_max)
4989 wchar_t tmpstr[8];
4990 wchar_t *p = s;
4991 int digits;
4993 if (!MSVCRT_CHECK_PMT(s != NULL)) return EINVAL;
4995 if (size < 3) {
4996 if (size) *s = 0;
4997 *_errno() = ERANGE;
4998 return ERANGE;
5000 *p++ = '\\';
5001 *p++ = 's';
5002 size -= 2;
5003 digits = msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
5004 if (digits+1 > size) {
5005 *s = 0;
5006 *_errno() = ERANGE;
5007 return ERANGE;
5009 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5010 p += digits;
5011 *p++ = '.';
5012 size -= digits+1;
5014 while(1) {
5015 while ((digits = *tmpnam_unique)+1 < tmp_max) {
5016 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
5017 break;
5020 digits = msvcrt_int_to_base32_w(digits, tmpstr);
5021 if (digits+1 > size) {
5022 *s = 0;
5023 *_errno() = ERANGE;
5024 return ERANGE;
5026 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5027 p[digits] = 0;
5029 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
5030 GetLastError() == ERROR_FILE_NOT_FOUND)
5031 break;
5033 return 0;
5036 /*********************************************************************
5037 * _wtmpnam_s (MSVCRT.@)
5039 int CDECL _wtmpnam_s(wchar_t *s, size_t size)
5041 return wtmpnam_helper(s, size, &tmpnam_s_unique, TMP_MAX_S);
5044 /*********************************************************************
5045 * _wtmpnam (MSVCRT.@)
5047 wchar_t * CDECL _wtmpnam(wchar_t *s)
5049 if (!s) {
5050 thread_data_t *data = msvcrt_get_thread_data();
5052 if(!data->wtmpnam_buffer)
5053 data->wtmpnam_buffer = malloc(sizeof(wchar_t[MAX_PATH]));
5055 s = data->wtmpnam_buffer;
5058 return wtmpnam_helper(s, -1, &tmpnam_unique, TMP_MAX) ? NULL : s;
5061 /*********************************************************************
5062 * tmpfile (MSVCRT.@)
5064 FILE* CDECL tmpfile(void)
5066 char *filename = _tempnam(",", "t");
5067 int fd;
5068 FILE* file = NULL;
5070 LOCK_FILES();
5071 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY,
5072 _S_IREAD | _S_IWRITE);
5073 if (fd != -1 && (file = msvcrt_alloc_fp()))
5075 if (msvcrt_init_fp(file, fd, _IORW) == -1)
5077 file->_flag = 0;
5078 file = NULL;
5080 else file->_tmpfname = _strdup(filename);
5083 if(fd != -1 && !file)
5084 _close(fd);
5085 free(filename);
5086 UNLOCK_FILES();
5087 return file;
5090 /*********************************************************************
5091 * tmpfile_s (MSVCRT.@)
5093 int CDECL tmpfile_s(FILE** file)
5095 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
5097 *file = tmpfile();
5098 return 0;
5101 static int puts_clbk_file_a(void *file, int len, const char *str)
5103 return fwrite(str, sizeof(char), len, file);
5106 static int puts_clbk_file_w(void *file, int len, const wchar_t *str)
5108 int i, ret;
5110 _lock_file(file);
5112 if(!(get_ioinfo_nolock(((FILE*)file)->_file)->wxflag & WX_TEXT)) {
5113 ret = _fwrite_nolock(str, sizeof(wchar_t), len, file);
5114 _unlock_file(file);
5115 return ret;
5118 for(i=0; i<len; i++) {
5119 if(_fputwc_nolock(str[i], file) == WEOF) {
5120 _unlock_file(file);
5121 return -1;
5125 _unlock_file(file);
5126 return len;
5129 static int vfprintf_helper(DWORD options, FILE* file, const char *format,
5130 _locale_t locale, __ms_va_list valist)
5132 printf_arg args_ctx[_ARGMAX+1];
5133 BOOL tmp_buf;
5134 int ret;
5136 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5137 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5139 if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
5140 memset(args_ctx, 0, sizeof(args_ctx));
5141 ret = create_positional_ctx_a(args_ctx, format, valist);
5142 if(ret < 0) {
5143 _invalid_parameter(NULL, NULL, NULL, 0, 0);
5144 *_errno() = EINVAL;
5145 return ret;
5146 } else if(!ret)
5147 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5150 _lock_file(file);
5151 tmp_buf = add_std_buffer(file);
5152 ret = pf_printf_a(puts_clbk_file_a, file, format, locale, options,
5153 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
5154 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
5155 if(tmp_buf) remove_std_buffer(file);
5156 _unlock_file(file);
5158 return ret;
5161 static int vfwprintf_helper(DWORD options, FILE* file, const wchar_t *format,
5162 _locale_t locale, __ms_va_list valist)
5164 printf_arg args_ctx[_ARGMAX+1];
5165 BOOL tmp_buf;
5166 int ret;
5168 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5169 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5171 if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
5172 memset(args_ctx, 0, sizeof(args_ctx));
5173 ret = create_positional_ctx_w(args_ctx, format, valist);
5174 if(ret < 0) {
5175 _invalid_parameter(NULL, NULL, NULL, 0, 0);
5176 *_errno() = EINVAL;
5177 return ret;
5178 } else if(!ret)
5179 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5182 _lock_file(file);
5183 tmp_buf = add_std_buffer(file);
5184 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, options,
5185 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
5186 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
5187 if(tmp_buf) remove_std_buffer(file);
5188 _unlock_file(file);
5190 return ret;
5193 /*********************************************************************
5194 * _vfprintf_s_l (MSVCRT.@)
5196 int CDECL _vfprintf_s_l(FILE* file, const char *format,
5197 _locale_t locale, __ms_va_list valist)
5199 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5202 /*********************************************************************
5203 * _vfwprintf_s_l (MSVCRT.@)
5205 int CDECL _vfwprintf_s_l(FILE* file, const wchar_t *format,
5206 _locale_t locale, __ms_va_list valist)
5208 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5211 /*********************************************************************
5212 * vfprintf (MSVCRT.@)
5214 int CDECL vfprintf(FILE* file, const char *format, __ms_va_list valist)
5216 return vfprintf_helper(0, file, format, NULL, valist);
5219 /*********************************************************************
5220 * vfprintf_s (MSVCRT.@)
5222 int CDECL vfprintf_s(FILE* file, const char *format, __ms_va_list valist)
5224 return _vfprintf_s_l(file, format, NULL, valist);
5227 /*********************************************************************
5228 * vfwprintf (MSVCRT.@)
5230 int CDECL vfwprintf(FILE* file, const wchar_t *format, __ms_va_list valist)
5232 return vfwprintf_helper(0, file, format, NULL, valist);
5235 /*********************************************************************
5236 * vfwprintf_s (MSVCRT.@)
5238 int CDECL vfwprintf_s(FILE* file, const wchar_t *format, __ms_va_list valist)
5240 return _vfwprintf_s_l(file, format, NULL, valist);
5243 #if _MSVCR_VER >= 140
5245 /*********************************************************************
5246 * __stdio_common_vfprintf (UCRTBASE.@)
5248 int CDECL _stdio_common_vfprintf(unsigned __int64 options, FILE *file, const char *format,
5249 _locale_t locale, __ms_va_list valist)
5251 if (options & ~UCRTBASE_PRINTF_MASK)
5252 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5254 return vfprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
5257 /*********************************************************************
5258 * __stdio_common_vfprintf_s (UCRTBASE.@)
5260 int CDECL __stdio_common_vfprintf_s(unsigned __int64 options, FILE *file, const char *format,
5261 _locale_t locale, __ms_va_list valist)
5263 if (options & ~UCRTBASE_PRINTF_MASK)
5264 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5266 return vfprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5267 file, format, locale, valist);
5270 /*********************************************************************
5271 * __stdio_common_vfwprintf (UCRTBASE.@)
5273 int CDECL __stdio_common_vfwprintf(unsigned __int64 options, FILE *file, const wchar_t *format,
5274 _locale_t locale, __ms_va_list valist)
5276 if (options & ~UCRTBASE_PRINTF_MASK)
5277 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5279 return vfwprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
5282 /*********************************************************************
5283 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5285 int CDECL __stdio_common_vfwprintf_s(unsigned __int64 options, FILE *file, const wchar_t *format,
5286 _locale_t locale, __ms_va_list valist)
5288 if (options & ~UCRTBASE_PRINTF_MASK)
5289 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5291 return vfwprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5292 file, format, locale, valist);
5295 #endif /* _MSVCR_VER >= 140 */
5297 /*********************************************************************
5298 * _vfprintf_l (MSVCRT.@)
5300 int CDECL _vfprintf_l(FILE* file, const char *format,
5301 _locale_t locale, __ms_va_list valist)
5303 return vfprintf_helper(0, file, format, locale, valist);
5306 /*********************************************************************
5307 * _vfwprintf_l (MSVCRT.@)
5309 int CDECL _vfwprintf_l(FILE* file, const wchar_t *format,
5310 _locale_t locale, __ms_va_list valist)
5312 return vfwprintf_helper(0, file, format, locale, valist);
5315 /*********************************************************************
5316 * _vfprintf_p_l (MSVCRT.@)
5318 int CDECL _vfprintf_p_l(FILE* file, const char *format,
5319 _locale_t locale, __ms_va_list valist)
5321 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5322 file, format, locale, valist);
5325 /*********************************************************************
5326 * _vfprintf_p (MSVCRT.@)
5328 int CDECL _vfprintf_p(FILE* file, const char *format, __ms_va_list valist)
5330 return _vfprintf_p_l(file, format, NULL, valist);
5333 /*********************************************************************
5334 * _vfwprintf_p_l (MSVCRT.@)
5336 int CDECL _vfwprintf_p_l(FILE* file, const wchar_t *format,
5337 _locale_t locale, __ms_va_list valist)
5339 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5340 file, format, locale, valist);
5343 /*********************************************************************
5344 * _vfwprintf_p (MSVCRT.@)
5346 int CDECL _vfwprintf_p(FILE* file, const wchar_t *format, __ms_va_list valist)
5348 return _vfwprintf_p_l(file, format, NULL, valist);
5351 /*********************************************************************
5352 * vprintf (MSVCRT.@)
5354 int CDECL vprintf(const char *format, __ms_va_list valist)
5356 return vfprintf(MSVCRT_stdout,format,valist);
5359 /*********************************************************************
5360 * vprintf_s (MSVCRT.@)
5362 int CDECL vprintf_s(const char *format, __ms_va_list valist)
5364 return vfprintf_s(MSVCRT_stdout,format,valist);
5367 /*********************************************************************
5368 * vwprintf (MSVCRT.@)
5370 int CDECL vwprintf(const wchar_t *format, __ms_va_list valist)
5372 return vfwprintf(MSVCRT_stdout,format,valist);
5375 /*********************************************************************
5376 * vwprintf_s (MSVCRT.@)
5378 int CDECL vwprintf_s(const wchar_t *format, __ms_va_list valist)
5380 return vfwprintf_s(MSVCRT_stdout,format,valist);
5383 /*********************************************************************
5384 * fprintf (MSVCRT.@)
5386 int WINAPIV fprintf(FILE* file, const char *format, ...)
5388 __ms_va_list valist;
5389 int res;
5390 __ms_va_start(valist, format);
5391 res = vfprintf(file, format, valist);
5392 __ms_va_end(valist);
5393 return res;
5396 /*********************************************************************
5397 * fprintf_s (MSVCRT.@)
5399 int WINAPIV fprintf_s(FILE* file, const char *format, ...)
5401 __ms_va_list valist;
5402 int res;
5403 __ms_va_start(valist, format);
5404 res = vfprintf_s(file, format, valist);
5405 __ms_va_end(valist);
5406 return res;
5409 /*********************************************************************
5410 * fwprintf (MSVCRT.@)
5412 int WINAPIV fwprintf(FILE* file, const wchar_t *format, ...)
5414 __ms_va_list valist;
5415 int res;
5416 __ms_va_start(valist, format);
5417 res = vfwprintf(file, format, valist);
5418 __ms_va_end(valist);
5419 return res;
5422 /*********************************************************************
5423 * fwprintf_s (MSVCRT.@)
5425 int WINAPIV fwprintf_s(FILE* file, const wchar_t *format, ...)
5427 __ms_va_list valist;
5428 int res;
5429 __ms_va_start(valist, format);
5430 res = vfwprintf_s(file, format, valist);
5431 __ms_va_end(valist);
5432 return res;
5435 /*********************************************************************
5436 * _fwprintf_l (MSVCRT.@)
5438 int WINAPIV _fwprintf_l(FILE* file, const wchar_t *format, _locale_t locale, ...)
5440 __ms_va_list valist;
5441 int res;
5442 __ms_va_start(valist, locale);
5443 res = _vfwprintf_l(file, format, locale, valist);
5444 __ms_va_end(valist);
5445 return res;
5448 /*********************************************************************
5449 * printf (MSVCRT.@)
5451 int WINAPIV printf(const char *format, ...)
5453 __ms_va_list valist;
5454 int res;
5455 __ms_va_start(valist, format);
5456 res = vfprintf(MSVCRT_stdout, format, valist);
5457 __ms_va_end(valist);
5458 return res;
5461 /*********************************************************************
5462 * printf_s (MSVCRT.@)
5464 int WINAPIV printf_s(const char *format, ...)
5466 __ms_va_list valist;
5467 int res;
5468 __ms_va_start(valist, format);
5469 res = vprintf_s(format, valist);
5470 __ms_va_end(valist);
5471 return res;
5474 /*********************************************************************
5475 * ungetc (MSVCRT.@)
5477 int CDECL ungetc(int c, FILE * file)
5479 int ret;
5481 if(!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
5483 _lock_file(file);
5484 ret = _ungetc_nolock(c, file);
5485 _unlock_file(file);
5487 return ret;
5490 /*********************************************************************
5491 * _ungetc_nolock (MSVCRT.@)
5493 int CDECL _ungetc_nolock(int c, FILE * file)
5495 if(!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
5497 if (c == EOF || !(file->_flag&_IOREAD ||
5498 (file->_flag&_IORW && !(file->_flag&_IOWRT))))
5499 return EOF;
5501 if((!(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF))
5502 && msvcrt_alloc_buffer(file))
5503 || (!file->_cnt && file->_ptr==file->_base))
5504 file->_ptr++;
5506 if(file->_ptr>file->_base) {
5507 file->_ptr--;
5508 if(file->_flag & _IOSTRG) {
5509 if(*file->_ptr != c) {
5510 file->_ptr++;
5511 return EOF;
5513 }else {
5514 *file->_ptr = c;
5516 file->_cnt++;
5517 file->_flag &= ~(_IOERR | _IOEOF);
5518 file->_flag |= _IOREAD;
5519 return c;
5522 return EOF;
5525 /*********************************************************************
5526 * ungetwc (MSVCRT.@)
5528 wint_t CDECL ungetwc(wint_t wc, FILE * file)
5530 wint_t ret;
5532 if(!MSVCRT_CHECK_PMT(file != NULL)) return WEOF;
5534 _lock_file(file);
5535 ret = _ungetwc_nolock(wc, file);
5536 _unlock_file(file);
5538 return ret;
5541 /*********************************************************************
5542 * _ungetwc_nolock (MSVCRT.@)
5544 wint_t CDECL _ungetwc_nolock(wint_t wc, FILE * file)
5546 wchar_t mwc = wc;
5548 if(!MSVCRT_CHECK_PMT(file != NULL)) return WEOF;
5549 if (wc == WEOF)
5550 return WEOF;
5552 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5553 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5554 unsigned char * pp = (unsigned char *)&mwc;
5555 int i;
5557 for(i=sizeof(wchar_t)-1;i>=0;i--) {
5558 if(pp[i] != _ungetc_nolock(pp[i],file))
5559 return WEOF;
5561 }else {
5562 char mbs[MB_LEN_MAX];
5563 int len;
5565 len = wctomb(mbs, mwc);
5566 if(len == -1)
5567 return WEOF;
5569 for(len--; len>=0; len--) {
5570 if(mbs[len] != _ungetc_nolock(mbs[len], file))
5571 return WEOF;
5575 return mwc;
5578 /*********************************************************************
5579 * wprintf (MSVCRT.@)
5581 int WINAPIV wprintf(const wchar_t *format, ...)
5583 __ms_va_list valist;
5584 int res;
5585 __ms_va_start(valist, format);
5586 res = vwprintf(format, valist);
5587 __ms_va_end(valist);
5588 return res;
5591 /*********************************************************************
5592 * wprintf_s (MSVCRT.@)
5594 int WINAPIV wprintf_s(const wchar_t *format, ...)
5596 __ms_va_list valist;
5597 int res;
5598 __ms_va_start(valist, format);
5599 res = vwprintf_s(format, valist);
5600 __ms_va_end(valist);
5601 return res;
5604 /*********************************************************************
5605 * _getmaxstdio (MSVCRT.@)
5607 int CDECL _getmaxstdio(void)
5609 return MSVCRT_max_streams;
5612 /*********************************************************************
5613 * _setmaxstdio (MSVCRT.@)
5615 int CDECL _setmaxstdio(int newmax)
5617 TRACE("%d\n", newmax);
5619 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5620 return -1;
5622 MSVCRT_max_streams = newmax;
5623 return MSVCRT_max_streams;
5626 #if _MSVCR_VER >= 140
5627 /*********************************************************************
5628 * _get_stream_buffer_pointers (UCRTBASE.@)
5630 int CDECL _get_stream_buffer_pointers(FILE *file, char*** base,
5631 char*** ptr, int** count)
5633 if (base)
5634 *base = &file->_base;
5635 if (ptr)
5636 *ptr = &file->_ptr;
5637 if (count)
5638 *count = &file->_cnt;
5639 return 0;
5641 #endif