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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "wine/unicode.h"
32 #include "msvcrt/direct.h"
33 #include "msvcrt/fcntl.h"
34 #include "msvcrt/io.h"
35 #include "msvcrt/stdio.h"
36 #include "msvcrt/stdlib.h"
37 #include "msvcrt/string.h"
38 #include "msvcrt/sys/stat.h"
39 #include "msvcrt/sys/utime.h"
40 #include "msvcrt/time.h"
41 #include "msvcrt/share.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
47 /* for stat mode, permissions apply to all,owner and group */
48 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
49 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
50 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
52 /* _access() bit flags FIXME: incomplete */
53 #define MSVCRT_W_OK 0x02
56 /* FIXME: Make this dynamic */
57 #define MSVCRT_MAX_FILES 257
59 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
60 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
61 int MSVCRT_flags[MSVCRT_MAX_FILES];
62 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
63 MSVCRT_FILE MSVCRT__iob[3];
64 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
65 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
66 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
68 static int MSVCRT_fdstart = 3; /* first unallocated fd */
69 static int MSVCRT_fdend = 3; /* highest allocated fd */
71 /* INTERNAL: process umask */
72 static int MSVCRT_umask = 0;
74 /* INTERNAL: Static buffer for temp file name */
75 static char MSVCRT_tmpname[MAX_PATH];
77 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
78 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
79 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
80 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
82 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
83 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
84 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
85 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
86 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
88 extern CRITICAL_SECTION MSVCRT_file_cs;
89 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
90 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
93 /* INTERNAL: Get the HANDLE for a fd */
94 static HANDLE msvcrt_fdtoh(int fd)
96 if (fd < 0 || fd >= MSVCRT_fdend ||
97 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
99 WARN(":fd (%d) - no handle!\n",fd);
100 SET_THREAD_VAR(doserrno,0);
101 SET_THREAD_VAR(errno,MSVCRT_EBADF);
102 return INVALID_HANDLE_VALUE;
104 return MSVCRT_handles[fd];
107 /* INTERNAL: free a file entry fd */
108 static void msvcrt_free_fd(int fd)
110 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
111 MSVCRT_files[fd] = 0;
112 MSVCRT_flags[fd] = 0;
113 TRACE(":fd (%d) freed\n",fd);
115 return; /* dont use 0,1,2 for user files */
116 if (fd == MSVCRT_fdend - 1)
118 if (fd < MSVCRT_fdstart)
122 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
123 static int msvcrt_alloc_fd(HANDLE hand, int flag)
125 int fd = MSVCRT_fdstart;
127 TRACE(":handle (%d) allocating fd (%d)\n",hand,fd);
128 if (fd >= MSVCRT_MAX_FILES)
130 WARN(":files exhausted!\n");
133 MSVCRT_handles[fd] = hand;
134 MSVCRT_flags[fd] = flag;
136 /* locate next free slot */
137 if (fd == MSVCRT_fdend)
138 MSVCRT_fdstart = ++MSVCRT_fdend;
140 while(MSVCRT_fdstart < MSVCRT_fdend &&
141 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
147 /* INTERNAL: Allocate a FILE* for an fd slot
148 * This is done lazily to avoid memory wastage for low level open/write
149 * usage when a FILE* is not requested (but may be later).
151 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
153 TRACE(":fd (%d) allocating FILE*\n",fd);
154 if (fd < 0 || fd >= MSVCRT_fdend ||
155 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
157 WARN(":invalid fd %d\n",fd);
158 SET_THREAD_VAR(doserrno,0);
159 SET_THREAD_VAR(errno,MSVCRT_EBADF);
162 if (!MSVCRT_files[fd])
164 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
166 MSVCRT_files[fd]->_file = fd;
167 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
168 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
171 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
172 return MSVCRT_files[fd];
176 /* INTERNAL: Set up stdin, stderr and stdout */
177 void msvcrt_init_io(void)
180 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
181 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
182 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
183 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
184 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
185 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
186 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
188 TRACE(":handles (%d)(%d)(%d)\n",MSVCRT_handles[0],
189 MSVCRT_handles[1],MSVCRT_handles[2]);
191 for (i = 0; i < 3; i++)
193 /* FILE structs for stdin/out/err are static and never deleted */
194 MSVCRT_files[i] = &MSVCRT__iob[i];
195 MSVCRT__iob[i]._file = i;
196 MSVCRT_tempfiles[i] = NULL;
200 /* INTERNAL: Flush stdio file buffer */
201 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
204 int cnt=file->_ptr-file->_base;
205 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
208 file->_ptr=file->_base;
209 file->_cnt=file->_bufsiz;
214 /* INTERNAL: Allocate stdio file buffer */
215 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
217 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
219 file->_bufsiz = MSVCRT_BUFSIZ;
220 file->_flag |= MSVCRT__IOMYBUF;
222 file->_base = (unsigned char *)(&file->_charbuf);
224 file->_bufsiz = sizeof(file->_charbuf);
226 file->_ptr = file->_base;
230 /*********************************************************************
233 MSVCRT_FILE *__p__iob(void)
235 return &MSVCRT__iob[0];
238 /*********************************************************************
241 int _access(const char *filename, int mode)
243 DWORD attr = GetFileAttributesA(filename);
245 TRACE("(%s,%d) %ld\n",filename,mode,attr);
247 if (!filename || attr == 0xffffffff)
249 MSVCRT__set_errno(GetLastError());
252 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
254 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
260 /*********************************************************************
261 * _waccess (MSVCRT.@)
263 int _waccess(const WCHAR *filename, int mode)
265 DWORD attr = GetFileAttributesW(filename);
267 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
269 if (!filename || attr == 0xffffffff)
271 MSVCRT__set_errno(GetLastError());
274 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
276 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
282 /*********************************************************************
285 int _chmod(const char *path, int flags)
287 DWORD oldFlags = GetFileAttributesA(path);
289 if (oldFlags != 0x0FFFFFFFF)
291 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
292 oldFlags | FILE_ATTRIBUTE_READONLY;
294 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
297 MSVCRT__set_errno(GetLastError());
301 /*********************************************************************
304 int _wchmod(const WCHAR *path, int flags)
306 DWORD oldFlags = GetFileAttributesW(path);
308 if (oldFlags != 0x0FFFFFFFF)
310 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
311 oldFlags | FILE_ATTRIBUTE_READONLY;
313 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
316 MSVCRT__set_errno(GetLastError());
320 /*********************************************************************
323 int _unlink(const char *path)
325 TRACE("(%s)\n",path);
326 if(DeleteFileA(path))
328 TRACE("failed (%ld)\n",GetLastError());
329 MSVCRT__set_errno(GetLastError());
333 /*********************************************************************
334 * _wunlink (MSVCRT.@)
336 int _wunlink(const WCHAR *path)
338 TRACE("(%s)\n",debugstr_w(path));
339 if(DeleteFileW(path))
341 TRACE("failed (%ld)\n",GetLastError());
342 MSVCRT__set_errno(GetLastError());
346 /*********************************************************************
351 HANDLE hand = msvcrt_fdtoh(fd);
353 TRACE(":fd (%d) handle (%d)\n",fd,hand);
354 if (hand == INVALID_HANDLE_VALUE)
356 /* flush stdio buffers */
357 if(MSVCRT_files[fd]) {
358 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
359 MSVCRT_fflush(MSVCRT_files[fd]);
361 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
362 MSVCRT_free(MSVCRT_files[fd]->_base);
365 /* Dont free std FILE*'s, they are not dynamic */
366 if (fd > 2 && MSVCRT_files[fd])
367 MSVCRT_free(MSVCRT_files[fd]);
371 if (!CloseHandle(hand))
373 WARN(":failed-last error (%ld)\n",GetLastError());
374 MSVCRT__set_errno(GetLastError());
377 if (MSVCRT_tempfiles[fd])
379 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
380 _unlink(MSVCRT_tempfiles[fd]);
381 MSVCRT_free(MSVCRT_tempfiles[fd]);
382 MSVCRT_tempfiles[fd] = NULL;
389 /*********************************************************************
394 HANDLE hand = msvcrt_fdtoh(fd);
396 TRACE(":fd (%d) handle (%d)\n",fd,hand);
397 if (hand == INVALID_HANDLE_VALUE)
400 if (!FlushFileBuffers(hand))
402 if (GetLastError() == ERROR_INVALID_HANDLE)
404 /* FlushFileBuffers fails for console handles
405 * so we ignore this error.
409 TRACE(":failed-last error (%ld)\n",GetLastError());
410 MSVCRT__set_errno(GetLastError());
417 /*********************************************************************
423 HANDLE hand = msvcrt_fdtoh(fd);
425 TRACE(":fd (%d) handle (%d)\n",fd,hand);
427 if (hand == INVALID_HANDLE_VALUE)
430 /* If we have a FILE* for this file, the EOF flag
431 * will be set by the read()/write() functions.
433 if (MSVCRT_files[fd])
434 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
436 /* Otherwise we do it the hard way */
437 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
438 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
440 if (curpos == endpos)
443 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
447 /*********************************************************************
448 * _fcloseall (MSVCRT.@)
452 int num_closed = 0, i;
454 for (i = 3; i < MSVCRT_fdend; i++)
455 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
461 TRACE(":closed (%d) handles\n",num_closed);
465 /*********************************************************************
468 LONG _lseek(int fd, LONG offset, int whence)
471 HANDLE hand = msvcrt_fdtoh(fd);
473 TRACE(":fd (%d) handle (%d)\n",fd,hand);
474 if (hand == INVALID_HANDLE_VALUE)
477 if (whence < 0 || whence > 2)
479 SET_THREAD_VAR(errno,MSVCRT_EINVAL);
483 TRACE(":fd (%d) to 0x%08lx pos %s\n",
484 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
485 (whence==SEEK_CUR)?"SEEK_CUR":
486 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
488 if ((ret = SetFilePointer(hand, offset, NULL, whence)) != 0xffffffff)
490 if (MSVCRT_files[fd])
491 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
492 /* FIXME: What if we seek _to_ EOF - is EOF set? */
495 TRACE(":error-last error (%ld)\n",GetLastError());
496 if (MSVCRT_files[fd])
497 switch(GetLastError())
499 case ERROR_NEGATIVE_SEEK:
500 case ERROR_SEEK_ON_DEVICE:
501 MSVCRT__set_errno(GetLastError());
502 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
510 /*********************************************************************
513 void MSVCRT_rewind(MSVCRT_FILE* file)
515 TRACE(":file (%p) fd (%d)\n",file,file->_file);
516 MSVCRT_fseek(file, 0L, SEEK_SET);
517 MSVCRT_clearerr(file);
520 /*********************************************************************
523 MSVCRT_FILE* _fdopen(int fd, const char *mode)
525 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
527 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
534 /*********************************************************************
535 * _wfdopen (MSVCRT.@)
537 MSVCRT_FILE* _wfdopen(int fd, const WCHAR *mode)
539 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
541 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
548 /*********************************************************************
549 * _filelength (MSVCRT.@)
551 LONG _filelength(int fd)
553 LONG curPos = _lseek(fd, 0, SEEK_CUR);
556 LONG endPos = _lseek(fd, 0, SEEK_END);
559 if (endPos != curPos)
560 _lseek(fd, curPos, SEEK_SET);
567 /*********************************************************************
570 int _fileno(MSVCRT_FILE* file)
572 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
576 /*********************************************************************
577 * _flushall (MSVCRT.@)
581 int num_flushed = 0, i = 3;
583 while(i < MSVCRT_fdend)
584 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
587 /* FIXME: flush, do not commit */
588 if (_commit(i) == -1)
590 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
592 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
593 MSVCRT_fflush(MSVCRT_files[i]);
598 TRACE(":flushed (%d) handles\n",num_flushed);
602 /*********************************************************************
605 int _fstat(int fd, struct _stat* buf)
608 BY_HANDLE_FILE_INFORMATION hfi;
609 HANDLE hand = msvcrt_fdtoh(fd);
611 TRACE(":fd (%d) stat (%p)\n",fd,buf);
612 if (hand == INVALID_HANDLE_VALUE)
617 WARN(":failed-NULL buf\n");
618 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
622 memset(&hfi, 0, sizeof(hfi));
623 memset(buf, 0, sizeof(struct _stat));
624 if (!GetFileInformationByHandle(hand, &hfi))
626 WARN(":failed-last error (%ld)\n",GetLastError());
627 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
630 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
631 buf->st_nlink = hfi.nNumberOfLinks;
632 buf->st_size = hfi.nFileSizeLow;
633 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
635 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
636 buf->st_mtime = buf->st_ctime = dw;
640 /*********************************************************************
643 int _futime(int fd, struct _utimbuf *t)
645 HANDLE hand = msvcrt_fdtoh(fd);
650 MSVCRT_time_t currTime;
651 MSVCRT_time(&currTime);
652 RtlSecondsSince1970ToTime(currTime, &at);
653 memcpy(&wt, &at, sizeof(wt));
657 RtlSecondsSince1970ToTime(t->actime, &at);
658 if (t->actime == t->modtime)
659 memcpy(&wt, &at, sizeof(wt));
661 RtlSecondsSince1970ToTime(t->modtime, &wt);
664 if (!SetFileTime(hand, NULL, &at, &wt))
666 MSVCRT__set_errno(GetLastError());
672 /*********************************************************************
673 * _get_osfhandle (MSVCRT.@)
675 long _get_osfhandle(int fd)
677 HANDLE hand = msvcrt_fdtoh(fd);
678 HANDLE newhand = hand;
679 TRACE(":fd (%d) handle (%d)\n",fd,hand);
681 if (hand != INVALID_HANDLE_VALUE)
683 /* FIXME: I'm not convinced that I should be copying the
684 * handle here - it may be leaked if the app doesn't
685 * close it (and the API docs dont say that it should)
686 * Not duplicating it means that it can't be inherited
687 * and so lcc's wedit doesn't cope when it passes it to
688 * child processes. I've an idea that it should either
689 * be copied by CreateProcess, or marked as inheritable
690 * when initialised, or maybe both? JG 21-9-00.
692 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
693 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
698 /*********************************************************************
703 HANDLE hand = msvcrt_fdtoh(fd);
705 TRACE(":fd (%d) handle (%d)\n",fd,hand);
706 if (hand == INVALID_HANDLE_VALUE)
709 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
712 /*********************************************************************
715 char *_mktemp(char *pattern)
718 char *retVal = pattern;
723 numX = (*pattern++ == 'X')? numX + 1 : 0;
727 id = GetCurrentProcessId();
731 int tempNum = id / 10;
732 *pattern-- = id - (tempNum * 10) + '0';
738 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
739 GetLastError() == ERROR_FILE_NOT_FOUND)
742 } while(letter != '|');
746 /*********************************************************************
747 * _wmktemp (MSVCRT.@)
749 WCHAR *_wmktemp(WCHAR *pattern)
752 WCHAR *retVal = pattern;
754 WCHAR letter = (WCHAR)L'a';
757 numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
761 id = GetCurrentProcessId();
765 int tempNum = id / 10;
766 *pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
772 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
773 GetLastError() == ERROR_FILE_NOT_FOUND)
776 } while(letter != (WCHAR)L'|');
780 /*********************************************************************
783 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
787 DWORD access = 0, creation = 0;
791 SECURITY_ATTRIBUTES sa;
794 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
795 path, oflags, shflags);
797 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
800 access |= GENERIC_READ;
801 ioflag |= MSVCRT__IOREAD;
804 access |= GENERIC_WRITE;
805 ioflag |= MSVCRT__IOWRT;
808 access |= GENERIC_WRITE | GENERIC_READ;
809 ioflag |= MSVCRT__IORW;
813 if (oflags & _O_CREAT)
815 va_start(ap, shflags);
816 pmode = va_arg(ap, int);
819 FIXME(": pmode 0x%04x ignored\n", pmode);
821 if (oflags & _O_EXCL)
822 creation = CREATE_NEW;
823 else if (oflags & _O_TRUNC)
824 creation = CREATE_ALWAYS;
826 creation = OPEN_ALWAYS;
828 else /* no _O_CREAT */
830 if (oflags & _O_TRUNC)
831 creation = TRUNCATE_EXISTING;
833 creation = OPEN_EXISTING;
835 if (oflags & _O_APPEND)
836 ioflag |= MSVCRT__IOAPPEND;
839 oflags |= _O_BINARY; /* FIXME: Default to text */
841 if (oflags & _O_TEXT)
843 /* Dont warn when writing */
844 if (ioflag & GENERIC_READ)
845 FIXME(":TEXT node not implemented\n");
855 sharing = FILE_SHARE_READ;
858 sharing = FILE_SHARE_WRITE;
861 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
864 ERR( "Unhandled shflags 0x%x\n", shflags );
868 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
869 |_O_CREAT|_O_RDWR|_O_TEMPORARY|_O_NOINHERIT))
870 TRACE(":unsupported oflags 0x%04x\n",oflags);
872 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
873 sa.lpSecurityDescriptor = NULL;
874 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
876 hand = CreateFileA(path, access, sharing,
877 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
879 if (hand == INVALID_HANDLE_VALUE) {
880 WARN(":failed-last error (%ld)\n",GetLastError());
881 MSVCRT__set_errno(GetLastError());
885 fd = msvcrt_alloc_fd(hand, ioflag);
887 TRACE(":fd (%d) handle (%d)\n",fd, hand);
891 if (oflags & _O_TEMPORARY)
892 MSVCRT_tempfiles[fd] = _strdup(path);
893 if (ioflag & MSVCRT__IOAPPEND)
894 _lseek(fd, 0, FILE_END);
900 /*********************************************************************
903 int MSVCRT__wsopen( const WCHAR* path, int oflags, int shflags, ... )
905 const unsigned int len = strlenW(path);
906 char *patha = MSVCRT_calloc(len + 1,1);
910 va_start(ap, shflags);
911 pmode = va_arg(ap, int);
914 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
916 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
921 MSVCRT__set_errno(GetLastError());
925 /*********************************************************************
928 int _open( const char *path, int flags, ... )
934 pmode = va_arg(ap, int);
937 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
940 /*********************************************************************
943 int _wopen(const WCHAR *path,int flags,...)
945 const unsigned int len = strlenW(path);
946 char *patha = MSVCRT_calloc(len + 1,1);
951 pmode = va_arg(ap, int);
954 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
956 int retval = _open(patha,flags,pmode);
961 MSVCRT__set_errno(GetLastError());
965 /*********************************************************************
968 int _creat(const char *path, int flags)
970 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
971 return _open(path, usedFlags);
974 /*********************************************************************
977 int _wcreat(const WCHAR *path, int flags)
979 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
980 return _wopen(path, usedFlags);
983 /*********************************************************************
984 * _open_osfhandle (MSVCRT.@)
986 int _open_osfhandle(long hand, int flags)
988 int fd = msvcrt_alloc_fd(hand,flags);
989 TRACE(":handle (%ld) fd (%d)\n",hand,fd);
993 /*********************************************************************
998 int num_removed = 0, i;
1000 for (i = 3; i < MSVCRT_fdend; i++)
1001 if (MSVCRT_tempfiles[i])
1008 TRACE(":removed (%d) temp files\n",num_removed);
1012 /*********************************************************************
1015 int _read(int fd, void *buf, unsigned int count)
1018 HANDLE hand = msvcrt_fdtoh(fd);
1020 /* Dont trace small reads, it gets *very* annoying */
1022 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1023 if (hand == INVALID_HANDLE_VALUE)
1026 if (ReadFile(hand, buf, count, &num_read, NULL))
1028 if (num_read != count && MSVCRT_files[fd])
1031 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1033 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1038 TRACE(":failed-last error (%ld)\n",GetLastError());
1039 if (MSVCRT_files[fd])
1040 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1044 /*********************************************************************
1047 int _getw(MSVCRT_FILE* file)
1050 if (_read(file->_file, &i, sizeof(int)) != 1)
1055 /*********************************************************************
1056 * _setmode (MSVCRT.@)
1058 int _setmode(int fd,int mode)
1061 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
1065 /*********************************************************************
1068 int _stat(const char* path, struct _stat * buf)
1071 WIN32_FILE_ATTRIBUTE_DATA hfi;
1072 unsigned short mode = MSVCRT_S_IREAD;
1075 TRACE(":file (%s) buf(%p)\n",path,buf);
1077 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1079 TRACE("failed (%ld)\n",GetLastError());
1080 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1084 memset(buf,0,sizeof(struct _stat));
1086 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1087 Bon 011120: This FIXME seems incorrect
1088 Also a letter as first char isn't enough to be classify
1091 if (isalpha(*path)&& (*(path+1)==':'))
1092 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1094 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1096 plen = strlen(path);
1098 /* Dir, or regular file? */
1099 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1100 (path[plen-1] == '\\'))
1101 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1106 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1108 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1109 (tolower(path[plen-3]) << 16);
1110 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1111 mode |= MSVCRT_S_IEXEC;
1115 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1116 mode |= MSVCRT_S_IWRITE;
1118 buf->st_mode = mode;
1120 buf->st_size = hfi.nFileSizeLow;
1121 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1123 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1124 buf->st_mtime = buf->st_ctime = dw;
1125 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1126 buf->st_atime,buf->st_mtime, buf->st_ctime);
1130 /*********************************************************************
1133 int _wstat(const WCHAR* path, struct _stat * buf)
1136 WIN32_FILE_ATTRIBUTE_DATA hfi;
1137 unsigned short mode = MSVCRT_S_IREAD;
1140 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1142 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1144 TRACE("failed (%ld)\n",GetLastError());
1145 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1149 memset(buf,0,sizeof(struct _stat));
1151 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1152 if (MSVCRT_iswalpha(*path))
1153 buf->st_dev = buf->st_rdev = toupperW(*path - (WCHAR)L'A'); /* drive num */
1155 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1157 plen = strlenW(path);
1159 /* Dir, or regular file? */
1160 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1161 (path[plen-1] == (WCHAR)L'\\'))
1162 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1167 if (plen > 6 && path[plen-4] == (WCHAR)L'.') /* shortest exe: "\x.exe" */
1169 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1170 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1171 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1172 mode |= MSVCRT_S_IEXEC;
1176 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1177 mode |= MSVCRT_S_IWRITE;
1179 buf->st_mode = mode;
1181 buf->st_size = hfi.nFileSizeLow;
1182 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1184 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1185 buf->st_mtime = buf->st_ctime = dw;
1186 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1187 buf->st_atime,buf->st_mtime, buf->st_ctime);
1191 /*********************************************************************
1196 return _lseek(fd, 0, SEEK_CUR);
1199 /*********************************************************************
1200 * _tempnam (MSVCRT.@)
1202 char *_tempnam(const char *dir, const char *prefix)
1204 char tmpbuf[MAX_PATH];
1206 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1207 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1209 TRACE("got name (%s)\n",tmpbuf);
1210 return _strdup(tmpbuf);
1212 TRACE("failed (%ld)\n",GetLastError());
1216 /*********************************************************************
1217 * _wtempnam (MSVCRT.@)
1219 WCHAR *_wtempnam(const WCHAR *dir, const WCHAR *prefix)
1221 WCHAR tmpbuf[MAX_PATH];
1223 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1224 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1226 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1227 return _wcsdup(tmpbuf);
1229 TRACE("failed (%ld)\n",GetLastError());
1233 /*********************************************************************
1236 int _umask(int umask)
1238 int old_umask = MSVCRT_umask;
1239 TRACE("(%d)\n",umask);
1240 MSVCRT_umask = umask;
1244 /*********************************************************************
1247 int _utime(const char* path, struct _utimbuf *t)
1249 int fd = _open(path, _O_WRONLY | _O_BINARY);
1253 int retVal = _futime(fd, t);
1260 /*********************************************************************
1261 * _wutime (MSVCRT.@)
1263 int _wutime(const WCHAR* path, struct _utimbuf *t)
1265 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1269 int retVal = _futime(fd, t);
1276 /*********************************************************************
1279 int _write(int fd, const void* buf, unsigned int count)
1282 HANDLE hand = msvcrt_fdtoh(fd);
1284 /* Dont trace small writes, it gets *very* annoying */
1286 // TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1287 if (hand == INVALID_HANDLE_VALUE)
1290 /* If appending, go to EOF */
1291 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1292 _lseek(fd, 0, FILE_END);
1294 if (WriteFile(hand, buf, count, &num_written, NULL)
1295 && (num_written == count))
1298 TRACE(":failed-last error (%ld)\n",GetLastError());
1299 if (MSVCRT_files[fd])
1300 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1305 /*********************************************************************
1308 int _putw(int val, MSVCRT_FILE* file)
1310 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1313 /*********************************************************************
1314 * clearerr (MSVCRT.@)
1316 void MSVCRT_clearerr(MSVCRT_FILE* file)
1318 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1319 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1322 /*********************************************************************
1325 int MSVCRT_fclose(MSVCRT_FILE* file)
1328 r=_close(file->_file);
1329 return ((r==MSVCRT_EOF) || (file->_flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1332 /*********************************************************************
1335 int MSVCRT_feof(MSVCRT_FILE* file)
1337 return file->_flag & MSVCRT__IOEOF;
1340 /*********************************************************************
1343 int MSVCRT_ferror(MSVCRT_FILE* file)
1345 return file->_flag & MSVCRT__IOERR;
1348 /*********************************************************************
1351 int MSVCRT_fflush(MSVCRT_FILE* file)
1357 int res=msvcrt_flush_buffer(file);
1362 /*********************************************************************
1365 int MSVCRT_fgetc(MSVCRT_FILE* file)
1369 return *(unsigned char *)file->_ptr++;
1371 return _filbuf(file);
1375 /*********************************************************************
1376 * _fgetchar (MSVCRT.@)
1380 return MSVCRT_fgetc(MSVCRT_stdin);
1383 /*********************************************************************
1384 * _filbuf (MSVCRT.@)
1386 int _filbuf(MSVCRT_FILE* file)
1389 /* Allocate buffer if needed */
1390 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1391 msvcrt_alloc_buffer(file);
1393 if(!(file->_flag & MSVCRT__IOREAD)) {
1394 if(file->_flag & MSVCRT__IORW) {
1395 file->_flag |= MSVCRT__IOREAD;
1400 if(file->_flag & MSVCRT__IONBF) {
1402 if (_read(file->_file,&c,1) != 1) {
1403 file->_flag |= MSVCRT__IOEOF;
1408 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1409 if(file->_cnt<0) file->_cnt = 0;
1411 file->_flag |= MSVCRT__IOEOF;
1415 file->_ptr = file->_base+1;
1416 return *(unsigned char *)file->_base;
1420 /*********************************************************************
1421 * fgetpos (MSVCRT.@)
1423 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1425 *pos = MSVCRT_ftell(file);
1426 return (*pos == -1? -1 : 0);
1429 /*********************************************************************
1432 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1435 char * buf_start = s;
1437 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1438 file,file->_file,s,size);
1440 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1441 cc = MSVCRT_fgetc(file))
1444 if (--size <= 0) break;
1447 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1449 TRACE(":nothing read\n");
1456 TRACE(":got '%s'\n", buf_start);
1460 /*********************************************************************
1463 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1466 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1471 /*********************************************************************
1474 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1476 return MSVCRT_fgetwc(file);
1479 /*********************************************************************
1480 * _fgetwchar (MSVCRT.@)
1482 MSVCRT_wint_t _fgetwchar(void)
1484 return MSVCRT_fgetwc(MSVCRT_stdin);
1487 /*********************************************************************
1488 * getwchar (MSVCRT.@)
1490 MSVCRT_wint_t MSVCRT_getwchar(void)
1492 return _fgetwchar();
1495 /*********************************************************************
1498 WCHAR *MSVCRT_fgetws(WCHAR *s, int size, MSVCRT_FILE* file)
1501 WCHAR * buf_start = s;
1503 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1504 file,file->_file,s,size);
1506 for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n';
1507 cc = MSVCRT_fgetwc(file))
1510 if (--size <= 0) break;
1513 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1515 TRACE(":nothing read\n");
1522 /* TRACE(":got '%s'\n", buf_start); */
1527 /*********************************************************************
1530 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1533 if (MSVCRT_fwrite( &mwc, 1, sizeof(mwc), file) != sizeof(mwc))
1538 /*********************************************************************
1539 * _fputwchar (MSVCRT.@)
1541 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1543 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1546 /*********************************************************************
1549 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1552 int flags = 0, plus = 0, fd;
1553 const char* search = mode;
1555 TRACE("(%s,%s)\n",path,mode);
1558 if (*search++ == '+')
1561 /* map mode string to open() flags. "man fopen" for possibilities. */
1565 flags = (plus ? _O_RDWR : _O_RDONLY);
1568 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1571 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1586 flags &= ~_O_BINARY;
1591 FIXME(":unknown flag %c not supported\n",mode[-1]);
1594 fd = _open(path, flags);
1599 file = msvcrt_alloc_fp(fd);
1600 TRACE(":got (%p)\n",file);
1607 /*********************************************************************
1608 * _wfopen (MSVCRT.@)
1610 MSVCRT_FILE *_wfopen(const WCHAR *path, const WCHAR *mode)
1612 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1613 char *patha = MSVCRT_calloc(plen + 1, 1);
1614 char *modea = MSVCRT_calloc(mlen + 1, 1);
1616 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1618 if (patha && modea &&
1619 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1620 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1622 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1628 MSVCRT__set_errno(GetLastError());
1632 /*********************************************************************
1633 * _fsopen (MSVCRT.@)
1635 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1637 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1638 return MSVCRT_fopen(path,mode);
1641 /*********************************************************************
1642 * _wfsopen (MSVCRT.@)
1644 MSVCRT_FILE* _wfsopen(const WCHAR *path, const WCHAR *mode, int share)
1646 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1647 debugstr_w(path),debugstr_w(mode),share);
1648 return _wfopen(path,mode);
1651 /*********************************************************************
1654 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1661 return _flsbuf(c, file);
1665 /*********************************************************************
1666 * _flsbuf (MSVCRT.@)
1668 int _flsbuf(int c, MSVCRT_FILE* file)
1670 /* Flush output buffer */
1671 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
1672 msvcrt_alloc_buffer(file);
1674 if(!(file->_flag & MSVCRT__IOWRT)) {
1675 if(file->_flag & MSVCRT__IORW) {
1676 file->_flag |= MSVCRT__IOWRT;
1682 int res=msvcrt_flush_buffer(file);
1683 return res?res : MSVCRT_fputc(c, file);
1686 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
1690 /*********************************************************************
1691 * _fputchar (MSVCRT.@)
1693 int _fputchar(int c)
1695 return MSVCRT_fputc(c, MSVCRT_stdout);
1698 /*********************************************************************
1701 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1702 { MSVCRT_size_t rcnt=size * nmemb;
1703 MSVCRT_size_t read=0;
1705 /* first buffered data */
1707 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
1708 memcpy(ptr, file->_ptr, pcnt);
1714 } else if(!(file->_flag & MSVCRT__IOREAD )) {
1715 if(file->_flag & MSVCRT__IORW) {
1716 file->_flag |= MSVCRT__IOREAD;
1720 if(rcnt) pread = _read(file->_file,ptr, rcnt);
1727 /*********************************************************************
1728 * freopen (MSVCRT.@)
1731 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
1733 MSVCRT_FILE* newfile;
1736 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1737 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
1743 FIXME(":reopen on user file not implemented!\n");
1744 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1747 if(MSVCRT_fclose(file))
1749 return MSVCRT_fopen(path, mode);
1752 /* first, create the new file */
1753 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
1756 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1757 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1758 MSVCRT_handles[newfile->_file]))
1760 /* Redirecting std handle to file , copy over.. */
1761 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
1762 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
1763 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
1764 MSVCRT__iob[fd]._file = fd;
1765 /* And free up the resources allocated by fopen, but
1766 * not the HANDLE we copied. */
1767 MSVCRT_free(MSVCRT_files[fd]);
1768 msvcrt_free_fd(newfile->_file);
1769 return &MSVCRT__iob[fd];
1772 WARN(":failed-last error (%ld)\n",GetLastError());
1773 MSVCRT_fclose(newfile);
1774 MSVCRT__set_errno(GetLastError());
1778 /*********************************************************************
1779 * fsetpos (MSVCRT.@)
1781 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1783 return _lseek(file->_file,*pos,SEEK_SET);
1786 /* helper function for fscanf. Returns the value of character c in the
1787 * given base, or -1 if the given character is not a digit of the base.
1789 static int char2digit(char c, int base) {
1790 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
1791 if (base<=10) return -1;
1792 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
1793 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
1797 /*********************************************************************
1799 * Implemented based on
1800 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
1801 * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
1802 * more types of format spec.
1804 int MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
1806 /* NOTE: If you extend this function, extend MSVCRT__cscanf in console.c too */
1810 if (!*format) return 0;
1811 WARN("%p (\"%s\"): semi-stub\n", file, format);
1812 nch = MSVCRT_fgetc(file);
1813 va_start(ap, format);
1815 /* a whitespace character in the format string causes scanf to read,
1816 * but not store, all consecutive white-space characters in the input
1817 * up to the next non-white-space character. One white space character
1818 * in the input matches any number (including zero) and combination of
1819 * white-space characters in the input. */
1820 if (isspace(*format)) {
1821 /* skip whitespace */
1822 while ((nch!=MSVCRT_EOF) && isspace(nch))
1823 nch = MSVCRT_fgetc(file);
1825 /* a format specification causes scanf to read and convert characters
1826 * in the input into values of a specified type. The value is assigned
1827 * to an argument in the argument list. Format specifications have
1828 * the form %[*][width][{h | l | I64 | L}]type */
1829 /* FIXME: unimplemented: h/l/I64/L modifiers and some type specs. */
1830 else if (*format == '%') {
1831 int st = 0; int suppress = 0; int width = 0;
1832 int base, number_signed;
1834 /* look for leading asterisk, which means 'suppress assignment of
1840 /* look for width specification */
1841 while (isdigit(*format)) {
1843 width+=*format++ - '0';
1845 if (width==0) width=-1; /* no width spec seen */
1847 case '%': /* read a percent symbol */
1848 if (nch!='%') break;
1849 nch = MSVCRT_fgetc(file);
1852 case 'X': /* hexadecimal integer. */
1853 base = 16; number_signed = 0;
1855 case 'o': /* octal integer */
1856 base = 8; number_signed = 0;
1858 case 'u': /* unsigned decimal integer */
1859 base = 10; number_signed = 0;
1861 case 'd': /* signed decimal integer */
1862 base = 10; number_signed = 1;
1864 case 'i': /* generic integer */
1865 base = 0; number_signed = 1;
1867 /* read an integer */
1868 int*val = suppress ? NULL : va_arg(ap, int*);
1869 int cur = 0; int negative = 0; int seendigit=0;
1870 /* skip initial whitespace */
1871 while ((nch!=MSVCRT_EOF) && isspace(nch))
1872 nch = MSVCRT_fgetc(file);
1874 if (number_signed && (nch == '-' || nch == '+')) {
1875 negative = (nch=='-');
1876 nch = MSVCRT_fgetc(file);
1877 if (width>0) width--;
1879 /* look for leading indication of base */
1880 if (width!=0 && nch == '0') {
1881 nch = MSVCRT_fgetc(file);
1882 if (width>0) width--;
1884 if (width!=0 && (nch=='x' || nch=='X')) {
1888 nch = MSVCRT_fgetc(file);
1889 if (width>0) width--;
1897 /* throw away leading zeros */
1898 while (width!=0 && nch=='0') {
1899 nch = MSVCRT_fgetc(file);
1900 if (width>0) width--;
1903 /* get first digit. Keep working copy negative, as the
1904 * range of negative numbers in two's complement notation
1905 * is one larger than the range of positive numbers. */
1906 if (width!=0 && char2digit(nch, base)!=-1) {
1907 cur = -char2digit(nch, base);
1908 nch = MSVCRT_fgetc(file);
1909 if (width>0) width--;
1912 /* read until no more digits */
1913 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
1914 cur = cur*base + char2digit(nch, base);
1915 nch = MSVCRT_fgetc(file);
1916 if (width>0) width--;
1919 /* negate parsed number if non-negative */
1920 if (!negative) cur=-cur;
1922 if (!seendigit) break; /* not a valid number */
1924 if (!suppress) *val = cur;
1931 case 'G': { /* read a float */
1932 float*val = suppress ? NULL : va_arg(ap, float*);
1935 /* skip initial whitespace */
1936 while ((nch!=MSVCRT_EOF) && isspace(nch))
1937 nch = MSVCRT_fgetc(file);
1939 if (nch == '-' || nch == '+') {
1940 negative = (nch=='-');
1941 if (width>0) width--;
1942 if (width==0) break;
1943 nch = MSVCRT_fgetc(file);
1945 /* get first digit. */
1946 if (!isdigit(nch)) break;
1947 cur = (nch - '0') * (negative ? -1 : 1);
1948 nch = MSVCRT_fgetc(file);
1949 if (width>0) width--;
1950 /* read until no more digits */
1951 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
1952 cur = cur*10 + (nch - '0');
1953 nch = MSVCRT_fgetc(file);
1954 if (width>0) width--;
1956 /* handle decimals */
1957 if (width!=0 && nch == '.') {
1959 nch = MSVCRT_fgetc(file);
1960 if (width>0) width--;
1961 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
1963 cur += dec * (nch - '0');
1964 nch = MSVCRT_fgetc(file);
1965 if (width>0) width--;
1968 /* handle exponent */
1969 if (width!=0 && (nch == 'e' || nch == 'E')) {
1970 int exponent = 0, negexp = 0;
1972 nch = MSVCRT_fgetc(file);
1973 if (width>0) width--;
1974 /* possible sign on the exponent */
1975 if (width!=0 && (nch=='+' || nch=='-')) {
1976 negexp = (nch=='-');
1977 nch = MSVCRT_fgetc(file);
1978 if (width>0) width--;
1980 /* exponent digits */
1981 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
1983 exponent += (nch - '0');
1984 nch = MSVCRT_fgetc(file);
1985 if (width>0) width--;
1987 /* update 'cur' with this exponent. */
1988 expcnt = negexp ? .1 : 10;
1989 while (exponent!=0) {
1993 expcnt=expcnt*expcnt;
1997 if (!suppress) *val = cur;
2000 case 's': { /* read a word */
2001 char*str = suppress ? NULL : va_arg(ap, char*);
2003 /* skip initial whitespace */
2004 while ((nch!=MSVCRT_EOF) && isspace(nch))
2005 nch = MSVCRT_fgetc(file);
2006 /* read until whitespace */
2007 while (width!=0 && (nch!=MSVCRT_EOF) && !isspace(nch)) {
2008 if (!suppress) *sptr++ = nch;
2010 nch = MSVCRT_fgetc(file);
2011 if (width>0) width--;
2014 if (!suppress) *sptr = 0;
2015 TRACE("read word: %s\n", str);
2018 default: FIXME("unhandled: %%%c\n", *format);
2019 /* From spec: "if a percent sign is followed by a character
2020 * that has no meaning as a format-control character, that
2021 * character and the following characters are treated as
2022 * an ordinary sequence of characters, that is, a sequence
2023 * of characters that must match the input. For example,
2024 * to specify that a percent-sign character is to be input,
2026 * LEAVING AS-IS because we catch bugs better that way. */
2028 if (st && !suppress) rd++;
2031 /* a non-white-space character causes scanf to read, but not store,
2032 * a matching non-white-space character. */
2034 /* check for character match */
2036 nch = MSVCRT_fgetc(file);
2041 if (nch!=MSVCRT_EOF) {
2042 FIXME("need ungetch\n");
2045 TRACE("returning %d\n", rd);
2049 /*********************************************************************
2052 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
2054 /* Flush output if needed */
2055 if(file->_flag & MSVCRT__IOWRT)
2056 msvcrt_flush_buffer(file);
2058 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
2059 offset -= file->_cnt;
2061 /* Discard buffered input */
2063 file->_ptr = file->_base;
2064 /* Reset direction of i/o */
2065 if(file->_flag & MSVCRT__IORW) {
2066 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2068 return _lseek(file->_file,offset,whence);
2071 /*********************************************************************
2074 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2079 if( file->_flag & MSVCRT__IOWRT ) {
2080 off = file->_ptr - file->_base;
2085 pos = _tell(file->_file);
2086 if(pos == -1) return pos;
2090 /*********************************************************************
2093 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2095 MSVCRT_size_t wrcnt=size * nmemb;
2098 int pcnt=(file->_cnt>wrcnt)? file->_cnt: wrcnt;
2099 memcpy(file->_ptr, ptr, pcnt);
2105 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2106 if(file->_flag & MSVCRT__IORW) {
2107 file->_flag |= MSVCRT__IOWRT;
2113 int res=msvcrt_flush_buffer(file);
2115 int pwritten = _write(file->_file, ptr, wrcnt);
2116 if (pwritten <= 0) pwritten=0;
2117 written += pwritten;
2120 return written / size;
2123 /*********************************************************************
2126 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2128 return MSVCRT_fwrite(s,strlen(s),1,file) == 1 ? 0 : MSVCRT_EOF;
2131 /*********************************************************************
2134 int MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
2136 return MSVCRT_fwrite(s,strlenW(s),1,file) == 1 ? 0 : MSVCRT_EOF;
2139 /*********************************************************************
2140 * getchar (MSVCRT.@)
2142 int MSVCRT_getchar(void)
2144 return MSVCRT_fgetc(MSVCRT_stdin);
2147 /*********************************************************************
2150 int MSVCRT_getc(MSVCRT_FILE* file)
2152 return MSVCRT_fgetc(file);
2155 /*********************************************************************
2158 char *MSVCRT_gets(char *buf)
2161 char * buf_start = buf;
2163 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2164 cc = MSVCRT_fgetc(MSVCRT_stdin))
2165 if(cc != '\r') *buf++ = (char)cc;
2169 TRACE("got '%s'\n", buf_start);
2173 /*********************************************************************
2176 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2178 return MSVCRT_fputc(c, file);
2181 /*********************************************************************
2182 * putchar (MSVCRT.@)
2184 int MSVCRT_putchar(int c)
2186 return MSVCRT_fputc(c, MSVCRT_stdout);
2189 /*********************************************************************
2192 int MSVCRT_puts(const char *s)
2194 int retval = MSVCRT_EOF;
2195 if (MSVCRT_fwrite(s,strlen(s),1,MSVCRT_stdout) == 1)
2196 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2200 /*********************************************************************
2203 int _putws(const WCHAR *s)
2205 static const WCHAR nl = (WCHAR)L'\n';
2206 if (MSVCRT_fwrite(s,strlenW(s),1,MSVCRT_stdout) == 1)
2207 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2211 /*********************************************************************
2214 int MSVCRT_remove(const char *path)
2216 TRACE("(%s)\n",path);
2217 if (DeleteFileA(path))
2219 TRACE(":failed (%ld)\n",GetLastError());
2220 MSVCRT__set_errno(GetLastError());
2224 /*********************************************************************
2225 * _wremove (MSVCRT.@)
2227 int _wremove(const WCHAR *path)
2229 TRACE("(%s)\n",debugstr_w(path));
2230 if (DeleteFileW(path))
2232 TRACE(":failed (%ld)\n",GetLastError());
2233 MSVCRT__set_errno(GetLastError());
2237 /*********************************************************************
2240 int MSVCRT_scanf(const char *format, ...)
2245 va_start(valist, format);
2246 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
2251 /*********************************************************************
2254 int MSVCRT_rename(const char *oldpath,const char *newpath)
2256 TRACE(":from %s to %s\n",oldpath,newpath);
2257 if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
2259 TRACE(":failed (%ld)\n",GetLastError());
2260 MSVCRT__set_errno(GetLastError());
2264 /*********************************************************************
2265 * _wrename (MSVCRT.@)
2267 int _wrename(const WCHAR *oldpath,const WCHAR *newpath)
2269 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2270 if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
2272 TRACE(":failed (%ld)\n",GetLastError());
2273 MSVCRT__set_errno(GetLastError());
2277 /*********************************************************************
2278 * setvbuf (MSVCRT.@)
2280 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2282 /* TODO: Check if file busy */
2284 MSVCRT_free(file->_base);
2288 if(mode == MSVCRT__IOFBF) {
2289 file->_flag &= ~MSVCRT__IONBF;
2290 file->_base = file->_ptr = buf;
2292 file->_bufsiz = size;
2295 file->_flag |= MSVCRT__IONBF;
2300 /*********************************************************************
2303 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2305 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2308 /*********************************************************************
2311 char *MSVCRT_tmpnam(char *s)
2313 char tmpbuf[MAX_PATH];
2314 char* prefix = "TMP";
2315 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
2316 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
2318 TRACE(":failed-last error (%ld)\n",GetLastError());
2321 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
2326 /*********************************************************************
2327 * tmpfile (MSVCRT.@)
2329 MSVCRT_FILE* MSVCRT_tmpfile(void)
2331 char *filename = MSVCRT_tmpnam(NULL);
2333 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2335 return msvcrt_alloc_fp(fd);
2339 /*********************************************************************
2340 * vfprintf (MSVCRT.@)
2342 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2344 char buf[2048], *mem = buf;
2345 int written, resize = sizeof(buf), retval;
2346 /* There are two conventions for vsnprintf failing:
2347 * Return -1 if we truncated, or
2348 * Return the number of bytes that would have been written
2349 * The code below handles both cases
2351 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2354 resize = (written == -1 ? resize * 2 : written + 1);
2357 if (!(mem = (char *)MSVCRT_malloc(resize)))
2360 retval = MSVCRT_fwrite(mem, 1, written, file);
2366 /*********************************************************************
2367 * vfwprintf (MSVCRT.@)
2369 * Is final char included in written (then resize is too big) or not
2370 * (then we must test for equality too)?
2372 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
2374 WCHAR buf[2048], *mem = buf;
2375 int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
2376 /* See vfprintf comments */
2377 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2380 resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
2383 if (!(mem = (WCHAR *)MSVCRT_malloc(resize*sizeof(*mem))))
2386 retval = MSVCRT_fwrite(mem, 1, written * sizeof (WCHAR), file);
2392 /*********************************************************************
2393 * vprintf (MSVCRT.@)
2395 int MSVCRT_vprintf(const char *format, va_list valist)
2397 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2400 /*********************************************************************
2401 * vwprintf (MSVCRT.@)
2403 int MSVCRT_vwprintf(const WCHAR *format, va_list valist)
2405 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2408 /*********************************************************************
2409 * fprintf (MSVCRT.@)
2411 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2415 va_start(valist, format);
2416 res = MSVCRT_vfprintf(file, format, valist);
2421 /*********************************************************************
2422 * fwprintf (MSVCRT.@)
2424 int MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
2428 va_start(valist, format);
2429 res = MSVCRT_vfwprintf(file, format, valist);
2434 /*********************************************************************
2437 int MSVCRT_printf(const char *format, ...)
2441 va_start(valist, format);
2442 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2447 /*********************************************************************
2450 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2452 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2453 msvcrt_alloc_buffer(file);
2456 if(file->_ptr>file->_base) {
2465 /*********************************************************************
2466 * ungetwc (MSVCRT.@)
2468 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2471 char * pp = (char *)&mwc;
2473 for(i=sizeof(WCHAR)-1;i>=0;i--) {
2474 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2480 /*********************************************************************
2481 * wprintf (MSVCRT.@)
2483 int MSVCRT_wprintf(const WCHAR *format, ...)
2487 va_start(valist, format);
2488 res = MSVCRT_vwprintf(format, valist);