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;
112 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
113 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
114 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
115 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
117 #define TOUL(x) (ULONGLONG)(x)
118 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
119 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
120 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
121 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
123 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
124 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
125 * and MSVCRT_stream_idx, from race conditions.
126 * It doesn't protect against race conditions manipulating the underlying files
127 * or flags; doing so would probably be better accomplished with per-file
128 * protection, rather than locking the whole table for every change.
130 static CRITICAL_SECTION MSVCRT_file_cs;
131 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
132 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
134 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
136 buf->st_dev = buf64->st_dev;
137 buf->st_ino = buf64->st_ino;
138 buf->st_mode = buf64->st_mode;
139 buf->st_nlink = buf64->st_nlink;
140 buf->st_uid = buf64->st_uid;
141 buf->st_gid = buf64->st_gid;
142 buf->st_rdev = buf64->st_rdev;
143 buf->st_size = buf64->st_size;
144 buf->st_atime = buf64->st_atime;
145 buf->st_mtime = buf64->st_mtime;
146 buf->st_ctime = buf64->st_ctime;
149 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
151 buf->st_dev = buf64->st_dev;
152 buf->st_ino = buf64->st_ino;
153 buf->st_mode = buf64->st_mode;
154 buf->st_nlink = buf64->st_nlink;
155 buf->st_uid = buf64->st_uid;
156 buf->st_gid = buf64->st_gid;
157 buf->st_rdev = buf64->st_rdev;
158 buf->st_size = buf64->st_size;
159 buf->st_atime = buf64->st_atime;
160 buf->st_mtime = buf64->st_mtime;
161 buf->st_ctime = buf64->st_ctime;
164 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
166 /* 1601 to 1970 is 369 years plus 89 leap days */
167 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
169 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
170 ft->dwHighDateTime = ticks >> 32;
171 ft->dwLowDateTime = ticks;
174 static inline ioinfo* msvcrt_get_ioinfo(int fd)
177 if(fd < MSVCRT_MAX_FILES)
178 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
180 return &MSVCRT___badioinfo;
182 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
185 static inline MSVCRT_FILE* msvcrt_get_file(int i)
189 if(i >= MSVCRT_max_streams)
193 return &MSVCRT__iob[i];
195 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
197 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
198 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
199 ERR("out of memory\n");
200 *MSVCRT__errno() = MSVCRT_ENOMEM;
204 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
206 ret += i%MSVCRT_FD_BLOCK_SIZE;
211 static inline BOOL msvcrt_is_valid_fd(int fd)
213 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
216 /* INTERNAL: Get the HANDLE for a fd
217 * This doesn't lock the table, because a failure will result in
218 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
219 * it returns a valid handle which is about to be closed, a subsequent call
220 * will fail, most likely in a sane way.
222 static HANDLE msvcrt_fdtoh(int fd)
224 if (!msvcrt_is_valid_fd(fd))
226 WARN(":fd (%d) - no handle!\n",fd);
227 *MSVCRT___doserrno() = 0;
228 *MSVCRT__errno() = MSVCRT_EBADF;
229 return INVALID_HANDLE_VALUE;
231 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
232 FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
233 return msvcrt_get_ioinfo(fd)->handle;
236 /* INTERNAL: free a file entry fd */
237 static void msvcrt_free_fd(int fd)
243 fdinfo = msvcrt_get_ioinfo(fd);
244 old_handle = fdinfo->handle;
245 if(fdinfo != &MSVCRT___badioinfo)
247 fdinfo->handle = INVALID_HANDLE_VALUE;
250 TRACE(":fd (%d) freed\n",fd);
251 if (fd < 3) /* don't use 0,1,2 for user files */
256 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
259 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
262 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
268 if (fd == MSVCRT_fdend - 1)
270 if (fd < MSVCRT_fdstart)
276 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
277 /* caller must hold the files lock */
278 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
282 if (fd >= MSVCRT_MAX_FILES)
284 WARN(":files exhausted!\n");
285 *MSVCRT__errno() = MSVCRT_ENFILE;
289 fdinfo = msvcrt_get_ioinfo(fd);
290 if(fdinfo == &MSVCRT___badioinfo) {
293 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
294 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
295 WARN(":out of memory!\n");
296 *MSVCRT__errno() = MSVCRT_ENOMEM;
300 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
301 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
303 fdinfo = msvcrt_get_ioinfo(fd);
306 fdinfo->handle = hand;
307 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
309 /* locate next free slot */
310 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
311 MSVCRT_fdstart = MSVCRT_fdend + 1;
313 while (MSVCRT_fdstart < MSVCRT_fdend &&
314 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
316 /* update last fd in use */
317 if (fd >= MSVCRT_fdend)
318 MSVCRT_fdend = fd + 1;
319 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
323 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
324 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
325 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
331 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
332 static int msvcrt_alloc_fd(HANDLE hand, int flag)
337 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
338 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
343 /* INTERNAL: Allocate a FILE* for an fd slot */
344 /* caller must hold the files lock */
345 static MSVCRT_FILE* msvcrt_alloc_fp(void)
350 for (i = 3; i < MSVCRT_max_streams; i++)
352 file = msvcrt_get_file(i);
356 if (file->_flag == 0)
358 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
366 /* INTERNAL: initialize a FILE* from an open fd */
367 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
369 TRACE(":fd (%d) allocating FILE*\n",fd);
370 if (!msvcrt_is_valid_fd(fd))
372 WARN(":invalid fd %d\n",fd);
373 *MSVCRT___doserrno() = 0;
374 *MSVCRT__errno() = MSVCRT_EBADF;
377 memset(file, 0, sizeof(*file));
379 file->_flag = stream_flags;
381 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
382 InitializeCriticalSection(&((file_crit*)file)->crit);
384 TRACE(":got FILE* (%p)\n",file);
388 /* INTERNAL: Create an inheritance data block (for spawned process)
389 * The inheritance block is made of:
390 * 00 int nb of file descriptor (NBFD)
391 * 04 char file flags (wxflag): repeated for each fd
392 * 4+NBFD HANDLE file handle: repeated for each fd
394 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
401 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
402 *block = MSVCRT_calloc(*size, 1);
408 wxflag_ptr = (char*)*block + sizeof(unsigned);
409 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
411 *(unsigned*)*block = MSVCRT_fdend;
412 for (fd = 0; fd < MSVCRT_fdend; fd++)
414 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
415 fdinfo = msvcrt_get_ioinfo(fd);
416 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
418 *wxflag_ptr = fdinfo->wxflag;
419 *handle_ptr = fdinfo->handle;
424 *handle_ptr = INVALID_HANDLE_VALUE;
426 wxflag_ptr++; handle_ptr++;
431 /* INTERNAL: Set up all file descriptors,
432 * as well as default streams (stdin, stderr and stdout)
434 void msvcrt_init_io(void)
440 InitializeCriticalSection(&MSVCRT_file_cs);
441 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
442 GetStartupInfoA(&si);
443 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
449 count = *(unsigned*)si.lpReserved2;
450 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
451 handle_ptr = (HANDLE*)(wxflag_ptr + count);
453 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
454 count = min(count, MSVCRT_MAX_FILES);
455 for (i = 0; i < count; i++)
457 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
458 msvcrt_alloc_fd_from(*handle_ptr, *wxflag_ptr, i);
460 wxflag_ptr++; handle_ptr++;
462 MSVCRT_fdend = max( 3, count );
463 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
464 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
467 if(!MSVCRT___pioinfo[0])
468 msvcrt_alloc_fd_from(INVALID_HANDLE_VALUE, 0, 3);
470 fdinfo = msvcrt_get_ioinfo(0);
471 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
473 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
474 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
475 GetCurrentProcess(), &fdinfo->handle,
476 0, TRUE, DUPLICATE_SAME_ACCESS))
477 fdinfo->wxflag = WX_OPEN | WX_TEXT;
480 fdinfo = msvcrt_get_ioinfo(1);
481 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
483 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
484 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
485 GetCurrentProcess(), &fdinfo->handle,
486 0, TRUE, DUPLICATE_SAME_ACCESS))
487 fdinfo->wxflag = WX_OPEN | WX_TEXT;
490 fdinfo = msvcrt_get_ioinfo(2);
491 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
493 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
494 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
495 GetCurrentProcess(), &fdinfo->handle,
496 0, TRUE, DUPLICATE_SAME_ACCESS))
497 fdinfo->wxflag = WX_OPEN | WX_TEXT;
500 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(0)->handle,
501 msvcrt_get_ioinfo(1)->handle, msvcrt_get_ioinfo(2)->handle);
503 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
504 for (i = 0; i < 3; i++)
506 /* FILE structs for stdin/out/err are static and never deleted */
507 MSVCRT__iob[i]._file = i;
508 MSVCRT__iob[i]._tmpfname = NULL;
509 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
511 MSVCRT_stream_idx = 3;
514 /* INTERNAL: Flush stdio file buffer */
515 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
518 int cnt=file->_ptr-file->_base;
519 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
520 file->_flag |= MSVCRT__IOERR;
523 file->_ptr=file->_base;
524 file->_cnt=file->_bufsiz;
529 /* INTERNAL: Allocate stdio file buffer */
530 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
532 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
534 file->_bufsiz = MSVCRT_BUFSIZ;
535 file->_flag |= MSVCRT__IOMYBUF;
537 file->_base = (char*)(&file->_charbuf);
539 file->_bufsiz = sizeof(file->_charbuf);
541 file->_ptr = file->_base;
545 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
546 static int msvcrt_int_to_base32(int num, char *str)
561 *p = (num & 31) + '0';
563 *p += ('a' - '0' - 10);
570 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
571 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
586 *p = (num & 31) + '0';
588 *p += ('a' - '0' - 10);
595 /*********************************************************************
596 * __iob_func(MSVCRT.@)
598 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
600 return &MSVCRT__iob[0];
603 /*********************************************************************
606 int CDECL MSVCRT__access(const char *filename, int mode)
608 DWORD attr = GetFileAttributesA(filename);
610 TRACE("(%s,%d) %d\n",filename,mode,attr);
612 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
614 msvcrt_set_errno(GetLastError());
617 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
619 msvcrt_set_errno(ERROR_ACCESS_DENIED);
625 /*********************************************************************
626 * _access_s (MSVCRT.@)
628 int CDECL _access_s(const char *filename, int mode)
630 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
631 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
633 *MSVCRT__errno() = MSVCRT_EINVAL;
637 return MSVCRT__access(filename, mode);
640 /*********************************************************************
641 * _waccess (MSVCRT.@)
643 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
645 DWORD attr = GetFileAttributesW(filename);
647 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
649 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
651 msvcrt_set_errno(GetLastError());
654 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
656 msvcrt_set_errno(ERROR_ACCESS_DENIED);
662 /*********************************************************************
663 * _waccess_s (MSVCRT.@)
665 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
667 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
668 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
670 *MSVCRT__errno() = MSVCRT_EINVAL;
674 return _waccess(filename, mode);
677 /*********************************************************************
680 int CDECL MSVCRT__chmod(const char *path, int flags)
682 DWORD oldFlags = GetFileAttributesA(path);
684 if (oldFlags != INVALID_FILE_ATTRIBUTES)
686 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
687 oldFlags | FILE_ATTRIBUTE_READONLY;
689 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
692 msvcrt_set_errno(GetLastError());
696 /*********************************************************************
699 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
701 DWORD oldFlags = GetFileAttributesW(path);
703 if (oldFlags != INVALID_FILE_ATTRIBUTES)
705 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
706 oldFlags | FILE_ATTRIBUTE_READONLY;
708 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
711 msvcrt_set_errno(GetLastError());
715 /*********************************************************************
718 int CDECL MSVCRT__unlink(const char *path)
720 TRACE("%s\n",debugstr_a(path));
721 if(DeleteFileA(path))
723 TRACE("failed (%d)\n",GetLastError());
724 msvcrt_set_errno(GetLastError());
728 /*********************************************************************
729 * _wunlink (MSVCRT.@)
731 int CDECL _wunlink(const MSVCRT_wchar_t *path)
733 TRACE("(%s)\n",debugstr_w(path));
734 if(DeleteFileW(path))
736 TRACE("failed (%d)\n",GetLastError());
737 msvcrt_set_errno(GetLastError());
741 /* _flushall calls MSVCRT_fflush which calls _flushall */
742 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
744 /*********************************************************************
745 * _flushall (MSVCRT.@)
747 int CDECL _flushall(void)
749 int i, num_flushed = 0;
753 for (i = 3; i < MSVCRT_stream_idx; i++) {
754 file = msvcrt_get_file(i);
758 if(file->_flag & MSVCRT__IOWRT) {
766 TRACE(":flushed (%d) handles\n",num_flushed);
770 /*********************************************************************
773 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
777 } else if(file->_flag & MSVCRT__IOWRT) {
780 MSVCRT__lock_file(file);
781 res = msvcrt_flush_buffer(file);
782 MSVCRT__unlock_file(file);
789 /*********************************************************************
792 int CDECL MSVCRT__close(int fd)
798 hand = msvcrt_fdtoh(fd);
799 TRACE(":fd (%d) handle (%p)\n",fd,hand);
800 if (hand == INVALID_HANDLE_VALUE)
802 else if (!CloseHandle(hand))
804 WARN(":failed-last error (%d)\n",GetLastError());
805 msvcrt_set_errno(GetLastError());
818 /*********************************************************************
821 int CDECL _commit(int fd)
823 HANDLE hand = msvcrt_fdtoh(fd);
825 TRACE(":fd (%d) handle (%p)\n",fd,hand);
826 if (hand == INVALID_HANDLE_VALUE)
829 if (!FlushFileBuffers(hand))
831 if (GetLastError() == ERROR_INVALID_HANDLE)
833 /* FlushFileBuffers fails for console handles
834 * so we ignore this error.
838 TRACE(":failed-last error (%d)\n",GetLastError());
839 msvcrt_set_errno(GetLastError());
846 /*********************************************************************
849 * MSDN isn't clear on this point, but the remarks for _pipe
850 * indicate file descriptors duplicated with _dup and _dup2 are always
853 int CDECL MSVCRT__dup2(int od, int nd)
857 TRACE("(od=%d, nd=%d)\n", od, nd);
859 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
863 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
864 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
866 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
868 if (msvcrt_is_valid_fd(nd))
870 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
874 *MSVCRT__errno() = MSVCRT_EMFILE;
878 /* _dup2 returns 0, not nd, on success */
885 msvcrt_set_errno(GetLastError());
890 *MSVCRT__errno() = MSVCRT_EBADF;
897 /*********************************************************************
900 int CDECL MSVCRT__dup(int od)
906 if (MSVCRT__dup2(od, fd) == 0)
914 /*********************************************************************
917 int CDECL _eof(int fd)
920 LONG hcurpos,hendpos;
921 HANDLE hand = msvcrt_fdtoh(fd);
923 TRACE(":fd (%d) handle (%p)\n",fd,hand);
925 if (hand == INVALID_HANDLE_VALUE)
928 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
930 /* Otherwise we do it the hard way */
931 hcurpos = hendpos = 0;
932 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
933 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
935 if (curpos == endpos && hcurpos == hendpos)
937 /* FIXME: shouldn't WX_ATEOF be set here? */
941 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
945 /*********************************************************************
946 * _fcloseall (MSVCRT.@)
948 int CDECL MSVCRT__fcloseall(void)
950 int num_closed = 0, i;
954 for (i = 3; i < MSVCRT_stream_idx; i++) {
955 file = msvcrt_get_file(i);
957 if (file->_flag && !MSVCRT_fclose(file))
962 TRACE(":closed (%d) handles\n",num_closed);
966 /* free everything on process exit */
967 void msvcrt_free_io(void)
972 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
973 * stdout, and stderr (unlike GNU), so we need to fclose() them here
974 * or they won't get flushed.
976 MSVCRT_fclose(&MSVCRT__iob[0]);
977 MSVCRT_fclose(&MSVCRT__iob[1]);
978 MSVCRT_fclose(&MSVCRT__iob[2]);
980 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
981 MSVCRT_free(MSVCRT___pioinfo[i]);
983 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
984 MSVCRT_free(MSVCRT_fstream[i]);
986 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
987 DeleteCriticalSection(&MSVCRT_file_cs);
990 /*********************************************************************
991 * _lseeki64 (MSVCRT.@)
993 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
995 HANDLE hand = msvcrt_fdtoh(fd);
998 TRACE(":fd (%d) handle (%p)\n",fd,hand);
999 if (hand == INVALID_HANDLE_VALUE)
1002 if (whence < 0 || whence > 2)
1004 *MSVCRT__errno() = MSVCRT_EINVAL;
1008 TRACE(":fd (%d) to %s pos %s\n",
1009 fd,wine_dbgstr_longlong(offset),
1010 (whence==SEEK_SET)?"SEEK_SET":
1011 (whence==SEEK_CUR)?"SEEK_CUR":
1012 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1014 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1015 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1016 ofs.QuadPart = offset;
1017 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1018 GetLastError() == ERROR_SUCCESS)
1020 msvcrt_get_ioinfo(fd)->wxflag &= ~(WX_ATEOF|WX_READEOF);
1021 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1023 return ofs.QuadPart;
1025 TRACE(":error-last error (%d)\n",GetLastError());
1026 msvcrt_set_errno(GetLastError());
1030 /*********************************************************************
1033 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1035 return MSVCRT__lseeki64(fd, offset, whence);
1038 /*********************************************************************
1039 * _lock_file (MSVCRT.@)
1041 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1043 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1044 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1046 EnterCriticalSection(&((file_crit*)file)->crit);
1049 /*********************************************************************
1050 * _unlock_file (MSVCRT.@)
1052 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1054 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1055 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1057 LeaveCriticalSection(&((file_crit*)file)->crit);
1060 /*********************************************************************
1061 * _locking (MSVCRT.@)
1063 * This is untested; the underlying LockFile doesn't work yet.
1065 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1069 HANDLE hand = msvcrt_fdtoh(fd);
1071 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1072 if (hand == INVALID_HANDLE_VALUE)
1075 if (mode < 0 || mode > 4)
1077 *MSVCRT__errno() = MSVCRT_EINVAL;
1081 TRACE(":fd (%d) by 0x%08x mode %s\n",
1082 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1083 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1084 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1085 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1086 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1089 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1091 FIXME ("Seek failed\n");
1092 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1095 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1098 ret = 1; /* just to satisfy gcc */
1101 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1106 else if (mode == MSVCRT__LK_UNLCK)
1107 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1109 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1110 /* FIXME - what about error settings? */
1111 return ret ? 0 : -1;
1114 /*********************************************************************
1115 * _fseeki64 (MSVCRT.@)
1117 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1121 MSVCRT__lock_file(file);
1122 /* Flush output if needed */
1123 if(file->_flag & MSVCRT__IOWRT)
1124 msvcrt_flush_buffer(file);
1126 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1127 offset -= file->_cnt;
1128 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
1129 /* Black magic correction for CR removal */
1131 for (i=0; i<file->_cnt; i++) {
1132 if (file->_ptr[i] == '\n')
1135 /* Black magic when reading CR at buffer boundary*/
1136 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
1140 /* Discard buffered input */
1142 file->_ptr = file->_base;
1143 /* Reset direction of i/o */
1144 if(file->_flag & MSVCRT__IORW) {
1145 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1147 /* Clear end of file flag */
1148 file->_flag &= ~MSVCRT__IOEOF;
1149 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1151 MSVCRT__unlock_file(file);
1155 /*********************************************************************
1158 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1160 return MSVCRT__fseeki64( file, offset, whence );
1163 /*********************************************************************
1164 * _chsize (MSVCRT.@)
1166 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1172 TRACE("(fd=%d, size=%d)\n", fd, size);
1176 handle = msvcrt_fdtoh(fd);
1177 if (handle != INVALID_HANDLE_VALUE)
1179 /* save the current file pointer */
1180 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1183 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1186 ret = SetEndOfFile(handle);
1187 if (!ret) msvcrt_set_errno(GetLastError());
1190 /* restore the file pointer */
1191 MSVCRT__lseek(fd, cur, SEEK_SET);
1196 return ret ? 0 : -1;
1199 /*********************************************************************
1200 * clearerr (MSVCRT.@)
1202 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1204 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1206 MSVCRT__lock_file(file);
1207 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1208 MSVCRT__unlock_file(file);
1211 /*********************************************************************
1214 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1216 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1218 MSVCRT__lock_file(file);
1219 MSVCRT_fseek(file, 0L, SEEK_SET);
1220 MSVCRT_clearerr(file);
1221 MSVCRT__unlock_file(file);
1224 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1226 int plus = strchrW(mode, '+') != NULL;
1231 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1232 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1235 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1236 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1239 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1240 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1243 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1244 *MSVCRT__errno() = MSVCRT_EINVAL;
1252 *open_flags |= MSVCRT__O_BINARY;
1253 *open_flags &= ~MSVCRT__O_TEXT;
1256 *open_flags |= MSVCRT__O_TEXT;
1257 *open_flags &= ~MSVCRT__O_BINARY;
1263 FIXME(":unknown flag %c not supported\n",mode[-1]);
1268 /*********************************************************************
1269 * _fdopen (MSVCRT.@)
1271 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1274 MSVCRT_wchar_t *modeW = NULL;
1276 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1278 ret = MSVCRT__wfdopen(fd, modeW);
1284 /*********************************************************************
1285 * _wfdopen (MSVCRT.@)
1287 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1289 int open_flags, stream_flags;
1292 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1295 if (!(file = msvcrt_alloc_fp()))
1297 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1302 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1308 /*********************************************************************
1309 * _filelength (MSVCRT.@)
1311 LONG CDECL MSVCRT__filelength(int fd)
1313 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1316 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1319 if (endPos != curPos)
1320 MSVCRT__lseek(fd, curPos, SEEK_SET);
1327 /*********************************************************************
1328 * _filelengthi64 (MSVCRT.@)
1330 __int64 CDECL MSVCRT__filelengthi64(int fd)
1332 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1335 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1338 if (endPos != curPos)
1339 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1346 /*********************************************************************
1347 * _fileno (MSVCRT.@)
1349 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1353 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1355 MSVCRT__lock_file(file);
1357 MSVCRT__unlock_file(file);
1362 /*********************************************************************
1363 * _fstat64 (MSVCRT.@)
1365 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1369 BY_HANDLE_FILE_INFORMATION hfi;
1370 HANDLE hand = msvcrt_fdtoh(fd);
1372 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1373 if (hand == INVALID_HANDLE_VALUE)
1378 WARN(":failed-NULL buf\n");
1379 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1383 memset(&hfi, 0, sizeof(hfi));
1384 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1385 type = GetFileType(hand);
1386 if (type == FILE_TYPE_PIPE)
1388 buf->st_dev = buf->st_rdev = fd;
1389 buf->st_mode = S_IFIFO;
1392 else if (type == FILE_TYPE_CHAR)
1394 buf->st_dev = buf->st_rdev = fd;
1395 buf->st_mode = S_IFCHR;
1398 else /* FILE_TYPE_DISK etc. */
1400 if (!GetFileInformationByHandle(hand, &hfi))
1402 WARN(":failed-last error (%d)\n",GetLastError());
1403 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1406 buf->st_mode = S_IFREG | 0444;
1407 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1408 buf->st_mode |= 0222;
1409 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1410 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1412 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1413 buf->st_mtime = buf->st_ctime = dw;
1414 buf->st_nlink = hfi.nNumberOfLinks;
1416 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1421 /*********************************************************************
1422 * _fstati64 (MSVCRT.@)
1424 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1427 struct MSVCRT__stat64 buf64;
1429 ret = MSVCRT__fstat64(fd, &buf64);
1431 msvcrt_stat64_to_stati64(&buf64, buf);
1435 /*********************************************************************
1438 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1440 struct MSVCRT__stat64 buf64;
1442 ret = MSVCRT__fstat64(fd, &buf64);
1444 msvcrt_stat64_to_stat(&buf64, buf);
1448 /*********************************************************************
1449 * _futime64 (MSVCRT.@)
1451 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1453 HANDLE hand = msvcrt_fdtoh(fd);
1458 time_to_filetime( MSVCRT__time64(NULL), &at );
1463 time_to_filetime( t->actime, &at );
1464 time_to_filetime( t->modtime, &wt );
1467 if (!SetFileTime(hand, NULL, &at, &wt))
1469 msvcrt_set_errno(GetLastError());
1475 /*********************************************************************
1476 * _futime32 (MSVCRT.@)
1478 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1480 struct MSVCRT___utimbuf64 t64;
1481 t64.actime = t->actime;
1482 t64.modtime = t->modtime;
1483 return _futime64( fd, &t64 );
1486 /*********************************************************************
1487 * _futime (MSVCRT.@)
1490 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1492 return _futime64( fd, t );
1495 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1497 return _futime32( fd, t );
1501 /*********************************************************************
1502 * _get_osfhandle (MSVCRT.@)
1504 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1506 HANDLE hand = msvcrt_fdtoh(fd);
1507 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1509 return (MSVCRT_intptr_t)hand;
1512 /*********************************************************************
1513 * _isatty (MSVCRT.@)
1515 int CDECL _isatty(int fd)
1517 HANDLE hand = msvcrt_fdtoh(fd);
1519 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1520 if (hand == INVALID_HANDLE_VALUE)
1523 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1526 /*********************************************************************
1527 * _mktemp (MSVCRT.@)
1529 char * CDECL _mktemp(char *pattern)
1532 char *retVal = pattern;
1537 numX = (*pattern++ == 'X')? numX + 1 : 0;
1541 id = GetCurrentProcessId();
1545 int tempNum = id / 10;
1546 *pattern-- = id - (tempNum * 10) + '0';
1552 *pattern = letter++;
1553 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1554 GetLastError() == ERROR_FILE_NOT_FOUND)
1556 } while(letter <= 'z');
1560 /*********************************************************************
1561 * _wmktemp (MSVCRT.@)
1563 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1566 MSVCRT_wchar_t *retVal = pattern;
1568 MSVCRT_wchar_t letter = 'a';
1571 numX = (*pattern++ == 'X')? numX + 1 : 0;
1575 id = GetCurrentProcessId();
1579 int tempNum = id / 10;
1580 *pattern-- = id - (tempNum * 10) + '0';
1586 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1587 GetLastError() == ERROR_FILE_NOT_FOUND)
1589 *pattern = letter++;
1590 } while(letter != '|');
1594 static unsigned split_oflags(unsigned oflags)
1597 unsigned unsupp; /* until we support everything */
1599 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1600 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1601 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1602 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1603 else wxflags |= WX_TEXT; /* default to TEXT*/
1604 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1606 if ((unsupp = oflags & ~(
1607 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1608 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1609 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1610 MSVCRT__O_NOINHERIT|
1611 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1613 ERR(":unsupported oflags 0x%04x\n",unsupp);
1618 /*********************************************************************
1621 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1624 SECURITY_ATTRIBUTES sa;
1625 HANDLE readHandle, writeHandle;
1629 *MSVCRT__errno() = MSVCRT_EINVAL;
1633 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1634 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1635 sa.lpSecurityDescriptor = NULL;
1636 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1638 unsigned int wxflags = split_oflags(textmode);
1642 fd = msvcrt_alloc_fd(readHandle, wxflags);
1646 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1654 MSVCRT__close(pfds[0]);
1655 CloseHandle(writeHandle);
1656 *MSVCRT__errno() = MSVCRT_EMFILE;
1661 CloseHandle(readHandle);
1662 CloseHandle(writeHandle);
1663 *MSVCRT__errno() = MSVCRT_EMFILE;
1668 msvcrt_set_errno(GetLastError());
1673 /*********************************************************************
1674 * _sopen_s (MSVCRT.@)
1676 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1678 DWORD access = 0, creation = 0, attrib;
1682 SECURITY_ATTRIBUTES sa;
1684 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1685 fd, path, oflags, shflags, pmode);
1689 MSVCRT_INVALID_PMT("null out fd pointer");
1690 *MSVCRT__errno() = MSVCRT_EINVAL;
1691 return MSVCRT_EINVAL;
1695 wxflag = split_oflags(oflags);
1696 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1698 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1699 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1700 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1703 if (oflags & MSVCRT__O_CREAT)
1705 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1706 FIXME(": pmode 0x%04x ignored\n", pmode);
1708 WARN(": pmode 0x%04x ignored\n", pmode);
1710 if (oflags & MSVCRT__O_EXCL)
1711 creation = CREATE_NEW;
1712 else if (oflags & MSVCRT__O_TRUNC)
1713 creation = CREATE_ALWAYS;
1715 creation = OPEN_ALWAYS;
1717 else /* no MSVCRT__O_CREAT */
1719 if (oflags & MSVCRT__O_TRUNC)
1720 creation = TRUNCATE_EXISTING;
1722 creation = OPEN_EXISTING;
1727 case MSVCRT__SH_DENYRW:
1730 case MSVCRT__SH_DENYWR:
1731 sharing = FILE_SHARE_READ;
1733 case MSVCRT__SH_DENYRD:
1734 sharing = FILE_SHARE_WRITE;
1736 case MSVCRT__SH_DENYNO:
1737 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1740 ERR( "Unhandled shflags 0x%x\n", shflags );
1741 return MSVCRT_EINVAL;
1743 attrib = FILE_ATTRIBUTE_NORMAL;
1745 if (oflags & MSVCRT__O_TEMPORARY)
1747 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1749 sharing |= FILE_SHARE_DELETE;
1752 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1753 sa.lpSecurityDescriptor = NULL;
1754 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1756 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1757 if (hand == INVALID_HANDLE_VALUE) {
1758 WARN(":failed-last error (%d)\n", GetLastError());
1759 msvcrt_set_errno(GetLastError());
1760 return *MSVCRT__errno();
1763 *fd = msvcrt_alloc_fd(hand, wxflag);
1765 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1769 /*********************************************************************
1772 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1777 if (oflags & MSVCRT__O_CREAT)
1781 __ms_va_start(ap, shflags);
1782 pmode = va_arg(ap, int);
1788 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
1792 /*********************************************************************
1793 * _wsopen_s (MSVCRT.@)
1795 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1797 DWORD access = 0, creation = 0, attrib;
1798 SECURITY_ATTRIBUTES sa;
1803 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1804 fd, debugstr_w(path), oflags, shflags, pmode);
1808 MSVCRT_INVALID_PMT("null out fd pointer");
1809 *MSVCRT__errno() = MSVCRT_EINVAL;
1810 return MSVCRT_EINVAL;
1814 wxflag = split_oflags(oflags);
1815 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1817 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1818 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1819 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1822 if (oflags & MSVCRT__O_CREAT)
1824 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1825 FIXME(": pmode 0x%04x ignored\n", pmode);
1827 WARN(": pmode 0x%04x ignored\n", pmode);
1829 if (oflags & MSVCRT__O_EXCL)
1830 creation = CREATE_NEW;
1831 else if (oflags & MSVCRT__O_TRUNC)
1832 creation = CREATE_ALWAYS;
1834 creation = OPEN_ALWAYS;
1836 else /* no MSVCRT__O_CREAT */
1838 if (oflags & MSVCRT__O_TRUNC)
1839 creation = TRUNCATE_EXISTING;
1841 creation = OPEN_EXISTING;
1846 case MSVCRT__SH_DENYRW:
1849 case MSVCRT__SH_DENYWR:
1850 sharing = FILE_SHARE_READ;
1852 case MSVCRT__SH_DENYRD:
1853 sharing = FILE_SHARE_WRITE;
1855 case MSVCRT__SH_DENYNO:
1856 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1859 ERR( "Unhandled shflags 0x%x\n", shflags );
1860 return MSVCRT_EINVAL;
1862 attrib = FILE_ATTRIBUTE_NORMAL;
1864 if (oflags & MSVCRT__O_TEMPORARY)
1866 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1868 sharing |= FILE_SHARE_DELETE;
1871 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1872 sa.lpSecurityDescriptor = NULL;
1873 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1875 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1877 if (hand == INVALID_HANDLE_VALUE) {
1878 WARN(":failed-last error (%d)\n",GetLastError());
1879 msvcrt_set_errno(GetLastError());
1880 msvcrt_set_errno(GetLastError());
1881 return *MSVCRT__errno();
1884 *fd = msvcrt_alloc_fd(hand, wxflag);
1886 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1890 /*********************************************************************
1891 * _wsopen (MSVCRT.@)
1893 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1898 if (oflags & MSVCRT__O_CREAT)
1902 __ms_va_start(ap, shflags);
1903 pmode = va_arg(ap, int);
1909 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1913 /*********************************************************************
1916 int CDECL MSVCRT__open( const char *path, int flags, ... )
1920 if (flags & MSVCRT__O_CREAT)
1923 __ms_va_start(ap, flags);
1924 pmode = va_arg(ap, int);
1926 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1929 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1932 /*********************************************************************
1935 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1939 if (flags & MSVCRT__O_CREAT)
1942 __ms_va_start(ap, flags);
1943 pmode = va_arg(ap, int);
1945 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1948 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1951 /*********************************************************************
1954 int CDECL MSVCRT__creat(const char *path, int flags)
1956 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1957 return MSVCRT__open(path, usedFlags);
1960 /*********************************************************************
1961 * _wcreat (MSVCRT.@)
1963 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1965 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1966 return _wopen(path, usedFlags);
1969 /*********************************************************************
1970 * _open_osfhandle (MSVCRT.@)
1972 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1976 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1977 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1978 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1979 * text - it never sets MSVCRT__O_BINARY.
1981 /* don't let split_oflags() decide the mode if no mode is passed */
1982 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1983 oflags |= MSVCRT__O_BINARY;
1985 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1986 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1990 /*********************************************************************
1993 int CDECL _rmtmp(void)
1995 int num_removed = 0, i;
1999 for (i = 3; i < MSVCRT_stream_idx; i++) {
2000 file = msvcrt_get_file(i);
2002 if (file->_tmpfname)
2004 MSVCRT_fclose(file);
2011 TRACE(":removed (%d) temp files\n",num_removed);
2015 /*********************************************************************
2018 * When reading \r as last character in text mode, read() positions
2019 * the file pointer on the \r character while getc() goes on to
2022 static int read_i(int fd, void *buf, unsigned int count)
2025 char *bufstart = buf;
2026 HANDLE hand = msvcrt_fdtoh(fd);
2027 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2032 if (fdinfo->wxflag & WX_READEOF) {
2033 fdinfo->wxflag |= WX_ATEOF;
2034 TRACE("already at EOF, returning 0\n");
2037 /* Don't trace small reads, it gets *very* annoying */
2039 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2040 if (hand == INVALID_HANDLE_VALUE)
2043 /* Reading single bytes in O_TEXT mode makes things slow
2044 * So read big chunks
2046 if (ReadFile(hand, bufstart, count, &num_read, NULL))
2048 if (count != 0 && num_read == 0)
2050 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2051 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2053 else if (fdinfo->wxflag & WX_TEXT)
2056 if (bufstart[num_read-1] == '\r')
2060 fdinfo->wxflag &= ~WX_READCR;
2061 ReadFile(hand, bufstart, 1, &num_read, NULL);
2065 fdinfo->wxflag |= WX_READCR;
2070 fdinfo->wxflag &= ~WX_READCR;
2071 for (i=0, j=0; i<num_read; i++)
2073 /* in text mode, a ctrl-z signals EOF */
2074 if (bufstart[i] == 0x1a)
2076 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2077 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2080 /* in text mode, strip \r if followed by \n.
2081 * BUG: should save state across calls somehow, so CR LF that
2082 * straddles buffer boundary gets recognized properly?
2084 if ((bufstart[i] != '\r')
2085 || ((i+1) < num_read && bufstart[i+1] != '\n'))
2086 bufstart[j++] = bufstart[i];
2093 if (GetLastError() == ERROR_BROKEN_PIPE)
2095 TRACE(":end-of-pipe\n");
2096 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2101 TRACE(":failed-last error (%d)\n",GetLastError());
2107 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2111 /*********************************************************************
2114 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2117 num_read = read_i(fd, buf, count);
2121 /*********************************************************************
2122 * _setmode (MSVCRT.@)
2124 int CDECL _setmode(int fd,int mode)
2126 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2127 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
2128 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
2129 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
2130 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2132 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2136 /*********************************************************************
2137 * _stat64 (MSVCRT.@)
2139 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2142 WIN32_FILE_ATTRIBUTE_DATA hfi;
2143 unsigned short mode = ALL_S_IREAD;
2146 TRACE(":file (%s) buf(%p)\n",path,buf);
2148 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2150 TRACE("failed (%d)\n",GetLastError());
2151 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2155 memset(buf,0,sizeof(struct MSVCRT__stat64));
2157 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2158 Bon 011120: This FIXME seems incorrect
2159 Also a letter as first char isn't enough to be classified
2162 if (isalpha(*path)&& (*(path+1)==':'))
2163 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2165 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2167 plen = strlen(path);
2169 /* Dir, or regular file? */
2170 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2171 (path[plen-1] == '\\'))
2172 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2175 mode |= MSVCRT__S_IFREG;
2177 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2179 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2180 (tolower(path[plen-3]) << 16);
2181 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2182 mode |= ALL_S_IEXEC;
2186 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2187 mode |= ALL_S_IWRITE;
2189 buf->st_mode = mode;
2191 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2192 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2194 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2195 buf->st_mtime = buf->st_ctime = dw;
2196 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2197 (int)(buf->st_size >> 32),(int)buf->st_size,
2198 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2202 /*********************************************************************
2203 * _stati64 (MSVCRT.@)
2205 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2208 struct MSVCRT__stat64 buf64;
2210 ret = MSVCRT_stat64(path, &buf64);
2212 msvcrt_stat64_to_stati64(&buf64, buf);
2216 /*********************************************************************
2219 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2221 struct MSVCRT__stat64 buf64;
2223 ret = MSVCRT_stat64( path, &buf64);
2225 msvcrt_stat64_to_stat(&buf64, buf);
2229 /*********************************************************************
2230 * _wstat64 (MSVCRT.@)
2232 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2235 WIN32_FILE_ATTRIBUTE_DATA hfi;
2236 unsigned short mode = ALL_S_IREAD;
2239 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2241 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2243 TRACE("failed (%d)\n",GetLastError());
2244 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2248 memset(buf,0,sizeof(struct MSVCRT__stat64));
2250 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2251 if (MSVCRT_iswalpha(*path))
2252 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2254 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2256 plen = strlenW(path);
2258 /* Dir, or regular file? */
2259 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2260 (path[plen-1] == '\\'))
2261 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2264 mode |= MSVCRT__S_IFREG;
2266 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2268 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2269 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2270 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2271 mode |= ALL_S_IEXEC;
2275 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2276 mode |= ALL_S_IWRITE;
2278 buf->st_mode = mode;
2280 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2281 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2283 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2284 buf->st_mtime = buf->st_ctime = dw;
2285 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2286 (int)(buf->st_size >> 32),(int)buf->st_size,
2287 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2291 /*********************************************************************
2292 * _wstati64 (MSVCRT.@)
2294 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2297 struct MSVCRT__stat64 buf64;
2299 ret = MSVCRT__wstat64(path, &buf64);
2301 msvcrt_stat64_to_stati64(&buf64, buf);
2305 /*********************************************************************
2308 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2311 struct MSVCRT__stat64 buf64;
2313 ret = MSVCRT__wstat64( path, &buf64 );
2314 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2318 /*********************************************************************
2321 MSVCRT_long CDECL MSVCRT__tell(int fd)
2323 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2326 /*********************************************************************
2327 * _telli64 (MSVCRT.@)
2329 __int64 CDECL _telli64(int fd)
2331 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2334 /*********************************************************************
2335 * _tempnam (MSVCRT.@)
2337 char * CDECL _tempnam(const char *dir, const char *prefix)
2339 char tmpbuf[MAX_PATH];
2340 const char *tmp_dir = MSVCRT_getenv("TMP");
2342 if (tmp_dir) dir = tmp_dir;
2344 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2345 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2347 TRACE("got name (%s)\n",tmpbuf);
2348 DeleteFileA(tmpbuf);
2349 return _strdup(tmpbuf);
2351 TRACE("failed (%d)\n",GetLastError());
2355 /*********************************************************************
2356 * _wtempnam (MSVCRT.@)
2358 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2360 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2362 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2363 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2365 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2366 DeleteFileW(tmpbuf);
2367 return _wcsdup(tmpbuf);
2369 TRACE("failed (%d)\n",GetLastError());
2373 /*********************************************************************
2376 int CDECL MSVCRT__umask(int umask)
2378 int old_umask = MSVCRT_umask;
2379 TRACE("(%d)\n",umask);
2380 MSVCRT_umask = umask;
2384 /*********************************************************************
2385 * _utime64 (MSVCRT.@)
2387 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2389 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2393 int retVal = _futime64(fd, t);
2400 /*********************************************************************
2401 * _utime32 (MSVCRT.@)
2403 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2405 struct MSVCRT___utimbuf64 t64;
2406 t64.actime = t->actime;
2407 t64.modtime = t->modtime;
2408 return _utime64( path, &t64 );
2411 /*********************************************************************
2415 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2417 return _utime64( path, t );
2420 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2422 return _utime32( path, t );
2426 /*********************************************************************
2427 * _wutime64 (MSVCRT.@)
2429 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2431 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2435 int retVal = _futime64(fd, t);
2442 /*********************************************************************
2443 * _wutime32 (MSVCRT.@)
2445 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2447 struct MSVCRT___utimbuf64 t64;
2448 t64.actime = t->actime;
2449 t64.modtime = t->modtime;
2450 return _wutime64( path, &t64 );
2453 /*********************************************************************
2454 * _wutime (MSVCRT.@)
2457 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2459 return _wutime64( path, t );
2462 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2464 return _wutime32( path, t );
2468 /*********************************************************************
2471 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2474 HANDLE hand = msvcrt_fdtoh(fd);
2476 /* Don't trace small writes, it gets *very* annoying */
2479 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2481 if (hand == INVALID_HANDLE_VALUE)
2483 *MSVCRT__errno() = MSVCRT_EBADF;
2487 /* If appending, go to EOF */
2488 if (msvcrt_get_ioinfo(fd)->wxflag & WX_APPEND)
2489 MSVCRT__lseek(fd, 0, FILE_END);
2491 if (!(msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT))
2493 if (WriteFile(hand, buf, count, &num_written, NULL)
2494 && (num_written == count))
2496 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2497 hand, GetLastError());
2498 *MSVCRT__errno() = MSVCRT_ENOSPC;
2502 unsigned int i, j, nr_lf;
2505 const char *s = buf, *buf_start = buf;
2506 /* find number of \n ( without preceding \r ) */
2507 for ( nr_lf=0,i = 0; i <count; i++)
2512 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2517 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2519 for (s = buf, i = 0, j = 0; i < count; i++)
2524 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2531 FIXME("Malloc failed\n");
2539 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2541 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2542 fd, hand, GetLastError(), num_written);
2543 *MSVCRT__errno() = MSVCRT_ENOSPC;
2546 return s - buf_start;
2558 /*********************************************************************
2561 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2565 MSVCRT__lock_file(file);
2566 len = MSVCRT__write(file->_file, &val, sizeof(val));
2567 if (len == sizeof(val)) {
2568 MSVCRT__unlock_file(file);
2572 file->_flag |= MSVCRT__IOERR;
2573 MSVCRT__unlock_file(file);
2577 /*********************************************************************
2580 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2584 MSVCRT__lock_file(file);
2586 MSVCRT_free(file->_tmpfname);
2587 file->_tmpfname = NULL;
2588 /* flush stdio buffers */
2589 if(file->_flag & MSVCRT__IOWRT)
2590 MSVCRT_fflush(file);
2591 if(file->_flag & MSVCRT__IOMYBUF)
2592 MSVCRT_free(file->_base);
2594 r=MSVCRT__close(file->_file);
2597 MSVCRT__unlock_file(file);
2598 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
2599 DeleteCriticalSection(&((file_crit*)file)->crit);
2601 if(file == msvcrt_get_file(MSVCRT_stream_idx-1)) {
2602 while(MSVCRT_stream_idx>3 && !file->_flag) {
2603 MSVCRT_stream_idx--;
2604 file = msvcrt_get_file(MSVCRT_stream_idx-1);
2608 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2611 /*********************************************************************
2614 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2618 MSVCRT__lock_file(file);
2619 ret = file->_flag & MSVCRT__IOEOF;
2620 MSVCRT__unlock_file(file);
2625 /*********************************************************************
2628 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2632 MSVCRT__lock_file(file);
2633 ret = file->_flag & MSVCRT__IOERR;
2634 MSVCRT__unlock_file(file);
2639 /*********************************************************************
2640 * _filbuf (MSVCRT.@)
2642 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2645 MSVCRT__lock_file(file);
2647 /* Allocate buffer if needed */
2648 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
2649 msvcrt_alloc_buffer(file);
2651 if(!(file->_flag & MSVCRT__IOREAD)) {
2652 if(file->_flag & MSVCRT__IORW)
2653 file->_flag |= MSVCRT__IOREAD;
2655 MSVCRT__unlock_file(file);
2660 if(file->_flag & MSVCRT__IONBF) {
2662 if ((r = read_i(file->_file,&c,1)) != 1) {
2663 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2664 MSVCRT__unlock_file(file);
2668 MSVCRT__unlock_file(file);
2671 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2673 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2675 MSVCRT__unlock_file(file);
2680 file->_ptr = file->_base+1;
2681 c = *(unsigned char *)file->_base;
2682 MSVCRT__unlock_file(file);
2687 /*********************************************************************
2690 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2695 MSVCRT__lock_file(file);
2698 i = (unsigned char *)file->_ptr++;
2701 j = MSVCRT__filbuf(file);
2703 MSVCRT__unlock_file(file);
2707 /*********************************************************************
2708 * _fgetchar (MSVCRT.@)
2710 int CDECL _fgetchar(void)
2712 return MSVCRT_fgetc(MSVCRT_stdin);
2715 /*********************************************************************
2718 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2720 int cc = MSVCRT_EOF;
2721 char * buf_start = s;
2723 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2724 file,file->_file,s,size);
2726 MSVCRT__lock_file(file);
2728 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2733 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2735 TRACE(":nothing read\n");
2736 MSVCRT__unlock_file(file);
2739 if ((cc != MSVCRT_EOF) && (size > 1))
2742 TRACE(":got %s\n", debugstr_a(buf_start));
2743 MSVCRT__unlock_file(file);
2747 /*********************************************************************
2750 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2751 * the CR from CR/LF combinations
2753 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2757 MSVCRT__lock_file(file);
2758 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
2765 for(i=0; i<sizeof(wc); i++)
2775 j = MSVCRT__filbuf(file);
2778 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2781 MSVCRT__unlock_file(file);
2788 MSVCRT__unlock_file(file);
2792 c = MSVCRT_fgetc(file);
2793 if ((get_locinfo()->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2795 FIXME("Treat Multibyte characters\n");
2798 MSVCRT__unlock_file(file);
2799 if (c == MSVCRT_EOF)
2802 return (MSVCRT_wint_t)c;
2805 /*********************************************************************
2808 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2815 MSVCRT__lock_file(file);
2816 for (j=0; j<sizeof(int); j++) {
2817 k = MSVCRT_fgetc(file);
2818 if (k == MSVCRT_EOF) {
2819 file->_flag |= MSVCRT__IOEOF;
2820 MSVCRT__unlock_file(file);
2826 MSVCRT__unlock_file(file);
2830 /*********************************************************************
2833 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2835 return MSVCRT_fgetwc(file);
2838 /*********************************************************************
2839 * _fgetwchar (MSVCRT.@)
2841 MSVCRT_wint_t CDECL _fgetwchar(void)
2843 return MSVCRT_fgetwc(MSVCRT_stdin);
2846 /*********************************************************************
2847 * getwchar (MSVCRT.@)
2849 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2851 return _fgetwchar();
2854 /*********************************************************************
2857 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2859 int cc = MSVCRT_WEOF;
2860 MSVCRT_wchar_t * buf_start = s;
2862 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2863 file,file->_file,s,size);
2865 MSVCRT__lock_file(file);
2867 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2872 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2874 TRACE(":nothing read\n");
2875 MSVCRT__unlock_file(file);
2878 if ((cc != MSVCRT_WEOF) && (size > 1))
2881 TRACE(":got %s\n", debugstr_w(buf_start));
2882 MSVCRT__unlock_file(file);
2886 /*********************************************************************
2889 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2891 MSVCRT_size_t wrcnt=size * nmemb;
2896 MSVCRT__lock_file(file);
2898 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2899 memcpy(file->_ptr, ptr, pcnt);
2904 ptr = (const char*)ptr + pcnt;
2905 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2906 if(file->_flag & MSVCRT__IORW) {
2907 file->_flag |= MSVCRT__IOWRT;
2909 MSVCRT__unlock_file(file);
2915 int res=msvcrt_flush_buffer(file);
2917 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2920 file->_flag |= MSVCRT__IOERR;
2923 written += pwritten;
2927 MSVCRT__unlock_file(file);
2928 return written / size;
2931 /*********************************************************************
2934 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2936 MSVCRT_wchar_t mwc=wc;
2937 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2942 /*********************************************************************
2943 * _fputwchar (MSVCRT.@)
2945 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2947 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2950 /*********************************************************************
2951 * _wfsopen (MSVCRT.@)
2953 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2956 int open_flags, stream_flags, fd;
2958 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2960 /* map mode string to open() flags. "man fopen" for possibilities. */
2961 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2965 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2968 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2970 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2977 TRACE(":got (%p)\n",file);
2978 if (fd >= 0 && !file)
2984 /*********************************************************************
2985 * _fsopen (MSVCRT.@)
2987 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2990 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2992 if (path && !(pathW = msvcrt_wstrdupa(path))) {
2993 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2994 *MSVCRT__errno() = MSVCRT_EINVAL;
2997 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3000 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3001 *MSVCRT__errno() = MSVCRT_EINVAL;
3005 ret = MSVCRT__wfsopen(pathW, modeW, share);
3012 /*********************************************************************
3015 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3017 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3020 /*********************************************************************
3021 * fopen_s (MSVCRT.@)
3023 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3024 const char *filename, const char *mode)
3026 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
3027 !MSVCRT_CHECK_PMT(mode != NULL)) {
3028 *MSVCRT__errno() = MSVCRT_EINVAL;
3029 return MSVCRT_EINVAL;
3032 *pFile = MSVCRT_fopen(filename, mode);
3035 return *MSVCRT__errno();
3039 /*********************************************************************
3040 * _wfopen (MSVCRT.@)
3042 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3044 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3047 /*********************************************************************
3048 * _wfopen_s (MSVCRT.@)
3050 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3051 const MSVCRT_wchar_t *mode)
3053 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
3054 !MSVCRT_CHECK_PMT(mode != NULL)) {
3055 *MSVCRT__errno() = MSVCRT_EINVAL;
3056 return MSVCRT_EINVAL;
3059 *pFile = MSVCRT__wfopen(filename, mode);
3062 return *MSVCRT__errno();
3066 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
3067 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
3069 /*********************************************************************
3072 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3076 MSVCRT__lock_file(file);
3082 res = msvcrt_flush_buffer(file);
3083 MSVCRT__unlock_file(file);
3084 return res ? res : c;
3087 MSVCRT__unlock_file(file);
3091 res = MSVCRT__flsbuf(c, file);
3092 MSVCRT__unlock_file(file);
3097 /*********************************************************************
3098 * _flsbuf (MSVCRT.@)
3100 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3102 MSVCRT__lock_file(file);
3104 /* Flush output buffer */
3105 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3106 msvcrt_alloc_buffer(file);
3108 if(!(file->_flag & MSVCRT__IOWRT)) {
3109 if(file->_flag & MSVCRT__IORW) {
3110 file->_flag |= MSVCRT__IOWRT;
3112 MSVCRT__unlock_file(file);
3117 int res=msvcrt_flush_buffer(file);
3119 res = MSVCRT_fputc(c, file);
3120 MSVCRT__unlock_file(file);
3125 /* set _cnt to 0 for unbuffered FILEs */
3127 len = MSVCRT__write(file->_file, &cc, 1);
3129 MSVCRT__unlock_file(file);
3132 file->_flag |= MSVCRT__IOERR;
3133 MSVCRT__unlock_file(file);
3138 /*********************************************************************
3139 * _fputchar (MSVCRT.@)
3141 int CDECL _fputchar(int c)
3143 return MSVCRT_fputc(c, MSVCRT_stdout);
3146 /*********************************************************************
3149 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3151 MSVCRT_size_t rcnt=size * nmemb;
3152 MSVCRT_size_t read=0;
3158 MSVCRT__lock_file(file);
3160 /* first buffered data */
3162 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3163 memcpy(ptr, file->_ptr, pcnt);
3168 ptr = (char*)ptr + pcnt;
3169 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3170 if(file->_flag & MSVCRT__IORW) {
3171 file->_flag |= MSVCRT__IOREAD;
3173 MSVCRT__unlock_file(file);
3180 /* Fill the buffer on small reads.
3181 * TODO: Use a better buffering strategy.
3183 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
3184 if (file->_bufsiz == 0) {
3185 msvcrt_alloc_buffer(file);
3187 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3188 file->_ptr = file->_base;
3189 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3190 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3191 if (i > 0 && i < file->_cnt) {
3192 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3193 file->_flag &= ~MSVCRT__IOEOF;
3196 memcpy(ptr, file->_ptr, i);
3201 i = MSVCRT__read(file->_file,ptr, rcnt);
3205 ptr = (char *)ptr+i;
3206 /* expose feof condition in the flags
3207 * MFC tests file->_flag for feof, and doesn't call feof())
3209 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3210 file->_flag |= MSVCRT__IOEOF;
3213 file->_flag |= MSVCRT__IOERR;
3220 MSVCRT__unlock_file(file);
3224 /*********************************************************************
3225 * _wfreopen (MSVCRT.@)
3228 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3230 int open_flags, stream_flags, fd;
3232 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3235 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3239 MSVCRT_fclose(file);
3240 /* map mode string to open() flags. "man fopen" for possibilities. */
3241 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3245 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3248 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
3251 WARN(":failed-last error (%d)\n",GetLastError());
3252 msvcrt_set_errno(GetLastError());
3261 /*********************************************************************
3262 * freopen (MSVCRT.@)
3265 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3268 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3270 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3271 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3277 ret = MSVCRT__wfreopen(pathW, modeW, file);
3284 /*********************************************************************
3285 * fsetpos (MSVCRT.@)
3287 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3291 MSVCRT__lock_file(file);
3292 /* Note that all this has been lifted 'as is' from fseek */
3293 if(file->_flag & MSVCRT__IOWRT)
3294 msvcrt_flush_buffer(file);
3296 /* Discard buffered input */
3298 file->_ptr = file->_base;
3300 /* Reset direction of i/o */
3301 if(file->_flag & MSVCRT__IORW) {
3302 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3305 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3306 MSVCRT__unlock_file(file);
3310 /*********************************************************************
3311 * _ftelli64 (MSVCRT.@)
3313 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3315 /* TODO: just call fgetpos and return lower half of result */
3319 MSVCRT__lock_file(file);
3320 pos = _telli64(file->_file);
3322 MSVCRT__unlock_file(file);
3326 if( file->_flag & MSVCRT__IOWRT ) {
3327 off = file->_ptr - file->_base;
3330 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3331 /* Black magic correction for CR removal */
3333 for (i=0; i<file->_cnt; i++) {
3334 if (file->_ptr[i] == '\n')
3337 /* Black magic when reading CR at buffer boundary*/
3338 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3344 MSVCRT__unlock_file(file);
3348 /*********************************************************************
3351 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3353 return MSVCRT__ftelli64(file);
3356 /*********************************************************************
3357 * fgetpos (MSVCRT.@)
3359 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3363 MSVCRT__lock_file(file);
3364 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3366 MSVCRT__unlock_file(file);
3370 if( file->_flag & MSVCRT__IOWRT ) {
3371 off = file->_ptr - file->_base;
3374 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3375 /* Black magic correction for CR removal */
3377 for (i=0; i<file->_cnt; i++) {
3378 if (file->_ptr[i] == '\n')
3381 /* Black magic when reading CR at buffer boundary*/
3382 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3388 MSVCRT__unlock_file(file);
3392 /*********************************************************************
3395 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3397 MSVCRT_size_t i, len = strlen(s);
3400 MSVCRT__lock_file(file);
3401 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3402 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3403 MSVCRT__unlock_file(file);
3406 for (i=0; i<len; i++)
3407 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF) {
3408 MSVCRT__unlock_file(file);
3412 MSVCRT__unlock_file(file);
3416 /*********************************************************************
3419 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3421 MSVCRT_size_t i, len = strlenW(s);
3424 MSVCRT__lock_file(file);
3425 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3426 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3427 MSVCRT__unlock_file(file);
3430 for (i=0; i<len; i++) {
3431 if (((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3432 || MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
3433 MSVCRT__unlock_file(file);
3438 MSVCRT__unlock_file(file);
3442 /*********************************************************************
3443 * getchar (MSVCRT.@)
3445 int CDECL MSVCRT_getchar(void)
3447 return MSVCRT_fgetc(MSVCRT_stdin);
3450 /*********************************************************************
3453 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3455 return MSVCRT_fgetc(file);
3458 /*********************************************************************
3461 char * CDECL MSVCRT_gets(char *buf)
3464 char * buf_start = buf;
3466 MSVCRT__lock_file(MSVCRT_stdin);
3467 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3468 cc = MSVCRT_fgetc(MSVCRT_stdin))
3469 if(cc != '\r') *buf++ = (char)cc;
3473 TRACE("got '%s'\n", buf_start);
3474 MSVCRT__unlock_file(MSVCRT_stdin);
3478 /*********************************************************************
3481 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3484 MSVCRT_wchar_t* ws = buf;
3486 MSVCRT__lock_file(MSVCRT_stdin);
3487 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3488 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3491 *buf++ = (MSVCRT_wchar_t)cc;
3495 TRACE("got %s\n", debugstr_w(ws));
3496 MSVCRT__unlock_file(MSVCRT_stdin);
3500 /*********************************************************************
3503 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3505 return MSVCRT_fputc(c, file);
3508 /*********************************************************************
3509 * putchar (MSVCRT.@)
3511 int CDECL MSVCRT_putchar(int c)
3513 return MSVCRT_fputc(c, MSVCRT_stdout);
3516 /*********************************************************************
3517 * _putwch (MSVCRT.@)
3519 int CDECL MSVCRT__putwch(int c)
3521 return MSVCRT_fputwc(c, MSVCRT_stdout);
3524 /*********************************************************************
3527 int CDECL MSVCRT_puts(const char *s)
3529 MSVCRT_size_t len = strlen(s);
3532 MSVCRT__lock_file(MSVCRT_stdout);
3533 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3534 MSVCRT__unlock_file(MSVCRT_stdout);
3538 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3539 MSVCRT__unlock_file(MSVCRT_stdout);
3543 /*********************************************************************
3546 int CDECL _putws(const MSVCRT_wchar_t *s)
3548 static const MSVCRT_wchar_t nl = '\n';
3549 MSVCRT_size_t len = strlenW(s);
3552 MSVCRT__lock_file(MSVCRT_stdout);
3553 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3554 MSVCRT__unlock_file(MSVCRT_stdout);
3558 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3559 MSVCRT__unlock_file(MSVCRT_stdout);
3563 /*********************************************************************
3566 int CDECL MSVCRT_remove(const char *path)
3568 TRACE("(%s)\n",path);
3569 if (DeleteFileA(path))
3571 TRACE(":failed (%d)\n",GetLastError());
3572 msvcrt_set_errno(GetLastError());
3576 /*********************************************************************
3577 * _wremove (MSVCRT.@)
3579 int CDECL _wremove(const MSVCRT_wchar_t *path)
3581 TRACE("(%s)\n",debugstr_w(path));
3582 if (DeleteFileW(path))
3584 TRACE(":failed (%d)\n",GetLastError());
3585 msvcrt_set_errno(GetLastError());
3589 /*********************************************************************
3592 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3594 TRACE(":from %s to %s\n",oldpath,newpath);
3595 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3597 TRACE(":failed (%d)\n",GetLastError());
3598 msvcrt_set_errno(GetLastError());
3602 /*********************************************************************
3603 * _wrename (MSVCRT.@)
3605 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3607 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3608 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3610 TRACE(":failed (%d)\n",GetLastError());
3611 msvcrt_set_errno(GetLastError());
3615 /*********************************************************************
3616 * setvbuf (MSVCRT.@)
3618 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3620 MSVCRT__lock_file(file);
3622 MSVCRT_free(file->_base);
3626 if(mode == MSVCRT__IOFBF) {
3627 file->_flag &= ~MSVCRT__IONBF;
3628 file->_base = file->_ptr = buf;
3630 file->_bufsiz = size;
3633 file->_flag |= MSVCRT__IONBF;
3635 MSVCRT__unlock_file(file);
3639 /*********************************************************************
3642 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3644 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3647 /*********************************************************************
3650 char * CDECL MSVCRT_tmpnam(char *s)
3657 thread_data_t *data = msvcrt_get_thread_data();
3659 if(!data->tmpnam_buffer)
3660 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
3662 s = data->tmpnam_buffer;
3665 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3666 p = s + sprintf(s, "\\s%s.", tmpstr);
3667 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3669 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
3670 memcpy(p, tmpstr, size);
3671 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3672 GetLastError() == ERROR_FILE_NOT_FOUND)
3678 /*********************************************************************
3679 * _wtmpnam (MSVCRT.@)
3681 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
3683 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
3684 MSVCRT_wchar_t tmpstr[16];
3688 thread_data_t *data = msvcrt_get_thread_data();
3690 if(!data->wtmpnam_buffer)
3691 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
3693 s = data->wtmpnam_buffer;
3696 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
3697 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
3698 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3700 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
3701 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
3702 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
3703 GetLastError() == ERROR_FILE_NOT_FOUND)
3709 /*********************************************************************
3710 * tmpfile (MSVCRT.@)
3712 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3714 char *filename = MSVCRT_tmpnam(NULL);
3716 MSVCRT_FILE* file = NULL;
3719 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3720 if (fd != -1 && (file = msvcrt_alloc_fp()))
3722 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3727 else file->_tmpfname = _strdup(filename);
3733 static int puts_clbk_file_a(void *file, int len, const char *str)
3735 return MSVCRT_fwrite(str, sizeof(char), len, file);
3738 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
3740 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
3743 /*********************************************************************
3744 * vfprintf (MSVCRT.@)
3746 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3750 MSVCRT__lock_file(file);
3751 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3752 MSVCRT__unlock_file(file);
3757 /*********************************************************************
3758 * vfprintf_s (MSVCRT.@)
3760 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3764 if(!MSVCRT_CHECK_PMT(file != NULL)) {
3765 *MSVCRT__errno() = MSVCRT_EINVAL;
3769 MSVCRT__lock_file(file);
3770 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3771 MSVCRT__unlock_file(file);
3776 /*********************************************************************
3777 * vfwprintf (MSVCRT.@)
3779 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3783 MSVCRT__lock_file(file);
3784 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3785 MSVCRT__unlock_file(file);
3790 /*********************************************************************
3791 * vfwprintf_s (MSVCRT.@)
3793 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3797 if(!MSVCRT_CHECK_PMT( file != NULL)) {
3798 *MSVCRT__errno() = MSVCRT_EINVAL;
3802 MSVCRT__lock_file(file);
3803 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3804 MSVCRT__unlock_file(file);
3809 /*********************************************************************
3810 * vprintf (MSVCRT.@)
3812 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3814 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3817 /*********************************************************************
3818 * vprintf_s (MSVCRT.@)
3820 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
3822 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
3825 /*********************************************************************
3826 * vwprintf (MSVCRT.@)
3828 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3830 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3833 /*********************************************************************
3834 * vwprintf_s (MSVCRT.@)
3836 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
3838 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
3841 /*********************************************************************
3842 * fprintf (MSVCRT.@)
3844 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3846 __ms_va_list valist;
3848 __ms_va_start(valist, format);
3849 res = MSVCRT_vfprintf(file, format, valist);
3850 __ms_va_end(valist);
3854 /*********************************************************************
3855 * fprintf_s (MSVCRT.@)
3857 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
3859 __ms_va_list valist;
3861 __ms_va_start(valist, format);
3862 res = MSVCRT_vfprintf_s(file, format, valist);
3863 __ms_va_end(valist);
3867 /*********************************************************************
3868 * fwprintf (MSVCRT.@)
3870 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3872 __ms_va_list valist;
3874 __ms_va_start(valist, format);
3875 res = MSVCRT_vfwprintf(file, format, valist);
3876 __ms_va_end(valist);
3880 /*********************************************************************
3881 * fwprintf_s (MSVCRT.@)
3883 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3885 __ms_va_list valist;
3887 __ms_va_start(valist, format);
3888 res = MSVCRT_vfwprintf_s(file, format, valist);
3889 __ms_va_end(valist);
3893 /*********************************************************************
3896 int CDECL MSVCRT_printf(const char *format, ...)
3898 __ms_va_list valist;
3900 __ms_va_start(valist, format);
3901 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3902 __ms_va_end(valist);
3906 /*********************************************************************
3907 * printf_s (MSVCRT.@)
3909 int CDECL MSVCRT_printf_s(const char *format, ...)
3911 __ms_va_list valist;
3913 __ms_va_start(valist, format);
3914 res = MSVCRT_vprintf_s(format, valist);
3915 __ms_va_end(valist);
3919 /*********************************************************************
3922 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3924 if (c == MSVCRT_EOF)
3927 MSVCRT__lock_file(file);
3928 if(file->_bufsiz == 0) {
3929 msvcrt_alloc_buffer(file);
3932 if(file->_ptr>file->_base) {
3936 MSVCRT_clearerr(file);
3937 MSVCRT__unlock_file(file);
3941 MSVCRT__unlock_file(file);
3945 /*********************************************************************
3946 * ungetwc (MSVCRT.@)
3948 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3950 MSVCRT_wchar_t mwc = wc;
3951 char * pp = (char *)&mwc;
3954 MSVCRT__lock_file(file);
3955 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3956 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
3957 MSVCRT__unlock_file(file);
3962 MSVCRT__unlock_file(file);
3966 /*********************************************************************
3967 * wprintf (MSVCRT.@)
3969 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3971 __ms_va_list valist;
3973 __ms_va_start(valist, format);
3974 res = MSVCRT_vwprintf(format, valist);
3975 __ms_va_end(valist);
3979 /*********************************************************************
3980 * wprintf_s (MSVCRT.@)
3982 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
3984 __ms_va_list valist;
3986 __ms_va_start(valist, format);
3987 res = MSVCRT_vwprintf_s(format, valist);
3988 __ms_va_end(valist);
3992 /*********************************************************************
3993 * _getmaxstdio (MSVCRT.@)
3995 int CDECL _getmaxstdio(void)
3997 return MSVCRT_max_streams;
4000 /*********************************************************************
4001 * _setmaxstdio (MSVCRT.@)
4003 int CDECL _setmaxstdio(int newmax)
4005 TRACE("%d\n", newmax);
4007 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4010 MSVCRT_max_streams = newmax;
4011 return MSVCRT_max_streams;