2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
38 #include <sys/types.h>
45 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
51 /* for stat mode, permissions apply to all,owner and group */
52 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
53 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
54 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
56 /* _access() bit flags FIXME: incomplete */
57 #define MSVCRT_W_OK 0x02
58 #define MSVCRT_R_OK 0x04
60 /* values for wxflag in file descriptor */
63 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
64 #define WX_READCR 0x08 /* underlying file is at \r */
65 #define WX_DONTINHERIT 0x10
66 #define WX_APPEND 0x20
69 /* FIXME: this should be allocated dynamically */
70 #define MSVCRT_MAX_FILES 2048
75 DWORD unkn[7]; /* critical section and init flag */
78 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
80 MSVCRT_FILE MSVCRT__iob[3] = { { 0 } };
82 static int MSVCRT_fdstart = 3; /* first unallocated fd */
83 static int MSVCRT_fdend = 3; /* highest allocated fd */
85 static MSVCRT_FILE* MSVCRT_fstreams[2048];
86 static int MSVCRT_stream_idx;
88 /* INTERNAL: process umask */
89 static int MSVCRT_umask = 0;
91 /* INTERNAL: Static buffer for temp file name */
92 static char MSVCRT_tmpname[MAX_PATH];
94 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
95 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
96 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
97 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
99 #define TOUL(x) (ULONGLONG)(x)
100 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
101 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
102 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
103 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
105 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
106 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
107 * and MSVCRT_stream_idx, from race conditions.
108 * It doesn't protect against race conditions manipulating the underlying files
109 * or flags; doing so would probably be better accomplished with per-file
110 * protection, rather than locking the whole table for every change.
112 static CRITICAL_SECTION MSVCRT_file_cs;
113 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
114 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
116 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
118 buf->st_dev = buf64->st_dev;
119 buf->st_ino = buf64->st_ino;
120 buf->st_mode = buf64->st_mode;
121 buf->st_nlink = buf64->st_nlink;
122 buf->st_uid = buf64->st_uid;
123 buf->st_gid = buf64->st_gid;
124 buf->st_rdev = buf64->st_rdev;
125 buf->st_size = buf64->st_size;
126 buf->st_atime = buf64->st_atime;
127 buf->st_mtime = buf64->st_mtime;
128 buf->st_ctime = buf64->st_ctime;
131 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
133 buf->st_dev = buf64->st_dev;
134 buf->st_ino = buf64->st_ino;
135 buf->st_mode = buf64->st_mode;
136 buf->st_nlink = buf64->st_nlink;
137 buf->st_uid = buf64->st_uid;
138 buf->st_gid = buf64->st_gid;
139 buf->st_rdev = buf64->st_rdev;
140 buf->st_size = buf64->st_size;
141 buf->st_atime = buf64->st_atime;
142 buf->st_mtime = buf64->st_mtime;
143 buf->st_ctime = buf64->st_ctime;
146 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
148 /* 1601 to 1970 is 369 years plus 89 leap days */
149 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
151 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
152 ft->dwHighDateTime = ticks >> 32;
153 ft->dwLowDateTime = ticks;
156 static inline BOOL msvcrt_is_valid_fd(int fd)
158 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
161 /* INTERNAL: Get the HANDLE for a fd
162 * This doesn't lock the table, because a failure will result in
163 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
164 * it returns a valid handle which is about to be closed, a subsequent call
165 * will fail, most likely in a sane way.
167 static HANDLE msvcrt_fdtoh(int fd)
169 if (!msvcrt_is_valid_fd(fd))
171 WARN(":fd (%d) - no handle!\n",fd);
172 *MSVCRT___doserrno() = 0;
173 *MSVCRT__errno() = MSVCRT_EBADF;
174 return INVALID_HANDLE_VALUE;
176 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
177 return MSVCRT_fdesc[fd].handle;
180 /* INTERNAL: free a file entry fd */
181 static void msvcrt_free_fd(int fd)
186 old_handle = MSVCRT_fdesc[fd].handle;
187 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
188 MSVCRT_fdesc[fd].wxflag = 0;
189 TRACE(":fd (%d) freed\n",fd);
190 if (fd < 3) /* don't use 0,1,2 for user files */
195 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
198 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
201 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
207 if (fd == MSVCRT_fdend - 1)
209 if (fd < MSVCRT_fdstart)
215 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
216 /* caller must hold the files lock */
217 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
219 if (fd >= MSVCRT_MAX_FILES)
221 WARN(":files exhausted!\n");
222 *MSVCRT__errno() = MSVCRT_ENFILE;
225 MSVCRT_fdesc[fd].handle = hand;
226 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
228 /* locate next free slot */
229 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
230 MSVCRT_fdstart = MSVCRT_fdend + 1;
232 while (MSVCRT_fdstart < MSVCRT_fdend &&
233 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
235 /* update last fd in use */
236 if (fd >= MSVCRT_fdend)
237 MSVCRT_fdend = fd + 1;
238 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
242 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
243 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
244 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
250 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
251 static int msvcrt_alloc_fd(HANDLE hand, int flag)
256 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
257 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
262 /* INTERNAL: Allocate a FILE* for an fd slot */
263 /* caller must hold the files lock */
264 static MSVCRT_FILE* msvcrt_alloc_fp(void)
268 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
270 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
272 if (!MSVCRT_fstreams[i])
274 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
276 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
278 return MSVCRT_fstreams[i];
284 /* INTERNAL: initialize a FILE* from an open fd */
285 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
287 TRACE(":fd (%d) allocating FILE*\n",fd);
288 if (!msvcrt_is_valid_fd(fd))
290 WARN(":invalid fd %d\n",fd);
291 *MSVCRT___doserrno() = 0;
292 *MSVCRT__errno() = MSVCRT_EBADF;
295 memset(file, 0, sizeof(*file));
297 file->_flag = stream_flags;
299 TRACE(":got FILE* (%p)\n",file);
303 /* INTERNAL: Create an inheritance data block (for spawned process)
304 * The inheritance block is made of:
305 * 00 int nb of file descriptor (NBFD)
306 * 04 char file flags (wxflag): repeated for each fd
307 * 4+NBFD HANDLE file handle: repeated for each fd
309 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
315 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
316 *block = MSVCRT_calloc(*size, 1);
322 wxflag_ptr = (char*)*block + sizeof(unsigned);
323 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
325 *(unsigned*)*block = MSVCRT_fdend;
326 for (fd = 0; fd < MSVCRT_fdend; fd++)
328 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
329 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
331 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
332 *handle_ptr = MSVCRT_fdesc[fd].handle;
337 *handle_ptr = INVALID_HANDLE_VALUE;
339 wxflag_ptr++; handle_ptr++;
344 /* INTERNAL: Set up all file descriptors,
345 * as well as default streams (stdin, stderr and stdout)
347 void msvcrt_init_io(void)
352 InitializeCriticalSection(&MSVCRT_file_cs);
353 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
354 GetStartupInfoA(&si);
355 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
361 count = *(unsigned*)si.lpReserved2;
362 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
363 handle_ptr = (HANDLE*)(wxflag_ptr + count);
365 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
366 count = min(count, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
367 for (i = 0; i < count; i++)
369 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
371 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
372 MSVCRT_fdesc[i].handle = *handle_ptr;
376 MSVCRT_fdesc[i].wxflag = 0;
377 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
379 wxflag_ptr++; handle_ptr++;
381 MSVCRT_fdend = max( 3, count );
382 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
383 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
386 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
388 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
389 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
390 GetCurrentProcess(), &MSVCRT_fdesc[0].handle,
391 0, TRUE, DUPLICATE_SAME_ACCESS))
392 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
394 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
396 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
397 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
398 GetCurrentProcess(), &MSVCRT_fdesc[1].handle,
399 0, TRUE, DUPLICATE_SAME_ACCESS))
400 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
402 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
404 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
405 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
406 GetCurrentProcess(), &MSVCRT_fdesc[2].handle,
407 0, TRUE, DUPLICATE_SAME_ACCESS))
408 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
411 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
412 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
414 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
415 for (i = 0; i < 3; i++)
417 /* FILE structs for stdin/out/err are static and never deleted */
418 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
419 MSVCRT__iob[i]._file = i;
420 MSVCRT__iob[i]._tmpfname = NULL;
421 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
423 MSVCRT_stream_idx = 3;
426 /* INTERNAL: Flush stdio file buffer */
427 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
430 int cnt=file->_ptr-file->_base;
431 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
432 file->_flag |= MSVCRT__IOERR;
435 file->_ptr=file->_base;
436 file->_cnt=file->_bufsiz;
441 /* INTERNAL: Allocate stdio file buffer */
442 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
444 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
446 file->_bufsiz = MSVCRT_BUFSIZ;
447 file->_flag |= MSVCRT__IOMYBUF;
449 file->_base = (char*)(&file->_charbuf);
451 file->_bufsiz = sizeof(file->_charbuf);
453 file->_ptr = file->_base;
457 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
458 static void msvcrt_int_to_base32(int num, char *str)
473 *p = (num & 31) + '0';
475 *p += ('a' - '0' - 10);
480 /*********************************************************************
481 * __iob_func(MSVCRT.@)
483 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
485 return &MSVCRT__iob[0];
488 /*********************************************************************
491 int CDECL MSVCRT__access(const char *filename, int mode)
493 DWORD attr = GetFileAttributesA(filename);
495 TRACE("(%s,%d) %d\n",filename,mode,attr);
497 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
499 msvcrt_set_errno(GetLastError());
502 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
504 msvcrt_set_errno(ERROR_ACCESS_DENIED);
510 /*********************************************************************
511 * _access_s (MSVCRT.@)
513 int CDECL _access_s(const char *filename, int mode)
515 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
516 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
518 *MSVCRT__errno() = MSVCRT_EINVAL;
522 return MSVCRT__access(filename, mode);
525 /*********************************************************************
526 * _waccess (MSVCRT.@)
528 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
530 DWORD attr = GetFileAttributesW(filename);
532 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
534 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
536 msvcrt_set_errno(GetLastError());
539 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
541 msvcrt_set_errno(ERROR_ACCESS_DENIED);
547 /*********************************************************************
548 * _waccess_s (MSVCRT.@)
550 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
552 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
553 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
555 *MSVCRT__errno() = MSVCRT_EINVAL;
559 return _waccess(filename, mode);
562 /*********************************************************************
565 int CDECL MSVCRT__chmod(const char *path, int flags)
567 DWORD oldFlags = GetFileAttributesA(path);
569 if (oldFlags != INVALID_FILE_ATTRIBUTES)
571 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
572 oldFlags | FILE_ATTRIBUTE_READONLY;
574 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
577 msvcrt_set_errno(GetLastError());
581 /*********************************************************************
584 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
586 DWORD oldFlags = GetFileAttributesW(path);
588 if (oldFlags != INVALID_FILE_ATTRIBUTES)
590 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
591 oldFlags | FILE_ATTRIBUTE_READONLY;
593 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
596 msvcrt_set_errno(GetLastError());
600 /*********************************************************************
603 int CDECL MSVCRT__unlink(const char *path)
605 TRACE("%s\n",debugstr_a(path));
606 if(DeleteFileA(path))
608 TRACE("failed (%d)\n",GetLastError());
609 msvcrt_set_errno(GetLastError());
613 /*********************************************************************
614 * _wunlink (MSVCRT.@)
616 int CDECL _wunlink(const MSVCRT_wchar_t *path)
618 TRACE("(%s)\n",debugstr_w(path));
619 if(DeleteFileW(path))
621 TRACE("failed (%d)\n",GetLastError());
622 msvcrt_set_errno(GetLastError());
626 /* _flushall calls MSVCRT_fflush which calls _flushall */
627 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
629 /*********************************************************************
630 * _flushall (MSVCRT.@)
632 int CDECL _flushall(void)
634 int i, num_flushed = 0;
637 for (i = 3; i < MSVCRT_stream_idx; i++)
638 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
641 /* FIXME: flush, do not commit */
642 if (_commit(i) == -1)
643 if (MSVCRT_fstreams[i])
644 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
646 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
647 MSVCRT_fflush(MSVCRT_fstreams[i]);
653 TRACE(":flushed (%d) handles\n",num_flushed);
657 /*********************************************************************
660 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
664 } else if(file->_flag & MSVCRT__IOWRT) {
665 int res=msvcrt_flush_buffer(file);
671 /*********************************************************************
674 int CDECL MSVCRT__close(int fd)
680 hand = msvcrt_fdtoh(fd);
681 TRACE(":fd (%d) handle (%p)\n",fd,hand);
682 if (hand == INVALID_HANDLE_VALUE)
684 else if (!CloseHandle(hand))
686 WARN(":failed-last error (%d)\n",GetLastError());
687 msvcrt_set_errno(GetLastError());
700 /*********************************************************************
703 int CDECL _commit(int fd)
705 HANDLE hand = msvcrt_fdtoh(fd);
707 TRACE(":fd (%d) handle (%p)\n",fd,hand);
708 if (hand == INVALID_HANDLE_VALUE)
711 if (!FlushFileBuffers(hand))
713 if (GetLastError() == ERROR_INVALID_HANDLE)
715 /* FlushFileBuffers fails for console handles
716 * so we ignore this error.
720 TRACE(":failed-last error (%d)\n",GetLastError());
721 msvcrt_set_errno(GetLastError());
728 /*********************************************************************
731 * MSDN isn't clear on this point, but the remarks for _pipe
732 * indicate file descriptors duplicated with _dup and _dup2 are always
735 int CDECL MSVCRT__dup2(int od, int nd)
739 TRACE("(od=%d, nd=%d)\n", od, nd);
741 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
745 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
746 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
748 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
750 if (msvcrt_is_valid_fd(nd))
752 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
756 *MSVCRT__errno() = MSVCRT_EMFILE;
760 /* _dup2 returns 0, not nd, on success */
767 msvcrt_set_errno(GetLastError());
772 *MSVCRT__errno() = MSVCRT_EBADF;
779 /*********************************************************************
782 int CDECL MSVCRT__dup(int od)
788 if (MSVCRT__dup2(od, fd) == 0)
796 /*********************************************************************
799 int CDECL _eof(int fd)
802 LONG hcurpos,hendpos;
803 HANDLE hand = msvcrt_fdtoh(fd);
805 TRACE(":fd (%d) handle (%p)\n",fd,hand);
807 if (hand == INVALID_HANDLE_VALUE)
810 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
812 /* Otherwise we do it the hard way */
813 hcurpos = hendpos = 0;
814 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
815 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
817 if (curpos == endpos && hcurpos == hendpos)
819 /* FIXME: shouldn't WX_ATEOF be set here? */
823 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
827 /*********************************************************************
828 * _fcloseall (MSVCRT.@)
830 int CDECL MSVCRT__fcloseall(void)
832 int num_closed = 0, i;
835 for (i = 3; i < MSVCRT_stream_idx; i++)
836 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
837 !MSVCRT_fclose(MSVCRT_fstreams[i]))
841 TRACE(":closed (%d) handles\n",num_closed);
845 /* free everything on process exit */
846 void msvcrt_free_io(void)
849 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
850 * stdout, and stderr (unlike GNU), so we need to fclose() them here
851 * or they won't get flushed.
853 MSVCRT_fclose(&MSVCRT__iob[0]);
854 MSVCRT_fclose(&MSVCRT__iob[1]);
855 MSVCRT_fclose(&MSVCRT__iob[2]);
856 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
857 DeleteCriticalSection(&MSVCRT_file_cs);
860 /*********************************************************************
861 * _lseeki64 (MSVCRT.@)
863 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
865 HANDLE hand = msvcrt_fdtoh(fd);
868 TRACE(":fd (%d) handle (%p)\n",fd,hand);
869 if (hand == INVALID_HANDLE_VALUE)
872 if (whence < 0 || whence > 2)
874 *MSVCRT__errno() = MSVCRT_EINVAL;
878 TRACE(":fd (%d) to %s pos %s\n",
879 fd,wine_dbgstr_longlong(offset),
880 (whence==SEEK_SET)?"SEEK_SET":
881 (whence==SEEK_CUR)?"SEEK_CUR":
882 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
884 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
885 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
886 ofs.QuadPart = offset;
887 if ((ofs.LowPart = SetFilePointer(hand, ofs.LowPart, &ofs.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
888 GetLastError() == ERROR_SUCCESS)
890 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
891 /* FIXME: What if we seek _to_ EOF - is EOF set? */
895 TRACE(":error-last error (%d)\n",GetLastError());
896 msvcrt_set_errno(GetLastError());
900 /*********************************************************************
903 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
905 return MSVCRT__lseeki64(fd, offset, whence);
908 /*********************************************************************
909 * _lock_file (MSVCRT.@)
911 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
913 FIXME("(%p) stub\n",file);
916 /*********************************************************************
917 * _unlock_file (MSVCRT.@)
919 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
921 FIXME("(%p) stub\n",file);
924 /*********************************************************************
925 * _locking (MSVCRT.@)
927 * This is untested; the underlying LockFile doesn't work yet.
929 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
933 HANDLE hand = msvcrt_fdtoh(fd);
935 TRACE(":fd (%d) handle (%p)\n",fd,hand);
936 if (hand == INVALID_HANDLE_VALUE)
939 if (mode < 0 || mode > 4)
941 *MSVCRT__errno() = MSVCRT_EINVAL;
945 TRACE(":fd (%d) by 0x%08x mode %s\n",
946 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
947 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
948 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
949 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
950 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
953 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
955 FIXME ("Seek failed\n");
956 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
959 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
962 ret = 1; /* just to satisfy gcc */
965 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
970 else if (mode == MSVCRT__LK_UNLCK)
971 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
973 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
974 /* FIXME - what about error settings? */
978 /*********************************************************************
979 * _fseeki64 (MSVCRT.@)
981 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
983 /* Flush output if needed */
984 if(file->_flag & MSVCRT__IOWRT)
985 msvcrt_flush_buffer(file);
987 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
988 offset -= file->_cnt;
989 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
990 /* Black magic correction for CR removal */
992 for (i=0; i<file->_cnt; i++) {
993 if (file->_ptr[i] == '\n')
996 /* Black magic when reading CR at buffer boundary*/
997 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
1001 /* Discard buffered input */
1003 file->_ptr = file->_base;
1004 /* Reset direction of i/o */
1005 if(file->_flag & MSVCRT__IORW) {
1006 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1008 /* Clear end of file flag */
1009 file->_flag &= ~MSVCRT__IOEOF;
1010 return (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1013 /*********************************************************************
1016 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1018 return MSVCRT__fseeki64( file, offset, whence );
1021 /*********************************************************************
1022 * _chsize (MSVCRT.@)
1024 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1030 TRACE("(fd=%d, size=%d)\n", fd, size);
1034 handle = msvcrt_fdtoh(fd);
1035 if (handle != INVALID_HANDLE_VALUE)
1037 /* save the current file pointer */
1038 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1041 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1044 ret = SetEndOfFile(handle);
1045 if (!ret) msvcrt_set_errno(GetLastError());
1048 /* restore the file pointer */
1049 MSVCRT__lseek(fd, cur, SEEK_SET);
1054 return ret ? 0 : -1;
1057 /*********************************************************************
1058 * clearerr (MSVCRT.@)
1060 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1062 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1063 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1066 /*********************************************************************
1069 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1071 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1072 MSVCRT_fseek(file, 0L, SEEK_SET);
1073 MSVCRT_clearerr(file);
1076 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1078 int plus = strchrW(mode, '+') != NULL;
1083 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1084 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1087 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1088 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1091 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1092 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1095 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1096 *MSVCRT__errno() = MSVCRT_EINVAL;
1104 *open_flags |= MSVCRT__O_BINARY;
1105 *open_flags &= ~MSVCRT__O_TEXT;
1108 *open_flags |= MSVCRT__O_TEXT;
1109 *open_flags &= ~MSVCRT__O_BINARY;
1115 FIXME(":unknown flag %c not supported\n",mode[-1]);
1120 /*********************************************************************
1121 * _fdopen (MSVCRT.@)
1123 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1126 MSVCRT_wchar_t *modeW = NULL;
1128 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1130 ret = MSVCRT__wfdopen(fd, modeW);
1136 /*********************************************************************
1137 * _wfdopen (MSVCRT.@)
1139 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1141 int open_flags, stream_flags;
1144 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1147 if (!(file = msvcrt_alloc_fp()))
1149 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1154 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1160 /*********************************************************************
1161 * _filelength (MSVCRT.@)
1163 LONG CDECL MSVCRT__filelength(int fd)
1165 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1168 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1171 if (endPos != curPos)
1172 MSVCRT__lseek(fd, curPos, SEEK_SET);
1179 /*********************************************************************
1180 * _filelengthi64 (MSVCRT.@)
1182 __int64 CDECL MSVCRT__filelengthi64(int fd)
1184 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1187 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1190 if (endPos != curPos)
1191 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1198 /*********************************************************************
1199 * _fileno (MSVCRT.@)
1201 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1203 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1207 /*********************************************************************
1208 * _fstat64 (MSVCRT.@)
1210 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1214 BY_HANDLE_FILE_INFORMATION hfi;
1215 HANDLE hand = msvcrt_fdtoh(fd);
1217 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1218 if (hand == INVALID_HANDLE_VALUE)
1223 WARN(":failed-NULL buf\n");
1224 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1228 memset(&hfi, 0, sizeof(hfi));
1229 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1230 type = GetFileType(hand);
1231 if (type == FILE_TYPE_PIPE)
1233 buf->st_dev = buf->st_rdev = fd;
1234 buf->st_mode = S_IFIFO;
1237 else if (type == FILE_TYPE_CHAR)
1239 buf->st_dev = buf->st_rdev = fd;
1240 buf->st_mode = S_IFCHR;
1243 else /* FILE_TYPE_DISK etc. */
1245 if (!GetFileInformationByHandle(hand, &hfi))
1247 WARN(":failed-last error (%d)\n",GetLastError());
1248 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1251 buf->st_mode = S_IFREG | 0444;
1252 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1253 buf->st_mode |= 0222;
1254 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1255 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1257 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1258 buf->st_mtime = buf->st_ctime = dw;
1259 buf->st_nlink = hfi.nNumberOfLinks;
1261 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1266 /*********************************************************************
1267 * _fstati64 (MSVCRT.@)
1269 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1272 struct MSVCRT__stat64 buf64;
1274 ret = MSVCRT__fstat64(fd, &buf64);
1276 msvcrt_stat64_to_stati64(&buf64, buf);
1280 /*********************************************************************
1283 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1285 struct MSVCRT__stat64 buf64;
1287 ret = MSVCRT__fstat64(fd, &buf64);
1289 msvcrt_stat64_to_stat(&buf64, buf);
1293 /*********************************************************************
1294 * _futime64 (MSVCRT.@)
1296 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1298 HANDLE hand = msvcrt_fdtoh(fd);
1303 time_to_filetime( MSVCRT__time64(NULL), &at );
1308 time_to_filetime( t->actime, &at );
1309 time_to_filetime( t->modtime, &wt );
1312 if (!SetFileTime(hand, NULL, &at, &wt))
1314 msvcrt_set_errno(GetLastError());
1320 /*********************************************************************
1321 * _futime32 (MSVCRT.@)
1323 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1325 struct MSVCRT___utimbuf64 t64;
1326 t64.actime = t->actime;
1327 t64.modtime = t->modtime;
1328 return _futime64( fd, &t64 );
1331 /*********************************************************************
1332 * _futime (MSVCRT.@)
1335 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1337 return _futime64( fd, t );
1340 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1342 return _futime32( fd, t );
1346 /*********************************************************************
1347 * _get_osfhandle (MSVCRT.@)
1349 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1351 HANDLE hand = msvcrt_fdtoh(fd);
1352 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1354 return (MSVCRT_intptr_t)hand;
1357 /*********************************************************************
1358 * _isatty (MSVCRT.@)
1360 int CDECL _isatty(int fd)
1362 HANDLE hand = msvcrt_fdtoh(fd);
1364 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1365 if (hand == INVALID_HANDLE_VALUE)
1368 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1371 /*********************************************************************
1372 * _mktemp (MSVCRT.@)
1374 char * CDECL _mktemp(char *pattern)
1377 char *retVal = pattern;
1382 numX = (*pattern++ == 'X')? numX + 1 : 0;
1386 id = GetCurrentProcessId();
1390 int tempNum = id / 10;
1391 *pattern-- = id - (tempNum * 10) + '0';
1397 *pattern = letter++;
1398 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1399 GetLastError() == ERROR_FILE_NOT_FOUND)
1401 } while(letter <= 'z');
1405 /*********************************************************************
1406 * _wmktemp (MSVCRT.@)
1408 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1411 MSVCRT_wchar_t *retVal = pattern;
1413 MSVCRT_wchar_t letter = 'a';
1416 numX = (*pattern++ == 'X')? numX + 1 : 0;
1420 id = GetCurrentProcessId();
1424 int tempNum = id / 10;
1425 *pattern-- = id - (tempNum * 10) + '0';
1431 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1432 GetLastError() == ERROR_FILE_NOT_FOUND)
1434 *pattern = letter++;
1435 } while(letter != '|');
1439 static unsigned split_oflags(unsigned oflags)
1442 unsigned unsupp; /* until we support everything */
1444 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1445 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1446 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1447 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1448 else wxflags |= WX_TEXT; /* default to TEXT*/
1449 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1451 if ((unsupp = oflags & ~(
1452 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1453 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1454 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1455 MSVCRT__O_NOINHERIT|
1456 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1458 ERR(":unsupported oflags 0x%04x\n",unsupp);
1463 /*********************************************************************
1466 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1469 SECURITY_ATTRIBUTES sa;
1470 HANDLE readHandle, writeHandle;
1474 *MSVCRT__errno() = MSVCRT_EINVAL;
1478 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1479 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1480 sa.lpSecurityDescriptor = NULL;
1481 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1483 unsigned int wxflags = split_oflags(textmode);
1487 fd = msvcrt_alloc_fd(readHandle, wxflags);
1491 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1499 MSVCRT__close(pfds[0]);
1500 CloseHandle(writeHandle);
1501 *MSVCRT__errno() = MSVCRT_EMFILE;
1506 CloseHandle(readHandle);
1507 CloseHandle(writeHandle);
1508 *MSVCRT__errno() = MSVCRT_EMFILE;
1513 msvcrt_set_errno(GetLastError());
1518 /*********************************************************************
1519 * _sopen_s (MSVCRT.@)
1521 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1523 DWORD access = 0, creation = 0, attrib;
1527 SECURITY_ATTRIBUTES sa;
1529 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1530 fd, path, oflags, shflags, pmode);
1534 MSVCRT_INVALID_PMT("null out fd pointer");
1535 *MSVCRT__errno() = MSVCRT_EINVAL;
1536 return MSVCRT_EINVAL;
1540 wxflag = split_oflags(oflags);
1541 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1543 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1544 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1545 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1548 if (oflags & MSVCRT__O_CREAT)
1550 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1551 FIXME(": pmode 0x%04x ignored\n", pmode);
1553 WARN(": pmode 0x%04x ignored\n", pmode);
1555 if (oflags & MSVCRT__O_EXCL)
1556 creation = CREATE_NEW;
1557 else if (oflags & MSVCRT__O_TRUNC)
1558 creation = CREATE_ALWAYS;
1560 creation = OPEN_ALWAYS;
1562 else /* no MSVCRT__O_CREAT */
1564 if (oflags & MSVCRT__O_TRUNC)
1565 creation = TRUNCATE_EXISTING;
1567 creation = OPEN_EXISTING;
1572 case MSVCRT__SH_DENYRW:
1575 case MSVCRT__SH_DENYWR:
1576 sharing = FILE_SHARE_READ;
1578 case MSVCRT__SH_DENYRD:
1579 sharing = FILE_SHARE_WRITE;
1581 case MSVCRT__SH_DENYNO:
1582 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1585 ERR( "Unhandled shflags 0x%x\n", shflags );
1586 return MSVCRT_EINVAL;
1588 attrib = FILE_ATTRIBUTE_NORMAL;
1590 if (oflags & MSVCRT__O_TEMPORARY)
1592 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1594 sharing |= FILE_SHARE_DELETE;
1597 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1598 sa.lpSecurityDescriptor = NULL;
1599 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1601 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1602 if (hand == INVALID_HANDLE_VALUE) {
1603 WARN(":failed-last error (%d)\n", GetLastError());
1604 msvcrt_set_errno(GetLastError());
1605 return *MSVCRT__errno();
1608 *fd = msvcrt_alloc_fd(hand, wxflag);
1610 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1614 /*********************************************************************
1617 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1622 if (oflags & MSVCRT__O_CREAT)
1626 __ms_va_start(ap, shflags);
1627 pmode = va_arg(ap, int);
1633 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
1637 /*********************************************************************
1638 * _wsopen_s (MSVCRT.@)
1640 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1642 DWORD access = 0, creation = 0, attrib;
1643 SECURITY_ATTRIBUTES sa;
1648 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1649 fd, debugstr_w(path), oflags, shflags, pmode);
1653 MSVCRT_INVALID_PMT("null out fd pointer");
1654 *MSVCRT__errno() = MSVCRT_EINVAL;
1655 return MSVCRT_EINVAL;
1659 wxflag = split_oflags(oflags);
1660 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1662 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1663 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1664 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1667 if (oflags & MSVCRT__O_CREAT)
1669 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1670 FIXME(": pmode 0x%04x ignored\n", pmode);
1672 WARN(": pmode 0x%04x ignored\n", pmode);
1674 if (oflags & MSVCRT__O_EXCL)
1675 creation = CREATE_NEW;
1676 else if (oflags & MSVCRT__O_TRUNC)
1677 creation = CREATE_ALWAYS;
1679 creation = OPEN_ALWAYS;
1681 else /* no MSVCRT__O_CREAT */
1683 if (oflags & MSVCRT__O_TRUNC)
1684 creation = TRUNCATE_EXISTING;
1686 creation = OPEN_EXISTING;
1691 case MSVCRT__SH_DENYRW:
1694 case MSVCRT__SH_DENYWR:
1695 sharing = FILE_SHARE_READ;
1697 case MSVCRT__SH_DENYRD:
1698 sharing = FILE_SHARE_WRITE;
1700 case MSVCRT__SH_DENYNO:
1701 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1704 ERR( "Unhandled shflags 0x%x\n", shflags );
1705 return MSVCRT_EINVAL;
1707 attrib = FILE_ATTRIBUTE_NORMAL;
1709 if (oflags & MSVCRT__O_TEMPORARY)
1711 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1713 sharing |= FILE_SHARE_DELETE;
1716 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1717 sa.lpSecurityDescriptor = NULL;
1718 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1720 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1722 if (hand == INVALID_HANDLE_VALUE) {
1723 WARN(":failed-last error (%d)\n",GetLastError());
1724 msvcrt_set_errno(GetLastError());
1725 msvcrt_set_errno(GetLastError());
1726 return *MSVCRT__errno();
1729 *fd = msvcrt_alloc_fd(hand, wxflag);
1731 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1735 /*********************************************************************
1736 * _wsopen (MSVCRT.@)
1738 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1743 if (oflags & MSVCRT__O_CREAT)
1747 __ms_va_start(ap, shflags);
1748 pmode = va_arg(ap, int);
1754 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1758 /*********************************************************************
1761 int CDECL MSVCRT__open( const char *path, int flags, ... )
1765 if (flags & MSVCRT__O_CREAT)
1768 __ms_va_start(ap, flags);
1769 pmode = va_arg(ap, int);
1771 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1774 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1777 /*********************************************************************
1780 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1784 if (flags & MSVCRT__O_CREAT)
1787 __ms_va_start(ap, flags);
1788 pmode = va_arg(ap, int);
1790 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1793 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1796 /*********************************************************************
1799 int CDECL MSVCRT__creat(const char *path, int flags)
1801 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1802 return MSVCRT__open(path, usedFlags);
1805 /*********************************************************************
1806 * _wcreat (MSVCRT.@)
1808 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1810 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1811 return _wopen(path, usedFlags);
1814 /*********************************************************************
1815 * _open_osfhandle (MSVCRT.@)
1817 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1821 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1822 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1823 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1824 * text - it never sets MSVCRT__O_BINARY.
1826 /* don't let split_oflags() decide the mode if no mode is passed */
1827 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1828 oflags |= MSVCRT__O_BINARY;
1830 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1831 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1835 /*********************************************************************
1838 int CDECL _rmtmp(void)
1840 int num_removed = 0, i;
1843 for (i = 3; i < MSVCRT_stream_idx; i++)
1844 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1846 MSVCRT_fclose(MSVCRT_fstreams[i]);
1852 TRACE(":removed (%d) temp files\n",num_removed);
1856 /*********************************************************************
1859 * When reading \r as last character in text mode, read() positions
1860 * the file pointer on the \r character while getc() goes on to
1863 static int read_i(int fd, void *buf, unsigned int count)
1866 char *bufstart = buf;
1867 HANDLE hand = msvcrt_fdtoh(fd);
1872 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1873 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1874 TRACE("already at EOF, returning 0\n");
1877 /* Don't trace small reads, it gets *very* annoying */
1879 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1880 if (hand == INVALID_HANDLE_VALUE)
1883 /* Reading single bytes in O_TEXT mode makes things slow
1884 * So read big chunks
1886 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1888 if (count != 0 && num_read == 0)
1890 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1891 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1893 else if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1896 if (bufstart[num_read-1] == '\r')
1900 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1901 ReadFile(hand, bufstart, 1, &num_read, NULL);
1905 MSVCRT_fdesc[fd].wxflag |= WX_READCR;
1910 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1911 for (i=0, j=0; i<num_read; i++)
1913 /* in text mode, a ctrl-z signals EOF */
1914 if (bufstart[i] == 0x1a)
1916 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1917 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1920 /* in text mode, strip \r if followed by \n.
1921 * BUG: should save state across calls somehow, so CR LF that
1922 * straddles buffer boundary gets recognized properly?
1924 if ((bufstart[i] != '\r')
1925 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1926 bufstart[j++] = bufstart[i];
1933 if (GetLastError() == ERROR_BROKEN_PIPE)
1935 TRACE(":end-of-pipe\n");
1936 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1941 TRACE(":failed-last error (%d)\n",GetLastError());
1947 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1951 /*********************************************************************
1954 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1957 num_read = read_i(fd, buf, count);
1961 /*********************************************************************
1962 * _setmode (MSVCRT.@)
1964 int CDECL _setmode(int fd,int mode)
1966 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1967 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1968 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1969 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1970 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1972 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1976 /*********************************************************************
1977 * _stat64 (MSVCRT.@)
1979 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
1982 WIN32_FILE_ATTRIBUTE_DATA hfi;
1983 unsigned short mode = ALL_S_IREAD;
1986 TRACE(":file (%s) buf(%p)\n",path,buf);
1988 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1990 TRACE("failed (%d)\n",GetLastError());
1991 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1995 memset(buf,0,sizeof(struct MSVCRT__stat64));
1997 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1998 Bon 011120: This FIXME seems incorrect
1999 Also a letter as first char isn't enough to be classified
2002 if (isalpha(*path)&& (*(path+1)==':'))
2003 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2005 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2007 plen = strlen(path);
2009 /* Dir, or regular file? */
2010 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2011 (path[plen-1] == '\\'))
2012 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2015 mode |= MSVCRT__S_IFREG;
2017 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2019 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2020 (tolower(path[plen-3]) << 16);
2021 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2022 mode |= ALL_S_IEXEC;
2026 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2027 mode |= ALL_S_IWRITE;
2029 buf->st_mode = mode;
2031 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2032 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2034 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2035 buf->st_mtime = buf->st_ctime = dw;
2036 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2037 (int)(buf->st_size >> 32),(int)buf->st_size,
2038 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2042 /*********************************************************************
2043 * _stati64 (MSVCRT.@)
2045 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2048 struct MSVCRT__stat64 buf64;
2050 ret = MSVCRT_stat64(path, &buf64);
2052 msvcrt_stat64_to_stati64(&buf64, buf);
2056 /*********************************************************************
2059 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2061 struct MSVCRT__stat64 buf64;
2063 ret = MSVCRT_stat64( path, &buf64);
2065 msvcrt_stat64_to_stat(&buf64, buf);
2069 /*********************************************************************
2070 * _wstat64 (MSVCRT.@)
2072 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2075 WIN32_FILE_ATTRIBUTE_DATA hfi;
2076 unsigned short mode = ALL_S_IREAD;
2079 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2081 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2083 TRACE("failed (%d)\n",GetLastError());
2084 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2088 memset(buf,0,sizeof(struct MSVCRT__stat64));
2090 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2091 if (MSVCRT_iswalpha(*path))
2092 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2094 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2096 plen = strlenW(path);
2098 /* Dir, or regular file? */
2099 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2100 (path[plen-1] == '\\'))
2101 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2104 mode |= MSVCRT__S_IFREG;
2106 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2108 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2109 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2110 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2111 mode |= ALL_S_IEXEC;
2115 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2116 mode |= ALL_S_IWRITE;
2118 buf->st_mode = mode;
2120 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2121 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2123 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2124 buf->st_mtime = buf->st_ctime = dw;
2125 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2126 (int)(buf->st_size >> 32),(int)buf->st_size,
2127 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2131 /*********************************************************************
2132 * _wstati64 (MSVCRT.@)
2134 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2137 struct MSVCRT__stat64 buf64;
2139 ret = MSVCRT__wstat64(path, &buf64);
2141 msvcrt_stat64_to_stati64(&buf64, buf);
2145 /*********************************************************************
2148 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2151 struct MSVCRT__stat64 buf64;
2153 ret = MSVCRT__wstat64( path, &buf64 );
2154 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2158 /*********************************************************************
2161 MSVCRT_long CDECL MSVCRT__tell(int fd)
2163 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2166 /*********************************************************************
2167 * _telli64 (MSVCRT.@)
2169 __int64 CDECL _telli64(int fd)
2171 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2174 /*********************************************************************
2175 * _tempnam (MSVCRT.@)
2177 char * CDECL _tempnam(const char *dir, const char *prefix)
2179 char tmpbuf[MAX_PATH];
2180 const char *tmp_dir = MSVCRT_getenv("TMP");
2182 if (tmp_dir) dir = tmp_dir;
2184 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2185 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2187 TRACE("got name (%s)\n",tmpbuf);
2188 DeleteFileA(tmpbuf);
2189 return _strdup(tmpbuf);
2191 TRACE("failed (%d)\n",GetLastError());
2195 /*********************************************************************
2196 * _wtempnam (MSVCRT.@)
2198 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2200 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2202 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2203 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2205 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2206 DeleteFileW(tmpbuf);
2207 return _wcsdup(tmpbuf);
2209 TRACE("failed (%d)\n",GetLastError());
2213 /*********************************************************************
2216 int CDECL MSVCRT__umask(int umask)
2218 int old_umask = MSVCRT_umask;
2219 TRACE("(%d)\n",umask);
2220 MSVCRT_umask = umask;
2224 /*********************************************************************
2225 * _utime64 (MSVCRT.@)
2227 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2229 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2233 int retVal = _futime64(fd, t);
2240 /*********************************************************************
2241 * _utime32 (MSVCRT.@)
2243 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2245 struct MSVCRT___utimbuf64 t64;
2246 t64.actime = t->actime;
2247 t64.modtime = t->modtime;
2248 return _utime64( path, &t64 );
2251 /*********************************************************************
2255 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2257 return _utime64( path, t );
2260 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2262 return _utime32( path, t );
2266 /*********************************************************************
2267 * _wutime64 (MSVCRT.@)
2269 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2271 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2275 int retVal = _futime64(fd, t);
2282 /*********************************************************************
2283 * _wutime32 (MSVCRT.@)
2285 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2287 struct MSVCRT___utimbuf64 t64;
2288 t64.actime = t->actime;
2289 t64.modtime = t->modtime;
2290 return _wutime64( path, &t64 );
2293 /*********************************************************************
2294 * _wutime (MSVCRT.@)
2297 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2299 return _wutime64( path, t );
2302 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2304 return _wutime32( path, t );
2308 /*********************************************************************
2311 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2314 HANDLE hand = msvcrt_fdtoh(fd);
2316 /* Don't trace small writes, it gets *very* annoying */
2319 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2321 if (hand == INVALID_HANDLE_VALUE)
2323 *MSVCRT__errno() = MSVCRT_EBADF;
2327 /* If appending, go to EOF */
2328 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2329 MSVCRT__lseek(fd, 0, FILE_END);
2331 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2333 if (WriteFile(hand, buf, count, &num_written, NULL)
2334 && (num_written == count))
2336 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2337 hand, GetLastError());
2338 *MSVCRT__errno() = MSVCRT_ENOSPC;
2342 unsigned int i, j, nr_lf;
2345 const char *s = buf, *buf_start = buf;
2346 /* find number of \n ( without preceding \r ) */
2347 for ( nr_lf=0,i = 0; i <count; i++)
2352 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2357 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2359 for (s = buf, i = 0, j = 0; i < count; i++)
2364 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2371 FIXME("Malloc failed\n");
2379 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2381 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2382 fd, hand, GetLastError(), num_written);
2383 *MSVCRT__errno() = MSVCRT_ENOSPC;
2386 return s - buf_start;
2398 /*********************************************************************
2401 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2404 len = MSVCRT__write(file->_file, &val, sizeof(val));
2405 if (len == sizeof(val)) return val;
2406 file->_flag |= MSVCRT__IOERR;
2410 /*********************************************************************
2413 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2418 MSVCRT_free(file->_tmpfname);
2419 file->_tmpfname = NULL;
2420 /* flush stdio buffers */
2421 if(file->_flag & MSVCRT__IOWRT)
2422 MSVCRT_fflush(file);
2423 if(file->_flag & MSVCRT__IOMYBUF)
2424 MSVCRT_free(file->_base);
2426 r=MSVCRT__close(file->_file);
2430 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2433 /*********************************************************************
2436 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2438 return file->_flag & MSVCRT__IOEOF;
2441 /*********************************************************************
2444 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2446 return file->_flag & MSVCRT__IOERR;
2449 /*********************************************************************
2450 * _filbuf (MSVCRT.@)
2452 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2454 /* Allocate buffer if needed */
2455 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2456 msvcrt_alloc_buffer(file);
2458 if(!(file->_flag & MSVCRT__IOREAD)) {
2459 if(file->_flag & MSVCRT__IORW) {
2460 file->_flag |= MSVCRT__IOREAD;
2465 if(file->_flag & MSVCRT__IONBF) {
2468 if ((r = read_i(file->_file,&c,1)) != 1) {
2469 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2474 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2476 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2481 file->_ptr = file->_base+1;
2482 return *(unsigned char *)file->_base;
2486 /*********************************************************************
2489 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2495 i = (unsigned char *)file->_ptr++;
2498 j = MSVCRT__filbuf(file);
2502 /*********************************************************************
2503 * _fgetchar (MSVCRT.@)
2505 int CDECL _fgetchar(void)
2507 return MSVCRT_fgetc(MSVCRT_stdin);
2510 /*********************************************************************
2513 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2515 int cc = MSVCRT_EOF;
2516 char * buf_start = s;
2518 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2519 file,file->_file,s,size);
2521 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2526 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2528 TRACE(":nothing read\n");
2531 if ((cc != MSVCRT_EOF) && (size > 1))
2534 TRACE(":got %s\n", debugstr_a(buf_start));
2538 /*********************************************************************
2541 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2542 * the CR from CR/LF combinations
2544 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2548 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2555 for(i=0; i<sizeof(wc); i++)
2565 j = MSVCRT__filbuf(file);
2568 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2578 c = MSVCRT_fgetc(file);
2579 if ((get_locale()->locinfo->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2581 FIXME("Treat Multibyte characters\n");
2583 if (c == MSVCRT_EOF)
2586 return (MSVCRT_wint_t)c;
2589 /*********************************************************************
2592 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2598 for (j=0; j<sizeof(int); j++) {
2599 k = MSVCRT_fgetc(file);
2600 if (k == MSVCRT_EOF) {
2601 file->_flag |= MSVCRT__IOEOF;
2609 /*********************************************************************
2612 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2614 return MSVCRT_fgetwc(file);
2617 /*********************************************************************
2618 * _fgetwchar (MSVCRT.@)
2620 MSVCRT_wint_t CDECL _fgetwchar(void)
2622 return MSVCRT_fgetwc(MSVCRT_stdin);
2625 /*********************************************************************
2626 * getwchar (MSVCRT.@)
2628 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2630 return _fgetwchar();
2633 /*********************************************************************
2636 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2638 int cc = MSVCRT_WEOF;
2639 MSVCRT_wchar_t * buf_start = s;
2641 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2642 file,file->_file,s,size);
2644 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2649 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2651 TRACE(":nothing read\n");
2654 if ((cc != MSVCRT_WEOF) && (size > 1))
2657 TRACE(":got %s\n", debugstr_w(buf_start));
2661 /*********************************************************************
2664 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2666 MSVCRT_size_t wrcnt=size * nmemb;
2671 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2672 memcpy(file->_ptr, ptr, pcnt);
2677 ptr = (const char*)ptr + pcnt;
2678 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2679 if(file->_flag & MSVCRT__IORW) {
2680 file->_flag |= MSVCRT__IOWRT;
2686 int res=msvcrt_flush_buffer(file);
2688 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2691 file->_flag |= MSVCRT__IOERR;
2694 written += pwritten;
2697 return written / size;
2700 /*********************************************************************
2703 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2705 MSVCRT_wchar_t mwc=wc;
2706 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2711 /*********************************************************************
2712 * _fputwchar (MSVCRT.@)
2714 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2716 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2719 /*********************************************************************
2720 * _wfsopen (MSVCRT.@)
2722 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2725 int open_flags, stream_flags, fd;
2727 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2729 /* map mode string to open() flags. "man fopen" for possibilities. */
2730 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2734 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2737 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2739 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2746 TRACE(":got (%p)\n",file);
2747 if (fd >= 0 && !file)
2753 /*********************************************************************
2754 * _fsopen (MSVCRT.@)
2756 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2759 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2761 if (path && !(pathW = msvcrt_wstrdupa(path))) {
2762 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2763 *MSVCRT__errno() = MSVCRT_EINVAL;
2766 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2769 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2770 *MSVCRT__errno() = MSVCRT_EINVAL;
2774 ret = MSVCRT__wfsopen(pathW, modeW, share);
2781 /*********************************************************************
2784 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2786 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2789 /*********************************************************************
2790 * fopen_s (MSVCRT.@)
2792 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
2793 const char *filename, const char *mode)
2795 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2796 !MSVCRT_CHECK_PMT(mode != NULL)) {
2797 *MSVCRT__errno() = MSVCRT_EINVAL;
2798 return MSVCRT_EINVAL;
2801 *pFile = MSVCRT_fopen(filename, mode);
2804 return *MSVCRT__errno();
2808 /*********************************************************************
2809 * _wfopen (MSVCRT.@)
2811 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2813 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2816 /*********************************************************************
2817 * _wfopen_s (MSVCRT.@)
2819 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
2820 const MSVCRT_wchar_t *mode)
2822 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2823 !MSVCRT_CHECK_PMT(mode != NULL)) {
2824 *MSVCRT__errno() = MSVCRT_EINVAL;
2825 return MSVCRT_EINVAL;
2828 *pFile = MSVCRT__wfopen(filename, mode);
2831 return *MSVCRT__errno();
2835 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2836 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2838 /*********************************************************************
2841 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2848 int res = msvcrt_flush_buffer(file);
2849 return res ? res : c;
2854 return MSVCRT__flsbuf(c, file);
2858 /*********************************************************************
2859 * _flsbuf (MSVCRT.@)
2861 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2863 /* Flush output buffer */
2864 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2865 msvcrt_alloc_buffer(file);
2867 if(!(file->_flag & MSVCRT__IOWRT)) {
2868 if(file->_flag & MSVCRT__IORW) {
2869 file->_flag |= MSVCRT__IOWRT;
2875 int res=msvcrt_flush_buffer(file);
2876 return res?res : MSVCRT_fputc(c, file);
2880 /* set _cnt to 0 for unbuffered FILEs */
2882 len = MSVCRT__write(file->_file, &cc, 1);
2883 if (len == 1) return c & 0xff;
2884 file->_flag |= MSVCRT__IOERR;
2889 /*********************************************************************
2890 * _fputchar (MSVCRT.@)
2892 int CDECL _fputchar(int c)
2894 return MSVCRT_fputc(c, MSVCRT_stdout);
2897 /*********************************************************************
2900 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2901 { MSVCRT_size_t rcnt=size * nmemb;
2902 MSVCRT_size_t read=0;
2908 /* first buffered data */
2910 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2911 memcpy(ptr, file->_ptr, pcnt);
2916 ptr = (char*)ptr + pcnt;
2917 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2918 if(file->_flag & MSVCRT__IORW) {
2919 file->_flag |= MSVCRT__IOREAD;
2926 /* Fill the buffer on small reads.
2927 * TODO: Use a better buffering strategy.
2929 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2930 if (file->_bufsiz == 0) {
2931 msvcrt_alloc_buffer(file);
2933 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2934 file->_ptr = file->_base;
2935 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2936 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2937 if (i > 0 && i < file->_cnt) {
2938 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2939 file->_flag &= ~MSVCRT__IOEOF;
2942 memcpy(ptr, file->_ptr, i);
2947 i = MSVCRT__read(file->_file,ptr, rcnt);
2951 ptr = (char *)ptr+i;
2952 /* expose feof condition in the flags
2953 * MFC tests file->_flag for feof, and doesn't call feof())
2955 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2956 file->_flag |= MSVCRT__IOEOF;
2959 file->_flag |= MSVCRT__IOERR;
2969 /*********************************************************************
2970 * _wfreopen (MSVCRT.@)
2973 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
2975 int open_flags, stream_flags, fd;
2977 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2980 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2984 MSVCRT_fclose(file);
2985 /* map mode string to open() flags. "man fopen" for possibilities. */
2986 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2990 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2993 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2996 WARN(":failed-last error (%d)\n",GetLastError());
2997 msvcrt_set_errno(GetLastError());
3006 /*********************************************************************
3007 * freopen (MSVCRT.@)
3010 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3013 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3015 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3016 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3022 ret = MSVCRT__wfreopen(pathW, modeW, file);
3029 /*********************************************************************
3030 * fsetpos (MSVCRT.@)
3032 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3034 /* Note that all this has been lifted 'as is' from fseek */
3035 if(file->_flag & MSVCRT__IOWRT)
3036 msvcrt_flush_buffer(file);
3038 /* Discard buffered input */
3040 file->_ptr = file->_base;
3042 /* Reset direction of i/o */
3043 if(file->_flag & MSVCRT__IORW) {
3044 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3047 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3050 /*********************************************************************
3051 * _ftelli64 (MSVCRT.@)
3053 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3055 /* TODO: just call fgetpos and return lower half of result */
3058 pos = _telli64(file->_file);
3059 if(pos == -1) return -1;
3061 if( file->_flag & MSVCRT__IOWRT ) {
3062 off = file->_ptr - file->_base;
3065 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3066 /* Black magic correction for CR removal */
3068 for (i=0; i<file->_cnt; i++) {
3069 if (file->_ptr[i] == '\n')
3072 /* Black magic when reading CR at buffer boundary*/
3073 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3082 /*********************************************************************
3085 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3087 return MSVCRT__ftelli64(file);
3090 /*********************************************************************
3091 * fgetpos (MSVCRT.@)
3093 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3096 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3097 if(*pos == -1) return -1;
3099 if( file->_flag & MSVCRT__IOWRT ) {
3100 off = file->_ptr - file->_base;
3103 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3104 /* Black magic correction for CR removal */
3106 for (i=0; i<file->_cnt; i++) {
3107 if (file->_ptr[i] == '\n')
3110 /* Black magic when reading CR at buffer boundary*/
3111 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3120 /*********************************************************************
3123 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3125 MSVCRT_size_t i, len = strlen(s);
3126 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3127 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3128 for (i=0; i<len; i++)
3129 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
3134 /*********************************************************************
3137 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3139 MSVCRT_size_t i, len = strlenW(s);
3140 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3141 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3142 for (i=0; i<len; i++)
3144 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3146 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
3152 /*********************************************************************
3153 * getchar (MSVCRT.@)
3155 int CDECL MSVCRT_getchar(void)
3157 return MSVCRT_fgetc(MSVCRT_stdin);
3160 /*********************************************************************
3163 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3165 return MSVCRT_fgetc(file);
3168 /*********************************************************************
3171 char * CDECL MSVCRT_gets(char *buf)
3174 char * buf_start = buf;
3176 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3177 cc = MSVCRT_fgetc(MSVCRT_stdin))
3178 if(cc != '\r') *buf++ = (char)cc;
3182 TRACE("got '%s'\n", buf_start);
3186 /*********************************************************************
3189 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3192 MSVCRT_wchar_t* ws = buf;
3194 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3195 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3198 *buf++ = (MSVCRT_wchar_t)cc;
3202 TRACE("got %s\n", debugstr_w(ws));
3206 /*********************************************************************
3209 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3211 return MSVCRT_fputc(c, file);
3214 /*********************************************************************
3215 * putchar (MSVCRT.@)
3217 int CDECL MSVCRT_putchar(int c)
3219 return MSVCRT_fputc(c, MSVCRT_stdout);
3222 /*********************************************************************
3223 * _putwch (MSVCRT.@)
3225 int CDECL MSVCRT__putwch(int c)
3227 return MSVCRT_fputwc(c, MSVCRT_stdout);
3230 /*********************************************************************
3233 int CDECL MSVCRT_puts(const char *s)
3235 MSVCRT_size_t len = strlen(s);
3236 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3237 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3240 /*********************************************************************
3243 int CDECL _putws(const MSVCRT_wchar_t *s)
3245 static const MSVCRT_wchar_t nl = '\n';
3246 MSVCRT_size_t len = strlenW(s);
3247 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3248 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3251 /*********************************************************************
3254 int CDECL MSVCRT_remove(const char *path)
3256 TRACE("(%s)\n",path);
3257 if (DeleteFileA(path))
3259 TRACE(":failed (%d)\n",GetLastError());
3260 msvcrt_set_errno(GetLastError());
3264 /*********************************************************************
3265 * _wremove (MSVCRT.@)
3267 int CDECL _wremove(const MSVCRT_wchar_t *path)
3269 TRACE("(%s)\n",debugstr_w(path));
3270 if (DeleteFileW(path))
3272 TRACE(":failed (%d)\n",GetLastError());
3273 msvcrt_set_errno(GetLastError());
3277 /*********************************************************************
3280 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3282 TRACE(":from %s to %s\n",oldpath,newpath);
3283 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3285 TRACE(":failed (%d)\n",GetLastError());
3286 msvcrt_set_errno(GetLastError());
3290 /*********************************************************************
3291 * _wrename (MSVCRT.@)
3293 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3295 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3296 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3298 TRACE(":failed (%d)\n",GetLastError());
3299 msvcrt_set_errno(GetLastError());
3303 /*********************************************************************
3304 * setvbuf (MSVCRT.@)
3306 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3308 /* TODO: Check if file busy */
3310 MSVCRT_free(file->_base);
3314 if(mode == MSVCRT__IOFBF) {
3315 file->_flag &= ~MSVCRT__IONBF;
3316 file->_base = file->_ptr = buf;
3318 file->_bufsiz = size;
3321 file->_flag |= MSVCRT__IONBF;
3326 /*********************************************************************
3329 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3331 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3334 /*********************************************************************
3337 char * CDECL MSVCRT_tmpnam(char *s)
3345 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3346 p = s + sprintf(s, "\\s%s.", tmpstr);
3347 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3349 msvcrt_int_to_base32(unique++, tmpstr);
3351 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3352 GetLastError() == ERROR_FILE_NOT_FOUND)
3358 /*********************************************************************
3359 * tmpfile (MSVCRT.@)
3361 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3363 char *filename = MSVCRT_tmpnam(NULL);
3365 MSVCRT_FILE* file = NULL;
3368 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3369 if (fd != -1 && (file = msvcrt_alloc_fp()))
3371 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3376 else file->_tmpfname = _strdup(filename);
3382 static int puts_clbk_file_a(void *file, int len, const char *str)
3384 return MSVCRT_fwrite(str, sizeof(char), len, file);
3387 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
3389 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
3392 /*********************************************************************
3393 * vfprintf (MSVCRT.@)
3395 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3397 return pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, valist);
3400 /*********************************************************************
3401 * vfprintf_s (MSVCRT.@)
3403 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3405 if(!MSVCRT_CHECK_PMT(file != NULL)) {
3406 *MSVCRT__errno() = MSVCRT_EINVAL;
3410 return pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, valist);
3413 /*********************************************************************
3414 * vfwprintf (MSVCRT.@)
3416 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3418 return pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, valist);
3421 /*********************************************************************
3422 * vfwprintf_s (MSVCRT.@)
3424 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3426 if(!MSVCRT_CHECK_PMT( file != NULL)) {
3427 *MSVCRT__errno() = MSVCRT_EINVAL;
3431 return pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, valist);
3434 /*********************************************************************
3435 * vprintf (MSVCRT.@)
3437 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3439 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3442 /*********************************************************************
3443 * vprintf_s (MSVCRT.@)
3445 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
3447 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
3450 /*********************************************************************
3451 * vwprintf (MSVCRT.@)
3453 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3455 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3458 /*********************************************************************
3459 * vwprintf_s (MSVCRT.@)
3461 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
3463 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
3466 /*********************************************************************
3467 * fprintf (MSVCRT.@)
3469 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3471 __ms_va_list valist;
3473 __ms_va_start(valist, format);
3474 res = MSVCRT_vfprintf(file, format, valist);
3475 __ms_va_end(valist);
3479 /*********************************************************************
3480 * fprintf_s (MSVCRT.@)
3482 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
3484 __ms_va_list valist;
3486 __ms_va_start(valist, format);
3487 res = MSVCRT_vfprintf_s(file, format, valist);
3488 __ms_va_end(valist);
3492 /*********************************************************************
3493 * fwprintf (MSVCRT.@)
3495 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3497 __ms_va_list valist;
3499 __ms_va_start(valist, format);
3500 res = MSVCRT_vfwprintf(file, format, valist);
3501 __ms_va_end(valist);
3505 /*********************************************************************
3506 * fwprintf_s (MSVCRT.@)
3508 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3510 __ms_va_list valist;
3512 __ms_va_start(valist, format);
3513 res = MSVCRT_vfwprintf_s(file, format, valist);
3514 __ms_va_end(valist);
3518 /*********************************************************************
3521 int CDECL MSVCRT_printf(const char *format, ...)
3523 __ms_va_list valist;
3525 __ms_va_start(valist, format);
3526 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3527 __ms_va_end(valist);
3531 /*********************************************************************
3532 * printf_s (MSVCRT.@)
3534 int CDECL MSVCRT_printf_s(const char *format, ...)
3536 __ms_va_list valist;
3538 __ms_va_start(valist, format);
3539 res = MSVCRT_vprintf_s(format, valist);
3540 __ms_va_end(valist);
3544 /*********************************************************************
3547 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3549 if (c == MSVCRT_EOF)
3551 if(file->_bufsiz == 0) {
3552 msvcrt_alloc_buffer(file);
3555 if(file->_ptr>file->_base) {
3559 MSVCRT_clearerr(file);
3565 /*********************************************************************
3566 * ungetwc (MSVCRT.@)
3568 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3570 MSVCRT_wchar_t mwc = wc;
3571 char * pp = (char *)&mwc;
3573 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3574 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3580 /*********************************************************************
3581 * wprintf (MSVCRT.@)
3583 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3585 __ms_va_list valist;
3587 __ms_va_start(valist, format);
3588 res = MSVCRT_vwprintf(format, valist);
3589 __ms_va_end(valist);
3593 /*********************************************************************
3594 * wprintf_s (MSVCRT.@)
3596 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
3598 __ms_va_list valist;
3600 __ms_va_start(valist, format);
3601 res = MSVCRT_vwprintf_s(format, valist);
3602 __ms_va_end(valist);
3606 /*********************************************************************
3607 * _getmaxstdio (MSVCRT.@)
3609 int CDECL _getmaxstdio(void)
3611 FIXME("stub, always returns 512\n");
3615 /*********************************************************************
3616 * _setmaxstdio_ (MSVCRT.@)
3618 int CDECL _setmaxstdio(int newmax)
3625 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3629 /*********************************************************************
3630 * __pioinfo (MSVCRT.@)
3631 * FIXME: see MSVCRT_MAX_FILES define.
3633 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3634 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3635 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3636 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3637 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3638 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3639 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3640 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3641 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3642 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3643 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3644 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3647 /*********************************************************************
3648 * __badioinfo (MSVCRT.@)
3650 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };