reg/tests: Use a helper function to delete registry values.
[wine.git] / dlls / msvcrt / file.c
blob8af9bc2db48bcd1fbb34547f1532c68a7efde903
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 <stdarg.h>
33 #include <stdio.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <sys/types.h>
38 #include <limits.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_READNL 0x04 /* read started with \n */
65 #define WX_PIPE 0x08
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
68 #define WX_TTY 0x40
69 #define WX_TEXT 0x80
71 /* values for exflag - it's used differently in msvcr90.dll*/
72 #define EF_UTF8 0x01
73 #define EF_UTF16 0x02
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
87 typedef struct {
88 HANDLE handle;
89 unsigned char wxflag;
90 char lookahead[3];
91 int exflag;
92 CRITICAL_SECTION crit;
93 #if _MSVCR_VER >= 80
94 char textmode : 7;
95 char unicode : 1;
96 char pipech2[2];
97 __int64 startpos;
98 BOOL utf8translations;
99 #endif
100 #if _MSVCR_VER >= 90
101 char dbcsBuffer;
102 BOOL dbcsBufferUsed;
103 #endif
104 } ioinfo;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
117 typedef struct {
118 MSVCRT_FILE file;
119 CRITICAL_SECTION crit;
120 } file_crit;
122 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
123 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
124 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
126 /* INTERNAL: process umask */
127 static int MSVCRT_umask = 0;
129 /* INTERNAL: static data for tmpnam and _wtmpname functions */
130 static int tmpnam_unique;
131 static int tmpnam_s_unique;
133 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
134 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
135 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
136 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
138 #define TOUL(x) (ULONGLONG)(x)
139 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
140 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
141 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
142 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
144 /* This critical section protects the MSVCRT_fstreams table
145 * and MSVCRT_stream_idx from race conditions. It also
146 * protects fd critical sections creation code.
148 static CRITICAL_SECTION MSVCRT_file_cs;
149 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
151 0, 0, &MSVCRT_file_cs,
152 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
153 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
155 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
156 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
157 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
159 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
161 buf->st_dev = buf64->st_dev;
162 buf->st_ino = buf64->st_ino;
163 buf->st_mode = buf64->st_mode;
164 buf->st_nlink = buf64->st_nlink;
165 buf->st_uid = buf64->st_uid;
166 buf->st_gid = buf64->st_gid;
167 buf->st_rdev = buf64->st_rdev;
168 buf->st_size = buf64->st_size;
169 buf->st_atime = buf64->st_atime;
170 buf->st_mtime = buf64->st_mtime;
171 buf->st_ctime = buf64->st_ctime;
174 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
176 buf->st_dev = buf64->st_dev;
177 buf->st_ino = buf64->st_ino;
178 buf->st_mode = buf64->st_mode;
179 buf->st_nlink = buf64->st_nlink;
180 buf->st_uid = buf64->st_uid;
181 buf->st_gid = buf64->st_gid;
182 buf->st_rdev = buf64->st_rdev;
183 buf->st_size = buf64->st_size;
184 buf->st_atime = buf64->st_atime;
185 buf->st_mtime = buf64->st_mtime;
186 buf->st_ctime = buf64->st_ctime;
189 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32 *buf)
191 buf->st_dev = buf64->st_dev;
192 buf->st_ino = buf64->st_ino;
193 buf->st_mode = buf64->st_mode;
194 buf->st_nlink = buf64->st_nlink;
195 buf->st_uid = buf64->st_uid;
196 buf->st_gid = buf64->st_gid;
197 buf->st_rdev = buf64->st_rdev;
198 buf->st_size = buf64->st_size;
199 buf->st_atime = buf64->st_atime;
200 buf->st_mtime = buf64->st_mtime;
201 buf->st_ctime = buf64->st_ctime;
204 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat64i32 *buf)
206 buf->st_dev = buf64->st_dev;
207 buf->st_ino = buf64->st_ino;
208 buf->st_mode = buf64->st_mode;
209 buf->st_nlink = buf64->st_nlink;
210 buf->st_uid = buf64->st_uid;
211 buf->st_gid = buf64->st_gid;
212 buf->st_rdev = buf64->st_rdev;
213 buf->st_size = buf64->st_size;
214 buf->st_atime = buf64->st_atime;
215 buf->st_mtime = buf64->st_mtime;
216 buf->st_ctime = buf64->st_ctime;
219 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32i64 *buf)
221 buf->st_dev = buf64->st_dev;
222 buf->st_ino = buf64->st_ino;
223 buf->st_mode = buf64->st_mode;
224 buf->st_nlink = buf64->st_nlink;
225 buf->st_uid = buf64->st_uid;
226 buf->st_gid = buf64->st_gid;
227 buf->st_rdev = buf64->st_rdev;
228 buf->st_size = buf64->st_size;
229 buf->st_atime = buf64->st_atime;
230 buf->st_mtime = buf64->st_mtime;
231 buf->st_ctime = buf64->st_ctime;
234 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
236 /* 1601 to 1970 is 369 years plus 89 leap days */
237 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
239 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
240 ft->dwHighDateTime = ticks >> 32;
241 ft->dwLowDateTime = ticks;
244 static inline ioinfo* get_ioinfo_nolock(int fd)
246 ioinfo *ret = NULL;
247 if(fd>=0 && fd<MSVCRT_MAX_FILES)
248 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
249 if(!ret)
250 return &MSVCRT___badioinfo;
252 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
255 static inline void init_ioinfo_cs(ioinfo *info)
257 if(!(info->exflag & EF_CRIT_INIT)) {
258 LOCK_FILES();
259 if(!(info->exflag & EF_CRIT_INIT)) {
260 InitializeCriticalSection(&info->crit);
261 info->exflag |= EF_CRIT_INIT;
263 UNLOCK_FILES();
267 static inline ioinfo* get_ioinfo(int fd)
269 ioinfo *ret = get_ioinfo_nolock(fd);
270 if(ret == &MSVCRT___badioinfo)
271 return ret;
272 init_ioinfo_cs(ret);
273 EnterCriticalSection(&ret->crit);
274 return ret;
277 static inline BOOL alloc_pioinfo_block(int fd)
279 ioinfo *block;
280 int i;
282 if(fd<0 || fd>=MSVCRT_MAX_FILES)
284 *MSVCRT__errno() = MSVCRT_ENFILE;
285 return FALSE;
288 block = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
289 if(!block)
291 WARN(":out of memory!\n");
292 *MSVCRT__errno() = MSVCRT_ENOMEM;
293 return FALSE;
295 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
296 block[i].handle = INVALID_HANDLE_VALUE;
297 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE], block, NULL))
298 MSVCRT_free(block);
299 return TRUE;
302 static inline ioinfo* get_ioinfo_alloc_fd(int fd)
304 ioinfo *ret;
306 ret = get_ioinfo(fd);
307 if(ret != &MSVCRT___badioinfo)
308 return ret;
310 if(!alloc_pioinfo_block(fd))
311 return &MSVCRT___badioinfo;
313 return get_ioinfo(fd);
316 static inline ioinfo* get_ioinfo_alloc(int *fd)
318 int i;
320 *fd = -1;
321 for(i=0; i<MSVCRT_MAX_FILES; i++)
323 ioinfo *info = get_ioinfo_nolock(i);
325 if(info == &MSVCRT___badioinfo)
327 if(!alloc_pioinfo_block(i))
328 return &MSVCRT___badioinfo;
329 info = get_ioinfo_nolock(i);
332 init_ioinfo_cs(info);
333 if(TryEnterCriticalSection(&info->crit))
335 if(info->handle == INVALID_HANDLE_VALUE)
337 *fd = i;
338 return info;
340 LeaveCriticalSection(&info->crit);
344 WARN(":files exhausted!\n");
345 *MSVCRT__errno() = MSVCRT_ENFILE;
346 return &MSVCRT___badioinfo;
349 static inline void release_ioinfo(ioinfo *info)
351 if(info!=&MSVCRT___badioinfo && info->exflag & EF_CRIT_INIT)
352 LeaveCriticalSection(&info->crit);
355 static inline MSVCRT_FILE* msvcrt_get_file(int i)
357 file_crit *ret;
359 if(i >= MSVCRT_max_streams)
360 return NULL;
362 if(i < _IOB_ENTRIES)
363 return &MSVCRT__iob[i];
365 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
366 if(!ret) {
367 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
368 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
369 ERR("out of memory\n");
370 *MSVCRT__errno() = MSVCRT_ENOMEM;
371 return NULL;
374 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
375 } else
376 ret += i%MSVCRT_FD_BLOCK_SIZE;
378 return &ret->file;
381 /* INTERNAL: free a file entry fd */
382 static void msvcrt_free_fd(int fd)
384 ioinfo *fdinfo = get_ioinfo(fd);
386 if(fdinfo != &MSVCRT___badioinfo)
388 fdinfo->handle = INVALID_HANDLE_VALUE;
389 fdinfo->wxflag = 0;
391 TRACE(":fd (%d) freed\n",fd);
393 if (fd < 3)
395 switch (fd)
397 case 0:
398 SetStdHandle(STD_INPUT_HANDLE, 0);
399 break;
400 case 1:
401 SetStdHandle(STD_OUTPUT_HANDLE, 0);
402 break;
403 case 2:
404 SetStdHandle(STD_ERROR_HANDLE, 0);
405 break;
408 release_ioinfo(fdinfo);
411 static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag)
413 fdinfo->handle = hand;
414 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_TTY));
415 fdinfo->lookahead[0] = '\n';
416 fdinfo->lookahead[1] = '\n';
417 fdinfo->lookahead[2] = '\n';
418 fdinfo->exflag &= EF_CRIT_INIT;
420 switch (fdinfo-MSVCRT___pioinfo[0])
422 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
423 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
424 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
428 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
429 static int msvcrt_alloc_fd(HANDLE hand, int flag)
431 int fd;
432 ioinfo *info = get_ioinfo_alloc(&fd);
434 TRACE(":handle (%p) allocating fd (%d)\n", hand, fd);
436 if(info == &MSVCRT___badioinfo)
437 return -1;
439 msvcrt_set_fd(info, hand, flag);
440 release_ioinfo(info);
441 return fd;
444 /* INTERNAL: Allocate a FILE* for an fd slot */
445 /* caller must hold the files lock */
446 static MSVCRT_FILE* msvcrt_alloc_fp(void)
448 int i;
449 MSVCRT_FILE *file;
451 for (i = 3; i < MSVCRT_max_streams; i++)
453 file = msvcrt_get_file(i);
454 if (!file)
455 return NULL;
457 if (file->_flag == 0)
459 if (i == MSVCRT_stream_idx)
461 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
463 InitializeCriticalSection(&((file_crit*)file)->crit);
464 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
466 MSVCRT_stream_idx++;
468 return file;
472 return NULL;
475 /* INTERNAL: initialize a FILE* from an open fd */
476 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
478 TRACE(":fd (%d) allocating FILE*\n",fd);
479 if (!(get_ioinfo_nolock(fd)->wxflag & WX_OPEN))
481 WARN(":invalid fd %d\n",fd);
482 *MSVCRT___doserrno() = 0;
483 *MSVCRT__errno() = MSVCRT_EBADF;
484 return -1;
486 file->_ptr = file->_base = NULL;
487 file->_cnt = 0;
488 file->_file = fd;
489 file->_flag = stream_flags;
490 file->_tmpfname = NULL;
492 TRACE(":got FILE* (%p)\n",file);
493 return 0;
496 /* INTERNAL: Create an inheritance data block (for spawned process)
497 * The inheritance block is made of:
498 * 00 int nb of file descriptor (NBFD)
499 * 04 char file flags (wxflag): repeated for each fd
500 * 4+NBFD HANDLE file handle: repeated for each fd
502 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
504 int fd, last_fd;
505 char* wxflag_ptr;
506 HANDLE* handle_ptr;
507 ioinfo* fdinfo;
509 for (last_fd=MSVCRT_MAX_FILES-1; last_fd>=0; last_fd--)
510 if (get_ioinfo_nolock(last_fd)->handle != INVALID_HANDLE_VALUE)
511 break;
512 last_fd++;
514 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * last_fd;
515 *block = MSVCRT_calloc(1, *size);
516 if (!*block)
518 *size = 0;
519 return FALSE;
521 wxflag_ptr = (char*)*block + sizeof(unsigned);
522 handle_ptr = (HANDLE*)(wxflag_ptr + last_fd);
524 *(unsigned*)*block = last_fd;
525 for (fd = 0; fd < last_fd; fd++)
527 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
528 fdinfo = get_ioinfo(fd);
529 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
531 *wxflag_ptr = fdinfo->wxflag;
532 *handle_ptr = fdinfo->handle;
534 else
536 *wxflag_ptr = 0;
537 *handle_ptr = INVALID_HANDLE_VALUE;
539 release_ioinfo(fdinfo);
540 wxflag_ptr++; handle_ptr++;
542 return TRUE;
545 /* INTERNAL: Set up all file descriptors,
546 * as well as default streams (stdin, stderr and stdout)
548 void msvcrt_init_io(void)
550 STARTUPINFOA si;
551 int i;
552 ioinfo *fdinfo;
554 GetStartupInfoA(&si);
555 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
557 BYTE* wxflag_ptr;
558 HANDLE* handle_ptr;
559 unsigned int count;
561 count = *(unsigned*)si.lpReserved2;
562 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
563 handle_ptr = (HANDLE*)(wxflag_ptr + count);
565 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
566 count = min(count, MSVCRT_MAX_FILES);
567 for (i = 0; i < count; i++)
569 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
571 fdinfo = get_ioinfo_alloc_fd(i);
572 if(fdinfo != &MSVCRT___badioinfo)
573 msvcrt_set_fd(fdinfo, *handle_ptr, *wxflag_ptr);
574 release_ioinfo(fdinfo);
577 wxflag_ptr++; handle_ptr++;
581 fdinfo = get_ioinfo_alloc_fd(MSVCRT_STDIN_FILENO);
582 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
583 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
584 DWORD type = GetFileType(h);
586 msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
587 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0));
589 release_ioinfo(fdinfo);
591 fdinfo = get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO);
592 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
593 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
594 DWORD type = GetFileType(h);
596 msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
597 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0));
599 release_ioinfo(fdinfo);
601 fdinfo = get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO);
602 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
603 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
604 DWORD type = GetFileType(h);
606 msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
607 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0));
609 release_ioinfo(fdinfo);
611 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO)->handle,
612 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO)->handle,
613 get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle);
615 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
616 for (i = 0; i < 3; i++)
618 /* FILE structs for stdin/out/err are static and never deleted */
619 MSVCRT__iob[i]._file = i;
620 MSVCRT__iob[i]._tmpfname = NULL;
621 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
623 MSVCRT_stream_idx = 3;
626 /* INTERNAL: Flush stdio file buffer */
627 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
629 if((file->_flag & (MSVCRT__IOREAD|MSVCRT__IOWRT)) == MSVCRT__IOWRT &&
630 file->_flag & (MSVCRT__IOMYBUF|MSVCRT__USERBUF)) {
631 int cnt=file->_ptr-file->_base;
632 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
633 file->_flag |= MSVCRT__IOERR;
634 return MSVCRT_EOF;
637 if(file->_flag & MSVCRT__IORW)
638 file->_flag &= ~MSVCRT__IOWRT;
641 file->_ptr=file->_base;
642 file->_cnt=0;
643 return 0;
646 /*********************************************************************
647 * _isatty (MSVCRT.@)
649 int CDECL MSVCRT__isatty(int fd)
651 TRACE(":fd (%d)\n",fd);
653 return get_ioinfo_nolock(fd)->wxflag & WX_TTY;
656 /* INTERNAL: Allocate stdio file buffer */
657 static BOOL msvcrt_alloc_buffer(MSVCRT_FILE* file)
659 if((file->_file==MSVCRT_STDOUT_FILENO || file->_file==MSVCRT_STDERR_FILENO)
660 && MSVCRT__isatty(file->_file))
661 return FALSE;
663 file->_base = MSVCRT_calloc(1, MSVCRT_INTERNAL_BUFSIZ);
664 if(file->_base) {
665 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
666 file->_flag |= MSVCRT__IOMYBUF;
667 } else {
668 file->_base = (char*)(&file->_charbuf);
669 file->_bufsiz = 2;
670 file->_flag |= MSVCRT__IONBF;
672 file->_ptr = file->_base;
673 file->_cnt = 0;
674 return TRUE;
677 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
678 static BOOL add_std_buffer(MSVCRT_FILE *file)
680 static char buffers[2][MSVCRT_BUFSIZ];
682 if((file->_file!=MSVCRT_STDOUT_FILENO && file->_file!=MSVCRT_STDERR_FILENO)
683 || (file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
684 || !MSVCRT__isatty(file->_file))
685 return FALSE;
687 file->_ptr = file->_base = buffers[file->_file == MSVCRT_STDOUT_FILENO ? 0 : 1];
688 file->_bufsiz = file->_cnt = MSVCRT_BUFSIZ;
689 file->_flag |= MSVCRT__USERBUF;
690 return TRUE;
693 /* INTERNAL: Removes temporary buffer from stdout or stderr */
694 /* Only call this function when add_std_buffer returned TRUE */
695 static void remove_std_buffer(MSVCRT_FILE *file)
697 msvcrt_flush_buffer(file);
698 file->_ptr = file->_base = NULL;
699 file->_bufsiz = file->_cnt = 0;
700 file->_flag &= ~MSVCRT__USERBUF;
703 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
704 static int msvcrt_int_to_base32(int num, char *str)
706 char *p;
707 int n = num;
708 int digits = 0;
710 while (n != 0)
712 n >>= 5;
713 digits++;
715 p = str + digits;
716 *p = 0;
717 while (--p >= str)
719 *p = (num & 31) + '0';
720 if (*p > '9')
721 *p += ('a' - '0' - 10);
722 num >>= 5;
725 return digits;
728 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
729 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
731 MSVCRT_wchar_t *p;
732 int n = num;
733 int digits = 0;
735 while (n != 0)
737 n >>= 5;
738 digits++;
740 p = str + digits;
741 *p = 0;
742 while (--p >= str)
744 *p = (num & 31) + '0';
745 if (*p > '9')
746 *p += ('a' - '0' - 10);
747 num >>= 5;
750 return digits;
753 /*********************************************************************
754 * __iob_func(MSVCRT.@)
756 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
758 return &MSVCRT__iob[0];
761 /*********************************************************************
762 * __acrt_iob_func(MSVCRT.@)
764 MSVCRT_FILE * CDECL MSVCRT___acrt_iob_func(unsigned idx)
766 return &MSVCRT__iob[idx];
769 /*********************************************************************
770 * _access (MSVCRT.@)
772 int CDECL MSVCRT__access(const char *filename, int mode)
774 DWORD attr = GetFileAttributesA(filename);
776 TRACE("(%s,%d) %d\n",filename,mode,attr);
778 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
780 msvcrt_set_errno(GetLastError());
781 return -1;
783 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
785 msvcrt_set_errno(ERROR_ACCESS_DENIED);
786 return -1;
788 return 0;
791 /*********************************************************************
792 * _access_s (MSVCRT.@)
794 int CDECL MSVCRT__access_s(const char *filename, int mode)
796 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
797 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
799 if (MSVCRT__access(filename, mode) == -1)
800 return *MSVCRT__errno();
801 return 0;
804 /*********************************************************************
805 * _waccess (MSVCRT.@)
807 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
809 DWORD attr = GetFileAttributesW(filename);
811 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
813 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
815 msvcrt_set_errno(GetLastError());
816 return -1;
818 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
820 msvcrt_set_errno(ERROR_ACCESS_DENIED);
821 return -1;
823 return 0;
826 /*********************************************************************
827 * _waccess_s (MSVCRT.@)
829 int CDECL MSVCRT__waccess_s(const MSVCRT_wchar_t *filename, int mode)
831 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
832 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
834 if (MSVCRT__waccess(filename, mode) == -1)
835 return *MSVCRT__errno();
836 return 0;
839 /*********************************************************************
840 * _chmod (MSVCRT.@)
842 int CDECL MSVCRT__chmod(const char *path, int flags)
844 DWORD oldFlags = GetFileAttributesA(path);
846 if (oldFlags != INVALID_FILE_ATTRIBUTES)
848 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
849 oldFlags | FILE_ATTRIBUTE_READONLY;
851 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
852 return 0;
854 msvcrt_set_errno(GetLastError());
855 return -1;
858 /*********************************************************************
859 * _wchmod (MSVCRT.@)
861 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
863 DWORD oldFlags = GetFileAttributesW(path);
865 if (oldFlags != INVALID_FILE_ATTRIBUTES)
867 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
868 oldFlags | FILE_ATTRIBUTE_READONLY;
870 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
871 return 0;
873 msvcrt_set_errno(GetLastError());
874 return -1;
877 /*********************************************************************
878 * _unlink (MSVCRT.@)
880 int CDECL MSVCRT__unlink(const char *path)
882 TRACE("%s\n",debugstr_a(path));
883 if(DeleteFileA(path))
884 return 0;
885 TRACE("failed (%d)\n",GetLastError());
886 msvcrt_set_errno(GetLastError());
887 return -1;
890 /*********************************************************************
891 * _wunlink (MSVCRT.@)
893 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
895 TRACE("(%s)\n",debugstr_w(path));
896 if(DeleteFileW(path))
897 return 0;
898 TRACE("failed (%d)\n",GetLastError());
899 msvcrt_set_errno(GetLastError());
900 return -1;
903 /*********************************************************************
904 * _commit (MSVCRT.@)
906 int CDECL MSVCRT__commit(int fd)
908 ioinfo *info = get_ioinfo(fd);
909 int ret;
911 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
913 if (info->handle == INVALID_HANDLE_VALUE)
914 ret = -1;
915 else if (!FlushFileBuffers(info->handle))
917 if (GetLastError() == ERROR_INVALID_HANDLE)
919 /* FlushFileBuffers fails for console handles
920 * so we ignore this error.
922 ret = 0;
924 else
926 TRACE(":failed-last error (%d)\n",GetLastError());
927 msvcrt_set_errno(GetLastError());
928 ret = -1;
931 else
933 TRACE(":ok\n");
934 ret = 0;
937 release_ioinfo(info);
938 return ret;
941 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
942 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
944 /* INTERNAL: Flush all stream buffer */
945 static int msvcrt_flush_all_buffers(int mask)
947 int i, num_flushed = 0;
948 MSVCRT_FILE *file;
950 LOCK_FILES();
951 for (i = 0; i < MSVCRT_stream_idx; i++) {
952 file = msvcrt_get_file(i);
954 if (file->_flag)
956 if(file->_flag & mask) {
957 MSVCRT_fflush(file);
958 num_flushed++;
962 UNLOCK_FILES();
964 TRACE(":flushed (%d) handles\n",num_flushed);
965 return num_flushed;
968 /*********************************************************************
969 * _flushall (MSVCRT.@)
971 int CDECL MSVCRT__flushall(void)
973 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
976 /*********************************************************************
977 * fflush (MSVCRT.@)
979 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
981 int ret;
983 if(!file) {
984 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
985 ret = 0;
986 } else {
987 MSVCRT__lock_file(file);
988 ret = MSVCRT__fflush_nolock(file);
989 MSVCRT__unlock_file(file);
992 return ret;
995 /*********************************************************************
996 * _fflush_nolock (MSVCRT.@)
998 int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file)
1000 int res;
1002 if(!file) {
1003 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
1004 return 0;
1007 res = msvcrt_flush_buffer(file);
1008 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
1009 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
1010 return res;
1013 /*********************************************************************
1014 * _close (MSVCRT.@)
1016 int CDECL MSVCRT__close(int fd)
1018 ioinfo *info = get_ioinfo(fd);
1019 int ret;
1021 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1022 if (!(info->wxflag & WX_OPEN)) {
1023 ret = -1;
1024 } else if (fd == MSVCRT_STDOUT_FILENO &&
1025 info->handle == get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle) {
1026 msvcrt_free_fd(fd);
1027 ret = 0;
1028 } else if (fd == MSVCRT_STDERR_FILENO &&
1029 info->handle == get_ioinfo_nolock(MSVCRT_STDOUT_FILENO)->handle) {
1030 msvcrt_free_fd(fd);
1031 ret = 0;
1032 } else {
1033 ret = CloseHandle(info->handle) ? 0 : -1;
1034 msvcrt_free_fd(fd);
1035 if (ret) {
1036 WARN(":failed-last error (%d)\n",GetLastError());
1037 msvcrt_set_errno(GetLastError());
1040 release_ioinfo(info);
1041 return ret;
1044 /*********************************************************************
1045 * _dup2 (MSVCRT.@)
1046 * NOTES
1047 * MSDN isn't clear on this point, but the remarks for _pipe
1048 * indicate file descriptors duplicated with _dup and _dup2 are always
1049 * inheritable.
1051 int CDECL MSVCRT__dup2(int od, int nd)
1053 ioinfo *info_od, *info_nd;
1054 int ret;
1056 TRACE("(od=%d, nd=%d)\n", od, nd);
1058 if (od < nd)
1060 info_od = get_ioinfo(od);
1061 info_nd = get_ioinfo_alloc_fd(nd);
1063 else
1065 info_nd = get_ioinfo_alloc_fd(nd);
1066 info_od = get_ioinfo(od);
1069 if (info_nd == &MSVCRT___badioinfo)
1071 ret = -1;
1073 else if (info_od->wxflag & WX_OPEN)
1075 HANDLE handle;
1077 if (DuplicateHandle(GetCurrentProcess(), info_od->handle,
1078 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1080 int wxflag = info_od->wxflag & ~MSVCRT__O_NOINHERIT;
1082 if (info_nd->wxflag & WX_OPEN)
1083 MSVCRT__close(nd);
1085 msvcrt_set_fd(info_nd, handle, wxflag);
1086 /* _dup2 returns 0, not nd, on success */
1087 ret = 0;
1089 else
1091 ret = -1;
1092 msvcrt_set_errno(GetLastError());
1095 else
1097 *MSVCRT__errno() = MSVCRT_EBADF;
1098 ret = -1;
1101 release_ioinfo(info_od);
1102 release_ioinfo(info_nd);
1103 return ret;
1106 /*********************************************************************
1107 * _dup (MSVCRT.@)
1109 int CDECL MSVCRT__dup(int od)
1111 int fd, ret;
1112 ioinfo *info = get_ioinfo_alloc(&fd);
1114 if (MSVCRT__dup2(od, fd) == 0)
1115 ret = fd;
1116 else
1117 ret = -1;
1118 release_ioinfo(info);
1119 return ret;
1122 /*********************************************************************
1123 * _eof (MSVCRT.@)
1125 int CDECL MSVCRT__eof(int fd)
1127 ioinfo *info = get_ioinfo(fd);
1128 DWORD curpos,endpos;
1129 LONG hcurpos,hendpos;
1131 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1133 if (info->handle == INVALID_HANDLE_VALUE)
1135 release_ioinfo(info);
1136 return -1;
1139 if (info->wxflag & WX_ATEOF)
1141 release_ioinfo(info);
1142 return TRUE;
1145 /* Otherwise we do it the hard way */
1146 hcurpos = hendpos = 0;
1147 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1148 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1150 if (curpos == endpos && hcurpos == hendpos)
1152 /* FIXME: shouldn't WX_ATEOF be set here? */
1153 release_ioinfo(info);
1154 return TRUE;
1157 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1158 release_ioinfo(info);
1159 return FALSE;
1162 /*********************************************************************
1163 * _fcloseall (MSVCRT.@)
1165 int CDECL MSVCRT__fcloseall(void)
1167 int num_closed = 0, i;
1168 MSVCRT_FILE *file;
1170 LOCK_FILES();
1171 for (i = 3; i < MSVCRT_stream_idx; i++) {
1172 file = msvcrt_get_file(i);
1174 if (file->_flag && !MSVCRT_fclose(file))
1175 num_closed++;
1177 UNLOCK_FILES();
1179 TRACE(":closed (%d) handles\n",num_closed);
1180 return num_closed;
1183 /* free everything on process exit */
1184 void msvcrt_free_io(void)
1186 unsigned int i;
1187 int j;
1189 MSVCRT__flushall();
1190 MSVCRT__fcloseall();
1192 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1194 if(!MSVCRT___pioinfo[i])
1195 continue;
1197 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1199 if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
1200 DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
1202 MSVCRT_free(MSVCRT___pioinfo[i]);
1205 for(j=0; j<MSVCRT_stream_idx; j++)
1207 MSVCRT_FILE *file = msvcrt_get_file(j);
1208 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1210 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1211 DeleteCriticalSection(&((file_crit*)file)->crit);
1215 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1216 MSVCRT_free(MSVCRT_fstream[i]);
1219 /*********************************************************************
1220 * _lseeki64 (MSVCRT.@)
1222 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1224 ioinfo *info = get_ioinfo(fd);
1225 LARGE_INTEGER ofs;
1227 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1229 if (info->handle == INVALID_HANDLE_VALUE)
1231 release_ioinfo(info);
1232 return -1;
1235 if (whence < 0 || whence > 2)
1237 release_ioinfo(info);
1238 *MSVCRT__errno() = MSVCRT_EINVAL;
1239 return -1;
1242 TRACE(":fd (%d) to %s pos %s\n",
1243 fd,wine_dbgstr_longlong(offset),
1244 (whence==MSVCRT_SEEK_SET)?"SEEK_SET":
1245 (whence==MSVCRT_SEEK_CUR)?"SEEK_CUR":
1246 (whence==MSVCRT_SEEK_END)?"SEEK_END":"UNKNOWN");
1248 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1249 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1250 ofs.QuadPart = offset;
1251 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1252 GetLastError() == ERROR_SUCCESS)
1254 info->wxflag &= ~WX_ATEOF;
1255 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1257 release_ioinfo(info);
1258 return ofs.QuadPart;
1260 release_ioinfo(info);
1261 TRACE(":error-last error (%d)\n",GetLastError());
1262 msvcrt_set_errno(GetLastError());
1263 return -1;
1266 /*********************************************************************
1267 * _lseek (MSVCRT.@)
1269 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1271 return MSVCRT__lseeki64(fd, offset, whence);
1274 /*********************************************************************
1275 * _lock_file (MSVCRT.@)
1277 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1279 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1280 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1281 else
1282 EnterCriticalSection(&((file_crit*)file)->crit);
1285 /*********************************************************************
1286 * _unlock_file (MSVCRT.@)
1288 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1290 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1291 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1292 else
1293 LeaveCriticalSection(&((file_crit*)file)->crit);
1296 /*********************************************************************
1297 * _locking (MSVCRT.@)
1299 * This is untested; the underlying LockFile doesn't work yet.
1301 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1303 ioinfo *info = get_ioinfo(fd);
1304 BOOL ret;
1305 DWORD cur_locn;
1307 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1308 if (info->handle == INVALID_HANDLE_VALUE)
1310 release_ioinfo(info);
1311 return -1;
1314 if (mode < 0 || mode > 4)
1316 release_ioinfo(info);
1317 *MSVCRT__errno() = MSVCRT_EINVAL;
1318 return -1;
1321 TRACE(":fd (%d) by 0x%08x mode %s\n",
1322 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1323 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1324 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1325 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1326 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1327 "UNKNOWN");
1329 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1331 release_ioinfo(info);
1332 FIXME ("Seek failed\n");
1333 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1334 return -1;
1336 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1338 int nretry = 10;
1339 ret = 1; /* just to satisfy gcc */
1340 while (nretry--)
1342 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1343 if (ret) break;
1344 Sleep(1);
1347 else if (mode == MSVCRT__LK_UNLCK)
1348 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1349 else
1350 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1351 /* FIXME - what about error settings? */
1352 release_ioinfo(info);
1353 return ret ? 0 : -1;
1356 /*********************************************************************
1357 * _fseeki64 (MSVCRT.@)
1359 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1361 int ret;
1363 MSVCRT__lock_file(file);
1364 ret = MSVCRT__fseeki64_nolock(file, offset, whence);
1365 MSVCRT__unlock_file(file);
1367 return ret;
1370 /*********************************************************************
1371 * _fseeki64_nolock (MSVCRT.@)
1373 int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
1375 int ret;
1377 if(whence == MSVCRT_SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1378 whence = MSVCRT_SEEK_SET;
1379 offset += MSVCRT__ftelli64_nolock(file);
1382 /* Flush output if needed */
1383 msvcrt_flush_buffer(file);
1384 /* Reset direction of i/o */
1385 if(file->_flag & MSVCRT__IORW) {
1386 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1388 /* Clear end of file flag */
1389 file->_flag &= ~MSVCRT__IOEOF;
1390 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1392 return ret;
1395 /*********************************************************************
1396 * fseek (MSVCRT.@)
1398 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1400 return MSVCRT__fseeki64( file, offset, whence );
1403 /*********************************************************************
1404 * _fseek_nolock (MSVCRT.@)
1406 int CDECL MSVCRT__fseek_nolock(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1408 return MSVCRT__fseeki64_nolock( file, offset, whence );
1411 /*********************************************************************
1412 * _chsize_s (MSVCRT.@)
1414 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1416 ioinfo *info;
1417 __int64 cur, pos;
1418 BOOL ret = FALSE;
1420 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1422 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1425 info = get_ioinfo(fd);
1426 if (info->handle != INVALID_HANDLE_VALUE)
1428 /* save the current file pointer */
1429 cur = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1430 if (cur >= 0)
1432 pos = MSVCRT__lseeki64(fd, size, MSVCRT_SEEK_SET);
1433 if (pos >= 0)
1435 ret = SetEndOfFile(info->handle);
1436 if (!ret) msvcrt_set_errno(GetLastError());
1439 /* restore the file pointer */
1440 MSVCRT__lseeki64(fd, cur, MSVCRT_SEEK_SET);
1444 release_ioinfo(info);
1445 return ret ? 0 : *MSVCRT__errno();
1448 /*********************************************************************
1449 * _chsize (MSVCRT.@)
1451 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1453 /* _chsize_s returns errno on failure but _chsize should return -1 */
1454 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1457 /*********************************************************************
1458 * clearerr (MSVCRT.@)
1460 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1462 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1464 MSVCRT__lock_file(file);
1465 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1466 MSVCRT__unlock_file(file);
1469 /*********************************************************************
1470 * rewind (MSVCRT.@)
1472 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1474 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1476 MSVCRT__lock_file(file);
1477 MSVCRT__fseek_nolock(file, 0L, MSVCRT_SEEK_SET);
1478 MSVCRT_clearerr(file);
1479 MSVCRT__unlock_file(file);
1482 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1484 int plus = strchrW(mode, '+') != NULL;
1486 TRACE("%s\n", debugstr_w(mode));
1488 while(*mode == ' ') mode++;
1490 switch(*mode++)
1492 case 'R': case 'r':
1493 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1494 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1495 break;
1496 case 'W': case 'w':
1497 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1498 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1499 break;
1500 case 'A': case 'a':
1501 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1502 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1503 break;
1504 default:
1505 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1506 return -1;
1509 *stream_flags |= MSVCRT__commode;
1511 while (*mode && *mode!=',')
1512 switch (*mode++)
1514 case 'B': case 'b':
1515 *open_flags |= MSVCRT__O_BINARY;
1516 *open_flags &= ~MSVCRT__O_TEXT;
1517 break;
1518 case 't':
1519 *open_flags |= MSVCRT__O_TEXT;
1520 *open_flags &= ~MSVCRT__O_BINARY;
1521 break;
1522 case 'D':
1523 *open_flags |= MSVCRT__O_TEMPORARY;
1524 break;
1525 case 'T':
1526 *open_flags |= MSVCRT__O_SHORT_LIVED;
1527 break;
1528 case 'c':
1529 *stream_flags |= MSVCRT__IOCOMMIT;
1530 break;
1531 case 'n':
1532 *stream_flags &= ~MSVCRT__IOCOMMIT;
1533 break;
1534 case 'N':
1535 *open_flags |= MSVCRT__O_NOINHERIT;
1536 break;
1537 case '+':
1538 case ' ':
1539 case 'a':
1540 case 'w':
1541 break;
1542 case 'S':
1543 case 'R':
1544 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1545 break;
1546 default:
1547 ERR("incorrect mode flag: %c\n", mode[-1]);
1548 break;
1551 if(*mode == ',')
1553 static const WCHAR ccs[] = {'c','c','s'};
1554 static const WCHAR utf8[] = {'u','t','f','-','8'};
1555 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1556 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1558 mode++;
1559 while(*mode == ' ') mode++;
1560 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1561 return -1;
1562 mode += sizeof(ccs)/sizeof(ccs[0]);
1563 while(*mode == ' ') mode++;
1564 if(!MSVCRT_CHECK_PMT(*mode == '='))
1565 return -1;
1566 mode++;
1567 while(*mode == ' ') mode++;
1569 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1571 *open_flags |= MSVCRT__O_U8TEXT;
1572 mode += sizeof(utf8)/sizeof(utf8[0]);
1574 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1576 *open_flags |= MSVCRT__O_U16TEXT;
1577 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1579 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1581 *open_flags |= MSVCRT__O_WTEXT;
1582 mode += sizeof(unicode)/sizeof(unicode[0]);
1584 else
1586 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1587 return -1;
1590 while(*mode == ' ') mode++;
1593 if(!MSVCRT_CHECK_PMT(*mode == 0))
1594 return -1;
1595 return 0;
1598 /*********************************************************************
1599 * _fdopen (MSVCRT.@)
1601 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1603 MSVCRT_FILE *ret;
1604 MSVCRT_wchar_t *modeW = NULL;
1606 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1608 ret = MSVCRT__wfdopen(fd, modeW);
1610 MSVCRT_free(modeW);
1611 return ret;
1614 /*********************************************************************
1615 * _wfdopen (MSVCRT.@)
1617 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1619 int open_flags, stream_flags;
1620 MSVCRT_FILE* file;
1622 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1624 LOCK_FILES();
1625 if (!(file = msvcrt_alloc_fp()))
1626 file = NULL;
1627 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1629 file->_flag = 0;
1630 file = NULL;
1632 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1633 UNLOCK_FILES();
1635 return file;
1638 /*********************************************************************
1639 * _filelength (MSVCRT.@)
1641 LONG CDECL MSVCRT__filelength(int fd)
1643 LONG curPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
1644 if (curPos != -1)
1646 LONG endPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_END);
1647 if (endPos != -1)
1649 if (endPos != curPos)
1650 MSVCRT__lseek(fd, curPos, MSVCRT_SEEK_SET);
1651 return endPos;
1654 return -1;
1657 /*********************************************************************
1658 * _filelengthi64 (MSVCRT.@)
1660 __int64 CDECL MSVCRT__filelengthi64(int fd)
1662 __int64 curPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1663 if (curPos != -1)
1665 __int64 endPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_END);
1666 if (endPos != -1)
1668 if (endPos != curPos)
1669 MSVCRT__lseeki64(fd, curPos, MSVCRT_SEEK_SET);
1670 return endPos;
1673 return -1;
1676 /*********************************************************************
1677 * _fileno (MSVCRT.@)
1679 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1681 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1682 return file->_file;
1685 /*********************************************************************
1686 * _fstat64 (MSVCRT.@)
1688 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1690 ioinfo *info = get_ioinfo(fd);
1691 DWORD dw;
1692 DWORD type;
1693 BY_HANDLE_FILE_INFORMATION hfi;
1695 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1696 if (info->handle == INVALID_HANDLE_VALUE)
1698 release_ioinfo(info);
1699 return -1;
1702 if (!buf)
1704 WARN(":failed-NULL buf\n");
1705 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1706 release_ioinfo(info);
1707 return -1;
1710 memset(&hfi, 0, sizeof(hfi));
1711 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1712 type = GetFileType(info->handle);
1713 if (type == FILE_TYPE_PIPE)
1715 buf->st_dev = buf->st_rdev = fd;
1716 buf->st_mode = MSVCRT__S_IFIFO;
1717 buf->st_nlink = 1;
1719 else if (type == FILE_TYPE_CHAR)
1721 buf->st_dev = buf->st_rdev = fd;
1722 buf->st_mode = MSVCRT__S_IFCHR;
1723 buf->st_nlink = 1;
1725 else /* FILE_TYPE_DISK etc. */
1727 if (!GetFileInformationByHandle(info->handle, &hfi))
1729 WARN(":failed-last error (%d)\n",GetLastError());
1730 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1731 release_ioinfo(info);
1732 return -1;
1734 buf->st_mode = MSVCRT__S_IFREG | 0444;
1735 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1736 buf->st_mode |= 0222;
1737 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1738 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1739 buf->st_atime = dw;
1740 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1741 buf->st_mtime = buf->st_ctime = dw;
1742 buf->st_nlink = hfi.nNumberOfLinks;
1744 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1745 buf->st_mode);
1746 release_ioinfo(info);
1747 return 0;
1750 /*********************************************************************
1751 * _fstati64 (MSVCRT.@)
1753 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1755 int ret;
1756 struct MSVCRT__stat64 buf64;
1758 ret = MSVCRT__fstat64(fd, &buf64);
1759 if (!ret)
1760 msvcrt_stat64_to_stati64(&buf64, buf);
1761 return ret;
1764 /*********************************************************************
1765 * _fstat (MSVCRT.@)
1767 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1768 { int ret;
1769 struct MSVCRT__stat64 buf64;
1771 ret = MSVCRT__fstat64(fd, &buf64);
1772 if (!ret)
1773 msvcrt_stat64_to_stat(&buf64, buf);
1774 return ret;
1777 /*********************************************************************
1778 * _fstat32 (MSVCR80.@)
1780 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1782 int ret;
1783 struct MSVCRT__stat64 buf64;
1785 ret = MSVCRT__fstat64(fd, &buf64);
1786 if (!ret)
1787 msvcrt_stat64_to_stat32(&buf64, buf);
1788 return ret;
1791 /*********************************************************************
1792 * _fstat32i64 (MSVCR80.@)
1794 int CDECL MSVCRT__fstat32i64(int fd, struct MSVCRT__stat32i64* buf)
1796 int ret;
1797 struct MSVCRT__stat64 buf64;
1799 ret = MSVCRT__fstat64(fd, &buf64);
1800 if (!ret)
1801 msvcrt_stat64_to_stat32i64(&buf64, buf);
1802 return ret;
1805 /*********************************************************************
1806 * _fstat64i32 (MSVCR80.@)
1808 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1810 int ret;
1811 struct MSVCRT__stat64 buf64;
1813 ret = MSVCRT__fstat64(fd, &buf64);
1814 if (!ret)
1815 msvcrt_stat64_to_stat64i32(&buf64, buf);
1816 return ret;
1819 /*********************************************************************
1820 * _futime64 (MSVCRT.@)
1822 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1824 ioinfo *info = get_ioinfo(fd);
1825 FILETIME at, wt;
1827 if (!t)
1829 time_to_filetime( MSVCRT__time64(NULL), &at );
1830 wt = at;
1832 else
1834 time_to_filetime( t->actime, &at );
1835 time_to_filetime( t->modtime, &wt );
1838 if (!SetFileTime(info->handle, NULL, &at, &wt))
1840 release_ioinfo(info);
1841 msvcrt_set_errno(GetLastError());
1842 return -1 ;
1844 release_ioinfo(info);
1845 return 0;
1848 /*********************************************************************
1849 * _futime32 (MSVCRT.@)
1851 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1853 if (t)
1855 struct MSVCRT___utimbuf64 t64;
1856 t64.actime = t->actime;
1857 t64.modtime = t->modtime;
1858 return _futime64( fd, &t64 );
1860 else
1861 return _futime64( fd, NULL );
1864 /*********************************************************************
1865 * _futime (MSVCRT.@)
1867 #ifdef _WIN64
1868 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1870 return _futime64( fd, t );
1872 #else
1873 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1875 return _futime32( fd, t );
1877 #endif
1879 /*********************************************************************
1880 * _get_osfhandle (MSVCRT.@)
1882 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1884 HANDLE hand = get_ioinfo_nolock(fd)->handle;
1885 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1887 if(hand == INVALID_HANDLE_VALUE)
1888 *MSVCRT__errno() = MSVCRT_EBADF;
1889 return (MSVCRT_intptr_t)hand;
1892 /*********************************************************************
1893 * _mktemp_s (MSVCRT.@)
1895 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1897 DWORD len, xno, id;
1899 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1900 return MSVCRT_EINVAL;
1902 for(len=0; len<size; len++)
1903 if(!pattern[len])
1904 break;
1905 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1906 if(size)
1907 pattern[0] = 0;
1908 return MSVCRT_EINVAL;
1911 for(xno=1; xno<=6; xno++)
1912 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1913 return MSVCRT_EINVAL;
1915 id = GetCurrentProcessId();
1916 for(xno=1; xno<6; xno++) {
1917 pattern[len-xno] = id%10 + '0';
1918 id /= 10;
1921 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1922 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1923 return 0;
1926 pattern[0] = 0;
1927 *MSVCRT__errno() = MSVCRT_EEXIST;
1928 return MSVCRT_EEXIST;
1931 /*********************************************************************
1932 * _mktemp (MSVCRT.@)
1934 char * CDECL MSVCRT__mktemp(char *pattern)
1936 int numX = 0;
1937 char *retVal = pattern;
1938 int id;
1939 char letter = 'a';
1941 if(!pattern)
1942 return NULL;
1944 while(*pattern)
1945 numX = (*pattern++ == 'X')? numX + 1 : 0;
1946 if (numX < 6)
1947 return NULL;
1948 pattern--;
1949 id = GetCurrentProcessId();
1950 numX = 6;
1951 while(numX--)
1953 int tempNum = id / 10;
1954 *pattern-- = id - (tempNum * 10) + '0';
1955 id = tempNum;
1957 pattern++;
1960 *pattern = letter++;
1961 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1962 return retVal;
1963 } while(letter <= 'z');
1964 return NULL;
1967 /*********************************************************************
1968 * _wmktemp_s (MSVCRT.@)
1970 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1972 DWORD len, xno, id;
1974 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1975 return MSVCRT_EINVAL;
1977 for(len=0; len<size; len++)
1978 if(!pattern[len])
1979 break;
1980 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1981 if(size)
1982 pattern[0] = 0;
1983 return MSVCRT_EINVAL;
1986 for(xno=1; xno<=6; xno++)
1987 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1988 return MSVCRT_EINVAL;
1990 id = GetCurrentProcessId();
1991 for(xno=1; xno<6; xno++) {
1992 pattern[len-xno] = id%10 + '0';
1993 id /= 10;
1996 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1997 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1998 return 0;
2001 pattern[0] = 0;
2002 *MSVCRT__errno() = MSVCRT_EEXIST;
2003 return MSVCRT_EEXIST;
2006 /*********************************************************************
2007 * _wmktemp (MSVCRT.@)
2009 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
2011 int numX = 0;
2012 MSVCRT_wchar_t *retVal = pattern;
2013 int id;
2014 MSVCRT_wchar_t letter = 'a';
2016 if(!pattern)
2017 return NULL;
2019 while(*pattern)
2020 numX = (*pattern++ == 'X')? numX + 1 : 0;
2021 if (numX < 6)
2022 return NULL;
2023 pattern--;
2024 id = GetCurrentProcessId();
2025 numX = 6;
2026 while(numX--)
2028 int tempNum = id / 10;
2029 *pattern-- = id - (tempNum * 10) + '0';
2030 id = tempNum;
2032 pattern++;
2035 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
2036 return retVal;
2037 *pattern = letter++;
2038 } while(letter != '|');
2039 return NULL;
2042 static unsigned split_oflags(unsigned oflags)
2044 int wxflags = 0;
2045 unsigned unsupp; /* until we support everything */
2047 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
2048 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
2049 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
2050 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
2051 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
2052 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
2053 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
2054 else wxflags |= WX_TEXT; /* default to TEXT*/
2055 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2057 if ((unsupp = oflags & ~(
2058 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
2059 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
2060 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
2061 MSVCRT__O_NOINHERIT|
2062 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
2063 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
2065 ERR(":unsupported oflags 0x%04x\n",unsupp);
2067 return wxflags;
2070 /*********************************************************************
2071 * _pipe (MSVCRT.@)
2073 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
2075 int ret = -1;
2076 SECURITY_ATTRIBUTES sa;
2077 HANDLE readHandle, writeHandle;
2079 if (!pfds)
2081 *MSVCRT__errno() = MSVCRT_EINVAL;
2082 return -1;
2085 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2086 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
2087 sa.lpSecurityDescriptor = NULL;
2088 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2090 unsigned int wxflags = split_oflags(textmode);
2091 int fd;
2093 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2094 if (fd != -1)
2096 pfds[0] = fd;
2097 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2098 if (fd != -1)
2100 pfds[1] = fd;
2101 ret = 0;
2103 else
2105 MSVCRT__close(pfds[0]);
2106 CloseHandle(writeHandle);
2107 *MSVCRT__errno() = MSVCRT_EMFILE;
2110 else
2112 CloseHandle(readHandle);
2113 CloseHandle(writeHandle);
2114 *MSVCRT__errno() = MSVCRT_EMFILE;
2117 else
2118 msvcrt_set_errno(GetLastError());
2120 return ret;
2123 static int check_bom(HANDLE h, int oflags, BOOL seek)
2125 char bom[sizeof(utf8_bom)];
2126 DWORD r;
2128 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2130 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2131 return oflags;
2133 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2134 oflags |= MSVCRT__O_U8TEXT;
2135 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2136 if (seek && r>2)
2137 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2138 oflags |= MSVCRT__O_U16TEXT;
2139 }else if (seek) {
2140 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2143 return oflags;
2146 /*********************************************************************
2147 * _wsopen_dispatch (UCRTBASE.@)
2149 int CDECL MSVCRT__wsopen_dispatch( const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode,
2150 int *fd, int secure )
2152 DWORD access = 0, creation = 0, attrib;
2153 SECURITY_ATTRIBUTES sa;
2154 DWORD sharing, type;
2155 int wxflag;
2156 HANDLE hand;
2158 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2159 debugstr_w(path), oflags, shflags, pmode, fd, secure);
2161 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2163 *fd = -1;
2164 wxflag = split_oflags(oflags);
2165 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2167 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2168 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2169 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2172 if (oflags & MSVCRT__O_CREAT)
2174 if (secure && !MSVCRT_CHECK_PMT(!(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))))
2175 return MSVCRT_EINVAL;
2177 if (oflags & MSVCRT__O_EXCL)
2178 creation = CREATE_NEW;
2179 else if (oflags & MSVCRT__O_TRUNC)
2180 creation = CREATE_ALWAYS;
2181 else
2182 creation = OPEN_ALWAYS;
2184 else /* no MSVCRT__O_CREAT */
2186 if (oflags & MSVCRT__O_TRUNC)
2187 creation = TRUNCATE_EXISTING;
2188 else
2189 creation = OPEN_EXISTING;
2192 switch( shflags )
2194 case MSVCRT__SH_DENYRW:
2195 sharing = 0L;
2196 break;
2197 case MSVCRT__SH_DENYWR:
2198 sharing = FILE_SHARE_READ;
2199 break;
2200 case MSVCRT__SH_DENYRD:
2201 sharing = FILE_SHARE_WRITE;
2202 break;
2203 case MSVCRT__SH_DENYNO:
2204 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2205 break;
2206 default:
2207 ERR( "Unhandled shflags 0x%x\n", shflags );
2208 return MSVCRT_EINVAL;
2211 if (!(pmode & ~MSVCRT_umask & MSVCRT__S_IWRITE))
2212 attrib = FILE_ATTRIBUTE_READONLY;
2213 else
2214 attrib = FILE_ATTRIBUTE_NORMAL;
2216 if (oflags & MSVCRT__O_TEMPORARY)
2218 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2219 access |= DELETE;
2220 sharing |= FILE_SHARE_DELETE;
2223 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2224 sa.lpSecurityDescriptor = NULL;
2225 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2227 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2228 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2229 && !(access&GENERIC_READ))
2231 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2232 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2233 if (hand != INVALID_HANDLE_VALUE)
2235 oflags = check_bom(hand, oflags, FALSE);
2236 CloseHandle(hand);
2238 else
2239 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2242 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2243 if (hand == INVALID_HANDLE_VALUE) {
2244 WARN(":failed-last error (%d)\n",GetLastError());
2245 msvcrt_set_errno(GetLastError());
2246 return *MSVCRT__errno();
2249 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2251 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2252 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2253 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2255 if (oflags & MSVCRT__O_U8TEXT)
2257 DWORD written = 0, tmp;
2259 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2260 sizeof(utf8_bom)-written, &tmp, NULL))
2261 written += tmp;
2262 if (written != sizeof(utf8_bom)) {
2263 WARN("error writing BOM\n");
2264 CloseHandle(hand);
2265 msvcrt_set_errno(GetLastError());
2266 return *MSVCRT__errno();
2269 else
2271 DWORD written = 0, tmp;
2273 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2274 sizeof(utf16_bom)-written, &tmp, NULL))
2275 written += tmp;
2276 if (written != sizeof(utf16_bom))
2278 WARN("error writing BOM\n");
2279 CloseHandle(hand);
2280 msvcrt_set_errno(GetLastError());
2281 return *MSVCRT__errno();
2285 else if (access & GENERIC_READ)
2286 oflags = check_bom(hand, oflags, TRUE);
2289 type = GetFileType(hand);
2290 if (type == FILE_TYPE_CHAR)
2291 wxflag |= WX_TTY;
2292 else if (type == FILE_TYPE_PIPE)
2293 wxflag |= WX_PIPE;
2295 *fd = msvcrt_alloc_fd(hand, wxflag);
2296 if (*fd == -1)
2297 return *MSVCRT__errno();
2299 if (oflags & MSVCRT__O_WTEXT)
2300 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2301 else if (oflags & MSVCRT__O_U16TEXT)
2302 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
2303 else if (oflags & MSVCRT__O_U8TEXT)
2304 get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
2306 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2307 return 0;
2311 /*********************************************************************
2312 * _wsopen_s (MSVCRT.@)
2314 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2316 return MSVCRT__wsopen_dispatch( path, oflags, shflags, pmode, fd, 1 );
2319 /*********************************************************************
2320 * _wsopen (MSVCRT.@)
2322 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2324 int pmode;
2325 int fd;
2327 if (oflags & MSVCRT__O_CREAT)
2329 __ms_va_list ap;
2331 __ms_va_start(ap, shflags);
2332 pmode = va_arg(ap, int);
2333 __ms_va_end(ap);
2335 else
2336 pmode = 0;
2338 return MSVCRT__wsopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2342 /*********************************************************************
2343 * _sopen_dispatch (UCRTBASE.@)
2345 int CDECL MSVCRT__sopen_dispatch( const char *path, int oflags, int shflags,
2346 int pmode, int *fd, int secure)
2348 MSVCRT_wchar_t *pathW;
2349 int ret;
2351 if (!MSVCRT_CHECK_PMT(fd != NULL))
2352 return MSVCRT_EINVAL;
2353 *fd = -1;
2354 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2355 return MSVCRT_EINVAL;
2357 ret = MSVCRT__wsopen_dispatch(pathW, oflags, shflags, pmode, fd, secure);
2358 MSVCRT_free(pathW);
2359 return ret;
2362 /*********************************************************************
2363 * _sopen_s (MSVCRT.@)
2365 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2367 return MSVCRT__sopen_dispatch(path, oflags, shflags, pmode, fd, 1);
2370 /*********************************************************************
2371 * _sopen (MSVCRT.@)
2373 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2375 int pmode;
2376 int fd;
2378 if (oflags & MSVCRT__O_CREAT)
2380 __ms_va_list ap;
2382 __ms_va_start(ap, shflags);
2383 pmode = va_arg(ap, int);
2384 __ms_va_end(ap);
2386 else
2387 pmode = 0;
2389 return MSVCRT__sopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2392 /*********************************************************************
2393 * _open (MSVCRT.@)
2395 int CDECL MSVCRT__open( const char *path, int flags, ... )
2397 __ms_va_list ap;
2399 if (flags & MSVCRT__O_CREAT)
2401 int pmode;
2402 __ms_va_start(ap, flags);
2403 pmode = va_arg(ap, int);
2404 __ms_va_end(ap);
2405 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2407 else
2408 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2411 /*********************************************************************
2412 * _wopen (MSVCRT.@)
2414 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2416 __ms_va_list ap;
2418 if (flags & MSVCRT__O_CREAT)
2420 int pmode;
2421 __ms_va_start(ap, flags);
2422 pmode = va_arg(ap, int);
2423 __ms_va_end(ap);
2424 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2426 else
2427 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2430 /*********************************************************************
2431 * _creat (MSVCRT.@)
2433 int CDECL MSVCRT__creat(const char *path, int pmode)
2435 int flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | MSVCRT__O_RDWR;
2436 return MSVCRT__open(path, flags, pmode);
2439 /*********************************************************************
2440 * _wcreat (MSVCRT.@)
2442 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int pmode)
2444 int flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | MSVCRT__O_RDWR;
2445 return MSVCRT__wopen(path, flags, pmode);
2448 /*********************************************************************
2449 * _open_osfhandle (MSVCRT.@)
2451 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2453 DWORD flags;
2454 int fd;
2456 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2457 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2458 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2459 * text - it never sets MSVCRT__O_BINARY.
2461 /* don't let split_oflags() decide the mode if no mode is passed */
2462 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2463 oflags |= MSVCRT__O_BINARY;
2465 flags = GetFileType((HANDLE)handle);
2466 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2468 msvcrt_set_errno(GetLastError());
2469 return -1;
2472 if (flags == FILE_TYPE_CHAR)
2473 flags = WX_TTY;
2474 else if (flags == FILE_TYPE_PIPE)
2475 flags = WX_PIPE;
2476 else
2477 flags = 0;
2478 flags |= split_oflags(oflags);
2480 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2481 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2482 return fd;
2485 /*********************************************************************
2486 * _rmtmp (MSVCRT.@)
2488 int CDECL MSVCRT__rmtmp(void)
2490 int num_removed = 0, i;
2491 MSVCRT_FILE *file;
2493 LOCK_FILES();
2494 for (i = 3; i < MSVCRT_stream_idx; i++) {
2495 file = msvcrt_get_file(i);
2497 if (file->_tmpfname)
2499 MSVCRT_fclose(file);
2500 num_removed++;
2503 UNLOCK_FILES();
2505 if (num_removed)
2506 TRACE(":removed (%d) temp files\n",num_removed);
2507 return num_removed;
2510 static inline int get_utf8_char_len(char ch)
2512 if((ch&0xf8) == 0xf0)
2513 return 4;
2514 else if((ch&0xf0) == 0xe0)
2515 return 3;
2516 else if((ch&0xe0) == 0xc0)
2517 return 2;
2518 return 1;
2521 /*********************************************************************
2522 * (internal) read_utf8
2524 static int read_utf8(ioinfo *fdinfo, MSVCRT_wchar_t *buf, unsigned int count)
2526 HANDLE hand = fdinfo->handle;
2527 char min_buf[4], *readbuf, lookahead;
2528 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2530 /* make the buffer big enough to hold at least one character */
2531 /* read bytes have to fit to output and lookahead buffers */
2532 count /= 2;
2533 readbuf_size = count < 4 ? 4 : count;
2534 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2535 readbuf_size = 4;
2536 readbuf = min_buf;
2539 if(fdinfo->lookahead[0] != '\n') {
2540 readbuf[pos++] = fdinfo->lookahead[0];
2541 fdinfo->lookahead[0] = '\n';
2543 if(fdinfo->lookahead[1] != '\n') {
2544 readbuf[pos++] = fdinfo->lookahead[1];
2545 fdinfo->lookahead[1] = '\n';
2547 if(fdinfo->lookahead[2] != '\n') {
2548 readbuf[pos++] = fdinfo->lookahead[2];
2549 fdinfo->lookahead[2] = '\n';
2554 /* NOTE: this case is broken in native dll, reading
2555 * sometimes fails when small buffer is passed
2557 if(count < 4) {
2558 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2559 if (GetLastError() == ERROR_BROKEN_PIPE) {
2560 fdinfo->wxflag |= WX_ATEOF;
2561 return 0;
2562 }else {
2563 msvcrt_set_errno(GetLastError());
2564 return -1;
2566 }else if(!num_read) {
2567 fdinfo->wxflag |= WX_ATEOF;
2568 return 0;
2569 }else {
2570 pos++;
2573 char_len = get_utf8_char_len(readbuf[0]);
2574 if(char_len>pos) {
2575 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2576 pos += num_read;
2579 if(readbuf[0] == '\n')
2580 fdinfo->wxflag |= WX_READNL;
2581 else
2582 fdinfo->wxflag &= ~WX_READNL;
2584 if(readbuf[0] == 0x1a) {
2585 fdinfo->wxflag |= WX_ATEOF;
2586 return 0;
2589 if(readbuf[0] == '\r') {
2590 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2591 buf[0] = '\r';
2592 else if(lookahead == '\n')
2593 buf[0] = '\n';
2594 else {
2595 buf[0] = '\r';
2596 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2597 fdinfo->lookahead[0] = lookahead;
2598 else
2599 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2601 return 2;
2604 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2605 msvcrt_set_errno(GetLastError());
2606 return -1;
2609 return num_read*2;
2612 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2613 if(pos) {
2614 num_read = 0;
2615 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2616 fdinfo->wxflag |= WX_ATEOF;
2617 if (readbuf != min_buf) MSVCRT_free(readbuf);
2618 return 0;
2619 }else {
2620 msvcrt_set_errno(GetLastError());
2621 if (readbuf != min_buf) MSVCRT_free(readbuf);
2622 return -1;
2624 }else if(!pos && !num_read) {
2625 fdinfo->wxflag |= WX_ATEOF;
2626 if (readbuf != min_buf) MSVCRT_free(readbuf);
2627 return 0;
2630 pos += num_read;
2631 if(readbuf[0] == '\n')
2632 fdinfo->wxflag |= WX_READNL;
2633 else
2634 fdinfo->wxflag &= ~WX_READNL;
2636 /* Find first byte of last character (may be incomplete) */
2637 for(i=pos-1; i>0 && i>pos-4; i--)
2638 if((readbuf[i]&0xc0) != 0x80)
2639 break;
2640 char_len = get_utf8_char_len(readbuf[i]);
2641 if(char_len+i <= pos)
2642 i += char_len;
2644 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2645 if(i < pos)
2646 fdinfo->lookahead[0] = readbuf[i];
2647 if(i+1 < pos)
2648 fdinfo->lookahead[1] = readbuf[i+1];
2649 if(i+2 < pos)
2650 fdinfo->lookahead[2] = readbuf[i+2];
2651 }else if(i < pos) {
2652 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2654 pos = i;
2656 for(i=0, j=0; i<pos; i++) {
2657 if(readbuf[i] == 0x1a) {
2658 fdinfo->wxflag |= WX_ATEOF;
2659 break;
2662 /* strip '\r' if followed by '\n' */
2663 if(readbuf[i] == '\r' && i+1==pos) {
2664 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2665 readbuf[j++] = '\r';
2666 }else if(lookahead == '\n' && j==0) {
2667 readbuf[j++] = '\n';
2668 }else {
2669 if(lookahead != '\n')
2670 readbuf[j++] = '\r';
2672 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2673 fdinfo->lookahead[0] = lookahead;
2674 else
2675 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2677 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2678 readbuf[j++] = readbuf[i];
2681 pos = j;
2683 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2684 msvcrt_set_errno(GetLastError());
2685 if (readbuf != min_buf) MSVCRT_free(readbuf);
2686 return -1;
2689 if (readbuf != min_buf) MSVCRT_free(readbuf);
2690 return num_read*2;
2693 /*********************************************************************
2694 * (internal) read_i
2696 * When reading \r as last character in text mode, read() positions
2697 * the file pointer on the \r character while getc() goes on to
2698 * the following \n
2700 static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
2702 DWORD num_read, utf16;
2703 char *bufstart = buf;
2705 if (count == 0)
2706 return 0;
2708 if (fdinfo->wxflag & WX_ATEOF) {
2709 TRACE("already at EOF, returning 0\n");
2710 return 0;
2712 /* Don't trace small reads, it gets *very* annoying */
2713 if (count > 4)
2714 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2715 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2717 *MSVCRT__errno() = MSVCRT_EBADF;
2718 return -1;
2721 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2722 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2724 *MSVCRT__errno() = MSVCRT_EINVAL;
2725 return -1;
2728 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2729 return read_utf8(fdinfo, buf, count);
2731 if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
2733 if (fdinfo->lookahead[0] != '\n')
2735 bufstart[0] = fdinfo->lookahead[0];
2736 fdinfo->lookahead[0] = '\n';
2738 if (utf16)
2740 bufstart[1] = fdinfo->lookahead[1];
2741 fdinfo->lookahead[1] = '\n';
2744 if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2745 num_read += 1+utf16;
2746 else
2747 num_read = 1+utf16;
2750 if(utf16 && (num_read&1))
2752 /* msvcr90 uses uninitialized value from the buffer in this case */
2753 /* msvcrt ignores additional data */
2754 ERR("got odd number of bytes in UTF16 mode\n");
2755 num_read--;
2758 if (count != 0 && num_read == 0)
2760 fdinfo->wxflag |= WX_ATEOF;
2761 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2763 else if (fdinfo->wxflag & WX_TEXT)
2765 DWORD i, j;
2767 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2768 fdinfo->wxflag |= WX_READNL;
2769 else
2770 fdinfo->wxflag &= ~WX_READNL;
2772 for (i=0, j=0; i<num_read; i+=1+utf16)
2774 /* in text mode, a ctrl-z signals EOF */
2775 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2777 fdinfo->wxflag |= WX_ATEOF;
2778 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2779 break;
2782 /* in text mode, strip \r if followed by \n */
2783 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2785 char lookahead[2];
2786 DWORD len;
2788 lookahead[1] = '\n';
2789 if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
2791 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2793 bufstart[j++] = '\n';
2794 if(utf16) bufstart[j++] = 0;
2796 else
2798 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2800 bufstart[j++] = '\r';
2801 if(utf16) bufstart[j++] = 0;
2804 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2806 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2808 bufstart[j++] = '\n';
2809 if (utf16) bufstart[j++] = 0;
2811 else
2813 fdinfo->lookahead[0] = lookahead[0];
2814 fdinfo->lookahead[1] = lookahead[1];
2817 else
2818 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2821 else
2823 bufstart[j++] = '\r';
2824 if(utf16) bufstart[j++] = 0;
2827 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2828 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2830 bufstart[j++] = bufstart[i];
2831 if(utf16) bufstart[j++] = bufstart[i+1];
2834 num_read = j;
2837 else
2839 if (GetLastError() == ERROR_BROKEN_PIPE)
2841 TRACE(":end-of-pipe\n");
2842 fdinfo->wxflag |= WX_ATEOF;
2843 return 0;
2845 else
2847 TRACE(":failed-last error (%d)\n",GetLastError());
2848 return -1;
2852 if (count > 4)
2853 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2854 return num_read;
2857 /*********************************************************************
2858 * _read (MSVCRT.@)
2860 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2862 ioinfo *info = get_ioinfo(fd);
2863 int num_read = read_i(fd, info, buf, count);
2864 release_ioinfo(info);
2865 return num_read;
2868 /*********************************************************************
2869 * _setmode (MSVCRT.@)
2871 int CDECL MSVCRT__setmode(int fd,int mode)
2873 ioinfo *info = get_ioinfo(fd);
2874 int ret = info->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2875 if(ret==MSVCRT__O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16)))
2876 ret = MSVCRT__O_WTEXT;
2878 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2879 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2880 *MSVCRT__errno() = MSVCRT_EINVAL;
2881 release_ioinfo(info);
2882 return -1;
2885 if(info == &MSVCRT___badioinfo) {
2886 *MSVCRT__errno() = MSVCRT_EBADF;
2887 return MSVCRT_EOF;
2890 if(mode == MSVCRT__O_BINARY) {
2891 info->wxflag &= ~WX_TEXT;
2892 info->exflag &= ~(EF_UTF8|EF_UTF16);
2893 release_ioinfo(info);
2894 return ret;
2897 info->wxflag |= WX_TEXT;
2898 if(mode == MSVCRT__O_TEXT)
2899 info->exflag &= ~(EF_UTF8|EF_UTF16);
2900 else if(mode == MSVCRT__O_U8TEXT)
2901 info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
2902 else
2903 info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
2905 release_ioinfo(info);
2906 return ret;
2909 /*********************************************************************
2910 * _stat64 (MSVCRT.@)
2912 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2914 DWORD dw;
2915 WIN32_FILE_ATTRIBUTE_DATA hfi;
2916 unsigned short mode = ALL_S_IREAD;
2917 int plen;
2919 TRACE(":file (%s) buf(%p)\n",path,buf);
2921 plen = strlen(path);
2922 while (plen && path[plen-1]==' ')
2923 plen--;
2925 if (plen && (plen<2 || path[plen-2]!=':') &&
2926 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2928 *MSVCRT__errno() = MSVCRT_ENOENT;
2929 return -1;
2932 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2934 TRACE("failed (%d)\n",GetLastError());
2935 *MSVCRT__errno() = MSVCRT_ENOENT;
2936 return -1;
2939 memset(buf,0,sizeof(struct MSVCRT__stat64));
2941 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2942 Bon 011120: This FIXME seems incorrect
2943 Also a letter as first char isn't enough to be classified
2944 as a drive letter
2946 if (isalpha(*path)&& (*(path+1)==':'))
2947 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2948 else
2949 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2951 /* Dir, or regular file? */
2952 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2953 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2954 else
2956 mode |= MSVCRT__S_IFREG;
2957 /* executable? */
2958 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2960 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2961 (tolower(path[plen-3]) << 16);
2962 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2963 mode |= ALL_S_IEXEC;
2967 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2968 mode |= ALL_S_IWRITE;
2970 buf->st_mode = mode;
2971 buf->st_nlink = 1;
2972 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2973 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2974 buf->st_atime = dw;
2975 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2976 buf->st_mtime = buf->st_ctime = dw;
2977 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2978 (int)(buf->st_size >> 32),(int)buf->st_size,
2979 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2980 return 0;
2983 /*********************************************************************
2984 * _stati64 (MSVCRT.@)
2986 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2988 int ret;
2989 struct MSVCRT__stat64 buf64;
2991 ret = MSVCRT_stat64(path, &buf64);
2992 if (!ret)
2993 msvcrt_stat64_to_stati64(&buf64, buf);
2994 return ret;
2997 /*********************************************************************
2998 * _stat (MSVCRT.@)
3000 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
3002 int ret;
3003 struct MSVCRT__stat64 buf64;
3005 ret = MSVCRT_stat64( path, &buf64);
3006 if (!ret)
3007 msvcrt_stat64_to_stat(&buf64, buf);
3008 return ret;
3011 /*********************************************************************
3012 * _stat32 (MSVCR100.@)
3014 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
3016 int ret;
3017 struct MSVCRT__stat64 buf64;
3019 ret = MSVCRT_stat64(path, &buf64);
3020 if (!ret)
3021 msvcrt_stat64_to_stat32(&buf64, buf);
3022 return ret;
3025 /*********************************************************************
3026 * _stat32i64 (MSVCR100.@)
3028 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
3030 int ret;
3031 struct MSVCRT__stat64 buf64;
3033 ret = MSVCRT_stat64(path, &buf64);
3034 if (!ret)
3035 msvcrt_stat64_to_stat32i64(&buf64, buf);
3036 return ret;
3039 /*********************************************************************
3040 * _stat64i32 (MSVCR100.@)
3042 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
3044 int ret;
3045 struct MSVCRT__stat64 buf64;
3047 ret = MSVCRT_stat64(path, &buf64);
3048 if (!ret)
3049 msvcrt_stat64_to_stat64i32(&buf64, buf);
3050 return ret;
3053 /*********************************************************************
3054 * _wstat64 (MSVCRT.@)
3056 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
3058 DWORD dw;
3059 WIN32_FILE_ATTRIBUTE_DATA hfi;
3060 unsigned short mode = ALL_S_IREAD;
3061 int plen;
3063 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
3065 plen = strlenW(path);
3066 while (plen && path[plen-1]==' ')
3067 plen--;
3069 if(plen && (plen<2 || path[plen-2]!=':') &&
3070 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
3072 *MSVCRT__errno() = MSVCRT_ENOENT;
3073 return -1;
3076 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
3078 TRACE("failed (%d)\n",GetLastError());
3079 *MSVCRT__errno() = MSVCRT_ENOENT;
3080 return -1;
3083 memset(buf,0,sizeof(struct MSVCRT__stat64));
3085 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3086 if (MSVCRT_iswalpha(*path))
3087 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
3088 else
3089 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3091 /* Dir, or regular file? */
3092 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3093 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3094 else
3096 mode |= MSVCRT__S_IFREG;
3097 /* executable? */
3098 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3100 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
3101 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
3102 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3103 mode |= ALL_S_IEXEC;
3107 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3108 mode |= ALL_S_IWRITE;
3110 buf->st_mode = mode;
3111 buf->st_nlink = 1;
3112 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3113 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3114 buf->st_atime = dw;
3115 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3116 buf->st_mtime = buf->st_ctime = dw;
3117 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3118 (int)(buf->st_size >> 32),(int)buf->st_size,
3119 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3120 return 0;
3123 /*********************************************************************
3124 * _wstati64 (MSVCRT.@)
3126 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
3128 int ret;
3129 struct MSVCRT__stat64 buf64;
3131 ret = MSVCRT__wstat64(path, &buf64);
3132 if (!ret)
3133 msvcrt_stat64_to_stati64(&buf64, buf);
3134 return ret;
3137 /*********************************************************************
3138 * _wstat (MSVCRT.@)
3140 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
3142 int ret;
3143 struct MSVCRT__stat64 buf64;
3145 ret = MSVCRT__wstat64( path, &buf64 );
3146 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3147 return ret;
3150 /*********************************************************************
3151 * _wstat32 (MSVCR100.@)
3153 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
3155 int ret;
3156 struct MSVCRT__stat64 buf64;
3158 ret = MSVCRT__wstat64(path, &buf64);
3159 if (!ret)
3160 msvcrt_stat64_to_stat32(&buf64, buf);
3161 return ret;
3164 /*********************************************************************
3165 * _wstat32i64 (MSVCR100.@)
3167 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
3169 int ret;
3170 struct MSVCRT__stat64 buf64;
3172 ret = MSVCRT__wstat64(path, &buf64);
3173 if (!ret)
3174 msvcrt_stat64_to_stat32i64(&buf64, buf);
3175 return ret;
3178 /*********************************************************************
3179 * _wstat64i32 (MSVCR100.@)
3181 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3183 int ret;
3184 struct MSVCRT__stat64 buf64;
3186 ret = MSVCRT__wstat64(path, &buf64);
3187 if (!ret)
3188 msvcrt_stat64_to_stat64i32(&buf64, buf);
3189 return ret;
3192 /*********************************************************************
3193 * _tell (MSVCRT.@)
3195 MSVCRT_long CDECL MSVCRT__tell(int fd)
3197 return MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
3200 /*********************************************************************
3201 * _telli64 (MSVCRT.@)
3203 __int64 CDECL _telli64(int fd)
3205 return MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
3208 /*********************************************************************
3209 * _tempnam (MSVCRT.@)
3211 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3213 char tmpbuf[MAX_PATH];
3214 const char *tmp_dir = MSVCRT_getenv("TMP");
3216 if (tmp_dir) dir = tmp_dir;
3218 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3219 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3221 TRACE("got name (%s)\n",tmpbuf);
3222 DeleteFileA(tmpbuf);
3223 return MSVCRT__strdup(tmpbuf);
3225 TRACE("failed (%d)\n",GetLastError());
3226 return NULL;
3229 /*********************************************************************
3230 * _wtempnam (MSVCRT.@)
3232 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3234 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3235 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3236 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3238 if (tmp_dir) dir = tmp_dir;
3240 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3241 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3243 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3244 DeleteFileW(tmpbuf);
3245 return MSVCRT__wcsdup(tmpbuf);
3247 TRACE("failed (%d)\n",GetLastError());
3248 return NULL;
3251 /*********************************************************************
3252 * _umask (MSVCRT.@)
3254 int CDECL MSVCRT__umask(int umask)
3256 int old_umask = MSVCRT_umask;
3257 TRACE("(%d)\n",umask);
3258 MSVCRT_umask = umask;
3259 return old_umask;
3262 /*********************************************************************
3263 * _utime64 (MSVCRT.@)
3265 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3267 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3269 if (fd > 0)
3271 int retVal = _futime64(fd, t);
3272 MSVCRT__close(fd);
3273 return retVal;
3275 return -1;
3278 /*********************************************************************
3279 * _utime32 (MSVCRT.@)
3281 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3283 if (t)
3285 struct MSVCRT___utimbuf64 t64;
3286 t64.actime = t->actime;
3287 t64.modtime = t->modtime;
3288 return _utime64( path, &t64 );
3290 else
3291 return _utime64( path, NULL );
3294 /*********************************************************************
3295 * _utime (MSVCRT.@)
3297 #ifdef _WIN64
3298 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3300 return _utime64( path, t );
3302 #else
3303 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3305 return _utime32( path, t );
3307 #endif
3309 /*********************************************************************
3310 * _wutime64 (MSVCRT.@)
3312 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3314 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3316 if (fd > 0)
3318 int retVal = _futime64(fd, t);
3319 MSVCRT__close(fd);
3320 return retVal;
3322 return -1;
3325 /*********************************************************************
3326 * _wutime32 (MSVCRT.@)
3328 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3330 if (t)
3332 struct MSVCRT___utimbuf64 t64;
3333 t64.actime = t->actime;
3334 t64.modtime = t->modtime;
3335 return _wutime64( path, &t64 );
3337 else
3338 return _wutime64( path, NULL );
3341 /*********************************************************************
3342 * _wutime (MSVCRT.@)
3344 #ifdef _WIN64
3345 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3347 return _wutime64( path, t );
3349 #else
3350 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3352 return _wutime32( path, t );
3354 #endif
3356 /*********************************************************************
3357 * _write (MSVCRT.@)
3359 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3361 DWORD num_written;
3362 ioinfo *info = get_ioinfo(fd);
3363 HANDLE hand = info->handle;
3365 /* Don't trace small writes, it gets *very* annoying */
3366 #if 0
3367 if (count > 32)
3368 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3369 #endif
3370 if (hand == INVALID_HANDLE_VALUE)
3372 *MSVCRT__errno() = MSVCRT_EBADF;
3373 release_ioinfo(info);
3374 return -1;
3377 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3379 *MSVCRT__errno() = MSVCRT_EINVAL;
3380 release_ioinfo(info);
3381 return -1;
3384 /* If appending, go to EOF */
3385 if (info->wxflag & WX_APPEND)
3386 MSVCRT__lseek(fd, 0, FILE_END);
3388 if (!(info->wxflag & WX_TEXT))
3390 if (WriteFile(hand, buf, count, &num_written, NULL)
3391 && (num_written == count))
3393 release_ioinfo(info);
3394 return num_written;
3396 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3397 hand, GetLastError());
3398 *MSVCRT__errno() = MSVCRT_ENOSPC;
3400 else
3402 unsigned int i, j, nr_lf, size;
3403 char *p = NULL;
3404 const char *q;
3405 const char *s = buf, *buf_start = buf;
3407 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3409 /* find number of \n */
3410 for (nr_lf=0, i=0; i<count; i++)
3411 if (s[i] == '\n')
3412 nr_lf++;
3413 if (nr_lf)
3415 size = count+nr_lf;
3416 if ((q = p = MSVCRT_malloc(size)))
3418 for (s = buf, i = 0, j = 0; i < count; i++)
3420 if (s[i] == '\n')
3421 p[j++] = '\r';
3422 p[j++] = s[i];
3425 else
3427 FIXME("Malloc failed\n");
3428 nr_lf = 0;
3429 size = count;
3430 q = buf;
3433 else
3435 size = count;
3436 q = buf;
3439 else if (info->exflag & EF_UTF16)
3441 for (nr_lf=0, i=0; i<count; i+=2)
3442 if (s[i]=='\n' && s[i+1]==0)
3443 nr_lf += 2;
3444 if (nr_lf)
3446 size = count+nr_lf;
3447 if ((q = p = MSVCRT_malloc(size)))
3449 for (s=buf, i=0, j=0; i<count; i++)
3451 if (s[i]=='\n' && s[i+1]==0)
3453 p[j++] = '\r';
3454 p[j++] = 0;
3456 p[j++] = s[i++];
3457 p[j++] = s[i];
3460 else
3462 FIXME("Malloc failed\n");
3463 nr_lf = 0;
3464 size = count;
3465 q = buf;
3468 else
3470 size = count;
3471 q = buf;
3474 else
3476 DWORD conv_len;
3478 for(nr_lf=0, i=0; i<count; i+=2)
3479 if (s[i]=='\n' && s[i+1]==0)
3480 nr_lf++;
3482 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3483 if(!conv_len) {
3484 msvcrt_set_errno(GetLastError());
3485 MSVCRT_free(p);
3486 release_ioinfo(info);
3487 return -1;
3490 size = conv_len+nr_lf;
3491 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3493 for (s=buf, i=0, j=0; i<count; i++)
3495 if (s[i]=='\n' && s[i+1]==0)
3497 p[j++] = '\r';
3498 p[j++] = 0;
3500 p[j++] = s[i++];
3501 p[j++] = s[i];
3503 q = p+count+nr_lf*2;
3504 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3505 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3507 else
3509 FIXME("Malloc failed\n");
3510 nr_lf = 0;
3511 size = count;
3512 q = buf;
3516 if (!WriteFile(hand, q, size, &num_written, NULL))
3517 num_written = -1;
3518 release_ioinfo(info);
3519 MSVCRT_free(p);
3520 if (num_written != size)
3522 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3523 fd, hand, GetLastError(), num_written);
3524 *MSVCRT__errno() = MSVCRT_ENOSPC;
3525 return s - buf_start;
3527 return count;
3530 release_ioinfo(info);
3531 return -1;
3534 /*********************************************************************
3535 * _putw (MSVCRT.@)
3537 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3539 int len;
3541 MSVCRT__lock_file(file);
3542 len = MSVCRT__write(file->_file, &val, sizeof(val));
3543 if (len == sizeof(val)) {
3544 MSVCRT__unlock_file(file);
3545 return val;
3548 file->_flag |= MSVCRT__IOERR;
3549 MSVCRT__unlock_file(file);
3550 return MSVCRT_EOF;
3553 /*********************************************************************
3554 * fclose (MSVCRT.@)
3556 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3558 int ret;
3560 MSVCRT__lock_file(file);
3561 ret = MSVCRT__fclose_nolock(file);
3562 MSVCRT__unlock_file(file);
3564 return ret;
3567 /*********************************************************************
3568 * _fclose_nolock (MSVCRT.@)
3570 int CDECL MSVCRT__fclose_nolock(MSVCRT_FILE* file)
3572 int r, flag;
3574 flag = file->_flag;
3575 MSVCRT_free(file->_tmpfname);
3576 file->_tmpfname = NULL;
3577 /* flush stdio buffers */
3578 if(file->_flag & MSVCRT__IOWRT)
3579 MSVCRT__fflush_nolock(file);
3580 if(file->_flag & MSVCRT__IOMYBUF)
3581 MSVCRT_free(file->_base);
3583 r=MSVCRT__close(file->_file);
3584 file->_flag = 0;
3586 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3589 /*********************************************************************
3590 * feof (MSVCRT.@)
3592 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3594 return file->_flag & MSVCRT__IOEOF;
3597 /*********************************************************************
3598 * ferror (MSVCRT.@)
3600 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3602 return file->_flag & MSVCRT__IOERR;
3605 /*********************************************************************
3606 * _filbuf (MSVCRT.@)
3608 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3610 unsigned char c;
3612 if(file->_flag & MSVCRT__IOSTRG)
3613 return MSVCRT_EOF;
3615 /* Allocate buffer if needed */
3616 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3617 msvcrt_alloc_buffer(file);
3619 if(!(file->_flag & MSVCRT__IOREAD)) {
3620 if(file->_flag & MSVCRT__IORW)
3621 file->_flag |= MSVCRT__IOREAD;
3622 else
3623 return MSVCRT_EOF;
3626 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3627 int r;
3628 if ((r = MSVCRT__read(file->_file,&c,1)) != 1) {
3629 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3630 return MSVCRT_EOF;
3633 return c;
3634 } else {
3635 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3636 if(file->_cnt<=0) {
3637 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3638 file->_cnt = 0;
3639 return MSVCRT_EOF;
3642 file->_cnt--;
3643 file->_ptr = file->_base+1;
3644 c = *(unsigned char *)file->_base;
3645 return c;
3649 /*********************************************************************
3650 * fgetc (MSVCRT.@)
3652 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3654 int ret;
3656 MSVCRT__lock_file(file);
3657 ret = MSVCRT__fgetc_nolock(file);
3658 MSVCRT__unlock_file(file);
3660 return ret;
3663 /*********************************************************************
3664 * _fgetc_nolock (MSVCRT.@)
3666 int CDECL MSVCRT__fgetc_nolock(MSVCRT_FILE* file)
3668 unsigned char *i;
3669 unsigned int j;
3671 if (file->_cnt>0) {
3672 file->_cnt--;
3673 i = (unsigned char *)file->_ptr++;
3674 j = *i;
3675 } else
3676 j = MSVCRT__filbuf(file);
3678 return j;
3681 /*********************************************************************
3682 * _fgetchar (MSVCRT.@)
3684 int CDECL MSVCRT__fgetchar(void)
3686 return MSVCRT_fgetc(MSVCRT_stdin);
3689 /*********************************************************************
3690 * fgets (MSVCRT.@)
3692 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3694 int cc = MSVCRT_EOF;
3695 char * buf_start = s;
3697 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3698 file,file->_file,s,size);
3700 MSVCRT__lock_file(file);
3702 while ((size >1) && (cc = MSVCRT__fgetc_nolock(file)) != MSVCRT_EOF && cc != '\n')
3704 *s++ = (char)cc;
3705 size --;
3707 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3709 TRACE(":nothing read\n");
3710 MSVCRT__unlock_file(file);
3711 return NULL;
3713 if ((cc != MSVCRT_EOF) && (size > 1))
3714 *s++ = cc;
3715 *s = '\0';
3716 TRACE(":got %s\n", debugstr_a(buf_start));
3717 MSVCRT__unlock_file(file);
3718 return buf_start;
3721 /*********************************************************************
3722 * fgetwc (MSVCRT.@)
3724 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3726 MSVCRT_wint_t ret;
3728 MSVCRT__lock_file(file);
3729 ret = MSVCRT__fgetwc_nolock(file);
3730 MSVCRT__unlock_file(file);
3732 return ret;
3735 /*********************************************************************
3736 * _fgetwc_nolock (MSVCRT.@)
3738 MSVCRT_wint_t CDECL MSVCRT__fgetwc_nolock(MSVCRT_FILE* file)
3740 MSVCRT_wint_t ret;
3741 int ch;
3743 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3744 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3745 char *p;
3747 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3748 ch = MSVCRT__fgetc_nolock(file);
3749 if(ch == MSVCRT_EOF) {
3750 ret = MSVCRT_WEOF;
3751 break;
3753 *p = (char)ch;
3755 }else {
3756 char mbs[MSVCRT_MB_LEN_MAX];
3757 int len = 0;
3759 ch = MSVCRT__fgetc_nolock(file);
3760 if(ch != MSVCRT_EOF) {
3761 mbs[0] = (char)ch;
3762 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3763 ch = MSVCRT__fgetc_nolock(file);
3764 if(ch != MSVCRT_EOF) {
3765 mbs[1] = (char)ch;
3766 len = 2;
3768 }else {
3769 len = 1;
3773 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3774 ret = MSVCRT_WEOF;
3777 return ret;
3780 /*********************************************************************
3781 * _getw (MSVCRT.@)
3783 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3785 char *ch;
3786 int i, k;
3787 unsigned int j;
3788 ch = (char *)&i;
3790 MSVCRT__lock_file(file);
3791 for (j=0; j<sizeof(int); j++) {
3792 k = MSVCRT__fgetc_nolock(file);
3793 if (k == MSVCRT_EOF) {
3794 file->_flag |= MSVCRT__IOEOF;
3795 MSVCRT__unlock_file(file);
3796 return MSVCRT_EOF;
3798 ch[j] = k;
3801 MSVCRT__unlock_file(file);
3802 return i;
3805 /*********************************************************************
3806 * getwc (MSVCRT.@)
3808 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3810 return MSVCRT_fgetwc(file);
3813 /*********************************************************************
3814 * _fgetwchar (MSVCRT.@)
3816 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3818 return MSVCRT_fgetwc(MSVCRT_stdin);
3821 /*********************************************************************
3822 * getwchar (MSVCRT.@)
3824 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3826 return MSVCRT__fgetwchar();
3829 /*********************************************************************
3830 * fgetws (MSVCRT.@)
3832 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3834 MSVCRT_wint_t cc = MSVCRT_WEOF;
3835 MSVCRT_wchar_t * buf_start = s;
3837 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3838 file,file->_file,s,size);
3840 MSVCRT__lock_file(file);
3842 while ((size >1) && (cc = MSVCRT__fgetwc_nolock(file)) != MSVCRT_WEOF && cc != '\n')
3844 *s++ = cc;
3845 size --;
3847 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3849 TRACE(":nothing read\n");
3850 MSVCRT__unlock_file(file);
3851 return NULL;
3853 if ((cc != MSVCRT_WEOF) && (size > 1))
3854 *s++ = cc;
3855 *s = 0;
3856 TRACE(":got %s\n", debugstr_w(buf_start));
3857 MSVCRT__unlock_file(file);
3858 return buf_start;
3861 /*********************************************************************
3862 * _flsbuf (MSVCRT.@)
3864 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3866 /* Flush output buffer */
3867 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3868 msvcrt_alloc_buffer(file);
3871 if(!(file->_flag & MSVCRT__IOWRT)) {
3872 if(!(file->_flag & MSVCRT__IORW)) {
3873 file->_flag |= MSVCRT__IOERR;
3874 return MSVCRT_EOF;
3876 file->_flag |= MSVCRT__IOWRT;
3878 if(file->_flag & MSVCRT__IOREAD) {
3879 if(!(file->_flag & MSVCRT__IOEOF)) {
3880 file->_flag |= MSVCRT__IOERR;
3881 return MSVCRT_EOF;
3883 file->_cnt = 0;
3884 file->_ptr = file->_base;
3885 file->_flag &= ~(MSVCRT__IOREAD | MSVCRT__IOEOF);
3888 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3889 int res = 0;
3891 if(file->_cnt <= 0) {
3892 res = msvcrt_flush_buffer(file);
3893 if(res)
3894 return res;
3895 file->_flag |= MSVCRT__IOWRT;
3896 file->_cnt=file->_bufsiz;
3898 *file->_ptr++ = c;
3899 file->_cnt--;
3900 return c&0xff;
3901 } else {
3902 unsigned char cc=c;
3903 int len;
3904 /* set _cnt to 0 for unbuffered FILEs */
3905 file->_cnt = 0;
3906 len = MSVCRT__write(file->_file, &cc, 1);
3907 if (len == 1)
3908 return c & 0xff;
3909 file->_flag |= MSVCRT__IOERR;
3910 return MSVCRT_EOF;
3914 /*********************************************************************
3915 * fwrite (MSVCRT.@)
3917 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3919 MSVCRT_size_t ret;
3921 MSVCRT__lock_file(file);
3922 ret = MSVCRT__fwrite_nolock(ptr, size, nmemb, file);
3923 MSVCRT__unlock_file(file);
3925 return ret;
3928 /*********************************************************************
3929 * _fwrite_nolock (MSVCRT.@)
3931 MSVCRT_size_t CDECL MSVCRT__fwrite_nolock(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3933 MSVCRT_size_t wrcnt=size * nmemb;
3934 int written = 0;
3935 if (size == 0)
3936 return 0;
3938 while(wrcnt) {
3939 if(file->_cnt < 0) {
3940 WARN("negative file->_cnt value in %p\n", file);
3941 file->_flag |= MSVCRT__IOERR;
3942 break;
3943 } else if(file->_cnt) {
3944 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3945 memcpy(file->_ptr, ptr, pcnt);
3946 file->_cnt -= pcnt;
3947 file->_ptr += pcnt;
3948 written += pcnt;
3949 wrcnt -= pcnt;
3950 ptr = (const char*)ptr + pcnt;
3951 } else if((file->_flag & MSVCRT__IONBF)
3952 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
3953 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
3954 MSVCRT_size_t pcnt;
3955 int bufsiz;
3957 if(file->_flag & MSVCRT__IONBF)
3958 bufsiz = 1;
3959 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3960 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
3961 else
3962 bufsiz = file->_bufsiz;
3964 pcnt = (wrcnt / bufsiz) * bufsiz;
3966 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
3967 break;
3969 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
3970 file->_flag |= MSVCRT__IOERR;
3971 break;
3973 written += pcnt;
3974 wrcnt -= pcnt;
3975 ptr = (const char*)ptr + pcnt;
3976 } else {
3977 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3978 break;
3979 written++;
3980 wrcnt--;
3981 ptr = (const char*)ptr + 1;
3985 return written / size;
3988 /*********************************************************************
3989 * fputwc (MSVCRT.@)
3991 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3993 MSVCRT_wint_t ret;
3995 MSVCRT__lock_file(file);
3996 ret = MSVCRT__fputwc_nolock(wc, file);
3997 MSVCRT__unlock_file(file);
3999 return ret;
4002 /*********************************************************************
4003 * _fputwc_nolock (MSVCRT.@)
4005 MSVCRT_wint_t CDECL MSVCRT__fputwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE* file)
4007 MSVCRT_wchar_t mwc=wc;
4008 ioinfo *fdinfo;
4009 MSVCRT_wint_t ret;
4011 fdinfo = get_ioinfo_nolock(file->_file);
4013 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
4014 char buf[MSVCRT_MB_LEN_MAX];
4015 int char_len;
4017 char_len = MSVCRT_wctomb(buf, mwc);
4018 if(char_len!=-1 && MSVCRT__fwrite_nolock(buf, char_len, 1, file)==1)
4019 ret = wc;
4020 else
4021 ret = MSVCRT_WEOF;
4022 }else if(MSVCRT__fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
4023 ret = wc;
4024 }else {
4025 ret = MSVCRT_WEOF;
4028 return ret;
4031 /*********************************************************************
4032 * _fputwchar (MSVCRT.@)
4034 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
4036 return MSVCRT_fputwc(wc, MSVCRT_stdout);
4039 /*********************************************************************
4040 * _wfsopen (MSVCRT.@)
4042 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
4044 MSVCRT_FILE* file;
4045 int open_flags, stream_flags, fd;
4047 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
4049 /* map mode string to open() flags. "man fopen" for possibilities. */
4050 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4051 return NULL;
4053 LOCK_FILES();
4054 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4055 if (fd < 0)
4056 file = NULL;
4057 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
4058 != -1)
4059 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
4060 else if (file)
4062 file->_flag = 0;
4063 file = NULL;
4066 TRACE(":got (%p)\n",file);
4067 if (fd >= 0 && !file)
4068 MSVCRT__close(fd);
4069 UNLOCK_FILES();
4070 return file;
4073 /*********************************************************************
4074 * _fsopen (MSVCRT.@)
4076 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
4078 MSVCRT_FILE *ret;
4079 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4081 if (path && !(pathW = msvcrt_wstrdupa(path))) {
4082 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4083 *MSVCRT__errno() = MSVCRT_EINVAL;
4084 return NULL;
4086 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4088 MSVCRT_free(pathW);
4089 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4090 *MSVCRT__errno() = MSVCRT_EINVAL;
4091 return NULL;
4094 ret = MSVCRT__wfsopen(pathW, modeW, share);
4096 MSVCRT_free(pathW);
4097 MSVCRT_free(modeW);
4098 return ret;
4101 /*********************************************************************
4102 * fopen (MSVCRT.@)
4104 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
4106 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
4109 /*********************************************************************
4110 * fopen_s (MSVCRT.@)
4112 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
4113 const char *filename, const char *mode)
4115 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4116 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4117 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4119 *pFile = MSVCRT_fopen(filename, mode);
4121 if(!*pFile)
4122 return *MSVCRT__errno();
4123 return 0;
4126 /*********************************************************************
4127 * _wfopen (MSVCRT.@)
4129 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
4131 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
4134 /*********************************************************************
4135 * _wfopen_s (MSVCRT.@)
4137 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
4138 const MSVCRT_wchar_t *mode)
4140 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4141 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4142 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4144 *pFile = MSVCRT__wfopen(filename, mode);
4146 if(!*pFile)
4147 return *MSVCRT__errno();
4148 return 0;
4151 /*********************************************************************
4152 * fputc (MSVCRT.@)
4154 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
4156 int ret;
4158 MSVCRT__lock_file(file);
4159 ret = MSVCRT__fputc_nolock(c, file);
4160 MSVCRT__unlock_file(file);
4162 return ret;
4165 /*********************************************************************
4166 * _fputc_nolock (MSVCRT.@)
4168 int CDECL MSVCRT__fputc_nolock(int c, MSVCRT_FILE* file)
4170 int res;
4172 if(file->_cnt>0) {
4173 *file->_ptr++=c;
4174 file->_cnt--;
4175 if (c == '\n')
4177 res = msvcrt_flush_buffer(file);
4178 return res ? res : c;
4180 else {
4181 return c & 0xff;
4183 } else {
4184 res = MSVCRT__flsbuf(c, file);
4185 return res;
4189 /*********************************************************************
4190 * _fputchar (MSVCRT.@)
4192 int CDECL MSVCRT__fputchar(int c)
4194 return MSVCRT_fputc(c, MSVCRT_stdout);
4197 /*********************************************************************
4198 * fread (MSVCRT.@)
4200 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4202 MSVCRT_size_t ret;
4204 MSVCRT__lock_file(file);
4205 ret = MSVCRT__fread_nolock(ptr, size, nmemb, file);
4206 MSVCRT__unlock_file(file);
4208 return ret;
4211 /*********************************************************************
4212 * _fread_nolock (MSVCRT.@)
4214 MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4216 MSVCRT_size_t rcnt=size * nmemb;
4217 MSVCRT_size_t read=0;
4218 MSVCRT_size_t pread=0;
4220 if(!rcnt)
4221 return 0;
4223 /* first buffered data */
4224 if(file->_cnt>0) {
4225 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4226 memcpy(ptr, file->_ptr, pcnt);
4227 file->_cnt -= pcnt;
4228 file->_ptr += pcnt;
4229 read += pcnt ;
4230 rcnt -= pcnt ;
4231 ptr = (char*)ptr + pcnt;
4232 } else if(!(file->_flag & MSVCRT__IOREAD )) {
4233 if(file->_flag & MSVCRT__IORW) {
4234 file->_flag |= MSVCRT__IOREAD;
4235 } else {
4236 return 0;
4240 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4241 msvcrt_alloc_buffer(file);
4243 while(rcnt>0)
4245 int i;
4246 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
4247 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
4248 file->_ptr = file->_base;
4249 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
4250 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4251 if (i > 0 && i < file->_cnt) {
4252 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4253 file->_flag &= ~MSVCRT__IOEOF;
4255 if (i > 0) {
4256 memcpy(ptr, file->_ptr, i);
4257 file->_cnt -= i;
4258 file->_ptr += i;
4260 } else if (rcnt > INT_MAX) {
4261 i = MSVCRT__read(file->_file, ptr, INT_MAX);
4262 } else if (rcnt < MSVCRT_BUFSIZ) {
4263 i = MSVCRT__read(file->_file, ptr, rcnt);
4264 } else {
4265 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
4267 pread += i;
4268 rcnt -= i;
4269 ptr = (char *)ptr+i;
4270 /* expose feof condition in the flags
4271 * MFC tests file->_flag for feof, and doesn't call feof())
4273 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4274 file->_flag |= MSVCRT__IOEOF;
4275 else if (i == -1)
4277 file->_flag |= MSVCRT__IOERR;
4278 pread = 0;
4279 rcnt = 0;
4281 if (i < 1) break;
4283 read+=pread;
4284 return read / size;
4288 /*********************************************************************
4289 * fread_s (MSVCR80.@)
4291 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4292 MSVCRT_size_t count, MSVCRT_FILE *stream)
4294 MSVCRT_size_t ret;
4296 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4297 if(buf && buf_size)
4298 memset(buf, 0, buf_size);
4299 return 0;
4301 if(!elem_size || !count) return 0;
4303 MSVCRT__lock_file(stream);
4304 ret = MSVCRT__fread_nolock_s(buf, buf_size, elem_size, count, stream);
4305 MSVCRT__unlock_file(stream);
4307 return ret;
4310 /*********************************************************************
4311 * _fread_nolock_s (MSVCR80.@)
4313 MSVCRT_size_t CDECL MSVCRT__fread_nolock_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4314 MSVCRT_size_t count, MSVCRT_FILE *stream)
4316 size_t bytes_left, buf_pos;
4318 TRACE("(%p %lu %lu %lu %p)\n", buf, buf_size, elem_size, count, stream);
4320 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4321 if(buf && buf_size)
4322 memset(buf, 0, buf_size);
4323 return 0;
4325 if(!elem_size || !count) return 0;
4326 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4327 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4329 bytes_left = elem_size*count;
4330 buf_pos = 0;
4331 while(bytes_left) {
4332 if(stream->_cnt > 0) {
4333 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4335 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4336 memset(buf, 0, buf_size);
4337 return 0;
4340 MSVCRT__fread_nolock((char*)buf+buf_pos, 1, size, stream);
4341 buf_pos += size;
4342 bytes_left -= size;
4343 }else {
4344 int c = MSVCRT__filbuf(stream);
4346 if(c == MSVCRT_EOF)
4347 break;
4349 if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, MSVCRT_ERANGE)) {
4350 memset(buf, 0, buf_size);
4351 return 0;
4354 ((char*)buf)[buf_pos++] = c;
4355 bytes_left--;
4359 return buf_pos/elem_size;
4362 /*********************************************************************
4363 * _wfreopen (MSVCRT.@)
4366 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4368 int open_flags, stream_flags, fd;
4370 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4372 LOCK_FILES();
4373 if (!file || ((fd = file->_file) < 0))
4374 file = NULL;
4375 else
4377 MSVCRT_fclose(file);
4378 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4379 file = NULL;
4380 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4381 file = NULL;
4382 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4384 file->_flag = 0;
4385 file = NULL;
4388 UNLOCK_FILES();
4389 return file;
4392 /*********************************************************************
4393 * _wfreopen_s (MSVCRT.@)
4395 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4396 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4398 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4399 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4400 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4401 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4403 *pFile = MSVCRT__wfreopen(path, mode, file);
4405 if(!*pFile)
4406 return *MSVCRT__errno();
4407 return 0;
4410 /*********************************************************************
4411 * freopen (MSVCRT.@)
4414 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4416 MSVCRT_FILE *ret;
4417 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4419 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4420 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4422 MSVCRT_free(pathW);
4423 return NULL;
4426 ret = MSVCRT__wfreopen(pathW, modeW, file);
4428 MSVCRT_free(pathW);
4429 MSVCRT_free(modeW);
4430 return ret;
4433 /*********************************************************************
4434 * freopen_s (MSVCRT.@)
4436 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4437 const char *path, const char *mode, MSVCRT_FILE* file)
4439 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4440 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4441 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4442 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4444 *pFile = MSVCRT_freopen(path, mode, file);
4446 if(!*pFile)
4447 return *MSVCRT__errno();
4448 return 0;
4451 /*********************************************************************
4452 * fsetpos (MSVCRT.@)
4454 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4456 int ret;
4458 MSVCRT__lock_file(file);
4459 msvcrt_flush_buffer(file);
4461 /* Reset direction of i/o */
4462 if(file->_flag & MSVCRT__IORW) {
4463 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4466 ret = (MSVCRT__lseeki64(file->_file,*pos,MSVCRT_SEEK_SET) == -1) ? -1 : 0;
4467 MSVCRT__unlock_file(file);
4468 return ret;
4471 /*********************************************************************
4472 * _ftelli64 (MSVCRT.@)
4474 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4476 __int64 ret;
4478 MSVCRT__lock_file(file);
4479 ret = MSVCRT__ftelli64_nolock(file);
4480 MSVCRT__unlock_file(file);
4482 return ret;
4485 /*********************************************************************
4486 * _ftelli64_nolock (MSVCRT.@)
4488 __int64 CDECL MSVCRT__ftelli64_nolock(MSVCRT_FILE* file)
4490 __int64 pos;
4492 pos = _telli64(file->_file);
4493 if(pos == -1)
4494 return -1;
4495 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4496 if(file->_flag & MSVCRT__IOWRT) {
4497 pos += file->_ptr - file->_base;
4499 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4500 char *p;
4502 for(p=file->_base; p<file->_ptr; p++)
4503 if(*p == '\n')
4504 pos++;
4506 } else if(!file->_cnt) { /* nothing to do */
4507 } else if(MSVCRT__lseeki64(file->_file, 0, MSVCRT_SEEK_END)==pos) {
4508 int i;
4510 pos -= file->_cnt;
4511 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4512 for(i=0; i<file->_cnt; i++)
4513 if(file->_ptr[i] == '\n')
4514 pos--;
4516 } else {
4517 char *p;
4519 if(MSVCRT__lseeki64(file->_file, pos, MSVCRT_SEEK_SET) != pos)
4520 return -1;
4522 pos -= file->_bufsiz;
4523 pos += file->_ptr - file->_base;
4525 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4526 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4527 pos--;
4529 for(p=file->_base; p<file->_ptr; p++)
4530 if(*p == '\n')
4531 pos++;
4536 return pos;
4539 /*********************************************************************
4540 * ftell (MSVCRT.@)
4542 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4544 return MSVCRT__ftelli64(file);
4547 /*********************************************************************
4548 * _ftell_nolock (MSVCRT.@)
4550 LONG CDECL MSVCRT__ftell_nolock(MSVCRT_FILE* file)
4552 return MSVCRT__ftelli64_nolock(file);
4555 /*********************************************************************
4556 * fgetpos (MSVCRT.@)
4558 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4560 *pos = MSVCRT__ftelli64(file);
4561 if(*pos == -1)
4562 return -1;
4563 return 0;
4566 /*********************************************************************
4567 * fputs (MSVCRT.@)
4569 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4571 MSVCRT_size_t len = strlen(s);
4572 int ret;
4574 MSVCRT__lock_file(file);
4575 ret = MSVCRT__fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4576 MSVCRT__unlock_file(file);
4577 return ret;
4580 /*********************************************************************
4581 * fputws (MSVCRT.@)
4583 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4585 MSVCRT_size_t i, len = strlenW(s);
4586 BOOL tmp_buf;
4587 int ret;
4589 MSVCRT__lock_file(file);
4590 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4591 ret = MSVCRT__fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4592 MSVCRT__unlock_file(file);
4593 return ret;
4596 tmp_buf = add_std_buffer(file);
4597 for (i=0; i<len; i++) {
4598 if(MSVCRT__fputwc_nolock(s[i], file) == MSVCRT_WEOF) {
4599 if(tmp_buf) remove_std_buffer(file);
4600 MSVCRT__unlock_file(file);
4601 return MSVCRT_WEOF;
4605 if(tmp_buf) remove_std_buffer(file);
4606 MSVCRT__unlock_file(file);
4607 return 0;
4610 /*********************************************************************
4611 * getchar (MSVCRT.@)
4613 int CDECL MSVCRT_getchar(void)
4615 return MSVCRT_fgetc(MSVCRT_stdin);
4618 /*********************************************************************
4619 * getc (MSVCRT.@)
4621 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4623 return MSVCRT_fgetc(file);
4626 /*********************************************************************
4627 * gets (MSVCRT.@)
4629 char * CDECL MSVCRT_gets(char *buf)
4631 int cc;
4632 char * buf_start = buf;
4634 MSVCRT__lock_file(MSVCRT_stdin);
4635 for(cc = MSVCRT__fgetc_nolock(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4636 cc = MSVCRT__fgetc_nolock(MSVCRT_stdin))
4637 if(cc != '\r') *buf++ = (char)cc;
4639 *buf = '\0';
4641 TRACE("got '%s'\n", buf_start);
4642 MSVCRT__unlock_file(MSVCRT_stdin);
4643 return buf_start;
4646 /*********************************************************************
4647 * _getws (MSVCRT.@)
4649 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4651 MSVCRT_wint_t cc;
4652 MSVCRT_wchar_t* ws = buf;
4654 MSVCRT__lock_file(MSVCRT_stdin);
4655 for (cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4656 cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin))
4658 if (cc != '\r')
4659 *buf++ = (MSVCRT_wchar_t)cc;
4661 *buf = '\0';
4663 TRACE("got %s\n", debugstr_w(ws));
4664 MSVCRT__unlock_file(MSVCRT_stdin);
4665 return ws;
4668 /*********************************************************************
4669 * putc (MSVCRT.@)
4671 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4673 return MSVCRT_fputc(c, file);
4676 /*********************************************************************
4677 * putchar (MSVCRT.@)
4679 int CDECL MSVCRT_putchar(int c)
4681 return MSVCRT_fputc(c, MSVCRT_stdout);
4684 /*********************************************************************
4685 * puts (MSVCRT.@)
4687 int CDECL MSVCRT_puts(const char *s)
4689 MSVCRT_size_t len = strlen(s);
4690 int ret;
4692 MSVCRT__lock_file(MSVCRT_stdout);
4693 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4694 MSVCRT__unlock_file(MSVCRT_stdout);
4695 return MSVCRT_EOF;
4698 ret = MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4699 MSVCRT__unlock_file(MSVCRT_stdout);
4700 return ret;
4703 /*********************************************************************
4704 * _putws (MSVCRT.@)
4706 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4708 static const MSVCRT_wchar_t nl = '\n';
4709 MSVCRT_size_t len = strlenW(s);
4710 int ret;
4712 MSVCRT__lock_file(MSVCRT_stdout);
4713 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4714 MSVCRT__unlock_file(MSVCRT_stdout);
4715 return MSVCRT_EOF;
4718 ret = MSVCRT__fwrite_nolock(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4719 MSVCRT__unlock_file(MSVCRT_stdout);
4720 return ret;
4723 /*********************************************************************
4724 * remove (MSVCRT.@)
4726 int CDECL MSVCRT_remove(const char *path)
4728 TRACE("(%s)\n",path);
4729 if (DeleteFileA(path))
4730 return 0;
4731 TRACE(":failed (%d)\n",GetLastError());
4732 msvcrt_set_errno(GetLastError());
4733 return -1;
4736 /*********************************************************************
4737 * _wremove (MSVCRT.@)
4739 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4741 TRACE("(%s)\n",debugstr_w(path));
4742 if (DeleteFileW(path))
4743 return 0;
4744 TRACE(":failed (%d)\n",GetLastError());
4745 msvcrt_set_errno(GetLastError());
4746 return -1;
4749 /*********************************************************************
4750 * rename (MSVCRT.@)
4752 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4754 TRACE(":from %s to %s\n",oldpath,newpath);
4755 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4756 return 0;
4757 TRACE(":failed (%d)\n",GetLastError());
4758 msvcrt_set_errno(GetLastError());
4759 return -1;
4762 /*********************************************************************
4763 * _wrename (MSVCRT.@)
4765 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4767 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4768 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4769 return 0;
4770 TRACE(":failed (%d)\n",GetLastError());
4771 msvcrt_set_errno(GetLastError());
4772 return -1;
4775 /*********************************************************************
4776 * setvbuf (MSVCRT.@)
4778 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4780 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4781 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4782 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4784 MSVCRT__lock_file(file);
4786 MSVCRT__fflush_nolock(file);
4787 if(file->_flag & MSVCRT__IOMYBUF)
4788 MSVCRT_free(file->_base);
4789 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4790 file->_cnt = 0;
4792 if(mode == MSVCRT__IONBF) {
4793 file->_flag |= MSVCRT__IONBF;
4794 file->_base = file->_ptr = (char*)&file->_charbuf;
4795 file->_bufsiz = 2;
4796 }else if(buf) {
4797 file->_base = file->_ptr = buf;
4798 file->_flag |= MSVCRT__USERBUF;
4799 file->_bufsiz = size;
4800 }else {
4801 file->_base = file->_ptr = MSVCRT_malloc(size);
4802 if(!file->_base) {
4803 file->_bufsiz = 0;
4804 MSVCRT__unlock_file(file);
4805 return -1;
4808 file->_flag |= MSVCRT__IOMYBUF;
4809 file->_bufsiz = size;
4811 MSVCRT__unlock_file(file);
4812 return 0;
4815 /*********************************************************************
4816 * setbuf (MSVCRT.@)
4818 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4820 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4823 static int tmpnam_helper(char *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
4825 char tmpstr[8];
4826 char *p = s;
4827 int digits;
4829 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
4831 if (size < 3) {
4832 if (size) *s = 0;
4833 *MSVCRT__errno() = MSVCRT_ERANGE;
4834 return MSVCRT_ERANGE;
4836 *p++ = '\\';
4837 *p++ = 's';
4838 size -= 2;
4839 digits = msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4840 if (digits+1 > size) {
4841 *s = 0;
4842 *MSVCRT__errno() = MSVCRT_ERANGE;
4843 return MSVCRT_ERANGE;
4845 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4846 p += digits;
4847 *p++ = '.';
4848 size -= digits+1;
4850 while(1) {
4851 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4852 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4853 break;
4856 digits = msvcrt_int_to_base32(digits, tmpstr);
4857 if (digits+1 > size) {
4858 *s = 0;
4859 *MSVCRT__errno() = MSVCRT_ERANGE;
4860 return MSVCRT_ERANGE;
4862 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4863 p[digits] = 0;
4865 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4866 GetLastError() == ERROR_FILE_NOT_FOUND)
4867 break;
4869 return 0;
4872 int CDECL MSVCRT_tmpnam_s(char *s, MSVCRT_size_t size)
4874 return tmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
4877 /*********************************************************************
4878 * tmpnam (MSVCRT.@)
4880 char * CDECL MSVCRT_tmpnam(char *s)
4882 if (!s) {
4883 thread_data_t *data = msvcrt_get_thread_data();
4885 if(!data->tmpnam_buffer)
4886 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4888 s = data->tmpnam_buffer;
4891 return tmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
4894 static int wtmpnam_helper(MSVCRT_wchar_t *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
4896 MSVCRT_wchar_t tmpstr[8];
4897 MSVCRT_wchar_t *p = s;
4898 int digits;
4900 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
4902 if (size < 3) {
4903 if (size) *s = 0;
4904 *MSVCRT__errno() = MSVCRT_ERANGE;
4905 return MSVCRT_ERANGE;
4907 *p++ = '\\';
4908 *p++ = 's';
4909 size -= 2;
4910 digits = msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4911 if (digits+1 > size) {
4912 *s = 0;
4913 *MSVCRT__errno() = MSVCRT_ERANGE;
4914 return MSVCRT_ERANGE;
4916 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4917 p += digits;
4918 *p++ = '.';
4919 size -= digits+1;
4921 while(1) {
4922 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4923 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4924 break;
4927 digits = msvcrt_int_to_base32_w(digits, tmpstr);
4928 if (digits+1 > size) {
4929 *s = 0;
4930 *MSVCRT__errno() = MSVCRT_ERANGE;
4931 return MSVCRT_ERANGE;
4933 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4934 p[digits] = 0;
4936 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4937 GetLastError() == ERROR_FILE_NOT_FOUND)
4938 break;
4940 return 0;
4943 /*********************************************************************
4944 * _wtmpnam_s (MSVCRT.@)
4946 int CDECL MSVCRT__wtmpnam_s(MSVCRT_wchar_t *s, MSVCRT_size_t size)
4948 return wtmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
4951 /*********************************************************************
4952 * _wtmpnam (MSVCRT.@)
4954 MSVCRT_wchar_t * CDECL MSVCRT__wtmpnam(MSVCRT_wchar_t *s)
4956 if (!s) {
4957 thread_data_t *data = msvcrt_get_thread_data();
4959 if(!data->wtmpnam_buffer)
4960 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4962 s = data->wtmpnam_buffer;
4965 return wtmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
4968 /*********************************************************************
4969 * tmpfile (MSVCRT.@)
4971 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4973 char *filename = MSVCRT__tempnam(",", "t");
4974 int fd;
4975 MSVCRT_FILE* file = NULL;
4977 LOCK_FILES();
4978 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4979 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4980 if (fd != -1 && (file = msvcrt_alloc_fp()))
4982 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4984 file->_flag = 0;
4985 file = NULL;
4987 else file->_tmpfname = MSVCRT__strdup(filename);
4990 if(fd != -1 && !file)
4991 MSVCRT__close(fd);
4992 MSVCRT_free(filename);
4993 UNLOCK_FILES();
4994 return file;
4997 /*********************************************************************
4998 * tmpfile_s (MSVCRT.@)
5000 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
5002 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
5004 *file = MSVCRT_tmpfile();
5005 return 0;
5008 static int puts_clbk_file_a(void *file, int len, const char *str)
5010 return MSVCRT_fwrite(str, sizeof(char), len, file);
5013 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
5015 int i, ret;
5017 MSVCRT__lock_file(file);
5019 if(!(get_ioinfo_nolock(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
5020 ret = MSVCRT__fwrite_nolock(str, sizeof(MSVCRT_wchar_t), len, file);
5021 MSVCRT__unlock_file(file);
5022 return ret;
5025 for(i=0; i<len; i++) {
5026 if(MSVCRT__fputwc_nolock(str[i], file) == MSVCRT_WEOF) {
5027 MSVCRT__unlock_file(file);
5028 return -1;
5032 MSVCRT__unlock_file(file);
5033 return len;
5036 /*********************************************************************
5037 * vfprintf (MSVCRT.@)
5039 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5041 BOOL tmp_buf;
5042 int ret;
5044 MSVCRT__lock_file(file);
5045 tmp_buf = add_std_buffer(file);
5046 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, 0, arg_clbk_valist, NULL, &valist);
5047 if(tmp_buf) remove_std_buffer(file);
5048 MSVCRT__unlock_file(file);
5050 return ret;
5053 /*********************************************************************
5054 * vfprintf_s (MSVCRT.@)
5056 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5058 BOOL tmp_buf;
5059 int ret;
5061 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
5063 MSVCRT__lock_file(file);
5064 tmp_buf = add_std_buffer(file);
5065 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL,
5066 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, arg_clbk_valist, NULL, &valist);
5067 if(tmp_buf) remove_std_buffer(file);
5068 MSVCRT__unlock_file(file);
5070 return ret;
5073 /*********************************************************************
5074 * vfwprintf (MSVCRT.@)
5076 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5078 BOOL tmp_buf;
5079 int ret;
5081 MSVCRT__lock_file(file);
5082 tmp_buf = add_std_buffer(file);
5083 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, 0, arg_clbk_valist, NULL, &valist);
5084 if(tmp_buf) remove_std_buffer(file);
5085 MSVCRT__unlock_file(file);
5087 return ret;
5090 /*********************************************************************
5091 * vfwprintf_s (MSVCRT.@)
5093 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5095 BOOL tmp_buf;
5096 int ret;
5098 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5100 MSVCRT__lock_file(file);
5101 tmp_buf = add_std_buffer(file);
5102 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL,
5103 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, arg_clbk_valist, NULL, &valist);
5104 if(tmp_buf) remove_std_buffer(file);
5105 MSVCRT__unlock_file(file);
5107 return ret;
5110 /*********************************************************************
5111 * __stdio_common_vfprintf (UCRTBASE.@)
5113 int CDECL MSVCRT__stdio_common_vfprintf(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
5114 MSVCRT__locale_t locale, __ms_va_list valist)
5116 BOOL tmp_buf;
5117 int ret;
5119 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5121 MSVCRT__lock_file(file);
5122 tmp_buf = add_std_buffer(file);
5124 if (options & ~UCRTBASE_PRINTF_MASK)
5125 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5126 ret = pf_printf_a(puts_clbk_file_a, file, format, locale,
5127 options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
5128 if(tmp_buf) remove_std_buffer(file);
5129 MSVCRT__unlock_file(file);
5131 return ret;
5134 /*********************************************************************
5135 * __stdio_common_vfprintf_s (UCRTBASE.@)
5137 int CDECL MSVCRT__stdio_common_vfprintf_s(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
5138 MSVCRT__locale_t locale, __ms_va_list valist)
5140 BOOL tmp_buf;
5141 int ret;
5143 if (!MSVCRT_CHECK_PMT(file != NULL)) return -1;
5145 MSVCRT__lock_file(file);
5146 tmp_buf = add_std_buffer(file);
5148 if (options & ~UCRTBASE_PRINTF_MASK)
5149 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5151 ret = pf_printf_a(puts_clbk_file_a, file, format, locale,
5152 (options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5153 arg_clbk_valist, NULL, &valist);
5155 if (tmp_buf) remove_std_buffer(file);
5156 MSVCRT__unlock_file(file);
5158 return ret;
5161 /*********************************************************************
5162 * __stdio_common_vfwprintf (UCRTBASE.@)
5164 int CDECL MSVCRT__stdio_common_vfwprintf(unsigned __int64 options, MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
5165 MSVCRT__locale_t locale, __ms_va_list valist)
5167 BOOL tmp_buf;
5168 int ret;
5170 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5171 if (!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5173 MSVCRT__lock_file(file);
5174 tmp_buf = add_std_buffer(file);
5176 if (options & ~UCRTBASE_PRINTF_MASK)
5177 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5179 ret = pf_printf_w(puts_clbk_file_w, file, format, locale,
5180 options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
5182 if(tmp_buf) remove_std_buffer(file);
5183 MSVCRT__unlock_file(file);
5185 return ret;
5188 /*********************************************************************
5189 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5191 int CDECL MSVCRT__stdio_common_vfwprintf_s(unsigned __int64 options, MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
5192 MSVCRT__locale_t locale, __ms_va_list valist)
5194 BOOL tmp_buf;
5195 int ret;
5197 if (!MSVCRT_CHECK_PMT(file != NULL)) return -1;
5198 if (!MSVCRT_CHECK_PMT(format != NULL)) return -1;
5200 MSVCRT__lock_file(file);
5201 tmp_buf = add_std_buffer(file);
5203 if (options & ~UCRTBASE_PRINTF_MASK)
5204 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5206 ret = pf_printf_w(puts_clbk_file_w, file, format, locale,
5207 (options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
5208 arg_clbk_valist, NULL, &valist);
5210 if (tmp_buf) remove_std_buffer(file);
5211 MSVCRT__unlock_file(file);
5213 return ret;
5216 /*********************************************************************
5217 * _vfprintf_l (MSVCRT.@)
5219 int CDECL MSVCRT__vfprintf_l(MSVCRT_FILE* file, const char *format,
5220 MSVCRT__locale_t locale, __ms_va_list valist)
5222 BOOL tmp_buf;
5223 int ret;
5225 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
5227 MSVCRT__lock_file(file);
5228 tmp_buf = add_std_buffer(file);
5229 ret = pf_printf_a(puts_clbk_file_a, file, format, locale, 0, arg_clbk_valist, NULL, &valist);
5230 if(tmp_buf) remove_std_buffer(file);
5231 MSVCRT__unlock_file(file);
5233 return ret;
5236 /*********************************************************************
5237 * _vfwprintf_l (MSVCRT.@)
5239 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5240 MSVCRT__locale_t locale, __ms_va_list valist)
5242 BOOL tmp_buf;
5243 int ret;
5245 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5247 MSVCRT__lock_file(file);
5248 tmp_buf = add_std_buffer(file);
5249 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, 0, arg_clbk_valist, NULL, &valist);
5250 if(tmp_buf) remove_std_buffer(file);
5251 MSVCRT__unlock_file(file);
5253 return ret;
5256 /*********************************************************************
5257 * vprintf (MSVCRT.@)
5259 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
5261 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
5264 /*********************************************************************
5265 * vprintf_s (MSVCRT.@)
5267 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
5269 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
5272 /*********************************************************************
5273 * vwprintf (MSVCRT.@)
5275 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
5277 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
5280 /*********************************************************************
5281 * vwprintf_s (MSVCRT.@)
5283 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
5285 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
5288 /*********************************************************************
5289 * fprintf (MSVCRT.@)
5291 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
5293 __ms_va_list valist;
5294 int res;
5295 __ms_va_start(valist, format);
5296 res = MSVCRT_vfprintf(file, format, valist);
5297 __ms_va_end(valist);
5298 return res;
5301 /*********************************************************************
5302 * fprintf_s (MSVCRT.@)
5304 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
5306 __ms_va_list valist;
5307 int res;
5308 __ms_va_start(valist, format);
5309 res = MSVCRT_vfprintf_s(file, format, valist);
5310 __ms_va_end(valist);
5311 return res;
5314 /*********************************************************************
5315 * fwprintf (MSVCRT.@)
5317 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5319 __ms_va_list valist;
5320 int res;
5321 __ms_va_start(valist, format);
5322 res = MSVCRT_vfwprintf(file, format, valist);
5323 __ms_va_end(valist);
5324 return res;
5327 /*********************************************************************
5328 * fwprintf_s (MSVCRT.@)
5330 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5332 __ms_va_list valist;
5333 int res;
5334 __ms_va_start(valist, format);
5335 res = MSVCRT_vfwprintf_s(file, format, valist);
5336 __ms_va_end(valist);
5337 return res;
5340 /*********************************************************************
5341 * _fwprintf_l (MSVCRT.@)
5343 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
5345 __ms_va_list valist;
5346 int res;
5347 __ms_va_start(valist, locale);
5348 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
5349 __ms_va_end(valist);
5350 return res;
5353 /*********************************************************************
5354 * printf (MSVCRT.@)
5356 int CDECL MSVCRT_printf(const char *format, ...)
5358 __ms_va_list valist;
5359 int res;
5360 __ms_va_start(valist, format);
5361 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
5362 __ms_va_end(valist);
5363 return res;
5366 /*********************************************************************
5367 * printf_s (MSVCRT.@)
5369 int CDECL MSVCRT_printf_s(const char *format, ...)
5371 __ms_va_list valist;
5372 int res;
5373 __ms_va_start(valist, format);
5374 res = MSVCRT_vprintf_s(format, valist);
5375 __ms_va_end(valist);
5376 return res;
5379 /*********************************************************************
5380 * ungetc (MSVCRT.@)
5382 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
5384 int ret;
5386 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5388 MSVCRT__lock_file(file);
5389 ret = MSVCRT__ungetc_nolock(c, file);
5390 MSVCRT__unlock_file(file);
5392 return ret;
5395 /*********************************************************************
5396 * _ungetc_nolock (MSVCRT.@)
5398 int CDECL MSVCRT__ungetc_nolock(int c, MSVCRT_FILE * file)
5400 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5402 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
5403 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
5404 return MSVCRT_EOF;
5406 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
5407 && msvcrt_alloc_buffer(file))
5408 || (!file->_cnt && file->_ptr==file->_base))
5409 file->_ptr++;
5411 if(file->_ptr>file->_base) {
5412 file->_ptr--;
5413 if(file->_flag & MSVCRT__IOSTRG) {
5414 if(*file->_ptr != c) {
5415 file->_ptr++;
5416 return MSVCRT_EOF;
5418 }else {
5419 *file->_ptr = c;
5421 file->_cnt++;
5422 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
5423 file->_flag |= MSVCRT__IOREAD;
5424 return c;
5427 return MSVCRT_EOF;
5430 /*********************************************************************
5431 * ungetwc (MSVCRT.@)
5433 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5435 MSVCRT_wint_t ret;
5437 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5439 MSVCRT__lock_file(file);
5440 ret = MSVCRT__ungetwc_nolock(wc, file);
5441 MSVCRT__unlock_file(file);
5443 return ret;
5446 /*********************************************************************
5447 * _ungetwc_nolock (MSVCRT.@)
5449 MSVCRT_wint_t CDECL MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5451 MSVCRT_wchar_t mwc = wc;
5453 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5454 if (wc == MSVCRT_WEOF)
5455 return MSVCRT_WEOF;
5457 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5458 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5459 unsigned char * pp = (unsigned char *)&mwc;
5460 int i;
5462 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
5463 if(pp[i] != MSVCRT__ungetc_nolock(pp[i],file))
5464 return MSVCRT_WEOF;
5466 }else {
5467 char mbs[MSVCRT_MB_LEN_MAX];
5468 int len;
5470 len = MSVCRT_wctomb(mbs, mwc);
5471 if(len == -1)
5472 return MSVCRT_WEOF;
5474 for(len--; len>=0; len--) {
5475 if(mbs[len] != MSVCRT__ungetc_nolock(mbs[len], file))
5476 return MSVCRT_WEOF;
5480 return mwc;
5483 /*********************************************************************
5484 * wprintf (MSVCRT.@)
5486 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
5488 __ms_va_list valist;
5489 int res;
5490 __ms_va_start(valist, format);
5491 res = MSVCRT_vwprintf(format, valist);
5492 __ms_va_end(valist);
5493 return res;
5496 /*********************************************************************
5497 * wprintf_s (MSVCRT.@)
5499 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
5501 __ms_va_list valist;
5502 int res;
5503 __ms_va_start(valist, format);
5504 res = MSVCRT_vwprintf_s(format, valist);
5505 __ms_va_end(valist);
5506 return res;
5509 /*********************************************************************
5510 * _getmaxstdio (MSVCRT.@)
5512 int CDECL MSVCRT__getmaxstdio(void)
5514 return MSVCRT_max_streams;
5517 /*********************************************************************
5518 * _setmaxstdio (MSVCRT.@)
5520 int CDECL MSVCRT__setmaxstdio(int newmax)
5522 TRACE("%d\n", newmax);
5524 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5525 return -1;
5527 MSVCRT_max_streams = newmax;
5528 return MSVCRT_max_streams;
5531 /*********************************************************************
5532 * _get_stream_buffer_pointers (UCRTBASE.@)
5534 int CDECL MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE *file, char*** base,
5535 char*** ptr, int** count)
5537 if (base)
5538 *base = &file->_base;
5539 if (ptr)
5540 *ptr = &file->_ptr;
5541 if (count)
5542 *count = &file->_cnt;
5543 return 0;