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"
39 #include "msvcrt/errno.h"
41 #include "wine/unicode.h"
42 #include "msvcrt/direct.h"
43 #include "msvcrt/fcntl.h"
44 #include "msvcrt/io.h"
45 #include "msvcrt/sys/locking.h"
46 #include "msvcrt/stdio.h"
47 #include "msvcrt/stdlib.h"
48 #include "msvcrt/string.h"
49 #include "msvcrt/sys/stat.h"
50 #include "msvcrt/sys/utime.h"
51 #include "msvcrt/time.h"
52 #include "msvcrt/share.h"
53 #include "msvcrt/wctype.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
59 /* for stat mode, permissions apply to all,owner and group */
60 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
61 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
62 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
64 /* _access() bit flags FIXME: incomplete */
65 #define MSVCRT_W_OK 0x02
68 /* FIXME: Make this dynamic */
69 #define MSVCRT_MAX_FILES 257
71 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
72 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
73 int MSVCRT_flags[MSVCRT_MAX_FILES];
74 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
75 MSVCRT_FILE MSVCRT__iob[3];
76 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
77 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
78 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
80 static int MSVCRT_fdstart = 3; /* first unallocated fd */
81 static int MSVCRT_fdend = 3; /* highest allocated fd */
83 /* INTERNAL: process umask */
84 static int MSVCRT_umask = 0;
86 /* INTERNAL: Static buffer for temp file name */
87 static char MSVCRT_tmpname[MAX_PATH];
89 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
90 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
91 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
92 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
94 #define TOUL(x) (ULONGLONG)(x)
95 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
96 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
97 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
98 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
100 extern CRITICAL_SECTION MSVCRT_file_cs;
101 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
102 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
104 static void msvcrt_cp_from_stati64(const struct _stati64 *bufi64, struct _stat *buf)
106 buf->st_dev = bufi64->st_dev;
107 buf->st_ino = bufi64->st_ino;
108 buf->st_mode = bufi64->st_mode;
109 buf->st_nlink = bufi64->st_nlink;
110 buf->st_uid = bufi64->st_uid;
111 buf->st_gid = bufi64->st_gid;
112 buf->st_rdev = bufi64->st_rdev;
113 buf->st_size = bufi64->st_size;
114 buf->st_atime = bufi64->st_atime;
115 buf->st_mtime = bufi64->st_mtime;
116 buf->st_ctime = bufi64->st_ctime;
119 /* INTERNAL: Get the HANDLE for a fd */
120 static HANDLE msvcrt_fdtoh(int fd)
122 if (fd < 0 || fd >= MSVCRT_fdend ||
123 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
125 WARN(":fd (%d) - no handle!\n",fd);
127 *MSVCRT__errno() = MSVCRT_EBADF;
128 return INVALID_HANDLE_VALUE;
130 return MSVCRT_handles[fd];
133 /* INTERNAL: free a file entry fd */
134 static void msvcrt_free_fd(int fd)
136 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
137 MSVCRT_files[fd] = 0;
138 MSVCRT_flags[fd] = 0;
139 TRACE(":fd (%d) freed\n",fd);
141 return; /* dont use 0,1,2 for user files */
142 if (fd == MSVCRT_fdend - 1)
144 if (fd < MSVCRT_fdstart)
148 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
149 static int msvcrt_alloc_fd(HANDLE hand, int flag)
151 int fd = MSVCRT_fdstart;
153 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
154 if (fd >= MSVCRT_MAX_FILES)
156 WARN(":files exhausted!\n");
159 MSVCRT_handles[fd] = hand;
160 MSVCRT_flags[fd] = flag;
162 /* locate next free slot */
163 if (fd == MSVCRT_fdend)
164 MSVCRT_fdstart = ++MSVCRT_fdend;
166 while(MSVCRT_fdstart < MSVCRT_fdend &&
167 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
173 /* INTERNAL: Allocate a FILE* for an fd slot
174 * This is done lazily to avoid memory wastage for low level open/write
175 * usage when a FILE* is not requested (but may be later).
177 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
179 TRACE(":fd (%d) allocating FILE*\n",fd);
180 if (fd < 0 || fd >= MSVCRT_fdend ||
181 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
183 WARN(":invalid fd %d\n",fd);
185 *MSVCRT__errno() = MSVCRT_EBADF;
188 if (!MSVCRT_files[fd])
190 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
192 MSVCRT_files[fd]->_file = fd;
193 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
194 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
197 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
198 return MSVCRT_files[fd];
202 /* INTERNAL: Set up stdin, stderr and stdout */
203 void msvcrt_init_io(void)
206 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
207 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
208 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
209 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
210 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
211 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
212 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
214 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles[0],
215 MSVCRT_handles[1],MSVCRT_handles[2]);
217 for (i = 0; i < 3; i++)
219 /* FILE structs for stdin/out/err are static and never deleted */
220 MSVCRT_files[i] = &MSVCRT__iob[i];
221 MSVCRT__iob[i]._file = i;
222 MSVCRT_tempfiles[i] = NULL;
226 /* free everything on process exit */
227 void msvcrt_free_io(void)
235 /* INTERNAL: Flush stdio file buffer */
236 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
239 int cnt=file->_ptr-file->_base;
240 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
243 file->_ptr=file->_base;
244 file->_cnt=file->_bufsiz;
249 /* INTERNAL: Allocate stdio file buffer */
250 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
252 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
254 file->_bufsiz = MSVCRT_BUFSIZ;
255 file->_flag |= MSVCRT__IOMYBUF;
257 file->_base = (unsigned char *)(&file->_charbuf);
259 file->_bufsiz = sizeof(file->_charbuf);
261 file->_ptr = file->_base;
265 /*********************************************************************
268 MSVCRT_FILE *__p__iob(void)
270 return &MSVCRT__iob[0];
273 /*********************************************************************
276 int _access(const char *filename, int mode)
278 DWORD attr = GetFileAttributesA(filename);
280 TRACE("(%s,%d) %ld\n",filename,mode,attr);
282 if (!filename || attr == 0xffffffff)
284 MSVCRT__set_errno(GetLastError());
287 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
289 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
295 /*********************************************************************
296 * _waccess (MSVCRT.@)
298 int _waccess(const MSVCRT_wchar_t *filename, int mode)
300 DWORD attr = GetFileAttributesW(filename);
302 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
304 if (!filename || attr == 0xffffffff)
306 MSVCRT__set_errno(GetLastError());
309 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
311 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
317 /*********************************************************************
320 int _chmod(const char *path, int flags)
322 DWORD oldFlags = GetFileAttributesA(path);
324 if (oldFlags != 0x0FFFFFFFF)
326 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
327 oldFlags | FILE_ATTRIBUTE_READONLY;
329 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
332 MSVCRT__set_errno(GetLastError());
336 /*********************************************************************
339 int _wchmod(const MSVCRT_wchar_t *path, int flags)
341 DWORD oldFlags = GetFileAttributesW(path);
343 if (oldFlags != 0x0FFFFFFFF)
345 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
346 oldFlags | FILE_ATTRIBUTE_READONLY;
348 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
351 MSVCRT__set_errno(GetLastError());
355 /*********************************************************************
358 int _chsize(int fd, long size)
360 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
364 /*********************************************************************
369 FIXME("(od=%d): stub\n", od);
373 /*********************************************************************
376 int _dup2(int od, int nd)
378 FIXME("(od=%d, nd=%d): stub\n", od, nd);
382 /*********************************************************************
385 int _unlink(const char *path)
387 TRACE("(%s)\n",path);
388 if(DeleteFileA(path))
390 TRACE("failed (%ld)\n",GetLastError());
391 MSVCRT__set_errno(GetLastError());
395 /*********************************************************************
396 * _wunlink (MSVCRT.@)
398 int _wunlink(const MSVCRT_wchar_t *path)
400 TRACE("(%s)\n",debugstr_w(path));
401 if(DeleteFileW(path))
403 TRACE("failed (%ld)\n",GetLastError());
404 MSVCRT__set_errno(GetLastError());
408 /*********************************************************************
413 HANDLE hand = msvcrt_fdtoh(fd);
415 TRACE(":fd (%d) handle (%p)\n",fd,hand);
416 if (hand == INVALID_HANDLE_VALUE)
418 /* flush stdio buffers */
419 if(MSVCRT_files[fd]) {
420 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
421 MSVCRT_fflush(MSVCRT_files[fd]);
423 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
424 MSVCRT_free(MSVCRT_files[fd]->_base);
427 /* Dont free std FILE*'s, they are not dynamic */
428 if (fd > 2 && MSVCRT_files[fd])
429 MSVCRT_free(MSVCRT_files[fd]);
433 if (!CloseHandle(hand))
435 WARN(":failed-last error (%ld)\n",GetLastError());
436 MSVCRT__set_errno(GetLastError());
439 if (MSVCRT_tempfiles[fd])
441 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
442 _unlink(MSVCRT_tempfiles[fd]);
443 MSVCRT_free(MSVCRT_tempfiles[fd]);
444 MSVCRT_tempfiles[fd] = NULL;
451 /*********************************************************************
456 HANDLE hand = msvcrt_fdtoh(fd);
458 TRACE(":fd (%d) handle (%p)\n",fd,hand);
459 if (hand == INVALID_HANDLE_VALUE)
462 if (!FlushFileBuffers(hand))
464 if (GetLastError() == ERROR_INVALID_HANDLE)
466 /* FlushFileBuffers fails for console handles
467 * so we ignore this error.
471 TRACE(":failed-last error (%ld)\n",GetLastError());
472 MSVCRT__set_errno(GetLastError());
479 /*********************************************************************
485 HANDLE hand = msvcrt_fdtoh(fd);
487 TRACE(":fd (%d) handle (%p)\n",fd,hand);
489 if (hand == INVALID_HANDLE_VALUE)
492 /* If we have a FILE* for this file, the EOF flag
493 * will be set by the read()/write() functions.
495 if (MSVCRT_files[fd])
496 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
498 /* Otherwise we do it the hard way */
499 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
500 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
502 if (curpos == endpos)
505 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
509 /*********************************************************************
510 * _fcloseall (MSVCRT.@)
514 int num_closed = 0, i;
516 for (i = 3; i < MSVCRT_fdend; i++)
517 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
523 TRACE(":closed (%d) handles\n",num_closed);
527 /*********************************************************************
530 __int64 _lseeki64(int fd, __int64 offset, int whence)
532 DWORD ret, hoffset = (DWORD) (offset >> 32);
533 HANDLE hand = msvcrt_fdtoh(fd);
535 TRACE(":fd (%d) handle (%p)\n",fd,hand);
536 if (hand == INVALID_HANDLE_VALUE)
539 if (whence < 0 || whence > 2)
541 *MSVCRT__errno() = MSVCRT_EINVAL;
545 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
546 fd,hoffset,(long)offset,
547 (whence==SEEK_SET)?"SEEK_SET":
548 (whence==SEEK_CUR)?"SEEK_CUR":
549 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
551 if (((ret = SetFilePointer(hand, (long)offset, &hoffset,
552 whence)) != INVALID_SET_FILE_POINTER) || !GetLastError())
554 if (MSVCRT_files[fd])
555 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
556 /* FIXME: What if we seek _to_ EOF - is EOF set? */
558 return ((__int64)hoffset << 32) | ret;
560 TRACE(":error-last error (%ld)\n",GetLastError());
561 if (MSVCRT_files[fd])
562 switch(GetLastError())
564 case ERROR_NEGATIVE_SEEK:
565 case ERROR_SEEK_ON_DEVICE:
566 MSVCRT__set_errno(GetLastError());
567 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
575 /*********************************************************************
578 LONG _lseek(int fd, LONG offset, int whence)
580 return _lseeki64(fd, offset, whence);
583 /*********************************************************************
584 * _locking (MSVCRT.@)
586 * This is untested; the underlying LockFile doesn't work yet.
588 int _locking(int fd, int mode, LONG nbytes)
592 HANDLE hand = msvcrt_fdtoh(fd);
594 TRACE(":fd (%d) handle (%p)\n",fd,hand);
595 if (hand == INVALID_HANDLE_VALUE)
598 if (mode < 0 || mode > 4)
600 *MSVCRT__errno() = MSVCRT_EINVAL;
604 TRACE(":fd (%d) by 0x%08lx mode %s\n",
605 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
606 (mode==_LK_LOCK)?"_LK_LOCK":
607 (mode==_LK_NBLCK)?"_LK_NBLCK":
608 (mode==_LK_RLCK)?"_LK_RLCK":
609 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
612 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
614 FIXME ("Seek failed\n");
615 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
618 if (mode == _LK_LOCK || mode == _LK_RLCK)
621 ret = 1; /* just to satisfy gcc */
624 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
629 else if (mode == _LK_UNLCK)
630 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
632 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
633 /* FIXME - what about error settings? */
637 /*********************************************************************
640 void MSVCRT_rewind(MSVCRT_FILE* file)
642 TRACE(":file (%p) fd (%d)\n",file,file->_file);
643 MSVCRT_fseek(file, 0L, SEEK_SET);
644 MSVCRT_clearerr(file);
647 /*********************************************************************
650 MSVCRT_FILE* _fdopen(int fd, const char *mode)
652 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
654 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
659 /*********************************************************************
660 * _wfdopen (MSVCRT.@)
662 MSVCRT_FILE* _wfdopen(int fd, const MSVCRT_wchar_t *mode)
664 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
666 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
673 /*********************************************************************
674 * _filelength (MSVCRT.@)
676 LONG _filelength(int fd)
678 LONG curPos = _lseek(fd, 0, SEEK_CUR);
681 LONG endPos = _lseek(fd, 0, SEEK_END);
684 if (endPos != curPos)
685 _lseek(fd, curPos, SEEK_SET);
692 /*********************************************************************
695 int _fileno(MSVCRT_FILE* file)
697 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
701 /*********************************************************************
702 * _flushall (MSVCRT.@)
706 int num_flushed = 0, i = 3;
708 while(i < MSVCRT_fdend)
709 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
712 /* FIXME: flush, do not commit */
713 if (_commit(i) == -1)
715 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
717 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
718 MSVCRT_fflush(MSVCRT_files[i]);
723 TRACE(":flushed (%d) handles\n",num_flushed);
727 /*********************************************************************
728 * _fstati64 (MSVCRT.@)
730 int _fstati64(int fd, struct _stati64* buf)
733 BY_HANDLE_FILE_INFORMATION hfi;
734 HANDLE hand = msvcrt_fdtoh(fd);
736 TRACE(":fd (%d) stat (%p)\n",fd,buf);
737 if (hand == INVALID_HANDLE_VALUE)
742 WARN(":failed-NULL buf\n");
743 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
747 memset(&hfi, 0, sizeof(hfi));
748 memset(buf, 0, sizeof(struct _stati64));
749 if (!GetFileInformationByHandle(hand, &hfi))
751 WARN(":failed-last error (%ld)\n",GetLastError());
752 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
755 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
756 buf->st_nlink = hfi.nNumberOfLinks;
757 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
758 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
760 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
761 buf->st_mtime = buf->st_ctime = dw;
765 /*********************************************************************
768 int MSVCRT__fstat(int fd, struct _stat* buf)
770 struct _stati64 bufi64;
772 ret = _fstati64(fd, &bufi64);
774 msvcrt_cp_from_stati64(&bufi64, buf);
778 /*********************************************************************
781 int _futime(int fd, struct _utimbuf *t)
783 HANDLE hand = msvcrt_fdtoh(fd);
788 MSVCRT_time_t currTime;
789 MSVCRT_time(&currTime);
790 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
791 memcpy(&wt, &at, sizeof(wt));
795 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
796 if (t->actime == t->modtime)
797 memcpy(&wt, &at, sizeof(wt));
799 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
802 if (!SetFileTime(hand, NULL, &at, &wt))
804 MSVCRT__set_errno(GetLastError());
810 /*********************************************************************
811 * _get_osfhandle (MSVCRT.@)
813 long _get_osfhandle(int fd)
815 HANDLE hand = msvcrt_fdtoh(fd);
816 HANDLE newhand = hand;
817 TRACE(":fd (%d) handle (%p)\n",fd,hand);
819 if (hand != INVALID_HANDLE_VALUE)
821 /* FIXME: I'm not convinced that I should be copying the
822 * handle here - it may be leaked if the app doesn't
823 * close it (and the API docs dont say that it should)
824 * Not duplicating it means that it can't be inherited
825 * and so lcc's wedit doesn't cope when it passes it to
826 * child processes. I've an idea that it should either
827 * be copied by CreateProcess, or marked as inheritable
828 * when initialised, or maybe both? JG 21-9-00.
830 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
831 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
833 return (long)newhand;
836 /*********************************************************************
841 HANDLE hand = msvcrt_fdtoh(fd);
843 TRACE(":fd (%d) handle (%p)\n",fd,hand);
844 if (hand == INVALID_HANDLE_VALUE)
847 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
850 /*********************************************************************
853 char *_mktemp(char *pattern)
856 char *retVal = pattern;
861 numX = (*pattern++ == 'X')? numX + 1 : 0;
865 id = GetCurrentProcessId();
869 int tempNum = id / 10;
870 *pattern-- = id - (tempNum * 10) + '0';
876 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
877 GetLastError() == ERROR_FILE_NOT_FOUND)
880 } while(letter != '|');
884 /*********************************************************************
885 * _wmktemp (MSVCRT.@)
887 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
890 MSVCRT_wchar_t *retVal = pattern;
892 MSVCRT_wchar_t letter = 'a';
895 numX = (*pattern++ == 'X')? numX + 1 : 0;
899 id = GetCurrentProcessId();
903 int tempNum = id / 10;
904 *pattern-- = id - (tempNum * 10) + '0';
910 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
911 GetLastError() == ERROR_FILE_NOT_FOUND)
914 } while(letter != '|');
918 /*********************************************************************
921 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
925 DWORD access = 0, creation = 0;
929 SECURITY_ATTRIBUTES sa;
932 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
933 path, oflags, shflags);
935 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
938 access |= GENERIC_READ;
939 ioflag |= MSVCRT__IOREAD;
942 access |= GENERIC_WRITE;
943 ioflag |= MSVCRT__IOWRT;
946 access |= GENERIC_WRITE | GENERIC_READ;
947 ioflag |= MSVCRT__IORW;
951 if (oflags & _O_CREAT)
953 va_start(ap, shflags);
954 pmode = va_arg(ap, int);
957 if(pmode & ~(_S_IREAD | _S_IWRITE))
958 FIXME(": pmode 0x%04x ignored\n", pmode);
960 WARN(": pmode 0x%04x ignored\n", pmode);
962 if (oflags & _O_EXCL)
963 creation = CREATE_NEW;
964 else if (oflags & _O_TRUNC)
965 creation = CREATE_ALWAYS;
967 creation = OPEN_ALWAYS;
969 else /* no _O_CREAT */
971 if (oflags & _O_TRUNC)
972 creation = TRUNCATE_EXISTING;
974 creation = OPEN_EXISTING;
976 if (oflags & _O_APPEND)
977 ioflag |= MSVCRT__IOAPPEND;
979 if (oflags & _O_BINARY)
981 else if (oflags & _O_TEXT)
983 else if (*__p__fmode() & _O_BINARY)
986 ioflag |= _O_TEXT; /* default to TEXT*/
994 sharing = FILE_SHARE_READ;
997 sharing = FILE_SHARE_WRITE;
1000 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1003 ERR( "Unhandled shflags 0x%x\n", shflags );
1007 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
1008 |_O_CREAT|_O_RDWR|_O_TEMPORARY|_O_NOINHERIT))
1009 TRACE(":unsupported oflags 0x%04x\n",oflags);
1011 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1012 sa.lpSecurityDescriptor = NULL;
1013 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
1015 hand = CreateFileA(path, access, sharing,
1016 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
1018 if (hand == INVALID_HANDLE_VALUE) {
1019 WARN(":failed-last error (%ld)\n",GetLastError());
1020 MSVCRT__set_errno(GetLastError());
1024 fd = msvcrt_alloc_fd(hand, ioflag);
1026 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1030 if (oflags & _O_TEMPORARY)
1031 MSVCRT_tempfiles[fd] = _strdup(path);
1032 if (ioflag & MSVCRT__IOAPPEND)
1033 _lseek(fd, 0, FILE_END);
1039 /*********************************************************************
1040 * _wsopen (MSVCRT.@)
1042 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1044 const unsigned int len = strlenW(path);
1045 char *patha = MSVCRT_calloc(len + 1,1);
1049 va_start(ap, shflags);
1050 pmode = va_arg(ap, int);
1053 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1055 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1060 MSVCRT__set_errno(GetLastError());
1064 /*********************************************************************
1067 int _open( const char *path, int flags, ... )
1071 if (flags & _O_CREAT)
1074 va_start(ap, flags);
1075 pmode = va_arg(ap, int);
1077 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1080 return MSVCRT__sopen( path, flags, _SH_DENYNO);
1083 /*********************************************************************
1086 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1088 const unsigned int len = strlenW(path);
1089 char *patha = MSVCRT_calloc(len + 1,1);
1093 va_start(ap, flags);
1094 pmode = va_arg(ap, int);
1097 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1099 int retval = _open(patha,flags,pmode);
1104 MSVCRT__set_errno(GetLastError());
1108 /*********************************************************************
1111 int _creat(const char *path, int flags)
1113 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1114 return _open(path, usedFlags);
1117 /*********************************************************************
1118 * _wcreat (MSVCRT.@)
1120 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1122 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1123 return _wopen(path, usedFlags);
1126 /*********************************************************************
1127 * _open_osfhandle (MSVCRT.@)
1129 int _open_osfhandle(long hand, int flags)
1133 /* _O_RDONLY (0) always matches, so set the read flag
1134 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1135 * file, so set the write flag. It also only sets _O_TEXT if it wants
1136 * text - it never sets _O_BINARY.
1138 /* FIXME: handle more flags */
1139 flags |= MSVCRT__IOREAD|MSVCRT__IOWRT;
1140 if ( !( flags & _O_TEXT ) ) flags |= _O_BINARY;
1142 fd = msvcrt_alloc_fd((HANDLE)hand,flags);
1143 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd, flags);
1147 /*********************************************************************
1152 int num_removed = 0, i;
1154 for (i = 3; i < MSVCRT_fdend; i++)
1155 if (MSVCRT_tempfiles[i])
1162 TRACE(":removed (%d) temp files\n",num_removed);
1166 /*********************************************************************
1169 int _read(int fd, void *buf, unsigned int count)
1172 HANDLE hand = msvcrt_fdtoh(fd);
1174 /* Dont trace small reads, it gets *very* annoying */
1176 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1177 if (hand == INVALID_HANDLE_VALUE)
1180 if (MSVCRT_flags[fd]& _O_BINARY)
1182 if (ReadFile(hand, buf, count, &num_read, NULL))
1184 if (num_read != count && MSVCRT_files[fd])
1187 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1189 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1192 TRACE("%s\n",debugstr_an(buf,num_read));
1195 TRACE(":failed-last error (%ld)\n",GetLastError());
1196 if (MSVCRT_files[fd])
1197 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1202 char cc, *s=(char*)buf,* buf_start=(char*)buf;
1205 for (i = 0 , num_read = 1; i < count && (num_read == 1);)
1207 if (ReadFile(hand, &cc, 1, &num_read, NULL))
1209 if ((cc != '\r') || MSVCRT_flags[fd] & _O_BINARY)
1218 if (MSVCRT_files[fd])
1219 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1221 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1226 TRACE("%s\n",debugstr_an(buf_start, s-buf_start));
1232 /*********************************************************************
1235 int _getw(MSVCRT_FILE* file)
1238 if (_read(file->_file, &i, sizeof(int)) != 1)
1243 /*********************************************************************
1244 * _setmode (MSVCRT.@)
1246 int _setmode(int fd,int mode)
1248 int ret = MSVCRT_flags[fd] & (_O_TEXT | _O_BINARY);
1249 if (mode & (~(_O_TEXT|_O_BINARY)))
1250 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1251 MSVCRT_flags[fd] &= ~(_O_TEXT|_O_BINARY);
1252 MSVCRT_flags[fd] |= mode & (_O_TEXT | _O_BINARY);
1256 /*********************************************************************
1257 * _stati64 (MSVCRT.@)
1259 int _stati64(const char* path, struct _stati64 * buf)
1262 WIN32_FILE_ATTRIBUTE_DATA hfi;
1263 unsigned short mode = MSVCRT_S_IREAD;
1266 TRACE(":file (%s) buf(%p)\n",path,buf);
1268 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1270 TRACE("failed (%ld)\n",GetLastError());
1271 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1275 memset(buf,0,sizeof(struct _stati64));
1277 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1278 Bon 011120: This FIXME seems incorrect
1279 Also a letter as first char isn't enough to be classify
1282 if (isalpha(*path)&& (*(path+1)==':'))
1283 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1285 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1287 plen = strlen(path);
1289 /* Dir, or regular file? */
1290 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1291 (path[plen-1] == '\\'))
1292 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1297 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1299 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1300 (tolower(path[plen-3]) << 16);
1301 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1302 mode |= MSVCRT_S_IEXEC;
1306 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1307 mode |= MSVCRT_S_IWRITE;
1309 buf->st_mode = mode;
1311 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1312 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1314 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1315 buf->st_mtime = buf->st_ctime = dw;
1316 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1317 (long)(buf->st_size >> 32),(long)buf->st_size,
1318 buf->st_atime,buf->st_mtime, buf->st_ctime);
1322 /*********************************************************************
1325 int MSVCRT__stat(const char* path, struct _stat * buf)
1327 struct _stati64 bufi64;
1329 ret = _stati64( path, &bufi64);
1331 msvcrt_cp_from_stati64(&bufi64, buf);
1335 /*********************************************************************
1338 int _wstat(const MSVCRT_wchar_t* path, struct _stat * buf)
1341 WIN32_FILE_ATTRIBUTE_DATA hfi;
1342 unsigned short mode = MSVCRT_S_IREAD;
1345 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1347 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1349 TRACE("failed (%ld)\n",GetLastError());
1350 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1354 memset(buf,0,sizeof(struct _stat));
1356 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1357 if (MSVCRT_iswalpha(*path))
1358 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1360 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1362 plen = strlenW(path);
1364 /* Dir, or regular file? */
1365 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1366 (path[plen-1] == '\\'))
1367 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1372 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1374 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1375 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1376 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1377 mode |= MSVCRT_S_IEXEC;
1381 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1382 mode |= MSVCRT_S_IWRITE;
1384 buf->st_mode = mode;
1386 buf->st_size = hfi.nFileSizeLow;
1387 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1389 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1390 buf->st_mtime = buf->st_ctime = dw;
1391 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1392 buf->st_atime,buf->st_mtime, buf->st_ctime);
1396 /*********************************************************************
1401 return _lseek(fd, 0, SEEK_CUR);
1404 /*********************************************************************
1405 * _tempnam (MSVCRT.@)
1407 char *_tempnam(const char *dir, const char *prefix)
1409 char tmpbuf[MAX_PATH];
1411 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1412 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1414 TRACE("got name (%s)\n",tmpbuf);
1415 return _strdup(tmpbuf);
1417 TRACE("failed (%ld)\n",GetLastError());
1421 /*********************************************************************
1422 * _wtempnam (MSVCRT.@)
1424 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1426 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1428 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1429 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1431 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1432 return _wcsdup(tmpbuf);
1434 TRACE("failed (%ld)\n",GetLastError());
1438 /*********************************************************************
1441 int _umask(int umask)
1443 int old_umask = MSVCRT_umask;
1444 TRACE("(%d)\n",umask);
1445 MSVCRT_umask = umask;
1449 /*********************************************************************
1452 int _utime(const char* path, struct _utimbuf *t)
1454 int fd = _open(path, _O_WRONLY | _O_BINARY);
1458 int retVal = _futime(fd, t);
1465 /*********************************************************************
1466 * _wutime (MSVCRT.@)
1468 int _wutime(const MSVCRT_wchar_t* path, struct _utimbuf *t)
1470 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1474 int retVal = _futime(fd, t);
1481 /*********************************************************************
1484 int _write(int fd, const void* buf, unsigned int count)
1487 HANDLE hand = msvcrt_fdtoh(fd);
1489 /* Dont trace small writes, it gets *very* annoying */
1492 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1494 if (hand == INVALID_HANDLE_VALUE)
1496 *MSVCRT__errno() = MSVCRT_EBADF;
1500 /* If appending, go to EOF */
1501 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1502 _lseek(fd, 0, FILE_END);
1504 if (MSVCRT_flags[fd] & _O_BINARY)
1506 if (WriteFile(hand, buf, count, &num_written, NULL)
1507 && (num_written == count))
1509 TRACE(":failed-last error (%ld)\n",GetLastError());
1510 if (MSVCRT_files[fd])
1512 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1513 *MSVCRT__errno() = MSVCRT_ENOSPC;
1518 char *s=(char*)buf, *buf_start=(char*)buf, *p;
1519 char crlf[]= {'\r','\n'};
1522 for (i = 0; i< count && !(MSVCRT_flags[fd] & MSVCRT__IOERR);i++, s++)
1534 if ((WriteFile(hand, p, num_to_write, &num_written, NULL) == 0 ) || (num_written != num_to_write))
1536 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
1537 if (MSVCRT_files[fd])
1539 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1540 *MSVCRT__errno() = MSVCRT_ENOSPC;
1541 return s - buf_start;
1545 return s - buf_start;
1550 /*********************************************************************
1553 int _putw(int val, MSVCRT_FILE* file)
1555 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1558 /*********************************************************************
1559 * clearerr (MSVCRT.@)
1561 void MSVCRT_clearerr(MSVCRT_FILE* file)
1563 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1564 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1567 /*********************************************************************
1570 int MSVCRT_fclose(MSVCRT_FILE* file)
1575 r=_close(file->_file);
1576 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1579 /*********************************************************************
1582 int MSVCRT_feof(MSVCRT_FILE* file)
1584 return file->_flag & MSVCRT__IOEOF;
1587 /*********************************************************************
1590 int MSVCRT_ferror(MSVCRT_FILE* file)
1592 return file->_flag & MSVCRT__IOERR;
1595 /*********************************************************************
1598 int MSVCRT_fflush(MSVCRT_FILE* file)
1604 int res=msvcrt_flush_buffer(file);
1609 /*********************************************************************
1612 int MSVCRT_fgetc(MSVCRT_FILE* file)
1616 return *(unsigned char *)file->_ptr++;
1618 return _filbuf(file);
1622 /*********************************************************************
1623 * _fgetchar (MSVCRT.@)
1627 return MSVCRT_fgetc(MSVCRT_stdin);
1630 /*********************************************************************
1631 * _filbuf (MSVCRT.@)
1633 int _filbuf(MSVCRT_FILE* file)
1636 /* Allocate buffer if needed */
1637 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1638 msvcrt_alloc_buffer(file);
1640 if(!(file->_flag & MSVCRT__IOREAD)) {
1641 if(file->_flag & MSVCRT__IORW) {
1642 file->_flag |= MSVCRT__IOREAD;
1647 if(file->_flag & MSVCRT__IONBF) {
1649 if (_read(file->_file,&c,1) != 1) {
1650 file->_flag |= MSVCRT__IOEOF;
1655 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1656 if(file->_cnt<0) file->_cnt = 0;
1658 file->_flag |= MSVCRT__IOEOF;
1662 file->_ptr = file->_base+1;
1663 return *(unsigned char *)file->_base;
1667 /*********************************************************************
1668 * fgetpos (MSVCRT.@)
1670 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1672 *pos = MSVCRT_ftell(file);
1673 return (*pos == -1? -1 : 0);
1676 /*********************************************************************
1679 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1682 char * buf_start = s;
1684 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1685 file,file->_file,s,size);
1687 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1688 cc = MSVCRT_fgetc(file))
1689 /* _read already handled the translation */
1691 if (--size <= 0) break;
1694 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1696 TRACE(":nothing read\n");
1703 TRACE(":got '%s'\n", debugstr_a(buf_start));
1707 /*********************************************************************
1710 * In _O_TEXT mode, bultibyte characters are read from the file, dropping
1711 * the CR from CR/LF combinations
1713 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1717 if (file->_flag & _O_BINARY)
1720 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1724 c = MSVCRT_fgetc(file);
1725 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
1727 FIXME("Treat Multibyte characters\n");
1729 if (c == MSVCRT_EOF)
1732 return (MSVCRT_wint_t)c;
1735 /*********************************************************************
1738 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1740 return MSVCRT_fgetwc(file);
1743 /*********************************************************************
1744 * _fgetwchar (MSVCRT.@)
1746 MSVCRT_wint_t _fgetwchar(void)
1748 return MSVCRT_fgetwc(MSVCRT_stdin);
1751 /*********************************************************************
1752 * getwchar (MSVCRT.@)
1754 MSVCRT_wint_t MSVCRT_getwchar(void)
1756 return _fgetwchar();
1759 /*********************************************************************
1762 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
1765 MSVCRT_wchar_t * buf_start = s;
1767 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1768 file,file->_file,s,size);
1770 for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n';
1771 cc = MSVCRT_fgetwc(file))
1772 /* _read already handled the translation */
1774 if (--size <= 0) break;
1777 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1779 TRACE(":nothing read\n");
1786 /* TRACE(":got '%s'\n", buf_start); */
1791 /*********************************************************************
1794 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1796 MSVCRT_wchar_t mwc=wc;
1797 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
1802 /*********************************************************************
1803 * _fputwchar (MSVCRT.@)
1805 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1807 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1810 /*********************************************************************
1813 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1816 int flags = 0, plus = 0, fd;
1817 const char* search = mode;
1819 TRACE("(%s,%s)\n",path,mode);
1822 if (*search++ == '+')
1825 /* map mode string to open() flags. "man fopen" for possibilities. */
1829 flags = (plus ? _O_RDWR : _O_RDONLY);
1832 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1835 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1850 flags &= ~_O_BINARY;
1855 FIXME(":unknown flag %c not supported\n",mode[-1]);
1858 fd = _open(path, flags);
1863 file = msvcrt_alloc_fp(fd);
1864 TRACE(":got (%p)\n",file);
1871 /*********************************************************************
1872 * _wfopen (MSVCRT.@)
1874 MSVCRT_FILE *_wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
1876 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1877 char *patha = MSVCRT_calloc(plen + 1, 1);
1878 char *modea = MSVCRT_calloc(mlen + 1, 1);
1880 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1882 if (patha && modea &&
1883 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1884 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1886 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1892 MSVCRT__set_errno(GetLastError());
1896 /*********************************************************************
1897 * _fsopen (MSVCRT.@)
1899 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1901 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1902 return MSVCRT_fopen(path,mode);
1905 /*********************************************************************
1906 * _wfsopen (MSVCRT.@)
1908 MSVCRT_FILE* _wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
1910 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1911 debugstr_w(path),debugstr_w(mode),share);
1912 return _wfopen(path,mode);
1915 /*********************************************************************
1918 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1925 return _flsbuf(c, file);
1929 /*********************************************************************
1930 * _flsbuf (MSVCRT.@)
1932 int _flsbuf(int c, MSVCRT_FILE* file)
1934 /* Flush output buffer */
1935 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
1936 msvcrt_alloc_buffer(file);
1938 if(!(file->_flag & MSVCRT__IOWRT)) {
1939 if(file->_flag & MSVCRT__IORW) {
1940 file->_flag |= MSVCRT__IOWRT;
1946 int res=msvcrt_flush_buffer(file);
1947 return res?res : MSVCRT_fputc(c, file);
1950 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
1954 /*********************************************************************
1955 * _fputchar (MSVCRT.@)
1957 int _fputchar(int c)
1959 return MSVCRT_fputc(c, MSVCRT_stdout);
1962 /*********************************************************************
1965 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1966 { MSVCRT_size_t rcnt=size * nmemb;
1967 MSVCRT_size_t read=0;
1969 /* first buffered data */
1971 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
1972 memcpy(ptr, file->_ptr, pcnt);
1977 ptr = (char*)ptr + pcnt;
1978 } else if(!(file->_flag & MSVCRT__IOREAD )) {
1979 if(file->_flag & MSVCRT__IORW) {
1980 file->_flag |= MSVCRT__IOREAD;
1984 if(rcnt) pread = _read(file->_file,ptr, rcnt);
1985 if (MSVCRT_flags[file->_file] & MSVCRT__IOEOF)
1986 /* expose feof condition in the flags
1987 MFC tests file->_flag for feof, and doesn't not call feof())
1989 file->_flag |= MSVCRT__IOEOF;
1996 /*********************************************************************
1997 * freopen (MSVCRT.@)
2000 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2002 MSVCRT_FILE* newfile;
2005 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2006 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2012 FIXME(":reopen on user file not implemented!\n");
2013 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
2016 if(MSVCRT_fclose(file))
2018 return MSVCRT_fopen(path, mode);
2021 /* first, create the new file */
2022 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
2025 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
2026 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
2027 MSVCRT_handles[newfile->_file]))
2029 /* Redirecting std handle to file , copy over.. */
2030 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
2031 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
2032 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
2033 MSVCRT__iob[fd]._file = fd;
2034 /* And free up the resources allocated by fopen, but
2035 * not the HANDLE we copied. */
2036 MSVCRT_free(MSVCRT_files[fd]);
2037 msvcrt_free_fd(newfile->_file);
2038 return &MSVCRT__iob[fd];
2041 WARN(":failed-last error (%ld)\n",GetLastError());
2042 MSVCRT_fclose(newfile);
2043 MSVCRT__set_errno(GetLastError());
2047 /*********************************************************************
2048 * fsetpos (MSVCRT.@)
2050 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2052 return _lseek(file->_file,*pos,SEEK_SET);
2055 /*********************************************************************
2058 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
2060 /* Flush output if needed */
2061 if(file->_flag & MSVCRT__IOWRT)
2062 msvcrt_flush_buffer(file);
2064 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
2065 offset -= file->_cnt;
2067 /* Discard buffered input */
2069 file->_ptr = file->_base;
2070 /* Reset direction of i/o */
2071 if(file->_flag & MSVCRT__IORW) {
2072 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2074 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
2077 /*********************************************************************
2080 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2085 if( file->_flag & MSVCRT__IOWRT ) {
2086 off = file->_ptr - file->_base;
2091 pos = _tell(file->_file);
2092 if(pos == -1) return pos;
2096 /*********************************************************************
2099 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2101 MSVCRT_size_t wrcnt=size * nmemb;
2106 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2107 memcpy(file->_ptr, ptr, pcnt);
2112 ptr = (char*)ptr + pcnt;
2113 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2114 if(file->_flag & MSVCRT__IORW) {
2115 file->_flag |= MSVCRT__IOWRT;
2121 int res=msvcrt_flush_buffer(file);
2123 int pwritten = _write(file->_file, ptr, wrcnt);
2124 if (pwritten <= 0) pwritten=0;
2125 written += pwritten;
2128 return written / size;
2131 /*********************************************************************
2134 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2136 size_t i, len = strlen(s);
2137 if (file->_flag & _O_BINARY)
2138 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2139 for (i=0; i<len; i++)
2140 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2145 /*********************************************************************
2148 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2150 size_t i, len = strlenW(s);
2151 if (file->_flag & _O_BINARY)
2152 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2153 for (i=0; i<len; i++)
2155 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2157 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2163 /*********************************************************************
2164 * getchar (MSVCRT.@)
2166 int MSVCRT_getchar(void)
2168 return MSVCRT_fgetc(MSVCRT_stdin);
2171 /*********************************************************************
2174 int MSVCRT_getc(MSVCRT_FILE* file)
2176 return MSVCRT_fgetc(file);
2179 /*********************************************************************
2182 char *MSVCRT_gets(char *buf)
2185 char * buf_start = buf;
2187 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2188 cc = MSVCRT_fgetc(MSVCRT_stdin))
2189 if(cc != '\r') *buf++ = (char)cc;
2193 TRACE("got '%s'\n", buf_start);
2197 /*********************************************************************
2200 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2203 MSVCRT_wchar_t* ws = buf;
2205 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2206 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2209 *buf++ = (MSVCRT_wchar_t)cc;
2213 TRACE("got '%s'\n", debugstr_w(ws));
2217 /*********************************************************************
2220 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2222 return MSVCRT_fputc(c, file);
2225 /*********************************************************************
2226 * putchar (MSVCRT.@)
2228 int MSVCRT_putchar(int c)
2230 return MSVCRT_fputc(c, MSVCRT_stdout);
2233 /*********************************************************************
2236 int MSVCRT_puts(const char *s)
2238 size_t len = strlen(s);
2239 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2240 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2243 /*********************************************************************
2246 int _putws(const MSVCRT_wchar_t *s)
2248 static const MSVCRT_wchar_t nl = '\n';
2249 size_t len = strlenW(s);
2250 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2251 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2254 /*********************************************************************
2257 int MSVCRT_remove(const char *path)
2259 TRACE("(%s)\n",path);
2260 if (DeleteFileA(path))
2262 TRACE(":failed (%ld)\n",GetLastError());
2263 MSVCRT__set_errno(GetLastError());
2267 /*********************************************************************
2268 * _wremove (MSVCRT.@)
2270 int _wremove(const MSVCRT_wchar_t *path)
2272 TRACE("(%s)\n",debugstr_w(path));
2273 if (DeleteFileW(path))
2275 TRACE(":failed (%ld)\n",GetLastError());
2276 MSVCRT__set_errno(GetLastError());
2280 /*********************************************************************
2283 int MSVCRT_scanf(const char *format, ...)
2288 va_start(valist, format);
2289 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
2294 /*********************************************************************
2297 int MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...)
2302 va_start(valist, format);
2303 res = MSVCRT_fwscanf(MSVCRT_stdin, format, valist);
2308 /*********************************************************************
2311 int MSVCRT_rename(const char *oldpath,const char *newpath)
2313 TRACE(":from %s to %s\n",oldpath,newpath);
2314 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2316 TRACE(":failed (%ld)\n",GetLastError());
2317 MSVCRT__set_errno(GetLastError());
2321 /*********************************************************************
2322 * _wrename (MSVCRT.@)
2324 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2326 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2327 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2329 TRACE(":failed (%ld)\n",GetLastError());
2330 MSVCRT__set_errno(GetLastError());
2334 /*********************************************************************
2335 * setvbuf (MSVCRT.@)
2337 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2339 /* TODO: Check if file busy */
2341 MSVCRT_free(file->_base);
2345 if(mode == MSVCRT__IOFBF) {
2346 file->_flag &= ~MSVCRT__IONBF;
2347 file->_base = file->_ptr = buf;
2349 file->_bufsiz = size;
2352 file->_flag |= MSVCRT__IONBF;
2357 /*********************************************************************
2360 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2362 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2365 /*********************************************************************
2368 char *MSVCRT_tmpnam(char *s)
2370 char tmpbuf[MAX_PATH];
2371 char* prefix = "TMP";
2372 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
2373 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
2375 TRACE(":failed-last error (%ld)\n",GetLastError());
2378 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
2383 /*********************************************************************
2384 * tmpfile (MSVCRT.@)
2386 MSVCRT_FILE* MSVCRT_tmpfile(void)
2388 char *filename = MSVCRT_tmpnam(NULL);
2390 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2392 return msvcrt_alloc_fp(fd);
2396 /*********************************************************************
2397 * vfprintf (MSVCRT.@)
2399 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2401 char buf[2048], *mem = buf;
2402 int written, resize = sizeof(buf), retval;
2403 /* There are two conventions for vsnprintf failing:
2404 * Return -1 if we truncated, or
2405 * Return the number of bytes that would have been written
2406 * The code below handles both cases
2408 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2411 resize = (written == -1 ? resize * 2 : written + 1);
2414 if (!(mem = (char *)MSVCRT_malloc(resize)))
2417 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2423 /*********************************************************************
2424 * vfwprintf (MSVCRT.@)
2426 * Is final char included in written (then resize is too big) or not
2427 * (then we must test for equality too)?
2429 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2431 MSVCRT_wchar_t buf[2048], *mem = buf;
2432 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2433 /* See vfprintf comments */
2434 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2437 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2440 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2443 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2449 /*********************************************************************
2450 * vprintf (MSVCRT.@)
2452 int MSVCRT_vprintf(const char *format, va_list valist)
2454 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2457 /*********************************************************************
2458 * vwprintf (MSVCRT.@)
2460 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2462 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2465 /*********************************************************************
2466 * fprintf (MSVCRT.@)
2468 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2472 va_start(valist, format);
2473 res = MSVCRT_vfprintf(file, format, valist);
2478 /*********************************************************************
2479 * fwprintf (MSVCRT.@)
2481 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2485 va_start(valist, format);
2486 res = MSVCRT_vfwprintf(file, format, valist);
2491 /*********************************************************************
2494 int MSVCRT_printf(const char *format, ...)
2498 va_start(valist, format);
2499 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2504 /*********************************************************************
2507 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2509 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2510 msvcrt_alloc_buffer(file);
2513 if(file->_ptr>file->_base) {
2522 /*********************************************************************
2523 * ungetwc (MSVCRT.@)
2525 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2527 MSVCRT_wchar_t mwc = wc;
2528 char * pp = (char *)&mwc;
2530 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2531 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2537 /*********************************************************************
2538 * wprintf (MSVCRT.@)
2540 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2544 va_start(valist, format);
2545 res = MSVCRT_vwprintf(format, valist);