msvcrt/tests: Make qsort_comp() static.
[wine/multimedia.git] / dlls / msvcrt / file.c
blob77a0fedf48649a89b7c20a03fb29c388baae2a4a
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_NOSEEK 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 /* ioinfo structure size is different in msvcrXX.dll's */
85 typedef struct {
86 HANDLE handle;
87 unsigned char wxflag;
88 char lookahead[3];
89 int exflag;
90 CRITICAL_SECTION crit;
91 #if _MSVCR_VER >= 80
92 char textmode : 7;
93 char unicode : 1;
94 char pipech2[2];
95 __int64 startpos;
96 BOOL utf8translations;
97 #endif
98 #if _MSVCR_VER >= 90
99 char dbcsBuffer;
100 BOOL dbcsBufferUsed;
101 #endif
102 } ioinfo;
104 /*********************************************************************
105 * __pioinfo (MSVCRT.@)
106 * array of pointers to ioinfo arrays [32]
108 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
110 /*********************************************************************
111 * __badioinfo (MSVCRT.@)
113 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
115 static int MSVCRT_fdstart = 3; /* first unallocated fd */
116 static int MSVCRT_fdend = 3; /* highest allocated fd */
118 typedef struct {
119 MSVCRT_FILE file;
120 CRITICAL_SECTION crit;
121 } file_crit;
123 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
124 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
125 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
127 /* INTERNAL: process umask */
128 static int MSVCRT_umask = 0;
130 /* INTERNAL: static data for tmpnam and _wtmpname functions */
131 static int tmpnam_unique;
133 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
134 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
135 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
136 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
138 #define TOUL(x) (ULONGLONG)(x)
139 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
140 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
141 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
142 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
144 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
145 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
146 * and MSVCRT_stream_idx, from race conditions.
147 * It doesn't protect against race conditions manipulating the underlying files
148 * or flags; doing so would probably be better accomplished with per-file
149 * protection, rather than locking the whole table for every change.
151 static CRITICAL_SECTION MSVCRT_file_cs;
152 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
154 0, 0, &MSVCRT_file_cs,
155 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
156 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
158 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
159 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
160 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
162 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
164 buf->st_dev = buf64->st_dev;
165 buf->st_ino = buf64->st_ino;
166 buf->st_mode = buf64->st_mode;
167 buf->st_nlink = buf64->st_nlink;
168 buf->st_uid = buf64->st_uid;
169 buf->st_gid = buf64->st_gid;
170 buf->st_rdev = buf64->st_rdev;
171 buf->st_size = buf64->st_size;
172 buf->st_atime = buf64->st_atime;
173 buf->st_mtime = buf64->st_mtime;
174 buf->st_ctime = buf64->st_ctime;
177 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
179 buf->st_dev = buf64->st_dev;
180 buf->st_ino = buf64->st_ino;
181 buf->st_mode = buf64->st_mode;
182 buf->st_nlink = buf64->st_nlink;
183 buf->st_uid = buf64->st_uid;
184 buf->st_gid = buf64->st_gid;
185 buf->st_rdev = buf64->st_rdev;
186 buf->st_size = buf64->st_size;
187 buf->st_atime = buf64->st_atime;
188 buf->st_mtime = buf64->st_mtime;
189 buf->st_ctime = buf64->st_ctime;
192 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32 *buf)
194 buf->st_dev = buf64->st_dev;
195 buf->st_ino = buf64->st_ino;
196 buf->st_mode = buf64->st_mode;
197 buf->st_nlink = buf64->st_nlink;
198 buf->st_uid = buf64->st_uid;
199 buf->st_gid = buf64->st_gid;
200 buf->st_rdev = buf64->st_rdev;
201 buf->st_size = buf64->st_size;
202 buf->st_atime = buf64->st_atime;
203 buf->st_mtime = buf64->st_mtime;
204 buf->st_ctime = buf64->st_ctime;
207 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat64i32 *buf)
209 buf->st_dev = buf64->st_dev;
210 buf->st_ino = buf64->st_ino;
211 buf->st_mode = buf64->st_mode;
212 buf->st_nlink = buf64->st_nlink;
213 buf->st_uid = buf64->st_uid;
214 buf->st_gid = buf64->st_gid;
215 buf->st_rdev = buf64->st_rdev;
216 buf->st_size = buf64->st_size;
217 buf->st_atime = buf64->st_atime;
218 buf->st_mtime = buf64->st_mtime;
219 buf->st_ctime = buf64->st_ctime;
222 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32i64 *buf)
224 buf->st_dev = buf64->st_dev;
225 buf->st_ino = buf64->st_ino;
226 buf->st_mode = buf64->st_mode;
227 buf->st_nlink = buf64->st_nlink;
228 buf->st_uid = buf64->st_uid;
229 buf->st_gid = buf64->st_gid;
230 buf->st_rdev = buf64->st_rdev;
231 buf->st_size = buf64->st_size;
232 buf->st_atime = buf64->st_atime;
233 buf->st_mtime = buf64->st_mtime;
234 buf->st_ctime = buf64->st_ctime;
237 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
239 /* 1601 to 1970 is 369 years plus 89 leap days */
240 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
242 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
243 ft->dwHighDateTime = ticks >> 32;
244 ft->dwLowDateTime = ticks;
247 static inline ioinfo* msvcrt_get_ioinfo(int fd)
249 ioinfo *ret = NULL;
250 if(fd < MSVCRT_MAX_FILES)
251 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
252 if(!ret)
253 return &MSVCRT___badioinfo;
255 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
258 static inline MSVCRT_FILE* msvcrt_get_file(int i)
260 file_crit *ret;
262 if(i >= MSVCRT_max_streams)
263 return NULL;
265 if(i < _IOB_ENTRIES)
266 return &MSVCRT__iob[i];
268 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
269 if(!ret) {
270 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
271 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
272 ERR("out of memory\n");
273 *MSVCRT__errno() = MSVCRT_ENOMEM;
274 return NULL;
277 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
278 } else
279 ret += i%MSVCRT_FD_BLOCK_SIZE;
281 return &ret->file;
284 static inline BOOL msvcrt_is_valid_fd(int fd)
286 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
289 /* INTERNAL: Get the HANDLE for a fd
290 * This doesn't lock the table, because a failure will result in
291 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
292 * it returns a valid handle which is about to be closed, a subsequent call
293 * will fail, most likely in a sane way.
295 static HANDLE msvcrt_fdtoh(int fd)
297 if (!msvcrt_is_valid_fd(fd))
299 WARN(":fd (%d) - no handle!\n",fd);
300 *MSVCRT___doserrno() = 0;
301 *MSVCRT__errno() = MSVCRT_EBADF;
302 return INVALID_HANDLE_VALUE;
304 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
305 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd);
306 return msvcrt_get_ioinfo(fd)->handle;
309 /* INTERNAL: free a file entry fd */
310 static void msvcrt_free_fd(int fd)
312 ioinfo *fdinfo;
314 LOCK_FILES();
315 fdinfo = msvcrt_get_ioinfo(fd);
316 if(fdinfo != &MSVCRT___badioinfo)
318 fdinfo->handle = INVALID_HANDLE_VALUE;
319 fdinfo->wxflag = 0;
321 TRACE(":fd (%d) freed\n",fd);
323 if (fd < 3)
325 switch (fd)
327 case 0:
328 SetStdHandle(STD_INPUT_HANDLE, 0);
329 break;
330 case 1:
331 SetStdHandle(STD_OUTPUT_HANDLE, 0);
332 break;
333 case 2:
334 SetStdHandle(STD_ERROR_HANDLE, 0);
335 break;
339 if (fd == MSVCRT_fdend - 1)
340 MSVCRT_fdend--;
341 if (fd < MSVCRT_fdstart)
342 MSVCRT_fdstart = fd;
343 UNLOCK_FILES();
346 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
347 /* caller must hold the files lock */
348 static int msvcrt_set_fd(HANDLE hand, int flag, int fd)
350 ioinfo *fdinfo;
352 if (fd >= MSVCRT_MAX_FILES)
354 WARN(":files exhausted!\n");
355 *MSVCRT__errno() = MSVCRT_ENFILE;
356 return -1;
359 fdinfo = msvcrt_get_ioinfo(fd);
360 if(fdinfo == &MSVCRT___badioinfo) {
361 int i;
363 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
364 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
365 WARN(":out of memory!\n");
366 *MSVCRT__errno() = MSVCRT_ENOMEM;
367 return -1;
370 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
371 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
373 fdinfo = msvcrt_get_ioinfo(fd);
376 fdinfo->handle = hand;
377 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_NOSEEK));
378 fdinfo->lookahead[0] = '\n';
379 fdinfo->lookahead[1] = '\n';
380 fdinfo->lookahead[2] = '\n';
381 fdinfo->exflag = 0;
383 /* locate next free slot */
384 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
385 MSVCRT_fdstart = MSVCRT_fdend + 1;
386 else
387 while (MSVCRT_fdstart < MSVCRT_fdend &&
388 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
389 MSVCRT_fdstart++;
390 /* update last fd in use */
391 if (fd >= MSVCRT_fdend)
392 MSVCRT_fdend = fd + 1;
393 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
395 switch (fd)
397 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
398 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
399 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
402 return fd;
405 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
406 static int msvcrt_alloc_fd(HANDLE hand, int flag)
408 int ret;
410 LOCK_FILES();
411 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
412 ret = msvcrt_set_fd(hand, flag, MSVCRT_fdstart);
413 UNLOCK_FILES();
414 return ret;
417 /* INTERNAL: Allocate a FILE* for an fd slot */
418 /* caller must hold the files lock */
419 static MSVCRT_FILE* msvcrt_alloc_fp(void)
421 int i;
422 MSVCRT_FILE *file;
424 for (i = 3; i < MSVCRT_max_streams; i++)
426 file = msvcrt_get_file(i);
427 if (!file)
428 return NULL;
430 if (file->_flag == 0)
432 if (i == MSVCRT_stream_idx)
434 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
436 InitializeCriticalSection(&((file_crit*)file)->crit);
437 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
439 MSVCRT_stream_idx++;
441 return file;
445 return NULL;
448 /* INTERNAL: initialize a FILE* from an open fd */
449 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
451 TRACE(":fd (%d) allocating FILE*\n",fd);
452 if (!msvcrt_is_valid_fd(fd))
454 WARN(":invalid fd %d\n",fd);
455 *MSVCRT___doserrno() = 0;
456 *MSVCRT__errno() = MSVCRT_EBADF;
457 return -1;
459 memset(file, 0, sizeof(*file));
460 file->_file = fd;
461 file->_flag = stream_flags;
463 TRACE(":got FILE* (%p)\n",file);
464 return 0;
467 /* INTERNAL: Create an inheritance data block (for spawned process)
468 * The inheritance block is made of:
469 * 00 int nb of file descriptor (NBFD)
470 * 04 char file flags (wxflag): repeated for each fd
471 * 4+NBFD HANDLE file handle: repeated for each fd
473 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
475 int fd;
476 char* wxflag_ptr;
477 HANDLE* handle_ptr;
478 ioinfo* fdinfo;
480 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
481 *block = MSVCRT_calloc(*size, 1);
482 if (!*block)
484 *size = 0;
485 return FALSE;
487 wxflag_ptr = (char*)*block + sizeof(unsigned);
488 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
490 *(unsigned*)*block = MSVCRT_fdend;
491 for (fd = 0; fd < MSVCRT_fdend; fd++)
493 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
494 fdinfo = msvcrt_get_ioinfo(fd);
495 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
497 *wxflag_ptr = fdinfo->wxflag;
498 *handle_ptr = fdinfo->handle;
500 else
502 *wxflag_ptr = 0;
503 *handle_ptr = INVALID_HANDLE_VALUE;
505 wxflag_ptr++; handle_ptr++;
507 return TRUE;
510 /* INTERNAL: Set up all file descriptors,
511 * as well as default streams (stdin, stderr and stdout)
513 void msvcrt_init_io(void)
515 STARTUPINFOA si;
516 int i;
517 ioinfo *fdinfo;
519 GetStartupInfoA(&si);
520 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
522 BYTE* wxflag_ptr;
523 HANDLE* handle_ptr;
524 unsigned int count;
526 count = *(unsigned*)si.lpReserved2;
527 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
528 handle_ptr = (HANDLE*)(wxflag_ptr + count);
530 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
531 count = min(count, MSVCRT_MAX_FILES);
532 for (i = 0; i < count; i++)
534 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
535 msvcrt_set_fd(*handle_ptr, *wxflag_ptr, i);
537 wxflag_ptr++; handle_ptr++;
539 MSVCRT_fdend = max( 3, count );
540 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
541 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
544 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO);
545 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
546 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
547 DWORD type = GetFileType(h);
549 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_NOSEEK : 0)
550 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDIN_FILENO);
553 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO);
554 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
555 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
556 DWORD type = GetFileType(h);
558 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_NOSEEK : 0)
559 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDOUT_FILENO);
562 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO);
563 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
564 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
565 DWORD type = GetFileType(h);
567 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_NOSEEK : 0)
568 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDERR_FILENO);
571 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO)->handle,
572 msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO)->handle,
573 msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO)->handle);
575 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
576 for (i = 0; i < 3; i++)
578 /* FILE structs for stdin/out/err are static and never deleted */
579 MSVCRT__iob[i]._file = i;
580 MSVCRT__iob[i]._tmpfname = NULL;
581 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
583 MSVCRT_stream_idx = 3;
586 /* INTERNAL: Flush stdio file buffer */
587 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
589 if(file->_bufsiz) {
590 int cnt=file->_ptr-file->_base;
591 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
592 file->_flag |= MSVCRT__IOERR;
593 return MSVCRT_EOF;
595 file->_ptr=file->_base;
596 file->_cnt=file->_bufsiz;
598 return 0;
601 /*********************************************************************
602 * _isatty (MSVCRT.@)
604 int CDECL MSVCRT__isatty(int fd)
606 HANDLE hand = msvcrt_fdtoh(fd);
608 TRACE(":fd (%d) handle (%p)\n",fd,hand);
609 if (hand == INVALID_HANDLE_VALUE)
610 return 0;
612 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
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_BUFSIZ,1);
623 if(file->_base) {
624 file->_bufsiz = MSVCRT_BUFSIZ;
625 file->_flag |= MSVCRT__IOMYBUF;
626 } else {
627 file->_base = (char*)(&file->_charbuf);
628 /* put here 2 ??? */
629 file->_bufsiz = sizeof(file->_charbuf);
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 || !MSVCRT__isatty(file->_file) || file->_bufsiz)
643 return FALSE;
645 file->_ptr = file->_base = buffers[file->_file == MSVCRT_STDOUT_FILENO ? 0 : 1];
646 file->_bufsiz = file->_cnt = MSVCRT_BUFSIZ;
647 return TRUE;
650 /* INTERNAL: Removes temporary buffer from stdout or stderr */
651 /* Only call this function when add_std_buffer returned TRUE */
652 static void remove_std_buffer(MSVCRT_FILE *file)
654 msvcrt_flush_buffer(file);
655 file->_ptr = file->_base = NULL;
656 file->_bufsiz = file->_cnt = 0;
659 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
660 static int msvcrt_int_to_base32(int num, char *str)
662 char *p;
663 int n = num;
664 int digits = 0;
666 while (n != 0)
668 n >>= 5;
669 digits++;
671 p = str + digits;
672 *p = 0;
673 while (--p >= str)
675 *p = (num & 31) + '0';
676 if (*p > '9')
677 *p += ('a' - '0' - 10);
678 num >>= 5;
681 return digits;
684 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
685 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
687 MSVCRT_wchar_t *p;
688 int n = num;
689 int digits = 0;
691 while (n != 0)
693 n >>= 5;
694 digits++;
696 p = str + digits;
697 *p = 0;
698 while (--p >= str)
700 *p = (num & 31) + '0';
701 if (*p > '9')
702 *p += ('a' - '0' - 10);
703 num >>= 5;
706 return digits;
709 /*********************************************************************
710 * __iob_func(MSVCRT.@)
712 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
714 return &MSVCRT__iob[0];
717 /*********************************************************************
718 * _access (MSVCRT.@)
720 int CDECL MSVCRT__access(const char *filename, int mode)
722 DWORD attr = GetFileAttributesA(filename);
724 TRACE("(%s,%d) %d\n",filename,mode,attr);
726 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
728 msvcrt_set_errno(GetLastError());
729 return -1;
731 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
733 msvcrt_set_errno(ERROR_ACCESS_DENIED);
734 return -1;
736 return 0;
739 /*********************************************************************
740 * _access_s (MSVCRT.@)
742 int CDECL MSVCRT__access_s(const char *filename, int mode)
744 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
745 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
747 if (MSVCRT__access(filename, mode) == -1)
748 return *MSVCRT__errno();
749 return 0;
752 /*********************************************************************
753 * _waccess (MSVCRT.@)
755 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
757 DWORD attr = GetFileAttributesW(filename);
759 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
761 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
763 msvcrt_set_errno(GetLastError());
764 return -1;
766 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
768 msvcrt_set_errno(ERROR_ACCESS_DENIED);
769 return -1;
771 return 0;
774 /*********************************************************************
775 * _waccess_s (MSVCRT.@)
777 int CDECL MSVCRT__waccess_s(const MSVCRT_wchar_t *filename, int mode)
779 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
780 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
782 if (MSVCRT__waccess(filename, mode) == -1)
783 return *MSVCRT__errno();
784 return 0;
787 /*********************************************************************
788 * _chmod (MSVCRT.@)
790 int CDECL MSVCRT__chmod(const char *path, int flags)
792 DWORD oldFlags = GetFileAttributesA(path);
794 if (oldFlags != INVALID_FILE_ATTRIBUTES)
796 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
797 oldFlags | FILE_ATTRIBUTE_READONLY;
799 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
800 return 0;
802 msvcrt_set_errno(GetLastError());
803 return -1;
806 /*********************************************************************
807 * _wchmod (MSVCRT.@)
809 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
811 DWORD oldFlags = GetFileAttributesW(path);
813 if (oldFlags != INVALID_FILE_ATTRIBUTES)
815 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
816 oldFlags | FILE_ATTRIBUTE_READONLY;
818 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
819 return 0;
821 msvcrt_set_errno(GetLastError());
822 return -1;
825 /*********************************************************************
826 * _unlink (MSVCRT.@)
828 int CDECL MSVCRT__unlink(const char *path)
830 TRACE("%s\n",debugstr_a(path));
831 if(DeleteFileA(path))
832 return 0;
833 TRACE("failed (%d)\n",GetLastError());
834 msvcrt_set_errno(GetLastError());
835 return -1;
838 /*********************************************************************
839 * _wunlink (MSVCRT.@)
841 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
843 TRACE("(%s)\n",debugstr_w(path));
844 if(DeleteFileW(path))
845 return 0;
846 TRACE("failed (%d)\n",GetLastError());
847 msvcrt_set_errno(GetLastError());
848 return -1;
851 /*********************************************************************
852 * _commit (MSVCRT.@)
854 int CDECL MSVCRT__commit(int fd)
856 HANDLE hand = msvcrt_fdtoh(fd);
858 TRACE(":fd (%d) handle (%p)\n",fd,hand);
859 if (hand == INVALID_HANDLE_VALUE)
860 return -1;
862 if (!FlushFileBuffers(hand))
864 if (GetLastError() == ERROR_INVALID_HANDLE)
866 /* FlushFileBuffers fails for console handles
867 * so we ignore this error.
869 return 0;
871 TRACE(":failed-last error (%d)\n",GetLastError());
872 msvcrt_set_errno(GetLastError());
873 return -1;
875 TRACE(":ok\n");
876 return 0;
879 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
880 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
882 /* INTERNAL: Flush all stream buffer */
883 static int msvcrt_flush_all_buffers(int mask)
885 int i, num_flushed = 0;
886 MSVCRT_FILE *file;
888 LOCK_FILES();
889 for (i = 0; i < MSVCRT_stream_idx; i++) {
890 file = msvcrt_get_file(i);
892 if (file->_flag)
894 if(file->_flag & mask) {
895 MSVCRT_fflush(file);
896 num_flushed++;
900 UNLOCK_FILES();
902 TRACE(":flushed (%d) handles\n",num_flushed);
903 return num_flushed;
906 /*********************************************************************
907 * _flushall (MSVCRT.@)
909 int CDECL MSVCRT__flushall(void)
911 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
914 /*********************************************************************
915 * fflush (MSVCRT.@)
917 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
919 if(!file) {
920 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
921 } else if(file->_flag & MSVCRT__IOWRT) {
922 int res;
924 MSVCRT__lock_file(file);
925 res = msvcrt_flush_buffer(file);
927 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
928 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
929 MSVCRT__unlock_file(file);
931 return res;
932 } else if(file->_flag & MSVCRT__IOREAD) {
933 MSVCRT__lock_file(file);
934 file->_cnt = 0;
935 file->_ptr = file->_base;
936 MSVCRT__unlock_file(file);
938 return 0;
940 return 0;
943 /*********************************************************************
944 * _close (MSVCRT.@)
946 int CDECL MSVCRT__close(int fd)
948 HANDLE hand;
949 int ret;
951 LOCK_FILES();
952 hand = msvcrt_fdtoh(fd);
953 TRACE(":fd (%d) handle (%p)\n",fd,hand);
954 if (!msvcrt_is_valid_fd(fd)) {
955 ret = -1;
956 } else {
957 msvcrt_free_fd(fd);
958 ret = CloseHandle(hand) ? 0 : -1;
959 if (ret) {
960 WARN(":failed-last error (%d)\n",GetLastError());
961 msvcrt_set_errno(GetLastError());
964 UNLOCK_FILES();
965 TRACE(":ok\n");
966 return ret;
969 /*********************************************************************
970 * _dup2 (MSVCRT.@)
971 * NOTES
972 * MSDN isn't clear on this point, but the remarks for _pipe
973 * indicate file descriptors duplicated with _dup and _dup2 are always
974 * inheritable.
976 int CDECL MSVCRT__dup2(int od, int nd)
978 int ret;
980 TRACE("(od=%d, nd=%d)\n", od, nd);
981 LOCK_FILES();
982 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
984 HANDLE handle;
986 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
987 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
989 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
991 if (msvcrt_is_valid_fd(nd))
992 MSVCRT__close(nd);
993 ret = msvcrt_set_fd(handle, wxflag, nd);
994 if (ret == -1)
996 CloseHandle(handle);
997 *MSVCRT__errno() = MSVCRT_EMFILE;
999 else
1001 /* _dup2 returns 0, not nd, on success */
1002 ret = 0;
1005 else
1007 ret = -1;
1008 msvcrt_set_errno(GetLastError());
1011 else
1013 *MSVCRT__errno() = MSVCRT_EBADF;
1014 ret = -1;
1016 UNLOCK_FILES();
1017 return ret;
1020 /*********************************************************************
1021 * _dup (MSVCRT.@)
1023 int CDECL MSVCRT__dup(int od)
1025 int fd, ret;
1027 LOCK_FILES();
1028 fd = MSVCRT_fdstart;
1029 if (MSVCRT__dup2(od, fd) == 0)
1030 ret = fd;
1031 else
1032 ret = -1;
1033 UNLOCK_FILES();
1034 return ret;
1037 /*********************************************************************
1038 * _eof (MSVCRT.@)
1040 int CDECL MSVCRT__eof(int fd)
1042 DWORD curpos,endpos;
1043 LONG hcurpos,hendpos;
1044 HANDLE hand = msvcrt_fdtoh(fd);
1046 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1048 if (hand == INVALID_HANDLE_VALUE)
1049 return -1;
1051 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
1053 /* Otherwise we do it the hard way */
1054 hcurpos = hendpos = 0;
1055 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
1056 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
1058 if (curpos == endpos && hcurpos == hendpos)
1060 /* FIXME: shouldn't WX_ATEOF be set here? */
1061 return TRUE;
1064 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
1065 return FALSE;
1068 /*********************************************************************
1069 * _fcloseall (MSVCRT.@)
1071 int CDECL MSVCRT__fcloseall(void)
1073 int num_closed = 0, i;
1074 MSVCRT_FILE *file;
1076 LOCK_FILES();
1077 for (i = 3; i < MSVCRT_stream_idx; i++) {
1078 file = msvcrt_get_file(i);
1080 if (file->_flag && !MSVCRT_fclose(file))
1081 num_closed++;
1083 UNLOCK_FILES();
1085 TRACE(":closed (%d) handles\n",num_closed);
1086 return num_closed;
1089 /* free everything on process exit */
1090 void msvcrt_free_io(void)
1092 unsigned int i;
1093 int j;
1095 MSVCRT__flushall();
1096 MSVCRT__fcloseall();
1098 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1099 MSVCRT_free(MSVCRT___pioinfo[i]);
1101 for(j=0; j<MSVCRT_stream_idx; j++)
1103 MSVCRT_FILE *file = msvcrt_get_file(j);
1104 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1106 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1107 DeleteCriticalSection(&((file_crit*)file)->crit);
1111 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1112 MSVCRT_free(MSVCRT_fstream[i]);
1114 DeleteCriticalSection(&MSVCRT_file_cs);
1117 /*********************************************************************
1118 * _lseeki64 (MSVCRT.@)
1120 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1122 HANDLE hand = msvcrt_fdtoh(fd);
1123 LARGE_INTEGER ofs;
1125 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1126 if (hand == INVALID_HANDLE_VALUE)
1127 return -1;
1129 if (whence < 0 || whence > 2)
1131 *MSVCRT__errno() = MSVCRT_EINVAL;
1132 return -1;
1135 TRACE(":fd (%d) to %s pos %s\n",
1136 fd,wine_dbgstr_longlong(offset),
1137 (whence==SEEK_SET)?"SEEK_SET":
1138 (whence==SEEK_CUR)?"SEEK_CUR":
1139 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1141 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1142 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1143 ofs.QuadPart = offset;
1144 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1145 GetLastError() == ERROR_SUCCESS)
1147 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_ATEOF;
1148 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1150 return ofs.QuadPart;
1152 TRACE(":error-last error (%d)\n",GetLastError());
1153 msvcrt_set_errno(GetLastError());
1154 return -1;
1157 /*********************************************************************
1158 * _lseek (MSVCRT.@)
1160 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1162 return MSVCRT__lseeki64(fd, offset, whence);
1165 /*********************************************************************
1166 * _lock_file (MSVCRT.@)
1168 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1170 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1171 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1172 else
1173 EnterCriticalSection(&((file_crit*)file)->crit);
1176 /*********************************************************************
1177 * _unlock_file (MSVCRT.@)
1179 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1181 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1182 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1183 else
1184 LeaveCriticalSection(&((file_crit*)file)->crit);
1187 /*********************************************************************
1188 * _locking (MSVCRT.@)
1190 * This is untested; the underlying LockFile doesn't work yet.
1192 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1194 BOOL ret;
1195 DWORD cur_locn;
1196 HANDLE hand = msvcrt_fdtoh(fd);
1198 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1199 if (hand == INVALID_HANDLE_VALUE)
1200 return -1;
1202 if (mode < 0 || mode > 4)
1204 *MSVCRT__errno() = MSVCRT_EINVAL;
1205 return -1;
1208 TRACE(":fd (%d) by 0x%08x mode %s\n",
1209 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1210 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1211 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1212 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1213 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1214 "UNKNOWN");
1216 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1218 FIXME ("Seek failed\n");
1219 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1220 return -1;
1222 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1224 int nretry = 10;
1225 ret = 1; /* just to satisfy gcc */
1226 while (nretry--)
1228 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1229 if (ret) break;
1230 Sleep(1);
1233 else if (mode == MSVCRT__LK_UNLCK)
1234 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1235 else
1236 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1237 /* FIXME - what about error settings? */
1238 return ret ? 0 : -1;
1241 /*********************************************************************
1242 * _fseeki64 (MSVCRT.@)
1244 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1246 int ret;
1248 MSVCRT__lock_file(file);
1249 /* Flush output if needed */
1250 if(file->_flag & MSVCRT__IOWRT)
1251 msvcrt_flush_buffer(file);
1253 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1254 whence = SEEK_SET;
1255 offset += MSVCRT__ftelli64(file);
1258 /* Discard buffered input */
1259 file->_cnt = 0;
1260 file->_ptr = file->_base;
1261 /* Reset direction of i/o */
1262 if(file->_flag & MSVCRT__IORW) {
1263 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1265 /* Clear end of file flag */
1266 file->_flag &= ~MSVCRT__IOEOF;
1267 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1269 MSVCRT__unlock_file(file);
1270 return ret;
1273 /*********************************************************************
1274 * fseek (MSVCRT.@)
1276 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1278 return MSVCRT__fseeki64( file, offset, whence );
1281 /*********************************************************************
1282 * _chsize_s (MSVCRT.@)
1284 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1286 __int64 cur, pos;
1287 HANDLE handle;
1288 BOOL ret = FALSE;
1290 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1292 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1294 LOCK_FILES();
1296 handle = msvcrt_fdtoh(fd);
1297 if (handle != INVALID_HANDLE_VALUE)
1299 /* save the current file pointer */
1300 cur = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1301 if (cur >= 0)
1303 pos = MSVCRT__lseeki64(fd, size, SEEK_SET);
1304 if (pos >= 0)
1306 ret = SetEndOfFile(handle);
1307 if (!ret) msvcrt_set_errno(GetLastError());
1310 /* restore the file pointer */
1311 MSVCRT__lseeki64(fd, cur, SEEK_SET);
1315 UNLOCK_FILES();
1316 return ret ? 0 : *MSVCRT__errno();
1319 /*********************************************************************
1320 * _chsize (MSVCRT.@)
1322 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1324 /* _chsize_s returns errno on failure but _chsize should return -1 */
1325 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1328 /*********************************************************************
1329 * clearerr (MSVCRT.@)
1331 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1333 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1335 MSVCRT__lock_file(file);
1336 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1337 MSVCRT__unlock_file(file);
1340 /*********************************************************************
1341 * rewind (MSVCRT.@)
1343 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1345 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1347 MSVCRT__lock_file(file);
1348 MSVCRT_fseek(file, 0L, SEEK_SET);
1349 MSVCRT_clearerr(file);
1350 MSVCRT__unlock_file(file);
1353 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1355 int plus = strchrW(mode, '+') != NULL;
1357 TRACE("%s\n", debugstr_w(mode));
1359 while(*mode == ' ') mode++;
1361 switch(*mode++)
1363 case 'R': case 'r':
1364 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1365 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1366 break;
1367 case 'W': case 'w':
1368 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1369 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1370 break;
1371 case 'A': case 'a':
1372 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1373 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1374 break;
1375 default:
1376 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1377 return -1;
1380 *stream_flags |= MSVCRT__commode;
1382 while (*mode && *mode!=',')
1383 switch (*mode++)
1385 case 'B': case 'b':
1386 *open_flags |= MSVCRT__O_BINARY;
1387 *open_flags &= ~MSVCRT__O_TEXT;
1388 break;
1389 case 't':
1390 *open_flags |= MSVCRT__O_TEXT;
1391 *open_flags &= ~MSVCRT__O_BINARY;
1392 break;
1393 case 'D':
1394 *open_flags |= MSVCRT__O_TEMPORARY;
1395 break;
1396 case 'T':
1397 *open_flags |= MSVCRT__O_SHORT_LIVED;
1398 break;
1399 case 'c':
1400 *stream_flags |= MSVCRT__IOCOMMIT;
1401 break;
1402 case 'n':
1403 *stream_flags &= ~MSVCRT__IOCOMMIT;
1404 break;
1405 case 'N':
1406 *open_flags |= MSVCRT__O_NOINHERIT;
1407 break;
1408 case '+':
1409 case ' ':
1410 case 'a':
1411 case 'w':
1412 break;
1413 case 'S':
1414 case 'R':
1415 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1416 break;
1417 default:
1418 ERR("incorrect mode flag: %c\n", mode[-1]);
1419 break;
1422 if(*mode == ',')
1424 static const WCHAR ccs[] = {'c','c','s'};
1425 static const WCHAR utf8[] = {'u','t','f','-','8'};
1426 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1427 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1429 mode++;
1430 while(*mode == ' ') mode++;
1431 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1432 return -1;
1433 mode += sizeof(ccs)/sizeof(ccs[0]);
1434 while(*mode == ' ') mode++;
1435 if(!MSVCRT_CHECK_PMT(*mode == '='))
1436 return -1;
1437 mode++;
1438 while(*mode == ' ') mode++;
1440 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1442 *open_flags |= MSVCRT__O_U8TEXT;
1443 mode += sizeof(utf8)/sizeof(utf8[0]);
1445 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1447 *open_flags |= MSVCRT__O_U16TEXT;
1448 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1450 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1452 *open_flags |= MSVCRT__O_WTEXT;
1453 mode += sizeof(unicode)/sizeof(unicode[0]);
1455 else
1457 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1458 return -1;
1461 while(*mode == ' ') mode++;
1464 if(!MSVCRT_CHECK_PMT(*mode == 0))
1465 return -1;
1466 return 0;
1469 /*********************************************************************
1470 * _fdopen (MSVCRT.@)
1472 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1474 MSVCRT_FILE *ret;
1475 MSVCRT_wchar_t *modeW = NULL;
1477 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1479 ret = MSVCRT__wfdopen(fd, modeW);
1481 MSVCRT_free(modeW);
1482 return ret;
1485 /*********************************************************************
1486 * _wfdopen (MSVCRT.@)
1488 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1490 int open_flags, stream_flags;
1491 MSVCRT_FILE* file;
1493 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1495 LOCK_FILES();
1496 if (!(file = msvcrt_alloc_fp()))
1497 file = NULL;
1498 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1500 file->_flag = 0;
1501 file = NULL;
1503 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1504 UNLOCK_FILES();
1506 return file;
1509 /*********************************************************************
1510 * _filelength (MSVCRT.@)
1512 LONG CDECL MSVCRT__filelength(int fd)
1514 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1515 if (curPos != -1)
1517 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1518 if (endPos != -1)
1520 if (endPos != curPos)
1521 MSVCRT__lseek(fd, curPos, SEEK_SET);
1522 return endPos;
1525 return -1;
1528 /*********************************************************************
1529 * _filelengthi64 (MSVCRT.@)
1531 __int64 CDECL MSVCRT__filelengthi64(int fd)
1533 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1534 if (curPos != -1)
1536 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1537 if (endPos != -1)
1539 if (endPos != curPos)
1540 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1541 return endPos;
1544 return -1;
1547 /*********************************************************************
1548 * _fileno (MSVCRT.@)
1550 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1552 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1553 return file->_file;
1556 /*********************************************************************
1557 * _fstat64 (MSVCRT.@)
1559 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1561 DWORD dw;
1562 DWORD type;
1563 BY_HANDLE_FILE_INFORMATION hfi;
1564 HANDLE hand = msvcrt_fdtoh(fd);
1566 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1567 if (hand == INVALID_HANDLE_VALUE)
1568 return -1;
1570 if (!buf)
1572 WARN(":failed-NULL buf\n");
1573 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1574 return -1;
1577 memset(&hfi, 0, sizeof(hfi));
1578 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1579 type = GetFileType(hand);
1580 if (type == FILE_TYPE_PIPE)
1582 buf->st_dev = buf->st_rdev = fd;
1583 buf->st_mode = S_IFIFO;
1584 buf->st_nlink = 1;
1586 else if (type == FILE_TYPE_CHAR)
1588 buf->st_dev = buf->st_rdev = fd;
1589 buf->st_mode = S_IFCHR;
1590 buf->st_nlink = 1;
1592 else /* FILE_TYPE_DISK etc. */
1594 if (!GetFileInformationByHandle(hand, &hfi))
1596 WARN(":failed-last error (%d)\n",GetLastError());
1597 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1598 return -1;
1600 buf->st_mode = S_IFREG | 0444;
1601 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1602 buf->st_mode |= 0222;
1603 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1604 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1605 buf->st_atime = dw;
1606 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1607 buf->st_mtime = buf->st_ctime = dw;
1608 buf->st_nlink = hfi.nNumberOfLinks;
1610 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1611 buf->st_mode);
1612 return 0;
1615 /*********************************************************************
1616 * _fstati64 (MSVCRT.@)
1618 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1620 int ret;
1621 struct MSVCRT__stat64 buf64;
1623 ret = MSVCRT__fstat64(fd, &buf64);
1624 if (!ret)
1625 msvcrt_stat64_to_stati64(&buf64, buf);
1626 return ret;
1629 /*********************************************************************
1630 * _fstat (MSVCRT.@)
1632 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1633 { int ret;
1634 struct MSVCRT__stat64 buf64;
1636 ret = MSVCRT__fstat64(fd, &buf64);
1637 if (!ret)
1638 msvcrt_stat64_to_stat(&buf64, buf);
1639 return ret;
1642 /*********************************************************************
1643 * _fstat32 (MSVCR80.@)
1645 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1647 int ret;
1648 struct MSVCRT__stat64 buf64;
1650 ret = MSVCRT__fstat64(fd, &buf64);
1651 if (!ret)
1652 msvcrt_stat64_to_stat32(&buf64, buf);
1653 return ret;
1656 /*********************************************************************
1657 * _fstat64i32 (MSVCR80.@)
1659 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1661 int ret;
1662 struct MSVCRT__stat64 buf64;
1664 ret = MSVCRT__fstat64(fd, &buf64);
1665 if (!ret)
1666 msvcrt_stat64_to_stat64i32(&buf64, buf);
1667 return ret;
1670 /*********************************************************************
1671 * _futime64 (MSVCRT.@)
1673 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1675 HANDLE hand = msvcrt_fdtoh(fd);
1676 FILETIME at, wt;
1678 if (!t)
1680 time_to_filetime( MSVCRT__time64(NULL), &at );
1681 wt = at;
1683 else
1685 time_to_filetime( t->actime, &at );
1686 time_to_filetime( t->modtime, &wt );
1689 if (!SetFileTime(hand, NULL, &at, &wt))
1691 msvcrt_set_errno(GetLastError());
1692 return -1 ;
1694 return 0;
1697 /*********************************************************************
1698 * _futime32 (MSVCRT.@)
1700 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1702 if (t)
1704 struct MSVCRT___utimbuf64 t64;
1705 t64.actime = t->actime;
1706 t64.modtime = t->modtime;
1707 return _futime64( fd, &t64 );
1709 else
1710 return _futime64( fd, NULL );
1713 /*********************************************************************
1714 * _futime (MSVCRT.@)
1716 #ifdef _WIN64
1717 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1719 return _futime64( fd, t );
1721 #else
1722 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1724 return _futime32( fd, t );
1726 #endif
1728 /*********************************************************************
1729 * _get_osfhandle (MSVCRT.@)
1731 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1733 HANDLE hand = msvcrt_fdtoh(fd);
1734 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1736 return (MSVCRT_intptr_t)hand;
1739 /*********************************************************************
1740 * _mktemp_s (MSVCRT.@)
1742 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1744 DWORD len, xno, id;
1746 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1747 return MSVCRT_EINVAL;
1749 for(len=0; len<size; len++)
1750 if(!pattern[len])
1751 break;
1752 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1753 if(size)
1754 pattern[0] = 0;
1755 return MSVCRT_EINVAL;
1758 for(xno=1; xno<=6; xno++)
1759 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1760 return MSVCRT_EINVAL;
1762 id = GetCurrentProcessId();
1763 for(xno=1; xno<6; xno++) {
1764 pattern[len-xno] = id%10 + '0';
1765 id /= 10;
1768 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1769 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1770 return 0;
1773 pattern[0] = 0;
1774 *MSVCRT__errno() = MSVCRT_EEXIST;
1775 return MSVCRT_EEXIST;
1778 /*********************************************************************
1779 * _mktemp (MSVCRT.@)
1781 char * CDECL MSVCRT__mktemp(char *pattern)
1783 int numX = 0;
1784 char *retVal = pattern;
1785 int id;
1786 char letter = 'a';
1788 if(!pattern)
1789 return NULL;
1791 while(*pattern)
1792 numX = (*pattern++ == 'X')? numX + 1 : 0;
1793 if (numX < 6)
1794 return NULL;
1795 pattern--;
1796 id = GetCurrentProcessId();
1797 numX = 6;
1798 while(numX--)
1800 int tempNum = id / 10;
1801 *pattern-- = id - (tempNum * 10) + '0';
1802 id = tempNum;
1804 pattern++;
1807 *pattern = letter++;
1808 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1809 return retVal;
1810 } while(letter <= 'z');
1811 return NULL;
1814 /*********************************************************************
1815 * _wmktemp_s (MSVCRT.@)
1817 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1819 DWORD len, xno, id;
1821 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1822 return MSVCRT_EINVAL;
1824 for(len=0; len<size; len++)
1825 if(!pattern[len])
1826 break;
1827 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1828 if(size)
1829 pattern[0] = 0;
1830 return MSVCRT_EINVAL;
1833 for(xno=1; xno<=6; xno++)
1834 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1835 return MSVCRT_EINVAL;
1837 id = GetCurrentProcessId();
1838 for(xno=1; xno<6; xno++) {
1839 pattern[len-xno] = id%10 + '0';
1840 id /= 10;
1843 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1844 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1845 return 0;
1848 pattern[0] = 0;
1849 *MSVCRT__errno() = MSVCRT_EEXIST;
1850 return MSVCRT_EEXIST;
1853 /*********************************************************************
1854 * _wmktemp (MSVCRT.@)
1856 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1858 int numX = 0;
1859 MSVCRT_wchar_t *retVal = pattern;
1860 int id;
1861 MSVCRT_wchar_t letter = 'a';
1863 if(!pattern)
1864 return NULL;
1866 while(*pattern)
1867 numX = (*pattern++ == 'X')? numX + 1 : 0;
1868 if (numX < 6)
1869 return NULL;
1870 pattern--;
1871 id = GetCurrentProcessId();
1872 numX = 6;
1873 while(numX--)
1875 int tempNum = id / 10;
1876 *pattern-- = id - (tempNum * 10) + '0';
1877 id = tempNum;
1879 pattern++;
1882 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
1883 return retVal;
1884 *pattern = letter++;
1885 } while(letter != '|');
1886 return NULL;
1889 static unsigned split_oflags(unsigned oflags)
1891 int wxflags = 0;
1892 unsigned unsupp; /* until we support everything */
1894 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1895 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1896 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1897 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
1898 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
1899 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
1900 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1901 else wxflags |= WX_TEXT; /* default to TEXT*/
1902 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1904 if ((unsupp = oflags & ~(
1905 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1906 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1907 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1908 MSVCRT__O_NOINHERIT|
1909 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
1910 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
1912 ERR(":unsupported oflags 0x%04x\n",unsupp);
1914 return wxflags;
1917 /*********************************************************************
1918 * _pipe (MSVCRT.@)
1920 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1922 int ret = -1;
1923 SECURITY_ATTRIBUTES sa;
1924 HANDLE readHandle, writeHandle;
1926 if (!pfds)
1928 *MSVCRT__errno() = MSVCRT_EINVAL;
1929 return -1;
1932 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1933 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1934 sa.lpSecurityDescriptor = NULL;
1935 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1937 unsigned int wxflags = split_oflags(textmode);
1938 int fd;
1940 LOCK_FILES();
1941 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
1942 if (fd != -1)
1944 pfds[0] = fd;
1945 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
1946 if (fd != -1)
1948 pfds[1] = fd;
1949 ret = 0;
1951 else
1953 MSVCRT__close(pfds[0]);
1954 CloseHandle(writeHandle);
1955 *MSVCRT__errno() = MSVCRT_EMFILE;
1958 else
1960 CloseHandle(readHandle);
1961 CloseHandle(writeHandle);
1962 *MSVCRT__errno() = MSVCRT_EMFILE;
1964 UNLOCK_FILES();
1966 else
1967 msvcrt_set_errno(GetLastError());
1969 return ret;
1972 static int check_bom(HANDLE h, int oflags, BOOL seek)
1974 char bom[sizeof(utf8_bom)];
1975 DWORD r;
1977 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
1979 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
1980 return oflags;
1982 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
1983 oflags |= MSVCRT__O_U8TEXT;
1984 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
1985 if (seek && r>2)
1986 SetFilePointer(h, 2, NULL, FILE_BEGIN);
1987 oflags |= MSVCRT__O_U16TEXT;
1988 }else if (seek) {
1989 SetFilePointer(h, 0, NULL, FILE_BEGIN);
1992 return oflags;
1995 /*********************************************************************
1996 * _wsopen_s (MSVCRT.@)
1998 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2000 DWORD access = 0, creation = 0, attrib;
2001 SECURITY_ATTRIBUTES sa;
2002 DWORD sharing;
2003 int wxflag;
2004 HANDLE hand;
2006 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2007 fd, debugstr_w(path), oflags, shflags, pmode);
2009 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2011 *fd = -1;
2012 wxflag = split_oflags(oflags);
2013 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2015 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2016 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2017 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2020 if (oflags & MSVCRT__O_CREAT)
2022 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
2023 FIXME(": pmode 0x%04x ignored\n", pmode);
2024 else
2025 WARN(": pmode 0x%04x ignored\n", pmode);
2027 if (oflags & MSVCRT__O_EXCL)
2028 creation = CREATE_NEW;
2029 else if (oflags & MSVCRT__O_TRUNC)
2030 creation = CREATE_ALWAYS;
2031 else
2032 creation = OPEN_ALWAYS;
2034 else /* no MSVCRT__O_CREAT */
2036 if (oflags & MSVCRT__O_TRUNC)
2037 creation = TRUNCATE_EXISTING;
2038 else
2039 creation = OPEN_EXISTING;
2042 switch( shflags )
2044 case MSVCRT__SH_DENYRW:
2045 sharing = 0L;
2046 break;
2047 case MSVCRT__SH_DENYWR:
2048 sharing = FILE_SHARE_READ;
2049 break;
2050 case MSVCRT__SH_DENYRD:
2051 sharing = FILE_SHARE_WRITE;
2052 break;
2053 case MSVCRT__SH_DENYNO:
2054 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2055 break;
2056 default:
2057 ERR( "Unhandled shflags 0x%x\n", shflags );
2058 return MSVCRT_EINVAL;
2060 attrib = FILE_ATTRIBUTE_NORMAL;
2062 if (oflags & MSVCRT__O_TEMPORARY)
2064 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2065 access |= DELETE;
2066 sharing |= FILE_SHARE_DELETE;
2069 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2070 sa.lpSecurityDescriptor = NULL;
2071 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2073 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2074 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2075 && !(access&GENERIC_READ))
2077 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2078 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2079 if (hand != INVALID_HANDLE_VALUE)
2081 oflags = check_bom(hand, oflags, FALSE);
2082 CloseHandle(hand);
2084 else
2085 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2088 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2089 if (hand == INVALID_HANDLE_VALUE) {
2090 WARN(":failed-last error (%d)\n",GetLastError());
2091 msvcrt_set_errno(GetLastError());
2092 return *MSVCRT__errno();
2095 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2097 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2098 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2099 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2101 if (oflags & MSVCRT__O_U8TEXT)
2103 DWORD written = 0, tmp;
2105 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2106 sizeof(utf8_bom)-written, &tmp, NULL))
2107 written += tmp;
2108 if (written != sizeof(utf8_bom)) {
2109 WARN("error writing BOM\n");
2110 CloseHandle(hand);
2111 msvcrt_set_errno(GetLastError());
2112 return *MSVCRT__errno();
2115 else
2117 DWORD written = 0, tmp;
2119 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2120 sizeof(utf16_bom)-written, &tmp, NULL))
2121 written += tmp;
2122 if (written != sizeof(utf16_bom))
2124 WARN("error writing BOM\n");
2125 CloseHandle(hand);
2126 msvcrt_set_errno(GetLastError());
2127 return *MSVCRT__errno();
2131 else if (access & GENERIC_READ)
2132 oflags = check_bom(hand, oflags, TRUE);
2135 *fd = msvcrt_alloc_fd(hand, wxflag);
2136 if (*fd == -1)
2137 return *MSVCRT__errno();
2139 if (oflags & MSVCRT__O_WTEXT)
2140 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2141 else if (oflags & MSVCRT__O_U16TEXT)
2142 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16;
2143 else if (oflags & MSVCRT__O_U8TEXT)
2144 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF8;
2146 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2147 return 0;
2150 /*********************************************************************
2151 * _wsopen (MSVCRT.@)
2153 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2155 int pmode;
2156 int fd;
2158 if (oflags & MSVCRT__O_CREAT)
2160 __ms_va_list ap;
2162 __ms_va_start(ap, shflags);
2163 pmode = va_arg(ap, int);
2164 __ms_va_end(ap);
2166 else
2167 pmode = 0;
2169 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
2170 return fd;
2173 /*********************************************************************
2174 * _sopen_s (MSVCRT.@)
2176 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2178 MSVCRT_wchar_t *pathW;
2179 int ret;
2181 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2182 return MSVCRT_EINVAL;
2184 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2185 MSVCRT_free(pathW);
2186 return ret;
2189 /*********************************************************************
2190 * _sopen (MSVCRT.@)
2192 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2194 int pmode;
2195 int fd;
2197 if (oflags & MSVCRT__O_CREAT)
2199 __ms_va_list ap;
2201 __ms_va_start(ap, shflags);
2202 pmode = va_arg(ap, int);
2203 __ms_va_end(ap);
2205 else
2206 pmode = 0;
2208 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2209 return fd;
2212 /*********************************************************************
2213 * _open (MSVCRT.@)
2215 int CDECL MSVCRT__open( const char *path, int flags, ... )
2217 __ms_va_list ap;
2219 if (flags & MSVCRT__O_CREAT)
2221 int pmode;
2222 __ms_va_start(ap, flags);
2223 pmode = va_arg(ap, int);
2224 __ms_va_end(ap);
2225 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2227 else
2228 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2231 /*********************************************************************
2232 * _wopen (MSVCRT.@)
2234 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2236 __ms_va_list ap;
2238 if (flags & MSVCRT__O_CREAT)
2240 int pmode;
2241 __ms_va_start(ap, flags);
2242 pmode = va_arg(ap, int);
2243 __ms_va_end(ap);
2244 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2246 else
2247 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2250 /*********************************************************************
2251 * _creat (MSVCRT.@)
2253 int CDECL MSVCRT__creat(const char *path, int flags)
2255 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2256 return MSVCRT__open(path, usedFlags);
2259 /*********************************************************************
2260 * _wcreat (MSVCRT.@)
2262 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2264 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2265 return MSVCRT__wopen(path, usedFlags);
2268 /*********************************************************************
2269 * _open_osfhandle (MSVCRT.@)
2271 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2273 DWORD flags;
2274 int fd;
2276 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2277 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2278 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2279 * text - it never sets MSVCRT__O_BINARY.
2281 /* don't let split_oflags() decide the mode if no mode is passed */
2282 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2283 oflags |= MSVCRT__O_BINARY;
2285 flags = GetFileType((HANDLE)handle);
2286 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2288 msvcrt_set_errno(GetLastError());
2289 return -1;
2292 if (flags == FILE_TYPE_CHAR)
2293 flags = WX_NOSEEK;
2294 else if (flags == FILE_TYPE_PIPE)
2295 flags = WX_PIPE;
2296 else
2297 flags = 0;
2298 flags |= split_oflags(oflags);
2300 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2301 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2302 return fd;
2305 /*********************************************************************
2306 * _rmtmp (MSVCRT.@)
2308 int CDECL MSVCRT__rmtmp(void)
2310 int num_removed = 0, i;
2311 MSVCRT_FILE *file;
2313 LOCK_FILES();
2314 for (i = 3; i < MSVCRT_stream_idx; i++) {
2315 file = msvcrt_get_file(i);
2317 if (file->_tmpfname)
2319 MSVCRT_fclose(file);
2320 num_removed++;
2323 UNLOCK_FILES();
2325 if (num_removed)
2326 TRACE(":removed (%d) temp files\n",num_removed);
2327 return num_removed;
2330 static inline int get_utf8_char_len(char ch)
2332 if((ch&0xf8) == 0xf0)
2333 return 4;
2334 else if((ch&0xf0) == 0xe0)
2335 return 3;
2336 else if((ch&0xe0) == 0xc0)
2337 return 2;
2338 return 1;
2341 /*********************************************************************
2342 * (internal) read_utf8
2344 static int read_utf8(int fd, MSVCRT_wchar_t *buf, unsigned int count)
2346 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2347 HANDLE hand = fdinfo->handle;
2348 char min_buf[4], *readbuf, lookahead;
2349 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2351 /* make the buffer big enough to hold at least one character */
2352 /* read bytes have to fit to output and lookahead buffers */
2353 count /= 2;
2354 readbuf_size = count < 4 ? 4 : count;
2355 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2356 readbuf_size = 4;
2357 readbuf = min_buf;
2360 if(fdinfo->lookahead[0] != '\n') {
2361 readbuf[pos++] = fdinfo->lookahead[0];
2362 fdinfo->lookahead[0] = '\n';
2364 if(fdinfo->lookahead[1] != '\n') {
2365 readbuf[pos++] = fdinfo->lookahead[1];
2366 fdinfo->lookahead[1] = '\n';
2368 if(fdinfo->lookahead[2] != '\n') {
2369 readbuf[pos++] = fdinfo->lookahead[2];
2370 fdinfo->lookahead[2] = '\n';
2375 /* NOTE: this case is broken in native dll, reading
2376 * sometimes fails when small buffer is passed
2378 if(count < 4) {
2379 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2380 if (GetLastError() == ERROR_BROKEN_PIPE) {
2381 fdinfo->wxflag |= WX_ATEOF;
2382 return 0;
2383 }else {
2384 msvcrt_set_errno(GetLastError());
2385 return -1;
2387 }else if(!num_read) {
2388 fdinfo->wxflag |= WX_ATEOF;
2389 return 0;
2390 }else {
2391 pos++;
2394 char_len = get_utf8_char_len(readbuf[0]);
2395 if(char_len>pos) {
2396 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2397 pos += num_read;
2400 if(readbuf[0] == '\n')
2401 fdinfo->wxflag |= WX_READNL;
2402 else
2403 fdinfo->wxflag &= ~WX_READNL;
2405 if(readbuf[0] == 0x1a) {
2406 fdinfo->wxflag |= WX_ATEOF;
2407 return 0;
2410 if(readbuf[0] == '\r') {
2411 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2412 buf[0] = '\r';
2413 else if(lookahead == '\n')
2414 buf[0] = '\n';
2415 else {
2416 buf[0] = '\r';
2417 if(fdinfo->wxflag & (WX_PIPE | WX_NOSEEK))
2418 fdinfo->lookahead[0] = lookahead;
2419 else
2420 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2422 return 2;
2425 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2426 msvcrt_set_errno(GetLastError());
2427 return -1;
2430 return num_read*2;
2433 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2434 if(pos) {
2435 num_read = 0;
2436 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2437 fdinfo->wxflag |= WX_ATEOF;
2438 if (readbuf != min_buf) MSVCRT_free(readbuf);
2439 return 0;
2440 }else {
2441 msvcrt_set_errno(GetLastError());
2442 if (readbuf != min_buf) MSVCRT_free(readbuf);
2443 return -1;
2445 }else if(!pos && !num_read) {
2446 fdinfo->wxflag |= WX_ATEOF;
2447 if (readbuf != min_buf) MSVCRT_free(readbuf);
2448 return 0;
2451 pos += num_read;
2452 if(readbuf[0] == '\n')
2453 fdinfo->wxflag |= WX_READNL;
2454 else
2455 fdinfo->wxflag &= ~WX_READNL;
2457 /* Find first byte of last character (may be incomplete) */
2458 for(i=pos-1; i>0 && i>pos-4; i--)
2459 if((readbuf[i]&0xc0) != 0x80)
2460 break;
2461 char_len = get_utf8_char_len(readbuf[i]);
2462 if(char_len+i <= pos)
2463 i += char_len;
2465 if(fdinfo->wxflag & (WX_PIPE | WX_NOSEEK)) {
2466 if(i < pos)
2467 fdinfo->lookahead[0] = readbuf[i];
2468 if(i+1 < pos)
2469 fdinfo->lookahead[1] = readbuf[i+1];
2470 if(i+2 < pos)
2471 fdinfo->lookahead[2] = readbuf[i+2];
2472 }else if(i < pos) {
2473 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2475 pos = i;
2477 for(i=0, j=0; i<pos; i++) {
2478 if(readbuf[i] == 0x1a) {
2479 fdinfo->wxflag |= WX_ATEOF;
2480 break;
2483 /* strip '\r' if followed by '\n' */
2484 if(readbuf[i] == '\r' && i+1==pos) {
2485 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2486 readbuf[j++] = '\r';
2487 }else if(lookahead == '\n' && j==0) {
2488 readbuf[j++] = '\n';
2489 }else {
2490 if(lookahead != '\n')
2491 readbuf[j++] = '\r';
2493 if(fdinfo->wxflag & (WX_PIPE | WX_NOSEEK))
2494 fdinfo->lookahead[0] = lookahead;
2495 else
2496 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2498 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2499 readbuf[j++] = readbuf[i];
2502 pos = j;
2504 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2505 msvcrt_set_errno(GetLastError());
2506 if (readbuf != min_buf) MSVCRT_free(readbuf);
2507 return -1;
2510 if (readbuf != min_buf) MSVCRT_free(readbuf);
2511 return num_read*2;
2514 /*********************************************************************
2515 * (internal) read_i
2517 * When reading \r as last character in text mode, read() positions
2518 * the file pointer on the \r character while getc() goes on to
2519 * the following \n
2521 static int read_i(int fd, void *buf, unsigned int count)
2523 DWORD num_read, utf16;
2524 char *bufstart = buf;
2525 HANDLE hand = msvcrt_fdtoh(fd);
2526 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2528 if (count == 0)
2529 return 0;
2531 if (fdinfo->wxflag & WX_ATEOF) {
2532 TRACE("already at EOF, returning 0\n");
2533 return 0;
2535 /* Don't trace small reads, it gets *very* annoying */
2536 if (count > 4)
2537 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2538 if (hand == INVALID_HANDLE_VALUE)
2540 *MSVCRT__errno() = MSVCRT_EBADF;
2541 return -1;
2544 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2545 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2547 *MSVCRT__errno() = MSVCRT_EINVAL;
2548 return -1;
2551 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2552 return read_utf8(fd, buf, count);
2554 if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL))
2556 if (fdinfo->lookahead[0] != '\n')
2558 bufstart[0] = fdinfo->lookahead[0];
2559 fdinfo->lookahead[0] = '\n';
2561 if (utf16)
2563 bufstart[1] = fdinfo->lookahead[1];
2564 fdinfo->lookahead[1] = '\n';
2567 if(count>1+utf16 && ReadFile(hand, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2568 num_read += 1+utf16;
2569 else
2570 num_read = 1+utf16;
2573 if(utf16 && (num_read&1))
2575 /* msvcr90 uses uninitialized value from the buffer in this case */
2576 /* msvcrt ignores additional data */
2577 ERR("got odd number of bytes in UTF16 mode\n");
2578 num_read--;
2581 if (count != 0 && num_read == 0)
2583 fdinfo->wxflag |= WX_ATEOF;
2584 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2586 else if (fdinfo->wxflag & WX_TEXT)
2588 DWORD i, j;
2590 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2591 fdinfo->wxflag |= WX_READNL;
2592 else
2593 fdinfo->wxflag &= ~WX_READNL;
2595 for (i=0, j=0; i<num_read; i+=1+utf16)
2597 /* in text mode, a ctrl-z signals EOF */
2598 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2600 fdinfo->wxflag |= WX_ATEOF;
2601 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2602 break;
2605 /* in text mode, strip \r if followed by \n */
2606 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2608 char lookahead[2];
2609 DWORD len;
2611 lookahead[1] = '\n';
2612 if (ReadFile(hand, lookahead, 1+utf16, &len, NULL) && len)
2614 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2616 bufstart[j++] = '\n';
2617 if(utf16) bufstart[j++] = 0;
2619 else
2621 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2623 bufstart[j++] = '\r';
2624 if(utf16) bufstart[j++] = 0;
2627 if (fdinfo->wxflag & (WX_PIPE | WX_NOSEEK))
2629 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2631 bufstart[j++] = '\n';
2632 if (utf16) bufstart[j++] = 0;
2634 else
2636 fdinfo->lookahead[0] = lookahead[0];
2637 fdinfo->lookahead[1] = lookahead[1];
2640 else
2641 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2644 else
2646 bufstart[j++] = '\r';
2647 if(utf16) bufstart[j++] = 0;
2650 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2651 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2653 bufstart[j++] = bufstart[i];
2654 if(utf16) bufstart[j++] = bufstart[i+1];
2657 num_read = j;
2660 else
2662 if (GetLastError() == ERROR_BROKEN_PIPE)
2664 TRACE(":end-of-pipe\n");
2665 fdinfo->wxflag |= WX_ATEOF;
2666 return 0;
2668 else
2670 TRACE(":failed-last error (%d)\n",GetLastError());
2671 return -1;
2675 if (count > 4)
2676 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2677 return num_read;
2680 /*********************************************************************
2681 * _read (MSVCRT.@)
2683 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2685 int num_read;
2686 num_read = read_i(fd, buf, count);
2687 return num_read;
2690 /*********************************************************************
2691 * _setmode (MSVCRT.@)
2693 int CDECL MSVCRT__setmode(int fd,int mode)
2695 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2696 if(ret==MSVCRT__O_TEXT && (msvcrt_get_ioinfo(fd)->exflag & (EF_UTF8|EF_UTF16)))
2697 ret = MSVCRT__O_WTEXT;
2699 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2700 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2701 *MSVCRT__errno() = MSVCRT_EINVAL;
2702 return -1;
2705 if(mode == MSVCRT__O_BINARY) {
2706 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2707 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2708 return ret;
2711 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2712 if(mode == MSVCRT__O_TEXT)
2713 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2714 else if(mode == MSVCRT__O_U8TEXT)
2715 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF16) | EF_UTF8;
2716 else
2717 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF8) | EF_UTF16;
2719 return ret;
2722 /*********************************************************************
2723 * _stat64 (MSVCRT.@)
2725 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2727 DWORD dw;
2728 WIN32_FILE_ATTRIBUTE_DATA hfi;
2729 unsigned short mode = ALL_S_IREAD;
2730 int plen;
2732 TRACE(":file (%s) buf(%p)\n",path,buf);
2734 plen = strlen(path);
2735 while (plen && path[plen-1]==' ')
2736 plen--;
2738 if (plen && (plen<2 || path[plen-2]!=':') &&
2739 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2741 *MSVCRT__errno() = MSVCRT_ENOENT;
2742 return -1;
2745 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2747 TRACE("failed (%d)\n",GetLastError());
2748 *MSVCRT__errno() = MSVCRT_ENOENT;
2749 return -1;
2752 memset(buf,0,sizeof(struct MSVCRT__stat64));
2754 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2755 Bon 011120: This FIXME seems incorrect
2756 Also a letter as first char isn't enough to be classified
2757 as a drive letter
2759 if (isalpha(*path)&& (*(path+1)==':'))
2760 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2761 else
2762 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2764 /* Dir, or regular file? */
2765 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2766 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2767 else
2769 mode |= MSVCRT__S_IFREG;
2770 /* executable? */
2771 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2773 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2774 (tolower(path[plen-3]) << 16);
2775 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2776 mode |= ALL_S_IEXEC;
2780 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2781 mode |= ALL_S_IWRITE;
2783 buf->st_mode = mode;
2784 buf->st_nlink = 1;
2785 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2786 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2787 buf->st_atime = dw;
2788 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2789 buf->st_mtime = buf->st_ctime = dw;
2790 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2791 (int)(buf->st_size >> 32),(int)buf->st_size,
2792 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2793 return 0;
2796 /*********************************************************************
2797 * _stati64 (MSVCRT.@)
2799 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2801 int ret;
2802 struct MSVCRT__stat64 buf64;
2804 ret = MSVCRT_stat64(path, &buf64);
2805 if (!ret)
2806 msvcrt_stat64_to_stati64(&buf64, buf);
2807 return ret;
2810 /*********************************************************************
2811 * _stat (MSVCRT.@)
2813 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2815 int ret;
2816 struct MSVCRT__stat64 buf64;
2818 ret = MSVCRT_stat64( path, &buf64);
2819 if (!ret)
2820 msvcrt_stat64_to_stat(&buf64, buf);
2821 return ret;
2824 /*********************************************************************
2825 * _stat32 (MSVCR100.@)
2827 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
2829 int ret;
2830 struct MSVCRT__stat64 buf64;
2832 ret = MSVCRT_stat64(path, &buf64);
2833 if (!ret)
2834 msvcrt_stat64_to_stat32(&buf64, buf);
2835 return ret;
2838 /*********************************************************************
2839 * _stat32i64 (MSVCR100.@)
2841 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
2843 int ret;
2844 struct MSVCRT__stat64 buf64;
2846 ret = MSVCRT_stat64(path, &buf64);
2847 if (!ret)
2848 msvcrt_stat64_to_stat32i64(&buf64, buf);
2849 return ret;
2852 /*********************************************************************
2853 * _stat64i32 (MSVCR100.@)
2855 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
2857 int ret;
2858 struct MSVCRT__stat64 buf64;
2860 ret = MSVCRT_stat64(path, &buf64);
2861 if (!ret)
2862 msvcrt_stat64_to_stat64i32(&buf64, buf);
2863 return ret;
2866 /*********************************************************************
2867 * _wstat64 (MSVCRT.@)
2869 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2871 DWORD dw;
2872 WIN32_FILE_ATTRIBUTE_DATA hfi;
2873 unsigned short mode = ALL_S_IREAD;
2874 int plen;
2876 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2878 plen = strlenW(path);
2879 while (plen && path[plen-1]==' ')
2880 plen--;
2882 if(plen && (plen<2 || path[plen-2]!=':') &&
2883 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2885 *MSVCRT__errno() = MSVCRT_ENOENT;
2886 return -1;
2889 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2891 TRACE("failed (%d)\n",GetLastError());
2892 *MSVCRT__errno() = MSVCRT_ENOENT;
2893 return -1;
2896 memset(buf,0,sizeof(struct MSVCRT__stat64));
2898 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2899 if (MSVCRT_iswalpha(*path))
2900 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2901 else
2902 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2904 /* Dir, or regular file? */
2905 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2906 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2907 else
2909 mode |= MSVCRT__S_IFREG;
2910 /* executable? */
2911 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2913 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2914 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2915 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2916 mode |= ALL_S_IEXEC;
2920 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2921 mode |= ALL_S_IWRITE;
2923 buf->st_mode = mode;
2924 buf->st_nlink = 1;
2925 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2926 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2927 buf->st_atime = dw;
2928 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2929 buf->st_mtime = buf->st_ctime = dw;
2930 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2931 (int)(buf->st_size >> 32),(int)buf->st_size,
2932 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2933 return 0;
2936 /*********************************************************************
2937 * _wstati64 (MSVCRT.@)
2939 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2941 int ret;
2942 struct MSVCRT__stat64 buf64;
2944 ret = MSVCRT__wstat64(path, &buf64);
2945 if (!ret)
2946 msvcrt_stat64_to_stati64(&buf64, buf);
2947 return ret;
2950 /*********************************************************************
2951 * _wstat (MSVCRT.@)
2953 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2955 int ret;
2956 struct MSVCRT__stat64 buf64;
2958 ret = MSVCRT__wstat64( path, &buf64 );
2959 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2960 return ret;
2963 /*********************************************************************
2964 * _wstat32 (MSVCR100.@)
2966 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
2968 int ret;
2969 struct MSVCRT__stat64 buf64;
2971 ret = MSVCRT__wstat64(path, &buf64);
2972 if (!ret)
2973 msvcrt_stat64_to_stat32(&buf64, buf);
2974 return ret;
2977 /*********************************************************************
2978 * _wstat32i64 (MSVCR100.@)
2980 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
2982 int ret;
2983 struct MSVCRT__stat64 buf64;
2985 ret = MSVCRT__wstat64(path, &buf64);
2986 if (!ret)
2987 msvcrt_stat64_to_stat32i64(&buf64, buf);
2988 return ret;
2991 /*********************************************************************
2992 * _wstat64i32 (MSVCR100.@)
2994 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
2996 int ret;
2997 struct MSVCRT__stat64 buf64;
2999 ret = MSVCRT__wstat64(path, &buf64);
3000 if (!ret)
3001 msvcrt_stat64_to_stat64i32(&buf64, buf);
3002 return ret;
3005 /*********************************************************************
3006 * _tell (MSVCRT.@)
3008 MSVCRT_long CDECL MSVCRT__tell(int fd)
3010 return MSVCRT__lseek(fd, 0, SEEK_CUR);
3013 /*********************************************************************
3014 * _telli64 (MSVCRT.@)
3016 __int64 CDECL _telli64(int fd)
3018 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
3021 /*********************************************************************
3022 * _tempnam (MSVCRT.@)
3024 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3026 char tmpbuf[MAX_PATH];
3027 const char *tmp_dir = MSVCRT_getenv("TMP");
3029 if (tmp_dir) dir = tmp_dir;
3031 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3032 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3034 TRACE("got name (%s)\n",tmpbuf);
3035 DeleteFileA(tmpbuf);
3036 return MSVCRT__strdup(tmpbuf);
3038 TRACE("failed (%d)\n",GetLastError());
3039 return NULL;
3042 /*********************************************************************
3043 * _wtempnam (MSVCRT.@)
3045 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3047 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3048 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3049 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3051 if (tmp_dir) dir = tmp_dir;
3053 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3054 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3056 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3057 DeleteFileW(tmpbuf);
3058 return MSVCRT__wcsdup(tmpbuf);
3060 TRACE("failed (%d)\n",GetLastError());
3061 return NULL;
3064 /*********************************************************************
3065 * _umask (MSVCRT.@)
3067 int CDECL MSVCRT__umask(int umask)
3069 int old_umask = MSVCRT_umask;
3070 TRACE("(%d)\n",umask);
3071 MSVCRT_umask = umask;
3072 return old_umask;
3075 /*********************************************************************
3076 * _utime64 (MSVCRT.@)
3078 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3080 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3082 if (fd > 0)
3084 int retVal = _futime64(fd, t);
3085 MSVCRT__close(fd);
3086 return retVal;
3088 return -1;
3091 /*********************************************************************
3092 * _utime32 (MSVCRT.@)
3094 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3096 if (t)
3098 struct MSVCRT___utimbuf64 t64;
3099 t64.actime = t->actime;
3100 t64.modtime = t->modtime;
3101 return _utime64( path, &t64 );
3103 else
3104 return _utime64( path, NULL );
3107 /*********************************************************************
3108 * _utime (MSVCRT.@)
3110 #ifdef _WIN64
3111 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3113 return _utime64( path, t );
3115 #else
3116 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3118 return _utime32( path, t );
3120 #endif
3122 /*********************************************************************
3123 * _wutime64 (MSVCRT.@)
3125 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3127 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3129 if (fd > 0)
3131 int retVal = _futime64(fd, t);
3132 MSVCRT__close(fd);
3133 return retVal;
3135 return -1;
3138 /*********************************************************************
3139 * _wutime32 (MSVCRT.@)
3141 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3143 if (t)
3145 struct MSVCRT___utimbuf64 t64;
3146 t64.actime = t->actime;
3147 t64.modtime = t->modtime;
3148 return _wutime64( path, &t64 );
3150 else
3151 return _wutime64( path, NULL );
3154 /*********************************************************************
3155 * _wutime (MSVCRT.@)
3157 #ifdef _WIN64
3158 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3160 return _wutime64( path, t );
3162 #else
3163 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3165 return _wutime32( path, t );
3167 #endif
3169 /*********************************************************************
3170 * _write (MSVCRT.@)
3172 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3174 DWORD num_written;
3175 ioinfo *info = msvcrt_get_ioinfo(fd);
3176 HANDLE hand = info->handle;
3178 /* Don't trace small writes, it gets *very* annoying */
3179 #if 0
3180 if (count > 32)
3181 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3182 #endif
3183 if (hand == INVALID_HANDLE_VALUE)
3185 *MSVCRT__errno() = MSVCRT_EBADF;
3186 return -1;
3189 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3191 *MSVCRT__errno() = MSVCRT_EINVAL;
3192 return -1;
3195 /* If appending, go to EOF */
3196 if (info->wxflag & WX_APPEND)
3197 MSVCRT__lseek(fd, 0, FILE_END);
3199 if (!(info->wxflag & WX_TEXT))
3201 if (WriteFile(hand, buf, count, &num_written, NULL)
3202 && (num_written == count))
3203 return num_written;
3204 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3205 hand, GetLastError());
3206 *MSVCRT__errno() = MSVCRT_ENOSPC;
3208 else
3210 unsigned int i, j, nr_lf, size;
3211 char *p = NULL;
3212 const char *q;
3213 const char *s = buf, *buf_start = buf;
3215 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3217 /* find number of \n */
3218 for (nr_lf=0, i=0; i<count; i++)
3219 if (s[i] == '\n')
3220 nr_lf++;
3221 if (nr_lf)
3223 size = count+nr_lf;
3224 if ((q = p = MSVCRT_malloc(size)))
3226 for (s = buf, i = 0, j = 0; i < count; i++)
3228 if (s[i] == '\n')
3229 p[j++] = '\r';
3230 p[j++] = s[i];
3233 else
3235 FIXME("Malloc failed\n");
3236 nr_lf = 0;
3237 size = count;
3238 q = buf;
3241 else
3243 size = count;
3244 q = buf;
3247 else if (info->exflag & EF_UTF16)
3249 for (nr_lf=0, i=0; i<count; i+=2)
3250 if (s[i]=='\n' && s[i+1]==0)
3251 nr_lf += 2;
3252 if (nr_lf)
3254 size = count+nr_lf;
3255 if ((q = p = MSVCRT_malloc(size)))
3257 for (s=buf, i=0, j=0; i<count; i++)
3259 if (s[i]=='\n' && s[i+1]==0)
3261 p[j++] = '\r';
3262 p[j++] = 0;
3264 p[j++] = s[i++];
3265 p[j++] = s[i];
3268 else
3270 FIXME("Malloc failed\n");
3271 nr_lf = 0;
3272 size = count;
3273 q = buf;
3276 else
3278 size = count;
3279 q = buf;
3282 else
3284 DWORD conv_len;
3286 for(nr_lf=0, i=0; i<count; i+=2)
3287 if (s[i]=='\n' && s[i+1]==0)
3288 nr_lf++;
3290 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3291 if(!conv_len) {
3292 msvcrt_set_errno(GetLastError());
3293 MSVCRT_free(p);
3294 return -1;
3297 size = conv_len+nr_lf;
3298 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3300 for (s=buf, i=0, j=0; i<count; i++)
3302 if (s[i]=='\n' && s[i+1]==0)
3304 p[j++] = '\r';
3305 p[j++] = 0;
3307 p[j++] = s[i++];
3308 p[j++] = s[i];
3310 q = p+count+nr_lf*2;
3311 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3312 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3314 else
3316 FIXME("Malloc failed\n");
3317 nr_lf = 0;
3318 size = count;
3319 q = buf;
3323 if (!WriteFile(hand, q, size, &num_written, NULL))
3324 num_written = -1;
3325 if(p)
3326 MSVCRT_free(p);
3327 if (num_written != size)
3329 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3330 fd, hand, GetLastError(), num_written);
3331 *MSVCRT__errno() = MSVCRT_ENOSPC;
3332 return s - buf_start;
3334 return count;
3337 return -1;
3340 /*********************************************************************
3341 * _putw (MSVCRT.@)
3343 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3345 int len;
3347 MSVCRT__lock_file(file);
3348 len = MSVCRT__write(file->_file, &val, sizeof(val));
3349 if (len == sizeof(val)) {
3350 MSVCRT__unlock_file(file);
3351 return val;
3354 file->_flag |= MSVCRT__IOERR;
3355 MSVCRT__unlock_file(file);
3356 return MSVCRT_EOF;
3359 /*********************************************************************
3360 * fclose (MSVCRT.@)
3362 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3364 int r, flag;
3366 MSVCRT__lock_file(file);
3367 flag = file->_flag;
3368 MSVCRT_free(file->_tmpfname);
3369 file->_tmpfname = NULL;
3370 /* flush stdio buffers */
3371 if(file->_flag & MSVCRT__IOWRT)
3372 MSVCRT_fflush(file);
3373 if(file->_flag & MSVCRT__IOMYBUF)
3374 MSVCRT_free(file->_base);
3376 r=MSVCRT__close(file->_file);
3378 file->_flag = 0;
3379 MSVCRT__unlock_file(file);
3381 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3384 /*********************************************************************
3385 * feof (MSVCRT.@)
3387 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3389 return file->_flag & MSVCRT__IOEOF;
3392 /*********************************************************************
3393 * ferror (MSVCRT.@)
3395 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3397 return file->_flag & MSVCRT__IOERR;
3400 /*********************************************************************
3401 * _filbuf (MSVCRT.@)
3403 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3405 unsigned char c;
3406 MSVCRT__lock_file(file);
3408 if(file->_flag & MSVCRT__IOSTRG) {
3409 MSVCRT__unlock_file(file);
3410 return MSVCRT_EOF;
3413 /* Allocate buffer if needed */
3414 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
3415 msvcrt_alloc_buffer(file);
3417 if(!(file->_flag & MSVCRT__IOREAD)) {
3418 if(file->_flag & MSVCRT__IORW)
3419 file->_flag |= MSVCRT__IOREAD;
3420 else {
3421 MSVCRT__unlock_file(file);
3422 return MSVCRT_EOF;
3426 if(file->_flag & MSVCRT__IONBF) {
3427 int r;
3428 if ((r = read_i(file->_file,&c,1)) != 1) {
3429 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3430 MSVCRT__unlock_file(file);
3431 return MSVCRT_EOF;
3434 MSVCRT__unlock_file(file);
3435 return c;
3436 } else {
3437 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
3438 if(file->_cnt<=0) {
3439 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3440 file->_cnt = 0;
3441 MSVCRT__unlock_file(file);
3442 return MSVCRT_EOF;
3445 file->_cnt--;
3446 file->_ptr = file->_base+1;
3447 c = *(unsigned char *)file->_base;
3448 MSVCRT__unlock_file(file);
3449 return c;
3453 /*********************************************************************
3454 * fgetc (MSVCRT.@)
3456 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3458 unsigned char *i;
3459 unsigned int j;
3461 MSVCRT__lock_file(file);
3462 if (file->_cnt>0) {
3463 file->_cnt--;
3464 i = (unsigned char *)file->_ptr++;
3465 j = *i;
3466 } else
3467 j = MSVCRT__filbuf(file);
3469 MSVCRT__unlock_file(file);
3470 return j;
3473 /*********************************************************************
3474 * _fgetchar (MSVCRT.@)
3476 int CDECL MSVCRT__fgetchar(void)
3478 return MSVCRT_fgetc(MSVCRT_stdin);
3481 /*********************************************************************
3482 * fgets (MSVCRT.@)
3484 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3486 int cc = MSVCRT_EOF;
3487 char * buf_start = s;
3489 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3490 file,file->_file,s,size);
3492 MSVCRT__lock_file(file);
3494 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
3496 *s++ = (char)cc;
3497 size --;
3499 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3501 TRACE(":nothing read\n");
3502 MSVCRT__unlock_file(file);
3503 return NULL;
3505 if ((cc != MSVCRT_EOF) && (size > 1))
3506 *s++ = cc;
3507 *s = '\0';
3508 TRACE(":got %s\n", debugstr_a(buf_start));
3509 MSVCRT__unlock_file(file);
3510 return buf_start;
3513 /*********************************************************************
3514 * fgetwc (MSVCRT.@)
3516 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3518 MSVCRT_wint_t ret;
3519 int ch;
3521 MSVCRT__lock_file(file);
3523 if((msvcrt_get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3524 || !(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3525 char *p;
3527 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3528 ch = MSVCRT_fgetc(file);
3529 if(ch == MSVCRT_EOF) {
3530 ret = MSVCRT_WEOF;
3531 break;
3533 *p = (char)ch;
3535 }else {
3536 char mbs[MSVCRT_MB_LEN_MAX];
3537 int len = 0;
3539 ch = MSVCRT_fgetc(file);
3540 if(ch != MSVCRT_EOF) {
3541 mbs[0] = (char)ch;
3542 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3543 ch = MSVCRT_fgetc(file);
3544 if(ch != MSVCRT_EOF) {
3545 mbs[1] = (char)ch;
3546 len = 2;
3548 }else {
3549 len = 1;
3553 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3554 ret = MSVCRT_WEOF;
3557 MSVCRT__unlock_file(file);
3558 return ret;
3561 /*********************************************************************
3562 * _getw (MSVCRT.@)
3564 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3566 char *ch;
3567 int i, k;
3568 unsigned int j;
3569 ch = (char *)&i;
3571 MSVCRT__lock_file(file);
3572 for (j=0; j<sizeof(int); j++) {
3573 k = MSVCRT_fgetc(file);
3574 if (k == MSVCRT_EOF) {
3575 file->_flag |= MSVCRT__IOEOF;
3576 MSVCRT__unlock_file(file);
3577 return EOF;
3579 ch[j] = k;
3582 MSVCRT__unlock_file(file);
3583 return i;
3586 /*********************************************************************
3587 * getwc (MSVCRT.@)
3589 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3591 return MSVCRT_fgetwc(file);
3594 /*********************************************************************
3595 * _fgetwchar (MSVCRT.@)
3597 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3599 return MSVCRT_fgetwc(MSVCRT_stdin);
3602 /*********************************************************************
3603 * getwchar (MSVCRT.@)
3605 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3607 return MSVCRT__fgetwchar();
3610 /*********************************************************************
3611 * fgetws (MSVCRT.@)
3613 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3615 MSVCRT_wint_t cc = MSVCRT_WEOF;
3616 MSVCRT_wchar_t * buf_start = s;
3618 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3619 file,file->_file,s,size);
3621 MSVCRT__lock_file(file);
3623 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
3625 *s++ = cc;
3626 size --;
3628 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3630 TRACE(":nothing read\n");
3631 MSVCRT__unlock_file(file);
3632 return NULL;
3634 if ((cc != MSVCRT_WEOF) && (size > 1))
3635 *s++ = cc;
3636 *s = 0;
3637 TRACE(":got %s\n", debugstr_w(buf_start));
3638 MSVCRT__unlock_file(file);
3639 return buf_start;
3642 /*********************************************************************
3643 * _flsbuf (MSVCRT.@)
3645 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3647 /* Flush output buffer */
3648 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3649 msvcrt_alloc_buffer(file);
3651 if(!(file->_flag & MSVCRT__IOWRT)) {
3652 if(file->_flag & MSVCRT__IORW)
3653 file->_flag |= MSVCRT__IOWRT;
3654 else
3655 return MSVCRT_EOF;
3657 if(file->_bufsiz) {
3658 int res = 0;
3660 if(file->_cnt <= 0)
3661 res = msvcrt_flush_buffer(file);
3662 if(!res) {
3663 *file->_ptr++ = c;
3664 file->_cnt--;
3667 return res ? res : c&0xff;
3668 } else {
3669 unsigned char cc=c;
3670 int len;
3671 /* set _cnt to 0 for unbuffered FILEs */
3672 file->_cnt = 0;
3673 len = MSVCRT__write(file->_file, &cc, 1);
3674 if (len == 1)
3675 return c & 0xff;
3676 file->_flag |= MSVCRT__IOERR;
3677 return MSVCRT_EOF;
3681 /*********************************************************************
3682 * fwrite (MSVCRT.@)
3684 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3686 MSVCRT_size_t wrcnt=size * nmemb;
3687 int written = 0;
3688 if (size == 0)
3689 return 0;
3691 MSVCRT__lock_file(file);
3693 while(wrcnt) {
3694 if(file->_cnt) {
3695 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3696 memcpy(file->_ptr, ptr, pcnt);
3697 file->_cnt -= pcnt;
3698 file->_ptr += pcnt;
3699 written += pcnt;
3700 wrcnt -= pcnt;
3701 ptr = (const char*)ptr + pcnt;
3702 } else if(!file->_bufsiz && (file->_flag & MSVCRT__IONBF)) {
3703 if(!(file->_flag & MSVCRT__IOWRT)) {
3704 if(file->_flag & MSVCRT__IORW)
3705 file->_flag |= MSVCRT__IOWRT;
3706 else
3707 break;
3710 if(MSVCRT__write(file->_file, ptr, wrcnt) <= 0) {
3711 file->_flag |= MSVCRT__IOERR;
3712 break;
3714 written += wrcnt;
3715 wrcnt = 0;
3716 } else {
3717 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3718 break;
3719 written++;
3720 wrcnt--;
3721 ptr = (const char*)ptr + 1;
3725 MSVCRT__unlock_file(file);
3726 return written / size;
3729 /*********************************************************************
3730 * fputwc (MSVCRT.@)
3732 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3734 MSVCRT_wchar_t mwc=wc;
3735 ioinfo *fdinfo;
3736 MSVCRT_wint_t ret;
3738 MSVCRT__lock_file(file);
3739 fdinfo = msvcrt_get_ioinfo(file->_file);
3741 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3742 char buf[MSVCRT_MB_LEN_MAX];
3743 int char_len;
3745 char_len = MSVCRT_wctomb(buf, mwc);
3746 if(char_len!=-1 && MSVCRT_fwrite(buf, char_len, 1, file)==1)
3747 ret = wc;
3748 else
3749 ret = MSVCRT_WEOF;
3750 }else if(MSVCRT_fwrite(&mwc, sizeof(mwc), 1, file) == 1) {
3751 ret = wc;
3752 }else {
3753 ret = MSVCRT_WEOF;
3756 MSVCRT__unlock_file(file);
3757 return ret;
3760 /*********************************************************************
3761 * _fputwchar (MSVCRT.@)
3763 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3765 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3768 /*********************************************************************
3769 * _wfsopen (MSVCRT.@)
3771 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
3773 MSVCRT_FILE* file;
3774 int open_flags, stream_flags, fd;
3776 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3778 /* map mode string to open() flags. "man fopen" for possibilities. */
3779 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3780 return NULL;
3782 LOCK_FILES();
3783 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3784 if (fd < 0)
3785 file = NULL;
3786 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3787 != -1)
3788 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3789 else if (file)
3791 file->_flag = 0;
3792 file = NULL;
3795 TRACE(":got (%p)\n",file);
3796 if (fd >= 0 && !file)
3797 MSVCRT__close(fd);
3798 UNLOCK_FILES();
3799 return file;
3802 /*********************************************************************
3803 * _fsopen (MSVCRT.@)
3805 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3807 MSVCRT_FILE *ret;
3808 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3810 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3811 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3812 *MSVCRT__errno() = MSVCRT_EINVAL;
3813 return NULL;
3815 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3817 MSVCRT_free(pathW);
3818 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3819 *MSVCRT__errno() = MSVCRT_EINVAL;
3820 return NULL;
3823 ret = MSVCRT__wfsopen(pathW, modeW, share);
3825 MSVCRT_free(pathW);
3826 MSVCRT_free(modeW);
3827 return ret;
3830 /*********************************************************************
3831 * fopen (MSVCRT.@)
3833 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3835 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3838 /*********************************************************************
3839 * fopen_s (MSVCRT.@)
3841 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3842 const char *filename, const char *mode)
3844 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3845 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3846 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3848 *pFile = MSVCRT_fopen(filename, mode);
3850 if(!*pFile)
3851 return *MSVCRT__errno();
3852 return 0;
3855 /*********************************************************************
3856 * _wfopen (MSVCRT.@)
3858 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3860 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3863 /*********************************************************************
3864 * _wfopen_s (MSVCRT.@)
3866 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3867 const MSVCRT_wchar_t *mode)
3869 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3870 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3871 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3873 *pFile = MSVCRT__wfopen(filename, mode);
3875 if(!*pFile)
3876 return *MSVCRT__errno();
3877 return 0;
3880 /*********************************************************************
3881 * fputc (MSVCRT.@)
3883 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3885 int res;
3887 MSVCRT__lock_file(file);
3888 if(file->_cnt>0) {
3889 *file->_ptr++=c;
3890 file->_cnt--;
3891 if (c == '\n')
3893 res = msvcrt_flush_buffer(file);
3894 MSVCRT__unlock_file(file);
3895 return res ? res : c;
3897 else {
3898 MSVCRT__unlock_file(file);
3899 return c & 0xff;
3901 } else {
3902 res = MSVCRT__flsbuf(c, file);
3903 MSVCRT__unlock_file(file);
3904 return res;
3908 /*********************************************************************
3909 * _fputchar (MSVCRT.@)
3911 int CDECL MSVCRT__fputchar(int c)
3913 return MSVCRT_fputc(c, MSVCRT_stdout);
3916 /*********************************************************************
3917 * fread (MSVCRT.@)
3919 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3921 MSVCRT_size_t rcnt=size * nmemb;
3922 MSVCRT_size_t read=0;
3923 int pread=0;
3925 if(!rcnt)
3926 return 0;
3928 MSVCRT__lock_file(file);
3930 /* first buffered data */
3931 if(file->_cnt>0) {
3932 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3933 memcpy(ptr, file->_ptr, pcnt);
3934 file->_cnt -= pcnt;
3935 file->_ptr += pcnt;
3936 read += pcnt ;
3937 rcnt -= pcnt ;
3938 ptr = (char*)ptr + pcnt;
3939 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3940 if(file->_flag & MSVCRT__IORW) {
3941 file->_flag |= MSVCRT__IOREAD;
3942 } else {
3943 MSVCRT__unlock_file(file);
3944 return 0;
3947 while(rcnt>0)
3949 int i;
3950 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && !(file->_flag & MSVCRT__IONBF)
3951 && (file->_bufsiz != 0 || msvcrt_alloc_buffer(file))) {
3952 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3953 file->_ptr = file->_base;
3954 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3955 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3956 if (i > 0 && i < file->_cnt) {
3957 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3958 file->_flag &= ~MSVCRT__IOEOF;
3960 if (i > 0) {
3961 memcpy(ptr, file->_ptr, i);
3962 file->_cnt -= i;
3963 file->_ptr += i;
3965 } else if (rcnt > UINT_MAX) {
3966 i = MSVCRT__read(file->_file, ptr, UINT_MAX);
3967 } else if (rcnt < MSVCRT_BUFSIZ) {
3968 i = MSVCRT__read(file->_file, ptr, rcnt);
3969 } else {
3970 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
3972 pread += i;
3973 rcnt -= i;
3974 ptr = (char *)ptr+i;
3975 /* expose feof condition in the flags
3976 * MFC tests file->_flag for feof, and doesn't call feof())
3978 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3979 file->_flag |= MSVCRT__IOEOF;
3980 else if (i == -1)
3982 file->_flag |= MSVCRT__IOERR;
3983 pread = 0;
3984 rcnt = 0;
3986 if (i < 1) break;
3988 read+=pread;
3989 MSVCRT__unlock_file(file);
3990 return read / size;
3994 /*********************************************************************
3995 * fread_s (MSVCR80.@)
3997 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
3998 MSVCRT_size_t count, MSVCRT_FILE *stream)
4000 size_t bytes_left, buf_pos;
4002 TRACE("(%p %lu %lu %lu %p\n", buf, buf_size, elem_size, count, stream);
4004 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4005 if(buf && buf_size)
4006 memset(buf, 0, buf_size);
4007 return 0;
4009 if(!elem_size || !count) return 0;
4010 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4011 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4013 bytes_left = elem_size*count;
4014 buf_pos = 0;
4015 while(bytes_left) {
4016 if(stream->_cnt > 0) {
4017 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4019 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4020 memset(buf, 0, buf_size);
4021 return 0;
4024 MSVCRT_fread((char*)buf+buf_pos, 1, size, stream);
4025 buf_pos += size;
4026 bytes_left -= size;
4027 }else {
4028 int c = MSVCRT__filbuf(stream);
4030 if(c == EOF)
4031 break;
4033 if(!MSVCRT_CHECK_PMT_ERR(buf_size-buf_pos > 0, MSVCRT_ERANGE)) {
4034 memset(buf, 0, buf_size);
4035 return 0;
4038 ((char*)buf)[buf_pos++] = c;
4039 bytes_left--;
4043 return buf_pos/elem_size;
4046 /*********************************************************************
4047 * _wfreopen (MSVCRT.@)
4050 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4052 int open_flags, stream_flags, fd;
4054 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4056 LOCK_FILES();
4057 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
4058 file = NULL;
4059 else
4061 MSVCRT_fclose(file);
4062 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4063 file = NULL;
4064 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4065 file = NULL;
4066 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4068 file->_flag = 0;
4069 file = NULL;
4072 UNLOCK_FILES();
4073 return file;
4076 /*********************************************************************
4077 * _wfreopen_s (MSVCRT.@)
4079 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4080 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4082 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4083 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4084 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4085 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4087 *pFile = MSVCRT__wfreopen(path, mode, file);
4089 if(!*pFile)
4090 return *MSVCRT__errno();
4091 return 0;
4094 /*********************************************************************
4095 * freopen (MSVCRT.@)
4098 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4100 MSVCRT_FILE *ret;
4101 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4103 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4104 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4106 MSVCRT_free(pathW);
4107 return NULL;
4110 ret = MSVCRT__wfreopen(pathW, modeW, file);
4112 MSVCRT_free(pathW);
4113 MSVCRT_free(modeW);
4114 return ret;
4117 /*********************************************************************
4118 * freopen_s (MSVCRT.@)
4120 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4121 const char *path, const char *mode, MSVCRT_FILE* file)
4123 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4124 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4125 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4126 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4128 *pFile = MSVCRT_freopen(path, mode, file);
4130 if(!*pFile)
4131 return *MSVCRT__errno();
4132 return 0;
4135 /*********************************************************************
4136 * fsetpos (MSVCRT.@)
4138 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4140 int ret;
4142 MSVCRT__lock_file(file);
4143 /* Note that all this has been lifted 'as is' from fseek */
4144 if(file->_flag & MSVCRT__IOWRT)
4145 msvcrt_flush_buffer(file);
4147 /* Discard buffered input */
4148 file->_cnt = 0;
4149 file->_ptr = file->_base;
4151 /* Reset direction of i/o */
4152 if(file->_flag & MSVCRT__IORW) {
4153 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4156 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
4157 MSVCRT__unlock_file(file);
4158 return ret;
4161 /*********************************************************************
4162 * _ftelli64 (MSVCRT.@)
4164 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4166 __int64 pos;
4168 MSVCRT__lock_file(file);
4169 pos = _telli64(file->_file);
4170 if(pos == -1) {
4171 MSVCRT__unlock_file(file);
4172 return -1;
4174 if(file->_bufsiz) {
4175 if(file->_flag & MSVCRT__IOWRT) {
4176 pos += file->_ptr - file->_base;
4178 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4179 char *p;
4181 for(p=file->_base; p<file->_ptr; p++)
4182 if(*p == '\n')
4183 pos++;
4185 } else if(!file->_cnt) { /* nothing to do */
4186 } else if(MSVCRT__lseeki64(file->_file, 0, SEEK_END)==pos) {
4187 int i;
4189 pos -= file->_cnt;
4190 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4191 for(i=0; i<file->_cnt; i++)
4192 if(file->_ptr[i] == '\n')
4193 pos--;
4195 } else {
4196 char *p;
4198 if(MSVCRT__lseeki64(file->_file, pos, SEEK_SET) != pos) {
4199 MSVCRT__unlock_file(file);
4200 return -1;
4203 pos -= file->_bufsiz;
4204 pos += file->_ptr - file->_base;
4206 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4207 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READNL)
4208 pos--;
4210 for(p=file->_base; p<file->_ptr; p++)
4211 if(*p == '\n')
4212 pos++;
4217 MSVCRT__unlock_file(file);
4218 return pos;
4221 /*********************************************************************
4222 * ftell (MSVCRT.@)
4224 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4226 return MSVCRT__ftelli64(file);
4229 /*********************************************************************
4230 * fgetpos (MSVCRT.@)
4232 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4234 *pos = MSVCRT__ftelli64(file);
4235 if(*pos == -1)
4236 return -1;
4237 return 0;
4240 /*********************************************************************
4241 * fputs (MSVCRT.@)
4243 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4245 MSVCRT_size_t len = strlen(s);
4246 int ret;
4248 MSVCRT__lock_file(file);
4249 ret = MSVCRT_fwrite(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4250 MSVCRT__unlock_file(file);
4251 return ret;
4254 /*********************************************************************
4255 * fputws (MSVCRT.@)
4257 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4259 MSVCRT_size_t i, len = strlenW(s);
4260 BOOL tmp_buf;
4261 int ret;
4263 MSVCRT__lock_file(file);
4264 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
4265 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4266 MSVCRT__unlock_file(file);
4267 return ret;
4270 tmp_buf = add_std_buffer(file);
4271 for (i=0; i<len; i++) {
4272 if(MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
4273 if(tmp_buf) remove_std_buffer(file);
4274 MSVCRT__unlock_file(file);
4275 return MSVCRT_WEOF;
4279 if(tmp_buf) remove_std_buffer(file);
4280 MSVCRT__unlock_file(file);
4281 return 0;
4284 /*********************************************************************
4285 * getchar (MSVCRT.@)
4287 int CDECL MSVCRT_getchar(void)
4289 return MSVCRT_fgetc(MSVCRT_stdin);
4292 /*********************************************************************
4293 * getc (MSVCRT.@)
4295 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4297 return MSVCRT_fgetc(file);
4300 /*********************************************************************
4301 * gets (MSVCRT.@)
4303 char * CDECL MSVCRT_gets(char *buf)
4305 int cc;
4306 char * buf_start = buf;
4308 MSVCRT__lock_file(MSVCRT_stdin);
4309 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4310 cc = MSVCRT_fgetc(MSVCRT_stdin))
4311 if(cc != '\r') *buf++ = (char)cc;
4313 *buf = '\0';
4315 TRACE("got '%s'\n", buf_start);
4316 MSVCRT__unlock_file(MSVCRT_stdin);
4317 return buf_start;
4320 /*********************************************************************
4321 * _getws (MSVCRT.@)
4323 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4325 MSVCRT_wint_t cc;
4326 MSVCRT_wchar_t* ws = buf;
4328 MSVCRT__lock_file(MSVCRT_stdin);
4329 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4330 cc = MSVCRT_fgetwc(MSVCRT_stdin))
4332 if (cc != '\r')
4333 *buf++ = (MSVCRT_wchar_t)cc;
4335 *buf = '\0';
4337 TRACE("got %s\n", debugstr_w(ws));
4338 MSVCRT__unlock_file(MSVCRT_stdin);
4339 return ws;
4342 /*********************************************************************
4343 * putc (MSVCRT.@)
4345 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4347 return MSVCRT_fputc(c, file);
4350 /*********************************************************************
4351 * putchar (MSVCRT.@)
4353 int CDECL MSVCRT_putchar(int c)
4355 return MSVCRT_fputc(c, MSVCRT_stdout);
4358 /*********************************************************************
4359 * _putwch (MSVCRT.@)
4361 int CDECL MSVCRT__putwch(int c)
4363 return MSVCRT_fputwc(c, MSVCRT_stdout);
4366 /*********************************************************************
4367 * puts (MSVCRT.@)
4369 int CDECL MSVCRT_puts(const char *s)
4371 MSVCRT_size_t len = strlen(s);
4372 int ret;
4374 MSVCRT__lock_file(MSVCRT_stdout);
4375 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4376 MSVCRT__unlock_file(MSVCRT_stdout);
4377 return MSVCRT_EOF;
4380 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4381 MSVCRT__unlock_file(MSVCRT_stdout);
4382 return ret;
4385 /*********************************************************************
4386 * _putws (MSVCRT.@)
4388 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4390 static const MSVCRT_wchar_t nl = '\n';
4391 MSVCRT_size_t len = strlenW(s);
4392 int ret;
4394 MSVCRT__lock_file(MSVCRT_stdout);
4395 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4396 MSVCRT__unlock_file(MSVCRT_stdout);
4397 return MSVCRT_EOF;
4400 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4401 MSVCRT__unlock_file(MSVCRT_stdout);
4402 return ret;
4405 /*********************************************************************
4406 * remove (MSVCRT.@)
4408 int CDECL MSVCRT_remove(const char *path)
4410 TRACE("(%s)\n",path);
4411 if (DeleteFileA(path))
4412 return 0;
4413 TRACE(":failed (%d)\n",GetLastError());
4414 msvcrt_set_errno(GetLastError());
4415 return -1;
4418 /*********************************************************************
4419 * _wremove (MSVCRT.@)
4421 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4423 TRACE("(%s)\n",debugstr_w(path));
4424 if (DeleteFileW(path))
4425 return 0;
4426 TRACE(":failed (%d)\n",GetLastError());
4427 msvcrt_set_errno(GetLastError());
4428 return -1;
4431 /*********************************************************************
4432 * rename (MSVCRT.@)
4434 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4436 TRACE(":from %s to %s\n",oldpath,newpath);
4437 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4438 return 0;
4439 TRACE(":failed (%d)\n",GetLastError());
4440 msvcrt_set_errno(GetLastError());
4441 return -1;
4444 /*********************************************************************
4445 * _wrename (MSVCRT.@)
4447 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4449 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4450 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4451 return 0;
4452 TRACE(":failed (%d)\n",GetLastError());
4453 msvcrt_set_errno(GetLastError());
4454 return -1;
4457 /*********************************************************************
4458 * setvbuf (MSVCRT.@)
4460 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4462 MSVCRT__lock_file(file);
4463 if(file->_bufsiz) {
4464 MSVCRT_free(file->_base);
4465 file->_bufsiz = 0;
4466 file->_cnt = 0;
4468 if(mode == MSVCRT__IOFBF) {
4469 file->_flag &= ~MSVCRT__IONBF;
4470 file->_base = file->_ptr = buf;
4471 if(buf) {
4472 file->_bufsiz = size;
4474 } else {
4475 file->_flag |= MSVCRT__IONBF;
4477 MSVCRT__unlock_file(file);
4478 return 0;
4481 /*********************************************************************
4482 * setbuf (MSVCRT.@)
4484 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4486 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4489 /*********************************************************************
4490 * tmpnam (MSVCRT.@)
4492 char * CDECL MSVCRT_tmpnam(char *s)
4494 char tmpstr[16];
4495 char *p;
4496 int count, size;
4498 if (!s) {
4499 thread_data_t *data = msvcrt_get_thread_data();
4501 if(!data->tmpnam_buffer)
4502 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4504 s = data->tmpnam_buffer;
4507 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4508 p = s + sprintf(s, "\\s%s.", tmpstr);
4509 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4511 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
4512 memcpy(p, tmpstr, size);
4513 p[size] = '\0';
4514 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4515 GetLastError() == ERROR_FILE_NOT_FOUND)
4516 break;
4518 return s;
4521 /*********************************************************************
4522 * _wtmpnam (MSVCRT.@)
4524 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
4526 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
4527 MSVCRT_wchar_t tmpstr[16];
4528 MSVCRT_wchar_t *p;
4529 int count, size;
4530 if (!s) {
4531 thread_data_t *data = msvcrt_get_thread_data();
4533 if(!data->wtmpnam_buffer)
4534 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4536 s = data->wtmpnam_buffer;
4539 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4540 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
4541 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4543 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
4544 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
4545 p[size] = '\0';
4546 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4547 GetLastError() == ERROR_FILE_NOT_FOUND)
4548 break;
4550 return s;
4553 /*********************************************************************
4554 * tmpfile (MSVCRT.@)
4556 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4558 char *filename = MSVCRT_tmpnam(NULL);
4559 int fd;
4560 MSVCRT_FILE* file = NULL;
4562 LOCK_FILES();
4563 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4564 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4565 if (fd != -1 && (file = msvcrt_alloc_fp()))
4567 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4569 file->_flag = 0;
4570 file = NULL;
4572 else file->_tmpfname = MSVCRT__strdup(filename);
4575 if(fd != -1 && !file)
4576 MSVCRT__close(fd);
4577 UNLOCK_FILES();
4578 return file;
4581 /*********************************************************************
4582 * tmpfile_s (MSVCRT.@)
4584 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4586 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4588 *file = MSVCRT_tmpfile();
4589 return 0;
4592 static int puts_clbk_file_a(void *file, int len, const char *str)
4594 return MSVCRT_fwrite(str, sizeof(char), len, file);
4597 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4599 int i, ret;
4601 MSVCRT__lock_file(file);
4603 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4604 ret = MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
4605 MSVCRT__unlock_file(file);
4606 return ret;
4609 for(i=0; i<len; i++) {
4610 if(MSVCRT_fputwc(str[i], file) == MSVCRT_WEOF) {
4611 MSVCRT__unlock_file(file);
4612 return -1;
4616 MSVCRT__unlock_file(file);
4617 return len;
4620 /*********************************************************************
4621 * vfprintf (MSVCRT.@)
4623 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4625 BOOL tmp_buf;
4626 int ret;
4628 MSVCRT__lock_file(file);
4629 tmp_buf = add_std_buffer(file);
4630 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4631 if(tmp_buf) remove_std_buffer(file);
4632 MSVCRT__unlock_file(file);
4634 return ret;
4637 /*********************************************************************
4638 * vfprintf_s (MSVCRT.@)
4640 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4642 BOOL tmp_buf;
4643 int ret;
4645 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4647 MSVCRT__lock_file(file);
4648 tmp_buf = add_std_buffer(file);
4649 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4650 if(tmp_buf) remove_std_buffer(file);
4651 MSVCRT__unlock_file(file);
4653 return ret;
4656 /*********************************************************************
4657 * vfwprintf (MSVCRT.@)
4659 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4661 BOOL tmp_buf;
4662 int ret;
4664 MSVCRT__lock_file(file);
4665 tmp_buf = add_std_buffer(file);
4666 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4667 if(tmp_buf) remove_std_buffer(file);
4668 MSVCRT__unlock_file(file);
4670 return ret;
4673 /*********************************************************************
4674 * vfwprintf_s (MSVCRT.@)
4676 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4678 BOOL tmp_buf;
4679 int ret;
4681 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4683 MSVCRT__lock_file(file);
4684 tmp_buf = add_std_buffer(file);
4685 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4686 if(tmp_buf) remove_std_buffer(file);
4687 MSVCRT__unlock_file(file);
4689 return ret;
4692 /*********************************************************************
4693 * _vfwprintf_l (MSVCRT.@)
4695 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
4696 MSVCRT__locale_t locale, __ms_va_list valist)
4698 BOOL tmp_buf;
4699 int ret;
4701 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4703 MSVCRT__lock_file(file);
4704 tmp_buf = add_std_buffer(file);
4705 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4706 if(tmp_buf) remove_std_buffer(file);
4707 MSVCRT__unlock_file(file);
4709 return ret;
4712 /*********************************************************************
4713 * vprintf (MSVCRT.@)
4715 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
4717 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
4720 /*********************************************************************
4721 * vprintf_s (MSVCRT.@)
4723 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
4725 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
4728 /*********************************************************************
4729 * vwprintf (MSVCRT.@)
4731 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
4733 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
4736 /*********************************************************************
4737 * vwprintf_s (MSVCRT.@)
4739 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
4741 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
4744 /*********************************************************************
4745 * fprintf (MSVCRT.@)
4747 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
4749 __ms_va_list valist;
4750 int res;
4751 __ms_va_start(valist, format);
4752 res = MSVCRT_vfprintf(file, format, valist);
4753 __ms_va_end(valist);
4754 return res;
4757 /*********************************************************************
4758 * fprintf_s (MSVCRT.@)
4760 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
4762 __ms_va_list valist;
4763 int res;
4764 __ms_va_start(valist, format);
4765 res = MSVCRT_vfprintf_s(file, format, valist);
4766 __ms_va_end(valist);
4767 return res;
4770 /*********************************************************************
4771 * fwprintf (MSVCRT.@)
4773 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4775 __ms_va_list valist;
4776 int res;
4777 __ms_va_start(valist, format);
4778 res = MSVCRT_vfwprintf(file, format, valist);
4779 __ms_va_end(valist);
4780 return res;
4783 /*********************************************************************
4784 * fwprintf_s (MSVCRT.@)
4786 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4788 __ms_va_list valist;
4789 int res;
4790 __ms_va_start(valist, format);
4791 res = MSVCRT_vfwprintf_s(file, format, valist);
4792 __ms_va_end(valist);
4793 return res;
4796 /*********************************************************************
4797 * _fwprintf_l (MSVCRT.@)
4799 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
4801 __ms_va_list valist;
4802 int res;
4803 __ms_va_start(valist, locale);
4804 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
4805 __ms_va_end(valist);
4806 return res;
4809 /*********************************************************************
4810 * printf (MSVCRT.@)
4812 int CDECL MSVCRT_printf(const char *format, ...)
4814 __ms_va_list valist;
4815 int res;
4816 __ms_va_start(valist, format);
4817 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
4818 __ms_va_end(valist);
4819 return res;
4822 /*********************************************************************
4823 * printf_s (MSVCRT.@)
4825 int CDECL MSVCRT_printf_s(const char *format, ...)
4827 __ms_va_list valist;
4828 int res;
4829 __ms_va_start(valist, format);
4830 res = MSVCRT_vprintf_s(format, valist);
4831 __ms_va_end(valist);
4832 return res;
4835 /*********************************************************************
4836 * ungetc (MSVCRT.@)
4838 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
4840 if (c == MSVCRT_EOF)
4841 return MSVCRT_EOF;
4843 MSVCRT__lock_file(file);
4844 if(file->_bufsiz == 0 && msvcrt_alloc_buffer(file))
4845 file->_ptr++;
4846 if(file->_ptr>file->_base) {
4847 file->_ptr--;
4848 if(file->_flag & MSVCRT__IOSTRG) {
4849 if(*file->_ptr != c) {
4850 file->_ptr++;
4851 MSVCRT__unlock_file(file);
4852 return MSVCRT_EOF;
4854 }else {
4855 *file->_ptr = c;
4857 file->_cnt++;
4858 MSVCRT_clearerr(file);
4859 MSVCRT__unlock_file(file);
4860 return c;
4863 MSVCRT__unlock_file(file);
4864 return MSVCRT_EOF;
4867 /*********************************************************************
4868 * ungetwc (MSVCRT.@)
4870 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
4872 MSVCRT_wchar_t mwc = wc;
4874 if (wc == MSVCRT_WEOF)
4875 return MSVCRT_WEOF;
4877 MSVCRT__lock_file(file);
4879 if((msvcrt_get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
4880 || !(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
4881 unsigned char * pp = (unsigned char *)&mwc;
4882 int i;
4884 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
4885 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
4886 MSVCRT__unlock_file(file);
4887 return MSVCRT_WEOF;
4890 }else {
4891 char mbs[MSVCRT_MB_LEN_MAX];
4892 int len;
4894 len = MSVCRT_wctomb(mbs, mwc);
4895 if(len == -1) {
4896 MSVCRT__unlock_file(file);
4897 return MSVCRT_WEOF;
4900 for(len--; len>=0; len--) {
4901 if(mbs[len] != MSVCRT_ungetc(mbs[len], file)) {
4902 MSVCRT__unlock_file(file);
4903 return MSVCRT_WEOF;
4908 MSVCRT__unlock_file(file);
4909 return mwc;
4912 /*********************************************************************
4913 * wprintf (MSVCRT.@)
4915 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
4917 __ms_va_list valist;
4918 int res;
4919 __ms_va_start(valist, format);
4920 res = MSVCRT_vwprintf(format, valist);
4921 __ms_va_end(valist);
4922 return res;
4925 /*********************************************************************
4926 * wprintf_s (MSVCRT.@)
4928 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
4930 __ms_va_list valist;
4931 int res;
4932 __ms_va_start(valist, format);
4933 res = MSVCRT_vwprintf_s(format, valist);
4934 __ms_va_end(valist);
4935 return res;
4938 /*********************************************************************
4939 * _getmaxstdio (MSVCRT.@)
4941 int CDECL MSVCRT__getmaxstdio(void)
4943 return MSVCRT_max_streams;
4946 /*********************************************************************
4947 * _setmaxstdio (MSVCRT.@)
4949 int CDECL MSVCRT__setmaxstdio(int newmax)
4951 TRACE("%d\n", newmax);
4953 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4954 return -1;
4956 MSVCRT_max_streams = newmax;
4957 return MSVCRT_max_streams;