2 * CRTDLL 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 * Implementation Notes:
10 * Mapping is performed between FILE*, fd and HANDLE's. This allows us to
11 * implement all calls using the Win32 API, support remapping fd's to
12 * FILES and do some other tricks as well (like closeall, _get_osfhandle).
13 * For mix and matching with the host libc, processes can use the Win32 HANDLE
14 * to get a real unix fd from the wineserver. Or we could do this once
15 * on create, and provide a function to return it quickly (store it
16 * in the mapping table). Note that If you actuall _do_ this, you should
17 * call rewind() before using any other crt functions on the file. To avoid
18 * the confusion I got when reading the API docs, fd is always refered
19 * to as a file descriptor here. In the API docs its called a file handle
20 * which is confusing with Win32 HANDLES.
21 * M$ CRT includes inline versions of some of these functions (like feof()).
22 * These inlines check/modify bitfields in the FILE structure, so we set
23 * _flags/_file/_cnt in the FILE* to be binary compatible with the win dll.
24 * lcc defines _IOAPPEND as one of the flags for a FILE*, but testing shows
25 * that M$ CRT never sets it. So we keep the flag in our mapping table but
26 * mask it out when we populate a FILE* with it. Then when we write we seek
27 * to EOF if _IOAPPEND is set for the underlying fd.
30 * Not MT safe. Need locking around file access and allocation for this.
31 * NT has no effective limit on files - neither should we. This will be fixed
32 * with dynamic allocation of the file mapping array.
33 * Buffering is handled differently. Have to investigate a) how much control
34 * we have over buffering in win32, and b) if we care ;-)
44 DEFAULT_DEBUG_CHANNEL(crtdll);
46 /* FIXME: Make this dynamic */
47 #define CRTDLL_MAX_FILES 257
49 HANDLE __CRTDLL_handles[CRTDLL_MAX_FILES];
50 CRTDLL_FILE* __CRTDLL_files[CRTDLL_MAX_FILES];
51 INT __CRTDLL_flags[CRTDLL_MAX_FILES];
52 CRTDLL_FILE __CRTDLL_iob[3];
54 static int __CRTDLL_fdstart = 3; /* first unallocated fd */
55 static int __CRTDLL_fdend = 3; /* highest allocated fd */
57 /* INTERNAL: process umask */
58 static INT __CRTDLL_umask = 0;
60 /* INTERNAL: Static buffer for temp file name */
61 static char CRTDLL_tmpname[MAX_PATH];
63 /* file extentions recognised as executables */
64 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
65 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
66 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
67 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
69 /* for stat mode, permissions apply to all,owner and group */
70 #define CRTDLL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
71 #define CRTDLL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
72 #define CRTDLL_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
75 /* INTERNAL: Get the HANDLE for a fd */
76 static HANDLE __CRTDLL__fdtoh(INT fd);
77 static HANDLE __CRTDLL__fdtoh(INT fd)
79 if (fd < 0 || fd >= __CRTDLL_fdend ||
80 __CRTDLL_handles[fd] == INVALID_HANDLE_VALUE)
82 WARN(":fd (%d) - no handle!\n",fd);
85 return INVALID_HANDLE_VALUE;
87 return __CRTDLL_handles[fd];
91 /* INTERNAL: free a file entry fd */
92 static void __CRTDLL__free_fd(INT fd);
93 static void __CRTDLL__free_fd(INT fd)
95 __CRTDLL_handles[fd] = INVALID_HANDLE_VALUE;
96 __CRTDLL_files[fd] = 0;
97 __CRTDLL_flags[fd] = 0;
98 TRACE(":fd (%d) freed\n",fd);
100 return; /* dont use 0,1,2 for user files */
101 if (fd == __CRTDLL_fdend - 1)
103 if (fd < __CRTDLL_fdstart)
104 __CRTDLL_fdstart = fd;
108 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
109 static INT __CRTDLL__alloc_fd(HANDLE hand, INT flag);
110 static INT __CRTDLL__alloc_fd(HANDLE hand, INT flag)
112 INT fd = __CRTDLL_fdstart;
114 TRACE(":handle (%d) allocating fd (%d)\n",hand,fd);
115 if (fd >= CRTDLL_MAX_FILES)
117 WARN(":files exhausted!\n");
120 __CRTDLL_handles[fd] = hand;
121 __CRTDLL_flags[fd] = flag;
123 /* locate next free slot */
124 if (fd == __CRTDLL_fdend)
125 __CRTDLL_fdstart = ++__CRTDLL_fdend;
127 while(__CRTDLL_fdstart < __CRTDLL_fdend &&
128 __CRTDLL_handles[__CRTDLL_fdstart] != INVALID_HANDLE_VALUE)
135 /* INTERNAL: Allocate a FILE* for an fd slot
136 * This is done lazily to avoid memory wastage for low level open/write
137 * usage when a FILE* is not requested (but may be later).
139 static CRTDLL_FILE* __CRTDLL__alloc_fp(INT fd);
140 static CRTDLL_FILE* __CRTDLL__alloc_fp(INT fd)
142 TRACE(":fd (%d) allocating FILE*\n",fd);
143 if (fd < 0 || fd >= __CRTDLL_fdend ||
144 __CRTDLL_handles[fd] == INVALID_HANDLE_VALUE)
146 WARN(":invalid fd %d\n",fd);
148 CRTDLL_errno = EBADF;
151 if (!__CRTDLL_files[fd])
153 if ((__CRTDLL_files[fd] = CRTDLL_calloc(sizeof(CRTDLL_FILE),1)))
155 __CRTDLL_files[fd]->_file = fd;
156 __CRTDLL_files[fd]->_flag = __CRTDLL_flags[fd];
157 __CRTDLL_files[fd]->_flag &= ~_IOAPPEND; /* mask out, see above */
160 TRACE(":got FILE* (%p)\n",__CRTDLL_files[fd]);
161 return __CRTDLL_files[fd];
165 /* INTERNAL: Set up stdin, stderr and stdout */
166 VOID __CRTDLL__init_io(VOID)
169 memset(__CRTDLL_iob,0,3*sizeof(CRTDLL_FILE));
170 __CRTDLL_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
171 __CRTDLL_flags[0] = __CRTDLL_iob[0]._flag = _IOREAD;
172 __CRTDLL_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
173 __CRTDLL_flags[1] = __CRTDLL_iob[1]._flag = _IOWRT;
174 __CRTDLL_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
175 __CRTDLL_flags[2] = __CRTDLL_iob[2]._flag = _IOWRT;
177 TRACE(":handles (%d)(%d)(%d)\n",__CRTDLL_handles[0],
178 __CRTDLL_handles[1],__CRTDLL_handles[2]);
180 for (i = 0; i < 3; i++)
182 /* FILE structs for stdin/out/err are static and never deleted */
183 __CRTDLL_files[i] = &__CRTDLL_iob[i];
184 __CRTDLL_iob[i]._file = i;
189 /*********************************************************************
190 * _access (CRTDLL.37)
192 INT __cdecl CRTDLL__access(LPCSTR filename, INT mode)
194 DWORD attr = GetFileAttributesA(filename);
196 if (attr == 0xffffffff)
200 /* FIXME: Should GetFileAttributesA() return this? */
201 __CRTDLL__set_errno(ERROR_INVALID_DATA);
204 __CRTDLL__set_errno(GetLastError());
207 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
209 __CRTDLL__set_errno(ERROR_ACCESS_DENIED);
212 TRACE(":file %s, mode (%d) ok\n",filename,mode);
217 /*********************************************************************
218 * _chmod (CRTDLL.054)
220 * Change a files permissions.
222 INT __cdecl CRTDLL__chmod(LPCSTR path, INT flags)
224 DWORD oldFlags = GetFileAttributesA(path);
226 if (oldFlags != 0x0FFFFFFFF)
228 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
229 oldFlags | FILE_ATTRIBUTE_READONLY;
231 if (newFlags == oldFlags || SetFileAttributesA( path, newFlags ))
234 __CRTDLL__set_errno(GetLastError());
239 /*********************************************************************
242 * Close an open file descriptor.
244 INT __cdecl CRTDLL__close(INT fd)
246 HANDLE hand = __CRTDLL__fdtoh(fd);
248 TRACE(":fd (%d) handle (%d)\n",fd,hand);
249 if (hand == INVALID_HANDLE_VALUE)
252 /* Dont free std FILE*'s, they are not dynamic */
253 if (fd > 2 && __CRTDLL_files[fd])
254 CRTDLL_free(__CRTDLL_files[fd]);
256 __CRTDLL__free_fd(fd);
258 if (!CloseHandle(hand))
260 WARN(":failed-last error (%ld)\n",GetLastError());
261 __CRTDLL__set_errno(GetLastError());
269 /*********************************************************************
270 * _commit (CRTDLL.58)
272 * Ensure all file operations have been flushed to the drive.
274 INT __cdecl CRTDLL__commit(INT fd)
276 HANDLE hand = __CRTDLL__fdtoh(fd);
278 TRACE(":fd (%d) handle (%d)\n",fd,hand);
279 if (hand == INVALID_HANDLE_VALUE)
282 if (!FlushFileBuffers(hand))
284 if (GetLastError() == ERROR_INVALID_HANDLE)
286 /* FlushFileBuffers fails for console handles
287 * so we ignore this error.
291 TRACE(":failed-last error (%ld)\n",GetLastError());
292 __CRTDLL__set_errno(GetLastError());
300 /*********************************************************************
301 * _creat (CRTDLL.066)
303 * Open a file, creating it if it is not present.
305 INT __cdecl CRTDLL__creat(LPCSTR path, INT flags)
307 INT usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
308 return CRTDLL__open(path, usedFlags);
312 /*********************************************************************
315 * Determine if the file pointer is at the end of a file.
318 * Care for large files
320 INT __cdecl CRTDLL__eof( INT fd )
323 HANDLE hand = __CRTDLL__fdtoh(fd);
325 TRACE(":fd (%d) handle (%d)\n",fd,hand);
327 if (hand == INVALID_HANDLE_VALUE)
330 /* If we have a FILE* for this file, the EOF flag
331 * will be set by the read()/write() functions.
333 if (__CRTDLL_files[fd])
334 return __CRTDLL_files[fd]->_flag & _IOEOF;
336 /* Otherwise we do it the hard way */
337 curpos = SetFilePointer( hand, 0, NULL, SEEK_CUR );
338 endpos = SetFilePointer( hand, 0, NULL, FILE_END );
340 if (curpos == endpos)
343 SetFilePointer( hand, curpos, 0, FILE_BEGIN);
348 /*********************************************************************
349 * _fcloseall (CRTDLL.089)
351 * Close all open files except stdin/stdout/stderr.
353 INT __cdecl CRTDLL__fcloseall(VOID)
355 int num_closed = 0, i = 3;
357 while(i < __CRTDLL_fdend)
358 if (__CRTDLL_handles[i] != INVALID_HANDLE_VALUE)
364 TRACE(":closed (%d) handles\n",num_closed);
369 /*********************************************************************
370 * _fdopen (CRTDLL.091)
372 * Get a FILE* from a low level file descriptor.
374 CRTDLL_FILE* __cdecl CRTDLL__fdopen(INT fd, LPCSTR mode)
376 CRTDLL_FILE* file = __CRTDLL__alloc_fp(fd);
378 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
386 /*********************************************************************
387 * _fgetchar (CRTDLL.092)
389 INT __cdecl CRTDLL__fgetchar( VOID )
391 return CRTDLL_fgetc(CRTDLL_stdin);
395 /*********************************************************************
396 * _filbuf (CRTDLL.094)
399 * The macro version of getc calls this function whenever FILE->_cnt
400 * becomes negative. We ensure that _cnt is always 0 after any read
401 * so this function is always called. Our implementation simply calls
402 * fgetc as all the underlying buffering is handled by Wines
403 * implementation of the Win32 file I/O calls.
405 INT __cdecl CRTDLL__filbuf(CRTDLL_FILE* file)
407 return CRTDLL_fgetc(file);
410 /*********************************************************************
411 * _filelength (CRTDLL.097)
413 * Get the length of an open file.
415 LONG __cdecl CRTDLL__filelength(INT fd)
417 LONG curPos = CRTDLL__lseek(fd, 0, SEEK_CUR);
420 LONG endPos = CRTDLL__lseek(fd, 0, SEEK_END);
423 if (endPos != curPos)
424 CRTDLL__lseek(fd, curPos, SEEK_SET);
432 /*********************************************************************
433 * _fileno (CRTDLL.097)
435 * Get the file descriptor from a FILE*.
438 * This returns the CRTDLL fd, _not_ the underlying *nix fd.
440 INT __cdecl CRTDLL__fileno(CRTDLL_FILE* file)
442 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
447 /*********************************************************************
448 * _flsbuf (CRTDLL.102)
451 * The macro version of putc calls this function whenever FILE->_cnt
452 * becomes negative. We ensure that _cnt is always 0 after any write
453 * so this function is always called. Our implementation simply calls
454 * fputc as all the underlying buffering is handled by Wines
455 * implementation of the Win32 file I/O calls.
457 INT __cdecl CRTDLL__flsbuf(INT c, CRTDLL_FILE* file)
459 return CRTDLL_fputc(c,file);
463 /*********************************************************************
464 * _flushall (CRTDLL.103)
466 * Flush all open files.
468 INT __cdecl CRTDLL__flushall(VOID)
470 int num_flushed = 0, i = 3;
472 while(i < __CRTDLL_fdend)
473 if (__CRTDLL_handles[i] != INVALID_HANDLE_VALUE)
475 if (CRTDLL__commit(i) == -1)
476 if (__CRTDLL_files[i])
477 __CRTDLL_files[i]->_flag |= _IOERR;
481 TRACE(":flushed (%d) handles\n",num_flushed);
486 /*********************************************************************
487 * _fputchar (CRTDLL.108)
489 * Put a character to a file.
491 INT __cdecl CRTDLL__fputchar(INT c)
493 return CRTDLL_fputc(c, CRTDLL_stdout);
497 /*********************************************************************
498 * _fsopen (CRTDLL.110)
500 * Open a FILE* with sharing.
502 CRTDLL_FILE* __cdecl CRTDLL__fsopen(LPCSTR path, LPCSTR mode, INT share)
504 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
505 return CRTDLL_fopen(path,mode);
509 /*********************************************************************
510 * _fstat (CRTDLL.111)
512 * Get information about an open file.
514 int __cdecl CRTDLL__fstat(int fd, struct _stat* buf)
517 BY_HANDLE_FILE_INFORMATION hfi;
518 HANDLE hand = __CRTDLL__fdtoh(fd);
520 TRACE(":fd (%d) stat (%p)\n",fd,buf);
521 if (hand == INVALID_HANDLE_VALUE)
526 WARN(":failed-NULL buf\n");
527 __CRTDLL__set_errno(ERROR_INVALID_PARAMETER);
531 memset(&hfi, 0, sizeof(hfi));
532 memset(buf, 0, sizeof(struct _stat));
533 if (!GetFileInformationByHandle(hand, &hfi))
535 WARN(":failed-last error (%ld)\n",GetLastError());
536 __CRTDLL__set_errno(ERROR_INVALID_PARAMETER);
539 FIXME(":dwFileAttributes = %d, mode set to 0",hfi.dwFileAttributes);
540 buf->st_nlink = hfi.nNumberOfLinks;
541 buf->st_size = hfi.nFileSizeLow;
542 RtlTimeToSecondsSince1970( &hfi.ftLastAccessTime, &dw );
544 RtlTimeToSecondsSince1970( &hfi.ftLastWriteTime, &dw );
545 buf->st_mtime = buf->st_ctime = dw;
550 /*********************************************************************
551 * _futime (CRTDLL.115)
553 * Set the file access/modification times on an open file.
555 INT __cdecl CRTDLL__futime(INT fd, struct _utimbuf *t)
557 HANDLE hand = __CRTDLL__fdtoh(fd);
563 CRTDLL_time(&currTime);
564 RtlSecondsSince1970ToTime( currTime, &at );
565 memcpy( &wt, &at, sizeof(wt) );
569 RtlSecondsSince1970ToTime( t->actime, &at );
570 if (t->actime == t->modtime)
571 memcpy( &wt, &at, sizeof(wt) );
573 RtlSecondsSince1970ToTime( t->modtime, &wt );
576 if (!SetFileTime( hand, NULL, &at, &wt ))
578 __CRTDLL__set_errno(GetLastError());
585 /*********************************************************************
586 * _get_osfhandle (CRTDLL.117)
588 * Return a Win32 HANDLE from a file descriptor.
591 * fd [in] A valid file descriptor
594 * Success: A Win32 HANDLE
596 * Failure: INVALID_HANDLE_VALUE.
599 HANDLE CRTDLL__get_osfhandle(INT fd)
601 HANDLE hand = __CRTDLL__fdtoh(fd);
602 HANDLE newhand = hand;
603 TRACE(":fd (%d) handle (%d)\n",fd,hand);
605 if (hand != INVALID_HANDLE_VALUE)
607 /* FIXME: I'm not convinced that I should be copying the
608 * handle here - it may be leaked if the app doesn't
609 * close it (and the API docs dont say that it should)
610 * Not duplicating it means that it can't be inherited
611 * and so lcc's wedit doesn't cope when it passes it to
612 * child processes. I've an idea that it should either
613 * be copied by CreateProcess, or marked as inheritable
614 * when initialised, or maybe both? JG 21-9-00.
616 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
617 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS );
623 /*********************************************************************
626 * Read an integter from a FILE*.
628 INT __cdecl CRTDLL__getw( CRTDLL_FILE* file )
631 if (CRTDLL__read(file->_file, &i, sizeof(INT)) != 1)
637 /*********************************************************************
638 * _isatty (CRTDLL.137)
640 * Return non zero if fd is a character device (e.g console).
642 INT __cdecl CRTDLL__isatty(INT fd)
644 HANDLE hand = __CRTDLL__fdtoh(fd);
646 TRACE(":fd (%d) handle (%d)\n",fd,hand);
647 if (hand == INVALID_HANDLE_VALUE)
650 return GetFileType(fd) == FILE_TYPE_CHAR? 1 : 0;
654 /*********************************************************************
655 * _lseek (CRTDLL.179)
657 * Move the file pointer within a file.
659 LONG __cdecl CRTDLL__lseek( INT fd, LONG offset, INT whence)
662 HANDLE hand = __CRTDLL__fdtoh(fd);
664 TRACE(":fd (%d) handle (%d)\n",fd,hand);
665 if (hand == INVALID_HANDLE_VALUE)
668 if (whence < 0 || whence > 2)
670 CRTDLL_errno = EINVAL;
674 TRACE(":fd (%d) to 0x%08lx pos %s\n",
675 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
676 (whence==SEEK_CUR)?"SEEK_CUR":
677 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
679 if ((ret = SetFilePointer( hand, offset, NULL, whence )) != 0xffffffff)
681 if ( __CRTDLL_files[fd])
682 __CRTDLL_files[fd]->_flag &= ~_IOEOF;
683 /* FIXME: What if we seek _to_ EOF - is EOF set? */
686 TRACE(":error-last error (%ld)\n",GetLastError());
687 if ( __CRTDLL_files[fd])
688 switch(GetLastError())
690 case ERROR_NEGATIVE_SEEK:
691 case ERROR_SEEK_ON_DEVICE:
692 __CRTDLL__set_errno(GetLastError());
693 __CRTDLL_files[fd]->_flag |= _IOERR;
701 /*********************************************************************
702 * _mktemp (CRTDLL.239)
704 * Create a temporary file name.
706 LPSTR __cdecl CRTDLL__mktemp(LPSTR pattern)
709 LPSTR retVal = pattern;
714 numX = (*pattern++ == 'X')? numX + 1 : 0;
718 id = GetCurrentProcessId();
722 INT tempNum = id / 10;
723 *pattern-- = id - (tempNum * 10) + '0';
729 if (GetFileAttributesA( retVal ) == 0xFFFFFFFF &&
730 GetLastError() == ERROR_FILE_NOT_FOUND)
733 } while(letter != '|');
737 /*********************************************************************
741 INT __cdecl CRTDLL__open(LPCSTR path,INT flags)
743 DWORD access = 0, creation = 0;
747 TRACE(":file (%s) mode 0x%04x\n",path,flags);
749 switch(flags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
752 access |= GENERIC_READ;
756 access |= GENERIC_WRITE;
760 access |= GENERIC_WRITE | GENERIC_READ;
765 if (flags & _O_CREAT)
768 creation = CREATE_NEW;
769 else if (flags & _O_TRUNC)
770 creation = CREATE_ALWAYS;
772 creation = OPEN_ALWAYS;
774 else /* no _O_CREAT */
776 if (flags & _O_TRUNC)
777 creation = TRUNCATE_EXISTING;
779 creation = OPEN_EXISTING;
781 if (flags & _O_APPEND)
785 flags |= _O_BINARY; /* FIXME: Default to text */
789 /* Dont warn when writing */
790 if (ioflag & GENERIC_READ)
791 FIXME(":TEXT node not implemented\n");
795 if (flags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL|_O_CREAT|_O_RDWR))
796 TRACE(":unsupported flags 0x%04x\n",flags);
798 /* clear those pesky flags ;-) */
799 flags &= (_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL|_O_CREAT|_O_RDWR);
801 hand = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
802 NULL, creation, FILE_ATTRIBUTE_NORMAL, -1);
804 if (hand == INVALID_HANDLE_VALUE)
806 WARN(":failed-last error (%ld)\n",GetLastError());
807 __CRTDLL__set_errno(GetLastError());
811 fd = __CRTDLL__alloc_fd(hand,ioflag);
813 TRACE(":fd (%d) handle (%d)\n",fd, hand);
815 if (flags & _IOAPPEND && fd > 0)
816 CRTDLL__lseek(fd, 0, FILE_END );
822 /*********************************************************************
823 * _open_osfhandle (CRTDLL.240)
825 * Create a file descriptor for a file HANDLE.
827 INT __cdecl CRTDLL__open_osfhandle(HANDLE hand, INT flags)
829 INT fd = __CRTDLL__alloc_fd(hand,flags);
830 TRACE(":handle (%d) fd (%d)\n",hand,fd);
835 /*********************************************************************
838 * Write an int to a FILE*.
840 INT __cdecl CRTDLL__putw(INT val, CRTDLL_FILE* file)
842 return CRTDLL__write(file->_file, &val, sizeof(val)) == 1? val : EOF;
846 /*********************************************************************
849 * Read data from a file.
851 INT __cdecl CRTDLL__read(INT fd, LPVOID buf, UINT count)
854 HANDLE hand = __CRTDLL__fdtoh(fd);
856 /* Dont trace small reads, it gets *very* annoying */
858 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
859 if (hand == INVALID_HANDLE_VALUE)
862 /* Set _cnt to 0 so optimised binaries will call our implementation
863 * of putc/getc. See _filbuf/_flsbuf comments.
865 if (__CRTDLL_files[fd])
866 __CRTDLL_files[fd]->_cnt = 0;
868 if (ReadFile(hand, buf, count, &num_read, NULL))
870 if (num_read != count && __CRTDLL_files[fd])
873 __CRTDLL_files[fd]->_flag |= _IOEOF;
877 TRACE(":failed-last error (%ld)\n",GetLastError());
878 if ( __CRTDLL_files[fd])
879 __CRTDLL_files[fd]->_flag |= _IOERR;
884 /*********************************************************************
885 * _setmode (CRTDLL.265)
887 * FIXME: At present we ignore the request to translate CR/LF to LF.
889 * We always translate when we read with fgets, we never do with fread
892 INT __cdecl CRTDLL__setmode(INT fd,INT mode)
895 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
900 /*********************************************************************
903 INT __cdecl CRTDLL__stat(const char* path, struct _stat * buf)
906 WIN32_FILE_ATTRIBUTE_DATA hfi;
907 unsigned short mode = CRTDLL_S_IREAD;
910 TRACE(":file (%s) buf(%p)\n",path,buf);
912 if (!GetFileAttributesExA( path, GetFileExInfoStandard, &hfi ))
914 TRACE("failed-last error (%ld)\n",GetLastError());
915 __CRTDLL__set_errno(ERROR_FILE_NOT_FOUND);
919 memset(buf,0,sizeof(struct _stat));
921 /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
923 buf->st_dev = buf->st_rdev = toupper(*path - 'A'); /* drive num */
925 buf->st_dev = buf->st_rdev = CRTDLL__getdrive() - 1;
929 /* Dir, or regular file? */
930 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
931 (path[plen-1] == '\\'))
932 mode |= (_S_IFDIR | CRTDLL_S_IEXEC);
937 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
939 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8)
940 | (tolower(path[plen-3]) << 16);
941 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
942 mode |= CRTDLL_S_IEXEC;
946 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
947 mode |= CRTDLL_S_IWRITE;
951 buf->st_size = hfi.nFileSizeLow;
952 RtlTimeToSecondsSince1970( &hfi.ftLastAccessTime, &dw );
954 RtlTimeToSecondsSince1970( &hfi.ftLastWriteTime, &dw );
955 buf->st_mtime = buf->st_ctime = dw;
956 TRACE("\n%d %d %d %d %d %d\n", buf->st_mode,buf->st_nlink,buf->st_size,
957 buf->st_atime,buf->st_mtime, buf->st_ctime);
962 /*********************************************************************
965 * Get current file position.
967 LONG __cdecl CRTDLL__tell(INT fd)
969 return CRTDLL__lseek(fd, 0, SEEK_CUR);
973 /*********************************************************************
974 * _tempnam (CRTDLL.305)
977 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
979 char tmpbuf[MAX_PATH];
981 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
982 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
984 TRACE("got name (%s)\n",tmpbuf);
985 return CRTDLL__strdup(tmpbuf);
987 TRACE("failed-last error (%ld)\n",GetLastError());
992 /*********************************************************************
993 * _umask (CRTDLL.310)
995 * Set the process-wide umask.
997 INT __cdecl CRTDLL__umask(INT umask)
999 INT old_umask = __CRTDLL_umask;
1000 TRACE("umask (%d)\n",umask);
1001 __CRTDLL_umask = umask;
1006 /*********************************************************************
1007 * _utime (CRTDLL.314)
1009 * Set the file access/modification times on a file.
1011 INT __cdecl CRTDLL__utime(LPCSTR path, struct _utimbuf *t)
1013 INT fd = CRTDLL__open( path, _O_WRONLY | _O_BINARY );
1017 INT retVal = CRTDLL__futime(fd, t);
1025 /*********************************************************************
1026 * _unlink (CRTDLL.315)
1030 INT __cdecl CRTDLL__unlink(LPCSTR path)
1032 TRACE("path (%s)\n",path);
1033 if(DeleteFileA( path ))
1036 TRACE("failed-last error (%ld)\n",GetLastError());
1037 __CRTDLL__set_errno(GetLastError());
1042 /*********************************************************************
1043 * _write (CRTDLL.332)
1045 * Write data to a file.
1047 UINT __cdecl CRTDLL__write(INT fd, LPCVOID buf, UINT count)
1050 HANDLE hand = __CRTDLL__fdtoh(fd);
1052 /* Dont trace small writes, it gets *very* annoying */
1054 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1055 if (hand == INVALID_HANDLE_VALUE)
1058 /* If appending, go to EOF */
1059 if (__CRTDLL_flags[fd] & _IOAPPEND)
1060 CRTDLL__lseek(fd, 0, FILE_END );
1062 /* Set _cnt to 0 so optimised binaries will call our implementation
1063 * of putc/getc. See _filbuf/_flsbuf comments.
1065 if (__CRTDLL_files[fd])
1066 __CRTDLL_files[fd]->_cnt = 0;
1068 if (WriteFile(hand, buf, count, &num_written, NULL)
1069 && (num_written == count))
1072 TRACE(":failed-last error (%ld)\n",GetLastError());
1073 if ( __CRTDLL_files[fd])
1074 __CRTDLL_files[fd]->_flag |= _IOERR;
1080 /*********************************************************************
1081 * clearerr (CRTDLL.349)
1083 * Clear a FILE's error indicator.
1085 VOID __cdecl CRTDLL_clearerr(CRTDLL_FILE* file)
1087 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1088 file->_flag &= ~(_IOERR | _IOEOF);
1092 /*********************************************************************
1093 * fclose (CRTDLL.362)
1095 * Close an open file.
1097 INT __cdecl CRTDLL_fclose( CRTDLL_FILE* file )
1099 return CRTDLL__close(file->_file);
1103 /*********************************************************************
1106 * Check the eof indicator on a file.
1108 INT __cdecl CRTDLL_feof( CRTDLL_FILE* file )
1110 return file->_flag & _IOEOF;
1114 /*********************************************************************
1115 * ferror (CRTDLL.361)
1117 * Check the error indicator on a file.
1119 INT __cdecl CRTDLL_ferror( CRTDLL_FILE* file )
1121 return file->_flag & _IOERR;
1125 /*********************************************************************
1126 * fflush (CRTDLL.362)
1128 INT __cdecl CRTDLL_fflush( CRTDLL_FILE* file )
1130 return CRTDLL__commit(file->_file);
1134 /*********************************************************************
1135 * fgetc (CRTDLL.363)
1137 INT __cdecl CRTDLL_fgetc( CRTDLL_FILE* file )
1140 if (CRTDLL__read(file->_file,&c,1) != 1)
1146 /*********************************************************************
1147 * fgetpos (CRTDLL.364)
1149 INT __cdecl CRTDLL_fgetpos( CRTDLL_FILE* file, fpos_t *pos)
1151 *pos = CRTDLL__tell(file->_file);
1152 return (*pos == -1? -1 : 0);
1156 /*********************************************************************
1157 * fgets (CRTDLL.365)
1159 CHAR* __cdecl CRTDLL_fgets(LPSTR s, INT size, CRTDLL_FILE* file)
1162 LPSTR buf_start = s;
1164 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1165 file,file->_file,s,size);
1167 /* BAD, for the whole WINE process blocks... just done this way to test
1168 * windows95's ftp.exe.
1169 * JG - Is this true now we use ReadFile() on stdin too?
1171 for(cc = CRTDLL_fgetc(file); cc != EOF && cc != '\n';
1172 cc = CRTDLL_fgetc(file))
1175 if (--size <= 0) break;
1178 if ((cc == EOF) && (s == buf_start)) /* If nothing read, return 0*/
1180 TRACE(":nothing read\n");
1187 TRACE(":got '%s'\n", buf_start);
1192 /*********************************************************************
1193 * fputs (CRTDLL.375)
1195 INT __cdecl CRTDLL_fputs( LPCSTR s, CRTDLL_FILE* file )
1197 return CRTDLL_fwrite(s,strlen(s),1,file);
1201 /*********************************************************************
1202 * fprintf (CRTDLL.370)
1204 INT __cdecl CRTDLL_fprintf( CRTDLL_FILE* file, LPCSTR format, ... )
1209 va_start( valist, format );
1210 res = CRTDLL_vfprintf( file, format, valist );
1216 /*********************************************************************
1217 * fopen (CRTDLL.372)
1221 CRTDLL_FILE* __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
1224 INT flags = 0, plus = 0, fd;
1225 const char* search = mode;
1227 TRACE(":path (%s) mode (%s)\n",path,mode);
1230 if (*search++ == '+')
1233 /* map mode string to open() flags. "man fopen" for possibilities. */
1237 flags = (plus ? _O_RDWR : _O_RDONLY);
1240 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1243 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1258 flags &= ~_O_BINARY;
1263 FIXME(":unknown flag %c not supported\n",mode[-1]);
1266 fd = CRTDLL__open(path, flags);
1271 file = __CRTDLL__alloc_fp(fd);
1272 TRACE(":get file (%p)\n",file);
1280 /*********************************************************************
1281 * fputc (CRTDLL.374)
1283 INT __cdecl CRTDLL_fputc( INT c, CRTDLL_FILE* file )
1285 return CRTDLL__write(file->_file, &c, 1) == 1? c : EOF;
1289 /*********************************************************************
1290 * fread (CRTDLL.377)
1292 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE* file)
1294 DWORD read = CRTDLL__read(file->_file,ptr, size * nmemb);
1301 /*********************************************************************
1302 * freopen (CRTDLL.379)
1305 CRTDLL_FILE* __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode,CRTDLL_FILE* file)
1307 CRTDLL_FILE* newfile;
1310 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1311 if (!file || ((fd = file->_file) < 0) || fd > __CRTDLL_fdend)
1316 FIXME(":reopen on user file not implemented!\n");
1317 __CRTDLL__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1321 /* first, create the new file */
1322 if ((newfile = CRTDLL_fopen(path,mode)) == NULL)
1325 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1326 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1327 __CRTDLL_handles[newfile->_file]))
1329 /* Redirecting std handle to file , copy over.. */
1330 __CRTDLL_handles[fd] = __CRTDLL_handles[newfile->_file];
1331 __CRTDLL_flags[fd] = __CRTDLL_flags[newfile->_file];
1332 memcpy(&__CRTDLL_iob[fd], newfile, sizeof (CRTDLL_FILE));
1333 __CRTDLL_iob[fd]._file = fd;
1334 /* And free up the resources allocated by fopen, but
1335 * not the HANDLE we copied. */
1336 CRTDLL_free(__CRTDLL_files[fd]);
1337 __CRTDLL__free_fd(newfile->_file);
1338 return &__CRTDLL_iob[fd];
1341 WARN(":failed-last error (%ld)\n",GetLastError());
1342 CRTDLL_fclose(newfile);
1343 __CRTDLL__set_errno(GetLastError());
1348 /*********************************************************************
1349 * fsetpos (CRTDLL.380)
1351 INT __cdecl CRTDLL_fsetpos( CRTDLL_FILE* file, fpos_t *pos)
1353 return CRTDLL__lseek(file->_file,*pos,SEEK_SET);
1357 /*********************************************************************
1358 * fscanf (CRTDLL.381)
1360 INT __cdecl CRTDLL_fscanf( CRTDLL_FILE* file, LPSTR format, ... )
1365 if (!*format) return 0;
1366 WARN("%p (\"%s\"): semi-stub\n", file, format);
1367 nch = CRTDLL_fgetc(file);
1368 va_start(ap, format);
1370 if (*format == ' ') {
1371 /* skip whitespace */
1372 while ((nch!=EOF) && isspace(nch))
1373 nch = CRTDLL_fgetc(file);
1375 else if (*format == '%') {
1379 case 'd': { /* read an integer */
1380 int*val = va_arg(ap, int*);
1382 /* skip initial whitespace */
1383 while ((nch!=EOF) && isspace(nch))
1384 nch = CRTDLL_fgetc(file);
1385 /* get sign and first digit */
1387 nch = CRTDLL_fgetc(file);
1396 nch = CRTDLL_fgetc(file);
1397 /* read until no more digits */
1398 while ((nch!=EOF) && isdigit(nch)) {
1399 cur = cur*10 + (nch - '0');
1400 nch = CRTDLL_fgetc(file);
1406 case 'f': { /* read a float */
1407 float*val = va_arg(ap, float*);
1409 /* skip initial whitespace */
1410 while ((nch!=EOF) && isspace(nch))
1411 nch = CRTDLL_fgetc(file);
1412 /* get sign and first digit */
1414 nch = CRTDLL_fgetc(file);
1423 /* read until no more digits */
1424 while ((nch!=EOF) && isdigit(nch)) {
1425 cur = cur*10 + (nch - '0');
1426 nch = CRTDLL_fgetc(file);
1429 /* handle decimals */
1431 nch = CRTDLL_fgetc(file);
1432 while ((nch!=EOF) && isdigit(nch)) {
1434 cur += dec * (nch - '0');
1435 nch = CRTDLL_fgetc(file);
1442 case 's': { /* read a word */
1443 char*str = va_arg(ap, char*);
1445 /* skip initial whitespace */
1446 while ((nch!=EOF) && isspace(nch))
1447 nch = CRTDLL_fgetc(file);
1448 /* read until whitespace */
1449 while ((nch!=EOF) && !isspace(nch)) {
1450 *sptr++ = nch; st++;
1451 nch = CRTDLL_fgetc(file);
1455 TRACE("read word: %s\n", str);
1458 default: FIXME("unhandled: %%%c\n", *format);
1464 /* check for character match */
1466 nch = CRTDLL_fgetc(file);
1473 WARN("need ungetch\n");
1475 TRACE("returning %d\n", rd);
1480 /*********************************************************************
1481 * fseek (CRTDLL.382)
1483 LONG __cdecl CRTDLL_fseek( CRTDLL_FILE* file, LONG offset, INT whence)
1485 return CRTDLL__lseek(file->_file,offset,whence);
1489 /*********************************************************************
1490 * ftell (CRTDLL.381)
1492 LONG __cdecl CRTDLL_ftell( CRTDLL_FILE* file )
1494 return CRTDLL__tell(file->_file);
1498 /*********************************************************************
1499 * fwrite (CRTDLL.383)
1501 UINT __cdecl CRTDLL_fwrite( LPCVOID ptr, INT size, INT nmemb, CRTDLL_FILE* file )
1503 UINT written = CRTDLL__write(file->_file, ptr, size * nmemb);
1506 return written / size;
1510 /*********************************************************************
1511 * getchar (CRTDLL.386)
1513 INT __cdecl CRTDLL_getchar( VOID )
1515 return CRTDLL_fgetc(CRTDLL_stdin);
1519 /*********************************************************************
1522 INT __cdecl CRTDLL_getc( CRTDLL_FILE* file )
1524 return CRTDLL_fgetc( file );
1528 /*********************************************************************
1531 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
1534 LPSTR buf_start = buf;
1536 /* BAD, for the whole WINE process blocks... just done this way to test
1537 * windows95's ftp.exe.
1538 * JG 19/9/00: Is this still true, now we are using ReadFile?
1540 for(cc = CRTDLL_fgetc(CRTDLL_stdin); cc != EOF && cc != '\n';
1541 cc = CRTDLL_fgetc(CRTDLL_stdin))
1542 if(cc != '\r') *buf++ = (char)cc;
1546 TRACE("got '%s'\n", buf_start);
1551 /*********************************************************************
1554 INT __cdecl CRTDLL_putc( INT c, CRTDLL_FILE* file )
1556 return CRTDLL_fputc( c, file );
1560 /*********************************************************************
1561 * putchar (CRTDLL.442)
1563 void __cdecl CRTDLL_putchar( INT c )
1565 CRTDLL_fputc(c, CRTDLL_stdout);
1569 /*********************************************************************
1572 INT __cdecl CRTDLL_puts(LPCSTR s)
1574 return CRTDLL_fputs(s, CRTDLL_stdout);
1578 /*********************************************************************
1579 * rewind (CRTDLL.447)
1581 * Set the file pointer to the start of a file and clear any error
1584 VOID __cdecl CRTDLL_rewind(CRTDLL_FILE* file)
1586 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1587 CRTDLL__lseek(file->_file,0,SEEK_SET);
1588 file->_flag &= ~(_IOEOF | _IOERR);
1592 /*********************************************************************
1593 * remove (CRTDLL.448)
1595 INT __cdecl CRTDLL_remove(LPCSTR path)
1597 TRACE(":path (%s)\n",path);
1598 if (DeleteFileA(path))
1600 TRACE(":failed-last error (%ld)\n",GetLastError());
1601 __CRTDLL__set_errno(GetLastError());
1606 /*********************************************************************
1607 * rename (CRTDLL.449)
1609 INT __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1611 TRACE(":from %s to %s\n",oldpath,newpath);
1612 if (MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1614 TRACE(":failed-last error (%ld)\n",GetLastError());
1615 __CRTDLL__set_errno(GetLastError());
1620 /*********************************************************************
1621 * setbuf (CRTDLL.452)
1623 INT __cdecl CRTDLL_setbuf(CRTDLL_FILE* file, LPSTR buf)
1625 TRACE(":file (%p) fd (%d) buf (%p)\n", file, file->_file,buf);
1627 WARN(":user buffer will not be used!\n");
1628 /* FIXME: no buffering for now */
1633 /*********************************************************************
1634 * tmpnam (CRTDLL.490)
1636 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1639 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1641 char tmpbuf[MAX_PATH];
1642 char* prefix = "TMP";
1643 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
1644 !GetTempFileNameA(tmpbuf,prefix,0,CRTDLL_tmpname))
1646 TRACE(":failed-last error (%ld)\n",GetLastError());
1649 TRACE(":got tmpnam %s\n",CRTDLL_tmpname);
1655 /*********************************************************************
1656 * vfprintf (CRTDLL.494)
1658 * Write formatted output to a file.
1661 /* we have avoided libc stdio.h so far, lets not start now */
1662 extern int vsprintf(void *, const void *, va_list);
1664 /********************************************************************/
1666 INT __cdecl CRTDLL_vfprintf( CRTDLL_FILE* file, LPCSTR format, va_list args )
1668 /* FIXME: We should parse the format string, calculate the maximum,
1669 * length of each arg, malloc a buffer, print to it, and fwrite that.
1670 * Yes this sucks, but not as much as crashing 1/2 way through an
1671 * app writing to a file :-(
1674 TRACE(":file (%p) fd (%d) fmt (%s)\n",file,file->_file,format);
1676 vsprintf( buffer, format, args );
1677 return CRTDLL_fwrite( buffer, 1, strlen(buffer), file );