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
25 #include "wine/port.h"
35 #include "msvcrt/errno.h"
37 #include "wine/unicode.h"
38 #include "msvcrt/direct.h"
39 #include "msvcrt/fcntl.h"
40 #include "msvcrt/io.h"
41 #include "msvcrt/sys/locking.h"
42 #include "msvcrt/stdio.h"
43 #include "msvcrt/stdlib.h"
44 #include "msvcrt/string.h"
45 #include "msvcrt/sys/stat.h"
46 #include "msvcrt/sys/utime.h"
47 #include "msvcrt/time.h"
48 #include "msvcrt/share.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
54 /* for stat mode, permissions apply to all,owner and group */
55 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
56 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
57 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
59 /* _access() bit flags FIXME: incomplete */
60 #define MSVCRT_W_OK 0x02
63 /* FIXME: Make this dynamic */
64 #define MSVCRT_MAX_FILES 257
66 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
67 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
68 int MSVCRT_flags[MSVCRT_MAX_FILES];
69 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
70 MSVCRT_FILE MSVCRT__iob[3];
71 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
72 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
73 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
75 static int MSVCRT_fdstart = 3; /* first unallocated fd */
76 static int MSVCRT_fdend = 3; /* highest allocated fd */
78 /* INTERNAL: process umask */
79 static int MSVCRT_umask = 0;
81 /* INTERNAL: Static buffer for temp file name */
82 static char MSVCRT_tmpname[MAX_PATH];
84 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
85 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
86 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
87 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
89 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
90 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
91 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
92 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
93 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
95 extern CRITICAL_SECTION MSVCRT_file_cs;
96 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
97 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
99 static void msvcrt_cp_from_stati64(const struct _stati64 *bufi64, struct _stat *buf)
101 buf->st_dev = bufi64->st_dev;
102 buf->st_ino = bufi64->st_ino;
103 buf->st_mode = bufi64->st_mode;
104 buf->st_nlink = bufi64->st_nlink;
105 buf->st_uid = bufi64->st_uid;
106 buf->st_gid = bufi64->st_gid;
107 buf->st_rdev = bufi64->st_rdev;
108 buf->st_size = bufi64->st_size;
109 buf->st_atime = bufi64->st_atime;
110 buf->st_mtime = bufi64->st_mtime;
111 buf->st_ctime = bufi64->st_ctime;
114 /* INTERNAL: Get the HANDLE for a fd */
115 static HANDLE msvcrt_fdtoh(int fd)
117 if (fd < 0 || fd >= MSVCRT_fdend ||
118 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
120 WARN(":fd (%d) - no handle!\n",fd);
122 *MSVCRT__errno() = MSVCRT_EBADF;
123 return INVALID_HANDLE_VALUE;
125 return MSVCRT_handles[fd];
128 /* INTERNAL: free a file entry fd */
129 static void msvcrt_free_fd(int fd)
131 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
132 MSVCRT_files[fd] = 0;
133 MSVCRT_flags[fd] = 0;
134 TRACE(":fd (%d) freed\n",fd);
136 return; /* dont use 0,1,2 for user files */
137 if (fd == MSVCRT_fdend - 1)
139 if (fd < MSVCRT_fdstart)
143 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
144 static int msvcrt_alloc_fd(HANDLE hand, int flag)
146 int fd = MSVCRT_fdstart;
148 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
149 if (fd >= MSVCRT_MAX_FILES)
151 WARN(":files exhausted!\n");
154 MSVCRT_handles[fd] = hand;
155 MSVCRT_flags[fd] = flag;
157 /* locate next free slot */
158 if (fd == MSVCRT_fdend)
159 MSVCRT_fdstart = ++MSVCRT_fdend;
161 while(MSVCRT_fdstart < MSVCRT_fdend &&
162 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
168 /* INTERNAL: Allocate a FILE* for an fd slot
169 * This is done lazily to avoid memory wastage for low level open/write
170 * usage when a FILE* is not requested (but may be later).
172 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
174 TRACE(":fd (%d) allocating FILE*\n",fd);
175 if (fd < 0 || fd >= MSVCRT_fdend ||
176 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
178 WARN(":invalid fd %d\n",fd);
180 *MSVCRT__errno() = MSVCRT_EBADF;
183 if (!MSVCRT_files[fd])
185 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
187 MSVCRT_files[fd]->_file = fd;
188 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
189 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
192 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
193 return MSVCRT_files[fd];
197 /* INTERNAL: Set up stdin, stderr and stdout */
198 void msvcrt_init_io(void)
201 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
202 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
203 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
204 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
205 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
206 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
207 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
209 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles[0],
210 MSVCRT_handles[1],MSVCRT_handles[2]);
212 for (i = 0; i < 3; i++)
214 /* FILE structs for stdin/out/err are static and never deleted */
215 MSVCRT_files[i] = &MSVCRT__iob[i];
216 MSVCRT__iob[i]._file = i;
217 MSVCRT_tempfiles[i] = NULL;
221 /* free everything on process exit */
222 void msvcrt_free_io(void)
230 /* INTERNAL: Flush stdio file buffer */
231 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
234 int cnt=file->_ptr-file->_base;
235 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
238 file->_ptr=file->_base;
239 file->_cnt=file->_bufsiz;
244 /* INTERNAL: Allocate stdio file buffer */
245 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
247 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
249 file->_bufsiz = MSVCRT_BUFSIZ;
250 file->_flag |= MSVCRT__IOMYBUF;
252 file->_base = (unsigned char *)(&file->_charbuf);
254 file->_bufsiz = sizeof(file->_charbuf);
256 file->_ptr = file->_base;
260 /*********************************************************************
263 MSVCRT_FILE *__p__iob(void)
265 return &MSVCRT__iob[0];
268 /*********************************************************************
271 int _access(const char *filename, int mode)
273 DWORD attr = GetFileAttributesA(filename);
275 TRACE("(%s,%d) %ld\n",filename,mode,attr);
277 if (!filename || attr == 0xffffffff)
279 MSVCRT__set_errno(GetLastError());
282 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
284 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
290 /*********************************************************************
291 * _waccess (MSVCRT.@)
293 int _waccess(const WCHAR *filename, int mode)
295 DWORD attr = GetFileAttributesW(filename);
297 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
299 if (!filename || attr == 0xffffffff)
301 MSVCRT__set_errno(GetLastError());
304 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
306 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
312 /*********************************************************************
315 int _chmod(const char *path, int flags)
317 DWORD oldFlags = GetFileAttributesA(path);
319 if (oldFlags != 0x0FFFFFFFF)
321 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
322 oldFlags | FILE_ATTRIBUTE_READONLY;
324 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
327 MSVCRT__set_errno(GetLastError());
331 /*********************************************************************
334 int _wchmod(const WCHAR *path, int flags)
336 DWORD oldFlags = GetFileAttributesW(path);
338 if (oldFlags != 0x0FFFFFFFF)
340 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
341 oldFlags | FILE_ATTRIBUTE_READONLY;
343 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
346 MSVCRT__set_errno(GetLastError());
350 /*********************************************************************
353 int _unlink(const char *path)
355 TRACE("(%s)\n",path);
356 if(DeleteFileA(path))
358 TRACE("failed (%ld)\n",GetLastError());
359 MSVCRT__set_errno(GetLastError());
363 /*********************************************************************
364 * _wunlink (MSVCRT.@)
366 int _wunlink(const WCHAR *path)
368 TRACE("(%s)\n",debugstr_w(path));
369 if(DeleteFileW(path))
371 TRACE("failed (%ld)\n",GetLastError());
372 MSVCRT__set_errno(GetLastError());
376 /*********************************************************************
381 HANDLE hand = msvcrt_fdtoh(fd);
383 TRACE(":fd (%d) handle (%p)\n",fd,hand);
384 if (hand == INVALID_HANDLE_VALUE)
386 /* flush stdio buffers */
387 if(MSVCRT_files[fd]) {
388 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
389 MSVCRT_fflush(MSVCRT_files[fd]);
391 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
392 MSVCRT_free(MSVCRT_files[fd]->_base);
395 /* Dont free std FILE*'s, they are not dynamic */
396 if (fd > 2 && MSVCRT_files[fd])
397 MSVCRT_free(MSVCRT_files[fd]);
401 if (!CloseHandle(hand))
403 WARN(":failed-last error (%ld)\n",GetLastError());
404 MSVCRT__set_errno(GetLastError());
407 if (MSVCRT_tempfiles[fd])
409 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
410 _unlink(MSVCRT_tempfiles[fd]);
411 MSVCRT_free(MSVCRT_tempfiles[fd]);
412 MSVCRT_tempfiles[fd] = NULL;
419 /*********************************************************************
424 HANDLE hand = msvcrt_fdtoh(fd);
426 TRACE(":fd (%d) handle (%p)\n",fd,hand);
427 if (hand == INVALID_HANDLE_VALUE)
430 if (!FlushFileBuffers(hand))
432 if (GetLastError() == ERROR_INVALID_HANDLE)
434 /* FlushFileBuffers fails for console handles
435 * so we ignore this error.
439 TRACE(":failed-last error (%ld)\n",GetLastError());
440 MSVCRT__set_errno(GetLastError());
447 /*********************************************************************
453 HANDLE hand = msvcrt_fdtoh(fd);
455 TRACE(":fd (%d) handle (%p)\n",fd,hand);
457 if (hand == INVALID_HANDLE_VALUE)
460 /* If we have a FILE* for this file, the EOF flag
461 * will be set by the read()/write() functions.
463 if (MSVCRT_files[fd])
464 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
466 /* Otherwise we do it the hard way */
467 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
468 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
470 if (curpos == endpos)
473 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
477 /*********************************************************************
478 * _fcloseall (MSVCRT.@)
482 int num_closed = 0, i;
484 for (i = 3; i < MSVCRT_fdend; i++)
485 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
491 TRACE(":closed (%d) handles\n",num_closed);
495 /*********************************************************************
498 __int64 _lseeki64(int fd, __int64 offset, int whence)
500 DWORD ret, hoffset = (DWORD) (offset >> 32);
501 HANDLE hand = msvcrt_fdtoh(fd);
503 TRACE(":fd (%d) handle (%p)\n",fd,hand);
504 if (hand == INVALID_HANDLE_VALUE)
507 if (whence < 0 || whence > 2)
509 *MSVCRT__errno() = MSVCRT_EINVAL;
513 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
514 fd,hoffset,(long)offset,
515 (whence==SEEK_SET)?"SEEK_SET":
516 (whence==SEEK_CUR)?"SEEK_CUR":
517 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
519 if (((ret = SetFilePointer(hand, (long)offset, &hoffset,
520 whence)) != INVALID_SET_FILE_POINTER) || !GetLastError())
522 if (MSVCRT_files[fd])
523 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
524 /* FIXME: What if we seek _to_ EOF - is EOF set? */
526 return ((__int64)hoffset << 32) | ret;
528 TRACE(":error-last error (%ld)\n",GetLastError());
529 if (MSVCRT_files[fd])
530 switch(GetLastError())
532 case ERROR_NEGATIVE_SEEK:
533 case ERROR_SEEK_ON_DEVICE:
534 MSVCRT__set_errno(GetLastError());
535 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
543 /*********************************************************************
546 LONG _lseek(int fd, LONG offset, int whence)
548 return _lseeki64(fd, offset, whence);
551 /*********************************************************************
552 * _locking (MSVCRT.@)
554 * This is untested; the underlying LockFile doesn't work yet.
556 int _locking(int fd, int mode, LONG nbytes)
560 HANDLE hand = msvcrt_fdtoh(fd);
562 TRACE(":fd (%d) handle (%p)\n",fd,hand);
563 if (hand == INVALID_HANDLE_VALUE)
566 if (mode < 0 || mode > 4)
568 *MSVCRT__errno() = MSVCRT_EINVAL;
572 TRACE(":fd (%d) by 0x%08lx mode %s\n",
573 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
574 (mode==_LK_LOCK)?"_LK_LOCK":
575 (mode==_LK_NBLCK)?"_LK_NBLCK":
576 (mode==_LK_RLCK)?"_LK_RLCK":
577 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
580 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == 0xffffffff)
582 FIXME ("Seek failed\n");
583 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
586 if (mode == _LK_LOCK || mode == _LK_RLCK)
589 ret = 1; /* just to satisfy gcc */
592 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
597 else if (mode == _LK_UNLCK)
598 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
600 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
601 /* FIXME - what about error settings? */
605 /*********************************************************************
608 void MSVCRT_rewind(MSVCRT_FILE* file)
610 TRACE(":file (%p) fd (%d)\n",file,file->_file);
611 MSVCRT_fseek(file, 0L, SEEK_SET);
612 MSVCRT_clearerr(file);
615 /*********************************************************************
618 MSVCRT_FILE* _fdopen(int fd, const char *mode)
620 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
622 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
627 /*********************************************************************
628 * _wfdopen (MSVCRT.@)
630 MSVCRT_FILE* _wfdopen(int fd, const WCHAR *mode)
632 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
634 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
641 /*********************************************************************
642 * _filelength (MSVCRT.@)
644 LONG _filelength(int fd)
646 LONG curPos = _lseek(fd, 0, SEEK_CUR);
649 LONG endPos = _lseek(fd, 0, SEEK_END);
652 if (endPos != curPos)
653 _lseek(fd, curPos, SEEK_SET);
660 /*********************************************************************
663 int _fileno(MSVCRT_FILE* file)
665 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
669 /*********************************************************************
670 * _flushall (MSVCRT.@)
674 int num_flushed = 0, i = 3;
676 while(i < MSVCRT_fdend)
677 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
680 /* FIXME: flush, do not commit */
681 if (_commit(i) == -1)
683 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
685 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
686 MSVCRT_fflush(MSVCRT_files[i]);
691 TRACE(":flushed (%d) handles\n",num_flushed);
695 /*********************************************************************
696 * _fstati64 (MSVCRT.@)
698 int _fstati64(int fd, struct _stati64* buf)
701 BY_HANDLE_FILE_INFORMATION hfi;
702 HANDLE hand = msvcrt_fdtoh(fd);
704 TRACE(":fd (%d) stat (%p)\n",fd,buf);
705 if (hand == INVALID_HANDLE_VALUE)
710 WARN(":failed-NULL buf\n");
711 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
715 memset(&hfi, 0, sizeof(hfi));
716 memset(buf, 0, sizeof(struct _stati64));
717 if (!GetFileInformationByHandle(hand, &hfi))
719 WARN(":failed-last error (%ld)\n",GetLastError());
720 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
723 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
724 buf->st_nlink = hfi.nNumberOfLinks;
725 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
726 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
728 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
729 buf->st_mtime = buf->st_ctime = dw;
733 /*********************************************************************
736 int MSVCRT__fstat(int fd, struct _stat* buf)
738 struct _stati64 bufi64;
740 ret = _fstati64(fd, &bufi64);
742 msvcrt_cp_from_stati64(&bufi64, buf);
746 /*********************************************************************
749 int _futime(int fd, struct _utimbuf *t)
751 HANDLE hand = msvcrt_fdtoh(fd);
756 MSVCRT_time_t currTime;
757 MSVCRT_time(&currTime);
758 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
759 memcpy(&wt, &at, sizeof(wt));
763 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
764 if (t->actime == t->modtime)
765 memcpy(&wt, &at, sizeof(wt));
767 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
770 if (!SetFileTime(hand, NULL, &at, &wt))
772 MSVCRT__set_errno(GetLastError());
778 /*********************************************************************
779 * _get_osfhandle (MSVCRT.@)
781 long _get_osfhandle(int fd)
783 HANDLE hand = msvcrt_fdtoh(fd);
784 HANDLE newhand = hand;
785 TRACE(":fd (%d) handle (%p)\n",fd,hand);
787 if (hand != INVALID_HANDLE_VALUE)
789 /* FIXME: I'm not convinced that I should be copying the
790 * handle here - it may be leaked if the app doesn't
791 * close it (and the API docs dont say that it should)
792 * Not duplicating it means that it can't be inherited
793 * and so lcc's wedit doesn't cope when it passes it to
794 * child processes. I've an idea that it should either
795 * be copied by CreateProcess, or marked as inheritable
796 * when initialised, or maybe both? JG 21-9-00.
798 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
799 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
801 return (long)newhand;
804 /*********************************************************************
809 HANDLE hand = msvcrt_fdtoh(fd);
811 TRACE(":fd (%d) handle (%p)\n",fd,hand);
812 if (hand == INVALID_HANDLE_VALUE)
815 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
818 /*********************************************************************
821 char *_mktemp(char *pattern)
824 char *retVal = pattern;
829 numX = (*pattern++ == 'X')? numX + 1 : 0;
833 id = GetCurrentProcessId();
837 int tempNum = id / 10;
838 *pattern-- = id - (tempNum * 10) + '0';
844 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
845 GetLastError() == ERROR_FILE_NOT_FOUND)
848 } while(letter != '|');
852 /*********************************************************************
853 * _wmktemp (MSVCRT.@)
855 WCHAR *_wmktemp(WCHAR *pattern)
858 WCHAR *retVal = pattern;
860 WCHAR letter = (WCHAR)L'a';
863 numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
867 id = GetCurrentProcessId();
871 int tempNum = id / 10;
872 *pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
878 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
879 GetLastError() == ERROR_FILE_NOT_FOUND)
882 } while(letter != (WCHAR)L'|');
886 /*********************************************************************
889 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
893 DWORD access = 0, creation = 0;
897 SECURITY_ATTRIBUTES sa;
900 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
901 path, oflags, shflags);
903 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
906 access |= GENERIC_READ;
907 ioflag |= MSVCRT__IOREAD;
910 access |= GENERIC_WRITE;
911 ioflag |= MSVCRT__IOWRT;
914 access |= GENERIC_WRITE | GENERIC_READ;
915 ioflag |= MSVCRT__IORW;
919 if (oflags & _O_CREAT)
921 va_start(ap, shflags);
922 pmode = va_arg(ap, int);
925 FIXME(": pmode 0x%04x ignored\n", pmode);
927 if (oflags & _O_EXCL)
928 creation = CREATE_NEW;
929 else if (oflags & _O_TRUNC)
930 creation = CREATE_ALWAYS;
932 creation = OPEN_ALWAYS;
934 else /* no _O_CREAT */
936 if (oflags & _O_TRUNC)
937 creation = TRUNCATE_EXISTING;
939 creation = OPEN_EXISTING;
941 if (oflags & _O_APPEND)
942 ioflag |= MSVCRT__IOAPPEND;
945 oflags |= _O_BINARY; /* FIXME: Default to text */
947 if (oflags & _O_TEXT)
949 /* Dont warn when writing */
950 if (ioflag & GENERIC_READ)
951 FIXME(":TEXT node not implemented\n");
961 sharing = FILE_SHARE_READ;
964 sharing = FILE_SHARE_WRITE;
967 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
970 ERR( "Unhandled shflags 0x%x\n", shflags );
974 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
975 |_O_CREAT|_O_RDWR|_O_TEMPORARY|_O_NOINHERIT))
976 TRACE(":unsupported oflags 0x%04x\n",oflags);
978 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
979 sa.lpSecurityDescriptor = NULL;
980 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
982 hand = CreateFileA(path, access, sharing,
983 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
985 if (hand == INVALID_HANDLE_VALUE) {
986 WARN(":failed-last error (%ld)\n",GetLastError());
987 MSVCRT__set_errno(GetLastError());
991 fd = msvcrt_alloc_fd(hand, ioflag);
993 TRACE(":fd (%d) handle (%p)\n",fd, hand);
997 if (oflags & _O_TEMPORARY)
998 MSVCRT_tempfiles[fd] = _strdup(path);
999 if (ioflag & MSVCRT__IOAPPEND)
1000 _lseek(fd, 0, FILE_END);
1006 /*********************************************************************
1007 * _wsopen (MSVCRT.@)
1009 int MSVCRT__wsopen( const WCHAR* path, int oflags, int shflags, ... )
1011 const unsigned int len = strlenW(path);
1012 char *patha = MSVCRT_calloc(len + 1,1);
1016 va_start(ap, shflags);
1017 pmode = va_arg(ap, int);
1020 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1022 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1027 MSVCRT__set_errno(GetLastError());
1031 /*********************************************************************
1034 int _open( const char *path, int flags, ... )
1039 va_start(ap, flags);
1040 pmode = va_arg(ap, int);
1043 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1046 /*********************************************************************
1049 int _wopen(const WCHAR *path,int flags,...)
1051 const unsigned int len = strlenW(path);
1052 char *patha = MSVCRT_calloc(len + 1,1);
1056 va_start(ap, flags);
1057 pmode = va_arg(ap, int);
1060 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1062 int retval = _open(patha,flags,pmode);
1067 MSVCRT__set_errno(GetLastError());
1071 /*********************************************************************
1074 int _creat(const char *path, int flags)
1076 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1077 return _open(path, usedFlags);
1080 /*********************************************************************
1081 * _wcreat (MSVCRT.@)
1083 int _wcreat(const WCHAR *path, int flags)
1085 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1086 return _wopen(path, usedFlags);
1089 /*********************************************************************
1090 * _open_osfhandle (MSVCRT.@)
1092 int _open_osfhandle(long hand, int flags)
1094 /* _O_RDONLY (0) always matches, so set the read flag*/
1095 /* FIXME: handle more flags */
1096 int fd= msvcrt_alloc_fd((HANDLE)hand,flags|MSVCRT__IOREAD);
1097 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd, flags |MSVCRT__IOREAD);
1101 /*********************************************************************
1106 int num_removed = 0, i;
1108 for (i = 3; i < MSVCRT_fdend; i++)
1109 if (MSVCRT_tempfiles[i])
1116 TRACE(":removed (%d) temp files\n",num_removed);
1120 /*********************************************************************
1123 int _read(int fd, void *buf, unsigned int count)
1126 HANDLE hand = msvcrt_fdtoh(fd);
1128 /* Dont trace small reads, it gets *very* annoying */
1130 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1131 if (hand == INVALID_HANDLE_VALUE)
1134 if (ReadFile(hand, buf, count, &num_read, NULL))
1136 if (num_read != count && MSVCRT_files[fd])
1139 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1141 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1146 TRACE(":failed-last error (%ld)\n",GetLastError());
1147 if (MSVCRT_files[fd])
1148 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1152 /*********************************************************************
1155 int _getw(MSVCRT_FILE* file)
1158 if (_read(file->_file, &i, sizeof(int)) != 1)
1163 /*********************************************************************
1164 * _setmode (MSVCRT.@)
1166 int _setmode(int fd,int mode)
1169 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
1173 /*********************************************************************
1174 * _stati64 (MSVCRT.@)
1176 int _stati64(const char* path, struct _stati64 * buf)
1179 WIN32_FILE_ATTRIBUTE_DATA hfi;
1180 unsigned short mode = MSVCRT_S_IREAD;
1183 TRACE(":file (%s) buf(%p)\n",path,buf);
1185 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1187 TRACE("failed (%ld)\n",GetLastError());
1188 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1192 memset(buf,0,sizeof(struct _stati64));
1194 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1195 Bon 011120: This FIXME seems incorrect
1196 Also a letter as first char isn't enough to be classify
1199 if (isalpha(*path)&& (*(path+1)==':'))
1200 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1202 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1204 plen = strlen(path);
1206 /* Dir, or regular file? */
1207 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1208 (path[plen-1] == '\\'))
1209 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1214 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1216 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1217 (tolower(path[plen-3]) << 16);
1218 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1219 mode |= MSVCRT_S_IEXEC;
1223 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1224 mode |= MSVCRT_S_IWRITE;
1226 buf->st_mode = mode;
1228 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1229 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1231 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1232 buf->st_mtime = buf->st_ctime = dw;
1233 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1234 (long)(buf->st_size >> 32),(long)buf->st_size,
1235 buf->st_atime,buf->st_mtime, buf->st_ctime);
1239 /*********************************************************************
1242 int MSVCRT__stat(const char* path, struct _stat * buf)
1244 struct _stati64 bufi64;
1246 ret = _stati64( path, &bufi64);
1248 msvcrt_cp_from_stati64(&bufi64, buf);
1252 /*********************************************************************
1255 int _wstat(const WCHAR* path, struct _stat * buf)
1258 WIN32_FILE_ATTRIBUTE_DATA hfi;
1259 unsigned short mode = MSVCRT_S_IREAD;
1262 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1264 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1266 TRACE("failed (%ld)\n",GetLastError());
1267 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1271 memset(buf,0,sizeof(struct _stat));
1273 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1274 if (MSVCRT_iswalpha(*path))
1275 buf->st_dev = buf->st_rdev = toupperW(*path - (WCHAR)L'A'); /* drive num */
1277 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1279 plen = strlenW(path);
1281 /* Dir, or regular file? */
1282 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1283 (path[plen-1] == (WCHAR)L'\\'))
1284 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1289 if (plen > 6 && path[plen-4] == (WCHAR)L'.') /* shortest exe: "\x.exe" */
1291 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1292 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1293 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1294 mode |= MSVCRT_S_IEXEC;
1298 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1299 mode |= MSVCRT_S_IWRITE;
1301 buf->st_mode = mode;
1303 buf->st_size = hfi.nFileSizeLow;
1304 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1306 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1307 buf->st_mtime = buf->st_ctime = dw;
1308 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1309 buf->st_atime,buf->st_mtime, buf->st_ctime);
1313 /*********************************************************************
1318 return _lseek(fd, 0, SEEK_CUR);
1321 /*********************************************************************
1322 * _tempnam (MSVCRT.@)
1324 char *_tempnam(const char *dir, const char *prefix)
1326 char tmpbuf[MAX_PATH];
1328 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1329 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1331 TRACE("got name (%s)\n",tmpbuf);
1332 return _strdup(tmpbuf);
1334 TRACE("failed (%ld)\n",GetLastError());
1338 /*********************************************************************
1339 * _wtempnam (MSVCRT.@)
1341 WCHAR *_wtempnam(const WCHAR *dir, const WCHAR *prefix)
1343 WCHAR tmpbuf[MAX_PATH];
1345 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1346 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1348 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1349 return _wcsdup(tmpbuf);
1351 TRACE("failed (%ld)\n",GetLastError());
1355 /*********************************************************************
1358 int _umask(int umask)
1360 int old_umask = MSVCRT_umask;
1361 TRACE("(%d)\n",umask);
1362 MSVCRT_umask = umask;
1366 /*********************************************************************
1369 int _utime(const char* path, struct _utimbuf *t)
1371 int fd = _open(path, _O_WRONLY | _O_BINARY);
1375 int retVal = _futime(fd, t);
1382 /*********************************************************************
1383 * _wutime (MSVCRT.@)
1385 int _wutime(const WCHAR* path, struct _utimbuf *t)
1387 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1391 int retVal = _futime(fd, t);
1398 /*********************************************************************
1401 int _write(int fd, const void* buf, unsigned int count)
1404 HANDLE hand = msvcrt_fdtoh(fd);
1406 /* Dont trace small writes, it gets *very* annoying */
1409 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1411 if (hand == INVALID_HANDLE_VALUE)
1414 /* If appending, go to EOF */
1415 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1416 _lseek(fd, 0, FILE_END);
1418 if (WriteFile(hand, buf, count, &num_written, NULL)
1419 && (num_written == count))
1422 TRACE(":failed-last error (%ld)\n",GetLastError());
1423 if (MSVCRT_files[fd])
1424 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1429 /*********************************************************************
1432 int _putw(int val, MSVCRT_FILE* file)
1434 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1437 /*********************************************************************
1438 * clearerr (MSVCRT.@)
1440 void MSVCRT_clearerr(MSVCRT_FILE* file)
1442 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1443 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1446 /*********************************************************************
1449 int MSVCRT_fclose(MSVCRT_FILE* file)
1452 r=_close(file->_file);
1453 return ((r==MSVCRT_EOF) || (file->_flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1456 /*********************************************************************
1459 int MSVCRT_feof(MSVCRT_FILE* file)
1461 return file->_flag & MSVCRT__IOEOF;
1464 /*********************************************************************
1467 int MSVCRT_ferror(MSVCRT_FILE* file)
1469 return file->_flag & MSVCRT__IOERR;
1472 /*********************************************************************
1475 int MSVCRT_fflush(MSVCRT_FILE* file)
1481 int res=msvcrt_flush_buffer(file);
1486 /*********************************************************************
1489 int MSVCRT_fgetc(MSVCRT_FILE* file)
1493 return *(unsigned char *)file->_ptr++;
1495 return _filbuf(file);
1499 /*********************************************************************
1500 * _fgetchar (MSVCRT.@)
1504 return MSVCRT_fgetc(MSVCRT_stdin);
1507 /*********************************************************************
1508 * _filbuf (MSVCRT.@)
1510 int _filbuf(MSVCRT_FILE* file)
1513 /* Allocate buffer if needed */
1514 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1515 msvcrt_alloc_buffer(file);
1517 if(!(file->_flag & MSVCRT__IOREAD)) {
1518 if(file->_flag & MSVCRT__IORW) {
1519 file->_flag |= MSVCRT__IOREAD;
1524 if(file->_flag & MSVCRT__IONBF) {
1526 if (_read(file->_file,&c,1) != 1) {
1527 file->_flag |= MSVCRT__IOEOF;
1532 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1533 if(file->_cnt<0) file->_cnt = 0;
1535 file->_flag |= MSVCRT__IOEOF;
1539 file->_ptr = file->_base+1;
1540 return *(unsigned char *)file->_base;
1544 /*********************************************************************
1545 * fgetpos (MSVCRT.@)
1547 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1549 *pos = MSVCRT_ftell(file);
1550 return (*pos == -1? -1 : 0);
1553 /*********************************************************************
1556 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1559 char * buf_start = s;
1561 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1562 file,file->_file,s,size);
1564 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1565 cc = MSVCRT_fgetc(file))
1568 if (--size <= 0) break;
1571 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1573 TRACE(":nothing read\n");
1580 TRACE(":got '%s'\n", buf_start);
1584 /*********************************************************************
1587 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1590 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1595 /*********************************************************************
1598 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1600 return MSVCRT_fgetwc(file);
1603 /*********************************************************************
1604 * _fgetwchar (MSVCRT.@)
1606 MSVCRT_wint_t _fgetwchar(void)
1608 return MSVCRT_fgetwc(MSVCRT_stdin);
1611 /*********************************************************************
1612 * getwchar (MSVCRT.@)
1614 MSVCRT_wint_t MSVCRT_getwchar(void)
1616 return _fgetwchar();
1619 /*********************************************************************
1622 WCHAR *MSVCRT_fgetws(WCHAR *s, int size, MSVCRT_FILE* file)
1625 WCHAR * buf_start = s;
1627 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1628 file,file->_file,s,size);
1630 for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n';
1631 cc = MSVCRT_fgetwc(file))
1634 if (--size <= 0) break;
1637 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1639 TRACE(":nothing read\n");
1646 /* TRACE(":got '%s'\n", buf_start); */
1651 /*********************************************************************
1654 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1657 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
1662 /*********************************************************************
1663 * _fputwchar (MSVCRT.@)
1665 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1667 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1670 /*********************************************************************
1673 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1676 int flags = 0, plus = 0, fd;
1677 const char* search = mode;
1679 TRACE("(%s,%s)\n",path,mode);
1682 if (*search++ == '+')
1685 /* map mode string to open() flags. "man fopen" for possibilities. */
1689 flags = (plus ? _O_RDWR : _O_RDONLY);
1692 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1695 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1710 flags &= ~_O_BINARY;
1715 FIXME(":unknown flag %c not supported\n",mode[-1]);
1718 fd = _open(path, flags);
1723 file = msvcrt_alloc_fp(fd);
1724 TRACE(":got (%p)\n",file);
1731 /*********************************************************************
1732 * _wfopen (MSVCRT.@)
1734 MSVCRT_FILE *_wfopen(const WCHAR *path, const WCHAR *mode)
1736 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1737 char *patha = MSVCRT_calloc(plen + 1, 1);
1738 char *modea = MSVCRT_calloc(mlen + 1, 1);
1740 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1742 if (patha && modea &&
1743 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1744 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1746 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1752 MSVCRT__set_errno(GetLastError());
1756 /*********************************************************************
1757 * _fsopen (MSVCRT.@)
1759 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1761 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1762 return MSVCRT_fopen(path,mode);
1765 /*********************************************************************
1766 * _wfsopen (MSVCRT.@)
1768 MSVCRT_FILE* _wfsopen(const WCHAR *path, const WCHAR *mode, int share)
1770 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1771 debugstr_w(path),debugstr_w(mode),share);
1772 return _wfopen(path,mode);
1775 /*********************************************************************
1778 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1785 return _flsbuf(c, file);
1789 /*********************************************************************
1790 * _flsbuf (MSVCRT.@)
1792 int _flsbuf(int c, MSVCRT_FILE* file)
1794 /* Flush output buffer */
1795 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
1796 msvcrt_alloc_buffer(file);
1798 if(!(file->_flag & MSVCRT__IOWRT)) {
1799 if(file->_flag & MSVCRT__IORW) {
1800 file->_flag |= MSVCRT__IOWRT;
1806 int res=msvcrt_flush_buffer(file);
1807 return res?res : MSVCRT_fputc(c, file);
1810 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
1814 /*********************************************************************
1815 * _fputchar (MSVCRT.@)
1817 int _fputchar(int c)
1819 return MSVCRT_fputc(c, MSVCRT_stdout);
1822 /*********************************************************************
1825 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1826 { MSVCRT_size_t rcnt=size * nmemb;
1827 MSVCRT_size_t read=0;
1829 /* first buffered data */
1831 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
1832 memcpy(ptr, file->_ptr, pcnt);
1837 ptr = (char*)ptr + pcnt;
1838 } else if(!(file->_flag & MSVCRT__IOREAD )) {
1839 if(file->_flag & MSVCRT__IORW) {
1840 file->_flag |= MSVCRT__IOREAD;
1844 if(rcnt) pread = _read(file->_file,ptr, rcnt);
1845 if (MSVCRT_flags[file->_file] & MSVCRT__IOEOF)
1846 /* expose feof condition in the flags
1847 MFC tests file->_flag for feof, and doesn't not call feof())
1849 file->_flag |= MSVCRT__IOEOF;
1856 /*********************************************************************
1857 * freopen (MSVCRT.@)
1860 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
1862 MSVCRT_FILE* newfile;
1865 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1866 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
1872 FIXME(":reopen on user file not implemented!\n");
1873 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1876 if(MSVCRT_fclose(file))
1878 return MSVCRT_fopen(path, mode);
1881 /* first, create the new file */
1882 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
1885 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1886 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1887 MSVCRT_handles[newfile->_file]))
1889 /* Redirecting std handle to file , copy over.. */
1890 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
1891 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
1892 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
1893 MSVCRT__iob[fd]._file = fd;
1894 /* And free up the resources allocated by fopen, but
1895 * not the HANDLE we copied. */
1896 MSVCRT_free(MSVCRT_files[fd]);
1897 msvcrt_free_fd(newfile->_file);
1898 return &MSVCRT__iob[fd];
1901 WARN(":failed-last error (%ld)\n",GetLastError());
1902 MSVCRT_fclose(newfile);
1903 MSVCRT__set_errno(GetLastError());
1907 /*********************************************************************
1908 * fsetpos (MSVCRT.@)
1910 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1912 return _lseek(file->_file,*pos,SEEK_SET);
1915 /*********************************************************************
1918 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
1920 /* Flush output if needed */
1921 if(file->_flag & MSVCRT__IOWRT)
1922 msvcrt_flush_buffer(file);
1924 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1925 offset -= file->_cnt;
1927 /* Discard buffered input */
1929 file->_ptr = file->_base;
1930 /* Reset direction of i/o */
1931 if(file->_flag & MSVCRT__IORW) {
1932 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1934 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
1937 /*********************************************************************
1940 LONG MSVCRT_ftell(MSVCRT_FILE* file)
1945 if( file->_flag & MSVCRT__IOWRT ) {
1946 off = file->_ptr - file->_base;
1951 pos = _tell(file->_file);
1952 if(pos == -1) return pos;
1956 /*********************************************************************
1959 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1961 MSVCRT_size_t wrcnt=size * nmemb;
1966 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
1967 memcpy(file->_ptr, ptr, pcnt);
1972 ptr = (char*)ptr + pcnt;
1973 } else if(!(file->_flag & MSVCRT__IOWRT)) {
1974 if(file->_flag & MSVCRT__IORW) {
1975 file->_flag |= MSVCRT__IOWRT;
1981 int res=msvcrt_flush_buffer(file);
1983 int pwritten = _write(file->_file, ptr, wrcnt);
1984 if (pwritten <= 0) pwritten=0;
1985 written += pwritten;
1988 return written / size;
1991 /*********************************************************************
1994 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
1996 size_t len = strlen(s);
1997 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2000 /*********************************************************************
2003 int MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
2005 size_t len = strlenW(s);
2006 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2009 /*********************************************************************
2010 * getchar (MSVCRT.@)
2012 int MSVCRT_getchar(void)
2014 return MSVCRT_fgetc(MSVCRT_stdin);
2017 /*********************************************************************
2020 int MSVCRT_getc(MSVCRT_FILE* file)
2022 return MSVCRT_fgetc(file);
2025 /*********************************************************************
2028 char *MSVCRT_gets(char *buf)
2031 char * buf_start = buf;
2033 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2034 cc = MSVCRT_fgetc(MSVCRT_stdin))
2035 if(cc != '\r') *buf++ = (char)cc;
2039 TRACE("got '%s'\n", buf_start);
2043 /*********************************************************************
2046 WCHAR* MSVCRT__getws(WCHAR* buf)
2051 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2052 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2059 TRACE("got '%s'\n", debugstr_w(ws));
2063 /*********************************************************************
2066 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2068 return MSVCRT_fputc(c, file);
2071 /*********************************************************************
2072 * putchar (MSVCRT.@)
2074 int MSVCRT_putchar(int c)
2076 return MSVCRT_fputc(c, MSVCRT_stdout);
2079 /*********************************************************************
2082 int MSVCRT_puts(const char *s)
2084 size_t len = strlen(s);
2085 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2086 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2089 /*********************************************************************
2092 int _putws(const WCHAR *s)
2094 static const WCHAR nl = '\n';
2095 size_t len = strlenW(s);
2096 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2097 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2100 /*********************************************************************
2103 int MSVCRT_remove(const char *path)
2105 TRACE("(%s)\n",path);
2106 if (DeleteFileA(path))
2108 TRACE(":failed (%ld)\n",GetLastError());
2109 MSVCRT__set_errno(GetLastError());
2113 /*********************************************************************
2114 * _wremove (MSVCRT.@)
2116 int _wremove(const WCHAR *path)
2118 TRACE("(%s)\n",debugstr_w(path));
2119 if (DeleteFileW(path))
2121 TRACE(":failed (%ld)\n",GetLastError());
2122 MSVCRT__set_errno(GetLastError());
2126 /*********************************************************************
2129 int MSVCRT_scanf(const char *format, ...)
2134 va_start(valist, format);
2135 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
2140 /*********************************************************************
2143 int MSVCRT_wscanf(const WCHAR *format, ...)
2148 va_start(valist, format);
2149 res = MSVCRT_fwscanf(MSVCRT_stdin, format, valist);
2154 /*********************************************************************
2157 int MSVCRT_rename(const char *oldpath,const char *newpath)
2159 TRACE(":from %s to %s\n",oldpath,newpath);
2160 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2162 TRACE(":failed (%ld)\n",GetLastError());
2163 MSVCRT__set_errno(GetLastError());
2167 /*********************************************************************
2168 * _wrename (MSVCRT.@)
2170 int _wrename(const WCHAR *oldpath,const WCHAR *newpath)
2172 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2173 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2175 TRACE(":failed (%ld)\n",GetLastError());
2176 MSVCRT__set_errno(GetLastError());
2180 /*********************************************************************
2181 * setvbuf (MSVCRT.@)
2183 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2185 /* TODO: Check if file busy */
2187 MSVCRT_free(file->_base);
2191 if(mode == MSVCRT__IOFBF) {
2192 file->_flag &= ~MSVCRT__IONBF;
2193 file->_base = file->_ptr = buf;
2195 file->_bufsiz = size;
2198 file->_flag |= MSVCRT__IONBF;
2203 /*********************************************************************
2206 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2208 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2211 /*********************************************************************
2214 char *MSVCRT_tmpnam(char *s)
2216 char tmpbuf[MAX_PATH];
2217 char* prefix = "TMP";
2218 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
2219 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
2221 TRACE(":failed-last error (%ld)\n",GetLastError());
2224 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
2229 /*********************************************************************
2230 * tmpfile (MSVCRT.@)
2232 MSVCRT_FILE* MSVCRT_tmpfile(void)
2234 char *filename = MSVCRT_tmpnam(NULL);
2236 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2238 return msvcrt_alloc_fp(fd);
2242 /*********************************************************************
2243 * vfprintf (MSVCRT.@)
2245 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2247 char buf[2048], *mem = buf;
2248 int written, resize = sizeof(buf), retval;
2249 /* There are two conventions for vsnprintf failing:
2250 * Return -1 if we truncated, or
2251 * Return the number of bytes that would have been written
2252 * The code below handles both cases
2254 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2257 resize = (written == -1 ? resize * 2 : written + 1);
2260 if (!(mem = (char *)MSVCRT_malloc(resize)))
2263 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2269 /*********************************************************************
2270 * vfwprintf (MSVCRT.@)
2272 * Is final char included in written (then resize is too big) or not
2273 * (then we must test for equality too)?
2275 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
2277 WCHAR buf[2048], *mem = buf;
2278 int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
2279 /* See vfprintf comments */
2280 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2283 resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
2286 if (!(mem = (WCHAR *)MSVCRT_malloc(resize*sizeof(*mem))))
2289 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2295 /*********************************************************************
2296 * vprintf (MSVCRT.@)
2298 int MSVCRT_vprintf(const char *format, va_list valist)
2300 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2303 /*********************************************************************
2304 * vwprintf (MSVCRT.@)
2306 int MSVCRT_vwprintf(const WCHAR *format, va_list valist)
2308 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2311 /*********************************************************************
2312 * fprintf (MSVCRT.@)
2314 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2318 va_start(valist, format);
2319 res = MSVCRT_vfprintf(file, format, valist);
2324 /*********************************************************************
2325 * fwprintf (MSVCRT.@)
2327 int MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
2331 va_start(valist, format);
2332 res = MSVCRT_vfwprintf(file, format, valist);
2337 /*********************************************************************
2340 int MSVCRT_printf(const char *format, ...)
2344 va_start(valist, format);
2345 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2350 /*********************************************************************
2353 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2355 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2356 msvcrt_alloc_buffer(file);
2359 if(file->_ptr>file->_base) {
2368 /*********************************************************************
2369 * ungetwc (MSVCRT.@)
2371 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2374 char * pp = (char *)&mwc;
2376 for(i=sizeof(WCHAR)-1;i>=0;i--) {
2377 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2383 /*********************************************************************
2384 * wprintf (MSVCRT.@)
2386 int MSVCRT_wprintf(const WCHAR *format, ...)
2390 va_start(valist, format);
2391 res = MSVCRT_vwprintf(format, valist);