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 _chsize(int fd, long size)
532 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
536 /*********************************************************************
539 int _unlink(const char *path)
541 TRACE("(%s)\n",path);
542 if(DeleteFileA(path))
544 TRACE("failed (%ld)\n",GetLastError());
545 msvcrt_set_errno(GetLastError());
549 /*********************************************************************
550 * _wunlink (MSVCRT.@)
552 int _wunlink(const MSVCRT_wchar_t *path)
554 TRACE("(%s)\n",debugstr_w(path));
555 if(DeleteFileW(path))
557 TRACE("failed (%ld)\n",GetLastError());
558 msvcrt_set_errno(GetLastError());
562 /* _flushall calls MSVCRT_fflush which calls _flushall */
563 int MSVCRT_fflush(MSVCRT_FILE* file);
565 /*********************************************************************
566 * _flushall (MSVCRT.@)
570 int i, num_flushed = 0;
573 for (i = 3; i < MSVCRT_stream_idx; i++)
574 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
577 /* FIXME: flush, do not commit */
578 if (_commit(i) == -1)
579 if (MSVCRT_fstreams[i])
580 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
582 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
583 MSVCRT_fflush(MSVCRT_fstreams[i]);
589 TRACE(":flushed (%d) handles\n",num_flushed);
593 /*********************************************************************
596 int MSVCRT_fflush(MSVCRT_FILE* file)
602 int res=msvcrt_flush_buffer(file);
607 /*********************************************************************
616 hand = msvcrt_fdtoh(fd);
617 TRACE(":fd (%d) handle (%p)\n",fd,hand);
618 if (hand == INVALID_HANDLE_VALUE)
620 else if (!CloseHandle(hand))
622 WARN(":failed-last error (%ld)\n",GetLastError());
623 msvcrt_set_errno(GetLastError());
636 /*********************************************************************
641 HANDLE hand = msvcrt_fdtoh(fd);
643 TRACE(":fd (%d) handle (%p)\n",fd,hand);
644 if (hand == INVALID_HANDLE_VALUE)
647 if (!FlushFileBuffers(hand))
649 if (GetLastError() == ERROR_INVALID_HANDLE)
651 /* FlushFileBuffers fails for console handles
652 * so we ignore this error.
656 TRACE(":failed-last error (%ld)\n",GetLastError());
657 msvcrt_set_errno(GetLastError());
664 /*********************************************************************
667 * MSDN isn't clear on this point, but the remarks for _pipe,
668 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
669 * indicate file descriptors duplicated with _dup and _dup2 are always
672 int _dup2(int od, int nd)
676 TRACE("(od=%d, nd=%d)\n", od, nd);
678 if (nd < MSVCRT_MAX_FILES && msvcrt_is_valid_fd(od))
682 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
683 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
685 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
687 if (msvcrt_is_valid_fd(nd))
689 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
693 *MSVCRT__errno() = MSVCRT_EMFILE;
697 /* _dup2 returns 0, not nd, on success */
704 msvcrt_set_errno(GetLastError());
709 *MSVCRT__errno() = MSVCRT_EBADF;
716 /*********************************************************************
725 if (_dup2(od, fd) == 0)
733 /*********************************************************************
738 DWORD curpos,endpos,hcurpos,hendpos;
739 HANDLE hand = msvcrt_fdtoh(fd);
741 TRACE(":fd (%d) handle (%p)\n",fd,hand);
743 if (hand == INVALID_HANDLE_VALUE)
746 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
748 /* Otherwise we do it the hard way */
749 hcurpos = hendpos = 0;
750 curpos = SetFilePointer(hand, 0, &hcurpos, SEEK_CUR);
751 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
753 if (curpos == endpos && hcurpos == hendpos)
755 /* FIXME: shouldn't WX_ATEOF be set here? */
759 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
763 /*********************************************************************
764 * _fcloseall (MSVCRT.@)
766 int MSVCRT__fcloseall(void)
768 int num_closed = 0, i;
771 for (i = 3; i < MSVCRT_stream_idx; i++)
772 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
773 MSVCRT_fclose(MSVCRT_fstreams[i]))
777 TRACE(":closed (%d) handles\n",num_closed);
781 /* free everything on process exit */
782 void msvcrt_free_io(void)
788 DeleteCriticalSection(&MSVCRT_file_cs);
791 /*********************************************************************
792 * _lseeki64 (MSVCRT.@)
794 __int64 _lseeki64(int fd, __int64 offset, int whence)
796 DWORD ret, hoffset = (DWORD) (offset >> 32);
797 HANDLE hand = msvcrt_fdtoh(fd);
799 TRACE(":fd (%d) handle (%p)\n",fd,hand);
800 if (hand == INVALID_HANDLE_VALUE)
803 if (whence < 0 || whence > 2)
805 *MSVCRT__errno() = MSVCRT_EINVAL;
809 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
810 fd,hoffset,(long)offset,
811 (whence==SEEK_SET)?"SEEK_SET":
812 (whence==SEEK_CUR)?"SEEK_CUR":
813 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
815 ret = SetFilePointer(hand, (long)offset, &hoffset, whence);
816 if (ret != INVALID_SET_FILE_POINTER || !GetLastError())
818 MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
819 /* FIXME: What if we seek _to_ EOF - is EOF set? */
821 return ((__int64)hoffset << 32) | ret;
823 TRACE(":error-last error (%ld)\n",GetLastError());
824 msvcrt_set_errno(GetLastError());
828 /*********************************************************************
831 LONG _lseek(int fd, LONG offset, int whence)
833 return _lseeki64(fd, offset, whence);
836 /*********************************************************************
837 * _locking (MSVCRT.@)
839 * This is untested; the underlying LockFile doesn't work yet.
841 int _locking(int fd, int mode, LONG nbytes)
845 HANDLE hand = msvcrt_fdtoh(fd);
847 TRACE(":fd (%d) handle (%p)\n",fd,hand);
848 if (hand == INVALID_HANDLE_VALUE)
851 if (mode < 0 || mode > 4)
853 *MSVCRT__errno() = MSVCRT_EINVAL;
857 TRACE(":fd (%d) by 0x%08lx mode %s\n",
858 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
859 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
860 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
861 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
862 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
865 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
867 FIXME ("Seek failed\n");
868 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
871 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
874 ret = 1; /* just to satisfy gcc */
877 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
882 else if (mode == MSVCRT__LK_UNLCK)
883 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
885 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
886 /* FIXME - what about error settings? */
890 /*********************************************************************
893 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
895 /* Flush output if needed */
896 if(file->_flag & MSVCRT__IOWRT)
897 msvcrt_flush_buffer(file);
899 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
900 offset -= file->_cnt;
902 /* Discard buffered input */
904 file->_ptr = file->_base;
905 /* Reset direction of i/o */
906 if(file->_flag & MSVCRT__IORW) {
907 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
909 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
912 /*********************************************************************
913 * clearerr (MSVCRT.@)
915 void MSVCRT_clearerr(MSVCRT_FILE* file)
917 TRACE(":file (%p) fd (%d)\n",file,file->_file);
918 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
921 /*********************************************************************
924 void MSVCRT_rewind(MSVCRT_FILE* file)
926 TRACE(":file (%p) fd (%d)\n",file,file->_file);
927 MSVCRT_fseek(file, 0L, SEEK_SET);
928 MSVCRT_clearerr(file);
931 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
933 int plus = strchr(mode, '+') != NULL;
938 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
939 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
942 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
943 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
946 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
947 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
957 *open_flags |= MSVCRT__O_BINARY;
958 *open_flags &= ~MSVCRT__O_TEXT;
961 *open_flags |= MSVCRT__O_TEXT;
962 *open_flags &= ~MSVCRT__O_BINARY;
967 FIXME(":unknown flag %c not supported\n",mode[-1]);
972 /*********************************************************************
975 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
977 int open_flags, stream_flags;
980 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
983 if (!(file = msvcrt_alloc_fp()))
985 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
990 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
996 /*********************************************************************
997 * _wfdopen (MSVCRT.@)
999 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1001 unsigned mlen = strlenW(mode);
1002 char *modea = MSVCRT_calloc(mlen + 1, 1);
1003 MSVCRT_FILE* file = NULL;
1004 int open_flags, stream_flags;
1007 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1009 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1011 if (!(file = msvcrt_alloc_fp()))
1013 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1021 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1022 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1029 /*********************************************************************
1030 * _filelength (MSVCRT.@)
1032 LONG _filelength(int fd)
1034 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1037 LONG endPos = _lseek(fd, 0, SEEK_END);
1040 if (endPos != curPos)
1041 _lseek(fd, curPos, SEEK_SET);
1048 /*********************************************************************
1049 * _filelengthi64 (MSVCRT.@)
1051 __int64 _filelengthi64(int fd)
1053 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1056 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1059 if (endPos != curPos)
1060 _lseeki64(fd, curPos, SEEK_SET);
1067 /*********************************************************************
1068 * _fileno (MSVCRT.@)
1070 int MSVCRT__fileno(MSVCRT_FILE* file)
1072 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1076 /*********************************************************************
1077 * _fstati64 (MSVCRT.@)
1079 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1082 BY_HANDLE_FILE_INFORMATION hfi;
1083 HANDLE hand = msvcrt_fdtoh(fd);
1085 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1086 if (hand == INVALID_HANDLE_VALUE)
1091 WARN(":failed-NULL buf\n");
1092 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1096 memset(&hfi, 0, sizeof(hfi));
1097 memset(buf, 0, sizeof(struct MSVCRT__stati64));
1098 if (!GetFileInformationByHandle(hand, &hfi))
1100 WARN(":failed-last error (%ld)\n",GetLastError());
1101 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1104 dw = GetFileType(hand);
1105 buf->st_mode = S_IREAD;
1106 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1107 buf->st_mode |= S_IWRITE;
1108 /* interestingly, Windows never seems to set S_IFDIR */
1109 if (dw == FILE_TYPE_CHAR)
1110 buf->st_mode |= S_IFCHR;
1111 else if (dw == FILE_TYPE_PIPE)
1112 buf->st_mode |= S_IFIFO;
1114 buf->st_mode |= S_IFREG;
1115 TRACE(":dwFileAttributes = 0x%lx, mode set to 0x%x\n",hfi.dwFileAttributes,
1117 buf->st_nlink = hfi.nNumberOfLinks;
1118 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1119 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1121 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1122 buf->st_mtime = buf->st_ctime = dw;
1126 /*********************************************************************
1129 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1131 struct MSVCRT__stati64 bufi64;
1133 ret = MSVCRT__fstati64(fd, &bufi64);
1135 msvcrt_cp_from_stati64(&bufi64, buf);
1139 /*********************************************************************
1140 * _futime (MSVCRT.@)
1142 int _futime(int fd, struct MSVCRT__utimbuf *t)
1144 HANDLE hand = msvcrt_fdtoh(fd);
1149 MSVCRT_time_t currTime;
1150 MSVCRT_time(&currTime);
1151 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1152 memcpy(&wt, &at, sizeof(wt));
1156 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1157 if (t->actime == t->modtime)
1158 memcpy(&wt, &at, sizeof(wt));
1160 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1163 if (!SetFileTime(hand, NULL, &at, &wt))
1165 msvcrt_set_errno(GetLastError());
1171 /*********************************************************************
1172 * _get_osfhandle (MSVCRT.@)
1174 long _get_osfhandle(int fd)
1176 HANDLE hand = msvcrt_fdtoh(fd);
1177 HANDLE newhand = hand;
1178 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1180 if (hand != INVALID_HANDLE_VALUE)
1182 /* FIXME: I'm not convinced that I should be copying the
1183 * handle here - it may be leaked if the app doesn't
1184 * close it (and the API docs don't say that it should)
1185 * Not duplicating it means that it can't be inherited
1186 * and so lcc's wedit doesn't cope when it passes it to
1187 * child processes. I've an idea that it should either
1188 * be copied by CreateProcess, or marked as inheritable
1189 * when initialised, or maybe both? JG 21-9-00.
1191 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
1192 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
1194 return (long)newhand;
1197 /*********************************************************************
1198 * _isatty (MSVCRT.@)
1202 HANDLE hand = msvcrt_fdtoh(fd);
1204 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1205 if (hand == INVALID_HANDLE_VALUE)
1208 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1211 /*********************************************************************
1212 * _mktemp (MSVCRT.@)
1214 char *_mktemp(char *pattern)
1217 char *retVal = pattern;
1222 numX = (*pattern++ == 'X')? numX + 1 : 0;
1226 id = GetCurrentProcessId();
1230 int tempNum = id / 10;
1231 *pattern-- = id - (tempNum * 10) + '0';
1237 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1238 GetLastError() == ERROR_FILE_NOT_FOUND)
1240 *pattern = letter++;
1241 } while(letter != '|');
1245 /*********************************************************************
1246 * _wmktemp (MSVCRT.@)
1248 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
1251 MSVCRT_wchar_t *retVal = pattern;
1253 MSVCRT_wchar_t letter = 'a';
1256 numX = (*pattern++ == 'X')? numX + 1 : 0;
1260 id = GetCurrentProcessId();
1264 int tempNum = id / 10;
1265 *pattern-- = id - (tempNum * 10) + '0';
1271 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1272 GetLastError() == ERROR_FILE_NOT_FOUND)
1274 *pattern = letter++;
1275 } while(letter != '|');
1279 static unsigned split_oflags(unsigned oflags)
1282 unsigned unsupp; /* until we support everything */
1284 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1285 if (oflags & MSVCRT__O_BINARY) ;
1286 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1287 else if (*__p__fmode() & MSVCRT__O_BINARY) ;
1288 else wxflags |= WX_TEXT; /* default to TEXT*/
1289 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1291 if ((unsupp = oflags & ~(
1292 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1293 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1294 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1295 MSVCRT__O_NOINHERIT|
1296 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1298 ERR(":unsupported oflags 0x%04x\n",unsupp);
1303 /*********************************************************************
1306 int _pipe(int *pfds, unsigned int psize, int textmode)
1309 SECURITY_ATTRIBUTES sa;
1310 HANDLE readHandle, writeHandle;
1314 *MSVCRT__errno() = MSVCRT_EINVAL;
1318 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1319 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1320 sa.lpSecurityDescriptor = NULL;
1321 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1323 unsigned int wxflags = split_oflags(textmode);
1327 fd = msvcrt_alloc_fd(readHandle, wxflags);
1331 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1340 CloseHandle(writeHandle);
1341 *MSVCRT__errno() = MSVCRT_EMFILE;
1346 CloseHandle(readHandle);
1347 CloseHandle(writeHandle);
1348 *MSVCRT__errno() = MSVCRT_EMFILE;
1353 msvcrt_set_errno(GetLastError());
1358 /*********************************************************************
1361 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1365 DWORD access = 0, creation = 0, attrib;
1369 SECURITY_ATTRIBUTES sa;
1372 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1373 path, oflags, shflags);
1375 wxflag = split_oflags(oflags);
1376 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1378 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1379 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1380 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1383 if (oflags & MSVCRT__O_CREAT)
1385 va_start(ap, shflags);
1386 pmode = va_arg(ap, int);
1389 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1390 FIXME(": pmode 0x%04x ignored\n", pmode);
1392 WARN(": pmode 0x%04x ignored\n", pmode);
1394 if (oflags & MSVCRT__O_EXCL)
1395 creation = CREATE_NEW;
1396 else if (oflags & MSVCRT__O_TRUNC)
1397 creation = CREATE_ALWAYS;
1399 creation = OPEN_ALWAYS;
1401 else /* no MSVCRT__O_CREAT */
1403 if (oflags & MSVCRT__O_TRUNC)
1404 creation = TRUNCATE_EXISTING;
1406 creation = OPEN_EXISTING;
1411 case MSVCRT__SH_DENYRW:
1414 case MSVCRT__SH_DENYWR:
1415 sharing = FILE_SHARE_READ;
1417 case MSVCRT__SH_DENYRD:
1418 sharing = FILE_SHARE_WRITE;
1420 case MSVCRT__SH_DENYNO:
1421 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1424 ERR( "Unhandled shflags 0x%x\n", shflags );
1427 attrib = FILE_ATTRIBUTE_NORMAL;
1429 if (oflags & MSVCRT__O_TEMPORARY)
1431 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1433 sharing |= FILE_SHARE_DELETE;
1436 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1437 sa.lpSecurityDescriptor = NULL;
1438 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1440 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1442 if (hand == INVALID_HANDLE_VALUE) {
1443 WARN(":failed-last error (%ld)\n",GetLastError());
1444 msvcrt_set_errno(GetLastError());
1448 fd = msvcrt_alloc_fd(hand, wxflag);
1450 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1454 if (oflags & MSVCRT__O_APPEND)
1455 _lseek(fd, 0, FILE_END);
1461 /*********************************************************************
1462 * _wsopen (MSVCRT.@)
1464 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1466 const unsigned int len = strlenW(path);
1467 char *patha = MSVCRT_calloc(len + 1,1);
1471 va_start(ap, shflags);
1472 pmode = va_arg(ap, int);
1475 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1477 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1482 msvcrt_set_errno(GetLastError());
1486 /*********************************************************************
1489 int _open( const char *path, int flags, ... )
1493 if (flags & MSVCRT__O_CREAT)
1496 va_start(ap, flags);
1497 pmode = va_arg(ap, int);
1499 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1502 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1505 /*********************************************************************
1508 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1510 const unsigned int len = strlenW(path);
1511 char *patha = MSVCRT_calloc(len + 1,1);
1515 va_start(ap, flags);
1516 pmode = va_arg(ap, int);
1519 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1521 int retval = _open(patha,flags,pmode);
1526 msvcrt_set_errno(GetLastError());
1530 /*********************************************************************
1533 int _creat(const char *path, int flags)
1535 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1536 return _open(path, usedFlags);
1539 /*********************************************************************
1540 * _wcreat (MSVCRT.@)
1542 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1544 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1545 return _wopen(path, usedFlags);
1548 /*********************************************************************
1549 * _open_osfhandle (MSVCRT.@)
1551 int _open_osfhandle(long handle, int oflags)
1555 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1556 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1557 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1558 * text - it never sets MSVCRT__O_BINARY.
1560 /* FIXME: handle more flags */
1561 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)) && (*__p__fmode() & MSVCRT__O_BINARY))
1562 oflags |= MSVCRT__O_BINARY;
1564 oflags |= MSVCRT__O_TEXT;
1566 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1567 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1571 /*********************************************************************
1576 int num_removed = 0, i;
1579 for (i = 3; i < MSVCRT_stream_idx; i++)
1580 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1582 MSVCRT_fclose(MSVCRT_fstreams[i]);
1588 TRACE(":removed (%d) temp files\n",num_removed);
1592 /*********************************************************************
1593 * (internal) remove_cr
1595 * Remove all \r inplace.
1596 * return the number of \r removed
1598 static unsigned int remove_cr(char *buf, unsigned int count)
1602 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1603 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1607 /*********************************************************************
1610 int _read(int fd, void *buf, unsigned int count)
1612 DWORD num_read, all_read = 0;
1613 char *bufstart = buf;
1614 HANDLE hand = msvcrt_fdtoh(fd);
1616 /* Don't trace small reads, it gets *very* annoying */
1618 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1619 if (hand == INVALID_HANDLE_VALUE)
1622 /* Reading single bytes in O_TEXT mode makes things slow
1623 * So read big chunks, then remove the \r in memory and try reading
1624 * the rest until the request is satisfied or EOF is met
1626 while (all_read < count)
1628 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1630 if (num_read != (count - all_read))
1633 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1634 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1635 num_read -= remove_cr(bufstart+all_read,num_read);
1636 all_read += num_read;
1638 TRACE("%s\n",debugstr_an(buf,all_read));
1641 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1643 num_read -= remove_cr(bufstart+all_read,num_read);
1645 all_read += num_read;
1649 TRACE(":failed-last error (%ld)\n",GetLastError());
1655 TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
1659 /*********************************************************************
1662 int MSVCRT__getw(MSVCRT_FILE* file)
1665 switch (_read(file->_file, &i, sizeof(int)))
1668 case 0: file->_flag |= MSVCRT__IOEOF; break;
1669 default: file->_flag |= MSVCRT__IOERR; break;
1674 /*********************************************************************
1675 * _setmode (MSVCRT.@)
1677 int _setmode(int fd,int mode)
1679 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1680 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1681 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1682 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1683 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1685 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1689 /*********************************************************************
1690 * _stati64 (MSVCRT.@)
1692 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1695 WIN32_FILE_ATTRIBUTE_DATA hfi;
1696 unsigned short mode = ALL_S_IREAD;
1699 TRACE(":file (%s) buf(%p)\n",path,buf);
1701 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1703 TRACE("failed (%ld)\n",GetLastError());
1704 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1708 memset(buf,0,sizeof(struct MSVCRT__stati64));
1710 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1711 Bon 011120: This FIXME seems incorrect
1712 Also a letter as first char isn't enough to be classified
1715 if (isalpha(*path)&& (*(path+1)==':'))
1716 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1718 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1720 plen = strlen(path);
1722 /* Dir, or regular file? */
1723 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1724 (path[plen-1] == '\\'))
1725 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1728 mode |= MSVCRT__S_IFREG;
1730 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1732 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1733 (tolower(path[plen-3]) << 16);
1734 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1735 mode |= ALL_S_IEXEC;
1739 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1740 mode |= ALL_S_IWRITE;
1742 buf->st_mode = mode;
1744 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1745 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1747 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1748 buf->st_mtime = buf->st_ctime = dw;
1749 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1750 (long)(buf->st_size >> 32),(long)buf->st_size,
1751 buf->st_atime,buf->st_mtime, buf->st_ctime);
1755 /*********************************************************************
1758 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1760 struct MSVCRT__stati64 bufi64;
1762 ret = MSVCRT__stati64( path, &bufi64);
1764 msvcrt_cp_from_stati64(&bufi64, buf);
1768 /*********************************************************************
1769 * _wstati64 (MSVCRT.@)
1771 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1774 WIN32_FILE_ATTRIBUTE_DATA hfi;
1775 unsigned short mode = ALL_S_IREAD;
1778 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1780 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1782 TRACE("failed (%ld)\n",GetLastError());
1783 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1787 memset(buf,0,sizeof(struct MSVCRT__stat));
1789 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1790 if (MSVCRT_iswalpha(*path))
1791 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1793 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1795 plen = strlenW(path);
1797 /* Dir, or regular file? */
1798 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1799 (path[plen-1] == '\\'))
1800 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1803 mode |= MSVCRT__S_IFREG;
1805 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1807 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1808 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1809 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1810 mode |= ALL_S_IEXEC;
1814 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1815 mode |= ALL_S_IWRITE;
1817 buf->st_mode = mode;
1819 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1820 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1822 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1823 buf->st_mtime = buf->st_ctime = dw;
1824 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1825 (long)(buf->st_size >> 32),(long)buf->st_size,
1826 buf->st_atime,buf->st_mtime, buf->st_ctime);
1830 /*********************************************************************
1833 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1836 struct MSVCRT__stati64 bufi64;
1838 ret = MSVCRT__wstati64( path, &bufi64 );
1839 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1843 /*********************************************************************
1848 return _lseek(fd, 0, SEEK_CUR);
1851 /*********************************************************************
1852 * _telli64 (MSVCRT.@)
1854 __int64 _telli64(int fd)
1856 return _lseeki64(fd, 0, SEEK_CUR);
1859 /*********************************************************************
1860 * _tempnam (MSVCRT.@)
1862 char *_tempnam(const char *dir, const char *prefix)
1864 char tmpbuf[MAX_PATH];
1866 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1867 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1869 TRACE("got name (%s)\n",tmpbuf);
1870 DeleteFileA(tmpbuf);
1871 return _strdup(tmpbuf);
1873 TRACE("failed (%ld)\n",GetLastError());
1877 /*********************************************************************
1878 * _wtempnam (MSVCRT.@)
1880 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1882 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1884 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1885 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1887 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1888 DeleteFileW(tmpbuf);
1889 return _wcsdup(tmpbuf);
1891 TRACE("failed (%ld)\n",GetLastError());
1895 /*********************************************************************
1898 int _umask(int umask)
1900 int old_umask = MSVCRT_umask;
1901 TRACE("(%d)\n",umask);
1902 MSVCRT_umask = umask;
1906 /*********************************************************************
1909 int _utime(const char* path, struct MSVCRT__utimbuf *t)
1911 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1915 int retVal = _futime(fd, t);
1922 /*********************************************************************
1923 * _wutime (MSVCRT.@)
1925 int _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1927 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1931 int retVal = _futime(fd, t);
1938 /*********************************************************************
1941 int _write(int fd, const void* buf, unsigned int count)
1944 HANDLE hand = msvcrt_fdtoh(fd);
1946 /* Don't trace small writes, it gets *very* annoying */
1949 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1951 if (hand == INVALID_HANDLE_VALUE)
1953 *MSVCRT__errno() = MSVCRT_EBADF;
1957 /* If appending, go to EOF */
1958 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
1959 _lseek(fd, 0, FILE_END);
1961 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
1963 if (WriteFile(hand, buf, count, &num_written, NULL)
1964 && (num_written == count))
1966 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd,
1967 hand, GetLastError());
1968 *MSVCRT__errno() = MSVCRT_ENOSPC;
1972 unsigned int i, j, nr_lf;
1973 char *s =(char*)buf, *buf_start=(char*)buf, *p;
1974 /* find number of \n ( without preceeding \r */
1975 for ( nr_lf=0,i = 0; i <count; i++)
1980 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1985 if ((p = MSVCRT_malloc(count + nr_lf)))
1987 for(s=(char*)buf, i=0, j=0; i<count; i++)
1992 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1999 FIXME("Malloc failed\n");
2007 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2009 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld), num_written %ld\n",
2010 fd, hand, GetLastError(), num_written);
2011 *MSVCRT__errno() = MSVCRT_ENOSPC;
2014 return s - buf_start;
2026 /*********************************************************************
2029 int MSVCRT__putw(int val, MSVCRT_FILE* file)
2032 len = _write(file->_file, &val, sizeof(val));
2033 if (len == sizeof(val)) return val;
2034 file->_flag |= MSVCRT__IOERR;
2038 /*********************************************************************
2041 int MSVCRT_fclose(MSVCRT_FILE* file)
2046 if (file->_tmpfname)
2048 MSVCRT_free(file->_tmpfname);
2049 file->_tmpfname = NULL;
2051 /* flush stdio buffers */
2052 if(file->_flag & MSVCRT__IOWRT)
2053 MSVCRT_fflush(file);
2054 if(file->_flag & MSVCRT__IOMYBUF)
2055 MSVCRT_free(file->_base);
2057 r=_close(file->_file);
2061 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2064 /*********************************************************************
2067 int MSVCRT_feof(MSVCRT_FILE* file)
2069 return file->_flag & MSVCRT__IOEOF;
2072 /*********************************************************************
2075 int MSVCRT_ferror(MSVCRT_FILE* file)
2077 return file->_flag & MSVCRT__IOERR;
2080 /*********************************************************************
2081 * _filbuf (MSVCRT.@)
2083 int MSVCRT__filbuf(MSVCRT_FILE* file)
2085 /* Allocate buffer if needed */
2086 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2087 msvcrt_alloc_buffer(file);
2089 if(!(file->_flag & MSVCRT__IOREAD)) {
2090 if(file->_flag & MSVCRT__IORW) {
2091 file->_flag |= MSVCRT__IOREAD;
2096 if(file->_flag & MSVCRT__IONBF) {
2099 if ((r = _read(file->_file,&c,1)) != 1) {
2100 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2105 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2107 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2112 file->_ptr = file->_base+1;
2113 return *(unsigned char *)file->_base;
2117 /*********************************************************************
2120 int MSVCRT_fgetc(MSVCRT_FILE* file)
2124 return *(unsigned char *)file->_ptr++;
2126 return MSVCRT__filbuf(file);
2130 /*********************************************************************
2131 * _fgetchar (MSVCRT.@)
2135 return MSVCRT_fgetc(MSVCRT_stdin);
2138 /*********************************************************************
2141 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2143 int cc = MSVCRT_EOF;
2144 char * buf_start = s;
2146 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2147 file,file->_file,s,size);
2149 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2154 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2156 TRACE(":nothing read\n");
2159 if ((cc != MSVCRT_EOF) && (size > 1))
2162 TRACE(":got '%s'\n", debugstr_a(buf_start));
2166 /*********************************************************************
2169 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2170 * the CR from CR/LF combinations
2172 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
2176 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2180 if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
2182 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2187 c = MSVCRT_fgetc(file);
2188 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2190 FIXME("Treat Multibyte characters\n");
2192 if (c == MSVCRT_EOF)
2195 return (MSVCRT_wint_t)c;
2198 /*********************************************************************
2201 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
2203 return MSVCRT_fgetwc(file);
2206 /*********************************************************************
2207 * _fgetwchar (MSVCRT.@)
2209 MSVCRT_wint_t _fgetwchar(void)
2211 return MSVCRT_fgetwc(MSVCRT_stdin);
2214 /*********************************************************************
2215 * getwchar (MSVCRT.@)
2217 MSVCRT_wint_t MSVCRT_getwchar(void)
2219 return _fgetwchar();
2222 /*********************************************************************
2225 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2227 int cc = MSVCRT_WEOF;
2228 MSVCRT_wchar_t * buf_start = s;
2230 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2231 file,file->_file,s,size);
2233 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2238 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2240 TRACE(":nothing read\n");
2243 if ((cc != MSVCRT_WEOF) && (size > 1))
2246 TRACE(":got %s\n", debugstr_w(buf_start));
2250 /*********************************************************************
2253 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2255 MSVCRT_size_t wrcnt=size * nmemb;
2260 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2261 memcpy(file->_ptr, ptr, pcnt);
2266 ptr = (const char*)ptr + pcnt;
2267 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2268 if(file->_flag & MSVCRT__IORW) {
2269 file->_flag |= MSVCRT__IOWRT;
2275 int res=msvcrt_flush_buffer(file);
2277 int pwritten = _write(file->_file, ptr, wrcnt);
2280 file->_flag |= MSVCRT__IOERR;
2283 written += pwritten;
2286 return written / size;
2289 /*********************************************************************
2292 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2294 MSVCRT_wchar_t mwc=wc;
2295 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2300 /*********************************************************************
2301 * _fputwchar (MSVCRT.@)
2303 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
2305 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2308 /*********************************************************************
2311 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
2314 int open_flags, stream_flags, fd;
2316 TRACE("(%s,%s)\n",path,mode);
2318 /* map mode string to open() flags. "man fopen" for possibilities. */
2319 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2323 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2326 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2328 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2335 TRACE(":got (%p)\n",file);
2342 /*********************************************************************
2343 * _wfopen (MSVCRT.@)
2345 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2347 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2348 char *patha = MSVCRT_calloc(plen + 1, 1);
2349 char *modea = MSVCRT_calloc(mlen + 1, 1);
2351 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2353 if (patha && modea &&
2354 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2355 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2357 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2363 msvcrt_set_errno(GetLastError());
2367 /*********************************************************************
2368 * _fsopen (MSVCRT.@)
2370 MSVCRT_FILE* MSVCRT__fsopen(const char *path, const char *mode, int share)
2372 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2373 return MSVCRT_fopen(path,mode);
2376 /*********************************************************************
2377 * _wfsopen (MSVCRT.@)
2379 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2381 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2382 debugstr_w(path),debugstr_w(mode),share);
2383 return MSVCRT__wfopen(path,mode);
2386 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2387 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2389 /*********************************************************************
2392 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2399 int res = msvcrt_flush_buffer(file);
2400 return res ? res : c;
2405 return MSVCRT__flsbuf(c, file);
2409 /*********************************************************************
2410 * _flsbuf (MSVCRT.@)
2412 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2414 /* Flush output buffer */
2415 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2416 msvcrt_alloc_buffer(file);
2418 if(!(file->_flag & MSVCRT__IOWRT)) {
2419 if(file->_flag & MSVCRT__IORW) {
2420 file->_flag |= MSVCRT__IOWRT;
2426 int res=msvcrt_flush_buffer(file);
2427 return res?res : MSVCRT_fputc(c, file);
2431 len = _write(file->_file, &cc, 1);
2432 if (len == 1) return c;
2433 file->_flag |= MSVCRT__IOERR;
2438 /*********************************************************************
2439 * _fputchar (MSVCRT.@)
2441 int _fputchar(int c)
2443 return MSVCRT_fputc(c, MSVCRT_stdout);
2446 /*********************************************************************
2449 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2450 { MSVCRT_size_t rcnt=size * nmemb;
2451 MSVCRT_size_t read=0;
2454 /* first buffered data */
2456 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2457 memcpy(ptr, file->_ptr, pcnt);
2462 ptr = (char*)ptr + pcnt;
2463 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2464 if(file->_flag & MSVCRT__IORW) {
2465 file->_flag |= MSVCRT__IOREAD;
2471 pread = _read(file->_file,ptr, rcnt);
2472 /* expose feof condition in the flags
2473 * MFC tests file->_flag for feof, and doesn't not call feof())
2476 file->_flag |= MSVCRT__IOEOF;
2477 else if (pread == -1)
2479 file->_flag |= MSVCRT__IOERR;
2487 /*********************************************************************
2488 * freopen (MSVCRT.@)
2491 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2493 int open_flags, stream_flags, fd;
2495 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2498 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2502 MSVCRT_fclose(file);
2503 /* map mode string to open() flags. "man fopen" for possibilities. */
2504 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2508 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2511 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2514 WARN(":failed-last error (%ld)\n",GetLastError());
2515 msvcrt_set_errno(GetLastError());
2524 /*********************************************************************
2525 * fsetpos (MSVCRT.@)
2527 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2529 /* Note that all this has been lifted 'as is' from fseek */
2530 if(file->_flag & MSVCRT__IOWRT)
2531 msvcrt_flush_buffer(file);
2533 /* Discard buffered input */
2535 file->_ptr = file->_base;
2537 /* Reset direction of i/o */
2538 if(file->_flag & MSVCRT__IORW) {
2539 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2542 return _lseeki64(file->_file,*pos,SEEK_SET);
2545 /*********************************************************************
2548 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2553 if( file->_flag & MSVCRT__IOWRT ) {
2554 off = file->_ptr - file->_base;
2559 pos = _tell(file->_file);
2560 if(pos == -1) return pos;
2564 /*********************************************************************
2565 * fgetpos (MSVCRT.@)
2567 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2569 /* This code has been lifted form the MSVCRT_ftell function */
2572 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2574 if (*pos == -1) return -1;
2577 if( file->_flag & MSVCRT__IOWRT ) {
2578 off = file->_ptr - file->_base;
2588 /*********************************************************************
2591 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2593 size_t i, len = strlen(s);
2594 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2595 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2596 for (i=0; i<len; i++)
2597 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2602 /*********************************************************************
2605 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2607 size_t i, len = strlenW(s);
2608 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2609 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2610 for (i=0; i<len; i++)
2612 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2614 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2620 /*********************************************************************
2621 * getchar (MSVCRT.@)
2623 int MSVCRT_getchar(void)
2625 return MSVCRT_fgetc(MSVCRT_stdin);
2628 /*********************************************************************
2631 int MSVCRT_getc(MSVCRT_FILE* file)
2633 return MSVCRT_fgetc(file);
2636 /*********************************************************************
2639 char *MSVCRT_gets(char *buf)
2642 char * buf_start = buf;
2644 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2645 cc = MSVCRT_fgetc(MSVCRT_stdin))
2646 if(cc != '\r') *buf++ = (char)cc;
2650 TRACE("got '%s'\n", buf_start);
2654 /*********************************************************************
2657 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2660 MSVCRT_wchar_t* ws = buf;
2662 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2663 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2666 *buf++ = (MSVCRT_wchar_t)cc;
2670 TRACE("got '%s'\n", debugstr_w(ws));
2674 /*********************************************************************
2677 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2679 return MSVCRT_fputc(c, file);
2682 /*********************************************************************
2683 * putchar (MSVCRT.@)
2685 int MSVCRT_putchar(int c)
2687 return MSVCRT_fputc(c, MSVCRT_stdout);
2690 /*********************************************************************
2693 int MSVCRT_puts(const char *s)
2695 size_t len = strlen(s);
2696 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2697 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2700 /*********************************************************************
2703 int _putws(const MSVCRT_wchar_t *s)
2705 static const MSVCRT_wchar_t nl = '\n';
2706 size_t len = strlenW(s);
2707 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2708 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2711 /*********************************************************************
2714 int MSVCRT_remove(const char *path)
2716 TRACE("(%s)\n",path);
2717 if (DeleteFileA(path))
2719 TRACE(":failed (%ld)\n",GetLastError());
2720 msvcrt_set_errno(GetLastError());
2724 /*********************************************************************
2725 * _wremove (MSVCRT.@)
2727 int _wremove(const MSVCRT_wchar_t *path)
2729 TRACE("(%s)\n",debugstr_w(path));
2730 if (DeleteFileW(path))
2732 TRACE(":failed (%ld)\n",GetLastError());
2733 msvcrt_set_errno(GetLastError());
2737 /*********************************************************************
2740 int MSVCRT_rename(const char *oldpath,const char *newpath)
2742 TRACE(":from %s to %s\n",oldpath,newpath);
2743 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2745 TRACE(":failed (%ld)\n",GetLastError());
2746 msvcrt_set_errno(GetLastError());
2750 /*********************************************************************
2751 * _wrename (MSVCRT.@)
2753 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2755 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2756 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2758 TRACE(":failed (%ld)\n",GetLastError());
2759 msvcrt_set_errno(GetLastError());
2763 /*********************************************************************
2764 * setvbuf (MSVCRT.@)
2766 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2768 /* TODO: Check if file busy */
2770 MSVCRT_free(file->_base);
2774 if(mode == MSVCRT__IOFBF) {
2775 file->_flag &= ~MSVCRT__IONBF;
2776 file->_base = file->_ptr = buf;
2778 file->_bufsiz = size;
2781 file->_flag |= MSVCRT__IONBF;
2786 /*********************************************************************
2789 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2791 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2794 /*********************************************************************
2797 char *MSVCRT_tmpnam(char *s)
2805 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2806 p = s + sprintf(s, "\\s%s.", tmpstr);
2807 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2809 msvcrt_int_to_base32(unique++, tmpstr);
2811 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2812 GetLastError() == ERROR_FILE_NOT_FOUND)
2818 /*********************************************************************
2819 * tmpfile (MSVCRT.@)
2821 MSVCRT_FILE* MSVCRT_tmpfile(void)
2823 char *filename = MSVCRT_tmpnam(NULL);
2825 MSVCRT_FILE* file = NULL;
2828 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2829 if (fd != -1 && (file = msvcrt_alloc_fp()))
2831 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2836 else file->_tmpfname = _strdup(filename);
2842 /*********************************************************************
2843 * vfprintf (MSVCRT.@)
2845 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2847 char buf[2048], *mem = buf;
2848 int written, resize = sizeof(buf), retval;
2849 /* There are two conventions for vsnprintf failing:
2850 * Return -1 if we truncated, or
2851 * Return the number of bytes that would have been written
2852 * The code below handles both cases
2854 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2857 resize = (written == -1 ? resize * 2 : written + 1);
2860 if (!(mem = (char *)MSVCRT_malloc(resize)))
2863 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2869 /*********************************************************************
2870 * vfwprintf (MSVCRT.@)
2872 * Is final char included in written (then resize is too big) or not
2873 * (then we must test for equality too)?
2875 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2877 MSVCRT_wchar_t buf[2048], *mem = buf;
2878 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2879 /* See vfprintf comments */
2880 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2883 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2886 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2889 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2895 /*********************************************************************
2896 * vprintf (MSVCRT.@)
2898 int MSVCRT_vprintf(const char *format, va_list valist)
2900 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2903 /*********************************************************************
2904 * vwprintf (MSVCRT.@)
2906 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2908 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2911 /*********************************************************************
2912 * fprintf (MSVCRT.@)
2914 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2918 va_start(valist, format);
2919 res = MSVCRT_vfprintf(file, format, valist);
2924 /*********************************************************************
2925 * fwprintf (MSVCRT.@)
2927 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2931 va_start(valist, format);
2932 res = MSVCRT_vfwprintf(file, format, valist);
2937 /*********************************************************************
2940 int MSVCRT_printf(const char *format, ...)
2944 va_start(valist, format);
2945 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2950 /*********************************************************************
2953 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2955 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2956 msvcrt_alloc_buffer(file);
2959 if(file->_ptr>file->_base) {
2968 /*********************************************************************
2969 * ungetwc (MSVCRT.@)
2971 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2973 MSVCRT_wchar_t mwc = wc;
2974 char * pp = (char *)&mwc;
2976 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2977 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2983 /*********************************************************************
2984 * wprintf (MSVCRT.@)
2986 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2990 va_start(valist, format);
2991 res = MSVCRT_vwprintf(format, valist);
2996 /*********************************************************************
2997 * __pioinfo (MSVCRT.@)
2998 * FIXME: see MSVCRT_MAX_FILES define.
3000 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3001 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3002 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3003 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3004 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3005 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3006 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3007 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3008 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3009 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3010 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3011 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3014 /*********************************************************************
3015 * __badioinfo (MSVCRT.@)
3017 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };