kernel32: Add GetConsoleFontInfo stub.
[wine.git] / dlls / msvcrt / file.c
blob71e578d8bee4ab59e1f1dda4801270a87dd56a0b
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 if (fd == MSVCRT_STDOUT_FILENO &&
1025 info->handle == get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle) {
1026 msvcrt_free_fd(fd);
1027 ret = 0;
1028 } else if (fd == MSVCRT_STDERR_FILENO &&
1029 info->handle == get_ioinfo_nolock(MSVCRT_STDOUT_FILENO)->handle) {
1030 msvcrt_free_fd(fd);
1031 ret = 0;
1032 } else {
1033 ret = CloseHandle(info->handle) ? 0 : -1;
1034 msvcrt_free_fd(fd);
1035 if (ret) {
1036 WARN(":failed-last error (%d)\n",GetLastError());
1037 msvcrt_set_errno(GetLastError());
1040 release_ioinfo(info);
1041 return ret;
1044 /*********************************************************************
1045 * _dup2 (MSVCRT.@)
1046 * NOTES
1047 * MSDN isn't clear on this point, but the remarks for _pipe
1048 * indicate file descriptors duplicated with _dup and _dup2 are always
1049 * inheritable.
1051 int CDECL MSVCRT__dup2(int od, int nd)
1053 ioinfo *info_od, *info_nd;
1054 int ret;
1056 TRACE("(od=%d, nd=%d)\n", od, nd);
1058 if (od < nd)
1060 info_od = get_ioinfo(od);
1061 info_nd = get_ioinfo_alloc_fd(nd);
1063 else
1065 info_nd = get_ioinfo_alloc_fd(nd);
1066 info_od = get_ioinfo(od);
1069 if (info_nd == &MSVCRT___badioinfo)
1071 ret = -1;
1073 else if (info_od->wxflag & WX_OPEN)
1075 HANDLE handle;
1077 if (DuplicateHandle(GetCurrentProcess(), info_od->handle,
1078 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1080 int wxflag = info_od->wxflag & ~MSVCRT__O_NOINHERIT;
1082 if (info_nd->wxflag & WX_OPEN)
1083 MSVCRT__close(nd);
1085 msvcrt_set_fd(info_nd, handle, wxflag);
1086 /* _dup2 returns 0, not nd, on success */
1087 ret = 0;
1089 else
1091 ret = -1;
1092 msvcrt_set_errno(GetLastError());
1095 else
1097 *MSVCRT__errno() = MSVCRT_EBADF;
1098 ret = -1;
1101 release_ioinfo(info_od);
1102 release_ioinfo(info_nd);
1103 return ret;
1106 /*********************************************************************
1107 * _dup (MSVCRT.@)
1109 int CDECL MSVCRT__dup(int od)
1111 int fd, ret;
1112 ioinfo *info = get_ioinfo_alloc(&fd);
1114 if (MSVCRT__dup2(od, fd) == 0)
1115 ret = fd;
1116 else
1117 ret = -1;
1118 release_ioinfo(info);
1119 return ret;
1122 /*********************************************************************
1123 * _eof (MSVCRT.@)
1125 int CDECL MSVCRT__eof(int fd)
1127 ioinfo *info = get_ioinfo(fd);
1128 DWORD curpos,endpos;
1129 LONG hcurpos,hendpos;
1131 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1133 if (info->handle == INVALID_HANDLE_VALUE)
1135 release_ioinfo(info);
1136 return -1;
1139 if (info->wxflag & WX_ATEOF)
1141 release_ioinfo(info);
1142 return TRUE;
1145 /* Otherwise we do it the hard way */
1146 hcurpos = hendpos = 0;
1147 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1148 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1150 if (curpos == endpos && hcurpos == hendpos)
1152 /* FIXME: shouldn't WX_ATEOF be set here? */
1153 release_ioinfo(info);
1154 return TRUE;
1157 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1158 release_ioinfo(info);
1159 return FALSE;
1162 /*********************************************************************
1163 * _fcloseall (MSVCRT.@)
1165 int CDECL MSVCRT__fcloseall(void)
1167 int num_closed = 0, i;
1168 MSVCRT_FILE *file;
1170 LOCK_FILES();
1171 for (i = 3; i < MSVCRT_stream_idx; i++) {
1172 file = msvcrt_get_file(i);
1174 if (file->_flag && !MSVCRT_fclose(file))
1175 num_closed++;
1177 UNLOCK_FILES();
1179 TRACE(":closed (%d) handles\n",num_closed);
1180 return num_closed;
1183 /* free everything on process exit */
1184 void msvcrt_free_io(void)
1186 unsigned int i;
1187 int j;
1189 MSVCRT__flushall();
1190 MSVCRT__fcloseall();
1192 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1194 if(!MSVCRT___pioinfo[i])
1195 continue;
1197 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1199 if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
1200 DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
1202 MSVCRT_free(MSVCRT___pioinfo[i]);
1205 for(j=0; j<MSVCRT_stream_idx; j++)
1207 MSVCRT_FILE *file = msvcrt_get_file(j);
1208 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1210 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1211 DeleteCriticalSection(&((file_crit*)file)->crit);
1215 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1216 MSVCRT_free(MSVCRT_fstream[i]);
1219 /*********************************************************************
1220 * _lseeki64 (MSVCRT.@)
1222 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1224 ioinfo *info = get_ioinfo(fd);
1225 LARGE_INTEGER ofs;
1227 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1229 if (info->handle == INVALID_HANDLE_VALUE)
1231 release_ioinfo(info);
1232 return -1;
1235 if (whence < 0 || whence > 2)
1237 release_ioinfo(info);
1238 *MSVCRT__errno() = MSVCRT_EINVAL;
1239 return -1;
1242 TRACE(":fd (%d) to %s pos %s\n",
1243 fd,wine_dbgstr_longlong(offset),
1244 (whence==MSVCRT_SEEK_SET)?"SEEK_SET":
1245 (whence==MSVCRT_SEEK_CUR)?"SEEK_CUR":
1246 (whence==MSVCRT_SEEK_END)?"SEEK_END":"UNKNOWN");
1248 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1249 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1250 ofs.QuadPart = offset;
1251 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1252 GetLastError() == ERROR_SUCCESS)
1254 info->wxflag &= ~WX_ATEOF;
1255 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1257 release_ioinfo(info);
1258 return ofs.QuadPart;
1260 release_ioinfo(info);
1261 TRACE(":error-last error (%d)\n",GetLastError());
1262 msvcrt_set_errno(GetLastError());
1263 return -1;
1266 /*********************************************************************
1267 * _lseek (MSVCRT.@)
1269 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1271 return MSVCRT__lseeki64(fd, offset, whence);
1274 /*********************************************************************
1275 * _lock_file (MSVCRT.@)
1277 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1279 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1280 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1281 else
1282 EnterCriticalSection(&((file_crit*)file)->crit);
1285 /*********************************************************************
1286 * _unlock_file (MSVCRT.@)
1288 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1290 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1291 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1292 else
1293 LeaveCriticalSection(&((file_crit*)file)->crit);
1296 /*********************************************************************
1297 * _locking (MSVCRT.@)
1299 * This is untested; the underlying LockFile doesn't work yet.
1301 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1303 ioinfo *info = get_ioinfo(fd);
1304 BOOL ret;
1305 DWORD cur_locn;
1307 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1308 if (info->handle == INVALID_HANDLE_VALUE)
1310 release_ioinfo(info);
1311 return -1;
1314 if (mode < 0 || mode > 4)
1316 release_ioinfo(info);
1317 *MSVCRT__errno() = MSVCRT_EINVAL;
1318 return -1;
1321 TRACE(":fd (%d) by 0x%08x mode %s\n",
1322 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1323 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1324 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1325 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1326 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1327 "UNKNOWN");
1329 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1331 release_ioinfo(info);
1332 FIXME ("Seek failed\n");
1333 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1334 return -1;
1336 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1338 int nretry = 10;
1339 ret = 1; /* just to satisfy gcc */
1340 while (nretry--)
1342 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1343 if (ret) break;
1344 Sleep(1);
1347 else if (mode == MSVCRT__LK_UNLCK)
1348 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1349 else
1350 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1351 /* FIXME - what about error settings? */
1352 release_ioinfo(info);
1353 return ret ? 0 : -1;
1356 /*********************************************************************
1357 * _fseeki64 (MSVCRT.@)
1359 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1361 int ret;
1363 MSVCRT__lock_file(file);
1364 ret = MSVCRT__fseeki64_nolock(file, offset, whence);
1365 MSVCRT__unlock_file(file);
1367 return ret;
1370 /*********************************************************************
1371 * _fseeki64_nolock (MSVCRT.@)
1373 int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
1375 int ret;
1377 if(whence == MSVCRT_SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1378 whence = MSVCRT_SEEK_SET;
1379 offset += MSVCRT__ftelli64_nolock(file);
1382 /* Flush output if needed */
1383 msvcrt_flush_buffer(file);
1384 /* Reset direction of i/o */
1385 if(file->_flag & MSVCRT__IORW) {
1386 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1388 /* Clear end of file flag */
1389 file->_flag &= ~MSVCRT__IOEOF;
1390 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1392 return ret;
1395 /*********************************************************************
1396 * fseek (MSVCRT.@)
1398 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1400 return MSVCRT__fseeki64( file, offset, whence );
1403 /*********************************************************************
1404 * _fseek_nolock (MSVCRT.@)
1406 int CDECL MSVCRT__fseek_nolock(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1408 return MSVCRT__fseeki64_nolock( file, offset, whence );
1411 /*********************************************************************
1412 * _chsize_s (MSVCRT.@)
1414 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1416 ioinfo *info;
1417 __int64 cur, pos;
1418 BOOL ret = FALSE;
1420 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1422 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1425 info = get_ioinfo(fd);
1426 if (info->handle != INVALID_HANDLE_VALUE)
1428 /* save the current file pointer */
1429 cur = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1430 if (cur >= 0)
1432 pos = MSVCRT__lseeki64(fd, size, MSVCRT_SEEK_SET);
1433 if (pos >= 0)
1435 ret = SetEndOfFile(info->handle);
1436 if (!ret) msvcrt_set_errno(GetLastError());
1439 /* restore the file pointer */
1440 MSVCRT__lseeki64(fd, cur, MSVCRT_SEEK_SET);
1444 release_ioinfo(info);
1445 return ret ? 0 : *MSVCRT__errno();
1448 /*********************************************************************
1449 * _chsize (MSVCRT.@)
1451 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1453 /* _chsize_s returns errno on failure but _chsize should return -1 */
1454 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1457 /*********************************************************************
1458 * clearerr (MSVCRT.@)
1460 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1462 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1464 MSVCRT__lock_file(file);
1465 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1466 MSVCRT__unlock_file(file);
1469 /*********************************************************************
1470 * rewind (MSVCRT.@)
1472 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1474 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1476 MSVCRT__lock_file(file);
1477 MSVCRT__fseek_nolock(file, 0L, MSVCRT_SEEK_SET);
1478 MSVCRT_clearerr(file);
1479 MSVCRT__unlock_file(file);
1482 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1484 int plus = strchrW(mode, '+') != NULL;
1486 TRACE("%s\n", debugstr_w(mode));
1488 while(*mode == ' ') mode++;
1490 switch(*mode++)
1492 case 'R': case 'r':
1493 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1494 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1495 break;
1496 case 'W': case 'w':
1497 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1498 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1499 break;
1500 case 'A': case 'a':
1501 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1502 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1503 break;
1504 default:
1505 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1506 return -1;
1509 *stream_flags |= MSVCRT__commode;
1511 while (*mode && *mode!=',')
1512 switch (*mode++)
1514 case 'B': case 'b':
1515 *open_flags |= MSVCRT__O_BINARY;
1516 *open_flags &= ~MSVCRT__O_TEXT;
1517 break;
1518 case 't':
1519 *open_flags |= MSVCRT__O_TEXT;
1520 *open_flags &= ~MSVCRT__O_BINARY;
1521 break;
1522 case 'D':
1523 *open_flags |= MSVCRT__O_TEMPORARY;
1524 break;
1525 case 'T':
1526 *open_flags |= MSVCRT__O_SHORT_LIVED;
1527 break;
1528 case 'c':
1529 *stream_flags |= MSVCRT__IOCOMMIT;
1530 break;
1531 case 'n':
1532 *stream_flags &= ~MSVCRT__IOCOMMIT;
1533 break;
1534 case 'N':
1535 *open_flags |= MSVCRT__O_NOINHERIT;
1536 break;
1537 case '+':
1538 case ' ':
1539 case 'a':
1540 case 'w':
1541 break;
1542 case 'S':
1543 case 'R':
1544 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1545 break;
1546 default:
1547 ERR("incorrect mode flag: %c\n", mode[-1]);
1548 break;
1551 if(*mode == ',')
1553 static const WCHAR ccs[] = {'c','c','s'};
1554 static const WCHAR utf8[] = {'u','t','f','-','8'};
1555 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1556 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1558 mode++;
1559 while(*mode == ' ') mode++;
1560 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1561 return -1;
1562 mode += sizeof(ccs)/sizeof(ccs[0]);
1563 while(*mode == ' ') mode++;
1564 if(!MSVCRT_CHECK_PMT(*mode == '='))
1565 return -1;
1566 mode++;
1567 while(*mode == ' ') mode++;
1569 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1571 *open_flags |= MSVCRT__O_U8TEXT;
1572 mode += sizeof(utf8)/sizeof(utf8[0]);
1574 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1576 *open_flags |= MSVCRT__O_U16TEXT;
1577 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1579 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1581 *open_flags |= MSVCRT__O_WTEXT;
1582 mode += sizeof(unicode)/sizeof(unicode[0]);
1584 else
1586 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1587 return -1;
1590 while(*mode == ' ') mode++;
1593 if(!MSVCRT_CHECK_PMT(*mode == 0))
1594 return -1;
1595 return 0;
1598 /*********************************************************************
1599 * _fdopen (MSVCRT.@)
1601 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1603 MSVCRT_FILE *ret;
1604 MSVCRT_wchar_t *modeW = NULL;
1606 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1608 ret = MSVCRT__wfdopen(fd, modeW);
1610 MSVCRT_free(modeW);
1611 return ret;
1614 /*********************************************************************
1615 * _wfdopen (MSVCRT.@)
1617 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1619 int open_flags, stream_flags;
1620 MSVCRT_FILE* file;
1622 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1624 LOCK_FILES();
1625 if (!(file = msvcrt_alloc_fp()))
1626 file = NULL;
1627 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1629 file->_flag = 0;
1630 file = NULL;
1632 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1633 UNLOCK_FILES();
1635 return file;
1638 /*********************************************************************
1639 * _filelength (MSVCRT.@)
1641 LONG CDECL MSVCRT__filelength(int fd)
1643 LONG curPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
1644 if (curPos != -1)
1646 LONG endPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_END);
1647 if (endPos != -1)
1649 if (endPos != curPos)
1650 MSVCRT__lseek(fd, curPos, MSVCRT_SEEK_SET);
1651 return endPos;
1654 return -1;
1657 /*********************************************************************
1658 * _filelengthi64 (MSVCRT.@)
1660 __int64 CDECL MSVCRT__filelengthi64(int fd)
1662 __int64 curPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1663 if (curPos != -1)
1665 __int64 endPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_END);
1666 if (endPos != -1)
1668 if (endPos != curPos)
1669 MSVCRT__lseeki64(fd, curPos, MSVCRT_SEEK_SET);
1670 return endPos;
1673 return -1;
1676 /*********************************************************************
1677 * _fileno (MSVCRT.@)
1679 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1681 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1682 return file->_file;
1685 /*********************************************************************
1686 * _fstat64 (MSVCRT.@)
1688 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1690 ioinfo *info = get_ioinfo(fd);
1691 DWORD dw;
1692 DWORD type;
1693 BY_HANDLE_FILE_INFORMATION hfi;
1695 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1696 if (info->handle == INVALID_HANDLE_VALUE)
1698 release_ioinfo(info);
1699 return -1;
1702 if (!buf)
1704 WARN(":failed-NULL buf\n");
1705 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1706 release_ioinfo(info);
1707 return -1;
1710 memset(&hfi, 0, sizeof(hfi));
1711 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1712 type = GetFileType(info->handle);
1713 if (type == FILE_TYPE_PIPE)
1715 buf->st_dev = buf->st_rdev = fd;
1716 buf->st_mode = MSVCRT__S_IFIFO;
1717 buf->st_nlink = 1;
1719 else if (type == FILE_TYPE_CHAR)
1721 buf->st_dev = buf->st_rdev = fd;
1722 buf->st_mode = MSVCRT__S_IFCHR;
1723 buf->st_nlink = 1;
1725 else /* FILE_TYPE_DISK etc. */
1727 if (!GetFileInformationByHandle(info->handle, &hfi))
1729 WARN(":failed-last error (%d)\n",GetLastError());
1730 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1731 release_ioinfo(info);
1732 return -1;
1734 buf->st_mode = MSVCRT__S_IFREG | 0444;
1735 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1736 buf->st_mode |= 0222;
1737 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1738 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1739 buf->st_atime = dw;
1740 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1741 buf->st_mtime = buf->st_ctime = dw;
1742 buf->st_nlink = hfi.nNumberOfLinks;
1744 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1745 buf->st_mode);
1746 release_ioinfo(info);
1747 return 0;
1750 /*********************************************************************
1751 * _fstati64 (MSVCRT.@)
1753 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1755 int ret;
1756 struct MSVCRT__stat64 buf64;
1758 ret = MSVCRT__fstat64(fd, &buf64);
1759 if (!ret)
1760 msvcrt_stat64_to_stati64(&buf64, buf);
1761 return ret;
1764 /*********************************************************************
1765 * _fstat (MSVCRT.@)
1767 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1768 { int ret;
1769 struct MSVCRT__stat64 buf64;
1771 ret = MSVCRT__fstat64(fd, &buf64);
1772 if (!ret)
1773 msvcrt_stat64_to_stat(&buf64, buf);
1774 return ret;
1777 /*********************************************************************
1778 * _fstat32 (MSVCR80.@)
1780 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1782 int ret;
1783 struct MSVCRT__stat64 buf64;
1785 ret = MSVCRT__fstat64(fd, &buf64);
1786 if (!ret)
1787 msvcrt_stat64_to_stat32(&buf64, buf);
1788 return ret;
1791 /*********************************************************************
1792 * _fstat64i32 (MSVCR80.@)
1794 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1796 int ret;
1797 struct MSVCRT__stat64 buf64;
1799 ret = MSVCRT__fstat64(fd, &buf64);
1800 if (!ret)
1801 msvcrt_stat64_to_stat64i32(&buf64, buf);
1802 return ret;
1805 /*********************************************************************
1806 * _futime64 (MSVCRT.@)
1808 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1810 ioinfo *info = get_ioinfo(fd);
1811 FILETIME at, wt;
1813 if (!t)
1815 time_to_filetime( MSVCRT__time64(NULL), &at );
1816 wt = at;
1818 else
1820 time_to_filetime( t->actime, &at );
1821 time_to_filetime( t->modtime, &wt );
1824 if (!SetFileTime(info->handle, NULL, &at, &wt))
1826 release_ioinfo(info);
1827 msvcrt_set_errno(GetLastError());
1828 return -1 ;
1830 release_ioinfo(info);
1831 return 0;
1834 /*********************************************************************
1835 * _futime32 (MSVCRT.@)
1837 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1839 if (t)
1841 struct MSVCRT___utimbuf64 t64;
1842 t64.actime = t->actime;
1843 t64.modtime = t->modtime;
1844 return _futime64( fd, &t64 );
1846 else
1847 return _futime64( fd, NULL );
1850 /*********************************************************************
1851 * _futime (MSVCRT.@)
1853 #ifdef _WIN64
1854 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1856 return _futime64( fd, t );
1858 #else
1859 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1861 return _futime32( fd, t );
1863 #endif
1865 /*********************************************************************
1866 * _get_osfhandle (MSVCRT.@)
1868 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1870 HANDLE hand = get_ioinfo_nolock(fd)->handle;
1871 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1873 if(hand == INVALID_HANDLE_VALUE)
1874 *MSVCRT__errno() = MSVCRT_EBADF;
1875 return (MSVCRT_intptr_t)hand;
1878 /*********************************************************************
1879 * _mktemp_s (MSVCRT.@)
1881 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1883 DWORD len, xno, id;
1885 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1886 return MSVCRT_EINVAL;
1888 for(len=0; len<size; len++)
1889 if(!pattern[len])
1890 break;
1891 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1892 if(size)
1893 pattern[0] = 0;
1894 return MSVCRT_EINVAL;
1897 for(xno=1; xno<=6; xno++)
1898 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1899 return MSVCRT_EINVAL;
1901 id = GetCurrentProcessId();
1902 for(xno=1; xno<6; xno++) {
1903 pattern[len-xno] = id%10 + '0';
1904 id /= 10;
1907 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1908 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1909 return 0;
1912 pattern[0] = 0;
1913 *MSVCRT__errno() = MSVCRT_EEXIST;
1914 return MSVCRT_EEXIST;
1917 /*********************************************************************
1918 * _mktemp (MSVCRT.@)
1920 char * CDECL MSVCRT__mktemp(char *pattern)
1922 int numX = 0;
1923 char *retVal = pattern;
1924 int id;
1925 char letter = 'a';
1927 if(!pattern)
1928 return NULL;
1930 while(*pattern)
1931 numX = (*pattern++ == 'X')? numX + 1 : 0;
1932 if (numX < 6)
1933 return NULL;
1934 pattern--;
1935 id = GetCurrentProcessId();
1936 numX = 6;
1937 while(numX--)
1939 int tempNum = id / 10;
1940 *pattern-- = id - (tempNum * 10) + '0';
1941 id = tempNum;
1943 pattern++;
1946 *pattern = letter++;
1947 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1948 return retVal;
1949 } while(letter <= 'z');
1950 return NULL;
1953 /*********************************************************************
1954 * _wmktemp_s (MSVCRT.@)
1956 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1958 DWORD len, xno, id;
1960 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1961 return MSVCRT_EINVAL;
1963 for(len=0; len<size; len++)
1964 if(!pattern[len])
1965 break;
1966 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1967 if(size)
1968 pattern[0] = 0;
1969 return MSVCRT_EINVAL;
1972 for(xno=1; xno<=6; xno++)
1973 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1974 return MSVCRT_EINVAL;
1976 id = GetCurrentProcessId();
1977 for(xno=1; xno<6; xno++) {
1978 pattern[len-xno] = id%10 + '0';
1979 id /= 10;
1982 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1983 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1984 return 0;
1987 pattern[0] = 0;
1988 *MSVCRT__errno() = MSVCRT_EEXIST;
1989 return MSVCRT_EEXIST;
1992 /*********************************************************************
1993 * _wmktemp (MSVCRT.@)
1995 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1997 int numX = 0;
1998 MSVCRT_wchar_t *retVal = pattern;
1999 int id;
2000 MSVCRT_wchar_t letter = 'a';
2002 if(!pattern)
2003 return NULL;
2005 while(*pattern)
2006 numX = (*pattern++ == 'X')? numX + 1 : 0;
2007 if (numX < 6)
2008 return NULL;
2009 pattern--;
2010 id = GetCurrentProcessId();
2011 numX = 6;
2012 while(numX--)
2014 int tempNum = id / 10;
2015 *pattern-- = id - (tempNum * 10) + '0';
2016 id = tempNum;
2018 pattern++;
2021 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
2022 return retVal;
2023 *pattern = letter++;
2024 } while(letter != '|');
2025 return NULL;
2028 static unsigned split_oflags(unsigned oflags)
2030 int wxflags = 0;
2031 unsigned unsupp; /* until we support everything */
2033 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
2034 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
2035 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
2036 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
2037 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
2038 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
2039 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
2040 else wxflags |= WX_TEXT; /* default to TEXT*/
2041 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2043 if ((unsupp = oflags & ~(
2044 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
2045 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
2046 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
2047 MSVCRT__O_NOINHERIT|
2048 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
2049 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
2051 ERR(":unsupported oflags 0x%04x\n",unsupp);
2053 return wxflags;
2056 /*********************************************************************
2057 * _pipe (MSVCRT.@)
2059 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
2061 int ret = -1;
2062 SECURITY_ATTRIBUTES sa;
2063 HANDLE readHandle, writeHandle;
2065 if (!pfds)
2067 *MSVCRT__errno() = MSVCRT_EINVAL;
2068 return -1;
2071 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2072 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
2073 sa.lpSecurityDescriptor = NULL;
2074 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2076 unsigned int wxflags = split_oflags(textmode);
2077 int fd;
2079 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2080 if (fd != -1)
2082 pfds[0] = fd;
2083 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2084 if (fd != -1)
2086 pfds[1] = fd;
2087 ret = 0;
2089 else
2091 MSVCRT__close(pfds[0]);
2092 CloseHandle(writeHandle);
2093 *MSVCRT__errno() = MSVCRT_EMFILE;
2096 else
2098 CloseHandle(readHandle);
2099 CloseHandle(writeHandle);
2100 *MSVCRT__errno() = MSVCRT_EMFILE;
2103 else
2104 msvcrt_set_errno(GetLastError());
2106 return ret;
2109 static int check_bom(HANDLE h, int oflags, BOOL seek)
2111 char bom[sizeof(utf8_bom)];
2112 DWORD r;
2114 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2116 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2117 return oflags;
2119 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2120 oflags |= MSVCRT__O_U8TEXT;
2121 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2122 if (seek && r>2)
2123 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2124 oflags |= MSVCRT__O_U16TEXT;
2125 }else if (seek) {
2126 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2129 return oflags;
2132 /*********************************************************************
2133 * _wsopen_s (MSVCRT.@)
2135 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2137 DWORD access = 0, creation = 0, attrib;
2138 SECURITY_ATTRIBUTES sa;
2139 DWORD sharing, type;
2140 int wxflag;
2141 HANDLE hand;
2143 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2144 fd, debugstr_w(path), oflags, shflags, pmode);
2146 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2148 *fd = -1;
2149 wxflag = split_oflags(oflags);
2150 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2152 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2153 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2154 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2157 if (oflags & MSVCRT__O_CREAT)
2159 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
2160 FIXME(": pmode 0x%04x ignored\n", pmode);
2162 if (oflags & MSVCRT__O_EXCL)
2163 creation = CREATE_NEW;
2164 else if (oflags & MSVCRT__O_TRUNC)
2165 creation = CREATE_ALWAYS;
2166 else
2167 creation = OPEN_ALWAYS;
2169 else /* no MSVCRT__O_CREAT */
2171 if (oflags & MSVCRT__O_TRUNC)
2172 creation = TRUNCATE_EXISTING;
2173 else
2174 creation = OPEN_EXISTING;
2177 switch( shflags )
2179 case MSVCRT__SH_DENYRW:
2180 sharing = 0L;
2181 break;
2182 case MSVCRT__SH_DENYWR:
2183 sharing = FILE_SHARE_READ;
2184 break;
2185 case MSVCRT__SH_DENYRD:
2186 sharing = FILE_SHARE_WRITE;
2187 break;
2188 case MSVCRT__SH_DENYNO:
2189 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2190 break;
2191 default:
2192 ERR( "Unhandled shflags 0x%x\n", shflags );
2193 return MSVCRT_EINVAL;
2196 if (!(pmode & ~MSVCRT_umask & MSVCRT__S_IWRITE))
2197 attrib = FILE_ATTRIBUTE_READONLY;
2198 else
2199 attrib = FILE_ATTRIBUTE_NORMAL;
2201 if (oflags & MSVCRT__O_TEMPORARY)
2203 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2204 access |= DELETE;
2205 sharing |= FILE_SHARE_DELETE;
2208 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2209 sa.lpSecurityDescriptor = NULL;
2210 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2212 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2213 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2214 && !(access&GENERIC_READ))
2216 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2217 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2218 if (hand != INVALID_HANDLE_VALUE)
2220 oflags = check_bom(hand, oflags, FALSE);
2221 CloseHandle(hand);
2223 else
2224 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2227 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2228 if (hand == INVALID_HANDLE_VALUE) {
2229 WARN(":failed-last error (%d)\n",GetLastError());
2230 msvcrt_set_errno(GetLastError());
2231 return *MSVCRT__errno();
2234 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2236 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2237 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2238 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2240 if (oflags & MSVCRT__O_U8TEXT)
2242 DWORD written = 0, tmp;
2244 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2245 sizeof(utf8_bom)-written, &tmp, NULL))
2246 written += tmp;
2247 if (written != sizeof(utf8_bom)) {
2248 WARN("error writing BOM\n");
2249 CloseHandle(hand);
2250 msvcrt_set_errno(GetLastError());
2251 return *MSVCRT__errno();
2254 else
2256 DWORD written = 0, tmp;
2258 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2259 sizeof(utf16_bom)-written, &tmp, NULL))
2260 written += tmp;
2261 if (written != sizeof(utf16_bom))
2263 WARN("error writing BOM\n");
2264 CloseHandle(hand);
2265 msvcrt_set_errno(GetLastError());
2266 return *MSVCRT__errno();
2270 else if (access & GENERIC_READ)
2271 oflags = check_bom(hand, oflags, TRUE);
2274 type = GetFileType(hand);
2275 if (type == FILE_TYPE_CHAR)
2276 wxflag |= WX_TTY;
2277 else if (type == FILE_TYPE_PIPE)
2278 wxflag |= WX_PIPE;
2280 *fd = msvcrt_alloc_fd(hand, wxflag);
2281 if (*fd == -1)
2282 return *MSVCRT__errno();
2284 if (oflags & MSVCRT__O_WTEXT)
2285 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2286 else if (oflags & MSVCRT__O_U16TEXT)
2287 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
2288 else if (oflags & MSVCRT__O_U8TEXT)
2289 get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
2291 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2292 return 0;
2295 /*********************************************************************
2296 * _wsopen (MSVCRT.@)
2298 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2300 int pmode;
2301 int fd;
2303 if (oflags & MSVCRT__O_CREAT)
2305 __ms_va_list ap;
2307 __ms_va_start(ap, shflags);
2308 pmode = va_arg(ap, int);
2309 __ms_va_end(ap);
2311 else
2312 pmode = 0;
2314 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
2315 return fd;
2318 /*********************************************************************
2319 * _sopen_s (MSVCRT.@)
2321 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2323 MSVCRT_wchar_t *pathW;
2324 int ret;
2326 if (!MSVCRT_CHECK_PMT(fd != NULL))
2327 return MSVCRT_EINVAL;
2328 *fd = -1;
2329 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2330 return MSVCRT_EINVAL;
2332 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2333 MSVCRT_free(pathW);
2334 return ret;
2337 /*********************************************************************
2338 * _sopen (MSVCRT.@)
2340 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2342 int pmode;
2343 int fd;
2345 if (oflags & MSVCRT__O_CREAT)
2347 __ms_va_list ap;
2349 __ms_va_start(ap, shflags);
2350 pmode = va_arg(ap, int);
2351 __ms_va_end(ap);
2353 else
2354 pmode = 0;
2356 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2357 return fd;
2360 /*********************************************************************
2361 * _open (MSVCRT.@)
2363 int CDECL MSVCRT__open( const char *path, int flags, ... )
2365 __ms_va_list ap;
2367 if (flags & MSVCRT__O_CREAT)
2369 int pmode;
2370 __ms_va_start(ap, flags);
2371 pmode = va_arg(ap, int);
2372 __ms_va_end(ap);
2373 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2375 else
2376 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2379 /*********************************************************************
2380 * _wopen (MSVCRT.@)
2382 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2384 __ms_va_list ap;
2386 if (flags & MSVCRT__O_CREAT)
2388 int pmode;
2389 __ms_va_start(ap, flags);
2390 pmode = va_arg(ap, int);
2391 __ms_va_end(ap);
2392 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2394 else
2395 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2398 /*********************************************************************
2399 * _creat (MSVCRT.@)
2401 int CDECL MSVCRT__creat(const char *path, int flags)
2403 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2404 return MSVCRT__open(path, usedFlags);
2407 /*********************************************************************
2408 * _wcreat (MSVCRT.@)
2410 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2412 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2413 return MSVCRT__wopen(path, usedFlags);
2416 /*********************************************************************
2417 * _open_osfhandle (MSVCRT.@)
2419 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2421 DWORD flags;
2422 int fd;
2424 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2425 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2426 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2427 * text - it never sets MSVCRT__O_BINARY.
2429 /* don't let split_oflags() decide the mode if no mode is passed */
2430 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2431 oflags |= MSVCRT__O_BINARY;
2433 flags = GetFileType((HANDLE)handle);
2434 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2436 msvcrt_set_errno(GetLastError());
2437 return -1;
2440 if (flags == FILE_TYPE_CHAR)
2441 flags = WX_TTY;
2442 else if (flags == FILE_TYPE_PIPE)
2443 flags = WX_PIPE;
2444 else
2445 flags = 0;
2446 flags |= split_oflags(oflags);
2448 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2449 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2450 return fd;
2453 /*********************************************************************
2454 * _rmtmp (MSVCRT.@)
2456 int CDECL MSVCRT__rmtmp(void)
2458 int num_removed = 0, i;
2459 MSVCRT_FILE *file;
2461 LOCK_FILES();
2462 for (i = 3; i < MSVCRT_stream_idx; i++) {
2463 file = msvcrt_get_file(i);
2465 if (file->_tmpfname)
2467 MSVCRT_fclose(file);
2468 num_removed++;
2471 UNLOCK_FILES();
2473 if (num_removed)
2474 TRACE(":removed (%d) temp files\n",num_removed);
2475 return num_removed;
2478 static inline int get_utf8_char_len(char ch)
2480 if((ch&0xf8) == 0xf0)
2481 return 4;
2482 else if((ch&0xf0) == 0xe0)
2483 return 3;
2484 else if((ch&0xe0) == 0xc0)
2485 return 2;
2486 return 1;
2489 /*********************************************************************
2490 * (internal) read_utf8
2492 static int read_utf8(ioinfo *fdinfo, MSVCRT_wchar_t *buf, unsigned int count)
2494 HANDLE hand = fdinfo->handle;
2495 char min_buf[4], *readbuf, lookahead;
2496 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2498 /* make the buffer big enough to hold at least one character */
2499 /* read bytes have to fit to output and lookahead buffers */
2500 count /= 2;
2501 readbuf_size = count < 4 ? 4 : count;
2502 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2503 readbuf_size = 4;
2504 readbuf = min_buf;
2507 if(fdinfo->lookahead[0] != '\n') {
2508 readbuf[pos++] = fdinfo->lookahead[0];
2509 fdinfo->lookahead[0] = '\n';
2511 if(fdinfo->lookahead[1] != '\n') {
2512 readbuf[pos++] = fdinfo->lookahead[1];
2513 fdinfo->lookahead[1] = '\n';
2515 if(fdinfo->lookahead[2] != '\n') {
2516 readbuf[pos++] = fdinfo->lookahead[2];
2517 fdinfo->lookahead[2] = '\n';
2522 /* NOTE: this case is broken in native dll, reading
2523 * sometimes fails when small buffer is passed
2525 if(count < 4) {
2526 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2527 if (GetLastError() == ERROR_BROKEN_PIPE) {
2528 fdinfo->wxflag |= WX_ATEOF;
2529 return 0;
2530 }else {
2531 msvcrt_set_errno(GetLastError());
2532 return -1;
2534 }else if(!num_read) {
2535 fdinfo->wxflag |= WX_ATEOF;
2536 return 0;
2537 }else {
2538 pos++;
2541 char_len = get_utf8_char_len(readbuf[0]);
2542 if(char_len>pos) {
2543 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2544 pos += num_read;
2547 if(readbuf[0] == '\n')
2548 fdinfo->wxflag |= WX_READNL;
2549 else
2550 fdinfo->wxflag &= ~WX_READNL;
2552 if(readbuf[0] == 0x1a) {
2553 fdinfo->wxflag |= WX_ATEOF;
2554 return 0;
2557 if(readbuf[0] == '\r') {
2558 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2559 buf[0] = '\r';
2560 else if(lookahead == '\n')
2561 buf[0] = '\n';
2562 else {
2563 buf[0] = '\r';
2564 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2565 fdinfo->lookahead[0] = lookahead;
2566 else
2567 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2569 return 2;
2572 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2573 msvcrt_set_errno(GetLastError());
2574 return -1;
2577 return num_read*2;
2580 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2581 if(pos) {
2582 num_read = 0;
2583 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2584 fdinfo->wxflag |= WX_ATEOF;
2585 if (readbuf != min_buf) MSVCRT_free(readbuf);
2586 return 0;
2587 }else {
2588 msvcrt_set_errno(GetLastError());
2589 if (readbuf != min_buf) MSVCRT_free(readbuf);
2590 return -1;
2592 }else if(!pos && !num_read) {
2593 fdinfo->wxflag |= WX_ATEOF;
2594 if (readbuf != min_buf) MSVCRT_free(readbuf);
2595 return 0;
2598 pos += num_read;
2599 if(readbuf[0] == '\n')
2600 fdinfo->wxflag |= WX_READNL;
2601 else
2602 fdinfo->wxflag &= ~WX_READNL;
2604 /* Find first byte of last character (may be incomplete) */
2605 for(i=pos-1; i>0 && i>pos-4; i--)
2606 if((readbuf[i]&0xc0) != 0x80)
2607 break;
2608 char_len = get_utf8_char_len(readbuf[i]);
2609 if(char_len+i <= pos)
2610 i += char_len;
2612 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2613 if(i < pos)
2614 fdinfo->lookahead[0] = readbuf[i];
2615 if(i+1 < pos)
2616 fdinfo->lookahead[1] = readbuf[i+1];
2617 if(i+2 < pos)
2618 fdinfo->lookahead[2] = readbuf[i+2];
2619 }else if(i < pos) {
2620 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2622 pos = i;
2624 for(i=0, j=0; i<pos; i++) {
2625 if(readbuf[i] == 0x1a) {
2626 fdinfo->wxflag |= WX_ATEOF;
2627 break;
2630 /* strip '\r' if followed by '\n' */
2631 if(readbuf[i] == '\r' && i+1==pos) {
2632 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2633 readbuf[j++] = '\r';
2634 }else if(lookahead == '\n' && j==0) {
2635 readbuf[j++] = '\n';
2636 }else {
2637 if(lookahead != '\n')
2638 readbuf[j++] = '\r';
2640 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2641 fdinfo->lookahead[0] = lookahead;
2642 else
2643 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2645 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2646 readbuf[j++] = readbuf[i];
2649 pos = j;
2651 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2652 msvcrt_set_errno(GetLastError());
2653 if (readbuf != min_buf) MSVCRT_free(readbuf);
2654 return -1;
2657 if (readbuf != min_buf) MSVCRT_free(readbuf);
2658 return num_read*2;
2661 /*********************************************************************
2662 * (internal) read_i
2664 * When reading \r as last character in text mode, read() positions
2665 * the file pointer on the \r character while getc() goes on to
2666 * the following \n
2668 static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
2670 DWORD num_read, utf16;
2671 char *bufstart = buf;
2673 if (count == 0)
2674 return 0;
2676 if (fdinfo->wxflag & WX_ATEOF) {
2677 TRACE("already at EOF, returning 0\n");
2678 return 0;
2680 /* Don't trace small reads, it gets *very* annoying */
2681 if (count > 4)
2682 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2683 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2685 *MSVCRT__errno() = MSVCRT_EBADF;
2686 return -1;
2689 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2690 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2692 *MSVCRT__errno() = MSVCRT_EINVAL;
2693 return -1;
2696 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2697 return read_utf8(fdinfo, buf, count);
2699 if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
2701 if (fdinfo->lookahead[0] != '\n')
2703 bufstart[0] = fdinfo->lookahead[0];
2704 fdinfo->lookahead[0] = '\n';
2706 if (utf16)
2708 bufstart[1] = fdinfo->lookahead[1];
2709 fdinfo->lookahead[1] = '\n';
2712 if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2713 num_read += 1+utf16;
2714 else
2715 num_read = 1+utf16;
2718 if(utf16 && (num_read&1))
2720 /* msvcr90 uses uninitialized value from the buffer in this case */
2721 /* msvcrt ignores additional data */
2722 ERR("got odd number of bytes in UTF16 mode\n");
2723 num_read--;
2726 if (count != 0 && num_read == 0)
2728 fdinfo->wxflag |= WX_ATEOF;
2729 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2731 else if (fdinfo->wxflag & WX_TEXT)
2733 DWORD i, j;
2735 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2736 fdinfo->wxflag |= WX_READNL;
2737 else
2738 fdinfo->wxflag &= ~WX_READNL;
2740 for (i=0, j=0; i<num_read; i+=1+utf16)
2742 /* in text mode, a ctrl-z signals EOF */
2743 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2745 fdinfo->wxflag |= WX_ATEOF;
2746 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2747 break;
2750 /* in text mode, strip \r if followed by \n */
2751 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2753 char lookahead[2];
2754 DWORD len;
2756 lookahead[1] = '\n';
2757 if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
2759 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2761 bufstart[j++] = '\n';
2762 if(utf16) bufstart[j++] = 0;
2764 else
2766 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2768 bufstart[j++] = '\r';
2769 if(utf16) bufstart[j++] = 0;
2772 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2774 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2776 bufstart[j++] = '\n';
2777 if (utf16) bufstart[j++] = 0;
2779 else
2781 fdinfo->lookahead[0] = lookahead[0];
2782 fdinfo->lookahead[1] = lookahead[1];
2785 else
2786 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2789 else
2791 bufstart[j++] = '\r';
2792 if(utf16) bufstart[j++] = 0;
2795 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2796 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2798 bufstart[j++] = bufstart[i];
2799 if(utf16) bufstart[j++] = bufstart[i+1];
2802 num_read = j;
2805 else
2807 if (GetLastError() == ERROR_BROKEN_PIPE)
2809 TRACE(":end-of-pipe\n");
2810 fdinfo->wxflag |= WX_ATEOF;
2811 return 0;
2813 else
2815 TRACE(":failed-last error (%d)\n",GetLastError());
2816 return -1;
2820 if (count > 4)
2821 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2822 return num_read;
2825 /*********************************************************************
2826 * _read (MSVCRT.@)
2828 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2830 ioinfo *info = get_ioinfo(fd);
2831 int num_read = read_i(fd, info, buf, count);
2832 release_ioinfo(info);
2833 return num_read;
2836 /*********************************************************************
2837 * _setmode (MSVCRT.@)
2839 int CDECL MSVCRT__setmode(int fd,int mode)
2841 ioinfo *info = get_ioinfo(fd);
2842 int ret = info->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2843 if(ret==MSVCRT__O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16)))
2844 ret = MSVCRT__O_WTEXT;
2846 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2847 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2848 *MSVCRT__errno() = MSVCRT_EINVAL;
2849 release_ioinfo(info);
2850 return -1;
2853 if(info == &MSVCRT___badioinfo) {
2854 *MSVCRT__errno() = MSVCRT_EBADF;
2855 return MSVCRT_EOF;
2858 if(mode == MSVCRT__O_BINARY) {
2859 info->wxflag &= ~WX_TEXT;
2860 info->exflag &= ~(EF_UTF8|EF_UTF16);
2861 release_ioinfo(info);
2862 return ret;
2865 info->wxflag |= WX_TEXT;
2866 if(mode == MSVCRT__O_TEXT)
2867 info->exflag &= ~(EF_UTF8|EF_UTF16);
2868 else if(mode == MSVCRT__O_U8TEXT)
2869 info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
2870 else
2871 info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
2873 release_ioinfo(info);
2874 return ret;
2877 /*********************************************************************
2878 * _stat64 (MSVCRT.@)
2880 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2882 DWORD dw;
2883 WIN32_FILE_ATTRIBUTE_DATA hfi;
2884 unsigned short mode = ALL_S_IREAD;
2885 int plen;
2887 TRACE(":file (%s) buf(%p)\n",path,buf);
2889 plen = strlen(path);
2890 while (plen && path[plen-1]==' ')
2891 plen--;
2893 if (plen && (plen<2 || path[plen-2]!=':') &&
2894 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2896 *MSVCRT__errno() = MSVCRT_ENOENT;
2897 return -1;
2900 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2902 TRACE("failed (%d)\n",GetLastError());
2903 *MSVCRT__errno() = MSVCRT_ENOENT;
2904 return -1;
2907 memset(buf,0,sizeof(struct MSVCRT__stat64));
2909 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2910 Bon 011120: This FIXME seems incorrect
2911 Also a letter as first char isn't enough to be classified
2912 as a drive letter
2914 if (isalpha(*path)&& (*(path+1)==':'))
2915 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2916 else
2917 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2919 /* Dir, or regular file? */
2920 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2921 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2922 else
2924 mode |= MSVCRT__S_IFREG;
2925 /* executable? */
2926 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2928 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2929 (tolower(path[plen-3]) << 16);
2930 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2931 mode |= ALL_S_IEXEC;
2935 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2936 mode |= ALL_S_IWRITE;
2938 buf->st_mode = mode;
2939 buf->st_nlink = 1;
2940 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2941 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2942 buf->st_atime = dw;
2943 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2944 buf->st_mtime = buf->st_ctime = dw;
2945 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2946 (int)(buf->st_size >> 32),(int)buf->st_size,
2947 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2948 return 0;
2951 /*********************************************************************
2952 * _stati64 (MSVCRT.@)
2954 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2956 int ret;
2957 struct MSVCRT__stat64 buf64;
2959 ret = MSVCRT_stat64(path, &buf64);
2960 if (!ret)
2961 msvcrt_stat64_to_stati64(&buf64, buf);
2962 return ret;
2965 /*********************************************************************
2966 * _stat (MSVCRT.@)
2968 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2970 int ret;
2971 struct MSVCRT__stat64 buf64;
2973 ret = MSVCRT_stat64( path, &buf64);
2974 if (!ret)
2975 msvcrt_stat64_to_stat(&buf64, buf);
2976 return ret;
2979 /*********************************************************************
2980 * _stat32 (MSVCR100.@)
2982 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
2984 int ret;
2985 struct MSVCRT__stat64 buf64;
2987 ret = MSVCRT_stat64(path, &buf64);
2988 if (!ret)
2989 msvcrt_stat64_to_stat32(&buf64, buf);
2990 return ret;
2993 /*********************************************************************
2994 * _stat32i64 (MSVCR100.@)
2996 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
2998 int ret;
2999 struct MSVCRT__stat64 buf64;
3001 ret = MSVCRT_stat64(path, &buf64);
3002 if (!ret)
3003 msvcrt_stat64_to_stat32i64(&buf64, buf);
3004 return ret;
3007 /*********************************************************************
3008 * _stat64i32 (MSVCR100.@)
3010 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
3012 int ret;
3013 struct MSVCRT__stat64 buf64;
3015 ret = MSVCRT_stat64(path, &buf64);
3016 if (!ret)
3017 msvcrt_stat64_to_stat64i32(&buf64, buf);
3018 return ret;
3021 /*********************************************************************
3022 * _wstat64 (MSVCRT.@)
3024 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
3026 DWORD dw;
3027 WIN32_FILE_ATTRIBUTE_DATA hfi;
3028 unsigned short mode = ALL_S_IREAD;
3029 int plen;
3031 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
3033 plen = strlenW(path);
3034 while (plen && path[plen-1]==' ')
3035 plen--;
3037 if(plen && (plen<2 || path[plen-2]!=':') &&
3038 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
3040 *MSVCRT__errno() = MSVCRT_ENOENT;
3041 return -1;
3044 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
3046 TRACE("failed (%d)\n",GetLastError());
3047 *MSVCRT__errno() = MSVCRT_ENOENT;
3048 return -1;
3051 memset(buf,0,sizeof(struct MSVCRT__stat64));
3053 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3054 if (MSVCRT_iswalpha(*path))
3055 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
3056 else
3057 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3059 /* Dir, or regular file? */
3060 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3061 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3062 else
3064 mode |= MSVCRT__S_IFREG;
3065 /* executable? */
3066 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3068 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
3069 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
3070 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3071 mode |= ALL_S_IEXEC;
3075 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3076 mode |= ALL_S_IWRITE;
3078 buf->st_mode = mode;
3079 buf->st_nlink = 1;
3080 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3081 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3082 buf->st_atime = dw;
3083 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3084 buf->st_mtime = buf->st_ctime = dw;
3085 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3086 (int)(buf->st_size >> 32),(int)buf->st_size,
3087 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3088 return 0;
3091 /*********************************************************************
3092 * _wstati64 (MSVCRT.@)
3094 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
3096 int ret;
3097 struct MSVCRT__stat64 buf64;
3099 ret = MSVCRT__wstat64(path, &buf64);
3100 if (!ret)
3101 msvcrt_stat64_to_stati64(&buf64, buf);
3102 return ret;
3105 /*********************************************************************
3106 * _wstat (MSVCRT.@)
3108 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
3110 int ret;
3111 struct MSVCRT__stat64 buf64;
3113 ret = MSVCRT__wstat64( path, &buf64 );
3114 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3115 return ret;
3118 /*********************************************************************
3119 * _wstat32 (MSVCR100.@)
3121 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
3123 int ret;
3124 struct MSVCRT__stat64 buf64;
3126 ret = MSVCRT__wstat64(path, &buf64);
3127 if (!ret)
3128 msvcrt_stat64_to_stat32(&buf64, buf);
3129 return ret;
3132 /*********************************************************************
3133 * _wstat32i64 (MSVCR100.@)
3135 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
3137 int ret;
3138 struct MSVCRT__stat64 buf64;
3140 ret = MSVCRT__wstat64(path, &buf64);
3141 if (!ret)
3142 msvcrt_stat64_to_stat32i64(&buf64, buf);
3143 return ret;
3146 /*********************************************************************
3147 * _wstat64i32 (MSVCR100.@)
3149 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3151 int ret;
3152 struct MSVCRT__stat64 buf64;
3154 ret = MSVCRT__wstat64(path, &buf64);
3155 if (!ret)
3156 msvcrt_stat64_to_stat64i32(&buf64, buf);
3157 return ret;
3160 /*********************************************************************
3161 * _tell (MSVCRT.@)
3163 MSVCRT_long CDECL MSVCRT__tell(int fd)
3165 return MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
3168 /*********************************************************************
3169 * _telli64 (MSVCRT.@)
3171 __int64 CDECL _telli64(int fd)
3173 return MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
3176 /*********************************************************************
3177 * _tempnam (MSVCRT.@)
3179 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3181 char tmpbuf[MAX_PATH];
3182 const char *tmp_dir = MSVCRT_getenv("TMP");
3184 if (tmp_dir) dir = tmp_dir;
3186 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3187 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3189 TRACE("got name (%s)\n",tmpbuf);
3190 DeleteFileA(tmpbuf);
3191 return MSVCRT__strdup(tmpbuf);
3193 TRACE("failed (%d)\n",GetLastError());
3194 return NULL;
3197 /*********************************************************************
3198 * _wtempnam (MSVCRT.@)
3200 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3202 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3203 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3204 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3206 if (tmp_dir) dir = tmp_dir;
3208 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3209 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3211 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3212 DeleteFileW(tmpbuf);
3213 return MSVCRT__wcsdup(tmpbuf);
3215 TRACE("failed (%d)\n",GetLastError());
3216 return NULL;
3219 /*********************************************************************
3220 * _umask (MSVCRT.@)
3222 int CDECL MSVCRT__umask(int umask)
3224 int old_umask = MSVCRT_umask;
3225 TRACE("(%d)\n",umask);
3226 MSVCRT_umask = umask;
3227 return old_umask;
3230 /*********************************************************************
3231 * _utime64 (MSVCRT.@)
3233 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3235 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3237 if (fd > 0)
3239 int retVal = _futime64(fd, t);
3240 MSVCRT__close(fd);
3241 return retVal;
3243 return -1;
3246 /*********************************************************************
3247 * _utime32 (MSVCRT.@)
3249 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3251 if (t)
3253 struct MSVCRT___utimbuf64 t64;
3254 t64.actime = t->actime;
3255 t64.modtime = t->modtime;
3256 return _utime64( path, &t64 );
3258 else
3259 return _utime64( path, NULL );
3262 /*********************************************************************
3263 * _utime (MSVCRT.@)
3265 #ifdef _WIN64
3266 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3268 return _utime64( path, t );
3270 #else
3271 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3273 return _utime32( path, t );
3275 #endif
3277 /*********************************************************************
3278 * _wutime64 (MSVCRT.@)
3280 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3282 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3284 if (fd > 0)
3286 int retVal = _futime64(fd, t);
3287 MSVCRT__close(fd);
3288 return retVal;
3290 return -1;
3293 /*********************************************************************
3294 * _wutime32 (MSVCRT.@)
3296 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3298 if (t)
3300 struct MSVCRT___utimbuf64 t64;
3301 t64.actime = t->actime;
3302 t64.modtime = t->modtime;
3303 return _wutime64( path, &t64 );
3305 else
3306 return _wutime64( path, NULL );
3309 /*********************************************************************
3310 * _wutime (MSVCRT.@)
3312 #ifdef _WIN64
3313 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3315 return _wutime64( path, t );
3317 #else
3318 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3320 return _wutime32( path, t );
3322 #endif
3324 /*********************************************************************
3325 * _write (MSVCRT.@)
3327 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3329 DWORD num_written;
3330 ioinfo *info = get_ioinfo(fd);
3331 HANDLE hand = info->handle;
3333 /* Don't trace small writes, it gets *very* annoying */
3334 #if 0
3335 if (count > 32)
3336 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3337 #endif
3338 if (hand == INVALID_HANDLE_VALUE)
3340 *MSVCRT__errno() = MSVCRT_EBADF;
3341 release_ioinfo(info);
3342 return -1;
3345 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3347 *MSVCRT__errno() = MSVCRT_EINVAL;
3348 release_ioinfo(info);
3349 return -1;
3352 /* If appending, go to EOF */
3353 if (info->wxflag & WX_APPEND)
3354 MSVCRT__lseek(fd, 0, FILE_END);
3356 if (!(info->wxflag & WX_TEXT))
3358 if (WriteFile(hand, buf, count, &num_written, NULL)
3359 && (num_written == count))
3361 release_ioinfo(info);
3362 return num_written;
3364 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3365 hand, GetLastError());
3366 *MSVCRT__errno() = MSVCRT_ENOSPC;
3368 else
3370 unsigned int i, j, nr_lf, size;
3371 char *p = NULL;
3372 const char *q;
3373 const char *s = buf, *buf_start = buf;
3375 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3377 /* find number of \n */
3378 for (nr_lf=0, i=0; i<count; i++)
3379 if (s[i] == '\n')
3380 nr_lf++;
3381 if (nr_lf)
3383 size = count+nr_lf;
3384 if ((q = p = MSVCRT_malloc(size)))
3386 for (s = buf, i = 0, j = 0; i < count; i++)
3388 if (s[i] == '\n')
3389 p[j++] = '\r';
3390 p[j++] = s[i];
3393 else
3395 FIXME("Malloc failed\n");
3396 nr_lf = 0;
3397 size = count;
3398 q = buf;
3401 else
3403 size = count;
3404 q = buf;
3407 else if (info->exflag & EF_UTF16)
3409 for (nr_lf=0, i=0; i<count; i+=2)
3410 if (s[i]=='\n' && s[i+1]==0)
3411 nr_lf += 2;
3412 if (nr_lf)
3414 size = count+nr_lf;
3415 if ((q = p = MSVCRT_malloc(size)))
3417 for (s=buf, i=0, j=0; i<count; i++)
3419 if (s[i]=='\n' && s[i+1]==0)
3421 p[j++] = '\r';
3422 p[j++] = 0;
3424 p[j++] = s[i++];
3425 p[j++] = s[i];
3428 else
3430 FIXME("Malloc failed\n");
3431 nr_lf = 0;
3432 size = count;
3433 q = buf;
3436 else
3438 size = count;
3439 q = buf;
3442 else
3444 DWORD conv_len;
3446 for(nr_lf=0, i=0; i<count; i+=2)
3447 if (s[i]=='\n' && s[i+1]==0)
3448 nr_lf++;
3450 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3451 if(!conv_len) {
3452 msvcrt_set_errno(GetLastError());
3453 MSVCRT_free(p);
3454 release_ioinfo(info);
3455 return -1;
3458 size = conv_len+nr_lf;
3459 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3461 for (s=buf, i=0, j=0; i<count; i++)
3463 if (s[i]=='\n' && s[i+1]==0)
3465 p[j++] = '\r';
3466 p[j++] = 0;
3468 p[j++] = s[i++];
3469 p[j++] = s[i];
3471 q = p+count+nr_lf*2;
3472 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3473 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3475 else
3477 FIXME("Malloc failed\n");
3478 nr_lf = 0;
3479 size = count;
3480 q = buf;
3484 if (!WriteFile(hand, q, size, &num_written, NULL))
3485 num_written = -1;
3486 release_ioinfo(info);
3487 if(p)
3488 MSVCRT_free(p);
3489 if (num_written != size)
3491 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3492 fd, hand, GetLastError(), num_written);
3493 *MSVCRT__errno() = MSVCRT_ENOSPC;
3494 return s - buf_start;
3496 return count;
3499 release_ioinfo(info);
3500 return -1;
3503 /*********************************************************************
3504 * _putw (MSVCRT.@)
3506 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3508 int len;
3510 MSVCRT__lock_file(file);
3511 len = MSVCRT__write(file->_file, &val, sizeof(val));
3512 if (len == sizeof(val)) {
3513 MSVCRT__unlock_file(file);
3514 return val;
3517 file->_flag |= MSVCRT__IOERR;
3518 MSVCRT__unlock_file(file);
3519 return MSVCRT_EOF;
3522 /*********************************************************************
3523 * fclose (MSVCRT.@)
3525 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3527 int ret;
3529 MSVCRT__lock_file(file);
3530 ret = MSVCRT__fclose_nolock(file);
3531 MSVCRT__unlock_file(file);
3533 return ret;
3536 /*********************************************************************
3537 * _fclose_nolock (MSVCRT.@)
3539 int CDECL MSVCRT__fclose_nolock(MSVCRT_FILE* file)
3541 int r, flag;
3543 flag = file->_flag;
3544 MSVCRT_free(file->_tmpfname);
3545 file->_tmpfname = NULL;
3546 /* flush stdio buffers */
3547 if(file->_flag & MSVCRT__IOWRT)
3548 MSVCRT__fflush_nolock(file);
3549 if(file->_flag & MSVCRT__IOMYBUF)
3550 MSVCRT_free(file->_base);
3552 r=MSVCRT__close(file->_file);
3553 file->_flag = 0;
3555 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3558 /*********************************************************************
3559 * feof (MSVCRT.@)
3561 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3563 return file->_flag & MSVCRT__IOEOF;
3566 /*********************************************************************
3567 * ferror (MSVCRT.@)
3569 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3571 return file->_flag & MSVCRT__IOERR;
3574 /*********************************************************************
3575 * _filbuf (MSVCRT.@)
3577 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3579 unsigned char c;
3581 if(file->_flag & MSVCRT__IOSTRG)
3582 return MSVCRT_EOF;
3584 /* Allocate buffer if needed */
3585 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3586 msvcrt_alloc_buffer(file);
3588 if(!(file->_flag & MSVCRT__IOREAD)) {
3589 if(file->_flag & MSVCRT__IORW)
3590 file->_flag |= MSVCRT__IOREAD;
3591 else
3592 return MSVCRT_EOF;
3595 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3596 int r;
3597 if ((r = MSVCRT__read(file->_file,&c,1)) != 1) {
3598 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3599 return MSVCRT_EOF;
3602 return c;
3603 } else {
3604 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3605 if(file->_cnt<=0) {
3606 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3607 file->_cnt = 0;
3608 return MSVCRT_EOF;
3611 file->_cnt--;
3612 file->_ptr = file->_base+1;
3613 c = *(unsigned char *)file->_base;
3614 return c;
3618 /*********************************************************************
3619 * fgetc (MSVCRT.@)
3621 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3623 int ret;
3625 MSVCRT__lock_file(file);
3626 ret = MSVCRT__fgetc_nolock(file);
3627 MSVCRT__unlock_file(file);
3629 return ret;
3632 /*********************************************************************
3633 * _fgetc_nolock (MSVCRT.@)
3635 int CDECL MSVCRT__fgetc_nolock(MSVCRT_FILE* file)
3637 unsigned char *i;
3638 unsigned int j;
3640 if (file->_cnt>0) {
3641 file->_cnt--;
3642 i = (unsigned char *)file->_ptr++;
3643 j = *i;
3644 } else
3645 j = MSVCRT__filbuf(file);
3647 return j;
3650 /*********************************************************************
3651 * _fgetchar (MSVCRT.@)
3653 int CDECL MSVCRT__fgetchar(void)
3655 return MSVCRT_fgetc(MSVCRT_stdin);
3658 /*********************************************************************
3659 * fgets (MSVCRT.@)
3661 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3663 int cc = MSVCRT_EOF;
3664 char * buf_start = s;
3666 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3667 file,file->_file,s,size);
3669 MSVCRT__lock_file(file);
3671 while ((size >1) && (cc = MSVCRT__fgetc_nolock(file)) != MSVCRT_EOF && cc != '\n')
3673 *s++ = (char)cc;
3674 size --;
3676 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3678 TRACE(":nothing read\n");
3679 MSVCRT__unlock_file(file);
3680 return NULL;
3682 if ((cc != MSVCRT_EOF) && (size > 1))
3683 *s++ = cc;
3684 *s = '\0';
3685 TRACE(":got %s\n", debugstr_a(buf_start));
3686 MSVCRT__unlock_file(file);
3687 return buf_start;
3690 /*********************************************************************
3691 * fgetwc (MSVCRT.@)
3693 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3695 MSVCRT_wint_t ret;
3697 MSVCRT__lock_file(file);
3698 ret = MSVCRT__fgetwc_nolock(file);
3699 MSVCRT__unlock_file(file);
3701 return ret;
3704 /*********************************************************************
3705 * _fgetwc_nolock (MSVCRT.@)
3707 MSVCRT_wint_t CDECL MSVCRT__fgetwc_nolock(MSVCRT_FILE* file)
3709 MSVCRT_wint_t ret;
3710 int ch;
3712 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3713 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3714 char *p;
3716 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3717 ch = MSVCRT__fgetc_nolock(file);
3718 if(ch == MSVCRT_EOF) {
3719 ret = MSVCRT_WEOF;
3720 break;
3722 *p = (char)ch;
3724 }else {
3725 char mbs[MSVCRT_MB_LEN_MAX];
3726 int len = 0;
3728 ch = MSVCRT__fgetc_nolock(file);
3729 if(ch != MSVCRT_EOF) {
3730 mbs[0] = (char)ch;
3731 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3732 ch = MSVCRT__fgetc_nolock(file);
3733 if(ch != MSVCRT_EOF) {
3734 mbs[1] = (char)ch;
3735 len = 2;
3737 }else {
3738 len = 1;
3742 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3743 ret = MSVCRT_WEOF;
3746 return ret;
3749 /*********************************************************************
3750 * _getw (MSVCRT.@)
3752 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3754 char *ch;
3755 int i, k;
3756 unsigned int j;
3757 ch = (char *)&i;
3759 MSVCRT__lock_file(file);
3760 for (j=0; j<sizeof(int); j++) {
3761 k = MSVCRT__fgetc_nolock(file);
3762 if (k == MSVCRT_EOF) {
3763 file->_flag |= MSVCRT__IOEOF;
3764 MSVCRT__unlock_file(file);
3765 return MSVCRT_EOF;
3767 ch[j] = k;
3770 MSVCRT__unlock_file(file);
3771 return i;
3774 /*********************************************************************
3775 * getwc (MSVCRT.@)
3777 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3779 return MSVCRT_fgetwc(file);
3782 /*********************************************************************
3783 * _fgetwchar (MSVCRT.@)
3785 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3787 return MSVCRT_fgetwc(MSVCRT_stdin);
3790 /*********************************************************************
3791 * getwchar (MSVCRT.@)
3793 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3795 return MSVCRT__fgetwchar();
3798 /*********************************************************************
3799 * fgetws (MSVCRT.@)
3801 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3803 MSVCRT_wint_t cc = MSVCRT_WEOF;
3804 MSVCRT_wchar_t * buf_start = s;
3806 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3807 file,file->_file,s,size);
3809 MSVCRT__lock_file(file);
3811 while ((size >1) && (cc = MSVCRT__fgetwc_nolock(file)) != MSVCRT_WEOF && cc != '\n')
3813 *s++ = cc;
3814 size --;
3816 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3818 TRACE(":nothing read\n");
3819 MSVCRT__unlock_file(file);
3820 return NULL;
3822 if ((cc != MSVCRT_WEOF) && (size > 1))
3823 *s++ = cc;
3824 *s = 0;
3825 TRACE(":got %s\n", debugstr_w(buf_start));
3826 MSVCRT__unlock_file(file);
3827 return buf_start;
3830 /*********************************************************************
3831 * _flsbuf (MSVCRT.@)
3833 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3835 /* Flush output buffer */
3836 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3837 msvcrt_alloc_buffer(file);
3840 if(!(file->_flag & MSVCRT__IOWRT)) {
3841 if(!(file->_flag & MSVCRT__IORW)) {
3842 file->_flag |= MSVCRT__IOERR;
3843 return MSVCRT_EOF;
3845 file->_flag |= MSVCRT__IOWRT;
3847 if(file->_flag & MSVCRT__IOREAD) {
3848 if(!(file->_flag & MSVCRT__IOEOF)) {
3849 file->_flag |= MSVCRT__IOERR;
3850 return MSVCRT_EOF;
3852 file->_cnt = 0;
3853 file->_ptr = file->_base;
3854 file->_flag &= ~(MSVCRT__IOREAD | MSVCRT__IOEOF);
3857 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3858 int res = 0;
3860 if(file->_cnt <= 0) {
3861 res = msvcrt_flush_buffer(file);
3862 if(res)
3863 return res;
3864 file->_flag |= MSVCRT__IOWRT;
3865 file->_cnt=file->_bufsiz;
3867 *file->_ptr++ = c;
3868 file->_cnt--;
3869 return c&0xff;
3870 } else {
3871 unsigned char cc=c;
3872 int len;
3873 /* set _cnt to 0 for unbuffered FILEs */
3874 file->_cnt = 0;
3875 len = MSVCRT__write(file->_file, &cc, 1);
3876 if (len == 1)
3877 return c & 0xff;
3878 file->_flag |= MSVCRT__IOERR;
3879 return MSVCRT_EOF;
3883 /*********************************************************************
3884 * fwrite (MSVCRT.@)
3886 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3888 MSVCRT_size_t ret;
3890 MSVCRT__lock_file(file);
3891 ret = MSVCRT__fwrite_nolock(ptr, size, nmemb, file);
3892 MSVCRT__unlock_file(file);
3894 return ret;
3897 /*********************************************************************
3898 * _fwrite_nolock (MSVCRT.@)
3900 MSVCRT_size_t CDECL MSVCRT__fwrite_nolock(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3902 MSVCRT_size_t wrcnt=size * nmemb;
3903 int written = 0;
3904 if (size == 0)
3905 return 0;
3907 while(wrcnt) {
3908 if(file->_cnt < 0) {
3909 WARN("negative file->_cnt value in %p\n", file);
3910 file->_flag |= MSVCRT__IOERR;
3911 break;
3912 } else if(file->_cnt) {
3913 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3914 memcpy(file->_ptr, ptr, pcnt);
3915 file->_cnt -= pcnt;
3916 file->_ptr += pcnt;
3917 written += pcnt;
3918 wrcnt -= pcnt;
3919 ptr = (const char*)ptr + pcnt;
3920 } else if((file->_flag & MSVCRT__IONBF)
3921 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
3922 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
3923 MSVCRT_size_t pcnt;
3924 int bufsiz;
3926 if(file->_flag & MSVCRT__IONBF)
3927 bufsiz = 1;
3928 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3929 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
3930 else
3931 bufsiz = file->_bufsiz;
3933 pcnt = (wrcnt / bufsiz) * bufsiz;
3935 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
3936 break;
3938 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
3939 file->_flag |= MSVCRT__IOERR;
3940 break;
3942 written += pcnt;
3943 wrcnt -= pcnt;
3944 ptr = (const char*)ptr + pcnt;
3945 } else {
3946 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3947 break;
3948 written++;
3949 wrcnt--;
3950 ptr = (const char*)ptr + 1;
3954 return written / size;
3957 /*********************************************************************
3958 * fputwc (MSVCRT.@)
3960 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3962 MSVCRT_wint_t ret;
3964 MSVCRT__lock_file(file);
3965 ret = MSVCRT__fputwc_nolock(wc, file);
3966 MSVCRT__unlock_file(file);
3968 return ret;
3971 /*********************************************************************
3972 * _fputwc_nolock (MSVCRT.@)
3974 MSVCRT_wint_t CDECL MSVCRT__fputwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3976 MSVCRT_wchar_t mwc=wc;
3977 ioinfo *fdinfo;
3978 MSVCRT_wint_t ret;
3980 fdinfo = get_ioinfo_nolock(file->_file);
3982 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3983 char buf[MSVCRT_MB_LEN_MAX];
3984 int char_len;
3986 char_len = MSVCRT_wctomb(buf, mwc);
3987 if(char_len!=-1 && MSVCRT__fwrite_nolock(buf, char_len, 1, file)==1)
3988 ret = wc;
3989 else
3990 ret = MSVCRT_WEOF;
3991 }else if(MSVCRT__fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
3992 ret = wc;
3993 }else {
3994 ret = MSVCRT_WEOF;
3997 return ret;
4000 /*********************************************************************
4001 * _fputwchar (MSVCRT.@)
4003 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
4005 return MSVCRT_fputwc(wc, MSVCRT_stdout);
4008 /*********************************************************************
4009 * _wfsopen (MSVCRT.@)
4011 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
4013 MSVCRT_FILE* file;
4014 int open_flags, stream_flags, fd;
4016 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
4018 /* map mode string to open() flags. "man fopen" for possibilities. */
4019 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4020 return NULL;
4022 LOCK_FILES();
4023 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4024 if (fd < 0)
4025 file = NULL;
4026 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
4027 != -1)
4028 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
4029 else if (file)
4031 file->_flag = 0;
4032 file = NULL;
4035 TRACE(":got (%p)\n",file);
4036 if (fd >= 0 && !file)
4037 MSVCRT__close(fd);
4038 UNLOCK_FILES();
4039 return file;
4042 /*********************************************************************
4043 * _fsopen (MSVCRT.@)
4045 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
4047 MSVCRT_FILE *ret;
4048 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4050 if (path && !(pathW = msvcrt_wstrdupa(path))) {
4051 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4052 *MSVCRT__errno() = MSVCRT_EINVAL;
4053 return NULL;
4055 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4057 MSVCRT_free(pathW);
4058 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4059 *MSVCRT__errno() = MSVCRT_EINVAL;
4060 return NULL;
4063 ret = MSVCRT__wfsopen(pathW, modeW, share);
4065 MSVCRT_free(pathW);
4066 MSVCRT_free(modeW);
4067 return ret;
4070 /*********************************************************************
4071 * fopen (MSVCRT.@)
4073 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
4075 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
4078 /*********************************************************************
4079 * fopen_s (MSVCRT.@)
4081 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
4082 const char *filename, const char *mode)
4084 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4085 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4086 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4088 *pFile = MSVCRT_fopen(filename, mode);
4090 if(!*pFile)
4091 return *MSVCRT__errno();
4092 return 0;
4095 /*********************************************************************
4096 * _wfopen (MSVCRT.@)
4098 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
4100 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
4103 /*********************************************************************
4104 * _wfopen_s (MSVCRT.@)
4106 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
4107 const MSVCRT_wchar_t *mode)
4109 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4110 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4111 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4113 *pFile = MSVCRT__wfopen(filename, mode);
4115 if(!*pFile)
4116 return *MSVCRT__errno();
4117 return 0;
4120 /*********************************************************************
4121 * fputc (MSVCRT.@)
4123 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
4125 int ret;
4127 MSVCRT__lock_file(file);
4128 ret = MSVCRT__fputc_nolock(c, file);
4129 MSVCRT__unlock_file(file);
4131 return ret;
4134 /*********************************************************************
4135 * _fputc_nolock (MSVCRT.@)
4137 int CDECL MSVCRT__fputc_nolock(int c, MSVCRT_FILE* file)
4139 int res;
4141 if(file->_cnt>0) {
4142 *file->_ptr++=c;
4143 file->_cnt--;
4144 if (c == '\n')
4146 res = msvcrt_flush_buffer(file);
4147 return res ? res : c;
4149 else {
4150 return c & 0xff;
4152 } else {
4153 res = MSVCRT__flsbuf(c, file);
4154 return res;
4158 /*********************************************************************
4159 * _fputchar (MSVCRT.@)
4161 int CDECL MSVCRT__fputchar(int c)
4163 return MSVCRT_fputc(c, MSVCRT_stdout);
4166 /*********************************************************************
4167 * fread (MSVCRT.@)
4169 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4171 MSVCRT_size_t ret;
4173 MSVCRT__lock_file(file);
4174 ret = MSVCRT__fread_nolock(ptr, size, nmemb, file);
4175 MSVCRT__unlock_file(file);
4177 return ret;
4180 /*********************************************************************
4181 * _fread_nolock (MSVCRT.@)
4183 MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4185 MSVCRT_size_t rcnt=size * nmemb;
4186 MSVCRT_size_t read=0;
4187 MSVCRT_size_t pread=0;
4189 if(!rcnt)
4190 return 0;
4192 /* first buffered data */
4193 if(file->_cnt>0) {
4194 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4195 memcpy(ptr, file->_ptr, pcnt);
4196 file->_cnt -= pcnt;
4197 file->_ptr += pcnt;
4198 read += pcnt ;
4199 rcnt -= pcnt ;
4200 ptr = (char*)ptr + pcnt;
4201 } else if(!(file->_flag & MSVCRT__IOREAD )) {
4202 if(file->_flag & MSVCRT__IORW) {
4203 file->_flag |= MSVCRT__IOREAD;
4204 } else {
4205 return 0;
4209 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4210 msvcrt_alloc_buffer(file);
4212 while(rcnt>0)
4214 int i;
4215 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
4216 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
4217 file->_ptr = file->_base;
4218 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
4219 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4220 if (i > 0 && i < file->_cnt) {
4221 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4222 file->_flag &= ~MSVCRT__IOEOF;
4224 if (i > 0) {
4225 memcpy(ptr, file->_ptr, i);
4226 file->_cnt -= i;
4227 file->_ptr += i;
4229 } else if (rcnt > INT_MAX) {
4230 i = MSVCRT__read(file->_file, ptr, INT_MAX);
4231 } else if (rcnt < MSVCRT_BUFSIZ) {
4232 i = MSVCRT__read(file->_file, ptr, rcnt);
4233 } else {
4234 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
4236 pread += i;
4237 rcnt -= i;
4238 ptr = (char *)ptr+i;
4239 /* expose feof condition in the flags
4240 * MFC tests file->_flag for feof, and doesn't call feof())
4242 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4243 file->_flag |= MSVCRT__IOEOF;
4244 else if (i == -1)
4246 file->_flag |= MSVCRT__IOERR;
4247 pread = 0;
4248 rcnt = 0;
4250 if (i < 1) break;
4252 read+=pread;
4253 return read / size;
4257 /*********************************************************************
4258 * fread_s (MSVCR80.@)
4260 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4261 MSVCRT_size_t count, MSVCRT_FILE *stream)
4263 MSVCRT_size_t ret;
4265 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4266 if(buf && buf_size)
4267 memset(buf, 0, buf_size);
4268 return 0;
4270 if(!elem_size || !count) return 0;
4272 MSVCRT__lock_file(stream);
4273 ret = MSVCRT__fread_nolock_s(buf, buf_size, elem_size, count, stream);
4274 MSVCRT__unlock_file(stream);
4276 return ret;
4279 /*********************************************************************
4280 * _fread_nolock_s (MSVCR80.@)
4282 MSVCRT_size_t CDECL MSVCRT__fread_nolock_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4283 MSVCRT_size_t count, MSVCRT_FILE *stream)
4285 size_t bytes_left, buf_pos;
4287 TRACE("(%p %lu %lu %lu %p)\n", buf, buf_size, elem_size, count, stream);
4289 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4290 if(buf && buf_size)
4291 memset(buf, 0, buf_size);
4292 return 0;
4294 if(!elem_size || !count) return 0;
4295 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4296 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4298 bytes_left = elem_size*count;
4299 buf_pos = 0;
4300 while(bytes_left) {
4301 if(stream->_cnt > 0) {
4302 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4304 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4305 memset(buf, 0, buf_size);
4306 return 0;
4309 MSVCRT__fread_nolock((char*)buf+buf_pos, 1, size, stream);
4310 buf_pos += size;
4311 bytes_left -= size;
4312 }else {
4313 int c = MSVCRT__filbuf(stream);
4315 if(c == MSVCRT_EOF)
4316 break;
4318 if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, MSVCRT_ERANGE)) {
4319 memset(buf, 0, buf_size);
4320 return 0;
4323 ((char*)buf)[buf_pos++] = c;
4324 bytes_left--;
4328 return buf_pos/elem_size;
4331 /*********************************************************************
4332 * _wfreopen (MSVCRT.@)
4335 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4337 int open_flags, stream_flags, fd;
4339 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4341 LOCK_FILES();
4342 if (!file || ((fd = file->_file) < 0))
4343 file = NULL;
4344 else
4346 MSVCRT_fclose(file);
4347 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4348 file = NULL;
4349 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4350 file = NULL;
4351 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4353 file->_flag = 0;
4354 file = NULL;
4357 UNLOCK_FILES();
4358 return file;
4361 /*********************************************************************
4362 * _wfreopen_s (MSVCRT.@)
4364 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4365 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4367 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4368 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4369 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4370 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4372 *pFile = MSVCRT__wfreopen(path, mode, file);
4374 if(!*pFile)
4375 return *MSVCRT__errno();
4376 return 0;
4379 /*********************************************************************
4380 * freopen (MSVCRT.@)
4383 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4385 MSVCRT_FILE *ret;
4386 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4388 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4389 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4391 MSVCRT_free(pathW);
4392 return NULL;
4395 ret = MSVCRT__wfreopen(pathW, modeW, file);
4397 MSVCRT_free(pathW);
4398 MSVCRT_free(modeW);
4399 return ret;
4402 /*********************************************************************
4403 * freopen_s (MSVCRT.@)
4405 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4406 const char *path, const char *mode, MSVCRT_FILE* file)
4408 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4409 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4410 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4411 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4413 *pFile = MSVCRT_freopen(path, mode, file);
4415 if(!*pFile)
4416 return *MSVCRT__errno();
4417 return 0;
4420 /*********************************************************************
4421 * fsetpos (MSVCRT.@)
4423 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4425 int ret;
4427 MSVCRT__lock_file(file);
4428 msvcrt_flush_buffer(file);
4430 /* Reset direction of i/o */
4431 if(file->_flag & MSVCRT__IORW) {
4432 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4435 ret = (MSVCRT__lseeki64(file->_file,*pos,MSVCRT_SEEK_SET) == -1) ? -1 : 0;
4436 MSVCRT__unlock_file(file);
4437 return ret;
4440 /*********************************************************************
4441 * _ftelli64 (MSVCRT.@)
4443 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4445 __int64 ret;
4447 MSVCRT__lock_file(file);
4448 ret = MSVCRT__ftelli64_nolock(file);
4449 MSVCRT__unlock_file(file);
4451 return ret;
4454 /*********************************************************************
4455 * _ftelli64_nolock (MSVCRT.@)
4457 __int64 CDECL MSVCRT__ftelli64_nolock(MSVCRT_FILE* file)
4459 __int64 pos;
4461 pos = _telli64(file->_file);
4462 if(pos == -1)
4463 return -1;
4464 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4465 if(file->_flag & MSVCRT__IOWRT) {
4466 pos += file->_ptr - file->_base;
4468 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4469 char *p;
4471 for(p=file->_base; p<file->_ptr; p++)
4472 if(*p == '\n')
4473 pos++;
4475 } else if(!file->_cnt) { /* nothing to do */
4476 } else if(MSVCRT__lseeki64(file->_file, 0, MSVCRT_SEEK_END)==pos) {
4477 int i;
4479 pos -= file->_cnt;
4480 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4481 for(i=0; i<file->_cnt; i++)
4482 if(file->_ptr[i] == '\n')
4483 pos--;
4485 } else {
4486 char *p;
4488 if(MSVCRT__lseeki64(file->_file, pos, MSVCRT_SEEK_SET) != pos)
4489 return -1;
4491 pos -= file->_bufsiz;
4492 pos += file->_ptr - file->_base;
4494 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4495 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4496 pos--;
4498 for(p=file->_base; p<file->_ptr; p++)
4499 if(*p == '\n')
4500 pos++;
4505 return pos;
4508 /*********************************************************************
4509 * ftell (MSVCRT.@)
4511 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4513 return MSVCRT__ftelli64(file);
4516 /*********************************************************************
4517 * _ftell_nolock (MSVCRT.@)
4519 LONG CDECL MSVCRT__ftell_nolock(MSVCRT_FILE* file)
4521 return MSVCRT__ftelli64_nolock(file);
4524 /*********************************************************************
4525 * fgetpos (MSVCRT.@)
4527 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4529 *pos = MSVCRT__ftelli64(file);
4530 if(*pos == -1)
4531 return -1;
4532 return 0;
4535 /*********************************************************************
4536 * fputs (MSVCRT.@)
4538 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4540 MSVCRT_size_t len = strlen(s);
4541 int ret;
4543 MSVCRT__lock_file(file);
4544 ret = MSVCRT__fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4545 MSVCRT__unlock_file(file);
4546 return ret;
4549 /*********************************************************************
4550 * fputws (MSVCRT.@)
4552 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4554 MSVCRT_size_t i, len = strlenW(s);
4555 BOOL tmp_buf;
4556 int ret;
4558 MSVCRT__lock_file(file);
4559 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4560 ret = MSVCRT__fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4561 MSVCRT__unlock_file(file);
4562 return ret;
4565 tmp_buf = add_std_buffer(file);
4566 for (i=0; i<len; i++) {
4567 if(MSVCRT__fputwc_nolock(s[i], file) == MSVCRT_WEOF) {
4568 if(tmp_buf) remove_std_buffer(file);
4569 MSVCRT__unlock_file(file);
4570 return MSVCRT_WEOF;
4574 if(tmp_buf) remove_std_buffer(file);
4575 MSVCRT__unlock_file(file);
4576 return 0;
4579 /*********************************************************************
4580 * getchar (MSVCRT.@)
4582 int CDECL MSVCRT_getchar(void)
4584 return MSVCRT_fgetc(MSVCRT_stdin);
4587 /*********************************************************************
4588 * getc (MSVCRT.@)
4590 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4592 return MSVCRT_fgetc(file);
4595 /*********************************************************************
4596 * gets (MSVCRT.@)
4598 char * CDECL MSVCRT_gets(char *buf)
4600 int cc;
4601 char * buf_start = buf;
4603 MSVCRT__lock_file(MSVCRT_stdin);
4604 for(cc = MSVCRT__fgetc_nolock(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4605 cc = MSVCRT__fgetc_nolock(MSVCRT_stdin))
4606 if(cc != '\r') *buf++ = (char)cc;
4608 *buf = '\0';
4610 TRACE("got '%s'\n", buf_start);
4611 MSVCRT__unlock_file(MSVCRT_stdin);
4612 return buf_start;
4615 /*********************************************************************
4616 * _getws (MSVCRT.@)
4618 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4620 MSVCRT_wint_t cc;
4621 MSVCRT_wchar_t* ws = buf;
4623 MSVCRT__lock_file(MSVCRT_stdin);
4624 for (cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4625 cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin))
4627 if (cc != '\r')
4628 *buf++ = (MSVCRT_wchar_t)cc;
4630 *buf = '\0';
4632 TRACE("got %s\n", debugstr_w(ws));
4633 MSVCRT__unlock_file(MSVCRT_stdin);
4634 return ws;
4637 /*********************************************************************
4638 * putc (MSVCRT.@)
4640 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4642 return MSVCRT_fputc(c, file);
4645 /*********************************************************************
4646 * putchar (MSVCRT.@)
4648 int CDECL MSVCRT_putchar(int c)
4650 return MSVCRT_fputc(c, MSVCRT_stdout);
4653 /*********************************************************************
4654 * puts (MSVCRT.@)
4656 int CDECL MSVCRT_puts(const char *s)
4658 MSVCRT_size_t len = strlen(s);
4659 int ret;
4661 MSVCRT__lock_file(MSVCRT_stdout);
4662 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4663 MSVCRT__unlock_file(MSVCRT_stdout);
4664 return MSVCRT_EOF;
4667 ret = MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4668 MSVCRT__unlock_file(MSVCRT_stdout);
4669 return ret;
4672 /*********************************************************************
4673 * _putws (MSVCRT.@)
4675 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4677 static const MSVCRT_wchar_t nl = '\n';
4678 MSVCRT_size_t len = strlenW(s);
4679 int ret;
4681 MSVCRT__lock_file(MSVCRT_stdout);
4682 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4683 MSVCRT__unlock_file(MSVCRT_stdout);
4684 return MSVCRT_EOF;
4687 ret = MSVCRT__fwrite_nolock(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4688 MSVCRT__unlock_file(MSVCRT_stdout);
4689 return ret;
4692 /*********************************************************************
4693 * remove (MSVCRT.@)
4695 int CDECL MSVCRT_remove(const char *path)
4697 TRACE("(%s)\n",path);
4698 if (DeleteFileA(path))
4699 return 0;
4700 TRACE(":failed (%d)\n",GetLastError());
4701 msvcrt_set_errno(GetLastError());
4702 return -1;
4705 /*********************************************************************
4706 * _wremove (MSVCRT.@)
4708 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4710 TRACE("(%s)\n",debugstr_w(path));
4711 if (DeleteFileW(path))
4712 return 0;
4713 TRACE(":failed (%d)\n",GetLastError());
4714 msvcrt_set_errno(GetLastError());
4715 return -1;
4718 /*********************************************************************
4719 * rename (MSVCRT.@)
4721 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4723 TRACE(":from %s to %s\n",oldpath,newpath);
4724 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4725 return 0;
4726 TRACE(":failed (%d)\n",GetLastError());
4727 msvcrt_set_errno(GetLastError());
4728 return -1;
4731 /*********************************************************************
4732 * _wrename (MSVCRT.@)
4734 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4736 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4737 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4738 return 0;
4739 TRACE(":failed (%d)\n",GetLastError());
4740 msvcrt_set_errno(GetLastError());
4741 return -1;
4744 /*********************************************************************
4745 * setvbuf (MSVCRT.@)
4747 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4749 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4750 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4751 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4753 MSVCRT__lock_file(file);
4755 MSVCRT__fflush_nolock(file);
4756 if(file->_flag & MSVCRT__IOMYBUF)
4757 MSVCRT_free(file->_base);
4758 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4759 file->_cnt = 0;
4761 if(mode == MSVCRT__IONBF) {
4762 file->_flag |= MSVCRT__IONBF;
4763 file->_base = file->_ptr = (char*)&file->_charbuf;
4764 file->_bufsiz = 2;
4765 }else if(buf) {
4766 file->_base = file->_ptr = buf;
4767 file->_flag |= MSVCRT__USERBUF;
4768 file->_bufsiz = size;
4769 }else {
4770 file->_base = file->_ptr = MSVCRT_malloc(size);
4771 if(!file->_base) {
4772 file->_bufsiz = 0;
4773 MSVCRT__unlock_file(file);
4774 return -1;
4777 file->_flag |= MSVCRT__IOMYBUF;
4778 file->_bufsiz = size;
4780 MSVCRT__unlock_file(file);
4781 return 0;
4784 /*********************************************************************
4785 * setbuf (MSVCRT.@)
4787 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4789 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4792 static int tmpnam_helper(char *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
4794 char tmpstr[8];
4795 char *p = s;
4796 int digits;
4798 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
4800 if (size < 3) {
4801 if (size) *s = 0;
4802 *MSVCRT__errno() = MSVCRT_ERANGE;
4803 return MSVCRT_ERANGE;
4805 *p++ = '\\';
4806 *p++ = 's';
4807 size -= 2;
4808 digits = msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4809 if (digits+1 > size) {
4810 *s = 0;
4811 *MSVCRT__errno() = MSVCRT_ERANGE;
4812 return MSVCRT_ERANGE;
4814 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4815 p += digits;
4816 *p++ = '.';
4817 size -= digits+1;
4819 while(1) {
4820 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4821 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4822 break;
4825 digits = msvcrt_int_to_base32(digits, tmpstr);
4826 if (digits+1 > size) {
4827 *s = 0;
4828 *MSVCRT__errno() = MSVCRT_ERANGE;
4829 return MSVCRT_ERANGE;
4831 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4832 p[digits] = 0;
4834 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4835 GetLastError() == ERROR_FILE_NOT_FOUND)
4836 break;
4838 return 0;
4841 int CDECL MSVCRT_tmpnam_s(char *s, MSVCRT_size_t size)
4843 return tmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
4846 /*********************************************************************
4847 * tmpnam (MSVCRT.@)
4849 char * CDECL MSVCRT_tmpnam(char *s)
4851 if (!s) {
4852 thread_data_t *data = msvcrt_get_thread_data();
4854 if(!data->tmpnam_buffer)
4855 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4857 s = data->tmpnam_buffer;
4860 return tmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
4863 static int wtmpnam_helper(MSVCRT_wchar_t *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
4865 MSVCRT_wchar_t tmpstr[8];
4866 MSVCRT_wchar_t *p = s;
4867 int digits;
4869 if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL;
4871 if (size < 3) {
4872 if (size) *s = 0;
4873 *MSVCRT__errno() = MSVCRT_ERANGE;
4874 return MSVCRT_ERANGE;
4876 *p++ = '\\';
4877 *p++ = 's';
4878 size -= 2;
4879 digits = msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4880 if (digits+1 > size) {
4881 *s = 0;
4882 *MSVCRT__errno() = MSVCRT_ERANGE;
4883 return MSVCRT_ERANGE;
4885 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4886 p += digits;
4887 *p++ = '.';
4888 size -= digits+1;
4890 while(1) {
4891 while ((digits = *tmpnam_unique)+1 < tmp_max) {
4892 if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
4893 break;
4896 digits = msvcrt_int_to_base32_w(digits, tmpstr);
4897 if (digits+1 > size) {
4898 *s = 0;
4899 *MSVCRT__errno() = MSVCRT_ERANGE;
4900 return MSVCRT_ERANGE;
4902 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
4903 p[digits] = 0;
4905 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4906 GetLastError() == ERROR_FILE_NOT_FOUND)
4907 break;
4909 return 0;
4912 /*********************************************************************
4913 * _wtmpnam_s (MSVCRT.@)
4915 int CDECL MSVCRT__wtmpnam_s(MSVCRT_wchar_t *s, MSVCRT_size_t size)
4917 return wtmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
4920 /*********************************************************************
4921 * _wtmpnam (MSVCRT.@)
4923 MSVCRT_wchar_t * CDECL MSVCRT__wtmpnam(MSVCRT_wchar_t *s)
4925 if (!s) {
4926 thread_data_t *data = msvcrt_get_thread_data();
4928 if(!data->wtmpnam_buffer)
4929 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4931 s = data->wtmpnam_buffer;
4934 return wtmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
4937 /*********************************************************************
4938 * tmpfile (MSVCRT.@)
4940 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4942 char *filename = MSVCRT_tmpnam(NULL);
4943 int fd;
4944 MSVCRT_FILE* file = NULL;
4946 LOCK_FILES();
4947 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4948 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4949 if (fd != -1 && (file = msvcrt_alloc_fp()))
4951 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4953 file->_flag = 0;
4954 file = NULL;
4956 else file->_tmpfname = MSVCRT__strdup(filename);
4959 if(fd != -1 && !file)
4960 MSVCRT__close(fd);
4961 UNLOCK_FILES();
4962 return file;
4965 /*********************************************************************
4966 * tmpfile_s (MSVCRT.@)
4968 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4970 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4972 *file = MSVCRT_tmpfile();
4973 return 0;
4976 static int puts_clbk_file_a(void *file, int len, const char *str)
4978 return MSVCRT_fwrite(str, sizeof(char), len, file);
4981 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4983 int i, ret;
4985 MSVCRT__lock_file(file);
4987 if(!(get_ioinfo_nolock(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4988 ret = MSVCRT__fwrite_nolock(str, sizeof(MSVCRT_wchar_t), len, file);
4989 MSVCRT__unlock_file(file);
4990 return ret;
4993 for(i=0; i<len; i++) {
4994 if(MSVCRT__fputwc_nolock(str[i], file) == MSVCRT_WEOF) {
4995 MSVCRT__unlock_file(file);
4996 return -1;
5000 MSVCRT__unlock_file(file);
5001 return len;
5004 /*********************************************************************
5005 * vfprintf (MSVCRT.@)
5007 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5009 BOOL tmp_buf;
5010 int ret;
5012 MSVCRT__lock_file(file);
5013 tmp_buf = add_std_buffer(file);
5014 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, 0, arg_clbk_valist, NULL, &valist);
5015 if(tmp_buf) remove_std_buffer(file);
5016 MSVCRT__unlock_file(file);
5018 return ret;
5021 /*********************************************************************
5022 * vfprintf_s (MSVCRT.@)
5024 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
5026 BOOL tmp_buf;
5027 int ret;
5029 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
5031 MSVCRT__lock_file(file);
5032 tmp_buf = add_std_buffer(file);
5033 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL,
5034 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, arg_clbk_valist, NULL, &valist);
5035 if(tmp_buf) remove_std_buffer(file);
5036 MSVCRT__unlock_file(file);
5038 return ret;
5041 /*********************************************************************
5042 * vfwprintf (MSVCRT.@)
5044 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5046 BOOL tmp_buf;
5047 int ret;
5049 MSVCRT__lock_file(file);
5050 tmp_buf = add_std_buffer(file);
5051 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, 0, arg_clbk_valist, NULL, &valist);
5052 if(tmp_buf) remove_std_buffer(file);
5053 MSVCRT__unlock_file(file);
5055 return ret;
5058 /*********************************************************************
5059 * vfwprintf_s (MSVCRT.@)
5061 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
5063 BOOL tmp_buf;
5064 int ret;
5066 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5068 MSVCRT__lock_file(file);
5069 tmp_buf = add_std_buffer(file);
5070 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL,
5071 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, arg_clbk_valist, NULL, &valist);
5072 if(tmp_buf) remove_std_buffer(file);
5073 MSVCRT__unlock_file(file);
5075 return ret;
5078 /*********************************************************************
5079 * __stdio_common_vfprintf (MSVCRT.@)
5081 int CDECL MSVCRT__stdio_common_vfprintf(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
5082 MSVCRT__locale_t locale, __ms_va_list valist)
5084 BOOL tmp_buf;
5085 int ret;
5087 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5089 MSVCRT__lock_file(file);
5090 tmp_buf = add_std_buffer(file);
5092 if (options & ~UCRTBASE_PRINTF_MASK)
5093 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5094 ret = pf_printf_a(puts_clbk_file_a, file, format, locale,
5095 options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
5096 if(tmp_buf) remove_std_buffer(file);
5097 MSVCRT__unlock_file(file);
5099 return ret;
5102 /*********************************************************************
5103 * __stdio_common_vfwprintf (MSVCRT.@)
5105 int CDECL MSVCRT__stdio_common_vfwprintf(unsigned __int64 options, MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
5106 MSVCRT__locale_t locale, __ms_va_list valist)
5108 BOOL tmp_buf;
5109 int ret;
5111 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5112 if (!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5114 MSVCRT__lock_file(file);
5115 tmp_buf = add_std_buffer(file);
5117 if (options & ~UCRTBASE_PRINTF_MASK)
5118 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5120 ret = pf_printf_w(puts_clbk_file_w, file, format, locale,
5121 options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
5123 if(tmp_buf) remove_std_buffer(file);
5124 MSVCRT__unlock_file(file);
5126 return ret;
5130 /*********************************************************************
5131 * _vfwprintf_l (MSVCRT.@)
5133 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5134 MSVCRT__locale_t locale, __ms_va_list valist)
5136 BOOL tmp_buf;
5137 int ret;
5139 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5141 MSVCRT__lock_file(file);
5142 tmp_buf = add_std_buffer(file);
5143 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, 0, arg_clbk_valist, NULL, &valist);
5144 if(tmp_buf) remove_std_buffer(file);
5145 MSVCRT__unlock_file(file);
5147 return ret;
5150 /*********************************************************************
5151 * vprintf (MSVCRT.@)
5153 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
5155 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
5158 /*********************************************************************
5159 * vprintf_s (MSVCRT.@)
5161 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
5163 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
5166 /*********************************************************************
5167 * vwprintf (MSVCRT.@)
5169 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
5171 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
5174 /*********************************************************************
5175 * vwprintf_s (MSVCRT.@)
5177 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
5179 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
5182 /*********************************************************************
5183 * fprintf (MSVCRT.@)
5185 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
5187 __ms_va_list valist;
5188 int res;
5189 __ms_va_start(valist, format);
5190 res = MSVCRT_vfprintf(file, format, valist);
5191 __ms_va_end(valist);
5192 return res;
5195 /*********************************************************************
5196 * fprintf_s (MSVCRT.@)
5198 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
5200 __ms_va_list valist;
5201 int res;
5202 __ms_va_start(valist, format);
5203 res = MSVCRT_vfprintf_s(file, format, valist);
5204 __ms_va_end(valist);
5205 return res;
5208 /*********************************************************************
5209 * fwprintf (MSVCRT.@)
5211 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5213 __ms_va_list valist;
5214 int res;
5215 __ms_va_start(valist, format);
5216 res = MSVCRT_vfwprintf(file, format, valist);
5217 __ms_va_end(valist);
5218 return res;
5221 /*********************************************************************
5222 * fwprintf_s (MSVCRT.@)
5224 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5226 __ms_va_list valist;
5227 int res;
5228 __ms_va_start(valist, format);
5229 res = MSVCRT_vfwprintf_s(file, format, valist);
5230 __ms_va_end(valist);
5231 return res;
5234 /*********************************************************************
5235 * _fwprintf_l (MSVCRT.@)
5237 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
5239 __ms_va_list valist;
5240 int res;
5241 __ms_va_start(valist, locale);
5242 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
5243 __ms_va_end(valist);
5244 return res;
5247 /*********************************************************************
5248 * printf (MSVCRT.@)
5250 int CDECL MSVCRT_printf(const char *format, ...)
5252 __ms_va_list valist;
5253 int res;
5254 __ms_va_start(valist, format);
5255 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
5256 __ms_va_end(valist);
5257 return res;
5260 /*********************************************************************
5261 * printf_s (MSVCRT.@)
5263 int CDECL MSVCRT_printf_s(const char *format, ...)
5265 __ms_va_list valist;
5266 int res;
5267 __ms_va_start(valist, format);
5268 res = MSVCRT_vprintf_s(format, valist);
5269 __ms_va_end(valist);
5270 return res;
5273 /*********************************************************************
5274 * ungetc (MSVCRT.@)
5276 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
5278 int ret;
5280 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5282 MSVCRT__lock_file(file);
5283 ret = MSVCRT__ungetc_nolock(c, file);
5284 MSVCRT__unlock_file(file);
5286 return ret;
5289 /*********************************************************************
5290 * _ungetc_nolock (MSVCRT.@)
5292 int CDECL MSVCRT__ungetc_nolock(int c, MSVCRT_FILE * file)
5294 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5296 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
5297 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
5298 return MSVCRT_EOF;
5300 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
5301 && msvcrt_alloc_buffer(file))
5302 || (!file->_cnt && file->_ptr==file->_base))
5303 file->_ptr++;
5305 if(file->_ptr>file->_base) {
5306 file->_ptr--;
5307 if(file->_flag & MSVCRT__IOSTRG) {
5308 if(*file->_ptr != c) {
5309 file->_ptr++;
5310 return MSVCRT_EOF;
5312 }else {
5313 *file->_ptr = c;
5315 file->_cnt++;
5316 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
5317 file->_flag |= MSVCRT__IOREAD;
5318 return c;
5321 return MSVCRT_EOF;
5324 /*********************************************************************
5325 * ungetwc (MSVCRT.@)
5327 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5329 MSVCRT_wint_t ret;
5331 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5333 MSVCRT__lock_file(file);
5334 ret = MSVCRT__ungetwc_nolock(wc, file);
5335 MSVCRT__unlock_file(file);
5337 return ret;
5340 /*********************************************************************
5341 * _ungetwc_nolock (MSVCRT.@)
5343 MSVCRT_wint_t CDECL MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5345 MSVCRT_wchar_t mwc = wc;
5347 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5348 if (wc == MSVCRT_WEOF)
5349 return MSVCRT_WEOF;
5351 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5352 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5353 unsigned char * pp = (unsigned char *)&mwc;
5354 int i;
5356 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
5357 if(pp[i] != MSVCRT__ungetc_nolock(pp[i],file))
5358 return MSVCRT_WEOF;
5360 }else {
5361 char mbs[MSVCRT_MB_LEN_MAX];
5362 int len;
5364 len = MSVCRT_wctomb(mbs, mwc);
5365 if(len == -1)
5366 return MSVCRT_WEOF;
5368 for(len--; len>=0; len--) {
5369 if(mbs[len] != MSVCRT__ungetc_nolock(mbs[len], file))
5370 return MSVCRT_WEOF;
5374 return mwc;
5377 /*********************************************************************
5378 * wprintf (MSVCRT.@)
5380 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
5382 __ms_va_list valist;
5383 int res;
5384 __ms_va_start(valist, format);
5385 res = MSVCRT_vwprintf(format, valist);
5386 __ms_va_end(valist);
5387 return res;
5390 /*********************************************************************
5391 * wprintf_s (MSVCRT.@)
5393 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
5395 __ms_va_list valist;
5396 int res;
5397 __ms_va_start(valist, format);
5398 res = MSVCRT_vwprintf_s(format, valist);
5399 __ms_va_end(valist);
5400 return res;
5403 /*********************************************************************
5404 * _getmaxstdio (MSVCRT.@)
5406 int CDECL MSVCRT__getmaxstdio(void)
5408 return MSVCRT_max_streams;
5411 /*********************************************************************
5412 * _setmaxstdio (MSVCRT.@)
5414 int CDECL MSVCRT__setmaxstdio(int newmax)
5416 TRACE("%d\n", newmax);
5418 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5419 return -1;
5421 MSVCRT_max_streams = newmax;
5422 return MSVCRT_max_streams;
5425 /*********************************************************************
5426 * _get_stream_buffer_pointers (UCRTBASE.@)
5428 int CDECL MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE *file, char*** base,
5429 char*** ptr, int** count)
5431 if (base)
5432 *base = &file->_base;
5433 if (ptr)
5434 *ptr = &file->_ptr;
5435 if (count)
5436 *count = &file->_cnt;
5437 return 0;