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>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
65 #define WX_READCR 0x08 /* underlying file is at \r */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
70 /* FIXME: this should be allocated dynamically */
71 #define MSVCRT_MAX_FILES 2048
72 #define MSVCRT_FD_BLOCK_SIZE 64
74 /* ioinfo structure size is different in msvcrXX.dll's */
80 CRITICAL_SECTION crit;
83 /*********************************************************************
84 * __pioinfo (MSVCRT.@)
85 * array of pointers to ioinfo arrays [64]
87 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
89 /*********************************************************************
90 * __badioinfo (MSVCRT.@)
92 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
94 static int MSVCRT_fdstart = 3; /* first unallocated fd */
95 static int MSVCRT_fdend = 3; /* highest allocated fd */
99 CRITICAL_SECTION crit;
102 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
103 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
104 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
106 /* INTERNAL: process umask */
107 static int MSVCRT_umask = 0;
109 /* INTERNAL: static data for tmpnam and _wtmpname functions */
110 static int tmpnam_unique;
111 static char MSVCRT_tmpname[MAX_PATH];
112 static MSVCRT_wchar_t MSVCRT_wtmpname[MAX_PATH];
114 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
115 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
116 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
117 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
119 #define TOUL(x) (ULONGLONG)(x)
120 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
121 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
122 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
123 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
125 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
126 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
127 * and MSVCRT_stream_idx, from race conditions.
128 * It doesn't protect against race conditions manipulating the underlying files
129 * or flags; doing so would probably be better accomplished with per-file
130 * protection, rather than locking the whole table for every change.
132 static CRITICAL_SECTION MSVCRT_file_cs;
133 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
134 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
136 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
138 buf->st_dev = buf64->st_dev;
139 buf->st_ino = buf64->st_ino;
140 buf->st_mode = buf64->st_mode;
141 buf->st_nlink = buf64->st_nlink;
142 buf->st_uid = buf64->st_uid;
143 buf->st_gid = buf64->st_gid;
144 buf->st_rdev = buf64->st_rdev;
145 buf->st_size = buf64->st_size;
146 buf->st_atime = buf64->st_atime;
147 buf->st_mtime = buf64->st_mtime;
148 buf->st_ctime = buf64->st_ctime;
151 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
153 buf->st_dev = buf64->st_dev;
154 buf->st_ino = buf64->st_ino;
155 buf->st_mode = buf64->st_mode;
156 buf->st_nlink = buf64->st_nlink;
157 buf->st_uid = buf64->st_uid;
158 buf->st_gid = buf64->st_gid;
159 buf->st_rdev = buf64->st_rdev;
160 buf->st_size = buf64->st_size;
161 buf->st_atime = buf64->st_atime;
162 buf->st_mtime = buf64->st_mtime;
163 buf->st_ctime = buf64->st_ctime;
166 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
168 /* 1601 to 1970 is 369 years plus 89 leap days */
169 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
171 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
172 ft->dwHighDateTime = ticks >> 32;
173 ft->dwLowDateTime = ticks;
176 static inline ioinfo* msvcrt_get_ioinfo(int fd)
179 if(fd < MSVCRT_MAX_FILES)
180 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
182 return &MSVCRT___badioinfo;
184 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
187 static inline MSVCRT_FILE* msvcrt_get_file(int i)
191 if(i >= MSVCRT_max_streams)
195 return &MSVCRT__iob[i];
197 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
199 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
200 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
201 ERR("out of memory");
202 *MSVCRT__errno() = MSVCRT_ENOMEM;
206 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
208 ret += i%MSVCRT_FD_BLOCK_SIZE;
213 static inline BOOL msvcrt_is_valid_fd(int fd)
215 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
218 /* INTERNAL: Get the HANDLE for a fd
219 * This doesn't lock the table, because a failure will result in
220 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
221 * it returns a valid handle which is about to be closed, a subsequent call
222 * will fail, most likely in a sane way.
224 static HANDLE msvcrt_fdtoh(int fd)
226 if (!msvcrt_is_valid_fd(fd))
228 WARN(":fd (%d) - no handle!\n",fd);
229 *MSVCRT___doserrno() = 0;
230 *MSVCRT__errno() = MSVCRT_EBADF;
231 return INVALID_HANDLE_VALUE;
233 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
234 FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
235 return msvcrt_get_ioinfo(fd)->handle;
238 /* INTERNAL: free a file entry fd */
239 static void msvcrt_free_fd(int fd)
245 fdinfo = msvcrt_get_ioinfo(fd);
246 old_handle = fdinfo->handle;
247 if(fdinfo != &MSVCRT___badioinfo)
249 fdinfo->handle = INVALID_HANDLE_VALUE;
252 TRACE(":fd (%d) freed\n",fd);
253 if (fd < 3) /* don't use 0,1,2 for user files */
258 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
261 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
264 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
270 if (fd == MSVCRT_fdend - 1)
272 if (fd < MSVCRT_fdstart)
278 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
279 /* caller must hold the files lock */
280 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
284 if (fd >= MSVCRT_MAX_FILES)
286 WARN(":files exhausted!\n");
287 *MSVCRT__errno() = MSVCRT_ENFILE;
291 fdinfo = msvcrt_get_ioinfo(fd);
292 if(fdinfo == &MSVCRT___badioinfo) {
295 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
296 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
297 WARN(":out of memory!\n");
298 *MSVCRT__errno() = MSVCRT_ENOMEM;
302 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
303 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
305 fdinfo = msvcrt_get_ioinfo(fd);
308 fdinfo->handle = hand;
309 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
311 /* locate next free slot */
312 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
313 MSVCRT_fdstart = MSVCRT_fdend + 1;
315 while (MSVCRT_fdstart < MSVCRT_fdend &&
316 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
318 /* update last fd in use */
319 if (fd >= MSVCRT_fdend)
320 MSVCRT_fdend = fd + 1;
321 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
325 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
326 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
327 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
333 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
334 static int msvcrt_alloc_fd(HANDLE hand, int flag)
339 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
340 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
345 /* INTERNAL: Allocate a FILE* for an fd slot */
346 /* caller must hold the files lock */
347 static MSVCRT_FILE* msvcrt_alloc_fp(void)
352 for (i = 3; i < MSVCRT_max_streams; i++)
354 file = msvcrt_get_file(i);
358 if (file->_flag == 0)
360 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
368 /* INTERNAL: initialize a FILE* from an open fd */
369 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
371 TRACE(":fd (%d) allocating FILE*\n",fd);
372 if (!msvcrt_is_valid_fd(fd))
374 WARN(":invalid fd %d\n",fd);
375 *MSVCRT___doserrno() = 0;
376 *MSVCRT__errno() = MSVCRT_EBADF;
379 memset(file, 0, sizeof(*file));
381 file->_flag = stream_flags;
383 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
384 InitializeCriticalSection(&((file_crit*)file)->crit);
386 TRACE(":got FILE* (%p)\n",file);
390 /* INTERNAL: Create an inheritance data block (for spawned process)
391 * The inheritance block is made of:
392 * 00 int nb of file descriptor (NBFD)
393 * 04 char file flags (wxflag): repeated for each fd
394 * 4+NBFD HANDLE file handle: repeated for each fd
396 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
403 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
404 *block = MSVCRT_calloc(*size, 1);
410 wxflag_ptr = (char*)*block + sizeof(unsigned);
411 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
413 *(unsigned*)*block = MSVCRT_fdend;
414 for (fd = 0; fd < MSVCRT_fdend; fd++)
416 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
417 fdinfo = msvcrt_get_ioinfo(fd);
418 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
420 *wxflag_ptr = fdinfo->wxflag;
421 *handle_ptr = fdinfo->handle;
426 *handle_ptr = INVALID_HANDLE_VALUE;
428 wxflag_ptr++; handle_ptr++;
433 /* INTERNAL: Set up all file descriptors,
434 * as well as default streams (stdin, stderr and stdout)
436 void msvcrt_init_io(void)
442 InitializeCriticalSection(&MSVCRT_file_cs);
443 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
444 GetStartupInfoA(&si);
445 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
451 count = *(unsigned*)si.lpReserved2;
452 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
453 handle_ptr = (HANDLE*)(wxflag_ptr + count);
455 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
456 count = min(count, MSVCRT_MAX_FILES);
457 for (i = 0; i < count; i++)
459 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
460 msvcrt_alloc_fd_from(*handle_ptr, *wxflag_ptr, i);
462 wxflag_ptr++; handle_ptr++;
464 MSVCRT_fdend = max( 3, count );
465 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
466 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
469 if(!MSVCRT___pioinfo[0])
470 msvcrt_alloc_fd_from(INVALID_HANDLE_VALUE, 0, 3);
472 fdinfo = msvcrt_get_ioinfo(0);
473 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
475 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
476 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
477 GetCurrentProcess(), &fdinfo->handle,
478 0, TRUE, DUPLICATE_SAME_ACCESS))
479 fdinfo->wxflag = WX_OPEN | WX_TEXT;
482 fdinfo = msvcrt_get_ioinfo(1);
483 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
485 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
486 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
487 GetCurrentProcess(), &fdinfo->handle,
488 0, TRUE, DUPLICATE_SAME_ACCESS))
489 fdinfo->wxflag = WX_OPEN | WX_TEXT;
492 fdinfo = msvcrt_get_ioinfo(2);
493 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
495 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
496 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
497 GetCurrentProcess(), &fdinfo->handle,
498 0, TRUE, DUPLICATE_SAME_ACCESS))
499 fdinfo->wxflag = WX_OPEN | WX_TEXT;
502 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(0)->handle,
503 msvcrt_get_ioinfo(1)->handle, msvcrt_get_ioinfo(2)->handle);
505 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
506 for (i = 0; i < 3; i++)
508 /* FILE structs for stdin/out/err are static and never deleted */
509 MSVCRT__iob[i]._file = i;
510 MSVCRT__iob[i]._tmpfname = NULL;
511 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
513 MSVCRT_stream_idx = 3;
516 /* INTERNAL: Flush stdio file buffer */
517 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
520 int cnt=file->_ptr-file->_base;
521 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
522 file->_flag |= MSVCRT__IOERR;
525 file->_ptr=file->_base;
526 file->_cnt=file->_bufsiz;
531 /* INTERNAL: Allocate stdio file buffer */
532 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
534 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
536 file->_bufsiz = MSVCRT_BUFSIZ;
537 file->_flag |= MSVCRT__IOMYBUF;
539 file->_base = (char*)(&file->_charbuf);
541 file->_bufsiz = sizeof(file->_charbuf);
543 file->_ptr = file->_base;
547 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
548 static int msvcrt_int_to_base32(int num, char *str)
563 *p = (num & 31) + '0';
565 *p += ('a' - '0' - 10);
572 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
573 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
588 *p = (num & 31) + '0';
590 *p += ('a' - '0' - 10);
597 /*********************************************************************
598 * __iob_func(MSVCRT.@)
600 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
602 return &MSVCRT__iob[0];
605 /*********************************************************************
608 int CDECL MSVCRT__access(const char *filename, int mode)
610 DWORD attr = GetFileAttributesA(filename);
612 TRACE("(%s,%d) %d\n",filename,mode,attr);
614 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
616 msvcrt_set_errno(GetLastError());
619 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
621 msvcrt_set_errno(ERROR_ACCESS_DENIED);
627 /*********************************************************************
628 * _access_s (MSVCRT.@)
630 int CDECL _access_s(const char *filename, int mode)
632 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
633 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
635 *MSVCRT__errno() = MSVCRT_EINVAL;
639 return MSVCRT__access(filename, mode);
642 /*********************************************************************
643 * _waccess (MSVCRT.@)
645 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
647 DWORD attr = GetFileAttributesW(filename);
649 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
651 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
653 msvcrt_set_errno(GetLastError());
656 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
658 msvcrt_set_errno(ERROR_ACCESS_DENIED);
664 /*********************************************************************
665 * _waccess_s (MSVCRT.@)
667 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
669 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
670 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
672 *MSVCRT__errno() = MSVCRT_EINVAL;
676 return _waccess(filename, mode);
679 /*********************************************************************
682 int CDECL MSVCRT__chmod(const char *path, int flags)
684 DWORD oldFlags = GetFileAttributesA(path);
686 if (oldFlags != INVALID_FILE_ATTRIBUTES)
688 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
689 oldFlags | FILE_ATTRIBUTE_READONLY;
691 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
694 msvcrt_set_errno(GetLastError());
698 /*********************************************************************
701 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
703 DWORD oldFlags = GetFileAttributesW(path);
705 if (oldFlags != INVALID_FILE_ATTRIBUTES)
707 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
708 oldFlags | FILE_ATTRIBUTE_READONLY;
710 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
713 msvcrt_set_errno(GetLastError());
717 /*********************************************************************
720 int CDECL MSVCRT__unlink(const char *path)
722 TRACE("%s\n",debugstr_a(path));
723 if(DeleteFileA(path))
725 TRACE("failed (%d)\n",GetLastError());
726 msvcrt_set_errno(GetLastError());
730 /*********************************************************************
731 * _wunlink (MSVCRT.@)
733 int CDECL _wunlink(const MSVCRT_wchar_t *path)
735 TRACE("(%s)\n",debugstr_w(path));
736 if(DeleteFileW(path))
738 TRACE("failed (%d)\n",GetLastError());
739 msvcrt_set_errno(GetLastError());
743 /* _flushall calls MSVCRT_fflush which calls _flushall */
744 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
746 /*********************************************************************
747 * _flushall (MSVCRT.@)
749 int CDECL _flushall(void)
751 int i, num_flushed = 0;
755 for (i = 3; i < MSVCRT_stream_idx; i++) {
756 file = msvcrt_get_file(i);
760 if(file->_flag & MSVCRT__IOWRT) {
768 TRACE(":flushed (%d) handles\n",num_flushed);
772 /*********************************************************************
775 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
779 } else if(file->_flag & MSVCRT__IOWRT) {
782 MSVCRT__lock_file(file);
783 res = msvcrt_flush_buffer(file);
784 MSVCRT__unlock_file(file);
791 /*********************************************************************
794 int CDECL MSVCRT__close(int fd)
800 hand = msvcrt_fdtoh(fd);
801 TRACE(":fd (%d) handle (%p)\n",fd,hand);
802 if (hand == INVALID_HANDLE_VALUE)
804 else if (!CloseHandle(hand))
806 WARN(":failed-last error (%d)\n",GetLastError());
807 msvcrt_set_errno(GetLastError());
820 /*********************************************************************
823 int CDECL _commit(int fd)
825 HANDLE hand = msvcrt_fdtoh(fd);
827 TRACE(":fd (%d) handle (%p)\n",fd,hand);
828 if (hand == INVALID_HANDLE_VALUE)
831 if (!FlushFileBuffers(hand))
833 if (GetLastError() == ERROR_INVALID_HANDLE)
835 /* FlushFileBuffers fails for console handles
836 * so we ignore this error.
840 TRACE(":failed-last error (%d)\n",GetLastError());
841 msvcrt_set_errno(GetLastError());
848 /*********************************************************************
851 * MSDN isn't clear on this point, but the remarks for _pipe
852 * indicate file descriptors duplicated with _dup and _dup2 are always
855 int CDECL MSVCRT__dup2(int od, int nd)
859 TRACE("(od=%d, nd=%d)\n", od, nd);
861 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
865 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
866 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
868 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
870 if (msvcrt_is_valid_fd(nd))
872 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
876 *MSVCRT__errno() = MSVCRT_EMFILE;
880 /* _dup2 returns 0, not nd, on success */
887 msvcrt_set_errno(GetLastError());
892 *MSVCRT__errno() = MSVCRT_EBADF;
899 /*********************************************************************
902 int CDECL MSVCRT__dup(int od)
908 if (MSVCRT__dup2(od, fd) == 0)
916 /*********************************************************************
919 int CDECL _eof(int fd)
922 LONG hcurpos,hendpos;
923 HANDLE hand = msvcrt_fdtoh(fd);
925 TRACE(":fd (%d) handle (%p)\n",fd,hand);
927 if (hand == INVALID_HANDLE_VALUE)
930 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
932 /* Otherwise we do it the hard way */
933 hcurpos = hendpos = 0;
934 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
935 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
937 if (curpos == endpos && hcurpos == hendpos)
939 /* FIXME: shouldn't WX_ATEOF be set here? */
943 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
947 /*********************************************************************
948 * _fcloseall (MSVCRT.@)
950 int CDECL MSVCRT__fcloseall(void)
952 int num_closed = 0, i;
956 for (i = 3; i < MSVCRT_stream_idx; i++) {
957 file = msvcrt_get_file(i);
959 if (file->_flag && !MSVCRT_fclose(file))
964 TRACE(":closed (%d) handles\n",num_closed);
968 /* free everything on process exit */
969 void msvcrt_free_io(void)
974 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
975 * stdout, and stderr (unlike GNU), so we need to fclose() them here
976 * or they won't get flushed.
978 MSVCRT_fclose(&MSVCRT__iob[0]);
979 MSVCRT_fclose(&MSVCRT__iob[1]);
980 MSVCRT_fclose(&MSVCRT__iob[2]);
982 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
983 MSVCRT_free(MSVCRT___pioinfo[i]);
985 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
986 MSVCRT_free(MSVCRT_fstream[i]);
988 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
989 DeleteCriticalSection(&MSVCRT_file_cs);
992 /*********************************************************************
993 * _lseeki64 (MSVCRT.@)
995 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
997 HANDLE hand = msvcrt_fdtoh(fd);
1000 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1001 if (hand == INVALID_HANDLE_VALUE)
1004 if (whence < 0 || whence > 2)
1006 *MSVCRT__errno() = MSVCRT_EINVAL;
1010 TRACE(":fd (%d) to %s pos %s\n",
1011 fd,wine_dbgstr_longlong(offset),
1012 (whence==SEEK_SET)?"SEEK_SET":
1013 (whence==SEEK_CUR)?"SEEK_CUR":
1014 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1016 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1017 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1018 ofs.QuadPart = offset;
1019 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1020 GetLastError() == ERROR_SUCCESS)
1022 msvcrt_get_ioinfo(fd)->wxflag &= ~(WX_ATEOF|WX_READEOF);
1023 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1025 return ofs.QuadPart;
1027 TRACE(":error-last error (%d)\n",GetLastError());
1028 msvcrt_set_errno(GetLastError());
1032 /*********************************************************************
1035 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1037 return MSVCRT__lseeki64(fd, offset, whence);
1040 /*********************************************************************
1041 * _lock_file (MSVCRT.@)
1043 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1045 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1046 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1048 EnterCriticalSection(&((file_crit*)file)->crit);
1051 /*********************************************************************
1052 * _unlock_file (MSVCRT.@)
1054 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1056 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1057 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1059 LeaveCriticalSection(&((file_crit*)file)->crit);
1062 /*********************************************************************
1063 * _locking (MSVCRT.@)
1065 * This is untested; the underlying LockFile doesn't work yet.
1067 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1071 HANDLE hand = msvcrt_fdtoh(fd);
1073 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1074 if (hand == INVALID_HANDLE_VALUE)
1077 if (mode < 0 || mode > 4)
1079 *MSVCRT__errno() = MSVCRT_EINVAL;
1083 TRACE(":fd (%d) by 0x%08x mode %s\n",
1084 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1085 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1086 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1087 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1088 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1091 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1093 FIXME ("Seek failed\n");
1094 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1097 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1100 ret = 1; /* just to satisfy gcc */
1103 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1108 else if (mode == MSVCRT__LK_UNLCK)
1109 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1111 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1112 /* FIXME - what about error settings? */
1113 return ret ? 0 : -1;
1116 /*********************************************************************
1117 * _fseeki64 (MSVCRT.@)
1119 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1121 /* Flush output if needed */
1122 if(file->_flag & MSVCRT__IOWRT)
1123 msvcrt_flush_buffer(file);
1125 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1126 offset -= file->_cnt;
1127 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
1128 /* Black magic correction for CR removal */
1130 for (i=0; i<file->_cnt; i++) {
1131 if (file->_ptr[i] == '\n')
1134 /* Black magic when reading CR at buffer boundary*/
1135 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
1139 /* Discard buffered input */
1141 file->_ptr = file->_base;
1142 /* Reset direction of i/o */
1143 if(file->_flag & MSVCRT__IORW) {
1144 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1146 /* Clear end of file flag */
1147 file->_flag &= ~MSVCRT__IOEOF;
1148 return (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1151 /*********************************************************************
1154 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1156 return MSVCRT__fseeki64( file, offset, whence );
1159 /*********************************************************************
1160 * _chsize (MSVCRT.@)
1162 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1168 TRACE("(fd=%d, size=%d)\n", fd, size);
1172 handle = msvcrt_fdtoh(fd);
1173 if (handle != INVALID_HANDLE_VALUE)
1175 /* save the current file pointer */
1176 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1179 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1182 ret = SetEndOfFile(handle);
1183 if (!ret) msvcrt_set_errno(GetLastError());
1186 /* restore the file pointer */
1187 MSVCRT__lseek(fd, cur, SEEK_SET);
1192 return ret ? 0 : -1;
1195 /*********************************************************************
1196 * clearerr (MSVCRT.@)
1198 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1200 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1201 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1204 /*********************************************************************
1207 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1209 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1210 MSVCRT_fseek(file, 0L, SEEK_SET);
1211 MSVCRT_clearerr(file);
1214 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1216 int plus = strchrW(mode, '+') != NULL;
1221 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1222 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1225 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1226 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1229 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1230 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1233 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1234 *MSVCRT__errno() = MSVCRT_EINVAL;
1242 *open_flags |= MSVCRT__O_BINARY;
1243 *open_flags &= ~MSVCRT__O_TEXT;
1246 *open_flags |= MSVCRT__O_TEXT;
1247 *open_flags &= ~MSVCRT__O_BINARY;
1253 FIXME(":unknown flag %c not supported\n",mode[-1]);
1258 /*********************************************************************
1259 * _fdopen (MSVCRT.@)
1261 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1264 MSVCRT_wchar_t *modeW = NULL;
1266 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1268 ret = MSVCRT__wfdopen(fd, modeW);
1274 /*********************************************************************
1275 * _wfdopen (MSVCRT.@)
1277 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1279 int open_flags, stream_flags;
1282 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1285 if (!(file = msvcrt_alloc_fp()))
1287 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1292 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1298 /*********************************************************************
1299 * _filelength (MSVCRT.@)
1301 LONG CDECL MSVCRT__filelength(int fd)
1303 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1306 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1309 if (endPos != curPos)
1310 MSVCRT__lseek(fd, curPos, SEEK_SET);
1317 /*********************************************************************
1318 * _filelengthi64 (MSVCRT.@)
1320 __int64 CDECL MSVCRT__filelengthi64(int fd)
1322 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1325 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1328 if (endPos != curPos)
1329 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1336 /*********************************************************************
1337 * _fileno (MSVCRT.@)
1339 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1341 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1345 /*********************************************************************
1346 * _fstat64 (MSVCRT.@)
1348 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1352 BY_HANDLE_FILE_INFORMATION hfi;
1353 HANDLE hand = msvcrt_fdtoh(fd);
1355 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1356 if (hand == INVALID_HANDLE_VALUE)
1361 WARN(":failed-NULL buf\n");
1362 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1366 memset(&hfi, 0, sizeof(hfi));
1367 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1368 type = GetFileType(hand);
1369 if (type == FILE_TYPE_PIPE)
1371 buf->st_dev = buf->st_rdev = fd;
1372 buf->st_mode = S_IFIFO;
1375 else if (type == FILE_TYPE_CHAR)
1377 buf->st_dev = buf->st_rdev = fd;
1378 buf->st_mode = S_IFCHR;
1381 else /* FILE_TYPE_DISK etc. */
1383 if (!GetFileInformationByHandle(hand, &hfi))
1385 WARN(":failed-last error (%d)\n",GetLastError());
1386 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1389 buf->st_mode = S_IFREG | 0444;
1390 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1391 buf->st_mode |= 0222;
1392 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1393 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1395 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1396 buf->st_mtime = buf->st_ctime = dw;
1397 buf->st_nlink = hfi.nNumberOfLinks;
1399 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1404 /*********************************************************************
1405 * _fstati64 (MSVCRT.@)
1407 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1410 struct MSVCRT__stat64 buf64;
1412 ret = MSVCRT__fstat64(fd, &buf64);
1414 msvcrt_stat64_to_stati64(&buf64, buf);
1418 /*********************************************************************
1421 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1423 struct MSVCRT__stat64 buf64;
1425 ret = MSVCRT__fstat64(fd, &buf64);
1427 msvcrt_stat64_to_stat(&buf64, buf);
1431 /*********************************************************************
1432 * _futime64 (MSVCRT.@)
1434 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1436 HANDLE hand = msvcrt_fdtoh(fd);
1441 time_to_filetime( MSVCRT__time64(NULL), &at );
1446 time_to_filetime( t->actime, &at );
1447 time_to_filetime( t->modtime, &wt );
1450 if (!SetFileTime(hand, NULL, &at, &wt))
1452 msvcrt_set_errno(GetLastError());
1458 /*********************************************************************
1459 * _futime32 (MSVCRT.@)
1461 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1463 struct MSVCRT___utimbuf64 t64;
1464 t64.actime = t->actime;
1465 t64.modtime = t->modtime;
1466 return _futime64( fd, &t64 );
1469 /*********************************************************************
1470 * _futime (MSVCRT.@)
1473 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1475 return _futime64( fd, t );
1478 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1480 return _futime32( fd, t );
1484 /*********************************************************************
1485 * _get_osfhandle (MSVCRT.@)
1487 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1489 HANDLE hand = msvcrt_fdtoh(fd);
1490 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1492 return (MSVCRT_intptr_t)hand;
1495 /*********************************************************************
1496 * _isatty (MSVCRT.@)
1498 int CDECL _isatty(int fd)
1500 HANDLE hand = msvcrt_fdtoh(fd);
1502 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1503 if (hand == INVALID_HANDLE_VALUE)
1506 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1509 /*********************************************************************
1510 * _mktemp (MSVCRT.@)
1512 char * CDECL _mktemp(char *pattern)
1515 char *retVal = pattern;
1520 numX = (*pattern++ == 'X')? numX + 1 : 0;
1524 id = GetCurrentProcessId();
1528 int tempNum = id / 10;
1529 *pattern-- = id - (tempNum * 10) + '0';
1535 *pattern = letter++;
1536 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1537 GetLastError() == ERROR_FILE_NOT_FOUND)
1539 } while(letter <= 'z');
1543 /*********************************************************************
1544 * _wmktemp (MSVCRT.@)
1546 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1549 MSVCRT_wchar_t *retVal = pattern;
1551 MSVCRT_wchar_t letter = 'a';
1554 numX = (*pattern++ == 'X')? numX + 1 : 0;
1558 id = GetCurrentProcessId();
1562 int tempNum = id / 10;
1563 *pattern-- = id - (tempNum * 10) + '0';
1569 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1570 GetLastError() == ERROR_FILE_NOT_FOUND)
1572 *pattern = letter++;
1573 } while(letter != '|');
1577 static unsigned split_oflags(unsigned oflags)
1580 unsigned unsupp; /* until we support everything */
1582 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1583 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1584 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1585 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1586 else wxflags |= WX_TEXT; /* default to TEXT*/
1587 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1589 if ((unsupp = oflags & ~(
1590 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1591 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1592 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1593 MSVCRT__O_NOINHERIT|
1594 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1596 ERR(":unsupported oflags 0x%04x\n",unsupp);
1601 /*********************************************************************
1604 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1607 SECURITY_ATTRIBUTES sa;
1608 HANDLE readHandle, writeHandle;
1612 *MSVCRT__errno() = MSVCRT_EINVAL;
1616 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1617 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1618 sa.lpSecurityDescriptor = NULL;
1619 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1621 unsigned int wxflags = split_oflags(textmode);
1625 fd = msvcrt_alloc_fd(readHandle, wxflags);
1629 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1637 MSVCRT__close(pfds[0]);
1638 CloseHandle(writeHandle);
1639 *MSVCRT__errno() = MSVCRT_EMFILE;
1644 CloseHandle(readHandle);
1645 CloseHandle(writeHandle);
1646 *MSVCRT__errno() = MSVCRT_EMFILE;
1651 msvcrt_set_errno(GetLastError());
1656 /*********************************************************************
1657 * _sopen_s (MSVCRT.@)
1659 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1661 DWORD access = 0, creation = 0, attrib;
1665 SECURITY_ATTRIBUTES sa;
1667 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1668 fd, path, oflags, shflags, pmode);
1672 MSVCRT_INVALID_PMT("null out fd pointer");
1673 *MSVCRT__errno() = MSVCRT_EINVAL;
1674 return MSVCRT_EINVAL;
1678 wxflag = split_oflags(oflags);
1679 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1681 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1682 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1683 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1686 if (oflags & MSVCRT__O_CREAT)
1688 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1689 FIXME(": pmode 0x%04x ignored\n", pmode);
1691 WARN(": pmode 0x%04x ignored\n", pmode);
1693 if (oflags & MSVCRT__O_EXCL)
1694 creation = CREATE_NEW;
1695 else if (oflags & MSVCRT__O_TRUNC)
1696 creation = CREATE_ALWAYS;
1698 creation = OPEN_ALWAYS;
1700 else /* no MSVCRT__O_CREAT */
1702 if (oflags & MSVCRT__O_TRUNC)
1703 creation = TRUNCATE_EXISTING;
1705 creation = OPEN_EXISTING;
1710 case MSVCRT__SH_DENYRW:
1713 case MSVCRT__SH_DENYWR:
1714 sharing = FILE_SHARE_READ;
1716 case MSVCRT__SH_DENYRD:
1717 sharing = FILE_SHARE_WRITE;
1719 case MSVCRT__SH_DENYNO:
1720 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1723 ERR( "Unhandled shflags 0x%x\n", shflags );
1724 return MSVCRT_EINVAL;
1726 attrib = FILE_ATTRIBUTE_NORMAL;
1728 if (oflags & MSVCRT__O_TEMPORARY)
1730 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1732 sharing |= FILE_SHARE_DELETE;
1735 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1736 sa.lpSecurityDescriptor = NULL;
1737 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1739 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1740 if (hand == INVALID_HANDLE_VALUE) {
1741 WARN(":failed-last error (%d)\n", GetLastError());
1742 msvcrt_set_errno(GetLastError());
1743 return *MSVCRT__errno();
1746 *fd = msvcrt_alloc_fd(hand, wxflag);
1748 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1752 /*********************************************************************
1755 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1760 if (oflags & MSVCRT__O_CREAT)
1764 __ms_va_start(ap, shflags);
1765 pmode = va_arg(ap, int);
1771 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
1775 /*********************************************************************
1776 * _wsopen_s (MSVCRT.@)
1778 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1780 DWORD access = 0, creation = 0, attrib;
1781 SECURITY_ATTRIBUTES sa;
1786 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1787 fd, debugstr_w(path), oflags, shflags, pmode);
1791 MSVCRT_INVALID_PMT("null out fd pointer");
1792 *MSVCRT__errno() = MSVCRT_EINVAL;
1793 return MSVCRT_EINVAL;
1797 wxflag = split_oflags(oflags);
1798 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1800 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1801 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1802 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1805 if (oflags & MSVCRT__O_CREAT)
1807 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1808 FIXME(": pmode 0x%04x ignored\n", pmode);
1810 WARN(": pmode 0x%04x ignored\n", pmode);
1812 if (oflags & MSVCRT__O_EXCL)
1813 creation = CREATE_NEW;
1814 else if (oflags & MSVCRT__O_TRUNC)
1815 creation = CREATE_ALWAYS;
1817 creation = OPEN_ALWAYS;
1819 else /* no MSVCRT__O_CREAT */
1821 if (oflags & MSVCRT__O_TRUNC)
1822 creation = TRUNCATE_EXISTING;
1824 creation = OPEN_EXISTING;
1829 case MSVCRT__SH_DENYRW:
1832 case MSVCRT__SH_DENYWR:
1833 sharing = FILE_SHARE_READ;
1835 case MSVCRT__SH_DENYRD:
1836 sharing = FILE_SHARE_WRITE;
1838 case MSVCRT__SH_DENYNO:
1839 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1842 ERR( "Unhandled shflags 0x%x\n", shflags );
1843 return MSVCRT_EINVAL;
1845 attrib = FILE_ATTRIBUTE_NORMAL;
1847 if (oflags & MSVCRT__O_TEMPORARY)
1849 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1851 sharing |= FILE_SHARE_DELETE;
1854 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1855 sa.lpSecurityDescriptor = NULL;
1856 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1858 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1860 if (hand == INVALID_HANDLE_VALUE) {
1861 WARN(":failed-last error (%d)\n",GetLastError());
1862 msvcrt_set_errno(GetLastError());
1863 msvcrt_set_errno(GetLastError());
1864 return *MSVCRT__errno();
1867 *fd = msvcrt_alloc_fd(hand, wxflag);
1869 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1873 /*********************************************************************
1874 * _wsopen (MSVCRT.@)
1876 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1881 if (oflags & MSVCRT__O_CREAT)
1885 __ms_va_start(ap, shflags);
1886 pmode = va_arg(ap, int);
1892 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1896 /*********************************************************************
1899 int CDECL MSVCRT__open( const char *path, int flags, ... )
1903 if (flags & MSVCRT__O_CREAT)
1906 __ms_va_start(ap, flags);
1907 pmode = va_arg(ap, int);
1909 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1912 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1915 /*********************************************************************
1918 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1922 if (flags & MSVCRT__O_CREAT)
1925 __ms_va_start(ap, flags);
1926 pmode = va_arg(ap, int);
1928 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1931 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1934 /*********************************************************************
1937 int CDECL MSVCRT__creat(const char *path, int flags)
1939 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1940 return MSVCRT__open(path, usedFlags);
1943 /*********************************************************************
1944 * _wcreat (MSVCRT.@)
1946 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1948 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1949 return _wopen(path, usedFlags);
1952 /*********************************************************************
1953 * _open_osfhandle (MSVCRT.@)
1955 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1959 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1960 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1961 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1962 * text - it never sets MSVCRT__O_BINARY.
1964 /* don't let split_oflags() decide the mode if no mode is passed */
1965 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1966 oflags |= MSVCRT__O_BINARY;
1968 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1969 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1973 /*********************************************************************
1976 int CDECL _rmtmp(void)
1978 int num_removed = 0, i;
1982 for (i = 3; i < MSVCRT_stream_idx; i++) {
1983 file = msvcrt_get_file(i);
1985 if (file->_tmpfname)
1987 MSVCRT_fclose(file);
1994 TRACE(":removed (%d) temp files\n",num_removed);
1998 /*********************************************************************
2001 * When reading \r as last character in text mode, read() positions
2002 * the file pointer on the \r character while getc() goes on to
2005 static int read_i(int fd, void *buf, unsigned int count)
2008 char *bufstart = buf;
2009 HANDLE hand = msvcrt_fdtoh(fd);
2010 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2015 if (fdinfo->wxflag & WX_READEOF) {
2016 fdinfo->wxflag |= WX_ATEOF;
2017 TRACE("already at EOF, returning 0\n");
2020 /* Don't trace small reads, it gets *very* annoying */
2022 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2023 if (hand == INVALID_HANDLE_VALUE)
2026 /* Reading single bytes in O_TEXT mode makes things slow
2027 * So read big chunks
2029 if (ReadFile(hand, bufstart, count, &num_read, NULL))
2031 if (count != 0 && num_read == 0)
2033 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2034 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2036 else if (fdinfo->wxflag & WX_TEXT)
2039 if (bufstart[num_read-1] == '\r')
2043 fdinfo->wxflag &= ~WX_READCR;
2044 ReadFile(hand, bufstart, 1, &num_read, NULL);
2048 fdinfo->wxflag |= WX_READCR;
2053 fdinfo->wxflag &= ~WX_READCR;
2054 for (i=0, j=0; i<num_read; i++)
2056 /* in text mode, a ctrl-z signals EOF */
2057 if (bufstart[i] == 0x1a)
2059 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2060 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2063 /* in text mode, strip \r if followed by \n.
2064 * BUG: should save state across calls somehow, so CR LF that
2065 * straddles buffer boundary gets recognized properly?
2067 if ((bufstart[i] != '\r')
2068 || ((i+1) < num_read && bufstart[i+1] != '\n'))
2069 bufstart[j++] = bufstart[i];
2076 if (GetLastError() == ERROR_BROKEN_PIPE)
2078 TRACE(":end-of-pipe\n");
2079 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2084 TRACE(":failed-last error (%d)\n",GetLastError());
2090 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2094 /*********************************************************************
2097 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2100 num_read = read_i(fd, buf, count);
2104 /*********************************************************************
2105 * _setmode (MSVCRT.@)
2107 int CDECL _setmode(int fd,int mode)
2109 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2110 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
2111 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
2112 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
2113 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2115 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2119 /*********************************************************************
2120 * _stat64 (MSVCRT.@)
2122 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2125 WIN32_FILE_ATTRIBUTE_DATA hfi;
2126 unsigned short mode = ALL_S_IREAD;
2129 TRACE(":file (%s) buf(%p)\n",path,buf);
2131 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2133 TRACE("failed (%d)\n",GetLastError());
2134 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2138 memset(buf,0,sizeof(struct MSVCRT__stat64));
2140 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2141 Bon 011120: This FIXME seems incorrect
2142 Also a letter as first char isn't enough to be classified
2145 if (isalpha(*path)&& (*(path+1)==':'))
2146 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2148 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2150 plen = strlen(path);
2152 /* Dir, or regular file? */
2153 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2154 (path[plen-1] == '\\'))
2155 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2158 mode |= MSVCRT__S_IFREG;
2160 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2162 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2163 (tolower(path[plen-3]) << 16);
2164 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2165 mode |= ALL_S_IEXEC;
2169 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2170 mode |= ALL_S_IWRITE;
2172 buf->st_mode = mode;
2174 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2175 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2177 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2178 buf->st_mtime = buf->st_ctime = dw;
2179 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2180 (int)(buf->st_size >> 32),(int)buf->st_size,
2181 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2185 /*********************************************************************
2186 * _stati64 (MSVCRT.@)
2188 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2191 struct MSVCRT__stat64 buf64;
2193 ret = MSVCRT_stat64(path, &buf64);
2195 msvcrt_stat64_to_stati64(&buf64, buf);
2199 /*********************************************************************
2202 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2204 struct MSVCRT__stat64 buf64;
2206 ret = MSVCRT_stat64( path, &buf64);
2208 msvcrt_stat64_to_stat(&buf64, buf);
2212 /*********************************************************************
2213 * _wstat64 (MSVCRT.@)
2215 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2218 WIN32_FILE_ATTRIBUTE_DATA hfi;
2219 unsigned short mode = ALL_S_IREAD;
2222 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2224 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2226 TRACE("failed (%d)\n",GetLastError());
2227 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2231 memset(buf,0,sizeof(struct MSVCRT__stat64));
2233 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2234 if (MSVCRT_iswalpha(*path))
2235 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2237 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2239 plen = strlenW(path);
2241 /* Dir, or regular file? */
2242 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2243 (path[plen-1] == '\\'))
2244 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2247 mode |= MSVCRT__S_IFREG;
2249 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2251 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2252 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2253 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2254 mode |= ALL_S_IEXEC;
2258 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2259 mode |= ALL_S_IWRITE;
2261 buf->st_mode = mode;
2263 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2264 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2266 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2267 buf->st_mtime = buf->st_ctime = dw;
2268 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2269 (int)(buf->st_size >> 32),(int)buf->st_size,
2270 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2274 /*********************************************************************
2275 * _wstati64 (MSVCRT.@)
2277 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2280 struct MSVCRT__stat64 buf64;
2282 ret = MSVCRT__wstat64(path, &buf64);
2284 msvcrt_stat64_to_stati64(&buf64, buf);
2288 /*********************************************************************
2291 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2294 struct MSVCRT__stat64 buf64;
2296 ret = MSVCRT__wstat64( path, &buf64 );
2297 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2301 /*********************************************************************
2304 MSVCRT_long CDECL MSVCRT__tell(int fd)
2306 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2309 /*********************************************************************
2310 * _telli64 (MSVCRT.@)
2312 __int64 CDECL _telli64(int fd)
2314 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2317 /*********************************************************************
2318 * _tempnam (MSVCRT.@)
2320 char * CDECL _tempnam(const char *dir, const char *prefix)
2322 char tmpbuf[MAX_PATH];
2323 const char *tmp_dir = MSVCRT_getenv("TMP");
2325 if (tmp_dir) dir = tmp_dir;
2327 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2328 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2330 TRACE("got name (%s)\n",tmpbuf);
2331 DeleteFileA(tmpbuf);
2332 return _strdup(tmpbuf);
2334 TRACE("failed (%d)\n",GetLastError());
2338 /*********************************************************************
2339 * _wtempnam (MSVCRT.@)
2341 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2343 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2345 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2346 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2348 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2349 DeleteFileW(tmpbuf);
2350 return _wcsdup(tmpbuf);
2352 TRACE("failed (%d)\n",GetLastError());
2356 /*********************************************************************
2359 int CDECL MSVCRT__umask(int umask)
2361 int old_umask = MSVCRT_umask;
2362 TRACE("(%d)\n",umask);
2363 MSVCRT_umask = umask;
2367 /*********************************************************************
2368 * _utime64 (MSVCRT.@)
2370 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2372 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2376 int retVal = _futime64(fd, t);
2383 /*********************************************************************
2384 * _utime32 (MSVCRT.@)
2386 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2388 struct MSVCRT___utimbuf64 t64;
2389 t64.actime = t->actime;
2390 t64.modtime = t->modtime;
2391 return _utime64( path, &t64 );
2394 /*********************************************************************
2398 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2400 return _utime64( path, t );
2403 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2405 return _utime32( path, t );
2409 /*********************************************************************
2410 * _wutime64 (MSVCRT.@)
2412 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2414 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2418 int retVal = _futime64(fd, t);
2425 /*********************************************************************
2426 * _wutime32 (MSVCRT.@)
2428 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2430 struct MSVCRT___utimbuf64 t64;
2431 t64.actime = t->actime;
2432 t64.modtime = t->modtime;
2433 return _wutime64( path, &t64 );
2436 /*********************************************************************
2437 * _wutime (MSVCRT.@)
2440 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2442 return _wutime64( path, t );
2445 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2447 return _wutime32( path, t );
2451 /*********************************************************************
2454 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2457 HANDLE hand = msvcrt_fdtoh(fd);
2459 /* Don't trace small writes, it gets *very* annoying */
2462 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2464 if (hand == INVALID_HANDLE_VALUE)
2466 *MSVCRT__errno() = MSVCRT_EBADF;
2470 /* If appending, go to EOF */
2471 if (msvcrt_get_ioinfo(fd)->wxflag & WX_APPEND)
2472 MSVCRT__lseek(fd, 0, FILE_END);
2474 if (!(msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT))
2476 if (WriteFile(hand, buf, count, &num_written, NULL)
2477 && (num_written == count))
2479 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2480 hand, GetLastError());
2481 *MSVCRT__errno() = MSVCRT_ENOSPC;
2485 unsigned int i, j, nr_lf;
2488 const char *s = buf, *buf_start = buf;
2489 /* find number of \n ( without preceding \r ) */
2490 for ( nr_lf=0,i = 0; i <count; i++)
2495 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2500 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2502 for (s = buf, i = 0, j = 0; i < count; i++)
2507 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2514 FIXME("Malloc failed\n");
2522 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2524 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2525 fd, hand, GetLastError(), num_written);
2526 *MSVCRT__errno() = MSVCRT_ENOSPC;
2529 return s - buf_start;
2541 /*********************************************************************
2544 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2547 len = MSVCRT__write(file->_file, &val, sizeof(val));
2548 if (len == sizeof(val)) return val;
2549 file->_flag |= MSVCRT__IOERR;
2553 /*********************************************************************
2556 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2560 MSVCRT__lock_file(file);
2562 MSVCRT_free(file->_tmpfname);
2563 file->_tmpfname = NULL;
2564 /* flush stdio buffers */
2565 if(file->_flag & MSVCRT__IOWRT)
2566 MSVCRT_fflush(file);
2567 if(file->_flag & MSVCRT__IOMYBUF)
2568 MSVCRT_free(file->_base);
2570 r=MSVCRT__close(file->_file);
2573 MSVCRT__unlock_file(file);
2574 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
2575 DeleteCriticalSection(&((file_crit*)file)->crit);
2577 if(file == msvcrt_get_file(MSVCRT_stream_idx-1)) {
2578 while(MSVCRT_stream_idx>3 && !file->_flag) {
2579 MSVCRT_stream_idx--;
2580 file = msvcrt_get_file(MSVCRT_stream_idx-1);
2584 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2587 /*********************************************************************
2590 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2592 return file->_flag & MSVCRT__IOEOF;
2595 /*********************************************************************
2598 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2600 return file->_flag & MSVCRT__IOERR;
2603 /*********************************************************************
2604 * _filbuf (MSVCRT.@)
2606 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2609 MSVCRT__lock_file(file);
2611 /* Allocate buffer if needed */
2612 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
2613 msvcrt_alloc_buffer(file);
2615 if(!(file->_flag & MSVCRT__IOREAD)) {
2616 if(file->_flag & MSVCRT__IORW)
2617 file->_flag |= MSVCRT__IOREAD;
2619 MSVCRT__unlock_file(file);
2624 if(file->_flag & MSVCRT__IONBF) {
2626 if ((r = read_i(file->_file,&c,1)) != 1) {
2627 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2628 MSVCRT__unlock_file(file);
2632 MSVCRT__unlock_file(file);
2635 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2637 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2639 MSVCRT__unlock_file(file);
2644 file->_ptr = file->_base+1;
2645 c = *(unsigned char *)file->_base;
2646 MSVCRT__unlock_file(file);
2651 /*********************************************************************
2654 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2660 i = (unsigned char *)file->_ptr++;
2663 j = MSVCRT__filbuf(file);
2667 /*********************************************************************
2668 * _fgetchar (MSVCRT.@)
2670 int CDECL _fgetchar(void)
2672 return MSVCRT_fgetc(MSVCRT_stdin);
2675 /*********************************************************************
2678 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2680 int cc = MSVCRT_EOF;
2681 char * buf_start = s;
2683 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2684 file,file->_file,s,size);
2686 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2691 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2693 TRACE(":nothing read\n");
2696 if ((cc != MSVCRT_EOF) && (size > 1))
2699 TRACE(":got %s\n", debugstr_a(buf_start));
2703 /*********************************************************************
2706 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2707 * the CR from CR/LF combinations
2709 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2713 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
2720 for(i=0; i<sizeof(wc); i++)
2730 j = MSVCRT__filbuf(file);
2733 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2743 c = MSVCRT_fgetc(file);
2744 if ((get_locale()->locinfo->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2746 FIXME("Treat Multibyte characters\n");
2748 if (c == MSVCRT_EOF)
2751 return (MSVCRT_wint_t)c;
2754 /*********************************************************************
2757 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2763 for (j=0; j<sizeof(int); j++) {
2764 k = MSVCRT_fgetc(file);
2765 if (k == MSVCRT_EOF) {
2766 file->_flag |= MSVCRT__IOEOF;
2774 /*********************************************************************
2777 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2779 return MSVCRT_fgetwc(file);
2782 /*********************************************************************
2783 * _fgetwchar (MSVCRT.@)
2785 MSVCRT_wint_t CDECL _fgetwchar(void)
2787 return MSVCRT_fgetwc(MSVCRT_stdin);
2790 /*********************************************************************
2791 * getwchar (MSVCRT.@)
2793 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2795 return _fgetwchar();
2798 /*********************************************************************
2801 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2803 int cc = MSVCRT_WEOF;
2804 MSVCRT_wchar_t * buf_start = s;
2806 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2807 file,file->_file,s,size);
2809 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2814 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2816 TRACE(":nothing read\n");
2819 if ((cc != MSVCRT_WEOF) && (size > 1))
2822 TRACE(":got %s\n", debugstr_w(buf_start));
2826 /*********************************************************************
2829 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2831 MSVCRT_size_t wrcnt=size * nmemb;
2836 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2837 memcpy(file->_ptr, ptr, pcnt);
2842 ptr = (const char*)ptr + pcnt;
2843 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2844 if(file->_flag & MSVCRT__IORW) {
2845 file->_flag |= MSVCRT__IOWRT;
2851 int res=msvcrt_flush_buffer(file);
2853 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2856 file->_flag |= MSVCRT__IOERR;
2859 written += pwritten;
2862 return written / size;
2865 /*********************************************************************
2868 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2870 MSVCRT_wchar_t mwc=wc;
2871 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2876 /*********************************************************************
2877 * _fputwchar (MSVCRT.@)
2879 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2881 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2884 /*********************************************************************
2885 * _wfsopen (MSVCRT.@)
2887 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2890 int open_flags, stream_flags, fd;
2892 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2894 /* map mode string to open() flags. "man fopen" for possibilities. */
2895 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2899 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2902 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2904 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2911 TRACE(":got (%p)\n",file);
2912 if (fd >= 0 && !file)
2918 /*********************************************************************
2919 * _fsopen (MSVCRT.@)
2921 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2924 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2926 if (path && !(pathW = msvcrt_wstrdupa(path))) {
2927 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2928 *MSVCRT__errno() = MSVCRT_EINVAL;
2931 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2934 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2935 *MSVCRT__errno() = MSVCRT_EINVAL;
2939 ret = MSVCRT__wfsopen(pathW, modeW, share);
2946 /*********************************************************************
2949 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2951 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2954 /*********************************************************************
2955 * fopen_s (MSVCRT.@)
2957 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
2958 const char *filename, const char *mode)
2960 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2961 !MSVCRT_CHECK_PMT(mode != NULL)) {
2962 *MSVCRT__errno() = MSVCRT_EINVAL;
2963 return MSVCRT_EINVAL;
2966 *pFile = MSVCRT_fopen(filename, mode);
2969 return *MSVCRT__errno();
2973 /*********************************************************************
2974 * _wfopen (MSVCRT.@)
2976 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2978 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2981 /*********************************************************************
2982 * _wfopen_s (MSVCRT.@)
2984 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
2985 const MSVCRT_wchar_t *mode)
2987 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2988 !MSVCRT_CHECK_PMT(mode != NULL)) {
2989 *MSVCRT__errno() = MSVCRT_EINVAL;
2990 return MSVCRT_EINVAL;
2993 *pFile = MSVCRT__wfopen(filename, mode);
2996 return *MSVCRT__errno();
3000 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
3001 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
3003 /*********************************************************************
3006 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3013 int res = msvcrt_flush_buffer(file);
3014 return res ? res : c;
3019 return MSVCRT__flsbuf(c, file);
3023 /*********************************************************************
3024 * _flsbuf (MSVCRT.@)
3026 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3028 /* Flush output buffer */
3029 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3030 msvcrt_alloc_buffer(file);
3032 if(!(file->_flag & MSVCRT__IOWRT)) {
3033 if(file->_flag & MSVCRT__IORW) {
3034 file->_flag |= MSVCRT__IOWRT;
3040 int res=msvcrt_flush_buffer(file);
3041 return res?res : MSVCRT_fputc(c, file);
3045 /* set _cnt to 0 for unbuffered FILEs */
3047 len = MSVCRT__write(file->_file, &cc, 1);
3048 if (len == 1) return c & 0xff;
3049 file->_flag |= MSVCRT__IOERR;
3054 /*********************************************************************
3055 * _fputchar (MSVCRT.@)
3057 int CDECL _fputchar(int c)
3059 return MSVCRT_fputc(c, MSVCRT_stdout);
3062 /*********************************************************************
3065 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3066 { MSVCRT_size_t rcnt=size * nmemb;
3067 MSVCRT_size_t read=0;
3073 /* first buffered data */
3075 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3076 memcpy(ptr, file->_ptr, pcnt);
3081 ptr = (char*)ptr + pcnt;
3082 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3083 if(file->_flag & MSVCRT__IORW) {
3084 file->_flag |= MSVCRT__IOREAD;
3091 /* Fill the buffer on small reads.
3092 * TODO: Use a better buffering strategy.
3094 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
3095 if (file->_bufsiz == 0) {
3096 msvcrt_alloc_buffer(file);
3098 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3099 file->_ptr = file->_base;
3100 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3101 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3102 if (i > 0 && i < file->_cnt) {
3103 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3104 file->_flag &= ~MSVCRT__IOEOF;
3107 memcpy(ptr, file->_ptr, i);
3112 i = MSVCRT__read(file->_file,ptr, rcnt);
3116 ptr = (char *)ptr+i;
3117 /* expose feof condition in the flags
3118 * MFC tests file->_flag for feof, and doesn't call feof())
3120 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3121 file->_flag |= MSVCRT__IOEOF;
3124 file->_flag |= MSVCRT__IOERR;
3134 /*********************************************************************
3135 * _wfreopen (MSVCRT.@)
3138 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3140 int open_flags, stream_flags, fd;
3142 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3145 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3149 MSVCRT_fclose(file);
3150 /* map mode string to open() flags. "man fopen" for possibilities. */
3151 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3155 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3158 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
3161 WARN(":failed-last error (%d)\n",GetLastError());
3162 msvcrt_set_errno(GetLastError());
3171 /*********************************************************************
3172 * freopen (MSVCRT.@)
3175 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3178 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3180 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3181 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3187 ret = MSVCRT__wfreopen(pathW, modeW, file);
3194 /*********************************************************************
3195 * fsetpos (MSVCRT.@)
3197 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3199 /* Note that all this has been lifted 'as is' from fseek */
3200 if(file->_flag & MSVCRT__IOWRT)
3201 msvcrt_flush_buffer(file);
3203 /* Discard buffered input */
3205 file->_ptr = file->_base;
3207 /* Reset direction of i/o */
3208 if(file->_flag & MSVCRT__IORW) {
3209 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3212 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3215 /*********************************************************************
3216 * _ftelli64 (MSVCRT.@)
3218 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3220 /* TODO: just call fgetpos and return lower half of result */
3223 pos = _telli64(file->_file);
3224 if(pos == -1) return -1;
3226 if( file->_flag & MSVCRT__IOWRT ) {
3227 off = file->_ptr - file->_base;
3230 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3231 /* Black magic correction for CR removal */
3233 for (i=0; i<file->_cnt; i++) {
3234 if (file->_ptr[i] == '\n')
3237 /* Black magic when reading CR at buffer boundary*/
3238 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3246 /*********************************************************************
3249 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3251 return MSVCRT__ftelli64(file);
3254 /*********************************************************************
3255 * fgetpos (MSVCRT.@)
3257 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3260 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3261 if(*pos == -1) return -1;
3263 if( file->_flag & MSVCRT__IOWRT ) {
3264 off = file->_ptr - file->_base;
3267 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3268 /* Black magic correction for CR removal */
3270 for (i=0; i<file->_cnt; i++) {
3271 if (file->_ptr[i] == '\n')
3274 /* Black magic when reading CR at buffer boundary*/
3275 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3284 /*********************************************************************
3287 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3289 MSVCRT_size_t i, len = strlen(s);
3290 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
3291 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3292 for (i=0; i<len; i++)
3293 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
3298 /*********************************************************************
3301 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3303 MSVCRT_size_t i, len = strlenW(s);
3304 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
3305 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3306 for (i=0; i<len; i++)
3308 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3310 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
3316 /*********************************************************************
3317 * getchar (MSVCRT.@)
3319 int CDECL MSVCRT_getchar(void)
3321 return MSVCRT_fgetc(MSVCRT_stdin);
3324 /*********************************************************************
3327 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3329 return MSVCRT_fgetc(file);
3332 /*********************************************************************
3335 char * CDECL MSVCRT_gets(char *buf)
3338 char * buf_start = buf;
3340 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3341 cc = MSVCRT_fgetc(MSVCRT_stdin))
3342 if(cc != '\r') *buf++ = (char)cc;
3346 TRACE("got '%s'\n", buf_start);
3350 /*********************************************************************
3353 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3356 MSVCRT_wchar_t* ws = buf;
3358 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3359 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3362 *buf++ = (MSVCRT_wchar_t)cc;
3366 TRACE("got %s\n", debugstr_w(ws));
3370 /*********************************************************************
3373 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3375 return MSVCRT_fputc(c, file);
3378 /*********************************************************************
3379 * putchar (MSVCRT.@)
3381 int CDECL MSVCRT_putchar(int c)
3383 return MSVCRT_fputc(c, MSVCRT_stdout);
3386 /*********************************************************************
3387 * _putwch (MSVCRT.@)
3389 int CDECL MSVCRT__putwch(int c)
3391 return MSVCRT_fputwc(c, MSVCRT_stdout);
3394 /*********************************************************************
3397 int CDECL MSVCRT_puts(const char *s)
3399 MSVCRT_size_t len = strlen(s);
3400 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3401 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3404 /*********************************************************************
3407 int CDECL _putws(const MSVCRT_wchar_t *s)
3409 static const MSVCRT_wchar_t nl = '\n';
3410 MSVCRT_size_t len = strlenW(s);
3411 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3412 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3415 /*********************************************************************
3418 int CDECL MSVCRT_remove(const char *path)
3420 TRACE("(%s)\n",path);
3421 if (DeleteFileA(path))
3423 TRACE(":failed (%d)\n",GetLastError());
3424 msvcrt_set_errno(GetLastError());
3428 /*********************************************************************
3429 * _wremove (MSVCRT.@)
3431 int CDECL _wremove(const MSVCRT_wchar_t *path)
3433 TRACE("(%s)\n",debugstr_w(path));
3434 if (DeleteFileW(path))
3436 TRACE(":failed (%d)\n",GetLastError());
3437 msvcrt_set_errno(GetLastError());
3441 /*********************************************************************
3444 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3446 TRACE(":from %s to %s\n",oldpath,newpath);
3447 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3449 TRACE(":failed (%d)\n",GetLastError());
3450 msvcrt_set_errno(GetLastError());
3454 /*********************************************************************
3455 * _wrename (MSVCRT.@)
3457 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3459 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3460 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3462 TRACE(":failed (%d)\n",GetLastError());
3463 msvcrt_set_errno(GetLastError());
3467 /*********************************************************************
3468 * setvbuf (MSVCRT.@)
3470 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3472 /* TODO: Check if file busy */
3474 MSVCRT_free(file->_base);
3478 if(mode == MSVCRT__IOFBF) {
3479 file->_flag &= ~MSVCRT__IONBF;
3480 file->_base = file->_ptr = buf;
3482 file->_bufsiz = size;
3485 file->_flag |= MSVCRT__IONBF;
3490 /*********************************************************************
3493 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3495 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3498 /*********************************************************************
3501 char * CDECL MSVCRT_tmpnam(char *s)
3508 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3509 p = s + sprintf(s, "\\s%s.", tmpstr);
3510 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3512 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
3513 memcpy(p, tmpstr, size);
3514 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3515 GetLastError() == ERROR_FILE_NOT_FOUND)
3521 /*********************************************************************
3522 * _wtmpnam (MSVCRT.@)
3524 MSVCRT_wchar_t * MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
3526 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
3527 MSVCRT_wchar_t tmpstr[16];
3531 s = MSVCRT_wtmpname;
3532 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
3533 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
3534 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3536 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
3537 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
3538 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
3539 GetLastError() == ERROR_FILE_NOT_FOUND)
3545 /*********************************************************************
3546 * tmpfile (MSVCRT.@)
3548 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3550 char *filename = MSVCRT_tmpnam(NULL);
3552 MSVCRT_FILE* file = NULL;
3555 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3556 if (fd != -1 && (file = msvcrt_alloc_fp()))
3558 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3563 else file->_tmpfname = _strdup(filename);
3569 static int puts_clbk_file_a(void *file, int len, const char *str)
3571 return MSVCRT_fwrite(str, sizeof(char), len, file);
3574 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
3576 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
3579 /*********************************************************************
3580 * vfprintf (MSVCRT.@)
3582 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3586 MSVCRT__lock_file(file);
3587 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3588 MSVCRT__unlock_file(file);
3593 /*********************************************************************
3594 * vfprintf_s (MSVCRT.@)
3596 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3600 if(!MSVCRT_CHECK_PMT(file != NULL)) {
3601 *MSVCRT__errno() = MSVCRT_EINVAL;
3605 MSVCRT__lock_file(file);
3606 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3607 MSVCRT__unlock_file(file);
3612 /*********************************************************************
3613 * vfwprintf (MSVCRT.@)
3615 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3619 MSVCRT__lock_file(file);
3620 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3621 MSVCRT__unlock_file(file);
3626 /*********************************************************************
3627 * vfwprintf_s (MSVCRT.@)
3629 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3633 if(!MSVCRT_CHECK_PMT( file != NULL)) {
3634 *MSVCRT__errno() = MSVCRT_EINVAL;
3638 MSVCRT__lock_file(file);
3639 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3640 MSVCRT__unlock_file(file);
3645 /*********************************************************************
3646 * vprintf (MSVCRT.@)
3648 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3650 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3653 /*********************************************************************
3654 * vprintf_s (MSVCRT.@)
3656 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
3658 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
3661 /*********************************************************************
3662 * vwprintf (MSVCRT.@)
3664 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3666 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3669 /*********************************************************************
3670 * vwprintf_s (MSVCRT.@)
3672 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
3674 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
3677 /*********************************************************************
3678 * fprintf (MSVCRT.@)
3680 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3682 __ms_va_list valist;
3684 __ms_va_start(valist, format);
3685 res = MSVCRT_vfprintf(file, format, valist);
3686 __ms_va_end(valist);
3690 /*********************************************************************
3691 * fprintf_s (MSVCRT.@)
3693 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
3695 __ms_va_list valist;
3697 __ms_va_start(valist, format);
3698 res = MSVCRT_vfprintf_s(file, format, valist);
3699 __ms_va_end(valist);
3703 /*********************************************************************
3704 * fwprintf (MSVCRT.@)
3706 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3708 __ms_va_list valist;
3710 __ms_va_start(valist, format);
3711 res = MSVCRT_vfwprintf(file, format, valist);
3712 __ms_va_end(valist);
3716 /*********************************************************************
3717 * fwprintf_s (MSVCRT.@)
3719 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3721 __ms_va_list valist;
3723 __ms_va_start(valist, format);
3724 res = MSVCRT_vfwprintf_s(file, format, valist);
3725 __ms_va_end(valist);
3729 /*********************************************************************
3732 int CDECL MSVCRT_printf(const char *format, ...)
3734 __ms_va_list valist;
3736 __ms_va_start(valist, format);
3737 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3738 __ms_va_end(valist);
3742 /*********************************************************************
3743 * printf_s (MSVCRT.@)
3745 int CDECL MSVCRT_printf_s(const char *format, ...)
3747 __ms_va_list valist;
3749 __ms_va_start(valist, format);
3750 res = MSVCRT_vprintf_s(format, valist);
3751 __ms_va_end(valist);
3755 /*********************************************************************
3758 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3760 if (c == MSVCRT_EOF)
3762 if(file->_bufsiz == 0) {
3763 msvcrt_alloc_buffer(file);
3766 if(file->_ptr>file->_base) {
3770 MSVCRT_clearerr(file);
3776 /*********************************************************************
3777 * ungetwc (MSVCRT.@)
3779 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3781 MSVCRT_wchar_t mwc = wc;
3782 char * pp = (char *)&mwc;
3784 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3785 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3791 /*********************************************************************
3792 * wprintf (MSVCRT.@)
3794 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3796 __ms_va_list valist;
3798 __ms_va_start(valist, format);
3799 res = MSVCRT_vwprintf(format, valist);
3800 __ms_va_end(valist);
3804 /*********************************************************************
3805 * wprintf_s (MSVCRT.@)
3807 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
3809 __ms_va_list valist;
3811 __ms_va_start(valist, format);
3812 res = MSVCRT_vwprintf_s(format, valist);
3813 __ms_va_end(valist);
3817 /*********************************************************************
3818 * _getmaxstdio (MSVCRT.@)
3820 int CDECL _getmaxstdio(void)
3822 return MSVCRT_max_streams;
3825 /*********************************************************************
3826 * _setmaxstdio (MSVCRT.@)
3828 int CDECL _setmaxstdio(int newmax)
3830 TRACE("%d\n", newmax);
3832 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
3835 MSVCRT_max_streams = newmax;
3836 return MSVCRT_max_streams;