msvcrt: Added _fflush_nolock implementation.
[wine.git] / dlls / msvcrt / file.c
blob2e07342c1246af354be133a31ec44e7bcd8b26dc
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 static int MSVCRT_fdstart = 3; /* first unallocated fd */
118 static int MSVCRT_fdend = 3; /* highest allocated fd */
120 typedef struct {
121 MSVCRT_FILE file;
122 CRITICAL_SECTION crit;
123 } file_crit;
125 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
126 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
127 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
129 /* INTERNAL: process umask */
130 static int MSVCRT_umask = 0;
132 /* INTERNAL: static data for tmpnam and _wtmpname functions */
133 static int tmpnam_unique;
135 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
136 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
137 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
138 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
140 #define TOUL(x) (ULONGLONG)(x)
141 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
142 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
143 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
144 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
146 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
147 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
148 * and MSVCRT_stream_idx, from race conditions.
149 * It doesn't protect against race conditions manipulating the underlying files
150 * or flags; doing so would probably be better accomplished with per-file
151 * protection, rather than locking the whole table for every change.
153 static CRITICAL_SECTION MSVCRT_file_cs;
154 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
156 0, 0, &MSVCRT_file_cs,
157 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
158 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
160 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
161 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
162 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
164 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
166 buf->st_dev = buf64->st_dev;
167 buf->st_ino = buf64->st_ino;
168 buf->st_mode = buf64->st_mode;
169 buf->st_nlink = buf64->st_nlink;
170 buf->st_uid = buf64->st_uid;
171 buf->st_gid = buf64->st_gid;
172 buf->st_rdev = buf64->st_rdev;
173 buf->st_size = buf64->st_size;
174 buf->st_atime = buf64->st_atime;
175 buf->st_mtime = buf64->st_mtime;
176 buf->st_ctime = buf64->st_ctime;
179 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
181 buf->st_dev = buf64->st_dev;
182 buf->st_ino = buf64->st_ino;
183 buf->st_mode = buf64->st_mode;
184 buf->st_nlink = buf64->st_nlink;
185 buf->st_uid = buf64->st_uid;
186 buf->st_gid = buf64->st_gid;
187 buf->st_rdev = buf64->st_rdev;
188 buf->st_size = buf64->st_size;
189 buf->st_atime = buf64->st_atime;
190 buf->st_mtime = buf64->st_mtime;
191 buf->st_ctime = buf64->st_ctime;
194 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32 *buf)
196 buf->st_dev = buf64->st_dev;
197 buf->st_ino = buf64->st_ino;
198 buf->st_mode = buf64->st_mode;
199 buf->st_nlink = buf64->st_nlink;
200 buf->st_uid = buf64->st_uid;
201 buf->st_gid = buf64->st_gid;
202 buf->st_rdev = buf64->st_rdev;
203 buf->st_size = buf64->st_size;
204 buf->st_atime = buf64->st_atime;
205 buf->st_mtime = buf64->st_mtime;
206 buf->st_ctime = buf64->st_ctime;
209 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat64i32 *buf)
211 buf->st_dev = buf64->st_dev;
212 buf->st_ino = buf64->st_ino;
213 buf->st_mode = buf64->st_mode;
214 buf->st_nlink = buf64->st_nlink;
215 buf->st_uid = buf64->st_uid;
216 buf->st_gid = buf64->st_gid;
217 buf->st_rdev = buf64->st_rdev;
218 buf->st_size = buf64->st_size;
219 buf->st_atime = buf64->st_atime;
220 buf->st_mtime = buf64->st_mtime;
221 buf->st_ctime = buf64->st_ctime;
224 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32i64 *buf)
226 buf->st_dev = buf64->st_dev;
227 buf->st_ino = buf64->st_ino;
228 buf->st_mode = buf64->st_mode;
229 buf->st_nlink = buf64->st_nlink;
230 buf->st_uid = buf64->st_uid;
231 buf->st_gid = buf64->st_gid;
232 buf->st_rdev = buf64->st_rdev;
233 buf->st_size = buf64->st_size;
234 buf->st_atime = buf64->st_atime;
235 buf->st_mtime = buf64->st_mtime;
236 buf->st_ctime = buf64->st_ctime;
239 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
241 /* 1601 to 1970 is 369 years plus 89 leap days */
242 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
244 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
245 ft->dwHighDateTime = ticks >> 32;
246 ft->dwLowDateTime = ticks;
249 static inline ioinfo* msvcrt_get_ioinfo(int fd)
251 ioinfo *ret = NULL;
252 if(fd < MSVCRT_MAX_FILES)
253 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
254 if(!ret)
255 return &MSVCRT___badioinfo;
257 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
260 static inline MSVCRT_FILE* msvcrt_get_file(int i)
262 file_crit *ret;
264 if(i >= MSVCRT_max_streams)
265 return NULL;
267 if(i < _IOB_ENTRIES)
268 return &MSVCRT__iob[i];
270 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
271 if(!ret) {
272 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
273 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
274 ERR("out of memory\n");
275 *MSVCRT__errno() = MSVCRT_ENOMEM;
276 return NULL;
279 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
280 } else
281 ret += i%MSVCRT_FD_BLOCK_SIZE;
283 return &ret->file;
286 static inline BOOL msvcrt_is_valid_fd(int fd)
288 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
291 /* INTERNAL: Get the HANDLE for a fd
292 * This doesn't lock the table, because a failure will result in
293 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
294 * it returns a valid handle which is about to be closed, a subsequent call
295 * will fail, most likely in a sane way.
297 static HANDLE msvcrt_fdtoh(int fd)
299 if (!msvcrt_is_valid_fd(fd))
301 WARN(":fd (%d) - no handle!\n",fd);
302 *MSVCRT___doserrno() = 0;
303 *MSVCRT__errno() = MSVCRT_EBADF;
304 return INVALID_HANDLE_VALUE;
306 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
307 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd);
308 return msvcrt_get_ioinfo(fd)->handle;
311 /* INTERNAL: free a file entry fd */
312 static void msvcrt_free_fd(int fd)
314 ioinfo *fdinfo;
316 LOCK_FILES();
317 fdinfo = msvcrt_get_ioinfo(fd);
318 if(fdinfo != &MSVCRT___badioinfo)
320 fdinfo->handle = INVALID_HANDLE_VALUE;
321 fdinfo->wxflag = 0;
323 TRACE(":fd (%d) freed\n",fd);
325 if (fd < 3)
327 switch (fd)
329 case 0:
330 SetStdHandle(STD_INPUT_HANDLE, 0);
331 break;
332 case 1:
333 SetStdHandle(STD_OUTPUT_HANDLE, 0);
334 break;
335 case 2:
336 SetStdHandle(STD_ERROR_HANDLE, 0);
337 break;
341 if (fd == MSVCRT_fdend - 1)
342 MSVCRT_fdend--;
343 if (fd < MSVCRT_fdstart)
344 MSVCRT_fdstart = fd;
345 UNLOCK_FILES();
348 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
349 /* caller must hold the files lock */
350 static int msvcrt_set_fd(HANDLE hand, int flag, int fd)
352 ioinfo *fdinfo;
354 if (fd >= MSVCRT_MAX_FILES)
356 WARN(":files exhausted!\n");
357 *MSVCRT__errno() = MSVCRT_ENFILE;
358 return -1;
361 fdinfo = msvcrt_get_ioinfo(fd);
362 if(fdinfo == &MSVCRT___badioinfo) {
363 int i;
365 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
366 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
367 WARN(":out of memory!\n");
368 *MSVCRT__errno() = MSVCRT_ENOMEM;
369 return -1;
372 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
373 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
375 fdinfo = msvcrt_get_ioinfo(fd);
378 fdinfo->handle = hand;
379 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_TTY));
380 fdinfo->lookahead[0] = '\n';
381 fdinfo->lookahead[1] = '\n';
382 fdinfo->lookahead[2] = '\n';
383 fdinfo->exflag = 0;
385 /* locate next free slot */
386 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
387 MSVCRT_fdstart = MSVCRT_fdend + 1;
388 else
389 while (MSVCRT_fdstart < MSVCRT_fdend &&
390 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
391 MSVCRT_fdstart++;
392 /* update last fd in use */
393 if (fd >= MSVCRT_fdend)
394 MSVCRT_fdend = fd + 1;
395 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
397 switch (fd)
399 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
400 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
401 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
404 return fd;
407 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
408 static int msvcrt_alloc_fd(HANDLE hand, int flag)
410 int ret;
412 LOCK_FILES();
413 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
414 ret = msvcrt_set_fd(hand, flag, MSVCRT_fdstart);
415 UNLOCK_FILES();
416 return ret;
419 /* INTERNAL: Allocate a FILE* for an fd slot */
420 /* caller must hold the files lock */
421 static MSVCRT_FILE* msvcrt_alloc_fp(void)
423 int i;
424 MSVCRT_FILE *file;
426 for (i = 3; i < MSVCRT_max_streams; i++)
428 file = msvcrt_get_file(i);
429 if (!file)
430 return NULL;
432 if (file->_flag == 0)
434 if (i == MSVCRT_stream_idx)
436 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
438 InitializeCriticalSection(&((file_crit*)file)->crit);
439 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
441 MSVCRT_stream_idx++;
443 return file;
447 return NULL;
450 /* INTERNAL: initialize a FILE* from an open fd */
451 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
453 TRACE(":fd (%d) allocating FILE*\n",fd);
454 if (!msvcrt_is_valid_fd(fd))
456 WARN(":invalid fd %d\n",fd);
457 *MSVCRT___doserrno() = 0;
458 *MSVCRT__errno() = MSVCRT_EBADF;
459 return -1;
461 file->_ptr = file->_base = NULL;
462 file->_cnt = 0;
463 file->_file = fd;
464 file->_flag = stream_flags;
465 file->_tmpfname = NULL;
467 TRACE(":got FILE* (%p)\n",file);
468 return 0;
471 /* INTERNAL: Create an inheritance data block (for spawned process)
472 * The inheritance block is made of:
473 * 00 int nb of file descriptor (NBFD)
474 * 04 char file flags (wxflag): repeated for each fd
475 * 4+NBFD HANDLE file handle: repeated for each fd
477 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
479 int fd;
480 char* wxflag_ptr;
481 HANDLE* handle_ptr;
482 ioinfo* fdinfo;
484 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
485 *block = MSVCRT_calloc(*size, 1);
486 if (!*block)
488 *size = 0;
489 return FALSE;
491 wxflag_ptr = (char*)*block + sizeof(unsigned);
492 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
494 *(unsigned*)*block = MSVCRT_fdend;
495 for (fd = 0; fd < MSVCRT_fdend; fd++)
497 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
498 fdinfo = msvcrt_get_ioinfo(fd);
499 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
501 *wxflag_ptr = fdinfo->wxflag;
502 *handle_ptr = fdinfo->handle;
504 else
506 *wxflag_ptr = 0;
507 *handle_ptr = INVALID_HANDLE_VALUE;
509 wxflag_ptr++; handle_ptr++;
511 return TRUE;
514 /* INTERNAL: Set up all file descriptors,
515 * as well as default streams (stdin, stderr and stdout)
517 void msvcrt_init_io(void)
519 STARTUPINFOA si;
520 int i;
521 ioinfo *fdinfo;
523 GetStartupInfoA(&si);
524 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
526 BYTE* wxflag_ptr;
527 HANDLE* handle_ptr;
528 unsigned int count;
530 count = *(unsigned*)si.lpReserved2;
531 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
532 handle_ptr = (HANDLE*)(wxflag_ptr + count);
534 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
535 count = min(count, MSVCRT_MAX_FILES);
536 for (i = 0; i < count; i++)
538 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
539 msvcrt_set_fd(*handle_ptr, *wxflag_ptr, i);
541 wxflag_ptr++; handle_ptr++;
543 MSVCRT_fdend = max( 3, count );
544 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
545 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
548 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO);
549 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
550 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
551 DWORD type = GetFileType(h);
553 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
554 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDIN_FILENO);
557 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO);
558 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
559 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
560 DWORD type = GetFileType(h);
562 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
563 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDOUT_FILENO);
566 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO);
567 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
568 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
569 DWORD type = GetFileType(h);
571 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
572 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDERR_FILENO);
575 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO)->handle,
576 msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO)->handle,
577 msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO)->handle);
579 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
580 for (i = 0; i < 3; i++)
582 /* FILE structs for stdin/out/err are static and never deleted */
583 MSVCRT__iob[i]._file = i;
584 MSVCRT__iob[i]._tmpfname = NULL;
585 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
587 MSVCRT_stream_idx = 3;
590 /* INTERNAL: Flush stdio file buffer */
591 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
593 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
594 int cnt=file->_ptr-file->_base;
595 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
596 file->_flag |= MSVCRT__IOERR;
597 return MSVCRT_EOF;
599 file->_ptr=file->_base;
600 file->_cnt=0;
602 return 0;
605 /*********************************************************************
606 * _isatty (MSVCRT.@)
608 int CDECL MSVCRT__isatty(int fd)
610 TRACE(":fd (%d)\n",fd);
612 return msvcrt_get_ioinfo(fd)->wxflag & WX_TTY;
615 /* INTERNAL: Allocate stdio file buffer */
616 static BOOL msvcrt_alloc_buffer(MSVCRT_FILE* file)
618 if((file->_file==MSVCRT_STDOUT_FILENO || file->_file==MSVCRT_STDERR_FILENO)
619 && MSVCRT__isatty(file->_file))
620 return FALSE;
622 file->_base = MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ,1);
623 if(file->_base) {
624 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
625 file->_flag |= MSVCRT__IOMYBUF;
626 } else {
627 file->_base = (char*)(&file->_charbuf);
628 file->_bufsiz = 2;
629 file->_flag |= MSVCRT__IONBF;
631 file->_ptr = file->_base;
632 file->_cnt = 0;
633 return TRUE;
636 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
637 static BOOL add_std_buffer(MSVCRT_FILE *file)
639 static char buffers[2][MSVCRT_BUFSIZ];
641 if((file->_file!=MSVCRT_STDOUT_FILENO && file->_file!=MSVCRT_STDERR_FILENO)
642 || (file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
643 || !MSVCRT__isatty(file->_file))
644 return FALSE;
646 file->_ptr = file->_base = buffers[file->_file == MSVCRT_STDOUT_FILENO ? 0 : 1];
647 file->_bufsiz = file->_cnt = MSVCRT_BUFSIZ;
648 file->_flag |= MSVCRT__USERBUF;
649 return TRUE;
652 /* INTERNAL: Removes temporary buffer from stdout or stderr */
653 /* Only call this function when add_std_buffer returned TRUE */
654 static void remove_std_buffer(MSVCRT_FILE *file)
656 msvcrt_flush_buffer(file);
657 file->_ptr = file->_base = NULL;
658 file->_bufsiz = file->_cnt = 0;
659 file->_flag &= ~MSVCRT__USERBUF;
662 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
663 static int msvcrt_int_to_base32(int num, char *str)
665 char *p;
666 int n = num;
667 int digits = 0;
669 while (n != 0)
671 n >>= 5;
672 digits++;
674 p = str + digits;
675 *p = 0;
676 while (--p >= str)
678 *p = (num & 31) + '0';
679 if (*p > '9')
680 *p += ('a' - '0' - 10);
681 num >>= 5;
684 return digits;
687 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
688 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
690 MSVCRT_wchar_t *p;
691 int n = num;
692 int digits = 0;
694 while (n != 0)
696 n >>= 5;
697 digits++;
699 p = str + digits;
700 *p = 0;
701 while (--p >= str)
703 *p = (num & 31) + '0';
704 if (*p > '9')
705 *p += ('a' - '0' - 10);
706 num >>= 5;
709 return digits;
712 /*********************************************************************
713 * __iob_func(MSVCRT.@)
715 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
717 return &MSVCRT__iob[0];
720 /*********************************************************************
721 * _access (MSVCRT.@)
723 int CDECL MSVCRT__access(const char *filename, int mode)
725 DWORD attr = GetFileAttributesA(filename);
727 TRACE("(%s,%d) %d\n",filename,mode,attr);
729 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
731 msvcrt_set_errno(GetLastError());
732 return -1;
734 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
736 msvcrt_set_errno(ERROR_ACCESS_DENIED);
737 return -1;
739 return 0;
742 /*********************************************************************
743 * _access_s (MSVCRT.@)
745 int CDECL MSVCRT__access_s(const char *filename, int mode)
747 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
748 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
750 if (MSVCRT__access(filename, mode) == -1)
751 return *MSVCRT__errno();
752 return 0;
755 /*********************************************************************
756 * _waccess (MSVCRT.@)
758 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
760 DWORD attr = GetFileAttributesW(filename);
762 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
764 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
766 msvcrt_set_errno(GetLastError());
767 return -1;
769 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
771 msvcrt_set_errno(ERROR_ACCESS_DENIED);
772 return -1;
774 return 0;
777 /*********************************************************************
778 * _waccess_s (MSVCRT.@)
780 int CDECL MSVCRT__waccess_s(const MSVCRT_wchar_t *filename, int mode)
782 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
783 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
785 if (MSVCRT__waccess(filename, mode) == -1)
786 return *MSVCRT__errno();
787 return 0;
790 /*********************************************************************
791 * _chmod (MSVCRT.@)
793 int CDECL MSVCRT__chmod(const char *path, int flags)
795 DWORD oldFlags = GetFileAttributesA(path);
797 if (oldFlags != INVALID_FILE_ATTRIBUTES)
799 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
800 oldFlags | FILE_ATTRIBUTE_READONLY;
802 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
803 return 0;
805 msvcrt_set_errno(GetLastError());
806 return -1;
809 /*********************************************************************
810 * _wchmod (MSVCRT.@)
812 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
814 DWORD oldFlags = GetFileAttributesW(path);
816 if (oldFlags != INVALID_FILE_ATTRIBUTES)
818 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
819 oldFlags | FILE_ATTRIBUTE_READONLY;
821 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
822 return 0;
824 msvcrt_set_errno(GetLastError());
825 return -1;
828 /*********************************************************************
829 * _unlink (MSVCRT.@)
831 int CDECL MSVCRT__unlink(const char *path)
833 TRACE("%s\n",debugstr_a(path));
834 if(DeleteFileA(path))
835 return 0;
836 TRACE("failed (%d)\n",GetLastError());
837 msvcrt_set_errno(GetLastError());
838 return -1;
841 /*********************************************************************
842 * _wunlink (MSVCRT.@)
844 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
846 TRACE("(%s)\n",debugstr_w(path));
847 if(DeleteFileW(path))
848 return 0;
849 TRACE("failed (%d)\n",GetLastError());
850 msvcrt_set_errno(GetLastError());
851 return -1;
854 /*********************************************************************
855 * _commit (MSVCRT.@)
857 int CDECL MSVCRT__commit(int fd)
859 HANDLE hand = msvcrt_fdtoh(fd);
861 TRACE(":fd (%d) handle (%p)\n",fd,hand);
862 if (hand == INVALID_HANDLE_VALUE)
863 return -1;
865 if (!FlushFileBuffers(hand))
867 if (GetLastError() == ERROR_INVALID_HANDLE)
869 /* FlushFileBuffers fails for console handles
870 * so we ignore this error.
872 return 0;
874 TRACE(":failed-last error (%d)\n",GetLastError());
875 msvcrt_set_errno(GetLastError());
876 return -1;
878 TRACE(":ok\n");
879 return 0;
882 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
883 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
885 /* INTERNAL: Flush all stream buffer */
886 static int msvcrt_flush_all_buffers(int mask)
888 int i, num_flushed = 0;
889 MSVCRT_FILE *file;
891 LOCK_FILES();
892 for (i = 0; i < MSVCRT_stream_idx; i++) {
893 file = msvcrt_get_file(i);
895 if (file->_flag)
897 if(file->_flag & mask) {
898 MSVCRT_fflush(file);
899 num_flushed++;
903 UNLOCK_FILES();
905 TRACE(":flushed (%d) handles\n",num_flushed);
906 return num_flushed;
909 /*********************************************************************
910 * _flushall (MSVCRT.@)
912 int CDECL MSVCRT__flushall(void)
914 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
917 /*********************************************************************
918 * fflush (MSVCRT.@)
920 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
922 int ret;
924 if(!file) {
925 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
926 ret = 0;
927 } else {
928 MSVCRT__lock_file(file);
929 ret = MSVCRT__fflush_nolock(file);
930 MSVCRT__unlock_file(file);
933 return ret;
936 /*********************************************************************
937 * _fflush_nolock (MSVCRT.@)
939 int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file)
941 if(!file) {
942 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
943 } else if(file->_flag & MSVCRT__IOWRT) {
944 int res;
946 res = msvcrt_flush_buffer(file);
947 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
948 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
950 return res;
951 } else if(file->_flag & MSVCRT__IOREAD) {
952 file->_cnt = 0;
953 file->_ptr = file->_base;
955 return 0;
957 return 0;
960 /*********************************************************************
961 * _close (MSVCRT.@)
963 int CDECL MSVCRT__close(int fd)
965 HANDLE hand;
966 int ret;
968 LOCK_FILES();
969 hand = msvcrt_fdtoh(fd);
970 TRACE(":fd (%d) handle (%p)\n",fd,hand);
971 if (!msvcrt_is_valid_fd(fd)) {
972 ret = -1;
973 } else {
974 msvcrt_free_fd(fd);
975 ret = CloseHandle(hand) ? 0 : -1;
976 if (ret) {
977 WARN(":failed-last error (%d)\n",GetLastError());
978 msvcrt_set_errno(GetLastError());
981 UNLOCK_FILES();
982 TRACE(":ok\n");
983 return ret;
986 /*********************************************************************
987 * _dup2 (MSVCRT.@)
988 * NOTES
989 * MSDN isn't clear on this point, but the remarks for _pipe
990 * indicate file descriptors duplicated with _dup and _dup2 are always
991 * inheritable.
993 int CDECL MSVCRT__dup2(int od, int nd)
995 int ret;
997 TRACE("(od=%d, nd=%d)\n", od, nd);
998 LOCK_FILES();
999 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
1001 HANDLE handle;
1003 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
1004 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1006 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
1008 if (msvcrt_is_valid_fd(nd))
1009 MSVCRT__close(nd);
1010 ret = msvcrt_set_fd(handle, wxflag, nd);
1011 if (ret == -1)
1013 CloseHandle(handle);
1014 *MSVCRT__errno() = MSVCRT_EMFILE;
1016 else
1018 /* _dup2 returns 0, not nd, on success */
1019 ret = 0;
1022 else
1024 ret = -1;
1025 msvcrt_set_errno(GetLastError());
1028 else
1030 *MSVCRT__errno() = MSVCRT_EBADF;
1031 ret = -1;
1033 UNLOCK_FILES();
1034 return ret;
1037 /*********************************************************************
1038 * _dup (MSVCRT.@)
1040 int CDECL MSVCRT__dup(int od)
1042 int fd, ret;
1044 LOCK_FILES();
1045 fd = MSVCRT_fdstart;
1046 if (MSVCRT__dup2(od, fd) == 0)
1047 ret = fd;
1048 else
1049 ret = -1;
1050 UNLOCK_FILES();
1051 return ret;
1054 /*********************************************************************
1055 * _eof (MSVCRT.@)
1057 int CDECL MSVCRT__eof(int fd)
1059 DWORD curpos,endpos;
1060 LONG hcurpos,hendpos;
1061 HANDLE hand = msvcrt_fdtoh(fd);
1063 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1065 if (hand == INVALID_HANDLE_VALUE)
1066 return -1;
1068 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
1070 /* Otherwise we do it the hard way */
1071 hcurpos = hendpos = 0;
1072 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
1073 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
1075 if (curpos == endpos && hcurpos == hendpos)
1077 /* FIXME: shouldn't WX_ATEOF be set here? */
1078 return TRUE;
1081 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
1082 return FALSE;
1085 /*********************************************************************
1086 * _fcloseall (MSVCRT.@)
1088 int CDECL MSVCRT__fcloseall(void)
1090 int num_closed = 0, i;
1091 MSVCRT_FILE *file;
1093 LOCK_FILES();
1094 for (i = 3; i < MSVCRT_stream_idx; i++) {
1095 file = msvcrt_get_file(i);
1097 if (file->_flag && !MSVCRT_fclose(file))
1098 num_closed++;
1100 UNLOCK_FILES();
1102 TRACE(":closed (%d) handles\n",num_closed);
1103 return num_closed;
1106 /* free everything on process exit */
1107 void msvcrt_free_io(void)
1109 unsigned int i;
1110 int j;
1112 MSVCRT__flushall();
1113 MSVCRT__fcloseall();
1115 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1116 MSVCRT_free(MSVCRT___pioinfo[i]);
1118 for(j=0; j<MSVCRT_stream_idx; j++)
1120 MSVCRT_FILE *file = msvcrt_get_file(j);
1121 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1123 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1124 DeleteCriticalSection(&((file_crit*)file)->crit);
1128 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1129 MSVCRT_free(MSVCRT_fstream[i]);
1131 DeleteCriticalSection(&MSVCRT_file_cs);
1134 /*********************************************************************
1135 * _lseeki64 (MSVCRT.@)
1137 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1139 HANDLE hand = msvcrt_fdtoh(fd);
1140 LARGE_INTEGER ofs;
1142 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1143 if (hand == INVALID_HANDLE_VALUE)
1144 return -1;
1146 if (whence < 0 || whence > 2)
1148 *MSVCRT__errno() = MSVCRT_EINVAL;
1149 return -1;
1152 TRACE(":fd (%d) to %s pos %s\n",
1153 fd,wine_dbgstr_longlong(offset),
1154 (whence==SEEK_SET)?"SEEK_SET":
1155 (whence==SEEK_CUR)?"SEEK_CUR":
1156 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1158 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1159 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1160 ofs.QuadPart = offset;
1161 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1162 GetLastError() == ERROR_SUCCESS)
1164 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_ATEOF;
1165 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1167 return ofs.QuadPart;
1169 TRACE(":error-last error (%d)\n",GetLastError());
1170 msvcrt_set_errno(GetLastError());
1171 return -1;
1174 /*********************************************************************
1175 * _lseek (MSVCRT.@)
1177 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1179 return MSVCRT__lseeki64(fd, offset, whence);
1182 /*********************************************************************
1183 * _lock_file (MSVCRT.@)
1185 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1187 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1188 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1189 else
1190 EnterCriticalSection(&((file_crit*)file)->crit);
1193 /*********************************************************************
1194 * _unlock_file (MSVCRT.@)
1196 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1198 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1199 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1200 else
1201 LeaveCriticalSection(&((file_crit*)file)->crit);
1204 /*********************************************************************
1205 * _locking (MSVCRT.@)
1207 * This is untested; the underlying LockFile doesn't work yet.
1209 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1211 BOOL ret;
1212 DWORD cur_locn;
1213 HANDLE hand = msvcrt_fdtoh(fd);
1215 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1216 if (hand == INVALID_HANDLE_VALUE)
1217 return -1;
1219 if (mode < 0 || mode > 4)
1221 *MSVCRT__errno() = MSVCRT_EINVAL;
1222 return -1;
1225 TRACE(":fd (%d) by 0x%08x mode %s\n",
1226 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1227 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1228 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1229 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1230 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1231 "UNKNOWN");
1233 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1235 FIXME ("Seek failed\n");
1236 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1237 return -1;
1239 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1241 int nretry = 10;
1242 ret = 1; /* just to satisfy gcc */
1243 while (nretry--)
1245 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1246 if (ret) break;
1247 Sleep(1);
1250 else if (mode == MSVCRT__LK_UNLCK)
1251 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1252 else
1253 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1254 /* FIXME - what about error settings? */
1255 return ret ? 0 : -1;
1258 /*********************************************************************
1259 * _fseeki64 (MSVCRT.@)
1261 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1263 int ret;
1265 MSVCRT__lock_file(file);
1266 ret = MSVCRT__fseeki64_nolock(file, offset, whence);
1267 MSVCRT__unlock_file(file);
1269 return ret;
1272 /*********************************************************************
1273 * _fseeki64_nolock (MSVCRT.@)
1275 int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
1277 int ret;
1279 /* Flush output if needed */
1280 if(file->_flag & MSVCRT__IOWRT)
1281 msvcrt_flush_buffer(file);
1283 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1284 whence = SEEK_SET;
1285 offset += MSVCRT__ftelli64_nolock(file);
1288 /* Discard buffered input */
1289 file->_cnt = 0;
1290 file->_ptr = file->_base;
1291 /* Reset direction of i/o */
1292 if(file->_flag & MSVCRT__IORW) {
1293 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1295 /* Clear end of file flag */
1296 file->_flag &= ~MSVCRT__IOEOF;
1297 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1299 return ret;
1302 /*********************************************************************
1303 * fseek (MSVCRT.@)
1305 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1307 return MSVCRT__fseeki64( file, offset, whence );
1310 /*********************************************************************
1311 * _fseek_nolock (MSVCRT.@)
1313 int CDECL MSVCRT__fseek_nolock(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1315 return MSVCRT__fseeki64_nolock( file, offset, whence );
1318 /*********************************************************************
1319 * _chsize_s (MSVCRT.@)
1321 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1323 __int64 cur, pos;
1324 HANDLE handle;
1325 BOOL ret = FALSE;
1327 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1329 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1331 LOCK_FILES();
1333 handle = msvcrt_fdtoh(fd);
1334 if (handle != INVALID_HANDLE_VALUE)
1336 /* save the current file pointer */
1337 cur = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1338 if (cur >= 0)
1340 pos = MSVCRT__lseeki64(fd, size, SEEK_SET);
1341 if (pos >= 0)
1343 ret = SetEndOfFile(handle);
1344 if (!ret) msvcrt_set_errno(GetLastError());
1347 /* restore the file pointer */
1348 MSVCRT__lseeki64(fd, cur, SEEK_SET);
1352 UNLOCK_FILES();
1353 return ret ? 0 : *MSVCRT__errno();
1356 /*********************************************************************
1357 * _chsize (MSVCRT.@)
1359 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1361 /* _chsize_s returns errno on failure but _chsize should return -1 */
1362 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1365 /*********************************************************************
1366 * clearerr (MSVCRT.@)
1368 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1370 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1372 MSVCRT__lock_file(file);
1373 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1374 MSVCRT__unlock_file(file);
1377 /*********************************************************************
1378 * rewind (MSVCRT.@)
1380 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1382 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1384 MSVCRT__lock_file(file);
1385 MSVCRT_fseek(file, 0L, SEEK_SET);
1386 MSVCRT_clearerr(file);
1387 MSVCRT__unlock_file(file);
1390 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1392 int plus = strchrW(mode, '+') != NULL;
1394 TRACE("%s\n", debugstr_w(mode));
1396 while(*mode == ' ') mode++;
1398 switch(*mode++)
1400 case 'R': case 'r':
1401 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1402 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1403 break;
1404 case 'W': case 'w':
1405 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1406 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1407 break;
1408 case 'A': case 'a':
1409 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1410 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1411 break;
1412 default:
1413 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1414 return -1;
1417 *stream_flags |= MSVCRT__commode;
1419 while (*mode && *mode!=',')
1420 switch (*mode++)
1422 case 'B': case 'b':
1423 *open_flags |= MSVCRT__O_BINARY;
1424 *open_flags &= ~MSVCRT__O_TEXT;
1425 break;
1426 case 't':
1427 *open_flags |= MSVCRT__O_TEXT;
1428 *open_flags &= ~MSVCRT__O_BINARY;
1429 break;
1430 case 'D':
1431 *open_flags |= MSVCRT__O_TEMPORARY;
1432 break;
1433 case 'T':
1434 *open_flags |= MSVCRT__O_SHORT_LIVED;
1435 break;
1436 case 'c':
1437 *stream_flags |= MSVCRT__IOCOMMIT;
1438 break;
1439 case 'n':
1440 *stream_flags &= ~MSVCRT__IOCOMMIT;
1441 break;
1442 case 'N':
1443 *open_flags |= MSVCRT__O_NOINHERIT;
1444 break;
1445 case '+':
1446 case ' ':
1447 case 'a':
1448 case 'w':
1449 break;
1450 case 'S':
1451 case 'R':
1452 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1453 break;
1454 default:
1455 ERR("incorrect mode flag: %c\n", mode[-1]);
1456 break;
1459 if(*mode == ',')
1461 static const WCHAR ccs[] = {'c','c','s'};
1462 static const WCHAR utf8[] = {'u','t','f','-','8'};
1463 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1464 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1466 mode++;
1467 while(*mode == ' ') mode++;
1468 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1469 return -1;
1470 mode += sizeof(ccs)/sizeof(ccs[0]);
1471 while(*mode == ' ') mode++;
1472 if(!MSVCRT_CHECK_PMT(*mode == '='))
1473 return -1;
1474 mode++;
1475 while(*mode == ' ') mode++;
1477 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1479 *open_flags |= MSVCRT__O_U8TEXT;
1480 mode += sizeof(utf8)/sizeof(utf8[0]);
1482 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1484 *open_flags |= MSVCRT__O_U16TEXT;
1485 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1487 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1489 *open_flags |= MSVCRT__O_WTEXT;
1490 mode += sizeof(unicode)/sizeof(unicode[0]);
1492 else
1494 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1495 return -1;
1498 while(*mode == ' ') mode++;
1501 if(!MSVCRT_CHECK_PMT(*mode == 0))
1502 return -1;
1503 return 0;
1506 /*********************************************************************
1507 * _fdopen (MSVCRT.@)
1509 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1511 MSVCRT_FILE *ret;
1512 MSVCRT_wchar_t *modeW = NULL;
1514 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1516 ret = MSVCRT__wfdopen(fd, modeW);
1518 MSVCRT_free(modeW);
1519 return ret;
1522 /*********************************************************************
1523 * _wfdopen (MSVCRT.@)
1525 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1527 int open_flags, stream_flags;
1528 MSVCRT_FILE* file;
1530 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1532 LOCK_FILES();
1533 if (!(file = msvcrt_alloc_fp()))
1534 file = NULL;
1535 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1537 file->_flag = 0;
1538 file = NULL;
1540 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1541 UNLOCK_FILES();
1543 return file;
1546 /*********************************************************************
1547 * _filelength (MSVCRT.@)
1549 LONG CDECL MSVCRT__filelength(int fd)
1551 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1552 if (curPos != -1)
1554 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1555 if (endPos != -1)
1557 if (endPos != curPos)
1558 MSVCRT__lseek(fd, curPos, SEEK_SET);
1559 return endPos;
1562 return -1;
1565 /*********************************************************************
1566 * _filelengthi64 (MSVCRT.@)
1568 __int64 CDECL MSVCRT__filelengthi64(int fd)
1570 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1571 if (curPos != -1)
1573 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1574 if (endPos != -1)
1576 if (endPos != curPos)
1577 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1578 return endPos;
1581 return -1;
1584 /*********************************************************************
1585 * _fileno (MSVCRT.@)
1587 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1589 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1590 return file->_file;
1593 /*********************************************************************
1594 * _fstat64 (MSVCRT.@)
1596 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1598 DWORD dw;
1599 DWORD type;
1600 BY_HANDLE_FILE_INFORMATION hfi;
1601 HANDLE hand = msvcrt_fdtoh(fd);
1603 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1604 if (hand == INVALID_HANDLE_VALUE)
1605 return -1;
1607 if (!buf)
1609 WARN(":failed-NULL buf\n");
1610 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1611 return -1;
1614 memset(&hfi, 0, sizeof(hfi));
1615 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1616 type = GetFileType(hand);
1617 if (type == FILE_TYPE_PIPE)
1619 buf->st_dev = buf->st_rdev = fd;
1620 buf->st_mode = S_IFIFO;
1621 buf->st_nlink = 1;
1623 else if (type == FILE_TYPE_CHAR)
1625 buf->st_dev = buf->st_rdev = fd;
1626 buf->st_mode = S_IFCHR;
1627 buf->st_nlink = 1;
1629 else /* FILE_TYPE_DISK etc. */
1631 if (!GetFileInformationByHandle(hand, &hfi))
1633 WARN(":failed-last error (%d)\n",GetLastError());
1634 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1635 return -1;
1637 buf->st_mode = S_IFREG | 0444;
1638 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1639 buf->st_mode |= 0222;
1640 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1641 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1642 buf->st_atime = dw;
1643 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1644 buf->st_mtime = buf->st_ctime = dw;
1645 buf->st_nlink = hfi.nNumberOfLinks;
1647 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1648 buf->st_mode);
1649 return 0;
1652 /*********************************************************************
1653 * _fstati64 (MSVCRT.@)
1655 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1657 int ret;
1658 struct MSVCRT__stat64 buf64;
1660 ret = MSVCRT__fstat64(fd, &buf64);
1661 if (!ret)
1662 msvcrt_stat64_to_stati64(&buf64, buf);
1663 return ret;
1666 /*********************************************************************
1667 * _fstat (MSVCRT.@)
1669 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1670 { int ret;
1671 struct MSVCRT__stat64 buf64;
1673 ret = MSVCRT__fstat64(fd, &buf64);
1674 if (!ret)
1675 msvcrt_stat64_to_stat(&buf64, buf);
1676 return ret;
1679 /*********************************************************************
1680 * _fstat32 (MSVCR80.@)
1682 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1684 int ret;
1685 struct MSVCRT__stat64 buf64;
1687 ret = MSVCRT__fstat64(fd, &buf64);
1688 if (!ret)
1689 msvcrt_stat64_to_stat32(&buf64, buf);
1690 return ret;
1693 /*********************************************************************
1694 * _fstat64i32 (MSVCR80.@)
1696 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1698 int ret;
1699 struct MSVCRT__stat64 buf64;
1701 ret = MSVCRT__fstat64(fd, &buf64);
1702 if (!ret)
1703 msvcrt_stat64_to_stat64i32(&buf64, buf);
1704 return ret;
1707 /*********************************************************************
1708 * _futime64 (MSVCRT.@)
1710 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1712 HANDLE hand = msvcrt_fdtoh(fd);
1713 FILETIME at, wt;
1715 if (!t)
1717 time_to_filetime( MSVCRT__time64(NULL), &at );
1718 wt = at;
1720 else
1722 time_to_filetime( t->actime, &at );
1723 time_to_filetime( t->modtime, &wt );
1726 if (!SetFileTime(hand, NULL, &at, &wt))
1728 msvcrt_set_errno(GetLastError());
1729 return -1 ;
1731 return 0;
1734 /*********************************************************************
1735 * _futime32 (MSVCRT.@)
1737 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1739 if (t)
1741 struct MSVCRT___utimbuf64 t64;
1742 t64.actime = t->actime;
1743 t64.modtime = t->modtime;
1744 return _futime64( fd, &t64 );
1746 else
1747 return _futime64( fd, NULL );
1750 /*********************************************************************
1751 * _futime (MSVCRT.@)
1753 #ifdef _WIN64
1754 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1756 return _futime64( fd, t );
1758 #else
1759 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1761 return _futime32( fd, t );
1763 #endif
1765 /*********************************************************************
1766 * _get_osfhandle (MSVCRT.@)
1768 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1770 HANDLE hand = msvcrt_fdtoh(fd);
1771 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1773 return (MSVCRT_intptr_t)hand;
1776 /*********************************************************************
1777 * _mktemp_s (MSVCRT.@)
1779 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1781 DWORD len, xno, id;
1783 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1784 return MSVCRT_EINVAL;
1786 for(len=0; len<size; len++)
1787 if(!pattern[len])
1788 break;
1789 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1790 if(size)
1791 pattern[0] = 0;
1792 return MSVCRT_EINVAL;
1795 for(xno=1; xno<=6; xno++)
1796 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1797 return MSVCRT_EINVAL;
1799 id = GetCurrentProcessId();
1800 for(xno=1; xno<6; xno++) {
1801 pattern[len-xno] = id%10 + '0';
1802 id /= 10;
1805 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1806 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1807 return 0;
1810 pattern[0] = 0;
1811 *MSVCRT__errno() = MSVCRT_EEXIST;
1812 return MSVCRT_EEXIST;
1815 /*********************************************************************
1816 * _mktemp (MSVCRT.@)
1818 char * CDECL MSVCRT__mktemp(char *pattern)
1820 int numX = 0;
1821 char *retVal = pattern;
1822 int id;
1823 char letter = 'a';
1825 if(!pattern)
1826 return NULL;
1828 while(*pattern)
1829 numX = (*pattern++ == 'X')? numX + 1 : 0;
1830 if (numX < 6)
1831 return NULL;
1832 pattern--;
1833 id = GetCurrentProcessId();
1834 numX = 6;
1835 while(numX--)
1837 int tempNum = id / 10;
1838 *pattern-- = id - (tempNum * 10) + '0';
1839 id = tempNum;
1841 pattern++;
1844 *pattern = letter++;
1845 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1846 return retVal;
1847 } while(letter <= 'z');
1848 return NULL;
1851 /*********************************************************************
1852 * _wmktemp_s (MSVCRT.@)
1854 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1856 DWORD len, xno, id;
1858 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1859 return MSVCRT_EINVAL;
1861 for(len=0; len<size; len++)
1862 if(!pattern[len])
1863 break;
1864 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1865 if(size)
1866 pattern[0] = 0;
1867 return MSVCRT_EINVAL;
1870 for(xno=1; xno<=6; xno++)
1871 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1872 return MSVCRT_EINVAL;
1874 id = GetCurrentProcessId();
1875 for(xno=1; xno<6; xno++) {
1876 pattern[len-xno] = id%10 + '0';
1877 id /= 10;
1880 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1881 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1882 return 0;
1885 pattern[0] = 0;
1886 *MSVCRT__errno() = MSVCRT_EEXIST;
1887 return MSVCRT_EEXIST;
1890 /*********************************************************************
1891 * _wmktemp (MSVCRT.@)
1893 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1895 int numX = 0;
1896 MSVCRT_wchar_t *retVal = pattern;
1897 int id;
1898 MSVCRT_wchar_t letter = 'a';
1900 if(!pattern)
1901 return NULL;
1903 while(*pattern)
1904 numX = (*pattern++ == 'X')? numX + 1 : 0;
1905 if (numX < 6)
1906 return NULL;
1907 pattern--;
1908 id = GetCurrentProcessId();
1909 numX = 6;
1910 while(numX--)
1912 int tempNum = id / 10;
1913 *pattern-- = id - (tempNum * 10) + '0';
1914 id = tempNum;
1916 pattern++;
1919 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
1920 return retVal;
1921 *pattern = letter++;
1922 } while(letter != '|');
1923 return NULL;
1926 static unsigned split_oflags(unsigned oflags)
1928 int wxflags = 0;
1929 unsigned unsupp; /* until we support everything */
1931 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1932 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1933 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1934 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
1935 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
1936 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
1937 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1938 else wxflags |= WX_TEXT; /* default to TEXT*/
1939 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1941 if ((unsupp = oflags & ~(
1942 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1943 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1944 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1945 MSVCRT__O_NOINHERIT|
1946 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
1947 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
1949 ERR(":unsupported oflags 0x%04x\n",unsupp);
1951 return wxflags;
1954 /*********************************************************************
1955 * _pipe (MSVCRT.@)
1957 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1959 int ret = -1;
1960 SECURITY_ATTRIBUTES sa;
1961 HANDLE readHandle, writeHandle;
1963 if (!pfds)
1965 *MSVCRT__errno() = MSVCRT_EINVAL;
1966 return -1;
1969 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1970 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1971 sa.lpSecurityDescriptor = NULL;
1972 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1974 unsigned int wxflags = split_oflags(textmode);
1975 int fd;
1977 LOCK_FILES();
1978 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
1979 if (fd != -1)
1981 pfds[0] = fd;
1982 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
1983 if (fd != -1)
1985 pfds[1] = fd;
1986 ret = 0;
1988 else
1990 MSVCRT__close(pfds[0]);
1991 CloseHandle(writeHandle);
1992 *MSVCRT__errno() = MSVCRT_EMFILE;
1995 else
1997 CloseHandle(readHandle);
1998 CloseHandle(writeHandle);
1999 *MSVCRT__errno() = MSVCRT_EMFILE;
2001 UNLOCK_FILES();
2003 else
2004 msvcrt_set_errno(GetLastError());
2006 return ret;
2009 static int check_bom(HANDLE h, int oflags, BOOL seek)
2011 char bom[sizeof(utf8_bom)];
2012 DWORD r;
2014 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2016 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2017 return oflags;
2019 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2020 oflags |= MSVCRT__O_U8TEXT;
2021 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2022 if (seek && r>2)
2023 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2024 oflags |= MSVCRT__O_U16TEXT;
2025 }else if (seek) {
2026 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2029 return oflags;
2032 /*********************************************************************
2033 * _wsopen_s (MSVCRT.@)
2035 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2037 DWORD access = 0, creation = 0, attrib;
2038 SECURITY_ATTRIBUTES sa;
2039 DWORD sharing, type;
2040 int wxflag;
2041 HANDLE hand;
2043 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2044 fd, debugstr_w(path), oflags, shflags, pmode);
2046 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2048 *fd = -1;
2049 wxflag = split_oflags(oflags);
2050 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2052 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2053 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2054 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2057 if (oflags & MSVCRT__O_CREAT)
2059 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
2060 FIXME(": pmode 0x%04x ignored\n", pmode);
2061 else
2062 WARN(": pmode 0x%04x ignored\n", pmode);
2064 if (oflags & MSVCRT__O_EXCL)
2065 creation = CREATE_NEW;
2066 else if (oflags & MSVCRT__O_TRUNC)
2067 creation = CREATE_ALWAYS;
2068 else
2069 creation = OPEN_ALWAYS;
2071 else /* no MSVCRT__O_CREAT */
2073 if (oflags & MSVCRT__O_TRUNC)
2074 creation = TRUNCATE_EXISTING;
2075 else
2076 creation = OPEN_EXISTING;
2079 switch( shflags )
2081 case MSVCRT__SH_DENYRW:
2082 sharing = 0L;
2083 break;
2084 case MSVCRT__SH_DENYWR:
2085 sharing = FILE_SHARE_READ;
2086 break;
2087 case MSVCRT__SH_DENYRD:
2088 sharing = FILE_SHARE_WRITE;
2089 break;
2090 case MSVCRT__SH_DENYNO:
2091 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2092 break;
2093 default:
2094 ERR( "Unhandled shflags 0x%x\n", shflags );
2095 return MSVCRT_EINVAL;
2097 attrib = FILE_ATTRIBUTE_NORMAL;
2099 if (oflags & MSVCRT__O_TEMPORARY)
2101 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2102 access |= DELETE;
2103 sharing |= FILE_SHARE_DELETE;
2106 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2107 sa.lpSecurityDescriptor = NULL;
2108 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2110 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2111 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2112 && !(access&GENERIC_READ))
2114 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2115 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2116 if (hand != INVALID_HANDLE_VALUE)
2118 oflags = check_bom(hand, oflags, FALSE);
2119 CloseHandle(hand);
2121 else
2122 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2125 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2126 if (hand == INVALID_HANDLE_VALUE) {
2127 WARN(":failed-last error (%d)\n",GetLastError());
2128 msvcrt_set_errno(GetLastError());
2129 return *MSVCRT__errno();
2132 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2134 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2135 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2136 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2138 if (oflags & MSVCRT__O_U8TEXT)
2140 DWORD written = 0, tmp;
2142 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2143 sizeof(utf8_bom)-written, &tmp, NULL))
2144 written += tmp;
2145 if (written != sizeof(utf8_bom)) {
2146 WARN("error writing BOM\n");
2147 CloseHandle(hand);
2148 msvcrt_set_errno(GetLastError());
2149 return *MSVCRT__errno();
2152 else
2154 DWORD written = 0, tmp;
2156 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2157 sizeof(utf16_bom)-written, &tmp, NULL))
2158 written += tmp;
2159 if (written != sizeof(utf16_bom))
2161 WARN("error writing BOM\n");
2162 CloseHandle(hand);
2163 msvcrt_set_errno(GetLastError());
2164 return *MSVCRT__errno();
2168 else if (access & GENERIC_READ)
2169 oflags = check_bom(hand, oflags, TRUE);
2172 type = GetFileType(hand);
2173 if (type == FILE_TYPE_CHAR)
2174 wxflag |= WX_TTY;
2175 else if (type == FILE_TYPE_PIPE)
2176 wxflag |= WX_PIPE;
2178 *fd = msvcrt_alloc_fd(hand, wxflag);
2179 if (*fd == -1)
2180 return *MSVCRT__errno();
2182 if (oflags & MSVCRT__O_WTEXT)
2183 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2184 else if (oflags & MSVCRT__O_U16TEXT)
2185 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16;
2186 else if (oflags & MSVCRT__O_U8TEXT)
2187 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF8;
2189 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2190 return 0;
2193 /*********************************************************************
2194 * _wsopen (MSVCRT.@)
2196 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2198 int pmode;
2199 int fd;
2201 if (oflags & MSVCRT__O_CREAT)
2203 __ms_va_list ap;
2205 __ms_va_start(ap, shflags);
2206 pmode = va_arg(ap, int);
2207 __ms_va_end(ap);
2209 else
2210 pmode = 0;
2212 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
2213 return fd;
2216 /*********************************************************************
2217 * _sopen_s (MSVCRT.@)
2219 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2221 MSVCRT_wchar_t *pathW;
2222 int ret;
2224 if (!MSVCRT_CHECK_PMT(fd != NULL))
2225 return MSVCRT_EINVAL;
2226 *fd = -1;
2227 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2228 return MSVCRT_EINVAL;
2230 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2231 MSVCRT_free(pathW);
2232 return ret;
2235 /*********************************************************************
2236 * _sopen (MSVCRT.@)
2238 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2240 int pmode;
2241 int fd;
2243 if (oflags & MSVCRT__O_CREAT)
2245 __ms_va_list ap;
2247 __ms_va_start(ap, shflags);
2248 pmode = va_arg(ap, int);
2249 __ms_va_end(ap);
2251 else
2252 pmode = 0;
2254 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2255 return fd;
2258 /*********************************************************************
2259 * _open (MSVCRT.@)
2261 int CDECL MSVCRT__open( const char *path, int flags, ... )
2263 __ms_va_list ap;
2265 if (flags & MSVCRT__O_CREAT)
2267 int pmode;
2268 __ms_va_start(ap, flags);
2269 pmode = va_arg(ap, int);
2270 __ms_va_end(ap);
2271 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2273 else
2274 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2277 /*********************************************************************
2278 * _wopen (MSVCRT.@)
2280 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2282 __ms_va_list ap;
2284 if (flags & MSVCRT__O_CREAT)
2286 int pmode;
2287 __ms_va_start(ap, flags);
2288 pmode = va_arg(ap, int);
2289 __ms_va_end(ap);
2290 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2292 else
2293 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2296 /*********************************************************************
2297 * _creat (MSVCRT.@)
2299 int CDECL MSVCRT__creat(const char *path, int flags)
2301 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2302 return MSVCRT__open(path, usedFlags);
2305 /*********************************************************************
2306 * _wcreat (MSVCRT.@)
2308 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2310 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2311 return MSVCRT__wopen(path, usedFlags);
2314 /*********************************************************************
2315 * _open_osfhandle (MSVCRT.@)
2317 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2319 DWORD flags;
2320 int fd;
2322 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2323 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2324 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2325 * text - it never sets MSVCRT__O_BINARY.
2327 /* don't let split_oflags() decide the mode if no mode is passed */
2328 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2329 oflags |= MSVCRT__O_BINARY;
2331 flags = GetFileType((HANDLE)handle);
2332 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2334 msvcrt_set_errno(GetLastError());
2335 return -1;
2338 if (flags == FILE_TYPE_CHAR)
2339 flags = WX_TTY;
2340 else if (flags == FILE_TYPE_PIPE)
2341 flags = WX_PIPE;
2342 else
2343 flags = 0;
2344 flags |= split_oflags(oflags);
2346 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2347 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2348 return fd;
2351 /*********************************************************************
2352 * _rmtmp (MSVCRT.@)
2354 int CDECL MSVCRT__rmtmp(void)
2356 int num_removed = 0, i;
2357 MSVCRT_FILE *file;
2359 LOCK_FILES();
2360 for (i = 3; i < MSVCRT_stream_idx; i++) {
2361 file = msvcrt_get_file(i);
2363 if (file->_tmpfname)
2365 MSVCRT_fclose(file);
2366 num_removed++;
2369 UNLOCK_FILES();
2371 if (num_removed)
2372 TRACE(":removed (%d) temp files\n",num_removed);
2373 return num_removed;
2376 static inline int get_utf8_char_len(char ch)
2378 if((ch&0xf8) == 0xf0)
2379 return 4;
2380 else if((ch&0xf0) == 0xe0)
2381 return 3;
2382 else if((ch&0xe0) == 0xc0)
2383 return 2;
2384 return 1;
2387 /*********************************************************************
2388 * (internal) read_utf8
2390 static int read_utf8(int fd, MSVCRT_wchar_t *buf, unsigned int count)
2392 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2393 HANDLE hand = fdinfo->handle;
2394 char min_buf[4], *readbuf, lookahead;
2395 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2397 /* make the buffer big enough to hold at least one character */
2398 /* read bytes have to fit to output and lookahead buffers */
2399 count /= 2;
2400 readbuf_size = count < 4 ? 4 : count;
2401 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2402 readbuf_size = 4;
2403 readbuf = min_buf;
2406 if(fdinfo->lookahead[0] != '\n') {
2407 readbuf[pos++] = fdinfo->lookahead[0];
2408 fdinfo->lookahead[0] = '\n';
2410 if(fdinfo->lookahead[1] != '\n') {
2411 readbuf[pos++] = fdinfo->lookahead[1];
2412 fdinfo->lookahead[1] = '\n';
2414 if(fdinfo->lookahead[2] != '\n') {
2415 readbuf[pos++] = fdinfo->lookahead[2];
2416 fdinfo->lookahead[2] = '\n';
2421 /* NOTE: this case is broken in native dll, reading
2422 * sometimes fails when small buffer is passed
2424 if(count < 4) {
2425 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2426 if (GetLastError() == ERROR_BROKEN_PIPE) {
2427 fdinfo->wxflag |= WX_ATEOF;
2428 return 0;
2429 }else {
2430 msvcrt_set_errno(GetLastError());
2431 return -1;
2433 }else if(!num_read) {
2434 fdinfo->wxflag |= WX_ATEOF;
2435 return 0;
2436 }else {
2437 pos++;
2440 char_len = get_utf8_char_len(readbuf[0]);
2441 if(char_len>pos) {
2442 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2443 pos += num_read;
2446 if(readbuf[0] == '\n')
2447 fdinfo->wxflag |= WX_READNL;
2448 else
2449 fdinfo->wxflag &= ~WX_READNL;
2451 if(readbuf[0] == 0x1a) {
2452 fdinfo->wxflag |= WX_ATEOF;
2453 return 0;
2456 if(readbuf[0] == '\r') {
2457 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2458 buf[0] = '\r';
2459 else if(lookahead == '\n')
2460 buf[0] = '\n';
2461 else {
2462 buf[0] = '\r';
2463 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2464 fdinfo->lookahead[0] = lookahead;
2465 else
2466 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2468 return 2;
2471 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2472 msvcrt_set_errno(GetLastError());
2473 return -1;
2476 return num_read*2;
2479 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2480 if(pos) {
2481 num_read = 0;
2482 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2483 fdinfo->wxflag |= WX_ATEOF;
2484 if (readbuf != min_buf) MSVCRT_free(readbuf);
2485 return 0;
2486 }else {
2487 msvcrt_set_errno(GetLastError());
2488 if (readbuf != min_buf) MSVCRT_free(readbuf);
2489 return -1;
2491 }else if(!pos && !num_read) {
2492 fdinfo->wxflag |= WX_ATEOF;
2493 if (readbuf != min_buf) MSVCRT_free(readbuf);
2494 return 0;
2497 pos += num_read;
2498 if(readbuf[0] == '\n')
2499 fdinfo->wxflag |= WX_READNL;
2500 else
2501 fdinfo->wxflag &= ~WX_READNL;
2503 /* Find first byte of last character (may be incomplete) */
2504 for(i=pos-1; i>0 && i>pos-4; i--)
2505 if((readbuf[i]&0xc0) != 0x80)
2506 break;
2507 char_len = get_utf8_char_len(readbuf[i]);
2508 if(char_len+i <= pos)
2509 i += char_len;
2511 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2512 if(i < pos)
2513 fdinfo->lookahead[0] = readbuf[i];
2514 if(i+1 < pos)
2515 fdinfo->lookahead[1] = readbuf[i+1];
2516 if(i+2 < pos)
2517 fdinfo->lookahead[2] = readbuf[i+2];
2518 }else if(i < pos) {
2519 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2521 pos = i;
2523 for(i=0, j=0; i<pos; i++) {
2524 if(readbuf[i] == 0x1a) {
2525 fdinfo->wxflag |= WX_ATEOF;
2526 break;
2529 /* strip '\r' if followed by '\n' */
2530 if(readbuf[i] == '\r' && i+1==pos) {
2531 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2532 readbuf[j++] = '\r';
2533 }else if(lookahead == '\n' && j==0) {
2534 readbuf[j++] = '\n';
2535 }else {
2536 if(lookahead != '\n')
2537 readbuf[j++] = '\r';
2539 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2540 fdinfo->lookahead[0] = lookahead;
2541 else
2542 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2544 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2545 readbuf[j++] = readbuf[i];
2548 pos = j;
2550 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2551 msvcrt_set_errno(GetLastError());
2552 if (readbuf != min_buf) MSVCRT_free(readbuf);
2553 return -1;
2556 if (readbuf != min_buf) MSVCRT_free(readbuf);
2557 return num_read*2;
2560 /*********************************************************************
2561 * (internal) read_i
2563 * When reading \r as last character in text mode, read() positions
2564 * the file pointer on the \r character while getc() goes on to
2565 * the following \n
2567 static int read_i(int fd, void *buf, unsigned int count)
2569 DWORD num_read, utf16;
2570 char *bufstart = buf;
2571 HANDLE hand = msvcrt_fdtoh(fd);
2572 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2574 if (count == 0)
2575 return 0;
2577 if (fdinfo->wxflag & WX_ATEOF) {
2578 TRACE("already at EOF, returning 0\n");
2579 return 0;
2581 /* Don't trace small reads, it gets *very* annoying */
2582 if (count > 4)
2583 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2584 if (hand == INVALID_HANDLE_VALUE)
2586 *MSVCRT__errno() = MSVCRT_EBADF;
2587 return -1;
2590 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2591 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2593 *MSVCRT__errno() = MSVCRT_EINVAL;
2594 return -1;
2597 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2598 return read_utf8(fd, buf, count);
2600 if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL))
2602 if (fdinfo->lookahead[0] != '\n')
2604 bufstart[0] = fdinfo->lookahead[0];
2605 fdinfo->lookahead[0] = '\n';
2607 if (utf16)
2609 bufstart[1] = fdinfo->lookahead[1];
2610 fdinfo->lookahead[1] = '\n';
2613 if(count>1+utf16 && ReadFile(hand, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2614 num_read += 1+utf16;
2615 else
2616 num_read = 1+utf16;
2619 if(utf16 && (num_read&1))
2621 /* msvcr90 uses uninitialized value from the buffer in this case */
2622 /* msvcrt ignores additional data */
2623 ERR("got odd number of bytes in UTF16 mode\n");
2624 num_read--;
2627 if (count != 0 && num_read == 0)
2629 fdinfo->wxflag |= WX_ATEOF;
2630 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2632 else if (fdinfo->wxflag & WX_TEXT)
2634 DWORD i, j;
2636 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2637 fdinfo->wxflag |= WX_READNL;
2638 else
2639 fdinfo->wxflag &= ~WX_READNL;
2641 for (i=0, j=0; i<num_read; i+=1+utf16)
2643 /* in text mode, a ctrl-z signals EOF */
2644 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2646 fdinfo->wxflag |= WX_ATEOF;
2647 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2648 break;
2651 /* in text mode, strip \r if followed by \n */
2652 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2654 char lookahead[2];
2655 DWORD len;
2657 lookahead[1] = '\n';
2658 if (ReadFile(hand, lookahead, 1+utf16, &len, NULL) && len)
2660 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2662 bufstart[j++] = '\n';
2663 if(utf16) bufstart[j++] = 0;
2665 else
2667 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2669 bufstart[j++] = '\r';
2670 if(utf16) bufstart[j++] = 0;
2673 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2675 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2677 bufstart[j++] = '\n';
2678 if (utf16) bufstart[j++] = 0;
2680 else
2682 fdinfo->lookahead[0] = lookahead[0];
2683 fdinfo->lookahead[1] = lookahead[1];
2686 else
2687 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2690 else
2692 bufstart[j++] = '\r';
2693 if(utf16) bufstart[j++] = 0;
2696 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2697 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2699 bufstart[j++] = bufstart[i];
2700 if(utf16) bufstart[j++] = bufstart[i+1];
2703 num_read = j;
2706 else
2708 if (GetLastError() == ERROR_BROKEN_PIPE)
2710 TRACE(":end-of-pipe\n");
2711 fdinfo->wxflag |= WX_ATEOF;
2712 return 0;
2714 else
2716 TRACE(":failed-last error (%d)\n",GetLastError());
2717 return -1;
2721 if (count > 4)
2722 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2723 return num_read;
2726 /*********************************************************************
2727 * _read (MSVCRT.@)
2729 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2731 int num_read;
2732 num_read = read_i(fd, buf, count);
2733 return num_read;
2736 /*********************************************************************
2737 * _setmode (MSVCRT.@)
2739 int CDECL MSVCRT__setmode(int fd,int mode)
2741 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2742 if(ret==MSVCRT__O_TEXT && (msvcrt_get_ioinfo(fd)->exflag & (EF_UTF8|EF_UTF16)))
2743 ret = MSVCRT__O_WTEXT;
2745 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2746 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2747 *MSVCRT__errno() = MSVCRT_EINVAL;
2748 return -1;
2751 if(mode == MSVCRT__O_BINARY) {
2752 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2753 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2754 return ret;
2757 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2758 if(mode == MSVCRT__O_TEXT)
2759 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2760 else if(mode == MSVCRT__O_U8TEXT)
2761 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF16) | EF_UTF8;
2762 else
2763 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF8) | EF_UTF16;
2765 return ret;
2768 /*********************************************************************
2769 * _stat64 (MSVCRT.@)
2771 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2773 DWORD dw;
2774 WIN32_FILE_ATTRIBUTE_DATA hfi;
2775 unsigned short mode = ALL_S_IREAD;
2776 int plen;
2778 TRACE(":file (%s) buf(%p)\n",path,buf);
2780 plen = strlen(path);
2781 while (plen && path[plen-1]==' ')
2782 plen--;
2784 if (plen && (plen<2 || path[plen-2]!=':') &&
2785 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2787 *MSVCRT__errno() = MSVCRT_ENOENT;
2788 return -1;
2791 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2793 TRACE("failed (%d)\n",GetLastError());
2794 *MSVCRT__errno() = MSVCRT_ENOENT;
2795 return -1;
2798 memset(buf,0,sizeof(struct MSVCRT__stat64));
2800 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2801 Bon 011120: This FIXME seems incorrect
2802 Also a letter as first char isn't enough to be classified
2803 as a drive letter
2805 if (isalpha(*path)&& (*(path+1)==':'))
2806 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2807 else
2808 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2810 /* Dir, or regular file? */
2811 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2812 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2813 else
2815 mode |= MSVCRT__S_IFREG;
2816 /* executable? */
2817 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2819 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2820 (tolower(path[plen-3]) << 16);
2821 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2822 mode |= ALL_S_IEXEC;
2826 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2827 mode |= ALL_S_IWRITE;
2829 buf->st_mode = mode;
2830 buf->st_nlink = 1;
2831 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2832 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2833 buf->st_atime = dw;
2834 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2835 buf->st_mtime = buf->st_ctime = dw;
2836 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2837 (int)(buf->st_size >> 32),(int)buf->st_size,
2838 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2839 return 0;
2842 /*********************************************************************
2843 * _stati64 (MSVCRT.@)
2845 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2847 int ret;
2848 struct MSVCRT__stat64 buf64;
2850 ret = MSVCRT_stat64(path, &buf64);
2851 if (!ret)
2852 msvcrt_stat64_to_stati64(&buf64, buf);
2853 return ret;
2856 /*********************************************************************
2857 * _stat (MSVCRT.@)
2859 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2861 int ret;
2862 struct MSVCRT__stat64 buf64;
2864 ret = MSVCRT_stat64( path, &buf64);
2865 if (!ret)
2866 msvcrt_stat64_to_stat(&buf64, buf);
2867 return ret;
2870 /*********************************************************************
2871 * _stat32 (MSVCR100.@)
2873 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
2875 int ret;
2876 struct MSVCRT__stat64 buf64;
2878 ret = MSVCRT_stat64(path, &buf64);
2879 if (!ret)
2880 msvcrt_stat64_to_stat32(&buf64, buf);
2881 return ret;
2884 /*********************************************************************
2885 * _stat32i64 (MSVCR100.@)
2887 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
2889 int ret;
2890 struct MSVCRT__stat64 buf64;
2892 ret = MSVCRT_stat64(path, &buf64);
2893 if (!ret)
2894 msvcrt_stat64_to_stat32i64(&buf64, buf);
2895 return ret;
2898 /*********************************************************************
2899 * _stat64i32 (MSVCR100.@)
2901 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
2903 int ret;
2904 struct MSVCRT__stat64 buf64;
2906 ret = MSVCRT_stat64(path, &buf64);
2907 if (!ret)
2908 msvcrt_stat64_to_stat64i32(&buf64, buf);
2909 return ret;
2912 /*********************************************************************
2913 * _wstat64 (MSVCRT.@)
2915 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2917 DWORD dw;
2918 WIN32_FILE_ATTRIBUTE_DATA hfi;
2919 unsigned short mode = ALL_S_IREAD;
2920 int plen;
2922 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2924 plen = strlenW(path);
2925 while (plen && path[plen-1]==' ')
2926 plen--;
2928 if(plen && (plen<2 || path[plen-2]!=':') &&
2929 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2931 *MSVCRT__errno() = MSVCRT_ENOENT;
2932 return -1;
2935 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2937 TRACE("failed (%d)\n",GetLastError());
2938 *MSVCRT__errno() = MSVCRT_ENOENT;
2939 return -1;
2942 memset(buf,0,sizeof(struct MSVCRT__stat64));
2944 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2945 if (MSVCRT_iswalpha(*path))
2946 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2947 else
2948 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2950 /* Dir, or regular file? */
2951 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2952 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2953 else
2955 mode |= MSVCRT__S_IFREG;
2956 /* executable? */
2957 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2959 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2960 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2961 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2962 mode |= ALL_S_IEXEC;
2966 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2967 mode |= ALL_S_IWRITE;
2969 buf->st_mode = mode;
2970 buf->st_nlink = 1;
2971 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2972 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2973 buf->st_atime = dw;
2974 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2975 buf->st_mtime = buf->st_ctime = dw;
2976 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2977 (int)(buf->st_size >> 32),(int)buf->st_size,
2978 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2979 return 0;
2982 /*********************************************************************
2983 * _wstati64 (MSVCRT.@)
2985 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2987 int ret;
2988 struct MSVCRT__stat64 buf64;
2990 ret = MSVCRT__wstat64(path, &buf64);
2991 if (!ret)
2992 msvcrt_stat64_to_stati64(&buf64, buf);
2993 return ret;
2996 /*********************************************************************
2997 * _wstat (MSVCRT.@)
2999 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
3001 int ret;
3002 struct MSVCRT__stat64 buf64;
3004 ret = MSVCRT__wstat64( path, &buf64 );
3005 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3006 return ret;
3009 /*********************************************************************
3010 * _wstat32 (MSVCR100.@)
3012 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
3014 int ret;
3015 struct MSVCRT__stat64 buf64;
3017 ret = MSVCRT__wstat64(path, &buf64);
3018 if (!ret)
3019 msvcrt_stat64_to_stat32(&buf64, buf);
3020 return ret;
3023 /*********************************************************************
3024 * _wstat32i64 (MSVCR100.@)
3026 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
3028 int ret;
3029 struct MSVCRT__stat64 buf64;
3031 ret = MSVCRT__wstat64(path, &buf64);
3032 if (!ret)
3033 msvcrt_stat64_to_stat32i64(&buf64, buf);
3034 return ret;
3037 /*********************************************************************
3038 * _wstat64i32 (MSVCR100.@)
3040 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3042 int ret;
3043 struct MSVCRT__stat64 buf64;
3045 ret = MSVCRT__wstat64(path, &buf64);
3046 if (!ret)
3047 msvcrt_stat64_to_stat64i32(&buf64, buf);
3048 return ret;
3051 /*********************************************************************
3052 * _tell (MSVCRT.@)
3054 MSVCRT_long CDECL MSVCRT__tell(int fd)
3056 return MSVCRT__lseek(fd, 0, SEEK_CUR);
3059 /*********************************************************************
3060 * _telli64 (MSVCRT.@)
3062 __int64 CDECL _telli64(int fd)
3064 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
3067 /*********************************************************************
3068 * _tempnam (MSVCRT.@)
3070 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3072 char tmpbuf[MAX_PATH];
3073 const char *tmp_dir = MSVCRT_getenv("TMP");
3075 if (tmp_dir) dir = tmp_dir;
3077 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3078 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3080 TRACE("got name (%s)\n",tmpbuf);
3081 DeleteFileA(tmpbuf);
3082 return MSVCRT__strdup(tmpbuf);
3084 TRACE("failed (%d)\n",GetLastError());
3085 return NULL;
3088 /*********************************************************************
3089 * _wtempnam (MSVCRT.@)
3091 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3093 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3094 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3095 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3097 if (tmp_dir) dir = tmp_dir;
3099 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3100 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3102 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3103 DeleteFileW(tmpbuf);
3104 return MSVCRT__wcsdup(tmpbuf);
3106 TRACE("failed (%d)\n",GetLastError());
3107 return NULL;
3110 /*********************************************************************
3111 * _umask (MSVCRT.@)
3113 int CDECL MSVCRT__umask(int umask)
3115 int old_umask = MSVCRT_umask;
3116 TRACE("(%d)\n",umask);
3117 MSVCRT_umask = umask;
3118 return old_umask;
3121 /*********************************************************************
3122 * _utime64 (MSVCRT.@)
3124 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3126 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3128 if (fd > 0)
3130 int retVal = _futime64(fd, t);
3131 MSVCRT__close(fd);
3132 return retVal;
3134 return -1;
3137 /*********************************************************************
3138 * _utime32 (MSVCRT.@)
3140 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3142 if (t)
3144 struct MSVCRT___utimbuf64 t64;
3145 t64.actime = t->actime;
3146 t64.modtime = t->modtime;
3147 return _utime64( path, &t64 );
3149 else
3150 return _utime64( path, NULL );
3153 /*********************************************************************
3154 * _utime (MSVCRT.@)
3156 #ifdef _WIN64
3157 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3159 return _utime64( path, t );
3161 #else
3162 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3164 return _utime32( path, t );
3166 #endif
3168 /*********************************************************************
3169 * _wutime64 (MSVCRT.@)
3171 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3173 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3175 if (fd > 0)
3177 int retVal = _futime64(fd, t);
3178 MSVCRT__close(fd);
3179 return retVal;
3181 return -1;
3184 /*********************************************************************
3185 * _wutime32 (MSVCRT.@)
3187 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3189 if (t)
3191 struct MSVCRT___utimbuf64 t64;
3192 t64.actime = t->actime;
3193 t64.modtime = t->modtime;
3194 return _wutime64( path, &t64 );
3196 else
3197 return _wutime64( path, NULL );
3200 /*********************************************************************
3201 * _wutime (MSVCRT.@)
3203 #ifdef _WIN64
3204 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3206 return _wutime64( path, t );
3208 #else
3209 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3211 return _wutime32( path, t );
3213 #endif
3215 /*********************************************************************
3216 * _write (MSVCRT.@)
3218 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3220 DWORD num_written;
3221 ioinfo *info = msvcrt_get_ioinfo(fd);
3222 HANDLE hand = info->handle;
3224 /* Don't trace small writes, it gets *very* annoying */
3225 #if 0
3226 if (count > 32)
3227 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3228 #endif
3229 if (hand == INVALID_HANDLE_VALUE)
3231 *MSVCRT__errno() = MSVCRT_EBADF;
3232 return -1;
3235 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3237 *MSVCRT__errno() = MSVCRT_EINVAL;
3238 return -1;
3241 /* If appending, go to EOF */
3242 if (info->wxflag & WX_APPEND)
3243 MSVCRT__lseek(fd, 0, FILE_END);
3245 if (!(info->wxflag & WX_TEXT))
3247 if (WriteFile(hand, buf, count, &num_written, NULL)
3248 && (num_written == count))
3249 return num_written;
3250 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3251 hand, GetLastError());
3252 *MSVCRT__errno() = MSVCRT_ENOSPC;
3254 else
3256 unsigned int i, j, nr_lf, size;
3257 char *p = NULL;
3258 const char *q;
3259 const char *s = buf, *buf_start = buf;
3261 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3263 /* find number of \n */
3264 for (nr_lf=0, i=0; i<count; i++)
3265 if (s[i] == '\n')
3266 nr_lf++;
3267 if (nr_lf)
3269 size = count+nr_lf;
3270 if ((q = p = MSVCRT_malloc(size)))
3272 for (s = buf, i = 0, j = 0; i < count; i++)
3274 if (s[i] == '\n')
3275 p[j++] = '\r';
3276 p[j++] = s[i];
3279 else
3281 FIXME("Malloc failed\n");
3282 nr_lf = 0;
3283 size = count;
3284 q = buf;
3287 else
3289 size = count;
3290 q = buf;
3293 else if (info->exflag & EF_UTF16)
3295 for (nr_lf=0, i=0; i<count; i+=2)
3296 if (s[i]=='\n' && s[i+1]==0)
3297 nr_lf += 2;
3298 if (nr_lf)
3300 size = count+nr_lf;
3301 if ((q = p = MSVCRT_malloc(size)))
3303 for (s=buf, i=0, j=0; i<count; i++)
3305 if (s[i]=='\n' && s[i+1]==0)
3307 p[j++] = '\r';
3308 p[j++] = 0;
3310 p[j++] = s[i++];
3311 p[j++] = s[i];
3314 else
3316 FIXME("Malloc failed\n");
3317 nr_lf = 0;
3318 size = count;
3319 q = buf;
3322 else
3324 size = count;
3325 q = buf;
3328 else
3330 DWORD conv_len;
3332 for(nr_lf=0, i=0; i<count; i+=2)
3333 if (s[i]=='\n' && s[i+1]==0)
3334 nr_lf++;
3336 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3337 if(!conv_len) {
3338 msvcrt_set_errno(GetLastError());
3339 MSVCRT_free(p);
3340 return -1;
3343 size = conv_len+nr_lf;
3344 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3346 for (s=buf, i=0, j=0; i<count; i++)
3348 if (s[i]=='\n' && s[i+1]==0)
3350 p[j++] = '\r';
3351 p[j++] = 0;
3353 p[j++] = s[i++];
3354 p[j++] = s[i];
3356 q = p+count+nr_lf*2;
3357 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3358 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3360 else
3362 FIXME("Malloc failed\n");
3363 nr_lf = 0;
3364 size = count;
3365 q = buf;
3369 if (!WriteFile(hand, q, size, &num_written, NULL))
3370 num_written = -1;
3371 if(p)
3372 MSVCRT_free(p);
3373 if (num_written != size)
3375 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3376 fd, hand, GetLastError(), num_written);
3377 *MSVCRT__errno() = MSVCRT_ENOSPC;
3378 return s - buf_start;
3380 return count;
3383 return -1;
3386 /*********************************************************************
3387 * _putw (MSVCRT.@)
3389 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3391 int len;
3393 MSVCRT__lock_file(file);
3394 len = MSVCRT__write(file->_file, &val, sizeof(val));
3395 if (len == sizeof(val)) {
3396 MSVCRT__unlock_file(file);
3397 return val;
3400 file->_flag |= MSVCRT__IOERR;
3401 MSVCRT__unlock_file(file);
3402 return MSVCRT_EOF;
3405 /*********************************************************************
3406 * fclose (MSVCRT.@)
3408 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3410 int ret;
3412 MSVCRT__lock_file(file);
3413 ret = MSVCRT__fclose_nolock(file);
3414 MSVCRT__unlock_file(file);
3416 return ret;
3419 /*********************************************************************
3420 * _fclose_nolock (MSVCRT.@)
3422 int CDECL MSVCRT__fclose_nolock(MSVCRT_FILE* file)
3424 int r, flag;
3426 flag = file->_flag;
3427 MSVCRT_free(file->_tmpfname);
3428 file->_tmpfname = NULL;
3429 /* flush stdio buffers */
3430 if(file->_flag & MSVCRT__IOWRT)
3431 MSVCRT_fflush(file);
3432 if(file->_flag & MSVCRT__IOMYBUF)
3433 MSVCRT_free(file->_base);
3435 r=MSVCRT__close(file->_file);
3436 file->_flag = 0;
3438 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3441 /*********************************************************************
3442 * feof (MSVCRT.@)
3444 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3446 return file->_flag & MSVCRT__IOEOF;
3449 /*********************************************************************
3450 * ferror (MSVCRT.@)
3452 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3454 return file->_flag & MSVCRT__IOERR;
3457 /*********************************************************************
3458 * _filbuf (MSVCRT.@)
3460 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3462 unsigned char c;
3463 MSVCRT__lock_file(file);
3465 if(file->_flag & MSVCRT__IOSTRG) {
3466 MSVCRT__unlock_file(file);
3467 return MSVCRT_EOF;
3470 /* Allocate buffer if needed */
3471 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3472 msvcrt_alloc_buffer(file);
3474 if(!(file->_flag & MSVCRT__IOREAD)) {
3475 if(file->_flag & MSVCRT__IORW)
3476 file->_flag |= MSVCRT__IOREAD;
3477 else {
3478 MSVCRT__unlock_file(file);
3479 return MSVCRT_EOF;
3483 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3484 int r;
3485 if ((r = read_i(file->_file,&c,1)) != 1) {
3486 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3487 MSVCRT__unlock_file(file);
3488 return MSVCRT_EOF;
3491 MSVCRT__unlock_file(file);
3492 return c;
3493 } else {
3494 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
3495 if(file->_cnt<=0) {
3496 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3497 file->_cnt = 0;
3498 MSVCRT__unlock_file(file);
3499 return MSVCRT_EOF;
3502 file->_cnt--;
3503 file->_ptr = file->_base+1;
3504 c = *(unsigned char *)file->_base;
3505 MSVCRT__unlock_file(file);
3506 return c;
3510 /*********************************************************************
3511 * fgetc (MSVCRT.@)
3513 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3515 unsigned char *i;
3516 unsigned int j;
3518 MSVCRT__lock_file(file);
3519 if (file->_cnt>0) {
3520 file->_cnt--;
3521 i = (unsigned char *)file->_ptr++;
3522 j = *i;
3523 } else
3524 j = MSVCRT__filbuf(file);
3526 MSVCRT__unlock_file(file);
3527 return j;
3530 /*********************************************************************
3531 * _fgetchar (MSVCRT.@)
3533 int CDECL MSVCRT__fgetchar(void)
3535 return MSVCRT_fgetc(MSVCRT_stdin);
3538 /*********************************************************************
3539 * fgets (MSVCRT.@)
3541 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3543 int cc = MSVCRT_EOF;
3544 char * buf_start = s;
3546 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3547 file,file->_file,s,size);
3549 MSVCRT__lock_file(file);
3551 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
3553 *s++ = (char)cc;
3554 size --;
3556 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3558 TRACE(":nothing read\n");
3559 MSVCRT__unlock_file(file);
3560 return NULL;
3562 if ((cc != MSVCRT_EOF) && (size > 1))
3563 *s++ = cc;
3564 *s = '\0';
3565 TRACE(":got %s\n", debugstr_a(buf_start));
3566 MSVCRT__unlock_file(file);
3567 return buf_start;
3570 /*********************************************************************
3571 * fgetwc (MSVCRT.@)
3573 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3575 MSVCRT_wint_t ret;
3576 int ch;
3578 MSVCRT__lock_file(file);
3580 if((msvcrt_get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3581 || !(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3582 char *p;
3584 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3585 ch = MSVCRT_fgetc(file);
3586 if(ch == MSVCRT_EOF) {
3587 ret = MSVCRT_WEOF;
3588 break;
3590 *p = (char)ch;
3592 }else {
3593 char mbs[MSVCRT_MB_LEN_MAX];
3594 int len = 0;
3596 ch = MSVCRT_fgetc(file);
3597 if(ch != MSVCRT_EOF) {
3598 mbs[0] = (char)ch;
3599 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3600 ch = MSVCRT_fgetc(file);
3601 if(ch != MSVCRT_EOF) {
3602 mbs[1] = (char)ch;
3603 len = 2;
3605 }else {
3606 len = 1;
3610 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3611 ret = MSVCRT_WEOF;
3614 MSVCRT__unlock_file(file);
3615 return ret;
3618 /*********************************************************************
3619 * _getw (MSVCRT.@)
3621 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3623 char *ch;
3624 int i, k;
3625 unsigned int j;
3626 ch = (char *)&i;
3628 MSVCRT__lock_file(file);
3629 for (j=0; j<sizeof(int); j++) {
3630 k = MSVCRT_fgetc(file);
3631 if (k == MSVCRT_EOF) {
3632 file->_flag |= MSVCRT__IOEOF;
3633 MSVCRT__unlock_file(file);
3634 return EOF;
3636 ch[j] = k;
3639 MSVCRT__unlock_file(file);
3640 return i;
3643 /*********************************************************************
3644 * getwc (MSVCRT.@)
3646 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3648 return MSVCRT_fgetwc(file);
3651 /*********************************************************************
3652 * _fgetwchar (MSVCRT.@)
3654 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3656 return MSVCRT_fgetwc(MSVCRT_stdin);
3659 /*********************************************************************
3660 * getwchar (MSVCRT.@)
3662 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3664 return MSVCRT__fgetwchar();
3667 /*********************************************************************
3668 * fgetws (MSVCRT.@)
3670 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3672 MSVCRT_wint_t cc = MSVCRT_WEOF;
3673 MSVCRT_wchar_t * buf_start = s;
3675 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3676 file,file->_file,s,size);
3678 MSVCRT__lock_file(file);
3680 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
3682 *s++ = cc;
3683 size --;
3685 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3687 TRACE(":nothing read\n");
3688 MSVCRT__unlock_file(file);
3689 return NULL;
3691 if ((cc != MSVCRT_WEOF) && (size > 1))
3692 *s++ = cc;
3693 *s = 0;
3694 TRACE(":got %s\n", debugstr_w(buf_start));
3695 MSVCRT__unlock_file(file);
3696 return buf_start;
3699 /*********************************************************************
3700 * _flsbuf (MSVCRT.@)
3702 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3704 /* Flush output buffer */
3705 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3706 msvcrt_alloc_buffer(file);
3708 if(!(file->_flag & MSVCRT__IOWRT)) {
3709 if(file->_flag & MSVCRT__IORW)
3710 file->_flag |= MSVCRT__IOWRT;
3711 else
3712 return MSVCRT_EOF;
3714 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3715 int res = 0;
3717 if(file->_cnt <= 0) {
3718 res = msvcrt_flush_buffer(file);
3719 if(res)
3720 return res;
3721 file->_cnt=file->_bufsiz;
3723 *file->_ptr++ = c;
3724 file->_cnt--;
3725 return c&0xff;
3726 } else {
3727 unsigned char cc=c;
3728 int len;
3729 /* set _cnt to 0 for unbuffered FILEs */
3730 file->_cnt = 0;
3731 len = MSVCRT__write(file->_file, &cc, 1);
3732 if (len == 1)
3733 return c & 0xff;
3734 file->_flag |= MSVCRT__IOERR;
3735 return MSVCRT_EOF;
3739 /*********************************************************************
3740 * fwrite (MSVCRT.@)
3742 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3744 int ret;
3746 MSVCRT__lock_file(file);
3747 ret = MSVCRT__fwrite_nolock(ptr, size, nmemb, file);
3748 MSVCRT__unlock_file(file);
3750 return ret;
3753 /*********************************************************************
3754 * _fwrite_nolock (MSVCRT.@)
3756 MSVCRT_size_t CDECL MSVCRT__fwrite_nolock(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3758 MSVCRT_size_t wrcnt=size * nmemb;
3759 int written = 0;
3760 if (size == 0)
3761 return 0;
3763 while(wrcnt) {
3764 if(file->_cnt < 0) {
3765 WARN("negative file->_cnt value in %p\n", file);
3766 file->_flag |= MSVCRT__IOERR;
3767 break;
3768 } else if(file->_cnt) {
3769 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3770 memcpy(file->_ptr, ptr, pcnt);
3771 file->_cnt -= pcnt;
3772 file->_ptr += pcnt;
3773 written += pcnt;
3774 wrcnt -= pcnt;
3775 ptr = (const char*)ptr + pcnt;
3776 } else if((file->_flag & MSVCRT__IONBF)
3777 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
3778 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
3779 MSVCRT_size_t pcnt;
3780 int bufsiz;
3782 if(file->_flag & MSVCRT__IONBF)
3783 bufsiz = 1;
3784 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3785 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
3786 else
3787 bufsiz = file->_bufsiz;
3789 pcnt = (wrcnt / bufsiz) * bufsiz;
3791 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
3792 break;
3794 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
3795 file->_flag |= MSVCRT__IOERR;
3796 break;
3798 written += pcnt;
3799 wrcnt -= pcnt;
3800 ptr = (const char*)ptr + pcnt;
3801 } else {
3802 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3803 break;
3804 written++;
3805 wrcnt--;
3806 ptr = (const char*)ptr + 1;
3810 return written / size;
3813 /*********************************************************************
3814 * fputwc (MSVCRT.@)
3816 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3818 MSVCRT_wchar_t mwc=wc;
3819 ioinfo *fdinfo;
3820 MSVCRT_wint_t ret;
3822 MSVCRT__lock_file(file);
3823 fdinfo = msvcrt_get_ioinfo(file->_file);
3825 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3826 char buf[MSVCRT_MB_LEN_MAX];
3827 int char_len;
3829 char_len = MSVCRT_wctomb(buf, mwc);
3830 if(char_len!=-1 && MSVCRT_fwrite(buf, char_len, 1, file)==1)
3831 ret = wc;
3832 else
3833 ret = MSVCRT_WEOF;
3834 }else if(MSVCRT_fwrite(&mwc, sizeof(mwc), 1, file) == 1) {
3835 ret = wc;
3836 }else {
3837 ret = MSVCRT_WEOF;
3840 MSVCRT__unlock_file(file);
3841 return ret;
3844 /*********************************************************************
3845 * _fputwchar (MSVCRT.@)
3847 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3849 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3852 /*********************************************************************
3853 * _wfsopen (MSVCRT.@)
3855 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
3857 MSVCRT_FILE* file;
3858 int open_flags, stream_flags, fd;
3860 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3862 /* map mode string to open() flags. "man fopen" for possibilities. */
3863 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3864 return NULL;
3866 LOCK_FILES();
3867 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3868 if (fd < 0)
3869 file = NULL;
3870 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3871 != -1)
3872 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3873 else if (file)
3875 file->_flag = 0;
3876 file = NULL;
3879 TRACE(":got (%p)\n",file);
3880 if (fd >= 0 && !file)
3881 MSVCRT__close(fd);
3882 UNLOCK_FILES();
3883 return file;
3886 /*********************************************************************
3887 * _fsopen (MSVCRT.@)
3889 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3891 MSVCRT_FILE *ret;
3892 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3894 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3895 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3896 *MSVCRT__errno() = MSVCRT_EINVAL;
3897 return NULL;
3899 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3901 MSVCRT_free(pathW);
3902 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3903 *MSVCRT__errno() = MSVCRT_EINVAL;
3904 return NULL;
3907 ret = MSVCRT__wfsopen(pathW, modeW, share);
3909 MSVCRT_free(pathW);
3910 MSVCRT_free(modeW);
3911 return ret;
3914 /*********************************************************************
3915 * fopen (MSVCRT.@)
3917 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3919 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3922 /*********************************************************************
3923 * fopen_s (MSVCRT.@)
3925 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3926 const char *filename, const char *mode)
3928 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3929 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3930 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3932 *pFile = MSVCRT_fopen(filename, mode);
3934 if(!*pFile)
3935 return *MSVCRT__errno();
3936 return 0;
3939 /*********************************************************************
3940 * _wfopen (MSVCRT.@)
3942 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3944 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3947 /*********************************************************************
3948 * _wfopen_s (MSVCRT.@)
3950 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3951 const MSVCRT_wchar_t *mode)
3953 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3954 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3955 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3957 *pFile = MSVCRT__wfopen(filename, mode);
3959 if(!*pFile)
3960 return *MSVCRT__errno();
3961 return 0;
3964 /*********************************************************************
3965 * fputc (MSVCRT.@)
3967 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3969 int res;
3971 MSVCRT__lock_file(file);
3972 if(file->_cnt>0) {
3973 *file->_ptr++=c;
3974 file->_cnt--;
3975 if (c == '\n')
3977 res = msvcrt_flush_buffer(file);
3978 MSVCRT__unlock_file(file);
3979 return res ? res : c;
3981 else {
3982 MSVCRT__unlock_file(file);
3983 return c & 0xff;
3985 } else {
3986 res = MSVCRT__flsbuf(c, file);
3987 MSVCRT__unlock_file(file);
3988 return res;
3992 /*********************************************************************
3993 * _fputchar (MSVCRT.@)
3995 int CDECL MSVCRT__fputchar(int c)
3997 return MSVCRT_fputc(c, MSVCRT_stdout);
4000 /*********************************************************************
4001 * fread (MSVCRT.@)
4003 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4005 int ret;
4007 MSVCRT__lock_file(file);
4008 ret = MSVCRT__fread_nolock(ptr, size, nmemb, file);
4009 MSVCRT__unlock_file(file);
4011 return ret;
4014 /*********************************************************************
4015 * _fread_nolock (MSVCRT.@)
4017 MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4019 MSVCRT_size_t rcnt=size * nmemb;
4020 MSVCRT_size_t read=0;
4021 MSVCRT_size_t pread=0;
4023 if(!rcnt)
4024 return 0;
4026 /* first buffered data */
4027 if(file->_cnt>0) {
4028 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4029 memcpy(ptr, file->_ptr, pcnt);
4030 file->_cnt -= pcnt;
4031 file->_ptr += pcnt;
4032 read += pcnt ;
4033 rcnt -= pcnt ;
4034 ptr = (char*)ptr + pcnt;
4035 } else if(!(file->_flag & MSVCRT__IOREAD )) {
4036 if(file->_flag & MSVCRT__IORW) {
4037 file->_flag |= MSVCRT__IOREAD;
4038 } else {
4039 return 0;
4043 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4044 msvcrt_alloc_buffer(file);
4046 while(rcnt>0)
4048 int i;
4049 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
4050 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
4051 file->_ptr = file->_base;
4052 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
4053 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4054 if (i > 0 && i < file->_cnt) {
4055 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
4056 file->_flag &= ~MSVCRT__IOEOF;
4058 if (i > 0) {
4059 memcpy(ptr, file->_ptr, i);
4060 file->_cnt -= i;
4061 file->_ptr += i;
4063 } else if (rcnt > INT_MAX) {
4064 i = MSVCRT__read(file->_file, ptr, INT_MAX);
4065 } else if (rcnt < MSVCRT_BUFSIZ) {
4066 i = MSVCRT__read(file->_file, ptr, rcnt);
4067 } else {
4068 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
4070 pread += i;
4071 rcnt -= i;
4072 ptr = (char *)ptr+i;
4073 /* expose feof condition in the flags
4074 * MFC tests file->_flag for feof, and doesn't call feof())
4076 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
4077 file->_flag |= MSVCRT__IOEOF;
4078 else if (i == -1)
4080 file->_flag |= MSVCRT__IOERR;
4081 pread = 0;
4082 rcnt = 0;
4084 if (i < 1) break;
4086 read+=pread;
4087 return read / size;
4091 /*********************************************************************
4092 * fread_s (MSVCR80.@)
4094 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4095 MSVCRT_size_t count, MSVCRT_FILE *stream)
4097 size_t bytes_left, buf_pos;
4099 TRACE("(%p %lu %lu %lu %p\n", buf, buf_size, elem_size, count, stream);
4101 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4102 if(buf && buf_size)
4103 memset(buf, 0, buf_size);
4104 return 0;
4106 if(!elem_size || !count) return 0;
4107 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4108 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4110 bytes_left = elem_size*count;
4111 buf_pos = 0;
4112 while(bytes_left) {
4113 if(stream->_cnt > 0) {
4114 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4116 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4117 memset(buf, 0, buf_size);
4118 return 0;
4121 MSVCRT_fread((char*)buf+buf_pos, 1, size, stream);
4122 buf_pos += size;
4123 bytes_left -= size;
4124 }else {
4125 int c = MSVCRT__filbuf(stream);
4127 if(c == EOF)
4128 break;
4130 if(!MSVCRT_CHECK_PMT_ERR(buf_size-buf_pos > 0, MSVCRT_ERANGE)) {
4131 memset(buf, 0, buf_size);
4132 return 0;
4135 ((char*)buf)[buf_pos++] = c;
4136 bytes_left--;
4140 return buf_pos/elem_size;
4143 /*********************************************************************
4144 * _wfreopen (MSVCRT.@)
4147 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4149 int open_flags, stream_flags, fd;
4151 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4153 LOCK_FILES();
4154 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
4155 file = NULL;
4156 else
4158 MSVCRT_fclose(file);
4159 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4160 file = NULL;
4161 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4162 file = NULL;
4163 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4165 file->_flag = 0;
4166 file = NULL;
4169 UNLOCK_FILES();
4170 return file;
4173 /*********************************************************************
4174 * _wfreopen_s (MSVCRT.@)
4176 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4177 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4179 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4180 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4181 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4182 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4184 *pFile = MSVCRT__wfreopen(path, mode, file);
4186 if(!*pFile)
4187 return *MSVCRT__errno();
4188 return 0;
4191 /*********************************************************************
4192 * freopen (MSVCRT.@)
4195 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4197 MSVCRT_FILE *ret;
4198 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4200 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4201 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4203 MSVCRT_free(pathW);
4204 return NULL;
4207 ret = MSVCRT__wfreopen(pathW, modeW, file);
4209 MSVCRT_free(pathW);
4210 MSVCRT_free(modeW);
4211 return ret;
4214 /*********************************************************************
4215 * freopen_s (MSVCRT.@)
4217 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4218 const char *path, const char *mode, MSVCRT_FILE* file)
4220 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4221 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4222 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4223 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4225 *pFile = MSVCRT_freopen(path, mode, file);
4227 if(!*pFile)
4228 return *MSVCRT__errno();
4229 return 0;
4232 /*********************************************************************
4233 * fsetpos (MSVCRT.@)
4235 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4237 int ret;
4239 MSVCRT__lock_file(file);
4240 /* Note that all this has been lifted 'as is' from fseek */
4241 if(file->_flag & MSVCRT__IOWRT)
4242 msvcrt_flush_buffer(file);
4244 /* Discard buffered input */
4245 file->_cnt = 0;
4246 file->_ptr = file->_base;
4248 /* Reset direction of i/o */
4249 if(file->_flag & MSVCRT__IORW) {
4250 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4253 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
4254 MSVCRT__unlock_file(file);
4255 return ret;
4258 /*********************************************************************
4259 * _ftelli64 (MSVCRT.@)
4261 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4263 __int64 ret;
4265 MSVCRT__lock_file(file);
4266 ret = MSVCRT__ftelli64_nolock(file);
4267 MSVCRT__unlock_file(file);
4269 return ret;
4272 /*********************************************************************
4273 * _ftelli64_nolock (MSVCRT.@)
4275 __int64 CDECL MSVCRT__ftelli64_nolock(MSVCRT_FILE* file)
4277 __int64 pos;
4279 pos = _telli64(file->_file);
4280 if(pos == -1)
4281 return -1;
4282 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4283 if(file->_flag & MSVCRT__IOWRT) {
4284 pos += file->_ptr - file->_base;
4286 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4287 char *p;
4289 for(p=file->_base; p<file->_ptr; p++)
4290 if(*p == '\n')
4291 pos++;
4293 } else if(!file->_cnt) { /* nothing to do */
4294 } else if(MSVCRT__lseeki64(file->_file, 0, SEEK_END)==pos) {
4295 int i;
4297 pos -= file->_cnt;
4298 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4299 for(i=0; i<file->_cnt; i++)
4300 if(file->_ptr[i] == '\n')
4301 pos--;
4303 } else {
4304 char *p;
4306 if(MSVCRT__lseeki64(file->_file, pos, SEEK_SET) != pos)
4307 return -1;
4309 pos -= file->_bufsiz;
4310 pos += file->_ptr - file->_base;
4312 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4313 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READNL)
4314 pos--;
4316 for(p=file->_base; p<file->_ptr; p++)
4317 if(*p == '\n')
4318 pos++;
4323 return pos;
4326 /*********************************************************************
4327 * ftell (MSVCRT.@)
4329 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4331 return MSVCRT__ftelli64(file);
4334 /*********************************************************************
4335 * _ftell_nolock (MSVCRT.@)
4337 LONG CDECL MSVCRT__ftell_nolock(MSVCRT_FILE* file)
4339 return MSVCRT__ftelli64_nolock(file);
4342 /*********************************************************************
4343 * fgetpos (MSVCRT.@)
4345 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4347 *pos = MSVCRT__ftelli64(file);
4348 if(*pos == -1)
4349 return -1;
4350 return 0;
4353 /*********************************************************************
4354 * fputs (MSVCRT.@)
4356 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4358 MSVCRT_size_t len = strlen(s);
4359 int ret;
4361 MSVCRT__lock_file(file);
4362 ret = MSVCRT_fwrite(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4363 MSVCRT__unlock_file(file);
4364 return ret;
4367 /*********************************************************************
4368 * fputws (MSVCRT.@)
4370 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4372 MSVCRT_size_t i, len = strlenW(s);
4373 BOOL tmp_buf;
4374 int ret;
4376 MSVCRT__lock_file(file);
4377 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
4378 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4379 MSVCRT__unlock_file(file);
4380 return ret;
4383 tmp_buf = add_std_buffer(file);
4384 for (i=0; i<len; i++) {
4385 if(MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
4386 if(tmp_buf) remove_std_buffer(file);
4387 MSVCRT__unlock_file(file);
4388 return MSVCRT_WEOF;
4392 if(tmp_buf) remove_std_buffer(file);
4393 MSVCRT__unlock_file(file);
4394 return 0;
4397 /*********************************************************************
4398 * getchar (MSVCRT.@)
4400 int CDECL MSVCRT_getchar(void)
4402 return MSVCRT_fgetc(MSVCRT_stdin);
4405 /*********************************************************************
4406 * getc (MSVCRT.@)
4408 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4410 return MSVCRT_fgetc(file);
4413 /*********************************************************************
4414 * gets (MSVCRT.@)
4416 char * CDECL MSVCRT_gets(char *buf)
4418 int cc;
4419 char * buf_start = buf;
4421 MSVCRT__lock_file(MSVCRT_stdin);
4422 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4423 cc = MSVCRT_fgetc(MSVCRT_stdin))
4424 if(cc != '\r') *buf++ = (char)cc;
4426 *buf = '\0';
4428 TRACE("got '%s'\n", buf_start);
4429 MSVCRT__unlock_file(MSVCRT_stdin);
4430 return buf_start;
4433 /*********************************************************************
4434 * _getws (MSVCRT.@)
4436 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4438 MSVCRT_wint_t cc;
4439 MSVCRT_wchar_t* ws = buf;
4441 MSVCRT__lock_file(MSVCRT_stdin);
4442 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4443 cc = MSVCRT_fgetwc(MSVCRT_stdin))
4445 if (cc != '\r')
4446 *buf++ = (MSVCRT_wchar_t)cc;
4448 *buf = '\0';
4450 TRACE("got %s\n", debugstr_w(ws));
4451 MSVCRT__unlock_file(MSVCRT_stdin);
4452 return ws;
4455 /*********************************************************************
4456 * putc (MSVCRT.@)
4458 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4460 return MSVCRT_fputc(c, file);
4463 /*********************************************************************
4464 * putchar (MSVCRT.@)
4466 int CDECL MSVCRT_putchar(int c)
4468 return MSVCRT_fputc(c, MSVCRT_stdout);
4471 /*********************************************************************
4472 * _putwch (MSVCRT.@)
4474 int CDECL MSVCRT__putwch(int c)
4476 return MSVCRT_fputwc(c, MSVCRT_stdout);
4479 /*********************************************************************
4480 * puts (MSVCRT.@)
4482 int CDECL MSVCRT_puts(const char *s)
4484 MSVCRT_size_t len = strlen(s);
4485 int ret;
4487 MSVCRT__lock_file(MSVCRT_stdout);
4488 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4489 MSVCRT__unlock_file(MSVCRT_stdout);
4490 return MSVCRT_EOF;
4493 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4494 MSVCRT__unlock_file(MSVCRT_stdout);
4495 return ret;
4498 /*********************************************************************
4499 * _putws (MSVCRT.@)
4501 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4503 static const MSVCRT_wchar_t nl = '\n';
4504 MSVCRT_size_t len = strlenW(s);
4505 int ret;
4507 MSVCRT__lock_file(MSVCRT_stdout);
4508 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4509 MSVCRT__unlock_file(MSVCRT_stdout);
4510 return MSVCRT_EOF;
4513 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4514 MSVCRT__unlock_file(MSVCRT_stdout);
4515 return ret;
4518 /*********************************************************************
4519 * remove (MSVCRT.@)
4521 int CDECL MSVCRT_remove(const char *path)
4523 TRACE("(%s)\n",path);
4524 if (DeleteFileA(path))
4525 return 0;
4526 TRACE(":failed (%d)\n",GetLastError());
4527 msvcrt_set_errno(GetLastError());
4528 return -1;
4531 /*********************************************************************
4532 * _wremove (MSVCRT.@)
4534 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4536 TRACE("(%s)\n",debugstr_w(path));
4537 if (DeleteFileW(path))
4538 return 0;
4539 TRACE(":failed (%d)\n",GetLastError());
4540 msvcrt_set_errno(GetLastError());
4541 return -1;
4544 /*********************************************************************
4545 * rename (MSVCRT.@)
4547 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4549 TRACE(":from %s to %s\n",oldpath,newpath);
4550 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4551 return 0;
4552 TRACE(":failed (%d)\n",GetLastError());
4553 msvcrt_set_errno(GetLastError());
4554 return -1;
4557 /*********************************************************************
4558 * _wrename (MSVCRT.@)
4560 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4562 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4563 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4564 return 0;
4565 TRACE(":failed (%d)\n",GetLastError());
4566 msvcrt_set_errno(GetLastError());
4567 return -1;
4570 /*********************************************************************
4571 * setvbuf (MSVCRT.@)
4573 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4575 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4576 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4577 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4579 MSVCRT__lock_file(file);
4581 MSVCRT_fflush(file);
4582 if(file->_flag & MSVCRT__IOMYBUF)
4583 MSVCRT_free(file->_base);
4584 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4585 file->_cnt = 0;
4587 if(mode == MSVCRT__IONBF) {
4588 file->_flag |= MSVCRT__IONBF;
4589 file->_base = file->_ptr = (char*)&file->_charbuf;
4590 file->_bufsiz = 2;
4591 }else if(buf) {
4592 file->_base = file->_ptr = buf;
4593 file->_flag |= MSVCRT__USERBUF;
4594 file->_bufsiz = size;
4595 }else {
4596 file->_base = file->_ptr = MSVCRT_malloc(size);
4597 if(!file->_base) {
4598 file->_bufsiz = 0;
4599 MSVCRT__unlock_file(file);
4600 return -1;
4603 file->_flag |= MSVCRT__IOMYBUF;
4604 file->_bufsiz = size;
4606 MSVCRT__unlock_file(file);
4607 return 0;
4610 /*********************************************************************
4611 * setbuf (MSVCRT.@)
4613 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4615 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4618 /*********************************************************************
4619 * tmpnam (MSVCRT.@)
4621 char * CDECL MSVCRT_tmpnam(char *s)
4623 char tmpstr[16];
4624 char *p;
4625 int count, size;
4627 if (!s) {
4628 thread_data_t *data = msvcrt_get_thread_data();
4630 if(!data->tmpnam_buffer)
4631 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4633 s = data->tmpnam_buffer;
4636 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4637 p = s + sprintf(s, "\\s%s.", tmpstr);
4638 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4640 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
4641 memcpy(p, tmpstr, size);
4642 p[size] = '\0';
4643 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4644 GetLastError() == ERROR_FILE_NOT_FOUND)
4645 break;
4647 return s;
4650 /*********************************************************************
4651 * _wtmpnam (MSVCRT.@)
4653 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
4655 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
4656 MSVCRT_wchar_t tmpstr[16];
4657 MSVCRT_wchar_t *p;
4658 int count, size;
4659 if (!s) {
4660 thread_data_t *data = msvcrt_get_thread_data();
4662 if(!data->wtmpnam_buffer)
4663 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4665 s = data->wtmpnam_buffer;
4668 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4669 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
4670 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4672 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
4673 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
4674 p[size] = '\0';
4675 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4676 GetLastError() == ERROR_FILE_NOT_FOUND)
4677 break;
4679 return s;
4682 /*********************************************************************
4683 * tmpfile (MSVCRT.@)
4685 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4687 char *filename = MSVCRT_tmpnam(NULL);
4688 int fd;
4689 MSVCRT_FILE* file = NULL;
4691 LOCK_FILES();
4692 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4693 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4694 if (fd != -1 && (file = msvcrt_alloc_fp()))
4696 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4698 file->_flag = 0;
4699 file = NULL;
4701 else file->_tmpfname = MSVCRT__strdup(filename);
4704 if(fd != -1 && !file)
4705 MSVCRT__close(fd);
4706 UNLOCK_FILES();
4707 return file;
4710 /*********************************************************************
4711 * tmpfile_s (MSVCRT.@)
4713 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4715 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4717 *file = MSVCRT_tmpfile();
4718 return 0;
4721 static int puts_clbk_file_a(void *file, int len, const char *str)
4723 return MSVCRT_fwrite(str, sizeof(char), len, file);
4726 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4728 int i, ret;
4730 MSVCRT__lock_file(file);
4732 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4733 ret = MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
4734 MSVCRT__unlock_file(file);
4735 return ret;
4738 for(i=0; i<len; i++) {
4739 if(MSVCRT_fputwc(str[i], file) == MSVCRT_WEOF) {
4740 MSVCRT__unlock_file(file);
4741 return -1;
4745 MSVCRT__unlock_file(file);
4746 return len;
4749 /*********************************************************************
4750 * vfprintf (MSVCRT.@)
4752 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4754 BOOL tmp_buf;
4755 int ret;
4757 MSVCRT__lock_file(file);
4758 tmp_buf = add_std_buffer(file);
4759 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4760 if(tmp_buf) remove_std_buffer(file);
4761 MSVCRT__unlock_file(file);
4763 return ret;
4766 /*********************************************************************
4767 * vfprintf_s (MSVCRT.@)
4769 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4771 BOOL tmp_buf;
4772 int ret;
4774 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4776 MSVCRT__lock_file(file);
4777 tmp_buf = add_std_buffer(file);
4778 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4779 if(tmp_buf) remove_std_buffer(file);
4780 MSVCRT__unlock_file(file);
4782 return ret;
4785 /*********************************************************************
4786 * vfwprintf (MSVCRT.@)
4788 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4790 BOOL tmp_buf;
4791 int ret;
4793 MSVCRT__lock_file(file);
4794 tmp_buf = add_std_buffer(file);
4795 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4796 if(tmp_buf) remove_std_buffer(file);
4797 MSVCRT__unlock_file(file);
4799 return ret;
4802 /*********************************************************************
4803 * vfwprintf_s (MSVCRT.@)
4805 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4807 BOOL tmp_buf;
4808 int ret;
4810 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4812 MSVCRT__lock_file(file);
4813 tmp_buf = add_std_buffer(file);
4814 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4815 if(tmp_buf) remove_std_buffer(file);
4816 MSVCRT__unlock_file(file);
4818 return ret;
4821 /*********************************************************************
4822 * _vfwprintf_l (MSVCRT.@)
4824 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
4825 MSVCRT__locale_t locale, __ms_va_list valist)
4827 BOOL tmp_buf;
4828 int ret;
4830 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4832 MSVCRT__lock_file(file);
4833 tmp_buf = add_std_buffer(file);
4834 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4835 if(tmp_buf) remove_std_buffer(file);
4836 MSVCRT__unlock_file(file);
4838 return ret;
4841 /*********************************************************************
4842 * vprintf (MSVCRT.@)
4844 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
4846 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
4849 /*********************************************************************
4850 * vprintf_s (MSVCRT.@)
4852 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
4854 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
4857 /*********************************************************************
4858 * vwprintf (MSVCRT.@)
4860 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
4862 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
4865 /*********************************************************************
4866 * vwprintf_s (MSVCRT.@)
4868 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
4870 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
4873 /*********************************************************************
4874 * fprintf (MSVCRT.@)
4876 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
4878 __ms_va_list valist;
4879 int res;
4880 __ms_va_start(valist, format);
4881 res = MSVCRT_vfprintf(file, format, valist);
4882 __ms_va_end(valist);
4883 return res;
4886 /*********************************************************************
4887 * fprintf_s (MSVCRT.@)
4889 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
4891 __ms_va_list valist;
4892 int res;
4893 __ms_va_start(valist, format);
4894 res = MSVCRT_vfprintf_s(file, format, valist);
4895 __ms_va_end(valist);
4896 return res;
4899 /*********************************************************************
4900 * fwprintf (MSVCRT.@)
4902 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4904 __ms_va_list valist;
4905 int res;
4906 __ms_va_start(valist, format);
4907 res = MSVCRT_vfwprintf(file, format, valist);
4908 __ms_va_end(valist);
4909 return res;
4912 /*********************************************************************
4913 * fwprintf_s (MSVCRT.@)
4915 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4917 __ms_va_list valist;
4918 int res;
4919 __ms_va_start(valist, format);
4920 res = MSVCRT_vfwprintf_s(file, format, valist);
4921 __ms_va_end(valist);
4922 return res;
4925 /*********************************************************************
4926 * _fwprintf_l (MSVCRT.@)
4928 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
4930 __ms_va_list valist;
4931 int res;
4932 __ms_va_start(valist, locale);
4933 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
4934 __ms_va_end(valist);
4935 return res;
4938 /*********************************************************************
4939 * printf (MSVCRT.@)
4941 int CDECL MSVCRT_printf(const char *format, ...)
4943 __ms_va_list valist;
4944 int res;
4945 __ms_va_start(valist, format);
4946 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
4947 __ms_va_end(valist);
4948 return res;
4951 /*********************************************************************
4952 * printf_s (MSVCRT.@)
4954 int CDECL MSVCRT_printf_s(const char *format, ...)
4956 __ms_va_list valist;
4957 int res;
4958 __ms_va_start(valist, format);
4959 res = MSVCRT_vprintf_s(format, valist);
4960 __ms_va_end(valist);
4961 return res;
4964 /*********************************************************************
4965 * ungetc (MSVCRT.@)
4967 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
4969 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
4971 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
4972 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
4973 return MSVCRT_EOF;
4975 MSVCRT__lock_file(file);
4976 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
4977 && msvcrt_alloc_buffer(file))
4978 || (!file->_cnt && file->_ptr==file->_base))
4979 file->_ptr++;
4981 if(file->_ptr>file->_base) {
4982 file->_ptr--;
4983 if(file->_flag & MSVCRT__IOSTRG) {
4984 if(*file->_ptr != c) {
4985 file->_ptr++;
4986 MSVCRT__unlock_file(file);
4987 return MSVCRT_EOF;
4989 }else {
4990 *file->_ptr = c;
4992 file->_cnt++;
4993 MSVCRT_clearerr(file);
4994 file->_flag |= MSVCRT__IOREAD;
4995 MSVCRT__unlock_file(file);
4996 return c;
4999 MSVCRT__unlock_file(file);
5000 return MSVCRT_EOF;
5003 /*********************************************************************
5004 * ungetwc (MSVCRT.@)
5006 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5008 MSVCRT_wchar_t mwc = wc;
5010 if (wc == MSVCRT_WEOF)
5011 return MSVCRT_WEOF;
5013 MSVCRT__lock_file(file);
5015 if((msvcrt_get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5016 || !(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
5017 unsigned char * pp = (unsigned char *)&mwc;
5018 int i;
5020 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
5021 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
5022 MSVCRT__unlock_file(file);
5023 return MSVCRT_WEOF;
5026 }else {
5027 char mbs[MSVCRT_MB_LEN_MAX];
5028 int len;
5030 len = MSVCRT_wctomb(mbs, mwc);
5031 if(len == -1) {
5032 MSVCRT__unlock_file(file);
5033 return MSVCRT_WEOF;
5036 for(len--; len>=0; len--) {
5037 if(mbs[len] != MSVCRT_ungetc(mbs[len], file)) {
5038 MSVCRT__unlock_file(file);
5039 return MSVCRT_WEOF;
5044 MSVCRT__unlock_file(file);
5045 return mwc;
5048 /*********************************************************************
5049 * wprintf (MSVCRT.@)
5051 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
5053 __ms_va_list valist;
5054 int res;
5055 __ms_va_start(valist, format);
5056 res = MSVCRT_vwprintf(format, valist);
5057 __ms_va_end(valist);
5058 return res;
5061 /*********************************************************************
5062 * wprintf_s (MSVCRT.@)
5064 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
5066 __ms_va_list valist;
5067 int res;
5068 __ms_va_start(valist, format);
5069 res = MSVCRT_vwprintf_s(format, valist);
5070 __ms_va_end(valist);
5071 return res;
5074 /*********************************************************************
5075 * _getmaxstdio (MSVCRT.@)
5077 int CDECL MSVCRT__getmaxstdio(void)
5079 return MSVCRT_max_streams;
5082 /*********************************************************************
5083 * _setmaxstdio (MSVCRT.@)
5085 int CDECL MSVCRT__setmaxstdio(int newmax)
5087 TRACE("%d\n", newmax);
5089 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5090 return -1;
5092 MSVCRT_max_streams = newmax;
5093 return MSVCRT_max_streams;