usp10: Handle GPOS ValueRecords.
[wine/multimedia.git] / dlls / msvcrt / file.c
blobdf2c7cdc6b88c3b4ea25c4f2ac5a892116677a51
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"
44 #include "mtdll.h"
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
62 #define WX_OPEN 0x01
63 #define WX_ATEOF 0x02
64 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
65 #define WX_READCR 0x08 /* underlying file is at \r */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
68 #define WX_TEXT 0x80
70 /* FIXME: this should be allocated dynamically */
71 #define MSVCRT_MAX_FILES 2048
72 #define MSVCRT_FD_BLOCK_SIZE 32
74 /* ioinfo structure size is different in msvcrXX.dll's */
75 typedef struct {
76 HANDLE handle;
77 unsigned char wxflag;
78 char unk1;
79 BOOL crit_init;
80 CRITICAL_SECTION crit;
81 } ioinfo;
83 /*********************************************************************
84 * __pioinfo (MSVCRT.@)
85 * array of pointers to ioinfo arrays [32]
87 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
89 /*********************************************************************
90 * __badioinfo (MSVCRT.@)
92 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
94 static int MSVCRT_fdstart = 3; /* first unallocated fd */
95 static int MSVCRT_fdend = 3; /* highest allocated fd */
97 typedef struct {
98 MSVCRT_FILE file;
99 CRITICAL_SECTION crit;
100 } file_crit;
102 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
103 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
104 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
106 /* INTERNAL: process umask */
107 static int MSVCRT_umask = 0;
109 /* INTERNAL: static data for tmpnam and _wtmpname functions */
110 static int tmpnam_unique;
112 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
113 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
114 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
115 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
117 #define TOUL(x) (ULONGLONG)(x)
118 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
119 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
120 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
121 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
123 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
124 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
125 * and MSVCRT_stream_idx, from race conditions.
126 * It doesn't protect against race conditions manipulating the underlying files
127 * or flags; doing so would probably be better accomplished with per-file
128 * protection, rather than locking the whole table for every change.
130 static CRITICAL_SECTION MSVCRT_file_cs;
131 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
133 0, 0, &MSVCRT_file_cs,
134 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
135 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
137 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
138 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
139 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
141 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
143 buf->st_dev = buf64->st_dev;
144 buf->st_ino = buf64->st_ino;
145 buf->st_mode = buf64->st_mode;
146 buf->st_nlink = buf64->st_nlink;
147 buf->st_uid = buf64->st_uid;
148 buf->st_gid = buf64->st_gid;
149 buf->st_rdev = buf64->st_rdev;
150 buf->st_size = buf64->st_size;
151 buf->st_atime = buf64->st_atime;
152 buf->st_mtime = buf64->st_mtime;
153 buf->st_ctime = buf64->st_ctime;
156 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
158 buf->st_dev = buf64->st_dev;
159 buf->st_ino = buf64->st_ino;
160 buf->st_mode = buf64->st_mode;
161 buf->st_nlink = buf64->st_nlink;
162 buf->st_uid = buf64->st_uid;
163 buf->st_gid = buf64->st_gid;
164 buf->st_rdev = buf64->st_rdev;
165 buf->st_size = buf64->st_size;
166 buf->st_atime = buf64->st_atime;
167 buf->st_mtime = buf64->st_mtime;
168 buf->st_ctime = buf64->st_ctime;
171 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
173 /* 1601 to 1970 is 369 years plus 89 leap days */
174 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
176 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
177 ft->dwHighDateTime = ticks >> 32;
178 ft->dwLowDateTime = ticks;
181 static inline ioinfo* msvcrt_get_ioinfo(int fd)
183 ioinfo *ret = NULL;
184 if(fd < MSVCRT_MAX_FILES)
185 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
186 if(!ret)
187 return &MSVCRT___badioinfo;
189 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
192 static inline MSVCRT_FILE* msvcrt_get_file(int i)
194 file_crit *ret;
196 if(i >= MSVCRT_max_streams)
197 return NULL;
199 if(i < _IOB_ENTRIES)
200 return &MSVCRT__iob[i];
202 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
203 if(!ret) {
204 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
205 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
206 ERR("out of memory\n");
207 *MSVCRT__errno() = MSVCRT_ENOMEM;
208 return NULL;
211 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
212 } else
213 ret += i%MSVCRT_FD_BLOCK_SIZE;
215 return &ret->file;
218 static inline BOOL msvcrt_is_valid_fd(int fd)
220 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
223 /* INTERNAL: Get the HANDLE for a fd
224 * This doesn't lock the table, because a failure will result in
225 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
226 * it returns a valid handle which is about to be closed, a subsequent call
227 * will fail, most likely in a sane way.
229 static HANDLE msvcrt_fdtoh(int fd)
231 if (!msvcrt_is_valid_fd(fd))
233 WARN(":fd (%d) - no handle!\n",fd);
234 *MSVCRT___doserrno() = 0;
235 *MSVCRT__errno() = MSVCRT_EBADF;
236 return INVALID_HANDLE_VALUE;
238 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
239 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd);
240 return msvcrt_get_ioinfo(fd)->handle;
243 /* INTERNAL: free a file entry fd */
244 static void msvcrt_free_fd(int fd)
246 HANDLE old_handle;
247 ioinfo *fdinfo;
249 LOCK_FILES();
250 fdinfo = msvcrt_get_ioinfo(fd);
251 old_handle = fdinfo->handle;
252 if(fdinfo != &MSVCRT___badioinfo)
254 fdinfo->handle = INVALID_HANDLE_VALUE;
255 fdinfo->wxflag = 0;
257 TRACE(":fd (%d) freed\n",fd);
258 if (fd < 3) /* don't use 0,1,2 for user files */
260 switch (fd)
262 case 0:
263 if (GetStdHandle(STD_INPUT_HANDLE) == old_handle) SetStdHandle(STD_INPUT_HANDLE, 0);
264 break;
265 case 1:
266 if (GetStdHandle(STD_OUTPUT_HANDLE) == old_handle) SetStdHandle(STD_OUTPUT_HANDLE, 0);
267 break;
268 case 2:
269 if (GetStdHandle(STD_ERROR_HANDLE) == old_handle) SetStdHandle(STD_ERROR_HANDLE, 0);
270 break;
273 else
275 if (fd == MSVCRT_fdend - 1)
276 MSVCRT_fdend--;
277 if (fd < MSVCRT_fdstart)
278 MSVCRT_fdstart = fd;
280 UNLOCK_FILES();
283 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
284 /* caller must hold the files lock */
285 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
287 ioinfo *fdinfo;
289 if (fd >= MSVCRT_MAX_FILES)
291 WARN(":files exhausted!\n");
292 *MSVCRT__errno() = MSVCRT_ENFILE;
293 return -1;
296 fdinfo = msvcrt_get_ioinfo(fd);
297 if(fdinfo == &MSVCRT___badioinfo) {
298 int i;
300 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
301 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
302 WARN(":out of memory!\n");
303 *MSVCRT__errno() = MSVCRT_ENOMEM;
304 return -1;
307 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
308 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
310 fdinfo = msvcrt_get_ioinfo(fd);
313 fdinfo->handle = hand;
314 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
316 /* locate next free slot */
317 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
318 MSVCRT_fdstart = MSVCRT_fdend + 1;
319 else
320 while (MSVCRT_fdstart < MSVCRT_fdend &&
321 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
322 MSVCRT_fdstart++;
323 /* update last fd in use */
324 if (fd >= MSVCRT_fdend)
325 MSVCRT_fdend = fd + 1;
326 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
328 switch (fd)
330 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
331 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
332 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
335 return fd;
338 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
339 static int msvcrt_alloc_fd(HANDLE hand, int flag)
341 int ret;
343 LOCK_FILES();
344 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
345 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
346 UNLOCK_FILES();
347 return ret;
350 /* INTERNAL: Allocate a FILE* for an fd slot */
351 /* caller must hold the files lock */
352 static MSVCRT_FILE* msvcrt_alloc_fp(void)
354 unsigned int i;
355 MSVCRT_FILE *file;
357 for (i = 3; i < MSVCRT_max_streams; i++)
359 file = msvcrt_get_file(i);
360 if (!file)
361 return NULL;
363 if (file->_flag == 0)
365 if (i == MSVCRT_stream_idx)
367 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
369 InitializeCriticalSection(&((file_crit*)file)->crit);
370 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
372 MSVCRT_stream_idx++;
374 return file;
378 return NULL;
381 /* INTERNAL: initialize a FILE* from an open fd */
382 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
384 TRACE(":fd (%d) allocating FILE*\n",fd);
385 if (!msvcrt_is_valid_fd(fd))
387 WARN(":invalid fd %d\n",fd);
388 *MSVCRT___doserrno() = 0;
389 *MSVCRT__errno() = MSVCRT_EBADF;
390 return -1;
392 memset(file, 0, sizeof(*file));
393 file->_file = fd;
394 file->_flag = stream_flags;
396 TRACE(":got FILE* (%p)\n",file);
397 return 0;
400 /* INTERNAL: Create an inheritance data block (for spawned process)
401 * The inheritance block is made of:
402 * 00 int nb of file descriptor (NBFD)
403 * 04 char file flags (wxflag): repeated for each fd
404 * 4+NBFD HANDLE file handle: repeated for each fd
406 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
408 int fd;
409 char* wxflag_ptr;
410 HANDLE* handle_ptr;
411 ioinfo* fdinfo;
413 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
414 *block = MSVCRT_calloc(*size, 1);
415 if (!*block)
417 *size = 0;
418 return FALSE;
420 wxflag_ptr = (char*)*block + sizeof(unsigned);
421 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
423 *(unsigned*)*block = MSVCRT_fdend;
424 for (fd = 0; fd < MSVCRT_fdend; fd++)
426 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
427 fdinfo = msvcrt_get_ioinfo(fd);
428 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
430 *wxflag_ptr = fdinfo->wxflag;
431 *handle_ptr = fdinfo->handle;
433 else
435 *wxflag_ptr = 0;
436 *handle_ptr = INVALID_HANDLE_VALUE;
438 wxflag_ptr++; handle_ptr++;
440 return TRUE;
443 /* INTERNAL: Set up all file descriptors,
444 * as well as default streams (stdin, stderr and stdout)
446 void msvcrt_init_io(void)
448 STARTUPINFOA si;
449 int i;
450 ioinfo *fdinfo;
452 GetStartupInfoA(&si);
453 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
455 BYTE* wxflag_ptr;
456 HANDLE* handle_ptr;
457 unsigned int count;
459 count = *(unsigned*)si.lpReserved2;
460 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
461 handle_ptr = (HANDLE*)(wxflag_ptr + count);
463 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
464 count = min(count, MSVCRT_MAX_FILES);
465 for (i = 0; i < count; i++)
467 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
468 msvcrt_alloc_fd_from(*handle_ptr, *wxflag_ptr, i);
470 wxflag_ptr++; handle_ptr++;
472 MSVCRT_fdend = max( 3, count );
473 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
474 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
477 if(!MSVCRT___pioinfo[0])
478 msvcrt_alloc_fd_from(INVALID_HANDLE_VALUE, 0, 3);
480 fdinfo = msvcrt_get_ioinfo(0);
481 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
483 HANDLE std = GetStdHandle(STD_INPUT_HANDLE);
484 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
485 GetCurrentProcess(), &fdinfo->handle,
486 0, TRUE, DUPLICATE_SAME_ACCESS))
487 fdinfo->wxflag = WX_OPEN | WX_TEXT;
490 fdinfo = msvcrt_get_ioinfo(1);
491 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
493 HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
494 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
495 GetCurrentProcess(), &fdinfo->handle,
496 0, TRUE, DUPLICATE_SAME_ACCESS))
497 fdinfo->wxflag = WX_OPEN | WX_TEXT;
500 fdinfo = msvcrt_get_ioinfo(2);
501 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE)
503 HANDLE std = GetStdHandle(STD_ERROR_HANDLE);
504 if (std != INVALID_HANDLE_VALUE && DuplicateHandle(GetCurrentProcess(), std,
505 GetCurrentProcess(), &fdinfo->handle,
506 0, TRUE, DUPLICATE_SAME_ACCESS))
507 fdinfo->wxflag = WX_OPEN | WX_TEXT;
510 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(0)->handle,
511 msvcrt_get_ioinfo(1)->handle, msvcrt_get_ioinfo(2)->handle);
513 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
514 for (i = 0; i < 3; i++)
516 /* FILE structs for stdin/out/err are static and never deleted */
517 MSVCRT__iob[i]._file = i;
518 MSVCRT__iob[i]._tmpfname = NULL;
519 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
521 MSVCRT_stream_idx = 3;
524 /* INTERNAL: Flush stdio file buffer */
525 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
527 if(file->_bufsiz) {
528 int cnt=file->_ptr-file->_base;
529 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
530 file->_flag |= MSVCRT__IOERR;
531 return MSVCRT_EOF;
533 file->_ptr=file->_base;
534 file->_cnt=file->_bufsiz;
536 return 0;
539 /* INTERNAL: Allocate stdio file buffer */
540 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
542 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
543 if(file->_base) {
544 file->_bufsiz = MSVCRT_BUFSIZ;
545 file->_flag |= MSVCRT__IOMYBUF;
546 } else {
547 file->_base = (char*)(&file->_charbuf);
548 /* put here 2 ??? */
549 file->_bufsiz = sizeof(file->_charbuf);
551 file->_ptr = file->_base;
552 file->_cnt = 0;
555 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
556 static int msvcrt_int_to_base32(int num, char *str)
558 char *p;
559 int n = num;
560 int digits = 0;
562 while (n != 0)
564 n >>= 5;
565 digits++;
567 p = str + digits;
568 *p = 0;
569 while (--p >= str)
571 *p = (num & 31) + '0';
572 if (*p > '9')
573 *p += ('a' - '0' - 10);
574 num >>= 5;
577 return digits;
580 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
581 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
583 MSVCRT_wchar_t *p;
584 int n = num;
585 int digits = 0;
587 while (n != 0)
589 n >>= 5;
590 digits++;
592 p = str + digits;
593 *p = 0;
594 while (--p >= str)
596 *p = (num & 31) + '0';
597 if (*p > '9')
598 *p += ('a' - '0' - 10);
599 num >>= 5;
602 return digits;
605 /*********************************************************************
606 * __iob_func(MSVCRT.@)
608 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
610 return &MSVCRT__iob[0];
613 /*********************************************************************
614 * _access (MSVCRT.@)
616 int CDECL MSVCRT__access(const char *filename, int mode)
618 DWORD attr = GetFileAttributesA(filename);
620 TRACE("(%s,%d) %d\n",filename,mode,attr);
622 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
624 msvcrt_set_errno(GetLastError());
625 return -1;
627 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
629 msvcrt_set_errno(ERROR_ACCESS_DENIED);
630 return -1;
632 return 0;
635 /*********************************************************************
636 * _access_s (MSVCRT.@)
638 int CDECL _access_s(const char *filename, int mode)
640 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
641 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
643 *MSVCRT__errno() = MSVCRT_EINVAL;
644 return -1;
647 return MSVCRT__access(filename, mode);
650 /*********************************************************************
651 * _waccess (MSVCRT.@)
653 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
655 DWORD attr = GetFileAttributesW(filename);
657 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
659 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
661 msvcrt_set_errno(GetLastError());
662 return -1;
664 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
666 msvcrt_set_errno(ERROR_ACCESS_DENIED);
667 return -1;
669 return 0;
672 /*********************************************************************
673 * _waccess_s (MSVCRT.@)
675 int CDECL _waccess_s(const MSVCRT_wchar_t *filename, int mode)
677 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
678 !MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0))
680 *MSVCRT__errno() = MSVCRT_EINVAL;
681 return -1;
684 return MSVCRT__waccess(filename, mode);
687 /*********************************************************************
688 * _chmod (MSVCRT.@)
690 int CDECL MSVCRT__chmod(const char *path, int flags)
692 DWORD oldFlags = GetFileAttributesA(path);
694 if (oldFlags != INVALID_FILE_ATTRIBUTES)
696 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
697 oldFlags | FILE_ATTRIBUTE_READONLY;
699 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
700 return 0;
702 msvcrt_set_errno(GetLastError());
703 return -1;
706 /*********************************************************************
707 * _wchmod (MSVCRT.@)
709 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
711 DWORD oldFlags = GetFileAttributesW(path);
713 if (oldFlags != INVALID_FILE_ATTRIBUTES)
715 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
716 oldFlags | FILE_ATTRIBUTE_READONLY;
718 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
719 return 0;
721 msvcrt_set_errno(GetLastError());
722 return -1;
725 /*********************************************************************
726 * _unlink (MSVCRT.@)
728 int CDECL MSVCRT__unlink(const char *path)
730 TRACE("%s\n",debugstr_a(path));
731 if(DeleteFileA(path))
732 return 0;
733 TRACE("failed (%d)\n",GetLastError());
734 msvcrt_set_errno(GetLastError());
735 return -1;
738 /*********************************************************************
739 * _wunlink (MSVCRT.@)
741 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
743 TRACE("(%s)\n",debugstr_w(path));
744 if(DeleteFileW(path))
745 return 0;
746 TRACE("failed (%d)\n",GetLastError());
747 msvcrt_set_errno(GetLastError());
748 return -1;
751 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
752 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
754 /* INTERNAL: Flush all stream buffer */
755 static int msvcrt_flush_all_buffers(int mask)
757 int i, num_flushed = 0;
758 MSVCRT_FILE *file;
760 LOCK_FILES();
761 for (i = 3; i < MSVCRT_stream_idx; i++) {
762 file = msvcrt_get_file(i);
764 if (file->_flag)
766 if(file->_flag & mask) {
767 MSVCRT_fflush(file);
768 num_flushed++;
772 UNLOCK_FILES();
774 TRACE(":flushed (%d) handles\n",num_flushed);
775 return num_flushed;
778 /*********************************************************************
779 * _flushall (MSVCRT.@)
781 int CDECL MSVCRT__flushall(void)
783 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
786 /*********************************************************************
787 * fflush (MSVCRT.@)
789 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
791 if(!file) {
792 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
793 } else if(file->_flag & MSVCRT__IOWRT) {
794 int res;
796 MSVCRT__lock_file(file);
797 res = msvcrt_flush_buffer(file);
798 MSVCRT__unlock_file(file);
800 return res;
801 } else if(file->_flag & MSVCRT__IOREAD) {
802 MSVCRT__lock_file(file);
803 file->_cnt = 0;
804 file->_ptr = file->_base;
805 MSVCRT__unlock_file(file);
807 return 0;
809 return 0;
812 /*********************************************************************
813 * _close (MSVCRT.@)
815 int CDECL MSVCRT__close(int fd)
817 HANDLE hand;
818 int ret;
820 LOCK_FILES();
821 hand = msvcrt_fdtoh(fd);
822 TRACE(":fd (%d) handle (%p)\n",fd,hand);
823 if (!msvcrt_is_valid_fd(fd)) {
824 ret = -1;
825 } else {
826 msvcrt_free_fd(fd);
827 ret = CloseHandle(hand) ? 0 : -1;
828 if (ret) {
829 WARN(":failed-last error (%d)\n",GetLastError());
830 msvcrt_set_errno(GetLastError());
833 UNLOCK_FILES();
834 TRACE(":ok\n");
835 return ret;
838 /*********************************************************************
839 * _commit (MSVCRT.@)
841 int CDECL MSVCRT__commit(int fd)
843 HANDLE hand = msvcrt_fdtoh(fd);
845 TRACE(":fd (%d) handle (%p)\n",fd,hand);
846 if (hand == INVALID_HANDLE_VALUE)
847 return -1;
849 if (!FlushFileBuffers(hand))
851 if (GetLastError() == ERROR_INVALID_HANDLE)
853 /* FlushFileBuffers fails for console handles
854 * so we ignore this error.
856 return 0;
858 TRACE(":failed-last error (%d)\n",GetLastError());
859 msvcrt_set_errno(GetLastError());
860 return -1;
862 TRACE(":ok\n");
863 return 0;
866 /*********************************************************************
867 * _dup2 (MSVCRT.@)
868 * NOTES
869 * MSDN isn't clear on this point, but the remarks for _pipe
870 * indicate file descriptors duplicated with _dup and _dup2 are always
871 * inheritable.
873 int CDECL MSVCRT__dup2(int od, int nd)
875 int ret;
877 TRACE("(od=%d, nd=%d)\n", od, nd);
878 LOCK_FILES();
879 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
881 HANDLE handle;
883 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
884 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
886 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
888 if (msvcrt_is_valid_fd(nd))
889 MSVCRT__close(nd);
890 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
891 if (ret == -1)
893 CloseHandle(handle);
894 *MSVCRT__errno() = MSVCRT_EMFILE;
896 else
898 /* _dup2 returns 0, not nd, on success */
899 ret = 0;
902 else
904 ret = -1;
905 msvcrt_set_errno(GetLastError());
908 else
910 *MSVCRT__errno() = MSVCRT_EBADF;
911 ret = -1;
913 UNLOCK_FILES();
914 return ret;
917 /*********************************************************************
918 * _dup (MSVCRT.@)
920 int CDECL MSVCRT__dup(int od)
922 int fd, ret;
924 LOCK_FILES();
925 fd = MSVCRT_fdstart;
926 if (MSVCRT__dup2(od, fd) == 0)
927 ret = fd;
928 else
929 ret = -1;
930 UNLOCK_FILES();
931 return ret;
934 /*********************************************************************
935 * _eof (MSVCRT.@)
937 int CDECL MSVCRT__eof(int fd)
939 DWORD curpos,endpos;
940 LONG hcurpos,hendpos;
941 HANDLE hand = msvcrt_fdtoh(fd);
943 TRACE(":fd (%d) handle (%p)\n",fd,hand);
945 if (hand == INVALID_HANDLE_VALUE)
946 return -1;
948 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
950 /* Otherwise we do it the hard way */
951 hcurpos = hendpos = 0;
952 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
953 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
955 if (curpos == endpos && hcurpos == hendpos)
957 /* FIXME: shouldn't WX_ATEOF be set here? */
958 return TRUE;
961 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
962 return FALSE;
965 /*********************************************************************
966 * _fcloseall (MSVCRT.@)
968 int CDECL MSVCRT__fcloseall(void)
970 int num_closed = 0, i;
971 MSVCRT_FILE *file;
973 LOCK_FILES();
974 for (i = 3; i < MSVCRT_stream_idx; i++) {
975 file = msvcrt_get_file(i);
977 if (file->_flag && !MSVCRT_fclose(file))
978 num_closed++;
980 UNLOCK_FILES();
982 TRACE(":closed (%d) handles\n",num_closed);
983 return num_closed;
986 /* free everything on process exit */
987 void msvcrt_free_io(void)
989 int i;
991 MSVCRT__fcloseall();
992 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
993 * stdout, and stderr (unlike GNU), so we need to fclose() them here
994 * or they won't get flushed.
996 MSVCRT_fclose(&MSVCRT__iob[0]);
997 MSVCRT_fclose(&MSVCRT__iob[1]);
998 MSVCRT_fclose(&MSVCRT__iob[2]);
1000 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1001 MSVCRT_free(MSVCRT___pioinfo[i]);
1003 for(i=0; i<MSVCRT_stream_idx; i++)
1005 MSVCRT_FILE *file = msvcrt_get_file(i);
1006 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1008 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1009 DeleteCriticalSection(&((file_crit*)file)->crit);
1013 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1014 MSVCRT_free(MSVCRT_fstream[i]);
1016 DeleteCriticalSection(&MSVCRT_file_cs);
1019 /*********************************************************************
1020 * _lseeki64 (MSVCRT.@)
1022 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1024 HANDLE hand = msvcrt_fdtoh(fd);
1025 LARGE_INTEGER ofs;
1027 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1028 if (hand == INVALID_HANDLE_VALUE)
1029 return -1;
1031 if (whence < 0 || whence > 2)
1033 *MSVCRT__errno() = MSVCRT_EINVAL;
1034 return -1;
1037 TRACE(":fd (%d) to %s pos %s\n",
1038 fd,wine_dbgstr_longlong(offset),
1039 (whence==SEEK_SET)?"SEEK_SET":
1040 (whence==SEEK_CUR)?"SEEK_CUR":
1041 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1043 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1044 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1045 ofs.QuadPart = offset;
1046 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1047 GetLastError() == ERROR_SUCCESS)
1049 msvcrt_get_ioinfo(fd)->wxflag &= ~(WX_ATEOF|WX_READEOF);
1050 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1052 return ofs.QuadPart;
1054 TRACE(":error-last error (%d)\n",GetLastError());
1055 msvcrt_set_errno(GetLastError());
1056 return -1;
1059 /*********************************************************************
1060 * _lseek (MSVCRT.@)
1062 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1064 return MSVCRT__lseeki64(fd, offset, whence);
1067 /*********************************************************************
1068 * _lock_file (MSVCRT.@)
1070 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1072 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1073 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1074 else
1075 EnterCriticalSection(&((file_crit*)file)->crit);
1078 /*********************************************************************
1079 * _unlock_file (MSVCRT.@)
1081 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1083 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1084 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1085 else
1086 LeaveCriticalSection(&((file_crit*)file)->crit);
1089 /*********************************************************************
1090 * _locking (MSVCRT.@)
1092 * This is untested; the underlying LockFile doesn't work yet.
1094 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1096 BOOL ret;
1097 DWORD cur_locn;
1098 HANDLE hand = msvcrt_fdtoh(fd);
1100 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1101 if (hand == INVALID_HANDLE_VALUE)
1102 return -1;
1104 if (mode < 0 || mode > 4)
1106 *MSVCRT__errno() = MSVCRT_EINVAL;
1107 return -1;
1110 TRACE(":fd (%d) by 0x%08x mode %s\n",
1111 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1112 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1113 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1114 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1115 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1116 "UNKNOWN");
1118 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1120 FIXME ("Seek failed\n");
1121 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1122 return -1;
1124 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1126 int nretry = 10;
1127 ret = 1; /* just to satisfy gcc */
1128 while (nretry--)
1130 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1131 if (ret) break;
1132 Sleep(1);
1135 else if (mode == MSVCRT__LK_UNLCK)
1136 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1137 else
1138 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1139 /* FIXME - what about error settings? */
1140 return ret ? 0 : -1;
1143 /*********************************************************************
1144 * _fseeki64 (MSVCRT.@)
1146 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1148 int ret;
1150 MSVCRT__lock_file(file);
1151 /* Flush output if needed */
1152 if(file->_flag & MSVCRT__IOWRT)
1153 msvcrt_flush_buffer(file);
1155 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1156 offset -= file->_cnt;
1157 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
1158 /* Black magic correction for CR removal */
1159 int i;
1160 for (i=0; i<file->_cnt; i++) {
1161 if (file->_ptr[i] == '\n')
1162 offset--;
1164 /* Black magic when reading CR at buffer boundary*/
1165 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
1166 offset--;
1169 /* Discard buffered input */
1170 file->_cnt = 0;
1171 file->_ptr = file->_base;
1172 /* Reset direction of i/o */
1173 if(file->_flag & MSVCRT__IORW) {
1174 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1176 /* Clear end of file flag */
1177 file->_flag &= ~MSVCRT__IOEOF;
1178 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1180 MSVCRT__unlock_file(file);
1181 return ret;
1184 /*********************************************************************
1185 * fseek (MSVCRT.@)
1187 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1189 return MSVCRT__fseeki64( file, offset, whence );
1192 /*********************************************************************
1193 * _chsize (MSVCRT.@)
1195 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1197 LONG cur, pos;
1198 HANDLE handle;
1199 BOOL ret = FALSE;
1201 TRACE("(fd=%d, size=%d)\n", fd, size);
1203 LOCK_FILES();
1205 handle = msvcrt_fdtoh(fd);
1206 if (handle != INVALID_HANDLE_VALUE)
1208 /* save the current file pointer */
1209 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
1210 if (cur >= 0)
1212 pos = MSVCRT__lseek(fd, size, SEEK_SET);
1213 if (pos >= 0)
1215 ret = SetEndOfFile(handle);
1216 if (!ret) msvcrt_set_errno(GetLastError());
1219 /* restore the file pointer */
1220 MSVCRT__lseek(fd, cur, SEEK_SET);
1224 UNLOCK_FILES();
1225 return ret ? 0 : -1;
1228 /*********************************************************************
1229 * clearerr (MSVCRT.@)
1231 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1233 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1235 MSVCRT__lock_file(file);
1236 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1237 MSVCRT__unlock_file(file);
1240 /*********************************************************************
1241 * rewind (MSVCRT.@)
1243 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1245 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1247 MSVCRT__lock_file(file);
1248 MSVCRT_fseek(file, 0L, SEEK_SET);
1249 MSVCRT_clearerr(file);
1250 MSVCRT__unlock_file(file);
1253 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1255 int plus = strchrW(mode, '+') != NULL;
1257 switch(*mode++)
1259 case 'R': case 'r':
1260 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1261 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1262 break;
1263 case 'W': case 'w':
1264 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1265 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1266 break;
1267 case 'A': case 'a':
1268 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1269 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1270 break;
1271 default:
1272 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1273 *MSVCRT__errno() = MSVCRT_EINVAL;
1274 return -1;
1277 while (*mode)
1278 switch (*mode++)
1280 case 'B': case 'b':
1281 *open_flags |= MSVCRT__O_BINARY;
1282 *open_flags &= ~MSVCRT__O_TEXT;
1283 break;
1284 case 't':
1285 *open_flags |= MSVCRT__O_TEXT;
1286 *open_flags &= ~MSVCRT__O_BINARY;
1287 break;
1288 case 'D':
1289 *open_flags |= MSVCRT__O_TEMPORARY;
1290 break;
1291 case 'T':
1292 *open_flags |= MSVCRT__O_SHORT_LIVED;
1293 break;
1294 case '+':
1295 case ' ':
1296 break;
1297 default:
1298 FIXME(":unknown flag %c not supported\n",mode[-1]);
1300 return 0;
1303 /*********************************************************************
1304 * _fdopen (MSVCRT.@)
1306 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1308 MSVCRT_FILE *ret;
1309 MSVCRT_wchar_t *modeW = NULL;
1311 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1313 ret = MSVCRT__wfdopen(fd, modeW);
1315 MSVCRT_free(modeW);
1316 return ret;
1319 /*********************************************************************
1320 * _wfdopen (MSVCRT.@)
1322 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1324 int open_flags, stream_flags;
1325 MSVCRT_FILE* file;
1327 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1329 LOCK_FILES();
1330 if (!(file = msvcrt_alloc_fp()))
1331 file = NULL;
1332 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1334 file->_flag = 0;
1335 file = NULL;
1337 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1338 UNLOCK_FILES();
1340 return file;
1343 /*********************************************************************
1344 * _filelength (MSVCRT.@)
1346 LONG CDECL MSVCRT__filelength(int fd)
1348 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1349 if (curPos != -1)
1351 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1352 if (endPos != -1)
1354 if (endPos != curPos)
1355 MSVCRT__lseek(fd, curPos, SEEK_SET);
1356 return endPos;
1359 return -1;
1362 /*********************************************************************
1363 * _filelengthi64 (MSVCRT.@)
1365 __int64 CDECL MSVCRT__filelengthi64(int fd)
1367 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1368 if (curPos != -1)
1370 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1371 if (endPos != -1)
1373 if (endPos != curPos)
1374 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1375 return endPos;
1378 return -1;
1381 /*********************************************************************
1382 * _fileno (MSVCRT.@)
1384 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1386 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1387 return file->_file;
1390 /*********************************************************************
1391 * _fstat64 (MSVCRT.@)
1393 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1395 DWORD dw;
1396 DWORD type;
1397 BY_HANDLE_FILE_INFORMATION hfi;
1398 HANDLE hand = msvcrt_fdtoh(fd);
1400 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1401 if (hand == INVALID_HANDLE_VALUE)
1402 return -1;
1404 if (!buf)
1406 WARN(":failed-NULL buf\n");
1407 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1408 return -1;
1411 memset(&hfi, 0, sizeof(hfi));
1412 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1413 type = GetFileType(hand);
1414 if (type == FILE_TYPE_PIPE)
1416 buf->st_dev = buf->st_rdev = fd;
1417 buf->st_mode = S_IFIFO;
1418 buf->st_nlink = 1;
1420 else if (type == FILE_TYPE_CHAR)
1422 buf->st_dev = buf->st_rdev = fd;
1423 buf->st_mode = S_IFCHR;
1424 buf->st_nlink = 1;
1426 else /* FILE_TYPE_DISK etc. */
1428 if (!GetFileInformationByHandle(hand, &hfi))
1430 WARN(":failed-last error (%d)\n",GetLastError());
1431 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1432 return -1;
1434 buf->st_mode = S_IFREG | 0444;
1435 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1436 buf->st_mode |= 0222;
1437 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1438 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1439 buf->st_atime = dw;
1440 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1441 buf->st_mtime = buf->st_ctime = dw;
1442 buf->st_nlink = hfi.nNumberOfLinks;
1444 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1445 buf->st_mode);
1446 return 0;
1449 /*********************************************************************
1450 * _fstati64 (MSVCRT.@)
1452 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1454 int ret;
1455 struct MSVCRT__stat64 buf64;
1457 ret = MSVCRT__fstat64(fd, &buf64);
1458 if (!ret)
1459 msvcrt_stat64_to_stati64(&buf64, buf);
1460 return ret;
1463 /*********************************************************************
1464 * _fstat (MSVCRT.@)
1466 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1467 { int ret;
1468 struct MSVCRT__stat64 buf64;
1470 ret = MSVCRT__fstat64(fd, &buf64);
1471 if (!ret)
1472 msvcrt_stat64_to_stat(&buf64, buf);
1473 return ret;
1476 /*********************************************************************
1477 * _futime64 (MSVCRT.@)
1479 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1481 HANDLE hand = msvcrt_fdtoh(fd);
1482 FILETIME at, wt;
1484 if (!t)
1486 time_to_filetime( MSVCRT__time64(NULL), &at );
1487 wt = at;
1489 else
1491 time_to_filetime( t->actime, &at );
1492 time_to_filetime( t->modtime, &wt );
1495 if (!SetFileTime(hand, NULL, &at, &wt))
1497 msvcrt_set_errno(GetLastError());
1498 return -1 ;
1500 return 0;
1503 /*********************************************************************
1504 * _futime32 (MSVCRT.@)
1506 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1508 if (t)
1510 struct MSVCRT___utimbuf64 t64;
1511 t64.actime = t->actime;
1512 t64.modtime = t->modtime;
1513 return _futime64( fd, &t64 );
1515 else
1516 return _futime64( fd, NULL );
1519 /*********************************************************************
1520 * _futime (MSVCRT.@)
1522 #ifdef _WIN64
1523 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1525 return _futime64( fd, t );
1527 #else
1528 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1530 return _futime32( fd, t );
1532 #endif
1534 /*********************************************************************
1535 * _get_osfhandle (MSVCRT.@)
1537 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1539 HANDLE hand = msvcrt_fdtoh(fd);
1540 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1542 return (MSVCRT_intptr_t)hand;
1545 /*********************************************************************
1546 * _isatty (MSVCRT.@)
1548 int CDECL MSVCRT__isatty(int fd)
1550 HANDLE hand = msvcrt_fdtoh(fd);
1552 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1553 if (hand == INVALID_HANDLE_VALUE)
1554 return 0;
1556 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1559 /*********************************************************************
1560 * _mktemp (MSVCRT.@)
1562 char * CDECL MSVCRT__mktemp(char *pattern)
1564 int numX = 0;
1565 char *retVal = pattern;
1566 int id;
1567 char letter = 'a';
1569 while(*pattern)
1570 numX = (*pattern++ == 'X')? numX + 1 : 0;
1571 if (numX < 5)
1572 return NULL;
1573 pattern--;
1574 id = GetCurrentProcessId();
1575 numX = 6;
1576 while(numX--)
1578 int tempNum = id / 10;
1579 *pattern-- = id - (tempNum * 10) + '0';
1580 id = tempNum;
1582 pattern++;
1585 *pattern = letter++;
1586 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1587 GetLastError() == ERROR_FILE_NOT_FOUND)
1588 return retVal;
1589 } while(letter <= 'z');
1590 return NULL;
1593 /*********************************************************************
1594 * _wmktemp (MSVCRT.@)
1596 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1598 int numX = 0;
1599 MSVCRT_wchar_t *retVal = pattern;
1600 int id;
1601 MSVCRT_wchar_t letter = 'a';
1603 while(*pattern)
1604 numX = (*pattern++ == 'X')? numX + 1 : 0;
1605 if (numX < 5)
1606 return NULL;
1607 pattern--;
1608 id = GetCurrentProcessId();
1609 numX = 6;
1610 while(numX--)
1612 int tempNum = id / 10;
1613 *pattern-- = id - (tempNum * 10) + '0';
1614 id = tempNum;
1616 pattern++;
1619 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1620 GetLastError() == ERROR_FILE_NOT_FOUND)
1621 return retVal;
1622 *pattern = letter++;
1623 } while(letter != '|');
1624 return NULL;
1627 static unsigned split_oflags(unsigned oflags)
1629 int wxflags = 0;
1630 unsigned unsupp; /* until we support everything */
1632 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1633 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1634 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1635 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1636 else wxflags |= WX_TEXT; /* default to TEXT*/
1637 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1639 if ((unsupp = oflags & ~(
1640 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1641 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1642 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1643 MSVCRT__O_NOINHERIT|
1644 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1646 ERR(":unsupported oflags 0x%04x\n",unsupp);
1648 return wxflags;
1651 /*********************************************************************
1652 * _pipe (MSVCRT.@)
1654 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1656 int ret = -1;
1657 SECURITY_ATTRIBUTES sa;
1658 HANDLE readHandle, writeHandle;
1660 if (!pfds)
1662 *MSVCRT__errno() = MSVCRT_EINVAL;
1663 return -1;
1666 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1667 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1668 sa.lpSecurityDescriptor = NULL;
1669 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1671 unsigned int wxflags = split_oflags(textmode);
1672 int fd;
1674 LOCK_FILES();
1675 fd = msvcrt_alloc_fd(readHandle, wxflags);
1676 if (fd != -1)
1678 pfds[0] = fd;
1679 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1680 if (fd != -1)
1682 pfds[1] = fd;
1683 ret = 0;
1685 else
1687 MSVCRT__close(pfds[0]);
1688 CloseHandle(writeHandle);
1689 *MSVCRT__errno() = MSVCRT_EMFILE;
1692 else
1694 CloseHandle(readHandle);
1695 CloseHandle(writeHandle);
1696 *MSVCRT__errno() = MSVCRT_EMFILE;
1698 UNLOCK_FILES();
1700 else
1701 msvcrt_set_errno(GetLastError());
1703 return ret;
1706 /*********************************************************************
1707 * _sopen_s (MSVCRT.@)
1709 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
1711 DWORD access = 0, creation = 0, attrib;
1712 DWORD sharing;
1713 int wxflag;
1714 HANDLE hand;
1715 SECURITY_ATTRIBUTES sa;
1717 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1718 fd, path, oflags, shflags, pmode);
1720 if (!fd)
1722 MSVCRT_INVALID_PMT("null out fd pointer");
1723 *MSVCRT__errno() = MSVCRT_EINVAL;
1724 return MSVCRT_EINVAL;
1727 *fd = -1;
1728 wxflag = split_oflags(oflags);
1729 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1731 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1732 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1733 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1736 if (oflags & MSVCRT__O_CREAT)
1738 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1739 FIXME(": pmode 0x%04x ignored\n", pmode);
1740 else
1741 WARN(": pmode 0x%04x ignored\n", pmode);
1743 if (oflags & MSVCRT__O_EXCL)
1744 creation = CREATE_NEW;
1745 else if (oflags & MSVCRT__O_TRUNC)
1746 creation = CREATE_ALWAYS;
1747 else
1748 creation = OPEN_ALWAYS;
1750 else /* no MSVCRT__O_CREAT */
1752 if (oflags & MSVCRT__O_TRUNC)
1753 creation = TRUNCATE_EXISTING;
1754 else
1755 creation = OPEN_EXISTING;
1758 switch( shflags )
1760 case MSVCRT__SH_DENYRW:
1761 sharing = 0L;
1762 break;
1763 case MSVCRT__SH_DENYWR:
1764 sharing = FILE_SHARE_READ;
1765 break;
1766 case MSVCRT__SH_DENYRD:
1767 sharing = FILE_SHARE_WRITE;
1768 break;
1769 case MSVCRT__SH_DENYNO:
1770 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1771 break;
1772 default:
1773 ERR( "Unhandled shflags 0x%x\n", shflags );
1774 return MSVCRT_EINVAL;
1776 attrib = FILE_ATTRIBUTE_NORMAL;
1778 if (oflags & MSVCRT__O_TEMPORARY)
1780 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1781 access |= DELETE;
1782 sharing |= FILE_SHARE_DELETE;
1785 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1786 sa.lpSecurityDescriptor = NULL;
1787 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1789 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1790 if (hand == INVALID_HANDLE_VALUE) {
1791 WARN(":failed-last error (%d)\n", GetLastError());
1792 msvcrt_set_errno(GetLastError());
1793 return *MSVCRT__errno();
1796 *fd = msvcrt_alloc_fd(hand, wxflag);
1798 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1799 return 0;
1802 /*********************************************************************
1803 * _sopen (MSVCRT.@)
1805 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1807 int pmode;
1808 int fd;
1810 if (oflags & MSVCRT__O_CREAT)
1812 __ms_va_list ap;
1814 __ms_va_start(ap, shflags);
1815 pmode = va_arg(ap, int);
1816 __ms_va_end(ap);
1818 else
1819 pmode = 0;
1821 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
1822 return fd;
1825 /*********************************************************************
1826 * _wsopen_s (MSVCRT.@)
1828 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
1830 DWORD access = 0, creation = 0, attrib;
1831 SECURITY_ATTRIBUTES sa;
1832 DWORD sharing;
1833 int wxflag;
1834 HANDLE hand;
1836 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1837 fd, debugstr_w(path), oflags, shflags, pmode);
1839 if (!fd)
1841 MSVCRT_INVALID_PMT("null out fd pointer");
1842 *MSVCRT__errno() = MSVCRT_EINVAL;
1843 return MSVCRT_EINVAL;
1846 *fd = -1;
1847 wxflag = split_oflags(oflags);
1848 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1850 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1851 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1852 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1855 if (oflags & MSVCRT__O_CREAT)
1857 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1858 FIXME(": pmode 0x%04x ignored\n", pmode);
1859 else
1860 WARN(": pmode 0x%04x ignored\n", pmode);
1862 if (oflags & MSVCRT__O_EXCL)
1863 creation = CREATE_NEW;
1864 else if (oflags & MSVCRT__O_TRUNC)
1865 creation = CREATE_ALWAYS;
1866 else
1867 creation = OPEN_ALWAYS;
1869 else /* no MSVCRT__O_CREAT */
1871 if (oflags & MSVCRT__O_TRUNC)
1872 creation = TRUNCATE_EXISTING;
1873 else
1874 creation = OPEN_EXISTING;
1877 switch( shflags )
1879 case MSVCRT__SH_DENYRW:
1880 sharing = 0L;
1881 break;
1882 case MSVCRT__SH_DENYWR:
1883 sharing = FILE_SHARE_READ;
1884 break;
1885 case MSVCRT__SH_DENYRD:
1886 sharing = FILE_SHARE_WRITE;
1887 break;
1888 case MSVCRT__SH_DENYNO:
1889 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1890 break;
1891 default:
1892 ERR( "Unhandled shflags 0x%x\n", shflags );
1893 return MSVCRT_EINVAL;
1895 attrib = FILE_ATTRIBUTE_NORMAL;
1897 if (oflags & MSVCRT__O_TEMPORARY)
1899 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1900 access |= DELETE;
1901 sharing |= FILE_SHARE_DELETE;
1904 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1905 sa.lpSecurityDescriptor = NULL;
1906 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1908 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1910 if (hand == INVALID_HANDLE_VALUE) {
1911 WARN(":failed-last error (%d)\n",GetLastError());
1912 msvcrt_set_errno(GetLastError());
1913 msvcrt_set_errno(GetLastError());
1914 return *MSVCRT__errno();
1917 *fd = msvcrt_alloc_fd(hand, wxflag);
1919 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1920 return 0;
1923 /*********************************************************************
1924 * _wsopen (MSVCRT.@)
1926 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
1928 int pmode;
1929 int fd;
1931 if (oflags & MSVCRT__O_CREAT)
1933 __ms_va_list ap;
1935 __ms_va_start(ap, shflags);
1936 pmode = va_arg(ap, int);
1937 __ms_va_end(ap);
1939 else
1940 pmode = 0;
1942 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
1943 return fd;
1946 /*********************************************************************
1947 * _open (MSVCRT.@)
1949 int CDECL MSVCRT__open( const char *path, int flags, ... )
1951 __ms_va_list ap;
1953 if (flags & MSVCRT__O_CREAT)
1955 int pmode;
1956 __ms_va_start(ap, flags);
1957 pmode = va_arg(ap, int);
1958 __ms_va_end(ap);
1959 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1961 else
1962 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1965 /*********************************************************************
1966 * _wopen (MSVCRT.@)
1968 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
1970 __ms_va_list ap;
1972 if (flags & MSVCRT__O_CREAT)
1974 int pmode;
1975 __ms_va_start(ap, flags);
1976 pmode = va_arg(ap, int);
1977 __ms_va_end(ap);
1978 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1980 else
1981 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
1984 /*********************************************************************
1985 * _creat (MSVCRT.@)
1987 int CDECL MSVCRT__creat(const char *path, int flags)
1989 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1990 return MSVCRT__open(path, usedFlags);
1993 /*********************************************************************
1994 * _wcreat (MSVCRT.@)
1996 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
1998 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1999 return MSVCRT__wopen(path, usedFlags);
2002 /*********************************************************************
2003 * _open_osfhandle (MSVCRT.@)
2005 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2007 int fd;
2009 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2010 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2011 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2012 * text - it never sets MSVCRT__O_BINARY.
2014 /* don't let split_oflags() decide the mode if no mode is passed */
2015 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2016 oflags |= MSVCRT__O_BINARY;
2018 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
2019 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
2020 return fd;
2023 /*********************************************************************
2024 * _rmtmp (MSVCRT.@)
2026 int CDECL MSVCRT__rmtmp(void)
2028 int num_removed = 0, i;
2029 MSVCRT_FILE *file;
2031 LOCK_FILES();
2032 for (i = 3; i < MSVCRT_stream_idx; i++) {
2033 file = msvcrt_get_file(i);
2035 if (file->_tmpfname)
2037 MSVCRT_fclose(file);
2038 num_removed++;
2041 UNLOCK_FILES();
2043 if (num_removed)
2044 TRACE(":removed (%d) temp files\n",num_removed);
2045 return num_removed;
2048 /*********************************************************************
2049 * (internal) read_i
2051 * When reading \r as last character in text mode, read() positions
2052 * the file pointer on the \r character while getc() goes on to
2053 * the following \n
2055 static int read_i(int fd, void *buf, unsigned int count)
2057 DWORD num_read;
2058 char *bufstart = buf;
2059 HANDLE hand = msvcrt_fdtoh(fd);
2060 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2062 if (count == 0)
2063 return 0;
2065 if (fdinfo->wxflag & WX_READEOF) {
2066 fdinfo->wxflag |= WX_ATEOF;
2067 TRACE("already at EOF, returning 0\n");
2068 return 0;
2070 /* Don't trace small reads, it gets *very* annoying */
2071 if (count > 4)
2072 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2073 if (hand == INVALID_HANDLE_VALUE)
2074 return -1;
2076 /* Reading single bytes in O_TEXT mode makes things slow
2077 * So read big chunks
2079 if (ReadFile(hand, bufstart, count, &num_read, NULL))
2081 if (count != 0 && num_read == 0)
2083 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2084 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2086 else if (fdinfo->wxflag & WX_TEXT)
2088 DWORD i, j;
2089 if (bufstart[num_read-1] == '\r')
2091 if(count == 1)
2093 fdinfo->wxflag &= ~WX_READCR;
2094 ReadFile(hand, bufstart, 1, &num_read, NULL);
2096 else
2098 fdinfo->wxflag |= WX_READCR;
2099 num_read--;
2102 else
2103 fdinfo->wxflag &= ~WX_READCR;
2104 for (i=0, j=0; i<num_read; i++)
2106 /* in text mode, a ctrl-z signals EOF */
2107 if (bufstart[i] == 0x1a)
2109 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2110 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2111 break;
2113 /* in text mode, strip \r if followed by \n.
2114 * BUG: should save state across calls somehow, so CR LF that
2115 * straddles buffer boundary gets recognized properly?
2117 if ((bufstart[i] != '\r')
2118 || ((i+1) < num_read && bufstart[i+1] != '\n'))
2119 bufstart[j++] = bufstart[i];
2121 num_read = j;
2124 else
2126 if (GetLastError() == ERROR_BROKEN_PIPE)
2128 TRACE(":end-of-pipe\n");
2129 fdinfo->wxflag |= (WX_ATEOF|WX_READEOF);
2130 return 0;
2132 else
2134 TRACE(":failed-last error (%d)\n",GetLastError());
2135 return -1;
2139 if (count > 4)
2140 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2141 return num_read;
2144 /*********************************************************************
2145 * _read (MSVCRT.@)
2147 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2149 int num_read;
2150 num_read = read_i(fd, buf, count);
2151 return num_read;
2154 /*********************************************************************
2155 * _setmode (MSVCRT.@)
2157 int CDECL MSVCRT__setmode(int fd,int mode)
2159 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2160 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
2161 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
2162 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
2163 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2164 else
2165 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2166 return ret;
2169 /*********************************************************************
2170 * _stat64 (MSVCRT.@)
2172 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2174 DWORD dw;
2175 WIN32_FILE_ATTRIBUTE_DATA hfi;
2176 unsigned short mode = ALL_S_IREAD;
2177 int plen;
2179 TRACE(":file (%s) buf(%p)\n",path,buf);
2181 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2183 TRACE("failed (%d)\n",GetLastError());
2184 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2185 return -1;
2188 memset(buf,0,sizeof(struct MSVCRT__stat64));
2190 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2191 Bon 011120: This FIXME seems incorrect
2192 Also a letter as first char isn't enough to be classified
2193 as a drive letter
2195 if (isalpha(*path)&& (*(path+1)==':'))
2196 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2197 else
2198 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2200 plen = strlen(path);
2202 /* Dir, or regular file? */
2203 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2204 (path[plen-1] == '\\'))
2205 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2206 else
2208 mode |= MSVCRT__S_IFREG;
2209 /* executable? */
2210 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2212 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2213 (tolower(path[plen-3]) << 16);
2214 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2215 mode |= ALL_S_IEXEC;
2219 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2220 mode |= ALL_S_IWRITE;
2222 buf->st_mode = mode;
2223 buf->st_nlink = 1;
2224 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2225 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2226 buf->st_atime = dw;
2227 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2228 buf->st_mtime = buf->st_ctime = dw;
2229 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2230 (int)(buf->st_size >> 32),(int)buf->st_size,
2231 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2232 return 0;
2235 /*********************************************************************
2236 * _stati64 (MSVCRT.@)
2238 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2240 int ret;
2241 struct MSVCRT__stat64 buf64;
2243 ret = MSVCRT_stat64(path, &buf64);
2244 if (!ret)
2245 msvcrt_stat64_to_stati64(&buf64, buf);
2246 return ret;
2249 /*********************************************************************
2250 * _stat (MSVCRT.@)
2252 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2253 { int ret;
2254 struct MSVCRT__stat64 buf64;
2256 ret = MSVCRT_stat64( path, &buf64);
2257 if (!ret)
2258 msvcrt_stat64_to_stat(&buf64, buf);
2259 return ret;
2262 /*********************************************************************
2263 * _wstat64 (MSVCRT.@)
2265 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2267 DWORD dw;
2268 WIN32_FILE_ATTRIBUTE_DATA hfi;
2269 unsigned short mode = ALL_S_IREAD;
2270 int plen;
2272 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2274 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2276 TRACE("failed (%d)\n",GetLastError());
2277 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
2278 return -1;
2281 memset(buf,0,sizeof(struct MSVCRT__stat64));
2283 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2284 if (MSVCRT_iswalpha(*path))
2285 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2286 else
2287 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2289 plen = strlenW(path);
2291 /* Dir, or regular file? */
2292 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
2293 (path[plen-1] == '\\'))
2294 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2295 else
2297 mode |= MSVCRT__S_IFREG;
2298 /* executable? */
2299 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2301 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2302 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2303 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2304 mode |= ALL_S_IEXEC;
2308 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2309 mode |= ALL_S_IWRITE;
2311 buf->st_mode = mode;
2312 buf->st_nlink = 1;
2313 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2314 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2315 buf->st_atime = dw;
2316 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2317 buf->st_mtime = buf->st_ctime = dw;
2318 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2319 (int)(buf->st_size >> 32),(int)buf->st_size,
2320 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2321 return 0;
2324 /*********************************************************************
2325 * _wstati64 (MSVCRT.@)
2327 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2329 int ret;
2330 struct MSVCRT__stat64 buf64;
2332 ret = MSVCRT__wstat64(path, &buf64);
2333 if (!ret)
2334 msvcrt_stat64_to_stati64(&buf64, buf);
2335 return ret;
2338 /*********************************************************************
2339 * _wstat (MSVCRT.@)
2341 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2343 int ret;
2344 struct MSVCRT__stat64 buf64;
2346 ret = MSVCRT__wstat64( path, &buf64 );
2347 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2348 return ret;
2351 /*********************************************************************
2352 * _tell (MSVCRT.@)
2354 MSVCRT_long CDECL MSVCRT__tell(int fd)
2356 return MSVCRT__lseek(fd, 0, SEEK_CUR);
2359 /*********************************************************************
2360 * _telli64 (MSVCRT.@)
2362 __int64 CDECL _telli64(int fd)
2364 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
2367 /*********************************************************************
2368 * _tempnam (MSVCRT.@)
2370 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
2372 char tmpbuf[MAX_PATH];
2373 const char *tmp_dir = MSVCRT_getenv("TMP");
2375 if (tmp_dir) dir = tmp_dir;
2377 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2378 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2380 TRACE("got name (%s)\n",tmpbuf);
2381 DeleteFileA(tmpbuf);
2382 return MSVCRT__strdup(tmpbuf);
2384 TRACE("failed (%d)\n",GetLastError());
2385 return NULL;
2388 /*********************************************************************
2389 * _wtempnam (MSVCRT.@)
2391 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
2393 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
2394 MSVCRT_wchar_t tmpbuf[MAX_PATH];
2395 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
2397 if (tmp_dir) dir = tmp_dir;
2399 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2400 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2402 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2403 DeleteFileW(tmpbuf);
2404 return MSVCRT__wcsdup(tmpbuf);
2406 TRACE("failed (%d)\n",GetLastError());
2407 return NULL;
2410 /*********************************************************************
2411 * _umask (MSVCRT.@)
2413 int CDECL MSVCRT__umask(int umask)
2415 int old_umask = MSVCRT_umask;
2416 TRACE("(%d)\n",umask);
2417 MSVCRT_umask = umask;
2418 return old_umask;
2421 /*********************************************************************
2422 * _utime64 (MSVCRT.@)
2424 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
2426 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2428 if (fd > 0)
2430 int retVal = _futime64(fd, t);
2431 MSVCRT__close(fd);
2432 return retVal;
2434 return -1;
2437 /*********************************************************************
2438 * _utime32 (MSVCRT.@)
2440 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
2442 if (t)
2444 struct MSVCRT___utimbuf64 t64;
2445 t64.actime = t->actime;
2446 t64.modtime = t->modtime;
2447 return _utime64( path, &t64 );
2449 else
2450 return _utime64( path, NULL );
2453 /*********************************************************************
2454 * _utime (MSVCRT.@)
2456 #ifdef _WIN64
2457 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
2459 return _utime64( path, t );
2461 #else
2462 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
2464 return _utime32( path, t );
2466 #endif
2468 /*********************************************************************
2469 * _wutime64 (MSVCRT.@)
2471 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2473 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2475 if (fd > 0)
2477 int retVal = _futime64(fd, t);
2478 MSVCRT__close(fd);
2479 return retVal;
2481 return -1;
2484 /*********************************************************************
2485 * _wutime32 (MSVCRT.@)
2487 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2489 if (t)
2491 struct MSVCRT___utimbuf64 t64;
2492 t64.actime = t->actime;
2493 t64.modtime = t->modtime;
2494 return _wutime64( path, &t64 );
2496 else
2497 return _wutime64( path, NULL );
2500 /*********************************************************************
2501 * _wutime (MSVCRT.@)
2503 #ifdef _WIN64
2504 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
2506 return _wutime64( path, t );
2508 #else
2509 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
2511 return _wutime32( path, t );
2513 #endif
2515 /*********************************************************************
2516 * _write (MSVCRT.@)
2518 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2520 DWORD num_written;
2521 HANDLE hand = msvcrt_fdtoh(fd);
2523 /* Don't trace small writes, it gets *very* annoying */
2524 #if 0
2525 if (count > 32)
2526 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2527 #endif
2528 if (hand == INVALID_HANDLE_VALUE)
2530 *MSVCRT__errno() = MSVCRT_EBADF;
2531 return -1;
2534 /* If appending, go to EOF */
2535 if (msvcrt_get_ioinfo(fd)->wxflag & WX_APPEND)
2536 MSVCRT__lseek(fd, 0, FILE_END);
2538 if (!(msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT))
2540 if (WriteFile(hand, buf, count, &num_written, NULL)
2541 && (num_written == count))
2542 return num_written;
2543 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2544 hand, GetLastError());
2545 *MSVCRT__errno() = MSVCRT_ENOSPC;
2547 else
2549 unsigned int i, j, nr_lf;
2550 char *p = NULL;
2551 const char *q;
2552 const char *s = buf, *buf_start = buf;
2553 /* find number of \n ( without preceding \r ) */
2554 for ( nr_lf=0,i = 0; i <count; i++)
2556 if (s[i]== '\n')
2558 nr_lf++;
2559 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2562 if (nr_lf)
2564 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2566 for (s = buf, i = 0, j = 0; i < count; i++)
2568 if (s[i]== '\n')
2570 p[j++] = '\r';
2571 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2573 p[j++] = s[i];
2576 else
2578 FIXME("Malloc failed\n");
2579 nr_lf =0;
2580 q = buf;
2583 else
2584 q = buf;
2586 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2588 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2589 fd, hand, GetLastError(), num_written);
2590 *MSVCRT__errno() = MSVCRT_ENOSPC;
2591 if(nr_lf)
2592 MSVCRT_free(p);
2593 return s - buf_start;
2595 else
2597 if(nr_lf)
2598 MSVCRT_free(p);
2599 return count;
2602 return -1;
2605 /*********************************************************************
2606 * _putw (MSVCRT.@)
2608 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2610 int len;
2612 MSVCRT__lock_file(file);
2613 len = MSVCRT__write(file->_file, &val, sizeof(val));
2614 if (len == sizeof(val)) {
2615 MSVCRT__unlock_file(file);
2616 return val;
2619 file->_flag |= MSVCRT__IOERR;
2620 MSVCRT__unlock_file(file);
2621 return MSVCRT_EOF;
2624 /*********************************************************************
2625 * fclose (MSVCRT.@)
2627 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2629 int r, flag;
2631 MSVCRT__lock_file(file);
2632 flag = file->_flag;
2633 MSVCRT_free(file->_tmpfname);
2634 file->_tmpfname = NULL;
2635 /* flush stdio buffers */
2636 if(file->_flag & MSVCRT__IOWRT)
2637 MSVCRT_fflush(file);
2638 if(file->_flag & MSVCRT__IOMYBUF)
2639 MSVCRT_free(file->_base);
2641 r=MSVCRT__close(file->_file);
2643 file->_flag = 0;
2644 MSVCRT__unlock_file(file);
2646 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2649 /*********************************************************************
2650 * feof (MSVCRT.@)
2652 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2654 return file->_flag & MSVCRT__IOEOF;
2657 /*********************************************************************
2658 * ferror (MSVCRT.@)
2660 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2662 return file->_flag & MSVCRT__IOERR;
2665 /*********************************************************************
2666 * _filbuf (MSVCRT.@)
2668 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2670 unsigned char c;
2671 MSVCRT__lock_file(file);
2673 /* Allocate buffer if needed */
2674 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF))
2675 msvcrt_alloc_buffer(file);
2677 if(!(file->_flag & MSVCRT__IOREAD)) {
2678 if(file->_flag & MSVCRT__IORW)
2679 file->_flag |= MSVCRT__IOREAD;
2680 else {
2681 MSVCRT__unlock_file(file);
2682 return MSVCRT_EOF;
2686 if(file->_flag & MSVCRT__IONBF) {
2687 int r;
2688 if ((r = read_i(file->_file,&c,1)) != 1) {
2689 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2690 MSVCRT__unlock_file(file);
2691 return MSVCRT_EOF;
2694 MSVCRT__unlock_file(file);
2695 return c;
2696 } else {
2697 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2698 if(file->_cnt<=0) {
2699 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2700 file->_cnt = 0;
2701 MSVCRT__unlock_file(file);
2702 return MSVCRT_EOF;
2705 file->_cnt--;
2706 file->_ptr = file->_base+1;
2707 c = *(unsigned char *)file->_base;
2708 MSVCRT__unlock_file(file);
2709 return c;
2713 /*********************************************************************
2714 * fgetc (MSVCRT.@)
2716 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2718 unsigned char *i;
2719 unsigned int j;
2721 MSVCRT__lock_file(file);
2722 if (file->_cnt>0) {
2723 file->_cnt--;
2724 i = (unsigned char *)file->_ptr++;
2725 j = *i;
2726 } else
2727 j = MSVCRT__filbuf(file);
2729 MSVCRT__unlock_file(file);
2730 return j;
2733 /*********************************************************************
2734 * _fgetchar (MSVCRT.@)
2736 int CDECL MSVCRT__fgetchar(void)
2738 return MSVCRT_fgetc(MSVCRT_stdin);
2741 /*********************************************************************
2742 * fgets (MSVCRT.@)
2744 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2746 int cc = MSVCRT_EOF;
2747 char * buf_start = s;
2749 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2750 file,file->_file,s,size);
2752 MSVCRT__lock_file(file);
2754 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2756 *s++ = (char)cc;
2757 size --;
2759 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2761 TRACE(":nothing read\n");
2762 MSVCRT__unlock_file(file);
2763 return NULL;
2765 if ((cc != MSVCRT_EOF) && (size > 1))
2766 *s++ = cc;
2767 *s = '\0';
2768 TRACE(":got %s\n", debugstr_a(buf_start));
2769 MSVCRT__unlock_file(file);
2770 return buf_start;
2773 /*********************************************************************
2774 * fgetwc (MSVCRT.@)
2776 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2777 * the CR from CR/LF combinations
2779 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2781 int c;
2783 MSVCRT__lock_file(file);
2784 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT))
2786 MSVCRT_wchar_t wc;
2787 unsigned int i;
2788 int j;
2789 char *chp, *wcp;
2790 wcp = (char *)&wc;
2791 for(i=0; i<sizeof(wc); i++)
2793 if (file->_cnt>0)
2795 file->_cnt--;
2796 chp = file->_ptr++;
2797 wcp[i] = *chp;
2799 else
2801 j = MSVCRT__filbuf(file);
2802 if(file->_cnt<=0)
2804 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2805 file->_cnt = 0;
2807 MSVCRT__unlock_file(file);
2808 return MSVCRT_WEOF;
2810 wcp[i] = j;
2814 MSVCRT__unlock_file(file);
2815 return wc;
2818 c = MSVCRT_fgetc(file);
2819 if ((get_locinfo()->mb_cur_max > 1) && MSVCRT_isleadbyte(c))
2821 FIXME("Treat Multibyte characters\n");
2824 MSVCRT__unlock_file(file);
2825 if (c == MSVCRT_EOF)
2826 return MSVCRT_WEOF;
2827 else
2828 return (MSVCRT_wint_t)c;
2831 /*********************************************************************
2832 * _getw (MSVCRT.@)
2834 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2836 char *ch;
2837 int i, k;
2838 unsigned int j;
2839 ch = (char *)&i;
2841 MSVCRT__lock_file(file);
2842 for (j=0; j<sizeof(int); j++) {
2843 k = MSVCRT_fgetc(file);
2844 if (k == MSVCRT_EOF) {
2845 file->_flag |= MSVCRT__IOEOF;
2846 MSVCRT__unlock_file(file);
2847 return EOF;
2849 ch[j] = k;
2852 MSVCRT__unlock_file(file);
2853 return i;
2856 /*********************************************************************
2857 * getwc (MSVCRT.@)
2859 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2861 return MSVCRT_fgetwc(file);
2864 /*********************************************************************
2865 * _fgetwchar (MSVCRT.@)
2867 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
2869 return MSVCRT_fgetwc(MSVCRT_stdin);
2872 /*********************************************************************
2873 * getwchar (MSVCRT.@)
2875 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2877 return MSVCRT__fgetwchar();
2880 /*********************************************************************
2881 * fgetws (MSVCRT.@)
2883 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2885 int cc = MSVCRT_WEOF;
2886 MSVCRT_wchar_t * buf_start = s;
2888 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2889 file,file->_file,s,size);
2891 MSVCRT__lock_file(file);
2893 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2895 *s++ = (char)cc;
2896 size --;
2898 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2900 TRACE(":nothing read\n");
2901 MSVCRT__unlock_file(file);
2902 return NULL;
2904 if ((cc != MSVCRT_WEOF) && (size > 1))
2905 *s++ = cc;
2906 *s = 0;
2907 TRACE(":got %s\n", debugstr_w(buf_start));
2908 MSVCRT__unlock_file(file);
2909 return buf_start;
2912 /*********************************************************************
2913 * fwrite (MSVCRT.@)
2915 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2917 MSVCRT_size_t wrcnt=size * nmemb;
2918 int written = 0;
2919 if (size == 0)
2920 return 0;
2922 MSVCRT__lock_file(file);
2923 if(file->_cnt) {
2924 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2925 memcpy(file->_ptr, ptr, pcnt);
2926 file->_cnt -= pcnt;
2927 file->_ptr += pcnt;
2928 written = pcnt;
2929 wrcnt -= pcnt;
2930 ptr = (const char*)ptr + pcnt;
2931 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2932 if(file->_flag & MSVCRT__IORW) {
2933 file->_flag |= MSVCRT__IOWRT;
2934 } else {
2935 MSVCRT__unlock_file(file);
2936 return 0;
2939 if(wrcnt) {
2940 /* Flush buffer */
2941 int res=msvcrt_flush_buffer(file);
2942 if(!res) {
2943 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2944 if (pwritten <= 0)
2946 file->_flag |= MSVCRT__IOERR;
2947 pwritten=0;
2949 written += pwritten;
2953 MSVCRT__unlock_file(file);
2954 return written / size;
2957 /*********************************************************************
2958 * fputwc (MSVCRT.@)
2960 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2962 MSVCRT_wchar_t mwc=wc;
2963 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2964 return MSVCRT_WEOF;
2965 return wc;
2968 /*********************************************************************
2969 * _fputwchar (MSVCRT.@)
2971 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
2973 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2976 /*********************************************************************
2977 * _wfsopen (MSVCRT.@)
2979 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2981 MSVCRT_FILE* file;
2982 int open_flags, stream_flags, fd;
2984 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
2986 /* map mode string to open() flags. "man fopen" for possibilities. */
2987 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2988 return NULL;
2990 LOCK_FILES();
2991 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2992 if (fd < 0)
2993 file = NULL;
2994 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2995 != -1)
2996 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
2997 else if (file)
2999 file->_flag = 0;
3000 file = NULL;
3003 TRACE(":got (%p)\n",file);
3004 if (fd >= 0 && !file)
3005 MSVCRT__close(fd);
3006 UNLOCK_FILES();
3007 return file;
3010 /*********************************************************************
3011 * _fsopen (MSVCRT.@)
3013 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3015 MSVCRT_FILE *ret;
3016 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3018 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3019 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3020 *MSVCRT__errno() = MSVCRT_EINVAL;
3021 return NULL;
3023 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3025 MSVCRT_free(pathW);
3026 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3027 *MSVCRT__errno() = MSVCRT_EINVAL;
3028 return NULL;
3031 ret = MSVCRT__wfsopen(pathW, modeW, share);
3033 MSVCRT_free(pathW);
3034 MSVCRT_free(modeW);
3035 return ret;
3038 /*********************************************************************
3039 * fopen (MSVCRT.@)
3041 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3043 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3046 /*********************************************************************
3047 * fopen_s (MSVCRT.@)
3049 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3050 const char *filename, const char *mode)
3052 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
3053 !MSVCRT_CHECK_PMT(mode != NULL)) {
3054 *MSVCRT__errno() = MSVCRT_EINVAL;
3055 return MSVCRT_EINVAL;
3058 *pFile = MSVCRT_fopen(filename, mode);
3060 if(!*pFile)
3061 return *MSVCRT__errno();
3062 return 0;
3065 /*********************************************************************
3066 * _wfopen (MSVCRT.@)
3068 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3070 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3073 /*********************************************************************
3074 * _wfopen_s (MSVCRT.@)
3076 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3077 const MSVCRT_wchar_t *mode)
3079 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
3080 !MSVCRT_CHECK_PMT(mode != NULL)) {
3081 *MSVCRT__errno() = MSVCRT_EINVAL;
3082 return MSVCRT_EINVAL;
3085 *pFile = MSVCRT__wfopen(filename, mode);
3087 if(!*pFile)
3088 return *MSVCRT__errno();
3089 return 0;
3092 /*********************************************************************
3093 * _flsbuf (MSVCRT.@)
3095 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3097 /* Flush output buffer */
3098 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3099 msvcrt_alloc_buffer(file);
3101 if(!(file->_flag & MSVCRT__IOWRT)) {
3102 if(file->_flag & MSVCRT__IORW)
3103 file->_flag |= MSVCRT__IOWRT;
3104 else
3105 return MSVCRT_EOF;
3107 if(file->_bufsiz) {
3108 int res = 0;
3110 if(file->_cnt <= 0)
3111 res = msvcrt_flush_buffer(file);
3112 if(!res) {
3113 *file->_ptr++ = c;
3114 file->_cnt--;
3115 res = msvcrt_flush_buffer(file);
3118 return res ? res : c&0xff;
3119 } else {
3120 unsigned char cc=c;
3121 int len;
3122 /* set _cnt to 0 for unbuffered FILEs */
3123 file->_cnt = 0;
3124 len = MSVCRT__write(file->_file, &cc, 1);
3125 if (len == 1)
3126 return c & 0xff;
3127 file->_flag |= MSVCRT__IOERR;
3128 return MSVCRT_EOF;
3132 /*********************************************************************
3133 * fputc (MSVCRT.@)
3135 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3137 int res;
3139 MSVCRT__lock_file(file);
3140 if(file->_cnt>0) {
3141 *file->_ptr++=c;
3142 file->_cnt--;
3143 if (c == '\n')
3145 res = msvcrt_flush_buffer(file);
3146 MSVCRT__unlock_file(file);
3147 return res ? res : c;
3149 else {
3150 MSVCRT__unlock_file(file);
3151 return c & 0xff;
3153 } else {
3154 res = MSVCRT__flsbuf(c, file);
3155 MSVCRT__unlock_file(file);
3156 return res;
3160 /*********************************************************************
3161 * _fputchar (MSVCRT.@)
3163 int CDECL MSVCRT__fputchar(int c)
3165 return MSVCRT_fputc(c, MSVCRT_stdout);
3168 /*********************************************************************
3169 * fread (MSVCRT.@)
3171 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3173 MSVCRT_size_t rcnt=size * nmemb;
3174 MSVCRT_size_t read=0;
3175 int pread=0;
3177 if(!rcnt)
3178 return 0;
3180 MSVCRT__lock_file(file);
3182 /* first buffered data */
3183 if(file->_cnt>0) {
3184 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3185 memcpy(ptr, file->_ptr, pcnt);
3186 file->_cnt -= pcnt;
3187 file->_ptr += pcnt;
3188 read += pcnt ;
3189 rcnt -= pcnt ;
3190 ptr = (char*)ptr + pcnt;
3191 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3192 if(file->_flag & MSVCRT__IORW) {
3193 file->_flag |= MSVCRT__IOREAD;
3194 } else {
3195 MSVCRT__unlock_file(file);
3196 return 0;
3199 while(rcnt>0)
3201 int i;
3202 /* Fill the buffer on small reads.
3203 * TODO: Use a better buffering strategy.
3205 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
3206 if (file->_bufsiz == 0) {
3207 msvcrt_alloc_buffer(file);
3209 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3210 file->_ptr = file->_base;
3211 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3212 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3213 if (i > 0 && i < file->_cnt) {
3214 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3215 file->_flag &= ~MSVCRT__IOEOF;
3217 if (i > 0) {
3218 memcpy(ptr, file->_ptr, i);
3219 file->_cnt -= i;
3220 file->_ptr += i;
3222 } else {
3223 i = MSVCRT__read(file->_file,ptr, rcnt);
3225 pread += i;
3226 rcnt -= i;
3227 ptr = (char *)ptr+i;
3228 /* expose feof condition in the flags
3229 * MFC tests file->_flag for feof, and doesn't call feof())
3231 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
3232 file->_flag |= MSVCRT__IOEOF;
3233 else if (i == -1)
3235 file->_flag |= MSVCRT__IOERR;
3236 pread = 0;
3237 rcnt = 0;
3239 if (i < 1) break;
3241 read+=pread;
3242 MSVCRT__unlock_file(file);
3243 return read / size;
3246 /*********************************************************************
3247 * _wfreopen (MSVCRT.@)
3250 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3252 int open_flags, stream_flags, fd;
3254 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
3256 LOCK_FILES();
3257 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
3258 file = NULL;
3259 else
3261 MSVCRT_fclose(file);
3262 /* map mode string to open() flags. "man fopen" for possibilities. */
3263 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3264 file = NULL;
3265 else
3267 fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3268 if (fd < 0)
3269 file = NULL;
3270 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
3272 file->_flag = 0;
3273 WARN(":failed-last error (%d)\n",GetLastError());
3274 msvcrt_set_errno(GetLastError());
3275 file = NULL;
3279 UNLOCK_FILES();
3280 return file;
3283 /*********************************************************************
3284 * _wfreopen_s (MSVCRT.@)
3286 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
3287 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
3289 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(path != NULL) ||
3290 !MSVCRT_CHECK_PMT(mode != NULL) || !MSVCRT_CHECK_PMT(file != NULL)) {
3291 *MSVCRT__errno() = MSVCRT_EINVAL;
3292 return MSVCRT_EINVAL;
3295 *pFile = MSVCRT__wfreopen(path, mode, file);
3297 if(!*pFile)
3298 return *MSVCRT__errno();
3299 return 0;
3302 /*********************************************************************
3303 * freopen (MSVCRT.@)
3306 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
3308 MSVCRT_FILE *ret;
3309 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3311 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3312 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3314 MSVCRT_free(pathW);
3315 return NULL;
3318 ret = MSVCRT__wfreopen(pathW, modeW, file);
3320 MSVCRT_free(pathW);
3321 MSVCRT_free(modeW);
3322 return ret;
3325 /*********************************************************************
3326 * freopen_s (MSVCRT.@)
3328 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
3329 const char *path, const char *mode, MSVCRT_FILE* file)
3331 if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(path != NULL) ||
3332 !MSVCRT_CHECK_PMT(mode != NULL) || !MSVCRT_CHECK_PMT(file != NULL)) {
3333 *MSVCRT__errno() = MSVCRT_EINVAL;
3334 return MSVCRT_EINVAL;
3337 *pFile = MSVCRT_freopen(path, mode, file);
3339 if(!*pFile)
3340 return *MSVCRT__errno();
3341 return 0;
3344 /*********************************************************************
3345 * fsetpos (MSVCRT.@)
3347 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3349 int ret;
3351 MSVCRT__lock_file(file);
3352 /* Note that all this has been lifted 'as is' from fseek */
3353 if(file->_flag & MSVCRT__IOWRT)
3354 msvcrt_flush_buffer(file);
3356 /* Discard buffered input */
3357 file->_cnt = 0;
3358 file->_ptr = file->_base;
3360 /* Reset direction of i/o */
3361 if(file->_flag & MSVCRT__IORW) {
3362 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
3365 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3366 MSVCRT__unlock_file(file);
3367 return ret;
3370 /*********************************************************************
3371 * _ftelli64 (MSVCRT.@)
3373 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
3375 /* TODO: just call fgetpos and return lower half of result */
3376 int off=0;
3377 __int64 pos;
3379 MSVCRT__lock_file(file);
3380 pos = _telli64(file->_file);
3381 if(pos == -1) {
3382 MSVCRT__unlock_file(file);
3383 return -1;
3385 if(file->_bufsiz) {
3386 if( file->_flag & MSVCRT__IOWRT ) {
3387 off = file->_ptr - file->_base;
3388 } else {
3389 off = -file->_cnt;
3390 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3391 /* Black magic correction for CR removal */
3392 int i;
3393 for (i=0; i<file->_cnt; i++) {
3394 if (file->_ptr[i] == '\n')
3395 off--;
3397 /* Black magic when reading CR at buffer boundary*/
3398 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3399 off--;
3404 MSVCRT__unlock_file(file);
3405 return off + pos;
3408 /*********************************************************************
3409 * ftell (MSVCRT.@)
3411 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
3413 return MSVCRT__ftelli64(file);
3416 /*********************************************************************
3417 * fgetpos (MSVCRT.@)
3419 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
3421 int off=0;
3423 MSVCRT__lock_file(file);
3424 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
3425 if(*pos == -1) {
3426 MSVCRT__unlock_file(file);
3427 return -1;
3429 if(file->_bufsiz) {
3430 if( file->_flag & MSVCRT__IOWRT ) {
3431 off = file->_ptr - file->_base;
3432 } else {
3433 off = -file->_cnt;
3434 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
3435 /* Black magic correction for CR removal */
3436 int i;
3437 for (i=0; i<file->_cnt; i++) {
3438 if (file->_ptr[i] == '\n')
3439 off--;
3441 /* Black magic when reading CR at buffer boundary*/
3442 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READCR)
3443 off--;
3447 *pos += off;
3448 MSVCRT__unlock_file(file);
3449 return 0;
3452 /*********************************************************************
3453 * fputs (MSVCRT.@)
3455 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
3457 MSVCRT_size_t i, len = strlen(s);
3458 int ret;
3460 MSVCRT__lock_file(file);
3461 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3462 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3463 MSVCRT__unlock_file(file);
3464 return ret;
3466 for (i=0; i<len; i++)
3467 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF) {
3468 MSVCRT__unlock_file(file);
3469 return MSVCRT_EOF;
3472 MSVCRT__unlock_file(file);
3473 return 0;
3476 /*********************************************************************
3477 * fputws (MSVCRT.@)
3479 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
3481 MSVCRT_size_t i, len = strlenW(s);
3482 int ret;
3484 MSVCRT__lock_file(file);
3485 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3486 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
3487 MSVCRT__unlock_file(file);
3488 return ret;
3490 for (i=0; i<len; i++) {
3491 if (((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
3492 || MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
3493 MSVCRT__unlock_file(file);
3494 return MSVCRT_WEOF;
3498 MSVCRT__unlock_file(file);
3499 return 0;
3502 /*********************************************************************
3503 * getchar (MSVCRT.@)
3505 int CDECL MSVCRT_getchar(void)
3507 return MSVCRT_fgetc(MSVCRT_stdin);
3510 /*********************************************************************
3511 * getc (MSVCRT.@)
3513 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
3515 return MSVCRT_fgetc(file);
3518 /*********************************************************************
3519 * gets (MSVCRT.@)
3521 char * CDECL MSVCRT_gets(char *buf)
3523 int cc;
3524 char * buf_start = buf;
3526 MSVCRT__lock_file(MSVCRT_stdin);
3527 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
3528 cc = MSVCRT_fgetc(MSVCRT_stdin))
3529 if(cc != '\r') *buf++ = (char)cc;
3531 *buf = '\0';
3533 TRACE("got '%s'\n", buf_start);
3534 MSVCRT__unlock_file(MSVCRT_stdin);
3535 return buf_start;
3538 /*********************************************************************
3539 * _getws (MSVCRT.@)
3541 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
3543 MSVCRT_wint_t cc;
3544 MSVCRT_wchar_t* ws = buf;
3546 MSVCRT__lock_file(MSVCRT_stdin);
3547 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
3548 cc = MSVCRT_fgetwc(MSVCRT_stdin))
3550 if (cc != '\r')
3551 *buf++ = (MSVCRT_wchar_t)cc;
3553 *buf = '\0';
3555 TRACE("got %s\n", debugstr_w(ws));
3556 MSVCRT__unlock_file(MSVCRT_stdin);
3557 return ws;
3560 /*********************************************************************
3561 * putc (MSVCRT.@)
3563 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
3565 return MSVCRT_fputc(c, file);
3568 /*********************************************************************
3569 * putchar (MSVCRT.@)
3571 int CDECL MSVCRT_putchar(int c)
3573 return MSVCRT_fputc(c, MSVCRT_stdout);
3576 /*********************************************************************
3577 * _putwch (MSVCRT.@)
3579 int CDECL MSVCRT__putwch(int c)
3581 return MSVCRT_fputwc(c, MSVCRT_stdout);
3584 /*********************************************************************
3585 * puts (MSVCRT.@)
3587 int CDECL MSVCRT_puts(const char *s)
3589 MSVCRT_size_t len = strlen(s);
3590 int ret;
3592 MSVCRT__lock_file(MSVCRT_stdout);
3593 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3594 MSVCRT__unlock_file(MSVCRT_stdout);
3595 return MSVCRT_EOF;
3598 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3599 MSVCRT__unlock_file(MSVCRT_stdout);
3600 return ret;
3603 /*********************************************************************
3604 * _putws (MSVCRT.@)
3606 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
3608 static const MSVCRT_wchar_t nl = '\n';
3609 MSVCRT_size_t len = strlenW(s);
3610 int ret;
3612 MSVCRT__lock_file(MSVCRT_stdout);
3613 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
3614 MSVCRT__unlock_file(MSVCRT_stdout);
3615 return MSVCRT_EOF;
3618 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
3619 MSVCRT__unlock_file(MSVCRT_stdout);
3620 return ret;
3623 /*********************************************************************
3624 * remove (MSVCRT.@)
3626 int CDECL MSVCRT_remove(const char *path)
3628 TRACE("(%s)\n",path);
3629 if (DeleteFileA(path))
3630 return 0;
3631 TRACE(":failed (%d)\n",GetLastError());
3632 msvcrt_set_errno(GetLastError());
3633 return -1;
3636 /*********************************************************************
3637 * _wremove (MSVCRT.@)
3639 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
3641 TRACE("(%s)\n",debugstr_w(path));
3642 if (DeleteFileW(path))
3643 return 0;
3644 TRACE(":failed (%d)\n",GetLastError());
3645 msvcrt_set_errno(GetLastError());
3646 return -1;
3649 /*********************************************************************
3650 * rename (MSVCRT.@)
3652 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
3654 TRACE(":from %s to %s\n",oldpath,newpath);
3655 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3656 return 0;
3657 TRACE(":failed (%d)\n",GetLastError());
3658 msvcrt_set_errno(GetLastError());
3659 return -1;
3662 /*********************************************************************
3663 * _wrename (MSVCRT.@)
3665 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
3667 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3668 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3669 return 0;
3670 TRACE(":failed (%d)\n",GetLastError());
3671 msvcrt_set_errno(GetLastError());
3672 return -1;
3675 /*********************************************************************
3676 * setvbuf (MSVCRT.@)
3678 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
3680 MSVCRT__lock_file(file);
3681 if(file->_bufsiz) {
3682 MSVCRT_free(file->_base);
3683 file->_bufsiz = 0;
3684 file->_cnt = 0;
3686 if(mode == MSVCRT__IOFBF) {
3687 file->_flag &= ~MSVCRT__IONBF;
3688 file->_base = file->_ptr = buf;
3689 if(buf) {
3690 file->_bufsiz = size;
3692 } else {
3693 file->_flag |= MSVCRT__IONBF;
3695 MSVCRT__unlock_file(file);
3696 return 0;
3699 /*********************************************************************
3700 * setbuf (MSVCRT.@)
3702 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
3704 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
3707 /*********************************************************************
3708 * tmpnam (MSVCRT.@)
3710 char * CDECL MSVCRT_tmpnam(char *s)
3712 char tmpstr[16];
3713 char *p;
3714 int count, size;
3716 if (!s) {
3717 thread_data_t *data = msvcrt_get_thread_data();
3719 if(!data->tmpnam_buffer)
3720 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
3722 s = data->tmpnam_buffer;
3725 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
3726 p = s + sprintf(s, "\\s%s.", tmpstr);
3727 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3729 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
3730 memcpy(p, tmpstr, size);
3731 p[size] = '\0';
3732 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
3733 GetLastError() == ERROR_FILE_NOT_FOUND)
3734 break;
3736 return s;
3739 /*********************************************************************
3740 * _wtmpnam (MSVCRT.@)
3742 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
3744 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
3745 MSVCRT_wchar_t tmpstr[16];
3746 MSVCRT_wchar_t *p;
3747 int count, size;
3748 if (!s) {
3749 thread_data_t *data = msvcrt_get_thread_data();
3751 if(!data->wtmpnam_buffer)
3752 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
3754 s = data->wtmpnam_buffer;
3757 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
3758 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
3759 for (count = 0; count < MSVCRT_TMP_MAX; count++)
3761 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
3762 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
3763 p[size] = '\0';
3764 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
3765 GetLastError() == ERROR_FILE_NOT_FOUND)
3766 break;
3768 return s;
3771 /*********************************************************************
3772 * tmpfile (MSVCRT.@)
3774 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
3776 char *filename = MSVCRT_tmpnam(NULL);
3777 int fd;
3778 MSVCRT_FILE* file = NULL;
3780 LOCK_FILES();
3781 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3782 if (fd != -1 && (file = msvcrt_alloc_fp()))
3784 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3786 file->_flag = 0;
3787 file = NULL;
3789 else file->_tmpfname = MSVCRT__strdup(filename);
3791 UNLOCK_FILES();
3792 return file;
3795 static int puts_clbk_file_a(void *file, int len, const char *str)
3797 return MSVCRT_fwrite(str, sizeof(char), len, file);
3800 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
3802 return MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
3805 /*********************************************************************
3806 * vfprintf (MSVCRT.@)
3808 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3810 int ret;
3812 MSVCRT__lock_file(file);
3813 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3814 MSVCRT__unlock_file(file);
3816 return ret;
3819 /*********************************************************************
3820 * vfprintf_s (MSVCRT.@)
3822 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
3824 int ret;
3826 if(!MSVCRT_CHECK_PMT(file != NULL)) {
3827 *MSVCRT__errno() = MSVCRT_EINVAL;
3828 return -1;
3831 MSVCRT__lock_file(file);
3832 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3833 MSVCRT__unlock_file(file);
3835 return ret;
3838 /*********************************************************************
3839 * vfwprintf (MSVCRT.@)
3841 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3843 int ret;
3845 MSVCRT__lock_file(file);
3846 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
3847 MSVCRT__unlock_file(file);
3849 return ret;
3852 /*********************************************************************
3853 * vfwprintf_s (MSVCRT.@)
3855 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
3857 int ret;
3859 if(!MSVCRT_CHECK_PMT( file != NULL)) {
3860 *MSVCRT__errno() = MSVCRT_EINVAL;
3861 return -1;
3864 MSVCRT__lock_file(file);
3865 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
3866 MSVCRT__unlock_file(file);
3868 return ret;
3871 /*********************************************************************
3872 * vprintf (MSVCRT.@)
3874 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
3876 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3879 /*********************************************************************
3880 * vprintf_s (MSVCRT.@)
3882 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
3884 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
3887 /*********************************************************************
3888 * vwprintf (MSVCRT.@)
3890 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
3892 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3895 /*********************************************************************
3896 * vwprintf_s (MSVCRT.@)
3898 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
3900 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
3903 /*********************************************************************
3904 * fprintf (MSVCRT.@)
3906 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3908 __ms_va_list valist;
3909 int res;
3910 __ms_va_start(valist, format);
3911 res = MSVCRT_vfprintf(file, format, valist);
3912 __ms_va_end(valist);
3913 return res;
3916 /*********************************************************************
3917 * fprintf_s (MSVCRT.@)
3919 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
3921 __ms_va_list valist;
3922 int res;
3923 __ms_va_start(valist, format);
3924 res = MSVCRT_vfprintf_s(file, format, valist);
3925 __ms_va_end(valist);
3926 return res;
3929 /*********************************************************************
3930 * fwprintf (MSVCRT.@)
3932 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3934 __ms_va_list valist;
3935 int res;
3936 __ms_va_start(valist, format);
3937 res = MSVCRT_vfwprintf(file, format, valist);
3938 __ms_va_end(valist);
3939 return res;
3942 /*********************************************************************
3943 * fwprintf_s (MSVCRT.@)
3945 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3947 __ms_va_list valist;
3948 int res;
3949 __ms_va_start(valist, format);
3950 res = MSVCRT_vfwprintf_s(file, format, valist);
3951 __ms_va_end(valist);
3952 return res;
3955 /*********************************************************************
3956 * printf (MSVCRT.@)
3958 int CDECL MSVCRT_printf(const char *format, ...)
3960 __ms_va_list valist;
3961 int res;
3962 __ms_va_start(valist, format);
3963 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3964 __ms_va_end(valist);
3965 return res;
3968 /*********************************************************************
3969 * printf_s (MSVCRT.@)
3971 int CDECL MSVCRT_printf_s(const char *format, ...)
3973 __ms_va_list valist;
3974 int res;
3975 __ms_va_start(valist, format);
3976 res = MSVCRT_vprintf_s(format, valist);
3977 __ms_va_end(valist);
3978 return res;
3981 /*********************************************************************
3982 * ungetc (MSVCRT.@)
3984 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3986 if (c == MSVCRT_EOF)
3987 return MSVCRT_EOF;
3989 MSVCRT__lock_file(file);
3990 if(file->_bufsiz == 0) {
3991 msvcrt_alloc_buffer(file);
3992 file->_ptr++;
3994 if(file->_ptr>file->_base) {
3995 file->_ptr--;
3996 *file->_ptr=c;
3997 file->_cnt++;
3998 MSVCRT_clearerr(file);
3999 MSVCRT__unlock_file(file);
4000 return c;
4003 MSVCRT__unlock_file(file);
4004 return MSVCRT_EOF;
4007 /*********************************************************************
4008 * ungetwc (MSVCRT.@)
4010 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
4012 MSVCRT_wchar_t mwc = wc;
4013 char * pp = (char *)&mwc;
4014 int i;
4016 MSVCRT__lock_file(file);
4017 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
4018 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
4019 MSVCRT__unlock_file(file);
4020 return MSVCRT_WEOF;
4024 MSVCRT__unlock_file(file);
4025 return mwc;
4028 /*********************************************************************
4029 * wprintf (MSVCRT.@)
4031 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
4033 __ms_va_list valist;
4034 int res;
4035 __ms_va_start(valist, format);
4036 res = MSVCRT_vwprintf(format, valist);
4037 __ms_va_end(valist);
4038 return res;
4041 /*********************************************************************
4042 * wprintf_s (MSVCRT.@)
4044 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
4046 __ms_va_list valist;
4047 int res;
4048 __ms_va_start(valist, format);
4049 res = MSVCRT_vwprintf_s(format, valist);
4050 __ms_va_end(valist);
4051 return res;
4054 /*********************************************************************
4055 * _getmaxstdio (MSVCRT.@)
4057 int CDECL MSVCRT__getmaxstdio(void)
4059 return MSVCRT_max_streams;
4062 /*********************************************************************
4063 * _setmaxstdio (MSVCRT.@)
4065 int CDECL MSVCRT__setmaxstdio(int newmax)
4067 TRACE("%d\n", newmax);
4069 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4070 return -1;
4072 MSVCRT_max_streams = newmax;
4073 return MSVCRT_max_streams;