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");
221 *MSVCRT__errno() = MSVCRT_ENFILE;
224 MSVCRT_fdesc[fd].handle = hand;
225 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
227 /* locate next free slot */
228 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
229 MSVCRT_fdstart = MSVCRT_fdend + 1;
231 while (MSVCRT_fdstart < MSVCRT_fdend &&
232 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
234 /* update last fd in use */
235 if (fd >= MSVCRT_fdend)
236 MSVCRT_fdend = fd + 1;
237 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
241 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
242 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
243 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
249 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
250 static int msvcrt_alloc_fd(HANDLE hand, int flag)
255 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
256 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
261 /* INTERNAL: Allocate a FILE* for an fd slot */
262 /* caller must hold the files lock */
263 static MSVCRT_FILE* msvcrt_alloc_fp(void)
267 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
269 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
271 if (!MSVCRT_fstreams[i])
273 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
275 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
277 return MSVCRT_fstreams[i];
283 /* INTERNAL: initialize a FILE* from an open fd */
284 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
286 TRACE(":fd (%d) allocating FILE*\n",fd);
287 if (!msvcrt_is_valid_fd(fd))
289 WARN(":invalid fd %d\n",fd);
290 *MSVCRT___doserrno() = 0;
291 *MSVCRT__errno() = MSVCRT_EBADF;
294 memset(file, 0, sizeof(*file));
296 file->_flag = stream_flags;
298 TRACE(":got FILE* (%p)\n",file);
302 /* INTERNAL: Create an inheritance data block (for spawned process)
303 * The inheritance block is made of:
304 * 00 int nb of file descriptor (NBFD)
305 * 04 char file flags (wxflag): repeated for each fd
306 * 4+NBFD HANDLE file handle: repeated for each fd
308 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
314 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
315 *block = MSVCRT_calloc(*size, 1);
321 wxflag_ptr = (char*)*block + sizeof(unsigned);
322 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
324 *(unsigned*)*block = MSVCRT_fdend;
325 for (fd = 0; fd < MSVCRT_fdend; fd++)
327 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
328 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
330 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
331 *handle_ptr = MSVCRT_fdesc[fd].handle;
336 *handle_ptr = INVALID_HANDLE_VALUE;
338 wxflag_ptr++; handle_ptr++;
343 /* INTERNAL: Set up all file descriptors,
344 * as well as default streams (stdin, stderr and stdout)
346 void msvcrt_init_io(void)
351 InitializeCriticalSection(&MSVCRT_file_cs);
352 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
353 GetStartupInfoA(&si);
354 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
360 count = *(unsigned*)si.lpReserved2;
361 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
362 handle_ptr = (HANDLE*)(wxflag_ptr + count);
364 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
365 count = min(count, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
366 for (i = 0; i < count; i++)
368 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
370 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
371 MSVCRT_fdesc[i].handle = *handle_ptr;
375 MSVCRT_fdesc[i].wxflag = 0;
376 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
378 wxflag_ptr++; handle_ptr++;
380 MSVCRT_fdend = max( 3, count );
381 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
382 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
385 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
387 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
388 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
389 GetCurrentProcess(), &MSVCRT_fdesc[0].handle,
390 0, TRUE, DUPLICATE_SAME_ACCESS))
391 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
393 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
395 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
396 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
397 GetCurrentProcess(), &MSVCRT_fdesc[1].handle,
398 0, TRUE, DUPLICATE_SAME_ACCESS))
399 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
401 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
403 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
404 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
405 GetCurrentProcess(), &MSVCRT_fdesc[2].handle,
406 0, TRUE, DUPLICATE_SAME_ACCESS))
407 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
410 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
411 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
413 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
414 for (i = 0; i < 3; i++)
416 /* FILE structs for stdin/out/err are static and never deleted */
417 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
418 MSVCRT__iob[i]._file = i;
419 MSVCRT__iob[i]._tmpfname = NULL;
420 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
422 MSVCRT_stream_idx = 3;
425 /* INTERNAL: Flush stdio file buffer */
426 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
429 int cnt=file->_ptr-file->_base;
430 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
431 file->_flag |= MSVCRT__IOERR;
434 file->_ptr=file->_base;
435 file->_cnt=file->_bufsiz;
440 /* INTERNAL: Allocate stdio file buffer */
441 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
443 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
445 file->_bufsiz = MSVCRT_BUFSIZ;
446 file->_flag |= MSVCRT__IOMYBUF;
448 file->_base = (char*)(&file->_charbuf);
450 file->_bufsiz = sizeof(file->_charbuf);
452 file->_ptr = file->_base;
456 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
457 static void msvcrt_int_to_base32(int num, char *str)
472 *p = (num & 31) + '0';
474 *p += ('a' - '0' - 10);
479 /*********************************************************************
480 * __iob_func(MSVCRT.@)
482 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
484 return &MSVCRT__iob[0];
487 /*********************************************************************
490 int CDECL MSVCRT__access(const char *filename, int mode)
492 DWORD attr = GetFileAttributesA(filename);
494 TRACE("(%s,%d) %d\n",filename,mode,attr);
496 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
498 msvcrt_set_errno(GetLastError());
501 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
503 msvcrt_set_errno(ERROR_ACCESS_DENIED);
509 /*********************************************************************
510 * _waccess (MSVCRT.@)
512 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
514 DWORD attr = GetFileAttributesW(filename);
516 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
518 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
520 msvcrt_set_errno(GetLastError());
523 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
525 msvcrt_set_errno(ERROR_ACCESS_DENIED);
531 /*********************************************************************
534 int CDECL MSVCRT__chmod(const char *path, int flags)
536 DWORD oldFlags = GetFileAttributesA(path);
538 if (oldFlags != INVALID_FILE_ATTRIBUTES)
540 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
541 oldFlags | FILE_ATTRIBUTE_READONLY;
543 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
546 msvcrt_set_errno(GetLastError());
550 /*********************************************************************
553 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
555 DWORD oldFlags = GetFileAttributesW(path);
557 if (oldFlags != INVALID_FILE_ATTRIBUTES)
559 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
560 oldFlags | FILE_ATTRIBUTE_READONLY;
562 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
565 msvcrt_set_errno(GetLastError());
569 /*********************************************************************
572 int CDECL MSVCRT__unlink(const char *path)
574 TRACE("%s\n",debugstr_a(path));
575 if(DeleteFileA(path))
577 TRACE("failed (%d)\n",GetLastError());
578 msvcrt_set_errno(GetLastError());
582 /*********************************************************************
583 * _wunlink (MSVCRT.@)
585 int CDECL _wunlink(const MSVCRT_wchar_t *path)
587 TRACE("(%s)\n",debugstr_w(path));
588 if(DeleteFileW(path))
590 TRACE("failed (%d)\n",GetLastError());
591 msvcrt_set_errno(GetLastError());
595 /* _flushall calls MSVCRT_fflush which calls _flushall */
596 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
598 /*********************************************************************
599 * _flushall (MSVCRT.@)
601 int CDECL _flushall(void)
603 int i, num_flushed = 0;
606 for (i = 3; i < MSVCRT_stream_idx; i++)
607 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
610 /* FIXME: flush, do not commit */
611 if (_commit(i) == -1)
612 if (MSVCRT_fstreams[i])
613 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
615 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
616 MSVCRT_fflush(MSVCRT_fstreams[i]);
622 TRACE(":flushed (%d) handles\n",num_flushed);
626 /*********************************************************************
629 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
633 } else if(file->_flag & MSVCRT__IOWRT) {
634 int res=msvcrt_flush_buffer(file);
640 /*********************************************************************
643 int CDECL MSVCRT__close(int fd)
649 hand = msvcrt_fdtoh(fd);
650 TRACE(":fd (%d) handle (%p)\n",fd,hand);
651 if (hand == INVALID_HANDLE_VALUE)
653 else if (!CloseHandle(hand))
655 WARN(":failed-last error (%d)\n",GetLastError());
656 msvcrt_set_errno(GetLastError());
669 /*********************************************************************
672 int CDECL _commit(int fd)
674 HANDLE hand = msvcrt_fdtoh(fd);
676 TRACE(":fd (%d) handle (%p)\n",fd,hand);
677 if (hand == INVALID_HANDLE_VALUE)
680 if (!FlushFileBuffers(hand))
682 if (GetLastError() == ERROR_INVALID_HANDLE)
684 /* FlushFileBuffers fails for console handles
685 * so we ignore this error.
689 TRACE(":failed-last error (%d)\n",GetLastError());
690 msvcrt_set_errno(GetLastError());
697 /*********************************************************************
700 * MSDN isn't clear on this point, but the remarks for _pipe
701 * indicate file descriptors duplicated with _dup and _dup2 are always
704 int CDECL MSVCRT__dup2(int od, int nd)
708 TRACE("(od=%d, nd=%d)\n", od, nd);
710 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
714 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
715 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
717 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
719 if (msvcrt_is_valid_fd(nd))
721 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
725 *MSVCRT__errno() = MSVCRT_EMFILE;
729 /* _dup2 returns 0, not nd, on success */
736 msvcrt_set_errno(GetLastError());
741 *MSVCRT__errno() = MSVCRT_EBADF;
748 /*********************************************************************
751 int CDECL MSVCRT__dup(int od)
757 if (MSVCRT__dup2(od, fd) == 0)
765 /*********************************************************************
768 int CDECL _eof(int fd)
771 LONG hcurpos,hendpos;
772 HANDLE hand = msvcrt_fdtoh(fd);
774 TRACE(":fd (%d) handle (%p)\n",fd,hand);
776 if (hand == INVALID_HANDLE_VALUE)
779 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
781 /* Otherwise we do it the hard way */
782 hcurpos = hendpos = 0;
783 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
784 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
786 if (curpos == endpos && hcurpos == hendpos)
788 /* FIXME: shouldn't WX_ATEOF be set here? */
792 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
796 /*********************************************************************
797 * _fcloseall (MSVCRT.@)
799 int CDECL MSVCRT__fcloseall(void)
801 int num_closed = 0, i;
804 for (i = 3; i < MSVCRT_stream_idx; i++)
805 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
806 !MSVCRT_fclose(MSVCRT_fstreams[i]))
810 TRACE(":closed (%d) handles\n",num_closed);
814 /* free everything on process exit */
815 void msvcrt_free_io(void)
818 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
819 * stdout, and stderr (unlike GNU), so we need to fclose() them here
820 * or they won't get flushed.
822 MSVCRT_fclose(&MSVCRT__iob[0]);
823 MSVCRT_fclose(&MSVCRT__iob[1]);
824 MSVCRT_fclose(&MSVCRT__iob[2]);
825 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
826 DeleteCriticalSection(&MSVCRT_file_cs);
829 /*********************************************************************
830 * _lseeki64 (MSVCRT.@)
832 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
834 HANDLE hand = msvcrt_fdtoh(fd);
835 LARGE_INTEGER ofs, ret;
837 TRACE(":fd (%d) handle (%p)\n",fd,hand);
838 if (hand == INVALID_HANDLE_VALUE)
841 if (whence < 0 || whence > 2)
843 *MSVCRT__errno() = MSVCRT_EINVAL;
847 TRACE(":fd (%d) to %s pos %s\n",
848 fd,wine_dbgstr_longlong(offset),
849 (whence==SEEK_SET)?"SEEK_SET":
850 (whence==SEEK_CUR)?"SEEK_CUR":
851 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
853 ofs.QuadPart = offset;
854 if (SetFilePointerEx(hand, ofs, &ret, whence))
856 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
857 /* FIXME: What if we seek _to_ EOF - is EOF set? */
861 TRACE(":error-last error (%d)\n",GetLastError());
862 msvcrt_set_errno(GetLastError());
866 /*********************************************************************
869 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
871 return MSVCRT__lseeki64(fd, offset, whence);
874 /*********************************************************************
875 * _locking (MSVCRT.@)
877 * This is untested; the underlying LockFile doesn't work yet.
879 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
883 HANDLE hand = msvcrt_fdtoh(fd);
885 TRACE(":fd (%d) handle (%p)\n",fd,hand);
886 if (hand == INVALID_HANDLE_VALUE)
889 if (mode < 0 || mode > 4)
891 *MSVCRT__errno() = MSVCRT_EINVAL;
895 TRACE(":fd (%d) by 0x%08x mode %s\n",
896 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
897 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
898 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
899 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
900 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
903 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
905 FIXME ("Seek failed\n");
906 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
909 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
912 ret = 1; /* just to satisfy gcc */
915 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
920 else if (mode == MSVCRT__LK_UNLCK)
921 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
923 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
924 /* FIXME - what about error settings? */
928 /*********************************************************************
931 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
933 /* Flush output if needed */
934 if(file->_flag & MSVCRT__IOWRT)
935 msvcrt_flush_buffer(file);
937 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
938 offset -= file->_cnt;
939 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
940 /* Black magic correction for CR removal */
942 for (i=0; i<file->_cnt; i++) {
943 if (file->_ptr[i] == '\n')
946 /* Black magic when reading CR at buffer boundary*/
947 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
951 /* Discard buffered input */
953 file->_ptr = file->_base;
954 /* Reset direction of i/o */
955 if(file->_flag & MSVCRT__IORW) {
956 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
958 /* Clear end of file flag */
959 file->_flag &= ~MSVCRT__IOEOF;
960 return (MSVCRT__lseek(file->_file,offset,whence) == -1)?-1:0;
963 /*********************************************************************
966 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
972 TRACE("(fd=%d, size=%d)\n", fd, size);
976 handle = msvcrt_fdtoh(fd);
977 if (handle != INVALID_HANDLE_VALUE)
979 /* save the current file pointer */
980 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
983 pos = MSVCRT__lseek(fd, size, SEEK_SET);
986 ret = SetEndOfFile(handle);
987 if (!ret) msvcrt_set_errno(GetLastError());
990 /* restore the file pointer */
991 MSVCRT__lseek(fd, cur, SEEK_SET);
999 /*********************************************************************
1000 * clearerr (MSVCRT.@)
1002 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1004 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1005 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1008 /*********************************************************************
1011 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1013 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1014 MSVCRT_fseek(file, 0L, SEEK_SET);
1015 MSVCRT_clearerr(file);
1018 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1020 int plus = strchrW(mode, '+') != NULL;
1025 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1026 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1029 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1030 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1033 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1034 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1037 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1038 *MSVCRT__errno() = MSVCRT_EINVAL;
1046 *open_flags |= MSVCRT__O_BINARY;
1047 *open_flags &= ~MSVCRT__O_TEXT;
1050 *open_flags |= MSVCRT__O_TEXT;
1051 *open_flags &= ~MSVCRT__O_BINARY;
1057 FIXME(":unknown flag %c not supported\n",mode[-1]);
1062 /*********************************************************************
1063 * _fdopen (MSVCRT.@)
1065 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1068 MSVCRT_wchar_t *modeW = NULL;
1070 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1072 ret = MSVCRT__wfdopen(fd, modeW);
1078 /*********************************************************************
1079 * _wfdopen (MSVCRT.@)
1081 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1083 int open_flags, stream_flags;
1086 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1089 if (!(file = msvcrt_alloc_fp()))
1091 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1096 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1102 /*********************************************************************
1103 * _filelength (MSVCRT.@)
1105 LONG CDECL MSVCRT__filelength(int fd)
1107 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1110 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1113 if (endPos != curPos)
1114 MSVCRT__lseek(fd, curPos, SEEK_SET);
1121 /*********************************************************************
1122 * _filelengthi64 (MSVCRT.@)
1124 __int64 CDECL MSVCRT__filelengthi64(int fd)
1126 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1129 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1132 if (endPos != curPos)
1133 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1140 /*********************************************************************
1141 * _fileno (MSVCRT.@)
1143 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1145 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1149 /*********************************************************************
1150 * _fstat64 (MSVCRT.@)
1152 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1156 BY_HANDLE_FILE_INFORMATION hfi;
1157 HANDLE hand = msvcrt_fdtoh(fd);
1159 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1160 if (hand == INVALID_HANDLE_VALUE)
1165 WARN(":failed-NULL buf\n");
1166 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1170 memset(&hfi, 0, sizeof(hfi));
1171 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1172 type = GetFileType(hand);
1173 if (type == FILE_TYPE_PIPE)
1175 buf->st_dev = buf->st_rdev = fd;
1176 buf->st_mode = S_IFIFO;
1179 else if (type == FILE_TYPE_CHAR)
1181 buf->st_dev = buf->st_rdev = fd;
1182 buf->st_mode = S_IFCHR;
1185 else /* FILE_TYPE_DISK etc. */
1187 if (!GetFileInformationByHandle(hand, &hfi))
1189 WARN(":failed-last error (%d)\n",GetLastError());
1190 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1193 buf->st_mode = S_IFREG | 0444;
1194 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1195 buf->st_mode |= 0222;
1196 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1197 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1199 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1200 buf->st_mtime = buf->st_ctime = dw;
1201 buf->st_nlink = hfi.nNumberOfLinks;
1203 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1208 /*********************************************************************
1209 * _fstati64 (MSVCRT.@)
1211 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1214 struct MSVCRT__stat64 buf64;
1216 ret = MSVCRT__fstat64(fd, &buf64);
1218 msvcrt_stat64_to_stati64(&buf64, buf);
1222 /*********************************************************************
1225 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1227 struct MSVCRT__stat64 buf64;
1229 ret = MSVCRT__fstat64(fd, &buf64);
1231 msvcrt_stat64_to_stat(&buf64, buf);
1235 /*********************************************************************
1236 * _futime64 (MSVCRT.@)
1238 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1240 HANDLE hand = msvcrt_fdtoh(fd);
1245 time_to_filetime( MSVCRT__time64(NULL), &at );
1250 time_to_filetime( t->actime, &at );
1251 time_to_filetime( t->modtime, &wt );
1254 if (!SetFileTime(hand, NULL, &at, &wt))
1256 msvcrt_set_errno(GetLastError());
1262 /*********************************************************************
1263 * _futime32 (MSVCRT.@)
1265 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1267 struct MSVCRT___utimbuf64 t64;
1268 t64.actime = t->actime;
1269 t64.modtime = t->modtime;
1270 return _futime64( fd, &t64 );
1273 /*********************************************************************
1274 * _futime (MSVCRT.@)
1277 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1279 return _futime64( fd, t );
1282 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1284 return _futime32( fd, t );
1288 /*********************************************************************
1289 * _get_osfhandle (MSVCRT.@)
1291 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1293 HANDLE hand = msvcrt_fdtoh(fd);
1294 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1296 return (MSVCRT_intptr_t)hand;
1299 /*********************************************************************
1300 * _isatty (MSVCRT.@)
1302 int CDECL _isatty(int fd)
1304 HANDLE hand = msvcrt_fdtoh(fd);
1306 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1307 if (hand == INVALID_HANDLE_VALUE)
1310 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1313 /*********************************************************************
1314 * _mktemp (MSVCRT.@)
1316 char * CDECL _mktemp(char *pattern)
1319 char *retVal = pattern;
1324 numX = (*pattern++ == 'X')? numX + 1 : 0;
1328 id = GetCurrentProcessId();
1332 int tempNum = id / 10;
1333 *pattern-- = id - (tempNum * 10) + '0';
1339 *pattern = letter++;
1340 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1341 GetLastError() == ERROR_FILE_NOT_FOUND)
1343 } while(letter <= 'z');
1347 /*********************************************************************
1348 * _wmktemp (MSVCRT.@)
1350 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1353 MSVCRT_wchar_t *retVal = pattern;
1355 MSVCRT_wchar_t letter = 'a';
1358 numX = (*pattern++ == 'X')? numX + 1 : 0;
1362 id = GetCurrentProcessId();
1366 int tempNum = id / 10;
1367 *pattern-- = id - (tempNum * 10) + '0';
1373 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1374 GetLastError() == ERROR_FILE_NOT_FOUND)
1376 *pattern = letter++;
1377 } while(letter != '|');
1381 static unsigned split_oflags(unsigned oflags)
1384 unsigned unsupp; /* until we support everything */
1386 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1387 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1388 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1389 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1390 else wxflags |= WX_TEXT; /* default to TEXT*/
1391 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1393 if ((unsupp = oflags & ~(
1394 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1395 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1396 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1397 MSVCRT__O_NOINHERIT|
1398 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1400 ERR(":unsupported oflags 0x%04x\n",unsupp);
1405 /*********************************************************************
1408 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1411 SECURITY_ATTRIBUTES sa;
1412 HANDLE readHandle, writeHandle;
1416 *MSVCRT__errno() = MSVCRT_EINVAL;
1420 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1421 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1422 sa.lpSecurityDescriptor = NULL;
1423 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1425 unsigned int wxflags = split_oflags(textmode);
1429 fd = msvcrt_alloc_fd(readHandle, wxflags);
1433 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1441 MSVCRT__close(pfds[0]);
1442 CloseHandle(writeHandle);
1443 *MSVCRT__errno() = MSVCRT_EMFILE;
1448 CloseHandle(readHandle);
1449 CloseHandle(writeHandle);
1450 *MSVCRT__errno() = MSVCRT_EMFILE;
1455 msvcrt_set_errno(GetLastError());
1460 /*********************************************************************
1463 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1467 DWORD access = 0, creation = 0, attrib;
1471 SECURITY_ATTRIBUTES sa;
1474 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1475 path, oflags, shflags);
1477 wxflag = split_oflags(oflags);
1478 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1480 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1481 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1482 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1485 if (oflags & MSVCRT__O_CREAT)
1487 __ms_va_start(ap, shflags);
1488 pmode = va_arg(ap, int);
1491 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1492 FIXME(": pmode 0x%04x ignored\n", pmode);
1494 WARN(": pmode 0x%04x ignored\n", pmode);
1496 if (oflags & MSVCRT__O_EXCL)
1497 creation = CREATE_NEW;
1498 else if (oflags & MSVCRT__O_TRUNC)
1499 creation = CREATE_ALWAYS;
1501 creation = OPEN_ALWAYS;
1503 else /* no MSVCRT__O_CREAT */
1505 if (oflags & MSVCRT__O_TRUNC)
1506 creation = TRUNCATE_EXISTING;
1508 creation = OPEN_EXISTING;
1513 case MSVCRT__SH_DENYRW:
1516 case MSVCRT__SH_DENYWR:
1517 sharing = FILE_SHARE_READ;
1519 case MSVCRT__SH_DENYRD:
1520 sharing = FILE_SHARE_WRITE;
1522 case MSVCRT__SH_DENYNO:
1523 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1526 ERR( "Unhandled shflags 0x%x\n", shflags );
1529 attrib = FILE_ATTRIBUTE_NORMAL;
1531 if (oflags & MSVCRT__O_TEMPORARY)
1533 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1535 sharing |= FILE_SHARE_DELETE;
1538 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1539 sa.lpSecurityDescriptor = NULL;
1540 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1542 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1544 if (hand == INVALID_HANDLE_VALUE) {
1545 WARN(":failed-last error (%d)\n",GetLastError());
1546 msvcrt_set_errno(GetLastError());
1550 fd = msvcrt_alloc_fd(hand, wxflag);
1552 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1556 /*********************************************************************
1557 * _wsopen (MSVCRT.@)
1559 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1563 DWORD access = 0, creation = 0, attrib;
1567 SECURITY_ATTRIBUTES sa;
1570 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1571 debugstr_w(path), oflags, shflags);
1573 wxflag = split_oflags(oflags);
1574 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1576 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1577 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1578 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1581 if (oflags & MSVCRT__O_CREAT)
1583 __ms_va_start(ap, shflags);
1584 pmode = va_arg(ap, int);
1587 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1588 FIXME(": pmode 0x%04x ignored\n", pmode);
1590 WARN(": pmode 0x%04x ignored\n", pmode);
1592 if (oflags & MSVCRT__O_EXCL)
1593 creation = CREATE_NEW;
1594 else if (oflags & MSVCRT__O_TRUNC)
1595 creation = CREATE_ALWAYS;
1597 creation = OPEN_ALWAYS;
1599 else /* no MSVCRT__O_CREAT */
1601 if (oflags & MSVCRT__O_TRUNC)
1602 creation = TRUNCATE_EXISTING;
1604 creation = OPEN_EXISTING;
1609 case MSVCRT__SH_DENYRW:
1612 case MSVCRT__SH_DENYWR:
1613 sharing = FILE_SHARE_READ;
1615 case MSVCRT__SH_DENYRD:
1616 sharing = FILE_SHARE_WRITE;
1618 case MSVCRT__SH_DENYNO:
1619 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1622 ERR( "Unhandled shflags 0x%x\n", shflags );
1625 attrib = FILE_ATTRIBUTE_NORMAL;
1627 if (oflags & MSVCRT__O_TEMPORARY)
1629 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1631 sharing |= FILE_SHARE_DELETE;
1634 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1635 sa.lpSecurityDescriptor = NULL;
1636 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1638 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1640 if (hand == INVALID_HANDLE_VALUE) {
1641 WARN(":failed-last error (%d)\n",GetLastError());
1642 msvcrt_set_errno(GetLastError());
1646 fd = msvcrt_alloc_fd(hand, wxflag);
1648 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1652 /*********************************************************************
1655 int CDECL MSVCRT__open( const char *path, int flags, ... )
1659 if (flags & MSVCRT__O_CREAT)
1662 __ms_va_start(ap, flags);
1663 pmode = va_arg(ap, int);
1665 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1668 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1671 /*********************************************************************
1674 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1678 if (flags & MSVCRT__O_CREAT)
1681 __ms_va_start(ap, flags);
1682 pmode = va_arg(ap, int);
1684 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1687 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1690 /*********************************************************************
1693 int CDECL MSVCRT__creat(const char *path, int flags)
1695 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1696 return MSVCRT__open(path, usedFlags);
1699 /*********************************************************************
1700 * _wcreat (MSVCRT.@)
1702 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1704 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1705 return _wopen(path, usedFlags);
1708 /*********************************************************************
1709 * _open_osfhandle (MSVCRT.@)
1711 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1715 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1716 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1717 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1718 * text - it never sets MSVCRT__O_BINARY.
1720 /* don't let split_oflags() decide the mode if no mode is passed */
1721 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1722 oflags |= MSVCRT__O_BINARY;
1724 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1725 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1729 /*********************************************************************
1732 int CDECL _rmtmp(void)
1734 int num_removed = 0, i;
1737 for (i = 3; i < MSVCRT_stream_idx; i++)
1738 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1740 MSVCRT_fclose(MSVCRT_fstreams[i]);
1746 TRACE(":removed (%d) temp files\n",num_removed);
1750 /*********************************************************************
1753 * When reading \r as last character in text mode, read() positions
1754 * the file pointer on the \r character while getc() goes on to
1757 static int read_i(int fd, void *buf, unsigned int count)
1760 char *bufstart = buf;
1761 HANDLE hand = msvcrt_fdtoh(fd);
1766 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1767 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1768 TRACE("already at EOF, returning 0\n");
1771 /* Don't trace small reads, it gets *very* annoying */
1773 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1774 if (hand == INVALID_HANDLE_VALUE)
1777 /* Reading single bytes in O_TEXT mode makes things slow
1778 * So read big chunks
1780 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1782 if (count != 0 && num_read == 0)
1784 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1785 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1787 else if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1790 if (bufstart[num_read-1] == '\r')
1794 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1795 ReadFile(hand, bufstart, 1, &num_read, NULL);
1799 MSVCRT_fdesc[fd].wxflag |= WX_READCR;
1804 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1805 for (i=0, j=0; i<num_read; i++)
1807 /* in text mode, a ctrl-z signals EOF */
1808 if (bufstart[i] == 0x1a)
1810 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1811 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1814 /* in text mode, strip \r if followed by \n.
1815 * BUG: should save state across calls somehow, so CR LF that
1816 * straddles buffer boundary gets recognized properly?
1818 if ((bufstart[i] != '\r')
1819 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1820 bufstart[j++] = bufstart[i];
1827 if (GetLastError() == ERROR_BROKEN_PIPE)
1829 TRACE(":end-of-pipe\n");
1830 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1835 TRACE(":failed-last error (%d)\n",GetLastError());
1841 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1845 /*********************************************************************
1848 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1851 num_read = read_i(fd, buf, count);
1855 /*********************************************************************
1856 * _setmode (MSVCRT.@)
1858 int CDECL _setmode(int fd,int mode)
1860 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1861 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1862 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1863 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1864 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1866 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1870 /*********************************************************************
1871 * _stat64 (MSVCRT.@)
1873 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
1876 WIN32_FILE_ATTRIBUTE_DATA hfi;
1877 unsigned short mode = ALL_S_IREAD;
1880 TRACE(":file (%s) buf(%p)\n",path,buf);
1882 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1884 TRACE("failed (%d)\n",GetLastError());
1885 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1889 memset(buf,0,sizeof(struct MSVCRT__stat64));
1891 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1892 Bon 011120: This FIXME seems incorrect
1893 Also a letter as first char isn't enough to be classified
1896 if (isalpha(*path)&& (*(path+1)==':'))
1897 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1899 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1901 plen = strlen(path);
1903 /* Dir, or regular file? */
1904 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1905 (path[plen-1] == '\\'))
1906 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1909 mode |= MSVCRT__S_IFREG;
1911 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1913 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1914 (tolower(path[plen-3]) << 16);
1915 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1916 mode |= ALL_S_IEXEC;
1920 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1921 mode |= ALL_S_IWRITE;
1923 buf->st_mode = mode;
1925 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1926 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1928 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1929 buf->st_mtime = buf->st_ctime = dw;
1930 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
1931 (int)(buf->st_size >> 32),(int)buf->st_size,
1932 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
1936 /*********************************************************************
1937 * _stati64 (MSVCRT.@)
1939 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
1942 struct MSVCRT__stat64 buf64;
1944 ret = MSVCRT_stat64(path, &buf64);
1946 msvcrt_stat64_to_stati64(&buf64, buf);
1950 /*********************************************************************
1953 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
1955 struct MSVCRT__stat64 buf64;
1957 ret = MSVCRT_stat64( path, &buf64);
1959 msvcrt_stat64_to_stat(&buf64, buf);
1963 /*********************************************************************
1964 * _wstat64 (MSVCRT.@)
1966 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1969 WIN32_FILE_ATTRIBUTE_DATA hfi;
1970 unsigned short mode = ALL_S_IREAD;
1973 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1975 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1977 TRACE("failed (%d)\n",GetLastError());
1978 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1982 memset(buf,0,sizeof(struct MSVCRT__stat64));
1984 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1985 if (MSVCRT_iswalpha(*path))
1986 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1988 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1990 plen = strlenW(path);
1992 /* Dir, or regular file? */
1993 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1994 (path[plen-1] == '\\'))
1995 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1998 mode |= MSVCRT__S_IFREG;
2000 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2002 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2003 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2004 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2005 mode |= ALL_S_IEXEC;
2009 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2010 mode |= ALL_S_IWRITE;
2012 buf->st_mode = mode;
2014 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2015 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2017 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2018 buf->st_mtime = buf->st_ctime = dw;
2019 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2020 (int)(buf->st_size >> 32),(int)buf->st_size,
2021 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2025 /*********************************************************************
2026 * _wstati64 (MSVCRT.@)
2028 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2031 struct MSVCRT__stat64 buf64;
2033 ret = MSVCRT__wstat64(path, &buf64);
2035 msvcrt_stat64_to_stati64(&buf64, buf);
2039 /*********************************************************************
2042 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2045 struct MSVCRT__stat64 buf64;
2047 ret = MSVCRT__wstat64( path, &buf64 );
2048 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2052 /*********************************************************************
2055 MSVCRT_long CDECL MSVCRT__tell(int fd)
2057 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2060 /*********************************************************************
2061 * _telli64 (MSVCRT.@)
2063 __int64 CDECL _telli64(int fd)
2065 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2068 /*********************************************************************
2069 * _tempnam (MSVCRT.@)
2071 char * CDECL _tempnam(const char *dir, const char *prefix)
2073 char tmpbuf[MAX_PATH];
2074 const char *tmp_dir = MSVCRT_getenv("TMP");
2076 if (tmp_dir) dir = tmp_dir;
2078 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2079 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2081 TRACE("got name (%s)\n",tmpbuf);
2082 DeleteFileA(tmpbuf);
2083 return _strdup(tmpbuf);
2085 TRACE("failed (%d)\n",GetLastError());
2089 /*********************************************************************
2090 * _wtempnam (MSVCRT.@)
2092 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2094 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2096 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2097 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2099 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2100 DeleteFileW(tmpbuf);
2101 return _wcsdup(tmpbuf);
2103 TRACE("failed (%d)\n",GetLastError());
2107 /*********************************************************************
2110 int CDECL MSVCRT__umask(int umask)
2112 int old_umask = MSVCRT_umask;
2113 TRACE("(%d)\n",umask);
2114 MSVCRT_umask = umask;
2118 /*********************************************************************
2119 * _utime64 (MSVCRT.@)
2121 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2123 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2127 int retVal = _futime64(fd, t);
2134 /*********************************************************************
2135 * _utime32 (MSVCRT.@)
2137 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2139 struct MSVCRT___utimbuf64 t64;
2140 t64.actime = t->actime;
2141 t64.modtime = t->modtime;
2142 return _utime64( path, &t64 );
2145 /*********************************************************************
2149 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2151 return _utime64( path, t );
2154 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2156 return _utime32( path, t );
2160 /*********************************************************************
2161 * _wutime64 (MSVCRT.@)
2163 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2165 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2169 int retVal = _futime64(fd, t);
2176 /*********************************************************************
2177 * _wutime32 (MSVCRT.@)
2179 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2181 struct MSVCRT___utimbuf64 t64;
2182 t64.actime = t->actime;
2183 t64.modtime = t->modtime;
2184 return _wutime64( path, &t64 );
2187 /*********************************************************************
2188 * _wutime (MSVCRT.@)
2191 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2193 return _wutime64( path, t );
2196 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2198 return _wutime32( path, t );
2202 /*********************************************************************
2205 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2208 HANDLE hand = msvcrt_fdtoh(fd);
2210 /* Don't trace small writes, it gets *very* annoying */
2213 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2215 if (hand == INVALID_HANDLE_VALUE)
2217 *MSVCRT__errno() = MSVCRT_EBADF;
2221 /* If appending, go to EOF */
2222 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2223 MSVCRT__lseek(fd, 0, FILE_END);
2225 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2227 if (WriteFile(hand, buf, count, &num_written, NULL)
2228 && (num_written == count))
2230 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2231 hand, GetLastError());
2232 *MSVCRT__errno() = MSVCRT_ENOSPC;
2236 unsigned int i, j, nr_lf;
2239 const char *s = buf, *buf_start = buf;
2240 /* find number of \n ( without preceding \r ) */
2241 for ( nr_lf=0,i = 0; i <count; i++)
2246 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2251 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2253 for (s = buf, i = 0, j = 0; i < count; i++)
2258 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2265 FIXME("Malloc failed\n");
2273 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2275 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2276 fd, hand, GetLastError(), num_written);
2277 *MSVCRT__errno() = MSVCRT_ENOSPC;
2280 return s - buf_start;
2292 /*********************************************************************
2295 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2298 len = MSVCRT__write(file->_file, &val, sizeof(val));
2299 if (len == sizeof(val)) return val;
2300 file->_flag |= MSVCRT__IOERR;
2304 /*********************************************************************
2307 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2312 MSVCRT_free(file->_tmpfname);
2313 file->_tmpfname = NULL;
2314 /* flush stdio buffers */
2315 if(file->_flag & MSVCRT__IOWRT)
2316 MSVCRT_fflush(file);
2317 if(file->_flag & MSVCRT__IOMYBUF)
2318 MSVCRT_free(file->_base);
2320 r=MSVCRT__close(file->_file);
2324 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2327 /*********************************************************************
2330 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2332 return file->_flag & MSVCRT__IOEOF;
2335 /*********************************************************************
2338 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2340 return file->_flag & MSVCRT__IOERR;
2343 /*********************************************************************
2344 * _filbuf (MSVCRT.@)
2346 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2348 /* Allocate buffer if needed */
2349 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2350 msvcrt_alloc_buffer(file);
2352 if(!(file->_flag & MSVCRT__IOREAD)) {
2353 if(file->_flag & MSVCRT__IORW) {
2354 file->_flag |= MSVCRT__IOREAD;
2359 if(file->_flag & MSVCRT__IONBF) {
2362 if ((r = read_i(file->_file,&c,1)) != 1) {
2363 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2368 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2370 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2375 file->_ptr = file->_base+1;
2376 return *(unsigned char *)file->_base;
2380 /*********************************************************************
2383 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2389 i = (unsigned char *)file->_ptr++;
2392 j = MSVCRT__filbuf(file);
2396 /*********************************************************************
2397 * _fgetchar (MSVCRT.@)
2399 int CDECL _fgetchar(void)
2401 return MSVCRT_fgetc(MSVCRT_stdin);
2404 /*********************************************************************
2407 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2409 int cc = MSVCRT_EOF;
2410 char * buf_start = s;
2412 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2413 file,file->_file,s,size);
2415 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2420 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2422 TRACE(":nothing read\n");
2425 if ((cc != MSVCRT_EOF) && (size > 1))
2428 TRACE(":got %s\n", debugstr_a(buf_start));
2432 /*********************************************************************
2435 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2436 * the CR from CR/LF combinations
2438 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2442 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2449 for(i=0; i<sizeof(wc); i++)
2459 j = MSVCRT__filbuf(file);
2462 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2472 c = MSVCRT_fgetc(file);
2473 if ((get_locale()->locinfo->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2475 FIXME("Treat Multibyte characters\n");
2477 if (c == MSVCRT_EOF)
2480 return (MSVCRT_wint_t)c;
2483 /*********************************************************************
2486 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2492 for (j=0; j<sizeof(int); j++) {
2493 k = MSVCRT_fgetc(file);
2494 if (k == MSVCRT_EOF) {
2495 file->_flag |= MSVCRT__IOEOF;
2503 /*********************************************************************
2506 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2508 return MSVCRT_fgetwc(file);
2511 /*********************************************************************
2512 * _fgetwchar (MSVCRT.@)
2514 MSVCRT_wint_t CDECL _fgetwchar(void)
2516 return MSVCRT_fgetwc(MSVCRT_stdin);
2519 /*********************************************************************
2520 * getwchar (MSVCRT.@)
2522 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2524 return _fgetwchar();
2527 /*********************************************************************
2530 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2532 int cc = MSVCRT_WEOF;
2533 MSVCRT_wchar_t * buf_start = s;
2535 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2536 file,file->_file,s,size);
2538 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2543 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2545 TRACE(":nothing read\n");
2548 if ((cc != MSVCRT_WEOF) && (size > 1))
2551 TRACE(":got %s\n", debugstr_w(buf_start));
2555 /*********************************************************************
2558 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2560 MSVCRT_size_t wrcnt=size * nmemb;
2565 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2566 memcpy(file->_ptr, ptr, pcnt);
2571 ptr = (const char*)ptr + pcnt;
2572 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2573 if(file->_flag & MSVCRT__IORW) {
2574 file->_flag |= MSVCRT__IOWRT;
2580 int res=msvcrt_flush_buffer(file);
2582 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2585 file->_flag |= MSVCRT__IOERR;
2588 written += pwritten;
2591 return written / size;
2594 /*********************************************************************
2597 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2599 MSVCRT_wchar_t mwc=wc;
2600 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2605 /*********************************************************************
2606 * _fputwchar (MSVCRT.@)
2608 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2610 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2613 /*********************************************************************
2614 * _wfsopen (MSVCRT.@)
2616 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2619 int open_flags, stream_flags, fd;
2621 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2623 /* map mode string to open() flags. "man fopen" for possibilities. */
2624 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2628 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2631 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2633 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2640 TRACE(":got (%p)\n",file);
2641 if (fd >= 0 && !file)
2647 /*********************************************************************
2648 * _fsopen (MSVCRT.@)
2650 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2653 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2655 if (path && !(pathW = msvcrt_wstrdupa(path))) {
2656 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2657 *MSVCRT__errno() = MSVCRT_EINVAL;
2660 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2663 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2664 *MSVCRT__errno() = MSVCRT_EINVAL;
2668 ret = MSVCRT__wfsopen(pathW, modeW, share);
2675 /*********************************************************************
2678 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2680 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2683 /*********************************************************************
2684 * fopen_s (MSVCRT.@)
2686 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
2687 const char *filename, const char *mode)
2690 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2691 *MSVCRT__errno() = MSVCRT_EINVAL;
2692 return MSVCRT_EINVAL;
2695 *pFile = MSVCRT_fopen(filename, mode);
2698 return *MSVCRT__errno();
2702 /*********************************************************************
2703 * _wfopen (MSVCRT.@)
2705 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2707 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2710 /*********************************************************************
2711 * _wfopen_s (MSVCRT.@)
2713 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
2714 const MSVCRT_wchar_t *mode)
2717 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2718 *MSVCRT__errno() = MSVCRT_EINVAL;
2719 return MSVCRT_EINVAL;
2722 *pFile = MSVCRT__wfopen(filename, mode);
2725 return *MSVCRT__errno();
2729 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2730 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2732 /*********************************************************************
2735 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2742 int res = msvcrt_flush_buffer(file);
2743 return res ? res : c;
2748 return MSVCRT__flsbuf(c, file);
2752 /*********************************************************************
2753 * _flsbuf (MSVCRT.@)
2755 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2757 /* Flush output buffer */
2758 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2759 msvcrt_alloc_buffer(file);
2761 if(!(file->_flag & MSVCRT__IOWRT)) {
2762 if(file->_flag & MSVCRT__IORW) {
2763 file->_flag |= MSVCRT__IOWRT;
2769 int res=msvcrt_flush_buffer(file);
2770 return res?res : MSVCRT_fputc(c, file);
2774 /* set _cnt to 0 for unbuffered FILEs */
2776 len = MSVCRT__write(file->_file, &cc, 1);
2777 if (len == 1) return c & 0xff;
2778 file->_flag |= MSVCRT__IOERR;
2783 /*********************************************************************
2784 * _fputchar (MSVCRT.@)
2786 int CDECL _fputchar(int c)
2788 return MSVCRT_fputc(c, MSVCRT_stdout);
2791 /*********************************************************************
2794 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2795 { MSVCRT_size_t rcnt=size * nmemb;
2796 MSVCRT_size_t read=0;
2802 /* first buffered data */
2804 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2805 memcpy(ptr, file->_ptr, pcnt);
2810 ptr = (char*)ptr + pcnt;
2811 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2812 if(file->_flag & MSVCRT__IORW) {
2813 file->_flag |= MSVCRT__IOREAD;
2820 /* Fill the buffer on small reads.
2821 * TODO: Use a better buffering strategy.
2823 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2824 if (file->_bufsiz == 0) {
2825 msvcrt_alloc_buffer(file);
2827 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2828 file->_ptr = file->_base;
2829 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2830 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2831 if (i > 0 && i < file->_cnt) {
2832 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2833 file->_flag &= ~MSVCRT__IOEOF;
2836 memcpy(ptr, file->_ptr, i);
2841 i = MSVCRT__read(file->_file,ptr, rcnt);
2845 ptr = (char *)ptr+i;
2846 /* expose feof condition in the flags
2847 * MFC tests file->_flag for feof, and doesn't call feof())
2849 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2850 file->_flag |= MSVCRT__IOEOF;
2853 file->_flag |= MSVCRT__IOERR;
2863 /*********************************************************************
2864 * _wfreopen (MSVCRT.@)
2867 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
2869 int open_flags, stream_flags, fd;
2871 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2874 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2878 MSVCRT_fclose(file);
2879 /* map mode string to open() flags. "man fopen" for possibilities. */
2880 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2884 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2887 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2890 WARN(":failed-last error (%d)\n",GetLastError());
2891 msvcrt_set_errno(GetLastError());
2900 /*********************************************************************
2901 * freopen (MSVCRT.@)
2904 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
2907 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2909 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2910 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2916 ret = MSVCRT__wfreopen(pathW, modeW, file);
2923 /*********************************************************************
2924 * fsetpos (MSVCRT.@)
2926 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2928 /* Note that all this has been lifted 'as is' from fseek */
2929 if(file->_flag & MSVCRT__IOWRT)
2930 msvcrt_flush_buffer(file);
2932 /* Discard buffered input */
2934 file->_ptr = file->_base;
2936 /* Reset direction of i/o */
2937 if(file->_flag & MSVCRT__IORW) {
2938 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2941 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2944 /*********************************************************************
2947 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2949 /* TODO: just call fgetpos and return lower half of result */
2952 pos = MSVCRT__tell(file->_file);
2953 if(pos == -1) return -1;
2955 if( file->_flag & MSVCRT__IOWRT ) {
2956 off = file->_ptr - file->_base;
2959 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2960 /* Black magic correction for CR removal */
2962 for (i=0; i<file->_cnt; i++) {
2963 if (file->_ptr[i] == '\n')
2966 /* Black magic when reading CR at buffer boundary*/
2967 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
2976 /*********************************************************************
2977 * fgetpos (MSVCRT.@)
2979 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2982 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
2983 if(*pos == -1) return -1;
2985 if( file->_flag & MSVCRT__IOWRT ) {
2986 off = file->_ptr - file->_base;
2989 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2990 /* Black magic correction for CR removal */
2992 for (i=0; i<file->_cnt; i++) {
2993 if (file->_ptr[i] == '\n')
2996 /* Black magic when reading CR at buffer boundary*/
2997 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3006 /*********************************************************************
3009 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3011 MSVCRT_size_t i, len = strlen(s);
3012 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3013 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3014 for (i=0; i<len; i++)
3015 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
3020 /*********************************************************************
3023 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3025 MSVCRT_size_t i, len = strlenW(s);
3026 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3027 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3028 for (i=0; i<len; i++)
3030 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3032 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
3038 /*********************************************************************
3039 * getchar (MSVCRT.@)
3041 int CDECL MSVCRT_getchar(void)
3043 return MSVCRT_fgetc(MSVCRT_stdin);
3046 /*********************************************************************
3049 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3051 return MSVCRT_fgetc(file);
3054 /*********************************************************************
3057 char * CDECL MSVCRT_gets(char *buf)
3060 char * buf_start = buf;
3062 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3063 cc = MSVCRT_fgetc(MSVCRT_stdin))
3064 if(cc != '\r') *buf++ = (char)cc;
3068 TRACE("got '%s'\n", buf_start);
3072 /*********************************************************************
3075 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3078 MSVCRT_wchar_t* ws = buf;
3080 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3081 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3084 *buf++ = (MSVCRT_wchar_t)cc;
3088 TRACE("got %s\n", debugstr_w(ws));
3092 /*********************************************************************
3095 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3097 return MSVCRT_fputc(c, file);
3100 /*********************************************************************
3101 * putchar (MSVCRT.@)
3103 int CDECL MSVCRT_putchar(int c)
3105 return MSVCRT_fputc(c, MSVCRT_stdout);
3108 /*********************************************************************
3111 int CDECL MSVCRT_puts(const char *s)
3113 MSVCRT_size_t len = strlen(s);
3114 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3115 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3118 /*********************************************************************
3121 int CDECL _putws(const MSVCRT_wchar_t *s)
3123 static const MSVCRT_wchar_t nl = '\n';
3124 MSVCRT_size_t len = strlenW(s);
3125 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3126 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3129 /*********************************************************************
3132 int CDECL MSVCRT_remove(const char *path)
3134 TRACE("(%s)\n",path);
3135 if (DeleteFileA(path))
3137 TRACE(":failed (%d)\n",GetLastError());
3138 msvcrt_set_errno(GetLastError());
3142 /*********************************************************************
3143 * _wremove (MSVCRT.@)
3145 int CDECL _wremove(const MSVCRT_wchar_t *path)
3147 TRACE("(%s)\n",debugstr_w(path));
3148 if (DeleteFileW(path))
3150 TRACE(":failed (%d)\n",GetLastError());
3151 msvcrt_set_errno(GetLastError());
3155 /*********************************************************************
3158 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3160 TRACE(":from %s to %s\n",oldpath,newpath);
3161 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3163 TRACE(":failed (%d)\n",GetLastError());
3164 msvcrt_set_errno(GetLastError());
3168 /*********************************************************************
3169 * _wrename (MSVCRT.@)
3171 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3173 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3174 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3176 TRACE(":failed (%d)\n",GetLastError());
3177 msvcrt_set_errno(GetLastError());
3181 /*********************************************************************
3182 * setvbuf (MSVCRT.@)
3184 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3186 /* TODO: Check if file busy */
3188 MSVCRT_free(file->_base);
3192 if(mode == MSVCRT__IOFBF) {
3193 file->_flag &= ~MSVCRT__IONBF;
3194 file->_base = file->_ptr = buf;
3196 file->_bufsiz = size;
3199 file->_flag |= MSVCRT__IONBF;
3204 /*********************************************************************
3207 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3209 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3212 /*********************************************************************
3215 char * CDECL MSVCRT_tmpnam(char *s)
3223 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3224 p = s + sprintf(s, "\\s%s.", tmpstr);
3225 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3227 msvcrt_int_to_base32(unique++, tmpstr);
3229 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3230 GetLastError() == ERROR_FILE_NOT_FOUND)
3236 /*********************************************************************
3237 * tmpfile (MSVCRT.@)
3239 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3241 char *filename = MSVCRT_tmpnam(NULL);
3243 MSVCRT_FILE* file = NULL;
3246 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3247 if (fd != -1 && (file = msvcrt_alloc_fp()))
3249 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3254 else file->_tmpfname = _strdup(filename);
3260 /*********************************************************************
3261 * vfprintf (MSVCRT.@)
3263 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3265 char buf[2048], *mem = buf;
3266 int written, resize = sizeof(buf), retval;
3267 /* There are two conventions for vsnprintf failing:
3268 * Return -1 if we truncated, or
3269 * Return the number of bytes that would have been written
3270 * The code below handles both cases
3272 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3275 resize = (written == -1 ? resize * 2 : written + 1);
3278 if (!(mem = MSVCRT_malloc(resize)))
3281 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3287 /*********************************************************************
3288 * vfwprintf (MSVCRT.@)
3290 * Is final char included in written (then resize is too big) or not
3291 * (then we must test for equality too)?
3293 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3295 MSVCRT_wchar_t buf[2048], *mem = buf;
3296 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3297 /* See vfprintf comments */
3298 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3301 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3304 if (!(mem = MSVCRT_malloc(resize*sizeof(*mem))))
3307 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3313 /*********************************************************************
3314 * vprintf (MSVCRT.@)
3316 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3318 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3321 /*********************************************************************
3322 * vwprintf (MSVCRT.@)
3324 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3326 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3329 /*********************************************************************
3330 * fprintf (MSVCRT.@)
3332 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3334 __ms_va_list valist;
3336 __ms_va_start(valist, format);
3337 res = MSVCRT_vfprintf(file, format, valist);
3338 __ms_va_end(valist);
3342 /*********************************************************************
3343 * fwprintf (MSVCRT.@)
3345 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3347 __ms_va_list valist;
3349 __ms_va_start(valist, format);
3350 res = MSVCRT_vfwprintf(file, format, valist);
3351 __ms_va_end(valist);
3355 /*********************************************************************
3358 int CDECL MSVCRT_printf(const char *format, ...)
3360 __ms_va_list valist;
3362 __ms_va_start(valist, format);
3363 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3364 __ms_va_end(valist);
3368 /*********************************************************************
3371 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3373 if (c == MSVCRT_EOF)
3375 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3376 msvcrt_alloc_buffer(file);
3379 if(file->_ptr>file->_base) {
3383 MSVCRT_clearerr(file);
3389 /*********************************************************************
3390 * ungetwc (MSVCRT.@)
3392 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3394 MSVCRT_wchar_t mwc = wc;
3395 char * pp = (char *)&mwc;
3397 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3398 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3404 /*********************************************************************
3405 * wprintf (MSVCRT.@)
3407 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3409 __ms_va_list valist;
3411 __ms_va_start(valist, format);
3412 res = MSVCRT_vwprintf(format, valist);
3413 __ms_va_end(valist);
3417 /*********************************************************************
3418 * _getmaxstdio (MSVCRT.@)
3420 int CDECL _getmaxstdio(void)
3422 FIXME("stub, always returns 512\n");
3426 /*********************************************************************
3427 * _setmaxstdio_ (MSVCRT.@)
3429 int CDECL _setmaxstdio(int newmax)
3436 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3440 /*********************************************************************
3441 * __pioinfo (MSVCRT.@)
3442 * FIXME: see MSVCRT_MAX_FILES define.
3444 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3445 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3446 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3447 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3448 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3449 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3450 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3451 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3452 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3453 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3454 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3455 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3458 /*********************************************************************
3459 * __badioinfo (MSVCRT.@)
3461 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };