kernel32: Update Italian translation.
[wine/testsucceed.git] / dlls / msvcrt / file.c
blobf9064013b3d526106165a9fee12524425ffdec40
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 *MSVCRT__errno() = MSVCRT_ENFILE;
222 return -1;
224 MSVCRT_fdesc[fd].handle = hand;
225 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
227 /* locate next free slot */
228 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
229 MSVCRT_fdstart = MSVCRT_fdend + 1;
230 else
231 while (MSVCRT_fdstart < MSVCRT_fdend &&
232 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
233 MSVCRT_fdstart++;
234 /* update last fd in use */
235 if (fd >= MSVCRT_fdend)
236 MSVCRT_fdend = fd + 1;
237 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
239 switch (fd)
241 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
242 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
243 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
246 return fd;
249 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
250 static int msvcrt_alloc_fd(HANDLE hand, int flag)
252 int ret;
254 LOCK_FILES();
255 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
256 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
257 UNLOCK_FILES();
258 return ret;
261 /* INTERNAL: Allocate a FILE* for an fd slot */
262 /* caller must hold the files lock */
263 static MSVCRT_FILE* msvcrt_alloc_fp(void)
265 unsigned int i;
267 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
269 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
271 if (!MSVCRT_fstreams[i])
273 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
274 return NULL;
275 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
277 return MSVCRT_fstreams[i];
280 return NULL;
283 /* INTERNAL: initialize a FILE* from an open fd */
284 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
286 TRACE(":fd (%d) allocating FILE*\n",fd);
287 if (!msvcrt_is_valid_fd(fd))
289 WARN(":invalid fd %d\n",fd);
290 *MSVCRT___doserrno() = 0;
291 *MSVCRT__errno() = MSVCRT_EBADF;
292 return -1;
294 memset(file, 0, sizeof(*file));
295 file->_file = fd;
296 file->_flag = stream_flags;
298 TRACE(":got FILE* (%p)\n",file);
299 return 0;
302 /* INTERNAL: Create an inheritance data block (for spawned process)
303 * The inheritance block is made of:
304 * 00 int nb of file descriptor (NBFD)
305 * 04 char file flags (wxflag): repeated for each fd
306 * 4+NBFD HANDLE file handle: repeated for each fd
308 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
310 int fd;
311 char* wxflag_ptr;
312 HANDLE* handle_ptr;
314 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
315 *block = MSVCRT_calloc(*size, 1);
316 if (!*block)
318 *size = 0;
319 return FALSE;
321 wxflag_ptr = (char*)*block + sizeof(unsigned);
322 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
324 *(unsigned*)*block = MSVCRT_fdend;
325 for (fd = 0; fd < MSVCRT_fdend; fd++)
327 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
328 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
330 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
331 *handle_ptr = MSVCRT_fdesc[fd].handle;
333 else
335 *wxflag_ptr = 0;
336 *handle_ptr = INVALID_HANDLE_VALUE;
338 wxflag_ptr++; handle_ptr++;
340 return TRUE;
343 /* INTERNAL: Set up all file descriptors,
344 * as well as default streams (stdin, stderr and stdout)
346 void msvcrt_init_io(void)
348 STARTUPINFOA si;
349 int i;
351 InitializeCriticalSection(&MSVCRT_file_cs);
352 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
353 GetStartupInfoA(&si);
354 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
356 BYTE* wxflag_ptr;
357 HANDLE* handle_ptr;
358 unsigned int count;
360 count = *(unsigned*)si.lpReserved2;
361 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
362 handle_ptr = (HANDLE*)(wxflag_ptr + count);
364 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
365 count = min(count, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
366 for (i = 0; i < count; i++)
368 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
370 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
371 MSVCRT_fdesc[i].handle = *handle_ptr;
373 else
375 MSVCRT_fdesc[i].wxflag = 0;
376 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
378 wxflag_ptr++; handle_ptr++;
380 MSVCRT_fdend = max( 3, count );
381 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
382 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
385 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
387 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
388 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
389 GetCurrentProcess(), &MSVCRT_fdesc[0].handle,
390 0, TRUE, DUPLICATE_SAME_ACCESS))
391 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
393 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
395 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
396 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
397 GetCurrentProcess(), &MSVCRT_fdesc[1].handle,
398 0, TRUE, DUPLICATE_SAME_ACCESS))
399 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
401 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
403 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
404 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
405 GetCurrentProcess(), &MSVCRT_fdesc[2].handle,
406 0, TRUE, DUPLICATE_SAME_ACCESS))
407 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
410 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
411 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
413 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
414 for (i = 0; i < 3; i++)
416 /* FILE structs for stdin/out/err are static and never deleted */
417 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
418 MSVCRT__iob[i]._file = i;
419 MSVCRT__iob[i]._tmpfname = NULL;
420 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
422 MSVCRT_stream_idx = 3;
425 /* INTERNAL: Flush stdio file buffer */
426 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
428 if(file->_bufsiz) {
429 int cnt=file->_ptr-file->_base;
430 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
431 file->_flag |= MSVCRT__IOERR;
432 return MSVCRT_EOF;
434 file->_ptr=file->_base;
435 file->_cnt=file->_bufsiz;
437 return 0;
440 /* INTERNAL: Allocate stdio file buffer */
441 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
443 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
444 if(file->_base) {
445 file->_bufsiz = MSVCRT_BUFSIZ;
446 file->_flag |= MSVCRT__IOMYBUF;
447 } else {
448 file->_base = (char*)(&file->_charbuf);
449 /* put here 2 ??? */
450 file->_bufsiz = sizeof(file->_charbuf);
452 file->_ptr = file->_base;
453 file->_cnt = 0;
456 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
457 static void msvcrt_int_to_base32(int num, char *str)
459 char *p;
460 int n = num;
461 int digits = 0;
463 while (n != 0)
465 n >>= 5;
466 digits++;
468 p = str + digits;
469 *p = 0;
470 while (--p >= str)
472 *p = (num & 31) + '0';
473 if (*p > '9')
474 *p += ('a' - '0' - 10);
475 num >>= 5;
479 /*********************************************************************
480 * __iob_func(MSVCRT.@)
482 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
484 return &MSVCRT__iob[0];
487 /*********************************************************************
488 * _access (MSVCRT.@)
490 int CDECL MSVCRT__access(const char *filename, int mode)
492 DWORD attr = GetFileAttributesA(filename);
494 TRACE("(%s,%d) %d\n",filename,mode,attr);
496 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
498 msvcrt_set_errno(GetLastError());
499 return -1;
501 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
503 msvcrt_set_errno(ERROR_ACCESS_DENIED);
504 return -1;
506 return 0;
509 /*********************************************************************
510 * _waccess (MSVCRT.@)
512 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
514 DWORD attr = GetFileAttributesW(filename);
516 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
518 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
520 msvcrt_set_errno(GetLastError());
521 return -1;
523 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
525 msvcrt_set_errno(ERROR_ACCESS_DENIED);
526 return -1;
528 return 0;
531 /*********************************************************************
532 * _chmod (MSVCRT.@)
534 int CDECL MSVCRT__chmod(const char *path, int flags)
536 DWORD oldFlags = GetFileAttributesA(path);
538 if (oldFlags != INVALID_FILE_ATTRIBUTES)
540 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
541 oldFlags | FILE_ATTRIBUTE_READONLY;
543 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
544 return 0;
546 msvcrt_set_errno(GetLastError());
547 return -1;
550 /*********************************************************************
551 * _wchmod (MSVCRT.@)
553 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
555 DWORD oldFlags = GetFileAttributesW(path);
557 if (oldFlags != INVALID_FILE_ATTRIBUTES)
559 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
560 oldFlags | FILE_ATTRIBUTE_READONLY;
562 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
563 return 0;
565 msvcrt_set_errno(GetLastError());
566 return -1;
569 /*********************************************************************
570 * _unlink (MSVCRT.@)
572 int CDECL MSVCRT__unlink(const char *path)
574 TRACE("%s\n",debugstr_a(path));
575 if(DeleteFileA(path))
576 return 0;
577 TRACE("failed (%d)\n",GetLastError());
578 msvcrt_set_errno(GetLastError());
579 return -1;
582 /*********************************************************************
583 * _wunlink (MSVCRT.@)
585 int CDECL _wunlink(const MSVCRT_wchar_t *path)
587 TRACE("(%s)\n",debugstr_w(path));
588 if(DeleteFileW(path))
589 return 0;
590 TRACE("failed (%d)\n",GetLastError());
591 msvcrt_set_errno(GetLastError());
592 return -1;
595 /* _flushall calls MSVCRT_fflush which calls _flushall */
596 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
598 /*********************************************************************
599 * _flushall (MSVCRT.@)
601 int CDECL _flushall(void)
603 int i, num_flushed = 0;
605 LOCK_FILES();
606 for (i = 3; i < MSVCRT_stream_idx; i++)
607 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
609 #if 0
610 /* FIXME: flush, do not commit */
611 if (_commit(i) == -1)
612 if (MSVCRT_fstreams[i])
613 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
614 #endif
615 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
616 MSVCRT_fflush(MSVCRT_fstreams[i]);
617 num_flushed++;
620 UNLOCK_FILES();
622 TRACE(":flushed (%d) handles\n",num_flushed);
623 return num_flushed;
626 /*********************************************************************
627 * fflush (MSVCRT.@)
629 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
631 if(!file) {
632 _flushall();
633 } else if(file->_flag & MSVCRT__IOWRT) {
634 int res=msvcrt_flush_buffer(file);
635 return res;
637 return 0;
640 /*********************************************************************
641 * _close (MSVCRT.@)
643 int CDECL MSVCRT__close(int fd)
645 HANDLE hand;
646 int ret;
648 LOCK_FILES();
649 hand = msvcrt_fdtoh(fd);
650 TRACE(":fd (%d) handle (%p)\n",fd,hand);
651 if (hand == INVALID_HANDLE_VALUE)
652 ret = -1;
653 else if (!CloseHandle(hand))
655 WARN(":failed-last error (%d)\n",GetLastError());
656 msvcrt_set_errno(GetLastError());
657 ret = -1;
659 else
661 msvcrt_free_fd(fd);
662 ret = 0;
664 UNLOCK_FILES();
665 TRACE(":ok\n");
666 return ret;
669 /*********************************************************************
670 * _commit (MSVCRT.@)
672 int CDECL _commit(int fd)
674 HANDLE hand = msvcrt_fdtoh(fd);
676 TRACE(":fd (%d) handle (%p)\n",fd,hand);
677 if (hand == INVALID_HANDLE_VALUE)
678 return -1;
680 if (!FlushFileBuffers(hand))
682 if (GetLastError() == ERROR_INVALID_HANDLE)
684 /* FlushFileBuffers fails for console handles
685 * so we ignore this error.
687 return 0;
689 TRACE(":failed-last error (%d)\n",GetLastError());
690 msvcrt_set_errno(GetLastError());
691 return -1;
693 TRACE(":ok\n");
694 return 0;
697 /*********************************************************************
698 * _dup2 (MSVCRT.@)
699 * NOTES
700 * MSDN isn't clear on this point, but the remarks for _pipe
701 * indicate file descriptors duplicated with _dup and _dup2 are always
702 * inheritable.
704 int CDECL MSVCRT__dup2(int od, int nd)
706 int ret;
708 TRACE("(od=%d, nd=%d)\n", od, nd);
709 LOCK_FILES();
710 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
712 HANDLE handle;
714 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
715 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
717 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
719 if (msvcrt_is_valid_fd(nd))
720 MSVCRT__close(nd);
721 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
722 if (ret == -1)
724 CloseHandle(handle);
725 *MSVCRT__errno() = MSVCRT_EMFILE;
727 else
729 /* _dup2 returns 0, not nd, on success */
730 ret = 0;
733 else
735 ret = -1;
736 msvcrt_set_errno(GetLastError());
739 else
741 *MSVCRT__errno() = MSVCRT_EBADF;
742 ret = -1;
744 UNLOCK_FILES();
745 return ret;
748 /*********************************************************************
749 * _dup (MSVCRT.@)
751 int CDECL MSVCRT__dup(int od)
753 int fd, ret;
755 LOCK_FILES();
756 fd = MSVCRT_fdstart;
757 if (MSVCRT__dup2(od, fd) == 0)
758 ret = fd;
759 else
760 ret = -1;
761 UNLOCK_FILES();
762 return ret;
765 /*********************************************************************
766 * _eof (MSVCRT.@)
768 int CDECL _eof(int fd)
770 DWORD curpos,endpos;
771 LONG hcurpos,hendpos;
772 HANDLE hand = msvcrt_fdtoh(fd);
774 TRACE(":fd (%d) handle (%p)\n",fd,hand);
776 if (hand == INVALID_HANDLE_VALUE)
777 return -1;
779 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
781 /* Otherwise we do it the hard way */
782 hcurpos = hendpos = 0;
783 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
784 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
786 if (curpos == endpos && hcurpos == hendpos)
788 /* FIXME: shouldn't WX_ATEOF be set here? */
789 return TRUE;
792 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
793 return FALSE;
796 /*********************************************************************
797 * _fcloseall (MSVCRT.@)
799 int CDECL MSVCRT__fcloseall(void)
801 int num_closed = 0, i;
803 LOCK_FILES();
804 for (i = 3; i < MSVCRT_stream_idx; i++)
805 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
806 !MSVCRT_fclose(MSVCRT_fstreams[i]))
807 num_closed++;
808 UNLOCK_FILES();
810 TRACE(":closed (%d) handles\n",num_closed);
811 return num_closed;
814 /* free everything on process exit */
815 void msvcrt_free_io(void)
817 MSVCRT__fcloseall();
818 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
819 * stdout, and stderr (unlike GNU), so we need to fclose() them here
820 * or they won't get flushed.
822 MSVCRT_fclose(&MSVCRT__iob[0]);
823 MSVCRT_fclose(&MSVCRT__iob[1]);
824 MSVCRT_fclose(&MSVCRT__iob[2]);
825 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
826 DeleteCriticalSection(&MSVCRT_file_cs);
829 /*********************************************************************
830 * _lseeki64 (MSVCRT.@)
832 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
834 HANDLE hand = msvcrt_fdtoh(fd);
835 LARGE_INTEGER ofs, ret;
837 TRACE(":fd (%d) handle (%p)\n",fd,hand);
838 if (hand == INVALID_HANDLE_VALUE)
839 return -1;
841 if (whence < 0 || whence > 2)
843 *MSVCRT__errno() = MSVCRT_EINVAL;
844 return -1;
847 TRACE(":fd (%d) to %s pos %s\n",
848 fd,wine_dbgstr_longlong(offset),
849 (whence==SEEK_SET)?"SEEK_SET":
850 (whence==SEEK_CUR)?"SEEK_CUR":
851 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
853 ofs.QuadPart = offset;
854 if (SetFilePointerEx(hand, ofs, &ret, whence))
856 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
857 /* FIXME: What if we seek _to_ EOF - is EOF set? */
859 return ret.QuadPart;
861 TRACE(":error-last error (%d)\n",GetLastError());
862 msvcrt_set_errno(GetLastError());
863 return -1;
866 /*********************************************************************
867 * _lseek (MSVCRT.@)
869 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
871 return MSVCRT__lseeki64(fd, offset, whence);
874 /*********************************************************************
875 * _locking (MSVCRT.@)
877 * This is untested; the underlying LockFile doesn't work yet.
879 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
881 BOOL ret;
882 DWORD cur_locn;
883 HANDLE hand = msvcrt_fdtoh(fd);
885 TRACE(":fd (%d) handle (%p)\n",fd,hand);
886 if (hand == INVALID_HANDLE_VALUE)
887 return -1;
889 if (mode < 0 || mode > 4)
891 *MSVCRT__errno() = MSVCRT_EINVAL;
892 return -1;
895 TRACE(":fd (%d) by 0x%08x mode %s\n",
896 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
897 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
898 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
899 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
900 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
901 "UNKNOWN");
903 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
905 FIXME ("Seek failed\n");
906 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
907 return -1;
909 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
911 int nretry = 10;
912 ret = 1; /* just to satisfy gcc */
913 while (nretry--)
915 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
916 if (ret) break;
917 Sleep(1);
920 else if (mode == MSVCRT__LK_UNLCK)
921 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
922 else
923 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
924 /* FIXME - what about error settings? */
925 return ret ? 0 : -1;
928 /*********************************************************************
929 * fseek (MSVCRT.@)
931 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
933 /* Flush output if needed */
934 if(file->_flag & MSVCRT__IOWRT)
935 msvcrt_flush_buffer(file);
937 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
938 offset -= file->_cnt;
939 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
940 /* Black magic correction for CR removal */
941 int i;
942 for (i=0; i<file->_cnt; i++) {
943 if (file->_ptr[i] == '\n')
944 offset--;
946 /* Black magic when reading CR at buffer boundary*/
947 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
948 offset--;
951 /* Discard buffered input */
952 file->_cnt = 0;
953 file->_ptr = file->_base;
954 /* Reset direction of i/o */
955 if(file->_flag & MSVCRT__IORW) {
956 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
958 /* Clear end of file flag */
959 file->_flag &= ~MSVCRT__IOEOF;
960 return (MSVCRT__lseek(file->_file,offset,whence) == -1)?-1:0;
963 /*********************************************************************
964 * _chsize (MSVCRT.@)
966 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
968 LONG cur, pos;
969 HANDLE handle;
970 BOOL ret = FALSE;
972 TRACE("(fd=%d, size=%d)\n", fd, size);
974 LOCK_FILES();
976 handle = msvcrt_fdtoh(fd);
977 if (handle != INVALID_HANDLE_VALUE)
979 /* save the current file pointer */
980 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
981 if (cur >= 0)
983 pos = MSVCRT__lseek(fd, size, SEEK_SET);
984 if (pos >= 0)
986 ret = SetEndOfFile(handle);
987 if (!ret) msvcrt_set_errno(GetLastError());
990 /* restore the file pointer */
991 MSVCRT__lseek(fd, cur, SEEK_SET);
995 UNLOCK_FILES();
996 return ret ? 0 : -1;
999 /*********************************************************************
1000 * clearerr (MSVCRT.@)
1002 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1004 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1005 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1008 /*********************************************************************
1009 * rewind (MSVCRT.@)
1011 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1013 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1014 MSVCRT_fseek(file, 0L, SEEK_SET);
1015 MSVCRT_clearerr(file);
1018 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1020 int plus = strchrW(mode, '+') != NULL;
1022 switch(*mode++)
1024 case 'R': case 'r':
1025 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1026 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1027 break;
1028 case 'W': case 'w':
1029 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1030 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1031 break;
1032 case 'A': case 'a':
1033 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1034 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1035 break;
1036 default:
1037 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1038 *MSVCRT__errno() = MSVCRT_EINVAL;
1039 return -1;
1042 while (*mode)
1043 switch (*mode++)
1045 case 'B': case 'b':
1046 *open_flags |= MSVCRT__O_BINARY;
1047 *open_flags &= ~MSVCRT__O_TEXT;
1048 break;
1049 case 'T': case 't':
1050 *open_flags |= MSVCRT__O_TEXT;
1051 *open_flags &= ~MSVCRT__O_BINARY;
1052 break;
1053 case '+':
1054 case ' ':
1055 break;
1056 default:
1057 FIXME(":unknown flag %c not supported\n",mode[-1]);
1059 return 0;
1062 /*********************************************************************
1063 * _fdopen (MSVCRT.@)
1065 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1067 MSVCRT_FILE *ret;
1068 MSVCRT_wchar_t *modeW = NULL;
1070 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1072 ret = MSVCRT__wfdopen(fd, modeW);
1074 MSVCRT_free(modeW);
1075 return ret;
1078 /*********************************************************************
1079 * _wfdopen (MSVCRT.@)
1081 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1083 int open_flags, stream_flags;
1084 MSVCRT_FILE* file;
1086 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1088 LOCK_FILES();
1089 if (!(file = msvcrt_alloc_fp()))
1090 file = NULL;
1091 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1093 file->_flag = 0;
1094 file = NULL;
1096 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1097 UNLOCK_FILES();
1099 return file;
1102 /*********************************************************************
1103 * _filelength (MSVCRT.@)
1105 LONG CDECL MSVCRT__filelength(int fd)
1107 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1108 if (curPos != -1)
1110 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1111 if (endPos != -1)
1113 if (endPos != curPos)
1114 MSVCRT__lseek(fd, curPos, SEEK_SET);
1115 return endPos;
1118 return -1;
1121 /*********************************************************************
1122 * _filelengthi64 (MSVCRT.@)
1124 __int64 CDECL MSVCRT__filelengthi64(int fd)
1126 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1127 if (curPos != -1)
1129 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1130 if (endPos != -1)
1132 if (endPos != curPos)
1133 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1134 return endPos;
1137 return -1;
1140 /*********************************************************************
1141 * _fileno (MSVCRT.@)
1143 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1145 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1146 return file->_file;
1149 /*********************************************************************
1150 * _fstat64 (MSVCRT.@)
1152 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1154 DWORD dw;
1155 DWORD type;
1156 BY_HANDLE_FILE_INFORMATION hfi;
1157 HANDLE hand = msvcrt_fdtoh(fd);
1159 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1160 if (hand == INVALID_HANDLE_VALUE)
1161 return -1;
1163 if (!buf)
1165 WARN(":failed-NULL buf\n");
1166 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1167 return -1;
1170 memset(&hfi, 0, sizeof(hfi));
1171 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1172 type = GetFileType(hand);
1173 if (type == FILE_TYPE_PIPE)
1175 buf->st_dev = buf->st_rdev = fd;
1176 buf->st_mode = S_IFIFO;
1177 buf->st_nlink = 1;
1179 else if (type == FILE_TYPE_CHAR)
1181 buf->st_dev = buf->st_rdev = fd;
1182 buf->st_mode = S_IFCHR;
1183 buf->st_nlink = 1;
1185 else /* FILE_TYPE_DISK etc. */
1187 if (!GetFileInformationByHandle(hand, &hfi))
1189 WARN(":failed-last error (%d)\n",GetLastError());
1190 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1191 return -1;
1193 buf->st_mode = S_IFREG | 0444;
1194 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1195 buf->st_mode |= 0222;
1196 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1197 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1198 buf->st_atime = dw;
1199 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1200 buf->st_mtime = buf->st_ctime = dw;
1201 buf->st_nlink = hfi.nNumberOfLinks;
1203 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1204 buf->st_mode);
1205 return 0;
1208 /*********************************************************************
1209 * _fstati64 (MSVCRT.@)
1211 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1213 int ret;
1214 struct MSVCRT__stat64 buf64;
1216 ret = MSVCRT__fstat64(fd, &buf64);
1217 if (!ret)
1218 msvcrt_stat64_to_stati64(&buf64, buf);
1219 return ret;
1222 /*********************************************************************
1223 * _fstat (MSVCRT.@)
1225 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1226 { int ret;
1227 struct MSVCRT__stat64 buf64;
1229 ret = MSVCRT__fstat64(fd, &buf64);
1230 if (!ret)
1231 msvcrt_stat64_to_stat(&buf64, buf);
1232 return ret;
1235 /*********************************************************************
1236 * _futime64 (MSVCRT.@)
1238 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1240 HANDLE hand = msvcrt_fdtoh(fd);
1241 FILETIME at, wt;
1243 if (!t)
1245 time_to_filetime( MSVCRT__time64(NULL), &at );
1246 wt = at;
1248 else
1250 time_to_filetime( t->actime, &at );
1251 time_to_filetime( t->modtime, &wt );
1254 if (!SetFileTime(hand, NULL, &at, &wt))
1256 msvcrt_set_errno(GetLastError());
1257 return -1 ;
1259 return 0;
1262 /*********************************************************************
1263 * _futime32 (MSVCRT.@)
1265 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1267 struct MSVCRT___utimbuf64 t64;
1268 t64.actime = t->actime;
1269 t64.modtime = t->modtime;
1270 return _futime64( fd, &t64 );
1273 /*********************************************************************
1274 * _futime (MSVCRT.@)
1276 #ifdef _WIN64
1277 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1279 return _futime64( fd, t );
1281 #else
1282 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1284 return _futime32( fd, t );
1286 #endif
1288 /*********************************************************************
1289 * _get_osfhandle (MSVCRT.@)
1291 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1293 HANDLE hand = msvcrt_fdtoh(fd);
1294 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1296 return (MSVCRT_intptr_t)hand;
1299 /*********************************************************************
1300 * _isatty (MSVCRT.@)
1302 int CDECL _isatty(int fd)
1304 HANDLE hand = msvcrt_fdtoh(fd);
1306 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1307 if (hand == INVALID_HANDLE_VALUE)
1308 return 0;
1310 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1313 /*********************************************************************
1314 * _mktemp (MSVCRT.@)
1316 char * CDECL _mktemp(char *pattern)
1318 int numX = 0;
1319 char *retVal = pattern;
1320 int id;
1321 char letter = 'a';
1323 while(*pattern)
1324 numX = (*pattern++ == 'X')? numX + 1 : 0;
1325 if (numX < 5)
1326 return NULL;
1327 pattern--;
1328 id = GetCurrentProcessId();
1329 numX = 6;
1330 while(numX--)
1332 int tempNum = id / 10;
1333 *pattern-- = id - (tempNum * 10) + '0';
1334 id = tempNum;
1336 pattern++;
1339 *pattern = letter++;
1340 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1341 GetLastError() == ERROR_FILE_NOT_FOUND)
1342 return retVal;
1343 } while(letter <= 'z');
1344 return NULL;
1347 /*********************************************************************
1348 * _wmktemp (MSVCRT.@)
1350 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1352 int numX = 0;
1353 MSVCRT_wchar_t *retVal = pattern;
1354 int id;
1355 MSVCRT_wchar_t letter = 'a';
1357 while(*pattern)
1358 numX = (*pattern++ == 'X')? numX + 1 : 0;
1359 if (numX < 5)
1360 return NULL;
1361 pattern--;
1362 id = GetCurrentProcessId();
1363 numX = 6;
1364 while(numX--)
1366 int tempNum = id / 10;
1367 *pattern-- = id - (tempNum * 10) + '0';
1368 id = tempNum;
1370 pattern++;
1373 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1374 GetLastError() == ERROR_FILE_NOT_FOUND)
1375 return retVal;
1376 *pattern = letter++;
1377 } while(letter != '|');
1378 return NULL;
1381 static unsigned split_oflags(unsigned oflags)
1383 int wxflags = 0;
1384 unsigned unsupp; /* until we support everything */
1386 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1387 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1388 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1389 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1390 else wxflags |= WX_TEXT; /* default to TEXT*/
1391 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1393 if ((unsupp = oflags & ~(
1394 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1395 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1396 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1397 MSVCRT__O_NOINHERIT|
1398 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1400 ERR(":unsupported oflags 0x%04x\n",unsupp);
1402 return wxflags;
1405 /*********************************************************************
1406 * _pipe (MSVCRT.@)
1408 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1410 int ret = -1;
1411 SECURITY_ATTRIBUTES sa;
1412 HANDLE readHandle, writeHandle;
1414 if (!pfds)
1416 *MSVCRT__errno() = MSVCRT_EINVAL;
1417 return -1;
1420 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1421 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1422 sa.lpSecurityDescriptor = NULL;
1423 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1425 unsigned int wxflags = split_oflags(textmode);
1426 int fd;
1428 LOCK_FILES();
1429 fd = msvcrt_alloc_fd(readHandle, wxflags);
1430 if (fd != -1)
1432 pfds[0] = fd;
1433 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1434 if (fd != -1)
1436 pfds[1] = fd;
1437 ret = 0;
1439 else
1441 MSVCRT__close(pfds[0]);
1442 CloseHandle(writeHandle);
1443 *MSVCRT__errno() = MSVCRT_EMFILE;
1446 else
1448 CloseHandle(readHandle);
1449 CloseHandle(writeHandle);
1450 *MSVCRT__errno() = MSVCRT_EMFILE;
1452 UNLOCK_FILES();
1454 else
1455 msvcrt_set_errno(GetLastError());
1457 return ret;
1460 /*********************************************************************
1461 * _sopen (MSVCRT.@)
1463 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1465 __ms_va_list ap;
1466 int pmode;
1467 DWORD access = 0, creation = 0, attrib;
1468 DWORD sharing;
1469 int wxflag = 0, fd;
1470 HANDLE hand;
1471 SECURITY_ATTRIBUTES sa;
1474 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1475 path, oflags, shflags);
1477 wxflag = split_oflags(oflags);
1478 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1480 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1481 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1482 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1485 if (oflags & MSVCRT__O_CREAT)
1487 __ms_va_start(ap, shflags);
1488 pmode = va_arg(ap, int);
1489 __ms_va_end(ap);
1491 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1492 FIXME(": pmode 0x%04x ignored\n", pmode);
1493 else
1494 WARN(": pmode 0x%04x ignored\n", pmode);
1496 if (oflags & MSVCRT__O_EXCL)
1497 creation = CREATE_NEW;
1498 else if (oflags & MSVCRT__O_TRUNC)
1499 creation = CREATE_ALWAYS;
1500 else
1501 creation = OPEN_ALWAYS;
1503 else /* no MSVCRT__O_CREAT */
1505 if (oflags & MSVCRT__O_TRUNC)
1506 creation = TRUNCATE_EXISTING;
1507 else
1508 creation = OPEN_EXISTING;
1511 switch( shflags )
1513 case MSVCRT__SH_DENYRW:
1514 sharing = 0L;
1515 break;
1516 case MSVCRT__SH_DENYWR:
1517 sharing = FILE_SHARE_READ;
1518 break;
1519 case MSVCRT__SH_DENYRD:
1520 sharing = FILE_SHARE_WRITE;
1521 break;
1522 case MSVCRT__SH_DENYNO:
1523 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1524 break;
1525 default:
1526 ERR( "Unhandled shflags 0x%x\n", shflags );
1527 return -1;
1529 attrib = FILE_ATTRIBUTE_NORMAL;
1531 if (oflags & MSVCRT__O_TEMPORARY)
1533 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1534 access |= DELETE;
1535 sharing |= FILE_SHARE_DELETE;
1538 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1539 sa.lpSecurityDescriptor = NULL;
1540 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1542 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1544 if (hand == INVALID_HANDLE_VALUE) {
1545 WARN(":failed-last error (%d)\n",GetLastError());
1546 msvcrt_set_errno(GetLastError());
1547 return -1;
1550 fd = msvcrt_alloc_fd(hand, wxflag);
1552 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1553 return fd;
1556 /*********************************************************************
1557 * _wsopen (MSVCRT.@)
1559 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1561 __ms_va_list ap;
1562 int pmode;
1563 DWORD access = 0, creation = 0, attrib;
1564 DWORD sharing;
1565 int wxflag = 0, fd;
1566 HANDLE hand;
1567 SECURITY_ATTRIBUTES sa;
1570 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1571 debugstr_w(path), oflags, shflags);
1573 wxflag = split_oflags(oflags);
1574 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1576 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1577 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1578 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1581 if (oflags & MSVCRT__O_CREAT)
1583 __ms_va_start(ap, shflags);
1584 pmode = va_arg(ap, int);
1585 __ms_va_end(ap);
1587 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1588 FIXME(": pmode 0x%04x ignored\n", pmode);
1589 else
1590 WARN(": pmode 0x%04x ignored\n", pmode);
1592 if (oflags & MSVCRT__O_EXCL)
1593 creation = CREATE_NEW;
1594 else if (oflags & MSVCRT__O_TRUNC)
1595 creation = CREATE_ALWAYS;
1596 else
1597 creation = OPEN_ALWAYS;
1599 else /* no MSVCRT__O_CREAT */
1601 if (oflags & MSVCRT__O_TRUNC)
1602 creation = TRUNCATE_EXISTING;
1603 else
1604 creation = OPEN_EXISTING;
1607 switch( shflags )
1609 case MSVCRT__SH_DENYRW:
1610 sharing = 0L;
1611 break;
1612 case MSVCRT__SH_DENYWR:
1613 sharing = FILE_SHARE_READ;
1614 break;
1615 case MSVCRT__SH_DENYRD:
1616 sharing = FILE_SHARE_WRITE;
1617 break;
1618 case MSVCRT__SH_DENYNO:
1619 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1620 break;
1621 default:
1622 ERR( "Unhandled shflags 0x%x\n", shflags );
1623 return -1;
1625 attrib = FILE_ATTRIBUTE_NORMAL;
1627 if (oflags & MSVCRT__O_TEMPORARY)
1629 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1630 access |= DELETE;
1631 sharing |= FILE_SHARE_DELETE;
1634 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1635 sa.lpSecurityDescriptor = NULL;
1636 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1638 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1640 if (hand == INVALID_HANDLE_VALUE) {
1641 WARN(":failed-last error (%d)\n",GetLastError());
1642 msvcrt_set_errno(GetLastError());
1643 return -1;
1646 fd = msvcrt_alloc_fd(hand, wxflag);
1648 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1649 return fd;
1652 /*********************************************************************
1653 * _open (MSVCRT.@)
1655 int CDECL MSVCRT__open( const char *path, int flags, ... )
1657 __ms_va_list ap;
1659 if (flags & MSVCRT__O_CREAT)
1661 int pmode;
1662 __ms_va_start(ap, flags);
1663 pmode = va_arg(ap, int);
1664 __ms_va_end(ap);
1665 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1667 else
1668 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1671 /*********************************************************************
1672 * _wopen (MSVCRT.@)
1674 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1676 __ms_va_list ap;
1678 if (flags & MSVCRT__O_CREAT)
1680 int pmode;
1681 __ms_va_start(ap, flags);
1682 pmode = va_arg(ap, int);
1683 __ms_va_end(ap);
1684 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1686 else
1687 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1690 /*********************************************************************
1691 * _creat (MSVCRT.@)
1693 int CDECL MSVCRT__creat(const char *path, int flags)
1695 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1696 return MSVCRT__open(path, usedFlags);
1699 /*********************************************************************
1700 * _wcreat (MSVCRT.@)
1702 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1704 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1705 return _wopen(path, usedFlags);
1708 /*********************************************************************
1709 * _open_osfhandle (MSVCRT.@)
1711 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1713 int fd;
1715 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1716 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1717 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1718 * text - it never sets MSVCRT__O_BINARY.
1720 /* don't let split_oflags() decide the mode if no mode is passed */
1721 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1722 oflags |= MSVCRT__O_BINARY;
1724 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1725 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1726 return fd;
1729 /*********************************************************************
1730 * _rmtmp (MSVCRT.@)
1732 int CDECL _rmtmp(void)
1734 int num_removed = 0, i;
1736 LOCK_FILES();
1737 for (i = 3; i < MSVCRT_stream_idx; i++)
1738 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1740 MSVCRT_fclose(MSVCRT_fstreams[i]);
1741 num_removed++;
1743 UNLOCK_FILES();
1745 if (num_removed)
1746 TRACE(":removed (%d) temp files\n",num_removed);
1747 return num_removed;
1750 /*********************************************************************
1751 * (internal) read_i
1753 * When reading \r as last character in text mode, read() positions
1754 * the file pointer on the \r character while getc() goes on to
1755 * the following \n
1757 static int read_i(int fd, void *buf, unsigned int count)
1759 DWORD num_read;
1760 char *bufstart = buf;
1761 HANDLE hand = msvcrt_fdtoh(fd);
1763 if (count == 0)
1764 return 0;
1766 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1767 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1768 TRACE("already at EOF, returning 0\n");
1769 return 0;
1771 /* Don't trace small reads, it gets *very* annoying */
1772 if (count > 4)
1773 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1774 if (hand == INVALID_HANDLE_VALUE)
1775 return -1;
1777 /* Reading single bytes in O_TEXT mode makes things slow
1778 * So read big chunks
1780 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1782 if (count != 0 && num_read == 0)
1784 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1785 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1787 else if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1789 DWORD i, j;
1790 if (bufstart[num_read-1] == '\r')
1792 if(count == 1)
1794 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1795 ReadFile(hand, bufstart, 1, &num_read, NULL);
1797 else
1799 MSVCRT_fdesc[fd].wxflag |= WX_READCR;
1800 num_read--;
1803 else
1804 MSVCRT_fdesc[fd].wxflag &= ~WX_READCR;
1805 for (i=0, j=0; i<num_read; i++)
1807 /* in text mode, a ctrl-z signals EOF */
1808 if (bufstart[i] == 0x1a)
1810 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1811 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1812 break;
1814 /* in text mode, strip \r if followed by \n.
1815 * BUG: should save state across calls somehow, so CR LF that
1816 * straddles buffer boundary gets recognized properly?
1818 if ((bufstart[i] != '\r')
1819 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1820 bufstart[j++] = bufstart[i];
1822 num_read = j;
1825 else
1827 if (GetLastError() == ERROR_BROKEN_PIPE)
1829 TRACE(":end-of-pipe\n");
1830 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1831 return 0;
1833 else
1835 TRACE(":failed-last error (%d)\n",GetLastError());
1836 return -1;
1840 if (count > 4)
1841 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1842 return num_read;
1845 /*********************************************************************
1846 * _read (MSVCRT.@)
1848 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1850 int num_read;
1851 num_read = read_i(fd, buf, count);
1852 return num_read;
1855 /*********************************************************************
1856 * _setmode (MSVCRT.@)
1858 int CDECL _setmode(int fd,int mode)
1860 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1861 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1862 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1863 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1864 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1865 else
1866 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1867 return ret;
1870 /*********************************************************************
1871 * _stat64 (MSVCRT.@)
1873 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
1875 DWORD dw;
1876 WIN32_FILE_ATTRIBUTE_DATA hfi;
1877 unsigned short mode = ALL_S_IREAD;
1878 int plen;
1880 TRACE(":file (%s) buf(%p)\n",path,buf);
1882 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1884 TRACE("failed (%d)\n",GetLastError());
1885 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1886 return -1;
1889 memset(buf,0,sizeof(struct MSVCRT__stat64));
1891 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1892 Bon 011120: This FIXME seems incorrect
1893 Also a letter as first char isn't enough to be classified
1894 as a drive letter
1896 if (isalpha(*path)&& (*(path+1)==':'))
1897 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1898 else
1899 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1901 plen = strlen(path);
1903 /* Dir, or regular file? */
1904 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1905 (path[plen-1] == '\\'))
1906 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1907 else
1909 mode |= MSVCRT__S_IFREG;
1910 /* executable? */
1911 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1913 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1914 (tolower(path[plen-3]) << 16);
1915 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1916 mode |= ALL_S_IEXEC;
1920 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1921 mode |= ALL_S_IWRITE;
1923 buf->st_mode = mode;
1924 buf->st_nlink = 1;
1925 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1926 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1927 buf->st_atime = dw;
1928 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1929 buf->st_mtime = buf->st_ctime = dw;
1930 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
1931 (int)(buf->st_size >> 32),(int)buf->st_size,
1932 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
1933 return 0;
1936 /*********************************************************************
1937 * _stati64 (MSVCRT.@)
1939 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
1941 int ret;
1942 struct MSVCRT__stat64 buf64;
1944 ret = MSVCRT_stat64(path, &buf64);
1945 if (!ret)
1946 msvcrt_stat64_to_stati64(&buf64, buf);
1947 return ret;
1950 /*********************************************************************
1951 * _stat (MSVCRT.@)
1953 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
1954 { int ret;
1955 struct MSVCRT__stat64 buf64;
1957 ret = MSVCRT_stat64( path, &buf64);
1958 if (!ret)
1959 msvcrt_stat64_to_stat(&buf64, buf);
1960 return ret;
1963 /*********************************************************************
1964 * _wstat64 (MSVCRT.@)
1966 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1968 DWORD dw;
1969 WIN32_FILE_ATTRIBUTE_DATA hfi;
1970 unsigned short mode = ALL_S_IREAD;
1971 int plen;
1973 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1975 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1977 TRACE("failed (%d)\n",GetLastError());
1978 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1979 return -1;
1982 memset(buf,0,sizeof(struct MSVCRT__stat64));
1984 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1985 if (MSVCRT_iswalpha(*path))
1986 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1987 else
1988 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1990 plen = strlenW(path);
1992 /* Dir, or regular file? */
1993 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1994 (path[plen-1] == '\\'))
1995 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1996 else
1998 mode |= MSVCRT__S_IFREG;
1999 /* executable? */
2000 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2002 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2003 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2004 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2005 mode |= ALL_S_IEXEC;
2009 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2010 mode |= ALL_S_IWRITE;
2012 buf->st_mode = mode;
2013 buf->st_nlink = 1;
2014 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2015 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2016 buf->st_atime = dw;
2017 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2018 buf->st_mtime = buf->st_ctime = dw;
2019 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2020 (int)(buf->st_size >> 32),(int)buf->st_size,
2021 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2022 return 0;
2025 /*********************************************************************
2026 * _wstati64 (MSVCRT.@)
2028 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2030 int ret;
2031 struct MSVCRT__stat64 buf64;
2033 ret = MSVCRT__wstat64(path, &buf64);
2034 if (!ret)
2035 msvcrt_stat64_to_stati64(&buf64, buf);
2036 return ret;
2039 /*********************************************************************
2040 * _wstat (MSVCRT.@)
2042 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2044 int ret;
2045 struct MSVCRT__stat64 buf64;
2047 ret = MSVCRT__wstat64( path, &buf64 );
2048 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2049 return ret;
2052 /*********************************************************************
2053 * _tell (MSVCRT.@)
2055 MSVCRT_long CDECL MSVCRT__tell(int fd)
2057 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2060 /*********************************************************************
2061 * _telli64 (MSVCRT.@)
2063 __int64 CDECL _telli64(int fd)
2065 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2068 /*********************************************************************
2069 * _tempnam (MSVCRT.@)
2071 char * CDECL _tempnam(const char *dir, const char *prefix)
2073 char tmpbuf[MAX_PATH];
2074 const char *tmp_dir = MSVCRT_getenv("TMP");
2076 if (tmp_dir) dir = tmp_dir;
2078 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2079 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2081 TRACE("got name (%s)\n",tmpbuf);
2082 DeleteFileA(tmpbuf);
2083 return _strdup(tmpbuf);
2085 TRACE("failed (%d)\n",GetLastError());
2086 return NULL;
2089 /*********************************************************************
2090 * _wtempnam (MSVCRT.@)
2092 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2094 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2096 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2097 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2099 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2100 DeleteFileW(tmpbuf);
2101 return _wcsdup(tmpbuf);
2103 TRACE("failed (%d)\n",GetLastError());
2104 return NULL;
2107 /*********************************************************************
2108 * _umask (MSVCRT.@)
2110 int CDECL MSVCRT__umask(int umask)
2112 int old_umask = MSVCRT_umask;
2113 TRACE("(%d)\n",umask);
2114 MSVCRT_umask = umask;
2115 return old_umask;
2118 /*********************************************************************
2119 * _utime64 (MSVCRT.@)
2121 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2123 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2125 if (fd > 0)
2127 int retVal = _futime64(fd, t);
2128 MSVCRT__close(fd);
2129 return retVal;
2131 return -1;
2134 /*********************************************************************
2135 * _utime32 (MSVCRT.@)
2137 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2139 struct MSVCRT___utimbuf64 t64;
2140 t64.actime = t->actime;
2141 t64.modtime = t->modtime;
2142 return _utime64( path, &t64 );
2145 /*********************************************************************
2146 * _utime (MSVCRT.@)
2148 #ifdef _WIN64
2149 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2151 return _utime64( path, t );
2153 #else
2154 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2156 return _utime32( path, t );
2158 #endif
2160 /*********************************************************************
2161 * _wutime64 (MSVCRT.@)
2163 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2165 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2167 if (fd > 0)
2169 int retVal = _futime64(fd, t);
2170 MSVCRT__close(fd);
2171 return retVal;
2173 return -1;
2176 /*********************************************************************
2177 * _wutime32 (MSVCRT.@)
2179 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2181 struct MSVCRT___utimbuf64 t64;
2182 t64.actime = t->actime;
2183 t64.modtime = t->modtime;
2184 return _wutime64( path, &t64 );
2187 /*********************************************************************
2188 * _wutime (MSVCRT.@)
2190 #ifdef _WIN64
2191 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2193 return _wutime64( path, t );
2195 #else
2196 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2198 return _wutime32( path, t );
2200 #endif
2202 /*********************************************************************
2203 * _write (MSVCRT.@)
2205 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2207 DWORD num_written;
2208 HANDLE hand = msvcrt_fdtoh(fd);
2210 /* Don't trace small writes, it gets *very* annoying */
2211 #if 0
2212 if (count > 32)
2213 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2214 #endif
2215 if (hand == INVALID_HANDLE_VALUE)
2217 *MSVCRT__errno() = MSVCRT_EBADF;
2218 return -1;
2221 /* If appending, go to EOF */
2222 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2223 MSVCRT__lseek(fd, 0, FILE_END);
2225 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2227 if (WriteFile(hand, buf, count, &num_written, NULL)
2228 && (num_written == count))
2229 return num_written;
2230 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2231 hand, GetLastError());
2232 *MSVCRT__errno() = MSVCRT_ENOSPC;
2234 else
2236 unsigned int i, j, nr_lf;
2237 char *p = NULL;
2238 const char *q;
2239 const char *s = buf, *buf_start = buf;
2240 /* find number of \n ( without preceding \r ) */
2241 for ( nr_lf=0,i = 0; i <count; i++)
2243 if (s[i]== '\n')
2245 nr_lf++;
2246 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2249 if (nr_lf)
2251 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2253 for (s = buf, i = 0, j = 0; i < count; i++)
2255 if (s[i]== '\n')
2257 p[j++] = '\r';
2258 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2260 p[j++] = s[i];
2263 else
2265 FIXME("Malloc failed\n");
2266 nr_lf =0;
2267 q = buf;
2270 else
2271 q = buf;
2273 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2275 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2276 fd, hand, GetLastError(), num_written);
2277 *MSVCRT__errno() = MSVCRT_ENOSPC;
2278 if(nr_lf)
2279 MSVCRT_free(p);
2280 return s - buf_start;
2282 else
2284 if(nr_lf)
2285 MSVCRT_free(p);
2286 return count;
2289 return -1;
2292 /*********************************************************************
2293 * _putw (MSVCRT.@)
2295 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2297 int len;
2298 len = MSVCRT__write(file->_file, &val, sizeof(val));
2299 if (len == sizeof(val)) return val;
2300 file->_flag |= MSVCRT__IOERR;
2301 return MSVCRT_EOF;
2304 /*********************************************************************
2305 * fclose (MSVCRT.@)
2307 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2309 int r, flag;
2311 flag = file->_flag;
2312 MSVCRT_free(file->_tmpfname);
2313 file->_tmpfname = NULL;
2314 /* flush stdio buffers */
2315 if(file->_flag & MSVCRT__IOWRT)
2316 MSVCRT_fflush(file);
2317 if(file->_flag & MSVCRT__IOMYBUF)
2318 MSVCRT_free(file->_base);
2320 r=MSVCRT__close(file->_file);
2322 file->_flag = 0;
2324 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2327 /*********************************************************************
2328 * feof (MSVCRT.@)
2330 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2332 return file->_flag & MSVCRT__IOEOF;
2335 /*********************************************************************
2336 * ferror (MSVCRT.@)
2338 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2340 return file->_flag & MSVCRT__IOERR;
2343 /*********************************************************************
2344 * _filbuf (MSVCRT.@)
2346 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2348 /* Allocate buffer if needed */
2349 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2350 msvcrt_alloc_buffer(file);
2352 if(!(file->_flag & MSVCRT__IOREAD)) {
2353 if(file->_flag & MSVCRT__IORW) {
2354 file->_flag |= MSVCRT__IOREAD;
2355 } else {
2356 return MSVCRT_EOF;
2359 if(file->_flag & MSVCRT__IONBF) {
2360 unsigned char c;
2361 int r;
2362 if ((r = read_i(file->_file,&c,1)) != 1) {
2363 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2364 return MSVCRT_EOF;
2366 return c;
2367 } else {
2368 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2369 if(file->_cnt<=0) {
2370 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2371 file->_cnt = 0;
2372 return MSVCRT_EOF;
2374 file->_cnt--;
2375 file->_ptr = file->_base+1;
2376 return *(unsigned char *)file->_base;
2380 /*********************************************************************
2381 * fgetc (MSVCRT.@)
2383 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2385 unsigned char *i;
2386 unsigned int j;
2387 if (file->_cnt>0) {
2388 file->_cnt--;
2389 i = (unsigned char *)file->_ptr++;
2390 j = *i;
2391 } else
2392 j = MSVCRT__filbuf(file);
2393 return j;
2396 /*********************************************************************
2397 * _fgetchar (MSVCRT.@)
2399 int CDECL _fgetchar(void)
2401 return MSVCRT_fgetc(MSVCRT_stdin);
2404 /*********************************************************************
2405 * fgets (MSVCRT.@)
2407 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2409 int cc = MSVCRT_EOF;
2410 char * buf_start = s;
2412 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2413 file,file->_file,s,size);
2415 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2417 *s++ = (char)cc;
2418 size --;
2420 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2422 TRACE(":nothing read\n");
2423 return NULL;
2425 if ((cc != MSVCRT_EOF) && (size > 1))
2426 *s++ = cc;
2427 *s = '\0';
2428 TRACE(":got %s\n", debugstr_a(buf_start));
2429 return buf_start;
2432 /*********************************************************************
2433 * fgetwc (MSVCRT.@)
2435 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2436 * the CR from CR/LF combinations
2438 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2440 char c;
2442 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2444 MSVCRT_wchar_t wc;
2445 unsigned int i;
2446 int j;
2447 char *chp, *wcp;
2448 wcp = (char *)&wc;
2449 for(i=0; i<sizeof(wc); i++)
2451 if (file->_cnt>0)
2453 file->_cnt--;
2454 chp = file->_ptr++;
2455 wcp[i] = *chp;
2457 else
2459 j = MSVCRT__filbuf(file);
2460 if(file->_cnt<=0)
2462 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2463 file->_cnt = 0;
2464 return MSVCRT_WEOF;
2466 wcp[i] = j;
2469 return wc;
2472 c = MSVCRT_fgetc(file);
2473 if ((get_locale()->locinfo->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2475 FIXME("Treat Multibyte characters\n");
2477 if (c == MSVCRT_EOF)
2478 return MSVCRT_WEOF;
2479 else
2480 return (MSVCRT_wint_t)c;
2483 /*********************************************************************
2484 * _getw (MSVCRT.@)
2486 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2488 char *ch;
2489 int i, k;
2490 unsigned int j;
2491 ch = (char *)&i;
2492 for (j=0; j<sizeof(int); j++) {
2493 k = MSVCRT_fgetc(file);
2494 if (k == MSVCRT_EOF) {
2495 file->_flag |= MSVCRT__IOEOF;
2496 return EOF;
2498 ch[j] = k;
2500 return i;
2503 /*********************************************************************
2504 * getwc (MSVCRT.@)
2506 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2508 return MSVCRT_fgetwc(file);
2511 /*********************************************************************
2512 * _fgetwchar (MSVCRT.@)
2514 MSVCRT_wint_t CDECL _fgetwchar(void)
2516 return MSVCRT_fgetwc(MSVCRT_stdin);
2519 /*********************************************************************
2520 * getwchar (MSVCRT.@)
2522 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2524 return _fgetwchar();
2527 /*********************************************************************
2528 * fgetws (MSVCRT.@)
2530 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2532 int cc = MSVCRT_WEOF;
2533 MSVCRT_wchar_t * buf_start = s;
2535 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2536 file,file->_file,s,size);
2538 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2540 *s++ = (char)cc;
2541 size --;
2543 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2545 TRACE(":nothing read\n");
2546 return NULL;
2548 if ((cc != MSVCRT_WEOF) && (size > 1))
2549 *s++ = cc;
2550 *s = 0;
2551 TRACE(":got %s\n", debugstr_w(buf_start));
2552 return buf_start;
2555 /*********************************************************************
2556 * fwrite (MSVCRT.@)
2558 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2560 MSVCRT_size_t wrcnt=size * nmemb;
2561 int written = 0;
2562 if (size == 0)
2563 return 0;
2564 if(file->_cnt) {
2565 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2566 memcpy(file->_ptr, ptr, pcnt);
2567 file->_cnt -= pcnt;
2568 file->_ptr += pcnt;
2569 written = pcnt;
2570 wrcnt -= pcnt;
2571 ptr = (const char*)ptr + pcnt;
2572 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2573 if(file->_flag & MSVCRT__IORW) {
2574 file->_flag |= MSVCRT__IOWRT;
2575 } else
2576 return 0;
2578 if(wrcnt) {
2579 /* Flush buffer */
2580 int res=msvcrt_flush_buffer(file);
2581 if(!res) {
2582 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2583 if (pwritten <= 0)
2585 file->_flag |= MSVCRT__IOERR;
2586 pwritten=0;
2588 written += pwritten;
2591 return written / size;
2594 /*********************************************************************
2595 * fputwc (MSVCRT.@)
2597 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2599 MSVCRT_wchar_t mwc=wc;
2600 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2601 return MSVCRT_WEOF;
2602 return wc;
2605 /*********************************************************************
2606 * _fputwchar (MSVCRT.@)
2608 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2610 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2613 /*********************************************************************
2614 * _wfsopen (MSVCRT.@)
2616 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2618 MSVCRT_FILE* file;
2619 int open_flags, stream_flags, fd;
2621 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2623 /* map mode string to open() flags. "man fopen" for possibilities. */
2624 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2625 return NULL;
2627 LOCK_FILES();
2628 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2629 if (fd < 0)
2630 file = NULL;
2631 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2632 != -1)
2633 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2634 else if (file)
2636 file->_flag = 0;
2637 file = NULL;
2640 TRACE(":got (%p)\n",file);
2641 if (fd >= 0 && !file)
2642 MSVCRT__close(fd);
2643 UNLOCK_FILES();
2644 return file;
2647 /*********************************************************************
2648 * _fsopen (MSVCRT.@)
2650 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2652 MSVCRT_FILE *ret;
2653 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2655 if (path && !(pathW = msvcrt_wstrdupa(path))) {
2656 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2657 *MSVCRT__errno() = MSVCRT_EINVAL;
2658 return NULL;
2660 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2662 MSVCRT_free(pathW);
2663 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2664 *MSVCRT__errno() = MSVCRT_EINVAL;
2665 return NULL;
2668 ret = MSVCRT__wfsopen(pathW, modeW, share);
2670 MSVCRT_free(pathW);
2671 MSVCRT_free(modeW);
2672 return ret;
2675 /*********************************************************************
2676 * fopen (MSVCRT.@)
2678 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2680 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2683 /*********************************************************************
2684 * fopen_s (MSVCRT.@)
2686 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
2687 const char *filename, const char *mode)
2689 if(!pFile) {
2690 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2691 *MSVCRT__errno() = MSVCRT_EINVAL;
2692 return MSVCRT_EINVAL;
2695 *pFile = MSVCRT_fopen(filename, mode);
2697 if(!*pFile)
2698 return *MSVCRT__errno();
2699 return 0;
2702 /*********************************************************************
2703 * _wfopen (MSVCRT.@)
2705 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2707 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2710 /*********************************************************************
2711 * _wfopen_s (MSVCRT.@)
2713 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
2714 const MSVCRT_wchar_t *mode)
2716 if(!pFile) {
2717 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
2718 *MSVCRT__errno() = MSVCRT_EINVAL;
2719 return MSVCRT_EINVAL;
2722 *pFile = MSVCRT__wfopen(filename, mode);
2724 if(!*pFile)
2725 return *MSVCRT__errno();
2726 return 0;
2729 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2730 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2732 /*********************************************************************
2733 * fputc (MSVCRT.@)
2735 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2737 if(file->_cnt>0) {
2738 *file->_ptr++=c;
2739 file->_cnt--;
2740 if (c == '\n')
2742 int res = msvcrt_flush_buffer(file);
2743 return res ? res : c;
2745 else
2746 return c & 0xff;
2747 } else {
2748 return MSVCRT__flsbuf(c, file);
2752 /*********************************************************************
2753 * _flsbuf (MSVCRT.@)
2755 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2757 /* Flush output buffer */
2758 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2759 msvcrt_alloc_buffer(file);
2761 if(!(file->_flag & MSVCRT__IOWRT)) {
2762 if(file->_flag & MSVCRT__IORW) {
2763 file->_flag |= MSVCRT__IOWRT;
2764 } else {
2765 return MSVCRT_EOF;
2768 if(file->_bufsiz) {
2769 int res=msvcrt_flush_buffer(file);
2770 return res?res : MSVCRT_fputc(c, file);
2771 } else {
2772 unsigned char cc=c;
2773 int len;
2774 /* set _cnt to 0 for unbuffered FILEs */
2775 file->_cnt = 0;
2776 len = MSVCRT__write(file->_file, &cc, 1);
2777 if (len == 1) return c & 0xff;
2778 file->_flag |= MSVCRT__IOERR;
2779 return MSVCRT_EOF;
2783 /*********************************************************************
2784 * _fputchar (MSVCRT.@)
2786 int CDECL _fputchar(int c)
2788 return MSVCRT_fputc(c, MSVCRT_stdout);
2791 /*********************************************************************
2792 * fread (MSVCRT.@)
2794 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2795 { MSVCRT_size_t rcnt=size * nmemb;
2796 MSVCRT_size_t read=0;
2797 int pread=0;
2799 if(!rcnt)
2800 return 0;
2802 /* first buffered data */
2803 if(file->_cnt>0) {
2804 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2805 memcpy(ptr, file->_ptr, pcnt);
2806 file->_cnt -= pcnt;
2807 file->_ptr += pcnt;
2808 read += pcnt ;
2809 rcnt -= pcnt ;
2810 ptr = (char*)ptr + pcnt;
2811 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2812 if(file->_flag & MSVCRT__IORW) {
2813 file->_flag |= MSVCRT__IOREAD;
2814 } else
2815 return 0;
2817 while(rcnt>0)
2819 int i;
2820 /* Fill the buffer on small reads.
2821 * TODO: Use a better buffering strategy.
2823 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2824 if (file->_bufsiz == 0) {
2825 msvcrt_alloc_buffer(file);
2827 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2828 file->_ptr = file->_base;
2829 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2830 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2831 if (i > 0 && i < file->_cnt) {
2832 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2833 file->_flag &= ~MSVCRT__IOEOF;
2835 if (i > 0) {
2836 memcpy(ptr, file->_ptr, i);
2837 file->_cnt -= i;
2838 file->_ptr += i;
2840 } else {
2841 i = MSVCRT__read(file->_file,ptr, rcnt);
2843 pread += i;
2844 rcnt -= i;
2845 ptr = (char *)ptr+i;
2846 /* expose feof condition in the flags
2847 * MFC tests file->_flag for feof, and doesn't call feof())
2849 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2850 file->_flag |= MSVCRT__IOEOF;
2851 else if (i == -1)
2853 file->_flag |= MSVCRT__IOERR;
2854 pread = 0;
2855 rcnt = 0;
2857 if (i < 1) break;
2859 read+=pread;
2860 return read / size;
2863 /*********************************************************************
2864 * _wfreopen (MSVCRT.@)
2867 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
2869 int open_flags, stream_flags, fd;
2871 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2873 LOCK_FILES();
2874 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2875 file = NULL;
2876 else
2878 MSVCRT_fclose(file);
2879 /* map mode string to open() flags. "man fopen" for possibilities. */
2880 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2881 file = NULL;
2882 else
2884 fd = _wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2885 if (fd < 0)
2886 file = NULL;
2887 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2889 file->_flag = 0;
2890 WARN(":failed-last error (%d)\n",GetLastError());
2891 msvcrt_set_errno(GetLastError());
2892 file = NULL;
2896 UNLOCK_FILES();
2897 return file;
2900 /*********************************************************************
2901 * freopen (MSVCRT.@)
2904 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
2906 MSVCRT_FILE *ret;
2907 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
2909 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
2910 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
2912 MSVCRT_free(pathW);
2913 return NULL;
2916 ret = MSVCRT__wfreopen(pathW, modeW, file);
2918 MSVCRT_free(pathW);
2919 MSVCRT_free(modeW);
2920 return ret;
2923 /*********************************************************************
2924 * fsetpos (MSVCRT.@)
2926 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2928 /* Note that all this has been lifted 'as is' from fseek */
2929 if(file->_flag & MSVCRT__IOWRT)
2930 msvcrt_flush_buffer(file);
2932 /* Discard buffered input */
2933 file->_cnt = 0;
2934 file->_ptr = file->_base;
2936 /* Reset direction of i/o */
2937 if(file->_flag & MSVCRT__IORW) {
2938 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2941 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2944 /*********************************************************************
2945 * ftell (MSVCRT.@)
2947 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2949 /* TODO: just call fgetpos and return lower half of result */
2950 int off=0;
2951 MSVCRT_long pos;
2952 pos = MSVCRT__tell(file->_file);
2953 if(pos == -1) return -1;
2954 if(file->_bufsiz) {
2955 if( file->_flag & MSVCRT__IOWRT ) {
2956 off = file->_ptr - file->_base;
2957 } else {
2958 off = -file->_cnt;
2959 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2960 /* Black magic correction for CR removal */
2961 int i;
2962 for (i=0; i<file->_cnt; i++) {
2963 if (file->_ptr[i] == '\n')
2964 off--;
2966 /* Black magic when reading CR at buffer boundary*/
2967 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
2968 off--;
2973 return off + pos;
2976 /*********************************************************************
2977 * fgetpos (MSVCRT.@)
2979 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2981 int off=0;
2982 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
2983 if(*pos == -1) return -1;
2984 if(file->_bufsiz) {
2985 if( file->_flag & MSVCRT__IOWRT ) {
2986 off = file->_ptr - file->_base;
2987 } else {
2988 off = -file->_cnt;
2989 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) {
2990 /* Black magic correction for CR removal */
2991 int i;
2992 for (i=0; i<file->_cnt; i++) {
2993 if (file->_ptr[i] == '\n')
2994 off--;
2996 /* Black magic when reading CR at buffer boundary*/
2997 if(MSVCRT_fdesc[file->_file].wxflag & WX_READCR)
2998 off--;
3002 *pos += off;
3003 return 0;
3006 /*********************************************************************
3007 * fputs (MSVCRT.@)
3009 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3011 MSVCRT_size_t i, len = strlen(s);
3012 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3013 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3014 for (i=0; i<len; i++)
3015 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
3016 return MSVCRT_EOF;
3017 return 0;
3020 /*********************************************************************
3021 * fputws (MSVCRT.@)
3023 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3025 MSVCRT_size_t i, len = strlenW(s);
3026 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
3027 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3028 for (i=0; i<len; i++)
3030 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3031 return MSVCRT_WEOF;
3032 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
3033 return MSVCRT_WEOF;
3035 return 0;
3038 /*********************************************************************
3039 * getchar (MSVCRT.@)
3041 int CDECL MSVCRT_getchar(void)
3043 return MSVCRT_fgetc(MSVCRT_stdin);
3046 /*********************************************************************
3047 * getc (MSVCRT.@)
3049 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3051 return MSVCRT_fgetc(file);
3054 /*********************************************************************
3055 * gets (MSVCRT.@)
3057 char * CDECL MSVCRT_gets(char *buf)
3059 int cc;
3060 char * buf_start = buf;
3062 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3063 cc = MSVCRT_fgetc(MSVCRT_stdin))
3064 if(cc != '\r') *buf++ = (char)cc;
3066 *buf = '\0';
3068 TRACE("got '%s'\n", buf_start);
3069 return buf_start;
3072 /*********************************************************************
3073 * _getws (MSVCRT.@)
3075 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3077 MSVCRT_wint_t cc;
3078 MSVCRT_wchar_t* ws = buf;
3080 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3081 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3083 if (cc != '\r')
3084 *buf++ = (MSVCRT_wchar_t)cc;
3086 *buf = '\0';
3088 TRACE("got %s\n", debugstr_w(ws));
3089 return ws;
3092 /*********************************************************************
3093 * putc (MSVCRT.@)
3095 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3097 return MSVCRT_fputc(c, file);
3100 /*********************************************************************
3101 * putchar (MSVCRT.@)
3103 int CDECL MSVCRT_putchar(int c)
3105 return MSVCRT_fputc(c, MSVCRT_stdout);
3108 /*********************************************************************
3109 * puts (MSVCRT.@)
3111 int CDECL MSVCRT_puts(const char *s)
3113 MSVCRT_size_t len = strlen(s);
3114 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3115 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3118 /*********************************************************************
3119 * _putws (MSVCRT.@)
3121 int CDECL _putws(const MSVCRT_wchar_t *s)
3123 static const MSVCRT_wchar_t nl = '\n';
3124 MSVCRT_size_t len = strlenW(s);
3125 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
3126 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3129 /*********************************************************************
3130 * remove (MSVCRT.@)
3132 int CDECL MSVCRT_remove(const char *path)
3134 TRACE("(%s)\n",path);
3135 if (DeleteFileA(path))
3136 return 0;
3137 TRACE(":failed (%d)\n",GetLastError());
3138 msvcrt_set_errno(GetLastError());
3139 return -1;
3142 /*********************************************************************
3143 * _wremove (MSVCRT.@)
3145 int CDECL _wremove(const MSVCRT_wchar_t *path)
3147 TRACE("(%s)\n",debugstr_w(path));
3148 if (DeleteFileW(path))
3149 return 0;
3150 TRACE(":failed (%d)\n",GetLastError());
3151 msvcrt_set_errno(GetLastError());
3152 return -1;
3155 /*********************************************************************
3156 * rename (MSVCRT.@)
3158 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3160 TRACE(":from %s to %s\n",oldpath,newpath);
3161 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3162 return 0;
3163 TRACE(":failed (%d)\n",GetLastError());
3164 msvcrt_set_errno(GetLastError());
3165 return -1;
3168 /*********************************************************************
3169 * _wrename (MSVCRT.@)
3171 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3173 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3174 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3175 return 0;
3176 TRACE(":failed (%d)\n",GetLastError());
3177 msvcrt_set_errno(GetLastError());
3178 return -1;
3181 /*********************************************************************
3182 * setvbuf (MSVCRT.@)
3184 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3186 /* TODO: Check if file busy */
3187 if(file->_bufsiz) {
3188 MSVCRT_free(file->_base);
3189 file->_bufsiz = 0;
3190 file->_cnt = 0;
3192 if(mode == MSVCRT__IOFBF) {
3193 file->_flag &= ~MSVCRT__IONBF;
3194 file->_base = file->_ptr = buf;
3195 if(buf) {
3196 file->_bufsiz = size;
3198 } else {
3199 file->_flag |= MSVCRT__IONBF;
3201 return 0;
3204 /*********************************************************************
3205 * setbuf (MSVCRT.@)
3207 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3209 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3212 /*********************************************************************
3213 * tmpnam (MSVCRT.@)
3215 char * CDECL MSVCRT_tmpnam(char *s)
3217 static int unique;
3218 char tmpstr[16];
3219 char *p;
3220 int count;
3221 if (s == 0)
3222 s = MSVCRT_tmpname;
3223 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3224 p = s + sprintf(s, "\\s%s.", tmpstr);
3225 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3227 msvcrt_int_to_base32(unique++, tmpstr);
3228 strcpy(p, tmpstr);
3229 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3230 GetLastError() == ERROR_FILE_NOT_FOUND)
3231 break;
3233 return s;
3236 /*********************************************************************
3237 * tmpfile (MSVCRT.@)
3239 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3241 char *filename = MSVCRT_tmpnam(NULL);
3242 int fd;
3243 MSVCRT_FILE* file = NULL;
3245 LOCK_FILES();
3246 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3247 if (fd != -1 && (file = msvcrt_alloc_fp()))
3249 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3251 file->_flag = 0;
3252 file = NULL;
3254 else file->_tmpfname = _strdup(filename);
3256 UNLOCK_FILES();
3257 return file;
3260 /*********************************************************************
3261 * vfprintf (MSVCRT.@)
3263 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3265 char buf[2048], *mem = buf;
3266 int written, resize = sizeof(buf), retval;
3267 /* There are two conventions for vsnprintf failing:
3268 * Return -1 if we truncated, or
3269 * Return the number of bytes that would have been written
3270 * The code below handles both cases
3272 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3273 written > resize)
3275 resize = (written == -1 ? resize * 2 : written + 1);
3276 if (mem != buf)
3277 MSVCRT_free (mem);
3278 if (!(mem = MSVCRT_malloc(resize)))
3279 return MSVCRT_EOF;
3281 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3282 if (mem != buf)
3283 MSVCRT_free (mem);
3284 return retval;
3287 /*********************************************************************
3288 * vfwprintf (MSVCRT.@)
3289 * FIXME:
3290 * Is final char included in written (then resize is too big) or not
3291 * (then we must test for equality too)?
3293 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3295 MSVCRT_wchar_t buf[2048], *mem = buf;
3296 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3297 /* See vfprintf comments */
3298 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3299 written > resize)
3301 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3302 if (mem != buf)
3303 MSVCRT_free (mem);
3304 if (!(mem = MSVCRT_malloc(resize*sizeof(*mem))))
3305 return MSVCRT_EOF;
3307 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3308 if (mem != buf)
3309 MSVCRT_free (mem);
3310 return retval;
3313 /*********************************************************************
3314 * vprintf (MSVCRT.@)
3316 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3318 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3321 /*********************************************************************
3322 * vwprintf (MSVCRT.@)
3324 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3326 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3329 /*********************************************************************
3330 * fprintf (MSVCRT.@)
3332 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3334 __ms_va_list valist;
3335 int res;
3336 __ms_va_start(valist, format);
3337 res = MSVCRT_vfprintf(file, format, valist);
3338 __ms_va_end(valist);
3339 return res;
3342 /*********************************************************************
3343 * fwprintf (MSVCRT.@)
3345 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3347 __ms_va_list valist;
3348 int res;
3349 __ms_va_start(valist, format);
3350 res = MSVCRT_vfwprintf(file, format, valist);
3351 __ms_va_end(valist);
3352 return res;
3355 /*********************************************************************
3356 * printf (MSVCRT.@)
3358 int CDECL MSVCRT_printf(const char *format, ...)
3360 __ms_va_list valist;
3361 int res;
3362 __ms_va_start(valist, format);
3363 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3364 __ms_va_end(valist);
3365 return res;
3368 /*********************************************************************
3369 * ungetc (MSVCRT.@)
3371 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3373 if (c == MSVCRT_EOF)
3374 return MSVCRT_EOF;
3375 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3376 msvcrt_alloc_buffer(file);
3377 file->_ptr++;
3379 if(file->_ptr>file->_base) {
3380 file->_ptr--;
3381 *file->_ptr=c;
3382 file->_cnt++;
3383 MSVCRT_clearerr(file);
3384 return c;
3386 return MSVCRT_EOF;
3389 /*********************************************************************
3390 * ungetwc (MSVCRT.@)
3392 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3394 MSVCRT_wchar_t mwc = wc;
3395 char * pp = (char *)&mwc;
3396 int i;
3397 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3398 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3399 return MSVCRT_WEOF;
3401 return mwc;
3404 /*********************************************************************
3405 * wprintf (MSVCRT.@)
3407 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3409 __ms_va_list valist;
3410 int res;
3411 __ms_va_start(valist, format);
3412 res = MSVCRT_vwprintf(format, valist);
3413 __ms_va_end(valist);
3414 return res;
3417 /*********************************************************************
3418 * _getmaxstdio (MSVCRT.@)
3420 int CDECL _getmaxstdio(void)
3422 FIXME("stub, always returns 512\n");
3423 return 512;
3426 /*********************************************************************
3427 * _setmaxstdio_ (MSVCRT.@)
3429 int CDECL _setmaxstdio(int newmax)
3431 int res;
3432 if( newmax > 2048)
3433 res = -1;
3434 else
3435 res = newmax;
3436 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3437 return res;
3440 /*********************************************************************
3441 * __pioinfo (MSVCRT.@)
3442 * FIXME: see MSVCRT_MAX_FILES define.
3444 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3445 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3446 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3447 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3448 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3449 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3450 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3451 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3452 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3453 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3454 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3455 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3458 /*********************************************************************
3459 * __badioinfo (MSVCRT.@)
3461 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };