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)
100 /* INTERNAL: Get the HANDLE for a fd */
101 static HANDLE msvcrt_fdtoh(int fd)
103 if (fd < 0 || fd >= MSVCRT_fdend ||
104 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
106 WARN(":fd (%d) - no handle!\n",fd);
108 *MSVCRT__errno() = MSVCRT_EBADF;
109 return INVALID_HANDLE_VALUE;
111 return MSVCRT_handles[fd];
114 /* INTERNAL: free a file entry fd */
115 static void msvcrt_free_fd(int fd)
117 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
118 MSVCRT_files[fd] = 0;
119 MSVCRT_flags[fd] = 0;
120 TRACE(":fd (%d) freed\n",fd);
122 return; /* dont use 0,1,2 for user files */
123 if (fd == MSVCRT_fdend - 1)
125 if (fd < MSVCRT_fdstart)
129 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
130 static int msvcrt_alloc_fd(HANDLE hand, int flag)
132 int fd = MSVCRT_fdstart;
134 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
135 if (fd >= MSVCRT_MAX_FILES)
137 WARN(":files exhausted!\n");
140 MSVCRT_handles[fd] = hand;
141 MSVCRT_flags[fd] = flag;
143 /* locate next free slot */
144 if (fd == MSVCRT_fdend)
145 MSVCRT_fdstart = ++MSVCRT_fdend;
147 while(MSVCRT_fdstart < MSVCRT_fdend &&
148 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
154 /* INTERNAL: Allocate a FILE* for an fd slot
155 * This is done lazily to avoid memory wastage for low level open/write
156 * usage when a FILE* is not requested (but may be later).
158 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
160 TRACE(":fd (%d) allocating FILE*\n",fd);
161 if (fd < 0 || fd >= MSVCRT_fdend ||
162 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
164 WARN(":invalid fd %d\n",fd);
166 *MSVCRT__errno() = MSVCRT_EBADF;
169 if (!MSVCRT_files[fd])
171 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
173 MSVCRT_files[fd]->_file = fd;
174 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
175 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
178 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
179 return MSVCRT_files[fd];
183 /* INTERNAL: Set up stdin, stderr and stdout */
184 void msvcrt_init_io(void)
187 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
188 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
189 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
190 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
191 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
192 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
193 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
195 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles[0],
196 MSVCRT_handles[1],MSVCRT_handles[2]);
198 for (i = 0; i < 3; i++)
200 /* FILE structs for stdin/out/err are static and never deleted */
201 MSVCRT_files[i] = &MSVCRT__iob[i];
202 MSVCRT__iob[i]._file = i;
203 MSVCRT_tempfiles[i] = NULL;
207 /* free everything on process exit */
208 void msvcrt_free_io(void)
216 /* INTERNAL: Flush stdio file buffer */
217 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
220 int cnt=file->_ptr-file->_base;
221 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
224 file->_ptr=file->_base;
225 file->_cnt=file->_bufsiz;
230 /* INTERNAL: Allocate stdio file buffer */
231 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
233 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
235 file->_bufsiz = MSVCRT_BUFSIZ;
236 file->_flag |= MSVCRT__IOMYBUF;
238 file->_base = (unsigned char *)(&file->_charbuf);
240 file->_bufsiz = sizeof(file->_charbuf);
242 file->_ptr = file->_base;
246 /*********************************************************************
249 MSVCRT_FILE *__p__iob(void)
251 return &MSVCRT__iob[0];
254 /*********************************************************************
257 int _access(const char *filename, int mode)
259 DWORD attr = GetFileAttributesA(filename);
261 TRACE("(%s,%d) %ld\n",filename,mode,attr);
263 if (!filename || attr == 0xffffffff)
265 MSVCRT__set_errno(GetLastError());
268 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
270 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
276 /*********************************************************************
277 * _waccess (MSVCRT.@)
279 int _waccess(const WCHAR *filename, int mode)
281 DWORD attr = GetFileAttributesW(filename);
283 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
285 if (!filename || attr == 0xffffffff)
287 MSVCRT__set_errno(GetLastError());
290 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
292 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
298 /*********************************************************************
301 int _chmod(const char *path, int flags)
303 DWORD oldFlags = GetFileAttributesA(path);
305 if (oldFlags != 0x0FFFFFFFF)
307 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
308 oldFlags | FILE_ATTRIBUTE_READONLY;
310 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
313 MSVCRT__set_errno(GetLastError());
317 /*********************************************************************
320 int _wchmod(const WCHAR *path, int flags)
322 DWORD oldFlags = GetFileAttributesW(path);
324 if (oldFlags != 0x0FFFFFFFF)
326 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
327 oldFlags | FILE_ATTRIBUTE_READONLY;
329 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
332 MSVCRT__set_errno(GetLastError());
336 /*********************************************************************
339 int _unlink(const char *path)
341 TRACE("(%s)\n",path);
342 if(DeleteFileA(path))
344 TRACE("failed (%ld)\n",GetLastError());
345 MSVCRT__set_errno(GetLastError());
349 /*********************************************************************
350 * _wunlink (MSVCRT.@)
352 int _wunlink(const WCHAR *path)
354 TRACE("(%s)\n",debugstr_w(path));
355 if(DeleteFileW(path))
357 TRACE("failed (%ld)\n",GetLastError());
358 MSVCRT__set_errno(GetLastError());
362 /*********************************************************************
367 HANDLE hand = msvcrt_fdtoh(fd);
369 TRACE(":fd (%d) handle (%p)\n",fd,hand);
370 if (hand == INVALID_HANDLE_VALUE)
372 /* flush stdio buffers */
373 if(MSVCRT_files[fd]) {
374 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
375 MSVCRT_fflush(MSVCRT_files[fd]);
377 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
378 MSVCRT_free(MSVCRT_files[fd]->_base);
381 /* Dont free std FILE*'s, they are not dynamic */
382 if (fd > 2 && MSVCRT_files[fd])
383 MSVCRT_free(MSVCRT_files[fd]);
387 if (!CloseHandle(hand))
389 WARN(":failed-last error (%ld)\n",GetLastError());
390 MSVCRT__set_errno(GetLastError());
393 if (MSVCRT_tempfiles[fd])
395 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
396 _unlink(MSVCRT_tempfiles[fd]);
397 MSVCRT_free(MSVCRT_tempfiles[fd]);
398 MSVCRT_tempfiles[fd] = NULL;
405 /*********************************************************************
410 HANDLE hand = msvcrt_fdtoh(fd);
412 TRACE(":fd (%d) handle (%p)\n",fd,hand);
413 if (hand == INVALID_HANDLE_VALUE)
416 if (!FlushFileBuffers(hand))
418 if (GetLastError() == ERROR_INVALID_HANDLE)
420 /* FlushFileBuffers fails for console handles
421 * so we ignore this error.
425 TRACE(":failed-last error (%ld)\n",GetLastError());
426 MSVCRT__set_errno(GetLastError());
433 /*********************************************************************
439 HANDLE hand = msvcrt_fdtoh(fd);
441 TRACE(":fd (%d) handle (%p)\n",fd,hand);
443 if (hand == INVALID_HANDLE_VALUE)
446 /* If we have a FILE* for this file, the EOF flag
447 * will be set by the read()/write() functions.
449 if (MSVCRT_files[fd])
450 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
452 /* Otherwise we do it the hard way */
453 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
454 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
456 if (curpos == endpos)
459 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
463 /*********************************************************************
464 * _fcloseall (MSVCRT.@)
468 int num_closed = 0, i;
470 for (i = 3; i < MSVCRT_fdend; i++)
471 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
477 TRACE(":closed (%d) handles\n",num_closed);
481 /*********************************************************************
484 LONG _lseek(int fd, LONG offset, int whence)
487 HANDLE hand = msvcrt_fdtoh(fd);
489 TRACE(":fd (%d) handle (%p)\n",fd,hand);
490 if (hand == INVALID_HANDLE_VALUE)
493 if (whence < 0 || whence > 2)
495 *MSVCRT__errno() = MSVCRT_EINVAL;
499 TRACE(":fd (%d) to 0x%08lx pos %s\n",
500 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
501 (whence==SEEK_CUR)?"SEEK_CUR":
502 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
504 if ((ret = SetFilePointer(hand, offset, NULL, whence)) != 0xffffffff)
506 if (MSVCRT_files[fd])
507 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
508 /* FIXME: What if we seek _to_ EOF - is EOF set? */
511 TRACE(":error-last error (%ld)\n",GetLastError());
512 if (MSVCRT_files[fd])
513 switch(GetLastError())
515 case ERROR_NEGATIVE_SEEK:
516 case ERROR_SEEK_ON_DEVICE:
517 MSVCRT__set_errno(GetLastError());
518 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
526 /*********************************************************************
527 * _locking (MSVCRT.@)
529 * This is untested; the underlying LockFile doesn't work yet.
531 int _locking(int fd, int mode, LONG nbytes)
535 HANDLE hand = msvcrt_fdtoh(fd);
537 TRACE(":fd (%d) handle (%p)\n",fd,hand);
538 if (hand == INVALID_HANDLE_VALUE)
541 if (mode < 0 || mode > 4)
543 *MSVCRT__errno() = MSVCRT_EINVAL;
547 TRACE(":fd (%d) by 0x%08lx mode %s\n",
548 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
549 (mode==_LK_LOCK)?"_LK_LOCK":
550 (mode==_LK_NBLCK)?"_LK_NBLCK":
551 (mode==_LK_RLCK)?"_LK_RLCK":
552 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
555 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == 0xffffffff)
557 FIXME ("Seek failed\n");
558 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
561 if (mode == _LK_LOCK || mode == _LK_RLCK)
564 ret = 1; /* just to satisfy gcc */
567 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
572 else if (mode == _LK_UNLCK)
573 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
575 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
576 /* FIXME - what about error settings? */
580 /*********************************************************************
583 void MSVCRT_rewind(MSVCRT_FILE* file)
585 TRACE(":file (%p) fd (%d)\n",file,file->_file);
586 MSVCRT_fseek(file, 0L, SEEK_SET);
587 MSVCRT_clearerr(file);
590 /*********************************************************************
593 MSVCRT_FILE* _fdopen(int fd, const char *mode)
595 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
597 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
604 /*********************************************************************
605 * _wfdopen (MSVCRT.@)
607 MSVCRT_FILE* _wfdopen(int fd, const WCHAR *mode)
609 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
611 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
618 /*********************************************************************
619 * _filelength (MSVCRT.@)
621 LONG _filelength(int fd)
623 LONG curPos = _lseek(fd, 0, SEEK_CUR);
626 LONG endPos = _lseek(fd, 0, SEEK_END);
629 if (endPos != curPos)
630 _lseek(fd, curPos, SEEK_SET);
637 /*********************************************************************
640 int _fileno(MSVCRT_FILE* file)
642 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
646 /*********************************************************************
647 * _flushall (MSVCRT.@)
651 int num_flushed = 0, i = 3;
653 while(i < MSVCRT_fdend)
654 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
657 /* FIXME: flush, do not commit */
658 if (_commit(i) == -1)
660 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
662 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
663 MSVCRT_fflush(MSVCRT_files[i]);
668 TRACE(":flushed (%d) handles\n",num_flushed);
672 /*********************************************************************
675 int MSVCRT__fstat(int fd, struct _stat* buf)
678 BY_HANDLE_FILE_INFORMATION hfi;
679 HANDLE hand = msvcrt_fdtoh(fd);
681 TRACE(":fd (%d) stat (%p)\n",fd,buf);
682 if (hand == INVALID_HANDLE_VALUE)
687 WARN(":failed-NULL buf\n");
688 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
692 memset(&hfi, 0, sizeof(hfi));
693 memset(buf, 0, sizeof(struct _stat));
694 if (!GetFileInformationByHandle(hand, &hfi))
696 WARN(":failed-last error (%ld)\n",GetLastError());
697 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
700 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
701 buf->st_nlink = hfi.nNumberOfLinks;
702 buf->st_size = hfi.nFileSizeLow;
703 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
705 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
706 buf->st_mtime = buf->st_ctime = dw;
710 /*********************************************************************
713 int _futime(int fd, struct _utimbuf *t)
715 HANDLE hand = msvcrt_fdtoh(fd);
720 MSVCRT_time_t currTime;
721 MSVCRT_time(&currTime);
722 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
723 memcpy(&wt, &at, sizeof(wt));
727 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
728 if (t->actime == t->modtime)
729 memcpy(&wt, &at, sizeof(wt));
731 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
734 if (!SetFileTime(hand, NULL, &at, &wt))
736 MSVCRT__set_errno(GetLastError());
742 /*********************************************************************
743 * _get_osfhandle (MSVCRT.@)
745 long _get_osfhandle(int fd)
747 HANDLE hand = msvcrt_fdtoh(fd);
748 HANDLE newhand = hand;
749 TRACE(":fd (%d) handle (%p)\n",fd,hand);
751 if (hand != INVALID_HANDLE_VALUE)
753 /* FIXME: I'm not convinced that I should be copying the
754 * handle here - it may be leaked if the app doesn't
755 * close it (and the API docs dont say that it should)
756 * Not duplicating it means that it can't be inherited
757 * and so lcc's wedit doesn't cope when it passes it to
758 * child processes. I've an idea that it should either
759 * be copied by CreateProcess, or marked as inheritable
760 * when initialised, or maybe both? JG 21-9-00.
762 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
763 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
765 return (long)newhand;
768 /*********************************************************************
773 HANDLE hand = msvcrt_fdtoh(fd);
775 TRACE(":fd (%d) handle (%p)\n",fd,hand);
776 if (hand == INVALID_HANDLE_VALUE)
779 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
782 /*********************************************************************
785 char *_mktemp(char *pattern)
788 char *retVal = pattern;
793 numX = (*pattern++ == 'X')? numX + 1 : 0;
797 id = GetCurrentProcessId();
801 int tempNum = id / 10;
802 *pattern-- = id - (tempNum * 10) + '0';
808 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
809 GetLastError() == ERROR_FILE_NOT_FOUND)
812 } while(letter != '|');
816 /*********************************************************************
817 * _wmktemp (MSVCRT.@)
819 WCHAR *_wmktemp(WCHAR *pattern)
822 WCHAR *retVal = pattern;
824 WCHAR letter = (WCHAR)L'a';
827 numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
831 id = GetCurrentProcessId();
835 int tempNum = id / 10;
836 *pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
842 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
843 GetLastError() == ERROR_FILE_NOT_FOUND)
846 } while(letter != (WCHAR)L'|');
850 /*********************************************************************
853 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
857 DWORD access = 0, creation = 0;
861 SECURITY_ATTRIBUTES sa;
864 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
865 path, oflags, shflags);
867 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
870 access |= GENERIC_READ;
871 ioflag |= MSVCRT__IOREAD;
874 access |= GENERIC_WRITE;
875 ioflag |= MSVCRT__IOWRT;
878 access |= GENERIC_WRITE | GENERIC_READ;
879 ioflag |= MSVCRT__IORW;
883 if (oflags & _O_CREAT)
885 va_start(ap, shflags);
886 pmode = va_arg(ap, int);
889 FIXME(": pmode 0x%04x ignored\n", pmode);
891 if (oflags & _O_EXCL)
892 creation = CREATE_NEW;
893 else if (oflags & _O_TRUNC)
894 creation = CREATE_ALWAYS;
896 creation = OPEN_ALWAYS;
898 else /* no _O_CREAT */
900 if (oflags & _O_TRUNC)
901 creation = TRUNCATE_EXISTING;
903 creation = OPEN_EXISTING;
905 if (oflags & _O_APPEND)
906 ioflag |= MSVCRT__IOAPPEND;
909 oflags |= _O_BINARY; /* FIXME: Default to text */
911 if (oflags & _O_TEXT)
913 /* Dont warn when writing */
914 if (ioflag & GENERIC_READ)
915 FIXME(":TEXT node not implemented\n");
925 sharing = FILE_SHARE_READ;
928 sharing = FILE_SHARE_WRITE;
931 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
934 ERR( "Unhandled shflags 0x%x\n", shflags );
938 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
939 |_O_CREAT|_O_RDWR|_O_TEMPORARY|_O_NOINHERIT))
940 TRACE(":unsupported oflags 0x%04x\n",oflags);
942 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
943 sa.lpSecurityDescriptor = NULL;
944 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
946 hand = CreateFileA(path, access, sharing,
947 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
949 if (hand == INVALID_HANDLE_VALUE) {
950 WARN(":failed-last error (%ld)\n",GetLastError());
951 MSVCRT__set_errno(GetLastError());
955 fd = msvcrt_alloc_fd(hand, ioflag);
957 TRACE(":fd (%d) handle (%p)\n",fd, hand);
961 if (oflags & _O_TEMPORARY)
962 MSVCRT_tempfiles[fd] = _strdup(path);
963 if (ioflag & MSVCRT__IOAPPEND)
964 _lseek(fd, 0, FILE_END);
970 /*********************************************************************
973 int MSVCRT__wsopen( const WCHAR* path, int oflags, int shflags, ... )
975 const unsigned int len = strlenW(path);
976 char *patha = MSVCRT_calloc(len + 1,1);
980 va_start(ap, shflags);
981 pmode = va_arg(ap, int);
984 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
986 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
991 MSVCRT__set_errno(GetLastError());
995 /*********************************************************************
998 int _open( const char *path, int flags, ... )
1003 va_start(ap, flags);
1004 pmode = va_arg(ap, int);
1007 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1010 /*********************************************************************
1013 int _wopen(const WCHAR *path,int flags,...)
1015 const unsigned int len = strlenW(path);
1016 char *patha = MSVCRT_calloc(len + 1,1);
1020 va_start(ap, flags);
1021 pmode = va_arg(ap, int);
1024 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1026 int retval = _open(patha,flags,pmode);
1031 MSVCRT__set_errno(GetLastError());
1035 /*********************************************************************
1038 int _creat(const char *path, int flags)
1040 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1041 return _open(path, usedFlags);
1044 /*********************************************************************
1045 * _wcreat (MSVCRT.@)
1047 int _wcreat(const WCHAR *path, int flags)
1049 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1050 return _wopen(path, usedFlags);
1053 /*********************************************************************
1054 * _open_osfhandle (MSVCRT.@)
1056 int _open_osfhandle(long hand, int flags)
1058 /* _O_RDONLY (0) always matches, so set the read flag*/
1059 /* FIXME: handle more flags */
1060 int fd= msvcrt_alloc_fd((HANDLE)hand,flags|MSVCRT__IOREAD);
1061 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd, flags |MSVCRT__IOREAD);
1065 /*********************************************************************
1070 int num_removed = 0, i;
1072 for (i = 3; i < MSVCRT_fdend; i++)
1073 if (MSVCRT_tempfiles[i])
1080 TRACE(":removed (%d) temp files\n",num_removed);
1084 /*********************************************************************
1087 int _read(int fd, void *buf, unsigned int count)
1090 HANDLE hand = msvcrt_fdtoh(fd);
1092 /* Dont trace small reads, it gets *very* annoying */
1094 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1095 if (hand == INVALID_HANDLE_VALUE)
1098 if (ReadFile(hand, buf, count, &num_read, NULL))
1100 if (num_read != count && MSVCRT_files[fd])
1103 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1105 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1110 TRACE(":failed-last error (%ld)\n",GetLastError());
1111 if (MSVCRT_files[fd])
1112 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1116 /*********************************************************************
1119 int _getw(MSVCRT_FILE* file)
1122 if (_read(file->_file, &i, sizeof(int)) != 1)
1127 /*********************************************************************
1128 * _setmode (MSVCRT.@)
1130 int _setmode(int fd,int mode)
1133 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
1137 /*********************************************************************
1140 int MSVCRT__stat(const char* path, struct _stat * buf)
1143 WIN32_FILE_ATTRIBUTE_DATA hfi;
1144 unsigned short mode = MSVCRT_S_IREAD;
1147 TRACE(":file (%s) buf(%p)\n",path,buf);
1149 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1151 TRACE("failed (%ld)\n",GetLastError());
1152 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1156 memset(buf,0,sizeof(struct _stat));
1158 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1159 Bon 011120: This FIXME seems incorrect
1160 Also a letter as first char isn't enough to be classify
1163 if (isalpha(*path)&& (*(path+1)==':'))
1164 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1166 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1168 plen = strlen(path);
1170 /* Dir, or regular file? */
1171 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1172 (path[plen-1] == '\\'))
1173 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1178 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1180 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1181 (tolower(path[plen-3]) << 16);
1182 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1183 mode |= MSVCRT_S_IEXEC;
1187 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1188 mode |= MSVCRT_S_IWRITE;
1190 buf->st_mode = mode;
1192 buf->st_size = hfi.nFileSizeLow;
1193 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1195 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1196 buf->st_mtime = buf->st_ctime = dw;
1197 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1198 buf->st_atime,buf->st_mtime, buf->st_ctime);
1202 /*********************************************************************
1205 int _wstat(const WCHAR* path, struct _stat * buf)
1208 WIN32_FILE_ATTRIBUTE_DATA hfi;
1209 unsigned short mode = MSVCRT_S_IREAD;
1212 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1214 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1216 TRACE("failed (%ld)\n",GetLastError());
1217 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1221 memset(buf,0,sizeof(struct _stat));
1223 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1224 if (MSVCRT_iswalpha(*path))
1225 buf->st_dev = buf->st_rdev = toupperW(*path - (WCHAR)L'A'); /* drive num */
1227 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1229 plen = strlenW(path);
1231 /* Dir, or regular file? */
1232 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1233 (path[plen-1] == (WCHAR)L'\\'))
1234 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1239 if (plen > 6 && path[plen-4] == (WCHAR)L'.') /* shortest exe: "\x.exe" */
1241 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1242 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1243 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1244 mode |= MSVCRT_S_IEXEC;
1248 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1249 mode |= MSVCRT_S_IWRITE;
1251 buf->st_mode = mode;
1253 buf->st_size = hfi.nFileSizeLow;
1254 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1256 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1257 buf->st_mtime = buf->st_ctime = dw;
1258 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1259 buf->st_atime,buf->st_mtime, buf->st_ctime);
1263 /*********************************************************************
1268 return _lseek(fd, 0, SEEK_CUR);
1271 /*********************************************************************
1272 * _tempnam (MSVCRT.@)
1274 char *_tempnam(const char *dir, const char *prefix)
1276 char tmpbuf[MAX_PATH];
1278 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1279 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1281 TRACE("got name (%s)\n",tmpbuf);
1282 return _strdup(tmpbuf);
1284 TRACE("failed (%ld)\n",GetLastError());
1288 /*********************************************************************
1289 * _wtempnam (MSVCRT.@)
1291 WCHAR *_wtempnam(const WCHAR *dir, const WCHAR *prefix)
1293 WCHAR tmpbuf[MAX_PATH];
1295 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1296 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1298 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1299 return _wcsdup(tmpbuf);
1301 TRACE("failed (%ld)\n",GetLastError());
1305 /*********************************************************************
1308 int _umask(int umask)
1310 int old_umask = MSVCRT_umask;
1311 TRACE("(%d)\n",umask);
1312 MSVCRT_umask = umask;
1316 /*********************************************************************
1319 int _utime(const char* path, struct _utimbuf *t)
1321 int fd = _open(path, _O_WRONLY | _O_BINARY);
1325 int retVal = _futime(fd, t);
1332 /*********************************************************************
1333 * _wutime (MSVCRT.@)
1335 int _wutime(const WCHAR* path, struct _utimbuf *t)
1337 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1341 int retVal = _futime(fd, t);
1348 /*********************************************************************
1351 int _write(int fd, const void* buf, unsigned int count)
1354 HANDLE hand = msvcrt_fdtoh(fd);
1356 /* Dont trace small writes, it gets *very* annoying */
1359 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1361 if (hand == INVALID_HANDLE_VALUE)
1364 /* If appending, go to EOF */
1365 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1366 _lseek(fd, 0, FILE_END);
1368 if (WriteFile(hand, buf, count, &num_written, NULL)
1369 && (num_written == count))
1372 TRACE(":failed-last error (%ld)\n",GetLastError());
1373 if (MSVCRT_files[fd])
1374 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1379 /*********************************************************************
1382 int _putw(int val, MSVCRT_FILE* file)
1384 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1387 /*********************************************************************
1388 * clearerr (MSVCRT.@)
1390 void MSVCRT_clearerr(MSVCRT_FILE* file)
1392 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1393 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1396 /*********************************************************************
1399 int MSVCRT_fclose(MSVCRT_FILE* file)
1402 r=_close(file->_file);
1403 return ((r==MSVCRT_EOF) || (file->_flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1406 /*********************************************************************
1409 int MSVCRT_feof(MSVCRT_FILE* file)
1411 return file->_flag & MSVCRT__IOEOF;
1414 /*********************************************************************
1417 int MSVCRT_ferror(MSVCRT_FILE* file)
1419 return file->_flag & MSVCRT__IOERR;
1422 /*********************************************************************
1425 int MSVCRT_fflush(MSVCRT_FILE* file)
1431 int res=msvcrt_flush_buffer(file);
1436 /*********************************************************************
1439 int MSVCRT_fgetc(MSVCRT_FILE* file)
1443 return *(unsigned char *)file->_ptr++;
1445 return _filbuf(file);
1449 /*********************************************************************
1450 * _fgetchar (MSVCRT.@)
1454 return MSVCRT_fgetc(MSVCRT_stdin);
1457 /*********************************************************************
1458 * _filbuf (MSVCRT.@)
1460 int _filbuf(MSVCRT_FILE* file)
1463 /* Allocate buffer if needed */
1464 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1465 msvcrt_alloc_buffer(file);
1467 if(!(file->_flag & MSVCRT__IOREAD)) {
1468 if(file->_flag & MSVCRT__IORW) {
1469 file->_flag |= MSVCRT__IOREAD;
1474 if(file->_flag & MSVCRT__IONBF) {
1476 if (_read(file->_file,&c,1) != 1) {
1477 file->_flag |= MSVCRT__IOEOF;
1482 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1483 if(file->_cnt<0) file->_cnt = 0;
1485 file->_flag |= MSVCRT__IOEOF;
1489 file->_ptr = file->_base+1;
1490 return *(unsigned char *)file->_base;
1494 /*********************************************************************
1495 * fgetpos (MSVCRT.@)
1497 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1499 *pos = MSVCRT_ftell(file);
1500 return (*pos == -1? -1 : 0);
1503 /*********************************************************************
1506 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1509 char * buf_start = s;
1511 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1512 file,file->_file,s,size);
1514 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1515 cc = MSVCRT_fgetc(file))
1518 if (--size <= 0) break;
1521 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1523 TRACE(":nothing read\n");
1530 TRACE(":got '%s'\n", buf_start);
1534 /*********************************************************************
1537 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1540 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1545 /*********************************************************************
1548 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1550 return MSVCRT_fgetwc(file);
1553 /*********************************************************************
1554 * _fgetwchar (MSVCRT.@)
1556 MSVCRT_wint_t _fgetwchar(void)
1558 return MSVCRT_fgetwc(MSVCRT_stdin);
1561 /*********************************************************************
1562 * getwchar (MSVCRT.@)
1564 MSVCRT_wint_t MSVCRT_getwchar(void)
1566 return _fgetwchar();
1569 /*********************************************************************
1572 WCHAR *MSVCRT_fgetws(WCHAR *s, int size, MSVCRT_FILE* file)
1575 WCHAR * buf_start = s;
1577 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1578 file,file->_file,s,size);
1580 for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n';
1581 cc = MSVCRT_fgetwc(file))
1584 if (--size <= 0) break;
1587 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1589 TRACE(":nothing read\n");
1596 /* TRACE(":got '%s'\n", buf_start); */
1601 /*********************************************************************
1604 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1607 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
1612 /*********************************************************************
1613 * _fputwchar (MSVCRT.@)
1615 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1617 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1620 /*********************************************************************
1623 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1626 int flags = 0, plus = 0, fd;
1627 const char* search = mode;
1629 TRACE("(%s,%s)\n",path,mode);
1632 if (*search++ == '+')
1635 /* map mode string to open() flags. "man fopen" for possibilities. */
1639 flags = (plus ? _O_RDWR : _O_RDONLY);
1642 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1645 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1660 flags &= ~_O_BINARY;
1665 FIXME(":unknown flag %c not supported\n",mode[-1]);
1668 fd = _open(path, flags);
1673 file = msvcrt_alloc_fp(fd);
1674 TRACE(":got (%p)\n",file);
1681 /*********************************************************************
1682 * _wfopen (MSVCRT.@)
1684 MSVCRT_FILE *_wfopen(const WCHAR *path, const WCHAR *mode)
1686 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1687 char *patha = MSVCRT_calloc(plen + 1, 1);
1688 char *modea = MSVCRT_calloc(mlen + 1, 1);
1690 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1692 if (patha && modea &&
1693 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1694 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1696 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1702 MSVCRT__set_errno(GetLastError());
1706 /*********************************************************************
1707 * _fsopen (MSVCRT.@)
1709 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1711 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1712 return MSVCRT_fopen(path,mode);
1715 /*********************************************************************
1716 * _wfsopen (MSVCRT.@)
1718 MSVCRT_FILE* _wfsopen(const WCHAR *path, const WCHAR *mode, int share)
1720 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1721 debugstr_w(path),debugstr_w(mode),share);
1722 return _wfopen(path,mode);
1725 /*********************************************************************
1728 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1735 return _flsbuf(c, file);
1739 /*********************************************************************
1740 * _flsbuf (MSVCRT.@)
1742 int _flsbuf(int c, MSVCRT_FILE* file)
1744 /* Flush output buffer */
1745 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
1746 msvcrt_alloc_buffer(file);
1748 if(!(file->_flag & MSVCRT__IOWRT)) {
1749 if(file->_flag & MSVCRT__IORW) {
1750 file->_flag |= MSVCRT__IOWRT;
1756 int res=msvcrt_flush_buffer(file);
1757 return res?res : MSVCRT_fputc(c, file);
1760 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
1764 /*********************************************************************
1765 * _fputchar (MSVCRT.@)
1767 int _fputchar(int c)
1769 return MSVCRT_fputc(c, MSVCRT_stdout);
1772 /*********************************************************************
1775 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1776 { MSVCRT_size_t rcnt=size * nmemb;
1777 MSVCRT_size_t read=0;
1779 /* first buffered data */
1781 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
1782 memcpy(ptr, file->_ptr, pcnt);
1787 ptr = (char*)ptr + pcnt;
1788 } else if(!(file->_flag & MSVCRT__IOREAD )) {
1789 if(file->_flag & MSVCRT__IORW) {
1790 file->_flag |= MSVCRT__IOREAD;
1794 if(rcnt) pread = _read(file->_file,ptr, rcnt);
1795 if (MSVCRT_flags[file->_file] & MSVCRT__IOEOF)
1796 /* expose feof condition in the flags
1797 MFC tests file->_flag for feof, and doesn't not call feof())
1799 file->_flag |= MSVCRT__IOEOF;
1806 /*********************************************************************
1807 * freopen (MSVCRT.@)
1810 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
1812 MSVCRT_FILE* newfile;
1815 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1816 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
1822 FIXME(":reopen on user file not implemented!\n");
1823 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1826 if(MSVCRT_fclose(file))
1828 return MSVCRT_fopen(path, mode);
1831 /* first, create the new file */
1832 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
1835 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1836 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1837 MSVCRT_handles[newfile->_file]))
1839 /* Redirecting std handle to file , copy over.. */
1840 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
1841 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
1842 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
1843 MSVCRT__iob[fd]._file = fd;
1844 /* And free up the resources allocated by fopen, but
1845 * not the HANDLE we copied. */
1846 MSVCRT_free(MSVCRT_files[fd]);
1847 msvcrt_free_fd(newfile->_file);
1848 return &MSVCRT__iob[fd];
1851 WARN(":failed-last error (%ld)\n",GetLastError());
1852 MSVCRT_fclose(newfile);
1853 MSVCRT__set_errno(GetLastError());
1857 /*********************************************************************
1858 * fsetpos (MSVCRT.@)
1860 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1862 return _lseek(file->_file,*pos,SEEK_SET);
1865 /*********************************************************************
1868 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
1870 /* Flush output if needed */
1871 if(file->_flag & MSVCRT__IOWRT)
1872 msvcrt_flush_buffer(file);
1874 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1875 offset -= file->_cnt;
1877 /* Discard buffered input */
1879 file->_ptr = file->_base;
1880 /* Reset direction of i/o */
1881 if(file->_flag & MSVCRT__IORW) {
1882 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1884 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
1887 /*********************************************************************
1890 LONG MSVCRT_ftell(MSVCRT_FILE* file)
1895 if( file->_flag & MSVCRT__IOWRT ) {
1896 off = file->_ptr - file->_base;
1901 pos = _tell(file->_file);
1902 if(pos == -1) return pos;
1906 /*********************************************************************
1909 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1911 MSVCRT_size_t wrcnt=size * nmemb;
1916 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
1917 memcpy(file->_ptr, ptr, pcnt);
1922 ptr = (char*)ptr + pcnt;
1923 } else if(!(file->_flag & MSVCRT__IOWRT)) {
1924 if(file->_flag & MSVCRT__IORW) {
1925 file->_flag |= MSVCRT__IOWRT;
1931 int res=msvcrt_flush_buffer(file);
1933 int pwritten = _write(file->_file, ptr, wrcnt);
1934 if (pwritten <= 0) pwritten=0;
1935 written += pwritten;
1938 return written / size;
1941 /*********************************************************************
1944 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
1946 size_t len = strlen(s);
1947 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
1950 /*********************************************************************
1953 int MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
1955 size_t len = strlenW(s);
1956 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
1959 /*********************************************************************
1960 * getchar (MSVCRT.@)
1962 int MSVCRT_getchar(void)
1964 return MSVCRT_fgetc(MSVCRT_stdin);
1967 /*********************************************************************
1970 int MSVCRT_getc(MSVCRT_FILE* file)
1972 return MSVCRT_fgetc(file);
1975 /*********************************************************************
1978 char *MSVCRT_gets(char *buf)
1981 char * buf_start = buf;
1983 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
1984 cc = MSVCRT_fgetc(MSVCRT_stdin))
1985 if(cc != '\r') *buf++ = (char)cc;
1989 TRACE("got '%s'\n", buf_start);
1993 /*********************************************************************
1996 WCHAR* MSVCRT__getws(WCHAR* buf)
2001 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2002 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2009 TRACE("got '%s'\n", debugstr_w(ws));
2013 /*********************************************************************
2016 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2018 return MSVCRT_fputc(c, file);
2021 /*********************************************************************
2022 * putchar (MSVCRT.@)
2024 int MSVCRT_putchar(int c)
2026 return MSVCRT_fputc(c, MSVCRT_stdout);
2029 /*********************************************************************
2032 int MSVCRT_puts(const char *s)
2034 size_t len = strlen(s);
2035 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2036 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2039 /*********************************************************************
2042 int _putws(const WCHAR *s)
2044 static const WCHAR nl = '\n';
2045 size_t len = strlenW(s);
2046 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2047 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2050 /*********************************************************************
2053 int MSVCRT_remove(const char *path)
2055 TRACE("(%s)\n",path);
2056 if (DeleteFileA(path))
2058 TRACE(":failed (%ld)\n",GetLastError());
2059 MSVCRT__set_errno(GetLastError());
2063 /*********************************************************************
2064 * _wremove (MSVCRT.@)
2066 int _wremove(const WCHAR *path)
2068 TRACE("(%s)\n",debugstr_w(path));
2069 if (DeleteFileW(path))
2071 TRACE(":failed (%ld)\n",GetLastError());
2072 MSVCRT__set_errno(GetLastError());
2076 /*********************************************************************
2079 int MSVCRT_scanf(const char *format, ...)
2084 va_start(valist, format);
2085 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
2090 /*********************************************************************
2093 int MSVCRT_wscanf(const WCHAR *format, ...)
2098 va_start(valist, format);
2099 res = MSVCRT_fwscanf(MSVCRT_stdin, format, valist);
2104 /*********************************************************************
2107 int MSVCRT_rename(const char *oldpath,const char *newpath)
2109 TRACE(":from %s to %s\n",oldpath,newpath);
2110 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2112 TRACE(":failed (%ld)\n",GetLastError());
2113 MSVCRT__set_errno(GetLastError());
2117 /*********************************************************************
2118 * _wrename (MSVCRT.@)
2120 int _wrename(const WCHAR *oldpath,const WCHAR *newpath)
2122 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2123 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2125 TRACE(":failed (%ld)\n",GetLastError());
2126 MSVCRT__set_errno(GetLastError());
2130 /*********************************************************************
2131 * setvbuf (MSVCRT.@)
2133 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2135 /* TODO: Check if file busy */
2137 MSVCRT_free(file->_base);
2141 if(mode == MSVCRT__IOFBF) {
2142 file->_flag &= ~MSVCRT__IONBF;
2143 file->_base = file->_ptr = buf;
2145 file->_bufsiz = size;
2148 file->_flag |= MSVCRT__IONBF;
2153 /*********************************************************************
2156 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2158 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2161 /*********************************************************************
2164 char *MSVCRT_tmpnam(char *s)
2166 char tmpbuf[MAX_PATH];
2167 char* prefix = "TMP";
2168 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
2169 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
2171 TRACE(":failed-last error (%ld)\n",GetLastError());
2174 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
2179 /*********************************************************************
2180 * tmpfile (MSVCRT.@)
2182 MSVCRT_FILE* MSVCRT_tmpfile(void)
2184 char *filename = MSVCRT_tmpnam(NULL);
2186 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2188 return msvcrt_alloc_fp(fd);
2192 /*********************************************************************
2193 * vfprintf (MSVCRT.@)
2195 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2197 char buf[2048], *mem = buf;
2198 int written, resize = sizeof(buf), retval;
2199 /* There are two conventions for vsnprintf failing:
2200 * Return -1 if we truncated, or
2201 * Return the number of bytes that would have been written
2202 * The code below handles both cases
2204 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2207 resize = (written == -1 ? resize * 2 : written + 1);
2210 if (!(mem = (char *)MSVCRT_malloc(resize)))
2213 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2219 /*********************************************************************
2220 * vfwprintf (MSVCRT.@)
2222 * Is final char included in written (then resize is too big) or not
2223 * (then we must test for equality too)?
2225 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
2227 WCHAR buf[2048], *mem = buf;
2228 int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
2229 /* See vfprintf comments */
2230 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2233 resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
2236 if (!(mem = (WCHAR *)MSVCRT_malloc(resize*sizeof(*mem))))
2239 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2245 /*********************************************************************
2246 * vprintf (MSVCRT.@)
2248 int MSVCRT_vprintf(const char *format, va_list valist)
2250 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2253 /*********************************************************************
2254 * vwprintf (MSVCRT.@)
2256 int MSVCRT_vwprintf(const WCHAR *format, va_list valist)
2258 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2261 /*********************************************************************
2262 * fprintf (MSVCRT.@)
2264 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2268 va_start(valist, format);
2269 res = MSVCRT_vfprintf(file, format, valist);
2274 /*********************************************************************
2275 * fwprintf (MSVCRT.@)
2277 int MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
2281 va_start(valist, format);
2282 res = MSVCRT_vfwprintf(file, format, valist);
2287 /*********************************************************************
2290 int MSVCRT_printf(const char *format, ...)
2294 va_start(valist, format);
2295 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2300 /*********************************************************************
2303 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2305 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2306 msvcrt_alloc_buffer(file);
2309 if(file->_ptr>file->_base) {
2318 /*********************************************************************
2319 * ungetwc (MSVCRT.@)
2321 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2324 char * pp = (char *)&mwc;
2326 for(i=sizeof(WCHAR)-1;i>=0;i--) {
2327 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2333 /*********************************************************************
2334 * wprintf (MSVCRT.@)
2336 int MSVCRT_wprintf(const WCHAR *format, ...)
2340 va_start(valist, format);
2341 res = MSVCRT_vwprintf(format, valist);