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
17 #include "wine/unicode.h"
18 #include "msvcrt/direct.h"
19 #include "msvcrt/fcntl.h"
20 #include "msvcrt/io.h"
21 #include "msvcrt/stdio.h"
22 #include "msvcrt/stdlib.h"
23 #include "msvcrt/string.h"
24 #include "msvcrt/sys/stat.h"
25 #include "msvcrt/sys/utime.h"
26 #include "msvcrt/time.h"
28 DEFAULT_DEBUG_CHANNEL(msvcrt);
30 /* for stat mode, permissions apply to all,owner and group */
31 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
32 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
33 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
35 /* _access() bit flags FIXME: incomplete */
39 /* FIXME: Make this dynamic */
40 #define MSVCRT_MAX_FILES 257
42 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
43 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
44 int MSVCRT_flags[MSVCRT_MAX_FILES];
45 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
46 MSVCRT_FILE MSVCRT__iob[3];
47 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
48 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
49 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
51 static int MSVCRT_fdstart = 3; /* first unallocated fd */
52 static int MSVCRT_fdend = 3; /* highest allocated fd */
54 /* INTERNAL: process umask */
55 static int MSVCRT_umask = 0;
57 /* INTERNAL: Static buffer for temp file name */
58 static char MSVCRT_tmpname[MAX_PATH];
60 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
61 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
62 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
63 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
65 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
66 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
67 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
68 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
69 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
71 extern CRITICAL_SECTION MSVCRT_file_cs;
72 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
73 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
76 /* INTERNAL: Get the HANDLE for a fd */
77 static HANDLE msvcrt_fdtoh(int fd)
79 if (fd < 0 || fd >= MSVCRT_fdend ||
80 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
82 WARN(":fd (%d) - no handle!\n",fd);
83 SET_THREAD_VAR(doserrno,0);
84 SET_THREAD_VAR(errno,MSVCRT_EBADF);
85 return INVALID_HANDLE_VALUE;
87 return MSVCRT_handles[fd];
90 /* INTERNAL: free a file entry fd */
91 static void msvcrt_free_fd(int fd)
93 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
96 TRACE(":fd (%d) freed\n",fd);
98 return; /* dont use 0,1,2 for user files */
99 if (fd == MSVCRT_fdend - 1)
101 if (fd < MSVCRT_fdstart)
105 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
106 static int msvcrt_alloc_fd(HANDLE hand, int flag)
108 int fd = MSVCRT_fdstart;
110 TRACE(":handle (%d) allocating fd (%d)\n",hand,fd);
111 if (fd >= MSVCRT_MAX_FILES)
113 WARN(":files exhausted!\n");
116 MSVCRT_handles[fd] = hand;
117 MSVCRT_flags[fd] = flag;
119 /* locate next free slot */
120 if (fd == MSVCRT_fdend)
121 MSVCRT_fdstart = ++MSVCRT_fdend;
123 while(MSVCRT_fdstart < MSVCRT_fdend &&
124 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
130 /* INTERNAL: Allocate a FILE* for an fd slot
131 * This is done lazily to avoid memory wastage for low level open/write
132 * usage when a FILE* is not requested (but may be later).
134 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
136 TRACE(":fd (%d) allocating FILE*\n",fd);
137 if (fd < 0 || fd >= MSVCRT_fdend ||
138 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
140 WARN(":invalid fd %d\n",fd);
141 SET_THREAD_VAR(doserrno,0);
142 SET_THREAD_VAR(errno,MSVCRT_EBADF);
145 if (!MSVCRT_files[fd])
147 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
149 MSVCRT_files[fd]->_file = fd;
150 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
151 MSVCRT_files[fd]->_flag &= ~_IOAPPEND; /* mask out, see above */
154 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
155 return MSVCRT_files[fd];
159 /* INTERNAL: Set up stdin, stderr and stdout */
160 void msvcrt_init_io(void)
163 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
164 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
165 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = _IOREAD;
166 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
167 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = _IOWRT;
168 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
169 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = _IOWRT;
171 TRACE(":handles (%d)(%d)(%d)\n",MSVCRT_handles[0],
172 MSVCRT_handles[1],MSVCRT_handles[2]);
174 for (i = 0; i < 3; i++)
176 /* FILE structs for stdin/out/err are static and never deleted */
177 MSVCRT_files[i] = &MSVCRT__iob[i];
178 MSVCRT__iob[i]._file = i;
179 MSVCRT_tempfiles[i] = NULL;
183 /*********************************************************************
186 MSVCRT_FILE *__p__iob(void)
188 return &MSVCRT__iob[0];
191 /*********************************************************************
194 int _access(const char *filename, int mode)
196 DWORD attr = GetFileAttributesA(filename);
198 TRACE("(%s,%d) %ld\n",filename,mode,attr);
200 if (!filename || attr == 0xffffffff)
202 MSVCRT__set_errno(GetLastError());
205 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
207 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
213 /*********************************************************************
214 * _waccess (MSVCRT.@)
216 int _waccess(const WCHAR *filename, int mode)
218 DWORD attr = GetFileAttributesW(filename);
220 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
222 if (!filename || attr == 0xffffffff)
224 MSVCRT__set_errno(GetLastError());
227 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
229 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
235 /*********************************************************************
238 int _chmod(const char *path, int flags)
240 DWORD oldFlags = GetFileAttributesA(path);
242 if (oldFlags != 0x0FFFFFFFF)
244 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
245 oldFlags | FILE_ATTRIBUTE_READONLY;
247 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
250 MSVCRT__set_errno(GetLastError());
254 /*********************************************************************
257 int _wchmod(const WCHAR *path, int flags)
259 DWORD oldFlags = GetFileAttributesW(path);
261 if (oldFlags != 0x0FFFFFFFF)
263 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
264 oldFlags | FILE_ATTRIBUTE_READONLY;
266 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
269 MSVCRT__set_errno(GetLastError());
273 /*********************************************************************
276 int _unlink(const char *path)
278 TRACE("(%s)\n",path);
279 if(DeleteFileA(path))
281 TRACE("failed (%ld)\n",GetLastError());
282 MSVCRT__set_errno(GetLastError());
286 /*********************************************************************
287 * _wunlink (MSVCRT.@)
289 int _wunlink(const WCHAR *path)
291 TRACE("(%s)\n",debugstr_w(path));
292 if(DeleteFileW(path))
294 TRACE("failed (%ld)\n",GetLastError());
295 MSVCRT__set_errno(GetLastError());
299 /*********************************************************************
304 HANDLE hand = msvcrt_fdtoh(fd);
306 TRACE(":fd (%d) handle (%d)\n",fd,hand);
307 if (hand == INVALID_HANDLE_VALUE)
310 /* Dont free std FILE*'s, they are not dynamic */
311 if (fd > 2 && MSVCRT_files[fd])
312 MSVCRT_free(MSVCRT_files[fd]);
316 if (!CloseHandle(hand))
318 WARN(":failed-last error (%ld)\n",GetLastError());
319 MSVCRT__set_errno(GetLastError());
322 if (MSVCRT_tempfiles[fd])
324 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
325 _unlink(MSVCRT_tempfiles[fd]);
326 MSVCRT_free(MSVCRT_tempfiles[fd]);
327 MSVCRT_tempfiles[fd] = NULL;
334 /*********************************************************************
339 HANDLE hand = msvcrt_fdtoh(fd);
341 TRACE(":fd (%d) handle (%d)\n",fd,hand);
342 if (hand == INVALID_HANDLE_VALUE)
345 if (!FlushFileBuffers(hand))
347 if (GetLastError() == ERROR_INVALID_HANDLE)
349 /* FlushFileBuffers fails for console handles
350 * so we ignore this error.
354 TRACE(":failed-last error (%ld)\n",GetLastError());
355 MSVCRT__set_errno(GetLastError());
362 /*********************************************************************
368 HANDLE hand = msvcrt_fdtoh(fd);
370 TRACE(":fd (%d) handle (%d)\n",fd,hand);
372 if (hand == INVALID_HANDLE_VALUE)
375 /* If we have a FILE* for this file, the EOF flag
376 * will be set by the read()/write() functions.
378 if (MSVCRT_files[fd])
379 return MSVCRT_files[fd]->_flag & _IOEOF;
381 /* Otherwise we do it the hard way */
382 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
383 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
385 if (curpos == endpos)
388 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
392 /*********************************************************************
393 * _fcloseall (MSVCRT.@)
397 int num_closed = 0, i;
399 for (i = 3; i < MSVCRT_fdend; i++)
400 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
406 TRACE(":closed (%d) handles\n",num_closed);
410 /*********************************************************************
413 LONG _lseek(int fd, LONG offset, int whence)
416 HANDLE hand = msvcrt_fdtoh(fd);
418 TRACE(":fd (%d) handle (%d)\n",fd,hand);
419 if (hand == INVALID_HANDLE_VALUE)
422 if (whence < 0 || whence > 2)
424 SET_THREAD_VAR(errno,MSVCRT_EINVAL);
428 TRACE(":fd (%d) to 0x%08lx pos %s\n",
429 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
430 (whence==SEEK_CUR)?"SEEK_CUR":
431 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
433 if ((ret = SetFilePointer(hand, offset, NULL, whence)) != 0xffffffff)
435 if (MSVCRT_files[fd])
436 MSVCRT_files[fd]->_flag &= ~_IOEOF;
437 /* FIXME: What if we seek _to_ EOF - is EOF set? */
440 TRACE(":error-last error (%ld)\n",GetLastError());
441 if (MSVCRT_files[fd])
442 switch(GetLastError())
444 case ERROR_NEGATIVE_SEEK:
445 case ERROR_SEEK_ON_DEVICE:
446 MSVCRT__set_errno(GetLastError());
447 MSVCRT_files[fd]->_flag |= _IOERR;
455 /*********************************************************************
458 void MSVCRT_rewind(MSVCRT_FILE* file)
460 TRACE(":file (%p) fd (%d)\n",file,file->_file);
461 _lseek(file->_file,0,SEEK_SET);
462 file->_flag &= ~(_IOEOF | _IOERR);
465 /*********************************************************************
468 MSVCRT_FILE* _fdopen(int fd, const char *mode)
470 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
472 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
479 /*********************************************************************
480 * _wfdopen (MSVCRT.@)
482 MSVCRT_FILE* _wfdopen(int fd, const WCHAR *mode)
484 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
486 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
493 /*********************************************************************
494 * _filelength (MSVCRT.@)
496 LONG _filelength(int fd)
498 LONG curPos = _lseek(fd, 0, SEEK_CUR);
501 LONG endPos = _lseek(fd, 0, SEEK_END);
504 if (endPos != curPos)
505 _lseek(fd, curPos, SEEK_SET);
512 /*********************************************************************
515 int _fileno(MSVCRT_FILE* file)
517 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
521 /*********************************************************************
522 * _flushall (MSVCRT.@)
526 int num_flushed = 0, i = 3;
528 while(i < MSVCRT_fdend)
529 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
531 if (_commit(i) == -1)
533 MSVCRT_files[i]->_flag |= _IOERR;
537 TRACE(":flushed (%d) handles\n",num_flushed);
541 /*********************************************************************
544 int _fstat(int fd, struct _stat* buf)
547 BY_HANDLE_FILE_INFORMATION hfi;
548 HANDLE hand = msvcrt_fdtoh(fd);
550 TRACE(":fd (%d) stat (%p)\n",fd,buf);
551 if (hand == INVALID_HANDLE_VALUE)
556 WARN(":failed-NULL buf\n");
557 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
561 memset(&hfi, 0, sizeof(hfi));
562 memset(buf, 0, sizeof(struct _stat));
563 if (!GetFileInformationByHandle(hand, &hfi))
565 WARN(":failed-last error (%ld)\n",GetLastError());
566 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
569 FIXME(":dwFileAttributes = %d, mode set to 0\n",hfi.dwFileAttributes);
570 buf->st_nlink = hfi.nNumberOfLinks;
571 buf->st_size = hfi.nFileSizeLow;
572 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
574 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
575 buf->st_mtime = buf->st_ctime = dw;
579 /*********************************************************************
582 int _futime(int fd, struct _utimbuf *t)
584 HANDLE hand = msvcrt_fdtoh(fd);
589 MSVCRT_time_t currTime;
590 MSVCRT_time(&currTime);
591 RtlSecondsSince1970ToTime(currTime, &at);
592 memcpy(&wt, &at, sizeof(wt));
596 RtlSecondsSince1970ToTime(t->actime, &at);
597 if (t->actime == t->modtime)
598 memcpy(&wt, &at, sizeof(wt));
600 RtlSecondsSince1970ToTime(t->modtime, &wt);
603 if (!SetFileTime(hand, NULL, &at, &wt))
605 MSVCRT__set_errno(GetLastError());
611 /*********************************************************************
612 * _get_osfhandle (MSVCRT.@)
614 long _get_osfhandle(int fd)
616 HANDLE hand = msvcrt_fdtoh(fd);
617 HANDLE newhand = hand;
618 TRACE(":fd (%d) handle (%d)\n",fd,hand);
620 if (hand != INVALID_HANDLE_VALUE)
622 /* FIXME: I'm not convinced that I should be copying the
623 * handle here - it may be leaked if the app doesn't
624 * close it (and the API docs dont say that it should)
625 * Not duplicating it means that it can't be inherited
626 * and so lcc's wedit doesn't cope when it passes it to
627 * child processes. I've an idea that it should either
628 * be copied by CreateProcess, or marked as inheritable
629 * when initialised, or maybe both? JG 21-9-00.
631 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
632 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
637 /*********************************************************************
642 HANDLE hand = msvcrt_fdtoh(fd);
644 TRACE(":fd (%d) handle (%d)\n",fd,hand);
645 if (hand == INVALID_HANDLE_VALUE)
648 return GetFileType(fd) == FILE_TYPE_CHAR? 1 : 0;
651 /*********************************************************************
654 char *_mktemp(char *pattern)
657 char *retVal = pattern;
662 numX = (*pattern++ == 'X')? numX + 1 : 0;
666 id = GetCurrentProcessId();
670 int tempNum = id / 10;
671 *pattern-- = id - (tempNum * 10) + '0';
677 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
678 GetLastError() == ERROR_FILE_NOT_FOUND)
681 } while(letter != '|');
685 /*********************************************************************
686 * _wmktemp (MSVCRT.@)
688 WCHAR *_wmktemp(WCHAR *pattern)
691 WCHAR *retVal = pattern;
693 WCHAR letter = (WCHAR)L'a';
696 numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
700 id = GetCurrentProcessId();
704 int tempNum = id / 10;
705 *pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
711 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
712 GetLastError() == ERROR_FILE_NOT_FOUND)
715 } while(letter != (WCHAR)L'|');
719 /*********************************************************************
722 int _open(const char *path,int flags,...)
724 DWORD access = 0, creation = 0;
728 TRACE(":file (%s) mode 0x%04x\n",path,flags);
730 switch(flags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
733 access |= GENERIC_READ;
737 access |= GENERIC_WRITE;
741 access |= GENERIC_WRITE | GENERIC_READ;
746 if (flags & _O_CREAT)
749 creation = CREATE_NEW;
750 else if (flags & _O_TRUNC)
751 creation = CREATE_ALWAYS;
753 creation = OPEN_ALWAYS;
755 else /* no _O_CREAT */
757 if (flags & _O_TRUNC)
758 creation = TRUNCATE_EXISTING;
760 creation = OPEN_EXISTING;
762 if (flags & _O_APPEND)
766 flags |= _O_BINARY; /* FIXME: Default to text */
770 /* Dont warn when writing */
771 if (ioflag & GENERIC_READ)
772 FIXME(":TEXT node not implemented\n");
776 if (flags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
777 |_O_CREAT|_O_RDWR|_O_TEMPORARY))
778 TRACE(":unsupported flags 0x%04x\n",flags);
780 hand = CreateFileA(path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
781 NULL, creation, FILE_ATTRIBUTE_NORMAL, 0);
783 if (hand == INVALID_HANDLE_VALUE)
785 WARN(":failed-last error (%ld)\n",GetLastError());
786 MSVCRT__set_errno(GetLastError());
790 fd = msvcrt_alloc_fd(hand, ioflag);
792 TRACE(":fd (%d) handle (%d)\n",fd, hand);
796 if (flags & _O_TEMPORARY)
797 MSVCRT_tempfiles[fd] = _strdup(path);
798 if (ioflag & _IOAPPEND)
799 _lseek(fd, 0, FILE_END);
805 /*********************************************************************
808 int _wopen(const WCHAR *path,int flags,...)
810 const unsigned int len = strlenW(path);
811 char *patha = MSVCRT_calloc(len + 1,1);
812 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
814 int retval = _open(patha,flags);
819 MSVCRT__set_errno(GetLastError());
823 /*********************************************************************
826 int MSVCRT__sopen(const char *path,int oflags,int shflags)
828 return _open(path, oflags | shflags);
831 /*********************************************************************
834 int _creat(const char *path, int flags)
836 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
837 return _open(path, usedFlags);
840 /*********************************************************************
843 int _wcreat(const WCHAR *path, int flags)
845 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
846 return _wopen(path, usedFlags);
849 /*********************************************************************
850 * _open_osfhandle (MSVCRT.@)
852 int _open_osfhandle(long hand, int flags)
854 int fd = msvcrt_alloc_fd(hand,flags);
855 TRACE(":handle (%ld) fd (%d)\n",hand,fd);
859 /*********************************************************************
864 int num_removed = 0, i;
866 for (i = 3; i < MSVCRT_fdend; i++)
867 if (MSVCRT_tempfiles[i])
874 TRACE(":removed (%d) temp files\n",num_removed);
878 /*********************************************************************
881 int _read(int fd, void *buf, unsigned int count)
884 HANDLE hand = msvcrt_fdtoh(fd);
886 /* Dont trace small reads, it gets *very* annoying */
888 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
889 if (hand == INVALID_HANDLE_VALUE)
892 /* Set _cnt to 0 so optimised binaries will call our implementation
893 * of putc/getc. See _filbuf/_flsbuf comments.
895 if (MSVCRT_files[fd])
896 MSVCRT_files[fd]->_cnt = 0;
898 if (ReadFile(hand, buf, count, &num_read, NULL))
900 if (num_read != count && MSVCRT_files[fd])
903 MSVCRT_files[fd]->_flag |= _IOEOF;
907 TRACE(":failed-last error (%ld)\n",GetLastError());
908 if (MSVCRT_files[fd])
909 MSVCRT_files[fd]->_flag |= _IOERR;
913 /*********************************************************************
916 int _getw(MSVCRT_FILE* file)
919 if (_read(file->_file, &i, sizeof(int)) != 1)
924 /*********************************************************************
925 * _setmode (MSVCRT.@)
927 int _setmode(int fd,int mode)
930 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
934 /*********************************************************************
937 int _stat(const char* path, struct _stat * buf)
940 WIN32_FILE_ATTRIBUTE_DATA hfi;
941 unsigned short mode = MSVCRT_S_IREAD;
944 TRACE(":file (%s) buf(%p)\n",path,buf);
946 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
948 TRACE("failed (%ld)\n",GetLastError());
949 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
953 memset(buf,0,sizeof(struct _stat));
955 /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
957 buf->st_dev = buf->st_rdev = toupper(*path - 'A'); /* drive num */
959 buf->st_dev = buf->st_rdev = _getdrive() - 1;
963 /* Dir, or regular file? */
964 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
965 (path[plen-1] == '\\'))
966 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
971 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
973 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
974 (tolower(path[plen-3]) << 16);
975 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
976 mode |= MSVCRT_S_IEXEC;
980 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
981 mode |= MSVCRT_S_IWRITE;
985 buf->st_size = hfi.nFileSizeLow;
986 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
988 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
989 buf->st_mtime = buf->st_ctime = dw;
990 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
991 buf->st_atime,buf->st_mtime, buf->st_ctime);
995 /*********************************************************************
998 int _wstat(const WCHAR* path, struct _stat * buf)
1001 WIN32_FILE_ATTRIBUTE_DATA hfi;
1002 unsigned short mode = MSVCRT_S_IREAD;
1005 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1007 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1009 TRACE("failed (%ld)\n",GetLastError());
1010 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1014 memset(buf,0,sizeof(struct _stat));
1016 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1017 if (MSVCRT_iswalpha(*path))
1018 buf->st_dev = buf->st_rdev = toupperW(*path - (WCHAR)L'A'); /* drive num */
1020 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1022 plen = strlenW(path);
1024 /* Dir, or regular file? */
1025 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1026 (path[plen-1] == (WCHAR)L'\\'))
1027 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1032 if (plen > 6 && path[plen-4] == (WCHAR)L'.') /* shortest exe: "\x.exe" */
1034 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1035 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1036 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1037 mode |= MSVCRT_S_IEXEC;
1041 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1042 mode |= MSVCRT_S_IWRITE;
1044 buf->st_mode = mode;
1046 buf->st_size = hfi.nFileSizeLow;
1047 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1049 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1050 buf->st_mtime = buf->st_ctime = dw;
1051 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1052 buf->st_atime,buf->st_mtime, buf->st_ctime);
1056 /*********************************************************************
1061 return _lseek(fd, 0, SEEK_CUR);
1064 /*********************************************************************
1065 * _tempnam (MSVCRT.@)
1067 char *_tempnam(const char *dir, const char *prefix)
1069 char tmpbuf[MAX_PATH];
1071 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1072 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1074 TRACE("got name (%s)\n",tmpbuf);
1075 return _strdup(tmpbuf);
1077 TRACE("failed (%ld)\n",GetLastError());
1081 /*********************************************************************
1082 * _wtempnam (MSVCRT.@)
1084 WCHAR *_wtempnam(const WCHAR *dir, const WCHAR *prefix)
1086 WCHAR tmpbuf[MAX_PATH];
1088 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1089 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1091 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1092 return _wcsdup(tmpbuf);
1094 TRACE("failed (%ld)\n",GetLastError());
1098 /*********************************************************************
1101 int _umask(int umask)
1103 int old_umask = MSVCRT_umask;
1104 TRACE("(%d)\n",umask);
1105 MSVCRT_umask = umask;
1109 /*********************************************************************
1112 int _utime(const char* path, struct _utimbuf *t)
1114 int fd = _open(path, _O_WRONLY | _O_BINARY);
1118 int retVal = _futime(fd, t);
1125 /*********************************************************************
1126 * _wutime (MSVCRT.@)
1128 int _wutime(const WCHAR* path, struct _utimbuf *t)
1130 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1134 int retVal = _futime(fd, t);
1141 /*********************************************************************
1144 int _write(int fd, const void* buf, unsigned int count)
1147 HANDLE hand = msvcrt_fdtoh(fd);
1149 /* Dont trace small writes, it gets *very* annoying */
1151 // TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1152 if (hand == INVALID_HANDLE_VALUE)
1155 /* If appending, go to EOF */
1156 if (MSVCRT_flags[fd] & _IOAPPEND)
1157 _lseek(fd, 0, FILE_END);
1159 /* Set _cnt to 0 so optimised binaries will call our implementation
1162 if (MSVCRT_files[fd])
1163 MSVCRT_files[fd]->_cnt = 0;
1165 if (WriteFile(hand, buf, count, &num_written, NULL)
1166 && (num_written == count))
1169 TRACE(":failed-last error (%ld)\n",GetLastError());
1170 if (MSVCRT_files[fd])
1171 MSVCRT_files[fd]->_flag |= _IOERR;
1176 /*********************************************************************
1179 int _putw(int val, MSVCRT_FILE* file)
1181 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1184 /*********************************************************************
1185 * clearerr (MSVCRT.@)
1187 void MSVCRT_clearerr(MSVCRT_FILE* file)
1189 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1190 file->_flag &= ~(_IOERR | _IOEOF);
1193 /*********************************************************************
1196 int MSVCRT_fclose(MSVCRT_FILE* file)
1198 return _close(file->_file);
1201 /*********************************************************************
1204 int MSVCRT_feof(MSVCRT_FILE* file)
1206 return file->_flag & _IOEOF;
1209 /*********************************************************************
1212 int MSVCRT_ferror(MSVCRT_FILE* file)
1214 return file->_flag & _IOERR;
1217 /*********************************************************************
1220 int MSVCRT_fflush(MSVCRT_FILE* file)
1222 return _commit(file->_file);
1225 /*********************************************************************
1228 int MSVCRT_fgetc(MSVCRT_FILE* file)
1231 if (_read(file->_file,&c,1) != 1)
1236 /*********************************************************************
1237 * _fgetchar (MSVCRT.@)
1241 return MSVCRT_fgetc(MSVCRT_stdin);
1244 /*********************************************************************
1245 * _filbuf (MSVCRT.@)
1247 int _filbuf(MSVCRT_FILE* file)
1249 return MSVCRT_fgetc(file);
1252 /*********************************************************************
1253 * fgetpos (MSVCRT.@)
1255 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1257 *pos = _tell(file->_file);
1258 return (*pos == -1? -1 : 0);
1261 /*********************************************************************
1264 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1267 char * buf_start = s;
1269 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1270 file,file->_file,s,size);
1272 /* BAD, for the whole WINE process blocks... just done this way to test
1273 * windows95's ftp.exe.
1274 * JG - Is this true now we use ReadFile() on stdin too?
1276 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1277 cc = MSVCRT_fgetc(file))
1280 if (--size <= 0) break;
1283 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1285 TRACE(":nothing read\n");
1292 TRACE(":got '%s'\n", buf_start);
1296 /*********************************************************************
1299 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1302 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1307 /*********************************************************************
1310 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1312 return MSVCRT_fgetwc(file);
1315 /*********************************************************************
1316 * _fgetwchar (MSVCRT.@)
1318 MSVCRT_wint_t _fgetwchar(void)
1320 return MSVCRT_fgetwc(MSVCRT_stdin);
1323 /*********************************************************************
1324 * getwchar (MSVCRT.@)
1326 MSVCRT_wint_t MSVCRT_getwchar(void)
1328 return _fgetwchar();
1331 /*********************************************************************
1334 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1336 if (_write(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1341 /*********************************************************************
1342 * _fputwchar (MSVCRT.@)
1344 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1346 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1349 /*********************************************************************
1352 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1355 int flags = 0, plus = 0, fd;
1356 const char* search = mode;
1358 TRACE("(%s,%s)\n",path,mode);
1361 if (*search++ == '+')
1364 /* map mode string to open() flags. "man fopen" for possibilities. */
1368 flags = (plus ? _O_RDWR : _O_RDONLY);
1371 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1374 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1389 flags &= ~_O_BINARY;
1394 FIXME(":unknown flag %c not supported\n",mode[-1]);
1397 fd = _open(path, flags);
1402 file = msvcrt_alloc_fp(fd);
1403 TRACE(":got (%p)\n",file);
1410 /*********************************************************************
1411 * _wfopen (MSVCRT.@)
1413 MSVCRT_FILE *_wfopen(const WCHAR *path, const WCHAR *mode)
1415 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1416 char *patha = MSVCRT_calloc(plen + 1, 1);
1417 char *modea = MSVCRT_calloc(mlen + 1, 1);
1419 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1421 if (patha && modea &&
1422 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1423 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1425 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1431 MSVCRT__set_errno(GetLastError());
1435 /*********************************************************************
1436 * _fsopen (MSVCRT.@)
1438 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1440 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1441 return MSVCRT_fopen(path,mode);
1444 /*********************************************************************
1445 * _wfsopen (MSVCRT.@)
1447 MSVCRT_FILE* _wfsopen(const WCHAR *path, const WCHAR *mode, int share)
1449 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1450 debugstr_w(path),debugstr_w(mode),share);
1451 return _wfopen(path,mode);
1454 /*********************************************************************
1457 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1459 return _write(file->_file, &c, 1) == 1? c : MSVCRT_EOF;
1462 /*********************************************************************
1463 * _flsbuf (MSVCRT.@)
1465 int _flsbuf(int c, MSVCRT_FILE* file)
1467 return MSVCRT_fputc(c,file);
1470 /*********************************************************************
1471 * _fputchar (MSVCRT.@)
1473 int _fputchar(int c)
1475 return MSVCRT_fputc(c, MSVCRT_stdout);
1478 /*********************************************************************
1481 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1483 int read = _read(file->_file,ptr, size * nmemb);
1489 /*********************************************************************
1490 * freopen (MSVCRT.@)
1493 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
1495 MSVCRT_FILE* newfile;
1498 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1499 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
1504 FIXME(":reopen on user file not implemented!\n");
1505 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1509 /* first, create the new file */
1510 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
1513 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1514 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1515 MSVCRT_handles[newfile->_file]))
1517 /* Redirecting std handle to file , copy over.. */
1518 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
1519 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
1520 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
1521 MSVCRT__iob[fd]._file = fd;
1522 /* And free up the resources allocated by fopen, but
1523 * not the HANDLE we copied. */
1524 MSVCRT_free(MSVCRT_files[fd]);
1525 msvcrt_free_fd(newfile->_file);
1526 return &MSVCRT__iob[fd];
1529 WARN(":failed-last error (%ld)\n",GetLastError());
1530 MSVCRT_fclose(newfile);
1531 MSVCRT__set_errno(GetLastError());
1535 /*********************************************************************
1536 * fsetpos (MSVCRT.@)
1538 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1540 return _lseek(file->_file,*pos,SEEK_SET);
1543 /*********************************************************************
1546 int MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
1548 /* NOTE: If you extend this function, extend MSVCRT__cscanf in console.c too */
1552 if (!*format) return 0;
1553 WARN("%p (\"%s\"): semi-stub\n", file, format);
1554 nch = MSVCRT_fgetc(file);
1555 va_start(ap, format);
1557 if (*format == ' ') {
1558 /* skip whitespace */
1559 while ((nch!=MSVCRT_EOF) && isspace(nch))
1560 nch = MSVCRT_fgetc(file);
1562 else if (*format == '%') {
1566 case 'd': { /* read an integer */
1567 int*val = va_arg(ap, int*);
1569 /* skip initial whitespace */
1570 while ((nch!=MSVCRT_EOF) && isspace(nch))
1571 nch = MSVCRT_fgetc(file);
1572 /* get sign and first digit */
1574 nch = MSVCRT_fgetc(file);
1583 nch = MSVCRT_fgetc(file);
1584 /* read until no more digits */
1585 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
1586 cur = cur*10 + (nch - '0');
1587 nch = MSVCRT_fgetc(file);
1593 case 'f': { /* read a float */
1594 float*val = va_arg(ap, float*);
1596 /* skip initial whitespace */
1597 while ((nch!=MSVCRT_EOF) && isspace(nch))
1598 nch = MSVCRT_fgetc(file);
1599 /* get sign and first digit */
1601 nch = MSVCRT_fgetc(file);
1610 /* read until no more digits */
1611 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
1612 cur = cur*10 + (nch - '0');
1613 nch = MSVCRT_fgetc(file);
1616 /* handle decimals */
1618 nch = MSVCRT_fgetc(file);
1619 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
1621 cur += dec * (nch - '0');
1622 nch = MSVCRT_fgetc(file);
1629 case 's': { /* read a word */
1630 char*str = va_arg(ap, char*);
1632 /* skip initial whitespace */
1633 while ((nch!=MSVCRT_EOF) && isspace(nch))
1634 nch = MSVCRT_fgetc(file);
1635 /* read until whitespace */
1636 while ((nch!=MSVCRT_EOF) && !isspace(nch)) {
1637 *sptr++ = nch; st++;
1638 nch = MSVCRT_fgetc(file);
1642 TRACE("read word: %s\n", str);
1645 default: FIXME("unhandled: %%%c\n", *format);
1651 /* check for character match */
1653 nch = MSVCRT_fgetc(file);
1659 if (nch!=MSVCRT_EOF) {
1660 WARN("need ungetch\n");
1662 TRACE("returning %d\n", rd);
1666 /*********************************************************************
1669 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
1671 return _lseek(file->_file,offset,whence);
1674 /*********************************************************************
1677 LONG MSVCRT_ftell(MSVCRT_FILE* file)
1679 return _tell(file->_file);
1682 /*********************************************************************
1685 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1687 int written = _write(file->_file, ptr, size * nmemb);
1690 return written / size;
1693 /*********************************************************************
1696 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
1698 return MSVCRT_fwrite(s,strlen(s),1,file) == 1 ? 0 : MSVCRT_EOF;
1701 /*********************************************************************
1704 int MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
1706 return MSVCRT_fwrite(s,strlenW(s),1,file) == 1 ? 0 : MSVCRT_EOF;
1709 /*********************************************************************
1710 * getchar (MSVCRT.@)
1712 int MSVCRT_getchar(void)
1714 return MSVCRT_fgetc(MSVCRT_stdin);
1717 /*********************************************************************
1720 int MSVCRT_getc(MSVCRT_FILE* file)
1722 return MSVCRT_fgetc(file);
1725 /*********************************************************************
1728 char *MSVCRT_gets(char *buf)
1731 char * buf_start = buf;
1733 /* BAD, for the whole WINE process blocks... just done this way to test
1734 * windows95's ftp.exe.
1735 * JG 19/9/00: Is this still true, now we are using ReadFile?
1737 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
1738 cc = MSVCRT_fgetc(MSVCRT_stdin))
1739 if(cc != '\r') *buf++ = (char)cc;
1743 TRACE("got '%s'\n", buf_start);
1747 /*********************************************************************
1750 int MSVCRT_putc(int c, MSVCRT_FILE* file)
1752 return MSVCRT_fputc(c, file);
1755 /*********************************************************************
1756 * putchar (MSVCRT.@)
1758 int MSVCRT_putchar(int c)
1760 return MSVCRT_fputc(c, MSVCRT_stdout);
1763 /*********************************************************************
1766 int MSVCRT_puts(const char *s)
1768 int retval = MSVCRT_EOF;
1769 if (MSVCRT_fwrite(s,strlen(s),1,MSVCRT_stdout) == 1)
1770 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
1774 /*********************************************************************
1777 int _putws(const WCHAR *s)
1779 static const WCHAR nl = (WCHAR)L'\n';
1780 if (MSVCRT_fwrite(s,strlenW(s),1,MSVCRT_stdout) == 1)
1781 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
1785 /*********************************************************************
1788 int MSVCRT_remove(const char *path)
1790 TRACE("(%s)\n",path);
1791 if (DeleteFileA(path))
1793 TRACE(":failed (%ld)\n",GetLastError());
1794 MSVCRT__set_errno(GetLastError());
1798 /*********************************************************************
1799 * _wremove (MSVCRT.@)
1801 int _wremove(const WCHAR *path)
1803 TRACE("(%s)\n",debugstr_w(path));
1804 if (DeleteFileW(path))
1806 TRACE(":failed (%ld)\n",GetLastError());
1807 MSVCRT__set_errno(GetLastError());
1811 /*********************************************************************
1814 int MSVCRT_scanf(const char *format, ...)
1819 va_start(valist, format);
1820 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
1825 /*********************************************************************
1828 int MSVCRT_rename(const char *oldpath,const char *newpath)
1830 TRACE(":from %s to %s\n",oldpath,newpath);
1831 if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1833 TRACE(":failed (%ld)\n",GetLastError());
1834 MSVCRT__set_errno(GetLastError());
1838 /*********************************************************************
1839 * _wrename (MSVCRT.@)
1841 int _wrename(const WCHAR *oldpath,const WCHAR *newpath)
1843 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
1844 if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1846 TRACE(":failed (%ld)\n",GetLastError());
1847 MSVCRT__set_errno(GetLastError());
1851 /*********************************************************************
1852 * setvbuf (MSVCRT.@)
1854 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
1856 FIXME("(%p,%p,%d,%d)stub\n",file, buf, mode, size);
1860 /*********************************************************************
1863 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
1865 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, BUFSIZ);
1868 /*********************************************************************
1871 char *MSVCRT_tmpnam(char *s)
1873 char tmpbuf[MAX_PATH];
1874 char* prefix = "TMP";
1875 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
1876 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
1878 TRACE(":failed-last error (%ld)\n",GetLastError());
1881 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
1886 /*********************************************************************
1887 * tmpfile (MSVCRT.@)
1889 MSVCRT_FILE* MSVCRT_tmpfile(void)
1891 char *filename = MSVCRT_tmpnam(NULL);
1893 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
1895 return msvcrt_alloc_fp(fd);
1899 /*********************************************************************
1900 * vfprintf (MSVCRT.@)
1902 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
1904 char buf[2048], *mem = buf;
1905 int written, resize = sizeof(buf), retval;
1906 /* There are two conventions for vsnprintf failing:
1907 * Return -1 if we truncated, or
1908 * Return the number of bytes that would have been written
1909 * The code below handles both cases
1911 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
1914 resize = (written == -1 ? resize * 2 : written + 1);
1917 if (!(mem = (char *)MSVCRT_malloc(resize)))
1920 retval = MSVCRT_fwrite(mem, 1, written, file);
1926 /*********************************************************************
1927 * vfwprintf (MSVCRT.@)
1929 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
1931 WCHAR buf[2048], *mem = buf;
1932 int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
1933 /* See vfprintf comments */
1934 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
1937 resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
1940 if (!(mem = (WCHAR *)MSVCRT_malloc(resize)))
1943 retval = MSVCRT_fwrite(mem, 1, written * sizeof (WCHAR), file);
1949 /*********************************************************************
1950 * vprintf (MSVCRT.@)
1952 int MSVCRT_vprintf(const char *format, va_list valist)
1954 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
1957 /*********************************************************************
1958 * vwprintf (MSVCRT.@)
1960 int MSVCRT_vwprintf(const WCHAR *format, va_list valist)
1962 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
1965 /*********************************************************************
1966 * fprintf (MSVCRT.@)
1968 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
1972 va_start(valist, format);
1973 res = MSVCRT_vfprintf(file, format, valist);
1978 /*********************************************************************
1979 * fwprintf (MSVCRT.@)
1981 int MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
1985 va_start(valist, format);
1986 res = MSVCRT_vfwprintf(file, format, valist);
1991 /*********************************************************************
1994 int MSVCRT_printf(const char *format, ...)
1998 va_start(valist, format);
1999 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2004 /*********************************************************************
2005 * wprintf (MSVCRT.@)
2007 int MSVCRT_wprintf(const WCHAR *format, ...)
2011 va_start(valist, format);
2012 res = MSVCRT_vwprintf(format, valist);