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 data for tmpnam and _wtmpname functions */
92 static int tmpnam_unique;
93 static char MSVCRT_tmpname[MAX_PATH];
94 static MSVCRT_wchar_t MSVCRT_wtmpname[MAX_PATH];
96 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
97 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
98 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
99 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
101 #define TOUL(x) (ULONGLONG)(x)
102 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
103 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
104 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
105 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
107 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
108 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
109 * and MSVCRT_stream_idx, from race conditions.
110 * It doesn't protect against race conditions manipulating the underlying files
111 * or flags; doing so would probably be better accomplished with per-file
112 * protection, rather than locking the whole table for every change.
114 static CRITICAL_SECTION MSVCRT_file_cs;
115 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
116 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
118 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
120 buf->st_dev = buf64->st_dev;
121 buf->st_ino = buf64->st_ino;
122 buf->st_mode = buf64->st_mode;
123 buf->st_nlink = buf64->st_nlink;
124 buf->st_uid = buf64->st_uid;
125 buf->st_gid = buf64->st_gid;
126 buf->st_rdev = buf64->st_rdev;
127 buf->st_size = buf64->st_size;
128 buf->st_atime = buf64->st_atime;
129 buf->st_mtime = buf64->st_mtime;
130 buf->st_ctime = buf64->st_ctime;
133 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
135 buf->st_dev = buf64->st_dev;
136 buf->st_ino = buf64->st_ino;
137 buf->st_mode = buf64->st_mode;
138 buf->st_nlink = buf64->st_nlink;
139 buf->st_uid = buf64->st_uid;
140 buf->st_gid = buf64->st_gid;
141 buf->st_rdev = buf64->st_rdev;
142 buf->st_size = buf64->st_size;
143 buf->st_atime = buf64->st_atime;
144 buf->st_mtime = buf64->st_mtime;
145 buf->st_ctime = buf64->st_ctime;
148 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
150 /* 1601 to 1970 is 369 years plus 89 leap days */
151 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
153 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
154 ft->dwHighDateTime = ticks >> 32;
155 ft->dwLowDateTime = ticks;
158 static inline BOOL msvcrt_is_valid_fd(int fd)
160 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
163 /* INTERNAL: Get the HANDLE for a fd
164 * This doesn't lock the table, because a failure will result in
165 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
166 * it returns a valid handle which is about to be closed, a subsequent call
167 * will fail, most likely in a sane way.
169 static HANDLE msvcrt_fdtoh(int fd)
171 if (!msvcrt_is_valid_fd(fd))
173 WARN(":fd (%d) - no handle!\n",fd);
174 *MSVCRT___doserrno() = 0;
175 *MSVCRT__errno() = MSVCRT_EBADF;
176 return INVALID_HANDLE_VALUE;
178 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
179 return MSVCRT_fdesc[fd].handle;
182 /* INTERNAL: free a file entry fd */
183 static void msvcrt_free_fd(int fd)
188 old_handle = MSVCRT_fdesc[fd].handle;
189 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
190 MSVCRT_fdesc[fd].wxflag = 0;
191 TRACE(":fd (%d) freed\n",fd);
192 if (fd < 3) /* don't use 0,1,2 for user files */
197 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
200 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
203 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
209 if (fd == MSVCRT_fdend - 1)
211 if (fd < MSVCRT_fdstart)
217 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
218 /* caller must hold the files lock */
219 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
221 if (fd >= MSVCRT_MAX_FILES)
223 WARN(":files exhausted!\n");
224 *MSVCRT__errno() = MSVCRT_ENFILE;
227 MSVCRT_fdesc[fd].handle = hand;
228 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
230 /* locate next free slot */
231 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
232 MSVCRT_fdstart = MSVCRT_fdend + 1;
234 while (MSVCRT_fdstart < MSVCRT_fdend &&
235 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
237 /* update last fd in use */
238 if (fd >= MSVCRT_fdend)
239 MSVCRT_fdend = fd + 1;
240 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
244 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
245 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
246 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
252 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
253 static int msvcrt_alloc_fd(HANDLE hand, int flag)
258 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
259 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
264 /* INTERNAL: Allocate a FILE* for an fd slot */
265 /* caller must hold the files lock */
266 static MSVCRT_FILE* msvcrt_alloc_fp(void)
270 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
272 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
274 if (!MSVCRT_fstreams[i])
276 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
278 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
280 return MSVCRT_fstreams[i];
286 /* INTERNAL: initialize a FILE* from an open fd */
287 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
289 TRACE(":fd (%d) allocating FILE*\n",fd);
290 if (!msvcrt_is_valid_fd(fd))
292 WARN(":invalid fd %d\n",fd);
293 *MSVCRT___doserrno() = 0;
294 *MSVCRT__errno() = MSVCRT_EBADF;
297 memset(file, 0, sizeof(*file));
299 file->_flag = stream_flags;
301 TRACE(":got FILE* (%p)\n",file);
305 /* INTERNAL: Create an inheritance data block (for spawned process)
306 * The inheritance block is made of:
307 * 00 int nb of file descriptor (NBFD)
308 * 04 char file flags (wxflag): repeated for each fd
309 * 4+NBFD HANDLE file handle: repeated for each fd
311 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
317 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
318 *block = MSVCRT_calloc(*size, 1);
324 wxflag_ptr = (char*)*block + sizeof(unsigned);
325 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
327 *(unsigned*)*block = MSVCRT_fdend;
328 for (fd = 0; fd < MSVCRT_fdend; fd++)
330 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
331 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
333 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
334 *handle_ptr = MSVCRT_fdesc[fd].handle;
339 *handle_ptr = INVALID_HANDLE_VALUE;
341 wxflag_ptr++; handle_ptr++;
346 /* INTERNAL: Set up all file descriptors,
347 * as well as default streams (stdin, stderr and stdout)
349 void msvcrt_init_io(void)
354 InitializeCriticalSection(&MSVCRT_file_cs);
355 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
356 GetStartupInfoA(&si);
357 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
363 count = *(unsigned*)si.lpReserved2;
364 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
365 handle_ptr = (HANDLE*)(wxflag_ptr + count);
367 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
368 count = min(count, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
369 for (i = 0; i < count; i++)
371 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
373 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
374 MSVCRT_fdesc[i].handle = *handle_ptr;
378 MSVCRT_fdesc[i].wxflag = 0;
379 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
381 wxflag_ptr++; handle_ptr++;
383 MSVCRT_fdend = max( 3, count );
384 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
385 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
388 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
390 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
391 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
392 GetCurrentProcess(), &MSVCRT_fdesc[0].handle,
393 0, TRUE, DUPLICATE_SAME_ACCESS))
394 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
396 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
398 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
399 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
400 GetCurrentProcess(), &MSVCRT_fdesc[1].handle,
401 0, TRUE, DUPLICATE_SAME_ACCESS))
402 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
404 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
406 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
407 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
408 GetCurrentProcess(), &MSVCRT_fdesc[2].handle,
409 0, TRUE, DUPLICATE_SAME_ACCESS))
410 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
413 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
414 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
416 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
417 for (i = 0; i < 3; i++)
419 /* FILE structs for stdin/out/err are static and never deleted */
420 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
421 MSVCRT__iob[i]._file = i;
422 MSVCRT__iob[i]._tmpfname = NULL;
423 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
425 MSVCRT_stream_idx = 3;
428 /* INTERNAL: Flush stdio file buffer */
429 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
432 int cnt=file->_ptr-file->_base;
433 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
434 file->_flag |= MSVCRT__IOERR;
437 file->_ptr=file->_base;
438 file->_cnt=file->_bufsiz;
443 /* INTERNAL: Allocate stdio file buffer */
444 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
446 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
448 file->_bufsiz = MSVCRT_BUFSIZ;
449 file->_flag |= MSVCRT__IOMYBUF;
451 file->_base = (char*)(&file->_charbuf);
453 file->_bufsiz = sizeof(file->_charbuf);
455 file->_ptr = file->_base;
459 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
460 static int msvcrt_int_to_base32(int num, char *str)
475 *p = (num & 31) + '0';
477 *p += ('a' - '0' - 10);
484 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
485 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
500 *p = (num & 31) + '0';
502 *p += ('a' - '0' - 10);
509 /*********************************************************************
510 * __iob_func(MSVCRT.@)
512 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
514 return &MSVCRT__iob[0];
517 /*********************************************************************
520 int CDECL MSVCRT__access(const char *filename, int mode)
522 DWORD attr = GetFileAttributesA(filename);
524 TRACE("(%s,%d) %d\n",filename,mode,attr);
526 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
528 msvcrt_set_errno(GetLastError());
531 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
533 msvcrt_set_errno(ERROR_ACCESS_DENIED);
539 /*********************************************************************
540 * _access_s (MSVCRT.@)
542 int CDECL _access_s(const char *filename, int mode)
544 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
545 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
547 *MSVCRT__errno() = MSVCRT_EINVAL;
551 return MSVCRT__access(filename, mode);
554 /*********************************************************************
555 * _waccess (MSVCRT.@)
557 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
559 DWORD attr = GetFileAttributesW(filename);
561 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
563 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
565 msvcrt_set_errno(GetLastError());
568 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
570 msvcrt_set_errno(ERROR_ACCESS_DENIED);
576 /*********************************************************************
577 * _waccess_s (MSVCRT.@)
579 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
581 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
582 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
584 *MSVCRT__errno() = MSVCRT_EINVAL;
588 return _waccess(filename, mode);
591 /*********************************************************************
594 int CDECL MSVCRT__chmod(const char *path, int flags)
596 DWORD oldFlags = GetFileAttributesA(path);
598 if (oldFlags != INVALID_FILE_ATTRIBUTES)
600 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
601 oldFlags | FILE_ATTRIBUTE_READONLY;
603 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
606 msvcrt_set_errno(GetLastError());
610 /*********************************************************************
613 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
615 DWORD oldFlags = GetFileAttributesW(path);
617 if (oldFlags != INVALID_FILE_ATTRIBUTES)
619 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
620 oldFlags | FILE_ATTRIBUTE_READONLY;
622 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
625 msvcrt_set_errno(GetLastError());
629 /*********************************************************************
632 int CDECL MSVCRT__unlink(const char *path)
634 TRACE("%s\n",debugstr_a(path));
635 if(DeleteFileA(path))
637 TRACE("failed (%d)\n",GetLastError());
638 msvcrt_set_errno(GetLastError());
642 /*********************************************************************
643 * _wunlink (MSVCRT.@)
645 int CDECL _wunlink(const MSVCRT_wchar_t *path)
647 TRACE("(%s)\n",debugstr_w(path));
648 if(DeleteFileW(path))
650 TRACE("failed (%d)\n",GetLastError());
651 msvcrt_set_errno(GetLastError());
655 /* _flushall calls MSVCRT_fflush which calls _flushall */
656 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
658 /*********************************************************************
659 * _flushall (MSVCRT.@)
661 int CDECL _flushall(void)
663 int i, num_flushed = 0;
666 for (i = 3; i < MSVCRT_stream_idx; i++)
667 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
670 /* FIXME: flush, do not commit */
671 if (_commit(i) == -1)
672 if (MSVCRT_fstreams[i])
673 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
675 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
676 MSVCRT_fflush(MSVCRT_fstreams[i]);
682 TRACE(":flushed (%d) handles\n",num_flushed);
686 /*********************************************************************
689 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
693 } else if(file->_flag & MSVCRT__IOWRT) {
694 int res=msvcrt_flush_buffer(file);
700 /*********************************************************************
703 int CDECL MSVCRT__close(int fd)
709 hand = msvcrt_fdtoh(fd);
710 TRACE(":fd (%d) handle (%p)\n",fd,hand);
711 if (hand == INVALID_HANDLE_VALUE)
713 else if (!CloseHandle(hand))
715 WARN(":failed-last error (%d)\n",GetLastError());
716 msvcrt_set_errno(GetLastError());
729 /*********************************************************************
732 int CDECL _commit(int fd)
734 HANDLE hand = msvcrt_fdtoh(fd);
736 TRACE(":fd (%d) handle (%p)\n",fd,hand);
737 if (hand == INVALID_HANDLE_VALUE)
740 if (!FlushFileBuffers(hand))
742 if (GetLastError() == ERROR_INVALID_HANDLE)
744 /* FlushFileBuffers fails for console handles
745 * so we ignore this error.
749 TRACE(":failed-last error (%d)\n",GetLastError());
750 msvcrt_set_errno(GetLastError());
757 /*********************************************************************
760 * MSDN isn't clear on this point, but the remarks for _pipe
761 * indicate file descriptors duplicated with _dup and _dup2 are always
764 int CDECL MSVCRT__dup2(int od, int nd)
768 TRACE("(od=%d, nd=%d)\n", od, nd);
770 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
774 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
775 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
777 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
779 if (msvcrt_is_valid_fd(nd))
781 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
785 *MSVCRT__errno() = MSVCRT_EMFILE;
789 /* _dup2 returns 0, not nd, on success */
796 msvcrt_set_errno(GetLastError());
801 *MSVCRT__errno() = MSVCRT_EBADF;
808 /*********************************************************************
811 int CDECL MSVCRT__dup(int od)
817 if (MSVCRT__dup2(od, fd) == 0)
825 /*********************************************************************
828 int CDECL _eof(int fd)
831 LONG hcurpos,hendpos;
832 HANDLE hand = msvcrt_fdtoh(fd);
834 TRACE(":fd (%d) handle (%p)\n",fd,hand);
836 if (hand == INVALID_HANDLE_VALUE)
839 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
841 /* Otherwise we do it the hard way */
842 hcurpos = hendpos = 0;
843 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
844 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
846 if (curpos == endpos && hcurpos == hendpos)
848 /* FIXME: shouldn't WX_ATEOF be set here? */
852 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
856 /*********************************************************************
857 * _fcloseall (MSVCRT.@)
859 int CDECL MSVCRT__fcloseall(void)
861 int num_closed = 0, i;
864 for (i = 3; i < MSVCRT_stream_idx; i++)
865 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
866 !MSVCRT_fclose(MSVCRT_fstreams[i]))
870 TRACE(":closed (%d) handles\n",num_closed);
874 /* free everything on process exit */
875 void msvcrt_free_io(void)
878 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
879 * stdout, and stderr (unlike GNU), so we need to fclose() them here
880 * or they won't get flushed.
882 MSVCRT_fclose(&MSVCRT__iob[0]);
883 MSVCRT_fclose(&MSVCRT__iob[1]);
884 MSVCRT_fclose(&MSVCRT__iob[2]);
885 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
886 DeleteCriticalSection(&MSVCRT_file_cs);
889 /*********************************************************************
890 * _lseeki64 (MSVCRT.@)
892 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
894 HANDLE hand = msvcrt_fdtoh(fd);
897 TRACE(":fd (%d) handle (%p)\n",fd,hand);
898 if (hand == INVALID_HANDLE_VALUE)
901 if (whence < 0 || whence > 2)
903 *MSVCRT__errno() = MSVCRT_EINVAL;
907 TRACE(":fd (%d) to %s pos %s\n",
908 fd,wine_dbgstr_longlong(offset),
909 (whence==SEEK_SET)?"SEEK_SET":
910 (whence==SEEK_CUR)?"SEEK_CUR":
911 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
913 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
914 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
915 ofs.QuadPart = offset;
916 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
917 GetLastError() == ERROR_SUCCESS)
919 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
920 /* FIXME: What if we seek _to_ EOF - is EOF set? */
924 TRACE(":error-last error (%d)\n",GetLastError());
925 msvcrt_set_errno(GetLastError());
929 /*********************************************************************
932 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
934 return MSVCRT__lseeki64(fd, offset, whence);
937 /*********************************************************************
938 * _lock_file (MSVCRT.@)
940 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
942 FIXME("(%p) stub\n",file);
945 /*********************************************************************
946 * _unlock_file (MSVCRT.@)
948 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
950 FIXME("(%p) stub\n",file);
953 /*********************************************************************
954 * _locking (MSVCRT.@)
956 * This is untested; the underlying LockFile doesn't work yet.
958 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
962 HANDLE hand = msvcrt_fdtoh(fd);
964 TRACE(":fd (%d) handle (%p)\n",fd,hand);
965 if (hand == INVALID_HANDLE_VALUE)
968 if (mode < 0 || mode > 4)
970 *MSVCRT__errno() = MSVCRT_EINVAL;
974 TRACE(":fd (%d) by 0x%08x mode %s\n",
975 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
976 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
977 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
978 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
979 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
982 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
984 FIXME ("Seek failed\n");
985 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
988 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
991 ret = 1; /* just to satisfy gcc */
994 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
999 else if (mode == MSVCRT__LK_UNLCK)
1000 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1002 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1003 /* FIXME - what about error settings? */
1004 return ret ? 0 : -1;
1007 /*********************************************************************
1008 * _fseeki64 (MSVCRT.@)
1010 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1012 /* Flush output if needed */
1013 if(file->_flag & MSVCRT__IOWRT)
1014 msvcrt_flush_buffer(file);
1016 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1017 offset -= file->_cnt;
1018 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
1019 /* Black magic correction for CR removal */
1021 for (i=0; i<file->_cnt; i++) {
1022 if (file->_ptr[i] == '\n')
1025 /* Black magic when reading CR at buffer boundary*/
1026 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
1030 /* Discard buffered input */
1032 file->_ptr = file->_base;
1033 /* Reset direction of i/o */
1034 if(file->_flag & MSVCRT__IORW) {
1035 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1037 /* Clear end of file flag */
1038 file->_flag &= ~MSVCRT__IOEOF;
1039 return (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1042 /*********************************************************************
1045 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1047 return MSVCRT__fseeki64( file, offset, whence );
1050 /*********************************************************************
1051 * _chsize (MSVCRT.@)
1053 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1059 TRACE("(fd=%d, size=%d)\n", fd, size);
1063 handle = msvcrt_fdtoh(fd);
1064 if (handle != INVALID_HANDLE_VALUE)
1066 /* save the current file pointer */
1067 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1070 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1073 ret = SetEndOfFile(handle);
1074 if (!ret) msvcrt_set_errno(GetLastError());
1077 /* restore the file pointer */
1078 MSVCRT__lseek(fd, cur, SEEK_SET);
1083 return ret ? 0 : -1;
1086 /*********************************************************************
1087 * clearerr (MSVCRT.@)
1089 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1091 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1092 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1095 /*********************************************************************
1098 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1100 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1101 MSVCRT_fseek(file, 0L, SEEK_SET);
1102 MSVCRT_clearerr(file);
1105 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1107 int plus = strchrW(mode, '+') != NULL;
1112 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1113 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1116 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1117 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1120 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1121 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1124 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1125 *MSVCRT__errno() = MSVCRT_EINVAL;
1133 *open_flags |= MSVCRT__O_BINARY;
1134 *open_flags &= ~MSVCRT__O_TEXT;
1137 *open_flags |= MSVCRT__O_TEXT;
1138 *open_flags &= ~MSVCRT__O_BINARY;
1144 FIXME(":unknown flag %c not supported\n",mode[-1]);
1149 /*********************************************************************
1150 * _fdopen (MSVCRT.@)
1152 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1155 MSVCRT_wchar_t *modeW = NULL;
1157 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1159 ret = MSVCRT__wfdopen(fd, modeW);
1165 /*********************************************************************
1166 * _wfdopen (MSVCRT.@)
1168 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1170 int open_flags, stream_flags;
1173 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1176 if (!(file = msvcrt_alloc_fp()))
1178 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1183 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1189 /*********************************************************************
1190 * _filelength (MSVCRT.@)
1192 LONG CDECL MSVCRT__filelength(int fd)
1194 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1197 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1200 if (endPos != curPos)
1201 MSVCRT__lseek(fd, curPos, SEEK_SET);
1208 /*********************************************************************
1209 * _filelengthi64 (MSVCRT.@)
1211 __int64 CDECL MSVCRT__filelengthi64(int fd)
1213 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1216 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1219 if (endPos != curPos)
1220 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1227 /*********************************************************************
1228 * _fileno (MSVCRT.@)
1230 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1232 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1236 /*********************************************************************
1237 * _fstat64 (MSVCRT.@)
1239 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1243 BY_HANDLE_FILE_INFORMATION hfi;
1244 HANDLE hand = msvcrt_fdtoh(fd);
1246 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1247 if (hand == INVALID_HANDLE_VALUE)
1252 WARN(":failed-NULL buf\n");
1253 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1257 memset(&hfi, 0, sizeof(hfi));
1258 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1259 type = GetFileType(hand);
1260 if (type == FILE_TYPE_PIPE)
1262 buf->st_dev = buf->st_rdev = fd;
1263 buf->st_mode = S_IFIFO;
1266 else if (type == FILE_TYPE_CHAR)
1268 buf->st_dev = buf->st_rdev = fd;
1269 buf->st_mode = S_IFCHR;
1272 else /* FILE_TYPE_DISK etc. */
1274 if (!GetFileInformationByHandle(hand, &hfi))
1276 WARN(":failed-last error (%d)\n",GetLastError());
1277 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1280 buf->st_mode = S_IFREG | 0444;
1281 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1282 buf->st_mode |= 0222;
1283 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1284 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1286 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1287 buf->st_mtime = buf->st_ctime = dw;
1288 buf->st_nlink = hfi.nNumberOfLinks;
1290 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1295 /*********************************************************************
1296 * _fstati64 (MSVCRT.@)
1298 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1301 struct MSVCRT__stat64 buf64;
1303 ret = MSVCRT__fstat64(fd, &buf64);
1305 msvcrt_stat64_to_stati64(&buf64, buf);
1309 /*********************************************************************
1312 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1314 struct MSVCRT__stat64 buf64;
1316 ret = MSVCRT__fstat64(fd, &buf64);
1318 msvcrt_stat64_to_stat(&buf64, buf);
1322 /*********************************************************************
1323 * _futime64 (MSVCRT.@)
1325 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1327 HANDLE hand = msvcrt_fdtoh(fd);
1332 time_to_filetime( MSVCRT__time64(NULL), &at );
1337 time_to_filetime( t->actime, &at );
1338 time_to_filetime( t->modtime, &wt );
1341 if (!SetFileTime(hand, NULL, &at, &wt))
1343 msvcrt_set_errno(GetLastError());
1349 /*********************************************************************
1350 * _futime32 (MSVCRT.@)
1352 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1354 struct MSVCRT___utimbuf64 t64;
1355 t64.actime = t->actime;
1356 t64.modtime = t->modtime;
1357 return _futime64( fd, &t64 );
1360 /*********************************************************************
1361 * _futime (MSVCRT.@)
1364 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1366 return _futime64( fd, t );
1369 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1371 return _futime32( fd, t );
1375 /*********************************************************************
1376 * _get_osfhandle (MSVCRT.@)
1378 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1380 HANDLE hand = msvcrt_fdtoh(fd);
1381 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1383 return (MSVCRT_intptr_t)hand;
1386 /*********************************************************************
1387 * _isatty (MSVCRT.@)
1389 int CDECL _isatty(int fd)
1391 HANDLE hand = msvcrt_fdtoh(fd);
1393 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1394 if (hand == INVALID_HANDLE_VALUE)
1397 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1400 /*********************************************************************
1401 * _mktemp (MSVCRT.@)
1403 char * CDECL _mktemp(char *pattern)
1406 char *retVal = pattern;
1411 numX = (*pattern++ == 'X')? numX + 1 : 0;
1415 id = GetCurrentProcessId();
1419 int tempNum = id / 10;
1420 *pattern-- = id - (tempNum * 10) + '0';
1426 *pattern = letter++;
1427 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1428 GetLastError() == ERROR_FILE_NOT_FOUND)
1430 } while(letter <= 'z');
1434 /*********************************************************************
1435 * _wmktemp (MSVCRT.@)
1437 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1440 MSVCRT_wchar_t *retVal = pattern;
1442 MSVCRT_wchar_t letter = 'a';
1445 numX = (*pattern++ == 'X')? numX + 1 : 0;
1449 id = GetCurrentProcessId();
1453 int tempNum = id / 10;
1454 *pattern-- = id - (tempNum * 10) + '0';
1460 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1461 GetLastError() == ERROR_FILE_NOT_FOUND)
1463 *pattern = letter++;
1464 } while(letter != '|');
1468 static unsigned split_oflags(unsigned oflags)
1471 unsigned unsupp; /* until we support everything */
1473 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1474 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1475 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1476 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1477 else wxflags |= WX_TEXT; /* default to TEXT*/
1478 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1480 if ((unsupp = oflags & ~(
1481 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1482 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1483 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1484 MSVCRT__O_NOINHERIT|
1485 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1487 ERR(":unsupported oflags 0x%04x\n",unsupp);
1492 /*********************************************************************
1495 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1498 SECURITY_ATTRIBUTES sa;
1499 HANDLE readHandle, writeHandle;
1503 *MSVCRT__errno() = MSVCRT_EINVAL;
1507 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1508 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1509 sa.lpSecurityDescriptor = NULL;
1510 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1512 unsigned int wxflags = split_oflags(textmode);
1516 fd = msvcrt_alloc_fd(readHandle, wxflags);
1520 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1528 MSVCRT__close(pfds[0]);
1529 CloseHandle(writeHandle);
1530 *MSVCRT__errno() = MSVCRT_EMFILE;
1535 CloseHandle(readHandle);
1536 CloseHandle(writeHandle);
1537 *MSVCRT__errno() = MSVCRT_EMFILE;
1542 msvcrt_set_errno(GetLastError());
1547 /*********************************************************************
1548 * _sopen_s (MSVCRT.@)
1550 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1552 DWORD access = 0, creation = 0, attrib;
1556 SECURITY_ATTRIBUTES sa;
1558 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1559 fd, path, oflags, shflags, pmode);
1563 MSVCRT_INVALID_PMT("null out fd pointer");
1564 *MSVCRT__errno() = MSVCRT_EINVAL;
1565 return MSVCRT_EINVAL;
1569 wxflag = split_oflags(oflags);
1570 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1572 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1573 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1574 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1577 if (oflags & MSVCRT__O_CREAT)
1579 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1580 FIXME(": pmode 0x%04x ignored\n", pmode);
1582 WARN(": pmode 0x%04x ignored\n", pmode);
1584 if (oflags & MSVCRT__O_EXCL)
1585 creation = CREATE_NEW;
1586 else if (oflags & MSVCRT__O_TRUNC)
1587 creation = CREATE_ALWAYS;
1589 creation = OPEN_ALWAYS;
1591 else /* no MSVCRT__O_CREAT */
1593 if (oflags & MSVCRT__O_TRUNC)
1594 creation = TRUNCATE_EXISTING;
1596 creation = OPEN_EXISTING;
1601 case MSVCRT__SH_DENYRW:
1604 case MSVCRT__SH_DENYWR:
1605 sharing = FILE_SHARE_READ;
1607 case MSVCRT__SH_DENYRD:
1608 sharing = FILE_SHARE_WRITE;
1610 case MSVCRT__SH_DENYNO:
1611 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1614 ERR( "Unhandled shflags 0x%x\n", shflags );
1615 return MSVCRT_EINVAL;
1617 attrib = FILE_ATTRIBUTE_NORMAL;
1619 if (oflags & MSVCRT__O_TEMPORARY)
1621 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1623 sharing |= FILE_SHARE_DELETE;
1626 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1627 sa.lpSecurityDescriptor = NULL;
1628 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1630 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1631 if (hand == INVALID_HANDLE_VALUE) {
1632 WARN(":failed-last error (%d)\n", GetLastError());
1633 msvcrt_set_errno(GetLastError());
1634 return *MSVCRT__errno();
1637 *fd = msvcrt_alloc_fd(hand, wxflag);
1639 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1643 /*********************************************************************
1646 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1651 if (oflags & MSVCRT__O_CREAT)
1655 __ms_va_start(ap, shflags);
1656 pmode = va_arg(ap, int);
1662 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
1666 /*********************************************************************
1667 * _wsopen_s (MSVCRT.@)
1669 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1671 DWORD access = 0, creation = 0, attrib;
1672 SECURITY_ATTRIBUTES sa;
1677 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1678 fd, debugstr_w(path), oflags, shflags, pmode);
1682 MSVCRT_INVALID_PMT("null out fd pointer");
1683 *MSVCRT__errno() = MSVCRT_EINVAL;
1684 return MSVCRT_EINVAL;
1688 wxflag = split_oflags(oflags);
1689 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1691 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1692 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1693 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1696 if (oflags & MSVCRT__O_CREAT)
1698 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1699 FIXME(": pmode 0x%04x ignored\n", pmode);
1701 WARN(": pmode 0x%04x ignored\n", pmode);
1703 if (oflags & MSVCRT__O_EXCL)
1704 creation = CREATE_NEW;
1705 else if (oflags & MSVCRT__O_TRUNC)
1706 creation = CREATE_ALWAYS;
1708 creation = OPEN_ALWAYS;
1710 else /* no MSVCRT__O_CREAT */
1712 if (oflags & MSVCRT__O_TRUNC)
1713 creation = TRUNCATE_EXISTING;
1715 creation = OPEN_EXISTING;
1720 case MSVCRT__SH_DENYRW:
1723 case MSVCRT__SH_DENYWR:
1724 sharing = FILE_SHARE_READ;
1726 case MSVCRT__SH_DENYRD:
1727 sharing = FILE_SHARE_WRITE;
1729 case MSVCRT__SH_DENYNO:
1730 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1733 ERR( "Unhandled shflags 0x%x\n", shflags );
1734 return MSVCRT_EINVAL;
1736 attrib = FILE_ATTRIBUTE_NORMAL;
1738 if (oflags & MSVCRT__O_TEMPORARY)
1740 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1742 sharing |= FILE_SHARE_DELETE;
1745 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1746 sa.lpSecurityDescriptor = NULL;
1747 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1749 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1751 if (hand == INVALID_HANDLE_VALUE) {
1752 WARN(":failed-last error (%d)\n",GetLastError());
1753 msvcrt_set_errno(GetLastError());
1754 msvcrt_set_errno(GetLastError());
1755 return *MSVCRT__errno();
1758 *fd = msvcrt_alloc_fd(hand, wxflag);
1760 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1764 /*********************************************************************
1765 * _wsopen (MSVCRT.@)
1767 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1772 if (oflags & MSVCRT__O_CREAT)
1776 __ms_va_start(ap, shflags);
1777 pmode = va_arg(ap, int);
1783 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1787 /*********************************************************************
1790 int CDECL MSVCRT__open( const char *path, int flags, ... )
1794 if (flags & MSVCRT__O_CREAT)
1797 __ms_va_start(ap, flags);
1798 pmode = va_arg(ap, int);
1800 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1803 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1806 /*********************************************************************
1809 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1813 if (flags & MSVCRT__O_CREAT)
1816 __ms_va_start(ap, flags);
1817 pmode = va_arg(ap, int);
1819 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1822 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1825 /*********************************************************************
1828 int CDECL MSVCRT__creat(const char *path, int flags)
1830 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1831 return MSVCRT__open(path, usedFlags);
1834 /*********************************************************************
1835 * _wcreat (MSVCRT.@)
1837 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1839 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1840 return _wopen(path, usedFlags);
1843 /*********************************************************************
1844 * _open_osfhandle (MSVCRT.@)
1846 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1850 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1851 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1852 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1853 * text - it never sets MSVCRT__O_BINARY.
1855 /* don't let split_oflags() decide the mode if no mode is passed */
1856 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1857 oflags |= MSVCRT__O_BINARY;
1859 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1860 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1864 /*********************************************************************
1867 int CDECL _rmtmp(void)
1869 int num_removed = 0, i;
1872 for (i = 3; i < MSVCRT_stream_idx; i++)
1873 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1875 MSVCRT_fclose(MSVCRT_fstreams[i]);
1881 TRACE(":removed (%d) temp files\n",num_removed);
1885 /*********************************************************************
1888 * When reading \r as last character in text mode, read() positions
1889 * the file pointer on the \r character while getc() goes on to
1892 static int read_i(int fd, void *buf, unsigned int count)
1895 char *bufstart = buf;
1896 HANDLE hand = msvcrt_fdtoh(fd);
1901 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1902 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1903 TRACE("already at EOF, returning 0\n");
1906 /* Don't trace small reads, it gets *very* annoying */
1908 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1909 if (hand == INVALID_HANDLE_VALUE)
1912 /* Reading single bytes in O_TEXT mode makes things slow
1913 * So read big chunks
1915 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1917 if (count != 0 && num_read == 0)
1919 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1920 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1922 else if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1925 if (bufstart[num_read-1] == '\r')
1929 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1930 ReadFile(hand, bufstart, 1, &num_read, NULL);
1934 MSVCRT_fdesc[fd].wxflag |= WX_READCR;
1939 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1940 for (i=0, j=0; i<num_read; i++)
1942 /* in text mode, a ctrl-z signals EOF */
1943 if (bufstart[i] == 0x1a)
1945 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1946 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1949 /* in text mode, strip \r if followed by \n.
1950 * BUG: should save state across calls somehow, so CR LF that
1951 * straddles buffer boundary gets recognized properly?
1953 if ((bufstart[i] != '\r')
1954 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1955 bufstart[j++] = bufstart[i];
1962 if (GetLastError() == ERROR_BROKEN_PIPE)
1964 TRACE(":end-of-pipe\n");
1965 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1970 TRACE(":failed-last error (%d)\n",GetLastError());
1976 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1980 /*********************************************************************
1983 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1986 num_read = read_i(fd, buf, count);
1990 /*********************************************************************
1991 * _setmode (MSVCRT.@)
1993 int CDECL _setmode(int fd,int mode)
1995 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1996 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1997 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1998 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1999 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
2001 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
2005 /*********************************************************************
2006 * _stat64 (MSVCRT.@)
2008 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2011 WIN32_FILE_ATTRIBUTE_DATA hfi;
2012 unsigned short mode = ALL_S_IREAD;
2015 TRACE(":file (%s) buf(%p)\n",path,buf);
2017 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2019 TRACE("failed (%d)\n",GetLastError());
2020 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2024 memset(buf,0,sizeof(struct MSVCRT__stat64));
2026 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2027 Bon 011120: This FIXME seems incorrect
2028 Also a letter as first char isn't enough to be classified
2031 if (isalpha(*path)&& (*(path+1)==':'))
2032 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2034 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2036 plen = strlen(path);
2038 /* Dir, or regular file? */
2039 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2040 (path[plen-1] == '\\'))
2041 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2044 mode |= MSVCRT__S_IFREG;
2046 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2048 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2049 (tolower(path[plen-3]) << 16);
2050 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2051 mode |= ALL_S_IEXEC;
2055 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2056 mode |= ALL_S_IWRITE;
2058 buf->st_mode = mode;
2060 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2061 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2063 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2064 buf->st_mtime = buf->st_ctime = dw;
2065 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2066 (int)(buf->st_size >> 32),(int)buf->st_size,
2067 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2071 /*********************************************************************
2072 * _stati64 (MSVCRT.@)
2074 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2077 struct MSVCRT__stat64 buf64;
2079 ret = MSVCRT_stat64(path, &buf64);
2081 msvcrt_stat64_to_stati64(&buf64, buf);
2085 /*********************************************************************
2088 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2090 struct MSVCRT__stat64 buf64;
2092 ret = MSVCRT_stat64( path, &buf64);
2094 msvcrt_stat64_to_stat(&buf64, buf);
2098 /*********************************************************************
2099 * _wstat64 (MSVCRT.@)
2101 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2104 WIN32_FILE_ATTRIBUTE_DATA hfi;
2105 unsigned short mode = ALL_S_IREAD;
2108 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2110 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2112 TRACE("failed (%d)\n",GetLastError());
2113 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2117 memset(buf,0,sizeof(struct MSVCRT__stat64));
2119 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2120 if (MSVCRT_iswalpha(*path))
2121 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2123 buf->st_dev = buf->st_rdev = _getdrive() - 1;
2125 plen = strlenW(path);
2127 /* Dir, or regular file? */
2128 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2129 (path[plen-1] == '\\'))
2130 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2133 mode |= MSVCRT__S_IFREG;
2135 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2137 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2138 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2139 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2140 mode |= ALL_S_IEXEC;
2144 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2145 mode |= ALL_S_IWRITE;
2147 buf->st_mode = mode;
2149 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2150 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2152 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2153 buf->st_mtime = buf->st_ctime = dw;
2154 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2155 (int)(buf->st_size >> 32),(int)buf->st_size,
2156 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2160 /*********************************************************************
2161 * _wstati64 (MSVCRT.@)
2163 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2166 struct MSVCRT__stat64 buf64;
2168 ret = MSVCRT__wstat64(path, &buf64);
2170 msvcrt_stat64_to_stati64(&buf64, buf);
2174 /*********************************************************************
2177 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2180 struct MSVCRT__stat64 buf64;
2182 ret = MSVCRT__wstat64( path, &buf64 );
2183 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2187 /*********************************************************************
2190 MSVCRT_long CDECL MSVCRT__tell(int fd)
2192 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2195 /*********************************************************************
2196 * _telli64 (MSVCRT.@)
2198 __int64 CDECL _telli64(int fd)
2200 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2203 /*********************************************************************
2204 * _tempnam (MSVCRT.@)
2206 char * CDECL _tempnam(const char *dir, const char *prefix)
2208 char tmpbuf[MAX_PATH];
2209 const char *tmp_dir = MSVCRT_getenv("TMP");
2211 if (tmp_dir) dir = tmp_dir;
2213 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2214 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2216 TRACE("got name (%s)\n",tmpbuf);
2217 DeleteFileA(tmpbuf);
2218 return _strdup(tmpbuf);
2220 TRACE("failed (%d)\n",GetLastError());
2224 /*********************************************************************
2225 * _wtempnam (MSVCRT.@)
2227 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2229 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2231 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2232 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2234 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2235 DeleteFileW(tmpbuf);
2236 return _wcsdup(tmpbuf);
2238 TRACE("failed (%d)\n",GetLastError());
2242 /*********************************************************************
2245 int CDECL MSVCRT__umask(int umask)
2247 int old_umask = MSVCRT_umask;
2248 TRACE("(%d)\n",umask);
2249 MSVCRT_umask = umask;
2253 /*********************************************************************
2254 * _utime64 (MSVCRT.@)
2256 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2258 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2262 int retVal = _futime64(fd, t);
2269 /*********************************************************************
2270 * _utime32 (MSVCRT.@)
2272 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2274 struct MSVCRT___utimbuf64 t64;
2275 t64.actime = t->actime;
2276 t64.modtime = t->modtime;
2277 return _utime64( path, &t64 );
2280 /*********************************************************************
2284 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2286 return _utime64( path, t );
2289 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2291 return _utime32( path, t );
2295 /*********************************************************************
2296 * _wutime64 (MSVCRT.@)
2298 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2300 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2304 int retVal = _futime64(fd, t);
2311 /*********************************************************************
2312 * _wutime32 (MSVCRT.@)
2314 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2316 struct MSVCRT___utimbuf64 t64;
2317 t64.actime = t->actime;
2318 t64.modtime = t->modtime;
2319 return _wutime64( path, &t64 );
2322 /*********************************************************************
2323 * _wutime (MSVCRT.@)
2326 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2328 return _wutime64( path, t );
2331 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2333 return _wutime32( path, t );
2337 /*********************************************************************
2340 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2343 HANDLE hand = msvcrt_fdtoh(fd);
2345 /* Don't trace small writes, it gets *very* annoying */
2348 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2350 if (hand == INVALID_HANDLE_VALUE)
2352 *MSVCRT__errno() = MSVCRT_EBADF;
2356 /* If appending, go to EOF */
2357 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2358 MSVCRT__lseek(fd, 0, FILE_END);
2360 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2362 if (WriteFile(hand, buf, count, &num_written, NULL)
2363 && (num_written == count))
2365 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2366 hand, GetLastError());
2367 *MSVCRT__errno() = MSVCRT_ENOSPC;
2371 unsigned int i, j, nr_lf;
2374 const char *s = buf, *buf_start = buf;
2375 /* find number of \n ( without preceding \r ) */
2376 for ( nr_lf=0,i = 0; i <count; i++)
2381 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2386 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2388 for (s = buf, i = 0, j = 0; i < count; i++)
2393 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2400 FIXME("Malloc failed\n");
2408 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2410 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2411 fd, hand, GetLastError(), num_written);
2412 *MSVCRT__errno() = MSVCRT_ENOSPC;
2415 return s - buf_start;
2427 /*********************************************************************
2430 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2433 len = MSVCRT__write(file->_file, &val, sizeof(val));
2434 if (len == sizeof(val)) return val;
2435 file->_flag |= MSVCRT__IOERR;
2439 /*********************************************************************
2442 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2447 MSVCRT_free(file->_tmpfname);
2448 file->_tmpfname = NULL;
2449 /* flush stdio buffers */
2450 if(file->_flag & MSVCRT__IOWRT)
2451 MSVCRT_fflush(file);
2452 if(file->_flag & MSVCRT__IOMYBUF)
2453 MSVCRT_free(file->_base);
2455 r=MSVCRT__close(file->_file);
2459 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2462 /*********************************************************************
2465 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2467 return file->_flag & MSVCRT__IOEOF;
2470 /*********************************************************************
2473 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2475 return file->_flag & MSVCRT__IOERR;
2478 /*********************************************************************
2479 * _filbuf (MSVCRT.@)
2481 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2483 /* Allocate buffer if needed */
2484 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2485 msvcrt_alloc_buffer(file);
2487 if(!(file->_flag & MSVCRT__IOREAD)) {
2488 if(file->_flag & MSVCRT__IORW) {
2489 file->_flag |= MSVCRT__IOREAD;
2494 if(file->_flag & MSVCRT__IONBF) {
2497 if ((r = read_i(file->_file,&c,1)) != 1) {
2498 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2503 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2505 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2510 file->_ptr = file->_base+1;
2511 return *(unsigned char *)file->_base;
2515 /*********************************************************************
2518 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2524 i = (unsigned char *)file->_ptr++;
2527 j = MSVCRT__filbuf(file);
2531 /*********************************************************************
2532 * _fgetchar (MSVCRT.@)
2534 int CDECL _fgetchar(void)
2536 return MSVCRT_fgetc(MSVCRT_stdin);
2539 /*********************************************************************
2542 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2544 int cc = MSVCRT_EOF;
2545 char * buf_start = s;
2547 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2548 file,file->_file,s,size);
2550 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2555 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2557 TRACE(":nothing read\n");
2560 if ((cc != MSVCRT_EOF) && (size > 1))
2563 TRACE(":got %s\n", debugstr_a(buf_start));
2567 /*********************************************************************
2570 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2571 * the CR from CR/LF combinations
2573 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2577 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2584 for(i=0; i<sizeof(wc); i++)
2594 j = MSVCRT__filbuf(file);
2597 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2607 c = MSVCRT_fgetc(file);
2608 if ((get_locale()->locinfo->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2610 FIXME("Treat Multibyte characters\n");
2612 if (c == MSVCRT_EOF)
2615 return (MSVCRT_wint_t)c;
2618 /*********************************************************************
2621 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2627 for (j=0; j<sizeof(int); j++) {
2628 k = MSVCRT_fgetc(file);
2629 if (k == MSVCRT_EOF) {
2630 file->_flag |= MSVCRT__IOEOF;
2638 /*********************************************************************
2641 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2643 return MSVCRT_fgetwc(file);
2646 /*********************************************************************
2647 * _fgetwchar (MSVCRT.@)
2649 MSVCRT_wint_t CDECL _fgetwchar(void)
2651 return MSVCRT_fgetwc(MSVCRT_stdin);
2654 /*********************************************************************
2655 * getwchar (MSVCRT.@)
2657 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2659 return _fgetwchar();
2662 /*********************************************************************
2665 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2667 int cc = MSVCRT_WEOF;
2668 MSVCRT_wchar_t * buf_start = s;
2670 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2671 file,file->_file,s,size);
2673 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2678 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2680 TRACE(":nothing read\n");
2683 if ((cc != MSVCRT_WEOF) && (size > 1))
2686 TRACE(":got %s\n", debugstr_w(buf_start));
2690 /*********************************************************************
2693 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2695 MSVCRT_size_t wrcnt=size * nmemb;
2700 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2701 memcpy(file->_ptr, ptr, pcnt);
2706 ptr = (const char*)ptr + pcnt;
2707 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2708 if(file->_flag & MSVCRT__IORW) {
2709 file->_flag |= MSVCRT__IOWRT;
2715 int res=msvcrt_flush_buffer(file);
2717 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2720 file->_flag |= MSVCRT__IOERR;
2723 written += pwritten;
2726 return written / size;
2729 /*********************************************************************
2732 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2734 MSVCRT_wchar_t mwc=wc;
2735 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2740 /*********************************************************************
2741 * _fputwchar (MSVCRT.@)
2743 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2745 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2748 /*********************************************************************
2749 * _wfsopen (MSVCRT.@)
2751 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2754 int open_flags, stream_flags, fd;
2756 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2758 /* map mode string to open() flags. "man fopen" for possibilities. */
2759 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2763 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2766 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2768 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2775 TRACE(":got (%p)\n",file);
2776 if (fd >= 0 && !file)
2782 /*********************************************************************
2783 * _fsopen (MSVCRT.@)
2785 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2788 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2790 if (path && !(pathW = msvcrt_wstrdupa(path))) {
2791 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2792 *MSVCRT__errno() = MSVCRT_EINVAL;
2795 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2798 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2799 *MSVCRT__errno() = MSVCRT_EINVAL;
2803 ret = MSVCRT__wfsopen(pathW, modeW, share);
2810 /*********************************************************************
2813 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2815 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2818 /*********************************************************************
2819 * fopen_s (MSVCRT.@)
2821 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
2822 const char *filename, const char *mode)
2824 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2825 !MSVCRT_CHECK_PMT(mode != NULL)) {
2826 *MSVCRT__errno() = MSVCRT_EINVAL;
2827 return MSVCRT_EINVAL;
2830 *pFile = MSVCRT_fopen(filename, mode);
2833 return *MSVCRT__errno();
2837 /*********************************************************************
2838 * _wfopen (MSVCRT.@)
2840 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2842 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2845 /*********************************************************************
2846 * _wfopen_s (MSVCRT.@)
2848 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
2849 const MSVCRT_wchar_t *mode)
2851 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
2852 !MSVCRT_CHECK_PMT(mode != NULL)) {
2853 *MSVCRT__errno() = MSVCRT_EINVAL;
2854 return MSVCRT_EINVAL;
2857 *pFile = MSVCRT__wfopen(filename, mode);
2860 return *MSVCRT__errno();
2864 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2865 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2867 /*********************************************************************
2870 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2877 int res = msvcrt_flush_buffer(file);
2878 return res ? res : c;
2883 return MSVCRT__flsbuf(c, file);
2887 /*********************************************************************
2888 * _flsbuf (MSVCRT.@)
2890 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2892 /* Flush output buffer */
2893 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2894 msvcrt_alloc_buffer(file);
2896 if(!(file->_flag & MSVCRT__IOWRT)) {
2897 if(file->_flag & MSVCRT__IORW) {
2898 file->_flag |= MSVCRT__IOWRT;
2904 int res=msvcrt_flush_buffer(file);
2905 return res?res : MSVCRT_fputc(c, file);
2909 /* set _cnt to 0 for unbuffered FILEs */
2911 len = MSVCRT__write(file->_file, &cc, 1);
2912 if (len == 1) return c & 0xff;
2913 file->_flag |= MSVCRT__IOERR;
2918 /*********************************************************************
2919 * _fputchar (MSVCRT.@)
2921 int CDECL _fputchar(int c)
2923 return MSVCRT_fputc(c, MSVCRT_stdout);
2926 /*********************************************************************
2929 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2930 { MSVCRT_size_t rcnt=size * nmemb;
2931 MSVCRT_size_t read=0;
2937 /* first buffered data */
2939 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2940 memcpy(ptr, file->_ptr, pcnt);
2945 ptr = (char*)ptr + pcnt;
2946 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2947 if(file->_flag & MSVCRT__IORW) {
2948 file->_flag |= MSVCRT__IOREAD;
2955 /* Fill the buffer on small reads.
2956 * TODO: Use a better buffering strategy.
2958 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2959 if (file->_bufsiz == 0) {
2960 msvcrt_alloc_buffer(file);
2962 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2963 file->_ptr = file->_base;
2964 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2965 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2966 if (i > 0 && i < file->_cnt) {
2967 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2968 file->_flag &= ~MSVCRT__IOEOF;
2971 memcpy(ptr, file->_ptr, i);
2976 i = MSVCRT__read(file->_file,ptr, rcnt);
2980 ptr = (char *)ptr+i;
2981 /* expose feof condition in the flags
2982 * MFC tests file->_flag for feof, and doesn't call feof())
2984 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2985 file->_flag |= MSVCRT__IOEOF;
2988 file->_flag |= MSVCRT__IOERR;
2998 /*********************************************************************
2999 * _wfreopen (MSVCRT.@)
3002 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3004 int open_flags, stream_flags, fd;
3006 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3009 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3013 MSVCRT_fclose(file);
3014 /* map mode string to open() flags. "man fopen" for possibilities. */
3015 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3019 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3022 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
3025 WARN(":failed-last error (%d)\n",GetLastError());
3026 msvcrt_set_errno(GetLastError());
3035 /*********************************************************************
3036 * freopen (MSVCRT.@)
3039 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3042 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3044 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3045 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3051 ret = MSVCRT__wfreopen(pathW, modeW, file);
3058 /*********************************************************************
3059 * fsetpos (MSVCRT.@)
3061 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3063 /* Note that all this has been lifted 'as is' from fseek */
3064 if(file->_flag & MSVCRT__IOWRT)
3065 msvcrt_flush_buffer(file);
3067 /* Discard buffered input */
3069 file->_ptr = file->_base;
3071 /* Reset direction of i/o */
3072 if(file->_flag & MSVCRT__IORW) {
3073 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3076 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3079 /*********************************************************************
3080 * _ftelli64 (MSVCRT.@)
3082 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3084 /* TODO: just call fgetpos and return lower half of result */
3087 pos = _telli64(file->_file);
3088 if(pos == -1) return -1;
3090 if( file->_flag & MSVCRT__IOWRT ) {
3091 off = file->_ptr - file->_base;
3094 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3095 /* Black magic correction for CR removal */
3097 for (i=0; i<file->_cnt; i++) {
3098 if (file->_ptr[i] == '\n')
3101 /* Black magic when reading CR at buffer boundary*/
3102 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3111 /*********************************************************************
3114 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3116 return MSVCRT__ftelli64(file);
3119 /*********************************************************************
3120 * fgetpos (MSVCRT.@)
3122 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3125 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3126 if(*pos == -1) return -1;
3128 if( file->_flag & MSVCRT__IOWRT ) {
3129 off = file->_ptr - file->_base;
3132 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
3133 /* Black magic correction for CR removal */
3135 for (i=0; i<file->_cnt; i++) {
3136 if (file->_ptr[i] == '\n')
3139 /* Black magic when reading CR at buffer boundary*/
3140 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
3149 /*********************************************************************
3152 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3154 MSVCRT_size_t i, len = strlen(s);
3155 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3156 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3157 for (i=0; i<len; i++)
3158 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
3163 /*********************************************************************
3166 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3168 MSVCRT_size_t i, len = strlenW(s);
3169 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3170 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3171 for (i=0; i<len; i++)
3173 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3175 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
3181 /*********************************************************************
3182 * getchar (MSVCRT.@)
3184 int CDECL MSVCRT_getchar(void)
3186 return MSVCRT_fgetc(MSVCRT_stdin);
3189 /*********************************************************************
3192 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3194 return MSVCRT_fgetc(file);
3197 /*********************************************************************
3200 char * CDECL MSVCRT_gets(char *buf)
3203 char * buf_start = buf;
3205 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3206 cc = MSVCRT_fgetc(MSVCRT_stdin))
3207 if(cc != '\r') *buf++ = (char)cc;
3211 TRACE("got '%s'\n", buf_start);
3215 /*********************************************************************
3218 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3221 MSVCRT_wchar_t* ws = buf;
3223 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3224 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3227 *buf++ = (MSVCRT_wchar_t)cc;
3231 TRACE("got %s\n", debugstr_w(ws));
3235 /*********************************************************************
3238 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3240 return MSVCRT_fputc(c, file);
3243 /*********************************************************************
3244 * putchar (MSVCRT.@)
3246 int CDECL MSVCRT_putchar(int c)
3248 return MSVCRT_fputc(c, MSVCRT_stdout);
3251 /*********************************************************************
3252 * _putwch (MSVCRT.@)
3254 int CDECL MSVCRT__putwch(int c)
3256 return MSVCRT_fputwc(c, MSVCRT_stdout);
3259 /*********************************************************************
3262 int CDECL MSVCRT_puts(const char *s)
3264 MSVCRT_size_t len = strlen(s);
3265 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3266 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3269 /*********************************************************************
3272 int CDECL _putws(const MSVCRT_wchar_t *s)
3274 static const MSVCRT_wchar_t nl = '\n';
3275 MSVCRT_size_t len = strlenW(s);
3276 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3277 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3280 /*********************************************************************
3283 int CDECL MSVCRT_remove(const char *path)
3285 TRACE("(%s)\n",path);
3286 if (DeleteFileA(path))
3288 TRACE(":failed (%d)\n",GetLastError());
3289 msvcrt_set_errno(GetLastError());
3293 /*********************************************************************
3294 * _wremove (MSVCRT.@)
3296 int CDECL _wremove(const MSVCRT_wchar_t *path)
3298 TRACE("(%s)\n",debugstr_w(path));
3299 if (DeleteFileW(path))
3301 TRACE(":failed (%d)\n",GetLastError());
3302 msvcrt_set_errno(GetLastError());
3306 /*********************************************************************
3309 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3311 TRACE(":from %s to %s\n",oldpath,newpath);
3312 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3314 TRACE(":failed (%d)\n",GetLastError());
3315 msvcrt_set_errno(GetLastError());
3319 /*********************************************************************
3320 * _wrename (MSVCRT.@)
3322 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3324 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3325 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3327 TRACE(":failed (%d)\n",GetLastError());
3328 msvcrt_set_errno(GetLastError());
3332 /*********************************************************************
3333 * setvbuf (MSVCRT.@)
3335 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3337 /* TODO: Check if file busy */
3339 MSVCRT_free(file->_base);
3343 if(mode == MSVCRT__IOFBF) {
3344 file->_flag &= ~MSVCRT__IONBF;
3345 file->_base = file->_ptr = buf;
3347 file->_bufsiz = size;
3350 file->_flag |= MSVCRT__IONBF;
3355 /*********************************************************************
3358 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3360 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3363 /*********************************************************************
3366 char * CDECL MSVCRT_tmpnam(char *s)
3373 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3374 p = s + sprintf(s, "\\s%s.", tmpstr);
3375 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3377 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
3378 memcpy(p, tmpstr, size);
3379 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3380 GetLastError() == ERROR_FILE_NOT_FOUND)
3386 /*********************************************************************
3387 * _wtmpnam (MSVCRT.@)
3389 MSVCRT_wchar_t * MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
3391 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
3392 MSVCRT_wchar_t tmpstr[16];
3396 s = MSVCRT_wtmpname;
3397 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
3398 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
3399 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3401 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
3402 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
3403 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
3404 GetLastError() == ERROR_FILE_NOT_FOUND)
3410 /*********************************************************************
3411 * tmpfile (MSVCRT.@)
3413 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3415 char *filename = MSVCRT_tmpnam(NULL);
3417 MSVCRT_FILE* file = NULL;
3420 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3421 if (fd != -1 && (file = msvcrt_alloc_fp()))
3423 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3428 else file->_tmpfname = _strdup(filename);
3434 static int puts_clbk_file_a(void *file, int len, const char *str)
3436 return MSVCRT_fwrite(str, sizeof(char), len, file);
3439 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
3441 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
3444 /*********************************************************************
3445 * vfprintf (MSVCRT.@)
3447 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3449 return pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, valist);
3452 /*********************************************************************
3453 * vfprintf_s (MSVCRT.@)
3455 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3457 if(!MSVCRT_CHECK_PMT(file != NULL)) {
3458 *MSVCRT__errno() = MSVCRT_EINVAL;
3462 return pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, valist);
3465 /*********************************************************************
3466 * vfwprintf (MSVCRT.@)
3468 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3470 return pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, valist);
3473 /*********************************************************************
3474 * vfwprintf_s (MSVCRT.@)
3476 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3478 if(!MSVCRT_CHECK_PMT( file != NULL)) {
3479 *MSVCRT__errno() = MSVCRT_EINVAL;
3483 return pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, valist);
3486 /*********************************************************************
3487 * vprintf (MSVCRT.@)
3489 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3491 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3494 /*********************************************************************
3495 * vprintf_s (MSVCRT.@)
3497 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
3499 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
3502 /*********************************************************************
3503 * vwprintf (MSVCRT.@)
3505 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3507 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3510 /*********************************************************************
3511 * vwprintf_s (MSVCRT.@)
3513 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
3515 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
3518 /*********************************************************************
3519 * fprintf (MSVCRT.@)
3521 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3523 __ms_va_list valist;
3525 __ms_va_start(valist, format);
3526 res = MSVCRT_vfprintf(file, format, valist);
3527 __ms_va_end(valist);
3531 /*********************************************************************
3532 * fprintf_s (MSVCRT.@)
3534 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
3536 __ms_va_list valist;
3538 __ms_va_start(valist, format);
3539 res = MSVCRT_vfprintf_s(file, format, valist);
3540 __ms_va_end(valist);
3544 /*********************************************************************
3545 * fwprintf (MSVCRT.@)
3547 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3549 __ms_va_list valist;
3551 __ms_va_start(valist, format);
3552 res = MSVCRT_vfwprintf(file, format, valist);
3553 __ms_va_end(valist);
3557 /*********************************************************************
3558 * fwprintf_s (MSVCRT.@)
3560 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3562 __ms_va_list valist;
3564 __ms_va_start(valist, format);
3565 res = MSVCRT_vfwprintf_s(file, format, valist);
3566 __ms_va_end(valist);
3570 /*********************************************************************
3573 int CDECL MSVCRT_printf(const char *format, ...)
3575 __ms_va_list valist;
3577 __ms_va_start(valist, format);
3578 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3579 __ms_va_end(valist);
3583 /*********************************************************************
3584 * printf_s (MSVCRT.@)
3586 int CDECL MSVCRT_printf_s(const char *format, ...)
3588 __ms_va_list valist;
3590 __ms_va_start(valist, format);
3591 res = MSVCRT_vprintf_s(format, valist);
3592 __ms_va_end(valist);
3596 /*********************************************************************
3599 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3601 if (c == MSVCRT_EOF)
3603 if(file->_bufsiz == 0) {
3604 msvcrt_alloc_buffer(file);
3607 if(file->_ptr>file->_base) {
3611 MSVCRT_clearerr(file);
3617 /*********************************************************************
3618 * ungetwc (MSVCRT.@)
3620 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3622 MSVCRT_wchar_t mwc = wc;
3623 char * pp = (char *)&mwc;
3625 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3626 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3632 /*********************************************************************
3633 * wprintf (MSVCRT.@)
3635 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3637 __ms_va_list valist;
3639 __ms_va_start(valist, format);
3640 res = MSVCRT_vwprintf(format, valist);
3641 __ms_va_end(valist);
3645 /*********************************************************************
3646 * wprintf_s (MSVCRT.@)
3648 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
3650 __ms_va_list valist;
3652 __ms_va_start(valist, format);
3653 res = MSVCRT_vwprintf_s(format, valist);
3654 __ms_va_end(valist);
3658 /*********************************************************************
3659 * _getmaxstdio (MSVCRT.@)
3661 int CDECL _getmaxstdio(void)
3663 FIXME("stub, always returns 512\n");
3667 /*********************************************************************
3668 * _setmaxstdio_ (MSVCRT.@)
3670 int CDECL _setmaxstdio(int newmax)
3677 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3681 /*********************************************************************
3682 * __pioinfo (MSVCRT.@)
3683 * FIXME: see MSVCRT_MAX_FILES define.
3685 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3686 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3687 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3688 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3689 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3690 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3691 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3692 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3693 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3694 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3695 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3696 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3699 /*********************************************************************
3700 * __badioinfo (MSVCRT.@)
3702 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };