winedump: Consistently print hex numbers with leading zeros and 'h' suffix.
[wine.git] / dlls / msvcrt / file.c
blob6b295cf36193f89ad0d8bd7212e2c2e0b1f21283
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 #if _MSVCR_VER >= 140
790 /*********************************************************************
791 * __acrt_iob_func(UCRTBASE.@)
793 MSVCRT_FILE * CDECL MSVCRT___acrt_iob_func(unsigned idx)
795 return &MSVCRT__iob[idx];
797 #endif
799 /*********************************************************************
800 * _access (MSVCRT.@)
802 int CDECL MSVCRT__access(const char *filename, int mode)
804 DWORD attr = GetFileAttributesA(filename);
806 TRACE("(%s,%d) %d\n",filename,mode,attr);
808 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
810 msvcrt_set_errno(GetLastError());
811 return -1;
813 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
815 msvcrt_set_errno(ERROR_ACCESS_DENIED);
816 return -1;
818 return 0;
821 /*********************************************************************
822 * _access_s (MSVCRT.@)
824 int CDECL MSVCRT__access_s(const char *filename, int mode)
826 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
827 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
829 if (MSVCRT__access(filename, mode) == -1)
830 return *MSVCRT__errno();
831 return 0;
834 /*********************************************************************
835 * _waccess (MSVCRT.@)
837 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
839 DWORD attr = GetFileAttributesW(filename);
841 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
843 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
845 msvcrt_set_errno(GetLastError());
846 return -1;
848 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
850 msvcrt_set_errno(ERROR_ACCESS_DENIED);
851 return -1;
853 return 0;
856 /*********************************************************************
857 * _waccess_s (MSVCRT.@)
859 int CDECL MSVCRT__waccess_s(const MSVCRT_wchar_t *filename, int mode)
861 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
862 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
864 if (MSVCRT__waccess(filename, mode) == -1)
865 return *MSVCRT__errno();
866 return 0;
869 /*********************************************************************
870 * _chmod (MSVCRT.@)
872 int CDECL MSVCRT__chmod(const char *path, int flags)
874 DWORD oldFlags = GetFileAttributesA(path);
876 if (oldFlags != INVALID_FILE_ATTRIBUTES)
878 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
879 oldFlags | FILE_ATTRIBUTE_READONLY;
881 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
882 return 0;
884 msvcrt_set_errno(GetLastError());
885 return -1;
888 /*********************************************************************
889 * _wchmod (MSVCRT.@)
891 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
893 DWORD oldFlags = GetFileAttributesW(path);
895 if (oldFlags != INVALID_FILE_ATTRIBUTES)
897 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
898 oldFlags | FILE_ATTRIBUTE_READONLY;
900 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
901 return 0;
903 msvcrt_set_errno(GetLastError());
904 return -1;
907 /*********************************************************************
908 * _unlink (MSVCRT.@)
910 int CDECL MSVCRT__unlink(const char *path)
912 TRACE("%s\n",debugstr_a(path));
913 if(DeleteFileA(path))
914 return 0;
915 TRACE("failed (%d)\n",GetLastError());
916 msvcrt_set_errno(GetLastError());
917 return -1;
920 /*********************************************************************
921 * _wunlink (MSVCRT.@)
923 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
925 TRACE("(%s)\n",debugstr_w(path));
926 if(DeleteFileW(path))
927 return 0;
928 TRACE("failed (%d)\n",GetLastError());
929 msvcrt_set_errno(GetLastError());
930 return -1;
933 /*********************************************************************
934 * _commit (MSVCRT.@)
936 int CDECL MSVCRT__commit(int fd)
938 ioinfo *info = get_ioinfo(fd);
939 int ret;
941 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
943 if (info->handle == INVALID_HANDLE_VALUE)
944 ret = -1;
945 else if (!FlushFileBuffers(info->handle))
947 if (GetLastError() == ERROR_INVALID_HANDLE)
949 /* FlushFileBuffers fails for console handles
950 * so we ignore this error.
952 ret = 0;
954 else
956 TRACE(":failed-last error (%d)\n",GetLastError());
957 msvcrt_set_errno(GetLastError());
958 ret = -1;
961 else
963 TRACE(":ok\n");
964 ret = 0;
967 release_ioinfo(info);
968 return ret;
971 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
972 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
974 /* INTERNAL: Flush all stream buffer */
975 static int msvcrt_flush_all_buffers(int mask)
977 int i, num_flushed = 0;
978 MSVCRT_FILE *file;
980 LOCK_FILES();
981 for (i = 0; i < MSVCRT_stream_idx; i++) {
982 file = msvcrt_get_file(i);
984 if (file->_flag)
986 if(file->_flag & mask) {
987 MSVCRT_fflush(file);
988 num_flushed++;
992 UNLOCK_FILES();
994 TRACE(":flushed (%d) handles\n",num_flushed);
995 return num_flushed;
998 /*********************************************************************
999 * _flushall (MSVCRT.@)
1001 int CDECL MSVCRT__flushall(void)
1003 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
1006 /*********************************************************************
1007 * fflush (MSVCRT.@)
1009 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
1011 int ret;
1013 if(!file) {
1014 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
1015 ret = 0;
1016 } else {
1017 MSVCRT__lock_file(file);
1018 ret = MSVCRT__fflush_nolock(file);
1019 MSVCRT__unlock_file(file);
1022 return ret;
1025 /*********************************************************************
1026 * _fflush_nolock (MSVCRT.@)
1028 int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file)
1030 int res;
1032 if(!file) {
1033 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
1034 return 0;
1037 res = msvcrt_flush_buffer(file);
1038 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
1039 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
1040 return res;
1043 /*********************************************************************
1044 * _close (MSVCRT.@)
1046 int CDECL MSVCRT__close(int fd)
1048 ioinfo *info = get_ioinfo(fd);
1049 int ret;
1051 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1053 if (fd == MSVCRT_NO_CONSOLE_FD) {
1054 *MSVCRT__errno() = MSVCRT_EBADF;
1055 ret = -1;
1056 } else if (!MSVCRT_CHECK_PMT_ERR(info->wxflag & WX_OPEN, MSVCRT_EBADF)) {
1057 ret = -1;
1058 } else if (fd == MSVCRT_STDOUT_FILENO &&
1059 info->handle == get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle) {
1060 msvcrt_free_fd(fd);
1061 ret = 0;
1062 } else if (fd == MSVCRT_STDERR_FILENO &&
1063 info->handle == get_ioinfo_nolock(MSVCRT_STDOUT_FILENO)->handle) {
1064 msvcrt_free_fd(fd);
1065 ret = 0;
1066 } else {
1067 ret = CloseHandle(info->handle) ? 0 : -1;
1068 msvcrt_free_fd(fd);
1069 if (ret) {
1070 WARN(":failed-last error (%d)\n",GetLastError());
1071 msvcrt_set_errno(GetLastError());
1074 release_ioinfo(info);
1075 return ret;
1078 /*********************************************************************
1079 * _dup2 (MSVCRT.@)
1080 * NOTES
1081 * MSDN isn't clear on this point, but the remarks for _pipe
1082 * indicate file descriptors duplicated with _dup and _dup2 are always
1083 * inheritable.
1085 int CDECL MSVCRT__dup2(int od, int nd)
1087 ioinfo *info_od, *info_nd;
1088 int ret;
1090 TRACE("(od=%d, nd=%d)\n", od, nd);
1092 if (od < nd)
1094 info_od = get_ioinfo(od);
1095 info_nd = get_ioinfo_alloc_fd(nd);
1097 else
1099 info_nd = get_ioinfo_alloc_fd(nd);
1100 info_od = get_ioinfo(od);
1103 if (info_nd == &MSVCRT___badioinfo)
1105 ret = -1;
1107 else if (info_od->wxflag & WX_OPEN)
1109 HANDLE handle;
1111 if (DuplicateHandle(GetCurrentProcess(), info_od->handle,
1112 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1114 int wxflag = info_od->wxflag & ~MSVCRT__O_NOINHERIT;
1116 if (info_nd->wxflag & WX_OPEN)
1117 MSVCRT__close(nd);
1119 msvcrt_set_fd(info_nd, handle, wxflag);
1120 /* _dup2 returns 0, not nd, on success */
1121 ret = 0;
1123 else
1125 ret = -1;
1126 msvcrt_set_errno(GetLastError());
1129 else
1131 *MSVCRT__errno() = MSVCRT_EBADF;
1132 ret = -1;
1135 release_ioinfo(info_od);
1136 release_ioinfo(info_nd);
1137 return ret;
1140 /*********************************************************************
1141 * _dup (MSVCRT.@)
1143 int CDECL MSVCRT__dup(int od)
1145 int fd, ret;
1146 ioinfo *info = get_ioinfo_alloc(&fd);
1148 if (MSVCRT__dup2(od, fd) == 0)
1149 ret = fd;
1150 else
1151 ret = -1;
1152 release_ioinfo(info);
1153 return ret;
1156 /*********************************************************************
1157 * _eof (MSVCRT.@)
1159 int CDECL MSVCRT__eof(int fd)
1161 ioinfo *info = get_ioinfo(fd);
1162 DWORD curpos,endpos;
1163 LONG hcurpos,hendpos;
1165 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1167 if (info->handle == INVALID_HANDLE_VALUE)
1169 release_ioinfo(info);
1170 return -1;
1173 if (info->wxflag & WX_ATEOF)
1175 release_ioinfo(info);
1176 return TRUE;
1179 /* Otherwise we do it the hard way */
1180 hcurpos = hendpos = 0;
1181 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1182 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1184 if (curpos == endpos && hcurpos == hendpos)
1186 /* FIXME: shouldn't WX_ATEOF be set here? */
1187 release_ioinfo(info);
1188 return TRUE;
1191 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1192 release_ioinfo(info);
1193 return FALSE;
1196 /*********************************************************************
1197 * _fcloseall (MSVCRT.@)
1199 int CDECL MSVCRT__fcloseall(void)
1201 int num_closed = 0, i;
1202 MSVCRT_FILE *file;
1204 LOCK_FILES();
1205 for (i = 3; i < MSVCRT_stream_idx; i++) {
1206 file = msvcrt_get_file(i);
1208 if (file->_flag && !MSVCRT_fclose(file))
1209 num_closed++;
1211 UNLOCK_FILES();
1213 TRACE(":closed (%d) handles\n",num_closed);
1214 return num_closed;
1217 /* free everything on process exit */
1218 void msvcrt_free_io(void)
1220 unsigned int i;
1221 int j;
1223 MSVCRT__flushall();
1224 MSVCRT__fcloseall();
1226 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1228 if(!MSVCRT___pioinfo[i])
1229 continue;
1231 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1233 if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
1234 DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
1236 MSVCRT_free(MSVCRT___pioinfo[i]);
1239 for(j=0; j<MSVCRT_stream_idx; j++)
1241 MSVCRT_FILE *file = msvcrt_get_file(j);
1242 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1244 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1245 DeleteCriticalSection(&((file_crit*)file)->crit);
1249 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1250 MSVCRT_free(MSVCRT_fstream[i]);
1253 /*********************************************************************
1254 * _lseeki64 (MSVCRT.@)
1256 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1258 ioinfo *info = get_ioinfo(fd);
1259 LARGE_INTEGER ofs;
1261 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1263 if (info->handle == INVALID_HANDLE_VALUE)
1265 release_ioinfo(info);
1266 return -1;
1269 if (whence < 0 || whence > 2)
1271 release_ioinfo(info);
1272 *MSVCRT__errno() = MSVCRT_EINVAL;
1273 return -1;
1276 TRACE(":fd (%d) to %s pos %s\n",
1277 fd,wine_dbgstr_longlong(offset),
1278 (whence==MSVCRT_SEEK_SET)?"SEEK_SET":
1279 (whence==MSVCRT_SEEK_CUR)?"SEEK_CUR":
1280 (whence==MSVCRT_SEEK_END)?"SEEK_END":"UNKNOWN");
1282 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1283 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1284 ofs.QuadPart = offset;
1285 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1286 GetLastError() == ERROR_SUCCESS)
1288 info->wxflag &= ~WX_ATEOF;
1289 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1291 release_ioinfo(info);
1292 return ofs.QuadPart;
1294 release_ioinfo(info);
1295 TRACE(":error-last error (%d)\n",GetLastError());
1296 msvcrt_set_errno(GetLastError());
1297 return -1;
1300 /*********************************************************************
1301 * _lseek (MSVCRT.@)
1303 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1305 return MSVCRT__lseeki64(fd, offset, whence);
1308 /*********************************************************************
1309 * _lock_file (MSVCRT.@)
1311 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1313 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1314 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1315 else
1316 EnterCriticalSection(&((file_crit*)file)->crit);
1319 /*********************************************************************
1320 * _unlock_file (MSVCRT.@)
1322 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1324 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1325 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1326 else
1327 LeaveCriticalSection(&((file_crit*)file)->crit);
1330 /*********************************************************************
1331 * _locking (MSVCRT.@)
1333 * This is untested; the underlying LockFile doesn't work yet.
1335 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1337 ioinfo *info = get_ioinfo(fd);
1338 BOOL ret;
1339 DWORD cur_locn;
1341 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1342 if (info->handle == INVALID_HANDLE_VALUE)
1344 release_ioinfo(info);
1345 return -1;
1348 if (mode < 0 || mode > 4)
1350 release_ioinfo(info);
1351 *MSVCRT__errno() = MSVCRT_EINVAL;
1352 return -1;
1355 TRACE(":fd (%d) by 0x%08x mode %s\n",
1356 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1357 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1358 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1359 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1360 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1361 "UNKNOWN");
1363 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1365 release_ioinfo(info);
1366 FIXME ("Seek failed\n");
1367 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1368 return -1;
1370 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1372 int nretry = 10;
1373 ret = 1; /* just to satisfy gcc */
1374 while (nretry--)
1376 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1377 if (ret) break;
1378 Sleep(1);
1381 else if (mode == MSVCRT__LK_UNLCK)
1382 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1383 else
1384 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1385 /* FIXME - what about error settings? */
1386 release_ioinfo(info);
1387 return ret ? 0 : -1;
1390 /*********************************************************************
1391 * _fseeki64 (MSVCRT.@)
1393 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1395 int ret;
1397 MSVCRT__lock_file(file);
1398 ret = MSVCRT__fseeki64_nolock(file, offset, whence);
1399 MSVCRT__unlock_file(file);
1401 return ret;
1404 /*********************************************************************
1405 * _fseeki64_nolock (MSVCRT.@)
1407 int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
1409 int ret;
1411 if(whence == MSVCRT_SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1412 whence = MSVCRT_SEEK_SET;
1413 offset += MSVCRT__ftelli64_nolock(file);
1416 /* Flush output if needed */
1417 msvcrt_flush_buffer(file);
1418 /* Reset direction of i/o */
1419 if(file->_flag & MSVCRT__IORW) {
1420 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1422 /* Clear end of file flag */
1423 file->_flag &= ~MSVCRT__IOEOF;
1424 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1426 return ret;
1429 /*********************************************************************
1430 * fseek (MSVCRT.@)
1432 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1434 return MSVCRT__fseeki64( file, offset, whence );
1437 /*********************************************************************
1438 * _fseek_nolock (MSVCRT.@)
1440 int CDECL MSVCRT__fseek_nolock(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1442 return MSVCRT__fseeki64_nolock( file, offset, whence );
1445 /*********************************************************************
1446 * _chsize_s (MSVCRT.@)
1448 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1450 ioinfo *info;
1451 __int64 cur, pos;
1452 BOOL ret = FALSE;
1454 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1456 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1459 info = get_ioinfo(fd);
1460 if (info->handle != INVALID_HANDLE_VALUE)
1462 /* save the current file pointer */
1463 cur = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1464 if (cur >= 0)
1466 pos = MSVCRT__lseeki64(fd, size, MSVCRT_SEEK_SET);
1467 if (pos >= 0)
1469 ret = SetEndOfFile(info->handle);
1470 if (!ret) msvcrt_set_errno(GetLastError());
1473 /* restore the file pointer */
1474 MSVCRT__lseeki64(fd, cur, MSVCRT_SEEK_SET);
1478 release_ioinfo(info);
1479 return ret ? 0 : *MSVCRT__errno();
1482 /*********************************************************************
1483 * _chsize (MSVCRT.@)
1485 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1487 /* _chsize_s returns errno on failure but _chsize should return -1 */
1488 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1491 /*********************************************************************
1492 * clearerr (MSVCRT.@)
1494 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1496 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1498 MSVCRT__lock_file(file);
1499 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1500 MSVCRT__unlock_file(file);
1503 /*********************************************************************
1504 * clearerr_s (MSVCRT.@)
1506 int CDECL MSVCRT_clearerr_s(MSVCRT_FILE* file)
1508 TRACE(":file (%p)\n",file);
1510 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
1512 MSVCRT__lock_file(file);
1513 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1514 MSVCRT__unlock_file(file);
1515 return 0;
1518 /*********************************************************************
1519 * rewind (MSVCRT.@)
1521 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1523 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1525 MSVCRT__lock_file(file);
1526 MSVCRT__fseek_nolock(file, 0L, MSVCRT_SEEK_SET);
1527 MSVCRT_clearerr(file);
1528 MSVCRT__unlock_file(file);
1531 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1533 int plus = strchrW(mode, '+') != NULL;
1535 TRACE("%s\n", debugstr_w(mode));
1537 while(*mode == ' ') mode++;
1539 switch(*mode++)
1541 case 'R': case 'r':
1542 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1543 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1544 break;
1545 case 'W': case 'w':
1546 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1547 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1548 break;
1549 case 'A': case 'a':
1550 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1551 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1552 break;
1553 default:
1554 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1555 return -1;
1558 *stream_flags |= MSVCRT__commode;
1560 while (*mode && *mode!=',')
1561 switch (*mode++)
1563 case 'B': case 'b':
1564 *open_flags |= MSVCRT__O_BINARY;
1565 *open_flags &= ~MSVCRT__O_TEXT;
1566 break;
1567 case 't':
1568 *open_flags |= MSVCRT__O_TEXT;
1569 *open_flags &= ~MSVCRT__O_BINARY;
1570 break;
1571 case 'D':
1572 *open_flags |= MSVCRT__O_TEMPORARY;
1573 break;
1574 case 'T':
1575 *open_flags |= MSVCRT__O_SHORT_LIVED;
1576 break;
1577 case 'c':
1578 *stream_flags |= MSVCRT__IOCOMMIT;
1579 break;
1580 case 'n':
1581 *stream_flags &= ~MSVCRT__IOCOMMIT;
1582 break;
1583 case 'N':
1584 *open_flags |= MSVCRT__O_NOINHERIT;
1585 break;
1586 case '+':
1587 case ' ':
1588 case 'a':
1589 case 'w':
1590 break;
1591 case 'S':
1592 case 'R':
1593 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1594 break;
1595 default:
1596 ERR("incorrect mode flag: %c\n", mode[-1]);
1597 break;
1600 if(*mode == ',')
1602 static const WCHAR ccs[] = {'c','c','s'};
1603 static const WCHAR utf8[] = {'u','t','f','-','8'};
1604 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1605 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1607 mode++;
1608 while(*mode == ' ') mode++;
1609 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1610 return -1;
1611 mode += sizeof(ccs)/sizeof(ccs[0]);
1612 while(*mode == ' ') mode++;
1613 if(!MSVCRT_CHECK_PMT(*mode == '='))
1614 return -1;
1615 mode++;
1616 while(*mode == ' ') mode++;
1618 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1620 *open_flags |= MSVCRT__O_U8TEXT;
1621 mode += sizeof(utf8)/sizeof(utf8[0]);
1623 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1625 *open_flags |= MSVCRT__O_U16TEXT;
1626 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1628 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1630 *open_flags |= MSVCRT__O_WTEXT;
1631 mode += sizeof(unicode)/sizeof(unicode[0]);
1633 else
1635 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1636 return -1;
1639 while(*mode == ' ') mode++;
1642 if(!MSVCRT_CHECK_PMT(*mode == 0))
1643 return -1;
1644 return 0;
1647 /*********************************************************************
1648 * _fdopen (MSVCRT.@)
1650 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1652 MSVCRT_FILE *ret;
1653 MSVCRT_wchar_t *modeW = NULL;
1655 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1657 ret = MSVCRT__wfdopen(fd, modeW);
1659 MSVCRT_free(modeW);
1660 return ret;
1663 /*********************************************************************
1664 * _wfdopen (MSVCRT.@)
1666 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1668 int open_flags, stream_flags;
1669 MSVCRT_FILE* file;
1671 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1673 LOCK_FILES();
1674 if (!(file = msvcrt_alloc_fp()))
1675 file = NULL;
1676 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1678 file->_flag = 0;
1679 file = NULL;
1681 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1682 UNLOCK_FILES();
1684 return file;
1687 /*********************************************************************
1688 * _filelength (MSVCRT.@)
1690 LONG CDECL MSVCRT__filelength(int fd)
1692 LONG curPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
1693 if (curPos != -1)
1695 LONG endPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_END);
1696 if (endPos != -1)
1698 if (endPos != curPos)
1699 MSVCRT__lseek(fd, curPos, MSVCRT_SEEK_SET);
1700 return endPos;
1703 return -1;
1706 /*********************************************************************
1707 * _filelengthi64 (MSVCRT.@)
1709 __int64 CDECL MSVCRT__filelengthi64(int fd)
1711 __int64 curPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1712 if (curPos != -1)
1714 __int64 endPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_END);
1715 if (endPos != -1)
1717 if (endPos != curPos)
1718 MSVCRT__lseeki64(fd, curPos, MSVCRT_SEEK_SET);
1719 return endPos;
1722 return -1;
1725 /*********************************************************************
1726 * _fileno (MSVCRT.@)
1728 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1730 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1731 return file->_file;
1734 /*********************************************************************
1735 * _fstat64 (MSVCRT.@)
1737 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1739 ioinfo *info = get_ioinfo(fd);
1740 DWORD dw;
1741 DWORD type;
1742 BY_HANDLE_FILE_INFORMATION hfi;
1744 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1745 if (info->handle == INVALID_HANDLE_VALUE)
1747 release_ioinfo(info);
1748 return -1;
1751 if (!buf)
1753 WARN(":failed-NULL buf\n");
1754 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1755 release_ioinfo(info);
1756 return -1;
1759 memset(&hfi, 0, sizeof(hfi));
1760 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1761 type = GetFileType(info->handle);
1762 if (type == FILE_TYPE_PIPE)
1764 buf->st_dev = buf->st_rdev = fd;
1765 buf->st_mode = MSVCRT__S_IFIFO;
1766 buf->st_nlink = 1;
1768 else if (type == FILE_TYPE_CHAR)
1770 buf->st_dev = buf->st_rdev = fd;
1771 buf->st_mode = MSVCRT__S_IFCHR;
1772 buf->st_nlink = 1;
1774 else /* FILE_TYPE_DISK etc. */
1776 if (!GetFileInformationByHandle(info->handle, &hfi))
1778 WARN(":failed-last error (%d)\n",GetLastError());
1779 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1780 release_ioinfo(info);
1781 return -1;
1783 buf->st_mode = MSVCRT__S_IFREG | 0444;
1784 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1785 buf->st_mode |= 0222;
1786 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1787 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1788 buf->st_atime = dw;
1789 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1790 buf->st_mtime = buf->st_ctime = dw;
1791 buf->st_nlink = hfi.nNumberOfLinks;
1793 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1794 buf->st_mode);
1795 release_ioinfo(info);
1796 return 0;
1799 /*********************************************************************
1800 * _fstati64 (MSVCRT.@)
1802 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1804 int ret;
1805 struct MSVCRT__stat64 buf64;
1807 ret = MSVCRT__fstat64(fd, &buf64);
1808 if (!ret)
1809 msvcrt_stat64_to_stati64(&buf64, buf);
1810 return ret;
1813 /*********************************************************************
1814 * _fstat (MSVCRT.@)
1816 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1817 { int ret;
1818 struct MSVCRT__stat64 buf64;
1820 ret = MSVCRT__fstat64(fd, &buf64);
1821 if (!ret)
1822 msvcrt_stat64_to_stat(&buf64, buf);
1823 return ret;
1826 /*********************************************************************
1827 * _fstat32 (MSVCR80.@)
1829 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1831 int ret;
1832 struct MSVCRT__stat64 buf64;
1834 ret = MSVCRT__fstat64(fd, &buf64);
1835 if (!ret)
1836 msvcrt_stat64_to_stat32(&buf64, buf);
1837 return ret;
1840 /*********************************************************************
1841 * _fstat32i64 (MSVCR80.@)
1843 int CDECL MSVCRT__fstat32i64(int fd, struct MSVCRT__stat32i64* buf)
1845 int ret;
1846 struct MSVCRT__stat64 buf64;
1848 ret = MSVCRT__fstat64(fd, &buf64);
1849 if (!ret)
1850 msvcrt_stat64_to_stat32i64(&buf64, buf);
1851 return ret;
1854 /*********************************************************************
1855 * _fstat64i32 (MSVCR80.@)
1857 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1859 int ret;
1860 struct MSVCRT__stat64 buf64;
1862 ret = MSVCRT__fstat64(fd, &buf64);
1863 if (!ret)
1864 msvcrt_stat64_to_stat64i32(&buf64, buf);
1865 return ret;
1868 /*********************************************************************
1869 * _futime64 (MSVCRT.@)
1871 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1873 ioinfo *info = get_ioinfo(fd);
1874 FILETIME at, wt;
1876 if (!t)
1878 time_to_filetime( MSVCRT__time64(NULL), &at );
1879 wt = at;
1881 else
1883 time_to_filetime( t->actime, &at );
1884 time_to_filetime( t->modtime, &wt );
1887 if (!SetFileTime(info->handle, NULL, &at, &wt))
1889 release_ioinfo(info);
1890 msvcrt_set_errno(GetLastError());
1891 return -1 ;
1893 release_ioinfo(info);
1894 return 0;
1897 /*********************************************************************
1898 * _futime32 (MSVCRT.@)
1900 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1902 if (t)
1904 struct MSVCRT___utimbuf64 t64;
1905 t64.actime = t->actime;
1906 t64.modtime = t->modtime;
1907 return _futime64( fd, &t64 );
1909 else
1910 return _futime64( fd, NULL );
1913 /*********************************************************************
1914 * _futime (MSVCRT.@)
1916 #ifdef _WIN64
1917 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1919 return _futime64( fd, t );
1921 #else
1922 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1924 return _futime32( fd, t );
1926 #endif
1928 /*********************************************************************
1929 * _get_osfhandle (MSVCRT.@)
1931 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1933 HANDLE hand = get_ioinfo_nolock(fd)->handle;
1934 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1936 if(hand == INVALID_HANDLE_VALUE)
1937 *MSVCRT__errno() = MSVCRT_EBADF;
1938 return (MSVCRT_intptr_t)hand;
1941 /*********************************************************************
1942 * _mktemp_s (MSVCRT.@)
1944 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1946 DWORD len, xno, id;
1948 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1949 return MSVCRT_EINVAL;
1951 for(len=0; len<size; len++)
1952 if(!pattern[len])
1953 break;
1954 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1955 if(size)
1956 pattern[0] = 0;
1957 return MSVCRT_EINVAL;
1960 for(xno=1; xno<=6; xno++)
1961 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1962 return MSVCRT_EINVAL;
1964 id = GetCurrentProcessId();
1965 for(xno=1; xno<6; xno++) {
1966 pattern[len-xno] = id%10 + '0';
1967 id /= 10;
1970 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1971 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1972 return 0;
1975 pattern[0] = 0;
1976 *MSVCRT__errno() = MSVCRT_EEXIST;
1977 return MSVCRT_EEXIST;
1980 /*********************************************************************
1981 * _mktemp (MSVCRT.@)
1983 char * CDECL MSVCRT__mktemp(char *pattern)
1985 int numX = 0;
1986 char *retVal = pattern;
1987 int id;
1988 char letter = 'a';
1990 if(!pattern)
1991 return NULL;
1993 while(*pattern)
1994 numX = (*pattern++ == 'X')? numX + 1 : 0;
1995 if (numX < 6)
1996 return NULL;
1997 pattern--;
1998 id = GetCurrentProcessId();
1999 numX = 6;
2000 while(numX--)
2002 int tempNum = id / 10;
2003 *pattern-- = id - (tempNum * 10) + '0';
2004 id = tempNum;
2006 pattern++;
2009 *pattern = letter++;
2010 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
2011 return retVal;
2012 } while(letter <= 'z');
2013 return NULL;
2016 /*********************************************************************
2017 * _wmktemp_s (MSVCRT.@)
2019 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
2021 DWORD len, xno, id;
2023 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
2024 return MSVCRT_EINVAL;
2026 for(len=0; len<size; len++)
2027 if(!pattern[len])
2028 break;
2029 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
2030 if(size)
2031 pattern[0] = 0;
2032 return MSVCRT_EINVAL;
2035 for(xno=1; xno<=6; xno++)
2036 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
2037 return MSVCRT_EINVAL;
2039 id = GetCurrentProcessId();
2040 for(xno=1; xno<6; xno++) {
2041 pattern[len-xno] = id%10 + '0';
2042 id /= 10;
2045 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
2046 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
2047 return 0;
2050 pattern[0] = 0;
2051 *MSVCRT__errno() = MSVCRT_EEXIST;
2052 return MSVCRT_EEXIST;
2055 /*********************************************************************
2056 * _wmktemp (MSVCRT.@)
2058 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
2060 int numX = 0;
2061 MSVCRT_wchar_t *retVal = pattern;
2062 int id;
2063 MSVCRT_wchar_t letter = 'a';
2065 if(!pattern)
2066 return NULL;
2068 while(*pattern)
2069 numX = (*pattern++ == 'X')? numX + 1 : 0;
2070 if (numX < 6)
2071 return NULL;
2072 pattern--;
2073 id = GetCurrentProcessId();
2074 numX = 6;
2075 while(numX--)
2077 int tempNum = id / 10;
2078 *pattern-- = id - (tempNum * 10) + '0';
2079 id = tempNum;
2081 pattern++;
2084 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
2085 return retVal;
2086 *pattern = letter++;
2087 } while(letter != '|');
2088 return NULL;
2091 static unsigned split_oflags(unsigned oflags)
2093 int wxflags = 0;
2094 unsigned unsupp; /* until we support everything */
2096 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
2097 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
2098 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
2099 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
2100 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
2101 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
2102 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
2103 else wxflags |= WX_TEXT; /* default to TEXT*/
2104 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2106 if ((unsupp = oflags & ~(
2107 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
2108 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
2109 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
2110 MSVCRT__O_NOINHERIT|
2111 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
2112 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
2114 ERR(":unsupported oflags 0x%04x\n",unsupp);
2116 return wxflags;
2119 /*********************************************************************
2120 * _pipe (MSVCRT.@)
2122 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
2124 int ret = -1;
2125 SECURITY_ATTRIBUTES sa;
2126 HANDLE readHandle, writeHandle;
2128 if (!pfds)
2130 *MSVCRT__errno() = MSVCRT_EINVAL;
2131 return -1;
2134 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2135 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
2136 sa.lpSecurityDescriptor = NULL;
2137 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2139 unsigned int wxflags = split_oflags(textmode);
2140 int fd;
2142 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2143 if (fd != -1)
2145 pfds[0] = fd;
2146 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2147 if (fd != -1)
2149 pfds[1] = fd;
2150 ret = 0;
2152 else
2154 MSVCRT__close(pfds[0]);
2155 CloseHandle(writeHandle);
2156 *MSVCRT__errno() = MSVCRT_EMFILE;
2159 else
2161 CloseHandle(readHandle);
2162 CloseHandle(writeHandle);
2163 *MSVCRT__errno() = MSVCRT_EMFILE;
2166 else
2167 msvcrt_set_errno(GetLastError());
2169 return ret;
2172 static int check_bom(HANDLE h, int oflags, BOOL seek)
2174 char bom[sizeof(utf8_bom)];
2175 DWORD r;
2177 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2179 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2180 return oflags;
2182 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2183 oflags |= MSVCRT__O_U8TEXT;
2184 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2185 if (seek && r>2)
2186 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2187 oflags |= MSVCRT__O_U16TEXT;
2188 }else if (seek) {
2189 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2192 return oflags;
2195 /*********************************************************************
2196 * _wsopen_dispatch (UCRTBASE.@)
2198 int CDECL MSVCRT__wsopen_dispatch( const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode,
2199 int *fd, int secure )
2201 DWORD access = 0, creation = 0, attrib;
2202 SECURITY_ATTRIBUTES sa;
2203 DWORD sharing, type;
2204 int wxflag;
2205 HANDLE hand;
2207 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2208 debugstr_w(path), oflags, shflags, pmode, fd, secure);
2210 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2212 *fd = -1;
2213 wxflag = split_oflags(oflags);
2214 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2216 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2217 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2218 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2221 if (oflags & MSVCRT__O_CREAT)
2223 if (secure && !MSVCRT_CHECK_PMT(!(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))))
2224 return MSVCRT_EINVAL;
2226 if (oflags & MSVCRT__O_EXCL)
2227 creation = CREATE_NEW;
2228 else if (oflags & MSVCRT__O_TRUNC)
2229 creation = CREATE_ALWAYS;
2230 else
2231 creation = OPEN_ALWAYS;
2233 else /* no MSVCRT__O_CREAT */
2235 if (oflags & MSVCRT__O_TRUNC)
2236 creation = TRUNCATE_EXISTING;
2237 else
2238 creation = OPEN_EXISTING;
2241 switch( shflags )
2243 case MSVCRT__SH_DENYRW:
2244 sharing = 0L;
2245 break;
2246 case MSVCRT__SH_DENYWR:
2247 sharing = FILE_SHARE_READ;
2248 break;
2249 case MSVCRT__SH_DENYRD:
2250 sharing = FILE_SHARE_WRITE;
2251 break;
2252 case MSVCRT__SH_DENYNO:
2253 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2254 break;
2255 default:
2256 ERR( "Unhandled shflags 0x%x\n", shflags );
2257 return MSVCRT_EINVAL;
2260 if (!(pmode & ~MSVCRT_umask & MSVCRT__S_IWRITE))
2261 attrib = FILE_ATTRIBUTE_READONLY;
2262 else
2263 attrib = FILE_ATTRIBUTE_NORMAL;
2265 if (oflags & MSVCRT__O_TEMPORARY)
2267 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2268 access |= DELETE;
2269 sharing |= FILE_SHARE_DELETE;
2272 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2273 sa.lpSecurityDescriptor = NULL;
2274 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2276 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2277 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2278 && !(access&GENERIC_READ))
2280 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2281 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2282 if (hand != INVALID_HANDLE_VALUE)
2284 oflags = check_bom(hand, oflags, FALSE);
2285 CloseHandle(hand);
2287 else
2288 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2291 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2292 if (hand == INVALID_HANDLE_VALUE) {
2293 WARN(":failed-last error (%d)\n",GetLastError());
2294 msvcrt_set_errno(GetLastError());
2295 return *MSVCRT__errno();
2298 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2300 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2301 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2302 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2304 if (oflags & MSVCRT__O_U8TEXT)
2306 DWORD written = 0, tmp;
2308 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2309 sizeof(utf8_bom)-written, &tmp, NULL))
2310 written += tmp;
2311 if (written != sizeof(utf8_bom)) {
2312 WARN("error writing BOM\n");
2313 CloseHandle(hand);
2314 msvcrt_set_errno(GetLastError());
2315 return *MSVCRT__errno();
2318 else
2320 DWORD written = 0, tmp;
2322 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2323 sizeof(utf16_bom)-written, &tmp, NULL))
2324 written += tmp;
2325 if (written != sizeof(utf16_bom))
2327 WARN("error writing BOM\n");
2328 CloseHandle(hand);
2329 msvcrt_set_errno(GetLastError());
2330 return *MSVCRT__errno();
2334 else if (access & GENERIC_READ)
2335 oflags = check_bom(hand, oflags, TRUE);
2338 type = GetFileType(hand);
2339 if (type == FILE_TYPE_CHAR)
2340 wxflag |= WX_TTY;
2341 else if (type == FILE_TYPE_PIPE)
2342 wxflag |= WX_PIPE;
2344 *fd = msvcrt_alloc_fd(hand, wxflag);
2345 if (*fd == -1)
2346 return *MSVCRT__errno();
2348 if (oflags & MSVCRT__O_WTEXT)
2349 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2350 else if (oflags & MSVCRT__O_U16TEXT)
2351 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
2352 else if (oflags & MSVCRT__O_U8TEXT)
2353 get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
2355 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2356 return 0;
2360 /*********************************************************************
2361 * _wsopen_s (MSVCRT.@)
2363 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2365 return MSVCRT__wsopen_dispatch( path, oflags, shflags, pmode, fd, 1 );
2368 /*********************************************************************
2369 * _wsopen (MSVCRT.@)
2371 int WINAPIV MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2373 int pmode;
2374 int fd;
2376 if (oflags & MSVCRT__O_CREAT)
2378 __ms_va_list ap;
2380 __ms_va_start(ap, shflags);
2381 pmode = va_arg(ap, int);
2382 __ms_va_end(ap);
2384 else
2385 pmode = 0;
2387 return MSVCRT__wsopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2391 /*********************************************************************
2392 * _sopen_dispatch (UCRTBASE.@)
2394 int CDECL MSVCRT__sopen_dispatch( const char *path, int oflags, int shflags,
2395 int pmode, int *fd, int secure)
2397 MSVCRT_wchar_t *pathW;
2398 int ret;
2400 if (!MSVCRT_CHECK_PMT(fd != NULL))
2401 return MSVCRT_EINVAL;
2402 *fd = -1;
2403 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2404 return MSVCRT_EINVAL;
2406 ret = MSVCRT__wsopen_dispatch(pathW, oflags, shflags, pmode, fd, secure);
2407 MSVCRT_free(pathW);
2408 return ret;
2411 /*********************************************************************
2412 * _sopen_s (MSVCRT.@)
2414 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2416 return MSVCRT__sopen_dispatch(path, oflags, shflags, pmode, fd, 1);
2419 /*********************************************************************
2420 * _sopen (MSVCRT.@)
2422 int WINAPIV MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2424 int pmode;
2425 int fd;
2427 if (oflags & MSVCRT__O_CREAT)
2429 __ms_va_list ap;
2431 __ms_va_start(ap, shflags);
2432 pmode = va_arg(ap, int);
2433 __ms_va_end(ap);
2435 else
2436 pmode = 0;
2438 return MSVCRT__sopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2441 /*********************************************************************
2442 * _open (MSVCRT.@)
2444 int WINAPIV MSVCRT__open( const char *path, int flags, ... )
2446 __ms_va_list ap;
2448 if (flags & MSVCRT__O_CREAT)
2450 int pmode;
2451 __ms_va_start(ap, flags);
2452 pmode = va_arg(ap, int);
2453 __ms_va_end(ap);
2454 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2456 else
2457 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2460 /*********************************************************************
2461 * _wopen (MSVCRT.@)
2463 int WINAPIV MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2465 __ms_va_list ap;
2467 if (flags & MSVCRT__O_CREAT)
2469 int pmode;
2470 __ms_va_start(ap, flags);
2471 pmode = va_arg(ap, int);
2472 __ms_va_end(ap);
2473 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2475 else
2476 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2479 /*********************************************************************
2480 * _creat (MSVCRT.@)
2482 int CDECL MSVCRT__creat(const char *path, int pmode)
2484 int flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | MSVCRT__O_RDWR;
2485 return MSVCRT__open(path, flags, pmode);
2488 /*********************************************************************
2489 * _wcreat (MSVCRT.@)
2491 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int pmode)
2493 int flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | MSVCRT__O_RDWR;
2494 return MSVCRT__wopen(path, flags, pmode);
2497 /*********************************************************************
2498 * _open_osfhandle (MSVCRT.@)
2500 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2502 DWORD flags;
2503 int fd;
2505 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2506 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2507 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2508 * text - it never sets MSVCRT__O_BINARY.
2510 /* don't let split_oflags() decide the mode if no mode is passed */
2511 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2512 oflags |= MSVCRT__O_BINARY;
2514 flags = GetFileType((HANDLE)handle);
2515 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2517 msvcrt_set_errno(GetLastError());
2518 return -1;
2521 if (flags == FILE_TYPE_CHAR)
2522 flags = WX_TTY;
2523 else if (flags == FILE_TYPE_PIPE)
2524 flags = WX_PIPE;
2525 else
2526 flags = 0;
2527 flags |= split_oflags(oflags);
2529 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2530 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2531 return fd;
2534 /*********************************************************************
2535 * _rmtmp (MSVCRT.@)
2537 int CDECL MSVCRT__rmtmp(void)
2539 int num_removed = 0, i;
2540 MSVCRT_FILE *file;
2542 LOCK_FILES();
2543 for (i = 3; i < MSVCRT_stream_idx; i++) {
2544 file = msvcrt_get_file(i);
2546 if (file->_tmpfname)
2548 MSVCRT_fclose(file);
2549 num_removed++;
2552 UNLOCK_FILES();
2554 if (num_removed)
2555 TRACE(":removed (%d) temp files\n",num_removed);
2556 return num_removed;
2559 static inline int get_utf8_char_len(char ch)
2561 if((ch&0xf8) == 0xf0)
2562 return 4;
2563 else if((ch&0xf0) == 0xe0)
2564 return 3;
2565 else if((ch&0xe0) == 0xc0)
2566 return 2;
2567 return 1;
2570 /*********************************************************************
2571 * (internal) read_utf8
2573 static int read_utf8(ioinfo *fdinfo, MSVCRT_wchar_t *buf, unsigned int count)
2575 HANDLE hand = fdinfo->handle;
2576 char min_buf[4], *readbuf, lookahead;
2577 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2579 /* make the buffer big enough to hold at least one character */
2580 /* read bytes have to fit to output and lookahead buffers */
2581 count /= 2;
2582 readbuf_size = count < 4 ? 4 : count;
2583 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2584 readbuf_size = 4;
2585 readbuf = min_buf;
2588 if(fdinfo->lookahead[0] != '\n') {
2589 readbuf[pos++] = fdinfo->lookahead[0];
2590 fdinfo->lookahead[0] = '\n';
2592 if(fdinfo->lookahead[1] != '\n') {
2593 readbuf[pos++] = fdinfo->lookahead[1];
2594 fdinfo->lookahead[1] = '\n';
2596 if(fdinfo->lookahead[2] != '\n') {
2597 readbuf[pos++] = fdinfo->lookahead[2];
2598 fdinfo->lookahead[2] = '\n';
2603 /* NOTE: this case is broken in native dll, reading
2604 * sometimes fails when small buffer is passed
2606 if(count < 4) {
2607 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2608 if (GetLastError() == ERROR_BROKEN_PIPE) {
2609 fdinfo->wxflag |= WX_ATEOF;
2610 return 0;
2611 }else {
2612 msvcrt_set_errno(GetLastError());
2613 return -1;
2615 }else if(!num_read) {
2616 fdinfo->wxflag |= WX_ATEOF;
2617 return 0;
2618 }else {
2619 pos++;
2622 char_len = get_utf8_char_len(readbuf[0]);
2623 if(char_len>pos) {
2624 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2625 pos += num_read;
2628 if(readbuf[0] == '\n')
2629 fdinfo->wxflag |= WX_READNL;
2630 else
2631 fdinfo->wxflag &= ~WX_READNL;
2633 if(readbuf[0] == 0x1a) {
2634 fdinfo->wxflag |= WX_ATEOF;
2635 return 0;
2638 if(readbuf[0] == '\r') {
2639 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2640 buf[0] = '\r';
2641 else if(lookahead == '\n')
2642 buf[0] = '\n';
2643 else {
2644 buf[0] = '\r';
2645 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2646 fdinfo->lookahead[0] = lookahead;
2647 else
2648 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2650 return 2;
2653 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2654 msvcrt_set_errno(GetLastError());
2655 return -1;
2658 return num_read*2;
2661 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2662 if(pos) {
2663 num_read = 0;
2664 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2665 fdinfo->wxflag |= WX_ATEOF;
2666 if (readbuf != min_buf) MSVCRT_free(readbuf);
2667 return 0;
2668 }else {
2669 msvcrt_set_errno(GetLastError());
2670 if (readbuf != min_buf) MSVCRT_free(readbuf);
2671 return -1;
2673 }else if(!pos && !num_read) {
2674 fdinfo->wxflag |= WX_ATEOF;
2675 if (readbuf != min_buf) MSVCRT_free(readbuf);
2676 return 0;
2679 pos += num_read;
2680 if(readbuf[0] == '\n')
2681 fdinfo->wxflag |= WX_READNL;
2682 else
2683 fdinfo->wxflag &= ~WX_READNL;
2685 /* Find first byte of last character (may be incomplete) */
2686 for(i=pos-1; i>0 && i>pos-4; i--)
2687 if((readbuf[i]&0xc0) != 0x80)
2688 break;
2689 char_len = get_utf8_char_len(readbuf[i]);
2690 if(char_len+i <= pos)
2691 i += char_len;
2693 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2694 if(i < pos)
2695 fdinfo->lookahead[0] = readbuf[i];
2696 if(i+1 < pos)
2697 fdinfo->lookahead[1] = readbuf[i+1];
2698 if(i+2 < pos)
2699 fdinfo->lookahead[2] = readbuf[i+2];
2700 }else if(i < pos) {
2701 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2703 pos = i;
2705 for(i=0, j=0; i<pos; i++) {
2706 if(readbuf[i] == 0x1a) {
2707 fdinfo->wxflag |= WX_ATEOF;
2708 break;
2711 /* strip '\r' if followed by '\n' */
2712 if(readbuf[i] == '\r' && i+1==pos) {
2713 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2714 readbuf[j++] = '\r';
2715 }else if(lookahead == '\n' && j==0) {
2716 readbuf[j++] = '\n';
2717 }else {
2718 if(lookahead != '\n')
2719 readbuf[j++] = '\r';
2721 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2722 fdinfo->lookahead[0] = lookahead;
2723 else
2724 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2726 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2727 readbuf[j++] = readbuf[i];
2730 pos = j;
2732 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2733 msvcrt_set_errno(GetLastError());
2734 if (readbuf != min_buf) MSVCRT_free(readbuf);
2735 return -1;
2738 if (readbuf != min_buf) MSVCRT_free(readbuf);
2739 return num_read*2;
2742 /*********************************************************************
2743 * (internal) read_i
2745 * When reading \r as last character in text mode, read() positions
2746 * the file pointer on the \r character while getc() goes on to
2747 * the following \n
2749 static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
2751 DWORD num_read, utf16;
2752 char *bufstart = buf;
2754 if (count == 0)
2755 return 0;
2757 if (fdinfo->wxflag & WX_ATEOF) {
2758 TRACE("already at EOF, returning 0\n");
2759 return 0;
2761 /* Don't trace small reads, it gets *very* annoying */
2762 if (count > 4)
2763 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2764 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2766 *MSVCRT__errno() = MSVCRT_EBADF;
2767 return -1;
2770 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2771 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2773 *MSVCRT__errno() = MSVCRT_EINVAL;
2774 return -1;
2777 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2778 return read_utf8(fdinfo, buf, count);
2780 if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
2782 if (fdinfo->lookahead[0] != '\n')
2784 bufstart[0] = fdinfo->lookahead[0];
2785 fdinfo->lookahead[0] = '\n';
2787 if (utf16)
2789 bufstart[1] = fdinfo->lookahead[1];
2790 fdinfo->lookahead[1] = '\n';
2793 if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2794 num_read += 1+utf16;
2795 else
2796 num_read = 1+utf16;
2799 if(utf16 && (num_read&1))
2801 /* msvcr90 uses uninitialized value from the buffer in this case */
2802 /* msvcrt ignores additional data */
2803 ERR("got odd number of bytes in UTF16 mode\n");
2804 num_read--;
2807 if (count != 0 && num_read == 0)
2809 fdinfo->wxflag |= WX_ATEOF;
2810 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2812 else if (fdinfo->wxflag & WX_TEXT)
2814 DWORD i, j;
2816 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2817 fdinfo->wxflag |= WX_READNL;
2818 else
2819 fdinfo->wxflag &= ~WX_READNL;
2821 for (i=0, j=0; i<num_read; i+=1+utf16)
2823 /* in text mode, a ctrl-z signals EOF */
2824 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2826 fdinfo->wxflag |= WX_ATEOF;
2827 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2828 break;
2831 /* in text mode, strip \r if followed by \n */
2832 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2834 char lookahead[2];
2835 DWORD len;
2837 lookahead[1] = '\n';
2838 if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
2840 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2842 bufstart[j++] = '\n';
2843 if(utf16) bufstart[j++] = 0;
2845 else
2847 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2849 bufstart[j++] = '\r';
2850 if(utf16) bufstart[j++] = 0;
2853 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2855 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2857 bufstart[j++] = '\n';
2858 if (utf16) bufstart[j++] = 0;
2860 else
2862 fdinfo->lookahead[0] = lookahead[0];
2863 fdinfo->lookahead[1] = lookahead[1];
2866 else
2867 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2870 else
2872 bufstart[j++] = '\r';
2873 if(utf16) bufstart[j++] = 0;
2876 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2877 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2879 bufstart[j++] = bufstart[i];
2880 if(utf16) bufstart[j++] = bufstart[i+1];
2883 num_read = j;
2886 else
2888 if (GetLastError() == ERROR_BROKEN_PIPE)
2890 TRACE(":end-of-pipe\n");
2891 fdinfo->wxflag |= WX_ATEOF;
2892 return 0;
2894 else
2896 TRACE(":failed-last error (%d)\n",GetLastError());
2897 msvcrt_set_errno(GetLastError());
2898 return -1;
2902 if (count > 4)
2903 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2904 return num_read;
2907 /*********************************************************************
2908 * _read (MSVCRT.@)
2910 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2912 ioinfo *info;
2913 int num_read;
2915 if(fd == MSVCRT_NO_CONSOLE_FD) {
2916 *MSVCRT__errno() = MSVCRT_EBADF;
2917 return -1;
2920 info = get_ioinfo(fd);
2921 num_read = read_i(fd, info, buf, count);
2922 release_ioinfo(info);
2923 return num_read;
2926 /*********************************************************************
2927 * _setmode (MSVCRT.@)
2929 int CDECL MSVCRT__setmode(int fd,int mode)
2931 ioinfo *info = get_ioinfo(fd);
2932 int ret = info->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2933 if(ret==MSVCRT__O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16)))
2934 ret = MSVCRT__O_WTEXT;
2936 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2937 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2938 *MSVCRT__errno() = MSVCRT_EINVAL;
2939 release_ioinfo(info);
2940 return -1;
2943 if(info == &MSVCRT___badioinfo) {
2944 *MSVCRT__errno() = MSVCRT_EBADF;
2945 return MSVCRT_EOF;
2948 if(mode == MSVCRT__O_BINARY) {
2949 info->wxflag &= ~WX_TEXT;
2950 info->exflag &= ~(EF_UTF8|EF_UTF16);
2951 release_ioinfo(info);
2952 return ret;
2955 info->wxflag |= WX_TEXT;
2956 if(mode == MSVCRT__O_TEXT)
2957 info->exflag &= ~(EF_UTF8|EF_UTF16);
2958 else if(mode == MSVCRT__O_U8TEXT)
2959 info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
2960 else
2961 info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
2963 release_ioinfo(info);
2964 return ret;
2967 /*********************************************************************
2968 * _stat64 (MSVCRT.@)
2970 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2972 DWORD dw;
2973 WIN32_FILE_ATTRIBUTE_DATA hfi;
2974 unsigned short mode = ALL_S_IREAD;
2975 int plen;
2977 TRACE(":file (%s) buf(%p)\n",path,buf);
2979 plen = strlen(path);
2980 while (plen && path[plen-1]==' ')
2981 plen--;
2983 if (plen && (plen<2 || path[plen-2]!=':') &&
2984 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2986 *MSVCRT__errno() = MSVCRT_ENOENT;
2987 return -1;
2990 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2992 TRACE("failed (%d)\n",GetLastError());
2993 *MSVCRT__errno() = MSVCRT_ENOENT;
2994 return -1;
2997 memset(buf,0,sizeof(struct MSVCRT__stat64));
2999 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3000 Bon 011120: This FIXME seems incorrect
3001 Also a letter as first char isn't enough to be classified
3002 as a drive letter
3004 if (isalpha(*path)&& (*(path+1)==':'))
3005 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
3006 else
3007 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3009 /* Dir, or regular file? */
3010 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3011 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3012 else
3014 mode |= MSVCRT__S_IFREG;
3015 /* executable? */
3016 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3018 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
3019 (tolower(path[plen-3]) << 16);
3020 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
3021 mode |= ALL_S_IEXEC;
3025 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3026 mode |= ALL_S_IWRITE;
3028 buf->st_mode = mode;
3029 buf->st_nlink = 1;
3030 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3031 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3032 buf->st_atime = dw;
3033 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3034 buf->st_mtime = buf->st_ctime = dw;
3035 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3036 (int)(buf->st_size >> 32),(int)buf->st_size,
3037 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3038 return 0;
3041 /*********************************************************************
3042 * _stati64 (MSVCRT.@)
3044 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
3046 int ret;
3047 struct MSVCRT__stat64 buf64;
3049 ret = MSVCRT_stat64(path, &buf64);
3050 if (!ret)
3051 msvcrt_stat64_to_stati64(&buf64, buf);
3052 return ret;
3055 /*********************************************************************
3056 * _stat (MSVCRT.@)
3058 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
3060 int ret;
3061 struct MSVCRT__stat64 buf64;
3063 ret = MSVCRT_stat64( path, &buf64);
3064 if (!ret)
3065 msvcrt_stat64_to_stat(&buf64, buf);
3066 return ret;
3069 #if _MSVCR_VER >= 80
3071 /*********************************************************************
3072 * _stat32 (MSVCR80.@)
3074 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
3076 int ret;
3077 struct MSVCRT__stat64 buf64;
3079 ret = MSVCRT_stat64(path, &buf64);
3080 if (!ret)
3081 msvcrt_stat64_to_stat32(&buf64, buf);
3082 return ret;
3085 /*********************************************************************
3086 * _stat32i64 (MSVCR80.@)
3088 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
3090 int ret;
3091 struct MSVCRT__stat64 buf64;
3093 ret = MSVCRT_stat64(path, &buf64);
3094 if (!ret)
3095 msvcrt_stat64_to_stat32i64(&buf64, buf);
3096 return ret;
3099 /*********************************************************************
3100 * _stat64i32 (MSVCR80.@)
3102 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
3104 int ret;
3105 struct MSVCRT__stat64 buf64;
3107 ret = MSVCRT_stat64(path, &buf64);
3108 if (!ret)
3109 msvcrt_stat64_to_stat64i32(&buf64, buf);
3110 return ret;
3113 #endif /* _MSVCR_VER >= 80 */
3115 /*********************************************************************
3116 * _wstat64 (MSVCRT.@)
3118 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
3120 DWORD dw;
3121 WIN32_FILE_ATTRIBUTE_DATA hfi;
3122 unsigned short mode = ALL_S_IREAD;
3123 int plen;
3125 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
3127 plen = strlenW(path);
3128 while (plen && path[plen-1]==' ')
3129 plen--;
3131 if(plen && (plen<2 || path[plen-2]!=':') &&
3132 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
3134 *MSVCRT__errno() = MSVCRT_ENOENT;
3135 return -1;
3138 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
3140 TRACE("failed (%d)\n",GetLastError());
3141 *MSVCRT__errno() = MSVCRT_ENOENT;
3142 return -1;
3145 memset(buf,0,sizeof(struct MSVCRT__stat64));
3147 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3148 if (MSVCRT_iswalpha(*path))
3149 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
3150 else
3151 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3153 /* Dir, or regular file? */
3154 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3155 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3156 else
3158 mode |= MSVCRT__S_IFREG;
3159 /* executable? */
3160 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3162 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
3163 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
3164 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3165 mode |= ALL_S_IEXEC;
3169 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3170 mode |= ALL_S_IWRITE;
3172 buf->st_mode = mode;
3173 buf->st_nlink = 1;
3174 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3175 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3176 buf->st_atime = dw;
3177 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3178 buf->st_mtime = buf->st_ctime = dw;
3179 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3180 (int)(buf->st_size >> 32),(int)buf->st_size,
3181 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3182 return 0;
3185 /*********************************************************************
3186 * _wstati64 (MSVCRT.@)
3188 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
3190 int ret;
3191 struct MSVCRT__stat64 buf64;
3193 ret = MSVCRT__wstat64(path, &buf64);
3194 if (!ret)
3195 msvcrt_stat64_to_stati64(&buf64, buf);
3196 return ret;
3199 /*********************************************************************
3200 * _wstat (MSVCRT.@)
3202 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
3204 int ret;
3205 struct MSVCRT__stat64 buf64;
3207 ret = MSVCRT__wstat64( path, &buf64 );
3208 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3209 return ret;
3212 #if _MSVCR_VER >= 80
3214 /*********************************************************************
3215 * _wstat32 (MSVCR80.@)
3217 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
3219 int ret;
3220 struct MSVCRT__stat64 buf64;
3222 ret = MSVCRT__wstat64(path, &buf64);
3223 if (!ret)
3224 msvcrt_stat64_to_stat32(&buf64, buf);
3225 return ret;
3228 /*********************************************************************
3229 * _wstat32i64 (MSVCR80.@)
3231 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
3233 int ret;
3234 struct MSVCRT__stat64 buf64;
3236 ret = MSVCRT__wstat64(path, &buf64);
3237 if (!ret)
3238 msvcrt_stat64_to_stat32i64(&buf64, buf);
3239 return ret;
3242 /*********************************************************************
3243 * _wstat64i32 (MSVCR80.@)
3245 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3247 int ret;
3248 struct MSVCRT__stat64 buf64;
3250 ret = MSVCRT__wstat64(path, &buf64);
3251 if (!ret)
3252 msvcrt_stat64_to_stat64i32(&buf64, buf);
3253 return ret;
3256 #endif /* _MSVCR_VER >= 80 */
3258 /*********************************************************************
3259 * _tell (MSVCRT.@)
3261 MSVCRT_long CDECL MSVCRT__tell(int fd)
3263 return MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
3266 /*********************************************************************
3267 * _telli64 (MSVCRT.@)
3269 __int64 CDECL _telli64(int fd)
3271 return MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
3274 /*********************************************************************
3275 * _tempnam (MSVCRT.@)
3277 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3279 char tmpbuf[MAX_PATH];
3280 const char *tmp_dir = MSVCRT_getenv("TMP");
3282 if (tmp_dir) dir = tmp_dir;
3284 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3285 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3287 TRACE("got name (%s)\n",tmpbuf);
3288 DeleteFileA(tmpbuf);
3289 return MSVCRT__strdup(tmpbuf);
3291 TRACE("failed (%d)\n",GetLastError());
3292 return NULL;
3295 /*********************************************************************
3296 * _wtempnam (MSVCRT.@)
3298 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3300 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3301 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3302 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3304 if (tmp_dir) dir = tmp_dir;
3306 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3307 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3309 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3310 DeleteFileW(tmpbuf);
3311 return MSVCRT__wcsdup(tmpbuf);
3313 TRACE("failed (%d)\n",GetLastError());
3314 return NULL;
3317 /*********************************************************************
3318 * _umask (MSVCRT.@)
3320 int CDECL MSVCRT__umask(int umask)
3322 int old_umask = MSVCRT_umask;
3323 TRACE("(%d)\n",umask);
3324 MSVCRT_umask = umask;
3325 return old_umask;
3328 /*********************************************************************
3329 * _utime64 (MSVCRT.@)
3331 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3333 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3335 if (fd > 0)
3337 int retVal = _futime64(fd, t);
3338 MSVCRT__close(fd);
3339 return retVal;
3341 return -1;
3344 /*********************************************************************
3345 * _utime32 (MSVCRT.@)
3347 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3349 if (t)
3351 struct MSVCRT___utimbuf64 t64;
3352 t64.actime = t->actime;
3353 t64.modtime = t->modtime;
3354 return _utime64( path, &t64 );
3356 else
3357 return _utime64( path, NULL );
3360 /*********************************************************************
3361 * _utime (MSVCRT.@)
3363 #ifdef _WIN64
3364 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3366 return _utime64( path, t );
3368 #else
3369 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3371 return _utime32( path, t );
3373 #endif
3375 /*********************************************************************
3376 * _wutime64 (MSVCRT.@)
3378 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3380 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3382 if (fd > 0)
3384 int retVal = _futime64(fd, t);
3385 MSVCRT__close(fd);
3386 return retVal;
3388 return -1;
3391 /*********************************************************************
3392 * _wutime32 (MSVCRT.@)
3394 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3396 if (t)
3398 struct MSVCRT___utimbuf64 t64;
3399 t64.actime = t->actime;
3400 t64.modtime = t->modtime;
3401 return _wutime64( path, &t64 );
3403 else
3404 return _wutime64( path, NULL );
3407 /*********************************************************************
3408 * _wutime (MSVCRT.@)
3410 #ifdef _WIN64
3411 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3413 return _wutime64( path, t );
3415 #else
3416 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3418 return _wutime32( path, t );
3420 #endif
3422 /*********************************************************************
3423 * _write (MSVCRT.@)
3425 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3427 DWORD num_written;
3428 ioinfo *info = get_ioinfo(fd);
3429 HANDLE hand = info->handle;
3431 /* Don't trace small writes, it gets *very* annoying */
3432 #if 0
3433 if (count > 32)
3434 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3435 #endif
3436 if (hand == INVALID_HANDLE_VALUE || fd == MSVCRT_NO_CONSOLE_FD)
3438 *MSVCRT__errno() = MSVCRT_EBADF;
3439 release_ioinfo(info);
3440 return -1;
3443 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3445 *MSVCRT__errno() = MSVCRT_EINVAL;
3446 release_ioinfo(info);
3447 return -1;
3450 /* If appending, go to EOF */
3451 if (info->wxflag & WX_APPEND)
3452 MSVCRT__lseek(fd, 0, FILE_END);
3454 if (!(info->wxflag & WX_TEXT))
3456 if (WriteFile(hand, buf, count, &num_written, NULL)
3457 && (num_written == count))
3459 release_ioinfo(info);
3460 return num_written;
3462 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3463 hand, GetLastError());
3464 msvcrt_set_errno(GetLastError());
3466 else
3468 unsigned int i, j, nr_lf, size;
3469 char *p = NULL;
3470 const char *q;
3471 const char *s = buf;
3473 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3475 /* find number of \n */
3476 for (nr_lf=0, i=0; i<count; i++)
3477 if (s[i] == '\n')
3478 nr_lf++;
3479 if (nr_lf)
3481 size = count+nr_lf;
3482 if ((q = p = MSVCRT_malloc(size)))
3484 for (s = buf, i = 0, j = 0; i < count; i++)
3486 if (s[i] == '\n')
3487 p[j++] = '\r';
3488 p[j++] = s[i];
3491 else
3493 FIXME("Malloc failed\n");
3494 nr_lf = 0;
3495 size = count;
3496 q = buf;
3499 else
3501 size = count;
3502 q = buf;
3505 else if (info->exflag & EF_UTF16)
3507 for (nr_lf=0, i=0; i<count; i+=2)
3508 if (s[i]=='\n' && s[i+1]==0)
3509 nr_lf += 2;
3510 if (nr_lf)
3512 size = count+nr_lf;
3513 if ((q = p = MSVCRT_malloc(size)))
3515 for (s=buf, i=0, j=0; i<count; i++)
3517 if (s[i]=='\n' && s[i+1]==0)
3519 p[j++] = '\r';
3520 p[j++] = 0;
3522 p[j++] = s[i++];
3523 p[j++] = s[i];
3526 else
3528 FIXME("Malloc failed\n");
3529 nr_lf = 0;
3530 size = count;
3531 q = buf;
3534 else
3536 size = count;
3537 q = buf;
3540 else
3542 DWORD conv_len;
3544 for(nr_lf=0, i=0; i<count; i+=2)
3545 if (s[i]=='\n' && s[i+1]==0)
3546 nr_lf++;
3548 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3549 if(!conv_len) {
3550 msvcrt_set_errno(GetLastError());
3551 MSVCRT_free(p);
3552 release_ioinfo(info);
3553 return -1;
3556 size = conv_len+nr_lf;
3557 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3559 for (s=buf, i=0, j=0; i<count; i++)
3561 if (s[i]=='\n' && s[i+1]==0)
3563 p[j++] = '\r';
3564 p[j++] = 0;
3566 p[j++] = s[i++];
3567 p[j++] = s[i];
3569 q = p+count+nr_lf*2;
3570 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3571 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3573 else
3575 FIXME("Malloc failed\n");
3576 nr_lf = 0;
3577 size = count;
3578 q = buf;
3582 if (!WriteFile(hand, q, size, &num_written, NULL))
3583 num_written = -1;
3584 release_ioinfo(info);
3585 MSVCRT_free(p);
3586 if (num_written != size)
3588 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3589 fd, hand, GetLastError(), num_written);
3590 msvcrt_set_errno(GetLastError());
3591 return -1;
3593 return count;
3596 release_ioinfo(info);
3597 return -1;
3600 /*********************************************************************
3601 * _putw (MSVCRT.@)
3603 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3605 int len;
3607 MSVCRT__lock_file(file);
3608 len = MSVCRT__write(file->_file, &val, sizeof(val));
3609 if (len == sizeof(val)) {
3610 MSVCRT__unlock_file(file);
3611 return val;
3614 file->_flag |= MSVCRT__IOERR;
3615 MSVCRT__unlock_file(file);
3616 return MSVCRT_EOF;
3619 /*********************************************************************
3620 * fclose (MSVCRT.@)
3622 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3624 int ret;
3626 MSVCRT__lock_file(file);
3627 ret = MSVCRT__fclose_nolock(file);
3628 MSVCRT__unlock_file(file);
3630 return ret;
3633 /*********************************************************************
3634 * _fclose_nolock (MSVCRT.@)
3636 int CDECL MSVCRT__fclose_nolock(MSVCRT_FILE* file)
3638 int r, flag;
3640 if(!(file->_flag & (MSVCRT__IOREAD | MSVCRT__IOWRT | MSVCRT__IORW)))
3642 file->_flag = 0;
3643 return MSVCRT_EOF;
3646 flag = file->_flag;
3647 MSVCRT_free(file->_tmpfname);
3648 file->_tmpfname = NULL;
3649 /* flush stdio buffers */
3650 if(file->_flag & MSVCRT__IOWRT)
3651 MSVCRT__fflush_nolock(file);
3652 if(file->_flag & MSVCRT__IOMYBUF)
3653 MSVCRT_free(file->_base);
3655 r=MSVCRT__close(file->_file);
3656 file->_flag = 0;
3658 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3661 /*********************************************************************
3662 * feof (MSVCRT.@)
3664 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3666 return file->_flag & MSVCRT__IOEOF;
3669 /*********************************************************************
3670 * ferror (MSVCRT.@)
3672 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3674 return file->_flag & MSVCRT__IOERR;
3677 /*********************************************************************
3678 * _filbuf (MSVCRT.@)
3680 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3682 unsigned char c;
3684 if(file->_flag & MSVCRT__IOSTRG)
3685 return MSVCRT_EOF;
3687 /* Allocate buffer if needed */
3688 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3689 msvcrt_alloc_buffer(file);
3691 if(!(file->_flag & MSVCRT__IOREAD)) {
3692 if(file->_flag & MSVCRT__IORW)
3693 file->_flag |= MSVCRT__IOREAD;
3694 else
3695 return MSVCRT_EOF;
3698 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3699 int r;
3700 if ((r = MSVCRT__read(file->_file,&c,1)) != 1) {
3701 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3702 return MSVCRT_EOF;
3705 return c;
3706 } else {
3707 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3708 if(file->_cnt<=0) {
3709 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3710 file->_cnt = 0;
3711 return MSVCRT_EOF;
3714 file->_cnt--;
3715 file->_ptr = file->_base+1;
3716 c = *(unsigned char *)file->_base;
3717 return c;
3721 /*********************************************************************
3722 * fgetc (MSVCRT.@)
3724 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3726 int ret;
3728 MSVCRT__lock_file(file);
3729 ret = MSVCRT__fgetc_nolock(file);
3730 MSVCRT__unlock_file(file);
3732 return ret;
3735 /*********************************************************************
3736 * _fgetc_nolock (MSVCRT.@)
3738 int CDECL MSVCRT__fgetc_nolock(MSVCRT_FILE* file)
3740 unsigned char *i;
3741 unsigned int j;
3743 if (file->_cnt>0) {
3744 file->_cnt--;
3745 i = (unsigned char *)file->_ptr++;
3746 j = *i;
3747 } else
3748 j = MSVCRT__filbuf(file);
3750 return j;
3753 /*********************************************************************
3754 * _fgetchar (MSVCRT.@)
3756 int CDECL MSVCRT__fgetchar(void)
3758 return MSVCRT_fgetc(MSVCRT_stdin);
3761 /*********************************************************************
3762 * fgets (MSVCRT.@)
3764 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3766 int cc = MSVCRT_EOF;
3767 char * buf_start = s;
3769 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3770 file,file->_file,s,size);
3772 MSVCRT__lock_file(file);
3774 while ((size >1) && (cc = MSVCRT__fgetc_nolock(file)) != MSVCRT_EOF && cc != '\n')
3776 *s++ = (char)cc;
3777 size --;
3779 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3781 TRACE(":nothing read\n");
3782 MSVCRT__unlock_file(file);
3783 return NULL;
3785 if ((cc != MSVCRT_EOF) && (size > 1))
3786 *s++ = cc;
3787 *s = '\0';
3788 TRACE(":got %s\n", debugstr_a(buf_start));
3789 MSVCRT__unlock_file(file);
3790 return buf_start;
3793 /*********************************************************************
3794 * fgetwc (MSVCRT.@)
3796 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3798 MSVCRT_wint_t ret;
3800 MSVCRT__lock_file(file);
3801 ret = MSVCRT__fgetwc_nolock(file);
3802 MSVCRT__unlock_file(file);
3804 return ret;
3807 /*********************************************************************
3808 * _fgetwc_nolock (MSVCRT.@)
3810 MSVCRT_wint_t CDECL MSVCRT__fgetwc_nolock(MSVCRT_FILE* file)
3812 MSVCRT_wint_t ret;
3813 int ch;
3815 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3816 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3817 char *p;
3819 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3820 ch = MSVCRT__fgetc_nolock(file);
3821 if(ch == MSVCRT_EOF) {
3822 ret = MSVCRT_WEOF;
3823 break;
3825 *p = (char)ch;
3827 }else {
3828 char mbs[MSVCRT_MB_LEN_MAX];
3829 int len = 0;
3831 ch = MSVCRT__fgetc_nolock(file);
3832 if(ch != MSVCRT_EOF) {
3833 mbs[0] = (char)ch;
3834 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3835 ch = MSVCRT__fgetc_nolock(file);
3836 if(ch != MSVCRT_EOF) {
3837 mbs[1] = (char)ch;
3838 len = 2;
3840 }else {
3841 len = 1;
3845 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3846 ret = MSVCRT_WEOF;
3849 return ret;
3852 /*********************************************************************
3853 * _getw (MSVCRT.@)
3855 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3857 char *ch;
3858 int i, k;
3859 unsigned int j;
3860 ch = (char *)&i;
3862 MSVCRT__lock_file(file);
3863 for (j=0; j<sizeof(int); j++) {
3864 k = MSVCRT__fgetc_nolock(file);
3865 if (k == MSVCRT_EOF) {
3866 file->_flag |= MSVCRT__IOEOF;
3867 MSVCRT__unlock_file(file);
3868 return MSVCRT_EOF;
3870 ch[j] = k;
3873 MSVCRT__unlock_file(file);
3874 return i;
3877 /*********************************************************************
3878 * getwc (MSVCRT.@)
3880 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3882 return MSVCRT_fgetwc(file);
3885 /*********************************************************************
3886 * _fgetwchar (MSVCRT.@)
3888 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3890 return MSVCRT_fgetwc(MSVCRT_stdin);
3893 /*********************************************************************
3894 * getwchar (MSVCRT.@)
3896 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3898 return MSVCRT__fgetwchar();
3901 /*********************************************************************
3902 * fgetws (MSVCRT.@)
3904 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3906 MSVCRT_wint_t cc = MSVCRT_WEOF;
3907 MSVCRT_wchar_t * buf_start = s;
3909 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3910 file,file->_file,s,size);
3912 MSVCRT__lock_file(file);
3914 while ((size >1) && (cc = MSVCRT__fgetwc_nolock(file)) != MSVCRT_WEOF && cc != '\n')
3916 *s++ = cc;
3917 size --;
3919 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3921 TRACE(":nothing read\n");
3922 MSVCRT__unlock_file(file);
3923 return NULL;
3925 if ((cc != MSVCRT_WEOF) && (size > 1))
3926 *s++ = cc;
3927 *s = 0;
3928 TRACE(":got %s\n", debugstr_w(buf_start));
3929 MSVCRT__unlock_file(file);
3930 return buf_start;
3933 /*********************************************************************
3934 * _flsbuf (MSVCRT.@)
3936 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3938 /* Flush output buffer */
3939 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3940 msvcrt_alloc_buffer(file);
3943 if(!(file->_flag & MSVCRT__IOWRT)) {
3944 if(!(file->_flag & MSVCRT__IORW)) {
3945 file->_flag |= MSVCRT__IOERR;
3946 *MSVCRT__errno() = MSVCRT_EBADF;
3947 return MSVCRT_EOF;
3949 file->_flag |= MSVCRT__IOWRT;
3951 if(file->_flag & MSVCRT__IOREAD) {
3952 if(!(file->_flag & MSVCRT__IOEOF)) {
3953 file->_flag |= MSVCRT__IOERR;
3954 return MSVCRT_EOF;
3956 file->_cnt = 0;
3957 file->_ptr = file->_base;
3958 file->_flag &= ~(MSVCRT__IOREAD | MSVCRT__IOEOF);
3961 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3962 int res = 0;
3964 if(file->_cnt <= 0) {
3965 res = msvcrt_flush_buffer(file);
3966 if(res)
3967 return res;
3968 file->_flag |= MSVCRT__IOWRT;
3969 file->_cnt=file->_bufsiz;
3971 *file->_ptr++ = c;
3972 file->_cnt--;
3973 return c&0xff;
3974 } else {
3975 unsigned char cc=c;
3976 int len;
3977 /* set _cnt to 0 for unbuffered FILEs */
3978 file->_cnt = 0;
3979 len = MSVCRT__write(file->_file, &cc, 1);
3980 if (len == 1)
3981 return c & 0xff;
3982 file->_flag |= MSVCRT__IOERR;
3983 return MSVCRT_EOF;
3987 /*********************************************************************
3988 * fwrite (MSVCRT.@)
3990 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3992 MSVCRT_size_t ret;
3994 MSVCRT__lock_file(file);
3995 ret = MSVCRT__fwrite_nolock(ptr, size, nmemb, file);
3996 MSVCRT__unlock_file(file);
3998 return ret;
4001 /*********************************************************************
4002 * _fwrite_nolock (MSVCRT.@)
4004 MSVCRT_size_t CDECL MSVCRT__fwrite_nolock(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4006 MSVCRT_size_t wrcnt=size * nmemb;
4007 int written = 0;
4008 if (size == 0)
4009 return 0;
4011 while(wrcnt) {
4012 if(file->_cnt < 0) {
4013 WARN("negative file->_cnt value in %p\n", file);
4014 file->_flag |= MSVCRT__IOERR;
4015 break;
4016 } else if(file->_cnt) {
4017 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
4018 memcpy(file->_ptr, ptr, pcnt);
4019 file->_cnt -= pcnt;
4020 file->_ptr += pcnt;
4021 written += pcnt;
4022 wrcnt -= pcnt;
4023 ptr = (const char*)ptr + pcnt;
4024 } else if((file->_flag & MSVCRT__IONBF)
4025 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
4026 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
4027 MSVCRT_size_t pcnt;
4028 int bufsiz;
4030 if(file->_flag & MSVCRT__IONBF)
4031 bufsiz = 1;
4032 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4033 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
4034 else
4035 bufsiz = file->_bufsiz;
4037 pcnt = (wrcnt / bufsiz) * bufsiz;
4039 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
4040 break;
4042 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
4043 file->_flag |= MSVCRT__IOERR;
4044 break;
4046 written += pcnt;
4047 wrcnt -= pcnt;
4048 ptr = (const char*)ptr + pcnt;
4049 } else {
4050 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
4051 break;
4052 written++;
4053 wrcnt--;
4054 ptr = (const char*)ptr + 1;
4058 return written / size;
4061 /*********************************************************************
4062 * fputwc (MSVCRT.@)
4064 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
4066 MSVCRT_wint_t ret;
4068 MSVCRT__lock_file(file);
4069 ret = MSVCRT__fputwc_nolock(wc, file);
4070 MSVCRT__unlock_file(file);
4072 return ret;
4075 /*********************************************************************
4076 * _fputwc_nolock (MSVCRT.@)
4078 MSVCRT_wint_t CDECL MSVCRT__fputwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE* file)
4080 MSVCRT_wchar_t mwc=wc;
4081 ioinfo *fdinfo;
4082 MSVCRT_wint_t ret;
4084 fdinfo = get_ioinfo_nolock(file->_file);
4086 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
4087 char buf[MSVCRT_MB_LEN_MAX];
4088 int char_len;
4090 char_len = MSVCRT_wctomb(buf, mwc);
4091 if(char_len!=-1 && MSVCRT__fwrite_nolock(buf, char_len, 1, file)==1)
4092 ret = wc;
4093 else
4094 ret = MSVCRT_WEOF;
4095 }else if(MSVCRT__fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
4096 ret = wc;
4097 }else {
4098 ret = MSVCRT_WEOF;
4101 return ret;
4104 /*********************************************************************
4105 * _fputwchar (MSVCRT.@)
4107 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
4109 return MSVCRT_fputwc(wc, MSVCRT_stdout);
4112 /*********************************************************************
4113 * _wfsopen (MSVCRT.@)
4115 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
4117 MSVCRT_FILE* file;
4118 int open_flags, stream_flags, fd;
4120 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
4122 /* map mode string to open() flags. "man fopen" for possibilities. */
4123 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4124 return NULL;
4126 LOCK_FILES();
4127 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4128 if (fd < 0)
4129 file = NULL;
4130 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
4131 != -1)
4132 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
4133 else if (file)
4135 file->_flag = 0;
4136 file = NULL;
4139 TRACE(":got (%p)\n",file);
4140 if (fd >= 0 && !file)
4141 MSVCRT__close(fd);
4142 UNLOCK_FILES();
4143 return file;
4146 /*********************************************************************
4147 * _fsopen (MSVCRT.@)
4149 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
4151 MSVCRT_FILE *ret;
4152 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4154 if (path && !(pathW = msvcrt_wstrdupa(path))) {
4155 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4156 *MSVCRT__errno() = MSVCRT_EINVAL;
4157 return NULL;
4159 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4161 MSVCRT_free(pathW);
4162 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4163 *MSVCRT__errno() = MSVCRT_EINVAL;
4164 return NULL;
4167 ret = MSVCRT__wfsopen(pathW, modeW, share);
4169 MSVCRT_free(pathW);
4170 MSVCRT_free(modeW);
4171 return ret;
4174 /*********************************************************************
4175 * fopen (MSVCRT.@)
4177 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
4179 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
4182 /*********************************************************************
4183 * fopen_s (MSVCRT.@)
4185 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
4186 const char *filename, const char *mode)
4188 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4189 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4190 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4192 *pFile = MSVCRT_fopen(filename, mode);
4194 if(!*pFile)
4195 return *MSVCRT__errno();
4196 return 0;
4199 /*********************************************************************
4200 * _wfopen (MSVCRT.@)
4202 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
4204 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
4207 /*********************************************************************
4208 * _wfopen_s (MSVCRT.@)
4210 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
4211 const MSVCRT_wchar_t *mode)
4213 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4214 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4215 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4217 *pFile = MSVCRT__wfopen(filename, mode);
4219 if(!*pFile)
4220 return *MSVCRT__errno();
4221 return 0;
4224 /*********************************************************************
4225 * fputc (MSVCRT.@)
4227 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
4229 int ret;
4231 MSVCRT__lock_file(file);
4232 ret = MSVCRT__fputc_nolock(c, file);
4233 MSVCRT__unlock_file(file);
4235 return ret;
4238 /*********************************************************************
4239 * _fputc_nolock (MSVCRT.@)
4241 int CDECL MSVCRT__fputc_nolock(int c, MSVCRT_FILE* file)
4243 int res;
4245 if(file->_cnt>0) {
4246 *file->_ptr++=c;
4247 file->_cnt--;
4248 if (c == '\n')
4250 res = msvcrt_flush_buffer(file);
4251 return res ? res : c;
4253 else {
4254 return c & 0xff;
4256 } else {
4257 res = MSVCRT__flsbuf(c, file);
4258 return res;
4262 /*********************************************************************
4263 * _fputchar (MSVCRT.@)
4265 int CDECL MSVCRT__fputchar(int c)
4267 return MSVCRT_fputc(c, MSVCRT_stdout);
4270 /*********************************************************************
4271 * fread (MSVCRT.@)
4273 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4275 MSVCRT_size_t ret;
4277 MSVCRT__lock_file(file);
4278 ret = MSVCRT__fread_nolock(ptr, size, nmemb, file);
4279 MSVCRT__unlock_file(file);
4281 return ret;
4284 /*********************************************************************
4285 * _fread_nolock (MSVCRT.@)
4287 MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4289 MSVCRT_size_t rcnt=size * nmemb;
4290 MSVCRT_size_t read=0;
4291 MSVCRT_size_t pread=0;
4293 if(!rcnt)
4294 return 0;
4296 /* first buffered data */
4297 if(file->_cnt>0) {
4298 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4299 memcpy(ptr, file->_ptr, pcnt);
4300 file->_cnt -= pcnt;
4301 file->_ptr += pcnt;
4302 read += pcnt ;
4303 rcnt -= pcnt ;
4304 ptr = (char*)ptr + pcnt;
4305 } else if(!(file->_flag & MSVCRT__IOREAD )) {
4306 if(file->_flag & MSVCRT__IORW) {
4307 file->_flag |= MSVCRT__IOREAD;
4308 } else {
4309 return 0;
4313 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4314 msvcrt_alloc_buffer(file);
4316 while(rcnt>0)
4318 int i;
4319 if (!file->_cnt && rcnt<file->_bufsiz && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
4320 i = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
4321 file->_ptr = file->_base;
4322 if (i != -1) {
4323 file->_cnt = i;
4324 if (i > rcnt) i = rcnt;
4326 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4327 if (i > 0 && i < file->_cnt) {
4328 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4329 file->_flag &= ~MSVCRT__IOEOF;
4331 if (i > 0) {
4332 memcpy(ptr, file->_ptr, i);
4333 file->_cnt -= i;
4334 file->_ptr += i;
4336 } else if (rcnt > INT_MAX) {
4337 i = MSVCRT__read(file->_file, ptr, INT_MAX);
4338 } else if (rcnt < (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ)) {
4339 i = MSVCRT__read(file->_file, ptr, rcnt);
4340 } else {
4341 i = MSVCRT__read(file->_file, ptr, rcnt - rcnt % (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ));
4343 pread += i;
4344 rcnt -= i;
4345 ptr = (char *)ptr+i;
4346 /* expose feof condition in the flags
4347 * MFC tests file->_flag for feof, and doesn't call feof())
4349 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4350 file->_flag |= MSVCRT__IOEOF;
4351 else if (i == -1)
4353 file->_flag |= MSVCRT__IOERR;
4354 pread = 0;
4355 rcnt = 0;
4357 if (i < 1) break;
4359 read+=pread;
4360 return read / size;
4363 #if _MSVCR_VER >= 80
4365 /*********************************************************************
4366 * fread_s (MSVCR80.@)
4368 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4369 MSVCRT_size_t count, MSVCRT_FILE *stream)
4371 MSVCRT_size_t ret;
4373 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4374 if(buf && buf_size)
4375 memset(buf, 0, buf_size);
4376 return 0;
4378 if(!elem_size || !count) return 0;
4380 MSVCRT__lock_file(stream);
4381 ret = MSVCRT__fread_nolock_s(buf, buf_size, elem_size, count, stream);
4382 MSVCRT__unlock_file(stream);
4384 return ret;
4387 /*********************************************************************
4388 * _fread_nolock_s (MSVCR80.@)
4390 MSVCRT_size_t CDECL MSVCRT__fread_nolock_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4391 MSVCRT_size_t count, MSVCRT_FILE *stream)
4393 size_t bytes_left, buf_pos;
4395 TRACE("(%p %lu %lu %lu %p)\n", buf, buf_size, elem_size, count, stream);
4397 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4398 if(buf && buf_size)
4399 memset(buf, 0, buf_size);
4400 return 0;
4402 if(!elem_size || !count) return 0;
4403 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4404 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4406 bytes_left = elem_size*count;
4407 buf_pos = 0;
4408 while(bytes_left) {
4409 if(stream->_cnt > 0) {
4410 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4412 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4413 memset(buf, 0, buf_size);
4414 return 0;
4417 MSVCRT__fread_nolock((char*)buf+buf_pos, 1, size, stream);
4418 buf_pos += size;
4419 bytes_left -= size;
4420 }else {
4421 int c = MSVCRT__filbuf(stream);
4423 if(c == MSVCRT_EOF)
4424 break;
4426 if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, MSVCRT_ERANGE)) {
4427 memset(buf, 0, buf_size);
4428 return 0;
4431 ((char*)buf)[buf_pos++] = c;
4432 bytes_left--;
4436 return buf_pos/elem_size;
4439 #endif /* _MSVCR_VER >= 80 */
4441 /*********************************************************************
4442 * _wfreopen (MSVCRT.@)
4445 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4447 int open_flags, stream_flags, fd;
4449 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4451 LOCK_FILES();
4452 if (!file || ((fd = file->_file) < 0))
4453 file = NULL;
4454 else
4456 MSVCRT_fclose(file);
4457 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4458 file = NULL;
4459 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4460 file = NULL;
4461 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4463 file->_flag = 0;
4464 file = NULL;
4467 UNLOCK_FILES();
4468 return file;
4471 /*********************************************************************
4472 * _wfreopen_s (MSVCRT.@)
4474 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4475 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4477 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4478 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4479 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4480 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4482 *pFile = MSVCRT__wfreopen(path, mode, file);
4484 if(!*pFile)
4485 return *MSVCRT__errno();
4486 return 0;
4489 /*********************************************************************
4490 * freopen (MSVCRT.@)
4493 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4495 MSVCRT_FILE *ret;
4496 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4498 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4499 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4501 MSVCRT_free(pathW);
4502 return NULL;
4505 ret = MSVCRT__wfreopen(pathW, modeW, file);
4507 MSVCRT_free(pathW);
4508 MSVCRT_free(modeW);
4509 return ret;
4512 /*********************************************************************
4513 * freopen_s (MSVCRT.@)
4515 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4516 const char *path, const char *mode, MSVCRT_FILE* file)
4518 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4519 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4520 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4521 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4523 *pFile = MSVCRT_freopen(path, mode, file);
4525 if(!*pFile)
4526 return *MSVCRT__errno();
4527 return 0;
4530 /*********************************************************************
4531 * fsetpos (MSVCRT.@)
4533 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4535 int ret;
4537 MSVCRT__lock_file(file);
4538 msvcrt_flush_buffer(file);
4540 /* Reset direction of i/o */
4541 if(file->_flag & MSVCRT__IORW) {
4542 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4545 ret = (MSVCRT__lseeki64(file->_file,*pos,MSVCRT_SEEK_SET) == -1) ? -1 : 0;
4546 MSVCRT__unlock_file(file);
4547 return ret;
4550 /*********************************************************************
4551 * _ftelli64 (MSVCRT.@)
4553 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4555 __int64 ret;
4557 MSVCRT__lock_file(file);
4558 ret = MSVCRT__ftelli64_nolock(file);
4559 MSVCRT__unlock_file(file);
4561 return ret;
4564 /*********************************************************************
4565 * _ftelli64_nolock (MSVCRT.@)
4567 __int64 CDECL MSVCRT__ftelli64_nolock(MSVCRT_FILE* file)
4569 __int64 pos;
4571 pos = _telli64(file->_file);
4572 if(pos == -1)
4573 return -1;
4574 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4575 if(file->_flag & MSVCRT__IOWRT) {
4576 pos += file->_ptr - file->_base;
4578 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4579 char *p;
4581 for(p=file->_base; p<file->_ptr; p++)
4582 if(*p == '\n')
4583 pos++;
4585 } else if(!file->_cnt) { /* nothing to do */
4586 } else if(MSVCRT__lseeki64(file->_file, 0, MSVCRT_SEEK_END)==pos) {
4587 int i;
4589 pos -= file->_cnt;
4590 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4591 for(i=0; i<file->_cnt; i++)
4592 if(file->_ptr[i] == '\n')
4593 pos--;
4595 } else {
4596 char *p;
4598 if(MSVCRT__lseeki64(file->_file, pos, MSVCRT_SEEK_SET) != pos)
4599 return -1;
4601 pos -= file->_bufsiz;
4602 pos += file->_ptr - file->_base;
4604 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4605 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4606 pos--;
4608 for(p=file->_base; p<file->_ptr; p++)
4609 if(*p == '\n')
4610 pos++;
4615 return pos;
4618 /*********************************************************************
4619 * ftell (MSVCRT.@)
4621 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4623 return MSVCRT__ftelli64(file);
4626 #if _MSVCR_VER >= 80
4627 /*********************************************************************
4628 * _ftell_nolock (MSVCR80.@)
4630 LONG CDECL MSVCRT__ftell_nolock(MSVCRT_FILE* file)
4632 return MSVCRT__ftelli64_nolock(file);
4634 #endif
4636 /*********************************************************************
4637 * fgetpos (MSVCRT.@)
4639 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4641 *pos = MSVCRT__ftelli64(file);
4642 if(*pos == -1)
4643 return -1;
4644 return 0;
4647 /*********************************************************************
4648 * fputs (MSVCRT.@)
4650 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4652 MSVCRT_size_t len = strlen(s);
4653 int ret;
4655 MSVCRT__lock_file(file);
4656 ret = MSVCRT__fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4657 MSVCRT__unlock_file(file);
4658 return ret;
4661 /*********************************************************************
4662 * fputws (MSVCRT.@)
4664 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4666 MSVCRT_size_t i, len = strlenW(s);
4667 BOOL tmp_buf;
4668 int ret;
4670 MSVCRT__lock_file(file);
4671 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4672 ret = MSVCRT__fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4673 MSVCRT__unlock_file(file);
4674 return ret;
4677 tmp_buf = add_std_buffer(file);
4678 for (i=0; i<len; i++) {
4679 if(MSVCRT__fputwc_nolock(s[i], file) == MSVCRT_WEOF) {
4680 if(tmp_buf) remove_std_buffer(file);
4681 MSVCRT__unlock_file(file);
4682 return MSVCRT_WEOF;
4686 if(tmp_buf) remove_std_buffer(file);
4687 MSVCRT__unlock_file(file);
4688 return 0;
4691 /*********************************************************************
4692 * getchar (MSVCRT.@)
4694 int CDECL MSVCRT_getchar(void)
4696 return MSVCRT_fgetc(MSVCRT_stdin);
4699 /*********************************************************************
4700 * getc (MSVCRT.@)
4702 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4704 return MSVCRT_fgetc(file);
4707 /*********************************************************************
4708 * gets (MSVCRT.@)
4710 char * CDECL MSVCRT_gets(char *buf)
4712 int cc;
4713 char * buf_start = buf;
4715 MSVCRT__lock_file(MSVCRT_stdin);
4716 for(cc = MSVCRT__fgetc_nolock(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4717 cc = MSVCRT__fgetc_nolock(MSVCRT_stdin))
4719 if(cc != '\r')
4720 *buf++ = (char)cc;
4722 MSVCRT__unlock_file(MSVCRT_stdin);
4724 if ((cc == MSVCRT_EOF) && (buf_start == buf))
4726 TRACE(":nothing read\n");
4727 return NULL;
4729 *buf = '\0';
4731 TRACE("got '%s'\n", buf_start);
4732 return buf_start;
4735 /*********************************************************************
4736 * _getws (MSVCRT.@)
4738 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4740 MSVCRT_wint_t cc;
4741 MSVCRT_wchar_t* ws = buf;
4743 MSVCRT__lock_file(MSVCRT_stdin);
4744 for (cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4745 cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin))
4747 if (cc != '\r')
4748 *buf++ = (MSVCRT_wchar_t)cc;
4750 MSVCRT__unlock_file(MSVCRT_stdin);
4752 if ((cc == MSVCRT_WEOF) && (ws == buf))
4754 TRACE(":nothing read\n");
4755 return NULL;
4757 *buf = '\0';
4759 TRACE("got %s\n", debugstr_w(ws));
4760 return ws;
4763 /*********************************************************************
4764 * putc (MSVCRT.@)
4766 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4768 return MSVCRT_fputc(c, file);
4771 /*********************************************************************
4772 * putchar (MSVCRT.@)
4774 int CDECL MSVCRT_putchar(int c)
4776 return MSVCRT_fputc(c, MSVCRT_stdout);
4779 /*********************************************************************
4780 * puts (MSVCRT.@)
4782 int CDECL MSVCRT_puts(const char *s)
4784 MSVCRT_size_t len = strlen(s);
4785 int ret;
4787 MSVCRT__lock_file(MSVCRT_stdout);
4788 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4789 MSVCRT__unlock_file(MSVCRT_stdout);
4790 return MSVCRT_EOF;
4793 ret = MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4794 MSVCRT__unlock_file(MSVCRT_stdout);
4795 return ret;
4798 /*********************************************************************
4799 * _putws (MSVCRT.@)
4801 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4803 static const MSVCRT_wchar_t nl = '\n';
4804 MSVCRT_size_t len = strlenW(s);
4805 int ret;
4807 MSVCRT__lock_file(MSVCRT_stdout);
4808 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4809 MSVCRT__unlock_file(MSVCRT_stdout);
4810 return MSVCRT_EOF;
4813 ret = MSVCRT__fwrite_nolock(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4814 MSVCRT__unlock_file(MSVCRT_stdout);
4815 return ret;
4818 /*********************************************************************
4819 * remove (MSVCRT.@)
4821 int CDECL MSVCRT_remove(const char *path)
4823 TRACE("(%s)\n",path);
4824 if (DeleteFileA(path))
4825 return 0;
4826 TRACE(":failed (%d)\n",GetLastError());
4827 msvcrt_set_errno(GetLastError());
4828 return -1;
4831 /*********************************************************************
4832 * _wremove (MSVCRT.@)
4834 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4836 TRACE("(%s)\n",debugstr_w(path));
4837 if (DeleteFileW(path))
4838 return 0;
4839 TRACE(":failed (%d)\n",GetLastError());
4840 msvcrt_set_errno(GetLastError());
4841 return -1;
4844 /*********************************************************************
4845 * rename (MSVCRT.@)
4847 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4849 TRACE(":from %s to %s\n",oldpath,newpath);
4850 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4851 return 0;
4852 TRACE(":failed (%d)\n",GetLastError());
4853 msvcrt_set_errno(GetLastError());
4854 return -1;
4857 /*********************************************************************
4858 * _wrename (MSVCRT.@)
4860 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4862 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4863 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4864 return 0;
4865 TRACE(":failed (%d)\n",GetLastError());
4866 msvcrt_set_errno(GetLastError());
4867 return -1;
4870 /*********************************************************************
4871 * setvbuf (MSVCRT.@)
4873 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4875 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4876 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4877 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4879 MSVCRT__lock_file(file);
4881 MSVCRT__fflush_nolock(file);
4882 if(file->_flag & MSVCRT__IOMYBUF)
4883 MSVCRT_free(file->_base);
4884 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4885 file->_cnt = 0;
4887 if(mode == MSVCRT__IONBF) {
4888 file->_flag |= MSVCRT__IONBF;
4889 file->_base = file->_ptr = (char*)&file->_charbuf;
4890 file->_bufsiz = 2;
4891 }else if(buf) {
4892 file->_base = file->_ptr = buf;
4893 file->_flag |= MSVCRT__USERBUF;
4894 file->_bufsiz = size;
4895 }else {
4896 file->_base = file->_ptr = MSVCRT_malloc(size);
4897 if(!file->_base) {
4898 file->_bufsiz = 0;
4899 MSVCRT__unlock_file(file);
4900 return -1;
4903 file->_flag |= MSVCRT__IOMYBUF;
4904 file->_bufsiz = size;
4906 MSVCRT__unlock_file(file);
4907 return 0;
4910 /*********************************************************************
4911 * setbuf (MSVCRT.@)
4913 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4915 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4918 static int tmpnam_helper(char *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
4920 char tmpstr[8];
4921 char *p = s;
4922 int digits;
4924 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
4926 if (size < 3) {
4927 if (size) *s = 0;
4928 *MSVCRT__errno() = MSVCRT_ERANGE;
4929 return MSVCRT_ERANGE;
4931 *p++ = '\\';
4932 *p++ = 's';
4933 size -= 2;
4934 digits = msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4935 if (digits+1 > size) {
4936 *s = 0;
4937 *MSVCRT__errno() = MSVCRT_ERANGE;
4938 return MSVCRT_ERANGE;
4940 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4941 p += digits;
4942 *p++ = '.';
4943 size -= digits+1;
4945 while(1) {
4946 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4947 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4948 break;
4951 digits = msvcrt_int_to_base32(digits, tmpstr);
4952 if (digits+1 > size) {
4953 *s = 0;
4954 *MSVCRT__errno() = MSVCRT_ERANGE;
4955 return MSVCRT_ERANGE;
4957 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4958 p[digits] = 0;
4960 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4961 GetLastError() == ERROR_FILE_NOT_FOUND)
4962 break;
4964 return 0;
4967 int CDECL MSVCRT_tmpnam_s(char *s, MSVCRT_size_t size)
4969 return tmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
4972 /*********************************************************************
4973 * tmpnam (MSVCRT.@)
4975 char * CDECL MSVCRT_tmpnam(char *s)
4977 if (!s) {
4978 thread_data_t *data = msvcrt_get_thread_data();
4980 if(!data->tmpnam_buffer)
4981 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4983 s = data->tmpnam_buffer;
4986 return tmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
4989 static int wtmpnam_helper(MSVCRT_wchar_t *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
4991 MSVCRT_wchar_t tmpstr[8];
4992 MSVCRT_wchar_t *p = s;
4993 int digits;
4995 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
4997 if (size < 3) {
4998 if (size) *s = 0;
4999 *MSVCRT__errno() = MSVCRT_ERANGE;
5000 return MSVCRT_ERANGE;
5002 *p++ = '\\';
5003 *p++ = 's';
5004 size -= 2;
5005 digits = msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
5006 if (digits+1 > size) {
5007 *s = 0;
5008 *MSVCRT__errno() = MSVCRT_ERANGE;
5009 return MSVCRT_ERANGE;
5011 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5012 p += digits;
5013 *p++ = '.';
5014 size -= digits+1;
5016 while(1) {
5017 while ((digits = *tmpnam_unique)+1 < tmp_max) {
5018 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
5019 break;
5022 digits = msvcrt_int_to_base32_w(digits, tmpstr);
5023 if (digits+1 > size) {
5024 *s = 0;
5025 *MSVCRT__errno() = MSVCRT_ERANGE;
5026 return MSVCRT_ERANGE;
5028 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5029 p[digits] = 0;
5031 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
5032 GetLastError() == ERROR_FILE_NOT_FOUND)
5033 break;
5035 return 0;
5038 /*********************************************************************
5039 * _wtmpnam_s (MSVCRT.@)
5041 int CDECL MSVCRT__wtmpnam_s(MSVCRT_wchar_t *s, MSVCRT_size_t size)
5043 return wtmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
5046 /*********************************************************************
5047 * _wtmpnam (MSVCRT.@)
5049 MSVCRT_wchar_t * CDECL MSVCRT__wtmpnam(MSVCRT_wchar_t *s)
5051 if (!s) {
5052 thread_data_t *data = msvcrt_get_thread_data();
5054 if(!data->wtmpnam_buffer)
5055 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
5057 s = data->wtmpnam_buffer;
5060 return wtmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
5063 /*********************************************************************
5064 * tmpfile (MSVCRT.@)
5066 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
5068 char *filename = MSVCRT__tempnam(",", "t");
5069 int fd;
5070 MSVCRT_FILE* file = NULL;
5072 LOCK_FILES();
5073 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
5074 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
5075 if (fd != -1 && (file = msvcrt_alloc_fp()))
5077 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
5079 file->_flag = 0;
5080 file = NULL;
5082 else file->_tmpfname = MSVCRT__strdup(filename);
5085 if(fd != -1 && !file)
5086 MSVCRT__close(fd);
5087 MSVCRT_free(filename);
5088 UNLOCK_FILES();
5089 return file;
5092 /*********************************************************************
5093 * tmpfile_s (MSVCRT.@)
5095 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
5097 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
5099 *file = MSVCRT_tmpfile();
5100 return 0;
5103 static int puts_clbk_file_a(void *file, int len, const char *str)
5105 return MSVCRT_fwrite(str, sizeof(char), len, file);
5108 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
5110 int i, ret;
5112 MSVCRT__lock_file(file);
5114 if(!(get_ioinfo_nolock(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
5115 ret = MSVCRT__fwrite_nolock(str, sizeof(MSVCRT_wchar_t), len, file);
5116 MSVCRT__unlock_file(file);
5117 return ret;
5120 for(i=0; i<len; i++) {
5121 if(MSVCRT__fputwc_nolock(str[i], file) == MSVCRT_WEOF) {
5122 MSVCRT__unlock_file(file);
5123 return -1;
5127 MSVCRT__unlock_file(file);
5128 return len;
5131 static int vfprintf_helper(DWORD options, MSVCRT_FILE* file, const char *format,
5132 MSVCRT__locale_t locale, __ms_va_list valist)
5134 printf_arg args_ctx[MSVCRT__ARGMAX+1];
5135 BOOL tmp_buf;
5136 int ret;
5138 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5139 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5141 if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
5142 memset(args_ctx, 0, sizeof(args_ctx));
5143 ret = create_positional_ctx_a(args_ctx, format, valist);
5144 if(ret < 0) {
5145 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
5146 *MSVCRT__errno() = MSVCRT_EINVAL;
5147 return ret;
5148 } else if(!ret)
5149 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5152 MSVCRT__lock_file(file);
5153 tmp_buf = add_std_buffer(file);
5154 ret = pf_printf_a(puts_clbk_file_a, file, format, locale, options,
5155 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
5156 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
5157 if(tmp_buf) remove_std_buffer(file);
5158 MSVCRT__unlock_file(file);
5160 return ret;
5163 static int vfwprintf_helper(DWORD options, MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5164 MSVCRT__locale_t locale, __ms_va_list valist)
5166 printf_arg args_ctx[MSVCRT__ARGMAX+1];
5167 BOOL tmp_buf;
5168 int ret;
5170 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5171 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5173 if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
5174 memset(args_ctx, 0, sizeof(args_ctx));
5175 ret = create_positional_ctx_w(args_ctx, format, valist);
5176 if(ret < 0) {
5177 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
5178 *MSVCRT__errno() = MSVCRT_EINVAL;
5179 return ret;
5180 } else if(!ret)
5181 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5184 MSVCRT__lock_file(file);
5185 tmp_buf = add_std_buffer(file);
5186 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, options,
5187 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
5188 options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
5189 if(tmp_buf) remove_std_buffer(file);
5190 MSVCRT__unlock_file(file);
5192 return ret;
5195 /*********************************************************************
5196 * _vfprintf_s_l (MSVCRT.@)
5198 int CDECL MSVCRT__vfprintf_s_l(MSVCRT_FILE* file, const char *format,
5199 MSVCRT__locale_t locale, __ms_va_list valist)
5201 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5204 /*********************************************************************
5205 * _vfwprintf_s_l (MSVCRT.@)
5207 int CDECL MSVCRT__vfwprintf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5208 MSVCRT__locale_t locale, __ms_va_list valist)
5210 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
5213 /*********************************************************************
5214 * vfprintf (MSVCRT.@)
5216 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5218 return vfprintf_helper(0, file, format, NULL, valist);
5221 /*********************************************************************
5222 * vfprintf_s (MSVCRT.@)
5224 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5226 return MSVCRT__vfprintf_s_l(file, format, NULL, valist);
5229 /*********************************************************************
5230 * vfwprintf (MSVCRT.@)
5232 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5234 return vfwprintf_helper(0, file, format, NULL, valist);
5237 /*********************************************************************
5238 * vfwprintf_s (MSVCRT.@)
5240 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5242 return MSVCRT__vfwprintf_s_l(file, format, NULL, valist);
5245 #if _MSVCR_VER >= 140
5247 /*********************************************************************
5248 * __stdio_common_vfprintf (UCRTBASE.@)
5250 int CDECL MSVCRT__stdio_common_vfprintf(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
5251 MSVCRT__locale_t locale, __ms_va_list valist)
5253 if (options & ~UCRTBASE_PRINTF_MASK)
5254 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5256 return vfprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
5259 /*********************************************************************
5260 * __stdio_common_vfprintf_s (UCRTBASE.@)
5262 int CDECL MSVCRT__stdio_common_vfprintf_s(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
5263 MSVCRT__locale_t locale, __ms_va_list valist)
5265 if (options & ~UCRTBASE_PRINTF_MASK)
5266 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5268 return vfprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5269 file, format, locale, valist);
5272 /*********************************************************************
5273 * __stdio_common_vfwprintf (UCRTBASE.@)
5275 int CDECL MSVCRT__stdio_common_vfwprintf(unsigned __int64 options, MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
5276 MSVCRT__locale_t locale, __ms_va_list valist)
5278 if (options & ~UCRTBASE_PRINTF_MASK)
5279 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5281 return vfwprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
5284 /*********************************************************************
5285 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5287 int CDECL MSVCRT__stdio_common_vfwprintf_s(unsigned __int64 options, MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
5288 MSVCRT__locale_t locale, __ms_va_list valist)
5290 if (options & ~UCRTBASE_PRINTF_MASK)
5291 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5293 return vfwprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5294 file, format, locale, valist);
5297 #endif /* _MSVCR_VER >= 140 */
5299 /*********************************************************************
5300 * _vfprintf_l (MSVCRT.@)
5302 int CDECL MSVCRT__vfprintf_l(MSVCRT_FILE* file, const char *format,
5303 MSVCRT__locale_t locale, __ms_va_list valist)
5305 return vfprintf_helper(0, file, format, locale, valist);
5308 /*********************************************************************
5309 * _vfwprintf_l (MSVCRT.@)
5311 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5312 MSVCRT__locale_t locale, __ms_va_list valist)
5314 return vfwprintf_helper(0, file, format, locale, valist);
5317 /*********************************************************************
5318 * _vfprintf_p_l (MSVCRT.@)
5320 int CDECL MSVCRT__vfprintf_p_l(MSVCRT_FILE* file, const char *format,
5321 MSVCRT__locale_t locale, __ms_va_list valist)
5323 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5324 file, format, locale, valist);
5327 /*********************************************************************
5328 * _vfprintf_p (MSVCRT.@)
5330 int CDECL MSVCRT__vfprintf_p(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5332 return MSVCRT__vfprintf_p_l(file, format, NULL, valist);
5335 /*********************************************************************
5336 * _vfwprintf_p_l (MSVCRT.@)
5338 int CDECL MSVCRT__vfwprintf_p_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5339 MSVCRT__locale_t locale, __ms_va_list valist)
5341 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5342 file, format, locale, valist);
5345 /*********************************************************************
5346 * _vfwprintf_p (MSVCRT.@)
5348 int CDECL MSVCRT__vfwprintf_p(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5350 return MSVCRT__vfwprintf_p_l(file, format, NULL, valist);
5353 /*********************************************************************
5354 * vprintf (MSVCRT.@)
5356 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
5358 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
5361 /*********************************************************************
5362 * vprintf_s (MSVCRT.@)
5364 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
5366 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
5369 /*********************************************************************
5370 * vwprintf (MSVCRT.@)
5372 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
5374 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
5377 /*********************************************************************
5378 * vwprintf_s (MSVCRT.@)
5380 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
5382 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
5385 /*********************************************************************
5386 * fprintf (MSVCRT.@)
5388 int WINAPIV MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
5390 __ms_va_list valist;
5391 int res;
5392 __ms_va_start(valist, format);
5393 res = MSVCRT_vfprintf(file, format, valist);
5394 __ms_va_end(valist);
5395 return res;
5398 /*********************************************************************
5399 * fprintf_s (MSVCRT.@)
5401 int WINAPIV MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
5403 __ms_va_list valist;
5404 int res;
5405 __ms_va_start(valist, format);
5406 res = MSVCRT_vfprintf_s(file, format, valist);
5407 __ms_va_end(valist);
5408 return res;
5411 /*********************************************************************
5412 * fwprintf (MSVCRT.@)
5414 int WINAPIV MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5416 __ms_va_list valist;
5417 int res;
5418 __ms_va_start(valist, format);
5419 res = MSVCRT_vfwprintf(file, format, valist);
5420 __ms_va_end(valist);
5421 return res;
5424 /*********************************************************************
5425 * fwprintf_s (MSVCRT.@)
5427 int WINAPIV MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5429 __ms_va_list valist;
5430 int res;
5431 __ms_va_start(valist, format);
5432 res = MSVCRT_vfwprintf_s(file, format, valist);
5433 __ms_va_end(valist);
5434 return res;
5437 /*********************************************************************
5438 * _fwprintf_l (MSVCRT.@)
5440 int WINAPIV MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
5442 __ms_va_list valist;
5443 int res;
5444 __ms_va_start(valist, locale);
5445 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
5446 __ms_va_end(valist);
5447 return res;
5450 /*********************************************************************
5451 * printf (MSVCRT.@)
5453 int WINAPIV MSVCRT_printf(const char *format, ...)
5455 __ms_va_list valist;
5456 int res;
5457 __ms_va_start(valist, format);
5458 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
5459 __ms_va_end(valist);
5460 return res;
5463 /*********************************************************************
5464 * printf_s (MSVCRT.@)
5466 int WINAPIV MSVCRT_printf_s(const char *format, ...)
5468 __ms_va_list valist;
5469 int res;
5470 __ms_va_start(valist, format);
5471 res = MSVCRT_vprintf_s(format, valist);
5472 __ms_va_end(valist);
5473 return res;
5476 /*********************************************************************
5477 * ungetc (MSVCRT.@)
5479 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
5481 int ret;
5483 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5485 MSVCRT__lock_file(file);
5486 ret = MSVCRT__ungetc_nolock(c, file);
5487 MSVCRT__unlock_file(file);
5489 return ret;
5492 /*********************************************************************
5493 * _ungetc_nolock (MSVCRT.@)
5495 int CDECL MSVCRT__ungetc_nolock(int c, MSVCRT_FILE * file)
5497 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5499 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
5500 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
5501 return MSVCRT_EOF;
5503 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
5504 && msvcrt_alloc_buffer(file))
5505 || (!file->_cnt && file->_ptr==file->_base))
5506 file->_ptr++;
5508 if(file->_ptr>file->_base) {
5509 file->_ptr--;
5510 if(file->_flag & MSVCRT__IOSTRG) {
5511 if(*file->_ptr != c) {
5512 file->_ptr++;
5513 return MSVCRT_EOF;
5515 }else {
5516 *file->_ptr = c;
5518 file->_cnt++;
5519 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
5520 file->_flag |= MSVCRT__IOREAD;
5521 return c;
5524 return MSVCRT_EOF;
5527 /*********************************************************************
5528 * ungetwc (MSVCRT.@)
5530 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5532 MSVCRT_wint_t ret;
5534 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5536 MSVCRT__lock_file(file);
5537 ret = MSVCRT__ungetwc_nolock(wc, file);
5538 MSVCRT__unlock_file(file);
5540 return ret;
5543 /*********************************************************************
5544 * _ungetwc_nolock (MSVCRT.@)
5546 MSVCRT_wint_t CDECL MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5548 MSVCRT_wchar_t mwc = wc;
5550 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5551 if (wc == MSVCRT_WEOF)
5552 return MSVCRT_WEOF;
5554 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5555 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5556 unsigned char * pp = (unsigned char *)&mwc;
5557 int i;
5559 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
5560 if(pp[i] != MSVCRT__ungetc_nolock(pp[i],file))
5561 return MSVCRT_WEOF;
5563 }else {
5564 char mbs[MSVCRT_MB_LEN_MAX];
5565 int len;
5567 len = MSVCRT_wctomb(mbs, mwc);
5568 if(len == -1)
5569 return MSVCRT_WEOF;
5571 for(len--; len>=0; len--) {
5572 if(mbs[len] != MSVCRT__ungetc_nolock(mbs[len], file))
5573 return MSVCRT_WEOF;
5577 return mwc;
5580 /*********************************************************************
5581 * wprintf (MSVCRT.@)
5583 int WINAPIV MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
5585 __ms_va_list valist;
5586 int res;
5587 __ms_va_start(valist, format);
5588 res = MSVCRT_vwprintf(format, valist);
5589 __ms_va_end(valist);
5590 return res;
5593 /*********************************************************************
5594 * wprintf_s (MSVCRT.@)
5596 int WINAPIV MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
5598 __ms_va_list valist;
5599 int res;
5600 __ms_va_start(valist, format);
5601 res = MSVCRT_vwprintf_s(format, valist);
5602 __ms_va_end(valist);
5603 return res;
5606 /*********************************************************************
5607 * _getmaxstdio (MSVCRT.@)
5609 int CDECL MSVCRT__getmaxstdio(void)
5611 return MSVCRT_max_streams;
5614 /*********************************************************************
5615 * _setmaxstdio (MSVCRT.@)
5617 int CDECL MSVCRT__setmaxstdio(int newmax)
5619 TRACE("%d\n", newmax);
5621 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5622 return -1;
5624 MSVCRT_max_streams = newmax;
5625 return MSVCRT_max_streams;
5628 #if _MSVCR_VER >= 140
5629 /*********************************************************************
5630 * _get_stream_buffer_pointers (UCRTBASE.@)
5632 int CDECL MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE *file, char*** base,
5633 char*** ptr, int** count)
5635 if (base)
5636 *base = &file->_base;
5637 if (ptr)
5638 *ptr = &file->_ptr;
5639 if (count)
5640 *count = &file->_cnt;
5641 return 0;
5643 #endif