psapi: Implement semi-stub for K32EnumProcessModulesEx.
[wine.git] / dlls / msvcrt / file.c
blobff1f5d635bfe78bfa8aff76321e7b3a14befa99e
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__IOMYBUF | MSVCRT__USERBUF)) {
610 int cnt=file->_ptr-file->_base;
611 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
612 file->_flag |= MSVCRT__IOERR;
613 return MSVCRT_EOF;
615 file->_ptr=file->_base;
616 file->_cnt=0;
618 return 0;
621 /*********************************************************************
622 * _isatty (MSVCRT.@)
624 int CDECL MSVCRT__isatty(int fd)
626 TRACE(":fd (%d)\n",fd);
628 return get_ioinfo_nolock(fd)->wxflag & WX_TTY;
631 /* INTERNAL: Allocate stdio file buffer */
632 static BOOL msvcrt_alloc_buffer(MSVCRT_FILE* file)
634 if((file->_file==MSVCRT_STDOUT_FILENO || file->_file==MSVCRT_STDERR_FILENO)
635 && MSVCRT__isatty(file->_file))
636 return FALSE;
638 file->_base = MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ,1);
639 if(file->_base) {
640 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
641 file->_flag |= MSVCRT__IOMYBUF;
642 } else {
643 file->_base = (char*)(&file->_charbuf);
644 file->_bufsiz = 2;
645 file->_flag |= MSVCRT__IONBF;
647 file->_ptr = file->_base;
648 file->_cnt = 0;
649 return TRUE;
652 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
653 static BOOL add_std_buffer(MSVCRT_FILE *file)
655 static char buffers[2][MSVCRT_BUFSIZ];
657 if((file->_file!=MSVCRT_STDOUT_FILENO && file->_file!=MSVCRT_STDERR_FILENO)
658 || (file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
659 || !MSVCRT__isatty(file->_file))
660 return FALSE;
662 file->_ptr = file->_base = buffers[file->_file == MSVCRT_STDOUT_FILENO ? 0 : 1];
663 file->_bufsiz = file->_cnt = MSVCRT_BUFSIZ;
664 file->_flag |= MSVCRT__USERBUF;
665 return TRUE;
668 /* INTERNAL: Removes temporary buffer from stdout or stderr */
669 /* Only call this function when add_std_buffer returned TRUE */
670 static void remove_std_buffer(MSVCRT_FILE *file)
672 msvcrt_flush_buffer(file);
673 file->_ptr = file->_base = NULL;
674 file->_bufsiz = file->_cnt = 0;
675 file->_flag &= ~MSVCRT__USERBUF;
678 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
679 static int msvcrt_int_to_base32(int num, char *str)
681 char *p;
682 int n = num;
683 int digits = 0;
685 while (n != 0)
687 n >>= 5;
688 digits++;
690 p = str + digits;
691 *p = 0;
692 while (--p >= str)
694 *p = (num & 31) + '0';
695 if (*p > '9')
696 *p += ('a' - '0' - 10);
697 num >>= 5;
700 return digits;
703 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
704 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
706 MSVCRT_wchar_t *p;
707 int n = num;
708 int digits = 0;
710 while (n != 0)
712 n >>= 5;
713 digits++;
715 p = str + digits;
716 *p = 0;
717 while (--p >= str)
719 *p = (num & 31) + '0';
720 if (*p > '9')
721 *p += ('a' - '0' - 10);
722 num >>= 5;
725 return digits;
728 /*********************************************************************
729 * __iob_func(MSVCRT.@)
731 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
733 return &MSVCRT__iob[0];
736 /*********************************************************************
737 * _access (MSVCRT.@)
739 int CDECL MSVCRT__access(const char *filename, int mode)
741 DWORD attr = GetFileAttributesA(filename);
743 TRACE("(%s,%d) %d\n",filename,mode,attr);
745 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
747 msvcrt_set_errno(GetLastError());
748 return -1;
750 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
752 msvcrt_set_errno(ERROR_ACCESS_DENIED);
753 return -1;
755 return 0;
758 /*********************************************************************
759 * _access_s (MSVCRT.@)
761 int CDECL MSVCRT__access_s(const char *filename, int mode)
763 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
764 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
766 if (MSVCRT__access(filename, mode) == -1)
767 return *MSVCRT__errno();
768 return 0;
771 /*********************************************************************
772 * _waccess (MSVCRT.@)
774 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
776 DWORD attr = GetFileAttributesW(filename);
778 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
780 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
782 msvcrt_set_errno(GetLastError());
783 return -1;
785 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
787 msvcrt_set_errno(ERROR_ACCESS_DENIED);
788 return -1;
790 return 0;
793 /*********************************************************************
794 * _waccess_s (MSVCRT.@)
796 int CDECL MSVCRT__waccess_s(const MSVCRT_wchar_t *filename, int mode)
798 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
799 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
801 if (MSVCRT__waccess(filename, mode) == -1)
802 return *MSVCRT__errno();
803 return 0;
806 /*********************************************************************
807 * _chmod (MSVCRT.@)
809 int CDECL MSVCRT__chmod(const char *path, int flags)
811 DWORD oldFlags = GetFileAttributesA(path);
813 if (oldFlags != INVALID_FILE_ATTRIBUTES)
815 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
816 oldFlags | FILE_ATTRIBUTE_READONLY;
818 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
819 return 0;
821 msvcrt_set_errno(GetLastError());
822 return -1;
825 /*********************************************************************
826 * _wchmod (MSVCRT.@)
828 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
830 DWORD oldFlags = GetFileAttributesW(path);
832 if (oldFlags != INVALID_FILE_ATTRIBUTES)
834 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
835 oldFlags | FILE_ATTRIBUTE_READONLY;
837 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
838 return 0;
840 msvcrt_set_errno(GetLastError());
841 return -1;
844 /*********************************************************************
845 * _unlink (MSVCRT.@)
847 int CDECL MSVCRT__unlink(const char *path)
849 TRACE("%s\n",debugstr_a(path));
850 if(DeleteFileA(path))
851 return 0;
852 TRACE("failed (%d)\n",GetLastError());
853 msvcrt_set_errno(GetLastError());
854 return -1;
857 /*********************************************************************
858 * _wunlink (MSVCRT.@)
860 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
862 TRACE("(%s)\n",debugstr_w(path));
863 if(DeleteFileW(path))
864 return 0;
865 TRACE("failed (%d)\n",GetLastError());
866 msvcrt_set_errno(GetLastError());
867 return -1;
870 /*********************************************************************
871 * _commit (MSVCRT.@)
873 int CDECL MSVCRT__commit(int fd)
875 ioinfo *info = get_ioinfo(fd);
876 int ret;
878 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
880 if (info->handle == INVALID_HANDLE_VALUE)
881 ret = -1;
882 else if (!FlushFileBuffers(info->handle))
884 if (GetLastError() == ERROR_INVALID_HANDLE)
886 /* FlushFileBuffers fails for console handles
887 * so we ignore this error.
889 ret = 0;
891 else
893 TRACE(":failed-last error (%d)\n",GetLastError());
894 msvcrt_set_errno(GetLastError());
895 ret = -1;
898 else
900 TRACE(":ok\n");
901 ret = 0;
904 release_ioinfo(info);
905 return ret;
908 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
909 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
911 /* INTERNAL: Flush all stream buffer */
912 static int msvcrt_flush_all_buffers(int mask)
914 int i, num_flushed = 0;
915 MSVCRT_FILE *file;
917 LOCK_FILES();
918 for (i = 0; i < MSVCRT_stream_idx; i++) {
919 file = msvcrt_get_file(i);
921 if (file->_flag)
923 if(file->_flag & mask) {
924 MSVCRT_fflush(file);
925 num_flushed++;
929 UNLOCK_FILES();
931 TRACE(":flushed (%d) handles\n",num_flushed);
932 return num_flushed;
935 /*********************************************************************
936 * _flushall (MSVCRT.@)
938 int CDECL MSVCRT__flushall(void)
940 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
943 /*********************************************************************
944 * fflush (MSVCRT.@)
946 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
948 int ret;
950 if(!file) {
951 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
952 ret = 0;
953 } else {
954 MSVCRT__lock_file(file);
955 ret = MSVCRT__fflush_nolock(file);
956 MSVCRT__unlock_file(file);
959 return ret;
962 /*********************************************************************
963 * _fflush_nolock (MSVCRT.@)
965 int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file)
967 if(!file) {
968 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
969 } else if(file->_flag & MSVCRT__IOWRT) {
970 int res;
972 res = msvcrt_flush_buffer(file);
973 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
974 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
976 return res;
977 } else if(file->_flag & MSVCRT__IOREAD) {
978 file->_cnt = 0;
979 file->_ptr = file->_base;
981 return 0;
983 return 0;
986 /*********************************************************************
987 * _close (MSVCRT.@)
989 int CDECL MSVCRT__close(int fd)
991 ioinfo *info = get_ioinfo(fd);
992 int ret;
994 LOCK_FILES();
995 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
996 if (!(info->wxflag & WX_OPEN)) {
997 ret = -1;
998 } else {
999 ret = CloseHandle(info->handle) ? 0 : -1;
1000 msvcrt_free_fd(fd);
1001 if (ret) {
1002 WARN(":failed-last error (%d)\n",GetLastError());
1003 msvcrt_set_errno(GetLastError());
1006 UNLOCK_FILES();
1007 release_ioinfo(info);
1008 return ret;
1011 /*********************************************************************
1012 * _dup2 (MSVCRT.@)
1013 * NOTES
1014 * MSDN isn't clear on this point, but the remarks for _pipe
1015 * indicate file descriptors duplicated with _dup and _dup2 are always
1016 * inheritable.
1018 int CDECL MSVCRT__dup2(int od, int nd)
1020 int ret;
1022 TRACE("(od=%d, nd=%d)\n", od, nd);
1023 LOCK_FILES();
1024 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
1026 HANDLE handle;
1028 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od)->handle,
1029 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1031 int wxflag = get_ioinfo_nolock(od)->wxflag & ~MSVCRT__O_NOINHERIT;
1033 if (msvcrt_is_valid_fd(nd))
1034 MSVCRT__close(nd);
1035 ret = msvcrt_set_fd(handle, wxflag, nd);
1036 if (ret == -1)
1038 CloseHandle(handle);
1039 *MSVCRT__errno() = MSVCRT_EMFILE;
1041 else
1043 /* _dup2 returns 0, not nd, on success */
1044 ret = 0;
1047 else
1049 ret = -1;
1050 msvcrt_set_errno(GetLastError());
1053 else
1055 *MSVCRT__errno() = MSVCRT_EBADF;
1056 ret = -1;
1058 UNLOCK_FILES();
1059 return ret;
1062 /*********************************************************************
1063 * _dup (MSVCRT.@)
1065 int CDECL MSVCRT__dup(int od)
1067 int fd, ret;
1069 LOCK_FILES();
1070 fd = MSVCRT_fdstart;
1071 if (MSVCRT__dup2(od, fd) == 0)
1072 ret = fd;
1073 else
1074 ret = -1;
1075 UNLOCK_FILES();
1076 return ret;
1079 /*********************************************************************
1080 * _eof (MSVCRT.@)
1082 int CDECL MSVCRT__eof(int fd)
1084 ioinfo *info = get_ioinfo(fd);
1085 DWORD curpos,endpos;
1086 LONG hcurpos,hendpos;
1088 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1090 if (info->handle == INVALID_HANDLE_VALUE)
1092 release_ioinfo(info);
1093 return -1;
1096 if (info->wxflag & WX_ATEOF)
1098 release_ioinfo(info);
1099 return TRUE;
1102 /* Otherwise we do it the hard way */
1103 hcurpos = hendpos = 0;
1104 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1105 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1107 if (curpos == endpos && hcurpos == hendpos)
1109 /* FIXME: shouldn't WX_ATEOF be set here? */
1110 release_ioinfo(info);
1111 return TRUE;
1114 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1115 release_ioinfo(info);
1116 return FALSE;
1119 /*********************************************************************
1120 * _fcloseall (MSVCRT.@)
1122 int CDECL MSVCRT__fcloseall(void)
1124 int num_closed = 0, i;
1125 MSVCRT_FILE *file;
1127 LOCK_FILES();
1128 for (i = 3; i < MSVCRT_stream_idx; i++) {
1129 file = msvcrt_get_file(i);
1131 if (file->_flag && !MSVCRT_fclose(file))
1132 num_closed++;
1134 UNLOCK_FILES();
1136 TRACE(":closed (%d) handles\n",num_closed);
1137 return num_closed;
1140 /* free everything on process exit */
1141 void msvcrt_free_io(void)
1143 unsigned int i;
1144 int j;
1146 MSVCRT__flushall();
1147 MSVCRT__fcloseall();
1149 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1151 if(!MSVCRT___pioinfo[i])
1152 continue;
1154 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1156 if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
1157 DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
1159 MSVCRT_free(MSVCRT___pioinfo[i]);
1162 for(j=0; j<MSVCRT_stream_idx; j++)
1164 MSVCRT_FILE *file = msvcrt_get_file(j);
1165 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1167 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1168 DeleteCriticalSection(&((file_crit*)file)->crit);
1172 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1173 MSVCRT_free(MSVCRT_fstream[i]);
1175 DeleteCriticalSection(&MSVCRT_file_cs);
1178 /*********************************************************************
1179 * _lseeki64 (MSVCRT.@)
1181 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1183 ioinfo *info = get_ioinfo(fd);
1184 LARGE_INTEGER ofs;
1186 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1188 if (info->handle == INVALID_HANDLE_VALUE)
1190 release_ioinfo(info);
1191 return -1;
1194 if (whence < 0 || whence > 2)
1196 release_ioinfo(info);
1197 *MSVCRT__errno() = MSVCRT_EINVAL;
1198 return -1;
1201 TRACE(":fd (%d) to %s pos %s\n",
1202 fd,wine_dbgstr_longlong(offset),
1203 (whence==SEEK_SET)?"SEEK_SET":
1204 (whence==SEEK_CUR)?"SEEK_CUR":
1205 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1207 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1208 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1209 ofs.QuadPart = offset;
1210 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1211 GetLastError() == ERROR_SUCCESS)
1213 info->wxflag &= ~WX_ATEOF;
1214 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1216 release_ioinfo(info);
1217 return ofs.QuadPart;
1219 release_ioinfo(info);
1220 TRACE(":error-last error (%d)\n",GetLastError());
1221 msvcrt_set_errno(GetLastError());
1222 return -1;
1225 /*********************************************************************
1226 * _lseek (MSVCRT.@)
1228 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1230 return MSVCRT__lseeki64(fd, offset, whence);
1233 /*********************************************************************
1234 * _lock_file (MSVCRT.@)
1236 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1238 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1239 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1240 else
1241 EnterCriticalSection(&((file_crit*)file)->crit);
1244 /*********************************************************************
1245 * _unlock_file (MSVCRT.@)
1247 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1249 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1250 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1251 else
1252 LeaveCriticalSection(&((file_crit*)file)->crit);
1255 /*********************************************************************
1256 * _locking (MSVCRT.@)
1258 * This is untested; the underlying LockFile doesn't work yet.
1260 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1262 ioinfo *info = get_ioinfo(fd);
1263 BOOL ret;
1264 DWORD cur_locn;
1266 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1267 if (info->handle == INVALID_HANDLE_VALUE)
1269 release_ioinfo(info);
1270 return -1;
1273 if (mode < 0 || mode > 4)
1275 release_ioinfo(info);
1276 *MSVCRT__errno() = MSVCRT_EINVAL;
1277 return -1;
1280 TRACE(":fd (%d) by 0x%08x mode %s\n",
1281 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1282 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1283 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1284 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1285 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1286 "UNKNOWN");
1288 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1290 release_ioinfo(info);
1291 FIXME ("Seek failed\n");
1292 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1293 return -1;
1295 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1297 int nretry = 10;
1298 ret = 1; /* just to satisfy gcc */
1299 while (nretry--)
1301 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1302 if (ret) break;
1303 Sleep(1);
1306 else if (mode == MSVCRT__LK_UNLCK)
1307 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1308 else
1309 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1310 /* FIXME - what about error settings? */
1311 release_ioinfo(info);
1312 return ret ? 0 : -1;
1315 /*********************************************************************
1316 * _fseeki64 (MSVCRT.@)
1318 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1320 int ret;
1322 MSVCRT__lock_file(file);
1323 ret = MSVCRT__fseeki64_nolock(file, offset, whence);
1324 MSVCRT__unlock_file(file);
1326 return ret;
1329 /*********************************************************************
1330 * _fseeki64_nolock (MSVCRT.@)
1332 int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
1334 int ret;
1336 /* Flush output if needed */
1337 if(file->_flag & MSVCRT__IOWRT)
1338 msvcrt_flush_buffer(file);
1340 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1341 whence = SEEK_SET;
1342 offset += MSVCRT__ftelli64_nolock(file);
1345 /* Discard buffered input */
1346 file->_cnt = 0;
1347 file->_ptr = file->_base;
1348 /* Reset direction of i/o */
1349 if(file->_flag & MSVCRT__IORW) {
1350 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1352 /* Clear end of file flag */
1353 file->_flag &= ~MSVCRT__IOEOF;
1354 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1356 return ret;
1359 /*********************************************************************
1360 * fseek (MSVCRT.@)
1362 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1364 return MSVCRT__fseeki64( file, offset, whence );
1367 /*********************************************************************
1368 * _fseek_nolock (MSVCRT.@)
1370 int CDECL MSVCRT__fseek_nolock(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1372 return MSVCRT__fseeki64_nolock( file, offset, whence );
1375 /*********************************************************************
1376 * _chsize_s (MSVCRT.@)
1378 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1380 ioinfo *info;
1381 __int64 cur, pos;
1382 BOOL ret = FALSE;
1384 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1386 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1389 info = get_ioinfo(fd);
1390 if (info->handle != INVALID_HANDLE_VALUE)
1392 /* save the current file pointer */
1393 cur = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1394 if (cur >= 0)
1396 pos = MSVCRT__lseeki64(fd, size, SEEK_SET);
1397 if (pos >= 0)
1399 ret = SetEndOfFile(info->handle);
1400 if (!ret) msvcrt_set_errno(GetLastError());
1403 /* restore the file pointer */
1404 MSVCRT__lseeki64(fd, cur, SEEK_SET);
1408 release_ioinfo(info);
1409 return ret ? 0 : *MSVCRT__errno();
1412 /*********************************************************************
1413 * _chsize (MSVCRT.@)
1415 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1417 /* _chsize_s returns errno on failure but _chsize should return -1 */
1418 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1421 /*********************************************************************
1422 * clearerr (MSVCRT.@)
1424 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1426 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1428 MSVCRT__lock_file(file);
1429 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1430 MSVCRT__unlock_file(file);
1433 /*********************************************************************
1434 * rewind (MSVCRT.@)
1436 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1438 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1440 MSVCRT__lock_file(file);
1441 MSVCRT__fseek_nolock(file, 0L, SEEK_SET);
1442 MSVCRT_clearerr(file);
1443 MSVCRT__unlock_file(file);
1446 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1448 int plus = strchrW(mode, '+') != NULL;
1450 TRACE("%s\n", debugstr_w(mode));
1452 while(*mode == ' ') mode++;
1454 switch(*mode++)
1456 case 'R': case 'r':
1457 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1458 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1459 break;
1460 case 'W': case 'w':
1461 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1462 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1463 break;
1464 case 'A': case 'a':
1465 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1466 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1467 break;
1468 default:
1469 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1470 return -1;
1473 *stream_flags |= MSVCRT__commode;
1475 while (*mode && *mode!=',')
1476 switch (*mode++)
1478 case 'B': case 'b':
1479 *open_flags |= MSVCRT__O_BINARY;
1480 *open_flags &= ~MSVCRT__O_TEXT;
1481 break;
1482 case 't':
1483 *open_flags |= MSVCRT__O_TEXT;
1484 *open_flags &= ~MSVCRT__O_BINARY;
1485 break;
1486 case 'D':
1487 *open_flags |= MSVCRT__O_TEMPORARY;
1488 break;
1489 case 'T':
1490 *open_flags |= MSVCRT__O_SHORT_LIVED;
1491 break;
1492 case 'c':
1493 *stream_flags |= MSVCRT__IOCOMMIT;
1494 break;
1495 case 'n':
1496 *stream_flags &= ~MSVCRT__IOCOMMIT;
1497 break;
1498 case 'N':
1499 *open_flags |= MSVCRT__O_NOINHERIT;
1500 break;
1501 case '+':
1502 case ' ':
1503 case 'a':
1504 case 'w':
1505 break;
1506 case 'S':
1507 case 'R':
1508 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1509 break;
1510 default:
1511 ERR("incorrect mode flag: %c\n", mode[-1]);
1512 break;
1515 if(*mode == ',')
1517 static const WCHAR ccs[] = {'c','c','s'};
1518 static const WCHAR utf8[] = {'u','t','f','-','8'};
1519 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1520 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1522 mode++;
1523 while(*mode == ' ') mode++;
1524 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1525 return -1;
1526 mode += sizeof(ccs)/sizeof(ccs[0]);
1527 while(*mode == ' ') mode++;
1528 if(!MSVCRT_CHECK_PMT(*mode == '='))
1529 return -1;
1530 mode++;
1531 while(*mode == ' ') mode++;
1533 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1535 *open_flags |= MSVCRT__O_U8TEXT;
1536 mode += sizeof(utf8)/sizeof(utf8[0]);
1538 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1540 *open_flags |= MSVCRT__O_U16TEXT;
1541 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1543 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1545 *open_flags |= MSVCRT__O_WTEXT;
1546 mode += sizeof(unicode)/sizeof(unicode[0]);
1548 else
1550 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1551 return -1;
1554 while(*mode == ' ') mode++;
1557 if(!MSVCRT_CHECK_PMT(*mode == 0))
1558 return -1;
1559 return 0;
1562 /*********************************************************************
1563 * _fdopen (MSVCRT.@)
1565 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1567 MSVCRT_FILE *ret;
1568 MSVCRT_wchar_t *modeW = NULL;
1570 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1572 ret = MSVCRT__wfdopen(fd, modeW);
1574 MSVCRT_free(modeW);
1575 return ret;
1578 /*********************************************************************
1579 * _wfdopen (MSVCRT.@)
1581 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1583 int open_flags, stream_flags;
1584 MSVCRT_FILE* file;
1586 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1588 LOCK_FILES();
1589 if (!(file = msvcrt_alloc_fp()))
1590 file = NULL;
1591 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1593 file->_flag = 0;
1594 file = NULL;
1596 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1597 UNLOCK_FILES();
1599 return file;
1602 /*********************************************************************
1603 * _filelength (MSVCRT.@)
1605 LONG CDECL MSVCRT__filelength(int fd)
1607 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1608 if (curPos != -1)
1610 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1611 if (endPos != -1)
1613 if (endPos != curPos)
1614 MSVCRT__lseek(fd, curPos, SEEK_SET);
1615 return endPos;
1618 return -1;
1621 /*********************************************************************
1622 * _filelengthi64 (MSVCRT.@)
1624 __int64 CDECL MSVCRT__filelengthi64(int fd)
1626 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1627 if (curPos != -1)
1629 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1630 if (endPos != -1)
1632 if (endPos != curPos)
1633 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1634 return endPos;
1637 return -1;
1640 /*********************************************************************
1641 * _fileno (MSVCRT.@)
1643 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1645 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1646 return file->_file;
1649 /*********************************************************************
1650 * _fstat64 (MSVCRT.@)
1652 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1654 ioinfo *info = get_ioinfo(fd);
1655 DWORD dw;
1656 DWORD type;
1657 BY_HANDLE_FILE_INFORMATION hfi;
1659 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1660 if (info->handle == INVALID_HANDLE_VALUE)
1662 release_ioinfo(info);
1663 return -1;
1666 if (!buf)
1668 WARN(":failed-NULL buf\n");
1669 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1670 release_ioinfo(info);
1671 return -1;
1674 memset(&hfi, 0, sizeof(hfi));
1675 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1676 type = GetFileType(info->handle);
1677 if (type == FILE_TYPE_PIPE)
1679 buf->st_dev = buf->st_rdev = fd;
1680 buf->st_mode = S_IFIFO;
1681 buf->st_nlink = 1;
1683 else if (type == FILE_TYPE_CHAR)
1685 buf->st_dev = buf->st_rdev = fd;
1686 buf->st_mode = S_IFCHR;
1687 buf->st_nlink = 1;
1689 else /* FILE_TYPE_DISK etc. */
1691 if (!GetFileInformationByHandle(info->handle, &hfi))
1693 WARN(":failed-last error (%d)\n",GetLastError());
1694 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1695 release_ioinfo(info);
1696 return -1;
1698 buf->st_mode = S_IFREG | 0444;
1699 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1700 buf->st_mode |= 0222;
1701 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1702 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1703 buf->st_atime = dw;
1704 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1705 buf->st_mtime = buf->st_ctime = dw;
1706 buf->st_nlink = hfi.nNumberOfLinks;
1708 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1709 buf->st_mode);
1710 release_ioinfo(info);
1711 return 0;
1714 /*********************************************************************
1715 * _fstati64 (MSVCRT.@)
1717 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1719 int ret;
1720 struct MSVCRT__stat64 buf64;
1722 ret = MSVCRT__fstat64(fd, &buf64);
1723 if (!ret)
1724 msvcrt_stat64_to_stati64(&buf64, buf);
1725 return ret;
1728 /*********************************************************************
1729 * _fstat (MSVCRT.@)
1731 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1732 { int ret;
1733 struct MSVCRT__stat64 buf64;
1735 ret = MSVCRT__fstat64(fd, &buf64);
1736 if (!ret)
1737 msvcrt_stat64_to_stat(&buf64, buf);
1738 return ret;
1741 /*********************************************************************
1742 * _fstat32 (MSVCR80.@)
1744 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1746 int ret;
1747 struct MSVCRT__stat64 buf64;
1749 ret = MSVCRT__fstat64(fd, &buf64);
1750 if (!ret)
1751 msvcrt_stat64_to_stat32(&buf64, buf);
1752 return ret;
1755 /*********************************************************************
1756 * _fstat64i32 (MSVCR80.@)
1758 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1760 int ret;
1761 struct MSVCRT__stat64 buf64;
1763 ret = MSVCRT__fstat64(fd, &buf64);
1764 if (!ret)
1765 msvcrt_stat64_to_stat64i32(&buf64, buf);
1766 return ret;
1769 /*********************************************************************
1770 * _futime64 (MSVCRT.@)
1772 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1774 HANDLE hand = msvcrt_fdtoh(fd);
1775 FILETIME at, wt;
1777 if (!t)
1779 time_to_filetime( MSVCRT__time64(NULL), &at );
1780 wt = at;
1782 else
1784 time_to_filetime( t->actime, &at );
1785 time_to_filetime( t->modtime, &wt );
1788 if (!SetFileTime(hand, NULL, &at, &wt))
1790 msvcrt_set_errno(GetLastError());
1791 return -1 ;
1793 return 0;
1796 /*********************************************************************
1797 * _futime32 (MSVCRT.@)
1799 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1801 if (t)
1803 struct MSVCRT___utimbuf64 t64;
1804 t64.actime = t->actime;
1805 t64.modtime = t->modtime;
1806 return _futime64( fd, &t64 );
1808 else
1809 return _futime64( fd, NULL );
1812 /*********************************************************************
1813 * _futime (MSVCRT.@)
1815 #ifdef _WIN64
1816 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1818 return _futime64( fd, t );
1820 #else
1821 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1823 return _futime32( fd, t );
1825 #endif
1827 /*********************************************************************
1828 * _get_osfhandle (MSVCRT.@)
1830 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1832 HANDLE hand = msvcrt_fdtoh(fd);
1833 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1835 return (MSVCRT_intptr_t)hand;
1838 /*********************************************************************
1839 * _mktemp_s (MSVCRT.@)
1841 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1843 DWORD len, xno, id;
1845 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1846 return MSVCRT_EINVAL;
1848 for(len=0; len<size; len++)
1849 if(!pattern[len])
1850 break;
1851 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1852 if(size)
1853 pattern[0] = 0;
1854 return MSVCRT_EINVAL;
1857 for(xno=1; xno<=6; xno++)
1858 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1859 return MSVCRT_EINVAL;
1861 id = GetCurrentProcessId();
1862 for(xno=1; xno<6; xno++) {
1863 pattern[len-xno] = id%10 + '0';
1864 id /= 10;
1867 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1868 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1869 return 0;
1872 pattern[0] = 0;
1873 *MSVCRT__errno() = MSVCRT_EEXIST;
1874 return MSVCRT_EEXIST;
1877 /*********************************************************************
1878 * _mktemp (MSVCRT.@)
1880 char * CDECL MSVCRT__mktemp(char *pattern)
1882 int numX = 0;
1883 char *retVal = pattern;
1884 int id;
1885 char letter = 'a';
1887 if(!pattern)
1888 return NULL;
1890 while(*pattern)
1891 numX = (*pattern++ == 'X')? numX + 1 : 0;
1892 if (numX < 6)
1893 return NULL;
1894 pattern--;
1895 id = GetCurrentProcessId();
1896 numX = 6;
1897 while(numX--)
1899 int tempNum = id / 10;
1900 *pattern-- = id - (tempNum * 10) + '0';
1901 id = tempNum;
1903 pattern++;
1906 *pattern = letter++;
1907 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1908 return retVal;
1909 } while(letter <= 'z');
1910 return NULL;
1913 /*********************************************************************
1914 * _wmktemp_s (MSVCRT.@)
1916 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1918 DWORD len, xno, id;
1920 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1921 return MSVCRT_EINVAL;
1923 for(len=0; len<size; len++)
1924 if(!pattern[len])
1925 break;
1926 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1927 if(size)
1928 pattern[0] = 0;
1929 return MSVCRT_EINVAL;
1932 for(xno=1; xno<=6; xno++)
1933 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1934 return MSVCRT_EINVAL;
1936 id = GetCurrentProcessId();
1937 for(xno=1; xno<6; xno++) {
1938 pattern[len-xno] = id%10 + '0';
1939 id /= 10;
1942 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1943 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1944 return 0;
1947 pattern[0] = 0;
1948 *MSVCRT__errno() = MSVCRT_EEXIST;
1949 return MSVCRT_EEXIST;
1952 /*********************************************************************
1953 * _wmktemp (MSVCRT.@)
1955 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1957 int numX = 0;
1958 MSVCRT_wchar_t *retVal = pattern;
1959 int id;
1960 MSVCRT_wchar_t letter = 'a';
1962 if(!pattern)
1963 return NULL;
1965 while(*pattern)
1966 numX = (*pattern++ == 'X')? numX + 1 : 0;
1967 if (numX < 6)
1968 return NULL;
1969 pattern--;
1970 id = GetCurrentProcessId();
1971 numX = 6;
1972 while(numX--)
1974 int tempNum = id / 10;
1975 *pattern-- = id - (tempNum * 10) + '0';
1976 id = tempNum;
1978 pattern++;
1981 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
1982 return retVal;
1983 *pattern = letter++;
1984 } while(letter != '|');
1985 return NULL;
1988 static unsigned split_oflags(unsigned oflags)
1990 int wxflags = 0;
1991 unsigned unsupp; /* until we support everything */
1993 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1994 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1995 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1996 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
1997 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
1998 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
1999 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
2000 else wxflags |= WX_TEXT; /* default to TEXT*/
2001 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2003 if ((unsupp = oflags & ~(
2004 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
2005 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
2006 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
2007 MSVCRT__O_NOINHERIT|
2008 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
2009 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
2011 ERR(":unsupported oflags 0x%04x\n",unsupp);
2013 return wxflags;
2016 /*********************************************************************
2017 * _pipe (MSVCRT.@)
2019 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
2021 int ret = -1;
2022 SECURITY_ATTRIBUTES sa;
2023 HANDLE readHandle, writeHandle;
2025 if (!pfds)
2027 *MSVCRT__errno() = MSVCRT_EINVAL;
2028 return -1;
2031 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2032 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
2033 sa.lpSecurityDescriptor = NULL;
2034 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2036 unsigned int wxflags = split_oflags(textmode);
2037 int fd;
2039 LOCK_FILES();
2040 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2041 if (fd != -1)
2043 pfds[0] = fd;
2044 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2045 if (fd != -1)
2047 pfds[1] = fd;
2048 ret = 0;
2050 else
2052 MSVCRT__close(pfds[0]);
2053 CloseHandle(writeHandle);
2054 *MSVCRT__errno() = MSVCRT_EMFILE;
2057 else
2059 CloseHandle(readHandle);
2060 CloseHandle(writeHandle);
2061 *MSVCRT__errno() = MSVCRT_EMFILE;
2063 UNLOCK_FILES();
2065 else
2066 msvcrt_set_errno(GetLastError());
2068 return ret;
2071 static int check_bom(HANDLE h, int oflags, BOOL seek)
2073 char bom[sizeof(utf8_bom)];
2074 DWORD r;
2076 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2078 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2079 return oflags;
2081 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2082 oflags |= MSVCRT__O_U8TEXT;
2083 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2084 if (seek && r>2)
2085 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2086 oflags |= MSVCRT__O_U16TEXT;
2087 }else if (seek) {
2088 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2091 return oflags;
2094 /*********************************************************************
2095 * _wsopen_s (MSVCRT.@)
2097 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2099 DWORD access = 0, creation = 0, attrib;
2100 SECURITY_ATTRIBUTES sa;
2101 DWORD sharing, type;
2102 int wxflag;
2103 HANDLE hand;
2105 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2106 fd, debugstr_w(path), oflags, shflags, pmode);
2108 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2110 *fd = -1;
2111 wxflag = split_oflags(oflags);
2112 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2114 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2115 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2116 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2119 if (oflags & MSVCRT__O_CREAT)
2121 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
2122 FIXME(": pmode 0x%04x ignored\n", pmode);
2123 else
2124 WARN(": pmode 0x%04x ignored\n", pmode);
2126 if (oflags & MSVCRT__O_EXCL)
2127 creation = CREATE_NEW;
2128 else if (oflags & MSVCRT__O_TRUNC)
2129 creation = CREATE_ALWAYS;
2130 else
2131 creation = OPEN_ALWAYS;
2133 else /* no MSVCRT__O_CREAT */
2135 if (oflags & MSVCRT__O_TRUNC)
2136 creation = TRUNCATE_EXISTING;
2137 else
2138 creation = OPEN_EXISTING;
2141 switch( shflags )
2143 case MSVCRT__SH_DENYRW:
2144 sharing = 0L;
2145 break;
2146 case MSVCRT__SH_DENYWR:
2147 sharing = FILE_SHARE_READ;
2148 break;
2149 case MSVCRT__SH_DENYRD:
2150 sharing = FILE_SHARE_WRITE;
2151 break;
2152 case MSVCRT__SH_DENYNO:
2153 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2154 break;
2155 default:
2156 ERR( "Unhandled shflags 0x%x\n", shflags );
2157 return MSVCRT_EINVAL;
2159 attrib = FILE_ATTRIBUTE_NORMAL;
2161 if (oflags & MSVCRT__O_TEMPORARY)
2163 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2164 access |= DELETE;
2165 sharing |= FILE_SHARE_DELETE;
2168 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2169 sa.lpSecurityDescriptor = NULL;
2170 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2172 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2173 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2174 && !(access&GENERIC_READ))
2176 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2177 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2178 if (hand != INVALID_HANDLE_VALUE)
2180 oflags = check_bom(hand, oflags, FALSE);
2181 CloseHandle(hand);
2183 else
2184 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2187 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2188 if (hand == INVALID_HANDLE_VALUE) {
2189 WARN(":failed-last error (%d)\n",GetLastError());
2190 msvcrt_set_errno(GetLastError());
2191 return *MSVCRT__errno();
2194 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2196 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2197 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2198 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2200 if (oflags & MSVCRT__O_U8TEXT)
2202 DWORD written = 0, tmp;
2204 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2205 sizeof(utf8_bom)-written, &tmp, NULL))
2206 written += tmp;
2207 if (written != sizeof(utf8_bom)) {
2208 WARN("error writing BOM\n");
2209 CloseHandle(hand);
2210 msvcrt_set_errno(GetLastError());
2211 return *MSVCRT__errno();
2214 else
2216 DWORD written = 0, tmp;
2218 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2219 sizeof(utf16_bom)-written, &tmp, NULL))
2220 written += tmp;
2221 if (written != sizeof(utf16_bom))
2223 WARN("error writing BOM\n");
2224 CloseHandle(hand);
2225 msvcrt_set_errno(GetLastError());
2226 return *MSVCRT__errno();
2230 else if (access & GENERIC_READ)
2231 oflags = check_bom(hand, oflags, TRUE);
2234 type = GetFileType(hand);
2235 if (type == FILE_TYPE_CHAR)
2236 wxflag |= WX_TTY;
2237 else if (type == FILE_TYPE_PIPE)
2238 wxflag |= WX_PIPE;
2240 *fd = msvcrt_alloc_fd(hand, wxflag);
2241 if (*fd == -1)
2242 return *MSVCRT__errno();
2244 if (oflags & MSVCRT__O_WTEXT)
2245 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2246 else if (oflags & MSVCRT__O_U16TEXT)
2247 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
2248 else if (oflags & MSVCRT__O_U8TEXT)
2249 get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
2251 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2252 return 0;
2255 /*********************************************************************
2256 * _wsopen (MSVCRT.@)
2258 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2260 int pmode;
2261 int fd;
2263 if (oflags & MSVCRT__O_CREAT)
2265 __ms_va_list ap;
2267 __ms_va_start(ap, shflags);
2268 pmode = va_arg(ap, int);
2269 __ms_va_end(ap);
2271 else
2272 pmode = 0;
2274 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
2275 return fd;
2278 /*********************************************************************
2279 * _sopen_s (MSVCRT.@)
2281 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2283 MSVCRT_wchar_t *pathW;
2284 int ret;
2286 if (!MSVCRT_CHECK_PMT(fd != NULL))
2287 return MSVCRT_EINVAL;
2288 *fd = -1;
2289 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2290 return MSVCRT_EINVAL;
2292 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2293 MSVCRT_free(pathW);
2294 return ret;
2297 /*********************************************************************
2298 * _sopen (MSVCRT.@)
2300 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2302 int pmode;
2303 int fd;
2305 if (oflags & MSVCRT__O_CREAT)
2307 __ms_va_list ap;
2309 __ms_va_start(ap, shflags);
2310 pmode = va_arg(ap, int);
2311 __ms_va_end(ap);
2313 else
2314 pmode = 0;
2316 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2317 return fd;
2320 /*********************************************************************
2321 * _open (MSVCRT.@)
2323 int CDECL MSVCRT__open( const char *path, int flags, ... )
2325 __ms_va_list ap;
2327 if (flags & MSVCRT__O_CREAT)
2329 int pmode;
2330 __ms_va_start(ap, flags);
2331 pmode = va_arg(ap, int);
2332 __ms_va_end(ap);
2333 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2335 else
2336 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2339 /*********************************************************************
2340 * _wopen (MSVCRT.@)
2342 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2344 __ms_va_list ap;
2346 if (flags & MSVCRT__O_CREAT)
2348 int pmode;
2349 __ms_va_start(ap, flags);
2350 pmode = va_arg(ap, int);
2351 __ms_va_end(ap);
2352 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2354 else
2355 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2358 /*********************************************************************
2359 * _creat (MSVCRT.@)
2361 int CDECL MSVCRT__creat(const char *path, int flags)
2363 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2364 return MSVCRT__open(path, usedFlags);
2367 /*********************************************************************
2368 * _wcreat (MSVCRT.@)
2370 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2372 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2373 return MSVCRT__wopen(path, usedFlags);
2376 /*********************************************************************
2377 * _open_osfhandle (MSVCRT.@)
2379 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2381 DWORD flags;
2382 int fd;
2384 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2385 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2386 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2387 * text - it never sets MSVCRT__O_BINARY.
2389 /* don't let split_oflags() decide the mode if no mode is passed */
2390 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2391 oflags |= MSVCRT__O_BINARY;
2393 flags = GetFileType((HANDLE)handle);
2394 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2396 msvcrt_set_errno(GetLastError());
2397 return -1;
2400 if (flags == FILE_TYPE_CHAR)
2401 flags = WX_TTY;
2402 else if (flags == FILE_TYPE_PIPE)
2403 flags = WX_PIPE;
2404 else
2405 flags = 0;
2406 flags |= split_oflags(oflags);
2408 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2409 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2410 return fd;
2413 /*********************************************************************
2414 * _rmtmp (MSVCRT.@)
2416 int CDECL MSVCRT__rmtmp(void)
2418 int num_removed = 0, i;
2419 MSVCRT_FILE *file;
2421 LOCK_FILES();
2422 for (i = 3; i < MSVCRT_stream_idx; i++) {
2423 file = msvcrt_get_file(i);
2425 if (file->_tmpfname)
2427 MSVCRT_fclose(file);
2428 num_removed++;
2431 UNLOCK_FILES();
2433 if (num_removed)
2434 TRACE(":removed (%d) temp files\n",num_removed);
2435 return num_removed;
2438 static inline int get_utf8_char_len(char ch)
2440 if((ch&0xf8) == 0xf0)
2441 return 4;
2442 else if((ch&0xf0) == 0xe0)
2443 return 3;
2444 else if((ch&0xe0) == 0xc0)
2445 return 2;
2446 return 1;
2449 /*********************************************************************
2450 * (internal) read_utf8
2452 static int read_utf8(int fd, MSVCRT_wchar_t *buf, unsigned int count)
2454 ioinfo *fdinfo = get_ioinfo_nolock(fd);
2455 HANDLE hand = fdinfo->handle;
2456 char min_buf[4], *readbuf, lookahead;
2457 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2459 /* make the buffer big enough to hold at least one character */
2460 /* read bytes have to fit to output and lookahead buffers */
2461 count /= 2;
2462 readbuf_size = count < 4 ? 4 : count;
2463 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2464 readbuf_size = 4;
2465 readbuf = min_buf;
2468 if(fdinfo->lookahead[0] != '\n') {
2469 readbuf[pos++] = fdinfo->lookahead[0];
2470 fdinfo->lookahead[0] = '\n';
2472 if(fdinfo->lookahead[1] != '\n') {
2473 readbuf[pos++] = fdinfo->lookahead[1];
2474 fdinfo->lookahead[1] = '\n';
2476 if(fdinfo->lookahead[2] != '\n') {
2477 readbuf[pos++] = fdinfo->lookahead[2];
2478 fdinfo->lookahead[2] = '\n';
2483 /* NOTE: this case is broken in native dll, reading
2484 * sometimes fails when small buffer is passed
2486 if(count < 4) {
2487 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2488 if (GetLastError() == ERROR_BROKEN_PIPE) {
2489 fdinfo->wxflag |= WX_ATEOF;
2490 return 0;
2491 }else {
2492 msvcrt_set_errno(GetLastError());
2493 return -1;
2495 }else if(!num_read) {
2496 fdinfo->wxflag |= WX_ATEOF;
2497 return 0;
2498 }else {
2499 pos++;
2502 char_len = get_utf8_char_len(readbuf[0]);
2503 if(char_len>pos) {
2504 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2505 pos += num_read;
2508 if(readbuf[0] == '\n')
2509 fdinfo->wxflag |= WX_READNL;
2510 else
2511 fdinfo->wxflag &= ~WX_READNL;
2513 if(readbuf[0] == 0x1a) {
2514 fdinfo->wxflag |= WX_ATEOF;
2515 return 0;
2518 if(readbuf[0] == '\r') {
2519 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2520 buf[0] = '\r';
2521 else if(lookahead == '\n')
2522 buf[0] = '\n';
2523 else {
2524 buf[0] = '\r';
2525 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2526 fdinfo->lookahead[0] = lookahead;
2527 else
2528 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2530 return 2;
2533 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2534 msvcrt_set_errno(GetLastError());
2535 return -1;
2538 return num_read*2;
2541 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2542 if(pos) {
2543 num_read = 0;
2544 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2545 fdinfo->wxflag |= WX_ATEOF;
2546 if (readbuf != min_buf) MSVCRT_free(readbuf);
2547 return 0;
2548 }else {
2549 msvcrt_set_errno(GetLastError());
2550 if (readbuf != min_buf) MSVCRT_free(readbuf);
2551 return -1;
2553 }else if(!pos && !num_read) {
2554 fdinfo->wxflag |= WX_ATEOF;
2555 if (readbuf != min_buf) MSVCRT_free(readbuf);
2556 return 0;
2559 pos += num_read;
2560 if(readbuf[0] == '\n')
2561 fdinfo->wxflag |= WX_READNL;
2562 else
2563 fdinfo->wxflag &= ~WX_READNL;
2565 /* Find first byte of last character (may be incomplete) */
2566 for(i=pos-1; i>0 && i>pos-4; i--)
2567 if((readbuf[i]&0xc0) != 0x80)
2568 break;
2569 char_len = get_utf8_char_len(readbuf[i]);
2570 if(char_len+i <= pos)
2571 i += char_len;
2573 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2574 if(i < pos)
2575 fdinfo->lookahead[0] = readbuf[i];
2576 if(i+1 < pos)
2577 fdinfo->lookahead[1] = readbuf[i+1];
2578 if(i+2 < pos)
2579 fdinfo->lookahead[2] = readbuf[i+2];
2580 }else if(i < pos) {
2581 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2583 pos = i;
2585 for(i=0, j=0; i<pos; i++) {
2586 if(readbuf[i] == 0x1a) {
2587 fdinfo->wxflag |= WX_ATEOF;
2588 break;
2591 /* strip '\r' if followed by '\n' */
2592 if(readbuf[i] == '\r' && i+1==pos) {
2593 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2594 readbuf[j++] = '\r';
2595 }else if(lookahead == '\n' && j==0) {
2596 readbuf[j++] = '\n';
2597 }else {
2598 if(lookahead != '\n')
2599 readbuf[j++] = '\r';
2601 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2602 fdinfo->lookahead[0] = lookahead;
2603 else
2604 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2606 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2607 readbuf[j++] = readbuf[i];
2610 pos = j;
2612 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2613 msvcrt_set_errno(GetLastError());
2614 if (readbuf != min_buf) MSVCRT_free(readbuf);
2615 return -1;
2618 if (readbuf != min_buf) MSVCRT_free(readbuf);
2619 return num_read*2;
2622 /*********************************************************************
2623 * (internal) read_i
2625 * When reading \r as last character in text mode, read() positions
2626 * the file pointer on the \r character while getc() goes on to
2627 * the following \n
2629 static int read_i(int fd, void *buf, unsigned int count)
2631 DWORD num_read, utf16;
2632 char *bufstart = buf;
2633 HANDLE hand = msvcrt_fdtoh(fd);
2634 ioinfo *fdinfo = get_ioinfo_nolock(fd);
2636 if (count == 0)
2637 return 0;
2639 if (fdinfo->wxflag & WX_ATEOF) {
2640 TRACE("already at EOF, returning 0\n");
2641 return 0;
2643 /* Don't trace small reads, it gets *very* annoying */
2644 if (count > 4)
2645 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2646 if (hand == INVALID_HANDLE_VALUE)
2648 *MSVCRT__errno() = MSVCRT_EBADF;
2649 return -1;
2652 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2653 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2655 *MSVCRT__errno() = MSVCRT_EINVAL;
2656 return -1;
2659 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2660 return read_utf8(fd, buf, count);
2662 if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL))
2664 if (fdinfo->lookahead[0] != '\n')
2666 bufstart[0] = fdinfo->lookahead[0];
2667 fdinfo->lookahead[0] = '\n';
2669 if (utf16)
2671 bufstart[1] = fdinfo->lookahead[1];
2672 fdinfo->lookahead[1] = '\n';
2675 if(count>1+utf16 && ReadFile(hand, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2676 num_read += 1+utf16;
2677 else
2678 num_read = 1+utf16;
2681 if(utf16 && (num_read&1))
2683 /* msvcr90 uses uninitialized value from the buffer in this case */
2684 /* msvcrt ignores additional data */
2685 ERR("got odd number of bytes in UTF16 mode\n");
2686 num_read--;
2689 if (count != 0 && num_read == 0)
2691 fdinfo->wxflag |= WX_ATEOF;
2692 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2694 else if (fdinfo->wxflag & WX_TEXT)
2696 DWORD i, j;
2698 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2699 fdinfo->wxflag |= WX_READNL;
2700 else
2701 fdinfo->wxflag &= ~WX_READNL;
2703 for (i=0, j=0; i<num_read; i+=1+utf16)
2705 /* in text mode, a ctrl-z signals EOF */
2706 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2708 fdinfo->wxflag |= WX_ATEOF;
2709 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2710 break;
2713 /* in text mode, strip \r if followed by \n */
2714 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2716 char lookahead[2];
2717 DWORD len;
2719 lookahead[1] = '\n';
2720 if (ReadFile(hand, lookahead, 1+utf16, &len, NULL) && len)
2722 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2724 bufstart[j++] = '\n';
2725 if(utf16) bufstart[j++] = 0;
2727 else
2729 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2731 bufstart[j++] = '\r';
2732 if(utf16) bufstart[j++] = 0;
2735 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2737 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2739 bufstart[j++] = '\n';
2740 if (utf16) bufstart[j++] = 0;
2742 else
2744 fdinfo->lookahead[0] = lookahead[0];
2745 fdinfo->lookahead[1] = lookahead[1];
2748 else
2749 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2752 else
2754 bufstart[j++] = '\r';
2755 if(utf16) bufstart[j++] = 0;
2758 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2759 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2761 bufstart[j++] = bufstart[i];
2762 if(utf16) bufstart[j++] = bufstart[i+1];
2765 num_read = j;
2768 else
2770 if (GetLastError() == ERROR_BROKEN_PIPE)
2772 TRACE(":end-of-pipe\n");
2773 fdinfo->wxflag |= WX_ATEOF;
2774 return 0;
2776 else
2778 TRACE(":failed-last error (%d)\n",GetLastError());
2779 return -1;
2783 if (count > 4)
2784 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2785 return num_read;
2788 /*********************************************************************
2789 * _read (MSVCRT.@)
2791 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2793 int num_read;
2794 num_read = read_i(fd, buf, count);
2795 return num_read;
2798 /*********************************************************************
2799 * _setmode (MSVCRT.@)
2801 int CDECL MSVCRT__setmode(int fd,int mode)
2803 int ret = get_ioinfo_nolock(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2804 if(ret==MSVCRT__O_TEXT && (get_ioinfo_nolock(fd)->exflag & (EF_UTF8|EF_UTF16)))
2805 ret = MSVCRT__O_WTEXT;
2807 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2808 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2809 *MSVCRT__errno() = MSVCRT_EINVAL;
2810 return -1;
2813 if(mode == MSVCRT__O_BINARY) {
2814 get_ioinfo_nolock(fd)->wxflag &= ~WX_TEXT;
2815 get_ioinfo_nolock(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2816 return ret;
2819 get_ioinfo_nolock(fd)->wxflag |= WX_TEXT;
2820 if(mode == MSVCRT__O_TEXT)
2821 get_ioinfo_nolock(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2822 else if(mode == MSVCRT__O_U8TEXT)
2823 get_ioinfo_nolock(fd)->exflag = (get_ioinfo_nolock(fd)->exflag & ~EF_UTF16) | EF_UTF8;
2824 else
2825 get_ioinfo_nolock(fd)->exflag = (get_ioinfo_nolock(fd)->exflag & ~EF_UTF8) | EF_UTF16;
2827 return ret;
2830 /*********************************************************************
2831 * _stat64 (MSVCRT.@)
2833 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2835 DWORD dw;
2836 WIN32_FILE_ATTRIBUTE_DATA hfi;
2837 unsigned short mode = ALL_S_IREAD;
2838 int plen;
2840 TRACE(":file (%s) buf(%p)\n",path,buf);
2842 plen = strlen(path);
2843 while (plen && path[plen-1]==' ')
2844 plen--;
2846 if (plen && (plen<2 || path[plen-2]!=':') &&
2847 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2849 *MSVCRT__errno() = MSVCRT_ENOENT;
2850 return -1;
2853 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2855 TRACE("failed (%d)\n",GetLastError());
2856 *MSVCRT__errno() = MSVCRT_ENOENT;
2857 return -1;
2860 memset(buf,0,sizeof(struct MSVCRT__stat64));
2862 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2863 Bon 011120: This FIXME seems incorrect
2864 Also a letter as first char isn't enough to be classified
2865 as a drive letter
2867 if (isalpha(*path)&& (*(path+1)==':'))
2868 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2869 else
2870 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2872 /* Dir, or regular file? */
2873 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2874 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2875 else
2877 mode |= MSVCRT__S_IFREG;
2878 /* executable? */
2879 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2881 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2882 (tolower(path[plen-3]) << 16);
2883 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2884 mode |= ALL_S_IEXEC;
2888 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2889 mode |= ALL_S_IWRITE;
2891 buf->st_mode = mode;
2892 buf->st_nlink = 1;
2893 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2894 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2895 buf->st_atime = dw;
2896 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2897 buf->st_mtime = buf->st_ctime = dw;
2898 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2899 (int)(buf->st_size >> 32),(int)buf->st_size,
2900 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2901 return 0;
2904 /*********************************************************************
2905 * _stati64 (MSVCRT.@)
2907 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2909 int ret;
2910 struct MSVCRT__stat64 buf64;
2912 ret = MSVCRT_stat64(path, &buf64);
2913 if (!ret)
2914 msvcrt_stat64_to_stati64(&buf64, buf);
2915 return ret;
2918 /*********************************************************************
2919 * _stat (MSVCRT.@)
2921 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2923 int ret;
2924 struct MSVCRT__stat64 buf64;
2926 ret = MSVCRT_stat64( path, &buf64);
2927 if (!ret)
2928 msvcrt_stat64_to_stat(&buf64, buf);
2929 return ret;
2932 /*********************************************************************
2933 * _stat32 (MSVCR100.@)
2935 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
2937 int ret;
2938 struct MSVCRT__stat64 buf64;
2940 ret = MSVCRT_stat64(path, &buf64);
2941 if (!ret)
2942 msvcrt_stat64_to_stat32(&buf64, buf);
2943 return ret;
2946 /*********************************************************************
2947 * _stat32i64 (MSVCR100.@)
2949 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
2951 int ret;
2952 struct MSVCRT__stat64 buf64;
2954 ret = MSVCRT_stat64(path, &buf64);
2955 if (!ret)
2956 msvcrt_stat64_to_stat32i64(&buf64, buf);
2957 return ret;
2960 /*********************************************************************
2961 * _stat64i32 (MSVCR100.@)
2963 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
2965 int ret;
2966 struct MSVCRT__stat64 buf64;
2968 ret = MSVCRT_stat64(path, &buf64);
2969 if (!ret)
2970 msvcrt_stat64_to_stat64i32(&buf64, buf);
2971 return ret;
2974 /*********************************************************************
2975 * _wstat64 (MSVCRT.@)
2977 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2979 DWORD dw;
2980 WIN32_FILE_ATTRIBUTE_DATA hfi;
2981 unsigned short mode = ALL_S_IREAD;
2982 int plen;
2984 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2986 plen = strlenW(path);
2987 while (plen && path[plen-1]==' ')
2988 plen--;
2990 if(plen && (plen<2 || path[plen-2]!=':') &&
2991 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2993 *MSVCRT__errno() = MSVCRT_ENOENT;
2994 return -1;
2997 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2999 TRACE("failed (%d)\n",GetLastError());
3000 *MSVCRT__errno() = MSVCRT_ENOENT;
3001 return -1;
3004 memset(buf,0,sizeof(struct MSVCRT__stat64));
3006 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3007 if (MSVCRT_iswalpha(*path))
3008 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
3009 else
3010 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3012 /* Dir, or regular file? */
3013 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3014 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3015 else
3017 mode |= MSVCRT__S_IFREG;
3018 /* executable? */
3019 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3021 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
3022 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
3023 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3024 mode |= ALL_S_IEXEC;
3028 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3029 mode |= ALL_S_IWRITE;
3031 buf->st_mode = mode;
3032 buf->st_nlink = 1;
3033 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3034 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3035 buf->st_atime = dw;
3036 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3037 buf->st_mtime = buf->st_ctime = dw;
3038 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3039 (int)(buf->st_size >> 32),(int)buf->st_size,
3040 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3041 return 0;
3044 /*********************************************************************
3045 * _wstati64 (MSVCRT.@)
3047 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
3049 int ret;
3050 struct MSVCRT__stat64 buf64;
3052 ret = MSVCRT__wstat64(path, &buf64);
3053 if (!ret)
3054 msvcrt_stat64_to_stati64(&buf64, buf);
3055 return ret;
3058 /*********************************************************************
3059 * _wstat (MSVCRT.@)
3061 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
3063 int ret;
3064 struct MSVCRT__stat64 buf64;
3066 ret = MSVCRT__wstat64( path, &buf64 );
3067 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3068 return ret;
3071 /*********************************************************************
3072 * _wstat32 (MSVCR100.@)
3074 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
3076 int ret;
3077 struct MSVCRT__stat64 buf64;
3079 ret = MSVCRT__wstat64(path, &buf64);
3080 if (!ret)
3081 msvcrt_stat64_to_stat32(&buf64, buf);
3082 return ret;
3085 /*********************************************************************
3086 * _wstat32i64 (MSVCR100.@)
3088 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
3090 int ret;
3091 struct MSVCRT__stat64 buf64;
3093 ret = MSVCRT__wstat64(path, &buf64);
3094 if (!ret)
3095 msvcrt_stat64_to_stat32i64(&buf64, buf);
3096 return ret;
3099 /*********************************************************************
3100 * _wstat64i32 (MSVCR100.@)
3102 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3104 int ret;
3105 struct MSVCRT__stat64 buf64;
3107 ret = MSVCRT__wstat64(path, &buf64);
3108 if (!ret)
3109 msvcrt_stat64_to_stat64i32(&buf64, buf);
3110 return ret;
3113 /*********************************************************************
3114 * _tell (MSVCRT.@)
3116 MSVCRT_long CDECL MSVCRT__tell(int fd)
3118 return MSVCRT__lseek(fd, 0, SEEK_CUR);
3121 /*********************************************************************
3122 * _telli64 (MSVCRT.@)
3124 __int64 CDECL _telli64(int fd)
3126 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
3129 /*********************************************************************
3130 * _tempnam (MSVCRT.@)
3132 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3134 char tmpbuf[MAX_PATH];
3135 const char *tmp_dir = MSVCRT_getenv("TMP");
3137 if (tmp_dir) dir = tmp_dir;
3139 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3140 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3142 TRACE("got name (%s)\n",tmpbuf);
3143 DeleteFileA(tmpbuf);
3144 return MSVCRT__strdup(tmpbuf);
3146 TRACE("failed (%d)\n",GetLastError());
3147 return NULL;
3150 /*********************************************************************
3151 * _wtempnam (MSVCRT.@)
3153 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3155 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3156 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3157 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3159 if (tmp_dir) dir = tmp_dir;
3161 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3162 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3164 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3165 DeleteFileW(tmpbuf);
3166 return MSVCRT__wcsdup(tmpbuf);
3168 TRACE("failed (%d)\n",GetLastError());
3169 return NULL;
3172 /*********************************************************************
3173 * _umask (MSVCRT.@)
3175 int CDECL MSVCRT__umask(int umask)
3177 int old_umask = MSVCRT_umask;
3178 TRACE("(%d)\n",umask);
3179 MSVCRT_umask = umask;
3180 return old_umask;
3183 /*********************************************************************
3184 * _utime64 (MSVCRT.@)
3186 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3188 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3190 if (fd > 0)
3192 int retVal = _futime64(fd, t);
3193 MSVCRT__close(fd);
3194 return retVal;
3196 return -1;
3199 /*********************************************************************
3200 * _utime32 (MSVCRT.@)
3202 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3204 if (t)
3206 struct MSVCRT___utimbuf64 t64;
3207 t64.actime = t->actime;
3208 t64.modtime = t->modtime;
3209 return _utime64( path, &t64 );
3211 else
3212 return _utime64( path, NULL );
3215 /*********************************************************************
3216 * _utime (MSVCRT.@)
3218 #ifdef _WIN64
3219 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3221 return _utime64( path, t );
3223 #else
3224 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3226 return _utime32( path, t );
3228 #endif
3230 /*********************************************************************
3231 * _wutime64 (MSVCRT.@)
3233 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3235 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3237 if (fd > 0)
3239 int retVal = _futime64(fd, t);
3240 MSVCRT__close(fd);
3241 return retVal;
3243 return -1;
3246 /*********************************************************************
3247 * _wutime32 (MSVCRT.@)
3249 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3251 if (t)
3253 struct MSVCRT___utimbuf64 t64;
3254 t64.actime = t->actime;
3255 t64.modtime = t->modtime;
3256 return _wutime64( path, &t64 );
3258 else
3259 return _wutime64( path, NULL );
3262 /*********************************************************************
3263 * _wutime (MSVCRT.@)
3265 #ifdef _WIN64
3266 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3268 return _wutime64( path, t );
3270 #else
3271 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3273 return _wutime32( path, t );
3275 #endif
3277 /*********************************************************************
3278 * _write (MSVCRT.@)
3280 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3282 DWORD num_written;
3283 ioinfo *info = get_ioinfo_nolock(fd);
3284 HANDLE hand = info->handle;
3286 /* Don't trace small writes, it gets *very* annoying */
3287 #if 0
3288 if (count > 32)
3289 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3290 #endif
3291 if (hand == INVALID_HANDLE_VALUE)
3293 *MSVCRT__errno() = MSVCRT_EBADF;
3294 return -1;
3297 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3299 *MSVCRT__errno() = MSVCRT_EINVAL;
3300 return -1;
3303 /* If appending, go to EOF */
3304 if (info->wxflag & WX_APPEND)
3305 MSVCRT__lseek(fd, 0, FILE_END);
3307 if (!(info->wxflag & WX_TEXT))
3309 if (WriteFile(hand, buf, count, &num_written, NULL)
3310 && (num_written == count))
3311 return num_written;
3312 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3313 hand, GetLastError());
3314 *MSVCRT__errno() = MSVCRT_ENOSPC;
3316 else
3318 unsigned int i, j, nr_lf, size;
3319 char *p = NULL;
3320 const char *q;
3321 const char *s = buf, *buf_start = buf;
3323 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3325 /* find number of \n */
3326 for (nr_lf=0, i=0; i<count; i++)
3327 if (s[i] == '\n')
3328 nr_lf++;
3329 if (nr_lf)
3331 size = count+nr_lf;
3332 if ((q = p = MSVCRT_malloc(size)))
3334 for (s = buf, i = 0, j = 0; i < count; i++)
3336 if (s[i] == '\n')
3337 p[j++] = '\r';
3338 p[j++] = s[i];
3341 else
3343 FIXME("Malloc failed\n");
3344 nr_lf = 0;
3345 size = count;
3346 q = buf;
3349 else
3351 size = count;
3352 q = buf;
3355 else if (info->exflag & EF_UTF16)
3357 for (nr_lf=0, i=0; i<count; i+=2)
3358 if (s[i]=='\n' && s[i+1]==0)
3359 nr_lf += 2;
3360 if (nr_lf)
3362 size = count+nr_lf;
3363 if ((q = p = MSVCRT_malloc(size)))
3365 for (s=buf, i=0, j=0; i<count; i++)
3367 if (s[i]=='\n' && s[i+1]==0)
3369 p[j++] = '\r';
3370 p[j++] = 0;
3372 p[j++] = s[i++];
3373 p[j++] = s[i];
3376 else
3378 FIXME("Malloc failed\n");
3379 nr_lf = 0;
3380 size = count;
3381 q = buf;
3384 else
3386 size = count;
3387 q = buf;
3390 else
3392 DWORD conv_len;
3394 for(nr_lf=0, i=0; i<count; i+=2)
3395 if (s[i]=='\n' && s[i+1]==0)
3396 nr_lf++;
3398 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3399 if(!conv_len) {
3400 msvcrt_set_errno(GetLastError());
3401 MSVCRT_free(p);
3402 return -1;
3405 size = conv_len+nr_lf;
3406 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3408 for (s=buf, i=0, j=0; i<count; i++)
3410 if (s[i]=='\n' && s[i+1]==0)
3412 p[j++] = '\r';
3413 p[j++] = 0;
3415 p[j++] = s[i++];
3416 p[j++] = s[i];
3418 q = p+count+nr_lf*2;
3419 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3420 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3422 else
3424 FIXME("Malloc failed\n");
3425 nr_lf = 0;
3426 size = count;
3427 q = buf;
3431 if (!WriteFile(hand, q, size, &num_written, NULL))
3432 num_written = -1;
3433 if(p)
3434 MSVCRT_free(p);
3435 if (num_written != size)
3437 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3438 fd, hand, GetLastError(), num_written);
3439 *MSVCRT__errno() = MSVCRT_ENOSPC;
3440 return s - buf_start;
3442 return count;
3445 return -1;
3448 /*********************************************************************
3449 * _putw (MSVCRT.@)
3451 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3453 int len;
3455 MSVCRT__lock_file(file);
3456 len = MSVCRT__write(file->_file, &val, sizeof(val));
3457 if (len == sizeof(val)) {
3458 MSVCRT__unlock_file(file);
3459 return val;
3462 file->_flag |= MSVCRT__IOERR;
3463 MSVCRT__unlock_file(file);
3464 return MSVCRT_EOF;
3467 /*********************************************************************
3468 * fclose (MSVCRT.@)
3470 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3472 int ret;
3474 MSVCRT__lock_file(file);
3475 ret = MSVCRT__fclose_nolock(file);
3476 MSVCRT__unlock_file(file);
3478 return ret;
3481 /*********************************************************************
3482 * _fclose_nolock (MSVCRT.@)
3484 int CDECL MSVCRT__fclose_nolock(MSVCRT_FILE* file)
3486 int r, flag;
3488 flag = file->_flag;
3489 MSVCRT_free(file->_tmpfname);
3490 file->_tmpfname = NULL;
3491 /* flush stdio buffers */
3492 if(file->_flag & MSVCRT__IOWRT)
3493 MSVCRT__fflush_nolock(file);
3494 if(file->_flag & MSVCRT__IOMYBUF)
3495 MSVCRT_free(file->_base);
3497 r=MSVCRT__close(file->_file);
3498 file->_flag = 0;
3500 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3503 /*********************************************************************
3504 * feof (MSVCRT.@)
3506 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3508 return file->_flag & MSVCRT__IOEOF;
3511 /*********************************************************************
3512 * ferror (MSVCRT.@)
3514 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3516 return file->_flag & MSVCRT__IOERR;
3519 /*********************************************************************
3520 * _filbuf (MSVCRT.@)
3522 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3524 unsigned char c;
3526 if(file->_flag & MSVCRT__IOSTRG)
3527 return MSVCRT_EOF;
3529 /* Allocate buffer if needed */
3530 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3531 msvcrt_alloc_buffer(file);
3533 if(!(file->_flag & MSVCRT__IOREAD)) {
3534 if(file->_flag & MSVCRT__IORW)
3535 file->_flag |= MSVCRT__IOREAD;
3536 else
3537 return MSVCRT_EOF;
3540 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3541 int r;
3542 if ((r = read_i(file->_file,&c,1)) != 1) {
3543 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3544 return MSVCRT_EOF;
3547 return c;
3548 } else {
3549 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
3550 if(file->_cnt<=0) {
3551 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3552 file->_cnt = 0;
3553 return MSVCRT_EOF;
3556 file->_cnt--;
3557 file->_ptr = file->_base+1;
3558 c = *(unsigned char *)file->_base;
3559 return c;
3563 /*********************************************************************
3564 * fgetc (MSVCRT.@)
3566 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3568 int ret;
3570 MSVCRT__lock_file(file);
3571 ret = MSVCRT__fgetc_nolock(file);
3572 MSVCRT__unlock_file(file);
3574 return ret;
3577 /*********************************************************************
3578 * _fgetc_nolock (MSVCRT.@)
3580 int CDECL MSVCRT__fgetc_nolock(MSVCRT_FILE* file)
3582 unsigned char *i;
3583 unsigned int j;
3585 if (file->_cnt>0) {
3586 file->_cnt--;
3587 i = (unsigned char *)file->_ptr++;
3588 j = *i;
3589 } else
3590 j = MSVCRT__filbuf(file);
3592 return j;
3595 /*********************************************************************
3596 * _fgetchar (MSVCRT.@)
3598 int CDECL MSVCRT__fgetchar(void)
3600 return MSVCRT_fgetc(MSVCRT_stdin);
3603 /*********************************************************************
3604 * fgets (MSVCRT.@)
3606 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3608 int cc = MSVCRT_EOF;
3609 char * buf_start = s;
3611 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3612 file,file->_file,s,size);
3614 MSVCRT__lock_file(file);
3616 while ((size >1) && (cc = MSVCRT__fgetc_nolock(file)) != MSVCRT_EOF && cc != '\n')
3618 *s++ = (char)cc;
3619 size --;
3621 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3623 TRACE(":nothing read\n");
3624 MSVCRT__unlock_file(file);
3625 return NULL;
3627 if ((cc != MSVCRT_EOF) && (size > 1))
3628 *s++ = cc;
3629 *s = '\0';
3630 TRACE(":got %s\n", debugstr_a(buf_start));
3631 MSVCRT__unlock_file(file);
3632 return buf_start;
3635 /*********************************************************************
3636 * fgetwc (MSVCRT.@)
3638 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3640 MSVCRT_wint_t ret;
3642 MSVCRT__lock_file(file);
3643 ret = MSVCRT__fgetwc_nolock(file);
3644 MSVCRT__unlock_file(file);
3646 return ret;
3649 /*********************************************************************
3650 * _fgetwc_nolock (MSVCRT.@)
3652 MSVCRT_wint_t CDECL MSVCRT__fgetwc_nolock(MSVCRT_FILE* file)
3654 MSVCRT_wint_t ret;
3655 int ch;
3657 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3658 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3659 char *p;
3661 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3662 ch = MSVCRT__fgetc_nolock(file);
3663 if(ch == MSVCRT_EOF) {
3664 ret = MSVCRT_WEOF;
3665 break;
3667 *p = (char)ch;
3669 }else {
3670 char mbs[MSVCRT_MB_LEN_MAX];
3671 int len = 0;
3673 ch = MSVCRT__fgetc_nolock(file);
3674 if(ch != MSVCRT_EOF) {
3675 mbs[0] = (char)ch;
3676 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3677 ch = MSVCRT__fgetc_nolock(file);
3678 if(ch != MSVCRT_EOF) {
3679 mbs[1] = (char)ch;
3680 len = 2;
3682 }else {
3683 len = 1;
3687 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3688 ret = MSVCRT_WEOF;
3691 return ret;
3694 /*********************************************************************
3695 * _getw (MSVCRT.@)
3697 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3699 char *ch;
3700 int i, k;
3701 unsigned int j;
3702 ch = (char *)&i;
3704 MSVCRT__lock_file(file);
3705 for (j=0; j<sizeof(int); j++) {
3706 k = MSVCRT__fgetc_nolock(file);
3707 if (k == MSVCRT_EOF) {
3708 file->_flag |= MSVCRT__IOEOF;
3709 MSVCRT__unlock_file(file);
3710 return EOF;
3712 ch[j] = k;
3715 MSVCRT__unlock_file(file);
3716 return i;
3719 /*********************************************************************
3720 * getwc (MSVCRT.@)
3722 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3724 return MSVCRT_fgetwc(file);
3727 /*********************************************************************
3728 * _fgetwchar (MSVCRT.@)
3730 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3732 return MSVCRT_fgetwc(MSVCRT_stdin);
3735 /*********************************************************************
3736 * getwchar (MSVCRT.@)
3738 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3740 return MSVCRT__fgetwchar();
3743 /*********************************************************************
3744 * fgetws (MSVCRT.@)
3746 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3748 MSVCRT_wint_t cc = MSVCRT_WEOF;
3749 MSVCRT_wchar_t * buf_start = s;
3751 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3752 file,file->_file,s,size);
3754 MSVCRT__lock_file(file);
3756 while ((size >1) && (cc = MSVCRT__fgetwc_nolock(file)) != MSVCRT_WEOF && cc != '\n')
3758 *s++ = cc;
3759 size --;
3761 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3763 TRACE(":nothing read\n");
3764 MSVCRT__unlock_file(file);
3765 return NULL;
3767 if ((cc != MSVCRT_WEOF) && (size > 1))
3768 *s++ = cc;
3769 *s = 0;
3770 TRACE(":got %s\n", debugstr_w(buf_start));
3771 MSVCRT__unlock_file(file);
3772 return buf_start;
3775 /*********************************************************************
3776 * _flsbuf (MSVCRT.@)
3778 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3780 /* Flush output buffer */
3781 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3782 msvcrt_alloc_buffer(file);
3784 if(!(file->_flag & MSVCRT__IOWRT)) {
3785 if(file->_flag & MSVCRT__IORW)
3786 file->_flag |= MSVCRT__IOWRT;
3787 else
3788 return MSVCRT_EOF;
3790 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3791 int res = 0;
3793 if(file->_cnt <= 0) {
3794 res = msvcrt_flush_buffer(file);
3795 if(res)
3796 return res;
3797 file->_cnt=file->_bufsiz;
3799 *file->_ptr++ = c;
3800 file->_cnt--;
3801 return c&0xff;
3802 } else {
3803 unsigned char cc=c;
3804 int len;
3805 /* set _cnt to 0 for unbuffered FILEs */
3806 file->_cnt = 0;
3807 len = MSVCRT__write(file->_file, &cc, 1);
3808 if (len == 1)
3809 return c & 0xff;
3810 file->_flag |= MSVCRT__IOERR;
3811 return MSVCRT_EOF;
3815 /*********************************************************************
3816 * fwrite (MSVCRT.@)
3818 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3820 MSVCRT_size_t ret;
3822 MSVCRT__lock_file(file);
3823 ret = MSVCRT__fwrite_nolock(ptr, size, nmemb, file);
3824 MSVCRT__unlock_file(file);
3826 return ret;
3829 /*********************************************************************
3830 * _fwrite_nolock (MSVCRT.@)
3832 MSVCRT_size_t CDECL MSVCRT__fwrite_nolock(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3834 MSVCRT_size_t wrcnt=size * nmemb;
3835 int written = 0;
3836 if (size == 0)
3837 return 0;
3839 while(wrcnt) {
3840 if(file->_cnt < 0) {
3841 WARN("negative file->_cnt value in %p\n", file);
3842 file->_flag |= MSVCRT__IOERR;
3843 break;
3844 } else if(file->_cnt) {
3845 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3846 memcpy(file->_ptr, ptr, pcnt);
3847 file->_cnt -= pcnt;
3848 file->_ptr += pcnt;
3849 written += pcnt;
3850 wrcnt -= pcnt;
3851 ptr = (const char*)ptr + pcnt;
3852 } else if((file->_flag & MSVCRT__IONBF)
3853 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
3854 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
3855 MSVCRT_size_t pcnt;
3856 int bufsiz;
3858 if(file->_flag & MSVCRT__IONBF)
3859 bufsiz = 1;
3860 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3861 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
3862 else
3863 bufsiz = file->_bufsiz;
3865 pcnt = (wrcnt / bufsiz) * bufsiz;
3867 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
3868 break;
3870 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
3871 file->_flag |= MSVCRT__IOERR;
3872 break;
3874 written += pcnt;
3875 wrcnt -= pcnt;
3876 ptr = (const char*)ptr + pcnt;
3877 } else {
3878 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3879 break;
3880 written++;
3881 wrcnt--;
3882 ptr = (const char*)ptr + 1;
3886 return written / size;
3889 /*********************************************************************
3890 * fputwc (MSVCRT.@)
3892 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3894 MSVCRT_wint_t ret;
3896 MSVCRT__lock_file(file);
3897 ret = MSVCRT__fputwc_nolock(wc, file);
3898 MSVCRT__unlock_file(file);
3900 return ret;
3903 /*********************************************************************
3904 * _fputwc_nolock (MSVCRT.@)
3906 MSVCRT_wint_t CDECL MSVCRT__fputwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3908 MSVCRT_wchar_t mwc=wc;
3909 ioinfo *fdinfo;
3910 MSVCRT_wint_t ret;
3912 fdinfo = get_ioinfo_nolock(file->_file);
3914 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3915 char buf[MSVCRT_MB_LEN_MAX];
3916 int char_len;
3918 char_len = MSVCRT_wctomb(buf, mwc);
3919 if(char_len!=-1 && MSVCRT__fwrite_nolock(buf, char_len, 1, file)==1)
3920 ret = wc;
3921 else
3922 ret = MSVCRT_WEOF;
3923 }else if(MSVCRT__fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
3924 ret = wc;
3925 }else {
3926 ret = MSVCRT_WEOF;
3929 return ret;
3932 /*********************************************************************
3933 * _fputwchar (MSVCRT.@)
3935 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3937 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3940 /*********************************************************************
3941 * _wfsopen (MSVCRT.@)
3943 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
3945 MSVCRT_FILE* file;
3946 int open_flags, stream_flags, fd;
3948 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3950 /* map mode string to open() flags. "man fopen" for possibilities. */
3951 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3952 return NULL;
3954 LOCK_FILES();
3955 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3956 if (fd < 0)
3957 file = NULL;
3958 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3959 != -1)
3960 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3961 else if (file)
3963 file->_flag = 0;
3964 file = NULL;
3967 TRACE(":got (%p)\n",file);
3968 if (fd >= 0 && !file)
3969 MSVCRT__close(fd);
3970 UNLOCK_FILES();
3971 return file;
3974 /*********************************************************************
3975 * _fsopen (MSVCRT.@)
3977 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3979 MSVCRT_FILE *ret;
3980 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3982 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3983 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3984 *MSVCRT__errno() = MSVCRT_EINVAL;
3985 return NULL;
3987 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3989 MSVCRT_free(pathW);
3990 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3991 *MSVCRT__errno() = MSVCRT_EINVAL;
3992 return NULL;
3995 ret = MSVCRT__wfsopen(pathW, modeW, share);
3997 MSVCRT_free(pathW);
3998 MSVCRT_free(modeW);
3999 return ret;
4002 /*********************************************************************
4003 * fopen (MSVCRT.@)
4005 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
4007 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
4010 /*********************************************************************
4011 * fopen_s (MSVCRT.@)
4013 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
4014 const char *filename, const char *mode)
4016 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4017 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4018 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4020 *pFile = MSVCRT_fopen(filename, mode);
4022 if(!*pFile)
4023 return *MSVCRT__errno();
4024 return 0;
4027 /*********************************************************************
4028 * _wfopen (MSVCRT.@)
4030 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
4032 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
4035 /*********************************************************************
4036 * _wfopen_s (MSVCRT.@)
4038 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
4039 const MSVCRT_wchar_t *mode)
4041 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4042 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4043 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4045 *pFile = MSVCRT__wfopen(filename, mode);
4047 if(!*pFile)
4048 return *MSVCRT__errno();
4049 return 0;
4052 /*********************************************************************
4053 * fputc (MSVCRT.@)
4055 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
4057 int ret;
4059 MSVCRT__lock_file(file);
4060 ret = MSVCRT__fputc_nolock(c, file);
4061 MSVCRT__unlock_file(file);
4063 return ret;
4066 /*********************************************************************
4067 * _fputc_nolock (MSVCRT.@)
4069 int CDECL MSVCRT__fputc_nolock(int c, MSVCRT_FILE* file)
4071 int res;
4073 if(file->_cnt>0) {
4074 *file->_ptr++=c;
4075 file->_cnt--;
4076 if (c == '\n')
4078 res = msvcrt_flush_buffer(file);
4079 return res ? res : c;
4081 else {
4082 return c & 0xff;
4084 } else {
4085 res = MSVCRT__flsbuf(c, file);
4086 return res;
4090 /*********************************************************************
4091 * _fputchar (MSVCRT.@)
4093 int CDECL MSVCRT__fputchar(int c)
4095 return MSVCRT_fputc(c, MSVCRT_stdout);
4098 /*********************************************************************
4099 * fread (MSVCRT.@)
4101 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4103 MSVCRT_size_t ret;
4105 MSVCRT__lock_file(file);
4106 ret = MSVCRT__fread_nolock(ptr, size, nmemb, file);
4107 MSVCRT__unlock_file(file);
4109 return ret;
4112 /*********************************************************************
4113 * _fread_nolock (MSVCRT.@)
4115 MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4117 MSVCRT_size_t rcnt=size * nmemb;
4118 MSVCRT_size_t read=0;
4119 MSVCRT_size_t pread=0;
4121 if(!rcnt)
4122 return 0;
4124 /* first buffered data */
4125 if(file->_cnt>0) {
4126 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4127 memcpy(ptr, file->_ptr, pcnt);
4128 file->_cnt -= pcnt;
4129 file->_ptr += pcnt;
4130 read += pcnt ;
4131 rcnt -= pcnt ;
4132 ptr = (char*)ptr + pcnt;
4133 } else if(!(file->_flag & MSVCRT__IOREAD )) {
4134 if(file->_flag & MSVCRT__IORW) {
4135 file->_flag |= MSVCRT__IOREAD;
4136 } else {
4137 return 0;
4141 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4142 msvcrt_alloc_buffer(file);
4144 while(rcnt>0)
4146 int i;
4147 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
4148 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
4149 file->_ptr = file->_base;
4150 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
4151 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4152 if (i > 0 && i < file->_cnt) {
4153 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4154 file->_flag &= ~MSVCRT__IOEOF;
4156 if (i > 0) {
4157 memcpy(ptr, file->_ptr, i);
4158 file->_cnt -= i;
4159 file->_ptr += i;
4161 } else if (rcnt > INT_MAX) {
4162 i = MSVCRT__read(file->_file, ptr, INT_MAX);
4163 } else if (rcnt < MSVCRT_BUFSIZ) {
4164 i = MSVCRT__read(file->_file, ptr, rcnt);
4165 } else {
4166 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
4168 pread += i;
4169 rcnt -= i;
4170 ptr = (char *)ptr+i;
4171 /* expose feof condition in the flags
4172 * MFC tests file->_flag for feof, and doesn't call feof())
4174 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4175 file->_flag |= MSVCRT__IOEOF;
4176 else if (i == -1)
4178 file->_flag |= MSVCRT__IOERR;
4179 pread = 0;
4180 rcnt = 0;
4182 if (i < 1) break;
4184 read+=pread;
4185 return read / size;
4189 /*********************************************************************
4190 * fread_s (MSVCR80.@)
4192 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4193 MSVCRT_size_t count, MSVCRT_FILE *stream)
4195 MSVCRT_size_t ret;
4197 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4198 if(buf && buf_size)
4199 memset(buf, 0, buf_size);
4200 return 0;
4202 if(!elem_size || !count) return 0;
4204 MSVCRT__lock_file(stream);
4205 ret = MSVCRT__fread_nolock_s(buf, buf_size, elem_size, count, stream);
4206 MSVCRT__unlock_file(stream);
4208 return ret;
4211 /*********************************************************************
4212 * _fread_nolock_s (MSVCR80.@)
4214 MSVCRT_size_t CDECL MSVCRT__fread_nolock_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4215 MSVCRT_size_t count, MSVCRT_FILE *stream)
4217 size_t bytes_left, buf_pos;
4219 TRACE("(%p %lu %lu %lu %p)\n", buf, buf_size, elem_size, count, stream);
4221 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4222 if(buf && buf_size)
4223 memset(buf, 0, buf_size);
4224 return 0;
4226 if(!elem_size || !count) return 0;
4227 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4228 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4230 bytes_left = elem_size*count;
4231 buf_pos = 0;
4232 while(bytes_left) {
4233 if(stream->_cnt > 0) {
4234 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4236 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4237 memset(buf, 0, buf_size);
4238 return 0;
4241 MSVCRT__fread_nolock((char*)buf+buf_pos, 1, size, stream);
4242 buf_pos += size;
4243 bytes_left -= size;
4244 }else {
4245 int c = MSVCRT__filbuf(stream);
4247 if(c == EOF)
4248 break;
4250 if(!MSVCRT_CHECK_PMT_ERR(buf_size-buf_pos > 0, MSVCRT_ERANGE)) {
4251 memset(buf, 0, buf_size);
4252 return 0;
4255 ((char*)buf)[buf_pos++] = c;
4256 bytes_left--;
4260 return buf_pos/elem_size;
4263 /*********************************************************************
4264 * _wfreopen (MSVCRT.@)
4267 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4269 int open_flags, stream_flags, fd;
4271 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4273 LOCK_FILES();
4274 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
4275 file = NULL;
4276 else
4278 MSVCRT_fclose(file);
4279 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4280 file = NULL;
4281 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4282 file = NULL;
4283 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4285 file->_flag = 0;
4286 file = NULL;
4289 UNLOCK_FILES();
4290 return file;
4293 /*********************************************************************
4294 * _wfreopen_s (MSVCRT.@)
4296 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4297 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4299 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4300 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4301 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4302 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4304 *pFile = MSVCRT__wfreopen(path, mode, file);
4306 if(!*pFile)
4307 return *MSVCRT__errno();
4308 return 0;
4311 /*********************************************************************
4312 * freopen (MSVCRT.@)
4315 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4317 MSVCRT_FILE *ret;
4318 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4320 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4321 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4323 MSVCRT_free(pathW);
4324 return NULL;
4327 ret = MSVCRT__wfreopen(pathW, modeW, file);
4329 MSVCRT_free(pathW);
4330 MSVCRT_free(modeW);
4331 return ret;
4334 /*********************************************************************
4335 * freopen_s (MSVCRT.@)
4337 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4338 const char *path, const char *mode, MSVCRT_FILE* file)
4340 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4341 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4342 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4343 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4345 *pFile = MSVCRT_freopen(path, mode, file);
4347 if(!*pFile)
4348 return *MSVCRT__errno();
4349 return 0;
4352 /*********************************************************************
4353 * fsetpos (MSVCRT.@)
4355 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4357 int ret;
4359 MSVCRT__lock_file(file);
4360 /* Note that all this has been lifted 'as is' from fseek */
4361 if(file->_flag & MSVCRT__IOWRT)
4362 msvcrt_flush_buffer(file);
4364 /* Discard buffered input */
4365 file->_cnt = 0;
4366 file->_ptr = file->_base;
4368 /* Reset direction of i/o */
4369 if(file->_flag & MSVCRT__IORW) {
4370 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4373 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
4374 MSVCRT__unlock_file(file);
4375 return ret;
4378 /*********************************************************************
4379 * _ftelli64 (MSVCRT.@)
4381 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4383 __int64 ret;
4385 MSVCRT__lock_file(file);
4386 ret = MSVCRT__ftelli64_nolock(file);
4387 MSVCRT__unlock_file(file);
4389 return ret;
4392 /*********************************************************************
4393 * _ftelli64_nolock (MSVCRT.@)
4395 __int64 CDECL MSVCRT__ftelli64_nolock(MSVCRT_FILE* file)
4397 __int64 pos;
4399 pos = _telli64(file->_file);
4400 if(pos == -1)
4401 return -1;
4402 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4403 if(file->_flag & MSVCRT__IOWRT) {
4404 pos += file->_ptr - file->_base;
4406 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4407 char *p;
4409 for(p=file->_base; p<file->_ptr; p++)
4410 if(*p == '\n')
4411 pos++;
4413 } else if(!file->_cnt) { /* nothing to do */
4414 } else if(MSVCRT__lseeki64(file->_file, 0, SEEK_END)==pos) {
4415 int i;
4417 pos -= file->_cnt;
4418 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4419 for(i=0; i<file->_cnt; i++)
4420 if(file->_ptr[i] == '\n')
4421 pos--;
4423 } else {
4424 char *p;
4426 if(MSVCRT__lseeki64(file->_file, pos, SEEK_SET) != pos)
4427 return -1;
4429 pos -= file->_bufsiz;
4430 pos += file->_ptr - file->_base;
4432 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4433 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4434 pos--;
4436 for(p=file->_base; p<file->_ptr; p++)
4437 if(*p == '\n')
4438 pos++;
4443 return pos;
4446 /*********************************************************************
4447 * ftell (MSVCRT.@)
4449 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4451 return MSVCRT__ftelli64(file);
4454 /*********************************************************************
4455 * _ftell_nolock (MSVCRT.@)
4457 LONG CDECL MSVCRT__ftell_nolock(MSVCRT_FILE* file)
4459 return MSVCRT__ftelli64_nolock(file);
4462 /*********************************************************************
4463 * fgetpos (MSVCRT.@)
4465 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4467 *pos = MSVCRT__ftelli64(file);
4468 if(*pos == -1)
4469 return -1;
4470 return 0;
4473 /*********************************************************************
4474 * fputs (MSVCRT.@)
4476 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4478 MSVCRT_size_t len = strlen(s);
4479 int ret;
4481 MSVCRT__lock_file(file);
4482 ret = MSVCRT__fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4483 MSVCRT__unlock_file(file);
4484 return ret;
4487 /*********************************************************************
4488 * fputws (MSVCRT.@)
4490 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4492 MSVCRT_size_t i, len = strlenW(s);
4493 BOOL tmp_buf;
4494 int ret;
4496 MSVCRT__lock_file(file);
4497 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4498 ret = MSVCRT__fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4499 MSVCRT__unlock_file(file);
4500 return ret;
4503 tmp_buf = add_std_buffer(file);
4504 for (i=0; i<len; i++) {
4505 if(MSVCRT__fputwc_nolock(s[i], file) == MSVCRT_WEOF) {
4506 if(tmp_buf) remove_std_buffer(file);
4507 MSVCRT__unlock_file(file);
4508 return MSVCRT_WEOF;
4512 if(tmp_buf) remove_std_buffer(file);
4513 MSVCRT__unlock_file(file);
4514 return 0;
4517 /*********************************************************************
4518 * getchar (MSVCRT.@)
4520 int CDECL MSVCRT_getchar(void)
4522 return MSVCRT_fgetc(MSVCRT_stdin);
4525 /*********************************************************************
4526 * getc (MSVCRT.@)
4528 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4530 return MSVCRT_fgetc(file);
4533 /*********************************************************************
4534 * gets (MSVCRT.@)
4536 char * CDECL MSVCRT_gets(char *buf)
4538 int cc;
4539 char * buf_start = buf;
4541 MSVCRT__lock_file(MSVCRT_stdin);
4542 for(cc = MSVCRT__fgetc_nolock(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4543 cc = MSVCRT__fgetc_nolock(MSVCRT_stdin))
4544 if(cc != '\r') *buf++ = (char)cc;
4546 *buf = '\0';
4548 TRACE("got '%s'\n", buf_start);
4549 MSVCRT__unlock_file(MSVCRT_stdin);
4550 return buf_start;
4553 /*********************************************************************
4554 * _getws (MSVCRT.@)
4556 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4558 MSVCRT_wint_t cc;
4559 MSVCRT_wchar_t* ws = buf;
4561 MSVCRT__lock_file(MSVCRT_stdin);
4562 for (cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4563 cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin))
4565 if (cc != '\r')
4566 *buf++ = (MSVCRT_wchar_t)cc;
4568 *buf = '\0';
4570 TRACE("got %s\n", debugstr_w(ws));
4571 MSVCRT__unlock_file(MSVCRT_stdin);
4572 return ws;
4575 /*********************************************************************
4576 * putc (MSVCRT.@)
4578 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4580 return MSVCRT_fputc(c, file);
4583 /*********************************************************************
4584 * putchar (MSVCRT.@)
4586 int CDECL MSVCRT_putchar(int c)
4588 return MSVCRT_fputc(c, MSVCRT_stdout);
4591 /*********************************************************************
4592 * _putwch (MSVCRT.@)
4594 int CDECL MSVCRT__putwch(int c)
4596 return MSVCRT_fputwc(c, MSVCRT_stdout);
4599 /*********************************************************************
4600 * puts (MSVCRT.@)
4602 int CDECL MSVCRT_puts(const char *s)
4604 MSVCRT_size_t len = strlen(s);
4605 int ret;
4607 MSVCRT__lock_file(MSVCRT_stdout);
4608 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4609 MSVCRT__unlock_file(MSVCRT_stdout);
4610 return MSVCRT_EOF;
4613 ret = MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4614 MSVCRT__unlock_file(MSVCRT_stdout);
4615 return ret;
4618 /*********************************************************************
4619 * _putws (MSVCRT.@)
4621 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4623 static const MSVCRT_wchar_t nl = '\n';
4624 MSVCRT_size_t len = strlenW(s);
4625 int ret;
4627 MSVCRT__lock_file(MSVCRT_stdout);
4628 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4629 MSVCRT__unlock_file(MSVCRT_stdout);
4630 return MSVCRT_EOF;
4633 ret = MSVCRT__fwrite_nolock(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4634 MSVCRT__unlock_file(MSVCRT_stdout);
4635 return ret;
4638 /*********************************************************************
4639 * remove (MSVCRT.@)
4641 int CDECL MSVCRT_remove(const char *path)
4643 TRACE("(%s)\n",path);
4644 if (DeleteFileA(path))
4645 return 0;
4646 TRACE(":failed (%d)\n",GetLastError());
4647 msvcrt_set_errno(GetLastError());
4648 return -1;
4651 /*********************************************************************
4652 * _wremove (MSVCRT.@)
4654 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4656 TRACE("(%s)\n",debugstr_w(path));
4657 if (DeleteFileW(path))
4658 return 0;
4659 TRACE(":failed (%d)\n",GetLastError());
4660 msvcrt_set_errno(GetLastError());
4661 return -1;
4664 /*********************************************************************
4665 * rename (MSVCRT.@)
4667 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4669 TRACE(":from %s to %s\n",oldpath,newpath);
4670 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4671 return 0;
4672 TRACE(":failed (%d)\n",GetLastError());
4673 msvcrt_set_errno(GetLastError());
4674 return -1;
4677 /*********************************************************************
4678 * _wrename (MSVCRT.@)
4680 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4682 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4683 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4684 return 0;
4685 TRACE(":failed (%d)\n",GetLastError());
4686 msvcrt_set_errno(GetLastError());
4687 return -1;
4690 /*********************************************************************
4691 * setvbuf (MSVCRT.@)
4693 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4695 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4696 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4697 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4699 MSVCRT__lock_file(file);
4701 MSVCRT__fflush_nolock(file);
4702 if(file->_flag & MSVCRT__IOMYBUF)
4703 MSVCRT_free(file->_base);
4704 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4705 file->_cnt = 0;
4707 if(mode == MSVCRT__IONBF) {
4708 file->_flag |= MSVCRT__IONBF;
4709 file->_base = file->_ptr = (char*)&file->_charbuf;
4710 file->_bufsiz = 2;
4711 }else if(buf) {
4712 file->_base = file->_ptr = buf;
4713 file->_flag |= MSVCRT__USERBUF;
4714 file->_bufsiz = size;
4715 }else {
4716 file->_base = file->_ptr = MSVCRT_malloc(size);
4717 if(!file->_base) {
4718 file->_bufsiz = 0;
4719 MSVCRT__unlock_file(file);
4720 return -1;
4723 file->_flag |= MSVCRT__IOMYBUF;
4724 file->_bufsiz = size;
4726 MSVCRT__unlock_file(file);
4727 return 0;
4730 /*********************************************************************
4731 * setbuf (MSVCRT.@)
4733 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4735 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4738 /*********************************************************************
4739 * tmpnam (MSVCRT.@)
4741 char * CDECL MSVCRT_tmpnam(char *s)
4743 char tmpstr[16];
4744 char *p;
4745 int count, size;
4747 if (!s) {
4748 thread_data_t *data = msvcrt_get_thread_data();
4750 if(!data->tmpnam_buffer)
4751 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4753 s = data->tmpnam_buffer;
4756 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4757 p = s + sprintf(s, "\\s%s.", tmpstr);
4758 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4760 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
4761 memcpy(p, tmpstr, size);
4762 p[size] = '\0';
4763 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4764 GetLastError() == ERROR_FILE_NOT_FOUND)
4765 break;
4767 return s;
4770 /*********************************************************************
4771 * _wtmpnam (MSVCRT.@)
4773 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
4775 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
4776 MSVCRT_wchar_t tmpstr[16];
4777 MSVCRT_wchar_t *p;
4778 int count, size;
4779 if (!s) {
4780 thread_data_t *data = msvcrt_get_thread_data();
4782 if(!data->wtmpnam_buffer)
4783 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4785 s = data->wtmpnam_buffer;
4788 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4789 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
4790 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4792 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
4793 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
4794 p[size] = '\0';
4795 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4796 GetLastError() == ERROR_FILE_NOT_FOUND)
4797 break;
4799 return s;
4802 /*********************************************************************
4803 * tmpfile (MSVCRT.@)
4805 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4807 char *filename = MSVCRT_tmpnam(NULL);
4808 int fd;
4809 MSVCRT_FILE* file = NULL;
4811 LOCK_FILES();
4812 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4813 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4814 if (fd != -1 && (file = msvcrt_alloc_fp()))
4816 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4818 file->_flag = 0;
4819 file = NULL;
4821 else file->_tmpfname = MSVCRT__strdup(filename);
4824 if(fd != -1 && !file)
4825 MSVCRT__close(fd);
4826 UNLOCK_FILES();
4827 return file;
4830 /*********************************************************************
4831 * tmpfile_s (MSVCRT.@)
4833 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4835 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4837 *file = MSVCRT_tmpfile();
4838 return 0;
4841 static int puts_clbk_file_a(void *file, int len, const char *str)
4843 return MSVCRT_fwrite(str, sizeof(char), len, file);
4846 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4848 int i, ret;
4850 MSVCRT__lock_file(file);
4852 if(!(get_ioinfo_nolock(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4853 ret = MSVCRT__fwrite_nolock(str, sizeof(MSVCRT_wchar_t), len, file);
4854 MSVCRT__unlock_file(file);
4855 return ret;
4858 for(i=0; i<len; i++) {
4859 if(MSVCRT__fputwc_nolock(str[i], file) == MSVCRT_WEOF) {
4860 MSVCRT__unlock_file(file);
4861 return -1;
4865 MSVCRT__unlock_file(file);
4866 return len;
4869 /*********************************************************************
4870 * vfprintf (MSVCRT.@)
4872 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4874 BOOL tmp_buf;
4875 int ret;
4877 MSVCRT__lock_file(file);
4878 tmp_buf = add_std_buffer(file);
4879 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4880 if(tmp_buf) remove_std_buffer(file);
4881 MSVCRT__unlock_file(file);
4883 return ret;
4886 /*********************************************************************
4887 * vfprintf_s (MSVCRT.@)
4889 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4891 BOOL tmp_buf;
4892 int ret;
4894 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4896 MSVCRT__lock_file(file);
4897 tmp_buf = add_std_buffer(file);
4898 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4899 if(tmp_buf) remove_std_buffer(file);
4900 MSVCRT__unlock_file(file);
4902 return ret;
4905 /*********************************************************************
4906 * vfwprintf (MSVCRT.@)
4908 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4910 BOOL tmp_buf;
4911 int ret;
4913 MSVCRT__lock_file(file);
4914 tmp_buf = add_std_buffer(file);
4915 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4916 if(tmp_buf) remove_std_buffer(file);
4917 MSVCRT__unlock_file(file);
4919 return ret;
4922 /*********************************************************************
4923 * vfwprintf_s (MSVCRT.@)
4925 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4927 BOOL tmp_buf;
4928 int ret;
4930 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4932 MSVCRT__lock_file(file);
4933 tmp_buf = add_std_buffer(file);
4934 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4935 if(tmp_buf) remove_std_buffer(file);
4936 MSVCRT__unlock_file(file);
4938 return ret;
4941 /*********************************************************************
4942 * _vfwprintf_l (MSVCRT.@)
4944 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
4945 MSVCRT__locale_t locale, __ms_va_list valist)
4947 BOOL tmp_buf;
4948 int ret;
4950 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4952 MSVCRT__lock_file(file);
4953 tmp_buf = add_std_buffer(file);
4954 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4955 if(tmp_buf) remove_std_buffer(file);
4956 MSVCRT__unlock_file(file);
4958 return ret;
4961 /*********************************************************************
4962 * vprintf (MSVCRT.@)
4964 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
4966 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
4969 /*********************************************************************
4970 * vprintf_s (MSVCRT.@)
4972 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
4974 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
4977 /*********************************************************************
4978 * vwprintf (MSVCRT.@)
4980 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
4982 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
4985 /*********************************************************************
4986 * vwprintf_s (MSVCRT.@)
4988 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
4990 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
4993 /*********************************************************************
4994 * fprintf (MSVCRT.@)
4996 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
4998 __ms_va_list valist;
4999 int res;
5000 __ms_va_start(valist, format);
5001 res = MSVCRT_vfprintf(file, format, valist);
5002 __ms_va_end(valist);
5003 return res;
5006 /*********************************************************************
5007 * fprintf_s (MSVCRT.@)
5009 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
5011 __ms_va_list valist;
5012 int res;
5013 __ms_va_start(valist, format);
5014 res = MSVCRT_vfprintf_s(file, format, valist);
5015 __ms_va_end(valist);
5016 return res;
5019 /*********************************************************************
5020 * fwprintf (MSVCRT.@)
5022 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5024 __ms_va_list valist;
5025 int res;
5026 __ms_va_start(valist, format);
5027 res = MSVCRT_vfwprintf(file, format, valist);
5028 __ms_va_end(valist);
5029 return res;
5032 /*********************************************************************
5033 * fwprintf_s (MSVCRT.@)
5035 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5037 __ms_va_list valist;
5038 int res;
5039 __ms_va_start(valist, format);
5040 res = MSVCRT_vfwprintf_s(file, format, valist);
5041 __ms_va_end(valist);
5042 return res;
5045 /*********************************************************************
5046 * _fwprintf_l (MSVCRT.@)
5048 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
5050 __ms_va_list valist;
5051 int res;
5052 __ms_va_start(valist, locale);
5053 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
5054 __ms_va_end(valist);
5055 return res;
5058 /*********************************************************************
5059 * printf (MSVCRT.@)
5061 int CDECL MSVCRT_printf(const char *format, ...)
5063 __ms_va_list valist;
5064 int res;
5065 __ms_va_start(valist, format);
5066 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
5067 __ms_va_end(valist);
5068 return res;
5071 /*********************************************************************
5072 * printf_s (MSVCRT.@)
5074 int CDECL MSVCRT_printf_s(const char *format, ...)
5076 __ms_va_list valist;
5077 int res;
5078 __ms_va_start(valist, format);
5079 res = MSVCRT_vprintf_s(format, valist);
5080 __ms_va_end(valist);
5081 return res;
5084 /*********************************************************************
5085 * ungetc (MSVCRT.@)
5087 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
5089 int ret;
5091 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5093 MSVCRT__lock_file(file);
5094 ret = MSVCRT__ungetc_nolock(c, file);
5095 MSVCRT__unlock_file(file);
5097 return ret;
5100 /*********************************************************************
5101 * _ungetc_nolock (MSVCRT.@)
5103 int CDECL MSVCRT__ungetc_nolock(int c, MSVCRT_FILE * file)
5105 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5107 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
5108 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
5109 return MSVCRT_EOF;
5111 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
5112 && msvcrt_alloc_buffer(file))
5113 || (!file->_cnt && file->_ptr==file->_base))
5114 file->_ptr++;
5116 if(file->_ptr>file->_base) {
5117 file->_ptr--;
5118 if(file->_flag & MSVCRT__IOSTRG) {
5119 if(*file->_ptr != c) {
5120 file->_ptr++;
5121 return MSVCRT_EOF;
5123 }else {
5124 *file->_ptr = c;
5126 file->_cnt++;
5127 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
5128 file->_flag |= MSVCRT__IOREAD;
5129 return c;
5132 return MSVCRT_EOF;
5135 /*********************************************************************
5136 * ungetwc (MSVCRT.@)
5138 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5140 MSVCRT_wint_t ret;
5142 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5144 MSVCRT__lock_file(file);
5145 ret = MSVCRT__ungetwc_nolock(wc, file);
5146 MSVCRT__unlock_file(file);
5148 return ret;
5151 /*********************************************************************
5152 * _ungetwc_nolock (MSVCRT.@)
5154 MSVCRT_wint_t CDECL MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5156 MSVCRT_wchar_t mwc = wc;
5158 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5159 if (wc == MSVCRT_WEOF)
5160 return MSVCRT_WEOF;
5162 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5163 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5164 unsigned char * pp = (unsigned char *)&mwc;
5165 int i;
5167 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
5168 if(pp[i] != MSVCRT__ungetc_nolock(pp[i],file))
5169 return MSVCRT_WEOF;
5171 }else {
5172 char mbs[MSVCRT_MB_LEN_MAX];
5173 int len;
5175 len = MSVCRT_wctomb(mbs, mwc);
5176 if(len == -1)
5177 return MSVCRT_WEOF;
5179 for(len--; len>=0; len--) {
5180 if(mbs[len] != MSVCRT__ungetc_nolock(mbs[len], file))
5181 return MSVCRT_WEOF;
5185 return mwc;
5188 /*********************************************************************
5189 * wprintf (MSVCRT.@)
5191 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
5193 __ms_va_list valist;
5194 int res;
5195 __ms_va_start(valist, format);
5196 res = MSVCRT_vwprintf(format, valist);
5197 __ms_va_end(valist);
5198 return res;
5201 /*********************************************************************
5202 * wprintf_s (MSVCRT.@)
5204 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
5206 __ms_va_list valist;
5207 int res;
5208 __ms_va_start(valist, format);
5209 res = MSVCRT_vwprintf_s(format, valist);
5210 __ms_va_end(valist);
5211 return res;
5214 /*********************************************************************
5215 * _getmaxstdio (MSVCRT.@)
5217 int CDECL MSVCRT__getmaxstdio(void)
5219 return MSVCRT_max_streams;
5222 /*********************************************************************
5223 * _setmaxstdio (MSVCRT.@)
5225 int CDECL MSVCRT__setmaxstdio(int newmax)
5227 TRACE("%d\n", newmax);
5229 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5230 return -1;
5232 MSVCRT_max_streams = newmax;
5233 return MSVCRT_max_streams;