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/fcntl.h"
43 #include "msvcrt/io.h"
44 #include "msvcrt/sys/locking.h"
45 #include "msvcrt/stdio.h"
46 #include "msvcrt/stdlib.h"
47 #include "msvcrt/string.h"
48 #include "msvcrt/sys/stat.h"
49 #include "msvcrt/sys/utime.h"
50 #include "msvcrt/time.h"
51 #include "msvcrt/share.h"
52 #include "msvcrt/wctype.h"
53 #include "msvcrt/direct.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];
77 static int MSVCRT_fdstart = 3; /* first unallocated fd */
78 static int MSVCRT_fdend = 3; /* highest allocated fd */
80 /* INTERNAL: process umask */
81 static int MSVCRT_umask = 0;
83 /* INTERNAL: Static buffer for temp file name */
84 static char MSVCRT_tmpname[MAX_PATH];
86 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
87 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
88 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
89 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
91 #define TOUL(x) (ULONGLONG)(x)
92 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
93 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
94 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
95 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
97 extern CRITICAL_SECTION MSVCRT_file_cs;
98 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
99 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
101 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64 *bufi64, struct MSVCRT__stat *buf)
103 buf->st_dev = bufi64->st_dev;
104 buf->st_ino = bufi64->st_ino;
105 buf->st_mode = bufi64->st_mode;
106 buf->st_nlink = bufi64->st_nlink;
107 buf->st_uid = bufi64->st_uid;
108 buf->st_gid = bufi64->st_gid;
109 buf->st_rdev = bufi64->st_rdev;
110 buf->st_size = bufi64->st_size;
111 buf->st_atime = bufi64->st_atime;
112 buf->st_mtime = bufi64->st_mtime;
113 buf->st_ctime = bufi64->st_ctime;
116 /* INTERNAL: Get the HANDLE for a fd */
117 static HANDLE msvcrt_fdtoh(int fd)
119 if (fd < 0 || fd >= MSVCRT_fdend ||
120 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
122 WARN(":fd (%d) - no handle!\n",fd);
123 *MSVCRT___doserrno() = 0;
124 *MSVCRT__errno() = MSVCRT_EBADF;
125 return INVALID_HANDLE_VALUE;
127 return MSVCRT_handles[fd];
130 /* INTERNAL: free a file entry fd */
131 static void msvcrt_free_fd(int fd)
133 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
134 MSVCRT_files[fd] = 0;
135 MSVCRT_flags[fd] = 0;
136 TRACE(":fd (%d) freed\n",fd);
138 return; /* don't use 0,1,2 for user files */
139 if (fd == MSVCRT_fdend - 1)
141 if (fd < MSVCRT_fdstart)
145 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
146 static int msvcrt_alloc_fd(HANDLE hand, int flag)
148 int fd = MSVCRT_fdstart;
150 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
151 if (fd >= MSVCRT_MAX_FILES)
153 WARN(":files exhausted!\n");
156 MSVCRT_handles[fd] = hand;
157 MSVCRT_flags[fd] = flag;
159 /* locate next free slot */
160 if (fd == MSVCRT_fdend)
161 MSVCRT_fdstart = ++MSVCRT_fdend;
163 while(MSVCRT_fdstart < MSVCRT_fdend &&
164 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
170 /* INTERNAL: Allocate a FILE* for an fd slot
171 * This is done lazily to avoid memory wastage for low level open/write
172 * usage when a FILE* is not requested (but may be later).
174 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
176 TRACE(":fd (%d) allocating FILE*\n",fd);
177 if (fd < 0 || fd >= MSVCRT_fdend ||
178 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
180 WARN(":invalid fd %d\n",fd);
181 *MSVCRT___doserrno() = 0;
182 *MSVCRT__errno() = MSVCRT_EBADF;
185 if (!MSVCRT_files[fd])
187 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
189 MSVCRT_files[fd]->_file = fd;
190 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
191 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
194 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
195 return MSVCRT_files[fd];
199 /* INTERNAL: Set up stdin, stderr and stdout */
200 void msvcrt_init_io(void)
203 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
204 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
205 GetCurrentProcess(), &MSVCRT_handles[0], 0, FALSE, DUPLICATE_SAME_ACCESS);
206 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
207 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
208 GetCurrentProcess(), &MSVCRT_handles[1], 0, FALSE, DUPLICATE_SAME_ACCESS);
209 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
210 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
211 GetCurrentProcess(), &MSVCRT_handles[2], 0, FALSE, DUPLICATE_SAME_ACCESS);
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 /* INTERNAL: Flush stdio file buffer */
227 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
230 int cnt=file->_ptr-file->_base;
231 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
234 file->_ptr=file->_base;
235 file->_cnt=file->_bufsiz;
240 /* INTERNAL: Allocate stdio file buffer */
241 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
243 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
245 file->_bufsiz = MSVCRT_BUFSIZ;
246 file->_flag |= MSVCRT__IOMYBUF;
248 file->_base = (unsigned char *)(&file->_charbuf);
250 file->_bufsiz = sizeof(file->_charbuf);
252 file->_ptr = file->_base;
256 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
257 static void msvcrt_int_to_base32(int num, char *str)
272 *p = (num & 31) + '0';
274 *p += ('a' - '0' - 10);
279 /*********************************************************************
282 MSVCRT_FILE *__p__iob(void)
284 return &MSVCRT__iob[0];
287 /*********************************************************************
290 int _access(const char *filename, int mode)
292 DWORD attr = GetFileAttributesA(filename);
294 TRACE("(%s,%d) %ld\n",filename,mode,attr);
296 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
298 MSVCRT__set_errno(GetLastError());
301 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
303 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
309 /*********************************************************************
310 * _waccess (MSVCRT.@)
312 int _waccess(const MSVCRT_wchar_t *filename, int mode)
314 DWORD attr = GetFileAttributesW(filename);
316 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
318 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
320 MSVCRT__set_errno(GetLastError());
323 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
325 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
331 /*********************************************************************
334 int _chmod(const char *path, int flags)
336 DWORD oldFlags = GetFileAttributesA(path);
338 if (oldFlags != INVALID_FILE_ATTRIBUTES)
340 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
341 oldFlags | FILE_ATTRIBUTE_READONLY;
343 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
346 MSVCRT__set_errno(GetLastError());
350 /*********************************************************************
353 int _wchmod(const MSVCRT_wchar_t *path, int flags)
355 DWORD oldFlags = GetFileAttributesW(path);
357 if (oldFlags != INVALID_FILE_ATTRIBUTES)
359 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
360 oldFlags | FILE_ATTRIBUTE_READONLY;
362 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
365 MSVCRT__set_errno(GetLastError());
369 /*********************************************************************
372 int _chsize(int fd, long size)
374 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
378 /*********************************************************************
383 FIXME("(od=%d): stub\n", od);
387 /*********************************************************************
390 int _dup2(int od, int nd)
392 FIXME("(od=%d, nd=%d): stub\n", od, nd);
396 /*********************************************************************
399 int _unlink(const char *path)
401 TRACE("(%s)\n",path);
402 if(DeleteFileA(path))
404 TRACE("failed (%ld)\n",GetLastError());
405 MSVCRT__set_errno(GetLastError());
409 /*********************************************************************
410 * _wunlink (MSVCRT.@)
412 int _wunlink(const MSVCRT_wchar_t *path)
414 TRACE("(%s)\n",debugstr_w(path));
415 if(DeleteFileW(path))
417 TRACE("failed (%ld)\n",GetLastError());
418 MSVCRT__set_errno(GetLastError());
422 /* _flushall calls MSVCRT_fflush which calls _flushall */
423 int MSVCRT_fflush(MSVCRT_FILE* file);
425 /*********************************************************************
426 * _flushall (MSVCRT.@)
430 int i, num_flushed = 0;
432 for (i = 3; i < MSVCRT_fdend; i++)
433 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
436 /* FIXME: flush, do not commit */
437 if (_commit(i) == -1)
439 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
441 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
442 MSVCRT_fflush(MSVCRT_files[i]);
447 TRACE(":flushed (%d) handles\n",num_flushed);
451 /*********************************************************************
454 int MSVCRT_fflush(MSVCRT_FILE* file)
460 int res=msvcrt_flush_buffer(file);
465 /*********************************************************************
470 HANDLE hand = msvcrt_fdtoh(fd);
472 TRACE(":fd (%d) handle (%p)\n",fd,hand);
473 if (hand == INVALID_HANDLE_VALUE)
475 /* flush stdio buffers */
476 if(MSVCRT_files[fd]) {
477 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
478 MSVCRT_fflush(MSVCRT_files[fd]);
480 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
481 MSVCRT_free(MSVCRT_files[fd]->_base);
484 /* Don't free std FILE*'s, they are not dynamic */
485 if (fd > 2 && MSVCRT_files[fd])
486 MSVCRT_free(MSVCRT_files[fd]);
490 if (!CloseHandle(hand))
492 WARN(":failed-last error (%ld)\n",GetLastError());
493 MSVCRT__set_errno(GetLastError());
496 if (MSVCRT_tempfiles[fd])
498 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
499 _unlink(MSVCRT_tempfiles[fd]);
500 MSVCRT_free(MSVCRT_tempfiles[fd]);
501 MSVCRT_tempfiles[fd] = NULL;
508 /*********************************************************************
513 HANDLE hand = msvcrt_fdtoh(fd);
515 TRACE(":fd (%d) handle (%p)\n",fd,hand);
516 if (hand == INVALID_HANDLE_VALUE)
519 if (!FlushFileBuffers(hand))
521 if (GetLastError() == ERROR_INVALID_HANDLE)
523 /* FlushFileBuffers fails for console handles
524 * so we ignore this error.
528 TRACE(":failed-last error (%ld)\n",GetLastError());
529 MSVCRT__set_errno(GetLastError());
536 /*********************************************************************
542 HANDLE hand = msvcrt_fdtoh(fd);
544 TRACE(":fd (%d) handle (%p)\n",fd,hand);
546 if (hand == INVALID_HANDLE_VALUE)
549 /* If we have a FILE* for this file, the EOF flag
550 * will be set by the read()/write() functions.
552 if (MSVCRT_files[fd])
553 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
555 /* Otherwise we do it the hard way */
556 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
557 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
559 if (curpos == endpos)
562 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
566 /*********************************************************************
567 * _fcloseall (MSVCRT.@)
569 int MSVCRT__fcloseall(void)
571 int num_closed = 0, i;
573 for (i = 3; i < MSVCRT_fdend; i++)
574 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
580 TRACE(":closed (%d) handles\n",num_closed);
584 /* free everything on process exit */
585 void msvcrt_free_io(void)
593 /*********************************************************************
594 * _lseeki64 (MSVCRT.@)
596 __int64 _lseeki64(int fd, __int64 offset, int whence)
598 DWORD ret, hoffset = (DWORD) (offset >> 32);
599 HANDLE hand = msvcrt_fdtoh(fd);
601 TRACE(":fd (%d) handle (%p)\n",fd,hand);
602 if (hand == INVALID_HANDLE_VALUE)
605 if (whence < 0 || whence > 2)
607 *MSVCRT__errno() = MSVCRT_EINVAL;
611 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
612 fd,hoffset,(long)offset,
613 (whence==SEEK_SET)?"SEEK_SET":
614 (whence==SEEK_CUR)?"SEEK_CUR":
615 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
617 if (((ret = SetFilePointer(hand, (long)offset, &hoffset,
618 whence)) != INVALID_SET_FILE_POINTER) || !GetLastError())
620 if (MSVCRT_files[fd])
621 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
622 /* FIXME: What if we seek _to_ EOF - is EOF set? */
624 return ((__int64)hoffset << 32) | ret;
626 TRACE(":error-last error (%ld)\n",GetLastError());
627 if (MSVCRT_files[fd])
628 switch(GetLastError())
630 case ERROR_NEGATIVE_SEEK:
631 case ERROR_SEEK_ON_DEVICE:
632 MSVCRT__set_errno(GetLastError());
633 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
641 /*********************************************************************
644 LONG _lseek(int fd, LONG offset, int whence)
646 return _lseeki64(fd, offset, whence);
649 /*********************************************************************
650 * _locking (MSVCRT.@)
652 * This is untested; the underlying LockFile doesn't work yet.
654 int _locking(int fd, int mode, LONG nbytes)
658 HANDLE hand = msvcrt_fdtoh(fd);
660 TRACE(":fd (%d) handle (%p)\n",fd,hand);
661 if (hand == INVALID_HANDLE_VALUE)
664 if (mode < 0 || mode > 4)
666 *MSVCRT__errno() = MSVCRT_EINVAL;
670 TRACE(":fd (%d) by 0x%08lx mode %s\n",
671 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
672 (mode==_LK_LOCK)?"_LK_LOCK":
673 (mode==_LK_NBLCK)?"_LK_NBLCK":
674 (mode==_LK_RLCK)?"_LK_RLCK":
675 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
678 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
680 FIXME ("Seek failed\n");
681 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
684 if (mode == _LK_LOCK || mode == _LK_RLCK)
687 ret = 1; /* just to satisfy gcc */
690 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
695 else if (mode == _LK_UNLCK)
696 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
698 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
699 /* FIXME - what about error settings? */
703 /*********************************************************************
706 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
708 /* Flush output if needed */
709 if(file->_flag & MSVCRT__IOWRT)
710 msvcrt_flush_buffer(file);
712 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
713 offset -= file->_cnt;
715 /* Discard buffered input */
717 file->_ptr = file->_base;
718 /* Reset direction of i/o */
719 if(file->_flag & MSVCRT__IORW) {
720 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
722 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
725 /*********************************************************************
726 * clearerr (MSVCRT.@)
728 void MSVCRT_clearerr(MSVCRT_FILE* file)
730 TRACE(":file (%p) fd (%d)\n",file,file->_file);
731 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
734 /*********************************************************************
737 void MSVCRT_rewind(MSVCRT_FILE* file)
739 TRACE(":file (%p) fd (%d)\n",file,file->_file);
740 MSVCRT_fseek(file, 0L, SEEK_SET);
741 MSVCRT_clearerr(file);
744 /*********************************************************************
747 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
749 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
751 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
756 /*********************************************************************
757 * _wfdopen (MSVCRT.@)
759 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
761 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
763 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
770 /*********************************************************************
771 * _filelength (MSVCRT.@)
773 LONG _filelength(int fd)
775 LONG curPos = _lseek(fd, 0, SEEK_CUR);
778 LONG endPos = _lseek(fd, 0, SEEK_END);
781 if (endPos != curPos)
782 _lseek(fd, curPos, SEEK_SET);
789 /*********************************************************************
790 * _filelengthi64 (MSVCRT.@)
792 __int64 _filelengthi64(int fd)
794 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
797 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
800 if (endPos != curPos)
801 _lseeki64(fd, curPos, SEEK_SET);
808 /*********************************************************************
811 int MSVCRT__fileno(MSVCRT_FILE* file)
813 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
817 /*********************************************************************
818 * _fstati64 (MSVCRT.@)
820 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
823 BY_HANDLE_FILE_INFORMATION hfi;
824 HANDLE hand = msvcrt_fdtoh(fd);
826 TRACE(":fd (%d) stat (%p)\n",fd,buf);
827 if (hand == INVALID_HANDLE_VALUE)
832 WARN(":failed-NULL buf\n");
833 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
837 memset(&hfi, 0, sizeof(hfi));
838 memset(buf, 0, sizeof(struct MSVCRT__stati64));
839 if (!GetFileInformationByHandle(hand, &hfi))
841 WARN(":failed-last error (%ld)\n",GetLastError());
842 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
845 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
846 buf->st_nlink = hfi.nNumberOfLinks;
847 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
848 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
850 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
851 buf->st_mtime = buf->st_ctime = dw;
855 /*********************************************************************
858 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
860 struct MSVCRT__stati64 bufi64;
862 ret = MSVCRT__fstati64(fd, &bufi64);
864 msvcrt_cp_from_stati64(&bufi64, buf);
868 /*********************************************************************
871 int _futime(int fd, struct _utimbuf *t)
873 HANDLE hand = msvcrt_fdtoh(fd);
878 MSVCRT_time_t currTime;
879 MSVCRT_time(&currTime);
880 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
881 memcpy(&wt, &at, sizeof(wt));
885 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
886 if (t->actime == t->modtime)
887 memcpy(&wt, &at, sizeof(wt));
889 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
892 if (!SetFileTime(hand, NULL, &at, &wt))
894 MSVCRT__set_errno(GetLastError());
900 /*********************************************************************
901 * _get_osfhandle (MSVCRT.@)
903 long _get_osfhandle(int fd)
905 HANDLE hand = msvcrt_fdtoh(fd);
906 HANDLE newhand = hand;
907 TRACE(":fd (%d) handle (%p)\n",fd,hand);
909 if (hand != INVALID_HANDLE_VALUE)
911 /* FIXME: I'm not convinced that I should be copying the
912 * handle here - it may be leaked if the app doesn't
913 * close it (and the API docs don't say that it should)
914 * Not duplicating it means that it can't be inherited
915 * and so lcc's wedit doesn't cope when it passes it to
916 * child processes. I've an idea that it should either
917 * be copied by CreateProcess, or marked as inheritable
918 * when initialised, or maybe both? JG 21-9-00.
920 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
921 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
923 return (long)newhand;
926 /*********************************************************************
931 HANDLE hand = msvcrt_fdtoh(fd);
933 TRACE(":fd (%d) handle (%p)\n",fd,hand);
934 if (hand == INVALID_HANDLE_VALUE)
937 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
940 /*********************************************************************
943 char *_mktemp(char *pattern)
946 char *retVal = pattern;
951 numX = (*pattern++ == 'X')? numX + 1 : 0;
955 id = GetCurrentProcessId();
959 int tempNum = id / 10;
960 *pattern-- = id - (tempNum * 10) + '0';
966 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
967 GetLastError() == ERROR_FILE_NOT_FOUND)
970 } while(letter != '|');
974 /*********************************************************************
975 * _wmktemp (MSVCRT.@)
977 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
980 MSVCRT_wchar_t *retVal = pattern;
982 MSVCRT_wchar_t letter = 'a';
985 numX = (*pattern++ == 'X')? numX + 1 : 0;
989 id = GetCurrentProcessId();
993 int tempNum = id / 10;
994 *pattern-- = id - (tempNum * 10) + '0';
1000 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1001 GetLastError() == ERROR_FILE_NOT_FOUND)
1003 *pattern = letter++;
1004 } while(letter != '|');
1008 /*********************************************************************
1011 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1015 DWORD access = 0, creation = 0;
1019 SECURITY_ATTRIBUTES sa;
1022 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1023 path, oflags, shflags);
1025 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
1028 access |= GENERIC_READ;
1029 ioflag |= MSVCRT__IOREAD;
1032 access |= GENERIC_WRITE;
1033 ioflag |= MSVCRT__IOWRT;
1036 access |= GENERIC_WRITE | GENERIC_READ;
1037 ioflag |= MSVCRT__IORW;
1041 if (oflags & _O_CREAT)
1043 va_start(ap, shflags);
1044 pmode = va_arg(ap, int);
1047 if(pmode & ~(_S_IREAD | _S_IWRITE))
1048 FIXME(": pmode 0x%04x ignored\n", pmode);
1050 WARN(": pmode 0x%04x ignored\n", pmode);
1052 if (oflags & _O_EXCL)
1053 creation = CREATE_NEW;
1054 else if (oflags & _O_TRUNC)
1055 creation = CREATE_ALWAYS;
1057 creation = OPEN_ALWAYS;
1059 else /* no _O_CREAT */
1061 if (oflags & _O_TRUNC)
1062 creation = TRUNCATE_EXISTING;
1064 creation = OPEN_EXISTING;
1066 if (oflags & _O_APPEND)
1067 ioflag |= MSVCRT__IOAPPEND;
1069 if (oflags & _O_BINARY)
1070 ioflag |= _O_BINARY;
1071 else if (oflags & _O_TEXT)
1073 else if (*__p__fmode() & _O_BINARY)
1074 ioflag |= _O_BINARY;
1076 ioflag |= _O_TEXT; /* default to TEXT*/
1084 sharing = FILE_SHARE_READ;
1087 sharing = FILE_SHARE_WRITE;
1090 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1093 ERR( "Unhandled shflags 0x%x\n", shflags );
1097 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
1098 |_O_CREAT|_O_RDWR|_O_WRONLY|_O_TEMPORARY|_O_NOINHERIT))
1099 ERR(":unsupported oflags 0x%04x\n",oflags);
1101 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1102 sa.lpSecurityDescriptor = NULL;
1103 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
1105 hand = CreateFileA(path, access, sharing,
1106 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
1108 if (hand == INVALID_HANDLE_VALUE) {
1109 WARN(":failed-last error (%ld)\n",GetLastError());
1110 MSVCRT__set_errno(GetLastError());
1114 fd = msvcrt_alloc_fd(hand, ioflag);
1116 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1120 if (oflags & _O_TEMPORARY)
1121 MSVCRT_tempfiles[fd] = _strdup(path);
1122 if (ioflag & MSVCRT__IOAPPEND)
1123 _lseek(fd, 0, FILE_END);
1129 /*********************************************************************
1130 * _wsopen (MSVCRT.@)
1132 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1134 const unsigned int len = strlenW(path);
1135 char *patha = MSVCRT_calloc(len + 1,1);
1139 va_start(ap, shflags);
1140 pmode = va_arg(ap, int);
1143 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1145 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1150 MSVCRT__set_errno(GetLastError());
1154 /*********************************************************************
1157 int _open( const char *path, int flags, ... )
1161 if (flags & _O_CREAT)
1164 va_start(ap, flags);
1165 pmode = va_arg(ap, int);
1167 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1170 return MSVCRT__sopen( path, flags, _SH_DENYNO);
1173 /*********************************************************************
1176 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1178 const unsigned int len = strlenW(path);
1179 char *patha = MSVCRT_calloc(len + 1,1);
1183 va_start(ap, flags);
1184 pmode = va_arg(ap, int);
1187 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1189 int retval = _open(patha,flags,pmode);
1194 MSVCRT__set_errno(GetLastError());
1198 /*********************************************************************
1201 int _creat(const char *path, int flags)
1203 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1204 return _open(path, usedFlags);
1207 /*********************************************************************
1208 * _wcreat (MSVCRT.@)
1210 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1212 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1213 return _wopen(path, usedFlags);
1216 /*********************************************************************
1217 * _open_osfhandle (MSVCRT.@)
1219 int _open_osfhandle(long hand, int flags)
1223 /* _O_RDONLY (0) always matches, so set the read flag
1224 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1225 * file, so set the write flag. It also only sets _O_TEXT if it wants
1226 * text - it never sets _O_BINARY.
1228 /* FIXME: handle more flags */
1229 flags |= MSVCRT__IOREAD|MSVCRT__IOWRT;
1230 if ( !( flags & _O_TEXT ) ) flags |= _O_BINARY;
1232 fd = msvcrt_alloc_fd((HANDLE)hand,flags);
1233 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd, flags);
1237 /*********************************************************************
1242 int num_removed = 0, i;
1244 for (i = 3; i < MSVCRT_fdend; i++)
1245 if (MSVCRT_tempfiles[i])
1252 TRACE(":removed (%d) temp files\n",num_removed);
1256 /*********************************************************************
1257 * (internal) remove_cr
1259 * Remove all \r inplace.
1260 * return the number of \r removed
1262 static unsigned int remove_cr(char *buf, unsigned int count)
1266 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1267 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1271 /*********************************************************************
1274 int _read(int fd, void *buf, unsigned int count)
1276 DWORD num_read, all_read =0;
1277 char *bufstart = buf;
1278 HANDLE hand = msvcrt_fdtoh(fd);
1280 /* Don't trace small reads, it gets *very* annoying */
1282 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1283 if (hand == INVALID_HANDLE_VALUE)
1286 /* Reading single bytes in O_TEXT mode makes things slow
1287 * So read big chunks, then remove the \r in memory and try reading
1288 * the rest until the request is satisfied or EOF is met
1290 while ( all_read < count)
1292 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1294 if (num_read != (count- all_read))
1297 if ( MSVCRT_files[fd])
1299 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1301 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1304 if ((MSVCRT_flags[fd]& _O_BINARY ) != _O_BINARY )
1305 num_read -= remove_cr(bufstart+all_read,num_read);
1306 all_read += num_read;
1308 TRACE("%s\n",debugstr_an(buf,all_read));
1311 if ((MSVCRT_flags[fd]& _O_BINARY ) != _O_BINARY )
1313 num_read -= remove_cr(bufstart+all_read,num_read);
1315 all_read += num_read;
1319 TRACE(":failed-last error (%ld)\n",GetLastError());
1320 if (MSVCRT_files[fd])
1321 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1327 TRACE("%s\n",debugstr_an(buf, all_read));
1331 /*********************************************************************
1334 int MSVCRT__getw(MSVCRT_FILE* file)
1337 if (_read(file->_file, &i, sizeof(int)) != 1)
1342 /*********************************************************************
1343 * _setmode (MSVCRT.@)
1345 int _setmode(int fd,int mode)
1347 int ret = MSVCRT_flags[fd] & (_O_TEXT | _O_BINARY);
1348 if (mode & (~(_O_TEXT|_O_BINARY)))
1349 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1350 MSVCRT_flags[fd] &= ~(_O_TEXT|_O_BINARY);
1351 MSVCRT_flags[fd] |= mode & (_O_TEXT | _O_BINARY);
1355 /*********************************************************************
1356 * _stati64 (MSVCRT.@)
1358 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1361 WIN32_FILE_ATTRIBUTE_DATA hfi;
1362 unsigned short mode = MSVCRT_S_IREAD;
1365 TRACE(":file (%s) buf(%p)\n",path,buf);
1367 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1369 TRACE("failed (%ld)\n",GetLastError());
1370 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1374 memset(buf,0,sizeof(struct MSVCRT__stati64));
1376 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1377 Bon 011120: This FIXME seems incorrect
1378 Also a letter as first char isn't enough to be classified
1381 if (isalpha(*path)&& (*(path+1)==':'))
1382 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1384 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1386 plen = strlen(path);
1388 /* Dir, or regular file? */
1389 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1390 (path[plen-1] == '\\'))
1391 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1396 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1398 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1399 (tolower(path[plen-3]) << 16);
1400 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1401 mode |= MSVCRT_S_IEXEC;
1405 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1406 mode |= MSVCRT_S_IWRITE;
1408 buf->st_mode = mode;
1410 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1411 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1413 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1414 buf->st_mtime = buf->st_ctime = dw;
1415 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1416 (long)(buf->st_size >> 32),(long)buf->st_size,
1417 buf->st_atime,buf->st_mtime, buf->st_ctime);
1421 /*********************************************************************
1424 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1426 struct MSVCRT__stati64 bufi64;
1428 ret = MSVCRT__stati64( path, &bufi64);
1430 msvcrt_cp_from_stati64(&bufi64, buf);
1434 /*********************************************************************
1435 * _wstati64 (MSVCRT.@)
1437 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1440 WIN32_FILE_ATTRIBUTE_DATA hfi;
1441 unsigned short mode = MSVCRT_S_IREAD;
1444 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1446 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1448 TRACE("failed (%ld)\n",GetLastError());
1449 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1453 memset(buf,0,sizeof(struct MSVCRT__stat));
1455 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1456 if (MSVCRT_iswalpha(*path))
1457 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1459 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1461 plen = strlenW(path);
1463 /* Dir, or regular file? */
1464 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1465 (path[plen-1] == '\\'))
1466 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1471 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1473 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1474 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1475 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1476 mode |= MSVCRT_S_IEXEC;
1480 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1481 mode |= MSVCRT_S_IWRITE;
1483 buf->st_mode = mode;
1485 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1486 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1488 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1489 buf->st_mtime = buf->st_ctime = dw;
1490 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1491 (long)(buf->st_size >> 32),(long)buf->st_size,
1492 buf->st_atime,buf->st_mtime, buf->st_ctime);
1496 /*********************************************************************
1499 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1502 struct MSVCRT__stati64 bufi64;
1504 ret = MSVCRT__wstati64( path, &bufi64 );
1505 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1509 /*********************************************************************
1514 return _lseek(fd, 0, SEEK_CUR);
1517 /*********************************************************************
1518 * _telli64 (MSVCRT.@)
1520 __int64 _telli64(int fd)
1522 return _lseeki64(fd, 0, SEEK_CUR);
1525 /*********************************************************************
1526 * _tempnam (MSVCRT.@)
1528 char *_tempnam(const char *dir, const char *prefix)
1530 char tmpbuf[MAX_PATH];
1532 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1533 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1535 TRACE("got name (%s)\n",tmpbuf);
1536 DeleteFileA(tmpbuf);
1537 return _strdup(tmpbuf);
1539 TRACE("failed (%ld)\n",GetLastError());
1543 /*********************************************************************
1544 * _wtempnam (MSVCRT.@)
1546 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1548 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1550 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1551 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1553 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1554 DeleteFileW(tmpbuf);
1555 return _wcsdup(tmpbuf);
1557 TRACE("failed (%ld)\n",GetLastError());
1561 /*********************************************************************
1564 int _umask(int umask)
1566 int old_umask = MSVCRT_umask;
1567 TRACE("(%d)\n",umask);
1568 MSVCRT_umask = umask;
1572 /*********************************************************************
1575 int _utime(const char* path, struct _utimbuf *t)
1577 int fd = _open(path, _O_WRONLY | _O_BINARY);
1581 int retVal = _futime(fd, t);
1588 /*********************************************************************
1589 * _wutime (MSVCRT.@)
1591 int _wutime(const MSVCRT_wchar_t* path, struct _utimbuf *t)
1593 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1597 int retVal = _futime(fd, t);
1604 /*********************************************************************
1607 int _write(int fd, const void* buf, unsigned int count)
1610 HANDLE hand = msvcrt_fdtoh(fd);
1612 /* Don't trace small writes, it gets *very* annoying */
1615 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1617 if (hand == INVALID_HANDLE_VALUE)
1619 *MSVCRT__errno() = MSVCRT_EBADF;
1623 /* If appending, go to EOF */
1624 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1625 _lseek(fd, 0, FILE_END);
1627 if (MSVCRT_flags[fd] & _O_BINARY)
1629 if (WriteFile(hand, buf, count, &num_written, NULL)
1630 && (num_written == count))
1632 TRACE(":failed-last error (%ld)\n",GetLastError());
1633 if (MSVCRT_files[fd])
1635 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1636 *MSVCRT__errno() = MSVCRT_ENOSPC;
1641 unsigned int i, j, nr_lf;
1642 char *s=(char*)buf, *buf_start=(char*)buf, *p;
1643 /* find number of \n ( without preceeding \r */
1644 for ( nr_lf=0,i = 0; i <count; i++)
1649 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1654 if ((p = MSVCRT_malloc(count + nr_lf)))
1656 for(s=(char*)buf, i=0, j=0; i<count; i++)
1661 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1668 FIXME("Malloc failed\n");
1676 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
1678 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
1679 if (MSVCRT_files[fd])
1681 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1682 *MSVCRT__errno() = MSVCRT_ENOSPC;
1685 return s - buf_start;
1698 /*********************************************************************
1701 int MSVCRT__putw(int val, MSVCRT_FILE* file)
1703 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1706 /*********************************************************************
1709 int MSVCRT_fclose(MSVCRT_FILE* file)
1714 r=_close(file->_file);
1715 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1718 /*********************************************************************
1721 int MSVCRT_feof(MSVCRT_FILE* file)
1723 return file->_flag & MSVCRT__IOEOF;
1726 /*********************************************************************
1729 int MSVCRT_ferror(MSVCRT_FILE* file)
1731 return file->_flag & MSVCRT__IOERR;
1734 /*********************************************************************
1735 * _filbuf (MSVCRT.@)
1737 int MSVCRT__filbuf(MSVCRT_FILE* file)
1740 /* Allocate buffer if needed */
1741 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1742 msvcrt_alloc_buffer(file);
1744 if(!(file->_flag & MSVCRT__IOREAD)) {
1745 if(file->_flag & MSVCRT__IORW) {
1746 file->_flag |= MSVCRT__IOREAD;
1751 if(file->_flag & MSVCRT__IONBF) {
1753 if (_read(file->_file,&c,1) != 1) {
1754 file->_flag |= MSVCRT__IOEOF;
1759 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1760 if(file->_cnt<0) file->_cnt = 0;
1762 file->_flag |= MSVCRT__IOEOF;
1766 file->_ptr = file->_base+1;
1767 return *(unsigned char *)file->_base;
1771 /*********************************************************************
1774 int MSVCRT_fgetc(MSVCRT_FILE* file)
1778 return *(unsigned char *)file->_ptr++;
1780 return MSVCRT__filbuf(file);
1784 /*********************************************************************
1785 * _fgetchar (MSVCRT.@)
1789 return MSVCRT_fgetc(MSVCRT_stdin);
1792 /*********************************************************************
1795 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1797 int cc = MSVCRT_EOF;
1798 char * buf_start = s;
1800 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1801 file,file->_file,s,size);
1803 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
1808 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1810 TRACE(":nothing read\n");
1813 if ((cc != MSVCRT_EOF) && (size > 1))
1816 TRACE(":got '%s'\n", debugstr_a(buf_start));
1820 /*********************************************************************
1823 * In _O_TEXT mode, bultibyte characters are read from the file, dropping
1824 * the CR from CR/LF combinations
1826 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1830 if (file->_flag & _O_BINARY)
1833 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1837 c = MSVCRT_fgetc(file);
1838 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
1840 FIXME("Treat Multibyte characters\n");
1842 if (c == MSVCRT_EOF)
1845 return (MSVCRT_wint_t)c;
1848 /*********************************************************************
1851 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1853 return MSVCRT_fgetwc(file);
1856 /*********************************************************************
1857 * _fgetwchar (MSVCRT.@)
1859 MSVCRT_wint_t _fgetwchar(void)
1861 return MSVCRT_fgetwc(MSVCRT_stdin);
1864 /*********************************************************************
1865 * getwchar (MSVCRT.@)
1867 MSVCRT_wint_t MSVCRT_getwchar(void)
1869 return _fgetwchar();
1872 /*********************************************************************
1875 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
1877 int cc = MSVCRT_WEOF;
1878 MSVCRT_wchar_t * buf_start = s;
1880 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1881 file,file->_file,s,size);
1883 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
1888 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
1890 TRACE(":nothing read\n");
1893 if ((cc != MSVCRT_WEOF) && (size > 1))
1896 TRACE(":got %s\n", debugstr_w(buf_start));
1900 /*********************************************************************
1903 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1905 MSVCRT_size_t wrcnt=size * nmemb;
1910 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
1911 memcpy(file->_ptr, ptr, pcnt);
1916 ptr = (char*)ptr + pcnt;
1917 } else if(!(file->_flag & MSVCRT__IOWRT)) {
1918 if(file->_flag & MSVCRT__IORW) {
1919 file->_flag |= MSVCRT__IOWRT;
1925 int res=msvcrt_flush_buffer(file);
1927 int pwritten = _write(file->_file, ptr, wrcnt);
1928 if (pwritten <= 0) pwritten=0;
1929 written += pwritten;
1932 return written / size;
1935 /*********************************************************************
1938 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1940 MSVCRT_wchar_t mwc=wc;
1941 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
1946 /*********************************************************************
1947 * _fputwchar (MSVCRT.@)
1949 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1951 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1954 /*********************************************************************
1957 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1960 int flags = 0, plus = 0, fd;
1961 const char* search = mode;
1963 TRACE("(%s,%s)\n",path,mode);
1966 if (*search++ == '+')
1969 /* map mode string to open() flags. "man fopen" for possibilities. */
1973 flags = (plus ? _O_RDWR : _O_RDONLY);
1976 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1979 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1994 flags &= ~_O_BINARY;
1999 FIXME(":unknown flag %c not supported\n",mode[-1]);
2002 fd = _open(path, flags, _S_IREAD | _S_IWRITE);
2007 file = msvcrt_alloc_fp(fd);
2008 TRACE(":got (%p)\n",file);
2015 /*********************************************************************
2016 * _wfopen (MSVCRT.@)
2018 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2020 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2021 char *patha = MSVCRT_calloc(plen + 1, 1);
2022 char *modea = MSVCRT_calloc(mlen + 1, 1);
2024 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2026 if (patha && modea &&
2027 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2028 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2030 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2036 MSVCRT__set_errno(GetLastError());
2040 /*********************************************************************
2041 * _fsopen (MSVCRT.@)
2043 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
2045 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2046 return MSVCRT_fopen(path,mode);
2049 /*********************************************************************
2050 * _wfsopen (MSVCRT.@)
2052 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2054 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2055 debugstr_w(path),debugstr_w(mode),share);
2056 return MSVCRT__wfopen(path,mode);
2059 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2060 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2062 /*********************************************************************
2065 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2072 return MSVCRT__flsbuf(c, file);
2076 /*********************************************************************
2077 * _flsbuf (MSVCRT.@)
2079 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2081 /* Flush output buffer */
2082 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2083 msvcrt_alloc_buffer(file);
2085 if(!(file->_flag & MSVCRT__IOWRT)) {
2086 if(file->_flag & MSVCRT__IORW) {
2087 file->_flag |= MSVCRT__IOWRT;
2093 int res=msvcrt_flush_buffer(file);
2094 return res?res : MSVCRT_fputc(c, file);
2097 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
2101 /*********************************************************************
2102 * _fputchar (MSVCRT.@)
2104 int _fputchar(int c)
2106 return MSVCRT_fputc(c, MSVCRT_stdout);
2109 /*********************************************************************
2112 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2113 { MSVCRT_size_t rcnt=size * nmemb;
2114 MSVCRT_size_t read=0;
2116 /* first buffered data */
2118 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2119 memcpy(ptr, file->_ptr, pcnt);
2124 ptr = (char*)ptr + pcnt;
2125 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2126 if(file->_flag & MSVCRT__IORW) {
2127 file->_flag |= MSVCRT__IOREAD;
2131 if(rcnt) pread = _read(file->_file,ptr, rcnt);
2132 if (MSVCRT_flags[file->_file] & MSVCRT__IOEOF)
2133 /* expose feof condition in the flags
2134 MFC tests file->_flag for feof, and doesn't not call feof())
2136 file->_flag |= MSVCRT__IOEOF;
2143 /*********************************************************************
2144 * freopen (MSVCRT.@)
2147 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2149 MSVCRT_FILE* newfile;
2152 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2153 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2159 FIXME(":reopen on user file not implemented!\n");
2160 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
2163 if(MSVCRT_fclose(file))
2165 return MSVCRT_fopen(path, mode);
2168 /* first, create the new file */
2169 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
2172 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
2173 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
2174 MSVCRT_handles[newfile->_file]))
2176 /* Redirecting std handle to file , copy over.. */
2177 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
2178 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
2179 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
2180 MSVCRT__iob[fd]._file = fd;
2181 /* And free up the resources allocated by fopen, but
2182 * not the HANDLE we copied. */
2183 MSVCRT_free(MSVCRT_files[fd]);
2184 msvcrt_free_fd(newfile->_file);
2185 return &MSVCRT__iob[fd];
2188 WARN(":failed-last error (%ld)\n",GetLastError());
2189 MSVCRT_fclose(newfile);
2190 MSVCRT__set_errno(GetLastError());
2194 /*********************************************************************
2195 * fsetpos (MSVCRT.@)
2197 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2199 return _lseek(file->_file,*pos,SEEK_SET);
2202 /*********************************************************************
2205 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2210 if( file->_flag & MSVCRT__IOWRT ) {
2211 off = file->_ptr - file->_base;
2216 pos = _tell(file->_file);
2217 if(pos == -1) return pos;
2221 /*********************************************************************
2222 * fgetpos (MSVCRT.@)
2224 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2226 *pos = MSVCRT_ftell(file);
2227 return (*pos == -1? -1 : 0);
2230 /*********************************************************************
2233 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2235 size_t i, len = strlen(s);
2236 if (file->_flag & _O_BINARY)
2237 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2238 for (i=0; i<len; i++)
2239 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2244 /*********************************************************************
2247 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2249 size_t i, len = strlenW(s);
2250 if (file->_flag & _O_BINARY)
2251 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2252 for (i=0; i<len; i++)
2254 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2256 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2262 /*********************************************************************
2263 * getchar (MSVCRT.@)
2265 int MSVCRT_getchar(void)
2267 return MSVCRT_fgetc(MSVCRT_stdin);
2270 /*********************************************************************
2273 int MSVCRT_getc(MSVCRT_FILE* file)
2275 return MSVCRT_fgetc(file);
2278 /*********************************************************************
2281 char *MSVCRT_gets(char *buf)
2284 char * buf_start = buf;
2286 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2287 cc = MSVCRT_fgetc(MSVCRT_stdin))
2288 if(cc != '\r') *buf++ = (char)cc;
2292 TRACE("got '%s'\n", buf_start);
2296 /*********************************************************************
2299 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2302 MSVCRT_wchar_t* ws = buf;
2304 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2305 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2308 *buf++ = (MSVCRT_wchar_t)cc;
2312 TRACE("got '%s'\n", debugstr_w(ws));
2316 /*********************************************************************
2319 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2321 return MSVCRT_fputc(c, file);
2324 /*********************************************************************
2325 * putchar (MSVCRT.@)
2327 int MSVCRT_putchar(int c)
2329 return MSVCRT_fputc(c, MSVCRT_stdout);
2332 /*********************************************************************
2335 int MSVCRT_puts(const char *s)
2337 size_t len = strlen(s);
2338 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2339 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2342 /*********************************************************************
2345 int _putws(const MSVCRT_wchar_t *s)
2347 static const MSVCRT_wchar_t nl = '\n';
2348 size_t len = strlenW(s);
2349 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2350 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2353 /*********************************************************************
2356 int MSVCRT_remove(const char *path)
2358 TRACE("(%s)\n",path);
2359 if (DeleteFileA(path))
2361 TRACE(":failed (%ld)\n",GetLastError());
2362 MSVCRT__set_errno(GetLastError());
2366 /*********************************************************************
2367 * _wremove (MSVCRT.@)
2369 int _wremove(const MSVCRT_wchar_t *path)
2371 TRACE("(%s)\n",debugstr_w(path));
2372 if (DeleteFileW(path))
2374 TRACE(":failed (%ld)\n",GetLastError());
2375 MSVCRT__set_errno(GetLastError());
2379 /*********************************************************************
2382 int MSVCRT_rename(const char *oldpath,const char *newpath)
2384 TRACE(":from %s to %s\n",oldpath,newpath);
2385 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2387 TRACE(":failed (%ld)\n",GetLastError());
2388 MSVCRT__set_errno(GetLastError());
2392 /*********************************************************************
2393 * _wrename (MSVCRT.@)
2395 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2397 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2398 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2400 TRACE(":failed (%ld)\n",GetLastError());
2401 MSVCRT__set_errno(GetLastError());
2405 /*********************************************************************
2406 * setvbuf (MSVCRT.@)
2408 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2410 /* TODO: Check if file busy */
2412 MSVCRT_free(file->_base);
2416 if(mode == MSVCRT__IOFBF) {
2417 file->_flag &= ~MSVCRT__IONBF;
2418 file->_base = file->_ptr = buf;
2420 file->_bufsiz = size;
2423 file->_flag |= MSVCRT__IONBF;
2428 /*********************************************************************
2431 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2433 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2436 /*********************************************************************
2439 char *MSVCRT_tmpnam(char *s)
2447 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2448 p = s + sprintf(s, "\\s%s.", tmpstr);
2449 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2451 msvcrt_int_to_base32(unique++, tmpstr);
2453 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2454 GetLastError() == ERROR_FILE_NOT_FOUND)
2460 /*********************************************************************
2461 * tmpfile (MSVCRT.@)
2463 MSVCRT_FILE* MSVCRT_tmpfile(void)
2465 char *filename = MSVCRT_tmpnam(NULL);
2467 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2469 return msvcrt_alloc_fp(fd);
2473 /*********************************************************************
2474 * vfprintf (MSVCRT.@)
2476 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2478 char buf[2048], *mem = buf;
2479 int written, resize = sizeof(buf), retval;
2480 /* There are two conventions for vsnprintf failing:
2481 * Return -1 if we truncated, or
2482 * Return the number of bytes that would have been written
2483 * The code below handles both cases
2485 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2488 resize = (written == -1 ? resize * 2 : written + 1);
2491 if (!(mem = (char *)MSVCRT_malloc(resize)))
2494 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2500 /*********************************************************************
2501 * vfwprintf (MSVCRT.@)
2503 * Is final char included in written (then resize is too big) or not
2504 * (then we must test for equality too)?
2506 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2508 MSVCRT_wchar_t buf[2048], *mem = buf;
2509 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2510 /* See vfprintf comments */
2511 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2514 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2517 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2520 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2526 /*********************************************************************
2527 * vprintf (MSVCRT.@)
2529 int MSVCRT_vprintf(const char *format, va_list valist)
2531 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2534 /*********************************************************************
2535 * vwprintf (MSVCRT.@)
2537 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2539 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2542 /*********************************************************************
2543 * fprintf (MSVCRT.@)
2545 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2549 va_start(valist, format);
2550 res = MSVCRT_vfprintf(file, format, valist);
2555 /*********************************************************************
2556 * fwprintf (MSVCRT.@)
2558 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2562 va_start(valist, format);
2563 res = MSVCRT_vfwprintf(file, format, valist);
2568 /*********************************************************************
2571 int MSVCRT_printf(const char *format, ...)
2575 va_start(valist, format);
2576 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2581 /*********************************************************************
2584 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2586 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2587 msvcrt_alloc_buffer(file);
2590 if(file->_ptr>file->_base) {
2599 /*********************************************************************
2600 * ungetwc (MSVCRT.@)
2602 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2604 MSVCRT_wchar_t mwc = wc;
2605 char * pp = (char *)&mwc;
2607 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2608 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2614 /*********************************************************************
2615 * wprintf (MSVCRT.@)
2617 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2621 va_start(valist, format);
2622 res = MSVCRT_vwprintf(format, valist);