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 32
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 [32]
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 WARN("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 MSVCRT__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 MSVCRT__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 MSVCRT__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 MSVCRT__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 MSVCRT__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 (!msvcrt_is_valid_fd(fd)) {
804 ret = CloseHandle(hand) ? 0 : -1;
806 WARN(":failed-last error (%d)\n",GetLastError());
807 msvcrt_set_errno(GetLastError());
815 /*********************************************************************
818 int CDECL MSVCRT__commit(int fd)
820 HANDLE hand = msvcrt_fdtoh(fd);
822 TRACE(":fd (%d) handle (%p)\n",fd,hand);
823 if (hand == INVALID_HANDLE_VALUE)
826 if (!FlushFileBuffers(hand))
828 if (GetLastError() == ERROR_INVALID_HANDLE)
830 /* FlushFileBuffers fails for console handles
831 * so we ignore this error.
835 TRACE(":failed-last error (%d)\n",GetLastError());
836 msvcrt_set_errno(GetLastError());
843 /*********************************************************************
846 * MSDN isn't clear on this point, but the remarks for _pipe
847 * indicate file descriptors duplicated with _dup and _dup2 are always
850 int CDECL MSVCRT__dup2(int od, int nd)
854 TRACE("(od=%d, nd=%d)\n", od, nd);
856 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
860 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
861 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
863 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
865 if (msvcrt_is_valid_fd(nd))
867 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
871 *MSVCRT__errno() = MSVCRT_EMFILE;
875 /* _dup2 returns 0, not nd, on success */
882 msvcrt_set_errno(GetLastError());
887 *MSVCRT__errno() = MSVCRT_EBADF;
894 /*********************************************************************
897 int CDECL MSVCRT__dup(int od)
903 if (MSVCRT__dup2(od, fd) == 0)
911 /*********************************************************************
914 int CDECL MSVCRT__eof(int fd)
917 LONG hcurpos,hendpos;
918 HANDLE hand = msvcrt_fdtoh(fd);
920 TRACE(":fd (%d) handle (%p)\n",fd,hand);
922 if (hand == INVALID_HANDLE_VALUE)
925 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
927 /* Otherwise we do it the hard way */
928 hcurpos = hendpos = 0;
929 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
930 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
932 if (curpos == endpos && hcurpos == hendpos)
934 /* FIXME: shouldn't WX_ATEOF be set here? */
938 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
942 /*********************************************************************
943 * _fcloseall (MSVCRT.@)
945 int CDECL MSVCRT__fcloseall(void)
947 int num_closed = 0, i;
951 for (i = 3; i < MSVCRT_stream_idx; i++) {
952 file = msvcrt_get_file(i);
954 if (file->_flag && !MSVCRT_fclose(file))
959 TRACE(":closed (%d) handles\n",num_closed);
963 /* free everything on process exit */
964 void msvcrt_free_io(void)
969 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
970 * stdout, and stderr (unlike GNU), so we need to fclose() them here
971 * or they won't get flushed.
973 MSVCRT_fclose(&MSVCRT__iob[0]);
974 MSVCRT_fclose(&MSVCRT__iob[1]);
975 MSVCRT_fclose(&MSVCRT__iob[2]);
977 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
978 MSVCRT_free(MSVCRT___pioinfo[i]);
980 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
981 MSVCRT_free(MSVCRT_fstream[i]);
983 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
984 DeleteCriticalSection(&MSVCRT_file_cs);
987 /*********************************************************************
988 * _lseeki64 (MSVCRT.@)
990 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
992 HANDLE hand = msvcrt_fdtoh(fd);
995 TRACE(":fd (%d) handle (%p)\n",fd,hand);
996 if (hand == INVALID_HANDLE_VALUE)
999 if (whence < 0 || whence > 2)
1001 *MSVCRT__errno() = MSVCRT_EINVAL;
1005 TRACE(":fd (%d) to %s pos %s\n",
1006 fd,wine_dbgstr_longlong(offset),
1007 (whence==SEEK_SET)?"SEEK_SET":
1008 (whence==SEEK_CUR)?"SEEK_CUR":
1009 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1011 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1012 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1013 ofs.QuadPart = offset;
1014 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1015 GetLastError() == ERROR_SUCCESS)
1017 msvcrt_get_ioinfo(fd)->wxflag &= ~(WX_ATEOF|WX_READEOF);
1018 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1020 return ofs.QuadPart;
1022 TRACE(":error-last error (%d)\n",GetLastError());
1023 msvcrt_set_errno(GetLastError());
1027 /*********************************************************************
1030 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1032 return MSVCRT__lseeki64(fd, offset, whence);
1035 /*********************************************************************
1036 * _lock_file (MSVCRT.@)
1038 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1040 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1041 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1043 EnterCriticalSection(&((file_crit*)file)->crit);
1046 /*********************************************************************
1047 * _unlock_file (MSVCRT.@)
1049 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1051 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1052 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1054 LeaveCriticalSection(&((file_crit*)file)->crit);
1057 /*********************************************************************
1058 * _locking (MSVCRT.@)
1060 * This is untested; the underlying LockFile doesn't work yet.
1062 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1066 HANDLE hand = msvcrt_fdtoh(fd);
1068 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1069 if (hand == INVALID_HANDLE_VALUE)
1072 if (mode < 0 || mode > 4)
1074 *MSVCRT__errno() = MSVCRT_EINVAL;
1078 TRACE(":fd (%d) by 0x%08x mode %s\n",
1079 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1080 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1081 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1082 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1083 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1086 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1088 FIXME ("Seek failed\n");
1089 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1092 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1095 ret = 1; /* just to satisfy gcc */
1098 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1103 else if (mode == MSVCRT__LK_UNLCK)
1104 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1106 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1107 /* FIXME - what about error settings? */
1108 return ret ? 0 : -1;
1111 /*********************************************************************
1112 * _fseeki64 (MSVCRT.@)
1114 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1118 MSVCRT__lock_file(file);
1119 /* Flush output if needed */
1120 if(file->_flag & MSVCRT__IOWRT)
1121 msvcrt_flush_buffer(file);
1123 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1124 offset -= file->_cnt;
1125 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
1126 /* Black magic correction for CR removal */
1128 for (i=0; i<file->_cnt; i++) {
1129 if (file->_ptr[i] == '\n')
1132 /* Black magic when reading CR at buffer boundary*/
1133 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
1137 /* Discard buffered input */
1139 file->_ptr = file->_base;
1140 /* Reset direction of i/o */
1141 if(file->_flag & MSVCRT__IORW) {
1142 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1144 /* Clear end of file flag */
1145 file->_flag &= ~MSVCRT__IOEOF;
1146 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1148 MSVCRT__unlock_file(file);
1152 /*********************************************************************
1155 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1157 return MSVCRT__fseeki64( file, offset, whence );
1160 /*********************************************************************
1161 * _chsize (MSVCRT.@)
1163 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1169 TRACE("(fd=%d, size=%d)\n", fd, size);
1173 handle = msvcrt_fdtoh(fd);
1174 if (handle != INVALID_HANDLE_VALUE)
1176 /* save the current file pointer */
1177 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1180 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1183 ret = SetEndOfFile(handle);
1184 if (!ret) msvcrt_set_errno(GetLastError());
1187 /* restore the file pointer */
1188 MSVCRT__lseek(fd, cur, SEEK_SET);
1193 return ret ? 0 : -1;
1196 /*********************************************************************
1197 * clearerr (MSVCRT.@)
1199 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1201 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1203 MSVCRT__lock_file(file);
1204 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1205 MSVCRT__unlock_file(file);
1208 /*********************************************************************
1211 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1213 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1215 MSVCRT__lock_file(file);
1216 MSVCRT_fseek(file, 0L, SEEK_SET);
1217 MSVCRT_clearerr(file);
1218 MSVCRT__unlock_file(file);
1221 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1223 int plus = strchrW(mode, '+') != NULL;
1228 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1229 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1232 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1233 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1236 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1237 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1240 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1241 *MSVCRT__errno() = MSVCRT_EINVAL;
1249 *open_flags |= MSVCRT__O_BINARY;
1250 *open_flags &= ~MSVCRT__O_TEXT;
1253 *open_flags |= MSVCRT__O_TEXT;
1254 *open_flags &= ~MSVCRT__O_BINARY;
1260 FIXME(":unknown flag %c not supported\n",mode[-1]);
1265 /*********************************************************************
1266 * _fdopen (MSVCRT.@)
1268 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1271 MSVCRT_wchar_t *modeW = NULL;
1273 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1275 ret = MSVCRT__wfdopen(fd, modeW);
1281 /*********************************************************************
1282 * _wfdopen (MSVCRT.@)
1284 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1286 int open_flags, stream_flags;
1289 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1292 if (!(file = msvcrt_alloc_fp()))
1294 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1299 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1305 /*********************************************************************
1306 * _filelength (MSVCRT.@)
1308 LONG CDECL MSVCRT__filelength(int fd)
1310 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1313 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1316 if (endPos != curPos)
1317 MSVCRT__lseek(fd, curPos, SEEK_SET);
1324 /*********************************************************************
1325 * _filelengthi64 (MSVCRT.@)
1327 __int64 CDECL MSVCRT__filelengthi64(int fd)
1329 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1332 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1335 if (endPos != curPos)
1336 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1343 /*********************************************************************
1344 * _fileno (MSVCRT.@)
1346 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1350 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1352 MSVCRT__lock_file(file);
1354 MSVCRT__unlock_file(file);
1359 /*********************************************************************
1360 * _fstat64 (MSVCRT.@)
1362 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1366 BY_HANDLE_FILE_INFORMATION hfi;
1367 HANDLE hand = msvcrt_fdtoh(fd);
1369 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1370 if (hand == INVALID_HANDLE_VALUE)
1375 WARN(":failed-NULL buf\n");
1376 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1380 memset(&hfi, 0, sizeof(hfi));
1381 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1382 type = GetFileType(hand);
1383 if (type == FILE_TYPE_PIPE)
1385 buf->st_dev = buf->st_rdev = fd;
1386 buf->st_mode = S_IFIFO;
1389 else if (type == FILE_TYPE_CHAR)
1391 buf->st_dev = buf->st_rdev = fd;
1392 buf->st_mode = S_IFCHR;
1395 else /* FILE_TYPE_DISK etc. */
1397 if (!GetFileInformationByHandle(hand, &hfi))
1399 WARN(":failed-last error (%d)\n",GetLastError());
1400 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1403 buf->st_mode = S_IFREG | 0444;
1404 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1405 buf->st_mode |= 0222;
1406 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1407 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1409 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1410 buf->st_mtime = buf->st_ctime = dw;
1411 buf->st_nlink = hfi.nNumberOfLinks;
1413 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1418 /*********************************************************************
1419 * _fstati64 (MSVCRT.@)
1421 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1424 struct MSVCRT__stat64 buf64;
1426 ret = MSVCRT__fstat64(fd, &buf64);
1428 msvcrt_stat64_to_stati64(&buf64, buf);
1432 /*********************************************************************
1435 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1437 struct MSVCRT__stat64 buf64;
1439 ret = MSVCRT__fstat64(fd, &buf64);
1441 msvcrt_stat64_to_stat(&buf64, buf);
1445 /*********************************************************************
1446 * _futime64 (MSVCRT.@)
1448 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1450 HANDLE hand = msvcrt_fdtoh(fd);
1455 time_to_filetime( MSVCRT__time64(NULL), &at );
1460 time_to_filetime( t->actime, &at );
1461 time_to_filetime( t->modtime, &wt );
1464 if (!SetFileTime(hand, NULL, &at, &wt))
1466 msvcrt_set_errno(GetLastError());
1472 /*********************************************************************
1473 * _futime32 (MSVCRT.@)
1475 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1479 struct MSVCRT___utimbuf64 t64;
1480 t64.actime = t->actime;
1481 t64.modtime = t->modtime;
1482 return _futime64( fd, &t64 );
1485 return _futime64( fd, NULL );
1488 /*********************************************************************
1489 * _futime (MSVCRT.@)
1492 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1494 return _futime64( fd, t );
1497 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1499 return _futime32( fd, t );
1503 /*********************************************************************
1504 * _get_osfhandle (MSVCRT.@)
1506 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1508 HANDLE hand = msvcrt_fdtoh(fd);
1509 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1511 return (MSVCRT_intptr_t)hand;
1514 /*********************************************************************
1515 * _isatty (MSVCRT.@)
1517 int CDECL MSVCRT__isatty(int fd)
1519 HANDLE hand = msvcrt_fdtoh(fd);
1521 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1522 if (hand == INVALID_HANDLE_VALUE)
1525 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1528 /*********************************************************************
1529 * _mktemp (MSVCRT.@)
1531 char * CDECL MSVCRT__mktemp(char *pattern)
1534 char *retVal = pattern;
1539 numX = (*pattern++ == 'X')? numX + 1 : 0;
1543 id = GetCurrentProcessId();
1547 int tempNum = id / 10;
1548 *pattern-- = id - (tempNum * 10) + '0';
1554 *pattern = letter++;
1555 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1556 GetLastError() == ERROR_FILE_NOT_FOUND)
1558 } while(letter <= 'z');
1562 /*********************************************************************
1563 * _wmktemp (MSVCRT.@)
1565 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1568 MSVCRT_wchar_t *retVal = pattern;
1570 MSVCRT_wchar_t letter = 'a';
1573 numX = (*pattern++ == 'X')? numX + 1 : 0;
1577 id = GetCurrentProcessId();
1581 int tempNum = id / 10;
1582 *pattern-- = id - (tempNum * 10) + '0';
1588 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1589 GetLastError() == ERROR_FILE_NOT_FOUND)
1591 *pattern = letter++;
1592 } while(letter != '|');
1596 static unsigned split_oflags(unsigned oflags)
1599 unsigned unsupp; /* until we support everything */
1601 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1602 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1603 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1604 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1605 else wxflags |= WX_TEXT; /* default to TEXT*/
1606 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1608 if ((unsupp = oflags & ~(
1609 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1610 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1611 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1612 MSVCRT__O_NOINHERIT|
1613 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1615 ERR(":unsupported oflags 0x%04x\n",unsupp);
1620 /*********************************************************************
1623 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1626 SECURITY_ATTRIBUTES sa;
1627 HANDLE readHandle, writeHandle;
1631 *MSVCRT__errno() = MSVCRT_EINVAL;
1635 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1636 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1637 sa.lpSecurityDescriptor = NULL;
1638 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1640 unsigned int wxflags = split_oflags(textmode);
1644 fd = msvcrt_alloc_fd(readHandle, wxflags);
1648 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1656 MSVCRT__close(pfds[0]);
1657 CloseHandle(writeHandle);
1658 *MSVCRT__errno() = MSVCRT_EMFILE;
1663 CloseHandle(readHandle);
1664 CloseHandle(writeHandle);
1665 *MSVCRT__errno() = MSVCRT_EMFILE;
1670 msvcrt_set_errno(GetLastError());
1675 /*********************************************************************
1676 * _sopen_s (MSVCRT.@)
1678 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1680 DWORD access = 0, creation = 0, attrib;
1684 SECURITY_ATTRIBUTES sa;
1686 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1687 fd, path, oflags, shflags, pmode);
1691 MSVCRT_INVALID_PMT("null out fd pointer");
1692 *MSVCRT__errno() = MSVCRT_EINVAL;
1693 return MSVCRT_EINVAL;
1697 wxflag = split_oflags(oflags);
1698 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1700 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1701 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1702 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1705 if (oflags & MSVCRT__O_CREAT)
1707 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1708 FIXME(": pmode 0x%04x ignored\n", pmode);
1710 WARN(": pmode 0x%04x ignored\n", pmode);
1712 if (oflags & MSVCRT__O_EXCL)
1713 creation = CREATE_NEW;
1714 else if (oflags & MSVCRT__O_TRUNC)
1715 creation = CREATE_ALWAYS;
1717 creation = OPEN_ALWAYS;
1719 else /* no MSVCRT__O_CREAT */
1721 if (oflags & MSVCRT__O_TRUNC)
1722 creation = TRUNCATE_EXISTING;
1724 creation = OPEN_EXISTING;
1729 case MSVCRT__SH_DENYRW:
1732 case MSVCRT__SH_DENYWR:
1733 sharing = FILE_SHARE_READ;
1735 case MSVCRT__SH_DENYRD:
1736 sharing = FILE_SHARE_WRITE;
1738 case MSVCRT__SH_DENYNO:
1739 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1742 ERR( "Unhandled shflags 0x%x\n", shflags );
1743 return MSVCRT_EINVAL;
1745 attrib = FILE_ATTRIBUTE_NORMAL;
1747 if (oflags & MSVCRT__O_TEMPORARY)
1749 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1751 sharing |= FILE_SHARE_DELETE;
1754 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1755 sa.lpSecurityDescriptor = NULL;
1756 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1758 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1759 if (hand == INVALID_HANDLE_VALUE) {
1760 WARN(":failed-last error (%d)\n", GetLastError());
1761 msvcrt_set_errno(GetLastError());
1762 return *MSVCRT__errno();
1765 *fd = msvcrt_alloc_fd(hand, wxflag);
1767 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1771 /*********************************************************************
1774 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1779 if (oflags & MSVCRT__O_CREAT)
1783 __ms_va_start(ap, shflags);
1784 pmode = va_arg(ap, int);
1790 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
1794 /*********************************************************************
1795 * _wsopen_s (MSVCRT.@)
1797 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1799 DWORD access = 0, creation = 0, attrib;
1800 SECURITY_ATTRIBUTES sa;
1805 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1806 fd, debugstr_w(path), oflags, shflags, pmode);
1810 MSVCRT_INVALID_PMT("null out fd pointer");
1811 *MSVCRT__errno() = MSVCRT_EINVAL;
1812 return MSVCRT_EINVAL;
1816 wxflag = split_oflags(oflags);
1817 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1819 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1820 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1821 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1824 if (oflags & MSVCRT__O_CREAT)
1826 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1827 FIXME(": pmode 0x%04x ignored\n", pmode);
1829 WARN(": pmode 0x%04x ignored\n", pmode);
1831 if (oflags & MSVCRT__O_EXCL)
1832 creation = CREATE_NEW;
1833 else if (oflags & MSVCRT__O_TRUNC)
1834 creation = CREATE_ALWAYS;
1836 creation = OPEN_ALWAYS;
1838 else /* no MSVCRT__O_CREAT */
1840 if (oflags & MSVCRT__O_TRUNC)
1841 creation = TRUNCATE_EXISTING;
1843 creation = OPEN_EXISTING;
1848 case MSVCRT__SH_DENYRW:
1851 case MSVCRT__SH_DENYWR:
1852 sharing = FILE_SHARE_READ;
1854 case MSVCRT__SH_DENYRD:
1855 sharing = FILE_SHARE_WRITE;
1857 case MSVCRT__SH_DENYNO:
1858 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1861 ERR( "Unhandled shflags 0x%x\n", shflags );
1862 return MSVCRT_EINVAL;
1864 attrib = FILE_ATTRIBUTE_NORMAL;
1866 if (oflags & MSVCRT__O_TEMPORARY)
1868 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1870 sharing |= FILE_SHARE_DELETE;
1873 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1874 sa.lpSecurityDescriptor = NULL;
1875 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1877 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1879 if (hand == INVALID_HANDLE_VALUE) {
1880 WARN(":failed-last error (%d)\n",GetLastError());
1881 msvcrt_set_errno(GetLastError());
1882 msvcrt_set_errno(GetLastError());
1883 return *MSVCRT__errno();
1886 *fd = msvcrt_alloc_fd(hand, wxflag);
1888 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1892 /*********************************************************************
1893 * _wsopen (MSVCRT.@)
1895 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1900 if (oflags & MSVCRT__O_CREAT)
1904 __ms_va_start(ap, shflags);
1905 pmode = va_arg(ap, int);
1911 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1915 /*********************************************************************
1918 int CDECL MSVCRT__open( const char *path, int flags, ... )
1922 if (flags & MSVCRT__O_CREAT)
1925 __ms_va_start(ap, flags);
1926 pmode = va_arg(ap, int);
1928 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1931 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1934 /*********************************************************************
1937 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
1941 if (flags & MSVCRT__O_CREAT)
1944 __ms_va_start(ap, flags);
1945 pmode = va_arg(ap, int);
1947 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1950 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1953 /*********************************************************************
1956 int CDECL MSVCRT__creat(const char *path, int flags)
1958 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1959 return MSVCRT__open(path, usedFlags);
1962 /*********************************************************************
1963 * _wcreat (MSVCRT.@)
1965 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
1967 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1968 return MSVCRT__wopen(path, usedFlags);
1971 /*********************************************************************
1972 * _open_osfhandle (MSVCRT.@)
1974 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1978 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1979 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1980 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1981 * text - it never sets MSVCRT__O_BINARY.
1983 /* don't let split_oflags() decide the mode if no mode is passed */
1984 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1985 oflags |= MSVCRT__O_BINARY;
1987 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1988 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1992 /*********************************************************************
1995 int CDECL MSVCRT__rmtmp(void)
1997 int num_removed = 0, i;
2001 for (i = 3; i < MSVCRT_stream_idx; i++) {
2002 file = msvcrt_get_file(i);
2004 if (file->_tmpfname)
2006 MSVCRT_fclose(file);
2013 TRACE(":removed (%d) temp files\n",num_removed);
2017 /*********************************************************************
2020 * When reading \r as last character in text mode, read() positions
2021 * the file pointer on the \r character while getc() goes on to
2024 static int read_i(int fd, void *buf, unsigned int count)
2027 char *bufstart = buf;
2028 HANDLE hand = msvcrt_fdtoh(fd);
2029 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2034 if (fdinfo->wxflag & WX_READEOF) {
2035 fdinfo->wxflag |= WX_ATEOF;
2036 TRACE("already at EOF, returning 0\n");
2039 /* Don't trace small reads, it gets *very* annoying */
2041 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2042 if (hand == INVALID_HANDLE_VALUE)
2045 /* Reading single bytes in O_TEXT mode makes things slow
2046 * So read big chunks
2048 if (ReadFile(hand, bufstart, count, &num_read, NULL))
2050 if (count != 0 && num_read == 0)
2052 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2053 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2055 else if (fdinfo->wxflag & WX_TEXT)
2058 if (bufstart[num_read-1] == '\r')
2062 fdinfo->wxflag &= ~WX_READCR;
2063 ReadFile(hand, bufstart, 1, &num_read, NULL);
2067 fdinfo->wxflag |= WX_READCR;
2072 fdinfo->wxflag &= ~WX_READCR;
2073 for (i=0, j=0; i<num_read; i++)
2075 /* in text mode, a ctrl-z signals EOF */
2076 if (bufstart[i] == 0x1a)
2078 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2079 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2082 /* in text mode, strip \r if followed by \n.
2083 * BUG: should save state across calls somehow, so CR LF that
2084 * straddles buffer boundary gets recognized properly?
2086 if ((bufstart[i] != '\r')
2087 || ((i+1) < num_read && bufstart[i+1] != '\n'))
2088 bufstart[j++] = bufstart[i];
2095 if (GetLastError() == ERROR_BROKEN_PIPE)
2097 TRACE(":end-of-pipe\n");
2098 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2103 TRACE(":failed-last error (%d)\n",GetLastError());
2109 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2113 /*********************************************************************
2116 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2119 num_read = read_i(fd, buf, count);
2123 /*********************************************************************
2124 * _setmode (MSVCRT.@)
2126 int CDECL MSVCRT__setmode(int fd,int mode)
2128 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2129 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
2130 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
2131 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
2132 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2134 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2138 /*********************************************************************
2139 * _stat64 (MSVCRT.@)
2141 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2144 WIN32_FILE_ATTRIBUTE_DATA hfi;
2145 unsigned short mode = ALL_S_IREAD;
2148 TRACE(":file (%s) buf(%p)\n",path,buf);
2150 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2152 TRACE("failed (%d)\n",GetLastError());
2153 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2157 memset(buf,0,sizeof(struct MSVCRT__stat64));
2159 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2160 Bon 011120: This FIXME seems incorrect
2161 Also a letter as first char isn't enough to be classified
2164 if (isalpha(*path)&& (*(path+1)==':'))
2165 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2167 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2169 plen = strlen(path);
2171 /* Dir, or regular file? */
2172 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2173 (path[plen-1] == '\\'))
2174 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2177 mode |= MSVCRT__S_IFREG;
2179 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2181 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2182 (tolower(path[plen-3]) << 16);
2183 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2184 mode |= ALL_S_IEXEC;
2188 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2189 mode |= ALL_S_IWRITE;
2191 buf->st_mode = mode;
2193 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2194 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2196 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2197 buf->st_mtime = buf->st_ctime = dw;
2198 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2199 (int)(buf->st_size >> 32),(int)buf->st_size,
2200 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2204 /*********************************************************************
2205 * _stati64 (MSVCRT.@)
2207 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2210 struct MSVCRT__stat64 buf64;
2212 ret = MSVCRT_stat64(path, &buf64);
2214 msvcrt_stat64_to_stati64(&buf64, buf);
2218 /*********************************************************************
2221 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2223 struct MSVCRT__stat64 buf64;
2225 ret = MSVCRT_stat64( path, &buf64);
2227 msvcrt_stat64_to_stat(&buf64, buf);
2231 /*********************************************************************
2232 * _wstat64 (MSVCRT.@)
2234 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2237 WIN32_FILE_ATTRIBUTE_DATA hfi;
2238 unsigned short mode = ALL_S_IREAD;
2241 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2243 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2245 TRACE("failed (%d)\n",GetLastError());
2246 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2250 memset(buf,0,sizeof(struct MSVCRT__stat64));
2252 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2253 if (MSVCRT_iswalpha(*path))
2254 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2256 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2258 plen = strlenW(path);
2260 /* Dir, or regular file? */
2261 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2262 (path[plen-1] == '\\'))
2263 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2266 mode |= MSVCRT__S_IFREG;
2268 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2270 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2271 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2272 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2273 mode |= ALL_S_IEXEC;
2277 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2278 mode |= ALL_S_IWRITE;
2280 buf->st_mode = mode;
2282 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2283 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2285 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2286 buf->st_mtime = buf->st_ctime = dw;
2287 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2288 (int)(buf->st_size >> 32),(int)buf->st_size,
2289 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2293 /*********************************************************************
2294 * _wstati64 (MSVCRT.@)
2296 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2299 struct MSVCRT__stat64 buf64;
2301 ret = MSVCRT__wstat64(path, &buf64);
2303 msvcrt_stat64_to_stati64(&buf64, buf);
2307 /*********************************************************************
2310 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2313 struct MSVCRT__stat64 buf64;
2315 ret = MSVCRT__wstat64( path, &buf64 );
2316 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2320 /*********************************************************************
2323 MSVCRT_long CDECL MSVCRT__tell(int fd)
2325 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2328 /*********************************************************************
2329 * _telli64 (MSVCRT.@)
2331 __int64 CDECL _telli64(int fd)
2333 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2336 /*********************************************************************
2337 * _tempnam (MSVCRT.@)
2339 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
2341 char tmpbuf[MAX_PATH];
2342 const char *tmp_dir = MSVCRT_getenv("TMP");
2344 if (tmp_dir) dir = tmp_dir;
2346 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2347 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2349 TRACE("got name (%s)\n",tmpbuf);
2350 DeleteFileA(tmpbuf);
2351 return MSVCRT__strdup(tmpbuf);
2353 TRACE("failed (%d)\n",GetLastError());
2357 /*********************************************************************
2358 * _wtempnam (MSVCRT.@)
2360 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2362 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2364 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2365 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2367 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2368 DeleteFileW(tmpbuf);
2369 return MSVCRT__wcsdup(tmpbuf);
2371 TRACE("failed (%d)\n",GetLastError());
2375 /*********************************************************************
2378 int CDECL MSVCRT__umask(int umask)
2380 int old_umask = MSVCRT_umask;
2381 TRACE("(%d)\n",umask);
2382 MSVCRT_umask = umask;
2386 /*********************************************************************
2387 * _utime64 (MSVCRT.@)
2389 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2391 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2395 int retVal = _futime64(fd, t);
2402 /*********************************************************************
2403 * _utime32 (MSVCRT.@)
2405 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2409 struct MSVCRT___utimbuf64 t64;
2410 t64.actime = t->actime;
2411 t64.modtime = t->modtime;
2412 return _utime64( path, &t64 );
2415 return _utime64( path, NULL );
2418 /*********************************************************************
2422 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2424 return _utime64( path, t );
2427 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2429 return _utime32( path, t );
2433 /*********************************************************************
2434 * _wutime64 (MSVCRT.@)
2436 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2438 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2442 int retVal = _futime64(fd, t);
2449 /*********************************************************************
2450 * _wutime32 (MSVCRT.@)
2452 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2456 struct MSVCRT___utimbuf64 t64;
2457 t64.actime = t->actime;
2458 t64.modtime = t->modtime;
2459 return _wutime64( path, &t64 );
2462 return _wutime64( path, NULL );
2465 /*********************************************************************
2466 * _wutime (MSVCRT.@)
2469 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2471 return _wutime64( path, t );
2474 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2476 return _wutime32( path, t );
2480 /*********************************************************************
2483 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2486 HANDLE hand = msvcrt_fdtoh(fd);
2488 /* Don't trace small writes, it gets *very* annoying */
2491 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2493 if (hand == INVALID_HANDLE_VALUE)
2495 *MSVCRT__errno() = MSVCRT_EBADF;
2499 /* If appending, go to EOF */
2500 if (msvcrt_get_ioinfo(fd)->wxflag & WX_APPEND)
2501 MSVCRT__lseek(fd, 0, FILE_END);
2503 if (!(msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT))
2505 if (WriteFile(hand, buf, count, &num_written, NULL)
2506 && (num_written == count))
2508 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2509 hand, GetLastError());
2510 *MSVCRT__errno() = MSVCRT_ENOSPC;
2514 unsigned int i, j, nr_lf;
2517 const char *s = buf, *buf_start = buf;
2518 /* find number of \n ( without preceding \r ) */
2519 for ( nr_lf=0,i = 0; i <count; i++)
2524 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2529 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2531 for (s = buf, i = 0, j = 0; i < count; i++)
2536 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2543 FIXME("Malloc failed\n");
2551 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2553 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2554 fd, hand, GetLastError(), num_written);
2555 *MSVCRT__errno() = MSVCRT_ENOSPC;
2558 return s - buf_start;
2570 /*********************************************************************
2573 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2577 MSVCRT__lock_file(file);
2578 len = MSVCRT__write(file->_file, &val, sizeof(val));
2579 if (len == sizeof(val)) {
2580 MSVCRT__unlock_file(file);
2584 file->_flag |= MSVCRT__IOERR;
2585 MSVCRT__unlock_file(file);
2589 /*********************************************************************
2592 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2596 MSVCRT__lock_file(file);
2598 MSVCRT_free(file->_tmpfname);
2599 file->_tmpfname = NULL;
2600 /* flush stdio buffers */
2601 if(file->_flag & MSVCRT__IOWRT)
2602 MSVCRT_fflush(file);
2603 if(file->_flag & MSVCRT__IOMYBUF)
2604 MSVCRT_free(file->_base);
2606 r=MSVCRT__close(file->_file);
2609 MSVCRT__unlock_file(file);
2610 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
2611 DeleteCriticalSection(&((file_crit*)file)->crit);
2613 if(file == msvcrt_get_file(MSVCRT_stream_idx-1)) {
2614 while(MSVCRT_stream_idx>3 && !file->_flag) {
2615 MSVCRT_stream_idx--;
2616 file = msvcrt_get_file(MSVCRT_stream_idx-1);
2620 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2623 /*********************************************************************
2626 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2630 MSVCRT__lock_file(file);
2631 ret = file->_flag & MSVCRT__IOEOF;
2632 MSVCRT__unlock_file(file);
2637 /*********************************************************************
2640 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2644 MSVCRT__lock_file(file);
2645 ret = file->_flag & MSVCRT__IOERR;
2646 MSVCRT__unlock_file(file);
2651 /*********************************************************************
2652 * _filbuf (MSVCRT.@)
2654 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2657 MSVCRT__lock_file(file);
2659 /* Allocate buffer if needed */
2660 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
2661 msvcrt_alloc_buffer(file);
2663 if(!(file->_flag & MSVCRT__IOREAD)) {
2664 if(file->_flag & MSVCRT__IORW)
2665 file->_flag |= MSVCRT__IOREAD;
2667 MSVCRT__unlock_file(file);
2672 if(file->_flag & MSVCRT__IONBF) {
2674 if ((r = read_i(file->_file,&c,1)) != 1) {
2675 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2676 MSVCRT__unlock_file(file);
2680 MSVCRT__unlock_file(file);
2683 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2685 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2687 MSVCRT__unlock_file(file);
2692 file->_ptr = file->_base+1;
2693 c = *(unsigned char *)file->_base;
2694 MSVCRT__unlock_file(file);
2699 /*********************************************************************
2702 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2707 MSVCRT__lock_file(file);
2710 i = (unsigned char *)file->_ptr++;
2713 j = MSVCRT__filbuf(file);
2715 MSVCRT__unlock_file(file);
2719 /*********************************************************************
2720 * _fgetchar (MSVCRT.@)
2722 int CDECL MSVCRT__fgetchar(void)
2724 return MSVCRT_fgetc(MSVCRT_stdin);
2727 /*********************************************************************
2730 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2732 int cc = MSVCRT_EOF;
2733 char * buf_start = s;
2735 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2736 file,file->_file,s,size);
2738 MSVCRT__lock_file(file);
2740 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2745 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2747 TRACE(":nothing read\n");
2748 MSVCRT__unlock_file(file);
2751 if ((cc != MSVCRT_EOF) && (size > 1))
2754 TRACE(":got %s\n", debugstr_a(buf_start));
2755 MSVCRT__unlock_file(file);
2759 /*********************************************************************
2762 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2763 * the CR from CR/LF combinations
2765 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2769 MSVCRT__lock_file(file);
2770 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
2777 for(i=0; i<sizeof(wc); i++)
2787 j = MSVCRT__filbuf(file);
2790 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2793 MSVCRT__unlock_file(file);
2800 MSVCRT__unlock_file(file);
2804 c = MSVCRT_fgetc(file);
2805 if ((get_locinfo()->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2807 FIXME("Treat Multibyte characters\n");
2810 MSVCRT__unlock_file(file);
2811 if (c == MSVCRT_EOF)
2814 return (MSVCRT_wint_t)c;
2817 /*********************************************************************
2820 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2827 MSVCRT__lock_file(file);
2828 for (j=0; j<sizeof(int); j++) {
2829 k = MSVCRT_fgetc(file);
2830 if (k == MSVCRT_EOF) {
2831 file->_flag |= MSVCRT__IOEOF;
2832 MSVCRT__unlock_file(file);
2838 MSVCRT__unlock_file(file);
2842 /*********************************************************************
2845 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2847 return MSVCRT_fgetwc(file);
2850 /*********************************************************************
2851 * _fgetwchar (MSVCRT.@)
2853 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
2855 return MSVCRT_fgetwc(MSVCRT_stdin);
2858 /*********************************************************************
2859 * getwchar (MSVCRT.@)
2861 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2863 return MSVCRT__fgetwchar();
2866 /*********************************************************************
2869 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2871 int cc = MSVCRT_WEOF;
2872 MSVCRT_wchar_t * buf_start = s;
2874 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2875 file,file->_file,s,size);
2877 MSVCRT__lock_file(file);
2879 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2884 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2886 TRACE(":nothing read\n");
2887 MSVCRT__unlock_file(file);
2890 if ((cc != MSVCRT_WEOF) && (size > 1))
2893 TRACE(":got %s\n", debugstr_w(buf_start));
2894 MSVCRT__unlock_file(file);
2898 /*********************************************************************
2901 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2903 MSVCRT_size_t wrcnt=size * nmemb;
2908 MSVCRT__lock_file(file);
2910 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2911 memcpy(file->_ptr, ptr, pcnt);
2916 ptr = (const char*)ptr + pcnt;
2917 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2918 if(file->_flag & MSVCRT__IORW) {
2919 file->_flag |= MSVCRT__IOWRT;
2921 MSVCRT__unlock_file(file);
2927 int res=msvcrt_flush_buffer(file);
2929 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2932 file->_flag |= MSVCRT__IOERR;
2935 written += pwritten;
2939 MSVCRT__unlock_file(file);
2940 return written / size;
2943 /*********************************************************************
2946 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2948 MSVCRT_wchar_t mwc=wc;
2949 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2954 /*********************************************************************
2955 * _fputwchar (MSVCRT.@)
2957 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
2959 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2962 /*********************************************************************
2963 * _wfsopen (MSVCRT.@)
2965 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2968 int open_flags, stream_flags, fd;
2970 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2972 /* map mode string to open() flags. "man fopen" for possibilities. */
2973 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2977 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2980 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2982 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2989 TRACE(":got (%p)\n",file);
2990 if (fd >= 0 && !file)
2996 /*********************************************************************
2997 * _fsopen (MSVCRT.@)
2999 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3002 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3004 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3005 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3006 *MSVCRT__errno() = MSVCRT_EINVAL;
3009 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3012 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3013 *MSVCRT__errno() = MSVCRT_EINVAL;
3017 ret = MSVCRT__wfsopen(pathW, modeW, share);
3024 /*********************************************************************
3027 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3029 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3032 /*********************************************************************
3033 * fopen_s (MSVCRT.@)
3035 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3036 const char *filename, const char *mode)
3038 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
3039 !MSVCRT_CHECK_PMT(mode != NULL)) {
3040 *MSVCRT__errno() = MSVCRT_EINVAL;
3041 return MSVCRT_EINVAL;
3044 *pFile = MSVCRT_fopen(filename, mode);
3047 return *MSVCRT__errno();
3051 /*********************************************************************
3052 * _wfopen (MSVCRT.@)
3054 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3056 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3059 /*********************************************************************
3060 * _wfopen_s (MSVCRT.@)
3062 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3063 const MSVCRT_wchar_t *mode)
3065 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
3066 !MSVCRT_CHECK_PMT(mode != NULL)) {
3067 *MSVCRT__errno() = MSVCRT_EINVAL;
3068 return MSVCRT_EINVAL;
3071 *pFile = MSVCRT__wfopen(filename, mode);
3074 return *MSVCRT__errno();
3078 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
3079 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
3081 /*********************************************************************
3084 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3088 MSVCRT__lock_file(file);
3094 res = msvcrt_flush_buffer(file);
3095 MSVCRT__unlock_file(file);
3096 return res ? res : c;
3099 MSVCRT__unlock_file(file);
3103 res = MSVCRT__flsbuf(c, file);
3104 MSVCRT__unlock_file(file);
3109 /*********************************************************************
3110 * _flsbuf (MSVCRT.@)
3112 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3114 MSVCRT__lock_file(file);
3116 /* Flush output buffer */
3117 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3118 msvcrt_alloc_buffer(file);
3120 if(!(file->_flag & MSVCRT__IOWRT)) {
3121 if(file->_flag & MSVCRT__IORW) {
3122 file->_flag |= MSVCRT__IOWRT;
3124 MSVCRT__unlock_file(file);
3129 int res=msvcrt_flush_buffer(file);
3131 res = MSVCRT_fputc(c, file);
3132 MSVCRT__unlock_file(file);
3137 /* set _cnt to 0 for unbuffered FILEs */
3139 len = MSVCRT__write(file->_file, &cc, 1);
3141 MSVCRT__unlock_file(file);
3144 file->_flag |= MSVCRT__IOERR;
3145 MSVCRT__unlock_file(file);
3150 /*********************************************************************
3151 * _fputchar (MSVCRT.@)
3153 int CDECL MSVCRT__fputchar(int c)
3155 return MSVCRT_fputc(c, MSVCRT_stdout);
3158 /*********************************************************************
3161 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3163 MSVCRT_size_t rcnt=size * nmemb;
3164 MSVCRT_size_t read=0;
3170 MSVCRT__lock_file(file);
3172 /* first buffered data */
3174 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3175 memcpy(ptr, file->_ptr, pcnt);
3180 ptr = (char*)ptr + pcnt;
3181 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3182 if(file->_flag & MSVCRT__IORW) {
3183 file->_flag |= MSVCRT__IOREAD;
3185 MSVCRT__unlock_file(file);
3192 /* Fill the buffer on small reads.
3193 * TODO: Use a better buffering strategy.
3195 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
3196 if (file->_bufsiz == 0) {
3197 msvcrt_alloc_buffer(file);
3199 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3200 file->_ptr = file->_base;
3201 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3202 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3203 if (i > 0 && i < file->_cnt) {
3204 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3205 file->_flag &= ~MSVCRT__IOEOF;
3208 memcpy(ptr, file->_ptr, i);
3213 i = MSVCRT__read(file->_file,ptr, rcnt);
3217 ptr = (char *)ptr+i;
3218 /* expose feof condition in the flags
3219 * MFC tests file->_flag for feof, and doesn't call feof())
3221 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3222 file->_flag |= MSVCRT__IOEOF;
3225 file->_flag |= MSVCRT__IOERR;
3232 MSVCRT__unlock_file(file);
3236 /*********************************************************************
3237 * _wfreopen (MSVCRT.@)
3240 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3242 int open_flags, stream_flags, fd;
3244 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3247 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3251 MSVCRT_fclose(file);
3252 /* map mode string to open() flags. "man fopen" for possibilities. */
3253 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3257 fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3260 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
3263 WARN(":failed-last error (%d)\n",GetLastError());
3264 msvcrt_set_errno(GetLastError());
3273 /*********************************************************************
3274 * freopen (MSVCRT.@)
3277 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3280 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3282 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3283 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3289 ret = MSVCRT__wfreopen(pathW, modeW, file);
3296 /*********************************************************************
3297 * fsetpos (MSVCRT.@)
3299 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3303 MSVCRT__lock_file(file);
3304 /* Note that all this has been lifted 'as is' from fseek */
3305 if(file->_flag & MSVCRT__IOWRT)
3306 msvcrt_flush_buffer(file);
3308 /* Discard buffered input */
3310 file->_ptr = file->_base;
3312 /* Reset direction of i/o */
3313 if(file->_flag & MSVCRT__IORW) {
3314 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3317 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3318 MSVCRT__unlock_file(file);
3322 /*********************************************************************
3323 * _ftelli64 (MSVCRT.@)
3325 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3327 /* TODO: just call fgetpos and return lower half of result */
3331 MSVCRT__lock_file(file);
3332 pos = _telli64(file->_file);
3334 MSVCRT__unlock_file(file);
3338 if( file->_flag & MSVCRT__IOWRT ) {
3339 off = file->_ptr - file->_base;
3342 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3343 /* Black magic correction for CR removal */
3345 for (i=0; i<file->_cnt; i++) {
3346 if (file->_ptr[i] == '\n')
3349 /* Black magic when reading CR at buffer boundary*/
3350 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3356 MSVCRT__unlock_file(file);
3360 /*********************************************************************
3363 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3365 return MSVCRT__ftelli64(file);
3368 /*********************************************************************
3369 * fgetpos (MSVCRT.@)
3371 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3375 MSVCRT__lock_file(file);
3376 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3378 MSVCRT__unlock_file(file);
3382 if( file->_flag & MSVCRT__IOWRT ) {
3383 off = file->_ptr - file->_base;
3386 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3387 /* Black magic correction for CR removal */
3389 for (i=0; i<file->_cnt; i++) {
3390 if (file->_ptr[i] == '\n')
3393 /* Black magic when reading CR at buffer boundary*/
3394 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3400 MSVCRT__unlock_file(file);
3404 /*********************************************************************
3407 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3409 MSVCRT_size_t i, len = strlen(s);
3412 MSVCRT__lock_file(file);
3413 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3414 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3415 MSVCRT__unlock_file(file);
3418 for (i=0; i<len; i++)
3419 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF) {
3420 MSVCRT__unlock_file(file);
3424 MSVCRT__unlock_file(file);
3428 /*********************************************************************
3431 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3433 MSVCRT_size_t i, len = strlenW(s);
3436 MSVCRT__lock_file(file);
3437 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3438 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3439 MSVCRT__unlock_file(file);
3442 for (i=0; i<len; i++) {
3443 if (((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3444 || MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
3445 MSVCRT__unlock_file(file);
3450 MSVCRT__unlock_file(file);
3454 /*********************************************************************
3455 * getchar (MSVCRT.@)
3457 int CDECL MSVCRT_getchar(void)
3459 return MSVCRT_fgetc(MSVCRT_stdin);
3462 /*********************************************************************
3465 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3467 return MSVCRT_fgetc(file);
3470 /*********************************************************************
3473 char * CDECL MSVCRT_gets(char *buf)
3476 char * buf_start = buf;
3478 MSVCRT__lock_file(MSVCRT_stdin);
3479 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3480 cc = MSVCRT_fgetc(MSVCRT_stdin))
3481 if(cc != '\r') *buf++ = (char)cc;
3485 TRACE("got '%s'\n", buf_start);
3486 MSVCRT__unlock_file(MSVCRT_stdin);
3490 /*********************************************************************
3493 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3496 MSVCRT_wchar_t* ws = buf;
3498 MSVCRT__lock_file(MSVCRT_stdin);
3499 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3500 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3503 *buf++ = (MSVCRT_wchar_t)cc;
3507 TRACE("got %s\n", debugstr_w(ws));
3508 MSVCRT__unlock_file(MSVCRT_stdin);
3512 /*********************************************************************
3515 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3517 return MSVCRT_fputc(c, file);
3520 /*********************************************************************
3521 * putchar (MSVCRT.@)
3523 int CDECL MSVCRT_putchar(int c)
3525 return MSVCRT_fputc(c, MSVCRT_stdout);
3528 /*********************************************************************
3529 * _putwch (MSVCRT.@)
3531 int CDECL MSVCRT__putwch(int c)
3533 return MSVCRT_fputwc(c, MSVCRT_stdout);
3536 /*********************************************************************
3539 int CDECL MSVCRT_puts(const char *s)
3541 MSVCRT_size_t len = strlen(s);
3544 MSVCRT__lock_file(MSVCRT_stdout);
3545 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3546 MSVCRT__unlock_file(MSVCRT_stdout);
3550 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3551 MSVCRT__unlock_file(MSVCRT_stdout);
3555 /*********************************************************************
3558 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
3560 static const MSVCRT_wchar_t nl = '\n';
3561 MSVCRT_size_t len = strlenW(s);
3564 MSVCRT__lock_file(MSVCRT_stdout);
3565 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3566 MSVCRT__unlock_file(MSVCRT_stdout);
3570 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3571 MSVCRT__unlock_file(MSVCRT_stdout);
3575 /*********************************************************************
3578 int CDECL MSVCRT_remove(const char *path)
3580 TRACE("(%s)\n",path);
3581 if (DeleteFileA(path))
3583 TRACE(":failed (%d)\n",GetLastError());
3584 msvcrt_set_errno(GetLastError());
3588 /*********************************************************************
3589 * _wremove (MSVCRT.@)
3591 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
3593 TRACE("(%s)\n",debugstr_w(path));
3594 if (DeleteFileW(path))
3596 TRACE(":failed (%d)\n",GetLastError());
3597 msvcrt_set_errno(GetLastError());
3601 /*********************************************************************
3604 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3606 TRACE(":from %s to %s\n",oldpath,newpath);
3607 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3609 TRACE(":failed (%d)\n",GetLastError());
3610 msvcrt_set_errno(GetLastError());
3614 /*********************************************************************
3615 * _wrename (MSVCRT.@)
3617 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3619 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3620 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3622 TRACE(":failed (%d)\n",GetLastError());
3623 msvcrt_set_errno(GetLastError());
3627 /*********************************************************************
3628 * setvbuf (MSVCRT.@)
3630 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3632 MSVCRT__lock_file(file);
3634 MSVCRT_free(file->_base);
3638 if(mode == MSVCRT__IOFBF) {
3639 file->_flag &= ~MSVCRT__IONBF;
3640 file->_base = file->_ptr = buf;
3642 file->_bufsiz = size;
3645 file->_flag |= MSVCRT__IONBF;
3647 MSVCRT__unlock_file(file);
3651 /*********************************************************************
3654 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3656 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3659 /*********************************************************************
3662 char * CDECL MSVCRT_tmpnam(char *s)
3669 thread_data_t *data = msvcrt_get_thread_data();
3671 if(!data->tmpnam_buffer)
3672 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
3674 s = data->tmpnam_buffer;
3677 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3678 p = s + sprintf(s, "\\s%s.", tmpstr);
3679 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3681 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
3682 memcpy(p, tmpstr, size);
3684 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3685 GetLastError() == ERROR_FILE_NOT_FOUND)
3691 /*********************************************************************
3692 * _wtmpnam (MSVCRT.@)
3694 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
3696 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
3697 MSVCRT_wchar_t tmpstr[16];
3701 thread_data_t *data = msvcrt_get_thread_data();
3703 if(!data->wtmpnam_buffer)
3704 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
3706 s = data->wtmpnam_buffer;
3709 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
3710 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
3711 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3713 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
3714 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
3716 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
3717 GetLastError() == ERROR_FILE_NOT_FOUND)
3723 /*********************************************************************
3724 * tmpfile (MSVCRT.@)
3726 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3728 char *filename = MSVCRT_tmpnam(NULL);
3730 MSVCRT_FILE* file = NULL;
3733 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3734 if (fd != -1 && (file = msvcrt_alloc_fp()))
3736 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3741 else file->_tmpfname = MSVCRT__strdup(filename);
3747 static int puts_clbk_file_a(void *file, int len, const char *str)
3749 return MSVCRT_fwrite(str, sizeof(char), len, file);
3752 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
3754 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
3757 /*********************************************************************
3758 * vfprintf (MSVCRT.@)
3760 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3764 MSVCRT__lock_file(file);
3765 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3766 MSVCRT__unlock_file(file);
3771 /*********************************************************************
3772 * vfprintf_s (MSVCRT.@)
3774 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3778 if(!MSVCRT_CHECK_PMT(file != NULL)) {
3779 *MSVCRT__errno() = MSVCRT_EINVAL;
3783 MSVCRT__lock_file(file);
3784 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3785 MSVCRT__unlock_file(file);
3790 /*********************************************************************
3791 * vfwprintf (MSVCRT.@)
3793 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3797 MSVCRT__lock_file(file);
3798 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3799 MSVCRT__unlock_file(file);
3804 /*********************************************************************
3805 * vfwprintf_s (MSVCRT.@)
3807 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3811 if(!MSVCRT_CHECK_PMT( file != NULL)) {
3812 *MSVCRT__errno() = MSVCRT_EINVAL;
3816 MSVCRT__lock_file(file);
3817 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3818 MSVCRT__unlock_file(file);
3823 /*********************************************************************
3824 * vprintf (MSVCRT.@)
3826 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3828 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3831 /*********************************************************************
3832 * vprintf_s (MSVCRT.@)
3834 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
3836 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
3839 /*********************************************************************
3840 * vwprintf (MSVCRT.@)
3842 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3844 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3847 /*********************************************************************
3848 * vwprintf_s (MSVCRT.@)
3850 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
3852 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
3855 /*********************************************************************
3856 * fprintf (MSVCRT.@)
3858 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3860 __ms_va_list valist;
3862 __ms_va_start(valist, format);
3863 res = MSVCRT_vfprintf(file, format, valist);
3864 __ms_va_end(valist);
3868 /*********************************************************************
3869 * fprintf_s (MSVCRT.@)
3871 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
3873 __ms_va_list valist;
3875 __ms_va_start(valist, format);
3876 res = MSVCRT_vfprintf_s(file, format, valist);
3877 __ms_va_end(valist);
3881 /*********************************************************************
3882 * fwprintf (MSVCRT.@)
3884 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3886 __ms_va_list valist;
3888 __ms_va_start(valist, format);
3889 res = MSVCRT_vfwprintf(file, format, valist);
3890 __ms_va_end(valist);
3894 /*********************************************************************
3895 * fwprintf_s (MSVCRT.@)
3897 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3899 __ms_va_list valist;
3901 __ms_va_start(valist, format);
3902 res = MSVCRT_vfwprintf_s(file, format, valist);
3903 __ms_va_end(valist);
3907 /*********************************************************************
3910 int CDECL MSVCRT_printf(const char *format, ...)
3912 __ms_va_list valist;
3914 __ms_va_start(valist, format);
3915 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3916 __ms_va_end(valist);
3920 /*********************************************************************
3921 * printf_s (MSVCRT.@)
3923 int CDECL MSVCRT_printf_s(const char *format, ...)
3925 __ms_va_list valist;
3927 __ms_va_start(valist, format);
3928 res = MSVCRT_vprintf_s(format, valist);
3929 __ms_va_end(valist);
3933 /*********************************************************************
3936 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3938 if (c == MSVCRT_EOF)
3941 MSVCRT__lock_file(file);
3942 if(file->_bufsiz == 0) {
3943 msvcrt_alloc_buffer(file);
3946 if(file->_ptr>file->_base) {
3950 MSVCRT_clearerr(file);
3951 MSVCRT__unlock_file(file);
3955 MSVCRT__unlock_file(file);
3959 /*********************************************************************
3960 * ungetwc (MSVCRT.@)
3962 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3964 MSVCRT_wchar_t mwc = wc;
3965 char * pp = (char *)&mwc;
3968 MSVCRT__lock_file(file);
3969 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3970 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
3971 MSVCRT__unlock_file(file);
3976 MSVCRT__unlock_file(file);
3980 /*********************************************************************
3981 * wprintf (MSVCRT.@)
3983 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3985 __ms_va_list valist;
3987 __ms_va_start(valist, format);
3988 res = MSVCRT_vwprintf(format, valist);
3989 __ms_va_end(valist);
3993 /*********************************************************************
3994 * wprintf_s (MSVCRT.@)
3996 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
3998 __ms_va_list valist;
4000 __ms_va_start(valist, format);
4001 res = MSVCRT_vwprintf_s(format, valist);
4002 __ms_va_end(valist);
4006 /*********************************************************************
4007 * _getmaxstdio (MSVCRT.@)
4009 int CDECL MSVCRT__getmaxstdio(void)
4011 return MSVCRT_max_streams;
4014 /*********************************************************************
4015 * _setmaxstdio (MSVCRT.@)
4017 int CDECL MSVCRT__setmaxstdio(int newmax)
4019 TRACE("%d\n", newmax);
4021 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4024 MSVCRT_max_streams = newmax;
4025 return MSVCRT_max_streams;