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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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, FALSE,
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, FALSE,
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, FALSE,
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 = (unsigned 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 *__p__iob(void)
442 return &MSVCRT__iob[0];
445 /*********************************************************************
448 int _access(const char *filename, int mode)
450 DWORD attr = GetFileAttributesA(filename);
452 TRACE("(%s,%d) %ld\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 _waccess(const MSVCRT_wchar_t *filename, int mode)
472 DWORD attr = GetFileAttributesW(filename);
474 TRACE("(%s,%d) %ld\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 _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 _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 _unlink(const char *path)
532 TRACE("(%s)\n",path);
533 if(DeleteFileA(path))
535 TRACE("failed (%ld)\n",GetLastError());
536 msvcrt_set_errno(GetLastError());
540 /*********************************************************************
541 * _wunlink (MSVCRT.@)
543 int _wunlink(const MSVCRT_wchar_t *path)
545 TRACE("(%s)\n",debugstr_w(path));
546 if(DeleteFileW(path))
548 TRACE("failed (%ld)\n",GetLastError());
549 msvcrt_set_errno(GetLastError());
553 /* _flushall calls MSVCRT_fflush which calls _flushall */
554 int MSVCRT_fflush(MSVCRT_FILE* file);
556 /*********************************************************************
557 * _flushall (MSVCRT.@)
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 MSVCRT_fflush(MSVCRT_FILE* file)
593 int res=msvcrt_flush_buffer(file);
598 /*********************************************************************
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 (%ld)\n",GetLastError());
614 msvcrt_set_errno(GetLastError());
627 /*********************************************************************
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 (%ld)\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 _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 /*********************************************************************
716 if (_dup2(od, fd) == 0)
724 /*********************************************************************
729 DWORD curpos,endpos,hcurpos,hendpos;
730 HANDLE hand = msvcrt_fdtoh(fd);
732 TRACE(":fd (%d) handle (%p)\n",fd,hand);
734 if (hand == INVALID_HANDLE_VALUE)
737 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
739 /* Otherwise we do it the hard way */
740 hcurpos = hendpos = 0;
741 curpos = SetFilePointer(hand, 0, &hcurpos, SEEK_CUR);
742 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
744 if (curpos == endpos && hcurpos == hendpos)
746 /* FIXME: shouldn't WX_ATEOF be set here? */
750 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
754 /*********************************************************************
755 * _fcloseall (MSVCRT.@)
757 int MSVCRT__fcloseall(void)
759 int num_closed = 0, i;
762 for (i = 3; i < MSVCRT_stream_idx; i++)
763 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
764 MSVCRT_fclose(MSVCRT_fstreams[i]))
768 TRACE(":closed (%d) handles\n",num_closed);
772 /* free everything on process exit */
773 void msvcrt_free_io(void)
779 DeleteCriticalSection(&MSVCRT_file_cs);
782 /*********************************************************************
783 * _lseeki64 (MSVCRT.@)
785 __int64 _lseeki64(int fd, __int64 offset, int whence)
787 DWORD ret, hoffset = (DWORD) (offset >> 32);
788 HANDLE hand = msvcrt_fdtoh(fd);
790 TRACE(":fd (%d) handle (%p)\n",fd,hand);
791 if (hand == INVALID_HANDLE_VALUE)
794 if (whence < 0 || whence > 2)
796 *MSVCRT__errno() = MSVCRT_EINVAL;
800 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
801 fd,hoffset,(long)offset,
802 (whence==SEEK_SET)?"SEEK_SET":
803 (whence==SEEK_CUR)?"SEEK_CUR":
804 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
806 ret = SetFilePointer(hand, (long)offset, &hoffset, whence);
807 if (ret != INVALID_SET_FILE_POINTER || !GetLastError())
809 MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
810 /* FIXME: What if we seek _to_ EOF - is EOF set? */
812 return ((__int64)hoffset << 32) | ret;
814 TRACE(":error-last error (%ld)\n",GetLastError());
815 msvcrt_set_errno(GetLastError());
819 /*********************************************************************
822 LONG _lseek(int fd, LONG offset, int whence)
824 return _lseeki64(fd, offset, whence);
827 /*********************************************************************
828 * _locking (MSVCRT.@)
830 * This is untested; the underlying LockFile doesn't work yet.
832 int _locking(int fd, int mode, LONG nbytes)
836 HANDLE hand = msvcrt_fdtoh(fd);
838 TRACE(":fd (%d) handle (%p)\n",fd,hand);
839 if (hand == INVALID_HANDLE_VALUE)
842 if (mode < 0 || mode > 4)
844 *MSVCRT__errno() = MSVCRT_EINVAL;
848 TRACE(":fd (%d) by 0x%08lx mode %s\n",
849 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
850 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
851 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
852 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
853 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
856 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
858 FIXME ("Seek failed\n");
859 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
862 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
865 ret = 1; /* just to satisfy gcc */
868 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
873 else if (mode == MSVCRT__LK_UNLCK)
874 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
876 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
877 /* FIXME - what about error settings? */
881 /*********************************************************************
884 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
886 /* Flush output if needed */
887 if(file->_flag & MSVCRT__IOWRT)
888 msvcrt_flush_buffer(file);
890 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
891 offset -= file->_cnt;
893 /* Discard buffered input */
895 file->_ptr = file->_base;
896 /* Reset direction of i/o */
897 if(file->_flag & MSVCRT__IORW) {
898 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
900 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
903 /*********************************************************************
906 int _chsize(int fd, long size)
912 TRACE("(fd=%d, size=%ld)\n", fd, size);
916 handle = msvcrt_fdtoh(fd);
917 if (handle != INVALID_HANDLE_VALUE)
919 /* save the current file pointer */
920 cur = _lseek(fd, 0, SEEK_CUR);
923 pos = _lseek(fd, size, SEEK_SET);
926 ret = SetEndOfFile(handle);
927 if (!ret) msvcrt_set_errno(GetLastError());
930 /* restore the file pointer */
931 _lseek(fd, cur, SEEK_SET);
939 /*********************************************************************
940 * clearerr (MSVCRT.@)
942 void MSVCRT_clearerr(MSVCRT_FILE* file)
944 TRACE(":file (%p) fd (%d)\n",file,file->_file);
945 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
948 /*********************************************************************
951 void MSVCRT_rewind(MSVCRT_FILE* file)
953 TRACE(":file (%p) fd (%d)\n",file,file->_file);
954 MSVCRT_fseek(file, 0L, SEEK_SET);
955 MSVCRT_clearerr(file);
958 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
960 int plus = strchr(mode, '+') != NULL;
965 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
966 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
969 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
970 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
973 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
974 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
984 *open_flags |= MSVCRT__O_BINARY;
985 *open_flags &= ~MSVCRT__O_TEXT;
988 *open_flags |= MSVCRT__O_TEXT;
989 *open_flags &= ~MSVCRT__O_BINARY;
994 FIXME(":unknown flag %c not supported\n",mode[-1]);
999 /*********************************************************************
1000 * _fdopen (MSVCRT.@)
1002 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
1004 int open_flags, stream_flags;
1007 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1010 if (!(file = msvcrt_alloc_fp()))
1012 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1017 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1023 /*********************************************************************
1024 * _wfdopen (MSVCRT.@)
1026 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1028 unsigned mlen = strlenW(mode);
1029 char *modea = MSVCRT_calloc(mlen + 1, 1);
1030 MSVCRT_FILE* file = NULL;
1031 int open_flags, stream_flags;
1034 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1036 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1038 if (!(file = msvcrt_alloc_fp()))
1040 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1048 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1049 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1056 /*********************************************************************
1057 * _filelength (MSVCRT.@)
1059 LONG _filelength(int fd)
1061 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1064 LONG endPos = _lseek(fd, 0, SEEK_END);
1067 if (endPos != curPos)
1068 _lseek(fd, curPos, SEEK_SET);
1075 /*********************************************************************
1076 * _filelengthi64 (MSVCRT.@)
1078 __int64 _filelengthi64(int fd)
1080 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1083 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1086 if (endPos != curPos)
1087 _lseeki64(fd, curPos, SEEK_SET);
1094 /*********************************************************************
1095 * _fileno (MSVCRT.@)
1097 int MSVCRT__fileno(MSVCRT_FILE* file)
1099 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1103 /*********************************************************************
1104 * _fstati64 (MSVCRT.@)
1106 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1109 BY_HANDLE_FILE_INFORMATION hfi;
1110 HANDLE hand = msvcrt_fdtoh(fd);
1112 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1113 if (hand == INVALID_HANDLE_VALUE)
1118 WARN(":failed-NULL buf\n");
1119 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1123 memset(&hfi, 0, sizeof(hfi));
1124 memset(buf, 0, sizeof(struct MSVCRT__stati64));
1125 if (!GetFileInformationByHandle(hand, &hfi))
1127 WARN(":failed-last error (%ld)\n",GetLastError());
1128 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1131 dw = GetFileType(hand);
1132 buf->st_mode = S_IREAD;
1133 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1134 buf->st_mode |= S_IWRITE;
1135 /* interestingly, Windows never seems to set S_IFDIR */
1136 if (dw == FILE_TYPE_CHAR)
1137 buf->st_mode |= S_IFCHR;
1138 else if (dw == FILE_TYPE_PIPE)
1139 buf->st_mode |= S_IFIFO;
1141 buf->st_mode |= S_IFREG;
1142 TRACE(":dwFileAttributes = 0x%lx, mode set to 0x%x\n",hfi.dwFileAttributes,
1144 buf->st_nlink = hfi.nNumberOfLinks;
1145 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1146 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1148 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1149 buf->st_mtime = buf->st_ctime = dw;
1153 /*********************************************************************
1156 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1158 struct MSVCRT__stati64 bufi64;
1160 ret = MSVCRT__fstati64(fd, &bufi64);
1162 msvcrt_cp_from_stati64(&bufi64, buf);
1166 /*********************************************************************
1167 * _futime (MSVCRT.@)
1169 int _futime(int fd, struct MSVCRT__utimbuf *t)
1171 HANDLE hand = msvcrt_fdtoh(fd);
1176 MSVCRT_time_t currTime;
1177 MSVCRT_time(&currTime);
1178 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1179 memcpy(&wt, &at, sizeof(wt));
1183 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1184 if (t->actime == t->modtime)
1185 memcpy(&wt, &at, sizeof(wt));
1187 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1190 if (!SetFileTime(hand, NULL, &at, &wt))
1192 msvcrt_set_errno(GetLastError());
1198 /*********************************************************************
1199 * _get_osfhandle (MSVCRT.@)
1201 long _get_osfhandle(int fd)
1203 HANDLE hand = msvcrt_fdtoh(fd);
1204 HANDLE newhand = hand;
1205 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1207 if (hand != INVALID_HANDLE_VALUE)
1209 /* FIXME: I'm not convinced that I should be copying the
1210 * handle here - it may be leaked if the app doesn't
1211 * close it (and the API docs don't say that it should)
1212 * Not duplicating it means that it can't be inherited
1213 * and so lcc's wedit doesn't cope when it passes it to
1214 * child processes. I've an idea that it should either
1215 * be copied by CreateProcess, or marked as inheritable
1216 * when initialised, or maybe both? JG 21-9-00.
1218 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
1219 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
1221 return (long)newhand;
1224 /*********************************************************************
1225 * _isatty (MSVCRT.@)
1229 HANDLE hand = msvcrt_fdtoh(fd);
1231 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1232 if (hand == INVALID_HANDLE_VALUE)
1235 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1238 /*********************************************************************
1239 * _mktemp (MSVCRT.@)
1241 char *_mktemp(char *pattern)
1244 char *retVal = pattern;
1249 numX = (*pattern++ == 'X')? numX + 1 : 0;
1253 id = GetCurrentProcessId();
1257 int tempNum = id / 10;
1258 *pattern-- = id - (tempNum * 10) + '0';
1264 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1265 GetLastError() == ERROR_FILE_NOT_FOUND)
1267 *pattern = letter++;
1268 } while(letter != '|');
1272 /*********************************************************************
1273 * _wmktemp (MSVCRT.@)
1275 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
1278 MSVCRT_wchar_t *retVal = pattern;
1280 MSVCRT_wchar_t letter = 'a';
1283 numX = (*pattern++ == 'X')? numX + 1 : 0;
1287 id = GetCurrentProcessId();
1291 int tempNum = id / 10;
1292 *pattern-- = id - (tempNum * 10) + '0';
1298 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1299 GetLastError() == ERROR_FILE_NOT_FOUND)
1301 *pattern = letter++;
1302 } while(letter != '|');
1306 static unsigned split_oflags(unsigned oflags)
1309 unsigned unsupp; /* until we support everything */
1311 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1312 if (oflags & MSVCRT__O_BINARY) ;
1313 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1314 else if (*__p__fmode() & MSVCRT__O_BINARY) ;
1315 else wxflags |= WX_TEXT; /* default to TEXT*/
1316 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1318 if ((unsupp = oflags & ~(
1319 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1320 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1321 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1322 MSVCRT__O_NOINHERIT|
1323 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1325 ERR(":unsupported oflags 0x%04x\n",unsupp);
1330 /*********************************************************************
1333 int _pipe(int *pfds, unsigned int psize, int textmode)
1336 SECURITY_ATTRIBUTES sa;
1337 HANDLE readHandle, writeHandle;
1341 *MSVCRT__errno() = MSVCRT_EINVAL;
1345 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1346 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1347 sa.lpSecurityDescriptor = NULL;
1348 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1350 unsigned int wxflags = split_oflags(textmode);
1354 fd = msvcrt_alloc_fd(readHandle, wxflags);
1358 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1367 CloseHandle(writeHandle);
1368 *MSVCRT__errno() = MSVCRT_EMFILE;
1373 CloseHandle(readHandle);
1374 CloseHandle(writeHandle);
1375 *MSVCRT__errno() = MSVCRT_EMFILE;
1380 msvcrt_set_errno(GetLastError());
1385 /*********************************************************************
1388 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1392 DWORD access = 0, creation = 0, attrib;
1396 SECURITY_ATTRIBUTES sa;
1399 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1400 path, oflags, shflags);
1402 wxflag = split_oflags(oflags);
1403 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1405 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1406 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1407 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1410 if (oflags & MSVCRT__O_CREAT)
1412 va_start(ap, shflags);
1413 pmode = va_arg(ap, int);
1416 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1417 FIXME(": pmode 0x%04x ignored\n", pmode);
1419 WARN(": pmode 0x%04x ignored\n", pmode);
1421 if (oflags & MSVCRT__O_EXCL)
1422 creation = CREATE_NEW;
1423 else if (oflags & MSVCRT__O_TRUNC)
1424 creation = CREATE_ALWAYS;
1426 creation = OPEN_ALWAYS;
1428 else /* no MSVCRT__O_CREAT */
1430 if (oflags & MSVCRT__O_TRUNC)
1431 creation = TRUNCATE_EXISTING;
1433 creation = OPEN_EXISTING;
1438 case MSVCRT__SH_DENYRW:
1441 case MSVCRT__SH_DENYWR:
1442 sharing = FILE_SHARE_READ;
1444 case MSVCRT__SH_DENYRD:
1445 sharing = FILE_SHARE_WRITE;
1447 case MSVCRT__SH_DENYNO:
1448 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1451 ERR( "Unhandled shflags 0x%x\n", shflags );
1454 attrib = FILE_ATTRIBUTE_NORMAL;
1456 if (oflags & MSVCRT__O_TEMPORARY)
1458 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1460 sharing |= FILE_SHARE_DELETE;
1463 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1464 sa.lpSecurityDescriptor = NULL;
1465 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1467 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1469 if (hand == INVALID_HANDLE_VALUE) {
1470 WARN(":failed-last error (%ld)\n",GetLastError());
1471 msvcrt_set_errno(GetLastError());
1475 fd = msvcrt_alloc_fd(hand, wxflag);
1477 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1481 if (oflags & MSVCRT__O_APPEND)
1482 _lseek(fd, 0, FILE_END);
1488 /*********************************************************************
1489 * _wsopen (MSVCRT.@)
1491 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1493 const unsigned int len = strlenW(path);
1494 char *patha = MSVCRT_calloc(len + 1,1);
1498 va_start(ap, shflags);
1499 pmode = va_arg(ap, int);
1502 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1504 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1509 msvcrt_set_errno(GetLastError());
1513 /*********************************************************************
1516 int _open( const char *path, int flags, ... )
1520 if (flags & MSVCRT__O_CREAT)
1523 va_start(ap, flags);
1524 pmode = va_arg(ap, int);
1526 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1529 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1532 /*********************************************************************
1535 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1537 const unsigned int len = strlenW(path);
1538 char *patha = MSVCRT_calloc(len + 1,1);
1542 va_start(ap, flags);
1543 pmode = va_arg(ap, int);
1546 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1548 int retval = _open(patha,flags,pmode);
1553 msvcrt_set_errno(GetLastError());
1557 /*********************************************************************
1560 int _creat(const char *path, int flags)
1562 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1563 return _open(path, usedFlags);
1566 /*********************************************************************
1567 * _wcreat (MSVCRT.@)
1569 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1571 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1572 return _wopen(path, usedFlags);
1575 /*********************************************************************
1576 * _open_osfhandle (MSVCRT.@)
1578 int _open_osfhandle(long handle, int oflags)
1582 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1583 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1584 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1585 * text - it never sets MSVCRT__O_BINARY.
1587 /* FIXME: handle more flags */
1588 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)) && (*__p__fmode() & MSVCRT__O_BINARY))
1589 oflags |= MSVCRT__O_BINARY;
1591 oflags |= MSVCRT__O_TEXT;
1593 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1594 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1598 /*********************************************************************
1603 int num_removed = 0, i;
1606 for (i = 3; i < MSVCRT_stream_idx; i++)
1607 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1609 MSVCRT_fclose(MSVCRT_fstreams[i]);
1615 TRACE(":removed (%d) temp files\n",num_removed);
1619 /*********************************************************************
1620 * (internal) remove_cr
1622 * Remove all \r inplace.
1623 * return the number of \r removed
1625 static unsigned int remove_cr(char *buf, unsigned int count)
1629 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1630 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1634 /*********************************************************************
1637 int _read(int fd, void *buf, unsigned int count)
1639 DWORD num_read, all_read = 0;
1640 char *bufstart = buf;
1641 HANDLE hand = msvcrt_fdtoh(fd);
1643 /* Don't trace small reads, it gets *very* annoying */
1645 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1646 if (hand == INVALID_HANDLE_VALUE)
1649 /* Reading single bytes in O_TEXT mode makes things slow
1650 * So read big chunks, then remove the \r in memory and try reading
1651 * the rest until the request is satisfied or EOF is met
1653 while (all_read < count)
1655 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1657 if (num_read != (count - all_read))
1660 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1661 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1662 num_read -= remove_cr(bufstart+all_read,num_read);
1663 all_read += num_read;
1665 TRACE("%s\n",debugstr_an(buf,all_read));
1668 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1670 num_read -= remove_cr(bufstart+all_read,num_read);
1672 all_read += num_read;
1676 TRACE(":failed-last error (%ld)\n",GetLastError());
1682 TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
1686 /*********************************************************************
1689 int MSVCRT__getw(MSVCRT_FILE* file)
1692 switch (_read(file->_file, &i, sizeof(int)))
1695 case 0: file->_flag |= MSVCRT__IOEOF; break;
1696 default: file->_flag |= MSVCRT__IOERR; break;
1701 /*********************************************************************
1702 * _setmode (MSVCRT.@)
1704 int _setmode(int fd,int mode)
1706 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1707 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1708 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1709 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1710 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1712 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1716 /*********************************************************************
1717 * _stati64 (MSVCRT.@)
1719 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1722 WIN32_FILE_ATTRIBUTE_DATA hfi;
1723 unsigned short mode = ALL_S_IREAD;
1726 TRACE(":file (%s) buf(%p)\n",path,buf);
1728 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1730 TRACE("failed (%ld)\n",GetLastError());
1731 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1735 memset(buf,0,sizeof(struct MSVCRT__stati64));
1737 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1738 Bon 011120: This FIXME seems incorrect
1739 Also a letter as first char isn't enough to be classified
1742 if (isalpha(*path)&& (*(path+1)==':'))
1743 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1745 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1747 plen = strlen(path);
1749 /* Dir, or regular file? */
1750 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1751 (path[plen-1] == '\\'))
1752 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1755 mode |= MSVCRT__S_IFREG;
1757 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1759 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1760 (tolower(path[plen-3]) << 16);
1761 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1762 mode |= ALL_S_IEXEC;
1766 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1767 mode |= ALL_S_IWRITE;
1769 buf->st_mode = mode;
1771 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1772 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1774 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1775 buf->st_mtime = buf->st_ctime = dw;
1776 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1777 (long)(buf->st_size >> 32),(long)buf->st_size,
1778 buf->st_atime,buf->st_mtime, buf->st_ctime);
1782 /*********************************************************************
1785 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1787 struct MSVCRT__stati64 bufi64;
1789 ret = MSVCRT__stati64( path, &bufi64);
1791 msvcrt_cp_from_stati64(&bufi64, buf);
1795 /*********************************************************************
1796 * _wstati64 (MSVCRT.@)
1798 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1801 WIN32_FILE_ATTRIBUTE_DATA hfi;
1802 unsigned short mode = ALL_S_IREAD;
1805 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1807 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1809 TRACE("failed (%ld)\n",GetLastError());
1810 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1814 memset(buf,0,sizeof(struct MSVCRT__stat));
1816 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1817 if (MSVCRT_iswalpha(*path))
1818 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1820 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1822 plen = strlenW(path);
1824 /* Dir, or regular file? */
1825 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1826 (path[plen-1] == '\\'))
1827 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1830 mode |= MSVCRT__S_IFREG;
1832 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1834 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1835 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1836 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1837 mode |= ALL_S_IEXEC;
1841 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1842 mode |= ALL_S_IWRITE;
1844 buf->st_mode = mode;
1846 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1847 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1849 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1850 buf->st_mtime = buf->st_ctime = dw;
1851 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1852 (long)(buf->st_size >> 32),(long)buf->st_size,
1853 buf->st_atime,buf->st_mtime, buf->st_ctime);
1857 /*********************************************************************
1860 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1863 struct MSVCRT__stati64 bufi64;
1865 ret = MSVCRT__wstati64( path, &bufi64 );
1866 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1870 /*********************************************************************
1875 return _lseek(fd, 0, SEEK_CUR);
1878 /*********************************************************************
1879 * _telli64 (MSVCRT.@)
1881 __int64 _telli64(int fd)
1883 return _lseeki64(fd, 0, SEEK_CUR);
1886 /*********************************************************************
1887 * _tempnam (MSVCRT.@)
1889 char *_tempnam(const char *dir, const char *prefix)
1891 char tmpbuf[MAX_PATH];
1893 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1894 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1896 TRACE("got name (%s)\n",tmpbuf);
1897 DeleteFileA(tmpbuf);
1898 return _strdup(tmpbuf);
1900 TRACE("failed (%ld)\n",GetLastError());
1904 /*********************************************************************
1905 * _wtempnam (MSVCRT.@)
1907 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1909 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1911 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1912 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1914 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1915 DeleteFileW(tmpbuf);
1916 return _wcsdup(tmpbuf);
1918 TRACE("failed (%ld)\n",GetLastError());
1922 /*********************************************************************
1925 int _umask(int umask)
1927 int old_umask = MSVCRT_umask;
1928 TRACE("(%d)\n",umask);
1929 MSVCRT_umask = umask;
1933 /*********************************************************************
1936 int _utime(const char* path, struct MSVCRT__utimbuf *t)
1938 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1942 int retVal = _futime(fd, t);
1949 /*********************************************************************
1950 * _wutime (MSVCRT.@)
1952 int _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1954 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1958 int retVal = _futime(fd, t);
1965 /*********************************************************************
1968 int _write(int fd, const void* buf, unsigned int count)
1971 HANDLE hand = msvcrt_fdtoh(fd);
1973 /* Don't trace small writes, it gets *very* annoying */
1976 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1978 if (hand == INVALID_HANDLE_VALUE)
1980 *MSVCRT__errno() = MSVCRT_EBADF;
1984 /* If appending, go to EOF */
1985 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
1986 _lseek(fd, 0, FILE_END);
1988 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
1990 if (WriteFile(hand, buf, count, &num_written, NULL)
1991 && (num_written == count))
1993 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd,
1994 hand, GetLastError());
1995 *MSVCRT__errno() = MSVCRT_ENOSPC;
1999 unsigned int i, j, nr_lf;
2000 char *s =(char*)buf, *buf_start=(char*)buf, *p;
2001 /* find number of \n ( without preceding \r ) */
2002 for ( nr_lf=0,i = 0; i <count; i++)
2007 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2012 if ((p = MSVCRT_malloc(count + nr_lf)))
2014 for(s=(char*)buf, i=0, j=0; i<count; i++)
2019 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2026 FIXME("Malloc failed\n");
2034 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2036 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld), num_written %ld\n",
2037 fd, hand, GetLastError(), num_written);
2038 *MSVCRT__errno() = MSVCRT_ENOSPC;
2041 return s - buf_start;
2053 /*********************************************************************
2056 int MSVCRT__putw(int val, MSVCRT_FILE* file)
2059 len = _write(file->_file, &val, sizeof(val));
2060 if (len == sizeof(val)) return val;
2061 file->_flag |= MSVCRT__IOERR;
2065 /*********************************************************************
2068 int MSVCRT_fclose(MSVCRT_FILE* file)
2073 if (file->_tmpfname)
2075 MSVCRT_free(file->_tmpfname);
2076 file->_tmpfname = NULL;
2078 /* flush stdio buffers */
2079 if(file->_flag & MSVCRT__IOWRT)
2080 MSVCRT_fflush(file);
2081 if(file->_flag & MSVCRT__IOMYBUF)
2082 MSVCRT_free(file->_base);
2084 r=_close(file->_file);
2088 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2091 /*********************************************************************
2094 int MSVCRT_feof(MSVCRT_FILE* file)
2096 return file->_flag & MSVCRT__IOEOF;
2099 /*********************************************************************
2102 int MSVCRT_ferror(MSVCRT_FILE* file)
2104 return file->_flag & MSVCRT__IOERR;
2107 /*********************************************************************
2108 * _filbuf (MSVCRT.@)
2110 int MSVCRT__filbuf(MSVCRT_FILE* file)
2112 /* Allocate buffer if needed */
2113 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2114 msvcrt_alloc_buffer(file);
2116 if(!(file->_flag & MSVCRT__IOREAD)) {
2117 if(file->_flag & MSVCRT__IORW) {
2118 file->_flag |= MSVCRT__IOREAD;
2123 if(file->_flag & MSVCRT__IONBF) {
2126 if ((r = _read(file->_file,&c,1)) != 1) {
2127 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2132 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2134 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2139 file->_ptr = file->_base+1;
2140 return *(unsigned char *)file->_base;
2144 /*********************************************************************
2147 int MSVCRT_fgetc(MSVCRT_FILE* file)
2151 return *(unsigned char *)file->_ptr++;
2153 return MSVCRT__filbuf(file);
2157 /*********************************************************************
2158 * _fgetchar (MSVCRT.@)
2162 return MSVCRT_fgetc(MSVCRT_stdin);
2165 /*********************************************************************
2168 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2170 int cc = MSVCRT_EOF;
2171 char * buf_start = s;
2173 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2174 file,file->_file,s,size);
2176 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2181 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2183 TRACE(":nothing read\n");
2186 if ((cc != MSVCRT_EOF) && (size > 1))
2189 TRACE(":got '%s'\n", debugstr_a(buf_start));
2193 /*********************************************************************
2196 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2197 * the CR from CR/LF combinations
2199 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
2203 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2207 if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
2209 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2214 c = MSVCRT_fgetc(file);
2215 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2217 FIXME("Treat Multibyte characters\n");
2219 if (c == MSVCRT_EOF)
2222 return (MSVCRT_wint_t)c;
2225 /*********************************************************************
2228 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
2230 return MSVCRT_fgetwc(file);
2233 /*********************************************************************
2234 * _fgetwchar (MSVCRT.@)
2236 MSVCRT_wint_t _fgetwchar(void)
2238 return MSVCRT_fgetwc(MSVCRT_stdin);
2241 /*********************************************************************
2242 * getwchar (MSVCRT.@)
2244 MSVCRT_wint_t MSVCRT_getwchar(void)
2246 return _fgetwchar();
2249 /*********************************************************************
2252 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2254 int cc = MSVCRT_WEOF;
2255 MSVCRT_wchar_t * buf_start = s;
2257 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2258 file,file->_file,s,size);
2260 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2265 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2267 TRACE(":nothing read\n");
2270 if ((cc != MSVCRT_WEOF) && (size > 1))
2273 TRACE(":got %s\n", debugstr_w(buf_start));
2277 /*********************************************************************
2280 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2282 MSVCRT_size_t wrcnt=size * nmemb;
2287 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2288 memcpy(file->_ptr, ptr, pcnt);
2293 ptr = (const char*)ptr + pcnt;
2294 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2295 if(file->_flag & MSVCRT__IORW) {
2296 file->_flag |= MSVCRT__IOWRT;
2302 int res=msvcrt_flush_buffer(file);
2304 int pwritten = _write(file->_file, ptr, wrcnt);
2307 file->_flag |= MSVCRT__IOERR;
2310 written += pwritten;
2313 return written / size;
2316 /*********************************************************************
2319 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2321 MSVCRT_wchar_t mwc=wc;
2322 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2327 /*********************************************************************
2328 * _fputwchar (MSVCRT.@)
2330 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
2332 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2335 /*********************************************************************
2338 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
2341 int open_flags, stream_flags, fd;
2343 TRACE("(%s,%s)\n",path,mode);
2345 /* map mode string to open() flags. "man fopen" for possibilities. */
2346 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2350 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2353 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2355 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2362 TRACE(":got (%p)\n",file);
2369 /*********************************************************************
2370 * _wfopen (MSVCRT.@)
2372 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2374 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2375 char *patha = MSVCRT_calloc(plen + 1, 1);
2376 char *modea = MSVCRT_calloc(mlen + 1, 1);
2378 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2380 if (patha && modea &&
2381 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2382 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2384 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2390 msvcrt_set_errno(GetLastError());
2394 /*********************************************************************
2395 * _fsopen (MSVCRT.@)
2397 MSVCRT_FILE* MSVCRT__fsopen(const char *path, const char *mode, int share)
2399 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2400 return MSVCRT_fopen(path,mode);
2403 /*********************************************************************
2404 * _wfsopen (MSVCRT.@)
2406 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2408 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2409 debugstr_w(path),debugstr_w(mode),share);
2410 return MSVCRT__wfopen(path,mode);
2413 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2414 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2416 /*********************************************************************
2419 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2426 int res = msvcrt_flush_buffer(file);
2427 return res ? res : c;
2432 return MSVCRT__flsbuf(c, file);
2436 /*********************************************************************
2437 * _flsbuf (MSVCRT.@)
2439 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2441 /* Flush output buffer */
2442 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2443 msvcrt_alloc_buffer(file);
2445 if(!(file->_flag & MSVCRT__IOWRT)) {
2446 if(file->_flag & MSVCRT__IORW) {
2447 file->_flag |= MSVCRT__IOWRT;
2453 int res=msvcrt_flush_buffer(file);
2454 return res?res : MSVCRT_fputc(c, file);
2458 len = _write(file->_file, &cc, 1);
2459 if (len == 1) return c;
2460 file->_flag |= MSVCRT__IOERR;
2465 /*********************************************************************
2466 * _fputchar (MSVCRT.@)
2468 int _fputchar(int c)
2470 return MSVCRT_fputc(c, MSVCRT_stdout);
2473 /*********************************************************************
2476 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2477 { MSVCRT_size_t rcnt=size * nmemb;
2478 MSVCRT_size_t read=0;
2481 /* first buffered data */
2483 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2484 memcpy(ptr, file->_ptr, pcnt);
2489 ptr = (char*)ptr + pcnt;
2490 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2491 if(file->_flag & MSVCRT__IORW) {
2492 file->_flag |= MSVCRT__IOREAD;
2498 pread = _read(file->_file,ptr, rcnt);
2499 /* expose feof condition in the flags
2500 * MFC tests file->_flag for feof, and doesn't not call feof())
2503 file->_flag |= MSVCRT__IOEOF;
2504 else if (pread == -1)
2506 file->_flag |= MSVCRT__IOERR;
2514 /*********************************************************************
2515 * freopen (MSVCRT.@)
2518 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2520 int open_flags, stream_flags, fd;
2522 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2525 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2529 MSVCRT_fclose(file);
2530 /* map mode string to open() flags. "man fopen" for possibilities. */
2531 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2535 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2538 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2541 WARN(":failed-last error (%ld)\n",GetLastError());
2542 msvcrt_set_errno(GetLastError());
2551 /*********************************************************************
2552 * fsetpos (MSVCRT.@)
2554 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2556 /* Note that all this has been lifted 'as is' from fseek */
2557 if(file->_flag & MSVCRT__IOWRT)
2558 msvcrt_flush_buffer(file);
2560 /* Discard buffered input */
2562 file->_ptr = file->_base;
2564 /* Reset direction of i/o */
2565 if(file->_flag & MSVCRT__IORW) {
2566 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2569 return _lseeki64(file->_file,*pos,SEEK_SET);
2572 /*********************************************************************
2575 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2580 if( file->_flag & MSVCRT__IOWRT ) {
2581 off = file->_ptr - file->_base;
2586 pos = _tell(file->_file);
2587 if(pos == -1) return pos;
2591 /*********************************************************************
2592 * fgetpos (MSVCRT.@)
2594 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2596 /* This code has been lifted form the MSVCRT_ftell function */
2599 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2601 if (*pos == -1) return -1;
2604 if( file->_flag & MSVCRT__IOWRT ) {
2605 off = file->_ptr - file->_base;
2615 /*********************************************************************
2618 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2620 size_t i, len = strlen(s);
2621 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2622 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2623 for (i=0; i<len; i++)
2624 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2629 /*********************************************************************
2632 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2634 size_t i, len = strlenW(s);
2635 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2636 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2637 for (i=0; i<len; i++)
2639 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2641 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2647 /*********************************************************************
2648 * getchar (MSVCRT.@)
2650 int MSVCRT_getchar(void)
2652 return MSVCRT_fgetc(MSVCRT_stdin);
2655 /*********************************************************************
2658 int MSVCRT_getc(MSVCRT_FILE* file)
2660 return MSVCRT_fgetc(file);
2663 /*********************************************************************
2666 char *MSVCRT_gets(char *buf)
2669 char * buf_start = buf;
2671 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2672 cc = MSVCRT_fgetc(MSVCRT_stdin))
2673 if(cc != '\r') *buf++ = (char)cc;
2677 TRACE("got '%s'\n", buf_start);
2681 /*********************************************************************
2684 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2687 MSVCRT_wchar_t* ws = buf;
2689 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2690 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2693 *buf++ = (MSVCRT_wchar_t)cc;
2697 TRACE("got '%s'\n", debugstr_w(ws));
2701 /*********************************************************************
2704 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2706 return MSVCRT_fputc(c, file);
2709 /*********************************************************************
2710 * putchar (MSVCRT.@)
2712 int MSVCRT_putchar(int c)
2714 return MSVCRT_fputc(c, MSVCRT_stdout);
2717 /*********************************************************************
2720 int MSVCRT_puts(const char *s)
2722 size_t len = strlen(s);
2723 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2724 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2727 /*********************************************************************
2730 int _putws(const MSVCRT_wchar_t *s)
2732 static const MSVCRT_wchar_t nl = '\n';
2733 size_t len = strlenW(s);
2734 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2735 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2738 /*********************************************************************
2741 int MSVCRT_remove(const char *path)
2743 TRACE("(%s)\n",path);
2744 if (DeleteFileA(path))
2746 TRACE(":failed (%ld)\n",GetLastError());
2747 msvcrt_set_errno(GetLastError());
2751 /*********************************************************************
2752 * _wremove (MSVCRT.@)
2754 int _wremove(const MSVCRT_wchar_t *path)
2756 TRACE("(%s)\n",debugstr_w(path));
2757 if (DeleteFileW(path))
2759 TRACE(":failed (%ld)\n",GetLastError());
2760 msvcrt_set_errno(GetLastError());
2764 /*********************************************************************
2767 int MSVCRT_rename(const char *oldpath,const char *newpath)
2769 TRACE(":from %s to %s\n",oldpath,newpath);
2770 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2772 TRACE(":failed (%ld)\n",GetLastError());
2773 msvcrt_set_errno(GetLastError());
2777 /*********************************************************************
2778 * _wrename (MSVCRT.@)
2780 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2782 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2783 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2785 TRACE(":failed (%ld)\n",GetLastError());
2786 msvcrt_set_errno(GetLastError());
2790 /*********************************************************************
2791 * setvbuf (MSVCRT.@)
2793 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2795 /* TODO: Check if file busy */
2797 MSVCRT_free(file->_base);
2801 if(mode == MSVCRT__IOFBF) {
2802 file->_flag &= ~MSVCRT__IONBF;
2803 file->_base = file->_ptr = buf;
2805 file->_bufsiz = size;
2808 file->_flag |= MSVCRT__IONBF;
2813 /*********************************************************************
2816 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2818 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2821 /*********************************************************************
2824 char *MSVCRT_tmpnam(char *s)
2832 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2833 p = s + sprintf(s, "\\s%s.", tmpstr);
2834 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2836 msvcrt_int_to_base32(unique++, tmpstr);
2838 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2839 GetLastError() == ERROR_FILE_NOT_FOUND)
2845 /*********************************************************************
2846 * tmpfile (MSVCRT.@)
2848 MSVCRT_FILE* MSVCRT_tmpfile(void)
2850 char *filename = MSVCRT_tmpnam(NULL);
2852 MSVCRT_FILE* file = NULL;
2855 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2856 if (fd != -1 && (file = msvcrt_alloc_fp()))
2858 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2863 else file->_tmpfname = _strdup(filename);
2869 /*********************************************************************
2870 * vfprintf (MSVCRT.@)
2872 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2874 char buf[2048], *mem = buf;
2875 int written, resize = sizeof(buf), retval;
2876 /* There are two conventions for vsnprintf failing:
2877 * Return -1 if we truncated, or
2878 * Return the number of bytes that would have been written
2879 * The code below handles both cases
2881 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
2884 resize = (written == -1 ? resize * 2 : written + 1);
2887 if (!(mem = (char *)MSVCRT_malloc(resize)))
2890 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2896 /*********************************************************************
2897 * vfwprintf (MSVCRT.@)
2899 * Is final char included in written (then resize is too big) or not
2900 * (then we must test for equality too)?
2902 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2904 MSVCRT_wchar_t buf[2048], *mem = buf;
2905 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2906 /* See vfprintf comments */
2907 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2910 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2913 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2916 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2922 /*********************************************************************
2923 * vprintf (MSVCRT.@)
2925 int MSVCRT_vprintf(const char *format, va_list valist)
2927 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2930 /*********************************************************************
2931 * vwprintf (MSVCRT.@)
2933 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2935 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2938 /*********************************************************************
2939 * fprintf (MSVCRT.@)
2941 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2945 va_start(valist, format);
2946 res = MSVCRT_vfprintf(file, format, valist);
2951 /*********************************************************************
2952 * fwprintf (MSVCRT.@)
2954 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2958 va_start(valist, format);
2959 res = MSVCRT_vfwprintf(file, format, valist);
2964 /*********************************************************************
2967 int MSVCRT_printf(const char *format, ...)
2971 va_start(valist, format);
2972 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2977 /*********************************************************************
2980 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2982 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2983 msvcrt_alloc_buffer(file);
2986 if(file->_ptr>file->_base) {
2995 /*********************************************************************
2996 * ungetwc (MSVCRT.@)
2998 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3000 MSVCRT_wchar_t mwc = wc;
3001 char * pp = (char *)&mwc;
3003 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3004 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3010 /*********************************************************************
3011 * wprintf (MSVCRT.@)
3013 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3017 va_start(valist, format);
3018 res = MSVCRT_vwprintf(format, valist);
3023 /*********************************************************************
3024 * __pioinfo (MSVCRT.@)
3025 * FIXME: see MSVCRT_MAX_FILES define.
3027 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3028 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3029 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3030 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3031 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3032 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3033 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3034 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3035 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3036 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3037 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3038 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3041 /*********************************************************************
3042 * __badioinfo (MSVCRT.@)
3044 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };