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
27 #include "wine/port.h"
35 #include <sys/types.h>
43 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
49 /* for stat mode, permissions apply to all,owner and group */
50 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
51 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
52 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
54 /* _access() bit flags FIXME: incomplete */
55 #define MSVCRT_W_OK 0x02
57 /* values for wxflag in file descriptor */
60 #define WX_DONTINHERIT 0x10
61 #define WX_APPEND 0x20
64 /* FIXME: this should be allocated dynamically */
65 #define MSVCRT_MAX_FILES 2048
70 DWORD unkn[7]; /* critical section and init flag */
73 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
75 MSVCRT_FILE MSVCRT__iob[3];
77 static int MSVCRT_fdstart = 3; /* first unallocated fd */
78 static int MSVCRT_fdend = 3; /* highest allocated fd */
80 static MSVCRT_FILE* MSVCRT_fstreams[2048];
81 static int MSVCRT_stream_idx;
83 /* INTERNAL: process umask */
84 static int MSVCRT_umask = 0;
86 /* INTERNAL: Static buffer for temp file name */
87 static char MSVCRT_tmpname[MAX_PATH];
89 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
90 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
91 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
92 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
94 #define TOUL(x) (ULONGLONG)(x)
95 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
96 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
97 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
98 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
100 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
101 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
102 * and MSVCRT_stream_idx, from race conditions.
103 * It doesn't protect against race conditions manipulating the underlying files
104 * or flags; doing so would probably be better accomplished with per-file
105 * protection, rather than locking the whole table for every change.
107 static CRITICAL_SECTION MSVCRT_file_cs;
108 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
109 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
111 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64 *bufi64, struct MSVCRT__stat *buf)
113 buf->st_dev = bufi64->st_dev;
114 buf->st_ino = bufi64->st_ino;
115 buf->st_mode = bufi64->st_mode;
116 buf->st_nlink = bufi64->st_nlink;
117 buf->st_uid = bufi64->st_uid;
118 buf->st_gid = bufi64->st_gid;
119 buf->st_rdev = bufi64->st_rdev;
120 buf->st_size = bufi64->st_size;
121 buf->st_atime = bufi64->st_atime;
122 buf->st_mtime = bufi64->st_mtime;
123 buf->st_ctime = bufi64->st_ctime;
126 static inline BOOL msvcrt_is_valid_fd(int fd)
128 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
131 /* INTERNAL: Get the HANDLE for a fd
132 * This doesn't lock the table, because a failure will result in
133 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
134 * it returns a valid handle which is about to be closed, a subsequent call
135 * will fail, most likely in a sane way.
137 static HANDLE msvcrt_fdtoh(int fd)
139 if (!msvcrt_is_valid_fd(fd))
141 WARN(":fd (%d) - no handle!\n",fd);
142 *MSVCRT___doserrno() = 0;
143 *MSVCRT__errno() = MSVCRT_EBADF;
144 return INVALID_HANDLE_VALUE;
146 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
147 return MSVCRT_fdesc[fd].handle;
150 /* INTERNAL: free a file entry fd */
151 static void msvcrt_free_fd(int fd)
154 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
155 MSVCRT_fdesc[fd].wxflag = 0;
156 TRACE(":fd (%d) freed\n",fd);
157 if (fd < 3) /* don't use 0,1,2 for user files */
161 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
162 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
163 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
168 if (fd == MSVCRT_fdend - 1)
170 if (fd < MSVCRT_fdstart)
176 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
177 /* caller must hold the files lock */
178 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
180 if (fd >= MSVCRT_MAX_FILES)
182 WARN(":files exhausted!\n");
185 MSVCRT_fdesc[fd].handle = hand;
186 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
188 /* locate next free slot */
189 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
190 MSVCRT_fdstart = MSVCRT_fdend + 1;
192 while (MSVCRT_fdstart < MSVCRT_fdend &&
193 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
195 /* update last fd in use */
196 if (fd >= MSVCRT_fdend)
197 MSVCRT_fdend = fd + 1;
198 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
202 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
203 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
204 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
210 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
211 static int msvcrt_alloc_fd(HANDLE hand, int flag)
216 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
217 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
222 /* INTERNAL: Allocate a FILE* for an fd slot */
223 /* caller must hold the files lock */
224 static MSVCRT_FILE* msvcrt_alloc_fp(void)
228 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
230 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
232 if (!MSVCRT_fstreams[i])
234 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
236 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
238 return MSVCRT_fstreams[i];
244 /* INTERNAL: initialize a FILE* from an open fd */
245 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
247 TRACE(":fd (%d) allocating FILE*\n",fd);
248 if (!msvcrt_is_valid_fd(fd))
250 WARN(":invalid fd %d\n",fd);
251 *MSVCRT___doserrno() = 0;
252 *MSVCRT__errno() = MSVCRT_EBADF;
255 memset(file, 0, sizeof(*file));
257 file->_flag = stream_flags;
259 TRACE(":got FILE* (%p)\n",file);
263 /* INTERNAL: Create an inheritance data block (for spawned process)
264 * The inheritance block is made of:
265 * 00 int nb of file descriptor (NBFD)
266 * 04 char file flags (wxflag): repeated for each fd
267 * 4+NBFD HANDLE file handle: repeated for each fd
269 unsigned msvcrt_create_io_inherit_block(STARTUPINFOA* si)
275 si->cbReserved2 = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
276 si->lpReserved2 = MSVCRT_calloc(si->cbReserved2, 1);
277 if (!si->lpReserved2)
282 wxflag_ptr = (char*)si->lpReserved2 + sizeof(unsigned);
283 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
285 *(unsigned*)si->lpReserved2 = MSVCRT_fdend;
286 for (fd = 0; fd < MSVCRT_fdend; fd++)
288 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
289 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
291 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
292 *handle_ptr = MSVCRT_fdesc[fd].handle;
297 *handle_ptr = INVALID_HANDLE_VALUE;
299 wxflag_ptr++; handle_ptr++;
304 /* INTERNAL: Set up all file descriptors,
305 * as well as default streams (stdin, stderr and stdout)
307 void msvcrt_init_io(void)
312 InitializeCriticalSection(&MSVCRT_file_cs);
313 GetStartupInfoA(&si);
314 if (si.cbReserved2 != 0 && si.lpReserved2 != NULL)
319 MSVCRT_fdend = *(unsigned*)si.lpReserved2;
321 wxflag_ptr = (char*)(si.lpReserved2 + sizeof(unsigned));
322 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
324 MSVCRT_fdend = min(MSVCRT_fdend, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
325 for (i = 0; i < MSVCRT_fdend; i++)
327 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
329 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
330 MSVCRT_fdesc[i].handle = *handle_ptr;
334 MSVCRT_fdesc[i].wxflag = 0;
335 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
337 wxflag_ptr++; handle_ptr++;
339 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
340 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
343 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
345 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
346 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, FALSE,
347 DUPLICATE_SAME_ACCESS);
348 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
350 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
352 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
353 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, FALSE,
354 DUPLICATE_SAME_ACCESS);
355 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
357 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
359 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
360 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, FALSE,
361 DUPLICATE_SAME_ACCESS);
362 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
365 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
366 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
368 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
369 for (i = 0; i < 3; i++)
371 /* FILE structs for stdin/out/err are static and never deleted */
372 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
373 MSVCRT__iob[i]._file = i;
374 MSVCRT__iob[i]._tmpfname = NULL;
375 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
377 MSVCRT_stream_idx = 3;
380 /* INTERNAL: Flush stdio file buffer */
381 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
384 int cnt=file->_ptr-file->_base;
385 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
386 file->_flag |= MSVCRT__IOERR;
389 file->_ptr=file->_base;
390 file->_cnt=file->_bufsiz;
395 /* INTERNAL: Allocate stdio file buffer */
396 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
398 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
400 file->_bufsiz = MSVCRT_BUFSIZ;
401 file->_flag |= MSVCRT__IOMYBUF;
403 file->_base = (unsigned char *)(&file->_charbuf);
405 file->_bufsiz = sizeof(file->_charbuf);
407 file->_ptr = file->_base;
411 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
412 static void msvcrt_int_to_base32(int num, char *str)
427 *p = (num & 31) + '0';
429 *p += ('a' - '0' - 10);
434 /*********************************************************************
437 MSVCRT_FILE *__p__iob(void)
439 return &MSVCRT__iob[0];
442 /*********************************************************************
445 int _access(const char *filename, int mode)
447 DWORD attr = GetFileAttributesA(filename);
449 TRACE("(%s,%d) %ld\n",filename,mode,attr);
451 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
453 msvcrt_set_errno(GetLastError());
456 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
458 msvcrt_set_errno(ERROR_ACCESS_DENIED);
464 /*********************************************************************
465 * _waccess (MSVCRT.@)
467 int _waccess(const MSVCRT_wchar_t *filename, int mode)
469 DWORD attr = GetFileAttributesW(filename);
471 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
473 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
475 msvcrt_set_errno(GetLastError());
478 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
480 msvcrt_set_errno(ERROR_ACCESS_DENIED);
486 /*********************************************************************
489 int _chmod(const char *path, int flags)
491 DWORD oldFlags = GetFileAttributesA(path);
493 if (oldFlags != INVALID_FILE_ATTRIBUTES)
495 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
496 oldFlags | FILE_ATTRIBUTE_READONLY;
498 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
501 msvcrt_set_errno(GetLastError());
505 /*********************************************************************
508 int _wchmod(const MSVCRT_wchar_t *path, int flags)
510 DWORD oldFlags = GetFileAttributesW(path);
512 if (oldFlags != INVALID_FILE_ATTRIBUTES)
514 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
515 oldFlags | FILE_ATTRIBUTE_READONLY;
517 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
520 msvcrt_set_errno(GetLastError());
524 /*********************************************************************
527 int _chsize(int fd, long size)
529 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
533 /*********************************************************************
536 int _unlink(const char *path)
538 TRACE("(%s)\n",path);
539 if(DeleteFileA(path))
541 TRACE("failed (%ld)\n",GetLastError());
542 msvcrt_set_errno(GetLastError());
546 /*********************************************************************
547 * _wunlink (MSVCRT.@)
549 int _wunlink(const MSVCRT_wchar_t *path)
551 TRACE("(%s)\n",debugstr_w(path));
552 if(DeleteFileW(path))
554 TRACE("failed (%ld)\n",GetLastError());
555 msvcrt_set_errno(GetLastError());
559 /* _flushall calls MSVCRT_fflush which calls _flushall */
560 int MSVCRT_fflush(MSVCRT_FILE* file);
562 /*********************************************************************
563 * _flushall (MSVCRT.@)
567 int i, num_flushed = 0;
570 for (i = 3; i < MSVCRT_stream_idx; i++)
571 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
574 /* FIXME: flush, do not commit */
575 if (_commit(i) == -1)
576 if (MSVCRT_fstreams[i])
577 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
579 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
580 MSVCRT_fflush(MSVCRT_fstreams[i]);
586 TRACE(":flushed (%d) handles\n",num_flushed);
590 /*********************************************************************
593 int MSVCRT_fflush(MSVCRT_FILE* file)
599 int res=msvcrt_flush_buffer(file);
604 /*********************************************************************
613 hand = msvcrt_fdtoh(fd);
614 TRACE(":fd (%d) handle (%p)\n",fd,hand);
615 if (hand == INVALID_HANDLE_VALUE)
617 else if (!CloseHandle(hand))
619 WARN(":failed-last error (%ld)\n",GetLastError());
620 msvcrt_set_errno(GetLastError());
633 /*********************************************************************
638 HANDLE hand = msvcrt_fdtoh(fd);
640 TRACE(":fd (%d) handle (%p)\n",fd,hand);
641 if (hand == INVALID_HANDLE_VALUE)
644 if (!FlushFileBuffers(hand))
646 if (GetLastError() == ERROR_INVALID_HANDLE)
648 /* FlushFileBuffers fails for console handles
649 * so we ignore this error.
653 TRACE(":failed-last error (%ld)\n",GetLastError());
654 msvcrt_set_errno(GetLastError());
661 /*********************************************************************
664 * MSDN isn't clear on this point, but the remarks for _pipe,
665 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
666 * indicate file descriptors duplicated with _dup and _dup2 are always
669 int _dup2(int od, int nd)
673 TRACE("(od=%d, nd=%d)\n", od, nd);
675 if (nd < MSVCRT_MAX_FILES && msvcrt_is_valid_fd(od))
679 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
680 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
682 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
684 if (msvcrt_is_valid_fd(nd))
686 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
690 *MSVCRT__errno() = MSVCRT_EMFILE;
694 /* _dup2 returns 0, not nd, on success */
701 msvcrt_set_errno(GetLastError());
706 *MSVCRT__errno() = MSVCRT_EBADF;
713 /*********************************************************************
722 if (_dup2(od, fd) == 0)
730 /*********************************************************************
735 DWORD curpos,endpos,hcurpos,hendpos;
736 HANDLE hand = msvcrt_fdtoh(fd);
738 TRACE(":fd (%d) handle (%p)\n",fd,hand);
740 if (hand == INVALID_HANDLE_VALUE)
743 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
745 /* Otherwise we do it the hard way */
746 hcurpos = hendpos = 0;
747 curpos = SetFilePointer(hand, 0, &hcurpos, SEEK_CUR);
748 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
750 if (curpos == endpos && hcurpos == hendpos)
752 /* FIXME: shouldn't WX_ATEOF be set here? */
756 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
760 /*********************************************************************
761 * _fcloseall (MSVCRT.@)
763 int MSVCRT__fcloseall(void)
765 int num_closed = 0, i;
768 for (i = 3; i < MSVCRT_stream_idx; i++)
769 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
770 MSVCRT_fclose(MSVCRT_fstreams[i]))
774 TRACE(":closed (%d) handles\n",num_closed);
778 /* free everything on process exit */
779 void msvcrt_free_io(void)
785 DeleteCriticalSection(&MSVCRT_file_cs);
788 /*********************************************************************
789 * _lseeki64 (MSVCRT.@)
791 __int64 _lseeki64(int fd, __int64 offset, int whence)
793 DWORD ret, hoffset = (DWORD) (offset >> 32);
794 HANDLE hand = msvcrt_fdtoh(fd);
796 TRACE(":fd (%d) handle (%p)\n",fd,hand);
797 if (hand == INVALID_HANDLE_VALUE)
800 if (whence < 0 || whence > 2)
802 *MSVCRT__errno() = MSVCRT_EINVAL;
806 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
807 fd,hoffset,(long)offset,
808 (whence==SEEK_SET)?"SEEK_SET":
809 (whence==SEEK_CUR)?"SEEK_CUR":
810 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
812 ret = SetFilePointer(hand, (long)offset, &hoffset, whence);
813 if (ret != INVALID_SET_FILE_POINTER || !GetLastError())
815 MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
816 /* FIXME: What if we seek _to_ EOF - is EOF set? */
818 return ((__int64)hoffset << 32) | ret;
820 TRACE(":error-last error (%ld)\n",GetLastError());
821 msvcrt_set_errno(GetLastError());
825 /*********************************************************************
828 LONG _lseek(int fd, LONG offset, int whence)
830 return _lseeki64(fd, offset, whence);
833 /*********************************************************************
834 * _locking (MSVCRT.@)
836 * This is untested; the underlying LockFile doesn't work yet.
838 int _locking(int fd, int mode, LONG nbytes)
842 HANDLE hand = msvcrt_fdtoh(fd);
844 TRACE(":fd (%d) handle (%p)\n",fd,hand);
845 if (hand == INVALID_HANDLE_VALUE)
848 if (mode < 0 || mode > 4)
850 *MSVCRT__errno() = MSVCRT_EINVAL;
854 TRACE(":fd (%d) by 0x%08lx mode %s\n",
855 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
856 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
857 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
858 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
859 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
862 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
864 FIXME ("Seek failed\n");
865 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
868 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
871 ret = 1; /* just to satisfy gcc */
874 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
879 else if (mode == MSVCRT__LK_UNLCK)
880 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
882 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
883 /* FIXME - what about error settings? */
887 /*********************************************************************
890 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
892 /* Flush output if needed */
893 if(file->_flag & MSVCRT__IOWRT)
894 msvcrt_flush_buffer(file);
896 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
897 offset -= file->_cnt;
899 /* Discard buffered input */
901 file->_ptr = file->_base;
902 /* Reset direction of i/o */
903 if(file->_flag & MSVCRT__IORW) {
904 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
906 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
909 /*********************************************************************
910 * clearerr (MSVCRT.@)
912 void MSVCRT_clearerr(MSVCRT_FILE* file)
914 TRACE(":file (%p) fd (%d)\n",file,file->_file);
915 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
918 /*********************************************************************
921 void MSVCRT_rewind(MSVCRT_FILE* file)
923 TRACE(":file (%p) fd (%d)\n",file,file->_file);
924 MSVCRT_fseek(file, 0L, SEEK_SET);
925 MSVCRT_clearerr(file);
928 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
930 int plus = strchr(mode, '+') != NULL;
935 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
936 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
939 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
940 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
943 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
944 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
954 *open_flags |= MSVCRT__O_BINARY;
955 *open_flags &= ~MSVCRT__O_TEXT;
958 *open_flags |= MSVCRT__O_TEXT;
959 *open_flags &= ~MSVCRT__O_BINARY;
964 FIXME(":unknown flag %c not supported\n",mode[-1]);
969 /*********************************************************************
972 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
974 int open_flags, stream_flags;
977 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
980 if (!(file = msvcrt_alloc_fp()))
982 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
987 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
993 /*********************************************************************
994 * _wfdopen (MSVCRT.@)
996 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
998 unsigned mlen = strlenW(mode);
999 char *modea = MSVCRT_calloc(mlen + 1, 1);
1000 MSVCRT_FILE* file = NULL;
1001 int open_flags, stream_flags;
1004 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1006 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1008 if (!(file = msvcrt_alloc_fp()))
1010 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1018 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1019 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1026 /*********************************************************************
1027 * _filelength (MSVCRT.@)
1029 LONG _filelength(int fd)
1031 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1034 LONG endPos = _lseek(fd, 0, SEEK_END);
1037 if (endPos != curPos)
1038 _lseek(fd, curPos, SEEK_SET);
1045 /*********************************************************************
1046 * _filelengthi64 (MSVCRT.@)
1048 __int64 _filelengthi64(int fd)
1050 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1053 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1056 if (endPos != curPos)
1057 _lseeki64(fd, curPos, SEEK_SET);
1064 /*********************************************************************
1065 * _fileno (MSVCRT.@)
1067 int MSVCRT__fileno(MSVCRT_FILE* file)
1069 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1073 /*********************************************************************
1074 * _fstati64 (MSVCRT.@)
1076 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1079 BY_HANDLE_FILE_INFORMATION hfi;
1080 HANDLE hand = msvcrt_fdtoh(fd);
1082 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1083 if (hand == INVALID_HANDLE_VALUE)
1088 WARN(":failed-NULL buf\n");
1089 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1093 memset(&hfi, 0, sizeof(hfi));
1094 memset(buf, 0, sizeof(struct MSVCRT__stati64));
1095 if (!GetFileInformationByHandle(hand, &hfi))
1097 WARN(":failed-last error (%ld)\n",GetLastError());
1098 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1101 dw = GetFileType(hand);
1102 buf->st_mode = S_IREAD;
1103 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1104 buf->st_mode |= S_IWRITE;
1105 /* interestingly, Windows never seems to set S_IFDIR */
1106 if (dw == FILE_TYPE_CHAR)
1107 buf->st_mode |= S_IFCHR;
1108 else if (dw == FILE_TYPE_PIPE)
1109 buf->st_mode |= S_IFIFO;
1111 buf->st_mode |= S_IFREG;
1112 TRACE(":dwFileAttributes = 0x%lx, mode set to 0x%x\n",hfi.dwFileAttributes,
1114 buf->st_nlink = hfi.nNumberOfLinks;
1115 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1116 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1118 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1119 buf->st_mtime = buf->st_ctime = dw;
1123 /*********************************************************************
1126 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1128 struct MSVCRT__stati64 bufi64;
1130 ret = MSVCRT__fstati64(fd, &bufi64);
1132 msvcrt_cp_from_stati64(&bufi64, buf);
1136 /*********************************************************************
1137 * _futime (MSVCRT.@)
1139 int _futime(int fd, struct MSVCRT__utimbuf *t)
1141 HANDLE hand = msvcrt_fdtoh(fd);
1146 MSVCRT_time_t currTime;
1147 MSVCRT_time(&currTime);
1148 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1149 memcpy(&wt, &at, sizeof(wt));
1153 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1154 if (t->actime == t->modtime)
1155 memcpy(&wt, &at, sizeof(wt));
1157 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1160 if (!SetFileTime(hand, NULL, &at, &wt))
1162 msvcrt_set_errno(GetLastError());
1168 /*********************************************************************
1169 * _get_osfhandle (MSVCRT.@)
1171 long _get_osfhandle(int fd)
1173 HANDLE hand = msvcrt_fdtoh(fd);
1174 HANDLE newhand = hand;
1175 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1177 if (hand != INVALID_HANDLE_VALUE)
1179 /* FIXME: I'm not convinced that I should be copying the
1180 * handle here - it may be leaked if the app doesn't
1181 * close it (and the API docs don't say that it should)
1182 * Not duplicating it means that it can't be inherited
1183 * and so lcc's wedit doesn't cope when it passes it to
1184 * child processes. I've an idea that it should either
1185 * be copied by CreateProcess, or marked as inheritable
1186 * when initialised, or maybe both? JG 21-9-00.
1188 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
1189 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
1191 return (long)newhand;
1194 /*********************************************************************
1195 * _isatty (MSVCRT.@)
1199 HANDLE hand = msvcrt_fdtoh(fd);
1201 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1202 if (hand == INVALID_HANDLE_VALUE)
1205 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1208 /*********************************************************************
1209 * _mktemp (MSVCRT.@)
1211 char *_mktemp(char *pattern)
1214 char *retVal = pattern;
1219 numX = (*pattern++ == 'X')? numX + 1 : 0;
1223 id = GetCurrentProcessId();
1227 int tempNum = id / 10;
1228 *pattern-- = id - (tempNum * 10) + '0';
1234 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1235 GetLastError() == ERROR_FILE_NOT_FOUND)
1237 *pattern = letter++;
1238 } while(letter != '|');
1242 /*********************************************************************
1243 * _wmktemp (MSVCRT.@)
1245 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
1248 MSVCRT_wchar_t *retVal = pattern;
1250 MSVCRT_wchar_t letter = 'a';
1253 numX = (*pattern++ == 'X')? numX + 1 : 0;
1257 id = GetCurrentProcessId();
1261 int tempNum = id / 10;
1262 *pattern-- = id - (tempNum * 10) + '0';
1268 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1269 GetLastError() == ERROR_FILE_NOT_FOUND)
1271 *pattern = letter++;
1272 } while(letter != '|');
1276 static unsigned split_oflags(unsigned oflags)
1280 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1281 if (oflags & MSVCRT__O_BINARY) ;
1282 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1283 else if (*__p__fmode() & MSVCRT__O_BINARY) ;
1284 else wxflags |= WX_TEXT; /* default to TEXT*/
1285 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1287 if (oflags & ~(MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|MSVCRT__O_TRUNC|
1288 MSVCRT__O_EXCL|MSVCRT__O_CREAT|MSVCRT__O_RDWR|MSVCRT__O_WRONLY|
1289 MSVCRT__O_TEMPORARY|MSVCRT__O_NOINHERIT))
1290 ERR(":unsupported oflags 0x%04x\n",oflags);
1295 /*********************************************************************
1298 int _pipe(int *pfds, unsigned int psize, int textmode)
1301 SECURITY_ATTRIBUTES sa;
1302 HANDLE readHandle, writeHandle;
1306 *MSVCRT__errno() = MSVCRT_EINVAL;
1310 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1311 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1312 sa.lpSecurityDescriptor = NULL;
1313 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1315 unsigned int wxflags = split_oflags(textmode);
1319 fd = msvcrt_alloc_fd(readHandle, wxflags);
1323 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1332 CloseHandle(writeHandle);
1333 *MSVCRT__errno() = MSVCRT_EMFILE;
1338 CloseHandle(readHandle);
1339 CloseHandle(writeHandle);
1340 *MSVCRT__errno() = MSVCRT_EMFILE;
1345 msvcrt_set_errno(GetLastError());
1350 /*********************************************************************
1353 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1357 DWORD access = 0, creation = 0, attrib;
1361 SECURITY_ATTRIBUTES sa;
1364 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1365 path, oflags, shflags);
1367 wxflag = split_oflags(oflags);
1368 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1370 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1371 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1372 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1375 if (oflags & MSVCRT__O_CREAT)
1377 va_start(ap, shflags);
1378 pmode = va_arg(ap, int);
1381 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1382 FIXME(": pmode 0x%04x ignored\n", pmode);
1384 WARN(": pmode 0x%04x ignored\n", pmode);
1386 if (oflags & MSVCRT__O_EXCL)
1387 creation = CREATE_NEW;
1388 else if (oflags & MSVCRT__O_TRUNC)
1389 creation = CREATE_ALWAYS;
1391 creation = OPEN_ALWAYS;
1393 else /* no MSVCRT__O_CREAT */
1395 if (oflags & MSVCRT__O_TRUNC)
1396 creation = TRUNCATE_EXISTING;
1398 creation = OPEN_EXISTING;
1403 case MSVCRT__SH_DENYRW:
1406 case MSVCRT__SH_DENYWR:
1407 sharing = FILE_SHARE_READ;
1409 case MSVCRT__SH_DENYRD:
1410 sharing = FILE_SHARE_WRITE;
1412 case MSVCRT__SH_DENYNO:
1413 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1416 ERR( "Unhandled shflags 0x%x\n", shflags );
1419 attrib = FILE_ATTRIBUTE_NORMAL;
1421 if (oflags & MSVCRT__O_TEMPORARY)
1423 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1425 sharing |= FILE_SHARE_DELETE;
1428 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1429 sa.lpSecurityDescriptor = NULL;
1430 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1432 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1434 if (hand == INVALID_HANDLE_VALUE) {
1435 WARN(":failed-last error (%ld)\n",GetLastError());
1436 msvcrt_set_errno(GetLastError());
1440 fd = msvcrt_alloc_fd(hand, wxflag);
1442 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1446 if (oflags & MSVCRT__O_APPEND)
1447 _lseek(fd, 0, FILE_END);
1453 /*********************************************************************
1454 * _wsopen (MSVCRT.@)
1456 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1458 const unsigned int len = strlenW(path);
1459 char *patha = MSVCRT_calloc(len + 1,1);
1463 va_start(ap, shflags);
1464 pmode = va_arg(ap, int);
1467 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1469 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1474 msvcrt_set_errno(GetLastError());
1478 /*********************************************************************
1481 int _open( const char *path, int flags, ... )
1485 if (flags & MSVCRT__O_CREAT)
1488 va_start(ap, flags);
1489 pmode = va_arg(ap, int);
1491 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1494 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1497 /*********************************************************************
1500 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1502 const unsigned int len = strlenW(path);
1503 char *patha = MSVCRT_calloc(len + 1,1);
1507 va_start(ap, flags);
1508 pmode = va_arg(ap, int);
1511 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1513 int retval = _open(patha,flags,pmode);
1518 msvcrt_set_errno(GetLastError());
1522 /*********************************************************************
1525 int _creat(const char *path, int flags)
1527 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1528 return _open(path, usedFlags);
1531 /*********************************************************************
1532 * _wcreat (MSVCRT.@)
1534 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1536 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1537 return _wopen(path, usedFlags);
1540 /*********************************************************************
1541 * _open_osfhandle (MSVCRT.@)
1543 int _open_osfhandle(long handle, int oflags)
1547 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1548 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1549 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1550 * text - it never sets MSVCRT__O_BINARY.
1552 /* FIXME: handle more flags */
1553 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)) && (*__p__fmode() & MSVCRT__O_BINARY))
1554 oflags |= MSVCRT__O_BINARY;
1556 oflags |= MSVCRT__O_TEXT;
1558 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1559 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1563 /*********************************************************************
1568 int num_removed = 0, i;
1571 for (i = 3; i < MSVCRT_stream_idx; i++)
1572 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1574 MSVCRT_fclose(MSVCRT_fstreams[i]);
1580 TRACE(":removed (%d) temp files\n",num_removed);
1584 /*********************************************************************
1585 * (internal) remove_cr
1587 * Remove all \r inplace.
1588 * return the number of \r removed
1590 static unsigned int remove_cr(char *buf, unsigned int count)
1594 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1595 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1599 /*********************************************************************
1602 int _read(int fd, void *buf, unsigned int count)
1604 DWORD num_read, all_read = 0;
1605 char *bufstart = buf;
1606 HANDLE hand = msvcrt_fdtoh(fd);
1608 /* Don't trace small reads, it gets *very* annoying */
1610 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1611 if (hand == INVALID_HANDLE_VALUE)
1614 /* Reading single bytes in O_TEXT mode makes things slow
1615 * So read big chunks, then remove the \r in memory and try reading
1616 * the rest until the request is satisfied or EOF is met
1618 while (all_read < count)
1620 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1622 if (num_read != (count - all_read))
1625 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1626 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1627 num_read -= remove_cr(bufstart+all_read,num_read);
1628 all_read += num_read;
1630 TRACE("%s\n",debugstr_an(buf,all_read));
1633 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1635 num_read -= remove_cr(bufstart+all_read,num_read);
1637 all_read += num_read;
1641 TRACE(":failed-last error (%ld)\n",GetLastError());
1647 TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
1651 /*********************************************************************
1654 int MSVCRT__getw(MSVCRT_FILE* file)
1657 switch (_read(file->_file, &i, sizeof(int)))
1660 case 0: file->_flag |= MSVCRT__IOEOF; break;
1661 default: file->_flag |= MSVCRT__IOERR; break;
1666 /*********************************************************************
1667 * _setmode (MSVCRT.@)
1669 int _setmode(int fd,int mode)
1671 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1672 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1673 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1674 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1675 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1677 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1681 /*********************************************************************
1682 * _stati64 (MSVCRT.@)
1684 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1687 WIN32_FILE_ATTRIBUTE_DATA hfi;
1688 unsigned short mode = ALL_S_IREAD;
1691 TRACE(":file (%s) buf(%p)\n",path,buf);
1693 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1695 TRACE("failed (%ld)\n",GetLastError());
1696 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1700 memset(buf,0,sizeof(struct MSVCRT__stati64));
1702 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1703 Bon 011120: This FIXME seems incorrect
1704 Also a letter as first char isn't enough to be classified
1707 if (isalpha(*path)&& (*(path+1)==':'))
1708 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1710 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1712 plen = strlen(path);
1714 /* Dir, or regular file? */
1715 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1716 (path[plen-1] == '\\'))
1717 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1720 mode |= MSVCRT__S_IFREG;
1722 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1724 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1725 (tolower(path[plen-3]) << 16);
1726 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1727 mode |= ALL_S_IEXEC;
1731 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1732 mode |= ALL_S_IWRITE;
1734 buf->st_mode = mode;
1736 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1737 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1739 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1740 buf->st_mtime = buf->st_ctime = dw;
1741 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1742 (long)(buf->st_size >> 32),(long)buf->st_size,
1743 buf->st_atime,buf->st_mtime, buf->st_ctime);
1747 /*********************************************************************
1750 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1752 struct MSVCRT__stati64 bufi64;
1754 ret = MSVCRT__stati64( path, &bufi64);
1756 msvcrt_cp_from_stati64(&bufi64, buf);
1760 /*********************************************************************
1761 * _wstati64 (MSVCRT.@)
1763 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1766 WIN32_FILE_ATTRIBUTE_DATA hfi;
1767 unsigned short mode = ALL_S_IREAD;
1770 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1772 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1774 TRACE("failed (%ld)\n",GetLastError());
1775 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1779 memset(buf,0,sizeof(struct MSVCRT__stat));
1781 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1782 if (MSVCRT_iswalpha(*path))
1783 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1785 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1787 plen = strlenW(path);
1789 /* Dir, or regular file? */
1790 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1791 (path[plen-1] == '\\'))
1792 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1795 mode |= MSVCRT__S_IFREG;
1797 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1799 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1800 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1801 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1802 mode |= ALL_S_IEXEC;
1806 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1807 mode |= ALL_S_IWRITE;
1809 buf->st_mode = mode;
1811 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1812 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1814 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1815 buf->st_mtime = buf->st_ctime = dw;
1816 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1817 (long)(buf->st_size >> 32),(long)buf->st_size,
1818 buf->st_atime,buf->st_mtime, buf->st_ctime);
1822 /*********************************************************************
1825 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1828 struct MSVCRT__stati64 bufi64;
1830 ret = MSVCRT__wstati64( path, &bufi64 );
1831 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1835 /*********************************************************************
1840 return _lseek(fd, 0, SEEK_CUR);
1843 /*********************************************************************
1844 * _telli64 (MSVCRT.@)
1846 __int64 _telli64(int fd)
1848 return _lseeki64(fd, 0, SEEK_CUR);
1851 /*********************************************************************
1852 * _tempnam (MSVCRT.@)
1854 char *_tempnam(const char *dir, const char *prefix)
1856 char tmpbuf[MAX_PATH];
1858 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1859 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1861 TRACE("got name (%s)\n",tmpbuf);
1862 DeleteFileA(tmpbuf);
1863 return _strdup(tmpbuf);
1865 TRACE("failed (%ld)\n",GetLastError());
1869 /*********************************************************************
1870 * _wtempnam (MSVCRT.@)
1872 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1874 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1876 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1877 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1879 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1880 DeleteFileW(tmpbuf);
1881 return _wcsdup(tmpbuf);
1883 TRACE("failed (%ld)\n",GetLastError());
1887 /*********************************************************************
1890 int _umask(int umask)
1892 int old_umask = MSVCRT_umask;
1893 TRACE("(%d)\n",umask);
1894 MSVCRT_umask = umask;
1898 /*********************************************************************
1901 int _utime(const char* path, struct MSVCRT__utimbuf *t)
1903 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1907 int retVal = _futime(fd, t);
1914 /*********************************************************************
1915 * _wutime (MSVCRT.@)
1917 int _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1919 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1923 int retVal = _futime(fd, t);
1930 /*********************************************************************
1933 int _write(int fd, const void* buf, unsigned int count)
1936 HANDLE hand = msvcrt_fdtoh(fd);
1938 /* Don't trace small writes, it gets *very* annoying */
1941 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1943 if (hand == INVALID_HANDLE_VALUE)
1945 *MSVCRT__errno() = MSVCRT_EBADF;
1949 /* If appending, go to EOF */
1950 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
1951 _lseek(fd, 0, FILE_END);
1953 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
1955 if (WriteFile(hand, buf, count, &num_written, NULL)
1956 && (num_written == count))
1958 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd,
1959 hand, GetLastError());
1960 *MSVCRT__errno() = MSVCRT_ENOSPC;
1964 unsigned int i, j, nr_lf;
1965 char *s =(char*)buf, *buf_start=(char*)buf, *p;
1966 /* find number of \n ( without preceeding \r */
1967 for ( nr_lf=0,i = 0; i <count; i++)
1972 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1977 if ((p = MSVCRT_malloc(count + nr_lf)))
1979 for(s=(char*)buf, i=0, j=0; i<count; i++)
1984 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1991 FIXME("Malloc failed\n");
1999 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2001 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld), num_written %ld\n",
2002 fd, hand, GetLastError(), num_written);
2003 *MSVCRT__errno() = MSVCRT_ENOSPC;
2006 return s - buf_start;
2018 /*********************************************************************
2021 int MSVCRT__putw(int val, MSVCRT_FILE* file)
2024 len = _write(file->_file, &val, sizeof(val));
2025 if (len == sizeof(val)) return val;
2026 file->_flag |= MSVCRT__IOERR;
2030 /*********************************************************************
2033 int MSVCRT_fclose(MSVCRT_FILE* file)
2038 if (file->_tmpfname)
2040 MSVCRT_free(file->_tmpfname);
2041 file->_tmpfname = NULL;
2043 /* flush stdio buffers */
2044 if(file->_flag & MSVCRT__IOWRT)
2045 MSVCRT_fflush(file);
2046 if(file->_flag & MSVCRT__IOMYBUF)
2047 MSVCRT_free(file->_base);
2049 r=_close(file->_file);
2053 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2056 /*********************************************************************
2059 int MSVCRT_feof(MSVCRT_FILE* file)
2061 return file->_flag & MSVCRT__IOEOF;
2064 /*********************************************************************
2067 int MSVCRT_ferror(MSVCRT_FILE* file)
2069 return file->_flag & MSVCRT__IOERR;
2072 /*********************************************************************
2073 * _filbuf (MSVCRT.@)
2075 int MSVCRT__filbuf(MSVCRT_FILE* file)
2077 /* Allocate buffer if needed */
2078 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2079 msvcrt_alloc_buffer(file);
2081 if(!(file->_flag & MSVCRT__IOREAD)) {
2082 if(file->_flag & MSVCRT__IORW) {
2083 file->_flag |= MSVCRT__IOREAD;
2088 if(file->_flag & MSVCRT__IONBF) {
2091 if ((r = _read(file->_file,&c,1)) != 1) {
2092 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2097 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2099 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2104 file->_ptr = file->_base+1;
2105 return *(unsigned char *)file->_base;
2109 /*********************************************************************
2112 int MSVCRT_fgetc(MSVCRT_FILE* file)
2116 return *(unsigned char *)file->_ptr++;
2118 return MSVCRT__filbuf(file);
2122 /*********************************************************************
2123 * _fgetchar (MSVCRT.@)
2127 return MSVCRT_fgetc(MSVCRT_stdin);
2130 /*********************************************************************
2133 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2135 int cc = MSVCRT_EOF;
2136 char * buf_start = s;
2138 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2139 file,file->_file,s,size);
2141 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2146 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2148 TRACE(":nothing read\n");
2151 if ((cc != MSVCRT_EOF) && (size > 1))
2154 TRACE(":got '%s'\n", debugstr_a(buf_start));
2158 /*********************************************************************
2161 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2162 * the CR from CR/LF combinations
2164 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
2168 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2172 if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
2174 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2179 c = MSVCRT_fgetc(file);
2180 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2182 FIXME("Treat Multibyte characters\n");
2184 if (c == MSVCRT_EOF)
2187 return (MSVCRT_wint_t)c;
2190 /*********************************************************************
2193 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
2195 return MSVCRT_fgetwc(file);
2198 /*********************************************************************
2199 * _fgetwchar (MSVCRT.@)
2201 MSVCRT_wint_t _fgetwchar(void)
2203 return MSVCRT_fgetwc(MSVCRT_stdin);
2206 /*********************************************************************
2207 * getwchar (MSVCRT.@)
2209 MSVCRT_wint_t MSVCRT_getwchar(void)
2211 return _fgetwchar();
2214 /*********************************************************************
2217 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2219 int cc = MSVCRT_WEOF;
2220 MSVCRT_wchar_t * buf_start = s;
2222 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2223 file,file->_file,s,size);
2225 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2230 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2232 TRACE(":nothing read\n");
2235 if ((cc != MSVCRT_WEOF) && (size > 1))
2238 TRACE(":got %s\n", debugstr_w(buf_start));
2242 /*********************************************************************
2245 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2247 MSVCRT_size_t wrcnt=size * nmemb;
2252 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2253 memcpy(file->_ptr, ptr, pcnt);
2258 ptr = (const char*)ptr + pcnt;
2259 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2260 if(file->_flag & MSVCRT__IORW) {
2261 file->_flag |= MSVCRT__IOWRT;
2267 int res=msvcrt_flush_buffer(file);
2269 int pwritten = _write(file->_file, ptr, wrcnt);
2272 file->_flag |= MSVCRT__IOERR;
2275 written += pwritten;
2278 return written / size;
2281 /*********************************************************************
2284 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2286 MSVCRT_wchar_t mwc=wc;
2287 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2292 /*********************************************************************
2293 * _fputwchar (MSVCRT.@)
2295 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
2297 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2300 /*********************************************************************
2303 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
2306 int open_flags, stream_flags, fd;
2308 TRACE("(%s,%s)\n",path,mode);
2310 /* map mode string to open() flags. "man fopen" for possibilities. */
2311 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2315 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2318 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2320 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2327 TRACE(":got (%p)\n",file);
2334 /*********************************************************************
2335 * _wfopen (MSVCRT.@)
2337 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2339 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2340 char *patha = MSVCRT_calloc(plen + 1, 1);
2341 char *modea = MSVCRT_calloc(mlen + 1, 1);
2343 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2345 if (patha && modea &&
2346 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2347 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2349 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2355 msvcrt_set_errno(GetLastError());
2359 /*********************************************************************
2360 * _fsopen (MSVCRT.@)
2362 MSVCRT_FILE* MSVCRT__fsopen(const char *path, const char *mode, int share)
2364 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2365 return MSVCRT_fopen(path,mode);
2368 /*********************************************************************
2369 * _wfsopen (MSVCRT.@)
2371 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2373 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2374 debugstr_w(path),debugstr_w(mode),share);
2375 return MSVCRT__wfopen(path,mode);
2378 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2379 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2381 /*********************************************************************
2384 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2391 int res = msvcrt_flush_buffer(file);
2392 return res ? res : c;
2397 return MSVCRT__flsbuf(c, file);
2401 /*********************************************************************
2402 * _flsbuf (MSVCRT.@)
2404 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2406 /* Flush output buffer */
2407 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2408 msvcrt_alloc_buffer(file);
2410 if(!(file->_flag & MSVCRT__IOWRT)) {
2411 if(file->_flag & MSVCRT__IORW) {
2412 file->_flag |= MSVCRT__IOWRT;
2418 int res=msvcrt_flush_buffer(file);
2419 return res?res : MSVCRT_fputc(c, file);
2423 len = _write(file->_file, &cc, 1);
2424 if (len == 1) return c;
2425 file->_flag |= MSVCRT__IOERR;
2430 /*********************************************************************
2431 * _fputchar (MSVCRT.@)
2433 int _fputchar(int c)
2435 return MSVCRT_fputc(c, MSVCRT_stdout);
2438 /*********************************************************************
2441 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2442 { MSVCRT_size_t rcnt=size * nmemb;
2443 MSVCRT_size_t read=0;
2446 /* first buffered data */
2448 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2449 memcpy(ptr, file->_ptr, pcnt);
2454 ptr = (char*)ptr + pcnt;
2455 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2456 if(file->_flag & MSVCRT__IORW) {
2457 file->_flag |= MSVCRT__IOREAD;
2463 pread = _read(file->_file,ptr, rcnt);
2464 /* expose feof condition in the flags
2465 * MFC tests file->_flag for feof, and doesn't not call feof())
2468 file->_flag |= MSVCRT__IOEOF;
2469 else if (pread == -1)
2471 file->_flag |= MSVCRT__IOERR;
2479 /*********************************************************************
2480 * freopen (MSVCRT.@)
2483 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2485 int open_flags, stream_flags, fd;
2487 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2490 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2494 MSVCRT_fclose(file);
2495 /* map mode string to open() flags. "man fopen" for possibilities. */
2496 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2500 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2503 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2506 WARN(":failed-last error (%ld)\n",GetLastError());
2507 msvcrt_set_errno(GetLastError());
2516 /*********************************************************************
2517 * fsetpos (MSVCRT.@)
2519 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2521 /* Note that all this has been lifted 'as is' from fseek */
2522 if(file->_flag & MSVCRT__IOWRT)
2523 msvcrt_flush_buffer(file);
2525 /* Discard buffered input */
2527 file->_ptr = file->_base;
2529 /* Reset direction of i/o */
2530 if(file->_flag & MSVCRT__IORW) {
2531 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2534 return _lseeki64(file->_file,*pos,SEEK_SET);
2537 /*********************************************************************
2540 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2545 if( file->_flag & MSVCRT__IOWRT ) {
2546 off = file->_ptr - file->_base;
2551 pos = _tell(file->_file);
2552 if(pos == -1) return pos;
2556 /*********************************************************************
2557 * fgetpos (MSVCRT.@)
2559 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2561 /* This code has been lifted form the MSVCRT_ftell function */
2564 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2566 if (*pos == -1) return -1;
2569 if( file->_flag & MSVCRT__IOWRT ) {
2570 off = file->_ptr - file->_base;
2580 /*********************************************************************
2583 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2585 size_t i, len = strlen(s);
2586 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2587 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2588 for (i=0; i<len; i++)
2589 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2594 /*********************************************************************
2597 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2599 size_t i, len = strlenW(s);
2600 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2601 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2602 for (i=0; i<len; i++)
2604 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2606 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2612 /*********************************************************************
2613 * getchar (MSVCRT.@)
2615 int MSVCRT_getchar(void)
2617 return MSVCRT_fgetc(MSVCRT_stdin);
2620 /*********************************************************************
2623 int MSVCRT_getc(MSVCRT_FILE* file)
2625 return MSVCRT_fgetc(file);
2628 /*********************************************************************
2631 char *MSVCRT_gets(char *buf)
2634 char * buf_start = buf;
2636 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2637 cc = MSVCRT_fgetc(MSVCRT_stdin))
2638 if(cc != '\r') *buf++ = (char)cc;
2642 TRACE("got '%s'\n", buf_start);
2646 /*********************************************************************
2649 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2652 MSVCRT_wchar_t* ws = buf;
2654 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2655 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2658 *buf++ = (MSVCRT_wchar_t)cc;
2662 TRACE("got '%s'\n", debugstr_w(ws));
2666 /*********************************************************************
2669 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2671 return MSVCRT_fputc(c, file);
2674 /*********************************************************************
2675 * putchar (MSVCRT.@)
2677 int MSVCRT_putchar(int c)
2679 return MSVCRT_fputc(c, MSVCRT_stdout);
2682 /*********************************************************************
2685 int MSVCRT_puts(const char *s)
2687 size_t len = strlen(s);
2688 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2689 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2692 /*********************************************************************
2695 int _putws(const MSVCRT_wchar_t *s)
2697 static const MSVCRT_wchar_t nl = '\n';
2698 size_t len = strlenW(s);
2699 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2700 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2703 /*********************************************************************
2706 int MSVCRT_remove(const char *path)
2708 TRACE("(%s)\n",path);
2709 if (DeleteFileA(path))
2711 TRACE(":failed (%ld)\n",GetLastError());
2712 msvcrt_set_errno(GetLastError());
2716 /*********************************************************************
2717 * _wremove (MSVCRT.@)
2719 int _wremove(const MSVCRT_wchar_t *path)
2721 TRACE("(%s)\n",debugstr_w(path));
2722 if (DeleteFileW(path))
2724 TRACE(":failed (%ld)\n",GetLastError());
2725 msvcrt_set_errno(GetLastError());
2729 /*********************************************************************
2732 int MSVCRT_rename(const char *oldpath,const char *newpath)
2734 TRACE(":from %s to %s\n",oldpath,newpath);
2735 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2737 TRACE(":failed (%ld)\n",GetLastError());
2738 msvcrt_set_errno(GetLastError());
2742 /*********************************************************************
2743 * _wrename (MSVCRT.@)
2745 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2747 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2748 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2750 TRACE(":failed (%ld)\n",GetLastError());
2751 msvcrt_set_errno(GetLastError());
2755 /*********************************************************************
2756 * setvbuf (MSVCRT.@)
2758 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2760 /* TODO: Check if file busy */
2762 MSVCRT_free(file->_base);
2766 if(mode == MSVCRT__IOFBF) {
2767 file->_flag &= ~MSVCRT__IONBF;
2768 file->_base = file->_ptr = buf;
2770 file->_bufsiz = size;
2773 file->_flag |= MSVCRT__IONBF;
2778 /*********************************************************************
2781 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2783 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2786 /*********************************************************************
2789 char *MSVCRT_tmpnam(char *s)
2797 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2798 p = s + sprintf(s, "\\s%s.", tmpstr);
2799 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2801 msvcrt_int_to_base32(unique++, tmpstr);
2803 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2804 GetLastError() == ERROR_FILE_NOT_FOUND)
2810 /*********************************************************************
2811 * tmpfile (MSVCRT.@)
2813 MSVCRT_FILE* MSVCRT_tmpfile(void)
2815 char *filename = MSVCRT_tmpnam(NULL);
2817 MSVCRT_FILE* file = NULL;
2820 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2821 if (fd != -1 && (file = msvcrt_alloc_fp()))
2823 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2828 else file->_tmpfname = _strdup(filename);
2834 /*********************************************************************
2835 * vfprintf (MSVCRT.@)
2837 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2839 char buf[2048], *mem = buf;
2840 int written, resize = sizeof(buf), retval;
2841 /* There are two conventions for vsnprintf failing:
2842 * Return -1 if we truncated, or
2843 * Return the number of bytes that would have been written
2844 * The code below handles both cases
2846 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2849 resize = (written == -1 ? resize * 2 : written + 1);
2852 if (!(mem = (char *)MSVCRT_malloc(resize)))
2855 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2861 /*********************************************************************
2862 * vfwprintf (MSVCRT.@)
2864 * Is final char included in written (then resize is too big) or not
2865 * (then we must test for equality too)?
2867 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2869 MSVCRT_wchar_t buf[2048], *mem = buf;
2870 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2871 /* See vfprintf comments */
2872 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2875 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2878 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2881 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2887 /*********************************************************************
2888 * vprintf (MSVCRT.@)
2890 int MSVCRT_vprintf(const char *format, va_list valist)
2892 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2895 /*********************************************************************
2896 * vwprintf (MSVCRT.@)
2898 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2900 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2903 /*********************************************************************
2904 * fprintf (MSVCRT.@)
2906 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2910 va_start(valist, format);
2911 res = MSVCRT_vfprintf(file, format, valist);
2916 /*********************************************************************
2917 * fwprintf (MSVCRT.@)
2919 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2923 va_start(valist, format);
2924 res = MSVCRT_vfwprintf(file, format, valist);
2929 /*********************************************************************
2932 int MSVCRT_printf(const char *format, ...)
2936 va_start(valist, format);
2937 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2942 /*********************************************************************
2945 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2947 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2948 msvcrt_alloc_buffer(file);
2951 if(file->_ptr>file->_base) {
2960 /*********************************************************************
2961 * ungetwc (MSVCRT.@)
2963 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2965 MSVCRT_wchar_t mwc = wc;
2966 char * pp = (char *)&mwc;
2968 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2969 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2975 /*********************************************************************
2976 * wprintf (MSVCRT.@)
2978 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2982 va_start(valist, format);
2983 res = MSVCRT_vwprintf(format, valist);
2988 /*********************************************************************
2989 * __pioinfo (MSVCRT.@)
2990 * FIXME: see MSVCRT_MAX_FILES define.
2992 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
2993 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
2994 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
2995 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
2996 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
2997 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
2998 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
2999 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3000 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3001 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3002 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3003 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3006 /*********************************************************************
3007 * __badioinfo (MSVCRT.@)
3009 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };