Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / msvcrt / file.c
blob91cd44806fdda235a2c5a324fd23843c9f85a2b3
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 <time.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <sys/types.h>
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winternl.h"
43 #include "msvcrt.h"
45 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
51 /* for stat mode, permissions apply to all,owner and group */
52 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
53 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
54 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
56 /* _access() bit flags FIXME: incomplete */
57 #define MSVCRT_W_OK 0x02
58 #define MSVCRT_R_OK 0x04
60 /* values for wxflag in file descriptor */
61 #define WX_OPEN 0x01
62 #define WX_ATEOF 0x02
63 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
64 #define WX_READCR 0x08 /* underlying file is at \r */
65 #define WX_DONTINHERIT 0x10
66 #define WX_APPEND 0x20
67 #define WX_TEXT 0x80
69 /* FIXME: this should be allocated dynamically */
70 #define MSVCRT_MAX_FILES 2048
72 typedef struct {
73 HANDLE handle;
74 unsigned char wxflag;
75 DWORD unkn[7]; /* critical section and init flag */
76 } ioinfo;
78 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
80 MSVCRT_FILE MSVCRT__iob[3] = { { 0 } };
82 static int MSVCRT_fdstart = 3; /* first unallocated fd */
83 static int MSVCRT_fdend = 3; /* highest allocated fd */
85 static MSVCRT_FILE* MSVCRT_fstreams[2048];
86 static int MSVCRT_stream_idx;
88 /* INTERNAL: process umask */
89 static int MSVCRT_umask = 0;
91 /* INTERNAL: Static buffer for temp file name */
92 static char MSVCRT_tmpname[MAX_PATH];
94 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
95 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
96 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
97 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
99 #define TOUL(x) (ULONGLONG)(x)
100 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
101 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
102 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
103 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
105 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
106 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
107 * and MSVCRT_stream_idx, from race conditions.
108 * It doesn't protect against race conditions manipulating the underlying files
109 * or flags; doing so would probably be better accomplished with per-file
110 * protection, rather than locking the whole table for every change.
112 static CRITICAL_SECTION MSVCRT_file_cs;
113 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
114 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
116 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
118 buf->st_dev = buf64->st_dev;
119 buf->st_ino = buf64->st_ino;
120 buf->st_mode = buf64->st_mode;
121 buf->st_nlink = buf64->st_nlink;
122 buf->st_uid = buf64->st_uid;
123 buf->st_gid = buf64->st_gid;
124 buf->st_rdev = buf64->st_rdev;
125 buf->st_size = buf64->st_size;
126 buf->st_atime = buf64->st_atime;
127 buf->st_mtime = buf64->st_mtime;
128 buf->st_ctime = buf64->st_ctime;
131 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
133 buf->st_dev = buf64->st_dev;
134 buf->st_ino = buf64->st_ino;
135 buf->st_mode = buf64->st_mode;
136 buf->st_nlink = buf64->st_nlink;
137 buf->st_uid = buf64->st_uid;
138 buf->st_gid = buf64->st_gid;
139 buf->st_rdev = buf64->st_rdev;
140 buf->st_size = buf64->st_size;
141 buf->st_atime = buf64->st_atime;
142 buf->st_mtime = buf64->st_mtime;
143 buf->st_ctime = buf64->st_ctime;
146 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
148 /* 1601 to 1970 is 369 years plus 89 leap days */
149 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
151 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
152 ft->dwHighDateTime = ticks >> 32;
153 ft->dwLowDateTime = ticks;
156 static inline BOOL msvcrt_is_valid_fd(int fd)
158 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
161 /* INTERNAL: Get the HANDLE for a fd
162 * This doesn't lock the table, because a failure will result in
163 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
164 * it returns a valid handle which is about to be closed, a subsequent call
165 * will fail, most likely in a sane way.
167 static HANDLE msvcrt_fdtoh(int fd)
169 if (!msvcrt_is_valid_fd(fd))
171 WARN(":fd (%d) - no handle!\n",fd);
172 *MSVCRT___doserrno() = 0;
173 *MSVCRT__errno() = MSVCRT_EBADF;
174 return INVALID_HANDLE_VALUE;
176 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
177 return MSVCRT_fdesc[fd].handle;
180 /* INTERNAL: free a file entry fd */
181 static void msvcrt_free_fd(int fd)
183 HANDLE old_handle;
185 LOCK_FILES();
186 old_handle = MSVCRT_fdesc[fd].handle;
187 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
188 MSVCRT_fdesc[fd].wxflag = 0;
189 TRACE(":fd (%d) freed\n",fd);
190 if (fd < 3) /* don't use 0,1,2 for user files */
192 switch (fd)
194 case 0:
195 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
196 break;
197 case 1:
198 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
199 break;
200 case 2:
201 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
202 break;
205 else
207 if (fd == MSVCRT_fdend - 1)
208 MSVCRT_fdend--;
209 if (fd < MSVCRT_fdstart)
210 MSVCRT_fdstart = fd;
212 UNLOCK_FILES();
215 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
216 /* caller must hold the files lock */
217 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
219 if (fd >= MSVCRT_MAX_FILES)
221 WARN(":files exhausted!\n");
222 *MSVCRT__errno() = MSVCRT_ENFILE;
223 return -1;
225 MSVCRT_fdesc[fd].handle = hand;
226 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
228 /* locate next free slot */
229 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
230 MSVCRT_fdstart = MSVCRT_fdend + 1;
231 else
232 while (MSVCRT_fdstart < MSVCRT_fdend &&
233 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
234 MSVCRT_fdstart++;
235 /* update last fd in use */
236 if (fd >= MSVCRT_fdend)
237 MSVCRT_fdend = fd + 1;
238 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
240 switch (fd)
242 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
243 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
244 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
247 return fd;
250 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
251 static int msvcrt_alloc_fd(HANDLE hand, int flag)
253 int ret;
255 LOCK_FILES();
256 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
257 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
258 UNLOCK_FILES();
259 return ret;
262 /* INTERNAL: Allocate a FILE* for an fd slot */
263 /* caller must hold the files lock */
264 static MSVCRT_FILE* msvcrt_alloc_fp(void)
266 unsigned int i;
268 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
270 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
272 if (!MSVCRT_fstreams[i])
274 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
275 return NULL;
276 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
278 return MSVCRT_fstreams[i];
281 return NULL;
284 /* INTERNAL: initialize a FILE* from an open fd */
285 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
287 TRACE(":fd (%d) allocating FILE*\n",fd);
288 if (!msvcrt_is_valid_fd(fd))
290 WARN(":invalid fd %d\n",fd);
291 *MSVCRT___doserrno() = 0;
292 *MSVCRT__errno() = MSVCRT_EBADF;
293 return -1;
295 memset(file, 0, sizeof(*file));
296 file->_file = fd;
297 file->_flag = stream_flags;
299 TRACE(":got FILE* (%p)\n",file);
300 return 0;
303 /* INTERNAL: Create an inheritance data block (for spawned process)
304 * The inheritance block is made of:
305 * 00 int nb of file descriptor (NBFD)
306 * 04 char file flags (wxflag): repeated for each fd
307 * 4+NBFD HANDLE file handle: repeated for each fd
309 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
311 int fd;
312 char* wxflag_ptr;
313 HANDLE* handle_ptr;
315 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
316 *block = MSVCRT_calloc(*size, 1);
317 if (!*block)
319 *size = 0;
320 return FALSE;
322 wxflag_ptr = (char*)*block + sizeof(unsigned);
323 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
325 *(unsigned*)*block = MSVCRT_fdend;
326 for (fd = 0; fd < MSVCRT_fdend; fd++)
328 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
329 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
331 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
332 *handle_ptr = MSVCRT_fdesc[fd].handle;
334 else
336 *wxflag_ptr = 0;
337 *handle_ptr = INVALID_HANDLE_VALUE;
339 wxflag_ptr++; handle_ptr++;
341 return TRUE;
344 /* INTERNAL: Set up all file descriptors,
345 * as well as default streams (stdin, stderr and stdout)
347 void msvcrt_init_io(void)
349 STARTUPINFOA si;
350 int i;
352 InitializeCriticalSection(&MSVCRT_file_cs);
353 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
354 GetStartupInfoA(&si);
355 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
357 BYTE* wxflag_ptr;
358 HANDLE* handle_ptr;
359 unsigned int count;
361 count = *(unsigned*)si.lpReserved2;
362 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
363 handle_ptr = (HANDLE*)(wxflag_ptr + count);
365 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
366 count = min(count, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
367 for (i = 0; i < count; i++)
369 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
371 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
372 MSVCRT_fdesc[i].handle = *handle_ptr;
374 else
376 MSVCRT_fdesc[i].wxflag = 0;
377 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
379 wxflag_ptr++; handle_ptr++;
381 MSVCRT_fdend = max( 3, count );
382 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
383 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
386 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
388 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
389 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
390 GetCurrentProcess(), &MSVCRT_fdesc[0].handle,
391 0, TRUE, DUPLICATE_SAME_ACCESS))
392 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
394 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
396 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
397 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
398 GetCurrentProcess(), &MSVCRT_fdesc[1].handle,
399 0, TRUE, DUPLICATE_SAME_ACCESS))
400 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
402 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
404 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
405 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
406 GetCurrentProcess(), &MSVCRT_fdesc[2].handle,
407 0, TRUE, DUPLICATE_SAME_ACCESS))
408 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
411 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
412 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
414 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
415 for (i = 0; i < 3; i++)
417 /* FILE structs for stdin/out/err are static and never deleted */
418 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
419 MSVCRT__iob[i]._file = i;
420 MSVCRT__iob[i]._tmpfname = NULL;
421 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
423 MSVCRT_stream_idx = 3;
426 /* INTERNAL: Flush stdio file buffer */
427 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
429 if(file->_bufsiz) {
430 int cnt=file->_ptr-file->_base;
431 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
432 file->_flag |= MSVCRT__IOERR;
433 return MSVCRT_EOF;
435 file->_ptr=file->_base;
436 file->_cnt=file->_bufsiz;
438 return 0;
441 /* INTERNAL: Allocate stdio file buffer */
442 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
444 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
445 if(file->_base) {
446 file->_bufsiz = MSVCRT_BUFSIZ;
447 file->_flag |= MSVCRT__IOMYBUF;
448 } else {
449 file->_base = (char*)(&file->_charbuf);
450 /* put here 2 ??? */
451 file->_bufsiz = sizeof(file->_charbuf);
453 file->_ptr = file->_base;
454 file->_cnt = 0;
457 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
458 static void msvcrt_int_to_base32(int num, char *str)
460 char *p;
461 int n = num;
462 int digits = 0;
464 while (n != 0)
466 n >>= 5;
467 digits++;
469 p = str + digits;
470 *p = 0;
471 while (--p >= str)
473 *p = (num & 31) + '0';
474 if (*p > '9')
475 *p += ('a' - '0' - 10);
476 num >>= 5;
480 /*********************************************************************
481 * __iob_func(MSVCRT.@)
483 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
485 return &MSVCRT__iob[0];
488 /*********************************************************************
489 * _access (MSVCRT.@)
491 int CDECL MSVCRT__access(const char *filename, int mode)
493 DWORD attr = GetFileAttributesA(filename);
495 TRACE("(%s,%d) %d\n",filename,mode,attr);
497 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
499 msvcrt_set_errno(GetLastError());
500 return -1;
502 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
504 msvcrt_set_errno(ERROR_ACCESS_DENIED);
505 return -1;
507 return 0;
510 /*********************************************************************
511 * _access_s (MSVCRT.@)
513 int CDECL _access_s(const char *filename, int mode)
515 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
516 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
518 *MSVCRT__errno() = MSVCRT_EINVAL;
519 return -1;
522 return MSVCRT__access(filename, mode);
525 /*********************************************************************
526 * _waccess (MSVCRT.@)
528 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
530 DWORD attr = GetFileAttributesW(filename);
532 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
534 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
536 msvcrt_set_errno(GetLastError());
537 return -1;
539 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
541 msvcrt_set_errno(ERROR_ACCESS_DENIED);
542 return -1;
544 return 0;
547 /*********************************************************************
548 * _waccess_s (MSVCRT.@)
550 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
552 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
553 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
555 *MSVCRT__errno() = MSVCRT_EINVAL;
556 return -1;
559 return _waccess(filename, mode);
562 /*********************************************************************
563 * _chmod (MSVCRT.@)
565 int CDECL MSVCRT__chmod(const char *path, int flags)
567 DWORD oldFlags = GetFileAttributesA(path);
569 if (oldFlags != INVALID_FILE_ATTRIBUTES)
571 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
572 oldFlags | FILE_ATTRIBUTE_READONLY;
574 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
575 return 0;
577 msvcrt_set_errno(GetLastError());
578 return -1;
581 /*********************************************************************
582 * _wchmod (MSVCRT.@)
584 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
586 DWORD oldFlags = GetFileAttributesW(path);
588 if (oldFlags != INVALID_FILE_ATTRIBUTES)
590 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
591 oldFlags | FILE_ATTRIBUTE_READONLY;
593 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
594 return 0;
596 msvcrt_set_errno(GetLastError());
597 return -1;
600 /*********************************************************************
601 * _unlink (MSVCRT.@)
603 int CDECL MSVCRT__unlink(const char *path)
605 TRACE("%s\n",debugstr_a(path));
606 if(DeleteFileA(path))
607 return 0;
608 TRACE("failed (%d)\n",GetLastError());
609 msvcrt_set_errno(GetLastError());
610 return -1;
613 /*********************************************************************
614 * _wunlink (MSVCRT.@)
616 int CDECL _wunlink(const MSVCRT_wchar_t *path)
618 TRACE("(%s)\n",debugstr_w(path));
619 if(DeleteFileW(path))
620 return 0;
621 TRACE("failed (%d)\n",GetLastError());
622 msvcrt_set_errno(GetLastError());
623 return -1;
626 /* _flushall calls MSVCRT_fflush which calls _flushall */
627 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
629 /*********************************************************************
630 * _flushall (MSVCRT.@)
632 int CDECL _flushall(void)
634 int i, num_flushed = 0;
636 LOCK_FILES();
637 for (i = 3; i < MSVCRT_stream_idx; i++)
638 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
640 #if 0
641 /* FIXME: flush, do not commit */
642 if (_commit(i) == -1)
643 if (MSVCRT_fstreams[i])
644 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
645 #endif
646 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
647 MSVCRT_fflush(MSVCRT_fstreams[i]);
648 num_flushed++;
651 UNLOCK_FILES();
653 TRACE(":flushed (%d) handles\n",num_flushed);
654 return num_flushed;
657 /*********************************************************************
658 * fflush (MSVCRT.@)
660 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
662 if(!file) {
663 _flushall();
664 } else if(file->_flag & MSVCRT__IOWRT) {
665 int res=msvcrt_flush_buffer(file);
666 return res;
668 return 0;
671 /*********************************************************************
672 * _close (MSVCRT.@)
674 int CDECL MSVCRT__close(int fd)
676 HANDLE hand;
677 int ret;
679 LOCK_FILES();
680 hand = msvcrt_fdtoh(fd);
681 TRACE(":fd (%d) handle (%p)\n",fd,hand);
682 if (hand == INVALID_HANDLE_VALUE)
683 ret = -1;
684 else if (!CloseHandle(hand))
686 WARN(":failed-last error (%d)\n",GetLastError());
687 msvcrt_set_errno(GetLastError());
688 ret = -1;
690 else
692 msvcrt_free_fd(fd);
693 ret = 0;
695 UNLOCK_FILES();
696 TRACE(":ok\n");
697 return ret;
700 /*********************************************************************
701 * _commit (MSVCRT.@)
703 int CDECL _commit(int fd)
705 HANDLE hand = msvcrt_fdtoh(fd);
707 TRACE(":fd (%d) handle (%p)\n",fd,hand);
708 if (hand == INVALID_HANDLE_VALUE)
709 return -1;
711 if (!FlushFileBuffers(hand))
713 if (GetLastError() == ERROR_INVALID_HANDLE)
715 /* FlushFileBuffers fails for console handles
716 * so we ignore this error.
718 return 0;
720 TRACE(":failed-last error (%d)\n",GetLastError());
721 msvcrt_set_errno(GetLastError());
722 return -1;
724 TRACE(":ok\n");
725 return 0;
728 /*********************************************************************
729 * _dup2 (MSVCRT.@)
730 * NOTES
731 * MSDN isn't clear on this point, but the remarks for _pipe
732 * indicate file descriptors duplicated with _dup and _dup2 are always
733 * inheritable.
735 int CDECL MSVCRT__dup2(int od, int nd)
737 int ret;
739 TRACE("(od=%d, nd=%d)\n", od, nd);
740 LOCK_FILES();
741 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
743 HANDLE handle;
745 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
746 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
748 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
750 if (msvcrt_is_valid_fd(nd))
751 MSVCRT__close(nd);
752 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
753 if (ret == -1)
755 CloseHandle(handle);
756 *MSVCRT__errno() = MSVCRT_EMFILE;
758 else
760 /* _dup2 returns 0, not nd, on success */
761 ret = 0;
764 else
766 ret = -1;
767 msvcrt_set_errno(GetLastError());
770 else
772 *MSVCRT__errno() = MSVCRT_EBADF;
773 ret = -1;
775 UNLOCK_FILES();
776 return ret;
779 /*********************************************************************
780 * _dup (MSVCRT.@)
782 int CDECL MSVCRT__dup(int od)
784 int fd, ret;
786 LOCK_FILES();
787 fd = MSVCRT_fdstart;
788 if (MSVCRT__dup2(od, fd) == 0)
789 ret = fd;
790 else
791 ret = -1;
792 UNLOCK_FILES();
793 return ret;
796 /*********************************************************************
797 * _eof (MSVCRT.@)
799 int CDECL _eof(int fd)
801 DWORD curpos,endpos;
802 LONG hcurpos,hendpos;
803 HANDLE hand = msvcrt_fdtoh(fd);
805 TRACE(":fd (%d) handle (%p)\n",fd,hand);
807 if (hand == INVALID_HANDLE_VALUE)
808 return -1;
810 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
812 /* Otherwise we do it the hard way */
813 hcurpos = hendpos = 0;
814 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
815 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
817 if (curpos == endpos && hcurpos == hendpos)
819 /* FIXME: shouldn't WX_ATEOF be set here? */
820 return TRUE;
823 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
824 return FALSE;
827 /*********************************************************************
828 * _fcloseall (MSVCRT.@)
830 int CDECL MSVCRT__fcloseall(void)
832 int num_closed = 0, i;
834 LOCK_FILES();
835 for (i = 3; i < MSVCRT_stream_idx; i++)
836 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
837 !MSVCRT_fclose(MSVCRT_fstreams[i]))
838 num_closed++;
839 UNLOCK_FILES();
841 TRACE(":closed (%d) handles\n",num_closed);
842 return num_closed;
845 /* free everything on process exit */
846 void msvcrt_free_io(void)
848 MSVCRT__fcloseall();
849 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
850 * stdout, and stderr (unlike GNU), so we need to fclose() them here
851 * or they won't get flushed.
853 MSVCRT_fclose(&MSVCRT__iob[0]);
854 MSVCRT_fclose(&MSVCRT__iob[1]);
855 MSVCRT_fclose(&MSVCRT__iob[2]);
856 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
857 DeleteCriticalSection(&MSVCRT_file_cs);
860 /*********************************************************************
861 * _lseeki64 (MSVCRT.@)
863 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
865 HANDLE hand = msvcrt_fdtoh(fd);
866 LARGE_INTEGER ofs, ret;
868 TRACE(":fd (%d) handle (%p)\n",fd,hand);
869 if (hand == INVALID_HANDLE_VALUE)
870 return -1;
872 if (whence < 0 || whence > 2)
874 *MSVCRT__errno() = MSVCRT_EINVAL;
875 return -1;
878 TRACE(":fd (%d) to %s pos %s\n",
879 fd,wine_dbgstr_longlong(offset),
880 (whence==SEEK_SET)?"SEEK_SET":
881 (whence==SEEK_CUR)?"SEEK_CUR":
882 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
884 ofs.QuadPart = offset;
885 if (SetFilePointerEx(hand, ofs, &ret, whence))
887 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
888 /* FIXME: What if we seek _to_ EOF - is EOF set? */
890 return ret.QuadPart;
892 TRACE(":error-last error (%d)\n",GetLastError());
893 msvcrt_set_errno(GetLastError());
894 return -1;
897 /*********************************************************************
898 * _lseek (MSVCRT.@)
900 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
902 return MSVCRT__lseeki64(fd, offset, whence);
905 /*********************************************************************
906 * _lock_file (MSVCRT.@)
908 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
910 FIXME("(%p) stub\n",file);
913 /*********************************************************************
914 * _unlock_file (MSVCRT.@)
916 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
918 FIXME("(%p) stub\n",file);
921 /*********************************************************************
922 * _locking (MSVCRT.@)
924 * This is untested; the underlying LockFile doesn't work yet.
926 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
928 BOOL ret;
929 DWORD cur_locn;
930 HANDLE hand = msvcrt_fdtoh(fd);
932 TRACE(":fd (%d) handle (%p)\n",fd,hand);
933 if (hand == INVALID_HANDLE_VALUE)
934 return -1;
936 if (mode < 0 || mode > 4)
938 *MSVCRT__errno() = MSVCRT_EINVAL;
939 return -1;
942 TRACE(":fd (%d) by 0x%08x mode %s\n",
943 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
944 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
945 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
946 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
947 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
948 "UNKNOWN");
950 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
952 FIXME ("Seek failed\n");
953 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
954 return -1;
956 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
958 int nretry = 10;
959 ret = 1; /* just to satisfy gcc */
960 while (nretry--)
962 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
963 if (ret) break;
964 Sleep(1);
967 else if (mode == MSVCRT__LK_UNLCK)
968 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
969 else
970 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
971 /* FIXME - what about error settings? */
972 return ret ? 0 : -1;
975 /*********************************************************************
976 * _fseeki64 (MSVCRT.@)
978 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
980 /* Flush output if needed */
981 if(file->_flag & MSVCRT__IOWRT)
982 msvcrt_flush_buffer(file);
984 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
985 offset -= file->_cnt;
986 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
987 /* Black magic correction for CR removal */
988 int i;
989 for (i=0; i<file->_cnt; i++) {
990 if (file->_ptr[i] == '\n')
991 offset--;
993 /* Black magic when reading CR at buffer boundary*/
994 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
995 offset--;
998 /* Discard buffered input */
999 file->_cnt = 0;
1000 file->_ptr = file->_base;
1001 /* Reset direction of i/o */
1002 if(file->_flag & MSVCRT__IORW) {
1003 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1005 /* Clear end of file flag */
1006 file->_flag &= ~MSVCRT__IOEOF;
1007 return (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1010 /*********************************************************************
1011 * fseek (MSVCRT.@)
1013 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1015 return MSVCRT__fseeki64( file, offset, whence );
1018 /*********************************************************************
1019 * _chsize (MSVCRT.@)
1021 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1023 LONG cur, pos;
1024 HANDLE handle;
1025 BOOL ret = FALSE;
1027 TRACE("(fd=%d, size=%d)\n", fd, size);
1029 LOCK_FILES();
1031 handle = msvcrt_fdtoh(fd);
1032 if (handle != INVALID_HANDLE_VALUE)
1034 /* save the current file pointer */
1035 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1036 if (cur >= 0)
1038 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1039 if (pos >= 0)
1041 ret = SetEndOfFile(handle);
1042 if (!ret) msvcrt_set_errno(GetLastError());
1045 /* restore the file pointer */
1046 MSVCRT__lseek(fd, cur, SEEK_SET);
1050 UNLOCK_FILES();
1051 return ret ? 0 : -1;
1054 /*********************************************************************
1055 * clearerr (MSVCRT.@)
1057 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1059 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1060 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1063 /*********************************************************************
1064 * rewind (MSVCRT.@)
1066 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1068 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1069 MSVCRT_fseek(file, 0L, SEEK_SET);
1070 MSVCRT_clearerr(file);
1073 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1075 int plus = strchrW(mode, '+') != NULL;
1077 switch(*mode++)
1079 case 'R': case 'r':
1080 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1081 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1082 break;
1083 case 'W': case 'w':
1084 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1085 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1086 break;
1087 case 'A': case 'a':
1088 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1089 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1090 break;
1091 default:
1092 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1093 *MSVCRT__errno() = MSVCRT_EINVAL;
1094 return -1;
1097 while (*mode)
1098 switch (*mode++)
1100 case 'B': case 'b':
1101 *open_flags |= MSVCRT__O_BINARY;
1102 *open_flags &= ~MSVCRT__O_TEXT;
1103 break;
1104 case 'T': case 't':
1105 *open_flags |= MSVCRT__O_TEXT;
1106 *open_flags &= ~MSVCRT__O_BINARY;
1107 break;
1108 case '+':
1109 case ' ':
1110 break;
1111 default:
1112 FIXME(":unknown flag %c not supported\n",mode[-1]);
1114 return 0;
1117 /*********************************************************************
1118 * _fdopen (MSVCRT.@)
1120 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1122 MSVCRT_FILE *ret;
1123 MSVCRT_wchar_t *modeW = NULL;
1125 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1127 ret = MSVCRT__wfdopen(fd, modeW);
1129 MSVCRT_free(modeW);
1130 return ret;
1133 /*********************************************************************
1134 * _wfdopen (MSVCRT.@)
1136 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1138 int open_flags, stream_flags;
1139 MSVCRT_FILE* file;
1141 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1143 LOCK_FILES();
1144 if (!(file = msvcrt_alloc_fp()))
1145 file = NULL;
1146 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1148 file->_flag = 0;
1149 file = NULL;
1151 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1152 UNLOCK_FILES();
1154 return file;
1157 /*********************************************************************
1158 * _filelength (MSVCRT.@)
1160 LONG CDECL MSVCRT__filelength(int fd)
1162 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1163 if (curPos != -1)
1165 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1166 if (endPos != -1)
1168 if (endPos != curPos)
1169 MSVCRT__lseek(fd, curPos, SEEK_SET);
1170 return endPos;
1173 return -1;
1176 /*********************************************************************
1177 * _filelengthi64 (MSVCRT.@)
1179 __int64 CDECL MSVCRT__filelengthi64(int fd)
1181 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1182 if (curPos != -1)
1184 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1185 if (endPos != -1)
1187 if (endPos != curPos)
1188 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1189 return endPos;
1192 return -1;
1195 /*********************************************************************
1196 * _fileno (MSVCRT.@)
1198 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1200 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1201 return file->_file;
1204 /*********************************************************************
1205 * _fstat64 (MSVCRT.@)
1207 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1209 DWORD dw;
1210 DWORD type;
1211 BY_HANDLE_FILE_INFORMATION hfi;
1212 HANDLE hand = msvcrt_fdtoh(fd);
1214 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1215 if (hand == INVALID_HANDLE_VALUE)
1216 return -1;
1218 if (!buf)
1220 WARN(":failed-NULL buf\n");
1221 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1222 return -1;
1225 memset(&hfi, 0, sizeof(hfi));
1226 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1227 type = GetFileType(hand);
1228 if (type == FILE_TYPE_PIPE)
1230 buf->st_dev = buf->st_rdev = fd;
1231 buf->st_mode = S_IFIFO;
1232 buf->st_nlink = 1;
1234 else if (type == FILE_TYPE_CHAR)
1236 buf->st_dev = buf->st_rdev = fd;
1237 buf->st_mode = S_IFCHR;
1238 buf->st_nlink = 1;
1240 else /* FILE_TYPE_DISK etc. */
1242 if (!GetFileInformationByHandle(hand, &hfi))
1244 WARN(":failed-last error (%d)\n",GetLastError());
1245 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1246 return -1;
1248 buf->st_mode = S_IFREG | 0444;
1249 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1250 buf->st_mode |= 0222;
1251 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1252 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1253 buf->st_atime = dw;
1254 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1255 buf->st_mtime = buf->st_ctime = dw;
1256 buf->st_nlink = hfi.nNumberOfLinks;
1258 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1259 buf->st_mode);
1260 return 0;
1263 /*********************************************************************
1264 * _fstati64 (MSVCRT.@)
1266 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1268 int ret;
1269 struct MSVCRT__stat64 buf64;
1271 ret = MSVCRT__fstat64(fd, &buf64);
1272 if (!ret)
1273 msvcrt_stat64_to_stati64(&buf64, buf);
1274 return ret;
1277 /*********************************************************************
1278 * _fstat (MSVCRT.@)
1280 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1281 { int ret;
1282 struct MSVCRT__stat64 buf64;
1284 ret = MSVCRT__fstat64(fd, &buf64);
1285 if (!ret)
1286 msvcrt_stat64_to_stat(&buf64, buf);
1287 return ret;
1290 /*********************************************************************
1291 * _futime64 (MSVCRT.@)
1293 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1295 HANDLE hand = msvcrt_fdtoh(fd);
1296 FILETIME at, wt;
1298 if (!t)
1300 time_to_filetime( MSVCRT__time64(NULL), &at );
1301 wt = at;
1303 else
1305 time_to_filetime( t->actime, &at );
1306 time_to_filetime( t->modtime, &wt );
1309 if (!SetFileTime(hand, NULL, &at, &wt))
1311 msvcrt_set_errno(GetLastError());
1312 return -1 ;
1314 return 0;
1317 /*********************************************************************
1318 * _futime32 (MSVCRT.@)
1320 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1322 struct MSVCRT___utimbuf64 t64;
1323 t64.actime = t->actime;
1324 t64.modtime = t->modtime;
1325 return _futime64( fd, &t64 );
1328 /*********************************************************************
1329 * _futime (MSVCRT.@)
1331 #ifdef _WIN64
1332 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1334 return _futime64( fd, t );
1336 #else
1337 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1339 return _futime32( fd, t );
1341 #endif
1343 /*********************************************************************
1344 * _get_osfhandle (MSVCRT.@)
1346 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1348 HANDLE hand = msvcrt_fdtoh(fd);
1349 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1351 return (MSVCRT_intptr_t)hand;
1354 /*********************************************************************
1355 * _isatty (MSVCRT.@)
1357 int CDECL _isatty(int fd)
1359 HANDLE hand = msvcrt_fdtoh(fd);
1361 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1362 if (hand == INVALID_HANDLE_VALUE)
1363 return 0;
1365 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1368 /*********************************************************************
1369 * _mktemp (MSVCRT.@)
1371 char * CDECL _mktemp(char *pattern)
1373 int numX = 0;
1374 char *retVal = pattern;
1375 int id;
1376 char letter = 'a';
1378 while(*pattern)
1379 numX = (*pattern++ == 'X')? numX + 1 : 0;
1380 if (numX < 5)
1381 return NULL;
1382 pattern--;
1383 id = GetCurrentProcessId();
1384 numX = 6;
1385 while(numX--)
1387 int tempNum = id / 10;
1388 *pattern-- = id - (tempNum * 10) + '0';
1389 id = tempNum;
1391 pattern++;
1394 *pattern = letter++;
1395 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1396 GetLastError() == ERROR_FILE_NOT_FOUND)
1397 return retVal;
1398 } while(letter <= 'z');
1399 return NULL;
1402 /*********************************************************************
1403 * _wmktemp (MSVCRT.@)
1405 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1407 int numX = 0;
1408 MSVCRT_wchar_t *retVal = pattern;
1409 int id;
1410 MSVCRT_wchar_t letter = 'a';
1412 while(*pattern)
1413 numX = (*pattern++ == 'X')? numX + 1 : 0;
1414 if (numX < 5)
1415 return NULL;
1416 pattern--;
1417 id = GetCurrentProcessId();
1418 numX = 6;
1419 while(numX--)
1421 int tempNum = id / 10;
1422 *pattern-- = id - (tempNum * 10) + '0';
1423 id = tempNum;
1425 pattern++;
1428 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1429 GetLastError() == ERROR_FILE_NOT_FOUND)
1430 return retVal;
1431 *pattern = letter++;
1432 } while(letter != '|');
1433 return NULL;
1436 static unsigned split_oflags(unsigned oflags)
1438 int wxflags = 0;
1439 unsigned unsupp; /* until we support everything */
1441 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1442 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1443 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1444 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1445 else wxflags |= WX_TEXT; /* default to TEXT*/
1446 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1448 if ((unsupp = oflags & ~(
1449 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1450 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1451 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1452 MSVCRT__O_NOINHERIT|
1453 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1455 ERR(":unsupported oflags 0x%04x\n",unsupp);
1457 return wxflags;
1460 /*********************************************************************
1461 * _pipe (MSVCRT.@)
1463 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1465 int ret = -1;
1466 SECURITY_ATTRIBUTES sa;
1467 HANDLE readHandle, writeHandle;
1469 if (!pfds)
1471 *MSVCRT__errno() = MSVCRT_EINVAL;
1472 return -1;
1475 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1476 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1477 sa.lpSecurityDescriptor = NULL;
1478 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1480 unsigned int wxflags = split_oflags(textmode);
1481 int fd;
1483 LOCK_FILES();
1484 fd = msvcrt_alloc_fd(readHandle, wxflags);
1485 if (fd != -1)
1487 pfds[0] = fd;
1488 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1489 if (fd != -1)
1491 pfds[1] = fd;
1492 ret = 0;
1494 else
1496 MSVCRT__close(pfds[0]);
1497 CloseHandle(writeHandle);
1498 *MSVCRT__errno() = MSVCRT_EMFILE;
1501 else
1503 CloseHandle(readHandle);
1504 CloseHandle(writeHandle);
1505 *MSVCRT__errno() = MSVCRT_EMFILE;
1507 UNLOCK_FILES();
1509 else
1510 msvcrt_set_errno(GetLastError());
1512 return ret;
1515 /*********************************************************************
1516 * _sopen (MSVCRT.@)
1518 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1520 __ms_va_list ap;
1521 int pmode;
1522 DWORD access = 0, creation = 0, attrib;
1523 DWORD sharing;
1524 int wxflag = 0, fd;
1525 HANDLE hand;
1526 SECURITY_ATTRIBUTES sa;
1529 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1530 path, oflags, shflags);
1532 wxflag = split_oflags(oflags);
1533 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1535 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1536 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1537 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1540 if (oflags & MSVCRT__O_CREAT)
1542 __ms_va_start(ap, shflags);
1543 pmode = va_arg(ap, int);
1544 __ms_va_end(ap);
1546 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1547 FIXME(": pmode 0x%04x ignored\n", pmode);
1548 else
1549 WARN(": pmode 0x%04x ignored\n", pmode);
1551 if (oflags & MSVCRT__O_EXCL)
1552 creation = CREATE_NEW;
1553 else if (oflags & MSVCRT__O_TRUNC)
1554 creation = CREATE_ALWAYS;
1555 else
1556 creation = OPEN_ALWAYS;
1558 else /* no MSVCRT__O_CREAT */
1560 if (oflags & MSVCRT__O_TRUNC)
1561 creation = TRUNCATE_EXISTING;
1562 else
1563 creation = OPEN_EXISTING;
1566 switch( shflags )
1568 case MSVCRT__SH_DENYRW:
1569 sharing = 0L;
1570 break;
1571 case MSVCRT__SH_DENYWR:
1572 sharing = FILE_SHARE_READ;
1573 break;
1574 case MSVCRT__SH_DENYRD:
1575 sharing = FILE_SHARE_WRITE;
1576 break;
1577 case MSVCRT__SH_DENYNO:
1578 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1579 break;
1580 default:
1581 ERR( "Unhandled shflags 0x%x\n", shflags );
1582 return -1;
1584 attrib = FILE_ATTRIBUTE_NORMAL;
1586 if (oflags & MSVCRT__O_TEMPORARY)
1588 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1589 access |= DELETE;
1590 sharing |= FILE_SHARE_DELETE;
1593 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1594 sa.lpSecurityDescriptor = NULL;
1595 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1597 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1599 if (hand == INVALID_HANDLE_VALUE) {
1600 WARN(":failed-last error (%d)\n",GetLastError());
1601 msvcrt_set_errno(GetLastError());
1602 return -1;
1605 fd = msvcrt_alloc_fd(hand, wxflag);
1607 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1608 return fd;
1611 /*********************************************************************
1612 * _wsopen (MSVCRT.@)
1614 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1616 __ms_va_list ap;
1617 int pmode;
1618 DWORD access = 0, creation = 0, attrib;
1619 DWORD sharing;
1620 int wxflag = 0, fd;
1621 HANDLE hand;
1622 SECURITY_ATTRIBUTES sa;
1625 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1626 debugstr_w(path), oflags, shflags);
1628 wxflag = split_oflags(oflags);
1629 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1631 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1632 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1633 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1636 if (oflags & MSVCRT__O_CREAT)
1638 __ms_va_start(ap, shflags);
1639 pmode = va_arg(ap, int);
1640 __ms_va_end(ap);
1642 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1643 FIXME(": pmode 0x%04x ignored\n", pmode);
1644 else
1645 WARN(": pmode 0x%04x ignored\n", pmode);
1647 if (oflags & MSVCRT__O_EXCL)
1648 creation = CREATE_NEW;
1649 else if (oflags & MSVCRT__O_TRUNC)
1650 creation = CREATE_ALWAYS;
1651 else
1652 creation = OPEN_ALWAYS;
1654 else /* no MSVCRT__O_CREAT */
1656 if (oflags & MSVCRT__O_TRUNC)
1657 creation = TRUNCATE_EXISTING;
1658 else
1659 creation = OPEN_EXISTING;
1662 switch( shflags )
1664 case MSVCRT__SH_DENYRW:
1665 sharing = 0L;
1666 break;
1667 case MSVCRT__SH_DENYWR:
1668 sharing = FILE_SHARE_READ;
1669 break;
1670 case MSVCRT__SH_DENYRD:
1671 sharing = FILE_SHARE_WRITE;
1672 break;
1673 case MSVCRT__SH_DENYNO:
1674 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1675 break;
1676 default:
1677 ERR( "Unhandled shflags 0x%x\n", shflags );
1678 return -1;
1680 attrib = FILE_ATTRIBUTE_NORMAL;
1682 if (oflags & MSVCRT__O_TEMPORARY)
1684 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1685 access |= DELETE;
1686 sharing |= FILE_SHARE_DELETE;
1689 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1690 sa.lpSecurityDescriptor = NULL;
1691 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1693 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1695 if (hand == INVALID_HANDLE_VALUE) {
1696 WARN(":failed-last error (%d)\n",GetLastError());
1697 msvcrt_set_errno(GetLastError());
1698 return -1;
1701 fd = msvcrt_alloc_fd(hand, wxflag);
1703 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1704 return fd;
1707 /*********************************************************************
1708 * _open (MSVCRT.@)
1710 int CDECL MSVCRT__open( const char *path, int flags, ... )
1712 __ms_va_list ap;
1714 if (flags & MSVCRT__O_CREAT)
1716 int pmode;
1717 __ms_va_start(ap, flags);
1718 pmode = va_arg(ap, int);
1719 __ms_va_end(ap);
1720 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1722 else
1723 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1726 /*********************************************************************
1727 * _wopen (MSVCRT.@)
1729 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1731 __ms_va_list ap;
1733 if (flags & MSVCRT__O_CREAT)
1735 int pmode;
1736 __ms_va_start(ap, flags);
1737 pmode = va_arg(ap, int);
1738 __ms_va_end(ap);
1739 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1741 else
1742 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1745 /*********************************************************************
1746 * _creat (MSVCRT.@)
1748 int CDECL MSVCRT__creat(const char *path, int flags)
1750 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1751 return MSVCRT__open(path, usedFlags);
1754 /*********************************************************************
1755 * _wcreat (MSVCRT.@)
1757 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1759 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1760 return _wopen(path, usedFlags);
1763 /*********************************************************************
1764 * _open_osfhandle (MSVCRT.@)
1766 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1768 int fd;
1770 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1771 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1772 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1773 * text - it never sets MSVCRT__O_BINARY.
1775 /* don't let split_oflags() decide the mode if no mode is passed */
1776 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1777 oflags |= MSVCRT__O_BINARY;
1779 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1780 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1781 return fd;
1784 /*********************************************************************
1785 * _rmtmp (MSVCRT.@)
1787 int CDECL _rmtmp(void)
1789 int num_removed = 0, i;
1791 LOCK_FILES();
1792 for (i = 3; i < MSVCRT_stream_idx; i++)
1793 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1795 MSVCRT_fclose(MSVCRT_fstreams[i]);
1796 num_removed++;
1798 UNLOCK_FILES();
1800 if (num_removed)
1801 TRACE(":removed (%d) temp files\n",num_removed);
1802 return num_removed;
1805 /*********************************************************************
1806 * (internal) read_i
1808 * When reading \r as last character in text mode, read() positions
1809 * the file pointer on the \r character while getc() goes on to
1810 * the following \n
1812 static int read_i(int fd, void *buf, unsigned int count)
1814 DWORD num_read;
1815 char *bufstart = buf;
1816 HANDLE hand = msvcrt_fdtoh(fd);
1818 if (count == 0)
1819 return 0;
1821 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1822 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1823 TRACE("already at EOF, returning 0\n");
1824 return 0;
1826 /* Don't trace small reads, it gets *very* annoying */
1827 if (count > 4)
1828 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1829 if (hand == INVALID_HANDLE_VALUE)
1830 return -1;
1832 /* Reading single bytes in O_TEXT mode makes things slow
1833 * So read big chunks
1835 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1837 if (count != 0 && num_read == 0)
1839 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1840 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1842 else if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1844 DWORD i, j;
1845 if (bufstart[num_read-1] == '\r')
1847 if(count == 1)
1849 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1850 ReadFile(hand, bufstart, 1, &num_read, NULL);
1852 else
1854 MSVCRT_fdesc[fd].wxflag |= WX_READCR;
1855 num_read--;
1858 else
1859 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1860 for (i=0, j=0; i<num_read; i++)
1862 /* in text mode, a ctrl-z signals EOF */
1863 if (bufstart[i] == 0x1a)
1865 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1866 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1867 break;
1869 /* in text mode, strip \r if followed by \n.
1870 * BUG: should save state across calls somehow, so CR LF that
1871 * straddles buffer boundary gets recognized properly?
1873 if ((bufstart[i] != '\r')
1874 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1875 bufstart[j++] = bufstart[i];
1877 num_read = j;
1880 else
1882 if (GetLastError() == ERROR_BROKEN_PIPE)
1884 TRACE(":end-of-pipe\n");
1885 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1886 return 0;
1888 else
1890 TRACE(":failed-last error (%d)\n",GetLastError());
1891 return -1;
1895 if (count > 4)
1896 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1897 return num_read;
1900 /*********************************************************************
1901 * _read (MSVCRT.@)
1903 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1905 int num_read;
1906 num_read = read_i(fd, buf, count);
1907 return num_read;
1910 /*********************************************************************
1911 * _setmode (MSVCRT.@)
1913 int CDECL _setmode(int fd,int mode)
1915 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1916 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1917 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1918 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1919 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1920 else
1921 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1922 return ret;
1925 /*********************************************************************
1926 * _stat64 (MSVCRT.@)
1928 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
1930 DWORD dw;
1931 WIN32_FILE_ATTRIBUTE_DATA hfi;
1932 unsigned short mode = ALL_S_IREAD;
1933 int plen;
1935 TRACE(":file (%s) buf(%p)\n",path,buf);
1937 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1939 TRACE("failed (%d)\n",GetLastError());
1940 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1941 return -1;
1944 memset(buf,0,sizeof(struct MSVCRT__stat64));
1946 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1947 Bon 011120: This FIXME seems incorrect
1948 Also a letter as first char isn't enough to be classified
1949 as a drive letter
1951 if (isalpha(*path)&& (*(path+1)==':'))
1952 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1953 else
1954 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1956 plen = strlen(path);
1958 /* Dir, or regular file? */
1959 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1960 (path[plen-1] == '\\'))
1961 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1962 else
1964 mode |= MSVCRT__S_IFREG;
1965 /* executable? */
1966 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1968 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1969 (tolower(path[plen-3]) << 16);
1970 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1971 mode |= ALL_S_IEXEC;
1975 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1976 mode |= ALL_S_IWRITE;
1978 buf->st_mode = mode;
1979 buf->st_nlink = 1;
1980 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1981 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1982 buf->st_atime = dw;
1983 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1984 buf->st_mtime = buf->st_ctime = dw;
1985 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
1986 (int)(buf->st_size >> 32),(int)buf->st_size,
1987 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
1988 return 0;
1991 /*********************************************************************
1992 * _stati64 (MSVCRT.@)
1994 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
1996 int ret;
1997 struct MSVCRT__stat64 buf64;
1999 ret = MSVCRT_stat64(path, &buf64);
2000 if (!ret)
2001 msvcrt_stat64_to_stati64(&buf64, buf);
2002 return ret;
2005 /*********************************************************************
2006 * _stat (MSVCRT.@)
2008 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2009 { int ret;
2010 struct MSVCRT__stat64 buf64;
2012 ret = MSVCRT_stat64( path, &buf64);
2013 if (!ret)
2014 msvcrt_stat64_to_stat(&buf64, buf);
2015 return ret;
2018 /*********************************************************************
2019 * _wstat64 (MSVCRT.@)
2021 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2023 DWORD dw;
2024 WIN32_FILE_ATTRIBUTE_DATA hfi;
2025 unsigned short mode = ALL_S_IREAD;
2026 int plen;
2028 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2030 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2032 TRACE("failed (%d)\n",GetLastError());
2033 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2034 return -1;
2037 memset(buf,0,sizeof(struct MSVCRT__stat64));
2039 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2040 if (MSVCRT_iswalpha(*path))
2041 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2042 else
2043 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2045 plen = strlenW(path);
2047 /* Dir, or regular file? */
2048 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2049 (path[plen-1] == '\\'))
2050 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2051 else
2053 mode |= MSVCRT__S_IFREG;
2054 /* executable? */
2055 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2057 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2058 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2059 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2060 mode |= ALL_S_IEXEC;
2064 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2065 mode |= ALL_S_IWRITE;
2067 buf->st_mode = mode;
2068 buf->st_nlink = 1;
2069 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2070 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2071 buf->st_atime = dw;
2072 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2073 buf->st_mtime = buf->st_ctime = dw;
2074 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2075 (int)(buf->st_size >> 32),(int)buf->st_size,
2076 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2077 return 0;
2080 /*********************************************************************
2081 * _wstati64 (MSVCRT.@)
2083 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2085 int ret;
2086 struct MSVCRT__stat64 buf64;
2088 ret = MSVCRT__wstat64(path, &buf64);
2089 if (!ret)
2090 msvcrt_stat64_to_stati64(&buf64, buf);
2091 return ret;
2094 /*********************************************************************
2095 * _wstat (MSVCRT.@)
2097 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2099 int ret;
2100 struct MSVCRT__stat64 buf64;
2102 ret = MSVCRT__wstat64( path, &buf64 );
2103 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2104 return ret;
2107 /*********************************************************************
2108 * _tell (MSVCRT.@)
2110 MSVCRT_long CDECL MSVCRT__tell(int fd)
2112 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2115 /*********************************************************************
2116 * _telli64 (MSVCRT.@)
2118 __int64 CDECL _telli64(int fd)
2120 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2123 /*********************************************************************
2124 * _tempnam (MSVCRT.@)
2126 char * CDECL _tempnam(const char *dir, const char *prefix)
2128 char tmpbuf[MAX_PATH];
2129 const char *tmp_dir = MSVCRT_getenv("TMP");
2131 if (tmp_dir) dir = tmp_dir;
2133 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2134 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2136 TRACE("got name (%s)\n",tmpbuf);
2137 DeleteFileA(tmpbuf);
2138 return _strdup(tmpbuf);
2140 TRACE("failed (%d)\n",GetLastError());
2141 return NULL;
2144 /*********************************************************************
2145 * _wtempnam (MSVCRT.@)
2147 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2149 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2151 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2152 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2154 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2155 DeleteFileW(tmpbuf);
2156 return _wcsdup(tmpbuf);
2158 TRACE("failed (%d)\n",GetLastError());
2159 return NULL;
2162 /*********************************************************************
2163 * _umask (MSVCRT.@)
2165 int CDECL MSVCRT__umask(int umask)
2167 int old_umask = MSVCRT_umask;
2168 TRACE("(%d)\n",umask);
2169 MSVCRT_umask = umask;
2170 return old_umask;
2173 /*********************************************************************
2174 * _utime64 (MSVCRT.@)
2176 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2178 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2180 if (fd > 0)
2182 int retVal = _futime64(fd, t);
2183 MSVCRT__close(fd);
2184 return retVal;
2186 return -1;
2189 /*********************************************************************
2190 * _utime32 (MSVCRT.@)
2192 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2194 struct MSVCRT___utimbuf64 t64;
2195 t64.actime = t->actime;
2196 t64.modtime = t->modtime;
2197 return _utime64( path, &t64 );
2200 /*********************************************************************
2201 * _utime (MSVCRT.@)
2203 #ifdef _WIN64
2204 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2206 return _utime64( path, t );
2208 #else
2209 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2211 return _utime32( path, t );
2213 #endif
2215 /*********************************************************************
2216 * _wutime64 (MSVCRT.@)
2218 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2220 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2222 if (fd > 0)
2224 int retVal = _futime64(fd, t);
2225 MSVCRT__close(fd);
2226 return retVal;
2228 return -1;
2231 /*********************************************************************
2232 * _wutime32 (MSVCRT.@)
2234 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2236 struct MSVCRT___utimbuf64 t64;
2237 t64.actime = t->actime;
2238 t64.modtime = t->modtime;
2239 return _wutime64( path, &t64 );
2242 /*********************************************************************
2243 * _wutime (MSVCRT.@)
2245 #ifdef _WIN64
2246 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2248 return _wutime64( path, t );
2250 #else
2251 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2253 return _wutime32( path, t );
2255 #endif
2257 /*********************************************************************
2258 * _write (MSVCRT.@)
2260 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2262 DWORD num_written;
2263 HANDLE hand = msvcrt_fdtoh(fd);
2265 /* Don't trace small writes, it gets *very* annoying */
2266 #if 0
2267 if (count > 32)
2268 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2269 #endif
2270 if (hand == INVALID_HANDLE_VALUE)
2272 *MSVCRT__errno() = MSVCRT_EBADF;
2273 return -1;
2276 /* If appending, go to EOF */
2277 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2278 MSVCRT__lseek(fd, 0, FILE_END);
2280 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2282 if (WriteFile(hand, buf, count, &num_written, NULL)
2283 && (num_written == count))
2284 return num_written;
2285 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2286 hand, GetLastError());
2287 *MSVCRT__errno() = MSVCRT_ENOSPC;
2289 else
2291 unsigned int i, j, nr_lf;
2292 char *p = NULL;
2293 const char *q;
2294 const char *s = buf, *buf_start = buf;
2295 /* find number of \n ( without preceding \r ) */
2296 for ( nr_lf=0,i = 0; i <count; i++)
2298 if (s[i]== '\n')
2300 nr_lf++;
2301 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2304 if (nr_lf)
2306 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2308 for (s = buf, i = 0, j = 0; i < count; i++)
2310 if (s[i]== '\n')
2312 p[j++] = '\r';
2313 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2315 p[j++] = s[i];
2318 else
2320 FIXME("Malloc failed\n");
2321 nr_lf =0;
2322 q = buf;
2325 else
2326 q = buf;
2328 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2330 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2331 fd, hand, GetLastError(), num_written);
2332 *MSVCRT__errno() = MSVCRT_ENOSPC;
2333 if(nr_lf)
2334 MSVCRT_free(p);
2335 return s - buf_start;
2337 else
2339 if(nr_lf)
2340 MSVCRT_free(p);
2341 return count;
2344 return -1;
2347 /*********************************************************************
2348 * _putw (MSVCRT.@)
2350 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2352 int len;
2353 len = MSVCRT__write(file->_file, &val, sizeof(val));
2354 if (len == sizeof(val)) return val;
2355 file->_flag |= MSVCRT__IOERR;
2356 return MSVCRT_EOF;
2359 /*********************************************************************
2360 * fclose (MSVCRT.@)
2362 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2364 int r, flag;
2366 flag = file->_flag;
2367 MSVCRT_free(file->_tmpfname);
2368 file->_tmpfname = NULL;
2369 /* flush stdio buffers */
2370 if(file->_flag & MSVCRT__IOWRT)
2371 MSVCRT_fflush(file);
2372 if(file->_flag & MSVCRT__IOMYBUF)
2373 MSVCRT_free(file->_base);
2375 r=MSVCRT__close(file->_file);
2377 file->_flag = 0;
2379 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2382 /*********************************************************************
2383 * feof (MSVCRT.@)
2385 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2387 return file->_flag & MSVCRT__IOEOF;
2390 /*********************************************************************
2391 * ferror (MSVCRT.@)
2393 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2395 return file->_flag & MSVCRT__IOERR;
2398 /*********************************************************************
2399 * _filbuf (MSVCRT.@)
2401 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2403 /* Allocate buffer if needed */
2404 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2405 msvcrt_alloc_buffer(file);
2407 if(!(file->_flag & MSVCRT__IOREAD)) {
2408 if(file->_flag & MSVCRT__IORW) {
2409 file->_flag |= MSVCRT__IOREAD;
2410 } else {
2411 return MSVCRT_EOF;
2414 if(file->_flag & MSVCRT__IONBF) {
2415 unsigned char c;
2416 int r;
2417 if ((r = read_i(file->_file,&c,1)) != 1) {
2418 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2419 return MSVCRT_EOF;
2421 return c;
2422 } else {
2423 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2424 if(file->_cnt<=0) {
2425 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2426 file->_cnt = 0;
2427 return MSVCRT_EOF;
2429 file->_cnt--;
2430 file->_ptr = file->_base+1;
2431 return *(unsigned char *)file->_base;
2435 /*********************************************************************
2436 * fgetc (MSVCRT.@)
2438 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2440 unsigned char *i;
2441 unsigned int j;
2442 if (file->_cnt>0) {
2443 file->_cnt--;
2444 i = (unsigned char *)file->_ptr++;
2445 j = *i;
2446 } else
2447 j = MSVCRT__filbuf(file);
2448 return j;
2451 /*********************************************************************
2452 * _fgetchar (MSVCRT.@)
2454 int CDECL _fgetchar(void)
2456 return MSVCRT_fgetc(MSVCRT_stdin);
2459 /*********************************************************************
2460 * fgets (MSVCRT.@)
2462 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2464 int cc = MSVCRT_EOF;
2465 char * buf_start = s;
2467 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2468 file,file->_file,s,size);
2470 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2472 *s++ = (char)cc;
2473 size --;
2475 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2477 TRACE(":nothing read\n");
2478 return NULL;
2480 if ((cc != MSVCRT_EOF) && (size > 1))
2481 *s++ = cc;
2482 *s = '\0';
2483 TRACE(":got %s\n", debugstr_a(buf_start));
2484 return buf_start;
2487 /*********************************************************************
2488 * fgetwc (MSVCRT.@)
2490 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2491 * the CR from CR/LF combinations
2493 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2495 char c;
2497 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2499 MSVCRT_wchar_t wc;
2500 unsigned int i;
2501 int j;
2502 char *chp, *wcp;
2503 wcp = (char *)&wc;
2504 for(i=0; i<sizeof(wc); i++)
2506 if (file->_cnt>0)
2508 file->_cnt--;
2509 chp = file->_ptr++;
2510 wcp[i] = *chp;
2512 else
2514 j = MSVCRT__filbuf(file);
2515 if(file->_cnt<=0)
2517 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2518 file->_cnt = 0;
2519 return MSVCRT_WEOF;
2521 wcp[i] = j;
2524 return wc;
2527 c = MSVCRT_fgetc(file);
2528 if ((get_locale()->locinfo->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2530 FIXME("Treat Multibyte characters\n");
2532 if (c == MSVCRT_EOF)
2533 return MSVCRT_WEOF;
2534 else
2535 return (MSVCRT_wint_t)c;
2538 /*********************************************************************
2539 * _getw (MSVCRT.@)
2541 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2543 char *ch;
2544 int i, k;
2545 unsigned int j;
2546 ch = (char *)&i;
2547 for (j=0; j<sizeof(int); j++) {
2548 k = MSVCRT_fgetc(file);
2549 if (k == MSVCRT_EOF) {
2550 file->_flag |= MSVCRT__IOEOF;
2551 return EOF;
2553 ch[j] = k;
2555 return i;
2558 /*********************************************************************
2559 * getwc (MSVCRT.@)
2561 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2563 return MSVCRT_fgetwc(file);
2566 /*********************************************************************
2567 * _fgetwchar (MSVCRT.@)
2569 MSVCRT_wint_t CDECL _fgetwchar(void)
2571 return MSVCRT_fgetwc(MSVCRT_stdin);
2574 /*********************************************************************
2575 * getwchar (MSVCRT.@)
2577 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2579 return _fgetwchar();
2582 /*********************************************************************
2583 * fgetws (MSVCRT.@)
2585 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2587 int cc = MSVCRT_WEOF;
2588 MSVCRT_wchar_t * buf_start = s;
2590 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2591 file,file->_file,s,size);
2593 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2595 *s++ = (char)cc;
2596 size --;
2598 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2600 TRACE(":nothing read\n");
2601 return NULL;
2603 if ((cc != MSVCRT_WEOF) && (size > 1))
2604 *s++ = cc;
2605 *s = 0;
2606 TRACE(":got %s\n", debugstr_w(buf_start));
2607 return buf_start;
2610 /*********************************************************************
2611 * fwrite (MSVCRT.@)
2613 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2615 MSVCRT_size_t wrcnt=size * nmemb;
2616 int written = 0;
2617 if (size == 0)
2618 return 0;
2619 if(file->_cnt) {
2620 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2621 memcpy(file->_ptr, ptr, pcnt);
2622 file->_cnt -= pcnt;
2623 file->_ptr += pcnt;
2624 written = pcnt;
2625 wrcnt -= pcnt;
2626 ptr = (const char*)ptr + pcnt;
2627 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2628 if(file->_flag & MSVCRT__IORW) {
2629 file->_flag |= MSVCRT__IOWRT;
2630 } else
2631 return 0;
2633 if(wrcnt) {
2634 /* Flush buffer */
2635 int res=msvcrt_flush_buffer(file);
2636 if(!res) {
2637 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2638 if (pwritten <= 0)
2640 file->_flag |= MSVCRT__IOERR;
2641 pwritten=0;
2643 written += pwritten;
2646 return written / size;
2649 /*********************************************************************
2650 * fputwc (MSVCRT.@)
2652 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2654 MSVCRT_wchar_t mwc=wc;
2655 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2656 return MSVCRT_WEOF;
2657 return wc;
2660 /*********************************************************************
2661 * _fputwchar (MSVCRT.@)
2663 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2665 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2668 /*********************************************************************
2669 * _wfsopen (MSVCRT.@)
2671 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2673 MSVCRT_FILE* file;
2674 int open_flags, stream_flags, fd;
2676 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2678 /* map mode string to open() flags. "man fopen" for possibilities. */
2679 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2680 return NULL;
2682 LOCK_FILES();
2683 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2684 if (fd < 0)
2685 file = NULL;
2686 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2687 != -1)
2688 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2689 else if (file)
2691 file->_flag = 0;
2692 file = NULL;
2695 TRACE(":got (%p)\n",file);
2696 if (fd >= 0 && !file)
2697 MSVCRT__close(fd);
2698 UNLOCK_FILES();
2699 return file;
2702 /*********************************************************************
2703 * _fsopen (MSVCRT.@)
2705 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2707 MSVCRT_FILE *ret;
2708 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2710 if (path && !(pathW = msvcrt_wstrdupa(path))) {
2711 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2712 *MSVCRT__errno() = MSVCRT_EINVAL;
2713 return NULL;
2715 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2717 MSVCRT_free(pathW);
2718 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2719 *MSVCRT__errno() = MSVCRT_EINVAL;
2720 return NULL;
2723 ret = MSVCRT__wfsopen(pathW, modeW, share);
2725 MSVCRT_free(pathW);
2726 MSVCRT_free(modeW);
2727 return ret;
2730 /*********************************************************************
2731 * fopen (MSVCRT.@)
2733 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2735 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2738 /*********************************************************************
2739 * fopen_s (MSVCRT.@)
2741 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
2742 const char *filename, const char *mode)
2744 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2745 !MSVCRT_CHECK_PMT(mode != NULL)) {
2746 *MSVCRT__errno() = MSVCRT_EINVAL;
2747 return MSVCRT_EINVAL;
2750 *pFile = MSVCRT_fopen(filename, mode);
2752 if(!*pFile)
2753 return *MSVCRT__errno();
2754 return 0;
2757 /*********************************************************************
2758 * _wfopen (MSVCRT.@)
2760 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2762 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2765 /*********************************************************************
2766 * _wfopen_s (MSVCRT.@)
2768 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
2769 const MSVCRT_wchar_t *mode)
2771 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2772 !MSVCRT_CHECK_PMT(mode != NULL)) {
2773 *MSVCRT__errno() = MSVCRT_EINVAL;
2774 return MSVCRT_EINVAL;
2777 *pFile = MSVCRT__wfopen(filename, mode);
2779 if(!*pFile)
2780 return *MSVCRT__errno();
2781 return 0;
2784 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2785 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2787 /*********************************************************************
2788 * fputc (MSVCRT.@)
2790 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2792 if(file->_cnt>0) {
2793 *file->_ptr++=c;
2794 file->_cnt--;
2795 if (c == '\n')
2797 int res = msvcrt_flush_buffer(file);
2798 return res ? res : c;
2800 else
2801 return c & 0xff;
2802 } else {
2803 return MSVCRT__flsbuf(c, file);
2807 /*********************************************************************
2808 * _flsbuf (MSVCRT.@)
2810 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2812 /* Flush output buffer */
2813 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2814 msvcrt_alloc_buffer(file);
2816 if(!(file->_flag & MSVCRT__IOWRT)) {
2817 if(file->_flag & MSVCRT__IORW) {
2818 file->_flag |= MSVCRT__IOWRT;
2819 } else {
2820 return MSVCRT_EOF;
2823 if(file->_bufsiz) {
2824 int res=msvcrt_flush_buffer(file);
2825 return res?res : MSVCRT_fputc(c, file);
2826 } else {
2827 unsigned char cc=c;
2828 int len;
2829 /* set _cnt to 0 for unbuffered FILEs */
2830 file->_cnt = 0;
2831 len = MSVCRT__write(file->_file, &cc, 1);
2832 if (len == 1) return c & 0xff;
2833 file->_flag |= MSVCRT__IOERR;
2834 return MSVCRT_EOF;
2838 /*********************************************************************
2839 * _fputchar (MSVCRT.@)
2841 int CDECL _fputchar(int c)
2843 return MSVCRT_fputc(c, MSVCRT_stdout);
2846 /*********************************************************************
2847 * fread (MSVCRT.@)
2849 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2850 { MSVCRT_size_t rcnt=size * nmemb;
2851 MSVCRT_size_t read=0;
2852 int pread=0;
2854 if(!rcnt)
2855 return 0;
2857 /* first buffered data */
2858 if(file->_cnt>0) {
2859 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2860 memcpy(ptr, file->_ptr, pcnt);
2861 file->_cnt -= pcnt;
2862 file->_ptr += pcnt;
2863 read += pcnt ;
2864 rcnt -= pcnt ;
2865 ptr = (char*)ptr + pcnt;
2866 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2867 if(file->_flag & MSVCRT__IORW) {
2868 file->_flag |= MSVCRT__IOREAD;
2869 } else
2870 return 0;
2872 while(rcnt>0)
2874 int i;
2875 /* Fill the buffer on small reads.
2876 * TODO: Use a better buffering strategy.
2878 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2879 if (file->_bufsiz == 0) {
2880 msvcrt_alloc_buffer(file);
2882 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2883 file->_ptr = file->_base;
2884 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2885 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2886 if (i > 0 && i < file->_cnt) {
2887 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2888 file->_flag &= ~MSVCRT__IOEOF;
2890 if (i > 0) {
2891 memcpy(ptr, file->_ptr, i);
2892 file->_cnt -= i;
2893 file->_ptr += i;
2895 } else {
2896 i = MSVCRT__read(file->_file,ptr, rcnt);
2898 pread += i;
2899 rcnt -= i;
2900 ptr = (char *)ptr+i;
2901 /* expose feof condition in the flags
2902 * MFC tests file->_flag for feof, and doesn't call feof())
2904 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2905 file->_flag |= MSVCRT__IOEOF;
2906 else if (i == -1)
2908 file->_flag |= MSVCRT__IOERR;
2909 pread = 0;
2910 rcnt = 0;
2912 if (i < 1) break;
2914 read+=pread;
2915 return read / size;
2918 /*********************************************************************
2919 * _wfreopen (MSVCRT.@)
2922 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
2924 int open_flags, stream_flags, fd;
2926 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2928 LOCK_FILES();
2929 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2930 file = NULL;
2931 else
2933 MSVCRT_fclose(file);
2934 /* map mode string to open() flags. "man fopen" for possibilities. */
2935 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2936 file = NULL;
2937 else
2939 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2940 if (fd < 0)
2941 file = NULL;
2942 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2944 file->_flag = 0;
2945 WARN(":failed-last error (%d)\n",GetLastError());
2946 msvcrt_set_errno(GetLastError());
2947 file = NULL;
2951 UNLOCK_FILES();
2952 return file;
2955 /*********************************************************************
2956 * freopen (MSVCRT.@)
2959 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
2961 MSVCRT_FILE *ret;
2962 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2964 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2965 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2967 MSVCRT_free(pathW);
2968 return NULL;
2971 ret = MSVCRT__wfreopen(pathW, modeW, file);
2973 MSVCRT_free(pathW);
2974 MSVCRT_free(modeW);
2975 return ret;
2978 /*********************************************************************
2979 * fsetpos (MSVCRT.@)
2981 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2983 /* Note that all this has been lifted 'as is' from fseek */
2984 if(file->_flag & MSVCRT__IOWRT)
2985 msvcrt_flush_buffer(file);
2987 /* Discard buffered input */
2988 file->_cnt = 0;
2989 file->_ptr = file->_base;
2991 /* Reset direction of i/o */
2992 if(file->_flag & MSVCRT__IORW) {
2993 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2996 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2999 /*********************************************************************
3000 * _ftelli64 (MSVCRT.@)
3002 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3004 /* TODO: just call fgetpos and return lower half of result */
3005 int off=0;
3006 __int64 pos;
3007 pos = _telli64(file->_file);
3008 if(pos == -1) return -1;
3009 if(file->_bufsiz) {
3010 if( file->_flag & MSVCRT__IOWRT ) {
3011 off = file->_ptr - file->_base;
3012 } else {
3013 off = -file->_cnt;
3014 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3015 /* Black magic correction for CR removal */
3016 int i;
3017 for (i=0; i<file->_cnt; i++) {
3018 if (file->_ptr[i] == '\n')
3019 off--;
3021 /* Black magic when reading CR at buffer boundary*/
3022 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3023 off--;
3028 return off + pos;
3031 /*********************************************************************
3032 * ftell (MSVCRT.@)
3034 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3036 return MSVCRT__ftelli64(file);
3039 /*********************************************************************
3040 * fgetpos (MSVCRT.@)
3042 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3044 int off=0;
3045 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3046 if(*pos == -1) return -1;
3047 if(file->_bufsiz) {
3048 if( file->_flag & MSVCRT__IOWRT ) {
3049 off = file->_ptr - file->_base;
3050 } else {
3051 off = -file->_cnt;
3052 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3053 /* Black magic correction for CR removal */
3054 int i;
3055 for (i=0; i<file->_cnt; i++) {
3056 if (file->_ptr[i] == '\n')
3057 off--;
3059 /* Black magic when reading CR at buffer boundary*/
3060 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3061 off--;
3065 *pos += off;
3066 return 0;
3069 /*********************************************************************
3070 * fputs (MSVCRT.@)
3072 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3074 MSVCRT_size_t i, len = strlen(s);
3075 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3076 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3077 for (i=0; i<len; i++)
3078 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
3079 return MSVCRT_EOF;
3080 return 0;
3083 /*********************************************************************
3084 * fputws (MSVCRT.@)
3086 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3088 MSVCRT_size_t i, len = strlenW(s);
3089 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3090 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3091 for (i=0; i<len; i++)
3093 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3094 return MSVCRT_WEOF;
3095 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
3096 return MSVCRT_WEOF;
3098 return 0;
3101 /*********************************************************************
3102 * getchar (MSVCRT.@)
3104 int CDECL MSVCRT_getchar(void)
3106 return MSVCRT_fgetc(MSVCRT_stdin);
3109 /*********************************************************************
3110 * getc (MSVCRT.@)
3112 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3114 return MSVCRT_fgetc(file);
3117 /*********************************************************************
3118 * gets (MSVCRT.@)
3120 char * CDECL MSVCRT_gets(char *buf)
3122 int cc;
3123 char * buf_start = buf;
3125 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3126 cc = MSVCRT_fgetc(MSVCRT_stdin))
3127 if(cc != '\r') *buf++ = (char)cc;
3129 *buf = '\0';
3131 TRACE("got '%s'\n", buf_start);
3132 return buf_start;
3135 /*********************************************************************
3136 * _getws (MSVCRT.@)
3138 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3140 MSVCRT_wint_t cc;
3141 MSVCRT_wchar_t* ws = buf;
3143 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3144 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3146 if (cc != '\r')
3147 *buf++ = (MSVCRT_wchar_t)cc;
3149 *buf = '\0';
3151 TRACE("got %s\n", debugstr_w(ws));
3152 return ws;
3155 /*********************************************************************
3156 * putc (MSVCRT.@)
3158 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3160 return MSVCRT_fputc(c, file);
3163 /*********************************************************************
3164 * putchar (MSVCRT.@)
3166 int CDECL MSVCRT_putchar(int c)
3168 return MSVCRT_fputc(c, MSVCRT_stdout);
3171 /*********************************************************************
3172 * _putwch (MSVCRT.@)
3174 int CDECL MSVCRT__putwch(int c)
3176 return MSVCRT_fputwc(c, MSVCRT_stdout);
3179 /*********************************************************************
3180 * puts (MSVCRT.@)
3182 int CDECL MSVCRT_puts(const char *s)
3184 MSVCRT_size_t len = strlen(s);
3185 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3186 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3189 /*********************************************************************
3190 * _putws (MSVCRT.@)
3192 int CDECL _putws(const MSVCRT_wchar_t *s)
3194 static const MSVCRT_wchar_t nl = '\n';
3195 MSVCRT_size_t len = strlenW(s);
3196 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3197 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3200 /*********************************************************************
3201 * remove (MSVCRT.@)
3203 int CDECL MSVCRT_remove(const char *path)
3205 TRACE("(%s)\n",path);
3206 if (DeleteFileA(path))
3207 return 0;
3208 TRACE(":failed (%d)\n",GetLastError());
3209 msvcrt_set_errno(GetLastError());
3210 return -1;
3213 /*********************************************************************
3214 * _wremove (MSVCRT.@)
3216 int CDECL _wremove(const MSVCRT_wchar_t *path)
3218 TRACE("(%s)\n",debugstr_w(path));
3219 if (DeleteFileW(path))
3220 return 0;
3221 TRACE(":failed (%d)\n",GetLastError());
3222 msvcrt_set_errno(GetLastError());
3223 return -1;
3226 /*********************************************************************
3227 * rename (MSVCRT.@)
3229 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3231 TRACE(":from %s to %s\n",oldpath,newpath);
3232 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3233 return 0;
3234 TRACE(":failed (%d)\n",GetLastError());
3235 msvcrt_set_errno(GetLastError());
3236 return -1;
3239 /*********************************************************************
3240 * _wrename (MSVCRT.@)
3242 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3244 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3245 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3246 return 0;
3247 TRACE(":failed (%d)\n",GetLastError());
3248 msvcrt_set_errno(GetLastError());
3249 return -1;
3252 /*********************************************************************
3253 * setvbuf (MSVCRT.@)
3255 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3257 /* TODO: Check if file busy */
3258 if(file->_bufsiz) {
3259 MSVCRT_free(file->_base);
3260 file->_bufsiz = 0;
3261 file->_cnt = 0;
3263 if(mode == MSVCRT__IOFBF) {
3264 file->_flag &= ~MSVCRT__IONBF;
3265 file->_base = file->_ptr = buf;
3266 if(buf) {
3267 file->_bufsiz = size;
3269 } else {
3270 file->_flag |= MSVCRT__IONBF;
3272 return 0;
3275 /*********************************************************************
3276 * setbuf (MSVCRT.@)
3278 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3280 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3283 /*********************************************************************
3284 * tmpnam (MSVCRT.@)
3286 char * CDECL MSVCRT_tmpnam(char *s)
3288 static int unique;
3289 char tmpstr[16];
3290 char *p;
3291 int count;
3292 if (s == 0)
3293 s = MSVCRT_tmpname;
3294 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3295 p = s + sprintf(s, "\\s%s.", tmpstr);
3296 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3298 msvcrt_int_to_base32(unique++, tmpstr);
3299 strcpy(p, tmpstr);
3300 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3301 GetLastError() == ERROR_FILE_NOT_FOUND)
3302 break;
3304 return s;
3307 /*********************************************************************
3308 * tmpfile (MSVCRT.@)
3310 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3312 char *filename = MSVCRT_tmpnam(NULL);
3313 int fd;
3314 MSVCRT_FILE* file = NULL;
3316 LOCK_FILES();
3317 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3318 if (fd != -1 && (file = msvcrt_alloc_fp()))
3320 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3322 file->_flag = 0;
3323 file = NULL;
3325 else file->_tmpfname = _strdup(filename);
3327 UNLOCK_FILES();
3328 return file;
3331 /*********************************************************************
3332 * vfprintf (MSVCRT.@)
3334 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3336 char buf[2048];
3337 LPWSTR formatW = NULL;
3338 DWORD sz;
3339 pf_output out;
3340 int written, retval;
3342 out.unicode = FALSE;
3343 out.buf.A = out.grow.A = buf;
3344 out.used = 0;
3345 out.len = sizeof(buf);
3347 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
3348 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
3349 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
3351 if ((written = pf_vsnprintf( &out, formatW, NULL, FALSE, valist )) >= 0)
3353 retval = MSVCRT_fwrite(out.buf.A, sizeof(*out.buf.A), written, file);
3355 else retval = -1;
3357 HeapFree( GetProcessHeap(), 0, formatW );
3359 if (out.buf.A != out.grow.A)
3360 MSVCRT_free (out.buf.A);
3361 return retval;
3364 /*********************************************************************
3365 * vfwprintf (MSVCRT.@)
3366 * FIXME:
3367 * Is final char included in written (then resize is too big) or not
3368 * (then we must test for equality too)?
3370 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3372 MSVCRT_wchar_t buf[2048];
3373 pf_output out;
3374 int written, retval;
3376 out.unicode = TRUE;
3377 out.buf.W = out.grow.W = buf;
3378 out.used = 0;
3379 out.len = sizeof(buf) / sizeof(buf[0]);
3381 if ((written = pf_vsnprintf( &out, format, NULL, FALSE, valist )) >= 0)
3383 retval = MSVCRT_fwrite(out.buf.W, sizeof(*out.buf.W), written, file);
3385 else retval = -1;
3386 if (out.buf.W != out.grow.W)
3387 MSVCRT_free (out.buf.W);
3388 return retval;
3391 /*********************************************************************
3392 * vprintf (MSVCRT.@)
3394 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3396 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3399 /*********************************************************************
3400 * vwprintf (MSVCRT.@)
3402 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3404 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3407 /*********************************************************************
3408 * fprintf (MSVCRT.@)
3410 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3412 __ms_va_list valist;
3413 int res;
3414 __ms_va_start(valist, format);
3415 res = MSVCRT_vfprintf(file, format, valist);
3416 __ms_va_end(valist);
3417 return res;
3420 /*********************************************************************
3421 * fwprintf (MSVCRT.@)
3423 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3425 __ms_va_list valist;
3426 int res;
3427 __ms_va_start(valist, format);
3428 res = MSVCRT_vfwprintf(file, format, valist);
3429 __ms_va_end(valist);
3430 return res;
3433 /*********************************************************************
3434 * printf (MSVCRT.@)
3436 int CDECL MSVCRT_printf(const char *format, ...)
3438 __ms_va_list valist;
3439 int res;
3440 __ms_va_start(valist, format);
3441 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3442 __ms_va_end(valist);
3443 return res;
3446 /*********************************************************************
3447 * ungetc (MSVCRT.@)
3449 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3451 if (c == MSVCRT_EOF)
3452 return MSVCRT_EOF;
3453 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3454 msvcrt_alloc_buffer(file);
3455 file->_ptr++;
3457 if(file->_ptr>file->_base) {
3458 file->_ptr--;
3459 *file->_ptr=c;
3460 file->_cnt++;
3461 MSVCRT_clearerr(file);
3462 return c;
3464 return MSVCRT_EOF;
3467 /*********************************************************************
3468 * ungetwc (MSVCRT.@)
3470 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3472 MSVCRT_wchar_t mwc = wc;
3473 char * pp = (char *)&mwc;
3474 int i;
3475 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3476 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3477 return MSVCRT_WEOF;
3479 return mwc;
3482 /*********************************************************************
3483 * wprintf (MSVCRT.@)
3485 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3487 __ms_va_list valist;
3488 int res;
3489 __ms_va_start(valist, format);
3490 res = MSVCRT_vwprintf(format, valist);
3491 __ms_va_end(valist);
3492 return res;
3495 /*********************************************************************
3496 * _getmaxstdio (MSVCRT.@)
3498 int CDECL _getmaxstdio(void)
3500 FIXME("stub, always returns 512\n");
3501 return 512;
3504 /*********************************************************************
3505 * _setmaxstdio_ (MSVCRT.@)
3507 int CDECL _setmaxstdio(int newmax)
3509 int res;
3510 if( newmax > 2048)
3511 res = -1;
3512 else
3513 res = newmax;
3514 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3515 return res;
3518 /*********************************************************************
3519 * __pioinfo (MSVCRT.@)
3520 * FIXME: see MSVCRT_MAX_FILES define.
3522 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3523 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3524 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3525 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3526 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3527 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3528 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3529 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3530 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3531 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3532 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3533 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3536 /*********************************************************************
3537 * __badioinfo (MSVCRT.@)
3539 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };