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_READNL 0x04 /* read started with \n */
65 #define WX_DONTINHERIT 0x10
66 #define WX_APPEND 0x20
69 /* values for exflag - it's used differently in msvcr90.dll*/
72 #define EF_CRIT_INIT 0x04
73 #define EF_UNK_UNICODE 0x08
75 static char utf8_bom[3] = { 0xef, 0xbb, 0xbf };
76 static char utf16_bom[2] = { 0xff, 0xfe };
78 /* FIXME: this should be allocated dynamically */
79 #define MSVCRT_MAX_FILES 2048
80 #define MSVCRT_FD_BLOCK_SIZE 32
82 /* ioinfo structure size is different in msvcrXX.dll's */
88 CRITICAL_SECTION crit;
91 /*********************************************************************
92 * __pioinfo (MSVCRT.@)
93 * array of pointers to ioinfo arrays [32]
95 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
97 /*********************************************************************
98 * __badioinfo (MSVCRT.@)
100 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
102 static int MSVCRT_fdstart = 3; /* first unallocated fd */
103 static int MSVCRT_fdend = 3; /* highest allocated fd */
107 CRITICAL_SECTION crit;
110 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
111 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
112 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
114 /* INTERNAL: process umask */
115 static int MSVCRT_umask = 0;
117 /* INTERNAL: static data for tmpnam and _wtmpname functions */
118 static int tmpnam_unique;
120 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
121 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
122 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
123 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
125 #define TOUL(x) (ULONGLONG)(x)
126 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
127 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
128 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
129 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
131 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
132 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
133 * and MSVCRT_stream_idx, from race conditions.
134 * It doesn't protect against race conditions manipulating the underlying files
135 * or flags; doing so would probably be better accomplished with per-file
136 * protection, rather than locking the whole table for every change.
138 static CRITICAL_SECTION MSVCRT_file_cs;
139 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
141 0, 0, &MSVCRT_file_cs,
142 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
143 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
145 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
146 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
147 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
149 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *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 msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
166 buf->st_dev = buf64->st_dev;
167 buf->st_ino = buf64->st_ino;
168 buf->st_mode = buf64->st_mode;
169 buf->st_nlink = buf64->st_nlink;
170 buf->st_uid = buf64->st_uid;
171 buf->st_gid = buf64->st_gid;
172 buf->st_rdev = buf64->st_rdev;
173 buf->st_size = buf64->st_size;
174 buf->st_atime = buf64->st_atime;
175 buf->st_mtime = buf64->st_mtime;
176 buf->st_ctime = buf64->st_ctime;
179 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
181 /* 1601 to 1970 is 369 years plus 89 leap days */
182 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
184 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
185 ft->dwHighDateTime = ticks >> 32;
186 ft->dwLowDateTime = ticks;
189 static inline ioinfo* msvcrt_get_ioinfo(int fd)
192 if(fd < MSVCRT_MAX_FILES)
193 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
195 return &MSVCRT___badioinfo;
197 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
200 static inline MSVCRT_FILE* msvcrt_get_file(int i)
204 if(i >= MSVCRT_max_streams)
208 return &MSVCRT__iob[i];
210 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
212 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
213 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
214 ERR("out of memory\n");
215 *MSVCRT__errno() = MSVCRT_ENOMEM;
219 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
221 ret += i%MSVCRT_FD_BLOCK_SIZE;
226 static inline BOOL msvcrt_is_valid_fd(int fd)
228 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
231 /* INTERNAL: Get the HANDLE for a fd
232 * This doesn't lock the table, because a failure will result in
233 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
234 * it returns a valid handle which is about to be closed, a subsequent call
235 * will fail, most likely in a sane way.
237 static HANDLE msvcrt_fdtoh(int fd)
239 if (!msvcrt_is_valid_fd(fd))
241 WARN(":fd (%d) - no handle!\n",fd);
242 *MSVCRT___doserrno() = 0;
243 *MSVCRT__errno() = MSVCRT_EBADF;
244 return INVALID_HANDLE_VALUE;
246 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
247 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd);
248 return msvcrt_get_ioinfo(fd)->handle;
251 /* INTERNAL: free a file entry fd */
252 static void msvcrt_free_fd(int fd)
258 fdinfo = msvcrt_get_ioinfo(fd);
259 old_handle = fdinfo->handle;
260 if(fdinfo != &MSVCRT___badioinfo)
262 fdinfo->handle = INVALID_HANDLE_VALUE;
265 TRACE(":fd (%d) freed\n",fd);
266 if (fd < 3) /* don't use 0,1,2 for user files */
271 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
274 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
277 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
283 if (fd == MSVCRT_fdend - 1)
285 if (fd < MSVCRT_fdstart)
291 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
292 /* caller must hold the files lock */
293 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
297 if (fd >= MSVCRT_MAX_FILES)
299 WARN(":files exhausted!\n");
300 *MSVCRT__errno() = MSVCRT_ENFILE;
304 fdinfo = msvcrt_get_ioinfo(fd);
305 if(fdinfo == &MSVCRT___badioinfo) {
308 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
309 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
310 WARN(":out of memory!\n");
311 *MSVCRT__errno() = MSVCRT_ENOMEM;
315 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
316 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
318 fdinfo = msvcrt_get_ioinfo(fd);
321 fdinfo->handle = hand;
322 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE));
323 fdinfo->lookahead[0] = '\n';
324 fdinfo->lookahead[1] = '\n';
325 fdinfo->lookahead[2] = '\n';
328 /* locate next free slot */
329 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
330 MSVCRT_fdstart = MSVCRT_fdend + 1;
332 while (MSVCRT_fdstart < MSVCRT_fdend &&
333 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
335 /* update last fd in use */
336 if (fd >= MSVCRT_fdend)
337 MSVCRT_fdend = fd + 1;
338 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
342 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
343 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
344 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
350 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
351 static int msvcrt_alloc_fd(HANDLE hand, int flag)
356 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
357 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
362 /* INTERNAL: Allocate a FILE* for an fd slot */
363 /* caller must hold the files lock */
364 static MSVCRT_FILE* msvcrt_alloc_fp(void)
369 for (i = 3; i < MSVCRT_max_streams; i++)
371 file = msvcrt_get_file(i);
375 if (file->_flag == 0)
377 if (i == MSVCRT_stream_idx)
379 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
381 InitializeCriticalSection(&((file_crit*)file)->crit);
382 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
393 /* INTERNAL: initialize a FILE* from an open fd */
394 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
396 TRACE(":fd (%d) allocating FILE*\n",fd);
397 if (!msvcrt_is_valid_fd(fd))
399 WARN(":invalid fd %d\n",fd);
400 *MSVCRT___doserrno() = 0;
401 *MSVCRT__errno() = MSVCRT_EBADF;
404 memset(file, 0, sizeof(*file));
406 file->_flag = stream_flags;
408 TRACE(":got FILE* (%p)\n",file);
412 /* INTERNAL: Create an inheritance data block (for spawned process)
413 * The inheritance block is made of:
414 * 00 int nb of file descriptor (NBFD)
415 * 04 char file flags (wxflag): repeated for each fd
416 * 4+NBFD HANDLE file handle: repeated for each fd
418 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
425 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
426 *block = MSVCRT_calloc(*size, 1);
432 wxflag_ptr = (char*)*block + sizeof(unsigned);
433 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
435 *(unsigned*)*block = MSVCRT_fdend;
436 for (fd = 0; fd < MSVCRT_fdend; fd++)
438 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
439 fdinfo = msvcrt_get_ioinfo(fd);
440 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
442 *wxflag_ptr = fdinfo->wxflag;
443 *handle_ptr = fdinfo->handle;
448 *handle_ptr = INVALID_HANDLE_VALUE;
450 wxflag_ptr++; handle_ptr++;
455 /* INTERNAL: Set up all file descriptors,
456 * as well as default streams (stdin, stderr and stdout)
458 void msvcrt_init_io(void)
464 GetStartupInfoA(&si);
465 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
471 count = *(unsigned*)si.lpReserved2;
472 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
473 handle_ptr = (HANDLE*)(wxflag_ptr + count);
475 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
476 count = min(count, MSVCRT_MAX_FILES);
477 for (i = 0; i < count; i++)
479 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
480 msvcrt_alloc_fd_from(*handle_ptr, *wxflag_ptr, i);
482 wxflag_ptr++; handle_ptr++;
484 MSVCRT_fdend = max( 3, count );
485 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
486 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
489 if(!MSVCRT___pioinfo[0])
490 msvcrt_alloc_fd_from(INVALID_HANDLE_VALUE, 0, 3);
492 fdinfo = msvcrt_get_ioinfo(0);
493 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
495 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
496 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
497 GetCurrentProcess(), &fdinfo->handle,
498 0, TRUE, DUPLICATE_SAME_ACCESS))
499 fdinfo->wxflag = WX_OPEN | WX_TEXT;
500 fdinfo->lookahead[0] = '\n';
501 fdinfo->lookahead[1] = '\n';
502 fdinfo->lookahead[2] = '\n';
506 fdinfo = msvcrt_get_ioinfo(1);
507 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
509 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
510 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
511 GetCurrentProcess(), &fdinfo->handle,
512 0, TRUE, DUPLICATE_SAME_ACCESS))
513 fdinfo->wxflag = WX_OPEN | WX_TEXT;
514 fdinfo->lookahead[0] = '\n';
515 fdinfo->lookahead[1] = '\n';
516 fdinfo->lookahead[2] = '\n';
520 fdinfo = msvcrt_get_ioinfo(2);
521 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
523 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
524 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
525 GetCurrentProcess(), &fdinfo->handle,
526 0, TRUE, DUPLICATE_SAME_ACCESS))
527 fdinfo->wxflag = WX_OPEN | WX_TEXT;
528 fdinfo->lookahead[0] = '\n';
529 fdinfo->lookahead[1] = '\n';
530 fdinfo->lookahead[2] = '\n';
534 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(0)->handle,
535 msvcrt_get_ioinfo(1)->handle, msvcrt_get_ioinfo(2)->handle);
537 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
538 for (i = 0; i < 3; i++)
540 /* FILE structs for stdin/out/err are static and never deleted */
541 MSVCRT__iob[i]._file = i;
542 MSVCRT__iob[i]._tmpfname = NULL;
543 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
545 MSVCRT_stream_idx = 3;
548 /* INTERNAL: Flush stdio file buffer */
549 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
552 int cnt=file->_ptr-file->_base;
553 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
554 file->_flag |= MSVCRT__IOERR;
557 file->_ptr=file->_base;
558 file->_cnt=file->_bufsiz;
563 /* INTERNAL: Allocate stdio file buffer */
564 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
566 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
568 file->_bufsiz = MSVCRT_BUFSIZ;
569 file->_flag |= MSVCRT__IOMYBUF;
571 file->_base = (char*)(&file->_charbuf);
573 file->_bufsiz = sizeof(file->_charbuf);
575 file->_ptr = file->_base;
579 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
580 static int msvcrt_int_to_base32(int num, char *str)
595 *p = (num & 31) + '0';
597 *p += ('a' - '0' - 10);
604 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
605 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
620 *p = (num & 31) + '0';
622 *p += ('a' - '0' - 10);
629 /*********************************************************************
630 * __iob_func(MSVCRT.@)
632 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
634 return &MSVCRT__iob[0];
637 /*********************************************************************
640 int CDECL MSVCRT__access(const char *filename, int mode)
642 DWORD attr = GetFileAttributesA(filename);
644 TRACE("(%s,%d) %d\n",filename,mode,attr);
646 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
648 msvcrt_set_errno(GetLastError());
651 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
653 msvcrt_set_errno(ERROR_ACCESS_DENIED);
659 /*********************************************************************
660 * _access_s (MSVCRT.@)
662 int CDECL _access_s(const char *filename, int mode)
664 if (!MSVCRT_CHECK_PMT(filename != NULL)) return -1;
665 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return -1;
667 return MSVCRT__access(filename, mode);
670 /*********************************************************************
671 * _waccess (MSVCRT.@)
673 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
675 DWORD attr = GetFileAttributesW(filename);
677 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
679 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
681 msvcrt_set_errno(GetLastError());
684 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
686 msvcrt_set_errno(ERROR_ACCESS_DENIED);
692 /*********************************************************************
693 * _waccess_s (MSVCRT.@)
695 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
697 if (!MSVCRT_CHECK_PMT(filename != NULL)) return -1;
698 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return -1;
700 return MSVCRT__waccess(filename, mode);
703 /*********************************************************************
706 int CDECL MSVCRT__chmod(const char *path, int flags)
708 DWORD oldFlags = GetFileAttributesA(path);
710 if (oldFlags != INVALID_FILE_ATTRIBUTES)
712 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
713 oldFlags | FILE_ATTRIBUTE_READONLY;
715 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
718 msvcrt_set_errno(GetLastError());
722 /*********************************************************************
725 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
727 DWORD oldFlags = GetFileAttributesW(path);
729 if (oldFlags != INVALID_FILE_ATTRIBUTES)
731 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
732 oldFlags | FILE_ATTRIBUTE_READONLY;
734 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
737 msvcrt_set_errno(GetLastError());
741 /*********************************************************************
744 int CDECL MSVCRT__unlink(const char *path)
746 TRACE("%s\n",debugstr_a(path));
747 if(DeleteFileA(path))
749 TRACE("failed (%d)\n",GetLastError());
750 msvcrt_set_errno(GetLastError());
754 /*********************************************************************
755 * _wunlink (MSVCRT.@)
757 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
759 TRACE("(%s)\n",debugstr_w(path));
760 if(DeleteFileW(path))
762 TRACE("failed (%d)\n",GetLastError());
763 msvcrt_set_errno(GetLastError());
767 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
768 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
770 /* INTERNAL: Flush all stream buffer */
771 static int msvcrt_flush_all_buffers(int mask)
773 int i, num_flushed = 0;
777 for (i = 3; i < MSVCRT_stream_idx; i++) {
778 file = msvcrt_get_file(i);
782 if(file->_flag & mask) {
790 TRACE(":flushed (%d) handles\n",num_flushed);
794 /*********************************************************************
795 * _flushall (MSVCRT.@)
797 int CDECL MSVCRT__flushall(void)
799 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
802 /*********************************************************************
805 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
808 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
809 } else if(file->_flag & MSVCRT__IOWRT) {
812 MSVCRT__lock_file(file);
813 res = msvcrt_flush_buffer(file);
814 MSVCRT__unlock_file(file);
817 } else if(file->_flag & MSVCRT__IOREAD) {
818 MSVCRT__lock_file(file);
820 file->_ptr = file->_base;
821 MSVCRT__unlock_file(file);
828 /*********************************************************************
831 int CDECL MSVCRT__close(int fd)
837 hand = msvcrt_fdtoh(fd);
838 TRACE(":fd (%d) handle (%p)\n",fd,hand);
839 if (!msvcrt_is_valid_fd(fd)) {
843 ret = CloseHandle(hand) ? 0 : -1;
845 WARN(":failed-last error (%d)\n",GetLastError());
846 msvcrt_set_errno(GetLastError());
854 /*********************************************************************
857 int CDECL MSVCRT__commit(int fd)
859 HANDLE hand = msvcrt_fdtoh(fd);
861 TRACE(":fd (%d) handle (%p)\n",fd,hand);
862 if (hand == INVALID_HANDLE_VALUE)
865 if (!FlushFileBuffers(hand))
867 if (GetLastError() == ERROR_INVALID_HANDLE)
869 /* FlushFileBuffers fails for console handles
870 * so we ignore this error.
874 TRACE(":failed-last error (%d)\n",GetLastError());
875 msvcrt_set_errno(GetLastError());
882 /*********************************************************************
885 * MSDN isn't clear on this point, but the remarks for _pipe
886 * indicate file descriptors duplicated with _dup and _dup2 are always
889 int CDECL MSVCRT__dup2(int od, int nd)
893 TRACE("(od=%d, nd=%d)\n", od, nd);
895 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
899 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
900 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
902 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
904 if (msvcrt_is_valid_fd(nd))
906 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
910 *MSVCRT__errno() = MSVCRT_EMFILE;
914 /* _dup2 returns 0, not nd, on success */
921 msvcrt_set_errno(GetLastError());
926 *MSVCRT__errno() = MSVCRT_EBADF;
933 /*********************************************************************
936 int CDECL MSVCRT__dup(int od)
942 if (MSVCRT__dup2(od, fd) == 0)
950 /*********************************************************************
953 int CDECL MSVCRT__eof(int fd)
956 LONG hcurpos,hendpos;
957 HANDLE hand = msvcrt_fdtoh(fd);
959 TRACE(":fd (%d) handle (%p)\n",fd,hand);
961 if (hand == INVALID_HANDLE_VALUE)
964 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
966 /* Otherwise we do it the hard way */
967 hcurpos = hendpos = 0;
968 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
969 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
971 if (curpos == endpos && hcurpos == hendpos)
973 /* FIXME: shouldn't WX_ATEOF be set here? */
977 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
981 /*********************************************************************
982 * _fcloseall (MSVCRT.@)
984 int CDECL MSVCRT__fcloseall(void)
986 int num_closed = 0, i;
990 for (i = 3; i < MSVCRT_stream_idx; i++) {
991 file = msvcrt_get_file(i);
993 if (file->_flag && !MSVCRT_fclose(file))
998 TRACE(":closed (%d) handles\n",num_closed);
1002 /* free everything on process exit */
1003 void msvcrt_free_io(void)
1007 MSVCRT__fcloseall();
1008 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
1009 * stdout, and stderr (unlike GNU), so we need to fclose() them here
1010 * or they won't get flushed.
1012 MSVCRT_fclose(&MSVCRT__iob[0]);
1013 MSVCRT_fclose(&MSVCRT__iob[1]);
1014 MSVCRT_fclose(&MSVCRT__iob[2]);
1016 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1017 MSVCRT_free(MSVCRT___pioinfo[i]);
1019 for(i=0; i<MSVCRT_stream_idx; i++)
1021 MSVCRT_FILE *file = msvcrt_get_file(i);
1022 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1024 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1025 DeleteCriticalSection(&((file_crit*)file)->crit);
1029 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1030 MSVCRT_free(MSVCRT_fstream[i]);
1032 DeleteCriticalSection(&MSVCRT_file_cs);
1035 /*********************************************************************
1036 * _lseeki64 (MSVCRT.@)
1038 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1040 HANDLE hand = msvcrt_fdtoh(fd);
1043 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1044 if (hand == INVALID_HANDLE_VALUE)
1047 if (whence < 0 || whence > 2)
1049 *MSVCRT__errno() = MSVCRT_EINVAL;
1053 TRACE(":fd (%d) to %s pos %s\n",
1054 fd,wine_dbgstr_longlong(offset),
1055 (whence==SEEK_SET)?"SEEK_SET":
1056 (whence==SEEK_CUR)?"SEEK_CUR":
1057 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1059 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1060 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1061 ofs.QuadPart = offset;
1062 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1063 GetLastError() == ERROR_SUCCESS)
1065 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_ATEOF;
1066 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1068 return ofs.QuadPart;
1070 TRACE(":error-last error (%d)\n",GetLastError());
1071 msvcrt_set_errno(GetLastError());
1075 /*********************************************************************
1078 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1080 return MSVCRT__lseeki64(fd, offset, whence);
1083 /*********************************************************************
1084 * _lock_file (MSVCRT.@)
1086 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1088 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1089 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1091 EnterCriticalSection(&((file_crit*)file)->crit);
1094 /*********************************************************************
1095 * _unlock_file (MSVCRT.@)
1097 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1099 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1100 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1102 LeaveCriticalSection(&((file_crit*)file)->crit);
1105 /*********************************************************************
1106 * _locking (MSVCRT.@)
1108 * This is untested; the underlying LockFile doesn't work yet.
1110 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1114 HANDLE hand = msvcrt_fdtoh(fd);
1116 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1117 if (hand == INVALID_HANDLE_VALUE)
1120 if (mode < 0 || mode > 4)
1122 *MSVCRT__errno() = MSVCRT_EINVAL;
1126 TRACE(":fd (%d) by 0x%08x mode %s\n",
1127 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1128 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1129 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1130 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1131 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1134 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1136 FIXME ("Seek failed\n");
1137 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1140 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1143 ret = 1; /* just to satisfy gcc */
1146 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1151 else if (mode == MSVCRT__LK_UNLCK)
1152 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1154 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1155 /* FIXME - what about error settings? */
1156 return ret ? 0 : -1;
1159 /*********************************************************************
1160 * _fseeki64 (MSVCRT.@)
1162 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1166 MSVCRT__lock_file(file);
1167 /* Flush output if needed */
1168 if(file->_flag & MSVCRT__IOWRT)
1169 msvcrt_flush_buffer(file);
1171 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1173 offset += MSVCRT__ftelli64(file);
1176 /* Discard buffered input */
1178 file->_ptr = file->_base;
1179 /* Reset direction of i/o */
1180 if(file->_flag & MSVCRT__IORW) {
1181 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1183 /* Clear end of file flag */
1184 file->_flag &= ~MSVCRT__IOEOF;
1185 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1187 MSVCRT__unlock_file(file);
1191 /*********************************************************************
1194 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1196 return MSVCRT__fseeki64( file, offset, whence );
1199 /*********************************************************************
1200 * _chsize (MSVCRT.@)
1202 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1208 TRACE("(fd=%d, size=%d)\n", fd, size);
1212 handle = msvcrt_fdtoh(fd);
1213 if (handle != INVALID_HANDLE_VALUE)
1215 /* save the current file pointer */
1216 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1219 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1222 ret = SetEndOfFile(handle);
1223 if (!ret) msvcrt_set_errno(GetLastError());
1226 /* restore the file pointer */
1227 MSVCRT__lseek(fd, cur, SEEK_SET);
1232 return ret ? 0 : -1;
1235 /*********************************************************************
1236 * clearerr (MSVCRT.@)
1238 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1240 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1242 MSVCRT__lock_file(file);
1243 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1244 MSVCRT__unlock_file(file);
1247 /*********************************************************************
1250 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1252 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1254 MSVCRT__lock_file(file);
1255 MSVCRT_fseek(file, 0L, SEEK_SET);
1256 MSVCRT_clearerr(file);
1257 MSVCRT__unlock_file(file);
1260 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1262 int plus = strchrW(mode, '+') != NULL;
1264 TRACE("%s\n", debugstr_w(mode));
1269 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1270 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1273 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1274 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1277 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1278 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1281 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1285 while (*mode && *mode!=',')
1289 *open_flags |= MSVCRT__O_BINARY;
1290 *open_flags &= ~MSVCRT__O_TEXT;
1293 *open_flags |= MSVCRT__O_TEXT;
1294 *open_flags &= ~MSVCRT__O_BINARY;
1297 *open_flags |= MSVCRT__O_TEMPORARY;
1300 *open_flags |= MSVCRT__O_SHORT_LIVED;
1306 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1312 static const WCHAR ccs[] = {'c','c','s'};
1313 static const WCHAR utf8[] = {'u','t','f','-','8'};
1314 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1315 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1318 while(*mode == ' ') mode++;
1319 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1321 mode += sizeof(ccs)/sizeof(ccs[0]);
1322 while(*mode == ' ') mode++;
1323 if(!MSVCRT_CHECK_PMT(*mode == '='))
1326 while(*mode == ' ') mode++;
1328 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1330 *open_flags |= MSVCRT__O_U8TEXT;
1331 mode += sizeof(utf8)/sizeof(utf8[0]);
1333 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1335 *open_flags |= MSVCRT__O_U16TEXT;
1336 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1338 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1340 *open_flags |= MSVCRT__O_WTEXT;
1341 mode += sizeof(unicode)/sizeof(unicode[0]);
1345 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1349 while(*mode == ' ') mode++;
1352 if(!MSVCRT_CHECK_PMT(*mode == 0))
1357 /*********************************************************************
1358 * _fdopen (MSVCRT.@)
1360 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1363 MSVCRT_wchar_t *modeW = NULL;
1365 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1367 ret = MSVCRT__wfdopen(fd, modeW);
1373 /*********************************************************************
1374 * _wfdopen (MSVCRT.@)
1376 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1378 int open_flags, stream_flags;
1381 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1384 if (!(file = msvcrt_alloc_fp()))
1386 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1391 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1397 /*********************************************************************
1398 * _filelength (MSVCRT.@)
1400 LONG CDECL MSVCRT__filelength(int fd)
1402 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1405 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1408 if (endPos != curPos)
1409 MSVCRT__lseek(fd, curPos, SEEK_SET);
1416 /*********************************************************************
1417 * _filelengthi64 (MSVCRT.@)
1419 __int64 CDECL MSVCRT__filelengthi64(int fd)
1421 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1424 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1427 if (endPos != curPos)
1428 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1435 /*********************************************************************
1436 * _fileno (MSVCRT.@)
1438 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1440 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1444 /*********************************************************************
1445 * _fstat64 (MSVCRT.@)
1447 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1451 BY_HANDLE_FILE_INFORMATION hfi;
1452 HANDLE hand = msvcrt_fdtoh(fd);
1454 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1455 if (hand == INVALID_HANDLE_VALUE)
1460 WARN(":failed-NULL buf\n");
1461 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1465 memset(&hfi, 0, sizeof(hfi));
1466 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1467 type = GetFileType(hand);
1468 if (type == FILE_TYPE_PIPE)
1470 buf->st_dev = buf->st_rdev = fd;
1471 buf->st_mode = S_IFIFO;
1474 else if (type == FILE_TYPE_CHAR)
1476 buf->st_dev = buf->st_rdev = fd;
1477 buf->st_mode = S_IFCHR;
1480 else /* FILE_TYPE_DISK etc. */
1482 if (!GetFileInformationByHandle(hand, &hfi))
1484 WARN(":failed-last error (%d)\n",GetLastError());
1485 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1488 buf->st_mode = S_IFREG | 0444;
1489 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1490 buf->st_mode |= 0222;
1491 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1492 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1494 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1495 buf->st_mtime = buf->st_ctime = dw;
1496 buf->st_nlink = hfi.nNumberOfLinks;
1498 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1503 /*********************************************************************
1504 * _fstati64 (MSVCRT.@)
1506 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1509 struct MSVCRT__stat64 buf64;
1511 ret = MSVCRT__fstat64(fd, &buf64);
1513 msvcrt_stat64_to_stati64(&buf64, buf);
1517 /*********************************************************************
1520 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1522 struct MSVCRT__stat64 buf64;
1524 ret = MSVCRT__fstat64(fd, &buf64);
1526 msvcrt_stat64_to_stat(&buf64, buf);
1530 /*********************************************************************
1531 * _futime64 (MSVCRT.@)
1533 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1535 HANDLE hand = msvcrt_fdtoh(fd);
1540 time_to_filetime( MSVCRT__time64(NULL), &at );
1545 time_to_filetime( t->actime, &at );
1546 time_to_filetime( t->modtime, &wt );
1549 if (!SetFileTime(hand, NULL, &at, &wt))
1551 msvcrt_set_errno(GetLastError());
1557 /*********************************************************************
1558 * _futime32 (MSVCRT.@)
1560 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1564 struct MSVCRT___utimbuf64 t64;
1565 t64.actime = t->actime;
1566 t64.modtime = t->modtime;
1567 return _futime64( fd, &t64 );
1570 return _futime64( fd, NULL );
1573 /*********************************************************************
1574 * _futime (MSVCRT.@)
1577 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1579 return _futime64( fd, t );
1582 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1584 return _futime32( fd, t );
1588 /*********************************************************************
1589 * _get_osfhandle (MSVCRT.@)
1591 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1593 HANDLE hand = msvcrt_fdtoh(fd);
1594 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1596 return (MSVCRT_intptr_t)hand;
1599 /*********************************************************************
1600 * _isatty (MSVCRT.@)
1602 int CDECL MSVCRT__isatty(int fd)
1604 HANDLE hand = msvcrt_fdtoh(fd);
1606 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1607 if (hand == INVALID_HANDLE_VALUE)
1610 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1613 /*********************************************************************
1614 * _mktemp (MSVCRT.@)
1616 char * CDECL MSVCRT__mktemp(char *pattern)
1619 char *retVal = pattern;
1624 numX = (*pattern++ == 'X')? numX + 1 : 0;
1628 id = GetCurrentProcessId();
1632 int tempNum = id / 10;
1633 *pattern-- = id - (tempNum * 10) + '0';
1639 *pattern = letter++;
1640 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1641 GetLastError() == ERROR_FILE_NOT_FOUND)
1643 } while(letter <= 'z');
1647 /*********************************************************************
1648 * _wmktemp (MSVCRT.@)
1650 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1653 MSVCRT_wchar_t *retVal = pattern;
1655 MSVCRT_wchar_t letter = 'a';
1658 numX = (*pattern++ == 'X')? numX + 1 : 0;
1662 id = GetCurrentProcessId();
1666 int tempNum = id / 10;
1667 *pattern-- = id - (tempNum * 10) + '0';
1673 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1674 GetLastError() == ERROR_FILE_NOT_FOUND)
1676 *pattern = letter++;
1677 } while(letter != '|');
1681 static unsigned split_oflags(unsigned oflags)
1684 unsigned unsupp; /* until we support everything */
1686 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1687 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1688 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1689 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
1690 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
1691 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
1692 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1693 else wxflags |= WX_TEXT; /* default to TEXT*/
1694 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1696 if ((unsupp = oflags & ~(
1697 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1698 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1699 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1700 MSVCRT__O_NOINHERIT|
1701 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
1702 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
1704 ERR(":unsupported oflags 0x%04x\n",unsupp);
1709 /*********************************************************************
1712 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1715 SECURITY_ATTRIBUTES sa;
1716 HANDLE readHandle, writeHandle;
1720 *MSVCRT__errno() = MSVCRT_EINVAL;
1724 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1725 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1726 sa.lpSecurityDescriptor = NULL;
1727 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1729 unsigned int wxflags = split_oflags(textmode);
1733 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
1737 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
1745 MSVCRT__close(pfds[0]);
1746 CloseHandle(writeHandle);
1747 *MSVCRT__errno() = MSVCRT_EMFILE;
1752 CloseHandle(readHandle);
1753 CloseHandle(writeHandle);
1754 *MSVCRT__errno() = MSVCRT_EMFILE;
1759 msvcrt_set_errno(GetLastError());
1764 static int check_bom(HANDLE h, int oflags, BOOL seek)
1766 char bom[sizeof(utf8_bom)];
1769 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
1771 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
1774 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
1775 oflags |= MSVCRT__O_U8TEXT;
1776 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
1778 SetFilePointer(h, 2, NULL, FILE_BEGIN);
1779 oflags |= MSVCRT__O_U16TEXT;
1781 SetFilePointer(h, 0, NULL, FILE_BEGIN);
1787 /*********************************************************************
1788 * _wsopen_s (MSVCRT.@)
1790 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1792 DWORD access = 0, creation = 0, attrib;
1793 SECURITY_ATTRIBUTES sa;
1798 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1799 fd, debugstr_w(path), oflags, shflags, pmode);
1801 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
1804 wxflag = split_oflags(oflags);
1805 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1807 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1808 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1809 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1812 if (oflags & MSVCRT__O_CREAT)
1814 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1815 FIXME(": pmode 0x%04x ignored\n", pmode);
1817 WARN(": pmode 0x%04x ignored\n", pmode);
1819 if (oflags & MSVCRT__O_EXCL)
1820 creation = CREATE_NEW;
1821 else if (oflags & MSVCRT__O_TRUNC)
1822 creation = CREATE_ALWAYS;
1824 creation = OPEN_ALWAYS;
1826 else /* no MSVCRT__O_CREAT */
1828 if (oflags & MSVCRT__O_TRUNC)
1829 creation = TRUNCATE_EXISTING;
1831 creation = OPEN_EXISTING;
1836 case MSVCRT__SH_DENYRW:
1839 case MSVCRT__SH_DENYWR:
1840 sharing = FILE_SHARE_READ;
1842 case MSVCRT__SH_DENYRD:
1843 sharing = FILE_SHARE_WRITE;
1845 case MSVCRT__SH_DENYNO:
1846 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1849 ERR( "Unhandled shflags 0x%x\n", shflags );
1850 return MSVCRT_EINVAL;
1852 attrib = FILE_ATTRIBUTE_NORMAL;
1854 if (oflags & MSVCRT__O_TEMPORARY)
1856 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1858 sharing |= FILE_SHARE_DELETE;
1861 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1862 sa.lpSecurityDescriptor = NULL;
1863 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
1865 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
1866 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
1867 && !(access&GENERIC_READ))
1869 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
1870 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
1871 if (hand != INVALID_HANDLE_VALUE)
1873 oflags = check_bom(hand, oflags, FALSE);
1877 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
1880 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1881 if (hand == INVALID_HANDLE_VALUE) {
1882 WARN(":failed-last error (%d)\n",GetLastError());
1883 msvcrt_set_errno(GetLastError());
1884 return *MSVCRT__errno();
1887 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
1889 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
1890 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
1891 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
1893 if (oflags & MSVCRT__O_U8TEXT)
1895 DWORD written = 0, tmp;
1897 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
1898 sizeof(utf8_bom)-written, &tmp, NULL))
1900 if (written != sizeof(utf8_bom)) {
1901 WARN("error writting BOM\n");
1903 msvcrt_set_errno(GetLastError());
1904 return *MSVCRT__errno();
1909 DWORD written = 0, tmp;
1911 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
1912 sizeof(utf16_bom)-written, &tmp, NULL))
1914 if (written != sizeof(utf16_bom))
1916 WARN("error writting BOM\n");
1918 msvcrt_set_errno(GetLastError());
1919 return *MSVCRT__errno();
1923 else if (access & GENERIC_READ)
1924 oflags = check_bom(hand, oflags, TRUE);
1927 *fd = msvcrt_alloc_fd(hand, wxflag);
1929 return *MSVCRT__errno();
1931 if (oflags & MSVCRT__O_WTEXT)
1932 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
1933 else if (oflags & MSVCRT__O_U16TEXT)
1934 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16;
1935 else if (oflags & MSVCRT__O_U8TEXT)
1936 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF8;
1938 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1942 /*********************************************************************
1943 * _wsopen (MSVCRT.@)
1945 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1950 if (oflags & MSVCRT__O_CREAT)
1954 __ms_va_start(ap, shflags);
1955 pmode = va_arg(ap, int);
1961 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1965 /*********************************************************************
1966 * _sopen_s (MSVCRT.@)
1968 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1970 MSVCRT_wchar_t *pathW;
1973 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
1974 return MSVCRT_EINVAL;
1976 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
1981 /*********************************************************************
1984 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1989 if (oflags & MSVCRT__O_CREAT)
1993 __ms_va_start(ap, shflags);
1994 pmode = va_arg(ap, int);
2000 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2004 /*********************************************************************
2007 int CDECL MSVCRT__open( const char *path, int flags, ... )
2011 if (flags & MSVCRT__O_CREAT)
2014 __ms_va_start(ap, flags);
2015 pmode = va_arg(ap, int);
2017 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2020 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2023 /*********************************************************************
2026 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2030 if (flags & MSVCRT__O_CREAT)
2033 __ms_va_start(ap, flags);
2034 pmode = va_arg(ap, int);
2036 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2039 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2042 /*********************************************************************
2045 int CDECL MSVCRT__creat(const char *path, int flags)
2047 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2048 return MSVCRT__open(path, usedFlags);
2051 /*********************************************************************
2052 * _wcreat (MSVCRT.@)
2054 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2056 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2057 return MSVCRT__wopen(path, usedFlags);
2060 /*********************************************************************
2061 * _open_osfhandle (MSVCRT.@)
2063 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2067 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2068 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2069 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2070 * text - it never sets MSVCRT__O_BINARY.
2072 /* don't let split_oflags() decide the mode if no mode is passed */
2073 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2074 oflags |= MSVCRT__O_BINARY;
2076 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
2077 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
2081 /*********************************************************************
2084 int CDECL MSVCRT__rmtmp(void)
2086 int num_removed = 0, i;
2090 for (i = 3; i < MSVCRT_stream_idx; i++) {
2091 file = msvcrt_get_file(i);
2093 if (file->_tmpfname)
2095 MSVCRT_fclose(file);
2102 TRACE(":removed (%d) temp files\n",num_removed);
2106 /*********************************************************************
2109 * When reading \r as last character in text mode, read() positions
2110 * the file pointer on the \r character while getc() goes on to
2113 static int read_i(int fd, void *buf, unsigned int count)
2115 DWORD num_read, utf16;
2116 char *bufstart = buf;
2117 HANDLE hand = msvcrt_fdtoh(fd);
2118 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2123 if (fdinfo->wxflag & WX_ATEOF) {
2124 TRACE("already at EOF, returning 0\n");
2127 /* Don't trace small reads, it gets *very* annoying */
2129 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2130 if (hand == INVALID_HANDLE_VALUE)
2132 *MSVCRT__errno() = MSVCRT_EBADF;
2136 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2137 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2139 *MSVCRT__errno() = MSVCRT_EINVAL;
2143 if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL))
2145 if (fdinfo->lookahead[0] != '\n')
2147 bufstart[0] = fdinfo->lookahead[0];
2148 fdinfo->lookahead[0] = '\n';
2152 bufstart[1] = fdinfo->lookahead[1];
2153 fdinfo->lookahead[1] = '\n';
2156 if(count>1+utf16 && ReadFile(hand, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2157 num_read += 1+utf16;
2162 if(utf16 && (num_read&1))
2164 /* msvcr90 uses uninitialized value from the buffer in this case */
2165 /* msvcrt ignores additional data */
2166 ERR("got odd number of bytes in UTF16 mode\n");
2170 if (count != 0 && num_read == 0)
2172 fdinfo->wxflag |= WX_ATEOF;
2173 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2175 else if (fdinfo->wxflag & WX_TEXT)
2179 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2180 fdinfo->wxflag |= WX_READNL;
2182 fdinfo->wxflag &= ~WX_READNL;
2184 for (i=0, j=0; i<num_read; i+=1+utf16)
2186 /* in text mode, a ctrl-z signals EOF */
2187 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2189 fdinfo->wxflag |= WX_ATEOF;
2190 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2194 /* in text mode, strip \r if followed by \n */
2195 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2200 lookahead[1] = '\n';
2201 if (ReadFile(hand, lookahead, 1+utf16, &len, NULL) && len)
2203 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2205 bufstart[j++] = '\n';
2206 if(utf16) bufstart[j++] = 0;
2210 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2212 bufstart[j++] = '\r';
2213 if(utf16) bufstart[j++] = 0;
2216 if (fdinfo->wxflag & WX_PIPE)
2218 fdinfo->lookahead[0] = lookahead[0];
2219 fdinfo->lookahead[1] = lookahead[1];
2222 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2227 bufstart[j++] = '\r';
2228 if(utf16) bufstart[j++] = 0;
2231 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2232 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2234 bufstart[j++] = bufstart[i];
2235 if(utf16) bufstart[j++] = bufstart[i+1];
2243 if (GetLastError() == ERROR_BROKEN_PIPE)
2245 TRACE(":end-of-pipe\n");
2246 fdinfo->wxflag |= WX_ATEOF;
2251 TRACE(":failed-last error (%d)\n",GetLastError());
2257 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2261 /*********************************************************************
2264 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2267 num_read = read_i(fd, buf, count);
2271 /*********************************************************************
2272 * _setmode (MSVCRT.@)
2274 int CDECL MSVCRT__setmode(int fd,int mode)
2276 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2277 if(ret==MSVCRT__O_TEXT && (msvcrt_get_ioinfo(fd)->exflag & (EF_UTF8|EF_UTF16)))
2278 ret = MSVCRT__O_WTEXT;
2280 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2281 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2282 *MSVCRT__errno() = MSVCRT_EINVAL;
2286 if(mode == MSVCRT__O_BINARY) {
2287 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2288 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2292 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2293 if(mode == MSVCRT__O_TEXT)
2294 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2295 else if(mode == MSVCRT__O_U8TEXT)
2296 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF16) | EF_UTF8;
2298 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF8) | EF_UTF16;
2303 /*********************************************************************
2304 * _stat64 (MSVCRT.@)
2306 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2309 WIN32_FILE_ATTRIBUTE_DATA hfi;
2310 unsigned short mode = ALL_S_IREAD;
2313 TRACE(":file (%s) buf(%p)\n",path,buf);
2315 plen = strlen(path);
2316 while (plen && path[plen-1]==' ')
2319 if (plen && (path[plen-1]=='\\' || path[plen-1]=='/'))
2321 *MSVCRT__errno() = MSVCRT_ENOENT;
2325 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2327 TRACE("failed (%d)\n",GetLastError());
2328 *MSVCRT__errno() = MSVCRT_ENOENT;
2332 memset(buf,0,sizeof(struct MSVCRT__stat64));
2334 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2335 Bon 011120: This FIXME seems incorrect
2336 Also a letter as first char isn't enough to be classified
2339 if (isalpha(*path)&& (*(path+1)==':'))
2340 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2342 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2344 /* Dir, or regular file? */
2345 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2346 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2349 mode |= MSVCRT__S_IFREG;
2351 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2353 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2354 (tolower(path[plen-3]) << 16);
2355 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2356 mode |= ALL_S_IEXEC;
2360 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2361 mode |= ALL_S_IWRITE;
2363 buf->st_mode = mode;
2365 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2366 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2368 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2369 buf->st_mtime = buf->st_ctime = dw;
2370 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2371 (int)(buf->st_size >> 32),(int)buf->st_size,
2372 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2376 /*********************************************************************
2377 * _stati64 (MSVCRT.@)
2379 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2382 struct MSVCRT__stat64 buf64;
2384 ret = MSVCRT_stat64(path, &buf64);
2386 msvcrt_stat64_to_stati64(&buf64, buf);
2390 /*********************************************************************
2393 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2395 struct MSVCRT__stat64 buf64;
2397 ret = MSVCRT_stat64( path, &buf64);
2399 msvcrt_stat64_to_stat(&buf64, buf);
2403 /*********************************************************************
2404 * _wstat64 (MSVCRT.@)
2406 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2409 WIN32_FILE_ATTRIBUTE_DATA hfi;
2410 unsigned short mode = ALL_S_IREAD;
2413 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2415 plen = strlenW(path);
2416 while (plen && path[plen-1]==' ')
2419 if(plen && (path[plen-1]=='\\' || path[plen-1]=='/'))
2421 *MSVCRT__errno() = MSVCRT_ENOENT;
2425 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2427 TRACE("failed (%d)\n",GetLastError());
2428 *MSVCRT__errno() = MSVCRT_ENOENT;
2432 memset(buf,0,sizeof(struct MSVCRT__stat64));
2434 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2435 if (MSVCRT_iswalpha(*path))
2436 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2438 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2440 /* Dir, or regular file? */
2441 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2442 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2445 mode |= MSVCRT__S_IFREG;
2447 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2449 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2450 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2451 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2452 mode |= ALL_S_IEXEC;
2456 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2457 mode |= ALL_S_IWRITE;
2459 buf->st_mode = mode;
2461 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2462 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2464 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2465 buf->st_mtime = buf->st_ctime = dw;
2466 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2467 (int)(buf->st_size >> 32),(int)buf->st_size,
2468 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2472 /*********************************************************************
2473 * _wstati64 (MSVCRT.@)
2475 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2478 struct MSVCRT__stat64 buf64;
2480 ret = MSVCRT__wstat64(path, &buf64);
2482 msvcrt_stat64_to_stati64(&buf64, buf);
2486 /*********************************************************************
2489 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2492 struct MSVCRT__stat64 buf64;
2494 ret = MSVCRT__wstat64( path, &buf64 );
2495 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2499 /*********************************************************************
2502 MSVCRT_long CDECL MSVCRT__tell(int fd)
2504 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2507 /*********************************************************************
2508 * _telli64 (MSVCRT.@)
2510 __int64 CDECL _telli64(int fd)
2512 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2515 /*********************************************************************
2516 * _tempnam (MSVCRT.@)
2518 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
2520 char tmpbuf[MAX_PATH];
2521 const char *tmp_dir = MSVCRT_getenv("TMP");
2523 if (tmp_dir) dir = tmp_dir;
2525 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2526 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2528 TRACE("got name (%s)\n",tmpbuf);
2529 DeleteFileA(tmpbuf);
2530 return MSVCRT__strdup(tmpbuf);
2532 TRACE("failed (%d)\n",GetLastError());
2536 /*********************************************************************
2537 * _wtempnam (MSVCRT.@)
2539 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2541 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
2542 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2543 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
2545 if (tmp_dir) dir = tmp_dir;
2547 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2548 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2550 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2551 DeleteFileW(tmpbuf);
2552 return MSVCRT__wcsdup(tmpbuf);
2554 TRACE("failed (%d)\n",GetLastError());
2558 /*********************************************************************
2561 int CDECL MSVCRT__umask(int umask)
2563 int old_umask = MSVCRT_umask;
2564 TRACE("(%d)\n",umask);
2565 MSVCRT_umask = umask;
2569 /*********************************************************************
2570 * _utime64 (MSVCRT.@)
2572 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2574 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2578 int retVal = _futime64(fd, t);
2585 /*********************************************************************
2586 * _utime32 (MSVCRT.@)
2588 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2592 struct MSVCRT___utimbuf64 t64;
2593 t64.actime = t->actime;
2594 t64.modtime = t->modtime;
2595 return _utime64( path, &t64 );
2598 return _utime64( path, NULL );
2601 /*********************************************************************
2605 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2607 return _utime64( path, t );
2610 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2612 return _utime32( path, t );
2616 /*********************************************************************
2617 * _wutime64 (MSVCRT.@)
2619 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2621 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2625 int retVal = _futime64(fd, t);
2632 /*********************************************************************
2633 * _wutime32 (MSVCRT.@)
2635 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2639 struct MSVCRT___utimbuf64 t64;
2640 t64.actime = t->actime;
2641 t64.modtime = t->modtime;
2642 return _wutime64( path, &t64 );
2645 return _wutime64( path, NULL );
2648 /*********************************************************************
2649 * _wutime (MSVCRT.@)
2652 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2654 return _wutime64( path, t );
2657 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2659 return _wutime32( path, t );
2663 /*********************************************************************
2666 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2669 ioinfo *info = msvcrt_get_ioinfo(fd);
2670 HANDLE hand = info->handle;
2672 /* Don't trace small writes, it gets *very* annoying */
2675 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2677 if (hand == INVALID_HANDLE_VALUE)
2679 *MSVCRT__errno() = MSVCRT_EBADF;
2683 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
2685 *MSVCRT__errno() = MSVCRT_EINVAL;
2689 /* If appending, go to EOF */
2690 if (info->wxflag & WX_APPEND)
2691 MSVCRT__lseek(fd, 0, FILE_END);
2693 if (!(info->wxflag & WX_TEXT))
2695 if (WriteFile(hand, buf, count, &num_written, NULL)
2696 && (num_written == count))
2698 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2699 hand, GetLastError());
2700 *MSVCRT__errno() = MSVCRT_ENOSPC;
2704 unsigned int i, j, nr_lf, size;
2707 const char *s = buf, *buf_start = buf;
2709 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
2711 /* find number of \n */
2712 for (nr_lf=0, i=0; i<count; i++)
2718 if ((q = p = MSVCRT_malloc(size)))
2720 for (s = buf, i = 0, j = 0; i < count; i++)
2729 FIXME("Malloc failed\n");
2741 else if (info->exflag & EF_UTF16)
2743 for (nr_lf=0, i=0; i<count; i+=2)
2744 if (s[i]=='\n' && s[i+1]==0)
2749 if ((q = p = MSVCRT_malloc(size)))
2751 for (s=buf, i=0, j=0; i<count; i++)
2753 if (s[i]=='\n' && s[i+1]==0)
2764 FIXME("Malloc failed\n");
2780 for(nr_lf=0, i=0; i<count; i+=2)
2781 if (s[i]=='\n' && s[i+1]==0)
2784 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
2786 msvcrt_set_errno(GetLastError());
2791 size = conv_len+nr_lf;
2792 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
2794 for (s=buf, i=0, j=0; i<count; i++)
2796 if (s[i]=='\n' && s[i+1]==0)
2804 q = p+count+nr_lf*2;
2805 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
2806 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
2810 FIXME("Malloc failed\n");
2817 if (!WriteFile(hand, q, size, &num_written, NULL))
2821 if (num_written != size)
2823 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2824 fd, hand, GetLastError(), num_written);
2825 *MSVCRT__errno() = MSVCRT_ENOSPC;
2826 return s - buf_start;
2834 /*********************************************************************
2837 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2841 MSVCRT__lock_file(file);
2842 len = MSVCRT__write(file->_file, &val, sizeof(val));
2843 if (len == sizeof(val)) {
2844 MSVCRT__unlock_file(file);
2848 file->_flag |= MSVCRT__IOERR;
2849 MSVCRT__unlock_file(file);
2853 /*********************************************************************
2856 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2860 MSVCRT__lock_file(file);
2862 MSVCRT_free(file->_tmpfname);
2863 file->_tmpfname = NULL;
2864 /* flush stdio buffers */
2865 if(file->_flag & MSVCRT__IOWRT)
2866 MSVCRT_fflush(file);
2867 if(file->_flag & MSVCRT__IOMYBUF)
2868 MSVCRT_free(file->_base);
2870 r=MSVCRT__close(file->_file);
2873 MSVCRT__unlock_file(file);
2875 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2878 /*********************************************************************
2881 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2883 return file->_flag & MSVCRT__IOEOF;
2886 /*********************************************************************
2889 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2891 return file->_flag & MSVCRT__IOERR;
2894 /*********************************************************************
2895 * _filbuf (MSVCRT.@)
2897 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2900 MSVCRT__lock_file(file);
2902 /* Allocate buffer if needed */
2903 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
2904 msvcrt_alloc_buffer(file);
2906 if(!(file->_flag & MSVCRT__IOREAD)) {
2907 if(file->_flag & MSVCRT__IORW)
2908 file->_flag |= MSVCRT__IOREAD;
2910 MSVCRT__unlock_file(file);
2915 if(file->_flag & MSVCRT__IONBF) {
2917 if ((r = read_i(file->_file,&c,1)) != 1) {
2918 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2919 MSVCRT__unlock_file(file);
2923 MSVCRT__unlock_file(file);
2926 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2928 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2930 MSVCRT__unlock_file(file);
2935 file->_ptr = file->_base+1;
2936 c = *(unsigned char *)file->_base;
2937 MSVCRT__unlock_file(file);
2942 /*********************************************************************
2945 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2950 MSVCRT__lock_file(file);
2953 i = (unsigned char *)file->_ptr++;
2956 j = MSVCRT__filbuf(file);
2958 MSVCRT__unlock_file(file);
2962 /*********************************************************************
2963 * _fgetchar (MSVCRT.@)
2965 int CDECL MSVCRT__fgetchar(void)
2967 return MSVCRT_fgetc(MSVCRT_stdin);
2970 /*********************************************************************
2973 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2975 int cc = MSVCRT_EOF;
2976 char * buf_start = s;
2978 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2979 file,file->_file,s,size);
2981 MSVCRT__lock_file(file);
2983 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2988 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2990 TRACE(":nothing read\n");
2991 MSVCRT__unlock_file(file);
2994 if ((cc != MSVCRT_EOF) && (size > 1))
2997 TRACE(":got %s\n", debugstr_a(buf_start));
2998 MSVCRT__unlock_file(file);
3002 /*********************************************************************
3005 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
3006 * the CR from CR/LF combinations
3008 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3012 MSVCRT__lock_file(file);
3013 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
3020 for(i=0; i<sizeof(wc); i++)
3030 j = MSVCRT__filbuf(file);
3033 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3036 MSVCRT__unlock_file(file);
3043 MSVCRT__unlock_file(file);
3047 c = MSVCRT_fgetc(file);
3048 if ((get_locinfo()->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
3050 FIXME("Treat Multibyte characters\n");
3053 MSVCRT__unlock_file(file);
3054 if (c == MSVCRT_EOF)
3057 return (MSVCRT_wint_t)c;
3060 /*********************************************************************
3063 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3070 MSVCRT__lock_file(file);
3071 for (j=0; j<sizeof(int); j++) {
3072 k = MSVCRT_fgetc(file);
3073 if (k == MSVCRT_EOF) {
3074 file->_flag |= MSVCRT__IOEOF;
3075 MSVCRT__unlock_file(file);
3081 MSVCRT__unlock_file(file);
3085 /*********************************************************************
3088 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3090 return MSVCRT_fgetwc(file);
3093 /*********************************************************************
3094 * _fgetwchar (MSVCRT.@)
3096 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3098 return MSVCRT_fgetwc(MSVCRT_stdin);
3101 /*********************************************************************
3102 * getwchar (MSVCRT.@)
3104 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3106 return MSVCRT__fgetwchar();
3109 /*********************************************************************
3112 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3114 int cc = MSVCRT_WEOF;
3115 MSVCRT_wchar_t * buf_start = s;
3117 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3118 file,file->_file,s,size);
3120 MSVCRT__lock_file(file);
3122 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
3127 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3129 TRACE(":nothing read\n");
3130 MSVCRT__unlock_file(file);
3133 if ((cc != MSVCRT_WEOF) && (size > 1))
3136 TRACE(":got %s\n", debugstr_w(buf_start));
3137 MSVCRT__unlock_file(file);
3141 /*********************************************************************
3144 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3146 MSVCRT_size_t wrcnt=size * nmemb;
3151 MSVCRT__lock_file(file);
3153 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3154 memcpy(file->_ptr, ptr, pcnt);
3159 ptr = (const char*)ptr + pcnt;
3160 } else if(!(file->_flag & MSVCRT__IOWRT)) {
3161 if(file->_flag & MSVCRT__IORW) {
3162 file->_flag |= MSVCRT__IOWRT;
3164 MSVCRT__unlock_file(file);
3171 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
3172 msvcrt_alloc_buffer(file);
3175 res=msvcrt_flush_buffer(file);
3177 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
3180 file->_flag |= MSVCRT__IOERR;
3183 written += pwritten;
3187 MSVCRT__unlock_file(file);
3188 return written / size;
3191 /*********************************************************************
3194 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3196 MSVCRT_wchar_t mwc=wc;
3197 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
3202 /*********************************************************************
3203 * _fputwchar (MSVCRT.@)
3205 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3207 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3210 /*********************************************************************
3211 * _wfsopen (MSVCRT.@)
3213 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
3216 int open_flags, stream_flags, fd;
3218 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3220 /* map mode string to open() flags. "man fopen" for possibilities. */
3221 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3225 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3228 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3230 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3237 TRACE(":got (%p)\n",file);
3238 if (fd >= 0 && !file)
3244 /*********************************************************************
3245 * _fsopen (MSVCRT.@)
3247 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3250 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3252 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3253 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3254 *MSVCRT__errno() = MSVCRT_EINVAL;
3257 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3260 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3261 *MSVCRT__errno() = MSVCRT_EINVAL;
3265 ret = MSVCRT__wfsopen(pathW, modeW, share);
3272 /*********************************************************************
3275 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3277 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3280 /*********************************************************************
3281 * fopen_s (MSVCRT.@)
3283 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3284 const char *filename, const char *mode)
3286 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3287 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3288 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3290 *pFile = MSVCRT_fopen(filename, mode);
3293 return *MSVCRT__errno();
3297 /*********************************************************************
3298 * _wfopen (MSVCRT.@)
3300 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3302 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3305 /*********************************************************************
3306 * _wfopen_s (MSVCRT.@)
3308 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3309 const MSVCRT_wchar_t *mode)
3311 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3312 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3313 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3315 *pFile = MSVCRT__wfopen(filename, mode);
3318 return *MSVCRT__errno();
3322 /*********************************************************************
3323 * _flsbuf (MSVCRT.@)
3325 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3327 /* Flush output buffer */
3328 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3329 msvcrt_alloc_buffer(file);
3331 if(!(file->_flag & MSVCRT__IOWRT)) {
3332 if(file->_flag & MSVCRT__IORW)
3333 file->_flag |= MSVCRT__IOWRT;
3341 res = msvcrt_flush_buffer(file);
3345 res = msvcrt_flush_buffer(file);
3348 return res ? res : c&0xff;
3352 /* set _cnt to 0 for unbuffered FILEs */
3354 len = MSVCRT__write(file->_file, &cc, 1);
3357 file->_flag |= MSVCRT__IOERR;
3362 /*********************************************************************
3365 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3369 MSVCRT__lock_file(file);
3375 res = msvcrt_flush_buffer(file);
3376 MSVCRT__unlock_file(file);
3377 return res ? res : c;
3380 MSVCRT__unlock_file(file);
3384 res = MSVCRT__flsbuf(c, file);
3385 MSVCRT__unlock_file(file);
3390 /*********************************************************************
3391 * _fputchar (MSVCRT.@)
3393 int CDECL MSVCRT__fputchar(int c)
3395 return MSVCRT_fputc(c, MSVCRT_stdout);
3398 /*********************************************************************
3401 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3403 MSVCRT_size_t rcnt=size * nmemb;
3404 MSVCRT_size_t read=0;
3410 MSVCRT__lock_file(file);
3412 /* first buffered data */
3414 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3415 memcpy(ptr, file->_ptr, pcnt);
3420 ptr = (char*)ptr + pcnt;
3421 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3422 if(file->_flag & MSVCRT__IORW) {
3423 file->_flag |= MSVCRT__IOREAD;
3425 MSVCRT__unlock_file(file);
3432 /* Fill the buffer on small reads.
3433 * TODO: Use a better buffering strategy.
3435 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
3436 if (file->_bufsiz == 0) {
3437 msvcrt_alloc_buffer(file);
3439 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3440 file->_ptr = file->_base;
3441 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3442 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3443 if (i > 0 && i < file->_cnt) {
3444 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3445 file->_flag &= ~MSVCRT__IOEOF;
3448 memcpy(ptr, file->_ptr, i);
3453 i = MSVCRT__read(file->_file,ptr, rcnt);
3457 ptr = (char *)ptr+i;
3458 /* expose feof condition in the flags
3459 * MFC tests file->_flag for feof, and doesn't call feof())
3461 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3462 file->_flag |= MSVCRT__IOEOF;
3465 file->_flag |= MSVCRT__IOERR;
3472 MSVCRT__unlock_file(file);
3476 /*********************************************************************
3477 * _wfreopen (MSVCRT.@)
3480 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3484 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3487 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3491 MSVCRT_fclose(file);
3492 file = MSVCRT__wfsopen(path, mode, MSVCRT__SH_DENYNO);
3498 /*********************************************************************
3499 * _wfreopen_s (MSVCRT.@)
3501 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
3502 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3504 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3505 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
3506 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3507 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3509 *pFile = MSVCRT__wfreopen(path, mode, file);
3512 return *MSVCRT__errno();
3516 /*********************************************************************
3517 * freopen (MSVCRT.@)
3520 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3523 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3525 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3526 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3532 ret = MSVCRT__wfreopen(pathW, modeW, file);
3539 /*********************************************************************
3540 * freopen_s (MSVCRT.@)
3542 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
3543 const char *path, const char *mode, MSVCRT_FILE* file)
3545 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3546 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
3547 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3548 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3550 *pFile = MSVCRT_freopen(path, mode, file);
3553 return *MSVCRT__errno();
3557 /*********************************************************************
3558 * fsetpos (MSVCRT.@)
3560 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3564 MSVCRT__lock_file(file);
3565 /* Note that all this has been lifted 'as is' from fseek */
3566 if(file->_flag & MSVCRT__IOWRT)
3567 msvcrt_flush_buffer(file);
3569 /* Discard buffered input */
3571 file->_ptr = file->_base;
3573 /* Reset direction of i/o */
3574 if(file->_flag & MSVCRT__IORW) {
3575 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3578 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3579 MSVCRT__unlock_file(file);
3583 /*********************************************************************
3584 * _ftelli64 (MSVCRT.@)
3586 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3590 MSVCRT__lock_file(file);
3591 pos = _telli64(file->_file);
3593 MSVCRT__unlock_file(file);
3597 if(file->_flag & MSVCRT__IOWRT) {
3598 pos += file->_ptr - file->_base;
3599 } else if(!file->_cnt) { /* nothing to do */
3600 } else if(MSVCRT__lseeki64(file->_file, 0, SEEK_END)==pos) {
3604 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3605 for(i=0; i<file->_cnt; i++)
3606 if(file->_ptr[i] == '\n')
3612 if(MSVCRT__lseeki64(file->_file, pos, SEEK_SET) != pos) {
3613 MSVCRT__unlock_file(file);
3617 pos -= file->_bufsiz;
3618 pos += file->_ptr - file->_base;
3620 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3621 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READNL)
3624 for(p=file->_base; p<file->_ptr; p++)
3631 MSVCRT__unlock_file(file);
3635 /*********************************************************************
3638 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3640 return MSVCRT__ftelli64(file);
3643 /*********************************************************************
3644 * fgetpos (MSVCRT.@)
3646 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3648 *pos = MSVCRT__ftelli64(file);
3654 /*********************************************************************
3657 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3659 MSVCRT_size_t len = strlen(s);
3662 MSVCRT__lock_file(file);
3663 ret = MSVCRT_fwrite(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
3664 MSVCRT__unlock_file(file);
3668 /*********************************************************************
3671 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3673 MSVCRT_size_t i, len = strlenW(s);
3676 MSVCRT__lock_file(file);
3677 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3678 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3679 MSVCRT__unlock_file(file);
3682 for (i=0; i<len; i++) {
3683 if (((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3684 || MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
3685 MSVCRT__unlock_file(file);
3690 MSVCRT__unlock_file(file);
3694 /*********************************************************************
3695 * getchar (MSVCRT.@)
3697 int CDECL MSVCRT_getchar(void)
3699 return MSVCRT_fgetc(MSVCRT_stdin);
3702 /*********************************************************************
3705 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3707 return MSVCRT_fgetc(file);
3710 /*********************************************************************
3713 char * CDECL MSVCRT_gets(char *buf)
3716 char * buf_start = buf;
3718 MSVCRT__lock_file(MSVCRT_stdin);
3719 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3720 cc = MSVCRT_fgetc(MSVCRT_stdin))
3721 if(cc != '\r') *buf++ = (char)cc;
3725 TRACE("got '%s'\n", buf_start);
3726 MSVCRT__unlock_file(MSVCRT_stdin);
3730 /*********************************************************************
3733 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3736 MSVCRT_wchar_t* ws = buf;
3738 MSVCRT__lock_file(MSVCRT_stdin);
3739 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3740 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3743 *buf++ = (MSVCRT_wchar_t)cc;
3747 TRACE("got %s\n", debugstr_w(ws));
3748 MSVCRT__unlock_file(MSVCRT_stdin);
3752 /*********************************************************************
3755 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3757 return MSVCRT_fputc(c, file);
3760 /*********************************************************************
3761 * putchar (MSVCRT.@)
3763 int CDECL MSVCRT_putchar(int c)
3765 return MSVCRT_fputc(c, MSVCRT_stdout);
3768 /*********************************************************************
3769 * _putwch (MSVCRT.@)
3771 int CDECL MSVCRT__putwch(int c)
3773 return MSVCRT_fputwc(c, MSVCRT_stdout);
3776 /*********************************************************************
3779 int CDECL MSVCRT_puts(const char *s)
3781 MSVCRT_size_t len = strlen(s);
3784 MSVCRT__lock_file(MSVCRT_stdout);
3785 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3786 MSVCRT__unlock_file(MSVCRT_stdout);
3790 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3791 MSVCRT__unlock_file(MSVCRT_stdout);
3795 /*********************************************************************
3798 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
3800 static const MSVCRT_wchar_t nl = '\n';
3801 MSVCRT_size_t len = strlenW(s);
3804 MSVCRT__lock_file(MSVCRT_stdout);
3805 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3806 MSVCRT__unlock_file(MSVCRT_stdout);
3810 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3811 MSVCRT__unlock_file(MSVCRT_stdout);
3815 /*********************************************************************
3818 int CDECL MSVCRT_remove(const char *path)
3820 TRACE("(%s)\n",path);
3821 if (DeleteFileA(path))
3823 TRACE(":failed (%d)\n",GetLastError());
3824 msvcrt_set_errno(GetLastError());
3828 /*********************************************************************
3829 * _wremove (MSVCRT.@)
3831 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
3833 TRACE("(%s)\n",debugstr_w(path));
3834 if (DeleteFileW(path))
3836 TRACE(":failed (%d)\n",GetLastError());
3837 msvcrt_set_errno(GetLastError());
3841 /*********************************************************************
3844 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3846 TRACE(":from %s to %s\n",oldpath,newpath);
3847 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3849 TRACE(":failed (%d)\n",GetLastError());
3850 msvcrt_set_errno(GetLastError());
3854 /*********************************************************************
3855 * _wrename (MSVCRT.@)
3857 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3859 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3860 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3862 TRACE(":failed (%d)\n",GetLastError());
3863 msvcrt_set_errno(GetLastError());
3867 /*********************************************************************
3868 * setvbuf (MSVCRT.@)
3870 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3872 MSVCRT__lock_file(file);
3874 MSVCRT_free(file->_base);
3878 if(mode == MSVCRT__IOFBF) {
3879 file->_flag &= ~MSVCRT__IONBF;
3880 file->_base = file->_ptr = buf;
3882 file->_bufsiz = size;
3885 file->_flag |= MSVCRT__IONBF;
3887 MSVCRT__unlock_file(file);
3891 /*********************************************************************
3894 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3896 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3899 /*********************************************************************
3902 char * CDECL MSVCRT_tmpnam(char *s)
3909 thread_data_t *data = msvcrt_get_thread_data();
3911 if(!data->tmpnam_buffer)
3912 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
3914 s = data->tmpnam_buffer;
3917 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3918 p = s + sprintf(s, "\\s%s.", tmpstr);
3919 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3921 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
3922 memcpy(p, tmpstr, size);
3924 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3925 GetLastError() == ERROR_FILE_NOT_FOUND)
3931 /*********************************************************************
3932 * _wtmpnam (MSVCRT.@)
3934 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
3936 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
3937 MSVCRT_wchar_t tmpstr[16];
3941 thread_data_t *data = msvcrt_get_thread_data();
3943 if(!data->wtmpnam_buffer)
3944 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
3946 s = data->wtmpnam_buffer;
3949 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
3950 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
3951 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3953 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
3954 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
3956 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
3957 GetLastError() == ERROR_FILE_NOT_FOUND)
3963 /*********************************************************************
3964 * tmpfile (MSVCRT.@)
3966 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3968 char *filename = MSVCRT_tmpnam(NULL);
3970 MSVCRT_FILE* file = NULL;
3973 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
3974 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3975 if (fd != -1 && (file = msvcrt_alloc_fp()))
3977 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
3982 else file->_tmpfname = MSVCRT__strdup(filename);
3985 if(fd != -1 && !file)
3991 /*********************************************************************
3992 * tmpfile_s (MSVCRT.@)
3994 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
3996 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3998 *file = MSVCRT_tmpfile();
4002 static int puts_clbk_file_a(void *file, int len, const char *str)
4004 return MSVCRT_fwrite(str, sizeof(char), len, file);
4007 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4009 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
4012 /*********************************************************************
4013 * vfprintf (MSVCRT.@)
4015 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4019 MSVCRT__lock_file(file);
4020 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4021 MSVCRT__unlock_file(file);
4026 /*********************************************************************
4027 * vfprintf_s (MSVCRT.@)
4029 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4033 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4035 MSVCRT__lock_file(file);
4036 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4037 MSVCRT__unlock_file(file);
4042 /*********************************************************************
4043 * vfwprintf (MSVCRT.@)
4045 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4049 MSVCRT__lock_file(file);
4050 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4051 MSVCRT__unlock_file(file);
4056 /*********************************************************************
4057 * vfwprintf_s (MSVCRT.@)
4059 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4063 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4065 MSVCRT__lock_file(file);
4066 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4067 MSVCRT__unlock_file(file);
4072 /*********************************************************************
4073 * _vfwprintf_l (MSVCRT.@)
4075 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
4076 MSVCRT__locale_t locale, __ms_va_list valist)
4080 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4082 MSVCRT__lock_file(file);
4083 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4084 MSVCRT__unlock_file(file);
4089 /*********************************************************************
4090 * vprintf (MSVCRT.@)
4092 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
4094 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
4097 /*********************************************************************
4098 * vprintf_s (MSVCRT.@)
4100 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
4102 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
4105 /*********************************************************************
4106 * vwprintf (MSVCRT.@)
4108 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
4110 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
4113 /*********************************************************************
4114 * vwprintf_s (MSVCRT.@)
4116 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
4118 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
4121 /*********************************************************************
4122 * fprintf (MSVCRT.@)
4124 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
4126 __ms_va_list valist;
4128 __ms_va_start(valist, format);
4129 res = MSVCRT_vfprintf(file, format, valist);
4130 __ms_va_end(valist);
4134 /*********************************************************************
4135 * fprintf_s (MSVCRT.@)
4137 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
4139 __ms_va_list valist;
4141 __ms_va_start(valist, format);
4142 res = MSVCRT_vfprintf_s(file, format, valist);
4143 __ms_va_end(valist);
4147 /*********************************************************************
4148 * fwprintf (MSVCRT.@)
4150 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4152 __ms_va_list valist;
4154 __ms_va_start(valist, format);
4155 res = MSVCRT_vfwprintf(file, format, valist);
4156 __ms_va_end(valist);
4160 /*********************************************************************
4161 * fwprintf_s (MSVCRT.@)
4163 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4165 __ms_va_list valist;
4167 __ms_va_start(valist, format);
4168 res = MSVCRT_vfwprintf_s(file, format, valist);
4169 __ms_va_end(valist);
4173 /*********************************************************************
4174 * _fwprintf_l (MSVCRT.@)
4176 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
4178 __ms_va_list valist;
4180 __ms_va_start(valist, locale);
4181 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
4182 __ms_va_end(valist);
4186 /*********************************************************************
4189 int CDECL MSVCRT_printf(const char *format, ...)
4191 __ms_va_list valist;
4193 __ms_va_start(valist, format);
4194 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
4195 __ms_va_end(valist);
4199 /*********************************************************************
4200 * printf_s (MSVCRT.@)
4202 int CDECL MSVCRT_printf_s(const char *format, ...)
4204 __ms_va_list valist;
4206 __ms_va_start(valist, format);
4207 res = MSVCRT_vprintf_s(format, valist);
4208 __ms_va_end(valist);
4212 /*********************************************************************
4215 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
4217 if (c == MSVCRT_EOF)
4220 MSVCRT__lock_file(file);
4221 if(file->_bufsiz == 0) {
4222 msvcrt_alloc_buffer(file);
4225 if(file->_ptr>file->_base) {
4229 MSVCRT_clearerr(file);
4230 MSVCRT__unlock_file(file);
4234 MSVCRT__unlock_file(file);
4238 /*********************************************************************
4239 * ungetwc (MSVCRT.@)
4241 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
4243 MSVCRT_wchar_t mwc = wc;
4244 unsigned char * pp = (unsigned char *)&mwc;
4247 if (wc == MSVCRT_WEOF)
4250 MSVCRT__lock_file(file);
4251 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
4252 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
4253 MSVCRT__unlock_file(file);
4258 MSVCRT__unlock_file(file);
4262 /*********************************************************************
4263 * wprintf (MSVCRT.@)
4265 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
4267 __ms_va_list valist;
4269 __ms_va_start(valist, format);
4270 res = MSVCRT_vwprintf(format, valist);
4271 __ms_va_end(valist);
4275 /*********************************************************************
4276 * wprintf_s (MSVCRT.@)
4278 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
4280 __ms_va_list valist;
4282 __ms_va_start(valist, format);
4283 res = MSVCRT_vwprintf_s(format, valist);
4284 __ms_va_end(valist);
4288 /*********************************************************************
4289 * _getmaxstdio (MSVCRT.@)
4291 int CDECL MSVCRT__getmaxstdio(void)
4293 return MSVCRT_max_streams;
4296 /*********************************************************************
4297 * _setmaxstdio (MSVCRT.@)
4299 int CDECL MSVCRT__setmaxstdio(int newmax)
4301 TRACE("%d\n", newmax);
4303 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4306 MSVCRT_max_streams = newmax;
4307 return MSVCRT_max_streams;