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>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
60 /* values for wxflag in file descriptor */
63 #define WX_DONTINHERIT 0x10
64 #define WX_APPEND 0x20
67 /* FIXME: this should be allocated dynamically */
68 #define MSVCRT_MAX_FILES 2048
73 DWORD unkn[7]; /* critical section and init flag */
76 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
78 MSVCRT_FILE MSVCRT__iob[3];
80 static int MSVCRT_fdstart = 3; /* first unallocated fd */
81 static int MSVCRT_fdend = 3; /* highest allocated fd */
83 static MSVCRT_FILE* MSVCRT_fstreams[2048];
84 static int MSVCRT_stream_idx;
86 /* INTERNAL: process umask */
87 static int MSVCRT_umask = 0;
89 /* INTERNAL: Static buffer for temp file name */
90 static char MSVCRT_tmpname[MAX_PATH];
92 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
93 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
94 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
95 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
97 #define TOUL(x) (ULONGLONG)(x)
98 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
99 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
100 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
101 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
103 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
104 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
105 * and MSVCRT_stream_idx, from race conditions.
106 * It doesn't protect against race conditions manipulating the underlying files
107 * or flags; doing so would probably be better accomplished with per-file
108 * protection, rather than locking the whole table for every change.
110 static CRITICAL_SECTION MSVCRT_file_cs;
111 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
112 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
114 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64 *bufi64, struct MSVCRT__stat *buf)
116 buf->st_dev = bufi64->st_dev;
117 buf->st_ino = bufi64->st_ino;
118 buf->st_mode = bufi64->st_mode;
119 buf->st_nlink = bufi64->st_nlink;
120 buf->st_uid = bufi64->st_uid;
121 buf->st_gid = bufi64->st_gid;
122 buf->st_rdev = bufi64->st_rdev;
123 buf->st_size = bufi64->st_size;
124 buf->st_atime = bufi64->st_atime;
125 buf->st_mtime = bufi64->st_mtime;
126 buf->st_ctime = bufi64->st_ctime;
129 static inline BOOL msvcrt_is_valid_fd(int fd)
131 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
134 /* INTERNAL: Get the HANDLE for a fd
135 * This doesn't lock the table, because a failure will result in
136 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
137 * it returns a valid handle which is about to be closed, a subsequent call
138 * will fail, most likely in a sane way.
140 static HANDLE msvcrt_fdtoh(int fd)
142 if (!msvcrt_is_valid_fd(fd))
144 WARN(":fd (%d) - no handle!\n",fd);
145 *MSVCRT___doserrno() = 0;
146 *MSVCRT__errno() = MSVCRT_EBADF;
147 return INVALID_HANDLE_VALUE;
149 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
150 return MSVCRT_fdesc[fd].handle;
153 /* INTERNAL: free a file entry fd */
154 static void msvcrt_free_fd(int fd)
157 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
158 MSVCRT_fdesc[fd].wxflag = 0;
159 TRACE(":fd (%d) freed\n",fd);
160 if (fd < 3) /* don't use 0,1,2 for user files */
164 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
165 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
166 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
171 if (fd == MSVCRT_fdend - 1)
173 if (fd < MSVCRT_fdstart)
179 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
180 /* caller must hold the files lock */
181 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
183 if (fd >= MSVCRT_MAX_FILES)
185 WARN(":files exhausted!\n");
188 MSVCRT_fdesc[fd].handle = hand;
189 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
191 /* locate next free slot */
192 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
193 MSVCRT_fdstart = MSVCRT_fdend + 1;
195 while (MSVCRT_fdstart < MSVCRT_fdend &&
196 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
198 /* update last fd in use */
199 if (fd >= MSVCRT_fdend)
200 MSVCRT_fdend = fd + 1;
201 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
205 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
206 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
207 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
213 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
214 static int msvcrt_alloc_fd(HANDLE hand, int flag)
219 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
220 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
225 /* INTERNAL: Allocate a FILE* for an fd slot */
226 /* caller must hold the files lock */
227 static MSVCRT_FILE* msvcrt_alloc_fp(void)
231 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
233 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
235 if (!MSVCRT_fstreams[i])
237 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
239 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
241 return MSVCRT_fstreams[i];
247 /* INTERNAL: initialize a FILE* from an open fd */
248 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
250 TRACE(":fd (%d) allocating FILE*\n",fd);
251 if (!msvcrt_is_valid_fd(fd))
253 WARN(":invalid fd %d\n",fd);
254 *MSVCRT___doserrno() = 0;
255 *MSVCRT__errno() = MSVCRT_EBADF;
258 memset(file, 0, sizeof(*file));
260 file->_flag = stream_flags;
262 TRACE(":got FILE* (%p)\n",file);
266 /* INTERNAL: Create an inheritance data block (for spawned process)
267 * The inheritance block is made of:
268 * 00 int nb of file descriptor (NBFD)
269 * 04 char file flags (wxflag): repeated for each fd
270 * 4+NBFD HANDLE file handle: repeated for each fd
272 unsigned msvcrt_create_io_inherit_block(STARTUPINFOA* si)
278 si->cbReserved2 = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
279 si->lpReserved2 = MSVCRT_calloc(si->cbReserved2, 1);
280 if (!si->lpReserved2)
285 wxflag_ptr = (char*)si->lpReserved2 + sizeof(unsigned);
286 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
288 *(unsigned*)si->lpReserved2 = MSVCRT_fdend;
289 for (fd = 0; fd < MSVCRT_fdend; fd++)
291 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
292 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
294 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
295 *handle_ptr = MSVCRT_fdesc[fd].handle;
300 *handle_ptr = INVALID_HANDLE_VALUE;
302 wxflag_ptr++; handle_ptr++;
307 /* INTERNAL: Set up all file descriptors,
308 * as well as default streams (stdin, stderr and stdout)
310 void msvcrt_init_io(void)
315 InitializeCriticalSection(&MSVCRT_file_cs);
316 GetStartupInfoA(&si);
317 if (si.cbReserved2 != 0 && si.lpReserved2 != NULL)
322 MSVCRT_fdend = *(unsigned*)si.lpReserved2;
324 wxflag_ptr = (char*)(si.lpReserved2 + sizeof(unsigned));
325 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
327 MSVCRT_fdend = min(MSVCRT_fdend, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
328 for (i = 0; i < MSVCRT_fdend; i++)
330 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
332 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
333 MSVCRT_fdesc[i].handle = *handle_ptr;
337 MSVCRT_fdesc[i].wxflag = 0;
338 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
340 wxflag_ptr++; handle_ptr++;
342 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
343 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
346 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
348 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
349 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, TRUE,
350 DUPLICATE_SAME_ACCESS);
351 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
353 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
355 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
356 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, TRUE,
357 DUPLICATE_SAME_ACCESS);
358 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
360 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
362 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
363 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, TRUE,
364 DUPLICATE_SAME_ACCESS);
365 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
368 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
369 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
371 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
372 for (i = 0; i < 3; i++)
374 /* FILE structs for stdin/out/err are static and never deleted */
375 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
376 MSVCRT__iob[i]._file = i;
377 MSVCRT__iob[i]._tmpfname = NULL;
378 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
380 MSVCRT_stream_idx = 3;
383 /* INTERNAL: Flush stdio file buffer */
384 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
387 int cnt=file->_ptr-file->_base;
388 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
389 file->_flag |= MSVCRT__IOERR;
392 file->_ptr=file->_base;
393 file->_cnt=file->_bufsiz;
398 /* INTERNAL: Allocate stdio file buffer */
399 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
401 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
403 file->_bufsiz = MSVCRT_BUFSIZ;
404 file->_flag |= MSVCRT__IOMYBUF;
406 file->_base = (char*)(&file->_charbuf);
408 file->_bufsiz = sizeof(file->_charbuf);
410 file->_ptr = file->_base;
414 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
415 static void msvcrt_int_to_base32(int num, char *str)
430 *p = (num & 31) + '0';
432 *p += ('a' - '0' - 10);
437 /*********************************************************************
440 MSVCRT_FILE * CDECL __p__iob(void)
442 return &MSVCRT__iob[0];
445 /*********************************************************************
448 int CDECL _access(const char *filename, int mode)
450 DWORD attr = GetFileAttributesA(filename);
452 TRACE("(%s,%d) %d\n",filename,mode,attr);
454 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
456 msvcrt_set_errno(GetLastError());
459 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
461 msvcrt_set_errno(ERROR_ACCESS_DENIED);
467 /*********************************************************************
468 * _waccess (MSVCRT.@)
470 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
472 DWORD attr = GetFileAttributesW(filename);
474 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
476 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
478 msvcrt_set_errno(GetLastError());
481 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
483 msvcrt_set_errno(ERROR_ACCESS_DENIED);
489 /*********************************************************************
492 int CDECL _chmod(const char *path, int flags)
494 DWORD oldFlags = GetFileAttributesA(path);
496 if (oldFlags != INVALID_FILE_ATTRIBUTES)
498 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
499 oldFlags | FILE_ATTRIBUTE_READONLY;
501 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
504 msvcrt_set_errno(GetLastError());
508 /*********************************************************************
511 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
513 DWORD oldFlags = GetFileAttributesW(path);
515 if (oldFlags != INVALID_FILE_ATTRIBUTES)
517 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
518 oldFlags | FILE_ATTRIBUTE_READONLY;
520 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
523 msvcrt_set_errno(GetLastError());
527 /*********************************************************************
530 int CDECL _unlink(const char *path)
532 TRACE("%s\n",debugstr_a(path));
533 if(DeleteFileA(path))
535 TRACE("failed (%d)\n",GetLastError());
536 msvcrt_set_errno(GetLastError());
540 /*********************************************************************
541 * _wunlink (MSVCRT.@)
543 int CDECL _wunlink(const MSVCRT_wchar_t *path)
545 TRACE("(%s)\n",debugstr_w(path));
546 if(DeleteFileW(path))
548 TRACE("failed (%d)\n",GetLastError());
549 msvcrt_set_errno(GetLastError());
553 /* _flushall calls MSVCRT_fflush which calls _flushall */
554 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
556 /*********************************************************************
557 * _flushall (MSVCRT.@)
559 int CDECL _flushall(void)
561 int i, num_flushed = 0;
564 for (i = 3; i < MSVCRT_stream_idx; i++)
565 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
568 /* FIXME: flush, do not commit */
569 if (_commit(i) == -1)
570 if (MSVCRT_fstreams[i])
571 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
573 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
574 MSVCRT_fflush(MSVCRT_fstreams[i]);
580 TRACE(":flushed (%d) handles\n",num_flushed);
584 /*********************************************************************
587 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
591 } else if(file->_flag & MSVCRT__IOWRT) {
592 int res=msvcrt_flush_buffer(file);
598 /*********************************************************************
601 int CDECL _close(int fd)
607 hand = msvcrt_fdtoh(fd);
608 TRACE(":fd (%d) handle (%p)\n",fd,hand);
609 if (hand == INVALID_HANDLE_VALUE)
611 else if (!CloseHandle(hand))
613 WARN(":failed-last error (%d)\n",GetLastError());
614 msvcrt_set_errno(GetLastError());
627 /*********************************************************************
630 int CDECL _commit(int fd)
632 HANDLE hand = msvcrt_fdtoh(fd);
634 TRACE(":fd (%d) handle (%p)\n",fd,hand);
635 if (hand == INVALID_HANDLE_VALUE)
638 if (!FlushFileBuffers(hand))
640 if (GetLastError() == ERROR_INVALID_HANDLE)
642 /* FlushFileBuffers fails for console handles
643 * so we ignore this error.
647 TRACE(":failed-last error (%d)\n",GetLastError());
648 msvcrt_set_errno(GetLastError());
655 /*********************************************************************
658 * MSDN isn't clear on this point, but the remarks for _pipe,
659 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
660 * indicate file descriptors duplicated with _dup and _dup2 are always
663 int CDECL _dup2(int od, int nd)
667 TRACE("(od=%d, nd=%d)\n", od, nd);
669 if (nd < MSVCRT_MAX_FILES && msvcrt_is_valid_fd(od))
673 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
674 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
676 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
678 if (msvcrt_is_valid_fd(nd))
680 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
684 *MSVCRT__errno() = MSVCRT_EMFILE;
688 /* _dup2 returns 0, not nd, on success */
695 msvcrt_set_errno(GetLastError());
700 *MSVCRT__errno() = MSVCRT_EBADF;
707 /*********************************************************************
710 int CDECL _dup(int od)
716 if (_dup2(od, fd) == 0)
724 /*********************************************************************
727 int CDECL _eof(int fd)
730 LONG hcurpos,hendpos;
731 HANDLE hand = msvcrt_fdtoh(fd);
733 TRACE(":fd (%d) handle (%p)\n",fd,hand);
735 if (hand == INVALID_HANDLE_VALUE)
738 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
740 /* Otherwise we do it the hard way */
741 hcurpos = hendpos = 0;
742 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
743 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
745 if (curpos == endpos && hcurpos == hendpos)
747 /* FIXME: shouldn't WX_ATEOF be set here? */
751 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
755 /*********************************************************************
756 * _fcloseall (MSVCRT.@)
758 int CDECL MSVCRT__fcloseall(void)
760 int num_closed = 0, i;
763 for (i = 3; i < MSVCRT_stream_idx; i++)
764 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
765 !MSVCRT_fclose(MSVCRT_fstreams[i]))
769 TRACE(":closed (%d) handles\n",num_closed);
773 /* free everything on process exit */
774 void msvcrt_free_io(void)
777 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
778 * stdout, and stderr (unlike GNU), so we need to fclose() them here
779 * or they won't get flushed.
781 MSVCRT_fclose(&MSVCRT__iob[0]);
782 MSVCRT_fclose(&MSVCRT__iob[1]);
783 MSVCRT_fclose(&MSVCRT__iob[2]);
784 DeleteCriticalSection(&MSVCRT_file_cs);
787 /*********************************************************************
788 * _lseeki64 (MSVCRT.@)
790 __int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
792 HANDLE hand = msvcrt_fdtoh(fd);
793 LARGE_INTEGER ofs, ret;
795 TRACE(":fd (%d) handle (%p)\n",fd,hand);
796 if (hand == INVALID_HANDLE_VALUE)
799 if (whence < 0 || whence > 2)
801 *MSVCRT__errno() = MSVCRT_EINVAL;
805 TRACE(":fd (%d) to %s pos %s\n",
806 fd,wine_dbgstr_longlong(offset),
807 (whence==SEEK_SET)?"SEEK_SET":
808 (whence==SEEK_CUR)?"SEEK_CUR":
809 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
811 ofs.QuadPart = offset;
812 if (SetFilePointerEx(hand, ofs, &ret, whence))
814 MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
815 /* FIXME: What if we seek _to_ EOF - is EOF set? */
819 TRACE(":error-last error (%d)\n",GetLastError());
820 msvcrt_set_errno(GetLastError());
824 /*********************************************************************
827 LONG CDECL _lseek(int fd, LONG offset, int whence)
829 return _lseeki64(fd, offset, whence);
832 /*********************************************************************
833 * _locking (MSVCRT.@)
835 * This is untested; the underlying LockFile doesn't work yet.
837 int CDECL _locking(int fd, int mode, LONG nbytes)
841 HANDLE hand = msvcrt_fdtoh(fd);
843 TRACE(":fd (%d) handle (%p)\n",fd,hand);
844 if (hand == INVALID_HANDLE_VALUE)
847 if (mode < 0 || mode > 4)
849 *MSVCRT__errno() = MSVCRT_EINVAL;
853 TRACE(":fd (%d) by 0x%08x mode %s\n",
854 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
855 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
856 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
857 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
858 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
861 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
863 FIXME ("Seek failed\n");
864 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
867 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
870 ret = 1; /* just to satisfy gcc */
873 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
878 else if (mode == MSVCRT__LK_UNLCK)
879 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
881 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
882 /* FIXME - what about error settings? */
886 /*********************************************************************
889 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
891 /* Flush output if needed */
892 if(file->_flag & MSVCRT__IOWRT)
893 msvcrt_flush_buffer(file);
895 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
896 offset -= file->_cnt;
898 /* Discard buffered input */
900 file->_ptr = file->_base;
901 /* Reset direction of i/o */
902 if(file->_flag & MSVCRT__IORW) {
903 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
905 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
908 /*********************************************************************
911 int CDECL _chsize(int fd, long size)
917 TRACE("(fd=%d, size=%ld)\n", fd, size);
921 handle = msvcrt_fdtoh(fd);
922 if (handle != INVALID_HANDLE_VALUE)
924 /* save the current file pointer */
925 cur = _lseek(fd, 0, SEEK_CUR);
928 pos = _lseek(fd, size, SEEK_SET);
931 ret = SetEndOfFile(handle);
932 if (!ret) msvcrt_set_errno(GetLastError());
935 /* restore the file pointer */
936 _lseek(fd, cur, SEEK_SET);
944 /*********************************************************************
945 * clearerr (MSVCRT.@)
947 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
949 TRACE(":file (%p) fd (%d)\n",file,file->_file);
950 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
953 /*********************************************************************
956 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
958 TRACE(":file (%p) fd (%d)\n",file,file->_file);
959 MSVCRT_fseek(file, 0L, SEEK_SET);
960 MSVCRT_clearerr(file);
963 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
965 int plus = strchr(mode, '+') != NULL;
970 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
971 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
974 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
975 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
978 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
979 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
989 *open_flags |= MSVCRT__O_BINARY;
990 *open_flags &= ~MSVCRT__O_TEXT;
993 *open_flags |= MSVCRT__O_TEXT;
994 *open_flags &= ~MSVCRT__O_BINARY;
999 FIXME(":unknown flag %c not supported\n",mode[-1]);
1004 /*********************************************************************
1005 * _fdopen (MSVCRT.@)
1007 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1009 int open_flags, stream_flags;
1012 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1015 if (!(file = msvcrt_alloc_fp()))
1017 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1022 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1028 /*********************************************************************
1029 * _wfdopen (MSVCRT.@)
1031 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1033 unsigned mlen = strlenW(mode);
1034 char *modea = MSVCRT_calloc(mlen + 1, 1);
1035 MSVCRT_FILE* file = NULL;
1036 int open_flags, stream_flags;
1039 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1041 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1043 if (!(file = msvcrt_alloc_fp()))
1045 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1053 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1054 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1061 /*********************************************************************
1062 * _filelength (MSVCRT.@)
1064 LONG CDECL _filelength(int fd)
1066 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1069 LONG endPos = _lseek(fd, 0, SEEK_END);
1072 if (endPos != curPos)
1073 _lseek(fd, curPos, SEEK_SET);
1080 /*********************************************************************
1081 * _filelengthi64 (MSVCRT.@)
1083 __int64 CDECL _filelengthi64(int fd)
1085 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1088 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1091 if (endPos != curPos)
1092 _lseeki64(fd, curPos, SEEK_SET);
1099 /*********************************************************************
1100 * _fileno (MSVCRT.@)
1102 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1104 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1108 /*********************************************************************
1109 * _fstati64 (MSVCRT.@)
1111 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1114 BY_HANDLE_FILE_INFORMATION hfi;
1115 HANDLE hand = msvcrt_fdtoh(fd);
1117 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1118 if (hand == INVALID_HANDLE_VALUE)
1123 WARN(":failed-NULL buf\n");
1124 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1128 memset(&hfi, 0, sizeof(hfi));
1129 memset(buf, 0, sizeof(struct MSVCRT__stati64));
1130 if (!GetFileInformationByHandle(hand, &hfi))
1132 WARN(":failed-last error (%d)\n",GetLastError());
1133 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1136 dw = GetFileType(hand);
1137 buf->st_mode = S_IREAD;
1138 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1139 buf->st_mode |= S_IWRITE;
1140 /* interestingly, Windows never seems to set S_IFDIR */
1141 if (dw == FILE_TYPE_CHAR)
1142 buf->st_mode |= S_IFCHR;
1143 else if (dw == FILE_TYPE_PIPE)
1144 buf->st_mode |= S_IFIFO;
1146 buf->st_mode |= S_IFREG;
1147 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1149 buf->st_nlink = hfi.nNumberOfLinks;
1150 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1151 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1153 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1154 buf->st_mtime = buf->st_ctime = dw;
1158 /*********************************************************************
1161 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1163 struct MSVCRT__stati64 bufi64;
1165 ret = MSVCRT__fstati64(fd, &bufi64);
1167 msvcrt_cp_from_stati64(&bufi64, buf);
1171 /*********************************************************************
1172 * _futime (MSVCRT.@)
1174 int CDECL _futime(int fd, struct MSVCRT__utimbuf *t)
1176 HANDLE hand = msvcrt_fdtoh(fd);
1181 MSVCRT_time_t currTime;
1182 MSVCRT_time(&currTime);
1183 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1184 memcpy(&wt, &at, sizeof(wt));
1188 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1189 if (t->actime == t->modtime)
1190 memcpy(&wt, &at, sizeof(wt));
1192 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1195 if (!SetFileTime(hand, NULL, &at, &wt))
1197 msvcrt_set_errno(GetLastError());
1203 /*********************************************************************
1204 * _get_osfhandle (MSVCRT.@)
1206 long CDECL _get_osfhandle(int fd)
1208 HANDLE hand = msvcrt_fdtoh(fd);
1209 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1214 /*********************************************************************
1215 * _isatty (MSVCRT.@)
1217 int CDECL _isatty(int fd)
1219 HANDLE hand = msvcrt_fdtoh(fd);
1221 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1222 if (hand == INVALID_HANDLE_VALUE)
1225 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1228 /*********************************************************************
1229 * _mktemp (MSVCRT.@)
1231 char * CDECL _mktemp(char *pattern)
1234 char *retVal = pattern;
1239 numX = (*pattern++ == 'X')? numX + 1 : 0;
1243 id = GetCurrentProcessId();
1247 int tempNum = id / 10;
1248 *pattern-- = id - (tempNum * 10) + '0';
1254 *pattern = letter++;
1255 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1256 GetLastError() == ERROR_FILE_NOT_FOUND)
1258 } while(letter <= 'z');
1262 /*********************************************************************
1263 * _wmktemp (MSVCRT.@)
1265 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1268 MSVCRT_wchar_t *retVal = pattern;
1270 MSVCRT_wchar_t letter = 'a';
1273 numX = (*pattern++ == 'X')? numX + 1 : 0;
1277 id = GetCurrentProcessId();
1281 int tempNum = id / 10;
1282 *pattern-- = id - (tempNum * 10) + '0';
1288 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1289 GetLastError() == ERROR_FILE_NOT_FOUND)
1291 *pattern = letter++;
1292 } while(letter != '|');
1296 static unsigned split_oflags(unsigned oflags)
1299 unsigned unsupp; /* until we support everything */
1301 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1302 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1303 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1304 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1305 else wxflags |= WX_TEXT; /* default to TEXT*/
1306 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1308 if ((unsupp = oflags & ~(
1309 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1310 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1311 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1312 MSVCRT__O_NOINHERIT|
1313 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1315 ERR(":unsupported oflags 0x%04x\n",unsupp);
1320 /*********************************************************************
1323 int CDECL _pipe(int *pfds, unsigned int psize, int textmode)
1326 SECURITY_ATTRIBUTES sa;
1327 HANDLE readHandle, writeHandle;
1331 *MSVCRT__errno() = MSVCRT_EINVAL;
1335 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1336 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1337 sa.lpSecurityDescriptor = NULL;
1338 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1340 unsigned int wxflags = split_oflags(textmode);
1344 fd = msvcrt_alloc_fd(readHandle, wxflags);
1348 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1357 CloseHandle(writeHandle);
1358 *MSVCRT__errno() = MSVCRT_EMFILE;
1363 CloseHandle(readHandle);
1364 CloseHandle(writeHandle);
1365 *MSVCRT__errno() = MSVCRT_EMFILE;
1370 msvcrt_set_errno(GetLastError());
1375 /*********************************************************************
1378 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1382 DWORD access = 0, creation = 0, attrib;
1386 SECURITY_ATTRIBUTES sa;
1389 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1390 path, oflags, shflags);
1392 wxflag = split_oflags(oflags);
1393 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1395 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1396 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1397 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1400 if (oflags & MSVCRT__O_CREAT)
1402 va_start(ap, shflags);
1403 pmode = va_arg(ap, int);
1406 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1407 FIXME(": pmode 0x%04x ignored\n", pmode);
1409 WARN(": pmode 0x%04x ignored\n", pmode);
1411 if (oflags & MSVCRT__O_EXCL)
1412 creation = CREATE_NEW;
1413 else if (oflags & MSVCRT__O_TRUNC)
1414 creation = CREATE_ALWAYS;
1416 creation = OPEN_ALWAYS;
1418 else /* no MSVCRT__O_CREAT */
1420 if (oflags & MSVCRT__O_TRUNC)
1421 creation = TRUNCATE_EXISTING;
1423 creation = OPEN_EXISTING;
1428 case MSVCRT__SH_DENYRW:
1431 case MSVCRT__SH_DENYWR:
1432 sharing = FILE_SHARE_READ;
1434 case MSVCRT__SH_DENYRD:
1435 sharing = FILE_SHARE_WRITE;
1437 case MSVCRT__SH_DENYNO:
1438 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1441 ERR( "Unhandled shflags 0x%x\n", shflags );
1444 attrib = FILE_ATTRIBUTE_NORMAL;
1446 if (oflags & MSVCRT__O_TEMPORARY)
1448 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1450 sharing |= FILE_SHARE_DELETE;
1453 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1454 sa.lpSecurityDescriptor = NULL;
1455 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1457 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1459 if (hand == INVALID_HANDLE_VALUE) {
1460 WARN(":failed-last error (%d)\n",GetLastError());
1461 msvcrt_set_errno(GetLastError());
1465 fd = msvcrt_alloc_fd(hand, wxflag);
1467 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1471 /*********************************************************************
1472 * _wsopen (MSVCRT.@)
1474 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1476 const unsigned int len = strlenW(path);
1477 char *patha = MSVCRT_calloc(len + 1,1);
1481 va_start(ap, shflags);
1482 pmode = va_arg(ap, int);
1485 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1487 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1492 msvcrt_set_errno(GetLastError());
1496 /*********************************************************************
1499 int CDECL _open( const char *path, int flags, ... )
1503 if (flags & MSVCRT__O_CREAT)
1506 va_start(ap, flags);
1507 pmode = va_arg(ap, int);
1509 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1512 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1515 /*********************************************************************
1518 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1520 const unsigned int len = strlenW(path);
1521 char *patha = MSVCRT_calloc(len + 1,1);
1525 va_start(ap, flags);
1526 pmode = va_arg(ap, int);
1529 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1531 int retval = _open(patha,flags,pmode);
1536 msvcrt_set_errno(GetLastError());
1540 /*********************************************************************
1543 int CDECL _creat(const char *path, int flags)
1545 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1546 return _open(path, usedFlags);
1549 /*********************************************************************
1550 * _wcreat (MSVCRT.@)
1552 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1554 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1555 return _wopen(path, usedFlags);
1558 /*********************************************************************
1559 * _open_osfhandle (MSVCRT.@)
1561 int CDECL _open_osfhandle(long handle, int oflags)
1565 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1566 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1567 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1568 * text - it never sets MSVCRT__O_BINARY.
1570 /* don't let split_oflags() decide the mode if no mode is passed */
1571 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1572 oflags |= MSVCRT__O_BINARY;
1574 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1575 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1579 /*********************************************************************
1582 int CDECL _rmtmp(void)
1584 int num_removed = 0, i;
1587 for (i = 3; i < MSVCRT_stream_idx; i++)
1588 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1590 MSVCRT_fclose(MSVCRT_fstreams[i]);
1596 TRACE(":removed (%d) temp files\n",num_removed);
1600 /*********************************************************************
1601 * (internal) remove_cr
1603 * Remove all \r inplace.
1604 * return the number of \r removed
1606 static unsigned int remove_cr(char *buf, unsigned int count)
1610 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1611 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1615 /*********************************************************************
1618 static int read_i(int fd, void *buf, unsigned int count)
1621 char *bufstart = buf;
1622 HANDLE hand = msvcrt_fdtoh(fd);
1624 /* Don't trace small reads, it gets *very* annoying */
1626 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1627 if (hand == INVALID_HANDLE_VALUE)
1630 /* Reading single bytes in O_TEXT mode makes things slow
1631 * So read big chunks
1633 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1635 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1638 /* in text mode, a ctrl-z signals EOF */
1639 for (i=0; i<num_read; i++)
1641 if (bufstart[i] == 0x1a)
1648 if (num_read != count)
1650 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1651 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1656 TRACE(":failed-last error (%d)\n",GetLastError());
1661 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1665 /*********************************************************************
1668 int CDECL _read(int fd, void *buf, unsigned int count)
1671 num_read = read_i(fd, buf, count);
1672 if (num_read>0 && MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1674 num_read -= remove_cr(buf,num_read);
1679 /*********************************************************************
1680 * _setmode (MSVCRT.@)
1682 int CDECL _setmode(int fd,int mode)
1684 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1685 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1686 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1687 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1688 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1690 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1694 /*********************************************************************
1695 * _stati64 (MSVCRT.@)
1697 int CDECL MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1700 WIN32_FILE_ATTRIBUTE_DATA hfi;
1701 unsigned short mode = ALL_S_IREAD;
1704 TRACE(":file (%s) buf(%p)\n",path,buf);
1706 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1708 TRACE("failed (%d)\n",GetLastError());
1709 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1713 memset(buf,0,sizeof(struct MSVCRT__stati64));
1715 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1716 Bon 011120: This FIXME seems incorrect
1717 Also a letter as first char isn't enough to be classified
1720 if (isalpha(*path)&& (*(path+1)==':'))
1721 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1723 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1725 plen = strlen(path);
1727 /* Dir, or regular file? */
1728 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1729 (path[plen-1] == '\\'))
1730 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1733 mode |= MSVCRT__S_IFREG;
1735 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1737 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1738 (tolower(path[plen-3]) << 16);
1739 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1740 mode |= ALL_S_IEXEC;
1744 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1745 mode |= ALL_S_IWRITE;
1747 buf->st_mode = mode;
1749 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1750 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1752 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1753 buf->st_mtime = buf->st_ctime = dw;
1754 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1755 (long)(buf->st_size >> 32),(long)buf->st_size,
1756 buf->st_atime,buf->st_mtime, buf->st_ctime);
1760 /*********************************************************************
1763 int CDECL MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1765 struct MSVCRT__stati64 bufi64;
1767 ret = MSVCRT__stati64( path, &bufi64);
1769 msvcrt_cp_from_stati64(&bufi64, buf);
1773 /*********************************************************************
1774 * _wstati64 (MSVCRT.@)
1776 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1779 WIN32_FILE_ATTRIBUTE_DATA hfi;
1780 unsigned short mode = ALL_S_IREAD;
1783 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1785 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1787 TRACE("failed (%d)\n",GetLastError());
1788 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1792 memset(buf,0,sizeof(struct MSVCRT__stat));
1794 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1795 if (MSVCRT_iswalpha(*path))
1796 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1798 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1800 plen = strlenW(path);
1802 /* Dir, or regular file? */
1803 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1804 (path[plen-1] == '\\'))
1805 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1808 mode |= MSVCRT__S_IFREG;
1810 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1812 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1813 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1814 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1815 mode |= ALL_S_IEXEC;
1819 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1820 mode |= ALL_S_IWRITE;
1822 buf->st_mode = mode;
1824 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1825 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1827 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1828 buf->st_mtime = buf->st_ctime = dw;
1829 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1830 (long)(buf->st_size >> 32),(long)buf->st_size,
1831 buf->st_atime,buf->st_mtime, buf->st_ctime);
1835 /*********************************************************************
1838 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1841 struct MSVCRT__stati64 bufi64;
1843 ret = MSVCRT__wstati64( path, &bufi64 );
1844 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1848 /*********************************************************************
1851 long CDECL _tell(int fd)
1853 return _lseek(fd, 0, SEEK_CUR);
1856 /*********************************************************************
1857 * _telli64 (MSVCRT.@)
1859 __int64 CDECL _telli64(int fd)
1861 return _lseeki64(fd, 0, SEEK_CUR);
1864 /*********************************************************************
1865 * _tempnam (MSVCRT.@)
1867 char * CDECL _tempnam(const char *dir, const char *prefix)
1869 char tmpbuf[MAX_PATH];
1870 const char *tmp_dir = MSVCRT_getenv("TMP");
1872 if (tmp_dir) dir = tmp_dir;
1874 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1875 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1877 TRACE("got name (%s)\n",tmpbuf);
1878 DeleteFileA(tmpbuf);
1879 return _strdup(tmpbuf);
1881 TRACE("failed (%d)\n",GetLastError());
1885 /*********************************************************************
1886 * _wtempnam (MSVCRT.@)
1888 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1890 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1892 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1893 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1895 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1896 DeleteFileW(tmpbuf);
1897 return _wcsdup(tmpbuf);
1899 TRACE("failed (%d)\n",GetLastError());
1903 /*********************************************************************
1906 int CDECL _umask(int umask)
1908 int old_umask = MSVCRT_umask;
1909 TRACE("(%d)\n",umask);
1910 MSVCRT_umask = umask;
1914 /*********************************************************************
1917 int CDECL _utime(const char* path, struct MSVCRT__utimbuf *t)
1919 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1923 int retVal = _futime(fd, t);
1930 /*********************************************************************
1931 * _wutime (MSVCRT.@)
1933 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1935 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1939 int retVal = _futime(fd, t);
1946 /*********************************************************************
1949 int CDECL _write(int fd, const void* buf, unsigned int count)
1952 HANDLE hand = msvcrt_fdtoh(fd);
1954 /* Don't trace small writes, it gets *very* annoying */
1957 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1959 if (hand == INVALID_HANDLE_VALUE)
1961 *MSVCRT__errno() = MSVCRT_EBADF;
1965 /* If appending, go to EOF */
1966 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
1967 _lseek(fd, 0, FILE_END);
1969 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
1971 if (WriteFile(hand, buf, count, &num_written, NULL)
1972 && (num_written == count))
1974 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
1975 hand, GetLastError());
1976 *MSVCRT__errno() = MSVCRT_ENOSPC;
1980 unsigned int i, j, nr_lf;
1983 const char *s = (const char *)buf, *buf_start = (const char *)buf;
1984 /* find number of \n ( without preceding \r ) */
1985 for ( nr_lf=0,i = 0; i <count; i++)
1990 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1995 if ((q = p = MSVCRT_malloc(count + nr_lf)))
1997 for (s = (const char *)buf, i = 0, j = 0; i < count; i++)
2002 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2009 FIXME("Malloc failed\n");
2017 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2019 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2020 fd, hand, GetLastError(), num_written);
2021 *MSVCRT__errno() = MSVCRT_ENOSPC;
2024 return s - buf_start;
2036 /*********************************************************************
2039 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2042 len = _write(file->_file, &val, sizeof(val));
2043 if (len == sizeof(val)) return val;
2044 file->_flag |= MSVCRT__IOERR;
2048 /*********************************************************************
2051 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2056 MSVCRT_free(file->_tmpfname);
2057 file->_tmpfname = NULL;
2058 /* flush stdio buffers */
2059 if(file->_flag & MSVCRT__IOWRT)
2060 MSVCRT_fflush(file);
2061 if(file->_flag & MSVCRT__IOMYBUF)
2062 MSVCRT_free(file->_base);
2064 r=_close(file->_file);
2068 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2071 /*********************************************************************
2074 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2076 return file->_flag & MSVCRT__IOEOF;
2079 /*********************************************************************
2082 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2084 return file->_flag & MSVCRT__IOERR;
2087 /*********************************************************************
2088 * _filbuf (MSVCRT.@)
2090 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2092 /* Allocate buffer if needed */
2093 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2094 msvcrt_alloc_buffer(file);
2096 if(!(file->_flag & MSVCRT__IOREAD)) {
2097 if(file->_flag & MSVCRT__IORW) {
2098 file->_flag |= MSVCRT__IOREAD;
2103 if(file->_flag & MSVCRT__IONBF) {
2106 if ((r = read_i(file->_file,&c,1)) != 1) {
2107 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2112 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2114 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2119 file->_ptr = file->_base+1;
2120 return *(unsigned char *)file->_base;
2124 /*********************************************************************
2127 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2134 i = (unsigned char *)file->_ptr++;
2137 j = MSVCRT__filbuf(file);
2138 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) || (j != '\r'))
2143 /*********************************************************************
2144 * _fgetchar (MSVCRT.@)
2146 int CDECL _fgetchar(void)
2148 return MSVCRT_fgetc(MSVCRT_stdin);
2151 /*********************************************************************
2154 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2156 int cc = MSVCRT_EOF;
2157 char * buf_start = s;
2159 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2160 file,file->_file,s,size);
2162 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2167 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2169 TRACE(":nothing read\n");
2172 if ((cc != MSVCRT_EOF) && (size > 1))
2175 TRACE(":got %s\n", debugstr_a(buf_start));
2179 /*********************************************************************
2182 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2183 * the CR from CR/LF combinations
2185 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2189 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2195 for(i=0; i<sizeof(wc); i++)
2205 j = MSVCRT__filbuf(file);
2208 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2218 c = MSVCRT_fgetc(file);
2219 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2221 FIXME("Treat Multibyte characters\n");
2223 if (c == MSVCRT_EOF)
2226 return (MSVCRT_wint_t)c;
2229 /*********************************************************************
2232 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2237 for (j=0; j<sizeof(int); j++) {
2238 k = MSVCRT_fgetc(file);
2239 if (k == MSVCRT_EOF) {
2240 file->_flag |= MSVCRT__IOEOF;
2248 /*********************************************************************
2251 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2253 return MSVCRT_fgetwc(file);
2256 /*********************************************************************
2257 * _fgetwchar (MSVCRT.@)
2259 MSVCRT_wint_t CDECL _fgetwchar(void)
2261 return MSVCRT_fgetwc(MSVCRT_stdin);
2264 /*********************************************************************
2265 * getwchar (MSVCRT.@)
2267 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2269 return _fgetwchar();
2272 /*********************************************************************
2275 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2277 int cc = MSVCRT_WEOF;
2278 MSVCRT_wchar_t * buf_start = s;
2280 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2281 file,file->_file,s,size);
2283 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2288 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2290 TRACE(":nothing read\n");
2293 if ((cc != MSVCRT_WEOF) && (size > 1))
2296 TRACE(":got %s\n", debugstr_w(buf_start));
2300 /*********************************************************************
2303 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2305 MSVCRT_size_t wrcnt=size * nmemb;
2310 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2311 memcpy(file->_ptr, ptr, pcnt);
2316 ptr = (const char*)ptr + pcnt;
2317 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2318 if(file->_flag & MSVCRT__IORW) {
2319 file->_flag |= MSVCRT__IOWRT;
2325 int res=msvcrt_flush_buffer(file);
2327 int pwritten = _write(file->_file, ptr, wrcnt);
2330 file->_flag |= MSVCRT__IOERR;
2333 written += pwritten;
2336 return written / size;
2339 /*********************************************************************
2342 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2344 MSVCRT_wchar_t mwc=wc;
2345 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2350 /*********************************************************************
2351 * _fputwchar (MSVCRT.@)
2353 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2355 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2358 /*********************************************************************
2359 * _fsopen (MSVCRT.@)
2361 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2364 int open_flags, stream_flags, fd;
2366 TRACE("(%s,%s)\n",path,mode);
2368 /* map mode string to open() flags. "man fopen" for possibilities. */
2369 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2373 fd = MSVCRT__sopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2376 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2378 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2385 TRACE(":got (%p)\n",file);
2386 if (fd >= 0 && !file)
2392 /*********************************************************************
2393 * _wfsopen (MSVCRT.@)
2395 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2397 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2398 char *patha = MSVCRT_calloc(plen + 1, 1);
2399 char *modea = MSVCRT_calloc(mlen + 1, 1);
2401 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2403 if (patha && modea &&
2404 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2405 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2407 MSVCRT_FILE *retval = MSVCRT__fsopen(patha,modea,share);
2413 msvcrt_set_errno(GetLastError());
2417 /*********************************************************************
2420 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2422 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2425 /*********************************************************************
2426 * _wfopen (MSVCRT.@)
2428 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2430 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2433 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2434 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2436 /*********************************************************************
2439 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2446 int res = msvcrt_flush_buffer(file);
2447 return res ? res : c;
2452 return MSVCRT__flsbuf(c, file);
2456 /*********************************************************************
2457 * _flsbuf (MSVCRT.@)
2459 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2461 /* Flush output buffer */
2462 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2463 msvcrt_alloc_buffer(file);
2465 if(!(file->_flag & MSVCRT__IOWRT)) {
2466 if(file->_flag & MSVCRT__IORW) {
2467 file->_flag |= MSVCRT__IOWRT;
2473 int res=msvcrt_flush_buffer(file);
2474 return res?res : MSVCRT_fputc(c, file);
2478 len = _write(file->_file, &cc, 1);
2479 if (len == 1) return c;
2480 file->_flag |= MSVCRT__IOERR;
2485 /*********************************************************************
2486 * _fputchar (MSVCRT.@)
2488 int CDECL _fputchar(int c)
2490 return MSVCRT_fputc(c, MSVCRT_stdout);
2493 /*********************************************************************
2496 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2497 { MSVCRT_size_t rcnt=size * nmemb;
2498 MSVCRT_size_t read=0;
2504 /* first buffered data */
2506 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2507 memcpy(ptr, file->_ptr, pcnt);
2510 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
2511 pcnt -= remove_cr(ptr,pcnt);
2514 ptr = (char*)ptr + pcnt;
2515 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2516 if(file->_flag & MSVCRT__IORW) {
2517 file->_flag |= MSVCRT__IOREAD;
2524 /* Fill the buffer on small reads.
2525 * TODO: Use a better buffering strategy.
2527 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2528 if (file->_bufsiz == 0) {
2529 msvcrt_alloc_buffer(file);
2531 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2532 file->_ptr = file->_base;
2533 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2534 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2535 if (i > 0 && i < file->_cnt) {
2536 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2537 file->_flag &= ~MSVCRT__IOEOF;
2540 memcpy(ptr, file->_ptr, i);
2545 i = _read(file->_file,ptr, rcnt);
2549 ptr = (char *)ptr+i;
2550 /* expose feof condition in the flags
2551 * MFC tests file->_flag for feof, and doesn't call feof())
2553 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2554 file->_flag |= MSVCRT__IOEOF;
2557 file->_flag |= MSVCRT__IOERR;
2567 /*********************************************************************
2568 * freopen (MSVCRT.@)
2571 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2573 int open_flags, stream_flags, fd;
2575 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2578 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2582 MSVCRT_fclose(file);
2583 /* map mode string to open() flags. "man fopen" for possibilities. */
2584 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2588 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2591 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2594 WARN(":failed-last error (%d)\n",GetLastError());
2595 msvcrt_set_errno(GetLastError());
2604 /*********************************************************************
2605 * fsetpos (MSVCRT.@)
2607 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2609 /* Note that all this has been lifted 'as is' from fseek */
2610 if(file->_flag & MSVCRT__IOWRT)
2611 msvcrt_flush_buffer(file);
2613 /* Discard buffered input */
2615 file->_ptr = file->_base;
2617 /* Reset direction of i/o */
2618 if(file->_flag & MSVCRT__IORW) {
2619 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2622 return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2625 /*********************************************************************
2628 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2633 if( file->_flag & MSVCRT__IOWRT ) {
2634 off = file->_ptr - file->_base;
2639 pos = _tell(file->_file);
2640 if(pos == -1) return pos;
2644 /*********************************************************************
2645 * fgetpos (MSVCRT.@)
2647 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2649 /* This code has been lifted form the MSVCRT_ftell function */
2652 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2654 if (*pos == -1) return -1;
2657 if( file->_flag & MSVCRT__IOWRT ) {
2658 off = file->_ptr - file->_base;
2668 /*********************************************************************
2671 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2673 size_t i, len = strlen(s);
2674 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2675 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2676 for (i=0; i<len; i++)
2677 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2682 /*********************************************************************
2685 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2687 size_t i, len = strlenW(s);
2688 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2689 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2690 for (i=0; i<len; i++)
2692 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2694 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2700 /*********************************************************************
2701 * getchar (MSVCRT.@)
2703 int CDECL MSVCRT_getchar(void)
2705 return MSVCRT_fgetc(MSVCRT_stdin);
2708 /*********************************************************************
2711 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
2713 return MSVCRT_fgetc(file);
2716 /*********************************************************************
2719 char * CDECL MSVCRT_gets(char *buf)
2722 char * buf_start = buf;
2724 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2725 cc = MSVCRT_fgetc(MSVCRT_stdin))
2726 if(cc != '\r') *buf++ = (char)cc;
2730 TRACE("got '%s'\n", buf_start);
2734 /*********************************************************************
2737 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
2740 MSVCRT_wchar_t* ws = buf;
2742 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2743 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2746 *buf++ = (MSVCRT_wchar_t)cc;
2750 TRACE("got %s\n", debugstr_w(ws));
2754 /*********************************************************************
2757 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
2759 return MSVCRT_fputc(c, file);
2762 /*********************************************************************
2763 * putchar (MSVCRT.@)
2765 int CDECL MSVCRT_putchar(int c)
2767 return MSVCRT_fputc(c, MSVCRT_stdout);
2770 /*********************************************************************
2773 int CDECL MSVCRT_puts(const char *s)
2775 size_t len = strlen(s);
2776 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2777 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2780 /*********************************************************************
2783 int CDECL _putws(const MSVCRT_wchar_t *s)
2785 static const MSVCRT_wchar_t nl = '\n';
2786 size_t len = strlenW(s);
2787 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2788 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2791 /*********************************************************************
2794 int CDECL MSVCRT_remove(const char *path)
2796 TRACE("(%s)\n",path);
2797 if (DeleteFileA(path))
2799 TRACE(":failed (%d)\n",GetLastError());
2800 msvcrt_set_errno(GetLastError());
2804 /*********************************************************************
2805 * _wremove (MSVCRT.@)
2807 int CDECL _wremove(const MSVCRT_wchar_t *path)
2809 TRACE("(%s)\n",debugstr_w(path));
2810 if (DeleteFileW(path))
2812 TRACE(":failed (%d)\n",GetLastError());
2813 msvcrt_set_errno(GetLastError());
2817 /*********************************************************************
2820 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
2822 TRACE(":from %s to %s\n",oldpath,newpath);
2823 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2825 TRACE(":failed (%d)\n",GetLastError());
2826 msvcrt_set_errno(GetLastError());
2830 /*********************************************************************
2831 * _wrename (MSVCRT.@)
2833 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2835 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2836 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2838 TRACE(":failed (%d)\n",GetLastError());
2839 msvcrt_set_errno(GetLastError());
2843 /*********************************************************************
2844 * setvbuf (MSVCRT.@)
2846 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2848 /* TODO: Check if file busy */
2850 MSVCRT_free(file->_base);
2854 if(mode == MSVCRT__IOFBF) {
2855 file->_flag &= ~MSVCRT__IONBF;
2856 file->_base = file->_ptr = buf;
2858 file->_bufsiz = size;
2861 file->_flag |= MSVCRT__IONBF;
2866 /*********************************************************************
2869 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2871 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2874 /*********************************************************************
2877 char * CDECL MSVCRT_tmpnam(char *s)
2885 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2886 p = s + sprintf(s, "\\s%s.", tmpstr);
2887 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2889 msvcrt_int_to_base32(unique++, tmpstr);
2891 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2892 GetLastError() == ERROR_FILE_NOT_FOUND)
2898 /*********************************************************************
2899 * tmpfile (MSVCRT.@)
2901 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
2903 char *filename = MSVCRT_tmpnam(NULL);
2905 MSVCRT_FILE* file = NULL;
2908 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2909 if (fd != -1 && (file = msvcrt_alloc_fp()))
2911 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2916 else file->_tmpfname = _strdup(filename);
2922 /*********************************************************************
2923 * vfprintf (MSVCRT.@)
2925 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2927 char buf[2048], *mem = buf;
2928 int written, resize = sizeof(buf), retval;
2929 /* There are two conventions for vsnprintf failing:
2930 * Return -1 if we truncated, or
2931 * Return the number of bytes that would have been written
2932 * The code below handles both cases
2934 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
2937 resize = (written == -1 ? resize * 2 : written + 1);
2940 if (!(mem = (char *)MSVCRT_malloc(resize)))
2943 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2949 /*********************************************************************
2950 * vfwprintf (MSVCRT.@)
2952 * Is final char included in written (then resize is too big) or not
2953 * (then we must test for equality too)?
2955 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2957 MSVCRT_wchar_t buf[2048], *mem = buf;
2958 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2959 /* See vfprintf comments */
2960 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
2963 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2966 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2969 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2975 /*********************************************************************
2976 * vprintf (MSVCRT.@)
2978 int CDECL MSVCRT_vprintf(const char *format, va_list valist)
2980 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2983 /*********************************************************************
2984 * vwprintf (MSVCRT.@)
2986 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2988 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2991 /*********************************************************************
2992 * fprintf (MSVCRT.@)
2994 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2998 va_start(valist, format);
2999 res = MSVCRT_vfprintf(file, format, valist);
3004 /*********************************************************************
3005 * fwprintf (MSVCRT.@)
3007 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3011 va_start(valist, format);
3012 res = MSVCRT_vfwprintf(file, format, valist);
3017 /*********************************************************************
3020 int CDECL MSVCRT_printf(const char *format, ...)
3024 va_start(valist, format);
3025 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3030 /*********************************************************************
3033 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3035 if (c == MSVCRT_EOF)
3037 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3038 msvcrt_alloc_buffer(file);
3041 if(file->_ptr>file->_base) {
3045 MSVCRT_clearerr(file);
3051 /*********************************************************************
3052 * ungetwc (MSVCRT.@)
3054 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3056 MSVCRT_wchar_t mwc = wc;
3057 char * pp = (char *)&mwc;
3059 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3060 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3066 /*********************************************************************
3067 * wprintf (MSVCRT.@)
3069 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3073 va_start(valist, format);
3074 res = MSVCRT_vwprintf(format, valist);
3079 /*********************************************************************
3080 * _getmaxstdio (MSVCRT.@)
3082 int CDECL _getmaxstdio(void)
3084 FIXME("stub, always returns 512\n");
3088 /*********************************************************************
3089 * _setmaxstdio_ (MSVCRT.@)
3091 int CDECL _setmaxstdio(int newmax)
3098 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3102 /*********************************************************************
3103 * __pioinfo (MSVCRT.@)
3104 * FIXME: see MSVCRT_MAX_FILES define.
3106 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3107 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3108 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3109 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3110 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3111 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3112 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3113 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3114 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3115 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3116 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3117 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3120 /*********************************************************************
3121 * __badioinfo (MSVCRT.@)
3123 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };