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 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
32 /* for stat mode, permissions apply to all,owner and group */
33 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
34 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
35 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
37 /* _access() bit flags FIXME: incomplete */
38 #define MSVCRT_W_OK 0x02
41 /* FIXME: Make this dynamic */
42 #define MSVCRT_MAX_FILES 257
44 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
45 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
46 int MSVCRT_flags[MSVCRT_MAX_FILES];
47 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
48 MSVCRT_FILE MSVCRT__iob[3];
49 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
50 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
51 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
53 static int MSVCRT_fdstart = 3; /* first unallocated fd */
54 static int MSVCRT_fdend = 3; /* highest allocated fd */
56 /* INTERNAL: process umask */
57 static int MSVCRT_umask = 0;
59 /* INTERNAL: Static buffer for temp file name */
60 static char MSVCRT_tmpname[MAX_PATH];
62 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
63 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
64 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
65 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
67 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
68 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
69 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
70 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
71 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
73 extern CRITICAL_SECTION MSVCRT_file_cs;
74 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
75 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
78 /* INTERNAL: Get the HANDLE for a fd */
79 static HANDLE msvcrt_fdtoh(int fd)
81 if (fd < 0 || fd >= MSVCRT_fdend ||
82 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
84 WARN(":fd (%d) - no handle!\n",fd);
85 SET_THREAD_VAR(doserrno,0);
86 SET_THREAD_VAR(errno,MSVCRT_EBADF);
87 return INVALID_HANDLE_VALUE;
89 return MSVCRT_handles[fd];
92 /* INTERNAL: free a file entry fd */
93 static void msvcrt_free_fd(int fd)
95 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
98 TRACE(":fd (%d) freed\n",fd);
100 return; /* dont use 0,1,2 for user files */
101 if (fd == MSVCRT_fdend - 1)
103 if (fd < MSVCRT_fdstart)
107 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
108 static int msvcrt_alloc_fd(HANDLE hand, int flag)
110 int fd = MSVCRT_fdstart;
112 TRACE(":handle (%d) allocating fd (%d)\n",hand,fd);
113 if (fd >= MSVCRT_MAX_FILES)
115 WARN(":files exhausted!\n");
118 MSVCRT_handles[fd] = hand;
119 MSVCRT_flags[fd] = flag;
121 /* locate next free slot */
122 if (fd == MSVCRT_fdend)
123 MSVCRT_fdstart = ++MSVCRT_fdend;
125 while(MSVCRT_fdstart < MSVCRT_fdend &&
126 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
132 /* INTERNAL: Allocate a FILE* for an fd slot
133 * This is done lazily to avoid memory wastage for low level open/write
134 * usage when a FILE* is not requested (but may be later).
136 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
138 TRACE(":fd (%d) allocating FILE*\n",fd);
139 if (fd < 0 || fd >= MSVCRT_fdend ||
140 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
142 WARN(":invalid fd %d\n",fd);
143 SET_THREAD_VAR(doserrno,0);
144 SET_THREAD_VAR(errno,MSVCRT_EBADF);
147 if (!MSVCRT_files[fd])
149 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
151 MSVCRT_files[fd]->_file = fd;
152 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
153 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
156 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
157 return MSVCRT_files[fd];
161 /* INTERNAL: Set up stdin, stderr and stdout */
162 void msvcrt_init_io(void)
165 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
166 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
167 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
168 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
169 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
170 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
171 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
173 TRACE(":handles (%d)(%d)(%d)\n",MSVCRT_handles[0],
174 MSVCRT_handles[1],MSVCRT_handles[2]);
176 for (i = 0; i < 3; i++)
178 /* FILE structs for stdin/out/err are static and never deleted */
179 MSVCRT_files[i] = &MSVCRT__iob[i];
180 MSVCRT__iob[i]._file = i;
181 MSVCRT_tempfiles[i] = NULL;
185 /*********************************************************************
188 MSVCRT_FILE *__p__iob(void)
190 return &MSVCRT__iob[0];
193 /*********************************************************************
196 int _access(const char *filename, int mode)
198 DWORD attr = GetFileAttributesA(filename);
200 TRACE("(%s,%d) %ld\n",filename,mode,attr);
202 if (!filename || attr == 0xffffffff)
204 MSVCRT__set_errno(GetLastError());
207 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
209 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
215 /*********************************************************************
216 * _waccess (MSVCRT.@)
218 int _waccess(const WCHAR *filename, int mode)
220 DWORD attr = GetFileAttributesW(filename);
222 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
224 if (!filename || attr == 0xffffffff)
226 MSVCRT__set_errno(GetLastError());
229 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
231 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
237 /*********************************************************************
240 int _chmod(const char *path, int flags)
242 DWORD oldFlags = GetFileAttributesA(path);
244 if (oldFlags != 0x0FFFFFFFF)
246 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
247 oldFlags | FILE_ATTRIBUTE_READONLY;
249 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
252 MSVCRT__set_errno(GetLastError());
256 /*********************************************************************
259 int _wchmod(const WCHAR *path, int flags)
261 DWORD oldFlags = GetFileAttributesW(path);
263 if (oldFlags != 0x0FFFFFFFF)
265 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
266 oldFlags | FILE_ATTRIBUTE_READONLY;
268 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
271 MSVCRT__set_errno(GetLastError());
275 /*********************************************************************
278 int _unlink(const char *path)
280 TRACE("(%s)\n",path);
281 if(DeleteFileA(path))
283 TRACE("failed (%ld)\n",GetLastError());
284 MSVCRT__set_errno(GetLastError());
288 /*********************************************************************
289 * _wunlink (MSVCRT.@)
291 int _wunlink(const WCHAR *path)
293 TRACE("(%s)\n",debugstr_w(path));
294 if(DeleteFileW(path))
296 TRACE("failed (%ld)\n",GetLastError());
297 MSVCRT__set_errno(GetLastError());
301 /*********************************************************************
306 HANDLE hand = msvcrt_fdtoh(fd);
308 TRACE(":fd (%d) handle (%d)\n",fd,hand);
309 if (hand == INVALID_HANDLE_VALUE)
312 /* Dont free std FILE*'s, they are not dynamic */
313 if (fd > 2 && MSVCRT_files[fd])
314 MSVCRT_free(MSVCRT_files[fd]);
318 if (!CloseHandle(hand))
320 WARN(":failed-last error (%ld)\n",GetLastError());
321 MSVCRT__set_errno(GetLastError());
324 if (MSVCRT_tempfiles[fd])
326 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
327 _unlink(MSVCRT_tempfiles[fd]);
328 MSVCRT_free(MSVCRT_tempfiles[fd]);
329 MSVCRT_tempfiles[fd] = NULL;
336 /*********************************************************************
341 HANDLE hand = msvcrt_fdtoh(fd);
343 TRACE(":fd (%d) handle (%d)\n",fd,hand);
344 if (hand == INVALID_HANDLE_VALUE)
347 if (!FlushFileBuffers(hand))
349 if (GetLastError() == ERROR_INVALID_HANDLE)
351 /* FlushFileBuffers fails for console handles
352 * so we ignore this error.
356 TRACE(":failed-last error (%ld)\n",GetLastError());
357 MSVCRT__set_errno(GetLastError());
364 /*********************************************************************
370 HANDLE hand = msvcrt_fdtoh(fd);
372 TRACE(":fd (%d) handle (%d)\n",fd,hand);
374 if (hand == INVALID_HANDLE_VALUE)
377 /* If we have a FILE* for this file, the EOF flag
378 * will be set by the read()/write() functions.
380 if (MSVCRT_files[fd])
381 return MSVCRT_files[fd]->_flag & MSVCRT__IOEOF;
383 /* Otherwise we do it the hard way */
384 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
385 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
387 if (curpos == endpos)
390 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
394 /*********************************************************************
395 * _fcloseall (MSVCRT.@)
399 int num_closed = 0, i;
401 for (i = 3; i < MSVCRT_fdend; i++)
402 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
408 TRACE(":closed (%d) handles\n",num_closed);
412 /*********************************************************************
415 LONG _lseek(int fd, LONG offset, int whence)
418 HANDLE hand = msvcrt_fdtoh(fd);
420 TRACE(":fd (%d) handle (%d)\n",fd,hand);
421 if (hand == INVALID_HANDLE_VALUE)
424 if (whence < 0 || whence > 2)
426 SET_THREAD_VAR(errno,MSVCRT_EINVAL);
430 TRACE(":fd (%d) to 0x%08lx pos %s\n",
431 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
432 (whence==SEEK_CUR)?"SEEK_CUR":
433 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
435 if ((ret = SetFilePointer(hand, offset, NULL, whence)) != 0xffffffff)
437 if (MSVCRT_files[fd])
438 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
439 /* FIXME: What if we seek _to_ EOF - is EOF set? */
442 TRACE(":error-last error (%ld)\n",GetLastError());
443 if (MSVCRT_files[fd])
444 switch(GetLastError())
446 case ERROR_NEGATIVE_SEEK:
447 case ERROR_SEEK_ON_DEVICE:
448 MSVCRT__set_errno(GetLastError());
449 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
457 /*********************************************************************
460 void MSVCRT_rewind(MSVCRT_FILE* file)
462 TRACE(":file (%p) fd (%d)\n",file,file->_file);
463 _lseek(file->_file,0,SEEK_SET);
464 file->_flag &= ~(MSVCRT__IOEOF | MSVCRT__IOERR);
467 /*********************************************************************
470 MSVCRT_FILE* _fdopen(int fd, const char *mode)
472 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
474 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
481 /*********************************************************************
482 * _wfdopen (MSVCRT.@)
484 MSVCRT_FILE* _wfdopen(int fd, const WCHAR *mode)
486 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
488 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
495 /*********************************************************************
496 * _filelength (MSVCRT.@)
498 LONG _filelength(int fd)
500 LONG curPos = _lseek(fd, 0, SEEK_CUR);
503 LONG endPos = _lseek(fd, 0, SEEK_END);
506 if (endPos != curPos)
507 _lseek(fd, curPos, SEEK_SET);
514 /*********************************************************************
517 int _fileno(MSVCRT_FILE* file)
519 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
523 /*********************************************************************
524 * _flushall (MSVCRT.@)
528 int num_flushed = 0, i = 3;
530 while(i < MSVCRT_fdend)
531 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
533 if (_commit(i) == -1)
535 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
539 TRACE(":flushed (%d) handles\n",num_flushed);
543 /*********************************************************************
546 int _fstat(int fd, struct _stat* buf)
549 BY_HANDLE_FILE_INFORMATION hfi;
550 HANDLE hand = msvcrt_fdtoh(fd);
552 TRACE(":fd (%d) stat (%p)\n",fd,buf);
553 if (hand == INVALID_HANDLE_VALUE)
558 WARN(":failed-NULL buf\n");
559 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
563 memset(&hfi, 0, sizeof(hfi));
564 memset(buf, 0, sizeof(struct _stat));
565 if (!GetFileInformationByHandle(hand, &hfi))
567 WARN(":failed-last error (%ld)\n",GetLastError());
568 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
571 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
572 buf->st_nlink = hfi.nNumberOfLinks;
573 buf->st_size = hfi.nFileSizeLow;
574 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
576 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
577 buf->st_mtime = buf->st_ctime = dw;
581 /*********************************************************************
584 int _futime(int fd, struct _utimbuf *t)
586 HANDLE hand = msvcrt_fdtoh(fd);
591 MSVCRT_time_t currTime;
592 MSVCRT_time(&currTime);
593 RtlSecondsSince1970ToTime(currTime, &at);
594 memcpy(&wt, &at, sizeof(wt));
598 RtlSecondsSince1970ToTime(t->actime, &at);
599 if (t->actime == t->modtime)
600 memcpy(&wt, &at, sizeof(wt));
602 RtlSecondsSince1970ToTime(t->modtime, &wt);
605 if (!SetFileTime(hand, NULL, &at, &wt))
607 MSVCRT__set_errno(GetLastError());
613 /*********************************************************************
614 * _get_osfhandle (MSVCRT.@)
616 long _get_osfhandle(int fd)
618 HANDLE hand = msvcrt_fdtoh(fd);
619 HANDLE newhand = hand;
620 TRACE(":fd (%d) handle (%d)\n",fd,hand);
622 if (hand != INVALID_HANDLE_VALUE)
624 /* FIXME: I'm not convinced that I should be copying the
625 * handle here - it may be leaked if the app doesn't
626 * close it (and the API docs dont say that it should)
627 * Not duplicating it means that it can't be inherited
628 * and so lcc's wedit doesn't cope when it passes it to
629 * child processes. I've an idea that it should either
630 * be copied by CreateProcess, or marked as inheritable
631 * when initialised, or maybe both? JG 21-9-00.
633 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
634 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
639 /*********************************************************************
644 HANDLE hand = msvcrt_fdtoh(fd);
646 TRACE(":fd (%d) handle (%d)\n",fd,hand);
647 if (hand == INVALID_HANDLE_VALUE)
650 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
653 /*********************************************************************
656 char *_mktemp(char *pattern)
659 char *retVal = pattern;
664 numX = (*pattern++ == 'X')? numX + 1 : 0;
668 id = GetCurrentProcessId();
672 int tempNum = id / 10;
673 *pattern-- = id - (tempNum * 10) + '0';
679 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
680 GetLastError() == ERROR_FILE_NOT_FOUND)
683 } while(letter != '|');
687 /*********************************************************************
688 * _wmktemp (MSVCRT.@)
690 WCHAR *_wmktemp(WCHAR *pattern)
693 WCHAR *retVal = pattern;
695 WCHAR letter = (WCHAR)L'a';
698 numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
702 id = GetCurrentProcessId();
706 int tempNum = id / 10;
707 *pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
713 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
714 GetLastError() == ERROR_FILE_NOT_FOUND)
717 } while(letter != (WCHAR)L'|');
721 /*********************************************************************
724 int _open(const char *path,int flags,...)
726 DWORD access = 0, creation = 0;
729 SECURITY_ATTRIBUTES sa;
731 TRACE(":file (%s) mode 0x%04x\n",path,flags);
733 switch(flags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
736 access |= GENERIC_READ;
737 ioflag |= MSVCRT__IOREAD;
740 access |= GENERIC_WRITE;
741 ioflag |= MSVCRT__IOWRT;
744 access |= GENERIC_WRITE | GENERIC_READ;
745 ioflag |= MSVCRT__IORW;
749 if (flags & _O_CREAT)
752 creation = CREATE_NEW;
753 else if (flags & _O_TRUNC)
754 creation = CREATE_ALWAYS;
756 creation = OPEN_ALWAYS;
758 else /* no _O_CREAT */
760 if (flags & _O_TRUNC)
761 creation = TRUNCATE_EXISTING;
763 creation = OPEN_EXISTING;
765 if (flags & _O_APPEND)
766 ioflag |= MSVCRT__IOAPPEND;
769 flags |= _O_BINARY; /* FIXME: Default to text */
773 /* Dont warn when writing */
774 if (ioflag & GENERIC_READ)
775 FIXME(":TEXT node not implemented\n");
779 if (flags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
780 |_O_CREAT|_O_RDWR|_O_TEMPORARY))
781 TRACE(":unsupported flags 0x%04x\n",flags);
783 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
784 sa.lpSecurityDescriptor = NULL;
785 sa.bInheritHandle = TRUE;
787 hand = CreateFileA(path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
788 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
790 if (hand == INVALID_HANDLE_VALUE)
792 WARN(":failed-last error (%ld)\n",GetLastError());
793 MSVCRT__set_errno(GetLastError());
797 fd = msvcrt_alloc_fd(hand, ioflag);
799 TRACE(":fd (%d) handle (%d)\n",fd, hand);
803 if (flags & _O_TEMPORARY)
804 MSVCRT_tempfiles[fd] = _strdup(path);
805 if (ioflag & MSVCRT__IOAPPEND)
806 _lseek(fd, 0, FILE_END);
812 /*********************************************************************
815 int _wopen(const WCHAR *path,int flags,...)
817 const unsigned int len = strlenW(path);
818 char *patha = MSVCRT_calloc(len + 1,1);
819 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
821 int retval = _open(patha,flags);
826 MSVCRT__set_errno(GetLastError());
830 /*********************************************************************
833 int MSVCRT__sopen(const char *path,int oflags,int shflags)
835 return _open(path, oflags | shflags);
838 /*********************************************************************
841 int _creat(const char *path, int flags)
843 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
844 return _open(path, usedFlags);
847 /*********************************************************************
850 int _wcreat(const WCHAR *path, int flags)
852 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
853 return _wopen(path, usedFlags);
856 /*********************************************************************
857 * _open_osfhandle (MSVCRT.@)
859 int _open_osfhandle(long hand, int flags)
861 int fd = msvcrt_alloc_fd(hand,flags);
862 TRACE(":handle (%ld) fd (%d)\n",hand,fd);
866 /*********************************************************************
871 int num_removed = 0, i;
873 for (i = 3; i < MSVCRT_fdend; i++)
874 if (MSVCRT_tempfiles[i])
881 TRACE(":removed (%d) temp files\n",num_removed);
885 /*********************************************************************
888 int _read(int fd, void *buf, unsigned int count)
891 HANDLE hand = msvcrt_fdtoh(fd);
893 /* Dont trace small reads, it gets *very* annoying */
895 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
896 if (hand == INVALID_HANDLE_VALUE)
899 /* Set _cnt to 0 so optimised binaries will call our implementation
900 * of putc/getc. See _filbuf/_flsbuf comments.
902 if (MSVCRT_files[fd])
903 MSVCRT_files[fd]->_cnt = 0;
905 if (ReadFile(hand, buf, count, &num_read, NULL))
907 if (num_read != count && MSVCRT_files[fd])
910 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
914 TRACE(":failed-last error (%ld)\n",GetLastError());
915 if (MSVCRT_files[fd])
916 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
920 /*********************************************************************
923 int _getw(MSVCRT_FILE* file)
926 if (_read(file->_file, &i, sizeof(int)) != 1)
931 /*********************************************************************
932 * _setmode (MSVCRT.@)
934 int _setmode(int fd,int mode)
937 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
941 /*********************************************************************
944 int _stat(const char* path, struct _stat * buf)
947 WIN32_FILE_ATTRIBUTE_DATA hfi;
948 unsigned short mode = MSVCRT_S_IREAD;
951 TRACE(":file (%s) buf(%p)\n",path,buf);
953 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
955 TRACE("failed (%ld)\n",GetLastError());
956 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
960 memset(buf,0,sizeof(struct _stat));
962 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
963 Bon 011120: This FIXME seems incorrect
964 Also a letter as first char isn't enough to be classify
967 if (isalpha(*path)&& (*(path+1)==':'))
968 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
970 buf->st_dev = buf->st_rdev = _getdrive() - 1;
974 /* Dir, or regular file? */
975 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
976 (path[plen-1] == '\\'))
977 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
982 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
984 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
985 (tolower(path[plen-3]) << 16);
986 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
987 mode |= MSVCRT_S_IEXEC;
991 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
992 mode |= MSVCRT_S_IWRITE;
996 buf->st_size = hfi.nFileSizeLow;
997 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
999 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1000 buf->st_mtime = buf->st_ctime = dw;
1001 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1002 buf->st_atime,buf->st_mtime, buf->st_ctime);
1006 /*********************************************************************
1009 int _wstat(const WCHAR* path, struct _stat * buf)
1012 WIN32_FILE_ATTRIBUTE_DATA hfi;
1013 unsigned short mode = MSVCRT_S_IREAD;
1016 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1018 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1020 TRACE("failed (%ld)\n",GetLastError());
1021 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1025 memset(buf,0,sizeof(struct _stat));
1027 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1028 if (MSVCRT_iswalpha(*path))
1029 buf->st_dev = buf->st_rdev = toupperW(*path - (WCHAR)L'A'); /* drive num */
1031 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1033 plen = strlenW(path);
1035 /* Dir, or regular file? */
1036 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1037 (path[plen-1] == (WCHAR)L'\\'))
1038 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1043 if (plen > 6 && path[plen-4] == (WCHAR)L'.') /* shortest exe: "\x.exe" */
1045 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1046 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1047 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1048 mode |= MSVCRT_S_IEXEC;
1052 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1053 mode |= MSVCRT_S_IWRITE;
1055 buf->st_mode = mode;
1057 buf->st_size = hfi.nFileSizeLow;
1058 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1060 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1061 buf->st_mtime = buf->st_ctime = dw;
1062 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1063 buf->st_atime,buf->st_mtime, buf->st_ctime);
1067 /*********************************************************************
1072 return _lseek(fd, 0, SEEK_CUR);
1075 /*********************************************************************
1076 * _tempnam (MSVCRT.@)
1078 char *_tempnam(const char *dir, const char *prefix)
1080 char tmpbuf[MAX_PATH];
1082 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1083 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1085 TRACE("got name (%s)\n",tmpbuf);
1086 return _strdup(tmpbuf);
1088 TRACE("failed (%ld)\n",GetLastError());
1092 /*********************************************************************
1093 * _wtempnam (MSVCRT.@)
1095 WCHAR *_wtempnam(const WCHAR *dir, const WCHAR *prefix)
1097 WCHAR tmpbuf[MAX_PATH];
1099 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1100 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1102 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1103 return _wcsdup(tmpbuf);
1105 TRACE("failed (%ld)\n",GetLastError());
1109 /*********************************************************************
1112 int _umask(int umask)
1114 int old_umask = MSVCRT_umask;
1115 TRACE("(%d)\n",umask);
1116 MSVCRT_umask = umask;
1120 /*********************************************************************
1123 int _utime(const char* path, struct _utimbuf *t)
1125 int fd = _open(path, _O_WRONLY | _O_BINARY);
1129 int retVal = _futime(fd, t);
1136 /*********************************************************************
1137 * _wutime (MSVCRT.@)
1139 int _wutime(const WCHAR* path, struct _utimbuf *t)
1141 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1145 int retVal = _futime(fd, t);
1152 /*********************************************************************
1155 int _write(int fd, const void* buf, unsigned int count)
1158 HANDLE hand = msvcrt_fdtoh(fd);
1160 /* Dont trace small writes, it gets *very* annoying */
1162 // TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1163 if (hand == INVALID_HANDLE_VALUE)
1166 /* If appending, go to EOF */
1167 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1168 _lseek(fd, 0, FILE_END);
1170 /* Set _cnt to 0 so optimised binaries will call our implementation
1173 if (MSVCRT_files[fd])
1174 MSVCRT_files[fd]->_cnt = 0;
1176 if (WriteFile(hand, buf, count, &num_written, NULL)
1177 && (num_written == count))
1180 TRACE(":failed-last error (%ld)\n",GetLastError());
1181 if (MSVCRT_files[fd])
1182 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1187 /*********************************************************************
1190 int _putw(int val, MSVCRT_FILE* file)
1192 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1195 /*********************************************************************
1196 * clearerr (MSVCRT.@)
1198 void MSVCRT_clearerr(MSVCRT_FILE* file)
1200 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1201 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1204 /*********************************************************************
1207 int MSVCRT_fclose(MSVCRT_FILE* file)
1210 r=_close(file->_file);
1211 return ((r==MSVCRT_EOF) || (file->_flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1214 /*********************************************************************
1217 int MSVCRT_feof(MSVCRT_FILE* file)
1219 return file->_flag & MSVCRT__IOEOF;
1222 /*********************************************************************
1225 int MSVCRT_ferror(MSVCRT_FILE* file)
1227 return file->_flag & MSVCRT__IOERR;
1230 /*********************************************************************
1233 int MSVCRT_fflush(MSVCRT_FILE* file)
1235 return _commit(file->_file);
1238 /*********************************************************************
1241 int MSVCRT_fgetc(MSVCRT_FILE* file)
1244 if (_read(file->_file,&c,1) != 1)
1249 /*********************************************************************
1250 * _fgetchar (MSVCRT.@)
1254 return MSVCRT_fgetc(MSVCRT_stdin);
1257 /*********************************************************************
1258 * _filbuf (MSVCRT.@)
1260 int _filbuf(MSVCRT_FILE* file)
1262 return MSVCRT_fgetc(file);
1265 /*********************************************************************
1266 * fgetpos (MSVCRT.@)
1268 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1270 *pos = _tell(file->_file);
1271 return (*pos == -1? -1 : 0);
1274 /*********************************************************************
1277 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1280 char * buf_start = s;
1282 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1283 file,file->_file,s,size);
1285 /* BAD, for the whole WINE process blocks... just done this way to test
1286 * windows95's ftp.exe.
1287 * JG - Is this true now we use ReadFile() on stdin too?
1289 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1290 cc = MSVCRT_fgetc(file))
1293 if (--size <= 0) break;
1296 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1298 TRACE(":nothing read\n");
1305 TRACE(":got '%s'\n", buf_start);
1309 /*********************************************************************
1312 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1315 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1320 /*********************************************************************
1323 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1325 return MSVCRT_fgetwc(file);
1328 /*********************************************************************
1329 * _fgetwchar (MSVCRT.@)
1331 MSVCRT_wint_t _fgetwchar(void)
1333 return MSVCRT_fgetwc(MSVCRT_stdin);
1336 /*********************************************************************
1337 * getwchar (MSVCRT.@)
1339 MSVCRT_wint_t MSVCRT_getwchar(void)
1341 return _fgetwchar();
1344 /*********************************************************************
1347 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1349 if (_write(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1354 /*********************************************************************
1355 * _fputwchar (MSVCRT.@)
1357 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1359 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1362 /*********************************************************************
1365 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1368 int flags = 0, plus = 0, fd;
1369 const char* search = mode;
1371 TRACE("(%s,%s)\n",path,mode);
1374 if (*search++ == '+')
1377 /* map mode string to open() flags. "man fopen" for possibilities. */
1381 flags = (plus ? _O_RDWR : _O_RDONLY);
1384 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1387 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1402 flags &= ~_O_BINARY;
1407 FIXME(":unknown flag %c not supported\n",mode[-1]);
1410 fd = _open(path, flags);
1415 file = msvcrt_alloc_fp(fd);
1416 TRACE(":got (%p)\n",file);
1423 /*********************************************************************
1424 * _wfopen (MSVCRT.@)
1426 MSVCRT_FILE *_wfopen(const WCHAR *path, const WCHAR *mode)
1428 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1429 char *patha = MSVCRT_calloc(plen + 1, 1);
1430 char *modea = MSVCRT_calloc(mlen + 1, 1);
1432 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1434 if (patha && modea &&
1435 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1436 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1438 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1444 MSVCRT__set_errno(GetLastError());
1448 /*********************************************************************
1449 * _fsopen (MSVCRT.@)
1451 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1453 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1454 return MSVCRT_fopen(path,mode);
1457 /*********************************************************************
1458 * _wfsopen (MSVCRT.@)
1460 MSVCRT_FILE* _wfsopen(const WCHAR *path, const WCHAR *mode, int share)
1462 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1463 debugstr_w(path),debugstr_w(mode),share);
1464 return _wfopen(path,mode);
1467 /*********************************************************************
1470 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1472 return _write(file->_file, &c, 1) == 1? c : MSVCRT_EOF;
1475 /*********************************************************************
1476 * _flsbuf (MSVCRT.@)
1478 int _flsbuf(int c, MSVCRT_FILE* file)
1480 return MSVCRT_fputc(c,file);
1483 /*********************************************************************
1484 * _fputchar (MSVCRT.@)
1486 int _fputchar(int c)
1488 return MSVCRT_fputc(c, MSVCRT_stdout);
1491 /*********************************************************************
1494 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1496 int read = _read(file->_file,ptr, size * nmemb);
1502 /*********************************************************************
1503 * freopen (MSVCRT.@)
1506 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
1508 MSVCRT_FILE* newfile;
1511 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1512 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
1517 FIXME(":reopen on user file not implemented!\n");
1518 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1522 /* first, create the new file */
1523 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
1526 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1527 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1528 MSVCRT_handles[newfile->_file]))
1530 /* Redirecting std handle to file , copy over.. */
1531 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
1532 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
1533 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
1534 MSVCRT__iob[fd]._file = fd;
1535 /* And free up the resources allocated by fopen, but
1536 * not the HANDLE we copied. */
1537 MSVCRT_free(MSVCRT_files[fd]);
1538 msvcrt_free_fd(newfile->_file);
1539 return &MSVCRT__iob[fd];
1542 WARN(":failed-last error (%ld)\n",GetLastError());
1543 MSVCRT_fclose(newfile);
1544 MSVCRT__set_errno(GetLastError());
1548 /*********************************************************************
1549 * fsetpos (MSVCRT.@)
1551 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1553 return _lseek(file->_file,*pos,SEEK_SET);
1556 /* helper function for fscanf. Returns the value of character c in the
1557 * given base, or -1 if the given character is not a digit of the base.
1559 static int char2digit(char c, int base) {
1560 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
1561 if (base<=10) return -1;
1562 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
1563 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
1567 /*********************************************************************
1569 * Implemented based on
1570 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
1571 * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
1572 * more types of format spec.
1574 int MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
1576 /* NOTE: If you extend this function, extend MSVCRT__cscanf in console.c too */
1580 if (!*format) return 0;
1581 WARN("%p (\"%s\"): semi-stub\n", file, format);
1582 nch = MSVCRT_fgetc(file);
1583 va_start(ap, format);
1585 /* a whitespace character in the format string causes scanf to read,
1586 * but not store, all consecutive white-space characters in the input
1587 * up to the next non-white-space character. One white space character
1588 * in the input matches any number (including zero) and combination of
1589 * white-space characters in the input. */
1590 if (isspace(*format)) {
1591 /* skip whitespace */
1592 while ((nch!=MSVCRT_EOF) && isspace(nch))
1593 nch = MSVCRT_fgetc(file);
1595 /* a format specification causes scanf to read and convert characters
1596 * in the input into values of a specified type. The value is assigned
1597 * to an argument in the argument list. Format specifications have
1598 * the form %[*][width][{h | l | I64 | L}]type */
1599 /* FIXME: unimplemented: h/l/I64/L modifiers and some type specs. */
1600 else if (*format == '%') {
1601 int st = 0; int suppress = 0; int width = 0;
1602 int base, number_signed;
1604 /* look for leading asterisk, which means 'suppress assignment of
1610 /* look for width specification */
1611 while (isdigit(*format)) {
1613 width+=*format++ - '0';
1615 if (width==0) width=-1; /* no width spec seen */
1617 case '%': /* read a percent symbol */
1618 if (nch!='%') break;
1619 nch = MSVCRT_fgetc(file);
1622 case 'X': /* hexadecimal integer. */
1623 base = 16; number_signed = 0;
1625 case 'o': /* octal integer */
1626 base = 8; number_signed = 0;
1628 case 'u': /* unsigned decimal integer */
1629 base = 10; number_signed = 0;
1631 case 'd': /* signed decimal integer */
1632 base = 10; number_signed = 1;
1634 case 'i': /* generic integer */
1635 base = 0; number_signed = 1;
1637 /* read an integer */
1638 int*val = suppress ? NULL : va_arg(ap, int*);
1639 int cur = 0; int negative = 0; int seendigit=0;
1640 /* skip initial whitespace */
1641 while ((nch!=MSVCRT_EOF) && isspace(nch))
1642 nch = MSVCRT_fgetc(file);
1644 if (number_signed && (nch == '-' || nch == '+')) {
1645 negative = (nch=='-');
1646 nch = MSVCRT_fgetc(file);
1647 if (width>0) width--;
1649 /* look for leading indication of base */
1650 if (width!=0 && nch == '0') {
1651 nch = MSVCRT_fgetc(file);
1652 if (width>0) width--;
1654 if (width!=0 && (nch=='x' || nch=='X')) {
1658 nch = MSVCRT_fgetc(file);
1659 if (width>0) width--;
1667 /* throw away leading zeros */
1668 while (width!=0 && nch=='0') {
1669 nch = MSVCRT_fgetc(file);
1670 if (width>0) width--;
1673 /* get first digit. Keep working copy negative, as the
1674 * range of negative numbers in two's complement notation
1675 * is one larger than the range of positive numbers. */
1676 if (width!=0 && char2digit(nch, base)!=-1) {
1677 cur = -char2digit(nch, base);
1678 nch = MSVCRT_fgetc(file);
1679 if (width>0) width--;
1682 /* read until no more digits */
1683 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
1684 cur = cur*base + char2digit(nch, base);
1685 nch = MSVCRT_fgetc(file);
1686 if (width>0) width--;
1689 /* negate parsed number if non-negative */
1690 if (!negative) cur=-cur;
1692 if (!seendigit) break; /* not a valid number */
1694 if (!suppress) *val = cur;
1701 case 'G': { /* read a float */
1702 float*val = suppress ? NULL : va_arg(ap, float*);
1705 /* skip initial whitespace */
1706 while ((nch!=MSVCRT_EOF) && isspace(nch))
1707 nch = MSVCRT_fgetc(file);
1709 if (nch == '-' || nch == '+') {
1710 negative = (nch=='-');
1711 if (width>0) width--;
1712 if (width==0) break;
1713 nch = MSVCRT_fgetc(file);
1715 /* get first digit. */
1716 if (!isdigit(nch)) break;
1717 cur = (nch - '0') * (negative ? -1 : 1);
1718 nch = MSVCRT_fgetc(file);
1719 if (width>0) width--;
1720 /* read until no more digits */
1721 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
1722 cur = cur*10 + (nch - '0');
1723 nch = MSVCRT_fgetc(file);
1724 if (width>0) width--;
1726 /* handle decimals */
1727 if (width!=0 && nch == '.') {
1729 nch = MSVCRT_fgetc(file);
1730 if (width>0) width--;
1731 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
1733 cur += dec * (nch - '0');
1734 nch = MSVCRT_fgetc(file);
1735 if (width>0) width--;
1738 /* handle exponent */
1739 if (width!=0 && (nch == 'e' || nch == 'E')) {
1740 int exponent = 0, negexp = 0;
1742 nch = MSVCRT_fgetc(file);
1743 if (width>0) width--;
1744 /* possible sign on the exponent */
1745 if (width!=0 && (nch=='+' || nch=='-')) {
1746 negexp = (nch=='-');
1747 nch = MSVCRT_fgetc(file);
1748 if (width>0) width--;
1750 /* exponent digits */
1751 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
1753 exponent += (nch - '0');
1754 nch = MSVCRT_fgetc(file);
1755 if (width>0) width--;
1757 /* update 'cur' with this exponent. */
1758 expcnt = negexp ? .1 : 10;
1759 while (exponent!=0) {
1763 expcnt=expcnt*expcnt;
1767 if (!suppress) *val = cur;
1770 case 's': { /* read a word */
1771 char*str = suppress ? NULL : va_arg(ap, char*);
1773 /* skip initial whitespace */
1774 while ((nch!=MSVCRT_EOF) && isspace(nch))
1775 nch = MSVCRT_fgetc(file);
1776 /* read until whitespace */
1777 while (width!=0 && (nch!=MSVCRT_EOF) && !isspace(nch)) {
1778 if (!suppress) *sptr++ = nch;
1780 nch = MSVCRT_fgetc(file);
1781 if (width>0) width--;
1784 if (!suppress) *sptr = 0;
1785 TRACE("read word: %s\n", str);
1788 default: FIXME("unhandled: %%%c\n", *format);
1789 /* From spec: "if a percent sign is followed by a character
1790 * that has no meaning as a format-control character, that
1791 * character and the following characters are treated as
1792 * an ordinary sequence of characters, that is, a sequence
1793 * of characters that must match the input. For example,
1794 * to specify that a percent-sign character is to be input,
1796 * LEAVING AS-IS because we catch bugs better that way. */
1798 if (st && !suppress) rd++;
1801 /* a non-white-space character causes scanf to read, but not store,
1802 * a matching non-white-space character. */
1804 /* check for character match */
1806 nch = MSVCRT_fgetc(file);
1811 if (nch!=MSVCRT_EOF) {
1812 FIXME("need ungetch\n");
1815 TRACE("returning %d\n", rd);
1819 /*********************************************************************
1822 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
1824 return _lseek(file->_file,offset,whence);
1827 /*********************************************************************
1830 LONG MSVCRT_ftell(MSVCRT_FILE* file)
1832 return _tell(file->_file);
1835 /*********************************************************************
1838 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1840 int written = _write(file->_file, ptr, size * nmemb);
1843 return written / size;
1846 /*********************************************************************
1849 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
1851 return MSVCRT_fwrite(s,strlen(s),1,file) == 1 ? 0 : MSVCRT_EOF;
1854 /*********************************************************************
1857 int MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
1859 return MSVCRT_fwrite(s,strlenW(s),1,file) == 1 ? 0 : MSVCRT_EOF;
1862 /*********************************************************************
1863 * getchar (MSVCRT.@)
1865 int MSVCRT_getchar(void)
1867 return MSVCRT_fgetc(MSVCRT_stdin);
1870 /*********************************************************************
1873 int MSVCRT_getc(MSVCRT_FILE* file)
1875 return MSVCRT_fgetc(file);
1878 /*********************************************************************
1881 char *MSVCRT_gets(char *buf)
1884 char * buf_start = buf;
1886 /* BAD, for the whole WINE process blocks... just done this way to test
1887 * windows95's ftp.exe.
1888 * JG 19/9/00: Is this still true, now we are using ReadFile?
1890 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
1891 cc = MSVCRT_fgetc(MSVCRT_stdin))
1892 if(cc != '\r') *buf++ = (char)cc;
1896 TRACE("got '%s'\n", buf_start);
1900 /*********************************************************************
1903 int MSVCRT_putc(int c, MSVCRT_FILE* file)
1905 return MSVCRT_fputc(c, file);
1908 /*********************************************************************
1909 * putchar (MSVCRT.@)
1911 int MSVCRT_putchar(int c)
1913 return MSVCRT_fputc(c, MSVCRT_stdout);
1916 /*********************************************************************
1919 int MSVCRT_puts(const char *s)
1921 int retval = MSVCRT_EOF;
1922 if (MSVCRT_fwrite(s,strlen(s),1,MSVCRT_stdout) == 1)
1923 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
1927 /*********************************************************************
1930 int _putws(const WCHAR *s)
1932 static const WCHAR nl = (WCHAR)L'\n';
1933 if (MSVCRT_fwrite(s,strlenW(s),1,MSVCRT_stdout) == 1)
1934 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
1938 /*********************************************************************
1941 int MSVCRT_remove(const char *path)
1943 TRACE("(%s)\n",path);
1944 if (DeleteFileA(path))
1946 TRACE(":failed (%ld)\n",GetLastError());
1947 MSVCRT__set_errno(GetLastError());
1951 /*********************************************************************
1952 * _wremove (MSVCRT.@)
1954 int _wremove(const WCHAR *path)
1956 TRACE("(%s)\n",debugstr_w(path));
1957 if (DeleteFileW(path))
1959 TRACE(":failed (%ld)\n",GetLastError());
1960 MSVCRT__set_errno(GetLastError());
1964 /*********************************************************************
1967 int MSVCRT_scanf(const char *format, ...)
1972 va_start(valist, format);
1973 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
1978 /*********************************************************************
1981 int MSVCRT_rename(const char *oldpath,const char *newpath)
1983 TRACE(":from %s to %s\n",oldpath,newpath);
1984 if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1986 TRACE(":failed (%ld)\n",GetLastError());
1987 MSVCRT__set_errno(GetLastError());
1991 /*********************************************************************
1992 * _wrename (MSVCRT.@)
1994 int _wrename(const WCHAR *oldpath,const WCHAR *newpath)
1996 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
1997 if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1999 TRACE(":failed (%ld)\n",GetLastError());
2000 MSVCRT__set_errno(GetLastError());
2004 /*********************************************************************
2005 * setvbuf (MSVCRT.@)
2007 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2009 FIXME("(%p,%p,%d,%d)stub\n",file, buf, mode, size);
2013 /*********************************************************************
2016 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2018 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, BUFSIZ);
2021 /*********************************************************************
2024 char *MSVCRT_tmpnam(char *s)
2026 char tmpbuf[MAX_PATH];
2027 char* prefix = "TMP";
2028 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
2029 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
2031 TRACE(":failed-last error (%ld)\n",GetLastError());
2034 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
2039 /*********************************************************************
2040 * tmpfile (MSVCRT.@)
2042 MSVCRT_FILE* MSVCRT_tmpfile(void)
2044 char *filename = MSVCRT_tmpnam(NULL);
2046 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2048 return msvcrt_alloc_fp(fd);
2052 /*********************************************************************
2053 * vfprintf (MSVCRT.@)
2055 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2057 char buf[2048], *mem = buf;
2058 int written, resize = sizeof(buf), retval;
2059 /* There are two conventions for vsnprintf failing:
2060 * Return -1 if we truncated, or
2061 * Return the number of bytes that would have been written
2062 * The code below handles both cases
2064 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2067 resize = (written == -1 ? resize * 2 : written + 1);
2070 if (!(mem = (char *)MSVCRT_malloc(resize)))
2073 retval = MSVCRT_fwrite(mem, 1, written, file);
2079 /*********************************************************************
2080 * vfwprintf (MSVCRT.@)
2082 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
2084 WCHAR buf[2048], *mem = buf;
2085 int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
2086 /* See vfprintf comments */
2087 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2090 resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
2093 if (!(mem = (WCHAR *)MSVCRT_malloc(resize)))
2096 retval = MSVCRT_fwrite(mem, 1, written * sizeof (WCHAR), file);
2102 /*********************************************************************
2103 * vprintf (MSVCRT.@)
2105 int MSVCRT_vprintf(const char *format, va_list valist)
2107 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2110 /*********************************************************************
2111 * vwprintf (MSVCRT.@)
2113 int MSVCRT_vwprintf(const WCHAR *format, va_list valist)
2115 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2118 /*********************************************************************
2119 * fprintf (MSVCRT.@)
2121 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2125 va_start(valist, format);
2126 res = MSVCRT_vfprintf(file, format, valist);
2131 /*********************************************************************
2132 * fwprintf (MSVCRT.@)
2134 int MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
2138 va_start(valist, format);
2139 res = MSVCRT_vfwprintf(file, format, valist);
2144 /*********************************************************************
2147 int MSVCRT_printf(const char *format, ...)
2151 va_start(valist, format);
2152 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2157 /*********************************************************************
2158 * wprintf (MSVCRT.@)
2160 int MSVCRT_wprintf(const WCHAR *format, ...)
2164 va_start(valist, format);
2165 res = MSVCRT_vwprintf(format, valist);