makefiles: Generate the config.status dependency as part of the makefile dependencies.
[wine/multimedia.git] / dlls / msvcrt / file.c
blob9dc1b54fdc421b4788a246a720ca7380f986f14f
1 /*
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
25 * TODO
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
29 #include "config.h"
30 #include "wine/port.h"
32 #include <time.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <sys/types.h>
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winternl.h"
43 #include "msvcrt.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 */
60 #define WX_OPEN 0x01
61 #define WX_ATEOF 0x02
62 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
63 #define WX_READCR 0x08 /* underlying file is at \r */
64 #define WX_DONTINHERIT 0x10
65 #define WX_APPEND 0x20
66 #define WX_TEXT 0x80
68 /* FIXME: this should be allocated dynamically */
69 #define MSVCRT_MAX_FILES 2048
71 typedef struct {
72 HANDLE handle;
73 unsigned char wxflag;
74 DWORD unkn[7]; /* critical section and init flag */
75 } ioinfo;
77 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
79 MSVCRT_FILE MSVCRT__iob[3] = { { 0 } };
81 static int MSVCRT_fdstart = 3; /* first unallocated fd */
82 static int MSVCRT_fdend = 3; /* highest allocated fd */
84 static MSVCRT_FILE* MSVCRT_fstreams[2048];
85 static int MSVCRT_stream_idx;
87 /* INTERNAL: process umask */
88 static int MSVCRT_umask = 0;
90 /* INTERNAL: Static buffer for temp file name */
91 static char MSVCRT_tmpname[MAX_PATH];
93 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
94 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
95 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
96 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
98 #define TOUL(x) (ULONGLONG)(x)
99 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
100 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
101 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
102 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
104 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
105 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
106 * and MSVCRT_stream_idx, from race conditions.
107 * It doesn't protect against race conditions manipulating the underlying files
108 * or flags; doing so would probably be better accomplished with per-file
109 * protection, rather than locking the whole table for every change.
111 static CRITICAL_SECTION MSVCRT_file_cs;
112 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
113 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
115 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
117 buf->st_dev = buf64->st_dev;
118 buf->st_ino = buf64->st_ino;
119 buf->st_mode = buf64->st_mode;
120 buf->st_nlink = buf64->st_nlink;
121 buf->st_uid = buf64->st_uid;
122 buf->st_gid = buf64->st_gid;
123 buf->st_rdev = buf64->st_rdev;
124 buf->st_size = buf64->st_size;
125 buf->st_atime = buf64->st_atime;
126 buf->st_mtime = buf64->st_mtime;
127 buf->st_ctime = buf64->st_ctime;
130 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
132 buf->st_dev = buf64->st_dev;
133 buf->st_ino = buf64->st_ino;
134 buf->st_mode = buf64->st_mode;
135 buf->st_nlink = buf64->st_nlink;
136 buf->st_uid = buf64->st_uid;
137 buf->st_gid = buf64->st_gid;
138 buf->st_rdev = buf64->st_rdev;
139 buf->st_size = buf64->st_size;
140 buf->st_atime = buf64->st_atime;
141 buf->st_mtime = buf64->st_mtime;
142 buf->st_ctime = buf64->st_ctime;
145 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
147 /* 1601 to 1970 is 369 years plus 89 leap days */
148 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
150 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
151 ft->dwHighDateTime = ticks >> 32;
152 ft->dwLowDateTime = ticks;
155 static inline BOOL msvcrt_is_valid_fd(int fd)
157 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
160 /* INTERNAL: Get the HANDLE for a fd
161 * This doesn't lock the table, because a failure will result in
162 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
163 * it returns a valid handle which is about to be closed, a subsequent call
164 * will fail, most likely in a sane way.
166 static HANDLE msvcrt_fdtoh(int fd)
168 if (!msvcrt_is_valid_fd(fd))
170 WARN(":fd (%d) - no handle!\n",fd);
171 *MSVCRT___doserrno() = 0;
172 *MSVCRT__errno() = MSVCRT_EBADF;
173 return INVALID_HANDLE_VALUE;
175 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
176 return MSVCRT_fdesc[fd].handle;
179 /* INTERNAL: free a file entry fd */
180 static void msvcrt_free_fd(int fd)
182 HANDLE old_handle;
184 LOCK_FILES();
185 old_handle = MSVCRT_fdesc[fd].handle;
186 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
187 MSVCRT_fdesc[fd].wxflag = 0;
188 TRACE(":fd (%d) freed\n",fd);
189 if (fd < 3) /* don't use 0,1,2 for user files */
191 switch (fd)
193 case 0:
194 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
195 break;
196 case 1:
197 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
198 break;
199 case 2:
200 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
201 break;
204 else
206 if (fd == MSVCRT_fdend - 1)
207 MSVCRT_fdend--;
208 if (fd < MSVCRT_fdstart)
209 MSVCRT_fdstart = fd;
211 UNLOCK_FILES();
214 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
215 /* caller must hold the files lock */
216 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
218 if (fd >= MSVCRT_MAX_FILES)
220 WARN(":files exhausted!\n");
221 return -1;
223 MSVCRT_fdesc[fd].handle = hand;
224 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
226 /* locate next free slot */
227 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
228 MSVCRT_fdstart = MSVCRT_fdend + 1;
229 else
230 while (MSVCRT_fdstart < MSVCRT_fdend &&
231 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
232 MSVCRT_fdstart++;
233 /* update last fd in use */
234 if (fd >= MSVCRT_fdend)
235 MSVCRT_fdend = fd + 1;
236 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
238 switch (fd)
240 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
241 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
242 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
245 return fd;
248 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
249 static int msvcrt_alloc_fd(HANDLE hand, int flag)
251 int ret;
253 LOCK_FILES();
254 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
255 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
256 UNLOCK_FILES();
257 return ret;
260 /* INTERNAL: Allocate a FILE* for an fd slot */
261 /* caller must hold the files lock */
262 static MSVCRT_FILE* msvcrt_alloc_fp(void)
264 unsigned int i;
266 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
268 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
270 if (!MSVCRT_fstreams[i])
272 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
273 return NULL;
274 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
276 return MSVCRT_fstreams[i];
279 return NULL;
282 /* INTERNAL: initialize a FILE* from an open fd */
283 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
285 TRACE(":fd (%d) allocating FILE*\n",fd);
286 if (!msvcrt_is_valid_fd(fd))
288 WARN(":invalid fd %d\n",fd);
289 *MSVCRT___doserrno() = 0;
290 *MSVCRT__errno() = MSVCRT_EBADF;
291 return -1;
293 memset(file, 0, sizeof(*file));
294 file->_file = fd;
295 file->_flag = stream_flags;
297 TRACE(":got FILE* (%p)\n",file);
298 return 0;
301 /* INTERNAL: Create an inheritance data block (for spawned process)
302 * The inheritance block is made of:
303 * 00 int nb of file descriptor (NBFD)
304 * 04 char file flags (wxflag): repeated for each fd
305 * 4+NBFD HANDLE file handle: repeated for each fd
307 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
309 int fd;
310 char* wxflag_ptr;
311 HANDLE* handle_ptr;
313 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
314 *block = MSVCRT_calloc(*size, 1);
315 if (!*block)
317 *size = 0;
318 return FALSE;
320 wxflag_ptr = (char*)*block + sizeof(unsigned);
321 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
323 *(unsigned*)*block = MSVCRT_fdend;
324 for (fd = 0; fd < MSVCRT_fdend; fd++)
326 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
327 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
329 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
330 *handle_ptr = MSVCRT_fdesc[fd].handle;
332 else
334 *wxflag_ptr = 0;
335 *handle_ptr = INVALID_HANDLE_VALUE;
337 wxflag_ptr++; handle_ptr++;
339 return TRUE;
342 /* INTERNAL: Set up all file descriptors,
343 * as well as default streams (stdin, stderr and stdout)
345 void msvcrt_init_io(void)
347 STARTUPINFOA si;
348 int i;
350 InitializeCriticalSection(&MSVCRT_file_cs);
351 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
352 GetStartupInfoA(&si);
353 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
355 BYTE* wxflag_ptr;
356 HANDLE* handle_ptr;
357 unsigned int count;
359 count = *(unsigned*)si.lpReserved2;
360 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
361 handle_ptr = (HANDLE*)(wxflag_ptr + count);
363 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
364 count = min(count, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
365 for (i = 0; i < count; i++)
367 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
369 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
370 MSVCRT_fdesc[i].handle = *handle_ptr;
372 else
374 MSVCRT_fdesc[i].wxflag = 0;
375 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
377 wxflag_ptr++; handle_ptr++;
379 MSVCRT_fdend = max( 3, count );
380 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
381 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
384 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
386 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
387 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
388 GetCurrentProcess(), &MSVCRT_fdesc[0].handle,
389 0, TRUE, DUPLICATE_SAME_ACCESS))
390 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
392 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
394 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
395 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
396 GetCurrentProcess(), &MSVCRT_fdesc[1].handle,
397 0, TRUE, DUPLICATE_SAME_ACCESS))
398 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
400 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
402 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
403 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
404 GetCurrentProcess(), &MSVCRT_fdesc[2].handle,
405 0, TRUE, DUPLICATE_SAME_ACCESS))
406 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
409 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
410 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
412 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
413 for (i = 0; i < 3; i++)
415 /* FILE structs for stdin/out/err are static and never deleted */
416 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
417 MSVCRT__iob[i]._file = i;
418 MSVCRT__iob[i]._tmpfname = NULL;
419 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
421 MSVCRT_stream_idx = 3;
424 /* INTERNAL: Flush stdio file buffer */
425 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
427 if(file->_bufsiz) {
428 int cnt=file->_ptr-file->_base;
429 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
430 file->_flag |= MSVCRT__IOERR;
431 return MSVCRT_EOF;
433 file->_ptr=file->_base;
434 file->_cnt=file->_bufsiz;
436 return 0;
439 /* INTERNAL: Allocate stdio file buffer */
440 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
442 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
443 if(file->_base) {
444 file->_bufsiz = MSVCRT_BUFSIZ;
445 file->_flag |= MSVCRT__IOMYBUF;
446 } else {
447 file->_base = (char*)(&file->_charbuf);
448 /* put here 2 ??? */
449 file->_bufsiz = sizeof(file->_charbuf);
451 file->_ptr = file->_base;
452 file->_cnt = 0;
455 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
456 static void msvcrt_int_to_base32(int num, char *str)
458 char *p;
459 int n = num;
460 int digits = 0;
462 while (n != 0)
464 n >>= 5;
465 digits++;
467 p = str + digits;
468 *p = 0;
469 while (--p >= str)
471 *p = (num & 31) + '0';
472 if (*p > '9')
473 *p += ('a' - '0' - 10);
474 num >>= 5;
478 /*********************************************************************
479 * __iob_func(MSVCRT.@)
481 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
483 return &MSVCRT__iob[0];
486 /*********************************************************************
487 * _access (MSVCRT.@)
489 int CDECL MSVCRT__access(const char *filename, int mode)
491 DWORD attr = GetFileAttributesA(filename);
493 TRACE("(%s,%d) %d\n",filename,mode,attr);
495 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
497 msvcrt_set_errno(GetLastError());
498 return -1;
500 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
502 msvcrt_set_errno(ERROR_ACCESS_DENIED);
503 return -1;
505 return 0;
508 /*********************************************************************
509 * _waccess (MSVCRT.@)
511 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
513 DWORD attr = GetFileAttributesW(filename);
515 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
517 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
519 msvcrt_set_errno(GetLastError());
520 return -1;
522 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
524 msvcrt_set_errno(ERROR_ACCESS_DENIED);
525 return -1;
527 return 0;
530 /*********************************************************************
531 * _chmod (MSVCRT.@)
533 int CDECL MSVCRT__chmod(const char *path, int flags)
535 DWORD oldFlags = GetFileAttributesA(path);
537 if (oldFlags != INVALID_FILE_ATTRIBUTES)
539 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
540 oldFlags | FILE_ATTRIBUTE_READONLY;
542 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
543 return 0;
545 msvcrt_set_errno(GetLastError());
546 return -1;
549 /*********************************************************************
550 * _wchmod (MSVCRT.@)
552 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
554 DWORD oldFlags = GetFileAttributesW(path);
556 if (oldFlags != INVALID_FILE_ATTRIBUTES)
558 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
559 oldFlags | FILE_ATTRIBUTE_READONLY;
561 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
562 return 0;
564 msvcrt_set_errno(GetLastError());
565 return -1;
568 /*********************************************************************
569 * _unlink (MSVCRT.@)
571 int CDECL MSVCRT__unlink(const char *path)
573 TRACE("%s\n",debugstr_a(path));
574 if(DeleteFileA(path))
575 return 0;
576 TRACE("failed (%d)\n",GetLastError());
577 msvcrt_set_errno(GetLastError());
578 return -1;
581 /*********************************************************************
582 * _wunlink (MSVCRT.@)
584 int CDECL _wunlink(const MSVCRT_wchar_t *path)
586 TRACE("(%s)\n",debugstr_w(path));
587 if(DeleteFileW(path))
588 return 0;
589 TRACE("failed (%d)\n",GetLastError());
590 msvcrt_set_errno(GetLastError());
591 return -1;
594 /* _flushall calls MSVCRT_fflush which calls _flushall */
595 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
597 /*********************************************************************
598 * _flushall (MSVCRT.@)
600 int CDECL _flushall(void)
602 int i, num_flushed = 0;
604 LOCK_FILES();
605 for (i = 3; i < MSVCRT_stream_idx; i++)
606 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
608 #if 0
609 /* FIXME: flush, do not commit */
610 if (_commit(i) == -1)
611 if (MSVCRT_fstreams[i])
612 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
613 #endif
614 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
615 MSVCRT_fflush(MSVCRT_fstreams[i]);
616 num_flushed++;
619 UNLOCK_FILES();
621 TRACE(":flushed (%d) handles\n",num_flushed);
622 return num_flushed;
625 /*********************************************************************
626 * fflush (MSVCRT.@)
628 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
630 if(!file) {
631 _flushall();
632 } else if(file->_flag & MSVCRT__IOWRT) {
633 int res=msvcrt_flush_buffer(file);
634 return res;
636 return 0;
639 /*********************************************************************
640 * _close (MSVCRT.@)
642 int CDECL MSVCRT__close(int fd)
644 HANDLE hand;
645 int ret;
647 LOCK_FILES();
648 hand = msvcrt_fdtoh(fd);
649 TRACE(":fd (%d) handle (%p)\n",fd,hand);
650 if (hand == INVALID_HANDLE_VALUE)
651 ret = -1;
652 else if (!CloseHandle(hand))
654 WARN(":failed-last error (%d)\n",GetLastError());
655 msvcrt_set_errno(GetLastError());
656 ret = -1;
658 else
660 msvcrt_free_fd(fd);
661 ret = 0;
663 UNLOCK_FILES();
664 TRACE(":ok\n");
665 return ret;
668 /*********************************************************************
669 * _commit (MSVCRT.@)
671 int CDECL _commit(int fd)
673 HANDLE hand = msvcrt_fdtoh(fd);
675 TRACE(":fd (%d) handle (%p)\n",fd,hand);
676 if (hand == INVALID_HANDLE_VALUE)
677 return -1;
679 if (!FlushFileBuffers(hand))
681 if (GetLastError() == ERROR_INVALID_HANDLE)
683 /* FlushFileBuffers fails for console handles
684 * so we ignore this error.
686 return 0;
688 TRACE(":failed-last error (%d)\n",GetLastError());
689 msvcrt_set_errno(GetLastError());
690 return -1;
692 TRACE(":ok\n");
693 return 0;
696 /*********************************************************************
697 * _dup2 (MSVCRT.@)
698 * NOTES
699 * MSDN isn't clear on this point, but the remarks for _pipe
700 * indicate file descriptors duplicated with _dup and _dup2 are always
701 * inheritable.
703 int CDECL MSVCRT__dup2(int od, int nd)
705 int ret;
707 TRACE("(od=%d, nd=%d)\n", od, nd);
708 LOCK_FILES();
709 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
711 HANDLE handle;
713 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
714 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
716 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
718 if (msvcrt_is_valid_fd(nd))
719 MSVCRT__close(nd);
720 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
721 if (ret == -1)
723 CloseHandle(handle);
724 *MSVCRT__errno() = MSVCRT_EMFILE;
726 else
728 /* _dup2 returns 0, not nd, on success */
729 ret = 0;
732 else
734 ret = -1;
735 msvcrt_set_errno(GetLastError());
738 else
740 *MSVCRT__errno() = MSVCRT_EBADF;
741 ret = -1;
743 UNLOCK_FILES();
744 return ret;
747 /*********************************************************************
748 * _dup (MSVCRT.@)
750 int CDECL MSVCRT__dup(int od)
752 int fd, ret;
754 LOCK_FILES();
755 fd = MSVCRT_fdstart;
756 if (MSVCRT__dup2(od, fd) == 0)
757 ret = fd;
758 else
759 ret = -1;
760 UNLOCK_FILES();
761 return ret;
764 /*********************************************************************
765 * _eof (MSVCRT.@)
767 int CDECL _eof(int fd)
769 DWORD curpos,endpos;
770 LONG hcurpos,hendpos;
771 HANDLE hand = msvcrt_fdtoh(fd);
773 TRACE(":fd (%d) handle (%p)\n",fd,hand);
775 if (hand == INVALID_HANDLE_VALUE)
776 return -1;
778 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
780 /* Otherwise we do it the hard way */
781 hcurpos = hendpos = 0;
782 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
783 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
785 if (curpos == endpos && hcurpos == hendpos)
787 /* FIXME: shouldn't WX_ATEOF be set here? */
788 return TRUE;
791 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
792 return FALSE;
795 /*********************************************************************
796 * _fcloseall (MSVCRT.@)
798 int CDECL MSVCRT__fcloseall(void)
800 int num_closed = 0, i;
802 LOCK_FILES();
803 for (i = 3; i < MSVCRT_stream_idx; i++)
804 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
805 !MSVCRT_fclose(MSVCRT_fstreams[i]))
806 num_closed++;
807 UNLOCK_FILES();
809 TRACE(":closed (%d) handles\n",num_closed);
810 return num_closed;
813 /* free everything on process exit */
814 void msvcrt_free_io(void)
816 MSVCRT__fcloseall();
817 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
818 * stdout, and stderr (unlike GNU), so we need to fclose() them here
819 * or they won't get flushed.
821 MSVCRT_fclose(&MSVCRT__iob[0]);
822 MSVCRT_fclose(&MSVCRT__iob[1]);
823 MSVCRT_fclose(&MSVCRT__iob[2]);
824 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
825 DeleteCriticalSection(&MSVCRT_file_cs);
828 /*********************************************************************
829 * _lseeki64 (MSVCRT.@)
831 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
833 HANDLE hand = msvcrt_fdtoh(fd);
834 LARGE_INTEGER ofs, ret;
836 TRACE(":fd (%d) handle (%p)\n",fd,hand);
837 if (hand == INVALID_HANDLE_VALUE)
838 return -1;
840 if (whence < 0 || whence > 2)
842 *MSVCRT__errno() = MSVCRT_EINVAL;
843 return -1;
846 TRACE(":fd (%d) to %s pos %s\n",
847 fd,wine_dbgstr_longlong(offset),
848 (whence==SEEK_SET)?"SEEK_SET":
849 (whence==SEEK_CUR)?"SEEK_CUR":
850 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
852 ofs.QuadPart = offset;
853 if (SetFilePointerEx(hand, ofs, &ret, whence))
855 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
856 /* FIXME: What if we seek _to_ EOF - is EOF set? */
858 return ret.QuadPart;
860 TRACE(":error-last error (%d)\n",GetLastError());
861 msvcrt_set_errno(GetLastError());
862 return -1;
865 /*********************************************************************
866 * _lseek (MSVCRT.@)
868 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
870 return MSVCRT__lseeki64(fd, offset, whence);
873 /*********************************************************************
874 * _locking (MSVCRT.@)
876 * This is untested; the underlying LockFile doesn't work yet.
878 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
880 BOOL ret;
881 DWORD cur_locn;
882 HANDLE hand = msvcrt_fdtoh(fd);
884 TRACE(":fd (%d) handle (%p)\n",fd,hand);
885 if (hand == INVALID_HANDLE_VALUE)
886 return -1;
888 if (mode < 0 || mode > 4)
890 *MSVCRT__errno() = MSVCRT_EINVAL;
891 return -1;
894 TRACE(":fd (%d) by 0x%08x mode %s\n",
895 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
896 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
897 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
898 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
899 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
900 "UNKNOWN");
902 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
904 FIXME ("Seek failed\n");
905 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
906 return -1;
908 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
910 int nretry = 10;
911 ret = 1; /* just to satisfy gcc */
912 while (nretry--)
914 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
915 if (ret) break;
916 Sleep(1);
919 else if (mode == MSVCRT__LK_UNLCK)
920 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
921 else
922 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
923 /* FIXME - what about error settings? */
924 return ret ? 0 : -1;
927 /*********************************************************************
928 * fseek (MSVCRT.@)
930 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
932 /* Flush output if needed */
933 if(file->_flag & MSVCRT__IOWRT)
934 msvcrt_flush_buffer(file);
936 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
937 offset -= file->_cnt;
938 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
939 /* Black magic correction for CR removal */
940 int i;
941 for (i=0; i<file->_cnt; i++) {
942 if (file->_ptr[i] == '\n')
943 offset--;
945 /* Black magic when reading CR at buffer boundary*/
946 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
947 offset--;
950 /* Discard buffered input */
951 file->_cnt = 0;
952 file->_ptr = file->_base;
953 /* Reset direction of i/o */
954 if(file->_flag & MSVCRT__IORW) {
955 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
957 /* Clear end of file flag */
958 file->_flag &= ~MSVCRT__IOEOF;
959 return (MSVCRT__lseek(file->_file,offset,whence) == -1)?-1:0;
962 /*********************************************************************
963 * _chsize (MSVCRT.@)
965 int CDECL _chsize(int fd, MSVCRT_long size)
967 LONG cur, pos;
968 HANDLE handle;
969 BOOL ret = FALSE;
971 TRACE("(fd=%d, size=%d)\n", fd, size);
973 LOCK_FILES();
975 handle = msvcrt_fdtoh(fd);
976 if (handle != INVALID_HANDLE_VALUE)
978 /* save the current file pointer */
979 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
980 if (cur >= 0)
982 pos = MSVCRT__lseek(fd, size, SEEK_SET);
983 if (pos >= 0)
985 ret = SetEndOfFile(handle);
986 if (!ret) msvcrt_set_errno(GetLastError());
989 /* restore the file pointer */
990 MSVCRT__lseek(fd, cur, SEEK_SET);
994 UNLOCK_FILES();
995 return ret ? 0 : -1;
998 /*********************************************************************
999 * clearerr (MSVCRT.@)
1001 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1003 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1004 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1007 /*********************************************************************
1008 * rewind (MSVCRT.@)
1010 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1012 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1013 MSVCRT_fseek(file, 0L, SEEK_SET);
1014 MSVCRT_clearerr(file);
1017 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1019 int plus = strchrW(mode, '+') != NULL;
1021 switch(*mode++)
1023 case 'R': case 'r':
1024 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1025 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1026 break;
1027 case 'W': case 'w':
1028 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1029 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1030 break;
1031 case 'A': case 'a':
1032 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1033 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1034 break;
1035 default:
1036 return -1;
1039 while (*mode)
1040 switch (*mode++)
1042 case 'B': case 'b':
1043 *open_flags |= MSVCRT__O_BINARY;
1044 *open_flags &= ~MSVCRT__O_TEXT;
1045 break;
1046 case 'T': case 't':
1047 *open_flags |= MSVCRT__O_TEXT;
1048 *open_flags &= ~MSVCRT__O_BINARY;
1049 break;
1050 case '+':
1051 case ' ':
1052 break;
1053 default:
1054 FIXME(":unknown flag %c not supported\n",mode[-1]);
1056 return 0;
1059 /*********************************************************************
1060 * _fdopen (MSVCRT.@)
1062 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1064 MSVCRT_FILE *ret;
1065 MSVCRT_wchar_t *modeW = NULL;
1067 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1069 ret = MSVCRT__wfdopen(fd, modeW);
1071 MSVCRT_free(modeW);
1072 return ret;
1075 /*********************************************************************
1076 * _wfdopen (MSVCRT.@)
1078 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1080 int open_flags, stream_flags;
1081 MSVCRT_FILE* file;
1083 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1085 LOCK_FILES();
1086 if (!(file = msvcrt_alloc_fp()))
1087 file = NULL;
1088 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1090 file->_flag = 0;
1091 file = NULL;
1093 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1094 UNLOCK_FILES();
1096 return file;
1099 /*********************************************************************
1100 * _filelength (MSVCRT.@)
1102 LONG CDECL MSVCRT__filelength(int fd)
1104 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1105 if (curPos != -1)
1107 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1108 if (endPos != -1)
1110 if (endPos != curPos)
1111 MSVCRT__lseek(fd, curPos, SEEK_SET);
1112 return endPos;
1115 return -1;
1118 /*********************************************************************
1119 * _filelengthi64 (MSVCRT.@)
1121 __int64 CDECL MSVCRT__filelengthi64(int fd)
1123 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1124 if (curPos != -1)
1126 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1127 if (endPos != -1)
1129 if (endPos != curPos)
1130 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1131 return endPos;
1134 return -1;
1137 /*********************************************************************
1138 * _fileno (MSVCRT.@)
1140 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1142 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1143 return file->_file;
1146 /*********************************************************************
1147 * _fstat64 (MSVCRT.@)
1149 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1151 DWORD dw;
1152 DWORD type;
1153 BY_HANDLE_FILE_INFORMATION hfi;
1154 HANDLE hand = msvcrt_fdtoh(fd);
1156 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1157 if (hand == INVALID_HANDLE_VALUE)
1158 return -1;
1160 if (!buf)
1162 WARN(":failed-NULL buf\n");
1163 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1164 return -1;
1167 memset(&hfi, 0, sizeof(hfi));
1168 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1169 type = GetFileType(hand);
1170 if (type == FILE_TYPE_PIPE)
1172 buf->st_dev = buf->st_rdev = fd;
1173 buf->st_mode = S_IFIFO;
1174 buf->st_nlink = 1;
1176 else if (type == FILE_TYPE_CHAR)
1178 buf->st_dev = buf->st_rdev = fd;
1179 buf->st_mode = S_IFCHR;
1180 buf->st_nlink = 1;
1182 else /* FILE_TYPE_DISK etc. */
1184 if (!GetFileInformationByHandle(hand, &hfi))
1186 WARN(":failed-last error (%d)\n",GetLastError());
1187 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1188 return -1;
1190 buf->st_mode = S_IFREG | 0444;
1191 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1192 buf->st_mode |= 0222;
1193 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1194 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1195 buf->st_atime = dw;
1196 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1197 buf->st_mtime = buf->st_ctime = dw;
1198 buf->st_nlink = hfi.nNumberOfLinks;
1200 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1201 buf->st_mode);
1202 return 0;
1205 /*********************************************************************
1206 * _fstati64 (MSVCRT.@)
1208 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1210 int ret;
1211 struct MSVCRT__stat64 buf64;
1213 ret = MSVCRT__fstat64(fd, &buf64);
1214 if (!ret)
1215 msvcrt_stat64_to_stati64(&buf64, buf);
1216 return ret;
1219 /*********************************************************************
1220 * _fstat (MSVCRT.@)
1222 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1223 { int ret;
1224 struct MSVCRT__stat64 buf64;
1226 ret = MSVCRT__fstat64(fd, &buf64);
1227 if (!ret)
1228 msvcrt_stat64_to_stat(&buf64, buf);
1229 return ret;
1232 /*********************************************************************
1233 * _futime64 (MSVCRT.@)
1235 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1237 HANDLE hand = msvcrt_fdtoh(fd);
1238 FILETIME at, wt;
1240 if (!t)
1242 time_to_filetime( MSVCRT__time64(NULL), &at );
1243 wt = at;
1245 else
1247 time_to_filetime( t->actime, &at );
1248 time_to_filetime( t->modtime, &wt );
1251 if (!SetFileTime(hand, NULL, &at, &wt))
1253 msvcrt_set_errno(GetLastError());
1254 return -1 ;
1256 return 0;
1259 /*********************************************************************
1260 * _futime32 (MSVCRT.@)
1262 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1264 struct MSVCRT___utimbuf64 t64;
1265 t64.actime = t->actime;
1266 t64.modtime = t->modtime;
1267 return _futime64( fd, &t64 );
1270 /*********************************************************************
1271 * _futime (MSVCRT.@)
1273 #ifdef _WIN64
1274 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1276 return _futime64( fd, t );
1278 #else
1279 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1281 return _futime32( fd, t );
1283 #endif
1285 /*********************************************************************
1286 * _get_osfhandle (MSVCRT.@)
1288 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1290 HANDLE hand = msvcrt_fdtoh(fd);
1291 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1293 return (MSVCRT_intptr_t)hand;
1296 /*********************************************************************
1297 * _isatty (MSVCRT.@)
1299 int CDECL _isatty(int fd)
1301 HANDLE hand = msvcrt_fdtoh(fd);
1303 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1304 if (hand == INVALID_HANDLE_VALUE)
1305 return 0;
1307 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1310 /*********************************************************************
1311 * _mktemp (MSVCRT.@)
1313 char * CDECL _mktemp(char *pattern)
1315 int numX = 0;
1316 char *retVal = pattern;
1317 int id;
1318 char letter = 'a';
1320 while(*pattern)
1321 numX = (*pattern++ == 'X')? numX + 1 : 0;
1322 if (numX < 5)
1323 return NULL;
1324 pattern--;
1325 id = GetCurrentProcessId();
1326 numX = 6;
1327 while(numX--)
1329 int tempNum = id / 10;
1330 *pattern-- = id - (tempNum * 10) + '0';
1331 id = tempNum;
1333 pattern++;
1336 *pattern = letter++;
1337 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1338 GetLastError() == ERROR_FILE_NOT_FOUND)
1339 return retVal;
1340 } while(letter <= 'z');
1341 return NULL;
1344 /*********************************************************************
1345 * _wmktemp (MSVCRT.@)
1347 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1349 int numX = 0;
1350 MSVCRT_wchar_t *retVal = pattern;
1351 int id;
1352 MSVCRT_wchar_t letter = 'a';
1354 while(*pattern)
1355 numX = (*pattern++ == 'X')? numX + 1 : 0;
1356 if (numX < 5)
1357 return NULL;
1358 pattern--;
1359 id = GetCurrentProcessId();
1360 numX = 6;
1361 while(numX--)
1363 int tempNum = id / 10;
1364 *pattern-- = id - (tempNum * 10) + '0';
1365 id = tempNum;
1367 pattern++;
1370 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1371 GetLastError() == ERROR_FILE_NOT_FOUND)
1372 return retVal;
1373 *pattern = letter++;
1374 } while(letter != '|');
1375 return NULL;
1378 static unsigned split_oflags(unsigned oflags)
1380 int wxflags = 0;
1381 unsigned unsupp; /* until we support everything */
1383 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1384 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1385 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1386 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1387 else wxflags |= WX_TEXT; /* default to TEXT*/
1388 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1390 if ((unsupp = oflags & ~(
1391 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1392 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1393 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1394 MSVCRT__O_NOINHERIT|
1395 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1397 ERR(":unsupported oflags 0x%04x\n",unsupp);
1399 return wxflags;
1402 /*********************************************************************
1403 * _pipe (MSVCRT.@)
1405 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1407 int ret = -1;
1408 SECURITY_ATTRIBUTES sa;
1409 HANDLE readHandle, writeHandle;
1411 if (!pfds)
1413 *MSVCRT__errno() = MSVCRT_EINVAL;
1414 return -1;
1417 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1418 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1419 sa.lpSecurityDescriptor = NULL;
1420 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1422 unsigned int wxflags = split_oflags(textmode);
1423 int fd;
1425 LOCK_FILES();
1426 fd = msvcrt_alloc_fd(readHandle, wxflags);
1427 if (fd != -1)
1429 pfds[0] = fd;
1430 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1431 if (fd != -1)
1433 pfds[1] = fd;
1434 ret = 0;
1436 else
1438 MSVCRT__close(pfds[0]);
1439 CloseHandle(writeHandle);
1440 *MSVCRT__errno() = MSVCRT_EMFILE;
1443 else
1445 CloseHandle(readHandle);
1446 CloseHandle(writeHandle);
1447 *MSVCRT__errno() = MSVCRT_EMFILE;
1449 UNLOCK_FILES();
1451 else
1452 msvcrt_set_errno(GetLastError());
1454 return ret;
1457 /*********************************************************************
1458 * _sopen (MSVCRT.@)
1460 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1462 __ms_va_list ap;
1463 int pmode;
1464 DWORD access = 0, creation = 0, attrib;
1465 DWORD sharing;
1466 int wxflag = 0, fd;
1467 HANDLE hand;
1468 SECURITY_ATTRIBUTES sa;
1471 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1472 path, oflags, shflags);
1474 wxflag = split_oflags(oflags);
1475 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1477 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1478 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1479 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1482 if (oflags & MSVCRT__O_CREAT)
1484 __ms_va_start(ap, shflags);
1485 pmode = va_arg(ap, int);
1486 __ms_va_end(ap);
1488 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1489 FIXME(": pmode 0x%04x ignored\n", pmode);
1490 else
1491 WARN(": pmode 0x%04x ignored\n", pmode);
1493 if (oflags & MSVCRT__O_EXCL)
1494 creation = CREATE_NEW;
1495 else if (oflags & MSVCRT__O_TRUNC)
1496 creation = CREATE_ALWAYS;
1497 else
1498 creation = OPEN_ALWAYS;
1500 else /* no MSVCRT__O_CREAT */
1502 if (oflags & MSVCRT__O_TRUNC)
1503 creation = TRUNCATE_EXISTING;
1504 else
1505 creation = OPEN_EXISTING;
1508 switch( shflags )
1510 case MSVCRT__SH_DENYRW:
1511 sharing = 0L;
1512 break;
1513 case MSVCRT__SH_DENYWR:
1514 sharing = FILE_SHARE_READ;
1515 break;
1516 case MSVCRT__SH_DENYRD:
1517 sharing = FILE_SHARE_WRITE;
1518 break;
1519 case MSVCRT__SH_DENYNO:
1520 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1521 break;
1522 default:
1523 ERR( "Unhandled shflags 0x%x\n", shflags );
1524 return -1;
1526 attrib = FILE_ATTRIBUTE_NORMAL;
1528 if (oflags & MSVCRT__O_TEMPORARY)
1530 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1531 access |= DELETE;
1532 sharing |= FILE_SHARE_DELETE;
1535 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1536 sa.lpSecurityDescriptor = NULL;
1537 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1539 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1541 if (hand == INVALID_HANDLE_VALUE) {
1542 WARN(":failed-last error (%d)\n",GetLastError());
1543 msvcrt_set_errno(GetLastError());
1544 return -1;
1547 fd = msvcrt_alloc_fd(hand, wxflag);
1549 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1550 return fd;
1553 /*********************************************************************
1554 * _wsopen (MSVCRT.@)
1556 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1558 __ms_va_list ap;
1559 int pmode;
1560 DWORD access = 0, creation = 0, attrib;
1561 DWORD sharing;
1562 int wxflag = 0, fd;
1563 HANDLE hand;
1564 SECURITY_ATTRIBUTES sa;
1567 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1568 debugstr_w(path), oflags, shflags);
1570 wxflag = split_oflags(oflags);
1571 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1573 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1574 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1575 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1578 if (oflags & MSVCRT__O_CREAT)
1580 __ms_va_start(ap, shflags);
1581 pmode = va_arg(ap, int);
1582 __ms_va_end(ap);
1584 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1585 FIXME(": pmode 0x%04x ignored\n", pmode);
1586 else
1587 WARN(": pmode 0x%04x ignored\n", pmode);
1589 if (oflags & MSVCRT__O_EXCL)
1590 creation = CREATE_NEW;
1591 else if (oflags & MSVCRT__O_TRUNC)
1592 creation = CREATE_ALWAYS;
1593 else
1594 creation = OPEN_ALWAYS;
1596 else /* no MSVCRT__O_CREAT */
1598 if (oflags & MSVCRT__O_TRUNC)
1599 creation = TRUNCATE_EXISTING;
1600 else
1601 creation = OPEN_EXISTING;
1604 switch( shflags )
1606 case MSVCRT__SH_DENYRW:
1607 sharing = 0L;
1608 break;
1609 case MSVCRT__SH_DENYWR:
1610 sharing = FILE_SHARE_READ;
1611 break;
1612 case MSVCRT__SH_DENYRD:
1613 sharing = FILE_SHARE_WRITE;
1614 break;
1615 case MSVCRT__SH_DENYNO:
1616 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1617 break;
1618 default:
1619 ERR( "Unhandled shflags 0x%x\n", shflags );
1620 return -1;
1622 attrib = FILE_ATTRIBUTE_NORMAL;
1624 if (oflags & MSVCRT__O_TEMPORARY)
1626 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1627 access |= DELETE;
1628 sharing |= FILE_SHARE_DELETE;
1631 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1632 sa.lpSecurityDescriptor = NULL;
1633 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1635 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1637 if (hand == INVALID_HANDLE_VALUE) {
1638 WARN(":failed-last error (%d)\n",GetLastError());
1639 msvcrt_set_errno(GetLastError());
1640 return -1;
1643 fd = msvcrt_alloc_fd(hand, wxflag);
1645 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1646 return fd;
1649 /*********************************************************************
1650 * _open (MSVCRT.@)
1652 int CDECL MSVCRT__open( const char *path, int flags, ... )
1654 __ms_va_list ap;
1656 if (flags & MSVCRT__O_CREAT)
1658 int pmode;
1659 __ms_va_start(ap, flags);
1660 pmode = va_arg(ap, int);
1661 __ms_va_end(ap);
1662 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1664 else
1665 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1668 /*********************************************************************
1669 * _wopen (MSVCRT.@)
1671 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1673 __ms_va_list ap;
1675 if (flags & MSVCRT__O_CREAT)
1677 int pmode;
1678 __ms_va_start(ap, flags);
1679 pmode = va_arg(ap, int);
1680 __ms_va_end(ap);
1681 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1683 else
1684 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1687 /*********************************************************************
1688 * _creat (MSVCRT.@)
1690 int CDECL MSVCRT__creat(const char *path, int flags)
1692 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1693 return MSVCRT__open(path, usedFlags);
1696 /*********************************************************************
1697 * _wcreat (MSVCRT.@)
1699 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1701 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1702 return _wopen(path, usedFlags);
1705 /*********************************************************************
1706 * _open_osfhandle (MSVCRT.@)
1708 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1710 int fd;
1712 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1713 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1714 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1715 * text - it never sets MSVCRT__O_BINARY.
1717 /* don't let split_oflags() decide the mode if no mode is passed */
1718 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1719 oflags |= MSVCRT__O_BINARY;
1721 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1722 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1723 return fd;
1726 /*********************************************************************
1727 * _rmtmp (MSVCRT.@)
1729 int CDECL _rmtmp(void)
1731 int num_removed = 0, i;
1733 LOCK_FILES();
1734 for (i = 3; i < MSVCRT_stream_idx; i++)
1735 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1737 MSVCRT_fclose(MSVCRT_fstreams[i]);
1738 num_removed++;
1740 UNLOCK_FILES();
1742 if (num_removed)
1743 TRACE(":removed (%d) temp files\n",num_removed);
1744 return num_removed;
1747 /*********************************************************************
1748 * (internal) read_i
1750 * When reading \r as last character in text mode, read() positions
1751 * the file pointer on the \r character while getc() goes on to
1752 * the following \n
1754 static int read_i(int fd, void *buf, unsigned int count)
1756 DWORD num_read;
1757 char *bufstart = buf;
1758 HANDLE hand = msvcrt_fdtoh(fd);
1760 if (count == 0)
1761 return 0;
1763 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1764 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1765 TRACE("already at EOF, returning 0\n");
1766 return 0;
1768 /* Don't trace small reads, it gets *very* annoying */
1769 if (count > 4)
1770 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1771 if (hand == INVALID_HANDLE_VALUE)
1772 return -1;
1774 /* Reading single bytes in O_TEXT mode makes things slow
1775 * So read big chunks
1777 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1779 if (count != 0 && num_read == 0)
1781 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1782 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1784 else if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1786 DWORD i, j;
1787 if (bufstart[num_read-1] == '\r')
1789 if(count == 1)
1791 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1792 ReadFile(hand, bufstart, 1, &num_read, NULL);
1794 else
1796 MSVCRT_fdesc[fd].wxflag |= WX_READCR;
1797 num_read--;
1800 else
1801 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1802 for (i=0, j=0; i<num_read; i++)
1804 /* in text mode, a ctrl-z signals EOF */
1805 if (bufstart[i] == 0x1a)
1807 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1808 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1809 break;
1811 /* in text mode, strip \r if followed by \n.
1812 * BUG: should save state across calls somehow, so CR LF that
1813 * straddles buffer boundary gets recognized properly?
1815 if ((bufstart[i] != '\r')
1816 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1817 bufstart[j++] = bufstart[i];
1819 num_read = j;
1822 else
1824 if (GetLastError() == ERROR_BROKEN_PIPE)
1826 TRACE(":end-of-pipe\n");
1827 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1828 return 0;
1830 else
1832 TRACE(":failed-last error (%d)\n",GetLastError());
1833 return -1;
1837 if (count > 4)
1838 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1839 return num_read;
1842 /*********************************************************************
1843 * _read (MSVCRT.@)
1845 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1847 int num_read;
1848 num_read = read_i(fd, buf, count);
1849 return num_read;
1852 /*********************************************************************
1853 * _setmode (MSVCRT.@)
1855 int CDECL _setmode(int fd,int mode)
1857 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1858 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1859 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1860 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1861 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1862 else
1863 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1864 return ret;
1867 /*********************************************************************
1868 * _stat64 (MSVCRT.@)
1870 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
1872 DWORD dw;
1873 WIN32_FILE_ATTRIBUTE_DATA hfi;
1874 unsigned short mode = ALL_S_IREAD;
1875 int plen;
1877 TRACE(":file (%s) buf(%p)\n",path,buf);
1879 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1881 TRACE("failed (%d)\n",GetLastError());
1882 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1883 return -1;
1886 memset(buf,0,sizeof(struct MSVCRT__stat64));
1888 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1889 Bon 011120: This FIXME seems incorrect
1890 Also a letter as first char isn't enough to be classified
1891 as a drive letter
1893 if (isalpha(*path)&& (*(path+1)==':'))
1894 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1895 else
1896 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1898 plen = strlen(path);
1900 /* Dir, or regular file? */
1901 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1902 (path[plen-1] == '\\'))
1903 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1904 else
1906 mode |= MSVCRT__S_IFREG;
1907 /* executable? */
1908 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1910 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1911 (tolower(path[plen-3]) << 16);
1912 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1913 mode |= ALL_S_IEXEC;
1917 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1918 mode |= ALL_S_IWRITE;
1920 buf->st_mode = mode;
1921 buf->st_nlink = 1;
1922 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1923 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1924 buf->st_atime = dw;
1925 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1926 buf->st_mtime = buf->st_ctime = dw;
1927 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
1928 (int)(buf->st_size >> 32),(int)buf->st_size,
1929 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
1930 return 0;
1933 /*********************************************************************
1934 * _stati64 (MSVCRT.@)
1936 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
1938 int ret;
1939 struct MSVCRT__stat64 buf64;
1941 ret = MSVCRT_stat64(path, &buf64);
1942 if (!ret)
1943 msvcrt_stat64_to_stati64(&buf64, buf);
1944 return ret;
1947 /*********************************************************************
1948 * _stat (MSVCRT.@)
1950 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
1951 { int ret;
1952 struct MSVCRT__stat64 buf64;
1954 ret = MSVCRT_stat64( path, &buf64);
1955 if (!ret)
1956 msvcrt_stat64_to_stat(&buf64, buf);
1957 return ret;
1960 /*********************************************************************
1961 * _wstat64 (MSVCRT.@)
1963 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1965 DWORD dw;
1966 WIN32_FILE_ATTRIBUTE_DATA hfi;
1967 unsigned short mode = ALL_S_IREAD;
1968 int plen;
1970 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1972 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1974 TRACE("failed (%d)\n",GetLastError());
1975 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1976 return -1;
1979 memset(buf,0,sizeof(struct MSVCRT__stat64));
1981 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1982 if (MSVCRT_iswalpha(*path))
1983 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1984 else
1985 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1987 plen = strlenW(path);
1989 /* Dir, or regular file? */
1990 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1991 (path[plen-1] == '\\'))
1992 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1993 else
1995 mode |= MSVCRT__S_IFREG;
1996 /* executable? */
1997 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1999 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2000 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2001 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2002 mode |= ALL_S_IEXEC;
2006 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2007 mode |= ALL_S_IWRITE;
2009 buf->st_mode = mode;
2010 buf->st_nlink = 1;
2011 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2012 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2013 buf->st_atime = dw;
2014 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2015 buf->st_mtime = buf->st_ctime = dw;
2016 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2017 (int)(buf->st_size >> 32),(int)buf->st_size,
2018 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2019 return 0;
2022 /*********************************************************************
2023 * _wstati64 (MSVCRT.@)
2025 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2027 int ret;
2028 struct MSVCRT__stat64 buf64;
2030 ret = MSVCRT__wstat64(path, &buf64);
2031 if (!ret)
2032 msvcrt_stat64_to_stati64(&buf64, buf);
2033 return ret;
2036 /*********************************************************************
2037 * _wstat (MSVCRT.@)
2039 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2041 int ret;
2042 struct MSVCRT__stat64 buf64;
2044 ret = MSVCRT__wstat64( path, &buf64 );
2045 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2046 return ret;
2049 /*********************************************************************
2050 * _tell (MSVCRT.@)
2052 MSVCRT_long CDECL _tell(int fd)
2054 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2057 /*********************************************************************
2058 * _telli64 (MSVCRT.@)
2060 __int64 CDECL _telli64(int fd)
2062 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2065 /*********************************************************************
2066 * _tempnam (MSVCRT.@)
2068 char * CDECL _tempnam(const char *dir, const char *prefix)
2070 char tmpbuf[MAX_PATH];
2071 const char *tmp_dir = MSVCRT_getenv("TMP");
2073 if (tmp_dir) dir = tmp_dir;
2075 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2076 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2078 TRACE("got name (%s)\n",tmpbuf);
2079 DeleteFileA(tmpbuf);
2080 return _strdup(tmpbuf);
2082 TRACE("failed (%d)\n",GetLastError());
2083 return NULL;
2086 /*********************************************************************
2087 * _wtempnam (MSVCRT.@)
2089 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2091 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2093 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2094 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2096 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2097 DeleteFileW(tmpbuf);
2098 return _wcsdup(tmpbuf);
2100 TRACE("failed (%d)\n",GetLastError());
2101 return NULL;
2104 /*********************************************************************
2105 * _umask (MSVCRT.@)
2107 int CDECL MSVCRT__umask(int umask)
2109 int old_umask = MSVCRT_umask;
2110 TRACE("(%d)\n",umask);
2111 MSVCRT_umask = umask;
2112 return old_umask;
2115 /*********************************************************************
2116 * _utime64 (MSVCRT.@)
2118 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2120 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2122 if (fd > 0)
2124 int retVal = _futime64(fd, t);
2125 MSVCRT__close(fd);
2126 return retVal;
2128 return -1;
2131 /*********************************************************************
2132 * _utime32 (MSVCRT.@)
2134 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2136 struct MSVCRT___utimbuf64 t64;
2137 t64.actime = t->actime;
2138 t64.modtime = t->modtime;
2139 return _utime64( path, &t64 );
2142 /*********************************************************************
2143 * _utime (MSVCRT.@)
2145 #ifdef _WIN64
2146 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2148 return _utime64( path, t );
2150 #else
2151 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2153 return _utime32( path, t );
2155 #endif
2157 /*********************************************************************
2158 * _wutime64 (MSVCRT.@)
2160 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2162 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2164 if (fd > 0)
2166 int retVal = _futime64(fd, t);
2167 MSVCRT__close(fd);
2168 return retVal;
2170 return -1;
2173 /*********************************************************************
2174 * _wutime32 (MSVCRT.@)
2176 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2178 struct MSVCRT___utimbuf64 t64;
2179 t64.actime = t->actime;
2180 t64.modtime = t->modtime;
2181 return _wutime64( path, &t64 );
2184 /*********************************************************************
2185 * _wutime (MSVCRT.@)
2187 #ifdef _WIN64
2188 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2190 return _wutime64( path, t );
2192 #else
2193 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2195 return _wutime32( path, t );
2197 #endif
2199 /*********************************************************************
2200 * _write (MSVCRT.@)
2202 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2204 DWORD num_written;
2205 HANDLE hand = msvcrt_fdtoh(fd);
2207 /* Don't trace small writes, it gets *very* annoying */
2208 #if 0
2209 if (count > 32)
2210 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2211 #endif
2212 if (hand == INVALID_HANDLE_VALUE)
2214 *MSVCRT__errno() = MSVCRT_EBADF;
2215 return -1;
2218 /* If appending, go to EOF */
2219 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2220 MSVCRT__lseek(fd, 0, FILE_END);
2222 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2224 if (WriteFile(hand, buf, count, &num_written, NULL)
2225 && (num_written == count))
2226 return num_written;
2227 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2228 hand, GetLastError());
2229 *MSVCRT__errno() = MSVCRT_ENOSPC;
2231 else
2233 unsigned int i, j, nr_lf;
2234 char *p = NULL;
2235 const char *q;
2236 const char *s = buf, *buf_start = buf;
2237 /* find number of \n ( without preceding \r ) */
2238 for ( nr_lf=0,i = 0; i <count; i++)
2240 if (s[i]== '\n')
2242 nr_lf++;
2243 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2246 if (nr_lf)
2248 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2250 for (s = buf, i = 0, j = 0; i < count; i++)
2252 if (s[i]== '\n')
2254 p[j++] = '\r';
2255 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2257 p[j++] = s[i];
2260 else
2262 FIXME("Malloc failed\n");
2263 nr_lf =0;
2264 q = buf;
2267 else
2268 q = buf;
2270 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2272 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2273 fd, hand, GetLastError(), num_written);
2274 *MSVCRT__errno() = MSVCRT_ENOSPC;
2275 if(nr_lf)
2276 MSVCRT_free(p);
2277 return s - buf_start;
2279 else
2281 if(nr_lf)
2282 MSVCRT_free(p);
2283 return count;
2286 return -1;
2289 /*********************************************************************
2290 * _putw (MSVCRT.@)
2292 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2294 int len;
2295 len = MSVCRT__write(file->_file, &val, sizeof(val));
2296 if (len == sizeof(val)) return val;
2297 file->_flag |= MSVCRT__IOERR;
2298 return MSVCRT_EOF;
2301 /*********************************************************************
2302 * fclose (MSVCRT.@)
2304 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2306 int r, flag;
2308 flag = file->_flag;
2309 MSVCRT_free(file->_tmpfname);
2310 file->_tmpfname = NULL;
2311 /* flush stdio buffers */
2312 if(file->_flag & MSVCRT__IOWRT)
2313 MSVCRT_fflush(file);
2314 if(file->_flag & MSVCRT__IOMYBUF)
2315 MSVCRT_free(file->_base);
2317 r=MSVCRT__close(file->_file);
2319 file->_flag = 0;
2321 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2324 /*********************************************************************
2325 * feof (MSVCRT.@)
2327 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2329 return file->_flag & MSVCRT__IOEOF;
2332 /*********************************************************************
2333 * ferror (MSVCRT.@)
2335 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2337 return file->_flag & MSVCRT__IOERR;
2340 /*********************************************************************
2341 * _filbuf (MSVCRT.@)
2343 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2345 /* Allocate buffer if needed */
2346 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2347 msvcrt_alloc_buffer(file);
2349 if(!(file->_flag & MSVCRT__IOREAD)) {
2350 if(file->_flag & MSVCRT__IORW) {
2351 file->_flag |= MSVCRT__IOREAD;
2352 } else {
2353 return MSVCRT_EOF;
2356 if(file->_flag & MSVCRT__IONBF) {
2357 unsigned char c;
2358 int r;
2359 if ((r = read_i(file->_file,&c,1)) != 1) {
2360 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2361 return MSVCRT_EOF;
2363 return c;
2364 } else {
2365 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2366 if(file->_cnt<=0) {
2367 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2368 file->_cnt = 0;
2369 return MSVCRT_EOF;
2371 file->_cnt--;
2372 file->_ptr = file->_base+1;
2373 return *(unsigned char *)file->_base;
2377 /*********************************************************************
2378 * fgetc (MSVCRT.@)
2380 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2382 unsigned char *i;
2383 unsigned int j;
2384 if (file->_cnt>0) {
2385 file->_cnt--;
2386 i = (unsigned char *)file->_ptr++;
2387 j = *i;
2388 } else
2389 j = MSVCRT__filbuf(file);
2390 return j;
2393 /*********************************************************************
2394 * _fgetchar (MSVCRT.@)
2396 int CDECL _fgetchar(void)
2398 return MSVCRT_fgetc(MSVCRT_stdin);
2401 /*********************************************************************
2402 * fgets (MSVCRT.@)
2404 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2406 int cc = MSVCRT_EOF;
2407 char * buf_start = s;
2409 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2410 file,file->_file,s,size);
2412 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2414 *s++ = (char)cc;
2415 size --;
2417 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2419 TRACE(":nothing read\n");
2420 return NULL;
2422 if ((cc != MSVCRT_EOF) && (size > 1))
2423 *s++ = cc;
2424 *s = '\0';
2425 TRACE(":got %s\n", debugstr_a(buf_start));
2426 return buf_start;
2429 /*********************************************************************
2430 * fgetwc (MSVCRT.@)
2432 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2433 * the CR from CR/LF combinations
2435 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2437 char c;
2439 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2441 MSVCRT_wchar_t wc;
2442 unsigned int i;
2443 int j;
2444 char *chp, *wcp;
2445 wcp = (char *)&wc;
2446 for(i=0; i<sizeof(wc); i++)
2448 if (file->_cnt>0)
2450 file->_cnt--;
2451 chp = file->_ptr++;
2452 wcp[i] = *chp;
2454 else
2456 j = MSVCRT__filbuf(file);
2457 if(file->_cnt<=0)
2459 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2460 file->_cnt = 0;
2461 return MSVCRT_WEOF;
2463 wcp[i] = j;
2466 return wc;
2469 c = MSVCRT_fgetc(file);
2470 if ((MSVCRT___mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2472 FIXME("Treat Multibyte characters\n");
2474 if (c == MSVCRT_EOF)
2475 return MSVCRT_WEOF;
2476 else
2477 return (MSVCRT_wint_t)c;
2480 /*********************************************************************
2481 * _getw (MSVCRT.@)
2483 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2485 char *ch;
2486 int i, k;
2487 unsigned int j;
2488 ch = (char *)&i;
2489 for (j=0; j<sizeof(int); j++) {
2490 k = MSVCRT_fgetc(file);
2491 if (k == MSVCRT_EOF) {
2492 file->_flag |= MSVCRT__IOEOF;
2493 return EOF;
2495 ch[j] = k;
2497 return i;
2500 /*********************************************************************
2501 * getwc (MSVCRT.@)
2503 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2505 return MSVCRT_fgetwc(file);
2508 /*********************************************************************
2509 * _fgetwchar (MSVCRT.@)
2511 MSVCRT_wint_t CDECL _fgetwchar(void)
2513 return MSVCRT_fgetwc(MSVCRT_stdin);
2516 /*********************************************************************
2517 * getwchar (MSVCRT.@)
2519 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2521 return _fgetwchar();
2524 /*********************************************************************
2525 * fgetws (MSVCRT.@)
2527 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2529 int cc = MSVCRT_WEOF;
2530 MSVCRT_wchar_t * buf_start = s;
2532 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2533 file,file->_file,s,size);
2535 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2537 *s++ = (char)cc;
2538 size --;
2540 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2542 TRACE(":nothing read\n");
2543 return NULL;
2545 if ((cc != MSVCRT_WEOF) && (size > 1))
2546 *s++ = cc;
2547 *s = 0;
2548 TRACE(":got %s\n", debugstr_w(buf_start));
2549 return buf_start;
2552 /*********************************************************************
2553 * fwrite (MSVCRT.@)
2555 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2557 MSVCRT_size_t wrcnt=size * nmemb;
2558 int written = 0;
2559 if (size == 0)
2560 return 0;
2561 if(file->_cnt) {
2562 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2563 memcpy(file->_ptr, ptr, pcnt);
2564 file->_cnt -= pcnt;
2565 file->_ptr += pcnt;
2566 written = pcnt;
2567 wrcnt -= pcnt;
2568 ptr = (const char*)ptr + pcnt;
2569 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2570 if(file->_flag & MSVCRT__IORW) {
2571 file->_flag |= MSVCRT__IOWRT;
2572 } else
2573 return 0;
2575 if(wrcnt) {
2576 /* Flush buffer */
2577 int res=msvcrt_flush_buffer(file);
2578 if(!res) {
2579 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2580 if (pwritten <= 0)
2582 file->_flag |= MSVCRT__IOERR;
2583 pwritten=0;
2585 written += pwritten;
2588 return written / size;
2591 /*********************************************************************
2592 * fputwc (MSVCRT.@)
2594 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2596 MSVCRT_wchar_t mwc=wc;
2597 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2598 return MSVCRT_WEOF;
2599 return wc;
2602 /*********************************************************************
2603 * _fputwchar (MSVCRT.@)
2605 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2607 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2610 /*********************************************************************
2611 * _wfsopen (MSVCRT.@)
2613 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2615 MSVCRT_FILE* file;
2616 int open_flags, stream_flags, fd;
2618 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2620 /* map mode string to open() flags. "man fopen" for possibilities. */
2621 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2622 return NULL;
2624 LOCK_FILES();
2625 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2626 if (fd < 0)
2627 file = NULL;
2628 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2629 != -1)
2630 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2631 else if (file)
2633 file->_flag = 0;
2634 file = NULL;
2637 TRACE(":got (%p)\n",file);
2638 if (fd >= 0 && !file)
2639 MSVCRT__close(fd);
2640 UNLOCK_FILES();
2641 return file;
2644 /*********************************************************************
2645 * _fsopen (MSVCRT.@)
2647 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2649 MSVCRT_FILE *ret;
2650 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2652 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2653 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2655 MSVCRT_free(pathW);
2656 return NULL;
2659 ret = MSVCRT__wfsopen(pathW, modeW, share);
2661 MSVCRT_free(pathW);
2662 MSVCRT_free(modeW);
2663 return ret;
2666 /*********************************************************************
2667 * fopen (MSVCRT.@)
2669 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2671 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2674 /*********************************************************************
2675 * _wfopen (MSVCRT.@)
2677 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2679 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2682 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2683 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2685 /*********************************************************************
2686 * fputc (MSVCRT.@)
2688 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2690 if(file->_cnt>0) {
2691 *file->_ptr++=c;
2692 file->_cnt--;
2693 if (c == '\n')
2695 int res = msvcrt_flush_buffer(file);
2696 return res ? res : c;
2698 else
2699 return c & 0xff;
2700 } else {
2701 return MSVCRT__flsbuf(c, file);
2705 /*********************************************************************
2706 * _flsbuf (MSVCRT.@)
2708 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2710 /* Flush output buffer */
2711 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2712 msvcrt_alloc_buffer(file);
2714 if(!(file->_flag & MSVCRT__IOWRT)) {
2715 if(file->_flag & MSVCRT__IORW) {
2716 file->_flag |= MSVCRT__IOWRT;
2717 } else {
2718 return MSVCRT_EOF;
2721 if(file->_bufsiz) {
2722 int res=msvcrt_flush_buffer(file);
2723 return res?res : MSVCRT_fputc(c, file);
2724 } else {
2725 unsigned char cc=c;
2726 int len;
2727 /* set _cnt to 0 for unbuffered FILEs */
2728 file->_cnt = 0;
2729 len = MSVCRT__write(file->_file, &cc, 1);
2730 if (len == 1) return c & 0xff;
2731 file->_flag |= MSVCRT__IOERR;
2732 return MSVCRT_EOF;
2736 /*********************************************************************
2737 * _fputchar (MSVCRT.@)
2739 int CDECL _fputchar(int c)
2741 return MSVCRT_fputc(c, MSVCRT_stdout);
2744 /*********************************************************************
2745 * fread (MSVCRT.@)
2747 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2748 { MSVCRT_size_t rcnt=size * nmemb;
2749 MSVCRT_size_t read=0;
2750 int pread=0;
2752 if(!rcnt)
2753 return 0;
2755 /* first buffered data */
2756 if(file->_cnt>0) {
2757 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2758 memcpy(ptr, file->_ptr, pcnt);
2759 file->_cnt -= pcnt;
2760 file->_ptr += pcnt;
2761 read += pcnt ;
2762 rcnt -= pcnt ;
2763 ptr = (char*)ptr + pcnt;
2764 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2765 if(file->_flag & MSVCRT__IORW) {
2766 file->_flag |= MSVCRT__IOREAD;
2767 } else
2768 return 0;
2770 while(rcnt>0)
2772 int i;
2773 /* Fill the buffer on small reads.
2774 * TODO: Use a better buffering strategy.
2776 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2777 if (file->_bufsiz == 0) {
2778 msvcrt_alloc_buffer(file);
2780 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2781 file->_ptr = file->_base;
2782 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2783 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2784 if (i > 0 && i < file->_cnt) {
2785 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2786 file->_flag &= ~MSVCRT__IOEOF;
2788 if (i > 0) {
2789 memcpy(ptr, file->_ptr, i);
2790 file->_cnt -= i;
2791 file->_ptr += i;
2793 } else {
2794 i = MSVCRT__read(file->_file,ptr, rcnt);
2796 pread += i;
2797 rcnt -= i;
2798 ptr = (char *)ptr+i;
2799 /* expose feof condition in the flags
2800 * MFC tests file->_flag for feof, and doesn't call feof())
2802 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2803 file->_flag |= MSVCRT__IOEOF;
2804 else if (i == -1)
2806 file->_flag |= MSVCRT__IOERR;
2807 pread = 0;
2808 rcnt = 0;
2810 if (i < 1) break;
2812 read+=pread;
2813 return read / size;
2816 /*********************************************************************
2817 * _wfreopen (MSVCRT.@)
2820 MSVCRT_FILE* CDECL _wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
2822 int open_flags, stream_flags, fd;
2824 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2826 LOCK_FILES();
2827 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2828 file = NULL;
2829 else
2831 MSVCRT_fclose(file);
2832 /* map mode string to open() flags. "man fopen" for possibilities. */
2833 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2834 file = NULL;
2835 else
2837 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2838 if (fd < 0)
2839 file = NULL;
2840 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2842 file->_flag = 0;
2843 WARN(":failed-last error (%d)\n",GetLastError());
2844 msvcrt_set_errno(GetLastError());
2845 file = NULL;
2849 UNLOCK_FILES();
2850 return file;
2853 /*********************************************************************
2854 * freopen (MSVCRT.@)
2857 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
2859 MSVCRT_FILE *ret;
2860 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2862 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2863 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2865 MSVCRT_free(pathW);
2866 return NULL;
2869 ret = _wfreopen(pathW, modeW, file);
2871 MSVCRT_free(pathW);
2872 MSVCRT_free(modeW);
2873 return ret;
2876 /*********************************************************************
2877 * fsetpos (MSVCRT.@)
2879 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2881 /* Note that all this has been lifted 'as is' from fseek */
2882 if(file->_flag & MSVCRT__IOWRT)
2883 msvcrt_flush_buffer(file);
2885 /* Discard buffered input */
2886 file->_cnt = 0;
2887 file->_ptr = file->_base;
2889 /* Reset direction of i/o */
2890 if(file->_flag & MSVCRT__IORW) {
2891 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2894 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2897 /*********************************************************************
2898 * ftell (MSVCRT.@)
2900 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2902 /* TODO: just call fgetpos and return lower half of result */
2903 int off=0;
2904 MSVCRT_long pos;
2905 pos = _tell(file->_file);
2906 if(pos == -1) return -1;
2907 if(file->_bufsiz) {
2908 if( file->_flag & MSVCRT__IOWRT ) {
2909 off = file->_ptr - file->_base;
2910 } else {
2911 off = -file->_cnt;
2912 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2913 /* Black magic correction for CR removal */
2914 int i;
2915 for (i=0; i<file->_cnt; i++) {
2916 if (file->_ptr[i] == '\n')
2917 off--;
2919 /* Black magic when reading CR at buffer boundary*/
2920 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
2921 off--;
2926 return off + pos;
2929 /*********************************************************************
2930 * fgetpos (MSVCRT.@)
2932 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2934 int off=0;
2935 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
2936 if(*pos == -1) return -1;
2937 if(file->_bufsiz) {
2938 if( file->_flag & MSVCRT__IOWRT ) {
2939 off = file->_ptr - file->_base;
2940 } else {
2941 off = -file->_cnt;
2942 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2943 /* Black magic correction for CR removal */
2944 int i;
2945 for (i=0; i<file->_cnt; i++) {
2946 if (file->_ptr[i] == '\n')
2947 off--;
2949 /* Black magic when reading CR at buffer boundary*/
2950 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
2951 off--;
2955 *pos += off;
2956 return 0;
2959 /*********************************************************************
2960 * fputs (MSVCRT.@)
2962 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2964 MSVCRT_size_t i, len = strlen(s);
2965 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2966 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2967 for (i=0; i<len; i++)
2968 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2969 return MSVCRT_EOF;
2970 return 0;
2973 /*********************************************************************
2974 * fputws (MSVCRT.@)
2976 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2978 MSVCRT_size_t i, len = strlenW(s);
2979 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2980 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2981 for (i=0; i<len; i++)
2983 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2984 return MSVCRT_WEOF;
2985 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2986 return MSVCRT_WEOF;
2988 return 0;
2991 /*********************************************************************
2992 * getchar (MSVCRT.@)
2994 int CDECL MSVCRT_getchar(void)
2996 return MSVCRT_fgetc(MSVCRT_stdin);
2999 /*********************************************************************
3000 * getc (MSVCRT.@)
3002 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3004 return MSVCRT_fgetc(file);
3007 /*********************************************************************
3008 * gets (MSVCRT.@)
3010 char * CDECL MSVCRT_gets(char *buf)
3012 int cc;
3013 char * buf_start = buf;
3015 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3016 cc = MSVCRT_fgetc(MSVCRT_stdin))
3017 if(cc != '\r') *buf++ = (char)cc;
3019 *buf = '\0';
3021 TRACE("got '%s'\n", buf_start);
3022 return buf_start;
3025 /*********************************************************************
3026 * _getws (MSVCRT.@)
3028 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3030 MSVCRT_wint_t cc;
3031 MSVCRT_wchar_t* ws = buf;
3033 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3034 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3036 if (cc != '\r')
3037 *buf++ = (MSVCRT_wchar_t)cc;
3039 *buf = '\0';
3041 TRACE("got %s\n", debugstr_w(ws));
3042 return ws;
3045 /*********************************************************************
3046 * putc (MSVCRT.@)
3048 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3050 return MSVCRT_fputc(c, file);
3053 /*********************************************************************
3054 * putchar (MSVCRT.@)
3056 int CDECL MSVCRT_putchar(int c)
3058 return MSVCRT_fputc(c, MSVCRT_stdout);
3061 /*********************************************************************
3062 * puts (MSVCRT.@)
3064 int CDECL MSVCRT_puts(const char *s)
3066 MSVCRT_size_t len = strlen(s);
3067 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3068 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3071 /*********************************************************************
3072 * _putws (MSVCRT.@)
3074 int CDECL _putws(const MSVCRT_wchar_t *s)
3076 static const MSVCRT_wchar_t nl = '\n';
3077 MSVCRT_size_t len = strlenW(s);
3078 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3079 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3082 /*********************************************************************
3083 * remove (MSVCRT.@)
3085 int CDECL MSVCRT_remove(const char *path)
3087 TRACE("(%s)\n",path);
3088 if (DeleteFileA(path))
3089 return 0;
3090 TRACE(":failed (%d)\n",GetLastError());
3091 msvcrt_set_errno(GetLastError());
3092 return -1;
3095 /*********************************************************************
3096 * _wremove (MSVCRT.@)
3098 int CDECL _wremove(const MSVCRT_wchar_t *path)
3100 TRACE("(%s)\n",debugstr_w(path));
3101 if (DeleteFileW(path))
3102 return 0;
3103 TRACE(":failed (%d)\n",GetLastError());
3104 msvcrt_set_errno(GetLastError());
3105 return -1;
3108 /*********************************************************************
3109 * rename (MSVCRT.@)
3111 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3113 TRACE(":from %s to %s\n",oldpath,newpath);
3114 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3115 return 0;
3116 TRACE(":failed (%d)\n",GetLastError());
3117 msvcrt_set_errno(GetLastError());
3118 return -1;
3121 /*********************************************************************
3122 * _wrename (MSVCRT.@)
3124 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3126 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3127 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3128 return 0;
3129 TRACE(":failed (%d)\n",GetLastError());
3130 msvcrt_set_errno(GetLastError());
3131 return -1;
3134 /*********************************************************************
3135 * setvbuf (MSVCRT.@)
3137 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3139 /* TODO: Check if file busy */
3140 if(file->_bufsiz) {
3141 MSVCRT_free(file->_base);
3142 file->_bufsiz = 0;
3143 file->_cnt = 0;
3145 if(mode == MSVCRT__IOFBF) {
3146 file->_flag &= ~MSVCRT__IONBF;
3147 file->_base = file->_ptr = buf;
3148 if(buf) {
3149 file->_bufsiz = size;
3151 } else {
3152 file->_flag |= MSVCRT__IONBF;
3154 return 0;
3157 /*********************************************************************
3158 * setbuf (MSVCRT.@)
3160 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3162 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3165 /*********************************************************************
3166 * tmpnam (MSVCRT.@)
3168 char * CDECL MSVCRT_tmpnam(char *s)
3170 static int unique;
3171 char tmpstr[16];
3172 char *p;
3173 int count;
3174 if (s == 0)
3175 s = MSVCRT_tmpname;
3176 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3177 p = s + sprintf(s, "\\s%s.", tmpstr);
3178 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3180 msvcrt_int_to_base32(unique++, tmpstr);
3181 strcpy(p, tmpstr);
3182 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3183 GetLastError() == ERROR_FILE_NOT_FOUND)
3184 break;
3186 return s;
3189 /*********************************************************************
3190 * tmpfile (MSVCRT.@)
3192 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3194 char *filename = MSVCRT_tmpnam(NULL);
3195 int fd;
3196 MSVCRT_FILE* file = NULL;
3198 LOCK_FILES();
3199 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3200 if (fd != -1 && (file = msvcrt_alloc_fp()))
3202 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3204 file->_flag = 0;
3205 file = NULL;
3207 else file->_tmpfname = _strdup(filename);
3209 UNLOCK_FILES();
3210 return file;
3213 /*********************************************************************
3214 * vfprintf (MSVCRT.@)
3216 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3218 char buf[2048], *mem = buf;
3219 int written, resize = sizeof(buf), retval;
3220 /* There are two conventions for vsnprintf failing:
3221 * Return -1 if we truncated, or
3222 * Return the number of bytes that would have been written
3223 * The code below handles both cases
3225 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3226 written > resize)
3228 resize = (written == -1 ? resize * 2 : written + 1);
3229 if (mem != buf)
3230 MSVCRT_free (mem);
3231 if (!(mem = MSVCRT_malloc(resize)))
3232 return MSVCRT_EOF;
3234 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3235 if (mem != buf)
3236 MSVCRT_free (mem);
3237 return retval;
3240 /*********************************************************************
3241 * vfwprintf (MSVCRT.@)
3242 * FIXME:
3243 * Is final char included in written (then resize is too big) or not
3244 * (then we must test for equality too)?
3246 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3248 MSVCRT_wchar_t buf[2048], *mem = buf;
3249 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3250 /* See vfprintf comments */
3251 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3252 written > resize)
3254 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3255 if (mem != buf)
3256 MSVCRT_free (mem);
3257 if (!(mem = MSVCRT_malloc(resize*sizeof(*mem))))
3258 return MSVCRT_EOF;
3260 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3261 if (mem != buf)
3262 MSVCRT_free (mem);
3263 return retval;
3266 /*********************************************************************
3267 * vprintf (MSVCRT.@)
3269 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3271 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3274 /*********************************************************************
3275 * vwprintf (MSVCRT.@)
3277 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3279 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3282 /*********************************************************************
3283 * fprintf (MSVCRT.@)
3285 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3287 __ms_va_list valist;
3288 int res;
3289 __ms_va_start(valist, format);
3290 res = MSVCRT_vfprintf(file, format, valist);
3291 __ms_va_end(valist);
3292 return res;
3295 /*********************************************************************
3296 * fwprintf (MSVCRT.@)
3298 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3300 __ms_va_list valist;
3301 int res;
3302 __ms_va_start(valist, format);
3303 res = MSVCRT_vfwprintf(file, format, valist);
3304 __ms_va_end(valist);
3305 return res;
3308 /*********************************************************************
3309 * printf (MSVCRT.@)
3311 int CDECL MSVCRT_printf(const char *format, ...)
3313 __ms_va_list valist;
3314 int res;
3315 __ms_va_start(valist, format);
3316 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3317 __ms_va_end(valist);
3318 return res;
3321 /*********************************************************************
3322 * ungetc (MSVCRT.@)
3324 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3326 if (c == MSVCRT_EOF)
3327 return MSVCRT_EOF;
3328 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3329 msvcrt_alloc_buffer(file);
3330 file->_ptr++;
3332 if(file->_ptr>file->_base) {
3333 file->_ptr--;
3334 *file->_ptr=c;
3335 file->_cnt++;
3336 MSVCRT_clearerr(file);
3337 return c;
3339 return MSVCRT_EOF;
3342 /*********************************************************************
3343 * ungetwc (MSVCRT.@)
3345 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3347 MSVCRT_wchar_t mwc = wc;
3348 char * pp = (char *)&mwc;
3349 int i;
3350 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3351 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3352 return MSVCRT_WEOF;
3354 return mwc;
3357 /*********************************************************************
3358 * wprintf (MSVCRT.@)
3360 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3362 __ms_va_list valist;
3363 int res;
3364 __ms_va_start(valist, format);
3365 res = MSVCRT_vwprintf(format, valist);
3366 __ms_va_end(valist);
3367 return res;
3370 /*********************************************************************
3371 * _getmaxstdio (MSVCRT.@)
3373 int CDECL _getmaxstdio(void)
3375 FIXME("stub, always returns 512\n");
3376 return 512;
3379 /*********************************************************************
3380 * _setmaxstdio_ (MSVCRT.@)
3382 int CDECL _setmaxstdio(int newmax)
3384 int res;
3385 if( newmax > 2048)
3386 res = -1;
3387 else
3388 res = newmax;
3389 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3390 return res;
3393 /*********************************************************************
3394 * __pioinfo (MSVCRT.@)
3395 * FIXME: see MSVCRT_MAX_FILES define.
3397 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3398 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3399 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3400 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3401 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3402 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3403 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3404 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3405 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3406 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3407 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3408 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3411 /*********************************************************************
3412 * __badioinfo (MSVCRT.@)
3414 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };