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"
40 #include "msvcrt/errno.h"
42 #include "wine/unicode.h"
43 #include "msvcrt/fcntl.h"
44 #include "msvcrt/io.h"
45 #include "msvcrt/sys/locking.h"
46 #include "msvcrt/stdio.h"
47 #include "msvcrt/stdlib.h"
48 #include "msvcrt/string.h"
49 #include "msvcrt/sys/stat.h"
50 #include "msvcrt/sys/utime.h"
51 #include "msvcrt/time.h"
52 #include "msvcrt/share.h"
53 #include "msvcrt/wctype.h"
54 #include "msvcrt/direct.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
60 /* for stat mode, permissions apply to all,owner and group */
61 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
62 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
63 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
65 /* _access() bit flags FIXME: incomplete */
66 #define MSVCRT_W_OK 0x02
68 /* values for xflag */
71 #define WX_DONTINHERIT 0x10
72 #define WX_APPEND 0x20
75 /* FIXME: Make this dynamic */
76 #define MSVCRT_MAX_FILES 257
81 } MSVCRT_fdesc[MSVCRT_MAX_FILES];
83 MSVCRT_FILE MSVCRT__iob[3];
85 static int MSVCRT_fdstart = 3; /* first unallocated fd */
86 static int MSVCRT_fdend = 3; /* highest allocated fd */
88 /* FIXME: make this dynamic */
89 static MSVCRT_FILE* MSVCRT_fstreams[1024];
90 static int MSVCRT_stream_idx;
92 /* INTERNAL: process umask */
93 static int MSVCRT_umask = 0;
95 /* INTERNAL: Static buffer for temp file name */
96 static char MSVCRT_tmpname[MAX_PATH];
98 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
99 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
100 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
101 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
103 #define TOUL(x) (ULONGLONG)(x)
104 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
105 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
106 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
107 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
109 extern CRITICAL_SECTION MSVCRT_file_cs;
110 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
111 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
113 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64 *bufi64, struct MSVCRT__stat *buf)
115 buf->st_dev = bufi64->st_dev;
116 buf->st_ino = bufi64->st_ino;
117 buf->st_mode = bufi64->st_mode;
118 buf->st_nlink = bufi64->st_nlink;
119 buf->st_uid = bufi64->st_uid;
120 buf->st_gid = bufi64->st_gid;
121 buf->st_rdev = bufi64->st_rdev;
122 buf->st_size = bufi64->st_size;
123 buf->st_atime = bufi64->st_atime;
124 buf->st_mtime = bufi64->st_mtime;
125 buf->st_ctime = bufi64->st_ctime;
128 static inline BOOL msvcrt_is_valid_fd(int fd)
130 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].xflag & WX_OPEN);
133 /* INTERNAL: Get the HANDLE for a fd */
134 static HANDLE msvcrt_fdtoh(int fd)
136 if (!msvcrt_is_valid_fd(fd))
138 WARN(":fd (%d) - no handle!\n",fd);
139 *MSVCRT___doserrno() = 0;
140 *MSVCRT__errno() = MSVCRT_EBADF;
141 return INVALID_HANDLE_VALUE;
143 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
144 return MSVCRT_fdesc[fd].handle;
147 /* INTERNAL: free a file entry fd */
148 static void msvcrt_free_fd(int fd)
150 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
151 MSVCRT_fdesc[fd].xflag = 0;
152 TRACE(":fd (%d) freed\n",fd);
153 if (fd < 3) /* don't use 0,1,2 for user files */
157 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
158 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
159 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
164 if (fd == MSVCRT_fdend - 1)
166 if (fd < MSVCRT_fdstart)
171 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
172 static int msvcrt_alloc_fd(HANDLE hand, int flag)
174 int fd = MSVCRT_fdstart;
176 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
177 if (fd >= MSVCRT_MAX_FILES)
179 WARN(":files exhausted!\n");
182 MSVCRT_fdesc[fd].handle = hand;
183 MSVCRT_fdesc[fd].xflag = WX_OPEN;
184 if (flag & _O_NOINHERIT) MSVCRT_fdesc[fd].xflag |= WX_DONTINHERIT;
185 if (flag & _O_APPEND) MSVCRT_fdesc[fd].xflag |= WX_APPEND;
186 if (flag & _O_TEXT) MSVCRT_fdesc[fd].xflag |= WX_TEXT;
188 /* locate next free slot */
189 if (fd == MSVCRT_fdend)
190 MSVCRT_fdstart = ++MSVCRT_fdend;
192 while(MSVCRT_fdstart < MSVCRT_fdend &&
193 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
198 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
199 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
200 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
206 /* INTERNAL: Allocate a FILE* for an fd slot
208 static MSVCRT_FILE* msvcrt_alloc_fp(void)
212 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
214 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
216 if (!MSVCRT_fstreams[i])
218 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
220 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
222 return MSVCRT_fstreams[i];
228 /* INTERNAL: initialize a FILE* from an open fd */
229 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
231 TRACE(":fd (%d) allocating FILE*\n",fd);
232 if (!msvcrt_is_valid_fd(fd))
234 WARN(":invalid fd %d\n",fd);
235 *MSVCRT___doserrno() = 0;
236 *MSVCRT__errno() = MSVCRT_EBADF;
239 memset(file, 0, sizeof(*file));
241 file->_flag = stream_flags;
243 TRACE(":got FILE* (%p)\n",file);
248 /* INTERNAL: Set up stdin, stderr and stdout */
249 void msvcrt_init_io(void)
253 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
254 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
255 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
256 MSVCRT_fdesc[0].xflag = WX_OPEN;
257 MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
258 MSVCRT__iob[0]._tmpfname = NULL;
259 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
260 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
261 MSVCRT_fdesc[1].xflag = WX_OPEN;
262 MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
263 MSVCRT__iob[1]._tmpfname = NULL;
264 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
265 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
266 MSVCRT_fdesc[2].xflag = WX_OPEN;
267 MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
268 MSVCRT__iob[2]._tmpfname = NULL;
270 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
271 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
273 for (i = 0; i < 3; i++)
275 /* FILE structs for stdin/out/err are static and never deleted */
276 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
277 MSVCRT__iob[i]._file = i;
279 MSVCRT_stream_idx = 3;
282 /* INTERNAL: Flush stdio file buffer */
283 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
286 int cnt=file->_ptr-file->_base;
287 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
288 file->_flag |= MSVCRT__IOERR;
291 file->_ptr=file->_base;
292 file->_cnt=file->_bufsiz;
297 /* INTERNAL: Allocate stdio file buffer */
298 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
300 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
302 file->_bufsiz = MSVCRT_BUFSIZ;
303 file->_flag |= MSVCRT__IOMYBUF;
305 file->_base = (unsigned char *)(&file->_charbuf);
307 file->_bufsiz = sizeof(file->_charbuf);
309 file->_ptr = file->_base;
313 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
314 static void msvcrt_int_to_base32(int num, char *str)
329 *p = (num & 31) + '0';
331 *p += ('a' - '0' - 10);
336 /*********************************************************************
339 MSVCRT_FILE *__p__iob(void)
341 return &MSVCRT__iob[0];
344 /*********************************************************************
347 int _access(const char *filename, int mode)
349 DWORD attr = GetFileAttributesA(filename);
351 TRACE("(%s,%d) %ld\n",filename,mode,attr);
353 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
355 MSVCRT__set_errno(GetLastError());
358 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
360 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
366 /*********************************************************************
367 * _waccess (MSVCRT.@)
369 int _waccess(const MSVCRT_wchar_t *filename, int mode)
371 DWORD attr = GetFileAttributesW(filename);
373 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
375 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
377 MSVCRT__set_errno(GetLastError());
380 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
382 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
388 /*********************************************************************
391 int _chmod(const char *path, int flags)
393 DWORD oldFlags = GetFileAttributesA(path);
395 if (oldFlags != INVALID_FILE_ATTRIBUTES)
397 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
398 oldFlags | FILE_ATTRIBUTE_READONLY;
400 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
403 MSVCRT__set_errno(GetLastError());
407 /*********************************************************************
410 int _wchmod(const MSVCRT_wchar_t *path, int flags)
412 DWORD oldFlags = GetFileAttributesW(path);
414 if (oldFlags != INVALID_FILE_ATTRIBUTES)
416 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
417 oldFlags | FILE_ATTRIBUTE_READONLY;
419 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
422 MSVCRT__set_errno(GetLastError());
426 /*********************************************************************
429 int _chsize(int fd, long size)
431 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
435 /*********************************************************************
440 FIXME("(od=%d): stub\n", od);
444 /*********************************************************************
447 int _dup2(int od, int nd)
449 FIXME("(od=%d, nd=%d): stub\n", od, nd);
453 /*********************************************************************
456 int _unlink(const char *path)
458 TRACE("(%s)\n",path);
459 if(DeleteFileA(path))
461 TRACE("failed (%ld)\n",GetLastError());
462 MSVCRT__set_errno(GetLastError());
466 /*********************************************************************
467 * _wunlink (MSVCRT.@)
469 int _wunlink(const MSVCRT_wchar_t *path)
471 TRACE("(%s)\n",debugstr_w(path));
472 if(DeleteFileW(path))
474 TRACE("failed (%ld)\n",GetLastError());
475 MSVCRT__set_errno(GetLastError());
479 /* _flushall calls MSVCRT_fflush which calls _flushall */
480 int MSVCRT_fflush(MSVCRT_FILE* file);
482 /*********************************************************************
483 * _flushall (MSVCRT.@)
487 int i, num_flushed = 0;
489 for (i = 3; i < MSVCRT_stream_idx; i++)
490 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
493 /* FIXME: flush, do not commit */
494 if (_commit(i) == -1)
495 if (MSVCRT_fstreams[i])
496 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
498 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
499 MSVCRT_fflush(MSVCRT_fstreams[i]);
504 TRACE(":flushed (%d) handles\n",num_flushed);
508 /*********************************************************************
511 int MSVCRT_fflush(MSVCRT_FILE* file)
517 int res=msvcrt_flush_buffer(file);
522 /*********************************************************************
527 HANDLE hand = msvcrt_fdtoh(fd);
529 TRACE(":fd (%d) handle (%p)\n",fd,hand);
530 if (hand == INVALID_HANDLE_VALUE)
533 if (!CloseHandle(hand))
535 WARN(":failed-last error (%ld)\n",GetLastError());
536 MSVCRT__set_errno(GetLastError());
544 /*********************************************************************
549 HANDLE hand = msvcrt_fdtoh(fd);
551 TRACE(":fd (%d) handle (%p)\n",fd,hand);
552 if (hand == INVALID_HANDLE_VALUE)
555 if (!FlushFileBuffers(hand))
557 if (GetLastError() == ERROR_INVALID_HANDLE)
559 /* FlushFileBuffers fails for console handles
560 * so we ignore this error.
564 TRACE(":failed-last error (%ld)\n",GetLastError());
565 MSVCRT__set_errno(GetLastError());
572 /*********************************************************************
577 DWORD curpos,endpos,hcurpos,hendpos;
578 HANDLE hand = msvcrt_fdtoh(fd);
580 TRACE(":fd (%d) handle (%p)\n",fd,hand);
582 if (hand == INVALID_HANDLE_VALUE)
585 if (MSVCRT_fdesc[fd].xflag & WX_ATEOF) return TRUE;
587 /* Otherwise we do it the hard way */
588 hcurpos = hendpos = 0;
589 curpos = SetFilePointer(hand, 0, &hcurpos, SEEK_CUR);
590 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
592 if (curpos == endpos && hcurpos == hendpos)
595 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
599 /*********************************************************************
600 * _fcloseall (MSVCRT.@)
602 int MSVCRT__fcloseall(void)
604 int num_closed = 0, i;
606 for (i = 3; i < MSVCRT_stream_idx; i++)
607 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
608 MSVCRT_fclose(MSVCRT_fstreams[i]))
611 TRACE(":closed (%d) handles\n",num_closed);
615 /* free everything on process exit */
616 void msvcrt_free_io(void)
624 /*********************************************************************
625 * _lseeki64 (MSVCRT.@)
627 __int64 _lseeki64(int fd, __int64 offset, int whence)
629 DWORD ret, hoffset = (DWORD) (offset >> 32);
630 HANDLE hand = msvcrt_fdtoh(fd);
632 TRACE(":fd (%d) handle (%p)\n",fd,hand);
633 if (hand == INVALID_HANDLE_VALUE)
636 if (whence < 0 || whence > 2)
638 *MSVCRT__errno() = MSVCRT_EINVAL;
642 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
643 fd,hoffset,(long)offset,
644 (whence==SEEK_SET)?"SEEK_SET":
645 (whence==SEEK_CUR)?"SEEK_CUR":
646 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
648 ret = SetFilePointer(hand, (long)offset, &hoffset, whence);
649 if (ret != INVALID_SET_FILE_POINTER || !GetLastError())
651 MSVCRT_fdesc[fd].xflag &= ~WX_ATEOF;
652 /* FIXME: What if we seek _to_ EOF - is EOF set? */
654 return ((__int64)hoffset << 32) | ret;
656 TRACE(":error-last error (%ld)\n",GetLastError());
657 MSVCRT__set_errno(GetLastError());
661 /*********************************************************************
664 LONG _lseek(int fd, LONG offset, int whence)
666 return _lseeki64(fd, offset, whence);
669 /*********************************************************************
670 * _locking (MSVCRT.@)
672 * This is untested; the underlying LockFile doesn't work yet.
674 int _locking(int fd, int mode, LONG nbytes)
678 HANDLE hand = msvcrt_fdtoh(fd);
680 TRACE(":fd (%d) handle (%p)\n",fd,hand);
681 if (hand == INVALID_HANDLE_VALUE)
684 if (mode < 0 || mode > 4)
686 *MSVCRT__errno() = MSVCRT_EINVAL;
690 TRACE(":fd (%d) by 0x%08lx mode %s\n",
691 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
692 (mode==_LK_LOCK)?"_LK_LOCK":
693 (mode==_LK_NBLCK)?"_LK_NBLCK":
694 (mode==_LK_RLCK)?"_LK_RLCK":
695 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
698 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
700 FIXME ("Seek failed\n");
701 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
704 if (mode == _LK_LOCK || mode == _LK_RLCK)
707 ret = 1; /* just to satisfy gcc */
710 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
715 else if (mode == _LK_UNLCK)
716 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
718 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
719 /* FIXME - what about error settings? */
723 /*********************************************************************
726 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
728 /* Flush output if needed */
729 if(file->_flag & MSVCRT__IOWRT)
730 msvcrt_flush_buffer(file);
732 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
733 offset -= file->_cnt;
735 /* Discard buffered input */
737 file->_ptr = file->_base;
738 /* Reset direction of i/o */
739 if(file->_flag & MSVCRT__IORW) {
740 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
742 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
745 /*********************************************************************
746 * clearerr (MSVCRT.@)
748 void MSVCRT_clearerr(MSVCRT_FILE* file)
750 TRACE(":file (%p) fd (%d)\n",file,file->_file);
751 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
754 /*********************************************************************
757 void MSVCRT_rewind(MSVCRT_FILE* file)
759 TRACE(":file (%p) fd (%d)\n",file,file->_file);
760 MSVCRT_fseek(file, 0L, SEEK_SET);
761 MSVCRT_clearerr(file);
764 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
766 int plus = strchr(mode, '+') != NULL;
771 *open_flags = plus ? _O_RDWR : _O_RDONLY;
772 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
775 *open_flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
776 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
779 *open_flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
780 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
790 *open_flags |= _O_BINARY;
791 *open_flags &= ~_O_TEXT;
794 *open_flags |= _O_TEXT;
795 *open_flags &= ~_O_BINARY;
800 FIXME(":unknown flag %c not supported\n",mode[-1]);
805 /*********************************************************************
808 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
810 int open_flags, stream_flags;
813 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
815 if (!(file = msvcrt_alloc_fp())) return NULL;
816 if (msvcrt_init_fp(file, fd, stream_flags) == -1)
821 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
826 /*********************************************************************
827 * _wfdopen (MSVCRT.@)
829 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
831 unsigned mlen = strlenW(mode);
832 char *modea = MSVCRT_calloc(mlen + 1, 1);
833 MSVCRT_FILE* file = NULL;
834 int open_flags, stream_flags;
837 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
839 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
840 if (!(file = msvcrt_alloc_fp())) return NULL;
841 if (msvcrt_init_fp(file, fd, stream_flags) == -1)
849 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
850 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
856 /*********************************************************************
857 * _filelength (MSVCRT.@)
859 LONG _filelength(int fd)
861 LONG curPos = _lseek(fd, 0, SEEK_CUR);
864 LONG endPos = _lseek(fd, 0, SEEK_END);
867 if (endPos != curPos)
868 _lseek(fd, curPos, SEEK_SET);
875 /*********************************************************************
876 * _filelengthi64 (MSVCRT.@)
878 __int64 _filelengthi64(int fd)
880 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
883 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
886 if (endPos != curPos)
887 _lseeki64(fd, curPos, SEEK_SET);
894 /*********************************************************************
897 int MSVCRT__fileno(MSVCRT_FILE* file)
899 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
903 /*********************************************************************
904 * _fstati64 (MSVCRT.@)
906 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
909 BY_HANDLE_FILE_INFORMATION hfi;
910 HANDLE hand = msvcrt_fdtoh(fd);
912 TRACE(":fd (%d) stat (%p)\n",fd,buf);
913 if (hand == INVALID_HANDLE_VALUE)
918 WARN(":failed-NULL buf\n");
919 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
923 memset(&hfi, 0, sizeof(hfi));
924 memset(buf, 0, sizeof(struct MSVCRT__stati64));
925 if (!GetFileInformationByHandle(hand, &hfi))
927 WARN(":failed-last error (%ld)\n",GetLastError());
928 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
931 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
932 buf->st_nlink = hfi.nNumberOfLinks;
933 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
934 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
936 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
937 buf->st_mtime = buf->st_ctime = dw;
941 /*********************************************************************
944 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
946 struct MSVCRT__stati64 bufi64;
948 ret = MSVCRT__fstati64(fd, &bufi64);
950 msvcrt_cp_from_stati64(&bufi64, buf);
954 /*********************************************************************
957 int _futime(int fd, struct _utimbuf *t)
959 HANDLE hand = msvcrt_fdtoh(fd);
964 MSVCRT_time_t currTime;
965 MSVCRT_time(&currTime);
966 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
967 memcpy(&wt, &at, sizeof(wt));
971 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
972 if (t->actime == t->modtime)
973 memcpy(&wt, &at, sizeof(wt));
975 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
978 if (!SetFileTime(hand, NULL, &at, &wt))
980 MSVCRT__set_errno(GetLastError());
986 /*********************************************************************
987 * _get_osfhandle (MSVCRT.@)
989 long _get_osfhandle(int fd)
991 HANDLE hand = msvcrt_fdtoh(fd);
992 HANDLE newhand = hand;
993 TRACE(":fd (%d) handle (%p)\n",fd,hand);
995 if (hand != INVALID_HANDLE_VALUE)
997 /* FIXME: I'm not convinced that I should be copying the
998 * handle here - it may be leaked if the app doesn't
999 * close it (and the API docs don't say that it should)
1000 * Not duplicating it means that it can't be inherited
1001 * and so lcc's wedit doesn't cope when it passes it to
1002 * child processes. I've an idea that it should either
1003 * be copied by CreateProcess, or marked as inheritable
1004 * when initialised, or maybe both? JG 21-9-00.
1006 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
1007 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
1009 return (long)newhand;
1012 /*********************************************************************
1013 * _isatty (MSVCRT.@)
1017 HANDLE hand = msvcrt_fdtoh(fd);
1019 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1020 if (hand == INVALID_HANDLE_VALUE)
1023 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1026 /*********************************************************************
1027 * _mktemp (MSVCRT.@)
1029 char *_mktemp(char *pattern)
1032 char *retVal = pattern;
1037 numX = (*pattern++ == 'X')? numX + 1 : 0;
1041 id = GetCurrentProcessId();
1045 int tempNum = id / 10;
1046 *pattern-- = id - (tempNum * 10) + '0';
1052 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1053 GetLastError() == ERROR_FILE_NOT_FOUND)
1055 *pattern = letter++;
1056 } while(letter != '|');
1060 /*********************************************************************
1061 * _wmktemp (MSVCRT.@)
1063 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
1066 MSVCRT_wchar_t *retVal = pattern;
1068 MSVCRT_wchar_t letter = 'a';
1071 numX = (*pattern++ == 'X')? numX + 1 : 0;
1075 id = GetCurrentProcessId();
1079 int tempNum = id / 10;
1080 *pattern-- = id - (tempNum * 10) + '0';
1086 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1087 GetLastError() == ERROR_FILE_NOT_FOUND)
1089 *pattern = letter++;
1090 } while(letter != '|');
1094 /*********************************************************************
1097 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1101 DWORD access = 0, creation = 0, attrib;
1105 SECURITY_ATTRIBUTES sa;
1108 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1109 path, oflags, shflags);
1111 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
1114 access |= GENERIC_READ;
1115 ioflag |= MSVCRT__IOREAD;
1118 access |= GENERIC_WRITE;
1119 ioflag |= MSVCRT__IOWRT;
1122 access |= GENERIC_WRITE | GENERIC_READ;
1123 ioflag |= MSVCRT__IORW;
1127 if (oflags & _O_CREAT)
1129 va_start(ap, shflags);
1130 pmode = va_arg(ap, int);
1133 if(pmode & ~(_S_IREAD | _S_IWRITE))
1134 FIXME(": pmode 0x%04x ignored\n", pmode);
1136 WARN(": pmode 0x%04x ignored\n", pmode);
1138 if (oflags & _O_EXCL)
1139 creation = CREATE_NEW;
1140 else if (oflags & _O_TRUNC)
1141 creation = CREATE_ALWAYS;
1143 creation = OPEN_ALWAYS;
1145 else /* no _O_CREAT */
1147 if (oflags & _O_TRUNC)
1148 creation = TRUNCATE_EXISTING;
1150 creation = OPEN_EXISTING;
1152 if (oflags & _O_APPEND)
1153 ioflag |= _O_APPEND;
1155 if (oflags & _O_BINARY)
1156 ioflag |= _O_BINARY;
1157 else if (oflags & _O_TEXT)
1159 else if (*__p__fmode() & _O_BINARY)
1160 ioflag |= _O_BINARY;
1162 ioflag |= _O_TEXT; /* default to TEXT*/
1170 sharing = FILE_SHARE_READ;
1173 sharing = FILE_SHARE_WRITE;
1176 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1179 ERR( "Unhandled shflags 0x%x\n", shflags );
1182 attrib = FILE_ATTRIBUTE_NORMAL;
1184 if (oflags & _O_TEMPORARY)
1186 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1188 sharing |= FILE_SHARE_DELETE;
1191 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
1192 |_O_CREAT|_O_RDWR|_O_WRONLY|_O_TEMPORARY|_O_NOINHERIT))
1193 ERR(":unsupported oflags 0x%04x\n",oflags);
1195 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1196 sa.lpSecurityDescriptor = NULL;
1197 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
1199 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1201 if (hand == INVALID_HANDLE_VALUE) {
1202 WARN(":failed-last error (%ld)\n",GetLastError());
1203 MSVCRT__set_errno(GetLastError());
1207 fd = msvcrt_alloc_fd(hand, ioflag);
1209 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1213 if (oflags & _O_APPEND)
1214 _lseek(fd, 0, FILE_END);
1220 /*********************************************************************
1221 * _wsopen (MSVCRT.@)
1223 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1225 const unsigned int len = strlenW(path);
1226 char *patha = MSVCRT_calloc(len + 1,1);
1230 va_start(ap, shflags);
1231 pmode = va_arg(ap, int);
1234 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1236 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1241 MSVCRT__set_errno(GetLastError());
1245 /*********************************************************************
1248 int _open( const char *path, int flags, ... )
1252 if (flags & _O_CREAT)
1255 va_start(ap, flags);
1256 pmode = va_arg(ap, int);
1258 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1261 return MSVCRT__sopen( path, flags, _SH_DENYNO);
1264 /*********************************************************************
1267 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1269 const unsigned int len = strlenW(path);
1270 char *patha = MSVCRT_calloc(len + 1,1);
1274 va_start(ap, flags);
1275 pmode = va_arg(ap, int);
1278 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1280 int retval = _open(patha,flags,pmode);
1285 MSVCRT__set_errno(GetLastError());
1289 /*********************************************************************
1292 int _creat(const char *path, int flags)
1294 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1295 return _open(path, usedFlags);
1298 /*********************************************************************
1299 * _wcreat (MSVCRT.@)
1301 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1303 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1304 return _wopen(path, usedFlags);
1307 /*********************************************************************
1308 * _open_osfhandle (MSVCRT.@)
1310 int _open_osfhandle(long hand, int flags)
1314 /* _O_RDONLY (0) always matches, so set the read flag
1315 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1316 * file, so set the write flag. It also only sets _O_TEXT if it wants
1317 * text - it never sets _O_BINARY.
1319 /* FIXME: handle more flags */
1320 if (!(flags & (_O_BINARY | _O_TEXT)) && (*__p__fmode() & _O_BINARY))
1325 fd = msvcrt_alloc_fd((HANDLE)hand,flags);
1326 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd,flags);
1330 /*********************************************************************
1335 int num_removed = 0, i;
1337 for (i = 3; i < MSVCRT_stream_idx; i++)
1338 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1340 MSVCRT_fclose(MSVCRT_fstreams[i]);
1345 TRACE(":removed (%d) temp files\n",num_removed);
1349 /*********************************************************************
1350 * (internal) remove_cr
1352 * Remove all \r inplace.
1353 * return the number of \r removed
1355 static unsigned int remove_cr(char *buf, unsigned int count)
1359 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1360 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1364 /*********************************************************************
1367 int _read(int fd, void *buf, unsigned int count)
1369 DWORD num_read, all_read = 0;
1370 char *bufstart = buf;
1371 HANDLE hand = msvcrt_fdtoh(fd);
1373 /* Don't trace small reads, it gets *very* annoying */
1375 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1376 if (hand == INVALID_HANDLE_VALUE)
1379 /* Reading single bytes in O_TEXT mode makes things slow
1380 * So read big chunks, then remove the \r in memory and try reading
1381 * the rest until the request is satisfied or EOF is met
1383 while (all_read < count)
1385 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1387 if (num_read != (count - all_read))
1390 MSVCRT_fdesc[fd].xflag |= WX_ATEOF;
1391 if (MSVCRT_fdesc[fd].xflag & WX_TEXT)
1392 num_read -= remove_cr(bufstart+all_read,num_read);
1393 all_read += num_read;
1395 TRACE("%s\n",debugstr_an(buf,all_read));
1398 if (MSVCRT_fdesc[fd].xflag & WX_TEXT)
1400 num_read -= remove_cr(bufstart+all_read,num_read);
1402 all_read += num_read;
1406 TRACE(":failed-last error (%ld)\n",GetLastError());
1412 TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
1416 /*********************************************************************
1419 int MSVCRT__getw(MSVCRT_FILE* file)
1422 switch (_read(file->_file, &i, sizeof(int)))
1425 case 0: file->_flag |= MSVCRT__IOEOF; break;
1426 default: file->_flag |= MSVCRT__IOERR; break;
1431 /*********************************************************************
1432 * _setmode (MSVCRT.@)
1434 int _setmode(int fd,int mode)
1436 int ret = MSVCRT_fdesc[fd].xflag & WX_TEXT ? _O_TEXT : _O_BINARY;
1437 if (mode & (~(_O_TEXT|_O_BINARY)))
1438 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1439 if ((mode & _O_TEXT) == _O_TEXT)
1440 MSVCRT_fdesc[fd].xflag |= WX_TEXT;
1442 MSVCRT_fdesc[fd].xflag &= WX_TEXT;
1446 /*********************************************************************
1447 * _stati64 (MSVCRT.@)
1449 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1452 WIN32_FILE_ATTRIBUTE_DATA hfi;
1453 unsigned short mode = MSVCRT_S_IREAD;
1456 TRACE(":file (%s) buf(%p)\n",path,buf);
1458 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1460 TRACE("failed (%ld)\n",GetLastError());
1461 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1465 memset(buf,0,sizeof(struct MSVCRT__stati64));
1467 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1468 Bon 011120: This FIXME seems incorrect
1469 Also a letter as first char isn't enough to be classified
1472 if (isalpha(*path)&& (*(path+1)==':'))
1473 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1475 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1477 plen = strlen(path);
1479 /* Dir, or regular file? */
1480 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1481 (path[plen-1] == '\\'))
1482 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1487 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1489 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1490 (tolower(path[plen-3]) << 16);
1491 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1492 mode |= MSVCRT_S_IEXEC;
1496 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1497 mode |= MSVCRT_S_IWRITE;
1499 buf->st_mode = mode;
1501 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1502 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1504 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1505 buf->st_mtime = buf->st_ctime = dw;
1506 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1507 (long)(buf->st_size >> 32),(long)buf->st_size,
1508 buf->st_atime,buf->st_mtime, buf->st_ctime);
1512 /*********************************************************************
1515 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1517 struct MSVCRT__stati64 bufi64;
1519 ret = MSVCRT__stati64( path, &bufi64);
1521 msvcrt_cp_from_stati64(&bufi64, buf);
1525 /*********************************************************************
1526 * _wstati64 (MSVCRT.@)
1528 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1531 WIN32_FILE_ATTRIBUTE_DATA hfi;
1532 unsigned short mode = MSVCRT_S_IREAD;
1535 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1537 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1539 TRACE("failed (%ld)\n",GetLastError());
1540 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1544 memset(buf,0,sizeof(struct MSVCRT__stat));
1546 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1547 if (MSVCRT_iswalpha(*path))
1548 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1550 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1552 plen = strlenW(path);
1554 /* Dir, or regular file? */
1555 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1556 (path[plen-1] == '\\'))
1557 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1562 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1564 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1565 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1566 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1567 mode |= MSVCRT_S_IEXEC;
1571 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1572 mode |= MSVCRT_S_IWRITE;
1574 buf->st_mode = mode;
1576 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1577 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1579 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1580 buf->st_mtime = buf->st_ctime = dw;
1581 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1582 (long)(buf->st_size >> 32),(long)buf->st_size,
1583 buf->st_atime,buf->st_mtime, buf->st_ctime);
1587 /*********************************************************************
1590 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1593 struct MSVCRT__stati64 bufi64;
1595 ret = MSVCRT__wstati64( path, &bufi64 );
1596 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1600 /*********************************************************************
1605 return _lseek(fd, 0, SEEK_CUR);
1608 /*********************************************************************
1609 * _telli64 (MSVCRT.@)
1611 __int64 _telli64(int fd)
1613 return _lseeki64(fd, 0, SEEK_CUR);
1616 /*********************************************************************
1617 * _tempnam (MSVCRT.@)
1619 char *_tempnam(const char *dir, const char *prefix)
1621 char tmpbuf[MAX_PATH];
1623 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1624 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1626 TRACE("got name (%s)\n",tmpbuf);
1627 DeleteFileA(tmpbuf);
1628 return _strdup(tmpbuf);
1630 TRACE("failed (%ld)\n",GetLastError());
1634 /*********************************************************************
1635 * _wtempnam (MSVCRT.@)
1637 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1639 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1641 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1642 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1644 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1645 DeleteFileW(tmpbuf);
1646 return _wcsdup(tmpbuf);
1648 TRACE("failed (%ld)\n",GetLastError());
1652 /*********************************************************************
1655 int _umask(int umask)
1657 int old_umask = MSVCRT_umask;
1658 TRACE("(%d)\n",umask);
1659 MSVCRT_umask = umask;
1663 /*********************************************************************
1666 int _utime(const char* path, struct _utimbuf *t)
1668 int fd = _open(path, _O_WRONLY | _O_BINARY);
1672 int retVal = _futime(fd, t);
1679 /*********************************************************************
1680 * _wutime (MSVCRT.@)
1682 int _wutime(const MSVCRT_wchar_t* path, struct _utimbuf *t)
1684 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1688 int retVal = _futime(fd, t);
1695 /*********************************************************************
1698 int _write(int fd, const void* buf, unsigned int count)
1701 HANDLE hand = msvcrt_fdtoh(fd);
1703 /* Don't trace small writes, it gets *very* annoying */
1706 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1708 if (hand == INVALID_HANDLE_VALUE)
1710 *MSVCRT__errno() = MSVCRT_EBADF;
1714 /* If appending, go to EOF */
1715 if (MSVCRT_fdesc[fd].xflag & WX_APPEND)
1716 _lseek(fd, 0, FILE_END);
1718 if (!(MSVCRT_fdesc[fd].xflag & WX_TEXT))
1720 if (WriteFile(hand, buf, count, &num_written, NULL)
1721 && (num_written == count))
1723 TRACE(":failed-last error (%ld)\n",GetLastError());
1724 *MSVCRT__errno() = MSVCRT_ENOSPC;
1728 unsigned int i, j, nr_lf;
1729 char *s=(char*)buf, *buf_start=(char*)buf, *p;
1730 /* find number of \n ( without preceeding \r */
1731 for ( nr_lf=0,i = 0; i <count; i++)
1736 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1741 if ((p = MSVCRT_malloc(count + nr_lf)))
1743 for(s=(char*)buf, i=0, j=0; i<count; i++)
1748 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1755 FIXME("Malloc failed\n");
1763 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
1765 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
1766 *MSVCRT__errno() = MSVCRT_ENOSPC;
1769 return s - buf_start;
1781 /*********************************************************************
1784 int MSVCRT__putw(int val, MSVCRT_FILE* file)
1787 len = _write(file->_file, &val, sizeof(val));
1788 if (len == sizeof(val)) return val;
1789 file->_flag |= MSVCRT__IOERR;
1793 /*********************************************************************
1796 int MSVCRT_fclose(MSVCRT_FILE* file)
1801 if (file->_tmpfname)
1803 MSVCRT_free(file->_tmpfname);
1804 file->_tmpfname = NULL;
1806 /* flush stdio buffers */
1807 if(file->_flag & MSVCRT__IOWRT)
1808 MSVCRT_fflush(file);
1809 if(file->_flag & MSVCRT__IOMYBUF)
1810 MSVCRT_free(file->_base);
1812 r=_close(file->_file);
1816 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1819 /*********************************************************************
1822 int MSVCRT_feof(MSVCRT_FILE* file)
1824 return file->_flag & MSVCRT__IOEOF;
1827 /*********************************************************************
1830 int MSVCRT_ferror(MSVCRT_FILE* file)
1832 return file->_flag & MSVCRT__IOERR;
1835 /*********************************************************************
1836 * _filbuf (MSVCRT.@)
1838 int MSVCRT__filbuf(MSVCRT_FILE* file)
1840 /* Allocate buffer if needed */
1841 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1842 msvcrt_alloc_buffer(file);
1844 if(!(file->_flag & MSVCRT__IOREAD)) {
1845 if(file->_flag & MSVCRT__IORW) {
1846 file->_flag |= MSVCRT__IOREAD;
1851 if(file->_flag & MSVCRT__IONBF) {
1854 if ((r = _read(file->_file,&c,1)) != 1) {
1855 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
1860 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1862 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
1867 file->_ptr = file->_base+1;
1868 return *(unsigned char *)file->_base;
1872 /*********************************************************************
1875 int MSVCRT_fgetc(MSVCRT_FILE* file)
1879 return *(unsigned char *)file->_ptr++;
1881 return MSVCRT__filbuf(file);
1885 /*********************************************************************
1886 * _fgetchar (MSVCRT.@)
1890 return MSVCRT_fgetc(MSVCRT_stdin);
1893 /*********************************************************************
1896 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1898 int cc = MSVCRT_EOF;
1899 char * buf_start = s;
1901 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1902 file,file->_file,s,size);
1904 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
1909 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1911 TRACE(":nothing read\n");
1914 if ((cc != MSVCRT_EOF) && (size > 1))
1917 TRACE(":got '%s'\n", debugstr_a(buf_start));
1921 /*********************************************************************
1924 * In _O_TEXT mode, multibyte characters are read from the file, dropping
1925 * the CR from CR/LF combinations
1927 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1931 if (!(MSVCRT_fdesc[file->_file].xflag & WX_TEXT))
1935 if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
1937 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
1942 c = MSVCRT_fgetc(file);
1943 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
1945 FIXME("Treat Multibyte characters\n");
1947 if (c == MSVCRT_EOF)
1950 return (MSVCRT_wint_t)c;
1953 /*********************************************************************
1956 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1958 return MSVCRT_fgetwc(file);
1961 /*********************************************************************
1962 * _fgetwchar (MSVCRT.@)
1964 MSVCRT_wint_t _fgetwchar(void)
1966 return MSVCRT_fgetwc(MSVCRT_stdin);
1969 /*********************************************************************
1970 * getwchar (MSVCRT.@)
1972 MSVCRT_wint_t MSVCRT_getwchar(void)
1974 return _fgetwchar();
1977 /*********************************************************************
1980 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
1982 int cc = MSVCRT_WEOF;
1983 MSVCRT_wchar_t * buf_start = s;
1985 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1986 file,file->_file,s,size);
1988 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
1993 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
1995 TRACE(":nothing read\n");
1998 if ((cc != MSVCRT_WEOF) && (size > 1))
2001 TRACE(":got %s\n", debugstr_w(buf_start));
2005 /*********************************************************************
2008 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2010 MSVCRT_size_t wrcnt=size * nmemb;
2015 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2016 memcpy(file->_ptr, ptr, pcnt);
2021 ptr = (char*)ptr + pcnt;
2022 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2023 if(file->_flag & MSVCRT__IORW) {
2024 file->_flag |= MSVCRT__IOWRT;
2030 int res=msvcrt_flush_buffer(file);
2032 int pwritten = _write(file->_file, ptr, wrcnt);
2035 file->_flag |= MSVCRT__IOERR;
2038 written += pwritten;
2041 return written / size;
2044 /*********************************************************************
2047 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2049 MSVCRT_wchar_t mwc=wc;
2050 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2055 /*********************************************************************
2056 * _fputwchar (MSVCRT.@)
2058 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
2060 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2063 /*********************************************************************
2066 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
2069 int open_flags, stream_flags, fd;
2071 TRACE("(%s,%s)\n",path,mode);
2073 /* map mode string to open() flags. "man fopen" for possibilities. */
2074 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2077 fd = _open(path, open_flags, _S_IREAD | _S_IWRITE);
2082 if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags) != -1)
2083 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2090 TRACE(":got (%p)\n",file);
2096 /*********************************************************************
2097 * _wfopen (MSVCRT.@)
2099 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2101 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2102 char *patha = MSVCRT_calloc(plen + 1, 1);
2103 char *modea = MSVCRT_calloc(mlen + 1, 1);
2105 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2107 if (patha && modea &&
2108 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2109 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2111 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2117 MSVCRT__set_errno(GetLastError());
2121 /*********************************************************************
2122 * _fsopen (MSVCRT.@)
2124 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
2126 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2127 return MSVCRT_fopen(path,mode);
2130 /*********************************************************************
2131 * _wfsopen (MSVCRT.@)
2133 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2135 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2136 debugstr_w(path),debugstr_w(mode),share);
2137 return MSVCRT__wfopen(path,mode);
2140 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2141 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2143 /*********************************************************************
2146 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2153 return MSVCRT__flsbuf(c, file);
2157 /*********************************************************************
2158 * _flsbuf (MSVCRT.@)
2160 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2162 /* Flush output buffer */
2163 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2164 msvcrt_alloc_buffer(file);
2166 if(!(file->_flag & MSVCRT__IOWRT)) {
2167 if(file->_flag & MSVCRT__IORW) {
2168 file->_flag |= MSVCRT__IOWRT;
2174 int res=msvcrt_flush_buffer(file);
2175 return res?res : MSVCRT_fputc(c, file);
2179 len = _write(file->_file, &cc, 1);
2180 if (len == 1) return c;
2181 file->_flag |= MSVCRT__IOERR;
2186 /*********************************************************************
2187 * _fputchar (MSVCRT.@)
2189 int _fputchar(int c)
2191 return MSVCRT_fputc(c, MSVCRT_stdout);
2194 /*********************************************************************
2197 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2198 { MSVCRT_size_t rcnt=size * nmemb;
2199 MSVCRT_size_t read=0;
2202 /* first buffered data */
2204 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2205 memcpy(ptr, file->_ptr, pcnt);
2210 ptr = (char*)ptr + pcnt;
2211 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2212 if(file->_flag & MSVCRT__IORW) {
2213 file->_flag |= MSVCRT__IOREAD;
2219 pread = _read(file->_file,ptr, rcnt);
2220 /* expose feof condition in the flags
2221 * MFC tests file->_flag for feof, and doesn't not call feof())
2224 file->_flag |= MSVCRT__IOEOF;
2225 else if (pread == -1)
2227 file->_flag |= MSVCRT__IOERR;
2235 /*********************************************************************
2236 * freopen (MSVCRT.@)
2239 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2241 int open_flags, stream_flags, fd;
2243 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2244 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2247 MSVCRT_fclose(file);
2249 /* map mode string to open() flags. "man fopen" for possibilities. */
2250 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2253 fd = _open(path, open_flags, _S_IREAD | _S_IWRITE);
2257 if (msvcrt_init_fp(file, fd, stream_flags) != -1)
2260 WARN(":failed-last error (%ld)\n",GetLastError());
2261 MSVCRT__set_errno(GetLastError());
2267 /*********************************************************************
2268 * fsetpos (MSVCRT.@)
2270 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2272 return _lseek(file->_file,*pos,SEEK_SET);
2275 /*********************************************************************
2278 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2283 if( file->_flag & MSVCRT__IOWRT ) {
2284 off = file->_ptr - file->_base;
2289 pos = _tell(file->_file);
2290 if(pos == -1) return pos;
2294 /*********************************************************************
2295 * fgetpos (MSVCRT.@)
2297 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2299 *pos = MSVCRT_ftell(file);
2300 return (*pos == -1? -1 : 0);
2303 /*********************************************************************
2306 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2308 size_t i, len = strlen(s);
2309 if (!(MSVCRT_fdesc[file->_file].xflag & WX_TEXT))
2310 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2311 for (i=0; i<len; i++)
2312 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2317 /*********************************************************************
2320 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2322 size_t i, len = strlenW(s);
2323 if (!(MSVCRT_fdesc[file->_file].xflag & WX_TEXT))
2324 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2325 for (i=0; i<len; i++)
2327 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2329 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2335 /*********************************************************************
2336 * getchar (MSVCRT.@)
2338 int MSVCRT_getchar(void)
2340 return MSVCRT_fgetc(MSVCRT_stdin);
2343 /*********************************************************************
2346 int MSVCRT_getc(MSVCRT_FILE* file)
2348 return MSVCRT_fgetc(file);
2351 /*********************************************************************
2354 char *MSVCRT_gets(char *buf)
2357 char * buf_start = buf;
2359 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2360 cc = MSVCRT_fgetc(MSVCRT_stdin))
2361 if(cc != '\r') *buf++ = (char)cc;
2365 TRACE("got '%s'\n", buf_start);
2369 /*********************************************************************
2372 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2375 MSVCRT_wchar_t* ws = buf;
2377 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2378 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2381 *buf++ = (MSVCRT_wchar_t)cc;
2385 TRACE("got '%s'\n", debugstr_w(ws));
2389 /*********************************************************************
2392 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2394 return MSVCRT_fputc(c, file);
2397 /*********************************************************************
2398 * putchar (MSVCRT.@)
2400 int MSVCRT_putchar(int c)
2402 return MSVCRT_fputc(c, MSVCRT_stdout);
2405 /*********************************************************************
2408 int MSVCRT_puts(const char *s)
2410 size_t len = strlen(s);
2411 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2412 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2415 /*********************************************************************
2418 int _putws(const MSVCRT_wchar_t *s)
2420 static const MSVCRT_wchar_t nl = '\n';
2421 size_t len = strlenW(s);
2422 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2423 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2426 /*********************************************************************
2429 int MSVCRT_remove(const char *path)
2431 TRACE("(%s)\n",path);
2432 if (DeleteFileA(path))
2434 TRACE(":failed (%ld)\n",GetLastError());
2435 MSVCRT__set_errno(GetLastError());
2439 /*********************************************************************
2440 * _wremove (MSVCRT.@)
2442 int _wremove(const MSVCRT_wchar_t *path)
2444 TRACE("(%s)\n",debugstr_w(path));
2445 if (DeleteFileW(path))
2447 TRACE(":failed (%ld)\n",GetLastError());
2448 MSVCRT__set_errno(GetLastError());
2452 /*********************************************************************
2455 int MSVCRT_rename(const char *oldpath,const char *newpath)
2457 TRACE(":from %s to %s\n",oldpath,newpath);
2458 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2460 TRACE(":failed (%ld)\n",GetLastError());
2461 MSVCRT__set_errno(GetLastError());
2465 /*********************************************************************
2466 * _wrename (MSVCRT.@)
2468 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2470 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2471 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2473 TRACE(":failed (%ld)\n",GetLastError());
2474 MSVCRT__set_errno(GetLastError());
2478 /*********************************************************************
2479 * setvbuf (MSVCRT.@)
2481 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2483 /* TODO: Check if file busy */
2485 MSVCRT_free(file->_base);
2489 if(mode == MSVCRT__IOFBF) {
2490 file->_flag &= ~MSVCRT__IONBF;
2491 file->_base = file->_ptr = buf;
2493 file->_bufsiz = size;
2496 file->_flag |= MSVCRT__IONBF;
2501 /*********************************************************************
2504 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2506 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2509 /*********************************************************************
2512 char *MSVCRT_tmpnam(char *s)
2520 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2521 p = s + sprintf(s, "\\s%s.", tmpstr);
2522 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2524 msvcrt_int_to_base32(unique++, tmpstr);
2526 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2527 GetLastError() == ERROR_FILE_NOT_FOUND)
2533 /*********************************************************************
2534 * tmpfile (MSVCRT.@)
2536 MSVCRT_FILE* MSVCRT_tmpfile(void)
2538 char *filename = MSVCRT_tmpnam(NULL);
2540 MSVCRT_FILE* file = NULL;
2542 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2543 if (fd != -1 && (file = msvcrt_alloc_fp()))
2545 if (msvcrt_init_fp(file, fd, _O_RDWR) == -1)
2550 else file->_tmpfname = _strdup(filename);
2555 /*********************************************************************
2556 * vfprintf (MSVCRT.@)
2558 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2560 char buf[2048], *mem = buf;
2561 int written, resize = sizeof(buf), retval;
2562 /* There are two conventions for vsnprintf failing:
2563 * Return -1 if we truncated, or
2564 * Return the number of bytes that would have been written
2565 * The code below handles both cases
2567 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2570 resize = (written == -1 ? resize * 2 : written + 1);
2573 if (!(mem = (char *)MSVCRT_malloc(resize)))
2576 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2582 /*********************************************************************
2583 * vfwprintf (MSVCRT.@)
2585 * Is final char included in written (then resize is too big) or not
2586 * (then we must test for equality too)?
2588 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2590 MSVCRT_wchar_t buf[2048], *mem = buf;
2591 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2592 /* See vfprintf comments */
2593 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2596 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2599 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2602 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2608 /*********************************************************************
2609 * vprintf (MSVCRT.@)
2611 int MSVCRT_vprintf(const char *format, va_list valist)
2613 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2616 /*********************************************************************
2617 * vwprintf (MSVCRT.@)
2619 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2621 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2624 /*********************************************************************
2625 * fprintf (MSVCRT.@)
2627 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2631 va_start(valist, format);
2632 res = MSVCRT_vfprintf(file, format, valist);
2637 /*********************************************************************
2638 * fwprintf (MSVCRT.@)
2640 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2644 va_start(valist, format);
2645 res = MSVCRT_vfwprintf(file, format, valist);
2650 /*********************************************************************
2653 int MSVCRT_printf(const char *format, ...)
2657 va_start(valist, format);
2658 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2663 /*********************************************************************
2666 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2668 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2669 msvcrt_alloc_buffer(file);
2672 if(file->_ptr>file->_base) {
2681 /*********************************************************************
2682 * ungetwc (MSVCRT.@)
2684 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2686 MSVCRT_wchar_t mwc = wc;
2687 char * pp = (char *)&mwc;
2689 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2690 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2696 /*********************************************************************
2697 * wprintf (MSVCRT.@)
2699 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2703 va_start(valist, format);
2704 res = MSVCRT_vwprintf(format, valist);