We should always allocate in NdrConformantStringUnmarshal if the
[wine.git] / dlls / msvcrt / file.c
blob0b428167621ebd2a7afb716cf5636839f5e1dd01
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <time.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <sys/types.h>
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winreg.h"
43 #include "winternl.h"
44 #include "msvcrt.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
60 /* values for wxflag in file descriptor */
61 #define WX_OPEN 0x01
62 #define WX_ATEOF 0x02
63 #define WX_DONTINHERIT 0x10
64 #define WX_APPEND 0x20
65 #define WX_TEXT 0x80
67 /* FIXME: this should be allocated dynamically */
68 #define MSVCRT_MAX_FILES 2048
70 typedef struct {
71 HANDLE handle;
72 unsigned char wxflag;
73 DWORD unkn[7]; /* critical section and init flag */
74 } ioinfo;
76 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
78 MSVCRT_FILE MSVCRT__iob[3];
80 static int MSVCRT_fdstart = 3; /* first unallocated fd */
81 static int MSVCRT_fdend = 3; /* highest allocated fd */
83 static MSVCRT_FILE* MSVCRT_fstreams[2048];
84 static int MSVCRT_stream_idx;
86 /* INTERNAL: process umask */
87 static int MSVCRT_umask = 0;
89 /* INTERNAL: Static buffer for temp file name */
90 static char MSVCRT_tmpname[MAX_PATH];
92 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
93 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
94 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
95 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
97 #define TOUL(x) (ULONGLONG)(x)
98 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
99 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
100 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
101 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
103 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
104 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
105 * and MSVCRT_stream_idx, from race conditions.
106 * It doesn't protect against race conditions manipulating the underlying files
107 * or flags; doing so would probably be better accomplished with per-file
108 * protection, rather than locking the whole table for every change.
110 static CRITICAL_SECTION MSVCRT_file_cs;
111 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
112 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
114 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64 *bufi64, struct MSVCRT__stat *buf)
116 buf->st_dev = bufi64->st_dev;
117 buf->st_ino = bufi64->st_ino;
118 buf->st_mode = bufi64->st_mode;
119 buf->st_nlink = bufi64->st_nlink;
120 buf->st_uid = bufi64->st_uid;
121 buf->st_gid = bufi64->st_gid;
122 buf->st_rdev = bufi64->st_rdev;
123 buf->st_size = bufi64->st_size;
124 buf->st_atime = bufi64->st_atime;
125 buf->st_mtime = bufi64->st_mtime;
126 buf->st_ctime = bufi64->st_ctime;
129 static inline BOOL msvcrt_is_valid_fd(int fd)
131 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
134 /* INTERNAL: Get the HANDLE for a fd
135 * This doesn't lock the table, because a failure will result in
136 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
137 * it returns a valid handle which is about to be closed, a subsequent call
138 * will fail, most likely in a sane way.
140 static HANDLE msvcrt_fdtoh(int fd)
142 if (!msvcrt_is_valid_fd(fd))
144 WARN(":fd (%d) - no handle!\n",fd);
145 *MSVCRT___doserrno() = 0;
146 *MSVCRT__errno() = MSVCRT_EBADF;
147 return INVALID_HANDLE_VALUE;
149 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
150 return MSVCRT_fdesc[fd].handle;
153 /* INTERNAL: free a file entry fd */
154 static void msvcrt_free_fd(int fd)
156 LOCK_FILES();
157 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
158 MSVCRT_fdesc[fd].wxflag = 0;
159 TRACE(":fd (%d) freed\n",fd);
160 if (fd < 3) /* don't use 0,1,2 for user files */
162 switch (fd)
164 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
165 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
166 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
169 else
171 if (fd == MSVCRT_fdend - 1)
172 MSVCRT_fdend--;
173 if (fd < MSVCRT_fdstart)
174 MSVCRT_fdstart = fd;
176 UNLOCK_FILES();
179 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
180 /* caller must hold the files lock */
181 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
183 if (fd >= MSVCRT_MAX_FILES)
185 WARN(":files exhausted!\n");
186 return -1;
188 MSVCRT_fdesc[fd].handle = hand;
189 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
191 /* locate next free slot */
192 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
193 MSVCRT_fdstart = MSVCRT_fdend + 1;
194 else
195 while (MSVCRT_fdstart < MSVCRT_fdend &&
196 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
197 MSVCRT_fdstart++;
198 /* update last fd in use */
199 if (fd >= MSVCRT_fdend)
200 MSVCRT_fdend = fd + 1;
201 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
203 switch (fd)
205 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
206 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
207 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
210 return fd;
213 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
214 static int msvcrt_alloc_fd(HANDLE hand, int flag)
216 int ret;
218 LOCK_FILES();
219 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
220 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
221 UNLOCK_FILES();
222 return ret;
225 /* INTERNAL: Allocate a FILE* for an fd slot */
226 /* caller must hold the files lock */
227 static MSVCRT_FILE* msvcrt_alloc_fp(void)
229 unsigned int i;
231 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
233 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
235 if (!MSVCRT_fstreams[i])
237 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
238 return NULL;
239 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
241 return MSVCRT_fstreams[i];
244 return NULL;
247 /* INTERNAL: initialize a FILE* from an open fd */
248 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
250 TRACE(":fd (%d) allocating FILE*\n",fd);
251 if (!msvcrt_is_valid_fd(fd))
253 WARN(":invalid fd %d\n",fd);
254 *MSVCRT___doserrno() = 0;
255 *MSVCRT__errno() = MSVCRT_EBADF;
256 return -1;
258 memset(file, 0, sizeof(*file));
259 file->_file = fd;
260 file->_flag = stream_flags;
262 TRACE(":got FILE* (%p)\n",file);
263 return 0;
266 /* INTERNAL: Create an inheritance data block (for spawned process)
267 * The inheritance block is made of:
268 * 00 int nb of file descriptor (NBFD)
269 * 04 char file flags (wxflag): repeated for each fd
270 * 4+NBFD HANDLE file handle: repeated for each fd
272 unsigned msvcrt_create_io_inherit_block(STARTUPINFOA* si)
274 int fd;
275 char* wxflag_ptr;
276 HANDLE* handle_ptr;
278 si->cbReserved2 = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
279 si->lpReserved2 = MSVCRT_calloc(si->cbReserved2, 1);
280 if (!si->lpReserved2)
282 si->cbReserved2 = 0;
283 return FALSE;
285 wxflag_ptr = (char*)si->lpReserved2 + sizeof(unsigned);
286 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
288 *(unsigned*)si->lpReserved2 = MSVCRT_fdend;
289 for (fd = 0; fd < MSVCRT_fdend; fd++)
291 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
292 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
294 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
295 *handle_ptr = MSVCRT_fdesc[fd].handle;
297 else
299 *wxflag_ptr = 0;
300 *handle_ptr = INVALID_HANDLE_VALUE;
302 wxflag_ptr++; handle_ptr++;
304 return TRUE;
307 /* INTERNAL: Set up all file descriptors,
308 * as well as default streams (stdin, stderr and stdout)
310 void msvcrt_init_io(void)
312 STARTUPINFOA si;
313 int i;
315 InitializeCriticalSection(&MSVCRT_file_cs);
316 GetStartupInfoA(&si);
317 if (si.cbReserved2 != 0 && si.lpReserved2 != NULL)
319 char* wxflag_ptr;
320 HANDLE* handle_ptr;
322 MSVCRT_fdend = *(unsigned*)si.lpReserved2;
324 wxflag_ptr = (char*)(si.lpReserved2 + sizeof(unsigned));
325 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
327 MSVCRT_fdend = min(MSVCRT_fdend, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
328 for (i = 0; i < MSVCRT_fdend; i++)
330 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
332 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
333 MSVCRT_fdesc[i].handle = *handle_ptr;
335 else
337 MSVCRT_fdesc[i].wxflag = 0;
338 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
340 wxflag_ptr++; handle_ptr++;
342 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
343 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
346 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
348 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
349 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, FALSE,
350 DUPLICATE_SAME_ACCESS);
351 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
353 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
355 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
356 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, FALSE,
357 DUPLICATE_SAME_ACCESS);
358 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
360 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
362 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
363 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, FALSE,
364 DUPLICATE_SAME_ACCESS);
365 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
368 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
369 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
371 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
372 for (i = 0; i < 3; i++)
374 /* FILE structs for stdin/out/err are static and never deleted */
375 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
376 MSVCRT__iob[i]._file = i;
377 MSVCRT__iob[i]._tmpfname = NULL;
378 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
380 MSVCRT_stream_idx = 3;
383 /* INTERNAL: Flush stdio file buffer */
384 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
386 if(file->_bufsiz) {
387 int cnt=file->_ptr-file->_base;
388 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
389 file->_flag |= MSVCRT__IOERR;
390 return MSVCRT_EOF;
392 file->_ptr=file->_base;
393 file->_cnt=file->_bufsiz;
395 return 0;
398 /* INTERNAL: Allocate stdio file buffer */
399 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
401 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
402 if(file->_base) {
403 file->_bufsiz = MSVCRT_BUFSIZ;
404 file->_flag |= MSVCRT__IOMYBUF;
405 } else {
406 file->_base = (char*)(&file->_charbuf);
407 /* put here 2 ??? */
408 file->_bufsiz = sizeof(file->_charbuf);
410 file->_ptr = file->_base;
411 file->_cnt = 0;
414 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
415 static void msvcrt_int_to_base32(int num, char *str)
417 char *p;
418 int n = num;
419 int digits = 0;
421 while (n != 0)
423 n >>= 5;
424 digits++;
426 p = str + digits;
427 *p = 0;
428 while (--p >= str)
430 *p = (num & 31) + '0';
431 if (*p > '9')
432 *p += ('a' - '0' - 10);
433 num >>= 5;
437 /*********************************************************************
438 * __p__iob(MSVCRT.@)
440 MSVCRT_FILE *__p__iob(void)
442 return &MSVCRT__iob[0];
445 /*********************************************************************
446 * _access (MSVCRT.@)
448 int _access(const char *filename, int mode)
450 DWORD attr = GetFileAttributesA(filename);
452 TRACE("(%s,%d) %ld\n",filename,mode,attr);
454 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
456 msvcrt_set_errno(GetLastError());
457 return -1;
459 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
461 msvcrt_set_errno(ERROR_ACCESS_DENIED);
462 return -1;
464 return 0;
467 /*********************************************************************
468 * _waccess (MSVCRT.@)
470 int _waccess(const MSVCRT_wchar_t *filename, int mode)
472 DWORD attr = GetFileAttributesW(filename);
474 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
476 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
478 msvcrt_set_errno(GetLastError());
479 return -1;
481 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
483 msvcrt_set_errno(ERROR_ACCESS_DENIED);
484 return -1;
486 return 0;
489 /*********************************************************************
490 * _chmod (MSVCRT.@)
492 int _chmod(const char *path, int flags)
494 DWORD oldFlags = GetFileAttributesA(path);
496 if (oldFlags != INVALID_FILE_ATTRIBUTES)
498 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
499 oldFlags | FILE_ATTRIBUTE_READONLY;
501 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
502 return 0;
504 msvcrt_set_errno(GetLastError());
505 return -1;
508 /*********************************************************************
509 * _wchmod (MSVCRT.@)
511 int _wchmod(const MSVCRT_wchar_t *path, int flags)
513 DWORD oldFlags = GetFileAttributesW(path);
515 if (oldFlags != INVALID_FILE_ATTRIBUTES)
517 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
518 oldFlags | FILE_ATTRIBUTE_READONLY;
520 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
521 return 0;
523 msvcrt_set_errno(GetLastError());
524 return -1;
527 /*********************************************************************
528 * _unlink (MSVCRT.@)
530 int _unlink(const char *path)
532 TRACE("%s\n",debugstr_a(path));
533 if(DeleteFileA(path))
534 return 0;
535 TRACE("failed (%ld)\n",GetLastError());
536 msvcrt_set_errno(GetLastError());
537 return -1;
540 /*********************************************************************
541 * _wunlink (MSVCRT.@)
543 int _wunlink(const MSVCRT_wchar_t *path)
545 TRACE("(%s)\n",debugstr_w(path));
546 if(DeleteFileW(path))
547 return 0;
548 TRACE("failed (%ld)\n",GetLastError());
549 msvcrt_set_errno(GetLastError());
550 return -1;
553 /* _flushall calls MSVCRT_fflush which calls _flushall */
554 int MSVCRT_fflush(MSVCRT_FILE* file);
556 /*********************************************************************
557 * _flushall (MSVCRT.@)
559 int _flushall(void)
561 int i, num_flushed = 0;
563 LOCK_FILES();
564 for (i = 3; i < MSVCRT_stream_idx; i++)
565 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
567 #if 0
568 /* FIXME: flush, do not commit */
569 if (_commit(i) == -1)
570 if (MSVCRT_fstreams[i])
571 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
572 #endif
573 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
574 MSVCRT_fflush(MSVCRT_fstreams[i]);
575 num_flushed++;
578 UNLOCK_FILES();
580 TRACE(":flushed (%d) handles\n",num_flushed);
581 return num_flushed;
584 /*********************************************************************
585 * fflush (MSVCRT.@)
587 int MSVCRT_fflush(MSVCRT_FILE* file)
589 if(!file) {
590 _flushall();
591 return 0;
592 } else {
593 int res=msvcrt_flush_buffer(file);
594 return res;
598 /*********************************************************************
599 * _close (MSVCRT.@)
601 int _close(int fd)
603 HANDLE hand;
604 int ret;
606 LOCK_FILES();
607 hand = msvcrt_fdtoh(fd);
608 TRACE(":fd (%d) handle (%p)\n",fd,hand);
609 if (hand == INVALID_HANDLE_VALUE)
610 ret = -1;
611 else if (!CloseHandle(hand))
613 WARN(":failed-last error (%ld)\n",GetLastError());
614 msvcrt_set_errno(GetLastError());
615 ret = -1;
617 else
619 msvcrt_free_fd(fd);
620 ret = 0;
622 UNLOCK_FILES();
623 TRACE(":ok\n");
624 return ret;
627 /*********************************************************************
628 * _commit (MSVCRT.@)
630 int _commit(int fd)
632 HANDLE hand = msvcrt_fdtoh(fd);
634 TRACE(":fd (%d) handle (%p)\n",fd,hand);
635 if (hand == INVALID_HANDLE_VALUE)
636 return -1;
638 if (!FlushFileBuffers(hand))
640 if (GetLastError() == ERROR_INVALID_HANDLE)
642 /* FlushFileBuffers fails for console handles
643 * so we ignore this error.
645 return 0;
647 TRACE(":failed-last error (%ld)\n",GetLastError());
648 msvcrt_set_errno(GetLastError());
649 return -1;
651 TRACE(":ok\n");
652 return 0;
655 /*********************************************************************
656 * _dup2 (MSVCRT.@)
657 * NOTES
658 * MSDN isn't clear on this point, but the remarks for _pipe,
659 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
660 * indicate file descriptors duplicated with _dup and _dup2 are always
661 * inheritable.
663 int _dup2(int od, int nd)
665 int ret;
667 TRACE("(od=%d, nd=%d)\n", od, nd);
668 LOCK_FILES();
669 if (nd < MSVCRT_MAX_FILES && msvcrt_is_valid_fd(od))
671 HANDLE handle;
673 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
674 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
676 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
678 if (msvcrt_is_valid_fd(nd))
679 _close(nd);
680 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
681 if (ret == -1)
683 CloseHandle(handle);
684 *MSVCRT__errno() = MSVCRT_EMFILE;
686 else
688 /* _dup2 returns 0, not nd, on success */
689 ret = 0;
692 else
694 ret = -1;
695 msvcrt_set_errno(GetLastError());
698 else
700 *MSVCRT__errno() = MSVCRT_EBADF;
701 ret = -1;
703 UNLOCK_FILES();
704 return ret;
707 /*********************************************************************
708 * _dup (MSVCRT.@)
710 int _dup(int od)
712 int fd, ret;
714 LOCK_FILES();
715 fd = MSVCRT_fdstart;
716 if (_dup2(od, fd) == 0)
717 ret = fd;
718 else
719 ret = -1;
720 UNLOCK_FILES();
721 return ret;
724 /*********************************************************************
725 * _eof (MSVCRT.@)
727 int _eof(int fd)
729 DWORD curpos,endpos;
730 LONG hcurpos,hendpos;
731 HANDLE hand = msvcrt_fdtoh(fd);
733 TRACE(":fd (%d) handle (%p)\n",fd,hand);
735 if (hand == INVALID_HANDLE_VALUE)
736 return -1;
738 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
740 /* Otherwise we do it the hard way */
741 hcurpos = hendpos = 0;
742 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
743 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
745 if (curpos == endpos && hcurpos == hendpos)
747 /* FIXME: shouldn't WX_ATEOF be set here? */
748 return TRUE;
751 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
752 return FALSE;
755 /*********************************************************************
756 * _fcloseall (MSVCRT.@)
758 int MSVCRT__fcloseall(void)
760 int num_closed = 0, i;
762 LOCK_FILES();
763 for (i = 3; i < MSVCRT_stream_idx; i++)
764 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
765 !MSVCRT_fclose(MSVCRT_fstreams[i]))
766 num_closed++;
767 UNLOCK_FILES();
769 TRACE(":closed (%d) handles\n",num_closed);
770 return num_closed;
773 /* free everything on process exit */
774 void msvcrt_free_io(void)
776 MSVCRT__fcloseall();
777 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
778 * stdout, and stderr (unlike GNU), so we need to fclose() them here
779 * or they won't get flushed.
781 MSVCRT_fclose(&MSVCRT__iob[0]);
782 MSVCRT_fclose(&MSVCRT__iob[1]);
783 MSVCRT_fclose(&MSVCRT__iob[2]);
784 DeleteCriticalSection(&MSVCRT_file_cs);
787 /*********************************************************************
788 * _lseeki64 (MSVCRT.@)
790 __int64 _lseeki64(int fd, __int64 offset, int whence)
792 HANDLE hand = msvcrt_fdtoh(fd);
793 LARGE_INTEGER ofs, ret;
795 TRACE(":fd (%d) handle (%p)\n",fd,hand);
796 if (hand == INVALID_HANDLE_VALUE)
797 return -1;
799 if (whence < 0 || whence > 2)
801 *MSVCRT__errno() = MSVCRT_EINVAL;
802 return -1;
805 TRACE(":fd (%d) to %s pos %s\n",
806 fd,wine_dbgstr_longlong(ofs.QuadPart),
807 (whence==SEEK_SET)?"SEEK_SET":
808 (whence==SEEK_CUR)?"SEEK_CUR":
809 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
811 ofs.QuadPart = offset;
812 if (SetFilePointerEx(hand, ofs, &ret, whence))
814 MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
815 /* FIXME: What if we seek _to_ EOF - is EOF set? */
817 return ret.QuadPart;
819 TRACE(":error-last error (%ld)\n",GetLastError());
820 msvcrt_set_errno(GetLastError());
821 return -1;
824 /*********************************************************************
825 * _lseek (MSVCRT.@)
827 LONG _lseek(int fd, LONG offset, int whence)
829 return _lseeki64(fd, offset, whence);
832 /*********************************************************************
833 * _locking (MSVCRT.@)
835 * This is untested; the underlying LockFile doesn't work yet.
837 int _locking(int fd, int mode, LONG nbytes)
839 BOOL ret;
840 DWORD cur_locn;
841 HANDLE hand = msvcrt_fdtoh(fd);
843 TRACE(":fd (%d) handle (%p)\n",fd,hand);
844 if (hand == INVALID_HANDLE_VALUE)
845 return -1;
847 if (mode < 0 || mode > 4)
849 *MSVCRT__errno() = MSVCRT_EINVAL;
850 return -1;
853 TRACE(":fd (%d) by 0x%08lx mode %s\n",
854 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
855 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
856 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
857 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
858 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
859 "UNKNOWN");
861 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
863 FIXME ("Seek failed\n");
864 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
865 return -1;
867 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
869 int nretry = 10;
870 ret = 1; /* just to satisfy gcc */
871 while (nretry--)
873 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
874 if (ret) break;
875 Sleep(1);
878 else if (mode == MSVCRT__LK_UNLCK)
879 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
880 else
881 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
882 /* FIXME - what about error settings? */
883 return ret ? 0 : -1;
886 /*********************************************************************
887 * fseek (MSVCRT.@)
889 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
891 /* Flush output if needed */
892 if(file->_flag & MSVCRT__IOWRT)
893 msvcrt_flush_buffer(file);
895 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
896 offset -= file->_cnt;
898 /* Discard buffered input */
899 file->_cnt = 0;
900 file->_ptr = file->_base;
901 /* Reset direction of i/o */
902 if(file->_flag & MSVCRT__IORW) {
903 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
905 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
908 /*********************************************************************
909 * _chsize (MSVCRT.@)
911 int _chsize(int fd, long size)
913 LONG cur, pos;
914 HANDLE handle;
915 BOOL ret = FALSE;
917 TRACE("(fd=%d, size=%ld)\n", fd, size);
919 LOCK_FILES();
921 handle = msvcrt_fdtoh(fd);
922 if (handle != INVALID_HANDLE_VALUE)
924 /* save the current file pointer */
925 cur = _lseek(fd, 0, SEEK_CUR);
926 if (cur >= 0)
928 pos = _lseek(fd, size, SEEK_SET);
929 if (pos >= 0)
931 ret = SetEndOfFile(handle);
932 if (!ret) msvcrt_set_errno(GetLastError());
935 /* restore the file pointer */
936 _lseek(fd, cur, SEEK_SET);
940 UNLOCK_FILES();
941 return ret ? 0 : -1;
944 /*********************************************************************
945 * clearerr (MSVCRT.@)
947 void MSVCRT_clearerr(MSVCRT_FILE* file)
949 TRACE(":file (%p) fd (%d)\n",file,file->_file);
950 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
953 /*********************************************************************
954 * rewind (MSVCRT.@)
956 void MSVCRT_rewind(MSVCRT_FILE* file)
958 TRACE(":file (%p) fd (%d)\n",file,file->_file);
959 MSVCRT_fseek(file, 0L, SEEK_SET);
960 MSVCRT_clearerr(file);
963 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
965 int plus = strchr(mode, '+') != NULL;
967 switch(*mode++)
969 case 'R': case 'r':
970 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
971 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
972 break;
973 case 'W': case 'w':
974 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
975 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
976 break;
977 case 'A': case 'a':
978 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
979 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
980 break;
981 default:
982 return -1;
985 while (*mode)
986 switch (*mode++)
988 case 'B': case 'b':
989 *open_flags |= MSVCRT__O_BINARY;
990 *open_flags &= ~MSVCRT__O_TEXT;
991 break;
992 case 'T': case 't':
993 *open_flags |= MSVCRT__O_TEXT;
994 *open_flags &= ~MSVCRT__O_BINARY;
995 break;
996 case '+':
997 break;
998 default:
999 FIXME(":unknown flag %c not supported\n",mode[-1]);
1001 return 0;
1004 /*********************************************************************
1005 * _fdopen (MSVCRT.@)
1007 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
1009 int open_flags, stream_flags;
1010 MSVCRT_FILE* file;
1012 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1014 LOCK_FILES();
1015 if (!(file = msvcrt_alloc_fp()))
1016 file = NULL;
1017 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1019 file->_flag = 0;
1020 file = NULL;
1022 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1023 UNLOCK_FILES();
1025 return file;
1028 /*********************************************************************
1029 * _wfdopen (MSVCRT.@)
1031 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1033 unsigned mlen = strlenW(mode);
1034 char *modea = MSVCRT_calloc(mlen + 1, 1);
1035 MSVCRT_FILE* file = NULL;
1036 int open_flags, stream_flags;
1038 if (modea &&
1039 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1041 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1042 LOCK_FILES();
1043 if (!(file = msvcrt_alloc_fp()))
1044 file = NULL;
1045 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1047 file->_flag = 0;
1048 file = NULL;
1050 else
1052 if (file)
1053 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1054 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1056 UNLOCK_FILES();
1058 return file;
1061 /*********************************************************************
1062 * _filelength (MSVCRT.@)
1064 LONG _filelength(int fd)
1066 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1067 if (curPos != -1)
1069 LONG endPos = _lseek(fd, 0, SEEK_END);
1070 if (endPos != -1)
1072 if (endPos != curPos)
1073 _lseek(fd, curPos, SEEK_SET);
1074 return endPos;
1077 return -1;
1080 /*********************************************************************
1081 * _filelengthi64 (MSVCRT.@)
1083 __int64 _filelengthi64(int fd)
1085 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1086 if (curPos != -1)
1088 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1089 if (endPos != -1)
1091 if (endPos != curPos)
1092 _lseeki64(fd, curPos, SEEK_SET);
1093 return endPos;
1096 return -1;
1099 /*********************************************************************
1100 * _fileno (MSVCRT.@)
1102 int MSVCRT__fileno(MSVCRT_FILE* file)
1104 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1105 return file->_file;
1108 /*********************************************************************
1109 * _fstati64 (MSVCRT.@)
1111 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1113 DWORD dw;
1114 BY_HANDLE_FILE_INFORMATION hfi;
1115 HANDLE hand = msvcrt_fdtoh(fd);
1117 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1118 if (hand == INVALID_HANDLE_VALUE)
1119 return -1;
1121 if (!buf)
1123 WARN(":failed-NULL buf\n");
1124 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1125 return -1;
1128 memset(&hfi, 0, sizeof(hfi));
1129 memset(buf, 0, sizeof(struct MSVCRT__stati64));
1130 if (!GetFileInformationByHandle(hand, &hfi))
1132 WARN(":failed-last error (%ld)\n",GetLastError());
1133 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1134 return -1;
1136 dw = GetFileType(hand);
1137 buf->st_mode = S_IREAD;
1138 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1139 buf->st_mode |= S_IWRITE;
1140 /* interestingly, Windows never seems to set S_IFDIR */
1141 if (dw == FILE_TYPE_CHAR)
1142 buf->st_mode |= S_IFCHR;
1143 else if (dw == FILE_TYPE_PIPE)
1144 buf->st_mode |= S_IFIFO;
1145 else
1146 buf->st_mode |= S_IFREG;
1147 TRACE(":dwFileAttributes = 0x%lx, mode set to 0x%x\n",hfi.dwFileAttributes,
1148 buf->st_mode);
1149 buf->st_nlink = hfi.nNumberOfLinks;
1150 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1151 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1152 buf->st_atime = dw;
1153 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1154 buf->st_mtime = buf->st_ctime = dw;
1155 return 0;
1158 /*********************************************************************
1159 * _fstat (MSVCRT.@)
1161 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1162 { int ret;
1163 struct MSVCRT__stati64 bufi64;
1165 ret = MSVCRT__fstati64(fd, &bufi64);
1166 if (!ret)
1167 msvcrt_cp_from_stati64(&bufi64, buf);
1168 return ret;
1171 /*********************************************************************
1172 * _futime (MSVCRT.@)
1174 int _futime(int fd, struct MSVCRT__utimbuf *t)
1176 HANDLE hand = msvcrt_fdtoh(fd);
1177 FILETIME at, wt;
1179 if (!t)
1181 MSVCRT_time_t currTime;
1182 MSVCRT_time(&currTime);
1183 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1184 memcpy(&wt, &at, sizeof(wt));
1186 else
1188 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1189 if (t->actime == t->modtime)
1190 memcpy(&wt, &at, sizeof(wt));
1191 else
1192 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1195 if (!SetFileTime(hand, NULL, &at, &wt))
1197 msvcrt_set_errno(GetLastError());
1198 return -1 ;
1200 return 0;
1203 /*********************************************************************
1204 * _get_osfhandle (MSVCRT.@)
1206 long _get_osfhandle(int fd)
1208 HANDLE hand = msvcrt_fdtoh(fd);
1209 HANDLE newhand = hand;
1210 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1212 if (hand != INVALID_HANDLE_VALUE)
1214 /* FIXME: I'm not convinced that I should be copying the
1215 * handle here - it may be leaked if the app doesn't
1216 * close it (and the API docs don't say that it should)
1217 * Not duplicating it means that it can't be inherited
1218 * and so lcc's wedit doesn't cope when it passes it to
1219 * child processes. I've an idea that it should either
1220 * be copied by CreateProcess, or marked as inheritable
1221 * when initialised, or maybe both? JG 21-9-00.
1223 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
1224 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
1226 return (long)newhand;
1229 /*********************************************************************
1230 * _isatty (MSVCRT.@)
1232 int _isatty(int fd)
1234 HANDLE hand = msvcrt_fdtoh(fd);
1236 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1237 if (hand == INVALID_HANDLE_VALUE)
1238 return 0;
1240 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1243 /*********************************************************************
1244 * _mktemp (MSVCRT.@)
1246 char *_mktemp(char *pattern)
1248 int numX = 0;
1249 char *retVal = pattern;
1250 int id;
1251 char letter = 'a';
1253 while(*pattern)
1254 numX = (*pattern++ == 'X')? numX + 1 : 0;
1255 if (numX < 5)
1256 return NULL;
1257 pattern--;
1258 id = GetCurrentProcessId();
1259 numX = 6;
1260 while(numX--)
1262 int tempNum = id / 10;
1263 *pattern-- = id - (tempNum * 10) + '0';
1264 id = tempNum;
1266 pattern++;
1269 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1270 GetLastError() == ERROR_FILE_NOT_FOUND)
1271 return retVal;
1272 *pattern = letter++;
1273 } while(letter != '|');
1274 return NULL;
1277 /*********************************************************************
1278 * _wmktemp (MSVCRT.@)
1280 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
1282 int numX = 0;
1283 MSVCRT_wchar_t *retVal = pattern;
1284 int id;
1285 MSVCRT_wchar_t letter = 'a';
1287 while(*pattern)
1288 numX = (*pattern++ == 'X')? numX + 1 : 0;
1289 if (numX < 5)
1290 return NULL;
1291 pattern--;
1292 id = GetCurrentProcessId();
1293 numX = 6;
1294 while(numX--)
1296 int tempNum = id / 10;
1297 *pattern-- = id - (tempNum * 10) + '0';
1298 id = tempNum;
1300 pattern++;
1303 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1304 GetLastError() == ERROR_FILE_NOT_FOUND)
1305 return retVal;
1306 *pattern = letter++;
1307 } while(letter != '|');
1308 return NULL;
1311 static unsigned split_oflags(unsigned oflags)
1313 int wxflags = 0;
1314 unsigned unsupp; /* until we support everything */
1316 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1317 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1318 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1319 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1320 else wxflags |= WX_TEXT; /* default to TEXT*/
1321 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1323 if ((unsupp = oflags & ~(
1324 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1325 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1326 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1327 MSVCRT__O_NOINHERIT|
1328 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1330 ERR(":unsupported oflags 0x%04x\n",unsupp);
1332 return wxflags;
1335 /*********************************************************************
1336 * _pipe (MSVCRT.@)
1338 int _pipe(int *pfds, unsigned int psize, int textmode)
1340 int ret = -1;
1341 SECURITY_ATTRIBUTES sa;
1342 HANDLE readHandle, writeHandle;
1344 if (!pfds)
1346 *MSVCRT__errno() = MSVCRT_EINVAL;
1347 return -1;
1350 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1351 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1352 sa.lpSecurityDescriptor = NULL;
1353 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1355 unsigned int wxflags = split_oflags(textmode);
1356 int fd;
1358 LOCK_FILES();
1359 fd = msvcrt_alloc_fd(readHandle, wxflags);
1360 if (fd != -1)
1362 pfds[0] = fd;
1363 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1364 if (fd != -1)
1366 pfds[1] = fd;
1367 ret = 0;
1369 else
1371 _close(pfds[0]);
1372 CloseHandle(writeHandle);
1373 *MSVCRT__errno() = MSVCRT_EMFILE;
1376 else
1378 CloseHandle(readHandle);
1379 CloseHandle(writeHandle);
1380 *MSVCRT__errno() = MSVCRT_EMFILE;
1382 UNLOCK_FILES();
1384 else
1385 msvcrt_set_errno(GetLastError());
1387 return ret;
1390 /*********************************************************************
1391 * _sopen (MSVCRT.@)
1393 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1395 va_list ap;
1396 int pmode;
1397 DWORD access = 0, creation = 0, attrib;
1398 DWORD sharing;
1399 int wxflag = 0, fd;
1400 HANDLE hand;
1401 SECURITY_ATTRIBUTES sa;
1404 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1405 path, oflags, shflags);
1407 wxflag = split_oflags(oflags);
1408 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1410 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1411 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1412 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1415 if (oflags & MSVCRT__O_CREAT)
1417 va_start(ap, shflags);
1418 pmode = va_arg(ap, int);
1419 va_end(ap);
1421 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1422 FIXME(": pmode 0x%04x ignored\n", pmode);
1423 else
1424 WARN(": pmode 0x%04x ignored\n", pmode);
1426 if (oflags & MSVCRT__O_EXCL)
1427 creation = CREATE_NEW;
1428 else if (oflags & MSVCRT__O_TRUNC)
1429 creation = CREATE_ALWAYS;
1430 else
1431 creation = OPEN_ALWAYS;
1433 else /* no MSVCRT__O_CREAT */
1435 if (oflags & MSVCRT__O_TRUNC)
1436 creation = TRUNCATE_EXISTING;
1437 else
1438 creation = OPEN_EXISTING;
1441 switch( shflags )
1443 case MSVCRT__SH_DENYRW:
1444 sharing = 0L;
1445 break;
1446 case MSVCRT__SH_DENYWR:
1447 sharing = FILE_SHARE_READ;
1448 break;
1449 case MSVCRT__SH_DENYRD:
1450 sharing = FILE_SHARE_WRITE;
1451 break;
1452 case MSVCRT__SH_DENYNO:
1453 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1454 break;
1455 default:
1456 ERR( "Unhandled shflags 0x%x\n", shflags );
1457 return -1;
1459 attrib = FILE_ATTRIBUTE_NORMAL;
1461 if (oflags & MSVCRT__O_TEMPORARY)
1463 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1464 access |= DELETE;
1465 sharing |= FILE_SHARE_DELETE;
1468 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1469 sa.lpSecurityDescriptor = NULL;
1470 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1472 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1474 if (hand == INVALID_HANDLE_VALUE) {
1475 WARN(":failed-last error (%ld)\n",GetLastError());
1476 msvcrt_set_errno(GetLastError());
1477 return -1;
1480 fd = msvcrt_alloc_fd(hand, wxflag);
1482 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1483 return fd;
1486 /*********************************************************************
1487 * _wsopen (MSVCRT.@)
1489 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1491 const unsigned int len = strlenW(path);
1492 char *patha = MSVCRT_calloc(len + 1,1);
1493 va_list ap;
1494 int pmode;
1496 va_start(ap, shflags);
1497 pmode = va_arg(ap, int);
1498 va_end(ap);
1500 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1502 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1503 MSVCRT_free(patha);
1504 return retval;
1507 msvcrt_set_errno(GetLastError());
1508 return -1;
1511 /*********************************************************************
1512 * _open (MSVCRT.@)
1514 int _open( const char *path, int flags, ... )
1516 va_list ap;
1518 if (flags & MSVCRT__O_CREAT)
1520 int pmode;
1521 va_start(ap, flags);
1522 pmode = va_arg(ap, int);
1523 va_end(ap);
1524 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1526 else
1527 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1530 /*********************************************************************
1531 * _wopen (MSVCRT.@)
1533 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1535 const unsigned int len = strlenW(path);
1536 char *patha = MSVCRT_calloc(len + 1,1);
1537 va_list ap;
1538 int pmode;
1540 va_start(ap, flags);
1541 pmode = va_arg(ap, int);
1542 va_end(ap);
1544 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1546 int retval = _open(patha,flags,pmode);
1547 MSVCRT_free(patha);
1548 return retval;
1551 msvcrt_set_errno(GetLastError());
1552 return -1;
1555 /*********************************************************************
1556 * _creat (MSVCRT.@)
1558 int _creat(const char *path, int flags)
1560 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1561 return _open(path, usedFlags);
1564 /*********************************************************************
1565 * _wcreat (MSVCRT.@)
1567 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1569 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1570 return _wopen(path, usedFlags);
1573 /*********************************************************************
1574 * _open_osfhandle (MSVCRT.@)
1576 int _open_osfhandle(long handle, int oflags)
1578 int fd;
1580 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1581 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1582 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1583 * text - it never sets MSVCRT__O_BINARY.
1585 /* FIXME: handle more flags */
1586 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)) && (*__p__fmode() & MSVCRT__O_BINARY))
1587 oflags |= MSVCRT__O_BINARY;
1588 else
1589 oflags |= MSVCRT__O_TEXT;
1591 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1592 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1593 return fd;
1596 /*********************************************************************
1597 * _rmtmp (MSVCRT.@)
1599 int _rmtmp(void)
1601 int num_removed = 0, i;
1603 LOCK_FILES();
1604 for (i = 3; i < MSVCRT_stream_idx; i++)
1605 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1607 MSVCRT_fclose(MSVCRT_fstreams[i]);
1608 num_removed++;
1610 UNLOCK_FILES();
1612 if (num_removed)
1613 TRACE(":removed (%d) temp files\n",num_removed);
1614 return num_removed;
1617 /*********************************************************************
1618 * (internal) remove_cr
1620 * Remove all \r inplace.
1621 * return the number of \r removed
1623 static unsigned int remove_cr(char *buf, unsigned int count)
1625 unsigned int i, j;
1627 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1628 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1629 return count - i;
1632 /*********************************************************************
1633 * _read (MSVCRT.@)
1635 int _read(int fd, void *buf, unsigned int count)
1637 DWORD num_read, all_read = 0;
1638 char *bufstart = buf;
1639 HANDLE hand = msvcrt_fdtoh(fd);
1641 /* Don't trace small reads, it gets *very* annoying */
1642 if (count > 4)
1643 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1644 if (hand == INVALID_HANDLE_VALUE)
1645 return -1;
1647 /* Reading single bytes in O_TEXT mode makes things slow
1648 * So read big chunks, then remove the \r in memory and try reading
1649 * the rest until the request is satisfied or EOF is met
1651 while (all_read < count)
1653 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1655 if (num_read != (count - all_read))
1657 TRACE(":EOF\n");
1658 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1659 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1660 num_read -= remove_cr(bufstart+all_read,num_read);
1661 all_read += num_read;
1662 if (count > 4)
1663 TRACE("%s\n",debugstr_an(buf,all_read));
1664 return all_read;
1666 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1668 num_read -= remove_cr(bufstart+all_read,num_read);
1670 all_read += num_read;
1672 else
1674 TRACE(":failed-last error (%ld)\n",GetLastError());
1675 return -1;
1679 if (count > 4)
1680 TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
1681 return all_read;
1684 /*********************************************************************
1685 * _getw (MSVCRT.@)
1687 int MSVCRT__getw(MSVCRT_FILE* file)
1689 int i;
1690 switch (_read(file->_file, &i, sizeof(int)))
1692 case 1: return i;
1693 case 0: file->_flag |= MSVCRT__IOEOF; break;
1694 default: file->_flag |= MSVCRT__IOERR; break;
1696 return EOF;
1699 /*********************************************************************
1700 * _setmode (MSVCRT.@)
1702 int _setmode(int fd,int mode)
1704 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1705 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1706 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1707 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1708 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1709 else
1710 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1711 return ret;
1714 /*********************************************************************
1715 * _stati64 (MSVCRT.@)
1717 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1719 DWORD dw;
1720 WIN32_FILE_ATTRIBUTE_DATA hfi;
1721 unsigned short mode = ALL_S_IREAD;
1722 int plen;
1724 TRACE(":file (%s) buf(%p)\n",path,buf);
1726 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1728 TRACE("failed (%ld)\n",GetLastError());
1729 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1730 return -1;
1733 memset(buf,0,sizeof(struct MSVCRT__stati64));
1735 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1736 Bon 011120: This FIXME seems incorrect
1737 Also a letter as first char isn't enough to be classified
1738 as a drive letter
1740 if (isalpha(*path)&& (*(path+1)==':'))
1741 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1742 else
1743 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1745 plen = strlen(path);
1747 /* Dir, or regular file? */
1748 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1749 (path[plen-1] == '\\'))
1750 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1751 else
1753 mode |= MSVCRT__S_IFREG;
1754 /* executable? */
1755 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1757 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1758 (tolower(path[plen-3]) << 16);
1759 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1760 mode |= ALL_S_IEXEC;
1764 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1765 mode |= ALL_S_IWRITE;
1767 buf->st_mode = mode;
1768 buf->st_nlink = 1;
1769 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1770 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1771 buf->st_atime = dw;
1772 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1773 buf->st_mtime = buf->st_ctime = dw;
1774 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1775 (long)(buf->st_size >> 32),(long)buf->st_size,
1776 buf->st_atime,buf->st_mtime, buf->st_ctime);
1777 return 0;
1780 /*********************************************************************
1781 * _stat (MSVCRT.@)
1783 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1784 { int ret;
1785 struct MSVCRT__stati64 bufi64;
1787 ret = MSVCRT__stati64( path, &bufi64);
1788 if (!ret)
1789 msvcrt_cp_from_stati64(&bufi64, buf);
1790 return ret;
1793 /*********************************************************************
1794 * _wstati64 (MSVCRT.@)
1796 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1798 DWORD dw;
1799 WIN32_FILE_ATTRIBUTE_DATA hfi;
1800 unsigned short mode = ALL_S_IREAD;
1801 int plen;
1803 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1805 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1807 TRACE("failed (%ld)\n",GetLastError());
1808 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1809 return -1;
1812 memset(buf,0,sizeof(struct MSVCRT__stat));
1814 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1815 if (MSVCRT_iswalpha(*path))
1816 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1817 else
1818 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1820 plen = strlenW(path);
1822 /* Dir, or regular file? */
1823 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1824 (path[plen-1] == '\\'))
1825 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1826 else
1828 mode |= MSVCRT__S_IFREG;
1829 /* executable? */
1830 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1832 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1833 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1834 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1835 mode |= ALL_S_IEXEC;
1839 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1840 mode |= ALL_S_IWRITE;
1842 buf->st_mode = mode;
1843 buf->st_nlink = 1;
1844 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1845 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1846 buf->st_atime = dw;
1847 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1848 buf->st_mtime = buf->st_ctime = dw;
1849 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1850 (long)(buf->st_size >> 32),(long)buf->st_size,
1851 buf->st_atime,buf->st_mtime, buf->st_ctime);
1852 return 0;
1855 /*********************************************************************
1856 * _wstat (MSVCRT.@)
1858 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1860 int ret;
1861 struct MSVCRT__stati64 bufi64;
1863 ret = MSVCRT__wstati64( path, &bufi64 );
1864 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1865 return ret;
1868 /*********************************************************************
1869 * _tell (MSVCRT.@)
1871 long _tell(int fd)
1873 return _lseek(fd, 0, SEEK_CUR);
1876 /*********************************************************************
1877 * _telli64 (MSVCRT.@)
1879 __int64 _telli64(int fd)
1881 return _lseeki64(fd, 0, SEEK_CUR);
1884 /*********************************************************************
1885 * _tempnam (MSVCRT.@)
1887 char *_tempnam(const char *dir, const char *prefix)
1889 char tmpbuf[MAX_PATH];
1891 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1892 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1894 TRACE("got name (%s)\n",tmpbuf);
1895 DeleteFileA(tmpbuf);
1896 return _strdup(tmpbuf);
1898 TRACE("failed (%ld)\n",GetLastError());
1899 return NULL;
1902 /*********************************************************************
1903 * _wtempnam (MSVCRT.@)
1905 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1907 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1909 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1910 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1912 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1913 DeleteFileW(tmpbuf);
1914 return _wcsdup(tmpbuf);
1916 TRACE("failed (%ld)\n",GetLastError());
1917 return NULL;
1920 /*********************************************************************
1921 * _umask (MSVCRT.@)
1923 int _umask(int umask)
1925 int old_umask = MSVCRT_umask;
1926 TRACE("(%d)\n",umask);
1927 MSVCRT_umask = umask;
1928 return old_umask;
1931 /*********************************************************************
1932 * _utime (MSVCRT.@)
1934 int _utime(const char* path, struct MSVCRT__utimbuf *t)
1936 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1938 if (fd > 0)
1940 int retVal = _futime(fd, t);
1941 _close(fd);
1942 return retVal;
1944 return -1;
1947 /*********************************************************************
1948 * _wutime (MSVCRT.@)
1950 int _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1952 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1954 if (fd > 0)
1956 int retVal = _futime(fd, t);
1957 _close(fd);
1958 return retVal;
1960 return -1;
1963 /*********************************************************************
1964 * _write (MSVCRT.@)
1966 int _write(int fd, const void* buf, unsigned int count)
1968 DWORD num_written;
1969 HANDLE hand = msvcrt_fdtoh(fd);
1971 /* Don't trace small writes, it gets *very* annoying */
1972 #if 0
1973 if (count > 32)
1974 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1975 #endif
1976 if (hand == INVALID_HANDLE_VALUE)
1978 *MSVCRT__errno() = MSVCRT_EBADF;
1979 return -1;
1982 /* If appending, go to EOF */
1983 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
1984 _lseek(fd, 0, FILE_END);
1986 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
1988 if (WriteFile(hand, buf, count, &num_written, NULL)
1989 && (num_written == count))
1990 return num_written;
1991 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd,
1992 hand, GetLastError());
1993 *MSVCRT__errno() = MSVCRT_ENOSPC;
1995 else
1997 unsigned int i, j, nr_lf;
1998 char *s =(char*)buf, *buf_start=(char*)buf, *p;
1999 /* find number of \n ( without preceding \r ) */
2000 for ( nr_lf=0,i = 0; i <count; i++)
2002 if (s[i]== '\n')
2004 nr_lf++;
2005 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2008 if (nr_lf)
2010 if ((p = MSVCRT_malloc(count + nr_lf)))
2012 for(s=(char*)buf, i=0, j=0; i<count; i++)
2014 if (s[i]== '\n')
2016 p[j++] = '\r';
2017 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2019 p[j++] = s[i];
2022 else
2024 FIXME("Malloc failed\n");
2025 nr_lf =0;
2026 p = (char*)buf;
2029 else
2030 p = (char*)buf;
2032 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2034 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld), num_written %ld\n",
2035 fd, hand, GetLastError(), num_written);
2036 *MSVCRT__errno() = MSVCRT_ENOSPC;
2037 if(nr_lf)
2038 MSVCRT_free(p);
2039 return s - buf_start;
2041 else
2043 if(nr_lf)
2044 MSVCRT_free(p);
2045 return count;
2048 return -1;
2051 /*********************************************************************
2052 * _putw (MSVCRT.@)
2054 int MSVCRT__putw(int val, MSVCRT_FILE* file)
2056 int len;
2057 len = _write(file->_file, &val, sizeof(val));
2058 if (len == sizeof(val)) return val;
2059 file->_flag |= MSVCRT__IOERR;
2060 return MSVCRT_EOF;
2063 /*********************************************************************
2064 * fclose (MSVCRT.@)
2066 int MSVCRT_fclose(MSVCRT_FILE* file)
2068 int r, flag;
2070 flag = file->_flag;
2071 if (file->_tmpfname)
2073 MSVCRT_free(file->_tmpfname);
2074 file->_tmpfname = NULL;
2076 /* flush stdio buffers */
2077 if(file->_flag & MSVCRT__IOWRT)
2078 MSVCRT_fflush(file);
2079 if(file->_flag & MSVCRT__IOMYBUF)
2080 MSVCRT_free(file->_base);
2082 r=_close(file->_file);
2084 file->_flag = 0;
2086 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2089 /*********************************************************************
2090 * feof (MSVCRT.@)
2092 int MSVCRT_feof(MSVCRT_FILE* file)
2094 return file->_flag & MSVCRT__IOEOF;
2097 /*********************************************************************
2098 * ferror (MSVCRT.@)
2100 int MSVCRT_ferror(MSVCRT_FILE* file)
2102 return file->_flag & MSVCRT__IOERR;
2105 /*********************************************************************
2106 * _filbuf (MSVCRT.@)
2108 int MSVCRT__filbuf(MSVCRT_FILE* file)
2110 /* Allocate buffer if needed */
2111 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2112 msvcrt_alloc_buffer(file);
2114 if(!(file->_flag & MSVCRT__IOREAD)) {
2115 if(file->_flag & MSVCRT__IORW) {
2116 file->_flag |= MSVCRT__IOREAD;
2117 } else {
2118 return MSVCRT_EOF;
2121 if(file->_flag & MSVCRT__IONBF) {
2122 unsigned char c;
2123 int r;
2124 if ((r = _read(file->_file,&c,1)) != 1) {
2125 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2126 return MSVCRT_EOF;
2128 return c;
2129 } else {
2130 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2131 if(file->_cnt<=0) {
2132 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2133 file->_cnt = 0;
2134 return MSVCRT_EOF;
2136 file->_cnt--;
2137 file->_ptr = file->_base+1;
2138 return *(unsigned char *)file->_base;
2142 /*********************************************************************
2143 * fgetc (MSVCRT.@)
2145 int MSVCRT_fgetc(MSVCRT_FILE* file)
2147 if (file->_cnt>0) {
2148 file->_cnt--;
2149 return *(unsigned char *)file->_ptr++;
2150 } else {
2151 return MSVCRT__filbuf(file);
2155 /*********************************************************************
2156 * _fgetchar (MSVCRT.@)
2158 int _fgetchar(void)
2160 return MSVCRT_fgetc(MSVCRT_stdin);
2163 /*********************************************************************
2164 * fgets (MSVCRT.@)
2166 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2168 int cc = MSVCRT_EOF;
2169 char * buf_start = s;
2171 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2172 file,file->_file,s,size);
2174 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2176 *s++ = (char)cc;
2177 size --;
2179 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2181 TRACE(":nothing read\n");
2182 return NULL;
2184 if ((cc != MSVCRT_EOF) && (size > 1))
2185 *s++ = cc;
2186 *s = '\0';
2187 TRACE(":got %s\n", debugstr_a(buf_start));
2188 return buf_start;
2191 /*********************************************************************
2192 * fgetwc (MSVCRT.@)
2194 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2195 * the CR from CR/LF combinations
2197 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
2199 char c;
2201 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2203 MSVCRT_wchar_t wc;
2204 int r;
2205 if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
2207 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2208 return MSVCRT_WEOF;
2210 return wc;
2212 c = MSVCRT_fgetc(file);
2213 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2215 FIXME("Treat Multibyte characters\n");
2217 if (c == MSVCRT_EOF)
2218 return MSVCRT_WEOF;
2219 else
2220 return (MSVCRT_wint_t)c;
2223 /*********************************************************************
2224 * getwc (MSVCRT.@)
2226 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
2228 return MSVCRT_fgetwc(file);
2231 /*********************************************************************
2232 * _fgetwchar (MSVCRT.@)
2234 MSVCRT_wint_t _fgetwchar(void)
2236 return MSVCRT_fgetwc(MSVCRT_stdin);
2239 /*********************************************************************
2240 * getwchar (MSVCRT.@)
2242 MSVCRT_wint_t MSVCRT_getwchar(void)
2244 return _fgetwchar();
2247 /*********************************************************************
2248 * fgetws (MSVCRT.@)
2250 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2252 int cc = MSVCRT_WEOF;
2253 MSVCRT_wchar_t * buf_start = s;
2255 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2256 file,file->_file,s,size);
2258 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2260 *s++ = (char)cc;
2261 size --;
2263 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2265 TRACE(":nothing read\n");
2266 return NULL;
2268 if ((cc != MSVCRT_WEOF) && (size > 1))
2269 *s++ = cc;
2270 *s = 0;
2271 TRACE(":got %s\n", debugstr_w(buf_start));
2272 return buf_start;
2275 /*********************************************************************
2276 * fwrite (MSVCRT.@)
2278 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2280 MSVCRT_size_t wrcnt=size * nmemb;
2281 int written = 0;
2282 if (size == 0)
2283 return 0;
2284 if(file->_cnt) {
2285 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2286 memcpy(file->_ptr, ptr, pcnt);
2287 file->_cnt -= pcnt;
2288 file->_ptr += pcnt;
2289 written = pcnt;
2290 wrcnt -= pcnt;
2291 ptr = (const char*)ptr + pcnt;
2292 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2293 if(file->_flag & MSVCRT__IORW) {
2294 file->_flag |= MSVCRT__IOWRT;
2295 } else
2296 return 0;
2298 if(wrcnt) {
2299 /* Flush buffer */
2300 int res=msvcrt_flush_buffer(file);
2301 if(!res) {
2302 int pwritten = _write(file->_file, ptr, wrcnt);
2303 if (pwritten <= 0)
2305 file->_flag |= MSVCRT__IOERR;
2306 pwritten=0;
2308 written += pwritten;
2311 return written / size;
2314 /*********************************************************************
2315 * fputwc (MSVCRT.@)
2317 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2319 MSVCRT_wchar_t mwc=wc;
2320 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2321 return MSVCRT_WEOF;
2322 return wc;
2325 /*********************************************************************
2326 * _fputwchar (MSVCRT.@)
2328 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
2330 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2333 /*********************************************************************
2334 * fopen (MSVCRT.@)
2336 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
2338 MSVCRT_FILE* file;
2339 int open_flags, stream_flags, fd;
2341 TRACE("(%s,%s)\n",path,mode);
2343 /* map mode string to open() flags. "man fopen" for possibilities. */
2344 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2345 return NULL;
2347 LOCK_FILES();
2348 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2349 if (fd < 0)
2350 file = NULL;
2351 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2352 != -1)
2353 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2354 else if (file)
2356 file->_flag = 0;
2357 file = NULL;
2360 TRACE(":got (%p)\n",file);
2361 if (fd >= 0 && !file)
2362 _close(fd);
2363 UNLOCK_FILES();
2364 return file;
2367 /*********************************************************************
2368 * _wfopen (MSVCRT.@)
2370 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2372 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2373 char *patha = MSVCRT_calloc(plen + 1, 1);
2374 char *modea = MSVCRT_calloc(mlen + 1, 1);
2376 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2378 if (patha && modea &&
2379 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2380 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2382 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2383 MSVCRT_free(patha);
2384 MSVCRT_free(modea);
2385 return retval;
2388 msvcrt_set_errno(GetLastError());
2389 return NULL;
2392 /*********************************************************************
2393 * _fsopen (MSVCRT.@)
2395 MSVCRT_FILE* MSVCRT__fsopen(const char *path, const char *mode, int share)
2397 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2398 return MSVCRT_fopen(path,mode);
2401 /*********************************************************************
2402 * _wfsopen (MSVCRT.@)
2404 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2406 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2407 debugstr_w(path),debugstr_w(mode),share);
2408 return MSVCRT__wfopen(path,mode);
2411 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2412 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2414 /*********************************************************************
2415 * fputc (MSVCRT.@)
2417 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2419 if(file->_cnt>0) {
2420 *file->_ptr++=c;
2421 file->_cnt--;
2422 if (c == '\n')
2424 int res = msvcrt_flush_buffer(file);
2425 return res ? res : c;
2427 else
2428 return c;
2429 } else {
2430 return MSVCRT__flsbuf(c, file);
2434 /*********************************************************************
2435 * _flsbuf (MSVCRT.@)
2437 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2439 /* Flush output buffer */
2440 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2441 msvcrt_alloc_buffer(file);
2443 if(!(file->_flag & MSVCRT__IOWRT)) {
2444 if(file->_flag & MSVCRT__IORW) {
2445 file->_flag |= MSVCRT__IOWRT;
2446 } else {
2447 return MSVCRT_EOF;
2450 if(file->_bufsiz) {
2451 int res=msvcrt_flush_buffer(file);
2452 return res?res : MSVCRT_fputc(c, file);
2453 } else {
2454 unsigned char cc=c;
2455 int len;
2456 len = _write(file->_file, &cc, 1);
2457 if (len == 1) return c;
2458 file->_flag |= MSVCRT__IOERR;
2459 return MSVCRT_EOF;
2463 /*********************************************************************
2464 * _fputchar (MSVCRT.@)
2466 int _fputchar(int c)
2468 return MSVCRT_fputc(c, MSVCRT_stdout);
2471 /*********************************************************************
2472 * fread (MSVCRT.@)
2474 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2475 { MSVCRT_size_t rcnt=size * nmemb;
2476 MSVCRT_size_t read=0;
2477 int pread=0;
2479 if(!rcnt)
2480 return 0;
2482 /* first buffered data */
2483 if(file->_cnt>0) {
2484 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2485 memcpy(ptr, file->_ptr, pcnt);
2486 file->_cnt -= pcnt;
2487 file->_ptr += pcnt;
2488 read += pcnt ;
2489 rcnt -= pcnt ;
2490 ptr = (char*)ptr + pcnt;
2491 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2492 if(file->_flag & MSVCRT__IORW) {
2493 file->_flag |= MSVCRT__IOREAD;
2494 } else
2495 return 0;
2497 if(rcnt)
2499 pread = _read(file->_file,ptr, rcnt);
2500 /* expose feof condition in the flags
2501 * MFC tests file->_flag for feof, and doesn't not call feof())
2503 if (pread == 0)
2504 file->_flag |= MSVCRT__IOEOF;
2505 else if (pread == -1)
2507 file->_flag |= MSVCRT__IOERR;
2508 pread = 0;
2511 read+=pread;
2512 return read / size;
2515 /*********************************************************************
2516 * freopen (MSVCRT.@)
2519 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2521 int open_flags, stream_flags, fd;
2523 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2525 LOCK_FILES();
2526 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2527 file = NULL;
2528 else
2530 MSVCRT_fclose(file);
2531 /* map mode string to open() flags. "man fopen" for possibilities. */
2532 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2533 file = NULL;
2534 else
2536 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2537 if (fd < 0)
2538 file = NULL;
2539 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2541 file->_flag = 0;
2542 WARN(":failed-last error (%ld)\n",GetLastError());
2543 msvcrt_set_errno(GetLastError());
2544 file = NULL;
2548 UNLOCK_FILES();
2549 return file;
2552 /*********************************************************************
2553 * fsetpos (MSVCRT.@)
2555 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2557 /* Note that all this has been lifted 'as is' from fseek */
2558 if(file->_flag & MSVCRT__IOWRT)
2559 msvcrt_flush_buffer(file);
2561 /* Discard buffered input */
2562 file->_cnt = 0;
2563 file->_ptr = file->_base;
2565 /* Reset direction of i/o */
2566 if(file->_flag & MSVCRT__IORW) {
2567 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2570 return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2573 /*********************************************************************
2574 * ftell (MSVCRT.@)
2576 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2578 int off=0;
2579 long pos;
2580 if(file->_bufsiz) {
2581 if( file->_flag & MSVCRT__IOWRT ) {
2582 off = file->_ptr - file->_base;
2583 } else {
2584 off = -file->_cnt;
2587 pos = _tell(file->_file);
2588 if(pos == -1) return pos;
2589 return off + pos;
2592 /*********************************************************************
2593 * fgetpos (MSVCRT.@)
2595 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2597 /* This code has been lifted form the MSVCRT_ftell function */
2598 int off=0;
2600 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2602 if (*pos == -1) return -1;
2604 if(file->_bufsiz) {
2605 if( file->_flag & MSVCRT__IOWRT ) {
2606 off = file->_ptr - file->_base;
2607 } else {
2608 off = -file->_cnt;
2611 *pos += off;
2613 return 0;
2616 /*********************************************************************
2617 * fputs (MSVCRT.@)
2619 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2621 size_t i, len = strlen(s);
2622 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2623 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2624 for (i=0; i<len; i++)
2625 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2626 return MSVCRT_EOF;
2627 return 0;
2630 /*********************************************************************
2631 * fputws (MSVCRT.@)
2633 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2635 size_t i, len = strlenW(s);
2636 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2637 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2638 for (i=0; i<len; i++)
2640 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2641 return MSVCRT_WEOF;
2642 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2643 return MSVCRT_WEOF;
2645 return 0;
2648 /*********************************************************************
2649 * getchar (MSVCRT.@)
2651 int MSVCRT_getchar(void)
2653 return MSVCRT_fgetc(MSVCRT_stdin);
2656 /*********************************************************************
2657 * getc (MSVCRT.@)
2659 int MSVCRT_getc(MSVCRT_FILE* file)
2661 return MSVCRT_fgetc(file);
2664 /*********************************************************************
2665 * gets (MSVCRT.@)
2667 char *MSVCRT_gets(char *buf)
2669 int cc;
2670 char * buf_start = buf;
2672 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2673 cc = MSVCRT_fgetc(MSVCRT_stdin))
2674 if(cc != '\r') *buf++ = (char)cc;
2676 *buf = '\0';
2678 TRACE("got '%s'\n", buf_start);
2679 return buf_start;
2682 /*********************************************************************
2683 * _getws (MSVCRT.@)
2685 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2687 MSVCRT_wint_t cc;
2688 MSVCRT_wchar_t* ws = buf;
2690 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2691 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2693 if (cc != '\r')
2694 *buf++ = (MSVCRT_wchar_t)cc;
2696 *buf = '\0';
2698 TRACE("got '%s'\n", debugstr_w(ws));
2699 return ws;
2702 /*********************************************************************
2703 * putc (MSVCRT.@)
2705 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2707 return MSVCRT_fputc(c, file);
2710 /*********************************************************************
2711 * putchar (MSVCRT.@)
2713 int MSVCRT_putchar(int c)
2715 return MSVCRT_fputc(c, MSVCRT_stdout);
2718 /*********************************************************************
2719 * puts (MSVCRT.@)
2721 int MSVCRT_puts(const char *s)
2723 size_t len = strlen(s);
2724 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2725 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2728 /*********************************************************************
2729 * _putws (MSVCRT.@)
2731 int _putws(const MSVCRT_wchar_t *s)
2733 static const MSVCRT_wchar_t nl = '\n';
2734 size_t len = strlenW(s);
2735 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2736 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2739 /*********************************************************************
2740 * remove (MSVCRT.@)
2742 int MSVCRT_remove(const char *path)
2744 TRACE("(%s)\n",path);
2745 if (DeleteFileA(path))
2746 return 0;
2747 TRACE(":failed (%ld)\n",GetLastError());
2748 msvcrt_set_errno(GetLastError());
2749 return -1;
2752 /*********************************************************************
2753 * _wremove (MSVCRT.@)
2755 int _wremove(const MSVCRT_wchar_t *path)
2757 TRACE("(%s)\n",debugstr_w(path));
2758 if (DeleteFileW(path))
2759 return 0;
2760 TRACE(":failed (%ld)\n",GetLastError());
2761 msvcrt_set_errno(GetLastError());
2762 return -1;
2765 /*********************************************************************
2766 * rename (MSVCRT.@)
2768 int MSVCRT_rename(const char *oldpath,const char *newpath)
2770 TRACE(":from %s to %s\n",oldpath,newpath);
2771 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2772 return 0;
2773 TRACE(":failed (%ld)\n",GetLastError());
2774 msvcrt_set_errno(GetLastError());
2775 return -1;
2778 /*********************************************************************
2779 * _wrename (MSVCRT.@)
2781 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2783 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2784 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2785 return 0;
2786 TRACE(":failed (%ld)\n",GetLastError());
2787 msvcrt_set_errno(GetLastError());
2788 return -1;
2791 /*********************************************************************
2792 * setvbuf (MSVCRT.@)
2794 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2796 /* TODO: Check if file busy */
2797 if(file->_bufsiz) {
2798 MSVCRT_free(file->_base);
2799 file->_bufsiz = 0;
2800 file->_cnt = 0;
2802 if(mode == MSVCRT__IOFBF) {
2803 file->_flag &= ~MSVCRT__IONBF;
2804 file->_base = file->_ptr = buf;
2805 if(buf) {
2806 file->_bufsiz = size;
2808 } else {
2809 file->_flag |= MSVCRT__IONBF;
2811 return 0;
2814 /*********************************************************************
2815 * setbuf (MSVCRT.@)
2817 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2819 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2822 /*********************************************************************
2823 * tmpnam (MSVCRT.@)
2825 char *MSVCRT_tmpnam(char *s)
2827 static int unique;
2828 char tmpstr[16];
2829 char *p;
2830 int count;
2831 if (s == 0)
2832 s = MSVCRT_tmpname;
2833 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2834 p = s + sprintf(s, "\\s%s.", tmpstr);
2835 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2837 msvcrt_int_to_base32(unique++, tmpstr);
2838 strcpy(p, tmpstr);
2839 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2840 GetLastError() == ERROR_FILE_NOT_FOUND)
2841 break;
2843 return s;
2846 /*********************************************************************
2847 * tmpfile (MSVCRT.@)
2849 MSVCRT_FILE* MSVCRT_tmpfile(void)
2851 char *filename = MSVCRT_tmpnam(NULL);
2852 int fd;
2853 MSVCRT_FILE* file = NULL;
2855 LOCK_FILES();
2856 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2857 if (fd != -1 && (file = msvcrt_alloc_fp()))
2859 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2861 file->_flag = 0;
2862 file = NULL;
2864 else file->_tmpfname = _strdup(filename);
2866 UNLOCK_FILES();
2867 return file;
2870 /*********************************************************************
2871 * vfprintf (MSVCRT.@)
2873 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2875 char buf[2048], *mem = buf;
2876 int written, resize = sizeof(buf), retval;
2877 /* There are two conventions for vsnprintf failing:
2878 * Return -1 if we truncated, or
2879 * Return the number of bytes that would have been written
2880 * The code below handles both cases
2882 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
2883 written > resize)
2885 resize = (written == -1 ? resize * 2 : written + 1);
2886 if (mem != buf)
2887 MSVCRT_free (mem);
2888 if (!(mem = (char *)MSVCRT_malloc(resize)))
2889 return MSVCRT_EOF;
2891 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2892 if (mem != buf)
2893 MSVCRT_free (mem);
2894 return retval;
2897 /*********************************************************************
2898 * vfwprintf (MSVCRT.@)
2899 * FIXME:
2900 * Is final char included in written (then resize is too big) or not
2901 * (then we must test for equality too)?
2903 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2905 MSVCRT_wchar_t buf[2048], *mem = buf;
2906 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2907 /* See vfprintf comments */
2908 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2909 written > resize)
2911 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2912 if (mem != buf)
2913 MSVCRT_free (mem);
2914 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2915 return MSVCRT_EOF;
2917 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2918 if (mem != buf)
2919 MSVCRT_free (mem);
2920 return retval;
2923 /*********************************************************************
2924 * vprintf (MSVCRT.@)
2926 int MSVCRT_vprintf(const char *format, va_list valist)
2928 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2931 /*********************************************************************
2932 * vwprintf (MSVCRT.@)
2934 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2936 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2939 /*********************************************************************
2940 * fprintf (MSVCRT.@)
2942 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2944 va_list valist;
2945 int res;
2946 va_start(valist, format);
2947 res = MSVCRT_vfprintf(file, format, valist);
2948 va_end(valist);
2949 return res;
2952 /*********************************************************************
2953 * fwprintf (MSVCRT.@)
2955 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2957 va_list valist;
2958 int res;
2959 va_start(valist, format);
2960 res = MSVCRT_vfwprintf(file, format, valist);
2961 va_end(valist);
2962 return res;
2965 /*********************************************************************
2966 * printf (MSVCRT.@)
2968 int MSVCRT_printf(const char *format, ...)
2970 va_list valist;
2971 int res;
2972 va_start(valist, format);
2973 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2974 va_end(valist);
2975 return res;
2978 /*********************************************************************
2979 * ungetc (MSVCRT.@)
2981 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2983 if (c == MSVCRT_EOF)
2984 return MSVCRT_EOF;
2985 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2986 msvcrt_alloc_buffer(file);
2987 file->_ptr++;
2989 if(file->_ptr>file->_base) {
2990 file->_ptr--;
2991 *file->_ptr=c;
2992 file->_cnt++;
2993 MSVCRT_clearerr(file);
2994 return c;
2996 return MSVCRT_EOF;
2999 /*********************************************************************
3000 * ungetwc (MSVCRT.@)
3002 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3004 MSVCRT_wchar_t mwc = wc;
3005 char * pp = (char *)&mwc;
3006 int i;
3007 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3008 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3009 return MSVCRT_WEOF;
3011 return mwc;
3014 /*********************************************************************
3015 * wprintf (MSVCRT.@)
3017 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3019 va_list valist;
3020 int res;
3021 va_start(valist, format);
3022 res = MSVCRT_vwprintf(format, valist);
3023 va_end(valist);
3024 return res;
3027 /*********************************************************************
3028 * __pioinfo (MSVCRT.@)
3029 * FIXME: see MSVCRT_MAX_FILES define.
3031 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3032 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3033 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3034 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3035 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3036 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3037 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3038 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3039 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3040 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3041 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3042 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3045 /*********************************************************************
3046 * __badioinfo (MSVCRT.@)
3048 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };