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);
866 LARGE_INTEGER ofs, ret;
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 ofs.QuadPart = offset;
885 if (SetFilePointerEx(hand, ofs, &ret, whence))
887 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
888 /* FIXME: What if we seek _to_ EOF - is EOF set? */
892 TRACE(":error-last error (%d)\n",GetLastError());
893 msvcrt_set_errno(GetLastError());
897 /*********************************************************************
900 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
902 return MSVCRT__lseeki64(fd, offset, whence);
905 /*********************************************************************
906 * _lock_file (MSVCRT.@)
908 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
910 FIXME("(%p) stub\n",file);
913 /*********************************************************************
914 * _unlock_file (MSVCRT.@)
916 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
918 FIXME("(%p) stub\n",file);
921 /*********************************************************************
922 * _locking (MSVCRT.@)
924 * This is untested; the underlying LockFile doesn't work yet.
926 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
930 HANDLE hand = msvcrt_fdtoh(fd);
932 TRACE(":fd (%d) handle (%p)\n",fd,hand);
933 if (hand == INVALID_HANDLE_VALUE)
936 if (mode < 0 || mode > 4)
938 *MSVCRT__errno() = MSVCRT_EINVAL;
942 TRACE(":fd (%d) by 0x%08x mode %s\n",
943 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
944 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
945 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
946 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
947 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
950 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
952 FIXME ("Seek failed\n");
953 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
956 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
959 ret = 1; /* just to satisfy gcc */
962 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
967 else if (mode == MSVCRT__LK_UNLCK)
968 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
970 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
971 /* FIXME - what about error settings? */
975 /*********************************************************************
976 * _fseeki64 (MSVCRT.@)
978 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
980 /* Flush output if needed */
981 if(file->_flag & MSVCRT__IOWRT)
982 msvcrt_flush_buffer(file);
984 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
985 offset -= file->_cnt;
986 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
987 /* Black magic correction for CR removal */
989 for (i=0; i<file->_cnt; i++) {
990 if (file->_ptr[i] == '\n')
993 /* Black magic when reading CR at buffer boundary*/
994 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
998 /* Discard buffered input */
1000 file->_ptr = file->_base;
1001 /* Reset direction of i/o */
1002 if(file->_flag & MSVCRT__IORW) {
1003 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1005 /* Clear end of file flag */
1006 file->_flag &= ~MSVCRT__IOEOF;
1007 return (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1010 /*********************************************************************
1013 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1015 return MSVCRT__fseeki64( file, offset, whence );
1018 /*********************************************************************
1019 * _chsize (MSVCRT.@)
1021 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1027 TRACE("(fd=%d, size=%d)\n", fd, size);
1031 handle = msvcrt_fdtoh(fd);
1032 if (handle != INVALID_HANDLE_VALUE)
1034 /* save the current file pointer */
1035 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1038 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1041 ret = SetEndOfFile(handle);
1042 if (!ret) msvcrt_set_errno(GetLastError());
1045 /* restore the file pointer */
1046 MSVCRT__lseek(fd, cur, SEEK_SET);
1051 return ret ? 0 : -1;
1054 /*********************************************************************
1055 * clearerr (MSVCRT.@)
1057 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1059 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1060 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1063 /*********************************************************************
1066 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1068 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1069 MSVCRT_fseek(file, 0L, SEEK_SET);
1070 MSVCRT_clearerr(file);
1073 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1075 int plus = strchrW(mode, '+') != NULL;
1080 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1081 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1084 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1085 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1088 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1089 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1092 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1093 *MSVCRT__errno() = MSVCRT_EINVAL;
1101 *open_flags |= MSVCRT__O_BINARY;
1102 *open_flags &= ~MSVCRT__O_TEXT;
1105 *open_flags |= MSVCRT__O_TEXT;
1106 *open_flags &= ~MSVCRT__O_BINARY;
1112 FIXME(":unknown flag %c not supported\n",mode[-1]);
1117 /*********************************************************************
1118 * _fdopen (MSVCRT.@)
1120 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1123 MSVCRT_wchar_t *modeW = NULL;
1125 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1127 ret = MSVCRT__wfdopen(fd, modeW);
1133 /*********************************************************************
1134 * _wfdopen (MSVCRT.@)
1136 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1138 int open_flags, stream_flags;
1141 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1144 if (!(file = msvcrt_alloc_fp()))
1146 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1151 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1157 /*********************************************************************
1158 * _filelength (MSVCRT.@)
1160 LONG CDECL MSVCRT__filelength(int fd)
1162 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1165 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1168 if (endPos != curPos)
1169 MSVCRT__lseek(fd, curPos, SEEK_SET);
1176 /*********************************************************************
1177 * _filelengthi64 (MSVCRT.@)
1179 __int64 CDECL MSVCRT__filelengthi64(int fd)
1181 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1184 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1187 if (endPos != curPos)
1188 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1195 /*********************************************************************
1196 * _fileno (MSVCRT.@)
1198 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1200 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1204 /*********************************************************************
1205 * _fstat64 (MSVCRT.@)
1207 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1211 BY_HANDLE_FILE_INFORMATION hfi;
1212 HANDLE hand = msvcrt_fdtoh(fd);
1214 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1215 if (hand == INVALID_HANDLE_VALUE)
1220 WARN(":failed-NULL buf\n");
1221 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1225 memset(&hfi, 0, sizeof(hfi));
1226 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1227 type = GetFileType(hand);
1228 if (type == FILE_TYPE_PIPE)
1230 buf->st_dev = buf->st_rdev = fd;
1231 buf->st_mode = S_IFIFO;
1234 else if (type == FILE_TYPE_CHAR)
1236 buf->st_dev = buf->st_rdev = fd;
1237 buf->st_mode = S_IFCHR;
1240 else /* FILE_TYPE_DISK etc. */
1242 if (!GetFileInformationByHandle(hand, &hfi))
1244 WARN(":failed-last error (%d)\n",GetLastError());
1245 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1248 buf->st_mode = S_IFREG | 0444;
1249 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1250 buf->st_mode |= 0222;
1251 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1252 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1254 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1255 buf->st_mtime = buf->st_ctime = dw;
1256 buf->st_nlink = hfi.nNumberOfLinks;
1258 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1263 /*********************************************************************
1264 * _fstati64 (MSVCRT.@)
1266 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1269 struct MSVCRT__stat64 buf64;
1271 ret = MSVCRT__fstat64(fd, &buf64);
1273 msvcrt_stat64_to_stati64(&buf64, buf);
1277 /*********************************************************************
1280 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1282 struct MSVCRT__stat64 buf64;
1284 ret = MSVCRT__fstat64(fd, &buf64);
1286 msvcrt_stat64_to_stat(&buf64, buf);
1290 /*********************************************************************
1291 * _futime64 (MSVCRT.@)
1293 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1295 HANDLE hand = msvcrt_fdtoh(fd);
1300 time_to_filetime( MSVCRT__time64(NULL), &at );
1305 time_to_filetime( t->actime, &at );
1306 time_to_filetime( t->modtime, &wt );
1309 if (!SetFileTime(hand, NULL, &at, &wt))
1311 msvcrt_set_errno(GetLastError());
1317 /*********************************************************************
1318 * _futime32 (MSVCRT.@)
1320 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1322 struct MSVCRT___utimbuf64 t64;
1323 t64.actime = t->actime;
1324 t64.modtime = t->modtime;
1325 return _futime64( fd, &t64 );
1328 /*********************************************************************
1329 * _futime (MSVCRT.@)
1332 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1334 return _futime64( fd, t );
1337 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1339 return _futime32( fd, t );
1343 /*********************************************************************
1344 * _get_osfhandle (MSVCRT.@)
1346 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1348 HANDLE hand = msvcrt_fdtoh(fd);
1349 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1351 return (MSVCRT_intptr_t)hand;
1354 /*********************************************************************
1355 * _isatty (MSVCRT.@)
1357 int CDECL _isatty(int fd)
1359 HANDLE hand = msvcrt_fdtoh(fd);
1361 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1362 if (hand == INVALID_HANDLE_VALUE)
1365 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1368 /*********************************************************************
1369 * _mktemp (MSVCRT.@)
1371 char * CDECL _mktemp(char *pattern)
1374 char *retVal = pattern;
1379 numX = (*pattern++ == 'X')? numX + 1 : 0;
1383 id = GetCurrentProcessId();
1387 int tempNum = id / 10;
1388 *pattern-- = id - (tempNum * 10) + '0';
1394 *pattern = letter++;
1395 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1396 GetLastError() == ERROR_FILE_NOT_FOUND)
1398 } while(letter <= 'z');
1402 /*********************************************************************
1403 * _wmktemp (MSVCRT.@)
1405 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1408 MSVCRT_wchar_t *retVal = pattern;
1410 MSVCRT_wchar_t letter = 'a';
1413 numX = (*pattern++ == 'X')? numX + 1 : 0;
1417 id = GetCurrentProcessId();
1421 int tempNum = id / 10;
1422 *pattern-- = id - (tempNum * 10) + '0';
1428 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1429 GetLastError() == ERROR_FILE_NOT_FOUND)
1431 *pattern = letter++;
1432 } while(letter != '|');
1436 static unsigned split_oflags(unsigned oflags)
1439 unsigned unsupp; /* until we support everything */
1441 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1442 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1443 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1444 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1445 else wxflags |= WX_TEXT; /* default to TEXT*/
1446 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1448 if ((unsupp = oflags & ~(
1449 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1450 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1451 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1452 MSVCRT__O_NOINHERIT|
1453 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1455 ERR(":unsupported oflags 0x%04x\n",unsupp);
1460 /*********************************************************************
1463 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1466 SECURITY_ATTRIBUTES sa;
1467 HANDLE readHandle, writeHandle;
1471 *MSVCRT__errno() = MSVCRT_EINVAL;
1475 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1476 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1477 sa.lpSecurityDescriptor = NULL;
1478 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1480 unsigned int wxflags = split_oflags(textmode);
1484 fd = msvcrt_alloc_fd(readHandle, wxflags);
1488 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1496 MSVCRT__close(pfds[0]);
1497 CloseHandle(writeHandle);
1498 *MSVCRT__errno() = MSVCRT_EMFILE;
1503 CloseHandle(readHandle);
1504 CloseHandle(writeHandle);
1505 *MSVCRT__errno() = MSVCRT_EMFILE;
1510 msvcrt_set_errno(GetLastError());
1515 /*********************************************************************
1516 * _sopen_s (MSVCRT.@)
1518 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1520 DWORD access = 0, creation = 0, attrib;
1524 SECURITY_ATTRIBUTES sa;
1526 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1527 fd, path, oflags, shflags, pmode);
1531 MSVCRT_INVALID_PMT("null out fd pointer");
1532 *MSVCRT__errno() = MSVCRT_EINVAL;
1533 return MSVCRT_EINVAL;
1537 wxflag = split_oflags(oflags);
1538 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1540 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1541 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1542 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1545 if (oflags & MSVCRT__O_CREAT)
1547 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1548 FIXME(": pmode 0x%04x ignored\n", pmode);
1550 WARN(": pmode 0x%04x ignored\n", pmode);
1552 if (oflags & MSVCRT__O_EXCL)
1553 creation = CREATE_NEW;
1554 else if (oflags & MSVCRT__O_TRUNC)
1555 creation = CREATE_ALWAYS;
1557 creation = OPEN_ALWAYS;
1559 else /* no MSVCRT__O_CREAT */
1561 if (oflags & MSVCRT__O_TRUNC)
1562 creation = TRUNCATE_EXISTING;
1564 creation = OPEN_EXISTING;
1569 case MSVCRT__SH_DENYRW:
1572 case MSVCRT__SH_DENYWR:
1573 sharing = FILE_SHARE_READ;
1575 case MSVCRT__SH_DENYRD:
1576 sharing = FILE_SHARE_WRITE;
1578 case MSVCRT__SH_DENYNO:
1579 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1582 ERR( "Unhandled shflags 0x%x\n", shflags );
1583 return MSVCRT_EINVAL;
1585 attrib = FILE_ATTRIBUTE_NORMAL;
1587 if (oflags & MSVCRT__O_TEMPORARY)
1589 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1591 sharing |= FILE_SHARE_DELETE;
1594 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1595 sa.lpSecurityDescriptor = NULL;
1596 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1598 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1599 if (hand == INVALID_HANDLE_VALUE) {
1600 WARN(":failed-last error (%d)\n", GetLastError());
1601 msvcrt_set_errno(GetLastError());
1602 return *MSVCRT__errno();
1605 *fd = msvcrt_alloc_fd(hand, wxflag);
1607 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1611 /*********************************************************************
1614 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1619 if (oflags & MSVCRT__O_CREAT)
1623 __ms_va_start(ap, shflags);
1624 pmode = va_arg(ap, int);
1630 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
1634 /*********************************************************************
1635 * _wsopen_s (MSVCRT.@)
1637 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1639 DWORD access = 0, creation = 0, attrib;
1640 SECURITY_ATTRIBUTES sa;
1645 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1646 fd, debugstr_w(path), oflags, shflags, pmode);
1650 MSVCRT_INVALID_PMT("null out fd pointer");
1651 *MSVCRT__errno() = MSVCRT_EINVAL;
1652 return MSVCRT_EINVAL;
1656 wxflag = split_oflags(oflags);
1657 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1659 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1660 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1661 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1664 if (oflags & MSVCRT__O_CREAT)
1666 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1667 FIXME(": pmode 0x%04x ignored\n", pmode);
1669 WARN(": pmode 0x%04x ignored\n", pmode);
1671 if (oflags & MSVCRT__O_EXCL)
1672 creation = CREATE_NEW;
1673 else if (oflags & MSVCRT__O_TRUNC)
1674 creation = CREATE_ALWAYS;
1676 creation = OPEN_ALWAYS;
1678 else /* no MSVCRT__O_CREAT */
1680 if (oflags & MSVCRT__O_TRUNC)
1681 creation = TRUNCATE_EXISTING;
1683 creation = OPEN_EXISTING;
1688 case MSVCRT__SH_DENYRW:
1691 case MSVCRT__SH_DENYWR:
1692 sharing = FILE_SHARE_READ;
1694 case MSVCRT__SH_DENYRD:
1695 sharing = FILE_SHARE_WRITE;
1697 case MSVCRT__SH_DENYNO:
1698 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1701 ERR( "Unhandled shflags 0x%x\n", shflags );
1702 return MSVCRT_EINVAL;
1704 attrib = FILE_ATTRIBUTE_NORMAL;
1706 if (oflags & MSVCRT__O_TEMPORARY)
1708 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1710 sharing |= FILE_SHARE_DELETE;
1713 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1714 sa.lpSecurityDescriptor = NULL;
1715 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1717 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1719 if (hand == INVALID_HANDLE_VALUE) {
1720 WARN(":failed-last error (%d)\n",GetLastError());
1721 msvcrt_set_errno(GetLastError());
1722 msvcrt_set_errno(GetLastError());
1723 return *MSVCRT__errno();
1726 *fd = msvcrt_alloc_fd(hand, wxflag);
1728 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1732 /*********************************************************************
1733 * _wsopen (MSVCRT.@)
1735 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1740 if (oflags & MSVCRT__O_CREAT)
1744 __ms_va_start(ap, shflags);
1745 pmode = va_arg(ap, int);
1751 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1755 /*********************************************************************
1758 int CDECL MSVCRT__open( const char *path, int flags, ... )
1762 if (flags & MSVCRT__O_CREAT)
1765 __ms_va_start(ap, flags);
1766 pmode = va_arg(ap, int);
1768 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1771 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1774 /*********************************************************************
1777 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1781 if (flags & MSVCRT__O_CREAT)
1784 __ms_va_start(ap, flags);
1785 pmode = va_arg(ap, int);
1787 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1790 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1793 /*********************************************************************
1796 int CDECL MSVCRT__creat(const char *path, int flags)
1798 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1799 return MSVCRT__open(path, usedFlags);
1802 /*********************************************************************
1803 * _wcreat (MSVCRT.@)
1805 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1807 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1808 return _wopen(path, usedFlags);
1811 /*********************************************************************
1812 * _open_osfhandle (MSVCRT.@)
1814 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1818 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1819 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1820 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1821 * text - it never sets MSVCRT__O_BINARY.
1823 /* don't let split_oflags() decide the mode if no mode is passed */
1824 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1825 oflags |= MSVCRT__O_BINARY;
1827 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1828 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1832 /*********************************************************************
1835 int CDECL _rmtmp(void)
1837 int num_removed = 0, i;
1840 for (i = 3; i < MSVCRT_stream_idx; i++)
1841 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1843 MSVCRT_fclose(MSVCRT_fstreams[i]);
1849 TRACE(":removed (%d) temp files\n",num_removed);
1853 /*********************************************************************
1856 * When reading \r as last character in text mode, read() positions
1857 * the file pointer on the \r character while getc() goes on to
1860 static int read_i(int fd, void *buf, unsigned int count)
1863 char *bufstart = buf;
1864 HANDLE hand = msvcrt_fdtoh(fd);
1869 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1870 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1871 TRACE("already at EOF, returning 0\n");
1874 /* Don't trace small reads, it gets *very* annoying */
1876 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1877 if (hand == INVALID_HANDLE_VALUE)
1880 /* Reading single bytes in O_TEXT mode makes things slow
1881 * So read big chunks
1883 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1885 if (count != 0 && num_read == 0)
1887 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1888 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1890 else if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1893 if (bufstart[num_read-1] == '\r')
1897 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1898 ReadFile(hand, bufstart, 1, &num_read, NULL);
1902 MSVCRT_fdesc[fd].wxflag |= WX_READCR;
1907 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1908 for (i=0, j=0; i<num_read; i++)
1910 /* in text mode, a ctrl-z signals EOF */
1911 if (bufstart[i] == 0x1a)
1913 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1914 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1917 /* in text mode, strip \r if followed by \n.
1918 * BUG: should save state across calls somehow, so CR LF that
1919 * straddles buffer boundary gets recognized properly?
1921 if ((bufstart[i] != '\r')
1922 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1923 bufstart[j++] = bufstart[i];
1930 if (GetLastError() == ERROR_BROKEN_PIPE)
1932 TRACE(":end-of-pipe\n");
1933 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1938 TRACE(":failed-last error (%d)\n",GetLastError());
1944 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1948 /*********************************************************************
1951 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1954 num_read = read_i(fd, buf, count);
1958 /*********************************************************************
1959 * _setmode (MSVCRT.@)
1961 int CDECL _setmode(int fd,int mode)
1963 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1964 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1965 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1966 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1967 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1969 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1973 /*********************************************************************
1974 * _stat64 (MSVCRT.@)
1976 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
1979 WIN32_FILE_ATTRIBUTE_DATA hfi;
1980 unsigned short mode = ALL_S_IREAD;
1983 TRACE(":file (%s) buf(%p)\n",path,buf);
1985 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1987 TRACE("failed (%d)\n",GetLastError());
1988 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1992 memset(buf,0,sizeof(struct MSVCRT__stat64));
1994 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1995 Bon 011120: This FIXME seems incorrect
1996 Also a letter as first char isn't enough to be classified
1999 if (isalpha(*path)&& (*(path+1)==':'))
2000 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2002 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2004 plen = strlen(path);
2006 /* Dir, or regular file? */
2007 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2008 (path[plen-1] == '\\'))
2009 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2012 mode |= MSVCRT__S_IFREG;
2014 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2016 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2017 (tolower(path[plen-3]) << 16);
2018 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2019 mode |= ALL_S_IEXEC;
2023 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2024 mode |= ALL_S_IWRITE;
2026 buf->st_mode = mode;
2028 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2029 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2031 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2032 buf->st_mtime = buf->st_ctime = dw;
2033 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2034 (int)(buf->st_size >> 32),(int)buf->st_size,
2035 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2039 /*********************************************************************
2040 * _stati64 (MSVCRT.@)
2042 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2045 struct MSVCRT__stat64 buf64;
2047 ret = MSVCRT_stat64(path, &buf64);
2049 msvcrt_stat64_to_stati64(&buf64, buf);
2053 /*********************************************************************
2056 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2058 struct MSVCRT__stat64 buf64;
2060 ret = MSVCRT_stat64( path, &buf64);
2062 msvcrt_stat64_to_stat(&buf64, buf);
2066 /*********************************************************************
2067 * _wstat64 (MSVCRT.@)
2069 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2072 WIN32_FILE_ATTRIBUTE_DATA hfi;
2073 unsigned short mode = ALL_S_IREAD;
2076 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2078 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2080 TRACE("failed (%d)\n",GetLastError());
2081 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2085 memset(buf,0,sizeof(struct MSVCRT__stat64));
2087 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2088 if (MSVCRT_iswalpha(*path))
2089 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2091 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2093 plen = strlenW(path);
2095 /* Dir, or regular file? */
2096 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2097 (path[plen-1] == '\\'))
2098 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2101 mode |= MSVCRT__S_IFREG;
2103 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2105 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2106 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2107 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2108 mode |= ALL_S_IEXEC;
2112 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2113 mode |= ALL_S_IWRITE;
2115 buf->st_mode = mode;
2117 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2118 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2120 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2121 buf->st_mtime = buf->st_ctime = dw;
2122 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2123 (int)(buf->st_size >> 32),(int)buf->st_size,
2124 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2128 /*********************************************************************
2129 * _wstati64 (MSVCRT.@)
2131 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2134 struct MSVCRT__stat64 buf64;
2136 ret = MSVCRT__wstat64(path, &buf64);
2138 msvcrt_stat64_to_stati64(&buf64, buf);
2142 /*********************************************************************
2145 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2148 struct MSVCRT__stat64 buf64;
2150 ret = MSVCRT__wstat64( path, &buf64 );
2151 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2155 /*********************************************************************
2158 MSVCRT_long CDECL MSVCRT__tell(int fd)
2160 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2163 /*********************************************************************
2164 * _telli64 (MSVCRT.@)
2166 __int64 CDECL _telli64(int fd)
2168 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2171 /*********************************************************************
2172 * _tempnam (MSVCRT.@)
2174 char * CDECL _tempnam(const char *dir, const char *prefix)
2176 char tmpbuf[MAX_PATH];
2177 const char *tmp_dir = MSVCRT_getenv("TMP");
2179 if (tmp_dir) dir = tmp_dir;
2181 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2182 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2184 TRACE("got name (%s)\n",tmpbuf);
2185 DeleteFileA(tmpbuf);
2186 return _strdup(tmpbuf);
2188 TRACE("failed (%d)\n",GetLastError());
2192 /*********************************************************************
2193 * _wtempnam (MSVCRT.@)
2195 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2197 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2199 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2200 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2202 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2203 DeleteFileW(tmpbuf);
2204 return _wcsdup(tmpbuf);
2206 TRACE("failed (%d)\n",GetLastError());
2210 /*********************************************************************
2213 int CDECL MSVCRT__umask(int umask)
2215 int old_umask = MSVCRT_umask;
2216 TRACE("(%d)\n",umask);
2217 MSVCRT_umask = umask;
2221 /*********************************************************************
2222 * _utime64 (MSVCRT.@)
2224 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2226 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2230 int retVal = _futime64(fd, t);
2237 /*********************************************************************
2238 * _utime32 (MSVCRT.@)
2240 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2242 struct MSVCRT___utimbuf64 t64;
2243 t64.actime = t->actime;
2244 t64.modtime = t->modtime;
2245 return _utime64( path, &t64 );
2248 /*********************************************************************
2252 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2254 return _utime64( path, t );
2257 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2259 return _utime32( path, t );
2263 /*********************************************************************
2264 * _wutime64 (MSVCRT.@)
2266 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2268 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2272 int retVal = _futime64(fd, t);
2279 /*********************************************************************
2280 * _wutime32 (MSVCRT.@)
2282 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2284 struct MSVCRT___utimbuf64 t64;
2285 t64.actime = t->actime;
2286 t64.modtime = t->modtime;
2287 return _wutime64( path, &t64 );
2290 /*********************************************************************
2291 * _wutime (MSVCRT.@)
2294 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2296 return _wutime64( path, t );
2299 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2301 return _wutime32( path, t );
2305 /*********************************************************************
2308 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2311 HANDLE hand = msvcrt_fdtoh(fd);
2313 /* Don't trace small writes, it gets *very* annoying */
2316 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2318 if (hand == INVALID_HANDLE_VALUE)
2320 *MSVCRT__errno() = MSVCRT_EBADF;
2324 /* If appending, go to EOF */
2325 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2326 MSVCRT__lseek(fd, 0, FILE_END);
2328 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2330 if (WriteFile(hand, buf, count, &num_written, NULL)
2331 && (num_written == count))
2333 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2334 hand, GetLastError());
2335 *MSVCRT__errno() = MSVCRT_ENOSPC;
2339 unsigned int i, j, nr_lf;
2342 const char *s = buf, *buf_start = buf;
2343 /* find number of \n ( without preceding \r ) */
2344 for ( nr_lf=0,i = 0; i <count; i++)
2349 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2354 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2356 for (s = buf, i = 0, j = 0; i < count; i++)
2361 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2368 FIXME("Malloc failed\n");
2376 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2378 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2379 fd, hand, GetLastError(), num_written);
2380 *MSVCRT__errno() = MSVCRT_ENOSPC;
2383 return s - buf_start;
2395 /*********************************************************************
2398 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2401 len = MSVCRT__write(file->_file, &val, sizeof(val));
2402 if (len == sizeof(val)) return val;
2403 file->_flag |= MSVCRT__IOERR;
2407 /*********************************************************************
2410 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2415 MSVCRT_free(file->_tmpfname);
2416 file->_tmpfname = NULL;
2417 /* flush stdio buffers */
2418 if(file->_flag & MSVCRT__IOWRT)
2419 MSVCRT_fflush(file);
2420 if(file->_flag & MSVCRT__IOMYBUF)
2421 MSVCRT_free(file->_base);
2423 r=MSVCRT__close(file->_file);
2427 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2430 /*********************************************************************
2433 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2435 return file->_flag & MSVCRT__IOEOF;
2438 /*********************************************************************
2441 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2443 return file->_flag & MSVCRT__IOERR;
2446 /*********************************************************************
2447 * _filbuf (MSVCRT.@)
2449 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2451 /* Allocate buffer if needed */
2452 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2453 msvcrt_alloc_buffer(file);
2455 if(!(file->_flag & MSVCRT__IOREAD)) {
2456 if(file->_flag & MSVCRT__IORW) {
2457 file->_flag |= MSVCRT__IOREAD;
2462 if(file->_flag & MSVCRT__IONBF) {
2465 if ((r = read_i(file->_file,&c,1)) != 1) {
2466 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2471 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2473 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2478 file->_ptr = file->_base+1;
2479 return *(unsigned char *)file->_base;
2483 /*********************************************************************
2486 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2492 i = (unsigned char *)file->_ptr++;
2495 j = MSVCRT__filbuf(file);
2499 /*********************************************************************
2500 * _fgetchar (MSVCRT.@)
2502 int CDECL _fgetchar(void)
2504 return MSVCRT_fgetc(MSVCRT_stdin);
2507 /*********************************************************************
2510 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2512 int cc = MSVCRT_EOF;
2513 char * buf_start = s;
2515 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2516 file,file->_file,s,size);
2518 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2523 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2525 TRACE(":nothing read\n");
2528 if ((cc != MSVCRT_EOF) && (size > 1))
2531 TRACE(":got %s\n", debugstr_a(buf_start));
2535 /*********************************************************************
2538 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2539 * the CR from CR/LF combinations
2541 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2545 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2552 for(i=0; i<sizeof(wc); i++)
2562 j = MSVCRT__filbuf(file);
2565 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2575 c = MSVCRT_fgetc(file);
2576 if ((get_locale()->locinfo->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2578 FIXME("Treat Multibyte characters\n");
2580 if (c == MSVCRT_EOF)
2583 return (MSVCRT_wint_t)c;
2586 /*********************************************************************
2589 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2595 for (j=0; j<sizeof(int); j++) {
2596 k = MSVCRT_fgetc(file);
2597 if (k == MSVCRT_EOF) {
2598 file->_flag |= MSVCRT__IOEOF;
2606 /*********************************************************************
2609 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2611 return MSVCRT_fgetwc(file);
2614 /*********************************************************************
2615 * _fgetwchar (MSVCRT.@)
2617 MSVCRT_wint_t CDECL _fgetwchar(void)
2619 return MSVCRT_fgetwc(MSVCRT_stdin);
2622 /*********************************************************************
2623 * getwchar (MSVCRT.@)
2625 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2627 return _fgetwchar();
2630 /*********************************************************************
2633 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2635 int cc = MSVCRT_WEOF;
2636 MSVCRT_wchar_t * buf_start = s;
2638 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2639 file,file->_file,s,size);
2641 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2646 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2648 TRACE(":nothing read\n");
2651 if ((cc != MSVCRT_WEOF) && (size > 1))
2654 TRACE(":got %s\n", debugstr_w(buf_start));
2658 /*********************************************************************
2661 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2663 MSVCRT_size_t wrcnt=size * nmemb;
2668 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2669 memcpy(file->_ptr, ptr, pcnt);
2674 ptr = (const char*)ptr + pcnt;
2675 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2676 if(file->_flag & MSVCRT__IORW) {
2677 file->_flag |= MSVCRT__IOWRT;
2683 int res=msvcrt_flush_buffer(file);
2685 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2688 file->_flag |= MSVCRT__IOERR;
2691 written += pwritten;
2694 return written / size;
2697 /*********************************************************************
2700 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2702 MSVCRT_wchar_t mwc=wc;
2703 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2708 /*********************************************************************
2709 * _fputwchar (MSVCRT.@)
2711 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2713 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2716 /*********************************************************************
2717 * _wfsopen (MSVCRT.@)
2719 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2722 int open_flags, stream_flags, fd;
2724 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2726 /* map mode string to open() flags. "man fopen" for possibilities. */
2727 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2731 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2734 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2736 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2743 TRACE(":got (%p)\n",file);
2744 if (fd >= 0 && !file)
2750 /*********************************************************************
2751 * _fsopen (MSVCRT.@)
2753 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2756 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2758 if (path && !(pathW = msvcrt_wstrdupa(path))) {
2759 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2760 *MSVCRT__errno() = MSVCRT_EINVAL;
2763 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2766 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2767 *MSVCRT__errno() = MSVCRT_EINVAL;
2771 ret = MSVCRT__wfsopen(pathW, modeW, share);
2778 /*********************************************************************
2781 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2783 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2786 /*********************************************************************
2787 * fopen_s (MSVCRT.@)
2789 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
2790 const char *filename, const char *mode)
2792 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2793 !MSVCRT_CHECK_PMT(mode != NULL)) {
2794 *MSVCRT__errno() = MSVCRT_EINVAL;
2795 return MSVCRT_EINVAL;
2798 *pFile = MSVCRT_fopen(filename, mode);
2801 return *MSVCRT__errno();
2805 /*********************************************************************
2806 * _wfopen (MSVCRT.@)
2808 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2810 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2813 /*********************************************************************
2814 * _wfopen_s (MSVCRT.@)
2816 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
2817 const MSVCRT_wchar_t *mode)
2819 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2820 !MSVCRT_CHECK_PMT(mode != NULL)) {
2821 *MSVCRT__errno() = MSVCRT_EINVAL;
2822 return MSVCRT_EINVAL;
2825 *pFile = MSVCRT__wfopen(filename, mode);
2828 return *MSVCRT__errno();
2832 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2833 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2835 /*********************************************************************
2838 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2845 int res = msvcrt_flush_buffer(file);
2846 return res ? res : c;
2851 return MSVCRT__flsbuf(c, file);
2855 /*********************************************************************
2856 * _flsbuf (MSVCRT.@)
2858 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2860 /* Flush output buffer */
2861 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2862 msvcrt_alloc_buffer(file);
2864 if(!(file->_flag & MSVCRT__IOWRT)) {
2865 if(file->_flag & MSVCRT__IORW) {
2866 file->_flag |= MSVCRT__IOWRT;
2872 int res=msvcrt_flush_buffer(file);
2873 return res?res : MSVCRT_fputc(c, file);
2877 /* set _cnt to 0 for unbuffered FILEs */
2879 len = MSVCRT__write(file->_file, &cc, 1);
2880 if (len == 1) return c & 0xff;
2881 file->_flag |= MSVCRT__IOERR;
2886 /*********************************************************************
2887 * _fputchar (MSVCRT.@)
2889 int CDECL _fputchar(int c)
2891 return MSVCRT_fputc(c, MSVCRT_stdout);
2894 /*********************************************************************
2897 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2898 { MSVCRT_size_t rcnt=size * nmemb;
2899 MSVCRT_size_t read=0;
2905 /* first buffered data */
2907 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2908 memcpy(ptr, file->_ptr, pcnt);
2913 ptr = (char*)ptr + pcnt;
2914 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2915 if(file->_flag & MSVCRT__IORW) {
2916 file->_flag |= MSVCRT__IOREAD;
2923 /* Fill the buffer on small reads.
2924 * TODO: Use a better buffering strategy.
2926 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2927 if (file->_bufsiz == 0) {
2928 msvcrt_alloc_buffer(file);
2930 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2931 file->_ptr = file->_base;
2932 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2933 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2934 if (i > 0 && i < file->_cnt) {
2935 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2936 file->_flag &= ~MSVCRT__IOEOF;
2939 memcpy(ptr, file->_ptr, i);
2944 i = MSVCRT__read(file->_file,ptr, rcnt);
2948 ptr = (char *)ptr+i;
2949 /* expose feof condition in the flags
2950 * MFC tests file->_flag for feof, and doesn't call feof())
2952 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2953 file->_flag |= MSVCRT__IOEOF;
2956 file->_flag |= MSVCRT__IOERR;
2966 /*********************************************************************
2967 * _wfreopen (MSVCRT.@)
2970 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
2972 int open_flags, stream_flags, fd;
2974 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2977 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2981 MSVCRT_fclose(file);
2982 /* map mode string to open() flags. "man fopen" for possibilities. */
2983 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2987 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2990 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2993 WARN(":failed-last error (%d)\n",GetLastError());
2994 msvcrt_set_errno(GetLastError());
3003 /*********************************************************************
3004 * freopen (MSVCRT.@)
3007 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3010 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3012 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3013 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3019 ret = MSVCRT__wfreopen(pathW, modeW, file);
3026 /*********************************************************************
3027 * fsetpos (MSVCRT.@)
3029 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3031 /* Note that all this has been lifted 'as is' from fseek */
3032 if(file->_flag & MSVCRT__IOWRT)
3033 msvcrt_flush_buffer(file);
3035 /* Discard buffered input */
3037 file->_ptr = file->_base;
3039 /* Reset direction of i/o */
3040 if(file->_flag & MSVCRT__IORW) {
3041 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3044 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3047 /*********************************************************************
3048 * _ftelli64 (MSVCRT.@)
3050 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3052 /* TODO: just call fgetpos and return lower half of result */
3055 pos = _telli64(file->_file);
3056 if(pos == -1) return -1;
3058 if( file->_flag & MSVCRT__IOWRT ) {
3059 off = file->_ptr - file->_base;
3062 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3063 /* Black magic correction for CR removal */
3065 for (i=0; i<file->_cnt; i++) {
3066 if (file->_ptr[i] == '\n')
3069 /* Black magic when reading CR at buffer boundary*/
3070 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3079 /*********************************************************************
3082 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3084 return MSVCRT__ftelli64(file);
3087 /*********************************************************************
3088 * fgetpos (MSVCRT.@)
3090 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3093 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3094 if(*pos == -1) return -1;
3096 if( file->_flag & MSVCRT__IOWRT ) {
3097 off = file->_ptr - file->_base;
3100 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3101 /* Black magic correction for CR removal */
3103 for (i=0; i<file->_cnt; i++) {
3104 if (file->_ptr[i] == '\n')
3107 /* Black magic when reading CR at buffer boundary*/
3108 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3117 /*********************************************************************
3120 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3122 MSVCRT_size_t i, len = strlen(s);
3123 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3124 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3125 for (i=0; i<len; i++)
3126 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
3131 /*********************************************************************
3134 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3136 MSVCRT_size_t i, len = strlenW(s);
3137 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3138 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3139 for (i=0; i<len; i++)
3141 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3143 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
3149 /*********************************************************************
3150 * getchar (MSVCRT.@)
3152 int CDECL MSVCRT_getchar(void)
3154 return MSVCRT_fgetc(MSVCRT_stdin);
3157 /*********************************************************************
3160 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3162 return MSVCRT_fgetc(file);
3165 /*********************************************************************
3168 char * CDECL MSVCRT_gets(char *buf)
3171 char * buf_start = buf;
3173 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3174 cc = MSVCRT_fgetc(MSVCRT_stdin))
3175 if(cc != '\r') *buf++ = (char)cc;
3179 TRACE("got '%s'\n", buf_start);
3183 /*********************************************************************
3186 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3189 MSVCRT_wchar_t* ws = buf;
3191 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3192 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3195 *buf++ = (MSVCRT_wchar_t)cc;
3199 TRACE("got %s\n", debugstr_w(ws));
3203 /*********************************************************************
3206 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3208 return MSVCRT_fputc(c, file);
3211 /*********************************************************************
3212 * putchar (MSVCRT.@)
3214 int CDECL MSVCRT_putchar(int c)
3216 return MSVCRT_fputc(c, MSVCRT_stdout);
3219 /*********************************************************************
3220 * _putwch (MSVCRT.@)
3222 int CDECL MSVCRT__putwch(int c)
3224 return MSVCRT_fputwc(c, MSVCRT_stdout);
3227 /*********************************************************************
3230 int CDECL MSVCRT_puts(const char *s)
3232 MSVCRT_size_t len = strlen(s);
3233 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3234 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3237 /*********************************************************************
3240 int CDECL _putws(const MSVCRT_wchar_t *s)
3242 static const MSVCRT_wchar_t nl = '\n';
3243 MSVCRT_size_t len = strlenW(s);
3244 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3245 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3248 /*********************************************************************
3251 int CDECL MSVCRT_remove(const char *path)
3253 TRACE("(%s)\n",path);
3254 if (DeleteFileA(path))
3256 TRACE(":failed (%d)\n",GetLastError());
3257 msvcrt_set_errno(GetLastError());
3261 /*********************************************************************
3262 * _wremove (MSVCRT.@)
3264 int CDECL _wremove(const MSVCRT_wchar_t *path)
3266 TRACE("(%s)\n",debugstr_w(path));
3267 if (DeleteFileW(path))
3269 TRACE(":failed (%d)\n",GetLastError());
3270 msvcrt_set_errno(GetLastError());
3274 /*********************************************************************
3277 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3279 TRACE(":from %s to %s\n",oldpath,newpath);
3280 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3282 TRACE(":failed (%d)\n",GetLastError());
3283 msvcrt_set_errno(GetLastError());
3287 /*********************************************************************
3288 * _wrename (MSVCRT.@)
3290 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3292 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3293 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3295 TRACE(":failed (%d)\n",GetLastError());
3296 msvcrt_set_errno(GetLastError());
3300 /*********************************************************************
3301 * setvbuf (MSVCRT.@)
3303 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3305 /* TODO: Check if file busy */
3307 MSVCRT_free(file->_base);
3311 if(mode == MSVCRT__IOFBF) {
3312 file->_flag &= ~MSVCRT__IONBF;
3313 file->_base = file->_ptr = buf;
3315 file->_bufsiz = size;
3318 file->_flag |= MSVCRT__IONBF;
3323 /*********************************************************************
3326 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3328 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3331 /*********************************************************************
3334 char * CDECL MSVCRT_tmpnam(char *s)
3342 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3343 p = s + sprintf(s, "\\s%s.", tmpstr);
3344 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3346 msvcrt_int_to_base32(unique++, tmpstr);
3348 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3349 GetLastError() == ERROR_FILE_NOT_FOUND)
3355 /*********************************************************************
3356 * tmpfile (MSVCRT.@)
3358 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3360 char *filename = MSVCRT_tmpnam(NULL);
3362 MSVCRT_FILE* file = NULL;
3365 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3366 if (fd != -1 && (file = msvcrt_alloc_fp()))
3368 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3373 else file->_tmpfname = _strdup(filename);
3379 /*********************************************************************
3380 * vfprintf (MSVCRT.@)
3382 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3385 LPWSTR formatW = NULL;
3388 int written, retval;
3390 out.unicode = FALSE;
3391 out.buf.A = out.grow.A = buf;
3393 out.len = sizeof(buf);
3395 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
3396 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
3397 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
3399 if ((written = pf_vsnprintf( &out, formatW, NULL, FALSE, valist )) >= 0)
3401 retval = MSVCRT_fwrite(out.buf.A, sizeof(*out.buf.A), written, file);
3405 HeapFree( GetProcessHeap(), 0, formatW );
3407 if (out.buf.A != out.grow.A)
3408 MSVCRT_free (out.buf.A);
3412 /*********************************************************************
3413 * vfprintf_s (MSVCRT.@)
3415 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3418 LPWSTR formatW = NULL;
3421 int written, retval;
3423 if( !MSVCRT_CHECK_PMT( file != NULL ) )
3425 *MSVCRT__errno() = MSVCRT_EINVAL;
3429 out.unicode = FALSE;
3430 out.buf.A = out.grow.A = buf;
3432 out.len = sizeof(buf);
3434 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
3435 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
3436 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
3438 if ((written = pf_vsnprintf( &out, formatW, NULL, TRUE, valist )) >= 0)
3440 retval = MSVCRT_fwrite(out.buf.A, sizeof(*out.buf.A), written, file);
3444 HeapFree( GetProcessHeap(), 0, formatW );
3446 if (out.buf.A != out.grow.A)
3447 MSVCRT_free (out.buf.A);
3451 /*********************************************************************
3452 * vfwprintf (MSVCRT.@)
3454 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3456 MSVCRT_wchar_t buf[2048];
3458 int written, retval;
3461 out.buf.W = out.grow.W = buf;
3463 out.len = sizeof(buf) / sizeof(buf[0]);
3465 if ((written = pf_vsnprintf( &out, format, NULL, FALSE, valist )) >= 0)
3467 retval = MSVCRT_fwrite(out.buf.W, sizeof(*out.buf.W), written, file);
3470 if (out.buf.W != out.grow.W)
3471 MSVCRT_free (out.buf.W);
3475 /*********************************************************************
3476 * vfwprintf_s (MSVCRT.@)
3478 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3480 MSVCRT_wchar_t buf[2048];
3482 int written, retval;
3484 if( !MSVCRT_CHECK_PMT( file != NULL ) )
3486 *MSVCRT__errno() = MSVCRT_EINVAL;
3491 out.buf.W = out.grow.W = buf;
3493 out.len = sizeof(buf) / sizeof(buf[0]);
3495 if ((written = pf_vsnprintf( &out, format, NULL, TRUE, valist )) >= 0)
3497 retval = MSVCRT_fwrite(out.buf.W, sizeof(*out.buf.W), written, file);
3500 if (out.buf.W != out.grow.W)
3501 MSVCRT_free (out.buf.W);
3505 /*********************************************************************
3506 * vprintf (MSVCRT.@)
3508 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3510 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3513 /*********************************************************************
3514 * vprintf_s (MSVCRT.@)
3516 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
3518 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
3521 /*********************************************************************
3522 * vwprintf (MSVCRT.@)
3524 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3526 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3529 /*********************************************************************
3530 * vwprintf_s (MSVCRT.@)
3532 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
3534 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
3537 /*********************************************************************
3538 * fprintf (MSVCRT.@)
3540 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3542 __ms_va_list valist;
3544 __ms_va_start(valist, format);
3545 res = MSVCRT_vfprintf(file, format, valist);
3546 __ms_va_end(valist);
3550 /*********************************************************************
3551 * fprintf_s (MSVCRT.@)
3553 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
3555 __ms_va_list valist;
3557 __ms_va_start(valist, format);
3558 res = MSVCRT_vfprintf_s(file, format, valist);
3559 __ms_va_end(valist);
3563 /*********************************************************************
3564 * fwprintf (MSVCRT.@)
3566 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3568 __ms_va_list valist;
3570 __ms_va_start(valist, format);
3571 res = MSVCRT_vfwprintf(file, format, valist);
3572 __ms_va_end(valist);
3576 /*********************************************************************
3577 * fwprintf_s (MSVCRT.@)
3579 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3581 __ms_va_list valist;
3583 __ms_va_start(valist, format);
3584 res = MSVCRT_vfwprintf_s(file, format, valist);
3585 __ms_va_end(valist);
3589 /*********************************************************************
3592 int CDECL MSVCRT_printf(const char *format, ...)
3594 __ms_va_list valist;
3596 __ms_va_start(valist, format);
3597 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3598 __ms_va_end(valist);
3602 /*********************************************************************
3603 * printf_s (MSVCRT.@)
3605 int CDECL MSVCRT_printf_s(const char *format, ...)
3607 __ms_va_list valist;
3609 __ms_va_start(valist, format);
3610 res = MSVCRT_vprintf_s(format, valist);
3611 __ms_va_end(valist);
3615 /*********************************************************************
3618 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3620 if (c == MSVCRT_EOF)
3622 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3623 msvcrt_alloc_buffer(file);
3626 if(file->_ptr>file->_base) {
3630 MSVCRT_clearerr(file);
3636 /*********************************************************************
3637 * ungetwc (MSVCRT.@)
3639 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3641 MSVCRT_wchar_t mwc = wc;
3642 char * pp = (char *)&mwc;
3644 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3645 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3651 /*********************************************************************
3652 * wprintf (MSVCRT.@)
3654 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3656 __ms_va_list valist;
3658 __ms_va_start(valist, format);
3659 res = MSVCRT_vwprintf(format, valist);
3660 __ms_va_end(valist);
3664 /*********************************************************************
3665 * wprintf_s (MSVCRT.@)
3667 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
3669 __ms_va_list valist;
3671 __ms_va_start(valist, format);
3672 res = MSVCRT_vwprintf_s(format, valist);
3673 __ms_va_end(valist);
3677 /*********************************************************************
3678 * _getmaxstdio (MSVCRT.@)
3680 int CDECL _getmaxstdio(void)
3682 FIXME("stub, always returns 512\n");
3686 /*********************************************************************
3687 * _setmaxstdio_ (MSVCRT.@)
3689 int CDECL _setmaxstdio(int newmax)
3696 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3700 /*********************************************************************
3701 * __pioinfo (MSVCRT.@)
3702 * FIXME: see MSVCRT_MAX_FILES define.
3704 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3705 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3706 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3707 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3708 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3709 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3710 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3711 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3712 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3713 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3714 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3715 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3718 /*********************************************************************
3719 * __badioinfo (MSVCRT.@)
3721 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };