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];
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 /*********************************************************************
456 MSVCRT_FILE * CDECL __p__iob(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 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
676 * indicate file descriptors duplicated with _dup and _dup2 are always
679 int CDECL MSVCRT__dup2(int od, int nd)
683 TRACE("(od=%d, nd=%d)\n", od, nd);
685 if (nd < MSVCRT_MAX_FILES && msvcrt_is_valid_fd(od))
689 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
690 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
692 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
694 if (msvcrt_is_valid_fd(nd))
696 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
700 *MSVCRT__errno() = MSVCRT_EMFILE;
704 /* _dup2 returns 0, not nd, on success */
711 msvcrt_set_errno(GetLastError());
716 *MSVCRT__errno() = MSVCRT_EBADF;
723 /*********************************************************************
726 int CDECL MSVCRT__dup(int od)
732 if (MSVCRT__dup2(od, fd) == 0)
740 /*********************************************************************
743 int CDECL _eof(int fd)
746 LONG hcurpos,hendpos;
747 HANDLE hand = msvcrt_fdtoh(fd);
749 TRACE(":fd (%d) handle (%p)\n",fd,hand);
751 if (hand == INVALID_HANDLE_VALUE)
754 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
756 /* Otherwise we do it the hard way */
757 hcurpos = hendpos = 0;
758 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
759 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
761 if (curpos == endpos && hcurpos == hendpos)
763 /* FIXME: shouldn't WX_ATEOF be set here? */
767 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
771 /*********************************************************************
772 * _fcloseall (MSVCRT.@)
774 int CDECL MSVCRT__fcloseall(void)
776 int num_closed = 0, i;
779 for (i = 3; i < MSVCRT_stream_idx; i++)
780 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
781 !MSVCRT_fclose(MSVCRT_fstreams[i]))
785 TRACE(":closed (%d) handles\n",num_closed);
789 /* free everything on process exit */
790 void msvcrt_free_io(void)
793 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
794 * stdout, and stderr (unlike GNU), so we need to fclose() them here
795 * or they won't get flushed.
797 MSVCRT_fclose(&MSVCRT__iob[0]);
798 MSVCRT_fclose(&MSVCRT__iob[1]);
799 MSVCRT_fclose(&MSVCRT__iob[2]);
800 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
801 DeleteCriticalSection(&MSVCRT_file_cs);
804 /*********************************************************************
805 * _lseeki64 (MSVCRT.@)
807 __int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
809 HANDLE hand = msvcrt_fdtoh(fd);
810 LARGE_INTEGER ofs, ret;
812 TRACE(":fd (%d) handle (%p)\n",fd,hand);
813 if (hand == INVALID_HANDLE_VALUE)
816 if (whence < 0 || whence > 2)
818 *MSVCRT__errno() = MSVCRT_EINVAL;
822 TRACE(":fd (%d) to %s pos %s\n",
823 fd,wine_dbgstr_longlong(offset),
824 (whence==SEEK_SET)?"SEEK_SET":
825 (whence==SEEK_CUR)?"SEEK_CUR":
826 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
828 ofs.QuadPart = offset;
829 if (SetFilePointerEx(hand, ofs, &ret, whence))
831 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
832 /* FIXME: What if we seek _to_ EOF - is EOF set? */
836 TRACE(":error-last error (%d)\n",GetLastError());
837 msvcrt_set_errno(GetLastError());
841 /*********************************************************************
844 LONG CDECL _lseek(int fd, LONG offset, int whence)
846 return _lseeki64(fd, offset, whence);
849 /*********************************************************************
850 * _locking (MSVCRT.@)
852 * This is untested; the underlying LockFile doesn't work yet.
854 int CDECL _locking(int fd, int mode, LONG nbytes)
858 HANDLE hand = msvcrt_fdtoh(fd);
860 TRACE(":fd (%d) handle (%p)\n",fd,hand);
861 if (hand == INVALID_HANDLE_VALUE)
864 if (mode < 0 || mode > 4)
866 *MSVCRT__errno() = MSVCRT_EINVAL;
870 TRACE(":fd (%d) by 0x%08x mode %s\n",
871 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
872 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
873 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
874 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
875 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
878 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
880 FIXME ("Seek failed\n");
881 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
884 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
887 ret = 1; /* just to satisfy gcc */
890 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
895 else if (mode == MSVCRT__LK_UNLCK)
896 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
898 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
899 /* FIXME - what about error settings? */
903 /*********************************************************************
906 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
908 /* Flush output if needed */
909 if(file->_flag & MSVCRT__IOWRT)
910 msvcrt_flush_buffer(file);
912 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
913 offset -= file->_cnt;
915 /* Discard buffered input */
917 file->_ptr = file->_base;
918 /* Reset direction of i/o */
919 if(file->_flag & MSVCRT__IORW) {
920 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
922 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
925 /*********************************************************************
928 int CDECL _chsize(int fd, long size)
934 TRACE("(fd=%d, size=%ld)\n", fd, size);
938 handle = msvcrt_fdtoh(fd);
939 if (handle != INVALID_HANDLE_VALUE)
941 /* save the current file pointer */
942 cur = _lseek(fd, 0, SEEK_CUR);
945 pos = _lseek(fd, size, SEEK_SET);
948 ret = SetEndOfFile(handle);
949 if (!ret) msvcrt_set_errno(GetLastError());
952 /* restore the file pointer */
953 _lseek(fd, cur, SEEK_SET);
961 /*********************************************************************
962 * clearerr (MSVCRT.@)
964 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
966 TRACE(":file (%p) fd (%d)\n",file,file->_file);
967 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
970 /*********************************************************************
973 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
975 TRACE(":file (%p) fd (%d)\n",file,file->_file);
976 MSVCRT_fseek(file, 0L, SEEK_SET);
977 MSVCRT_clearerr(file);
980 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
982 int plus = strchr(mode, '+') != NULL;
987 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
988 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
991 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
992 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
995 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
996 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1006 *open_flags |= MSVCRT__O_BINARY;
1007 *open_flags &= ~MSVCRT__O_TEXT;
1010 *open_flags |= MSVCRT__O_TEXT;
1011 *open_flags &= ~MSVCRT__O_BINARY;
1016 FIXME(":unknown flag %c not supported\n",mode[-1]);
1021 /*********************************************************************
1022 * _fdopen (MSVCRT.@)
1024 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1026 int open_flags, stream_flags;
1029 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1032 if (!(file = msvcrt_alloc_fp()))
1034 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1039 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1045 /*********************************************************************
1046 * _wfdopen (MSVCRT.@)
1048 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1050 unsigned mlen = strlenW(mode);
1051 char *modea = MSVCRT_calloc(mlen + 1, 1);
1052 MSVCRT_FILE* file = NULL;
1053 int open_flags, stream_flags;
1056 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1058 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1060 if (!(file = msvcrt_alloc_fp()))
1062 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1070 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1071 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1078 /*********************************************************************
1079 * _filelength (MSVCRT.@)
1081 LONG CDECL _filelength(int fd)
1083 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1086 LONG endPos = _lseek(fd, 0, SEEK_END);
1089 if (endPos != curPos)
1090 _lseek(fd, curPos, SEEK_SET);
1097 /*********************************************************************
1098 * _filelengthi64 (MSVCRT.@)
1100 __int64 CDECL _filelengthi64(int fd)
1102 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1105 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1108 if (endPos != curPos)
1109 _lseeki64(fd, curPos, SEEK_SET);
1116 /*********************************************************************
1117 * _fileno (MSVCRT.@)
1119 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1121 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1125 /*********************************************************************
1126 * _fstat64 (MSVCRT.@)
1128 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1132 BY_HANDLE_FILE_INFORMATION hfi;
1133 HANDLE hand = msvcrt_fdtoh(fd);
1135 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1136 if (hand == INVALID_HANDLE_VALUE)
1141 WARN(":failed-NULL buf\n");
1142 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1146 memset(&hfi, 0, sizeof(hfi));
1147 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1148 type = GetFileType(hand);
1149 if (type == FILE_TYPE_PIPE)
1151 buf->st_dev = buf->st_rdev = fd;
1152 buf->st_mode = S_IFIFO;
1155 else if (type == FILE_TYPE_CHAR)
1157 buf->st_dev = buf->st_rdev = fd;
1158 buf->st_mode = S_IFCHR;
1161 else /* FILE_TYPE_DISK etc. */
1163 if (!GetFileInformationByHandle(hand, &hfi))
1165 WARN(":failed-last error (%d)\n",GetLastError());
1166 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1169 buf->st_mode = S_IFREG | S_IREAD;
1170 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1171 buf->st_mode |= S_IWRITE;
1172 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1173 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1175 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1176 buf->st_mtime = buf->st_ctime = dw;
1177 buf->st_nlink = hfi.nNumberOfLinks;
1179 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1184 /*********************************************************************
1185 * _fstati64 (MSVCRT.@)
1187 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1190 struct MSVCRT__stat64 buf64;
1192 ret = MSVCRT__fstat64(fd, &buf64);
1194 msvcrt_stat64_to_stati64(&buf64, buf);
1198 /*********************************************************************
1201 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1203 struct MSVCRT__stat64 buf64;
1205 ret = MSVCRT__fstat64(fd, &buf64);
1207 msvcrt_stat64_to_stat(&buf64, buf);
1211 /*********************************************************************
1212 * _futime (MSVCRT.@)
1214 int CDECL _futime(int fd, struct MSVCRT__utimbuf *t)
1216 HANDLE hand = msvcrt_fdtoh(fd);
1221 MSVCRT_time_t currTime;
1222 MSVCRT_time(&currTime);
1223 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1224 memcpy(&wt, &at, sizeof(wt));
1228 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1229 if (t->actime == t->modtime)
1230 memcpy(&wt, &at, sizeof(wt));
1232 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1235 if (!SetFileTime(hand, NULL, &at, &wt))
1237 msvcrt_set_errno(GetLastError());
1243 /*********************************************************************
1244 * _get_osfhandle (MSVCRT.@)
1246 long CDECL _get_osfhandle(int fd)
1248 HANDLE hand = msvcrt_fdtoh(fd);
1249 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1254 /*********************************************************************
1255 * _isatty (MSVCRT.@)
1257 int CDECL _isatty(int fd)
1259 HANDLE hand = msvcrt_fdtoh(fd);
1261 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1262 if (hand == INVALID_HANDLE_VALUE)
1265 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1268 /*********************************************************************
1269 * _mktemp (MSVCRT.@)
1271 char * CDECL _mktemp(char *pattern)
1274 char *retVal = pattern;
1279 numX = (*pattern++ == 'X')? numX + 1 : 0;
1283 id = GetCurrentProcessId();
1287 int tempNum = id / 10;
1288 *pattern-- = id - (tempNum * 10) + '0';
1294 *pattern = letter++;
1295 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1296 GetLastError() == ERROR_FILE_NOT_FOUND)
1298 } while(letter <= 'z');
1302 /*********************************************************************
1303 * _wmktemp (MSVCRT.@)
1305 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1308 MSVCRT_wchar_t *retVal = pattern;
1310 MSVCRT_wchar_t letter = 'a';
1313 numX = (*pattern++ == 'X')? numX + 1 : 0;
1317 id = GetCurrentProcessId();
1321 int tempNum = id / 10;
1322 *pattern-- = id - (tempNum * 10) + '0';
1328 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1329 GetLastError() == ERROR_FILE_NOT_FOUND)
1331 *pattern = letter++;
1332 } while(letter != '|');
1336 static unsigned split_oflags(unsigned oflags)
1339 unsigned unsupp; /* until we support everything */
1341 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1342 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1343 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1344 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1345 else wxflags |= WX_TEXT; /* default to TEXT*/
1346 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1348 if ((unsupp = oflags & ~(
1349 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1350 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1351 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1352 MSVCRT__O_NOINHERIT|
1353 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1355 ERR(":unsupported oflags 0x%04x\n",unsupp);
1360 /*********************************************************************
1363 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1366 SECURITY_ATTRIBUTES sa;
1367 HANDLE readHandle, writeHandle;
1371 *MSVCRT__errno() = MSVCRT_EINVAL;
1375 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1376 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1377 sa.lpSecurityDescriptor = NULL;
1378 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1380 unsigned int wxflags = split_oflags(textmode);
1384 fd = msvcrt_alloc_fd(readHandle, wxflags);
1388 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1396 MSVCRT__close(pfds[0]);
1397 CloseHandle(writeHandle);
1398 *MSVCRT__errno() = MSVCRT_EMFILE;
1403 CloseHandle(readHandle);
1404 CloseHandle(writeHandle);
1405 *MSVCRT__errno() = MSVCRT_EMFILE;
1410 msvcrt_set_errno(GetLastError());
1415 /*********************************************************************
1418 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1422 DWORD access = 0, creation = 0, attrib;
1426 SECURITY_ATTRIBUTES sa;
1429 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1430 path, oflags, shflags);
1432 wxflag = split_oflags(oflags);
1433 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1435 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1436 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1437 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1440 if (oflags & MSVCRT__O_CREAT)
1442 va_start(ap, shflags);
1443 pmode = va_arg(ap, int);
1446 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1447 FIXME(": pmode 0x%04x ignored\n", pmode);
1449 WARN(": pmode 0x%04x ignored\n", pmode);
1451 if (oflags & MSVCRT__O_EXCL)
1452 creation = CREATE_NEW;
1453 else if (oflags & MSVCRT__O_TRUNC)
1454 creation = CREATE_ALWAYS;
1456 creation = OPEN_ALWAYS;
1458 else /* no MSVCRT__O_CREAT */
1460 if (oflags & MSVCRT__O_TRUNC)
1461 creation = TRUNCATE_EXISTING;
1463 creation = OPEN_EXISTING;
1468 case MSVCRT__SH_DENYRW:
1471 case MSVCRT__SH_DENYWR:
1472 sharing = FILE_SHARE_READ;
1474 case MSVCRT__SH_DENYRD:
1475 sharing = FILE_SHARE_WRITE;
1477 case MSVCRT__SH_DENYNO:
1478 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1481 ERR( "Unhandled shflags 0x%x\n", shflags );
1484 attrib = FILE_ATTRIBUTE_NORMAL;
1486 if (oflags & MSVCRT__O_TEMPORARY)
1488 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1490 sharing |= FILE_SHARE_DELETE;
1493 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1494 sa.lpSecurityDescriptor = NULL;
1495 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1497 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1499 if (hand == INVALID_HANDLE_VALUE) {
1500 WARN(":failed-last error (%d)\n",GetLastError());
1501 msvcrt_set_errno(GetLastError());
1505 fd = msvcrt_alloc_fd(hand, wxflag);
1507 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1511 /*********************************************************************
1512 * _wsopen (MSVCRT.@)
1514 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1516 const unsigned int len = strlenW(path);
1517 char *patha = MSVCRT_calloc(len + 1,1);
1521 va_start(ap, shflags);
1522 pmode = va_arg(ap, int);
1525 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1527 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1532 msvcrt_set_errno(GetLastError());
1536 /*********************************************************************
1539 int CDECL MSVCRT__open( const char *path, int flags, ... )
1543 if (flags & MSVCRT__O_CREAT)
1546 va_start(ap, flags);
1547 pmode = va_arg(ap, int);
1549 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1552 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1555 /*********************************************************************
1558 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1560 const unsigned int len = strlenW(path);
1561 char *patha = MSVCRT_calloc(len + 1,1);
1565 va_start(ap, flags);
1566 pmode = va_arg(ap, int);
1569 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1571 int retval = MSVCRT__open(patha,flags,pmode);
1576 msvcrt_set_errno(GetLastError());
1580 /*********************************************************************
1583 int CDECL MSVCRT__creat(const char *path, int flags)
1585 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1586 return MSVCRT__open(path, usedFlags);
1589 /*********************************************************************
1590 * _wcreat (MSVCRT.@)
1592 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1594 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1595 return _wopen(path, usedFlags);
1598 /*********************************************************************
1599 * _open_osfhandle (MSVCRT.@)
1601 int CDECL _open_osfhandle(long handle, int oflags)
1605 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1606 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1607 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1608 * text - it never sets MSVCRT__O_BINARY.
1610 /* don't let split_oflags() decide the mode if no mode is passed */
1611 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1612 oflags |= MSVCRT__O_BINARY;
1614 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1615 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1619 /*********************************************************************
1622 int CDECL _rmtmp(void)
1624 int num_removed = 0, i;
1627 for (i = 3; i < MSVCRT_stream_idx; i++)
1628 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1630 MSVCRT_fclose(MSVCRT_fstreams[i]);
1636 TRACE(":removed (%d) temp files\n",num_removed);
1640 /*********************************************************************
1641 * (internal) remove_cr
1643 * Translate all \r\n to \n inplace.
1644 * return the number of \r removed
1645 * Corner cases required by some apps:
1647 * BUG: should save state across calls somehow, so CR LF that
1648 * straddles buffer boundary gets recognized properly?
1650 static unsigned int remove_cr(char *buf, unsigned int count)
1654 for (i=0, j=0; j < count; j++)
1655 if ((buf[j] != '\r') || ((j+1) < count && buf[j+1] != '\n'))
1661 /*********************************************************************
1664 static int read_i(int fd, void *buf, unsigned int count)
1667 char *bufstart = buf;
1668 HANDLE hand = msvcrt_fdtoh(fd);
1670 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1671 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1672 TRACE("already at EOF, returning 0\n");
1675 /* Don't trace small reads, it gets *very* annoying */
1677 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1678 if (hand == INVALID_HANDLE_VALUE)
1681 /* Reading single bytes in O_TEXT mode makes things slow
1682 * So read big chunks
1684 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1686 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1689 /* in text mode, a ctrl-z signals EOF */
1690 for (i=0; i<num_read; i++)
1692 if (bufstart[i] == 0x1a)
1695 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1696 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1701 if (count != 0 && num_read == 0)
1703 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1704 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1709 if (GetLastError() == ERROR_BROKEN_PIPE)
1711 TRACE(":end-of-pipe\n");
1712 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1717 TRACE(":failed-last error (%d)\n",GetLastError());
1723 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1727 /*********************************************************************
1730 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1733 num_read = read_i(fd, buf, count);
1734 if (num_read>0 && MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1736 num_read -= remove_cr(buf,num_read);
1741 /*********************************************************************
1742 * _setmode (MSVCRT.@)
1744 int CDECL _setmode(int fd,int mode)
1746 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1747 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1748 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1749 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1750 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1752 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1756 /*********************************************************************
1757 * _stat64 (MSVCRT.@)
1759 int CDECL MSVCRT__stat64(const char* path, struct MSVCRT__stat64 * buf)
1762 WIN32_FILE_ATTRIBUTE_DATA hfi;
1763 unsigned short mode = ALL_S_IREAD;
1766 TRACE(":file (%s) buf(%p)\n",path,buf);
1768 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1770 TRACE("failed (%d)\n",GetLastError());
1771 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1775 memset(buf,0,sizeof(struct MSVCRT__stat64));
1777 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1778 Bon 011120: This FIXME seems incorrect
1779 Also a letter as first char isn't enough to be classified
1782 if (isalpha(*path)&& (*(path+1)==':'))
1783 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1785 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1787 plen = strlen(path);
1789 /* Dir, or regular file? */
1790 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1791 (path[plen-1] == '\\'))
1792 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1795 mode |= MSVCRT__S_IFREG;
1797 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1799 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1800 (tolower(path[plen-3]) << 16);
1801 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1802 mode |= ALL_S_IEXEC;
1806 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1807 mode |= ALL_S_IWRITE;
1809 buf->st_mode = mode;
1811 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1812 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1814 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1815 buf->st_mtime = buf->st_ctime = dw;
1816 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1817 (long)(buf->st_size >> 32),(long)buf->st_size,
1818 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1822 /*********************************************************************
1823 * _stati64 (MSVCRT.@)
1825 int CDECL MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1828 struct MSVCRT__stat64 buf64;
1830 ret = MSVCRT__stat64(path, &buf64);
1832 msvcrt_stat64_to_stati64(&buf64, buf);
1836 /*********************************************************************
1839 int CDECL MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1841 struct MSVCRT__stat64 buf64;
1843 ret = MSVCRT__stat64( path, &buf64);
1845 msvcrt_stat64_to_stat(&buf64, buf);
1849 /*********************************************************************
1850 * _wstat64 (MSVCRT.@)
1852 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1855 WIN32_FILE_ATTRIBUTE_DATA hfi;
1856 unsigned short mode = ALL_S_IREAD;
1859 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1861 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1863 TRACE("failed (%d)\n",GetLastError());
1864 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1868 memset(buf,0,sizeof(struct MSVCRT__stat64));
1870 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1871 if (MSVCRT_iswalpha(*path))
1872 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1874 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1876 plen = strlenW(path);
1878 /* Dir, or regular file? */
1879 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1880 (path[plen-1] == '\\'))
1881 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1884 mode |= MSVCRT__S_IFREG;
1886 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1888 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1889 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1890 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1891 mode |= ALL_S_IEXEC;
1895 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1896 mode |= ALL_S_IWRITE;
1898 buf->st_mode = mode;
1900 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1901 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1903 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1904 buf->st_mtime = buf->st_ctime = dw;
1905 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1906 (long)(buf->st_size >> 32),(long)buf->st_size,
1907 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1911 /*********************************************************************
1912 * _wstati64 (MSVCRT.@)
1914 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1917 struct MSVCRT__stat64 buf64;
1919 ret = MSVCRT__wstat64(path, &buf64);
1921 msvcrt_stat64_to_stati64(&buf64, buf);
1925 /*********************************************************************
1928 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1931 struct MSVCRT__stat64 buf64;
1933 ret = MSVCRT__wstat64( path, &buf64 );
1934 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
1938 /*********************************************************************
1941 long CDECL _tell(int fd)
1943 return _lseek(fd, 0, SEEK_CUR);
1946 /*********************************************************************
1947 * _telli64 (MSVCRT.@)
1949 __int64 CDECL _telli64(int fd)
1951 return _lseeki64(fd, 0, SEEK_CUR);
1954 /*********************************************************************
1955 * _tempnam (MSVCRT.@)
1957 char * CDECL _tempnam(const char *dir, const char *prefix)
1959 char tmpbuf[MAX_PATH];
1960 const char *tmp_dir = MSVCRT_getenv("TMP");
1962 if (tmp_dir) dir = tmp_dir;
1964 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1965 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1967 TRACE("got name (%s)\n",tmpbuf);
1968 DeleteFileA(tmpbuf);
1969 return _strdup(tmpbuf);
1971 TRACE("failed (%d)\n",GetLastError());
1975 /*********************************************************************
1976 * _wtempnam (MSVCRT.@)
1978 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1980 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1982 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1983 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1985 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1986 DeleteFileW(tmpbuf);
1987 return _wcsdup(tmpbuf);
1989 TRACE("failed (%d)\n",GetLastError());
1993 /*********************************************************************
1996 int CDECL MSVCRT__umask(int umask)
1998 int old_umask = MSVCRT_umask;
1999 TRACE("(%d)\n",umask);
2000 MSVCRT_umask = umask;
2004 /*********************************************************************
2007 int CDECL _utime(const char* path, struct MSVCRT__utimbuf *t)
2009 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2013 int retVal = _futime(fd, t);
2020 /*********************************************************************
2021 * _wutime (MSVCRT.@)
2023 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
2025 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2029 int retVal = _futime(fd, t);
2036 /*********************************************************************
2039 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2042 HANDLE hand = msvcrt_fdtoh(fd);
2044 /* Don't trace small writes, it gets *very* annoying */
2047 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2049 if (hand == INVALID_HANDLE_VALUE)
2051 *MSVCRT__errno() = MSVCRT_EBADF;
2055 /* If appending, go to EOF */
2056 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2057 _lseek(fd, 0, FILE_END);
2059 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2061 if (WriteFile(hand, buf, count, &num_written, NULL)
2062 && (num_written == count))
2064 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2065 hand, GetLastError());
2066 *MSVCRT__errno() = MSVCRT_ENOSPC;
2070 unsigned int i, j, nr_lf;
2073 const char *s = (const char *)buf, *buf_start = (const char *)buf;
2074 /* find number of \n ( without preceding \r ) */
2075 for ( nr_lf=0,i = 0; i <count; i++)
2080 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2085 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2087 for (s = (const char *)buf, i = 0, j = 0; i < count; i++)
2092 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2099 FIXME("Malloc failed\n");
2107 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2109 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2110 fd, hand, GetLastError(), num_written);
2111 *MSVCRT__errno() = MSVCRT_ENOSPC;
2114 return s - buf_start;
2126 /*********************************************************************
2129 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2132 len = MSVCRT__write(file->_file, &val, sizeof(val));
2133 if (len == sizeof(val)) return val;
2134 file->_flag |= MSVCRT__IOERR;
2138 /*********************************************************************
2141 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2146 MSVCRT_free(file->_tmpfname);
2147 file->_tmpfname = NULL;
2148 /* flush stdio buffers */
2149 if(file->_flag & MSVCRT__IOWRT)
2150 MSVCRT_fflush(file);
2151 if(file->_flag & MSVCRT__IOMYBUF)
2152 MSVCRT_free(file->_base);
2154 r=MSVCRT__close(file->_file);
2158 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2161 /*********************************************************************
2164 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2166 return file->_flag & MSVCRT__IOEOF;
2169 /*********************************************************************
2172 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2174 return file->_flag & MSVCRT__IOERR;
2177 /*********************************************************************
2178 * _filbuf (MSVCRT.@)
2180 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2182 /* Allocate buffer if needed */
2183 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2184 msvcrt_alloc_buffer(file);
2186 if(!(file->_flag & MSVCRT__IOREAD)) {
2187 if(file->_flag & MSVCRT__IORW) {
2188 file->_flag |= MSVCRT__IOREAD;
2193 if(file->_flag & MSVCRT__IONBF) {
2196 if ((r = read_i(file->_file,&c,1)) != 1) {
2197 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2202 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2204 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2209 file->_ptr = file->_base+1;
2210 return *(unsigned char *)file->_base;
2214 /*********************************************************************
2217 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2224 i = (unsigned char *)file->_ptr++;
2227 j = MSVCRT__filbuf(file);
2228 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
2229 || ((j != '\r') || (file->_cnt && file->_ptr[0] != '\n')))
2234 /*********************************************************************
2235 * _fgetchar (MSVCRT.@)
2237 int CDECL _fgetchar(void)
2239 return MSVCRT_fgetc(MSVCRT_stdin);
2242 /*********************************************************************
2245 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2247 int cc = MSVCRT_EOF;
2248 char * buf_start = s;
2250 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2251 file,file->_file,s,size);
2253 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2258 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2260 TRACE(":nothing read\n");
2263 if ((cc != MSVCRT_EOF) && (size > 1))
2266 TRACE(":got %s\n", debugstr_a(buf_start));
2270 /*********************************************************************
2273 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2274 * the CR from CR/LF combinations
2276 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2280 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2286 for(i=0; i<sizeof(wc); i++)
2296 j = MSVCRT__filbuf(file);
2299 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2309 c = MSVCRT_fgetc(file);
2310 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2312 FIXME("Treat Multibyte characters\n");
2314 if (c == MSVCRT_EOF)
2317 return (MSVCRT_wint_t)c;
2320 /*********************************************************************
2323 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2328 for (j=0; j<sizeof(int); j++) {
2329 k = MSVCRT_fgetc(file);
2330 if (k == MSVCRT_EOF) {
2331 file->_flag |= MSVCRT__IOEOF;
2339 /*********************************************************************
2342 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2344 return MSVCRT_fgetwc(file);
2347 /*********************************************************************
2348 * _fgetwchar (MSVCRT.@)
2350 MSVCRT_wint_t CDECL _fgetwchar(void)
2352 return MSVCRT_fgetwc(MSVCRT_stdin);
2355 /*********************************************************************
2356 * getwchar (MSVCRT.@)
2358 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2360 return _fgetwchar();
2363 /*********************************************************************
2366 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2368 int cc = MSVCRT_WEOF;
2369 MSVCRT_wchar_t * buf_start = s;
2371 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2372 file,file->_file,s,size);
2374 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2379 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2381 TRACE(":nothing read\n");
2384 if ((cc != MSVCRT_WEOF) && (size > 1))
2387 TRACE(":got %s\n", debugstr_w(buf_start));
2391 /*********************************************************************
2394 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2396 MSVCRT_size_t wrcnt=size * nmemb;
2401 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2402 memcpy(file->_ptr, ptr, pcnt);
2407 ptr = (const char*)ptr + pcnt;
2408 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2409 if(file->_flag & MSVCRT__IORW) {
2410 file->_flag |= MSVCRT__IOWRT;
2416 int res=msvcrt_flush_buffer(file);
2418 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2421 file->_flag |= MSVCRT__IOERR;
2424 written += pwritten;
2427 return written / size;
2430 /*********************************************************************
2433 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2435 MSVCRT_wchar_t mwc=wc;
2436 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2441 /*********************************************************************
2442 * _fputwchar (MSVCRT.@)
2444 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2446 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2449 /*********************************************************************
2450 * _fsopen (MSVCRT.@)
2452 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2455 int open_flags, stream_flags, fd;
2457 TRACE("(%s,%s)\n",path,mode);
2459 /* map mode string to open() flags. "man fopen" for possibilities. */
2460 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2464 fd = MSVCRT__sopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2467 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2469 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2476 TRACE(":got (%p)\n",file);
2477 if (fd >= 0 && !file)
2483 /*********************************************************************
2484 * _wfsopen (MSVCRT.@)
2486 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2488 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2489 char *patha = MSVCRT_calloc(plen + 1, 1);
2490 char *modea = MSVCRT_calloc(mlen + 1, 1);
2492 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2494 if (patha && modea &&
2495 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2496 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2498 MSVCRT_FILE *retval = MSVCRT__fsopen(patha,modea,share);
2504 msvcrt_set_errno(GetLastError());
2508 /*********************************************************************
2511 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2513 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2516 /*********************************************************************
2517 * _wfopen (MSVCRT.@)
2519 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2521 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2524 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2525 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2527 /*********************************************************************
2530 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2537 int res = msvcrt_flush_buffer(file);
2538 return res ? res : c;
2543 return MSVCRT__flsbuf(c, file);
2547 /*********************************************************************
2548 * _flsbuf (MSVCRT.@)
2550 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2552 /* Flush output buffer */
2553 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2554 msvcrt_alloc_buffer(file);
2556 if(!(file->_flag & MSVCRT__IOWRT)) {
2557 if(file->_flag & MSVCRT__IORW) {
2558 file->_flag |= MSVCRT__IOWRT;
2564 int res=msvcrt_flush_buffer(file);
2565 return res?res : MSVCRT_fputc(c, file);
2569 len = MSVCRT__write(file->_file, &cc, 1);
2570 if (len == 1) return c;
2571 file->_flag |= MSVCRT__IOERR;
2576 /*********************************************************************
2577 * _fputchar (MSVCRT.@)
2579 int CDECL _fputchar(int c)
2581 return MSVCRT_fputc(c, MSVCRT_stdout);
2584 /*********************************************************************
2587 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2588 { MSVCRT_size_t rcnt=size * nmemb;
2589 MSVCRT_size_t read=0;
2595 /* first buffered data */
2597 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2598 memcpy(ptr, file->_ptr, pcnt);
2601 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
2602 pcnt -= remove_cr(ptr,pcnt);
2605 ptr = (char*)ptr + pcnt;
2606 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2607 if(file->_flag & MSVCRT__IORW) {
2608 file->_flag |= MSVCRT__IOREAD;
2615 /* Fill the buffer on small reads.
2616 * TODO: Use a better buffering strategy.
2618 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2619 if (file->_bufsiz == 0) {
2620 msvcrt_alloc_buffer(file);
2622 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2623 file->_ptr = file->_base;
2624 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2625 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2626 if (i > 0 && i < file->_cnt) {
2627 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2628 file->_flag &= ~MSVCRT__IOEOF;
2631 memcpy(ptr, file->_ptr, i);
2636 i = MSVCRT__read(file->_file,ptr, rcnt);
2640 ptr = (char *)ptr+i;
2641 /* expose feof condition in the flags
2642 * MFC tests file->_flag for feof, and doesn't call feof())
2644 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2645 file->_flag |= MSVCRT__IOEOF;
2648 file->_flag |= MSVCRT__IOERR;
2658 /*********************************************************************
2659 * freopen (MSVCRT.@)
2662 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2664 int open_flags, stream_flags, fd;
2666 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2669 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2673 MSVCRT_fclose(file);
2674 /* map mode string to open() flags. "man fopen" for possibilities. */
2675 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2679 fd = MSVCRT__open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2682 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2685 WARN(":failed-last error (%d)\n",GetLastError());
2686 msvcrt_set_errno(GetLastError());
2695 /*********************************************************************
2696 * fsetpos (MSVCRT.@)
2698 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2700 /* Note that all this has been lifted 'as is' from fseek */
2701 if(file->_flag & MSVCRT__IOWRT)
2702 msvcrt_flush_buffer(file);
2704 /* Discard buffered input */
2706 file->_ptr = file->_base;
2708 /* Reset direction of i/o */
2709 if(file->_flag & MSVCRT__IORW) {
2710 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2713 return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2716 /*********************************************************************
2719 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2724 if( file->_flag & MSVCRT__IOWRT ) {
2725 off = file->_ptr - file->_base;
2730 pos = _tell(file->_file);
2731 if(pos == -1) return pos;
2735 /*********************************************************************
2736 * fgetpos (MSVCRT.@)
2738 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2740 /* This code has been lifted form the MSVCRT_ftell function */
2743 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2745 if (*pos == -1) return -1;
2748 if( file->_flag & MSVCRT__IOWRT ) {
2749 off = file->_ptr - file->_base;
2759 /*********************************************************************
2762 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2764 size_t i, len = strlen(s);
2765 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2766 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2767 for (i=0; i<len; i++)
2768 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2773 /*********************************************************************
2776 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2778 size_t i, len = strlenW(s);
2779 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2780 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2781 for (i=0; i<len; i++)
2783 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2785 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2791 /*********************************************************************
2792 * getchar (MSVCRT.@)
2794 int CDECL MSVCRT_getchar(void)
2796 return MSVCRT_fgetc(MSVCRT_stdin);
2799 /*********************************************************************
2802 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
2804 return MSVCRT_fgetc(file);
2807 /*********************************************************************
2810 char * CDECL MSVCRT_gets(char *buf)
2813 char * buf_start = buf;
2815 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2816 cc = MSVCRT_fgetc(MSVCRT_stdin))
2817 if(cc != '\r') *buf++ = (char)cc;
2821 TRACE("got '%s'\n", buf_start);
2825 /*********************************************************************
2828 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
2831 MSVCRT_wchar_t* ws = buf;
2833 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2834 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2837 *buf++ = (MSVCRT_wchar_t)cc;
2841 TRACE("got %s\n", debugstr_w(ws));
2845 /*********************************************************************
2848 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
2850 return MSVCRT_fputc(c, file);
2853 /*********************************************************************
2854 * putchar (MSVCRT.@)
2856 int CDECL MSVCRT_putchar(int c)
2858 return MSVCRT_fputc(c, MSVCRT_stdout);
2861 /*********************************************************************
2864 int CDECL MSVCRT_puts(const char *s)
2866 size_t len = strlen(s);
2867 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2868 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2871 /*********************************************************************
2874 int CDECL _putws(const MSVCRT_wchar_t *s)
2876 static const MSVCRT_wchar_t nl = '\n';
2877 size_t len = strlenW(s);
2878 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2879 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2882 /*********************************************************************
2885 int CDECL MSVCRT_remove(const char *path)
2887 TRACE("(%s)\n",path);
2888 if (DeleteFileA(path))
2890 TRACE(":failed (%d)\n",GetLastError());
2891 msvcrt_set_errno(GetLastError());
2895 /*********************************************************************
2896 * _wremove (MSVCRT.@)
2898 int CDECL _wremove(const MSVCRT_wchar_t *path)
2900 TRACE("(%s)\n",debugstr_w(path));
2901 if (DeleteFileW(path))
2903 TRACE(":failed (%d)\n",GetLastError());
2904 msvcrt_set_errno(GetLastError());
2908 /*********************************************************************
2911 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
2913 TRACE(":from %s to %s\n",oldpath,newpath);
2914 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2916 TRACE(":failed (%d)\n",GetLastError());
2917 msvcrt_set_errno(GetLastError());
2921 /*********************************************************************
2922 * _wrename (MSVCRT.@)
2924 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2926 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2927 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2929 TRACE(":failed (%d)\n",GetLastError());
2930 msvcrt_set_errno(GetLastError());
2934 /*********************************************************************
2935 * setvbuf (MSVCRT.@)
2937 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2939 /* TODO: Check if file busy */
2941 MSVCRT_free(file->_base);
2945 if(mode == MSVCRT__IOFBF) {
2946 file->_flag &= ~MSVCRT__IONBF;
2947 file->_base = file->_ptr = buf;
2949 file->_bufsiz = size;
2952 file->_flag |= MSVCRT__IONBF;
2957 /*********************************************************************
2960 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2962 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2965 /*********************************************************************
2968 char * CDECL MSVCRT_tmpnam(char *s)
2976 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2977 p = s + sprintf(s, "\\s%s.", tmpstr);
2978 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2980 msvcrt_int_to_base32(unique++, tmpstr);
2982 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2983 GetLastError() == ERROR_FILE_NOT_FOUND)
2989 /*********************************************************************
2990 * tmpfile (MSVCRT.@)
2992 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
2994 char *filename = MSVCRT_tmpnam(NULL);
2996 MSVCRT_FILE* file = NULL;
2999 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3000 if (fd != -1 && (file = msvcrt_alloc_fp()))
3002 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3007 else file->_tmpfname = _strdup(filename);
3013 /*********************************************************************
3014 * vfprintf (MSVCRT.@)
3016 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
3018 char buf[2048], *mem = buf;
3019 int written, resize = sizeof(buf), retval;
3020 /* There are two conventions for vsnprintf failing:
3021 * Return -1 if we truncated, or
3022 * Return the number of bytes that would have been written
3023 * The code below handles both cases
3025 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3028 resize = (written == -1 ? resize * 2 : written + 1);
3031 if (!(mem = MSVCRT_malloc(resize)))
3034 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3040 /*********************************************************************
3041 * vfwprintf (MSVCRT.@)
3043 * Is final char included in written (then resize is too big) or not
3044 * (then we must test for equality too)?
3046 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
3048 MSVCRT_wchar_t buf[2048], *mem = buf;
3049 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3050 /* See vfprintf comments */
3051 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3054 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3057 if (!(mem = MSVCRT_malloc(resize*sizeof(*mem))))
3060 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3066 /*********************************************************************
3067 * vprintf (MSVCRT.@)
3069 int CDECL MSVCRT_vprintf(const char *format, va_list valist)
3071 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3074 /*********************************************************************
3075 * vwprintf (MSVCRT.@)
3077 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
3079 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3082 /*********************************************************************
3083 * fprintf (MSVCRT.@)
3085 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3089 va_start(valist, format);
3090 res = MSVCRT_vfprintf(file, format, valist);
3095 /*********************************************************************
3096 * fwprintf (MSVCRT.@)
3098 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3102 va_start(valist, format);
3103 res = MSVCRT_vfwprintf(file, format, valist);
3108 /*********************************************************************
3111 int CDECL MSVCRT_printf(const char *format, ...)
3115 va_start(valist, format);
3116 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3121 /*********************************************************************
3124 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3126 if (c == MSVCRT_EOF)
3128 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3129 msvcrt_alloc_buffer(file);
3132 if(file->_ptr>file->_base) {
3136 MSVCRT_clearerr(file);
3142 /*********************************************************************
3143 * ungetwc (MSVCRT.@)
3145 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3147 MSVCRT_wchar_t mwc = wc;
3148 char * pp = (char *)&mwc;
3150 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3151 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3157 /*********************************************************************
3158 * wprintf (MSVCRT.@)
3160 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3164 va_start(valist, format);
3165 res = MSVCRT_vwprintf(format, valist);
3170 /*********************************************************************
3171 * _getmaxstdio (MSVCRT.@)
3173 int CDECL _getmaxstdio(void)
3175 FIXME("stub, always returns 512\n");
3179 /*********************************************************************
3180 * _setmaxstdio_ (MSVCRT.@)
3182 int CDECL _setmaxstdio(int newmax)
3189 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3193 /*********************************************************************
3194 * __pioinfo (MSVCRT.@)
3195 * FIXME: see MSVCRT_MAX_FILES define.
3197 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3198 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3199 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3200 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3201 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3202 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3203 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3204 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3205 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3206 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3207 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3208 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3211 /*********************************************************************
3212 * __badioinfo (MSVCRT.@)
3214 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };