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_READCR 0x04 /* underlying file is at \r */
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 ) {
1172 offset -= file->_cnt;
1173 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
1174 /* Black magic correction for CR removal */
1176 for (i=0; i<file->_cnt; i++) {
1177 if (file->_ptr[i] == '\n')
1180 /* Black magic when reading CR at buffer boundary*/
1181 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
1185 /* Discard buffered input */
1187 file->_ptr = file->_base;
1188 /* Reset direction of i/o */
1189 if(file->_flag & MSVCRT__IORW) {
1190 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1192 /* Clear end of file flag */
1193 file->_flag &= ~MSVCRT__IOEOF;
1194 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1196 MSVCRT__unlock_file(file);
1200 /*********************************************************************
1203 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1205 return MSVCRT__fseeki64( file, offset, whence );
1208 /*********************************************************************
1209 * _chsize (MSVCRT.@)
1211 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1217 TRACE("(fd=%d, size=%d)\n", fd, size);
1221 handle = msvcrt_fdtoh(fd);
1222 if (handle != INVALID_HANDLE_VALUE)
1224 /* save the current file pointer */
1225 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1228 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1231 ret = SetEndOfFile(handle);
1232 if (!ret) msvcrt_set_errno(GetLastError());
1235 /* restore the file pointer */
1236 MSVCRT__lseek(fd, cur, SEEK_SET);
1241 return ret ? 0 : -1;
1244 /*********************************************************************
1245 * clearerr (MSVCRT.@)
1247 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1249 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1251 MSVCRT__lock_file(file);
1252 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1253 MSVCRT__unlock_file(file);
1256 /*********************************************************************
1259 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1261 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1263 MSVCRT__lock_file(file);
1264 MSVCRT_fseek(file, 0L, SEEK_SET);
1265 MSVCRT_clearerr(file);
1266 MSVCRT__unlock_file(file);
1269 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1271 int plus = strchrW(mode, '+') != NULL;
1273 TRACE("%s\n", debugstr_w(mode));
1278 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1279 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1282 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1283 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1286 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1287 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1290 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1294 while (*mode && *mode!=',')
1298 *open_flags |= MSVCRT__O_BINARY;
1299 *open_flags &= ~MSVCRT__O_TEXT;
1302 *open_flags |= MSVCRT__O_TEXT;
1303 *open_flags &= ~MSVCRT__O_BINARY;
1306 *open_flags |= MSVCRT__O_TEMPORARY;
1309 *open_flags |= MSVCRT__O_SHORT_LIVED;
1315 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1321 static const WCHAR ccs[] = {'c','c','s'};
1322 static const WCHAR utf8[] = {'u','t','f','-','8'};
1323 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1324 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1327 while(*mode == ' ') mode++;
1328 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1330 mode += sizeof(ccs)/sizeof(ccs[0]);
1331 while(*mode == ' ') mode++;
1332 if(!MSVCRT_CHECK_PMT(*mode == '='))
1335 while(*mode == ' ') mode++;
1337 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1339 *open_flags |= MSVCRT__O_U8TEXT;
1340 mode += sizeof(utf8)/sizeof(utf8[0]);
1342 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1344 *open_flags |= MSVCRT__O_U16TEXT;
1345 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1347 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1349 *open_flags |= MSVCRT__O_WTEXT;
1350 mode += sizeof(unicode)/sizeof(unicode[0]);
1354 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1358 while(*mode == ' ') mode++;
1361 if(!MSVCRT_CHECK_PMT(*mode == 0))
1366 /*********************************************************************
1367 * _fdopen (MSVCRT.@)
1369 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1372 MSVCRT_wchar_t *modeW = NULL;
1374 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1376 ret = MSVCRT__wfdopen(fd, modeW);
1382 /*********************************************************************
1383 * _wfdopen (MSVCRT.@)
1385 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1387 int open_flags, stream_flags;
1390 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1393 if (!(file = msvcrt_alloc_fp()))
1395 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1400 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1406 /*********************************************************************
1407 * _filelength (MSVCRT.@)
1409 LONG CDECL MSVCRT__filelength(int fd)
1411 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1414 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1417 if (endPos != curPos)
1418 MSVCRT__lseek(fd, curPos, SEEK_SET);
1425 /*********************************************************************
1426 * _filelengthi64 (MSVCRT.@)
1428 __int64 CDECL MSVCRT__filelengthi64(int fd)
1430 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1433 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1436 if (endPos != curPos)
1437 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1444 /*********************************************************************
1445 * _fileno (MSVCRT.@)
1447 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1449 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1453 /*********************************************************************
1454 * _fstat64 (MSVCRT.@)
1456 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1460 BY_HANDLE_FILE_INFORMATION hfi;
1461 HANDLE hand = msvcrt_fdtoh(fd);
1463 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1464 if (hand == INVALID_HANDLE_VALUE)
1469 WARN(":failed-NULL buf\n");
1470 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1474 memset(&hfi, 0, sizeof(hfi));
1475 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1476 type = GetFileType(hand);
1477 if (type == FILE_TYPE_PIPE)
1479 buf->st_dev = buf->st_rdev = fd;
1480 buf->st_mode = S_IFIFO;
1483 else if (type == FILE_TYPE_CHAR)
1485 buf->st_dev = buf->st_rdev = fd;
1486 buf->st_mode = S_IFCHR;
1489 else /* FILE_TYPE_DISK etc. */
1491 if (!GetFileInformationByHandle(hand, &hfi))
1493 WARN(":failed-last error (%d)\n",GetLastError());
1494 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1497 buf->st_mode = S_IFREG | 0444;
1498 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1499 buf->st_mode |= 0222;
1500 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1501 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1503 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1504 buf->st_mtime = buf->st_ctime = dw;
1505 buf->st_nlink = hfi.nNumberOfLinks;
1507 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1512 /*********************************************************************
1513 * _fstati64 (MSVCRT.@)
1515 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1518 struct MSVCRT__stat64 buf64;
1520 ret = MSVCRT__fstat64(fd, &buf64);
1522 msvcrt_stat64_to_stati64(&buf64, buf);
1526 /*********************************************************************
1529 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1531 struct MSVCRT__stat64 buf64;
1533 ret = MSVCRT__fstat64(fd, &buf64);
1535 msvcrt_stat64_to_stat(&buf64, buf);
1539 /*********************************************************************
1540 * _futime64 (MSVCRT.@)
1542 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1544 HANDLE hand = msvcrt_fdtoh(fd);
1549 time_to_filetime( MSVCRT__time64(NULL), &at );
1554 time_to_filetime( t->actime, &at );
1555 time_to_filetime( t->modtime, &wt );
1558 if (!SetFileTime(hand, NULL, &at, &wt))
1560 msvcrt_set_errno(GetLastError());
1566 /*********************************************************************
1567 * _futime32 (MSVCRT.@)
1569 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1573 struct MSVCRT___utimbuf64 t64;
1574 t64.actime = t->actime;
1575 t64.modtime = t->modtime;
1576 return _futime64( fd, &t64 );
1579 return _futime64( fd, NULL );
1582 /*********************************************************************
1583 * _futime (MSVCRT.@)
1586 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1588 return _futime64( fd, t );
1591 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1593 return _futime32( fd, t );
1597 /*********************************************************************
1598 * _get_osfhandle (MSVCRT.@)
1600 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1602 HANDLE hand = msvcrt_fdtoh(fd);
1603 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1605 return (MSVCRT_intptr_t)hand;
1608 /*********************************************************************
1609 * _isatty (MSVCRT.@)
1611 int CDECL MSVCRT__isatty(int fd)
1613 HANDLE hand = msvcrt_fdtoh(fd);
1615 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1616 if (hand == INVALID_HANDLE_VALUE)
1619 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1622 /*********************************************************************
1623 * _mktemp (MSVCRT.@)
1625 char * CDECL MSVCRT__mktemp(char *pattern)
1628 char *retVal = pattern;
1633 numX = (*pattern++ == 'X')? numX + 1 : 0;
1637 id = GetCurrentProcessId();
1641 int tempNum = id / 10;
1642 *pattern-- = id - (tempNum * 10) + '0';
1648 *pattern = letter++;
1649 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1650 GetLastError() == ERROR_FILE_NOT_FOUND)
1652 } while(letter <= 'z');
1656 /*********************************************************************
1657 * _wmktemp (MSVCRT.@)
1659 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1662 MSVCRT_wchar_t *retVal = pattern;
1664 MSVCRT_wchar_t letter = 'a';
1667 numX = (*pattern++ == 'X')? numX + 1 : 0;
1671 id = GetCurrentProcessId();
1675 int tempNum = id / 10;
1676 *pattern-- = id - (tempNum * 10) + '0';
1682 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1683 GetLastError() == ERROR_FILE_NOT_FOUND)
1685 *pattern = letter++;
1686 } while(letter != '|');
1690 static unsigned split_oflags(unsigned oflags)
1693 unsigned unsupp; /* until we support everything */
1695 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1696 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1697 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1698 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
1699 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
1700 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
1701 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1702 else wxflags |= WX_TEXT; /* default to TEXT*/
1703 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1705 if ((unsupp = oflags & ~(
1706 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1707 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1708 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1709 MSVCRT__O_NOINHERIT|
1710 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
1711 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
1713 ERR(":unsupported oflags 0x%04x\n",unsupp);
1718 /*********************************************************************
1721 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1724 SECURITY_ATTRIBUTES sa;
1725 HANDLE readHandle, writeHandle;
1729 *MSVCRT__errno() = MSVCRT_EINVAL;
1733 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1734 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1735 sa.lpSecurityDescriptor = NULL;
1736 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1738 unsigned int wxflags = split_oflags(textmode);
1742 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
1746 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
1754 MSVCRT__close(pfds[0]);
1755 CloseHandle(writeHandle);
1756 *MSVCRT__errno() = MSVCRT_EMFILE;
1761 CloseHandle(readHandle);
1762 CloseHandle(writeHandle);
1763 *MSVCRT__errno() = MSVCRT_EMFILE;
1768 msvcrt_set_errno(GetLastError());
1773 static int check_bom(HANDLE h, int oflags, BOOL seek)
1775 char bom[sizeof(utf8_bom)];
1778 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
1780 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
1783 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
1784 oflags |= MSVCRT__O_U8TEXT;
1785 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
1787 SetFilePointer(h, 2, NULL, FILE_BEGIN);
1788 oflags |= MSVCRT__O_U16TEXT;
1790 SetFilePointer(h, 0, NULL, FILE_BEGIN);
1796 /*********************************************************************
1797 * _wsopen_s (MSVCRT.@)
1799 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1801 DWORD access = 0, creation = 0, attrib;
1802 SECURITY_ATTRIBUTES sa;
1807 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1808 fd, debugstr_w(path), oflags, shflags, pmode);
1810 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
1813 wxflag = split_oflags(oflags);
1814 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1816 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1817 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1818 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1821 if (oflags & MSVCRT__O_CREAT)
1823 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1824 FIXME(": pmode 0x%04x ignored\n", pmode);
1826 WARN(": pmode 0x%04x ignored\n", pmode);
1828 if (oflags & MSVCRT__O_EXCL)
1829 creation = CREATE_NEW;
1830 else if (oflags & MSVCRT__O_TRUNC)
1831 creation = CREATE_ALWAYS;
1833 creation = OPEN_ALWAYS;
1835 else /* no MSVCRT__O_CREAT */
1837 if (oflags & MSVCRT__O_TRUNC)
1838 creation = TRUNCATE_EXISTING;
1840 creation = OPEN_EXISTING;
1845 case MSVCRT__SH_DENYRW:
1848 case MSVCRT__SH_DENYWR:
1849 sharing = FILE_SHARE_READ;
1851 case MSVCRT__SH_DENYRD:
1852 sharing = FILE_SHARE_WRITE;
1854 case MSVCRT__SH_DENYNO:
1855 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1858 ERR( "Unhandled shflags 0x%x\n", shflags );
1859 return MSVCRT_EINVAL;
1861 attrib = FILE_ATTRIBUTE_NORMAL;
1863 if (oflags & MSVCRT__O_TEMPORARY)
1865 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1867 sharing |= FILE_SHARE_DELETE;
1870 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1871 sa.lpSecurityDescriptor = NULL;
1872 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
1874 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
1875 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
1876 && !(access&GENERIC_READ))
1878 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
1879 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
1880 if (hand != INVALID_HANDLE_VALUE)
1882 oflags = check_bom(hand, oflags, FALSE);
1886 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
1889 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1890 if (hand == INVALID_HANDLE_VALUE) {
1891 WARN(":failed-last error (%d)\n",GetLastError());
1892 msvcrt_set_errno(GetLastError());
1893 return *MSVCRT__errno();
1896 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
1898 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
1899 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
1900 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
1902 if (oflags & MSVCRT__O_U8TEXT)
1904 DWORD written = 0, tmp;
1906 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
1907 sizeof(utf8_bom)-written, &tmp, NULL))
1909 if (written != sizeof(utf8_bom)) {
1910 WARN("error writting BOM\n");
1912 msvcrt_set_errno(GetLastError());
1913 return *MSVCRT__errno();
1918 DWORD written = 0, tmp;
1920 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
1921 sizeof(utf16_bom)-written, &tmp, NULL))
1923 if (written != sizeof(utf16_bom))
1925 WARN("error writting BOM\n");
1927 msvcrt_set_errno(GetLastError());
1928 return *MSVCRT__errno();
1932 else if (access & GENERIC_READ)
1933 oflags = check_bom(hand, oflags, TRUE);
1936 *fd = msvcrt_alloc_fd(hand, wxflag);
1938 return *MSVCRT__errno();
1940 if (oflags & MSVCRT__O_WTEXT)
1941 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
1942 else if (oflags & MSVCRT__O_U16TEXT)
1943 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16;
1944 else if (oflags & MSVCRT__O_U8TEXT)
1945 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF8;
1947 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1951 /*********************************************************************
1952 * _wsopen (MSVCRT.@)
1954 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1959 if (oflags & MSVCRT__O_CREAT)
1963 __ms_va_start(ap, shflags);
1964 pmode = va_arg(ap, int);
1970 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1974 /*********************************************************************
1975 * _sopen_s (MSVCRT.@)
1977 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1979 MSVCRT_wchar_t *pathW;
1982 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
1983 return MSVCRT_EINVAL;
1985 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
1990 /*********************************************************************
1993 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1998 if (oflags & MSVCRT__O_CREAT)
2002 __ms_va_start(ap, shflags);
2003 pmode = va_arg(ap, int);
2009 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2013 /*********************************************************************
2016 int CDECL MSVCRT__open( const char *path, int flags, ... )
2020 if (flags & MSVCRT__O_CREAT)
2023 __ms_va_start(ap, flags);
2024 pmode = va_arg(ap, int);
2026 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2029 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2032 /*********************************************************************
2035 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2039 if (flags & MSVCRT__O_CREAT)
2042 __ms_va_start(ap, flags);
2043 pmode = va_arg(ap, int);
2045 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2048 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2051 /*********************************************************************
2054 int CDECL MSVCRT__creat(const char *path, int flags)
2056 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2057 return MSVCRT__open(path, usedFlags);
2060 /*********************************************************************
2061 * _wcreat (MSVCRT.@)
2063 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2065 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2066 return MSVCRT__wopen(path, usedFlags);
2069 /*********************************************************************
2070 * _open_osfhandle (MSVCRT.@)
2072 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2076 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2077 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2078 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2079 * text - it never sets MSVCRT__O_BINARY.
2081 /* don't let split_oflags() decide the mode if no mode is passed */
2082 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2083 oflags |= MSVCRT__O_BINARY;
2085 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
2086 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
2090 /*********************************************************************
2093 int CDECL MSVCRT__rmtmp(void)
2095 int num_removed = 0, i;
2099 for (i = 3; i < MSVCRT_stream_idx; i++) {
2100 file = msvcrt_get_file(i);
2102 if (file->_tmpfname)
2104 MSVCRT_fclose(file);
2111 TRACE(":removed (%d) temp files\n",num_removed);
2115 /*********************************************************************
2118 * When reading \r as last character in text mode, read() positions
2119 * the file pointer on the \r character while getc() goes on to
2122 static int read_i(int fd, void *buf, unsigned int count)
2125 char *bufstart = buf;
2126 HANDLE hand = msvcrt_fdtoh(fd);
2127 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2132 if (fdinfo->wxflag & WX_ATEOF) {
2133 TRACE("already at EOF, returning 0\n");
2136 /* Don't trace small reads, it gets *very* annoying */
2138 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2139 if (hand == INVALID_HANDLE_VALUE)
2142 if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL))
2144 if (fdinfo->lookahead[0] != '\n')
2146 bufstart[0] = fdinfo->lookahead[0];
2147 fdinfo->lookahead[0] = '\n';
2149 if(count>1 && ReadFile(hand, bufstart+1, count-1, &num_read, NULL))
2155 if (count != 0 && num_read == 0)
2157 fdinfo->wxflag |= WX_ATEOF;
2158 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2160 else if (fdinfo->wxflag & WX_TEXT)
2163 if (bufstart[num_read-1] == '\r')
2167 fdinfo->wxflag &= ~WX_READCR;
2168 ReadFile(hand, bufstart, 1, &num_read, NULL);
2172 fdinfo->wxflag |= WX_READCR;
2177 fdinfo->wxflag &= ~WX_READCR;
2178 for (i=0, j=0; i<num_read; i++)
2180 /* in text mode, a ctrl-z signals EOF */
2181 if (bufstart[i] == 0x1a)
2183 fdinfo->wxflag |= WX_ATEOF;
2184 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2188 /* in text mode, strip \r if followed by \n */
2189 if (bufstart[i]=='\r' && i+1==num_read)
2194 if (ReadFile(hand, &lookahead, 1, &len, NULL) && len)
2196 if(lookahead=='\n' && j==0)
2197 bufstart[j++] = '\n';
2200 if(lookahead != '\n')
2201 bufstart[j++] = '\r';
2203 if (fdinfo->wxflag & WX_PIPE)
2204 fdinfo->lookahead[0] = lookahead;
2206 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2210 bufstart[j++] = '\r';
2212 else if(bufstart[i]!='\r' || bufstart[i+1]!='\n')
2213 bufstart[j++] = bufstart[i];
2220 if (GetLastError() == ERROR_BROKEN_PIPE)
2222 TRACE(":end-of-pipe\n");
2223 fdinfo->wxflag |= WX_ATEOF;
2228 TRACE(":failed-last error (%d)\n",GetLastError());
2234 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2238 /*********************************************************************
2241 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2244 num_read = read_i(fd, buf, count);
2248 /*********************************************************************
2249 * _setmode (MSVCRT.@)
2251 int CDECL MSVCRT__setmode(int fd,int mode)
2253 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2254 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
2255 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
2256 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
2257 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2259 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2263 /*********************************************************************
2264 * _stat64 (MSVCRT.@)
2266 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2269 WIN32_FILE_ATTRIBUTE_DATA hfi;
2270 unsigned short mode = ALL_S_IREAD;
2273 TRACE(":file (%s) buf(%p)\n",path,buf);
2275 plen = strlen(path);
2276 while (plen && path[plen-1]==' ')
2279 if (plen && (path[plen-1]=='\\' || path[plen-1]=='/'))
2281 *MSVCRT__errno() = MSVCRT_ENOENT;
2285 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2287 TRACE("failed (%d)\n",GetLastError());
2288 *MSVCRT__errno() = MSVCRT_ENOENT;
2292 memset(buf,0,sizeof(struct MSVCRT__stat64));
2294 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2295 Bon 011120: This FIXME seems incorrect
2296 Also a letter as first char isn't enough to be classified
2299 if (isalpha(*path)&& (*(path+1)==':'))
2300 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2302 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2304 /* Dir, or regular file? */
2305 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2306 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2309 mode |= MSVCRT__S_IFREG;
2311 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2313 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2314 (tolower(path[plen-3]) << 16);
2315 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2316 mode |= ALL_S_IEXEC;
2320 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2321 mode |= ALL_S_IWRITE;
2323 buf->st_mode = mode;
2325 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2326 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2328 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2329 buf->st_mtime = buf->st_ctime = dw;
2330 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2331 (int)(buf->st_size >> 32),(int)buf->st_size,
2332 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2336 /*********************************************************************
2337 * _stati64 (MSVCRT.@)
2339 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2342 struct MSVCRT__stat64 buf64;
2344 ret = MSVCRT_stat64(path, &buf64);
2346 msvcrt_stat64_to_stati64(&buf64, buf);
2350 /*********************************************************************
2353 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2355 struct MSVCRT__stat64 buf64;
2357 ret = MSVCRT_stat64( path, &buf64);
2359 msvcrt_stat64_to_stat(&buf64, buf);
2363 /*********************************************************************
2364 * _wstat64 (MSVCRT.@)
2366 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2369 WIN32_FILE_ATTRIBUTE_DATA hfi;
2370 unsigned short mode = ALL_S_IREAD;
2373 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2375 plen = strlenW(path);
2376 while (plen && path[plen-1]==' ')
2379 if(plen && (path[plen-1]=='\\' || path[plen-1]=='/'))
2381 *MSVCRT__errno() = MSVCRT_ENOENT;
2385 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2387 TRACE("failed (%d)\n",GetLastError());
2388 *MSVCRT__errno() = MSVCRT_ENOENT;
2392 memset(buf,0,sizeof(struct MSVCRT__stat64));
2394 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2395 if (MSVCRT_iswalpha(*path))
2396 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2398 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2400 /* Dir, or regular file? */
2401 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2402 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2405 mode |= MSVCRT__S_IFREG;
2407 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2409 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2410 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2411 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2412 mode |= ALL_S_IEXEC;
2416 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2417 mode |= ALL_S_IWRITE;
2419 buf->st_mode = mode;
2421 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2422 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2424 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2425 buf->st_mtime = buf->st_ctime = dw;
2426 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2427 (int)(buf->st_size >> 32),(int)buf->st_size,
2428 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2432 /*********************************************************************
2433 * _wstati64 (MSVCRT.@)
2435 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2438 struct MSVCRT__stat64 buf64;
2440 ret = MSVCRT__wstat64(path, &buf64);
2442 msvcrt_stat64_to_stati64(&buf64, buf);
2446 /*********************************************************************
2449 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2452 struct MSVCRT__stat64 buf64;
2454 ret = MSVCRT__wstat64( path, &buf64 );
2455 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2459 /*********************************************************************
2462 MSVCRT_long CDECL MSVCRT__tell(int fd)
2464 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2467 /*********************************************************************
2468 * _telli64 (MSVCRT.@)
2470 __int64 CDECL _telli64(int fd)
2472 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2475 /*********************************************************************
2476 * _tempnam (MSVCRT.@)
2478 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
2480 char tmpbuf[MAX_PATH];
2481 const char *tmp_dir = MSVCRT_getenv("TMP");
2483 if (tmp_dir) dir = tmp_dir;
2485 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2486 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2488 TRACE("got name (%s)\n",tmpbuf);
2489 DeleteFileA(tmpbuf);
2490 return MSVCRT__strdup(tmpbuf);
2492 TRACE("failed (%d)\n",GetLastError());
2496 /*********************************************************************
2497 * _wtempnam (MSVCRT.@)
2499 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2501 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
2502 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2503 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
2505 if (tmp_dir) dir = tmp_dir;
2507 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2508 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2510 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2511 DeleteFileW(tmpbuf);
2512 return MSVCRT__wcsdup(tmpbuf);
2514 TRACE("failed (%d)\n",GetLastError());
2518 /*********************************************************************
2521 int CDECL MSVCRT__umask(int umask)
2523 int old_umask = MSVCRT_umask;
2524 TRACE("(%d)\n",umask);
2525 MSVCRT_umask = umask;
2529 /*********************************************************************
2530 * _utime64 (MSVCRT.@)
2532 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2534 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2538 int retVal = _futime64(fd, t);
2545 /*********************************************************************
2546 * _utime32 (MSVCRT.@)
2548 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2552 struct MSVCRT___utimbuf64 t64;
2553 t64.actime = t->actime;
2554 t64.modtime = t->modtime;
2555 return _utime64( path, &t64 );
2558 return _utime64( path, NULL );
2561 /*********************************************************************
2565 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2567 return _utime64( path, t );
2570 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2572 return _utime32( path, t );
2576 /*********************************************************************
2577 * _wutime64 (MSVCRT.@)
2579 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2581 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2585 int retVal = _futime64(fd, t);
2592 /*********************************************************************
2593 * _wutime32 (MSVCRT.@)
2595 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2599 struct MSVCRT___utimbuf64 t64;
2600 t64.actime = t->actime;
2601 t64.modtime = t->modtime;
2602 return _wutime64( path, &t64 );
2605 return _wutime64( path, NULL );
2608 /*********************************************************************
2609 * _wutime (MSVCRT.@)
2612 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2614 return _wutime64( path, t );
2617 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2619 return _wutime32( path, t );
2623 /*********************************************************************
2626 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2629 ioinfo *info = msvcrt_get_ioinfo(fd);
2630 HANDLE hand = info->handle;
2632 /* Don't trace small writes, it gets *very* annoying */
2635 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2637 if (hand == INVALID_HANDLE_VALUE)
2639 *MSVCRT__errno() = MSVCRT_EBADF;
2643 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
2645 *MSVCRT__errno() = MSVCRT_EINVAL;
2649 /* If appending, go to EOF */
2650 if (info->wxflag & WX_APPEND)
2651 MSVCRT__lseek(fd, 0, FILE_END);
2653 if (!(info->wxflag & WX_TEXT))
2655 if (WriteFile(hand, buf, count, &num_written, NULL)
2656 && (num_written == count))
2658 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2659 hand, GetLastError());
2660 *MSVCRT__errno() = MSVCRT_ENOSPC;
2664 unsigned int i, j, nr_lf, size;
2667 const char *s = buf, *buf_start = buf;
2669 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
2671 /* find number of \n */
2672 for (nr_lf=0, i=0; i<count; i++)
2678 if ((q = p = MSVCRT_malloc(size)))
2680 for (s = buf, i = 0, j = 0; i < count; i++)
2689 FIXME("Malloc failed\n");
2701 else if (info->exflag & EF_UTF16)
2703 for (nr_lf=0, i=0; i<count; i+=2)
2704 if (s[i]=='\n' && s[i+1]==0)
2709 if ((q = p = MSVCRT_malloc(size)))
2711 for (s=buf, i=0, j=0; i<count; i++)
2713 if (s[i]=='\n' && s[i+1]==0)
2724 FIXME("Malloc failed\n");
2740 for(nr_lf=0, i=0; i<count; i+=2)
2741 if (s[i]=='\n' && s[i+1]==0)
2744 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
2746 msvcrt_set_errno(GetLastError());
2751 size = conv_len+nr_lf;
2752 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
2754 for (s=buf, i=0, j=0; i<count; i++)
2756 if (s[i]=='\n' && s[i+1]==0)
2764 q = p+count+nr_lf*2;
2765 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
2766 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
2770 FIXME("Malloc failed\n");
2777 if (!WriteFile(hand, q, size, &num_written, NULL))
2781 if (num_written != size)
2783 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2784 fd, hand, GetLastError(), num_written);
2785 *MSVCRT__errno() = MSVCRT_ENOSPC;
2786 return s - buf_start;
2794 /*********************************************************************
2797 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2801 MSVCRT__lock_file(file);
2802 len = MSVCRT__write(file->_file, &val, sizeof(val));
2803 if (len == sizeof(val)) {
2804 MSVCRT__unlock_file(file);
2808 file->_flag |= MSVCRT__IOERR;
2809 MSVCRT__unlock_file(file);
2813 /*********************************************************************
2816 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2820 MSVCRT__lock_file(file);
2822 MSVCRT_free(file->_tmpfname);
2823 file->_tmpfname = NULL;
2824 /* flush stdio buffers */
2825 if(file->_flag & MSVCRT__IOWRT)
2826 MSVCRT_fflush(file);
2827 if(file->_flag & MSVCRT__IOMYBUF)
2828 MSVCRT_free(file->_base);
2830 r=MSVCRT__close(file->_file);
2833 MSVCRT__unlock_file(file);
2835 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2838 /*********************************************************************
2841 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2843 return file->_flag & MSVCRT__IOEOF;
2846 /*********************************************************************
2849 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2851 return file->_flag & MSVCRT__IOERR;
2854 /*********************************************************************
2855 * _filbuf (MSVCRT.@)
2857 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2860 MSVCRT__lock_file(file);
2862 /* Allocate buffer if needed */
2863 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
2864 msvcrt_alloc_buffer(file);
2866 if(!(file->_flag & MSVCRT__IOREAD)) {
2867 if(file->_flag & MSVCRT__IORW)
2868 file->_flag |= MSVCRT__IOREAD;
2870 MSVCRT__unlock_file(file);
2875 if(file->_flag & MSVCRT__IONBF) {
2877 if ((r = read_i(file->_file,&c,1)) != 1) {
2878 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2879 MSVCRT__unlock_file(file);
2883 MSVCRT__unlock_file(file);
2886 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2888 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2890 MSVCRT__unlock_file(file);
2895 file->_ptr = file->_base+1;
2896 c = *(unsigned char *)file->_base;
2897 MSVCRT__unlock_file(file);
2902 /*********************************************************************
2905 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2910 MSVCRT__lock_file(file);
2913 i = (unsigned char *)file->_ptr++;
2916 j = MSVCRT__filbuf(file);
2918 MSVCRT__unlock_file(file);
2922 /*********************************************************************
2923 * _fgetchar (MSVCRT.@)
2925 int CDECL MSVCRT__fgetchar(void)
2927 return MSVCRT_fgetc(MSVCRT_stdin);
2930 /*********************************************************************
2933 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2935 int cc = MSVCRT_EOF;
2936 char * buf_start = s;
2938 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2939 file,file->_file,s,size);
2941 MSVCRT__lock_file(file);
2943 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2948 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2950 TRACE(":nothing read\n");
2951 MSVCRT__unlock_file(file);
2954 if ((cc != MSVCRT_EOF) && (size > 1))
2957 TRACE(":got %s\n", debugstr_a(buf_start));
2958 MSVCRT__unlock_file(file);
2962 /*********************************************************************
2965 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2966 * the CR from CR/LF combinations
2968 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2972 MSVCRT__lock_file(file);
2973 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
2980 for(i=0; i<sizeof(wc); i++)
2990 j = MSVCRT__filbuf(file);
2993 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2996 MSVCRT__unlock_file(file);
3003 MSVCRT__unlock_file(file);
3007 c = MSVCRT_fgetc(file);
3008 if ((get_locinfo()->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
3010 FIXME("Treat Multibyte characters\n");
3013 MSVCRT__unlock_file(file);
3014 if (c == MSVCRT_EOF)
3017 return (MSVCRT_wint_t)c;
3020 /*********************************************************************
3023 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3030 MSVCRT__lock_file(file);
3031 for (j=0; j<sizeof(int); j++) {
3032 k = MSVCRT_fgetc(file);
3033 if (k == MSVCRT_EOF) {
3034 file->_flag |= MSVCRT__IOEOF;
3035 MSVCRT__unlock_file(file);
3041 MSVCRT__unlock_file(file);
3045 /*********************************************************************
3048 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3050 return MSVCRT_fgetwc(file);
3053 /*********************************************************************
3054 * _fgetwchar (MSVCRT.@)
3056 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3058 return MSVCRT_fgetwc(MSVCRT_stdin);
3061 /*********************************************************************
3062 * getwchar (MSVCRT.@)
3064 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3066 return MSVCRT__fgetwchar();
3069 /*********************************************************************
3072 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3074 int cc = MSVCRT_WEOF;
3075 MSVCRT_wchar_t * buf_start = s;
3077 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3078 file,file->_file,s,size);
3080 MSVCRT__lock_file(file);
3082 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
3087 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3089 TRACE(":nothing read\n");
3090 MSVCRT__unlock_file(file);
3093 if ((cc != MSVCRT_WEOF) && (size > 1))
3096 TRACE(":got %s\n", debugstr_w(buf_start));
3097 MSVCRT__unlock_file(file);
3101 /*********************************************************************
3104 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3106 MSVCRT_size_t wrcnt=size * nmemb;
3111 MSVCRT__lock_file(file);
3113 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3114 memcpy(file->_ptr, ptr, pcnt);
3119 ptr = (const char*)ptr + pcnt;
3120 } else if(!(file->_flag & MSVCRT__IOWRT)) {
3121 if(file->_flag & MSVCRT__IORW) {
3122 file->_flag |= MSVCRT__IOWRT;
3124 MSVCRT__unlock_file(file);
3131 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
3132 msvcrt_alloc_buffer(file);
3135 res=msvcrt_flush_buffer(file);
3137 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
3140 file->_flag |= MSVCRT__IOERR;
3143 written += pwritten;
3147 MSVCRT__unlock_file(file);
3148 return written / size;
3151 /*********************************************************************
3154 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3156 MSVCRT_wchar_t mwc=wc;
3157 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
3162 /*********************************************************************
3163 * _fputwchar (MSVCRT.@)
3165 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3167 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3170 /*********************************************************************
3171 * _wfsopen (MSVCRT.@)
3173 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
3176 int open_flags, stream_flags, fd;
3178 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3180 /* map mode string to open() flags. "man fopen" for possibilities. */
3181 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3185 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3188 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3190 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3197 TRACE(":got (%p)\n",file);
3198 if (fd >= 0 && !file)
3204 /*********************************************************************
3205 * _fsopen (MSVCRT.@)
3207 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3210 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3212 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3213 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3214 *MSVCRT__errno() = MSVCRT_EINVAL;
3217 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3220 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3221 *MSVCRT__errno() = MSVCRT_EINVAL;
3225 ret = MSVCRT__wfsopen(pathW, modeW, share);
3232 /*********************************************************************
3235 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3237 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3240 /*********************************************************************
3241 * fopen_s (MSVCRT.@)
3243 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3244 const char *filename, const char *mode)
3246 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3247 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3248 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3250 *pFile = MSVCRT_fopen(filename, mode);
3253 return *MSVCRT__errno();
3257 /*********************************************************************
3258 * _wfopen (MSVCRT.@)
3260 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3262 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3265 /*********************************************************************
3266 * _wfopen_s (MSVCRT.@)
3268 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3269 const MSVCRT_wchar_t *mode)
3271 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3272 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3273 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3275 *pFile = MSVCRT__wfopen(filename, mode);
3278 return *MSVCRT__errno();
3282 /*********************************************************************
3283 * _flsbuf (MSVCRT.@)
3285 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3287 /* Flush output buffer */
3288 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3289 msvcrt_alloc_buffer(file);
3291 if(!(file->_flag & MSVCRT__IOWRT)) {
3292 if(file->_flag & MSVCRT__IORW)
3293 file->_flag |= MSVCRT__IOWRT;
3301 res = msvcrt_flush_buffer(file);
3305 res = msvcrt_flush_buffer(file);
3308 return res ? res : c&0xff;
3312 /* set _cnt to 0 for unbuffered FILEs */
3314 len = MSVCRT__write(file->_file, &cc, 1);
3317 file->_flag |= MSVCRT__IOERR;
3322 /*********************************************************************
3325 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3329 MSVCRT__lock_file(file);
3335 res = msvcrt_flush_buffer(file);
3336 MSVCRT__unlock_file(file);
3337 return res ? res : c;
3340 MSVCRT__unlock_file(file);
3344 res = MSVCRT__flsbuf(c, file);
3345 MSVCRT__unlock_file(file);
3350 /*********************************************************************
3351 * _fputchar (MSVCRT.@)
3353 int CDECL MSVCRT__fputchar(int c)
3355 return MSVCRT_fputc(c, MSVCRT_stdout);
3358 /*********************************************************************
3361 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3363 MSVCRT_size_t rcnt=size * nmemb;
3364 MSVCRT_size_t read=0;
3370 MSVCRT__lock_file(file);
3372 /* first buffered data */
3374 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3375 memcpy(ptr, file->_ptr, pcnt);
3380 ptr = (char*)ptr + pcnt;
3381 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3382 if(file->_flag & MSVCRT__IORW) {
3383 file->_flag |= MSVCRT__IOREAD;
3385 MSVCRT__unlock_file(file);
3392 /* Fill the buffer on small reads.
3393 * TODO: Use a better buffering strategy.
3395 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
3396 if (file->_bufsiz == 0) {
3397 msvcrt_alloc_buffer(file);
3399 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3400 file->_ptr = file->_base;
3401 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3402 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3403 if (i > 0 && i < file->_cnt) {
3404 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3405 file->_flag &= ~MSVCRT__IOEOF;
3408 memcpy(ptr, file->_ptr, i);
3413 i = MSVCRT__read(file->_file,ptr, rcnt);
3417 ptr = (char *)ptr+i;
3418 /* expose feof condition in the flags
3419 * MFC tests file->_flag for feof, and doesn't call feof())
3421 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3422 file->_flag |= MSVCRT__IOEOF;
3425 file->_flag |= MSVCRT__IOERR;
3432 MSVCRT__unlock_file(file);
3436 /*********************************************************************
3437 * _wfreopen (MSVCRT.@)
3440 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3444 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3447 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3451 MSVCRT_fclose(file);
3452 file = MSVCRT__wfsopen(path, mode, MSVCRT__SH_DENYNO);
3458 /*********************************************************************
3459 * _wfreopen_s (MSVCRT.@)
3461 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
3462 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3464 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3465 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
3466 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3467 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3469 *pFile = MSVCRT__wfreopen(path, mode, file);
3472 return *MSVCRT__errno();
3476 /*********************************************************************
3477 * freopen (MSVCRT.@)
3480 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3483 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3485 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3486 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3492 ret = MSVCRT__wfreopen(pathW, modeW, file);
3499 /*********************************************************************
3500 * freopen_s (MSVCRT.@)
3502 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
3503 const char *path, const char *mode, MSVCRT_FILE* file)
3505 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3506 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
3507 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3508 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3510 *pFile = MSVCRT_freopen(path, mode, file);
3513 return *MSVCRT__errno();
3517 /*********************************************************************
3518 * fsetpos (MSVCRT.@)
3520 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3524 MSVCRT__lock_file(file);
3525 /* Note that all this has been lifted 'as is' from fseek */
3526 if(file->_flag & MSVCRT__IOWRT)
3527 msvcrt_flush_buffer(file);
3529 /* Discard buffered input */
3531 file->_ptr = file->_base;
3533 /* Reset direction of i/o */
3534 if(file->_flag & MSVCRT__IORW) {
3535 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3538 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3539 MSVCRT__unlock_file(file);
3543 /*********************************************************************
3544 * _ftelli64 (MSVCRT.@)
3546 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3548 /* TODO: just call fgetpos and return lower half of result */
3552 MSVCRT__lock_file(file);
3553 pos = _telli64(file->_file);
3555 MSVCRT__unlock_file(file);
3559 if( file->_flag & MSVCRT__IOWRT ) {
3560 off = file->_ptr - file->_base;
3563 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3564 /* Black magic correction for CR removal */
3566 for (i=0; i<file->_cnt; i++) {
3567 if (file->_ptr[i] == '\n')
3570 /* Black magic when reading CR at buffer boundary*/
3571 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3577 MSVCRT__unlock_file(file);
3581 /*********************************************************************
3584 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3586 return MSVCRT__ftelli64(file);
3589 /*********************************************************************
3590 * fgetpos (MSVCRT.@)
3592 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3596 MSVCRT__lock_file(file);
3597 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3599 MSVCRT__unlock_file(file);
3603 if( file->_flag & MSVCRT__IOWRT ) {
3604 off = file->_ptr - file->_base;
3607 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3608 /* Black magic correction for CR removal */
3610 for (i=0; i<file->_cnt; i++) {
3611 if (file->_ptr[i] == '\n')
3614 /* Black magic when reading CR at buffer boundary*/
3615 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3621 MSVCRT__unlock_file(file);
3625 /*********************************************************************
3628 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3630 MSVCRT_size_t len = strlen(s);
3633 MSVCRT__lock_file(file);
3634 ret = MSVCRT_fwrite(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
3635 MSVCRT__unlock_file(file);
3639 /*********************************************************************
3642 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3644 MSVCRT_size_t i, len = strlenW(s);
3647 MSVCRT__lock_file(file);
3648 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3649 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3650 MSVCRT__unlock_file(file);
3653 for (i=0; i<len; i++) {
3654 if (((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3655 || MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
3656 MSVCRT__unlock_file(file);
3661 MSVCRT__unlock_file(file);
3665 /*********************************************************************
3666 * getchar (MSVCRT.@)
3668 int CDECL MSVCRT_getchar(void)
3670 return MSVCRT_fgetc(MSVCRT_stdin);
3673 /*********************************************************************
3676 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3678 return MSVCRT_fgetc(file);
3681 /*********************************************************************
3684 char * CDECL MSVCRT_gets(char *buf)
3687 char * buf_start = buf;
3689 MSVCRT__lock_file(MSVCRT_stdin);
3690 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3691 cc = MSVCRT_fgetc(MSVCRT_stdin))
3692 if(cc != '\r') *buf++ = (char)cc;
3696 TRACE("got '%s'\n", buf_start);
3697 MSVCRT__unlock_file(MSVCRT_stdin);
3701 /*********************************************************************
3704 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3707 MSVCRT_wchar_t* ws = buf;
3709 MSVCRT__lock_file(MSVCRT_stdin);
3710 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3711 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3714 *buf++ = (MSVCRT_wchar_t)cc;
3718 TRACE("got %s\n", debugstr_w(ws));
3719 MSVCRT__unlock_file(MSVCRT_stdin);
3723 /*********************************************************************
3726 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3728 return MSVCRT_fputc(c, file);
3731 /*********************************************************************
3732 * putchar (MSVCRT.@)
3734 int CDECL MSVCRT_putchar(int c)
3736 return MSVCRT_fputc(c, MSVCRT_stdout);
3739 /*********************************************************************
3740 * _putwch (MSVCRT.@)
3742 int CDECL MSVCRT__putwch(int c)
3744 return MSVCRT_fputwc(c, MSVCRT_stdout);
3747 /*********************************************************************
3750 int CDECL MSVCRT_puts(const char *s)
3752 MSVCRT_size_t len = strlen(s);
3755 MSVCRT__lock_file(MSVCRT_stdout);
3756 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3757 MSVCRT__unlock_file(MSVCRT_stdout);
3761 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3762 MSVCRT__unlock_file(MSVCRT_stdout);
3766 /*********************************************************************
3769 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
3771 static const MSVCRT_wchar_t nl = '\n';
3772 MSVCRT_size_t len = strlenW(s);
3775 MSVCRT__lock_file(MSVCRT_stdout);
3776 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3777 MSVCRT__unlock_file(MSVCRT_stdout);
3781 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3782 MSVCRT__unlock_file(MSVCRT_stdout);
3786 /*********************************************************************
3789 int CDECL MSVCRT_remove(const char *path)
3791 TRACE("(%s)\n",path);
3792 if (DeleteFileA(path))
3794 TRACE(":failed (%d)\n",GetLastError());
3795 msvcrt_set_errno(GetLastError());
3799 /*********************************************************************
3800 * _wremove (MSVCRT.@)
3802 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
3804 TRACE("(%s)\n",debugstr_w(path));
3805 if (DeleteFileW(path))
3807 TRACE(":failed (%d)\n",GetLastError());
3808 msvcrt_set_errno(GetLastError());
3812 /*********************************************************************
3815 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3817 TRACE(":from %s to %s\n",oldpath,newpath);
3818 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3820 TRACE(":failed (%d)\n",GetLastError());
3821 msvcrt_set_errno(GetLastError());
3825 /*********************************************************************
3826 * _wrename (MSVCRT.@)
3828 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3830 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3831 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3833 TRACE(":failed (%d)\n",GetLastError());
3834 msvcrt_set_errno(GetLastError());
3838 /*********************************************************************
3839 * setvbuf (MSVCRT.@)
3841 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3843 MSVCRT__lock_file(file);
3845 MSVCRT_free(file->_base);
3849 if(mode == MSVCRT__IOFBF) {
3850 file->_flag &= ~MSVCRT__IONBF;
3851 file->_base = file->_ptr = buf;
3853 file->_bufsiz = size;
3856 file->_flag |= MSVCRT__IONBF;
3858 MSVCRT__unlock_file(file);
3862 /*********************************************************************
3865 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3867 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3870 /*********************************************************************
3873 char * CDECL MSVCRT_tmpnam(char *s)
3880 thread_data_t *data = msvcrt_get_thread_data();
3882 if(!data->tmpnam_buffer)
3883 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
3885 s = data->tmpnam_buffer;
3888 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3889 p = s + sprintf(s, "\\s%s.", tmpstr);
3890 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3892 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
3893 memcpy(p, tmpstr, size);
3895 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3896 GetLastError() == ERROR_FILE_NOT_FOUND)
3902 /*********************************************************************
3903 * _wtmpnam (MSVCRT.@)
3905 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
3907 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
3908 MSVCRT_wchar_t tmpstr[16];
3912 thread_data_t *data = msvcrt_get_thread_data();
3914 if(!data->wtmpnam_buffer)
3915 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
3917 s = data->wtmpnam_buffer;
3920 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
3921 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
3922 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3924 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
3925 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
3927 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
3928 GetLastError() == ERROR_FILE_NOT_FOUND)
3934 /*********************************************************************
3935 * tmpfile (MSVCRT.@)
3937 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3939 char *filename = MSVCRT_tmpnam(NULL);
3941 MSVCRT_FILE* file = NULL;
3944 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
3945 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3946 if (fd != -1 && (file = msvcrt_alloc_fp()))
3948 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
3953 else file->_tmpfname = MSVCRT__strdup(filename);
3956 if(fd != -1 && !file)
3962 /*********************************************************************
3963 * tmpfile_s (MSVCRT.@)
3965 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
3967 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3969 *file = MSVCRT_tmpfile();
3973 static int puts_clbk_file_a(void *file, int len, const char *str)
3975 return MSVCRT_fwrite(str, sizeof(char), len, file);
3978 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
3980 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
3983 /*********************************************************************
3984 * vfprintf (MSVCRT.@)
3986 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3990 MSVCRT__lock_file(file);
3991 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3992 MSVCRT__unlock_file(file);
3997 /*********************************************************************
3998 * vfprintf_s (MSVCRT.@)
4000 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4004 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4006 MSVCRT__lock_file(file);
4007 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4008 MSVCRT__unlock_file(file);
4013 /*********************************************************************
4014 * vfwprintf (MSVCRT.@)
4016 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4020 MSVCRT__lock_file(file);
4021 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4022 MSVCRT__unlock_file(file);
4027 /*********************************************************************
4028 * vfwprintf_s (MSVCRT.@)
4030 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4034 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4036 MSVCRT__lock_file(file);
4037 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4038 MSVCRT__unlock_file(file);
4043 /*********************************************************************
4044 * _vfwprintf_l (MSVCRT.@)
4046 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
4047 MSVCRT__locale_t locale, __ms_va_list valist)
4051 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4053 MSVCRT__lock_file(file);
4054 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4055 MSVCRT__unlock_file(file);
4060 /*********************************************************************
4061 * vprintf (MSVCRT.@)
4063 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
4065 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
4068 /*********************************************************************
4069 * vprintf_s (MSVCRT.@)
4071 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
4073 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
4076 /*********************************************************************
4077 * vwprintf (MSVCRT.@)
4079 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
4081 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
4084 /*********************************************************************
4085 * vwprintf_s (MSVCRT.@)
4087 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
4089 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
4092 /*********************************************************************
4093 * fprintf (MSVCRT.@)
4095 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
4097 __ms_va_list valist;
4099 __ms_va_start(valist, format);
4100 res = MSVCRT_vfprintf(file, format, valist);
4101 __ms_va_end(valist);
4105 /*********************************************************************
4106 * fprintf_s (MSVCRT.@)
4108 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
4110 __ms_va_list valist;
4112 __ms_va_start(valist, format);
4113 res = MSVCRT_vfprintf_s(file, format, valist);
4114 __ms_va_end(valist);
4118 /*********************************************************************
4119 * fwprintf (MSVCRT.@)
4121 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4123 __ms_va_list valist;
4125 __ms_va_start(valist, format);
4126 res = MSVCRT_vfwprintf(file, format, valist);
4127 __ms_va_end(valist);
4131 /*********************************************************************
4132 * fwprintf_s (MSVCRT.@)
4134 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4136 __ms_va_list valist;
4138 __ms_va_start(valist, format);
4139 res = MSVCRT_vfwprintf_s(file, format, valist);
4140 __ms_va_end(valist);
4144 /*********************************************************************
4145 * _fwprintf_l (MSVCRT.@)
4147 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
4149 __ms_va_list valist;
4151 __ms_va_start(valist, locale);
4152 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
4153 __ms_va_end(valist);
4157 /*********************************************************************
4160 int CDECL MSVCRT_printf(const char *format, ...)
4162 __ms_va_list valist;
4164 __ms_va_start(valist, format);
4165 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
4166 __ms_va_end(valist);
4170 /*********************************************************************
4171 * printf_s (MSVCRT.@)
4173 int CDECL MSVCRT_printf_s(const char *format, ...)
4175 __ms_va_list valist;
4177 __ms_va_start(valist, format);
4178 res = MSVCRT_vprintf_s(format, valist);
4179 __ms_va_end(valist);
4183 /*********************************************************************
4186 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
4188 if (c == MSVCRT_EOF)
4191 MSVCRT__lock_file(file);
4192 if(file->_bufsiz == 0) {
4193 msvcrt_alloc_buffer(file);
4196 if(file->_ptr>file->_base) {
4200 MSVCRT_clearerr(file);
4201 MSVCRT__unlock_file(file);
4205 MSVCRT__unlock_file(file);
4209 /*********************************************************************
4210 * ungetwc (MSVCRT.@)
4212 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
4214 MSVCRT_wchar_t mwc = wc;
4215 unsigned char * pp = (unsigned char *)&mwc;
4218 if (wc == MSVCRT_WEOF)
4221 MSVCRT__lock_file(file);
4222 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
4223 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
4224 MSVCRT__unlock_file(file);
4229 MSVCRT__unlock_file(file);
4233 /*********************************************************************
4234 * wprintf (MSVCRT.@)
4236 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
4238 __ms_va_list valist;
4240 __ms_va_start(valist, format);
4241 res = MSVCRT_vwprintf(format, valist);
4242 __ms_va_end(valist);
4246 /*********************************************************************
4247 * wprintf_s (MSVCRT.@)
4249 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
4251 __ms_va_list valist;
4253 __ms_va_start(valist, format);
4254 res = MSVCRT_vwprintf_s(format, valist);
4255 __ms_va_end(valist);
4259 /*********************************************************************
4260 * _getmaxstdio (MSVCRT.@)
4262 int CDECL MSVCRT__getmaxstdio(void)
4264 return MSVCRT_max_streams;
4267 /*********************************************************************
4268 * _setmaxstdio (MSVCRT.@)
4270 int CDECL MSVCRT__setmaxstdio(int newmax)
4272 TRACE("%d\n", newmax);
4274 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4277 MSVCRT_max_streams = newmax;
4278 return MSVCRT_max_streams;