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
26 #include "wine/port.h"
34 #include <sys/types.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 xflag */
59 #define WX_DONTINHERIT 0x10
60 #define WX_APPEND 0x20
63 /* FIXME: Make this dynamic */
64 #define MSVCRT_MAX_FILES 257
69 } MSVCRT_fdesc[MSVCRT_MAX_FILES];
71 MSVCRT_FILE MSVCRT__iob[3];
73 static int MSVCRT_fdstart = 3; /* first unallocated fd */
74 static int MSVCRT_fdend = 3; /* highest allocated fd */
76 /* FIXME: make this dynamic */
77 static MSVCRT_FILE* MSVCRT_fstreams[1024];
78 static int MSVCRT_stream_idx;
80 /* INTERNAL: process umask */
81 static int MSVCRT_umask = 0;
83 /* INTERNAL: Static buffer for temp file name */
84 static char MSVCRT_tmpname[MAX_PATH];
86 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
87 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
88 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
89 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
91 #define TOUL(x) (ULONGLONG)(x)
92 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
93 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
94 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
95 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
97 extern CRITICAL_SECTION MSVCRT_file_cs;
98 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
99 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
101 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64 *bufi64, struct MSVCRT__stat *buf)
103 buf->st_dev = bufi64->st_dev;
104 buf->st_ino = bufi64->st_ino;
105 buf->st_mode = bufi64->st_mode;
106 buf->st_nlink = bufi64->st_nlink;
107 buf->st_uid = bufi64->st_uid;
108 buf->st_gid = bufi64->st_gid;
109 buf->st_rdev = bufi64->st_rdev;
110 buf->st_size = bufi64->st_size;
111 buf->st_atime = bufi64->st_atime;
112 buf->st_mtime = bufi64->st_mtime;
113 buf->st_ctime = bufi64->st_ctime;
116 static inline BOOL msvcrt_is_valid_fd(int fd)
118 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].xflag & WX_OPEN);
121 /* INTERNAL: Get the HANDLE for a fd */
122 static HANDLE msvcrt_fdtoh(int fd)
124 if (!msvcrt_is_valid_fd(fd))
126 WARN(":fd (%d) - no handle!\n",fd);
127 *MSVCRT___doserrno() = 0;
128 *MSVCRT__errno() = MSVCRT_EBADF;
129 return INVALID_HANDLE_VALUE;
131 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
132 return MSVCRT_fdesc[fd].handle;
135 /* INTERNAL: free a file entry fd */
136 static void msvcrt_free_fd(int fd)
138 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
139 MSVCRT_fdesc[fd].xflag = 0;
140 TRACE(":fd (%d) freed\n",fd);
141 if (fd < 3) /* don't use 0,1,2 for user files */
145 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
146 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
147 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
152 if (fd == MSVCRT_fdend - 1)
154 if (fd < MSVCRT_fdstart)
159 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
160 static int msvcrt_alloc_fd(HANDLE hand, int flag)
162 int fd = MSVCRT_fdstart;
164 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
165 if (fd >= MSVCRT_MAX_FILES)
167 WARN(":files exhausted!\n");
170 MSVCRT_fdesc[fd].handle = hand;
171 MSVCRT_fdesc[fd].xflag = WX_OPEN;
172 if (flag & MSVCRT__O_NOINHERIT) MSVCRT_fdesc[fd].xflag |= WX_DONTINHERIT;
173 if (flag & MSVCRT__O_APPEND) MSVCRT_fdesc[fd].xflag |= WX_APPEND;
174 if (flag & MSVCRT__O_TEXT) MSVCRT_fdesc[fd].xflag |= WX_TEXT;
176 /* locate next free slot */
177 if (fd == MSVCRT_fdend)
178 MSVCRT_fdstart = ++MSVCRT_fdend;
180 while(MSVCRT_fdstart < MSVCRT_fdend &&
181 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
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;
194 /* INTERNAL: Allocate a FILE* for an fd slot
196 static MSVCRT_FILE* msvcrt_alloc_fp(void)
200 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
202 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
204 if (!MSVCRT_fstreams[i])
206 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
208 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
210 return MSVCRT_fstreams[i];
216 /* INTERNAL: initialize a FILE* from an open fd */
217 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
219 TRACE(":fd (%d) allocating FILE*\n",fd);
220 if (!msvcrt_is_valid_fd(fd))
222 WARN(":invalid fd %d\n",fd);
223 *MSVCRT___doserrno() = 0;
224 *MSVCRT__errno() = MSVCRT_EBADF;
227 memset(file, 0, sizeof(*file));
229 file->_flag = stream_flags;
231 TRACE(":got FILE* (%p)\n",file);
236 /* INTERNAL: Set up stdin, stderr and stdout */
237 void msvcrt_init_io(void)
241 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
242 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
243 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
244 MSVCRT_fdesc[0].xflag = WX_OPEN;
245 MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
246 MSVCRT__iob[0]._tmpfname = NULL;
247 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
248 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
249 MSVCRT_fdesc[1].xflag = WX_OPEN;
250 MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
251 MSVCRT__iob[1]._tmpfname = NULL;
252 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
253 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
254 MSVCRT_fdesc[2].xflag = WX_OPEN;
255 MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
256 MSVCRT__iob[2]._tmpfname = NULL;
258 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
259 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
261 for (i = 0; i < 3; i++)
263 /* FILE structs for stdin/out/err are static and never deleted */
264 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
265 MSVCRT__iob[i]._file = i;
267 MSVCRT_stream_idx = 3;
270 /* INTERNAL: Flush stdio file buffer */
271 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
274 int cnt=file->_ptr-file->_base;
275 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
276 file->_flag |= MSVCRT__IOERR;
279 file->_ptr=file->_base;
280 file->_cnt=file->_bufsiz;
285 /* INTERNAL: Allocate stdio file buffer */
286 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
288 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
290 file->_bufsiz = MSVCRT_BUFSIZ;
291 file->_flag |= MSVCRT__IOMYBUF;
293 file->_base = (unsigned char *)(&file->_charbuf);
295 file->_bufsiz = sizeof(file->_charbuf);
297 file->_ptr = file->_base;
301 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
302 static void msvcrt_int_to_base32(int num, char *str)
317 *p = (num & 31) + '0';
319 *p += ('a' - '0' - 10);
324 /*********************************************************************
327 MSVCRT_FILE *__p__iob(void)
329 return &MSVCRT__iob[0];
332 /*********************************************************************
335 int _access(const char *filename, int mode)
337 DWORD attr = GetFileAttributesA(filename);
339 TRACE("(%s,%d) %ld\n",filename,mode,attr);
341 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
343 msvcrt_set_errno(GetLastError());
346 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
348 msvcrt_set_errno(ERROR_ACCESS_DENIED);
354 /*********************************************************************
355 * _waccess (MSVCRT.@)
357 int _waccess(const MSVCRT_wchar_t *filename, int mode)
359 DWORD attr = GetFileAttributesW(filename);
361 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
363 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
365 msvcrt_set_errno(GetLastError());
368 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
370 msvcrt_set_errno(ERROR_ACCESS_DENIED);
376 /*********************************************************************
379 int _chmod(const char *path, int flags)
381 DWORD oldFlags = GetFileAttributesA(path);
383 if (oldFlags != INVALID_FILE_ATTRIBUTES)
385 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
386 oldFlags | FILE_ATTRIBUTE_READONLY;
388 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
391 msvcrt_set_errno(GetLastError());
395 /*********************************************************************
398 int _wchmod(const MSVCRT_wchar_t *path, int flags)
400 DWORD oldFlags = GetFileAttributesW(path);
402 if (oldFlags != INVALID_FILE_ATTRIBUTES)
404 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
405 oldFlags | FILE_ATTRIBUTE_READONLY;
407 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
410 msvcrt_set_errno(GetLastError());
414 /*********************************************************************
417 int _chsize(int fd, long size)
419 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
423 /*********************************************************************
428 FIXME("(od=%d): stub\n", od);
432 /*********************************************************************
435 int _dup2(int od, int nd)
437 FIXME("(od=%d, nd=%d): stub\n", od, nd);
441 /*********************************************************************
444 int _unlink(const char *path)
446 TRACE("(%s)\n",path);
447 if(DeleteFileA(path))
449 TRACE("failed (%ld)\n",GetLastError());
450 msvcrt_set_errno(GetLastError());
454 /*********************************************************************
455 * _wunlink (MSVCRT.@)
457 int _wunlink(const MSVCRT_wchar_t *path)
459 TRACE("(%s)\n",debugstr_w(path));
460 if(DeleteFileW(path))
462 TRACE("failed (%ld)\n",GetLastError());
463 msvcrt_set_errno(GetLastError());
467 /* _flushall calls MSVCRT_fflush which calls _flushall */
468 int MSVCRT_fflush(MSVCRT_FILE* file);
470 /*********************************************************************
471 * _flushall (MSVCRT.@)
475 int i, num_flushed = 0;
477 for (i = 3; i < MSVCRT_stream_idx; i++)
478 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
481 /* FIXME: flush, do not commit */
482 if (_commit(i) == -1)
483 if (MSVCRT_fstreams[i])
484 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
486 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
487 MSVCRT_fflush(MSVCRT_fstreams[i]);
492 TRACE(":flushed (%d) handles\n",num_flushed);
496 /*********************************************************************
499 int MSVCRT_fflush(MSVCRT_FILE* file)
505 int res=msvcrt_flush_buffer(file);
510 /*********************************************************************
515 HANDLE hand = msvcrt_fdtoh(fd);
517 TRACE(":fd (%d) handle (%p)\n",fd,hand);
518 if (hand == INVALID_HANDLE_VALUE)
521 if (!CloseHandle(hand))
523 WARN(":failed-last error (%ld)\n",GetLastError());
524 msvcrt_set_errno(GetLastError());
532 /*********************************************************************
537 HANDLE hand = msvcrt_fdtoh(fd);
539 TRACE(":fd (%d) handle (%p)\n",fd,hand);
540 if (hand == INVALID_HANDLE_VALUE)
543 if (!FlushFileBuffers(hand))
545 if (GetLastError() == ERROR_INVALID_HANDLE)
547 /* FlushFileBuffers fails for console handles
548 * so we ignore this error.
552 TRACE(":failed-last error (%ld)\n",GetLastError());
553 msvcrt_set_errno(GetLastError());
560 /*********************************************************************
565 DWORD curpos,endpos,hcurpos,hendpos;
566 HANDLE hand = msvcrt_fdtoh(fd);
568 TRACE(":fd (%d) handle (%p)\n",fd,hand);
570 if (hand == INVALID_HANDLE_VALUE)
573 if (MSVCRT_fdesc[fd].xflag & WX_ATEOF) return TRUE;
575 /* Otherwise we do it the hard way */
576 hcurpos = hendpos = 0;
577 curpos = SetFilePointer(hand, 0, &hcurpos, SEEK_CUR);
578 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
580 if (curpos == endpos && hcurpos == hendpos)
583 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
587 /*********************************************************************
588 * _fcloseall (MSVCRT.@)
590 int MSVCRT__fcloseall(void)
592 int num_closed = 0, i;
594 for (i = 3; i < MSVCRT_stream_idx; i++)
595 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
596 MSVCRT_fclose(MSVCRT_fstreams[i]))
599 TRACE(":closed (%d) handles\n",num_closed);
603 /* free everything on process exit */
604 void msvcrt_free_io(void)
612 /*********************************************************************
613 * _lseeki64 (MSVCRT.@)
615 __int64 _lseeki64(int fd, __int64 offset, int whence)
617 DWORD ret, hoffset = (DWORD) (offset >> 32);
618 HANDLE hand = msvcrt_fdtoh(fd);
620 TRACE(":fd (%d) handle (%p)\n",fd,hand);
621 if (hand == INVALID_HANDLE_VALUE)
624 if (whence < 0 || whence > 2)
626 *MSVCRT__errno() = MSVCRT_EINVAL;
630 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
631 fd,hoffset,(long)offset,
632 (whence==SEEK_SET)?"SEEK_SET":
633 (whence==SEEK_CUR)?"SEEK_CUR":
634 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
636 ret = SetFilePointer(hand, (long)offset, &hoffset, whence);
637 if (ret != INVALID_SET_FILE_POINTER || !GetLastError())
639 MSVCRT_fdesc[fd].xflag &= ~WX_ATEOF;
640 /* FIXME: What if we seek _to_ EOF - is EOF set? */
642 return ((__int64)hoffset << 32) | ret;
644 TRACE(":error-last error (%ld)\n",GetLastError());
645 msvcrt_set_errno(GetLastError());
649 /*********************************************************************
652 LONG _lseek(int fd, LONG offset, int whence)
654 return _lseeki64(fd, offset, whence);
657 /*********************************************************************
658 * _locking (MSVCRT.@)
660 * This is untested; the underlying LockFile doesn't work yet.
662 int _locking(int fd, int mode, LONG nbytes)
666 HANDLE hand = msvcrt_fdtoh(fd);
668 TRACE(":fd (%d) handle (%p)\n",fd,hand);
669 if (hand == INVALID_HANDLE_VALUE)
672 if (mode < 0 || mode > 4)
674 *MSVCRT__errno() = MSVCRT_EINVAL;
678 TRACE(":fd (%d) by 0x%08lx mode %s\n",
679 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
680 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
681 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
682 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
683 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
686 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
688 FIXME ("Seek failed\n");
689 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
692 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
695 ret = 1; /* just to satisfy gcc */
698 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
703 else if (mode == MSVCRT__LK_UNLCK)
704 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
706 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
707 /* FIXME - what about error settings? */
711 /*********************************************************************
714 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
716 /* Flush output if needed */
717 if(file->_flag & MSVCRT__IOWRT)
718 msvcrt_flush_buffer(file);
720 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
721 offset -= file->_cnt;
723 /* Discard buffered input */
725 file->_ptr = file->_base;
726 /* Reset direction of i/o */
727 if(file->_flag & MSVCRT__IORW) {
728 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
730 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
733 /*********************************************************************
734 * clearerr (MSVCRT.@)
736 void MSVCRT_clearerr(MSVCRT_FILE* file)
738 TRACE(":file (%p) fd (%d)\n",file,file->_file);
739 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
742 /*********************************************************************
745 void MSVCRT_rewind(MSVCRT_FILE* file)
747 TRACE(":file (%p) fd (%d)\n",file,file->_file);
748 MSVCRT_fseek(file, 0L, SEEK_SET);
749 MSVCRT_clearerr(file);
752 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
754 int plus = strchr(mode, '+') != NULL;
759 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
760 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
763 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
764 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
767 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
768 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
778 *open_flags |= MSVCRT__O_BINARY;
779 *open_flags &= ~MSVCRT__O_TEXT;
782 *open_flags |= MSVCRT__O_TEXT;
783 *open_flags &= ~MSVCRT__O_BINARY;
788 FIXME(":unknown flag %c not supported\n",mode[-1]);
793 /*********************************************************************
796 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
798 int open_flags, stream_flags;
801 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
803 if (!(file = msvcrt_alloc_fp())) return NULL;
804 if (msvcrt_init_fp(file, fd, stream_flags) == -1)
809 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
814 /*********************************************************************
815 * _wfdopen (MSVCRT.@)
817 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
819 unsigned mlen = strlenW(mode);
820 char *modea = MSVCRT_calloc(mlen + 1, 1);
821 MSVCRT_FILE* file = NULL;
822 int open_flags, stream_flags;
825 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
827 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
828 if (!(file = msvcrt_alloc_fp())) return NULL;
829 if (msvcrt_init_fp(file, fd, stream_flags) == -1)
837 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
838 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
844 /*********************************************************************
845 * _filelength (MSVCRT.@)
847 LONG _filelength(int fd)
849 LONG curPos = _lseek(fd, 0, SEEK_CUR);
852 LONG endPos = _lseek(fd, 0, SEEK_END);
855 if (endPos != curPos)
856 _lseek(fd, curPos, SEEK_SET);
863 /*********************************************************************
864 * _filelengthi64 (MSVCRT.@)
866 __int64 _filelengthi64(int fd)
868 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
871 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
874 if (endPos != curPos)
875 _lseeki64(fd, curPos, SEEK_SET);
882 /*********************************************************************
885 int MSVCRT__fileno(MSVCRT_FILE* file)
887 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
891 /*********************************************************************
892 * _fstati64 (MSVCRT.@)
894 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
897 BY_HANDLE_FILE_INFORMATION hfi;
898 HANDLE hand = msvcrt_fdtoh(fd);
900 TRACE(":fd (%d) stat (%p)\n",fd,buf);
901 if (hand == INVALID_HANDLE_VALUE)
906 WARN(":failed-NULL buf\n");
907 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
911 memset(&hfi, 0, sizeof(hfi));
912 memset(buf, 0, sizeof(struct MSVCRT__stati64));
913 if (!GetFileInformationByHandle(hand, &hfi))
915 WARN(":failed-last error (%ld)\n",GetLastError());
916 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
919 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
920 buf->st_nlink = hfi.nNumberOfLinks;
921 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
922 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
924 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
925 buf->st_mtime = buf->st_ctime = dw;
929 /*********************************************************************
932 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
934 struct MSVCRT__stati64 bufi64;
936 ret = MSVCRT__fstati64(fd, &bufi64);
938 msvcrt_cp_from_stati64(&bufi64, buf);
942 /*********************************************************************
945 int _futime(int fd, struct MSVCRT__utimbuf *t)
947 HANDLE hand = msvcrt_fdtoh(fd);
952 MSVCRT_time_t currTime;
953 MSVCRT_time(&currTime);
954 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
955 memcpy(&wt, &at, sizeof(wt));
959 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
960 if (t->actime == t->modtime)
961 memcpy(&wt, &at, sizeof(wt));
963 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
966 if (!SetFileTime(hand, NULL, &at, &wt))
968 msvcrt_set_errno(GetLastError());
974 /*********************************************************************
975 * _get_osfhandle (MSVCRT.@)
977 long _get_osfhandle(int fd)
979 HANDLE hand = msvcrt_fdtoh(fd);
980 HANDLE newhand = hand;
981 TRACE(":fd (%d) handle (%p)\n",fd,hand);
983 if (hand != INVALID_HANDLE_VALUE)
985 /* FIXME: I'm not convinced that I should be copying the
986 * handle here - it may be leaked if the app doesn't
987 * close it (and the API docs don't say that it should)
988 * Not duplicating it means that it can't be inherited
989 * and so lcc's wedit doesn't cope when it passes it to
990 * child processes. I've an idea that it should either
991 * be copied by CreateProcess, or marked as inheritable
992 * when initialised, or maybe both? JG 21-9-00.
994 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
995 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
997 return (long)newhand;
1000 /*********************************************************************
1001 * _isatty (MSVCRT.@)
1005 HANDLE hand = msvcrt_fdtoh(fd);
1007 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1008 if (hand == INVALID_HANDLE_VALUE)
1011 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1014 /*********************************************************************
1015 * _mktemp (MSVCRT.@)
1017 char *_mktemp(char *pattern)
1020 char *retVal = pattern;
1025 numX = (*pattern++ == 'X')? numX + 1 : 0;
1029 id = GetCurrentProcessId();
1033 int tempNum = id / 10;
1034 *pattern-- = id - (tempNum * 10) + '0';
1040 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1041 GetLastError() == ERROR_FILE_NOT_FOUND)
1043 *pattern = letter++;
1044 } while(letter != '|');
1048 /*********************************************************************
1049 * _wmktemp (MSVCRT.@)
1051 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
1054 MSVCRT_wchar_t *retVal = pattern;
1056 MSVCRT_wchar_t letter = 'a';
1059 numX = (*pattern++ == 'X')? numX + 1 : 0;
1063 id = GetCurrentProcessId();
1067 int tempNum = id / 10;
1068 *pattern-- = id - (tempNum * 10) + '0';
1074 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1075 GetLastError() == ERROR_FILE_NOT_FOUND)
1077 *pattern = letter++;
1078 } while(letter != '|');
1082 /*********************************************************************
1085 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1089 DWORD access = 0, creation = 0, attrib;
1093 SECURITY_ATTRIBUTES sa;
1096 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1097 path, oflags, shflags);
1099 switch(oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1101 case MSVCRT__O_RDONLY:
1102 access |= GENERIC_READ;
1103 ioflag |= MSVCRT__IOREAD;
1105 case MSVCRT__O_WRONLY:
1106 access |= GENERIC_WRITE;
1107 ioflag |= MSVCRT__IOWRT;
1109 case MSVCRT__O_RDWR:
1110 access |= GENERIC_WRITE | GENERIC_READ;
1111 ioflag |= MSVCRT__IORW;
1115 if (oflags & MSVCRT__O_CREAT)
1117 va_start(ap, shflags);
1118 pmode = va_arg(ap, int);
1121 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1122 FIXME(": pmode 0x%04x ignored\n", pmode);
1124 WARN(": pmode 0x%04x ignored\n", pmode);
1126 if (oflags & MSVCRT__O_EXCL)
1127 creation = CREATE_NEW;
1128 else if (oflags & MSVCRT__O_TRUNC)
1129 creation = CREATE_ALWAYS;
1131 creation = OPEN_ALWAYS;
1133 else /* no MSVCRT__O_CREAT */
1135 if (oflags & MSVCRT__O_TRUNC)
1136 creation = TRUNCATE_EXISTING;
1138 creation = OPEN_EXISTING;
1140 if (oflags & MSVCRT__O_APPEND)
1141 ioflag |= MSVCRT__O_APPEND;
1143 if (oflags & MSVCRT__O_BINARY)
1144 ioflag |= MSVCRT__O_BINARY;
1145 else if (oflags & MSVCRT__O_TEXT)
1146 ioflag |= MSVCRT__O_TEXT;
1147 else if (*__p__fmode() & MSVCRT__O_BINARY)
1148 ioflag |= MSVCRT__O_BINARY;
1150 ioflag |= MSVCRT__O_TEXT; /* default to TEXT*/
1154 case MSVCRT__SH_DENYRW:
1157 case MSVCRT__SH_DENYWR:
1158 sharing = FILE_SHARE_READ;
1160 case MSVCRT__SH_DENYRD:
1161 sharing = FILE_SHARE_WRITE;
1163 case MSVCRT__SH_DENYNO:
1164 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1167 ERR( "Unhandled shflags 0x%x\n", shflags );
1170 attrib = FILE_ATTRIBUTE_NORMAL;
1172 if (oflags & MSVCRT__O_TEMPORARY)
1174 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1176 sharing |= FILE_SHARE_DELETE;
1179 if (oflags & ~(MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|MSVCRT__O_TRUNC|MSVCRT__O_EXCL
1180 |MSVCRT__O_CREAT|MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|MSVCRT__O_NOINHERIT))
1181 ERR(":unsupported oflags 0x%04x\n",oflags);
1183 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1184 sa.lpSecurityDescriptor = NULL;
1185 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1187 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1189 if (hand == INVALID_HANDLE_VALUE) {
1190 WARN(":failed-last error (%ld)\n",GetLastError());
1191 msvcrt_set_errno(GetLastError());
1195 fd = msvcrt_alloc_fd(hand, ioflag);
1197 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1201 if (oflags & MSVCRT__O_APPEND)
1202 _lseek(fd, 0, FILE_END);
1208 /*********************************************************************
1209 * _wsopen (MSVCRT.@)
1211 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1213 const unsigned int len = strlenW(path);
1214 char *patha = MSVCRT_calloc(len + 1,1);
1218 va_start(ap, shflags);
1219 pmode = va_arg(ap, int);
1222 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1224 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1229 msvcrt_set_errno(GetLastError());
1233 /*********************************************************************
1236 int _open( const char *path, int flags, ... )
1240 if (flags & MSVCRT__O_CREAT)
1243 va_start(ap, flags);
1244 pmode = va_arg(ap, int);
1246 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1249 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1252 /*********************************************************************
1255 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1257 const unsigned int len = strlenW(path);
1258 char *patha = MSVCRT_calloc(len + 1,1);
1262 va_start(ap, flags);
1263 pmode = va_arg(ap, int);
1266 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1268 int retval = _open(patha,flags,pmode);
1273 msvcrt_set_errno(GetLastError());
1277 /*********************************************************************
1280 int _creat(const char *path, int flags)
1282 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1283 return _open(path, usedFlags);
1286 /*********************************************************************
1287 * _wcreat (MSVCRT.@)
1289 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1291 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1292 return _wopen(path, usedFlags);
1295 /*********************************************************************
1296 * _open_osfhandle (MSVCRT.@)
1298 int _open_osfhandle(long hand, int flags)
1302 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1303 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1304 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1305 * text - it never sets MSVCRT__O_BINARY.
1307 /* FIXME: handle more flags */
1308 if (!(flags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)) && (*__p__fmode() & MSVCRT__O_BINARY))
1309 flags |= MSVCRT__O_BINARY;
1311 flags |= MSVCRT__O_TEXT;
1313 fd = msvcrt_alloc_fd((HANDLE)hand,flags);
1314 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd,flags);
1318 /*********************************************************************
1323 int num_removed = 0, i;
1325 for (i = 3; i < MSVCRT_stream_idx; i++)
1326 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1328 MSVCRT_fclose(MSVCRT_fstreams[i]);
1333 TRACE(":removed (%d) temp files\n",num_removed);
1337 /*********************************************************************
1338 * (internal) remove_cr
1340 * Remove all \r inplace.
1341 * return the number of \r removed
1343 static unsigned int remove_cr(char *buf, unsigned int count)
1347 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1348 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1352 /*********************************************************************
1355 int _read(int fd, void *buf, unsigned int count)
1357 DWORD num_read, all_read = 0;
1358 char *bufstart = buf;
1359 HANDLE hand = msvcrt_fdtoh(fd);
1361 /* Don't trace small reads, it gets *very* annoying */
1363 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1364 if (hand == INVALID_HANDLE_VALUE)
1367 /* Reading single bytes in O_TEXT mode makes things slow
1368 * So read big chunks, then remove the \r in memory and try reading
1369 * the rest until the request is satisfied or EOF is met
1371 while (all_read < count)
1373 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1375 if (num_read != (count - all_read))
1378 MSVCRT_fdesc[fd].xflag |= WX_ATEOF;
1379 if (MSVCRT_fdesc[fd].xflag & WX_TEXT)
1380 num_read -= remove_cr(bufstart+all_read,num_read);
1381 all_read += num_read;
1383 TRACE("%s\n",debugstr_an(buf,all_read));
1386 if (MSVCRT_fdesc[fd].xflag & WX_TEXT)
1388 num_read -= remove_cr(bufstart+all_read,num_read);
1390 all_read += num_read;
1394 TRACE(":failed-last error (%ld)\n",GetLastError());
1400 TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
1404 /*********************************************************************
1407 int MSVCRT__getw(MSVCRT_FILE* file)
1410 switch (_read(file->_file, &i, sizeof(int)))
1413 case 0: file->_flag |= MSVCRT__IOEOF; break;
1414 default: file->_flag |= MSVCRT__IOERR; break;
1419 /*********************************************************************
1420 * _setmode (MSVCRT.@)
1422 int _setmode(int fd,int mode)
1424 int ret = MSVCRT_fdesc[fd].xflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1425 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1426 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1427 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1428 MSVCRT_fdesc[fd].xflag |= WX_TEXT;
1430 MSVCRT_fdesc[fd].xflag &= ~WX_TEXT;
1434 /*********************************************************************
1435 * _stati64 (MSVCRT.@)
1437 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1440 WIN32_FILE_ATTRIBUTE_DATA hfi;
1441 unsigned short mode = ALL_S_IREAD;
1444 TRACE(":file (%s) buf(%p)\n",path,buf);
1446 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1448 TRACE("failed (%ld)\n",GetLastError());
1449 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1453 memset(buf,0,sizeof(struct MSVCRT__stati64));
1455 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1456 Bon 011120: This FIXME seems incorrect
1457 Also a letter as first char isn't enough to be classified
1460 if (isalpha(*path)&& (*(path+1)==':'))
1461 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1463 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1465 plen = strlen(path);
1467 /* Dir, or regular file? */
1468 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1469 (path[plen-1] == '\\'))
1470 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1473 mode |= MSVCRT__S_IFREG;
1475 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1477 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1478 (tolower(path[plen-3]) << 16);
1479 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1480 mode |= ALL_S_IEXEC;
1484 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1485 mode |= ALL_S_IWRITE;
1487 buf->st_mode = mode;
1489 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1490 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1492 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1493 buf->st_mtime = buf->st_ctime = dw;
1494 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1495 (long)(buf->st_size >> 32),(long)buf->st_size,
1496 buf->st_atime,buf->st_mtime, buf->st_ctime);
1500 /*********************************************************************
1503 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1505 struct MSVCRT__stati64 bufi64;
1507 ret = MSVCRT__stati64( path, &bufi64);
1509 msvcrt_cp_from_stati64(&bufi64, buf);
1513 /*********************************************************************
1514 * _wstati64 (MSVCRT.@)
1516 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1519 WIN32_FILE_ATTRIBUTE_DATA hfi;
1520 unsigned short mode = ALL_S_IREAD;
1523 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1525 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1527 TRACE("failed (%ld)\n",GetLastError());
1528 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1532 memset(buf,0,sizeof(struct MSVCRT__stat));
1534 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1535 if (MSVCRT_iswalpha(*path))
1536 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1538 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1540 plen = strlenW(path);
1542 /* Dir, or regular file? */
1543 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1544 (path[plen-1] == '\\'))
1545 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1548 mode |= MSVCRT__S_IFREG;
1550 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1552 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1553 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1554 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1555 mode |= ALL_S_IEXEC;
1559 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1560 mode |= ALL_S_IWRITE;
1562 buf->st_mode = mode;
1564 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1565 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1567 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1568 buf->st_mtime = buf->st_ctime = dw;
1569 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1570 (long)(buf->st_size >> 32),(long)buf->st_size,
1571 buf->st_atime,buf->st_mtime, buf->st_ctime);
1575 /*********************************************************************
1578 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1581 struct MSVCRT__stati64 bufi64;
1583 ret = MSVCRT__wstati64( path, &bufi64 );
1584 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1588 /*********************************************************************
1593 return _lseek(fd, 0, SEEK_CUR);
1596 /*********************************************************************
1597 * _telli64 (MSVCRT.@)
1599 __int64 _telli64(int fd)
1601 return _lseeki64(fd, 0, SEEK_CUR);
1604 /*********************************************************************
1605 * _tempnam (MSVCRT.@)
1607 char *_tempnam(const char *dir, const char *prefix)
1609 char tmpbuf[MAX_PATH];
1611 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1612 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1614 TRACE("got name (%s)\n",tmpbuf);
1615 DeleteFileA(tmpbuf);
1616 return _strdup(tmpbuf);
1618 TRACE("failed (%ld)\n",GetLastError());
1622 /*********************************************************************
1623 * _wtempnam (MSVCRT.@)
1625 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1627 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1629 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1630 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1632 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1633 DeleteFileW(tmpbuf);
1634 return _wcsdup(tmpbuf);
1636 TRACE("failed (%ld)\n",GetLastError());
1640 /*********************************************************************
1643 int _umask(int umask)
1645 int old_umask = MSVCRT_umask;
1646 TRACE("(%d)\n",umask);
1647 MSVCRT_umask = umask;
1651 /*********************************************************************
1654 int _utime(const char* path, struct MSVCRT__utimbuf *t)
1656 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1660 int retVal = _futime(fd, t);
1667 /*********************************************************************
1668 * _wutime (MSVCRT.@)
1670 int _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1672 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1676 int retVal = _futime(fd, t);
1683 /*********************************************************************
1686 int _write(int fd, const void* buf, unsigned int count)
1689 HANDLE hand = msvcrt_fdtoh(fd);
1691 /* Don't trace small writes, it gets *very* annoying */
1694 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1696 if (hand == INVALID_HANDLE_VALUE)
1698 *MSVCRT__errno() = MSVCRT_EBADF;
1702 /* If appending, go to EOF */
1703 if (MSVCRT_fdesc[fd].xflag & WX_APPEND)
1704 _lseek(fd, 0, FILE_END);
1706 if (!(MSVCRT_fdesc[fd].xflag & WX_TEXT))
1708 if (WriteFile(hand, buf, count, &num_written, NULL)
1709 && (num_written == count))
1711 TRACE(":failed-last error (%ld)\n",GetLastError());
1712 *MSVCRT__errno() = MSVCRT_ENOSPC;
1716 unsigned int i, j, nr_lf;
1717 char *s=(char*)buf, *buf_start=(char*)buf, *p;
1718 /* find number of \n ( without preceeding \r */
1719 for ( nr_lf=0,i = 0; i <count; i++)
1724 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1729 if ((p = MSVCRT_malloc(count + nr_lf)))
1731 for(s=(char*)buf, i=0, j=0; i<count; i++)
1736 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1743 FIXME("Malloc failed\n");
1751 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
1753 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
1754 *MSVCRT__errno() = MSVCRT_ENOSPC;
1757 return s - buf_start;
1769 /*********************************************************************
1772 int MSVCRT__putw(int val, MSVCRT_FILE* file)
1775 len = _write(file->_file, &val, sizeof(val));
1776 if (len == sizeof(val)) return val;
1777 file->_flag |= MSVCRT__IOERR;
1781 /*********************************************************************
1784 int MSVCRT_fclose(MSVCRT_FILE* file)
1789 if (file->_tmpfname)
1791 MSVCRT_free(file->_tmpfname);
1792 file->_tmpfname = NULL;
1794 /* flush stdio buffers */
1795 if(file->_flag & MSVCRT__IOWRT)
1796 MSVCRT_fflush(file);
1797 if(file->_flag & MSVCRT__IOMYBUF)
1798 MSVCRT_free(file->_base);
1800 r=_close(file->_file);
1804 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1807 /*********************************************************************
1810 int MSVCRT_feof(MSVCRT_FILE* file)
1812 return file->_flag & MSVCRT__IOEOF;
1815 /*********************************************************************
1818 int MSVCRT_ferror(MSVCRT_FILE* file)
1820 return file->_flag & MSVCRT__IOERR;
1823 /*********************************************************************
1824 * _filbuf (MSVCRT.@)
1826 int MSVCRT__filbuf(MSVCRT_FILE* file)
1828 /* Allocate buffer if needed */
1829 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1830 msvcrt_alloc_buffer(file);
1832 if(!(file->_flag & MSVCRT__IOREAD)) {
1833 if(file->_flag & MSVCRT__IORW) {
1834 file->_flag |= MSVCRT__IOREAD;
1839 if(file->_flag & MSVCRT__IONBF) {
1842 if ((r = _read(file->_file,&c,1)) != 1) {
1843 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
1848 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1850 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
1855 file->_ptr = file->_base+1;
1856 return *(unsigned char *)file->_base;
1860 /*********************************************************************
1863 int MSVCRT_fgetc(MSVCRT_FILE* file)
1867 return *(unsigned char *)file->_ptr++;
1869 return MSVCRT__filbuf(file);
1873 /*********************************************************************
1874 * _fgetchar (MSVCRT.@)
1878 return MSVCRT_fgetc(MSVCRT_stdin);
1881 /*********************************************************************
1884 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1886 int cc = MSVCRT_EOF;
1887 char * buf_start = s;
1889 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1890 file,file->_file,s,size);
1892 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
1897 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1899 TRACE(":nothing read\n");
1902 if ((cc != MSVCRT_EOF) && (size > 1))
1905 TRACE(":got '%s'\n", debugstr_a(buf_start));
1909 /*********************************************************************
1912 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
1913 * the CR from CR/LF combinations
1915 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1919 if (!(MSVCRT_fdesc[file->_file].xflag & WX_TEXT))
1923 if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
1925 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
1930 c = MSVCRT_fgetc(file);
1931 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
1933 FIXME("Treat Multibyte characters\n");
1935 if (c == MSVCRT_EOF)
1938 return (MSVCRT_wint_t)c;
1941 /*********************************************************************
1944 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1946 return MSVCRT_fgetwc(file);
1949 /*********************************************************************
1950 * _fgetwchar (MSVCRT.@)
1952 MSVCRT_wint_t _fgetwchar(void)
1954 return MSVCRT_fgetwc(MSVCRT_stdin);
1957 /*********************************************************************
1958 * getwchar (MSVCRT.@)
1960 MSVCRT_wint_t MSVCRT_getwchar(void)
1962 return _fgetwchar();
1965 /*********************************************************************
1968 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
1970 int cc = MSVCRT_WEOF;
1971 MSVCRT_wchar_t * buf_start = s;
1973 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1974 file,file->_file,s,size);
1976 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
1981 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
1983 TRACE(":nothing read\n");
1986 if ((cc != MSVCRT_WEOF) && (size > 1))
1989 TRACE(":got %s\n", debugstr_w(buf_start));
1993 /*********************************************************************
1996 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1998 MSVCRT_size_t wrcnt=size * nmemb;
2003 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2004 memcpy(file->_ptr, ptr, pcnt);
2009 ptr = (char*)ptr + pcnt;
2010 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2011 if(file->_flag & MSVCRT__IORW) {
2012 file->_flag |= MSVCRT__IOWRT;
2018 int res=msvcrt_flush_buffer(file);
2020 int pwritten = _write(file->_file, ptr, wrcnt);
2023 file->_flag |= MSVCRT__IOERR;
2026 written += pwritten;
2029 return written / size;
2032 /*********************************************************************
2035 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2037 MSVCRT_wchar_t mwc=wc;
2038 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2043 /*********************************************************************
2044 * _fputwchar (MSVCRT.@)
2046 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
2048 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2051 /*********************************************************************
2054 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
2057 int open_flags, stream_flags, fd;
2059 TRACE("(%s,%s)\n",path,mode);
2061 /* map mode string to open() flags. "man fopen" for possibilities. */
2062 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2065 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2070 if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags) != -1)
2071 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2078 TRACE(":got (%p)\n",file);
2084 /*********************************************************************
2085 * _wfopen (MSVCRT.@)
2087 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2089 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2090 char *patha = MSVCRT_calloc(plen + 1, 1);
2091 char *modea = MSVCRT_calloc(mlen + 1, 1);
2093 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2095 if (patha && modea &&
2096 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2097 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2099 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2105 msvcrt_set_errno(GetLastError());
2109 /*********************************************************************
2110 * _fsopen (MSVCRT.@)
2112 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
2114 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2115 return MSVCRT_fopen(path,mode);
2118 /*********************************************************************
2119 * _wfsopen (MSVCRT.@)
2121 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2123 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2124 debugstr_w(path),debugstr_w(mode),share);
2125 return MSVCRT__wfopen(path,mode);
2128 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2129 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2131 /*********************************************************************
2134 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2141 return MSVCRT__flsbuf(c, file);
2145 /*********************************************************************
2146 * _flsbuf (MSVCRT.@)
2148 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2150 /* Flush output buffer */
2151 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2152 msvcrt_alloc_buffer(file);
2154 if(!(file->_flag & MSVCRT__IOWRT)) {
2155 if(file->_flag & MSVCRT__IORW) {
2156 file->_flag |= MSVCRT__IOWRT;
2162 int res=msvcrt_flush_buffer(file);
2163 return res?res : MSVCRT_fputc(c, file);
2167 len = _write(file->_file, &cc, 1);
2168 if (len == 1) return c;
2169 file->_flag |= MSVCRT__IOERR;
2174 /*********************************************************************
2175 * _fputchar (MSVCRT.@)
2177 int _fputchar(int c)
2179 return MSVCRT_fputc(c, MSVCRT_stdout);
2182 /*********************************************************************
2185 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2186 { MSVCRT_size_t rcnt=size * nmemb;
2187 MSVCRT_size_t read=0;
2190 /* first buffered data */
2192 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2193 memcpy(ptr, file->_ptr, pcnt);
2198 ptr = (char*)ptr + pcnt;
2199 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2200 if(file->_flag & MSVCRT__IORW) {
2201 file->_flag |= MSVCRT__IOREAD;
2207 pread = _read(file->_file,ptr, rcnt);
2208 /* expose feof condition in the flags
2209 * MFC tests file->_flag for feof, and doesn't not call feof())
2212 file->_flag |= MSVCRT__IOEOF;
2213 else if (pread == -1)
2215 file->_flag |= MSVCRT__IOERR;
2223 /*********************************************************************
2224 * freopen (MSVCRT.@)
2227 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2229 int open_flags, stream_flags, fd;
2231 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2232 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2235 MSVCRT_fclose(file);
2237 /* map mode string to open() flags. "man fopen" for possibilities. */
2238 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2241 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2245 if (msvcrt_init_fp(file, fd, stream_flags) != -1)
2248 WARN(":failed-last error (%ld)\n",GetLastError());
2249 msvcrt_set_errno(GetLastError());
2255 /*********************************************************************
2256 * fsetpos (MSVCRT.@)
2258 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2260 return _lseek(file->_file,*pos,SEEK_SET);
2263 /*********************************************************************
2266 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2271 if( file->_flag & MSVCRT__IOWRT ) {
2272 off = file->_ptr - file->_base;
2277 pos = _tell(file->_file);
2278 if(pos == -1) return pos;
2282 /*********************************************************************
2283 * fgetpos (MSVCRT.@)
2285 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2287 *pos = MSVCRT_ftell(file);
2288 return (*pos == -1? -1 : 0);
2291 /*********************************************************************
2294 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2296 size_t i, len = strlen(s);
2297 if (!(MSVCRT_fdesc[file->_file].xflag & WX_TEXT))
2298 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2299 for (i=0; i<len; i++)
2300 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2305 /*********************************************************************
2308 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2310 size_t i, len = strlenW(s);
2311 if (!(MSVCRT_fdesc[file->_file].xflag & WX_TEXT))
2312 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2313 for (i=0; i<len; i++)
2315 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2317 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2323 /*********************************************************************
2324 * getchar (MSVCRT.@)
2326 int MSVCRT_getchar(void)
2328 return MSVCRT_fgetc(MSVCRT_stdin);
2331 /*********************************************************************
2334 int MSVCRT_getc(MSVCRT_FILE* file)
2336 return MSVCRT_fgetc(file);
2339 /*********************************************************************
2342 char *MSVCRT_gets(char *buf)
2345 char * buf_start = buf;
2347 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2348 cc = MSVCRT_fgetc(MSVCRT_stdin))
2349 if(cc != '\r') *buf++ = (char)cc;
2353 TRACE("got '%s'\n", buf_start);
2357 /*********************************************************************
2360 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2363 MSVCRT_wchar_t* ws = buf;
2365 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2366 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2369 *buf++ = (MSVCRT_wchar_t)cc;
2373 TRACE("got '%s'\n", debugstr_w(ws));
2377 /*********************************************************************
2380 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2382 return MSVCRT_fputc(c, file);
2385 /*********************************************************************
2386 * putchar (MSVCRT.@)
2388 int MSVCRT_putchar(int c)
2390 return MSVCRT_fputc(c, MSVCRT_stdout);
2393 /*********************************************************************
2396 int MSVCRT_puts(const char *s)
2398 size_t len = strlen(s);
2399 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2400 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2403 /*********************************************************************
2406 int _putws(const MSVCRT_wchar_t *s)
2408 static const MSVCRT_wchar_t nl = '\n';
2409 size_t len = strlenW(s);
2410 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2411 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2414 /*********************************************************************
2417 int MSVCRT_remove(const char *path)
2419 TRACE("(%s)\n",path);
2420 if (DeleteFileA(path))
2422 TRACE(":failed (%ld)\n",GetLastError());
2423 msvcrt_set_errno(GetLastError());
2427 /*********************************************************************
2428 * _wremove (MSVCRT.@)
2430 int _wremove(const MSVCRT_wchar_t *path)
2432 TRACE("(%s)\n",debugstr_w(path));
2433 if (DeleteFileW(path))
2435 TRACE(":failed (%ld)\n",GetLastError());
2436 msvcrt_set_errno(GetLastError());
2440 /*********************************************************************
2443 int MSVCRT_rename(const char *oldpath,const char *newpath)
2445 TRACE(":from %s to %s\n",oldpath,newpath);
2446 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2448 TRACE(":failed (%ld)\n",GetLastError());
2449 msvcrt_set_errno(GetLastError());
2453 /*********************************************************************
2454 * _wrename (MSVCRT.@)
2456 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2458 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2459 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2461 TRACE(":failed (%ld)\n",GetLastError());
2462 msvcrt_set_errno(GetLastError());
2466 /*********************************************************************
2467 * setvbuf (MSVCRT.@)
2469 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2471 /* TODO: Check if file busy */
2473 MSVCRT_free(file->_base);
2477 if(mode == MSVCRT__IOFBF) {
2478 file->_flag &= ~MSVCRT__IONBF;
2479 file->_base = file->_ptr = buf;
2481 file->_bufsiz = size;
2484 file->_flag |= MSVCRT__IONBF;
2489 /*********************************************************************
2492 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2494 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2497 /*********************************************************************
2500 char *MSVCRT_tmpnam(char *s)
2508 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2509 p = s + sprintf(s, "\\s%s.", tmpstr);
2510 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2512 msvcrt_int_to_base32(unique++, tmpstr);
2514 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2515 GetLastError() == ERROR_FILE_NOT_FOUND)
2521 /*********************************************************************
2522 * tmpfile (MSVCRT.@)
2524 MSVCRT_FILE* MSVCRT_tmpfile(void)
2526 char *filename = MSVCRT_tmpnam(NULL);
2528 MSVCRT_FILE* file = NULL;
2530 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2531 if (fd != -1 && (file = msvcrt_alloc_fp()))
2533 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2538 else file->_tmpfname = _strdup(filename);
2543 /*********************************************************************
2544 * vfprintf (MSVCRT.@)
2546 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2548 char buf[2048], *mem = buf;
2549 int written, resize = sizeof(buf), retval;
2550 /* There are two conventions for vsnprintf failing:
2551 * Return -1 if we truncated, or
2552 * Return the number of bytes that would have been written
2553 * The code below handles both cases
2555 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2558 resize = (written == -1 ? resize * 2 : written + 1);
2561 if (!(mem = (char *)MSVCRT_malloc(resize)))
2564 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2570 /*********************************************************************
2571 * vfwprintf (MSVCRT.@)
2573 * Is final char included in written (then resize is too big) or not
2574 * (then we must test for equality too)?
2576 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2578 MSVCRT_wchar_t buf[2048], *mem = buf;
2579 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2580 /* See vfprintf comments */
2581 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2584 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2587 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2590 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2596 /*********************************************************************
2597 * vprintf (MSVCRT.@)
2599 int MSVCRT_vprintf(const char *format, va_list valist)
2601 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2604 /*********************************************************************
2605 * vwprintf (MSVCRT.@)
2607 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2609 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2612 /*********************************************************************
2613 * fprintf (MSVCRT.@)
2615 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2619 va_start(valist, format);
2620 res = MSVCRT_vfprintf(file, format, valist);
2625 /*********************************************************************
2626 * fwprintf (MSVCRT.@)
2628 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2632 va_start(valist, format);
2633 res = MSVCRT_vfwprintf(file, format, valist);
2638 /*********************************************************************
2641 int MSVCRT_printf(const char *format, ...)
2645 va_start(valist, format);
2646 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2651 /*********************************************************************
2654 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2656 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2657 msvcrt_alloc_buffer(file);
2660 if(file->_ptr>file->_base) {
2669 /*********************************************************************
2670 * ungetwc (MSVCRT.@)
2672 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2674 MSVCRT_wchar_t mwc = wc;
2675 char * pp = (char *)&mwc;
2677 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2678 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2684 /*********************************************************************
2685 * wprintf (MSVCRT.@)
2687 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2691 va_start(valist, format);
2692 res = MSVCRT_vwprintf(format, valist);