TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / msvcrt / file.c
blob9f35a62101ec190b9844d0e406822a82a86b68d1
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(*size, 1);
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(MSVCRT_INTERNAL_BUFSIZ,1);
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 {
1025 ret = CloseHandle(info->handle) ? 0 : -1;
1026 msvcrt_free_fd(fd);
1027 if (ret) {
1028 WARN(":failed-last error (%d)\n",GetLastError());
1029 msvcrt_set_errno(GetLastError());
1032 release_ioinfo(info);
1033 return ret;
1036 /*********************************************************************
1037 * _dup2 (MSVCRT.@)
1038 * NOTES
1039 * MSDN isn't clear on this point, but the remarks for _pipe
1040 * indicate file descriptors duplicated with _dup and _dup2 are always
1041 * inheritable.
1043 int CDECL MSVCRT__dup2(int od, int nd)
1045 ioinfo *info_od, *info_nd;
1046 int ret;
1048 TRACE("(od=%d, nd=%d)\n", od, nd);
1050 if (od < nd)
1052 info_od = get_ioinfo(od);
1053 info_nd = get_ioinfo_alloc_fd(nd);
1055 else
1057 info_nd = get_ioinfo_alloc_fd(nd);
1058 info_od = get_ioinfo(od);
1061 if (info_nd == &MSVCRT___badioinfo)
1063 ret = -1;
1065 else if (info_od->wxflag & WX_OPEN)
1067 HANDLE handle;
1069 if (DuplicateHandle(GetCurrentProcess(), info_od->handle,
1070 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1072 int wxflag = info_od->wxflag & ~MSVCRT__O_NOINHERIT;
1074 if (info_nd->wxflag & WX_OPEN)
1075 MSVCRT__close(nd);
1077 msvcrt_set_fd(info_nd, handle, wxflag);
1078 /* _dup2 returns 0, not nd, on success */
1079 ret = 0;
1081 else
1083 ret = -1;
1084 msvcrt_set_errno(GetLastError());
1087 else
1089 *MSVCRT__errno() = MSVCRT_EBADF;
1090 ret = -1;
1093 release_ioinfo(info_od);
1094 release_ioinfo(info_nd);
1095 return ret;
1098 /*********************************************************************
1099 * _dup (MSVCRT.@)
1101 int CDECL MSVCRT__dup(int od)
1103 int fd, ret;
1104 ioinfo *info = get_ioinfo_alloc(&fd);
1106 if (MSVCRT__dup2(od, fd) == 0)
1107 ret = fd;
1108 else
1109 ret = -1;
1110 release_ioinfo(info);
1111 return ret;
1114 /*********************************************************************
1115 * _eof (MSVCRT.@)
1117 int CDECL MSVCRT__eof(int fd)
1119 ioinfo *info = get_ioinfo(fd);
1120 DWORD curpos,endpos;
1121 LONG hcurpos,hendpos;
1123 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1125 if (info->handle == INVALID_HANDLE_VALUE)
1127 release_ioinfo(info);
1128 return -1;
1131 if (info->wxflag & WX_ATEOF)
1133 release_ioinfo(info);
1134 return TRUE;
1137 /* Otherwise we do it the hard way */
1138 hcurpos = hendpos = 0;
1139 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1140 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1142 if (curpos == endpos && hcurpos == hendpos)
1144 /* FIXME: shouldn't WX_ATEOF be set here? */
1145 release_ioinfo(info);
1146 return TRUE;
1149 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1150 release_ioinfo(info);
1151 return FALSE;
1154 /*********************************************************************
1155 * _fcloseall (MSVCRT.@)
1157 int CDECL MSVCRT__fcloseall(void)
1159 int num_closed = 0, i;
1160 MSVCRT_FILE *file;
1162 LOCK_FILES();
1163 for (i = 3; i < MSVCRT_stream_idx; i++) {
1164 file = msvcrt_get_file(i);
1166 if (file->_flag && !MSVCRT_fclose(file))
1167 num_closed++;
1169 UNLOCK_FILES();
1171 TRACE(":closed (%d) handles\n",num_closed);
1172 return num_closed;
1175 /* free everything on process exit */
1176 void msvcrt_free_io(void)
1178 unsigned int i;
1179 int j;
1181 MSVCRT__flushall();
1182 MSVCRT__fcloseall();
1184 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1186 if(!MSVCRT___pioinfo[i])
1187 continue;
1189 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1191 if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
1192 DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
1194 MSVCRT_free(MSVCRT___pioinfo[i]);
1197 for(j=0; j<MSVCRT_stream_idx; j++)
1199 MSVCRT_FILE *file = msvcrt_get_file(j);
1200 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1202 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1203 DeleteCriticalSection(&((file_crit*)file)->crit);
1207 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1208 MSVCRT_free(MSVCRT_fstream[i]);
1211 /*********************************************************************
1212 * _lseeki64 (MSVCRT.@)
1214 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1216 ioinfo *info = get_ioinfo(fd);
1217 LARGE_INTEGER ofs;
1219 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1221 if (info->handle == INVALID_HANDLE_VALUE)
1223 release_ioinfo(info);
1224 return -1;
1227 if (whence < 0 || whence > 2)
1229 release_ioinfo(info);
1230 *MSVCRT__errno() = MSVCRT_EINVAL;
1231 return -1;
1234 TRACE(":fd (%d) to %s pos %s\n",
1235 fd,wine_dbgstr_longlong(offset),
1236 (whence==MSVCRT_SEEK_SET)?"SEEK_SET":
1237 (whence==MSVCRT_SEEK_CUR)?"SEEK_CUR":
1238 (whence==MSVCRT_SEEK_END)?"SEEK_END":"UNKNOWN");
1240 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1241 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1242 ofs.QuadPart = offset;
1243 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1244 GetLastError() == ERROR_SUCCESS)
1246 info->wxflag &= ~WX_ATEOF;
1247 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1249 release_ioinfo(info);
1250 return ofs.QuadPart;
1252 release_ioinfo(info);
1253 TRACE(":error-last error (%d)\n",GetLastError());
1254 msvcrt_set_errno(GetLastError());
1255 return -1;
1258 /*********************************************************************
1259 * _lseek (MSVCRT.@)
1261 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1263 return MSVCRT__lseeki64(fd, offset, whence);
1266 /*********************************************************************
1267 * _lock_file (MSVCRT.@)
1269 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1271 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1272 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1273 else
1274 EnterCriticalSection(&((file_crit*)file)->crit);
1277 /*********************************************************************
1278 * _unlock_file (MSVCRT.@)
1280 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1282 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1283 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1284 else
1285 LeaveCriticalSection(&((file_crit*)file)->crit);
1288 /*********************************************************************
1289 * _locking (MSVCRT.@)
1291 * This is untested; the underlying LockFile doesn't work yet.
1293 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1295 ioinfo *info = get_ioinfo(fd);
1296 BOOL ret;
1297 DWORD cur_locn;
1299 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1300 if (info->handle == INVALID_HANDLE_VALUE)
1302 release_ioinfo(info);
1303 return -1;
1306 if (mode < 0 || mode > 4)
1308 release_ioinfo(info);
1309 *MSVCRT__errno() = MSVCRT_EINVAL;
1310 return -1;
1313 TRACE(":fd (%d) by 0x%08x mode %s\n",
1314 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1315 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1316 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1317 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1318 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1319 "UNKNOWN");
1321 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1323 release_ioinfo(info);
1324 FIXME ("Seek failed\n");
1325 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1326 return -1;
1328 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1330 int nretry = 10;
1331 ret = 1; /* just to satisfy gcc */
1332 while (nretry--)
1334 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1335 if (ret) break;
1336 Sleep(1);
1339 else if (mode == MSVCRT__LK_UNLCK)
1340 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1341 else
1342 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1343 /* FIXME - what about error settings? */
1344 release_ioinfo(info);
1345 return ret ? 0 : -1;
1348 /*********************************************************************
1349 * _fseeki64 (MSVCRT.@)
1351 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1353 int ret;
1355 MSVCRT__lock_file(file);
1356 ret = MSVCRT__fseeki64_nolock(file, offset, whence);
1357 MSVCRT__unlock_file(file);
1359 return ret;
1362 /*********************************************************************
1363 * _fseeki64_nolock (MSVCRT.@)
1365 int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
1367 int ret;
1369 if(whence == MSVCRT_SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1370 whence = MSVCRT_SEEK_SET;
1371 offset += MSVCRT__ftelli64_nolock(file);
1374 /* Flush output if needed */
1375 msvcrt_flush_buffer(file);
1376 /* Reset direction of i/o */
1377 if(file->_flag & MSVCRT__IORW) {
1378 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1380 /* Clear end of file flag */
1381 file->_flag &= ~MSVCRT__IOEOF;
1382 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1384 return ret;
1387 /*********************************************************************
1388 * fseek (MSVCRT.@)
1390 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1392 return MSVCRT__fseeki64( file, offset, whence );
1395 /*********************************************************************
1396 * _fseek_nolock (MSVCRT.@)
1398 int CDECL MSVCRT__fseek_nolock(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1400 return MSVCRT__fseeki64_nolock( file, offset, whence );
1403 /*********************************************************************
1404 * _chsize_s (MSVCRT.@)
1406 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1408 ioinfo *info;
1409 __int64 cur, pos;
1410 BOOL ret = FALSE;
1412 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1414 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1417 info = get_ioinfo(fd);
1418 if (info->handle != INVALID_HANDLE_VALUE)
1420 /* save the current file pointer */
1421 cur = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1422 if (cur >= 0)
1424 pos = MSVCRT__lseeki64(fd, size, MSVCRT_SEEK_SET);
1425 if (pos >= 0)
1427 ret = SetEndOfFile(info->handle);
1428 if (!ret) msvcrt_set_errno(GetLastError());
1431 /* restore the file pointer */
1432 MSVCRT__lseeki64(fd, cur, MSVCRT_SEEK_SET);
1436 release_ioinfo(info);
1437 return ret ? 0 : *MSVCRT__errno();
1440 /*********************************************************************
1441 * _chsize (MSVCRT.@)
1443 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1445 /* _chsize_s returns errno on failure but _chsize should return -1 */
1446 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1449 /*********************************************************************
1450 * clearerr (MSVCRT.@)
1452 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1454 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1456 MSVCRT__lock_file(file);
1457 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1458 MSVCRT__unlock_file(file);
1461 /*********************************************************************
1462 * rewind (MSVCRT.@)
1464 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1466 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1468 MSVCRT__lock_file(file);
1469 MSVCRT__fseek_nolock(file, 0L, MSVCRT_SEEK_SET);
1470 MSVCRT_clearerr(file);
1471 MSVCRT__unlock_file(file);
1474 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1476 int plus = strchrW(mode, '+') != NULL;
1478 TRACE("%s\n", debugstr_w(mode));
1480 while(*mode == ' ') mode++;
1482 switch(*mode++)
1484 case 'R': case 'r':
1485 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1486 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1487 break;
1488 case 'W': case 'w':
1489 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1490 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1491 break;
1492 case 'A': case 'a':
1493 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1494 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1495 break;
1496 default:
1497 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1498 return -1;
1501 *stream_flags |= MSVCRT__commode;
1503 while (*mode && *mode!=',')
1504 switch (*mode++)
1506 case 'B': case 'b':
1507 *open_flags |= MSVCRT__O_BINARY;
1508 *open_flags &= ~MSVCRT__O_TEXT;
1509 break;
1510 case 't':
1511 *open_flags |= MSVCRT__O_TEXT;
1512 *open_flags &= ~MSVCRT__O_BINARY;
1513 break;
1514 case 'D':
1515 *open_flags |= MSVCRT__O_TEMPORARY;
1516 break;
1517 case 'T':
1518 *open_flags |= MSVCRT__O_SHORT_LIVED;
1519 break;
1520 case 'c':
1521 *stream_flags |= MSVCRT__IOCOMMIT;
1522 break;
1523 case 'n':
1524 *stream_flags &= ~MSVCRT__IOCOMMIT;
1525 break;
1526 case 'N':
1527 *open_flags |= MSVCRT__O_NOINHERIT;
1528 break;
1529 case '+':
1530 case ' ':
1531 case 'a':
1532 case 'w':
1533 break;
1534 case 'S':
1535 case 'R':
1536 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1537 break;
1538 default:
1539 ERR("incorrect mode flag: %c\n", mode[-1]);
1540 break;
1543 if(*mode == ',')
1545 static const WCHAR ccs[] = {'c','c','s'};
1546 static const WCHAR utf8[] = {'u','t','f','-','8'};
1547 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1548 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1550 mode++;
1551 while(*mode == ' ') mode++;
1552 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1553 return -1;
1554 mode += sizeof(ccs)/sizeof(ccs[0]);
1555 while(*mode == ' ') mode++;
1556 if(!MSVCRT_CHECK_PMT(*mode == '='))
1557 return -1;
1558 mode++;
1559 while(*mode == ' ') mode++;
1561 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1563 *open_flags |= MSVCRT__O_U8TEXT;
1564 mode += sizeof(utf8)/sizeof(utf8[0]);
1566 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1568 *open_flags |= MSVCRT__O_U16TEXT;
1569 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1571 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1573 *open_flags |= MSVCRT__O_WTEXT;
1574 mode += sizeof(unicode)/sizeof(unicode[0]);
1576 else
1578 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1579 return -1;
1582 while(*mode == ' ') mode++;
1585 if(!MSVCRT_CHECK_PMT(*mode == 0))
1586 return -1;
1587 return 0;
1590 /*********************************************************************
1591 * _fdopen (MSVCRT.@)
1593 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1595 MSVCRT_FILE *ret;
1596 MSVCRT_wchar_t *modeW = NULL;
1598 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1600 ret = MSVCRT__wfdopen(fd, modeW);
1602 MSVCRT_free(modeW);
1603 return ret;
1606 /*********************************************************************
1607 * _wfdopen (MSVCRT.@)
1609 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1611 int open_flags, stream_flags;
1612 MSVCRT_FILE* file;
1614 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1616 LOCK_FILES();
1617 if (!(file = msvcrt_alloc_fp()))
1618 file = NULL;
1619 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1621 file->_flag = 0;
1622 file = NULL;
1624 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1625 UNLOCK_FILES();
1627 return file;
1630 /*********************************************************************
1631 * _filelength (MSVCRT.@)
1633 LONG CDECL MSVCRT__filelength(int fd)
1635 LONG curPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
1636 if (curPos != -1)
1638 LONG endPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_END);
1639 if (endPos != -1)
1641 if (endPos != curPos)
1642 MSVCRT__lseek(fd, curPos, MSVCRT_SEEK_SET);
1643 return endPos;
1646 return -1;
1649 /*********************************************************************
1650 * _filelengthi64 (MSVCRT.@)
1652 __int64 CDECL MSVCRT__filelengthi64(int fd)
1654 __int64 curPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1655 if (curPos != -1)
1657 __int64 endPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_END);
1658 if (endPos != -1)
1660 if (endPos != curPos)
1661 MSVCRT__lseeki64(fd, curPos, MSVCRT_SEEK_SET);
1662 return endPos;
1665 return -1;
1668 /*********************************************************************
1669 * _fileno (MSVCRT.@)
1671 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1673 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1674 return file->_file;
1677 /*********************************************************************
1678 * _fstat64 (MSVCRT.@)
1680 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1682 ioinfo *info = get_ioinfo(fd);
1683 DWORD dw;
1684 DWORD type;
1685 BY_HANDLE_FILE_INFORMATION hfi;
1687 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1688 if (info->handle == INVALID_HANDLE_VALUE)
1690 release_ioinfo(info);
1691 return -1;
1694 if (!buf)
1696 WARN(":failed-NULL buf\n");
1697 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1698 release_ioinfo(info);
1699 return -1;
1702 memset(&hfi, 0, sizeof(hfi));
1703 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1704 type = GetFileType(info->handle);
1705 if (type == FILE_TYPE_PIPE)
1707 buf->st_dev = buf->st_rdev = fd;
1708 buf->st_mode = MSVCRT__S_IFIFO;
1709 buf->st_nlink = 1;
1711 else if (type == FILE_TYPE_CHAR)
1713 buf->st_dev = buf->st_rdev = fd;
1714 buf->st_mode = MSVCRT__S_IFCHR;
1715 buf->st_nlink = 1;
1717 else /* FILE_TYPE_DISK etc. */
1719 if (!GetFileInformationByHandle(info->handle, &hfi))
1721 WARN(":failed-last error (%d)\n",GetLastError());
1722 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1723 release_ioinfo(info);
1724 return -1;
1726 buf->st_mode = MSVCRT__S_IFREG | 0444;
1727 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1728 buf->st_mode |= 0222;
1729 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1730 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1731 buf->st_atime = dw;
1732 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1733 buf->st_mtime = buf->st_ctime = dw;
1734 buf->st_nlink = hfi.nNumberOfLinks;
1736 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1737 buf->st_mode);
1738 release_ioinfo(info);
1739 return 0;
1742 /*********************************************************************
1743 * _fstati64 (MSVCRT.@)
1745 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1747 int ret;
1748 struct MSVCRT__stat64 buf64;
1750 ret = MSVCRT__fstat64(fd, &buf64);
1751 if (!ret)
1752 msvcrt_stat64_to_stati64(&buf64, buf);
1753 return ret;
1756 /*********************************************************************
1757 * _fstat (MSVCRT.@)
1759 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1760 { int ret;
1761 struct MSVCRT__stat64 buf64;
1763 ret = MSVCRT__fstat64(fd, &buf64);
1764 if (!ret)
1765 msvcrt_stat64_to_stat(&buf64, buf);
1766 return ret;
1769 /*********************************************************************
1770 * _fstat32 (MSVCR80.@)
1772 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1774 int ret;
1775 struct MSVCRT__stat64 buf64;
1777 ret = MSVCRT__fstat64(fd, &buf64);
1778 if (!ret)
1779 msvcrt_stat64_to_stat32(&buf64, buf);
1780 return ret;
1783 /*********************************************************************
1784 * _fstat64i32 (MSVCR80.@)
1786 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1788 int ret;
1789 struct MSVCRT__stat64 buf64;
1791 ret = MSVCRT__fstat64(fd, &buf64);
1792 if (!ret)
1793 msvcrt_stat64_to_stat64i32(&buf64, buf);
1794 return ret;
1797 /*********************************************************************
1798 * _futime64 (MSVCRT.@)
1800 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1802 ioinfo *info = get_ioinfo(fd);
1803 FILETIME at, wt;
1805 if (!t)
1807 time_to_filetime( MSVCRT__time64(NULL), &at );
1808 wt = at;
1810 else
1812 time_to_filetime( t->actime, &at );
1813 time_to_filetime( t->modtime, &wt );
1816 if (!SetFileTime(info->handle, NULL, &at, &wt))
1818 release_ioinfo(info);
1819 msvcrt_set_errno(GetLastError());
1820 return -1 ;
1822 release_ioinfo(info);
1823 return 0;
1826 /*********************************************************************
1827 * _futime32 (MSVCRT.@)
1829 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1831 if (t)
1833 struct MSVCRT___utimbuf64 t64;
1834 t64.actime = t->actime;
1835 t64.modtime = t->modtime;
1836 return _futime64( fd, &t64 );
1838 else
1839 return _futime64( fd, NULL );
1842 /*********************************************************************
1843 * _futime (MSVCRT.@)
1845 #ifdef _WIN64
1846 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1848 return _futime64( fd, t );
1850 #else
1851 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1853 return _futime32( fd, t );
1855 #endif
1857 /*********************************************************************
1858 * _get_osfhandle (MSVCRT.@)
1860 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1862 HANDLE hand = get_ioinfo_nolock(fd)->handle;
1863 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1865 if(hand == INVALID_HANDLE_VALUE)
1866 *MSVCRT__errno() = MSVCRT_EBADF;
1867 return (MSVCRT_intptr_t)hand;
1870 /*********************************************************************
1871 * _mktemp_s (MSVCRT.@)
1873 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1875 DWORD len, xno, id;
1877 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1878 return MSVCRT_EINVAL;
1880 for(len=0; len<size; len++)
1881 if(!pattern[len])
1882 break;
1883 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1884 if(size)
1885 pattern[0] = 0;
1886 return MSVCRT_EINVAL;
1889 for(xno=1; xno<=6; xno++)
1890 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1891 return MSVCRT_EINVAL;
1893 id = GetCurrentProcessId();
1894 for(xno=1; xno<6; xno++) {
1895 pattern[len-xno] = id%10 + '0';
1896 id /= 10;
1899 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1900 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1901 return 0;
1904 pattern[0] = 0;
1905 *MSVCRT__errno() = MSVCRT_EEXIST;
1906 return MSVCRT_EEXIST;
1909 /*********************************************************************
1910 * _mktemp (MSVCRT.@)
1912 char * CDECL MSVCRT__mktemp(char *pattern)
1914 int numX = 0;
1915 char *retVal = pattern;
1916 int id;
1917 char letter = 'a';
1919 if(!pattern)
1920 return NULL;
1922 while(*pattern)
1923 numX = (*pattern++ == 'X')? numX + 1 : 0;
1924 if (numX < 6)
1925 return NULL;
1926 pattern--;
1927 id = GetCurrentProcessId();
1928 numX = 6;
1929 while(numX--)
1931 int tempNum = id / 10;
1932 *pattern-- = id - (tempNum * 10) + '0';
1933 id = tempNum;
1935 pattern++;
1938 *pattern = letter++;
1939 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1940 return retVal;
1941 } while(letter <= 'z');
1942 return NULL;
1945 /*********************************************************************
1946 * _wmktemp_s (MSVCRT.@)
1948 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1950 DWORD len, xno, id;
1952 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1953 return MSVCRT_EINVAL;
1955 for(len=0; len<size; len++)
1956 if(!pattern[len])
1957 break;
1958 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1959 if(size)
1960 pattern[0] = 0;
1961 return MSVCRT_EINVAL;
1964 for(xno=1; xno<=6; xno++)
1965 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1966 return MSVCRT_EINVAL;
1968 id = GetCurrentProcessId();
1969 for(xno=1; xno<6; xno++) {
1970 pattern[len-xno] = id%10 + '0';
1971 id /= 10;
1974 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1975 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1976 return 0;
1979 pattern[0] = 0;
1980 *MSVCRT__errno() = MSVCRT_EEXIST;
1981 return MSVCRT_EEXIST;
1984 /*********************************************************************
1985 * _wmktemp (MSVCRT.@)
1987 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1989 int numX = 0;
1990 MSVCRT_wchar_t *retVal = pattern;
1991 int id;
1992 MSVCRT_wchar_t letter = 'a';
1994 if(!pattern)
1995 return NULL;
1997 while(*pattern)
1998 numX = (*pattern++ == 'X')? numX + 1 : 0;
1999 if (numX < 6)
2000 return NULL;
2001 pattern--;
2002 id = GetCurrentProcessId();
2003 numX = 6;
2004 while(numX--)
2006 int tempNum = id / 10;
2007 *pattern-- = id - (tempNum * 10) + '0';
2008 id = tempNum;
2010 pattern++;
2013 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
2014 return retVal;
2015 *pattern = letter++;
2016 } while(letter != '|');
2017 return NULL;
2020 static unsigned split_oflags(unsigned oflags)
2022 int wxflags = 0;
2023 unsigned unsupp; /* until we support everything */
2025 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
2026 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
2027 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
2028 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
2029 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
2030 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
2031 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
2032 else wxflags |= WX_TEXT; /* default to TEXT*/
2033 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2035 if ((unsupp = oflags & ~(
2036 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
2037 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
2038 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
2039 MSVCRT__O_NOINHERIT|
2040 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
2041 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
2043 ERR(":unsupported oflags 0x%04x\n",unsupp);
2045 return wxflags;
2048 /*********************************************************************
2049 * _pipe (MSVCRT.@)
2051 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
2053 int ret = -1;
2054 SECURITY_ATTRIBUTES sa;
2055 HANDLE readHandle, writeHandle;
2057 if (!pfds)
2059 *MSVCRT__errno() = MSVCRT_EINVAL;
2060 return -1;
2063 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2064 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
2065 sa.lpSecurityDescriptor = NULL;
2066 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2068 unsigned int wxflags = split_oflags(textmode);
2069 int fd;
2071 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2072 if (fd != -1)
2074 pfds[0] = fd;
2075 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2076 if (fd != -1)
2078 pfds[1] = fd;
2079 ret = 0;
2081 else
2083 MSVCRT__close(pfds[0]);
2084 CloseHandle(writeHandle);
2085 *MSVCRT__errno() = MSVCRT_EMFILE;
2088 else
2090 CloseHandle(readHandle);
2091 CloseHandle(writeHandle);
2092 *MSVCRT__errno() = MSVCRT_EMFILE;
2095 else
2096 msvcrt_set_errno(GetLastError());
2098 return ret;
2101 static int check_bom(HANDLE h, int oflags, BOOL seek)
2103 char bom[sizeof(utf8_bom)];
2104 DWORD r;
2106 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2108 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2109 return oflags;
2111 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2112 oflags |= MSVCRT__O_U8TEXT;
2113 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2114 if (seek && r>2)
2115 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2116 oflags |= MSVCRT__O_U16TEXT;
2117 }else if (seek) {
2118 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2121 return oflags;
2124 /*********************************************************************
2125 * _wsopen_s (MSVCRT.@)
2127 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2129 DWORD access = 0, creation = 0, attrib;
2130 SECURITY_ATTRIBUTES sa;
2131 DWORD sharing, type;
2132 int wxflag;
2133 HANDLE hand;
2135 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2136 fd, debugstr_w(path), oflags, shflags, pmode);
2138 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2140 *fd = -1;
2141 wxflag = split_oflags(oflags);
2142 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2144 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2145 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2146 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2149 if (oflags & MSVCRT__O_CREAT)
2151 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
2152 FIXME(": pmode 0x%04x ignored\n", pmode);
2154 if (oflags & MSVCRT__O_EXCL)
2155 creation = CREATE_NEW;
2156 else if (oflags & MSVCRT__O_TRUNC)
2157 creation = CREATE_ALWAYS;
2158 else
2159 creation = OPEN_ALWAYS;
2161 else /* no MSVCRT__O_CREAT */
2163 if (oflags & MSVCRT__O_TRUNC)
2164 creation = TRUNCATE_EXISTING;
2165 else
2166 creation = OPEN_EXISTING;
2169 switch( shflags )
2171 case MSVCRT__SH_DENYRW:
2172 sharing = 0L;
2173 break;
2174 case MSVCRT__SH_DENYWR:
2175 sharing = FILE_SHARE_READ;
2176 break;
2177 case MSVCRT__SH_DENYRD:
2178 sharing = FILE_SHARE_WRITE;
2179 break;
2180 case MSVCRT__SH_DENYNO:
2181 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2182 break;
2183 default:
2184 ERR( "Unhandled shflags 0x%x\n", shflags );
2185 return MSVCRT_EINVAL;
2188 if (!(pmode & ~MSVCRT_umask & MSVCRT__S_IWRITE))
2189 attrib = FILE_ATTRIBUTE_READONLY;
2190 else
2191 attrib = FILE_ATTRIBUTE_NORMAL;
2193 if (oflags & MSVCRT__O_TEMPORARY)
2195 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2196 access |= DELETE;
2197 sharing |= FILE_SHARE_DELETE;
2200 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2201 sa.lpSecurityDescriptor = NULL;
2202 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2204 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2205 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2206 && !(access&GENERIC_READ))
2208 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2209 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2210 if (hand != INVALID_HANDLE_VALUE)
2212 oflags = check_bom(hand, oflags, FALSE);
2213 CloseHandle(hand);
2215 else
2216 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2219 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2220 if (hand == INVALID_HANDLE_VALUE) {
2221 WARN(":failed-last error (%d)\n",GetLastError());
2222 msvcrt_set_errno(GetLastError());
2223 return *MSVCRT__errno();
2226 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2228 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2229 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2230 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2232 if (oflags & MSVCRT__O_U8TEXT)
2234 DWORD written = 0, tmp;
2236 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2237 sizeof(utf8_bom)-written, &tmp, NULL))
2238 written += tmp;
2239 if (written != sizeof(utf8_bom)) {
2240 WARN("error writing BOM\n");
2241 CloseHandle(hand);
2242 msvcrt_set_errno(GetLastError());
2243 return *MSVCRT__errno();
2246 else
2248 DWORD written = 0, tmp;
2250 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2251 sizeof(utf16_bom)-written, &tmp, NULL))
2252 written += tmp;
2253 if (written != sizeof(utf16_bom))
2255 WARN("error writing BOM\n");
2256 CloseHandle(hand);
2257 msvcrt_set_errno(GetLastError());
2258 return *MSVCRT__errno();
2262 else if (access & GENERIC_READ)
2263 oflags = check_bom(hand, oflags, TRUE);
2266 type = GetFileType(hand);
2267 if (type == FILE_TYPE_CHAR)
2268 wxflag |= WX_TTY;
2269 else if (type == FILE_TYPE_PIPE)
2270 wxflag |= WX_PIPE;
2272 *fd = msvcrt_alloc_fd(hand, wxflag);
2273 if (*fd == -1)
2274 return *MSVCRT__errno();
2276 if (oflags & MSVCRT__O_WTEXT)
2277 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2278 else if (oflags & MSVCRT__O_U16TEXT)
2279 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
2280 else if (oflags & MSVCRT__O_U8TEXT)
2281 get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
2283 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2284 return 0;
2287 /*********************************************************************
2288 * _wsopen (MSVCRT.@)
2290 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2292 int pmode;
2293 int fd;
2295 if (oflags & MSVCRT__O_CREAT)
2297 __ms_va_list ap;
2299 __ms_va_start(ap, shflags);
2300 pmode = va_arg(ap, int);
2301 __ms_va_end(ap);
2303 else
2304 pmode = 0;
2306 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
2307 return fd;
2310 /*********************************************************************
2311 * _sopen_s (MSVCRT.@)
2313 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2315 MSVCRT_wchar_t *pathW;
2316 int ret;
2318 if (!MSVCRT_CHECK_PMT(fd != NULL))
2319 return MSVCRT_EINVAL;
2320 *fd = -1;
2321 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2322 return MSVCRT_EINVAL;
2324 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2325 MSVCRT_free(pathW);
2326 return ret;
2329 /*********************************************************************
2330 * _sopen (MSVCRT.@)
2332 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2334 int pmode;
2335 int fd;
2337 if (oflags & MSVCRT__O_CREAT)
2339 __ms_va_list ap;
2341 __ms_va_start(ap, shflags);
2342 pmode = va_arg(ap, int);
2343 __ms_va_end(ap);
2345 else
2346 pmode = 0;
2348 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2349 return fd;
2352 /*********************************************************************
2353 * _open (MSVCRT.@)
2355 int CDECL MSVCRT__open( const char *path, int flags, ... )
2357 __ms_va_list ap;
2359 if (flags & MSVCRT__O_CREAT)
2361 int pmode;
2362 __ms_va_start(ap, flags);
2363 pmode = va_arg(ap, int);
2364 __ms_va_end(ap);
2365 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2367 else
2368 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2371 /*********************************************************************
2372 * _wopen (MSVCRT.@)
2374 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2376 __ms_va_list ap;
2378 if (flags & MSVCRT__O_CREAT)
2380 int pmode;
2381 __ms_va_start(ap, flags);
2382 pmode = va_arg(ap, int);
2383 __ms_va_end(ap);
2384 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2386 else
2387 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2390 /*********************************************************************
2391 * _creat (MSVCRT.@)
2393 int CDECL MSVCRT__creat(const char *path, int flags)
2395 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2396 return MSVCRT__open(path, usedFlags);
2399 /*********************************************************************
2400 * _wcreat (MSVCRT.@)
2402 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2404 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2405 return MSVCRT__wopen(path, usedFlags);
2408 /*********************************************************************
2409 * _open_osfhandle (MSVCRT.@)
2411 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2413 DWORD flags;
2414 int fd;
2416 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2417 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2418 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2419 * text - it never sets MSVCRT__O_BINARY.
2421 /* don't let split_oflags() decide the mode if no mode is passed */
2422 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2423 oflags |= MSVCRT__O_BINARY;
2425 flags = GetFileType((HANDLE)handle);
2426 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2428 msvcrt_set_errno(GetLastError());
2429 return -1;
2432 if (flags == FILE_TYPE_CHAR)
2433 flags = WX_TTY;
2434 else if (flags == FILE_TYPE_PIPE)
2435 flags = WX_PIPE;
2436 else
2437 flags = 0;
2438 flags |= split_oflags(oflags);
2440 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2441 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2442 return fd;
2445 /*********************************************************************
2446 * _rmtmp (MSVCRT.@)
2448 int CDECL MSVCRT__rmtmp(void)
2450 int num_removed = 0, i;
2451 MSVCRT_FILE *file;
2453 LOCK_FILES();
2454 for (i = 3; i < MSVCRT_stream_idx; i++) {
2455 file = msvcrt_get_file(i);
2457 if (file->_tmpfname)
2459 MSVCRT_fclose(file);
2460 num_removed++;
2463 UNLOCK_FILES();
2465 if (num_removed)
2466 TRACE(":removed (%d) temp files\n",num_removed);
2467 return num_removed;
2470 static inline int get_utf8_char_len(char ch)
2472 if((ch&0xf8) == 0xf0)
2473 return 4;
2474 else if((ch&0xf0) == 0xe0)
2475 return 3;
2476 else if((ch&0xe0) == 0xc0)
2477 return 2;
2478 return 1;
2481 /*********************************************************************
2482 * (internal) read_utf8
2484 static int read_utf8(ioinfo *fdinfo, MSVCRT_wchar_t *buf, unsigned int count)
2486 HANDLE hand = fdinfo->handle;
2487 char min_buf[4], *readbuf, lookahead;
2488 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2490 /* make the buffer big enough to hold at least one character */
2491 /* read bytes have to fit to output and lookahead buffers */
2492 count /= 2;
2493 readbuf_size = count < 4 ? 4 : count;
2494 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2495 readbuf_size = 4;
2496 readbuf = min_buf;
2499 if(fdinfo->lookahead[0] != '\n') {
2500 readbuf[pos++] = fdinfo->lookahead[0];
2501 fdinfo->lookahead[0] = '\n';
2503 if(fdinfo->lookahead[1] != '\n') {
2504 readbuf[pos++] = fdinfo->lookahead[1];
2505 fdinfo->lookahead[1] = '\n';
2507 if(fdinfo->lookahead[2] != '\n') {
2508 readbuf[pos++] = fdinfo->lookahead[2];
2509 fdinfo->lookahead[2] = '\n';
2514 /* NOTE: this case is broken in native dll, reading
2515 * sometimes fails when small buffer is passed
2517 if(count < 4) {
2518 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2519 if (GetLastError() == ERROR_BROKEN_PIPE) {
2520 fdinfo->wxflag |= WX_ATEOF;
2521 return 0;
2522 }else {
2523 msvcrt_set_errno(GetLastError());
2524 return -1;
2526 }else if(!num_read) {
2527 fdinfo->wxflag |= WX_ATEOF;
2528 return 0;
2529 }else {
2530 pos++;
2533 char_len = get_utf8_char_len(readbuf[0]);
2534 if(char_len>pos) {
2535 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2536 pos += num_read;
2539 if(readbuf[0] == '\n')
2540 fdinfo->wxflag |= WX_READNL;
2541 else
2542 fdinfo->wxflag &= ~WX_READNL;
2544 if(readbuf[0] == 0x1a) {
2545 fdinfo->wxflag |= WX_ATEOF;
2546 return 0;
2549 if(readbuf[0] == '\r') {
2550 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2551 buf[0] = '\r';
2552 else if(lookahead == '\n')
2553 buf[0] = '\n';
2554 else {
2555 buf[0] = '\r';
2556 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2557 fdinfo->lookahead[0] = lookahead;
2558 else
2559 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2561 return 2;
2564 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2565 msvcrt_set_errno(GetLastError());
2566 return -1;
2569 return num_read*2;
2572 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2573 if(pos) {
2574 num_read = 0;
2575 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2576 fdinfo->wxflag |= WX_ATEOF;
2577 if (readbuf != min_buf) MSVCRT_free(readbuf);
2578 return 0;
2579 }else {
2580 msvcrt_set_errno(GetLastError());
2581 if (readbuf != min_buf) MSVCRT_free(readbuf);
2582 return -1;
2584 }else if(!pos && !num_read) {
2585 fdinfo->wxflag |= WX_ATEOF;
2586 if (readbuf != min_buf) MSVCRT_free(readbuf);
2587 return 0;
2590 pos += num_read;
2591 if(readbuf[0] == '\n')
2592 fdinfo->wxflag |= WX_READNL;
2593 else
2594 fdinfo->wxflag &= ~WX_READNL;
2596 /* Find first byte of last character (may be incomplete) */
2597 for(i=pos-1; i>0 && i>pos-4; i--)
2598 if((readbuf[i]&0xc0) != 0x80)
2599 break;
2600 char_len = get_utf8_char_len(readbuf[i]);
2601 if(char_len+i <= pos)
2602 i += char_len;
2604 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2605 if(i < pos)
2606 fdinfo->lookahead[0] = readbuf[i];
2607 if(i+1 < pos)
2608 fdinfo->lookahead[1] = readbuf[i+1];
2609 if(i+2 < pos)
2610 fdinfo->lookahead[2] = readbuf[i+2];
2611 }else if(i < pos) {
2612 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2614 pos = i;
2616 for(i=0, j=0; i<pos; i++) {
2617 if(readbuf[i] == 0x1a) {
2618 fdinfo->wxflag |= WX_ATEOF;
2619 break;
2622 /* strip '\r' if followed by '\n' */
2623 if(readbuf[i] == '\r' && i+1==pos) {
2624 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2625 readbuf[j++] = '\r';
2626 }else if(lookahead == '\n' && j==0) {
2627 readbuf[j++] = '\n';
2628 }else {
2629 if(lookahead != '\n')
2630 readbuf[j++] = '\r';
2632 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2633 fdinfo->lookahead[0] = lookahead;
2634 else
2635 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2637 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2638 readbuf[j++] = readbuf[i];
2641 pos = j;
2643 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2644 msvcrt_set_errno(GetLastError());
2645 if (readbuf != min_buf) MSVCRT_free(readbuf);
2646 return -1;
2649 if (readbuf != min_buf) MSVCRT_free(readbuf);
2650 return num_read*2;
2653 /*********************************************************************
2654 * (internal) read_i
2656 * When reading \r as last character in text mode, read() positions
2657 * the file pointer on the \r character while getc() goes on to
2658 * the following \n
2660 static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
2662 DWORD num_read, utf16;
2663 char *bufstart = buf;
2665 if (count == 0)
2666 return 0;
2668 if (fdinfo->wxflag & WX_ATEOF) {
2669 TRACE("already at EOF, returning 0\n");
2670 return 0;
2672 /* Don't trace small reads, it gets *very* annoying */
2673 if (count > 4)
2674 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2675 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2677 *MSVCRT__errno() = MSVCRT_EBADF;
2678 return -1;
2681 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2682 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2684 *MSVCRT__errno() = MSVCRT_EINVAL;
2685 return -1;
2688 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2689 return read_utf8(fdinfo, buf, count);
2691 if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
2693 if (fdinfo->lookahead[0] != '\n')
2695 bufstart[0] = fdinfo->lookahead[0];
2696 fdinfo->lookahead[0] = '\n';
2698 if (utf16)
2700 bufstart[1] = fdinfo->lookahead[1];
2701 fdinfo->lookahead[1] = '\n';
2704 if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2705 num_read += 1+utf16;
2706 else
2707 num_read = 1+utf16;
2710 if(utf16 && (num_read&1))
2712 /* msvcr90 uses uninitialized value from the buffer in this case */
2713 /* msvcrt ignores additional data */
2714 ERR("got odd number of bytes in UTF16 mode\n");
2715 num_read--;
2718 if (count != 0 && num_read == 0)
2720 fdinfo->wxflag |= WX_ATEOF;
2721 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2723 else if (fdinfo->wxflag & WX_TEXT)
2725 DWORD i, j;
2727 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2728 fdinfo->wxflag |= WX_READNL;
2729 else
2730 fdinfo->wxflag &= ~WX_READNL;
2732 for (i=0, j=0; i<num_read; i+=1+utf16)
2734 /* in text mode, a ctrl-z signals EOF */
2735 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2737 fdinfo->wxflag |= WX_ATEOF;
2738 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2739 break;
2742 /* in text mode, strip \r if followed by \n */
2743 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2745 char lookahead[2];
2746 DWORD len;
2748 lookahead[1] = '\n';
2749 if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
2751 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2753 bufstart[j++] = '\n';
2754 if(utf16) bufstart[j++] = 0;
2756 else
2758 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2760 bufstart[j++] = '\r';
2761 if(utf16) bufstart[j++] = 0;
2764 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2766 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2768 bufstart[j++] = '\n';
2769 if (utf16) bufstart[j++] = 0;
2771 else
2773 fdinfo->lookahead[0] = lookahead[0];
2774 fdinfo->lookahead[1] = lookahead[1];
2777 else
2778 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2781 else
2783 bufstart[j++] = '\r';
2784 if(utf16) bufstart[j++] = 0;
2787 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2788 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2790 bufstart[j++] = bufstart[i];
2791 if(utf16) bufstart[j++] = bufstart[i+1];
2794 num_read = j;
2797 else
2799 if (GetLastError() == ERROR_BROKEN_PIPE)
2801 TRACE(":end-of-pipe\n");
2802 fdinfo->wxflag |= WX_ATEOF;
2803 return 0;
2805 else
2807 TRACE(":failed-last error (%d)\n",GetLastError());
2808 return -1;
2812 if (count > 4)
2813 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2814 return num_read;
2817 /*********************************************************************
2818 * _read (MSVCRT.@)
2820 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2822 ioinfo *info = get_ioinfo(fd);
2823 int num_read = read_i(fd, info, buf, count);
2824 release_ioinfo(info);
2825 return num_read;
2828 /*********************************************************************
2829 * _setmode (MSVCRT.@)
2831 int CDECL MSVCRT__setmode(int fd,int mode)
2833 ioinfo *info = get_ioinfo(fd);
2834 int ret = info->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2835 if(ret==MSVCRT__O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16)))
2836 ret = MSVCRT__O_WTEXT;
2838 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2839 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2840 *MSVCRT__errno() = MSVCRT_EINVAL;
2841 release_ioinfo(info);
2842 return -1;
2845 if(info == &MSVCRT___badioinfo) {
2846 *MSVCRT__errno() = MSVCRT_EBADF;
2847 return MSVCRT_EOF;
2850 if(mode == MSVCRT__O_BINARY) {
2851 info->wxflag &= ~WX_TEXT;
2852 info->exflag &= ~(EF_UTF8|EF_UTF16);
2853 release_ioinfo(info);
2854 return ret;
2857 info->wxflag |= WX_TEXT;
2858 if(mode == MSVCRT__O_TEXT)
2859 info->exflag &= ~(EF_UTF8|EF_UTF16);
2860 else if(mode == MSVCRT__O_U8TEXT)
2861 info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
2862 else
2863 info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
2865 release_ioinfo(info);
2866 return ret;
2869 /*********************************************************************
2870 * _stat64 (MSVCRT.@)
2872 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2874 DWORD dw;
2875 WIN32_FILE_ATTRIBUTE_DATA hfi;
2876 unsigned short mode = ALL_S_IREAD;
2877 int plen;
2879 TRACE(":file (%s) buf(%p)\n",path,buf);
2881 plen = strlen(path);
2882 while (plen && path[plen-1]==' ')
2883 plen--;
2885 if (plen && (plen<2 || path[plen-2]!=':') &&
2886 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2888 *MSVCRT__errno() = MSVCRT_ENOENT;
2889 return -1;
2892 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2894 TRACE("failed (%d)\n",GetLastError());
2895 *MSVCRT__errno() = MSVCRT_ENOENT;
2896 return -1;
2899 memset(buf,0,sizeof(struct MSVCRT__stat64));
2901 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2902 Bon 011120: This FIXME seems incorrect
2903 Also a letter as first char isn't enough to be classified
2904 as a drive letter
2906 if (isalpha(*path)&& (*(path+1)==':'))
2907 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2908 else
2909 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2911 /* Dir, or regular file? */
2912 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2913 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2914 else
2916 mode |= MSVCRT__S_IFREG;
2917 /* executable? */
2918 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2920 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2921 (tolower(path[plen-3]) << 16);
2922 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2923 mode |= ALL_S_IEXEC;
2927 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2928 mode |= ALL_S_IWRITE;
2930 buf->st_mode = mode;
2931 buf->st_nlink = 1;
2932 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2933 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2934 buf->st_atime = dw;
2935 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2936 buf->st_mtime = buf->st_ctime = dw;
2937 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2938 (int)(buf->st_size >> 32),(int)buf->st_size,
2939 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2940 return 0;
2943 /*********************************************************************
2944 * _stati64 (MSVCRT.@)
2946 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2948 int ret;
2949 struct MSVCRT__stat64 buf64;
2951 ret = MSVCRT_stat64(path, &buf64);
2952 if (!ret)
2953 msvcrt_stat64_to_stati64(&buf64, buf);
2954 return ret;
2957 /*********************************************************************
2958 * _stat (MSVCRT.@)
2960 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2962 int ret;
2963 struct MSVCRT__stat64 buf64;
2965 ret = MSVCRT_stat64( path, &buf64);
2966 if (!ret)
2967 msvcrt_stat64_to_stat(&buf64, buf);
2968 return ret;
2971 /*********************************************************************
2972 * _stat32 (MSVCR100.@)
2974 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
2976 int ret;
2977 struct MSVCRT__stat64 buf64;
2979 ret = MSVCRT_stat64(path, &buf64);
2980 if (!ret)
2981 msvcrt_stat64_to_stat32(&buf64, buf);
2982 return ret;
2985 /*********************************************************************
2986 * _stat32i64 (MSVCR100.@)
2988 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
2990 int ret;
2991 struct MSVCRT__stat64 buf64;
2993 ret = MSVCRT_stat64(path, &buf64);
2994 if (!ret)
2995 msvcrt_stat64_to_stat32i64(&buf64, buf);
2996 return ret;
2999 /*********************************************************************
3000 * _stat64i32 (MSVCR100.@)
3002 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
3004 int ret;
3005 struct MSVCRT__stat64 buf64;
3007 ret = MSVCRT_stat64(path, &buf64);
3008 if (!ret)
3009 msvcrt_stat64_to_stat64i32(&buf64, buf);
3010 return ret;
3013 /*********************************************************************
3014 * _wstat64 (MSVCRT.@)
3016 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
3018 DWORD dw;
3019 WIN32_FILE_ATTRIBUTE_DATA hfi;
3020 unsigned short mode = ALL_S_IREAD;
3021 int plen;
3023 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
3025 plen = strlenW(path);
3026 while (plen && path[plen-1]==' ')
3027 plen--;
3029 if(plen && (plen<2 || path[plen-2]!=':') &&
3030 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
3032 *MSVCRT__errno() = MSVCRT_ENOENT;
3033 return -1;
3036 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
3038 TRACE("failed (%d)\n",GetLastError());
3039 *MSVCRT__errno() = MSVCRT_ENOENT;
3040 return -1;
3043 memset(buf,0,sizeof(struct MSVCRT__stat64));
3045 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3046 if (MSVCRT_iswalpha(*path))
3047 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
3048 else
3049 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3051 /* Dir, or regular file? */
3052 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3053 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3054 else
3056 mode |= MSVCRT__S_IFREG;
3057 /* executable? */
3058 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3060 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
3061 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
3062 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3063 mode |= ALL_S_IEXEC;
3067 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3068 mode |= ALL_S_IWRITE;
3070 buf->st_mode = mode;
3071 buf->st_nlink = 1;
3072 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3073 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3074 buf->st_atime = dw;
3075 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3076 buf->st_mtime = buf->st_ctime = dw;
3077 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3078 (int)(buf->st_size >> 32),(int)buf->st_size,
3079 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3080 return 0;
3083 /*********************************************************************
3084 * _wstati64 (MSVCRT.@)
3086 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
3088 int ret;
3089 struct MSVCRT__stat64 buf64;
3091 ret = MSVCRT__wstat64(path, &buf64);
3092 if (!ret)
3093 msvcrt_stat64_to_stati64(&buf64, buf);
3094 return ret;
3097 /*********************************************************************
3098 * _wstat (MSVCRT.@)
3100 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
3102 int ret;
3103 struct MSVCRT__stat64 buf64;
3105 ret = MSVCRT__wstat64( path, &buf64 );
3106 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3107 return ret;
3110 /*********************************************************************
3111 * _wstat32 (MSVCR100.@)
3113 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
3115 int ret;
3116 struct MSVCRT__stat64 buf64;
3118 ret = MSVCRT__wstat64(path, &buf64);
3119 if (!ret)
3120 msvcrt_stat64_to_stat32(&buf64, buf);
3121 return ret;
3124 /*********************************************************************
3125 * _wstat32i64 (MSVCR100.@)
3127 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
3129 int ret;
3130 struct MSVCRT__stat64 buf64;
3132 ret = MSVCRT__wstat64(path, &buf64);
3133 if (!ret)
3134 msvcrt_stat64_to_stat32i64(&buf64, buf);
3135 return ret;
3138 /*********************************************************************
3139 * _wstat64i32 (MSVCR100.@)
3141 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3143 int ret;
3144 struct MSVCRT__stat64 buf64;
3146 ret = MSVCRT__wstat64(path, &buf64);
3147 if (!ret)
3148 msvcrt_stat64_to_stat64i32(&buf64, buf);
3149 return ret;
3152 /*********************************************************************
3153 * _tell (MSVCRT.@)
3155 MSVCRT_long CDECL MSVCRT__tell(int fd)
3157 return MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
3160 /*********************************************************************
3161 * _telli64 (MSVCRT.@)
3163 __int64 CDECL _telli64(int fd)
3165 return MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
3168 /*********************************************************************
3169 * _tempnam (MSVCRT.@)
3171 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3173 char tmpbuf[MAX_PATH];
3174 const char *tmp_dir = MSVCRT_getenv("TMP");
3176 if (tmp_dir) dir = tmp_dir;
3178 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3179 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3181 TRACE("got name (%s)\n",tmpbuf);
3182 DeleteFileA(tmpbuf);
3183 return MSVCRT__strdup(tmpbuf);
3185 TRACE("failed (%d)\n",GetLastError());
3186 return NULL;
3189 /*********************************************************************
3190 * _wtempnam (MSVCRT.@)
3192 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3194 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3195 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3196 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3198 if (tmp_dir) dir = tmp_dir;
3200 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3201 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3203 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3204 DeleteFileW(tmpbuf);
3205 return MSVCRT__wcsdup(tmpbuf);
3207 TRACE("failed (%d)\n",GetLastError());
3208 return NULL;
3211 /*********************************************************************
3212 * _umask (MSVCRT.@)
3214 int CDECL MSVCRT__umask(int umask)
3216 int old_umask = MSVCRT_umask;
3217 TRACE("(%d)\n",umask);
3218 MSVCRT_umask = umask;
3219 return old_umask;
3222 /*********************************************************************
3223 * _utime64 (MSVCRT.@)
3225 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3227 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3229 if (fd > 0)
3231 int retVal = _futime64(fd, t);
3232 MSVCRT__close(fd);
3233 return retVal;
3235 return -1;
3238 /*********************************************************************
3239 * _utime32 (MSVCRT.@)
3241 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3243 if (t)
3245 struct MSVCRT___utimbuf64 t64;
3246 t64.actime = t->actime;
3247 t64.modtime = t->modtime;
3248 return _utime64( path, &t64 );
3250 else
3251 return _utime64( path, NULL );
3254 /*********************************************************************
3255 * _utime (MSVCRT.@)
3257 #ifdef _WIN64
3258 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3260 return _utime64( path, t );
3262 #else
3263 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3265 return _utime32( path, t );
3267 #endif
3269 /*********************************************************************
3270 * _wutime64 (MSVCRT.@)
3272 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3274 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3276 if (fd > 0)
3278 int retVal = _futime64(fd, t);
3279 MSVCRT__close(fd);
3280 return retVal;
3282 return -1;
3285 /*********************************************************************
3286 * _wutime32 (MSVCRT.@)
3288 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3290 if (t)
3292 struct MSVCRT___utimbuf64 t64;
3293 t64.actime = t->actime;
3294 t64.modtime = t->modtime;
3295 return _wutime64( path, &t64 );
3297 else
3298 return _wutime64( path, NULL );
3301 /*********************************************************************
3302 * _wutime (MSVCRT.@)
3304 #ifdef _WIN64
3305 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3307 return _wutime64( path, t );
3309 #else
3310 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3312 return _wutime32( path, t );
3314 #endif
3316 /*********************************************************************
3317 * _write (MSVCRT.@)
3319 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3321 DWORD num_written;
3322 ioinfo *info = get_ioinfo(fd);
3323 HANDLE hand = info->handle;
3325 /* Don't trace small writes, it gets *very* annoying */
3326 #if 0
3327 if (count > 32)
3328 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3329 #endif
3330 if (hand == INVALID_HANDLE_VALUE)
3332 *MSVCRT__errno() = MSVCRT_EBADF;
3333 release_ioinfo(info);
3334 return -1;
3337 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3339 *MSVCRT__errno() = MSVCRT_EINVAL;
3340 release_ioinfo(info);
3341 return -1;
3344 /* If appending, go to EOF */
3345 if (info->wxflag & WX_APPEND)
3346 MSVCRT__lseek(fd, 0, FILE_END);
3348 if (!(info->wxflag & WX_TEXT))
3350 if (WriteFile(hand, buf, count, &num_written, NULL)
3351 && (num_written == count))
3353 release_ioinfo(info);
3354 return num_written;
3356 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3357 hand, GetLastError());
3358 *MSVCRT__errno() = MSVCRT_ENOSPC;
3360 else
3362 unsigned int i, j, nr_lf, size;
3363 char *p = NULL;
3364 const char *q;
3365 const char *s = buf, *buf_start = buf;
3367 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3369 /* find number of \n */
3370 for (nr_lf=0, i=0; i<count; i++)
3371 if (s[i] == '\n')
3372 nr_lf++;
3373 if (nr_lf)
3375 size = count+nr_lf;
3376 if ((q = p = MSVCRT_malloc(size)))
3378 for (s = buf, i = 0, j = 0; i < count; i++)
3380 if (s[i] == '\n')
3381 p[j++] = '\r';
3382 p[j++] = s[i];
3385 else
3387 FIXME("Malloc failed\n");
3388 nr_lf = 0;
3389 size = count;
3390 q = buf;
3393 else
3395 size = count;
3396 q = buf;
3399 else if (info->exflag & EF_UTF16)
3401 for (nr_lf=0, i=0; i<count; i+=2)
3402 if (s[i]=='\n' && s[i+1]==0)
3403 nr_lf += 2;
3404 if (nr_lf)
3406 size = count+nr_lf;
3407 if ((q = p = MSVCRT_malloc(size)))
3409 for (s=buf, i=0, j=0; i<count; i++)
3411 if (s[i]=='\n' && s[i+1]==0)
3413 p[j++] = '\r';
3414 p[j++] = 0;
3416 p[j++] = s[i++];
3417 p[j++] = s[i];
3420 else
3422 FIXME("Malloc failed\n");
3423 nr_lf = 0;
3424 size = count;
3425 q = buf;
3428 else
3430 size = count;
3431 q = buf;
3434 else
3436 DWORD conv_len;
3438 for(nr_lf=0, i=0; i<count; i+=2)
3439 if (s[i]=='\n' && s[i+1]==0)
3440 nr_lf++;
3442 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3443 if(!conv_len) {
3444 msvcrt_set_errno(GetLastError());
3445 MSVCRT_free(p);
3446 release_ioinfo(info);
3447 return -1;
3450 size = conv_len+nr_lf;
3451 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3453 for (s=buf, i=0, j=0; i<count; i++)
3455 if (s[i]=='\n' && s[i+1]==0)
3457 p[j++] = '\r';
3458 p[j++] = 0;
3460 p[j++] = s[i++];
3461 p[j++] = s[i];
3463 q = p+count+nr_lf*2;
3464 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3465 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3467 else
3469 FIXME("Malloc failed\n");
3470 nr_lf = 0;
3471 size = count;
3472 q = buf;
3476 if (!WriteFile(hand, q, size, &num_written, NULL))
3477 num_written = -1;
3478 release_ioinfo(info);
3479 if(p)
3480 MSVCRT_free(p);
3481 if (num_written != size)
3483 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3484 fd, hand, GetLastError(), num_written);
3485 *MSVCRT__errno() = MSVCRT_ENOSPC;
3486 return s - buf_start;
3488 return count;
3491 release_ioinfo(info);
3492 return -1;
3495 /*********************************************************************
3496 * _putw (MSVCRT.@)
3498 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3500 int len;
3502 MSVCRT__lock_file(file);
3503 len = MSVCRT__write(file->_file, &val, sizeof(val));
3504 if (len == sizeof(val)) {
3505 MSVCRT__unlock_file(file);
3506 return val;
3509 file->_flag |= MSVCRT__IOERR;
3510 MSVCRT__unlock_file(file);
3511 return MSVCRT_EOF;
3514 /*********************************************************************
3515 * fclose (MSVCRT.@)
3517 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3519 int ret;
3521 MSVCRT__lock_file(file);
3522 ret = MSVCRT__fclose_nolock(file);
3523 MSVCRT__unlock_file(file);
3525 return ret;
3528 /*********************************************************************
3529 * _fclose_nolock (MSVCRT.@)
3531 int CDECL MSVCRT__fclose_nolock(MSVCRT_FILE* file)
3533 int r, flag;
3535 flag = file->_flag;
3536 MSVCRT_free(file->_tmpfname);
3537 file->_tmpfname = NULL;
3538 /* flush stdio buffers */
3539 if(file->_flag & MSVCRT__IOWRT)
3540 MSVCRT__fflush_nolock(file);
3541 if(file->_flag & MSVCRT__IOMYBUF)
3542 MSVCRT_free(file->_base);
3544 r=MSVCRT__close(file->_file);
3545 file->_flag = 0;
3547 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3550 /*********************************************************************
3551 * feof (MSVCRT.@)
3553 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3555 return file->_flag & MSVCRT__IOEOF;
3558 /*********************************************************************
3559 * ferror (MSVCRT.@)
3561 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3563 return file->_flag & MSVCRT__IOERR;
3566 /*********************************************************************
3567 * _filbuf (MSVCRT.@)
3569 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3571 unsigned char c;
3573 if(file->_flag & MSVCRT__IOSTRG)
3574 return MSVCRT_EOF;
3576 /* Allocate buffer if needed */
3577 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3578 msvcrt_alloc_buffer(file);
3580 if(!(file->_flag & MSVCRT__IOREAD)) {
3581 if(file->_flag & MSVCRT__IORW)
3582 file->_flag |= MSVCRT__IOREAD;
3583 else
3584 return MSVCRT_EOF;
3587 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3588 int r;
3589 if ((r = MSVCRT__read(file->_file,&c,1)) != 1) {
3590 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3591 return MSVCRT_EOF;
3594 return c;
3595 } else {
3596 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3597 if(file->_cnt<=0) {
3598 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3599 file->_cnt = 0;
3600 return MSVCRT_EOF;
3603 file->_cnt--;
3604 file->_ptr = file->_base+1;
3605 c = *(unsigned char *)file->_base;
3606 return c;
3610 /*********************************************************************
3611 * fgetc (MSVCRT.@)
3613 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3615 int ret;
3617 MSVCRT__lock_file(file);
3618 ret = MSVCRT__fgetc_nolock(file);
3619 MSVCRT__unlock_file(file);
3621 return ret;
3624 /*********************************************************************
3625 * _fgetc_nolock (MSVCRT.@)
3627 int CDECL MSVCRT__fgetc_nolock(MSVCRT_FILE* file)
3629 unsigned char *i;
3630 unsigned int j;
3632 if (file->_cnt>0) {
3633 file->_cnt--;
3634 i = (unsigned char *)file->_ptr++;
3635 j = *i;
3636 } else
3637 j = MSVCRT__filbuf(file);
3639 return j;
3642 /*********************************************************************
3643 * _fgetchar (MSVCRT.@)
3645 int CDECL MSVCRT__fgetchar(void)
3647 return MSVCRT_fgetc(MSVCRT_stdin);
3650 /*********************************************************************
3651 * fgets (MSVCRT.@)
3653 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3655 int cc = MSVCRT_EOF;
3656 char * buf_start = s;
3658 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3659 file,file->_file,s,size);
3661 MSVCRT__lock_file(file);
3663 while ((size >1) && (cc = MSVCRT__fgetc_nolock(file)) != MSVCRT_EOF && cc != '\n')
3665 *s++ = (char)cc;
3666 size --;
3668 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3670 TRACE(":nothing read\n");
3671 MSVCRT__unlock_file(file);
3672 return NULL;
3674 if ((cc != MSVCRT_EOF) && (size > 1))
3675 *s++ = cc;
3676 *s = '\0';
3677 TRACE(":got %s\n", debugstr_a(buf_start));
3678 MSVCRT__unlock_file(file);
3679 return buf_start;
3682 /*********************************************************************
3683 * fgetwc (MSVCRT.@)
3685 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3687 MSVCRT_wint_t ret;
3689 MSVCRT__lock_file(file);
3690 ret = MSVCRT__fgetwc_nolock(file);
3691 MSVCRT__unlock_file(file);
3693 return ret;
3696 /*********************************************************************
3697 * _fgetwc_nolock (MSVCRT.@)
3699 MSVCRT_wint_t CDECL MSVCRT__fgetwc_nolock(MSVCRT_FILE* file)
3701 MSVCRT_wint_t ret;
3702 int ch;
3704 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3705 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3706 char *p;
3708 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3709 ch = MSVCRT__fgetc_nolock(file);
3710 if(ch == MSVCRT_EOF) {
3711 ret = MSVCRT_WEOF;
3712 break;
3714 *p = (char)ch;
3716 }else {
3717 char mbs[MSVCRT_MB_LEN_MAX];
3718 int len = 0;
3720 ch = MSVCRT__fgetc_nolock(file);
3721 if(ch != MSVCRT_EOF) {
3722 mbs[0] = (char)ch;
3723 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3724 ch = MSVCRT__fgetc_nolock(file);
3725 if(ch != MSVCRT_EOF) {
3726 mbs[1] = (char)ch;
3727 len = 2;
3729 }else {
3730 len = 1;
3734 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3735 ret = MSVCRT_WEOF;
3738 return ret;
3741 /*********************************************************************
3742 * _getw (MSVCRT.@)
3744 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3746 char *ch;
3747 int i, k;
3748 unsigned int j;
3749 ch = (char *)&i;
3751 MSVCRT__lock_file(file);
3752 for (j=0; j<sizeof(int); j++) {
3753 k = MSVCRT__fgetc_nolock(file);
3754 if (k == MSVCRT_EOF) {
3755 file->_flag |= MSVCRT__IOEOF;
3756 MSVCRT__unlock_file(file);
3757 return MSVCRT_EOF;
3759 ch[j] = k;
3762 MSVCRT__unlock_file(file);
3763 return i;
3766 /*********************************************************************
3767 * getwc (MSVCRT.@)
3769 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3771 return MSVCRT_fgetwc(file);
3774 /*********************************************************************
3775 * _fgetwchar (MSVCRT.@)
3777 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3779 return MSVCRT_fgetwc(MSVCRT_stdin);
3782 /*********************************************************************
3783 * getwchar (MSVCRT.@)
3785 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3787 return MSVCRT__fgetwchar();
3790 /*********************************************************************
3791 * fgetws (MSVCRT.@)
3793 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3795 MSVCRT_wint_t cc = MSVCRT_WEOF;
3796 MSVCRT_wchar_t * buf_start = s;
3798 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3799 file,file->_file,s,size);
3801 MSVCRT__lock_file(file);
3803 while ((size >1) && (cc = MSVCRT__fgetwc_nolock(file)) != MSVCRT_WEOF && cc != '\n')
3805 *s++ = cc;
3806 size --;
3808 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3810 TRACE(":nothing read\n");
3811 MSVCRT__unlock_file(file);
3812 return NULL;
3814 if ((cc != MSVCRT_WEOF) && (size > 1))
3815 *s++ = cc;
3816 *s = 0;
3817 TRACE(":got %s\n", debugstr_w(buf_start));
3818 MSVCRT__unlock_file(file);
3819 return buf_start;
3822 /*********************************************************************
3823 * _flsbuf (MSVCRT.@)
3825 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3827 /* Flush output buffer */
3828 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3829 msvcrt_alloc_buffer(file);
3832 if(!(file->_flag & MSVCRT__IOWRT)) {
3833 if(!(file->_flag & MSVCRT__IORW)) {
3834 file->_flag |= MSVCRT__IOERR;
3835 return MSVCRT_EOF;
3837 file->_flag |= MSVCRT__IOWRT;
3839 if(file->_flag & MSVCRT__IOREAD) {
3840 if(!(file->_flag & MSVCRT__IOEOF)) {
3841 file->_flag |= MSVCRT__IOERR;
3842 return MSVCRT_EOF;
3844 file->_cnt = 0;
3845 file->_ptr = file->_base;
3846 file->_flag &= ~(MSVCRT__IOREAD | MSVCRT__IOEOF);
3849 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3850 int res = 0;
3852 if(file->_cnt <= 0) {
3853 res = msvcrt_flush_buffer(file);
3854 if(res)
3855 return res;
3856 file->_flag |= MSVCRT__IOWRT;
3857 file->_cnt=file->_bufsiz;
3859 *file->_ptr++ = c;
3860 file->_cnt--;
3861 return c&0xff;
3862 } else {
3863 unsigned char cc=c;
3864 int len;
3865 /* set _cnt to 0 for unbuffered FILEs */
3866 file->_cnt = 0;
3867 len = MSVCRT__write(file->_file, &cc, 1);
3868 if (len == 1)
3869 return c & 0xff;
3870 file->_flag |= MSVCRT__IOERR;
3871 return MSVCRT_EOF;
3875 /*********************************************************************
3876 * fwrite (MSVCRT.@)
3878 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3880 MSVCRT_size_t ret;
3882 MSVCRT__lock_file(file);
3883 ret = MSVCRT__fwrite_nolock(ptr, size, nmemb, file);
3884 MSVCRT__unlock_file(file);
3886 return ret;
3889 /*********************************************************************
3890 * _fwrite_nolock (MSVCRT.@)
3892 MSVCRT_size_t CDECL MSVCRT__fwrite_nolock(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3894 MSVCRT_size_t wrcnt=size * nmemb;
3895 int written = 0;
3896 if (size == 0)
3897 return 0;
3899 while(wrcnt) {
3900 if(file->_cnt < 0) {
3901 WARN("negative file->_cnt value in %p\n", file);
3902 file->_flag |= MSVCRT__IOERR;
3903 break;
3904 } else if(file->_cnt) {
3905 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3906 memcpy(file->_ptr, ptr, pcnt);
3907 file->_cnt -= pcnt;
3908 file->_ptr += pcnt;
3909 written += pcnt;
3910 wrcnt -= pcnt;
3911 ptr = (const char*)ptr + pcnt;
3912 } else if((file->_flag & MSVCRT__IONBF)
3913 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
3914 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
3915 MSVCRT_size_t pcnt;
3916 int bufsiz;
3918 if(file->_flag & MSVCRT__IONBF)
3919 bufsiz = 1;
3920 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3921 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
3922 else
3923 bufsiz = file->_bufsiz;
3925 pcnt = (wrcnt / bufsiz) * bufsiz;
3927 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
3928 break;
3930 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
3931 file->_flag |= MSVCRT__IOERR;
3932 break;
3934 written += pcnt;
3935 wrcnt -= pcnt;
3936 ptr = (const char*)ptr + pcnt;
3937 } else {
3938 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3939 break;
3940 written++;
3941 wrcnt--;
3942 ptr = (const char*)ptr + 1;
3946 return written / size;
3949 /*********************************************************************
3950 * fputwc (MSVCRT.@)
3952 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3954 MSVCRT_wint_t ret;
3956 MSVCRT__lock_file(file);
3957 ret = MSVCRT__fputwc_nolock(wc, file);
3958 MSVCRT__unlock_file(file);
3960 return ret;
3963 /*********************************************************************
3964 * _fputwc_nolock (MSVCRT.@)
3966 MSVCRT_wint_t CDECL MSVCRT__fputwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3968 MSVCRT_wchar_t mwc=wc;
3969 ioinfo *fdinfo;
3970 MSVCRT_wint_t ret;
3972 fdinfo = get_ioinfo_nolock(file->_file);
3974 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3975 char buf[MSVCRT_MB_LEN_MAX];
3976 int char_len;
3978 char_len = MSVCRT_wctomb(buf, mwc);
3979 if(char_len!=-1 && MSVCRT__fwrite_nolock(buf, char_len, 1, file)==1)
3980 ret = wc;
3981 else
3982 ret = MSVCRT_WEOF;
3983 }else if(MSVCRT__fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
3984 ret = wc;
3985 }else {
3986 ret = MSVCRT_WEOF;
3989 return ret;
3992 /*********************************************************************
3993 * _fputwchar (MSVCRT.@)
3995 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3997 return MSVCRT_fputwc(wc, MSVCRT_stdout);
4000 /*********************************************************************
4001 * _wfsopen (MSVCRT.@)
4003 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
4005 MSVCRT_FILE* file;
4006 int open_flags, stream_flags, fd;
4008 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
4010 /* map mode string to open() flags. "man fopen" for possibilities. */
4011 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4012 return NULL;
4014 LOCK_FILES();
4015 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4016 if (fd < 0)
4017 file = NULL;
4018 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
4019 != -1)
4020 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
4021 else if (file)
4023 file->_flag = 0;
4024 file = NULL;
4027 TRACE(":got (%p)\n",file);
4028 if (fd >= 0 && !file)
4029 MSVCRT__close(fd);
4030 UNLOCK_FILES();
4031 return file;
4034 /*********************************************************************
4035 * _fsopen (MSVCRT.@)
4037 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
4039 MSVCRT_FILE *ret;
4040 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4042 if (path && !(pathW = msvcrt_wstrdupa(path))) {
4043 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4044 *MSVCRT__errno() = MSVCRT_EINVAL;
4045 return NULL;
4047 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4049 MSVCRT_free(pathW);
4050 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4051 *MSVCRT__errno() = MSVCRT_EINVAL;
4052 return NULL;
4055 ret = MSVCRT__wfsopen(pathW, modeW, share);
4057 MSVCRT_free(pathW);
4058 MSVCRT_free(modeW);
4059 return ret;
4062 /*********************************************************************
4063 * fopen (MSVCRT.@)
4065 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
4067 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
4070 /*********************************************************************
4071 * fopen_s (MSVCRT.@)
4073 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
4074 const char *filename, const char *mode)
4076 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4077 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4078 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4080 *pFile = MSVCRT_fopen(filename, mode);
4082 if(!*pFile)
4083 return *MSVCRT__errno();
4084 return 0;
4087 /*********************************************************************
4088 * _wfopen (MSVCRT.@)
4090 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
4092 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
4095 /*********************************************************************
4096 * _wfopen_s (MSVCRT.@)
4098 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
4099 const MSVCRT_wchar_t *mode)
4101 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4102 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4103 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4105 *pFile = MSVCRT__wfopen(filename, mode);
4107 if(!*pFile)
4108 return *MSVCRT__errno();
4109 return 0;
4112 /*********************************************************************
4113 * fputc (MSVCRT.@)
4115 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
4117 int ret;
4119 MSVCRT__lock_file(file);
4120 ret = MSVCRT__fputc_nolock(c, file);
4121 MSVCRT__unlock_file(file);
4123 return ret;
4126 /*********************************************************************
4127 * _fputc_nolock (MSVCRT.@)
4129 int CDECL MSVCRT__fputc_nolock(int c, MSVCRT_FILE* file)
4131 int res;
4133 if(file->_cnt>0) {
4134 *file->_ptr++=c;
4135 file->_cnt--;
4136 if (c == '\n')
4138 res = msvcrt_flush_buffer(file);
4139 return res ? res : c;
4141 else {
4142 return c & 0xff;
4144 } else {
4145 res = MSVCRT__flsbuf(c, file);
4146 return res;
4150 /*********************************************************************
4151 * _fputchar (MSVCRT.@)
4153 int CDECL MSVCRT__fputchar(int c)
4155 return MSVCRT_fputc(c, MSVCRT_stdout);
4158 /*********************************************************************
4159 * fread (MSVCRT.@)
4161 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4163 MSVCRT_size_t ret;
4165 MSVCRT__lock_file(file);
4166 ret = MSVCRT__fread_nolock(ptr, size, nmemb, file);
4167 MSVCRT__unlock_file(file);
4169 return ret;
4172 /*********************************************************************
4173 * _fread_nolock (MSVCRT.@)
4175 MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4177 MSVCRT_size_t rcnt=size * nmemb;
4178 MSVCRT_size_t read=0;
4179 MSVCRT_size_t pread=0;
4181 if(!rcnt)
4182 return 0;
4184 /* first buffered data */
4185 if(file->_cnt>0) {
4186 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4187 memcpy(ptr, file->_ptr, pcnt);
4188 file->_cnt -= pcnt;
4189 file->_ptr += pcnt;
4190 read += pcnt ;
4191 rcnt -= pcnt ;
4192 ptr = (char*)ptr + pcnt;
4193 } else if(!(file->_flag & MSVCRT__IOREAD )) {
4194 if(file->_flag & MSVCRT__IORW) {
4195 file->_flag |= MSVCRT__IOREAD;
4196 } else {
4197 return 0;
4201 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4202 msvcrt_alloc_buffer(file);
4204 while(rcnt>0)
4206 int i;
4207 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
4208 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
4209 file->_ptr = file->_base;
4210 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
4211 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4212 if (i > 0 && i < file->_cnt) {
4213 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4214 file->_flag &= ~MSVCRT__IOEOF;
4216 if (i > 0) {
4217 memcpy(ptr, file->_ptr, i);
4218 file->_cnt -= i;
4219 file->_ptr += i;
4221 } else if (rcnt > INT_MAX) {
4222 i = MSVCRT__read(file->_file, ptr, INT_MAX);
4223 } else if (rcnt < MSVCRT_BUFSIZ) {
4224 i = MSVCRT__read(file->_file, ptr, rcnt);
4225 } else {
4226 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
4228 pread += i;
4229 rcnt -= i;
4230 ptr = (char *)ptr+i;
4231 /* expose feof condition in the flags
4232 * MFC tests file->_flag for feof, and doesn't call feof())
4234 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4235 file->_flag |= MSVCRT__IOEOF;
4236 else if (i == -1)
4238 file->_flag |= MSVCRT__IOERR;
4239 pread = 0;
4240 rcnt = 0;
4242 if (i < 1) break;
4244 read+=pread;
4245 return read / size;
4249 /*********************************************************************
4250 * fread_s (MSVCR80.@)
4252 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4253 MSVCRT_size_t count, MSVCRT_FILE *stream)
4255 MSVCRT_size_t ret;
4257 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4258 if(buf && buf_size)
4259 memset(buf, 0, buf_size);
4260 return 0;
4262 if(!elem_size || !count) return 0;
4264 MSVCRT__lock_file(stream);
4265 ret = MSVCRT__fread_nolock_s(buf, buf_size, elem_size, count, stream);
4266 MSVCRT__unlock_file(stream);
4268 return ret;
4271 /*********************************************************************
4272 * _fread_nolock_s (MSVCR80.@)
4274 MSVCRT_size_t CDECL MSVCRT__fread_nolock_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4275 MSVCRT_size_t count, MSVCRT_FILE *stream)
4277 size_t bytes_left, buf_pos;
4279 TRACE("(%p %lu %lu %lu %p)\n", buf, buf_size, elem_size, count, stream);
4281 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4282 if(buf && buf_size)
4283 memset(buf, 0, buf_size);
4284 return 0;
4286 if(!elem_size || !count) return 0;
4287 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4288 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4290 bytes_left = elem_size*count;
4291 buf_pos = 0;
4292 while(bytes_left) {
4293 if(stream->_cnt > 0) {
4294 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4296 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4297 memset(buf, 0, buf_size);
4298 return 0;
4301 MSVCRT__fread_nolock((char*)buf+buf_pos, 1, size, stream);
4302 buf_pos += size;
4303 bytes_left -= size;
4304 }else {
4305 int c = MSVCRT__filbuf(stream);
4307 if(c == MSVCRT_EOF)
4308 break;
4310 if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, MSVCRT_ERANGE)) {
4311 memset(buf, 0, buf_size);
4312 return 0;
4315 ((char*)buf)[buf_pos++] = c;
4316 bytes_left--;
4320 return buf_pos/elem_size;
4323 /*********************************************************************
4324 * _wfreopen (MSVCRT.@)
4327 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4329 int open_flags, stream_flags, fd;
4331 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4333 LOCK_FILES();
4334 if (!file || ((fd = file->_file) < 0))
4335 file = NULL;
4336 else
4338 MSVCRT_fclose(file);
4339 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4340 file = NULL;
4341 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4342 file = NULL;
4343 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4345 file->_flag = 0;
4346 file = NULL;
4349 UNLOCK_FILES();
4350 return file;
4353 /*********************************************************************
4354 * _wfreopen_s (MSVCRT.@)
4356 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4357 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4359 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4360 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4361 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4362 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4364 *pFile = MSVCRT__wfreopen(path, mode, file);
4366 if(!*pFile)
4367 return *MSVCRT__errno();
4368 return 0;
4371 /*********************************************************************
4372 * freopen (MSVCRT.@)
4375 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4377 MSVCRT_FILE *ret;
4378 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4380 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4381 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4383 MSVCRT_free(pathW);
4384 return NULL;
4387 ret = MSVCRT__wfreopen(pathW, modeW, file);
4389 MSVCRT_free(pathW);
4390 MSVCRT_free(modeW);
4391 return ret;
4394 /*********************************************************************
4395 * freopen_s (MSVCRT.@)
4397 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4398 const char *path, const char *mode, MSVCRT_FILE* file)
4400 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4401 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4402 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4403 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4405 *pFile = MSVCRT_freopen(path, mode, file);
4407 if(!*pFile)
4408 return *MSVCRT__errno();
4409 return 0;
4412 /*********************************************************************
4413 * fsetpos (MSVCRT.@)
4415 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4417 int ret;
4419 MSVCRT__lock_file(file);
4420 msvcrt_flush_buffer(file);
4422 /* Reset direction of i/o */
4423 if(file->_flag & MSVCRT__IORW) {
4424 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4427 ret = (MSVCRT__lseeki64(file->_file,*pos,MSVCRT_SEEK_SET) == -1) ? -1 : 0;
4428 MSVCRT__unlock_file(file);
4429 return ret;
4432 /*********************************************************************
4433 * _ftelli64 (MSVCRT.@)
4435 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4437 __int64 ret;
4439 MSVCRT__lock_file(file);
4440 ret = MSVCRT__ftelli64_nolock(file);
4441 MSVCRT__unlock_file(file);
4443 return ret;
4446 /*********************************************************************
4447 * _ftelli64_nolock (MSVCRT.@)
4449 __int64 CDECL MSVCRT__ftelli64_nolock(MSVCRT_FILE* file)
4451 __int64 pos;
4453 pos = _telli64(file->_file);
4454 if(pos == -1)
4455 return -1;
4456 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4457 if(file->_flag & MSVCRT__IOWRT) {
4458 pos += file->_ptr - file->_base;
4460 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4461 char *p;
4463 for(p=file->_base; p<file->_ptr; p++)
4464 if(*p == '\n')
4465 pos++;
4467 } else if(!file->_cnt) { /* nothing to do */
4468 } else if(MSVCRT__lseeki64(file->_file, 0, MSVCRT_SEEK_END)==pos) {
4469 int i;
4471 pos -= file->_cnt;
4472 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4473 for(i=0; i<file->_cnt; i++)
4474 if(file->_ptr[i] == '\n')
4475 pos--;
4477 } else {
4478 char *p;
4480 if(MSVCRT__lseeki64(file->_file, pos, MSVCRT_SEEK_SET) != pos)
4481 return -1;
4483 pos -= file->_bufsiz;
4484 pos += file->_ptr - file->_base;
4486 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4487 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4488 pos--;
4490 for(p=file->_base; p<file->_ptr; p++)
4491 if(*p == '\n')
4492 pos++;
4497 return pos;
4500 /*********************************************************************
4501 * ftell (MSVCRT.@)
4503 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4505 return MSVCRT__ftelli64(file);
4508 /*********************************************************************
4509 * _ftell_nolock (MSVCRT.@)
4511 LONG CDECL MSVCRT__ftell_nolock(MSVCRT_FILE* file)
4513 return MSVCRT__ftelli64_nolock(file);
4516 /*********************************************************************
4517 * fgetpos (MSVCRT.@)
4519 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4521 *pos = MSVCRT__ftelli64(file);
4522 if(*pos == -1)
4523 return -1;
4524 return 0;
4527 /*********************************************************************
4528 * fputs (MSVCRT.@)
4530 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4532 MSVCRT_size_t len = strlen(s);
4533 int ret;
4535 MSVCRT__lock_file(file);
4536 ret = MSVCRT__fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4537 MSVCRT__unlock_file(file);
4538 return ret;
4541 /*********************************************************************
4542 * fputws (MSVCRT.@)
4544 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4546 MSVCRT_size_t i, len = strlenW(s);
4547 BOOL tmp_buf;
4548 int ret;
4550 MSVCRT__lock_file(file);
4551 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4552 ret = MSVCRT__fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4553 MSVCRT__unlock_file(file);
4554 return ret;
4557 tmp_buf = add_std_buffer(file);
4558 for (i=0; i<len; i++) {
4559 if(MSVCRT__fputwc_nolock(s[i], file) == MSVCRT_WEOF) {
4560 if(tmp_buf) remove_std_buffer(file);
4561 MSVCRT__unlock_file(file);
4562 return MSVCRT_WEOF;
4566 if(tmp_buf) remove_std_buffer(file);
4567 MSVCRT__unlock_file(file);
4568 return 0;
4571 /*********************************************************************
4572 * getchar (MSVCRT.@)
4574 int CDECL MSVCRT_getchar(void)
4576 return MSVCRT_fgetc(MSVCRT_stdin);
4579 /*********************************************************************
4580 * getc (MSVCRT.@)
4582 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4584 return MSVCRT_fgetc(file);
4587 /*********************************************************************
4588 * gets (MSVCRT.@)
4590 char * CDECL MSVCRT_gets(char *buf)
4592 int cc;
4593 char * buf_start = buf;
4595 MSVCRT__lock_file(MSVCRT_stdin);
4596 for(cc = MSVCRT__fgetc_nolock(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4597 cc = MSVCRT__fgetc_nolock(MSVCRT_stdin))
4598 if(cc != '\r') *buf++ = (char)cc;
4600 *buf = '\0';
4602 TRACE("got '%s'\n", buf_start);
4603 MSVCRT__unlock_file(MSVCRT_stdin);
4604 return buf_start;
4607 /*********************************************************************
4608 * _getws (MSVCRT.@)
4610 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4612 MSVCRT_wint_t cc;
4613 MSVCRT_wchar_t* ws = buf;
4615 MSVCRT__lock_file(MSVCRT_stdin);
4616 for (cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4617 cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin))
4619 if (cc != '\r')
4620 *buf++ = (MSVCRT_wchar_t)cc;
4622 *buf = '\0';
4624 TRACE("got %s\n", debugstr_w(ws));
4625 MSVCRT__unlock_file(MSVCRT_stdin);
4626 return ws;
4629 /*********************************************************************
4630 * putc (MSVCRT.@)
4632 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4634 return MSVCRT_fputc(c, file);
4637 /*********************************************************************
4638 * putchar (MSVCRT.@)
4640 int CDECL MSVCRT_putchar(int c)
4642 return MSVCRT_fputc(c, MSVCRT_stdout);
4645 /*********************************************************************
4646 * puts (MSVCRT.@)
4648 int CDECL MSVCRT_puts(const char *s)
4650 MSVCRT_size_t len = strlen(s);
4651 int ret;
4653 MSVCRT__lock_file(MSVCRT_stdout);
4654 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4655 MSVCRT__unlock_file(MSVCRT_stdout);
4656 return MSVCRT_EOF;
4659 ret = MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4660 MSVCRT__unlock_file(MSVCRT_stdout);
4661 return ret;
4664 /*********************************************************************
4665 * _putws (MSVCRT.@)
4667 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4669 static const MSVCRT_wchar_t nl = '\n';
4670 MSVCRT_size_t len = strlenW(s);
4671 int ret;
4673 MSVCRT__lock_file(MSVCRT_stdout);
4674 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4675 MSVCRT__unlock_file(MSVCRT_stdout);
4676 return MSVCRT_EOF;
4679 ret = MSVCRT__fwrite_nolock(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4680 MSVCRT__unlock_file(MSVCRT_stdout);
4681 return ret;
4684 /*********************************************************************
4685 * remove (MSVCRT.@)
4687 int CDECL MSVCRT_remove(const char *path)
4689 TRACE("(%s)\n",path);
4690 if (DeleteFileA(path))
4691 return 0;
4692 TRACE(":failed (%d)\n",GetLastError());
4693 msvcrt_set_errno(GetLastError());
4694 return -1;
4697 /*********************************************************************
4698 * _wremove (MSVCRT.@)
4700 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4702 TRACE("(%s)\n",debugstr_w(path));
4703 if (DeleteFileW(path))
4704 return 0;
4705 TRACE(":failed (%d)\n",GetLastError());
4706 msvcrt_set_errno(GetLastError());
4707 return -1;
4710 /*********************************************************************
4711 * rename (MSVCRT.@)
4713 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4715 TRACE(":from %s to %s\n",oldpath,newpath);
4716 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4717 return 0;
4718 TRACE(":failed (%d)\n",GetLastError());
4719 msvcrt_set_errno(GetLastError());
4720 return -1;
4723 /*********************************************************************
4724 * _wrename (MSVCRT.@)
4726 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4728 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4729 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4730 return 0;
4731 TRACE(":failed (%d)\n",GetLastError());
4732 msvcrt_set_errno(GetLastError());
4733 return -1;
4736 /*********************************************************************
4737 * setvbuf (MSVCRT.@)
4739 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4741 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4742 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4743 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4745 MSVCRT__lock_file(file);
4747 MSVCRT__fflush_nolock(file);
4748 if(file->_flag & MSVCRT__IOMYBUF)
4749 MSVCRT_free(file->_base);
4750 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4751 file->_cnt = 0;
4753 if(mode == MSVCRT__IONBF) {
4754 file->_flag |= MSVCRT__IONBF;
4755 file->_base = file->_ptr = (char*)&file->_charbuf;
4756 file->_bufsiz = 2;
4757 }else if(buf) {
4758 file->_base = file->_ptr = buf;
4759 file->_flag |= MSVCRT__USERBUF;
4760 file->_bufsiz = size;
4761 }else {
4762 file->_base = file->_ptr = MSVCRT_malloc(size);
4763 if(!file->_base) {
4764 file->_bufsiz = 0;
4765 MSVCRT__unlock_file(file);
4766 return -1;
4769 file->_flag |= MSVCRT__IOMYBUF;
4770 file->_bufsiz = size;
4772 MSVCRT__unlock_file(file);
4773 return 0;
4776 /*********************************************************************
4777 * setbuf (MSVCRT.@)
4779 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4781 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4784 static int tmpnam_helper(char *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
4786 char tmpstr[8];
4787 char *p = s;
4788 int digits;
4790 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
4792 if (size < 3) {
4793 if (size) *s = 0;
4794 *MSVCRT__errno() = MSVCRT_ERANGE;
4795 return MSVCRT_ERANGE;
4797 *p++ = '\\';
4798 *p++ = 's';
4799 size -= 2;
4800 digits = msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4801 if (digits+1 > size) {
4802 *s = 0;
4803 *MSVCRT__errno() = MSVCRT_ERANGE;
4804 return MSVCRT_ERANGE;
4806 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4807 p += digits;
4808 *p++ = '.';
4809 size -= digits+1;
4811 while(1) {
4812 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4813 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4814 break;
4817 digits = msvcrt_int_to_base32(digits, tmpstr);
4818 if (digits+1 > size) {
4819 *s = 0;
4820 *MSVCRT__errno() = MSVCRT_ERANGE;
4821 return MSVCRT_ERANGE;
4823 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4824 p[digits] = 0;
4826 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4827 GetLastError() == ERROR_FILE_NOT_FOUND)
4828 break;
4830 return 0;
4833 int CDECL MSVCRT_tmpnam_s(char *s, MSVCRT_size_t size)
4835 return tmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
4838 /*********************************************************************
4839 * tmpnam (MSVCRT.@)
4841 char * CDECL MSVCRT_tmpnam(char *s)
4843 if (!s) {
4844 thread_data_t *data = msvcrt_get_thread_data();
4846 if(!data->tmpnam_buffer)
4847 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4849 s = data->tmpnam_buffer;
4852 return tmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
4855 static int wtmpnam_helper(MSVCRT_wchar_t *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
4857 MSVCRT_wchar_t tmpstr[8];
4858 MSVCRT_wchar_t *p = s;
4859 int digits;
4861 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
4863 if (size < 3) {
4864 if (size) *s = 0;
4865 *MSVCRT__errno() = MSVCRT_ERANGE;
4866 return MSVCRT_ERANGE;
4868 *p++ = '\\';
4869 *p++ = 's';
4870 size -= 2;
4871 digits = msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4872 if (digits+1 > size) {
4873 *s = 0;
4874 *MSVCRT__errno() = MSVCRT_ERANGE;
4875 return MSVCRT_ERANGE;
4877 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4878 p += digits;
4879 *p++ = '.';
4880 size -= digits+1;
4882 while(1) {
4883 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4884 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4885 break;
4888 digits = msvcrt_int_to_base32_w(digits, tmpstr);
4889 if (digits+1 > size) {
4890 *s = 0;
4891 *MSVCRT__errno() = MSVCRT_ERANGE;
4892 return MSVCRT_ERANGE;
4894 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4895 p[digits] = 0;
4897 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4898 GetLastError() == ERROR_FILE_NOT_FOUND)
4899 break;
4901 return 0;
4904 /*********************************************************************
4905 * _wtmpnam_s (MSVCRT.@)
4907 int CDECL MSVCRT__wtmpnam_s(MSVCRT_wchar_t *s, MSVCRT_size_t size)
4909 return wtmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
4912 /*********************************************************************
4913 * _wtmpnam (MSVCRT.@)
4915 MSVCRT_wchar_t * CDECL MSVCRT__wtmpnam(MSVCRT_wchar_t *s)
4917 if (!s) {
4918 thread_data_t *data = msvcrt_get_thread_data();
4920 if(!data->wtmpnam_buffer)
4921 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4923 s = data->wtmpnam_buffer;
4926 return wtmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
4929 /*********************************************************************
4930 * tmpfile (MSVCRT.@)
4932 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4934 char *filename = MSVCRT_tmpnam(NULL);
4935 int fd;
4936 MSVCRT_FILE* file = NULL;
4938 LOCK_FILES();
4939 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4940 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4941 if (fd != -1 && (file = msvcrt_alloc_fp()))
4943 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4945 file->_flag = 0;
4946 file = NULL;
4948 else file->_tmpfname = MSVCRT__strdup(filename);
4951 if(fd != -1 && !file)
4952 MSVCRT__close(fd);
4953 UNLOCK_FILES();
4954 return file;
4957 /*********************************************************************
4958 * tmpfile_s (MSVCRT.@)
4960 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4962 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4964 *file = MSVCRT_tmpfile();
4965 return 0;
4968 static int puts_clbk_file_a(void *file, int len, const char *str)
4970 return MSVCRT_fwrite(str, sizeof(char), len, file);
4973 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4975 int i, ret;
4977 MSVCRT__lock_file(file);
4979 if(!(get_ioinfo_nolock(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4980 ret = MSVCRT__fwrite_nolock(str, sizeof(MSVCRT_wchar_t), len, file);
4981 MSVCRT__unlock_file(file);
4982 return ret;
4985 for(i=0; i<len; i++) {
4986 if(MSVCRT__fputwc_nolock(str[i], file) == MSVCRT_WEOF) {
4987 MSVCRT__unlock_file(file);
4988 return -1;
4992 MSVCRT__unlock_file(file);
4993 return len;
4996 /*********************************************************************
4997 * vfprintf (MSVCRT.@)
4999 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5001 BOOL tmp_buf;
5002 int ret;
5004 MSVCRT__lock_file(file);
5005 tmp_buf = add_std_buffer(file);
5006 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, 0, arg_clbk_valist, NULL, &valist);
5007 if(tmp_buf) remove_std_buffer(file);
5008 MSVCRT__unlock_file(file);
5010 return ret;
5013 /*********************************************************************
5014 * vfprintf_s (MSVCRT.@)
5016 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5018 BOOL tmp_buf;
5019 int ret;
5021 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
5023 MSVCRT__lock_file(file);
5024 tmp_buf = add_std_buffer(file);
5025 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL,
5026 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, arg_clbk_valist, NULL, &valist);
5027 if(tmp_buf) remove_std_buffer(file);
5028 MSVCRT__unlock_file(file);
5030 return ret;
5033 /*********************************************************************
5034 * vfwprintf (MSVCRT.@)
5036 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5038 BOOL tmp_buf;
5039 int ret;
5041 MSVCRT__lock_file(file);
5042 tmp_buf = add_std_buffer(file);
5043 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, 0, arg_clbk_valist, NULL, &valist);
5044 if(tmp_buf) remove_std_buffer(file);
5045 MSVCRT__unlock_file(file);
5047 return ret;
5050 /*********************************************************************
5051 * vfwprintf_s (MSVCRT.@)
5053 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5055 BOOL tmp_buf;
5056 int ret;
5058 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5060 MSVCRT__lock_file(file);
5061 tmp_buf = add_std_buffer(file);
5062 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL,
5063 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, arg_clbk_valist, NULL, &valist);
5064 if(tmp_buf) remove_std_buffer(file);
5065 MSVCRT__unlock_file(file);
5067 return ret;
5070 /*********************************************************************
5071 * __stdio_common_vfprintf (MSVCRT.@)
5073 int CDECL MSVCRT__stdio_common_vfprintf(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
5074 MSVCRT__locale_t locale, __ms_va_list valist)
5076 BOOL tmp_buf;
5077 int ret;
5079 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5081 MSVCRT__lock_file(file);
5082 tmp_buf = add_std_buffer(file);
5084 if (options & ~UCRTBASE_PRINTF_MASK)
5085 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5086 ret = pf_printf_a(puts_clbk_file_a, file, format, locale,
5087 options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
5088 if(tmp_buf) remove_std_buffer(file);
5089 MSVCRT__unlock_file(file);
5091 return ret;
5094 /*********************************************************************
5095 * _vfwprintf_l (MSVCRT.@)
5097 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5098 MSVCRT__locale_t locale, __ms_va_list valist)
5100 BOOL tmp_buf;
5101 int ret;
5103 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5105 MSVCRT__lock_file(file);
5106 tmp_buf = add_std_buffer(file);
5107 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, 0, arg_clbk_valist, NULL, &valist);
5108 if(tmp_buf) remove_std_buffer(file);
5109 MSVCRT__unlock_file(file);
5111 return ret;
5114 /*********************************************************************
5115 * vprintf (MSVCRT.@)
5117 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
5119 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
5122 /*********************************************************************
5123 * vprintf_s (MSVCRT.@)
5125 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
5127 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
5130 /*********************************************************************
5131 * vwprintf (MSVCRT.@)
5133 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
5135 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
5138 /*********************************************************************
5139 * vwprintf_s (MSVCRT.@)
5141 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
5143 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
5146 /*********************************************************************
5147 * fprintf (MSVCRT.@)
5149 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
5151 __ms_va_list valist;
5152 int res;
5153 __ms_va_start(valist, format);
5154 res = MSVCRT_vfprintf(file, format, valist);
5155 __ms_va_end(valist);
5156 return res;
5159 /*********************************************************************
5160 * fprintf_s (MSVCRT.@)
5162 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
5164 __ms_va_list valist;
5165 int res;
5166 __ms_va_start(valist, format);
5167 res = MSVCRT_vfprintf_s(file, format, valist);
5168 __ms_va_end(valist);
5169 return res;
5172 /*********************************************************************
5173 * fwprintf (MSVCRT.@)
5175 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5177 __ms_va_list valist;
5178 int res;
5179 __ms_va_start(valist, format);
5180 res = MSVCRT_vfwprintf(file, format, valist);
5181 __ms_va_end(valist);
5182 return res;
5185 /*********************************************************************
5186 * fwprintf_s (MSVCRT.@)
5188 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5190 __ms_va_list valist;
5191 int res;
5192 __ms_va_start(valist, format);
5193 res = MSVCRT_vfwprintf_s(file, format, valist);
5194 __ms_va_end(valist);
5195 return res;
5198 /*********************************************************************
5199 * _fwprintf_l (MSVCRT.@)
5201 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
5203 __ms_va_list valist;
5204 int res;
5205 __ms_va_start(valist, locale);
5206 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
5207 __ms_va_end(valist);
5208 return res;
5211 /*********************************************************************
5212 * printf (MSVCRT.@)
5214 int CDECL MSVCRT_printf(const char *format, ...)
5216 __ms_va_list valist;
5217 int res;
5218 __ms_va_start(valist, format);
5219 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
5220 __ms_va_end(valist);
5221 return res;
5224 /*********************************************************************
5225 * printf_s (MSVCRT.@)
5227 int CDECL MSVCRT_printf_s(const char *format, ...)
5229 __ms_va_list valist;
5230 int res;
5231 __ms_va_start(valist, format);
5232 res = MSVCRT_vprintf_s(format, valist);
5233 __ms_va_end(valist);
5234 return res;
5237 /*********************************************************************
5238 * ungetc (MSVCRT.@)
5240 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
5242 int ret;
5244 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5246 MSVCRT__lock_file(file);
5247 ret = MSVCRT__ungetc_nolock(c, file);
5248 MSVCRT__unlock_file(file);
5250 return ret;
5253 /*********************************************************************
5254 * _ungetc_nolock (MSVCRT.@)
5256 int CDECL MSVCRT__ungetc_nolock(int c, MSVCRT_FILE * file)
5258 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5260 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
5261 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
5262 return MSVCRT_EOF;
5264 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
5265 && msvcrt_alloc_buffer(file))
5266 || (!file->_cnt && file->_ptr==file->_base))
5267 file->_ptr++;
5269 if(file->_ptr>file->_base) {
5270 file->_ptr--;
5271 if(file->_flag & MSVCRT__IOSTRG) {
5272 if(*file->_ptr != c) {
5273 file->_ptr++;
5274 return MSVCRT_EOF;
5276 }else {
5277 *file->_ptr = c;
5279 file->_cnt++;
5280 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
5281 file->_flag |= MSVCRT__IOREAD;
5282 return c;
5285 return MSVCRT_EOF;
5288 /*********************************************************************
5289 * ungetwc (MSVCRT.@)
5291 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5293 MSVCRT_wint_t ret;
5295 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5297 MSVCRT__lock_file(file);
5298 ret = MSVCRT__ungetwc_nolock(wc, file);
5299 MSVCRT__unlock_file(file);
5301 return ret;
5304 /*********************************************************************
5305 * _ungetwc_nolock (MSVCRT.@)
5307 MSVCRT_wint_t CDECL MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5309 MSVCRT_wchar_t mwc = wc;
5311 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5312 if (wc == MSVCRT_WEOF)
5313 return MSVCRT_WEOF;
5315 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5316 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5317 unsigned char * pp = (unsigned char *)&mwc;
5318 int i;
5320 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
5321 if(pp[i] != MSVCRT__ungetc_nolock(pp[i],file))
5322 return MSVCRT_WEOF;
5324 }else {
5325 char mbs[MSVCRT_MB_LEN_MAX];
5326 int len;
5328 len = MSVCRT_wctomb(mbs, mwc);
5329 if(len == -1)
5330 return MSVCRT_WEOF;
5332 for(len--; len>=0; len--) {
5333 if(mbs[len] != MSVCRT__ungetc_nolock(mbs[len], file))
5334 return MSVCRT_WEOF;
5338 return mwc;
5341 /*********************************************************************
5342 * wprintf (MSVCRT.@)
5344 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
5346 __ms_va_list valist;
5347 int res;
5348 __ms_va_start(valist, format);
5349 res = MSVCRT_vwprintf(format, valist);
5350 __ms_va_end(valist);
5351 return res;
5354 /*********************************************************************
5355 * wprintf_s (MSVCRT.@)
5357 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
5359 __ms_va_list valist;
5360 int res;
5361 __ms_va_start(valist, format);
5362 res = MSVCRT_vwprintf_s(format, valist);
5363 __ms_va_end(valist);
5364 return res;
5367 /*********************************************************************
5368 * _getmaxstdio (MSVCRT.@)
5370 int CDECL MSVCRT__getmaxstdio(void)
5372 return MSVCRT_max_streams;
5375 /*********************************************************************
5376 * _setmaxstdio (MSVCRT.@)
5378 int CDECL MSVCRT__setmaxstdio(int newmax)
5380 TRACE("%d\n", newmax);
5382 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5383 return -1;
5385 MSVCRT_max_streams = newmax;
5386 return MSVCRT_max_streams;
5389 /*********************************************************************
5390 * _get_stream_buffer_pointers (UCRTBASE.@)
5392 int CDECL MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE *file, char*** base,
5393 char*** ptr, int** count)
5395 if (base)
5396 *base = &file->_base;
5397 if (ptr)
5398 *ptr = &file->_ptr;
5399 if (count)
5400 *count = &file->_cnt;
5401 return 0;