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
14 DEFAULT_DEBUG_CHANNEL(msvcrt);
16 /* stat() mode bits */
17 #define _S_IFMT 0xF000
18 #define _S_IFREG 0x8000
19 #define _S_IFDIR 0x4000
20 #define _S_IFCHR 0x2000
21 #define _S_IFIFO 0x1000
22 #define _S_IFBLK 0x3000
23 #define _S_IREAD 0x0100
24 #define _S_IWRITE 0x0080
25 #define _S_IEXEC 0x0040
27 /* for stat mode, permissions apply to all,owner and group */
28 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
29 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
30 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
33 #define _O_RDONLY 0x0000
34 #define _O_WRONLY 0x0001
35 #define _O_RDWR 0x0002
36 #define _O_APPEND 0x0008
37 #define _O_CREAT 0x0100
38 #define _O_TRUNC 0x0200
39 #define _O_EXCL 0x0400
40 #define _O_TEXT 0x4000
41 #define _O_BINARY 0x8000
42 #define _O_TEMPORARY 0x0040 /* Will be closed and deleted on exit */
44 /* _access() bit flags FIXME: incomplete */
47 typedef struct _crtfile
59 /* file._flag flags */
60 #define _IOREAD 0x0001
65 #define _IOAPPEND 0x0200
77 #define MSVCRT_stdin (&MSVCRT__iob[0])
78 #define MSVCRT_stdout (&MSVCRT__iob[1])
79 #define MSVCRT_stderr (&MSVCRT__iob[2])
83 unsigned short st_dev;
84 unsigned short st_ino;
85 unsigned short st_mode;
96 typedef long MSVCRT_fpos_t;
104 /* FIXME: Make this dynamic */
105 #define MSVCRT_MAX_FILES 257
107 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
108 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
109 int MSVCRT_flags[MSVCRT_MAX_FILES];
110 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
111 MSVCRT_FILE MSVCRT__iob[3];
113 static int MSVCRT_fdstart = 3; /* first unallocated fd */
114 static int MSVCRT_fdend = 3; /* highest allocated fd */
116 /* INTERNAL: process umask */
117 static int MSVCRT_umask = 0;
119 /* INTERNAL: Static buffer for temp file name */
120 static char MSVCRT_tmpname[MAX_PATH];
122 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
123 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
124 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
125 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
127 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
128 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
129 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
130 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
131 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
133 extern CRITICAL_SECTION MSVCRT_file_cs;
134 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
135 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
137 time_t __cdecl MSVCRT_time(time_t *);
138 int __cdecl MSVCRT__getdrive(void);
139 WCHAR *__cdecl MSVCRT__wcsdup(const WCHAR *);
140 unsigned int __cdecl wcslen(const WCHAR*);
141 int __cdecl iswalpha(WCHAR);
142 int __cdecl towupper(WCHAR);
143 int __cdecl towlower(WCHAR);
144 int __cdecl MSVCRT__vsnwprintf(WCHAR *,unsigned int,const WCHAR *,va_list);
146 /* INTERNAL: Get the HANDLE for a fd */
147 static HANDLE MSVCRT__fdtoh(int fd)
149 if (fd < 0 || fd >= MSVCRT_fdend ||
150 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
152 WARN(":fd (%d) - no handle!\n",fd);
153 SET_THREAD_VAR(doserrno,0);
154 SET_THREAD_VAR(errno,MSVCRT_EBADF);
155 return INVALID_HANDLE_VALUE;
157 return MSVCRT_handles[fd];
160 /* INTERNAL: free a file entry fd */
161 static void MSVCRT__free_fd(int fd)
163 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
164 MSVCRT_files[fd] = 0;
165 MSVCRT_flags[fd] = 0;
166 TRACE(":fd (%d) freed\n",fd);
168 return; /* dont use 0,1,2 for user files */
169 if (fd == MSVCRT_fdend - 1)
171 if (fd < MSVCRT_fdstart)
175 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
176 static int MSVCRT__alloc_fd(HANDLE hand, int flag)
178 int fd = MSVCRT_fdstart;
180 TRACE(":handle (%d) allocating fd (%d)\n",hand,fd);
181 if (fd >= MSVCRT_MAX_FILES)
183 WARN(":files exhausted!\n");
186 MSVCRT_handles[fd] = hand;
187 MSVCRT_flags[fd] = flag;
189 /* locate next free slot */
190 if (fd == MSVCRT_fdend)
191 MSVCRT_fdstart = ++MSVCRT_fdend;
193 while(MSVCRT_fdstart < MSVCRT_fdend &&
194 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
200 /* INTERNAL: Allocate a FILE* for an fd slot
201 * This is done lazily to avoid memory wastage for low level open/write
202 * usage when a FILE* is not requested (but may be later).
204 static MSVCRT_FILE* MSVCRT__alloc_fp(int fd)
206 TRACE(":fd (%d) allocating FILE*\n",fd);
207 if (fd < 0 || fd >= MSVCRT_fdend ||
208 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
210 WARN(":invalid fd %d\n",fd);
211 SET_THREAD_VAR(doserrno,0);
212 SET_THREAD_VAR(errno,MSVCRT_EBADF);
215 if (!MSVCRT_files[fd])
217 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
219 MSVCRT_files[fd]->_file = fd;
220 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
221 MSVCRT_files[fd]->_flag &= ~_IOAPPEND; /* mask out, see above */
224 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
225 return MSVCRT_files[fd];
229 /* INTERNAL: Set up stdin, stderr and stdout */
230 void MSVCRT_init_io(void)
233 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
234 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
235 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = _IOREAD;
236 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
237 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = _IOWRT;
238 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
239 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = _IOWRT;
241 TRACE(":handles (%d)(%d)(%d)\n",MSVCRT_handles[0],
242 MSVCRT_handles[1],MSVCRT_handles[2]);
244 for (i = 0; i < 3; i++)
246 /* FILE structs for stdin/out/err are static and never deleted */
247 MSVCRT_files[i] = &MSVCRT__iob[i];
248 MSVCRT__iob[i]._file = i;
249 MSVCRT_tempfiles[i] = NULL;
253 /*********************************************************************
256 MSVCRT_FILE *MSVCRT___p__iob(void)
258 return &MSVCRT__iob[0];
261 /*********************************************************************
264 int __cdecl MSVCRT__access(const char *filename, int mode)
266 DWORD attr = GetFileAttributesA(filename);
268 TRACE("(%s,%d) %ld\n",filename,mode,attr);
270 if (!filename || attr == 0xffffffff)
272 MSVCRT__set_errno(GetLastError());
275 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
277 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
283 /*********************************************************************
284 * _waccess (MSVCRT.@)
286 int __cdecl MSVCRT__waccess(const WCHAR *filename, int mode)
288 DWORD attr = GetFileAttributesW(filename);
290 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
292 if (!filename || attr == 0xffffffff)
294 MSVCRT__set_errno(GetLastError());
297 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
299 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
305 /*********************************************************************
308 int __cdecl MSVCRT__chmod(const char *path, int flags)
310 DWORD oldFlags = GetFileAttributesA(path);
312 if (oldFlags != 0x0FFFFFFFF)
314 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
315 oldFlags | FILE_ATTRIBUTE_READONLY;
317 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
320 MSVCRT__set_errno(GetLastError());
324 /*********************************************************************
327 int __cdecl MSVCRT__wchmod(const WCHAR *path, int flags)
329 DWORD oldFlags = GetFileAttributesW(path);
331 if (oldFlags != 0x0FFFFFFFF)
333 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
334 oldFlags | FILE_ATTRIBUTE_READONLY;
336 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
339 MSVCRT__set_errno(GetLastError());
343 /*********************************************************************
346 int __cdecl MSVCRT__unlink(const char *path)
348 TRACE("(%s)\n",path);
349 if(DeleteFileA(path))
351 TRACE("failed (%ld)\n",GetLastError());
352 MSVCRT__set_errno(GetLastError());
356 /*********************************************************************
357 * _wunlink (MSVCRT.@)
359 int __cdecl MSVCRT__wunlink(const WCHAR *path)
361 TRACE("(%s)\n",debugstr_w(path));
362 if(DeleteFileW(path))
364 TRACE("failed (%ld)\n",GetLastError());
365 MSVCRT__set_errno(GetLastError());
369 /*********************************************************************
372 int __cdecl MSVCRT__close(int fd)
374 HANDLE hand = MSVCRT__fdtoh(fd);
376 TRACE(":fd (%d) handle (%d)\n",fd,hand);
377 if (hand == INVALID_HANDLE_VALUE)
380 /* Dont free std FILE*'s, they are not dynamic */
381 if (fd > 2 && MSVCRT_files[fd])
382 MSVCRT_free(MSVCRT_files[fd]);
386 if (!CloseHandle(hand))
388 WARN(":failed-last error (%ld)\n",GetLastError());
389 MSVCRT__set_errno(GetLastError());
392 if (MSVCRT_tempfiles[fd])
394 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
395 MSVCRT__unlink(MSVCRT_tempfiles[fd]);
396 MSVCRT_free(MSVCRT_tempfiles[fd]);
397 MSVCRT_tempfiles[fd] = NULL;
404 /*********************************************************************
407 int __cdecl MSVCRT__commit(int fd)
409 HANDLE hand = MSVCRT__fdtoh(fd);
411 TRACE(":fd (%d) handle (%d)\n",fd,hand);
412 if (hand == INVALID_HANDLE_VALUE)
415 if (!FlushFileBuffers(hand))
417 if (GetLastError() == ERROR_INVALID_HANDLE)
419 /* FlushFileBuffers fails for console handles
420 * so we ignore this error.
424 TRACE(":failed-last error (%ld)\n",GetLastError());
425 MSVCRT__set_errno(GetLastError());
432 /*********************************************************************
435 int __cdecl MSVCRT__eof(int fd)
438 HANDLE hand = MSVCRT__fdtoh(fd);
440 TRACE(":fd (%d) handle (%d)\n",fd,hand);
442 if (hand == INVALID_HANDLE_VALUE)
445 /* If we have a FILE* for this file, the EOF flag
446 * will be set by the read()/write() functions.
448 if (MSVCRT_files[fd])
449 return MSVCRT_files[fd]->_flag & _IOEOF;
451 /* Otherwise we do it the hard way */
452 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
453 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
455 if (curpos == endpos)
458 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
462 /*********************************************************************
463 * _fcloseall (MSVCRT.@)
465 int __cdecl MSVCRT__fcloseall(void)
467 int num_closed = 0, i;
469 for (i = 3; i < MSVCRT_fdend; i++)
470 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
476 TRACE(":closed (%d) handles\n",num_closed);
480 /*********************************************************************
483 LONG __cdecl MSVCRT__lseek(int fd, LONG offset, int whence)
486 HANDLE hand = MSVCRT__fdtoh(fd);
488 TRACE(":fd (%d) handle (%d)\n",fd,hand);
489 if (hand == INVALID_HANDLE_VALUE)
492 if (whence < 0 || whence > 2)
494 SET_THREAD_VAR(errno,MSVCRT_EINVAL);
498 TRACE(":fd (%d) to 0x%08lx pos %s\n",
499 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
500 (whence==SEEK_CUR)?"SEEK_CUR":
501 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
503 if ((ret = SetFilePointer(hand, offset, NULL, whence)) != 0xffffffff)
505 if (MSVCRT_files[fd])
506 MSVCRT_files[fd]->_flag &= ~_IOEOF;
507 /* FIXME: What if we seek _to_ EOF - is EOF set? */
510 TRACE(":error-last error (%ld)\n",GetLastError());
511 if (MSVCRT_files[fd])
512 switch(GetLastError())
514 case ERROR_NEGATIVE_SEEK:
515 case ERROR_SEEK_ON_DEVICE:
516 MSVCRT__set_errno(GetLastError());
517 MSVCRT_files[fd]->_flag |= _IOERR;
525 /*********************************************************************
528 void __cdecl MSVCRT_rewind(MSVCRT_FILE* file)
530 TRACE(":file (%p) fd (%d)\n",file,file->_file);
531 MSVCRT__lseek(file->_file,0,SEEK_SET);
532 file->_flag &= ~(_IOEOF | _IOERR);
535 /*********************************************************************
538 MSVCRT_FILE* __cdecl MSVCRT__fdopen(int fd, const char *mode)
540 MSVCRT_FILE* file = MSVCRT__alloc_fp(fd);
542 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
549 /*********************************************************************
550 * _wfdopen (MSVCRT.@)
552 MSVCRT_FILE* __cdecl MSVCRT__wfdopen(int fd, const WCHAR *mode)
554 MSVCRT_FILE* file = MSVCRT__alloc_fp(fd);
556 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
563 /*********************************************************************
564 * _filelength (MSVCRT.@)
566 LONG __cdecl MSVCRT__filelength(int fd)
568 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
571 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
574 if (endPos != curPos)
575 MSVCRT__lseek(fd, curPos, SEEK_SET);
582 /*********************************************************************
585 int __cdecl MSVCRT__fileno(MSVCRT_FILE* file)
587 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
591 /*********************************************************************
592 * _flushall (MSVCRT.@)
594 int __cdecl MSVCRT__flushall(void)
596 int num_flushed = 0, i = 3;
598 while(i < MSVCRT_fdend)
599 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
601 if (MSVCRT__commit(i) == -1)
603 MSVCRT_files[i]->_flag |= _IOERR;
607 TRACE(":flushed (%d) handles\n",num_flushed);
611 /*********************************************************************
614 int __cdecl MSVCRT__fstat(int fd, struct _stat* buf)
617 BY_HANDLE_FILE_INFORMATION hfi;
618 HANDLE hand = MSVCRT__fdtoh(fd);
620 TRACE(":fd (%d) stat (%p)\n",fd,buf);
621 if (hand == INVALID_HANDLE_VALUE)
626 WARN(":failed-NULL buf\n");
627 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
631 memset(&hfi, 0, sizeof(hfi));
632 memset(buf, 0, sizeof(struct _stat));
633 if (!GetFileInformationByHandle(hand, &hfi))
635 WARN(":failed-last error (%ld)\n",GetLastError());
636 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
639 FIXME(":dwFileAttributes = %d, mode set to 0",hfi.dwFileAttributes);
640 buf->st_nlink = hfi.nNumberOfLinks;
641 buf->st_size = hfi.nFileSizeLow;
642 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
644 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
645 buf->st_mtime = buf->st_ctime = dw;
649 /*********************************************************************
652 int __cdecl MSVCRT__futime(int fd, struct _utimbuf *t)
654 HANDLE hand = MSVCRT__fdtoh(fd);
660 MSVCRT_time(&currTime);
661 RtlSecondsSince1970ToTime(currTime, &at);
662 memcpy(&wt, &at, sizeof(wt));
666 RtlSecondsSince1970ToTime(t->actime, &at);
667 if (t->actime == t->modtime)
668 memcpy(&wt, &at, sizeof(wt));
670 RtlSecondsSince1970ToTime(t->modtime, &wt);
673 if (!SetFileTime(hand, NULL, &at, &wt))
675 MSVCRT__set_errno(GetLastError());
681 /*********************************************************************
682 * _get_osfhandle (MSVCRT.@)
684 HANDLE MSVCRT__get_osfhandle(int fd)
686 HANDLE hand = MSVCRT__fdtoh(fd);
687 HANDLE newhand = hand;
688 TRACE(":fd (%d) handle (%d)\n",fd,hand);
690 if (hand != INVALID_HANDLE_VALUE)
692 /* FIXME: I'm not convinced that I should be copying the
693 * handle here - it may be leaked if the app doesn't
694 * close it (and the API docs dont say that it should)
695 * Not duplicating it means that it can't be inherited
696 * and so lcc's wedit doesn't cope when it passes it to
697 * child processes. I've an idea that it should either
698 * be copied by CreateProcess, or marked as inheritable
699 * when initialised, or maybe both? JG 21-9-00.
701 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
702 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
707 /*********************************************************************
710 int __cdecl MSVCRT__isatty(int fd)
712 HANDLE hand = MSVCRT__fdtoh(fd);
714 TRACE(":fd (%d) handle (%d)\n",fd,hand);
715 if (hand == INVALID_HANDLE_VALUE)
718 return GetFileType(fd) == FILE_TYPE_CHAR? 1 : 0;
721 /*********************************************************************
724 char *__cdecl MSVCRT__mktemp(char *pattern)
727 char *retVal = pattern;
732 numX = (*pattern++ == 'X')? numX + 1 : 0;
736 id = GetCurrentProcessId();
740 int tempNum = id / 10;
741 *pattern-- = id - (tempNum * 10) + '0';
747 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
748 GetLastError() == ERROR_FILE_NOT_FOUND)
751 } while(letter != '|');
755 /*********************************************************************
756 * _wmktemp (MSVCRT.@)
758 WCHAR *__cdecl MSVCRT__wmktemp(WCHAR *pattern)
761 WCHAR *retVal = pattern;
763 WCHAR letter = (WCHAR)L'a';
766 numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
770 id = GetCurrentProcessId();
774 int tempNum = id / 10;
775 *pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
781 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
782 GetLastError() == ERROR_FILE_NOT_FOUND)
785 } while(letter != (WCHAR)L'|');
789 /*********************************************************************
792 int __cdecl MSVCRT__open(const char *path,int flags)
794 DWORD access = 0, creation = 0;
798 TRACE(":file (%s) mode 0x%04x\n",path,flags);
800 switch(flags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
803 access |= GENERIC_READ;
807 access |= GENERIC_WRITE;
811 access |= GENERIC_WRITE | GENERIC_READ;
816 if (flags & _O_CREAT)
819 creation = CREATE_NEW;
820 else if (flags & _O_TRUNC)
821 creation = CREATE_ALWAYS;
823 creation = OPEN_ALWAYS;
825 else /* no _O_CREAT */
827 if (flags & _O_TRUNC)
828 creation = TRUNCATE_EXISTING;
830 creation = OPEN_EXISTING;
832 if (flags & _O_APPEND)
836 flags |= _O_BINARY; /* FIXME: Default to text */
840 /* Dont warn when writing */
841 if (ioflag & GENERIC_READ)
842 FIXME(":TEXT node not implemented\n");
846 if (flags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
847 |_O_CREAT|_O_RDWR|_O_TEMPORARY))
848 TRACE(":unsupported flags 0x%04x\n",flags);
850 hand = CreateFileA(path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
851 NULL, creation, FILE_ATTRIBUTE_NORMAL, 0);
853 if (hand == INVALID_HANDLE_VALUE)
855 WARN(":failed-last error (%ld)\n",GetLastError());
856 MSVCRT__set_errno(GetLastError());
860 fd = MSVCRT__alloc_fd(hand, ioflag);
862 TRACE(":fd (%d) handle (%d)\n",fd, hand);
866 if (flags & _O_TEMPORARY)
867 MSVCRT_tempfiles[fd] = MSVCRT__strdup(path);
868 if (ioflag & _IOAPPEND)
869 MSVCRT__lseek(fd, 0, FILE_END);
875 /*********************************************************************
878 int __cdecl MSVCRT__wopen(const WCHAR *path,int flags)
880 const unsigned int len = wcslen(path);
881 char *patha = MSVCRT_calloc(len + 1,1);
882 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
884 int retval = MSVCRT__open(patha,flags);
889 MSVCRT__set_errno(GetLastError());
893 /*********************************************************************
896 int __cdecl MSVCRT__creat(const char *path, int flags)
898 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
899 return MSVCRT__open(path, usedFlags);
902 /*********************************************************************
905 int __cdecl MSVCRT__wcreat(const WCHAR *path, int flags)
907 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
908 return MSVCRT__wopen(path, usedFlags);
911 /*********************************************************************
912 * _open_osfhandle (MSVCRT.@)
914 int __cdecl MSVCRT__open_osfhandle(HANDLE hand, int flags)
916 int fd = MSVCRT__alloc_fd(hand,flags);
917 TRACE(":handle (%d) fd (%d)\n",hand,fd);
921 /*********************************************************************
924 int __cdecl MSVCRT__rmtmp(void)
926 int num_removed = 0, i;
928 for (i = 3; i < MSVCRT_fdend; i++)
929 if (MSVCRT_tempfiles[i])
936 TRACE(":removed (%d) temp files\n",num_removed);
940 /*********************************************************************
943 int __cdecl MSVCRT__read(int fd, void *buf, unsigned int count)
946 HANDLE hand = MSVCRT__fdtoh(fd);
948 /* Dont trace small reads, it gets *very* annoying */
950 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
951 if (hand == INVALID_HANDLE_VALUE)
954 /* Set _cnt to 0 so optimised binaries will call our implementation
955 * of putc/getc. See _filbuf/_flsbuf comments.
957 if (MSVCRT_files[fd])
958 MSVCRT_files[fd]->_cnt = 0;
960 if (ReadFile(hand, buf, count, &num_read, NULL))
962 if (num_read != count && MSVCRT_files[fd])
965 MSVCRT_files[fd]->_flag |= _IOEOF;
969 TRACE(":failed-last error (%ld)\n",GetLastError());
970 if (MSVCRT_files[fd])
971 MSVCRT_files[fd]->_flag |= _IOERR;
975 /*********************************************************************
978 int __cdecl MSVCRT__getw(MSVCRT_FILE* file)
981 if (MSVCRT__read(file->_file, &i, sizeof(int)) != 1)
986 /*********************************************************************
987 * _setmode (MSVCRT.@)
989 int __cdecl MSVCRT__setmode(int fd,int mode)
992 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
996 /*********************************************************************
999 int __cdecl MSVCRT__stat(const char* path, struct _stat * buf)
1002 WIN32_FILE_ATTRIBUTE_DATA hfi;
1003 unsigned short mode = MSVCRT_S_IREAD;
1006 TRACE(":file (%s) buf(%p)\n",path,buf);
1008 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1010 TRACE("failed (%ld)\n",GetLastError());
1011 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1015 memset(buf,0,sizeof(struct _stat));
1017 /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
1019 buf->st_dev = buf->st_rdev = toupper(*path - 'A'); /* drive num */
1021 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
1023 plen = strlen(path);
1025 /* Dir, or regular file? */
1026 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1027 (path[plen-1] == '\\'))
1028 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1033 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1035 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1036 (tolower(path[plen-3]) << 16);
1037 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1038 mode |= MSVCRT_S_IEXEC;
1042 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1043 mode |= MSVCRT_S_IWRITE;
1045 buf->st_mode = mode;
1047 buf->st_size = hfi.nFileSizeLow;
1048 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1050 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1051 buf->st_mtime = buf->st_ctime = dw;
1052 TRACE("\n%d %d %d %d %d %d\n", buf->st_mode,buf->st_nlink,buf->st_size,
1053 buf->st_atime,buf->st_mtime, buf->st_ctime);
1057 /*********************************************************************
1060 int __cdecl MSVCRT__wstat(const WCHAR* path, struct _stat * buf)
1063 WIN32_FILE_ATTRIBUTE_DATA hfi;
1064 unsigned short mode = MSVCRT_S_IREAD;
1067 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1069 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1071 TRACE("failed (%ld)\n",GetLastError());
1072 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1076 memset(buf,0,sizeof(struct _stat));
1078 /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
1079 if (iswalpha(*path))
1080 buf->st_dev = buf->st_rdev = towupper(*path - (WCHAR)L'A'); /* drive num */
1082 buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
1084 plen = wcslen(path);
1086 /* Dir, or regular file? */
1087 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1088 (path[plen-1] == (WCHAR)L'\\'))
1089 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1094 if (plen > 6 && path[plen-4] == (WCHAR)L'.') /* shortest exe: "\x.exe" */
1096 ULONGLONG ext = towlower(path[plen-1]) | (towlower(path[plen-2]) << 16) |
1097 ((ULONGLONG)towlower(path[plen-3]) << 32);
1098 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1099 mode |= MSVCRT_S_IEXEC;
1103 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1104 mode |= MSVCRT_S_IWRITE;
1106 buf->st_mode = mode;
1108 buf->st_size = hfi.nFileSizeLow;
1109 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1111 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1112 buf->st_mtime = buf->st_ctime = dw;
1113 TRACE("\n%d %d %d %d %d %d\n", buf->st_mode,buf->st_nlink,buf->st_size,
1114 buf->st_atime,buf->st_mtime, buf->st_ctime);
1118 /*********************************************************************
1121 LONG __cdecl MSVCRT__tell(int fd)
1123 return MSVCRT__lseek(fd, 0, SEEK_CUR);
1126 /*********************************************************************
1127 * _tempnam (MSVCRT.@)
1129 char *__cdecl MSVCRT__tempnam(const char *dir, const char *prefix)
1131 char tmpbuf[MAX_PATH];
1133 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1134 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1136 TRACE("got name (%s)\n",tmpbuf);
1137 return MSVCRT__strdup(tmpbuf);
1139 TRACE("failed (%ld)\n",GetLastError());
1143 /*********************************************************************
1144 * _wtempnam (MSVCRT.@)
1146 WCHAR *__cdecl MSVCRT__wtempnam(const WCHAR *dir, const WCHAR *prefix)
1148 WCHAR tmpbuf[MAX_PATH];
1150 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1151 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1153 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1154 return MSVCRT__wcsdup(tmpbuf);
1156 TRACE("failed (%ld)\n",GetLastError());
1160 /*********************************************************************
1163 int __cdecl MSVCRT__umask(int umask)
1165 int old_umask = MSVCRT_umask;
1166 TRACE("(%d)\n",umask);
1167 MSVCRT_umask = umask;
1171 /*********************************************************************
1174 int __cdecl MSVCRT__utime(const char *path, struct _utimbuf *t)
1176 int fd = MSVCRT__open(path, _O_WRONLY | _O_BINARY);
1180 int retVal = MSVCRT__futime(fd, t);
1187 /*********************************************************************
1188 * _wutime (MSVCRT.@)
1190 int __cdecl MSVCRT__wutime(const WCHAR *path, struct _utimbuf *t)
1192 int fd = MSVCRT__wopen(path, _O_WRONLY | _O_BINARY);
1196 int retVal = MSVCRT__futime(fd, t);
1203 /*********************************************************************
1206 unsigned int __cdecl MSVCRT__write(int fd, const void *buf, unsigned int count)
1209 HANDLE hand = MSVCRT__fdtoh(fd);
1211 /* Dont trace small writes, it gets *very* annoying */
1213 // TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1214 if (hand == INVALID_HANDLE_VALUE)
1217 /* If appending, go to EOF */
1218 if (MSVCRT_flags[fd] & _IOAPPEND)
1219 MSVCRT__lseek(fd, 0, FILE_END);
1221 /* Set _cnt to 0 so optimised binaries will call our implementation
1224 if (MSVCRT_files[fd])
1225 MSVCRT_files[fd]->_cnt = 0;
1227 if (WriteFile(hand, buf, count, &num_written, NULL)
1228 && (num_written == count))
1231 TRACE(":failed-last error (%ld)\n",GetLastError());
1232 if (MSVCRT_files[fd])
1233 MSVCRT_files[fd]->_flag |= _IOERR;
1238 /*********************************************************************
1241 int __cdecl MSVCRT__putw(int val, MSVCRT_FILE* file)
1243 return MSVCRT__write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1246 /*********************************************************************
1247 * clearerr (MSVCRT.@)
1249 void __cdecl MSVCRT_clearerr(MSVCRT_FILE* file)
1251 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1252 file->_flag &= ~(_IOERR | _IOEOF);
1255 /*********************************************************************
1258 int __cdecl MSVCRT_fclose(MSVCRT_FILE* file)
1260 return MSVCRT__close(file->_file);
1263 /*********************************************************************
1266 int __cdecl MSVCRT_feof(MSVCRT_FILE* file)
1268 return file->_flag & _IOEOF;
1271 /*********************************************************************
1274 int __cdecl MSVCRT_ferror(MSVCRT_FILE* file)
1276 return file->_flag & _IOERR;
1279 /*********************************************************************
1282 int __cdecl MSVCRT_fflush(MSVCRT_FILE* file)
1284 return MSVCRT__commit(file->_file);
1287 /*********************************************************************
1290 int __cdecl MSVCRT_fgetc(MSVCRT_FILE* file)
1293 if (MSVCRT__read(file->_file,&c,1) != 1)
1298 /*********************************************************************
1299 * _fgetchar (MSVCRT.@)
1301 int __cdecl MSVCRT__fgetchar(void)
1303 return MSVCRT_fgetc(MSVCRT_stdin);
1306 /*********************************************************************
1307 * _filbuf (MSVCRT.@)
1309 int __cdecl MSVCRT__filbuf(MSVCRT_FILE* file)
1311 return MSVCRT_fgetc(file);
1314 /*********************************************************************
1315 * fgetpos (MSVCRT.@)
1317 int __cdecl MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1319 *pos = MSVCRT__tell(file->_file);
1320 return (*pos == -1? -1 : 0);
1323 /*********************************************************************
1326 char *__cdecl MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1329 char * buf_start = s;
1331 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1332 file,file->_file,s,size);
1334 /* BAD, for the whole WINE process blocks... just done this way to test
1335 * windows95's ftp.exe.
1336 * JG - Is this true now we use ReadFile() on stdin too?
1338 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1339 cc = MSVCRT_fgetc(file))
1342 if (--size <= 0) break;
1345 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1347 TRACE(":nothing read\n");
1354 TRACE(":got '%s'\n", buf_start);
1358 /*********************************************************************
1361 WCHAR __cdecl MSVCRT_fgetwc(MSVCRT_FILE* file)
1364 if (MSVCRT__read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1369 /*********************************************************************
1372 WCHAR __cdecl MSVCRT_getwc(MSVCRT_FILE* file)
1374 return MSVCRT_fgetwc(file);
1377 /*********************************************************************
1378 * _fgetwchar (MSVCRT.@)
1380 WCHAR __cdecl MSVCRT__fgetwchar(void)
1382 return MSVCRT_fgetwc(MSVCRT_stdin);
1385 /*********************************************************************
1386 * getwchar (MSVCRT.@)
1388 WCHAR __cdecl MSVCRT_getwchar(void)
1390 return MSVCRT__fgetwchar();
1393 /*********************************************************************
1396 WCHAR __cdecl MSVCRT_fputwc(WCHAR wc, MSVCRT_FILE* file)
1398 if (MSVCRT__write(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1403 /*********************************************************************
1404 * _fputwchar (MSVCRT.@)
1406 WCHAR __cdecl MSVCRT__fputwchar(WCHAR wc)
1408 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1411 /*********************************************************************
1414 MSVCRT_FILE* __cdecl MSVCRT_fopen(const char *path, const char *mode)
1417 int flags = 0, plus = 0, fd;
1418 const char* search = mode;
1420 TRACE("(%s,%s)\n",path,mode);
1423 if (*search++ == '+')
1426 /* map mode string to open() flags. "man fopen" for possibilities. */
1430 flags = (plus ? _O_RDWR : _O_RDONLY);
1433 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1436 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1451 flags &= ~_O_BINARY;
1456 FIXME(":unknown flag %c not supported\n",mode[-1]);
1459 fd = MSVCRT__open(path, flags);
1464 file = MSVCRT__alloc_fp(fd);
1465 TRACE(":got (%p)\n",file);
1472 /*********************************************************************
1473 * _wfopen (MSVCRT.@)
1475 MSVCRT_FILE *__cdecl MSVCRT__wfopen(const WCHAR *path, const WCHAR *mode)
1477 const unsigned int plen = wcslen(path), mlen = wcslen(mode);
1478 char *patha = MSVCRT_calloc(plen + 1, 1);
1479 char *modea = MSVCRT_calloc(mlen + 1, 1);
1481 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1483 if (patha && modea &&
1484 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1485 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1487 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1493 MSVCRT__set_errno(GetLastError());
1497 /*********************************************************************
1498 * _fsopen (MSVCRT.@)
1500 MSVCRT_FILE* __cdecl MSVCRT__fsopen(const char *path, const char *mode, int share)
1502 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1503 return MSVCRT_fopen(path,mode);
1506 /*********************************************************************
1507 * _wfsopen (MSVCRT.@)
1509 MSVCRT_FILE* __cdecl MSVCRT__wfsopen(const WCHAR *path, const WCHAR *mode, int share)
1511 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1512 debugstr_w(path),debugstr_w(mode),share);
1513 return MSVCRT__wfopen(path,mode);
1516 /*********************************************************************
1519 int __cdecl MSVCRT_fputc(int c, MSVCRT_FILE* file)
1521 return MSVCRT__write(file->_file, &c, 1) == 1? c : MSVCRT_EOF;
1524 /*********************************************************************
1525 * _flsbuf (MSVCRT.@)
1527 int __cdecl MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
1529 return MSVCRT_fputc(c,file);
1532 /*********************************************************************
1533 * _fputchar (MSVCRT.@)
1535 int __cdecl MSVCRT__fputchar(int c)
1537 return MSVCRT_fputc(c, MSVCRT_stdout);
1540 /*********************************************************************
1543 DWORD __cdecl MSVCRT_fread(void *ptr, int size, int nmemb, MSVCRT_FILE* file)
1545 DWORD read = MSVCRT__read(file->_file,ptr, size * nmemb);
1551 /*********************************************************************
1552 * freopen (MSVCRT.@)
1555 MSVCRT_FILE* __cdecl MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
1557 MSVCRT_FILE* newfile;
1560 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1561 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
1566 FIXME(":reopen on user file not implemented!\n");
1567 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1571 /* first, create the new file */
1572 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
1575 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1576 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1577 MSVCRT_handles[newfile->_file]))
1579 /* Redirecting std handle to file , copy over.. */
1580 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
1581 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
1582 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
1583 MSVCRT__iob[fd]._file = fd;
1584 /* And free up the resources allocated by fopen, but
1585 * not the HANDLE we copied. */
1586 MSVCRT_free(MSVCRT_files[fd]);
1587 MSVCRT__free_fd(newfile->_file);
1588 return &MSVCRT__iob[fd];
1591 WARN(":failed-last error (%ld)\n",GetLastError());
1592 MSVCRT_fclose(newfile);
1593 MSVCRT__set_errno(GetLastError());
1597 /*********************************************************************
1598 * fsetpos (MSVCRT.@)
1600 int __cdecl MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1602 return MSVCRT__lseek(file->_file,*pos,SEEK_SET);
1605 /*********************************************************************
1608 int __cdecl MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
1610 /* NOTE: If you extend this function, extend MSVCRT__cscanf in console.c too */
1614 if (!*format) return 0;
1615 WARN("%p (\"%s\"): semi-stub\n", file, format);
1616 nch = MSVCRT_fgetc(file);
1617 va_start(ap, format);
1619 if (*format == ' ') {
1620 /* skip whitespace */
1621 while ((nch!=MSVCRT_EOF) && isspace(nch))
1622 nch = MSVCRT_fgetc(file);
1624 else if (*format == '%') {
1628 case 'd': { /* read an integer */
1629 int*val = va_arg(ap, int*);
1631 /* skip initial whitespace */
1632 while ((nch!=MSVCRT_EOF) && isspace(nch))
1633 nch = MSVCRT_fgetc(file);
1634 /* get sign and first digit */
1636 nch = MSVCRT_fgetc(file);
1645 nch = MSVCRT_fgetc(file);
1646 /* read until no more digits */
1647 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
1648 cur = cur*10 + (nch - '0');
1649 nch = MSVCRT_fgetc(file);
1655 case 'f': { /* read a float */
1656 float*val = va_arg(ap, float*);
1658 /* skip initial whitespace */
1659 while ((nch!=MSVCRT_EOF) && isspace(nch))
1660 nch = MSVCRT_fgetc(file);
1661 /* get sign and first digit */
1663 nch = MSVCRT_fgetc(file);
1672 /* read until no more digits */
1673 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
1674 cur = cur*10 + (nch - '0');
1675 nch = MSVCRT_fgetc(file);
1678 /* handle decimals */
1680 nch = MSVCRT_fgetc(file);
1681 while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
1683 cur += dec * (nch - '0');
1684 nch = MSVCRT_fgetc(file);
1691 case 's': { /* read a word */
1692 char*str = va_arg(ap, char*);
1694 /* skip initial whitespace */
1695 while ((nch!=MSVCRT_EOF) && isspace(nch))
1696 nch = MSVCRT_fgetc(file);
1697 /* read until whitespace */
1698 while ((nch!=MSVCRT_EOF) && !isspace(nch)) {
1699 *sptr++ = nch; st++;
1700 nch = MSVCRT_fgetc(file);
1704 TRACE("read word: %s\n", str);
1707 default: FIXME("unhandled: %%%c\n", *format);
1713 /* check for character match */
1715 nch = MSVCRT_fgetc(file);
1721 if (nch!=MSVCRT_EOF) {
1722 WARN("need ungetch\n");
1724 TRACE("returning %d\n", rd);
1728 /*********************************************************************
1731 LONG __cdecl MSVCRT_fseek(MSVCRT_FILE* file, LONG offset, int whence)
1733 return MSVCRT__lseek(file->_file,offset,whence);
1736 /*********************************************************************
1739 LONG __cdecl MSVCRT_ftell(MSVCRT_FILE* file)
1741 return MSVCRT__tell(file->_file);
1744 /*********************************************************************
1747 unsigned int __cdecl MSVCRT_fwrite(const void *ptr, int size, int nmemb, MSVCRT_FILE* file)
1749 unsigned int written = MSVCRT__write(file->_file, ptr, size * nmemb);
1752 return written / size;
1755 /*********************************************************************
1758 int __cdecl MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
1760 return MSVCRT_fwrite(s,strlen(s),1,file) == 1 ? 0 : MSVCRT_EOF;
1763 /*********************************************************************
1766 int __cdecl MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
1768 return MSVCRT_fwrite(s,wcslen(s),1,file) == 1 ? 0 : MSVCRT_EOF;
1771 /*********************************************************************
1772 * getchar (MSVCRT.@)
1774 int __cdecl MSVCRT_getchar(void)
1776 return MSVCRT_fgetc(MSVCRT_stdin);
1779 /*********************************************************************
1782 int __cdecl MSVCRT_getc(MSVCRT_FILE* file)
1784 return MSVCRT_fgetc(file);
1787 /*********************************************************************
1790 char *__cdecl MSVCRT_gets(char *buf)
1793 char * buf_start = buf;
1795 /* BAD, for the whole WINE process blocks... just done this way to test
1796 * windows95's ftp.exe.
1797 * JG 19/9/00: Is this still true, now we are using ReadFile?
1799 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
1800 cc = MSVCRT_fgetc(MSVCRT_stdin))
1801 if(cc != '\r') *buf++ = (char)cc;
1805 TRACE("got '%s'\n", buf_start);
1809 /*********************************************************************
1812 int __cdecl MSVCRT_putc(int c, MSVCRT_FILE* file)
1814 return MSVCRT_fputc(c, file);
1817 /*********************************************************************
1818 * putchar (MSVCRT.@)
1820 void __cdecl MSVCRT_putchar(int c)
1822 MSVCRT_fputc(c, MSVCRT_stdout);
1825 /*********************************************************************
1828 int __cdecl MSVCRT_puts(const char *s)
1830 int retval = MSVCRT_EOF;
1831 if (MSVCRT_fwrite(s,strlen(s),1,MSVCRT_stdout) == 1)
1832 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
1836 /*********************************************************************
1839 int __cdecl MSVCRT__putws(const WCHAR *s)
1841 static const WCHAR nl = (WCHAR)L'\n';
1842 if (MSVCRT_fwrite(s,wcslen(s),1,MSVCRT_stdout) == 1)
1843 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
1847 /*********************************************************************
1850 int __cdecl MSVCRT_remove(const char *path)
1852 TRACE("(%s)\n",path);
1853 if (DeleteFileA(path))
1855 TRACE(":failed (%ld)\n",GetLastError());
1856 MSVCRT__set_errno(GetLastError());
1860 /*********************************************************************
1861 * _wremove (MSVCRT.@)
1863 int __cdecl MSVCRT__wremove(const WCHAR *path)
1865 TRACE("(%s)\n",debugstr_w(path));
1866 if (DeleteFileW(path))
1868 TRACE(":failed (%ld)\n",GetLastError());
1869 MSVCRT__set_errno(GetLastError());
1873 /*********************************************************************
1876 int __cdecl MSVCRT_scanf(const char *format, ...)
1881 va_start(valist, format);
1882 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
1887 /*********************************************************************
1890 int __cdecl MSVCRT_rename(const char *oldpath,const char *newpath)
1892 TRACE(":from %s to %s\n",oldpath,newpath);
1893 if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1895 TRACE(":failed (%ld)\n",GetLastError());
1896 MSVCRT__set_errno(GetLastError());
1900 /*********************************************************************
1901 * _wrename (MSVCRT.@)
1903 int __cdecl MSVCRT__wrename(const WCHAR *oldpath,const WCHAR *newpath)
1905 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
1906 if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1908 TRACE(":failed (%ld)\n",GetLastError());
1909 MSVCRT__set_errno(GetLastError());
1913 /*********************************************************************
1914 * setvbuf (MSVCRT.@)
1916 void MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, unsigned int size)
1918 FIXME("(%p,%p,%d,%d)stub\n",file, buf, mode, size);
1921 /*********************************************************************
1924 void __cdecl MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
1926 MSVCRT_setvbuf(file, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
1929 /*********************************************************************
1932 char *__cdecl MSVCRT_tmpnam(char *s)
1934 char tmpbuf[MAX_PATH];
1935 char* prefix = "TMP";
1936 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
1937 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
1939 TRACE(":failed-last error (%ld)\n",GetLastError());
1942 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
1947 /*********************************************************************
1948 * tmpfile (MSVCRT.@)
1950 MSVCRT_FILE* __cdecl MSVCRT_tmpfile(void)
1952 char *filename = MSVCRT_tmpnam(NULL);
1954 fd = MSVCRT__open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
1956 return MSVCRT__alloc_fp(fd);
1959 extern int vsnprintf(void *, unsigned int, const void*, va_list);
1961 /*********************************************************************
1962 * vfprintf (MSVCRT.@)
1964 int __cdecl MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
1966 char buf[2048], *mem = buf;
1967 int written, resize = sizeof(buf), retval;
1968 /* There are two conventions for vsnprintf failing:
1969 * Return -1 if we truncated, or
1970 * Return the number of bytes that would have been written
1971 * The code below handles both cases
1973 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
1976 resize = (written == -1 ? resize * 2 : written + 1);
1979 if (!(mem = (char *)MSVCRT_malloc(resize)))
1982 retval = MSVCRT_fwrite(mem, 1, written, file);
1988 /*********************************************************************
1989 * vfwprintf (MSVCRT.@)
1991 int __cdecl MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
1993 WCHAR buf[2048], *mem = buf;
1994 int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
1995 /* See vfprintf comments */
1996 while ((written = MSVCRT__vsnwprintf(mem, resize, format, valist)) == -1 ||
1999 resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
2002 if (!(mem = (WCHAR *)MSVCRT_malloc(resize)))
2005 retval = MSVCRT_fwrite(mem, 1, written * sizeof (WCHAR), file);
2011 /*********************************************************************
2012 * vprintf (MSVCRT.@)
2014 int __cdecl MSVCRT_vprintf(const char *format, va_list valist)
2016 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2019 /*********************************************************************
2020 * vwprintf (MSVCRT.@)
2022 int __cdecl MSVCRT_vwprintf(const WCHAR *format, va_list valist)
2024 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2027 /*********************************************************************
2028 * fprintf (MSVCRT.@)
2030 int __cdecl MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2034 va_start(valist, format);
2035 res = MSVCRT_vfprintf(file, format, valist);
2040 /*********************************************************************
2041 * fwprintf (MSVCRT.@)
2043 int __cdecl MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
2047 va_start(valist, format);
2048 res = MSVCRT_vfwprintf(file, format, valist);
2053 /*********************************************************************
2056 int __cdecl MSVCRT_printf(const char *format, ...)
2060 va_start(valist, format);
2061 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2066 /*********************************************************************
2067 * wprintf (MSVCRT.@)
2069 int __cdecl MSVCRT_wprintf(const WCHAR *format, ...)
2073 va_start(valist, format);
2074 res = MSVCRT_vwprintf(format, valist);