2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
38 #include <sys/types.h>
45 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
51 /* for stat mode, permissions apply to all,owner and group */
52 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
53 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
54 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
56 /* _access() bit flags FIXME: incomplete */
57 #define MSVCRT_W_OK 0x02
59 /* values for wxflag in file descriptor */
62 #define WX_DONTINHERIT 0x10
63 #define WX_APPEND 0x20
66 /* FIXME: this should be allocated dynamically */
67 #define MSVCRT_MAX_FILES 2048
72 DWORD unkn[7]; /* critical section and init flag */
75 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
77 MSVCRT_FILE MSVCRT__iob[3];
79 static int MSVCRT_fdstart = 3; /* first unallocated fd */
80 static int MSVCRT_fdend = 3; /* highest allocated fd */
82 static MSVCRT_FILE* MSVCRT_fstreams[2048];
83 static int MSVCRT_stream_idx;
85 /* INTERNAL: process umask */
86 static int MSVCRT_umask = 0;
88 /* INTERNAL: Static buffer for temp file name */
89 static char MSVCRT_tmpname[MAX_PATH];
91 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
92 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
93 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
94 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
96 #define TOUL(x) (ULONGLONG)(x)
97 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
98 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
99 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
100 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
102 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
103 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
104 * and MSVCRT_stream_idx, from race conditions.
105 * It doesn't protect against race conditions manipulating the underlying files
106 * or flags; doing so would probably be better accomplished with per-file
107 * protection, rather than locking the whole table for every change.
109 static CRITICAL_SECTION MSVCRT_file_cs;
110 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
111 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
113 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
115 buf->st_dev = buf64->st_dev;
116 buf->st_ino = buf64->st_ino;
117 buf->st_mode = buf64->st_mode;
118 buf->st_nlink = buf64->st_nlink;
119 buf->st_uid = buf64->st_uid;
120 buf->st_gid = buf64->st_gid;
121 buf->st_rdev = buf64->st_rdev;
122 buf->st_size = buf64->st_size;
123 buf->st_atime = buf64->st_atime;
124 buf->st_mtime = buf64->st_mtime;
125 buf->st_ctime = buf64->st_ctime;
128 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
130 buf->st_dev = buf64->st_dev;
131 buf->st_ino = buf64->st_ino;
132 buf->st_mode = buf64->st_mode;
133 buf->st_nlink = buf64->st_nlink;
134 buf->st_uid = buf64->st_uid;
135 buf->st_gid = buf64->st_gid;
136 buf->st_rdev = buf64->st_rdev;
137 buf->st_size = buf64->st_size;
138 buf->st_atime = buf64->st_atime;
139 buf->st_mtime = buf64->st_mtime;
140 buf->st_ctime = buf64->st_ctime;
143 static inline BOOL msvcrt_is_valid_fd(int fd)
145 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
148 /* INTERNAL: Get the HANDLE for a fd
149 * This doesn't lock the table, because a failure will result in
150 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
151 * it returns a valid handle which is about to be closed, a subsequent call
152 * will fail, most likely in a sane way.
154 static HANDLE msvcrt_fdtoh(int fd)
156 if (!msvcrt_is_valid_fd(fd))
158 WARN(":fd (%d) - no handle!\n",fd);
159 *MSVCRT___doserrno() = 0;
160 *MSVCRT__errno() = MSVCRT_EBADF;
161 return INVALID_HANDLE_VALUE;
163 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
164 return MSVCRT_fdesc[fd].handle;
167 /* INTERNAL: free a file entry fd */
168 static void msvcrt_free_fd(int fd)
171 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
172 MSVCRT_fdesc[fd].wxflag = 0;
173 TRACE(":fd (%d) freed\n",fd);
174 if (fd < 3) /* don't use 0,1,2 for user files */
178 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
179 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
180 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
185 if (fd == MSVCRT_fdend - 1)
187 if (fd < MSVCRT_fdstart)
193 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
194 /* caller must hold the files lock */
195 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
197 if (fd >= MSVCRT_MAX_FILES)
199 WARN(":files exhausted!\n");
202 MSVCRT_fdesc[fd].handle = hand;
203 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
205 /* locate next free slot */
206 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
207 MSVCRT_fdstart = MSVCRT_fdend + 1;
209 while (MSVCRT_fdstart < MSVCRT_fdend &&
210 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
212 /* update last fd in use */
213 if (fd >= MSVCRT_fdend)
214 MSVCRT_fdend = fd + 1;
215 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
219 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
220 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
221 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
227 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
228 static int msvcrt_alloc_fd(HANDLE hand, int flag)
233 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
234 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
239 /* INTERNAL: Allocate a FILE* for an fd slot */
240 /* caller must hold the files lock */
241 static MSVCRT_FILE* msvcrt_alloc_fp(void)
245 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
247 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
249 if (!MSVCRT_fstreams[i])
251 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
253 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
255 return MSVCRT_fstreams[i];
261 /* INTERNAL: initialize a FILE* from an open fd */
262 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
264 TRACE(":fd (%d) allocating FILE*\n",fd);
265 if (!msvcrt_is_valid_fd(fd))
267 WARN(":invalid fd %d\n",fd);
268 *MSVCRT___doserrno() = 0;
269 *MSVCRT__errno() = MSVCRT_EBADF;
272 memset(file, 0, sizeof(*file));
274 file->_flag = stream_flags;
276 TRACE(":got FILE* (%p)\n",file);
280 /* INTERNAL: Create an inheritance data block (for spawned process)
281 * The inheritance block is made of:
282 * 00 int nb of file descriptor (NBFD)
283 * 04 char file flags (wxflag): repeated for each fd
284 * 4+NBFD HANDLE file handle: repeated for each fd
286 unsigned msvcrt_create_io_inherit_block(STARTUPINFOA* si)
292 si->cbReserved2 = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
293 si->lpReserved2 = MSVCRT_calloc(si->cbReserved2, 1);
294 if (!si->lpReserved2)
299 wxflag_ptr = (char*)si->lpReserved2 + sizeof(unsigned);
300 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
302 *(unsigned*)si->lpReserved2 = MSVCRT_fdend;
303 for (fd = 0; fd < MSVCRT_fdend; fd++)
305 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
306 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
308 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
309 *handle_ptr = MSVCRT_fdesc[fd].handle;
314 *handle_ptr = INVALID_HANDLE_VALUE;
316 wxflag_ptr++; handle_ptr++;
321 /* INTERNAL: Set up all file descriptors,
322 * as well as default streams (stdin, stderr and stdout)
324 void msvcrt_init_io(void)
329 InitializeCriticalSection(&MSVCRT_file_cs);
330 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
331 GetStartupInfoA(&si);
332 if (si.cbReserved2 != 0 && si.lpReserved2 != NULL)
337 MSVCRT_fdend = *(unsigned*)si.lpReserved2;
339 wxflag_ptr = (char*)(si.lpReserved2 + sizeof(unsigned));
340 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
342 MSVCRT_fdend = min(MSVCRT_fdend, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
343 for (i = 0; i < MSVCRT_fdend; i++)
345 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
347 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
348 MSVCRT_fdesc[i].handle = *handle_ptr;
352 MSVCRT_fdesc[i].wxflag = 0;
353 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
355 wxflag_ptr++; handle_ptr++;
357 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
358 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
361 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
363 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
364 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, TRUE,
365 DUPLICATE_SAME_ACCESS);
366 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
368 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
370 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
371 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, TRUE,
372 DUPLICATE_SAME_ACCESS);
373 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
375 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
377 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
378 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, TRUE,
379 DUPLICATE_SAME_ACCESS);
380 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
383 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
384 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
386 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
387 for (i = 0; i < 3; i++)
389 /* FILE structs for stdin/out/err are static and never deleted */
390 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
391 MSVCRT__iob[i]._file = i;
392 MSVCRT__iob[i]._tmpfname = NULL;
393 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
395 MSVCRT_stream_idx = 3;
398 /* INTERNAL: Flush stdio file buffer */
399 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
402 int cnt=file->_ptr-file->_base;
403 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
404 file->_flag |= MSVCRT__IOERR;
407 file->_ptr=file->_base;
408 file->_cnt=file->_bufsiz;
413 /* INTERNAL: Allocate stdio file buffer */
414 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
416 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
418 file->_bufsiz = MSVCRT_BUFSIZ;
419 file->_flag |= MSVCRT__IOMYBUF;
421 file->_base = (char*)(&file->_charbuf);
423 file->_bufsiz = sizeof(file->_charbuf);
425 file->_ptr = file->_base;
429 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
430 static void msvcrt_int_to_base32(int num, char *str)
445 *p = (num & 31) + '0';
447 *p += ('a' - '0' - 10);
452 /*********************************************************************
455 MSVCRT_FILE * CDECL __p__iob(void)
457 return &MSVCRT__iob[0];
460 /*********************************************************************
463 int CDECL _access(const char *filename, int mode)
465 DWORD attr = GetFileAttributesA(filename);
467 TRACE("(%s,%d) %d\n",filename,mode,attr);
469 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
471 msvcrt_set_errno(GetLastError());
474 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
476 msvcrt_set_errno(ERROR_ACCESS_DENIED);
482 /*********************************************************************
483 * _waccess (MSVCRT.@)
485 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
487 DWORD attr = GetFileAttributesW(filename);
489 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
491 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
493 msvcrt_set_errno(GetLastError());
496 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
498 msvcrt_set_errno(ERROR_ACCESS_DENIED);
504 /*********************************************************************
507 int CDECL _chmod(const char *path, int flags)
509 DWORD oldFlags = GetFileAttributesA(path);
511 if (oldFlags != INVALID_FILE_ATTRIBUTES)
513 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
514 oldFlags | FILE_ATTRIBUTE_READONLY;
516 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
519 msvcrt_set_errno(GetLastError());
523 /*********************************************************************
526 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
528 DWORD oldFlags = GetFileAttributesW(path);
530 if (oldFlags != INVALID_FILE_ATTRIBUTES)
532 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
533 oldFlags | FILE_ATTRIBUTE_READONLY;
535 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
538 msvcrt_set_errno(GetLastError());
542 /*********************************************************************
545 int CDECL _unlink(const char *path)
547 TRACE("%s\n",debugstr_a(path));
548 if(DeleteFileA(path))
550 TRACE("failed (%d)\n",GetLastError());
551 msvcrt_set_errno(GetLastError());
555 /*********************************************************************
556 * _wunlink (MSVCRT.@)
558 int CDECL _wunlink(const MSVCRT_wchar_t *path)
560 TRACE("(%s)\n",debugstr_w(path));
561 if(DeleteFileW(path))
563 TRACE("failed (%d)\n",GetLastError());
564 msvcrt_set_errno(GetLastError());
568 /* _flushall calls MSVCRT_fflush which calls _flushall */
569 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
571 /*********************************************************************
572 * _flushall (MSVCRT.@)
574 int CDECL _flushall(void)
576 int i, num_flushed = 0;
579 for (i = 3; i < MSVCRT_stream_idx; i++)
580 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
583 /* FIXME: flush, do not commit */
584 if (_commit(i) == -1)
585 if (MSVCRT_fstreams[i])
586 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
588 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
589 MSVCRT_fflush(MSVCRT_fstreams[i]);
595 TRACE(":flushed (%d) handles\n",num_flushed);
599 /*********************************************************************
602 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
606 } else if(file->_flag & MSVCRT__IOWRT) {
607 int res=msvcrt_flush_buffer(file);
613 /*********************************************************************
616 int CDECL _close(int fd)
622 hand = msvcrt_fdtoh(fd);
623 TRACE(":fd (%d) handle (%p)\n",fd,hand);
624 if (hand == INVALID_HANDLE_VALUE)
626 else if (!CloseHandle(hand))
628 WARN(":failed-last error (%d)\n",GetLastError());
629 msvcrt_set_errno(GetLastError());
642 /*********************************************************************
645 int CDECL _commit(int fd)
647 HANDLE hand = msvcrt_fdtoh(fd);
649 TRACE(":fd (%d) handle (%p)\n",fd,hand);
650 if (hand == INVALID_HANDLE_VALUE)
653 if (!FlushFileBuffers(hand))
655 if (GetLastError() == ERROR_INVALID_HANDLE)
657 /* FlushFileBuffers fails for console handles
658 * so we ignore this error.
662 TRACE(":failed-last error (%d)\n",GetLastError());
663 msvcrt_set_errno(GetLastError());
670 /*********************************************************************
673 * MSDN isn't clear on this point, but the remarks for _pipe,
674 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
675 * indicate file descriptors duplicated with _dup and _dup2 are always
678 int CDECL _dup2(int od, int nd)
682 TRACE("(od=%d, nd=%d)\n", od, nd);
684 if (nd < MSVCRT_MAX_FILES && msvcrt_is_valid_fd(od))
688 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
689 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
691 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
693 if (msvcrt_is_valid_fd(nd))
695 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
699 *MSVCRT__errno() = MSVCRT_EMFILE;
703 /* _dup2 returns 0, not nd, on success */
710 msvcrt_set_errno(GetLastError());
715 *MSVCRT__errno() = MSVCRT_EBADF;
722 /*********************************************************************
725 int CDECL _dup(int od)
731 if (_dup2(od, fd) == 0)
739 /*********************************************************************
742 int CDECL _eof(int fd)
745 LONG hcurpos,hendpos;
746 HANDLE hand = msvcrt_fdtoh(fd);
748 TRACE(":fd (%d) handle (%p)\n",fd,hand);
750 if (hand == INVALID_HANDLE_VALUE)
753 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
755 /* Otherwise we do it the hard way */
756 hcurpos = hendpos = 0;
757 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
758 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
760 if (curpos == endpos && hcurpos == hendpos)
762 /* FIXME: shouldn't WX_ATEOF be set here? */
766 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
770 /*********************************************************************
771 * _fcloseall (MSVCRT.@)
773 int CDECL MSVCRT__fcloseall(void)
775 int num_closed = 0, i;
778 for (i = 3; i < MSVCRT_stream_idx; i++)
779 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
780 !MSVCRT_fclose(MSVCRT_fstreams[i]))
784 TRACE(":closed (%d) handles\n",num_closed);
788 /* free everything on process exit */
789 void msvcrt_free_io(void)
792 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
793 * stdout, and stderr (unlike GNU), so we need to fclose() them here
794 * or they won't get flushed.
796 MSVCRT_fclose(&MSVCRT__iob[0]);
797 MSVCRT_fclose(&MSVCRT__iob[1]);
798 MSVCRT_fclose(&MSVCRT__iob[2]);
799 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
800 DeleteCriticalSection(&MSVCRT_file_cs);
803 /*********************************************************************
804 * _lseeki64 (MSVCRT.@)
806 __int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
808 HANDLE hand = msvcrt_fdtoh(fd);
809 LARGE_INTEGER ofs, ret;
811 TRACE(":fd (%d) handle (%p)\n",fd,hand);
812 if (hand == INVALID_HANDLE_VALUE)
815 if (whence < 0 || whence > 2)
817 *MSVCRT__errno() = MSVCRT_EINVAL;
821 TRACE(":fd (%d) to %s pos %s\n",
822 fd,wine_dbgstr_longlong(offset),
823 (whence==SEEK_SET)?"SEEK_SET":
824 (whence==SEEK_CUR)?"SEEK_CUR":
825 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
827 ofs.QuadPart = offset;
828 if (SetFilePointerEx(hand, ofs, &ret, whence))
830 MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
831 /* FIXME: What if we seek _to_ EOF - is EOF set? */
835 TRACE(":error-last error (%d)\n",GetLastError());
836 msvcrt_set_errno(GetLastError());
840 /*********************************************************************
843 LONG CDECL _lseek(int fd, LONG offset, int whence)
845 return _lseeki64(fd, offset, whence);
848 /*********************************************************************
849 * _locking (MSVCRT.@)
851 * This is untested; the underlying LockFile doesn't work yet.
853 int CDECL _locking(int fd, int mode, LONG nbytes)
857 HANDLE hand = msvcrt_fdtoh(fd);
859 TRACE(":fd (%d) handle (%p)\n",fd,hand);
860 if (hand == INVALID_HANDLE_VALUE)
863 if (mode < 0 || mode > 4)
865 *MSVCRT__errno() = MSVCRT_EINVAL;
869 TRACE(":fd (%d) by 0x%08x mode %s\n",
870 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
871 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
872 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
873 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
874 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
877 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
879 FIXME ("Seek failed\n");
880 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
883 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
886 ret = 1; /* just to satisfy gcc */
889 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
894 else if (mode == MSVCRT__LK_UNLCK)
895 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
897 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
898 /* FIXME - what about error settings? */
902 /*********************************************************************
905 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
907 /* Flush output if needed */
908 if(file->_flag & MSVCRT__IOWRT)
909 msvcrt_flush_buffer(file);
911 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
912 offset -= file->_cnt;
914 /* Discard buffered input */
916 file->_ptr = file->_base;
917 /* Reset direction of i/o */
918 if(file->_flag & MSVCRT__IORW) {
919 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
921 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
924 /*********************************************************************
927 int CDECL _chsize(int fd, long size)
933 TRACE("(fd=%d, size=%ld)\n", fd, size);
937 handle = msvcrt_fdtoh(fd);
938 if (handle != INVALID_HANDLE_VALUE)
940 /* save the current file pointer */
941 cur = _lseek(fd, 0, SEEK_CUR);
944 pos = _lseek(fd, size, SEEK_SET);
947 ret = SetEndOfFile(handle);
948 if (!ret) msvcrt_set_errno(GetLastError());
951 /* restore the file pointer */
952 _lseek(fd, cur, SEEK_SET);
960 /*********************************************************************
961 * clearerr (MSVCRT.@)
963 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
965 TRACE(":file (%p) fd (%d)\n",file,file->_file);
966 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
969 /*********************************************************************
972 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
974 TRACE(":file (%p) fd (%d)\n",file,file->_file);
975 MSVCRT_fseek(file, 0L, SEEK_SET);
976 MSVCRT_clearerr(file);
979 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
981 int plus = strchr(mode, '+') != NULL;
986 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
987 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
990 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
991 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
994 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
995 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1005 *open_flags |= MSVCRT__O_BINARY;
1006 *open_flags &= ~MSVCRT__O_TEXT;
1009 *open_flags |= MSVCRT__O_TEXT;
1010 *open_flags &= ~MSVCRT__O_BINARY;
1015 FIXME(":unknown flag %c not supported\n",mode[-1]);
1020 /*********************************************************************
1021 * _fdopen (MSVCRT.@)
1023 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1025 int open_flags, stream_flags;
1028 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1031 if (!(file = msvcrt_alloc_fp()))
1033 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1038 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1044 /*********************************************************************
1045 * _wfdopen (MSVCRT.@)
1047 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1049 unsigned mlen = strlenW(mode);
1050 char *modea = MSVCRT_calloc(mlen + 1, 1);
1051 MSVCRT_FILE* file = NULL;
1052 int open_flags, stream_flags;
1055 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1057 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1059 if (!(file = msvcrt_alloc_fp()))
1061 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1069 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1070 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1077 /*********************************************************************
1078 * _filelength (MSVCRT.@)
1080 LONG CDECL _filelength(int fd)
1082 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1085 LONG endPos = _lseek(fd, 0, SEEK_END);
1088 if (endPos != curPos)
1089 _lseek(fd, curPos, SEEK_SET);
1096 /*********************************************************************
1097 * _filelengthi64 (MSVCRT.@)
1099 __int64 CDECL _filelengthi64(int fd)
1101 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1104 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1107 if (endPos != curPos)
1108 _lseeki64(fd, curPos, SEEK_SET);
1115 /*********************************************************************
1116 * _fileno (MSVCRT.@)
1118 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1120 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1124 /*********************************************************************
1125 * _fstat64 (MSVCRT.@)
1127 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1131 BY_HANDLE_FILE_INFORMATION hfi;
1132 HANDLE hand = msvcrt_fdtoh(fd);
1134 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1135 if (hand == INVALID_HANDLE_VALUE)
1140 WARN(":failed-NULL buf\n");
1141 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1145 memset(&hfi, 0, sizeof(hfi));
1146 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1147 type = GetFileType(hand);
1148 if (type == FILE_TYPE_PIPE)
1150 buf->st_dev = buf->st_rdev = fd;
1151 buf->st_mode = S_IFIFO;
1154 else if (type == FILE_TYPE_CHAR)
1156 buf->st_dev = buf->st_rdev = fd;
1157 buf->st_mode = S_IFCHR;
1160 else /* FILE_TYPE_DISK etc. */
1162 if (!GetFileInformationByHandle(hand, &hfi))
1164 WARN(":failed-last error (%d)\n",GetLastError());
1165 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1168 buf->st_mode = S_IFREG | S_IREAD;
1169 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1170 buf->st_mode |= S_IWRITE;
1171 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1172 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1174 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1175 buf->st_mtime = buf->st_ctime = dw;
1176 buf->st_nlink = hfi.nNumberOfLinks;
1178 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1183 /*********************************************************************
1184 * _fstati64 (MSVCRT.@)
1186 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1189 struct MSVCRT__stat64 buf64;
1191 ret = MSVCRT__fstat64(fd, &buf64);
1193 msvcrt_stat64_to_stati64(&buf64, buf);
1197 /*********************************************************************
1200 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1202 struct MSVCRT__stat64 buf64;
1204 ret = MSVCRT__fstat64(fd, &buf64);
1206 msvcrt_stat64_to_stat(&buf64, buf);
1210 /*********************************************************************
1211 * _futime (MSVCRT.@)
1213 int CDECL _futime(int fd, struct MSVCRT__utimbuf *t)
1215 HANDLE hand = msvcrt_fdtoh(fd);
1220 MSVCRT_time_t currTime;
1221 MSVCRT_time(&currTime);
1222 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1223 memcpy(&wt, &at, sizeof(wt));
1227 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1228 if (t->actime == t->modtime)
1229 memcpy(&wt, &at, sizeof(wt));
1231 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1234 if (!SetFileTime(hand, NULL, &at, &wt))
1236 msvcrt_set_errno(GetLastError());
1242 /*********************************************************************
1243 * _get_osfhandle (MSVCRT.@)
1245 long CDECL _get_osfhandle(int fd)
1247 HANDLE hand = msvcrt_fdtoh(fd);
1248 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1253 /*********************************************************************
1254 * _isatty (MSVCRT.@)
1256 int CDECL _isatty(int fd)
1258 HANDLE hand = msvcrt_fdtoh(fd);
1260 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1261 if (hand == INVALID_HANDLE_VALUE)
1264 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1267 /*********************************************************************
1268 * _mktemp (MSVCRT.@)
1270 char * CDECL _mktemp(char *pattern)
1273 char *retVal = pattern;
1278 numX = (*pattern++ == 'X')? numX + 1 : 0;
1282 id = GetCurrentProcessId();
1286 int tempNum = id / 10;
1287 *pattern-- = id - (tempNum * 10) + '0';
1293 *pattern = letter++;
1294 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1295 GetLastError() == ERROR_FILE_NOT_FOUND)
1297 } while(letter <= 'z');
1301 /*********************************************************************
1302 * _wmktemp (MSVCRT.@)
1304 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1307 MSVCRT_wchar_t *retVal = pattern;
1309 MSVCRT_wchar_t letter = 'a';
1312 numX = (*pattern++ == 'X')? numX + 1 : 0;
1316 id = GetCurrentProcessId();
1320 int tempNum = id / 10;
1321 *pattern-- = id - (tempNum * 10) + '0';
1327 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1328 GetLastError() == ERROR_FILE_NOT_FOUND)
1330 *pattern = letter++;
1331 } while(letter != '|');
1335 static unsigned split_oflags(unsigned oflags)
1338 unsigned unsupp; /* until we support everything */
1340 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1341 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1342 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1343 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1344 else wxflags |= WX_TEXT; /* default to TEXT*/
1345 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1347 if ((unsupp = oflags & ~(
1348 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1349 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1350 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1351 MSVCRT__O_NOINHERIT|
1352 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1354 ERR(":unsupported oflags 0x%04x\n",unsupp);
1359 /*********************************************************************
1362 int CDECL _pipe(int *pfds, unsigned int psize, int textmode)
1365 SECURITY_ATTRIBUTES sa;
1366 HANDLE readHandle, writeHandle;
1370 *MSVCRT__errno() = MSVCRT_EINVAL;
1374 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1375 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1376 sa.lpSecurityDescriptor = NULL;
1377 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1379 unsigned int wxflags = split_oflags(textmode);
1383 fd = msvcrt_alloc_fd(readHandle, wxflags);
1387 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1396 CloseHandle(writeHandle);
1397 *MSVCRT__errno() = MSVCRT_EMFILE;
1402 CloseHandle(readHandle);
1403 CloseHandle(writeHandle);
1404 *MSVCRT__errno() = MSVCRT_EMFILE;
1409 msvcrt_set_errno(GetLastError());
1414 /*********************************************************************
1417 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1421 DWORD access = 0, creation = 0, attrib;
1425 SECURITY_ATTRIBUTES sa;
1428 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1429 path, oflags, shflags);
1431 wxflag = split_oflags(oflags);
1432 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1434 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1435 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1436 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1439 if (oflags & MSVCRT__O_CREAT)
1441 va_start(ap, shflags);
1442 pmode = va_arg(ap, int);
1445 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1446 FIXME(": pmode 0x%04x ignored\n", pmode);
1448 WARN(": pmode 0x%04x ignored\n", pmode);
1450 if (oflags & MSVCRT__O_EXCL)
1451 creation = CREATE_NEW;
1452 else if (oflags & MSVCRT__O_TRUNC)
1453 creation = CREATE_ALWAYS;
1455 creation = OPEN_ALWAYS;
1457 else /* no MSVCRT__O_CREAT */
1459 if (oflags & MSVCRT__O_TRUNC)
1460 creation = TRUNCATE_EXISTING;
1462 creation = OPEN_EXISTING;
1467 case MSVCRT__SH_DENYRW:
1470 case MSVCRT__SH_DENYWR:
1471 sharing = FILE_SHARE_READ;
1473 case MSVCRT__SH_DENYRD:
1474 sharing = FILE_SHARE_WRITE;
1476 case MSVCRT__SH_DENYNO:
1477 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1480 ERR( "Unhandled shflags 0x%x\n", shflags );
1483 attrib = FILE_ATTRIBUTE_NORMAL;
1485 if (oflags & MSVCRT__O_TEMPORARY)
1487 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1489 sharing |= FILE_SHARE_DELETE;
1492 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1493 sa.lpSecurityDescriptor = NULL;
1494 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1496 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1498 if (hand == INVALID_HANDLE_VALUE) {
1499 WARN(":failed-last error (%d)\n",GetLastError());
1500 msvcrt_set_errno(GetLastError());
1504 fd = msvcrt_alloc_fd(hand, wxflag);
1506 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1510 /*********************************************************************
1511 * _wsopen (MSVCRT.@)
1513 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1515 const unsigned int len = strlenW(path);
1516 char *patha = MSVCRT_calloc(len + 1,1);
1520 va_start(ap, shflags);
1521 pmode = va_arg(ap, int);
1524 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1526 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1531 msvcrt_set_errno(GetLastError());
1535 /*********************************************************************
1538 int CDECL _open( const char *path, int flags, ... )
1542 if (flags & MSVCRT__O_CREAT)
1545 va_start(ap, flags);
1546 pmode = va_arg(ap, int);
1548 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1551 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1554 /*********************************************************************
1557 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1559 const unsigned int len = strlenW(path);
1560 char *patha = MSVCRT_calloc(len + 1,1);
1564 va_start(ap, flags);
1565 pmode = va_arg(ap, int);
1568 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1570 int retval = _open(patha,flags,pmode);
1575 msvcrt_set_errno(GetLastError());
1579 /*********************************************************************
1582 int CDECL _creat(const char *path, int flags)
1584 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1585 return _open(path, usedFlags);
1588 /*********************************************************************
1589 * _wcreat (MSVCRT.@)
1591 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1593 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1594 return _wopen(path, usedFlags);
1597 /*********************************************************************
1598 * _open_osfhandle (MSVCRT.@)
1600 int CDECL _open_osfhandle(long handle, int oflags)
1604 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1605 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1606 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1607 * text - it never sets MSVCRT__O_BINARY.
1609 /* don't let split_oflags() decide the mode if no mode is passed */
1610 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1611 oflags |= MSVCRT__O_BINARY;
1613 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1614 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1618 /*********************************************************************
1621 int CDECL _rmtmp(void)
1623 int num_removed = 0, i;
1626 for (i = 3; i < MSVCRT_stream_idx; i++)
1627 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1629 MSVCRT_fclose(MSVCRT_fstreams[i]);
1635 TRACE(":removed (%d) temp files\n",num_removed);
1639 /*********************************************************************
1640 * (internal) remove_cr
1642 * Remove all \r inplace.
1643 * return the number of \r removed
1645 static unsigned int remove_cr(char *buf, unsigned int count)
1649 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1650 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1654 /*********************************************************************
1657 static int read_i(int fd, void *buf, unsigned int count)
1660 char *bufstart = buf;
1661 HANDLE hand = msvcrt_fdtoh(fd);
1663 /* Don't trace small reads, it gets *very* annoying */
1665 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1666 if (hand == INVALID_HANDLE_VALUE)
1669 /* Reading single bytes in O_TEXT mode makes things slow
1670 * So read big chunks
1672 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1674 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1677 /* in text mode, a ctrl-z signals EOF */
1678 for (i=0; i<num_read; i++)
1680 if (bufstart[i] == 0x1a)
1687 if (num_read != count)
1689 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1690 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1695 if (GetLastError() == ERROR_BROKEN_PIPE)
1697 TRACE(":end-of-pipe\n");
1702 TRACE(":failed-last error (%d)\n",GetLastError());
1708 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1712 /*********************************************************************
1715 int CDECL _read(int fd, void *buf, unsigned int count)
1718 num_read = read_i(fd, buf, count);
1719 if (num_read>0 && MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1721 num_read -= remove_cr(buf,num_read);
1726 /*********************************************************************
1727 * _setmode (MSVCRT.@)
1729 int CDECL _setmode(int fd,int mode)
1731 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1732 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1733 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1734 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1735 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1737 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1741 /*********************************************************************
1742 * _stat64 (MSVCRT.@)
1744 int CDECL MSVCRT__stat64(const char* path, struct MSVCRT__stat64 * buf)
1747 WIN32_FILE_ATTRIBUTE_DATA hfi;
1748 unsigned short mode = ALL_S_IREAD;
1751 TRACE(":file (%s) buf(%p)\n",path,buf);
1753 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1755 TRACE("failed (%d)\n",GetLastError());
1756 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1760 memset(buf,0,sizeof(struct MSVCRT__stat64));
1762 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1763 Bon 011120: This FIXME seems incorrect
1764 Also a letter as first char isn't enough to be classified
1767 if (isalpha(*path)&& (*(path+1)==':'))
1768 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1770 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1772 plen = strlen(path);
1774 /* Dir, or regular file? */
1775 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1776 (path[plen-1] == '\\'))
1777 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1780 mode |= MSVCRT__S_IFREG;
1782 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1784 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1785 (tolower(path[plen-3]) << 16);
1786 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1787 mode |= ALL_S_IEXEC;
1791 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1792 mode |= ALL_S_IWRITE;
1794 buf->st_mode = mode;
1796 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1797 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1799 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1800 buf->st_mtime = buf->st_ctime = dw;
1801 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1802 (long)(buf->st_size >> 32),(long)buf->st_size,
1803 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1807 /*********************************************************************
1808 * _stati64 (MSVCRT.@)
1810 int CDECL MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1813 struct MSVCRT__stat64 buf64;
1815 ret = MSVCRT__stat64(path, &buf64);
1817 msvcrt_stat64_to_stati64(&buf64, buf);
1821 /*********************************************************************
1824 int CDECL MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1826 struct MSVCRT__stat64 buf64;
1828 ret = MSVCRT__stat64( path, &buf64);
1830 msvcrt_stat64_to_stat(&buf64, buf);
1834 /*********************************************************************
1835 * _wstat64 (MSVCRT.@)
1837 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1840 WIN32_FILE_ATTRIBUTE_DATA hfi;
1841 unsigned short mode = ALL_S_IREAD;
1844 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1846 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1848 TRACE("failed (%d)\n",GetLastError());
1849 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1853 memset(buf,0,sizeof(struct MSVCRT__stat64));
1855 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1856 if (MSVCRT_iswalpha(*path))
1857 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1859 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1861 plen = strlenW(path);
1863 /* Dir, or regular file? */
1864 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1865 (path[plen-1] == '\\'))
1866 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1869 mode |= MSVCRT__S_IFREG;
1871 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1873 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1874 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1875 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1876 mode |= ALL_S_IEXEC;
1880 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1881 mode |= ALL_S_IWRITE;
1883 buf->st_mode = mode;
1885 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1886 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1888 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1889 buf->st_mtime = buf->st_ctime = dw;
1890 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1891 (long)(buf->st_size >> 32),(long)buf->st_size,
1892 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1896 /*********************************************************************
1897 * _wstati64 (MSVCRT.@)
1899 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1902 struct MSVCRT__stat64 buf64;
1904 ret = MSVCRT__wstat64(path, &buf64);
1906 msvcrt_stat64_to_stati64(&buf64, buf);
1910 /*********************************************************************
1913 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1916 struct MSVCRT__stat64 buf64;
1918 ret = MSVCRT__wstat64( path, &buf64 );
1919 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
1923 /*********************************************************************
1926 long CDECL _tell(int fd)
1928 return _lseek(fd, 0, SEEK_CUR);
1931 /*********************************************************************
1932 * _telli64 (MSVCRT.@)
1934 __int64 CDECL _telli64(int fd)
1936 return _lseeki64(fd, 0, SEEK_CUR);
1939 /*********************************************************************
1940 * _tempnam (MSVCRT.@)
1942 char * CDECL _tempnam(const char *dir, const char *prefix)
1944 char tmpbuf[MAX_PATH];
1945 const char *tmp_dir = MSVCRT_getenv("TMP");
1947 if (tmp_dir) dir = tmp_dir;
1949 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1950 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1952 TRACE("got name (%s)\n",tmpbuf);
1953 DeleteFileA(tmpbuf);
1954 return _strdup(tmpbuf);
1956 TRACE("failed (%d)\n",GetLastError());
1960 /*********************************************************************
1961 * _wtempnam (MSVCRT.@)
1963 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1965 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1967 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1968 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1970 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1971 DeleteFileW(tmpbuf);
1972 return _wcsdup(tmpbuf);
1974 TRACE("failed (%d)\n",GetLastError());
1978 /*********************************************************************
1981 int CDECL _umask(int umask)
1983 int old_umask = MSVCRT_umask;
1984 TRACE("(%d)\n",umask);
1985 MSVCRT_umask = umask;
1989 /*********************************************************************
1992 int CDECL _utime(const char* path, struct MSVCRT__utimbuf *t)
1994 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1998 int retVal = _futime(fd, t);
2005 /*********************************************************************
2006 * _wutime (MSVCRT.@)
2008 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
2010 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2014 int retVal = _futime(fd, t);
2021 /*********************************************************************
2024 int CDECL _write(int fd, const void* buf, unsigned int count)
2027 HANDLE hand = msvcrt_fdtoh(fd);
2029 /* Don't trace small writes, it gets *very* annoying */
2032 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2034 if (hand == INVALID_HANDLE_VALUE)
2036 *MSVCRT__errno() = MSVCRT_EBADF;
2040 /* If appending, go to EOF */
2041 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2042 _lseek(fd, 0, FILE_END);
2044 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2046 if (WriteFile(hand, buf, count, &num_written, NULL)
2047 && (num_written == count))
2049 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2050 hand, GetLastError());
2051 *MSVCRT__errno() = MSVCRT_ENOSPC;
2055 unsigned int i, j, nr_lf;
2058 const char *s = (const char *)buf, *buf_start = (const char *)buf;
2059 /* find number of \n ( without preceding \r ) */
2060 for ( nr_lf=0,i = 0; i <count; i++)
2065 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2070 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2072 for (s = (const char *)buf, i = 0, j = 0; i < count; i++)
2077 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2084 FIXME("Malloc failed\n");
2092 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2094 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2095 fd, hand, GetLastError(), num_written);
2096 *MSVCRT__errno() = MSVCRT_ENOSPC;
2099 return s - buf_start;
2111 /*********************************************************************
2114 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2117 len = _write(file->_file, &val, sizeof(val));
2118 if (len == sizeof(val)) return val;
2119 file->_flag |= MSVCRT__IOERR;
2123 /*********************************************************************
2126 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2131 MSVCRT_free(file->_tmpfname);
2132 file->_tmpfname = NULL;
2133 /* flush stdio buffers */
2134 if(file->_flag & MSVCRT__IOWRT)
2135 MSVCRT_fflush(file);
2136 if(file->_flag & MSVCRT__IOMYBUF)
2137 MSVCRT_free(file->_base);
2139 r=_close(file->_file);
2143 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2146 /*********************************************************************
2149 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2151 return file->_flag & MSVCRT__IOEOF;
2154 /*********************************************************************
2157 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2159 return file->_flag & MSVCRT__IOERR;
2162 /*********************************************************************
2163 * _filbuf (MSVCRT.@)
2165 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2167 /* Allocate buffer if needed */
2168 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2169 msvcrt_alloc_buffer(file);
2171 if(!(file->_flag & MSVCRT__IOREAD)) {
2172 if(file->_flag & MSVCRT__IORW) {
2173 file->_flag |= MSVCRT__IOREAD;
2178 if(file->_flag & MSVCRT__IONBF) {
2181 if ((r = read_i(file->_file,&c,1)) != 1) {
2182 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2187 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2189 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2194 file->_ptr = file->_base+1;
2195 return *(unsigned char *)file->_base;
2199 /*********************************************************************
2202 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2209 i = (unsigned char *)file->_ptr++;
2212 j = MSVCRT__filbuf(file);
2213 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) || (j != '\r'))
2218 /*********************************************************************
2219 * _fgetchar (MSVCRT.@)
2221 int CDECL _fgetchar(void)
2223 return MSVCRT_fgetc(MSVCRT_stdin);
2226 /*********************************************************************
2229 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2231 int cc = MSVCRT_EOF;
2232 char * buf_start = s;
2234 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2235 file,file->_file,s,size);
2237 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2242 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2244 TRACE(":nothing read\n");
2247 if ((cc != MSVCRT_EOF) && (size > 1))
2250 TRACE(":got %s\n", debugstr_a(buf_start));
2254 /*********************************************************************
2257 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2258 * the CR from CR/LF combinations
2260 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2264 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2270 for(i=0; i<sizeof(wc); i++)
2280 j = MSVCRT__filbuf(file);
2283 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2293 c = MSVCRT_fgetc(file);
2294 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2296 FIXME("Treat Multibyte characters\n");
2298 if (c == MSVCRT_EOF)
2301 return (MSVCRT_wint_t)c;
2304 /*********************************************************************
2307 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2312 for (j=0; j<sizeof(int); j++) {
2313 k = MSVCRT_fgetc(file);
2314 if (k == MSVCRT_EOF) {
2315 file->_flag |= MSVCRT__IOEOF;
2323 /*********************************************************************
2326 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2328 return MSVCRT_fgetwc(file);
2331 /*********************************************************************
2332 * _fgetwchar (MSVCRT.@)
2334 MSVCRT_wint_t CDECL _fgetwchar(void)
2336 return MSVCRT_fgetwc(MSVCRT_stdin);
2339 /*********************************************************************
2340 * getwchar (MSVCRT.@)
2342 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2344 return _fgetwchar();
2347 /*********************************************************************
2350 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2352 int cc = MSVCRT_WEOF;
2353 MSVCRT_wchar_t * buf_start = s;
2355 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2356 file,file->_file,s,size);
2358 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2363 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2365 TRACE(":nothing read\n");
2368 if ((cc != MSVCRT_WEOF) && (size > 1))
2371 TRACE(":got %s\n", debugstr_w(buf_start));
2375 /*********************************************************************
2378 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2380 MSVCRT_size_t wrcnt=size * nmemb;
2385 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2386 memcpy(file->_ptr, ptr, pcnt);
2391 ptr = (const char*)ptr + pcnt;
2392 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2393 if(file->_flag & MSVCRT__IORW) {
2394 file->_flag |= MSVCRT__IOWRT;
2400 int res=msvcrt_flush_buffer(file);
2402 int pwritten = _write(file->_file, ptr, wrcnt);
2405 file->_flag |= MSVCRT__IOERR;
2408 written += pwritten;
2411 return written / size;
2414 /*********************************************************************
2417 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2419 MSVCRT_wchar_t mwc=wc;
2420 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2425 /*********************************************************************
2426 * _fputwchar (MSVCRT.@)
2428 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2430 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2433 /*********************************************************************
2434 * _fsopen (MSVCRT.@)
2436 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2439 int open_flags, stream_flags, fd;
2441 TRACE("(%s,%s)\n",path,mode);
2443 /* map mode string to open() flags. "man fopen" for possibilities. */
2444 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2448 fd = MSVCRT__sopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2451 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2453 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2460 TRACE(":got (%p)\n",file);
2461 if (fd >= 0 && !file)
2467 /*********************************************************************
2468 * _wfsopen (MSVCRT.@)
2470 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2472 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2473 char *patha = MSVCRT_calloc(plen + 1, 1);
2474 char *modea = MSVCRT_calloc(mlen + 1, 1);
2476 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2478 if (patha && modea &&
2479 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2480 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2482 MSVCRT_FILE *retval = MSVCRT__fsopen(patha,modea,share);
2488 msvcrt_set_errno(GetLastError());
2492 /*********************************************************************
2495 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2497 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2500 /*********************************************************************
2501 * _wfopen (MSVCRT.@)
2503 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2505 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2508 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2509 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2511 /*********************************************************************
2514 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2521 int res = msvcrt_flush_buffer(file);
2522 return res ? res : c;
2527 return MSVCRT__flsbuf(c, file);
2531 /*********************************************************************
2532 * _flsbuf (MSVCRT.@)
2534 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2536 /* Flush output buffer */
2537 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2538 msvcrt_alloc_buffer(file);
2540 if(!(file->_flag & MSVCRT__IOWRT)) {
2541 if(file->_flag & MSVCRT__IORW) {
2542 file->_flag |= MSVCRT__IOWRT;
2548 int res=msvcrt_flush_buffer(file);
2549 return res?res : MSVCRT_fputc(c, file);
2553 len = _write(file->_file, &cc, 1);
2554 if (len == 1) return c;
2555 file->_flag |= MSVCRT__IOERR;
2560 /*********************************************************************
2561 * _fputchar (MSVCRT.@)
2563 int CDECL _fputchar(int c)
2565 return MSVCRT_fputc(c, MSVCRT_stdout);
2568 /*********************************************************************
2571 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2572 { MSVCRT_size_t rcnt=size * nmemb;
2573 MSVCRT_size_t read=0;
2579 /* first buffered data */
2581 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2582 memcpy(ptr, file->_ptr, pcnt);
2585 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
2586 pcnt -= remove_cr(ptr,pcnt);
2589 ptr = (char*)ptr + pcnt;
2590 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2591 if(file->_flag & MSVCRT__IORW) {
2592 file->_flag |= MSVCRT__IOREAD;
2599 /* Fill the buffer on small reads.
2600 * TODO: Use a better buffering strategy.
2602 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2603 if (file->_bufsiz == 0) {
2604 msvcrt_alloc_buffer(file);
2606 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2607 file->_ptr = file->_base;
2608 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2609 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2610 if (i > 0 && i < file->_cnt) {
2611 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2612 file->_flag &= ~MSVCRT__IOEOF;
2615 memcpy(ptr, file->_ptr, i);
2620 i = _read(file->_file,ptr, rcnt);
2624 ptr = (char *)ptr+i;
2625 /* expose feof condition in the flags
2626 * MFC tests file->_flag for feof, and doesn't call feof())
2628 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2629 file->_flag |= MSVCRT__IOEOF;
2632 file->_flag |= MSVCRT__IOERR;
2642 /*********************************************************************
2643 * freopen (MSVCRT.@)
2646 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2648 int open_flags, stream_flags, fd;
2650 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2653 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2657 MSVCRT_fclose(file);
2658 /* map mode string to open() flags. "man fopen" for possibilities. */
2659 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2663 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2666 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2669 WARN(":failed-last error (%d)\n",GetLastError());
2670 msvcrt_set_errno(GetLastError());
2679 /*********************************************************************
2680 * fsetpos (MSVCRT.@)
2682 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2684 /* Note that all this has been lifted 'as is' from fseek */
2685 if(file->_flag & MSVCRT__IOWRT)
2686 msvcrt_flush_buffer(file);
2688 /* Discard buffered input */
2690 file->_ptr = file->_base;
2692 /* Reset direction of i/o */
2693 if(file->_flag & MSVCRT__IORW) {
2694 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2697 return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2700 /*********************************************************************
2703 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2708 if( file->_flag & MSVCRT__IOWRT ) {
2709 off = file->_ptr - file->_base;
2714 pos = _tell(file->_file);
2715 if(pos == -1) return pos;
2719 /*********************************************************************
2720 * fgetpos (MSVCRT.@)
2722 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2724 /* This code has been lifted form the MSVCRT_ftell function */
2727 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2729 if (*pos == -1) return -1;
2732 if( file->_flag & MSVCRT__IOWRT ) {
2733 off = file->_ptr - file->_base;
2743 /*********************************************************************
2746 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2748 size_t i, len = strlen(s);
2749 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2750 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2751 for (i=0; i<len; i++)
2752 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2757 /*********************************************************************
2760 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2762 size_t i, len = strlenW(s);
2763 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2764 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2765 for (i=0; i<len; i++)
2767 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2769 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2775 /*********************************************************************
2776 * getchar (MSVCRT.@)
2778 int CDECL MSVCRT_getchar(void)
2780 return MSVCRT_fgetc(MSVCRT_stdin);
2783 /*********************************************************************
2786 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
2788 return MSVCRT_fgetc(file);
2791 /*********************************************************************
2794 char * CDECL MSVCRT_gets(char *buf)
2797 char * buf_start = buf;
2799 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2800 cc = MSVCRT_fgetc(MSVCRT_stdin))
2801 if(cc != '\r') *buf++ = (char)cc;
2805 TRACE("got '%s'\n", buf_start);
2809 /*********************************************************************
2812 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
2815 MSVCRT_wchar_t* ws = buf;
2817 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2818 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2821 *buf++ = (MSVCRT_wchar_t)cc;
2825 TRACE("got %s\n", debugstr_w(ws));
2829 /*********************************************************************
2832 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
2834 return MSVCRT_fputc(c, file);
2837 /*********************************************************************
2838 * putchar (MSVCRT.@)
2840 int CDECL MSVCRT_putchar(int c)
2842 return MSVCRT_fputc(c, MSVCRT_stdout);
2845 /*********************************************************************
2848 int CDECL MSVCRT_puts(const char *s)
2850 size_t len = strlen(s);
2851 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2852 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2855 /*********************************************************************
2858 int CDECL _putws(const MSVCRT_wchar_t *s)
2860 static const MSVCRT_wchar_t nl = '\n';
2861 size_t len = strlenW(s);
2862 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2863 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2866 /*********************************************************************
2869 int CDECL MSVCRT_remove(const char *path)
2871 TRACE("(%s)\n",path);
2872 if (DeleteFileA(path))
2874 TRACE(":failed (%d)\n",GetLastError());
2875 msvcrt_set_errno(GetLastError());
2879 /*********************************************************************
2880 * _wremove (MSVCRT.@)
2882 int CDECL _wremove(const MSVCRT_wchar_t *path)
2884 TRACE("(%s)\n",debugstr_w(path));
2885 if (DeleteFileW(path))
2887 TRACE(":failed (%d)\n",GetLastError());
2888 msvcrt_set_errno(GetLastError());
2892 /*********************************************************************
2895 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
2897 TRACE(":from %s to %s\n",oldpath,newpath);
2898 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2900 TRACE(":failed (%d)\n",GetLastError());
2901 msvcrt_set_errno(GetLastError());
2905 /*********************************************************************
2906 * _wrename (MSVCRT.@)
2908 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2910 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2911 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2913 TRACE(":failed (%d)\n",GetLastError());
2914 msvcrt_set_errno(GetLastError());
2918 /*********************************************************************
2919 * setvbuf (MSVCRT.@)
2921 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2923 /* TODO: Check if file busy */
2925 MSVCRT_free(file->_base);
2929 if(mode == MSVCRT__IOFBF) {
2930 file->_flag &= ~MSVCRT__IONBF;
2931 file->_base = file->_ptr = buf;
2933 file->_bufsiz = size;
2936 file->_flag |= MSVCRT__IONBF;
2941 /*********************************************************************
2944 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2946 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2949 /*********************************************************************
2952 char * CDECL MSVCRT_tmpnam(char *s)
2960 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2961 p = s + sprintf(s, "\\s%s.", tmpstr);
2962 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2964 msvcrt_int_to_base32(unique++, tmpstr);
2966 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2967 GetLastError() == ERROR_FILE_NOT_FOUND)
2973 /*********************************************************************
2974 * tmpfile (MSVCRT.@)
2976 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
2978 char *filename = MSVCRT_tmpnam(NULL);
2980 MSVCRT_FILE* file = NULL;
2983 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2984 if (fd != -1 && (file = msvcrt_alloc_fp()))
2986 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2991 else file->_tmpfname = _strdup(filename);
2997 /*********************************************************************
2998 * vfprintf (MSVCRT.@)
3000 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
3002 char buf[2048], *mem = buf;
3003 int written, resize = sizeof(buf), retval;
3004 /* There are two conventions for vsnprintf failing:
3005 * Return -1 if we truncated, or
3006 * Return the number of bytes that would have been written
3007 * The code below handles both cases
3009 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3012 resize = (written == -1 ? resize * 2 : written + 1);
3015 if (!(mem = (char *)MSVCRT_malloc(resize)))
3018 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3024 /*********************************************************************
3025 * vfwprintf (MSVCRT.@)
3027 * Is final char included in written (then resize is too big) or not
3028 * (then we must test for equality too)?
3030 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
3032 MSVCRT_wchar_t buf[2048], *mem = buf;
3033 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3034 /* See vfprintf comments */
3035 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3038 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3041 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
3044 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3050 /*********************************************************************
3051 * vprintf (MSVCRT.@)
3053 int CDECL MSVCRT_vprintf(const char *format, va_list valist)
3055 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3058 /*********************************************************************
3059 * vwprintf (MSVCRT.@)
3061 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
3063 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3066 /*********************************************************************
3067 * fprintf (MSVCRT.@)
3069 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3073 va_start(valist, format);
3074 res = MSVCRT_vfprintf(file, format, valist);
3079 /*********************************************************************
3080 * fwprintf (MSVCRT.@)
3082 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3086 va_start(valist, format);
3087 res = MSVCRT_vfwprintf(file, format, valist);
3092 /*********************************************************************
3095 int CDECL MSVCRT_printf(const char *format, ...)
3099 va_start(valist, format);
3100 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3105 /*********************************************************************
3108 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3110 if (c == MSVCRT_EOF)
3112 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3113 msvcrt_alloc_buffer(file);
3116 if(file->_ptr>file->_base) {
3120 MSVCRT_clearerr(file);
3126 /*********************************************************************
3127 * ungetwc (MSVCRT.@)
3129 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3131 MSVCRT_wchar_t mwc = wc;
3132 char * pp = (char *)&mwc;
3134 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3135 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3141 /*********************************************************************
3142 * wprintf (MSVCRT.@)
3144 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3148 va_start(valist, format);
3149 res = MSVCRT_vwprintf(format, valist);
3154 /*********************************************************************
3155 * _getmaxstdio (MSVCRT.@)
3157 int CDECL _getmaxstdio(void)
3159 FIXME("stub, always returns 512\n");
3163 /*********************************************************************
3164 * _setmaxstdio_ (MSVCRT.@)
3166 int CDECL _setmaxstdio(int newmax)
3173 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3177 /*********************************************************************
3178 * __pioinfo (MSVCRT.@)
3179 * FIXME: see MSVCRT_MAX_FILES define.
3181 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3182 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3183 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3184 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3185 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3186 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3187 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3188 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3189 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3190 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3191 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3192 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3195 /*********************************************************************
3196 * __badioinfo (MSVCRT.@)
3198 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };