riched20: Implement ITextRange::GetChar.
[wine/multimedia.git] / dlls / msvcrt / file.c
blob7ce8b5912eade52900f140337dfc6ed94af7ebd5
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* msvcrt_get_ioinfo(int fd)
251 ioinfo *ret = NULL;
252 if(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 MSVCRT_FILE* msvcrt_get_file(int i)
262 file_crit *ret;
264 if(i >= MSVCRT_max_streams)
265 return NULL;
267 if(i < _IOB_ENTRIES)
268 return &MSVCRT__iob[i];
270 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
271 if(!ret) {
272 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
273 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
274 ERR("out of memory\n");
275 *MSVCRT__errno() = MSVCRT_ENOMEM;
276 return NULL;
279 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
280 } else
281 ret += i%MSVCRT_FD_BLOCK_SIZE;
283 return &ret->file;
286 static inline BOOL msvcrt_is_valid_fd(int fd)
288 return fd >= 0 && fd < MSVCRT_fdend && (msvcrt_get_ioinfo(fd)->wxflag & WX_OPEN);
291 /* INTERNAL: Get the HANDLE for a fd
292 * This doesn't lock the table, because a failure will result in
293 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
294 * it returns a valid handle which is about to be closed, a subsequent call
295 * will fail, most likely in a sane way.
297 static HANDLE msvcrt_fdtoh(int fd)
299 if (!msvcrt_is_valid_fd(fd))
301 WARN(":fd (%d) - no handle!\n",fd);
302 *MSVCRT___doserrno() = 0;
303 *MSVCRT__errno() = MSVCRT_EBADF;
304 return INVALID_HANDLE_VALUE;
306 if (msvcrt_get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
307 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd);
308 return msvcrt_get_ioinfo(fd)->handle;
311 /* INTERNAL: free a file entry fd */
312 static void msvcrt_free_fd(int fd)
314 ioinfo *fdinfo;
316 LOCK_FILES();
317 fdinfo = msvcrt_get_ioinfo(fd);
318 if(fdinfo != &MSVCRT___badioinfo)
320 fdinfo->handle = INVALID_HANDLE_VALUE;
321 fdinfo->wxflag = 0;
323 TRACE(":fd (%d) freed\n",fd);
325 if (fd < 3)
327 switch (fd)
329 case 0:
330 SetStdHandle(STD_INPUT_HANDLE, 0);
331 break;
332 case 1:
333 SetStdHandle(STD_OUTPUT_HANDLE, 0);
334 break;
335 case 2:
336 SetStdHandle(STD_ERROR_HANDLE, 0);
337 break;
341 if (fd == MSVCRT_fdend - 1)
342 MSVCRT_fdend--;
343 if (fd < MSVCRT_fdstart)
344 MSVCRT_fdstart = fd;
345 UNLOCK_FILES();
348 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
349 /* caller must hold the files lock */
350 static int msvcrt_set_fd(HANDLE hand, int flag, int fd)
352 ioinfo *fdinfo;
354 if (fd >= MSVCRT_MAX_FILES)
356 WARN(":files exhausted!\n");
357 *MSVCRT__errno() = MSVCRT_ENFILE;
358 return -1;
361 fdinfo = msvcrt_get_ioinfo(fd);
362 if(fdinfo == &MSVCRT___badioinfo) {
363 int i;
365 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
366 if(!MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE]) {
367 WARN(":out of memory!\n");
368 *MSVCRT__errno() = MSVCRT_ENOMEM;
369 return -1;
372 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
373 MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE][i].handle = INVALID_HANDLE_VALUE;
375 fdinfo = msvcrt_get_ioinfo(fd);
378 fdinfo->handle = hand;
379 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_TTY));
380 fdinfo->lookahead[0] = '\n';
381 fdinfo->lookahead[1] = '\n';
382 fdinfo->lookahead[2] = '\n';
383 fdinfo->exflag = 0;
385 /* locate next free slot */
386 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
387 MSVCRT_fdstart = MSVCRT_fdend + 1;
388 else
389 while (MSVCRT_fdstart < MSVCRT_fdend &&
390 msvcrt_get_ioinfo(MSVCRT_fdstart)->handle != INVALID_HANDLE_VALUE)
391 MSVCRT_fdstart++;
392 /* update last fd in use */
393 if (fd >= MSVCRT_fdend)
394 MSVCRT_fdend = fd + 1;
395 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
397 switch (fd)
399 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
400 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
401 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
404 return fd;
407 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
408 static int msvcrt_alloc_fd(HANDLE hand, int flag)
410 int ret;
412 LOCK_FILES();
413 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
414 ret = msvcrt_set_fd(hand, flag, MSVCRT_fdstart);
415 UNLOCK_FILES();
416 return ret;
419 /* INTERNAL: Allocate a FILE* for an fd slot */
420 /* caller must hold the files lock */
421 static MSVCRT_FILE* msvcrt_alloc_fp(void)
423 int i;
424 MSVCRT_FILE *file;
426 for (i = 3; i < MSVCRT_max_streams; i++)
428 file = msvcrt_get_file(i);
429 if (!file)
430 return NULL;
432 if (file->_flag == 0)
434 if (i == MSVCRT_stream_idx)
436 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
438 InitializeCriticalSection(&((file_crit*)file)->crit);
439 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
441 MSVCRT_stream_idx++;
443 return file;
447 return NULL;
450 /* INTERNAL: initialize a FILE* from an open fd */
451 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
453 TRACE(":fd (%d) allocating FILE*\n",fd);
454 if (!msvcrt_is_valid_fd(fd))
456 WARN(":invalid fd %d\n",fd);
457 *MSVCRT___doserrno() = 0;
458 *MSVCRT__errno() = MSVCRT_EBADF;
459 return -1;
461 file->_ptr = file->_base = NULL;
462 file->_cnt = 0;
463 file->_file = fd;
464 file->_flag = stream_flags;
465 file->_tmpfname = NULL;
467 TRACE(":got FILE* (%p)\n",file);
468 return 0;
471 /* INTERNAL: Create an inheritance data block (for spawned process)
472 * The inheritance block is made of:
473 * 00 int nb of file descriptor (NBFD)
474 * 04 char file flags (wxflag): repeated for each fd
475 * 4+NBFD HANDLE file handle: repeated for each fd
477 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
479 int fd;
480 char* wxflag_ptr;
481 HANDLE* handle_ptr;
482 ioinfo* fdinfo;
484 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
485 *block = MSVCRT_calloc(*size, 1);
486 if (!*block)
488 *size = 0;
489 return FALSE;
491 wxflag_ptr = (char*)*block + sizeof(unsigned);
492 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
494 *(unsigned*)*block = MSVCRT_fdend;
495 for (fd = 0; fd < MSVCRT_fdend; fd++)
497 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
498 fdinfo = msvcrt_get_ioinfo(fd);
499 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
501 *wxflag_ptr = fdinfo->wxflag;
502 *handle_ptr = fdinfo->handle;
504 else
506 *wxflag_ptr = 0;
507 *handle_ptr = INVALID_HANDLE_VALUE;
509 wxflag_ptr++; handle_ptr++;
511 return TRUE;
514 /* INTERNAL: Set up all file descriptors,
515 * as well as default streams (stdin, stderr and stdout)
517 void msvcrt_init_io(void)
519 STARTUPINFOA si;
520 int i;
521 ioinfo *fdinfo;
523 GetStartupInfoA(&si);
524 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
526 BYTE* wxflag_ptr;
527 HANDLE* handle_ptr;
528 unsigned int count;
530 count = *(unsigned*)si.lpReserved2;
531 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
532 handle_ptr = (HANDLE*)(wxflag_ptr + count);
534 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
535 count = min(count, MSVCRT_MAX_FILES);
536 for (i = 0; i < count; i++)
538 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
539 msvcrt_set_fd(*handle_ptr, *wxflag_ptr, i);
541 wxflag_ptr++; handle_ptr++;
543 MSVCRT_fdend = max( 3, count );
544 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
545 if (msvcrt_get_ioinfo(MSVCRT_fdstart)->handle == INVALID_HANDLE_VALUE) break;
548 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO);
549 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
550 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
551 DWORD type = GetFileType(h);
553 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
554 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDIN_FILENO);
557 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO);
558 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
559 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
560 DWORD type = GetFileType(h);
562 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
563 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDOUT_FILENO);
566 fdinfo = msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO);
567 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
568 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
569 DWORD type = GetFileType(h);
571 msvcrt_set_fd(h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
572 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0), MSVCRT_STDERR_FILENO);
575 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO)->handle,
576 msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO)->handle,
577 msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO)->handle);
579 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
580 for (i = 0; i < 3; i++)
582 /* FILE structs for stdin/out/err are static and never deleted */
583 MSVCRT__iob[i]._file = i;
584 MSVCRT__iob[i]._tmpfname = NULL;
585 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
587 MSVCRT_stream_idx = 3;
590 /* INTERNAL: Flush stdio file buffer */
591 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
593 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
594 int cnt=file->_ptr-file->_base;
595 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
596 file->_flag |= MSVCRT__IOERR;
597 return MSVCRT_EOF;
599 file->_ptr=file->_base;
600 file->_cnt=0;
602 return 0;
605 /*********************************************************************
606 * _isatty (MSVCRT.@)
608 int CDECL MSVCRT__isatty(int fd)
610 TRACE(":fd (%d)\n",fd);
612 return msvcrt_get_ioinfo(fd)->wxflag & WX_TTY;
615 /* INTERNAL: Allocate stdio file buffer */
616 static BOOL msvcrt_alloc_buffer(MSVCRT_FILE* file)
618 if((file->_file==MSVCRT_STDOUT_FILENO || file->_file==MSVCRT_STDERR_FILENO)
619 && MSVCRT__isatty(file->_file))
620 return FALSE;
622 file->_base = MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ,1);
623 if(file->_base) {
624 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
625 file->_flag |= MSVCRT__IOMYBUF;
626 } else {
627 file->_base = (char*)(&file->_charbuf);
628 file->_bufsiz = 2;
629 file->_flag |= MSVCRT__IONBF;
631 file->_ptr = file->_base;
632 file->_cnt = 0;
633 return TRUE;
636 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
637 static BOOL add_std_buffer(MSVCRT_FILE *file)
639 static char buffers[2][MSVCRT_BUFSIZ];
641 if((file->_file!=MSVCRT_STDOUT_FILENO && file->_file!=MSVCRT_STDERR_FILENO)
642 || (file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
643 || !MSVCRT__isatty(file->_file))
644 return FALSE;
646 file->_ptr = file->_base = buffers[file->_file == MSVCRT_STDOUT_FILENO ? 0 : 1];
647 file->_bufsiz = file->_cnt = MSVCRT_BUFSIZ;
648 file->_flag |= MSVCRT__USERBUF;
649 return TRUE;
652 /* INTERNAL: Removes temporary buffer from stdout or stderr */
653 /* Only call this function when add_std_buffer returned TRUE */
654 static void remove_std_buffer(MSVCRT_FILE *file)
656 msvcrt_flush_buffer(file);
657 file->_ptr = file->_base = NULL;
658 file->_bufsiz = file->_cnt = 0;
659 file->_flag &= ~MSVCRT__USERBUF;
662 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
663 static int msvcrt_int_to_base32(int num, char *str)
665 char *p;
666 int n = num;
667 int digits = 0;
669 while (n != 0)
671 n >>= 5;
672 digits++;
674 p = str + digits;
675 *p = 0;
676 while (--p >= str)
678 *p = (num & 31) + '0';
679 if (*p > '9')
680 *p += ('a' - '0' - 10);
681 num >>= 5;
684 return digits;
687 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
688 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
690 MSVCRT_wchar_t *p;
691 int n = num;
692 int digits = 0;
694 while (n != 0)
696 n >>= 5;
697 digits++;
699 p = str + digits;
700 *p = 0;
701 while (--p >= str)
703 *p = (num & 31) + '0';
704 if (*p > '9')
705 *p += ('a' - '0' - 10);
706 num >>= 5;
709 return digits;
712 /*********************************************************************
713 * __iob_func(MSVCRT.@)
715 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
717 return &MSVCRT__iob[0];
720 /*********************************************************************
721 * _access (MSVCRT.@)
723 int CDECL MSVCRT__access(const char *filename, int mode)
725 DWORD attr = GetFileAttributesA(filename);
727 TRACE("(%s,%d) %d\n",filename,mode,attr);
729 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
731 msvcrt_set_errno(GetLastError());
732 return -1;
734 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
736 msvcrt_set_errno(ERROR_ACCESS_DENIED);
737 return -1;
739 return 0;
742 /*********************************************************************
743 * _access_s (MSVCRT.@)
745 int CDECL MSVCRT__access_s(const char *filename, int mode)
747 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
748 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
750 if (MSVCRT__access(filename, mode) == -1)
751 return *MSVCRT__errno();
752 return 0;
755 /*********************************************************************
756 * _waccess (MSVCRT.@)
758 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
760 DWORD attr = GetFileAttributesW(filename);
762 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
764 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
766 msvcrt_set_errno(GetLastError());
767 return -1;
769 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
771 msvcrt_set_errno(ERROR_ACCESS_DENIED);
772 return -1;
774 return 0;
777 /*********************************************************************
778 * _waccess_s (MSVCRT.@)
780 int CDECL MSVCRT__waccess_s(const MSVCRT_wchar_t *filename, int mode)
782 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
783 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
785 if (MSVCRT__waccess(filename, mode) == -1)
786 return *MSVCRT__errno();
787 return 0;
790 /*********************************************************************
791 * _chmod (MSVCRT.@)
793 int CDECL MSVCRT__chmod(const char *path, int flags)
795 DWORD oldFlags = GetFileAttributesA(path);
797 if (oldFlags != INVALID_FILE_ATTRIBUTES)
799 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
800 oldFlags | FILE_ATTRIBUTE_READONLY;
802 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
803 return 0;
805 msvcrt_set_errno(GetLastError());
806 return -1;
809 /*********************************************************************
810 * _wchmod (MSVCRT.@)
812 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
814 DWORD oldFlags = GetFileAttributesW(path);
816 if (oldFlags != INVALID_FILE_ATTRIBUTES)
818 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
819 oldFlags | FILE_ATTRIBUTE_READONLY;
821 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
822 return 0;
824 msvcrt_set_errno(GetLastError());
825 return -1;
828 /*********************************************************************
829 * _unlink (MSVCRT.@)
831 int CDECL MSVCRT__unlink(const char *path)
833 TRACE("%s\n",debugstr_a(path));
834 if(DeleteFileA(path))
835 return 0;
836 TRACE("failed (%d)\n",GetLastError());
837 msvcrt_set_errno(GetLastError());
838 return -1;
841 /*********************************************************************
842 * _wunlink (MSVCRT.@)
844 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
846 TRACE("(%s)\n",debugstr_w(path));
847 if(DeleteFileW(path))
848 return 0;
849 TRACE("failed (%d)\n",GetLastError());
850 msvcrt_set_errno(GetLastError());
851 return -1;
854 /*********************************************************************
855 * _commit (MSVCRT.@)
857 int CDECL MSVCRT__commit(int fd)
859 HANDLE hand = msvcrt_fdtoh(fd);
861 TRACE(":fd (%d) handle (%p)\n",fd,hand);
862 if (hand == INVALID_HANDLE_VALUE)
863 return -1;
865 if (!FlushFileBuffers(hand))
867 if (GetLastError() == ERROR_INVALID_HANDLE)
869 /* FlushFileBuffers fails for console handles
870 * so we ignore this error.
872 return 0;
874 TRACE(":failed-last error (%d)\n",GetLastError());
875 msvcrt_set_errno(GetLastError());
876 return -1;
878 TRACE(":ok\n");
879 return 0;
882 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
883 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
885 /* INTERNAL: Flush all stream buffer */
886 static int msvcrt_flush_all_buffers(int mask)
888 int i, num_flushed = 0;
889 MSVCRT_FILE *file;
891 LOCK_FILES();
892 for (i = 0; i < MSVCRT_stream_idx; i++) {
893 file = msvcrt_get_file(i);
895 if (file->_flag)
897 if(file->_flag & mask) {
898 MSVCRT_fflush(file);
899 num_flushed++;
903 UNLOCK_FILES();
905 TRACE(":flushed (%d) handles\n",num_flushed);
906 return num_flushed;
909 /*********************************************************************
910 * _flushall (MSVCRT.@)
912 int CDECL MSVCRT__flushall(void)
914 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
917 /*********************************************************************
918 * fflush (MSVCRT.@)
920 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
922 if(!file) {
923 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
924 } else if(file->_flag & MSVCRT__IOWRT) {
925 int res;
927 MSVCRT__lock_file(file);
928 res = msvcrt_flush_buffer(file);
930 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
931 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
932 MSVCRT__unlock_file(file);
934 return res;
935 } else if(file->_flag & MSVCRT__IOREAD) {
936 MSVCRT__lock_file(file);
937 file->_cnt = 0;
938 file->_ptr = file->_base;
939 MSVCRT__unlock_file(file);
941 return 0;
943 return 0;
946 /*********************************************************************
947 * _close (MSVCRT.@)
949 int CDECL MSVCRT__close(int fd)
951 HANDLE hand;
952 int ret;
954 LOCK_FILES();
955 hand = msvcrt_fdtoh(fd);
956 TRACE(":fd (%d) handle (%p)\n",fd,hand);
957 if (!msvcrt_is_valid_fd(fd)) {
958 ret = -1;
959 } else {
960 msvcrt_free_fd(fd);
961 ret = CloseHandle(hand) ? 0 : -1;
962 if (ret) {
963 WARN(":failed-last error (%d)\n",GetLastError());
964 msvcrt_set_errno(GetLastError());
967 UNLOCK_FILES();
968 TRACE(":ok\n");
969 return ret;
972 /*********************************************************************
973 * _dup2 (MSVCRT.@)
974 * NOTES
975 * MSDN isn't clear on this point, but the remarks for _pipe
976 * indicate file descriptors duplicated with _dup and _dup2 are always
977 * inheritable.
979 int CDECL MSVCRT__dup2(int od, int nd)
981 int ret;
983 TRACE("(od=%d, nd=%d)\n", od, nd);
984 LOCK_FILES();
985 if (nd < MSVCRT_MAX_FILES && nd >= 0 && msvcrt_is_valid_fd(od))
987 HANDLE handle;
989 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od)->handle,
990 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
992 int wxflag = msvcrt_get_ioinfo(od)->wxflag & ~MSVCRT__O_NOINHERIT;
994 if (msvcrt_is_valid_fd(nd))
995 MSVCRT__close(nd);
996 ret = msvcrt_set_fd(handle, wxflag, nd);
997 if (ret == -1)
999 CloseHandle(handle);
1000 *MSVCRT__errno() = MSVCRT_EMFILE;
1002 else
1004 /* _dup2 returns 0, not nd, on success */
1005 ret = 0;
1008 else
1010 ret = -1;
1011 msvcrt_set_errno(GetLastError());
1014 else
1016 *MSVCRT__errno() = MSVCRT_EBADF;
1017 ret = -1;
1019 UNLOCK_FILES();
1020 return ret;
1023 /*********************************************************************
1024 * _dup (MSVCRT.@)
1026 int CDECL MSVCRT__dup(int od)
1028 int fd, ret;
1030 LOCK_FILES();
1031 fd = MSVCRT_fdstart;
1032 if (MSVCRT__dup2(od, fd) == 0)
1033 ret = fd;
1034 else
1035 ret = -1;
1036 UNLOCK_FILES();
1037 return ret;
1040 /*********************************************************************
1041 * _eof (MSVCRT.@)
1043 int CDECL MSVCRT__eof(int fd)
1045 DWORD curpos,endpos;
1046 LONG hcurpos,hendpos;
1047 HANDLE hand = msvcrt_fdtoh(fd);
1049 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1051 if (hand == INVALID_HANDLE_VALUE)
1052 return -1;
1054 if (msvcrt_get_ioinfo(fd)->wxflag & WX_ATEOF) return TRUE;
1056 /* Otherwise we do it the hard way */
1057 hcurpos = hendpos = 0;
1058 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
1059 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
1061 if (curpos == endpos && hcurpos == hendpos)
1063 /* FIXME: shouldn't WX_ATEOF be set here? */
1064 return TRUE;
1067 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
1068 return FALSE;
1071 /*********************************************************************
1072 * _fcloseall (MSVCRT.@)
1074 int CDECL MSVCRT__fcloseall(void)
1076 int num_closed = 0, i;
1077 MSVCRT_FILE *file;
1079 LOCK_FILES();
1080 for (i = 3; i < MSVCRT_stream_idx; i++) {
1081 file = msvcrt_get_file(i);
1083 if (file->_flag && !MSVCRT_fclose(file))
1084 num_closed++;
1086 UNLOCK_FILES();
1088 TRACE(":closed (%d) handles\n",num_closed);
1089 return num_closed;
1092 /* free everything on process exit */
1093 void msvcrt_free_io(void)
1095 unsigned int i;
1096 int j;
1098 MSVCRT__flushall();
1099 MSVCRT__fcloseall();
1101 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1102 MSVCRT_free(MSVCRT___pioinfo[i]);
1104 for(j=0; j<MSVCRT_stream_idx; j++)
1106 MSVCRT_FILE *file = msvcrt_get_file(j);
1107 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1109 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1110 DeleteCriticalSection(&((file_crit*)file)->crit);
1114 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1115 MSVCRT_free(MSVCRT_fstream[i]);
1117 DeleteCriticalSection(&MSVCRT_file_cs);
1120 /*********************************************************************
1121 * _lseeki64 (MSVCRT.@)
1123 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1125 HANDLE hand = msvcrt_fdtoh(fd);
1126 LARGE_INTEGER ofs;
1128 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1129 if (hand == INVALID_HANDLE_VALUE)
1130 return -1;
1132 if (whence < 0 || whence > 2)
1134 *MSVCRT__errno() = MSVCRT_EINVAL;
1135 return -1;
1138 TRACE(":fd (%d) to %s pos %s\n",
1139 fd,wine_dbgstr_longlong(offset),
1140 (whence==SEEK_SET)?"SEEK_SET":
1141 (whence==SEEK_CUR)?"SEEK_CUR":
1142 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1144 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1145 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1146 ofs.QuadPart = offset;
1147 if ((ofs.u.LowPart = SetFilePointer(hand, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1148 GetLastError() == ERROR_SUCCESS)
1150 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_ATEOF;
1151 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1153 return ofs.QuadPart;
1155 TRACE(":error-last error (%d)\n",GetLastError());
1156 msvcrt_set_errno(GetLastError());
1157 return -1;
1160 /*********************************************************************
1161 * _lseek (MSVCRT.@)
1163 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1165 return MSVCRT__lseeki64(fd, offset, whence);
1168 /*********************************************************************
1169 * _lock_file (MSVCRT.@)
1171 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1173 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1174 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1175 else
1176 EnterCriticalSection(&((file_crit*)file)->crit);
1179 /*********************************************************************
1180 * _unlock_file (MSVCRT.@)
1182 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1184 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1185 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1186 else
1187 LeaveCriticalSection(&((file_crit*)file)->crit);
1190 /*********************************************************************
1191 * _locking (MSVCRT.@)
1193 * This is untested; the underlying LockFile doesn't work yet.
1195 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1197 BOOL ret;
1198 DWORD cur_locn;
1199 HANDLE hand = msvcrt_fdtoh(fd);
1201 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1202 if (hand == INVALID_HANDLE_VALUE)
1203 return -1;
1205 if (mode < 0 || mode > 4)
1207 *MSVCRT__errno() = MSVCRT_EINVAL;
1208 return -1;
1211 TRACE(":fd (%d) by 0x%08x mode %s\n",
1212 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1213 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1214 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1215 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1216 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1217 "UNKNOWN");
1219 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
1221 FIXME ("Seek failed\n");
1222 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1223 return -1;
1225 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1227 int nretry = 10;
1228 ret = 1; /* just to satisfy gcc */
1229 while (nretry--)
1231 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1232 if (ret) break;
1233 Sleep(1);
1236 else if (mode == MSVCRT__LK_UNLCK)
1237 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
1238 else
1239 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
1240 /* FIXME - what about error settings? */
1241 return ret ? 0 : -1;
1244 /*********************************************************************
1245 * _fseeki64 (MSVCRT.@)
1247 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1249 int ret;
1251 MSVCRT__lock_file(file);
1252 /* Flush output if needed */
1253 if(file->_flag & MSVCRT__IOWRT)
1254 msvcrt_flush_buffer(file);
1256 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1257 whence = SEEK_SET;
1258 offset += MSVCRT__ftelli64(file);
1261 /* Discard buffered input */
1262 file->_cnt = 0;
1263 file->_ptr = file->_base;
1264 /* Reset direction of i/o */
1265 if(file->_flag & MSVCRT__IORW) {
1266 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1268 /* Clear end of file flag */
1269 file->_flag &= ~MSVCRT__IOEOF;
1270 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1272 MSVCRT__unlock_file(file);
1273 return ret;
1276 /*********************************************************************
1277 * fseek (MSVCRT.@)
1279 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1281 return MSVCRT__fseeki64( file, offset, whence );
1284 /*********************************************************************
1285 * _chsize_s (MSVCRT.@)
1287 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1289 __int64 cur, pos;
1290 HANDLE handle;
1291 BOOL ret = FALSE;
1293 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1295 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1297 LOCK_FILES();
1299 handle = msvcrt_fdtoh(fd);
1300 if (handle != INVALID_HANDLE_VALUE)
1302 /* save the current file pointer */
1303 cur = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1304 if (cur >= 0)
1306 pos = MSVCRT__lseeki64(fd, size, SEEK_SET);
1307 if (pos >= 0)
1309 ret = SetEndOfFile(handle);
1310 if (!ret) msvcrt_set_errno(GetLastError());
1313 /* restore the file pointer */
1314 MSVCRT__lseeki64(fd, cur, SEEK_SET);
1318 UNLOCK_FILES();
1319 return ret ? 0 : *MSVCRT__errno();
1322 /*********************************************************************
1323 * _chsize (MSVCRT.@)
1325 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1327 /* _chsize_s returns errno on failure but _chsize should return -1 */
1328 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1331 /*********************************************************************
1332 * clearerr (MSVCRT.@)
1334 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1336 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1338 MSVCRT__lock_file(file);
1339 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1340 MSVCRT__unlock_file(file);
1343 /*********************************************************************
1344 * rewind (MSVCRT.@)
1346 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1348 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1350 MSVCRT__lock_file(file);
1351 MSVCRT_fseek(file, 0L, SEEK_SET);
1352 MSVCRT_clearerr(file);
1353 MSVCRT__unlock_file(file);
1356 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1358 int plus = strchrW(mode, '+') != NULL;
1360 TRACE("%s\n", debugstr_w(mode));
1362 while(*mode == ' ') mode++;
1364 switch(*mode++)
1366 case 'R': case 'r':
1367 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1368 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1369 break;
1370 case 'W': case 'w':
1371 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1372 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1373 break;
1374 case 'A': case 'a':
1375 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1376 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1377 break;
1378 default:
1379 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1380 return -1;
1383 *stream_flags |= MSVCRT__commode;
1385 while (*mode && *mode!=',')
1386 switch (*mode++)
1388 case 'B': case 'b':
1389 *open_flags |= MSVCRT__O_BINARY;
1390 *open_flags &= ~MSVCRT__O_TEXT;
1391 break;
1392 case 't':
1393 *open_flags |= MSVCRT__O_TEXT;
1394 *open_flags &= ~MSVCRT__O_BINARY;
1395 break;
1396 case 'D':
1397 *open_flags |= MSVCRT__O_TEMPORARY;
1398 break;
1399 case 'T':
1400 *open_flags |= MSVCRT__O_SHORT_LIVED;
1401 break;
1402 case 'c':
1403 *stream_flags |= MSVCRT__IOCOMMIT;
1404 break;
1405 case 'n':
1406 *stream_flags &= ~MSVCRT__IOCOMMIT;
1407 break;
1408 case 'N':
1409 *open_flags |= MSVCRT__O_NOINHERIT;
1410 break;
1411 case '+':
1412 case ' ':
1413 case 'a':
1414 case 'w':
1415 break;
1416 case 'S':
1417 case 'R':
1418 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1419 break;
1420 default:
1421 ERR("incorrect mode flag: %c\n", mode[-1]);
1422 break;
1425 if(*mode == ',')
1427 static const WCHAR ccs[] = {'c','c','s'};
1428 static const WCHAR utf8[] = {'u','t','f','-','8'};
1429 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1430 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1432 mode++;
1433 while(*mode == ' ') mode++;
1434 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1435 return -1;
1436 mode += sizeof(ccs)/sizeof(ccs[0]);
1437 while(*mode == ' ') mode++;
1438 if(!MSVCRT_CHECK_PMT(*mode == '='))
1439 return -1;
1440 mode++;
1441 while(*mode == ' ') mode++;
1443 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1445 *open_flags |= MSVCRT__O_U8TEXT;
1446 mode += sizeof(utf8)/sizeof(utf8[0]);
1448 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1450 *open_flags |= MSVCRT__O_U16TEXT;
1451 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1453 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1455 *open_flags |= MSVCRT__O_WTEXT;
1456 mode += sizeof(unicode)/sizeof(unicode[0]);
1458 else
1460 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1461 return -1;
1464 while(*mode == ' ') mode++;
1467 if(!MSVCRT_CHECK_PMT(*mode == 0))
1468 return -1;
1469 return 0;
1472 /*********************************************************************
1473 * _fdopen (MSVCRT.@)
1475 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1477 MSVCRT_FILE *ret;
1478 MSVCRT_wchar_t *modeW = NULL;
1480 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1482 ret = MSVCRT__wfdopen(fd, modeW);
1484 MSVCRT_free(modeW);
1485 return ret;
1488 /*********************************************************************
1489 * _wfdopen (MSVCRT.@)
1491 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1493 int open_flags, stream_flags;
1494 MSVCRT_FILE* file;
1496 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1498 LOCK_FILES();
1499 if (!(file = msvcrt_alloc_fp()))
1500 file = NULL;
1501 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1503 file->_flag = 0;
1504 file = NULL;
1506 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1507 UNLOCK_FILES();
1509 return file;
1512 /*********************************************************************
1513 * _filelength (MSVCRT.@)
1515 LONG CDECL MSVCRT__filelength(int fd)
1517 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1518 if (curPos != -1)
1520 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1521 if (endPos != -1)
1523 if (endPos != curPos)
1524 MSVCRT__lseek(fd, curPos, SEEK_SET);
1525 return endPos;
1528 return -1;
1531 /*********************************************************************
1532 * _filelengthi64 (MSVCRT.@)
1534 __int64 CDECL MSVCRT__filelengthi64(int fd)
1536 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1537 if (curPos != -1)
1539 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1540 if (endPos != -1)
1542 if (endPos != curPos)
1543 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1544 return endPos;
1547 return -1;
1550 /*********************************************************************
1551 * _fileno (MSVCRT.@)
1553 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1555 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1556 return file->_file;
1559 /*********************************************************************
1560 * _fstat64 (MSVCRT.@)
1562 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1564 DWORD dw;
1565 DWORD type;
1566 BY_HANDLE_FILE_INFORMATION hfi;
1567 HANDLE hand = msvcrt_fdtoh(fd);
1569 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1570 if (hand == INVALID_HANDLE_VALUE)
1571 return -1;
1573 if (!buf)
1575 WARN(":failed-NULL buf\n");
1576 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1577 return -1;
1580 memset(&hfi, 0, sizeof(hfi));
1581 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1582 type = GetFileType(hand);
1583 if (type == FILE_TYPE_PIPE)
1585 buf->st_dev = buf->st_rdev = fd;
1586 buf->st_mode = S_IFIFO;
1587 buf->st_nlink = 1;
1589 else if (type == FILE_TYPE_CHAR)
1591 buf->st_dev = buf->st_rdev = fd;
1592 buf->st_mode = S_IFCHR;
1593 buf->st_nlink = 1;
1595 else /* FILE_TYPE_DISK etc. */
1597 if (!GetFileInformationByHandle(hand, &hfi))
1599 WARN(":failed-last error (%d)\n",GetLastError());
1600 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1601 return -1;
1603 buf->st_mode = S_IFREG | 0444;
1604 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1605 buf->st_mode |= 0222;
1606 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1607 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1608 buf->st_atime = dw;
1609 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1610 buf->st_mtime = buf->st_ctime = dw;
1611 buf->st_nlink = hfi.nNumberOfLinks;
1613 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1614 buf->st_mode);
1615 return 0;
1618 /*********************************************************************
1619 * _fstati64 (MSVCRT.@)
1621 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1623 int ret;
1624 struct MSVCRT__stat64 buf64;
1626 ret = MSVCRT__fstat64(fd, &buf64);
1627 if (!ret)
1628 msvcrt_stat64_to_stati64(&buf64, buf);
1629 return ret;
1632 /*********************************************************************
1633 * _fstat (MSVCRT.@)
1635 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1636 { int ret;
1637 struct MSVCRT__stat64 buf64;
1639 ret = MSVCRT__fstat64(fd, &buf64);
1640 if (!ret)
1641 msvcrt_stat64_to_stat(&buf64, buf);
1642 return ret;
1645 /*********************************************************************
1646 * _fstat32 (MSVCR80.@)
1648 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1650 int ret;
1651 struct MSVCRT__stat64 buf64;
1653 ret = MSVCRT__fstat64(fd, &buf64);
1654 if (!ret)
1655 msvcrt_stat64_to_stat32(&buf64, buf);
1656 return ret;
1659 /*********************************************************************
1660 * _fstat64i32 (MSVCR80.@)
1662 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1664 int ret;
1665 struct MSVCRT__stat64 buf64;
1667 ret = MSVCRT__fstat64(fd, &buf64);
1668 if (!ret)
1669 msvcrt_stat64_to_stat64i32(&buf64, buf);
1670 return ret;
1673 /*********************************************************************
1674 * _futime64 (MSVCRT.@)
1676 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1678 HANDLE hand = msvcrt_fdtoh(fd);
1679 FILETIME at, wt;
1681 if (!t)
1683 time_to_filetime( MSVCRT__time64(NULL), &at );
1684 wt = at;
1686 else
1688 time_to_filetime( t->actime, &at );
1689 time_to_filetime( t->modtime, &wt );
1692 if (!SetFileTime(hand, NULL, &at, &wt))
1694 msvcrt_set_errno(GetLastError());
1695 return -1 ;
1697 return 0;
1700 /*********************************************************************
1701 * _futime32 (MSVCRT.@)
1703 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1705 if (t)
1707 struct MSVCRT___utimbuf64 t64;
1708 t64.actime = t->actime;
1709 t64.modtime = t->modtime;
1710 return _futime64( fd, &t64 );
1712 else
1713 return _futime64( fd, NULL );
1716 /*********************************************************************
1717 * _futime (MSVCRT.@)
1719 #ifdef _WIN64
1720 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1722 return _futime64( fd, t );
1724 #else
1725 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1727 return _futime32( fd, t );
1729 #endif
1731 /*********************************************************************
1732 * _get_osfhandle (MSVCRT.@)
1734 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1736 HANDLE hand = msvcrt_fdtoh(fd);
1737 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1739 return (MSVCRT_intptr_t)hand;
1742 /*********************************************************************
1743 * _mktemp_s (MSVCRT.@)
1745 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1747 DWORD len, xno, id;
1749 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1750 return MSVCRT_EINVAL;
1752 for(len=0; len<size; len++)
1753 if(!pattern[len])
1754 break;
1755 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1756 if(size)
1757 pattern[0] = 0;
1758 return MSVCRT_EINVAL;
1761 for(xno=1; xno<=6; xno++)
1762 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1763 return MSVCRT_EINVAL;
1765 id = GetCurrentProcessId();
1766 for(xno=1; xno<6; xno++) {
1767 pattern[len-xno] = id%10 + '0';
1768 id /= 10;
1771 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1772 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1773 return 0;
1776 pattern[0] = 0;
1777 *MSVCRT__errno() = MSVCRT_EEXIST;
1778 return MSVCRT_EEXIST;
1781 /*********************************************************************
1782 * _mktemp (MSVCRT.@)
1784 char * CDECL MSVCRT__mktemp(char *pattern)
1786 int numX = 0;
1787 char *retVal = pattern;
1788 int id;
1789 char letter = 'a';
1791 if(!pattern)
1792 return NULL;
1794 while(*pattern)
1795 numX = (*pattern++ == 'X')? numX + 1 : 0;
1796 if (numX < 6)
1797 return NULL;
1798 pattern--;
1799 id = GetCurrentProcessId();
1800 numX = 6;
1801 while(numX--)
1803 int tempNum = id / 10;
1804 *pattern-- = id - (tempNum * 10) + '0';
1805 id = tempNum;
1807 pattern++;
1810 *pattern = letter++;
1811 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1812 return retVal;
1813 } while(letter <= 'z');
1814 return NULL;
1817 /*********************************************************************
1818 * _wmktemp_s (MSVCRT.@)
1820 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1822 DWORD len, xno, id;
1824 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1825 return MSVCRT_EINVAL;
1827 for(len=0; len<size; len++)
1828 if(!pattern[len])
1829 break;
1830 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1831 if(size)
1832 pattern[0] = 0;
1833 return MSVCRT_EINVAL;
1836 for(xno=1; xno<=6; xno++)
1837 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1838 return MSVCRT_EINVAL;
1840 id = GetCurrentProcessId();
1841 for(xno=1; xno<6; xno++) {
1842 pattern[len-xno] = id%10 + '0';
1843 id /= 10;
1846 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1847 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1848 return 0;
1851 pattern[0] = 0;
1852 *MSVCRT__errno() = MSVCRT_EEXIST;
1853 return MSVCRT_EEXIST;
1856 /*********************************************************************
1857 * _wmktemp (MSVCRT.@)
1859 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1861 int numX = 0;
1862 MSVCRT_wchar_t *retVal = pattern;
1863 int id;
1864 MSVCRT_wchar_t letter = 'a';
1866 if(!pattern)
1867 return NULL;
1869 while(*pattern)
1870 numX = (*pattern++ == 'X')? numX + 1 : 0;
1871 if (numX < 6)
1872 return NULL;
1873 pattern--;
1874 id = GetCurrentProcessId();
1875 numX = 6;
1876 while(numX--)
1878 int tempNum = id / 10;
1879 *pattern-- = id - (tempNum * 10) + '0';
1880 id = tempNum;
1882 pattern++;
1885 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
1886 return retVal;
1887 *pattern = letter++;
1888 } while(letter != '|');
1889 return NULL;
1892 static unsigned split_oflags(unsigned oflags)
1894 int wxflags = 0;
1895 unsigned unsupp; /* until we support everything */
1897 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1898 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1899 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1900 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
1901 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
1902 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
1903 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1904 else wxflags |= WX_TEXT; /* default to TEXT*/
1905 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1907 if ((unsupp = oflags & ~(
1908 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1909 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1910 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1911 MSVCRT__O_NOINHERIT|
1912 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
1913 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
1915 ERR(":unsupported oflags 0x%04x\n",unsupp);
1917 return wxflags;
1920 /*********************************************************************
1921 * _pipe (MSVCRT.@)
1923 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1925 int ret = -1;
1926 SECURITY_ATTRIBUTES sa;
1927 HANDLE readHandle, writeHandle;
1929 if (!pfds)
1931 *MSVCRT__errno() = MSVCRT_EINVAL;
1932 return -1;
1935 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1936 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1937 sa.lpSecurityDescriptor = NULL;
1938 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1940 unsigned int wxflags = split_oflags(textmode);
1941 int fd;
1943 LOCK_FILES();
1944 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
1945 if (fd != -1)
1947 pfds[0] = fd;
1948 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
1949 if (fd != -1)
1951 pfds[1] = fd;
1952 ret = 0;
1954 else
1956 MSVCRT__close(pfds[0]);
1957 CloseHandle(writeHandle);
1958 *MSVCRT__errno() = MSVCRT_EMFILE;
1961 else
1963 CloseHandle(readHandle);
1964 CloseHandle(writeHandle);
1965 *MSVCRT__errno() = MSVCRT_EMFILE;
1967 UNLOCK_FILES();
1969 else
1970 msvcrt_set_errno(GetLastError());
1972 return ret;
1975 static int check_bom(HANDLE h, int oflags, BOOL seek)
1977 char bom[sizeof(utf8_bom)];
1978 DWORD r;
1980 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
1982 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
1983 return oflags;
1985 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
1986 oflags |= MSVCRT__O_U8TEXT;
1987 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
1988 if (seek && r>2)
1989 SetFilePointer(h, 2, NULL, FILE_BEGIN);
1990 oflags |= MSVCRT__O_U16TEXT;
1991 }else if (seek) {
1992 SetFilePointer(h, 0, NULL, FILE_BEGIN);
1995 return oflags;
1998 /*********************************************************************
1999 * _wsopen_s (MSVCRT.@)
2001 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2003 DWORD access = 0, creation = 0, attrib;
2004 SECURITY_ATTRIBUTES sa;
2005 DWORD sharing, type;
2006 int wxflag;
2007 HANDLE hand;
2009 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2010 fd, debugstr_w(path), oflags, shflags, pmode);
2012 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2014 *fd = -1;
2015 wxflag = split_oflags(oflags);
2016 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2018 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2019 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2020 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2023 if (oflags & MSVCRT__O_CREAT)
2025 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
2026 FIXME(": pmode 0x%04x ignored\n", pmode);
2027 else
2028 WARN(": pmode 0x%04x ignored\n", pmode);
2030 if (oflags & MSVCRT__O_EXCL)
2031 creation = CREATE_NEW;
2032 else if (oflags & MSVCRT__O_TRUNC)
2033 creation = CREATE_ALWAYS;
2034 else
2035 creation = OPEN_ALWAYS;
2037 else /* no MSVCRT__O_CREAT */
2039 if (oflags & MSVCRT__O_TRUNC)
2040 creation = TRUNCATE_EXISTING;
2041 else
2042 creation = OPEN_EXISTING;
2045 switch( shflags )
2047 case MSVCRT__SH_DENYRW:
2048 sharing = 0L;
2049 break;
2050 case MSVCRT__SH_DENYWR:
2051 sharing = FILE_SHARE_READ;
2052 break;
2053 case MSVCRT__SH_DENYRD:
2054 sharing = FILE_SHARE_WRITE;
2055 break;
2056 case MSVCRT__SH_DENYNO:
2057 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2058 break;
2059 default:
2060 ERR( "Unhandled shflags 0x%x\n", shflags );
2061 return MSVCRT_EINVAL;
2063 attrib = FILE_ATTRIBUTE_NORMAL;
2065 if (oflags & MSVCRT__O_TEMPORARY)
2067 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2068 access |= DELETE;
2069 sharing |= FILE_SHARE_DELETE;
2072 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2073 sa.lpSecurityDescriptor = NULL;
2074 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2076 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2077 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2078 && !(access&GENERIC_READ))
2080 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2081 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2082 if (hand != INVALID_HANDLE_VALUE)
2084 oflags = check_bom(hand, oflags, FALSE);
2085 CloseHandle(hand);
2087 else
2088 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2091 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2092 if (hand == INVALID_HANDLE_VALUE) {
2093 WARN(":failed-last error (%d)\n",GetLastError());
2094 msvcrt_set_errno(GetLastError());
2095 return *MSVCRT__errno();
2098 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2100 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2101 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2102 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2104 if (oflags & MSVCRT__O_U8TEXT)
2106 DWORD written = 0, tmp;
2108 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2109 sizeof(utf8_bom)-written, &tmp, NULL))
2110 written += tmp;
2111 if (written != sizeof(utf8_bom)) {
2112 WARN("error writing BOM\n");
2113 CloseHandle(hand);
2114 msvcrt_set_errno(GetLastError());
2115 return *MSVCRT__errno();
2118 else
2120 DWORD written = 0, tmp;
2122 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2123 sizeof(utf16_bom)-written, &tmp, NULL))
2124 written += tmp;
2125 if (written != sizeof(utf16_bom))
2127 WARN("error writing BOM\n");
2128 CloseHandle(hand);
2129 msvcrt_set_errno(GetLastError());
2130 return *MSVCRT__errno();
2134 else if (access & GENERIC_READ)
2135 oflags = check_bom(hand, oflags, TRUE);
2138 type = GetFileType(hand);
2139 if (type == FILE_TYPE_CHAR)
2140 wxflag |= WX_TTY;
2141 else if (type == FILE_TYPE_PIPE)
2142 wxflag |= WX_PIPE;
2144 *fd = msvcrt_alloc_fd(hand, wxflag);
2145 if (*fd == -1)
2146 return *MSVCRT__errno();
2148 if (oflags & MSVCRT__O_WTEXT)
2149 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2150 else if (oflags & MSVCRT__O_U16TEXT)
2151 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF16;
2152 else if (oflags & MSVCRT__O_U8TEXT)
2153 msvcrt_get_ioinfo(*fd)->exflag |= EF_UTF8;
2155 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2156 return 0;
2159 /*********************************************************************
2160 * _wsopen (MSVCRT.@)
2162 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2164 int pmode;
2165 int fd;
2167 if (oflags & MSVCRT__O_CREAT)
2169 __ms_va_list ap;
2171 __ms_va_start(ap, shflags);
2172 pmode = va_arg(ap, int);
2173 __ms_va_end(ap);
2175 else
2176 pmode = 0;
2178 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
2179 return fd;
2182 /*********************************************************************
2183 * _sopen_s (MSVCRT.@)
2185 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2187 MSVCRT_wchar_t *pathW;
2188 int ret;
2190 if (!MSVCRT_CHECK_PMT(fd != NULL))
2191 return MSVCRT_EINVAL;
2192 *fd = -1;
2193 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2194 return MSVCRT_EINVAL;
2196 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2197 MSVCRT_free(pathW);
2198 return ret;
2201 /*********************************************************************
2202 * _sopen (MSVCRT.@)
2204 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2206 int pmode;
2207 int fd;
2209 if (oflags & MSVCRT__O_CREAT)
2211 __ms_va_list ap;
2213 __ms_va_start(ap, shflags);
2214 pmode = va_arg(ap, int);
2215 __ms_va_end(ap);
2217 else
2218 pmode = 0;
2220 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2221 return fd;
2224 /*********************************************************************
2225 * _open (MSVCRT.@)
2227 int CDECL MSVCRT__open( const char *path, int flags, ... )
2229 __ms_va_list ap;
2231 if (flags & MSVCRT__O_CREAT)
2233 int pmode;
2234 __ms_va_start(ap, flags);
2235 pmode = va_arg(ap, int);
2236 __ms_va_end(ap);
2237 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2239 else
2240 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2243 /*********************************************************************
2244 * _wopen (MSVCRT.@)
2246 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2248 __ms_va_list ap;
2250 if (flags & MSVCRT__O_CREAT)
2252 int pmode;
2253 __ms_va_start(ap, flags);
2254 pmode = va_arg(ap, int);
2255 __ms_va_end(ap);
2256 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2258 else
2259 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2262 /*********************************************************************
2263 * _creat (MSVCRT.@)
2265 int CDECL MSVCRT__creat(const char *path, int flags)
2267 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2268 return MSVCRT__open(path, usedFlags);
2271 /*********************************************************************
2272 * _wcreat (MSVCRT.@)
2274 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2276 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2277 return MSVCRT__wopen(path, usedFlags);
2280 /*********************************************************************
2281 * _open_osfhandle (MSVCRT.@)
2283 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2285 DWORD flags;
2286 int fd;
2288 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2289 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2290 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2291 * text - it never sets MSVCRT__O_BINARY.
2293 /* don't let split_oflags() decide the mode if no mode is passed */
2294 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2295 oflags |= MSVCRT__O_BINARY;
2297 flags = GetFileType((HANDLE)handle);
2298 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2300 msvcrt_set_errno(GetLastError());
2301 return -1;
2304 if (flags == FILE_TYPE_CHAR)
2305 flags = WX_TTY;
2306 else if (flags == FILE_TYPE_PIPE)
2307 flags = WX_PIPE;
2308 else
2309 flags = 0;
2310 flags |= split_oflags(oflags);
2312 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2313 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2314 return fd;
2317 /*********************************************************************
2318 * _rmtmp (MSVCRT.@)
2320 int CDECL MSVCRT__rmtmp(void)
2322 int num_removed = 0, i;
2323 MSVCRT_FILE *file;
2325 LOCK_FILES();
2326 for (i = 3; i < MSVCRT_stream_idx; i++) {
2327 file = msvcrt_get_file(i);
2329 if (file->_tmpfname)
2331 MSVCRT_fclose(file);
2332 num_removed++;
2335 UNLOCK_FILES();
2337 if (num_removed)
2338 TRACE(":removed (%d) temp files\n",num_removed);
2339 return num_removed;
2342 static inline int get_utf8_char_len(char ch)
2344 if((ch&0xf8) == 0xf0)
2345 return 4;
2346 else if((ch&0xf0) == 0xe0)
2347 return 3;
2348 else if((ch&0xe0) == 0xc0)
2349 return 2;
2350 return 1;
2353 /*********************************************************************
2354 * (internal) read_utf8
2356 static int read_utf8(int fd, MSVCRT_wchar_t *buf, unsigned int count)
2358 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2359 HANDLE hand = fdinfo->handle;
2360 char min_buf[4], *readbuf, lookahead;
2361 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2363 /* make the buffer big enough to hold at least one character */
2364 /* read bytes have to fit to output and lookahead buffers */
2365 count /= 2;
2366 readbuf_size = count < 4 ? 4 : count;
2367 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2368 readbuf_size = 4;
2369 readbuf = min_buf;
2372 if(fdinfo->lookahead[0] != '\n') {
2373 readbuf[pos++] = fdinfo->lookahead[0];
2374 fdinfo->lookahead[0] = '\n';
2376 if(fdinfo->lookahead[1] != '\n') {
2377 readbuf[pos++] = fdinfo->lookahead[1];
2378 fdinfo->lookahead[1] = '\n';
2380 if(fdinfo->lookahead[2] != '\n') {
2381 readbuf[pos++] = fdinfo->lookahead[2];
2382 fdinfo->lookahead[2] = '\n';
2387 /* NOTE: this case is broken in native dll, reading
2388 * sometimes fails when small buffer is passed
2390 if(count < 4) {
2391 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2392 if (GetLastError() == ERROR_BROKEN_PIPE) {
2393 fdinfo->wxflag |= WX_ATEOF;
2394 return 0;
2395 }else {
2396 msvcrt_set_errno(GetLastError());
2397 return -1;
2399 }else if(!num_read) {
2400 fdinfo->wxflag |= WX_ATEOF;
2401 return 0;
2402 }else {
2403 pos++;
2406 char_len = get_utf8_char_len(readbuf[0]);
2407 if(char_len>pos) {
2408 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2409 pos += num_read;
2412 if(readbuf[0] == '\n')
2413 fdinfo->wxflag |= WX_READNL;
2414 else
2415 fdinfo->wxflag &= ~WX_READNL;
2417 if(readbuf[0] == 0x1a) {
2418 fdinfo->wxflag |= WX_ATEOF;
2419 return 0;
2422 if(readbuf[0] == '\r') {
2423 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2424 buf[0] = '\r';
2425 else if(lookahead == '\n')
2426 buf[0] = '\n';
2427 else {
2428 buf[0] = '\r';
2429 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2430 fdinfo->lookahead[0] = lookahead;
2431 else
2432 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2434 return 2;
2437 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2438 msvcrt_set_errno(GetLastError());
2439 return -1;
2442 return num_read*2;
2445 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2446 if(pos) {
2447 num_read = 0;
2448 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2449 fdinfo->wxflag |= WX_ATEOF;
2450 if (readbuf != min_buf) MSVCRT_free(readbuf);
2451 return 0;
2452 }else {
2453 msvcrt_set_errno(GetLastError());
2454 if (readbuf != min_buf) MSVCRT_free(readbuf);
2455 return -1;
2457 }else if(!pos && !num_read) {
2458 fdinfo->wxflag |= WX_ATEOF;
2459 if (readbuf != min_buf) MSVCRT_free(readbuf);
2460 return 0;
2463 pos += num_read;
2464 if(readbuf[0] == '\n')
2465 fdinfo->wxflag |= WX_READNL;
2466 else
2467 fdinfo->wxflag &= ~WX_READNL;
2469 /* Find first byte of last character (may be incomplete) */
2470 for(i=pos-1; i>0 && i>pos-4; i--)
2471 if((readbuf[i]&0xc0) != 0x80)
2472 break;
2473 char_len = get_utf8_char_len(readbuf[i]);
2474 if(char_len+i <= pos)
2475 i += char_len;
2477 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2478 if(i < pos)
2479 fdinfo->lookahead[0] = readbuf[i];
2480 if(i+1 < pos)
2481 fdinfo->lookahead[1] = readbuf[i+1];
2482 if(i+2 < pos)
2483 fdinfo->lookahead[2] = readbuf[i+2];
2484 }else if(i < pos) {
2485 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2487 pos = i;
2489 for(i=0, j=0; i<pos; i++) {
2490 if(readbuf[i] == 0x1a) {
2491 fdinfo->wxflag |= WX_ATEOF;
2492 break;
2495 /* strip '\r' if followed by '\n' */
2496 if(readbuf[i] == '\r' && i+1==pos) {
2497 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2498 readbuf[j++] = '\r';
2499 }else if(lookahead == '\n' && j==0) {
2500 readbuf[j++] = '\n';
2501 }else {
2502 if(lookahead != '\n')
2503 readbuf[j++] = '\r';
2505 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2506 fdinfo->lookahead[0] = lookahead;
2507 else
2508 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2510 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2511 readbuf[j++] = readbuf[i];
2514 pos = j;
2516 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2517 msvcrt_set_errno(GetLastError());
2518 if (readbuf != min_buf) MSVCRT_free(readbuf);
2519 return -1;
2522 if (readbuf != min_buf) MSVCRT_free(readbuf);
2523 return num_read*2;
2526 /*********************************************************************
2527 * (internal) read_i
2529 * When reading \r as last character in text mode, read() positions
2530 * the file pointer on the \r character while getc() goes on to
2531 * the following \n
2533 static int read_i(int fd, void *buf, unsigned int count)
2535 DWORD num_read, utf16;
2536 char *bufstart = buf;
2537 HANDLE hand = msvcrt_fdtoh(fd);
2538 ioinfo *fdinfo = msvcrt_get_ioinfo(fd);
2540 if (count == 0)
2541 return 0;
2543 if (fdinfo->wxflag & WX_ATEOF) {
2544 TRACE("already at EOF, returning 0\n");
2545 return 0;
2547 /* Don't trace small reads, it gets *very* annoying */
2548 if (count > 4)
2549 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
2550 if (hand == INVALID_HANDLE_VALUE)
2552 *MSVCRT__errno() = MSVCRT_EBADF;
2553 return -1;
2556 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2557 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2559 *MSVCRT__errno() = MSVCRT_EINVAL;
2560 return -1;
2563 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2564 return read_utf8(fd, buf, count);
2566 if (fdinfo->lookahead[0]!='\n' || ReadFile(hand, bufstart, count, &num_read, NULL))
2568 if (fdinfo->lookahead[0] != '\n')
2570 bufstart[0] = fdinfo->lookahead[0];
2571 fdinfo->lookahead[0] = '\n';
2573 if (utf16)
2575 bufstart[1] = fdinfo->lookahead[1];
2576 fdinfo->lookahead[1] = '\n';
2579 if(count>1+utf16 && ReadFile(hand, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2580 num_read += 1+utf16;
2581 else
2582 num_read = 1+utf16;
2585 if(utf16 && (num_read&1))
2587 /* msvcr90 uses uninitialized value from the buffer in this case */
2588 /* msvcrt ignores additional data */
2589 ERR("got odd number of bytes in UTF16 mode\n");
2590 num_read--;
2593 if (count != 0 && num_read == 0)
2595 fdinfo->wxflag |= WX_ATEOF;
2596 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2598 else if (fdinfo->wxflag & WX_TEXT)
2600 DWORD i, j;
2602 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2603 fdinfo->wxflag |= WX_READNL;
2604 else
2605 fdinfo->wxflag &= ~WX_READNL;
2607 for (i=0, j=0; i<num_read; i+=1+utf16)
2609 /* in text mode, a ctrl-z signals EOF */
2610 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2612 fdinfo->wxflag |= WX_ATEOF;
2613 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2614 break;
2617 /* in text mode, strip \r if followed by \n */
2618 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2620 char lookahead[2];
2621 DWORD len;
2623 lookahead[1] = '\n';
2624 if (ReadFile(hand, lookahead, 1+utf16, &len, NULL) && len)
2626 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2628 bufstart[j++] = '\n';
2629 if(utf16) bufstart[j++] = 0;
2631 else
2633 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2635 bufstart[j++] = '\r';
2636 if(utf16) bufstart[j++] = 0;
2639 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2641 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2643 bufstart[j++] = '\n';
2644 if (utf16) bufstart[j++] = 0;
2646 else
2648 fdinfo->lookahead[0] = lookahead[0];
2649 fdinfo->lookahead[1] = lookahead[1];
2652 else
2653 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2656 else
2658 bufstart[j++] = '\r';
2659 if(utf16) bufstart[j++] = 0;
2662 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2663 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2665 bufstart[j++] = bufstart[i];
2666 if(utf16) bufstart[j++] = bufstart[i+1];
2669 num_read = j;
2672 else
2674 if (GetLastError() == ERROR_BROKEN_PIPE)
2676 TRACE(":end-of-pipe\n");
2677 fdinfo->wxflag |= WX_ATEOF;
2678 return 0;
2680 else
2682 TRACE(":failed-last error (%d)\n",GetLastError());
2683 return -1;
2687 if (count > 4)
2688 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2689 return num_read;
2692 /*********************************************************************
2693 * _read (MSVCRT.@)
2695 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2697 int num_read;
2698 num_read = read_i(fd, buf, count);
2699 return num_read;
2702 /*********************************************************************
2703 * _setmode (MSVCRT.@)
2705 int CDECL MSVCRT__setmode(int fd,int mode)
2707 int ret = msvcrt_get_ioinfo(fd)->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2708 if(ret==MSVCRT__O_TEXT && (msvcrt_get_ioinfo(fd)->exflag & (EF_UTF8|EF_UTF16)))
2709 ret = MSVCRT__O_WTEXT;
2711 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2712 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2713 *MSVCRT__errno() = MSVCRT_EINVAL;
2714 return -1;
2717 if(mode == MSVCRT__O_BINARY) {
2718 msvcrt_get_ioinfo(fd)->wxflag &= ~WX_TEXT;
2719 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2720 return ret;
2723 msvcrt_get_ioinfo(fd)->wxflag |= WX_TEXT;
2724 if(mode == MSVCRT__O_TEXT)
2725 msvcrt_get_ioinfo(fd)->exflag &= ~(EF_UTF8|EF_UTF16);
2726 else if(mode == MSVCRT__O_U8TEXT)
2727 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF16) | EF_UTF8;
2728 else
2729 msvcrt_get_ioinfo(fd)->exflag = (msvcrt_get_ioinfo(fd)->exflag & ~EF_UTF8) | EF_UTF16;
2731 return ret;
2734 /*********************************************************************
2735 * _stat64 (MSVCRT.@)
2737 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2739 DWORD dw;
2740 WIN32_FILE_ATTRIBUTE_DATA hfi;
2741 unsigned short mode = ALL_S_IREAD;
2742 int plen;
2744 TRACE(":file (%s) buf(%p)\n",path,buf);
2746 plen = strlen(path);
2747 while (plen && path[plen-1]==' ')
2748 plen--;
2750 if (plen && (plen<2 || path[plen-2]!=':') &&
2751 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2753 *MSVCRT__errno() = MSVCRT_ENOENT;
2754 return -1;
2757 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2759 TRACE("failed (%d)\n",GetLastError());
2760 *MSVCRT__errno() = MSVCRT_ENOENT;
2761 return -1;
2764 memset(buf,0,sizeof(struct MSVCRT__stat64));
2766 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2767 Bon 011120: This FIXME seems incorrect
2768 Also a letter as first char isn't enough to be classified
2769 as a drive letter
2771 if (isalpha(*path)&& (*(path+1)==':'))
2772 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2773 else
2774 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2776 /* Dir, or regular file? */
2777 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2778 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2779 else
2781 mode |= MSVCRT__S_IFREG;
2782 /* executable? */
2783 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2785 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2786 (tolower(path[plen-3]) << 16);
2787 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2788 mode |= ALL_S_IEXEC;
2792 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2793 mode |= ALL_S_IWRITE;
2795 buf->st_mode = mode;
2796 buf->st_nlink = 1;
2797 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2798 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2799 buf->st_atime = dw;
2800 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2801 buf->st_mtime = buf->st_ctime = dw;
2802 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2803 (int)(buf->st_size >> 32),(int)buf->st_size,
2804 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2805 return 0;
2808 /*********************************************************************
2809 * _stati64 (MSVCRT.@)
2811 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2813 int ret;
2814 struct MSVCRT__stat64 buf64;
2816 ret = MSVCRT_stat64(path, &buf64);
2817 if (!ret)
2818 msvcrt_stat64_to_stati64(&buf64, buf);
2819 return ret;
2822 /*********************************************************************
2823 * _stat (MSVCRT.@)
2825 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2827 int ret;
2828 struct MSVCRT__stat64 buf64;
2830 ret = MSVCRT_stat64( path, &buf64);
2831 if (!ret)
2832 msvcrt_stat64_to_stat(&buf64, buf);
2833 return ret;
2836 /*********************************************************************
2837 * _stat32 (MSVCR100.@)
2839 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
2841 int ret;
2842 struct MSVCRT__stat64 buf64;
2844 ret = MSVCRT_stat64(path, &buf64);
2845 if (!ret)
2846 msvcrt_stat64_to_stat32(&buf64, buf);
2847 return ret;
2850 /*********************************************************************
2851 * _stat32i64 (MSVCR100.@)
2853 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
2855 int ret;
2856 struct MSVCRT__stat64 buf64;
2858 ret = MSVCRT_stat64(path, &buf64);
2859 if (!ret)
2860 msvcrt_stat64_to_stat32i64(&buf64, buf);
2861 return ret;
2864 /*********************************************************************
2865 * _stat64i32 (MSVCR100.@)
2867 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
2869 int ret;
2870 struct MSVCRT__stat64 buf64;
2872 ret = MSVCRT_stat64(path, &buf64);
2873 if (!ret)
2874 msvcrt_stat64_to_stat64i32(&buf64, buf);
2875 return ret;
2878 /*********************************************************************
2879 * _wstat64 (MSVCRT.@)
2881 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
2883 DWORD dw;
2884 WIN32_FILE_ATTRIBUTE_DATA hfi;
2885 unsigned short mode = ALL_S_IREAD;
2886 int plen;
2888 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
2890 plen = strlenW(path);
2891 while (plen && path[plen-1]==' ')
2892 plen--;
2894 if(plen && (plen<2 || path[plen-2]!=':') &&
2895 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2897 *MSVCRT__errno() = MSVCRT_ENOENT;
2898 return -1;
2901 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
2903 TRACE("failed (%d)\n",GetLastError());
2904 *MSVCRT__errno() = MSVCRT_ENOENT;
2905 return -1;
2908 memset(buf,0,sizeof(struct MSVCRT__stat64));
2910 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2911 if (MSVCRT_iswalpha(*path))
2912 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
2913 else
2914 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2916 /* Dir, or regular file? */
2917 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2918 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2919 else
2921 mode |= MSVCRT__S_IFREG;
2922 /* executable? */
2923 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2925 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
2926 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
2927 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
2928 mode |= ALL_S_IEXEC;
2932 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2933 mode |= ALL_S_IWRITE;
2935 buf->st_mode = mode;
2936 buf->st_nlink = 1;
2937 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2938 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2939 buf->st_atime = dw;
2940 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2941 buf->st_mtime = buf->st_ctime = dw;
2942 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2943 (int)(buf->st_size >> 32),(int)buf->st_size,
2944 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2945 return 0;
2948 /*********************************************************************
2949 * _wstati64 (MSVCRT.@)
2951 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
2953 int ret;
2954 struct MSVCRT__stat64 buf64;
2956 ret = MSVCRT__wstat64(path, &buf64);
2957 if (!ret)
2958 msvcrt_stat64_to_stati64(&buf64, buf);
2959 return ret;
2962 /*********************************************************************
2963 * _wstat (MSVCRT.@)
2965 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
2967 int ret;
2968 struct MSVCRT__stat64 buf64;
2970 ret = MSVCRT__wstat64( path, &buf64 );
2971 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
2972 return ret;
2975 /*********************************************************************
2976 * _wstat32 (MSVCR100.@)
2978 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
2980 int ret;
2981 struct MSVCRT__stat64 buf64;
2983 ret = MSVCRT__wstat64(path, &buf64);
2984 if (!ret)
2985 msvcrt_stat64_to_stat32(&buf64, buf);
2986 return ret;
2989 /*********************************************************************
2990 * _wstat32i64 (MSVCR100.@)
2992 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
2994 int ret;
2995 struct MSVCRT__stat64 buf64;
2997 ret = MSVCRT__wstat64(path, &buf64);
2998 if (!ret)
2999 msvcrt_stat64_to_stat32i64(&buf64, buf);
3000 return ret;
3003 /*********************************************************************
3004 * _wstat64i32 (MSVCR100.@)
3006 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3008 int ret;
3009 struct MSVCRT__stat64 buf64;
3011 ret = MSVCRT__wstat64(path, &buf64);
3012 if (!ret)
3013 msvcrt_stat64_to_stat64i32(&buf64, buf);
3014 return ret;
3017 /*********************************************************************
3018 * _tell (MSVCRT.@)
3020 MSVCRT_long CDECL MSVCRT__tell(int fd)
3022 return MSVCRT__lseek(fd, 0, SEEK_CUR);
3025 /*********************************************************************
3026 * _telli64 (MSVCRT.@)
3028 __int64 CDECL _telli64(int fd)
3030 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
3033 /*********************************************************************
3034 * _tempnam (MSVCRT.@)
3036 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3038 char tmpbuf[MAX_PATH];
3039 const char *tmp_dir = MSVCRT_getenv("TMP");
3041 if (tmp_dir) dir = tmp_dir;
3043 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3044 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3046 TRACE("got name (%s)\n",tmpbuf);
3047 DeleteFileA(tmpbuf);
3048 return MSVCRT__strdup(tmpbuf);
3050 TRACE("failed (%d)\n",GetLastError());
3051 return NULL;
3054 /*********************************************************************
3055 * _wtempnam (MSVCRT.@)
3057 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3059 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3060 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3061 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3063 if (tmp_dir) dir = tmp_dir;
3065 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3066 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3068 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3069 DeleteFileW(tmpbuf);
3070 return MSVCRT__wcsdup(tmpbuf);
3072 TRACE("failed (%d)\n",GetLastError());
3073 return NULL;
3076 /*********************************************************************
3077 * _umask (MSVCRT.@)
3079 int CDECL MSVCRT__umask(int umask)
3081 int old_umask = MSVCRT_umask;
3082 TRACE("(%d)\n",umask);
3083 MSVCRT_umask = umask;
3084 return old_umask;
3087 /*********************************************************************
3088 * _utime64 (MSVCRT.@)
3090 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3092 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3094 if (fd > 0)
3096 int retVal = _futime64(fd, t);
3097 MSVCRT__close(fd);
3098 return retVal;
3100 return -1;
3103 /*********************************************************************
3104 * _utime32 (MSVCRT.@)
3106 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3108 if (t)
3110 struct MSVCRT___utimbuf64 t64;
3111 t64.actime = t->actime;
3112 t64.modtime = t->modtime;
3113 return _utime64( path, &t64 );
3115 else
3116 return _utime64( path, NULL );
3119 /*********************************************************************
3120 * _utime (MSVCRT.@)
3122 #ifdef _WIN64
3123 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3125 return _utime64( path, t );
3127 #else
3128 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3130 return _utime32( path, t );
3132 #endif
3134 /*********************************************************************
3135 * _wutime64 (MSVCRT.@)
3137 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3139 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3141 if (fd > 0)
3143 int retVal = _futime64(fd, t);
3144 MSVCRT__close(fd);
3145 return retVal;
3147 return -1;
3150 /*********************************************************************
3151 * _wutime32 (MSVCRT.@)
3153 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3155 if (t)
3157 struct MSVCRT___utimbuf64 t64;
3158 t64.actime = t->actime;
3159 t64.modtime = t->modtime;
3160 return _wutime64( path, &t64 );
3162 else
3163 return _wutime64( path, NULL );
3166 /*********************************************************************
3167 * _wutime (MSVCRT.@)
3169 #ifdef _WIN64
3170 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3172 return _wutime64( path, t );
3174 #else
3175 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3177 return _wutime32( path, t );
3179 #endif
3181 /*********************************************************************
3182 * _write (MSVCRT.@)
3184 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3186 DWORD num_written;
3187 ioinfo *info = msvcrt_get_ioinfo(fd);
3188 HANDLE hand = info->handle;
3190 /* Don't trace small writes, it gets *very* annoying */
3191 #if 0
3192 if (count > 32)
3193 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3194 #endif
3195 if (hand == INVALID_HANDLE_VALUE)
3197 *MSVCRT__errno() = MSVCRT_EBADF;
3198 return -1;
3201 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3203 *MSVCRT__errno() = MSVCRT_EINVAL;
3204 return -1;
3207 /* If appending, go to EOF */
3208 if (info->wxflag & WX_APPEND)
3209 MSVCRT__lseek(fd, 0, FILE_END);
3211 if (!(info->wxflag & WX_TEXT))
3213 if (WriteFile(hand, buf, count, &num_written, NULL)
3214 && (num_written == count))
3215 return num_written;
3216 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3217 hand, GetLastError());
3218 *MSVCRT__errno() = MSVCRT_ENOSPC;
3220 else
3222 unsigned int i, j, nr_lf, size;
3223 char *p = NULL;
3224 const char *q;
3225 const char *s = buf, *buf_start = buf;
3227 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3229 /* find number of \n */
3230 for (nr_lf=0, i=0; i<count; i++)
3231 if (s[i] == '\n')
3232 nr_lf++;
3233 if (nr_lf)
3235 size = count+nr_lf;
3236 if ((q = p = MSVCRT_malloc(size)))
3238 for (s = buf, i = 0, j = 0; i < count; i++)
3240 if (s[i] == '\n')
3241 p[j++] = '\r';
3242 p[j++] = s[i];
3245 else
3247 FIXME("Malloc failed\n");
3248 nr_lf = 0;
3249 size = count;
3250 q = buf;
3253 else
3255 size = count;
3256 q = buf;
3259 else if (info->exflag & EF_UTF16)
3261 for (nr_lf=0, i=0; i<count; i+=2)
3262 if (s[i]=='\n' && s[i+1]==0)
3263 nr_lf += 2;
3264 if (nr_lf)
3266 size = count+nr_lf;
3267 if ((q = p = MSVCRT_malloc(size)))
3269 for (s=buf, i=0, j=0; i<count; i++)
3271 if (s[i]=='\n' && s[i+1]==0)
3273 p[j++] = '\r';
3274 p[j++] = 0;
3276 p[j++] = s[i++];
3277 p[j++] = s[i];
3280 else
3282 FIXME("Malloc failed\n");
3283 nr_lf = 0;
3284 size = count;
3285 q = buf;
3288 else
3290 size = count;
3291 q = buf;
3294 else
3296 DWORD conv_len;
3298 for(nr_lf=0, i=0; i<count; i+=2)
3299 if (s[i]=='\n' && s[i+1]==0)
3300 nr_lf++;
3302 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3303 if(!conv_len) {
3304 msvcrt_set_errno(GetLastError());
3305 MSVCRT_free(p);
3306 return -1;
3309 size = conv_len+nr_lf;
3310 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3312 for (s=buf, i=0, j=0; i<count; i++)
3314 if (s[i]=='\n' && s[i+1]==0)
3316 p[j++] = '\r';
3317 p[j++] = 0;
3319 p[j++] = s[i++];
3320 p[j++] = s[i];
3322 q = p+count+nr_lf*2;
3323 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3324 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3326 else
3328 FIXME("Malloc failed\n");
3329 nr_lf = 0;
3330 size = count;
3331 q = buf;
3335 if (!WriteFile(hand, q, size, &num_written, NULL))
3336 num_written = -1;
3337 if(p)
3338 MSVCRT_free(p);
3339 if (num_written != size)
3341 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3342 fd, hand, GetLastError(), num_written);
3343 *MSVCRT__errno() = MSVCRT_ENOSPC;
3344 return s - buf_start;
3346 return count;
3349 return -1;
3352 /*********************************************************************
3353 * _putw (MSVCRT.@)
3355 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3357 int len;
3359 MSVCRT__lock_file(file);
3360 len = MSVCRT__write(file->_file, &val, sizeof(val));
3361 if (len == sizeof(val)) {
3362 MSVCRT__unlock_file(file);
3363 return val;
3366 file->_flag |= MSVCRT__IOERR;
3367 MSVCRT__unlock_file(file);
3368 return MSVCRT_EOF;
3371 /*********************************************************************
3372 * fclose (MSVCRT.@)
3374 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3376 int r, flag;
3378 MSVCRT__lock_file(file);
3379 flag = file->_flag;
3380 MSVCRT_free(file->_tmpfname);
3381 file->_tmpfname = NULL;
3382 /* flush stdio buffers */
3383 if(file->_flag & MSVCRT__IOWRT)
3384 MSVCRT_fflush(file);
3385 if(file->_flag & MSVCRT__IOMYBUF)
3386 MSVCRT_free(file->_base);
3388 r=MSVCRT__close(file->_file);
3390 file->_flag = 0;
3391 MSVCRT__unlock_file(file);
3393 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3396 /*********************************************************************
3397 * feof (MSVCRT.@)
3399 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3401 return file->_flag & MSVCRT__IOEOF;
3404 /*********************************************************************
3405 * ferror (MSVCRT.@)
3407 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3409 return file->_flag & MSVCRT__IOERR;
3412 /*********************************************************************
3413 * _filbuf (MSVCRT.@)
3415 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3417 unsigned char c;
3418 MSVCRT__lock_file(file);
3420 if(file->_flag & MSVCRT__IOSTRG) {
3421 MSVCRT__unlock_file(file);
3422 return MSVCRT_EOF;
3425 /* Allocate buffer if needed */
3426 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3427 msvcrt_alloc_buffer(file);
3429 if(!(file->_flag & MSVCRT__IOREAD)) {
3430 if(file->_flag & MSVCRT__IORW)
3431 file->_flag |= MSVCRT__IOREAD;
3432 else {
3433 MSVCRT__unlock_file(file);
3434 return MSVCRT_EOF;
3438 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3439 int r;
3440 if ((r = read_i(file->_file,&c,1)) != 1) {
3441 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3442 MSVCRT__unlock_file(file);
3443 return MSVCRT_EOF;
3446 MSVCRT__unlock_file(file);
3447 return c;
3448 } else {
3449 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
3450 if(file->_cnt<=0) {
3451 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3452 file->_cnt = 0;
3453 MSVCRT__unlock_file(file);
3454 return MSVCRT_EOF;
3457 file->_cnt--;
3458 file->_ptr = file->_base+1;
3459 c = *(unsigned char *)file->_base;
3460 MSVCRT__unlock_file(file);
3461 return c;
3465 /*********************************************************************
3466 * fgetc (MSVCRT.@)
3468 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3470 unsigned char *i;
3471 unsigned int j;
3473 MSVCRT__lock_file(file);
3474 if (file->_cnt>0) {
3475 file->_cnt--;
3476 i = (unsigned char *)file->_ptr++;
3477 j = *i;
3478 } else
3479 j = MSVCRT__filbuf(file);
3481 MSVCRT__unlock_file(file);
3482 return j;
3485 /*********************************************************************
3486 * _fgetchar (MSVCRT.@)
3488 int CDECL MSVCRT__fgetchar(void)
3490 return MSVCRT_fgetc(MSVCRT_stdin);
3493 /*********************************************************************
3494 * fgets (MSVCRT.@)
3496 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3498 int cc = MSVCRT_EOF;
3499 char * buf_start = s;
3501 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3502 file,file->_file,s,size);
3504 MSVCRT__lock_file(file);
3506 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
3508 *s++ = (char)cc;
3509 size --;
3511 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3513 TRACE(":nothing read\n");
3514 MSVCRT__unlock_file(file);
3515 return NULL;
3517 if ((cc != MSVCRT_EOF) && (size > 1))
3518 *s++ = cc;
3519 *s = '\0';
3520 TRACE(":got %s\n", debugstr_a(buf_start));
3521 MSVCRT__unlock_file(file);
3522 return buf_start;
3525 /*********************************************************************
3526 * fgetwc (MSVCRT.@)
3528 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3530 MSVCRT_wint_t ret;
3531 int ch;
3533 MSVCRT__lock_file(file);
3535 if((msvcrt_get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3536 || !(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
3537 char *p;
3539 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3540 ch = MSVCRT_fgetc(file);
3541 if(ch == MSVCRT_EOF) {
3542 ret = MSVCRT_WEOF;
3543 break;
3545 *p = (char)ch;
3547 }else {
3548 char mbs[MSVCRT_MB_LEN_MAX];
3549 int len = 0;
3551 ch = MSVCRT_fgetc(file);
3552 if(ch != MSVCRT_EOF) {
3553 mbs[0] = (char)ch;
3554 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3555 ch = MSVCRT_fgetc(file);
3556 if(ch != MSVCRT_EOF) {
3557 mbs[1] = (char)ch;
3558 len = 2;
3560 }else {
3561 len = 1;
3565 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3566 ret = MSVCRT_WEOF;
3569 MSVCRT__unlock_file(file);
3570 return ret;
3573 /*********************************************************************
3574 * _getw (MSVCRT.@)
3576 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3578 char *ch;
3579 int i, k;
3580 unsigned int j;
3581 ch = (char *)&i;
3583 MSVCRT__lock_file(file);
3584 for (j=0; j<sizeof(int); j++) {
3585 k = MSVCRT_fgetc(file);
3586 if (k == MSVCRT_EOF) {
3587 file->_flag |= MSVCRT__IOEOF;
3588 MSVCRT__unlock_file(file);
3589 return EOF;
3591 ch[j] = k;
3594 MSVCRT__unlock_file(file);
3595 return i;
3598 /*********************************************************************
3599 * getwc (MSVCRT.@)
3601 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3603 return MSVCRT_fgetwc(file);
3606 /*********************************************************************
3607 * _fgetwchar (MSVCRT.@)
3609 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3611 return MSVCRT_fgetwc(MSVCRT_stdin);
3614 /*********************************************************************
3615 * getwchar (MSVCRT.@)
3617 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3619 return MSVCRT__fgetwchar();
3622 /*********************************************************************
3623 * fgetws (MSVCRT.@)
3625 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3627 MSVCRT_wint_t cc = MSVCRT_WEOF;
3628 MSVCRT_wchar_t * buf_start = s;
3630 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3631 file,file->_file,s,size);
3633 MSVCRT__lock_file(file);
3635 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
3637 *s++ = cc;
3638 size --;
3640 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3642 TRACE(":nothing read\n");
3643 MSVCRT__unlock_file(file);
3644 return NULL;
3646 if ((cc != MSVCRT_WEOF) && (size > 1))
3647 *s++ = cc;
3648 *s = 0;
3649 TRACE(":got %s\n", debugstr_w(buf_start));
3650 MSVCRT__unlock_file(file);
3651 return buf_start;
3654 /*********************************************************************
3655 * _flsbuf (MSVCRT.@)
3657 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3659 /* Flush output buffer */
3660 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3661 msvcrt_alloc_buffer(file);
3663 if(!(file->_flag & MSVCRT__IOWRT)) {
3664 if(file->_flag & MSVCRT__IORW)
3665 file->_flag |= MSVCRT__IOWRT;
3666 else
3667 return MSVCRT_EOF;
3669 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3670 int res = 0;
3672 if(file->_cnt <= 0) {
3673 res = msvcrt_flush_buffer(file);
3674 if(res)
3675 return res;
3676 file->_cnt=file->_bufsiz;
3678 *file->_ptr++ = c;
3679 file->_cnt--;
3680 return c&0xff;
3681 } else {
3682 unsigned char cc=c;
3683 int len;
3684 /* set _cnt to 0 for unbuffered FILEs */
3685 file->_cnt = 0;
3686 len = MSVCRT__write(file->_file, &cc, 1);
3687 if (len == 1)
3688 return c & 0xff;
3689 file->_flag |= MSVCRT__IOERR;
3690 return MSVCRT_EOF;
3694 /*********************************************************************
3695 * fwrite (MSVCRT.@)
3697 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3699 MSVCRT_size_t wrcnt=size * nmemb;
3700 int written = 0;
3701 if (size == 0)
3702 return 0;
3704 MSVCRT__lock_file(file);
3706 while(wrcnt) {
3707 if(file->_cnt < 0) {
3708 WARN("negative file->_cnt value in %p\n", file);
3709 file->_flag |= MSVCRT__IOERR;
3710 break;
3711 } else if(file->_cnt) {
3712 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3713 memcpy(file->_ptr, ptr, pcnt);
3714 file->_cnt -= pcnt;
3715 file->_ptr += pcnt;
3716 written += pcnt;
3717 wrcnt -= pcnt;
3718 ptr = (const char*)ptr + pcnt;
3719 } else if((file->_flag & MSVCRT__IONBF)
3720 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
3721 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
3722 MSVCRT_size_t pcnt;
3723 int bufsiz;
3725 if(file->_flag & MSVCRT__IONBF)
3726 bufsiz = 1;
3727 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3728 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
3729 else
3730 bufsiz = file->_bufsiz;
3732 pcnt = (wrcnt / bufsiz) * bufsiz;
3734 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
3735 break;
3737 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
3738 file->_flag |= MSVCRT__IOERR;
3739 break;
3741 written += pcnt;
3742 wrcnt -= pcnt;
3743 ptr = (const char*)ptr + pcnt;
3744 } else {
3745 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3746 break;
3747 written++;
3748 wrcnt--;
3749 ptr = (const char*)ptr + 1;
3753 MSVCRT__unlock_file(file);
3754 return written / size;
3757 /*********************************************************************
3758 * fputwc (MSVCRT.@)
3760 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3762 MSVCRT_wchar_t mwc=wc;
3763 ioinfo *fdinfo;
3764 MSVCRT_wint_t ret;
3766 MSVCRT__lock_file(file);
3767 fdinfo = msvcrt_get_ioinfo(file->_file);
3769 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3770 char buf[MSVCRT_MB_LEN_MAX];
3771 int char_len;
3773 char_len = MSVCRT_wctomb(buf, mwc);
3774 if(char_len!=-1 && MSVCRT_fwrite(buf, char_len, 1, file)==1)
3775 ret = wc;
3776 else
3777 ret = MSVCRT_WEOF;
3778 }else if(MSVCRT_fwrite(&mwc, sizeof(mwc), 1, file) == 1) {
3779 ret = wc;
3780 }else {
3781 ret = MSVCRT_WEOF;
3784 MSVCRT__unlock_file(file);
3785 return ret;
3788 /*********************************************************************
3789 * _fputwchar (MSVCRT.@)
3791 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3793 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3796 /*********************************************************************
3797 * _wfsopen (MSVCRT.@)
3799 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
3801 MSVCRT_FILE* file;
3802 int open_flags, stream_flags, fd;
3804 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3806 /* map mode string to open() flags. "man fopen" for possibilities. */
3807 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3808 return NULL;
3810 LOCK_FILES();
3811 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
3812 if (fd < 0)
3813 file = NULL;
3814 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3815 != -1)
3816 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3817 else if (file)
3819 file->_flag = 0;
3820 file = NULL;
3823 TRACE(":got (%p)\n",file);
3824 if (fd >= 0 && !file)
3825 MSVCRT__close(fd);
3826 UNLOCK_FILES();
3827 return file;
3830 /*********************************************************************
3831 * _fsopen (MSVCRT.@)
3833 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
3835 MSVCRT_FILE *ret;
3836 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
3838 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3839 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3840 *MSVCRT__errno() = MSVCRT_EINVAL;
3841 return NULL;
3843 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3845 MSVCRT_free(pathW);
3846 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
3847 *MSVCRT__errno() = MSVCRT_EINVAL;
3848 return NULL;
3851 ret = MSVCRT__wfsopen(pathW, modeW, share);
3853 MSVCRT_free(pathW);
3854 MSVCRT_free(modeW);
3855 return ret;
3858 /*********************************************************************
3859 * fopen (MSVCRT.@)
3861 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
3863 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
3866 /*********************************************************************
3867 * fopen_s (MSVCRT.@)
3869 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
3870 const char *filename, const char *mode)
3872 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3873 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3874 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3876 *pFile = MSVCRT_fopen(filename, mode);
3878 if(!*pFile)
3879 return *MSVCRT__errno();
3880 return 0;
3883 /*********************************************************************
3884 * _wfopen (MSVCRT.@)
3886 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
3888 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
3891 /*********************************************************************
3892 * _wfopen_s (MSVCRT.@)
3894 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
3895 const MSVCRT_wchar_t *mode)
3897 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
3898 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
3899 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
3901 *pFile = MSVCRT__wfopen(filename, mode);
3903 if(!*pFile)
3904 return *MSVCRT__errno();
3905 return 0;
3908 /*********************************************************************
3909 * fputc (MSVCRT.@)
3911 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
3913 int res;
3915 MSVCRT__lock_file(file);
3916 if(file->_cnt>0) {
3917 *file->_ptr++=c;
3918 file->_cnt--;
3919 if (c == '\n')
3921 res = msvcrt_flush_buffer(file);
3922 MSVCRT__unlock_file(file);
3923 return res ? res : c;
3925 else {
3926 MSVCRT__unlock_file(file);
3927 return c & 0xff;
3929 } else {
3930 res = MSVCRT__flsbuf(c, file);
3931 MSVCRT__unlock_file(file);
3932 return res;
3936 /*********************************************************************
3937 * _fputchar (MSVCRT.@)
3939 int CDECL MSVCRT__fputchar(int c)
3941 return MSVCRT_fputc(c, MSVCRT_stdout);
3944 /*********************************************************************
3945 * fread (MSVCRT.@)
3947 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3949 MSVCRT_size_t rcnt=size * nmemb;
3950 MSVCRT_size_t read=0;
3951 MSVCRT_size_t pread=0;
3953 if(!rcnt)
3954 return 0;
3956 MSVCRT__lock_file(file);
3958 /* first buffered data */
3959 if(file->_cnt>0) {
3960 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3961 memcpy(ptr, file->_ptr, pcnt);
3962 file->_cnt -= pcnt;
3963 file->_ptr += pcnt;
3964 read += pcnt ;
3965 rcnt -= pcnt ;
3966 ptr = (char*)ptr + pcnt;
3967 } else if(!(file->_flag & MSVCRT__IOREAD )) {
3968 if(file->_flag & MSVCRT__IORW) {
3969 file->_flag |= MSVCRT__IOREAD;
3970 } else {
3971 MSVCRT__unlock_file(file);
3972 return 0;
3976 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3977 msvcrt_alloc_buffer(file);
3979 while(rcnt>0)
3981 int i;
3982 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3983 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3984 file->_ptr = file->_base;
3985 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3986 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3987 if (i > 0 && i < file->_cnt) {
3988 msvcrt_get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
3989 file->_flag &= ~MSVCRT__IOEOF;
3991 if (i > 0) {
3992 memcpy(ptr, file->_ptr, i);
3993 file->_cnt -= i;
3994 file->_ptr += i;
3996 } else if (rcnt > INT_MAX) {
3997 i = MSVCRT__read(file->_file, ptr, INT_MAX);
3998 } else if (rcnt < MSVCRT_BUFSIZ) {
3999 i = MSVCRT__read(file->_file, ptr, rcnt);
4000 } else {
4001 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
4003 pread += i;
4004 rcnt -= i;
4005 ptr = (char *)ptr+i;
4006 /* expose feof condition in the flags
4007 * MFC tests file->_flag for feof, and doesn't call feof())
4009 if (msvcrt_get_ioinfo(file->_file)->wxflag & WX_ATEOF)
4010 file->_flag |= MSVCRT__IOEOF;
4011 else if (i == -1)
4013 file->_flag |= MSVCRT__IOERR;
4014 pread = 0;
4015 rcnt = 0;
4017 if (i < 1) break;
4019 read+=pread;
4020 MSVCRT__unlock_file(file);
4021 return read / size;
4025 /*********************************************************************
4026 * fread_s (MSVCR80.@)
4028 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4029 MSVCRT_size_t count, MSVCRT_FILE *stream)
4031 size_t bytes_left, buf_pos;
4033 TRACE("(%p %lu %lu %lu %p\n", buf, buf_size, elem_size, count, stream);
4035 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4036 if(buf && buf_size)
4037 memset(buf, 0, buf_size);
4038 return 0;
4040 if(!elem_size || !count) return 0;
4041 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4042 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4044 bytes_left = elem_size*count;
4045 buf_pos = 0;
4046 while(bytes_left) {
4047 if(stream->_cnt > 0) {
4048 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4050 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4051 memset(buf, 0, buf_size);
4052 return 0;
4055 MSVCRT_fread((char*)buf+buf_pos, 1, size, stream);
4056 buf_pos += size;
4057 bytes_left -= size;
4058 }else {
4059 int c = MSVCRT__filbuf(stream);
4061 if(c == EOF)
4062 break;
4064 if(!MSVCRT_CHECK_PMT_ERR(buf_size-buf_pos > 0, MSVCRT_ERANGE)) {
4065 memset(buf, 0, buf_size);
4066 return 0;
4069 ((char*)buf)[buf_pos++] = c;
4070 bytes_left--;
4074 return buf_pos/elem_size;
4077 /*********************************************************************
4078 * _wfreopen (MSVCRT.@)
4081 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4083 int open_flags, stream_flags, fd;
4085 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4087 LOCK_FILES();
4088 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
4089 file = NULL;
4090 else
4092 MSVCRT_fclose(file);
4093 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4094 file = NULL;
4095 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4096 file = NULL;
4097 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4099 file->_flag = 0;
4100 file = NULL;
4103 UNLOCK_FILES();
4104 return file;
4107 /*********************************************************************
4108 * _wfreopen_s (MSVCRT.@)
4110 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4111 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4113 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4114 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4115 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4116 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4118 *pFile = MSVCRT__wfreopen(path, mode, file);
4120 if(!*pFile)
4121 return *MSVCRT__errno();
4122 return 0;
4125 /*********************************************************************
4126 * freopen (MSVCRT.@)
4129 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4131 MSVCRT_FILE *ret;
4132 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4134 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4135 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4137 MSVCRT_free(pathW);
4138 return NULL;
4141 ret = MSVCRT__wfreopen(pathW, modeW, file);
4143 MSVCRT_free(pathW);
4144 MSVCRT_free(modeW);
4145 return ret;
4148 /*********************************************************************
4149 * freopen_s (MSVCRT.@)
4151 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4152 const char *path, const char *mode, MSVCRT_FILE* file)
4154 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4155 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4156 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4157 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4159 *pFile = MSVCRT_freopen(path, mode, file);
4161 if(!*pFile)
4162 return *MSVCRT__errno();
4163 return 0;
4166 /*********************************************************************
4167 * fsetpos (MSVCRT.@)
4169 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4171 int ret;
4173 MSVCRT__lock_file(file);
4174 /* Note that all this has been lifted 'as is' from fseek */
4175 if(file->_flag & MSVCRT__IOWRT)
4176 msvcrt_flush_buffer(file);
4178 /* Discard buffered input */
4179 file->_cnt = 0;
4180 file->_ptr = file->_base;
4182 /* Reset direction of i/o */
4183 if(file->_flag & MSVCRT__IORW) {
4184 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4187 ret = (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
4188 MSVCRT__unlock_file(file);
4189 return ret;
4192 /*********************************************************************
4193 * _ftelli64 (MSVCRT.@)
4195 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4197 __int64 pos;
4199 MSVCRT__lock_file(file);
4200 pos = _telli64(file->_file);
4201 if(pos == -1) {
4202 MSVCRT__unlock_file(file);
4203 return -1;
4205 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4206 if(file->_flag & MSVCRT__IOWRT) {
4207 pos += file->_ptr - file->_base;
4209 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4210 char *p;
4212 for(p=file->_base; p<file->_ptr; p++)
4213 if(*p == '\n')
4214 pos++;
4216 } else if(!file->_cnt) { /* nothing to do */
4217 } else if(MSVCRT__lseeki64(file->_file, 0, SEEK_END)==pos) {
4218 int i;
4220 pos -= file->_cnt;
4221 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4222 for(i=0; i<file->_cnt; i++)
4223 if(file->_ptr[i] == '\n')
4224 pos--;
4226 } else {
4227 char *p;
4229 if(MSVCRT__lseeki64(file->_file, pos, SEEK_SET) != pos) {
4230 MSVCRT__unlock_file(file);
4231 return -1;
4234 pos -= file->_bufsiz;
4235 pos += file->_ptr - file->_base;
4237 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT) {
4238 if(msvcrt_get_ioinfo(file->_file)->wxflag & WX_READNL)
4239 pos--;
4241 for(p=file->_base; p<file->_ptr; p++)
4242 if(*p == '\n')
4243 pos++;
4248 MSVCRT__unlock_file(file);
4249 return pos;
4252 /*********************************************************************
4253 * ftell (MSVCRT.@)
4255 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4257 return MSVCRT__ftelli64(file);
4260 /*********************************************************************
4261 * fgetpos (MSVCRT.@)
4263 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4265 *pos = MSVCRT__ftelli64(file);
4266 if(*pos == -1)
4267 return -1;
4268 return 0;
4271 /*********************************************************************
4272 * fputs (MSVCRT.@)
4274 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4276 MSVCRT_size_t len = strlen(s);
4277 int ret;
4279 MSVCRT__lock_file(file);
4280 ret = MSVCRT_fwrite(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4281 MSVCRT__unlock_file(file);
4282 return ret;
4285 /*********************************************************************
4286 * fputws (MSVCRT.@)
4288 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4290 MSVCRT_size_t i, len = strlenW(s);
4291 BOOL tmp_buf;
4292 int ret;
4294 MSVCRT__lock_file(file);
4295 if (!(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
4296 ret = MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4297 MSVCRT__unlock_file(file);
4298 return ret;
4301 tmp_buf = add_std_buffer(file);
4302 for (i=0; i<len; i++) {
4303 if(MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) {
4304 if(tmp_buf) remove_std_buffer(file);
4305 MSVCRT__unlock_file(file);
4306 return MSVCRT_WEOF;
4310 if(tmp_buf) remove_std_buffer(file);
4311 MSVCRT__unlock_file(file);
4312 return 0;
4315 /*********************************************************************
4316 * getchar (MSVCRT.@)
4318 int CDECL MSVCRT_getchar(void)
4320 return MSVCRT_fgetc(MSVCRT_stdin);
4323 /*********************************************************************
4324 * getc (MSVCRT.@)
4326 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4328 return MSVCRT_fgetc(file);
4331 /*********************************************************************
4332 * gets (MSVCRT.@)
4334 char * CDECL MSVCRT_gets(char *buf)
4336 int cc;
4337 char * buf_start = buf;
4339 MSVCRT__lock_file(MSVCRT_stdin);
4340 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4341 cc = MSVCRT_fgetc(MSVCRT_stdin))
4342 if(cc != '\r') *buf++ = (char)cc;
4344 *buf = '\0';
4346 TRACE("got '%s'\n", buf_start);
4347 MSVCRT__unlock_file(MSVCRT_stdin);
4348 return buf_start;
4351 /*********************************************************************
4352 * _getws (MSVCRT.@)
4354 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4356 MSVCRT_wint_t cc;
4357 MSVCRT_wchar_t* ws = buf;
4359 MSVCRT__lock_file(MSVCRT_stdin);
4360 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4361 cc = MSVCRT_fgetwc(MSVCRT_stdin))
4363 if (cc != '\r')
4364 *buf++ = (MSVCRT_wchar_t)cc;
4366 *buf = '\0';
4368 TRACE("got %s\n", debugstr_w(ws));
4369 MSVCRT__unlock_file(MSVCRT_stdin);
4370 return ws;
4373 /*********************************************************************
4374 * putc (MSVCRT.@)
4376 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4378 return MSVCRT_fputc(c, file);
4381 /*********************************************************************
4382 * putchar (MSVCRT.@)
4384 int CDECL MSVCRT_putchar(int c)
4386 return MSVCRT_fputc(c, MSVCRT_stdout);
4389 /*********************************************************************
4390 * _putwch (MSVCRT.@)
4392 int CDECL MSVCRT__putwch(int c)
4394 return MSVCRT_fputwc(c, MSVCRT_stdout);
4397 /*********************************************************************
4398 * puts (MSVCRT.@)
4400 int CDECL MSVCRT_puts(const char *s)
4402 MSVCRT_size_t len = strlen(s);
4403 int ret;
4405 MSVCRT__lock_file(MSVCRT_stdout);
4406 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4407 MSVCRT__unlock_file(MSVCRT_stdout);
4408 return MSVCRT_EOF;
4411 ret = MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4412 MSVCRT__unlock_file(MSVCRT_stdout);
4413 return ret;
4416 /*********************************************************************
4417 * _putws (MSVCRT.@)
4419 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4421 static const MSVCRT_wchar_t nl = '\n';
4422 MSVCRT_size_t len = strlenW(s);
4423 int ret;
4425 MSVCRT__lock_file(MSVCRT_stdout);
4426 if(MSVCRT_fwrite(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4427 MSVCRT__unlock_file(MSVCRT_stdout);
4428 return MSVCRT_EOF;
4431 ret = MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4432 MSVCRT__unlock_file(MSVCRT_stdout);
4433 return ret;
4436 /*********************************************************************
4437 * remove (MSVCRT.@)
4439 int CDECL MSVCRT_remove(const char *path)
4441 TRACE("(%s)\n",path);
4442 if (DeleteFileA(path))
4443 return 0;
4444 TRACE(":failed (%d)\n",GetLastError());
4445 msvcrt_set_errno(GetLastError());
4446 return -1;
4449 /*********************************************************************
4450 * _wremove (MSVCRT.@)
4452 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4454 TRACE("(%s)\n",debugstr_w(path));
4455 if (DeleteFileW(path))
4456 return 0;
4457 TRACE(":failed (%d)\n",GetLastError());
4458 msvcrt_set_errno(GetLastError());
4459 return -1;
4462 /*********************************************************************
4463 * rename (MSVCRT.@)
4465 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4467 TRACE(":from %s to %s\n",oldpath,newpath);
4468 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4469 return 0;
4470 TRACE(":failed (%d)\n",GetLastError());
4471 msvcrt_set_errno(GetLastError());
4472 return -1;
4475 /*********************************************************************
4476 * _wrename (MSVCRT.@)
4478 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4480 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4481 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4482 return 0;
4483 TRACE(":failed (%d)\n",GetLastError());
4484 msvcrt_set_errno(GetLastError());
4485 return -1;
4488 /*********************************************************************
4489 * setvbuf (MSVCRT.@)
4491 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4493 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4494 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4495 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4497 MSVCRT__lock_file(file);
4499 MSVCRT_fflush(file);
4500 if(file->_flag & MSVCRT__IOMYBUF)
4501 MSVCRT_free(file->_base);
4502 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4503 file->_cnt = 0;
4505 if(mode == MSVCRT__IONBF) {
4506 file->_flag |= MSVCRT__IONBF;
4507 file->_base = file->_ptr = (char*)&file->_charbuf;
4508 file->_bufsiz = 2;
4509 }else if(buf) {
4510 file->_base = file->_ptr = buf;
4511 file->_flag |= MSVCRT__USERBUF;
4512 file->_bufsiz = size;
4513 }else {
4514 file->_base = file->_ptr = MSVCRT_malloc(size);
4515 if(!file->_base) {
4516 file->_bufsiz = 0;
4517 MSVCRT__unlock_file(file);
4518 return -1;
4521 file->_flag |= MSVCRT__IOMYBUF;
4522 file->_bufsiz = size;
4524 MSVCRT__unlock_file(file);
4525 return 0;
4528 /*********************************************************************
4529 * setbuf (MSVCRT.@)
4531 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4533 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4536 /*********************************************************************
4537 * tmpnam (MSVCRT.@)
4539 char * CDECL MSVCRT_tmpnam(char *s)
4541 char tmpstr[16];
4542 char *p;
4543 int count, size;
4545 if (!s) {
4546 thread_data_t *data = msvcrt_get_thread_data();
4548 if(!data->tmpnam_buffer)
4549 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4551 s = data->tmpnam_buffer;
4554 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4555 p = s + sprintf(s, "\\s%s.", tmpstr);
4556 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4558 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
4559 memcpy(p, tmpstr, size);
4560 p[size] = '\0';
4561 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4562 GetLastError() == ERROR_FILE_NOT_FOUND)
4563 break;
4565 return s;
4568 /*********************************************************************
4569 * _wtmpnam (MSVCRT.@)
4571 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
4573 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
4574 MSVCRT_wchar_t tmpstr[16];
4575 MSVCRT_wchar_t *p;
4576 int count, size;
4577 if (!s) {
4578 thread_data_t *data = msvcrt_get_thread_data();
4580 if(!data->wtmpnam_buffer)
4581 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4583 s = data->wtmpnam_buffer;
4586 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4587 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
4588 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4590 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
4591 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
4592 p[size] = '\0';
4593 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4594 GetLastError() == ERROR_FILE_NOT_FOUND)
4595 break;
4597 return s;
4600 /*********************************************************************
4601 * tmpfile (MSVCRT.@)
4603 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4605 char *filename = MSVCRT_tmpnam(NULL);
4606 int fd;
4607 MSVCRT_FILE* file = NULL;
4609 LOCK_FILES();
4610 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4611 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4612 if (fd != -1 && (file = msvcrt_alloc_fp()))
4614 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4616 file->_flag = 0;
4617 file = NULL;
4619 else file->_tmpfname = MSVCRT__strdup(filename);
4622 if(fd != -1 && !file)
4623 MSVCRT__close(fd);
4624 UNLOCK_FILES();
4625 return file;
4628 /*********************************************************************
4629 * tmpfile_s (MSVCRT.@)
4631 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4633 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4635 *file = MSVCRT_tmpfile();
4636 return 0;
4639 static int puts_clbk_file_a(void *file, int len, const char *str)
4641 return MSVCRT_fwrite(str, sizeof(char), len, file);
4644 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4646 int i, ret;
4648 MSVCRT__lock_file(file);
4650 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4651 ret = MSVCRT_fwrite(str, sizeof(MSVCRT_wchar_t), len, file);
4652 MSVCRT__unlock_file(file);
4653 return ret;
4656 for(i=0; i<len; i++) {
4657 if(MSVCRT_fputwc(str[i], file) == MSVCRT_WEOF) {
4658 MSVCRT__unlock_file(file);
4659 return -1;
4663 MSVCRT__unlock_file(file);
4664 return len;
4667 /*********************************************************************
4668 * vfprintf (MSVCRT.@)
4670 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4672 BOOL tmp_buf;
4673 int ret;
4675 MSVCRT__lock_file(file);
4676 tmp_buf = add_std_buffer(file);
4677 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4678 if(tmp_buf) remove_std_buffer(file);
4679 MSVCRT__unlock_file(file);
4681 return ret;
4684 /*********************************************************************
4685 * vfprintf_s (MSVCRT.@)
4687 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4689 BOOL tmp_buf;
4690 int ret;
4692 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4694 MSVCRT__lock_file(file);
4695 tmp_buf = add_std_buffer(file);
4696 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4697 if(tmp_buf) remove_std_buffer(file);
4698 MSVCRT__unlock_file(file);
4700 return ret;
4703 /*********************************************************************
4704 * vfwprintf (MSVCRT.@)
4706 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4708 BOOL tmp_buf;
4709 int ret;
4711 MSVCRT__lock_file(file);
4712 tmp_buf = add_std_buffer(file);
4713 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4714 if(tmp_buf) remove_std_buffer(file);
4715 MSVCRT__unlock_file(file);
4717 return ret;
4720 /*********************************************************************
4721 * vfwprintf_s (MSVCRT.@)
4723 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4725 BOOL tmp_buf;
4726 int ret;
4728 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4730 MSVCRT__lock_file(file);
4731 tmp_buf = add_std_buffer(file);
4732 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, FALSE, TRUE, arg_clbk_valist, NULL, &valist);
4733 if(tmp_buf) remove_std_buffer(file);
4734 MSVCRT__unlock_file(file);
4736 return ret;
4739 /*********************************************************************
4740 * _vfwprintf_l (MSVCRT.@)
4742 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
4743 MSVCRT__locale_t locale, __ms_va_list valist)
4745 BOOL tmp_buf;
4746 int ret;
4748 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4750 MSVCRT__lock_file(file);
4751 tmp_buf = add_std_buffer(file);
4752 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, FALSE, FALSE, arg_clbk_valist, NULL, &valist);
4753 if(tmp_buf) remove_std_buffer(file);
4754 MSVCRT__unlock_file(file);
4756 return ret;
4759 /*********************************************************************
4760 * vprintf (MSVCRT.@)
4762 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
4764 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
4767 /*********************************************************************
4768 * vprintf_s (MSVCRT.@)
4770 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
4772 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
4775 /*********************************************************************
4776 * vwprintf (MSVCRT.@)
4778 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
4780 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
4783 /*********************************************************************
4784 * vwprintf_s (MSVCRT.@)
4786 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
4788 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
4791 /*********************************************************************
4792 * fprintf (MSVCRT.@)
4794 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
4796 __ms_va_list valist;
4797 int res;
4798 __ms_va_start(valist, format);
4799 res = MSVCRT_vfprintf(file, format, valist);
4800 __ms_va_end(valist);
4801 return res;
4804 /*********************************************************************
4805 * fprintf_s (MSVCRT.@)
4807 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
4809 __ms_va_list valist;
4810 int res;
4811 __ms_va_start(valist, format);
4812 res = MSVCRT_vfprintf_s(file, format, valist);
4813 __ms_va_end(valist);
4814 return res;
4817 /*********************************************************************
4818 * fwprintf (MSVCRT.@)
4820 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4822 __ms_va_list valist;
4823 int res;
4824 __ms_va_start(valist, format);
4825 res = MSVCRT_vfwprintf(file, format, valist);
4826 __ms_va_end(valist);
4827 return res;
4830 /*********************************************************************
4831 * fwprintf_s (MSVCRT.@)
4833 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
4835 __ms_va_list valist;
4836 int res;
4837 __ms_va_start(valist, format);
4838 res = MSVCRT_vfwprintf_s(file, format, valist);
4839 __ms_va_end(valist);
4840 return res;
4843 /*********************************************************************
4844 * _fwprintf_l (MSVCRT.@)
4846 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
4848 __ms_va_list valist;
4849 int res;
4850 __ms_va_start(valist, locale);
4851 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
4852 __ms_va_end(valist);
4853 return res;
4856 /*********************************************************************
4857 * printf (MSVCRT.@)
4859 int CDECL MSVCRT_printf(const char *format, ...)
4861 __ms_va_list valist;
4862 int res;
4863 __ms_va_start(valist, format);
4864 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
4865 __ms_va_end(valist);
4866 return res;
4869 /*********************************************************************
4870 * printf_s (MSVCRT.@)
4872 int CDECL MSVCRT_printf_s(const char *format, ...)
4874 __ms_va_list valist;
4875 int res;
4876 __ms_va_start(valist, format);
4877 res = MSVCRT_vprintf_s(format, valist);
4878 __ms_va_end(valist);
4879 return res;
4882 /*********************************************************************
4883 * ungetc (MSVCRT.@)
4885 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
4887 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
4889 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
4890 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
4891 return MSVCRT_EOF;
4893 MSVCRT__lock_file(file);
4894 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
4895 && msvcrt_alloc_buffer(file))
4896 || (!file->_cnt && file->_ptr==file->_base))
4897 file->_ptr++;
4899 if(file->_ptr>file->_base) {
4900 file->_ptr--;
4901 if(file->_flag & MSVCRT__IOSTRG) {
4902 if(*file->_ptr != c) {
4903 file->_ptr++;
4904 MSVCRT__unlock_file(file);
4905 return MSVCRT_EOF;
4907 }else {
4908 *file->_ptr = c;
4910 file->_cnt++;
4911 MSVCRT_clearerr(file);
4912 file->_flag |= MSVCRT__IOREAD;
4913 MSVCRT__unlock_file(file);
4914 return c;
4917 MSVCRT__unlock_file(file);
4918 return MSVCRT_EOF;
4921 /*********************************************************************
4922 * ungetwc (MSVCRT.@)
4924 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
4926 MSVCRT_wchar_t mwc = wc;
4928 if (wc == MSVCRT_WEOF)
4929 return MSVCRT_WEOF;
4931 MSVCRT__lock_file(file);
4933 if((msvcrt_get_ioinfo(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
4934 || !(msvcrt_get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
4935 unsigned char * pp = (unsigned char *)&mwc;
4936 int i;
4938 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
4939 if(pp[i] != MSVCRT_ungetc(pp[i],file)) {
4940 MSVCRT__unlock_file(file);
4941 return MSVCRT_WEOF;
4944 }else {
4945 char mbs[MSVCRT_MB_LEN_MAX];
4946 int len;
4948 len = MSVCRT_wctomb(mbs, mwc);
4949 if(len == -1) {
4950 MSVCRT__unlock_file(file);
4951 return MSVCRT_WEOF;
4954 for(len--; len>=0; len--) {
4955 if(mbs[len] != MSVCRT_ungetc(mbs[len], file)) {
4956 MSVCRT__unlock_file(file);
4957 return MSVCRT_WEOF;
4962 MSVCRT__unlock_file(file);
4963 return mwc;
4966 /*********************************************************************
4967 * wprintf (MSVCRT.@)
4969 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
4971 __ms_va_list valist;
4972 int res;
4973 __ms_va_start(valist, format);
4974 res = MSVCRT_vwprintf(format, valist);
4975 __ms_va_end(valist);
4976 return res;
4979 /*********************************************************************
4980 * wprintf_s (MSVCRT.@)
4982 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
4984 __ms_va_list valist;
4985 int res;
4986 __ms_va_start(valist, format);
4987 res = MSVCRT_vwprintf_s(format, valist);
4988 __ms_va_end(valist);
4989 return res;
4992 /*********************************************************************
4993 * _getmaxstdio (MSVCRT.@)
4995 int CDECL MSVCRT__getmaxstdio(void)
4997 return MSVCRT_max_streams;
5000 /*********************************************************************
5001 * _setmaxstdio (MSVCRT.@)
5003 int CDECL MSVCRT__setmaxstdio(int newmax)
5005 TRACE("%d\n", newmax);
5007 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5008 return -1;
5010 MSVCRT_max_streams = newmax;
5011 return MSVCRT_max_streams;