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
58 #define MSVCRT_R_OK 0x04
60 /* values for wxflag in file descriptor */
63 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
64 #define WX_READCR 0x08 /* underlying file is at \r */
65 #define WX_DONTINHERIT 0x10
66 #define WX_APPEND 0x20
69 /* FIXME: this should be allocated dynamically */
70 #define MSVCRT_MAX_FILES 2048
75 DWORD unkn[7]; /* critical section and init flag */
78 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
80 MSVCRT_FILE MSVCRT__iob[3] = { { 0 } };
82 static int MSVCRT_fdstart = 3; /* first unallocated fd */
83 static int MSVCRT_fdend = 3; /* highest allocated fd */
85 static MSVCRT_FILE* MSVCRT_fstreams[2048];
86 static int MSVCRT_stream_idx;
88 /* INTERNAL: process umask */
89 static int MSVCRT_umask = 0;
91 /* INTERNAL: Static buffer for temp file name */
92 static char MSVCRT_tmpname[MAX_PATH];
94 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
95 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
96 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
97 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
99 #define TOUL(x) (ULONGLONG)(x)
100 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
101 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
102 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
103 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
105 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
106 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
107 * and MSVCRT_stream_idx, from race conditions.
108 * It doesn't protect against race conditions manipulating the underlying files
109 * or flags; doing so would probably be better accomplished with per-file
110 * protection, rather than locking the whole table for every change.
112 static CRITICAL_SECTION MSVCRT_file_cs;
113 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
114 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
116 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
118 buf->st_dev = buf64->st_dev;
119 buf->st_ino = buf64->st_ino;
120 buf->st_mode = buf64->st_mode;
121 buf->st_nlink = buf64->st_nlink;
122 buf->st_uid = buf64->st_uid;
123 buf->st_gid = buf64->st_gid;
124 buf->st_rdev = buf64->st_rdev;
125 buf->st_size = buf64->st_size;
126 buf->st_atime = buf64->st_atime;
127 buf->st_mtime = buf64->st_mtime;
128 buf->st_ctime = buf64->st_ctime;
131 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
133 buf->st_dev = buf64->st_dev;
134 buf->st_ino = buf64->st_ino;
135 buf->st_mode = buf64->st_mode;
136 buf->st_nlink = buf64->st_nlink;
137 buf->st_uid = buf64->st_uid;
138 buf->st_gid = buf64->st_gid;
139 buf->st_rdev = buf64->st_rdev;
140 buf->st_size = buf64->st_size;
141 buf->st_atime = buf64->st_atime;
142 buf->st_mtime = buf64->st_mtime;
143 buf->st_ctime = buf64->st_ctime;
146 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
148 /* 1601 to 1970 is 369 years plus 89 leap days */
149 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
151 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
152 ft->dwHighDateTime = ticks >> 32;
153 ft->dwLowDateTime = ticks;
156 static inline BOOL msvcrt_is_valid_fd(int fd)
158 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
161 /* INTERNAL: Get the HANDLE for a fd
162 * This doesn't lock the table, because a failure will result in
163 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
164 * it returns a valid handle which is about to be closed, a subsequent call
165 * will fail, most likely in a sane way.
167 static HANDLE msvcrt_fdtoh(int fd)
169 if (!msvcrt_is_valid_fd(fd))
171 WARN(":fd (%d) - no handle!\n",fd);
172 *MSVCRT___doserrno() = 0;
173 *MSVCRT__errno() = MSVCRT_EBADF;
174 return INVALID_HANDLE_VALUE;
176 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
177 return MSVCRT_fdesc[fd].handle;
180 /* INTERNAL: free a file entry fd */
181 static void msvcrt_free_fd(int fd)
186 old_handle = MSVCRT_fdesc[fd].handle;
187 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
188 MSVCRT_fdesc[fd].wxflag = 0;
189 TRACE(":fd (%d) freed\n",fd);
190 if (fd < 3) /* don't use 0,1,2 for user files */
195 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
198 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
201 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
207 if (fd == MSVCRT_fdend - 1)
209 if (fd < MSVCRT_fdstart)
215 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
216 /* caller must hold the files lock */
217 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
219 if (fd >= MSVCRT_MAX_FILES)
221 WARN(":files exhausted!\n");
222 *MSVCRT__errno() = MSVCRT_ENFILE;
225 MSVCRT_fdesc[fd].handle = hand;
226 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
228 /* locate next free slot */
229 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
230 MSVCRT_fdstart = MSVCRT_fdend + 1;
232 while (MSVCRT_fdstart < MSVCRT_fdend &&
233 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
235 /* update last fd in use */
236 if (fd >= MSVCRT_fdend)
237 MSVCRT_fdend = fd + 1;
238 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
242 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
243 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
244 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
250 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
251 static int msvcrt_alloc_fd(HANDLE hand, int flag)
256 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
257 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
262 /* INTERNAL: Allocate a FILE* for an fd slot */
263 /* caller must hold the files lock */
264 static MSVCRT_FILE* msvcrt_alloc_fp(void)
268 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
270 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
272 if (!MSVCRT_fstreams[i])
274 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
276 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
278 return MSVCRT_fstreams[i];
284 /* INTERNAL: initialize a FILE* from an open fd */
285 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
287 TRACE(":fd (%d) allocating FILE*\n",fd);
288 if (!msvcrt_is_valid_fd(fd))
290 WARN(":invalid fd %d\n",fd);
291 *MSVCRT___doserrno() = 0;
292 *MSVCRT__errno() = MSVCRT_EBADF;
295 memset(file, 0, sizeof(*file));
297 file->_flag = stream_flags;
299 TRACE(":got FILE* (%p)\n",file);
303 /* INTERNAL: Create an inheritance data block (for spawned process)
304 * The inheritance block is made of:
305 * 00 int nb of file descriptor (NBFD)
306 * 04 char file flags (wxflag): repeated for each fd
307 * 4+NBFD HANDLE file handle: repeated for each fd
309 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
315 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
316 *block = MSVCRT_calloc(*size, 1);
322 wxflag_ptr = (char*)*block + sizeof(unsigned);
323 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
325 *(unsigned*)*block = MSVCRT_fdend;
326 for (fd = 0; fd < MSVCRT_fdend; fd++)
328 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
329 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
331 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
332 *handle_ptr = MSVCRT_fdesc[fd].handle;
337 *handle_ptr = INVALID_HANDLE_VALUE;
339 wxflag_ptr++; handle_ptr++;
344 /* INTERNAL: Set up all file descriptors,
345 * as well as default streams (stdin, stderr and stdout)
347 void msvcrt_init_io(void)
352 InitializeCriticalSection(&MSVCRT_file_cs);
353 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
354 GetStartupInfoA(&si);
355 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
361 count = *(unsigned*)si.lpReserved2;
362 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
363 handle_ptr = (HANDLE*)(wxflag_ptr + count);
365 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
366 count = min(count, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
367 for (i = 0; i < count; i++)
369 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
371 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
372 MSVCRT_fdesc[i].handle = *handle_ptr;
376 MSVCRT_fdesc[i].wxflag = 0;
377 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
379 wxflag_ptr++; handle_ptr++;
381 MSVCRT_fdend = max( 3, count );
382 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
383 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
386 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
388 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
389 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
390 GetCurrentProcess(), &MSVCRT_fdesc[0].handle,
391 0, TRUE, DUPLICATE_SAME_ACCESS))
392 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
394 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
396 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
397 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
398 GetCurrentProcess(), &MSVCRT_fdesc[1].handle,
399 0, TRUE, DUPLICATE_SAME_ACCESS))
400 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
402 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
404 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
405 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
406 GetCurrentProcess(), &MSVCRT_fdesc[2].handle,
407 0, TRUE, DUPLICATE_SAME_ACCESS))
408 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
411 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
412 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
414 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
415 for (i = 0; i < 3; i++)
417 /* FILE structs for stdin/out/err are static and never deleted */
418 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
419 MSVCRT__iob[i]._file = i;
420 MSVCRT__iob[i]._tmpfname = NULL;
421 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
423 MSVCRT_stream_idx = 3;
426 /* INTERNAL: Flush stdio file buffer */
427 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
430 int cnt=file->_ptr-file->_base;
431 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
432 file->_flag |= MSVCRT__IOERR;
435 file->_ptr=file->_base;
436 file->_cnt=file->_bufsiz;
441 /* INTERNAL: Allocate stdio file buffer */
442 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
444 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
446 file->_bufsiz = MSVCRT_BUFSIZ;
447 file->_flag |= MSVCRT__IOMYBUF;
449 file->_base = (char*)(&file->_charbuf);
451 file->_bufsiz = sizeof(file->_charbuf);
453 file->_ptr = file->_base;
457 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
458 static void msvcrt_int_to_base32(int num, char *str)
473 *p = (num & 31) + '0';
475 *p += ('a' - '0' - 10);
480 /*********************************************************************
481 * __iob_func(MSVCRT.@)
483 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
485 return &MSVCRT__iob[0];
488 /*********************************************************************
491 int CDECL MSVCRT__access(const char *filename, int mode)
493 DWORD attr = GetFileAttributesA(filename);
495 TRACE("(%s,%d) %d\n",filename,mode,attr);
497 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
499 msvcrt_set_errno(GetLastError());
502 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
504 msvcrt_set_errno(ERROR_ACCESS_DENIED);
510 /*********************************************************************
511 * _access_s (MSVCRT.@)
513 int CDECL _access_s(const char *filename, int mode)
515 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
516 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
518 *MSVCRT__errno() = MSVCRT_EINVAL;
522 return MSVCRT__access(filename, mode);
525 /*********************************************************************
526 * _waccess (MSVCRT.@)
528 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
530 DWORD attr = GetFileAttributesW(filename);
532 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
534 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
536 msvcrt_set_errno(GetLastError());
539 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
541 msvcrt_set_errno(ERROR_ACCESS_DENIED);
547 /*********************************************************************
548 * _waccess_s (MSVCRT.@)
550 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
552 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
553 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
555 *MSVCRT__errno() = MSVCRT_EINVAL;
559 return _waccess(filename, mode);
562 /*********************************************************************
565 int CDECL MSVCRT__chmod(const char *path, int flags)
567 DWORD oldFlags = GetFileAttributesA(path);
569 if (oldFlags != INVALID_FILE_ATTRIBUTES)
571 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
572 oldFlags | FILE_ATTRIBUTE_READONLY;
574 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
577 msvcrt_set_errno(GetLastError());
581 /*********************************************************************
584 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
586 DWORD oldFlags = GetFileAttributesW(path);
588 if (oldFlags != INVALID_FILE_ATTRIBUTES)
590 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
591 oldFlags | FILE_ATTRIBUTE_READONLY;
593 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
596 msvcrt_set_errno(GetLastError());
600 /*********************************************************************
603 int CDECL MSVCRT__unlink(const char *path)
605 TRACE("%s\n",debugstr_a(path));
606 if(DeleteFileA(path))
608 TRACE("failed (%d)\n",GetLastError());
609 msvcrt_set_errno(GetLastError());
613 /*********************************************************************
614 * _wunlink (MSVCRT.@)
616 int CDECL _wunlink(const MSVCRT_wchar_t *path)
618 TRACE("(%s)\n",debugstr_w(path));
619 if(DeleteFileW(path))
621 TRACE("failed (%d)\n",GetLastError());
622 msvcrt_set_errno(GetLastError());
626 /* _flushall calls MSVCRT_fflush which calls _flushall */
627 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
629 /*********************************************************************
630 * _flushall (MSVCRT.@)
632 int CDECL _flushall(void)
634 int i, num_flushed = 0;
637 for (i = 3; i < MSVCRT_stream_idx; i++)
638 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
641 /* FIXME: flush, do not commit */
642 if (_commit(i) == -1)
643 if (MSVCRT_fstreams[i])
644 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
646 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
647 MSVCRT_fflush(MSVCRT_fstreams[i]);
653 TRACE(":flushed (%d) handles\n",num_flushed);
657 /*********************************************************************
660 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
664 } else if(file->_flag & MSVCRT__IOWRT) {
665 int res=msvcrt_flush_buffer(file);
671 /*********************************************************************
674 int CDECL MSVCRT__close(int fd)
680 hand = msvcrt_fdtoh(fd);
681 TRACE(":fd (%d) handle (%p)\n",fd,hand);
682 if (hand == INVALID_HANDLE_VALUE)
684 else if (!CloseHandle(hand))
686 WARN(":failed-last error (%d)\n",GetLastError());
687 msvcrt_set_errno(GetLastError());
700 /*********************************************************************
703 int CDECL _commit(int fd)
705 HANDLE hand = msvcrt_fdtoh(fd);
707 TRACE(":fd (%d) handle (%p)\n",fd,hand);
708 if (hand == INVALID_HANDLE_VALUE)
711 if (!FlushFileBuffers(hand))
713 if (GetLastError() == ERROR_INVALID_HANDLE)
715 /* FlushFileBuffers fails for console handles
716 * so we ignore this error.
720 TRACE(":failed-last error (%d)\n",GetLastError());
721 msvcrt_set_errno(GetLastError());
728 /*********************************************************************
731 * MSDN isn't clear on this point, but the remarks for _pipe
732 * indicate file descriptors duplicated with _dup and _dup2 are always
735 int CDECL MSVCRT__dup2(int od, int nd)
739 TRACE("(od=%d, nd=%d)\n", od, nd);
741 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
745 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
746 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
748 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
750 if (msvcrt_is_valid_fd(nd))
752 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
756 *MSVCRT__errno() = MSVCRT_EMFILE;
760 /* _dup2 returns 0, not nd, on success */
767 msvcrt_set_errno(GetLastError());
772 *MSVCRT__errno() = MSVCRT_EBADF;
779 /*********************************************************************
782 int CDECL MSVCRT__dup(int od)
788 if (MSVCRT__dup2(od, fd) == 0)
796 /*********************************************************************
799 int CDECL _eof(int fd)
802 LONG hcurpos,hendpos;
803 HANDLE hand = msvcrt_fdtoh(fd);
805 TRACE(":fd (%d) handle (%p)\n",fd,hand);
807 if (hand == INVALID_HANDLE_VALUE)
810 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
812 /* Otherwise we do it the hard way */
813 hcurpos = hendpos = 0;
814 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
815 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
817 if (curpos == endpos && hcurpos == hendpos)
819 /* FIXME: shouldn't WX_ATEOF be set here? */
823 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
827 /*********************************************************************
828 * _fcloseall (MSVCRT.@)
830 int CDECL MSVCRT__fcloseall(void)
832 int num_closed = 0, i;
835 for (i = 3; i < MSVCRT_stream_idx; i++)
836 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
837 !MSVCRT_fclose(MSVCRT_fstreams[i]))
841 TRACE(":closed (%d) handles\n",num_closed);
845 /* free everything on process exit */
846 void msvcrt_free_io(void)
849 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
850 * stdout, and stderr (unlike GNU), so we need to fclose() them here
851 * or they won't get flushed.
853 MSVCRT_fclose(&MSVCRT__iob[0]);
854 MSVCRT_fclose(&MSVCRT__iob[1]);
855 MSVCRT_fclose(&MSVCRT__iob[2]);
856 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
857 DeleteCriticalSection(&MSVCRT_file_cs);
860 /*********************************************************************
861 * _lseeki64 (MSVCRT.@)
863 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
865 HANDLE hand = msvcrt_fdtoh(fd);
866 LARGE_INTEGER ofs, ret;
868 TRACE(":fd (%d) handle (%p)\n",fd,hand);
869 if (hand == INVALID_HANDLE_VALUE)
872 if (whence < 0 || whence > 2)
874 *MSVCRT__errno() = MSVCRT_EINVAL;
878 TRACE(":fd (%d) to %s pos %s\n",
879 fd,wine_dbgstr_longlong(offset),
880 (whence==SEEK_SET)?"SEEK_SET":
881 (whence==SEEK_CUR)?"SEEK_CUR":
882 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
884 ofs.QuadPart = offset;
885 if (SetFilePointerEx(hand, ofs, &ret, whence))
887 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
888 /* FIXME: What if we seek _to_ EOF - is EOF set? */
892 TRACE(":error-last error (%d)\n",GetLastError());
893 msvcrt_set_errno(GetLastError());
897 /*********************************************************************
900 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
902 return MSVCRT__lseeki64(fd, offset, whence);
905 /*********************************************************************
906 * _lock_file (MSVCRT.@)
908 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
910 FIXME("(%p) stub\n",file);
913 /*********************************************************************
914 * _unlock_file (MSVCRT.@)
916 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
918 FIXME("(%p) stub\n",file);
921 /*********************************************************************
922 * _locking (MSVCRT.@)
924 * This is untested; the underlying LockFile doesn't work yet.
926 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
930 HANDLE hand = msvcrt_fdtoh(fd);
932 TRACE(":fd (%d) handle (%p)\n",fd,hand);
933 if (hand == INVALID_HANDLE_VALUE)
936 if (mode < 0 || mode > 4)
938 *MSVCRT__errno() = MSVCRT_EINVAL;
942 TRACE(":fd (%d) by 0x%08x mode %s\n",
943 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
944 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
945 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
946 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
947 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
950 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
952 FIXME ("Seek failed\n");
953 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
956 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
959 ret = 1; /* just to satisfy gcc */
962 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
967 else if (mode == MSVCRT__LK_UNLCK)
968 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
970 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
971 /* FIXME - what about error settings? */
975 /*********************************************************************
976 * _fseeki64 (MSVCRT.@)
978 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
980 /* Flush output if needed */
981 if(file->_flag & MSVCRT__IOWRT)
982 msvcrt_flush_buffer(file);
984 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
985 offset -= file->_cnt;
986 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
987 /* Black magic correction for CR removal */
989 for (i=0; i<file->_cnt; i++) {
990 if (file->_ptr[i] == '\n')
993 /* Black magic when reading CR at buffer boundary*/
994 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
998 /* Discard buffered input */
1000 file->_ptr = file->_base;
1001 /* Reset direction of i/o */
1002 if(file->_flag & MSVCRT__IORW) {
1003 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1005 /* Clear end of file flag */
1006 file->_flag &= ~MSVCRT__IOEOF;
1007 return (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1010 /*********************************************************************
1013 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1015 return MSVCRT__fseeki64( file, offset, whence );
1018 /*********************************************************************
1019 * _chsize (MSVCRT.@)
1021 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1027 TRACE("(fd=%d, size=%d)\n", fd, size);
1031 handle = msvcrt_fdtoh(fd);
1032 if (handle != INVALID_HANDLE_VALUE)
1034 /* save the current file pointer */
1035 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1038 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1041 ret = SetEndOfFile(handle);
1042 if (!ret) msvcrt_set_errno(GetLastError());
1045 /* restore the file pointer */
1046 MSVCRT__lseek(fd, cur, SEEK_SET);
1051 return ret ? 0 : -1;
1054 /*********************************************************************
1055 * clearerr (MSVCRT.@)
1057 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1059 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1060 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1063 /*********************************************************************
1066 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1068 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1069 MSVCRT_fseek(file, 0L, SEEK_SET);
1070 MSVCRT_clearerr(file);
1073 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1075 int plus = strchrW(mode, '+') != NULL;
1080 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1081 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1084 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1085 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1088 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1089 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1092 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1093 *MSVCRT__errno() = MSVCRT_EINVAL;
1101 *open_flags |= MSVCRT__O_BINARY;
1102 *open_flags &= ~MSVCRT__O_TEXT;
1105 *open_flags |= MSVCRT__O_TEXT;
1106 *open_flags &= ~MSVCRT__O_BINARY;
1112 FIXME(":unknown flag %c not supported\n",mode[-1]);
1117 /*********************************************************************
1118 * _fdopen (MSVCRT.@)
1120 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1123 MSVCRT_wchar_t *modeW = NULL;
1125 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1127 ret = MSVCRT__wfdopen(fd, modeW);
1133 /*********************************************************************
1134 * _wfdopen (MSVCRT.@)
1136 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1138 int open_flags, stream_flags;
1141 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1144 if (!(file = msvcrt_alloc_fp()))
1146 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1151 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1157 /*********************************************************************
1158 * _filelength (MSVCRT.@)
1160 LONG CDECL MSVCRT__filelength(int fd)
1162 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1165 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1168 if (endPos != curPos)
1169 MSVCRT__lseek(fd, curPos, SEEK_SET);
1176 /*********************************************************************
1177 * _filelengthi64 (MSVCRT.@)
1179 __int64 CDECL MSVCRT__filelengthi64(int fd)
1181 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1184 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1187 if (endPos != curPos)
1188 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1195 /*********************************************************************
1196 * _fileno (MSVCRT.@)
1198 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1200 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1204 /*********************************************************************
1205 * _fstat64 (MSVCRT.@)
1207 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1211 BY_HANDLE_FILE_INFORMATION hfi;
1212 HANDLE hand = msvcrt_fdtoh(fd);
1214 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1215 if (hand == INVALID_HANDLE_VALUE)
1220 WARN(":failed-NULL buf\n");
1221 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1225 memset(&hfi, 0, sizeof(hfi));
1226 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1227 type = GetFileType(hand);
1228 if (type == FILE_TYPE_PIPE)
1230 buf->st_dev = buf->st_rdev = fd;
1231 buf->st_mode = S_IFIFO;
1234 else if (type == FILE_TYPE_CHAR)
1236 buf->st_dev = buf->st_rdev = fd;
1237 buf->st_mode = S_IFCHR;
1240 else /* FILE_TYPE_DISK etc. */
1242 if (!GetFileInformationByHandle(hand, &hfi))
1244 WARN(":failed-last error (%d)\n",GetLastError());
1245 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1248 buf->st_mode = S_IFREG | 0444;
1249 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1250 buf->st_mode |= 0222;
1251 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1252 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1254 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1255 buf->st_mtime = buf->st_ctime = dw;
1256 buf->st_nlink = hfi.nNumberOfLinks;
1258 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1263 /*********************************************************************
1264 * _fstati64 (MSVCRT.@)
1266 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1269 struct MSVCRT__stat64 buf64;
1271 ret = MSVCRT__fstat64(fd, &buf64);
1273 msvcrt_stat64_to_stati64(&buf64, buf);
1277 /*********************************************************************
1280 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1282 struct MSVCRT__stat64 buf64;
1284 ret = MSVCRT__fstat64(fd, &buf64);
1286 msvcrt_stat64_to_stat(&buf64, buf);
1290 /*********************************************************************
1291 * _futime64 (MSVCRT.@)
1293 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1295 HANDLE hand = msvcrt_fdtoh(fd);
1300 time_to_filetime( MSVCRT__time64(NULL), &at );
1305 time_to_filetime( t->actime, &at );
1306 time_to_filetime( t->modtime, &wt );
1309 if (!SetFileTime(hand, NULL, &at, &wt))
1311 msvcrt_set_errno(GetLastError());
1317 /*********************************************************************
1318 * _futime32 (MSVCRT.@)
1320 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1322 struct MSVCRT___utimbuf64 t64;
1323 t64.actime = t->actime;
1324 t64.modtime = t->modtime;
1325 return _futime64( fd, &t64 );
1328 /*********************************************************************
1329 * _futime (MSVCRT.@)
1332 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1334 return _futime64( fd, t );
1337 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1339 return _futime32( fd, t );
1343 /*********************************************************************
1344 * _get_osfhandle (MSVCRT.@)
1346 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1348 HANDLE hand = msvcrt_fdtoh(fd);
1349 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1351 return (MSVCRT_intptr_t)hand;
1354 /*********************************************************************
1355 * _isatty (MSVCRT.@)
1357 int CDECL _isatty(int fd)
1359 HANDLE hand = msvcrt_fdtoh(fd);
1361 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1362 if (hand == INVALID_HANDLE_VALUE)
1365 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1368 /*********************************************************************
1369 * _mktemp (MSVCRT.@)
1371 char * CDECL _mktemp(char *pattern)
1374 char *retVal = pattern;
1379 numX = (*pattern++ == 'X')? numX + 1 : 0;
1383 id = GetCurrentProcessId();
1387 int tempNum = id / 10;
1388 *pattern-- = id - (tempNum * 10) + '0';
1394 *pattern = letter++;
1395 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1396 GetLastError() == ERROR_FILE_NOT_FOUND)
1398 } while(letter <= 'z');
1402 /*********************************************************************
1403 * _wmktemp (MSVCRT.@)
1405 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1408 MSVCRT_wchar_t *retVal = pattern;
1410 MSVCRT_wchar_t letter = 'a';
1413 numX = (*pattern++ == 'X')? numX + 1 : 0;
1417 id = GetCurrentProcessId();
1421 int tempNum = id / 10;
1422 *pattern-- = id - (tempNum * 10) + '0';
1428 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1429 GetLastError() == ERROR_FILE_NOT_FOUND)
1431 *pattern = letter++;
1432 } while(letter != '|');
1436 static unsigned split_oflags(unsigned oflags)
1439 unsigned unsupp; /* until we support everything */
1441 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1442 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1443 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1444 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1445 else wxflags |= WX_TEXT; /* default to TEXT*/
1446 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1448 if ((unsupp = oflags & ~(
1449 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1450 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1451 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1452 MSVCRT__O_NOINHERIT|
1453 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1455 ERR(":unsupported oflags 0x%04x\n",unsupp);
1460 /*********************************************************************
1463 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1466 SECURITY_ATTRIBUTES sa;
1467 HANDLE readHandle, writeHandle;
1471 *MSVCRT__errno() = MSVCRT_EINVAL;
1475 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1476 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1477 sa.lpSecurityDescriptor = NULL;
1478 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1480 unsigned int wxflags = split_oflags(textmode);
1484 fd = msvcrt_alloc_fd(readHandle, wxflags);
1488 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1496 MSVCRT__close(pfds[0]);
1497 CloseHandle(writeHandle);
1498 *MSVCRT__errno() = MSVCRT_EMFILE;
1503 CloseHandle(readHandle);
1504 CloseHandle(writeHandle);
1505 *MSVCRT__errno() = MSVCRT_EMFILE;
1510 msvcrt_set_errno(GetLastError());
1515 /*********************************************************************
1518 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1522 DWORD access = 0, creation = 0, attrib;
1526 SECURITY_ATTRIBUTES sa;
1529 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1530 path, oflags, shflags);
1532 wxflag = split_oflags(oflags);
1533 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1535 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1536 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1537 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1540 if (oflags & MSVCRT__O_CREAT)
1542 __ms_va_start(ap, shflags);
1543 pmode = va_arg(ap, int);
1546 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1547 FIXME(": pmode 0x%04x ignored\n", pmode);
1549 WARN(": pmode 0x%04x ignored\n", pmode);
1551 if (oflags & MSVCRT__O_EXCL)
1552 creation = CREATE_NEW;
1553 else if (oflags & MSVCRT__O_TRUNC)
1554 creation = CREATE_ALWAYS;
1556 creation = OPEN_ALWAYS;
1558 else /* no MSVCRT__O_CREAT */
1560 if (oflags & MSVCRT__O_TRUNC)
1561 creation = TRUNCATE_EXISTING;
1563 creation = OPEN_EXISTING;
1568 case MSVCRT__SH_DENYRW:
1571 case MSVCRT__SH_DENYWR:
1572 sharing = FILE_SHARE_READ;
1574 case MSVCRT__SH_DENYRD:
1575 sharing = FILE_SHARE_WRITE;
1577 case MSVCRT__SH_DENYNO:
1578 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1581 ERR( "Unhandled shflags 0x%x\n", shflags );
1584 attrib = FILE_ATTRIBUTE_NORMAL;
1586 if (oflags & MSVCRT__O_TEMPORARY)
1588 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1590 sharing |= FILE_SHARE_DELETE;
1593 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1594 sa.lpSecurityDescriptor = NULL;
1595 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1597 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1599 if (hand == INVALID_HANDLE_VALUE) {
1600 WARN(":failed-last error (%d)\n",GetLastError());
1601 msvcrt_set_errno(GetLastError());
1605 fd = msvcrt_alloc_fd(hand, wxflag);
1607 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1611 /*********************************************************************
1612 * _wsopen (MSVCRT.@)
1614 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1618 DWORD access = 0, creation = 0, attrib;
1622 SECURITY_ATTRIBUTES sa;
1625 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1626 debugstr_w(path), oflags, shflags);
1628 wxflag = split_oflags(oflags);
1629 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1631 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1632 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1633 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1636 if (oflags & MSVCRT__O_CREAT)
1638 __ms_va_start(ap, shflags);
1639 pmode = va_arg(ap, int);
1642 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1643 FIXME(": pmode 0x%04x ignored\n", pmode);
1645 WARN(": pmode 0x%04x ignored\n", pmode);
1647 if (oflags & MSVCRT__O_EXCL)
1648 creation = CREATE_NEW;
1649 else if (oflags & MSVCRT__O_TRUNC)
1650 creation = CREATE_ALWAYS;
1652 creation = OPEN_ALWAYS;
1654 else /* no MSVCRT__O_CREAT */
1656 if (oflags & MSVCRT__O_TRUNC)
1657 creation = TRUNCATE_EXISTING;
1659 creation = OPEN_EXISTING;
1664 case MSVCRT__SH_DENYRW:
1667 case MSVCRT__SH_DENYWR:
1668 sharing = FILE_SHARE_READ;
1670 case MSVCRT__SH_DENYRD:
1671 sharing = FILE_SHARE_WRITE;
1673 case MSVCRT__SH_DENYNO:
1674 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1677 ERR( "Unhandled shflags 0x%x\n", shflags );
1680 attrib = FILE_ATTRIBUTE_NORMAL;
1682 if (oflags & MSVCRT__O_TEMPORARY)
1684 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1686 sharing |= FILE_SHARE_DELETE;
1689 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1690 sa.lpSecurityDescriptor = NULL;
1691 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1693 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1695 if (hand == INVALID_HANDLE_VALUE) {
1696 WARN(":failed-last error (%d)\n",GetLastError());
1697 msvcrt_set_errno(GetLastError());
1701 fd = msvcrt_alloc_fd(hand, wxflag);
1703 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1707 /*********************************************************************
1710 int CDECL MSVCRT__open( const char *path, int flags, ... )
1714 if (flags & MSVCRT__O_CREAT)
1717 __ms_va_start(ap, flags);
1718 pmode = va_arg(ap, int);
1720 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1723 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1726 /*********************************************************************
1729 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1733 if (flags & MSVCRT__O_CREAT)
1736 __ms_va_start(ap, flags);
1737 pmode = va_arg(ap, int);
1739 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1742 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1745 /*********************************************************************
1748 int CDECL MSVCRT__creat(const char *path, int flags)
1750 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1751 return MSVCRT__open(path, usedFlags);
1754 /*********************************************************************
1755 * _wcreat (MSVCRT.@)
1757 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1759 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1760 return _wopen(path, usedFlags);
1763 /*********************************************************************
1764 * _open_osfhandle (MSVCRT.@)
1766 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1770 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1771 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1772 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1773 * text - it never sets MSVCRT__O_BINARY.
1775 /* don't let split_oflags() decide the mode if no mode is passed */
1776 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1777 oflags |= MSVCRT__O_BINARY;
1779 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1780 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1784 /*********************************************************************
1787 int CDECL _rmtmp(void)
1789 int num_removed = 0, i;
1792 for (i = 3; i < MSVCRT_stream_idx; i++)
1793 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1795 MSVCRT_fclose(MSVCRT_fstreams[i]);
1801 TRACE(":removed (%d) temp files\n",num_removed);
1805 /*********************************************************************
1808 * When reading \r as last character in text mode, read() positions
1809 * the file pointer on the \r character while getc() goes on to
1812 static int read_i(int fd, void *buf, unsigned int count)
1815 char *bufstart = buf;
1816 HANDLE hand = msvcrt_fdtoh(fd);
1821 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1822 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1823 TRACE("already at EOF, returning 0\n");
1826 /* Don't trace small reads, it gets *very* annoying */
1828 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1829 if (hand == INVALID_HANDLE_VALUE)
1832 /* Reading single bytes in O_TEXT mode makes things slow
1833 * So read big chunks
1835 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1837 if (count != 0 && num_read == 0)
1839 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1840 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1842 else if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1845 if (bufstart[num_read-1] == '\r')
1849 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1850 ReadFile(hand, bufstart, 1, &num_read, NULL);
1854 MSVCRT_fdesc[fd].wxflag |= WX_READCR;
1859 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1860 for (i=0, j=0; i<num_read; i++)
1862 /* in text mode, a ctrl-z signals EOF */
1863 if (bufstart[i] == 0x1a)
1865 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1866 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1869 /* in text mode, strip \r if followed by \n.
1870 * BUG: should save state across calls somehow, so CR LF that
1871 * straddles buffer boundary gets recognized properly?
1873 if ((bufstart[i] != '\r')
1874 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1875 bufstart[j++] = bufstart[i];
1882 if (GetLastError() == ERROR_BROKEN_PIPE)
1884 TRACE(":end-of-pipe\n");
1885 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1890 TRACE(":failed-last error (%d)\n",GetLastError());
1896 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1900 /*********************************************************************
1903 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1906 num_read = read_i(fd, buf, count);
1910 /*********************************************************************
1911 * _setmode (MSVCRT.@)
1913 int CDECL _setmode(int fd,int mode)
1915 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1916 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1917 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1918 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1919 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1921 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1925 /*********************************************************************
1926 * _stat64 (MSVCRT.@)
1928 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
1931 WIN32_FILE_ATTRIBUTE_DATA hfi;
1932 unsigned short mode = ALL_S_IREAD;
1935 TRACE(":file (%s) buf(%p)\n",path,buf);
1937 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1939 TRACE("failed (%d)\n",GetLastError());
1940 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1944 memset(buf,0,sizeof(struct MSVCRT__stat64));
1946 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1947 Bon 011120: This FIXME seems incorrect
1948 Also a letter as first char isn't enough to be classified
1951 if (isalpha(*path)&& (*(path+1)==':'))
1952 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1954 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1956 plen = strlen(path);
1958 /* Dir, or regular file? */
1959 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1960 (path[plen-1] == '\\'))
1961 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1964 mode |= MSVCRT__S_IFREG;
1966 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1968 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1969 (tolower(path[plen-3]) << 16);
1970 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1971 mode |= ALL_S_IEXEC;
1975 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1976 mode |= ALL_S_IWRITE;
1978 buf->st_mode = mode;
1980 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1981 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1983 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1984 buf->st_mtime = buf->st_ctime = dw;
1985 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
1986 (int)(buf->st_size >> 32),(int)buf->st_size,
1987 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
1991 /*********************************************************************
1992 * _stati64 (MSVCRT.@)
1994 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
1997 struct MSVCRT__stat64 buf64;
1999 ret = MSVCRT_stat64(path, &buf64);
2001 msvcrt_stat64_to_stati64(&buf64, buf);
2005 /*********************************************************************
2008 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2010 struct MSVCRT__stat64 buf64;
2012 ret = MSVCRT_stat64( path, &buf64);
2014 msvcrt_stat64_to_stat(&buf64, buf);
2018 /*********************************************************************
2019 * _wstat64 (MSVCRT.@)
2021 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2024 WIN32_FILE_ATTRIBUTE_DATA hfi;
2025 unsigned short mode = ALL_S_IREAD;
2028 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2030 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2032 TRACE("failed (%d)\n",GetLastError());
2033 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2037 memset(buf,0,sizeof(struct MSVCRT__stat64));
2039 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2040 if (MSVCRT_iswalpha(*path))
2041 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2043 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2045 plen = strlenW(path);
2047 /* Dir, or regular file? */
2048 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2049 (path[plen-1] == '\\'))
2050 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2053 mode |= MSVCRT__S_IFREG;
2055 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2057 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2058 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2059 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2060 mode |= ALL_S_IEXEC;
2064 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2065 mode |= ALL_S_IWRITE;
2067 buf->st_mode = mode;
2069 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2070 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2072 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2073 buf->st_mtime = buf->st_ctime = dw;
2074 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2075 (int)(buf->st_size >> 32),(int)buf->st_size,
2076 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2080 /*********************************************************************
2081 * _wstati64 (MSVCRT.@)
2083 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2086 struct MSVCRT__stat64 buf64;
2088 ret = MSVCRT__wstat64(path, &buf64);
2090 msvcrt_stat64_to_stati64(&buf64, buf);
2094 /*********************************************************************
2097 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2100 struct MSVCRT__stat64 buf64;
2102 ret = MSVCRT__wstat64( path, &buf64 );
2103 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2107 /*********************************************************************
2110 MSVCRT_long CDECL MSVCRT__tell(int fd)
2112 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2115 /*********************************************************************
2116 * _telli64 (MSVCRT.@)
2118 __int64 CDECL _telli64(int fd)
2120 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2123 /*********************************************************************
2124 * _tempnam (MSVCRT.@)
2126 char * CDECL _tempnam(const char *dir, const char *prefix)
2128 char tmpbuf[MAX_PATH];
2129 const char *tmp_dir = MSVCRT_getenv("TMP");
2131 if (tmp_dir) dir = tmp_dir;
2133 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2134 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2136 TRACE("got name (%s)\n",tmpbuf);
2137 DeleteFileA(tmpbuf);
2138 return _strdup(tmpbuf);
2140 TRACE("failed (%d)\n",GetLastError());
2144 /*********************************************************************
2145 * _wtempnam (MSVCRT.@)
2147 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2149 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2151 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2152 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2154 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2155 DeleteFileW(tmpbuf);
2156 return _wcsdup(tmpbuf);
2158 TRACE("failed (%d)\n",GetLastError());
2162 /*********************************************************************
2165 int CDECL MSVCRT__umask(int umask)
2167 int old_umask = MSVCRT_umask;
2168 TRACE("(%d)\n",umask);
2169 MSVCRT_umask = umask;
2173 /*********************************************************************
2174 * _utime64 (MSVCRT.@)
2176 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2178 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2182 int retVal = _futime64(fd, t);
2189 /*********************************************************************
2190 * _utime32 (MSVCRT.@)
2192 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2194 struct MSVCRT___utimbuf64 t64;
2195 t64.actime = t->actime;
2196 t64.modtime = t->modtime;
2197 return _utime64( path, &t64 );
2200 /*********************************************************************
2204 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2206 return _utime64( path, t );
2209 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2211 return _utime32( path, t );
2215 /*********************************************************************
2216 * _wutime64 (MSVCRT.@)
2218 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2220 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2224 int retVal = _futime64(fd, t);
2231 /*********************************************************************
2232 * _wutime32 (MSVCRT.@)
2234 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2236 struct MSVCRT___utimbuf64 t64;
2237 t64.actime = t->actime;
2238 t64.modtime = t->modtime;
2239 return _wutime64( path, &t64 );
2242 /*********************************************************************
2243 * _wutime (MSVCRT.@)
2246 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2248 return _wutime64( path, t );
2251 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2253 return _wutime32( path, t );
2257 /*********************************************************************
2260 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2263 HANDLE hand = msvcrt_fdtoh(fd);
2265 /* Don't trace small writes, it gets *very* annoying */
2268 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2270 if (hand == INVALID_HANDLE_VALUE)
2272 *MSVCRT__errno() = MSVCRT_EBADF;
2276 /* If appending, go to EOF */
2277 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2278 MSVCRT__lseek(fd, 0, FILE_END);
2280 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2282 if (WriteFile(hand, buf, count, &num_written, NULL)
2283 && (num_written == count))
2285 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2286 hand, GetLastError());
2287 *MSVCRT__errno() = MSVCRT_ENOSPC;
2291 unsigned int i, j, nr_lf;
2294 const char *s = buf, *buf_start = buf;
2295 /* find number of \n ( without preceding \r ) */
2296 for ( nr_lf=0,i = 0; i <count; i++)
2301 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2306 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2308 for (s = buf, i = 0, j = 0; i < count; i++)
2313 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2320 FIXME("Malloc failed\n");
2328 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2330 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2331 fd, hand, GetLastError(), num_written);
2332 *MSVCRT__errno() = MSVCRT_ENOSPC;
2335 return s - buf_start;
2347 /*********************************************************************
2350 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2353 len = MSVCRT__write(file->_file, &val, sizeof(val));
2354 if (len == sizeof(val)) return val;
2355 file->_flag |= MSVCRT__IOERR;
2359 /*********************************************************************
2362 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2367 MSVCRT_free(file->_tmpfname);
2368 file->_tmpfname = NULL;
2369 /* flush stdio buffers */
2370 if(file->_flag & MSVCRT__IOWRT)
2371 MSVCRT_fflush(file);
2372 if(file->_flag & MSVCRT__IOMYBUF)
2373 MSVCRT_free(file->_base);
2375 r=MSVCRT__close(file->_file);
2379 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2382 /*********************************************************************
2385 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2387 return file->_flag & MSVCRT__IOEOF;
2390 /*********************************************************************
2393 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2395 return file->_flag & MSVCRT__IOERR;
2398 /*********************************************************************
2399 * _filbuf (MSVCRT.@)
2401 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2403 /* Allocate buffer if needed */
2404 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2405 msvcrt_alloc_buffer(file);
2407 if(!(file->_flag & MSVCRT__IOREAD)) {
2408 if(file->_flag & MSVCRT__IORW) {
2409 file->_flag |= MSVCRT__IOREAD;
2414 if(file->_flag & MSVCRT__IONBF) {
2417 if ((r = read_i(file->_file,&c,1)) != 1) {
2418 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2423 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2425 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2430 file->_ptr = file->_base+1;
2431 return *(unsigned char *)file->_base;
2435 /*********************************************************************
2438 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2444 i = (unsigned char *)file->_ptr++;
2447 j = MSVCRT__filbuf(file);
2451 /*********************************************************************
2452 * _fgetchar (MSVCRT.@)
2454 int CDECL _fgetchar(void)
2456 return MSVCRT_fgetc(MSVCRT_stdin);
2459 /*********************************************************************
2462 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2464 int cc = MSVCRT_EOF;
2465 char * buf_start = s;
2467 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2468 file,file->_file,s,size);
2470 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2475 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2477 TRACE(":nothing read\n");
2480 if ((cc != MSVCRT_EOF) && (size > 1))
2483 TRACE(":got %s\n", debugstr_a(buf_start));
2487 /*********************************************************************
2490 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2491 * the CR from CR/LF combinations
2493 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2497 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2504 for(i=0; i<sizeof(wc); i++)
2514 j = MSVCRT__filbuf(file);
2517 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2527 c = MSVCRT_fgetc(file);
2528 if ((get_locale()->locinfo->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2530 FIXME("Treat Multibyte characters\n");
2532 if (c == MSVCRT_EOF)
2535 return (MSVCRT_wint_t)c;
2538 /*********************************************************************
2541 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2547 for (j=0; j<sizeof(int); j++) {
2548 k = MSVCRT_fgetc(file);
2549 if (k == MSVCRT_EOF) {
2550 file->_flag |= MSVCRT__IOEOF;
2558 /*********************************************************************
2561 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2563 return MSVCRT_fgetwc(file);
2566 /*********************************************************************
2567 * _fgetwchar (MSVCRT.@)
2569 MSVCRT_wint_t CDECL _fgetwchar(void)
2571 return MSVCRT_fgetwc(MSVCRT_stdin);
2574 /*********************************************************************
2575 * getwchar (MSVCRT.@)
2577 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2579 return _fgetwchar();
2582 /*********************************************************************
2585 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2587 int cc = MSVCRT_WEOF;
2588 MSVCRT_wchar_t * buf_start = s;
2590 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2591 file,file->_file,s,size);
2593 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2598 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2600 TRACE(":nothing read\n");
2603 if ((cc != MSVCRT_WEOF) && (size > 1))
2606 TRACE(":got %s\n", debugstr_w(buf_start));
2610 /*********************************************************************
2613 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2615 MSVCRT_size_t wrcnt=size * nmemb;
2620 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2621 memcpy(file->_ptr, ptr, pcnt);
2626 ptr = (const char*)ptr + pcnt;
2627 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2628 if(file->_flag & MSVCRT__IORW) {
2629 file->_flag |= MSVCRT__IOWRT;
2635 int res=msvcrt_flush_buffer(file);
2637 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2640 file->_flag |= MSVCRT__IOERR;
2643 written += pwritten;
2646 return written / size;
2649 /*********************************************************************
2652 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2654 MSVCRT_wchar_t mwc=wc;
2655 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2660 /*********************************************************************
2661 * _fputwchar (MSVCRT.@)
2663 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2665 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2668 /*********************************************************************
2669 * _wfsopen (MSVCRT.@)
2671 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2674 int open_flags, stream_flags, fd;
2676 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2678 /* map mode string to open() flags. "man fopen" for possibilities. */
2679 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2683 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2686 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2688 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2695 TRACE(":got (%p)\n",file);
2696 if (fd >= 0 && !file)
2702 /*********************************************************************
2703 * _fsopen (MSVCRT.@)
2705 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2708 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2710 if (path && !(pathW = msvcrt_wstrdupa(path))) {
2711 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2712 *MSVCRT__errno() = MSVCRT_EINVAL;
2715 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2718 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2719 *MSVCRT__errno() = MSVCRT_EINVAL;
2723 ret = MSVCRT__wfsopen(pathW, modeW, share);
2730 /*********************************************************************
2733 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2735 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2738 /*********************************************************************
2739 * fopen_s (MSVCRT.@)
2741 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
2742 const char *filename, const char *mode)
2744 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2745 MSVCRT_CHECK_PMT(mode != NULL)) {
2746 *MSVCRT__errno() = MSVCRT_EINVAL;
2747 return MSVCRT_EINVAL;
2750 *pFile = MSVCRT_fopen(filename, mode);
2753 return *MSVCRT__errno();
2757 /*********************************************************************
2758 * _wfopen (MSVCRT.@)
2760 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2762 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2765 /*********************************************************************
2766 * _wfopen_s (MSVCRT.@)
2768 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
2769 const MSVCRT_wchar_t *mode)
2771 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2772 MSVCRT_CHECK_PMT(mode != NULL)) {
2773 *MSVCRT__errno() = MSVCRT_EINVAL;
2774 return MSVCRT_EINVAL;
2777 *pFile = MSVCRT__wfopen(filename, mode);
2780 return *MSVCRT__errno();
2784 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2785 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2787 /*********************************************************************
2790 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2797 int res = msvcrt_flush_buffer(file);
2798 return res ? res : c;
2803 return MSVCRT__flsbuf(c, file);
2807 /*********************************************************************
2808 * _flsbuf (MSVCRT.@)
2810 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2812 /* Flush output buffer */
2813 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2814 msvcrt_alloc_buffer(file);
2816 if(!(file->_flag & MSVCRT__IOWRT)) {
2817 if(file->_flag & MSVCRT__IORW) {
2818 file->_flag |= MSVCRT__IOWRT;
2824 int res=msvcrt_flush_buffer(file);
2825 return res?res : MSVCRT_fputc(c, file);
2829 /* set _cnt to 0 for unbuffered FILEs */
2831 len = MSVCRT__write(file->_file, &cc, 1);
2832 if (len == 1) return c & 0xff;
2833 file->_flag |= MSVCRT__IOERR;
2838 /*********************************************************************
2839 * _fputchar (MSVCRT.@)
2841 int CDECL _fputchar(int c)
2843 return MSVCRT_fputc(c, MSVCRT_stdout);
2846 /*********************************************************************
2849 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2850 { MSVCRT_size_t rcnt=size * nmemb;
2851 MSVCRT_size_t read=0;
2857 /* first buffered data */
2859 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2860 memcpy(ptr, file->_ptr, pcnt);
2865 ptr = (char*)ptr + pcnt;
2866 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2867 if(file->_flag & MSVCRT__IORW) {
2868 file->_flag |= MSVCRT__IOREAD;
2875 /* Fill the buffer on small reads.
2876 * TODO: Use a better buffering strategy.
2878 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2879 if (file->_bufsiz == 0) {
2880 msvcrt_alloc_buffer(file);
2882 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2883 file->_ptr = file->_base;
2884 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2885 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2886 if (i > 0 && i < file->_cnt) {
2887 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2888 file->_flag &= ~MSVCRT__IOEOF;
2891 memcpy(ptr, file->_ptr, i);
2896 i = MSVCRT__read(file->_file,ptr, rcnt);
2900 ptr = (char *)ptr+i;
2901 /* expose feof condition in the flags
2902 * MFC tests file->_flag for feof, and doesn't call feof())
2904 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2905 file->_flag |= MSVCRT__IOEOF;
2908 file->_flag |= MSVCRT__IOERR;
2918 /*********************************************************************
2919 * _wfreopen (MSVCRT.@)
2922 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
2924 int open_flags, stream_flags, fd;
2926 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2929 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2933 MSVCRT_fclose(file);
2934 /* map mode string to open() flags. "man fopen" for possibilities. */
2935 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2939 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2942 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2945 WARN(":failed-last error (%d)\n",GetLastError());
2946 msvcrt_set_errno(GetLastError());
2955 /*********************************************************************
2956 * freopen (MSVCRT.@)
2959 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
2962 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2964 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2965 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2971 ret = MSVCRT__wfreopen(pathW, modeW, file);
2978 /*********************************************************************
2979 * fsetpos (MSVCRT.@)
2981 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2983 /* Note that all this has been lifted 'as is' from fseek */
2984 if(file->_flag & MSVCRT__IOWRT)
2985 msvcrt_flush_buffer(file);
2987 /* Discard buffered input */
2989 file->_ptr = file->_base;
2991 /* Reset direction of i/o */
2992 if(file->_flag & MSVCRT__IORW) {
2993 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2996 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2999 /*********************************************************************
3000 * _ftelli64 (MSVCRT.@)
3002 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3004 /* TODO: just call fgetpos and return lower half of result */
3007 pos = _telli64(file->_file);
3008 if(pos == -1) return -1;
3010 if( file->_flag & MSVCRT__IOWRT ) {
3011 off = file->_ptr - file->_base;
3014 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3015 /* Black magic correction for CR removal */
3017 for (i=0; i<file->_cnt; i++) {
3018 if (file->_ptr[i] == '\n')
3021 /* Black magic when reading CR at buffer boundary*/
3022 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3031 /*********************************************************************
3034 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3036 return MSVCRT__ftelli64(file);
3039 /*********************************************************************
3040 * fgetpos (MSVCRT.@)
3042 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3045 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3046 if(*pos == -1) return -1;
3048 if( file->_flag & MSVCRT__IOWRT ) {
3049 off = file->_ptr - file->_base;
3052 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3053 /* Black magic correction for CR removal */
3055 for (i=0; i<file->_cnt; i++) {
3056 if (file->_ptr[i] == '\n')
3059 /* Black magic when reading CR at buffer boundary*/
3060 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3069 /*********************************************************************
3072 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3074 MSVCRT_size_t i, len = strlen(s);
3075 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3076 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3077 for (i=0; i<len; i++)
3078 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
3083 /*********************************************************************
3086 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3088 MSVCRT_size_t i, len = strlenW(s);
3089 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3090 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3091 for (i=0; i<len; i++)
3093 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3095 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
3101 /*********************************************************************
3102 * getchar (MSVCRT.@)
3104 int CDECL MSVCRT_getchar(void)
3106 return MSVCRT_fgetc(MSVCRT_stdin);
3109 /*********************************************************************
3112 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3114 return MSVCRT_fgetc(file);
3117 /*********************************************************************
3120 char * CDECL MSVCRT_gets(char *buf)
3123 char * buf_start = buf;
3125 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3126 cc = MSVCRT_fgetc(MSVCRT_stdin))
3127 if(cc != '\r') *buf++ = (char)cc;
3131 TRACE("got '%s'\n", buf_start);
3135 /*********************************************************************
3138 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3141 MSVCRT_wchar_t* ws = buf;
3143 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3144 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3147 *buf++ = (MSVCRT_wchar_t)cc;
3151 TRACE("got %s\n", debugstr_w(ws));
3155 /*********************************************************************
3158 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3160 return MSVCRT_fputc(c, file);
3163 /*********************************************************************
3164 * putchar (MSVCRT.@)
3166 int CDECL MSVCRT_putchar(int c)
3168 return MSVCRT_fputc(c, MSVCRT_stdout);
3171 /*********************************************************************
3172 * _putwch (MSVCRT.@)
3174 int CDECL MSVCRT__putwch(int c)
3176 return MSVCRT_fputwc(c, MSVCRT_stdout);
3179 /*********************************************************************
3182 int CDECL MSVCRT_puts(const char *s)
3184 MSVCRT_size_t len = strlen(s);
3185 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3186 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3189 /*********************************************************************
3192 int CDECL _putws(const MSVCRT_wchar_t *s)
3194 static const MSVCRT_wchar_t nl = '\n';
3195 MSVCRT_size_t len = strlenW(s);
3196 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3197 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3200 /*********************************************************************
3203 int CDECL MSVCRT_remove(const char *path)
3205 TRACE("(%s)\n",path);
3206 if (DeleteFileA(path))
3208 TRACE(":failed (%d)\n",GetLastError());
3209 msvcrt_set_errno(GetLastError());
3213 /*********************************************************************
3214 * _wremove (MSVCRT.@)
3216 int CDECL _wremove(const MSVCRT_wchar_t *path)
3218 TRACE("(%s)\n",debugstr_w(path));
3219 if (DeleteFileW(path))
3221 TRACE(":failed (%d)\n",GetLastError());
3222 msvcrt_set_errno(GetLastError());
3226 /*********************************************************************
3229 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3231 TRACE(":from %s to %s\n",oldpath,newpath);
3232 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3234 TRACE(":failed (%d)\n",GetLastError());
3235 msvcrt_set_errno(GetLastError());
3239 /*********************************************************************
3240 * _wrename (MSVCRT.@)
3242 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3244 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3245 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3247 TRACE(":failed (%d)\n",GetLastError());
3248 msvcrt_set_errno(GetLastError());
3252 /*********************************************************************
3253 * setvbuf (MSVCRT.@)
3255 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3257 /* TODO: Check if file busy */
3259 MSVCRT_free(file->_base);
3263 if(mode == MSVCRT__IOFBF) {
3264 file->_flag &= ~MSVCRT__IONBF;
3265 file->_base = file->_ptr = buf;
3267 file->_bufsiz = size;
3270 file->_flag |= MSVCRT__IONBF;
3275 /*********************************************************************
3278 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3280 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3283 /*********************************************************************
3286 char * CDECL MSVCRT_tmpnam(char *s)
3294 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3295 p = s + sprintf(s, "\\s%s.", tmpstr);
3296 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3298 msvcrt_int_to_base32(unique++, tmpstr);
3300 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3301 GetLastError() == ERROR_FILE_NOT_FOUND)
3307 /*********************************************************************
3308 * tmpfile (MSVCRT.@)
3310 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3312 char *filename = MSVCRT_tmpnam(NULL);
3314 MSVCRT_FILE* file = NULL;
3317 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3318 if (fd != -1 && (file = msvcrt_alloc_fp()))
3320 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3325 else file->_tmpfname = _strdup(filename);
3331 /*********************************************************************
3332 * vfprintf (MSVCRT.@)
3334 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3336 char buf[2048], *mem = buf;
3337 int written, resize = sizeof(buf), retval;
3338 /* There are two conventions for vsnprintf failing:
3339 * Return -1 if we truncated, or
3340 * Return the number of bytes that would have been written
3341 * The code below handles both cases
3343 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3346 resize = (written == -1 ? resize * 2 : written + 1);
3349 if (!(mem = MSVCRT_malloc(resize)))
3352 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3358 /*********************************************************************
3359 * vfwprintf (MSVCRT.@)
3361 * Is final char included in written (then resize is too big) or not
3362 * (then we must test for equality too)?
3364 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3366 MSVCRT_wchar_t buf[2048], *mem = buf;
3367 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3368 /* See vfprintf comments */
3369 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3372 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3375 if (!(mem = MSVCRT_malloc(resize*sizeof(*mem))))
3378 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3384 /*********************************************************************
3385 * vprintf (MSVCRT.@)
3387 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3389 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3392 /*********************************************************************
3393 * vwprintf (MSVCRT.@)
3395 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3397 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3400 /*********************************************************************
3401 * fprintf (MSVCRT.@)
3403 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3405 __ms_va_list valist;
3407 __ms_va_start(valist, format);
3408 res = MSVCRT_vfprintf(file, format, valist);
3409 __ms_va_end(valist);
3413 /*********************************************************************
3414 * fwprintf (MSVCRT.@)
3416 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3418 __ms_va_list valist;
3420 __ms_va_start(valist, format);
3421 res = MSVCRT_vfwprintf(file, format, valist);
3422 __ms_va_end(valist);
3426 /*********************************************************************
3429 int CDECL MSVCRT_printf(const char *format, ...)
3431 __ms_va_list valist;
3433 __ms_va_start(valist, format);
3434 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3435 __ms_va_end(valist);
3439 /*********************************************************************
3442 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3444 if (c == MSVCRT_EOF)
3446 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3447 msvcrt_alloc_buffer(file);
3450 if(file->_ptr>file->_base) {
3454 MSVCRT_clearerr(file);
3460 /*********************************************************************
3461 * ungetwc (MSVCRT.@)
3463 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3465 MSVCRT_wchar_t mwc = wc;
3466 char * pp = (char *)&mwc;
3468 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3469 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3475 /*********************************************************************
3476 * wprintf (MSVCRT.@)
3478 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3480 __ms_va_list valist;
3482 __ms_va_start(valist, format);
3483 res = MSVCRT_vwprintf(format, valist);
3484 __ms_va_end(valist);
3488 /*********************************************************************
3489 * _getmaxstdio (MSVCRT.@)
3491 int CDECL _getmaxstdio(void)
3493 FIXME("stub, always returns 512\n");
3497 /*********************************************************************
3498 * _setmaxstdio_ (MSVCRT.@)
3500 int CDECL _setmaxstdio(int newmax)
3507 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3511 /*********************************************************************
3512 * __pioinfo (MSVCRT.@)
3513 * FIXME: see MSVCRT_MAX_FILES define.
3515 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3516 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3517 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3518 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3519 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3520 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3521 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3522 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3523 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3524 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3525 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3526 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3529 /*********************************************************************
3530 * __badioinfo (MSVCRT.@)
3532 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };