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
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
38 #include <sys/types.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
59 /* values for wxflag in file descriptor */
62 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
63 #define WX_READCR 0x08 /* underlying file is at \r */
64 #define WX_DONTINHERIT 0x10
65 #define WX_APPEND 0x20
68 /* FIXME: this should be allocated dynamically */
69 #define MSVCRT_MAX_FILES 2048
74 DWORD unkn[7]; /* critical section and init flag */
77 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
79 MSVCRT_FILE MSVCRT__iob[3] = { { 0 } };
81 static int MSVCRT_fdstart = 3; /* first unallocated fd */
82 static int MSVCRT_fdend = 3; /* highest allocated fd */
84 static MSVCRT_FILE* MSVCRT_fstreams[2048];
85 static int MSVCRT_stream_idx;
87 /* INTERNAL: process umask */
88 static int MSVCRT_umask = 0;
90 /* INTERNAL: Static buffer for temp file name */
91 static char MSVCRT_tmpname[MAX_PATH];
93 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
94 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
95 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
96 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
98 #define TOUL(x) (ULONGLONG)(x)
99 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
100 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
101 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
102 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
104 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
105 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
106 * and MSVCRT_stream_idx, from race conditions.
107 * It doesn't protect against race conditions manipulating the underlying files
108 * or flags; doing so would probably be better accomplished with per-file
109 * protection, rather than locking the whole table for every change.
111 static CRITICAL_SECTION MSVCRT_file_cs;
112 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
113 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
115 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
117 buf->st_dev = buf64->st_dev;
118 buf->st_ino = buf64->st_ino;
119 buf->st_mode = buf64->st_mode;
120 buf->st_nlink = buf64->st_nlink;
121 buf->st_uid = buf64->st_uid;
122 buf->st_gid = buf64->st_gid;
123 buf->st_rdev = buf64->st_rdev;
124 buf->st_size = buf64->st_size;
125 buf->st_atime = buf64->st_atime;
126 buf->st_mtime = buf64->st_mtime;
127 buf->st_ctime = buf64->st_ctime;
130 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
132 buf->st_dev = buf64->st_dev;
133 buf->st_ino = buf64->st_ino;
134 buf->st_mode = buf64->st_mode;
135 buf->st_nlink = buf64->st_nlink;
136 buf->st_uid = buf64->st_uid;
137 buf->st_gid = buf64->st_gid;
138 buf->st_rdev = buf64->st_rdev;
139 buf->st_size = buf64->st_size;
140 buf->st_atime = buf64->st_atime;
141 buf->st_mtime = buf64->st_mtime;
142 buf->st_ctime = buf64->st_ctime;
145 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
147 /* 1601 to 1970 is 369 years plus 89 leap days */
148 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
150 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
151 ft->dwHighDateTime = ticks >> 32;
152 ft->dwLowDateTime = ticks;
155 static inline BOOL msvcrt_is_valid_fd(int fd)
157 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
160 /* INTERNAL: Get the HANDLE for a fd
161 * This doesn't lock the table, because a failure will result in
162 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
163 * it returns a valid handle which is about to be closed, a subsequent call
164 * will fail, most likely in a sane way.
166 static HANDLE msvcrt_fdtoh(int fd)
168 if (!msvcrt_is_valid_fd(fd))
170 WARN(":fd (%d) - no handle!\n",fd);
171 *MSVCRT___doserrno() = 0;
172 *MSVCRT__errno() = MSVCRT_EBADF;
173 return INVALID_HANDLE_VALUE;
175 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
176 return MSVCRT_fdesc[fd].handle;
179 /* INTERNAL: free a file entry fd */
180 static void msvcrt_free_fd(int fd)
185 old_handle = MSVCRT_fdesc[fd].handle;
186 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
187 MSVCRT_fdesc[fd].wxflag = 0;
188 TRACE(":fd (%d) freed\n",fd);
189 if (fd < 3) /* don't use 0,1,2 for user files */
194 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
197 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
200 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
206 if (fd == MSVCRT_fdend - 1)
208 if (fd < MSVCRT_fdstart)
214 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
215 /* caller must hold the files lock */
216 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
218 if (fd >= MSVCRT_MAX_FILES)
220 WARN(":files exhausted!\n");
223 MSVCRT_fdesc[fd].handle = hand;
224 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
226 /* locate next free slot */
227 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
228 MSVCRT_fdstart = MSVCRT_fdend + 1;
230 while (MSVCRT_fdstart < MSVCRT_fdend &&
231 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
233 /* update last fd in use */
234 if (fd >= MSVCRT_fdend)
235 MSVCRT_fdend = fd + 1;
236 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
240 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
241 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
242 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
248 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
249 static int msvcrt_alloc_fd(HANDLE hand, int flag)
254 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
255 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
260 /* INTERNAL: Allocate a FILE* for an fd slot */
261 /* caller must hold the files lock */
262 static MSVCRT_FILE* msvcrt_alloc_fp(void)
266 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
268 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
270 if (!MSVCRT_fstreams[i])
272 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
274 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
276 return MSVCRT_fstreams[i];
282 /* INTERNAL: initialize a FILE* from an open fd */
283 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
285 TRACE(":fd (%d) allocating FILE*\n",fd);
286 if (!msvcrt_is_valid_fd(fd))
288 WARN(":invalid fd %d\n",fd);
289 *MSVCRT___doserrno() = 0;
290 *MSVCRT__errno() = MSVCRT_EBADF;
293 memset(file, 0, sizeof(*file));
295 file->_flag = stream_flags;
297 TRACE(":got FILE* (%p)\n",file);
301 /* INTERNAL: Create an inheritance data block (for spawned process)
302 * The inheritance block is made of:
303 * 00 int nb of file descriptor (NBFD)
304 * 04 char file flags (wxflag): repeated for each fd
305 * 4+NBFD HANDLE file handle: repeated for each fd
307 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
313 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
314 *block = MSVCRT_calloc(*size, 1);
320 wxflag_ptr = (char*)*block + sizeof(unsigned);
321 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
323 *(unsigned*)*block = MSVCRT_fdend;
324 for (fd = 0; fd < MSVCRT_fdend; fd++)
326 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
327 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
329 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
330 *handle_ptr = MSVCRT_fdesc[fd].handle;
335 *handle_ptr = INVALID_HANDLE_VALUE;
337 wxflag_ptr++; handle_ptr++;
342 /* INTERNAL: Set up all file descriptors,
343 * as well as default streams (stdin, stderr and stdout)
345 void msvcrt_init_io(void)
350 InitializeCriticalSection(&MSVCRT_file_cs);
351 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
352 GetStartupInfoA(&si);
353 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
359 count = *(unsigned*)si.lpReserved2;
360 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
361 handle_ptr = (HANDLE*)(wxflag_ptr + count);
363 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
364 count = min(count, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
365 for (i = 0; i < count; i++)
367 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
369 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
370 MSVCRT_fdesc[i].handle = *handle_ptr;
374 MSVCRT_fdesc[i].wxflag = 0;
375 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
377 wxflag_ptr++; handle_ptr++;
379 MSVCRT_fdend = max( 3, count );
380 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
381 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
384 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
386 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
387 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
388 GetCurrentProcess(), &MSVCRT_fdesc[0].handle,
389 0, TRUE, DUPLICATE_SAME_ACCESS))
390 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
392 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
394 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
395 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
396 GetCurrentProcess(), &MSVCRT_fdesc[1].handle,
397 0, TRUE, DUPLICATE_SAME_ACCESS))
398 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
400 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
402 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
403 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
404 GetCurrentProcess(), &MSVCRT_fdesc[2].handle,
405 0, TRUE, DUPLICATE_SAME_ACCESS))
406 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
409 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
410 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
412 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
413 for (i = 0; i < 3; i++)
415 /* FILE structs for stdin/out/err are static and never deleted */
416 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
417 MSVCRT__iob[i]._file = i;
418 MSVCRT__iob[i]._tmpfname = NULL;
419 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
421 MSVCRT_stream_idx = 3;
424 /* INTERNAL: Flush stdio file buffer */
425 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
428 int cnt=file->_ptr-file->_base;
429 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
430 file->_flag |= MSVCRT__IOERR;
433 file->_ptr=file->_base;
434 file->_cnt=file->_bufsiz;
439 /* INTERNAL: Allocate stdio file buffer */
440 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
442 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
444 file->_bufsiz = MSVCRT_BUFSIZ;
445 file->_flag |= MSVCRT__IOMYBUF;
447 file->_base = (char*)(&file->_charbuf);
449 file->_bufsiz = sizeof(file->_charbuf);
451 file->_ptr = file->_base;
455 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
456 static void msvcrt_int_to_base32(int num, char *str)
471 *p = (num & 31) + '0';
473 *p += ('a' - '0' - 10);
478 /*********************************************************************
479 * __iob_func(MSVCRT.@)
481 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
483 return &MSVCRT__iob[0];
486 /*********************************************************************
489 int CDECL MSVCRT__access(const char *filename, int mode)
491 DWORD attr = GetFileAttributesA(filename);
493 TRACE("(%s,%d) %d\n",filename,mode,attr);
495 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
497 msvcrt_set_errno(GetLastError());
500 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
502 msvcrt_set_errno(ERROR_ACCESS_DENIED);
508 /*********************************************************************
509 * _waccess (MSVCRT.@)
511 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
513 DWORD attr = GetFileAttributesW(filename);
515 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
517 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
519 msvcrt_set_errno(GetLastError());
522 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
524 msvcrt_set_errno(ERROR_ACCESS_DENIED);
530 /*********************************************************************
533 int CDECL MSVCRT__chmod(const char *path, int flags)
535 DWORD oldFlags = GetFileAttributesA(path);
537 if (oldFlags != INVALID_FILE_ATTRIBUTES)
539 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
540 oldFlags | FILE_ATTRIBUTE_READONLY;
542 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
545 msvcrt_set_errno(GetLastError());
549 /*********************************************************************
552 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
554 DWORD oldFlags = GetFileAttributesW(path);
556 if (oldFlags != INVALID_FILE_ATTRIBUTES)
558 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
559 oldFlags | FILE_ATTRIBUTE_READONLY;
561 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
564 msvcrt_set_errno(GetLastError());
568 /*********************************************************************
571 int CDECL MSVCRT__unlink(const char *path)
573 TRACE("%s\n",debugstr_a(path));
574 if(DeleteFileA(path))
576 TRACE("failed (%d)\n",GetLastError());
577 msvcrt_set_errno(GetLastError());
581 /*********************************************************************
582 * _wunlink (MSVCRT.@)
584 int CDECL _wunlink(const MSVCRT_wchar_t *path)
586 TRACE("(%s)\n",debugstr_w(path));
587 if(DeleteFileW(path))
589 TRACE("failed (%d)\n",GetLastError());
590 msvcrt_set_errno(GetLastError());
594 /* _flushall calls MSVCRT_fflush which calls _flushall */
595 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
597 /*********************************************************************
598 * _flushall (MSVCRT.@)
600 int CDECL _flushall(void)
602 int i, num_flushed = 0;
605 for (i = 3; i < MSVCRT_stream_idx; i++)
606 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
609 /* FIXME: flush, do not commit */
610 if (_commit(i) == -1)
611 if (MSVCRT_fstreams[i])
612 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
614 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
615 MSVCRT_fflush(MSVCRT_fstreams[i]);
621 TRACE(":flushed (%d) handles\n",num_flushed);
625 /*********************************************************************
628 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
632 } else if(file->_flag & MSVCRT__IOWRT) {
633 int res=msvcrt_flush_buffer(file);
639 /*********************************************************************
642 int CDECL MSVCRT__close(int fd)
648 hand = msvcrt_fdtoh(fd);
649 TRACE(":fd (%d) handle (%p)\n",fd,hand);
650 if (hand == INVALID_HANDLE_VALUE)
652 else if (!CloseHandle(hand))
654 WARN(":failed-last error (%d)\n",GetLastError());
655 msvcrt_set_errno(GetLastError());
668 /*********************************************************************
671 int CDECL _commit(int fd)
673 HANDLE hand = msvcrt_fdtoh(fd);
675 TRACE(":fd (%d) handle (%p)\n",fd,hand);
676 if (hand == INVALID_HANDLE_VALUE)
679 if (!FlushFileBuffers(hand))
681 if (GetLastError() == ERROR_INVALID_HANDLE)
683 /* FlushFileBuffers fails for console handles
684 * so we ignore this error.
688 TRACE(":failed-last error (%d)\n",GetLastError());
689 msvcrt_set_errno(GetLastError());
696 /*********************************************************************
699 * MSDN isn't clear on this point, but the remarks for _pipe
700 * indicate file descriptors duplicated with _dup and _dup2 are always
703 int CDECL MSVCRT__dup2(int od, int nd)
707 TRACE("(od=%d, nd=%d)\n", od, nd);
709 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
713 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
714 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
716 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
718 if (msvcrt_is_valid_fd(nd))
720 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
724 *MSVCRT__errno() = MSVCRT_EMFILE;
728 /* _dup2 returns 0, not nd, on success */
735 msvcrt_set_errno(GetLastError());
740 *MSVCRT__errno() = MSVCRT_EBADF;
747 /*********************************************************************
750 int CDECL MSVCRT__dup(int od)
756 if (MSVCRT__dup2(od, fd) == 0)
764 /*********************************************************************
767 int CDECL _eof(int fd)
770 LONG hcurpos,hendpos;
771 HANDLE hand = msvcrt_fdtoh(fd);
773 TRACE(":fd (%d) handle (%p)\n",fd,hand);
775 if (hand == INVALID_HANDLE_VALUE)
778 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
780 /* Otherwise we do it the hard way */
781 hcurpos = hendpos = 0;
782 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
783 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
785 if (curpos == endpos && hcurpos == hendpos)
787 /* FIXME: shouldn't WX_ATEOF be set here? */
791 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
795 /*********************************************************************
796 * _fcloseall (MSVCRT.@)
798 int CDECL MSVCRT__fcloseall(void)
800 int num_closed = 0, i;
803 for (i = 3; i < MSVCRT_stream_idx; i++)
804 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
805 !MSVCRT_fclose(MSVCRT_fstreams[i]))
809 TRACE(":closed (%d) handles\n",num_closed);
813 /* free everything on process exit */
814 void msvcrt_free_io(void)
817 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
818 * stdout, and stderr (unlike GNU), so we need to fclose() them here
819 * or they won't get flushed.
821 MSVCRT_fclose(&MSVCRT__iob[0]);
822 MSVCRT_fclose(&MSVCRT__iob[1]);
823 MSVCRT_fclose(&MSVCRT__iob[2]);
824 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
825 DeleteCriticalSection(&MSVCRT_file_cs);
828 /*********************************************************************
829 * _lseeki64 (MSVCRT.@)
831 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
833 HANDLE hand = msvcrt_fdtoh(fd);
834 LARGE_INTEGER ofs, ret;
836 TRACE(":fd (%d) handle (%p)\n",fd,hand);
837 if (hand == INVALID_HANDLE_VALUE)
840 if (whence < 0 || whence > 2)
842 *MSVCRT__errno() = MSVCRT_EINVAL;
846 TRACE(":fd (%d) to %s pos %s\n",
847 fd,wine_dbgstr_longlong(offset),
848 (whence==SEEK_SET)?"SEEK_SET":
849 (whence==SEEK_CUR)?"SEEK_CUR":
850 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
852 ofs.QuadPart = offset;
853 if (SetFilePointerEx(hand, ofs, &ret, whence))
855 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
856 /* FIXME: What if we seek _to_ EOF - is EOF set? */
860 TRACE(":error-last error (%d)\n",GetLastError());
861 msvcrt_set_errno(GetLastError());
865 /*********************************************************************
868 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
870 return MSVCRT__lseeki64(fd, offset, whence);
873 /*********************************************************************
874 * _locking (MSVCRT.@)
876 * This is untested; the underlying LockFile doesn't work yet.
878 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
882 HANDLE hand = msvcrt_fdtoh(fd);
884 TRACE(":fd (%d) handle (%p)\n",fd,hand);
885 if (hand == INVALID_HANDLE_VALUE)
888 if (mode < 0 || mode > 4)
890 *MSVCRT__errno() = MSVCRT_EINVAL;
894 TRACE(":fd (%d) by 0x%08x mode %s\n",
895 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
896 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
897 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
898 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
899 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
902 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
904 FIXME ("Seek failed\n");
905 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
908 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
911 ret = 1; /* just to satisfy gcc */
914 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
919 else if (mode == MSVCRT__LK_UNLCK)
920 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
922 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
923 /* FIXME - what about error settings? */
927 /*********************************************************************
930 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
932 /* Flush output if needed */
933 if(file->_flag & MSVCRT__IOWRT)
934 msvcrt_flush_buffer(file);
936 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
937 offset -= file->_cnt;
938 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
939 /* Black magic correction for CR removal */
941 for (i=0; i<file->_cnt; i++) {
942 if (file->_ptr[i] == '\n')
945 /* Black magic when reading CR at buffer boundary*/
946 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
950 /* Discard buffered input */
952 file->_ptr = file->_base;
953 /* Reset direction of i/o */
954 if(file->_flag & MSVCRT__IORW) {
955 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
957 /* Clear end of file flag */
958 file->_flag &= ~MSVCRT__IOEOF;
959 return (MSVCRT__lseek(file->_file,offset,whence) == -1)?-1:0;
962 /*********************************************************************
965 int CDECL _chsize(int fd, MSVCRT_long size)
971 TRACE("(fd=%d, size=%d)\n", fd, size);
975 handle = msvcrt_fdtoh(fd);
976 if (handle != INVALID_HANDLE_VALUE)
978 /* save the current file pointer */
979 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
982 pos = MSVCRT__lseek(fd, size, SEEK_SET);
985 ret = SetEndOfFile(handle);
986 if (!ret) msvcrt_set_errno(GetLastError());
989 /* restore the file pointer */
990 MSVCRT__lseek(fd, cur, SEEK_SET);
998 /*********************************************************************
999 * clearerr (MSVCRT.@)
1001 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1003 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1004 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1007 /*********************************************************************
1010 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1012 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1013 MSVCRT_fseek(file, 0L, SEEK_SET);
1014 MSVCRT_clearerr(file);
1017 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1019 int plus = strchrW(mode, '+') != NULL;
1024 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1025 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1028 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1029 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1032 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1033 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1043 *open_flags |= MSVCRT__O_BINARY;
1044 *open_flags &= ~MSVCRT__O_TEXT;
1047 *open_flags |= MSVCRT__O_TEXT;
1048 *open_flags &= ~MSVCRT__O_BINARY;
1054 FIXME(":unknown flag %c not supported\n",mode[-1]);
1059 /*********************************************************************
1060 * _fdopen (MSVCRT.@)
1062 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1065 MSVCRT_wchar_t *modeW = NULL;
1067 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1069 ret = MSVCRT__wfdopen(fd, modeW);
1075 /*********************************************************************
1076 * _wfdopen (MSVCRT.@)
1078 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1080 int open_flags, stream_flags;
1083 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1086 if (!(file = msvcrt_alloc_fp()))
1088 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1093 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1099 /*********************************************************************
1100 * _filelength (MSVCRT.@)
1102 LONG CDECL MSVCRT__filelength(int fd)
1104 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1107 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1110 if (endPos != curPos)
1111 MSVCRT__lseek(fd, curPos, SEEK_SET);
1118 /*********************************************************************
1119 * _filelengthi64 (MSVCRT.@)
1121 __int64 CDECL MSVCRT__filelengthi64(int fd)
1123 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1126 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1129 if (endPos != curPos)
1130 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1137 /*********************************************************************
1138 * _fileno (MSVCRT.@)
1140 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1142 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1146 /*********************************************************************
1147 * _fstat64 (MSVCRT.@)
1149 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1153 BY_HANDLE_FILE_INFORMATION hfi;
1154 HANDLE hand = msvcrt_fdtoh(fd);
1156 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1157 if (hand == INVALID_HANDLE_VALUE)
1162 WARN(":failed-NULL buf\n");
1163 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1167 memset(&hfi, 0, sizeof(hfi));
1168 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1169 type = GetFileType(hand);
1170 if (type == FILE_TYPE_PIPE)
1172 buf->st_dev = buf->st_rdev = fd;
1173 buf->st_mode = S_IFIFO;
1176 else if (type == FILE_TYPE_CHAR)
1178 buf->st_dev = buf->st_rdev = fd;
1179 buf->st_mode = S_IFCHR;
1182 else /* FILE_TYPE_DISK etc. */
1184 if (!GetFileInformationByHandle(hand, &hfi))
1186 WARN(":failed-last error (%d)\n",GetLastError());
1187 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1190 buf->st_mode = S_IFREG | 0444;
1191 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1192 buf->st_mode |= 0222;
1193 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1194 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1196 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1197 buf->st_mtime = buf->st_ctime = dw;
1198 buf->st_nlink = hfi.nNumberOfLinks;
1200 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1205 /*********************************************************************
1206 * _fstati64 (MSVCRT.@)
1208 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1211 struct MSVCRT__stat64 buf64;
1213 ret = MSVCRT__fstat64(fd, &buf64);
1215 msvcrt_stat64_to_stati64(&buf64, buf);
1219 /*********************************************************************
1222 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1224 struct MSVCRT__stat64 buf64;
1226 ret = MSVCRT__fstat64(fd, &buf64);
1228 msvcrt_stat64_to_stat(&buf64, buf);
1232 /*********************************************************************
1233 * _futime64 (MSVCRT.@)
1235 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1237 HANDLE hand = msvcrt_fdtoh(fd);
1242 time_to_filetime( MSVCRT__time64(NULL), &at );
1247 time_to_filetime( t->actime, &at );
1248 time_to_filetime( t->modtime, &wt );
1251 if (!SetFileTime(hand, NULL, &at, &wt))
1253 msvcrt_set_errno(GetLastError());
1259 /*********************************************************************
1260 * _futime32 (MSVCRT.@)
1262 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1264 struct MSVCRT___utimbuf64 t64;
1265 t64.actime = t->actime;
1266 t64.modtime = t->modtime;
1267 return _futime64( fd, &t64 );
1270 /*********************************************************************
1271 * _futime (MSVCRT.@)
1274 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1276 return _futime64( fd, t );
1279 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1281 return _futime32( fd, t );
1285 /*********************************************************************
1286 * _get_osfhandle (MSVCRT.@)
1288 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1290 HANDLE hand = msvcrt_fdtoh(fd);
1291 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1293 return (MSVCRT_intptr_t)hand;
1296 /*********************************************************************
1297 * _isatty (MSVCRT.@)
1299 int CDECL _isatty(int fd)
1301 HANDLE hand = msvcrt_fdtoh(fd);
1303 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1304 if (hand == INVALID_HANDLE_VALUE)
1307 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1310 /*********************************************************************
1311 * _mktemp (MSVCRT.@)
1313 char * CDECL _mktemp(char *pattern)
1316 char *retVal = pattern;
1321 numX = (*pattern++ == 'X')? numX + 1 : 0;
1325 id = GetCurrentProcessId();
1329 int tempNum = id / 10;
1330 *pattern-- = id - (tempNum * 10) + '0';
1336 *pattern = letter++;
1337 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1338 GetLastError() == ERROR_FILE_NOT_FOUND)
1340 } while(letter <= 'z');
1344 /*********************************************************************
1345 * _wmktemp (MSVCRT.@)
1347 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1350 MSVCRT_wchar_t *retVal = pattern;
1352 MSVCRT_wchar_t letter = 'a';
1355 numX = (*pattern++ == 'X')? numX + 1 : 0;
1359 id = GetCurrentProcessId();
1363 int tempNum = id / 10;
1364 *pattern-- = id - (tempNum * 10) + '0';
1370 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1371 GetLastError() == ERROR_FILE_NOT_FOUND)
1373 *pattern = letter++;
1374 } while(letter != '|');
1378 static unsigned split_oflags(unsigned oflags)
1381 unsigned unsupp; /* until we support everything */
1383 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1384 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1385 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1386 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1387 else wxflags |= WX_TEXT; /* default to TEXT*/
1388 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1390 if ((unsupp = oflags & ~(
1391 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1392 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1393 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1394 MSVCRT__O_NOINHERIT|
1395 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1397 ERR(":unsupported oflags 0x%04x\n",unsupp);
1402 /*********************************************************************
1405 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1408 SECURITY_ATTRIBUTES sa;
1409 HANDLE readHandle, writeHandle;
1413 *MSVCRT__errno() = MSVCRT_EINVAL;
1417 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1418 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1419 sa.lpSecurityDescriptor = NULL;
1420 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1422 unsigned int wxflags = split_oflags(textmode);
1426 fd = msvcrt_alloc_fd(readHandle, wxflags);
1430 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1438 MSVCRT__close(pfds[0]);
1439 CloseHandle(writeHandle);
1440 *MSVCRT__errno() = MSVCRT_EMFILE;
1445 CloseHandle(readHandle);
1446 CloseHandle(writeHandle);
1447 *MSVCRT__errno() = MSVCRT_EMFILE;
1452 msvcrt_set_errno(GetLastError());
1457 /*********************************************************************
1460 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1464 DWORD access = 0, creation = 0, attrib;
1468 SECURITY_ATTRIBUTES sa;
1471 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1472 path, oflags, shflags);
1474 wxflag = split_oflags(oflags);
1475 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1477 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1478 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1479 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1482 if (oflags & MSVCRT__O_CREAT)
1484 __ms_va_start(ap, shflags);
1485 pmode = va_arg(ap, int);
1488 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1489 FIXME(": pmode 0x%04x ignored\n", pmode);
1491 WARN(": pmode 0x%04x ignored\n", pmode);
1493 if (oflags & MSVCRT__O_EXCL)
1494 creation = CREATE_NEW;
1495 else if (oflags & MSVCRT__O_TRUNC)
1496 creation = CREATE_ALWAYS;
1498 creation = OPEN_ALWAYS;
1500 else /* no MSVCRT__O_CREAT */
1502 if (oflags & MSVCRT__O_TRUNC)
1503 creation = TRUNCATE_EXISTING;
1505 creation = OPEN_EXISTING;
1510 case MSVCRT__SH_DENYRW:
1513 case MSVCRT__SH_DENYWR:
1514 sharing = FILE_SHARE_READ;
1516 case MSVCRT__SH_DENYRD:
1517 sharing = FILE_SHARE_WRITE;
1519 case MSVCRT__SH_DENYNO:
1520 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1523 ERR( "Unhandled shflags 0x%x\n", shflags );
1526 attrib = FILE_ATTRIBUTE_NORMAL;
1528 if (oflags & MSVCRT__O_TEMPORARY)
1530 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1532 sharing |= FILE_SHARE_DELETE;
1535 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1536 sa.lpSecurityDescriptor = NULL;
1537 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1539 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1541 if (hand == INVALID_HANDLE_VALUE) {
1542 WARN(":failed-last error (%d)\n",GetLastError());
1543 msvcrt_set_errno(GetLastError());
1547 fd = msvcrt_alloc_fd(hand, wxflag);
1549 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1553 /*********************************************************************
1554 * _wsopen (MSVCRT.@)
1556 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1560 DWORD access = 0, creation = 0, attrib;
1564 SECURITY_ATTRIBUTES sa;
1567 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1568 debugstr_w(path), oflags, shflags);
1570 wxflag = split_oflags(oflags);
1571 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1573 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1574 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1575 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1578 if (oflags & MSVCRT__O_CREAT)
1580 __ms_va_start(ap, shflags);
1581 pmode = va_arg(ap, int);
1584 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1585 FIXME(": pmode 0x%04x ignored\n", pmode);
1587 WARN(": pmode 0x%04x ignored\n", pmode);
1589 if (oflags & MSVCRT__O_EXCL)
1590 creation = CREATE_NEW;
1591 else if (oflags & MSVCRT__O_TRUNC)
1592 creation = CREATE_ALWAYS;
1594 creation = OPEN_ALWAYS;
1596 else /* no MSVCRT__O_CREAT */
1598 if (oflags & MSVCRT__O_TRUNC)
1599 creation = TRUNCATE_EXISTING;
1601 creation = OPEN_EXISTING;
1606 case MSVCRT__SH_DENYRW:
1609 case MSVCRT__SH_DENYWR:
1610 sharing = FILE_SHARE_READ;
1612 case MSVCRT__SH_DENYRD:
1613 sharing = FILE_SHARE_WRITE;
1615 case MSVCRT__SH_DENYNO:
1616 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1619 ERR( "Unhandled shflags 0x%x\n", shflags );
1622 attrib = FILE_ATTRIBUTE_NORMAL;
1624 if (oflags & MSVCRT__O_TEMPORARY)
1626 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1628 sharing |= FILE_SHARE_DELETE;
1631 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1632 sa.lpSecurityDescriptor = NULL;
1633 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1635 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1637 if (hand == INVALID_HANDLE_VALUE) {
1638 WARN(":failed-last error (%d)\n",GetLastError());
1639 msvcrt_set_errno(GetLastError());
1643 fd = msvcrt_alloc_fd(hand, wxflag);
1645 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1649 /*********************************************************************
1652 int CDECL MSVCRT__open( const char *path, int flags, ... )
1656 if (flags & MSVCRT__O_CREAT)
1659 __ms_va_start(ap, flags);
1660 pmode = va_arg(ap, int);
1662 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1665 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1668 /*********************************************************************
1671 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1675 if (flags & MSVCRT__O_CREAT)
1678 __ms_va_start(ap, flags);
1679 pmode = va_arg(ap, int);
1681 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1684 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1687 /*********************************************************************
1690 int CDECL MSVCRT__creat(const char *path, int flags)
1692 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1693 return MSVCRT__open(path, usedFlags);
1696 /*********************************************************************
1697 * _wcreat (MSVCRT.@)
1699 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1701 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1702 return _wopen(path, usedFlags);
1705 /*********************************************************************
1706 * _open_osfhandle (MSVCRT.@)
1708 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1712 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1713 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1714 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1715 * text - it never sets MSVCRT__O_BINARY.
1717 /* don't let split_oflags() decide the mode if no mode is passed */
1718 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1719 oflags |= MSVCRT__O_BINARY;
1721 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1722 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1726 /*********************************************************************
1729 int CDECL _rmtmp(void)
1731 int num_removed = 0, i;
1734 for (i = 3; i < MSVCRT_stream_idx; i++)
1735 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1737 MSVCRT_fclose(MSVCRT_fstreams[i]);
1743 TRACE(":removed (%d) temp files\n",num_removed);
1747 /*********************************************************************
1750 * When reading \r as last character in text mode, read() positions
1751 * the file pointer on the \r character while getc() goes on to
1754 static int read_i(int fd, void *buf, unsigned int count)
1757 char *bufstart = buf;
1758 HANDLE hand = msvcrt_fdtoh(fd);
1760 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1761 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1762 TRACE("already at EOF, returning 0\n");
1765 /* Don't trace small reads, it gets *very* annoying */
1767 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1768 if (hand == INVALID_HANDLE_VALUE)
1771 /* Reading single bytes in O_TEXT mode makes things slow
1772 * So read big chunks
1774 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1776 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1779 if (bufstart[num_read-1] == '\r')
1781 MSVCRT_fdesc[fd].wxflag |= WX_READCR;
1785 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1786 for (i=0, j=0; i<num_read; i++)
1788 /* in text mode, a ctrl-z signals EOF */
1789 if (bufstart[i] == 0x1a)
1791 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1792 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1795 /* in text mode, strip \r if followed by \n.
1796 * BUG: should save state across calls somehow, so CR LF that
1797 * straddles buffer boundary gets recognized properly?
1799 if ((bufstart[i] != '\r')
1800 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1801 bufstart[j++] = bufstart[i];
1805 if (count != 0 && num_read == 0)
1807 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1808 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1813 if (GetLastError() == ERROR_BROKEN_PIPE)
1815 TRACE(":end-of-pipe\n");
1816 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1821 TRACE(":failed-last error (%d)\n",GetLastError());
1827 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1831 /*********************************************************************
1834 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1837 num_read = read_i(fd, buf, count);
1841 /*********************************************************************
1842 * _setmode (MSVCRT.@)
1844 int CDECL _setmode(int fd,int mode)
1846 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1847 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1848 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1849 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1850 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1852 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1856 /*********************************************************************
1857 * _stat64 (MSVCRT.@)
1859 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
1862 WIN32_FILE_ATTRIBUTE_DATA hfi;
1863 unsigned short mode = ALL_S_IREAD;
1866 TRACE(":file (%s) buf(%p)\n",path,buf);
1868 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1870 TRACE("failed (%d)\n",GetLastError());
1871 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1875 memset(buf,0,sizeof(struct MSVCRT__stat64));
1877 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1878 Bon 011120: This FIXME seems incorrect
1879 Also a letter as first char isn't enough to be classified
1882 if (isalpha(*path)&& (*(path+1)==':'))
1883 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1885 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1887 plen = strlen(path);
1889 /* Dir, or regular file? */
1890 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1891 (path[plen-1] == '\\'))
1892 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1895 mode |= MSVCRT__S_IFREG;
1897 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1899 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1900 (tolower(path[plen-3]) << 16);
1901 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1902 mode |= ALL_S_IEXEC;
1906 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1907 mode |= ALL_S_IWRITE;
1909 buf->st_mode = mode;
1911 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1912 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1914 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1915 buf->st_mtime = buf->st_ctime = dw;
1916 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
1917 (int)(buf->st_size >> 32),(int)buf->st_size,
1918 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
1922 /*********************************************************************
1923 * _stati64 (MSVCRT.@)
1925 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
1928 struct MSVCRT__stat64 buf64;
1930 ret = MSVCRT_stat64(path, &buf64);
1932 msvcrt_stat64_to_stati64(&buf64, buf);
1936 /*********************************************************************
1939 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
1941 struct MSVCRT__stat64 buf64;
1943 ret = MSVCRT_stat64( path, &buf64);
1945 msvcrt_stat64_to_stat(&buf64, buf);
1949 /*********************************************************************
1950 * _wstat64 (MSVCRT.@)
1952 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1955 WIN32_FILE_ATTRIBUTE_DATA hfi;
1956 unsigned short mode = ALL_S_IREAD;
1959 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1961 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1963 TRACE("failed (%d)\n",GetLastError());
1964 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1968 memset(buf,0,sizeof(struct MSVCRT__stat64));
1970 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1971 if (MSVCRT_iswalpha(*path))
1972 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1974 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1976 plen = strlenW(path);
1978 /* Dir, or regular file? */
1979 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1980 (path[plen-1] == '\\'))
1981 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1984 mode |= MSVCRT__S_IFREG;
1986 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1988 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1989 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1990 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1991 mode |= ALL_S_IEXEC;
1995 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1996 mode |= ALL_S_IWRITE;
1998 buf->st_mode = mode;
2000 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2001 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2003 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2004 buf->st_mtime = buf->st_ctime = dw;
2005 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2006 (int)(buf->st_size >> 32),(int)buf->st_size,
2007 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2011 /*********************************************************************
2012 * _wstati64 (MSVCRT.@)
2014 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2017 struct MSVCRT__stat64 buf64;
2019 ret = MSVCRT__wstat64(path, &buf64);
2021 msvcrt_stat64_to_stati64(&buf64, buf);
2025 /*********************************************************************
2028 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2031 struct MSVCRT__stat64 buf64;
2033 ret = MSVCRT__wstat64( path, &buf64 );
2034 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2038 /*********************************************************************
2041 MSVCRT_long CDECL _tell(int fd)
2043 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2046 /*********************************************************************
2047 * _telli64 (MSVCRT.@)
2049 __int64 CDECL _telli64(int fd)
2051 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2054 /*********************************************************************
2055 * _tempnam (MSVCRT.@)
2057 char * CDECL _tempnam(const char *dir, const char *prefix)
2059 char tmpbuf[MAX_PATH];
2060 const char *tmp_dir = MSVCRT_getenv("TMP");
2062 if (tmp_dir) dir = tmp_dir;
2064 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2065 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2067 TRACE("got name (%s)\n",tmpbuf);
2068 DeleteFileA(tmpbuf);
2069 return _strdup(tmpbuf);
2071 TRACE("failed (%d)\n",GetLastError());
2075 /*********************************************************************
2076 * _wtempnam (MSVCRT.@)
2078 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2080 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2082 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2083 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2085 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2086 DeleteFileW(tmpbuf);
2087 return _wcsdup(tmpbuf);
2089 TRACE("failed (%d)\n",GetLastError());
2093 /*********************************************************************
2096 int CDECL MSVCRT__umask(int umask)
2098 int old_umask = MSVCRT_umask;
2099 TRACE("(%d)\n",umask);
2100 MSVCRT_umask = umask;
2104 /*********************************************************************
2105 * _utime64 (MSVCRT.@)
2107 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2109 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2113 int retVal = _futime64(fd, t);
2120 /*********************************************************************
2121 * _utime32 (MSVCRT.@)
2123 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2125 struct MSVCRT___utimbuf64 t64;
2126 t64.actime = t->actime;
2127 t64.modtime = t->modtime;
2128 return _utime64( path, &t64 );
2131 /*********************************************************************
2135 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2137 return _utime64( path, t );
2140 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2142 return _utime32( path, t );
2146 /*********************************************************************
2147 * _wutime64 (MSVCRT.@)
2149 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2151 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2155 int retVal = _futime64(fd, t);
2162 /*********************************************************************
2163 * _wutime32 (MSVCRT.@)
2165 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2167 struct MSVCRT___utimbuf64 t64;
2168 t64.actime = t->actime;
2169 t64.modtime = t->modtime;
2170 return _wutime64( path, &t64 );
2173 /*********************************************************************
2174 * _wutime (MSVCRT.@)
2177 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2179 return _wutime64( path, t );
2182 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2184 return _wutime32( path, t );
2188 /*********************************************************************
2191 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2194 HANDLE hand = msvcrt_fdtoh(fd);
2196 /* Don't trace small writes, it gets *very* annoying */
2199 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2201 if (hand == INVALID_HANDLE_VALUE)
2203 *MSVCRT__errno() = MSVCRT_EBADF;
2207 /* If appending, go to EOF */
2208 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2209 MSVCRT__lseek(fd, 0, FILE_END);
2211 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2213 if (WriteFile(hand, buf, count, &num_written, NULL)
2214 && (num_written == count))
2216 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2217 hand, GetLastError());
2218 *MSVCRT__errno() = MSVCRT_ENOSPC;
2222 unsigned int i, j, nr_lf;
2225 const char *s = buf, *buf_start = buf;
2226 /* find number of \n ( without preceding \r ) */
2227 for ( nr_lf=0,i = 0; i <count; i++)
2232 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2237 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2239 for (s = buf, i = 0, j = 0; i < count; i++)
2244 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2251 FIXME("Malloc failed\n");
2259 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2261 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2262 fd, hand, GetLastError(), num_written);
2263 *MSVCRT__errno() = MSVCRT_ENOSPC;
2266 return s - buf_start;
2278 /*********************************************************************
2281 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2284 len = MSVCRT__write(file->_file, &val, sizeof(val));
2285 if (len == sizeof(val)) return val;
2286 file->_flag |= MSVCRT__IOERR;
2290 /*********************************************************************
2293 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2298 MSVCRT_free(file->_tmpfname);
2299 file->_tmpfname = NULL;
2300 /* flush stdio buffers */
2301 if(file->_flag & MSVCRT__IOWRT)
2302 MSVCRT_fflush(file);
2303 if(file->_flag & MSVCRT__IOMYBUF)
2304 MSVCRT_free(file->_base);
2306 r=MSVCRT__close(file->_file);
2310 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2313 /*********************************************************************
2316 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2318 return file->_flag & MSVCRT__IOEOF;
2321 /*********************************************************************
2324 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2326 return file->_flag & MSVCRT__IOERR;
2329 /*********************************************************************
2330 * _filbuf (MSVCRT.@)
2332 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2334 /* Allocate buffer if needed */
2335 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2336 msvcrt_alloc_buffer(file);
2338 if(!(file->_flag & MSVCRT__IOREAD)) {
2339 if(file->_flag & MSVCRT__IORW) {
2340 file->_flag |= MSVCRT__IOREAD;
2345 if(file->_flag & MSVCRT__IONBF) {
2348 if ((r = read_i(file->_file,&c,1)) != 1) {
2349 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2354 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2356 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2361 file->_ptr = file->_base+1;
2362 return *(unsigned char *)file->_base;
2366 /*********************************************************************
2369 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2375 i = (unsigned char *)file->_ptr++;
2378 j = MSVCRT__filbuf(file);
2382 /*********************************************************************
2383 * _fgetchar (MSVCRT.@)
2385 int CDECL _fgetchar(void)
2387 return MSVCRT_fgetc(MSVCRT_stdin);
2390 /*********************************************************************
2393 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2395 int cc = MSVCRT_EOF;
2396 char * buf_start = s;
2398 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2399 file,file->_file,s,size);
2401 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2406 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2408 TRACE(":nothing read\n");
2411 if ((cc != MSVCRT_EOF) && (size > 1))
2414 TRACE(":got %s\n", debugstr_a(buf_start));
2418 /*********************************************************************
2421 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2422 * the CR from CR/LF combinations
2424 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2428 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2435 for(i=0; i<sizeof(wc); i++)
2445 j = MSVCRT__filbuf(file);
2448 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2458 c = MSVCRT_fgetc(file);
2459 if ((MSVCRT___mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2461 FIXME("Treat Multibyte characters\n");
2463 if (c == MSVCRT_EOF)
2466 return (MSVCRT_wint_t)c;
2469 /*********************************************************************
2472 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2478 for (j=0; j<sizeof(int); j++) {
2479 k = MSVCRT_fgetc(file);
2480 if (k == MSVCRT_EOF) {
2481 file->_flag |= MSVCRT__IOEOF;
2489 /*********************************************************************
2492 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2494 return MSVCRT_fgetwc(file);
2497 /*********************************************************************
2498 * _fgetwchar (MSVCRT.@)
2500 MSVCRT_wint_t CDECL _fgetwchar(void)
2502 return MSVCRT_fgetwc(MSVCRT_stdin);
2505 /*********************************************************************
2506 * getwchar (MSVCRT.@)
2508 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2510 return _fgetwchar();
2513 /*********************************************************************
2516 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2518 int cc = MSVCRT_WEOF;
2519 MSVCRT_wchar_t * buf_start = s;
2521 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2522 file,file->_file,s,size);
2524 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2529 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2531 TRACE(":nothing read\n");
2534 if ((cc != MSVCRT_WEOF) && (size > 1))
2537 TRACE(":got %s\n", debugstr_w(buf_start));
2541 /*********************************************************************
2544 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2546 MSVCRT_size_t wrcnt=size * nmemb;
2551 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2552 memcpy(file->_ptr, ptr, pcnt);
2557 ptr = (const char*)ptr + pcnt;
2558 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2559 if(file->_flag & MSVCRT__IORW) {
2560 file->_flag |= MSVCRT__IOWRT;
2566 int res=msvcrt_flush_buffer(file);
2568 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2571 file->_flag |= MSVCRT__IOERR;
2574 written += pwritten;
2577 return written / size;
2580 /*********************************************************************
2583 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2585 MSVCRT_wchar_t mwc=wc;
2586 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2591 /*********************************************************************
2592 * _fputwchar (MSVCRT.@)
2594 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2596 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2599 /*********************************************************************
2600 * _wfsopen (MSVCRT.@)
2602 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2605 int open_flags, stream_flags, fd;
2607 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2609 /* map mode string to open() flags. "man fopen" for possibilities. */
2610 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2614 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2617 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2619 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2626 TRACE(":got (%p)\n",file);
2627 if (fd >= 0 && !file)
2633 /*********************************************************************
2634 * _fsopen (MSVCRT.@)
2636 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2639 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2641 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2642 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2648 ret = MSVCRT__wfsopen(pathW, modeW, share);
2655 /*********************************************************************
2658 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2660 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2663 /*********************************************************************
2664 * _wfopen (MSVCRT.@)
2666 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2668 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2671 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2672 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2674 /*********************************************************************
2677 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2684 int res = msvcrt_flush_buffer(file);
2685 return res ? res : c;
2690 return MSVCRT__flsbuf(c, file);
2694 /*********************************************************************
2695 * _flsbuf (MSVCRT.@)
2697 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2699 /* Flush output buffer */
2700 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2701 msvcrt_alloc_buffer(file);
2703 if(!(file->_flag & MSVCRT__IOWRT)) {
2704 if(file->_flag & MSVCRT__IORW) {
2705 file->_flag |= MSVCRT__IOWRT;
2711 int res=msvcrt_flush_buffer(file);
2712 return res?res : MSVCRT_fputc(c, file);
2716 /* set _cnt to 0 for unbuffered FILEs */
2718 len = MSVCRT__write(file->_file, &cc, 1);
2719 if (len == 1) return c & 0xff;
2720 file->_flag |= MSVCRT__IOERR;
2725 /*********************************************************************
2726 * _fputchar (MSVCRT.@)
2728 int CDECL _fputchar(int c)
2730 return MSVCRT_fputc(c, MSVCRT_stdout);
2733 /*********************************************************************
2736 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2737 { MSVCRT_size_t rcnt=size * nmemb;
2738 MSVCRT_size_t read=0;
2744 /* first buffered data */
2746 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2747 memcpy(ptr, file->_ptr, pcnt);
2752 ptr = (char*)ptr + pcnt;
2753 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2754 if(file->_flag & MSVCRT__IORW) {
2755 file->_flag |= MSVCRT__IOREAD;
2762 /* Fill the buffer on small reads.
2763 * TODO: Use a better buffering strategy.
2765 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2766 if (file->_bufsiz == 0) {
2767 msvcrt_alloc_buffer(file);
2769 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2770 file->_ptr = file->_base;
2771 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2772 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2773 if (i > 0 && i < file->_cnt) {
2774 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2775 file->_flag &= ~MSVCRT__IOEOF;
2778 memcpy(ptr, file->_ptr, i);
2783 i = MSVCRT__read(file->_file,ptr, rcnt);
2787 ptr = (char *)ptr+i;
2788 /* expose feof condition in the flags
2789 * MFC tests file->_flag for feof, and doesn't call feof())
2791 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2792 file->_flag |= MSVCRT__IOEOF;
2795 file->_flag |= MSVCRT__IOERR;
2805 /*********************************************************************
2806 * _wfreopen (MSVCRT.@)
2809 MSVCRT_FILE* CDECL _wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
2811 int open_flags, stream_flags, fd;
2813 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2816 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2820 MSVCRT_fclose(file);
2821 /* map mode string to open() flags. "man fopen" for possibilities. */
2822 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2826 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2829 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2832 WARN(":failed-last error (%d)\n",GetLastError());
2833 msvcrt_set_errno(GetLastError());
2842 /*********************************************************************
2843 * freopen (MSVCRT.@)
2846 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
2849 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2851 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2852 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2858 ret = _wfreopen(pathW, modeW, file);
2865 /*********************************************************************
2866 * fsetpos (MSVCRT.@)
2868 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2870 /* Note that all this has been lifted 'as is' from fseek */
2871 if(file->_flag & MSVCRT__IOWRT)
2872 msvcrt_flush_buffer(file);
2874 /* Discard buffered input */
2876 file->_ptr = file->_base;
2878 /* Reset direction of i/o */
2879 if(file->_flag & MSVCRT__IORW) {
2880 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2883 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2886 /*********************************************************************
2889 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2891 /* TODO: just call fgetpos and return lower half of result */
2894 pos = _tell(file->_file);
2895 if(pos == -1) return -1;
2897 if( file->_flag & MSVCRT__IOWRT ) {
2898 off = file->_ptr - file->_base;
2901 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2902 /* Black magic correction for CR removal */
2904 for (i=0; i<file->_cnt; i++) {
2905 if (file->_ptr[i] == '\n')
2908 /* Black magic when reading CR at buffer boundary*/
2909 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
2918 /*********************************************************************
2919 * fgetpos (MSVCRT.@)
2921 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2924 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
2925 if(*pos == -1) return -1;
2927 if( file->_flag & MSVCRT__IOWRT ) {
2928 off = file->_ptr - file->_base;
2931 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2932 /* Black magic correction for CR removal */
2934 for (i=0; i<file->_cnt; i++) {
2935 if (file->_ptr[i] == '\n')
2938 /* Black magic when reading CR at buffer boundary*/
2939 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
2948 /*********************************************************************
2951 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2953 MSVCRT_size_t i, len = strlen(s);
2954 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2955 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2956 for (i=0; i<len; i++)
2957 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2962 /*********************************************************************
2965 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2967 MSVCRT_size_t i, len = strlenW(s);
2968 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2969 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2970 for (i=0; i<len; i++)
2972 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2974 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2980 /*********************************************************************
2981 * getchar (MSVCRT.@)
2983 int CDECL MSVCRT_getchar(void)
2985 return MSVCRT_fgetc(MSVCRT_stdin);
2988 /*********************************************************************
2991 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
2993 return MSVCRT_fgetc(file);
2996 /*********************************************************************
2999 char * CDECL MSVCRT_gets(char *buf)
3002 char * buf_start = buf;
3004 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3005 cc = MSVCRT_fgetc(MSVCRT_stdin))
3006 if(cc != '\r') *buf++ = (char)cc;
3010 TRACE("got '%s'\n", buf_start);
3014 /*********************************************************************
3017 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3020 MSVCRT_wchar_t* ws = buf;
3022 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3023 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3026 *buf++ = (MSVCRT_wchar_t)cc;
3030 TRACE("got %s\n", debugstr_w(ws));
3034 /*********************************************************************
3037 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3039 return MSVCRT_fputc(c, file);
3042 /*********************************************************************
3043 * putchar (MSVCRT.@)
3045 int CDECL MSVCRT_putchar(int c)
3047 return MSVCRT_fputc(c, MSVCRT_stdout);
3050 /*********************************************************************
3053 int CDECL MSVCRT_puts(const char *s)
3055 MSVCRT_size_t len = strlen(s);
3056 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3057 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3060 /*********************************************************************
3063 int CDECL _putws(const MSVCRT_wchar_t *s)
3065 static const MSVCRT_wchar_t nl = '\n';
3066 MSVCRT_size_t len = strlenW(s);
3067 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3068 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3071 /*********************************************************************
3074 int CDECL MSVCRT_remove(const char *path)
3076 TRACE("(%s)\n",path);
3077 if (DeleteFileA(path))
3079 TRACE(":failed (%d)\n",GetLastError());
3080 msvcrt_set_errno(GetLastError());
3084 /*********************************************************************
3085 * _wremove (MSVCRT.@)
3087 int CDECL _wremove(const MSVCRT_wchar_t *path)
3089 TRACE("(%s)\n",debugstr_w(path));
3090 if (DeleteFileW(path))
3092 TRACE(":failed (%d)\n",GetLastError());
3093 msvcrt_set_errno(GetLastError());
3097 /*********************************************************************
3100 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3102 TRACE(":from %s to %s\n",oldpath,newpath);
3103 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3105 TRACE(":failed (%d)\n",GetLastError());
3106 msvcrt_set_errno(GetLastError());
3110 /*********************************************************************
3111 * _wrename (MSVCRT.@)
3113 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3115 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3116 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3118 TRACE(":failed (%d)\n",GetLastError());
3119 msvcrt_set_errno(GetLastError());
3123 /*********************************************************************
3124 * setvbuf (MSVCRT.@)
3126 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3128 /* TODO: Check if file busy */
3130 MSVCRT_free(file->_base);
3134 if(mode == MSVCRT__IOFBF) {
3135 file->_flag &= ~MSVCRT__IONBF;
3136 file->_base = file->_ptr = buf;
3138 file->_bufsiz = size;
3141 file->_flag |= MSVCRT__IONBF;
3146 /*********************************************************************
3149 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3151 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3154 /*********************************************************************
3157 char * CDECL MSVCRT_tmpnam(char *s)
3165 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3166 p = s + sprintf(s, "\\s%s.", tmpstr);
3167 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3169 msvcrt_int_to_base32(unique++, tmpstr);
3171 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3172 GetLastError() == ERROR_FILE_NOT_FOUND)
3178 /*********************************************************************
3179 * tmpfile (MSVCRT.@)
3181 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3183 char *filename = MSVCRT_tmpnam(NULL);
3185 MSVCRT_FILE* file = NULL;
3188 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3189 if (fd != -1 && (file = msvcrt_alloc_fp()))
3191 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3196 else file->_tmpfname = _strdup(filename);
3202 /*********************************************************************
3203 * vfprintf (MSVCRT.@)
3205 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3207 char buf[2048], *mem = buf;
3208 int written, resize = sizeof(buf), retval;
3209 /* There are two conventions for vsnprintf failing:
3210 * Return -1 if we truncated, or
3211 * Return the number of bytes that would have been written
3212 * The code below handles both cases
3214 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3217 resize = (written == -1 ? resize * 2 : written + 1);
3220 if (!(mem = MSVCRT_malloc(resize)))
3223 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3229 /*********************************************************************
3230 * vfwprintf (MSVCRT.@)
3232 * Is final char included in written (then resize is too big) or not
3233 * (then we must test for equality too)?
3235 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3237 MSVCRT_wchar_t buf[2048], *mem = buf;
3238 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3239 /* See vfprintf comments */
3240 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3243 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3246 if (!(mem = MSVCRT_malloc(resize*sizeof(*mem))))
3249 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3255 /*********************************************************************
3256 * vprintf (MSVCRT.@)
3258 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3260 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3263 /*********************************************************************
3264 * vwprintf (MSVCRT.@)
3266 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3268 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3271 /*********************************************************************
3272 * fprintf (MSVCRT.@)
3274 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3276 __ms_va_list valist;
3278 __ms_va_start(valist, format);
3279 res = MSVCRT_vfprintf(file, format, valist);
3280 __ms_va_end(valist);
3284 /*********************************************************************
3285 * fwprintf (MSVCRT.@)
3287 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3289 __ms_va_list valist;
3291 __ms_va_start(valist, format);
3292 res = MSVCRT_vfwprintf(file, format, valist);
3293 __ms_va_end(valist);
3297 /*********************************************************************
3300 int CDECL MSVCRT_printf(const char *format, ...)
3302 __ms_va_list valist;
3304 __ms_va_start(valist, format);
3305 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3306 __ms_va_end(valist);
3310 /*********************************************************************
3313 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3315 if (c == MSVCRT_EOF)
3317 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3318 msvcrt_alloc_buffer(file);
3321 if(file->_ptr>file->_base) {
3325 MSVCRT_clearerr(file);
3331 /*********************************************************************
3332 * ungetwc (MSVCRT.@)
3334 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3336 MSVCRT_wchar_t mwc = wc;
3337 char * pp = (char *)&mwc;
3339 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3340 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3346 /*********************************************************************
3347 * wprintf (MSVCRT.@)
3349 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3351 __ms_va_list valist;
3353 __ms_va_start(valist, format);
3354 res = MSVCRT_vwprintf(format, valist);
3355 __ms_va_end(valist);
3359 /*********************************************************************
3360 * _getmaxstdio (MSVCRT.@)
3362 int CDECL _getmaxstdio(void)
3364 FIXME("stub, always returns 512\n");
3368 /*********************************************************************
3369 * _setmaxstdio_ (MSVCRT.@)
3371 int CDECL _setmaxstdio(int newmax)
3378 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3382 /*********************************************************************
3383 * __pioinfo (MSVCRT.@)
3384 * FIXME: see MSVCRT_MAX_FILES define.
3386 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3387 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3388 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3389 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3390 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3391 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3392 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3393 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3394 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3395 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3396 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3397 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3400 /*********************************************************************
3401 * __badioinfo (MSVCRT.@)
3403 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };