wine.inf: Add HKLM\System\CurrentControlSet\Control\ServiceGroupOrder\List registry...
[wine/multimedia.git] / dlls / msvcrt / file.c
blob22bb9253c4cdaa6a72d72a200064abaa3905f7c7
1 /*
2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * TODO
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdarg.h>
33 #include <stdio.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <sys/types.h>
38 #include <limits.h>
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winternl.h"
43 #include "msvcrt.h"
44 #include "mtdll.h"
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
62 #define WX_OPEN 0x01
63 #define WX_ATEOF 0x02
64 #define WX_READNL 0x04 /* read started with \n */
65 #define WX_PIPE 0x08
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
68 #define WX_TTY 0x40
69 #define WX_TEXT 0x80
71 /* values for exflag - it's used differently in msvcr90.dll*/
72 #define EF_UTF8 0x01
73 #define EF_UTF16 0x02
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
87 typedef struct {
88 HANDLE handle;
89 unsigned char wxflag;
90 char lookahead[3];
91 int exflag;
92 CRITICAL_SECTION crit;
93 #if _MSVCR_VER >= 80
94 char textmode : 7;
95 char unicode : 1;
96 char pipech2[2];
97 __int64 startpos;
98 BOOL utf8translations;
99 #endif
100 #if _MSVCR_VER >= 90
101 char dbcsBuffer;
102 BOOL dbcsBufferUsed;
103 #endif
104 } ioinfo;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
117 typedef struct {
118 MSVCRT_FILE file;
119 CRITICAL_SECTION crit;
120 } file_crit;
122 MSVCRT_FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
123 static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
124 static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
126 /* INTERNAL: process umask */
127 static int MSVCRT_umask = 0;
129 /* INTERNAL: static data for tmpnam and _wtmpname functions */
130 static int tmpnam_unique;
132 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
133 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
134 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
135 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
137 #define TOUL(x) (ULONGLONG)(x)
138 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
139 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
140 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
141 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
143 /* This critical section protects the MSVCRT_fstreams table
144 * and MSVCRT_stream_idx from race conditions. It also
145 * protects fd critical sections creation code.
147 static CRITICAL_SECTION MSVCRT_file_cs;
148 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
150 0, 0, &MSVCRT_file_cs,
151 { &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
152 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
154 static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
155 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
156 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
158 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
160 buf->st_dev = buf64->st_dev;
161 buf->st_ino = buf64->st_ino;
162 buf->st_mode = buf64->st_mode;
163 buf->st_nlink = buf64->st_nlink;
164 buf->st_uid = buf64->st_uid;
165 buf->st_gid = buf64->st_gid;
166 buf->st_rdev = buf64->st_rdev;
167 buf->st_size = buf64->st_size;
168 buf->st_atime = buf64->st_atime;
169 buf->st_mtime = buf64->st_mtime;
170 buf->st_ctime = buf64->st_ctime;
173 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
175 buf->st_dev = buf64->st_dev;
176 buf->st_ino = buf64->st_ino;
177 buf->st_mode = buf64->st_mode;
178 buf->st_nlink = buf64->st_nlink;
179 buf->st_uid = buf64->st_uid;
180 buf->st_gid = buf64->st_gid;
181 buf->st_rdev = buf64->st_rdev;
182 buf->st_size = buf64->st_size;
183 buf->st_atime = buf64->st_atime;
184 buf->st_mtime = buf64->st_mtime;
185 buf->st_ctime = buf64->st_ctime;
188 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32 *buf)
190 buf->st_dev = buf64->st_dev;
191 buf->st_ino = buf64->st_ino;
192 buf->st_mode = buf64->st_mode;
193 buf->st_nlink = buf64->st_nlink;
194 buf->st_uid = buf64->st_uid;
195 buf->st_gid = buf64->st_gid;
196 buf->st_rdev = buf64->st_rdev;
197 buf->st_size = buf64->st_size;
198 buf->st_atime = buf64->st_atime;
199 buf->st_mtime = buf64->st_mtime;
200 buf->st_ctime = buf64->st_ctime;
203 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat64i32 *buf)
205 buf->st_dev = buf64->st_dev;
206 buf->st_ino = buf64->st_ino;
207 buf->st_mode = buf64->st_mode;
208 buf->st_nlink = buf64->st_nlink;
209 buf->st_uid = buf64->st_uid;
210 buf->st_gid = buf64->st_gid;
211 buf->st_rdev = buf64->st_rdev;
212 buf->st_size = buf64->st_size;
213 buf->st_atime = buf64->st_atime;
214 buf->st_mtime = buf64->st_mtime;
215 buf->st_ctime = buf64->st_ctime;
218 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat32i64 *buf)
220 buf->st_dev = buf64->st_dev;
221 buf->st_ino = buf64->st_ino;
222 buf->st_mode = buf64->st_mode;
223 buf->st_nlink = buf64->st_nlink;
224 buf->st_uid = buf64->st_uid;
225 buf->st_gid = buf64->st_gid;
226 buf->st_rdev = buf64->st_rdev;
227 buf->st_size = buf64->st_size;
228 buf->st_atime = buf64->st_atime;
229 buf->st_mtime = buf64->st_mtime;
230 buf->st_ctime = buf64->st_ctime;
233 static void time_to_filetime( MSVCRT___time64_t time, FILETIME *ft )
235 /* 1601 to 1970 is 369 years plus 89 leap days */
236 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
238 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
239 ft->dwHighDateTime = ticks >> 32;
240 ft->dwLowDateTime = ticks;
243 static inline ioinfo* get_ioinfo_nolock(int fd)
245 ioinfo *ret = NULL;
246 if(fd>=0 && fd<MSVCRT_MAX_FILES)
247 ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
248 if(!ret)
249 return &MSVCRT___badioinfo;
251 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
254 static inline void init_ioinfo_cs(ioinfo *info)
256 if(!(info->exflag & EF_CRIT_INIT)) {
257 LOCK_FILES();
258 if(!(info->exflag & EF_CRIT_INIT)) {
259 InitializeCriticalSection(&info->crit);
260 info->exflag |= EF_CRIT_INIT;
262 UNLOCK_FILES();
266 static inline ioinfo* get_ioinfo(int fd)
268 ioinfo *ret = get_ioinfo_nolock(fd);
269 if(ret == &MSVCRT___badioinfo)
270 return ret;
271 init_ioinfo_cs(ret);
272 EnterCriticalSection(&ret->crit);
273 return ret;
276 static inline BOOL alloc_pioinfo_block(int fd)
278 ioinfo *block;
279 int i;
281 if(fd<0 || fd>=MSVCRT_MAX_FILES)
283 *MSVCRT__errno() = MSVCRT_ENFILE;
284 return FALSE;
287 block = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
288 if(!block)
290 WARN(":out of memory!\n");
291 *MSVCRT__errno() = MSVCRT_ENOMEM;
292 return FALSE;
294 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
295 block[i].handle = INVALID_HANDLE_VALUE;
296 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE], block, NULL))
297 MSVCRT_free(block);
298 return TRUE;
301 static inline ioinfo* get_ioinfo_alloc_fd(int fd)
303 ioinfo *ret;
305 ret = get_ioinfo(fd);
306 if(ret != &MSVCRT___badioinfo)
307 return ret;
309 if(!alloc_pioinfo_block(fd))
310 return &MSVCRT___badioinfo;
312 return get_ioinfo(fd);
315 static inline ioinfo* get_ioinfo_alloc(int *fd)
317 int i;
319 *fd = -1;
320 for(i=0; i<MSVCRT_MAX_FILES; i++)
322 ioinfo *info = get_ioinfo_nolock(i);
324 if(info == &MSVCRT___badioinfo)
326 if(!alloc_pioinfo_block(i))
327 return &MSVCRT___badioinfo;
328 info = get_ioinfo_nolock(i);
331 init_ioinfo_cs(info);
332 if(TryEnterCriticalSection(&info->crit))
334 if(info->handle == INVALID_HANDLE_VALUE)
336 *fd = i;
337 return info;
339 LeaveCriticalSection(&info->crit);
343 WARN(":files exhausted!\n");
344 *MSVCRT__errno() = MSVCRT_ENFILE;
345 return &MSVCRT___badioinfo;
348 static inline void release_ioinfo(ioinfo *info)
350 if(info!=&MSVCRT___badioinfo && info->exflag & EF_CRIT_INIT)
351 LeaveCriticalSection(&info->crit);
354 static inline MSVCRT_FILE* msvcrt_get_file(int i)
356 file_crit *ret;
358 if(i >= MSVCRT_max_streams)
359 return NULL;
361 if(i < _IOB_ENTRIES)
362 return &MSVCRT__iob[i];
364 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
365 if(!ret) {
366 MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
367 if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
368 ERR("out of memory\n");
369 *MSVCRT__errno() = MSVCRT_ENOMEM;
370 return NULL;
373 ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
374 } else
375 ret += i%MSVCRT_FD_BLOCK_SIZE;
377 return &ret->file;
380 /* INTERNAL: free a file entry fd */
381 static void msvcrt_free_fd(int fd)
383 ioinfo *fdinfo = get_ioinfo(fd);
385 if(fdinfo != &MSVCRT___badioinfo)
387 fdinfo->handle = INVALID_HANDLE_VALUE;
388 fdinfo->wxflag = 0;
390 TRACE(":fd (%d) freed\n",fd);
392 if (fd < 3)
394 switch (fd)
396 case 0:
397 SetStdHandle(STD_INPUT_HANDLE, 0);
398 break;
399 case 1:
400 SetStdHandle(STD_OUTPUT_HANDLE, 0);
401 break;
402 case 2:
403 SetStdHandle(STD_ERROR_HANDLE, 0);
404 break;
407 release_ioinfo(fdinfo);
410 static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag)
412 fdinfo->handle = hand;
413 fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_TTY));
414 fdinfo->lookahead[0] = '\n';
415 fdinfo->lookahead[1] = '\n';
416 fdinfo->lookahead[2] = '\n';
417 fdinfo->exflag &= EF_CRIT_INIT;
419 switch (fdinfo-MSVCRT___pioinfo[0])
421 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
422 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
423 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
427 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
428 static int msvcrt_alloc_fd(HANDLE hand, int flag)
430 int fd;
431 ioinfo *info = get_ioinfo_alloc(&fd);
433 TRACE(":handle (%p) allocating fd (%d)\n", hand, fd);
435 if(info == &MSVCRT___badioinfo)
436 return -1;
438 msvcrt_set_fd(info, hand, flag);
439 release_ioinfo(info);
440 return fd;
443 /* INTERNAL: Allocate a FILE* for an fd slot */
444 /* caller must hold the files lock */
445 static MSVCRT_FILE* msvcrt_alloc_fp(void)
447 int i;
448 MSVCRT_FILE *file;
450 for (i = 3; i < MSVCRT_max_streams; i++)
452 file = msvcrt_get_file(i);
453 if (!file)
454 return NULL;
456 if (file->_flag == 0)
458 if (i == MSVCRT_stream_idx)
460 if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
462 InitializeCriticalSection(&((file_crit*)file)->crit);
463 ((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
465 MSVCRT_stream_idx++;
467 return file;
471 return NULL;
474 /* INTERNAL: initialize a FILE* from an open fd */
475 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
477 TRACE(":fd (%d) allocating FILE*\n",fd);
478 if (!(get_ioinfo_nolock(fd)->wxflag & WX_OPEN))
480 WARN(":invalid fd %d\n",fd);
481 *MSVCRT___doserrno() = 0;
482 *MSVCRT__errno() = MSVCRT_EBADF;
483 return -1;
485 file->_ptr = file->_base = NULL;
486 file->_cnt = 0;
487 file->_file = fd;
488 file->_flag = stream_flags;
489 file->_tmpfname = NULL;
491 TRACE(":got FILE* (%p)\n",file);
492 return 0;
495 /* INTERNAL: Create an inheritance data block (for spawned process)
496 * The inheritance block is made of:
497 * 00 int nb of file descriptor (NBFD)
498 * 04 char file flags (wxflag): repeated for each fd
499 * 4+NBFD HANDLE file handle: repeated for each fd
501 unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
503 int fd, last_fd;
504 char* wxflag_ptr;
505 HANDLE* handle_ptr;
506 ioinfo* fdinfo;
508 for (last_fd=MSVCRT_MAX_FILES-1; last_fd>=0; last_fd--)
509 if (get_ioinfo_nolock(last_fd)->handle != INVALID_HANDLE_VALUE)
510 break;
511 last_fd++;
513 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * last_fd;
514 *block = MSVCRT_calloc(*size, 1);
515 if (!*block)
517 *size = 0;
518 return FALSE;
520 wxflag_ptr = (char*)*block + sizeof(unsigned);
521 handle_ptr = (HANDLE*)(wxflag_ptr + last_fd);
523 *(unsigned*)*block = last_fd;
524 for (fd = 0; fd < last_fd; fd++)
526 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
527 fdinfo = get_ioinfo(fd);
528 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
530 *wxflag_ptr = fdinfo->wxflag;
531 *handle_ptr = fdinfo->handle;
533 else
535 *wxflag_ptr = 0;
536 *handle_ptr = INVALID_HANDLE_VALUE;
538 release_ioinfo(fdinfo);
539 wxflag_ptr++; handle_ptr++;
541 return TRUE;
544 /* INTERNAL: Set up all file descriptors,
545 * as well as default streams (stdin, stderr and stdout)
547 void msvcrt_init_io(void)
549 STARTUPINFOA si;
550 int i;
551 ioinfo *fdinfo;
553 GetStartupInfoA(&si);
554 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
556 BYTE* wxflag_ptr;
557 HANDLE* handle_ptr;
558 unsigned int count;
560 count = *(unsigned*)si.lpReserved2;
561 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
562 handle_ptr = (HANDLE*)(wxflag_ptr + count);
564 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
565 count = min(count, MSVCRT_MAX_FILES);
566 for (i = 0; i < count; i++)
568 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
570 fdinfo = get_ioinfo_alloc_fd(i);
571 if(fdinfo != &MSVCRT___badioinfo)
572 msvcrt_set_fd(fdinfo, *handle_ptr, *wxflag_ptr);
573 release_ioinfo(fdinfo);
576 wxflag_ptr++; handle_ptr++;
580 fdinfo = get_ioinfo_alloc_fd(MSVCRT_STDIN_FILENO);
581 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
582 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
583 DWORD type = GetFileType(h);
585 msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
586 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0));
588 release_ioinfo(fdinfo);
590 fdinfo = get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO);
591 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
592 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
593 DWORD type = GetFileType(h);
595 msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
596 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0));
598 release_ioinfo(fdinfo);
600 fdinfo = get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO);
601 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
602 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
603 DWORD type = GetFileType(h);
605 msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
606 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0));
608 release_ioinfo(fdinfo);
610 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO)->handle,
611 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO)->handle,
612 get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle);
614 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
615 for (i = 0; i < 3; i++)
617 /* FILE structs for stdin/out/err are static and never deleted */
618 MSVCRT__iob[i]._file = i;
619 MSVCRT__iob[i]._tmpfname = NULL;
620 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
622 MSVCRT_stream_idx = 3;
625 /* INTERNAL: Flush stdio file buffer */
626 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
628 if((file->_flag & (MSVCRT__IOREAD|MSVCRT__IOWRT)) == MSVCRT__IOWRT &&
629 file->_flag & (MSVCRT__IOMYBUF|MSVCRT__USERBUF)) {
630 int cnt=file->_ptr-file->_base;
631 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
632 file->_flag |= MSVCRT__IOERR;
633 return MSVCRT_EOF;
636 if(file->_flag & MSVCRT__IORW)
637 file->_flag &= ~MSVCRT__IOWRT;
640 file->_ptr=file->_base;
641 file->_cnt=0;
642 return 0;
645 /*********************************************************************
646 * _isatty (MSVCRT.@)
648 int CDECL MSVCRT__isatty(int fd)
650 TRACE(":fd (%d)\n",fd);
652 return get_ioinfo_nolock(fd)->wxflag & WX_TTY;
655 /* INTERNAL: Allocate stdio file buffer */
656 static BOOL msvcrt_alloc_buffer(MSVCRT_FILE* file)
658 if((file->_file==MSVCRT_STDOUT_FILENO || file->_file==MSVCRT_STDERR_FILENO)
659 && MSVCRT__isatty(file->_file))
660 return FALSE;
662 file->_base = MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ,1);
663 if(file->_base) {
664 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
665 file->_flag |= MSVCRT__IOMYBUF;
666 } else {
667 file->_base = (char*)(&file->_charbuf);
668 file->_bufsiz = 2;
669 file->_flag |= MSVCRT__IONBF;
671 file->_ptr = file->_base;
672 file->_cnt = 0;
673 return TRUE;
676 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
677 static BOOL add_std_buffer(MSVCRT_FILE *file)
679 static char buffers[2][MSVCRT_BUFSIZ];
681 if((file->_file!=MSVCRT_STDOUT_FILENO && file->_file!=MSVCRT_STDERR_FILENO)
682 || (file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
683 || !MSVCRT__isatty(file->_file))
684 return FALSE;
686 file->_ptr = file->_base = buffers[file->_file == MSVCRT_STDOUT_FILENO ? 0 : 1];
687 file->_bufsiz = file->_cnt = MSVCRT_BUFSIZ;
688 file->_flag |= MSVCRT__USERBUF;
689 return TRUE;
692 /* INTERNAL: Removes temporary buffer from stdout or stderr */
693 /* Only call this function when add_std_buffer returned TRUE */
694 static void remove_std_buffer(MSVCRT_FILE *file)
696 msvcrt_flush_buffer(file);
697 file->_ptr = file->_base = NULL;
698 file->_bufsiz = file->_cnt = 0;
699 file->_flag &= ~MSVCRT__USERBUF;
702 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
703 static int msvcrt_int_to_base32(int num, char *str)
705 char *p;
706 int n = num;
707 int digits = 0;
709 while (n != 0)
711 n >>= 5;
712 digits++;
714 p = str + digits;
715 *p = 0;
716 while (--p >= str)
718 *p = (num & 31) + '0';
719 if (*p > '9')
720 *p += ('a' - '0' - 10);
721 num >>= 5;
724 return digits;
727 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
728 static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str)
730 MSVCRT_wchar_t *p;
731 int n = num;
732 int digits = 0;
734 while (n != 0)
736 n >>= 5;
737 digits++;
739 p = str + digits;
740 *p = 0;
741 while (--p >= str)
743 *p = (num & 31) + '0';
744 if (*p > '9')
745 *p += ('a' - '0' - 10);
746 num >>= 5;
749 return digits;
752 /*********************************************************************
753 * __iob_func(MSVCRT.@)
755 MSVCRT_FILE * CDECL MSVCRT___iob_func(void)
757 return &MSVCRT__iob[0];
760 /*********************************************************************
761 * __acrt_iob_func(MSVCRT.@)
763 MSVCRT_FILE * CDECL MSVCRT___acrt_iob_func(unsigned idx)
765 return &MSVCRT__iob[idx];
768 /*********************************************************************
769 * _access (MSVCRT.@)
771 int CDECL MSVCRT__access(const char *filename, int mode)
773 DWORD attr = GetFileAttributesA(filename);
775 TRACE("(%s,%d) %d\n",filename,mode,attr);
777 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
779 msvcrt_set_errno(GetLastError());
780 return -1;
782 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
784 msvcrt_set_errno(ERROR_ACCESS_DENIED);
785 return -1;
787 return 0;
790 /*********************************************************************
791 * _access_s (MSVCRT.@)
793 int CDECL MSVCRT__access_s(const char *filename, int mode)
795 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
796 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
798 if (MSVCRT__access(filename, mode) == -1)
799 return *MSVCRT__errno();
800 return 0;
803 /*********************************************************************
804 * _waccess (MSVCRT.@)
806 int CDECL MSVCRT__waccess(const MSVCRT_wchar_t *filename, int mode)
808 DWORD attr = GetFileAttributesW(filename);
810 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
812 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
814 msvcrt_set_errno(GetLastError());
815 return -1;
817 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
819 msvcrt_set_errno(ERROR_ACCESS_DENIED);
820 return -1;
822 return 0;
825 /*********************************************************************
826 * _waccess_s (MSVCRT.@)
828 int CDECL MSVCRT__waccess_s(const MSVCRT_wchar_t *filename, int mode)
830 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *MSVCRT__errno();
831 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *MSVCRT__errno();
833 if (MSVCRT__waccess(filename, mode) == -1)
834 return *MSVCRT__errno();
835 return 0;
838 /*********************************************************************
839 * _chmod (MSVCRT.@)
841 int CDECL MSVCRT__chmod(const char *path, int flags)
843 DWORD oldFlags = GetFileAttributesA(path);
845 if (oldFlags != INVALID_FILE_ATTRIBUTES)
847 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
848 oldFlags | FILE_ATTRIBUTE_READONLY;
850 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
851 return 0;
853 msvcrt_set_errno(GetLastError());
854 return -1;
857 /*********************************************************************
858 * _wchmod (MSVCRT.@)
860 int CDECL MSVCRT__wchmod(const MSVCRT_wchar_t *path, int flags)
862 DWORD oldFlags = GetFileAttributesW(path);
864 if (oldFlags != INVALID_FILE_ATTRIBUTES)
866 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
867 oldFlags | FILE_ATTRIBUTE_READONLY;
869 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
870 return 0;
872 msvcrt_set_errno(GetLastError());
873 return -1;
876 /*********************************************************************
877 * _unlink (MSVCRT.@)
879 int CDECL MSVCRT__unlink(const char *path)
881 TRACE("%s\n",debugstr_a(path));
882 if(DeleteFileA(path))
883 return 0;
884 TRACE("failed (%d)\n",GetLastError());
885 msvcrt_set_errno(GetLastError());
886 return -1;
889 /*********************************************************************
890 * _wunlink (MSVCRT.@)
892 int CDECL MSVCRT__wunlink(const MSVCRT_wchar_t *path)
894 TRACE("(%s)\n",debugstr_w(path));
895 if(DeleteFileW(path))
896 return 0;
897 TRACE("failed (%d)\n",GetLastError());
898 msvcrt_set_errno(GetLastError());
899 return -1;
902 /*********************************************************************
903 * _commit (MSVCRT.@)
905 int CDECL MSVCRT__commit(int fd)
907 ioinfo *info = get_ioinfo(fd);
908 int ret;
910 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
912 if (info->handle == INVALID_HANDLE_VALUE)
913 ret = -1;
914 else if (!FlushFileBuffers(info->handle))
916 if (GetLastError() == ERROR_INVALID_HANDLE)
918 /* FlushFileBuffers fails for console handles
919 * so we ignore this error.
921 ret = 0;
923 else
925 TRACE(":failed-last error (%d)\n",GetLastError());
926 msvcrt_set_errno(GetLastError());
927 ret = -1;
930 else
932 TRACE(":ok\n");
933 ret = 0;
936 release_ioinfo(info);
937 return ret;
940 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
941 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
943 /* INTERNAL: Flush all stream buffer */
944 static int msvcrt_flush_all_buffers(int mask)
946 int i, num_flushed = 0;
947 MSVCRT_FILE *file;
949 LOCK_FILES();
950 for (i = 0; i < MSVCRT_stream_idx; i++) {
951 file = msvcrt_get_file(i);
953 if (file->_flag)
955 if(file->_flag & mask) {
956 MSVCRT_fflush(file);
957 num_flushed++;
961 UNLOCK_FILES();
963 TRACE(":flushed (%d) handles\n",num_flushed);
964 return num_flushed;
967 /*********************************************************************
968 * _flushall (MSVCRT.@)
970 int CDECL MSVCRT__flushall(void)
972 return msvcrt_flush_all_buffers(MSVCRT__IOWRT | MSVCRT__IOREAD);
975 /*********************************************************************
976 * fflush (MSVCRT.@)
978 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
980 int ret;
982 if(!file) {
983 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
984 ret = 0;
985 } else {
986 MSVCRT__lock_file(file);
987 ret = MSVCRT__fflush_nolock(file);
988 MSVCRT__unlock_file(file);
991 return ret;
994 /*********************************************************************
995 * _fflush_nolock (MSVCRT.@)
997 int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file)
999 int res;
1001 if(!file) {
1002 msvcrt_flush_all_buffers(MSVCRT__IOWRT);
1003 return 0;
1006 res = msvcrt_flush_buffer(file);
1007 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
1008 res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
1009 return res;
1012 /*********************************************************************
1013 * _close (MSVCRT.@)
1015 int CDECL MSVCRT__close(int fd)
1017 ioinfo *info = get_ioinfo(fd);
1018 int ret;
1020 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1021 if (!(info->wxflag & WX_OPEN)) {
1022 ret = -1;
1023 } else {
1024 ret = CloseHandle(info->handle) ? 0 : -1;
1025 msvcrt_free_fd(fd);
1026 if (ret) {
1027 WARN(":failed-last error (%d)\n",GetLastError());
1028 msvcrt_set_errno(GetLastError());
1031 release_ioinfo(info);
1032 return ret;
1035 /*********************************************************************
1036 * _dup2 (MSVCRT.@)
1037 * NOTES
1038 * MSDN isn't clear on this point, but the remarks for _pipe
1039 * indicate file descriptors duplicated with _dup and _dup2 are always
1040 * inheritable.
1042 int CDECL MSVCRT__dup2(int od, int nd)
1044 ioinfo *info_od, *info_nd;
1045 int ret;
1047 TRACE("(od=%d, nd=%d)\n", od, nd);
1049 if (od < nd)
1051 info_od = get_ioinfo(od);
1052 info_nd = get_ioinfo_alloc_fd(nd);
1054 else
1056 info_nd = get_ioinfo_alloc_fd(nd);
1057 info_od = get_ioinfo(od);
1060 if (info_nd == &MSVCRT___badioinfo)
1062 ret = -1;
1064 else if (info_od->wxflag & WX_OPEN)
1066 HANDLE handle;
1068 if (DuplicateHandle(GetCurrentProcess(), info_od->handle,
1069 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
1071 int wxflag = info_od->wxflag & ~MSVCRT__O_NOINHERIT;
1073 if (info_nd->wxflag & WX_OPEN)
1074 MSVCRT__close(nd);
1076 msvcrt_set_fd(info_nd, handle, wxflag);
1077 /* _dup2 returns 0, not nd, on success */
1078 ret = 0;
1080 else
1082 ret = -1;
1083 msvcrt_set_errno(GetLastError());
1086 else
1088 *MSVCRT__errno() = MSVCRT_EBADF;
1089 ret = -1;
1092 release_ioinfo(info_od);
1093 release_ioinfo(info_nd);
1094 return ret;
1097 /*********************************************************************
1098 * _dup (MSVCRT.@)
1100 int CDECL MSVCRT__dup(int od)
1102 int fd, ret;
1103 ioinfo *info = get_ioinfo_alloc(&fd);
1105 if (MSVCRT__dup2(od, fd) == 0)
1106 ret = fd;
1107 else
1108 ret = -1;
1109 release_ioinfo(info);
1110 return ret;
1113 /*********************************************************************
1114 * _eof (MSVCRT.@)
1116 int CDECL MSVCRT__eof(int fd)
1118 ioinfo *info = get_ioinfo(fd);
1119 DWORD curpos,endpos;
1120 LONG hcurpos,hendpos;
1122 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1124 if (info->handle == INVALID_HANDLE_VALUE)
1126 release_ioinfo(info);
1127 return -1;
1130 if (info->wxflag & WX_ATEOF)
1132 release_ioinfo(info);
1133 return TRUE;
1136 /* Otherwise we do it the hard way */
1137 hcurpos = hendpos = 0;
1138 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1139 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1141 if (curpos == endpos && hcurpos == hendpos)
1143 /* FIXME: shouldn't WX_ATEOF be set here? */
1144 release_ioinfo(info);
1145 return TRUE;
1148 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1149 release_ioinfo(info);
1150 return FALSE;
1153 /*********************************************************************
1154 * _fcloseall (MSVCRT.@)
1156 int CDECL MSVCRT__fcloseall(void)
1158 int num_closed = 0, i;
1159 MSVCRT_FILE *file;
1161 LOCK_FILES();
1162 for (i = 3; i < MSVCRT_stream_idx; i++) {
1163 file = msvcrt_get_file(i);
1165 if (file->_flag && !MSVCRT_fclose(file))
1166 num_closed++;
1168 UNLOCK_FILES();
1170 TRACE(":closed (%d) handles\n",num_closed);
1171 return num_closed;
1174 /* free everything on process exit */
1175 void msvcrt_free_io(void)
1177 unsigned int i;
1178 int j;
1180 MSVCRT__flushall();
1181 MSVCRT__fcloseall();
1183 for(i=0; i<sizeof(MSVCRT___pioinfo)/sizeof(MSVCRT___pioinfo[0]); i++)
1185 if(!MSVCRT___pioinfo[i])
1186 continue;
1188 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1190 if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
1191 DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
1193 MSVCRT_free(MSVCRT___pioinfo[i]);
1196 for(j=0; j<MSVCRT_stream_idx; j++)
1198 MSVCRT_FILE *file = msvcrt_get_file(j);
1199 if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
1201 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1202 DeleteCriticalSection(&((file_crit*)file)->crit);
1206 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1207 MSVCRT_free(MSVCRT_fstream[i]);
1210 /*********************************************************************
1211 * _lseeki64 (MSVCRT.@)
1213 __int64 CDECL MSVCRT__lseeki64(int fd, __int64 offset, int whence)
1215 ioinfo *info = get_ioinfo(fd);
1216 LARGE_INTEGER ofs;
1218 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1220 if (info->handle == INVALID_HANDLE_VALUE)
1222 release_ioinfo(info);
1223 return -1;
1226 if (whence < 0 || whence > 2)
1228 release_ioinfo(info);
1229 *MSVCRT__errno() = MSVCRT_EINVAL;
1230 return -1;
1233 TRACE(":fd (%d) to %s pos %s\n",
1234 fd,wine_dbgstr_longlong(offset),
1235 (whence==MSVCRT_SEEK_SET)?"SEEK_SET":
1236 (whence==MSVCRT_SEEK_CUR)?"SEEK_CUR":
1237 (whence==MSVCRT_SEEK_END)?"SEEK_END":"UNKNOWN");
1239 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1240 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1241 ofs.QuadPart = offset;
1242 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1243 GetLastError() == ERROR_SUCCESS)
1245 info->wxflag &= ~WX_ATEOF;
1246 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1248 release_ioinfo(info);
1249 return ofs.QuadPart;
1251 release_ioinfo(info);
1252 TRACE(":error-last error (%d)\n",GetLastError());
1253 msvcrt_set_errno(GetLastError());
1254 return -1;
1257 /*********************************************************************
1258 * _lseek (MSVCRT.@)
1260 LONG CDECL MSVCRT__lseek(int fd, LONG offset, int whence)
1262 return MSVCRT__lseeki64(fd, offset, whence);
1265 /*********************************************************************
1266 * _lock_file (MSVCRT.@)
1268 void CDECL MSVCRT__lock_file(MSVCRT_FILE *file)
1270 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1271 _lock(_STREAM_LOCKS+(file-MSVCRT__iob));
1272 else
1273 EnterCriticalSection(&((file_crit*)file)->crit);
1276 /*********************************************************************
1277 * _unlock_file (MSVCRT.@)
1279 void CDECL MSVCRT__unlock_file(MSVCRT_FILE *file)
1281 if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
1282 _unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
1283 else
1284 LeaveCriticalSection(&((file_crit*)file)->crit);
1287 /*********************************************************************
1288 * _locking (MSVCRT.@)
1290 * This is untested; the underlying LockFile doesn't work yet.
1292 int CDECL MSVCRT__locking(int fd, int mode, LONG nbytes)
1294 ioinfo *info = get_ioinfo(fd);
1295 BOOL ret;
1296 DWORD cur_locn;
1298 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1299 if (info->handle == INVALID_HANDLE_VALUE)
1301 release_ioinfo(info);
1302 return -1;
1305 if (mode < 0 || mode > 4)
1307 release_ioinfo(info);
1308 *MSVCRT__errno() = MSVCRT_EINVAL;
1309 return -1;
1312 TRACE(":fd (%d) by 0x%08x mode %s\n",
1313 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
1314 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
1315 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
1316 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
1317 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
1318 "UNKNOWN");
1320 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1322 release_ioinfo(info);
1323 FIXME ("Seek failed\n");
1324 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
1325 return -1;
1327 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
1329 int nretry = 10;
1330 ret = 1; /* just to satisfy gcc */
1331 while (nretry--)
1333 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1334 if (ret) break;
1335 Sleep(1);
1338 else if (mode == MSVCRT__LK_UNLCK)
1339 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1340 else
1341 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1342 /* FIXME - what about error settings? */
1343 release_ioinfo(info);
1344 return ret ? 0 : -1;
1347 /*********************************************************************
1348 * _fseeki64 (MSVCRT.@)
1350 int CDECL MSVCRT__fseeki64(MSVCRT_FILE* file, __int64 offset, int whence)
1352 int ret;
1354 MSVCRT__lock_file(file);
1355 ret = MSVCRT__fseeki64_nolock(file, offset, whence);
1356 MSVCRT__unlock_file(file);
1358 return ret;
1361 /*********************************************************************
1362 * _fseeki64_nolock (MSVCRT.@)
1364 int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
1366 int ret;
1368 if(whence == MSVCRT_SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1369 whence = MSVCRT_SEEK_SET;
1370 offset += MSVCRT__ftelli64_nolock(file);
1373 /* Flush output if needed */
1374 msvcrt_flush_buffer(file);
1375 /* Reset direction of i/o */
1376 if(file->_flag & MSVCRT__IORW) {
1377 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1379 /* Clear end of file flag */
1380 file->_flag &= ~MSVCRT__IOEOF;
1381 ret = (MSVCRT__lseeki64(file->_file,offset,whence) == -1)?-1:0;
1383 return ret;
1386 /*********************************************************************
1387 * fseek (MSVCRT.@)
1389 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1391 return MSVCRT__fseeki64( file, offset, whence );
1394 /*********************************************************************
1395 * _fseek_nolock (MSVCRT.@)
1397 int CDECL MSVCRT__fseek_nolock(MSVCRT_FILE* file, MSVCRT_long offset, int whence)
1399 return MSVCRT__fseeki64_nolock( file, offset, whence );
1402 /*********************************************************************
1403 * _chsize_s (MSVCRT.@)
1405 int CDECL MSVCRT__chsize_s(int fd, __int64 size)
1407 ioinfo *info;
1408 __int64 cur, pos;
1409 BOOL ret = FALSE;
1411 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1413 if (!MSVCRT_CHECK_PMT(size >= 0)) return MSVCRT_EINVAL;
1416 info = get_ioinfo(fd);
1417 if (info->handle != INVALID_HANDLE_VALUE)
1419 /* save the current file pointer */
1420 cur = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1421 if (cur >= 0)
1423 pos = MSVCRT__lseeki64(fd, size, MSVCRT_SEEK_SET);
1424 if (pos >= 0)
1426 ret = SetEndOfFile(info->handle);
1427 if (!ret) msvcrt_set_errno(GetLastError());
1430 /* restore the file pointer */
1431 MSVCRT__lseeki64(fd, cur, MSVCRT_SEEK_SET);
1435 release_ioinfo(info);
1436 return ret ? 0 : *MSVCRT__errno();
1439 /*********************************************************************
1440 * _chsize (MSVCRT.@)
1442 int CDECL MSVCRT__chsize(int fd, MSVCRT_long size)
1444 /* _chsize_s returns errno on failure but _chsize should return -1 */
1445 return MSVCRT__chsize_s( fd, size ) == 0 ? 0 : -1;
1448 /*********************************************************************
1449 * clearerr (MSVCRT.@)
1451 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
1453 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1455 MSVCRT__lock_file(file);
1456 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1457 MSVCRT__unlock_file(file);
1460 /*********************************************************************
1461 * rewind (MSVCRT.@)
1463 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
1465 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1467 MSVCRT__lock_file(file);
1468 MSVCRT__fseek_nolock(file, 0L, MSVCRT_SEEK_SET);
1469 MSVCRT_clearerr(file);
1470 MSVCRT__unlock_file(file);
1473 static int msvcrt_get_flags(const MSVCRT_wchar_t* mode, int *open_flags, int* stream_flags)
1475 int plus = strchrW(mode, '+') != NULL;
1477 TRACE("%s\n", debugstr_w(mode));
1479 while(*mode == ' ') mode++;
1481 switch(*mode++)
1483 case 'R': case 'r':
1484 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
1485 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
1486 break;
1487 case 'W': case 'w':
1488 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1489 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1490 break;
1491 case 'A': case 'a':
1492 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
1493 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
1494 break;
1495 default:
1496 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1497 return -1;
1500 *stream_flags |= MSVCRT__commode;
1502 while (*mode && *mode!=',')
1503 switch (*mode++)
1505 case 'B': case 'b':
1506 *open_flags |= MSVCRT__O_BINARY;
1507 *open_flags &= ~MSVCRT__O_TEXT;
1508 break;
1509 case 't':
1510 *open_flags |= MSVCRT__O_TEXT;
1511 *open_flags &= ~MSVCRT__O_BINARY;
1512 break;
1513 case 'D':
1514 *open_flags |= MSVCRT__O_TEMPORARY;
1515 break;
1516 case 'T':
1517 *open_flags |= MSVCRT__O_SHORT_LIVED;
1518 break;
1519 case 'c':
1520 *stream_flags |= MSVCRT__IOCOMMIT;
1521 break;
1522 case 'n':
1523 *stream_flags &= ~MSVCRT__IOCOMMIT;
1524 break;
1525 case 'N':
1526 *open_flags |= MSVCRT__O_NOINHERIT;
1527 break;
1528 case '+':
1529 case ' ':
1530 case 'a':
1531 case 'w':
1532 break;
1533 case 'S':
1534 case 'R':
1535 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1536 break;
1537 default:
1538 ERR("incorrect mode flag: %c\n", mode[-1]);
1539 break;
1542 if(*mode == ',')
1544 static const WCHAR ccs[] = {'c','c','s'};
1545 static const WCHAR utf8[] = {'u','t','f','-','8'};
1546 static const WCHAR utf16le[] = {'u','t','f','-','1','6','l','e'};
1547 static const WCHAR unicode[] = {'u','n','i','c','o','d','e'};
1549 mode++;
1550 while(*mode == ' ') mode++;
1551 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1552 return -1;
1553 mode += sizeof(ccs)/sizeof(ccs[0]);
1554 while(*mode == ' ') mode++;
1555 if(!MSVCRT_CHECK_PMT(*mode == '='))
1556 return -1;
1557 mode++;
1558 while(*mode == ' ') mode++;
1560 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1562 *open_flags |= MSVCRT__O_U8TEXT;
1563 mode += sizeof(utf8)/sizeof(utf8[0]);
1565 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1567 *open_flags |= MSVCRT__O_U16TEXT;
1568 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1570 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1572 *open_flags |= MSVCRT__O_WTEXT;
1573 mode += sizeof(unicode)/sizeof(unicode[0]);
1575 else
1577 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL);
1578 return -1;
1581 while(*mode == ' ') mode++;
1584 if(!MSVCRT_CHECK_PMT(*mode == 0))
1585 return -1;
1586 return 0;
1589 /*********************************************************************
1590 * _fdopen (MSVCRT.@)
1592 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1594 MSVCRT_FILE *ret;
1595 MSVCRT_wchar_t *modeW = NULL;
1597 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1599 ret = MSVCRT__wfdopen(fd, modeW);
1601 MSVCRT_free(modeW);
1602 return ret;
1605 /*********************************************************************
1606 * _wfdopen (MSVCRT.@)
1608 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1610 int open_flags, stream_flags;
1611 MSVCRT_FILE* file;
1613 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1615 LOCK_FILES();
1616 if (!(file = msvcrt_alloc_fp()))
1617 file = NULL;
1618 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1620 file->_flag = 0;
1621 file = NULL;
1623 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1624 UNLOCK_FILES();
1626 return file;
1629 /*********************************************************************
1630 * _filelength (MSVCRT.@)
1632 LONG CDECL MSVCRT__filelength(int fd)
1634 LONG curPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
1635 if (curPos != -1)
1637 LONG endPos = MSVCRT__lseek(fd, 0, MSVCRT_SEEK_END);
1638 if (endPos != -1)
1640 if (endPos != curPos)
1641 MSVCRT__lseek(fd, curPos, MSVCRT_SEEK_SET);
1642 return endPos;
1645 return -1;
1648 /*********************************************************************
1649 * _filelengthi64 (MSVCRT.@)
1651 __int64 CDECL MSVCRT__filelengthi64(int fd)
1653 __int64 curPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
1654 if (curPos != -1)
1656 __int64 endPos = MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_END);
1657 if (endPos != -1)
1659 if (endPos != curPos)
1660 MSVCRT__lseeki64(fd, curPos, MSVCRT_SEEK_SET);
1661 return endPos;
1664 return -1;
1667 /*********************************************************************
1668 * _fileno (MSVCRT.@)
1670 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1672 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1673 return file->_file;
1676 /*********************************************************************
1677 * _fstat64 (MSVCRT.@)
1679 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1681 ioinfo *info = get_ioinfo(fd);
1682 DWORD dw;
1683 DWORD type;
1684 BY_HANDLE_FILE_INFORMATION hfi;
1686 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1687 if (info->handle == INVALID_HANDLE_VALUE)
1689 release_ioinfo(info);
1690 return -1;
1693 if (!buf)
1695 WARN(":failed-NULL buf\n");
1696 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1697 release_ioinfo(info);
1698 return -1;
1701 memset(&hfi, 0, sizeof(hfi));
1702 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1703 type = GetFileType(info->handle);
1704 if (type == FILE_TYPE_PIPE)
1706 buf->st_dev = buf->st_rdev = fd;
1707 buf->st_mode = MSVCRT__S_IFIFO;
1708 buf->st_nlink = 1;
1710 else if (type == FILE_TYPE_CHAR)
1712 buf->st_dev = buf->st_rdev = fd;
1713 buf->st_mode = MSVCRT__S_IFCHR;
1714 buf->st_nlink = 1;
1716 else /* FILE_TYPE_DISK etc. */
1718 if (!GetFileInformationByHandle(info->handle, &hfi))
1720 WARN(":failed-last error (%d)\n",GetLastError());
1721 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1722 release_ioinfo(info);
1723 return -1;
1725 buf->st_mode = MSVCRT__S_IFREG | 0444;
1726 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1727 buf->st_mode |= 0222;
1728 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1729 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1730 buf->st_atime = dw;
1731 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1732 buf->st_mtime = buf->st_ctime = dw;
1733 buf->st_nlink = hfi.nNumberOfLinks;
1735 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1736 buf->st_mode);
1737 release_ioinfo(info);
1738 return 0;
1741 /*********************************************************************
1742 * _fstati64 (MSVCRT.@)
1744 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1746 int ret;
1747 struct MSVCRT__stat64 buf64;
1749 ret = MSVCRT__fstat64(fd, &buf64);
1750 if (!ret)
1751 msvcrt_stat64_to_stati64(&buf64, buf);
1752 return ret;
1755 /*********************************************************************
1756 * _fstat (MSVCRT.@)
1758 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1759 { int ret;
1760 struct MSVCRT__stat64 buf64;
1762 ret = MSVCRT__fstat64(fd, &buf64);
1763 if (!ret)
1764 msvcrt_stat64_to_stat(&buf64, buf);
1765 return ret;
1768 /*********************************************************************
1769 * _fstat32 (MSVCR80.@)
1771 int CDECL MSVCRT__fstat32(int fd, struct MSVCRT__stat32* buf)
1773 int ret;
1774 struct MSVCRT__stat64 buf64;
1776 ret = MSVCRT__fstat64(fd, &buf64);
1777 if (!ret)
1778 msvcrt_stat64_to_stat32(&buf64, buf);
1779 return ret;
1782 /*********************************************************************
1783 * _fstat64i32 (MSVCR80.@)
1785 int CDECL MSVCRT__fstat64i32(int fd, struct MSVCRT__stat64i32* buf)
1787 int ret;
1788 struct MSVCRT__stat64 buf64;
1790 ret = MSVCRT__fstat64(fd, &buf64);
1791 if (!ret)
1792 msvcrt_stat64_to_stat64i32(&buf64, buf);
1793 return ret;
1796 /*********************************************************************
1797 * _futime64 (MSVCRT.@)
1799 int CDECL _futime64(int fd, struct MSVCRT___utimbuf64 *t)
1801 ioinfo *info = get_ioinfo(fd);
1802 FILETIME at, wt;
1804 if (!t)
1806 time_to_filetime( MSVCRT__time64(NULL), &at );
1807 wt = at;
1809 else
1811 time_to_filetime( t->actime, &at );
1812 time_to_filetime( t->modtime, &wt );
1815 if (!SetFileTime(info->handle, NULL, &at, &wt))
1817 release_ioinfo(info);
1818 msvcrt_set_errno(GetLastError());
1819 return -1 ;
1821 release_ioinfo(info);
1822 return 0;
1825 /*********************************************************************
1826 * _futime32 (MSVCRT.@)
1828 int CDECL _futime32(int fd, struct MSVCRT___utimbuf32 *t)
1830 if (t)
1832 struct MSVCRT___utimbuf64 t64;
1833 t64.actime = t->actime;
1834 t64.modtime = t->modtime;
1835 return _futime64( fd, &t64 );
1837 else
1838 return _futime64( fd, NULL );
1841 /*********************************************************************
1842 * _futime (MSVCRT.@)
1844 #ifdef _WIN64
1845 int CDECL _futime(int fd, struct MSVCRT___utimbuf64 *t)
1847 return _futime64( fd, t );
1849 #else
1850 int CDECL _futime(int fd, struct MSVCRT___utimbuf32 *t)
1852 return _futime32( fd, t );
1854 #endif
1856 /*********************************************************************
1857 * _get_osfhandle (MSVCRT.@)
1859 MSVCRT_intptr_t CDECL MSVCRT__get_osfhandle(int fd)
1861 HANDLE hand = get_ioinfo_nolock(fd)->handle;
1862 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1864 if(hand == INVALID_HANDLE_VALUE)
1865 *MSVCRT__errno() = MSVCRT_EBADF;
1866 return (MSVCRT_intptr_t)hand;
1869 /*********************************************************************
1870 * _mktemp_s (MSVCRT.@)
1872 int CDECL MSVCRT__mktemp_s(char *pattern, MSVCRT_size_t size)
1874 DWORD len, xno, id;
1876 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1877 return MSVCRT_EINVAL;
1879 for(len=0; len<size; len++)
1880 if(!pattern[len])
1881 break;
1882 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1883 if(size)
1884 pattern[0] = 0;
1885 return MSVCRT_EINVAL;
1888 for(xno=1; xno<=6; xno++)
1889 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1890 return MSVCRT_EINVAL;
1892 id = GetCurrentProcessId();
1893 for(xno=1; xno<6; xno++) {
1894 pattern[len-xno] = id%10 + '0';
1895 id /= 10;
1898 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1899 if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
1900 return 0;
1903 pattern[0] = 0;
1904 *MSVCRT__errno() = MSVCRT_EEXIST;
1905 return MSVCRT_EEXIST;
1908 /*********************************************************************
1909 * _mktemp (MSVCRT.@)
1911 char * CDECL MSVCRT__mktemp(char *pattern)
1913 int numX = 0;
1914 char *retVal = pattern;
1915 int id;
1916 char letter = 'a';
1918 if(!pattern)
1919 return NULL;
1921 while(*pattern)
1922 numX = (*pattern++ == 'X')? numX + 1 : 0;
1923 if (numX < 6)
1924 return NULL;
1925 pattern--;
1926 id = GetCurrentProcessId();
1927 numX = 6;
1928 while(numX--)
1930 int tempNum = id / 10;
1931 *pattern-- = id - (tempNum * 10) + '0';
1932 id = tempNum;
1934 pattern++;
1937 *pattern = letter++;
1938 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
1939 return retVal;
1940 } while(letter <= 'z');
1941 return NULL;
1944 /*********************************************************************
1945 * _wmktemp_s (MSVCRT.@)
1947 int CDECL MSVCRT__wmktemp_s(MSVCRT_wchar_t *pattern, MSVCRT_size_t size)
1949 DWORD len, xno, id;
1951 if(!MSVCRT_CHECK_PMT(pattern!=NULL))
1952 return MSVCRT_EINVAL;
1954 for(len=0; len<size; len++)
1955 if(!pattern[len])
1956 break;
1957 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
1958 if(size)
1959 pattern[0] = 0;
1960 return MSVCRT_EINVAL;
1963 for(xno=1; xno<=6; xno++)
1964 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
1965 return MSVCRT_EINVAL;
1967 id = GetCurrentProcessId();
1968 for(xno=1; xno<6; xno++) {
1969 pattern[len-xno] = id%10 + '0';
1970 id /= 10;
1973 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
1974 if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
1975 return 0;
1978 pattern[0] = 0;
1979 *MSVCRT__errno() = MSVCRT_EEXIST;
1980 return MSVCRT_EEXIST;
1983 /*********************************************************************
1984 * _wmktemp (MSVCRT.@)
1986 MSVCRT_wchar_t * CDECL MSVCRT__wmktemp(MSVCRT_wchar_t *pattern)
1988 int numX = 0;
1989 MSVCRT_wchar_t *retVal = pattern;
1990 int id;
1991 MSVCRT_wchar_t letter = 'a';
1993 if(!pattern)
1994 return NULL;
1996 while(*pattern)
1997 numX = (*pattern++ == 'X')? numX + 1 : 0;
1998 if (numX < 6)
1999 return NULL;
2000 pattern--;
2001 id = GetCurrentProcessId();
2002 numX = 6;
2003 while(numX--)
2005 int tempNum = id / 10;
2006 *pattern-- = id - (tempNum * 10) + '0';
2007 id = tempNum;
2009 pattern++;
2012 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
2013 return retVal;
2014 *pattern = letter++;
2015 } while(letter != '|');
2016 return NULL;
2019 static unsigned split_oflags(unsigned oflags)
2021 int wxflags = 0;
2022 unsigned unsupp; /* until we support everything */
2024 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
2025 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
2026 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
2027 else if (oflags & MSVCRT__O_WTEXT) wxflags |= WX_TEXT;
2028 else if (oflags & MSVCRT__O_U16TEXT) wxflags |= WX_TEXT;
2029 else if (oflags & MSVCRT__O_U8TEXT) wxflags |= WX_TEXT;
2030 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
2031 else wxflags |= WX_TEXT; /* default to TEXT*/
2032 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2034 if ((unsupp = oflags & ~(
2035 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
2036 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
2037 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
2038 MSVCRT__O_NOINHERIT|
2039 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED|
2040 MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT
2042 ERR(":unsupported oflags 0x%04x\n",unsupp);
2044 return wxflags;
2047 /*********************************************************************
2048 * _pipe (MSVCRT.@)
2050 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
2052 int ret = -1;
2053 SECURITY_ATTRIBUTES sa;
2054 HANDLE readHandle, writeHandle;
2056 if (!pfds)
2058 *MSVCRT__errno() = MSVCRT_EINVAL;
2059 return -1;
2062 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2063 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
2064 sa.lpSecurityDescriptor = NULL;
2065 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2067 unsigned int wxflags = split_oflags(textmode);
2068 int fd;
2070 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2071 if (fd != -1)
2073 pfds[0] = fd;
2074 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2075 if (fd != -1)
2077 pfds[1] = fd;
2078 ret = 0;
2080 else
2082 MSVCRT__close(pfds[0]);
2083 CloseHandle(writeHandle);
2084 *MSVCRT__errno() = MSVCRT_EMFILE;
2087 else
2089 CloseHandle(readHandle);
2090 CloseHandle(writeHandle);
2091 *MSVCRT__errno() = MSVCRT_EMFILE;
2094 else
2095 msvcrt_set_errno(GetLastError());
2097 return ret;
2100 static int check_bom(HANDLE h, int oflags, BOOL seek)
2102 char bom[sizeof(utf8_bom)];
2103 DWORD r;
2105 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2107 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2108 return oflags;
2110 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2111 oflags |= MSVCRT__O_U8TEXT;
2112 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2113 if (seek && r>2)
2114 SetFilePointer(h, 2, NULL, FILE_BEGIN);
2115 oflags |= MSVCRT__O_U16TEXT;
2116 }else if (seek) {
2117 SetFilePointer(h, 0, NULL, FILE_BEGIN);
2120 return oflags;
2123 /*********************************************************************
2124 * _wsopen_s (MSVCRT.@)
2126 int CDECL MSVCRT__wsopen_s( int *fd, const MSVCRT_wchar_t* path, int oflags, int shflags, int pmode )
2128 DWORD access = 0, creation = 0, attrib;
2129 SECURITY_ATTRIBUTES sa;
2130 DWORD sharing, type;
2131 int wxflag;
2132 HANDLE hand;
2134 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2135 fd, debugstr_w(path), oflags, shflags, pmode);
2137 if (!MSVCRT_CHECK_PMT( fd != NULL )) return MSVCRT_EINVAL;
2139 *fd = -1;
2140 wxflag = split_oflags(oflags);
2141 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
2143 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
2144 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
2145 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2148 if (oflags & MSVCRT__O_CREAT)
2150 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
2151 FIXME(": pmode 0x%04x ignored\n", pmode);
2153 if (oflags & MSVCRT__O_EXCL)
2154 creation = CREATE_NEW;
2155 else if (oflags & MSVCRT__O_TRUNC)
2156 creation = CREATE_ALWAYS;
2157 else
2158 creation = OPEN_ALWAYS;
2160 else /* no MSVCRT__O_CREAT */
2162 if (oflags & MSVCRT__O_TRUNC)
2163 creation = TRUNCATE_EXISTING;
2164 else
2165 creation = OPEN_EXISTING;
2168 switch( shflags )
2170 case MSVCRT__SH_DENYRW:
2171 sharing = 0L;
2172 break;
2173 case MSVCRT__SH_DENYWR:
2174 sharing = FILE_SHARE_READ;
2175 break;
2176 case MSVCRT__SH_DENYRD:
2177 sharing = FILE_SHARE_WRITE;
2178 break;
2179 case MSVCRT__SH_DENYNO:
2180 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
2181 break;
2182 default:
2183 ERR( "Unhandled shflags 0x%x\n", shflags );
2184 return MSVCRT_EINVAL;
2187 if (!(pmode & ~MSVCRT_umask & MSVCRT__S_IWRITE))
2188 attrib = FILE_ATTRIBUTE_READONLY;
2189 else
2190 attrib = FILE_ATTRIBUTE_NORMAL;
2192 if (oflags & MSVCRT__O_TEMPORARY)
2194 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2195 access |= DELETE;
2196 sharing |= FILE_SHARE_DELETE;
2199 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2200 sa.lpSecurityDescriptor = NULL;
2201 sa.bInheritHandle = !(oflags & MSVCRT__O_NOINHERIT);
2203 if ((oflags&(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2204 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2205 && !(access&GENERIC_READ))
2207 hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2208 &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2209 if (hand != INVALID_HANDLE_VALUE)
2211 oflags = check_bom(hand, oflags, FALSE);
2212 CloseHandle(hand);
2214 else
2215 oflags &= ~(MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT);
2218 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2219 if (hand == INVALID_HANDLE_VALUE) {
2220 WARN(":failed-last error (%d)\n",GetLastError());
2221 msvcrt_set_errno(GetLastError());
2222 return *MSVCRT__errno();
2225 if (oflags & (MSVCRT__O_WTEXT|MSVCRT__O_U16TEXT|MSVCRT__O_U8TEXT))
2227 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2228 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2229 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
2231 if (oflags & MSVCRT__O_U8TEXT)
2233 DWORD written = 0, tmp;
2235 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2236 sizeof(utf8_bom)-written, &tmp, NULL))
2237 written += tmp;
2238 if (written != sizeof(utf8_bom)) {
2239 WARN("error writing BOM\n");
2240 CloseHandle(hand);
2241 msvcrt_set_errno(GetLastError());
2242 return *MSVCRT__errno();
2245 else
2247 DWORD written = 0, tmp;
2249 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2250 sizeof(utf16_bom)-written, &tmp, NULL))
2251 written += tmp;
2252 if (written != sizeof(utf16_bom))
2254 WARN("error writing BOM\n");
2255 CloseHandle(hand);
2256 msvcrt_set_errno(GetLastError());
2257 return *MSVCRT__errno();
2261 else if (access & GENERIC_READ)
2262 oflags = check_bom(hand, oflags, TRUE);
2265 type = GetFileType(hand);
2266 if (type == FILE_TYPE_CHAR)
2267 wxflag |= WX_TTY;
2268 else if (type == FILE_TYPE_PIPE)
2269 wxflag |= WX_PIPE;
2271 *fd = msvcrt_alloc_fd(hand, wxflag);
2272 if (*fd == -1)
2273 return *MSVCRT__errno();
2275 if (oflags & MSVCRT__O_WTEXT)
2276 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
2277 else if (oflags & MSVCRT__O_U16TEXT)
2278 get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
2279 else if (oflags & MSVCRT__O_U8TEXT)
2280 get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
2282 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2283 return 0;
2286 /*********************************************************************
2287 * _wsopen (MSVCRT.@)
2289 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t *path, int oflags, int shflags, ... )
2291 int pmode;
2292 int fd;
2294 if (oflags & MSVCRT__O_CREAT)
2296 __ms_va_list ap;
2298 __ms_va_start(ap, shflags);
2299 pmode = va_arg(ap, int);
2300 __ms_va_end(ap);
2302 else
2303 pmode = 0;
2305 MSVCRT__wsopen_s(&fd, path, oflags, shflags, pmode);
2306 return fd;
2309 /*********************************************************************
2310 * _sopen_s (MSVCRT.@)
2312 int CDECL MSVCRT__sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2314 MSVCRT_wchar_t *pathW;
2315 int ret;
2317 if (!MSVCRT_CHECK_PMT(fd != NULL))
2318 return MSVCRT_EINVAL;
2319 *fd = -1;
2320 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
2321 return MSVCRT_EINVAL;
2323 ret = MSVCRT__wsopen_s(fd, pathW, oflags, shflags, pmode);
2324 MSVCRT_free(pathW);
2325 return ret;
2328 /*********************************************************************
2329 * _sopen (MSVCRT.@)
2331 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
2333 int pmode;
2334 int fd;
2336 if (oflags & MSVCRT__O_CREAT)
2338 __ms_va_list ap;
2340 __ms_va_start(ap, shflags);
2341 pmode = va_arg(ap, int);
2342 __ms_va_end(ap);
2344 else
2345 pmode = 0;
2347 MSVCRT__sopen_s(&fd, path, oflags, shflags, pmode);
2348 return fd;
2351 /*********************************************************************
2352 * _open (MSVCRT.@)
2354 int CDECL MSVCRT__open( const char *path, int flags, ... )
2356 __ms_va_list ap;
2358 if (flags & MSVCRT__O_CREAT)
2360 int pmode;
2361 __ms_va_start(ap, flags);
2362 pmode = va_arg(ap, int);
2363 __ms_va_end(ap);
2364 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2366 else
2367 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
2370 /*********************************************************************
2371 * _wopen (MSVCRT.@)
2373 int CDECL MSVCRT__wopen(const MSVCRT_wchar_t *path,int flags,...)
2375 __ms_va_list ap;
2377 if (flags & MSVCRT__O_CREAT)
2379 int pmode;
2380 __ms_va_start(ap, flags);
2381 pmode = va_arg(ap, int);
2382 __ms_va_end(ap);
2383 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO, pmode );
2385 else
2386 return MSVCRT__wsopen( path, flags, MSVCRT__SH_DENYNO);
2389 /*********************************************************************
2390 * _creat (MSVCRT.@)
2392 int CDECL MSVCRT__creat(const char *path, int flags)
2394 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2395 return MSVCRT__open(path, usedFlags);
2398 /*********************************************************************
2399 * _wcreat (MSVCRT.@)
2401 int CDECL MSVCRT__wcreat(const MSVCRT_wchar_t *path, int flags)
2403 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
2404 return MSVCRT__wopen(path, usedFlags);
2407 /*********************************************************************
2408 * _open_osfhandle (MSVCRT.@)
2410 int CDECL MSVCRT__open_osfhandle(MSVCRT_intptr_t handle, int oflags)
2412 DWORD flags;
2413 int fd;
2415 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2416 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2417 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2418 * text - it never sets MSVCRT__O_BINARY.
2420 /* don't let split_oflags() decide the mode if no mode is passed */
2421 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
2422 oflags |= MSVCRT__O_BINARY;
2424 flags = GetFileType((HANDLE)handle);
2425 if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
2427 msvcrt_set_errno(GetLastError());
2428 return -1;
2431 if (flags == FILE_TYPE_CHAR)
2432 flags = WX_TTY;
2433 else if (flags == FILE_TYPE_PIPE)
2434 flags = WX_PIPE;
2435 else
2436 flags = 0;
2437 flags |= split_oflags(oflags);
2439 fd = msvcrt_alloc_fd((HANDLE)handle, flags);
2440 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2441 return fd;
2444 /*********************************************************************
2445 * _rmtmp (MSVCRT.@)
2447 int CDECL MSVCRT__rmtmp(void)
2449 int num_removed = 0, i;
2450 MSVCRT_FILE *file;
2452 LOCK_FILES();
2453 for (i = 3; i < MSVCRT_stream_idx; i++) {
2454 file = msvcrt_get_file(i);
2456 if (file->_tmpfname)
2458 MSVCRT_fclose(file);
2459 num_removed++;
2462 UNLOCK_FILES();
2464 if (num_removed)
2465 TRACE(":removed (%d) temp files\n",num_removed);
2466 return num_removed;
2469 static inline int get_utf8_char_len(char ch)
2471 if((ch&0xf8) == 0xf0)
2472 return 4;
2473 else if((ch&0xf0) == 0xe0)
2474 return 3;
2475 else if((ch&0xe0) == 0xc0)
2476 return 2;
2477 return 1;
2480 /*********************************************************************
2481 * (internal) read_utf8
2483 static int read_utf8(ioinfo *fdinfo, MSVCRT_wchar_t *buf, unsigned int count)
2485 HANDLE hand = fdinfo->handle;
2486 char min_buf[4], *readbuf, lookahead;
2487 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2489 /* make the buffer big enough to hold at least one character */
2490 /* read bytes have to fit to output and lookahead buffers */
2491 count /= 2;
2492 readbuf_size = count < 4 ? 4 : count;
2493 if(readbuf_size<=4 || !(readbuf = MSVCRT_malloc(readbuf_size))) {
2494 readbuf_size = 4;
2495 readbuf = min_buf;
2498 if(fdinfo->lookahead[0] != '\n') {
2499 readbuf[pos++] = fdinfo->lookahead[0];
2500 fdinfo->lookahead[0] = '\n';
2502 if(fdinfo->lookahead[1] != '\n') {
2503 readbuf[pos++] = fdinfo->lookahead[1];
2504 fdinfo->lookahead[1] = '\n';
2506 if(fdinfo->lookahead[2] != '\n') {
2507 readbuf[pos++] = fdinfo->lookahead[2];
2508 fdinfo->lookahead[2] = '\n';
2513 /* NOTE: this case is broken in native dll, reading
2514 * sometimes fails when small buffer is passed
2516 if(count < 4) {
2517 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2518 if (GetLastError() == ERROR_BROKEN_PIPE) {
2519 fdinfo->wxflag |= WX_ATEOF;
2520 return 0;
2521 }else {
2522 msvcrt_set_errno(GetLastError());
2523 return -1;
2525 }else if(!num_read) {
2526 fdinfo->wxflag |= WX_ATEOF;
2527 return 0;
2528 }else {
2529 pos++;
2532 char_len = get_utf8_char_len(readbuf[0]);
2533 if(char_len>pos) {
2534 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2535 pos += num_read;
2538 if(readbuf[0] == '\n')
2539 fdinfo->wxflag |= WX_READNL;
2540 else
2541 fdinfo->wxflag &= ~WX_READNL;
2543 if(readbuf[0] == 0x1a) {
2544 fdinfo->wxflag |= WX_ATEOF;
2545 return 0;
2548 if(readbuf[0] == '\r') {
2549 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2550 buf[0] = '\r';
2551 else if(lookahead == '\n')
2552 buf[0] = '\n';
2553 else {
2554 buf[0] = '\r';
2555 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2556 fdinfo->lookahead[0] = lookahead;
2557 else
2558 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2560 return 2;
2563 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2564 msvcrt_set_errno(GetLastError());
2565 return -1;
2568 return num_read*2;
2571 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2572 if(pos) {
2573 num_read = 0;
2574 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2575 fdinfo->wxflag |= WX_ATEOF;
2576 if (readbuf != min_buf) MSVCRT_free(readbuf);
2577 return 0;
2578 }else {
2579 msvcrt_set_errno(GetLastError());
2580 if (readbuf != min_buf) MSVCRT_free(readbuf);
2581 return -1;
2583 }else if(!pos && !num_read) {
2584 fdinfo->wxflag |= WX_ATEOF;
2585 if (readbuf != min_buf) MSVCRT_free(readbuf);
2586 return 0;
2589 pos += num_read;
2590 if(readbuf[0] == '\n')
2591 fdinfo->wxflag |= WX_READNL;
2592 else
2593 fdinfo->wxflag &= ~WX_READNL;
2595 /* Find first byte of last character (may be incomplete) */
2596 for(i=pos-1; i>0 && i>pos-4; i--)
2597 if((readbuf[i]&0xc0) != 0x80)
2598 break;
2599 char_len = get_utf8_char_len(readbuf[i]);
2600 if(char_len+i <= pos)
2601 i += char_len;
2603 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2604 if(i < pos)
2605 fdinfo->lookahead[0] = readbuf[i];
2606 if(i+1 < pos)
2607 fdinfo->lookahead[1] = readbuf[i+1];
2608 if(i+2 < pos)
2609 fdinfo->lookahead[2] = readbuf[i+2];
2610 }else if(i < pos) {
2611 SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
2613 pos = i;
2615 for(i=0, j=0; i<pos; i++) {
2616 if(readbuf[i] == 0x1a) {
2617 fdinfo->wxflag |= WX_ATEOF;
2618 break;
2621 /* strip '\r' if followed by '\n' */
2622 if(readbuf[i] == '\r' && i+1==pos) {
2623 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2624 readbuf[j++] = '\r';
2625 }else if(lookahead == '\n' && j==0) {
2626 readbuf[j++] = '\n';
2627 }else {
2628 if(lookahead != '\n')
2629 readbuf[j++] = '\r';
2631 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2632 fdinfo->lookahead[0] = lookahead;
2633 else
2634 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2636 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2637 readbuf[j++] = readbuf[i];
2640 pos = j;
2642 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2643 msvcrt_set_errno(GetLastError());
2644 if (readbuf != min_buf) MSVCRT_free(readbuf);
2645 return -1;
2648 if (readbuf != min_buf) MSVCRT_free(readbuf);
2649 return num_read*2;
2652 /*********************************************************************
2653 * (internal) read_i
2655 * When reading \r as last character in text mode, read() positions
2656 * the file pointer on the \r character while getc() goes on to
2657 * the following \n
2659 static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
2661 DWORD num_read, utf16;
2662 char *bufstart = buf;
2664 if (count == 0)
2665 return 0;
2667 if (fdinfo->wxflag & WX_ATEOF) {
2668 TRACE("already at EOF, returning 0\n");
2669 return 0;
2671 /* Don't trace small reads, it gets *very* annoying */
2672 if (count > 4)
2673 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2674 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2676 *MSVCRT__errno() = MSVCRT_EBADF;
2677 return -1;
2680 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2681 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2683 *MSVCRT__errno() = MSVCRT_EINVAL;
2684 return -1;
2687 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2688 return read_utf8(fdinfo, buf, count);
2690 if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
2692 if (fdinfo->lookahead[0] != '\n')
2694 bufstart[0] = fdinfo->lookahead[0];
2695 fdinfo->lookahead[0] = '\n';
2697 if (utf16)
2699 bufstart[1] = fdinfo->lookahead[1];
2700 fdinfo->lookahead[1] = '\n';
2703 if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2704 num_read += 1+utf16;
2705 else
2706 num_read = 1+utf16;
2709 if(utf16 && (num_read&1))
2711 /* msvcr90 uses uninitialized value from the buffer in this case */
2712 /* msvcrt ignores additional data */
2713 ERR("got odd number of bytes in UTF16 mode\n");
2714 num_read--;
2717 if (count != 0 && num_read == 0)
2719 fdinfo->wxflag |= WX_ATEOF;
2720 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2722 else if (fdinfo->wxflag & WX_TEXT)
2724 DWORD i, j;
2726 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2727 fdinfo->wxflag |= WX_READNL;
2728 else
2729 fdinfo->wxflag &= ~WX_READNL;
2731 for (i=0, j=0; i<num_read; i+=1+utf16)
2733 /* in text mode, a ctrl-z signals EOF */
2734 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2736 fdinfo->wxflag |= WX_ATEOF;
2737 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2738 break;
2741 /* in text mode, strip \r if followed by \n */
2742 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2744 char lookahead[2];
2745 DWORD len;
2747 lookahead[1] = '\n';
2748 if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
2750 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2752 bufstart[j++] = '\n';
2753 if(utf16) bufstart[j++] = 0;
2755 else
2757 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2759 bufstart[j++] = '\r';
2760 if(utf16) bufstart[j++] = 0;
2763 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2765 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2767 bufstart[j++] = '\n';
2768 if (utf16) bufstart[j++] = 0;
2770 else
2772 fdinfo->lookahead[0] = lookahead[0];
2773 fdinfo->lookahead[1] = lookahead[1];
2776 else
2777 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2780 else
2782 bufstart[j++] = '\r';
2783 if(utf16) bufstart[j++] = 0;
2786 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2787 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2789 bufstart[j++] = bufstart[i];
2790 if(utf16) bufstart[j++] = bufstart[i+1];
2793 num_read = j;
2796 else
2798 if (GetLastError() == ERROR_BROKEN_PIPE)
2800 TRACE(":end-of-pipe\n");
2801 fdinfo->wxflag |= WX_ATEOF;
2802 return 0;
2804 else
2806 TRACE(":failed-last error (%d)\n",GetLastError());
2807 return -1;
2811 if (count > 4)
2812 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2813 return num_read;
2816 /*********************************************************************
2817 * _read (MSVCRT.@)
2819 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
2821 ioinfo *info = get_ioinfo(fd);
2822 int num_read = read_i(fd, info, buf, count);
2823 release_ioinfo(info);
2824 return num_read;
2827 /*********************************************************************
2828 * _setmode (MSVCRT.@)
2830 int CDECL MSVCRT__setmode(int fd,int mode)
2832 ioinfo *info = get_ioinfo(fd);
2833 int ret = info->wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
2834 if(ret==MSVCRT__O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16)))
2835 ret = MSVCRT__O_WTEXT;
2837 if(mode!=MSVCRT__O_TEXT && mode!=MSVCRT__O_BINARY && mode!=MSVCRT__O_WTEXT
2838 && mode!=MSVCRT__O_U16TEXT && mode!=MSVCRT__O_U8TEXT) {
2839 *MSVCRT__errno() = MSVCRT_EINVAL;
2840 release_ioinfo(info);
2841 return -1;
2844 if(info == &MSVCRT___badioinfo) {
2845 *MSVCRT__errno() = MSVCRT_EBADF;
2846 return MSVCRT_EOF;
2849 if(mode == MSVCRT__O_BINARY) {
2850 info->wxflag &= ~WX_TEXT;
2851 info->exflag &= ~(EF_UTF8|EF_UTF16);
2852 release_ioinfo(info);
2853 return ret;
2856 info->wxflag |= WX_TEXT;
2857 if(mode == MSVCRT__O_TEXT)
2858 info->exflag &= ~(EF_UTF8|EF_UTF16);
2859 else if(mode == MSVCRT__O_U8TEXT)
2860 info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
2861 else
2862 info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
2864 release_ioinfo(info);
2865 return ret;
2868 /*********************************************************************
2869 * _stat64 (MSVCRT.@)
2871 int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf)
2873 DWORD dw;
2874 WIN32_FILE_ATTRIBUTE_DATA hfi;
2875 unsigned short mode = ALL_S_IREAD;
2876 int plen;
2878 TRACE(":file (%s) buf(%p)\n",path,buf);
2880 plen = strlen(path);
2881 while (plen && path[plen-1]==' ')
2882 plen--;
2884 if (plen && (plen<2 || path[plen-2]!=':') &&
2885 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
2887 *MSVCRT__errno() = MSVCRT_ENOENT;
2888 return -1;
2891 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
2893 TRACE("failed (%d)\n",GetLastError());
2894 *MSVCRT__errno() = MSVCRT_ENOENT;
2895 return -1;
2898 memset(buf,0,sizeof(struct MSVCRT__stat64));
2900 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2901 Bon 011120: This FIXME seems incorrect
2902 Also a letter as first char isn't enough to be classified
2903 as a drive letter
2905 if (isalpha(*path)&& (*(path+1)==':'))
2906 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
2907 else
2908 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
2910 /* Dir, or regular file? */
2911 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2912 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
2913 else
2915 mode |= MSVCRT__S_IFREG;
2916 /* executable? */
2917 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
2919 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
2920 (tolower(path[plen-3]) << 16);
2921 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
2922 mode |= ALL_S_IEXEC;
2926 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
2927 mode |= ALL_S_IWRITE;
2929 buf->st_mode = mode;
2930 buf->st_nlink = 1;
2931 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
2932 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
2933 buf->st_atime = dw;
2934 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
2935 buf->st_mtime = buf->st_ctime = dw;
2936 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
2937 (int)(buf->st_size >> 32),(int)buf->st_size,
2938 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
2939 return 0;
2942 /*********************************************************************
2943 * _stati64 (MSVCRT.@)
2945 int CDECL MSVCRT_stati64(const char* path, struct MSVCRT__stati64 * buf)
2947 int ret;
2948 struct MSVCRT__stat64 buf64;
2950 ret = MSVCRT_stat64(path, &buf64);
2951 if (!ret)
2952 msvcrt_stat64_to_stati64(&buf64, buf);
2953 return ret;
2956 /*********************************************************************
2957 * _stat (MSVCRT.@)
2959 int CDECL MSVCRT_stat(const char* path, struct MSVCRT__stat * buf)
2961 int ret;
2962 struct MSVCRT__stat64 buf64;
2964 ret = MSVCRT_stat64( path, &buf64);
2965 if (!ret)
2966 msvcrt_stat64_to_stat(&buf64, buf);
2967 return ret;
2970 /*********************************************************************
2971 * _stat32 (MSVCR100.@)
2973 int CDECL MSVCRT__stat32(const char *path, struct MSVCRT__stat32 *buf)
2975 int ret;
2976 struct MSVCRT__stat64 buf64;
2978 ret = MSVCRT_stat64(path, &buf64);
2979 if (!ret)
2980 msvcrt_stat64_to_stat32(&buf64, buf);
2981 return ret;
2984 /*********************************************************************
2985 * _stat32i64 (MSVCR100.@)
2987 int CDECL MSVCRT__stat32i64(const char *path, struct MSVCRT__stat32i64 *buf)
2989 int ret;
2990 struct MSVCRT__stat64 buf64;
2992 ret = MSVCRT_stat64(path, &buf64);
2993 if (!ret)
2994 msvcrt_stat64_to_stat32i64(&buf64, buf);
2995 return ret;
2998 /*********************************************************************
2999 * _stat64i32 (MSVCR100.@)
3001 int CDECL MSVCRT__stat64i32(const char* path, struct MSVCRT__stat64i32 *buf)
3003 int ret;
3004 struct MSVCRT__stat64 buf64;
3006 ret = MSVCRT_stat64(path, &buf64);
3007 if (!ret)
3008 msvcrt_stat64_to_stat64i32(&buf64, buf);
3009 return ret;
3012 /*********************************************************************
3013 * _wstat64 (MSVCRT.@)
3015 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
3017 DWORD dw;
3018 WIN32_FILE_ATTRIBUTE_DATA hfi;
3019 unsigned short mode = ALL_S_IREAD;
3020 int plen;
3022 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
3024 plen = strlenW(path);
3025 while (plen && path[plen-1]==' ')
3026 plen--;
3028 if(plen && (plen<2 || path[plen-2]!=':') &&
3029 (path[plen-1]==':' || path[plen-1]=='\\' || path[plen-1]=='/'))
3031 *MSVCRT__errno() = MSVCRT_ENOENT;
3032 return -1;
3035 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
3037 TRACE("failed (%d)\n",GetLastError());
3038 *MSVCRT__errno() = MSVCRT_ENOENT;
3039 return -1;
3042 memset(buf,0,sizeof(struct MSVCRT__stat64));
3044 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3045 if (MSVCRT_iswalpha(*path))
3046 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
3047 else
3048 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
3050 /* Dir, or regular file? */
3051 if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
3052 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
3053 else
3055 mode |= MSVCRT__S_IFREG;
3056 /* executable? */
3057 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3059 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
3060 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
3061 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3062 mode |= ALL_S_IEXEC;
3066 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
3067 mode |= ALL_S_IWRITE;
3069 buf->st_mode = mode;
3070 buf->st_nlink = 1;
3071 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3072 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
3073 buf->st_atime = dw;
3074 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
3075 buf->st_mtime = buf->st_ctime = dw;
3076 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
3077 (int)(buf->st_size >> 32),(int)buf->st_size,
3078 (int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
3079 return 0;
3082 /*********************************************************************
3083 * _wstati64 (MSVCRT.@)
3085 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
3087 int ret;
3088 struct MSVCRT__stat64 buf64;
3090 ret = MSVCRT__wstat64(path, &buf64);
3091 if (!ret)
3092 msvcrt_stat64_to_stati64(&buf64, buf);
3093 return ret;
3096 /*********************************************************************
3097 * _wstat (MSVCRT.@)
3099 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
3101 int ret;
3102 struct MSVCRT__stat64 buf64;
3104 ret = MSVCRT__wstat64( path, &buf64 );
3105 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3106 return ret;
3109 /*********************************************************************
3110 * _wstat32 (MSVCR100.@)
3112 int CDECL MSVCRT__wstat32(const MSVCRT_wchar_t *path, struct MSVCRT__stat32 *buf)
3114 int ret;
3115 struct MSVCRT__stat64 buf64;
3117 ret = MSVCRT__wstat64(path, &buf64);
3118 if (!ret)
3119 msvcrt_stat64_to_stat32(&buf64, buf);
3120 return ret;
3123 /*********************************************************************
3124 * _wstat32i64 (MSVCR100.@)
3126 int CDECL MSVCRT__wstat32i64(const MSVCRT_wchar_t *path, struct MSVCRT__stat32i64 *buf)
3128 int ret;
3129 struct MSVCRT__stat64 buf64;
3131 ret = MSVCRT__wstat64(path, &buf64);
3132 if (!ret)
3133 msvcrt_stat64_to_stat32i64(&buf64, buf);
3134 return ret;
3137 /*********************************************************************
3138 * _wstat64i32 (MSVCR100.@)
3140 int CDECL MSVCRT__wstat64i32(const MSVCRT_wchar_t *path, struct MSVCRT__stat64i32 *buf)
3142 int ret;
3143 struct MSVCRT__stat64 buf64;
3145 ret = MSVCRT__wstat64(path, &buf64);
3146 if (!ret)
3147 msvcrt_stat64_to_stat64i32(&buf64, buf);
3148 return ret;
3151 /*********************************************************************
3152 * _tell (MSVCRT.@)
3154 MSVCRT_long CDECL MSVCRT__tell(int fd)
3156 return MSVCRT__lseek(fd, 0, MSVCRT_SEEK_CUR);
3159 /*********************************************************************
3160 * _telli64 (MSVCRT.@)
3162 __int64 CDECL _telli64(int fd)
3164 return MSVCRT__lseeki64(fd, 0, MSVCRT_SEEK_CUR);
3167 /*********************************************************************
3168 * _tempnam (MSVCRT.@)
3170 char * CDECL MSVCRT__tempnam(const char *dir, const char *prefix)
3172 char tmpbuf[MAX_PATH];
3173 const char *tmp_dir = MSVCRT_getenv("TMP");
3175 if (tmp_dir) dir = tmp_dir;
3177 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
3178 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
3180 TRACE("got name (%s)\n",tmpbuf);
3181 DeleteFileA(tmpbuf);
3182 return MSVCRT__strdup(tmpbuf);
3184 TRACE("failed (%d)\n",GetLastError());
3185 return NULL;
3188 /*********************************************************************
3189 * _wtempnam (MSVCRT.@)
3191 MSVCRT_wchar_t * CDECL MSVCRT__wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
3193 static const MSVCRT_wchar_t tmpW[] = {'T','M','P',0};
3194 MSVCRT_wchar_t tmpbuf[MAX_PATH];
3195 const MSVCRT_wchar_t *tmp_dir = MSVCRT__wgetenv(tmpW);
3197 if (tmp_dir) dir = tmp_dir;
3199 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
3200 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3202 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
3203 DeleteFileW(tmpbuf);
3204 return MSVCRT__wcsdup(tmpbuf);
3206 TRACE("failed (%d)\n",GetLastError());
3207 return NULL;
3210 /*********************************************************************
3211 * _umask (MSVCRT.@)
3213 int CDECL MSVCRT__umask(int umask)
3215 int old_umask = MSVCRT_umask;
3216 TRACE("(%d)\n",umask);
3217 MSVCRT_umask = umask;
3218 return old_umask;
3221 /*********************************************************************
3222 * _utime64 (MSVCRT.@)
3224 int CDECL _utime64(const char* path, struct MSVCRT___utimbuf64 *t)
3226 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3228 if (fd > 0)
3230 int retVal = _futime64(fd, t);
3231 MSVCRT__close(fd);
3232 return retVal;
3234 return -1;
3237 /*********************************************************************
3238 * _utime32 (MSVCRT.@)
3240 int CDECL _utime32(const char* path, struct MSVCRT___utimbuf32 *t)
3242 if (t)
3244 struct MSVCRT___utimbuf64 t64;
3245 t64.actime = t->actime;
3246 t64.modtime = t->modtime;
3247 return _utime64( path, &t64 );
3249 else
3250 return _utime64( path, NULL );
3253 /*********************************************************************
3254 * _utime (MSVCRT.@)
3256 #ifdef _WIN64
3257 int CDECL _utime(const char* path, struct MSVCRT___utimbuf64 *t)
3259 return _utime64( path, t );
3261 #else
3262 int CDECL _utime(const char* path, struct MSVCRT___utimbuf32 *t)
3264 return _utime32( path, t );
3266 #endif
3268 /*********************************************************************
3269 * _wutime64 (MSVCRT.@)
3271 int CDECL _wutime64(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3273 int fd = MSVCRT__wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
3275 if (fd > 0)
3277 int retVal = _futime64(fd, t);
3278 MSVCRT__close(fd);
3279 return retVal;
3281 return -1;
3284 /*********************************************************************
3285 * _wutime32 (MSVCRT.@)
3287 int CDECL _wutime32(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3289 if (t)
3291 struct MSVCRT___utimbuf64 t64;
3292 t64.actime = t->actime;
3293 t64.modtime = t->modtime;
3294 return _wutime64( path, &t64 );
3296 else
3297 return _wutime64( path, NULL );
3300 /*********************************************************************
3301 * _wutime (MSVCRT.@)
3303 #ifdef _WIN64
3304 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf64 *t)
3306 return _wutime64( path, t );
3308 #else
3309 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT___utimbuf32 *t)
3311 return _wutime32( path, t );
3313 #endif
3315 /*********************************************************************
3316 * _write (MSVCRT.@)
3318 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
3320 DWORD num_written;
3321 ioinfo *info = get_ioinfo(fd);
3322 HANDLE hand = info->handle;
3324 /* Don't trace small writes, it gets *very* annoying */
3325 #if 0
3326 if (count > 32)
3327 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
3328 #endif
3329 if (hand == INVALID_HANDLE_VALUE)
3331 *MSVCRT__errno() = MSVCRT_EBADF;
3332 release_ioinfo(info);
3333 return -1;
3336 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
3338 *MSVCRT__errno() = MSVCRT_EINVAL;
3339 release_ioinfo(info);
3340 return -1;
3343 /* If appending, go to EOF */
3344 if (info->wxflag & WX_APPEND)
3345 MSVCRT__lseek(fd, 0, FILE_END);
3347 if (!(info->wxflag & WX_TEXT))
3349 if (WriteFile(hand, buf, count, &num_written, NULL)
3350 && (num_written == count))
3352 release_ioinfo(info);
3353 return num_written;
3355 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
3356 hand, GetLastError());
3357 *MSVCRT__errno() = MSVCRT_ENOSPC;
3359 else
3361 unsigned int i, j, nr_lf, size;
3362 char *p = NULL;
3363 const char *q;
3364 const char *s = buf, *buf_start = buf;
3366 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
3368 /* find number of \n */
3369 for (nr_lf=0, i=0; i<count; i++)
3370 if (s[i] == '\n')
3371 nr_lf++;
3372 if (nr_lf)
3374 size = count+nr_lf;
3375 if ((q = p = MSVCRT_malloc(size)))
3377 for (s = buf, i = 0, j = 0; i < count; i++)
3379 if (s[i] == '\n')
3380 p[j++] = '\r';
3381 p[j++] = s[i];
3384 else
3386 FIXME("Malloc failed\n");
3387 nr_lf = 0;
3388 size = count;
3389 q = buf;
3392 else
3394 size = count;
3395 q = buf;
3398 else if (info->exflag & EF_UTF16)
3400 for (nr_lf=0, i=0; i<count; i+=2)
3401 if (s[i]=='\n' && s[i+1]==0)
3402 nr_lf += 2;
3403 if (nr_lf)
3405 size = count+nr_lf;
3406 if ((q = p = MSVCRT_malloc(size)))
3408 for (s=buf, i=0, j=0; i<count; i++)
3410 if (s[i]=='\n' && s[i+1]==0)
3412 p[j++] = '\r';
3413 p[j++] = 0;
3415 p[j++] = s[i++];
3416 p[j++] = s[i];
3419 else
3421 FIXME("Malloc failed\n");
3422 nr_lf = 0;
3423 size = count;
3424 q = buf;
3427 else
3429 size = count;
3430 q = buf;
3433 else
3435 DWORD conv_len;
3437 for(nr_lf=0, i=0; i<count; i+=2)
3438 if (s[i]=='\n' && s[i+1]==0)
3439 nr_lf++;
3441 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
3442 if(!conv_len) {
3443 msvcrt_set_errno(GetLastError());
3444 MSVCRT_free(p);
3445 release_ioinfo(info);
3446 return -1;
3449 size = conv_len+nr_lf;
3450 if((p = MSVCRT_malloc(count+nr_lf*2+size)))
3452 for (s=buf, i=0, j=0; i<count; i++)
3454 if (s[i]=='\n' && s[i+1]==0)
3456 p[j++] = '\r';
3457 p[j++] = 0;
3459 p[j++] = s[i++];
3460 p[j++] = s[i];
3462 q = p+count+nr_lf*2;
3463 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
3464 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
3466 else
3468 FIXME("Malloc failed\n");
3469 nr_lf = 0;
3470 size = count;
3471 q = buf;
3475 if (!WriteFile(hand, q, size, &num_written, NULL))
3476 num_written = -1;
3477 release_ioinfo(info);
3478 if(p)
3479 MSVCRT_free(p);
3480 if (num_written != size)
3482 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3483 fd, hand, GetLastError(), num_written);
3484 *MSVCRT__errno() = MSVCRT_ENOSPC;
3485 return s - buf_start;
3487 return count;
3490 release_ioinfo(info);
3491 return -1;
3494 /*********************************************************************
3495 * _putw (MSVCRT.@)
3497 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
3499 int len;
3501 MSVCRT__lock_file(file);
3502 len = MSVCRT__write(file->_file, &val, sizeof(val));
3503 if (len == sizeof(val)) {
3504 MSVCRT__unlock_file(file);
3505 return val;
3508 file->_flag |= MSVCRT__IOERR;
3509 MSVCRT__unlock_file(file);
3510 return MSVCRT_EOF;
3513 /*********************************************************************
3514 * fclose (MSVCRT.@)
3516 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
3518 int ret;
3520 MSVCRT__lock_file(file);
3521 ret = MSVCRT__fclose_nolock(file);
3522 MSVCRT__unlock_file(file);
3524 return ret;
3527 /*********************************************************************
3528 * _fclose_nolock (MSVCRT.@)
3530 int CDECL MSVCRT__fclose_nolock(MSVCRT_FILE* file)
3532 int r, flag;
3534 flag = file->_flag;
3535 MSVCRT_free(file->_tmpfname);
3536 file->_tmpfname = NULL;
3537 /* flush stdio buffers */
3538 if(file->_flag & MSVCRT__IOWRT)
3539 MSVCRT__fflush_nolock(file);
3540 if(file->_flag & MSVCRT__IOMYBUF)
3541 MSVCRT_free(file->_base);
3543 r=MSVCRT__close(file->_file);
3544 file->_flag = 0;
3546 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
3549 /*********************************************************************
3550 * feof (MSVCRT.@)
3552 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
3554 return file->_flag & MSVCRT__IOEOF;
3557 /*********************************************************************
3558 * ferror (MSVCRT.@)
3560 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
3562 return file->_flag & MSVCRT__IOERR;
3565 /*********************************************************************
3566 * _filbuf (MSVCRT.@)
3568 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
3570 unsigned char c;
3572 if(file->_flag & MSVCRT__IOSTRG)
3573 return MSVCRT_EOF;
3575 /* Allocate buffer if needed */
3576 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3577 msvcrt_alloc_buffer(file);
3579 if(!(file->_flag & MSVCRT__IOREAD)) {
3580 if(file->_flag & MSVCRT__IORW)
3581 file->_flag |= MSVCRT__IOREAD;
3582 else
3583 return MSVCRT_EOF;
3586 if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3587 int r;
3588 if ((r = MSVCRT__read(file->_file,&c,1)) != 1) {
3589 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3590 return MSVCRT_EOF;
3593 return c;
3594 } else {
3595 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
3596 if(file->_cnt<=0) {
3597 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
3598 file->_cnt = 0;
3599 return MSVCRT_EOF;
3602 file->_cnt--;
3603 file->_ptr = file->_base+1;
3604 c = *(unsigned char *)file->_base;
3605 return c;
3609 /*********************************************************************
3610 * fgetc (MSVCRT.@)
3612 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
3614 int ret;
3616 MSVCRT__lock_file(file);
3617 ret = MSVCRT__fgetc_nolock(file);
3618 MSVCRT__unlock_file(file);
3620 return ret;
3623 /*********************************************************************
3624 * _fgetc_nolock (MSVCRT.@)
3626 int CDECL MSVCRT__fgetc_nolock(MSVCRT_FILE* file)
3628 unsigned char *i;
3629 unsigned int j;
3631 if (file->_cnt>0) {
3632 file->_cnt--;
3633 i = (unsigned char *)file->_ptr++;
3634 j = *i;
3635 } else
3636 j = MSVCRT__filbuf(file);
3638 return j;
3641 /*********************************************************************
3642 * _fgetchar (MSVCRT.@)
3644 int CDECL MSVCRT__fgetchar(void)
3646 return MSVCRT_fgetc(MSVCRT_stdin);
3649 /*********************************************************************
3650 * fgets (MSVCRT.@)
3652 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
3654 int cc = MSVCRT_EOF;
3655 char * buf_start = s;
3657 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3658 file,file->_file,s,size);
3660 MSVCRT__lock_file(file);
3662 while ((size >1) && (cc = MSVCRT__fgetc_nolock(file)) != MSVCRT_EOF && cc != '\n')
3664 *s++ = (char)cc;
3665 size --;
3667 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
3669 TRACE(":nothing read\n");
3670 MSVCRT__unlock_file(file);
3671 return NULL;
3673 if ((cc != MSVCRT_EOF) && (size > 1))
3674 *s++ = cc;
3675 *s = '\0';
3676 TRACE(":got %s\n", debugstr_a(buf_start));
3677 MSVCRT__unlock_file(file);
3678 return buf_start;
3681 /*********************************************************************
3682 * fgetwc (MSVCRT.@)
3684 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
3686 MSVCRT_wint_t ret;
3688 MSVCRT__lock_file(file);
3689 ret = MSVCRT__fgetwc_nolock(file);
3690 MSVCRT__unlock_file(file);
3692 return ret;
3695 /*********************************************************************
3696 * _fgetwc_nolock (MSVCRT.@)
3698 MSVCRT_wint_t CDECL MSVCRT__fgetwc_nolock(MSVCRT_FILE* file)
3700 MSVCRT_wint_t ret;
3701 int ch;
3703 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3704 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3705 char *p;
3707 for(p=(char*)&ret; (MSVCRT_wint_t*)p<&ret+1; p++) {
3708 ch = MSVCRT__fgetc_nolock(file);
3709 if(ch == MSVCRT_EOF) {
3710 ret = MSVCRT_WEOF;
3711 break;
3713 *p = (char)ch;
3715 }else {
3716 char mbs[MSVCRT_MB_LEN_MAX];
3717 int len = 0;
3719 ch = MSVCRT__fgetc_nolock(file);
3720 if(ch != MSVCRT_EOF) {
3721 mbs[0] = (char)ch;
3722 if(MSVCRT_isleadbyte((unsigned char)mbs[0])) {
3723 ch = MSVCRT__fgetc_nolock(file);
3724 if(ch != MSVCRT_EOF) {
3725 mbs[1] = (char)ch;
3726 len = 2;
3728 }else {
3729 len = 1;
3733 if(!len || MSVCRT_mbtowc(&ret, mbs, len)==-1)
3734 ret = MSVCRT_WEOF;
3737 return ret;
3740 /*********************************************************************
3741 * _getw (MSVCRT.@)
3743 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
3745 char *ch;
3746 int i, k;
3747 unsigned int j;
3748 ch = (char *)&i;
3750 MSVCRT__lock_file(file);
3751 for (j=0; j<sizeof(int); j++) {
3752 k = MSVCRT__fgetc_nolock(file);
3753 if (k == MSVCRT_EOF) {
3754 file->_flag |= MSVCRT__IOEOF;
3755 MSVCRT__unlock_file(file);
3756 return MSVCRT_EOF;
3758 ch[j] = k;
3761 MSVCRT__unlock_file(file);
3762 return i;
3765 /*********************************************************************
3766 * getwc (MSVCRT.@)
3768 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
3770 return MSVCRT_fgetwc(file);
3773 /*********************************************************************
3774 * _fgetwchar (MSVCRT.@)
3776 MSVCRT_wint_t CDECL MSVCRT__fgetwchar(void)
3778 return MSVCRT_fgetwc(MSVCRT_stdin);
3781 /*********************************************************************
3782 * getwchar (MSVCRT.@)
3784 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
3786 return MSVCRT__fgetwchar();
3789 /*********************************************************************
3790 * fgetws (MSVCRT.@)
3792 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
3794 MSVCRT_wint_t cc = MSVCRT_WEOF;
3795 MSVCRT_wchar_t * buf_start = s;
3797 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3798 file,file->_file,s,size);
3800 MSVCRT__lock_file(file);
3802 while ((size >1) && (cc = MSVCRT__fgetwc_nolock(file)) != MSVCRT_WEOF && cc != '\n')
3804 *s++ = cc;
3805 size --;
3807 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3809 TRACE(":nothing read\n");
3810 MSVCRT__unlock_file(file);
3811 return NULL;
3813 if ((cc != MSVCRT_WEOF) && (size > 1))
3814 *s++ = cc;
3815 *s = 0;
3816 TRACE(":got %s\n", debugstr_w(buf_start));
3817 MSVCRT__unlock_file(file);
3818 return buf_start;
3821 /*********************************************************************
3822 * _flsbuf (MSVCRT.@)
3824 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
3826 /* Flush output buffer */
3827 if(!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
3828 msvcrt_alloc_buffer(file);
3831 if(!(file->_flag & MSVCRT__IOWRT)) {
3832 if(!(file->_flag & MSVCRT__IORW)) {
3833 file->_flag |= MSVCRT__IOERR;
3834 return MSVCRT_EOF;
3836 file->_flag |= MSVCRT__IOWRT;
3838 if(file->_flag & MSVCRT__IOREAD) {
3839 if(!(file->_flag & MSVCRT__IOEOF)) {
3840 file->_flag |= MSVCRT__IOERR;
3841 return MSVCRT_EOF;
3843 file->_cnt = 0;
3844 file->_ptr = file->_base;
3845 file->_flag &= ~(MSVCRT__IOREAD | MSVCRT__IOEOF);
3848 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
3849 int res = 0;
3851 if(file->_cnt <= 0) {
3852 res = msvcrt_flush_buffer(file);
3853 if(res)
3854 return res;
3855 file->_flag |= MSVCRT__IOWRT;
3856 file->_cnt=file->_bufsiz;
3858 *file->_ptr++ = c;
3859 file->_cnt--;
3860 return c&0xff;
3861 } else {
3862 unsigned char cc=c;
3863 int len;
3864 /* set _cnt to 0 for unbuffered FILEs */
3865 file->_cnt = 0;
3866 len = MSVCRT__write(file->_file, &cc, 1);
3867 if (len == 1)
3868 return c & 0xff;
3869 file->_flag |= MSVCRT__IOERR;
3870 return MSVCRT_EOF;
3874 /*********************************************************************
3875 * fwrite (MSVCRT.@)
3877 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3879 MSVCRT_size_t ret;
3881 MSVCRT__lock_file(file);
3882 ret = MSVCRT__fwrite_nolock(ptr, size, nmemb, file);
3883 MSVCRT__unlock_file(file);
3885 return ret;
3888 /*********************************************************************
3889 * _fwrite_nolock (MSVCRT.@)
3891 MSVCRT_size_t CDECL MSVCRT__fwrite_nolock(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
3893 MSVCRT_size_t wrcnt=size * nmemb;
3894 int written = 0;
3895 if (size == 0)
3896 return 0;
3898 while(wrcnt) {
3899 if(file->_cnt < 0) {
3900 WARN("negative file->_cnt value in %p\n", file);
3901 file->_flag |= MSVCRT__IOERR;
3902 break;
3903 } else if(file->_cnt) {
3904 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3905 memcpy(file->_ptr, ptr, pcnt);
3906 file->_cnt -= pcnt;
3907 file->_ptr += pcnt;
3908 written += pcnt;
3909 wrcnt -= pcnt;
3910 ptr = (const char*)ptr + pcnt;
3911 } else if((file->_flag & MSVCRT__IONBF)
3912 || ((file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
3913 || (!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
3914 MSVCRT_size_t pcnt;
3915 int bufsiz;
3917 if(file->_flag & MSVCRT__IONBF)
3918 bufsiz = 1;
3919 else if(!(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
3920 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
3921 else
3922 bufsiz = file->_bufsiz;
3924 pcnt = (wrcnt / bufsiz) * bufsiz;
3926 if(msvcrt_flush_buffer(file) == MSVCRT_EOF)
3927 break;
3929 if(MSVCRT__write(file->_file, ptr, pcnt) <= 0) {
3930 file->_flag |= MSVCRT__IOERR;
3931 break;
3933 written += pcnt;
3934 wrcnt -= pcnt;
3935 ptr = (const char*)ptr + pcnt;
3936 } else {
3937 if(MSVCRT__flsbuf(*(const char*)ptr, file) == MSVCRT_EOF)
3938 break;
3939 written++;
3940 wrcnt--;
3941 ptr = (const char*)ptr + 1;
3945 return written / size;
3948 /*********************************************************************
3949 * fputwc (MSVCRT.@)
3951 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3953 MSVCRT_wint_t ret;
3955 MSVCRT__lock_file(file);
3956 ret = MSVCRT__fputwc_nolock(wc, file);
3957 MSVCRT__unlock_file(file);
3959 return ret;
3962 /*********************************************************************
3963 * _fputwc_nolock (MSVCRT.@)
3965 MSVCRT_wint_t CDECL MSVCRT__fputwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE* file)
3967 MSVCRT_wchar_t mwc=wc;
3968 ioinfo *fdinfo;
3969 MSVCRT_wint_t ret;
3971 fdinfo = get_ioinfo_nolock(file->_file);
3973 if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
3974 char buf[MSVCRT_MB_LEN_MAX];
3975 int char_len;
3977 char_len = MSVCRT_wctomb(buf, mwc);
3978 if(char_len!=-1 && MSVCRT__fwrite_nolock(buf, char_len, 1, file)==1)
3979 ret = wc;
3980 else
3981 ret = MSVCRT_WEOF;
3982 }else if(MSVCRT__fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
3983 ret = wc;
3984 }else {
3985 ret = MSVCRT_WEOF;
3988 return ret;
3991 /*********************************************************************
3992 * _fputwchar (MSVCRT.@)
3994 MSVCRT_wint_t CDECL MSVCRT__fputwchar(MSVCRT_wint_t wc)
3996 return MSVCRT_fputwc(wc, MSVCRT_stdout);
3999 /*********************************************************************
4000 * _wfsopen (MSVCRT.@)
4002 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
4004 MSVCRT_FILE* file;
4005 int open_flags, stream_flags, fd;
4007 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
4009 /* map mode string to open() flags. "man fopen" for possibilities. */
4010 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4011 return NULL;
4013 LOCK_FILES();
4014 fd = MSVCRT__wsopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4015 if (fd < 0)
4016 file = NULL;
4017 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
4018 != -1)
4019 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
4020 else if (file)
4022 file->_flag = 0;
4023 file = NULL;
4026 TRACE(":got (%p)\n",file);
4027 if (fd >= 0 && !file)
4028 MSVCRT__close(fd);
4029 UNLOCK_FILES();
4030 return file;
4033 /*********************************************************************
4034 * _fsopen (MSVCRT.@)
4036 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
4038 MSVCRT_FILE *ret;
4039 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4041 if (path && !(pathW = msvcrt_wstrdupa(path))) {
4042 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4043 *MSVCRT__errno() = MSVCRT_EINVAL;
4044 return NULL;
4046 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4048 MSVCRT_free(pathW);
4049 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
4050 *MSVCRT__errno() = MSVCRT_EINVAL;
4051 return NULL;
4054 ret = MSVCRT__wfsopen(pathW, modeW, share);
4056 MSVCRT_free(pathW);
4057 MSVCRT_free(modeW);
4058 return ret;
4061 /*********************************************************************
4062 * fopen (MSVCRT.@)
4064 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
4066 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
4069 /*********************************************************************
4070 * fopen_s (MSVCRT.@)
4072 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
4073 const char *filename, const char *mode)
4075 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4076 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4077 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4079 *pFile = MSVCRT_fopen(filename, mode);
4081 if(!*pFile)
4082 return *MSVCRT__errno();
4083 return 0;
4086 /*********************************************************************
4087 * _wfopen (MSVCRT.@)
4089 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
4091 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
4094 /*********************************************************************
4095 * _wfopen_s (MSVCRT.@)
4097 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
4098 const MSVCRT_wchar_t *mode)
4100 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4101 if (!MSVCRT_CHECK_PMT(filename != NULL)) return MSVCRT_EINVAL;
4102 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4104 *pFile = MSVCRT__wfopen(filename, mode);
4106 if(!*pFile)
4107 return *MSVCRT__errno();
4108 return 0;
4111 /*********************************************************************
4112 * fputc (MSVCRT.@)
4114 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
4116 int ret;
4118 MSVCRT__lock_file(file);
4119 ret = MSVCRT__fputc_nolock(c, file);
4120 MSVCRT__unlock_file(file);
4122 return ret;
4125 /*********************************************************************
4126 * _fputc_nolock (MSVCRT.@)
4128 int CDECL MSVCRT__fputc_nolock(int c, MSVCRT_FILE* file)
4130 int res;
4132 if(file->_cnt>0) {
4133 *file->_ptr++=c;
4134 file->_cnt--;
4135 if (c == '\n')
4137 res = msvcrt_flush_buffer(file);
4138 return res ? res : c;
4140 else {
4141 return c & 0xff;
4143 } else {
4144 res = MSVCRT__flsbuf(c, file);
4145 return res;
4149 /*********************************************************************
4150 * _fputchar (MSVCRT.@)
4152 int CDECL MSVCRT__fputchar(int c)
4154 return MSVCRT_fputc(c, MSVCRT_stdout);
4157 /*********************************************************************
4158 * fread (MSVCRT.@)
4160 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4162 MSVCRT_size_t ret;
4164 MSVCRT__lock_file(file);
4165 ret = MSVCRT__fread_nolock(ptr, size, nmemb, file);
4166 MSVCRT__unlock_file(file);
4168 return ret;
4171 /*********************************************************************
4172 * _fread_nolock (MSVCRT.@)
4174 MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
4176 MSVCRT_size_t rcnt=size * nmemb;
4177 MSVCRT_size_t read=0;
4178 MSVCRT_size_t pread=0;
4180 if(!rcnt)
4181 return 0;
4183 /* first buffered data */
4184 if(file->_cnt>0) {
4185 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4186 memcpy(ptr, file->_ptr, pcnt);
4187 file->_cnt -= pcnt;
4188 file->_ptr += pcnt;
4189 read += pcnt ;
4190 rcnt -= pcnt ;
4191 ptr = (char*)ptr + pcnt;
4192 } else if(!(file->_flag & MSVCRT__IOREAD )) {
4193 if(file->_flag & MSVCRT__IORW) {
4194 file->_flag |= MSVCRT__IOREAD;
4195 } else {
4196 return 0;
4200 if(rcnt>0 && !(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF)))
4201 msvcrt_alloc_buffer(file);
4203 while(rcnt>0)
4205 int i;
4206 if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
4207 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
4208 file->_ptr = file->_base;
4209 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
4210 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4211 if (i > 0 && i < file->_cnt) {
4212 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4213 file->_flag &= ~MSVCRT__IOEOF;
4215 if (i > 0) {
4216 memcpy(ptr, file->_ptr, i);
4217 file->_cnt -= i;
4218 file->_ptr += i;
4220 } else if (rcnt > INT_MAX) {
4221 i = MSVCRT__read(file->_file, ptr, INT_MAX);
4222 } else if (rcnt < MSVCRT_BUFSIZ) {
4223 i = MSVCRT__read(file->_file, ptr, rcnt);
4224 } else {
4225 i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
4227 pread += i;
4228 rcnt -= i;
4229 ptr = (char *)ptr+i;
4230 /* expose feof condition in the flags
4231 * MFC tests file->_flag for feof, and doesn't call feof())
4233 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4234 file->_flag |= MSVCRT__IOEOF;
4235 else if (i == -1)
4237 file->_flag |= MSVCRT__IOERR;
4238 pread = 0;
4239 rcnt = 0;
4241 if (i < 1) break;
4243 read+=pread;
4244 return read / size;
4248 /*********************************************************************
4249 * fread_s (MSVCR80.@)
4251 MSVCRT_size_t CDECL MSVCRT_fread_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4252 MSVCRT_size_t count, MSVCRT_FILE *stream)
4254 MSVCRT_size_t ret;
4256 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4257 if(buf && buf_size)
4258 memset(buf, 0, buf_size);
4259 return 0;
4261 if(!elem_size || !count) return 0;
4263 MSVCRT__lock_file(stream);
4264 ret = MSVCRT__fread_nolock_s(buf, buf_size, elem_size, count, stream);
4265 MSVCRT__unlock_file(stream);
4267 return ret;
4270 /*********************************************************************
4271 * _fread_nolock_s (MSVCR80.@)
4273 MSVCRT_size_t CDECL MSVCRT__fread_nolock_s(void *buf, MSVCRT_size_t buf_size, MSVCRT_size_t elem_size,
4274 MSVCRT_size_t count, MSVCRT_FILE *stream)
4276 size_t bytes_left, buf_pos;
4278 TRACE("(%p %lu %lu %lu %p)\n", buf, buf_size, elem_size, count, stream);
4280 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4281 if(buf && buf_size)
4282 memset(buf, 0, buf_size);
4283 return 0;
4285 if(!elem_size || !count) return 0;
4286 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4287 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX/count >= elem_size)) return 0;
4289 bytes_left = elem_size*count;
4290 buf_pos = 0;
4291 while(bytes_left) {
4292 if(stream->_cnt > 0) {
4293 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4295 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, MSVCRT_ERANGE)) {
4296 memset(buf, 0, buf_size);
4297 return 0;
4300 MSVCRT__fread_nolock((char*)buf+buf_pos, 1, size, stream);
4301 buf_pos += size;
4302 bytes_left -= size;
4303 }else {
4304 int c = MSVCRT__filbuf(stream);
4306 if(c == MSVCRT_EOF)
4307 break;
4309 if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, MSVCRT_ERANGE)) {
4310 memset(buf, 0, buf_size);
4311 return 0;
4314 ((char*)buf)[buf_pos++] = c;
4315 bytes_left--;
4319 return buf_pos/elem_size;
4322 /*********************************************************************
4323 * _wfreopen (MSVCRT.@)
4326 MSVCRT_FILE* CDECL MSVCRT__wfreopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4328 int open_flags, stream_flags, fd;
4330 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4332 LOCK_FILES();
4333 if (!file || ((fd = file->_file) < 0))
4334 file = NULL;
4335 else
4337 MSVCRT_fclose(file);
4338 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4339 file = NULL;
4340 else if((fd = MSVCRT__wopen(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE)) < 0)
4341 file = NULL;
4342 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4344 file->_flag = 0;
4345 file = NULL;
4348 UNLOCK_FILES();
4349 return file;
4352 /*********************************************************************
4353 * _wfreopen_s (MSVCRT.@)
4355 int CDECL MSVCRT__wfreopen_s(MSVCRT_FILE** pFile,
4356 const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, MSVCRT_FILE* file)
4358 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4359 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4360 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4361 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4363 *pFile = MSVCRT__wfreopen(path, mode, file);
4365 if(!*pFile)
4366 return *MSVCRT__errno();
4367 return 0;
4370 /*********************************************************************
4371 * freopen (MSVCRT.@)
4374 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode, MSVCRT_FILE* file)
4376 MSVCRT_FILE *ret;
4377 MSVCRT_wchar_t *pathW = NULL, *modeW = NULL;
4379 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
4380 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4382 MSVCRT_free(pathW);
4383 return NULL;
4386 ret = MSVCRT__wfreopen(pathW, modeW, file);
4388 MSVCRT_free(pathW);
4389 MSVCRT_free(modeW);
4390 return ret;
4393 /*********************************************************************
4394 * freopen_s (MSVCRT.@)
4396 int CDECL MSVCRT_freopen_s(MSVCRT_FILE** pFile,
4397 const char *path, const char *mode, MSVCRT_FILE* file)
4399 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return MSVCRT_EINVAL;
4400 if (!MSVCRT_CHECK_PMT(path != NULL)) return MSVCRT_EINVAL;
4401 if (!MSVCRT_CHECK_PMT(mode != NULL)) return MSVCRT_EINVAL;
4402 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4404 *pFile = MSVCRT_freopen(path, mode, file);
4406 if(!*pFile)
4407 return *MSVCRT__errno();
4408 return 0;
4411 /*********************************************************************
4412 * fsetpos (MSVCRT.@)
4414 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4416 int ret;
4418 MSVCRT__lock_file(file);
4419 msvcrt_flush_buffer(file);
4421 /* Reset direction of i/o */
4422 if(file->_flag & MSVCRT__IORW) {
4423 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
4426 ret = (MSVCRT__lseeki64(file->_file,*pos,MSVCRT_SEEK_SET) == -1) ? -1 : 0;
4427 MSVCRT__unlock_file(file);
4428 return ret;
4431 /*********************************************************************
4432 * _ftelli64 (MSVCRT.@)
4434 __int64 CDECL MSVCRT__ftelli64(MSVCRT_FILE* file)
4436 __int64 ret;
4438 MSVCRT__lock_file(file);
4439 ret = MSVCRT__ftelli64_nolock(file);
4440 MSVCRT__unlock_file(file);
4442 return ret;
4445 /*********************************************************************
4446 * _ftelli64_nolock (MSVCRT.@)
4448 __int64 CDECL MSVCRT__ftelli64_nolock(MSVCRT_FILE* file)
4450 __int64 pos;
4452 pos = _telli64(file->_file);
4453 if(pos == -1)
4454 return -1;
4455 if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) {
4456 if(file->_flag & MSVCRT__IOWRT) {
4457 pos += file->_ptr - file->_base;
4459 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4460 char *p;
4462 for(p=file->_base; p<file->_ptr; p++)
4463 if(*p == '\n')
4464 pos++;
4466 } else if(!file->_cnt) { /* nothing to do */
4467 } else if(MSVCRT__lseeki64(file->_file, 0, MSVCRT_SEEK_END)==pos) {
4468 int i;
4470 pos -= file->_cnt;
4471 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4472 for(i=0; i<file->_cnt; i++)
4473 if(file->_ptr[i] == '\n')
4474 pos--;
4476 } else {
4477 char *p;
4479 if(MSVCRT__lseeki64(file->_file, pos, MSVCRT_SEEK_SET) != pos)
4480 return -1;
4482 pos -= file->_bufsiz;
4483 pos += file->_ptr - file->_base;
4485 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4486 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4487 pos--;
4489 for(p=file->_base; p<file->_ptr; p++)
4490 if(*p == '\n')
4491 pos++;
4496 return pos;
4499 /*********************************************************************
4500 * ftell (MSVCRT.@)
4502 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
4504 return MSVCRT__ftelli64(file);
4507 /*********************************************************************
4508 * _ftell_nolock (MSVCRT.@)
4510 LONG CDECL MSVCRT__ftell_nolock(MSVCRT_FILE* file)
4512 return MSVCRT__ftelli64_nolock(file);
4515 /*********************************************************************
4516 * fgetpos (MSVCRT.@)
4518 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
4520 *pos = MSVCRT__ftelli64(file);
4521 if(*pos == -1)
4522 return -1;
4523 return 0;
4526 /*********************************************************************
4527 * fputs (MSVCRT.@)
4529 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
4531 MSVCRT_size_t len = strlen(s);
4532 int ret;
4534 MSVCRT__lock_file(file);
4535 ret = MSVCRT__fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : MSVCRT_EOF;
4536 MSVCRT__unlock_file(file);
4537 return ret;
4540 /*********************************************************************
4541 * fputws (MSVCRT.@)
4543 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
4545 MSVCRT_size_t i, len = strlenW(s);
4546 BOOL tmp_buf;
4547 int ret;
4549 MSVCRT__lock_file(file);
4550 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4551 ret = MSVCRT__fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
4552 MSVCRT__unlock_file(file);
4553 return ret;
4556 tmp_buf = add_std_buffer(file);
4557 for (i=0; i<len; i++) {
4558 if(MSVCRT__fputwc_nolock(s[i], file) == MSVCRT_WEOF) {
4559 if(tmp_buf) remove_std_buffer(file);
4560 MSVCRT__unlock_file(file);
4561 return MSVCRT_WEOF;
4565 if(tmp_buf) remove_std_buffer(file);
4566 MSVCRT__unlock_file(file);
4567 return 0;
4570 /*********************************************************************
4571 * getchar (MSVCRT.@)
4573 int CDECL MSVCRT_getchar(void)
4575 return MSVCRT_fgetc(MSVCRT_stdin);
4578 /*********************************************************************
4579 * getc (MSVCRT.@)
4581 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
4583 return MSVCRT_fgetc(file);
4586 /*********************************************************************
4587 * gets (MSVCRT.@)
4589 char * CDECL MSVCRT_gets(char *buf)
4591 int cc;
4592 char * buf_start = buf;
4594 MSVCRT__lock_file(MSVCRT_stdin);
4595 for(cc = MSVCRT__fgetc_nolock(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
4596 cc = MSVCRT__fgetc_nolock(MSVCRT_stdin))
4597 if(cc != '\r') *buf++ = (char)cc;
4599 *buf = '\0';
4601 TRACE("got '%s'\n", buf_start);
4602 MSVCRT__unlock_file(MSVCRT_stdin);
4603 return buf_start;
4606 /*********************************************************************
4607 * _getws (MSVCRT.@)
4609 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
4611 MSVCRT_wint_t cc;
4612 MSVCRT_wchar_t* ws = buf;
4614 MSVCRT__lock_file(MSVCRT_stdin);
4615 for (cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
4616 cc = MSVCRT__fgetwc_nolock(MSVCRT_stdin))
4618 if (cc != '\r')
4619 *buf++ = (MSVCRT_wchar_t)cc;
4621 *buf = '\0';
4623 TRACE("got %s\n", debugstr_w(ws));
4624 MSVCRT__unlock_file(MSVCRT_stdin);
4625 return ws;
4628 /*********************************************************************
4629 * putc (MSVCRT.@)
4631 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
4633 return MSVCRT_fputc(c, file);
4636 /*********************************************************************
4637 * putchar (MSVCRT.@)
4639 int CDECL MSVCRT_putchar(int c)
4641 return MSVCRT_fputc(c, MSVCRT_stdout);
4644 /*********************************************************************
4645 * puts (MSVCRT.@)
4647 int CDECL MSVCRT_puts(const char *s)
4649 MSVCRT_size_t len = strlen(s);
4650 int ret;
4652 MSVCRT__lock_file(MSVCRT_stdout);
4653 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4654 MSVCRT__unlock_file(MSVCRT_stdout);
4655 return MSVCRT_EOF;
4658 ret = MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4659 MSVCRT__unlock_file(MSVCRT_stdout);
4660 return ret;
4663 /*********************************************************************
4664 * _putws (MSVCRT.@)
4666 int CDECL MSVCRT__putws(const MSVCRT_wchar_t *s)
4668 static const MSVCRT_wchar_t nl = '\n';
4669 MSVCRT_size_t len = strlenW(s);
4670 int ret;
4672 MSVCRT__lock_file(MSVCRT_stdout);
4673 if(MSVCRT__fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
4674 MSVCRT__unlock_file(MSVCRT_stdout);
4675 return MSVCRT_EOF;
4678 ret = MSVCRT__fwrite_nolock(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
4679 MSVCRT__unlock_file(MSVCRT_stdout);
4680 return ret;
4683 /*********************************************************************
4684 * remove (MSVCRT.@)
4686 int CDECL MSVCRT_remove(const char *path)
4688 TRACE("(%s)\n",path);
4689 if (DeleteFileA(path))
4690 return 0;
4691 TRACE(":failed (%d)\n",GetLastError());
4692 msvcrt_set_errno(GetLastError());
4693 return -1;
4696 /*********************************************************************
4697 * _wremove (MSVCRT.@)
4699 int CDECL MSVCRT__wremove(const MSVCRT_wchar_t *path)
4701 TRACE("(%s)\n",debugstr_w(path));
4702 if (DeleteFileW(path))
4703 return 0;
4704 TRACE(":failed (%d)\n",GetLastError());
4705 msvcrt_set_errno(GetLastError());
4706 return -1;
4709 /*********************************************************************
4710 * rename (MSVCRT.@)
4712 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
4714 TRACE(":from %s to %s\n",oldpath,newpath);
4715 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4716 return 0;
4717 TRACE(":failed (%d)\n",GetLastError());
4718 msvcrt_set_errno(GetLastError());
4719 return -1;
4722 /*********************************************************************
4723 * _wrename (MSVCRT.@)
4725 int CDECL MSVCRT__wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
4727 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
4728 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4729 return 0;
4730 TRACE(":failed (%d)\n",GetLastError());
4731 msvcrt_set_errno(GetLastError());
4732 return -1;
4735 /*********************************************************************
4736 * setvbuf (MSVCRT.@)
4738 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
4740 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4741 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || mode==MSVCRT__IOFBF || mode==MSVCRT__IOLBF)) return -1;
4742 if(!MSVCRT_CHECK_PMT(mode==MSVCRT__IONBF || (size>=2 && size<=INT_MAX))) return -1;
4744 MSVCRT__lock_file(file);
4746 MSVCRT__fflush_nolock(file);
4747 if(file->_flag & MSVCRT__IOMYBUF)
4748 MSVCRT_free(file->_base);
4749 file->_flag &= ~(MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF);
4750 file->_cnt = 0;
4752 if(mode == MSVCRT__IONBF) {
4753 file->_flag |= MSVCRT__IONBF;
4754 file->_base = file->_ptr = (char*)&file->_charbuf;
4755 file->_bufsiz = 2;
4756 }else if(buf) {
4757 file->_base = file->_ptr = buf;
4758 file->_flag |= MSVCRT__USERBUF;
4759 file->_bufsiz = size;
4760 }else {
4761 file->_base = file->_ptr = MSVCRT_malloc(size);
4762 if(!file->_base) {
4763 file->_bufsiz = 0;
4764 MSVCRT__unlock_file(file);
4765 return -1;
4768 file->_flag |= MSVCRT__IOMYBUF;
4769 file->_bufsiz = size;
4771 MSVCRT__unlock_file(file);
4772 return 0;
4775 /*********************************************************************
4776 * setbuf (MSVCRT.@)
4778 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
4780 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
4783 /*********************************************************************
4784 * tmpnam (MSVCRT.@)
4786 char * CDECL MSVCRT_tmpnam(char *s)
4788 char tmpstr[16];
4789 char *p;
4790 int count, size;
4792 if (!s) {
4793 thread_data_t *data = msvcrt_get_thread_data();
4795 if(!data->tmpnam_buffer)
4796 data->tmpnam_buffer = MSVCRT_malloc(MAX_PATH);
4798 s = data->tmpnam_buffer;
4801 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
4802 p = s + sprintf(s, "\\s%s.", tmpstr);
4803 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4805 size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
4806 memcpy(p, tmpstr, size);
4807 p[size] = '\0';
4808 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
4809 GetLastError() == ERROR_FILE_NOT_FOUND)
4810 break;
4812 return s;
4815 /*********************************************************************
4816 * _wtmpnam (MSVCRT.@)
4818 MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s)
4820 static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0};
4821 MSVCRT_wchar_t tmpstr[16];
4822 MSVCRT_wchar_t *p;
4823 int count, size;
4824 if (!s) {
4825 thread_data_t *data = msvcrt_get_thread_data();
4827 if(!data->wtmpnam_buffer)
4828 data->wtmpnam_buffer = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[MAX_PATH]));
4830 s = data->wtmpnam_buffer;
4833 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
4834 p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr);
4835 for (count = 0; count < MSVCRT_TMP_MAX; count++)
4837 size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr);
4838 memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t));
4839 p[size] = '\0';
4840 if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
4841 GetLastError() == ERROR_FILE_NOT_FOUND)
4842 break;
4844 return s;
4847 /*********************************************************************
4848 * tmpfile (MSVCRT.@)
4850 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
4852 char *filename = MSVCRT_tmpnam(NULL);
4853 int fd;
4854 MSVCRT_FILE* file = NULL;
4856 LOCK_FILES();
4857 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY,
4858 MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
4859 if (fd != -1 && (file = msvcrt_alloc_fp()))
4861 if (msvcrt_init_fp(file, fd, MSVCRT__IORW) == -1)
4863 file->_flag = 0;
4864 file = NULL;
4866 else file->_tmpfname = MSVCRT__strdup(filename);
4869 if(fd != -1 && !file)
4870 MSVCRT__close(fd);
4871 UNLOCK_FILES();
4872 return file;
4875 /*********************************************************************
4876 * tmpfile_s (MSVCRT.@)
4878 int CDECL MSVCRT_tmpfile_s(MSVCRT_FILE** file)
4880 if (!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EINVAL;
4882 *file = MSVCRT_tmpfile();
4883 return 0;
4886 static int puts_clbk_file_a(void *file, int len, const char *str)
4888 return MSVCRT_fwrite(str, sizeof(char), len, file);
4891 static int puts_clbk_file_w(void *file, int len, const MSVCRT_wchar_t *str)
4893 int i, ret;
4895 MSVCRT__lock_file(file);
4897 if(!(get_ioinfo_nolock(((MSVCRT_FILE*)file)->_file)->wxflag & WX_TEXT)) {
4898 ret = MSVCRT__fwrite_nolock(str, sizeof(MSVCRT_wchar_t), len, file);
4899 MSVCRT__unlock_file(file);
4900 return ret;
4903 for(i=0; i<len; i++) {
4904 if(MSVCRT__fputwc_nolock(str[i], file) == MSVCRT_WEOF) {
4905 MSVCRT__unlock_file(file);
4906 return -1;
4910 MSVCRT__unlock_file(file);
4911 return len;
4914 /*********************************************************************
4915 * vfprintf (MSVCRT.@)
4917 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4919 BOOL tmp_buf;
4920 int ret;
4922 MSVCRT__lock_file(file);
4923 tmp_buf = add_std_buffer(file);
4924 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL, 0, arg_clbk_valist, NULL, &valist);
4925 if(tmp_buf) remove_std_buffer(file);
4926 MSVCRT__unlock_file(file);
4928 return ret;
4931 /*********************************************************************
4932 * vfprintf_s (MSVCRT.@)
4934 int CDECL MSVCRT_vfprintf_s(MSVCRT_FILE* file, const char *format, __ms_va_list valist)
4936 BOOL tmp_buf;
4937 int ret;
4939 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
4941 MSVCRT__lock_file(file);
4942 tmp_buf = add_std_buffer(file);
4943 ret = pf_printf_a(puts_clbk_file_a, file, format, NULL,
4944 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, arg_clbk_valist, NULL, &valist);
4945 if(tmp_buf) remove_std_buffer(file);
4946 MSVCRT__unlock_file(file);
4948 return ret;
4951 /*********************************************************************
4952 * vfwprintf (MSVCRT.@)
4954 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4956 BOOL tmp_buf;
4957 int ret;
4959 MSVCRT__lock_file(file);
4960 tmp_buf = add_std_buffer(file);
4961 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL, 0, arg_clbk_valist, NULL, &valist);
4962 if(tmp_buf) remove_std_buffer(file);
4963 MSVCRT__unlock_file(file);
4965 return ret;
4968 /*********************************************************************
4969 * vfwprintf_s (MSVCRT.@)
4971 int CDECL MSVCRT_vfwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, __ms_va_list valist)
4973 BOOL tmp_buf;
4974 int ret;
4976 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4978 MSVCRT__lock_file(file);
4979 tmp_buf = add_std_buffer(file);
4980 ret = pf_printf_w(puts_clbk_file_w, file, format, NULL,
4981 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, arg_clbk_valist, NULL, &valist);
4982 if(tmp_buf) remove_std_buffer(file);
4983 MSVCRT__unlock_file(file);
4985 return ret;
4988 /*********************************************************************
4989 * __stdio_common_vfprintf (MSVCRT.@)
4991 int CDECL MSVCRT__stdio_common_vfprintf(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
4992 MSVCRT__locale_t locale, __ms_va_list valist)
4994 BOOL tmp_buf;
4995 int ret;
4997 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
4999 MSVCRT__lock_file(file);
5000 tmp_buf = add_std_buffer(file);
5002 if (options & ~UCRTBASE_PRINTF_MASK)
5003 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
5004 ret = pf_printf_a(puts_clbk_file_a, file, format, locale,
5005 options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
5006 if(tmp_buf) remove_std_buffer(file);
5007 MSVCRT__unlock_file(file);
5009 return ret;
5012 /*********************************************************************
5013 * _vfwprintf_l (MSVCRT.@)
5015 int CDECL MSVCRT__vfwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format,
5016 MSVCRT__locale_t locale, __ms_va_list valist)
5018 BOOL tmp_buf;
5019 int ret;
5021 if (!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5023 MSVCRT__lock_file(file);
5024 tmp_buf = add_std_buffer(file);
5025 ret = pf_printf_w(puts_clbk_file_w, file, format, locale, 0, arg_clbk_valist, NULL, &valist);
5026 if(tmp_buf) remove_std_buffer(file);
5027 MSVCRT__unlock_file(file);
5029 return ret;
5032 /*********************************************************************
5033 * vprintf (MSVCRT.@)
5035 int CDECL MSVCRT_vprintf(const char *format, __ms_va_list valist)
5037 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
5040 /*********************************************************************
5041 * vprintf_s (MSVCRT.@)
5043 int CDECL MSVCRT_vprintf_s(const char *format, __ms_va_list valist)
5045 return MSVCRT_vfprintf_s(MSVCRT_stdout,format,valist);
5048 /*********************************************************************
5049 * vwprintf (MSVCRT.@)
5051 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, __ms_va_list valist)
5053 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
5056 /*********************************************************************
5057 * vwprintf_s (MSVCRT.@)
5059 int CDECL MSVCRT_vwprintf_s(const MSVCRT_wchar_t *format, __ms_va_list valist)
5061 return MSVCRT_vfwprintf_s(MSVCRT_stdout,format,valist);
5064 /*********************************************************************
5065 * fprintf (MSVCRT.@)
5067 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
5069 __ms_va_list valist;
5070 int res;
5071 __ms_va_start(valist, format);
5072 res = MSVCRT_vfprintf(file, format, valist);
5073 __ms_va_end(valist);
5074 return res;
5077 /*********************************************************************
5078 * fprintf_s (MSVCRT.@)
5080 int CDECL MSVCRT_fprintf_s(MSVCRT_FILE* file, const char *format, ...)
5082 __ms_va_list valist;
5083 int res;
5084 __ms_va_start(valist, format);
5085 res = MSVCRT_vfprintf_s(file, format, valist);
5086 __ms_va_end(valist);
5087 return res;
5090 /*********************************************************************
5091 * fwprintf (MSVCRT.@)
5093 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5095 __ms_va_list valist;
5096 int res;
5097 __ms_va_start(valist, format);
5098 res = MSVCRT_vfwprintf(file, format, valist);
5099 __ms_va_end(valist);
5100 return res;
5103 /*********************************************************************
5104 * fwprintf_s (MSVCRT.@)
5106 int CDECL MSVCRT_fwprintf_s(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
5108 __ms_va_list valist;
5109 int res;
5110 __ms_va_start(valist, format);
5111 res = MSVCRT_vfwprintf_s(file, format, valist);
5112 __ms_va_end(valist);
5113 return res;
5116 /*********************************************************************
5117 * _fwprintf_l (MSVCRT.@)
5119 int CDECL MSVCRT__fwprintf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
5121 __ms_va_list valist;
5122 int res;
5123 __ms_va_start(valist, locale);
5124 res = MSVCRT__vfwprintf_l(file, format, locale, valist);
5125 __ms_va_end(valist);
5126 return res;
5129 /*********************************************************************
5130 * printf (MSVCRT.@)
5132 int CDECL MSVCRT_printf(const char *format, ...)
5134 __ms_va_list valist;
5135 int res;
5136 __ms_va_start(valist, format);
5137 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
5138 __ms_va_end(valist);
5139 return res;
5142 /*********************************************************************
5143 * printf_s (MSVCRT.@)
5145 int CDECL MSVCRT_printf_s(const char *format, ...)
5147 __ms_va_list valist;
5148 int res;
5149 __ms_va_start(valist, format);
5150 res = MSVCRT_vprintf_s(format, valist);
5151 __ms_va_end(valist);
5152 return res;
5155 /*********************************************************************
5156 * ungetc (MSVCRT.@)
5158 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
5160 int ret;
5162 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5164 MSVCRT__lock_file(file);
5165 ret = MSVCRT__ungetc_nolock(c, file);
5166 MSVCRT__unlock_file(file);
5168 return ret;
5171 /*********************************************************************
5172 * _ungetc_nolock (MSVCRT.@)
5174 int CDECL MSVCRT__ungetc_nolock(int c, MSVCRT_FILE * file)
5176 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_EOF;
5178 if (c == MSVCRT_EOF || !(file->_flag&MSVCRT__IOREAD ||
5179 (file->_flag&MSVCRT__IORW && !(file->_flag&MSVCRT__IOWRT))))
5180 return MSVCRT_EOF;
5182 if((!(file->_flag & (MSVCRT__IONBF | MSVCRT__IOMYBUF | MSVCRT__USERBUF))
5183 && msvcrt_alloc_buffer(file))
5184 || (!file->_cnt && file->_ptr==file->_base))
5185 file->_ptr++;
5187 if(file->_ptr>file->_base) {
5188 file->_ptr--;
5189 if(file->_flag & MSVCRT__IOSTRG) {
5190 if(*file->_ptr != c) {
5191 file->_ptr++;
5192 return MSVCRT_EOF;
5194 }else {
5195 *file->_ptr = c;
5197 file->_cnt++;
5198 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
5199 file->_flag |= MSVCRT__IOREAD;
5200 return c;
5203 return MSVCRT_EOF;
5206 /*********************************************************************
5207 * ungetwc (MSVCRT.@)
5209 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5211 MSVCRT_wint_t ret;
5213 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5215 MSVCRT__lock_file(file);
5216 ret = MSVCRT__ungetwc_nolock(wc, file);
5217 MSVCRT__unlock_file(file);
5219 return ret;
5222 /*********************************************************************
5223 * _ungetwc_nolock (MSVCRT.@)
5225 MSVCRT_wint_t CDECL MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc, MSVCRT_FILE * file)
5227 MSVCRT_wchar_t mwc = wc;
5229 if(!MSVCRT_CHECK_PMT(file != NULL)) return MSVCRT_WEOF;
5230 if (wc == MSVCRT_WEOF)
5231 return MSVCRT_WEOF;
5233 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
5234 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5235 unsigned char * pp = (unsigned char *)&mwc;
5236 int i;
5238 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
5239 if(pp[i] != MSVCRT__ungetc_nolock(pp[i],file))
5240 return MSVCRT_WEOF;
5242 }else {
5243 char mbs[MSVCRT_MB_LEN_MAX];
5244 int len;
5246 len = MSVCRT_wctomb(mbs, mwc);
5247 if(len == -1)
5248 return MSVCRT_WEOF;
5250 for(len--; len>=0; len--) {
5251 if(mbs[len] != MSVCRT__ungetc_nolock(mbs[len], file))
5252 return MSVCRT_WEOF;
5256 return mwc;
5259 /*********************************************************************
5260 * wprintf (MSVCRT.@)
5262 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
5264 __ms_va_list valist;
5265 int res;
5266 __ms_va_start(valist, format);
5267 res = MSVCRT_vwprintf(format, valist);
5268 __ms_va_end(valist);
5269 return res;
5272 /*********************************************************************
5273 * wprintf_s (MSVCRT.@)
5275 int CDECL MSVCRT_wprintf_s(const MSVCRT_wchar_t *format, ...)
5277 __ms_va_list valist;
5278 int res;
5279 __ms_va_start(valist, format);
5280 res = MSVCRT_vwprintf_s(format, valist);
5281 __ms_va_end(valist);
5282 return res;
5285 /*********************************************************************
5286 * _getmaxstdio (MSVCRT.@)
5288 int CDECL MSVCRT__getmaxstdio(void)
5290 return MSVCRT_max_streams;
5293 /*********************************************************************
5294 * _setmaxstdio (MSVCRT.@)
5296 int CDECL MSVCRT__setmaxstdio(int newmax)
5298 TRACE("%d\n", newmax);
5300 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5301 return -1;
5303 MSVCRT_max_streams = newmax;
5304 return MSVCRT_max_streams;
5307 /*********************************************************************
5308 * _get_stream_buffer_pointers (UCRTBASE.@)
5310 int CDECL MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE *file, char*** base,
5311 char*** ptr, int** count)
5313 if (base)
5314 *base = &file->_base;
5315 if (ptr)
5316 *ptr = &file->_ptr;
5317 if (count)
5318 *count = &file->_cnt;
5319 return 0;