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"
37 #include <sys/types.h>
45 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
51 /* for stat mode, permissions apply to all,owner and group */
52 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
53 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
54 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
56 /* _access() bit flags FIXME: incomplete */
57 #define MSVCRT_W_OK 0x02
58 #define MSVCRT_R_OK 0x04
60 /* values for wxflag in file descriptor */
63 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
64 #define WX_READCR 0x08 /* underlying file is at \r */
65 #define WX_DONTINHERIT 0x10
66 #define WX_APPEND 0x20
69 /* FIXME: this should be allocated dynamically */
70 #define MSVCRT_MAX_FILES 2048
71 #define MSVCRT_FD_BLOCK_SIZE 32
73 /* ioinfo structure size is different in msvcrXX.dll's */
79 CRITICAL_SECTION crit;
82 /*********************************************************************
83 * __pioinfo (MSVCRT.@)
84 * array of pointers to ioinfo arrays [32]
86 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
88 /*********************************************************************
89 * __badioinfo (MSVCRT.@)
91 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
93 static int MSVCRT_fdstart = 3; /* first unallocated fd */
94 static int MSVCRT_fdend = 3; /* highest allocated fd */
98 CRITICAL_SECTION crit;
101 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
102 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
103 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
105 /* INTERNAL: process umask */
106 static int MSVCRT_umask = 0;
108 /* INTERNAL: static data for tmpnam and _wtmpname functions */
109 static int tmpnam_unique;
111 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
112 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
113 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
114 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
116 #define TOUL(x) (ULONGLONG)(x)
117 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
118 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
119 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
120 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
122 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
123 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
124 * and MSVCRT_stream_idx, from race conditions.
125 * It doesn't protect against race conditions manipulating the underlying files
126 * or flags; doing so would probably be better accomplished with per-file
127 * protection, rather than locking the whole table for every change.
129 static CRITICAL_SECTION MSVCRT_file_cs;
130 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
132 0, 0, &MSVCRT_file_cs,
133 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
134 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
136 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
137 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
138 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
140 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
142 buf->st_dev = buf64->st_dev;
143 buf->st_ino = buf64->st_ino;
144 buf->st_mode = buf64->st_mode;
145 buf->st_nlink = buf64->st_nlink;
146 buf->st_uid = buf64->st_uid;
147 buf->st_gid = buf64->st_gid;
148 buf->st_rdev = buf64->st_rdev;
149 buf->st_size = buf64->st_size;
150 buf->st_atime = buf64->st_atime;
151 buf->st_mtime = buf64->st_mtime;
152 buf->st_ctime = buf64->st_ctime;
155 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
157 buf->st_dev = buf64->st_dev;
158 buf->st_ino = buf64->st_ino;
159 buf->st_mode = buf64->st_mode;
160 buf->st_nlink = buf64->st_nlink;
161 buf->st_uid = buf64->st_uid;
162 buf->st_gid = buf64->st_gid;
163 buf->st_rdev = buf64->st_rdev;
164 buf->st_size = buf64->st_size;
165 buf->st_atime = buf64->st_atime;
166 buf->st_mtime = buf64->st_mtime;
167 buf->st_ctime = buf64->st_ctime;
170 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
172 /* 1601 to 1970 is 369 years plus 89 leap days */
173 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
175 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
176 ft->dwHighDateTime = ticks >> 32;
177 ft->dwLowDateTime = ticks;
180 static inline ioinfo* msvcrt_get_ioinfo(int fd)
183 if(fd < MSVCRT_MAX_FILES)
184 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
186 return &MSVCRT___badioinfo;
188 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
191 static inline MSVCRT_FILE* msvcrt_get_file(int i)
195 if(i >= MSVCRT_max_streams)
199 return &MSVCRT__iob[i];
201 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
203 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
204 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
205 ERR("out of memory\n");
206 *MSVCRT__errno() = MSVCRT_ENOMEM;
210 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
212 ret += i%MSVCRT_FD_BLOCK_SIZE;
217 static inline BOOL msvcrt_is_valid_fd(int fd)
219 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
222 /* INTERNAL: Get the HANDLE for a fd
223 * This doesn't lock the table, because a failure will result in
224 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
225 * it returns a valid handle which is about to be closed, a subsequent call
226 * will fail, most likely in a sane way.
228 static HANDLE msvcrt_fdtoh(int fd)
230 if (!msvcrt_is_valid_fd(fd))
232 WARN(":fd (%d) - no handle!\n",fd);
233 *MSVCRT___doserrno() = 0;
234 *MSVCRT__errno() = MSVCRT_EBADF;
235 return INVALID_HANDLE_VALUE;
237 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
238 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd);
239 return msvcrt_get_ioinfo(fd)->handle;
242 /* INTERNAL: free a file entry fd */
243 static void msvcrt_free_fd(int fd)
249 fdinfo = msvcrt_get_ioinfo(fd);
250 old_handle = fdinfo->handle;
251 if(fdinfo != &MSVCRT___badioinfo)
253 fdinfo->handle = INVALID_HANDLE_VALUE;
256 TRACE(":fd (%d) freed\n",fd);
257 if (fd < 3) /* don't use 0,1,2 for user files */
262 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
265 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
268 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
274 if (fd == MSVCRT_fdend - 1)
276 if (fd < MSVCRT_fdstart)
282 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
283 /* caller must hold the files lock */
284 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
288 if (fd >= MSVCRT_MAX_FILES)
290 WARN(":files exhausted!\n");
291 *MSVCRT__errno() = MSVCRT_ENFILE;
295 fdinfo = msvcrt_get_ioinfo(fd);
296 if(fdinfo == &MSVCRT___badioinfo) {
299 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
300 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
301 WARN(":out of memory!\n");
302 *MSVCRT__errno() = MSVCRT_ENOMEM;
306 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
307 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
309 fdinfo = msvcrt_get_ioinfo(fd);
312 fdinfo->handle = hand;
313 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
315 /* locate next free slot */
316 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
317 MSVCRT_fdstart = MSVCRT_fdend + 1;
319 while (MSVCRT_fdstart < MSVCRT_fdend &&
320 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
322 /* update last fd in use */
323 if (fd >= MSVCRT_fdend)
324 MSVCRT_fdend = fd + 1;
325 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
329 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
330 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
331 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
337 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
338 static int msvcrt_alloc_fd(HANDLE hand, int flag)
343 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
344 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
349 /* INTERNAL: Allocate a FILE* for an fd slot */
350 /* caller must hold the files lock */
351 static MSVCRT_FILE* msvcrt_alloc_fp(void)
356 for (i = 3; i < MSVCRT_max_streams; i++)
358 file = msvcrt_get_file(i);
362 if (file->_flag == 0)
364 if (i == MSVCRT_stream_idx)
366 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
368 InitializeCriticalSection(&((file_crit*)file)->crit);
369 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
380 /* INTERNAL: initialize a FILE* from an open fd */
381 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
383 TRACE(":fd (%d) allocating FILE*\n",fd);
384 if (!msvcrt_is_valid_fd(fd))
386 WARN(":invalid fd %d\n",fd);
387 *MSVCRT___doserrno() = 0;
388 *MSVCRT__errno() = MSVCRT_EBADF;
391 memset(file, 0, sizeof(*file));
393 file->_flag = stream_flags;
395 TRACE(":got FILE* (%p)\n",file);
399 /* INTERNAL: Create an inheritance data block (for spawned process)
400 * The inheritance block is made of:
401 * 00 int nb of file descriptor (NBFD)
402 * 04 char file flags (wxflag): repeated for each fd
403 * 4+NBFD HANDLE file handle: repeated for each fd
405 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
412 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
413 *block = MSVCRT_calloc(*size, 1);
419 wxflag_ptr = (char*)*block + sizeof(unsigned);
420 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
422 *(unsigned*)*block = MSVCRT_fdend;
423 for (fd = 0; fd < MSVCRT_fdend; fd++)
425 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
426 fdinfo = msvcrt_get_ioinfo(fd);
427 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
429 *wxflag_ptr = fdinfo->wxflag;
430 *handle_ptr = fdinfo->handle;
435 *handle_ptr = INVALID_HANDLE_VALUE;
437 wxflag_ptr++; handle_ptr++;
442 /* INTERNAL: Set up all file descriptors,
443 * as well as default streams (stdin, stderr and stdout)
445 void msvcrt_init_io(void)
451 GetStartupInfoA(&si);
452 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
458 count = *(unsigned*)si.lpReserved2;
459 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
460 handle_ptr = (HANDLE*)(wxflag_ptr + count);
462 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
463 count = min(count, MSVCRT_MAX_FILES);
464 for (i = 0; i < count; i++)
466 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
467 msvcrt_alloc_fd_from(*handle_ptr, *wxflag_ptr, i);
469 wxflag_ptr++; handle_ptr++;
471 MSVCRT_fdend = max( 3, count );
472 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
473 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
476 if(!MSVCRT___pioinfo[0])
477 msvcrt_alloc_fd_from(INVALID_HANDLE_VALUE, 0, 3);
479 fdinfo = msvcrt_get_ioinfo(0);
480 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
482 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
483 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
484 GetCurrentProcess(), &fdinfo->handle,
485 0, TRUE, DUPLICATE_SAME_ACCESS))
486 fdinfo->wxflag = WX_OPEN | WX_TEXT;
489 fdinfo = msvcrt_get_ioinfo(1);
490 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
492 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
493 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
494 GetCurrentProcess(), &fdinfo->handle,
495 0, TRUE, DUPLICATE_SAME_ACCESS))
496 fdinfo->wxflag = WX_OPEN | WX_TEXT;
499 fdinfo = msvcrt_get_ioinfo(2);
500 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
502 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
503 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
504 GetCurrentProcess(), &fdinfo->handle,
505 0, TRUE, DUPLICATE_SAME_ACCESS))
506 fdinfo->wxflag = WX_OPEN | WX_TEXT;
509 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(0)->handle,
510 msvcrt_get_ioinfo(1)->handle, msvcrt_get_ioinfo(2)->handle);
512 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
513 for (i = 0; i < 3; i++)
515 /* FILE structs for stdin/out/err are static and never deleted */
516 MSVCRT__iob[i]._file = i;
517 MSVCRT__iob[i]._tmpfname = NULL;
518 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
520 MSVCRT_stream_idx = 3;
523 /* INTERNAL: Flush stdio file buffer */
524 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
527 int cnt=file->_ptr-file->_base;
528 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
529 file->_flag |= MSVCRT__IOERR;
532 file->_ptr=file->_base;
533 file->_cnt=file->_bufsiz;
538 /* INTERNAL: Allocate stdio file buffer */
539 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
541 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
543 file->_bufsiz = MSVCRT_BUFSIZ;
544 file->_flag |= MSVCRT__IOMYBUF;
546 file->_base = (char*)(&file->_charbuf);
548 file->_bufsiz = sizeof(file->_charbuf);
550 file->_ptr = file->_base;
554 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
555 static int msvcrt_int_to_base32(int num, char *str)
570 *p = (num & 31) + '0';
572 *p += ('a' - '0' - 10);
579 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
580 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
595 *p = (num & 31) + '0';
597 *p += ('a' - '0' - 10);
604 /*********************************************************************
605 * __iob_func(MSVCRT.@)
607 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
609 return &MSVCRT__iob[0];
612 /*********************************************************************
615 int CDECL MSVCRT__access(const char *filename, int mode)
617 DWORD attr = GetFileAttributesA(filename);
619 TRACE("(%s,%d) %d\n",filename,mode,attr);
621 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
623 msvcrt_set_errno(GetLastError());
626 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
628 msvcrt_set_errno(ERROR_ACCESS_DENIED);
634 /*********************************************************************
635 * _access_s (MSVCRT.@)
637 int CDECL _access_s(const char *filename, int mode)
639 if (!MSVCRT_CHECK_PMT(filename != NULL)) return -1;
640 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return -1;
642 return MSVCRT__access(filename, mode);
645 /*********************************************************************
646 * _waccess (MSVCRT.@)
648 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
650 DWORD attr = GetFileAttributesW(filename);
652 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
654 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
656 msvcrt_set_errno(GetLastError());
659 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
661 msvcrt_set_errno(ERROR_ACCESS_DENIED);
667 /*********************************************************************
668 * _waccess_s (MSVCRT.@)
670 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
672 if (!MSVCRT_CHECK_PMT(filename != NULL)) return -1;
673 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return -1;
675 return MSVCRT__waccess(filename, mode);
678 /*********************************************************************
681 int CDECL MSVCRT__chmod(const char *path, int flags)
683 DWORD oldFlags = GetFileAttributesA(path);
685 if (oldFlags != INVALID_FILE_ATTRIBUTES)
687 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
688 oldFlags | FILE_ATTRIBUTE_READONLY;
690 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
693 msvcrt_set_errno(GetLastError());
697 /*********************************************************************
700 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
702 DWORD oldFlags = GetFileAttributesW(path);
704 if (oldFlags != INVALID_FILE_ATTRIBUTES)
706 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
707 oldFlags | FILE_ATTRIBUTE_READONLY;
709 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
712 msvcrt_set_errno(GetLastError());
716 /*********************************************************************
719 int CDECL MSVCRT__unlink(const char *path)
721 TRACE("%s\n",debugstr_a(path));
722 if(DeleteFileA(path))
724 TRACE("failed (%d)\n",GetLastError());
725 msvcrt_set_errno(GetLastError());
729 /*********************************************************************
730 * _wunlink (MSVCRT.@)
732 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
734 TRACE("(%s)\n",debugstr_w(path));
735 if(DeleteFileW(path))
737 TRACE("failed (%d)\n",GetLastError());
738 msvcrt_set_errno(GetLastError());
742 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
743 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
745 /* INTERNAL: Flush all stream buffer */
746 static int msvcrt_flush_all_buffers(int mask)
748 int i, num_flushed = 0;
752 for (i = 3; i < MSVCRT_stream_idx; i++) {
753 file = msvcrt_get_file(i);
757 if(file->_flag & mask) {
765 TRACE(":flushed (%d) handles\n",num_flushed);
769 /*********************************************************************
770 * _flushall (MSVCRT.@)
772 int CDECL MSVCRT__flushall(void)
774 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
777 /*********************************************************************
780 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
783 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
784 } else if(file->_flag & MSVCRT__IOWRT) {
787 MSVCRT__lock_file(file);
788 res = msvcrt_flush_buffer(file);
789 MSVCRT__unlock_file(file);
792 } else if(file->_flag & MSVCRT__IOREAD) {
793 MSVCRT__lock_file(file);
795 file->_ptr = file->_base;
796 MSVCRT__unlock_file(file);
803 /*********************************************************************
806 int CDECL MSVCRT__close(int fd)
812 hand = msvcrt_fdtoh(fd);
813 TRACE(":fd (%d) handle (%p)\n",fd,hand);
814 if (!msvcrt_is_valid_fd(fd)) {
818 ret = CloseHandle(hand) ? 0 : -1;
820 WARN(":failed-last error (%d)\n",GetLastError());
821 msvcrt_set_errno(GetLastError());
829 /*********************************************************************
832 int CDECL MSVCRT__commit(int fd)
834 HANDLE hand = msvcrt_fdtoh(fd);
836 TRACE(":fd (%d) handle (%p)\n",fd,hand);
837 if (hand == INVALID_HANDLE_VALUE)
840 if (!FlushFileBuffers(hand))
842 if (GetLastError() == ERROR_INVALID_HANDLE)
844 /* FlushFileBuffers fails for console handles
845 * so we ignore this error.
849 TRACE(":failed-last error (%d)\n",GetLastError());
850 msvcrt_set_errno(GetLastError());
857 /*********************************************************************
860 * MSDN isn't clear on this point, but the remarks for _pipe
861 * indicate file descriptors duplicated with _dup and _dup2 are always
864 int CDECL MSVCRT__dup2(int od, int nd)
868 TRACE("(od=%d, nd=%d)\n", od, nd);
870 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
874 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
875 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
877 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
879 if (msvcrt_is_valid_fd(nd))
881 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
885 *MSVCRT__errno() = MSVCRT_EMFILE;
889 /* _dup2 returns 0, not nd, on success */
896 msvcrt_set_errno(GetLastError());
901 *MSVCRT__errno() = MSVCRT_EBADF;
908 /*********************************************************************
911 int CDECL MSVCRT__dup(int od)
917 if (MSVCRT__dup2(od, fd) == 0)
925 /*********************************************************************
928 int CDECL MSVCRT__eof(int fd)
931 LONG hcurpos,hendpos;
932 HANDLE hand = msvcrt_fdtoh(fd);
934 TRACE(":fd (%d) handle (%p)\n",fd,hand);
936 if (hand == INVALID_HANDLE_VALUE)
939 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
941 /* Otherwise we do it the hard way */
942 hcurpos = hendpos = 0;
943 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
944 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
946 if (curpos == endpos && hcurpos == hendpos)
948 /* FIXME: shouldn't WX_ATEOF be set here? */
952 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
956 /*********************************************************************
957 * _fcloseall (MSVCRT.@)
959 int CDECL MSVCRT__fcloseall(void)
961 int num_closed = 0, i;
965 for (i = 3; i < MSVCRT_stream_idx; i++) {
966 file = msvcrt_get_file(i);
968 if (file->_flag && !MSVCRT_fclose(file))
973 TRACE(":closed (%d) handles\n",num_closed);
977 /* free everything on process exit */
978 void msvcrt_free_io(void)
983 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
984 * stdout, and stderr (unlike GNU), so we need to fclose() them here
985 * or they won't get flushed.
987 MSVCRT_fclose(&MSVCRT__iob[0]);
988 MSVCRT_fclose(&MSVCRT__iob[1]);
989 MSVCRT_fclose(&MSVCRT__iob[2]);
991 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
992 MSVCRT_free(MSVCRT___pioinfo[i]);
994 for(i=0; i<MSVCRT_stream_idx; i++)
996 MSVCRT_FILE *file = msvcrt_get_file(i);
997 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
999 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1000 DeleteCriticalSection(&((file_crit*)file)->crit);
1004 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1005 MSVCRT_free(MSVCRT_fstream[i]);
1007 DeleteCriticalSection(&MSVCRT_file_cs);
1010 /*********************************************************************
1011 * _lseeki64 (MSVCRT.@)
1013 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1015 HANDLE hand = msvcrt_fdtoh(fd);
1018 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1019 if (hand == INVALID_HANDLE_VALUE)
1022 if (whence < 0 || whence > 2)
1024 *MSVCRT__errno() = MSVCRT_EINVAL;
1028 TRACE(":fd (%d) to %s pos %s\n",
1029 fd,wine_dbgstr_longlong(offset),
1030 (whence==SEEK_SET)?"SEEK_SET":
1031 (whence==SEEK_CUR)?"SEEK_CUR":
1032 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1034 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1035 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1036 ofs.QuadPart = offset;
1037 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1038 GetLastError() == ERROR_SUCCESS)
1040 msvcrt_get_ioinfo(fd)->wxflag &= ~(WX_ATEOF|WX_READEOF);
1041 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1043 return ofs.QuadPart;
1045 TRACE(":error-last error (%d)\n",GetLastError());
1046 msvcrt_set_errno(GetLastError());
1050 /*********************************************************************
1053 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1055 return MSVCRT__lseeki64(fd, offset, whence);
1058 /*********************************************************************
1059 * _lock_file (MSVCRT.@)
1061 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1063 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1064 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1066 EnterCriticalSection(&((file_crit*)file)->crit);
1069 /*********************************************************************
1070 * _unlock_file (MSVCRT.@)
1072 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1074 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1075 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1077 LeaveCriticalSection(&((file_crit*)file)->crit);
1080 /*********************************************************************
1081 * _locking (MSVCRT.@)
1083 * This is untested; the underlying LockFile doesn't work yet.
1085 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1089 HANDLE hand = msvcrt_fdtoh(fd);
1091 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1092 if (hand == INVALID_HANDLE_VALUE)
1095 if (mode < 0 || mode > 4)
1097 *MSVCRT__errno() = MSVCRT_EINVAL;
1101 TRACE(":fd (%d) by 0x%08x mode %s\n",
1102 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1103 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1104 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1105 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1106 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1109 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1111 FIXME ("Seek failed\n");
1112 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1115 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1118 ret = 1; /* just to satisfy gcc */
1121 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1126 else if (mode == MSVCRT__LK_UNLCK)
1127 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1129 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1130 /* FIXME - what about error settings? */
1131 return ret ? 0 : -1;
1134 /*********************************************************************
1135 * _fseeki64 (MSVCRT.@)
1137 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1141 MSVCRT__lock_file(file);
1142 /* Flush output if needed */
1143 if(file->_flag & MSVCRT__IOWRT)
1144 msvcrt_flush_buffer(file);
1146 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1147 offset -= file->_cnt;
1148 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
1149 /* Black magic correction for CR removal */
1151 for (i=0; i<file->_cnt; i++) {
1152 if (file->_ptr[i] == '\n')
1155 /* Black magic when reading CR at buffer boundary*/
1156 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
1160 /* Discard buffered input */
1162 file->_ptr = file->_base;
1163 /* Reset direction of i/o */
1164 if(file->_flag & MSVCRT__IORW) {
1165 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1167 /* Clear end of file flag */
1168 file->_flag &= ~MSVCRT__IOEOF;
1169 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1171 MSVCRT__unlock_file(file);
1175 /*********************************************************************
1178 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1180 return MSVCRT__fseeki64( file, offset, whence );
1183 /*********************************************************************
1184 * _chsize (MSVCRT.@)
1186 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1192 TRACE("(fd=%d, size=%d)\n", fd, size);
1196 handle = msvcrt_fdtoh(fd);
1197 if (handle != INVALID_HANDLE_VALUE)
1199 /* save the current file pointer */
1200 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1203 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1206 ret = SetEndOfFile(handle);
1207 if (!ret) msvcrt_set_errno(GetLastError());
1210 /* restore the file pointer */
1211 MSVCRT__lseek(fd, cur, SEEK_SET);
1216 return ret ? 0 : -1;
1219 /*********************************************************************
1220 * clearerr (MSVCRT.@)
1222 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1224 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1226 MSVCRT__lock_file(file);
1227 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1228 MSVCRT__unlock_file(file);
1231 /*********************************************************************
1234 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1236 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1238 MSVCRT__lock_file(file);
1239 MSVCRT_fseek(file, 0L, SEEK_SET);
1240 MSVCRT_clearerr(file);
1241 MSVCRT__unlock_file(file);
1244 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1246 int plus = strchrW(mode, '+') != NULL;
1251 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1252 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1255 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1256 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1259 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1260 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1263 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1264 *MSVCRT__errno() = MSVCRT_EINVAL;
1272 *open_flags |= MSVCRT__O_BINARY;
1273 *open_flags &= ~MSVCRT__O_TEXT;
1276 *open_flags |= MSVCRT__O_TEXT;
1277 *open_flags &= ~MSVCRT__O_BINARY;
1280 *open_flags |= MSVCRT__O_TEMPORARY;
1283 *open_flags |= MSVCRT__O_SHORT_LIVED;
1289 FIXME(":unknown flag %c not supported\n",mode[-1]);
1294 /*********************************************************************
1295 * _fdopen (MSVCRT.@)
1297 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1300 MSVCRT_wchar_t *modeW = NULL;
1302 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1304 ret = MSVCRT__wfdopen(fd, modeW);
1310 /*********************************************************************
1311 * _wfdopen (MSVCRT.@)
1313 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1315 int open_flags, stream_flags;
1318 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1321 if (!(file = msvcrt_alloc_fp()))
1323 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1328 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1334 /*********************************************************************
1335 * _filelength (MSVCRT.@)
1337 LONG CDECL MSVCRT__filelength(int fd)
1339 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1342 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1345 if (endPos != curPos)
1346 MSVCRT__lseek(fd, curPos, SEEK_SET);
1353 /*********************************************************************
1354 * _filelengthi64 (MSVCRT.@)
1356 __int64 CDECL MSVCRT__filelengthi64(int fd)
1358 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1361 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1364 if (endPos != curPos)
1365 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1372 /*********************************************************************
1373 * _fileno (MSVCRT.@)
1375 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1377 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1381 /*********************************************************************
1382 * _fstat64 (MSVCRT.@)
1384 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1388 BY_HANDLE_FILE_INFORMATION hfi;
1389 HANDLE hand = msvcrt_fdtoh(fd);
1391 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1392 if (hand == INVALID_HANDLE_VALUE)
1397 WARN(":failed-NULL buf\n");
1398 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1402 memset(&hfi, 0, sizeof(hfi));
1403 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1404 type = GetFileType(hand);
1405 if (type == FILE_TYPE_PIPE)
1407 buf->st_dev = buf->st_rdev = fd;
1408 buf->st_mode = S_IFIFO;
1411 else if (type == FILE_TYPE_CHAR)
1413 buf->st_dev = buf->st_rdev = fd;
1414 buf->st_mode = S_IFCHR;
1417 else /* FILE_TYPE_DISK etc. */
1419 if (!GetFileInformationByHandle(hand, &hfi))
1421 WARN(":failed-last error (%d)\n",GetLastError());
1422 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1425 buf->st_mode = S_IFREG | 0444;
1426 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1427 buf->st_mode |= 0222;
1428 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1429 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1431 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1432 buf->st_mtime = buf->st_ctime = dw;
1433 buf->st_nlink = hfi.nNumberOfLinks;
1435 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1440 /*********************************************************************
1441 * _fstati64 (MSVCRT.@)
1443 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1446 struct MSVCRT__stat64 buf64;
1448 ret = MSVCRT__fstat64(fd, &buf64);
1450 msvcrt_stat64_to_stati64(&buf64, buf);
1454 /*********************************************************************
1457 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1459 struct MSVCRT__stat64 buf64;
1461 ret = MSVCRT__fstat64(fd, &buf64);
1463 msvcrt_stat64_to_stat(&buf64, buf);
1467 /*********************************************************************
1468 * _futime64 (MSVCRT.@)
1470 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1472 HANDLE hand = msvcrt_fdtoh(fd);
1477 time_to_filetime( MSVCRT__time64(NULL), &at );
1482 time_to_filetime( t->actime, &at );
1483 time_to_filetime( t->modtime, &wt );
1486 if (!SetFileTime(hand, NULL, &at, &wt))
1488 msvcrt_set_errno(GetLastError());
1494 /*********************************************************************
1495 * _futime32 (MSVCRT.@)
1497 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1501 struct MSVCRT___utimbuf64 t64;
1502 t64.actime = t->actime;
1503 t64.modtime = t->modtime;
1504 return _futime64( fd, &t64 );
1507 return _futime64( fd, NULL );
1510 /*********************************************************************
1511 * _futime (MSVCRT.@)
1514 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1516 return _futime64( fd, t );
1519 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1521 return _futime32( fd, t );
1525 /*********************************************************************
1526 * _get_osfhandle (MSVCRT.@)
1528 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1530 HANDLE hand = msvcrt_fdtoh(fd);
1531 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1533 return (MSVCRT_intptr_t)hand;
1536 /*********************************************************************
1537 * _isatty (MSVCRT.@)
1539 int CDECL MSVCRT__isatty(int fd)
1541 HANDLE hand = msvcrt_fdtoh(fd);
1543 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1544 if (hand == INVALID_HANDLE_VALUE)
1547 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1550 /*********************************************************************
1551 * _mktemp (MSVCRT.@)
1553 char * CDECL MSVCRT__mktemp(char *pattern)
1556 char *retVal = pattern;
1561 numX = (*pattern++ == 'X')? numX + 1 : 0;
1565 id = GetCurrentProcessId();
1569 int tempNum = id / 10;
1570 *pattern-- = id - (tempNum * 10) + '0';
1576 *pattern = letter++;
1577 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1578 GetLastError() == ERROR_FILE_NOT_FOUND)
1580 } while(letter <= 'z');
1584 /*********************************************************************
1585 * _wmktemp (MSVCRT.@)
1587 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1590 MSVCRT_wchar_t *retVal = pattern;
1592 MSVCRT_wchar_t letter = 'a';
1595 numX = (*pattern++ == 'X')? numX + 1 : 0;
1599 id = GetCurrentProcessId();
1603 int tempNum = id / 10;
1604 *pattern-- = id - (tempNum * 10) + '0';
1610 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1611 GetLastError() == ERROR_FILE_NOT_FOUND)
1613 *pattern = letter++;
1614 } while(letter != '|');
1618 static unsigned split_oflags(unsigned oflags)
1621 unsigned unsupp; /* until we support everything */
1623 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1624 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1625 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1626 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1627 else wxflags |= WX_TEXT; /* default to TEXT*/
1628 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1630 if ((unsupp = oflags & ~(
1631 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1632 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1633 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1634 MSVCRT__O_NOINHERIT|
1635 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1637 ERR(":unsupported oflags 0x%04x\n",unsupp);
1642 /*********************************************************************
1645 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1648 SECURITY_ATTRIBUTES sa;
1649 HANDLE readHandle, writeHandle;
1653 *MSVCRT__errno() = MSVCRT_EINVAL;
1657 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1658 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1659 sa.lpSecurityDescriptor = NULL;
1660 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1662 unsigned int wxflags = split_oflags(textmode);
1666 fd = msvcrt_alloc_fd(readHandle, wxflags);
1670 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1678 MSVCRT__close(pfds[0]);
1679 CloseHandle(writeHandle);
1680 *MSVCRT__errno() = MSVCRT_EMFILE;
1685 CloseHandle(readHandle);
1686 CloseHandle(writeHandle);
1687 *MSVCRT__errno() = MSVCRT_EMFILE;
1692 msvcrt_set_errno(GetLastError());
1697 /*********************************************************************
1698 * _sopen_s (MSVCRT.@)
1700 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1702 DWORD access = 0, creation = 0, attrib;
1706 SECURITY_ATTRIBUTES sa;
1708 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1709 fd, path, oflags, shflags, pmode);
1711 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
1714 wxflag = split_oflags(oflags);
1715 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1717 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1718 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1719 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1722 if (oflags & MSVCRT__O_CREAT)
1724 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1725 FIXME(": pmode 0x%04x ignored\n", pmode);
1727 WARN(": pmode 0x%04x ignored\n", pmode);
1729 if (oflags & MSVCRT__O_EXCL)
1730 creation = CREATE_NEW;
1731 else if (oflags & MSVCRT__O_TRUNC)
1732 creation = CREATE_ALWAYS;
1734 creation = OPEN_ALWAYS;
1736 else /* no MSVCRT__O_CREAT */
1738 if (oflags & MSVCRT__O_TRUNC)
1739 creation = TRUNCATE_EXISTING;
1741 creation = OPEN_EXISTING;
1746 case MSVCRT__SH_DENYRW:
1749 case MSVCRT__SH_DENYWR:
1750 sharing = FILE_SHARE_READ;
1752 case MSVCRT__SH_DENYRD:
1753 sharing = FILE_SHARE_WRITE;
1755 case MSVCRT__SH_DENYNO:
1756 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1759 ERR( "Unhandled shflags 0x%x\n", shflags );
1760 return MSVCRT_EINVAL;
1762 attrib = FILE_ATTRIBUTE_NORMAL;
1764 if (oflags & MSVCRT__O_TEMPORARY)
1766 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1768 sharing |= FILE_SHARE_DELETE;
1771 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1772 sa.lpSecurityDescriptor = NULL;
1773 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
1775 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1776 if (hand == INVALID_HANDLE_VALUE) {
1777 WARN(":failed-last error (%d)\n", GetLastError());
1778 msvcrt_set_errno(GetLastError());
1779 return *MSVCRT__errno();
1782 *fd = msvcrt_alloc_fd(hand, wxflag);
1784 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1788 /*********************************************************************
1791 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1796 if (oflags & MSVCRT__O_CREAT)
1800 __ms_va_start(ap, shflags);
1801 pmode = va_arg(ap, int);
1807 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
1811 /*********************************************************************
1812 * _wsopen_s (MSVCRT.@)
1814 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1816 DWORD access = 0, creation = 0, attrib;
1817 SECURITY_ATTRIBUTES sa;
1822 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1823 fd, debugstr_w(path), oflags, shflags, pmode);
1825 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
1828 wxflag = split_oflags(oflags);
1829 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1831 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1832 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1833 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1836 if (oflags & MSVCRT__O_CREAT)
1838 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1839 FIXME(": pmode 0x%04x ignored\n", pmode);
1841 WARN(": pmode 0x%04x ignored\n", pmode);
1843 if (oflags & MSVCRT__O_EXCL)
1844 creation = CREATE_NEW;
1845 else if (oflags & MSVCRT__O_TRUNC)
1846 creation = CREATE_ALWAYS;
1848 creation = OPEN_ALWAYS;
1850 else /* no MSVCRT__O_CREAT */
1852 if (oflags & MSVCRT__O_TRUNC)
1853 creation = TRUNCATE_EXISTING;
1855 creation = OPEN_EXISTING;
1860 case MSVCRT__SH_DENYRW:
1863 case MSVCRT__SH_DENYWR:
1864 sharing = FILE_SHARE_READ;
1866 case MSVCRT__SH_DENYRD:
1867 sharing = FILE_SHARE_WRITE;
1869 case MSVCRT__SH_DENYNO:
1870 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1873 ERR( "Unhandled shflags 0x%x\n", shflags );
1874 return MSVCRT_EINVAL;
1876 attrib = FILE_ATTRIBUTE_NORMAL;
1878 if (oflags & MSVCRT__O_TEMPORARY)
1880 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1882 sharing |= FILE_SHARE_DELETE;
1885 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1886 sa.lpSecurityDescriptor = NULL;
1887 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
1889 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1891 if (hand == INVALID_HANDLE_VALUE) {
1892 WARN(":failed-last error (%d)\n",GetLastError());
1893 msvcrt_set_errno(GetLastError());
1894 msvcrt_set_errno(GetLastError());
1895 return *MSVCRT__errno();
1898 *fd = msvcrt_alloc_fd(hand, wxflag);
1900 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1904 /*********************************************************************
1905 * _wsopen (MSVCRT.@)
1907 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1912 if (oflags & MSVCRT__O_CREAT)
1916 __ms_va_start(ap, shflags);
1917 pmode = va_arg(ap, int);
1923 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1927 /*********************************************************************
1930 int CDECL MSVCRT__open( const char *path, int flags, ... )
1934 if (flags & MSVCRT__O_CREAT)
1937 __ms_va_start(ap, flags);
1938 pmode = va_arg(ap, int);
1940 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1943 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1946 /*********************************************************************
1949 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
1953 if (flags & MSVCRT__O_CREAT)
1956 __ms_va_start(ap, flags);
1957 pmode = va_arg(ap, int);
1959 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1962 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1965 /*********************************************************************
1968 int CDECL MSVCRT__creat(const char *path, int flags)
1970 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1971 return MSVCRT__open(path, usedFlags);
1974 /*********************************************************************
1975 * _wcreat (MSVCRT.@)
1977 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
1979 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1980 return MSVCRT__wopen(path, usedFlags);
1983 /*********************************************************************
1984 * _open_osfhandle (MSVCRT.@)
1986 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1990 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1991 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1992 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1993 * text - it never sets MSVCRT__O_BINARY.
1995 /* don't let split_oflags() decide the mode if no mode is passed */
1996 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1997 oflags |= MSVCRT__O_BINARY;
1999 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
2000 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
2004 /*********************************************************************
2007 int CDECL MSVCRT__rmtmp(void)
2009 int num_removed = 0, i;
2013 for (i = 3; i < MSVCRT_stream_idx; i++) {
2014 file = msvcrt_get_file(i);
2016 if (file->_tmpfname)
2018 MSVCRT_fclose(file);
2025 TRACE(":removed (%d) temp files\n",num_removed);
2029 /*********************************************************************
2032 * When reading \r as last character in text mode, read() positions
2033 * the file pointer on the \r character while getc() goes on to
2036 static int read_i(int fd, void *buf, unsigned int count)
2039 char *bufstart = buf;
2040 HANDLE hand = msvcrt_fdtoh(fd);
2041 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2046 if (fdinfo->wxflag & WX_READEOF) {
2047 fdinfo->wxflag |= WX_ATEOF;
2048 TRACE("already at EOF, returning 0\n");
2051 /* Don't trace small reads, it gets *very* annoying */
2053 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2054 if (hand == INVALID_HANDLE_VALUE)
2057 /* Reading single bytes in O_TEXT mode makes things slow
2058 * So read big chunks
2060 if (ReadFile(hand, bufstart, count, &num_read, NULL))
2062 if (count != 0 && num_read == 0)
2064 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2065 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2067 else if (fdinfo->wxflag & WX_TEXT)
2070 if (bufstart[num_read-1] == '\r')
2074 fdinfo->wxflag &= ~WX_READCR;
2075 ReadFile(hand, bufstart, 1, &num_read, NULL);
2079 fdinfo->wxflag |= WX_READCR;
2084 fdinfo->wxflag &= ~WX_READCR;
2085 for (i=0, j=0; i<num_read; i++)
2087 /* in text mode, a ctrl-z signals EOF */
2088 if (bufstart[i] == 0x1a)
2090 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2091 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2094 /* in text mode, strip \r if followed by \n.
2095 * BUG: should save state across calls somehow, so CR LF that
2096 * straddles buffer boundary gets recognized properly?
2098 if ((bufstart[i] != '\r')
2099 || ((i+1) < num_read && bufstart[i+1] != '\n'))
2100 bufstart[j++] = bufstart[i];
2107 if (GetLastError() == ERROR_BROKEN_PIPE)
2109 TRACE(":end-of-pipe\n");
2110 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2115 TRACE(":failed-last error (%d)\n",GetLastError());
2121 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2125 /*********************************************************************
2128 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2131 num_read = read_i(fd, buf, count);
2135 /*********************************************************************
2136 * _setmode (MSVCRT.@)
2138 int CDECL MSVCRT__setmode(int fd,int mode)
2140 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2141 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
2142 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
2143 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
2144 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2146 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2150 /*********************************************************************
2151 * _stat64 (MSVCRT.@)
2153 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2156 WIN32_FILE_ATTRIBUTE_DATA hfi;
2157 unsigned short mode = ALL_S_IREAD;
2160 TRACE(":file (%s) buf(%p)\n",path,buf);
2162 plen = strlen(path);
2163 while (plen && path[plen-1]==' ')
2166 if (plen && (path[plen-1]=='\\' || path[plen-1]=='/'))
2168 *MSVCRT__errno() = MSVCRT_ENOENT;
2172 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2174 TRACE("failed (%d)\n",GetLastError());
2175 *MSVCRT__errno() = MSVCRT_ENOENT;
2179 memset(buf,0,sizeof(struct MSVCRT__stat64));
2181 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2182 Bon 011120: This FIXME seems incorrect
2183 Also a letter as first char isn't enough to be classified
2186 if (isalpha(*path)&& (*(path+1)==':'))
2187 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2189 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2191 /* Dir, or regular file? */
2192 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2193 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2196 mode |= MSVCRT__S_IFREG;
2198 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2200 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2201 (tolower(path[plen-3]) << 16);
2202 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2203 mode |= ALL_S_IEXEC;
2207 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2208 mode |= ALL_S_IWRITE;
2210 buf->st_mode = mode;
2212 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2213 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2215 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2216 buf->st_mtime = buf->st_ctime = dw;
2217 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2218 (int)(buf->st_size >> 32),(int)buf->st_size,
2219 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2223 /*********************************************************************
2224 * _stati64 (MSVCRT.@)
2226 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2229 struct MSVCRT__stat64 buf64;
2231 ret = MSVCRT_stat64(path, &buf64);
2233 msvcrt_stat64_to_stati64(&buf64, buf);
2237 /*********************************************************************
2240 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2242 struct MSVCRT__stat64 buf64;
2244 ret = MSVCRT_stat64( path, &buf64);
2246 msvcrt_stat64_to_stat(&buf64, buf);
2250 /*********************************************************************
2251 * _wstat64 (MSVCRT.@)
2253 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2256 WIN32_FILE_ATTRIBUTE_DATA hfi;
2257 unsigned short mode = ALL_S_IREAD;
2260 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2262 plen = strlenW(path);
2263 while (plen && path[plen-1]==' ')
2266 if(plen && (path[plen-1]=='\\' || path[plen-1]=='/'))
2268 *MSVCRT__errno() = MSVCRT_ENOENT;
2272 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2274 TRACE("failed (%d)\n",GetLastError());
2275 *MSVCRT__errno() = MSVCRT_ENOENT;
2279 memset(buf,0,sizeof(struct MSVCRT__stat64));
2281 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2282 if (MSVCRT_iswalpha(*path))
2283 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2285 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2287 /* Dir, or regular file? */
2288 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2289 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2292 mode |= MSVCRT__S_IFREG;
2294 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2296 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2297 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2298 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2299 mode |= ALL_S_IEXEC;
2303 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2304 mode |= ALL_S_IWRITE;
2306 buf->st_mode = mode;
2308 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2309 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2311 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2312 buf->st_mtime = buf->st_ctime = dw;
2313 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2314 (int)(buf->st_size >> 32),(int)buf->st_size,
2315 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2319 /*********************************************************************
2320 * _wstati64 (MSVCRT.@)
2322 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2325 struct MSVCRT__stat64 buf64;
2327 ret = MSVCRT__wstat64(path, &buf64);
2329 msvcrt_stat64_to_stati64(&buf64, buf);
2333 /*********************************************************************
2336 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2339 struct MSVCRT__stat64 buf64;
2341 ret = MSVCRT__wstat64( path, &buf64 );
2342 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2346 /*********************************************************************
2349 MSVCRT_long CDECL MSVCRT__tell(int fd)
2351 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2354 /*********************************************************************
2355 * _telli64 (MSVCRT.@)
2357 __int64 CDECL _telli64(int fd)
2359 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2362 /*********************************************************************
2363 * _tempnam (MSVCRT.@)
2365 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
2367 char tmpbuf[MAX_PATH];
2368 const char *tmp_dir = MSVCRT_getenv("TMP");
2370 if (tmp_dir) dir = tmp_dir;
2372 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2373 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2375 TRACE("got name (%s)\n",tmpbuf);
2376 DeleteFileA(tmpbuf);
2377 return MSVCRT__strdup(tmpbuf);
2379 TRACE("failed (%d)\n",GetLastError());
2383 /*********************************************************************
2384 * _wtempnam (MSVCRT.@)
2386 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2388 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
2389 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2390 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
2392 if (tmp_dir) dir = tmp_dir;
2394 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2395 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2397 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2398 DeleteFileW(tmpbuf);
2399 return MSVCRT__wcsdup(tmpbuf);
2401 TRACE("failed (%d)\n",GetLastError());
2405 /*********************************************************************
2408 int CDECL MSVCRT__umask(int umask)
2410 int old_umask = MSVCRT_umask;
2411 TRACE("(%d)\n",umask);
2412 MSVCRT_umask = umask;
2416 /*********************************************************************
2417 * _utime64 (MSVCRT.@)
2419 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2421 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2425 int retVal = _futime64(fd, t);
2432 /*********************************************************************
2433 * _utime32 (MSVCRT.@)
2435 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2439 struct MSVCRT___utimbuf64 t64;
2440 t64.actime = t->actime;
2441 t64.modtime = t->modtime;
2442 return _utime64( path, &t64 );
2445 return _utime64( path, NULL );
2448 /*********************************************************************
2452 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2454 return _utime64( path, t );
2457 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2459 return _utime32( path, t );
2463 /*********************************************************************
2464 * _wutime64 (MSVCRT.@)
2466 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2468 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2472 int retVal = _futime64(fd, t);
2479 /*********************************************************************
2480 * _wutime32 (MSVCRT.@)
2482 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2486 struct MSVCRT___utimbuf64 t64;
2487 t64.actime = t->actime;
2488 t64.modtime = t->modtime;
2489 return _wutime64( path, &t64 );
2492 return _wutime64( path, NULL );
2495 /*********************************************************************
2496 * _wutime (MSVCRT.@)
2499 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2501 return _wutime64( path, t );
2504 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2506 return _wutime32( path, t );
2510 /*********************************************************************
2513 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2516 HANDLE hand = msvcrt_fdtoh(fd);
2518 /* Don't trace small writes, it gets *very* annoying */
2521 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2523 if (hand == INVALID_HANDLE_VALUE)
2525 *MSVCRT__errno() = MSVCRT_EBADF;
2529 /* If appending, go to EOF */
2530 if (msvcrt_get_ioinfo(fd)->wxflag & WX_APPEND)
2531 MSVCRT__lseek(fd, 0, FILE_END);
2533 if (!(msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT))
2535 if (WriteFile(hand, buf, count, &num_written, NULL)
2536 && (num_written == count))
2538 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2539 hand, GetLastError());
2540 *MSVCRT__errno() = MSVCRT_ENOSPC;
2544 unsigned int i, j, nr_lf;
2547 const char *s = buf, *buf_start = buf;
2548 /* find number of \n ( without preceding \r ) */
2549 for ( nr_lf=0,i = 0; i <count; i++)
2554 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2559 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2561 for (s = buf, i = 0, j = 0; i < count; i++)
2566 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2573 FIXME("Malloc failed\n");
2581 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2583 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2584 fd, hand, GetLastError(), num_written);
2585 *MSVCRT__errno() = MSVCRT_ENOSPC;
2588 return s - buf_start;
2600 /*********************************************************************
2603 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2607 MSVCRT__lock_file(file);
2608 len = MSVCRT__write(file->_file, &val, sizeof(val));
2609 if (len == sizeof(val)) {
2610 MSVCRT__unlock_file(file);
2614 file->_flag |= MSVCRT__IOERR;
2615 MSVCRT__unlock_file(file);
2619 /*********************************************************************
2622 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2626 MSVCRT__lock_file(file);
2628 MSVCRT_free(file->_tmpfname);
2629 file->_tmpfname = NULL;
2630 /* flush stdio buffers */
2631 if(file->_flag & MSVCRT__IOWRT)
2632 MSVCRT_fflush(file);
2633 if(file->_flag & MSVCRT__IOMYBUF)
2634 MSVCRT_free(file->_base);
2636 r=MSVCRT__close(file->_file);
2639 MSVCRT__unlock_file(file);
2641 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2644 /*********************************************************************
2647 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2649 return file->_flag & MSVCRT__IOEOF;
2652 /*********************************************************************
2655 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2657 return file->_flag & MSVCRT__IOERR;
2660 /*********************************************************************
2661 * _filbuf (MSVCRT.@)
2663 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2666 MSVCRT__lock_file(file);
2668 /* Allocate buffer if needed */
2669 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
2670 msvcrt_alloc_buffer(file);
2672 if(!(file->_flag & MSVCRT__IOREAD)) {
2673 if(file->_flag & MSVCRT__IORW)
2674 file->_flag |= MSVCRT__IOREAD;
2676 MSVCRT__unlock_file(file);
2681 if(file->_flag & MSVCRT__IONBF) {
2683 if ((r = read_i(file->_file,&c,1)) != 1) {
2684 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2685 MSVCRT__unlock_file(file);
2689 MSVCRT__unlock_file(file);
2692 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2694 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2696 MSVCRT__unlock_file(file);
2701 file->_ptr = file->_base+1;
2702 c = *(unsigned char *)file->_base;
2703 MSVCRT__unlock_file(file);
2708 /*********************************************************************
2711 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2716 MSVCRT__lock_file(file);
2719 i = (unsigned char *)file->_ptr++;
2722 j = MSVCRT__filbuf(file);
2724 MSVCRT__unlock_file(file);
2728 /*********************************************************************
2729 * _fgetchar (MSVCRT.@)
2731 int CDECL MSVCRT__fgetchar(void)
2733 return MSVCRT_fgetc(MSVCRT_stdin);
2736 /*********************************************************************
2739 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2741 int cc = MSVCRT_EOF;
2742 char * buf_start = s;
2744 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2745 file,file->_file,s,size);
2747 MSVCRT__lock_file(file);
2749 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2754 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2756 TRACE(":nothing read\n");
2757 MSVCRT__unlock_file(file);
2760 if ((cc != MSVCRT_EOF) && (size > 1))
2763 TRACE(":got %s\n", debugstr_a(buf_start));
2764 MSVCRT__unlock_file(file);
2768 /*********************************************************************
2771 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2772 * the CR from CR/LF combinations
2774 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2778 MSVCRT__lock_file(file);
2779 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
2786 for(i=0; i<sizeof(wc); i++)
2796 j = MSVCRT__filbuf(file);
2799 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2802 MSVCRT__unlock_file(file);
2809 MSVCRT__unlock_file(file);
2813 c = MSVCRT_fgetc(file);
2814 if ((get_locinfo()->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2816 FIXME("Treat Multibyte characters\n");
2819 MSVCRT__unlock_file(file);
2820 if (c == MSVCRT_EOF)
2823 return (MSVCRT_wint_t)c;
2826 /*********************************************************************
2829 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2836 MSVCRT__lock_file(file);
2837 for (j=0; j<sizeof(int); j++) {
2838 k = MSVCRT_fgetc(file);
2839 if (k == MSVCRT_EOF) {
2840 file->_flag |= MSVCRT__IOEOF;
2841 MSVCRT__unlock_file(file);
2847 MSVCRT__unlock_file(file);
2851 /*********************************************************************
2854 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2856 return MSVCRT_fgetwc(file);
2859 /*********************************************************************
2860 * _fgetwchar (MSVCRT.@)
2862 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
2864 return MSVCRT_fgetwc(MSVCRT_stdin);
2867 /*********************************************************************
2868 * getwchar (MSVCRT.@)
2870 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2872 return MSVCRT__fgetwchar();
2875 /*********************************************************************
2878 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2880 int cc = MSVCRT_WEOF;
2881 MSVCRT_wchar_t * buf_start = s;
2883 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2884 file,file->_file,s,size);
2886 MSVCRT__lock_file(file);
2888 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2893 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2895 TRACE(":nothing read\n");
2896 MSVCRT__unlock_file(file);
2899 if ((cc != MSVCRT_WEOF) && (size > 1))
2902 TRACE(":got %s\n", debugstr_w(buf_start));
2903 MSVCRT__unlock_file(file);
2907 /*********************************************************************
2910 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2912 MSVCRT_size_t wrcnt=size * nmemb;
2917 MSVCRT__lock_file(file);
2919 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2920 memcpy(file->_ptr, ptr, pcnt);
2925 ptr = (const char*)ptr + pcnt;
2926 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2927 if(file->_flag & MSVCRT__IORW) {
2928 file->_flag |= MSVCRT__IOWRT;
2930 MSVCRT__unlock_file(file);
2937 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
2938 msvcrt_alloc_buffer(file);
2941 res=msvcrt_flush_buffer(file);
2943 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2946 file->_flag |= MSVCRT__IOERR;
2949 written += pwritten;
2953 MSVCRT__unlock_file(file);
2954 return written / size;
2957 /*********************************************************************
2960 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2962 MSVCRT_wchar_t mwc=wc;
2963 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2968 /*********************************************************************
2969 * _fputwchar (MSVCRT.@)
2971 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
2973 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2976 /*********************************************************************
2977 * _wfsopen (MSVCRT.@)
2979 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2982 int open_flags, stream_flags, fd;
2984 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2986 /* map mode string to open() flags. "man fopen" for possibilities. */
2987 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2991 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2994 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2996 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3003 TRACE(":got (%p)\n",file);
3004 if (fd >= 0 && !file)
3010 /*********************************************************************
3011 * _fsopen (MSVCRT.@)
3013 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3016 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3018 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3019 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3020 *MSVCRT__errno() = MSVCRT_EINVAL;
3023 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3026 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3027 *MSVCRT__errno() = MSVCRT_EINVAL;
3031 ret = MSVCRT__wfsopen(pathW, modeW, share);
3038 /*********************************************************************
3041 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3043 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3046 /*********************************************************************
3047 * fopen_s (MSVCRT.@)
3049 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3050 const char *filename, const char *mode)
3052 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3053 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3054 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3056 *pFile = MSVCRT_fopen(filename, mode);
3059 return *MSVCRT__errno();
3063 /*********************************************************************
3064 * _wfopen (MSVCRT.@)
3066 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3068 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3071 /*********************************************************************
3072 * _wfopen_s (MSVCRT.@)
3074 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3075 const MSVCRT_wchar_t *mode)
3077 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3078 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3079 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3081 *pFile = MSVCRT__wfopen(filename, mode);
3084 return *MSVCRT__errno();
3088 /*********************************************************************
3089 * _flsbuf (MSVCRT.@)
3091 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3093 /* Flush output buffer */
3094 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3095 msvcrt_alloc_buffer(file);
3097 if(!(file->_flag & MSVCRT__IOWRT)) {
3098 if(file->_flag & MSVCRT__IORW)
3099 file->_flag |= MSVCRT__IOWRT;
3107 res = msvcrt_flush_buffer(file);
3111 res = msvcrt_flush_buffer(file);
3114 return res ? res : c&0xff;
3118 /* set _cnt to 0 for unbuffered FILEs */
3120 len = MSVCRT__write(file->_file, &cc, 1);
3123 file->_flag |= MSVCRT__IOERR;
3128 /*********************************************************************
3131 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3135 MSVCRT__lock_file(file);
3141 res = msvcrt_flush_buffer(file);
3142 MSVCRT__unlock_file(file);
3143 return res ? res : c;
3146 MSVCRT__unlock_file(file);
3150 res = MSVCRT__flsbuf(c, file);
3151 MSVCRT__unlock_file(file);
3156 /*********************************************************************
3157 * _fputchar (MSVCRT.@)
3159 int CDECL MSVCRT__fputchar(int c)
3161 return MSVCRT_fputc(c, MSVCRT_stdout);
3164 /*********************************************************************
3167 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3169 MSVCRT_size_t rcnt=size * nmemb;
3170 MSVCRT_size_t read=0;
3176 MSVCRT__lock_file(file);
3178 /* first buffered data */
3180 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3181 memcpy(ptr, file->_ptr, pcnt);
3186 ptr = (char*)ptr + pcnt;
3187 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3188 if(file->_flag & MSVCRT__IORW) {
3189 file->_flag |= MSVCRT__IOREAD;
3191 MSVCRT__unlock_file(file);
3198 /* Fill the buffer on small reads.
3199 * TODO: Use a better buffering strategy.
3201 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
3202 if (file->_bufsiz == 0) {
3203 msvcrt_alloc_buffer(file);
3205 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3206 file->_ptr = file->_base;
3207 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3208 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3209 if (i > 0 && i < file->_cnt) {
3210 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3211 file->_flag &= ~MSVCRT__IOEOF;
3214 memcpy(ptr, file->_ptr, i);
3219 i = MSVCRT__read(file->_file,ptr, rcnt);
3223 ptr = (char *)ptr+i;
3224 /* expose feof condition in the flags
3225 * MFC tests file->_flag for feof, and doesn't call feof())
3227 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3228 file->_flag |= MSVCRT__IOEOF;
3231 file->_flag |= MSVCRT__IOERR;
3238 MSVCRT__unlock_file(file);
3242 /*********************************************************************
3243 * _wfreopen (MSVCRT.@)
3246 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3250 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3253 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3257 MSVCRT_fclose(file);
3258 file = MSVCRT__wfsopen(path, mode, MSVCRT__SH_DENYNO);
3264 /*********************************************************************
3265 * _wfreopen_s (MSVCRT.@)
3267 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
3268 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3270 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3271 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
3272 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3273 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3275 *pFile = MSVCRT__wfreopen(path, mode, file);
3278 return *MSVCRT__errno();
3282 /*********************************************************************
3283 * freopen (MSVCRT.@)
3286 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3289 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3291 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3292 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3298 ret = MSVCRT__wfreopen(pathW, modeW, file);
3305 /*********************************************************************
3306 * freopen_s (MSVCRT.@)
3308 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
3309 const char *path, const char *mode, MSVCRT_FILE* file)
3311 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3312 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
3313 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3314 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3316 *pFile = MSVCRT_freopen(path, mode, file);
3319 return *MSVCRT__errno();
3323 /*********************************************************************
3324 * fsetpos (MSVCRT.@)
3326 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3330 MSVCRT__lock_file(file);
3331 /* Note that all this has been lifted 'as is' from fseek */
3332 if(file->_flag & MSVCRT__IOWRT)
3333 msvcrt_flush_buffer(file);
3335 /* Discard buffered input */
3337 file->_ptr = file->_base;
3339 /* Reset direction of i/o */
3340 if(file->_flag & MSVCRT__IORW) {
3341 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3344 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3345 MSVCRT__unlock_file(file);
3349 /*********************************************************************
3350 * _ftelli64 (MSVCRT.@)
3352 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3354 /* TODO: just call fgetpos and return lower half of result */
3358 MSVCRT__lock_file(file);
3359 pos = _telli64(file->_file);
3361 MSVCRT__unlock_file(file);
3365 if( file->_flag & MSVCRT__IOWRT ) {
3366 off = file->_ptr - file->_base;
3369 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3370 /* Black magic correction for CR removal */
3372 for (i=0; i<file->_cnt; i++) {
3373 if (file->_ptr[i] == '\n')
3376 /* Black magic when reading CR at buffer boundary*/
3377 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3383 MSVCRT__unlock_file(file);
3387 /*********************************************************************
3390 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3392 return MSVCRT__ftelli64(file);
3395 /*********************************************************************
3396 * fgetpos (MSVCRT.@)
3398 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3402 MSVCRT__lock_file(file);
3403 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3405 MSVCRT__unlock_file(file);
3409 if( file->_flag & MSVCRT__IOWRT ) {
3410 off = file->_ptr - file->_base;
3413 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3414 /* Black magic correction for CR removal */
3416 for (i=0; i<file->_cnt; i++) {
3417 if (file->_ptr[i] == '\n')
3420 /* Black magic when reading CR at buffer boundary*/
3421 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3427 MSVCRT__unlock_file(file);
3431 /*********************************************************************
3434 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3436 MSVCRT_size_t len = strlen(s);
3439 MSVCRT__lock_file(file);
3440 ret = MSVCRT_fwrite(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
3441 MSVCRT__unlock_file(file);
3445 /*********************************************************************
3448 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3450 MSVCRT_size_t i, len = strlenW(s);
3453 MSVCRT__lock_file(file);
3454 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3455 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3456 MSVCRT__unlock_file(file);
3459 for (i=0; i<len; i++) {
3460 if (((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3461 || MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
3462 MSVCRT__unlock_file(file);
3467 MSVCRT__unlock_file(file);
3471 /*********************************************************************
3472 * getchar (MSVCRT.@)
3474 int CDECL MSVCRT_getchar(void)
3476 return MSVCRT_fgetc(MSVCRT_stdin);
3479 /*********************************************************************
3482 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3484 return MSVCRT_fgetc(file);
3487 /*********************************************************************
3490 char * CDECL MSVCRT_gets(char *buf)
3493 char * buf_start = buf;
3495 MSVCRT__lock_file(MSVCRT_stdin);
3496 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3497 cc = MSVCRT_fgetc(MSVCRT_stdin))
3498 if(cc != '\r') *buf++ = (char)cc;
3502 TRACE("got '%s'\n", buf_start);
3503 MSVCRT__unlock_file(MSVCRT_stdin);
3507 /*********************************************************************
3510 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3513 MSVCRT_wchar_t* ws = buf;
3515 MSVCRT__lock_file(MSVCRT_stdin);
3516 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3517 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3520 *buf++ = (MSVCRT_wchar_t)cc;
3524 TRACE("got %s\n", debugstr_w(ws));
3525 MSVCRT__unlock_file(MSVCRT_stdin);
3529 /*********************************************************************
3532 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3534 return MSVCRT_fputc(c, file);
3537 /*********************************************************************
3538 * putchar (MSVCRT.@)
3540 int CDECL MSVCRT_putchar(int c)
3542 return MSVCRT_fputc(c, MSVCRT_stdout);
3545 /*********************************************************************
3546 * _putwch (MSVCRT.@)
3548 int CDECL MSVCRT__putwch(int c)
3550 return MSVCRT_fputwc(c, MSVCRT_stdout);
3553 /*********************************************************************
3556 int CDECL MSVCRT_puts(const char *s)
3558 MSVCRT_size_t len = strlen(s);
3561 MSVCRT__lock_file(MSVCRT_stdout);
3562 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3563 MSVCRT__unlock_file(MSVCRT_stdout);
3567 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3568 MSVCRT__unlock_file(MSVCRT_stdout);
3572 /*********************************************************************
3575 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
3577 static const MSVCRT_wchar_t nl = '\n';
3578 MSVCRT_size_t len = strlenW(s);
3581 MSVCRT__lock_file(MSVCRT_stdout);
3582 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3583 MSVCRT__unlock_file(MSVCRT_stdout);
3587 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3588 MSVCRT__unlock_file(MSVCRT_stdout);
3592 /*********************************************************************
3595 int CDECL MSVCRT_remove(const char *path)
3597 TRACE("(%s)\n",path);
3598 if (DeleteFileA(path))
3600 TRACE(":failed (%d)\n",GetLastError());
3601 msvcrt_set_errno(GetLastError());
3605 /*********************************************************************
3606 * _wremove (MSVCRT.@)
3608 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
3610 TRACE("(%s)\n",debugstr_w(path));
3611 if (DeleteFileW(path))
3613 TRACE(":failed (%d)\n",GetLastError());
3614 msvcrt_set_errno(GetLastError());
3618 /*********************************************************************
3621 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3623 TRACE(":from %s to %s\n",oldpath,newpath);
3624 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3626 TRACE(":failed (%d)\n",GetLastError());
3627 msvcrt_set_errno(GetLastError());
3631 /*********************************************************************
3632 * _wrename (MSVCRT.@)
3634 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3636 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3637 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3639 TRACE(":failed (%d)\n",GetLastError());
3640 msvcrt_set_errno(GetLastError());
3644 /*********************************************************************
3645 * setvbuf (MSVCRT.@)
3647 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3649 MSVCRT__lock_file(file);
3651 MSVCRT_free(file->_base);
3655 if(mode == MSVCRT__IOFBF) {
3656 file->_flag &= ~MSVCRT__IONBF;
3657 file->_base = file->_ptr = buf;
3659 file->_bufsiz = size;
3662 file->_flag |= MSVCRT__IONBF;
3664 MSVCRT__unlock_file(file);
3668 /*********************************************************************
3671 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3673 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3676 /*********************************************************************
3679 char * CDECL MSVCRT_tmpnam(char *s)
3686 thread_data_t *data = msvcrt_get_thread_data();
3688 if(!data->tmpnam_buffer)
3689 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
3691 s = data->tmpnam_buffer;
3694 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3695 p = s + sprintf(s, "\\s%s.", tmpstr);
3696 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3698 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
3699 memcpy(p, tmpstr, size);
3701 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3702 GetLastError() == ERROR_FILE_NOT_FOUND)
3708 /*********************************************************************
3709 * _wtmpnam (MSVCRT.@)
3711 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
3713 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
3714 MSVCRT_wchar_t tmpstr[16];
3718 thread_data_t *data = msvcrt_get_thread_data();
3720 if(!data->wtmpnam_buffer)
3721 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
3723 s = data->wtmpnam_buffer;
3726 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
3727 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
3728 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3730 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
3731 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
3733 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
3734 GetLastError() == ERROR_FILE_NOT_FOUND)
3740 /*********************************************************************
3741 * tmpfile (MSVCRT.@)
3743 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3745 char *filename = MSVCRT_tmpnam(NULL);
3747 MSVCRT_FILE* file = NULL;
3750 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
3751 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3752 if (fd != -1 && (file = msvcrt_alloc_fp()))
3754 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
3759 else file->_tmpfname = MSVCRT__strdup(filename);
3762 if(fd != -1 && !file)
3768 /*********************************************************************
3769 * tmpfile_s (MSVCRT.@)
3771 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
3773 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3775 *file = MSVCRT_tmpfile();
3779 static int puts_clbk_file_a(void *file, int len, const char *str)
3781 return MSVCRT_fwrite(str, sizeof(char), len, file);
3784 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
3786 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
3789 /*********************************************************************
3790 * vfprintf (MSVCRT.@)
3792 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3796 MSVCRT__lock_file(file);
3797 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3798 MSVCRT__unlock_file(file);
3803 /*********************************************************************
3804 * vfprintf_s (MSVCRT.@)
3806 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3810 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
3812 MSVCRT__lock_file(file);
3813 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3814 MSVCRT__unlock_file(file);
3819 /*********************************************************************
3820 * vfwprintf (MSVCRT.@)
3822 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3826 MSVCRT__lock_file(file);
3827 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3828 MSVCRT__unlock_file(file);
3833 /*********************************************************************
3834 * vfwprintf_s (MSVCRT.@)
3836 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3840 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
3842 MSVCRT__lock_file(file);
3843 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3844 MSVCRT__unlock_file(file);
3849 /*********************************************************************
3850 * _vfwprintf_l (MSVCRT.@)
3852 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
3853 MSVCRT__locale_t locale, __ms_va_list valist)
3857 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
3859 MSVCRT__lock_file(file);
3860 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3861 MSVCRT__unlock_file(file);
3866 /*********************************************************************
3867 * vprintf (MSVCRT.@)
3869 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3871 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3874 /*********************************************************************
3875 * vprintf_s (MSVCRT.@)
3877 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
3879 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
3882 /*********************************************************************
3883 * vwprintf (MSVCRT.@)
3885 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3887 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3890 /*********************************************************************
3891 * vwprintf_s (MSVCRT.@)
3893 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
3895 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
3898 /*********************************************************************
3899 * fprintf (MSVCRT.@)
3901 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3903 __ms_va_list valist;
3905 __ms_va_start(valist, format);
3906 res = MSVCRT_vfprintf(file, format, valist);
3907 __ms_va_end(valist);
3911 /*********************************************************************
3912 * fprintf_s (MSVCRT.@)
3914 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
3916 __ms_va_list valist;
3918 __ms_va_start(valist, format);
3919 res = MSVCRT_vfprintf_s(file, format, valist);
3920 __ms_va_end(valist);
3924 /*********************************************************************
3925 * fwprintf (MSVCRT.@)
3927 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3929 __ms_va_list valist;
3931 __ms_va_start(valist, format);
3932 res = MSVCRT_vfwprintf(file, format, valist);
3933 __ms_va_end(valist);
3937 /*********************************************************************
3938 * fwprintf_s (MSVCRT.@)
3940 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3942 __ms_va_list valist;
3944 __ms_va_start(valist, format);
3945 res = MSVCRT_vfwprintf_s(file, format, valist);
3946 __ms_va_end(valist);
3950 /*********************************************************************
3951 * _fwprintf_l (MSVCRT.@)
3953 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
3955 __ms_va_list valist;
3957 __ms_va_start(valist, locale);
3958 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
3959 __ms_va_end(valist);
3963 /*********************************************************************
3966 int CDECL MSVCRT_printf(const char *format, ...)
3968 __ms_va_list valist;
3970 __ms_va_start(valist, format);
3971 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3972 __ms_va_end(valist);
3976 /*********************************************************************
3977 * printf_s (MSVCRT.@)
3979 int CDECL MSVCRT_printf_s(const char *format, ...)
3981 __ms_va_list valist;
3983 __ms_va_start(valist, format);
3984 res = MSVCRT_vprintf_s(format, valist);
3985 __ms_va_end(valist);
3989 /*********************************************************************
3992 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3994 if (c == MSVCRT_EOF)
3997 MSVCRT__lock_file(file);
3998 if(file->_bufsiz == 0) {
3999 msvcrt_alloc_buffer(file);
4002 if(file->_ptr>file->_base) {
4006 MSVCRT_clearerr(file);
4007 MSVCRT__unlock_file(file);
4011 MSVCRT__unlock_file(file);
4015 /*********************************************************************
4016 * ungetwc (MSVCRT.@)
4018 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
4020 MSVCRT_wchar_t mwc = wc;
4021 unsigned char * pp = (unsigned char *)&mwc;
4024 if (wc == MSVCRT_WEOF)
4027 MSVCRT__lock_file(file);
4028 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
4029 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
4030 MSVCRT__unlock_file(file);
4035 MSVCRT__unlock_file(file);
4039 /*********************************************************************
4040 * wprintf (MSVCRT.@)
4042 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
4044 __ms_va_list valist;
4046 __ms_va_start(valist, format);
4047 res = MSVCRT_vwprintf(format, valist);
4048 __ms_va_end(valist);
4052 /*********************************************************************
4053 * wprintf_s (MSVCRT.@)
4055 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
4057 __ms_va_list valist;
4059 __ms_va_start(valist, format);
4060 res = MSVCRT_vwprintf_s(format, valist);
4061 __ms_va_end(valist);
4065 /*********************************************************************
4066 * _getmaxstdio (MSVCRT.@)
4068 int CDECL MSVCRT__getmaxstdio(void)
4070 return MSVCRT_max_streams;
4073 /*********************************************************************
4074 * _setmaxstdio (MSVCRT.@)
4076 int CDECL MSVCRT__setmaxstdio(int newmax)
4078 TRACE("%d\n", newmax);
4080 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4083 MSVCRT_max_streams = newmax;
4084 return MSVCRT_max_streams;