- implement __pioinfo, __badioinfo and _adjust_fdiv
[wine/multimedia.git] / dlls / msvcrt / file.c
blob1a693364a0fb82120bce1bb4501beac34115fa5c
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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <time.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #include <sys/types.h>
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winreg.h"
39 #include "winternl.h"
40 #include "msvcrt.h"
42 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
48 /* for stat mode, permissions apply to all,owner and group */
49 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
50 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
51 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
53 /* _access() bit flags FIXME: incomplete */
54 #define MSVCRT_W_OK 0x02
56 /* values for wxflag in file descriptor */
57 #define WX_OPEN 0x01
58 #define WX_ATEOF 0x02
59 #define WX_DONTINHERIT 0x10
60 #define WX_APPEND 0x20
61 #define WX_TEXT 0x80
63 /* FIXME: this should be allocated dynamically */
64 #define MSVCRT_MAX_FILES 2048
66 typedef struct {
67 HANDLE handle;
68 unsigned char wxflag;
69 DWORD unkn[7]; /* critical section and init flag */
70 } ioinfo;
72 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
74 MSVCRT_FILE MSVCRT__iob[3];
76 static int MSVCRT_fdstart = 3; /* first unallocated fd */
77 static int MSVCRT_fdend = 3; /* highest allocated fd */
79 static MSVCRT_FILE* MSVCRT_fstreams[2048];
80 static int MSVCRT_stream_idx;
82 /* INTERNAL: process umask */
83 static int MSVCRT_umask = 0;
85 /* INTERNAL: Static buffer for temp file name */
86 static char MSVCRT_tmpname[MAX_PATH];
88 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
89 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
90 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
91 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
93 #define TOUL(x) (ULONGLONG)(x)
94 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
95 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
96 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
97 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
99 extern CRITICAL_SECTION MSVCRT_file_cs;
100 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
101 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
103 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64 *bufi64, struct MSVCRT__stat *buf)
105 buf->st_dev = bufi64->st_dev;
106 buf->st_ino = bufi64->st_ino;
107 buf->st_mode = bufi64->st_mode;
108 buf->st_nlink = bufi64->st_nlink;
109 buf->st_uid = bufi64->st_uid;
110 buf->st_gid = bufi64->st_gid;
111 buf->st_rdev = bufi64->st_rdev;
112 buf->st_size = bufi64->st_size;
113 buf->st_atime = bufi64->st_atime;
114 buf->st_mtime = bufi64->st_mtime;
115 buf->st_ctime = bufi64->st_ctime;
118 static inline BOOL msvcrt_is_valid_fd(int fd)
120 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
123 /* INTERNAL: Get the HANDLE for a fd */
124 static HANDLE msvcrt_fdtoh(int fd)
126 if (!msvcrt_is_valid_fd(fd))
128 WARN(":fd (%d) - no handle!\n",fd);
129 *MSVCRT___doserrno() = 0;
130 *MSVCRT__errno() = MSVCRT_EBADF;
131 return INVALID_HANDLE_VALUE;
133 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
134 return MSVCRT_fdesc[fd].handle;
137 /* INTERNAL: free a file entry fd */
138 static void msvcrt_free_fd(int fd)
140 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
141 MSVCRT_fdesc[fd].wxflag = 0;
142 TRACE(":fd (%d) freed\n",fd);
143 if (fd < 3) /* don't use 0,1,2 for user files */
145 switch (fd)
147 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
148 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
149 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
152 else
154 if (fd == MSVCRT_fdend - 1)
155 MSVCRT_fdend--;
156 if (fd < MSVCRT_fdstart)
157 MSVCRT_fdstart = fd;
161 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
162 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
164 if (fd >= MSVCRT_MAX_FILES)
166 WARN(":files exhausted!\n");
167 return -1;
169 MSVCRT_fdesc[fd].handle = hand;
170 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
172 /* locate next free slot */
173 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
174 MSVCRT_fdstart = MSVCRT_fdend + 1;
175 else
176 while (MSVCRT_fdstart < MSVCRT_fdend &&
177 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
178 MSVCRT_fdstart++;
179 /* update last fd in use */
180 if (fd >= MSVCRT_fdend)
181 MSVCRT_fdend = fd + 1;
182 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
184 switch (fd)
186 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
187 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
188 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
191 return fd;
194 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
195 static int msvcrt_alloc_fd(HANDLE hand, int flag)
197 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
198 return msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
201 /* INTERNAL: Allocate a FILE* for an fd slot
203 static MSVCRT_FILE* msvcrt_alloc_fp(void)
205 unsigned int i;
207 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
209 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
211 if (!MSVCRT_fstreams[i])
213 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
214 return NULL;
215 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
217 return MSVCRT_fstreams[i];
220 return NULL;
223 /* INTERNAL: initialize a FILE* from an open fd */
224 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
226 TRACE(":fd (%d) allocating FILE*\n",fd);
227 if (!msvcrt_is_valid_fd(fd))
229 WARN(":invalid fd %d\n",fd);
230 *MSVCRT___doserrno() = 0;
231 *MSVCRT__errno() = MSVCRT_EBADF;
232 return -1;
234 memset(file, 0, sizeof(*file));
235 file->_file = fd;
236 file->_flag = stream_flags;
238 TRACE(":got FILE* (%p)\n",file);
239 return 0;
242 /* INTERNAL: Create an inheritance data block (for spawned process)
243 * The inheritance block is made of:
244 * 00 int nb of file descriptor (NBFD)
245 * 04 char file flags (wxflag): repeated for each fd
246 * 4+NBFD HANDLE file handle: repeated for each fd
248 unsigned msvcrt_create_io_inherit_block(STARTUPINFOA* si)
250 int fd;
251 char* wxflag_ptr;
252 HANDLE* handle_ptr;
254 si->cbReserved2 = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
255 si->lpReserved2 = MSVCRT_calloc(si->cbReserved2, 1);
256 if (!si->lpReserved2)
258 si->cbReserved2 = 0;
259 return FALSE;
261 wxflag_ptr = (char*)si->lpReserved2 + sizeof(unsigned);
262 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
264 *(unsigned*)si->lpReserved2 = MSVCRT_fdend;
265 for (fd = 0; fd < MSVCRT_fdend; fd++)
267 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
268 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
270 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
271 *handle_ptr = MSVCRT_fdesc[fd].handle;
273 else
275 *wxflag_ptr = 0;
276 *handle_ptr = INVALID_HANDLE_VALUE;
278 wxflag_ptr++; handle_ptr++;
280 return TRUE;
283 /* INTERNAL: Set up all file descriptors,
284 * as well as default streams (stdin, stderr and stdout)
286 void msvcrt_init_io(void)
288 STARTUPINFOA si;
289 int i;
291 GetStartupInfoA(&si);
292 if (si.cbReserved2 != 0 && si.lpReserved2 != NULL)
294 char* wxflag_ptr;
295 HANDLE* handle_ptr;
297 MSVCRT_fdend = *(unsigned*)si.lpReserved2;
299 wxflag_ptr = (char*)(si.lpReserved2 + sizeof(unsigned));
300 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
302 MSVCRT_fdend = min(MSVCRT_fdend, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
303 for (i = 0; i < MSVCRT_fdend; i++)
305 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
307 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
308 MSVCRT_fdesc[i].handle = *handle_ptr;
310 else
312 MSVCRT_fdesc[i].wxflag = 0;
313 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
315 wxflag_ptr++; handle_ptr++;
317 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
318 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
321 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
323 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
324 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, FALSE,
325 DUPLICATE_SAME_ACCESS);
326 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
328 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
330 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
331 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, FALSE,
332 DUPLICATE_SAME_ACCESS);
333 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
335 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
337 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
338 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, FALSE,
339 DUPLICATE_SAME_ACCESS);
340 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
343 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
344 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
346 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
347 for (i = 0; i < 3; i++)
349 /* FILE structs for stdin/out/err are static and never deleted */
350 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
351 MSVCRT__iob[i]._file = i;
352 MSVCRT__iob[i]._tmpfname = NULL;
353 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
355 MSVCRT_stream_idx = 3;
358 /* INTERNAL: Flush stdio file buffer */
359 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
361 if(file->_bufsiz) {
362 int cnt=file->_ptr-file->_base;
363 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
364 file->_flag |= MSVCRT__IOERR;
365 return MSVCRT_EOF;
367 file->_ptr=file->_base;
368 file->_cnt=file->_bufsiz;
370 return 0;
373 /* INTERNAL: Allocate stdio file buffer */
374 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
376 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
377 if(file->_base) {
378 file->_bufsiz = MSVCRT_BUFSIZ;
379 file->_flag |= MSVCRT__IOMYBUF;
380 } else {
381 file->_base = (unsigned char *)(&file->_charbuf);
382 /* put here 2 ??? */
383 file->_bufsiz = sizeof(file->_charbuf);
385 file->_ptr = file->_base;
386 file->_cnt = 0;
389 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
390 static void msvcrt_int_to_base32(int num, char *str)
392 char *p;
393 int n = num;
394 int digits = 0;
396 while (n != 0)
398 n >>= 5;
399 digits++;
401 p = str + digits;
402 *p = 0;
403 while (--p >= str)
405 *p = (num & 31) + '0';
406 if (*p > '9')
407 *p += ('a' - '0' - 10);
408 num >>= 5;
412 /*********************************************************************
413 * __p__iob(MSVCRT.@)
415 MSVCRT_FILE *__p__iob(void)
417 return &MSVCRT__iob[0];
420 /*********************************************************************
421 * _access (MSVCRT.@)
423 int _access(const char *filename, int mode)
425 DWORD attr = GetFileAttributesA(filename);
427 TRACE("(%s,%d) %ld\n",filename,mode,attr);
429 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
431 msvcrt_set_errno(GetLastError());
432 return -1;
434 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
436 msvcrt_set_errno(ERROR_ACCESS_DENIED);
437 return -1;
439 return 0;
442 /*********************************************************************
443 * _waccess (MSVCRT.@)
445 int _waccess(const MSVCRT_wchar_t *filename, int mode)
447 DWORD attr = GetFileAttributesW(filename);
449 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
451 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
453 msvcrt_set_errno(GetLastError());
454 return -1;
456 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
458 msvcrt_set_errno(ERROR_ACCESS_DENIED);
459 return -1;
461 return 0;
464 /*********************************************************************
465 * _chmod (MSVCRT.@)
467 int _chmod(const char *path, int flags)
469 DWORD oldFlags = GetFileAttributesA(path);
471 if (oldFlags != INVALID_FILE_ATTRIBUTES)
473 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
474 oldFlags | FILE_ATTRIBUTE_READONLY;
476 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
477 return 0;
479 msvcrt_set_errno(GetLastError());
480 return -1;
483 /*********************************************************************
484 * _wchmod (MSVCRT.@)
486 int _wchmod(const MSVCRT_wchar_t *path, int flags)
488 DWORD oldFlags = GetFileAttributesW(path);
490 if (oldFlags != INVALID_FILE_ATTRIBUTES)
492 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
493 oldFlags | FILE_ATTRIBUTE_READONLY;
495 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
496 return 0;
498 msvcrt_set_errno(GetLastError());
499 return -1;
502 /*********************************************************************
503 * _chsize (MSVCRT.@)
505 int _chsize(int fd, long size)
507 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
508 return -1;
511 /*********************************************************************
512 * _unlink (MSVCRT.@)
514 int _unlink(const char *path)
516 TRACE("(%s)\n",path);
517 if(DeleteFileA(path))
518 return 0;
519 TRACE("failed (%ld)\n",GetLastError());
520 msvcrt_set_errno(GetLastError());
521 return -1;
524 /*********************************************************************
525 * _wunlink (MSVCRT.@)
527 int _wunlink(const MSVCRT_wchar_t *path)
529 TRACE("(%s)\n",debugstr_w(path));
530 if(DeleteFileW(path))
531 return 0;
532 TRACE("failed (%ld)\n",GetLastError());
533 msvcrt_set_errno(GetLastError());
534 return -1;
537 /* _flushall calls MSVCRT_fflush which calls _flushall */
538 int MSVCRT_fflush(MSVCRT_FILE* file);
540 /*********************************************************************
541 * _flushall (MSVCRT.@)
543 int _flushall(void)
545 int i, num_flushed = 0;
547 for (i = 3; i < MSVCRT_stream_idx; i++)
548 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
550 #if 0
551 /* FIXME: flush, do not commit */
552 if (_commit(i) == -1)
553 if (MSVCRT_fstreams[i])
554 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
555 #endif
556 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
557 MSVCRT_fflush(MSVCRT_fstreams[i]);
558 num_flushed++;
562 TRACE(":flushed (%d) handles\n",num_flushed);
563 return num_flushed;
566 /*********************************************************************
567 * fflush (MSVCRT.@)
569 int MSVCRT_fflush(MSVCRT_FILE* file)
571 if(!file) {
572 _flushall();
573 return 0;
574 } else {
575 int res=msvcrt_flush_buffer(file);
576 return res;
580 /*********************************************************************
581 * _close (MSVCRT.@)
583 int _close(int fd)
585 HANDLE hand = msvcrt_fdtoh(fd);
587 TRACE(":fd (%d) handle (%p)\n",fd,hand);
588 if (hand == INVALID_HANDLE_VALUE)
589 return -1;
591 if (!CloseHandle(hand))
593 WARN(":failed-last error (%ld)\n",GetLastError());
594 msvcrt_set_errno(GetLastError());
595 return -1;
597 msvcrt_free_fd(fd);
598 TRACE(":ok\n");
599 return 0;
602 /*********************************************************************
603 * _commit (MSVCRT.@)
605 int _commit(int fd)
607 HANDLE hand = msvcrt_fdtoh(fd);
609 TRACE(":fd (%d) handle (%p)\n",fd,hand);
610 if (hand == INVALID_HANDLE_VALUE)
611 return -1;
613 if (!FlushFileBuffers(hand))
615 if (GetLastError() == ERROR_INVALID_HANDLE)
617 /* FlushFileBuffers fails for console handles
618 * so we ignore this error.
620 return 0;
622 TRACE(":failed-last error (%ld)\n",GetLastError());
623 msvcrt_set_errno(GetLastError());
624 return -1;
626 TRACE(":ok\n");
627 return 0;
630 /*********************************************************************
631 * _dup2 (MSVCRT.@)
632 * NOTES
633 * MSDN isn't clear on this point, but the remarks for _pipe,
634 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
635 * indicate file descriptors duplicated with _dup and _dup2 are always
636 * inheritable.
638 int _dup2(int od, int nd)
640 int ret;
642 TRACE("(od=%d, nd=%d)\n", od, nd);
643 if (nd < MSVCRT_MAX_FILES && msvcrt_is_valid_fd(od))
645 HANDLE handle;
647 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
648 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
650 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
652 if (msvcrt_is_valid_fd(nd))
653 _close(nd);
654 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
655 if (ret == -1)
657 CloseHandle(handle);
658 *MSVCRT__errno() = MSVCRT_EMFILE;
660 else
662 /* _dup2 returns 0, not nd, on success */
663 ret = 0;
666 else
668 ret = -1;
669 msvcrt_set_errno(GetLastError());
672 else
674 *MSVCRT__errno() = MSVCRT_EBADF;
675 ret = -1;
677 return ret;
680 /*********************************************************************
681 * _dup (MSVCRT.@)
683 int _dup(int od)
685 int fd = MSVCRT_fdstart;
687 if (_dup2(od, fd) == 0)
688 return fd;
689 return -1;
692 /*********************************************************************
693 * _eof (MSVCRT.@)
695 int _eof(int fd)
697 DWORD curpos,endpos,hcurpos,hendpos;
698 HANDLE hand = msvcrt_fdtoh(fd);
700 TRACE(":fd (%d) handle (%p)\n",fd,hand);
702 if (hand == INVALID_HANDLE_VALUE)
703 return -1;
705 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
707 /* Otherwise we do it the hard way */
708 hcurpos = hendpos = 0;
709 curpos = SetFilePointer(hand, 0, &hcurpos, SEEK_CUR);
710 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
712 if (curpos == endpos && hcurpos == hendpos)
713 return TRUE;
715 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
716 return FALSE;
719 /*********************************************************************
720 * _fcloseall (MSVCRT.@)
722 int MSVCRT__fcloseall(void)
724 int num_closed = 0, i;
726 for (i = 3; i < MSVCRT_stream_idx; i++)
727 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
728 MSVCRT_fclose(MSVCRT_fstreams[i]))
729 num_closed++;
731 TRACE(":closed (%d) handles\n",num_closed);
732 return num_closed;
735 /* free everything on process exit */
736 void msvcrt_free_io(void)
738 MSVCRT__fcloseall();
739 _close(0);
740 _close(1);
741 _close(2);
744 /*********************************************************************
745 * _lseeki64 (MSVCRT.@)
747 __int64 _lseeki64(int fd, __int64 offset, int whence)
749 DWORD ret, hoffset = (DWORD) (offset >> 32);
750 HANDLE hand = msvcrt_fdtoh(fd);
752 TRACE(":fd (%d) handle (%p)\n",fd,hand);
753 if (hand == INVALID_HANDLE_VALUE)
754 return -1;
756 if (whence < 0 || whence > 2)
758 *MSVCRT__errno() = MSVCRT_EINVAL;
759 return -1;
762 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
763 fd,hoffset,(long)offset,
764 (whence==SEEK_SET)?"SEEK_SET":
765 (whence==SEEK_CUR)?"SEEK_CUR":
766 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
768 ret = SetFilePointer(hand, (long)offset, &hoffset, whence);
769 if (ret != INVALID_SET_FILE_POINTER || !GetLastError())
771 MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
772 /* FIXME: What if we seek _to_ EOF - is EOF set? */
774 return ((__int64)hoffset << 32) | ret;
776 TRACE(":error-last error (%ld)\n",GetLastError());
777 msvcrt_set_errno(GetLastError());
778 return -1;
781 /*********************************************************************
782 * _lseek (MSVCRT.@)
784 LONG _lseek(int fd, LONG offset, int whence)
786 return _lseeki64(fd, offset, whence);
789 /*********************************************************************
790 * _locking (MSVCRT.@)
792 * This is untested; the underlying LockFile doesn't work yet.
794 int _locking(int fd, int mode, LONG nbytes)
796 BOOL ret;
797 DWORD cur_locn;
798 HANDLE hand = msvcrt_fdtoh(fd);
800 TRACE(":fd (%d) handle (%p)\n",fd,hand);
801 if (hand == INVALID_HANDLE_VALUE)
802 return -1;
804 if (mode < 0 || mode > 4)
806 *MSVCRT__errno() = MSVCRT_EINVAL;
807 return -1;
810 TRACE(":fd (%d) by 0x%08lx mode %s\n",
811 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
812 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
813 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
814 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
815 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
816 "UNKNOWN");
818 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
820 FIXME ("Seek failed\n");
821 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
822 return -1;
824 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
826 int nretry = 10;
827 ret = 1; /* just to satisfy gcc */
828 while (nretry--)
830 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
831 if (ret) break;
832 Sleep(1);
835 else if (mode == MSVCRT__LK_UNLCK)
836 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
837 else
838 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
839 /* FIXME - what about error settings? */
840 return ret ? 0 : -1;
843 /*********************************************************************
844 * fseek (MSVCRT.@)
846 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
848 /* Flush output if needed */
849 if(file->_flag & MSVCRT__IOWRT)
850 msvcrt_flush_buffer(file);
852 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
853 offset -= file->_cnt;
855 /* Discard buffered input */
856 file->_cnt = 0;
857 file->_ptr = file->_base;
858 /* Reset direction of i/o */
859 if(file->_flag & MSVCRT__IORW) {
860 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
862 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
865 /*********************************************************************
866 * clearerr (MSVCRT.@)
868 void MSVCRT_clearerr(MSVCRT_FILE* file)
870 TRACE(":file (%p) fd (%d)\n",file,file->_file);
871 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
874 /*********************************************************************
875 * rewind (MSVCRT.@)
877 void MSVCRT_rewind(MSVCRT_FILE* file)
879 TRACE(":file (%p) fd (%d)\n",file,file->_file);
880 MSVCRT_fseek(file, 0L, SEEK_SET);
881 MSVCRT_clearerr(file);
884 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
886 int plus = strchr(mode, '+') != NULL;
888 switch(*mode++)
890 case 'R': case 'r':
891 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
892 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
893 break;
894 case 'W': case 'w':
895 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
896 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
897 break;
898 case 'A': case 'a':
899 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
900 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
901 break;
902 default:
903 return -1;
906 while (*mode)
907 switch (*mode++)
909 case 'B': case 'b':
910 *open_flags |= MSVCRT__O_BINARY;
911 *open_flags &= ~MSVCRT__O_TEXT;
912 break;
913 case 'T': case 't':
914 *open_flags |= MSVCRT__O_TEXT;
915 *open_flags &= ~MSVCRT__O_BINARY;
916 break;
917 case '+':
918 break;
919 default:
920 FIXME(":unknown flag %c not supported\n",mode[-1]);
922 return 0;
925 /*********************************************************************
926 * _fdopen (MSVCRT.@)
928 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
930 int open_flags, stream_flags;
931 MSVCRT_FILE* file;
933 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
935 if (!(file = msvcrt_alloc_fp())) return NULL;
936 if (msvcrt_init_fp(file, fd, stream_flags) == -1)
938 file->_flag = 0;
939 file = NULL;
941 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
943 return file;
946 /*********************************************************************
947 * _wfdopen (MSVCRT.@)
949 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
951 unsigned mlen = strlenW(mode);
952 char *modea = MSVCRT_calloc(mlen + 1, 1);
953 MSVCRT_FILE* file = NULL;
954 int open_flags, stream_flags;
956 if (modea &&
957 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
959 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
960 if (!(file = msvcrt_alloc_fp())) return NULL;
961 if (msvcrt_init_fp(file, fd, stream_flags) == -1)
963 file->_flag = 0;
964 file = NULL;
966 else
968 if (file)
969 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
970 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
973 return file;
976 /*********************************************************************
977 * _filelength (MSVCRT.@)
979 LONG _filelength(int fd)
981 LONG curPos = _lseek(fd, 0, SEEK_CUR);
982 if (curPos != -1)
984 LONG endPos = _lseek(fd, 0, SEEK_END);
985 if (endPos != -1)
987 if (endPos != curPos)
988 _lseek(fd, curPos, SEEK_SET);
989 return endPos;
992 return -1;
995 /*********************************************************************
996 * _filelengthi64 (MSVCRT.@)
998 __int64 _filelengthi64(int fd)
1000 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1001 if (curPos != -1)
1003 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1004 if (endPos != -1)
1006 if (endPos != curPos)
1007 _lseeki64(fd, curPos, SEEK_SET);
1008 return endPos;
1011 return -1;
1014 /*********************************************************************
1015 * _fileno (MSVCRT.@)
1017 int MSVCRT__fileno(MSVCRT_FILE* file)
1019 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1020 return file->_file;
1023 /*********************************************************************
1024 * _fstati64 (MSVCRT.@)
1026 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1028 DWORD dw;
1029 BY_HANDLE_FILE_INFORMATION hfi;
1030 HANDLE hand = msvcrt_fdtoh(fd);
1032 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1033 if (hand == INVALID_HANDLE_VALUE)
1034 return -1;
1036 if (!buf)
1038 WARN(":failed-NULL buf\n");
1039 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1040 return -1;
1043 memset(&hfi, 0, sizeof(hfi));
1044 memset(buf, 0, sizeof(struct MSVCRT__stati64));
1045 if (!GetFileInformationByHandle(hand, &hfi))
1047 WARN(":failed-last error (%ld)\n",GetLastError());
1048 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1049 return -1;
1051 dw = GetFileType(hand);
1052 buf->st_mode = S_IREAD;
1053 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1054 buf->st_mode |= S_IWRITE;
1055 /* interestingly, Windows never seems to set S_IFDIR */
1056 if (dw == FILE_TYPE_CHAR)
1057 buf->st_mode |= S_IFCHR;
1058 else if (dw == FILE_TYPE_PIPE)
1059 buf->st_mode |= S_IFIFO;
1060 else
1061 buf->st_mode |= S_IFREG;
1062 TRACE(":dwFileAttributes = 0x%lx, mode set to 0x%x\n",hfi.dwFileAttributes,
1063 buf->st_mode);
1064 buf->st_nlink = hfi.nNumberOfLinks;
1065 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1066 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1067 buf->st_atime = dw;
1068 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1069 buf->st_mtime = buf->st_ctime = dw;
1070 return 0;
1073 /*********************************************************************
1074 * _fstat (MSVCRT.@)
1076 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1077 { int ret;
1078 struct MSVCRT__stati64 bufi64;
1080 ret = MSVCRT__fstati64(fd, &bufi64);
1081 if (!ret)
1082 msvcrt_cp_from_stati64(&bufi64, buf);
1083 return ret;
1086 /*********************************************************************
1087 * _futime (MSVCRT.@)
1089 int _futime(int fd, struct MSVCRT__utimbuf *t)
1091 HANDLE hand = msvcrt_fdtoh(fd);
1092 FILETIME at, wt;
1094 if (!t)
1096 MSVCRT_time_t currTime;
1097 MSVCRT_time(&currTime);
1098 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1099 memcpy(&wt, &at, sizeof(wt));
1101 else
1103 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1104 if (t->actime == t->modtime)
1105 memcpy(&wt, &at, sizeof(wt));
1106 else
1107 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1110 if (!SetFileTime(hand, NULL, &at, &wt))
1112 msvcrt_set_errno(GetLastError());
1113 return -1 ;
1115 return 0;
1118 /*********************************************************************
1119 * _get_osfhandle (MSVCRT.@)
1121 long _get_osfhandle(int fd)
1123 HANDLE hand = msvcrt_fdtoh(fd);
1124 HANDLE newhand = hand;
1125 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1127 if (hand != INVALID_HANDLE_VALUE)
1129 /* FIXME: I'm not convinced that I should be copying the
1130 * handle here - it may be leaked if the app doesn't
1131 * close it (and the API docs don't say that it should)
1132 * Not duplicating it means that it can't be inherited
1133 * and so lcc's wedit doesn't cope when it passes it to
1134 * child processes. I've an idea that it should either
1135 * be copied by CreateProcess, or marked as inheritable
1136 * when initialised, or maybe both? JG 21-9-00.
1138 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
1139 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
1141 return (long)newhand;
1144 /*********************************************************************
1145 * _isatty (MSVCRT.@)
1147 int _isatty(int fd)
1149 HANDLE hand = msvcrt_fdtoh(fd);
1151 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1152 if (hand == INVALID_HANDLE_VALUE)
1153 return 0;
1155 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1158 /*********************************************************************
1159 * _mktemp (MSVCRT.@)
1161 char *_mktemp(char *pattern)
1163 int numX = 0;
1164 char *retVal = pattern;
1165 int id;
1166 char letter = 'a';
1168 while(*pattern)
1169 numX = (*pattern++ == 'X')? numX + 1 : 0;
1170 if (numX < 5)
1171 return NULL;
1172 pattern--;
1173 id = GetCurrentProcessId();
1174 numX = 6;
1175 while(numX--)
1177 int tempNum = id / 10;
1178 *pattern-- = id - (tempNum * 10) + '0';
1179 id = tempNum;
1181 pattern++;
1184 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1185 GetLastError() == ERROR_FILE_NOT_FOUND)
1186 return retVal;
1187 *pattern = letter++;
1188 } while(letter != '|');
1189 return NULL;
1192 /*********************************************************************
1193 * _wmktemp (MSVCRT.@)
1195 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
1197 int numX = 0;
1198 MSVCRT_wchar_t *retVal = pattern;
1199 int id;
1200 MSVCRT_wchar_t letter = 'a';
1202 while(*pattern)
1203 numX = (*pattern++ == 'X')? numX + 1 : 0;
1204 if (numX < 5)
1205 return NULL;
1206 pattern--;
1207 id = GetCurrentProcessId();
1208 numX = 6;
1209 while(numX--)
1211 int tempNum = id / 10;
1212 *pattern-- = id - (tempNum * 10) + '0';
1213 id = tempNum;
1215 pattern++;
1218 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1219 GetLastError() == ERROR_FILE_NOT_FOUND)
1220 return retVal;
1221 *pattern = letter++;
1222 } while(letter != '|');
1223 return NULL;
1226 static unsigned split_oflags(unsigned oflags)
1228 int wxflags = 0;
1230 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1231 if (oflags & MSVCRT__O_BINARY) ;
1232 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1233 else if (*__p__fmode() & MSVCRT__O_BINARY) ;
1234 else wxflags |= WX_TEXT; /* default to TEXT*/
1235 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1237 if (oflags & ~(MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|MSVCRT__O_TRUNC|
1238 MSVCRT__O_EXCL|MSVCRT__O_CREAT|MSVCRT__O_RDWR|MSVCRT__O_WRONLY|
1239 MSVCRT__O_TEMPORARY|MSVCRT__O_NOINHERIT))
1240 ERR(":unsupported oflags 0x%04x\n",oflags);
1242 return wxflags;
1245 /*********************************************************************
1246 * _pipe (MSVCRT.@)
1248 int _pipe(int *pfds, unsigned int psize, int textmode)
1250 int ret = -1;
1251 SECURITY_ATTRIBUTES sa;
1252 HANDLE readHandle, writeHandle;
1254 if (!pfds)
1256 *MSVCRT__errno() = MSVCRT_EINVAL;
1257 return -1;
1260 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1261 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1262 sa.lpSecurityDescriptor = NULL;
1263 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1265 unsigned int wxflags = split_oflags(textmode);
1266 int fd;
1268 fd = msvcrt_alloc_fd(readHandle, wxflags);
1269 if (fd != -1)
1271 pfds[0] = fd;
1272 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1273 if (fd != -1)
1275 pfds[1] = fd;
1276 ret = 0;
1278 else
1280 _close(pfds[0]);
1281 CloseHandle(writeHandle);
1282 *MSVCRT__errno() = MSVCRT_EMFILE;
1285 else
1287 CloseHandle(readHandle);
1288 CloseHandle(writeHandle);
1289 *MSVCRT__errno() = MSVCRT_EMFILE;
1292 else
1293 msvcrt_set_errno(GetLastError());
1295 return ret;
1298 /*********************************************************************
1299 * _sopen (MSVCRT.@)
1301 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1303 va_list ap;
1304 int pmode;
1305 DWORD access = 0, creation = 0, attrib;
1306 DWORD sharing;
1307 int wxflag = 0, fd;
1308 HANDLE hand;
1309 SECURITY_ATTRIBUTES sa;
1312 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1313 path, oflags, shflags);
1315 wxflag = split_oflags(oflags);
1316 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1318 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1319 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1320 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1323 if (oflags & MSVCRT__O_CREAT)
1325 va_start(ap, shflags);
1326 pmode = va_arg(ap, int);
1327 va_end(ap);
1329 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1330 FIXME(": pmode 0x%04x ignored\n", pmode);
1331 else
1332 WARN(": pmode 0x%04x ignored\n", pmode);
1334 if (oflags & MSVCRT__O_EXCL)
1335 creation = CREATE_NEW;
1336 else if (oflags & MSVCRT__O_TRUNC)
1337 creation = CREATE_ALWAYS;
1338 else
1339 creation = OPEN_ALWAYS;
1341 else /* no MSVCRT__O_CREAT */
1343 if (oflags & MSVCRT__O_TRUNC)
1344 creation = TRUNCATE_EXISTING;
1345 else
1346 creation = OPEN_EXISTING;
1349 switch( shflags )
1351 case MSVCRT__SH_DENYRW:
1352 sharing = 0L;
1353 break;
1354 case MSVCRT__SH_DENYWR:
1355 sharing = FILE_SHARE_READ;
1356 break;
1357 case MSVCRT__SH_DENYRD:
1358 sharing = FILE_SHARE_WRITE;
1359 break;
1360 case MSVCRT__SH_DENYNO:
1361 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1362 break;
1363 default:
1364 ERR( "Unhandled shflags 0x%x\n", shflags );
1365 return -1;
1367 attrib = FILE_ATTRIBUTE_NORMAL;
1369 if (oflags & MSVCRT__O_TEMPORARY)
1371 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1372 access |= DELETE;
1373 sharing |= FILE_SHARE_DELETE;
1376 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1377 sa.lpSecurityDescriptor = NULL;
1378 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1380 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1382 if (hand == INVALID_HANDLE_VALUE) {
1383 WARN(":failed-last error (%ld)\n",GetLastError());
1384 msvcrt_set_errno(GetLastError());
1385 return -1;
1388 fd = msvcrt_alloc_fd(hand, wxflag);
1390 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1392 if (fd > 0)
1394 if (oflags & MSVCRT__O_APPEND)
1395 _lseek(fd, 0, FILE_END);
1398 return fd;
1401 /*********************************************************************
1402 * _wsopen (MSVCRT.@)
1404 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1406 const unsigned int len = strlenW(path);
1407 char *patha = MSVCRT_calloc(len + 1,1);
1408 va_list ap;
1409 int pmode;
1411 va_start(ap, shflags);
1412 pmode = va_arg(ap, int);
1413 va_end(ap);
1415 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1417 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1418 MSVCRT_free(patha);
1419 return retval;
1422 msvcrt_set_errno(GetLastError());
1423 return -1;
1426 /*********************************************************************
1427 * _open (MSVCRT.@)
1429 int _open( const char *path, int flags, ... )
1431 va_list ap;
1433 if (flags & MSVCRT__O_CREAT)
1435 int pmode;
1436 va_start(ap, flags);
1437 pmode = va_arg(ap, int);
1438 va_end(ap);
1439 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1441 else
1442 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1445 /*********************************************************************
1446 * _wopen (MSVCRT.@)
1448 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1450 const unsigned int len = strlenW(path);
1451 char *patha = MSVCRT_calloc(len + 1,1);
1452 va_list ap;
1453 int pmode;
1455 va_start(ap, flags);
1456 pmode = va_arg(ap, int);
1457 va_end(ap);
1459 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1461 int retval = _open(patha,flags,pmode);
1462 MSVCRT_free(patha);
1463 return retval;
1466 msvcrt_set_errno(GetLastError());
1467 return -1;
1470 /*********************************************************************
1471 * _creat (MSVCRT.@)
1473 int _creat(const char *path, int flags)
1475 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1476 return _open(path, usedFlags);
1479 /*********************************************************************
1480 * _wcreat (MSVCRT.@)
1482 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1484 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1485 return _wopen(path, usedFlags);
1488 /*********************************************************************
1489 * _open_osfhandle (MSVCRT.@)
1491 int _open_osfhandle(long handle, int oflags)
1493 int fd;
1495 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1496 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1497 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1498 * text - it never sets MSVCRT__O_BINARY.
1500 /* FIXME: handle more flags */
1501 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)) && (*__p__fmode() & MSVCRT__O_BINARY))
1502 oflags |= MSVCRT__O_BINARY;
1503 else
1504 oflags |= MSVCRT__O_TEXT;
1506 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1507 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1508 return fd;
1511 /*********************************************************************
1512 * _rmtmp (MSVCRT.@)
1514 int _rmtmp(void)
1516 int num_removed = 0, i;
1518 for (i = 3; i < MSVCRT_stream_idx; i++)
1519 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1521 MSVCRT_fclose(MSVCRT_fstreams[i]);
1522 num_removed++;
1525 if (num_removed)
1526 TRACE(":removed (%d) temp files\n",num_removed);
1527 return num_removed;
1530 /*********************************************************************
1531 * (internal) remove_cr
1533 * Remove all \r inplace.
1534 * return the number of \r removed
1536 static unsigned int remove_cr(char *buf, unsigned int count)
1538 unsigned int i, j;
1540 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1541 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1542 return count - i;
1545 /*********************************************************************
1546 * _read (MSVCRT.@)
1548 int _read(int fd, void *buf, unsigned int count)
1550 DWORD num_read, all_read = 0;
1551 char *bufstart = buf;
1552 HANDLE hand = msvcrt_fdtoh(fd);
1554 /* Don't trace small reads, it gets *very* annoying */
1555 if (count > 4)
1556 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1557 if (hand == INVALID_HANDLE_VALUE)
1558 return -1;
1560 /* Reading single bytes in O_TEXT mode makes things slow
1561 * So read big chunks, then remove the \r in memory and try reading
1562 * the rest until the request is satisfied or EOF is met
1564 while (all_read < count)
1566 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1568 if (num_read != (count - all_read))
1570 TRACE(":EOF\n");
1571 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1572 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1573 num_read -= remove_cr(bufstart+all_read,num_read);
1574 all_read += num_read;
1575 if (count > 4)
1576 TRACE("%s\n",debugstr_an(buf,all_read));
1577 return all_read;
1579 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1581 num_read -= remove_cr(bufstart+all_read,num_read);
1583 all_read += num_read;
1585 else
1587 TRACE(":failed-last error (%ld)\n",GetLastError());
1588 return -1;
1592 if (count > 4)
1593 TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
1594 return all_read;
1597 /*********************************************************************
1598 * _getw (MSVCRT.@)
1600 int MSVCRT__getw(MSVCRT_FILE* file)
1602 int i;
1603 switch (_read(file->_file, &i, sizeof(int)))
1605 case 1: return i;
1606 case 0: file->_flag |= MSVCRT__IOEOF; break;
1607 default: file->_flag |= MSVCRT__IOERR; break;
1609 return EOF;
1612 /*********************************************************************
1613 * _setmode (MSVCRT.@)
1615 int _setmode(int fd,int mode)
1617 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1618 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1619 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1620 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1621 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1622 else
1623 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1624 return ret;
1627 /*********************************************************************
1628 * _stati64 (MSVCRT.@)
1630 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1632 DWORD dw;
1633 WIN32_FILE_ATTRIBUTE_DATA hfi;
1634 unsigned short mode = ALL_S_IREAD;
1635 int plen;
1637 TRACE(":file (%s) buf(%p)\n",path,buf);
1639 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1641 TRACE("failed (%ld)\n",GetLastError());
1642 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1643 return -1;
1646 memset(buf,0,sizeof(struct MSVCRT__stati64));
1648 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1649 Bon 011120: This FIXME seems incorrect
1650 Also a letter as first char isn't enough to be classified
1651 as a drive letter
1653 if (isalpha(*path)&& (*(path+1)==':'))
1654 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1655 else
1656 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1658 plen = strlen(path);
1660 /* Dir, or regular file? */
1661 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1662 (path[plen-1] == '\\'))
1663 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1664 else
1666 mode |= MSVCRT__S_IFREG;
1667 /* executable? */
1668 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1670 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1671 (tolower(path[plen-3]) << 16);
1672 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1673 mode |= ALL_S_IEXEC;
1677 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1678 mode |= ALL_S_IWRITE;
1680 buf->st_mode = mode;
1681 buf->st_nlink = 1;
1682 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1683 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1684 buf->st_atime = dw;
1685 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1686 buf->st_mtime = buf->st_ctime = dw;
1687 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1688 (long)(buf->st_size >> 32),(long)buf->st_size,
1689 buf->st_atime,buf->st_mtime, buf->st_ctime);
1690 return 0;
1693 /*********************************************************************
1694 * _stat (MSVCRT.@)
1696 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1697 { int ret;
1698 struct MSVCRT__stati64 bufi64;
1700 ret = MSVCRT__stati64( path, &bufi64);
1701 if (!ret)
1702 msvcrt_cp_from_stati64(&bufi64, buf);
1703 return ret;
1706 /*********************************************************************
1707 * _wstati64 (MSVCRT.@)
1709 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1711 DWORD dw;
1712 WIN32_FILE_ATTRIBUTE_DATA hfi;
1713 unsigned short mode = ALL_S_IREAD;
1714 int plen;
1716 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1718 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1720 TRACE("failed (%ld)\n",GetLastError());
1721 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1722 return -1;
1725 memset(buf,0,sizeof(struct MSVCRT__stat));
1727 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1728 if (MSVCRT_iswalpha(*path))
1729 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1730 else
1731 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1733 plen = strlenW(path);
1735 /* Dir, or regular file? */
1736 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1737 (path[plen-1] == '\\'))
1738 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1739 else
1741 mode |= MSVCRT__S_IFREG;
1742 /* executable? */
1743 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1745 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1746 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1747 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1748 mode |= ALL_S_IEXEC;
1752 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1753 mode |= ALL_S_IWRITE;
1755 buf->st_mode = mode;
1756 buf->st_nlink = 1;
1757 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1758 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1759 buf->st_atime = dw;
1760 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1761 buf->st_mtime = buf->st_ctime = dw;
1762 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1763 (long)(buf->st_size >> 32),(long)buf->st_size,
1764 buf->st_atime,buf->st_mtime, buf->st_ctime);
1765 return 0;
1768 /*********************************************************************
1769 * _wstat (MSVCRT.@)
1771 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1773 int ret;
1774 struct MSVCRT__stati64 bufi64;
1776 ret = MSVCRT__wstati64( path, &bufi64 );
1777 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1778 return ret;
1781 /*********************************************************************
1782 * _tell (MSVCRT.@)
1784 long _tell(int fd)
1786 return _lseek(fd, 0, SEEK_CUR);
1789 /*********************************************************************
1790 * _telli64 (MSVCRT.@)
1792 __int64 _telli64(int fd)
1794 return _lseeki64(fd, 0, SEEK_CUR);
1797 /*********************************************************************
1798 * _tempnam (MSVCRT.@)
1800 char *_tempnam(const char *dir, const char *prefix)
1802 char tmpbuf[MAX_PATH];
1804 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1805 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1807 TRACE("got name (%s)\n",tmpbuf);
1808 DeleteFileA(tmpbuf);
1809 return _strdup(tmpbuf);
1811 TRACE("failed (%ld)\n",GetLastError());
1812 return NULL;
1815 /*********************************************************************
1816 * _wtempnam (MSVCRT.@)
1818 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1820 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1822 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1823 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1825 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1826 DeleteFileW(tmpbuf);
1827 return _wcsdup(tmpbuf);
1829 TRACE("failed (%ld)\n",GetLastError());
1830 return NULL;
1833 /*********************************************************************
1834 * _umask (MSVCRT.@)
1836 int _umask(int umask)
1838 int old_umask = MSVCRT_umask;
1839 TRACE("(%d)\n",umask);
1840 MSVCRT_umask = umask;
1841 return old_umask;
1844 /*********************************************************************
1845 * _utime (MSVCRT.@)
1847 int _utime(const char* path, struct MSVCRT__utimbuf *t)
1849 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1851 if (fd > 0)
1853 int retVal = _futime(fd, t);
1854 _close(fd);
1855 return retVal;
1857 return -1;
1860 /*********************************************************************
1861 * _wutime (MSVCRT.@)
1863 int _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1865 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1867 if (fd > 0)
1869 int retVal = _futime(fd, t);
1870 _close(fd);
1871 return retVal;
1873 return -1;
1876 /*********************************************************************
1877 * _write (MSVCRT.@)
1879 int _write(int fd, const void* buf, unsigned int count)
1881 DWORD num_written;
1882 HANDLE hand = msvcrt_fdtoh(fd);
1884 /* Don't trace small writes, it gets *very* annoying */
1885 #if 0
1886 if (count > 32)
1887 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1888 #endif
1889 if (hand == INVALID_HANDLE_VALUE)
1891 *MSVCRT__errno() = MSVCRT_EBADF;
1892 return -1;
1895 /* If appending, go to EOF */
1896 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
1897 _lseek(fd, 0, FILE_END);
1899 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
1901 if (WriteFile(hand, buf, count, &num_written, NULL)
1902 && (num_written == count))
1903 return num_written;
1904 TRACE(":failed-last error (%ld)\n",GetLastError());
1905 *MSVCRT__errno() = MSVCRT_ENOSPC;
1907 else
1909 unsigned int i, j, nr_lf;
1910 char *s =(char*)buf, *buf_start=(char*)buf, *p;
1911 /* find number of \n ( without preceeding \r */
1912 for ( nr_lf=0,i = 0; i <count; i++)
1914 if (s[i]== '\n')
1916 nr_lf++;
1917 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1920 if (nr_lf)
1922 if ((p = MSVCRT_malloc(count + nr_lf)))
1924 for(s=(char*)buf, i=0, j=0; i<count; i++)
1926 if (s[i]== '\n')
1928 p[j++] = '\r';
1929 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1931 p[j++] = s[i];
1934 else
1936 FIXME("Malloc failed\n");
1937 nr_lf =0;
1938 p = (char*)buf;
1941 else
1942 p = (char*)buf;
1944 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
1946 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
1947 *MSVCRT__errno() = MSVCRT_ENOSPC;
1948 if(nr_lf)
1949 MSVCRT_free(p);
1950 return s - buf_start;
1952 else
1954 if(nr_lf)
1955 MSVCRT_free(p);
1956 return count;
1959 return -1;
1962 /*********************************************************************
1963 * _putw (MSVCRT.@)
1965 int MSVCRT__putw(int val, MSVCRT_FILE* file)
1967 int len;
1968 len = _write(file->_file, &val, sizeof(val));
1969 if (len == sizeof(val)) return val;
1970 file->_flag |= MSVCRT__IOERR;
1971 return MSVCRT_EOF;
1974 /*********************************************************************
1975 * fclose (MSVCRT.@)
1977 int MSVCRT_fclose(MSVCRT_FILE* file)
1979 int r, flag;
1981 flag = file->_flag;
1982 if (file->_tmpfname)
1984 MSVCRT_free(file->_tmpfname);
1985 file->_tmpfname = NULL;
1987 /* flush stdio buffers */
1988 if(file->_flag & MSVCRT__IOWRT)
1989 MSVCRT_fflush(file);
1990 if(file->_flag & MSVCRT__IOMYBUF)
1991 MSVCRT_free(file->_base);
1993 r=_close(file->_file);
1995 file->_flag = 0;
1997 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2000 /*********************************************************************
2001 * feof (MSVCRT.@)
2003 int MSVCRT_feof(MSVCRT_FILE* file)
2005 return file->_flag & MSVCRT__IOEOF;
2008 /*********************************************************************
2009 * ferror (MSVCRT.@)
2011 int MSVCRT_ferror(MSVCRT_FILE* file)
2013 return file->_flag & MSVCRT__IOERR;
2016 /*********************************************************************
2017 * _filbuf (MSVCRT.@)
2019 int MSVCRT__filbuf(MSVCRT_FILE* file)
2021 /* Allocate buffer if needed */
2022 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2023 msvcrt_alloc_buffer(file);
2025 if(!(file->_flag & MSVCRT__IOREAD)) {
2026 if(file->_flag & MSVCRT__IORW) {
2027 file->_flag |= MSVCRT__IOREAD;
2028 } else {
2029 return MSVCRT_EOF;
2032 if(file->_flag & MSVCRT__IONBF) {
2033 unsigned char c;
2034 int r;
2035 if ((r = _read(file->_file,&c,1)) != 1) {
2036 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2037 return MSVCRT_EOF;
2039 return c;
2040 } else {
2041 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2042 if(file->_cnt<=0) {
2043 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2044 file->_cnt = 0;
2045 return MSVCRT_EOF;
2047 file->_cnt--;
2048 file->_ptr = file->_base+1;
2049 return *(unsigned char *)file->_base;
2053 /*********************************************************************
2054 * fgetc (MSVCRT.@)
2056 int MSVCRT_fgetc(MSVCRT_FILE* file)
2058 if (file->_cnt>0) {
2059 file->_cnt--;
2060 return *(unsigned char *)file->_ptr++;
2061 } else {
2062 return MSVCRT__filbuf(file);
2066 /*********************************************************************
2067 * _fgetchar (MSVCRT.@)
2069 int _fgetchar(void)
2071 return MSVCRT_fgetc(MSVCRT_stdin);
2074 /*********************************************************************
2075 * fgets (MSVCRT.@)
2077 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2079 int cc = MSVCRT_EOF;
2080 char * buf_start = s;
2082 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2083 file,file->_file,s,size);
2085 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2087 *s++ = (char)cc;
2088 size --;
2090 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2092 TRACE(":nothing read\n");
2093 return NULL;
2095 if ((cc != MSVCRT_EOF) && (size > 1))
2096 *s++ = cc;
2097 *s = '\0';
2098 TRACE(":got '%s'\n", debugstr_a(buf_start));
2099 return buf_start;
2102 /*********************************************************************
2103 * fgetwc (MSVCRT.@)
2105 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2106 * the CR from CR/LF combinations
2108 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
2110 char c;
2112 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2114 MSVCRT_wchar_t wc;
2115 int r;
2116 if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
2118 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2119 return MSVCRT_WEOF;
2121 return wc;
2123 c = MSVCRT_fgetc(file);
2124 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2126 FIXME("Treat Multibyte characters\n");
2128 if (c == MSVCRT_EOF)
2129 return MSVCRT_WEOF;
2130 else
2131 return (MSVCRT_wint_t)c;
2134 /*********************************************************************
2135 * getwc (MSVCRT.@)
2137 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
2139 return MSVCRT_fgetwc(file);
2142 /*********************************************************************
2143 * _fgetwchar (MSVCRT.@)
2145 MSVCRT_wint_t _fgetwchar(void)
2147 return MSVCRT_fgetwc(MSVCRT_stdin);
2150 /*********************************************************************
2151 * getwchar (MSVCRT.@)
2153 MSVCRT_wint_t MSVCRT_getwchar(void)
2155 return _fgetwchar();
2158 /*********************************************************************
2159 * fgetws (MSVCRT.@)
2161 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2163 int cc = MSVCRT_WEOF;
2164 MSVCRT_wchar_t * buf_start = s;
2166 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2167 file,file->_file,s,size);
2169 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2171 *s++ = (char)cc;
2172 size --;
2174 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2176 TRACE(":nothing read\n");
2177 return NULL;
2179 if ((cc != MSVCRT_WEOF) && (size > 1))
2180 *s++ = cc;
2181 *s = 0;
2182 TRACE(":got %s\n", debugstr_w(buf_start));
2183 return buf_start;
2186 /*********************************************************************
2187 * fwrite (MSVCRT.@)
2189 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2191 MSVCRT_size_t wrcnt=size * nmemb;
2192 int written = 0;
2193 if (size == 0)
2194 return 0;
2195 if(file->_cnt) {
2196 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2197 memcpy(file->_ptr, ptr, pcnt);
2198 file->_cnt -= pcnt;
2199 file->_ptr += pcnt;
2200 written = pcnt;
2201 wrcnt -= pcnt;
2202 ptr = (const char*)ptr + pcnt;
2203 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2204 if(file->_flag & MSVCRT__IORW) {
2205 file->_flag |= MSVCRT__IOWRT;
2206 } else
2207 return 0;
2209 if(wrcnt) {
2210 /* Flush buffer */
2211 int res=msvcrt_flush_buffer(file);
2212 if(!res) {
2213 int pwritten = _write(file->_file, ptr, wrcnt);
2214 if (pwritten <= 0)
2216 file->_flag |= MSVCRT__IOERR;
2217 pwritten=0;
2219 written += pwritten;
2222 return written / size;
2225 /*********************************************************************
2226 * fputwc (MSVCRT.@)
2228 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2230 MSVCRT_wchar_t mwc=wc;
2231 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2232 return MSVCRT_WEOF;
2233 return wc;
2236 /*********************************************************************
2237 * _fputwchar (MSVCRT.@)
2239 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
2241 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2244 /*********************************************************************
2245 * fopen (MSVCRT.@)
2247 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
2249 MSVCRT_FILE* file;
2250 int open_flags, stream_flags, fd;
2252 TRACE("(%s,%s)\n",path,mode);
2254 /* map mode string to open() flags. "man fopen" for possibilities. */
2255 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2256 return NULL;
2258 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2260 if (fd < 0)
2261 return NULL;
2263 if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags) != -1)
2264 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2265 else if (file)
2267 file->_flag = 0;
2268 file = NULL;
2271 TRACE(":got (%p)\n",file);
2272 if (!file)
2273 _close(fd);
2274 return file;
2277 /*********************************************************************
2278 * _wfopen (MSVCRT.@)
2280 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2282 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2283 char *patha = MSVCRT_calloc(plen + 1, 1);
2284 char *modea = MSVCRT_calloc(mlen + 1, 1);
2286 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2288 if (patha && modea &&
2289 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2290 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2292 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2293 MSVCRT_free(patha);
2294 MSVCRT_free(modea);
2295 return retval;
2298 msvcrt_set_errno(GetLastError());
2299 return NULL;
2302 /*********************************************************************
2303 * _fsopen (MSVCRT.@)
2305 MSVCRT_FILE* MSVCRT__fsopen(const char *path, const char *mode, int share)
2307 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2308 return MSVCRT_fopen(path,mode);
2311 /*********************************************************************
2312 * _wfsopen (MSVCRT.@)
2314 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2316 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2317 debugstr_w(path),debugstr_w(mode),share);
2318 return MSVCRT__wfopen(path,mode);
2321 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2322 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2324 /*********************************************************************
2325 * fputc (MSVCRT.@)
2327 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2329 if(file->_cnt>0) {
2330 *file->_ptr++=c;
2331 file->_cnt--;
2332 if (c == '\n')
2334 int res = msvcrt_flush_buffer(file);
2335 return res ? res : c;
2337 else
2338 return c;
2339 } else {
2340 return MSVCRT__flsbuf(c, file);
2344 /*********************************************************************
2345 * _flsbuf (MSVCRT.@)
2347 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2349 /* Flush output buffer */
2350 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2351 msvcrt_alloc_buffer(file);
2353 if(!(file->_flag & MSVCRT__IOWRT)) {
2354 if(file->_flag & MSVCRT__IORW) {
2355 file->_flag |= MSVCRT__IOWRT;
2356 } else {
2357 return MSVCRT_EOF;
2360 if(file->_bufsiz) {
2361 int res=msvcrt_flush_buffer(file);
2362 return res?res : MSVCRT_fputc(c, file);
2363 } else {
2364 unsigned char cc=c;
2365 int len;
2366 len = _write(file->_file, &cc, 1);
2367 if (len == 1) return c;
2368 file->_flag |= MSVCRT__IOERR;
2369 return MSVCRT_EOF;
2373 /*********************************************************************
2374 * _fputchar (MSVCRT.@)
2376 int _fputchar(int c)
2378 return MSVCRT_fputc(c, MSVCRT_stdout);
2381 /*********************************************************************
2382 * fread (MSVCRT.@)
2384 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2385 { MSVCRT_size_t rcnt=size * nmemb;
2386 MSVCRT_size_t read=0;
2387 int pread=0;
2389 /* first buffered data */
2390 if(file->_cnt>0) {
2391 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2392 memcpy(ptr, file->_ptr, pcnt);
2393 file->_cnt -= pcnt;
2394 file->_ptr += pcnt;
2395 read += pcnt ;
2396 rcnt -= pcnt ;
2397 ptr = (char*)ptr + pcnt;
2398 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2399 if(file->_flag & MSVCRT__IORW) {
2400 file->_flag |= MSVCRT__IOREAD;
2401 } else
2402 return 0;
2404 if(rcnt)
2406 pread = _read(file->_file,ptr, rcnt);
2407 /* expose feof condition in the flags
2408 * MFC tests file->_flag for feof, and doesn't not call feof())
2410 if (pread == 0)
2411 file->_flag |= MSVCRT__IOEOF;
2412 else if (pread == -1)
2414 file->_flag |= MSVCRT__IOERR;
2415 pread = 0;
2418 read+=pread;
2419 return read / size;
2422 /*********************************************************************
2423 * freopen (MSVCRT.@)
2426 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2428 int open_flags, stream_flags, fd;
2430 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2431 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2432 return NULL;
2434 MSVCRT_fclose(file);
2436 /* map mode string to open() flags. "man fopen" for possibilities. */
2437 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2438 return NULL;
2440 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2442 if (fd < 0)
2443 return NULL;
2444 if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2446 file->_flag = 0;
2447 WARN(":failed-last error (%ld)\n",GetLastError());
2448 msvcrt_set_errno(GetLastError());
2449 return NULL;
2451 return file;
2454 /*********************************************************************
2455 * fsetpos (MSVCRT.@)
2457 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2459 return _lseek(file->_file,*pos,SEEK_SET);
2462 /*********************************************************************
2463 * ftell (MSVCRT.@)
2465 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2467 int off=0;
2468 long pos;
2469 if(file->_bufsiz) {
2470 if( file->_flag & MSVCRT__IOWRT ) {
2471 off = file->_ptr - file->_base;
2472 } else {
2473 off = -file->_cnt;
2476 pos = _tell(file->_file);
2477 if(pos == -1) return pos;
2478 return off + pos;
2481 /*********************************************************************
2482 * fgetpos (MSVCRT.@)
2484 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2486 *pos = MSVCRT_ftell(file);
2487 return (*pos == -1? -1 : 0);
2490 /*********************************************************************
2491 * fputs (MSVCRT.@)
2493 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2495 size_t i, len = strlen(s);
2496 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2497 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2498 for (i=0; i<len; i++)
2499 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2500 return MSVCRT_EOF;
2501 return 0;
2504 /*********************************************************************
2505 * fputws (MSVCRT.@)
2507 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2509 size_t i, len = strlenW(s);
2510 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2511 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2512 for (i=0; i<len; i++)
2514 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2515 return MSVCRT_WEOF;
2516 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2517 return MSVCRT_WEOF;
2519 return 0;
2522 /*********************************************************************
2523 * getchar (MSVCRT.@)
2525 int MSVCRT_getchar(void)
2527 return MSVCRT_fgetc(MSVCRT_stdin);
2530 /*********************************************************************
2531 * getc (MSVCRT.@)
2533 int MSVCRT_getc(MSVCRT_FILE* file)
2535 return MSVCRT_fgetc(file);
2538 /*********************************************************************
2539 * gets (MSVCRT.@)
2541 char *MSVCRT_gets(char *buf)
2543 int cc;
2544 char * buf_start = buf;
2546 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2547 cc = MSVCRT_fgetc(MSVCRT_stdin))
2548 if(cc != '\r') *buf++ = (char)cc;
2550 *buf = '\0';
2552 TRACE("got '%s'\n", buf_start);
2553 return buf_start;
2556 /*********************************************************************
2557 * _getws (MSVCRT.@)
2559 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2561 MSVCRT_wint_t cc;
2562 MSVCRT_wchar_t* ws = buf;
2564 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2565 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2567 if (cc != '\r')
2568 *buf++ = (MSVCRT_wchar_t)cc;
2570 *buf = '\0';
2572 TRACE("got '%s'\n", debugstr_w(ws));
2573 return ws;
2576 /*********************************************************************
2577 * putc (MSVCRT.@)
2579 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2581 return MSVCRT_fputc(c, file);
2584 /*********************************************************************
2585 * putchar (MSVCRT.@)
2587 int MSVCRT_putchar(int c)
2589 return MSVCRT_fputc(c, MSVCRT_stdout);
2592 /*********************************************************************
2593 * puts (MSVCRT.@)
2595 int MSVCRT_puts(const char *s)
2597 size_t len = strlen(s);
2598 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2599 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2602 /*********************************************************************
2603 * _putws (MSVCRT.@)
2605 int _putws(const MSVCRT_wchar_t *s)
2607 static const MSVCRT_wchar_t nl = '\n';
2608 size_t len = strlenW(s);
2609 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2610 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2613 /*********************************************************************
2614 * remove (MSVCRT.@)
2616 int MSVCRT_remove(const char *path)
2618 TRACE("(%s)\n",path);
2619 if (DeleteFileA(path))
2620 return 0;
2621 TRACE(":failed (%ld)\n",GetLastError());
2622 msvcrt_set_errno(GetLastError());
2623 return -1;
2626 /*********************************************************************
2627 * _wremove (MSVCRT.@)
2629 int _wremove(const MSVCRT_wchar_t *path)
2631 TRACE("(%s)\n",debugstr_w(path));
2632 if (DeleteFileW(path))
2633 return 0;
2634 TRACE(":failed (%ld)\n",GetLastError());
2635 msvcrt_set_errno(GetLastError());
2636 return -1;
2639 /*********************************************************************
2640 * rename (MSVCRT.@)
2642 int MSVCRT_rename(const char *oldpath,const char *newpath)
2644 TRACE(":from %s to %s\n",oldpath,newpath);
2645 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2646 return 0;
2647 TRACE(":failed (%ld)\n",GetLastError());
2648 msvcrt_set_errno(GetLastError());
2649 return -1;
2652 /*********************************************************************
2653 * _wrename (MSVCRT.@)
2655 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2657 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2658 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2659 return 0;
2660 TRACE(":failed (%ld)\n",GetLastError());
2661 msvcrt_set_errno(GetLastError());
2662 return -1;
2665 /*********************************************************************
2666 * setvbuf (MSVCRT.@)
2668 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2670 /* TODO: Check if file busy */
2671 if(file->_bufsiz) {
2672 MSVCRT_free(file->_base);
2673 file->_bufsiz = 0;
2674 file->_cnt = 0;
2676 if(mode == MSVCRT__IOFBF) {
2677 file->_flag &= ~MSVCRT__IONBF;
2678 file->_base = file->_ptr = buf;
2679 if(buf) {
2680 file->_bufsiz = size;
2682 } else {
2683 file->_flag |= MSVCRT__IONBF;
2685 return 0;
2688 /*********************************************************************
2689 * setbuf (MSVCRT.@)
2691 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2693 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2696 /*********************************************************************
2697 * tmpnam (MSVCRT.@)
2699 char *MSVCRT_tmpnam(char *s)
2701 static int unique;
2702 char tmpstr[16];
2703 char *p;
2704 int count;
2705 if (s == 0)
2706 s = MSVCRT_tmpname;
2707 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2708 p = s + sprintf(s, "\\s%s.", tmpstr);
2709 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2711 msvcrt_int_to_base32(unique++, tmpstr);
2712 strcpy(p, tmpstr);
2713 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2714 GetLastError() == ERROR_FILE_NOT_FOUND)
2715 break;
2717 return s;
2720 /*********************************************************************
2721 * tmpfile (MSVCRT.@)
2723 MSVCRT_FILE* MSVCRT_tmpfile(void)
2725 char *filename = MSVCRT_tmpnam(NULL);
2726 int fd;
2727 MSVCRT_FILE* file = NULL;
2729 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2730 if (fd != -1 && (file = msvcrt_alloc_fp()))
2732 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2734 file->_flag = 0;
2735 file = NULL;
2737 else file->_tmpfname = _strdup(filename);
2739 return file;
2742 /*********************************************************************
2743 * vfprintf (MSVCRT.@)
2745 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2747 char buf[2048], *mem = buf;
2748 int written, resize = sizeof(buf), retval;
2749 /* There are two conventions for vsnprintf failing:
2750 * Return -1 if we truncated, or
2751 * Return the number of bytes that would have been written
2752 * The code below handles both cases
2754 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2755 written > resize)
2757 resize = (written == -1 ? resize * 2 : written + 1);
2758 if (mem != buf)
2759 MSVCRT_free (mem);
2760 if (!(mem = (char *)MSVCRT_malloc(resize)))
2761 return MSVCRT_EOF;
2763 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2764 if (mem != buf)
2765 MSVCRT_free (mem);
2766 return retval;
2769 /*********************************************************************
2770 * vfwprintf (MSVCRT.@)
2771 * FIXME:
2772 * Is final char included in written (then resize is too big) or not
2773 * (then we must test for equality too)?
2775 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2777 MSVCRT_wchar_t buf[2048], *mem = buf;
2778 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2779 /* See vfprintf comments */
2780 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2781 written > resize)
2783 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2784 if (mem != buf)
2785 MSVCRT_free (mem);
2786 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2787 return MSVCRT_EOF;
2789 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2790 if (mem != buf)
2791 MSVCRT_free (mem);
2792 return retval;
2795 /*********************************************************************
2796 * vprintf (MSVCRT.@)
2798 int MSVCRT_vprintf(const char *format, va_list valist)
2800 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2803 /*********************************************************************
2804 * vwprintf (MSVCRT.@)
2806 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2808 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2811 /*********************************************************************
2812 * fprintf (MSVCRT.@)
2814 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2816 va_list valist;
2817 int res;
2818 va_start(valist, format);
2819 res = MSVCRT_vfprintf(file, format, valist);
2820 va_end(valist);
2821 return res;
2824 /*********************************************************************
2825 * fwprintf (MSVCRT.@)
2827 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2829 va_list valist;
2830 int res;
2831 va_start(valist, format);
2832 res = MSVCRT_vfwprintf(file, format, valist);
2833 va_end(valist);
2834 return res;
2837 /*********************************************************************
2838 * printf (MSVCRT.@)
2840 int MSVCRT_printf(const char *format, ...)
2842 va_list valist;
2843 int res;
2844 va_start(valist, format);
2845 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2846 va_end(valist);
2847 return res;
2850 /*********************************************************************
2851 * ungetc (MSVCRT.@)
2853 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2855 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2856 msvcrt_alloc_buffer(file);
2857 file->_ptr++;
2859 if(file->_ptr>file->_base) {
2860 file->_ptr--;
2861 *file->_ptr=c;
2862 file->_cnt++;
2863 return c;
2865 return MSVCRT_EOF;
2868 /*********************************************************************
2869 * ungetwc (MSVCRT.@)
2871 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2873 MSVCRT_wchar_t mwc = wc;
2874 char * pp = (char *)&mwc;
2875 int i;
2876 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2877 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2878 return MSVCRT_WEOF;
2880 return mwc;
2883 /*********************************************************************
2884 * wprintf (MSVCRT.@)
2886 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2888 va_list valist;
2889 int res;
2890 va_start(valist, format);
2891 res = MSVCRT_vwprintf(format, valist);
2892 va_end(valist);
2893 return res;
2896 /*********************************************************************
2897 * __pioinfo (MSVCRT.@)
2898 * FIXME: see MSVCRT_MAX_FILES define.
2900 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
2901 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
2902 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
2903 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
2904 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
2905 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
2906 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
2907 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
2908 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
2909 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
2910 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
2911 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
2914 /*********************************************************************
2915 * __badioinfo (MSVCRT.@)
2917 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };