kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / msvcrt / file.c
blobe06bb0076d674366f15e9ea94a9df4ed0992c881
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 "config.h"
30 #include "wine/port.h"
32 #include <stdarg.h>
33 #include <stdio.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <sys/types.h>
38 #include <limits.h>
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winternl.h"
43 #include "msvcrt.h"
44 #include "mtdll.h"
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
62 #define WX_OPEN 0x01
63 #define WX_ATEOF 0x02
64 #define WX_READNL 0x04 /* read started with \n */
65 #define WX_PIPE 0x08
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
68 #define WX_TTY 0x40
69 #define WX_TEXT 0x80
71 /* values for exflag - it's used differently in msvcr90.dll*/
72 #define EF_UTF8 0x01
73 #define EF_UTF16 0x02
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
87 typedef struct {
88 HANDLE handle;
89 unsigned char wxflag;
90 char lookahead[3];
91 int exflag;
92 CRITICAL_SECTION crit;
93 #if _MSVCR_VER >= 80
94 char textmode : 7;
95 char unicode : 1;
96 char pipech2[2];
97 __int64 startpos;
98 BOOL utf8translations;
99 #endif
100 #if _MSVCR_VER >= 90
101 char dbcsBuffer;
102 BOOL dbcsBufferUsed;
103 #endif
104 } ioinfo;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
117 typedef struct {
118 MSVCRT_FILE file;
119 CRITICAL_SECTION crit;
120 } file_crit;
122 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
123 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
124 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
126 /* INTERNAL: process umask */
127 static int MSVCRT_umask = 0;
129 /* INTERNAL: static data for tmpnam and _wtmpname functions */
130 static int tmpnam_unique;
131 static int tmpnam_s_unique;
133 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
134 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
135 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
136 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
138 #define TOUL(x) (ULONGLONG)(x)
139 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
140 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
141 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
142 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
144 /* This critical section protects the MSVCRT_fstreams table
145 * and MSVCRT_stream_idx from race conditions. It also
146 * protects fd critical sections creation code.
148 static CRITICAL_SECTION MSVCRT_file_cs;
149 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
151 0, 0, &MSVCRT_file_cs,
152 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
153 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
155 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
156 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
157 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
159 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
161 buf->st_dev = buf64->st_dev;
162 buf->st_ino = buf64->st_ino;
163 buf->st_mode = buf64->st_mode;
164 buf->st_nlink = buf64->st_nlink;
165 buf->st_uid = buf64->st_uid;
166 buf->st_gid = buf64->st_gid;
167 buf->st_rdev = buf64->st_rdev;
168 buf->st_size = buf64->st_size;
169 buf->st_atime = buf64->st_atime;
170 buf->st_mtime = buf64->st_mtime;
171 buf->st_ctime = buf64->st_ctime;
174 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
176 buf->st_dev = buf64->st_dev;
177 buf->st_ino = buf64->st_ino;
178 buf->st_mode = buf64->st_mode;
179 buf->st_nlink = buf64->st_nlink;
180 buf->st_uid = buf64->st_uid;
181 buf->st_gid = buf64->st_gid;
182 buf->st_rdev = buf64->st_rdev;
183 buf->st_size = buf64->st_size;
184 buf->st_atime = buf64->st_atime;
185 buf->st_mtime = buf64->st_mtime;
186 buf->st_ctime = buf64->st_ctime;
189 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32 *buf)
191 buf->st_dev = buf64->st_dev;
192 buf->st_ino = buf64->st_ino;
193 buf->st_mode = buf64->st_mode;
194 buf->st_nlink = buf64->st_nlink;
195 buf->st_uid = buf64->st_uid;
196 buf->st_gid = buf64->st_gid;
197 buf->st_rdev = buf64->st_rdev;
198 buf->st_size = buf64->st_size;
199 buf->st_atime = buf64->st_atime;
200 buf->st_mtime = buf64->st_mtime;
201 buf->st_ctime = buf64->st_ctime;
204 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat64i32 *buf)
206 buf->st_dev = buf64->st_dev;
207 buf->st_ino = buf64->st_ino;
208 buf->st_mode = buf64->st_mode;
209 buf->st_nlink = buf64->st_nlink;
210 buf->st_uid = buf64->st_uid;
211 buf->st_gid = buf64->st_gid;
212 buf->st_rdev = buf64->st_rdev;
213 buf->st_size = buf64->st_size;
214 buf->st_atime = buf64->st_atime;
215 buf->st_mtime = buf64->st_mtime;
216 buf->st_ctime = buf64->st_ctime;
219 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32i64 *buf)
221 buf->st_dev = buf64->st_dev;
222 buf->st_ino = buf64->st_ino;
223 buf->st_mode = buf64->st_mode;
224 buf->st_nlink = buf64->st_nlink;
225 buf->st_uid = buf64->st_uid;
226 buf->st_gid = buf64->st_gid;
227 buf->st_rdev = buf64->st_rdev;
228 buf->st_size = buf64->st_size;
229 buf->st_atime = buf64->st_atime;
230 buf->st_mtime = buf64->st_mtime;
231 buf->st_ctime = buf64->st_ctime;
234 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
236 /* 1601 to 1970 is 369 years plus 89 leap days */
237 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
239 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
240 ft->dwHighDateTime = ticks >> 32;
241 ft->dwLowDateTime = ticks;
244 static inline ioinfo* get_ioinfo_nolock(int fd)
246 ioinfo *ret = NULL;
247 if(fd>=0 && fd<MSVCRT_MAX_FILES)
248 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
249 if(!ret)
250 return &MSVCRT___badioinfo;
252 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
255 static inline void init_ioinfo_cs(ioinfo *info)
257 if(!(info->exflag & EF_CRIT_INIT)) {
258 LOCK_FILES();
259 if(!(info->exflag & EF_CRIT_INIT)) {
260 InitializeCriticalSection(&info->crit);
261 info->exflag |= EF_CRIT_INIT;
263 UNLOCK_FILES();
267 static inline ioinfo* get_ioinfo(int fd)
269 ioinfo *ret = get_ioinfo_nolock(fd);
270 if(ret == &MSVCRT___badioinfo)
271 return ret;
272 init_ioinfo_cs(ret);
273 EnterCriticalSection(&ret->crit);
274 return ret;
277 static inline BOOL alloc_pioinfo_block(int fd)
279 ioinfo *block;
280 int i;
282 if(fd<0 || fd>=MSVCRT_MAX_FILES)
284 *MSVCRT__errno() = MSVCRT_ENFILE;
285 return FALSE;
288 block = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
289 if(!block)
291 WARN(":out of memory!\n");
292 *MSVCRT__errno() = MSVCRT_ENOMEM;
293 return FALSE;
295 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
296 block[i].handle = INVALID_HANDLE_VALUE;
297 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE], block, NULL))
298 MSVCRT_free(block);
299 return TRUE;
302 static inline ioinfo* get_ioinfo_alloc_fd(int fd)
304 ioinfo *ret;
306 ret = get_ioinfo(fd);
307 if(ret != &MSVCRT___badioinfo)
308 return ret;
310 if(!alloc_pioinfo_block(fd))
311 return &MSVCRT___badioinfo;
313 return get_ioinfo(fd);
316 static inline ioinfo* get_ioinfo_alloc(int *fd)
318 int i;
320 *fd = -1;
321 for(i=0; i<MSVCRT_MAX_FILES; i++)
323 ioinfo *info = get_ioinfo_nolock(i);
325 if(info == &MSVCRT___badioinfo)
327 if(!alloc_pioinfo_block(i))
328 return &MSVCRT___badioinfo;
329 info = get_ioinfo_nolock(i);
332 init_ioinfo_cs(info);
333 if(TryEnterCriticalSection(&info->crit))
335 if(info->handle == INVALID_HANDLE_VALUE)
337 *fd = i;
338 return info;
340 LeaveCriticalSection(&info->crit);
344 WARN(":files exhausted!\n");
345 *MSVCRT__errno() = MSVCRT_ENFILE;
346 return &MSVCRT___badioinfo;
349 static inline void release_ioinfo(ioinfo *info)
351 if(info!=&MSVCRT___badioinfo && info->exflag & EF_CRIT_INIT)
352 LeaveCriticalSection(&info->crit);
355 static inline MSVCRT_FILE* msvcrt_get_file(int i)
357 file_crit *ret;
359 if(i >= MSVCRT_max_streams)
360 return NULL;
362 if(i < _IOB_ENTRIES)
363 return &MSVCRT__iob[i];
365 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
366 if(!ret) {
367 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
368 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
369 ERR("out of memory\n");
370 *MSVCRT__errno() = MSVCRT_ENOMEM;
371 return NULL;
374 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
375 } else
376 ret += i%MSVCRT_FD_BLOCK_SIZE;
378 return &ret->file;
381 /* INTERNAL: free a file entry fd */
382 static void msvcrt_free_fd(int fd)
384 ioinfo *fdinfo = get_ioinfo(fd);
386 if(fdinfo != &MSVCRT___badioinfo)
388 fdinfo->handle = INVALID_HANDLE_VALUE;
389 fdinfo->wxflag = 0;
391 TRACE(":fd (%d) freed\n",fd);
393 if (fd < 3)
395 switch (fd)
397 case 0:
398 SetStdHandle(STD_INPUT_HANDLE, 0);
399 break;
400 case 1:
401 SetStdHandle(STD_OUTPUT_HANDLE, 0);
402 break;
403 case 2:
404 SetStdHandle(STD_ERROR_HANDLE, 0);
405 break;
408 release_ioinfo(fdinfo);
411 static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag)
413 fdinfo->handle = hand;
414 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_TTY));
415 fdinfo->lookahead[0] = '\n';
416 fdinfo->lookahead[1] = '\n';
417 fdinfo->lookahead[2] = '\n';
418 fdinfo->exflag &= EF_CRIT_INIT;
420 switch (fdinfo-MSVCRT___pioinfo[0])
422 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
423 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
424 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
428 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
429 static int msvcrt_alloc_fd(HANDLE hand, int flag)
431 int fd;
432 ioinfo *info = get_ioinfo_alloc(&fd);
434 TRACE(":handle (%p) allocating fd (%d)\n", hand, fd);
436 if(info == &MSVCRT___badioinfo)
437 return -1;
439 msvcrt_set_fd(info, hand, flag);
440 release_ioinfo(info);
441 return fd;
444 /* INTERNAL: Allocate a FILE* for an fd slot */
445 /* caller must hold the files lock */
446 static MSVCRT_FILE* msvcrt_alloc_fp(void)
448 int i;
449 MSVCRT_FILE *file;
451 for (i = 3; i < MSVCRT_max_streams; i++)
453 file = msvcrt_get_file(i);
454 if (!file)
455 return NULL;
457 if (file->_flag == 0)
459 if (i == MSVCRT_stream_idx)
461 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
463 InitializeCriticalSection(&((file_crit*)file)->crit);
464 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
466 MSVCRT_stream_idx++;
468 return file;
472 return NULL;
475 /* INTERNAL: initialize a FILE* from an open fd */
476 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
478 TRACE(":fd (%d) allocating FILE*\n",fd);
479 if (!(get_ioinfo_nolock(fd)->wxflag & WX_OPEN))
481 WARN(":invalid fd %d\n",fd);
482 *MSVCRT___doserrno() = 0;
483 *MSVCRT__errno() = MSVCRT_EBADF;
484 return -1;
486 file->_ptr = file->_base = NULL;
487 file->_cnt = 0;
488 file->_file = fd;
489 file->_flag = stream_flags;
490 file->_tmpfname = NULL;
492 TRACE(":got FILE* (%p)\n",file);
493 return 0;
496 /* INTERNAL: Create an inheritance data block (for spawned process)
497 * The inheritance block is made of:
498 * 00 int nb of file descriptor (NBFD)
499 * 04 char file flags (wxflag): repeated for each fd
500 * 4+NBFD HANDLE file handle: repeated for each fd
502 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
504 int fd, last_fd;
505 char* wxflag_ptr;
506 HANDLE* handle_ptr;
507 ioinfo* fdinfo;
509 for (last_fd=MSVCRT_MAX_FILES-1; last_fd>=0; last_fd--)
510 if (get_ioinfo_nolock(last_fd)->handle != INVALID_HANDLE_VALUE)
511 break;
512 last_fd++;
514 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * last_fd;
515 *block = MSVCRT_calloc(1, *size);
516 if (!*block)
518 *size = 0;
519 return FALSE;
521 wxflag_ptr = (char*)*block + sizeof(unsigned);
522 handle_ptr = (HANDLE*)(wxflag_ptr + last_fd);
524 *(unsigned*)*block = last_fd;
525 for (fd = 0; fd < last_fd; fd++)
527 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
528 fdinfo = get_ioinfo(fd);
529 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
531 *wxflag_ptr = fdinfo->wxflag;
532 *handle_ptr = fdinfo->handle;
534 else
536 *wxflag_ptr = 0;
537 *handle_ptr = INVALID_HANDLE_VALUE;
539 release_ioinfo(fdinfo);
540 wxflag_ptr++; handle_ptr++;
542 return TRUE;
545 /* INTERNAL: Set up all file descriptors,
546 * as well as default streams (stdin, stderr and stdout)
548 void msvcrt_init_io(void)
550 STARTUPINFOA si;
551 int i;
552 ioinfo *fdinfo;
554 GetStartupInfoA(&si);
555 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
557 BYTE* wxflag_ptr;
558 HANDLE* handle_ptr;
559 unsigned int count;
561 count = *(unsigned*)si.lpReserved2;
562 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
563 handle_ptr = (HANDLE*)(wxflag_ptr + count);
565 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
566 count = min(count, MSVCRT_MAX_FILES);
567 for (i = 0; i < count; i++)
569 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
571 fdinfo = get_ioinfo_alloc_fd(i);
572 if(fdinfo != &MSVCRT___badioinfo)
573 msvcrt_set_fd(fdinfo, *handle_ptr, *wxflag_ptr);
574 release_ioinfo(fdinfo);
577 wxflag_ptr++; handle_ptr++;
581 fdinfo = get_ioinfo_alloc_fd(MSVCRT_STDIN_FILENO);
582 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
583 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
584 DWORD flags = WX_OPEN | WX_TEXT;
585 DWORD type = GetFileType(h);
587 if (type == FILE_TYPE_UNKNOWN) {
588 h = MSVCRT_NO_CONSOLE;
589 flags |= WX_TTY;
590 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
591 flags |= WX_TTY;
592 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
593 flags |= WX_PIPE;
596 msvcrt_set_fd(fdinfo, h, flags);
598 release_ioinfo(fdinfo);
600 fdinfo = get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO);
601 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
602 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
603 DWORD flags = WX_OPEN | WX_TEXT;
604 DWORD type = GetFileType(h);
606 if (type == FILE_TYPE_UNKNOWN) {
607 h = MSVCRT_NO_CONSOLE;
608 flags |= WX_TTY;
609 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
610 flags |= WX_TTY;
611 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
612 flags |= WX_PIPE;
615 msvcrt_set_fd(fdinfo, h, flags);
617 release_ioinfo(fdinfo);
619 fdinfo = get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO);
620 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
621 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
622 DWORD flags = WX_OPEN | WX_TEXT;
623 DWORD type = GetFileType(h);
625 if (type == FILE_TYPE_UNKNOWN) {
626 h = MSVCRT_NO_CONSOLE;
627 flags |= WX_TTY;
628 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
629 flags |= WX_TTY;
630 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
631 flags |= WX_PIPE;
634 msvcrt_set_fd(fdinfo, h, flags);
636 release_ioinfo(fdinfo);
638 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO)->handle,
639 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO)->handle,
640 get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle);
642 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
643 for (i = 0; i < 3; i++)
645 /* FILE structs for stdin/out/err are static and never deleted */
646 MSVCRT__iob[i]._file = get_ioinfo_nolock(i)->handle == MSVCRT_NO_CONSOLE ?
647 MSVCRT_NO_CONSOLE_FD : i;
648 MSVCRT__iob[i]._tmpfname = NULL;
649 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
651 MSVCRT_stream_idx = 3;
654 /* INTERNAL: Flush stdio file buffer */
655 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
657 if((file->_flag & (MSVCRT__IOREAD|MSVCRT__IOWRT)) == MSVCRT__IOWRT &&
658 file->_flag & (MSVCRT__IOMYBUF|MSVCRT__USERBUF)) {
659 int cnt=file->_ptr-file->_base;
660 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
661 file->_flag |= MSVCRT__IOERR;
662 return MSVCRT_EOF;
665 if(file->_flag & MSVCRT__IORW)
666 file->_flag &= ~MSVCRT__IOWRT;
669 file->_ptr=file->_base;
670 file->_cnt=0;
671 return 0;
674 /*********************************************************************
675 * _isatty (MSVCRT.@)
677 int CDECL MSVCRT__isatty(int fd)
679 TRACE(":fd (%d)\n",fd);
681 return get_ioinfo_nolock(fd)->wxflag & WX_TTY;
684 /* INTERNAL: Allocate stdio file buffer */
685 static BOOL msvcrt_alloc_buffer(MSVCRT_FILE* file)
687 if((file->_file==MSVCRT_STDOUT_FILENO || file->_file==MSVCRT_STDERR_FILENO)
688 && MSVCRT__isatty(file->_file))
689 return FALSE;
691 file->_base = MSVCRT_calloc(1, MSVCRT_INTERNAL_BUFSIZ);
692 if(file->_base) {
693 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
694 file->_flag |= MSVCRT__IOMYBUF;
695 } else {
696 file->_base = (char*)(&file->_charbuf);
697 file->_bufsiz = 2;
698 file->_flag |= MSVCRT__IONBF;
700 file->_ptr = file->_base;
701 file->_cnt = 0;
702 return TRUE;
705 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
706 static BOOL add_std_buffer(MSVCRT_FILE *file)
708 static char buffers[2][MSVCRT_BUFSIZ];
710 if((file->_file!=MSVCRT_STDOUT_FILENO && file->_file!=MSVCRT_STDERR_FILENO)
711 || (file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
712 || !MSVCRT__isatty(file->_file))
713 return FALSE;
715 file->_ptr = file->_base = buffers[file->_file == MSVCRT_STDOUT_FILENO ? 0 : 1];
716 file->_bufsiz = file->_cnt = MSVCRT_BUFSIZ;
717 file->_flag |= MSVCRT__USERBUF;
718 return TRUE;
721 /* INTERNAL: Removes temporary buffer from stdout or stderr */
722 /* Only call this function when add_std_buffer returned TRUE */
723 static void remove_std_buffer(MSVCRT_FILE *file)
725 msvcrt_flush_buffer(file);
726 file->_ptr = file->_base = NULL;
727 file->_bufsiz = file->_cnt = 0;
728 file->_flag &= ~MSVCRT__USERBUF;
731 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
732 static int msvcrt_int_to_base32(int num, char *str)
734 char *p;
735 int n = num;
736 int digits = 0;
738 while (n != 0)
740 n >>= 5;
741 digits++;
743 p = str + digits;
744 *p = 0;
745 while (--p >= str)
747 *p = (num & 31) + '0';
748 if (*p > '9')
749 *p += ('a' - '0' - 10);
750 num >>= 5;
753 return digits;
756 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
757 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
759 MSVCRT_wchar_t *p;
760 int n = num;
761 int digits = 0;
763 while (n != 0)
765 n >>= 5;
766 digits++;
768 p = str + digits;
769 *p = 0;
770 while (--p >= str)
772 *p = (num & 31) + '0';
773 if (*p > '9')
774 *p += ('a' - '0' - 10);
775 num >>= 5;
778 return digits;
781 /*********************************************************************
782 * __iob_func(MSVCRT.@)
784 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
786 return &MSVCRT__iob[0];
789 /*********************************************************************
790 * __acrt_iob_func(UCRTBASE.@)
792 MSVCRT_FILE * CDECL MSVCRT___acrt_iob_func(unsigned idx)
794 return &MSVCRT__iob[idx];
797 /*********************************************************************
798 * _access (MSVCRT.@)
800 int CDECL MSVCRT__access(const char *filename, int mode)
802 DWORD attr = GetFileAttributesA(filename);
804 TRACE("(%s,%d) %d\n",filename,mode,attr);
806 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
808 msvcrt_set_errno(GetLastError());
809 return -1;
811 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
813 msvcrt_set_errno(ERROR_ACCESS_DENIED);
814 return -1;
816 return 0;
819 /*********************************************************************
820 * _access_s (MSVCRT.@)
822 int CDECL MSVCRT__access_s(const char *filename, int mode)
824 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
825 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
827 if (MSVCRT__access(filename, mode) == -1)
828 return *MSVCRT__errno();
829 return 0;
832 /*********************************************************************
833 * _waccess (MSVCRT.@)
835 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
837 DWORD attr = GetFileAttributesW(filename);
839 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
841 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
843 msvcrt_set_errno(GetLastError());
844 return -1;
846 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
848 msvcrt_set_errno(ERROR_ACCESS_DENIED);
849 return -1;
851 return 0;
854 /*********************************************************************
855 * _waccess_s (MSVCRT.@)
857 int CDECL MSVCRT__waccess_s(const MSVCRT_wchar_t *filename, int mode)
859 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
860 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
862 if (MSVCRT__waccess(filename, mode) == -1)
863 return *MSVCRT__errno();
864 return 0;
867 /*********************************************************************
868 * _chmod (MSVCRT.@)
870 int CDECL MSVCRT__chmod(const char *path, int flags)
872 DWORD oldFlags = GetFileAttributesA(path);
874 if (oldFlags != INVALID_FILE_ATTRIBUTES)
876 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
877 oldFlags | FILE_ATTRIBUTE_READONLY;
879 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
880 return 0;
882 msvcrt_set_errno(GetLastError());
883 return -1;
886 /*********************************************************************
887 * _wchmod (MSVCRT.@)
889 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
891 DWORD oldFlags = GetFileAttributesW(path);
893 if (oldFlags != INVALID_FILE_ATTRIBUTES)
895 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
896 oldFlags | FILE_ATTRIBUTE_READONLY;
898 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
899 return 0;
901 msvcrt_set_errno(GetLastError());
902 return -1;
905 /*********************************************************************
906 * _unlink (MSVCRT.@)
908 int CDECL MSVCRT__unlink(const char *path)
910 TRACE("%s\n",debugstr_a(path));
911 if(DeleteFileA(path))
912 return 0;
913 TRACE("failed (%d)\n",GetLastError());
914 msvcrt_set_errno(GetLastError());
915 return -1;
918 /*********************************************************************
919 * _wunlink (MSVCRT.@)
921 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
923 TRACE("(%s)\n",debugstr_w(path));
924 if(DeleteFileW(path))
925 return 0;
926 TRACE("failed (%d)\n",GetLastError());
927 msvcrt_set_errno(GetLastError());
928 return -1;
931 /*********************************************************************
932 * _commit (MSVCRT.@)
934 int CDECL MSVCRT__commit(int fd)
936 ioinfo *info = get_ioinfo(fd);
937 int ret;
939 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
941 if (info->handle == INVALID_HANDLE_VALUE)
942 ret = -1;
943 else if (!FlushFileBuffers(info->handle))
945 if (GetLastError() == ERROR_INVALID_HANDLE)
947 /* FlushFileBuffers fails for console handles
948 * so we ignore this error.
950 ret = 0;
952 else
954 TRACE(":failed-last error (%d)\n",GetLastError());
955 msvcrt_set_errno(GetLastError());
956 ret = -1;
959 else
961 TRACE(":ok\n");
962 ret = 0;
965 release_ioinfo(info);
966 return ret;
969 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
970 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
972 /* INTERNAL: Flush all stream buffer */
973 static int msvcrt_flush_all_buffers(int mask)
975 int i, num_flushed = 0;
976 MSVCRT_FILE *file;
978 LOCK_FILES();
979 for (i = 0; i < MSVCRT_stream_idx; i++) {
980 file = msvcrt_get_file(i);
982 if (file->_flag)
984 if(file->_flag & mask) {
985 MSVCRT_fflush(file);
986 num_flushed++;
990 UNLOCK_FILES();
992 TRACE(":flushed (%d) handles\n",num_flushed);
993 return num_flushed;
996 /*********************************************************************
997 * _flushall (MSVCRT.@)
999 int CDECL MSVCRT__flushall(void)
1001 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
1004 /*********************************************************************
1005 * fflush (MSVCRT.@)
1007 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
1009 int ret;
1011 if(!file) {
1012 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
1013 ret = 0;
1014 } else {
1015 MSVCRT__lock_file(file);
1016 ret = MSVCRT__fflush_nolock(file);
1017 MSVCRT__unlock_file(file);
1020 return ret;
1023 /*********************************************************************
1024 * _fflush_nolock (MSVCRT.@)
1026 int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file)
1028 int res;
1030 if(!file) {
1031 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
1032 return 0;
1035 res = msvcrt_flush_buffer(file);
1036 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
1037 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
1038 return res;
1041 /*********************************************************************
1042 * _close (MSVCRT.@)
1044 int CDECL MSVCRT__close(int fd)
1046 ioinfo *info = get_ioinfo(fd);
1047 int ret;
1049 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1051 if (fd == MSVCRT_NO_CONSOLE_FD) {
1052 *MSVCRT__errno() = MSVCRT_EBADF;
1053 ret = -1;
1054 } else if (!MSVCRT_CHECK_PMT_ERR(info->wxflag & WX_OPEN, MSVCRT_EBADF)) {
1055 ret = -1;
1056 } else if (fd == MSVCRT_STDOUT_FILENO &&
1057 info->handle == get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle) {
1058 msvcrt_free_fd(fd);
1059 ret = 0;
1060 } else if (fd == MSVCRT_STDERR_FILENO &&
1061 info->handle == get_ioinfo_nolock(MSVCRT_STDOUT_FILENO)->handle) {
1062 msvcrt_free_fd(fd);
1063 ret = 0;
1064 } else {
1065 ret = CloseHandle(info->handle) ? 0 : -1;
1066 msvcrt_free_fd(fd);
1067 if (ret) {
1068 WARN(":failed-last error (%d)\n",GetLastError());
1069 msvcrt_set_errno(GetLastError());
1072 release_ioinfo(info);
1073 return ret;
1076 /*********************************************************************
1077 * _dup2 (MSVCRT.@)
1078 * NOTES
1079 * MSDN isn't clear on this point, but the remarks for _pipe
1080 * indicate file descriptors duplicated with _dup and _dup2 are always
1081 * inheritable.
1083 int CDECL MSVCRT__dup2(int od, int nd)
1085 ioinfo *info_od, *info_nd;
1086 int ret;
1088 TRACE("(od=%d, nd=%d)\n", od, nd);
1090 if (od < nd)
1092 info_od = get_ioinfo(od);
1093 info_nd = get_ioinfo_alloc_fd(nd);
1095 else
1097 info_nd = get_ioinfo_alloc_fd(nd);
1098 info_od = get_ioinfo(od);
1101 if (info_nd == &MSVCRT___badioinfo)
1103 ret = -1;
1105 else if (info_od->wxflag & WX_OPEN)
1107 HANDLE handle;
1109 if (DuplicateHandle(GetCurrentProcess(), info_od->handle,
1110 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1112 int wxflag = info_od->wxflag & ~MSVCRT__O_NOINHERIT;
1114 if (info_nd->wxflag & WX_OPEN)
1115 MSVCRT__close(nd);
1117 msvcrt_set_fd(info_nd, handle, wxflag);
1118 /* _dup2 returns 0, not nd, on success */
1119 ret = 0;
1121 else
1123 ret = -1;
1124 msvcrt_set_errno(GetLastError());
1127 else
1129 *MSVCRT__errno() = MSVCRT_EBADF;
1130 ret = -1;
1133 release_ioinfo(info_od);
1134 release_ioinfo(info_nd);
1135 return ret;
1138 /*********************************************************************
1139 * _dup (MSVCRT.@)
1141 int CDECL MSVCRT__dup(int od)
1143 int fd, ret;
1144 ioinfo *info = get_ioinfo_alloc(&fd);
1146 if (MSVCRT__dup2(od, fd) == 0)
1147 ret = fd;
1148 else
1149 ret = -1;
1150 release_ioinfo(info);
1151 return ret;
1154 /*********************************************************************
1155 * _eof (MSVCRT.@)
1157 int CDECL MSVCRT__eof(int fd)
1159 ioinfo *info = get_ioinfo(fd);
1160 DWORD curpos,endpos;
1161 LONG hcurpos,hendpos;
1163 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1165 if (info->handle == INVALID_HANDLE_VALUE)
1167 release_ioinfo(info);
1168 return -1;
1171 if (info->wxflag & WX_ATEOF)
1173 release_ioinfo(info);
1174 return TRUE;
1177 /* Otherwise we do it the hard way */
1178 hcurpos = hendpos = 0;
1179 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1180 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1182 if (curpos == endpos && hcurpos == hendpos)
1184 /* FIXME: shouldn't WX_ATEOF be set here? */
1185 release_ioinfo(info);
1186 return TRUE;
1189 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1190 release_ioinfo(info);
1191 return FALSE;
1194 /*********************************************************************
1195 * _fcloseall (MSVCRT.@)
1197 int CDECL MSVCRT__fcloseall(void)
1199 int num_closed = 0, i;
1200 MSVCRT_FILE *file;
1202 LOCK_FILES();
1203 for (i = 3; i < MSVCRT_stream_idx; i++) {
1204 file = msvcrt_get_file(i);
1206 if (file->_flag && !MSVCRT_fclose(file))
1207 num_closed++;
1209 UNLOCK_FILES();
1211 TRACE(":closed (%d) handles\n",num_closed);
1212 return num_closed;
1215 /* free everything on process exit */
1216 void msvcrt_free_io(void)
1218 unsigned int i;
1219 int j;
1221 MSVCRT__flushall();
1222 MSVCRT__fcloseall();
1224 for(i=0; i<ARRAY_SIZE(MSVCRT___pioinfo); i++)
1226 if(!MSVCRT___pioinfo[i])
1227 continue;
1229 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1231 if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
1232 DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
1234 MSVCRT_free(MSVCRT___pioinfo[i]);
1237 for(j=0; j<MSVCRT_stream_idx; j++)
1239 MSVCRT_FILE *file = msvcrt_get_file(j);
1240 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1242 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1243 DeleteCriticalSection(&((file_crit*)file)->crit);
1247 for(i=0; i<ARRAY_SIZE(MSVCRT_fstream); i++)
1248 MSVCRT_free(MSVCRT_fstream[i]);
1251 /*********************************************************************
1252 * _lseeki64 (MSVCRT.@)
1254 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1256 ioinfo *info = get_ioinfo(fd);
1257 LARGE_INTEGER ofs;
1259 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1261 if (info->handle == INVALID_HANDLE_VALUE)
1263 release_ioinfo(info);
1264 return -1;
1267 if (whence < 0 || whence > 2)
1269 release_ioinfo(info);
1270 *MSVCRT__errno() = MSVCRT_EINVAL;
1271 return -1;
1274 TRACE(":fd (%d) to %s pos %s\n",
1275 fd,wine_dbgstr_longlong(offset),
1276 (whence==MSVCRT_SEEK_SET)?"SEEK_SET":
1277 (whence==MSVCRT_SEEK_CUR)?"SEEK_CUR":
1278 (whence==MSVCRT_SEEK_END)?"SEEK_END":"UNKNOWN");
1280 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1281 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1282 ofs.QuadPart = offset;
1283 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1284 GetLastError() == ERROR_SUCCESS)
1286 info->wxflag &= ~WX_ATEOF;
1287 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1289 release_ioinfo(info);
1290 return ofs.QuadPart;
1292 release_ioinfo(info);
1293 TRACE(":error-last error (%d)\n",GetLastError());
1294 msvcrt_set_errno(GetLastError());
1295 return -1;
1298 /*********************************************************************
1299 * _lseek (MSVCRT.@)
1301 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1303 return MSVCRT__lseeki64(fd, offset, whence);
1306 /*********************************************************************
1307 * _lock_file (MSVCRT.@)
1309 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1311 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1312 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1313 else
1314 EnterCriticalSection(&((file_crit*)file)->crit);
1317 /*********************************************************************
1318 * _unlock_file (MSVCRT.@)
1320 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1322 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1323 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1324 else
1325 LeaveCriticalSection(&((file_crit*)file)->crit);
1328 /*********************************************************************
1329 * _locking (MSVCRT.@)
1331 * This is untested; the underlying LockFile doesn't work yet.
1333 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1335 ioinfo *info = get_ioinfo(fd);
1336 BOOL ret;
1337 DWORD cur_locn;
1339 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1340 if (info->handle == INVALID_HANDLE_VALUE)
1342 release_ioinfo(info);
1343 return -1;
1346 if (mode < 0 || mode > 4)
1348 release_ioinfo(info);
1349 *MSVCRT__errno() = MSVCRT_EINVAL;
1350 return -1;
1353 TRACE(":fd (%d) by 0x%08x mode %s\n",
1354 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1355 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1356 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1357 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1358 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1359 "UNKNOWN");
1361 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1363 release_ioinfo(info);
1364 FIXME ("Seek failed\n");
1365 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1366 return -1;
1368 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1370 int nretry = 10;
1371 ret = 1; /* just to satisfy gcc */
1372 while (nretry--)
1374 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1375 if (ret) break;
1376 Sleep(1);
1379 else if (mode == MSVCRT__LK_UNLCK)
1380 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1381 else
1382 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1383 /* FIXME - what about error settings? */
1384 release_ioinfo(info);
1385 return ret ? 0 : -1;
1388 /*********************************************************************
1389 * _fseeki64 (MSVCRT.@)
1391 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1393 int ret;
1395 MSVCRT__lock_file(file);
1396 ret = MSVCRT__fseeki64_nolock(file, offset, whence);
1397 MSVCRT__unlock_file(file);
1399 return ret;
1402 /*********************************************************************
1403 * _fseeki64_nolock (MSVCRT.@)
1405 int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
1407 int ret;
1409 if(whence == MSVCRT_SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1410 whence = MSVCRT_SEEK_SET;
1411 offset += MSVCRT__ftelli64_nolock(file);
1414 /* Flush output if needed */
1415 msvcrt_flush_buffer(file);
1416 /* Reset direction of i/o */
1417 if(file->_flag & MSVCRT__IORW) {
1418 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1420 /* Clear end of file flag */
1421 file->_flag &= ~MSVCRT__IOEOF;
1422 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1424 return ret;
1427 /*********************************************************************
1428 * fseek (MSVCRT.@)
1430 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1432 return MSVCRT__fseeki64( file, offset, whence );
1435 /*********************************************************************
1436 * _fseek_nolock (MSVCRT.@)
1438 int CDECL MSVCRT__fseek_nolock(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1440 return MSVCRT__fseeki64_nolock( file, offset, whence );
1443 /*********************************************************************
1444 * _chsize_s (MSVCRT.@)
1446 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1448 ioinfo *info;
1449 __int64 cur, pos;
1450 BOOL ret = FALSE;
1452 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1454 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1457 info = get_ioinfo(fd);
1458 if (info->handle != INVALID_HANDLE_VALUE)
1460 /* save the current file pointer */
1461 cur = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1462 if (cur >= 0)
1464 pos = MSVCRT__lseeki64(fd, size, MSVCRT_SEEK_SET);
1465 if (pos >= 0)
1467 ret = SetEndOfFile(info->handle);
1468 if (!ret) msvcrt_set_errno(GetLastError());
1471 /* restore the file pointer */
1472 MSVCRT__lseeki64(fd, cur, MSVCRT_SEEK_SET);
1476 release_ioinfo(info);
1477 return ret ? 0 : *MSVCRT__errno();
1480 /*********************************************************************
1481 * _chsize (MSVCRT.@)
1483 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1485 /* _chsize_s returns errno on failure but _chsize should return -1 */
1486 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1489 /*********************************************************************
1490 * clearerr (MSVCRT.@)
1492 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1494 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1496 MSVCRT__lock_file(file);
1497 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1498 MSVCRT__unlock_file(file);
1501 /*********************************************************************
1502 * clearerr_s (MSVCRT.@)
1504 int CDECL MSVCRT_clearerr_s(MSVCRT_FILE* file)
1506 TRACE(":file (%p)\n",file);
1508 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
1510 MSVCRT__lock_file(file);
1511 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1512 MSVCRT__unlock_file(file);
1513 return 0;
1516 /*********************************************************************
1517 * rewind (MSVCRT.@)
1519 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1521 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1523 MSVCRT__lock_file(file);
1524 MSVCRT__fseek_nolock(file, 0L, MSVCRT_SEEK_SET);
1525 MSVCRT_clearerr(file);
1526 MSVCRT__unlock_file(file);
1529 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1531 int plus = strchrW(mode, '+') != NULL;
1533 TRACE("%s\n", debugstr_w(mode));
1535 while(*mode == ' ') mode++;
1537 switch(*mode++)
1539 case 'R': case 'r':
1540 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1541 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1542 break;
1543 case 'W': case 'w':
1544 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1545 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1546 break;
1547 case 'A': case 'a':
1548 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1549 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1550 break;
1551 default:
1552 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1553 return -1;
1556 *stream_flags |= MSVCRT__commode;
1558 while (*mode && *mode!=',')
1559 switch (*mode++)
1561 case 'B': case 'b':
1562 *open_flags |= MSVCRT__O_BINARY;
1563 *open_flags &= ~MSVCRT__O_TEXT;
1564 break;
1565 case 't':
1566 *open_flags |= MSVCRT__O_TEXT;
1567 *open_flags &= ~MSVCRT__O_BINARY;
1568 break;
1569 case 'D':
1570 *open_flags |= MSVCRT__O_TEMPORARY;
1571 break;
1572 case 'T':
1573 *open_flags |= MSVCRT__O_SHORT_LIVED;
1574 break;
1575 case 'c':
1576 *stream_flags |= MSVCRT__IOCOMMIT;
1577 break;
1578 case 'n':
1579 *stream_flags &= ~MSVCRT__IOCOMMIT;
1580 break;
1581 case 'N':
1582 *open_flags |= MSVCRT__O_NOINHERIT;
1583 break;
1584 case '+':
1585 case ' ':
1586 case 'a':
1587 case 'w':
1588 break;
1589 case 'S':
1590 case 'R':
1591 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1592 break;
1593 default:
1594 ERR("incorrect mode flag: %c\n", mode[-1]);
1595 break;
1598 if(*mode == ',')
1600 static const WCHAR ccs[] = {'c','c','s'};
1601 static const WCHAR utf8[] = {'u','t','f','-','8'};
1602 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1603 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1605 mode++;
1606 while(*mode == ' ') mode++;
1607 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, ARRAY_SIZE(ccs))))
1608 return -1;
1609 mode += ARRAY_SIZE(ccs);
1610 while(*mode == ' ') mode++;
1611 if(!MSVCRT_CHECK_PMT(*mode == '='))
1612 return -1;
1613 mode++;
1614 while(*mode == ' ') mode++;
1616 if(!strncmpiW(utf8, mode, ARRAY_SIZE(utf8)))
1618 *open_flags |= MSVCRT__O_U8TEXT;
1619 mode += ARRAY_SIZE(utf8);
1621 else if(!strncmpiW(utf16le, mode, ARRAY_SIZE(utf16le)))
1623 *open_flags |= MSVCRT__O_U16TEXT;
1624 mode += ARRAY_SIZE(utf16le);
1626 else if(!strncmpiW(unicode, mode, ARRAY_SIZE(unicode)))
1628 *open_flags |= MSVCRT__O_WTEXT;
1629 mode += ARRAY_SIZE(unicode);
1631 else
1633 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1634 return -1;
1637 while(*mode == ' ') mode++;
1640 if(!MSVCRT_CHECK_PMT(*mode == 0))
1641 return -1;
1642 return 0;
1645 /*********************************************************************
1646 * _fdopen (MSVCRT.@)
1648 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1650 MSVCRT_FILE *ret;
1651 MSVCRT_wchar_t *modeW = NULL;
1653 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1655 ret = MSVCRT__wfdopen(fd, modeW);
1657 MSVCRT_free(modeW);
1658 return ret;
1661 /*********************************************************************
1662 * _wfdopen (MSVCRT.@)
1664 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1666 int open_flags, stream_flags;
1667 MSVCRT_FILE* file;
1669 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1671 LOCK_FILES();
1672 if (!(file = msvcrt_alloc_fp()))
1673 file = NULL;
1674 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1676 file->_flag = 0;
1677 file = NULL;
1679 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1680 UNLOCK_FILES();
1682 return file;
1685 /*********************************************************************
1686 * _filelength (MSVCRT.@)
1688 LONG CDECL MSVCRT__filelength(int fd)
1690 LONG curPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
1691 if (curPos != -1)
1693 LONG endPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_END);
1694 if (endPos != -1)
1696 if (endPos != curPos)
1697 MSVCRT__lseek(fd, curPos, MSVCRT_SEEK_SET);
1698 return endPos;
1701 return -1;
1704 /*********************************************************************
1705 * _filelengthi64 (MSVCRT.@)
1707 __int64 CDECL MSVCRT__filelengthi64(int fd)
1709 __int64 curPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1710 if (curPos != -1)
1712 __int64 endPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_END);
1713 if (endPos != -1)
1715 if (endPos != curPos)
1716 MSVCRT__lseeki64(fd, curPos, MSVCRT_SEEK_SET);
1717 return endPos;
1720 return -1;
1723 /*********************************************************************
1724 * _fileno (MSVCRT.@)
1726 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1728 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1729 return file->_file;
1732 /*********************************************************************
1733 * _fstat64 (MSVCRT.@)
1735 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1737 ioinfo *info = get_ioinfo(fd);
1738 DWORD dw;
1739 DWORD type;
1740 BY_HANDLE_FILE_INFORMATION hfi;
1742 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1743 if (info->handle == INVALID_HANDLE_VALUE)
1745 release_ioinfo(info);
1746 return -1;
1749 if (!buf)
1751 WARN(":failed-NULL buf\n");
1752 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1753 release_ioinfo(info);
1754 return -1;
1757 memset(&hfi, 0, sizeof(hfi));
1758 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1759 type = GetFileType(info->handle);
1760 if (type == FILE_TYPE_PIPE)
1762 buf->st_dev = buf->st_rdev = fd;
1763 buf->st_mode = MSVCRT__S_IFIFO;
1764 buf->st_nlink = 1;
1766 else if (type == FILE_TYPE_CHAR)
1768 buf->st_dev = buf->st_rdev = fd;
1769 buf->st_mode = MSVCRT__S_IFCHR;
1770 buf->st_nlink = 1;
1772 else /* FILE_TYPE_DISK etc. */
1774 if (!GetFileInformationByHandle(info->handle, &hfi))
1776 WARN(":failed-last error (%d)\n",GetLastError());
1777 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1778 release_ioinfo(info);
1779 return -1;
1781 buf->st_mode = MSVCRT__S_IFREG | 0444;
1782 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1783 buf->st_mode |= 0222;
1784 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1785 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1786 buf->st_atime = dw;
1787 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1788 buf->st_mtime = buf->st_ctime = dw;
1789 buf->st_nlink = hfi.nNumberOfLinks;
1791 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1792 buf->st_mode);
1793 release_ioinfo(info);
1794 return 0;
1797 /*********************************************************************
1798 * _fstati64 (MSVCRT.@)
1800 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1802 int ret;
1803 struct MSVCRT__stat64 buf64;
1805 ret = MSVCRT__fstat64(fd, &buf64);
1806 if (!ret)
1807 msvcrt_stat64_to_stati64(&buf64, buf);
1808 return ret;
1811 /*********************************************************************
1812 * _fstat (MSVCRT.@)
1814 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1815 { int ret;
1816 struct MSVCRT__stat64 buf64;
1818 ret = MSVCRT__fstat64(fd, &buf64);
1819 if (!ret)
1820 msvcrt_stat64_to_stat(&buf64, buf);
1821 return ret;
1824 /*********************************************************************
1825 * _fstat32 (MSVCR80.@)
1827 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1829 int ret;
1830 struct MSVCRT__stat64 buf64;
1832 ret = MSVCRT__fstat64(fd, &buf64);
1833 if (!ret)
1834 msvcrt_stat64_to_stat32(&buf64, buf);
1835 return ret;
1838 /*********************************************************************
1839 * _fstat32i64 (MSVCR80.@)
1841 int CDECL MSVCRT__fstat32i64(int fd, struct MSVCRT__stat32i64* buf)
1843 int ret;
1844 struct MSVCRT__stat64 buf64;
1846 ret = MSVCRT__fstat64(fd, &buf64);
1847 if (!ret)
1848 msvcrt_stat64_to_stat32i64(&buf64, buf);
1849 return ret;
1852 /*********************************************************************
1853 * _fstat64i32 (MSVCR80.@)
1855 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1857 int ret;
1858 struct MSVCRT__stat64 buf64;
1860 ret = MSVCRT__fstat64(fd, &buf64);
1861 if (!ret)
1862 msvcrt_stat64_to_stat64i32(&buf64, buf);
1863 return ret;
1866 /*********************************************************************
1867 * _futime64 (MSVCRT.@)
1869 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1871 ioinfo *info = get_ioinfo(fd);
1872 FILETIME at, wt;
1874 if (!t)
1876 time_to_filetime( MSVCRT__time64(NULL), &at );
1877 wt = at;
1879 else
1881 time_to_filetime( t->actime, &at );
1882 time_to_filetime( t->modtime, &wt );
1885 if (!SetFileTime(info->handle, NULL, &at, &wt))
1887 release_ioinfo(info);
1888 msvcrt_set_errno(GetLastError());
1889 return -1 ;
1891 release_ioinfo(info);
1892 return 0;
1895 /*********************************************************************
1896 * _futime32 (MSVCRT.@)
1898 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1900 if (t)
1902 struct MSVCRT___utimbuf64 t64;
1903 t64.actime = t->actime;
1904 t64.modtime = t->modtime;
1905 return _futime64( fd, &t64 );
1907 else
1908 return _futime64( fd, NULL );
1911 /*********************************************************************
1912 * _futime (MSVCRT.@)
1914 #ifdef _WIN64
1915 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1917 return _futime64( fd, t );
1919 #else
1920 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1922 return _futime32( fd, t );
1924 #endif
1926 /*********************************************************************
1927 * _get_osfhandle (MSVCRT.@)
1929 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1931 HANDLE hand = get_ioinfo_nolock(fd)->handle;
1932 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1934 if(hand == INVALID_HANDLE_VALUE)
1935 *MSVCRT__errno() = MSVCRT_EBADF;
1936 return (MSVCRT_intptr_t)hand;
1939 /*********************************************************************
1940 * _mktemp_s (MSVCRT.@)
1942 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1944 DWORD len, xno, id;
1946 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1947 return MSVCRT_EINVAL;
1949 for(len=0; len<size; len++)
1950 if(!pattern[len])
1951 break;
1952 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1953 if(size)
1954 pattern[0] = 0;
1955 return MSVCRT_EINVAL;
1958 for(xno=1; xno<=6; xno++)
1959 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1960 return MSVCRT_EINVAL;
1962 id = GetCurrentProcessId();
1963 for(xno=1; xno<6; xno++) {
1964 pattern[len-xno] = id%10 + '0';
1965 id /= 10;
1968 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1969 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1970 return 0;
1973 pattern[0] = 0;
1974 *MSVCRT__errno() = MSVCRT_EEXIST;
1975 return MSVCRT_EEXIST;
1978 /*********************************************************************
1979 * _mktemp (MSVCRT.@)
1981 char * CDECL MSVCRT__mktemp(char *pattern)
1983 int numX = 0;
1984 char *retVal = pattern;
1985 int id;
1986 char letter = 'a';
1988 if(!pattern)
1989 return NULL;
1991 while(*pattern)
1992 numX = (*pattern++ == 'X')? numX + 1 : 0;
1993 if (numX < 6)
1994 return NULL;
1995 pattern--;
1996 id = GetCurrentProcessId();
1997 numX = 6;
1998 while(numX--)
2000 int tempNum = id / 10;
2001 *pattern-- = id - (tempNum * 10) + '0';
2002 id = tempNum;
2004 pattern++;
2007 *pattern = letter++;
2008 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
2009 return retVal;
2010 } while(letter <= 'z');
2011 return NULL;
2014 /*********************************************************************
2015 * _wmktemp_s (MSVCRT.@)
2017 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
2019 DWORD len, xno, id;
2021 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
2022 return MSVCRT_EINVAL;
2024 for(len=0; len<size; len++)
2025 if(!pattern[len])
2026 break;
2027 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
2028 if(size)
2029 pattern[0] = 0;
2030 return MSVCRT_EINVAL;
2033 for(xno=1; xno<=6; xno++)
2034 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
2035 return MSVCRT_EINVAL;
2037 id = GetCurrentProcessId();
2038 for(xno=1; xno<6; xno++) {
2039 pattern[len-xno] = id%10 + '0';
2040 id /= 10;
2043 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
2044 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
2045 return 0;
2048 pattern[0] = 0;
2049 *MSVCRT__errno() = MSVCRT_EEXIST;
2050 return MSVCRT_EEXIST;
2053 /*********************************************************************
2054 * _wmktemp (MSVCRT.@)
2056 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
2058 int numX = 0;
2059 MSVCRT_wchar_t *retVal = pattern;
2060 int id;
2061 MSVCRT_wchar_t letter = 'a';
2063 if(!pattern)
2064 return NULL;
2066 while(*pattern)
2067 numX = (*pattern++ == 'X')? numX + 1 : 0;
2068 if (numX < 6)
2069 return NULL;
2070 pattern--;
2071 id = GetCurrentProcessId();
2072 numX = 6;
2073 while(numX--)
2075 int tempNum = id / 10;
2076 *pattern-- = id - (tempNum * 10) + '0';
2077 id = tempNum;
2079 pattern++;
2082 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
2083 return retVal;
2084 *pattern = letter++;
2085 } while(letter != '|');
2086 return NULL;
2089 static unsigned split_oflags(unsigned oflags)
2091 int wxflags = 0;
2092 unsigned unsupp; /* until we support everything */
2094 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
2095 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
2096 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
2097 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
2098 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
2099 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
2100 else if (*MSVCRT___p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
2101 else wxflags |= WX_TEXT; /* default to TEXT*/
2102 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2104 if ((unsupp = oflags & ~(
2105 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
2106 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
2107 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
2108 MSVCRT__O_NOINHERIT|
2109 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
2110 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
2112 ERR(":unsupported oflags 0x%04x\n",unsupp);
2114 return wxflags;
2117 /*********************************************************************
2118 * _pipe (MSVCRT.@)
2120 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
2122 int ret = -1;
2123 SECURITY_ATTRIBUTES sa;
2124 HANDLE readHandle, writeHandle;
2126 if (!pfds)
2128 *MSVCRT__errno() = MSVCRT_EINVAL;
2129 return -1;
2132 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2133 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
2134 sa.lpSecurityDescriptor = NULL;
2135 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2137 unsigned int wxflags = split_oflags(textmode);
2138 int fd;
2140 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2141 if (fd != -1)
2143 pfds[0] = fd;
2144 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2145 if (fd != -1)
2147 pfds[1] = fd;
2148 ret = 0;
2150 else
2152 MSVCRT__close(pfds[0]);
2153 CloseHandle(writeHandle);
2154 *MSVCRT__errno() = MSVCRT_EMFILE;
2157 else
2159 CloseHandle(readHandle);
2160 CloseHandle(writeHandle);
2161 *MSVCRT__errno() = MSVCRT_EMFILE;
2164 else
2165 msvcrt_set_errno(GetLastError());
2167 return ret;
2170 static int check_bom(HANDLE h, int oflags, BOOL seek)
2172 char bom[sizeof(utf8_bom)];
2173 DWORD r;
2175 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2177 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2178 return oflags;
2180 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2181 oflags |= MSVCRT__O_U8TEXT;
2182 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2183 if (seek && r>2)
2184 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2185 oflags |= MSVCRT__O_U16TEXT;
2186 }else if (seek) {
2187 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2190 return oflags;
2193 /*********************************************************************
2194 * _wsopen_dispatch (UCRTBASE.@)
2196 int CDECL MSVCRT__wsopen_dispatch( const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode,
2197 int *fd, int secure )
2199 DWORD access = 0, creation = 0, attrib;
2200 SECURITY_ATTRIBUTES sa;
2201 DWORD sharing, type;
2202 int wxflag;
2203 HANDLE hand;
2205 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2206 debugstr_w(path), oflags, shflags, pmode, fd, secure);
2208 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2210 *fd = -1;
2211 wxflag = split_oflags(oflags);
2212 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2214 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2215 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2216 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2219 if (oflags & MSVCRT__O_CREAT)
2221 if (secure && !MSVCRT_CHECK_PMT(!(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))))
2222 return MSVCRT_EINVAL;
2224 if (oflags & MSVCRT__O_EXCL)
2225 creation = CREATE_NEW;
2226 else if (oflags & MSVCRT__O_TRUNC)
2227 creation = CREATE_ALWAYS;
2228 else
2229 creation = OPEN_ALWAYS;
2231 else /* no MSVCRT__O_CREAT */
2233 if (oflags & MSVCRT__O_TRUNC)
2234 creation = TRUNCATE_EXISTING;
2235 else
2236 creation = OPEN_EXISTING;
2239 switch( shflags )
2241 case MSVCRT__SH_DENYRW:
2242 sharing = 0L;
2243 break;
2244 case MSVCRT__SH_DENYWR:
2245 sharing = FILE_SHARE_READ;
2246 break;
2247 case MSVCRT__SH_DENYRD:
2248 sharing = FILE_SHARE_WRITE;
2249 break;
2250 case MSVCRT__SH_DENYNO:
2251 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2252 break;
2253 default:
2254 ERR( "Unhandled shflags 0x%x\n", shflags );
2255 return MSVCRT_EINVAL;
2258 if (!(pmode & ~MSVCRT_umask & MSVCRT__S_IWRITE))
2259 attrib = FILE_ATTRIBUTE_READONLY;
2260 else
2261 attrib = FILE_ATTRIBUTE_NORMAL;
2263 if (oflags & MSVCRT__O_TEMPORARY)
2265 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2266 access |= DELETE;
2267 sharing |= FILE_SHARE_DELETE;
2270 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2271 sa.lpSecurityDescriptor = NULL;
2272 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2274 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2275 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2276 && !(access&GENERIC_READ))
2278 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2279 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2280 if (hand != INVALID_HANDLE_VALUE)
2282 oflags = check_bom(hand, oflags, FALSE);
2283 CloseHandle(hand);
2285 else
2286 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2289 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2290 if (hand == INVALID_HANDLE_VALUE) {
2291 WARN(":failed-last error (%d)\n",GetLastError());
2292 msvcrt_set_errno(GetLastError());
2293 return *MSVCRT__errno();
2296 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2298 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2299 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2300 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2302 if (oflags & MSVCRT__O_U8TEXT)
2304 DWORD written = 0, tmp;
2306 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2307 sizeof(utf8_bom)-written, &tmp, NULL))
2308 written += tmp;
2309 if (written != sizeof(utf8_bom)) {
2310 WARN("error writing BOM\n");
2311 CloseHandle(hand);
2312 msvcrt_set_errno(GetLastError());
2313 return *MSVCRT__errno();
2316 else
2318 DWORD written = 0, tmp;
2320 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2321 sizeof(utf16_bom)-written, &tmp, NULL))
2322 written += tmp;
2323 if (written != sizeof(utf16_bom))
2325 WARN("error writing BOM\n");
2326 CloseHandle(hand);
2327 msvcrt_set_errno(GetLastError());
2328 return *MSVCRT__errno();
2332 else if (access & GENERIC_READ)
2333 oflags = check_bom(hand, oflags, TRUE);
2336 type = GetFileType(hand);
2337 if (type == FILE_TYPE_CHAR)
2338 wxflag |= WX_TTY;
2339 else if (type == FILE_TYPE_PIPE)
2340 wxflag |= WX_PIPE;
2342 *fd = msvcrt_alloc_fd(hand, wxflag);
2343 if (*fd == -1)
2344 return *MSVCRT__errno();
2346 if (oflags & MSVCRT__O_WTEXT)
2347 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2348 else if (oflags & MSVCRT__O_U16TEXT)
2349 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
2350 else if (oflags & MSVCRT__O_U8TEXT)
2351 get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
2353 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2354 return 0;
2358 /*********************************************************************
2359 * _wsopen_s (MSVCRT.@)
2361 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2363 return MSVCRT__wsopen_dispatch( path, oflags, shflags, pmode, fd, 1 );
2366 /*********************************************************************
2367 * _wsopen (MSVCRT.@)
2369 int WINAPIV MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2371 int pmode;
2372 int fd;
2374 if (oflags & MSVCRT__O_CREAT)
2376 __ms_va_list ap;
2378 __ms_va_start(ap, shflags);
2379 pmode = va_arg(ap, int);
2380 __ms_va_end(ap);
2382 else
2383 pmode = 0;
2385 return MSVCRT__wsopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2389 /*********************************************************************
2390 * _sopen_dispatch (UCRTBASE.@)
2392 int CDECL MSVCRT__sopen_dispatch( const char *path, int oflags, int shflags,
2393 int pmode, int *fd, int secure)
2395 MSVCRT_wchar_t *pathW;
2396 int ret;
2398 if (!MSVCRT_CHECK_PMT(fd != NULL))
2399 return MSVCRT_EINVAL;
2400 *fd = -1;
2401 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2402 return MSVCRT_EINVAL;
2404 ret = MSVCRT__wsopen_dispatch(pathW, oflags, shflags, pmode, fd, secure);
2405 MSVCRT_free(pathW);
2406 return ret;
2409 /*********************************************************************
2410 * _sopen_s (MSVCRT.@)
2412 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2414 return MSVCRT__sopen_dispatch(path, oflags, shflags, pmode, fd, 1);
2417 /*********************************************************************
2418 * _sopen (MSVCRT.@)
2420 int WINAPIV MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2422 int pmode;
2423 int fd;
2425 if (oflags & MSVCRT__O_CREAT)
2427 __ms_va_list ap;
2429 __ms_va_start(ap, shflags);
2430 pmode = va_arg(ap, int);
2431 __ms_va_end(ap);
2433 else
2434 pmode = 0;
2436 return MSVCRT__sopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2439 /*********************************************************************
2440 * _open (MSVCRT.@)
2442 int WINAPIV MSVCRT__open( const char *path, int flags, ... )
2444 __ms_va_list ap;
2446 if (flags & MSVCRT__O_CREAT)
2448 int pmode;
2449 __ms_va_start(ap, flags);
2450 pmode = va_arg(ap, int);
2451 __ms_va_end(ap);
2452 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2454 else
2455 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2458 /*********************************************************************
2459 * _wopen (MSVCRT.@)
2461 int WINAPIV MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2463 __ms_va_list ap;
2465 if (flags & MSVCRT__O_CREAT)
2467 int pmode;
2468 __ms_va_start(ap, flags);
2469 pmode = va_arg(ap, int);
2470 __ms_va_end(ap);
2471 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2473 else
2474 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2477 /*********************************************************************
2478 * _creat (MSVCRT.@)
2480 int CDECL MSVCRT__creat(const char *path, int pmode)
2482 int flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | MSVCRT__O_RDWR;
2483 return MSVCRT__open(path, flags, pmode);
2486 /*********************************************************************
2487 * _wcreat (MSVCRT.@)
2489 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int pmode)
2491 int flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | MSVCRT__O_RDWR;
2492 return MSVCRT__wopen(path, flags, pmode);
2495 /*********************************************************************
2496 * _open_osfhandle (MSVCRT.@)
2498 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2500 DWORD flags;
2501 int fd;
2503 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2504 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2505 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2506 * text - it never sets MSVCRT__O_BINARY.
2508 /* don't let split_oflags() decide the mode if no mode is passed */
2509 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2510 oflags |= MSVCRT__O_BINARY;
2512 flags = GetFileType((HANDLE)handle);
2513 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2515 msvcrt_set_errno(GetLastError());
2516 return -1;
2519 if (flags == FILE_TYPE_CHAR)
2520 flags = WX_TTY;
2521 else if (flags == FILE_TYPE_PIPE)
2522 flags = WX_PIPE;
2523 else
2524 flags = 0;
2525 flags |= split_oflags(oflags);
2527 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2528 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2529 return fd;
2532 /*********************************************************************
2533 * _rmtmp (MSVCRT.@)
2535 int CDECL MSVCRT__rmtmp(void)
2537 int num_removed = 0, i;
2538 MSVCRT_FILE *file;
2540 LOCK_FILES();
2541 for (i = 3; i < MSVCRT_stream_idx; i++) {
2542 file = msvcrt_get_file(i);
2544 if (file->_tmpfname)
2546 MSVCRT_fclose(file);
2547 num_removed++;
2550 UNLOCK_FILES();
2552 if (num_removed)
2553 TRACE(":removed (%d) temp files\n",num_removed);
2554 return num_removed;
2557 static inline int get_utf8_char_len(char ch)
2559 if((ch&0xf8) == 0xf0)
2560 return 4;
2561 else if((ch&0xf0) == 0xe0)
2562 return 3;
2563 else if((ch&0xe0) == 0xc0)
2564 return 2;
2565 return 1;
2568 /*********************************************************************
2569 * (internal) read_utf8
2571 static int read_utf8(ioinfo *fdinfo, MSVCRT_wchar_t *buf, unsigned int count)
2573 HANDLE hand = fdinfo->handle;
2574 char min_buf[4], *readbuf, lookahead;
2575 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2577 /* make the buffer big enough to hold at least one character */
2578 /* read bytes have to fit to output and lookahead buffers */
2579 count /= 2;
2580 readbuf_size = count < 4 ? 4 : count;
2581 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2582 readbuf_size = 4;
2583 readbuf = min_buf;
2586 if(fdinfo->lookahead[0] != '\n') {
2587 readbuf[pos++] = fdinfo->lookahead[0];
2588 fdinfo->lookahead[0] = '\n';
2590 if(fdinfo->lookahead[1] != '\n') {
2591 readbuf[pos++] = fdinfo->lookahead[1];
2592 fdinfo->lookahead[1] = '\n';
2594 if(fdinfo->lookahead[2] != '\n') {
2595 readbuf[pos++] = fdinfo->lookahead[2];
2596 fdinfo->lookahead[2] = '\n';
2601 /* NOTE: this case is broken in native dll, reading
2602 * sometimes fails when small buffer is passed
2604 if(count < 4) {
2605 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2606 if (GetLastError() == ERROR_BROKEN_PIPE) {
2607 fdinfo->wxflag |= WX_ATEOF;
2608 return 0;
2609 }else {
2610 msvcrt_set_errno(GetLastError());
2611 return -1;
2613 }else if(!num_read) {
2614 fdinfo->wxflag |= WX_ATEOF;
2615 return 0;
2616 }else {
2617 pos++;
2620 char_len = get_utf8_char_len(readbuf[0]);
2621 if(char_len>pos) {
2622 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2623 pos += num_read;
2626 if(readbuf[0] == '\n')
2627 fdinfo->wxflag |= WX_READNL;
2628 else
2629 fdinfo->wxflag &= ~WX_READNL;
2631 if(readbuf[0] == 0x1a) {
2632 fdinfo->wxflag |= WX_ATEOF;
2633 return 0;
2636 if(readbuf[0] == '\r') {
2637 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2638 buf[0] = '\r';
2639 else if(lookahead == '\n')
2640 buf[0] = '\n';
2641 else {
2642 buf[0] = '\r';
2643 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2644 fdinfo->lookahead[0] = lookahead;
2645 else
2646 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2648 return 2;
2651 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2652 msvcrt_set_errno(GetLastError());
2653 return -1;
2656 return num_read*2;
2659 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2660 if(pos) {
2661 num_read = 0;
2662 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2663 fdinfo->wxflag |= WX_ATEOF;
2664 if (readbuf != min_buf) MSVCRT_free(readbuf);
2665 return 0;
2666 }else {
2667 msvcrt_set_errno(GetLastError());
2668 if (readbuf != min_buf) MSVCRT_free(readbuf);
2669 return -1;
2671 }else if(!pos && !num_read) {
2672 fdinfo->wxflag |= WX_ATEOF;
2673 if (readbuf != min_buf) MSVCRT_free(readbuf);
2674 return 0;
2677 pos += num_read;
2678 if(readbuf[0] == '\n')
2679 fdinfo->wxflag |= WX_READNL;
2680 else
2681 fdinfo->wxflag &= ~WX_READNL;
2683 /* Find first byte of last character (may be incomplete) */
2684 for(i=pos-1; i>0 && i>pos-4; i--)
2685 if((readbuf[i]&0xc0) != 0x80)
2686 break;
2687 char_len = get_utf8_char_len(readbuf[i]);
2688 if(char_len+i <= pos)
2689 i += char_len;
2691 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2692 if(i < pos)
2693 fdinfo->lookahead[0] = readbuf[i];
2694 if(i+1 < pos)
2695 fdinfo->lookahead[1] = readbuf[i+1];
2696 if(i+2 < pos)
2697 fdinfo->lookahead[2] = readbuf[i+2];
2698 }else if(i < pos) {
2699 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2701 pos = i;
2703 for(i=0, j=0; i<pos; i++) {
2704 if(readbuf[i] == 0x1a) {
2705 fdinfo->wxflag |= WX_ATEOF;
2706 break;
2709 /* strip '\r' if followed by '\n' */
2710 if(readbuf[i] == '\r' && i+1==pos) {
2711 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2712 readbuf[j++] = '\r';
2713 }else if(lookahead == '\n' && j==0) {
2714 readbuf[j++] = '\n';
2715 }else {
2716 if(lookahead != '\n')
2717 readbuf[j++] = '\r';
2719 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2720 fdinfo->lookahead[0] = lookahead;
2721 else
2722 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2724 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2725 readbuf[j++] = readbuf[i];
2728 pos = j;
2730 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2731 msvcrt_set_errno(GetLastError());
2732 if (readbuf != min_buf) MSVCRT_free(readbuf);
2733 return -1;
2736 if (readbuf != min_buf) MSVCRT_free(readbuf);
2737 return num_read*2;
2740 /*********************************************************************
2741 * (internal) read_i
2743 * When reading \r as last character in text mode, read() positions
2744 * the file pointer on the \r character while getc() goes on to
2745 * the following \n
2747 static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
2749 DWORD num_read, utf16;
2750 char *bufstart = buf;
2752 if (count == 0)
2753 return 0;
2755 if (fdinfo->wxflag & WX_ATEOF) {
2756 TRACE("already at EOF, returning 0\n");
2757 return 0;
2759 /* Don't trace small reads, it gets *very* annoying */
2760 if (count > 4)
2761 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2762 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2764 *MSVCRT__errno() = MSVCRT_EBADF;
2765 return -1;
2768 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2769 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2771 *MSVCRT__errno() = MSVCRT_EINVAL;
2772 return -1;
2775 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2776 return read_utf8(fdinfo, buf, count);
2778 if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
2780 if (fdinfo->lookahead[0] != '\n')
2782 bufstart[0] = fdinfo->lookahead[0];
2783 fdinfo->lookahead[0] = '\n';
2785 if (utf16)
2787 bufstart[1] = fdinfo->lookahead[1];
2788 fdinfo->lookahead[1] = '\n';
2791 if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2792 num_read += 1+utf16;
2793 else
2794 num_read = 1+utf16;
2797 if(utf16 && (num_read&1))
2799 /* msvcr90 uses uninitialized value from the buffer in this case */
2800 /* msvcrt ignores additional data */
2801 ERR("got odd number of bytes in UTF16 mode\n");
2802 num_read--;
2805 if (count != 0 && num_read == 0)
2807 fdinfo->wxflag |= WX_ATEOF;
2808 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2810 else if (fdinfo->wxflag & WX_TEXT)
2812 DWORD i, j;
2814 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2815 fdinfo->wxflag |= WX_READNL;
2816 else
2817 fdinfo->wxflag &= ~WX_READNL;
2819 for (i=0, j=0; i<num_read; i+=1+utf16)
2821 /* in text mode, a ctrl-z signals EOF */
2822 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2824 fdinfo->wxflag |= WX_ATEOF;
2825 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2826 break;
2829 /* in text mode, strip \r if followed by \n */
2830 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2832 char lookahead[2];
2833 DWORD len;
2835 lookahead[1] = '\n';
2836 if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
2838 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2840 bufstart[j++] = '\n';
2841 if(utf16) bufstart[j++] = 0;
2843 else
2845 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2847 bufstart[j++] = '\r';
2848 if(utf16) bufstart[j++] = 0;
2851 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2853 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2855 bufstart[j++] = '\n';
2856 if (utf16) bufstart[j++] = 0;
2858 else
2860 fdinfo->lookahead[0] = lookahead[0];
2861 fdinfo->lookahead[1] = lookahead[1];
2864 else
2865 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2868 else
2870 bufstart[j++] = '\r';
2871 if(utf16) bufstart[j++] = 0;
2874 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2875 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2877 bufstart[j++] = bufstart[i];
2878 if(utf16) bufstart[j++] = bufstart[i+1];
2881 num_read = j;
2884 else
2886 if (GetLastError() == ERROR_BROKEN_PIPE)
2888 TRACE(":end-of-pipe\n");
2889 fdinfo->wxflag |= WX_ATEOF;
2890 return 0;
2892 else
2894 TRACE(":failed-last error (%d)\n",GetLastError());
2895 msvcrt_set_errno(GetLastError());
2896 return -1;
2900 if (count > 4)
2901 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2902 return num_read;
2905 /*********************************************************************
2906 * _read (MSVCRT.@)
2908 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2910 ioinfo *info;
2911 int num_read;
2913 if(fd == MSVCRT_NO_CONSOLE_FD) {
2914 *MSVCRT__errno() = MSVCRT_EBADF;
2915 return -1;
2918 info = get_ioinfo(fd);
2919 num_read = read_i(fd, info, buf, count);
2920 release_ioinfo(info);
2921 return num_read;
2924 /*********************************************************************
2925 * _setmode (MSVCRT.@)
2927 int CDECL MSVCRT__setmode(int fd,int mode)
2929 ioinfo *info = get_ioinfo(fd);
2930 int ret = info->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2931 if(ret==MSVCRT__O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16)))
2932 ret = MSVCRT__O_WTEXT;
2934 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2935 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2936 *MSVCRT__errno() = MSVCRT_EINVAL;
2937 release_ioinfo(info);
2938 return -1;
2941 if(info == &MSVCRT___badioinfo) {
2942 *MSVCRT__errno() = MSVCRT_EBADF;
2943 return MSVCRT_EOF;
2946 if(mode == MSVCRT__O_BINARY) {
2947 info->wxflag &= ~WX_TEXT;
2948 info->exflag &= ~(EF_UTF8|EF_UTF16);
2949 release_ioinfo(info);
2950 return ret;
2953 info->wxflag |= WX_TEXT;
2954 if(mode == MSVCRT__O_TEXT)
2955 info->exflag &= ~(EF_UTF8|EF_UTF16);
2956 else if(mode == MSVCRT__O_U8TEXT)
2957 info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
2958 else
2959 info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
2961 release_ioinfo(info);
2962 return ret;
2965 /*********************************************************************
2966 * _stat64 (MSVCRT.@)
2968 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2970 DWORD dw;
2971 WIN32_FILE_ATTRIBUTE_DATA hfi;
2972 unsigned short mode = ALL_S_IREAD;
2973 int plen;
2975 TRACE(":file (%s) buf(%p)\n",path,buf);
2977 plen = strlen(path);
2978 while (plen && path[plen-1]==' ')
2979 plen--;
2981 if (plen==2 && path[1]==':')
2983 *MSVCRT__errno() = MSVCRT_ENOENT;
2984 return -1;
2987 #if _MSVCR_VER<140
2988 if (plen>=2 && path[plen-2]!=':' && (path[plen-1]=='\\' || path[plen-1]=='/'))
2990 *MSVCRT__errno() = MSVCRT_ENOENT;
2991 return -1;
2993 #endif
2995 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2997 TRACE("failed (%d)\n",GetLastError());
2998 *MSVCRT__errno() = MSVCRT_ENOENT;
2999 return -1;
3002 memset(buf,0,sizeof(struct MSVCRT__stat64));
3004 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3005 Bon 011120: This FIXME seems incorrect
3006 Also a letter as first char isn't enough to be classified
3007 as a drive letter
3009 if (isalpha(*path)&& (*(path+1)==':'))
3010 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
3011 else
3012 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3014 /* Dir, or regular file? */
3015 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3016 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3017 else
3019 mode |= MSVCRT__S_IFREG;
3020 /* executable? */
3021 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3023 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
3024 (tolower(path[plen-3]) << 16);
3025 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
3026 mode |= ALL_S_IEXEC;
3030 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3031 mode |= ALL_S_IWRITE;
3033 buf->st_mode = mode;
3034 buf->st_nlink = 1;
3035 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3036 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3037 buf->st_atime = dw;
3038 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3039 buf->st_mtime = buf->st_ctime = dw;
3040 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3041 (int)(buf->st_size >> 32),(int)buf->st_size,
3042 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3043 return 0;
3046 /*********************************************************************
3047 * _stati64 (MSVCRT.@)
3049 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
3051 int ret;
3052 struct MSVCRT__stat64 buf64;
3054 ret = MSVCRT_stat64(path, &buf64);
3055 if (!ret)
3056 msvcrt_stat64_to_stati64(&buf64, buf);
3057 return ret;
3060 /*********************************************************************
3061 * _stat (MSVCRT.@)
3063 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
3065 int ret;
3066 struct MSVCRT__stat64 buf64;
3068 ret = MSVCRT_stat64( path, &buf64);
3069 if (!ret)
3070 msvcrt_stat64_to_stat(&buf64, buf);
3071 return ret;
3074 #if _MSVCR_VER >= 80
3076 /*********************************************************************
3077 * _stat32 (MSVCR80.@)
3079 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
3081 int ret;
3082 struct MSVCRT__stat64 buf64;
3084 ret = MSVCRT_stat64(path, &buf64);
3085 if (!ret)
3086 msvcrt_stat64_to_stat32(&buf64, buf);
3087 return ret;
3090 /*********************************************************************
3091 * _stat32i64 (MSVCR80.@)
3093 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
3095 int ret;
3096 struct MSVCRT__stat64 buf64;
3098 ret = MSVCRT_stat64(path, &buf64);
3099 if (!ret)
3100 msvcrt_stat64_to_stat32i64(&buf64, buf);
3101 return ret;
3104 /*********************************************************************
3105 * _stat64i32 (MSVCR80.@)
3107 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
3109 int ret;
3110 struct MSVCRT__stat64 buf64;
3112 ret = MSVCRT_stat64(path, &buf64);
3113 if (!ret)
3114 msvcrt_stat64_to_stat64i32(&buf64, buf);
3115 return ret;
3118 #endif /* _MSVCR_VER >= 80 */
3120 /*********************************************************************
3121 * _wstat64 (MSVCRT.@)
3123 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
3125 DWORD dw;
3126 WIN32_FILE_ATTRIBUTE_DATA hfi;
3127 unsigned short mode = ALL_S_IREAD;
3128 int plen;
3130 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
3132 plen = strlenW(path);
3133 while (plen && path[plen-1]==' ')
3134 plen--;
3136 if (plen==2 && path[1]==':')
3138 *MSVCRT__errno() = MSVCRT_ENOENT;
3139 return -1;
3142 #if _MSVCR_VER<140
3143 if (plen>=2 && path[plen-2]!=':' && (path[plen-1]=='\\' || path[plen-1]=='/'))
3145 *MSVCRT__errno() = MSVCRT_ENOENT;
3146 return -1;
3148 #endif
3150 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
3152 TRACE("failed (%d)\n",GetLastError());
3153 *MSVCRT__errno() = MSVCRT_ENOENT;
3154 return -1;
3157 memset(buf,0,sizeof(struct MSVCRT__stat64));
3159 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3160 if (MSVCRT_iswalpha(*path))
3161 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
3162 else
3163 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3165 /* Dir, or regular file? */
3166 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3167 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3168 else
3170 mode |= MSVCRT__S_IFREG;
3171 /* executable? */
3172 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3174 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
3175 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
3176 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3177 mode |= ALL_S_IEXEC;
3181 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3182 mode |= ALL_S_IWRITE;
3184 buf->st_mode = mode;
3185 buf->st_nlink = 1;
3186 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3187 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3188 buf->st_atime = dw;
3189 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3190 buf->st_mtime = buf->st_ctime = dw;
3191 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3192 (int)(buf->st_size >> 32),(int)buf->st_size,
3193 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3194 return 0;
3197 /*********************************************************************
3198 * _wstati64 (MSVCRT.@)
3200 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
3202 int ret;
3203 struct MSVCRT__stat64 buf64;
3205 ret = MSVCRT__wstat64(path, &buf64);
3206 if (!ret)
3207 msvcrt_stat64_to_stati64(&buf64, buf);
3208 return ret;
3211 /*********************************************************************
3212 * _wstat (MSVCRT.@)
3214 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
3216 int ret;
3217 struct MSVCRT__stat64 buf64;
3219 ret = MSVCRT__wstat64( path, &buf64 );
3220 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3221 return ret;
3224 #if _MSVCR_VER >= 80
3226 /*********************************************************************
3227 * _wstat32 (MSVCR80.@)
3229 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
3231 int ret;
3232 struct MSVCRT__stat64 buf64;
3234 ret = MSVCRT__wstat64(path, &buf64);
3235 if (!ret)
3236 msvcrt_stat64_to_stat32(&buf64, buf);
3237 return ret;
3240 /*********************************************************************
3241 * _wstat32i64 (MSVCR80.@)
3243 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
3245 int ret;
3246 struct MSVCRT__stat64 buf64;
3248 ret = MSVCRT__wstat64(path, &buf64);
3249 if (!ret)
3250 msvcrt_stat64_to_stat32i64(&buf64, buf);
3251 return ret;
3254 /*********************************************************************
3255 * _wstat64i32 (MSVCR80.@)
3257 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3259 int ret;
3260 struct MSVCRT__stat64 buf64;
3262 ret = MSVCRT__wstat64(path, &buf64);
3263 if (!ret)
3264 msvcrt_stat64_to_stat64i32(&buf64, buf);
3265 return ret;
3268 #endif /* _MSVCR_VER >= 80 */
3270 /*********************************************************************
3271 * _tell (MSVCRT.@)
3273 MSVCRT_long CDECL MSVCRT__tell(int fd)
3275 return MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
3278 /*********************************************************************
3279 * _telli64 (MSVCRT.@)
3281 __int64 CDECL _telli64(int fd)
3283 return MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
3286 /*********************************************************************
3287 * _tempnam (MSVCRT.@)
3289 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3291 char tmpbuf[MAX_PATH];
3292 const char *tmp_dir = MSVCRT_getenv("TMP");
3294 if (tmp_dir) dir = tmp_dir;
3296 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3297 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3299 TRACE("got name (%s)\n",tmpbuf);
3300 DeleteFileA(tmpbuf);
3301 return MSVCRT__strdup(tmpbuf);
3303 TRACE("failed (%d)\n",GetLastError());
3304 return NULL;
3307 /*********************************************************************
3308 * _wtempnam (MSVCRT.@)
3310 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3312 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3313 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3314 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3316 if (tmp_dir) dir = tmp_dir;
3318 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3319 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3321 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3322 DeleteFileW(tmpbuf);
3323 return MSVCRT__wcsdup(tmpbuf);
3325 TRACE("failed (%d)\n",GetLastError());
3326 return NULL;
3329 /*********************************************************************
3330 * _umask (MSVCRT.@)
3332 int CDECL MSVCRT__umask(int umask)
3334 int old_umask = MSVCRT_umask;
3335 TRACE("(%d)\n",umask);
3336 MSVCRT_umask = umask;
3337 return old_umask;
3340 /*********************************************************************
3341 * _utime64 (MSVCRT.@)
3343 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3345 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3347 if (fd > 0)
3349 int retVal = _futime64(fd, t);
3350 MSVCRT__close(fd);
3351 return retVal;
3353 return -1;
3356 /*********************************************************************
3357 * _utime32 (MSVCRT.@)
3359 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3361 if (t)
3363 struct MSVCRT___utimbuf64 t64;
3364 t64.actime = t->actime;
3365 t64.modtime = t->modtime;
3366 return _utime64( path, &t64 );
3368 else
3369 return _utime64( path, NULL );
3372 /*********************************************************************
3373 * _utime (MSVCRT.@)
3375 #ifdef _WIN64
3376 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3378 return _utime64( path, t );
3380 #else
3381 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3383 return _utime32( path, t );
3385 #endif
3387 /*********************************************************************
3388 * _wutime64 (MSVCRT.@)
3390 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3392 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3394 if (fd > 0)
3396 int retVal = _futime64(fd, t);
3397 MSVCRT__close(fd);
3398 return retVal;
3400 return -1;
3403 /*********************************************************************
3404 * _wutime32 (MSVCRT.@)
3406 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3408 if (t)
3410 struct MSVCRT___utimbuf64 t64;
3411 t64.actime = t->actime;
3412 t64.modtime = t->modtime;
3413 return _wutime64( path, &t64 );
3415 else
3416 return _wutime64( path, NULL );
3419 /*********************************************************************
3420 * _wutime (MSVCRT.@)
3422 #ifdef _WIN64
3423 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3425 return _wutime64( path, t );
3427 #else
3428 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3430 return _wutime32( path, t );
3432 #endif
3434 /*********************************************************************
3435 * _write (MSVCRT.@)
3437 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3439 DWORD num_written;
3440 ioinfo *info = get_ioinfo(fd);
3441 HANDLE hand = info->handle;
3443 /* Don't trace small writes, it gets *very* annoying */
3444 #if 0
3445 if (count > 32)
3446 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3447 #endif
3448 if (hand == INVALID_HANDLE_VALUE || fd == MSVCRT_NO_CONSOLE_FD)
3450 *MSVCRT__errno() = MSVCRT_EBADF;
3451 release_ioinfo(info);
3452 return -1;
3455 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3457 *MSVCRT__errno() = MSVCRT_EINVAL;
3458 release_ioinfo(info);
3459 return -1;
3462 /* If appending, go to EOF */
3463 if (info->wxflag & WX_APPEND)
3464 MSVCRT__lseek(fd, 0, FILE_END);
3466 if (!(info->wxflag & WX_TEXT))
3468 if (WriteFile(hand, buf, count, &num_written, NULL)
3469 && (num_written == count))
3471 release_ioinfo(info);
3472 return num_written;
3474 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3475 hand, GetLastError());
3476 msvcrt_set_errno(GetLastError());
3478 else
3480 unsigned int i, j, nr_lf, size;
3481 char *p = NULL;
3482 const char *q;
3483 const char *s = buf;
3485 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3487 /* find number of \n */
3488 for (nr_lf=0, i=0; i<count; i++)
3489 if (s[i] == '\n')
3490 nr_lf++;
3491 if (nr_lf)
3493 size = count+nr_lf;
3494 if ((q = p = MSVCRT_malloc(size)))
3496 for (s = buf, i = 0, j = 0; i < count; i++)
3498 if (s[i] == '\n')
3499 p[j++] = '\r';
3500 p[j++] = s[i];
3503 else
3505 FIXME("Malloc failed\n");
3506 nr_lf = 0;
3507 size = count;
3508 q = buf;
3511 else
3513 size = count;
3514 q = buf;
3517 else if (info->exflag & EF_UTF16)
3519 for (nr_lf=0, i=0; i<count; i+=2)
3520 if (s[i]=='\n' && s[i+1]==0)
3521 nr_lf += 2;
3522 if (nr_lf)
3524 size = count+nr_lf;
3525 if ((q = p = MSVCRT_malloc(size)))
3527 for (s=buf, i=0, j=0; i<count; i++)
3529 if (s[i]=='\n' && s[i+1]==0)
3531 p[j++] = '\r';
3532 p[j++] = 0;
3534 p[j++] = s[i++];
3535 p[j++] = s[i];
3538 else
3540 FIXME("Malloc failed\n");
3541 nr_lf = 0;
3542 size = count;
3543 q = buf;
3546 else
3548 size = count;
3549 q = buf;
3552 else
3554 DWORD conv_len;
3556 for(nr_lf=0, i=0; i<count; i+=2)
3557 if (s[i]=='\n' && s[i+1]==0)
3558 nr_lf++;
3560 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3561 if(!conv_len) {
3562 msvcrt_set_errno(GetLastError());
3563 MSVCRT_free(p);
3564 release_ioinfo(info);
3565 return -1;
3568 size = conv_len+nr_lf;
3569 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3571 for (s=buf, i=0, j=0; i<count; i++)
3573 if (s[i]=='\n' && s[i+1]==0)
3575 p[j++] = '\r';
3576 p[j++] = 0;
3578 p[j++] = s[i++];
3579 p[j++] = s[i];
3581 q = p+count+nr_lf*2;
3582 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3583 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3585 else
3587 FIXME("Malloc failed\n");
3588 nr_lf = 0;
3589 size = count;
3590 q = buf;
3594 if (!WriteFile(hand, q, size, &num_written, NULL))
3595 num_written = -1;
3596 release_ioinfo(info);
3597 MSVCRT_free(p);
3598 if (num_written != size)
3600 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3601 fd, hand, GetLastError(), num_written);
3602 msvcrt_set_errno(GetLastError());
3603 return -1;
3605 return count;
3608 release_ioinfo(info);
3609 return -1;
3612 /*********************************************************************
3613 * _putw (MSVCRT.@)
3615 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3617 int len;
3619 MSVCRT__lock_file(file);
3620 len = MSVCRT__write(file->_file, &val, sizeof(val));
3621 if (len == sizeof(val)) {
3622 MSVCRT__unlock_file(file);
3623 return val;
3626 file->_flag |= MSVCRT__IOERR;
3627 MSVCRT__unlock_file(file);
3628 return MSVCRT_EOF;
3631 /*********************************************************************
3632 * fclose (MSVCRT.@)
3634 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3636 int ret;
3638 MSVCRT__lock_file(file);
3639 ret = MSVCRT__fclose_nolock(file);
3640 MSVCRT__unlock_file(file);
3642 return ret;
3645 /*********************************************************************
3646 * _fclose_nolock (MSVCRT.@)
3648 int CDECL MSVCRT__fclose_nolock(MSVCRT_FILE* file)
3650 int r, flag;
3652 if(!(file->_flag & (MSVCRT__IOREAD | MSVCRT__IOWRT | MSVCRT__IORW)))
3654 file->_flag = 0;
3655 return MSVCRT_EOF;
3658 flag = file->_flag;
3659 MSVCRT_free(file->_tmpfname);
3660 file->_tmpfname = NULL;
3661 /* flush stdio buffers */
3662 if(file->_flag & MSVCRT__IOWRT)
3663 MSVCRT__fflush_nolock(file);
3664 if(file->_flag & MSVCRT__IOMYBUF)
3665 MSVCRT_free(file->_base);
3667 r=MSVCRT__close(file->_file);
3668 file->_flag = 0;
3670 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3673 /*********************************************************************
3674 * feof (MSVCRT.@)
3676 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3678 return file->_flag & MSVCRT__IOEOF;
3681 /*********************************************************************
3682 * ferror (MSVCRT.@)
3684 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3686 return file->_flag & MSVCRT__IOERR;
3689 /*********************************************************************
3690 * _filbuf (MSVCRT.@)
3692 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3694 unsigned char c;
3696 if(file->_flag & MSVCRT__IOSTRG)
3697 return MSVCRT_EOF;
3699 /* Allocate buffer if needed */
3700 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3701 msvcrt_alloc_buffer(file);
3703 if(!(file->_flag & MSVCRT__IOREAD)) {
3704 if(file->_flag & MSVCRT__IORW)
3705 file->_flag |= MSVCRT__IOREAD;
3706 else
3707 return MSVCRT_EOF;
3710 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3711 int r;
3712 if ((r = MSVCRT__read(file->_file,&c,1)) != 1) {
3713 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3714 return MSVCRT_EOF;
3717 return c;
3718 } else {
3719 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3720 if(file->_cnt<=0) {
3721 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3722 file->_cnt = 0;
3723 return MSVCRT_EOF;
3726 file->_cnt--;
3727 file->_ptr = file->_base+1;
3728 c = *(unsigned char *)file->_base;
3729 return c;
3733 /*********************************************************************
3734 * fgetc (MSVCRT.@)
3736 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3738 int ret;
3740 MSVCRT__lock_file(file);
3741 ret = MSVCRT__fgetc_nolock(file);
3742 MSVCRT__unlock_file(file);
3744 return ret;
3747 /*********************************************************************
3748 * _fgetc_nolock (MSVCRT.@)
3750 int CDECL MSVCRT__fgetc_nolock(MSVCRT_FILE* file)
3752 unsigned char *i;
3753 unsigned int j;
3755 if (file->_cnt>0) {
3756 file->_cnt--;
3757 i = (unsigned char *)file->_ptr++;
3758 j = *i;
3759 } else
3760 j = MSVCRT__filbuf(file);
3762 return j;
3765 /*********************************************************************
3766 * _fgetchar (MSVCRT.@)
3768 int CDECL MSVCRT__fgetchar(void)
3770 return MSVCRT_fgetc(MSVCRT_stdin);
3773 /*********************************************************************
3774 * fgets (MSVCRT.@)
3776 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3778 int cc = MSVCRT_EOF;
3779 char * buf_start = s;
3781 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3782 file,file->_file,s,size);
3784 MSVCRT__lock_file(file);
3786 while ((size >1) && (cc = MSVCRT__fgetc_nolock(file)) != MSVCRT_EOF && cc != '\n')
3788 *s++ = (char)cc;
3789 size --;
3791 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3793 TRACE(":nothing read\n");
3794 MSVCRT__unlock_file(file);
3795 return NULL;
3797 if ((cc != MSVCRT_EOF) && (size > 1))
3798 *s++ = cc;
3799 *s = '\0';
3800 TRACE(":got %s\n", debugstr_a(buf_start));
3801 MSVCRT__unlock_file(file);
3802 return buf_start;
3805 /*********************************************************************
3806 * fgetwc (MSVCRT.@)
3808 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3810 MSVCRT_wint_t ret;
3812 MSVCRT__lock_file(file);
3813 ret = MSVCRT__fgetwc_nolock(file);
3814 MSVCRT__unlock_file(file);
3816 return ret;
3819 /*********************************************************************
3820 * _fgetwc_nolock (MSVCRT.@)
3822 MSVCRT_wint_t CDECL MSVCRT__fgetwc_nolock(MSVCRT_FILE* file)
3824 MSVCRT_wint_t ret;
3825 int ch;
3827 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3828 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3829 char *p;
3831 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3832 ch = MSVCRT__fgetc_nolock(file);
3833 if(ch == MSVCRT_EOF) {
3834 ret = MSVCRT_WEOF;
3835 break;
3837 *p = (char)ch;
3839 }else {
3840 char mbs[MSVCRT_MB_LEN_MAX];
3841 int len = 0;
3843 ch = MSVCRT__fgetc_nolock(file);
3844 if(ch != MSVCRT_EOF) {
3845 mbs[0] = (char)ch;
3846 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3847 ch = MSVCRT__fgetc_nolock(file);
3848 if(ch != MSVCRT_EOF) {
3849 mbs[1] = (char)ch;
3850 len = 2;
3852 }else {
3853 len = 1;
3857 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3858 ret = MSVCRT_WEOF;
3861 return ret;
3864 /*********************************************************************
3865 * _getw (MSVCRT.@)
3867 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3869 char *ch;
3870 int i, k;
3871 unsigned int j;
3872 ch = (char *)&i;
3874 MSVCRT__lock_file(file);
3875 for (j=0; j<sizeof(int); j++) {
3876 k = MSVCRT__fgetc_nolock(file);
3877 if (k == MSVCRT_EOF) {
3878 file->_flag |= MSVCRT__IOEOF;
3879 MSVCRT__unlock_file(file);
3880 return MSVCRT_EOF;
3882 ch[j] = k;
3885 MSVCRT__unlock_file(file);
3886 return i;
3889 /*********************************************************************
3890 * getwc (MSVCRT.@)
3892 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3894 return MSVCRT_fgetwc(file);
3897 /*********************************************************************
3898 * _fgetwchar (MSVCRT.@)
3900 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3902 return MSVCRT_fgetwc(MSVCRT_stdin);
3905 /*********************************************************************
3906 * getwchar (MSVCRT.@)
3908 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3910 return MSVCRT__fgetwchar();
3913 /*********************************************************************
3914 * fgetws (MSVCRT.@)
3916 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3918 MSVCRT_wint_t cc = MSVCRT_WEOF;
3919 MSVCRT_wchar_t * buf_start = s;
3921 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3922 file,file->_file,s,size);
3924 MSVCRT__lock_file(file);
3926 while ((size >1) && (cc = MSVCRT__fgetwc_nolock(file)) != MSVCRT_WEOF && cc != '\n')
3928 *s++ = cc;
3929 size --;
3931 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3933 TRACE(":nothing read\n");
3934 MSVCRT__unlock_file(file);
3935 return NULL;
3937 if ((cc != MSVCRT_WEOF) && (size > 1))
3938 *s++ = cc;
3939 *s = 0;
3940 TRACE(":got %s\n", debugstr_w(buf_start));
3941 MSVCRT__unlock_file(file);
3942 return buf_start;
3945 /*********************************************************************
3946 * _flsbuf (MSVCRT.@)
3948 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3950 /* Flush output buffer */
3951 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3952 msvcrt_alloc_buffer(file);
3955 if(!(file->_flag & MSVCRT__IOWRT)) {
3956 if(!(file->_flag & MSVCRT__IORW)) {
3957 file->_flag |= MSVCRT__IOERR;
3958 *MSVCRT__errno() = MSVCRT_EBADF;
3959 return MSVCRT_EOF;
3961 file->_flag |= MSVCRT__IOWRT;
3963 if(file->_flag & MSVCRT__IOREAD) {
3964 if(!(file->_flag & MSVCRT__IOEOF)) {
3965 file->_flag |= MSVCRT__IOERR;
3966 return MSVCRT_EOF;
3968 file->_cnt = 0;
3969 file->_ptr = file->_base;
3970 file->_flag &= ~(MSVCRT__IOREAD | MSVCRT__IOEOF);
3973 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3974 int res = 0;
3976 if(file->_cnt <= 0) {
3977 res = msvcrt_flush_buffer(file);
3978 if(res)
3979 return res;
3980 file->_flag |= MSVCRT__IOWRT;
3981 file->_cnt=file->_bufsiz;
3983 *file->_ptr++ = c;
3984 file->_cnt--;
3985 return c&0xff;
3986 } else {
3987 unsigned char cc=c;
3988 int len;
3989 /* set _cnt to 0 for unbuffered FILEs */
3990 file->_cnt = 0;
3991 len = MSVCRT__write(file->_file, &cc, 1);
3992 if (len == 1)
3993 return c & 0xff;
3994 file->_flag |= MSVCRT__IOERR;
3995 return MSVCRT_EOF;
3999 /*********************************************************************
4000 * fwrite (MSVCRT.@)
4002 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4004 MSVCRT_size_t ret;
4006 MSVCRT__lock_file(file);
4007 ret = MSVCRT__fwrite_nolock(ptr, size, nmemb, file);
4008 MSVCRT__unlock_file(file);
4010 return ret;
4013 /*********************************************************************
4014 * _fwrite_nolock (MSVCRT.@)
4016 MSVCRT_size_t CDECL MSVCRT__fwrite_nolock(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4018 MSVCRT_size_t wrcnt=size * nmemb;
4019 int written = 0;
4020 if (size == 0)
4021 return 0;
4023 while(wrcnt) {
4024 if(file->_cnt < 0) {
4025 WARN("negative file->_cnt value in %p\n", file);
4026 file->_flag |= MSVCRT__IOERR;
4027 break;
4028 } else if(file->_cnt) {
4029 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
4030 memcpy(file->_ptr, ptr, pcnt);
4031 file->_cnt -= pcnt;
4032 file->_ptr += pcnt;
4033 written += pcnt;
4034 wrcnt -= pcnt;
4035 ptr = (const char*)ptr + pcnt;
4036 } else if((file->_flag & MSVCRT__IONBF)
4037 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
4038 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
4039 MSVCRT_size_t pcnt;
4040 int bufsiz;
4042 if(file->_flag & MSVCRT__IONBF)
4043 bufsiz = 1;
4044 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4045 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
4046 else
4047 bufsiz = file->_bufsiz;
4049 pcnt = (wrcnt / bufsiz) * bufsiz;
4051 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
4052 break;
4054 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
4055 file->_flag |= MSVCRT__IOERR;
4056 break;
4058 written += pcnt;
4059 wrcnt -= pcnt;
4060 ptr = (const char*)ptr + pcnt;
4061 } else {
4062 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
4063 break;
4064 written++;
4065 wrcnt--;
4066 ptr = (const char*)ptr + 1;
4070 return written / size;
4073 /*********************************************************************
4074 * fputwc (MSVCRT.@)
4076 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
4078 MSVCRT_wint_t ret;
4080 MSVCRT__lock_file(file);
4081 ret = MSVCRT__fputwc_nolock(wc, file);
4082 MSVCRT__unlock_file(file);
4084 return ret;
4087 /*********************************************************************
4088 * _fputwc_nolock (MSVCRT.@)
4090 MSVCRT_wint_t CDECL MSVCRT__fputwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE* file)
4092 MSVCRT_wchar_t mwc=wc;
4093 ioinfo *fdinfo;
4094 MSVCRT_wint_t ret;
4096 fdinfo = get_ioinfo_nolock(file->_file);
4098 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
4099 char buf[MSVCRT_MB_LEN_MAX];
4100 int char_len;
4102 char_len = MSVCRT_wctomb(buf, mwc);
4103 if(char_len!=-1 && MSVCRT__fwrite_nolock(buf, char_len, 1, file)==1)
4104 ret = wc;
4105 else
4106 ret = MSVCRT_WEOF;
4107 }else if(MSVCRT__fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
4108 ret = wc;
4109 }else {
4110 ret = MSVCRT_WEOF;
4113 return ret;
4116 /*********************************************************************
4117 * _fputwchar (MSVCRT.@)
4119 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
4121 return MSVCRT_fputwc(wc, MSVCRT_stdout);
4124 /*********************************************************************
4125 * _wfsopen (MSVCRT.@)
4127 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
4129 MSVCRT_FILE* file;
4130 int open_flags, stream_flags, fd;
4132 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
4134 /* map mode string to open() flags. "man fopen" for possibilities. */
4135 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4136 return NULL;
4138 LOCK_FILES();
4139 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4140 if (fd < 0)
4141 file = NULL;
4142 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
4143 != -1)
4144 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
4145 else if (file)
4147 file->_flag = 0;
4148 file = NULL;
4151 TRACE(":got (%p)\n",file);
4152 if (fd >= 0 && !file)
4153 MSVCRT__close(fd);
4154 UNLOCK_FILES();
4155 return file;
4158 /*********************************************************************
4159 * _fsopen (MSVCRT.@)
4161 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
4163 MSVCRT_FILE *ret;
4164 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4166 if (path && !(pathW = msvcrt_wstrdupa(path))) {
4167 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4168 *MSVCRT__errno() = MSVCRT_EINVAL;
4169 return NULL;
4171 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4173 MSVCRT_free(pathW);
4174 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4175 *MSVCRT__errno() = MSVCRT_EINVAL;
4176 return NULL;
4179 ret = MSVCRT__wfsopen(pathW, modeW, share);
4181 MSVCRT_free(pathW);
4182 MSVCRT_free(modeW);
4183 return ret;
4186 /*********************************************************************
4187 * fopen (MSVCRT.@)
4189 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
4191 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
4194 /*********************************************************************
4195 * fopen_s (MSVCRT.@)
4197 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
4198 const char *filename, const char *mode)
4200 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4201 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4202 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4204 *pFile = MSVCRT_fopen(filename, mode);
4206 if(!*pFile)
4207 return *MSVCRT__errno();
4208 return 0;
4211 /*********************************************************************
4212 * _wfopen (MSVCRT.@)
4214 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
4216 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
4219 /*********************************************************************
4220 * _wfopen_s (MSVCRT.@)
4222 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
4223 const MSVCRT_wchar_t *mode)
4225 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4226 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4227 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4229 *pFile = MSVCRT__wfopen(filename, mode);
4231 if(!*pFile)
4232 return *MSVCRT__errno();
4233 return 0;
4236 /*********************************************************************
4237 * fputc (MSVCRT.@)
4239 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
4241 int ret;
4243 MSVCRT__lock_file(file);
4244 ret = MSVCRT__fputc_nolock(c, file);
4245 MSVCRT__unlock_file(file);
4247 return ret;
4250 /*********************************************************************
4251 * _fputc_nolock (MSVCRT.@)
4253 int CDECL MSVCRT__fputc_nolock(int c, MSVCRT_FILE* file)
4255 int res;
4257 if(file->_cnt>0) {
4258 *file->_ptr++=c;
4259 file->_cnt--;
4260 if (c == '\n')
4262 res = msvcrt_flush_buffer(file);
4263 return res ? res : c;
4265 else {
4266 return c & 0xff;
4268 } else {
4269 res = MSVCRT__flsbuf(c, file);
4270 return res;
4274 /*********************************************************************
4275 * _fputchar (MSVCRT.@)
4277 int CDECL MSVCRT__fputchar(int c)
4279 return MSVCRT_fputc(c, MSVCRT_stdout);
4282 /*********************************************************************
4283 * fread (MSVCRT.@)
4285 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4287 MSVCRT_size_t ret;
4289 MSVCRT__lock_file(file);
4290 ret = MSVCRT__fread_nolock(ptr, size, nmemb, file);
4291 MSVCRT__unlock_file(file);
4293 return ret;
4296 /*********************************************************************
4297 * _fread_nolock (MSVCRT.@)
4299 MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4301 MSVCRT_size_t rcnt=size * nmemb;
4302 MSVCRT_size_t read=0;
4303 MSVCRT_size_t pread=0;
4305 if(!rcnt)
4306 return 0;
4308 /* first buffered data */
4309 if(file->_cnt>0) {
4310 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4311 memcpy(ptr, file->_ptr, pcnt);
4312 file->_cnt -= pcnt;
4313 file->_ptr += pcnt;
4314 read += pcnt ;
4315 rcnt -= pcnt ;
4316 ptr = (char*)ptr + pcnt;
4317 } else if(!(file->_flag & MSVCRT__IOREAD )) {
4318 if(file->_flag & MSVCRT__IORW) {
4319 file->_flag |= MSVCRT__IOREAD;
4320 } else {
4321 return 0;
4325 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4326 msvcrt_alloc_buffer(file);
4328 while(rcnt>0)
4330 int i;
4331 if (!file->_cnt && rcnt<file->_bufsiz && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
4332 i = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
4333 file->_ptr = file->_base;
4334 if (i != -1) {
4335 file->_cnt = i;
4336 if (i > rcnt) i = rcnt;
4338 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4339 if (i > 0 && i < file->_cnt) {
4340 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4341 file->_flag &= ~MSVCRT__IOEOF;
4343 if (i > 0) {
4344 memcpy(ptr, file->_ptr, i);
4345 file->_cnt -= i;
4346 file->_ptr += i;
4348 } else if (rcnt > INT_MAX) {
4349 i = MSVCRT__read(file->_file, ptr, INT_MAX);
4350 } else if (rcnt < (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ)) {
4351 i = MSVCRT__read(file->_file, ptr, rcnt);
4352 } else {
4353 i = MSVCRT__read(file->_file, ptr, rcnt - rcnt % (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ));
4355 pread += i;
4356 rcnt -= i;
4357 ptr = (char *)ptr+i;
4358 /* expose feof condition in the flags
4359 * MFC tests file->_flag for feof, and doesn't call feof())
4361 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4362 file->_flag |= MSVCRT__IOEOF;
4363 else if (i == -1)
4365 file->_flag |= MSVCRT__IOERR;
4366 pread = 0;
4367 rcnt = 0;
4369 if (i < 1) break;
4371 read+=pread;
4372 return read / size;
4375 #if _MSVCR_VER >= 80
4377 /*********************************************************************
4378 * fread_s (MSVCR80.@)
4380 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4381 MSVCRT_size_t count, MSVCRT_FILE *stream)
4383 MSVCRT_size_t ret;
4385 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4386 if(buf && buf_size)
4387 memset(buf, 0, buf_size);
4388 return 0;
4390 if(!elem_size || !count) return 0;
4392 MSVCRT__lock_file(stream);
4393 ret = MSVCRT__fread_nolock_s(buf, buf_size, elem_size, count, stream);
4394 MSVCRT__unlock_file(stream);
4396 return ret;
4399 /*********************************************************************
4400 * _fread_nolock_s (MSVCR80.@)
4402 MSVCRT_size_t CDECL MSVCRT__fread_nolock_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4403 MSVCRT_size_t count, MSVCRT_FILE *stream)
4405 size_t bytes_left, buf_pos;
4407 TRACE("(%p %lu %lu %lu %p)\n", buf, buf_size, elem_size, count, stream);
4409 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4410 if(buf && buf_size)
4411 memset(buf, 0, buf_size);
4412 return 0;
4414 if(!elem_size || !count) return 0;
4415 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4416 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4418 bytes_left = elem_size*count;
4419 buf_pos = 0;
4420 while(bytes_left) {
4421 if(stream->_cnt > 0) {
4422 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4424 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4425 memset(buf, 0, buf_size);
4426 return 0;
4429 MSVCRT__fread_nolock((char*)buf+buf_pos, 1, size, stream);
4430 buf_pos += size;
4431 bytes_left -= size;
4432 }else {
4433 int c = MSVCRT__filbuf(stream);
4435 if(c == MSVCRT_EOF)
4436 break;
4438 if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, MSVCRT_ERANGE)) {
4439 memset(buf, 0, buf_size);
4440 return 0;
4443 ((char*)buf)[buf_pos++] = c;
4444 bytes_left--;
4448 return buf_pos/elem_size;
4451 #endif /* _MSVCR_VER >= 80 */
4453 /*********************************************************************
4454 * _wfreopen (MSVCRT.@)
4457 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4459 int open_flags, stream_flags, fd;
4461 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4463 LOCK_FILES();
4464 if (!file || ((fd = file->_file) < 0))
4465 file = NULL;
4466 else
4468 MSVCRT_fclose(file);
4469 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4470 file = NULL;
4471 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4472 file = NULL;
4473 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4475 file->_flag = 0;
4476 file = NULL;
4479 UNLOCK_FILES();
4480 return file;
4483 /*********************************************************************
4484 * _wfreopen_s (MSVCRT.@)
4486 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4487 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4489 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4490 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4491 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4492 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4494 *pFile = MSVCRT__wfreopen(path, mode, file);
4496 if(!*pFile)
4497 return *MSVCRT__errno();
4498 return 0;
4501 /*********************************************************************
4502 * freopen (MSVCRT.@)
4505 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4507 MSVCRT_FILE *ret;
4508 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4510 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4511 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4513 MSVCRT_free(pathW);
4514 return NULL;
4517 ret = MSVCRT__wfreopen(pathW, modeW, file);
4519 MSVCRT_free(pathW);
4520 MSVCRT_free(modeW);
4521 return ret;
4524 /*********************************************************************
4525 * freopen_s (MSVCRT.@)
4527 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4528 const char *path, const char *mode, MSVCRT_FILE* file)
4530 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4531 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4532 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4533 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4535 *pFile = MSVCRT_freopen(path, mode, file);
4537 if(!*pFile)
4538 return *MSVCRT__errno();
4539 return 0;
4542 /*********************************************************************
4543 * fsetpos (MSVCRT.@)
4545 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4547 int ret;
4549 MSVCRT__lock_file(file);
4550 msvcrt_flush_buffer(file);
4552 /* Reset direction of i/o */
4553 if(file->_flag & MSVCRT__IORW) {
4554 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4557 ret = (MSVCRT__lseeki64(file->_file,*pos,MSVCRT_SEEK_SET) == -1) ? -1 : 0;
4558 MSVCRT__unlock_file(file);
4559 return ret;
4562 /*********************************************************************
4563 * _ftelli64 (MSVCRT.@)
4565 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4567 __int64 ret;
4569 MSVCRT__lock_file(file);
4570 ret = MSVCRT__ftelli64_nolock(file);
4571 MSVCRT__unlock_file(file);
4573 return ret;
4576 /*********************************************************************
4577 * _ftelli64_nolock (MSVCRT.@)
4579 __int64 CDECL MSVCRT__ftelli64_nolock(MSVCRT_FILE* file)
4581 __int64 pos;
4583 pos = _telli64(file->_file);
4584 if(pos == -1)
4585 return -1;
4586 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4587 if(file->_flag & MSVCRT__IOWRT) {
4588 pos += file->_ptr - file->_base;
4590 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4591 char *p;
4593 for(p=file->_base; p<file->_ptr; p++)
4594 if(*p == '\n')
4595 pos++;
4597 } else if(!file->_cnt) { /* nothing to do */
4598 } else if(MSVCRT__lseeki64(file->_file, 0, MSVCRT_SEEK_END)==pos) {
4599 int i;
4601 pos -= file->_cnt;
4602 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4603 for(i=0; i<file->_cnt; i++)
4604 if(file->_ptr[i] == '\n')
4605 pos--;
4607 } else {
4608 char *p;
4610 if(MSVCRT__lseeki64(file->_file, pos, MSVCRT_SEEK_SET) != pos)
4611 return -1;
4613 pos -= file->_bufsiz;
4614 pos += file->_ptr - file->_base;
4616 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4617 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4618 pos--;
4620 for(p=file->_base; p<file->_ptr; p++)
4621 if(*p == '\n')
4622 pos++;
4627 return pos;
4630 /*********************************************************************
4631 * ftell (MSVCRT.@)
4633 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4635 return MSVCRT__ftelli64(file);
4638 #if _MSVCR_VER >= 80
4639 /*********************************************************************
4640 * _ftell_nolock (MSVCR80.@)
4642 LONG CDECL MSVCRT__ftell_nolock(MSVCRT_FILE* file)
4644 return MSVCRT__ftelli64_nolock(file);
4646 #endif
4648 /*********************************************************************
4649 * fgetpos (MSVCRT.@)
4651 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4653 *pos = MSVCRT__ftelli64(file);
4654 if(*pos == -1)
4655 return -1;
4656 return 0;
4659 /*********************************************************************
4660 * fputs (MSVCRT.@)
4662 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4664 MSVCRT_size_t len = strlen(s);
4665 int ret;
4667 MSVCRT__lock_file(file);
4668 ret = MSVCRT__fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4669 MSVCRT__unlock_file(file);
4670 return ret;
4673 /*********************************************************************
4674 * fputws (MSVCRT.@)
4676 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4678 MSVCRT_size_t i, len = strlenW(s);
4679 BOOL tmp_buf;
4680 int ret;
4682 MSVCRT__lock_file(file);
4683 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4684 ret = MSVCRT__fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4685 MSVCRT__unlock_file(file);
4686 return ret;
4689 tmp_buf = add_std_buffer(file);
4690 for (i=0; i<len; i++) {
4691 if(MSVCRT__fputwc_nolock(s[i], file) == MSVCRT_WEOF) {
4692 if(tmp_buf) remove_std_buffer(file);
4693 MSVCRT__unlock_file(file);
4694 return MSVCRT_WEOF;
4698 if(tmp_buf) remove_std_buffer(file);
4699 MSVCRT__unlock_file(file);
4700 return 0;
4703 /*********************************************************************
4704 * getchar (MSVCRT.@)
4706 int CDECL MSVCRT_getchar(void)
4708 return MSVCRT_fgetc(MSVCRT_stdin);
4711 /*********************************************************************
4712 * getc (MSVCRT.@)
4714 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4716 return MSVCRT_fgetc(file);
4719 /*********************************************************************
4720 * gets_s (MSVCR80.@)
4722 char * CDECL MSVCRT_gets_s(char *buf, MSVCRT_size_t len)
4724 char *buf_start = buf;
4725 int cc;
4727 if (!MSVCRT_CHECK_PMT(buf != NULL)) return NULL;
4728 if (!MSVCRT_CHECK_PMT(len != 0)) return NULL;
4730 MSVCRT__lock_file(MSVCRT_stdin);
4731 for(cc = MSVCRT__fgetc_nolock(MSVCRT_stdin);
4732 len != 0 && cc != MSVCRT_EOF && cc != '\n';
4733 cc = MSVCRT__fgetc_nolock(MSVCRT_stdin))
4735 if (cc != '\r')
4737 *buf++ = (char)cc;
4738 len--;
4741 MSVCRT__unlock_file(MSVCRT_stdin);
4743 if (!len)
4745 *buf_start = 0;
4746 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4747 return NULL;
4750 if ((cc == MSVCRT_EOF) && (buf_start == buf))
4752 TRACE(":nothing read\n");
4753 return NULL;
4755 *buf = '\0';
4757 TRACE("got '%s'\n", buf_start);
4758 return buf_start;
4761 /*********************************************************************
4762 * gets (MSVCRT.@)
4764 char * CDECL MSVCRT_gets(char *buf)
4766 return MSVCRT_gets_s(buf, -1);
4769 /*********************************************************************
4770 * _getws (MSVCRT.@)
4772 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4774 MSVCRT_wint_t cc;
4775 MSVCRT_wchar_t* ws = buf;
4777 MSVCRT__lock_file(MSVCRT_stdin);
4778 for (cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4779 cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin))
4781 if (cc != '\r')
4782 *buf++ = (MSVCRT_wchar_t)cc;
4784 MSVCRT__unlock_file(MSVCRT_stdin);
4786 if ((cc == MSVCRT_WEOF) && (ws == buf))
4788 TRACE(":nothing read\n");
4789 return NULL;
4791 *buf = '\0';
4793 TRACE("got %s\n", debugstr_w(ws));
4794 return ws;
4797 /*********************************************************************
4798 * putc (MSVCRT.@)
4800 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4802 return MSVCRT_fputc(c, file);
4805 /*********************************************************************
4806 * putchar (MSVCRT.@)
4808 int CDECL MSVCRT_putchar(int c)
4810 return MSVCRT_fputc(c, MSVCRT_stdout);
4813 /*********************************************************************
4814 * puts (MSVCRT.@)
4816 int CDECL MSVCRT_puts(const char *s)
4818 MSVCRT_size_t len = strlen(s);
4819 int ret;
4821 MSVCRT__lock_file(MSVCRT_stdout);
4822 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4823 MSVCRT__unlock_file(MSVCRT_stdout);
4824 return MSVCRT_EOF;
4827 ret = MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4828 MSVCRT__unlock_file(MSVCRT_stdout);
4829 return ret;
4832 /*********************************************************************
4833 * _putws (MSVCRT.@)
4835 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4837 static const MSVCRT_wchar_t nl = '\n';
4838 MSVCRT_size_t len = strlenW(s);
4839 int ret;
4841 MSVCRT__lock_file(MSVCRT_stdout);
4842 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4843 MSVCRT__unlock_file(MSVCRT_stdout);
4844 return MSVCRT_EOF;
4847 ret = MSVCRT__fwrite_nolock(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4848 MSVCRT__unlock_file(MSVCRT_stdout);
4849 return ret;
4852 /*********************************************************************
4853 * remove (MSVCRT.@)
4855 int CDECL MSVCRT_remove(const char *path)
4857 TRACE("(%s)\n",path);
4858 if (DeleteFileA(path))
4859 return 0;
4860 TRACE(":failed (%d)\n",GetLastError());
4861 msvcrt_set_errno(GetLastError());
4862 return -1;
4865 /*********************************************************************
4866 * _wremove (MSVCRT.@)
4868 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4870 TRACE("(%s)\n",debugstr_w(path));
4871 if (DeleteFileW(path))
4872 return 0;
4873 TRACE(":failed (%d)\n",GetLastError());
4874 msvcrt_set_errno(GetLastError());
4875 return -1;
4878 /*********************************************************************
4879 * rename (MSVCRT.@)
4881 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4883 TRACE(":from %s to %s\n",oldpath,newpath);
4884 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4885 return 0;
4886 TRACE(":failed (%d)\n",GetLastError());
4887 msvcrt_set_errno(GetLastError());
4888 return -1;
4891 /*********************************************************************
4892 * _wrename (MSVCRT.@)
4894 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4896 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4897 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4898 return 0;
4899 TRACE(":failed (%d)\n",GetLastError());
4900 msvcrt_set_errno(GetLastError());
4901 return -1;
4904 /*********************************************************************
4905 * setvbuf (MSVCRT.@)
4907 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4909 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4910 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4911 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4913 MSVCRT__lock_file(file);
4915 MSVCRT__fflush_nolock(file);
4916 if(file->_flag & MSVCRT__IOMYBUF)
4917 MSVCRT_free(file->_base);
4918 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4919 file->_cnt = 0;
4921 if(mode == MSVCRT__IONBF) {
4922 file->_flag |= MSVCRT__IONBF;
4923 file->_base = file->_ptr = (char*)&file->_charbuf;
4924 file->_bufsiz = 2;
4925 }else if(buf) {
4926 file->_base = file->_ptr = buf;
4927 file->_flag |= MSVCRT__USERBUF;
4928 file->_bufsiz = size;
4929 }else {
4930 file->_base = file->_ptr = MSVCRT_malloc(size);
4931 if(!file->_base) {
4932 file->_bufsiz = 0;
4933 MSVCRT__unlock_file(file);
4934 return -1;
4937 file->_flag |= MSVCRT__IOMYBUF;
4938 file->_bufsiz = size;
4940 MSVCRT__unlock_file(file);
4941 return 0;
4944 /*********************************************************************
4945 * setbuf (MSVCRT.@)
4947 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4949 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4952 static int tmpnam_helper(char *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
4954 char tmpstr[8];
4955 char *p = s;
4956 int digits;
4958 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
4960 if (size < 3) {
4961 if (size) *s = 0;
4962 *MSVCRT__errno() = MSVCRT_ERANGE;
4963 return MSVCRT_ERANGE;
4965 *p++ = '\\';
4966 *p++ = 's';
4967 size -= 2;
4968 digits = msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4969 if (digits+1 > size) {
4970 *s = 0;
4971 *MSVCRT__errno() = MSVCRT_ERANGE;
4972 return MSVCRT_ERANGE;
4974 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4975 p += digits;
4976 *p++ = '.';
4977 size -= digits+1;
4979 while(1) {
4980 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4981 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4982 break;
4985 digits = msvcrt_int_to_base32(digits, tmpstr);
4986 if (digits+1 > size) {
4987 *s = 0;
4988 *MSVCRT__errno() = MSVCRT_ERANGE;
4989 return MSVCRT_ERANGE;
4991 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4992 p[digits] = 0;
4994 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4995 GetLastError() == ERROR_FILE_NOT_FOUND)
4996 break;
4998 return 0;
5001 int CDECL MSVCRT_tmpnam_s(char *s, MSVCRT_size_t size)
5003 return tmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
5006 /*********************************************************************
5007 * tmpnam (MSVCRT.@)
5009 char * CDECL MSVCRT_tmpnam(char *s)
5011 if (!s) {
5012 thread_data_t *data = msvcrt_get_thread_data();
5014 if(!data->tmpnam_buffer)
5015 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
5017 s = data->tmpnam_buffer;
5020 return tmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
5023 static int wtmpnam_helper(MSVCRT_wchar_t *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
5025 MSVCRT_wchar_t tmpstr[8];
5026 MSVCRT_wchar_t *p = s;
5027 int digits;
5029 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
5031 if (size < 3) {
5032 if (size) *s = 0;
5033 *MSVCRT__errno() = MSVCRT_ERANGE;
5034 return MSVCRT_ERANGE;
5036 *p++ = '\\';
5037 *p++ = 's';
5038 size -= 2;
5039 digits = msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
5040 if (digits+1 > size) {
5041 *s = 0;
5042 *MSVCRT__errno() = MSVCRT_ERANGE;
5043 return MSVCRT_ERANGE;
5045 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5046 p += digits;
5047 *p++ = '.';
5048 size -= digits+1;
5050 while(1) {
5051 while ((digits = *tmpnam_unique)+1 < tmp_max) {
5052 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
5053 break;
5056 digits = msvcrt_int_to_base32_w(digits, tmpstr);
5057 if (digits+1 > size) {
5058 *s = 0;
5059 *MSVCRT__errno() = MSVCRT_ERANGE;
5060 return MSVCRT_ERANGE;
5062 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5063 p[digits] = 0;
5065 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
5066 GetLastError() == ERROR_FILE_NOT_FOUND)
5067 break;
5069 return 0;
5072 /*********************************************************************
5073 * _wtmpnam_s (MSVCRT.@)
5075 int CDECL MSVCRT__wtmpnam_s(MSVCRT_wchar_t *s, MSVCRT_size_t size)
5077 return wtmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
5080 /*********************************************************************
5081 * _wtmpnam (MSVCRT.@)
5083 MSVCRT_wchar_t * CDECL MSVCRT__wtmpnam(MSVCRT_wchar_t *s)
5085 if (!s) {
5086 thread_data_t *data = msvcrt_get_thread_data();
5088 if(!data->wtmpnam_buffer)
5089 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
5091 s = data->wtmpnam_buffer;
5094 return wtmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
5097 /*********************************************************************
5098 * tmpfile (MSVCRT.@)
5100 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
5102 char *filename = MSVCRT__tempnam(",", "t");
5103 int fd;
5104 MSVCRT_FILE* file = NULL;
5106 LOCK_FILES();
5107 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
5108 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
5109 if (fd != -1 && (file = msvcrt_alloc_fp()))
5111 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
5113 file->_flag = 0;
5114 file = NULL;
5116 else file->_tmpfname = MSVCRT__strdup(filename);
5119 if(fd != -1 && !file)
5120 MSVCRT__close(fd);
5121 MSVCRT_free(filename);
5122 UNLOCK_FILES();
5123 return file;
5126 /*********************************************************************
5127 * tmpfile_s (MSVCRT.@)
5129 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
5131 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
5133 *file = MSVCRT_tmpfile();
5134 return 0;
5137 static int puts_clbk_file_a(void *file, int len, const char *str)
5139 return MSVCRT_fwrite(str, sizeof(char), len, file);
5142 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
5144 int i, ret;
5146 MSVCRT__lock_file(file);
5148 if(!(get_ioinfo_nolock(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
5149 ret = MSVCRT__fwrite_nolock(str, sizeof(MSVCRT_wchar_t), len, file);
5150 MSVCRT__unlock_file(file);
5151 return ret;
5154 for(i=0; i<len; i++) {
5155 if(MSVCRT__fputwc_nolock(str[i], file) == MSVCRT_WEOF) {
5156 MSVCRT__unlock_file(file);
5157 return -1;
5161 MSVCRT__unlock_file(file);
5162 return len;
5165 static int vfprintf_helper(DWORD options, MSVCRT_FILE* file, const char *format,
5166 MSVCRT__locale_t locale, __ms_va_list valist)
5168 printf_arg args_ctx[MSVCRT__ARGMAX+1];
5169 BOOL tmp_buf;
5170 int ret;
5172 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5173 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5175 if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
5176 memset(args_ctx, 0, sizeof(args_ctx));
5177 ret = create_positional_ctx_a(args_ctx, format, valist);
5178 if(ret < 0) {
5179 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
5180 *MSVCRT__errno() = MSVCRT_EINVAL;
5181 return ret;
5182 } else if(!ret)
5183 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5186 MSVCRT__lock_file(file);
5187 tmp_buf = add_std_buffer(file);
5188 ret = pf_printf_a(puts_clbk_file_a, file, format, locale, options,
5189 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
5190 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
5191 if(tmp_buf) remove_std_buffer(file);
5192 MSVCRT__unlock_file(file);
5194 return ret;
5197 static int vfwprintf_helper(DWORD options, MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5198 MSVCRT__locale_t locale, __ms_va_list valist)
5200 printf_arg args_ctx[MSVCRT__ARGMAX+1];
5201 BOOL tmp_buf;
5202 int ret;
5204 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5205 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5207 if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
5208 memset(args_ctx, 0, sizeof(args_ctx));
5209 ret = create_positional_ctx_w(args_ctx, format, valist);
5210 if(ret < 0) {
5211 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
5212 *MSVCRT__errno() = MSVCRT_EINVAL;
5213 return ret;
5214 } else if(!ret)
5215 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5218 MSVCRT__lock_file(file);
5219 tmp_buf = add_std_buffer(file);
5220 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, options,
5221 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
5222 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
5223 if(tmp_buf) remove_std_buffer(file);
5224 MSVCRT__unlock_file(file);
5226 return ret;
5229 /*********************************************************************
5230 * _vfprintf_s_l (MSVCRT.@)
5232 int CDECL MSVCRT__vfprintf_s_l(MSVCRT_FILE* file, const char *format,
5233 MSVCRT__locale_t locale, __ms_va_list valist)
5235 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5238 /*********************************************************************
5239 * _vfwprintf_s_l (MSVCRT.@)
5241 int CDECL MSVCRT__vfwprintf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5242 MSVCRT__locale_t locale, __ms_va_list valist)
5244 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5247 /*********************************************************************
5248 * vfprintf (MSVCRT.@)
5250 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5252 return vfprintf_helper(0, file, format, NULL, valist);
5255 /*********************************************************************
5256 * vfprintf_s (MSVCRT.@)
5258 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5260 return MSVCRT__vfprintf_s_l(file, format, NULL, valist);
5263 /*********************************************************************
5264 * vfwprintf (MSVCRT.@)
5266 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5268 return vfwprintf_helper(0, file, format, NULL, valist);
5271 /*********************************************************************
5272 * vfwprintf_s (MSVCRT.@)
5274 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5276 return MSVCRT__vfwprintf_s_l(file, format, NULL, valist);
5279 #if _MSVCR_VER >= 140
5281 /*********************************************************************
5282 * __stdio_common_vfprintf (UCRTBASE.@)
5284 int CDECL MSVCRT__stdio_common_vfprintf(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
5285 MSVCRT__locale_t locale, __ms_va_list valist)
5287 if (options & ~UCRTBASE_PRINTF_MASK)
5288 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5290 return vfprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
5293 /*********************************************************************
5294 * __stdio_common_vfprintf_s (UCRTBASE.@)
5296 int CDECL MSVCRT__stdio_common_vfprintf_s(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
5297 MSVCRT__locale_t locale, __ms_va_list valist)
5299 if (options & ~UCRTBASE_PRINTF_MASK)
5300 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5302 return vfprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5303 file, format, locale, valist);
5306 /*********************************************************************
5307 * __stdio_common_vfwprintf (UCRTBASE.@)
5309 int CDECL MSVCRT__stdio_common_vfwprintf(unsigned __int64 options, MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
5310 MSVCRT__locale_t locale, __ms_va_list valist)
5312 if (options & ~UCRTBASE_PRINTF_MASK)
5313 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5315 return vfwprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
5318 /*********************************************************************
5319 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5321 int CDECL MSVCRT__stdio_common_vfwprintf_s(unsigned __int64 options, MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
5322 MSVCRT__locale_t locale, __ms_va_list valist)
5324 if (options & ~UCRTBASE_PRINTF_MASK)
5325 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5327 return vfwprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5328 file, format, locale, valist);
5331 #endif /* _MSVCR_VER >= 140 */
5333 /*********************************************************************
5334 * _vfprintf_l (MSVCRT.@)
5336 int CDECL MSVCRT__vfprintf_l(MSVCRT_FILE* file, const char *format,
5337 MSVCRT__locale_t locale, __ms_va_list valist)
5339 return vfprintf_helper(0, file, format, locale, valist);
5342 /*********************************************************************
5343 * _vfwprintf_l (MSVCRT.@)
5345 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5346 MSVCRT__locale_t locale, __ms_va_list valist)
5348 return vfwprintf_helper(0, file, format, locale, valist);
5351 /*********************************************************************
5352 * _vfprintf_p_l (MSVCRT.@)
5354 int CDECL MSVCRT__vfprintf_p_l(MSVCRT_FILE* file, const char *format,
5355 MSVCRT__locale_t locale, __ms_va_list valist)
5357 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5358 file, format, locale, valist);
5361 /*********************************************************************
5362 * _vfprintf_p (MSVCRT.@)
5364 int CDECL MSVCRT__vfprintf_p(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5366 return MSVCRT__vfprintf_p_l(file, format, NULL, valist);
5369 /*********************************************************************
5370 * _vfwprintf_p_l (MSVCRT.@)
5372 int CDECL MSVCRT__vfwprintf_p_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5373 MSVCRT__locale_t locale, __ms_va_list valist)
5375 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5376 file, format, locale, valist);
5379 /*********************************************************************
5380 * _vfwprintf_p (MSVCRT.@)
5382 int CDECL MSVCRT__vfwprintf_p(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5384 return MSVCRT__vfwprintf_p_l(file, format, NULL, valist);
5387 /*********************************************************************
5388 * vprintf (MSVCRT.@)
5390 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
5392 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
5395 /*********************************************************************
5396 * vprintf_s (MSVCRT.@)
5398 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
5400 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
5403 /*********************************************************************
5404 * vwprintf (MSVCRT.@)
5406 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
5408 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
5411 /*********************************************************************
5412 * vwprintf_s (MSVCRT.@)
5414 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
5416 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
5419 /*********************************************************************
5420 * fprintf (MSVCRT.@)
5422 int WINAPIV MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
5424 __ms_va_list valist;
5425 int res;
5426 __ms_va_start(valist, format);
5427 res = MSVCRT_vfprintf(file, format, valist);
5428 __ms_va_end(valist);
5429 return res;
5432 /*********************************************************************
5433 * fprintf_s (MSVCRT.@)
5435 int WINAPIV MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
5437 __ms_va_list valist;
5438 int res;
5439 __ms_va_start(valist, format);
5440 res = MSVCRT_vfprintf_s(file, format, valist);
5441 __ms_va_end(valist);
5442 return res;
5445 /*********************************************************************
5446 * fwprintf (MSVCRT.@)
5448 int WINAPIV MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5450 __ms_va_list valist;
5451 int res;
5452 __ms_va_start(valist, format);
5453 res = MSVCRT_vfwprintf(file, format, valist);
5454 __ms_va_end(valist);
5455 return res;
5458 /*********************************************************************
5459 * fwprintf_s (MSVCRT.@)
5461 int WINAPIV MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5463 __ms_va_list valist;
5464 int res;
5465 __ms_va_start(valist, format);
5466 res = MSVCRT_vfwprintf_s(file, format, valist);
5467 __ms_va_end(valist);
5468 return res;
5471 /*********************************************************************
5472 * _fwprintf_l (MSVCRT.@)
5474 int WINAPIV MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
5476 __ms_va_list valist;
5477 int res;
5478 __ms_va_start(valist, locale);
5479 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
5480 __ms_va_end(valist);
5481 return res;
5484 /*********************************************************************
5485 * printf (MSVCRT.@)
5487 int WINAPIV MSVCRT_printf(const char *format, ...)
5489 __ms_va_list valist;
5490 int res;
5491 __ms_va_start(valist, format);
5492 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
5493 __ms_va_end(valist);
5494 return res;
5497 /*********************************************************************
5498 * printf_s (MSVCRT.@)
5500 int WINAPIV MSVCRT_printf_s(const char *format, ...)
5502 __ms_va_list valist;
5503 int res;
5504 __ms_va_start(valist, format);
5505 res = MSVCRT_vprintf_s(format, valist);
5506 __ms_va_end(valist);
5507 return res;
5510 /*********************************************************************
5511 * ungetc (MSVCRT.@)
5513 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
5515 int ret;
5517 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5519 MSVCRT__lock_file(file);
5520 ret = MSVCRT__ungetc_nolock(c, file);
5521 MSVCRT__unlock_file(file);
5523 return ret;
5526 /*********************************************************************
5527 * _ungetc_nolock (MSVCRT.@)
5529 int CDECL MSVCRT__ungetc_nolock(int c, MSVCRT_FILE * file)
5531 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5533 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
5534 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
5535 return MSVCRT_EOF;
5537 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
5538 && msvcrt_alloc_buffer(file))
5539 || (!file->_cnt && file->_ptr==file->_base))
5540 file->_ptr++;
5542 if(file->_ptr>file->_base) {
5543 file->_ptr--;
5544 if(file->_flag & MSVCRT__IOSTRG) {
5545 if(*file->_ptr != c) {
5546 file->_ptr++;
5547 return MSVCRT_EOF;
5549 }else {
5550 *file->_ptr = c;
5552 file->_cnt++;
5553 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
5554 file->_flag |= MSVCRT__IOREAD;
5555 return c;
5558 return MSVCRT_EOF;
5561 /*********************************************************************
5562 * ungetwc (MSVCRT.@)
5564 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5566 MSVCRT_wint_t ret;
5568 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5570 MSVCRT__lock_file(file);
5571 ret = MSVCRT__ungetwc_nolock(wc, file);
5572 MSVCRT__unlock_file(file);
5574 return ret;
5577 /*********************************************************************
5578 * _ungetwc_nolock (MSVCRT.@)
5580 MSVCRT_wint_t CDECL MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5582 MSVCRT_wchar_t mwc = wc;
5584 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5585 if (wc == MSVCRT_WEOF)
5586 return MSVCRT_WEOF;
5588 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5589 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5590 unsigned char * pp = (unsigned char *)&mwc;
5591 int i;
5593 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
5594 if(pp[i] != MSVCRT__ungetc_nolock(pp[i],file))
5595 return MSVCRT_WEOF;
5597 }else {
5598 char mbs[MSVCRT_MB_LEN_MAX];
5599 int len;
5601 len = MSVCRT_wctomb(mbs, mwc);
5602 if(len == -1)
5603 return MSVCRT_WEOF;
5605 for(len--; len>=0; len--) {
5606 if(mbs[len] != MSVCRT__ungetc_nolock(mbs[len], file))
5607 return MSVCRT_WEOF;
5611 return mwc;
5614 /*********************************************************************
5615 * wprintf (MSVCRT.@)
5617 int WINAPIV MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
5619 __ms_va_list valist;
5620 int res;
5621 __ms_va_start(valist, format);
5622 res = MSVCRT_vwprintf(format, valist);
5623 __ms_va_end(valist);
5624 return res;
5627 /*********************************************************************
5628 * wprintf_s (MSVCRT.@)
5630 int WINAPIV MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
5632 __ms_va_list valist;
5633 int res;
5634 __ms_va_start(valist, format);
5635 res = MSVCRT_vwprintf_s(format, valist);
5636 __ms_va_end(valist);
5637 return res;
5640 /*********************************************************************
5641 * _getmaxstdio (MSVCRT.@)
5643 int CDECL MSVCRT__getmaxstdio(void)
5645 return MSVCRT_max_streams;
5648 /*********************************************************************
5649 * _setmaxstdio (MSVCRT.@)
5651 int CDECL MSVCRT__setmaxstdio(int newmax)
5653 TRACE("%d\n", newmax);
5655 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5656 return -1;
5658 MSVCRT_max_streams = newmax;
5659 return MSVCRT_max_streams;
5662 #if _MSVCR_VER >= 140
5663 /*********************************************************************
5664 * _get_stream_buffer_pointers (UCRTBASE.@)
5666 int CDECL MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE *file, char*** base,
5667 char*** ptr, int** count)
5669 if (base)
5670 *base = &file->_base;
5671 if (ptr)
5672 *ptr = &file->_ptr;
5673 if (count)
5674 *count = &file->_cnt;
5675 return 0;
5677 #endif