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 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
208 GetCurrentProcess(), &MSVCRT_handles[0], 0, FALSE, DUPLICATE_SAME_ACCESS);
209 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
210 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
211 GetCurrentProcess(), &MSVCRT_handles[1], 0, FALSE, DUPLICATE_SAME_ACCESS);
212 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
213 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
214 GetCurrentProcess(), &MSVCRT_handles[2], 0, FALSE, DUPLICATE_SAME_ACCESS);
215 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
217 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles[0],
218 MSVCRT_handles[1],MSVCRT_handles[2]);
220 for (i = 0; i < 3; i++)
222 /* FILE structs for stdin/out/err are static and never deleted */
223 MSVCRT_files[i] = &MSVCRT__iob[i];
224 MSVCRT__iob[i]._file = i;
225 MSVCRT_tempfiles[i] = NULL;
229 /* free everything on process exit */
230 void msvcrt_free_io(void)
238 /* INTERNAL: Flush stdio file buffer */
239 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
242 int cnt=file->_ptr-file->_base;
243 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
246 file->_ptr=file->_base;
247 file->_cnt=file->_bufsiz;
252 /* INTERNAL: Allocate stdio file buffer */
253 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
255 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
257 file->_bufsiz = MSVCRT_BUFSIZ;
258 file->_flag |= MSVCRT__IOMYBUF;
260 file->_base = (unsigned char *)(&file->_charbuf);
262 file->_bufsiz = sizeof(file->_charbuf);
264 file->_ptr = file->_base;
268 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
269 static void msvcrt_int_to_base32(int num, char *str)
284 *p = (num & 31) + '0';
286 *p += ('a' - '0' - 10);
291 /*********************************************************************
294 MSVCRT_FILE *__p__iob(void)
296 return &MSVCRT__iob[0];
299 /*********************************************************************
302 int _access(const char *filename, int mode)
304 DWORD attr = GetFileAttributesA(filename);
306 TRACE("(%s,%d) %ld\n",filename,mode,attr);
308 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
310 MSVCRT__set_errno(GetLastError());
313 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
315 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
321 /*********************************************************************
322 * _waccess (MSVCRT.@)
324 int _waccess(const MSVCRT_wchar_t *filename, int mode)
326 DWORD attr = GetFileAttributesW(filename);
328 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
330 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
332 MSVCRT__set_errno(GetLastError());
335 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
337 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
343 /*********************************************************************
346 int _chmod(const char *path, int flags)
348 DWORD oldFlags = GetFileAttributesA(path);
350 if (oldFlags != INVALID_FILE_ATTRIBUTES)
352 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
353 oldFlags | FILE_ATTRIBUTE_READONLY;
355 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
358 MSVCRT__set_errno(GetLastError());
362 /*********************************************************************
365 int _wchmod(const MSVCRT_wchar_t *path, int flags)
367 DWORD oldFlags = GetFileAttributesW(path);
369 if (oldFlags != INVALID_FILE_ATTRIBUTES)
371 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
372 oldFlags | FILE_ATTRIBUTE_READONLY;
374 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
377 MSVCRT__set_errno(GetLastError());
381 /*********************************************************************
384 int _chsize(int fd, long size)
386 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
390 /*********************************************************************
395 FIXME("(od=%d): stub\n", od);
399 /*********************************************************************
402 int _dup2(int od, int nd)
404 FIXME("(od=%d, nd=%d): stub\n", od, nd);
408 /*********************************************************************
411 int _unlink(const char *path)
413 TRACE("(%s)\n",path);
414 if(DeleteFileA(path))
416 TRACE("failed (%ld)\n",GetLastError());
417 MSVCRT__set_errno(GetLastError());
421 /*********************************************************************
422 * _wunlink (MSVCRT.@)
424 int _wunlink(const MSVCRT_wchar_t *path)
426 TRACE("(%s)\n",debugstr_w(path));
427 if(DeleteFileW(path))
429 TRACE("failed (%ld)\n",GetLastError());
430 MSVCRT__set_errno(GetLastError());
434 /*********************************************************************
439 HANDLE hand = msvcrt_fdtoh(fd);
441 TRACE(":fd (%d) handle (%p)\n",fd,hand);
442 if (hand == INVALID_HANDLE_VALUE)
444 /* flush stdio buffers */
445 if(MSVCRT_files[fd]) {
446 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
447 MSVCRT_fflush(MSVCRT_files[fd]);
449 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
450 MSVCRT_free(MSVCRT_files[fd]->_base);
453 /* Dont free std FILE*'s, they are not dynamic */
454 if (fd > 2 && MSVCRT_files[fd])
455 MSVCRT_free(MSVCRT_files[fd]);
459 if (!CloseHandle(hand))
461 WARN(":failed-last error (%ld)\n",GetLastError());
462 MSVCRT__set_errno(GetLastError());
465 if (MSVCRT_tempfiles[fd])
467 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
468 _unlink(MSVCRT_tempfiles[fd]);
469 MSVCRT_free(MSVCRT_tempfiles[fd]);
470 MSVCRT_tempfiles[fd] = NULL;
477 /*********************************************************************
482 HANDLE hand = msvcrt_fdtoh(fd);
484 TRACE(":fd (%d) handle (%p)\n",fd,hand);
485 if (hand == INVALID_HANDLE_VALUE)
488 if (!FlushFileBuffers(hand))
490 if (GetLastError() == ERROR_INVALID_HANDLE)
492 /* FlushFileBuffers fails for console handles
493 * so we ignore this error.
497 TRACE(":failed-last error (%ld)\n",GetLastError());
498 MSVCRT__set_errno(GetLastError());
505 /*********************************************************************
511 HANDLE hand = msvcrt_fdtoh(fd);
513 TRACE(":fd (%d) handle (%p)\n",fd,hand);
515 if (hand == INVALID_HANDLE_VALUE)
518 /* If we have a FILE* for this file, the EOF flag
519 * will be set by the read()/write() functions.
521 if (MSVCRT_files[fd])
522 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
524 /* Otherwise we do it the hard way */
525 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
526 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
528 if (curpos == endpos)
531 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
535 /*********************************************************************
536 * _fcloseall (MSVCRT.@)
540 int num_closed = 0, i;
542 for (i = 3; i < MSVCRT_fdend; i++)
543 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
549 TRACE(":closed (%d) handles\n",num_closed);
553 /*********************************************************************
556 __int64 _lseeki64(int fd, __int64 offset, int whence)
558 DWORD ret, hoffset = (DWORD) (offset >> 32);
559 HANDLE hand = msvcrt_fdtoh(fd);
561 TRACE(":fd (%d) handle (%p)\n",fd,hand);
562 if (hand == INVALID_HANDLE_VALUE)
565 if (whence < 0 || whence > 2)
567 *MSVCRT__errno() = MSVCRT_EINVAL;
571 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
572 fd,hoffset,(long)offset,
573 (whence==SEEK_SET)?"SEEK_SET":
574 (whence==SEEK_CUR)?"SEEK_CUR":
575 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
577 if (((ret = SetFilePointer(hand, (long)offset, &hoffset,
578 whence)) != INVALID_SET_FILE_POINTER) || !GetLastError())
580 if (MSVCRT_files[fd])
581 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
582 /* FIXME: What if we seek _to_ EOF - is EOF set? */
584 return ((__int64)hoffset << 32) | ret;
586 TRACE(":error-last error (%ld)\n",GetLastError());
587 if (MSVCRT_files[fd])
588 switch(GetLastError())
590 case ERROR_NEGATIVE_SEEK:
591 case ERROR_SEEK_ON_DEVICE:
592 MSVCRT__set_errno(GetLastError());
593 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
601 /*********************************************************************
604 LONG _lseek(int fd, LONG offset, int whence)
606 return _lseeki64(fd, offset, whence);
609 /*********************************************************************
610 * _locking (MSVCRT.@)
612 * This is untested; the underlying LockFile doesn't work yet.
614 int _locking(int fd, int mode, LONG nbytes)
618 HANDLE hand = msvcrt_fdtoh(fd);
620 TRACE(":fd (%d) handle (%p)\n",fd,hand);
621 if (hand == INVALID_HANDLE_VALUE)
624 if (mode < 0 || mode > 4)
626 *MSVCRT__errno() = MSVCRT_EINVAL;
630 TRACE(":fd (%d) by 0x%08lx mode %s\n",
631 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
632 (mode==_LK_LOCK)?"_LK_LOCK":
633 (mode==_LK_NBLCK)?"_LK_NBLCK":
634 (mode==_LK_RLCK)?"_LK_RLCK":
635 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
638 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
640 FIXME ("Seek failed\n");
641 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
644 if (mode == _LK_LOCK || mode == _LK_RLCK)
647 ret = 1; /* just to satisfy gcc */
650 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
655 else if (mode == _LK_UNLCK)
656 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
658 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
659 /* FIXME - what about error settings? */
663 /*********************************************************************
666 void MSVCRT_rewind(MSVCRT_FILE* file)
668 TRACE(":file (%p) fd (%d)\n",file,file->_file);
669 MSVCRT_fseek(file, 0L, SEEK_SET);
670 MSVCRT_clearerr(file);
673 /*********************************************************************
676 MSVCRT_FILE* _fdopen(int fd, const char *mode)
678 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
680 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
685 /*********************************************************************
686 * _wfdopen (MSVCRT.@)
688 MSVCRT_FILE* _wfdopen(int fd, const MSVCRT_wchar_t *mode)
690 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
692 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
699 /*********************************************************************
700 * _filelength (MSVCRT.@)
702 LONG _filelength(int fd)
704 LONG curPos = _lseek(fd, 0, SEEK_CUR);
707 LONG endPos = _lseek(fd, 0, SEEK_END);
710 if (endPos != curPos)
711 _lseek(fd, curPos, SEEK_SET);
718 /*********************************************************************
721 int _fileno(MSVCRT_FILE* file)
723 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
727 /*********************************************************************
728 * _flushall (MSVCRT.@)
732 int num_flushed = 0, i = 3;
734 while(i < MSVCRT_fdend)
735 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
738 /* FIXME: flush, do not commit */
739 if (_commit(i) == -1)
741 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
743 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
744 MSVCRT_fflush(MSVCRT_files[i]);
749 TRACE(":flushed (%d) handles\n",num_flushed);
753 /*********************************************************************
754 * _fstati64 (MSVCRT.@)
756 int _fstati64(int fd, struct _stati64* buf)
759 BY_HANDLE_FILE_INFORMATION hfi;
760 HANDLE hand = msvcrt_fdtoh(fd);
762 TRACE(":fd (%d) stat (%p)\n",fd,buf);
763 if (hand == INVALID_HANDLE_VALUE)
768 WARN(":failed-NULL buf\n");
769 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
773 memset(&hfi, 0, sizeof(hfi));
774 memset(buf, 0, sizeof(struct _stati64));
775 if (!GetFileInformationByHandle(hand, &hfi))
777 WARN(":failed-last error (%ld)\n",GetLastError());
778 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
781 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
782 buf->st_nlink = hfi.nNumberOfLinks;
783 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
784 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
786 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
787 buf->st_mtime = buf->st_ctime = dw;
791 /*********************************************************************
794 int MSVCRT__fstat(int fd, struct _stat* buf)
796 struct _stati64 bufi64;
798 ret = _fstati64(fd, &bufi64);
800 msvcrt_cp_from_stati64(&bufi64, buf);
804 /*********************************************************************
807 int _futime(int fd, struct _utimbuf *t)
809 HANDLE hand = msvcrt_fdtoh(fd);
814 MSVCRT_time_t currTime;
815 MSVCRT_time(&currTime);
816 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
817 memcpy(&wt, &at, sizeof(wt));
821 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
822 if (t->actime == t->modtime)
823 memcpy(&wt, &at, sizeof(wt));
825 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
828 if (!SetFileTime(hand, NULL, &at, &wt))
830 MSVCRT__set_errno(GetLastError());
836 /*********************************************************************
837 * _get_osfhandle (MSVCRT.@)
839 long _get_osfhandle(int fd)
841 HANDLE hand = msvcrt_fdtoh(fd);
842 HANDLE newhand = hand;
843 TRACE(":fd (%d) handle (%p)\n",fd,hand);
845 if (hand != INVALID_HANDLE_VALUE)
847 /* FIXME: I'm not convinced that I should be copying the
848 * handle here - it may be leaked if the app doesn't
849 * close it (and the API docs dont say that it should)
850 * Not duplicating it means that it can't be inherited
851 * and so lcc's wedit doesn't cope when it passes it to
852 * child processes. I've an idea that it should either
853 * be copied by CreateProcess, or marked as inheritable
854 * when initialised, or maybe both? JG 21-9-00.
856 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
857 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
859 return (long)newhand;
862 /*********************************************************************
867 HANDLE hand = msvcrt_fdtoh(fd);
869 TRACE(":fd (%d) handle (%p)\n",fd,hand);
870 if (hand == INVALID_HANDLE_VALUE)
873 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
876 /*********************************************************************
879 char *_mktemp(char *pattern)
882 char *retVal = pattern;
887 numX = (*pattern++ == 'X')? numX + 1 : 0;
891 id = GetCurrentProcessId();
895 int tempNum = id / 10;
896 *pattern-- = id - (tempNum * 10) + '0';
902 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
903 GetLastError() == ERROR_FILE_NOT_FOUND)
906 } while(letter != '|');
910 /*********************************************************************
911 * _wmktemp (MSVCRT.@)
913 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
916 MSVCRT_wchar_t *retVal = pattern;
918 MSVCRT_wchar_t letter = 'a';
921 numX = (*pattern++ == 'X')? numX + 1 : 0;
925 id = GetCurrentProcessId();
929 int tempNum = id / 10;
930 *pattern-- = id - (tempNum * 10) + '0';
936 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
937 GetLastError() == ERROR_FILE_NOT_FOUND)
940 } while(letter != '|');
944 /*********************************************************************
947 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
951 DWORD access = 0, creation = 0;
955 SECURITY_ATTRIBUTES sa;
958 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
959 path, oflags, shflags);
961 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
964 access |= GENERIC_READ;
965 ioflag |= MSVCRT__IOREAD;
968 access |= GENERIC_WRITE;
969 ioflag |= MSVCRT__IOWRT;
972 access |= GENERIC_WRITE | GENERIC_READ;
973 ioflag |= MSVCRT__IORW;
977 if (oflags & _O_CREAT)
979 va_start(ap, shflags);
980 pmode = va_arg(ap, int);
983 if(pmode & ~(_S_IREAD | _S_IWRITE))
984 FIXME(": pmode 0x%04x ignored\n", pmode);
986 WARN(": pmode 0x%04x ignored\n", pmode);
988 if (oflags & _O_EXCL)
989 creation = CREATE_NEW;
990 else if (oflags & _O_TRUNC)
991 creation = CREATE_ALWAYS;
993 creation = OPEN_ALWAYS;
995 else /* no _O_CREAT */
997 if (oflags & _O_TRUNC)
998 creation = TRUNCATE_EXISTING;
1000 creation = OPEN_EXISTING;
1002 if (oflags & _O_APPEND)
1003 ioflag |= MSVCRT__IOAPPEND;
1005 if (oflags & _O_BINARY)
1006 ioflag |= _O_BINARY;
1007 else if (oflags & _O_TEXT)
1009 else if (*__p__fmode() & _O_BINARY)
1010 ioflag |= _O_BINARY;
1012 ioflag |= _O_TEXT; /* default to TEXT*/
1020 sharing = FILE_SHARE_READ;
1023 sharing = FILE_SHARE_WRITE;
1026 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1029 ERR( "Unhandled shflags 0x%x\n", shflags );
1033 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
1034 |_O_CREAT|_O_RDWR|_O_WRONLY|_O_TEMPORARY|_O_NOINHERIT))
1035 ERR(":unsupported oflags 0x%04x\n",oflags);
1037 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1038 sa.lpSecurityDescriptor = NULL;
1039 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
1041 hand = CreateFileA(path, access, sharing,
1042 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
1044 if (hand == INVALID_HANDLE_VALUE) {
1045 WARN(":failed-last error (%ld)\n",GetLastError());
1046 MSVCRT__set_errno(GetLastError());
1050 fd = msvcrt_alloc_fd(hand, ioflag);
1052 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1056 if (oflags & _O_TEMPORARY)
1057 MSVCRT_tempfiles[fd] = _strdup(path);
1058 if (ioflag & MSVCRT__IOAPPEND)
1059 _lseek(fd, 0, FILE_END);
1065 /*********************************************************************
1066 * _wsopen (MSVCRT.@)
1068 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1070 const unsigned int len = strlenW(path);
1071 char *patha = MSVCRT_calloc(len + 1,1);
1075 va_start(ap, shflags);
1076 pmode = va_arg(ap, int);
1079 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1081 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1086 MSVCRT__set_errno(GetLastError());
1090 /*********************************************************************
1093 int _open( const char *path, int flags, ... )
1097 if (flags & _O_CREAT)
1100 va_start(ap, flags);
1101 pmode = va_arg(ap, int);
1103 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1106 return MSVCRT__sopen( path, flags, _SH_DENYNO);
1109 /*********************************************************************
1112 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1114 const unsigned int len = strlenW(path);
1115 char *patha = MSVCRT_calloc(len + 1,1);
1119 va_start(ap, flags);
1120 pmode = va_arg(ap, int);
1123 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1125 int retval = _open(patha,flags,pmode);
1130 MSVCRT__set_errno(GetLastError());
1134 /*********************************************************************
1137 int _creat(const char *path, int flags)
1139 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1140 return _open(path, usedFlags);
1143 /*********************************************************************
1144 * _wcreat (MSVCRT.@)
1146 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1148 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1149 return _wopen(path, usedFlags);
1152 /*********************************************************************
1153 * _open_osfhandle (MSVCRT.@)
1155 int _open_osfhandle(long hand, int flags)
1159 /* _O_RDONLY (0) always matches, so set the read flag
1160 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1161 * file, so set the write flag. It also only sets _O_TEXT if it wants
1162 * text - it never sets _O_BINARY.
1164 /* FIXME: handle more flags */
1165 flags |= MSVCRT__IOREAD|MSVCRT__IOWRT;
1166 if ( !( flags & _O_TEXT ) ) flags |= _O_BINARY;
1168 fd = msvcrt_alloc_fd((HANDLE)hand,flags);
1169 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd, flags);
1173 /*********************************************************************
1178 int num_removed = 0, i;
1180 for (i = 3; i < MSVCRT_fdend; i++)
1181 if (MSVCRT_tempfiles[i])
1188 TRACE(":removed (%d) temp files\n",num_removed);
1192 /*********************************************************************
1193 * (internal) remove_cr
1195 * Remove all \r inplace.
1196 * return the number of \r removed
1198 static unsigned int remove_cr(char *buf, unsigned int count)
1202 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1203 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1207 /*********************************************************************
1210 int _read(int fd, void *buf, unsigned int count)
1212 DWORD num_read, all_read =0;
1213 char *bufstart = buf;
1214 HANDLE hand = msvcrt_fdtoh(fd);
1216 /* Dont trace small reads, it gets *very* annoying */
1218 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1219 if (hand == INVALID_HANDLE_VALUE)
1222 /* Reading single bytes in O_TEXT mode makes things slow
1223 * So read big chunks, then remove the \r in memory and try reading
1224 * the rest until the request is satisfied or EOF is met
1226 while ( all_read < count)
1228 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1230 if (num_read != (count- all_read))
1233 if ( MSVCRT_files[fd])
1235 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1237 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1240 if ((MSVCRT_flags[fd]& _O_BINARY ) != _O_BINARY )
1241 num_read -= remove_cr(bufstart+all_read,num_read);
1242 all_read += num_read;
1244 TRACE("%s\n",debugstr_an(buf,all_read));
1247 if ((MSVCRT_flags[fd]& _O_BINARY ) != _O_BINARY )
1249 num_read -= remove_cr(bufstart+all_read,num_read);
1251 all_read += num_read;
1255 TRACE(":failed-last error (%ld)\n",GetLastError());
1256 if (MSVCRT_files[fd])
1257 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1263 TRACE("%s\n",debugstr_an(buf, all_read));
1267 /*********************************************************************
1270 int _getw(MSVCRT_FILE* file)
1273 if (_read(file->_file, &i, sizeof(int)) != 1)
1278 /*********************************************************************
1279 * _setmode (MSVCRT.@)
1281 int _setmode(int fd,int mode)
1283 int ret = MSVCRT_flags[fd] & (_O_TEXT | _O_BINARY);
1284 if (mode & (~(_O_TEXT|_O_BINARY)))
1285 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1286 MSVCRT_flags[fd] &= ~(_O_TEXT|_O_BINARY);
1287 MSVCRT_flags[fd] |= mode & (_O_TEXT | _O_BINARY);
1291 /*********************************************************************
1292 * _stati64 (MSVCRT.@)
1294 int _stati64(const char* path, struct _stati64 * buf)
1297 WIN32_FILE_ATTRIBUTE_DATA hfi;
1298 unsigned short mode = MSVCRT_S_IREAD;
1301 TRACE(":file (%s) buf(%p)\n",path,buf);
1303 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1305 TRACE("failed (%ld)\n",GetLastError());
1306 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1310 memset(buf,0,sizeof(struct _stati64));
1312 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1313 Bon 011120: This FIXME seems incorrect
1314 Also a letter as first char isn't enough to be classify
1317 if (isalpha(*path)&& (*(path+1)==':'))
1318 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1320 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1322 plen = strlen(path);
1324 /* Dir, or regular file? */
1325 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1326 (path[plen-1] == '\\'))
1327 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1332 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1334 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1335 (tolower(path[plen-3]) << 16);
1336 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1337 mode |= MSVCRT_S_IEXEC;
1341 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1342 mode |= MSVCRT_S_IWRITE;
1344 buf->st_mode = mode;
1346 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1347 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1349 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1350 buf->st_mtime = buf->st_ctime = dw;
1351 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1352 (long)(buf->st_size >> 32),(long)buf->st_size,
1353 buf->st_atime,buf->st_mtime, buf->st_ctime);
1357 /*********************************************************************
1360 int MSVCRT__stat(const char* path, struct _stat * buf)
1362 struct _stati64 bufi64;
1364 ret = _stati64( path, &bufi64);
1366 msvcrt_cp_from_stati64(&bufi64, buf);
1370 /*********************************************************************
1373 int _wstat(const MSVCRT_wchar_t* path, struct _stat * buf)
1376 WIN32_FILE_ATTRIBUTE_DATA hfi;
1377 unsigned short mode = MSVCRT_S_IREAD;
1380 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1382 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1384 TRACE("failed (%ld)\n",GetLastError());
1385 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1389 memset(buf,0,sizeof(struct _stat));
1391 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1392 if (MSVCRT_iswalpha(*path))
1393 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1395 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1397 plen = strlenW(path);
1399 /* Dir, or regular file? */
1400 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1401 (path[plen-1] == '\\'))
1402 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1407 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1409 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1410 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1411 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1412 mode |= MSVCRT_S_IEXEC;
1416 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1417 mode |= MSVCRT_S_IWRITE;
1419 buf->st_mode = mode;
1421 buf->st_size = hfi.nFileSizeLow;
1422 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1424 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1425 buf->st_mtime = buf->st_ctime = dw;
1426 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1427 buf->st_atime,buf->st_mtime, buf->st_ctime);
1431 /*********************************************************************
1436 return _lseek(fd, 0, SEEK_CUR);
1439 /*********************************************************************
1440 * _tempnam (MSVCRT.@)
1442 char *_tempnam(const char *dir, const char *prefix)
1444 char tmpbuf[MAX_PATH];
1446 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1447 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1449 TRACE("got name (%s)\n",tmpbuf);
1450 DeleteFileA(tmpbuf);
1451 return _strdup(tmpbuf);
1453 TRACE("failed (%ld)\n",GetLastError());
1457 /*********************************************************************
1458 * _wtempnam (MSVCRT.@)
1460 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1462 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1464 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1465 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1467 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1468 DeleteFileW(tmpbuf);
1469 return _wcsdup(tmpbuf);
1471 TRACE("failed (%ld)\n",GetLastError());
1475 /*********************************************************************
1478 int _umask(int umask)
1480 int old_umask = MSVCRT_umask;
1481 TRACE("(%d)\n",umask);
1482 MSVCRT_umask = umask;
1486 /*********************************************************************
1489 int _utime(const char* path, struct _utimbuf *t)
1491 int fd = _open(path, _O_WRONLY | _O_BINARY);
1495 int retVal = _futime(fd, t);
1502 /*********************************************************************
1503 * _wutime (MSVCRT.@)
1505 int _wutime(const MSVCRT_wchar_t* path, struct _utimbuf *t)
1507 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1511 int retVal = _futime(fd, t);
1518 /*********************************************************************
1521 int _write(int fd, const void* buf, unsigned int count)
1524 HANDLE hand = msvcrt_fdtoh(fd);
1526 /* Dont trace small writes, it gets *very* annoying */
1529 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1531 if (hand == INVALID_HANDLE_VALUE)
1533 *MSVCRT__errno() = MSVCRT_EBADF;
1537 /* If appending, go to EOF */
1538 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1539 _lseek(fd, 0, FILE_END);
1541 if (MSVCRT_flags[fd] & _O_BINARY)
1543 if (WriteFile(hand, buf, count, &num_written, NULL)
1544 && (num_written == count))
1546 TRACE(":failed-last error (%ld)\n",GetLastError());
1547 if (MSVCRT_files[fd])
1549 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1550 *MSVCRT__errno() = MSVCRT_ENOSPC;
1555 unsigned int i, j, nr_lf;
1556 char *s=(char*)buf, *buf_start=(char*)buf, *p;
1557 /* find number of \n ( without preceeding \r */
1558 for ( nr_lf=0,i = 0; i <count; i++)
1563 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1568 if ((p = MSVCRT_malloc(count + nr_lf)))
1570 for(s=(char*)buf, i=0, j=0; i<count; i++)
1575 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1582 FIXME("Malloc failed\n");
1590 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
1592 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
1593 if (MSVCRT_files[fd])
1595 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1596 *MSVCRT__errno() = MSVCRT_ENOSPC;
1599 return s - buf_start;
1612 /*********************************************************************
1615 int _putw(int val, MSVCRT_FILE* file)
1617 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1620 /*********************************************************************
1621 * clearerr (MSVCRT.@)
1623 void MSVCRT_clearerr(MSVCRT_FILE* file)
1625 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1626 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1629 /*********************************************************************
1632 int MSVCRT_fclose(MSVCRT_FILE* file)
1637 r=_close(file->_file);
1638 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1641 /*********************************************************************
1644 int MSVCRT_feof(MSVCRT_FILE* file)
1646 return file->_flag & MSVCRT__IOEOF;
1649 /*********************************************************************
1652 int MSVCRT_ferror(MSVCRT_FILE* file)
1654 return file->_flag & MSVCRT__IOERR;
1657 /*********************************************************************
1660 int MSVCRT_fflush(MSVCRT_FILE* file)
1666 int res=msvcrt_flush_buffer(file);
1671 /*********************************************************************
1674 int MSVCRT_fgetc(MSVCRT_FILE* file)
1678 return *(unsigned char *)file->_ptr++;
1680 return _filbuf(file);
1684 /*********************************************************************
1685 * _fgetchar (MSVCRT.@)
1689 return MSVCRT_fgetc(MSVCRT_stdin);
1692 /*********************************************************************
1693 * _filbuf (MSVCRT.@)
1695 int _filbuf(MSVCRT_FILE* file)
1698 /* Allocate buffer if needed */
1699 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1700 msvcrt_alloc_buffer(file);
1702 if(!(file->_flag & MSVCRT__IOREAD)) {
1703 if(file->_flag & MSVCRT__IORW) {
1704 file->_flag |= MSVCRT__IOREAD;
1709 if(file->_flag & MSVCRT__IONBF) {
1711 if (_read(file->_file,&c,1) != 1) {
1712 file->_flag |= MSVCRT__IOEOF;
1717 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1718 if(file->_cnt<0) file->_cnt = 0;
1720 file->_flag |= MSVCRT__IOEOF;
1724 file->_ptr = file->_base+1;
1725 return *(unsigned char *)file->_base;
1729 /*********************************************************************
1730 * fgetpos (MSVCRT.@)
1732 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1734 *pos = MSVCRT_ftell(file);
1735 return (*pos == -1? -1 : 0);
1738 /*********************************************************************
1741 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1743 int cc = MSVCRT_EOF;
1744 char * buf_start = s;
1746 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1747 file,file->_file,s,size);
1749 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
1754 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1756 TRACE(":nothing read\n");
1759 if ((cc != MSVCRT_EOF) && (size > 1))
1762 TRACE(":got '%s'\n", debugstr_a(buf_start));
1766 /*********************************************************************
1769 * In _O_TEXT mode, bultibyte characters are read from the file, dropping
1770 * the CR from CR/LF combinations
1772 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1776 if (file->_flag & _O_BINARY)
1779 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1783 c = MSVCRT_fgetc(file);
1784 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
1786 FIXME("Treat Multibyte characters\n");
1788 if (c == MSVCRT_EOF)
1791 return (MSVCRT_wint_t)c;
1794 /*********************************************************************
1797 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1799 return MSVCRT_fgetwc(file);
1802 /*********************************************************************
1803 * _fgetwchar (MSVCRT.@)
1805 MSVCRT_wint_t _fgetwchar(void)
1807 return MSVCRT_fgetwc(MSVCRT_stdin);
1810 /*********************************************************************
1811 * getwchar (MSVCRT.@)
1813 MSVCRT_wint_t MSVCRT_getwchar(void)
1815 return _fgetwchar();
1818 /*********************************************************************
1821 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
1823 int cc = MSVCRT_WEOF;
1824 MSVCRT_wchar_t * buf_start = s;
1826 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1827 file,file->_file,s,size);
1829 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
1834 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
1836 TRACE(":nothing read\n");
1839 if ((cc != MSVCRT_WEOF) && (size > 1))
1842 TRACE(":got %s\n", debugstr_w(buf_start));
1847 /*********************************************************************
1850 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1852 MSVCRT_wchar_t mwc=wc;
1853 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
1858 /*********************************************************************
1859 * _fputwchar (MSVCRT.@)
1861 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1863 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1866 /*********************************************************************
1869 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1872 int flags = 0, plus = 0, fd;
1873 const char* search = mode;
1875 TRACE("(%s,%s)\n",path,mode);
1878 if (*search++ == '+')
1881 /* map mode string to open() flags. "man fopen" for possibilities. */
1885 flags = (plus ? _O_RDWR : _O_RDONLY);
1888 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1891 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1906 flags &= ~_O_BINARY;
1911 FIXME(":unknown flag %c not supported\n",mode[-1]);
1914 fd = _open(path, flags, _S_IREAD | _S_IWRITE);
1919 file = msvcrt_alloc_fp(fd);
1920 TRACE(":got (%p)\n",file);
1927 /*********************************************************************
1928 * _wfopen (MSVCRT.@)
1930 MSVCRT_FILE *_wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
1932 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1933 char *patha = MSVCRT_calloc(plen + 1, 1);
1934 char *modea = MSVCRT_calloc(mlen + 1, 1);
1936 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1938 if (patha && modea &&
1939 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1940 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1942 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1948 MSVCRT__set_errno(GetLastError());
1952 /*********************************************************************
1953 * _fsopen (MSVCRT.@)
1955 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1957 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1958 return MSVCRT_fopen(path,mode);
1961 /*********************************************************************
1962 * _wfsopen (MSVCRT.@)
1964 MSVCRT_FILE* _wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
1966 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1967 debugstr_w(path),debugstr_w(mode),share);
1968 return _wfopen(path,mode);
1971 /*********************************************************************
1974 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1981 return _flsbuf(c, file);
1985 /*********************************************************************
1986 * _flsbuf (MSVCRT.@)
1988 int _flsbuf(int c, MSVCRT_FILE* file)
1990 /* Flush output buffer */
1991 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
1992 msvcrt_alloc_buffer(file);
1994 if(!(file->_flag & MSVCRT__IOWRT)) {
1995 if(file->_flag & MSVCRT__IORW) {
1996 file->_flag |= MSVCRT__IOWRT;
2002 int res=msvcrt_flush_buffer(file);
2003 return res?res : MSVCRT_fputc(c, file);
2006 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
2010 /*********************************************************************
2011 * _fputchar (MSVCRT.@)
2013 int _fputchar(int c)
2015 return MSVCRT_fputc(c, MSVCRT_stdout);
2018 /*********************************************************************
2021 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2022 { MSVCRT_size_t rcnt=size * nmemb;
2023 MSVCRT_size_t read=0;
2025 /* first buffered data */
2027 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2028 memcpy(ptr, file->_ptr, pcnt);
2033 ptr = (char*)ptr + pcnt;
2034 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2035 if(file->_flag & MSVCRT__IORW) {
2036 file->_flag |= MSVCRT__IOREAD;
2040 if(rcnt) pread = _read(file->_file,ptr, rcnt);
2041 if (MSVCRT_flags[file->_file] & MSVCRT__IOEOF)
2042 /* expose feof condition in the flags
2043 MFC tests file->_flag for feof, and doesn't not call feof())
2045 file->_flag |= MSVCRT__IOEOF;
2052 /*********************************************************************
2053 * freopen (MSVCRT.@)
2056 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2058 MSVCRT_FILE* newfile;
2061 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2062 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2068 FIXME(":reopen on user file not implemented!\n");
2069 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
2072 if(MSVCRT_fclose(file))
2074 return MSVCRT_fopen(path, mode);
2077 /* first, create the new file */
2078 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
2081 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
2082 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
2083 MSVCRT_handles[newfile->_file]))
2085 /* Redirecting std handle to file , copy over.. */
2086 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
2087 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
2088 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
2089 MSVCRT__iob[fd]._file = fd;
2090 /* And free up the resources allocated by fopen, but
2091 * not the HANDLE we copied. */
2092 MSVCRT_free(MSVCRT_files[fd]);
2093 msvcrt_free_fd(newfile->_file);
2094 return &MSVCRT__iob[fd];
2097 WARN(":failed-last error (%ld)\n",GetLastError());
2098 MSVCRT_fclose(newfile);
2099 MSVCRT__set_errno(GetLastError());
2103 /*********************************************************************
2104 * fsetpos (MSVCRT.@)
2106 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2108 return _lseek(file->_file,*pos,SEEK_SET);
2111 /*********************************************************************
2114 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
2116 /* Flush output if needed */
2117 if(file->_flag & MSVCRT__IOWRT)
2118 msvcrt_flush_buffer(file);
2120 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
2121 offset -= file->_cnt;
2123 /* Discard buffered input */
2125 file->_ptr = file->_base;
2126 /* Reset direction of i/o */
2127 if(file->_flag & MSVCRT__IORW) {
2128 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2130 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
2133 /*********************************************************************
2136 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2141 if( file->_flag & MSVCRT__IOWRT ) {
2142 off = file->_ptr - file->_base;
2147 pos = _tell(file->_file);
2148 if(pos == -1) return pos;
2152 /*********************************************************************
2155 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2157 MSVCRT_size_t wrcnt=size * nmemb;
2162 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2163 memcpy(file->_ptr, ptr, pcnt);
2168 ptr = (char*)ptr + pcnt;
2169 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2170 if(file->_flag & MSVCRT__IORW) {
2171 file->_flag |= MSVCRT__IOWRT;
2177 int res=msvcrt_flush_buffer(file);
2179 int pwritten = _write(file->_file, ptr, wrcnt);
2180 if (pwritten <= 0) pwritten=0;
2181 written += pwritten;
2184 return written / size;
2187 /*********************************************************************
2190 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2192 size_t i, len = strlen(s);
2193 if (file->_flag & _O_BINARY)
2194 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2195 for (i=0; i<len; i++)
2196 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2201 /*********************************************************************
2204 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2206 size_t i, len = strlenW(s);
2207 if (file->_flag & _O_BINARY)
2208 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2209 for (i=0; i<len; i++)
2211 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2213 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2219 /*********************************************************************
2220 * getchar (MSVCRT.@)
2222 int MSVCRT_getchar(void)
2224 return MSVCRT_fgetc(MSVCRT_stdin);
2227 /*********************************************************************
2230 int MSVCRT_getc(MSVCRT_FILE* file)
2232 return MSVCRT_fgetc(file);
2235 /*********************************************************************
2238 char *MSVCRT_gets(char *buf)
2241 char * buf_start = buf;
2243 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2244 cc = MSVCRT_fgetc(MSVCRT_stdin))
2245 if(cc != '\r') *buf++ = (char)cc;
2249 TRACE("got '%s'\n", buf_start);
2253 /*********************************************************************
2256 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2259 MSVCRT_wchar_t* ws = buf;
2261 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2262 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2265 *buf++ = (MSVCRT_wchar_t)cc;
2269 TRACE("got '%s'\n", debugstr_w(ws));
2273 /*********************************************************************
2276 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2278 return MSVCRT_fputc(c, file);
2281 /*********************************************************************
2282 * putchar (MSVCRT.@)
2284 int MSVCRT_putchar(int c)
2286 return MSVCRT_fputc(c, MSVCRT_stdout);
2289 /*********************************************************************
2292 int MSVCRT_puts(const char *s)
2294 size_t len = strlen(s);
2295 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2296 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2299 /*********************************************************************
2302 int _putws(const MSVCRT_wchar_t *s)
2304 static const MSVCRT_wchar_t nl = '\n';
2305 size_t len = strlenW(s);
2306 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2307 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2310 /*********************************************************************
2313 int MSVCRT_remove(const char *path)
2315 TRACE("(%s)\n",path);
2316 if (DeleteFileA(path))
2318 TRACE(":failed (%ld)\n",GetLastError());
2319 MSVCRT__set_errno(GetLastError());
2323 /*********************************************************************
2324 * _wremove (MSVCRT.@)
2326 int _wremove(const MSVCRT_wchar_t *path)
2328 TRACE("(%s)\n",debugstr_w(path));
2329 if (DeleteFileW(path))
2331 TRACE(":failed (%ld)\n",GetLastError());
2332 MSVCRT__set_errno(GetLastError());
2336 /*********************************************************************
2339 int MSVCRT_scanf(const char *format, ...)
2344 va_start(valist, format);
2345 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
2350 /*********************************************************************
2353 int MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...)
2358 va_start(valist, format);
2359 res = MSVCRT_fwscanf(MSVCRT_stdin, format, valist);
2364 /*********************************************************************
2367 int MSVCRT_rename(const char *oldpath,const char *newpath)
2369 TRACE(":from %s to %s\n",oldpath,newpath);
2370 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2372 TRACE(":failed (%ld)\n",GetLastError());
2373 MSVCRT__set_errno(GetLastError());
2377 /*********************************************************************
2378 * _wrename (MSVCRT.@)
2380 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2382 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2383 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2385 TRACE(":failed (%ld)\n",GetLastError());
2386 MSVCRT__set_errno(GetLastError());
2390 /*********************************************************************
2391 * setvbuf (MSVCRT.@)
2393 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2395 /* TODO: Check if file busy */
2397 MSVCRT_free(file->_base);
2401 if(mode == MSVCRT__IOFBF) {
2402 file->_flag &= ~MSVCRT__IONBF;
2403 file->_base = file->_ptr = buf;
2405 file->_bufsiz = size;
2408 file->_flag |= MSVCRT__IONBF;
2413 /*********************************************************************
2416 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2418 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2421 /*********************************************************************
2424 char *MSVCRT_tmpnam(char *s)
2432 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2433 p = s + sprintf(s, "\\s%s.", tmpstr);
2434 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2436 msvcrt_int_to_base32(unique++, tmpstr);
2438 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2439 GetLastError() == ERROR_FILE_NOT_FOUND)
2445 /*********************************************************************
2446 * tmpfile (MSVCRT.@)
2448 MSVCRT_FILE* MSVCRT_tmpfile(void)
2450 char *filename = MSVCRT_tmpnam(NULL);
2452 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2454 return msvcrt_alloc_fp(fd);
2458 /*********************************************************************
2459 * vfprintf (MSVCRT.@)
2461 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2463 char buf[2048], *mem = buf;
2464 int written, resize = sizeof(buf), retval;
2465 /* There are two conventions for vsnprintf failing:
2466 * Return -1 if we truncated, or
2467 * Return the number of bytes that would have been written
2468 * The code below handles both cases
2470 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2473 resize = (written == -1 ? resize * 2 : written + 1);
2476 if (!(mem = (char *)MSVCRT_malloc(resize)))
2479 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2485 /*********************************************************************
2486 * vfwprintf (MSVCRT.@)
2488 * Is final char included in written (then resize is too big) or not
2489 * (then we must test for equality too)?
2491 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2493 MSVCRT_wchar_t buf[2048], *mem = buf;
2494 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2495 /* See vfprintf comments */
2496 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2499 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2502 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2505 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2511 /*********************************************************************
2512 * vprintf (MSVCRT.@)
2514 int MSVCRT_vprintf(const char *format, va_list valist)
2516 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2519 /*********************************************************************
2520 * vwprintf (MSVCRT.@)
2522 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2524 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2527 /*********************************************************************
2528 * fprintf (MSVCRT.@)
2530 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2534 va_start(valist, format);
2535 res = MSVCRT_vfprintf(file, format, valist);
2540 /*********************************************************************
2541 * fwprintf (MSVCRT.@)
2543 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2547 va_start(valist, format);
2548 res = MSVCRT_vfwprintf(file, format, valist);
2553 /*********************************************************************
2556 int MSVCRT_printf(const char *format, ...)
2560 va_start(valist, format);
2561 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2566 /*********************************************************************
2569 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2571 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2572 msvcrt_alloc_buffer(file);
2575 if(file->_ptr>file->_base) {
2584 /*********************************************************************
2585 * ungetwc (MSVCRT.@)
2587 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2589 MSVCRT_wchar_t mwc = wc;
2590 char * pp = (char *)&mwc;
2592 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2593 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2599 /*********************************************************************
2600 * wprintf (MSVCRT.@)
2602 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2606 va_start(valist, format);
2607 res = MSVCRT_vwprintf(format, valist);