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 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 (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 MSVCRT__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 MSVCRT__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)
1482 struct MSVCRT___utimbuf64 t64;
1483 t64.actime = t->actime;
1484 t64.modtime = t->modtime;
1485 return _futime64( fd, &t64 );
1488 return _futime64( fd, NULL );
1491 /*********************************************************************
1492 * _futime (MSVCRT.@)
1495 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1497 return _futime64( fd, t );
1500 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1502 return _futime32( fd, t );
1506 /*********************************************************************
1507 * _get_osfhandle (MSVCRT.@)
1509 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1511 HANDLE hand = msvcrt_fdtoh(fd);
1512 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1514 return (MSVCRT_intptr_t)hand;
1517 /*********************************************************************
1518 * _isatty (MSVCRT.@)
1520 int CDECL MSVCRT__isatty(int fd)
1522 HANDLE hand = msvcrt_fdtoh(fd);
1524 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1525 if (hand == INVALID_HANDLE_VALUE)
1528 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1531 /*********************************************************************
1532 * _mktemp (MSVCRT.@)
1534 char * CDECL MSVCRT__mktemp(char *pattern)
1537 char *retVal = pattern;
1542 numX = (*pattern++ == 'X')? numX + 1 : 0;
1546 id = GetCurrentProcessId();
1550 int tempNum = id / 10;
1551 *pattern-- = id - (tempNum * 10) + '0';
1557 *pattern = letter++;
1558 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1559 GetLastError() == ERROR_FILE_NOT_FOUND)
1561 } while(letter <= 'z');
1565 /*********************************************************************
1566 * _wmktemp (MSVCRT.@)
1568 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1571 MSVCRT_wchar_t *retVal = pattern;
1573 MSVCRT_wchar_t letter = 'a';
1576 numX = (*pattern++ == 'X')? numX + 1 : 0;
1580 id = GetCurrentProcessId();
1584 int tempNum = id / 10;
1585 *pattern-- = id - (tempNum * 10) + '0';
1591 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1592 GetLastError() == ERROR_FILE_NOT_FOUND)
1594 *pattern = letter++;
1595 } while(letter != '|');
1599 static unsigned split_oflags(unsigned oflags)
1602 unsigned unsupp; /* until we support everything */
1604 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1605 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1606 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1607 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1608 else wxflags |= WX_TEXT; /* default to TEXT*/
1609 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1611 if ((unsupp = oflags & ~(
1612 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1613 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1614 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1615 MSVCRT__O_NOINHERIT|
1616 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1618 ERR(":unsupported oflags 0x%04x\n",unsupp);
1623 /*********************************************************************
1626 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1629 SECURITY_ATTRIBUTES sa;
1630 HANDLE readHandle, writeHandle;
1634 *MSVCRT__errno() = MSVCRT_EINVAL;
1638 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1639 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1640 sa.lpSecurityDescriptor = NULL;
1641 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1643 unsigned int wxflags = split_oflags(textmode);
1647 fd = msvcrt_alloc_fd(readHandle, wxflags);
1651 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1659 MSVCRT__close(pfds[0]);
1660 CloseHandle(writeHandle);
1661 *MSVCRT__errno() = MSVCRT_EMFILE;
1666 CloseHandle(readHandle);
1667 CloseHandle(writeHandle);
1668 *MSVCRT__errno() = MSVCRT_EMFILE;
1673 msvcrt_set_errno(GetLastError());
1678 /*********************************************************************
1679 * _sopen_s (MSVCRT.@)
1681 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1683 DWORD access = 0, creation = 0, attrib;
1687 SECURITY_ATTRIBUTES sa;
1689 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1690 fd, path, oflags, shflags, pmode);
1694 MSVCRT_INVALID_PMT("null out fd pointer");
1695 *MSVCRT__errno() = MSVCRT_EINVAL;
1696 return MSVCRT_EINVAL;
1700 wxflag = split_oflags(oflags);
1701 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1703 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1704 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1705 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1708 if (oflags & MSVCRT__O_CREAT)
1710 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1711 FIXME(": pmode 0x%04x ignored\n", pmode);
1713 WARN(": pmode 0x%04x ignored\n", pmode);
1715 if (oflags & MSVCRT__O_EXCL)
1716 creation = CREATE_NEW;
1717 else if (oflags & MSVCRT__O_TRUNC)
1718 creation = CREATE_ALWAYS;
1720 creation = OPEN_ALWAYS;
1722 else /* no MSVCRT__O_CREAT */
1724 if (oflags & MSVCRT__O_TRUNC)
1725 creation = TRUNCATE_EXISTING;
1727 creation = OPEN_EXISTING;
1732 case MSVCRT__SH_DENYRW:
1735 case MSVCRT__SH_DENYWR:
1736 sharing = FILE_SHARE_READ;
1738 case MSVCRT__SH_DENYRD:
1739 sharing = FILE_SHARE_WRITE;
1741 case MSVCRT__SH_DENYNO:
1742 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1745 ERR( "Unhandled shflags 0x%x\n", shflags );
1746 return MSVCRT_EINVAL;
1748 attrib = FILE_ATTRIBUTE_NORMAL;
1750 if (oflags & MSVCRT__O_TEMPORARY)
1752 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1754 sharing |= FILE_SHARE_DELETE;
1757 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1758 sa.lpSecurityDescriptor = NULL;
1759 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1761 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1762 if (hand == INVALID_HANDLE_VALUE) {
1763 WARN(":failed-last error (%d)\n", GetLastError());
1764 msvcrt_set_errno(GetLastError());
1765 return *MSVCRT__errno();
1768 *fd = msvcrt_alloc_fd(hand, wxflag);
1770 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1774 /*********************************************************************
1777 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1782 if (oflags & MSVCRT__O_CREAT)
1786 __ms_va_start(ap, shflags);
1787 pmode = va_arg(ap, int);
1793 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
1797 /*********************************************************************
1798 * _wsopen_s (MSVCRT.@)
1800 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1802 DWORD access = 0, creation = 0, attrib;
1803 SECURITY_ATTRIBUTES sa;
1808 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1809 fd, debugstr_w(path), oflags, shflags, pmode);
1813 MSVCRT_INVALID_PMT("null out fd pointer");
1814 *MSVCRT__errno() = MSVCRT_EINVAL;
1815 return MSVCRT_EINVAL;
1819 wxflag = split_oflags(oflags);
1820 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1822 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1823 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1824 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1827 if (oflags & MSVCRT__O_CREAT)
1829 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1830 FIXME(": pmode 0x%04x ignored\n", pmode);
1832 WARN(": pmode 0x%04x ignored\n", pmode);
1834 if (oflags & MSVCRT__O_EXCL)
1835 creation = CREATE_NEW;
1836 else if (oflags & MSVCRT__O_TRUNC)
1837 creation = CREATE_ALWAYS;
1839 creation = OPEN_ALWAYS;
1841 else /* no MSVCRT__O_CREAT */
1843 if (oflags & MSVCRT__O_TRUNC)
1844 creation = TRUNCATE_EXISTING;
1846 creation = OPEN_EXISTING;
1851 case MSVCRT__SH_DENYRW:
1854 case MSVCRT__SH_DENYWR:
1855 sharing = FILE_SHARE_READ;
1857 case MSVCRT__SH_DENYRD:
1858 sharing = FILE_SHARE_WRITE;
1860 case MSVCRT__SH_DENYNO:
1861 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1864 ERR( "Unhandled shflags 0x%x\n", shflags );
1865 return MSVCRT_EINVAL;
1867 attrib = FILE_ATTRIBUTE_NORMAL;
1869 if (oflags & MSVCRT__O_TEMPORARY)
1871 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1873 sharing |= FILE_SHARE_DELETE;
1876 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1877 sa.lpSecurityDescriptor = NULL;
1878 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1880 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1882 if (hand == INVALID_HANDLE_VALUE) {
1883 WARN(":failed-last error (%d)\n",GetLastError());
1884 msvcrt_set_errno(GetLastError());
1885 msvcrt_set_errno(GetLastError());
1886 return *MSVCRT__errno();
1889 *fd = msvcrt_alloc_fd(hand, wxflag);
1891 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1895 /*********************************************************************
1896 * _wsopen (MSVCRT.@)
1898 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1903 if (oflags & MSVCRT__O_CREAT)
1907 __ms_va_start(ap, shflags);
1908 pmode = va_arg(ap, int);
1914 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1918 /*********************************************************************
1921 int CDECL MSVCRT__open( const char *path, int flags, ... )
1925 if (flags & MSVCRT__O_CREAT)
1928 __ms_va_start(ap, flags);
1929 pmode = va_arg(ap, int);
1931 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1934 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1937 /*********************************************************************
1940 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
1944 if (flags & MSVCRT__O_CREAT)
1947 __ms_va_start(ap, flags);
1948 pmode = va_arg(ap, int);
1950 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1953 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1956 /*********************************************************************
1959 int CDECL MSVCRT__creat(const char *path, int flags)
1961 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1962 return MSVCRT__open(path, usedFlags);
1965 /*********************************************************************
1966 * _wcreat (MSVCRT.@)
1968 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
1970 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1971 return MSVCRT__wopen(path, usedFlags);
1974 /*********************************************************************
1975 * _open_osfhandle (MSVCRT.@)
1977 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1981 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1982 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1983 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1984 * text - it never sets MSVCRT__O_BINARY.
1986 /* don't let split_oflags() decide the mode if no mode is passed */
1987 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1988 oflags |= MSVCRT__O_BINARY;
1990 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1991 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1995 /*********************************************************************
1998 int CDECL MSVCRT__rmtmp(void)
2000 int num_removed = 0, i;
2004 for (i = 3; i < MSVCRT_stream_idx; i++) {
2005 file = msvcrt_get_file(i);
2007 if (file->_tmpfname)
2009 MSVCRT_fclose(file);
2016 TRACE(":removed (%d) temp files\n",num_removed);
2020 /*********************************************************************
2023 * When reading \r as last character in text mode, read() positions
2024 * the file pointer on the \r character while getc() goes on to
2027 static int read_i(int fd, void *buf, unsigned int count)
2030 char *bufstart = buf;
2031 HANDLE hand = msvcrt_fdtoh(fd);
2032 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2037 if (fdinfo->wxflag & WX_READEOF) {
2038 fdinfo->wxflag |= WX_ATEOF;
2039 TRACE("already at EOF, returning 0\n");
2042 /* Don't trace small reads, it gets *very* annoying */
2044 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2045 if (hand == INVALID_HANDLE_VALUE)
2048 /* Reading single bytes in O_TEXT mode makes things slow
2049 * So read big chunks
2051 if (ReadFile(hand, bufstart, count, &num_read, NULL))
2053 if (count != 0 && num_read == 0)
2055 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2056 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2058 else if (fdinfo->wxflag & WX_TEXT)
2061 if (bufstart[num_read-1] == '\r')
2065 fdinfo->wxflag &= ~WX_READCR;
2066 ReadFile(hand, bufstart, 1, &num_read, NULL);
2070 fdinfo->wxflag |= WX_READCR;
2075 fdinfo->wxflag &= ~WX_READCR;
2076 for (i=0, j=0; i<num_read; i++)
2078 /* in text mode, a ctrl-z signals EOF */
2079 if (bufstart[i] == 0x1a)
2081 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2082 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2085 /* in text mode, strip \r if followed by \n.
2086 * BUG: should save state across calls somehow, so CR LF that
2087 * straddles buffer boundary gets recognized properly?
2089 if ((bufstart[i] != '\r')
2090 || ((i+1) < num_read && bufstart[i+1] != '\n'))
2091 bufstart[j++] = bufstart[i];
2098 if (GetLastError() == ERROR_BROKEN_PIPE)
2100 TRACE(":end-of-pipe\n");
2101 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2106 TRACE(":failed-last error (%d)\n",GetLastError());
2112 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2116 /*********************************************************************
2119 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2122 num_read = read_i(fd, buf, count);
2126 /*********************************************************************
2127 * _setmode (MSVCRT.@)
2129 int CDECL MSVCRT__setmode(int fd,int mode)
2131 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2132 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
2133 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
2134 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
2135 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2137 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2141 /*********************************************************************
2142 * _stat64 (MSVCRT.@)
2144 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2147 WIN32_FILE_ATTRIBUTE_DATA hfi;
2148 unsigned short mode = ALL_S_IREAD;
2151 TRACE(":file (%s) buf(%p)\n",path,buf);
2153 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2155 TRACE("failed (%d)\n",GetLastError());
2156 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2160 memset(buf,0,sizeof(struct MSVCRT__stat64));
2162 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2163 Bon 011120: This FIXME seems incorrect
2164 Also a letter as first char isn't enough to be classified
2167 if (isalpha(*path)&& (*(path+1)==':'))
2168 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2170 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2172 plen = strlen(path);
2174 /* Dir, or regular file? */
2175 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2176 (path[plen-1] == '\\'))
2177 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2180 mode |= MSVCRT__S_IFREG;
2182 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2184 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2185 (tolower(path[plen-3]) << 16);
2186 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2187 mode |= ALL_S_IEXEC;
2191 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2192 mode |= ALL_S_IWRITE;
2194 buf->st_mode = mode;
2196 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2197 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2199 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2200 buf->st_mtime = buf->st_ctime = dw;
2201 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2202 (int)(buf->st_size >> 32),(int)buf->st_size,
2203 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2207 /*********************************************************************
2208 * _stati64 (MSVCRT.@)
2210 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2213 struct MSVCRT__stat64 buf64;
2215 ret = MSVCRT_stat64(path, &buf64);
2217 msvcrt_stat64_to_stati64(&buf64, buf);
2221 /*********************************************************************
2224 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2226 struct MSVCRT__stat64 buf64;
2228 ret = MSVCRT_stat64( path, &buf64);
2230 msvcrt_stat64_to_stat(&buf64, buf);
2234 /*********************************************************************
2235 * _wstat64 (MSVCRT.@)
2237 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2240 WIN32_FILE_ATTRIBUTE_DATA hfi;
2241 unsigned short mode = ALL_S_IREAD;
2244 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2246 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2248 TRACE("failed (%d)\n",GetLastError());
2249 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2253 memset(buf,0,sizeof(struct MSVCRT__stat64));
2255 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2256 if (MSVCRT_iswalpha(*path))
2257 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2259 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2261 plen = strlenW(path);
2263 /* Dir, or regular file? */
2264 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2265 (path[plen-1] == '\\'))
2266 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2269 mode |= MSVCRT__S_IFREG;
2271 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2273 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2274 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2275 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2276 mode |= ALL_S_IEXEC;
2280 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2281 mode |= ALL_S_IWRITE;
2283 buf->st_mode = mode;
2285 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2286 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2288 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2289 buf->st_mtime = buf->st_ctime = dw;
2290 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2291 (int)(buf->st_size >> 32),(int)buf->st_size,
2292 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2296 /*********************************************************************
2297 * _wstati64 (MSVCRT.@)
2299 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2302 struct MSVCRT__stat64 buf64;
2304 ret = MSVCRT__wstat64(path, &buf64);
2306 msvcrt_stat64_to_stati64(&buf64, buf);
2310 /*********************************************************************
2313 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2316 struct MSVCRT__stat64 buf64;
2318 ret = MSVCRT__wstat64( path, &buf64 );
2319 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2323 /*********************************************************************
2326 MSVCRT_long CDECL MSVCRT__tell(int fd)
2328 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2331 /*********************************************************************
2332 * _telli64 (MSVCRT.@)
2334 __int64 CDECL _telli64(int fd)
2336 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2339 /*********************************************************************
2340 * _tempnam (MSVCRT.@)
2342 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
2344 char tmpbuf[MAX_PATH];
2345 const char *tmp_dir = MSVCRT_getenv("TMP");
2347 if (tmp_dir) dir = tmp_dir;
2349 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2350 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2352 TRACE("got name (%s)\n",tmpbuf);
2353 DeleteFileA(tmpbuf);
2354 return MSVCRT__strdup(tmpbuf);
2356 TRACE("failed (%d)\n",GetLastError());
2360 /*********************************************************************
2361 * _wtempnam (MSVCRT.@)
2363 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2365 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2367 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2368 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2370 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2371 DeleteFileW(tmpbuf);
2372 return MSVCRT__wcsdup(tmpbuf);
2374 TRACE("failed (%d)\n",GetLastError());
2378 /*********************************************************************
2381 int CDECL MSVCRT__umask(int umask)
2383 int old_umask = MSVCRT_umask;
2384 TRACE("(%d)\n",umask);
2385 MSVCRT_umask = umask;
2389 /*********************************************************************
2390 * _utime64 (MSVCRT.@)
2392 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2394 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2398 int retVal = _futime64(fd, t);
2405 /*********************************************************************
2406 * _utime32 (MSVCRT.@)
2408 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2412 struct MSVCRT___utimbuf64 t64;
2413 t64.actime = t->actime;
2414 t64.modtime = t->modtime;
2415 return _utime64( path, &t64 );
2418 return _utime64( path, NULL );
2421 /*********************************************************************
2425 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2427 return _utime64( path, t );
2430 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2432 return _utime32( path, t );
2436 /*********************************************************************
2437 * _wutime64 (MSVCRT.@)
2439 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2441 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2445 int retVal = _futime64(fd, t);
2452 /*********************************************************************
2453 * _wutime32 (MSVCRT.@)
2455 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2459 struct MSVCRT___utimbuf64 t64;
2460 t64.actime = t->actime;
2461 t64.modtime = t->modtime;
2462 return _wutime64( path, &t64 );
2465 return _wutime64( path, NULL );
2468 /*********************************************************************
2469 * _wutime (MSVCRT.@)
2472 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2474 return _wutime64( path, t );
2477 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2479 return _wutime32( path, t );
2483 /*********************************************************************
2486 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2489 HANDLE hand = msvcrt_fdtoh(fd);
2491 /* Don't trace small writes, it gets *very* annoying */
2494 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2496 if (hand == INVALID_HANDLE_VALUE)
2498 *MSVCRT__errno() = MSVCRT_EBADF;
2502 /* If appending, go to EOF */
2503 if (msvcrt_get_ioinfo(fd)->wxflag & WX_APPEND)
2504 MSVCRT__lseek(fd, 0, FILE_END);
2506 if (!(msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT))
2508 if (WriteFile(hand, buf, count, &num_written, NULL)
2509 && (num_written == count))
2511 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2512 hand, GetLastError());
2513 *MSVCRT__errno() = MSVCRT_ENOSPC;
2517 unsigned int i, j, nr_lf;
2520 const char *s = buf, *buf_start = buf;
2521 /* find number of \n ( without preceding \r ) */
2522 for ( nr_lf=0,i = 0; i <count; i++)
2527 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2532 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2534 for (s = buf, i = 0, j = 0; i < count; i++)
2539 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2546 FIXME("Malloc failed\n");
2554 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2556 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2557 fd, hand, GetLastError(), num_written);
2558 *MSVCRT__errno() = MSVCRT_ENOSPC;
2561 return s - buf_start;
2573 /*********************************************************************
2576 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2580 MSVCRT__lock_file(file);
2581 len = MSVCRT__write(file->_file, &val, sizeof(val));
2582 if (len == sizeof(val)) {
2583 MSVCRT__unlock_file(file);
2587 file->_flag |= MSVCRT__IOERR;
2588 MSVCRT__unlock_file(file);
2592 /*********************************************************************
2595 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2599 MSVCRT__lock_file(file);
2601 MSVCRT_free(file->_tmpfname);
2602 file->_tmpfname = NULL;
2603 /* flush stdio buffers */
2604 if(file->_flag & MSVCRT__IOWRT)
2605 MSVCRT_fflush(file);
2606 if(file->_flag & MSVCRT__IOMYBUF)
2607 MSVCRT_free(file->_base);
2609 r=MSVCRT__close(file->_file);
2612 MSVCRT__unlock_file(file);
2613 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
2614 DeleteCriticalSection(&((file_crit*)file)->crit);
2616 if(file == msvcrt_get_file(MSVCRT_stream_idx-1)) {
2617 while(MSVCRT_stream_idx>3 && !file->_flag) {
2618 MSVCRT_stream_idx--;
2619 file = msvcrt_get_file(MSVCRT_stream_idx-1);
2623 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2626 /*********************************************************************
2629 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2633 MSVCRT__lock_file(file);
2634 ret = file->_flag & MSVCRT__IOEOF;
2635 MSVCRT__unlock_file(file);
2640 /*********************************************************************
2643 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2647 MSVCRT__lock_file(file);
2648 ret = file->_flag & MSVCRT__IOERR;
2649 MSVCRT__unlock_file(file);
2654 /*********************************************************************
2655 * _filbuf (MSVCRT.@)
2657 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2660 MSVCRT__lock_file(file);
2662 /* Allocate buffer if needed */
2663 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
2664 msvcrt_alloc_buffer(file);
2666 if(!(file->_flag & MSVCRT__IOREAD)) {
2667 if(file->_flag & MSVCRT__IORW)
2668 file->_flag |= MSVCRT__IOREAD;
2670 MSVCRT__unlock_file(file);
2675 if(file->_flag & MSVCRT__IONBF) {
2677 if ((r = read_i(file->_file,&c,1)) != 1) {
2678 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2679 MSVCRT__unlock_file(file);
2683 MSVCRT__unlock_file(file);
2686 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2688 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2690 MSVCRT__unlock_file(file);
2695 file->_ptr = file->_base+1;
2696 c = *(unsigned char *)file->_base;
2697 MSVCRT__unlock_file(file);
2702 /*********************************************************************
2705 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2710 MSVCRT__lock_file(file);
2713 i = (unsigned char *)file->_ptr++;
2716 j = MSVCRT__filbuf(file);
2718 MSVCRT__unlock_file(file);
2722 /*********************************************************************
2723 * _fgetchar (MSVCRT.@)
2725 int CDECL MSVCRT__fgetchar(void)
2727 return MSVCRT_fgetc(MSVCRT_stdin);
2730 /*********************************************************************
2733 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2735 int cc = MSVCRT_EOF;
2736 char * buf_start = s;
2738 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2739 file,file->_file,s,size);
2741 MSVCRT__lock_file(file);
2743 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2748 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2750 TRACE(":nothing read\n");
2751 MSVCRT__unlock_file(file);
2754 if ((cc != MSVCRT_EOF) && (size > 1))
2757 TRACE(":got %s\n", debugstr_a(buf_start));
2758 MSVCRT__unlock_file(file);
2762 /*********************************************************************
2765 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2766 * the CR from CR/LF combinations
2768 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2772 MSVCRT__lock_file(file);
2773 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
2780 for(i=0; i<sizeof(wc); i++)
2790 j = MSVCRT__filbuf(file);
2793 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2796 MSVCRT__unlock_file(file);
2803 MSVCRT__unlock_file(file);
2807 c = MSVCRT_fgetc(file);
2808 if ((get_locinfo()->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2810 FIXME("Treat Multibyte characters\n");
2813 MSVCRT__unlock_file(file);
2814 if (c == MSVCRT_EOF)
2817 return (MSVCRT_wint_t)c;
2820 /*********************************************************************
2823 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2830 MSVCRT__lock_file(file);
2831 for (j=0; j<sizeof(int); j++) {
2832 k = MSVCRT_fgetc(file);
2833 if (k == MSVCRT_EOF) {
2834 file->_flag |= MSVCRT__IOEOF;
2835 MSVCRT__unlock_file(file);
2841 MSVCRT__unlock_file(file);
2845 /*********************************************************************
2848 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2850 return MSVCRT_fgetwc(file);
2853 /*********************************************************************
2854 * _fgetwchar (MSVCRT.@)
2856 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
2858 return MSVCRT_fgetwc(MSVCRT_stdin);
2861 /*********************************************************************
2862 * getwchar (MSVCRT.@)
2864 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2866 return MSVCRT__fgetwchar();
2869 /*********************************************************************
2872 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2874 int cc = MSVCRT_WEOF;
2875 MSVCRT_wchar_t * buf_start = s;
2877 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2878 file,file->_file,s,size);
2880 MSVCRT__lock_file(file);
2882 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2887 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2889 TRACE(":nothing read\n");
2890 MSVCRT__unlock_file(file);
2893 if ((cc != MSVCRT_WEOF) && (size > 1))
2896 TRACE(":got %s\n", debugstr_w(buf_start));
2897 MSVCRT__unlock_file(file);
2901 /*********************************************************************
2904 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2906 MSVCRT_size_t wrcnt=size * nmemb;
2911 MSVCRT__lock_file(file);
2913 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2914 memcpy(file->_ptr, ptr, pcnt);
2919 ptr = (const char*)ptr + pcnt;
2920 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2921 if(file->_flag & MSVCRT__IORW) {
2922 file->_flag |= MSVCRT__IOWRT;
2924 MSVCRT__unlock_file(file);
2930 int res=msvcrt_flush_buffer(file);
2932 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2935 file->_flag |= MSVCRT__IOERR;
2938 written += pwritten;
2942 MSVCRT__unlock_file(file);
2943 return written / size;
2946 /*********************************************************************
2949 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2951 MSVCRT_wchar_t mwc=wc;
2952 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2957 /*********************************************************************
2958 * _fputwchar (MSVCRT.@)
2960 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
2962 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2965 /*********************************************************************
2966 * _wfsopen (MSVCRT.@)
2968 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2971 int open_flags, stream_flags, fd;
2973 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2975 /* map mode string to open() flags. "man fopen" for possibilities. */
2976 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2980 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2983 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2985 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2992 TRACE(":got (%p)\n",file);
2993 if (fd >= 0 && !file)
2999 /*********************************************************************
3000 * _fsopen (MSVCRT.@)
3002 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3005 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3007 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3008 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3009 *MSVCRT__errno() = MSVCRT_EINVAL;
3012 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3015 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3016 *MSVCRT__errno() = MSVCRT_EINVAL;
3020 ret = MSVCRT__wfsopen(pathW, modeW, share);
3027 /*********************************************************************
3030 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3032 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3035 /*********************************************************************
3036 * fopen_s (MSVCRT.@)
3038 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3039 const char *filename, const char *mode)
3041 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
3042 !MSVCRT_CHECK_PMT(mode != NULL)) {
3043 *MSVCRT__errno() = MSVCRT_EINVAL;
3044 return MSVCRT_EINVAL;
3047 *pFile = MSVCRT_fopen(filename, mode);
3050 return *MSVCRT__errno();
3054 /*********************************************************************
3055 * _wfopen (MSVCRT.@)
3057 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3059 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3062 /*********************************************************************
3063 * _wfopen_s (MSVCRT.@)
3065 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3066 const MSVCRT_wchar_t *mode)
3068 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
3069 !MSVCRT_CHECK_PMT(mode != NULL)) {
3070 *MSVCRT__errno() = MSVCRT_EINVAL;
3071 return MSVCRT_EINVAL;
3074 *pFile = MSVCRT__wfopen(filename, mode);
3077 return *MSVCRT__errno();
3081 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
3082 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
3084 /*********************************************************************
3087 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3091 MSVCRT__lock_file(file);
3097 res = msvcrt_flush_buffer(file);
3098 MSVCRT__unlock_file(file);
3099 return res ? res : c;
3102 MSVCRT__unlock_file(file);
3106 res = MSVCRT__flsbuf(c, file);
3107 MSVCRT__unlock_file(file);
3112 /*********************************************************************
3113 * _flsbuf (MSVCRT.@)
3115 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3117 MSVCRT__lock_file(file);
3119 /* Flush output buffer */
3120 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3121 msvcrt_alloc_buffer(file);
3123 if(!(file->_flag & MSVCRT__IOWRT)) {
3124 if(file->_flag & MSVCRT__IORW) {
3125 file->_flag |= MSVCRT__IOWRT;
3127 MSVCRT__unlock_file(file);
3132 int res=msvcrt_flush_buffer(file);
3134 res = MSVCRT_fputc(c, file);
3135 MSVCRT__unlock_file(file);
3140 /* set _cnt to 0 for unbuffered FILEs */
3142 len = MSVCRT__write(file->_file, &cc, 1);
3144 MSVCRT__unlock_file(file);
3147 file->_flag |= MSVCRT__IOERR;
3148 MSVCRT__unlock_file(file);
3153 /*********************************************************************
3154 * _fputchar (MSVCRT.@)
3156 int CDECL MSVCRT__fputchar(int c)
3158 return MSVCRT_fputc(c, MSVCRT_stdout);
3161 /*********************************************************************
3164 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3166 MSVCRT_size_t rcnt=size * nmemb;
3167 MSVCRT_size_t read=0;
3173 MSVCRT__lock_file(file);
3175 /* first buffered data */
3177 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3178 memcpy(ptr, file->_ptr, pcnt);
3183 ptr = (char*)ptr + pcnt;
3184 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3185 if(file->_flag & MSVCRT__IORW) {
3186 file->_flag |= MSVCRT__IOREAD;
3188 MSVCRT__unlock_file(file);
3195 /* Fill the buffer on small reads.
3196 * TODO: Use a better buffering strategy.
3198 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
3199 if (file->_bufsiz == 0) {
3200 msvcrt_alloc_buffer(file);
3202 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3203 file->_ptr = file->_base;
3204 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3205 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3206 if (i > 0 && i < file->_cnt) {
3207 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3208 file->_flag &= ~MSVCRT__IOEOF;
3211 memcpy(ptr, file->_ptr, i);
3216 i = MSVCRT__read(file->_file,ptr, rcnt);
3220 ptr = (char *)ptr+i;
3221 /* expose feof condition in the flags
3222 * MFC tests file->_flag for feof, and doesn't call feof())
3224 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3225 file->_flag |= MSVCRT__IOEOF;
3228 file->_flag |= MSVCRT__IOERR;
3235 MSVCRT__unlock_file(file);
3239 /*********************************************************************
3240 * _wfreopen (MSVCRT.@)
3243 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3245 int open_flags, stream_flags, fd;
3247 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3250 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3254 MSVCRT_fclose(file);
3255 /* map mode string to open() flags. "man fopen" for possibilities. */
3256 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3260 fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3263 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
3266 WARN(":failed-last error (%d)\n",GetLastError());
3267 msvcrt_set_errno(GetLastError());
3276 /*********************************************************************
3277 * freopen (MSVCRT.@)
3280 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3283 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3285 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3286 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3292 ret = MSVCRT__wfreopen(pathW, modeW, file);
3299 /*********************************************************************
3300 * fsetpos (MSVCRT.@)
3302 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3306 MSVCRT__lock_file(file);
3307 /* Note that all this has been lifted 'as is' from fseek */
3308 if(file->_flag & MSVCRT__IOWRT)
3309 msvcrt_flush_buffer(file);
3311 /* Discard buffered input */
3313 file->_ptr = file->_base;
3315 /* Reset direction of i/o */
3316 if(file->_flag & MSVCRT__IORW) {
3317 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3320 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3321 MSVCRT__unlock_file(file);
3325 /*********************************************************************
3326 * _ftelli64 (MSVCRT.@)
3328 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3330 /* TODO: just call fgetpos and return lower half of result */
3334 MSVCRT__lock_file(file);
3335 pos = _telli64(file->_file);
3337 MSVCRT__unlock_file(file);
3341 if( file->_flag & MSVCRT__IOWRT ) {
3342 off = file->_ptr - file->_base;
3345 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3346 /* Black magic correction for CR removal */
3348 for (i=0; i<file->_cnt; i++) {
3349 if (file->_ptr[i] == '\n')
3352 /* Black magic when reading CR at buffer boundary*/
3353 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3359 MSVCRT__unlock_file(file);
3363 /*********************************************************************
3366 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3368 return MSVCRT__ftelli64(file);
3371 /*********************************************************************
3372 * fgetpos (MSVCRT.@)
3374 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3378 MSVCRT__lock_file(file);
3379 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3381 MSVCRT__unlock_file(file);
3385 if( file->_flag & MSVCRT__IOWRT ) {
3386 off = file->_ptr - file->_base;
3389 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3390 /* Black magic correction for CR removal */
3392 for (i=0; i<file->_cnt; i++) {
3393 if (file->_ptr[i] == '\n')
3396 /* Black magic when reading CR at buffer boundary*/
3397 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3403 MSVCRT__unlock_file(file);
3407 /*********************************************************************
3410 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3412 MSVCRT_size_t i, len = strlen(s);
3415 MSVCRT__lock_file(file);
3416 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3417 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3418 MSVCRT__unlock_file(file);
3421 for (i=0; i<len; i++)
3422 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF) {
3423 MSVCRT__unlock_file(file);
3427 MSVCRT__unlock_file(file);
3431 /*********************************************************************
3434 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3436 MSVCRT_size_t i, len = strlenW(s);
3439 MSVCRT__lock_file(file);
3440 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3441 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3442 MSVCRT__unlock_file(file);
3445 for (i=0; i<len; i++) {
3446 if (((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3447 || MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
3448 MSVCRT__unlock_file(file);
3453 MSVCRT__unlock_file(file);
3457 /*********************************************************************
3458 * getchar (MSVCRT.@)
3460 int CDECL MSVCRT_getchar(void)
3462 return MSVCRT_fgetc(MSVCRT_stdin);
3465 /*********************************************************************
3468 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3470 return MSVCRT_fgetc(file);
3473 /*********************************************************************
3476 char * CDECL MSVCRT_gets(char *buf)
3479 char * buf_start = buf;
3481 MSVCRT__lock_file(MSVCRT_stdin);
3482 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3483 cc = MSVCRT_fgetc(MSVCRT_stdin))
3484 if(cc != '\r') *buf++ = (char)cc;
3488 TRACE("got '%s'\n", buf_start);
3489 MSVCRT__unlock_file(MSVCRT_stdin);
3493 /*********************************************************************
3496 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3499 MSVCRT_wchar_t* ws = buf;
3501 MSVCRT__lock_file(MSVCRT_stdin);
3502 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3503 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3506 *buf++ = (MSVCRT_wchar_t)cc;
3510 TRACE("got %s\n", debugstr_w(ws));
3511 MSVCRT__unlock_file(MSVCRT_stdin);
3515 /*********************************************************************
3518 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3520 return MSVCRT_fputc(c, file);
3523 /*********************************************************************
3524 * putchar (MSVCRT.@)
3526 int CDECL MSVCRT_putchar(int c)
3528 return MSVCRT_fputc(c, MSVCRT_stdout);
3531 /*********************************************************************
3532 * _putwch (MSVCRT.@)
3534 int CDECL MSVCRT__putwch(int c)
3536 return MSVCRT_fputwc(c, MSVCRT_stdout);
3539 /*********************************************************************
3542 int CDECL MSVCRT_puts(const char *s)
3544 MSVCRT_size_t len = strlen(s);
3547 MSVCRT__lock_file(MSVCRT_stdout);
3548 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3549 MSVCRT__unlock_file(MSVCRT_stdout);
3553 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3554 MSVCRT__unlock_file(MSVCRT_stdout);
3558 /*********************************************************************
3561 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
3563 static const MSVCRT_wchar_t nl = '\n';
3564 MSVCRT_size_t len = strlenW(s);
3567 MSVCRT__lock_file(MSVCRT_stdout);
3568 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3569 MSVCRT__unlock_file(MSVCRT_stdout);
3573 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3574 MSVCRT__unlock_file(MSVCRT_stdout);
3578 /*********************************************************************
3581 int CDECL MSVCRT_remove(const char *path)
3583 TRACE("(%s)\n",path);
3584 if (DeleteFileA(path))
3586 TRACE(":failed (%d)\n",GetLastError());
3587 msvcrt_set_errno(GetLastError());
3591 /*********************************************************************
3592 * _wremove (MSVCRT.@)
3594 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
3596 TRACE("(%s)\n",debugstr_w(path));
3597 if (DeleteFileW(path))
3599 TRACE(":failed (%d)\n",GetLastError());
3600 msvcrt_set_errno(GetLastError());
3604 /*********************************************************************
3607 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3609 TRACE(":from %s to %s\n",oldpath,newpath);
3610 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3612 TRACE(":failed (%d)\n",GetLastError());
3613 msvcrt_set_errno(GetLastError());
3617 /*********************************************************************
3618 * _wrename (MSVCRT.@)
3620 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3622 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3623 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3625 TRACE(":failed (%d)\n",GetLastError());
3626 msvcrt_set_errno(GetLastError());
3630 /*********************************************************************
3631 * setvbuf (MSVCRT.@)
3633 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3635 MSVCRT__lock_file(file);
3637 MSVCRT_free(file->_base);
3641 if(mode == MSVCRT__IOFBF) {
3642 file->_flag &= ~MSVCRT__IONBF;
3643 file->_base = file->_ptr = buf;
3645 file->_bufsiz = size;
3648 file->_flag |= MSVCRT__IONBF;
3650 MSVCRT__unlock_file(file);
3654 /*********************************************************************
3657 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3659 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3662 /*********************************************************************
3665 char * CDECL MSVCRT_tmpnam(char *s)
3672 thread_data_t *data = msvcrt_get_thread_data();
3674 if(!data->tmpnam_buffer)
3675 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
3677 s = data->tmpnam_buffer;
3680 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3681 p = s + sprintf(s, "\\s%s.", tmpstr);
3682 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3684 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
3685 memcpy(p, tmpstr, size);
3687 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3688 GetLastError() == ERROR_FILE_NOT_FOUND)
3694 /*********************************************************************
3695 * _wtmpnam (MSVCRT.@)
3697 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
3699 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
3700 MSVCRT_wchar_t tmpstr[16];
3704 thread_data_t *data = msvcrt_get_thread_data();
3706 if(!data->wtmpnam_buffer)
3707 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
3709 s = data->wtmpnam_buffer;
3712 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
3713 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
3714 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3716 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
3717 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
3719 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
3720 GetLastError() == ERROR_FILE_NOT_FOUND)
3726 /*********************************************************************
3727 * tmpfile (MSVCRT.@)
3729 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3731 char *filename = MSVCRT_tmpnam(NULL);
3733 MSVCRT_FILE* file = NULL;
3736 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3737 if (fd != -1 && (file = msvcrt_alloc_fp()))
3739 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3744 else file->_tmpfname = MSVCRT__strdup(filename);
3750 static int puts_clbk_file_a(void *file, int len, const char *str)
3752 return MSVCRT_fwrite(str, sizeof(char), len, file);
3755 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
3757 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
3760 /*********************************************************************
3761 * vfprintf (MSVCRT.@)
3763 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3767 MSVCRT__lock_file(file);
3768 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3769 MSVCRT__unlock_file(file);
3774 /*********************************************************************
3775 * vfprintf_s (MSVCRT.@)
3777 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3781 if(!MSVCRT_CHECK_PMT(file != NULL)) {
3782 *MSVCRT__errno() = MSVCRT_EINVAL;
3786 MSVCRT__lock_file(file);
3787 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3788 MSVCRT__unlock_file(file);
3793 /*********************************************************************
3794 * vfwprintf (MSVCRT.@)
3796 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3800 MSVCRT__lock_file(file);
3801 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3802 MSVCRT__unlock_file(file);
3807 /*********************************************************************
3808 * vfwprintf_s (MSVCRT.@)
3810 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3814 if(!MSVCRT_CHECK_PMT( file != NULL)) {
3815 *MSVCRT__errno() = MSVCRT_EINVAL;
3819 MSVCRT__lock_file(file);
3820 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3821 MSVCRT__unlock_file(file);
3826 /*********************************************************************
3827 * vprintf (MSVCRT.@)
3829 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3831 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3834 /*********************************************************************
3835 * vprintf_s (MSVCRT.@)
3837 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
3839 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
3842 /*********************************************************************
3843 * vwprintf (MSVCRT.@)
3845 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3847 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3850 /*********************************************************************
3851 * vwprintf_s (MSVCRT.@)
3853 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
3855 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
3858 /*********************************************************************
3859 * fprintf (MSVCRT.@)
3861 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3863 __ms_va_list valist;
3865 __ms_va_start(valist, format);
3866 res = MSVCRT_vfprintf(file, format, valist);
3867 __ms_va_end(valist);
3871 /*********************************************************************
3872 * fprintf_s (MSVCRT.@)
3874 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
3876 __ms_va_list valist;
3878 __ms_va_start(valist, format);
3879 res = MSVCRT_vfprintf_s(file, format, valist);
3880 __ms_va_end(valist);
3884 /*********************************************************************
3885 * fwprintf (MSVCRT.@)
3887 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3889 __ms_va_list valist;
3891 __ms_va_start(valist, format);
3892 res = MSVCRT_vfwprintf(file, format, valist);
3893 __ms_va_end(valist);
3897 /*********************************************************************
3898 * fwprintf_s (MSVCRT.@)
3900 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3902 __ms_va_list valist;
3904 __ms_va_start(valist, format);
3905 res = MSVCRT_vfwprintf_s(file, format, valist);
3906 __ms_va_end(valist);
3910 /*********************************************************************
3913 int CDECL MSVCRT_printf(const char *format, ...)
3915 __ms_va_list valist;
3917 __ms_va_start(valist, format);
3918 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3919 __ms_va_end(valist);
3923 /*********************************************************************
3924 * printf_s (MSVCRT.@)
3926 int CDECL MSVCRT_printf_s(const char *format, ...)
3928 __ms_va_list valist;
3930 __ms_va_start(valist, format);
3931 res = MSVCRT_vprintf_s(format, valist);
3932 __ms_va_end(valist);
3936 /*********************************************************************
3939 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3941 if (c == MSVCRT_EOF)
3944 MSVCRT__lock_file(file);
3945 if(file->_bufsiz == 0) {
3946 msvcrt_alloc_buffer(file);
3949 if(file->_ptr>file->_base) {
3953 MSVCRT_clearerr(file);
3954 MSVCRT__unlock_file(file);
3958 MSVCRT__unlock_file(file);
3962 /*********************************************************************
3963 * ungetwc (MSVCRT.@)
3965 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3967 MSVCRT_wchar_t mwc = wc;
3968 char * pp = (char *)&mwc;
3971 MSVCRT__lock_file(file);
3972 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3973 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
3974 MSVCRT__unlock_file(file);
3979 MSVCRT__unlock_file(file);
3983 /*********************************************************************
3984 * wprintf (MSVCRT.@)
3986 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3988 __ms_va_list valist;
3990 __ms_va_start(valist, format);
3991 res = MSVCRT_vwprintf(format, valist);
3992 __ms_va_end(valist);
3996 /*********************************************************************
3997 * wprintf_s (MSVCRT.@)
3999 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
4001 __ms_va_list valist;
4003 __ms_va_start(valist, format);
4004 res = MSVCRT_vwprintf_s(format, valist);
4005 __ms_va_end(valist);
4009 /*********************************************************************
4010 * _getmaxstdio (MSVCRT.@)
4012 int CDECL MSVCRT__getmaxstdio(void)
4014 return MSVCRT_max_streams;
4017 /*********************************************************************
4018 * _setmaxstdio (MSVCRT.@)
4020 int CDECL MSVCRT__setmaxstdio(int newmax)
4022 TRACE("%d\n", newmax);
4024 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4027 MSVCRT_max_streams = newmax;
4028 return MSVCRT_max_streams;