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_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
63 #define WX_DONTINHERIT 0x10
64 #define WX_APPEND 0x20
67 /* FIXME: this should be allocated dynamically */
68 #define MSVCRT_MAX_FILES 2048
73 DWORD unkn[7]; /* critical section and init flag */
76 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
78 MSVCRT_FILE MSVCRT__iob[3] = { { 0 } };
80 static int MSVCRT_fdstart = 3; /* first unallocated fd */
81 static int MSVCRT_fdend = 3; /* highest allocated fd */
83 static MSVCRT_FILE* MSVCRT_fstreams[2048];
84 static int MSVCRT_stream_idx;
86 /* INTERNAL: process umask */
87 static int MSVCRT_umask = 0;
89 /* INTERNAL: Static buffer for temp file name */
90 static char MSVCRT_tmpname[MAX_PATH];
92 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
93 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
94 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
95 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
97 #define TOUL(x) (ULONGLONG)(x)
98 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
99 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
100 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
101 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
103 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
104 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
105 * and MSVCRT_stream_idx, from race conditions.
106 * It doesn't protect against race conditions manipulating the underlying files
107 * or flags; doing so would probably be better accomplished with per-file
108 * protection, rather than locking the whole table for every change.
110 static CRITICAL_SECTION MSVCRT_file_cs;
111 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
112 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
114 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
116 buf->st_dev = buf64->st_dev;
117 buf->st_ino = buf64->st_ino;
118 buf->st_mode = buf64->st_mode;
119 buf->st_nlink = buf64->st_nlink;
120 buf->st_uid = buf64->st_uid;
121 buf->st_gid = buf64->st_gid;
122 buf->st_rdev = buf64->st_rdev;
123 buf->st_size = buf64->st_size;
124 buf->st_atime = buf64->st_atime;
125 buf->st_mtime = buf64->st_mtime;
126 buf->st_ctime = buf64->st_ctime;
129 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
131 buf->st_dev = buf64->st_dev;
132 buf->st_ino = buf64->st_ino;
133 buf->st_mode = buf64->st_mode;
134 buf->st_nlink = buf64->st_nlink;
135 buf->st_uid = buf64->st_uid;
136 buf->st_gid = buf64->st_gid;
137 buf->st_rdev = buf64->st_rdev;
138 buf->st_size = buf64->st_size;
139 buf->st_atime = buf64->st_atime;
140 buf->st_mtime = buf64->st_mtime;
141 buf->st_ctime = buf64->st_ctime;
144 static inline BOOL msvcrt_is_valid_fd(int fd)
146 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
149 /* INTERNAL: Get the HANDLE for a fd
150 * This doesn't lock the table, because a failure will result in
151 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
152 * it returns a valid handle which is about to be closed, a subsequent call
153 * will fail, most likely in a sane way.
155 static HANDLE msvcrt_fdtoh(int fd)
157 if (!msvcrt_is_valid_fd(fd))
159 WARN(":fd (%d) - no handle!\n",fd);
160 *MSVCRT___doserrno() = 0;
161 *MSVCRT__errno() = MSVCRT_EBADF;
162 return INVALID_HANDLE_VALUE;
164 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
165 return MSVCRT_fdesc[fd].handle;
168 /* INTERNAL: free a file entry fd */
169 static void msvcrt_free_fd(int fd)
172 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
173 MSVCRT_fdesc[fd].wxflag = 0;
174 TRACE(":fd (%d) freed\n",fd);
175 if (fd < 3) /* don't use 0,1,2 for user files */
179 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
180 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
181 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
186 if (fd == MSVCRT_fdend - 1)
188 if (fd < MSVCRT_fdstart)
194 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
195 /* caller must hold the files lock */
196 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
198 if (fd >= MSVCRT_MAX_FILES)
200 WARN(":files exhausted!\n");
203 MSVCRT_fdesc[fd].handle = hand;
204 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
206 /* locate next free slot */
207 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
208 MSVCRT_fdstart = MSVCRT_fdend + 1;
210 while (MSVCRT_fdstart < MSVCRT_fdend &&
211 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
213 /* update last fd in use */
214 if (fd >= MSVCRT_fdend)
215 MSVCRT_fdend = fd + 1;
216 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
220 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
221 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
222 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
228 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
229 static int msvcrt_alloc_fd(HANDLE hand, int flag)
234 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
235 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
240 /* INTERNAL: Allocate a FILE* for an fd slot */
241 /* caller must hold the files lock */
242 static MSVCRT_FILE* msvcrt_alloc_fp(void)
246 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
248 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
250 if (!MSVCRT_fstreams[i])
252 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
254 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
256 return MSVCRT_fstreams[i];
262 /* INTERNAL: initialize a FILE* from an open fd */
263 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
265 TRACE(":fd (%d) allocating FILE*\n",fd);
266 if (!msvcrt_is_valid_fd(fd))
268 WARN(":invalid fd %d\n",fd);
269 *MSVCRT___doserrno() = 0;
270 *MSVCRT__errno() = MSVCRT_EBADF;
273 memset(file, 0, sizeof(*file));
275 file->_flag = stream_flags;
277 TRACE(":got FILE* (%p)\n",file);
281 /* INTERNAL: Create an inheritance data block (for spawned process)
282 * The inheritance block is made of:
283 * 00 int nb of file descriptor (NBFD)
284 * 04 char file flags (wxflag): repeated for each fd
285 * 4+NBFD HANDLE file handle: repeated for each fd
287 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
293 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
294 *block = MSVCRT_calloc(*size, 1);
300 wxflag_ptr = (char*)*block + sizeof(unsigned);
301 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
303 *(unsigned*)*block = MSVCRT_fdend;
304 for (fd = 0; fd < MSVCRT_fdend; fd++)
306 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
307 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
309 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
310 *handle_ptr = MSVCRT_fdesc[fd].handle;
315 *handle_ptr = INVALID_HANDLE_VALUE;
317 wxflag_ptr++; handle_ptr++;
322 /* INTERNAL: Set up all file descriptors,
323 * as well as default streams (stdin, stderr and stdout)
325 void msvcrt_init_io(void)
330 InitializeCriticalSection(&MSVCRT_file_cs);
331 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
332 GetStartupInfoA(&si);
333 if (si.cbReserved2 != 0 && si.lpReserved2 != NULL)
338 MSVCRT_fdend = *(unsigned*)si.lpReserved2;
340 wxflag_ptr = (char*)(si.lpReserved2 + sizeof(unsigned));
341 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
343 MSVCRT_fdend = min(MSVCRT_fdend, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
344 for (i = 0; i < MSVCRT_fdend; i++)
346 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
348 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
349 MSVCRT_fdesc[i].handle = *handle_ptr;
353 MSVCRT_fdesc[i].wxflag = 0;
354 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
356 wxflag_ptr++; handle_ptr++;
358 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
359 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
362 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
364 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
365 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, TRUE,
366 DUPLICATE_SAME_ACCESS);
367 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
369 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
371 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
372 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, TRUE,
373 DUPLICATE_SAME_ACCESS);
374 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
376 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
378 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
379 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, TRUE,
380 DUPLICATE_SAME_ACCESS);
381 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
384 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
385 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
387 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
388 for (i = 0; i < 3; i++)
390 /* FILE structs for stdin/out/err are static and never deleted */
391 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
392 MSVCRT__iob[i]._file = i;
393 MSVCRT__iob[i]._tmpfname = NULL;
394 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
396 MSVCRT_stream_idx = 3;
399 /* INTERNAL: Flush stdio file buffer */
400 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
403 int cnt=file->_ptr-file->_base;
404 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
405 file->_flag |= MSVCRT__IOERR;
408 file->_ptr=file->_base;
409 file->_cnt=file->_bufsiz;
414 /* INTERNAL: Allocate stdio file buffer */
415 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
417 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
419 file->_bufsiz = MSVCRT_BUFSIZ;
420 file->_flag |= MSVCRT__IOMYBUF;
422 file->_base = (char*)(&file->_charbuf);
424 file->_bufsiz = sizeof(file->_charbuf);
426 file->_ptr = file->_base;
430 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
431 static void msvcrt_int_to_base32(int num, char *str)
446 *p = (num & 31) + '0';
448 *p += ('a' - '0' - 10);
453 /*********************************************************************
454 * __iob_func(MSVCRT.@)
456 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
458 return &MSVCRT__iob[0];
461 /*********************************************************************
464 int CDECL MSVCRT__access(const char *filename, int mode)
466 DWORD attr = GetFileAttributesA(filename);
468 TRACE("(%s,%d) %d\n",filename,mode,attr);
470 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
472 msvcrt_set_errno(GetLastError());
475 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
477 msvcrt_set_errno(ERROR_ACCESS_DENIED);
483 /*********************************************************************
484 * _waccess (MSVCRT.@)
486 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
488 DWORD attr = GetFileAttributesW(filename);
490 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
492 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
494 msvcrt_set_errno(GetLastError());
497 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
499 msvcrt_set_errno(ERROR_ACCESS_DENIED);
505 /*********************************************************************
508 int CDECL MSVCRT__chmod(const char *path, int flags)
510 DWORD oldFlags = GetFileAttributesA(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 || SetFileAttributesA(path, newFlags))
520 msvcrt_set_errno(GetLastError());
524 /*********************************************************************
527 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
529 DWORD oldFlags = GetFileAttributesW(path);
531 if (oldFlags != INVALID_FILE_ATTRIBUTES)
533 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
534 oldFlags | FILE_ATTRIBUTE_READONLY;
536 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
539 msvcrt_set_errno(GetLastError());
543 /*********************************************************************
546 int CDECL MSVCRT__unlink(const char *path)
548 TRACE("%s\n",debugstr_a(path));
549 if(DeleteFileA(path))
551 TRACE("failed (%d)\n",GetLastError());
552 msvcrt_set_errno(GetLastError());
556 /*********************************************************************
557 * _wunlink (MSVCRT.@)
559 int CDECL _wunlink(const MSVCRT_wchar_t *path)
561 TRACE("(%s)\n",debugstr_w(path));
562 if(DeleteFileW(path))
564 TRACE("failed (%d)\n",GetLastError());
565 msvcrt_set_errno(GetLastError());
569 /* _flushall calls MSVCRT_fflush which calls _flushall */
570 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
572 /*********************************************************************
573 * _flushall (MSVCRT.@)
575 int CDECL _flushall(void)
577 int i, num_flushed = 0;
580 for (i = 3; i < MSVCRT_stream_idx; i++)
581 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
584 /* FIXME: flush, do not commit */
585 if (_commit(i) == -1)
586 if (MSVCRT_fstreams[i])
587 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
589 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
590 MSVCRT_fflush(MSVCRT_fstreams[i]);
596 TRACE(":flushed (%d) handles\n",num_flushed);
600 /*********************************************************************
603 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
607 } else if(file->_flag & MSVCRT__IOWRT) {
608 int res=msvcrt_flush_buffer(file);
614 /*********************************************************************
617 int CDECL MSVCRT__close(int fd)
623 hand = msvcrt_fdtoh(fd);
624 TRACE(":fd (%d) handle (%p)\n",fd,hand);
625 if (hand == INVALID_HANDLE_VALUE)
627 else if (!CloseHandle(hand))
629 WARN(":failed-last error (%d)\n",GetLastError());
630 msvcrt_set_errno(GetLastError());
643 /*********************************************************************
646 int CDECL _commit(int fd)
648 HANDLE hand = msvcrt_fdtoh(fd);
650 TRACE(":fd (%d) handle (%p)\n",fd,hand);
651 if (hand == INVALID_HANDLE_VALUE)
654 if (!FlushFileBuffers(hand))
656 if (GetLastError() == ERROR_INVALID_HANDLE)
658 /* FlushFileBuffers fails for console handles
659 * so we ignore this error.
663 TRACE(":failed-last error (%d)\n",GetLastError());
664 msvcrt_set_errno(GetLastError());
671 /*********************************************************************
674 * MSDN isn't clear on this point, but the remarks for _pipe
675 * indicate file descriptors duplicated with _dup and _dup2 are always
678 int CDECL MSVCRT__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 MSVCRT__dup(int od)
731 if (MSVCRT__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 MSVCRT__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|WX_READEOF);
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 MSVCRT__lseek(int fd, LONG offset, int whence)
845 return MSVCRT__lseeki64(fd, offset, whence);
848 /*********************************************************************
849 * _locking (MSVCRT.@)
851 * This is untested; the underlying LockFile doesn't work yet.
853 int CDECL MSVCRT__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;
913 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
914 /* Black magic correction for CR removal */
916 for (i=0; i<file->_cnt; i++) {
917 if (file->_ptr[i] == '\n')
922 /* Discard buffered input */
924 file->_ptr = file->_base;
925 /* Reset direction of i/o */
926 if(file->_flag & MSVCRT__IORW) {
927 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
929 /* Clear end of file flag */
930 file->_flag &= ~MSVCRT__IOEOF;
931 return (MSVCRT__lseek(file->_file,offset,whence) == -1)?-1:0;
934 /*********************************************************************
937 int CDECL _chsize(int fd, long size)
943 TRACE("(fd=%d, size=%ld)\n", fd, size);
947 handle = msvcrt_fdtoh(fd);
948 if (handle != INVALID_HANDLE_VALUE)
950 /* save the current file pointer */
951 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
954 pos = MSVCRT__lseek(fd, size, SEEK_SET);
957 ret = SetEndOfFile(handle);
958 if (!ret) msvcrt_set_errno(GetLastError());
961 /* restore the file pointer */
962 MSVCRT__lseek(fd, cur, SEEK_SET);
970 /*********************************************************************
971 * clearerr (MSVCRT.@)
973 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
975 TRACE(":file (%p) fd (%d)\n",file,file->_file);
976 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
979 /*********************************************************************
982 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
984 TRACE(":file (%p) fd (%d)\n",file,file->_file);
985 MSVCRT_fseek(file, 0L, SEEK_SET);
986 MSVCRT_clearerr(file);
989 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
991 int plus = strchrW(mode, '+') != NULL;
996 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
997 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1000 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1001 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1004 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1005 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1015 *open_flags |= MSVCRT__O_BINARY;
1016 *open_flags &= ~MSVCRT__O_TEXT;
1019 *open_flags |= MSVCRT__O_TEXT;
1020 *open_flags &= ~MSVCRT__O_BINARY;
1025 FIXME(":unknown flag %c not supported\n",mode[-1]);
1030 /*********************************************************************
1031 * _fdopen (MSVCRT.@)
1033 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1036 MSVCRT_wchar_t *modeW = NULL;
1038 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1040 ret = MSVCRT__wfdopen(fd, modeW);
1046 /*********************************************************************
1047 * _wfdopen (MSVCRT.@)
1049 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1051 int open_flags, stream_flags;
1054 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1057 if (!(file = msvcrt_alloc_fp()))
1059 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1064 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1070 /*********************************************************************
1071 * _filelength (MSVCRT.@)
1073 LONG CDECL MSVCRT__filelength(int fd)
1075 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1078 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1081 if (endPos != curPos)
1082 MSVCRT__lseek(fd, curPos, SEEK_SET);
1089 /*********************************************************************
1090 * _filelengthi64 (MSVCRT.@)
1092 __int64 CDECL MSVCRT__filelengthi64(int fd)
1094 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1097 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1100 if (endPos != curPos)
1101 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1108 /*********************************************************************
1109 * _fileno (MSVCRT.@)
1111 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1113 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1117 /*********************************************************************
1118 * _fstat64 (MSVCRT.@)
1120 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1124 BY_HANDLE_FILE_INFORMATION hfi;
1125 HANDLE hand = msvcrt_fdtoh(fd);
1127 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1128 if (hand == INVALID_HANDLE_VALUE)
1133 WARN(":failed-NULL buf\n");
1134 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1138 memset(&hfi, 0, sizeof(hfi));
1139 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1140 type = GetFileType(hand);
1141 if (type == FILE_TYPE_PIPE)
1143 buf->st_dev = buf->st_rdev = fd;
1144 buf->st_mode = S_IFIFO;
1147 else if (type == FILE_TYPE_CHAR)
1149 buf->st_dev = buf->st_rdev = fd;
1150 buf->st_mode = S_IFCHR;
1153 else /* FILE_TYPE_DISK etc. */
1155 if (!GetFileInformationByHandle(hand, &hfi))
1157 WARN(":failed-last error (%d)\n",GetLastError());
1158 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1161 buf->st_mode = S_IFREG | 0444;
1162 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1163 buf->st_mode |= 0222;
1164 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1165 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1167 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1168 buf->st_mtime = buf->st_ctime = dw;
1169 buf->st_nlink = hfi.nNumberOfLinks;
1171 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1176 /*********************************************************************
1177 * _fstati64 (MSVCRT.@)
1179 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1182 struct MSVCRT__stat64 buf64;
1184 ret = MSVCRT__fstat64(fd, &buf64);
1186 msvcrt_stat64_to_stati64(&buf64, buf);
1190 /*********************************************************************
1193 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1195 struct MSVCRT__stat64 buf64;
1197 ret = MSVCRT__fstat64(fd, &buf64);
1199 msvcrt_stat64_to_stat(&buf64, buf);
1203 /*********************************************************************
1204 * _futime (MSVCRT.@)
1206 int CDECL _futime(int fd, struct MSVCRT__utimbuf *t)
1208 HANDLE hand = msvcrt_fdtoh(fd);
1213 MSVCRT_time_t currTime;
1214 MSVCRT_time(&currTime);
1215 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1220 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1221 if (t->actime == t->modtime)
1224 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1227 if (!SetFileTime(hand, NULL, &at, &wt))
1229 msvcrt_set_errno(GetLastError());
1235 /*********************************************************************
1236 * _get_osfhandle (MSVCRT.@)
1238 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1240 HANDLE hand = msvcrt_fdtoh(fd);
1241 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1243 return (MSVCRT_intptr_t)hand;
1246 /*********************************************************************
1247 * _isatty (MSVCRT.@)
1249 int CDECL _isatty(int fd)
1251 HANDLE hand = msvcrt_fdtoh(fd);
1253 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1254 if (hand == INVALID_HANDLE_VALUE)
1257 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1260 /*********************************************************************
1261 * _mktemp (MSVCRT.@)
1263 char * CDECL _mktemp(char *pattern)
1266 char *retVal = pattern;
1271 numX = (*pattern++ == 'X')? numX + 1 : 0;
1275 id = GetCurrentProcessId();
1279 int tempNum = id / 10;
1280 *pattern-- = id - (tempNum * 10) + '0';
1286 *pattern = letter++;
1287 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1288 GetLastError() == ERROR_FILE_NOT_FOUND)
1290 } while(letter <= 'z');
1294 /*********************************************************************
1295 * _wmktemp (MSVCRT.@)
1297 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1300 MSVCRT_wchar_t *retVal = pattern;
1302 MSVCRT_wchar_t letter = 'a';
1305 numX = (*pattern++ == 'X')? numX + 1 : 0;
1309 id = GetCurrentProcessId();
1313 int tempNum = id / 10;
1314 *pattern-- = id - (tempNum * 10) + '0';
1320 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1321 GetLastError() == ERROR_FILE_NOT_FOUND)
1323 *pattern = letter++;
1324 } while(letter != '|');
1328 static unsigned split_oflags(unsigned oflags)
1331 unsigned unsupp; /* until we support everything */
1333 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1334 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1335 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1336 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1337 else wxflags |= WX_TEXT; /* default to TEXT*/
1338 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1340 if ((unsupp = oflags & ~(
1341 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1342 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1343 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1344 MSVCRT__O_NOINHERIT|
1345 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1347 ERR(":unsupported oflags 0x%04x\n",unsupp);
1352 /*********************************************************************
1355 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1358 SECURITY_ATTRIBUTES sa;
1359 HANDLE readHandle, writeHandle;
1363 *MSVCRT__errno() = MSVCRT_EINVAL;
1367 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1368 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1369 sa.lpSecurityDescriptor = NULL;
1370 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1372 unsigned int wxflags = split_oflags(textmode);
1376 fd = msvcrt_alloc_fd(readHandle, wxflags);
1380 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1388 MSVCRT__close(pfds[0]);
1389 CloseHandle(writeHandle);
1390 *MSVCRT__errno() = MSVCRT_EMFILE;
1395 CloseHandle(readHandle);
1396 CloseHandle(writeHandle);
1397 *MSVCRT__errno() = MSVCRT_EMFILE;
1402 msvcrt_set_errno(GetLastError());
1407 /*********************************************************************
1410 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1414 DWORD access = 0, creation = 0, attrib;
1418 SECURITY_ATTRIBUTES sa;
1421 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1422 path, oflags, shflags);
1424 wxflag = split_oflags(oflags);
1425 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1427 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1428 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1429 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1432 if (oflags & MSVCRT__O_CREAT)
1434 __ms_va_start(ap, shflags);
1435 pmode = va_arg(ap, int);
1438 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1439 FIXME(": pmode 0x%04x ignored\n", pmode);
1441 WARN(": pmode 0x%04x ignored\n", pmode);
1443 if (oflags & MSVCRT__O_EXCL)
1444 creation = CREATE_NEW;
1445 else if (oflags & MSVCRT__O_TRUNC)
1446 creation = CREATE_ALWAYS;
1448 creation = OPEN_ALWAYS;
1450 else /* no MSVCRT__O_CREAT */
1452 if (oflags & MSVCRT__O_TRUNC)
1453 creation = TRUNCATE_EXISTING;
1455 creation = OPEN_EXISTING;
1460 case MSVCRT__SH_DENYRW:
1463 case MSVCRT__SH_DENYWR:
1464 sharing = FILE_SHARE_READ;
1466 case MSVCRT__SH_DENYRD:
1467 sharing = FILE_SHARE_WRITE;
1469 case MSVCRT__SH_DENYNO:
1470 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1473 ERR( "Unhandled shflags 0x%x\n", shflags );
1476 attrib = FILE_ATTRIBUTE_NORMAL;
1478 if (oflags & MSVCRT__O_TEMPORARY)
1480 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1482 sharing |= FILE_SHARE_DELETE;
1485 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1486 sa.lpSecurityDescriptor = NULL;
1487 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1489 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1491 if (hand == INVALID_HANDLE_VALUE) {
1492 WARN(":failed-last error (%d)\n",GetLastError());
1493 msvcrt_set_errno(GetLastError());
1497 fd = msvcrt_alloc_fd(hand, wxflag);
1499 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1503 /*********************************************************************
1504 * _wsopen (MSVCRT.@)
1506 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1510 DWORD access = 0, creation = 0, attrib;
1514 SECURITY_ATTRIBUTES sa;
1517 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1518 debugstr_w(path), oflags, shflags);
1520 wxflag = split_oflags(oflags);
1521 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1523 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1524 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1525 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1528 if (oflags & MSVCRT__O_CREAT)
1530 __ms_va_start(ap, shflags);
1531 pmode = va_arg(ap, int);
1534 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1535 FIXME(": pmode 0x%04x ignored\n", pmode);
1537 WARN(": pmode 0x%04x ignored\n", pmode);
1539 if (oflags & MSVCRT__O_EXCL)
1540 creation = CREATE_NEW;
1541 else if (oflags & MSVCRT__O_TRUNC)
1542 creation = CREATE_ALWAYS;
1544 creation = OPEN_ALWAYS;
1546 else /* no MSVCRT__O_CREAT */
1548 if (oflags & MSVCRT__O_TRUNC)
1549 creation = TRUNCATE_EXISTING;
1551 creation = OPEN_EXISTING;
1556 case MSVCRT__SH_DENYRW:
1559 case MSVCRT__SH_DENYWR:
1560 sharing = FILE_SHARE_READ;
1562 case MSVCRT__SH_DENYRD:
1563 sharing = FILE_SHARE_WRITE;
1565 case MSVCRT__SH_DENYNO:
1566 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1569 ERR( "Unhandled shflags 0x%x\n", shflags );
1572 attrib = FILE_ATTRIBUTE_NORMAL;
1574 if (oflags & MSVCRT__O_TEMPORARY)
1576 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1578 sharing |= FILE_SHARE_DELETE;
1581 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1582 sa.lpSecurityDescriptor = NULL;
1583 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1585 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1587 if (hand == INVALID_HANDLE_VALUE) {
1588 WARN(":failed-last error (%d)\n",GetLastError());
1589 msvcrt_set_errno(GetLastError());
1593 fd = msvcrt_alloc_fd(hand, wxflag);
1595 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1599 /*********************************************************************
1602 int CDECL MSVCRT__open( const char *path, int flags, ... )
1606 if (flags & MSVCRT__O_CREAT)
1609 __ms_va_start(ap, flags);
1610 pmode = va_arg(ap, int);
1612 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1615 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1618 /*********************************************************************
1621 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1625 if (flags & MSVCRT__O_CREAT)
1628 __ms_va_start(ap, flags);
1629 pmode = va_arg(ap, int);
1631 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1634 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1637 /*********************************************************************
1640 int CDECL MSVCRT__creat(const char *path, int flags)
1642 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1643 return MSVCRT__open(path, usedFlags);
1646 /*********************************************************************
1647 * _wcreat (MSVCRT.@)
1649 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1651 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1652 return _wopen(path, usedFlags);
1655 /*********************************************************************
1656 * _open_osfhandle (MSVCRT.@)
1658 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1662 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1663 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1664 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1665 * text - it never sets MSVCRT__O_BINARY.
1667 /* don't let split_oflags() decide the mode if no mode is passed */
1668 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1669 oflags |= MSVCRT__O_BINARY;
1671 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1672 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1676 /*********************************************************************
1679 int CDECL _rmtmp(void)
1681 int num_removed = 0, i;
1684 for (i = 3; i < MSVCRT_stream_idx; i++)
1685 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1687 MSVCRT_fclose(MSVCRT_fstreams[i]);
1693 TRACE(":removed (%d) temp files\n",num_removed);
1697 /*********************************************************************
1700 static int read_i(int fd, void *buf, unsigned int count)
1703 char *bufstart = buf;
1704 HANDLE hand = msvcrt_fdtoh(fd);
1706 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1707 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1708 TRACE("already at EOF, returning 0\n");
1711 /* Don't trace small reads, it gets *very* annoying */
1713 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1714 if (hand == INVALID_HANDLE_VALUE)
1717 /* Reading single bytes in O_TEXT mode makes things slow
1718 * So read big chunks
1720 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1722 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1725 for (i=0, j=0; i<num_read; i++)
1727 /* in text mode, a ctrl-z signals EOF */
1728 if (bufstart[i] == 0x1a)
1730 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1731 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1734 /* in text mode, strip \r if followed by \n.
1735 * BUG: should save state across calls somehow, so CR LF that
1736 * straddles buffer boundary gets recognized properly?
1738 if ((bufstart[i] != '\r')
1739 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1740 bufstart[j++] = bufstart[i];
1744 if (count != 0 && num_read == 0)
1746 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1747 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1752 if (GetLastError() == ERROR_BROKEN_PIPE)
1754 TRACE(":end-of-pipe\n");
1755 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1760 TRACE(":failed-last error (%d)\n",GetLastError());
1766 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1770 /*********************************************************************
1773 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1776 num_read = read_i(fd, buf, count);
1780 /*********************************************************************
1781 * _setmode (MSVCRT.@)
1783 int CDECL _setmode(int fd,int mode)
1785 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1786 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1787 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1788 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1789 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1791 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1795 /*********************************************************************
1796 * _stat64 (MSVCRT.@)
1798 int CDECL MSVCRT__stat64(const char* path, struct MSVCRT__stat64 * buf)
1801 WIN32_FILE_ATTRIBUTE_DATA hfi;
1802 unsigned short mode = ALL_S_IREAD;
1805 TRACE(":file (%s) buf(%p)\n",path,buf);
1807 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1809 TRACE("failed (%d)\n",GetLastError());
1810 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1814 memset(buf,0,sizeof(struct MSVCRT__stat64));
1816 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1817 Bon 011120: This FIXME seems incorrect
1818 Also a letter as first char isn't enough to be classified
1821 if (isalpha(*path)&& (*(path+1)==':'))
1822 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1824 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1826 plen = strlen(path);
1828 /* Dir, or regular file? */
1829 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1830 (path[plen-1] == '\\'))
1831 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1834 mode |= MSVCRT__S_IFREG;
1836 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1838 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1839 (tolower(path[plen-3]) << 16);
1840 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1841 mode |= ALL_S_IEXEC;
1845 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1846 mode |= ALL_S_IWRITE;
1848 buf->st_mode = mode;
1850 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1851 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1853 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1854 buf->st_mtime = buf->st_ctime = dw;
1855 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1856 (long)(buf->st_size >> 32),(long)buf->st_size,
1857 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1861 /*********************************************************************
1862 * _stati64 (MSVCRT.@)
1864 int CDECL MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1867 struct MSVCRT__stat64 buf64;
1869 ret = MSVCRT__stat64(path, &buf64);
1871 msvcrt_stat64_to_stati64(&buf64, buf);
1875 /*********************************************************************
1878 int CDECL MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1880 struct MSVCRT__stat64 buf64;
1882 ret = MSVCRT__stat64( path, &buf64);
1884 msvcrt_stat64_to_stat(&buf64, buf);
1888 /*********************************************************************
1889 * _wstat64 (MSVCRT.@)
1891 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1894 WIN32_FILE_ATTRIBUTE_DATA hfi;
1895 unsigned short mode = ALL_S_IREAD;
1898 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1900 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1902 TRACE("failed (%d)\n",GetLastError());
1903 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1907 memset(buf,0,sizeof(struct MSVCRT__stat64));
1909 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1910 if (MSVCRT_iswalpha(*path))
1911 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1913 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1915 plen = strlenW(path);
1917 /* Dir, or regular file? */
1918 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1919 (path[plen-1] == '\\'))
1920 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1923 mode |= MSVCRT__S_IFREG;
1925 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1927 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1928 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1929 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1930 mode |= ALL_S_IEXEC;
1934 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1935 mode |= ALL_S_IWRITE;
1937 buf->st_mode = mode;
1939 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1940 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1942 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1943 buf->st_mtime = buf->st_ctime = dw;
1944 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1945 (long)(buf->st_size >> 32),(long)buf->st_size,
1946 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1950 /*********************************************************************
1951 * _wstati64 (MSVCRT.@)
1953 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1956 struct MSVCRT__stat64 buf64;
1958 ret = MSVCRT__wstat64(path, &buf64);
1960 msvcrt_stat64_to_stati64(&buf64, buf);
1964 /*********************************************************************
1967 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1970 struct MSVCRT__stat64 buf64;
1972 ret = MSVCRT__wstat64( path, &buf64 );
1973 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
1977 /*********************************************************************
1980 long CDECL _tell(int fd)
1982 return MSVCRT__lseek(fd, 0, SEEK_CUR);
1985 /*********************************************************************
1986 * _telli64 (MSVCRT.@)
1988 __int64 CDECL _telli64(int fd)
1990 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1993 /*********************************************************************
1994 * _tempnam (MSVCRT.@)
1996 char * CDECL _tempnam(const char *dir, const char *prefix)
1998 char tmpbuf[MAX_PATH];
1999 const char *tmp_dir = MSVCRT_getenv("TMP");
2001 if (tmp_dir) dir = tmp_dir;
2003 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2004 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2006 TRACE("got name (%s)\n",tmpbuf);
2007 DeleteFileA(tmpbuf);
2008 return _strdup(tmpbuf);
2010 TRACE("failed (%d)\n",GetLastError());
2014 /*********************************************************************
2015 * _wtempnam (MSVCRT.@)
2017 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2019 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2021 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2022 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2024 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2025 DeleteFileW(tmpbuf);
2026 return _wcsdup(tmpbuf);
2028 TRACE("failed (%d)\n",GetLastError());
2032 /*********************************************************************
2035 int CDECL MSVCRT__umask(int umask)
2037 int old_umask = MSVCRT_umask;
2038 TRACE("(%d)\n",umask);
2039 MSVCRT_umask = umask;
2043 /*********************************************************************
2046 int CDECL _utime(const char* path, struct MSVCRT__utimbuf *t)
2048 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2052 int retVal = _futime(fd, t);
2059 /*********************************************************************
2060 * _wutime (MSVCRT.@)
2062 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
2064 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2068 int retVal = _futime(fd, t);
2075 /*********************************************************************
2078 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2081 HANDLE hand = msvcrt_fdtoh(fd);
2083 /* Don't trace small writes, it gets *very* annoying */
2086 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2088 if (hand == INVALID_HANDLE_VALUE)
2090 *MSVCRT__errno() = MSVCRT_EBADF;
2094 /* If appending, go to EOF */
2095 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2096 MSVCRT__lseek(fd, 0, FILE_END);
2098 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2100 if (WriteFile(hand, buf, count, &num_written, NULL)
2101 && (num_written == count))
2103 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2104 hand, GetLastError());
2105 *MSVCRT__errno() = MSVCRT_ENOSPC;
2109 unsigned int i, j, nr_lf;
2112 const char *s = buf, *buf_start = buf;
2113 /* find number of \n ( without preceding \r ) */
2114 for ( nr_lf=0,i = 0; i <count; i++)
2119 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2124 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2126 for (s = buf, i = 0, j = 0; i < count; i++)
2131 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2138 FIXME("Malloc failed\n");
2146 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2148 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2149 fd, hand, GetLastError(), num_written);
2150 *MSVCRT__errno() = MSVCRT_ENOSPC;
2153 return s - buf_start;
2165 /*********************************************************************
2168 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2171 len = MSVCRT__write(file->_file, &val, sizeof(val));
2172 if (len == sizeof(val)) return val;
2173 file->_flag |= MSVCRT__IOERR;
2177 /*********************************************************************
2180 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2185 MSVCRT_free(file->_tmpfname);
2186 file->_tmpfname = NULL;
2187 /* flush stdio buffers */
2188 if(file->_flag & MSVCRT__IOWRT)
2189 MSVCRT_fflush(file);
2190 if(file->_flag & MSVCRT__IOMYBUF)
2191 MSVCRT_free(file->_base);
2193 r=MSVCRT__close(file->_file);
2197 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2200 /*********************************************************************
2203 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2205 return file->_flag & MSVCRT__IOEOF;
2208 /*********************************************************************
2211 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2213 return file->_flag & MSVCRT__IOERR;
2216 /*********************************************************************
2217 * _filbuf (MSVCRT.@)
2219 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2221 /* Allocate buffer if needed */
2222 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2223 msvcrt_alloc_buffer(file);
2225 if(!(file->_flag & MSVCRT__IOREAD)) {
2226 if(file->_flag & MSVCRT__IORW) {
2227 file->_flag |= MSVCRT__IOREAD;
2232 if(file->_flag & MSVCRT__IONBF) {
2235 if ((r = read_i(file->_file,&c,1)) != 1) {
2236 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2241 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2243 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2248 file->_ptr = file->_base+1;
2249 return *(unsigned char *)file->_base;
2253 /*********************************************************************
2256 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2262 i = (unsigned char *)file->_ptr++;
2265 j = MSVCRT__filbuf(file);
2269 /*********************************************************************
2270 * _fgetchar (MSVCRT.@)
2272 int CDECL _fgetchar(void)
2274 return MSVCRT_fgetc(MSVCRT_stdin);
2277 /*********************************************************************
2280 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2282 int cc = MSVCRT_EOF;
2283 char * buf_start = s;
2285 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2286 file,file->_file,s,size);
2288 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2293 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2295 TRACE(":nothing read\n");
2298 if ((cc != MSVCRT_EOF) && (size > 1))
2301 TRACE(":got %s\n", debugstr_a(buf_start));
2305 /*********************************************************************
2308 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2309 * the CR from CR/LF combinations
2311 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2315 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2322 for(i=0; i<sizeof(wc); i++)
2332 j = MSVCRT__filbuf(file);
2335 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2345 c = MSVCRT_fgetc(file);
2346 if ((MSVCRT___mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2348 FIXME("Treat Multibyte characters\n");
2350 if (c == MSVCRT_EOF)
2353 return (MSVCRT_wint_t)c;
2356 /*********************************************************************
2359 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2365 for (j=0; j<sizeof(int); j++) {
2366 k = MSVCRT_fgetc(file);
2367 if (k == MSVCRT_EOF) {
2368 file->_flag |= MSVCRT__IOEOF;
2376 /*********************************************************************
2379 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2381 return MSVCRT_fgetwc(file);
2384 /*********************************************************************
2385 * _fgetwchar (MSVCRT.@)
2387 MSVCRT_wint_t CDECL _fgetwchar(void)
2389 return MSVCRT_fgetwc(MSVCRT_stdin);
2392 /*********************************************************************
2393 * getwchar (MSVCRT.@)
2395 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2397 return _fgetwchar();
2400 /*********************************************************************
2403 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2405 int cc = MSVCRT_WEOF;
2406 MSVCRT_wchar_t * buf_start = s;
2408 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2409 file,file->_file,s,size);
2411 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2416 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2418 TRACE(":nothing read\n");
2421 if ((cc != MSVCRT_WEOF) && (size > 1))
2424 TRACE(":got %s\n", debugstr_w(buf_start));
2428 /*********************************************************************
2431 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2433 MSVCRT_size_t wrcnt=size * nmemb;
2438 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2439 memcpy(file->_ptr, ptr, pcnt);
2444 ptr = (const char*)ptr + pcnt;
2445 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2446 if(file->_flag & MSVCRT__IORW) {
2447 file->_flag |= MSVCRT__IOWRT;
2453 int res=msvcrt_flush_buffer(file);
2455 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2458 file->_flag |= MSVCRT__IOERR;
2461 written += pwritten;
2464 return written / size;
2467 /*********************************************************************
2470 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2472 MSVCRT_wchar_t mwc=wc;
2473 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2478 /*********************************************************************
2479 * _fputwchar (MSVCRT.@)
2481 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2483 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2486 /*********************************************************************
2487 * _wfsopen (MSVCRT.@)
2489 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2492 int open_flags, stream_flags, fd;
2494 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2496 /* map mode string to open() flags. "man fopen" for possibilities. */
2497 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2501 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2504 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2506 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2513 TRACE(":got (%p)\n",file);
2514 if (fd >= 0 && !file)
2520 /*********************************************************************
2521 * _fsopen (MSVCRT.@)
2523 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2526 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2528 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2529 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2535 ret = MSVCRT__wfsopen(pathW, modeW, share);
2542 /*********************************************************************
2545 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2547 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2550 /*********************************************************************
2551 * _wfopen (MSVCRT.@)
2553 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2555 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2558 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2559 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2561 /*********************************************************************
2564 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2571 int res = msvcrt_flush_buffer(file);
2572 return res ? res : c;
2577 return MSVCRT__flsbuf(c, file);
2581 /*********************************************************************
2582 * _flsbuf (MSVCRT.@)
2584 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2586 /* Flush output buffer */
2587 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2588 msvcrt_alloc_buffer(file);
2590 if(!(file->_flag & MSVCRT__IOWRT)) {
2591 if(file->_flag & MSVCRT__IORW) {
2592 file->_flag |= MSVCRT__IOWRT;
2598 int res=msvcrt_flush_buffer(file);
2599 return res?res : MSVCRT_fputc(c, file);
2603 /* set _cnt to 0 for unbuffered FILEs */
2605 len = MSVCRT__write(file->_file, &cc, 1);
2606 if (len == 1) return c & 0xff;
2607 file->_flag |= MSVCRT__IOERR;
2612 /*********************************************************************
2613 * _fputchar (MSVCRT.@)
2615 int CDECL _fputchar(int c)
2617 return MSVCRT_fputc(c, MSVCRT_stdout);
2620 /*********************************************************************
2623 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2624 { MSVCRT_size_t rcnt=size * nmemb;
2625 MSVCRT_size_t read=0;
2631 /* first buffered data */
2633 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2634 memcpy(ptr, file->_ptr, pcnt);
2639 ptr = (char*)ptr + pcnt;
2640 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2641 if(file->_flag & MSVCRT__IORW) {
2642 file->_flag |= MSVCRT__IOREAD;
2649 /* Fill the buffer on small reads.
2650 * TODO: Use a better buffering strategy.
2652 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2653 if (file->_bufsiz == 0) {
2654 msvcrt_alloc_buffer(file);
2656 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2657 file->_ptr = file->_base;
2658 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2659 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2660 if (i > 0 && i < file->_cnt) {
2661 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2662 file->_flag &= ~MSVCRT__IOEOF;
2665 memcpy(ptr, file->_ptr, i);
2670 i = MSVCRT__read(file->_file,ptr, rcnt);
2674 ptr = (char *)ptr+i;
2675 /* expose feof condition in the flags
2676 * MFC tests file->_flag for feof, and doesn't call feof())
2678 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2679 file->_flag |= MSVCRT__IOEOF;
2682 file->_flag |= MSVCRT__IOERR;
2692 /*********************************************************************
2693 * _wfreopen (MSVCRT.@)
2696 MSVCRT_FILE* CDECL _wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
2698 int open_flags, stream_flags, fd;
2700 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2703 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2707 MSVCRT_fclose(file);
2708 /* map mode string to open() flags. "man fopen" for possibilities. */
2709 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2713 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2716 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2719 WARN(":failed-last error (%d)\n",GetLastError());
2720 msvcrt_set_errno(GetLastError());
2729 /*********************************************************************
2730 * freopen (MSVCRT.@)
2733 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
2736 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2738 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2739 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2745 ret = _wfreopen(pathW, modeW, file);
2752 /*********************************************************************
2753 * fsetpos (MSVCRT.@)
2755 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2757 /* Note that all this has been lifted 'as is' from fseek */
2758 if(file->_flag & MSVCRT__IOWRT)
2759 msvcrt_flush_buffer(file);
2761 /* Discard buffered input */
2763 file->_ptr = file->_base;
2765 /* Reset direction of i/o */
2766 if(file->_flag & MSVCRT__IORW) {
2767 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2770 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2773 /*********************************************************************
2776 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2778 /* TODO: just call fgetpos and return lower half of result */
2781 pos = _tell(file->_file);
2782 if(pos == -1) return -1;
2784 if( file->_flag & MSVCRT__IOWRT ) {
2785 off = file->_ptr - file->_base;
2788 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2789 /* Black magic correction for CR removal */
2791 for (i=0; i<file->_cnt; i++) {
2792 if (file->_ptr[i] == '\n')
2801 /*********************************************************************
2802 * fgetpos (MSVCRT.@)
2804 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2807 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
2808 if(*pos == -1) return -1;
2810 if( file->_flag & MSVCRT__IOWRT ) {
2811 off = file->_ptr - file->_base;
2814 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2815 /* Black magic correction for CR removal */
2817 for (i=0; i<file->_cnt; i++) {
2818 if (file->_ptr[i] == '\n')
2828 /*********************************************************************
2831 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2833 size_t i, len = strlen(s);
2834 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2835 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2836 for (i=0; i<len; i++)
2837 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2842 /*********************************************************************
2845 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2847 size_t i, len = strlenW(s);
2848 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2849 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2850 for (i=0; i<len; i++)
2852 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2854 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2860 /*********************************************************************
2861 * getchar (MSVCRT.@)
2863 int CDECL MSVCRT_getchar(void)
2865 return MSVCRT_fgetc(MSVCRT_stdin);
2868 /*********************************************************************
2871 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
2873 return MSVCRT_fgetc(file);
2876 /*********************************************************************
2879 char * CDECL MSVCRT_gets(char *buf)
2882 char * buf_start = buf;
2884 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2885 cc = MSVCRT_fgetc(MSVCRT_stdin))
2886 if(cc != '\r') *buf++ = (char)cc;
2890 TRACE("got '%s'\n", buf_start);
2894 /*********************************************************************
2897 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
2900 MSVCRT_wchar_t* ws = buf;
2902 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2903 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2906 *buf++ = (MSVCRT_wchar_t)cc;
2910 TRACE("got %s\n", debugstr_w(ws));
2914 /*********************************************************************
2917 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
2919 return MSVCRT_fputc(c, file);
2922 /*********************************************************************
2923 * putchar (MSVCRT.@)
2925 int CDECL MSVCRT_putchar(int c)
2927 return MSVCRT_fputc(c, MSVCRT_stdout);
2930 /*********************************************************************
2933 int CDECL MSVCRT_puts(const char *s)
2935 size_t len = strlen(s);
2936 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2937 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2940 /*********************************************************************
2943 int CDECL _putws(const MSVCRT_wchar_t *s)
2945 static const MSVCRT_wchar_t nl = '\n';
2946 size_t len = strlenW(s);
2947 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2948 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2951 /*********************************************************************
2954 int CDECL MSVCRT_remove(const char *path)
2956 TRACE("(%s)\n",path);
2957 if (DeleteFileA(path))
2959 TRACE(":failed (%d)\n",GetLastError());
2960 msvcrt_set_errno(GetLastError());
2964 /*********************************************************************
2965 * _wremove (MSVCRT.@)
2967 int CDECL _wremove(const MSVCRT_wchar_t *path)
2969 TRACE("(%s)\n",debugstr_w(path));
2970 if (DeleteFileW(path))
2972 TRACE(":failed (%d)\n",GetLastError());
2973 msvcrt_set_errno(GetLastError());
2977 /*********************************************************************
2980 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
2982 TRACE(":from %s to %s\n",oldpath,newpath);
2983 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2985 TRACE(":failed (%d)\n",GetLastError());
2986 msvcrt_set_errno(GetLastError());
2990 /*********************************************************************
2991 * _wrename (MSVCRT.@)
2993 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2995 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2996 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2998 TRACE(":failed (%d)\n",GetLastError());
2999 msvcrt_set_errno(GetLastError());
3003 /*********************************************************************
3004 * setvbuf (MSVCRT.@)
3006 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3008 /* TODO: Check if file busy */
3010 MSVCRT_free(file->_base);
3014 if(mode == MSVCRT__IOFBF) {
3015 file->_flag &= ~MSVCRT__IONBF;
3016 file->_base = file->_ptr = buf;
3018 file->_bufsiz = size;
3021 file->_flag |= MSVCRT__IONBF;
3026 /*********************************************************************
3029 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3031 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3034 /*********************************************************************
3037 char * CDECL MSVCRT_tmpnam(char *s)
3045 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3046 p = s + sprintf(s, "\\s%s.", tmpstr);
3047 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3049 msvcrt_int_to_base32(unique++, tmpstr);
3051 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3052 GetLastError() == ERROR_FILE_NOT_FOUND)
3058 /*********************************************************************
3059 * tmpfile (MSVCRT.@)
3061 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3063 char *filename = MSVCRT_tmpnam(NULL);
3065 MSVCRT_FILE* file = NULL;
3068 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3069 if (fd != -1 && (file = msvcrt_alloc_fp()))
3071 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3076 else file->_tmpfname = _strdup(filename);
3082 /*********************************************************************
3083 * vfprintf (MSVCRT.@)
3085 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3087 char buf[2048], *mem = buf;
3088 int written, resize = sizeof(buf), retval;
3089 /* There are two conventions for vsnprintf failing:
3090 * Return -1 if we truncated, or
3091 * Return the number of bytes that would have been written
3092 * The code below handles both cases
3094 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3097 resize = (written == -1 ? resize * 2 : written + 1);
3100 if (!(mem = MSVCRT_malloc(resize)))
3103 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3109 /*********************************************************************
3110 * vfwprintf (MSVCRT.@)
3112 * Is final char included in written (then resize is too big) or not
3113 * (then we must test for equality too)?
3115 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3117 MSVCRT_wchar_t buf[2048], *mem = buf;
3118 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3119 /* See vfprintf comments */
3120 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3123 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3126 if (!(mem = MSVCRT_malloc(resize*sizeof(*mem))))
3129 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3135 /*********************************************************************
3136 * vprintf (MSVCRT.@)
3138 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3140 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3143 /*********************************************************************
3144 * vwprintf (MSVCRT.@)
3146 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3148 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3151 /*********************************************************************
3152 * fprintf (MSVCRT.@)
3154 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3156 __ms_va_list valist;
3158 __ms_va_start(valist, format);
3159 res = MSVCRT_vfprintf(file, format, valist);
3160 __ms_va_end(valist);
3164 /*********************************************************************
3165 * fwprintf (MSVCRT.@)
3167 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3169 __ms_va_list valist;
3171 __ms_va_start(valist, format);
3172 res = MSVCRT_vfwprintf(file, format, valist);
3173 __ms_va_end(valist);
3177 /*********************************************************************
3180 int CDECL MSVCRT_printf(const char *format, ...)
3182 __ms_va_list valist;
3184 __ms_va_start(valist, format);
3185 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3186 __ms_va_end(valist);
3190 /*********************************************************************
3193 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3195 if (c == MSVCRT_EOF)
3197 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3198 msvcrt_alloc_buffer(file);
3201 if(file->_ptr>file->_base) {
3205 MSVCRT_clearerr(file);
3211 /*********************************************************************
3212 * ungetwc (MSVCRT.@)
3214 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3216 MSVCRT_wchar_t mwc = wc;
3217 char * pp = (char *)&mwc;
3219 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3220 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3226 /*********************************************************************
3227 * wprintf (MSVCRT.@)
3229 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3231 __ms_va_list valist;
3233 __ms_va_start(valist, format);
3234 res = MSVCRT_vwprintf(format, valist);
3235 __ms_va_end(valist);
3239 /*********************************************************************
3240 * _getmaxstdio (MSVCRT.@)
3242 int CDECL _getmaxstdio(void)
3244 FIXME("stub, always returns 512\n");
3248 /*********************************************************************
3249 * _setmaxstdio_ (MSVCRT.@)
3251 int CDECL _setmaxstdio(int newmax)
3258 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3262 /*********************************************************************
3263 * __pioinfo (MSVCRT.@)
3264 * FIXME: see MSVCRT_MAX_FILES define.
3266 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3267 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3268 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3269 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3270 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3271 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3272 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3273 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3274 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3275 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3276 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3277 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3280 /*********************************************************************
3281 * __badioinfo (MSVCRT.@)
3283 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };