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)
776 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
777 * stdout, and stderr (unlike GNU), so we need to fclose() them here
778 * or they won't get flushed.
780 MSVCRT_fclose(&MSVCRT__iob[0]);
781 MSVCRT_fclose(&MSVCRT__iob[1]);
782 MSVCRT_fclose(&MSVCRT__iob[2]);
783 DeleteCriticalSection(&MSVCRT_file_cs);
786 /*********************************************************************
787 * _lseeki64 (MSVCRT.@)
789 __int64 _lseeki64(int fd, __int64 offset, int whence)
791 DWORD ret, hoffset = (DWORD) (offset >> 32);
792 HANDLE hand = msvcrt_fdtoh(fd);
794 TRACE(":fd (%d) handle (%p)\n",fd,hand);
795 if (hand == INVALID_HANDLE_VALUE)
798 if (whence < 0 || whence > 2)
800 *MSVCRT__errno() = MSVCRT_EINVAL;
804 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
805 fd,hoffset,(long)offset,
806 (whence==SEEK_SET)?"SEEK_SET":
807 (whence==SEEK_CUR)?"SEEK_CUR":
808 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
810 ret = SetFilePointer(hand, (long)offset, &hoffset, whence);
811 if (ret != INVALID_SET_FILE_POINTER || !GetLastError())
813 MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
814 /* FIXME: What if we seek _to_ EOF - is EOF set? */
816 return ((__int64)hoffset << 32) | ret;
818 TRACE(":error-last error (%ld)\n",GetLastError());
819 msvcrt_set_errno(GetLastError());
823 /*********************************************************************
826 LONG _lseek(int fd, LONG offset, int whence)
828 return _lseeki64(fd, offset, whence);
831 /*********************************************************************
832 * _locking (MSVCRT.@)
834 * This is untested; the underlying LockFile doesn't work yet.
836 int _locking(int fd, int mode, LONG nbytes)
840 HANDLE hand = msvcrt_fdtoh(fd);
842 TRACE(":fd (%d) handle (%p)\n",fd,hand);
843 if (hand == INVALID_HANDLE_VALUE)
846 if (mode < 0 || mode > 4)
848 *MSVCRT__errno() = MSVCRT_EINVAL;
852 TRACE(":fd (%d) by 0x%08lx mode %s\n",
853 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
854 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
855 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
856 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
857 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
860 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
862 FIXME ("Seek failed\n");
863 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
866 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
869 ret = 1; /* just to satisfy gcc */
872 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
877 else if (mode == MSVCRT__LK_UNLCK)
878 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
880 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
881 /* FIXME - what about error settings? */
885 /*********************************************************************
888 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
890 /* Flush output if needed */
891 if(file->_flag & MSVCRT__IOWRT)
892 msvcrt_flush_buffer(file);
894 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
895 offset -= file->_cnt;
897 /* Discard buffered input */
899 file->_ptr = file->_base;
900 /* Reset direction of i/o */
901 if(file->_flag & MSVCRT__IORW) {
902 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
904 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
907 /*********************************************************************
910 int _chsize(int fd, long size)
916 TRACE("(fd=%d, size=%ld)\n", fd, size);
920 handle = msvcrt_fdtoh(fd);
921 if (handle != INVALID_HANDLE_VALUE)
923 /* save the current file pointer */
924 cur = _lseek(fd, 0, SEEK_CUR);
927 pos = _lseek(fd, size, SEEK_SET);
930 ret = SetEndOfFile(handle);
931 if (!ret) msvcrt_set_errno(GetLastError());
934 /* restore the file pointer */
935 _lseek(fd, cur, SEEK_SET);
943 /*********************************************************************
944 * clearerr (MSVCRT.@)
946 void MSVCRT_clearerr(MSVCRT_FILE* file)
948 TRACE(":file (%p) fd (%d)\n",file,file->_file);
949 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
952 /*********************************************************************
955 void MSVCRT_rewind(MSVCRT_FILE* file)
957 TRACE(":file (%p) fd (%d)\n",file,file->_file);
958 MSVCRT_fseek(file, 0L, SEEK_SET);
959 MSVCRT_clearerr(file);
962 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
964 int plus = strchr(mode, '+') != NULL;
969 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
970 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
973 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
974 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
977 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
978 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
988 *open_flags |= MSVCRT__O_BINARY;
989 *open_flags &= ~MSVCRT__O_TEXT;
992 *open_flags |= MSVCRT__O_TEXT;
993 *open_flags &= ~MSVCRT__O_BINARY;
998 FIXME(":unknown flag %c not supported\n",mode[-1]);
1003 /*********************************************************************
1004 * _fdopen (MSVCRT.@)
1006 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
1008 int open_flags, stream_flags;
1011 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1014 if (!(file = msvcrt_alloc_fp()))
1016 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1021 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1027 /*********************************************************************
1028 * _wfdopen (MSVCRT.@)
1030 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1032 unsigned mlen = strlenW(mode);
1033 char *modea = MSVCRT_calloc(mlen + 1, 1);
1034 MSVCRT_FILE* file = NULL;
1035 int open_flags, stream_flags;
1038 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1040 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1042 if (!(file = msvcrt_alloc_fp()))
1044 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1052 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1053 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1060 /*********************************************************************
1061 * _filelength (MSVCRT.@)
1063 LONG _filelength(int fd)
1065 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1068 LONG endPos = _lseek(fd, 0, SEEK_END);
1071 if (endPos != curPos)
1072 _lseek(fd, curPos, SEEK_SET);
1079 /*********************************************************************
1080 * _filelengthi64 (MSVCRT.@)
1082 __int64 _filelengthi64(int fd)
1084 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1087 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1090 if (endPos != curPos)
1091 _lseeki64(fd, curPos, SEEK_SET);
1098 /*********************************************************************
1099 * _fileno (MSVCRT.@)
1101 int MSVCRT__fileno(MSVCRT_FILE* file)
1103 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1107 /*********************************************************************
1108 * _fstati64 (MSVCRT.@)
1110 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1113 BY_HANDLE_FILE_INFORMATION hfi;
1114 HANDLE hand = msvcrt_fdtoh(fd);
1116 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1117 if (hand == INVALID_HANDLE_VALUE)
1122 WARN(":failed-NULL buf\n");
1123 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1127 memset(&hfi, 0, sizeof(hfi));
1128 memset(buf, 0, sizeof(struct MSVCRT__stati64));
1129 if (!GetFileInformationByHandle(hand, &hfi))
1131 WARN(":failed-last error (%ld)\n",GetLastError());
1132 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1135 dw = GetFileType(hand);
1136 buf->st_mode = S_IREAD;
1137 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1138 buf->st_mode |= S_IWRITE;
1139 /* interestingly, Windows never seems to set S_IFDIR */
1140 if (dw == FILE_TYPE_CHAR)
1141 buf->st_mode |= S_IFCHR;
1142 else if (dw == FILE_TYPE_PIPE)
1143 buf->st_mode |= S_IFIFO;
1145 buf->st_mode |= S_IFREG;
1146 TRACE(":dwFileAttributes = 0x%lx, mode set to 0x%x\n",hfi.dwFileAttributes,
1148 buf->st_nlink = hfi.nNumberOfLinks;
1149 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1150 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1152 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1153 buf->st_mtime = buf->st_ctime = dw;
1157 /*********************************************************************
1160 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1162 struct MSVCRT__stati64 bufi64;
1164 ret = MSVCRT__fstati64(fd, &bufi64);
1166 msvcrt_cp_from_stati64(&bufi64, buf);
1170 /*********************************************************************
1171 * _futime (MSVCRT.@)
1173 int _futime(int fd, struct MSVCRT__utimbuf *t)
1175 HANDLE hand = msvcrt_fdtoh(fd);
1180 MSVCRT_time_t currTime;
1181 MSVCRT_time(&currTime);
1182 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1183 memcpy(&wt, &at, sizeof(wt));
1187 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1188 if (t->actime == t->modtime)
1189 memcpy(&wt, &at, sizeof(wt));
1191 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1194 if (!SetFileTime(hand, NULL, &at, &wt))
1196 msvcrt_set_errno(GetLastError());
1202 /*********************************************************************
1203 * _get_osfhandle (MSVCRT.@)
1205 long _get_osfhandle(int fd)
1207 HANDLE hand = msvcrt_fdtoh(fd);
1208 HANDLE newhand = hand;
1209 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1211 if (hand != INVALID_HANDLE_VALUE)
1213 /* FIXME: I'm not convinced that I should be copying the
1214 * handle here - it may be leaked if the app doesn't
1215 * close it (and the API docs don't say that it should)
1216 * Not duplicating it means that it can't be inherited
1217 * and so lcc's wedit doesn't cope when it passes it to
1218 * child processes. I've an idea that it should either
1219 * be copied by CreateProcess, or marked as inheritable
1220 * when initialised, or maybe both? JG 21-9-00.
1222 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
1223 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
1225 return (long)newhand;
1228 /*********************************************************************
1229 * _isatty (MSVCRT.@)
1233 HANDLE hand = msvcrt_fdtoh(fd);
1235 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1236 if (hand == INVALID_HANDLE_VALUE)
1239 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1242 /*********************************************************************
1243 * _mktemp (MSVCRT.@)
1245 char *_mktemp(char *pattern)
1248 char *retVal = pattern;
1253 numX = (*pattern++ == 'X')? numX + 1 : 0;
1257 id = GetCurrentProcessId();
1261 int tempNum = id / 10;
1262 *pattern-- = id - (tempNum * 10) + '0';
1268 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1269 GetLastError() == ERROR_FILE_NOT_FOUND)
1271 *pattern = letter++;
1272 } while(letter != '|');
1276 /*********************************************************************
1277 * _wmktemp (MSVCRT.@)
1279 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
1282 MSVCRT_wchar_t *retVal = pattern;
1284 MSVCRT_wchar_t letter = 'a';
1287 numX = (*pattern++ == 'X')? numX + 1 : 0;
1291 id = GetCurrentProcessId();
1295 int tempNum = id / 10;
1296 *pattern-- = id - (tempNum * 10) + '0';
1302 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1303 GetLastError() == ERROR_FILE_NOT_FOUND)
1305 *pattern = letter++;
1306 } while(letter != '|');
1310 static unsigned split_oflags(unsigned oflags)
1313 unsigned unsupp; /* until we support everything */
1315 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1316 if (oflags & MSVCRT__O_BINARY) ;
1317 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1318 else if (*__p__fmode() & MSVCRT__O_BINARY) ;
1319 else wxflags |= WX_TEXT; /* default to TEXT*/
1320 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1322 if ((unsupp = oflags & ~(
1323 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1324 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1325 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1326 MSVCRT__O_NOINHERIT|
1327 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1329 ERR(":unsupported oflags 0x%04x\n",unsupp);
1334 /*********************************************************************
1337 int _pipe(int *pfds, unsigned int psize, int textmode)
1340 SECURITY_ATTRIBUTES sa;
1341 HANDLE readHandle, writeHandle;
1345 *MSVCRT__errno() = MSVCRT_EINVAL;
1349 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1350 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1351 sa.lpSecurityDescriptor = NULL;
1352 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1354 unsigned int wxflags = split_oflags(textmode);
1358 fd = msvcrt_alloc_fd(readHandle, wxflags);
1362 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1371 CloseHandle(writeHandle);
1372 *MSVCRT__errno() = MSVCRT_EMFILE;
1377 CloseHandle(readHandle);
1378 CloseHandle(writeHandle);
1379 *MSVCRT__errno() = MSVCRT_EMFILE;
1384 msvcrt_set_errno(GetLastError());
1389 /*********************************************************************
1392 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1396 DWORD access = 0, creation = 0, attrib;
1400 SECURITY_ATTRIBUTES sa;
1403 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1404 path, oflags, shflags);
1406 wxflag = split_oflags(oflags);
1407 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1409 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1410 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1411 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1414 if (oflags & MSVCRT__O_CREAT)
1416 va_start(ap, shflags);
1417 pmode = va_arg(ap, int);
1420 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1421 FIXME(": pmode 0x%04x ignored\n", pmode);
1423 WARN(": pmode 0x%04x ignored\n", pmode);
1425 if (oflags & MSVCRT__O_EXCL)
1426 creation = CREATE_NEW;
1427 else if (oflags & MSVCRT__O_TRUNC)
1428 creation = CREATE_ALWAYS;
1430 creation = OPEN_ALWAYS;
1432 else /* no MSVCRT__O_CREAT */
1434 if (oflags & MSVCRT__O_TRUNC)
1435 creation = TRUNCATE_EXISTING;
1437 creation = OPEN_EXISTING;
1442 case MSVCRT__SH_DENYRW:
1445 case MSVCRT__SH_DENYWR:
1446 sharing = FILE_SHARE_READ;
1448 case MSVCRT__SH_DENYRD:
1449 sharing = FILE_SHARE_WRITE;
1451 case MSVCRT__SH_DENYNO:
1452 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1455 ERR( "Unhandled shflags 0x%x\n", shflags );
1458 attrib = FILE_ATTRIBUTE_NORMAL;
1460 if (oflags & MSVCRT__O_TEMPORARY)
1462 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1464 sharing |= FILE_SHARE_DELETE;
1467 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1468 sa.lpSecurityDescriptor = NULL;
1469 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1471 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1473 if (hand == INVALID_HANDLE_VALUE) {
1474 WARN(":failed-last error (%ld)\n",GetLastError());
1475 msvcrt_set_errno(GetLastError());
1479 fd = msvcrt_alloc_fd(hand, wxflag);
1481 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1485 if (oflags & MSVCRT__O_APPEND)
1486 _lseek(fd, 0, FILE_END);
1492 /*********************************************************************
1493 * _wsopen (MSVCRT.@)
1495 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1497 const unsigned int len = strlenW(path);
1498 char *patha = MSVCRT_calloc(len + 1,1);
1502 va_start(ap, shflags);
1503 pmode = va_arg(ap, int);
1506 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1508 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1513 msvcrt_set_errno(GetLastError());
1517 /*********************************************************************
1520 int _open( const char *path, int flags, ... )
1524 if (flags & MSVCRT__O_CREAT)
1527 va_start(ap, flags);
1528 pmode = va_arg(ap, int);
1530 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1533 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1536 /*********************************************************************
1539 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1541 const unsigned int len = strlenW(path);
1542 char *patha = MSVCRT_calloc(len + 1,1);
1546 va_start(ap, flags);
1547 pmode = va_arg(ap, int);
1550 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1552 int retval = _open(patha,flags,pmode);
1557 msvcrt_set_errno(GetLastError());
1561 /*********************************************************************
1564 int _creat(const char *path, int flags)
1566 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1567 return _open(path, usedFlags);
1570 /*********************************************************************
1571 * _wcreat (MSVCRT.@)
1573 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1575 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1576 return _wopen(path, usedFlags);
1579 /*********************************************************************
1580 * _open_osfhandle (MSVCRT.@)
1582 int _open_osfhandle(long handle, int oflags)
1586 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1587 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1588 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1589 * text - it never sets MSVCRT__O_BINARY.
1591 /* FIXME: handle more flags */
1592 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)) && (*__p__fmode() & MSVCRT__O_BINARY))
1593 oflags |= MSVCRT__O_BINARY;
1595 oflags |= MSVCRT__O_TEXT;
1597 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1598 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1602 /*********************************************************************
1607 int num_removed = 0, i;
1610 for (i = 3; i < MSVCRT_stream_idx; i++)
1611 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1613 MSVCRT_fclose(MSVCRT_fstreams[i]);
1619 TRACE(":removed (%d) temp files\n",num_removed);
1623 /*********************************************************************
1624 * (internal) remove_cr
1626 * Remove all \r inplace.
1627 * return the number of \r removed
1629 static unsigned int remove_cr(char *buf, unsigned int count)
1633 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1634 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1638 /*********************************************************************
1641 int _read(int fd, void *buf, unsigned int count)
1643 DWORD num_read, all_read = 0;
1644 char *bufstart = buf;
1645 HANDLE hand = msvcrt_fdtoh(fd);
1647 /* Don't trace small reads, it gets *very* annoying */
1649 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1650 if (hand == INVALID_HANDLE_VALUE)
1653 /* Reading single bytes in O_TEXT mode makes things slow
1654 * So read big chunks, then remove the \r in memory and try reading
1655 * the rest until the request is satisfied or EOF is met
1657 while (all_read < count)
1659 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1661 if (num_read != (count - all_read))
1664 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1665 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1666 num_read -= remove_cr(bufstart+all_read,num_read);
1667 all_read += num_read;
1669 TRACE("%s\n",debugstr_an(buf,all_read));
1672 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1674 num_read -= remove_cr(bufstart+all_read,num_read);
1676 all_read += num_read;
1680 TRACE(":failed-last error (%ld)\n",GetLastError());
1686 TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
1690 /*********************************************************************
1693 int MSVCRT__getw(MSVCRT_FILE* file)
1696 switch (_read(file->_file, &i, sizeof(int)))
1699 case 0: file->_flag |= MSVCRT__IOEOF; break;
1700 default: file->_flag |= MSVCRT__IOERR; break;
1705 /*********************************************************************
1706 * _setmode (MSVCRT.@)
1708 int _setmode(int fd,int mode)
1710 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1711 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1712 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1713 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1714 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1716 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1720 /*********************************************************************
1721 * _stati64 (MSVCRT.@)
1723 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1726 WIN32_FILE_ATTRIBUTE_DATA hfi;
1727 unsigned short mode = ALL_S_IREAD;
1730 TRACE(":file (%s) buf(%p)\n",path,buf);
1732 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1734 TRACE("failed (%ld)\n",GetLastError());
1735 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1739 memset(buf,0,sizeof(struct MSVCRT__stati64));
1741 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1742 Bon 011120: This FIXME seems incorrect
1743 Also a letter as first char isn't enough to be classified
1746 if (isalpha(*path)&& (*(path+1)==':'))
1747 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1749 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1751 plen = strlen(path);
1753 /* Dir, or regular file? */
1754 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1755 (path[plen-1] == '\\'))
1756 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1759 mode |= MSVCRT__S_IFREG;
1761 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1763 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1764 (tolower(path[plen-3]) << 16);
1765 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1766 mode |= ALL_S_IEXEC;
1770 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1771 mode |= ALL_S_IWRITE;
1773 buf->st_mode = mode;
1775 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1776 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1778 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1779 buf->st_mtime = buf->st_ctime = dw;
1780 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1781 (long)(buf->st_size >> 32),(long)buf->st_size,
1782 buf->st_atime,buf->st_mtime, buf->st_ctime);
1786 /*********************************************************************
1789 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1791 struct MSVCRT__stati64 bufi64;
1793 ret = MSVCRT__stati64( path, &bufi64);
1795 msvcrt_cp_from_stati64(&bufi64, buf);
1799 /*********************************************************************
1800 * _wstati64 (MSVCRT.@)
1802 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1805 WIN32_FILE_ATTRIBUTE_DATA hfi;
1806 unsigned short mode = ALL_S_IREAD;
1809 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1811 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1813 TRACE("failed (%ld)\n",GetLastError());
1814 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1818 memset(buf,0,sizeof(struct MSVCRT__stat));
1820 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1821 if (MSVCRT_iswalpha(*path))
1822 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1824 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1826 plen = strlenW(path);
1828 /* Dir, or regular file? */
1829 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1830 (path[plen-1] == '\\'))
1831 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1834 mode |= MSVCRT__S_IFREG;
1836 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1838 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1839 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1840 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1841 mode |= ALL_S_IEXEC;
1845 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1846 mode |= ALL_S_IWRITE;
1848 buf->st_mode = mode;
1850 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1851 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1853 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1854 buf->st_mtime = buf->st_ctime = dw;
1855 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1856 (long)(buf->st_size >> 32),(long)buf->st_size,
1857 buf->st_atime,buf->st_mtime, buf->st_ctime);
1861 /*********************************************************************
1864 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1867 struct MSVCRT__stati64 bufi64;
1869 ret = MSVCRT__wstati64( path, &bufi64 );
1870 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1874 /*********************************************************************
1879 return _lseek(fd, 0, SEEK_CUR);
1882 /*********************************************************************
1883 * _telli64 (MSVCRT.@)
1885 __int64 _telli64(int fd)
1887 return _lseeki64(fd, 0, SEEK_CUR);
1890 /*********************************************************************
1891 * _tempnam (MSVCRT.@)
1893 char *_tempnam(const char *dir, const char *prefix)
1895 char tmpbuf[MAX_PATH];
1897 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1898 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1900 TRACE("got name (%s)\n",tmpbuf);
1901 DeleteFileA(tmpbuf);
1902 return _strdup(tmpbuf);
1904 TRACE("failed (%ld)\n",GetLastError());
1908 /*********************************************************************
1909 * _wtempnam (MSVCRT.@)
1911 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1913 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1915 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1916 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1918 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1919 DeleteFileW(tmpbuf);
1920 return _wcsdup(tmpbuf);
1922 TRACE("failed (%ld)\n",GetLastError());
1926 /*********************************************************************
1929 int _umask(int umask)
1931 int old_umask = MSVCRT_umask;
1932 TRACE("(%d)\n",umask);
1933 MSVCRT_umask = umask;
1937 /*********************************************************************
1940 int _utime(const char* path, struct MSVCRT__utimbuf *t)
1942 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1946 int retVal = _futime(fd, t);
1953 /*********************************************************************
1954 * _wutime (MSVCRT.@)
1956 int _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1958 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1962 int retVal = _futime(fd, t);
1969 /*********************************************************************
1972 int _write(int fd, const void* buf, unsigned int count)
1975 HANDLE hand = msvcrt_fdtoh(fd);
1977 /* Don't trace small writes, it gets *very* annoying */
1980 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1982 if (hand == INVALID_HANDLE_VALUE)
1984 *MSVCRT__errno() = MSVCRT_EBADF;
1988 /* If appending, go to EOF */
1989 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
1990 _lseek(fd, 0, FILE_END);
1992 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
1994 if (WriteFile(hand, buf, count, &num_written, NULL)
1995 && (num_written == count))
1997 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd,
1998 hand, GetLastError());
1999 *MSVCRT__errno() = MSVCRT_ENOSPC;
2003 unsigned int i, j, nr_lf;
2004 char *s =(char*)buf, *buf_start=(char*)buf, *p;
2005 /* find number of \n ( without preceding \r ) */
2006 for ( nr_lf=0,i = 0; i <count; i++)
2011 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2016 if ((p = MSVCRT_malloc(count + nr_lf)))
2018 for(s=(char*)buf, i=0, j=0; i<count; i++)
2023 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2030 FIXME("Malloc failed\n");
2038 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2040 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld), num_written %ld\n",
2041 fd, hand, GetLastError(), num_written);
2042 *MSVCRT__errno() = MSVCRT_ENOSPC;
2045 return s - buf_start;
2057 /*********************************************************************
2060 int MSVCRT__putw(int val, MSVCRT_FILE* file)
2063 len = _write(file->_file, &val, sizeof(val));
2064 if (len == sizeof(val)) return val;
2065 file->_flag |= MSVCRT__IOERR;
2069 /*********************************************************************
2072 int MSVCRT_fclose(MSVCRT_FILE* file)
2077 if (file->_tmpfname)
2079 MSVCRT_free(file->_tmpfname);
2080 file->_tmpfname = NULL;
2082 /* flush stdio buffers */
2083 if(file->_flag & MSVCRT__IOWRT)
2084 MSVCRT_fflush(file);
2085 if(file->_flag & MSVCRT__IOMYBUF)
2086 MSVCRT_free(file->_base);
2088 r=_close(file->_file);
2092 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2095 /*********************************************************************
2098 int MSVCRT_feof(MSVCRT_FILE* file)
2100 return file->_flag & MSVCRT__IOEOF;
2103 /*********************************************************************
2106 int MSVCRT_ferror(MSVCRT_FILE* file)
2108 return file->_flag & MSVCRT__IOERR;
2111 /*********************************************************************
2112 * _filbuf (MSVCRT.@)
2114 int MSVCRT__filbuf(MSVCRT_FILE* file)
2116 /* Allocate buffer if needed */
2117 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2118 msvcrt_alloc_buffer(file);
2120 if(!(file->_flag & MSVCRT__IOREAD)) {
2121 if(file->_flag & MSVCRT__IORW) {
2122 file->_flag |= MSVCRT__IOREAD;
2127 if(file->_flag & MSVCRT__IONBF) {
2130 if ((r = _read(file->_file,&c,1)) != 1) {
2131 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2136 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2138 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2143 file->_ptr = file->_base+1;
2144 return *(unsigned char *)file->_base;
2148 /*********************************************************************
2151 int MSVCRT_fgetc(MSVCRT_FILE* file)
2155 return *(unsigned char *)file->_ptr++;
2157 return MSVCRT__filbuf(file);
2161 /*********************************************************************
2162 * _fgetchar (MSVCRT.@)
2166 return MSVCRT_fgetc(MSVCRT_stdin);
2169 /*********************************************************************
2172 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2174 int cc = MSVCRT_EOF;
2175 char * buf_start = s;
2177 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2178 file,file->_file,s,size);
2180 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2185 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2187 TRACE(":nothing read\n");
2190 if ((cc != MSVCRT_EOF) && (size > 1))
2193 TRACE(":got %s\n", debugstr_a(buf_start));
2197 /*********************************************************************
2200 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2201 * the CR from CR/LF combinations
2203 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
2207 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2211 if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
2213 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2218 c = MSVCRT_fgetc(file);
2219 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2221 FIXME("Treat Multibyte characters\n");
2223 if (c == MSVCRT_EOF)
2226 return (MSVCRT_wint_t)c;
2229 /*********************************************************************
2232 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
2234 return MSVCRT_fgetwc(file);
2237 /*********************************************************************
2238 * _fgetwchar (MSVCRT.@)
2240 MSVCRT_wint_t _fgetwchar(void)
2242 return MSVCRT_fgetwc(MSVCRT_stdin);
2245 /*********************************************************************
2246 * getwchar (MSVCRT.@)
2248 MSVCRT_wint_t MSVCRT_getwchar(void)
2250 return _fgetwchar();
2253 /*********************************************************************
2256 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2258 int cc = MSVCRT_WEOF;
2259 MSVCRT_wchar_t * buf_start = s;
2261 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2262 file,file->_file,s,size);
2264 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2269 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2271 TRACE(":nothing read\n");
2274 if ((cc != MSVCRT_WEOF) && (size > 1))
2277 TRACE(":got %s\n", debugstr_w(buf_start));
2281 /*********************************************************************
2284 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2286 MSVCRT_size_t wrcnt=size * nmemb;
2291 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2292 memcpy(file->_ptr, ptr, pcnt);
2297 ptr = (const char*)ptr + pcnt;
2298 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2299 if(file->_flag & MSVCRT__IORW) {
2300 file->_flag |= MSVCRT__IOWRT;
2306 int res=msvcrt_flush_buffer(file);
2308 int pwritten = _write(file->_file, ptr, wrcnt);
2311 file->_flag |= MSVCRT__IOERR;
2314 written += pwritten;
2317 return written / size;
2320 /*********************************************************************
2323 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2325 MSVCRT_wchar_t mwc=wc;
2326 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2331 /*********************************************************************
2332 * _fputwchar (MSVCRT.@)
2334 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
2336 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2339 /*********************************************************************
2342 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
2345 int open_flags, stream_flags, fd;
2347 TRACE("(%s,%s)\n",path,mode);
2349 /* map mode string to open() flags. "man fopen" for possibilities. */
2350 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2354 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2357 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2359 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2366 TRACE(":got (%p)\n",file);
2367 if (fd >= 0 && !file)
2373 /*********************************************************************
2374 * _wfopen (MSVCRT.@)
2376 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2378 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2379 char *patha = MSVCRT_calloc(plen + 1, 1);
2380 char *modea = MSVCRT_calloc(mlen + 1, 1);
2382 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2384 if (patha && modea &&
2385 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2386 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2388 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2394 msvcrt_set_errno(GetLastError());
2398 /*********************************************************************
2399 * _fsopen (MSVCRT.@)
2401 MSVCRT_FILE* MSVCRT__fsopen(const char *path, const char *mode, int share)
2403 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2404 return MSVCRT_fopen(path,mode);
2407 /*********************************************************************
2408 * _wfsopen (MSVCRT.@)
2410 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2412 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2413 debugstr_w(path),debugstr_w(mode),share);
2414 return MSVCRT__wfopen(path,mode);
2417 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2418 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2420 /*********************************************************************
2423 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2430 int res = msvcrt_flush_buffer(file);
2431 return res ? res : c;
2436 return MSVCRT__flsbuf(c, file);
2440 /*********************************************************************
2441 * _flsbuf (MSVCRT.@)
2443 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2445 /* Flush output buffer */
2446 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2447 msvcrt_alloc_buffer(file);
2449 if(!(file->_flag & MSVCRT__IOWRT)) {
2450 if(file->_flag & MSVCRT__IORW) {
2451 file->_flag |= MSVCRT__IOWRT;
2457 int res=msvcrt_flush_buffer(file);
2458 return res?res : MSVCRT_fputc(c, file);
2462 len = _write(file->_file, &cc, 1);
2463 if (len == 1) return c;
2464 file->_flag |= MSVCRT__IOERR;
2469 /*********************************************************************
2470 * _fputchar (MSVCRT.@)
2472 int _fputchar(int c)
2474 return MSVCRT_fputc(c, MSVCRT_stdout);
2477 /*********************************************************************
2480 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2481 { MSVCRT_size_t rcnt=size * nmemb;
2482 MSVCRT_size_t read=0;
2488 /* first buffered data */
2490 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2491 memcpy(ptr, file->_ptr, pcnt);
2496 ptr = (char*)ptr + pcnt;
2497 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2498 if(file->_flag & MSVCRT__IORW) {
2499 file->_flag |= MSVCRT__IOREAD;
2505 pread = _read(file->_file,ptr, rcnt);
2506 /* expose feof condition in the flags
2507 * MFC tests file->_flag for feof, and doesn't not call feof())
2510 file->_flag |= MSVCRT__IOEOF;
2511 else if (pread == -1)
2513 file->_flag |= MSVCRT__IOERR;
2521 /*********************************************************************
2522 * freopen (MSVCRT.@)
2525 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2527 int open_flags, stream_flags, fd;
2529 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2532 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2536 MSVCRT_fclose(file);
2537 /* map mode string to open() flags. "man fopen" for possibilities. */
2538 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2542 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2545 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2548 WARN(":failed-last error (%ld)\n",GetLastError());
2549 msvcrt_set_errno(GetLastError());
2558 /*********************************************************************
2559 * fsetpos (MSVCRT.@)
2561 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2563 /* Note that all this has been lifted 'as is' from fseek */
2564 if(file->_flag & MSVCRT__IOWRT)
2565 msvcrt_flush_buffer(file);
2567 /* Discard buffered input */
2569 file->_ptr = file->_base;
2571 /* Reset direction of i/o */
2572 if(file->_flag & MSVCRT__IORW) {
2573 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2576 return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2579 /*********************************************************************
2582 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2587 if( file->_flag & MSVCRT__IOWRT ) {
2588 off = file->_ptr - file->_base;
2593 pos = _tell(file->_file);
2594 if(pos == -1) return pos;
2598 /*********************************************************************
2599 * fgetpos (MSVCRT.@)
2601 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2603 /* This code has been lifted form the MSVCRT_ftell function */
2606 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2608 if (*pos == -1) return -1;
2611 if( file->_flag & MSVCRT__IOWRT ) {
2612 off = file->_ptr - file->_base;
2622 /*********************************************************************
2625 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2627 size_t i, len = strlen(s);
2628 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2629 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2630 for (i=0; i<len; i++)
2631 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2636 /*********************************************************************
2639 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2641 size_t i, len = strlenW(s);
2642 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2643 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2644 for (i=0; i<len; i++)
2646 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2648 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2654 /*********************************************************************
2655 * getchar (MSVCRT.@)
2657 int MSVCRT_getchar(void)
2659 return MSVCRT_fgetc(MSVCRT_stdin);
2662 /*********************************************************************
2665 int MSVCRT_getc(MSVCRT_FILE* file)
2667 return MSVCRT_fgetc(file);
2670 /*********************************************************************
2673 char *MSVCRT_gets(char *buf)
2676 char * buf_start = buf;
2678 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2679 cc = MSVCRT_fgetc(MSVCRT_stdin))
2680 if(cc != '\r') *buf++ = (char)cc;
2684 TRACE("got '%s'\n", buf_start);
2688 /*********************************************************************
2691 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2694 MSVCRT_wchar_t* ws = buf;
2696 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2697 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2700 *buf++ = (MSVCRT_wchar_t)cc;
2704 TRACE("got '%s'\n", debugstr_w(ws));
2708 /*********************************************************************
2711 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2713 return MSVCRT_fputc(c, file);
2716 /*********************************************************************
2717 * putchar (MSVCRT.@)
2719 int MSVCRT_putchar(int c)
2721 return MSVCRT_fputc(c, MSVCRT_stdout);
2724 /*********************************************************************
2727 int MSVCRT_puts(const char *s)
2729 size_t len = strlen(s);
2730 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2731 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2734 /*********************************************************************
2737 int _putws(const MSVCRT_wchar_t *s)
2739 static const MSVCRT_wchar_t nl = '\n';
2740 size_t len = strlenW(s);
2741 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2742 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2745 /*********************************************************************
2748 int MSVCRT_remove(const char *path)
2750 TRACE("(%s)\n",path);
2751 if (DeleteFileA(path))
2753 TRACE(":failed (%ld)\n",GetLastError());
2754 msvcrt_set_errno(GetLastError());
2758 /*********************************************************************
2759 * _wremove (MSVCRT.@)
2761 int _wremove(const MSVCRT_wchar_t *path)
2763 TRACE("(%s)\n",debugstr_w(path));
2764 if (DeleteFileW(path))
2766 TRACE(":failed (%ld)\n",GetLastError());
2767 msvcrt_set_errno(GetLastError());
2771 /*********************************************************************
2774 int MSVCRT_rename(const char *oldpath,const char *newpath)
2776 TRACE(":from %s to %s\n",oldpath,newpath);
2777 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2779 TRACE(":failed (%ld)\n",GetLastError());
2780 msvcrt_set_errno(GetLastError());
2784 /*********************************************************************
2785 * _wrename (MSVCRT.@)
2787 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2789 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2790 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2792 TRACE(":failed (%ld)\n",GetLastError());
2793 msvcrt_set_errno(GetLastError());
2797 /*********************************************************************
2798 * setvbuf (MSVCRT.@)
2800 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2802 /* TODO: Check if file busy */
2804 MSVCRT_free(file->_base);
2808 if(mode == MSVCRT__IOFBF) {
2809 file->_flag &= ~MSVCRT__IONBF;
2810 file->_base = file->_ptr = buf;
2812 file->_bufsiz = size;
2815 file->_flag |= MSVCRT__IONBF;
2820 /*********************************************************************
2823 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2825 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2828 /*********************************************************************
2831 char *MSVCRT_tmpnam(char *s)
2839 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2840 p = s + sprintf(s, "\\s%s.", tmpstr);
2841 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2843 msvcrt_int_to_base32(unique++, tmpstr);
2845 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2846 GetLastError() == ERROR_FILE_NOT_FOUND)
2852 /*********************************************************************
2853 * tmpfile (MSVCRT.@)
2855 MSVCRT_FILE* MSVCRT_tmpfile(void)
2857 char *filename = MSVCRT_tmpnam(NULL);
2859 MSVCRT_FILE* file = NULL;
2862 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2863 if (fd != -1 && (file = msvcrt_alloc_fp()))
2865 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2870 else file->_tmpfname = _strdup(filename);
2876 /*********************************************************************
2877 * vfprintf (MSVCRT.@)
2879 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2881 char buf[2048], *mem = buf;
2882 int written, resize = sizeof(buf), retval;
2883 /* There are two conventions for vsnprintf failing:
2884 * Return -1 if we truncated, or
2885 * Return the number of bytes that would have been written
2886 * The code below handles both cases
2888 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
2891 resize = (written == -1 ? resize * 2 : written + 1);
2894 if (!(mem = (char *)MSVCRT_malloc(resize)))
2897 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2903 /*********************************************************************
2904 * vfwprintf (MSVCRT.@)
2906 * Is final char included in written (then resize is too big) or not
2907 * (then we must test for equality too)?
2909 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2911 MSVCRT_wchar_t buf[2048], *mem = buf;
2912 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2913 /* See vfprintf comments */
2914 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2917 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2920 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2923 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2929 /*********************************************************************
2930 * vprintf (MSVCRT.@)
2932 int MSVCRT_vprintf(const char *format, va_list valist)
2934 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2937 /*********************************************************************
2938 * vwprintf (MSVCRT.@)
2940 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2942 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2945 /*********************************************************************
2946 * fprintf (MSVCRT.@)
2948 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2952 va_start(valist, format);
2953 res = MSVCRT_vfprintf(file, format, valist);
2958 /*********************************************************************
2959 * fwprintf (MSVCRT.@)
2961 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2965 va_start(valist, format);
2966 res = MSVCRT_vfwprintf(file, format, valist);
2971 /*********************************************************************
2974 int MSVCRT_printf(const char *format, ...)
2978 va_start(valist, format);
2979 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2984 /*********************************************************************
2987 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2989 if (c == MSVCRT_EOF)
2991 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2992 msvcrt_alloc_buffer(file);
2995 if(file->_ptr>file->_base) {
2999 MSVCRT_clearerr(file);
3005 /*********************************************************************
3006 * ungetwc (MSVCRT.@)
3008 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3010 MSVCRT_wchar_t mwc = wc;
3011 char * pp = (char *)&mwc;
3013 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3014 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3020 /*********************************************************************
3021 * wprintf (MSVCRT.@)
3023 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3027 va_start(valist, format);
3028 res = MSVCRT_vwprintf(format, valist);
3033 /*********************************************************************
3034 * __pioinfo (MSVCRT.@)
3035 * FIXME: see MSVCRT_MAX_FILES define.
3037 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3038 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3039 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3040 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3041 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3042 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3043 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3044 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3045 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3046 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3047 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3048 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3051 /*********************************************************************
3052 * __badioinfo (MSVCRT.@)
3054 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };