2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
45 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
51 /* for stat mode, permissions apply to all,owner and group */
52 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
53 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
54 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
56 /* _access() bit flags FIXME: incomplete */
57 #define MSVCRT_W_OK 0x02
58 #define MSVCRT_R_OK 0x04
60 /* values for wxflag in file descriptor */
63 #define WX_READNL 0x04 /* read started with \n */
65 #define WX_DONTINHERIT 0x10
66 #define WX_APPEND 0x20
69 /* values for exflag - it's used differently in msvcr90.dll*/
72 #define EF_CRIT_INIT 0x04
73 #define EF_UNK_UNICODE 0x08
75 static char utf8_bom[3] = { 0xef, 0xbb, 0xbf };
76 static char utf16_bom[2] = { 0xff, 0xfe };
78 /* FIXME: this should be allocated dynamically */
79 #define MSVCRT_MAX_FILES 2048
80 #define MSVCRT_FD_BLOCK_SIZE 32
82 /* ioinfo structure size is different in msvcrXX.dll's */
88 CRITICAL_SECTION crit;
91 /*********************************************************************
92 * __pioinfo (MSVCRT.@)
93 * array of pointers to ioinfo arrays [32]
95 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
97 /*********************************************************************
98 * __badioinfo (MSVCRT.@)
100 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
102 static int MSVCRT_fdstart = 3; /* first unallocated fd */
103 static int MSVCRT_fdend = 3; /* highest allocated fd */
107 CRITICAL_SECTION crit;
110 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
111 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
112 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
114 /* INTERNAL: process umask */
115 static int MSVCRT_umask = 0;
117 /* INTERNAL: static data for tmpnam and _wtmpname functions */
118 static int tmpnam_unique;
120 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
121 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
122 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
123 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
125 #define TOUL(x) (ULONGLONG)(x)
126 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
127 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
128 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
129 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
131 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
132 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
133 * and MSVCRT_stream_idx, from race conditions.
134 * It doesn't protect against race conditions manipulating the underlying files
135 * or flags; doing so would probably be better accomplished with per-file
136 * protection, rather than locking the whole table for every change.
138 static CRITICAL_SECTION MSVCRT_file_cs;
139 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
141 0, 0, &MSVCRT_file_cs,
142 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
143 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
145 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
146 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
147 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
149 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
151 buf->st_dev = buf64->st_dev;
152 buf->st_ino = buf64->st_ino;
153 buf->st_mode = buf64->st_mode;
154 buf->st_nlink = buf64->st_nlink;
155 buf->st_uid = buf64->st_uid;
156 buf->st_gid = buf64->st_gid;
157 buf->st_rdev = buf64->st_rdev;
158 buf->st_size = buf64->st_size;
159 buf->st_atime = buf64->st_atime;
160 buf->st_mtime = buf64->st_mtime;
161 buf->st_ctime = buf64->st_ctime;
164 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
166 buf->st_dev = buf64->st_dev;
167 buf->st_ino = buf64->st_ino;
168 buf->st_mode = buf64->st_mode;
169 buf->st_nlink = buf64->st_nlink;
170 buf->st_uid = buf64->st_uid;
171 buf->st_gid = buf64->st_gid;
172 buf->st_rdev = buf64->st_rdev;
173 buf->st_size = buf64->st_size;
174 buf->st_atime = buf64->st_atime;
175 buf->st_mtime = buf64->st_mtime;
176 buf->st_ctime = buf64->st_ctime;
179 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
181 /* 1601 to 1970 is 369 years plus 89 leap days */
182 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
184 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
185 ft->dwHighDateTime = ticks >> 32;
186 ft->dwLowDateTime = ticks;
189 static inline ioinfo* msvcrt_get_ioinfo(int fd)
192 if(fd < MSVCRT_MAX_FILES)
193 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
195 return &MSVCRT___badioinfo;
197 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
200 static inline MSVCRT_FILE* msvcrt_get_file(int i)
204 if(i >= MSVCRT_max_streams)
208 return &MSVCRT__iob[i];
210 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
212 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
213 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
214 ERR("out of memory\n");
215 *MSVCRT__errno() = MSVCRT_ENOMEM;
219 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
221 ret += i%MSVCRT_FD_BLOCK_SIZE;
226 static inline BOOL msvcrt_is_valid_fd(int fd)
228 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
231 /* INTERNAL: Get the HANDLE for a fd
232 * This doesn't lock the table, because a failure will result in
233 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
234 * it returns a valid handle which is about to be closed, a subsequent call
235 * will fail, most likely in a sane way.
237 static HANDLE msvcrt_fdtoh(int fd)
239 if (!msvcrt_is_valid_fd(fd))
241 WARN(":fd (%d) - no handle!\n",fd);
242 *MSVCRT___doserrno() = 0;
243 *MSVCRT__errno() = MSVCRT_EBADF;
244 return INVALID_HANDLE_VALUE;
246 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
247 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd);
248 return msvcrt_get_ioinfo(fd)->handle;
251 /* INTERNAL: free a file entry fd */
252 static void msvcrt_free_fd(int fd)
258 fdinfo = msvcrt_get_ioinfo(fd);
259 old_handle = fdinfo->handle;
260 if(fdinfo != &MSVCRT___badioinfo)
262 fdinfo->handle = INVALID_HANDLE_VALUE;
265 TRACE(":fd (%d) freed\n",fd);
266 if (fd < 3) /* don't use 0,1,2 for user files */
271 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
274 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
277 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
283 if (fd == MSVCRT_fdend - 1)
285 if (fd < MSVCRT_fdstart)
291 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
292 /* caller must hold the files lock */
293 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
297 if (fd >= MSVCRT_MAX_FILES)
299 WARN(":files exhausted!\n");
300 *MSVCRT__errno() = MSVCRT_ENFILE;
304 fdinfo = msvcrt_get_ioinfo(fd);
305 if(fdinfo == &MSVCRT___badioinfo) {
308 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
309 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
310 WARN(":out of memory!\n");
311 *MSVCRT__errno() = MSVCRT_ENOMEM;
315 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
316 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
318 fdinfo = msvcrt_get_ioinfo(fd);
321 fdinfo->handle = hand;
322 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE));
323 fdinfo->lookahead[0] = '\n';
324 fdinfo->lookahead[1] = '\n';
325 fdinfo->lookahead[2] = '\n';
328 /* locate next free slot */
329 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
330 MSVCRT_fdstart = MSVCRT_fdend + 1;
332 while (MSVCRT_fdstart < MSVCRT_fdend &&
333 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
335 /* update last fd in use */
336 if (fd >= MSVCRT_fdend)
337 MSVCRT_fdend = fd + 1;
338 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
342 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
343 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
344 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
350 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
351 static int msvcrt_alloc_fd(HANDLE hand, int flag)
356 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
357 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
362 /* INTERNAL: Allocate a FILE* for an fd slot */
363 /* caller must hold the files lock */
364 static MSVCRT_FILE* msvcrt_alloc_fp(void)
369 for (i = 3; i < MSVCRT_max_streams; i++)
371 file = msvcrt_get_file(i);
375 if (file->_flag == 0)
377 if (i == MSVCRT_stream_idx)
379 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
381 InitializeCriticalSection(&((file_crit*)file)->crit);
382 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
393 /* INTERNAL: initialize a FILE* from an open fd */
394 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
396 TRACE(":fd (%d) allocating FILE*\n",fd);
397 if (!msvcrt_is_valid_fd(fd))
399 WARN(":invalid fd %d\n",fd);
400 *MSVCRT___doserrno() = 0;
401 *MSVCRT__errno() = MSVCRT_EBADF;
404 memset(file, 0, sizeof(*file));
406 file->_flag = stream_flags;
408 TRACE(":got FILE* (%p)\n",file);
412 /* INTERNAL: Create an inheritance data block (for spawned process)
413 * The inheritance block is made of:
414 * 00 int nb of file descriptor (NBFD)
415 * 04 char file flags (wxflag): repeated for each fd
416 * 4+NBFD HANDLE file handle: repeated for each fd
418 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
425 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
426 *block = MSVCRT_calloc(*size, 1);
432 wxflag_ptr = (char*)*block + sizeof(unsigned);
433 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
435 *(unsigned*)*block = MSVCRT_fdend;
436 for (fd = 0; fd < MSVCRT_fdend; fd++)
438 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
439 fdinfo = msvcrt_get_ioinfo(fd);
440 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
442 *wxflag_ptr = fdinfo->wxflag;
443 *handle_ptr = fdinfo->handle;
448 *handle_ptr = INVALID_HANDLE_VALUE;
450 wxflag_ptr++; handle_ptr++;
455 /* INTERNAL: Set up all file descriptors,
456 * as well as default streams (stdin, stderr and stdout)
458 void msvcrt_init_io(void)
464 GetStartupInfoA(&si);
465 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
471 count = *(unsigned*)si.lpReserved2;
472 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
473 handle_ptr = (HANDLE*)(wxflag_ptr + count);
475 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
476 count = min(count, MSVCRT_MAX_FILES);
477 for (i = 0; i < count; i++)
479 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
480 msvcrt_alloc_fd_from(*handle_ptr, *wxflag_ptr, i);
482 wxflag_ptr++; handle_ptr++;
484 MSVCRT_fdend = max( 3, count );
485 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
486 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
489 if(!MSVCRT___pioinfo[0])
490 msvcrt_alloc_fd_from(INVALID_HANDLE_VALUE, 0, 3);
492 fdinfo = msvcrt_get_ioinfo(0);
493 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
495 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
496 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
497 GetCurrentProcess(), &fdinfo->handle,
498 0, TRUE, DUPLICATE_SAME_ACCESS))
499 fdinfo->wxflag = WX_OPEN | WX_TEXT;
500 fdinfo->lookahead[0] = '\n';
501 fdinfo->lookahead[1] = '\n';
502 fdinfo->lookahead[2] = '\n';
506 fdinfo = msvcrt_get_ioinfo(1);
507 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
509 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
510 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
511 GetCurrentProcess(), &fdinfo->handle,
512 0, TRUE, DUPLICATE_SAME_ACCESS))
513 fdinfo->wxflag = WX_OPEN | WX_TEXT;
514 fdinfo->lookahead[0] = '\n';
515 fdinfo->lookahead[1] = '\n';
516 fdinfo->lookahead[2] = '\n';
520 fdinfo = msvcrt_get_ioinfo(2);
521 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
523 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
524 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
525 GetCurrentProcess(), &fdinfo->handle,
526 0, TRUE, DUPLICATE_SAME_ACCESS))
527 fdinfo->wxflag = WX_OPEN | WX_TEXT;
528 fdinfo->lookahead[0] = '\n';
529 fdinfo->lookahead[1] = '\n';
530 fdinfo->lookahead[2] = '\n';
534 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(0)->handle,
535 msvcrt_get_ioinfo(1)->handle, msvcrt_get_ioinfo(2)->handle);
537 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
538 for (i = 0; i < 3; i++)
540 /* FILE structs for stdin/out/err are static and never deleted */
541 MSVCRT__iob[i]._file = i;
542 MSVCRT__iob[i]._tmpfname = NULL;
543 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
545 MSVCRT_stream_idx = 3;
548 /* INTERNAL: Flush stdio file buffer */
549 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
552 int cnt=file->_ptr-file->_base;
553 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
554 file->_flag |= MSVCRT__IOERR;
557 file->_ptr=file->_base;
558 file->_cnt=file->_bufsiz;
563 /* INTERNAL: Allocate stdio file buffer */
564 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
566 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
568 file->_bufsiz = MSVCRT_BUFSIZ;
569 file->_flag |= MSVCRT__IOMYBUF;
571 file->_base = (char*)(&file->_charbuf);
573 file->_bufsiz = sizeof(file->_charbuf);
575 file->_ptr = file->_base;
579 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
580 static int msvcrt_int_to_base32(int num, char *str)
595 *p = (num & 31) + '0';
597 *p += ('a' - '0' - 10);
604 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
605 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
620 *p = (num & 31) + '0';
622 *p += ('a' - '0' - 10);
629 /*********************************************************************
630 * __iob_func(MSVCRT.@)
632 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
634 return &MSVCRT__iob[0];
637 /*********************************************************************
640 int CDECL MSVCRT__access(const char *filename, int mode)
642 DWORD attr = GetFileAttributesA(filename);
644 TRACE("(%s,%d) %d\n",filename,mode,attr);
646 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
648 msvcrt_set_errno(GetLastError());
651 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
653 msvcrt_set_errno(ERROR_ACCESS_DENIED);
659 /*********************************************************************
660 * _access_s (MSVCRT.@)
662 int CDECL _access_s(const char *filename, int mode)
664 if (!MSVCRT_CHECK_PMT(filename != NULL)) return -1;
665 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return -1;
667 return MSVCRT__access(filename, mode);
670 /*********************************************************************
671 * _waccess (MSVCRT.@)
673 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
675 DWORD attr = GetFileAttributesW(filename);
677 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
679 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
681 msvcrt_set_errno(GetLastError());
684 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
686 msvcrt_set_errno(ERROR_ACCESS_DENIED);
692 /*********************************************************************
693 * _waccess_s (MSVCRT.@)
695 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
697 if (!MSVCRT_CHECK_PMT(filename != NULL)) return -1;
698 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return -1;
700 return MSVCRT__waccess(filename, mode);
703 /*********************************************************************
706 int CDECL MSVCRT__chmod(const char *path, int flags)
708 DWORD oldFlags = GetFileAttributesA(path);
710 if (oldFlags != INVALID_FILE_ATTRIBUTES)
712 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
713 oldFlags | FILE_ATTRIBUTE_READONLY;
715 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
718 msvcrt_set_errno(GetLastError());
722 /*********************************************************************
725 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
727 DWORD oldFlags = GetFileAttributesW(path);
729 if (oldFlags != INVALID_FILE_ATTRIBUTES)
731 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
732 oldFlags | FILE_ATTRIBUTE_READONLY;
734 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
737 msvcrt_set_errno(GetLastError());
741 /*********************************************************************
744 int CDECL MSVCRT__unlink(const char *path)
746 TRACE("%s\n",debugstr_a(path));
747 if(DeleteFileA(path))
749 TRACE("failed (%d)\n",GetLastError());
750 msvcrt_set_errno(GetLastError());
754 /*********************************************************************
755 * _wunlink (MSVCRT.@)
757 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
759 TRACE("(%s)\n",debugstr_w(path));
760 if(DeleteFileW(path))
762 TRACE("failed (%d)\n",GetLastError());
763 msvcrt_set_errno(GetLastError());
767 /*********************************************************************
770 int CDECL MSVCRT__commit(int fd)
772 HANDLE hand = msvcrt_fdtoh(fd);
774 TRACE(":fd (%d) handle (%p)\n",fd,hand);
775 if (hand == INVALID_HANDLE_VALUE)
778 if (!FlushFileBuffers(hand))
780 if (GetLastError() == ERROR_INVALID_HANDLE)
782 /* FlushFileBuffers fails for console handles
783 * so we ignore this error.
787 TRACE(":failed-last error (%d)\n",GetLastError());
788 msvcrt_set_errno(GetLastError());
795 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
796 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
798 /* INTERNAL: Flush all stream buffer */
799 static int msvcrt_flush_all_buffers(int mask)
801 int i, num_flushed = 0;
805 for (i = 3; i < MSVCRT_stream_idx; i++) {
806 file = msvcrt_get_file(i);
810 if(file->_flag & mask) {
818 TRACE(":flushed (%d) handles\n",num_flushed);
822 /*********************************************************************
823 * _flushall (MSVCRT.@)
825 int CDECL MSVCRT__flushall(void)
827 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
830 /*********************************************************************
833 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
836 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
837 } else if(file->_flag & MSVCRT__IOWRT) {
840 MSVCRT__lock_file(file);
841 res = msvcrt_flush_buffer(file);
843 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
844 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
845 MSVCRT__unlock_file(file);
848 } else if(file->_flag & MSVCRT__IOREAD) {
849 MSVCRT__lock_file(file);
851 file->_ptr = file->_base;
852 MSVCRT__unlock_file(file);
859 /*********************************************************************
862 int CDECL MSVCRT__close(int fd)
868 hand = msvcrt_fdtoh(fd);
869 TRACE(":fd (%d) handle (%p)\n",fd,hand);
870 if (!msvcrt_is_valid_fd(fd)) {
874 ret = CloseHandle(hand) ? 0 : -1;
876 WARN(":failed-last error (%d)\n",GetLastError());
877 msvcrt_set_errno(GetLastError());
885 /*********************************************************************
888 * MSDN isn't clear on this point, but the remarks for _pipe
889 * indicate file descriptors duplicated with _dup and _dup2 are always
892 int CDECL MSVCRT__dup2(int od, int nd)
896 TRACE("(od=%d, nd=%d)\n", od, nd);
898 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
902 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
903 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
905 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
907 if (msvcrt_is_valid_fd(nd))
909 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
913 *MSVCRT__errno() = MSVCRT_EMFILE;
917 /* _dup2 returns 0, not nd, on success */
924 msvcrt_set_errno(GetLastError());
929 *MSVCRT__errno() = MSVCRT_EBADF;
936 /*********************************************************************
939 int CDECL MSVCRT__dup(int od)
945 if (MSVCRT__dup2(od, fd) == 0)
953 /*********************************************************************
956 int CDECL MSVCRT__eof(int fd)
959 LONG hcurpos,hendpos;
960 HANDLE hand = msvcrt_fdtoh(fd);
962 TRACE(":fd (%d) handle (%p)\n",fd,hand);
964 if (hand == INVALID_HANDLE_VALUE)
967 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
969 /* Otherwise we do it the hard way */
970 hcurpos = hendpos = 0;
971 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
972 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
974 if (curpos == endpos && hcurpos == hendpos)
976 /* FIXME: shouldn't WX_ATEOF be set here? */
980 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
984 /*********************************************************************
985 * _fcloseall (MSVCRT.@)
987 int CDECL MSVCRT__fcloseall(void)
989 int num_closed = 0, i;
993 for (i = 3; i < MSVCRT_stream_idx; i++) {
994 file = msvcrt_get_file(i);
996 if (file->_flag && !MSVCRT_fclose(file))
1001 TRACE(":closed (%d) handles\n",num_closed);
1005 /* free everything on process exit */
1006 void msvcrt_free_io(void)
1010 MSVCRT__fcloseall();
1011 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
1012 * stdout, and stderr (unlike GNU), so we need to fclose() them here
1013 * or they won't get flushed.
1015 MSVCRT_fclose(&MSVCRT__iob[0]);
1016 MSVCRT_fclose(&MSVCRT__iob[1]);
1017 MSVCRT_fclose(&MSVCRT__iob[2]);
1019 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1020 MSVCRT_free(MSVCRT___pioinfo[i]);
1022 for(i=0; i<MSVCRT_stream_idx; i++)
1024 MSVCRT_FILE *file = msvcrt_get_file(i);
1025 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1027 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1028 DeleteCriticalSection(&((file_crit*)file)->crit);
1032 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1033 MSVCRT_free(MSVCRT_fstream[i]);
1035 DeleteCriticalSection(&MSVCRT_file_cs);
1038 /*********************************************************************
1039 * _lseeki64 (MSVCRT.@)
1041 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1043 HANDLE hand = msvcrt_fdtoh(fd);
1046 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1047 if (hand == INVALID_HANDLE_VALUE)
1050 if (whence < 0 || whence > 2)
1052 *MSVCRT__errno() = MSVCRT_EINVAL;
1056 TRACE(":fd (%d) to %s pos %s\n",
1057 fd,wine_dbgstr_longlong(offset),
1058 (whence==SEEK_SET)?"SEEK_SET":
1059 (whence==SEEK_CUR)?"SEEK_CUR":
1060 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1062 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1063 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1064 ofs.QuadPart = offset;
1065 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1066 GetLastError() == ERROR_SUCCESS)
1068 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_ATEOF;
1069 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1071 return ofs.QuadPart;
1073 TRACE(":error-last error (%d)\n",GetLastError());
1074 msvcrt_set_errno(GetLastError());
1078 /*********************************************************************
1081 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1083 return MSVCRT__lseeki64(fd, offset, whence);
1086 /*********************************************************************
1087 * _lock_file (MSVCRT.@)
1089 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1091 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1092 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1094 EnterCriticalSection(&((file_crit*)file)->crit);
1097 /*********************************************************************
1098 * _unlock_file (MSVCRT.@)
1100 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1102 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1103 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1105 LeaveCriticalSection(&((file_crit*)file)->crit);
1108 /*********************************************************************
1109 * _locking (MSVCRT.@)
1111 * This is untested; the underlying LockFile doesn't work yet.
1113 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1117 HANDLE hand = msvcrt_fdtoh(fd);
1119 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1120 if (hand == INVALID_HANDLE_VALUE)
1123 if (mode < 0 || mode > 4)
1125 *MSVCRT__errno() = MSVCRT_EINVAL;
1129 TRACE(":fd (%d) by 0x%08x mode %s\n",
1130 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1131 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1132 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1133 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1134 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1137 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1139 FIXME ("Seek failed\n");
1140 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1143 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1146 ret = 1; /* just to satisfy gcc */
1149 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1154 else if (mode == MSVCRT__LK_UNLCK)
1155 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1157 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1158 /* FIXME - what about error settings? */
1159 return ret ? 0 : -1;
1162 /*********************************************************************
1163 * _fseeki64 (MSVCRT.@)
1165 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1169 MSVCRT__lock_file(file);
1170 /* Flush output if needed */
1171 if(file->_flag & MSVCRT__IOWRT)
1172 msvcrt_flush_buffer(file);
1174 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1176 offset += MSVCRT__ftelli64(file);
1179 /* Discard buffered input */
1181 file->_ptr = file->_base;
1182 /* Reset direction of i/o */
1183 if(file->_flag & MSVCRT__IORW) {
1184 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1186 /* Clear end of file flag */
1187 file->_flag &= ~MSVCRT__IOEOF;
1188 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1190 MSVCRT__unlock_file(file);
1194 /*********************************************************************
1197 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1199 return MSVCRT__fseeki64( file, offset, whence );
1202 /*********************************************************************
1203 * _chsize (MSVCRT.@)
1205 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1211 TRACE("(fd=%d, size=%d)\n", fd, size);
1215 handle = msvcrt_fdtoh(fd);
1216 if (handle != INVALID_HANDLE_VALUE)
1218 /* save the current file pointer */
1219 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1222 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1225 ret = SetEndOfFile(handle);
1226 if (!ret) msvcrt_set_errno(GetLastError());
1229 /* restore the file pointer */
1230 MSVCRT__lseek(fd, cur, SEEK_SET);
1235 return ret ? 0 : -1;
1238 /*********************************************************************
1239 * clearerr (MSVCRT.@)
1241 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1243 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1245 MSVCRT__lock_file(file);
1246 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1247 MSVCRT__unlock_file(file);
1250 /*********************************************************************
1253 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1255 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1257 MSVCRT__lock_file(file);
1258 MSVCRT_fseek(file, 0L, SEEK_SET);
1259 MSVCRT_clearerr(file);
1260 MSVCRT__unlock_file(file);
1263 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1265 int plus = strchrW(mode, '+') != NULL;
1267 TRACE("%s\n", debugstr_w(mode));
1269 while(*mode == ' ') mode++;
1274 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1275 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1278 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1279 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1282 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1283 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1286 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1290 *stream_flags |= MSVCRT__commode;
1292 while (*mode && *mode!=',')
1296 *open_flags |= MSVCRT__O_BINARY;
1297 *open_flags &= ~MSVCRT__O_TEXT;
1300 *open_flags |= MSVCRT__O_TEXT;
1301 *open_flags &= ~MSVCRT__O_BINARY;
1304 *open_flags |= MSVCRT__O_TEMPORARY;
1307 *open_flags |= MSVCRT__O_SHORT_LIVED;
1310 *stream_flags |= MSVCRT__IOCOMMIT;
1313 *stream_flags &= ~MSVCRT__IOCOMMIT;
1316 *open_flags |= MSVCRT__O_NOINHERIT;
1325 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1328 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1334 static const WCHAR ccs[] = {'c','c','s'};
1335 static const WCHAR utf8[] = {'u','t','f','-','8'};
1336 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1337 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1340 while(*mode == ' ') mode++;
1341 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1343 mode += sizeof(ccs)/sizeof(ccs[0]);
1344 while(*mode == ' ') mode++;
1345 if(!MSVCRT_CHECK_PMT(*mode == '='))
1348 while(*mode == ' ') mode++;
1350 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1352 *open_flags |= MSVCRT__O_U8TEXT;
1353 mode += sizeof(utf8)/sizeof(utf8[0]);
1355 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1357 *open_flags |= MSVCRT__O_U16TEXT;
1358 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1360 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1362 *open_flags |= MSVCRT__O_WTEXT;
1363 mode += sizeof(unicode)/sizeof(unicode[0]);
1367 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1371 while(*mode == ' ') mode++;
1374 if(!MSVCRT_CHECK_PMT(*mode == 0))
1379 /*********************************************************************
1380 * _fdopen (MSVCRT.@)
1382 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1385 MSVCRT_wchar_t *modeW = NULL;
1387 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1389 ret = MSVCRT__wfdopen(fd, modeW);
1395 /*********************************************************************
1396 * _wfdopen (MSVCRT.@)
1398 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1400 int open_flags, stream_flags;
1403 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1406 if (!(file = msvcrt_alloc_fp()))
1408 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1413 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1419 /*********************************************************************
1420 * _filelength (MSVCRT.@)
1422 LONG CDECL MSVCRT__filelength(int fd)
1424 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1427 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1430 if (endPos != curPos)
1431 MSVCRT__lseek(fd, curPos, SEEK_SET);
1438 /*********************************************************************
1439 * _filelengthi64 (MSVCRT.@)
1441 __int64 CDECL MSVCRT__filelengthi64(int fd)
1443 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1446 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1449 if (endPos != curPos)
1450 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1457 /*********************************************************************
1458 * _fileno (MSVCRT.@)
1460 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1462 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1466 /*********************************************************************
1467 * _fstat64 (MSVCRT.@)
1469 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1473 BY_HANDLE_FILE_INFORMATION hfi;
1474 HANDLE hand = msvcrt_fdtoh(fd);
1476 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1477 if (hand == INVALID_HANDLE_VALUE)
1482 WARN(":failed-NULL buf\n");
1483 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1487 memset(&hfi, 0, sizeof(hfi));
1488 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1489 type = GetFileType(hand);
1490 if (type == FILE_TYPE_PIPE)
1492 buf->st_dev = buf->st_rdev = fd;
1493 buf->st_mode = S_IFIFO;
1496 else if (type == FILE_TYPE_CHAR)
1498 buf->st_dev = buf->st_rdev = fd;
1499 buf->st_mode = S_IFCHR;
1502 else /* FILE_TYPE_DISK etc. */
1504 if (!GetFileInformationByHandle(hand, &hfi))
1506 WARN(":failed-last error (%d)\n",GetLastError());
1507 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1510 buf->st_mode = S_IFREG | 0444;
1511 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1512 buf->st_mode |= 0222;
1513 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1514 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1516 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1517 buf->st_mtime = buf->st_ctime = dw;
1518 buf->st_nlink = hfi.nNumberOfLinks;
1520 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1525 /*********************************************************************
1526 * _fstati64 (MSVCRT.@)
1528 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1531 struct MSVCRT__stat64 buf64;
1533 ret = MSVCRT__fstat64(fd, &buf64);
1535 msvcrt_stat64_to_stati64(&buf64, buf);
1539 /*********************************************************************
1542 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1544 struct MSVCRT__stat64 buf64;
1546 ret = MSVCRT__fstat64(fd, &buf64);
1548 msvcrt_stat64_to_stat(&buf64, buf);
1552 /*********************************************************************
1553 * _futime64 (MSVCRT.@)
1555 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1557 HANDLE hand = msvcrt_fdtoh(fd);
1562 time_to_filetime( MSVCRT__time64(NULL), &at );
1567 time_to_filetime( t->actime, &at );
1568 time_to_filetime( t->modtime, &wt );
1571 if (!SetFileTime(hand, NULL, &at, &wt))
1573 msvcrt_set_errno(GetLastError());
1579 /*********************************************************************
1580 * _futime32 (MSVCRT.@)
1582 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1586 struct MSVCRT___utimbuf64 t64;
1587 t64.actime = t->actime;
1588 t64.modtime = t->modtime;
1589 return _futime64( fd, &t64 );
1592 return _futime64( fd, NULL );
1595 /*********************************************************************
1596 * _futime (MSVCRT.@)
1599 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1601 return _futime64( fd, t );
1604 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1606 return _futime32( fd, t );
1610 /*********************************************************************
1611 * _get_osfhandle (MSVCRT.@)
1613 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1615 HANDLE hand = msvcrt_fdtoh(fd);
1616 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1618 return (MSVCRT_intptr_t)hand;
1621 /*********************************************************************
1622 * _isatty (MSVCRT.@)
1624 int CDECL MSVCRT__isatty(int fd)
1626 HANDLE hand = msvcrt_fdtoh(fd);
1628 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1629 if (hand == INVALID_HANDLE_VALUE)
1632 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1635 /*********************************************************************
1636 * _mktemp (MSVCRT.@)
1638 char * CDECL MSVCRT__mktemp(char *pattern)
1641 char *retVal = pattern;
1646 numX = (*pattern++ == 'X')? numX + 1 : 0;
1650 id = GetCurrentProcessId();
1654 int tempNum = id / 10;
1655 *pattern-- = id - (tempNum * 10) + '0';
1661 *pattern = letter++;
1662 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1663 GetLastError() == ERROR_FILE_NOT_FOUND)
1665 } while(letter <= 'z');
1669 /*********************************************************************
1670 * _wmktemp (MSVCRT.@)
1672 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1675 MSVCRT_wchar_t *retVal = pattern;
1677 MSVCRT_wchar_t letter = 'a';
1680 numX = (*pattern++ == 'X')? numX + 1 : 0;
1684 id = GetCurrentProcessId();
1688 int tempNum = id / 10;
1689 *pattern-- = id - (tempNum * 10) + '0';
1695 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1696 GetLastError() == ERROR_FILE_NOT_FOUND)
1698 *pattern = letter++;
1699 } while(letter != '|');
1703 static unsigned split_oflags(unsigned oflags)
1706 unsigned unsupp; /* until we support everything */
1708 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1709 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1710 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1711 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
1712 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
1713 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
1714 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1715 else wxflags |= WX_TEXT; /* default to TEXT*/
1716 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1718 if ((unsupp = oflags & ~(
1719 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1720 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1721 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1722 MSVCRT__O_NOINHERIT|
1723 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
1724 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
1726 ERR(":unsupported oflags 0x%04x\n",unsupp);
1731 /*********************************************************************
1734 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1737 SECURITY_ATTRIBUTES sa;
1738 HANDLE readHandle, writeHandle;
1742 *MSVCRT__errno() = MSVCRT_EINVAL;
1746 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1747 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1748 sa.lpSecurityDescriptor = NULL;
1749 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1751 unsigned int wxflags = split_oflags(textmode);
1755 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
1759 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
1767 MSVCRT__close(pfds[0]);
1768 CloseHandle(writeHandle);
1769 *MSVCRT__errno() = MSVCRT_EMFILE;
1774 CloseHandle(readHandle);
1775 CloseHandle(writeHandle);
1776 *MSVCRT__errno() = MSVCRT_EMFILE;
1781 msvcrt_set_errno(GetLastError());
1786 static int check_bom(HANDLE h, int oflags, BOOL seek)
1788 char bom[sizeof(utf8_bom)];
1791 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
1793 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
1796 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
1797 oflags |= MSVCRT__O_U8TEXT;
1798 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
1800 SetFilePointer(h, 2, NULL, FILE_BEGIN);
1801 oflags |= MSVCRT__O_U16TEXT;
1803 SetFilePointer(h, 0, NULL, FILE_BEGIN);
1809 /*********************************************************************
1810 * _wsopen_s (MSVCRT.@)
1812 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1814 DWORD access = 0, creation = 0, attrib;
1815 SECURITY_ATTRIBUTES sa;
1820 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1821 fd, debugstr_w(path), oflags, shflags, pmode);
1823 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
1826 wxflag = split_oflags(oflags);
1827 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1829 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1830 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1831 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1834 if (oflags & MSVCRT__O_CREAT)
1836 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1837 FIXME(": pmode 0x%04x ignored\n", pmode);
1839 WARN(": pmode 0x%04x ignored\n", pmode);
1841 if (oflags & MSVCRT__O_EXCL)
1842 creation = CREATE_NEW;
1843 else if (oflags & MSVCRT__O_TRUNC)
1844 creation = CREATE_ALWAYS;
1846 creation = OPEN_ALWAYS;
1848 else /* no MSVCRT__O_CREAT */
1850 if (oflags & MSVCRT__O_TRUNC)
1851 creation = TRUNCATE_EXISTING;
1853 creation = OPEN_EXISTING;
1858 case MSVCRT__SH_DENYRW:
1861 case MSVCRT__SH_DENYWR:
1862 sharing = FILE_SHARE_READ;
1864 case MSVCRT__SH_DENYRD:
1865 sharing = FILE_SHARE_WRITE;
1867 case MSVCRT__SH_DENYNO:
1868 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1871 ERR( "Unhandled shflags 0x%x\n", shflags );
1872 return MSVCRT_EINVAL;
1874 attrib = FILE_ATTRIBUTE_NORMAL;
1876 if (oflags & MSVCRT__O_TEMPORARY)
1878 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1880 sharing |= FILE_SHARE_DELETE;
1883 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1884 sa.lpSecurityDescriptor = NULL;
1885 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
1887 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
1888 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
1889 && !(access&GENERIC_READ))
1891 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
1892 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
1893 if (hand != INVALID_HANDLE_VALUE)
1895 oflags = check_bom(hand, oflags, FALSE);
1899 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
1902 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1903 if (hand == INVALID_HANDLE_VALUE) {
1904 WARN(":failed-last error (%d)\n",GetLastError());
1905 msvcrt_set_errno(GetLastError());
1906 return *MSVCRT__errno();
1909 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
1911 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
1912 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
1913 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
1915 if (oflags & MSVCRT__O_U8TEXT)
1917 DWORD written = 0, tmp;
1919 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
1920 sizeof(utf8_bom)-written, &tmp, NULL))
1922 if (written != sizeof(utf8_bom)) {
1923 WARN("error writting BOM\n");
1925 msvcrt_set_errno(GetLastError());
1926 return *MSVCRT__errno();
1931 DWORD written = 0, tmp;
1933 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
1934 sizeof(utf16_bom)-written, &tmp, NULL))
1936 if (written != sizeof(utf16_bom))
1938 WARN("error writting BOM\n");
1940 msvcrt_set_errno(GetLastError());
1941 return *MSVCRT__errno();
1945 else if (access & GENERIC_READ)
1946 oflags = check_bom(hand, oflags, TRUE);
1949 *fd = msvcrt_alloc_fd(hand, wxflag);
1951 return *MSVCRT__errno();
1953 if (oflags & MSVCRT__O_WTEXT)
1954 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
1955 else if (oflags & MSVCRT__O_U16TEXT)
1956 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16;
1957 else if (oflags & MSVCRT__O_U8TEXT)
1958 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF8;
1960 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1964 /*********************************************************************
1965 * _wsopen (MSVCRT.@)
1967 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1972 if (oflags & MSVCRT__O_CREAT)
1976 __ms_va_start(ap, shflags);
1977 pmode = va_arg(ap, int);
1983 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1987 /*********************************************************************
1988 * _sopen_s (MSVCRT.@)
1990 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1992 MSVCRT_wchar_t *pathW;
1995 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
1996 return MSVCRT_EINVAL;
1998 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2003 /*********************************************************************
2006 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2011 if (oflags & MSVCRT__O_CREAT)
2015 __ms_va_start(ap, shflags);
2016 pmode = va_arg(ap, int);
2022 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2026 /*********************************************************************
2029 int CDECL MSVCRT__open( const char *path, int flags, ... )
2033 if (flags & MSVCRT__O_CREAT)
2036 __ms_va_start(ap, flags);
2037 pmode = va_arg(ap, int);
2039 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2042 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2045 /*********************************************************************
2048 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2052 if (flags & MSVCRT__O_CREAT)
2055 __ms_va_start(ap, flags);
2056 pmode = va_arg(ap, int);
2058 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2061 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2064 /*********************************************************************
2067 int CDECL MSVCRT__creat(const char *path, int flags)
2069 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2070 return MSVCRT__open(path, usedFlags);
2073 /*********************************************************************
2074 * _wcreat (MSVCRT.@)
2076 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2078 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2079 return MSVCRT__wopen(path, usedFlags);
2082 /*********************************************************************
2083 * _open_osfhandle (MSVCRT.@)
2085 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2089 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2090 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2091 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2092 * text - it never sets MSVCRT__O_BINARY.
2094 /* don't let split_oflags() decide the mode if no mode is passed */
2095 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2096 oflags |= MSVCRT__O_BINARY;
2098 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
2099 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
2103 /*********************************************************************
2106 int CDECL MSVCRT__rmtmp(void)
2108 int num_removed = 0, i;
2112 for (i = 3; i < MSVCRT_stream_idx; i++) {
2113 file = msvcrt_get_file(i);
2115 if (file->_tmpfname)
2117 MSVCRT_fclose(file);
2124 TRACE(":removed (%d) temp files\n",num_removed);
2128 static inline int get_utf8_char_len(char ch)
2130 if((ch&0xf8) == 0xf0)
2132 else if((ch&0xf0) == 0xe0)
2134 else if((ch&0xe0) == 0xc0)
2139 /*********************************************************************
2140 * (internal) read_utf8
2142 static int read_utf8(int fd, MSVCRT_wchar_t *buf, unsigned int count)
2144 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2145 HANDLE hand = fdinfo->handle;
2146 char min_buf[4], *readbuf, lookahead;
2147 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2149 /* make the buffer big enough to hold at least one character */
2150 /* read bytes have to fit to output and lookahead buffers */
2152 readbuf_size = count < 4 ? 4 : count;
2153 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2158 if(fdinfo->lookahead[0] != '\n') {
2159 readbuf[pos++] = fdinfo->lookahead[0];
2160 fdinfo->lookahead[0] = '\n';
2162 if(fdinfo->lookahead[1] != '\n') {
2163 readbuf[pos++] = fdinfo->lookahead[1];
2164 fdinfo->lookahead[1] = '\n';
2166 if(fdinfo->lookahead[2] != '\n') {
2167 readbuf[pos++] = fdinfo->lookahead[2];
2168 fdinfo->lookahead[2] = '\n';
2173 /* NOTE: this case is broken in native dll, reading
2174 * sometimes fails when small buffer is passed
2177 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2178 if (GetLastError() == ERROR_BROKEN_PIPE) {
2179 fdinfo->wxflag |= WX_ATEOF;
2182 msvcrt_set_errno(GetLastError());
2185 }else if(!num_read) {
2186 fdinfo->wxflag |= WX_ATEOF;
2192 char_len = get_utf8_char_len(readbuf[0]);
2194 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2198 if(readbuf[0] == '\n')
2199 fdinfo->wxflag |= WX_READNL;
2201 fdinfo->wxflag &= ~WX_READNL;
2203 if(readbuf[0] == 0x1a) {
2204 fdinfo->wxflag |= WX_ATEOF;
2208 if(readbuf[0] == '\r') {
2209 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2211 else if(lookahead == '\n')
2215 if(fdinfo->wxflag & WX_PIPE)
2216 fdinfo->lookahead[0] = lookahead;
2218 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2223 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2224 msvcrt_set_errno(GetLastError());
2231 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2234 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2235 fdinfo->wxflag |= WX_ATEOF;
2236 if (readbuf != min_buf) MSVCRT_free(readbuf);
2239 msvcrt_set_errno(GetLastError());
2240 if (readbuf != min_buf) MSVCRT_free(readbuf);
2243 }else if(!pos && !num_read) {
2244 fdinfo->wxflag |= WX_ATEOF;
2245 if (readbuf != min_buf) MSVCRT_free(readbuf);
2250 if(readbuf[0] == '\n')
2251 fdinfo->wxflag |= WX_READNL;
2253 fdinfo->wxflag &= ~WX_READNL;
2255 /* Find first byte of last character (may be incomplete) */
2256 for(i=pos-1; i>0 && i>pos-4; i--)
2257 if((readbuf[i]&0xc0) != 0x80)
2259 char_len = get_utf8_char_len(readbuf[i]);
2260 if(char_len+i <= pos)
2263 if(fdinfo->wxflag & WX_PIPE) {
2265 fdinfo->lookahead[0] = readbuf[i];
2267 fdinfo->lookahead[1] = readbuf[i+1];
2269 fdinfo->lookahead[2] = readbuf[i+2];
2271 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2275 for(i=0, j=0; i<pos; i++) {
2276 if(readbuf[i] == 0x1a) {
2277 fdinfo->wxflag |= WX_ATEOF;
2281 /* strip '\r' if followed by '\n' */
2282 if(readbuf[i] == '\r' && i+1==pos) {
2283 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2284 readbuf[j++] = '\r';
2285 }else if(lookahead == '\n' && j==0) {
2286 readbuf[j++] = '\n';
2288 if(lookahead != '\n')
2289 readbuf[j++] = '\r';
2291 if(fdinfo->wxflag & WX_PIPE)
2292 fdinfo->lookahead[0] = lookahead;
2294 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2296 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2297 readbuf[j++] = readbuf[i];
2302 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2303 msvcrt_set_errno(GetLastError());
2304 if (readbuf != min_buf) MSVCRT_free(readbuf);
2308 if (readbuf != min_buf) MSVCRT_free(readbuf);
2312 /*********************************************************************
2315 * When reading \r as last character in text mode, read() positions
2316 * the file pointer on the \r character while getc() goes on to
2319 static int read_i(int fd, void *buf, unsigned int count)
2321 DWORD num_read, utf16;
2322 char *bufstart = buf;
2323 HANDLE hand = msvcrt_fdtoh(fd);
2324 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2329 if (fdinfo->wxflag & WX_ATEOF) {
2330 TRACE("already at EOF, returning 0\n");
2333 /* Don't trace small reads, it gets *very* annoying */
2335 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2336 if (hand == INVALID_HANDLE_VALUE)
2338 *MSVCRT__errno() = MSVCRT_EBADF;
2342 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2343 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2345 *MSVCRT__errno() = MSVCRT_EINVAL;
2349 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2350 return read_utf8(fd, buf, count);
2352 if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL))
2354 if (fdinfo->lookahead[0] != '\n')
2356 bufstart[0] = fdinfo->lookahead[0];
2357 fdinfo->lookahead[0] = '\n';
2361 bufstart[1] = fdinfo->lookahead[1];
2362 fdinfo->lookahead[1] = '\n';
2365 if(count>1+utf16 && ReadFile(hand, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2366 num_read += 1+utf16;
2371 if(utf16 && (num_read&1))
2373 /* msvcr90 uses uninitialized value from the buffer in this case */
2374 /* msvcrt ignores additional data */
2375 ERR("got odd number of bytes in UTF16 mode\n");
2379 if (count != 0 && num_read == 0)
2381 fdinfo->wxflag |= WX_ATEOF;
2382 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2384 else if (fdinfo->wxflag & WX_TEXT)
2388 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2389 fdinfo->wxflag |= WX_READNL;
2391 fdinfo->wxflag &= ~WX_READNL;
2393 for (i=0, j=0; i<num_read; i+=1+utf16)
2395 /* in text mode, a ctrl-z signals EOF */
2396 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2398 fdinfo->wxflag |= WX_ATEOF;
2399 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2403 /* in text mode, strip \r if followed by \n */
2404 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2409 lookahead[1] = '\n';
2410 if (ReadFile(hand, lookahead, 1+utf16, &len, NULL) && len)
2412 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2414 bufstart[j++] = '\n';
2415 if(utf16) bufstart[j++] = 0;
2419 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2421 bufstart[j++] = '\r';
2422 if(utf16) bufstart[j++] = 0;
2425 if (fdinfo->wxflag & WX_PIPE)
2427 fdinfo->lookahead[0] = lookahead[0];
2428 fdinfo->lookahead[1] = lookahead[1];
2431 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2436 bufstart[j++] = '\r';
2437 if(utf16) bufstart[j++] = 0;
2440 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2441 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2443 bufstart[j++] = bufstart[i];
2444 if(utf16) bufstart[j++] = bufstart[i+1];
2452 if (GetLastError() == ERROR_BROKEN_PIPE)
2454 TRACE(":end-of-pipe\n");
2455 fdinfo->wxflag |= WX_ATEOF;
2460 TRACE(":failed-last error (%d)\n",GetLastError());
2466 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2470 /*********************************************************************
2473 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2476 num_read = read_i(fd, buf, count);
2480 /*********************************************************************
2481 * _setmode (MSVCRT.@)
2483 int CDECL MSVCRT__setmode(int fd,int mode)
2485 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2486 if(ret==MSVCRT__O_TEXT && (msvcrt_get_ioinfo(fd)->exflag & (EF_UTF8|EF_UTF16)))
2487 ret = MSVCRT__O_WTEXT;
2489 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2490 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2491 *MSVCRT__errno() = MSVCRT_EINVAL;
2495 if(mode == MSVCRT__O_BINARY) {
2496 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2497 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2501 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2502 if(mode == MSVCRT__O_TEXT)
2503 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2504 else if(mode == MSVCRT__O_U8TEXT)
2505 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF16) | EF_UTF8;
2507 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF8) | EF_UTF16;
2512 /*********************************************************************
2513 * _stat64 (MSVCRT.@)
2515 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2518 WIN32_FILE_ATTRIBUTE_DATA hfi;
2519 unsigned short mode = ALL_S_IREAD;
2522 TRACE(":file (%s) buf(%p)\n",path,buf);
2524 plen = strlen(path);
2525 while (plen && path[plen-1]==' ')
2528 if (plen && (path[plen-1]=='\\' || path[plen-1]=='/'))
2530 *MSVCRT__errno() = MSVCRT_ENOENT;
2534 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2536 TRACE("failed (%d)\n",GetLastError());
2537 *MSVCRT__errno() = MSVCRT_ENOENT;
2541 memset(buf,0,sizeof(struct MSVCRT__stat64));
2543 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2544 Bon 011120: This FIXME seems incorrect
2545 Also a letter as first char isn't enough to be classified
2548 if (isalpha(*path)&& (*(path+1)==':'))
2549 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2551 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2553 /* Dir, or regular file? */
2554 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2555 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2558 mode |= MSVCRT__S_IFREG;
2560 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2562 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2563 (tolower(path[plen-3]) << 16);
2564 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2565 mode |= ALL_S_IEXEC;
2569 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2570 mode |= ALL_S_IWRITE;
2572 buf->st_mode = mode;
2574 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2575 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2577 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2578 buf->st_mtime = buf->st_ctime = dw;
2579 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2580 (int)(buf->st_size >> 32),(int)buf->st_size,
2581 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2585 /*********************************************************************
2586 * _stati64 (MSVCRT.@)
2588 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2591 struct MSVCRT__stat64 buf64;
2593 ret = MSVCRT_stat64(path, &buf64);
2595 msvcrt_stat64_to_stati64(&buf64, buf);
2599 /*********************************************************************
2602 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2604 struct MSVCRT__stat64 buf64;
2606 ret = MSVCRT_stat64( path, &buf64);
2608 msvcrt_stat64_to_stat(&buf64, buf);
2612 /*********************************************************************
2613 * _wstat64 (MSVCRT.@)
2615 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2618 WIN32_FILE_ATTRIBUTE_DATA hfi;
2619 unsigned short mode = ALL_S_IREAD;
2622 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2624 plen = strlenW(path);
2625 while (plen && path[plen-1]==' ')
2628 if(plen && (path[plen-1]=='\\' || path[plen-1]=='/'))
2630 *MSVCRT__errno() = MSVCRT_ENOENT;
2634 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2636 TRACE("failed (%d)\n",GetLastError());
2637 *MSVCRT__errno() = MSVCRT_ENOENT;
2641 memset(buf,0,sizeof(struct MSVCRT__stat64));
2643 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2644 if (MSVCRT_iswalpha(*path))
2645 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2647 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2649 /* Dir, or regular file? */
2650 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2651 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2654 mode |= MSVCRT__S_IFREG;
2656 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2658 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2659 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2660 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2661 mode |= ALL_S_IEXEC;
2665 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2666 mode |= ALL_S_IWRITE;
2668 buf->st_mode = mode;
2670 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2671 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2673 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2674 buf->st_mtime = buf->st_ctime = dw;
2675 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2676 (int)(buf->st_size >> 32),(int)buf->st_size,
2677 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2681 /*********************************************************************
2682 * _wstati64 (MSVCRT.@)
2684 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2687 struct MSVCRT__stat64 buf64;
2689 ret = MSVCRT__wstat64(path, &buf64);
2691 msvcrt_stat64_to_stati64(&buf64, buf);
2695 /*********************************************************************
2698 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2701 struct MSVCRT__stat64 buf64;
2703 ret = MSVCRT__wstat64( path, &buf64 );
2704 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2708 /*********************************************************************
2711 MSVCRT_long CDECL MSVCRT__tell(int fd)
2713 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2716 /*********************************************************************
2717 * _telli64 (MSVCRT.@)
2719 __int64 CDECL _telli64(int fd)
2721 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2724 /*********************************************************************
2725 * _tempnam (MSVCRT.@)
2727 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
2729 char tmpbuf[MAX_PATH];
2730 const char *tmp_dir = MSVCRT_getenv("TMP");
2732 if (tmp_dir) dir = tmp_dir;
2734 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2735 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2737 TRACE("got name (%s)\n",tmpbuf);
2738 DeleteFileA(tmpbuf);
2739 return MSVCRT__strdup(tmpbuf);
2741 TRACE("failed (%d)\n",GetLastError());
2745 /*********************************************************************
2746 * _wtempnam (MSVCRT.@)
2748 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2750 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
2751 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2752 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
2754 if (tmp_dir) dir = tmp_dir;
2756 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2757 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2759 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2760 DeleteFileW(tmpbuf);
2761 return MSVCRT__wcsdup(tmpbuf);
2763 TRACE("failed (%d)\n",GetLastError());
2767 /*********************************************************************
2770 int CDECL MSVCRT__umask(int umask)
2772 int old_umask = MSVCRT_umask;
2773 TRACE("(%d)\n",umask);
2774 MSVCRT_umask = umask;
2778 /*********************************************************************
2779 * _utime64 (MSVCRT.@)
2781 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2783 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2787 int retVal = _futime64(fd, t);
2794 /*********************************************************************
2795 * _utime32 (MSVCRT.@)
2797 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2801 struct MSVCRT___utimbuf64 t64;
2802 t64.actime = t->actime;
2803 t64.modtime = t->modtime;
2804 return _utime64( path, &t64 );
2807 return _utime64( path, NULL );
2810 /*********************************************************************
2814 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2816 return _utime64( path, t );
2819 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2821 return _utime32( path, t );
2825 /*********************************************************************
2826 * _wutime64 (MSVCRT.@)
2828 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2830 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2834 int retVal = _futime64(fd, t);
2841 /*********************************************************************
2842 * _wutime32 (MSVCRT.@)
2844 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2848 struct MSVCRT___utimbuf64 t64;
2849 t64.actime = t->actime;
2850 t64.modtime = t->modtime;
2851 return _wutime64( path, &t64 );
2854 return _wutime64( path, NULL );
2857 /*********************************************************************
2858 * _wutime (MSVCRT.@)
2861 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2863 return _wutime64( path, t );
2866 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2868 return _wutime32( path, t );
2872 /*********************************************************************
2875 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2878 ioinfo *info = msvcrt_get_ioinfo(fd);
2879 HANDLE hand = info->handle;
2881 /* Don't trace small writes, it gets *very* annoying */
2884 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2886 if (hand == INVALID_HANDLE_VALUE)
2888 *MSVCRT__errno() = MSVCRT_EBADF;
2892 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
2894 *MSVCRT__errno() = MSVCRT_EINVAL;
2898 /* If appending, go to EOF */
2899 if (info->wxflag & WX_APPEND)
2900 MSVCRT__lseek(fd, 0, FILE_END);
2902 if (!(info->wxflag & WX_TEXT))
2904 if (WriteFile(hand, buf, count, &num_written, NULL)
2905 && (num_written == count))
2907 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2908 hand, GetLastError());
2909 *MSVCRT__errno() = MSVCRT_ENOSPC;
2913 unsigned int i, j, nr_lf, size;
2916 const char *s = buf, *buf_start = buf;
2918 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
2920 /* find number of \n */
2921 for (nr_lf=0, i=0; i<count; i++)
2927 if ((q = p = MSVCRT_malloc(size)))
2929 for (s = buf, i = 0, j = 0; i < count; i++)
2938 FIXME("Malloc failed\n");
2950 else if (info->exflag & EF_UTF16)
2952 for (nr_lf=0, i=0; i<count; i+=2)
2953 if (s[i]=='\n' && s[i+1]==0)
2958 if ((q = p = MSVCRT_malloc(size)))
2960 for (s=buf, i=0, j=0; i<count; i++)
2962 if (s[i]=='\n' && s[i+1]==0)
2973 FIXME("Malloc failed\n");
2989 for(nr_lf=0, i=0; i<count; i+=2)
2990 if (s[i]=='\n' && s[i+1]==0)
2993 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
2995 msvcrt_set_errno(GetLastError());
3000 size = conv_len+nr_lf;
3001 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3003 for (s=buf, i=0, j=0; i<count; i++)
3005 if (s[i]=='\n' && s[i+1]==0)
3013 q = p+count+nr_lf*2;
3014 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3015 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3019 FIXME("Malloc failed\n");
3026 if (!WriteFile(hand, q, size, &num_written, NULL))
3030 if (num_written != size)
3032 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3033 fd, hand, GetLastError(), num_written);
3034 *MSVCRT__errno() = MSVCRT_ENOSPC;
3035 return s - buf_start;
3043 /*********************************************************************
3046 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3050 MSVCRT__lock_file(file);
3051 len = MSVCRT__write(file->_file, &val, sizeof(val));
3052 if (len == sizeof(val)) {
3053 MSVCRT__unlock_file(file);
3057 file->_flag |= MSVCRT__IOERR;
3058 MSVCRT__unlock_file(file);
3062 /*********************************************************************
3065 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3069 MSVCRT__lock_file(file);
3071 MSVCRT_free(file->_tmpfname);
3072 file->_tmpfname = NULL;
3073 /* flush stdio buffers */
3074 if(file->_flag & MSVCRT__IOWRT)
3075 MSVCRT_fflush(file);
3076 if(file->_flag & MSVCRT__IOMYBUF)
3077 MSVCRT_free(file->_base);
3079 r=MSVCRT__close(file->_file);
3082 MSVCRT__unlock_file(file);
3084 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3087 /*********************************************************************
3090 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3092 return file->_flag & MSVCRT__IOEOF;
3095 /*********************************************************************
3098 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3100 return file->_flag & MSVCRT__IOERR;
3103 /*********************************************************************
3104 * _filbuf (MSVCRT.@)
3106 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3109 MSVCRT__lock_file(file);
3111 /* Allocate buffer if needed */
3112 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
3113 msvcrt_alloc_buffer(file);
3115 if(!(file->_flag & MSVCRT__IOREAD)) {
3116 if(file->_flag & MSVCRT__IORW)
3117 file->_flag |= MSVCRT__IOREAD;
3119 MSVCRT__unlock_file(file);
3124 if(file->_flag & MSVCRT__IONBF) {
3126 if ((r = read_i(file->_file,&c,1)) != 1) {
3127 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3128 MSVCRT__unlock_file(file);
3132 MSVCRT__unlock_file(file);
3135 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
3137 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3139 MSVCRT__unlock_file(file);
3144 file->_ptr = file->_base+1;
3145 c = *(unsigned char *)file->_base;
3146 MSVCRT__unlock_file(file);
3151 /*********************************************************************
3154 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3159 MSVCRT__lock_file(file);
3162 i = (unsigned char *)file->_ptr++;
3165 j = MSVCRT__filbuf(file);
3167 MSVCRT__unlock_file(file);
3171 /*********************************************************************
3172 * _fgetchar (MSVCRT.@)
3174 int CDECL MSVCRT__fgetchar(void)
3176 return MSVCRT_fgetc(MSVCRT_stdin);
3179 /*********************************************************************
3182 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3184 int cc = MSVCRT_EOF;
3185 char * buf_start = s;
3187 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3188 file,file->_file,s,size);
3190 MSVCRT__lock_file(file);
3192 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
3197 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3199 TRACE(":nothing read\n");
3200 MSVCRT__unlock_file(file);
3203 if ((cc != MSVCRT_EOF) && (size > 1))
3206 TRACE(":got %s\n", debugstr_a(buf_start));
3207 MSVCRT__unlock_file(file);
3211 /*********************************************************************
3214 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
3215 * the CR from CR/LF combinations
3217 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3221 MSVCRT__lock_file(file);
3222 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
3229 for(i=0; i<sizeof(wc); i++)
3239 j = MSVCRT__filbuf(file);
3242 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3245 MSVCRT__unlock_file(file);
3252 MSVCRT__unlock_file(file);
3256 c = MSVCRT_fgetc(file);
3257 if ((get_locinfo()->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
3259 FIXME("Treat Multibyte characters\n");
3262 MSVCRT__unlock_file(file);
3263 if (c == MSVCRT_EOF)
3266 return (MSVCRT_wint_t)c;
3269 /*********************************************************************
3272 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3279 MSVCRT__lock_file(file);
3280 for (j=0; j<sizeof(int); j++) {
3281 k = MSVCRT_fgetc(file);
3282 if (k == MSVCRT_EOF) {
3283 file->_flag |= MSVCRT__IOEOF;
3284 MSVCRT__unlock_file(file);
3290 MSVCRT__unlock_file(file);
3294 /*********************************************************************
3297 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3299 return MSVCRT_fgetwc(file);
3302 /*********************************************************************
3303 * _fgetwchar (MSVCRT.@)
3305 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3307 return MSVCRT_fgetwc(MSVCRT_stdin);
3310 /*********************************************************************
3311 * getwchar (MSVCRT.@)
3313 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3315 return MSVCRT__fgetwchar();
3318 /*********************************************************************
3321 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3323 int cc = MSVCRT_WEOF;
3324 MSVCRT_wchar_t * buf_start = s;
3326 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3327 file,file->_file,s,size);
3329 MSVCRT__lock_file(file);
3331 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
3336 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3338 TRACE(":nothing read\n");
3339 MSVCRT__unlock_file(file);
3342 if ((cc != MSVCRT_WEOF) && (size > 1))
3345 TRACE(":got %s\n", debugstr_w(buf_start));
3346 MSVCRT__unlock_file(file);
3350 /*********************************************************************
3353 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3355 MSVCRT_size_t wrcnt=size * nmemb;
3360 MSVCRT__lock_file(file);
3362 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3363 memcpy(file->_ptr, ptr, pcnt);
3368 ptr = (const char*)ptr + pcnt;
3369 } else if(!(file->_flag & MSVCRT__IOWRT)) {
3370 if(file->_flag & MSVCRT__IORW) {
3371 file->_flag |= MSVCRT__IOWRT;
3373 MSVCRT__unlock_file(file);
3380 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
3381 msvcrt_alloc_buffer(file);
3384 res=msvcrt_flush_buffer(file);
3386 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
3389 file->_flag |= MSVCRT__IOERR;
3392 written += pwritten;
3396 MSVCRT__unlock_file(file);
3397 return written / size;
3400 /*********************************************************************
3403 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3405 MSVCRT_wchar_t mwc=wc;
3409 MSVCRT__lock_file(file);
3410 fdinfo = msvcrt_get_ioinfo(file->_file);
3412 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3413 char buf[MSVCRT_MB_LEN_MAX];
3416 char_len = MSVCRT_wctomb(buf, mwc);
3417 if(char_len!=-1 && MSVCRT_fwrite(buf, char_len, 1, file)==1)
3421 }else if(MSVCRT_fwrite(&mwc, sizeof(mwc), 1, file) == 1) {
3427 MSVCRT__unlock_file(file);
3431 /*********************************************************************
3432 * _fputwchar (MSVCRT.@)
3434 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3436 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3439 /*********************************************************************
3440 * _wfsopen (MSVCRT.@)
3442 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
3445 int open_flags, stream_flags, fd;
3447 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3449 /* map mode string to open() flags. "man fopen" for possibilities. */
3450 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3454 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3457 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3459 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3466 TRACE(":got (%p)\n",file);
3467 if (fd >= 0 && !file)
3473 /*********************************************************************
3474 * _fsopen (MSVCRT.@)
3476 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3479 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3481 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3482 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3483 *MSVCRT__errno() = MSVCRT_EINVAL;
3486 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3489 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3490 *MSVCRT__errno() = MSVCRT_EINVAL;
3494 ret = MSVCRT__wfsopen(pathW, modeW, share);
3501 /*********************************************************************
3504 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3506 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3509 /*********************************************************************
3510 * fopen_s (MSVCRT.@)
3512 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3513 const char *filename, const char *mode)
3515 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3516 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3517 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3519 *pFile = MSVCRT_fopen(filename, mode);
3522 return *MSVCRT__errno();
3526 /*********************************************************************
3527 * _wfopen (MSVCRT.@)
3529 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3531 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3534 /*********************************************************************
3535 * _wfopen_s (MSVCRT.@)
3537 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3538 const MSVCRT_wchar_t *mode)
3540 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3541 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3542 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3544 *pFile = MSVCRT__wfopen(filename, mode);
3547 return *MSVCRT__errno();
3551 /*********************************************************************
3552 * _flsbuf (MSVCRT.@)
3554 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3556 /* Flush output buffer */
3557 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3558 msvcrt_alloc_buffer(file);
3560 if(!(file->_flag & MSVCRT__IOWRT)) {
3561 if(file->_flag & MSVCRT__IORW)
3562 file->_flag |= MSVCRT__IOWRT;
3570 res = msvcrt_flush_buffer(file);
3574 res = msvcrt_flush_buffer(file);
3577 return res ? res : c&0xff;
3581 /* set _cnt to 0 for unbuffered FILEs */
3583 len = MSVCRT__write(file->_file, &cc, 1);
3586 file->_flag |= MSVCRT__IOERR;
3591 /*********************************************************************
3594 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3598 MSVCRT__lock_file(file);
3604 res = msvcrt_flush_buffer(file);
3605 MSVCRT__unlock_file(file);
3606 return res ? res : c;
3609 MSVCRT__unlock_file(file);
3613 res = MSVCRT__flsbuf(c, file);
3614 MSVCRT__unlock_file(file);
3619 /*********************************************************************
3620 * _fputchar (MSVCRT.@)
3622 int CDECL MSVCRT__fputchar(int c)
3624 return MSVCRT_fputc(c, MSVCRT_stdout);
3627 /*********************************************************************
3630 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3632 MSVCRT_size_t rcnt=size * nmemb;
3633 MSVCRT_size_t read=0;
3639 MSVCRT__lock_file(file);
3641 /* first buffered data */
3643 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3644 memcpy(ptr, file->_ptr, pcnt);
3649 ptr = (char*)ptr + pcnt;
3650 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3651 if(file->_flag & MSVCRT__IORW) {
3652 file->_flag |= MSVCRT__IOREAD;
3654 MSVCRT__unlock_file(file);
3661 /* Fill the buffer on small reads.
3662 * TODO: Use a better buffering strategy.
3664 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
3665 if (file->_bufsiz == 0) {
3666 msvcrt_alloc_buffer(file);
3668 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3669 file->_ptr = file->_base;
3670 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3671 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3672 if (i > 0 && i < file->_cnt) {
3673 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3674 file->_flag &= ~MSVCRT__IOEOF;
3677 memcpy(ptr, file->_ptr, i);
3682 i = MSVCRT__read(file->_file,ptr, rcnt);
3686 ptr = (char *)ptr+i;
3687 /* expose feof condition in the flags
3688 * MFC tests file->_flag for feof, and doesn't call feof())
3690 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3691 file->_flag |= MSVCRT__IOEOF;
3694 file->_flag |= MSVCRT__IOERR;
3701 MSVCRT__unlock_file(file);
3705 /*********************************************************************
3706 * _wfreopen (MSVCRT.@)
3709 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3713 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3716 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3720 MSVCRT_fclose(file);
3721 file = MSVCRT__wfsopen(path, mode, MSVCRT__SH_DENYNO);
3727 /*********************************************************************
3728 * _wfreopen_s (MSVCRT.@)
3730 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
3731 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3733 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3734 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
3735 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3736 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3738 *pFile = MSVCRT__wfreopen(path, mode, file);
3741 return *MSVCRT__errno();
3745 /*********************************************************************
3746 * freopen (MSVCRT.@)
3749 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3752 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3754 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3755 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3761 ret = MSVCRT__wfreopen(pathW, modeW, file);
3768 /*********************************************************************
3769 * freopen_s (MSVCRT.@)
3771 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
3772 const char *path, const char *mode, MSVCRT_FILE* file)
3774 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3775 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
3776 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3777 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
3779 *pFile = MSVCRT_freopen(path, mode, file);
3782 return *MSVCRT__errno();
3786 /*********************************************************************
3787 * fsetpos (MSVCRT.@)
3789 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3793 MSVCRT__lock_file(file);
3794 /* Note that all this has been lifted 'as is' from fseek */
3795 if(file->_flag & MSVCRT__IOWRT)
3796 msvcrt_flush_buffer(file);
3798 /* Discard buffered input */
3800 file->_ptr = file->_base;
3802 /* Reset direction of i/o */
3803 if(file->_flag & MSVCRT__IORW) {
3804 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3807 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3808 MSVCRT__unlock_file(file);
3812 /*********************************************************************
3813 * _ftelli64 (MSVCRT.@)
3815 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3819 MSVCRT__lock_file(file);
3820 pos = _telli64(file->_file);
3822 MSVCRT__unlock_file(file);
3826 if(file->_flag & MSVCRT__IOWRT) {
3827 pos += file->_ptr - file->_base;
3829 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3832 for(p=file->_base; p<file->_ptr; p++)
3836 } else if(!file->_cnt) { /* nothing to do */
3837 } else if(MSVCRT__lseeki64(file->_file, 0, SEEK_END)==pos) {
3841 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3842 for(i=0; i<file->_cnt; i++)
3843 if(file->_ptr[i] == '\n')
3849 if(MSVCRT__lseeki64(file->_file, pos, SEEK_SET) != pos) {
3850 MSVCRT__unlock_file(file);
3854 pos -= file->_bufsiz;
3855 pos += file->_ptr - file->_base;
3857 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3858 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READNL)
3861 for(p=file->_base; p<file->_ptr; p++)
3868 MSVCRT__unlock_file(file);
3872 /*********************************************************************
3875 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3877 return MSVCRT__ftelli64(file);
3880 /*********************************************************************
3881 * fgetpos (MSVCRT.@)
3883 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3885 *pos = MSVCRT__ftelli64(file);
3891 /*********************************************************************
3894 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3896 MSVCRT_size_t len = strlen(s);
3899 MSVCRT__lock_file(file);
3900 ret = MSVCRT_fwrite(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
3901 MSVCRT__unlock_file(file);
3905 /*********************************************************************
3908 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3910 MSVCRT_size_t i, len = strlenW(s);
3913 MSVCRT__lock_file(file);
3914 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3915 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3916 MSVCRT__unlock_file(file);
3919 for (i=0; i<len; i++) {
3920 if(MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
3921 MSVCRT__unlock_file(file);
3926 MSVCRT__unlock_file(file);
3930 /*********************************************************************
3931 * getchar (MSVCRT.@)
3933 int CDECL MSVCRT_getchar(void)
3935 return MSVCRT_fgetc(MSVCRT_stdin);
3938 /*********************************************************************
3941 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3943 return MSVCRT_fgetc(file);
3946 /*********************************************************************
3949 char * CDECL MSVCRT_gets(char *buf)
3952 char * buf_start = buf;
3954 MSVCRT__lock_file(MSVCRT_stdin);
3955 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3956 cc = MSVCRT_fgetc(MSVCRT_stdin))
3957 if(cc != '\r') *buf++ = (char)cc;
3961 TRACE("got '%s'\n", buf_start);
3962 MSVCRT__unlock_file(MSVCRT_stdin);
3966 /*********************************************************************
3969 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3972 MSVCRT_wchar_t* ws = buf;
3974 MSVCRT__lock_file(MSVCRT_stdin);
3975 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3976 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3979 *buf++ = (MSVCRT_wchar_t)cc;
3983 TRACE("got %s\n", debugstr_w(ws));
3984 MSVCRT__unlock_file(MSVCRT_stdin);
3988 /*********************************************************************
3991 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3993 return MSVCRT_fputc(c, file);
3996 /*********************************************************************
3997 * putchar (MSVCRT.@)
3999 int CDECL MSVCRT_putchar(int c)
4001 return MSVCRT_fputc(c, MSVCRT_stdout);
4004 /*********************************************************************
4005 * _putwch (MSVCRT.@)
4007 int CDECL MSVCRT__putwch(int c)
4009 return MSVCRT_fputwc(c, MSVCRT_stdout);
4012 /*********************************************************************
4015 int CDECL MSVCRT_puts(const char *s)
4017 MSVCRT_size_t len = strlen(s);
4020 MSVCRT__lock_file(MSVCRT_stdout);
4021 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4022 MSVCRT__unlock_file(MSVCRT_stdout);
4026 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4027 MSVCRT__unlock_file(MSVCRT_stdout);
4031 /*********************************************************************
4034 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4036 static const MSVCRT_wchar_t nl = '\n';
4037 MSVCRT_size_t len = strlenW(s);
4040 MSVCRT__lock_file(MSVCRT_stdout);
4041 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4042 MSVCRT__unlock_file(MSVCRT_stdout);
4046 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4047 MSVCRT__unlock_file(MSVCRT_stdout);
4051 /*********************************************************************
4054 int CDECL MSVCRT_remove(const char *path)
4056 TRACE("(%s)\n",path);
4057 if (DeleteFileA(path))
4059 TRACE(":failed (%d)\n",GetLastError());
4060 msvcrt_set_errno(GetLastError());
4064 /*********************************************************************
4065 * _wremove (MSVCRT.@)
4067 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4069 TRACE("(%s)\n",debugstr_w(path));
4070 if (DeleteFileW(path))
4072 TRACE(":failed (%d)\n",GetLastError());
4073 msvcrt_set_errno(GetLastError());
4077 /*********************************************************************
4080 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4082 TRACE(":from %s to %s\n",oldpath,newpath);
4083 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4085 TRACE(":failed (%d)\n",GetLastError());
4086 msvcrt_set_errno(GetLastError());
4090 /*********************************************************************
4091 * _wrename (MSVCRT.@)
4093 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4095 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4096 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4098 TRACE(":failed (%d)\n",GetLastError());
4099 msvcrt_set_errno(GetLastError());
4103 /*********************************************************************
4104 * setvbuf (MSVCRT.@)
4106 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4108 MSVCRT__lock_file(file);
4110 MSVCRT_free(file->_base);
4114 if(mode == MSVCRT__IOFBF) {
4115 file->_flag &= ~MSVCRT__IONBF;
4116 file->_base = file->_ptr = buf;
4118 file->_bufsiz = size;
4121 file->_flag |= MSVCRT__IONBF;
4123 MSVCRT__unlock_file(file);
4127 /*********************************************************************
4130 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4132 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4135 /*********************************************************************
4138 char * CDECL MSVCRT_tmpnam(char *s)
4145 thread_data_t *data = msvcrt_get_thread_data();
4147 if(!data->tmpnam_buffer)
4148 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4150 s = data->tmpnam_buffer;
4153 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4154 p = s + sprintf(s, "\\s%s.", tmpstr);
4155 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4157 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
4158 memcpy(p, tmpstr, size);
4160 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4161 GetLastError() == ERROR_FILE_NOT_FOUND)
4167 /*********************************************************************
4168 * _wtmpnam (MSVCRT.@)
4170 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
4172 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
4173 MSVCRT_wchar_t tmpstr[16];
4177 thread_data_t *data = msvcrt_get_thread_data();
4179 if(!data->wtmpnam_buffer)
4180 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4182 s = data->wtmpnam_buffer;
4185 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4186 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
4187 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4189 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
4190 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
4192 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4193 GetLastError() == ERROR_FILE_NOT_FOUND)
4199 /*********************************************************************
4200 * tmpfile (MSVCRT.@)
4202 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4204 char *filename = MSVCRT_tmpnam(NULL);
4206 MSVCRT_FILE* file = NULL;
4209 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4210 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4211 if (fd != -1 && (file = msvcrt_alloc_fp()))
4213 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4218 else file->_tmpfname = MSVCRT__strdup(filename);
4221 if(fd != -1 && !file)
4227 /*********************************************************************
4228 * tmpfile_s (MSVCRT.@)
4230 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4232 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4234 *file = MSVCRT_tmpfile();
4238 static int puts_clbk_file_a(void *file, int len, const char *str)
4240 return MSVCRT_fwrite(str, sizeof(char), len, file);
4243 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4247 MSVCRT__lock_file(file);
4249 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4250 ret = MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
4251 MSVCRT__unlock_file(file);
4255 for(i=0; i<len; i++) {
4256 if(MSVCRT_fputwc(str[i], file) == MSVCRT_WEOF) {
4257 MSVCRT__unlock_file(file);
4262 MSVCRT__unlock_file(file);
4266 /*********************************************************************
4267 * vfprintf (MSVCRT.@)
4269 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4273 MSVCRT__lock_file(file);
4274 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4275 MSVCRT__unlock_file(file);
4280 /*********************************************************************
4281 * vfprintf_s (MSVCRT.@)
4283 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4287 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4289 MSVCRT__lock_file(file);
4290 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4291 MSVCRT__unlock_file(file);
4296 /*********************************************************************
4297 * vfwprintf (MSVCRT.@)
4299 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4303 MSVCRT__lock_file(file);
4304 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4305 MSVCRT__unlock_file(file);
4310 /*********************************************************************
4311 * vfwprintf_s (MSVCRT.@)
4313 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4317 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4319 MSVCRT__lock_file(file);
4320 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4321 MSVCRT__unlock_file(file);
4326 /*********************************************************************
4327 * _vfwprintf_l (MSVCRT.@)
4329 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
4330 MSVCRT__locale_t locale, __ms_va_list valist)
4334 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4336 MSVCRT__lock_file(file);
4337 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4338 MSVCRT__unlock_file(file);
4343 /*********************************************************************
4344 * vprintf (MSVCRT.@)
4346 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
4348 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
4351 /*********************************************************************
4352 * vprintf_s (MSVCRT.@)
4354 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
4356 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
4359 /*********************************************************************
4360 * vwprintf (MSVCRT.@)
4362 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
4364 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
4367 /*********************************************************************
4368 * vwprintf_s (MSVCRT.@)
4370 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
4372 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
4375 /*********************************************************************
4376 * fprintf (MSVCRT.@)
4378 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
4380 __ms_va_list valist;
4382 __ms_va_start(valist, format);
4383 res = MSVCRT_vfprintf(file, format, valist);
4384 __ms_va_end(valist);
4388 /*********************************************************************
4389 * fprintf_s (MSVCRT.@)
4391 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
4393 __ms_va_list valist;
4395 __ms_va_start(valist, format);
4396 res = MSVCRT_vfprintf_s(file, format, valist);
4397 __ms_va_end(valist);
4401 /*********************************************************************
4402 * fwprintf (MSVCRT.@)
4404 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4406 __ms_va_list valist;
4408 __ms_va_start(valist, format);
4409 res = MSVCRT_vfwprintf(file, format, valist);
4410 __ms_va_end(valist);
4414 /*********************************************************************
4415 * fwprintf_s (MSVCRT.@)
4417 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4419 __ms_va_list valist;
4421 __ms_va_start(valist, format);
4422 res = MSVCRT_vfwprintf_s(file, format, valist);
4423 __ms_va_end(valist);
4427 /*********************************************************************
4428 * _fwprintf_l (MSVCRT.@)
4430 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
4432 __ms_va_list valist;
4434 __ms_va_start(valist, locale);
4435 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
4436 __ms_va_end(valist);
4440 /*********************************************************************
4443 int CDECL MSVCRT_printf(const char *format, ...)
4445 __ms_va_list valist;
4447 __ms_va_start(valist, format);
4448 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
4449 __ms_va_end(valist);
4453 /*********************************************************************
4454 * printf_s (MSVCRT.@)
4456 int CDECL MSVCRT_printf_s(const char *format, ...)
4458 __ms_va_list valist;
4460 __ms_va_start(valist, format);
4461 res = MSVCRT_vprintf_s(format, valist);
4462 __ms_va_end(valist);
4466 /*********************************************************************
4469 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
4471 if (c == MSVCRT_EOF)
4474 MSVCRT__lock_file(file);
4475 if(file->_bufsiz == 0) {
4476 msvcrt_alloc_buffer(file);
4479 if(file->_ptr>file->_base) {
4483 MSVCRT_clearerr(file);
4484 MSVCRT__unlock_file(file);
4488 MSVCRT__unlock_file(file);
4492 /*********************************************************************
4493 * ungetwc (MSVCRT.@)
4495 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
4497 MSVCRT_wchar_t mwc = wc;
4498 unsigned char * pp = (unsigned char *)&mwc;
4501 if (wc == MSVCRT_WEOF)
4504 MSVCRT__lock_file(file);
4505 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
4506 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
4507 MSVCRT__unlock_file(file);
4512 MSVCRT__unlock_file(file);
4516 /*********************************************************************
4517 * wprintf (MSVCRT.@)
4519 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
4521 __ms_va_list valist;
4523 __ms_va_start(valist, format);
4524 res = MSVCRT_vwprintf(format, valist);
4525 __ms_va_end(valist);
4529 /*********************************************************************
4530 * wprintf_s (MSVCRT.@)
4532 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
4534 __ms_va_list valist;
4536 __ms_va_start(valist, format);
4537 res = MSVCRT_vwprintf_s(format, valist);
4538 __ms_va_end(valist);
4542 /*********************************************************************
4543 * _getmaxstdio (MSVCRT.@)
4545 int CDECL MSVCRT__getmaxstdio(void)
4547 return MSVCRT_max_streams;
4550 /*********************************************************************
4551 * _setmaxstdio (MSVCRT.@)
4553 int CDECL MSVCRT__setmaxstdio(int newmax)
4555 TRACE("%d\n", newmax);
4557 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4560 MSVCRT_max_streams = newmax;
4561 return MSVCRT_max_streams;