winex11: Don't reset OpenGL swap interval of window on SetParent().
[wine.git] / dlls / msvcrt / file.c
blob44629e20894ce6de4e99e1545ebf5be9a95bae32
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 } ioinfo;
93 /*********************************************************************
94 * __pioinfo (MSVCRT.@)
95 * array of pointers to ioinfo arrays [32]
97 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
99 /*********************************************************************
100 * __badioinfo (MSVCRT.@)
102 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
104 static int MSVCRT_fdstart = 3; /* first unallocated fd */
105 static int MSVCRT_fdend = 3; /* highest allocated fd */
107 typedef struct {
108 MSVCRT_FILE file;
109 CRITICAL_SECTION crit;
110 } file_crit;
112 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
113 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
114 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
116 /* INTERNAL: process umask */
117 static int MSVCRT_umask = 0;
119 /* INTERNAL: static data for tmpnam and _wtmpname functions */
120 static int tmpnam_unique;
122 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
123 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
124 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
125 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
127 #define TOUL(x) (ULONGLONG)(x)
128 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
129 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
130 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
131 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
133 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
134 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
135 * and MSVCRT_stream_idx, from race conditions.
136 * It doesn't protect against race conditions manipulating the underlying files
137 * or flags; doing so would probably be better accomplished with per-file
138 * protection, rather than locking the whole table for every change.
140 static CRITICAL_SECTION MSVCRT_file_cs;
141 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
143 0, 0, &MSVCRT_file_cs,
144 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
145 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
147 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
148 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
149 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
151 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
153 buf->st_dev = buf64->st_dev;
154 buf->st_ino = buf64->st_ino;
155 buf->st_mode = buf64->st_mode;
156 buf->st_nlink = buf64->st_nlink;
157 buf->st_uid = buf64->st_uid;
158 buf->st_gid = buf64->st_gid;
159 buf->st_rdev = buf64->st_rdev;
160 buf->st_size = buf64->st_size;
161 buf->st_atime = buf64->st_atime;
162 buf->st_mtime = buf64->st_mtime;
163 buf->st_ctime = buf64->st_ctime;
166 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
168 buf->st_dev = buf64->st_dev;
169 buf->st_ino = buf64->st_ino;
170 buf->st_mode = buf64->st_mode;
171 buf->st_nlink = buf64->st_nlink;
172 buf->st_uid = buf64->st_uid;
173 buf->st_gid = buf64->st_gid;
174 buf->st_rdev = buf64->st_rdev;
175 buf->st_size = buf64->st_size;
176 buf->st_atime = buf64->st_atime;
177 buf->st_mtime = buf64->st_mtime;
178 buf->st_ctime = buf64->st_ctime;
181 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32 *buf)
183 buf->st_dev = buf64->st_dev;
184 buf->st_ino = buf64->st_ino;
185 buf->st_mode = buf64->st_mode;
186 buf->st_nlink = buf64->st_nlink;
187 buf->st_uid = buf64->st_uid;
188 buf->st_gid = buf64->st_gid;
189 buf->st_rdev = buf64->st_rdev;
190 buf->st_size = buf64->st_size;
191 buf->st_atime = buf64->st_atime;
192 buf->st_mtime = buf64->st_mtime;
193 buf->st_ctime = buf64->st_ctime;
196 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat64i32 *buf)
198 buf->st_dev = buf64->st_dev;
199 buf->st_ino = buf64->st_ino;
200 buf->st_mode = buf64->st_mode;
201 buf->st_nlink = buf64->st_nlink;
202 buf->st_uid = buf64->st_uid;
203 buf->st_gid = buf64->st_gid;
204 buf->st_rdev = buf64->st_rdev;
205 buf->st_size = buf64->st_size;
206 buf->st_atime = buf64->st_atime;
207 buf->st_mtime = buf64->st_mtime;
208 buf->st_ctime = buf64->st_ctime;
211 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
213 /* 1601 to 1970 is 369 years plus 89 leap days */
214 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
216 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
217 ft->dwHighDateTime = ticks >> 32;
218 ft->dwLowDateTime = ticks;
221 static inline ioinfo* msvcrt_get_ioinfo(int fd)
223 ioinfo *ret = NULL;
224 if(fd < MSVCRT_MAX_FILES)
225 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
226 if(!ret)
227 return &MSVCRT___badioinfo;
229 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
232 static inline MSVCRT_FILE* msvcrt_get_file(int i)
234 file_crit *ret;
236 if(i >= MSVCRT_max_streams)
237 return NULL;
239 if(i < _IOB_ENTRIES)
240 return &MSVCRT__iob[i];
242 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
243 if(!ret) {
244 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
245 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
246 ERR("out of memory\n");
247 *MSVCRT__errno() = MSVCRT_ENOMEM;
248 return NULL;
251 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
252 } else
253 ret += i%MSVCRT_FD_BLOCK_SIZE;
255 return &ret->file;
258 static inline BOOL msvcrt_is_valid_fd(int fd)
260 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
263 /* INTERNAL: Get the HANDLE for a fd
264 * This doesn't lock the table, because a failure will result in
265 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
266 * it returns a valid handle which is about to be closed, a subsequent call
267 * will fail, most likely in a sane way.
269 static HANDLE msvcrt_fdtoh(int fd)
271 if (!msvcrt_is_valid_fd(fd))
273 WARN(":fd (%d) - no handle!\n",fd);
274 *MSVCRT___doserrno() = 0;
275 *MSVCRT__errno() = MSVCRT_EBADF;
276 return INVALID_HANDLE_VALUE;
278 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
279 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd);
280 return msvcrt_get_ioinfo(fd)->handle;
283 /* INTERNAL: free a file entry fd */
284 static void msvcrt_free_fd(int fd)
286 ioinfo *fdinfo;
288 LOCK_FILES();
289 fdinfo = msvcrt_get_ioinfo(fd);
290 if(fdinfo != &MSVCRT___badioinfo)
292 fdinfo->handle = INVALID_HANDLE_VALUE;
293 fdinfo->wxflag = 0;
295 TRACE(":fd (%d) freed\n",fd);
297 if (fd < 3)
299 switch (fd)
301 case 0:
302 SetStdHandle(STD_INPUT_HANDLE, 0);
303 break;
304 case 1:
305 SetStdHandle(STD_OUTPUT_HANDLE, 0);
306 break;
307 case 2:
308 SetStdHandle(STD_ERROR_HANDLE, 0);
309 break;
313 if (fd == MSVCRT_fdend - 1)
314 MSVCRT_fdend--;
315 if (fd < MSVCRT_fdstart)
316 MSVCRT_fdstart = fd;
317 UNLOCK_FILES();
320 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
321 /* caller must hold the files lock */
322 static int msvcrt_set_fd(HANDLE hand, int flag, int fd)
324 ioinfo *fdinfo;
326 if (fd >= MSVCRT_MAX_FILES)
328 WARN(":files exhausted!\n");
329 *MSVCRT__errno() = MSVCRT_ENFILE;
330 return -1;
333 fdinfo = msvcrt_get_ioinfo(fd);
334 if(fdinfo == &MSVCRT___badioinfo) {
335 int i;
337 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
338 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
339 WARN(":out of memory!\n");
340 *MSVCRT__errno() = MSVCRT_ENOMEM;
341 return -1;
344 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
345 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
347 fdinfo = msvcrt_get_ioinfo(fd);
350 fdinfo->handle = hand;
351 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_NOSEEK));
352 fdinfo->lookahead[0] = '\n';
353 fdinfo->lookahead[1] = '\n';
354 fdinfo->lookahead[2] = '\n';
355 fdinfo->exflag = 0;
357 /* locate next free slot */
358 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
359 MSVCRT_fdstart = MSVCRT_fdend + 1;
360 else
361 while (MSVCRT_fdstart < MSVCRT_fdend &&
362 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
363 MSVCRT_fdstart++;
364 /* update last fd in use */
365 if (fd >= MSVCRT_fdend)
366 MSVCRT_fdend = fd + 1;
367 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
369 switch (fd)
371 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
372 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
373 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
376 return fd;
379 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
380 static int msvcrt_alloc_fd(HANDLE hand, int flag)
382 int ret;
384 LOCK_FILES();
385 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
386 ret = msvcrt_set_fd(hand, flag, MSVCRT_fdstart);
387 UNLOCK_FILES();
388 return ret;
391 /* INTERNAL: Allocate a FILE* for an fd slot */
392 /* caller must hold the files lock */
393 static MSVCRT_FILE* msvcrt_alloc_fp(void)
395 int i;
396 MSVCRT_FILE *file;
398 for (i = 3; i < MSVCRT_max_streams; i++)
400 file = msvcrt_get_file(i);
401 if (!file)
402 return NULL;
404 if (file->_flag == 0)
406 if (i == MSVCRT_stream_idx)
408 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
410 InitializeCriticalSection(&((file_crit*)file)->crit);
411 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
413 MSVCRT_stream_idx++;
415 return file;
419 return NULL;
422 /* INTERNAL: initialize a FILE* from an open fd */
423 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
425 TRACE(":fd (%d) allocating FILE*\n",fd);
426 if (!msvcrt_is_valid_fd(fd))
428 WARN(":invalid fd %d\n",fd);
429 *MSVCRT___doserrno() = 0;
430 *MSVCRT__errno() = MSVCRT_EBADF;
431 return -1;
433 memset(file, 0, sizeof(*file));
434 file->_file = fd;
435 file->_flag = stream_flags;
437 TRACE(":got FILE* (%p)\n",file);
438 return 0;
441 /* INTERNAL: Create an inheritance data block (for spawned process)
442 * The inheritance block is made of:
443 * 00 int nb of file descriptor (NBFD)
444 * 04 char file flags (wxflag): repeated for each fd
445 * 4+NBFD HANDLE file handle: repeated for each fd
447 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
449 int fd;
450 char* wxflag_ptr;
451 HANDLE* handle_ptr;
452 ioinfo* fdinfo;
454 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
455 *block = MSVCRT_calloc(*size, 1);
456 if (!*block)
458 *size = 0;
459 return FALSE;
461 wxflag_ptr = (char*)*block + sizeof(unsigned);
462 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
464 *(unsigned*)*block = MSVCRT_fdend;
465 for (fd = 0; fd < MSVCRT_fdend; fd++)
467 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
468 fdinfo = msvcrt_get_ioinfo(fd);
469 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
471 *wxflag_ptr = fdinfo->wxflag;
472 *handle_ptr = fdinfo->handle;
474 else
476 *wxflag_ptr = 0;
477 *handle_ptr = INVALID_HANDLE_VALUE;
479 wxflag_ptr++; handle_ptr++;
481 return TRUE;
484 /* INTERNAL: Set up all file descriptors,
485 * as well as default streams (stdin, stderr and stdout)
487 void msvcrt_init_io(void)
489 STARTUPINFOA si;
490 int i;
491 ioinfo *fdinfo;
493 GetStartupInfoA(&si);
494 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
496 BYTE* wxflag_ptr;
497 HANDLE* handle_ptr;
498 unsigned int count;
500 count = *(unsigned*)si.lpReserved2;
501 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
502 handle_ptr = (HANDLE*)(wxflag_ptr + count);
504 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
505 count = min(count, MSVCRT_MAX_FILES);
506 for (i = 0; i < count; i++)
508 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
509 msvcrt_set_fd(*handle_ptr, *wxflag_ptr, i);
511 wxflag_ptr++; handle_ptr++;
513 MSVCRT_fdend = max( 3, count );
514 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
515 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
518 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO);
519 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
520 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
521 DWORD type = GetFileType(h);
523 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_NOSEEK : 0)
524 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDIN_FILENO);
527 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO);
528 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
529 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
530 DWORD type = GetFileType(h);
532 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_NOSEEK : 0)
533 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDOUT_FILENO);
536 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO);
537 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
538 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
539 DWORD type = GetFileType(h);
541 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_NOSEEK : 0)
542 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDERR_FILENO);
545 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO)->handle,
546 msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO)->handle,
547 msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO)->handle);
549 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
550 for (i = 0; i < 3; i++)
552 /* FILE structs for stdin/out/err are static and never deleted */
553 MSVCRT__iob[i]._file = i;
554 MSVCRT__iob[i]._tmpfname = NULL;
555 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
557 MSVCRT_stream_idx = 3;
560 /* INTERNAL: Flush stdio file buffer */
561 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
563 if(file->_bufsiz) {
564 int cnt=file->_ptr-file->_base;
565 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
566 file->_flag |= MSVCRT__IOERR;
567 return MSVCRT_EOF;
569 file->_ptr=file->_base;
570 file->_cnt=file->_bufsiz;
572 return 0;
575 /*********************************************************************
576 * _isatty (MSVCRT.@)
578 int CDECL MSVCRT__isatty(int fd)
580 HANDLE hand = msvcrt_fdtoh(fd);
582 TRACE(":fd (%d) handle (%p)\n",fd,hand);
583 if (hand == INVALID_HANDLE_VALUE)
584 return 0;
586 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
589 /* INTERNAL: Allocate stdio file buffer */
590 static BOOL msvcrt_alloc_buffer(MSVCRT_FILE* file)
592 if((file->_file==MSVCRT_STDOUT_FILENO || file->_file==MSVCRT_STDERR_FILENO)
593 && MSVCRT__isatty(file->_file))
594 return FALSE;
596 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
597 if(file->_base) {
598 file->_bufsiz = MSVCRT_BUFSIZ;
599 file->_flag |= MSVCRT__IOMYBUF;
600 } else {
601 file->_base = (char*)(&file->_charbuf);
602 /* put here 2 ??? */
603 file->_bufsiz = sizeof(file->_charbuf);
605 file->_ptr = file->_base;
606 file->_cnt = 0;
607 return TRUE;
610 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
611 static BOOL add_std_buffer(MSVCRT_FILE *file)
613 static char buffers[2][MSVCRT_BUFSIZ];
615 if((file->_file!=MSVCRT_STDOUT_FILENO && file->_file!=MSVCRT_STDERR_FILENO)
616 || !MSVCRT__isatty(file->_file) || file->_bufsiz)
617 return FALSE;
619 file->_ptr = file->_base = buffers[file->_file == MSVCRT_STDOUT_FILENO ? 0 : 1];
620 file->_bufsiz = file->_cnt = MSVCRT_BUFSIZ;
621 return TRUE;
624 /* INTERNAL: Removes temporary buffer from stdout or stderr */
625 /* Only call this function when add_std_buffer returned TRUE */
626 static void remove_std_buffer(MSVCRT_FILE *file)
628 msvcrt_flush_buffer(file);
629 file->_ptr = file->_base = NULL;
630 file->_bufsiz = file->_cnt = 0;
633 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
634 static int msvcrt_int_to_base32(int num, char *str)
636 char *p;
637 int n = num;
638 int digits = 0;
640 while (n != 0)
642 n >>= 5;
643 digits++;
645 p = str + digits;
646 *p = 0;
647 while (--p >= str)
649 *p = (num & 31) + '0';
650 if (*p > '9')
651 *p += ('a' - '0' - 10);
652 num >>= 5;
655 return digits;
658 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
659 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
661 MSVCRT_wchar_t *p;
662 int n = num;
663 int digits = 0;
665 while (n != 0)
667 n >>= 5;
668 digits++;
670 p = str + digits;
671 *p = 0;
672 while (--p >= str)
674 *p = (num & 31) + '0';
675 if (*p > '9')
676 *p += ('a' - '0' - 10);
677 num >>= 5;
680 return digits;
683 /*********************************************************************
684 * __iob_func(MSVCRT.@)
686 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
688 return &MSVCRT__iob[0];
691 /*********************************************************************
692 * _access (MSVCRT.@)
694 int CDECL MSVCRT__access(const char *filename, int mode)
696 DWORD attr = GetFileAttributesA(filename);
698 TRACE("(%s,%d) %d\n",filename,mode,attr);
700 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
702 msvcrt_set_errno(GetLastError());
703 return -1;
705 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
707 msvcrt_set_errno(ERROR_ACCESS_DENIED);
708 return -1;
710 return 0;
713 /*********************************************************************
714 * _access_s (MSVCRT.@)
716 int CDECL _access_s(const char *filename, int mode)
718 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
719 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
721 if (MSVCRT__access(filename, mode) == -1)
722 return *MSVCRT__errno();
723 return 0;
726 /*********************************************************************
727 * _waccess (MSVCRT.@)
729 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
731 DWORD attr = GetFileAttributesW(filename);
733 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
735 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
737 msvcrt_set_errno(GetLastError());
738 return -1;
740 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
742 msvcrt_set_errno(ERROR_ACCESS_DENIED);
743 return -1;
745 return 0;
748 /*********************************************************************
749 * _waccess_s (MSVCRT.@)
751 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
753 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
754 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
756 if (MSVCRT__waccess(filename, mode) == -1)
757 return *MSVCRT__errno();
758 return 0;
761 /*********************************************************************
762 * _chmod (MSVCRT.@)
764 int CDECL MSVCRT__chmod(const char *path, int flags)
766 DWORD oldFlags = GetFileAttributesA(path);
768 if (oldFlags != INVALID_FILE_ATTRIBUTES)
770 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
771 oldFlags | FILE_ATTRIBUTE_READONLY;
773 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
774 return 0;
776 msvcrt_set_errno(GetLastError());
777 return -1;
780 /*********************************************************************
781 * _wchmod (MSVCRT.@)
783 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
785 DWORD oldFlags = GetFileAttributesW(path);
787 if (oldFlags != INVALID_FILE_ATTRIBUTES)
789 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
790 oldFlags | FILE_ATTRIBUTE_READONLY;
792 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
793 return 0;
795 msvcrt_set_errno(GetLastError());
796 return -1;
799 /*********************************************************************
800 * _unlink (MSVCRT.@)
802 int CDECL MSVCRT__unlink(const char *path)
804 TRACE("%s\n",debugstr_a(path));
805 if(DeleteFileA(path))
806 return 0;
807 TRACE("failed (%d)\n",GetLastError());
808 msvcrt_set_errno(GetLastError());
809 return -1;
812 /*********************************************************************
813 * _wunlink (MSVCRT.@)
815 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
817 TRACE("(%s)\n",debugstr_w(path));
818 if(DeleteFileW(path))
819 return 0;
820 TRACE("failed (%d)\n",GetLastError());
821 msvcrt_set_errno(GetLastError());
822 return -1;
825 /*********************************************************************
826 * _commit (MSVCRT.@)
828 int CDECL MSVCRT__commit(int fd)
830 HANDLE hand = msvcrt_fdtoh(fd);
832 TRACE(":fd (%d) handle (%p)\n",fd,hand);
833 if (hand == INVALID_HANDLE_VALUE)
834 return -1;
836 if (!FlushFileBuffers(hand))
838 if (GetLastError() == ERROR_INVALID_HANDLE)
840 /* FlushFileBuffers fails for console handles
841 * so we ignore this error.
843 return 0;
845 TRACE(":failed-last error (%d)\n",GetLastError());
846 msvcrt_set_errno(GetLastError());
847 return -1;
849 TRACE(":ok\n");
850 return 0;
853 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
854 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
856 /* INTERNAL: Flush all stream buffer */
857 static int msvcrt_flush_all_buffers(int mask)
859 int i, num_flushed = 0;
860 MSVCRT_FILE *file;
862 LOCK_FILES();
863 for (i = 0; i < MSVCRT_stream_idx; i++) {
864 file = msvcrt_get_file(i);
866 if (file->_flag)
868 if(file->_flag & mask) {
869 MSVCRT_fflush(file);
870 num_flushed++;
874 UNLOCK_FILES();
876 TRACE(":flushed (%d) handles\n",num_flushed);
877 return num_flushed;
880 /*********************************************************************
881 * _flushall (MSVCRT.@)
883 int CDECL MSVCRT__flushall(void)
885 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
888 /*********************************************************************
889 * fflush (MSVCRT.@)
891 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
893 if(!file) {
894 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
895 } else if(file->_flag & MSVCRT__IOWRT) {
896 int res;
898 MSVCRT__lock_file(file);
899 res = msvcrt_flush_buffer(file);
901 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
902 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
903 MSVCRT__unlock_file(file);
905 return res;
906 } else if(file->_flag & MSVCRT__IOREAD) {
907 MSVCRT__lock_file(file);
908 file->_cnt = 0;
909 file->_ptr = file->_base;
910 MSVCRT__unlock_file(file);
912 return 0;
914 return 0;
917 /*********************************************************************
918 * _close (MSVCRT.@)
920 int CDECL MSVCRT__close(int fd)
922 HANDLE hand;
923 int ret;
925 LOCK_FILES();
926 hand = msvcrt_fdtoh(fd);
927 TRACE(":fd (%d) handle (%p)\n",fd,hand);
928 if (!msvcrt_is_valid_fd(fd)) {
929 ret = -1;
930 } else {
931 msvcrt_free_fd(fd);
932 ret = CloseHandle(hand) ? 0 : -1;
933 if (ret) {
934 WARN(":failed-last error (%d)\n",GetLastError());
935 msvcrt_set_errno(GetLastError());
938 UNLOCK_FILES();
939 TRACE(":ok\n");
940 return ret;
943 /*********************************************************************
944 * _dup2 (MSVCRT.@)
945 * NOTES
946 * MSDN isn't clear on this point, but the remarks for _pipe
947 * indicate file descriptors duplicated with _dup and _dup2 are always
948 * inheritable.
950 int CDECL MSVCRT__dup2(int od, int nd)
952 int ret;
954 TRACE("(od=%d, nd=%d)\n", od, nd);
955 LOCK_FILES();
956 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
958 HANDLE handle;
960 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
961 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
963 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
965 if (msvcrt_is_valid_fd(nd))
966 MSVCRT__close(nd);
967 ret = msvcrt_set_fd(handle, wxflag, nd);
968 if (ret == -1)
970 CloseHandle(handle);
971 *MSVCRT__errno() = MSVCRT_EMFILE;
973 else
975 /* _dup2 returns 0, not nd, on success */
976 ret = 0;
979 else
981 ret = -1;
982 msvcrt_set_errno(GetLastError());
985 else
987 *MSVCRT__errno() = MSVCRT_EBADF;
988 ret = -1;
990 UNLOCK_FILES();
991 return ret;
994 /*********************************************************************
995 * _dup (MSVCRT.@)
997 int CDECL MSVCRT__dup(int od)
999 int fd, ret;
1001 LOCK_FILES();
1002 fd = MSVCRT_fdstart;
1003 if (MSVCRT__dup2(od, fd) == 0)
1004 ret = fd;
1005 else
1006 ret = -1;
1007 UNLOCK_FILES();
1008 return ret;
1011 /*********************************************************************
1012 * _eof (MSVCRT.@)
1014 int CDECL MSVCRT__eof(int fd)
1016 DWORD curpos,endpos;
1017 LONG hcurpos,hendpos;
1018 HANDLE hand = msvcrt_fdtoh(fd);
1020 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1022 if (hand == INVALID_HANDLE_VALUE)
1023 return -1;
1025 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
1027 /* Otherwise we do it the hard way */
1028 hcurpos = hendpos = 0;
1029 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
1030 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
1032 if (curpos == endpos && hcurpos == hendpos)
1034 /* FIXME: shouldn't WX_ATEOF be set here? */
1035 return TRUE;
1038 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
1039 return FALSE;
1042 /*********************************************************************
1043 * _fcloseall (MSVCRT.@)
1045 int CDECL MSVCRT__fcloseall(void)
1047 int num_closed = 0, i;
1048 MSVCRT_FILE *file;
1050 LOCK_FILES();
1051 for (i = 3; i < MSVCRT_stream_idx; i++) {
1052 file = msvcrt_get_file(i);
1054 if (file->_flag && !MSVCRT_fclose(file))
1055 num_closed++;
1057 UNLOCK_FILES();
1059 TRACE(":closed (%d) handles\n",num_closed);
1060 return num_closed;
1063 /* free everything on process exit */
1064 void msvcrt_free_io(void)
1066 unsigned int i;
1067 int j;
1069 MSVCRT__flushall();
1070 MSVCRT__fcloseall();
1072 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1073 MSVCRT_free(MSVCRT___pioinfo[i]);
1075 for(j=0; j<MSVCRT_stream_idx; j++)
1077 MSVCRT_FILE *file = msvcrt_get_file(j);
1078 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1080 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1081 DeleteCriticalSection(&((file_crit*)file)->crit);
1085 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1086 MSVCRT_free(MSVCRT_fstream[i]);
1088 DeleteCriticalSection(&MSVCRT_file_cs);
1091 /*********************************************************************
1092 * _lseeki64 (MSVCRT.@)
1094 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1096 HANDLE hand = msvcrt_fdtoh(fd);
1097 LARGE_INTEGER ofs;
1099 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1100 if (hand == INVALID_HANDLE_VALUE)
1101 return -1;
1103 if (whence < 0 || whence > 2)
1105 *MSVCRT__errno() = MSVCRT_EINVAL;
1106 return -1;
1109 TRACE(":fd (%d) to %s pos %s\n",
1110 fd,wine_dbgstr_longlong(offset),
1111 (whence==SEEK_SET)?"SEEK_SET":
1112 (whence==SEEK_CUR)?"SEEK_CUR":
1113 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1115 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1116 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1117 ofs.QuadPart = offset;
1118 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1119 GetLastError() == ERROR_SUCCESS)
1121 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_ATEOF;
1122 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1124 return ofs.QuadPart;
1126 TRACE(":error-last error (%d)\n",GetLastError());
1127 msvcrt_set_errno(GetLastError());
1128 return -1;
1131 /*********************************************************************
1132 * _lseek (MSVCRT.@)
1134 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1136 return MSVCRT__lseeki64(fd, offset, whence);
1139 /*********************************************************************
1140 * _lock_file (MSVCRT.@)
1142 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1144 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1145 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1146 else
1147 EnterCriticalSection(&((file_crit*)file)->crit);
1150 /*********************************************************************
1151 * _unlock_file (MSVCRT.@)
1153 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1155 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1156 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1157 else
1158 LeaveCriticalSection(&((file_crit*)file)->crit);
1161 /*********************************************************************
1162 * _locking (MSVCRT.@)
1164 * This is untested; the underlying LockFile doesn't work yet.
1166 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1168 BOOL ret;
1169 DWORD cur_locn;
1170 HANDLE hand = msvcrt_fdtoh(fd);
1172 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1173 if (hand == INVALID_HANDLE_VALUE)
1174 return -1;
1176 if (mode < 0 || mode > 4)
1178 *MSVCRT__errno() = MSVCRT_EINVAL;
1179 return -1;
1182 TRACE(":fd (%d) by 0x%08x mode %s\n",
1183 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1184 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1185 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1186 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1187 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1188 "UNKNOWN");
1190 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1192 FIXME ("Seek failed\n");
1193 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1194 return -1;
1196 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1198 int nretry = 10;
1199 ret = 1; /* just to satisfy gcc */
1200 while (nretry--)
1202 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1203 if (ret) break;
1204 Sleep(1);
1207 else if (mode == MSVCRT__LK_UNLCK)
1208 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1209 else
1210 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1211 /* FIXME - what about error settings? */
1212 return ret ? 0 : -1;
1215 /*********************************************************************
1216 * _fseeki64 (MSVCRT.@)
1218 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1220 int ret;
1222 MSVCRT__lock_file(file);
1223 /* Flush output if needed */
1224 if(file->_flag & MSVCRT__IOWRT)
1225 msvcrt_flush_buffer(file);
1227 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1228 whence = SEEK_SET;
1229 offset += MSVCRT__ftelli64(file);
1232 /* Discard buffered input */
1233 file->_cnt = 0;
1234 file->_ptr = file->_base;
1235 /* Reset direction of i/o */
1236 if(file->_flag & MSVCRT__IORW) {
1237 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1239 /* Clear end of file flag */
1240 file->_flag &= ~MSVCRT__IOEOF;
1241 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1243 MSVCRT__unlock_file(file);
1244 return ret;
1247 /*********************************************************************
1248 * fseek (MSVCRT.@)
1250 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1252 return MSVCRT__fseeki64( file, offset, whence );
1255 /*********************************************************************
1256 * _chsize_s (MSVCRT.@)
1258 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1260 __int64 cur, pos;
1261 HANDLE handle;
1262 BOOL ret = FALSE;
1264 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1266 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1268 LOCK_FILES();
1270 handle = msvcrt_fdtoh(fd);
1271 if (handle != INVALID_HANDLE_VALUE)
1273 /* save the current file pointer */
1274 cur = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1275 if (cur >= 0)
1277 pos = MSVCRT__lseeki64(fd, size, SEEK_SET);
1278 if (pos >= 0)
1280 ret = SetEndOfFile(handle);
1281 if (!ret) msvcrt_set_errno(GetLastError());
1284 /* restore the file pointer */
1285 MSVCRT__lseeki64(fd, cur, SEEK_SET);
1289 UNLOCK_FILES();
1290 return ret ? 0 : *MSVCRT__errno();
1293 /*********************************************************************
1294 * _chsize (MSVCRT.@)
1296 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1298 /* _chsize_s returns errno on failure but _chsize should return -1 */
1299 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1302 /*********************************************************************
1303 * clearerr (MSVCRT.@)
1305 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1307 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1309 MSVCRT__lock_file(file);
1310 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1311 MSVCRT__unlock_file(file);
1314 /*********************************************************************
1315 * rewind (MSVCRT.@)
1317 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1319 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1321 MSVCRT__lock_file(file);
1322 MSVCRT_fseek(file, 0L, SEEK_SET);
1323 MSVCRT_clearerr(file);
1324 MSVCRT__unlock_file(file);
1327 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1329 int plus = strchrW(mode, '+') != NULL;
1331 TRACE("%s\n", debugstr_w(mode));
1333 while(*mode == ' ') mode++;
1335 switch(*mode++)
1337 case 'R': case 'r':
1338 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1339 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1340 break;
1341 case 'W': case 'w':
1342 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1343 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1344 break;
1345 case 'A': case 'a':
1346 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1347 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1348 break;
1349 default:
1350 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1351 return -1;
1354 *stream_flags |= MSVCRT__commode;
1356 while (*mode && *mode!=',')
1357 switch (*mode++)
1359 case 'B': case 'b':
1360 *open_flags |= MSVCRT__O_BINARY;
1361 *open_flags &= ~MSVCRT__O_TEXT;
1362 break;
1363 case 't':
1364 *open_flags |= MSVCRT__O_TEXT;
1365 *open_flags &= ~MSVCRT__O_BINARY;
1366 break;
1367 case 'D':
1368 *open_flags |= MSVCRT__O_TEMPORARY;
1369 break;
1370 case 'T':
1371 *open_flags |= MSVCRT__O_SHORT_LIVED;
1372 break;
1373 case 'c':
1374 *stream_flags |= MSVCRT__IOCOMMIT;
1375 break;
1376 case 'n':
1377 *stream_flags &= ~MSVCRT__IOCOMMIT;
1378 break;
1379 case 'N':
1380 *open_flags |= MSVCRT__O_NOINHERIT;
1381 break;
1382 case '+':
1383 case ' ':
1384 case 'a':
1385 case 'w':
1386 break;
1387 case 'S':
1388 case 'R':
1389 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1390 break;
1391 default:
1392 ERR("incorrect mode flag: %c\n", mode[-1]);
1393 break;
1396 if(*mode == ',')
1398 static const WCHAR ccs[] = {'c','c','s'};
1399 static const WCHAR utf8[] = {'u','t','f','-','8'};
1400 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1401 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1403 mode++;
1404 while(*mode == ' ') mode++;
1405 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1406 return -1;
1407 mode += sizeof(ccs)/sizeof(ccs[0]);
1408 while(*mode == ' ') mode++;
1409 if(!MSVCRT_CHECK_PMT(*mode == '='))
1410 return -1;
1411 mode++;
1412 while(*mode == ' ') mode++;
1414 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1416 *open_flags |= MSVCRT__O_U8TEXT;
1417 mode += sizeof(utf8)/sizeof(utf8[0]);
1419 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1421 *open_flags |= MSVCRT__O_U16TEXT;
1422 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1424 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1426 *open_flags |= MSVCRT__O_WTEXT;
1427 mode += sizeof(unicode)/sizeof(unicode[0]);
1429 else
1431 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1432 return -1;
1435 while(*mode == ' ') mode++;
1438 if(!MSVCRT_CHECK_PMT(*mode == 0))
1439 return -1;
1440 return 0;
1443 /*********************************************************************
1444 * _fdopen (MSVCRT.@)
1446 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1448 MSVCRT_FILE *ret;
1449 MSVCRT_wchar_t *modeW = NULL;
1451 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1453 ret = MSVCRT__wfdopen(fd, modeW);
1455 MSVCRT_free(modeW);
1456 return ret;
1459 /*********************************************************************
1460 * _wfdopen (MSVCRT.@)
1462 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1464 int open_flags, stream_flags;
1465 MSVCRT_FILE* file;
1467 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1469 LOCK_FILES();
1470 if (!(file = msvcrt_alloc_fp()))
1471 file = NULL;
1472 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1474 file->_flag = 0;
1475 file = NULL;
1477 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1478 UNLOCK_FILES();
1480 return file;
1483 /*********************************************************************
1484 * _filelength (MSVCRT.@)
1486 LONG CDECL MSVCRT__filelength(int fd)
1488 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1489 if (curPos != -1)
1491 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1492 if (endPos != -1)
1494 if (endPos != curPos)
1495 MSVCRT__lseek(fd, curPos, SEEK_SET);
1496 return endPos;
1499 return -1;
1502 /*********************************************************************
1503 * _filelengthi64 (MSVCRT.@)
1505 __int64 CDECL MSVCRT__filelengthi64(int fd)
1507 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1508 if (curPos != -1)
1510 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1511 if (endPos != -1)
1513 if (endPos != curPos)
1514 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1515 return endPos;
1518 return -1;
1521 /*********************************************************************
1522 * _fileno (MSVCRT.@)
1524 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1526 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1527 return file->_file;
1530 /*********************************************************************
1531 * _fstat64 (MSVCRT.@)
1533 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1535 DWORD dw;
1536 DWORD type;
1537 BY_HANDLE_FILE_INFORMATION hfi;
1538 HANDLE hand = msvcrt_fdtoh(fd);
1540 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1541 if (hand == INVALID_HANDLE_VALUE)
1542 return -1;
1544 if (!buf)
1546 WARN(":failed-NULL buf\n");
1547 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1548 return -1;
1551 memset(&hfi, 0, sizeof(hfi));
1552 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1553 type = GetFileType(hand);
1554 if (type == FILE_TYPE_PIPE)
1556 buf->st_dev = buf->st_rdev = fd;
1557 buf->st_mode = S_IFIFO;
1558 buf->st_nlink = 1;
1560 else if (type == FILE_TYPE_CHAR)
1562 buf->st_dev = buf->st_rdev = fd;
1563 buf->st_mode = S_IFCHR;
1564 buf->st_nlink = 1;
1566 else /* FILE_TYPE_DISK etc. */
1568 if (!GetFileInformationByHandle(hand, &hfi))
1570 WARN(":failed-last error (%d)\n",GetLastError());
1571 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1572 return -1;
1574 buf->st_mode = S_IFREG | 0444;
1575 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1576 buf->st_mode |= 0222;
1577 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1578 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1579 buf->st_atime = dw;
1580 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1581 buf->st_mtime = buf->st_ctime = dw;
1582 buf->st_nlink = hfi.nNumberOfLinks;
1584 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1585 buf->st_mode);
1586 return 0;
1589 /*********************************************************************
1590 * _fstati64 (MSVCRT.@)
1592 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1594 int ret;
1595 struct MSVCRT__stat64 buf64;
1597 ret = MSVCRT__fstat64(fd, &buf64);
1598 if (!ret)
1599 msvcrt_stat64_to_stati64(&buf64, buf);
1600 return ret;
1603 /*********************************************************************
1604 * _fstat (MSVCRT.@)
1606 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1607 { int ret;
1608 struct MSVCRT__stat64 buf64;
1610 ret = MSVCRT__fstat64(fd, &buf64);
1611 if (!ret)
1612 msvcrt_stat64_to_stat(&buf64, buf);
1613 return ret;
1616 /* _fstat32 - not exported in native msvcrt */
1617 int CDECL _fstat32(int fd, struct MSVCRT__stat32* buf)
1619 int ret;
1620 struct MSVCRT__stat64 buf64;
1622 ret = MSVCRT__fstat64(fd, &buf64);
1623 if (!ret)
1624 msvcrt_stat64_to_stat32(&buf64, buf);
1625 return ret;
1628 /* _fstat64i32 - not exported in native msvcrt */
1629 int CDECL _fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1631 int ret;
1632 struct MSVCRT__stat64 buf64;
1634 ret = MSVCRT__fstat64(fd, &buf64);
1635 if (!ret)
1636 msvcrt_stat64_to_stat64i32(&buf64, buf);
1637 return ret;
1640 /*********************************************************************
1641 * _futime64 (MSVCRT.@)
1643 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1645 HANDLE hand = msvcrt_fdtoh(fd);
1646 FILETIME at, wt;
1648 if (!t)
1650 time_to_filetime( MSVCRT__time64(NULL), &at );
1651 wt = at;
1653 else
1655 time_to_filetime( t->actime, &at );
1656 time_to_filetime( t->modtime, &wt );
1659 if (!SetFileTime(hand, NULL, &at, &wt))
1661 msvcrt_set_errno(GetLastError());
1662 return -1 ;
1664 return 0;
1667 /*********************************************************************
1668 * _futime32 (MSVCRT.@)
1670 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1672 if (t)
1674 struct MSVCRT___utimbuf64 t64;
1675 t64.actime = t->actime;
1676 t64.modtime = t->modtime;
1677 return _futime64( fd, &t64 );
1679 else
1680 return _futime64( fd, NULL );
1683 /*********************************************************************
1684 * _futime (MSVCRT.@)
1686 #ifdef _WIN64
1687 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1689 return _futime64( fd, t );
1691 #else
1692 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1694 return _futime32( fd, t );
1696 #endif
1698 /*********************************************************************
1699 * _get_osfhandle (MSVCRT.@)
1701 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1703 HANDLE hand = msvcrt_fdtoh(fd);
1704 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1706 return (MSVCRT_intptr_t)hand;
1709 /*********************************************************************
1710 * _mktemp_s (MSVCRT.@)
1712 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1714 DWORD len, xno, id;
1716 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1717 return MSVCRT_EINVAL;
1719 for(len=0; len<size; len++)
1720 if(!pattern[len])
1721 break;
1722 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1723 if(size)
1724 pattern[0] = 0;
1725 return MSVCRT_EINVAL;
1728 for(xno=1; xno<=6; xno++)
1729 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1730 return MSVCRT_EINVAL;
1732 id = GetCurrentProcessId();
1733 for(xno=1; xno<6; xno++) {
1734 pattern[len-xno] = id%10 + '0';
1735 id /= 10;
1738 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1739 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1740 return 0;
1743 pattern[0] = 0;
1744 *MSVCRT__errno() = MSVCRT_EEXIST;
1745 return MSVCRT_EEXIST;
1748 /*********************************************************************
1749 * _mktemp (MSVCRT.@)
1751 char * CDECL MSVCRT__mktemp(char *pattern)
1753 int numX = 0;
1754 char *retVal = pattern;
1755 int id;
1756 char letter = 'a';
1758 if(!pattern)
1759 return NULL;
1761 while(*pattern)
1762 numX = (*pattern++ == 'X')? numX + 1 : 0;
1763 if (numX < 6)
1764 return NULL;
1765 pattern--;
1766 id = GetCurrentProcessId();
1767 numX = 6;
1768 while(numX--)
1770 int tempNum = id / 10;
1771 *pattern-- = id - (tempNum * 10) + '0';
1772 id = tempNum;
1774 pattern++;
1777 *pattern = letter++;
1778 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1779 return retVal;
1780 } while(letter <= 'z');
1781 return NULL;
1784 /*********************************************************************
1785 * _wmktemp_s (MSVCRT.@)
1787 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1789 DWORD len, xno, id;
1791 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1792 return MSVCRT_EINVAL;
1794 for(len=0; len<size; len++)
1795 if(!pattern[len])
1796 break;
1797 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1798 if(size)
1799 pattern[0] = 0;
1800 return MSVCRT_EINVAL;
1803 for(xno=1; xno<=6; xno++)
1804 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1805 return MSVCRT_EINVAL;
1807 id = GetCurrentProcessId();
1808 for(xno=1; xno<6; xno++) {
1809 pattern[len-xno] = id%10 + '0';
1810 id /= 10;
1813 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1814 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1815 return 0;
1818 pattern[0] = 0;
1819 *MSVCRT__errno() = MSVCRT_EEXIST;
1820 return MSVCRT_EEXIST;
1823 /*********************************************************************
1824 * _wmktemp (MSVCRT.@)
1826 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1828 int numX = 0;
1829 MSVCRT_wchar_t *retVal = pattern;
1830 int id;
1831 MSVCRT_wchar_t letter = 'a';
1833 if(!pattern)
1834 return NULL;
1836 while(*pattern)
1837 numX = (*pattern++ == 'X')? numX + 1 : 0;
1838 if (numX < 6)
1839 return NULL;
1840 pattern--;
1841 id = GetCurrentProcessId();
1842 numX = 6;
1843 while(numX--)
1845 int tempNum = id / 10;
1846 *pattern-- = id - (tempNum * 10) + '0';
1847 id = tempNum;
1849 pattern++;
1852 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
1853 return retVal;
1854 *pattern = letter++;
1855 } while(letter != '|');
1856 return NULL;
1859 static unsigned split_oflags(unsigned oflags)
1861 int wxflags = 0;
1862 unsigned unsupp; /* until we support everything */
1864 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1865 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1866 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1867 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
1868 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
1869 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
1870 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1871 else wxflags |= WX_TEXT; /* default to TEXT*/
1872 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1874 if ((unsupp = oflags & ~(
1875 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1876 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1877 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1878 MSVCRT__O_NOINHERIT|
1879 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
1880 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
1882 ERR(":unsupported oflags 0x%04x\n",unsupp);
1884 return wxflags;
1887 /*********************************************************************
1888 * _pipe (MSVCRT.@)
1890 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1892 int ret = -1;
1893 SECURITY_ATTRIBUTES sa;
1894 HANDLE readHandle, writeHandle;
1896 if (!pfds)
1898 *MSVCRT__errno() = MSVCRT_EINVAL;
1899 return -1;
1902 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1903 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1904 sa.lpSecurityDescriptor = NULL;
1905 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1907 unsigned int wxflags = split_oflags(textmode);
1908 int fd;
1910 LOCK_FILES();
1911 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
1912 if (fd != -1)
1914 pfds[0] = fd;
1915 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
1916 if (fd != -1)
1918 pfds[1] = fd;
1919 ret = 0;
1921 else
1923 MSVCRT__close(pfds[0]);
1924 CloseHandle(writeHandle);
1925 *MSVCRT__errno() = MSVCRT_EMFILE;
1928 else
1930 CloseHandle(readHandle);
1931 CloseHandle(writeHandle);
1932 *MSVCRT__errno() = MSVCRT_EMFILE;
1934 UNLOCK_FILES();
1936 else
1937 msvcrt_set_errno(GetLastError());
1939 return ret;
1942 static int check_bom(HANDLE h, int oflags, BOOL seek)
1944 char bom[sizeof(utf8_bom)];
1945 DWORD r;
1947 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
1949 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
1950 return oflags;
1952 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
1953 oflags |= MSVCRT__O_U8TEXT;
1954 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
1955 if (seek && r>2)
1956 SetFilePointer(h, 2, NULL, FILE_BEGIN);
1957 oflags |= MSVCRT__O_U16TEXT;
1958 }else if (seek) {
1959 SetFilePointer(h, 0, NULL, FILE_BEGIN);
1962 return oflags;
1965 /*********************************************************************
1966 * _wsopen_s (MSVCRT.@)
1968 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1970 DWORD access = 0, creation = 0, attrib;
1971 SECURITY_ATTRIBUTES sa;
1972 DWORD sharing;
1973 int wxflag;
1974 HANDLE hand;
1976 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1977 fd, debugstr_w(path), oflags, shflags, pmode);
1979 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
1981 *fd = -1;
1982 wxflag = split_oflags(oflags);
1983 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1985 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1986 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1987 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1990 if (oflags & MSVCRT__O_CREAT)
1992 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1993 FIXME(": pmode 0x%04x ignored\n", pmode);
1994 else
1995 WARN(": pmode 0x%04x ignored\n", pmode);
1997 if (oflags & MSVCRT__O_EXCL)
1998 creation = CREATE_NEW;
1999 else if (oflags & MSVCRT__O_TRUNC)
2000 creation = CREATE_ALWAYS;
2001 else
2002 creation = OPEN_ALWAYS;
2004 else /* no MSVCRT__O_CREAT */
2006 if (oflags & MSVCRT__O_TRUNC)
2007 creation = TRUNCATE_EXISTING;
2008 else
2009 creation = OPEN_EXISTING;
2012 switch( shflags )
2014 case MSVCRT__SH_DENYRW:
2015 sharing = 0L;
2016 break;
2017 case MSVCRT__SH_DENYWR:
2018 sharing = FILE_SHARE_READ;
2019 break;
2020 case MSVCRT__SH_DENYRD:
2021 sharing = FILE_SHARE_WRITE;
2022 break;
2023 case MSVCRT__SH_DENYNO:
2024 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2025 break;
2026 default:
2027 ERR( "Unhandled shflags 0x%x\n", shflags );
2028 return MSVCRT_EINVAL;
2030 attrib = FILE_ATTRIBUTE_NORMAL;
2032 if (oflags & MSVCRT__O_TEMPORARY)
2034 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2035 access |= DELETE;
2036 sharing |= FILE_SHARE_DELETE;
2039 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2040 sa.lpSecurityDescriptor = NULL;
2041 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2043 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2044 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2045 && !(access&GENERIC_READ))
2047 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2048 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2049 if (hand != INVALID_HANDLE_VALUE)
2051 oflags = check_bom(hand, oflags, FALSE);
2052 CloseHandle(hand);
2054 else
2055 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2058 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2059 if (hand == INVALID_HANDLE_VALUE) {
2060 WARN(":failed-last error (%d)\n",GetLastError());
2061 msvcrt_set_errno(GetLastError());
2062 return *MSVCRT__errno();
2065 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2067 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2068 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2069 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2071 if (oflags & MSVCRT__O_U8TEXT)
2073 DWORD written = 0, tmp;
2075 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2076 sizeof(utf8_bom)-written, &tmp, NULL))
2077 written += tmp;
2078 if (written != sizeof(utf8_bom)) {
2079 WARN("error writing BOM\n");
2080 CloseHandle(hand);
2081 msvcrt_set_errno(GetLastError());
2082 return *MSVCRT__errno();
2085 else
2087 DWORD written = 0, tmp;
2089 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2090 sizeof(utf16_bom)-written, &tmp, NULL))
2091 written += tmp;
2092 if (written != sizeof(utf16_bom))
2094 WARN("error writing BOM\n");
2095 CloseHandle(hand);
2096 msvcrt_set_errno(GetLastError());
2097 return *MSVCRT__errno();
2101 else if (access & GENERIC_READ)
2102 oflags = check_bom(hand, oflags, TRUE);
2105 *fd = msvcrt_alloc_fd(hand, wxflag);
2106 if (*fd == -1)
2107 return *MSVCRT__errno();
2109 if (oflags & MSVCRT__O_WTEXT)
2110 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2111 else if (oflags & MSVCRT__O_U16TEXT)
2112 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16;
2113 else if (oflags & MSVCRT__O_U8TEXT)
2114 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF8;
2116 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2117 return 0;
2120 /*********************************************************************
2121 * _wsopen (MSVCRT.@)
2123 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2125 int pmode;
2126 int fd;
2128 if (oflags & MSVCRT__O_CREAT)
2130 __ms_va_list ap;
2132 __ms_va_start(ap, shflags);
2133 pmode = va_arg(ap, int);
2134 __ms_va_end(ap);
2136 else
2137 pmode = 0;
2139 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
2140 return fd;
2143 /*********************************************************************
2144 * _sopen_s (MSVCRT.@)
2146 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2148 MSVCRT_wchar_t *pathW;
2149 int ret;
2151 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2152 return MSVCRT_EINVAL;
2154 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2155 MSVCRT_free(pathW);
2156 return ret;
2159 /*********************************************************************
2160 * _sopen (MSVCRT.@)
2162 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2164 int pmode;
2165 int fd;
2167 if (oflags & MSVCRT__O_CREAT)
2169 __ms_va_list ap;
2171 __ms_va_start(ap, shflags);
2172 pmode = va_arg(ap, int);
2173 __ms_va_end(ap);
2175 else
2176 pmode = 0;
2178 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2179 return fd;
2182 /*********************************************************************
2183 * _open (MSVCRT.@)
2185 int CDECL MSVCRT__open( const char *path, int flags, ... )
2187 __ms_va_list ap;
2189 if (flags & MSVCRT__O_CREAT)
2191 int pmode;
2192 __ms_va_start(ap, flags);
2193 pmode = va_arg(ap, int);
2194 __ms_va_end(ap);
2195 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2197 else
2198 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2201 /*********************************************************************
2202 * _wopen (MSVCRT.@)
2204 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2206 __ms_va_list ap;
2208 if (flags & MSVCRT__O_CREAT)
2210 int pmode;
2211 __ms_va_start(ap, flags);
2212 pmode = va_arg(ap, int);
2213 __ms_va_end(ap);
2214 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2216 else
2217 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2220 /*********************************************************************
2221 * _creat (MSVCRT.@)
2223 int CDECL MSVCRT__creat(const char *path, int flags)
2225 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2226 return MSVCRT__open(path, usedFlags);
2229 /*********************************************************************
2230 * _wcreat (MSVCRT.@)
2232 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2234 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2235 return MSVCRT__wopen(path, usedFlags);
2238 /*********************************************************************
2239 * _open_osfhandle (MSVCRT.@)
2241 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2243 DWORD flags;
2244 int fd;
2246 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2247 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2248 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2249 * text - it never sets MSVCRT__O_BINARY.
2251 /* don't let split_oflags() decide the mode if no mode is passed */
2252 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2253 oflags |= MSVCRT__O_BINARY;
2255 flags = GetFileType((HANDLE)handle);
2256 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2258 msvcrt_set_errno(GetLastError());
2259 return -1;
2262 if (flags == FILE_TYPE_CHAR)
2263 flags = WX_NOSEEK;
2264 else if (flags == FILE_TYPE_PIPE)
2265 flags = WX_PIPE;
2266 else
2267 flags = 0;
2268 flags |= split_oflags(oflags);
2270 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2271 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2272 return fd;
2275 /*********************************************************************
2276 * _rmtmp (MSVCRT.@)
2278 int CDECL MSVCRT__rmtmp(void)
2280 int num_removed = 0, i;
2281 MSVCRT_FILE *file;
2283 LOCK_FILES();
2284 for (i = 3; i < MSVCRT_stream_idx; i++) {
2285 file = msvcrt_get_file(i);
2287 if (file->_tmpfname)
2289 MSVCRT_fclose(file);
2290 num_removed++;
2293 UNLOCK_FILES();
2295 if (num_removed)
2296 TRACE(":removed (%d) temp files\n",num_removed);
2297 return num_removed;
2300 static inline int get_utf8_char_len(char ch)
2302 if((ch&0xf8) == 0xf0)
2303 return 4;
2304 else if((ch&0xf0) == 0xe0)
2305 return 3;
2306 else if((ch&0xe0) == 0xc0)
2307 return 2;
2308 return 1;
2311 /*********************************************************************
2312 * (internal) read_utf8
2314 static int read_utf8(int fd, MSVCRT_wchar_t *buf, unsigned int count)
2316 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2317 HANDLE hand = fdinfo->handle;
2318 char min_buf[4], *readbuf, lookahead;
2319 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2321 /* make the buffer big enough to hold at least one character */
2322 /* read bytes have to fit to output and lookahead buffers */
2323 count /= 2;
2324 readbuf_size = count < 4 ? 4 : count;
2325 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2326 readbuf_size = 4;
2327 readbuf = min_buf;
2330 if(fdinfo->lookahead[0] != '\n') {
2331 readbuf[pos++] = fdinfo->lookahead[0];
2332 fdinfo->lookahead[0] = '\n';
2334 if(fdinfo->lookahead[1] != '\n') {
2335 readbuf[pos++] = fdinfo->lookahead[1];
2336 fdinfo->lookahead[1] = '\n';
2338 if(fdinfo->lookahead[2] != '\n') {
2339 readbuf[pos++] = fdinfo->lookahead[2];
2340 fdinfo->lookahead[2] = '\n';
2345 /* NOTE: this case is broken in native dll, reading
2346 * sometimes fails when small buffer is passed
2348 if(count < 4) {
2349 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2350 if (GetLastError() == ERROR_BROKEN_PIPE) {
2351 fdinfo->wxflag |= WX_ATEOF;
2352 return 0;
2353 }else {
2354 msvcrt_set_errno(GetLastError());
2355 return -1;
2357 }else if(!num_read) {
2358 fdinfo->wxflag |= WX_ATEOF;
2359 return 0;
2360 }else {
2361 pos++;
2364 char_len = get_utf8_char_len(readbuf[0]);
2365 if(char_len>pos) {
2366 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2367 pos += num_read;
2370 if(readbuf[0] == '\n')
2371 fdinfo->wxflag |= WX_READNL;
2372 else
2373 fdinfo->wxflag &= ~WX_READNL;
2375 if(readbuf[0] == 0x1a) {
2376 fdinfo->wxflag |= WX_ATEOF;
2377 return 0;
2380 if(readbuf[0] == '\r') {
2381 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2382 buf[0] = '\r';
2383 else if(lookahead == '\n')
2384 buf[0] = '\n';
2385 else {
2386 buf[0] = '\r';
2387 if(fdinfo->wxflag & (WX_PIPE | WX_NOSEEK))
2388 fdinfo->lookahead[0] = lookahead;
2389 else
2390 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2392 return 2;
2395 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2396 msvcrt_set_errno(GetLastError());
2397 return -1;
2400 return num_read*2;
2403 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2404 if(pos) {
2405 num_read = 0;
2406 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2407 fdinfo->wxflag |= WX_ATEOF;
2408 if (readbuf != min_buf) MSVCRT_free(readbuf);
2409 return 0;
2410 }else {
2411 msvcrt_set_errno(GetLastError());
2412 if (readbuf != min_buf) MSVCRT_free(readbuf);
2413 return -1;
2415 }else if(!pos && !num_read) {
2416 fdinfo->wxflag |= WX_ATEOF;
2417 if (readbuf != min_buf) MSVCRT_free(readbuf);
2418 return 0;
2421 pos += num_read;
2422 if(readbuf[0] == '\n')
2423 fdinfo->wxflag |= WX_READNL;
2424 else
2425 fdinfo->wxflag &= ~WX_READNL;
2427 /* Find first byte of last character (may be incomplete) */
2428 for(i=pos-1; i>0 && i>pos-4; i--)
2429 if((readbuf[i]&0xc0) != 0x80)
2430 break;
2431 char_len = get_utf8_char_len(readbuf[i]);
2432 if(char_len+i <= pos)
2433 i += char_len;
2435 if(fdinfo->wxflag & (WX_PIPE | WX_NOSEEK)) {
2436 if(i < pos)
2437 fdinfo->lookahead[0] = readbuf[i];
2438 if(i+1 < pos)
2439 fdinfo->lookahead[1] = readbuf[i+1];
2440 if(i+2 < pos)
2441 fdinfo->lookahead[2] = readbuf[i+2];
2442 }else if(i < pos) {
2443 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2445 pos = i;
2447 for(i=0, j=0; i<pos; i++) {
2448 if(readbuf[i] == 0x1a) {
2449 fdinfo->wxflag |= WX_ATEOF;
2450 break;
2453 /* strip '\r' if followed by '\n' */
2454 if(readbuf[i] == '\r' && i+1==pos) {
2455 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2456 readbuf[j++] = '\r';
2457 }else if(lookahead == '\n' && j==0) {
2458 readbuf[j++] = '\n';
2459 }else {
2460 if(lookahead != '\n')
2461 readbuf[j++] = '\r';
2463 if(fdinfo->wxflag & (WX_PIPE | WX_NOSEEK))
2464 fdinfo->lookahead[0] = lookahead;
2465 else
2466 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2468 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2469 readbuf[j++] = readbuf[i];
2472 pos = j;
2474 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2475 msvcrt_set_errno(GetLastError());
2476 if (readbuf != min_buf) MSVCRT_free(readbuf);
2477 return -1;
2480 if (readbuf != min_buf) MSVCRT_free(readbuf);
2481 return num_read*2;
2484 /*********************************************************************
2485 * (internal) read_i
2487 * When reading \r as last character in text mode, read() positions
2488 * the file pointer on the \r character while getc() goes on to
2489 * the following \n
2491 static int read_i(int fd, void *buf, unsigned int count)
2493 DWORD num_read, utf16;
2494 char *bufstart = buf;
2495 HANDLE hand = msvcrt_fdtoh(fd);
2496 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2498 if (count == 0)
2499 return 0;
2501 if (fdinfo->wxflag & WX_ATEOF) {
2502 TRACE("already at EOF, returning 0\n");
2503 return 0;
2505 /* Don't trace small reads, it gets *very* annoying */
2506 if (count > 4)
2507 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2508 if (hand == INVALID_HANDLE_VALUE)
2510 *MSVCRT__errno() = MSVCRT_EBADF;
2511 return -1;
2514 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2515 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2517 *MSVCRT__errno() = MSVCRT_EINVAL;
2518 return -1;
2521 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2522 return read_utf8(fd, buf, count);
2524 if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL))
2526 if (fdinfo->lookahead[0] != '\n')
2528 bufstart[0] = fdinfo->lookahead[0];
2529 fdinfo->lookahead[0] = '\n';
2531 if (utf16)
2533 bufstart[1] = fdinfo->lookahead[1];
2534 fdinfo->lookahead[1] = '\n';
2537 if(count>1+utf16 && ReadFile(hand, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2538 num_read += 1+utf16;
2539 else
2540 num_read = 1+utf16;
2543 if(utf16 && (num_read&1))
2545 /* msvcr90 uses uninitialized value from the buffer in this case */
2546 /* msvcrt ignores additional data */
2547 ERR("got odd number of bytes in UTF16 mode\n");
2548 num_read--;
2551 if (count != 0 && num_read == 0)
2553 fdinfo->wxflag |= WX_ATEOF;
2554 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2556 else if (fdinfo->wxflag & WX_TEXT)
2558 DWORD i, j;
2560 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2561 fdinfo->wxflag |= WX_READNL;
2562 else
2563 fdinfo->wxflag &= ~WX_READNL;
2565 for (i=0, j=0; i<num_read; i+=1+utf16)
2567 /* in text mode, a ctrl-z signals EOF */
2568 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2570 fdinfo->wxflag |= WX_ATEOF;
2571 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2572 break;
2575 /* in text mode, strip \r if followed by \n */
2576 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2578 char lookahead[2];
2579 DWORD len;
2581 lookahead[1] = '\n';
2582 if (ReadFile(hand, lookahead, 1+utf16, &len, NULL) && len)
2584 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2586 bufstart[j++] = '\n';
2587 if(utf16) bufstart[j++] = 0;
2589 else
2591 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2593 bufstart[j++] = '\r';
2594 if(utf16) bufstart[j++] = 0;
2597 if (fdinfo->wxflag & (WX_PIPE | WX_NOSEEK))
2599 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2601 bufstart[j++] = '\n';
2602 if (utf16) bufstart[j++] = 0;
2604 else
2606 fdinfo->lookahead[0] = lookahead[0];
2607 fdinfo->lookahead[1] = lookahead[1];
2610 else
2611 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2614 else
2616 bufstart[j++] = '\r';
2617 if(utf16) bufstart[j++] = 0;
2620 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2621 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2623 bufstart[j++] = bufstart[i];
2624 if(utf16) bufstart[j++] = bufstart[i+1];
2627 num_read = j;
2630 else
2632 if (GetLastError() == ERROR_BROKEN_PIPE)
2634 TRACE(":end-of-pipe\n");
2635 fdinfo->wxflag |= WX_ATEOF;
2636 return 0;
2638 else
2640 TRACE(":failed-last error (%d)\n",GetLastError());
2641 return -1;
2645 if (count > 4)
2646 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2647 return num_read;
2650 /*********************************************************************
2651 * _read (MSVCRT.@)
2653 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2655 int num_read;
2656 num_read = read_i(fd, buf, count);
2657 return num_read;
2660 /*********************************************************************
2661 * _setmode (MSVCRT.@)
2663 int CDECL MSVCRT__setmode(int fd,int mode)
2665 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2666 if(ret==MSVCRT__O_TEXT && (msvcrt_get_ioinfo(fd)->exflag & (EF_UTF8|EF_UTF16)))
2667 ret = MSVCRT__O_WTEXT;
2669 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2670 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2671 *MSVCRT__errno() = MSVCRT_EINVAL;
2672 return -1;
2675 if(mode == MSVCRT__O_BINARY) {
2676 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2677 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2678 return ret;
2681 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2682 if(mode == MSVCRT__O_TEXT)
2683 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2684 else if(mode == MSVCRT__O_U8TEXT)
2685 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF16) | EF_UTF8;
2686 else
2687 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF8) | EF_UTF16;
2689 return ret;
2692 /*********************************************************************
2693 * _stat64 (MSVCRT.@)
2695 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2697 DWORD dw;
2698 WIN32_FILE_ATTRIBUTE_DATA hfi;
2699 unsigned short mode = ALL_S_IREAD;
2700 int plen;
2702 TRACE(":file (%s) buf(%p)\n",path,buf);
2704 plen = strlen(path);
2705 while (plen && path[plen-1]==' ')
2706 plen--;
2708 if (plen && (plen<2 || path[plen-2]!=':') &&
2709 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2711 *MSVCRT__errno() = MSVCRT_ENOENT;
2712 return -1;
2715 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2717 TRACE("failed (%d)\n",GetLastError());
2718 *MSVCRT__errno() = MSVCRT_ENOENT;
2719 return -1;
2722 memset(buf,0,sizeof(struct MSVCRT__stat64));
2724 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2725 Bon 011120: This FIXME seems incorrect
2726 Also a letter as first char isn't enough to be classified
2727 as a drive letter
2729 if (isalpha(*path)&& (*(path+1)==':'))
2730 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2731 else
2732 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2734 /* Dir, or regular file? */
2735 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2736 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2737 else
2739 mode |= MSVCRT__S_IFREG;
2740 /* executable? */
2741 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2743 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2744 (tolower(path[plen-3]) << 16);
2745 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2746 mode |= ALL_S_IEXEC;
2750 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2751 mode |= ALL_S_IWRITE;
2753 buf->st_mode = mode;
2754 buf->st_nlink = 1;
2755 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2756 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2757 buf->st_atime = dw;
2758 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2759 buf->st_mtime = buf->st_ctime = dw;
2760 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2761 (int)(buf->st_size >> 32),(int)buf->st_size,
2762 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2763 return 0;
2766 /*********************************************************************
2767 * _stati64 (MSVCRT.@)
2769 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2771 int ret;
2772 struct MSVCRT__stat64 buf64;
2774 ret = MSVCRT_stat64(path, &buf64);
2775 if (!ret)
2776 msvcrt_stat64_to_stati64(&buf64, buf);
2777 return ret;
2780 /*********************************************************************
2781 * _stat (MSVCRT.@)
2783 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2784 { int ret;
2785 struct MSVCRT__stat64 buf64;
2787 ret = MSVCRT_stat64( path, &buf64);
2788 if (!ret)
2789 msvcrt_stat64_to_stat(&buf64, buf);
2790 return ret;
2793 /*********************************************************************
2794 * _wstat64 (MSVCRT.@)
2796 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2798 DWORD dw;
2799 WIN32_FILE_ATTRIBUTE_DATA hfi;
2800 unsigned short mode = ALL_S_IREAD;
2801 int plen;
2803 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2805 plen = strlenW(path);
2806 while (plen && path[plen-1]==' ')
2807 plen--;
2809 if(plen && (plen<2 || path[plen-2]!=':') &&
2810 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2812 *MSVCRT__errno() = MSVCRT_ENOENT;
2813 return -1;
2816 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2818 TRACE("failed (%d)\n",GetLastError());
2819 *MSVCRT__errno() = MSVCRT_ENOENT;
2820 return -1;
2823 memset(buf,0,sizeof(struct MSVCRT__stat64));
2825 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2826 if (MSVCRT_iswalpha(*path))
2827 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2828 else
2829 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2831 /* Dir, or regular file? */
2832 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2833 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2834 else
2836 mode |= MSVCRT__S_IFREG;
2837 /* executable? */
2838 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2840 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2841 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2842 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2843 mode |= ALL_S_IEXEC;
2847 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2848 mode |= ALL_S_IWRITE;
2850 buf->st_mode = mode;
2851 buf->st_nlink = 1;
2852 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2853 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2854 buf->st_atime = dw;
2855 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2856 buf->st_mtime = buf->st_ctime = dw;
2857 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2858 (int)(buf->st_size >> 32),(int)buf->st_size,
2859 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2860 return 0;
2863 /*********************************************************************
2864 * _wstati64 (MSVCRT.@)
2866 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2868 int ret;
2869 struct MSVCRT__stat64 buf64;
2871 ret = MSVCRT__wstat64(path, &buf64);
2872 if (!ret)
2873 msvcrt_stat64_to_stati64(&buf64, buf);
2874 return ret;
2877 /*********************************************************************
2878 * _wstat (MSVCRT.@)
2880 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2882 int ret;
2883 struct MSVCRT__stat64 buf64;
2885 ret = MSVCRT__wstat64( path, &buf64 );
2886 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2887 return ret;
2890 /*********************************************************************
2891 * _tell (MSVCRT.@)
2893 MSVCRT_long CDECL MSVCRT__tell(int fd)
2895 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2898 /*********************************************************************
2899 * _telli64 (MSVCRT.@)
2901 __int64 CDECL _telli64(int fd)
2903 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2906 /*********************************************************************
2907 * _tempnam (MSVCRT.@)
2909 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
2911 char tmpbuf[MAX_PATH];
2912 const char *tmp_dir = MSVCRT_getenv("TMP");
2914 if (tmp_dir) dir = tmp_dir;
2916 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2917 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2919 TRACE("got name (%s)\n",tmpbuf);
2920 DeleteFileA(tmpbuf);
2921 return MSVCRT__strdup(tmpbuf);
2923 TRACE("failed (%d)\n",GetLastError());
2924 return NULL;
2927 /*********************************************************************
2928 * _wtempnam (MSVCRT.@)
2930 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2932 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
2933 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2934 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
2936 if (tmp_dir) dir = tmp_dir;
2938 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2939 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2941 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2942 DeleteFileW(tmpbuf);
2943 return MSVCRT__wcsdup(tmpbuf);
2945 TRACE("failed (%d)\n",GetLastError());
2946 return NULL;
2949 /*********************************************************************
2950 * _umask (MSVCRT.@)
2952 int CDECL MSVCRT__umask(int umask)
2954 int old_umask = MSVCRT_umask;
2955 TRACE("(%d)\n",umask);
2956 MSVCRT_umask = umask;
2957 return old_umask;
2960 /*********************************************************************
2961 * _utime64 (MSVCRT.@)
2963 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2965 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2967 if (fd > 0)
2969 int retVal = _futime64(fd, t);
2970 MSVCRT__close(fd);
2971 return retVal;
2973 return -1;
2976 /*********************************************************************
2977 * _utime32 (MSVCRT.@)
2979 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2981 if (t)
2983 struct MSVCRT___utimbuf64 t64;
2984 t64.actime = t->actime;
2985 t64.modtime = t->modtime;
2986 return _utime64( path, &t64 );
2988 else
2989 return _utime64( path, NULL );
2992 /*********************************************************************
2993 * _utime (MSVCRT.@)
2995 #ifdef _WIN64
2996 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2998 return _utime64( path, t );
3000 #else
3001 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3003 return _utime32( path, t );
3005 #endif
3007 /*********************************************************************
3008 * _wutime64 (MSVCRT.@)
3010 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3012 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3014 if (fd > 0)
3016 int retVal = _futime64(fd, t);
3017 MSVCRT__close(fd);
3018 return retVal;
3020 return -1;
3023 /*********************************************************************
3024 * _wutime32 (MSVCRT.@)
3026 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3028 if (t)
3030 struct MSVCRT___utimbuf64 t64;
3031 t64.actime = t->actime;
3032 t64.modtime = t->modtime;
3033 return _wutime64( path, &t64 );
3035 else
3036 return _wutime64( path, NULL );
3039 /*********************************************************************
3040 * _wutime (MSVCRT.@)
3042 #ifdef _WIN64
3043 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3045 return _wutime64( path, t );
3047 #else
3048 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3050 return _wutime32( path, t );
3052 #endif
3054 /*********************************************************************
3055 * _write (MSVCRT.@)
3057 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3059 DWORD num_written;
3060 ioinfo *info = msvcrt_get_ioinfo(fd);
3061 HANDLE hand = info->handle;
3063 /* Don't trace small writes, it gets *very* annoying */
3064 #if 0
3065 if (count > 32)
3066 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3067 #endif
3068 if (hand == INVALID_HANDLE_VALUE)
3070 *MSVCRT__errno() = MSVCRT_EBADF;
3071 return -1;
3074 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3076 *MSVCRT__errno() = MSVCRT_EINVAL;
3077 return -1;
3080 /* If appending, go to EOF */
3081 if (info->wxflag & WX_APPEND)
3082 MSVCRT__lseek(fd, 0, FILE_END);
3084 if (!(info->wxflag & WX_TEXT))
3086 if (WriteFile(hand, buf, count, &num_written, NULL)
3087 && (num_written == count))
3088 return num_written;
3089 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3090 hand, GetLastError());
3091 *MSVCRT__errno() = MSVCRT_ENOSPC;
3093 else
3095 unsigned int i, j, nr_lf, size;
3096 char *p = NULL;
3097 const char *q;
3098 const char *s = buf, *buf_start = buf;
3100 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3102 /* find number of \n */
3103 for (nr_lf=0, i=0; i<count; i++)
3104 if (s[i] == '\n')
3105 nr_lf++;
3106 if (nr_lf)
3108 size = count+nr_lf;
3109 if ((q = p = MSVCRT_malloc(size)))
3111 for (s = buf, i = 0, j = 0; i < count; i++)
3113 if (s[i] == '\n')
3114 p[j++] = '\r';
3115 p[j++] = s[i];
3118 else
3120 FIXME("Malloc failed\n");
3121 nr_lf = 0;
3122 size = count;
3123 q = buf;
3126 else
3128 size = count;
3129 q = buf;
3132 else if (info->exflag & EF_UTF16)
3134 for (nr_lf=0, i=0; i<count; i+=2)
3135 if (s[i]=='\n' && s[i+1]==0)
3136 nr_lf += 2;
3137 if (nr_lf)
3139 size = count+nr_lf;
3140 if ((q = p = MSVCRT_malloc(size)))
3142 for (s=buf, i=0, j=0; i<count; i++)
3144 if (s[i]=='\n' && s[i+1]==0)
3146 p[j++] = '\r';
3147 p[j++] = 0;
3149 p[j++] = s[i++];
3150 p[j++] = s[i];
3153 else
3155 FIXME("Malloc failed\n");
3156 nr_lf = 0;
3157 size = count;
3158 q = buf;
3161 else
3163 size = count;
3164 q = buf;
3167 else
3169 DWORD conv_len;
3171 for(nr_lf=0, i=0; i<count; i+=2)
3172 if (s[i]=='\n' && s[i+1]==0)
3173 nr_lf++;
3175 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3176 if(!conv_len) {
3177 msvcrt_set_errno(GetLastError());
3178 MSVCRT_free(p);
3179 return -1;
3182 size = conv_len+nr_lf;
3183 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3185 for (s=buf, i=0, j=0; i<count; i++)
3187 if (s[i]=='\n' && s[i+1]==0)
3189 p[j++] = '\r';
3190 p[j++] = 0;
3192 p[j++] = s[i++];
3193 p[j++] = s[i];
3195 q = p+count+nr_lf*2;
3196 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3197 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3199 else
3201 FIXME("Malloc failed\n");
3202 nr_lf = 0;
3203 size = count;
3204 q = buf;
3208 if (!WriteFile(hand, q, size, &num_written, NULL))
3209 num_written = -1;
3210 if(p)
3211 MSVCRT_free(p);
3212 if (num_written != size)
3214 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3215 fd, hand, GetLastError(), num_written);
3216 *MSVCRT__errno() = MSVCRT_ENOSPC;
3217 return s - buf_start;
3219 return count;
3222 return -1;
3225 /*********************************************************************
3226 * _putw (MSVCRT.@)
3228 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3230 int len;
3232 MSVCRT__lock_file(file);
3233 len = MSVCRT__write(file->_file, &val, sizeof(val));
3234 if (len == sizeof(val)) {
3235 MSVCRT__unlock_file(file);
3236 return val;
3239 file->_flag |= MSVCRT__IOERR;
3240 MSVCRT__unlock_file(file);
3241 return MSVCRT_EOF;
3244 /*********************************************************************
3245 * fclose (MSVCRT.@)
3247 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3249 int r, flag;
3251 MSVCRT__lock_file(file);
3252 flag = file->_flag;
3253 MSVCRT_free(file->_tmpfname);
3254 file->_tmpfname = NULL;
3255 /* flush stdio buffers */
3256 if(file->_flag & MSVCRT__IOWRT)
3257 MSVCRT_fflush(file);
3258 if(file->_flag & MSVCRT__IOMYBUF)
3259 MSVCRT_free(file->_base);
3261 r=MSVCRT__close(file->_file);
3263 file->_flag = 0;
3264 MSVCRT__unlock_file(file);
3266 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3269 /*********************************************************************
3270 * feof (MSVCRT.@)
3272 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3274 return file->_flag & MSVCRT__IOEOF;
3277 /*********************************************************************
3278 * ferror (MSVCRT.@)
3280 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3282 return file->_flag & MSVCRT__IOERR;
3285 /*********************************************************************
3286 * _filbuf (MSVCRT.@)
3288 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3290 unsigned char c;
3291 MSVCRT__lock_file(file);
3293 if(file->_flag & MSVCRT__IOSTRG) {
3294 MSVCRT__unlock_file(file);
3295 return MSVCRT_EOF;
3298 /* Allocate buffer if needed */
3299 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
3300 msvcrt_alloc_buffer(file);
3302 if(!(file->_flag & MSVCRT__IOREAD)) {
3303 if(file->_flag & MSVCRT__IORW)
3304 file->_flag |= MSVCRT__IOREAD;
3305 else {
3306 MSVCRT__unlock_file(file);
3307 return MSVCRT_EOF;
3311 if(file->_flag & MSVCRT__IONBF) {
3312 int r;
3313 if ((r = read_i(file->_file,&c,1)) != 1) {
3314 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3315 MSVCRT__unlock_file(file);
3316 return MSVCRT_EOF;
3319 MSVCRT__unlock_file(file);
3320 return c;
3321 } else {
3322 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
3323 if(file->_cnt<=0) {
3324 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3325 file->_cnt = 0;
3326 MSVCRT__unlock_file(file);
3327 return MSVCRT_EOF;
3330 file->_cnt--;
3331 file->_ptr = file->_base+1;
3332 c = *(unsigned char *)file->_base;
3333 MSVCRT__unlock_file(file);
3334 return c;
3338 /*********************************************************************
3339 * fgetc (MSVCRT.@)
3341 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3343 unsigned char *i;
3344 unsigned int j;
3346 MSVCRT__lock_file(file);
3347 if (file->_cnt>0) {
3348 file->_cnt--;
3349 i = (unsigned char *)file->_ptr++;
3350 j = *i;
3351 } else
3352 j = MSVCRT__filbuf(file);
3354 MSVCRT__unlock_file(file);
3355 return j;
3358 /*********************************************************************
3359 * _fgetchar (MSVCRT.@)
3361 int CDECL MSVCRT__fgetchar(void)
3363 return MSVCRT_fgetc(MSVCRT_stdin);
3366 /*********************************************************************
3367 * fgets (MSVCRT.@)
3369 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3371 int cc = MSVCRT_EOF;
3372 char * buf_start = s;
3374 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3375 file,file->_file,s,size);
3377 MSVCRT__lock_file(file);
3379 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
3381 *s++ = (char)cc;
3382 size --;
3384 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3386 TRACE(":nothing read\n");
3387 MSVCRT__unlock_file(file);
3388 return NULL;
3390 if ((cc != MSVCRT_EOF) && (size > 1))
3391 *s++ = cc;
3392 *s = '\0';
3393 TRACE(":got %s\n", debugstr_a(buf_start));
3394 MSVCRT__unlock_file(file);
3395 return buf_start;
3398 /*********************************************************************
3399 * fgetwc (MSVCRT.@)
3401 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3403 MSVCRT_wint_t ret;
3404 int ch;
3406 MSVCRT__lock_file(file);
3408 if((msvcrt_get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3409 || !(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3410 char *p;
3412 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3413 ch = MSVCRT_fgetc(file);
3414 if(ch == MSVCRT_EOF) {
3415 ret = MSVCRT_WEOF;
3416 break;
3418 *p = (char)ch;
3420 }else {
3421 char mbs[MSVCRT_MB_LEN_MAX];
3422 int len = 0;
3424 ch = MSVCRT_fgetc(file);
3425 if(ch != MSVCRT_EOF) {
3426 mbs[0] = (char)ch;
3427 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3428 ch = MSVCRT_fgetc(file);
3429 if(ch != MSVCRT_EOF) {
3430 mbs[1] = (char)ch;
3431 len = 2;
3433 }else {
3434 len = 1;
3438 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3439 ret = MSVCRT_WEOF;
3442 MSVCRT__unlock_file(file);
3443 return ret;
3446 /*********************************************************************
3447 * _getw (MSVCRT.@)
3449 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3451 char *ch;
3452 int i, k;
3453 unsigned int j;
3454 ch = (char *)&i;
3456 MSVCRT__lock_file(file);
3457 for (j=0; j<sizeof(int); j++) {
3458 k = MSVCRT_fgetc(file);
3459 if (k == MSVCRT_EOF) {
3460 file->_flag |= MSVCRT__IOEOF;
3461 MSVCRT__unlock_file(file);
3462 return EOF;
3464 ch[j] = k;
3467 MSVCRT__unlock_file(file);
3468 return i;
3471 /*********************************************************************
3472 * getwc (MSVCRT.@)
3474 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3476 return MSVCRT_fgetwc(file);
3479 /*********************************************************************
3480 * _fgetwchar (MSVCRT.@)
3482 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3484 return MSVCRT_fgetwc(MSVCRT_stdin);
3487 /*********************************************************************
3488 * getwchar (MSVCRT.@)
3490 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3492 return MSVCRT__fgetwchar();
3495 /*********************************************************************
3496 * fgetws (MSVCRT.@)
3498 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3500 MSVCRT_wint_t cc = MSVCRT_WEOF;
3501 MSVCRT_wchar_t * buf_start = s;
3503 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3504 file,file->_file,s,size);
3506 MSVCRT__lock_file(file);
3508 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
3510 *s++ = cc;
3511 size --;
3513 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3515 TRACE(":nothing read\n");
3516 MSVCRT__unlock_file(file);
3517 return NULL;
3519 if ((cc != MSVCRT_WEOF) && (size > 1))
3520 *s++ = cc;
3521 *s = 0;
3522 TRACE(":got %s\n", debugstr_w(buf_start));
3523 MSVCRT__unlock_file(file);
3524 return buf_start;
3527 /*********************************************************************
3528 * _flsbuf (MSVCRT.@)
3530 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3532 /* Flush output buffer */
3533 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3534 msvcrt_alloc_buffer(file);
3536 if(!(file->_flag & MSVCRT__IOWRT)) {
3537 if(file->_flag & MSVCRT__IORW)
3538 file->_flag |= MSVCRT__IOWRT;
3539 else
3540 return MSVCRT_EOF;
3542 if(file->_bufsiz) {
3543 int res = 0;
3545 if(file->_cnt <= 0)
3546 res = msvcrt_flush_buffer(file);
3547 if(!res) {
3548 *file->_ptr++ = c;
3549 file->_cnt--;
3552 return res ? res : c&0xff;
3553 } else {
3554 unsigned char cc=c;
3555 int len;
3556 /* set _cnt to 0 for unbuffered FILEs */
3557 file->_cnt = 0;
3558 len = MSVCRT__write(file->_file, &cc, 1);
3559 if (len == 1)
3560 return c & 0xff;
3561 file->_flag |= MSVCRT__IOERR;
3562 return MSVCRT_EOF;
3566 /*********************************************************************
3567 * fwrite (MSVCRT.@)
3569 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3571 MSVCRT_size_t wrcnt=size * nmemb;
3572 int written = 0;
3573 if (size == 0)
3574 return 0;
3576 MSVCRT__lock_file(file);
3578 while(wrcnt) {
3579 if(file->_cnt) {
3580 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3581 memcpy(file->_ptr, ptr, pcnt);
3582 file->_cnt -= pcnt;
3583 file->_ptr += pcnt;
3584 written += pcnt;
3585 wrcnt -= pcnt;
3586 ptr = (const char*)ptr + pcnt;
3587 } else if(!file->_bufsiz && (file->_flag & MSVCRT__IONBF)) {
3588 if(!(file->_flag & MSVCRT__IOWRT)) {
3589 if(file->_flag & MSVCRT__IORW)
3590 file->_flag |= MSVCRT__IOWRT;
3591 else
3592 break;
3595 if(MSVCRT__write(file->_file, ptr, wrcnt) <= 0) {
3596 file->_flag |= MSVCRT__IOERR;
3597 break;
3599 written += wrcnt;
3600 wrcnt = 0;
3601 } else {
3602 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3603 break;
3604 written++;
3605 wrcnt--;
3606 ptr = (const char*)ptr + 1;
3610 MSVCRT__unlock_file(file);
3611 return written / size;
3614 /*********************************************************************
3615 * fputwc (MSVCRT.@)
3617 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3619 MSVCRT_wchar_t mwc=wc;
3620 ioinfo *fdinfo;
3621 MSVCRT_wint_t ret;
3623 MSVCRT__lock_file(file);
3624 fdinfo = msvcrt_get_ioinfo(file->_file);
3626 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3627 char buf[MSVCRT_MB_LEN_MAX];
3628 int char_len;
3630 char_len = MSVCRT_wctomb(buf, mwc);
3631 if(char_len!=-1 && MSVCRT_fwrite(buf, char_len, 1, file)==1)
3632 ret = wc;
3633 else
3634 ret = MSVCRT_WEOF;
3635 }else if(MSVCRT_fwrite(&mwc, sizeof(mwc), 1, file) == 1) {
3636 ret = wc;
3637 }else {
3638 ret = MSVCRT_WEOF;
3641 MSVCRT__unlock_file(file);
3642 return ret;
3645 /*********************************************************************
3646 * _fputwchar (MSVCRT.@)
3648 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3650 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3653 /*********************************************************************
3654 * _wfsopen (MSVCRT.@)
3656 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
3658 MSVCRT_FILE* file;
3659 int open_flags, stream_flags, fd;
3661 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3663 /* map mode string to open() flags. "man fopen" for possibilities. */
3664 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3665 return NULL;
3667 LOCK_FILES();
3668 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3669 if (fd < 0)
3670 file = NULL;
3671 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3672 != -1)
3673 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3674 else if (file)
3676 file->_flag = 0;
3677 file = NULL;
3680 TRACE(":got (%p)\n",file);
3681 if (fd >= 0 && !file)
3682 MSVCRT__close(fd);
3683 UNLOCK_FILES();
3684 return file;
3687 /*********************************************************************
3688 * _fsopen (MSVCRT.@)
3690 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3692 MSVCRT_FILE *ret;
3693 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3695 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3696 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3697 *MSVCRT__errno() = MSVCRT_EINVAL;
3698 return NULL;
3700 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3702 MSVCRT_free(pathW);
3703 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3704 *MSVCRT__errno() = MSVCRT_EINVAL;
3705 return NULL;
3708 ret = MSVCRT__wfsopen(pathW, modeW, share);
3710 MSVCRT_free(pathW);
3711 MSVCRT_free(modeW);
3712 return ret;
3715 /*********************************************************************
3716 * fopen (MSVCRT.@)
3718 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3720 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3723 /*********************************************************************
3724 * fopen_s (MSVCRT.@)
3726 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3727 const char *filename, const char *mode)
3729 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3730 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3731 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3733 *pFile = MSVCRT_fopen(filename, mode);
3735 if(!*pFile)
3736 return *MSVCRT__errno();
3737 return 0;
3740 /*********************************************************************
3741 * _wfopen (MSVCRT.@)
3743 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3745 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3748 /*********************************************************************
3749 * _wfopen_s (MSVCRT.@)
3751 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3752 const MSVCRT_wchar_t *mode)
3754 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3755 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3756 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3758 *pFile = MSVCRT__wfopen(filename, mode);
3760 if(!*pFile)
3761 return *MSVCRT__errno();
3762 return 0;
3765 /*********************************************************************
3766 * fputc (MSVCRT.@)
3768 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3770 int res;
3772 MSVCRT__lock_file(file);
3773 if(file->_cnt>0) {
3774 *file->_ptr++=c;
3775 file->_cnt--;
3776 if (c == '\n')
3778 res = msvcrt_flush_buffer(file);
3779 MSVCRT__unlock_file(file);
3780 return res ? res : c;
3782 else {
3783 MSVCRT__unlock_file(file);
3784 return c & 0xff;
3786 } else {
3787 res = MSVCRT__flsbuf(c, file);
3788 MSVCRT__unlock_file(file);
3789 return res;
3793 /*********************************************************************
3794 * _fputchar (MSVCRT.@)
3796 int CDECL MSVCRT__fputchar(int c)
3798 return MSVCRT_fputc(c, MSVCRT_stdout);
3801 /*********************************************************************
3802 * fread (MSVCRT.@)
3804 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3806 MSVCRT_size_t rcnt=size * nmemb;
3807 MSVCRT_size_t read=0;
3808 int pread=0;
3810 if(!rcnt)
3811 return 0;
3813 MSVCRT__lock_file(file);
3815 /* first buffered data */
3816 if(file->_cnt>0) {
3817 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3818 memcpy(ptr, file->_ptr, pcnt);
3819 file->_cnt -= pcnt;
3820 file->_ptr += pcnt;
3821 read += pcnt ;
3822 rcnt -= pcnt ;
3823 ptr = (char*)ptr + pcnt;
3824 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3825 if(file->_flag & MSVCRT__IORW) {
3826 file->_flag |= MSVCRT__IOREAD;
3827 } else {
3828 MSVCRT__unlock_file(file);
3829 return 0;
3832 while(rcnt>0)
3834 int i;
3835 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && !(file->_flag & MSVCRT__IONBF)
3836 && (file->_bufsiz != 0 || msvcrt_alloc_buffer(file))) {
3837 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3838 file->_ptr = file->_base;
3839 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3840 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3841 if (i > 0 && i < file->_cnt) {
3842 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3843 file->_flag &= ~MSVCRT__IOEOF;
3845 if (i > 0) {
3846 memcpy(ptr, file->_ptr, i);
3847 file->_cnt -= i;
3848 file->_ptr += i;
3850 } else if (rcnt > UINT_MAX) {
3851 i = MSVCRT__read(file->_file, ptr, UINT_MAX);
3852 } else if (rcnt < MSVCRT_BUFSIZ) {
3853 i = MSVCRT__read(file->_file, ptr, rcnt);
3854 } else {
3855 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
3857 pread += i;
3858 rcnt -= i;
3859 ptr = (char *)ptr+i;
3860 /* expose feof condition in the flags
3861 * MFC tests file->_flag for feof, and doesn't call feof())
3863 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3864 file->_flag |= MSVCRT__IOEOF;
3865 else if (i == -1)
3867 file->_flag |= MSVCRT__IOERR;
3868 pread = 0;
3869 rcnt = 0;
3871 if (i < 1) break;
3873 read+=pread;
3874 MSVCRT__unlock_file(file);
3875 return read / size;
3879 /* fread_s - not exported in native msvcrt */
3880 MSVCRT_size_t CDECL fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
3881 MSVCRT_size_t count, MSVCRT_FILE *stream)
3883 size_t bytes_left, buf_pos;
3885 TRACE("(%p %lu %lu %lu %p\n", buf, buf_size, elem_size, count, stream);
3887 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
3888 if(buf && buf_size)
3889 memset(buf, 0, buf_size);
3890 return 0;
3892 if(!elem_size || !count) return 0;
3893 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
3894 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
3896 bytes_left = elem_size*count;
3897 buf_pos = 0;
3898 while(bytes_left) {
3899 if(stream->_cnt > 0) {
3900 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
3902 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
3903 memset(buf, 0, buf_size);
3904 return 0;
3907 MSVCRT_fread((char*)buf+buf_pos, 1, size, stream);
3908 buf_pos += size;
3909 bytes_left -= size;
3910 }else {
3911 int c = MSVCRT__filbuf(stream);
3913 if(c == EOF)
3914 break;
3916 if(!MSVCRT_CHECK_PMT_ERR(buf_size-buf_pos > 0, MSVCRT_ERANGE)) {
3917 memset(buf, 0, buf_size);
3918 return 0;
3921 ((char*)buf)[buf_pos++] = c;
3922 bytes_left--;
3926 return buf_pos/elem_size;
3929 /*********************************************************************
3930 * _wfreopen (MSVCRT.@)
3933 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3935 int open_flags, stream_flags, fd;
3937 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3939 LOCK_FILES();
3940 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3941 file = NULL;
3942 else
3944 MSVCRT_fclose(file);
3945 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3946 file = NULL;
3947 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
3948 file = NULL;
3949 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
3951 file->_flag = 0;
3952 file = NULL;
3955 UNLOCK_FILES();
3956 return file;
3959 /*********************************************************************
3960 * _wfreopen_s (MSVCRT.@)
3962 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
3963 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3965 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3966 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
3967 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3968 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3970 *pFile = MSVCRT__wfreopen(path, mode, file);
3972 if(!*pFile)
3973 return *MSVCRT__errno();
3974 return 0;
3977 /*********************************************************************
3978 * freopen (MSVCRT.@)
3981 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3983 MSVCRT_FILE *ret;
3984 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3986 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3987 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3989 MSVCRT_free(pathW);
3990 return NULL;
3993 ret = MSVCRT__wfreopen(pathW, modeW, file);
3995 MSVCRT_free(pathW);
3996 MSVCRT_free(modeW);
3997 return ret;
4000 /*********************************************************************
4001 * freopen_s (MSVCRT.@)
4003 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4004 const char *path, const char *mode, MSVCRT_FILE* file)
4006 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4007 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4008 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4009 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4011 *pFile = MSVCRT_freopen(path, mode, file);
4013 if(!*pFile)
4014 return *MSVCRT__errno();
4015 return 0;
4018 /*********************************************************************
4019 * fsetpos (MSVCRT.@)
4021 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4023 int ret;
4025 MSVCRT__lock_file(file);
4026 /* Note that all this has been lifted 'as is' from fseek */
4027 if(file->_flag & MSVCRT__IOWRT)
4028 msvcrt_flush_buffer(file);
4030 /* Discard buffered input */
4031 file->_cnt = 0;
4032 file->_ptr = file->_base;
4034 /* Reset direction of i/o */
4035 if(file->_flag & MSVCRT__IORW) {
4036 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4039 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
4040 MSVCRT__unlock_file(file);
4041 return ret;
4044 /*********************************************************************
4045 * _ftelli64 (MSVCRT.@)
4047 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4049 __int64 pos;
4051 MSVCRT__lock_file(file);
4052 pos = _telli64(file->_file);
4053 if(pos == -1) {
4054 MSVCRT__unlock_file(file);
4055 return -1;
4057 if(file->_bufsiz) {
4058 if(file->_flag & MSVCRT__IOWRT) {
4059 pos += file->_ptr - file->_base;
4061 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4062 char *p;
4064 for(p=file->_base; p<file->_ptr; p++)
4065 if(*p == '\n')
4066 pos++;
4068 } else if(!file->_cnt) { /* nothing to do */
4069 } else if(MSVCRT__lseeki64(file->_file, 0, SEEK_END)==pos) {
4070 int i;
4072 pos -= file->_cnt;
4073 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4074 for(i=0; i<file->_cnt; i++)
4075 if(file->_ptr[i] == '\n')
4076 pos--;
4078 } else {
4079 char *p;
4081 if(MSVCRT__lseeki64(file->_file, pos, SEEK_SET) != pos) {
4082 MSVCRT__unlock_file(file);
4083 return -1;
4086 pos -= file->_bufsiz;
4087 pos += file->_ptr - file->_base;
4089 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4090 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READNL)
4091 pos--;
4093 for(p=file->_base; p<file->_ptr; p++)
4094 if(*p == '\n')
4095 pos++;
4100 MSVCRT__unlock_file(file);
4101 return pos;
4104 /*********************************************************************
4105 * ftell (MSVCRT.@)
4107 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4109 return MSVCRT__ftelli64(file);
4112 /*********************************************************************
4113 * fgetpos (MSVCRT.@)
4115 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4117 *pos = MSVCRT__ftelli64(file);
4118 if(*pos == -1)
4119 return -1;
4120 return 0;
4123 /*********************************************************************
4124 * fputs (MSVCRT.@)
4126 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4128 MSVCRT_size_t len = strlen(s);
4129 int ret;
4131 MSVCRT__lock_file(file);
4132 ret = MSVCRT_fwrite(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4133 MSVCRT__unlock_file(file);
4134 return ret;
4137 /*********************************************************************
4138 * fputws (MSVCRT.@)
4140 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4142 MSVCRT_size_t i, len = strlenW(s);
4143 BOOL tmp_buf;
4144 int ret;
4146 MSVCRT__lock_file(file);
4147 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
4148 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4149 MSVCRT__unlock_file(file);
4150 return ret;
4153 tmp_buf = add_std_buffer(file);
4154 for (i=0; i<len; i++) {
4155 if(MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
4156 if(tmp_buf) remove_std_buffer(file);
4157 MSVCRT__unlock_file(file);
4158 return MSVCRT_WEOF;
4162 if(tmp_buf) remove_std_buffer(file);
4163 MSVCRT__unlock_file(file);
4164 return 0;
4167 /*********************************************************************
4168 * getchar (MSVCRT.@)
4170 int CDECL MSVCRT_getchar(void)
4172 return MSVCRT_fgetc(MSVCRT_stdin);
4175 /*********************************************************************
4176 * getc (MSVCRT.@)
4178 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4180 return MSVCRT_fgetc(file);
4183 /*********************************************************************
4184 * gets (MSVCRT.@)
4186 char * CDECL MSVCRT_gets(char *buf)
4188 int cc;
4189 char * buf_start = buf;
4191 MSVCRT__lock_file(MSVCRT_stdin);
4192 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4193 cc = MSVCRT_fgetc(MSVCRT_stdin))
4194 if(cc != '\r') *buf++ = (char)cc;
4196 *buf = '\0';
4198 TRACE("got '%s'\n", buf_start);
4199 MSVCRT__unlock_file(MSVCRT_stdin);
4200 return buf_start;
4203 /*********************************************************************
4204 * _getws (MSVCRT.@)
4206 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4208 MSVCRT_wint_t cc;
4209 MSVCRT_wchar_t* ws = buf;
4211 MSVCRT__lock_file(MSVCRT_stdin);
4212 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4213 cc = MSVCRT_fgetwc(MSVCRT_stdin))
4215 if (cc != '\r')
4216 *buf++ = (MSVCRT_wchar_t)cc;
4218 *buf = '\0';
4220 TRACE("got %s\n", debugstr_w(ws));
4221 MSVCRT__unlock_file(MSVCRT_stdin);
4222 return ws;
4225 /*********************************************************************
4226 * putc (MSVCRT.@)
4228 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4230 return MSVCRT_fputc(c, file);
4233 /*********************************************************************
4234 * putchar (MSVCRT.@)
4236 int CDECL MSVCRT_putchar(int c)
4238 return MSVCRT_fputc(c, MSVCRT_stdout);
4241 /*********************************************************************
4242 * _putwch (MSVCRT.@)
4244 int CDECL MSVCRT__putwch(int c)
4246 return MSVCRT_fputwc(c, MSVCRT_stdout);
4249 /*********************************************************************
4250 * puts (MSVCRT.@)
4252 int CDECL MSVCRT_puts(const char *s)
4254 MSVCRT_size_t len = strlen(s);
4255 int ret;
4257 MSVCRT__lock_file(MSVCRT_stdout);
4258 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4259 MSVCRT__unlock_file(MSVCRT_stdout);
4260 return MSVCRT_EOF;
4263 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4264 MSVCRT__unlock_file(MSVCRT_stdout);
4265 return ret;
4268 /*********************************************************************
4269 * _putws (MSVCRT.@)
4271 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4273 static const MSVCRT_wchar_t nl = '\n';
4274 MSVCRT_size_t len = strlenW(s);
4275 int ret;
4277 MSVCRT__lock_file(MSVCRT_stdout);
4278 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4279 MSVCRT__unlock_file(MSVCRT_stdout);
4280 return MSVCRT_EOF;
4283 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4284 MSVCRT__unlock_file(MSVCRT_stdout);
4285 return ret;
4288 /*********************************************************************
4289 * remove (MSVCRT.@)
4291 int CDECL MSVCRT_remove(const char *path)
4293 TRACE("(%s)\n",path);
4294 if (DeleteFileA(path))
4295 return 0;
4296 TRACE(":failed (%d)\n",GetLastError());
4297 msvcrt_set_errno(GetLastError());
4298 return -1;
4301 /*********************************************************************
4302 * _wremove (MSVCRT.@)
4304 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4306 TRACE("(%s)\n",debugstr_w(path));
4307 if (DeleteFileW(path))
4308 return 0;
4309 TRACE(":failed (%d)\n",GetLastError());
4310 msvcrt_set_errno(GetLastError());
4311 return -1;
4314 /*********************************************************************
4315 * rename (MSVCRT.@)
4317 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4319 TRACE(":from %s to %s\n",oldpath,newpath);
4320 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4321 return 0;
4322 TRACE(":failed (%d)\n",GetLastError());
4323 msvcrt_set_errno(GetLastError());
4324 return -1;
4327 /*********************************************************************
4328 * _wrename (MSVCRT.@)
4330 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4332 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4333 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4334 return 0;
4335 TRACE(":failed (%d)\n",GetLastError());
4336 msvcrt_set_errno(GetLastError());
4337 return -1;
4340 /*********************************************************************
4341 * setvbuf (MSVCRT.@)
4343 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4345 MSVCRT__lock_file(file);
4346 if(file->_bufsiz) {
4347 MSVCRT_free(file->_base);
4348 file->_bufsiz = 0;
4349 file->_cnt = 0;
4351 if(mode == MSVCRT__IOFBF) {
4352 file->_flag &= ~MSVCRT__IONBF;
4353 file->_base = file->_ptr = buf;
4354 if(buf) {
4355 file->_bufsiz = size;
4357 } else {
4358 file->_flag |= MSVCRT__IONBF;
4360 MSVCRT__unlock_file(file);
4361 return 0;
4364 /*********************************************************************
4365 * setbuf (MSVCRT.@)
4367 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4369 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4372 /*********************************************************************
4373 * tmpnam (MSVCRT.@)
4375 char * CDECL MSVCRT_tmpnam(char *s)
4377 char tmpstr[16];
4378 char *p;
4379 int count, size;
4381 if (!s) {
4382 thread_data_t *data = msvcrt_get_thread_data();
4384 if(!data->tmpnam_buffer)
4385 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4387 s = data->tmpnam_buffer;
4390 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4391 p = s + sprintf(s, "\\s%s.", tmpstr);
4392 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4394 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
4395 memcpy(p, tmpstr, size);
4396 p[size] = '\0';
4397 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4398 GetLastError() == ERROR_FILE_NOT_FOUND)
4399 break;
4401 return s;
4404 /*********************************************************************
4405 * _wtmpnam (MSVCRT.@)
4407 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
4409 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
4410 MSVCRT_wchar_t tmpstr[16];
4411 MSVCRT_wchar_t *p;
4412 int count, size;
4413 if (!s) {
4414 thread_data_t *data = msvcrt_get_thread_data();
4416 if(!data->wtmpnam_buffer)
4417 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4419 s = data->wtmpnam_buffer;
4422 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4423 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
4424 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4426 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
4427 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
4428 p[size] = '\0';
4429 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4430 GetLastError() == ERROR_FILE_NOT_FOUND)
4431 break;
4433 return s;
4436 /*********************************************************************
4437 * tmpfile (MSVCRT.@)
4439 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4441 char *filename = MSVCRT_tmpnam(NULL);
4442 int fd;
4443 MSVCRT_FILE* file = NULL;
4445 LOCK_FILES();
4446 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4447 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4448 if (fd != -1 && (file = msvcrt_alloc_fp()))
4450 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4452 file->_flag = 0;
4453 file = NULL;
4455 else file->_tmpfname = MSVCRT__strdup(filename);
4458 if(fd != -1 && !file)
4459 MSVCRT__close(fd);
4460 UNLOCK_FILES();
4461 return file;
4464 /*********************************************************************
4465 * tmpfile_s (MSVCRT.@)
4467 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4469 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4471 *file = MSVCRT_tmpfile();
4472 return 0;
4475 static int puts_clbk_file_a(void *file, int len, const char *str)
4477 return MSVCRT_fwrite(str, sizeof(char), len, file);
4480 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4482 int i, ret;
4484 MSVCRT__lock_file(file);
4486 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4487 ret = MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
4488 MSVCRT__unlock_file(file);
4489 return ret;
4492 for(i=0; i<len; i++) {
4493 if(MSVCRT_fputwc(str[i], file) == MSVCRT_WEOF) {
4494 MSVCRT__unlock_file(file);
4495 return -1;
4499 MSVCRT__unlock_file(file);
4500 return len;
4503 /*********************************************************************
4504 * vfprintf (MSVCRT.@)
4506 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4508 BOOL tmp_buf;
4509 int ret;
4511 MSVCRT__lock_file(file);
4512 tmp_buf = add_std_buffer(file);
4513 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4514 if(tmp_buf) remove_std_buffer(file);
4515 MSVCRT__unlock_file(file);
4517 return ret;
4520 /*********************************************************************
4521 * vfprintf_s (MSVCRT.@)
4523 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4525 BOOL tmp_buf;
4526 int ret;
4528 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4530 MSVCRT__lock_file(file);
4531 tmp_buf = add_std_buffer(file);
4532 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4533 if(tmp_buf) remove_std_buffer(file);
4534 MSVCRT__unlock_file(file);
4536 return ret;
4539 /*********************************************************************
4540 * vfwprintf (MSVCRT.@)
4542 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4544 BOOL tmp_buf;
4545 int ret;
4547 MSVCRT__lock_file(file);
4548 tmp_buf = add_std_buffer(file);
4549 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4550 if(tmp_buf) remove_std_buffer(file);
4551 MSVCRT__unlock_file(file);
4553 return ret;
4556 /*********************************************************************
4557 * vfwprintf_s (MSVCRT.@)
4559 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4561 BOOL tmp_buf;
4562 int ret;
4564 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4566 MSVCRT__lock_file(file);
4567 tmp_buf = add_std_buffer(file);
4568 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4569 if(tmp_buf) remove_std_buffer(file);
4570 MSVCRT__unlock_file(file);
4572 return ret;
4575 /*********************************************************************
4576 * _vfwprintf_l (MSVCRT.@)
4578 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
4579 MSVCRT__locale_t locale, __ms_va_list valist)
4581 BOOL tmp_buf;
4582 int ret;
4584 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4586 MSVCRT__lock_file(file);
4587 tmp_buf = add_std_buffer(file);
4588 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4589 if(tmp_buf) remove_std_buffer(file);
4590 MSVCRT__unlock_file(file);
4592 return ret;
4595 /*********************************************************************
4596 * vprintf (MSVCRT.@)
4598 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
4600 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
4603 /*********************************************************************
4604 * vprintf_s (MSVCRT.@)
4606 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
4608 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
4611 /*********************************************************************
4612 * vwprintf (MSVCRT.@)
4614 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
4616 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
4619 /*********************************************************************
4620 * vwprintf_s (MSVCRT.@)
4622 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
4624 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
4627 /*********************************************************************
4628 * fprintf (MSVCRT.@)
4630 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
4632 __ms_va_list valist;
4633 int res;
4634 __ms_va_start(valist, format);
4635 res = MSVCRT_vfprintf(file, format, valist);
4636 __ms_va_end(valist);
4637 return res;
4640 /*********************************************************************
4641 * fprintf_s (MSVCRT.@)
4643 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
4645 __ms_va_list valist;
4646 int res;
4647 __ms_va_start(valist, format);
4648 res = MSVCRT_vfprintf_s(file, format, valist);
4649 __ms_va_end(valist);
4650 return res;
4653 /*********************************************************************
4654 * fwprintf (MSVCRT.@)
4656 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4658 __ms_va_list valist;
4659 int res;
4660 __ms_va_start(valist, format);
4661 res = MSVCRT_vfwprintf(file, format, valist);
4662 __ms_va_end(valist);
4663 return res;
4666 /*********************************************************************
4667 * fwprintf_s (MSVCRT.@)
4669 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4671 __ms_va_list valist;
4672 int res;
4673 __ms_va_start(valist, format);
4674 res = MSVCRT_vfwprintf_s(file, format, valist);
4675 __ms_va_end(valist);
4676 return res;
4679 /*********************************************************************
4680 * _fwprintf_l (MSVCRT.@)
4682 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
4684 __ms_va_list valist;
4685 int res;
4686 __ms_va_start(valist, locale);
4687 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
4688 __ms_va_end(valist);
4689 return res;
4692 /*********************************************************************
4693 * printf (MSVCRT.@)
4695 int CDECL MSVCRT_printf(const char *format, ...)
4697 __ms_va_list valist;
4698 int res;
4699 __ms_va_start(valist, format);
4700 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
4701 __ms_va_end(valist);
4702 return res;
4705 /*********************************************************************
4706 * printf_s (MSVCRT.@)
4708 int CDECL MSVCRT_printf_s(const char *format, ...)
4710 __ms_va_list valist;
4711 int res;
4712 __ms_va_start(valist, format);
4713 res = MSVCRT_vprintf_s(format, valist);
4714 __ms_va_end(valist);
4715 return res;
4718 /*********************************************************************
4719 * ungetc (MSVCRT.@)
4721 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
4723 if (c == MSVCRT_EOF)
4724 return MSVCRT_EOF;
4726 MSVCRT__lock_file(file);
4727 if(file->_bufsiz == 0 && msvcrt_alloc_buffer(file))
4728 file->_ptr++;
4729 if(file->_ptr>file->_base) {
4730 file->_ptr--;
4731 if(file->_flag & MSVCRT__IOSTRG) {
4732 if(*file->_ptr != c) {
4733 file->_ptr++;
4734 MSVCRT__unlock_file(file);
4735 return MSVCRT_EOF;
4737 }else {
4738 *file->_ptr = c;
4740 file->_cnt++;
4741 MSVCRT_clearerr(file);
4742 MSVCRT__unlock_file(file);
4743 return c;
4746 MSVCRT__unlock_file(file);
4747 return MSVCRT_EOF;
4750 /*********************************************************************
4751 * ungetwc (MSVCRT.@)
4753 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
4755 MSVCRT_wchar_t mwc = wc;
4757 if (wc == MSVCRT_WEOF)
4758 return MSVCRT_WEOF;
4760 MSVCRT__lock_file(file);
4762 if((msvcrt_get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
4763 || !(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
4764 unsigned char * pp = (unsigned char *)&mwc;
4765 int i;
4767 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
4768 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
4769 MSVCRT__unlock_file(file);
4770 return MSVCRT_WEOF;
4773 }else {
4774 char mbs[MSVCRT_MB_LEN_MAX];
4775 int len;
4777 len = MSVCRT_wctomb(mbs, mwc);
4778 if(len == -1) {
4779 MSVCRT__unlock_file(file);
4780 return MSVCRT_WEOF;
4783 for(len--; len>=0; len--) {
4784 if(mbs[len] != MSVCRT_ungetc(mbs[len], file)) {
4785 MSVCRT__unlock_file(file);
4786 return MSVCRT_WEOF;
4791 MSVCRT__unlock_file(file);
4792 return mwc;
4795 /*********************************************************************
4796 * wprintf (MSVCRT.@)
4798 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
4800 __ms_va_list valist;
4801 int res;
4802 __ms_va_start(valist, format);
4803 res = MSVCRT_vwprintf(format, valist);
4804 __ms_va_end(valist);
4805 return res;
4808 /*********************************************************************
4809 * wprintf_s (MSVCRT.@)
4811 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
4813 __ms_va_list valist;
4814 int res;
4815 __ms_va_start(valist, format);
4816 res = MSVCRT_vwprintf_s(format, valist);
4817 __ms_va_end(valist);
4818 return res;
4821 /*********************************************************************
4822 * _getmaxstdio (MSVCRT.@)
4824 int CDECL MSVCRT__getmaxstdio(void)
4826 return MSVCRT_max_streams;
4829 /*********************************************************************
4830 * _setmaxstdio (MSVCRT.@)
4832 int CDECL MSVCRT__setmaxstdio(int newmax)
4834 TRACE("%d\n", newmax);
4836 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4837 return -1;
4839 MSVCRT_max_streams = newmax;
4840 return MSVCRT_max_streams;