msvcrt: Reset write direction after flush on read/write stream.
[wine/multimedia.git] / dlls / msvcrt / file.c
blob2e473a90ac899512687d6736ecef21ce0ddfc936
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 static int MSVCRT_fdstart = 3; /* first unallocated fd */
118 static int MSVCRT_fdend = 3; /* highest allocated fd */
120 typedef struct {
121 MSVCRT_FILE file;
122 CRITICAL_SECTION crit;
123 } file_crit;
125 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
126 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
127 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
129 /* INTERNAL: process umask */
130 static int MSVCRT_umask = 0;
132 /* INTERNAL: static data for tmpnam and _wtmpname functions */
133 static int tmpnam_unique;
135 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
136 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
137 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
138 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
140 #define TOUL(x) (ULONGLONG)(x)
141 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
142 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
143 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
144 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
146 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
147 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
148 * and MSVCRT_stream_idx, from race conditions.
149 * It doesn't protect against race conditions manipulating the underlying files
150 * or flags; doing so would probably be better accomplished with per-file
151 * protection, rather than locking the whole table for every change.
153 static CRITICAL_SECTION MSVCRT_file_cs;
154 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
156 0, 0, &MSVCRT_file_cs,
157 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
158 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
160 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
161 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
162 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
164 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
166 buf->st_dev = buf64->st_dev;
167 buf->st_ino = buf64->st_ino;
168 buf->st_mode = buf64->st_mode;
169 buf->st_nlink = buf64->st_nlink;
170 buf->st_uid = buf64->st_uid;
171 buf->st_gid = buf64->st_gid;
172 buf->st_rdev = buf64->st_rdev;
173 buf->st_size = buf64->st_size;
174 buf->st_atime = buf64->st_atime;
175 buf->st_mtime = buf64->st_mtime;
176 buf->st_ctime = buf64->st_ctime;
179 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
181 buf->st_dev = buf64->st_dev;
182 buf->st_ino = buf64->st_ino;
183 buf->st_mode = buf64->st_mode;
184 buf->st_nlink = buf64->st_nlink;
185 buf->st_uid = buf64->st_uid;
186 buf->st_gid = buf64->st_gid;
187 buf->st_rdev = buf64->st_rdev;
188 buf->st_size = buf64->st_size;
189 buf->st_atime = buf64->st_atime;
190 buf->st_mtime = buf64->st_mtime;
191 buf->st_ctime = buf64->st_ctime;
194 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32 *buf)
196 buf->st_dev = buf64->st_dev;
197 buf->st_ino = buf64->st_ino;
198 buf->st_mode = buf64->st_mode;
199 buf->st_nlink = buf64->st_nlink;
200 buf->st_uid = buf64->st_uid;
201 buf->st_gid = buf64->st_gid;
202 buf->st_rdev = buf64->st_rdev;
203 buf->st_size = buf64->st_size;
204 buf->st_atime = buf64->st_atime;
205 buf->st_mtime = buf64->st_mtime;
206 buf->st_ctime = buf64->st_ctime;
209 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat64i32 *buf)
211 buf->st_dev = buf64->st_dev;
212 buf->st_ino = buf64->st_ino;
213 buf->st_mode = buf64->st_mode;
214 buf->st_nlink = buf64->st_nlink;
215 buf->st_uid = buf64->st_uid;
216 buf->st_gid = buf64->st_gid;
217 buf->st_rdev = buf64->st_rdev;
218 buf->st_size = buf64->st_size;
219 buf->st_atime = buf64->st_atime;
220 buf->st_mtime = buf64->st_mtime;
221 buf->st_ctime = buf64->st_ctime;
224 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32i64 *buf)
226 buf->st_dev = buf64->st_dev;
227 buf->st_ino = buf64->st_ino;
228 buf->st_mode = buf64->st_mode;
229 buf->st_nlink = buf64->st_nlink;
230 buf->st_uid = buf64->st_uid;
231 buf->st_gid = buf64->st_gid;
232 buf->st_rdev = buf64->st_rdev;
233 buf->st_size = buf64->st_size;
234 buf->st_atime = buf64->st_atime;
235 buf->st_mtime = buf64->st_mtime;
236 buf->st_ctime = buf64->st_ctime;
239 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
241 /* 1601 to 1970 is 369 years plus 89 leap days */
242 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
244 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
245 ft->dwHighDateTime = ticks >> 32;
246 ft->dwLowDateTime = ticks;
249 static inline ioinfo* get_ioinfo_nolock(int fd)
251 ioinfo *ret = NULL;
252 if(fd>=0 && fd<MSVCRT_MAX_FILES)
253 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
254 if(!ret)
255 return &MSVCRT___badioinfo;
257 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
260 static inline ioinfo* get_ioinfo(int fd)
262 ioinfo *ret = get_ioinfo_nolock(fd);
263 if(ret->exflag & EF_CRIT_INIT)
264 EnterCriticalSection(&ret->crit);
265 return ret;
268 static inline void release_ioinfo(ioinfo *info)
270 if(info->exflag & EF_CRIT_INIT)
271 LeaveCriticalSection(&info->crit);
274 static inline MSVCRT_FILE* msvcrt_get_file(int i)
276 file_crit *ret;
278 if(i >= MSVCRT_max_streams)
279 return NULL;
281 if(i < _IOB_ENTRIES)
282 return &MSVCRT__iob[i];
284 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
285 if(!ret) {
286 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
287 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
288 ERR("out of memory\n");
289 *MSVCRT__errno() = MSVCRT_ENOMEM;
290 return NULL;
293 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
294 } else
295 ret += i%MSVCRT_FD_BLOCK_SIZE;
297 return &ret->file;
300 static inline BOOL msvcrt_is_valid_fd(int fd)
302 return fd >= 0 && fd < MSVCRT_fdend && (get_ioinfo_nolock(fd)->wxflag & WX_OPEN);
305 /* INTERNAL: Get the HANDLE for a fd
306 * This doesn't lock the table, because a failure will result in
307 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
308 * it returns a valid handle which is about to be closed, a subsequent call
309 * will fail, most likely in a sane way.
311 static HANDLE msvcrt_fdtoh(int fd)
313 if (!msvcrt_is_valid_fd(fd))
315 WARN(":fd (%d) - no handle!\n",fd);
316 *MSVCRT___doserrno() = 0;
317 *MSVCRT__errno() = MSVCRT_EBADF;
318 return INVALID_HANDLE_VALUE;
320 if (get_ioinfo_nolock(fd)->handle == INVALID_HANDLE_VALUE)
321 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd);
322 return get_ioinfo_nolock(fd)->handle;
325 /* INTERNAL: free a file entry fd */
326 static void msvcrt_free_fd(int fd)
328 ioinfo *fdinfo;
330 LOCK_FILES();
331 fdinfo = get_ioinfo_nolock(fd);
332 if(fdinfo != &MSVCRT___badioinfo)
334 fdinfo->handle = INVALID_HANDLE_VALUE;
335 fdinfo->wxflag = 0;
337 TRACE(":fd (%d) freed\n",fd);
339 if (fd < 3)
341 switch (fd)
343 case 0:
344 SetStdHandle(STD_INPUT_HANDLE, 0);
345 break;
346 case 1:
347 SetStdHandle(STD_OUTPUT_HANDLE, 0);
348 break;
349 case 2:
350 SetStdHandle(STD_ERROR_HANDLE, 0);
351 break;
355 if (fd == MSVCRT_fdend - 1)
356 MSVCRT_fdend--;
357 if (fd < MSVCRT_fdstart)
358 MSVCRT_fdstart = fd;
359 UNLOCK_FILES();
362 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
363 /* caller must hold the files lock */
364 static int msvcrt_set_fd(HANDLE hand, int flag, int fd)
366 ioinfo *fdinfo;
368 if (fd >= MSVCRT_MAX_FILES)
370 WARN(":files exhausted!\n");
371 *MSVCRT__errno() = MSVCRT_ENFILE;
372 return -1;
375 fdinfo = get_ioinfo_nolock(fd);
376 if(fdinfo == &MSVCRT___badioinfo) {
377 int i;
379 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
380 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
381 WARN(":out of memory!\n");
382 *MSVCRT__errno() = MSVCRT_ENOMEM;
383 return -1;
386 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
387 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
389 fdinfo = get_ioinfo_nolock(fd);
392 fdinfo->handle = hand;
393 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_TTY));
394 fdinfo->lookahead[0] = '\n';
395 fdinfo->lookahead[1] = '\n';
396 fdinfo->lookahead[2] = '\n';
397 if(!(fdinfo->exflag & EF_CRIT_INIT))
398 InitializeCriticalSection(&fdinfo->crit);
399 fdinfo->exflag = EF_CRIT_INIT;
401 /* locate next free slot */
402 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
403 MSVCRT_fdstart = MSVCRT_fdend + 1;
404 else
405 while (MSVCRT_fdstart < MSVCRT_fdend &&
406 get_ioinfo_nolock(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
407 MSVCRT_fdstart++;
408 /* update last fd in use */
409 if (fd >= MSVCRT_fdend)
410 MSVCRT_fdend = fd + 1;
411 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
413 switch (fd)
415 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
416 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
417 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
420 return fd;
423 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
424 static int msvcrt_alloc_fd(HANDLE hand, int flag)
426 int ret;
428 LOCK_FILES();
429 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
430 ret = msvcrt_set_fd(hand, flag, MSVCRT_fdstart);
431 UNLOCK_FILES();
432 return ret;
435 /* INTERNAL: Allocate a FILE* for an fd slot */
436 /* caller must hold the files lock */
437 static MSVCRT_FILE* msvcrt_alloc_fp(void)
439 int i;
440 MSVCRT_FILE *file;
442 for (i = 3; i < MSVCRT_max_streams; i++)
444 file = msvcrt_get_file(i);
445 if (!file)
446 return NULL;
448 if (file->_flag == 0)
450 if (i == MSVCRT_stream_idx)
452 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
454 InitializeCriticalSection(&((file_crit*)file)->crit);
455 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
457 MSVCRT_stream_idx++;
459 return file;
463 return NULL;
466 /* INTERNAL: initialize a FILE* from an open fd */
467 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
469 TRACE(":fd (%d) allocating FILE*\n",fd);
470 if (!msvcrt_is_valid_fd(fd))
472 WARN(":invalid fd %d\n",fd);
473 *MSVCRT___doserrno() = 0;
474 *MSVCRT__errno() = MSVCRT_EBADF;
475 return -1;
477 file->_ptr = file->_base = NULL;
478 file->_cnt = 0;
479 file->_file = fd;
480 file->_flag = stream_flags;
481 file->_tmpfname = NULL;
483 TRACE(":got FILE* (%p)\n",file);
484 return 0;
487 /* INTERNAL: Create an inheritance data block (for spawned process)
488 * The inheritance block is made of:
489 * 00 int nb of file descriptor (NBFD)
490 * 04 char file flags (wxflag): repeated for each fd
491 * 4+NBFD HANDLE file handle: repeated for each fd
493 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
495 int fd;
496 char* wxflag_ptr;
497 HANDLE* handle_ptr;
498 ioinfo* fdinfo;
500 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
501 *block = MSVCRT_calloc(*size, 1);
502 if (!*block)
504 *size = 0;
505 return FALSE;
507 wxflag_ptr = (char*)*block + sizeof(unsigned);
508 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
510 *(unsigned*)*block = MSVCRT_fdend;
511 for (fd = 0; fd < MSVCRT_fdend; fd++)
513 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
514 fdinfo = get_ioinfo_nolock(fd);
515 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
517 *wxflag_ptr = fdinfo->wxflag;
518 *handle_ptr = fdinfo->handle;
520 else
522 *wxflag_ptr = 0;
523 *handle_ptr = INVALID_HANDLE_VALUE;
525 wxflag_ptr++; handle_ptr++;
527 return TRUE;
530 /* INTERNAL: Set up all file descriptors,
531 * as well as default streams (stdin, stderr and stdout)
533 void msvcrt_init_io(void)
535 STARTUPINFOA si;
536 int i;
537 ioinfo *fdinfo;
539 GetStartupInfoA(&si);
540 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
542 BYTE* wxflag_ptr;
543 HANDLE* handle_ptr;
544 unsigned int count;
546 count = *(unsigned*)si.lpReserved2;
547 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
548 handle_ptr = (HANDLE*)(wxflag_ptr + count);
550 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
551 count = min(count, MSVCRT_MAX_FILES);
552 for (i = 0; i < count; i++)
554 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
555 msvcrt_set_fd(*handle_ptr, *wxflag_ptr, i);
557 wxflag_ptr++; handle_ptr++;
559 MSVCRT_fdend = max( 3, count );
560 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
561 if (get_ioinfo_nolock(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
564 fdinfo = get_ioinfo_nolock(MSVCRT_STDIN_FILENO);
565 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
566 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
567 DWORD type = GetFileType(h);
569 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
570 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDIN_FILENO);
573 fdinfo = get_ioinfo_nolock(MSVCRT_STDOUT_FILENO);
574 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
575 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
576 DWORD type = GetFileType(h);
578 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
579 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDOUT_FILENO);
582 fdinfo = get_ioinfo_nolock(MSVCRT_STDERR_FILENO);
583 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
584 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
585 DWORD type = GetFileType(h);
587 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
588 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDERR_FILENO);
591 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO)->handle,
592 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO)->handle,
593 get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle);
595 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
596 for (i = 0; i < 3; i++)
598 /* FILE structs for stdin/out/err are static and never deleted */
599 MSVCRT__iob[i]._file = i;
600 MSVCRT__iob[i]._tmpfname = NULL;
601 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
603 MSVCRT_stream_idx = 3;
606 /* INTERNAL: Flush stdio file buffer */
607 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
609 if((file->_flag & (MSVCRT__IOREAD|MSVCRT__IOWRT)) == MSVCRT__IOWRT &&
610 file->_flag & (MSVCRT__IOMYBUF|MSVCRT__USERBUF)) {
611 int cnt=file->_ptr-file->_base;
612 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
613 file->_flag |= MSVCRT__IOERR;
614 return MSVCRT_EOF;
617 if(file->_flag & MSVCRT__IORW)
618 file->_flag &= ~MSVCRT__IOWRT;
621 file->_ptr=file->_base;
622 file->_cnt=0;
623 return 0;
626 /*********************************************************************
627 * _isatty (MSVCRT.@)
629 int CDECL MSVCRT__isatty(int fd)
631 TRACE(":fd (%d)\n",fd);
633 return get_ioinfo_nolock(fd)->wxflag & WX_TTY;
636 /* INTERNAL: Allocate stdio file buffer */
637 static BOOL msvcrt_alloc_buffer(MSVCRT_FILE* file)
639 if((file->_file==MSVCRT_STDOUT_FILENO || file->_file==MSVCRT_STDERR_FILENO)
640 && MSVCRT__isatty(file->_file))
641 return FALSE;
643 file->_base = MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ,1);
644 if(file->_base) {
645 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
646 file->_flag |= MSVCRT__IOMYBUF;
647 } else {
648 file->_base = (char*)(&file->_charbuf);
649 file->_bufsiz = 2;
650 file->_flag |= MSVCRT__IONBF;
652 file->_ptr = file->_base;
653 file->_cnt = 0;
654 return TRUE;
657 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
658 static BOOL add_std_buffer(MSVCRT_FILE *file)
660 static char buffers[2][MSVCRT_BUFSIZ];
662 if((file->_file!=MSVCRT_STDOUT_FILENO && file->_file!=MSVCRT_STDERR_FILENO)
663 || (file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
664 || !MSVCRT__isatty(file->_file))
665 return FALSE;
667 file->_ptr = file->_base = buffers[file->_file == MSVCRT_STDOUT_FILENO ? 0 : 1];
668 file->_bufsiz = file->_cnt = MSVCRT_BUFSIZ;
669 file->_flag |= MSVCRT__USERBUF;
670 return TRUE;
673 /* INTERNAL: Removes temporary buffer from stdout or stderr */
674 /* Only call this function when add_std_buffer returned TRUE */
675 static void remove_std_buffer(MSVCRT_FILE *file)
677 msvcrt_flush_buffer(file);
678 file->_ptr = file->_base = NULL;
679 file->_bufsiz = file->_cnt = 0;
680 file->_flag &= ~MSVCRT__USERBUF;
683 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
684 static int msvcrt_int_to_base32(int num, char *str)
686 char *p;
687 int n = num;
688 int digits = 0;
690 while (n != 0)
692 n >>= 5;
693 digits++;
695 p = str + digits;
696 *p = 0;
697 while (--p >= str)
699 *p = (num & 31) + '0';
700 if (*p > '9')
701 *p += ('a' - '0' - 10);
702 num >>= 5;
705 return digits;
708 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
709 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
711 MSVCRT_wchar_t *p;
712 int n = num;
713 int digits = 0;
715 while (n != 0)
717 n >>= 5;
718 digits++;
720 p = str + digits;
721 *p = 0;
722 while (--p >= str)
724 *p = (num & 31) + '0';
725 if (*p > '9')
726 *p += ('a' - '0' - 10);
727 num >>= 5;
730 return digits;
733 /*********************************************************************
734 * __iob_func(MSVCRT.@)
736 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
738 return &MSVCRT__iob[0];
741 /*********************************************************************
742 * _access (MSVCRT.@)
744 int CDECL MSVCRT__access(const char *filename, int mode)
746 DWORD attr = GetFileAttributesA(filename);
748 TRACE("(%s,%d) %d\n",filename,mode,attr);
750 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
752 msvcrt_set_errno(GetLastError());
753 return -1;
755 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
757 msvcrt_set_errno(ERROR_ACCESS_DENIED);
758 return -1;
760 return 0;
763 /*********************************************************************
764 * _access_s (MSVCRT.@)
766 int CDECL MSVCRT__access_s(const char *filename, int mode)
768 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
769 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
771 if (MSVCRT__access(filename, mode) == -1)
772 return *MSVCRT__errno();
773 return 0;
776 /*********************************************************************
777 * _waccess (MSVCRT.@)
779 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
781 DWORD attr = GetFileAttributesW(filename);
783 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
785 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
787 msvcrt_set_errno(GetLastError());
788 return -1;
790 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
792 msvcrt_set_errno(ERROR_ACCESS_DENIED);
793 return -1;
795 return 0;
798 /*********************************************************************
799 * _waccess_s (MSVCRT.@)
801 int CDECL MSVCRT__waccess_s(const MSVCRT_wchar_t *filename, int mode)
803 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
804 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
806 if (MSVCRT__waccess(filename, mode) == -1)
807 return *MSVCRT__errno();
808 return 0;
811 /*********************************************************************
812 * _chmod (MSVCRT.@)
814 int CDECL MSVCRT__chmod(const char *path, int flags)
816 DWORD oldFlags = GetFileAttributesA(path);
818 if (oldFlags != INVALID_FILE_ATTRIBUTES)
820 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
821 oldFlags | FILE_ATTRIBUTE_READONLY;
823 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
824 return 0;
826 msvcrt_set_errno(GetLastError());
827 return -1;
830 /*********************************************************************
831 * _wchmod (MSVCRT.@)
833 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
835 DWORD oldFlags = GetFileAttributesW(path);
837 if (oldFlags != INVALID_FILE_ATTRIBUTES)
839 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
840 oldFlags | FILE_ATTRIBUTE_READONLY;
842 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
843 return 0;
845 msvcrt_set_errno(GetLastError());
846 return -1;
849 /*********************************************************************
850 * _unlink (MSVCRT.@)
852 int CDECL MSVCRT__unlink(const char *path)
854 TRACE("%s\n",debugstr_a(path));
855 if(DeleteFileA(path))
856 return 0;
857 TRACE("failed (%d)\n",GetLastError());
858 msvcrt_set_errno(GetLastError());
859 return -1;
862 /*********************************************************************
863 * _wunlink (MSVCRT.@)
865 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
867 TRACE("(%s)\n",debugstr_w(path));
868 if(DeleteFileW(path))
869 return 0;
870 TRACE("failed (%d)\n",GetLastError());
871 msvcrt_set_errno(GetLastError());
872 return -1;
875 /*********************************************************************
876 * _commit (MSVCRT.@)
878 int CDECL MSVCRT__commit(int fd)
880 ioinfo *info = get_ioinfo(fd);
881 int ret;
883 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
885 if (info->handle == INVALID_HANDLE_VALUE)
886 ret = -1;
887 else if (!FlushFileBuffers(info->handle))
889 if (GetLastError() == ERROR_INVALID_HANDLE)
891 /* FlushFileBuffers fails for console handles
892 * so we ignore this error.
894 ret = 0;
896 else
898 TRACE(":failed-last error (%d)\n",GetLastError());
899 msvcrt_set_errno(GetLastError());
900 ret = -1;
903 else
905 TRACE(":ok\n");
906 ret = 0;
909 release_ioinfo(info);
910 return ret;
913 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
914 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
916 /* INTERNAL: Flush all stream buffer */
917 static int msvcrt_flush_all_buffers(int mask)
919 int i, num_flushed = 0;
920 MSVCRT_FILE *file;
922 LOCK_FILES();
923 for (i = 0; i < MSVCRT_stream_idx; i++) {
924 file = msvcrt_get_file(i);
926 if (file->_flag)
928 if(file->_flag & mask) {
929 MSVCRT_fflush(file);
930 num_flushed++;
934 UNLOCK_FILES();
936 TRACE(":flushed (%d) handles\n",num_flushed);
937 return num_flushed;
940 /*********************************************************************
941 * _flushall (MSVCRT.@)
943 int CDECL MSVCRT__flushall(void)
945 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
948 /*********************************************************************
949 * fflush (MSVCRT.@)
951 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
953 int ret;
955 if(!file) {
956 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
957 ret = 0;
958 } else {
959 MSVCRT__lock_file(file);
960 ret = MSVCRT__fflush_nolock(file);
961 MSVCRT__unlock_file(file);
964 return ret;
967 /*********************************************************************
968 * _fflush_nolock (MSVCRT.@)
970 int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file)
972 int res;
974 if(!file) {
975 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
976 return 0;
979 res = msvcrt_flush_buffer(file);
980 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
981 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
982 return res;
985 /*********************************************************************
986 * _close (MSVCRT.@)
988 int CDECL MSVCRT__close(int fd)
990 ioinfo *info = get_ioinfo(fd);
991 int ret;
993 LOCK_FILES();
994 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
995 if (!(info->wxflag & WX_OPEN)) {
996 ret = -1;
997 } else {
998 ret = CloseHandle(info->handle) ? 0 : -1;
999 msvcrt_free_fd(fd);
1000 if (ret) {
1001 WARN(":failed-last error (%d)\n",GetLastError());
1002 msvcrt_set_errno(GetLastError());
1005 UNLOCK_FILES();
1006 release_ioinfo(info);
1007 return ret;
1010 /*********************************************************************
1011 * _dup2 (MSVCRT.@)
1012 * NOTES
1013 * MSDN isn't clear on this point, but the remarks for _pipe
1014 * indicate file descriptors duplicated with _dup and _dup2 are always
1015 * inheritable.
1017 int CDECL MSVCRT__dup2(int od, int nd)
1019 int ret;
1021 TRACE("(od=%d, nd=%d)\n", od, nd);
1022 LOCK_FILES();
1023 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
1025 HANDLE handle;
1027 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od)->handle,
1028 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1030 int wxflag = get_ioinfo_nolock(od)->wxflag & ~MSVCRT__O_NOINHERIT;
1032 if (msvcrt_is_valid_fd(nd))
1033 MSVCRT__close(nd);
1034 ret = msvcrt_set_fd(handle, wxflag, nd);
1035 if (ret == -1)
1037 CloseHandle(handle);
1038 *MSVCRT__errno() = MSVCRT_EMFILE;
1040 else
1042 /* _dup2 returns 0, not nd, on success */
1043 ret = 0;
1046 else
1048 ret = -1;
1049 msvcrt_set_errno(GetLastError());
1052 else
1054 *MSVCRT__errno() = MSVCRT_EBADF;
1055 ret = -1;
1057 UNLOCK_FILES();
1058 return ret;
1061 /*********************************************************************
1062 * _dup (MSVCRT.@)
1064 int CDECL MSVCRT__dup(int od)
1066 int fd, ret;
1068 LOCK_FILES();
1069 fd = MSVCRT_fdstart;
1070 if (MSVCRT__dup2(od, fd) == 0)
1071 ret = fd;
1072 else
1073 ret = -1;
1074 UNLOCK_FILES();
1075 return ret;
1078 /*********************************************************************
1079 * _eof (MSVCRT.@)
1081 int CDECL MSVCRT__eof(int fd)
1083 ioinfo *info = get_ioinfo(fd);
1084 DWORD curpos,endpos;
1085 LONG hcurpos,hendpos;
1087 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1089 if (info->handle == INVALID_HANDLE_VALUE)
1091 release_ioinfo(info);
1092 return -1;
1095 if (info->wxflag & WX_ATEOF)
1097 release_ioinfo(info);
1098 return TRUE;
1101 /* Otherwise we do it the hard way */
1102 hcurpos = hendpos = 0;
1103 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1104 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1106 if (curpos == endpos && hcurpos == hendpos)
1108 /* FIXME: shouldn't WX_ATEOF be set here? */
1109 release_ioinfo(info);
1110 return TRUE;
1113 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1114 release_ioinfo(info);
1115 return FALSE;
1118 /*********************************************************************
1119 * _fcloseall (MSVCRT.@)
1121 int CDECL MSVCRT__fcloseall(void)
1123 int num_closed = 0, i;
1124 MSVCRT_FILE *file;
1126 LOCK_FILES();
1127 for (i = 3; i < MSVCRT_stream_idx; i++) {
1128 file = msvcrt_get_file(i);
1130 if (file->_flag && !MSVCRT_fclose(file))
1131 num_closed++;
1133 UNLOCK_FILES();
1135 TRACE(":closed (%d) handles\n",num_closed);
1136 return num_closed;
1139 /* free everything on process exit */
1140 void msvcrt_free_io(void)
1142 unsigned int i;
1143 int j;
1145 MSVCRT__flushall();
1146 MSVCRT__fcloseall();
1148 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1150 if(!MSVCRT___pioinfo[i])
1151 continue;
1153 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1155 if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
1156 DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
1158 MSVCRT_free(MSVCRT___pioinfo[i]);
1161 for(j=0; j<MSVCRT_stream_idx; j++)
1163 MSVCRT_FILE *file = msvcrt_get_file(j);
1164 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1166 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1167 DeleteCriticalSection(&((file_crit*)file)->crit);
1171 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1172 MSVCRT_free(MSVCRT_fstream[i]);
1174 DeleteCriticalSection(&MSVCRT_file_cs);
1177 /*********************************************************************
1178 * _lseeki64 (MSVCRT.@)
1180 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1182 ioinfo *info = get_ioinfo(fd);
1183 LARGE_INTEGER ofs;
1185 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1187 if (info->handle == INVALID_HANDLE_VALUE)
1189 release_ioinfo(info);
1190 return -1;
1193 if (whence < 0 || whence > 2)
1195 release_ioinfo(info);
1196 *MSVCRT__errno() = MSVCRT_EINVAL;
1197 return -1;
1200 TRACE(":fd (%d) to %s pos %s\n",
1201 fd,wine_dbgstr_longlong(offset),
1202 (whence==SEEK_SET)?"SEEK_SET":
1203 (whence==SEEK_CUR)?"SEEK_CUR":
1204 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1206 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1207 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1208 ofs.QuadPart = offset;
1209 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1210 GetLastError() == ERROR_SUCCESS)
1212 info->wxflag &= ~WX_ATEOF;
1213 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1215 release_ioinfo(info);
1216 return ofs.QuadPart;
1218 release_ioinfo(info);
1219 TRACE(":error-last error (%d)\n",GetLastError());
1220 msvcrt_set_errno(GetLastError());
1221 return -1;
1224 /*********************************************************************
1225 * _lseek (MSVCRT.@)
1227 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1229 return MSVCRT__lseeki64(fd, offset, whence);
1232 /*********************************************************************
1233 * _lock_file (MSVCRT.@)
1235 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1237 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1238 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1239 else
1240 EnterCriticalSection(&((file_crit*)file)->crit);
1243 /*********************************************************************
1244 * _unlock_file (MSVCRT.@)
1246 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1248 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1249 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1250 else
1251 LeaveCriticalSection(&((file_crit*)file)->crit);
1254 /*********************************************************************
1255 * _locking (MSVCRT.@)
1257 * This is untested; the underlying LockFile doesn't work yet.
1259 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1261 ioinfo *info = get_ioinfo(fd);
1262 BOOL ret;
1263 DWORD cur_locn;
1265 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1266 if (info->handle == INVALID_HANDLE_VALUE)
1268 release_ioinfo(info);
1269 return -1;
1272 if (mode < 0 || mode > 4)
1274 release_ioinfo(info);
1275 *MSVCRT__errno() = MSVCRT_EINVAL;
1276 return -1;
1279 TRACE(":fd (%d) by 0x%08x mode %s\n",
1280 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1281 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1282 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1283 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1284 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1285 "UNKNOWN");
1287 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1289 release_ioinfo(info);
1290 FIXME ("Seek failed\n");
1291 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1292 return -1;
1294 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1296 int nretry = 10;
1297 ret = 1; /* just to satisfy gcc */
1298 while (nretry--)
1300 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1301 if (ret) break;
1302 Sleep(1);
1305 else if (mode == MSVCRT__LK_UNLCK)
1306 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1307 else
1308 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1309 /* FIXME - what about error settings? */
1310 release_ioinfo(info);
1311 return ret ? 0 : -1;
1314 /*********************************************************************
1315 * _fseeki64 (MSVCRT.@)
1317 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1319 int ret;
1321 MSVCRT__lock_file(file);
1322 ret = MSVCRT__fseeki64_nolock(file, offset, whence);
1323 MSVCRT__unlock_file(file);
1325 return ret;
1328 /*********************************************************************
1329 * _fseeki64_nolock (MSVCRT.@)
1331 int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
1333 int ret;
1335 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1336 whence = SEEK_SET;
1337 offset += MSVCRT__ftelli64_nolock(file);
1340 /* Flush output if needed */
1341 msvcrt_flush_buffer(file);
1342 /* Reset direction of i/o */
1343 if(file->_flag & MSVCRT__IORW) {
1344 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1346 /* Clear end of file flag */
1347 file->_flag &= ~MSVCRT__IOEOF;
1348 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1350 return ret;
1353 /*********************************************************************
1354 * fseek (MSVCRT.@)
1356 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1358 return MSVCRT__fseeki64( file, offset, whence );
1361 /*********************************************************************
1362 * _fseek_nolock (MSVCRT.@)
1364 int CDECL MSVCRT__fseek_nolock(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1366 return MSVCRT__fseeki64_nolock( file, offset, whence );
1369 /*********************************************************************
1370 * _chsize_s (MSVCRT.@)
1372 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1374 ioinfo *info;
1375 __int64 cur, pos;
1376 BOOL ret = FALSE;
1378 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1380 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1383 info = get_ioinfo(fd);
1384 if (info->handle != INVALID_HANDLE_VALUE)
1386 /* save the current file pointer */
1387 cur = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1388 if (cur >= 0)
1390 pos = MSVCRT__lseeki64(fd, size, SEEK_SET);
1391 if (pos >= 0)
1393 ret = SetEndOfFile(info->handle);
1394 if (!ret) msvcrt_set_errno(GetLastError());
1397 /* restore the file pointer */
1398 MSVCRT__lseeki64(fd, cur, SEEK_SET);
1402 release_ioinfo(info);
1403 return ret ? 0 : *MSVCRT__errno();
1406 /*********************************************************************
1407 * _chsize (MSVCRT.@)
1409 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1411 /* _chsize_s returns errno on failure but _chsize should return -1 */
1412 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1415 /*********************************************************************
1416 * clearerr (MSVCRT.@)
1418 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1420 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1422 MSVCRT__lock_file(file);
1423 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1424 MSVCRT__unlock_file(file);
1427 /*********************************************************************
1428 * rewind (MSVCRT.@)
1430 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1432 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1434 MSVCRT__lock_file(file);
1435 MSVCRT__fseek_nolock(file, 0L, SEEK_SET);
1436 MSVCRT_clearerr(file);
1437 MSVCRT__unlock_file(file);
1440 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1442 int plus = strchrW(mode, '+') != NULL;
1444 TRACE("%s\n", debugstr_w(mode));
1446 while(*mode == ' ') mode++;
1448 switch(*mode++)
1450 case 'R': case 'r':
1451 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1452 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1453 break;
1454 case 'W': case 'w':
1455 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1456 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1457 break;
1458 case 'A': case 'a':
1459 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1460 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1461 break;
1462 default:
1463 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1464 return -1;
1467 *stream_flags |= MSVCRT__commode;
1469 while (*mode && *mode!=',')
1470 switch (*mode++)
1472 case 'B': case 'b':
1473 *open_flags |= MSVCRT__O_BINARY;
1474 *open_flags &= ~MSVCRT__O_TEXT;
1475 break;
1476 case 't':
1477 *open_flags |= MSVCRT__O_TEXT;
1478 *open_flags &= ~MSVCRT__O_BINARY;
1479 break;
1480 case 'D':
1481 *open_flags |= MSVCRT__O_TEMPORARY;
1482 break;
1483 case 'T':
1484 *open_flags |= MSVCRT__O_SHORT_LIVED;
1485 break;
1486 case 'c':
1487 *stream_flags |= MSVCRT__IOCOMMIT;
1488 break;
1489 case 'n':
1490 *stream_flags &= ~MSVCRT__IOCOMMIT;
1491 break;
1492 case 'N':
1493 *open_flags |= MSVCRT__O_NOINHERIT;
1494 break;
1495 case '+':
1496 case ' ':
1497 case 'a':
1498 case 'w':
1499 break;
1500 case 'S':
1501 case 'R':
1502 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1503 break;
1504 default:
1505 ERR("incorrect mode flag: %c\n", mode[-1]);
1506 break;
1509 if(*mode == ',')
1511 static const WCHAR ccs[] = {'c','c','s'};
1512 static const WCHAR utf8[] = {'u','t','f','-','8'};
1513 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1514 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1516 mode++;
1517 while(*mode == ' ') mode++;
1518 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1519 return -1;
1520 mode += sizeof(ccs)/sizeof(ccs[0]);
1521 while(*mode == ' ') mode++;
1522 if(!MSVCRT_CHECK_PMT(*mode == '='))
1523 return -1;
1524 mode++;
1525 while(*mode == ' ') mode++;
1527 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1529 *open_flags |= MSVCRT__O_U8TEXT;
1530 mode += sizeof(utf8)/sizeof(utf8[0]);
1532 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1534 *open_flags |= MSVCRT__O_U16TEXT;
1535 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1537 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1539 *open_flags |= MSVCRT__O_WTEXT;
1540 mode += sizeof(unicode)/sizeof(unicode[0]);
1542 else
1544 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1545 return -1;
1548 while(*mode == ' ') mode++;
1551 if(!MSVCRT_CHECK_PMT(*mode == 0))
1552 return -1;
1553 return 0;
1556 /*********************************************************************
1557 * _fdopen (MSVCRT.@)
1559 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1561 MSVCRT_FILE *ret;
1562 MSVCRT_wchar_t *modeW = NULL;
1564 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1566 ret = MSVCRT__wfdopen(fd, modeW);
1568 MSVCRT_free(modeW);
1569 return ret;
1572 /*********************************************************************
1573 * _wfdopen (MSVCRT.@)
1575 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1577 int open_flags, stream_flags;
1578 MSVCRT_FILE* file;
1580 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1582 LOCK_FILES();
1583 if (!(file = msvcrt_alloc_fp()))
1584 file = NULL;
1585 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1587 file->_flag = 0;
1588 file = NULL;
1590 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1591 UNLOCK_FILES();
1593 return file;
1596 /*********************************************************************
1597 * _filelength (MSVCRT.@)
1599 LONG CDECL MSVCRT__filelength(int fd)
1601 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1602 if (curPos != -1)
1604 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1605 if (endPos != -1)
1607 if (endPos != curPos)
1608 MSVCRT__lseek(fd, curPos, SEEK_SET);
1609 return endPos;
1612 return -1;
1615 /*********************************************************************
1616 * _filelengthi64 (MSVCRT.@)
1618 __int64 CDECL MSVCRT__filelengthi64(int fd)
1620 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1621 if (curPos != -1)
1623 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1624 if (endPos != -1)
1626 if (endPos != curPos)
1627 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1628 return endPos;
1631 return -1;
1634 /*********************************************************************
1635 * _fileno (MSVCRT.@)
1637 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1639 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1640 return file->_file;
1643 /*********************************************************************
1644 * _fstat64 (MSVCRT.@)
1646 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1648 ioinfo *info = get_ioinfo(fd);
1649 DWORD dw;
1650 DWORD type;
1651 BY_HANDLE_FILE_INFORMATION hfi;
1653 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1654 if (info->handle == INVALID_HANDLE_VALUE)
1656 release_ioinfo(info);
1657 return -1;
1660 if (!buf)
1662 WARN(":failed-NULL buf\n");
1663 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1664 release_ioinfo(info);
1665 return -1;
1668 memset(&hfi, 0, sizeof(hfi));
1669 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1670 type = GetFileType(info->handle);
1671 if (type == FILE_TYPE_PIPE)
1673 buf->st_dev = buf->st_rdev = fd;
1674 buf->st_mode = S_IFIFO;
1675 buf->st_nlink = 1;
1677 else if (type == FILE_TYPE_CHAR)
1679 buf->st_dev = buf->st_rdev = fd;
1680 buf->st_mode = S_IFCHR;
1681 buf->st_nlink = 1;
1683 else /* FILE_TYPE_DISK etc. */
1685 if (!GetFileInformationByHandle(info->handle, &hfi))
1687 WARN(":failed-last error (%d)\n",GetLastError());
1688 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1689 release_ioinfo(info);
1690 return -1;
1692 buf->st_mode = S_IFREG | 0444;
1693 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1694 buf->st_mode |= 0222;
1695 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1696 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1697 buf->st_atime = dw;
1698 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1699 buf->st_mtime = buf->st_ctime = dw;
1700 buf->st_nlink = hfi.nNumberOfLinks;
1702 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1703 buf->st_mode);
1704 release_ioinfo(info);
1705 return 0;
1708 /*********************************************************************
1709 * _fstati64 (MSVCRT.@)
1711 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1713 int ret;
1714 struct MSVCRT__stat64 buf64;
1716 ret = MSVCRT__fstat64(fd, &buf64);
1717 if (!ret)
1718 msvcrt_stat64_to_stati64(&buf64, buf);
1719 return ret;
1722 /*********************************************************************
1723 * _fstat (MSVCRT.@)
1725 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1726 { int ret;
1727 struct MSVCRT__stat64 buf64;
1729 ret = MSVCRT__fstat64(fd, &buf64);
1730 if (!ret)
1731 msvcrt_stat64_to_stat(&buf64, buf);
1732 return ret;
1735 /*********************************************************************
1736 * _fstat32 (MSVCR80.@)
1738 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1740 int ret;
1741 struct MSVCRT__stat64 buf64;
1743 ret = MSVCRT__fstat64(fd, &buf64);
1744 if (!ret)
1745 msvcrt_stat64_to_stat32(&buf64, buf);
1746 return ret;
1749 /*********************************************************************
1750 * _fstat64i32 (MSVCR80.@)
1752 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1754 int ret;
1755 struct MSVCRT__stat64 buf64;
1757 ret = MSVCRT__fstat64(fd, &buf64);
1758 if (!ret)
1759 msvcrt_stat64_to_stat64i32(&buf64, buf);
1760 return ret;
1763 /*********************************************************************
1764 * _futime64 (MSVCRT.@)
1766 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1768 HANDLE hand = msvcrt_fdtoh(fd);
1769 FILETIME at, wt;
1771 if (!t)
1773 time_to_filetime( MSVCRT__time64(NULL), &at );
1774 wt = at;
1776 else
1778 time_to_filetime( t->actime, &at );
1779 time_to_filetime( t->modtime, &wt );
1782 if (!SetFileTime(hand, NULL, &at, &wt))
1784 msvcrt_set_errno(GetLastError());
1785 return -1 ;
1787 return 0;
1790 /*********************************************************************
1791 * _futime32 (MSVCRT.@)
1793 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1795 if (t)
1797 struct MSVCRT___utimbuf64 t64;
1798 t64.actime = t->actime;
1799 t64.modtime = t->modtime;
1800 return _futime64( fd, &t64 );
1802 else
1803 return _futime64( fd, NULL );
1806 /*********************************************************************
1807 * _futime (MSVCRT.@)
1809 #ifdef _WIN64
1810 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1812 return _futime64( fd, t );
1814 #else
1815 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1817 return _futime32( fd, t );
1819 #endif
1821 /*********************************************************************
1822 * _get_osfhandle (MSVCRT.@)
1824 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1826 HANDLE hand = msvcrt_fdtoh(fd);
1827 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1829 return (MSVCRT_intptr_t)hand;
1832 /*********************************************************************
1833 * _mktemp_s (MSVCRT.@)
1835 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1837 DWORD len, xno, id;
1839 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1840 return MSVCRT_EINVAL;
1842 for(len=0; len<size; len++)
1843 if(!pattern[len])
1844 break;
1845 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1846 if(size)
1847 pattern[0] = 0;
1848 return MSVCRT_EINVAL;
1851 for(xno=1; xno<=6; xno++)
1852 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1853 return MSVCRT_EINVAL;
1855 id = GetCurrentProcessId();
1856 for(xno=1; xno<6; xno++) {
1857 pattern[len-xno] = id%10 + '0';
1858 id /= 10;
1861 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1862 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1863 return 0;
1866 pattern[0] = 0;
1867 *MSVCRT__errno() = MSVCRT_EEXIST;
1868 return MSVCRT_EEXIST;
1871 /*********************************************************************
1872 * _mktemp (MSVCRT.@)
1874 char * CDECL MSVCRT__mktemp(char *pattern)
1876 int numX = 0;
1877 char *retVal = pattern;
1878 int id;
1879 char letter = 'a';
1881 if(!pattern)
1882 return NULL;
1884 while(*pattern)
1885 numX = (*pattern++ == 'X')? numX + 1 : 0;
1886 if (numX < 6)
1887 return NULL;
1888 pattern--;
1889 id = GetCurrentProcessId();
1890 numX = 6;
1891 while(numX--)
1893 int tempNum = id / 10;
1894 *pattern-- = id - (tempNum * 10) + '0';
1895 id = tempNum;
1897 pattern++;
1900 *pattern = letter++;
1901 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1902 return retVal;
1903 } while(letter <= 'z');
1904 return NULL;
1907 /*********************************************************************
1908 * _wmktemp_s (MSVCRT.@)
1910 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1912 DWORD len, xno, id;
1914 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1915 return MSVCRT_EINVAL;
1917 for(len=0; len<size; len++)
1918 if(!pattern[len])
1919 break;
1920 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1921 if(size)
1922 pattern[0] = 0;
1923 return MSVCRT_EINVAL;
1926 for(xno=1; xno<=6; xno++)
1927 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1928 return MSVCRT_EINVAL;
1930 id = GetCurrentProcessId();
1931 for(xno=1; xno<6; xno++) {
1932 pattern[len-xno] = id%10 + '0';
1933 id /= 10;
1936 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1937 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1938 return 0;
1941 pattern[0] = 0;
1942 *MSVCRT__errno() = MSVCRT_EEXIST;
1943 return MSVCRT_EEXIST;
1946 /*********************************************************************
1947 * _wmktemp (MSVCRT.@)
1949 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1951 int numX = 0;
1952 MSVCRT_wchar_t *retVal = pattern;
1953 int id;
1954 MSVCRT_wchar_t letter = 'a';
1956 if(!pattern)
1957 return NULL;
1959 while(*pattern)
1960 numX = (*pattern++ == 'X')? numX + 1 : 0;
1961 if (numX < 6)
1962 return NULL;
1963 pattern--;
1964 id = GetCurrentProcessId();
1965 numX = 6;
1966 while(numX--)
1968 int tempNum = id / 10;
1969 *pattern-- = id - (tempNum * 10) + '0';
1970 id = tempNum;
1972 pattern++;
1975 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
1976 return retVal;
1977 *pattern = letter++;
1978 } while(letter != '|');
1979 return NULL;
1982 static unsigned split_oflags(unsigned oflags)
1984 int wxflags = 0;
1985 unsigned unsupp; /* until we support everything */
1987 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1988 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1989 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1990 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
1991 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
1992 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
1993 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1994 else wxflags |= WX_TEXT; /* default to TEXT*/
1995 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1997 if ((unsupp = oflags & ~(
1998 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1999 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
2000 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
2001 MSVCRT__O_NOINHERIT|
2002 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
2003 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
2005 ERR(":unsupported oflags 0x%04x\n",unsupp);
2007 return wxflags;
2010 /*********************************************************************
2011 * _pipe (MSVCRT.@)
2013 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
2015 int ret = -1;
2016 SECURITY_ATTRIBUTES sa;
2017 HANDLE readHandle, writeHandle;
2019 if (!pfds)
2021 *MSVCRT__errno() = MSVCRT_EINVAL;
2022 return -1;
2025 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2026 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
2027 sa.lpSecurityDescriptor = NULL;
2028 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2030 unsigned int wxflags = split_oflags(textmode);
2031 int fd;
2033 LOCK_FILES();
2034 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2035 if (fd != -1)
2037 pfds[0] = fd;
2038 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2039 if (fd != -1)
2041 pfds[1] = fd;
2042 ret = 0;
2044 else
2046 MSVCRT__close(pfds[0]);
2047 CloseHandle(writeHandle);
2048 *MSVCRT__errno() = MSVCRT_EMFILE;
2051 else
2053 CloseHandle(readHandle);
2054 CloseHandle(writeHandle);
2055 *MSVCRT__errno() = MSVCRT_EMFILE;
2057 UNLOCK_FILES();
2059 else
2060 msvcrt_set_errno(GetLastError());
2062 return ret;
2065 static int check_bom(HANDLE h, int oflags, BOOL seek)
2067 char bom[sizeof(utf8_bom)];
2068 DWORD r;
2070 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2072 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2073 return oflags;
2075 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2076 oflags |= MSVCRT__O_U8TEXT;
2077 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2078 if (seek && r>2)
2079 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2080 oflags |= MSVCRT__O_U16TEXT;
2081 }else if (seek) {
2082 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2085 return oflags;
2088 /*********************************************************************
2089 * _wsopen_s (MSVCRT.@)
2091 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2093 DWORD access = 0, creation = 0, attrib;
2094 SECURITY_ATTRIBUTES sa;
2095 DWORD sharing, type;
2096 int wxflag;
2097 HANDLE hand;
2099 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2100 fd, debugstr_w(path), oflags, shflags, pmode);
2102 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2104 *fd = -1;
2105 wxflag = split_oflags(oflags);
2106 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2108 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2109 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2110 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2113 if (oflags & MSVCRT__O_CREAT)
2115 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
2116 FIXME(": pmode 0x%04x ignored\n", pmode);
2117 else
2118 WARN(": pmode 0x%04x ignored\n", pmode);
2120 if (oflags & MSVCRT__O_EXCL)
2121 creation = CREATE_NEW;
2122 else if (oflags & MSVCRT__O_TRUNC)
2123 creation = CREATE_ALWAYS;
2124 else
2125 creation = OPEN_ALWAYS;
2127 else /* no MSVCRT__O_CREAT */
2129 if (oflags & MSVCRT__O_TRUNC)
2130 creation = TRUNCATE_EXISTING;
2131 else
2132 creation = OPEN_EXISTING;
2135 switch( shflags )
2137 case MSVCRT__SH_DENYRW:
2138 sharing = 0L;
2139 break;
2140 case MSVCRT__SH_DENYWR:
2141 sharing = FILE_SHARE_READ;
2142 break;
2143 case MSVCRT__SH_DENYRD:
2144 sharing = FILE_SHARE_WRITE;
2145 break;
2146 case MSVCRT__SH_DENYNO:
2147 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2148 break;
2149 default:
2150 ERR( "Unhandled shflags 0x%x\n", shflags );
2151 return MSVCRT_EINVAL;
2153 attrib = FILE_ATTRIBUTE_NORMAL;
2155 if (oflags & MSVCRT__O_TEMPORARY)
2157 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2158 access |= DELETE;
2159 sharing |= FILE_SHARE_DELETE;
2162 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2163 sa.lpSecurityDescriptor = NULL;
2164 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2166 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2167 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2168 && !(access&GENERIC_READ))
2170 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2171 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2172 if (hand != INVALID_HANDLE_VALUE)
2174 oflags = check_bom(hand, oflags, FALSE);
2175 CloseHandle(hand);
2177 else
2178 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2181 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2182 if (hand == INVALID_HANDLE_VALUE) {
2183 WARN(":failed-last error (%d)\n",GetLastError());
2184 msvcrt_set_errno(GetLastError());
2185 return *MSVCRT__errno();
2188 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2190 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2191 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2192 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2194 if (oflags & MSVCRT__O_U8TEXT)
2196 DWORD written = 0, tmp;
2198 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2199 sizeof(utf8_bom)-written, &tmp, NULL))
2200 written += tmp;
2201 if (written != sizeof(utf8_bom)) {
2202 WARN("error writing BOM\n");
2203 CloseHandle(hand);
2204 msvcrt_set_errno(GetLastError());
2205 return *MSVCRT__errno();
2208 else
2210 DWORD written = 0, tmp;
2212 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2213 sizeof(utf16_bom)-written, &tmp, NULL))
2214 written += tmp;
2215 if (written != sizeof(utf16_bom))
2217 WARN("error writing BOM\n");
2218 CloseHandle(hand);
2219 msvcrt_set_errno(GetLastError());
2220 return *MSVCRT__errno();
2224 else if (access & GENERIC_READ)
2225 oflags = check_bom(hand, oflags, TRUE);
2228 type = GetFileType(hand);
2229 if (type == FILE_TYPE_CHAR)
2230 wxflag |= WX_TTY;
2231 else if (type == FILE_TYPE_PIPE)
2232 wxflag |= WX_PIPE;
2234 *fd = msvcrt_alloc_fd(hand, wxflag);
2235 if (*fd == -1)
2236 return *MSVCRT__errno();
2238 if (oflags & MSVCRT__O_WTEXT)
2239 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2240 else if (oflags & MSVCRT__O_U16TEXT)
2241 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
2242 else if (oflags & MSVCRT__O_U8TEXT)
2243 get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
2245 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2246 return 0;
2249 /*********************************************************************
2250 * _wsopen (MSVCRT.@)
2252 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2254 int pmode;
2255 int fd;
2257 if (oflags & MSVCRT__O_CREAT)
2259 __ms_va_list ap;
2261 __ms_va_start(ap, shflags);
2262 pmode = va_arg(ap, int);
2263 __ms_va_end(ap);
2265 else
2266 pmode = 0;
2268 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
2269 return fd;
2272 /*********************************************************************
2273 * _sopen_s (MSVCRT.@)
2275 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2277 MSVCRT_wchar_t *pathW;
2278 int ret;
2280 if (!MSVCRT_CHECK_PMT(fd != NULL))
2281 return MSVCRT_EINVAL;
2282 *fd = -1;
2283 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2284 return MSVCRT_EINVAL;
2286 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2287 MSVCRT_free(pathW);
2288 return ret;
2291 /*********************************************************************
2292 * _sopen (MSVCRT.@)
2294 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2296 int pmode;
2297 int fd;
2299 if (oflags & MSVCRT__O_CREAT)
2301 __ms_va_list ap;
2303 __ms_va_start(ap, shflags);
2304 pmode = va_arg(ap, int);
2305 __ms_va_end(ap);
2307 else
2308 pmode = 0;
2310 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2311 return fd;
2314 /*********************************************************************
2315 * _open (MSVCRT.@)
2317 int CDECL MSVCRT__open( const char *path, int flags, ... )
2319 __ms_va_list ap;
2321 if (flags & MSVCRT__O_CREAT)
2323 int pmode;
2324 __ms_va_start(ap, flags);
2325 pmode = va_arg(ap, int);
2326 __ms_va_end(ap);
2327 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2329 else
2330 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2333 /*********************************************************************
2334 * _wopen (MSVCRT.@)
2336 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2338 __ms_va_list ap;
2340 if (flags & MSVCRT__O_CREAT)
2342 int pmode;
2343 __ms_va_start(ap, flags);
2344 pmode = va_arg(ap, int);
2345 __ms_va_end(ap);
2346 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2348 else
2349 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2352 /*********************************************************************
2353 * _creat (MSVCRT.@)
2355 int CDECL MSVCRT__creat(const char *path, int flags)
2357 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2358 return MSVCRT__open(path, usedFlags);
2361 /*********************************************************************
2362 * _wcreat (MSVCRT.@)
2364 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2366 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2367 return MSVCRT__wopen(path, usedFlags);
2370 /*********************************************************************
2371 * _open_osfhandle (MSVCRT.@)
2373 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2375 DWORD flags;
2376 int fd;
2378 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2379 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2380 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2381 * text - it never sets MSVCRT__O_BINARY.
2383 /* don't let split_oflags() decide the mode if no mode is passed */
2384 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2385 oflags |= MSVCRT__O_BINARY;
2387 flags = GetFileType((HANDLE)handle);
2388 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2390 msvcrt_set_errno(GetLastError());
2391 return -1;
2394 if (flags == FILE_TYPE_CHAR)
2395 flags = WX_TTY;
2396 else if (flags == FILE_TYPE_PIPE)
2397 flags = WX_PIPE;
2398 else
2399 flags = 0;
2400 flags |= split_oflags(oflags);
2402 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2403 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2404 return fd;
2407 /*********************************************************************
2408 * _rmtmp (MSVCRT.@)
2410 int CDECL MSVCRT__rmtmp(void)
2412 int num_removed = 0, i;
2413 MSVCRT_FILE *file;
2415 LOCK_FILES();
2416 for (i = 3; i < MSVCRT_stream_idx; i++) {
2417 file = msvcrt_get_file(i);
2419 if (file->_tmpfname)
2421 MSVCRT_fclose(file);
2422 num_removed++;
2425 UNLOCK_FILES();
2427 if (num_removed)
2428 TRACE(":removed (%d) temp files\n",num_removed);
2429 return num_removed;
2432 static inline int get_utf8_char_len(char ch)
2434 if((ch&0xf8) == 0xf0)
2435 return 4;
2436 else if((ch&0xf0) == 0xe0)
2437 return 3;
2438 else if((ch&0xe0) == 0xc0)
2439 return 2;
2440 return 1;
2443 /*********************************************************************
2444 * (internal) read_utf8
2446 static int read_utf8(int fd, MSVCRT_wchar_t *buf, unsigned int count)
2448 ioinfo *fdinfo = get_ioinfo_nolock(fd);
2449 HANDLE hand = fdinfo->handle;
2450 char min_buf[4], *readbuf, lookahead;
2451 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2453 /* make the buffer big enough to hold at least one character */
2454 /* read bytes have to fit to output and lookahead buffers */
2455 count /= 2;
2456 readbuf_size = count < 4 ? 4 : count;
2457 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2458 readbuf_size = 4;
2459 readbuf = min_buf;
2462 if(fdinfo->lookahead[0] != '\n') {
2463 readbuf[pos++] = fdinfo->lookahead[0];
2464 fdinfo->lookahead[0] = '\n';
2466 if(fdinfo->lookahead[1] != '\n') {
2467 readbuf[pos++] = fdinfo->lookahead[1];
2468 fdinfo->lookahead[1] = '\n';
2470 if(fdinfo->lookahead[2] != '\n') {
2471 readbuf[pos++] = fdinfo->lookahead[2];
2472 fdinfo->lookahead[2] = '\n';
2477 /* NOTE: this case is broken in native dll, reading
2478 * sometimes fails when small buffer is passed
2480 if(count < 4) {
2481 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2482 if (GetLastError() == ERROR_BROKEN_PIPE) {
2483 fdinfo->wxflag |= WX_ATEOF;
2484 return 0;
2485 }else {
2486 msvcrt_set_errno(GetLastError());
2487 return -1;
2489 }else if(!num_read) {
2490 fdinfo->wxflag |= WX_ATEOF;
2491 return 0;
2492 }else {
2493 pos++;
2496 char_len = get_utf8_char_len(readbuf[0]);
2497 if(char_len>pos) {
2498 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2499 pos += num_read;
2502 if(readbuf[0] == '\n')
2503 fdinfo->wxflag |= WX_READNL;
2504 else
2505 fdinfo->wxflag &= ~WX_READNL;
2507 if(readbuf[0] == 0x1a) {
2508 fdinfo->wxflag |= WX_ATEOF;
2509 return 0;
2512 if(readbuf[0] == '\r') {
2513 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2514 buf[0] = '\r';
2515 else if(lookahead == '\n')
2516 buf[0] = '\n';
2517 else {
2518 buf[0] = '\r';
2519 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2520 fdinfo->lookahead[0] = lookahead;
2521 else
2522 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2524 return 2;
2527 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2528 msvcrt_set_errno(GetLastError());
2529 return -1;
2532 return num_read*2;
2535 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2536 if(pos) {
2537 num_read = 0;
2538 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2539 fdinfo->wxflag |= WX_ATEOF;
2540 if (readbuf != min_buf) MSVCRT_free(readbuf);
2541 return 0;
2542 }else {
2543 msvcrt_set_errno(GetLastError());
2544 if (readbuf != min_buf) MSVCRT_free(readbuf);
2545 return -1;
2547 }else if(!pos && !num_read) {
2548 fdinfo->wxflag |= WX_ATEOF;
2549 if (readbuf != min_buf) MSVCRT_free(readbuf);
2550 return 0;
2553 pos += num_read;
2554 if(readbuf[0] == '\n')
2555 fdinfo->wxflag |= WX_READNL;
2556 else
2557 fdinfo->wxflag &= ~WX_READNL;
2559 /* Find first byte of last character (may be incomplete) */
2560 for(i=pos-1; i>0 && i>pos-4; i--)
2561 if((readbuf[i]&0xc0) != 0x80)
2562 break;
2563 char_len = get_utf8_char_len(readbuf[i]);
2564 if(char_len+i <= pos)
2565 i += char_len;
2567 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2568 if(i < pos)
2569 fdinfo->lookahead[0] = readbuf[i];
2570 if(i+1 < pos)
2571 fdinfo->lookahead[1] = readbuf[i+1];
2572 if(i+2 < pos)
2573 fdinfo->lookahead[2] = readbuf[i+2];
2574 }else if(i < pos) {
2575 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2577 pos = i;
2579 for(i=0, j=0; i<pos; i++) {
2580 if(readbuf[i] == 0x1a) {
2581 fdinfo->wxflag |= WX_ATEOF;
2582 break;
2585 /* strip '\r' if followed by '\n' */
2586 if(readbuf[i] == '\r' && i+1==pos) {
2587 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2588 readbuf[j++] = '\r';
2589 }else if(lookahead == '\n' && j==0) {
2590 readbuf[j++] = '\n';
2591 }else {
2592 if(lookahead != '\n')
2593 readbuf[j++] = '\r';
2595 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2596 fdinfo->lookahead[0] = lookahead;
2597 else
2598 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2600 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2601 readbuf[j++] = readbuf[i];
2604 pos = j;
2606 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2607 msvcrt_set_errno(GetLastError());
2608 if (readbuf != min_buf) MSVCRT_free(readbuf);
2609 return -1;
2612 if (readbuf != min_buf) MSVCRT_free(readbuf);
2613 return num_read*2;
2616 /*********************************************************************
2617 * (internal) read_i
2619 * When reading \r as last character in text mode, read() positions
2620 * the file pointer on the \r character while getc() goes on to
2621 * the following \n
2623 static int read_i(int fd, void *buf, unsigned int count)
2625 DWORD num_read, utf16;
2626 char *bufstart = buf;
2627 HANDLE hand = msvcrt_fdtoh(fd);
2628 ioinfo *fdinfo = get_ioinfo_nolock(fd);
2630 if (count == 0)
2631 return 0;
2633 if (fdinfo->wxflag & WX_ATEOF) {
2634 TRACE("already at EOF, returning 0\n");
2635 return 0;
2637 /* Don't trace small reads, it gets *very* annoying */
2638 if (count > 4)
2639 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2640 if (hand == INVALID_HANDLE_VALUE)
2642 *MSVCRT__errno() = MSVCRT_EBADF;
2643 return -1;
2646 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2647 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2649 *MSVCRT__errno() = MSVCRT_EINVAL;
2650 return -1;
2653 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2654 return read_utf8(fd, buf, count);
2656 if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL))
2658 if (fdinfo->lookahead[0] != '\n')
2660 bufstart[0] = fdinfo->lookahead[0];
2661 fdinfo->lookahead[0] = '\n';
2663 if (utf16)
2665 bufstart[1] = fdinfo->lookahead[1];
2666 fdinfo->lookahead[1] = '\n';
2669 if(count>1+utf16 && ReadFile(hand, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2670 num_read += 1+utf16;
2671 else
2672 num_read = 1+utf16;
2675 if(utf16 && (num_read&1))
2677 /* msvcr90 uses uninitialized value from the buffer in this case */
2678 /* msvcrt ignores additional data */
2679 ERR("got odd number of bytes in UTF16 mode\n");
2680 num_read--;
2683 if (count != 0 && num_read == 0)
2685 fdinfo->wxflag |= WX_ATEOF;
2686 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2688 else if (fdinfo->wxflag & WX_TEXT)
2690 DWORD i, j;
2692 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2693 fdinfo->wxflag |= WX_READNL;
2694 else
2695 fdinfo->wxflag &= ~WX_READNL;
2697 for (i=0, j=0; i<num_read; i+=1+utf16)
2699 /* in text mode, a ctrl-z signals EOF */
2700 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2702 fdinfo->wxflag |= WX_ATEOF;
2703 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2704 break;
2707 /* in text mode, strip \r if followed by \n */
2708 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2710 char lookahead[2];
2711 DWORD len;
2713 lookahead[1] = '\n';
2714 if (ReadFile(hand, lookahead, 1+utf16, &len, NULL) && len)
2716 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2718 bufstart[j++] = '\n';
2719 if(utf16) bufstart[j++] = 0;
2721 else
2723 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2725 bufstart[j++] = '\r';
2726 if(utf16) bufstart[j++] = 0;
2729 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2731 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2733 bufstart[j++] = '\n';
2734 if (utf16) bufstart[j++] = 0;
2736 else
2738 fdinfo->lookahead[0] = lookahead[0];
2739 fdinfo->lookahead[1] = lookahead[1];
2742 else
2743 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2746 else
2748 bufstart[j++] = '\r';
2749 if(utf16) bufstart[j++] = 0;
2752 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2753 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2755 bufstart[j++] = bufstart[i];
2756 if(utf16) bufstart[j++] = bufstart[i+1];
2759 num_read = j;
2762 else
2764 if (GetLastError() == ERROR_BROKEN_PIPE)
2766 TRACE(":end-of-pipe\n");
2767 fdinfo->wxflag |= WX_ATEOF;
2768 return 0;
2770 else
2772 TRACE(":failed-last error (%d)\n",GetLastError());
2773 return -1;
2777 if (count > 4)
2778 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2779 return num_read;
2782 /*********************************************************************
2783 * _read (MSVCRT.@)
2785 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2787 int num_read;
2788 num_read = read_i(fd, buf, count);
2789 return num_read;
2792 /*********************************************************************
2793 * _setmode (MSVCRT.@)
2795 int CDECL MSVCRT__setmode(int fd,int mode)
2797 int ret = get_ioinfo_nolock(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2798 if(ret==MSVCRT__O_TEXT && (get_ioinfo_nolock(fd)->exflag & (EF_UTF8|EF_UTF16)))
2799 ret = MSVCRT__O_WTEXT;
2801 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2802 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2803 *MSVCRT__errno() = MSVCRT_EINVAL;
2804 return -1;
2807 if(mode == MSVCRT__O_BINARY) {
2808 get_ioinfo_nolock(fd)->wxflag &= ~WX_TEXT;
2809 get_ioinfo_nolock(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2810 return ret;
2813 get_ioinfo_nolock(fd)->wxflag |= WX_TEXT;
2814 if(mode == MSVCRT__O_TEXT)
2815 get_ioinfo_nolock(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2816 else if(mode == MSVCRT__O_U8TEXT)
2817 get_ioinfo_nolock(fd)->exflag = (get_ioinfo_nolock(fd)->exflag & ~EF_UTF16) | EF_UTF8;
2818 else
2819 get_ioinfo_nolock(fd)->exflag = (get_ioinfo_nolock(fd)->exflag & ~EF_UTF8) | EF_UTF16;
2821 return ret;
2824 /*********************************************************************
2825 * _stat64 (MSVCRT.@)
2827 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2829 DWORD dw;
2830 WIN32_FILE_ATTRIBUTE_DATA hfi;
2831 unsigned short mode = ALL_S_IREAD;
2832 int plen;
2834 TRACE(":file (%s) buf(%p)\n",path,buf);
2836 plen = strlen(path);
2837 while (plen && path[plen-1]==' ')
2838 plen--;
2840 if (plen && (plen<2 || path[plen-2]!=':') &&
2841 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2843 *MSVCRT__errno() = MSVCRT_ENOENT;
2844 return -1;
2847 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2849 TRACE("failed (%d)\n",GetLastError());
2850 *MSVCRT__errno() = MSVCRT_ENOENT;
2851 return -1;
2854 memset(buf,0,sizeof(struct MSVCRT__stat64));
2856 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2857 Bon 011120: This FIXME seems incorrect
2858 Also a letter as first char isn't enough to be classified
2859 as a drive letter
2861 if (isalpha(*path)&& (*(path+1)==':'))
2862 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2863 else
2864 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2866 /* Dir, or regular file? */
2867 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2868 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2869 else
2871 mode |= MSVCRT__S_IFREG;
2872 /* executable? */
2873 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2875 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2876 (tolower(path[plen-3]) << 16);
2877 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2878 mode |= ALL_S_IEXEC;
2882 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2883 mode |= ALL_S_IWRITE;
2885 buf->st_mode = mode;
2886 buf->st_nlink = 1;
2887 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2888 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2889 buf->st_atime = dw;
2890 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2891 buf->st_mtime = buf->st_ctime = dw;
2892 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2893 (int)(buf->st_size >> 32),(int)buf->st_size,
2894 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2895 return 0;
2898 /*********************************************************************
2899 * _stati64 (MSVCRT.@)
2901 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2903 int ret;
2904 struct MSVCRT__stat64 buf64;
2906 ret = MSVCRT_stat64(path, &buf64);
2907 if (!ret)
2908 msvcrt_stat64_to_stati64(&buf64, buf);
2909 return ret;
2912 /*********************************************************************
2913 * _stat (MSVCRT.@)
2915 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2917 int ret;
2918 struct MSVCRT__stat64 buf64;
2920 ret = MSVCRT_stat64( path, &buf64);
2921 if (!ret)
2922 msvcrt_stat64_to_stat(&buf64, buf);
2923 return ret;
2926 /*********************************************************************
2927 * _stat32 (MSVCR100.@)
2929 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
2931 int ret;
2932 struct MSVCRT__stat64 buf64;
2934 ret = MSVCRT_stat64(path, &buf64);
2935 if (!ret)
2936 msvcrt_stat64_to_stat32(&buf64, buf);
2937 return ret;
2940 /*********************************************************************
2941 * _stat32i64 (MSVCR100.@)
2943 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
2945 int ret;
2946 struct MSVCRT__stat64 buf64;
2948 ret = MSVCRT_stat64(path, &buf64);
2949 if (!ret)
2950 msvcrt_stat64_to_stat32i64(&buf64, buf);
2951 return ret;
2954 /*********************************************************************
2955 * _stat64i32 (MSVCR100.@)
2957 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
2959 int ret;
2960 struct MSVCRT__stat64 buf64;
2962 ret = MSVCRT_stat64(path, &buf64);
2963 if (!ret)
2964 msvcrt_stat64_to_stat64i32(&buf64, buf);
2965 return ret;
2968 /*********************************************************************
2969 * _wstat64 (MSVCRT.@)
2971 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2973 DWORD dw;
2974 WIN32_FILE_ATTRIBUTE_DATA hfi;
2975 unsigned short mode = ALL_S_IREAD;
2976 int plen;
2978 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2980 plen = strlenW(path);
2981 while (plen && path[plen-1]==' ')
2982 plen--;
2984 if(plen && (plen<2 || path[plen-2]!=':') &&
2985 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2987 *MSVCRT__errno() = MSVCRT_ENOENT;
2988 return -1;
2991 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2993 TRACE("failed (%d)\n",GetLastError());
2994 *MSVCRT__errno() = MSVCRT_ENOENT;
2995 return -1;
2998 memset(buf,0,sizeof(struct MSVCRT__stat64));
3000 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3001 if (MSVCRT_iswalpha(*path))
3002 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
3003 else
3004 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3006 /* Dir, or regular file? */
3007 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3008 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3009 else
3011 mode |= MSVCRT__S_IFREG;
3012 /* executable? */
3013 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3015 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
3016 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
3017 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3018 mode |= ALL_S_IEXEC;
3022 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3023 mode |= ALL_S_IWRITE;
3025 buf->st_mode = mode;
3026 buf->st_nlink = 1;
3027 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3028 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3029 buf->st_atime = dw;
3030 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3031 buf->st_mtime = buf->st_ctime = dw;
3032 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3033 (int)(buf->st_size >> 32),(int)buf->st_size,
3034 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3035 return 0;
3038 /*********************************************************************
3039 * _wstati64 (MSVCRT.@)
3041 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
3043 int ret;
3044 struct MSVCRT__stat64 buf64;
3046 ret = MSVCRT__wstat64(path, &buf64);
3047 if (!ret)
3048 msvcrt_stat64_to_stati64(&buf64, buf);
3049 return ret;
3052 /*********************************************************************
3053 * _wstat (MSVCRT.@)
3055 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
3057 int ret;
3058 struct MSVCRT__stat64 buf64;
3060 ret = MSVCRT__wstat64( path, &buf64 );
3061 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3062 return ret;
3065 /*********************************************************************
3066 * _wstat32 (MSVCR100.@)
3068 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
3070 int ret;
3071 struct MSVCRT__stat64 buf64;
3073 ret = MSVCRT__wstat64(path, &buf64);
3074 if (!ret)
3075 msvcrt_stat64_to_stat32(&buf64, buf);
3076 return ret;
3079 /*********************************************************************
3080 * _wstat32i64 (MSVCR100.@)
3082 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
3084 int ret;
3085 struct MSVCRT__stat64 buf64;
3087 ret = MSVCRT__wstat64(path, &buf64);
3088 if (!ret)
3089 msvcrt_stat64_to_stat32i64(&buf64, buf);
3090 return ret;
3093 /*********************************************************************
3094 * _wstat64i32 (MSVCR100.@)
3096 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3098 int ret;
3099 struct MSVCRT__stat64 buf64;
3101 ret = MSVCRT__wstat64(path, &buf64);
3102 if (!ret)
3103 msvcrt_stat64_to_stat64i32(&buf64, buf);
3104 return ret;
3107 /*********************************************************************
3108 * _tell (MSVCRT.@)
3110 MSVCRT_long CDECL MSVCRT__tell(int fd)
3112 return MSVCRT__lseek(fd, 0, SEEK_CUR);
3115 /*********************************************************************
3116 * _telli64 (MSVCRT.@)
3118 __int64 CDECL _telli64(int fd)
3120 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
3123 /*********************************************************************
3124 * _tempnam (MSVCRT.@)
3126 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3128 char tmpbuf[MAX_PATH];
3129 const char *tmp_dir = MSVCRT_getenv("TMP");
3131 if (tmp_dir) dir = tmp_dir;
3133 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3134 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3136 TRACE("got name (%s)\n",tmpbuf);
3137 DeleteFileA(tmpbuf);
3138 return MSVCRT__strdup(tmpbuf);
3140 TRACE("failed (%d)\n",GetLastError());
3141 return NULL;
3144 /*********************************************************************
3145 * _wtempnam (MSVCRT.@)
3147 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3149 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3150 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3151 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3153 if (tmp_dir) dir = tmp_dir;
3155 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3156 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3158 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3159 DeleteFileW(tmpbuf);
3160 return MSVCRT__wcsdup(tmpbuf);
3162 TRACE("failed (%d)\n",GetLastError());
3163 return NULL;
3166 /*********************************************************************
3167 * _umask (MSVCRT.@)
3169 int CDECL MSVCRT__umask(int umask)
3171 int old_umask = MSVCRT_umask;
3172 TRACE("(%d)\n",umask);
3173 MSVCRT_umask = umask;
3174 return old_umask;
3177 /*********************************************************************
3178 * _utime64 (MSVCRT.@)
3180 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3182 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3184 if (fd > 0)
3186 int retVal = _futime64(fd, t);
3187 MSVCRT__close(fd);
3188 return retVal;
3190 return -1;
3193 /*********************************************************************
3194 * _utime32 (MSVCRT.@)
3196 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3198 if (t)
3200 struct MSVCRT___utimbuf64 t64;
3201 t64.actime = t->actime;
3202 t64.modtime = t->modtime;
3203 return _utime64( path, &t64 );
3205 else
3206 return _utime64( path, NULL );
3209 /*********************************************************************
3210 * _utime (MSVCRT.@)
3212 #ifdef _WIN64
3213 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3215 return _utime64( path, t );
3217 #else
3218 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3220 return _utime32( path, t );
3222 #endif
3224 /*********************************************************************
3225 * _wutime64 (MSVCRT.@)
3227 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3229 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3231 if (fd > 0)
3233 int retVal = _futime64(fd, t);
3234 MSVCRT__close(fd);
3235 return retVal;
3237 return -1;
3240 /*********************************************************************
3241 * _wutime32 (MSVCRT.@)
3243 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3245 if (t)
3247 struct MSVCRT___utimbuf64 t64;
3248 t64.actime = t->actime;
3249 t64.modtime = t->modtime;
3250 return _wutime64( path, &t64 );
3252 else
3253 return _wutime64( path, NULL );
3256 /*********************************************************************
3257 * _wutime (MSVCRT.@)
3259 #ifdef _WIN64
3260 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3262 return _wutime64( path, t );
3264 #else
3265 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3267 return _wutime32( path, t );
3269 #endif
3271 /*********************************************************************
3272 * _write (MSVCRT.@)
3274 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3276 DWORD num_written;
3277 ioinfo *info = get_ioinfo_nolock(fd);
3278 HANDLE hand = info->handle;
3280 /* Don't trace small writes, it gets *very* annoying */
3281 #if 0
3282 if (count > 32)
3283 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3284 #endif
3285 if (hand == INVALID_HANDLE_VALUE)
3287 *MSVCRT__errno() = MSVCRT_EBADF;
3288 return -1;
3291 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3293 *MSVCRT__errno() = MSVCRT_EINVAL;
3294 return -1;
3297 /* If appending, go to EOF */
3298 if (info->wxflag & WX_APPEND)
3299 MSVCRT__lseek(fd, 0, FILE_END);
3301 if (!(info->wxflag & WX_TEXT))
3303 if (WriteFile(hand, buf, count, &num_written, NULL)
3304 && (num_written == count))
3305 return num_written;
3306 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3307 hand, GetLastError());
3308 *MSVCRT__errno() = MSVCRT_ENOSPC;
3310 else
3312 unsigned int i, j, nr_lf, size;
3313 char *p = NULL;
3314 const char *q;
3315 const char *s = buf, *buf_start = buf;
3317 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3319 /* find number of \n */
3320 for (nr_lf=0, i=0; i<count; i++)
3321 if (s[i] == '\n')
3322 nr_lf++;
3323 if (nr_lf)
3325 size = count+nr_lf;
3326 if ((q = p = MSVCRT_malloc(size)))
3328 for (s = buf, i = 0, j = 0; i < count; i++)
3330 if (s[i] == '\n')
3331 p[j++] = '\r';
3332 p[j++] = s[i];
3335 else
3337 FIXME("Malloc failed\n");
3338 nr_lf = 0;
3339 size = count;
3340 q = buf;
3343 else
3345 size = count;
3346 q = buf;
3349 else if (info->exflag & EF_UTF16)
3351 for (nr_lf=0, i=0; i<count; i+=2)
3352 if (s[i]=='\n' && s[i+1]==0)
3353 nr_lf += 2;
3354 if (nr_lf)
3356 size = count+nr_lf;
3357 if ((q = p = MSVCRT_malloc(size)))
3359 for (s=buf, i=0, j=0; i<count; i++)
3361 if (s[i]=='\n' && s[i+1]==0)
3363 p[j++] = '\r';
3364 p[j++] = 0;
3366 p[j++] = s[i++];
3367 p[j++] = s[i];
3370 else
3372 FIXME("Malloc failed\n");
3373 nr_lf = 0;
3374 size = count;
3375 q = buf;
3378 else
3380 size = count;
3381 q = buf;
3384 else
3386 DWORD conv_len;
3388 for(nr_lf=0, i=0; i<count; i+=2)
3389 if (s[i]=='\n' && s[i+1]==0)
3390 nr_lf++;
3392 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3393 if(!conv_len) {
3394 msvcrt_set_errno(GetLastError());
3395 MSVCRT_free(p);
3396 return -1;
3399 size = conv_len+nr_lf;
3400 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3402 for (s=buf, i=0, j=0; i<count; i++)
3404 if (s[i]=='\n' && s[i+1]==0)
3406 p[j++] = '\r';
3407 p[j++] = 0;
3409 p[j++] = s[i++];
3410 p[j++] = s[i];
3412 q = p+count+nr_lf*2;
3413 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3414 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3416 else
3418 FIXME("Malloc failed\n");
3419 nr_lf = 0;
3420 size = count;
3421 q = buf;
3425 if (!WriteFile(hand, q, size, &num_written, NULL))
3426 num_written = -1;
3427 if(p)
3428 MSVCRT_free(p);
3429 if (num_written != size)
3431 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3432 fd, hand, GetLastError(), num_written);
3433 *MSVCRT__errno() = MSVCRT_ENOSPC;
3434 return s - buf_start;
3436 return count;
3439 return -1;
3442 /*********************************************************************
3443 * _putw (MSVCRT.@)
3445 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3447 int len;
3449 MSVCRT__lock_file(file);
3450 len = MSVCRT__write(file->_file, &val, sizeof(val));
3451 if (len == sizeof(val)) {
3452 MSVCRT__unlock_file(file);
3453 return val;
3456 file->_flag |= MSVCRT__IOERR;
3457 MSVCRT__unlock_file(file);
3458 return MSVCRT_EOF;
3461 /*********************************************************************
3462 * fclose (MSVCRT.@)
3464 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3466 int ret;
3468 MSVCRT__lock_file(file);
3469 ret = MSVCRT__fclose_nolock(file);
3470 MSVCRT__unlock_file(file);
3472 return ret;
3475 /*********************************************************************
3476 * _fclose_nolock (MSVCRT.@)
3478 int CDECL MSVCRT__fclose_nolock(MSVCRT_FILE* file)
3480 int r, flag;
3482 flag = file->_flag;
3483 MSVCRT_free(file->_tmpfname);
3484 file->_tmpfname = NULL;
3485 /* flush stdio buffers */
3486 if(file->_flag & MSVCRT__IOWRT)
3487 MSVCRT__fflush_nolock(file);
3488 if(file->_flag & MSVCRT__IOMYBUF)
3489 MSVCRT_free(file->_base);
3491 r=MSVCRT__close(file->_file);
3492 file->_flag = 0;
3494 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3497 /*********************************************************************
3498 * feof (MSVCRT.@)
3500 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3502 return file->_flag & MSVCRT__IOEOF;
3505 /*********************************************************************
3506 * ferror (MSVCRT.@)
3508 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3510 return file->_flag & MSVCRT__IOERR;
3513 /*********************************************************************
3514 * _filbuf (MSVCRT.@)
3516 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3518 unsigned char c;
3520 if(file->_flag & MSVCRT__IOSTRG)
3521 return MSVCRT_EOF;
3523 /* Allocate buffer if needed */
3524 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3525 msvcrt_alloc_buffer(file);
3527 if(!(file->_flag & MSVCRT__IOREAD)) {
3528 if(file->_flag & MSVCRT__IORW)
3529 file->_flag |= MSVCRT__IOREAD;
3530 else
3531 return MSVCRT_EOF;
3534 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3535 int r;
3536 if ((r = read_i(file->_file,&c,1)) != 1) {
3537 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3538 return MSVCRT_EOF;
3541 return c;
3542 } else {
3543 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
3544 if(file->_cnt<=0) {
3545 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3546 file->_cnt = 0;
3547 return MSVCRT_EOF;
3550 file->_cnt--;
3551 file->_ptr = file->_base+1;
3552 c = *(unsigned char *)file->_base;
3553 return c;
3557 /*********************************************************************
3558 * fgetc (MSVCRT.@)
3560 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3562 int ret;
3564 MSVCRT__lock_file(file);
3565 ret = MSVCRT__fgetc_nolock(file);
3566 MSVCRT__unlock_file(file);
3568 return ret;
3571 /*********************************************************************
3572 * _fgetc_nolock (MSVCRT.@)
3574 int CDECL MSVCRT__fgetc_nolock(MSVCRT_FILE* file)
3576 unsigned char *i;
3577 unsigned int j;
3579 if (file->_cnt>0) {
3580 file->_cnt--;
3581 i = (unsigned char *)file->_ptr++;
3582 j = *i;
3583 } else
3584 j = MSVCRT__filbuf(file);
3586 return j;
3589 /*********************************************************************
3590 * _fgetchar (MSVCRT.@)
3592 int CDECL MSVCRT__fgetchar(void)
3594 return MSVCRT_fgetc(MSVCRT_stdin);
3597 /*********************************************************************
3598 * fgets (MSVCRT.@)
3600 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3602 int cc = MSVCRT_EOF;
3603 char * buf_start = s;
3605 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3606 file,file->_file,s,size);
3608 MSVCRT__lock_file(file);
3610 while ((size >1) && (cc = MSVCRT__fgetc_nolock(file)) != MSVCRT_EOF && cc != '\n')
3612 *s++ = (char)cc;
3613 size --;
3615 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3617 TRACE(":nothing read\n");
3618 MSVCRT__unlock_file(file);
3619 return NULL;
3621 if ((cc != MSVCRT_EOF) && (size > 1))
3622 *s++ = cc;
3623 *s = '\0';
3624 TRACE(":got %s\n", debugstr_a(buf_start));
3625 MSVCRT__unlock_file(file);
3626 return buf_start;
3629 /*********************************************************************
3630 * fgetwc (MSVCRT.@)
3632 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3634 MSVCRT_wint_t ret;
3636 MSVCRT__lock_file(file);
3637 ret = MSVCRT__fgetwc_nolock(file);
3638 MSVCRT__unlock_file(file);
3640 return ret;
3643 /*********************************************************************
3644 * _fgetwc_nolock (MSVCRT.@)
3646 MSVCRT_wint_t CDECL MSVCRT__fgetwc_nolock(MSVCRT_FILE* file)
3648 MSVCRT_wint_t ret;
3649 int ch;
3651 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3652 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3653 char *p;
3655 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3656 ch = MSVCRT__fgetc_nolock(file);
3657 if(ch == MSVCRT_EOF) {
3658 ret = MSVCRT_WEOF;
3659 break;
3661 *p = (char)ch;
3663 }else {
3664 char mbs[MSVCRT_MB_LEN_MAX];
3665 int len = 0;
3667 ch = MSVCRT__fgetc_nolock(file);
3668 if(ch != MSVCRT_EOF) {
3669 mbs[0] = (char)ch;
3670 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3671 ch = MSVCRT__fgetc_nolock(file);
3672 if(ch != MSVCRT_EOF) {
3673 mbs[1] = (char)ch;
3674 len = 2;
3676 }else {
3677 len = 1;
3681 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3682 ret = MSVCRT_WEOF;
3685 return ret;
3688 /*********************************************************************
3689 * _getw (MSVCRT.@)
3691 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3693 char *ch;
3694 int i, k;
3695 unsigned int j;
3696 ch = (char *)&i;
3698 MSVCRT__lock_file(file);
3699 for (j=0; j<sizeof(int); j++) {
3700 k = MSVCRT__fgetc_nolock(file);
3701 if (k == MSVCRT_EOF) {
3702 file->_flag |= MSVCRT__IOEOF;
3703 MSVCRT__unlock_file(file);
3704 return EOF;
3706 ch[j] = k;
3709 MSVCRT__unlock_file(file);
3710 return i;
3713 /*********************************************************************
3714 * getwc (MSVCRT.@)
3716 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3718 return MSVCRT_fgetwc(file);
3721 /*********************************************************************
3722 * _fgetwchar (MSVCRT.@)
3724 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3726 return MSVCRT_fgetwc(MSVCRT_stdin);
3729 /*********************************************************************
3730 * getwchar (MSVCRT.@)
3732 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3734 return MSVCRT__fgetwchar();
3737 /*********************************************************************
3738 * fgetws (MSVCRT.@)
3740 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3742 MSVCRT_wint_t cc = MSVCRT_WEOF;
3743 MSVCRT_wchar_t * buf_start = s;
3745 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3746 file,file->_file,s,size);
3748 MSVCRT__lock_file(file);
3750 while ((size >1) && (cc = MSVCRT__fgetwc_nolock(file)) != MSVCRT_WEOF && cc != '\n')
3752 *s++ = cc;
3753 size --;
3755 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3757 TRACE(":nothing read\n");
3758 MSVCRT__unlock_file(file);
3759 return NULL;
3761 if ((cc != MSVCRT_WEOF) && (size > 1))
3762 *s++ = cc;
3763 *s = 0;
3764 TRACE(":got %s\n", debugstr_w(buf_start));
3765 MSVCRT__unlock_file(file);
3766 return buf_start;
3769 /*********************************************************************
3770 * _flsbuf (MSVCRT.@)
3772 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3774 /* Flush output buffer */
3775 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3776 msvcrt_alloc_buffer(file);
3779 if(!(file->_flag & MSVCRT__IOWRT)) {
3780 if(!(file->_flag & MSVCRT__IORW)) {
3781 file->_flag |= MSVCRT__IOERR;
3782 return MSVCRT_EOF;
3784 file->_flag |= MSVCRT__IOWRT;
3786 if(file->_flag & MSVCRT__IOREAD) {
3787 if(!(file->_flag & MSVCRT__IOEOF)) {
3788 file->_flag |= MSVCRT__IOERR;
3789 return MSVCRT_EOF;
3791 file->_cnt = 0;
3792 file->_ptr = file->_base;
3793 file->_flag &= ~(MSVCRT__IOREAD | MSVCRT__IOEOF);
3796 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3797 int res = 0;
3799 if(file->_cnt <= 0) {
3800 res = msvcrt_flush_buffer(file);
3801 if(res)
3802 return res;
3803 file->_flag |= MSVCRT__IOWRT;
3804 file->_cnt=file->_bufsiz;
3806 *file->_ptr++ = c;
3807 file->_cnt--;
3808 return c&0xff;
3809 } else {
3810 unsigned char cc=c;
3811 int len;
3812 /* set _cnt to 0 for unbuffered FILEs */
3813 file->_cnt = 0;
3814 len = MSVCRT__write(file->_file, &cc, 1);
3815 if (len == 1)
3816 return c & 0xff;
3817 file->_flag |= MSVCRT__IOERR;
3818 return MSVCRT_EOF;
3822 /*********************************************************************
3823 * fwrite (MSVCRT.@)
3825 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3827 MSVCRT_size_t ret;
3829 MSVCRT__lock_file(file);
3830 ret = MSVCRT__fwrite_nolock(ptr, size, nmemb, file);
3831 MSVCRT__unlock_file(file);
3833 return ret;
3836 /*********************************************************************
3837 * _fwrite_nolock (MSVCRT.@)
3839 MSVCRT_size_t CDECL MSVCRT__fwrite_nolock(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3841 MSVCRT_size_t wrcnt=size * nmemb;
3842 int written = 0;
3843 if (size == 0)
3844 return 0;
3846 while(wrcnt) {
3847 if(file->_cnt < 0) {
3848 WARN("negative file->_cnt value in %p\n", file);
3849 file->_flag |= MSVCRT__IOERR;
3850 break;
3851 } else if(file->_cnt) {
3852 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3853 memcpy(file->_ptr, ptr, pcnt);
3854 file->_cnt -= pcnt;
3855 file->_ptr += pcnt;
3856 written += pcnt;
3857 wrcnt -= pcnt;
3858 ptr = (const char*)ptr + pcnt;
3859 } else if((file->_flag & MSVCRT__IONBF)
3860 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
3861 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
3862 MSVCRT_size_t pcnt;
3863 int bufsiz;
3865 if(file->_flag & MSVCRT__IONBF)
3866 bufsiz = 1;
3867 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3868 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
3869 else
3870 bufsiz = file->_bufsiz;
3872 pcnt = (wrcnt / bufsiz) * bufsiz;
3874 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
3875 break;
3877 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
3878 file->_flag |= MSVCRT__IOERR;
3879 break;
3881 written += pcnt;
3882 wrcnt -= pcnt;
3883 ptr = (const char*)ptr + pcnt;
3884 } else {
3885 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3886 break;
3887 written++;
3888 wrcnt--;
3889 ptr = (const char*)ptr + 1;
3893 return written / size;
3896 /*********************************************************************
3897 * fputwc (MSVCRT.@)
3899 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3901 MSVCRT_wint_t ret;
3903 MSVCRT__lock_file(file);
3904 ret = MSVCRT__fputwc_nolock(wc, file);
3905 MSVCRT__unlock_file(file);
3907 return ret;
3910 /*********************************************************************
3911 * _fputwc_nolock (MSVCRT.@)
3913 MSVCRT_wint_t CDECL MSVCRT__fputwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3915 MSVCRT_wchar_t mwc=wc;
3916 ioinfo *fdinfo;
3917 MSVCRT_wint_t ret;
3919 fdinfo = get_ioinfo_nolock(file->_file);
3921 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3922 char buf[MSVCRT_MB_LEN_MAX];
3923 int char_len;
3925 char_len = MSVCRT_wctomb(buf, mwc);
3926 if(char_len!=-1 && MSVCRT__fwrite_nolock(buf, char_len, 1, file)==1)
3927 ret = wc;
3928 else
3929 ret = MSVCRT_WEOF;
3930 }else if(MSVCRT__fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
3931 ret = wc;
3932 }else {
3933 ret = MSVCRT_WEOF;
3936 return ret;
3939 /*********************************************************************
3940 * _fputwchar (MSVCRT.@)
3942 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3944 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3947 /*********************************************************************
3948 * _wfsopen (MSVCRT.@)
3950 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
3952 MSVCRT_FILE* file;
3953 int open_flags, stream_flags, fd;
3955 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3957 /* map mode string to open() flags. "man fopen" for possibilities. */
3958 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3959 return NULL;
3961 LOCK_FILES();
3962 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3963 if (fd < 0)
3964 file = NULL;
3965 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3966 != -1)
3967 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3968 else if (file)
3970 file->_flag = 0;
3971 file = NULL;
3974 TRACE(":got (%p)\n",file);
3975 if (fd >= 0 && !file)
3976 MSVCRT__close(fd);
3977 UNLOCK_FILES();
3978 return file;
3981 /*********************************************************************
3982 * _fsopen (MSVCRT.@)
3984 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3986 MSVCRT_FILE *ret;
3987 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3989 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3990 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3991 *MSVCRT__errno() = MSVCRT_EINVAL;
3992 return NULL;
3994 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3996 MSVCRT_free(pathW);
3997 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3998 *MSVCRT__errno() = MSVCRT_EINVAL;
3999 return NULL;
4002 ret = MSVCRT__wfsopen(pathW, modeW, share);
4004 MSVCRT_free(pathW);
4005 MSVCRT_free(modeW);
4006 return ret;
4009 /*********************************************************************
4010 * fopen (MSVCRT.@)
4012 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
4014 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
4017 /*********************************************************************
4018 * fopen_s (MSVCRT.@)
4020 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
4021 const char *filename, const char *mode)
4023 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4024 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4025 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4027 *pFile = MSVCRT_fopen(filename, mode);
4029 if(!*pFile)
4030 return *MSVCRT__errno();
4031 return 0;
4034 /*********************************************************************
4035 * _wfopen (MSVCRT.@)
4037 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
4039 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
4042 /*********************************************************************
4043 * _wfopen_s (MSVCRT.@)
4045 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
4046 const MSVCRT_wchar_t *mode)
4048 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4049 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4050 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4052 *pFile = MSVCRT__wfopen(filename, mode);
4054 if(!*pFile)
4055 return *MSVCRT__errno();
4056 return 0;
4059 /*********************************************************************
4060 * fputc (MSVCRT.@)
4062 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
4064 int ret;
4066 MSVCRT__lock_file(file);
4067 ret = MSVCRT__fputc_nolock(c, file);
4068 MSVCRT__unlock_file(file);
4070 return ret;
4073 /*********************************************************************
4074 * _fputc_nolock (MSVCRT.@)
4076 int CDECL MSVCRT__fputc_nolock(int c, MSVCRT_FILE* file)
4078 int res;
4080 if(file->_cnt>0) {
4081 *file->_ptr++=c;
4082 file->_cnt--;
4083 if (c == '\n')
4085 res = msvcrt_flush_buffer(file);
4086 return res ? res : c;
4088 else {
4089 return c & 0xff;
4091 } else {
4092 res = MSVCRT__flsbuf(c, file);
4093 return res;
4097 /*********************************************************************
4098 * _fputchar (MSVCRT.@)
4100 int CDECL MSVCRT__fputchar(int c)
4102 return MSVCRT_fputc(c, MSVCRT_stdout);
4105 /*********************************************************************
4106 * fread (MSVCRT.@)
4108 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4110 MSVCRT_size_t ret;
4112 MSVCRT__lock_file(file);
4113 ret = MSVCRT__fread_nolock(ptr, size, nmemb, file);
4114 MSVCRT__unlock_file(file);
4116 return ret;
4119 /*********************************************************************
4120 * _fread_nolock (MSVCRT.@)
4122 MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4124 MSVCRT_size_t rcnt=size * nmemb;
4125 MSVCRT_size_t read=0;
4126 MSVCRT_size_t pread=0;
4128 if(!rcnt)
4129 return 0;
4131 /* first buffered data */
4132 if(file->_cnt>0) {
4133 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4134 memcpy(ptr, file->_ptr, pcnt);
4135 file->_cnt -= pcnt;
4136 file->_ptr += pcnt;
4137 read += pcnt ;
4138 rcnt -= pcnt ;
4139 ptr = (char*)ptr + pcnt;
4140 } else if(!(file->_flag & MSVCRT__IOREAD )) {
4141 if(file->_flag & MSVCRT__IORW) {
4142 file->_flag |= MSVCRT__IOREAD;
4143 } else {
4144 return 0;
4148 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4149 msvcrt_alloc_buffer(file);
4151 while(rcnt>0)
4153 int i;
4154 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
4155 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
4156 file->_ptr = file->_base;
4157 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
4158 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4159 if (i > 0 && i < file->_cnt) {
4160 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4161 file->_flag &= ~MSVCRT__IOEOF;
4163 if (i > 0) {
4164 memcpy(ptr, file->_ptr, i);
4165 file->_cnt -= i;
4166 file->_ptr += i;
4168 } else if (rcnt > INT_MAX) {
4169 i = MSVCRT__read(file->_file, ptr, INT_MAX);
4170 } else if (rcnt < MSVCRT_BUFSIZ) {
4171 i = MSVCRT__read(file->_file, ptr, rcnt);
4172 } else {
4173 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
4175 pread += i;
4176 rcnt -= i;
4177 ptr = (char *)ptr+i;
4178 /* expose feof condition in the flags
4179 * MFC tests file->_flag for feof, and doesn't call feof())
4181 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4182 file->_flag |= MSVCRT__IOEOF;
4183 else if (i == -1)
4185 file->_flag |= MSVCRT__IOERR;
4186 pread = 0;
4187 rcnt = 0;
4189 if (i < 1) break;
4191 read+=pread;
4192 return read / size;
4196 /*********************************************************************
4197 * fread_s (MSVCR80.@)
4199 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4200 MSVCRT_size_t count, MSVCRT_FILE *stream)
4202 MSVCRT_size_t ret;
4204 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4205 if(buf && buf_size)
4206 memset(buf, 0, buf_size);
4207 return 0;
4209 if(!elem_size || !count) return 0;
4211 MSVCRT__lock_file(stream);
4212 ret = MSVCRT__fread_nolock_s(buf, buf_size, elem_size, count, stream);
4213 MSVCRT__unlock_file(stream);
4215 return ret;
4218 /*********************************************************************
4219 * _fread_nolock_s (MSVCR80.@)
4221 MSVCRT_size_t CDECL MSVCRT__fread_nolock_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4222 MSVCRT_size_t count, MSVCRT_FILE *stream)
4224 size_t bytes_left, buf_pos;
4226 TRACE("(%p %lu %lu %lu %p)\n", buf, buf_size, elem_size, count, stream);
4228 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4229 if(buf && buf_size)
4230 memset(buf, 0, buf_size);
4231 return 0;
4233 if(!elem_size || !count) return 0;
4234 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4235 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4237 bytes_left = elem_size*count;
4238 buf_pos = 0;
4239 while(bytes_left) {
4240 if(stream->_cnt > 0) {
4241 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4243 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4244 memset(buf, 0, buf_size);
4245 return 0;
4248 MSVCRT__fread_nolock((char*)buf+buf_pos, 1, size, stream);
4249 buf_pos += size;
4250 bytes_left -= size;
4251 }else {
4252 int c = MSVCRT__filbuf(stream);
4254 if(c == EOF)
4255 break;
4257 if(!MSVCRT_CHECK_PMT_ERR(buf_size-buf_pos > 0, MSVCRT_ERANGE)) {
4258 memset(buf, 0, buf_size);
4259 return 0;
4262 ((char*)buf)[buf_pos++] = c;
4263 bytes_left--;
4267 return buf_pos/elem_size;
4270 /*********************************************************************
4271 * _wfreopen (MSVCRT.@)
4274 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4276 int open_flags, stream_flags, fd;
4278 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4280 LOCK_FILES();
4281 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
4282 file = NULL;
4283 else
4285 MSVCRT_fclose(file);
4286 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4287 file = NULL;
4288 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4289 file = NULL;
4290 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4292 file->_flag = 0;
4293 file = NULL;
4296 UNLOCK_FILES();
4297 return file;
4300 /*********************************************************************
4301 * _wfreopen_s (MSVCRT.@)
4303 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4304 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4306 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4307 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4308 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4309 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4311 *pFile = MSVCRT__wfreopen(path, mode, file);
4313 if(!*pFile)
4314 return *MSVCRT__errno();
4315 return 0;
4318 /*********************************************************************
4319 * freopen (MSVCRT.@)
4322 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4324 MSVCRT_FILE *ret;
4325 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4327 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4328 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4330 MSVCRT_free(pathW);
4331 return NULL;
4334 ret = MSVCRT__wfreopen(pathW, modeW, file);
4336 MSVCRT_free(pathW);
4337 MSVCRT_free(modeW);
4338 return ret;
4341 /*********************************************************************
4342 * freopen_s (MSVCRT.@)
4344 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4345 const char *path, const char *mode, MSVCRT_FILE* file)
4347 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4348 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4349 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4350 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4352 *pFile = MSVCRT_freopen(path, mode, file);
4354 if(!*pFile)
4355 return *MSVCRT__errno();
4356 return 0;
4359 /*********************************************************************
4360 * fsetpos (MSVCRT.@)
4362 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4364 int ret;
4366 MSVCRT__lock_file(file);
4367 msvcrt_flush_buffer(file);
4369 /* Reset direction of i/o */
4370 if(file->_flag & MSVCRT__IORW) {
4371 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4374 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
4375 MSVCRT__unlock_file(file);
4376 return ret;
4379 /*********************************************************************
4380 * _ftelli64 (MSVCRT.@)
4382 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4384 __int64 ret;
4386 MSVCRT__lock_file(file);
4387 ret = MSVCRT__ftelli64_nolock(file);
4388 MSVCRT__unlock_file(file);
4390 return ret;
4393 /*********************************************************************
4394 * _ftelli64_nolock (MSVCRT.@)
4396 __int64 CDECL MSVCRT__ftelli64_nolock(MSVCRT_FILE* file)
4398 __int64 pos;
4400 pos = _telli64(file->_file);
4401 if(pos == -1)
4402 return -1;
4403 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4404 if(file->_flag & MSVCRT__IOWRT) {
4405 pos += file->_ptr - file->_base;
4407 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4408 char *p;
4410 for(p=file->_base; p<file->_ptr; p++)
4411 if(*p == '\n')
4412 pos++;
4414 } else if(!file->_cnt) { /* nothing to do */
4415 } else if(MSVCRT__lseeki64(file->_file, 0, SEEK_END)==pos) {
4416 int i;
4418 pos -= file->_cnt;
4419 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4420 for(i=0; i<file->_cnt; i++)
4421 if(file->_ptr[i] == '\n')
4422 pos--;
4424 } else {
4425 char *p;
4427 if(MSVCRT__lseeki64(file->_file, pos, SEEK_SET) != pos)
4428 return -1;
4430 pos -= file->_bufsiz;
4431 pos += file->_ptr - file->_base;
4433 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4434 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4435 pos--;
4437 for(p=file->_base; p<file->_ptr; p++)
4438 if(*p == '\n')
4439 pos++;
4444 return pos;
4447 /*********************************************************************
4448 * ftell (MSVCRT.@)
4450 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4452 return MSVCRT__ftelli64(file);
4455 /*********************************************************************
4456 * _ftell_nolock (MSVCRT.@)
4458 LONG CDECL MSVCRT__ftell_nolock(MSVCRT_FILE* file)
4460 return MSVCRT__ftelli64_nolock(file);
4463 /*********************************************************************
4464 * fgetpos (MSVCRT.@)
4466 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4468 *pos = MSVCRT__ftelli64(file);
4469 if(*pos == -1)
4470 return -1;
4471 return 0;
4474 /*********************************************************************
4475 * fputs (MSVCRT.@)
4477 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4479 MSVCRT_size_t len = strlen(s);
4480 int ret;
4482 MSVCRT__lock_file(file);
4483 ret = MSVCRT__fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4484 MSVCRT__unlock_file(file);
4485 return ret;
4488 /*********************************************************************
4489 * fputws (MSVCRT.@)
4491 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4493 MSVCRT_size_t i, len = strlenW(s);
4494 BOOL tmp_buf;
4495 int ret;
4497 MSVCRT__lock_file(file);
4498 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4499 ret = MSVCRT__fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4500 MSVCRT__unlock_file(file);
4501 return ret;
4504 tmp_buf = add_std_buffer(file);
4505 for (i=0; i<len; i++) {
4506 if(MSVCRT__fputwc_nolock(s[i], file) == MSVCRT_WEOF) {
4507 if(tmp_buf) remove_std_buffer(file);
4508 MSVCRT__unlock_file(file);
4509 return MSVCRT_WEOF;
4513 if(tmp_buf) remove_std_buffer(file);
4514 MSVCRT__unlock_file(file);
4515 return 0;
4518 /*********************************************************************
4519 * getchar (MSVCRT.@)
4521 int CDECL MSVCRT_getchar(void)
4523 return MSVCRT_fgetc(MSVCRT_stdin);
4526 /*********************************************************************
4527 * getc (MSVCRT.@)
4529 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4531 return MSVCRT_fgetc(file);
4534 /*********************************************************************
4535 * gets (MSVCRT.@)
4537 char * CDECL MSVCRT_gets(char *buf)
4539 int cc;
4540 char * buf_start = buf;
4542 MSVCRT__lock_file(MSVCRT_stdin);
4543 for(cc = MSVCRT__fgetc_nolock(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4544 cc = MSVCRT__fgetc_nolock(MSVCRT_stdin))
4545 if(cc != '\r') *buf++ = (char)cc;
4547 *buf = '\0';
4549 TRACE("got '%s'\n", buf_start);
4550 MSVCRT__unlock_file(MSVCRT_stdin);
4551 return buf_start;
4554 /*********************************************************************
4555 * _getws (MSVCRT.@)
4557 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4559 MSVCRT_wint_t cc;
4560 MSVCRT_wchar_t* ws = buf;
4562 MSVCRT__lock_file(MSVCRT_stdin);
4563 for (cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4564 cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin))
4566 if (cc != '\r')
4567 *buf++ = (MSVCRT_wchar_t)cc;
4569 *buf = '\0';
4571 TRACE("got %s\n", debugstr_w(ws));
4572 MSVCRT__unlock_file(MSVCRT_stdin);
4573 return ws;
4576 /*********************************************************************
4577 * putc (MSVCRT.@)
4579 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4581 return MSVCRT_fputc(c, file);
4584 /*********************************************************************
4585 * putchar (MSVCRT.@)
4587 int CDECL MSVCRT_putchar(int c)
4589 return MSVCRT_fputc(c, MSVCRT_stdout);
4592 /*********************************************************************
4593 * _putwch (MSVCRT.@)
4595 int CDECL MSVCRT__putwch(int c)
4597 return MSVCRT_fputwc(c, MSVCRT_stdout);
4600 /*********************************************************************
4601 * puts (MSVCRT.@)
4603 int CDECL MSVCRT_puts(const char *s)
4605 MSVCRT_size_t len = strlen(s);
4606 int ret;
4608 MSVCRT__lock_file(MSVCRT_stdout);
4609 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4610 MSVCRT__unlock_file(MSVCRT_stdout);
4611 return MSVCRT_EOF;
4614 ret = MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4615 MSVCRT__unlock_file(MSVCRT_stdout);
4616 return ret;
4619 /*********************************************************************
4620 * _putws (MSVCRT.@)
4622 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4624 static const MSVCRT_wchar_t nl = '\n';
4625 MSVCRT_size_t len = strlenW(s);
4626 int ret;
4628 MSVCRT__lock_file(MSVCRT_stdout);
4629 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4630 MSVCRT__unlock_file(MSVCRT_stdout);
4631 return MSVCRT_EOF;
4634 ret = MSVCRT__fwrite_nolock(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4635 MSVCRT__unlock_file(MSVCRT_stdout);
4636 return ret;
4639 /*********************************************************************
4640 * remove (MSVCRT.@)
4642 int CDECL MSVCRT_remove(const char *path)
4644 TRACE("(%s)\n",path);
4645 if (DeleteFileA(path))
4646 return 0;
4647 TRACE(":failed (%d)\n",GetLastError());
4648 msvcrt_set_errno(GetLastError());
4649 return -1;
4652 /*********************************************************************
4653 * _wremove (MSVCRT.@)
4655 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4657 TRACE("(%s)\n",debugstr_w(path));
4658 if (DeleteFileW(path))
4659 return 0;
4660 TRACE(":failed (%d)\n",GetLastError());
4661 msvcrt_set_errno(GetLastError());
4662 return -1;
4665 /*********************************************************************
4666 * rename (MSVCRT.@)
4668 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4670 TRACE(":from %s to %s\n",oldpath,newpath);
4671 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4672 return 0;
4673 TRACE(":failed (%d)\n",GetLastError());
4674 msvcrt_set_errno(GetLastError());
4675 return -1;
4678 /*********************************************************************
4679 * _wrename (MSVCRT.@)
4681 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4683 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4684 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4685 return 0;
4686 TRACE(":failed (%d)\n",GetLastError());
4687 msvcrt_set_errno(GetLastError());
4688 return -1;
4691 /*********************************************************************
4692 * setvbuf (MSVCRT.@)
4694 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4696 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4697 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4698 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4700 MSVCRT__lock_file(file);
4702 MSVCRT__fflush_nolock(file);
4703 if(file->_flag & MSVCRT__IOMYBUF)
4704 MSVCRT_free(file->_base);
4705 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4706 file->_cnt = 0;
4708 if(mode == MSVCRT__IONBF) {
4709 file->_flag |= MSVCRT__IONBF;
4710 file->_base = file->_ptr = (char*)&file->_charbuf;
4711 file->_bufsiz = 2;
4712 }else if(buf) {
4713 file->_base = file->_ptr = buf;
4714 file->_flag |= MSVCRT__USERBUF;
4715 file->_bufsiz = size;
4716 }else {
4717 file->_base = file->_ptr = MSVCRT_malloc(size);
4718 if(!file->_base) {
4719 file->_bufsiz = 0;
4720 MSVCRT__unlock_file(file);
4721 return -1;
4724 file->_flag |= MSVCRT__IOMYBUF;
4725 file->_bufsiz = size;
4727 MSVCRT__unlock_file(file);
4728 return 0;
4731 /*********************************************************************
4732 * setbuf (MSVCRT.@)
4734 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4736 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4739 /*********************************************************************
4740 * tmpnam (MSVCRT.@)
4742 char * CDECL MSVCRT_tmpnam(char *s)
4744 char tmpstr[16];
4745 char *p;
4746 int count, size;
4748 if (!s) {
4749 thread_data_t *data = msvcrt_get_thread_data();
4751 if(!data->tmpnam_buffer)
4752 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4754 s = data->tmpnam_buffer;
4757 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4758 p = s + sprintf(s, "\\s%s.", tmpstr);
4759 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4761 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
4762 memcpy(p, tmpstr, size);
4763 p[size] = '\0';
4764 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4765 GetLastError() == ERROR_FILE_NOT_FOUND)
4766 break;
4768 return s;
4771 /*********************************************************************
4772 * _wtmpnam (MSVCRT.@)
4774 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
4776 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
4777 MSVCRT_wchar_t tmpstr[16];
4778 MSVCRT_wchar_t *p;
4779 int count, size;
4780 if (!s) {
4781 thread_data_t *data = msvcrt_get_thread_data();
4783 if(!data->wtmpnam_buffer)
4784 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4786 s = data->wtmpnam_buffer;
4789 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4790 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
4791 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4793 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
4794 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
4795 p[size] = '\0';
4796 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4797 GetLastError() == ERROR_FILE_NOT_FOUND)
4798 break;
4800 return s;
4803 /*********************************************************************
4804 * tmpfile (MSVCRT.@)
4806 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4808 char *filename = MSVCRT_tmpnam(NULL);
4809 int fd;
4810 MSVCRT_FILE* file = NULL;
4812 LOCK_FILES();
4813 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4814 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4815 if (fd != -1 && (file = msvcrt_alloc_fp()))
4817 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4819 file->_flag = 0;
4820 file = NULL;
4822 else file->_tmpfname = MSVCRT__strdup(filename);
4825 if(fd != -1 && !file)
4826 MSVCRT__close(fd);
4827 UNLOCK_FILES();
4828 return file;
4831 /*********************************************************************
4832 * tmpfile_s (MSVCRT.@)
4834 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4836 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4838 *file = MSVCRT_tmpfile();
4839 return 0;
4842 static int puts_clbk_file_a(void *file, int len, const char *str)
4844 return MSVCRT_fwrite(str, sizeof(char), len, file);
4847 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4849 int i, ret;
4851 MSVCRT__lock_file(file);
4853 if(!(get_ioinfo_nolock(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4854 ret = MSVCRT__fwrite_nolock(str, sizeof(MSVCRT_wchar_t), len, file);
4855 MSVCRT__unlock_file(file);
4856 return ret;
4859 for(i=0; i<len; i++) {
4860 if(MSVCRT__fputwc_nolock(str[i], file) == MSVCRT_WEOF) {
4861 MSVCRT__unlock_file(file);
4862 return -1;
4866 MSVCRT__unlock_file(file);
4867 return len;
4870 /*********************************************************************
4871 * vfprintf (MSVCRT.@)
4873 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4875 BOOL tmp_buf;
4876 int ret;
4878 MSVCRT__lock_file(file);
4879 tmp_buf = add_std_buffer(file);
4880 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4881 if(tmp_buf) remove_std_buffer(file);
4882 MSVCRT__unlock_file(file);
4884 return ret;
4887 /*********************************************************************
4888 * vfprintf_s (MSVCRT.@)
4890 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4892 BOOL tmp_buf;
4893 int ret;
4895 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4897 MSVCRT__lock_file(file);
4898 tmp_buf = add_std_buffer(file);
4899 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4900 if(tmp_buf) remove_std_buffer(file);
4901 MSVCRT__unlock_file(file);
4903 return ret;
4906 /*********************************************************************
4907 * vfwprintf (MSVCRT.@)
4909 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4911 BOOL tmp_buf;
4912 int ret;
4914 MSVCRT__lock_file(file);
4915 tmp_buf = add_std_buffer(file);
4916 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4917 if(tmp_buf) remove_std_buffer(file);
4918 MSVCRT__unlock_file(file);
4920 return ret;
4923 /*********************************************************************
4924 * vfwprintf_s (MSVCRT.@)
4926 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4928 BOOL tmp_buf;
4929 int ret;
4931 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4933 MSVCRT__lock_file(file);
4934 tmp_buf = add_std_buffer(file);
4935 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4936 if(tmp_buf) remove_std_buffer(file);
4937 MSVCRT__unlock_file(file);
4939 return ret;
4942 /*********************************************************************
4943 * _vfwprintf_l (MSVCRT.@)
4945 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
4946 MSVCRT__locale_t locale, __ms_va_list valist)
4948 BOOL tmp_buf;
4949 int ret;
4951 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4953 MSVCRT__lock_file(file);
4954 tmp_buf = add_std_buffer(file);
4955 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4956 if(tmp_buf) remove_std_buffer(file);
4957 MSVCRT__unlock_file(file);
4959 return ret;
4962 /*********************************************************************
4963 * vprintf (MSVCRT.@)
4965 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
4967 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
4970 /*********************************************************************
4971 * vprintf_s (MSVCRT.@)
4973 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
4975 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
4978 /*********************************************************************
4979 * vwprintf (MSVCRT.@)
4981 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
4983 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
4986 /*********************************************************************
4987 * vwprintf_s (MSVCRT.@)
4989 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
4991 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
4994 /*********************************************************************
4995 * fprintf (MSVCRT.@)
4997 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
4999 __ms_va_list valist;
5000 int res;
5001 __ms_va_start(valist, format);
5002 res = MSVCRT_vfprintf(file, format, valist);
5003 __ms_va_end(valist);
5004 return res;
5007 /*********************************************************************
5008 * fprintf_s (MSVCRT.@)
5010 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
5012 __ms_va_list valist;
5013 int res;
5014 __ms_va_start(valist, format);
5015 res = MSVCRT_vfprintf_s(file, format, valist);
5016 __ms_va_end(valist);
5017 return res;
5020 /*********************************************************************
5021 * fwprintf (MSVCRT.@)
5023 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5025 __ms_va_list valist;
5026 int res;
5027 __ms_va_start(valist, format);
5028 res = MSVCRT_vfwprintf(file, format, valist);
5029 __ms_va_end(valist);
5030 return res;
5033 /*********************************************************************
5034 * fwprintf_s (MSVCRT.@)
5036 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5038 __ms_va_list valist;
5039 int res;
5040 __ms_va_start(valist, format);
5041 res = MSVCRT_vfwprintf_s(file, format, valist);
5042 __ms_va_end(valist);
5043 return res;
5046 /*********************************************************************
5047 * _fwprintf_l (MSVCRT.@)
5049 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
5051 __ms_va_list valist;
5052 int res;
5053 __ms_va_start(valist, locale);
5054 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
5055 __ms_va_end(valist);
5056 return res;
5059 /*********************************************************************
5060 * printf (MSVCRT.@)
5062 int CDECL MSVCRT_printf(const char *format, ...)
5064 __ms_va_list valist;
5065 int res;
5066 __ms_va_start(valist, format);
5067 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
5068 __ms_va_end(valist);
5069 return res;
5072 /*********************************************************************
5073 * printf_s (MSVCRT.@)
5075 int CDECL MSVCRT_printf_s(const char *format, ...)
5077 __ms_va_list valist;
5078 int res;
5079 __ms_va_start(valist, format);
5080 res = MSVCRT_vprintf_s(format, valist);
5081 __ms_va_end(valist);
5082 return res;
5085 /*********************************************************************
5086 * ungetc (MSVCRT.@)
5088 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
5090 int ret;
5092 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5094 MSVCRT__lock_file(file);
5095 ret = MSVCRT__ungetc_nolock(c, file);
5096 MSVCRT__unlock_file(file);
5098 return ret;
5101 /*********************************************************************
5102 * _ungetc_nolock (MSVCRT.@)
5104 int CDECL MSVCRT__ungetc_nolock(int c, MSVCRT_FILE * file)
5106 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5108 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
5109 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
5110 return MSVCRT_EOF;
5112 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
5113 && msvcrt_alloc_buffer(file))
5114 || (!file->_cnt && file->_ptr==file->_base))
5115 file->_ptr++;
5117 if(file->_ptr>file->_base) {
5118 file->_ptr--;
5119 if(file->_flag & MSVCRT__IOSTRG) {
5120 if(*file->_ptr != c) {
5121 file->_ptr++;
5122 return MSVCRT_EOF;
5124 }else {
5125 *file->_ptr = c;
5127 file->_cnt++;
5128 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
5129 file->_flag |= MSVCRT__IOREAD;
5130 return c;
5133 return MSVCRT_EOF;
5136 /*********************************************************************
5137 * ungetwc (MSVCRT.@)
5139 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5141 MSVCRT_wint_t ret;
5143 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5145 MSVCRT__lock_file(file);
5146 ret = MSVCRT__ungetwc_nolock(wc, file);
5147 MSVCRT__unlock_file(file);
5149 return ret;
5152 /*********************************************************************
5153 * _ungetwc_nolock (MSVCRT.@)
5155 MSVCRT_wint_t CDECL MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5157 MSVCRT_wchar_t mwc = wc;
5159 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5160 if (wc == MSVCRT_WEOF)
5161 return MSVCRT_WEOF;
5163 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5164 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5165 unsigned char * pp = (unsigned char *)&mwc;
5166 int i;
5168 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
5169 if(pp[i] != MSVCRT__ungetc_nolock(pp[i],file))
5170 return MSVCRT_WEOF;
5172 }else {
5173 char mbs[MSVCRT_MB_LEN_MAX];
5174 int len;
5176 len = MSVCRT_wctomb(mbs, mwc);
5177 if(len == -1)
5178 return MSVCRT_WEOF;
5180 for(len--; len>=0; len--) {
5181 if(mbs[len] != MSVCRT__ungetc_nolock(mbs[len], file))
5182 return MSVCRT_WEOF;
5186 return mwc;
5189 /*********************************************************************
5190 * wprintf (MSVCRT.@)
5192 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
5194 __ms_va_list valist;
5195 int res;
5196 __ms_va_start(valist, format);
5197 res = MSVCRT_vwprintf(format, valist);
5198 __ms_va_end(valist);
5199 return res;
5202 /*********************************************************************
5203 * wprintf_s (MSVCRT.@)
5205 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
5207 __ms_va_list valist;
5208 int res;
5209 __ms_va_start(valist, format);
5210 res = MSVCRT_vwprintf_s(format, valist);
5211 __ms_va_end(valist);
5212 return res;
5215 /*********************************************************************
5216 * _getmaxstdio (MSVCRT.@)
5218 int CDECL MSVCRT__getmaxstdio(void)
5220 return MSVCRT_max_streams;
5223 /*********************************************************************
5224 * _setmaxstdio (MSVCRT.@)
5226 int CDECL MSVCRT__setmaxstdio(int newmax)
5228 TRACE("%d\n", newmax);
5230 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5231 return -1;
5233 MSVCRT_max_streams = newmax;
5234 return MSVCRT_max_streams;