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 compatable 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 CTRDLL__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.
317 /* FIXME: Care for large files */
318 INT __cdecl CRTDLL__eof( INT fd )
321 HANDLE hand = __CRTDLL__fdtoh(fd);
323 TRACE(":fd (%d) handle (%d)\n",fd,hand);
325 if (hand == INVALID_HANDLE_VALUE)
328 /* If we have a FILE* for this file, the EOF flag
329 * will be set by the read()/write() functions.
331 if (__CRTDLL_files[fd])
332 return __CRTDLL_files[fd]->_flag & _IOEOF;
334 /* Otherwise we do it the hard way */
335 curpos = SetFilePointer( hand, 0, NULL, SEEK_CUR );
336 endpos = SetFilePointer( hand, 0, NULL, FILE_END );
338 if (curpos == endpos)
341 SetFilePointer( hand, curpos, 0, FILE_BEGIN);
346 /*********************************************************************
347 * _fcloseall (CRTDLL.089)
349 * Close all open files except stdin/stdout/stderr.
351 INT __cdecl CRTDLL__fcloseall(VOID)
353 int num_closed = 0, i = 3;
355 while(i < __CRTDLL_fdend)
356 if (__CRTDLL_handles[i] != INVALID_HANDLE_VALUE)
362 TRACE(":closed (%d) handles\n",num_closed);
367 /*********************************************************************
368 * _fdopen (CRTDLL.091)
370 * Get a FILE* from a low level file descriptor.
372 CRTDLL_FILE* __cdecl CRTDLL__fdopen(INT fd, LPCSTR mode)
374 CRTDLL_FILE* file = __CRTDLL__alloc_fp(fd);
376 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
384 /*********************************************************************
385 * _fgetchar (CRTDLL.092)
387 INT __cdecl CRTDLL__fgetchar( VOID )
389 return CRTDLL_fgetc(CRTDLL_stdin);
393 /*********************************************************************
394 * _filbuf (CRTDLL.094)
397 * The macro version of getc calls this function whenever FILE->_cnt
398 * becomes negative. We ensure that _cnt is always 0 after any read
399 * so this function is always called. Our implementation simply calls
400 * fgetc as all the underlying buffering is handled by Wines
401 * implementation of the Win32 file I/O calls.
403 INT __cdecl CRTDLL__filbuf(CRTDLL_FILE* file)
405 return CRTDLL_fgetc(file);
408 /*********************************************************************
409 * _filelength (CRTDLL.097)
411 * Get the length of an open file.
413 LONG __cdecl CRTDLL__filelength(INT fd)
415 LONG curPos = CRTDLL__lseek(fd, 0, SEEK_CUR);
418 LONG endPos = CRTDLL__lseek(fd, 0, SEEK_END);
421 if (endPos != curPos)
422 CRTDLL__lseek(fd, curPos, SEEK_SET);
430 /*********************************************************************
431 * _fileno (CRTDLL.097)
433 * Get the file descriptor from a FILE*.
436 * This returns the CRTDLL fd, _not_ the underlying *nix fd.
438 INT __cdecl CRTDLL__fileno(CRTDLL_FILE* file)
440 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
445 /*********************************************************************
446 * _flsbuf (CRTDLL.102)
449 * The macro version of putc calls this function whenever FILE->_cnt
450 * becomes negative. We ensure that _cnt is always 0 after any write
451 * so this function is always called. Our implementation simply calls
452 * fputc as all the underlying buffering is handled by Wines
453 * implementation of the Win32 file I/O calls.
455 INT __cdecl CRTDLL__flsbuf(INT c, CRTDLL_FILE* file)
457 return CRTDLL_fputc(c,file);
461 /*********************************************************************
462 * _flushall (CRTDLL.103)
464 * Flush all open files.
466 INT __cdecl CRTDLL__flushall(VOID)
468 int num_flushed = 0, i = 3;
470 while(i < __CRTDLL_fdend)
471 if (__CRTDLL_handles[i] != INVALID_HANDLE_VALUE)
473 if (CRTDLL__commit(i) == -1)
474 if (__CRTDLL_files[i])
475 __CRTDLL_files[i]->_flag |= _IOERR;
479 TRACE(":flushed (%d) handles\n",num_flushed);
484 /*********************************************************************
485 * _fputchar (CRTDLL.108)
487 * Put a character to a file.
489 INT __cdecl CRTDLL__fputchar(INT c)
491 return CRTDLL_fputc(c, CRTDLL_stdout);
495 /*********************************************************************
496 * _fsopen (CRTDLL.110)
498 * Open a FILE* with sharing.
500 CRTDLL_FILE* __cdecl CRTDLL__fsopen(LPCSTR path, LPCSTR mode, INT share)
502 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
503 return CRTDLL_fopen(path,mode);
507 /*********************************************************************
508 * _fstat (CRTDLL.111)
510 * Get information about an open file.
512 int __cdecl CRTDLL__fstat(int fd, struct _stat* buf)
515 BY_HANDLE_FILE_INFORMATION hfi;
516 HANDLE hand = __CRTDLL__fdtoh(fd);
518 TRACE(":fd (%d) stat (%p)\n",fd,buf);
519 if (hand == INVALID_HANDLE_VALUE)
524 WARN(":failed-NULL buf\n");
525 __CRTDLL__set_errno(ERROR_INVALID_PARAMETER);
529 memset(&hfi, 0, sizeof(hfi));
530 memset(buf, 0, sizeof(struct _stat));
531 if (!GetFileInformationByHandle(hand, &hfi))
533 WARN(":failed-last error (%ld)\n",GetLastError());
534 __CRTDLL__set_errno(ERROR_INVALID_PARAMETER);
537 FIXME(":dwFileAttributes = %d, mode set to 0",hfi.dwFileAttributes);
538 buf->st_nlink = hfi.nNumberOfLinks;
539 buf->st_size = hfi.nFileSizeLow;
540 RtlTimeToSecondsSince1970( &hfi.ftLastAccessTime, &dw );
542 RtlTimeToSecondsSince1970( &hfi.ftLastWriteTime, &dw );
543 buf->st_mtime = buf->st_ctime = dw;
548 /*********************************************************************
549 * _futime (CRTDLL.115)
551 * Set the file access/modification times on an open file.
553 INT __cdecl CRTDLL__futime(INT fd, struct _utimbuf *t)
555 HANDLE hand = __CRTDLL__fdtoh(fd);
561 CRTDLL_time(&currTime);
562 RtlSecondsSince1970ToTime( currTime, &at );
563 memcpy( &wt, &at, sizeof(wt) );
567 RtlSecondsSince1970ToTime( t->actime, &at );
568 if (t->actime == t->modtime)
569 memcpy( &wt, &at, sizeof(wt) );
571 RtlSecondsSince1970ToTime( t->modtime, &wt );
574 if (!SetFileTime( hand, NULL, &at, &wt ))
576 __CRTDLL__set_errno(GetLastError());
583 /*********************************************************************
584 * _get_osfhandle (CRTDLL.117)
586 * Return a Win32 HANDLE from a file descriptor.
589 * fd [in] A valid file descriptor
592 * Success: A Win32 HANDLE
594 * Failure: INVALID_HANDLE_VALUE.
597 HANDLE CRTDLL__get_osfhandle(INT fd)
599 HANDLE hand = __CRTDLL__fdtoh(fd);
600 HANDLE newhand = hand;
601 TRACE(":fd (%d) handle (%d)\n",fd,hand);
603 if (hand != INVALID_HANDLE_VALUE)
605 /* FIXME: I'm not convinced that I should be copying the
606 * handle here - it may be leaked if the app doesn't
607 * close it (and the API docs dont say that it should)
608 * Not duplicating it means that it can't be inherited
609 * and so lcc's wedit doesn't cope when it passes it to
610 * child processes. I've an idea that it should either
611 * be copied by CreateProcess, or marked as inheritable
612 * when initialised, or maybe both? JG 21-9-00.
614 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
615 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS );
621 /*********************************************************************
624 * Read an integter from a FILE*.
626 INT __cdecl CRTDLL__getw( CRTDLL_FILE* file )
629 if (CRTDLL__read(file->_file, &i, sizeof(INT)) != 1)
635 /*********************************************************************
636 * _isatty (CRTDLL.137)
638 * Return non zero if fd is a character device (e.g console).
640 INT __cdecl CRTDLL__isatty(INT fd)
642 HANDLE hand = __CRTDLL__fdtoh(fd);
644 TRACE(":fd (%d) handle (%d)\n",fd,hand);
645 if (hand == INVALID_HANDLE_VALUE)
648 return GetFileType(fd) == FILE_TYPE_CHAR? 1 : 0;
652 /*********************************************************************
653 * _lseek (CRTDLL.179)
655 * Move the file pointer within a file.
657 LONG __cdecl CRTDLL__lseek( INT fd, LONG offset, INT whence)
660 HANDLE hand = __CRTDLL__fdtoh(fd);
662 TRACE(":fd (%d) handle (%d)\n",fd,hand);
663 if (hand == INVALID_HANDLE_VALUE)
666 if (whence < 0 || whence > 2)
668 CRTDLL_errno = EINVAL;
672 TRACE(":fd (%d) to 0x%08lx pos %s\n",
673 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
674 (whence==SEEK_CUR)?"SEEK_CUR":
675 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
677 if ((ret = SetFilePointer( hand, offset, NULL, whence )) != 0xffffffff)
679 if ( __CRTDLL_files[fd])
680 __CRTDLL_files[fd]->_flag &= ~_IOEOF;
681 /* FIXME: What if we seek _to_ EOF - is EOF set? */
684 TRACE(":error-last error (%ld)\n",GetLastError());
685 if ( __CRTDLL_files[fd])
686 switch(GetLastError())
688 case ERROR_NEGATIVE_SEEK:
689 case ERROR_SEEK_ON_DEVICE:
690 __CRTDLL__set_errno(GetLastError());
691 __CRTDLL_files[fd]->_flag |= _IOERR;
699 /*********************************************************************
700 * _mktemp (CRTDLL.239)
702 * Create a temporary file name.
704 LPSTR __cdecl CRTDLL__mktemp(LPSTR pattern)
707 LPSTR retVal = pattern;
712 numX = (*pattern++ == 'X')? numX + 1 : 0;
716 id = GetCurrentProcessId();
720 INT tempNum = id / 10;
721 *pattern-- = id - (tempNum * 10) + '0';
727 if (GetFileAttributesA( retVal ) == 0xFFFFFFFF &&
728 GetLastError() == ERROR_FILE_NOT_FOUND)
731 } while(letter != '|');
735 /*********************************************************************
739 INT __cdecl CRTDLL__open(LPCSTR path,INT flags)
741 DWORD access = 0, creation = 0;
745 TRACE(":file (%s) mode 0x%04x\n",path,flags);
747 switch(flags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
750 access |= GENERIC_READ;
754 access |= GENERIC_WRITE;
758 access |= GENERIC_WRITE | GENERIC_READ;
763 if (flags & _O_CREAT)
766 creation = CREATE_NEW;
767 else if (flags & _O_TRUNC)
768 creation = CREATE_ALWAYS;
770 creation = OPEN_ALWAYS;
772 else /* no _O_CREAT */
774 if (flags & _O_TRUNC)
775 creation = TRUNCATE_EXISTING;
777 creation = OPEN_EXISTING;
779 if (flags & _O_APPEND)
783 flags |= _O_BINARY; /* FIXME: Default to text */
787 /* Dont warn when writing */
788 if (ioflag & GENERIC_READ)
789 FIXME(":TEXT node not implemented\n");
793 if (flags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL|_O_CREAT|_O_RDWR))
794 TRACE(":unsupported flags 0x%04x\n",flags);
796 /* clear those pesky flags ;-) */
797 flags &= (_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL|_O_CREAT|_O_RDWR);
799 hand = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
800 NULL, creation, FILE_ATTRIBUTE_NORMAL, -1);
802 if (hand == INVALID_HANDLE_VALUE)
804 WARN(":failed-last error (%ld)\n",GetLastError());
805 __CRTDLL__set_errno(GetLastError());
809 fd = __CRTDLL__alloc_fd(hand,ioflag);
811 TRACE(":fd (%d) handle (%d)\n",fd, hand);
813 if (flags & _IOAPPEND && fd > 0)
814 CRTDLL__lseek(fd, 0, FILE_END );
820 /*********************************************************************
821 * _open_osfhandle (CRTDLL.240)
823 * Create a file descriptor for a file HANDLE.
825 INT __cdecl CRTDLL__open_osfhandle(HANDLE hand, INT flags)
827 INT fd = __CRTDLL__alloc_fd(hand,flags);
828 TRACE(":handle (%d) fd (%d)\n",hand,fd);
833 /*********************************************************************
836 * Write an int to a FILE*.
838 INT __cdecl CRTDLL__putw(INT val, CRTDLL_FILE* file)
840 return CRTDLL__write(file->_file, &val, sizeof(val)) == 1? val : EOF;
844 /*********************************************************************
847 * Read data from a file.
849 INT __cdecl CRTDLL__read(INT fd, LPVOID buf, UINT count)
852 HANDLE hand = __CRTDLL__fdtoh(fd);
854 /* Dont trace small reads, it gets *very* annoying */
856 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
857 if (hand == INVALID_HANDLE_VALUE)
860 /* Set _cnt to 0 so optimised binaries will call our implementation
861 * of putc/getc. See _filbuf/_flsbuf comments.
863 if (__CRTDLL_files[fd])
864 __CRTDLL_files[fd]->_cnt = 0;
866 if (ReadFile(hand, buf, count, &num_read, NULL))
868 if (num_read != count && __CRTDLL_files[fd])
871 __CRTDLL_files[fd]->_flag |= _IOEOF;
875 TRACE(":failed-last error (%ld)\n",GetLastError());
876 if ( __CRTDLL_files[fd])
877 __CRTDLL_files[fd]->_flag |= _IOERR;
882 /*********************************************************************
883 * _setmode (CRTDLL.265)
885 * FIXME: At present we ignore the request to translate CR/LF to LF.
887 * We always translate when we read with fgets, we never do with fread
890 INT __cdecl CRTDLL__setmode(INT fd,INT mode)
893 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
898 /*********************************************************************
901 INT __cdecl CRTDLL__stat(const char* path, struct _stat * buf)
904 WIN32_FILE_ATTRIBUTE_DATA hfi;
905 unsigned short mode = CRTDLL_S_IREAD;
908 TRACE(":file (%s) buf(%p)\n",path,buf);
910 if (!GetFileAttributesExA( path, GetFileExInfoStandard, &hfi ))
912 TRACE("failed-last error (%ld)\n",GetLastError());
913 __CRTDLL__set_errno(ERROR_FILE_NOT_FOUND);
917 memset(buf,0,sizeof(struct _stat));
919 /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
921 buf->st_dev = buf->st_rdev = toupper(*path - 'A'); /* drive num */
923 buf->st_dev = buf->st_rdev = CRTDLL__getdrive() - 1;
927 /* Dir, or regular file? */
928 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
929 (path[plen-1] == '\\'))
930 mode |= (_S_IFDIR | CRTDLL_S_IEXEC);
935 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
937 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8)
938 | (tolower(path[plen-3]) << 16);
939 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
940 mode |= CRTDLL_S_IEXEC;
944 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
945 mode |= CRTDLL_S_IWRITE;
949 buf->st_size = hfi.nFileSizeLow;
950 RtlTimeToSecondsSince1970( &hfi.ftLastAccessTime, &dw );
952 RtlTimeToSecondsSince1970( &hfi.ftLastWriteTime, &dw );
953 buf->st_mtime = buf->st_ctime = dw;
954 TRACE("\n%d %d %d %d %d %d\n", buf->st_mode,buf->st_nlink,buf->st_size,
955 buf->st_atime,buf->st_mtime, buf->st_ctime);
960 /*********************************************************************
963 * Get current file position.
965 LONG __cdecl CRTDLL__tell(INT fd)
967 return CRTDLL__lseek(fd, 0, SEEK_CUR);
971 /*********************************************************************
972 * _tempnam (CRTDLL.305)
975 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
977 char tmpbuf[MAX_PATH];
979 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
980 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
982 TRACE("got name (%s)\n",tmpbuf);
983 return CRTDLL__strdup(tmpbuf);
985 TRACE("failed-last error (%ld)\n",GetLastError());
990 /*********************************************************************
991 * _umask (CRTDLL.310)
993 * Set the process-wide umask.
995 INT __cdecl CRTDLL__umask(INT umask)
997 INT old_umask = __CRTDLL_umask;
998 TRACE("umask (%d)\n",umask);
999 __CRTDLL_umask = umask;
1004 /*********************************************************************
1005 * _utime (CRTDLL.314)
1007 * Set the file access/modification times on a file.
1009 INT __cdecl CRTDLL__utime(LPCSTR path, struct _utimbuf *t)
1011 INT fd = CRTDLL__open( path, _O_WRONLY | _O_BINARY );
1015 INT retVal = CRTDLL__futime(fd, t);
1023 /*********************************************************************
1024 * _unlink (CRTDLL.315)
1028 INT __cdecl CRTDLL__unlink(LPCSTR path)
1030 TRACE("path (%s)\n",path);
1031 if(DeleteFileA( path ))
1034 TRACE("failed-last error (%ld)\n",GetLastError());
1035 __CRTDLL__set_errno(GetLastError());
1040 /*********************************************************************
1041 * _write (CRTDLL.332)
1043 * Write data to a file.
1045 UINT __cdecl CRTDLL__write(INT fd, LPCVOID buf, UINT count)
1048 HANDLE hand = __CRTDLL__fdtoh(fd);
1050 /* Dont trace small writes, it gets *very* annoying */
1052 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1053 if (hand == INVALID_HANDLE_VALUE)
1056 /* If appending, go to EOF */
1057 if (__CRTDLL_flags[fd] & _IOAPPEND)
1058 CRTDLL__lseek(fd, 0, FILE_END );
1060 /* Set _cnt to 0 so optimised binaries will call our implementation
1061 * of putc/getc. See _filbuf/_flsbuf comments.
1063 if (__CRTDLL_files[fd])
1064 __CRTDLL_files[fd]->_cnt = 0;
1066 if (WriteFile(hand, buf, count, &num_written, NULL)
1067 && (num_written == count))
1070 TRACE(":failed-last error (%ld)\n",GetLastError());
1071 if ( __CRTDLL_files[fd])
1072 __CRTDLL_files[fd]->_flag |= _IOERR;
1078 /*********************************************************************
1079 * clearerr (CRTDLL.349)
1081 * Clear a FILE's error indicator.
1083 VOID __cdecl CRTDLL_clearerr(CRTDLL_FILE* file)
1085 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1086 file->_flag &= ~(_IOERR | _IOEOF);
1090 /*********************************************************************
1091 * fclose (CRTDLL.362)
1093 * Close an open file.
1095 INT __cdecl CRTDLL_fclose( CRTDLL_FILE* file )
1097 return CRTDLL__close(file->_file);
1101 /*********************************************************************
1104 * Check the eof indicator on a file.
1106 INT __cdecl CRTDLL_feof( CRTDLL_FILE* file )
1108 return file->_flag & _IOEOF;
1112 /*********************************************************************
1113 * ferror (CRTDLL.361)
1115 * Check the error indicator on a file.
1117 INT __cdecl CRTDLL_ferror( CRTDLL_FILE* file )
1119 return file->_flag & _IOERR;
1123 /*********************************************************************
1124 * fflush (CRTDLL.362)
1126 INT __cdecl CRTDLL_fflush( CRTDLL_FILE* file )
1128 return CRTDLL__commit(file->_file);
1132 /*********************************************************************
1133 * fgetc (CRTDLL.363)
1135 INT __cdecl CRTDLL_fgetc( CRTDLL_FILE* file )
1138 if (CRTDLL__read(file->_file,&c,1) != 1)
1144 /*********************************************************************
1145 * fgetpos (CRTDLL.364)
1147 INT __cdecl CRTDLL_fgetpos( CRTDLL_FILE* file, fpos_t *pos)
1149 *pos = CRTDLL__tell(file->_file);
1150 return (*pos == -1? -1 : 0);
1154 /*********************************************************************
1155 * fgets (CRTDLL.365)
1157 CHAR* __cdecl CRTDLL_fgets(LPSTR s, INT size, CRTDLL_FILE* file)
1160 LPSTR buf_start = s;
1162 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1163 file,file->_file,s,size);
1165 /* BAD, for the whole WINE process blocks... just done this way to test
1166 * windows95's ftp.exe.
1167 * JG - Is this true now we use ReadFile() on stdin too?
1169 for(cc = CRTDLL_fgetc(file); cc != EOF && cc != '\n';
1170 cc = CRTDLL_fgetc(file))
1173 if (--size <= 0) break;
1176 if ((cc == EOF) && (s == buf_start)) /* If nothing read, return 0*/
1178 TRACE(":nothing read\n");
1185 TRACE(":got '%s'\n", buf_start);
1190 /*********************************************************************
1191 * fputs (CRTDLL.375)
1193 INT __cdecl CRTDLL_fputs( LPCSTR s, CRTDLL_FILE* file )
1195 return CRTDLL_fwrite(s,strlen(s),1,file);
1199 /*********************************************************************
1200 * fprintf (CRTDLL.370)
1202 INT __cdecl CRTDLL_fprintf( CRTDLL_FILE* file, LPCSTR format, ... )
1207 va_start( valist, format );
1208 res = CRTDLL_vfprintf( file, format, valist );
1214 /*********************************************************************
1215 * fopen (CRTDLL.372)
1219 CRTDLL_FILE* __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
1222 INT flags = 0, plus = 0, fd;
1223 const char* search = mode;
1225 TRACE(":path (%s) mode (%s)\n",path,mode);
1228 if (*search++ == '+')
1231 /* map mode string to open() flags. "man fopen" for possibilities. */
1235 flags = (plus ? _O_RDWR : _O_RDONLY);
1238 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1241 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1256 flags &= ~_O_BINARY;
1261 FIXME(":unknown flag %c not supported\n",mode[-1]);
1264 fd = CRTDLL__open(path, flags);
1269 file = __CRTDLL__alloc_fp(fd);
1270 TRACE(":get file (%p)\n",file);
1278 /*********************************************************************
1279 * fputc (CRTDLL.374)
1281 INT __cdecl CRTDLL_fputc( INT c, CRTDLL_FILE* file )
1283 return CRTDLL__write(file->_file, &c, 1) == 1? c : EOF;
1287 /*********************************************************************
1288 * fread (CRTDLL.377)
1290 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE* file)
1292 DWORD read = CRTDLL__read(file->_file,ptr, size * nmemb);
1299 /*********************************************************************
1300 * freopen (CRTDLL.379)
1303 CRTDLL_FILE* __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode,CRTDLL_FILE* file)
1305 CRTDLL_FILE* newfile;
1308 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1309 if (!file || ((fd = file->_file) < 0) || fd > __CRTDLL_fdend)
1314 FIXME(":reopen on user file not implemented!\n");
1315 __CRTDLL__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1319 /* first, create the new file */
1320 if ((newfile = CRTDLL_fopen(path,mode)) == NULL)
1323 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1324 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1325 __CRTDLL_handles[newfile->_file]))
1327 /* Redirecting std handle to file , copy over.. */
1328 __CRTDLL_handles[fd] = __CRTDLL_handles[newfile->_file];
1329 __CRTDLL_flags[fd] = __CRTDLL_flags[newfile->_file];
1330 memcpy(&__CRTDLL_iob[fd], newfile, sizeof (CRTDLL_FILE));
1331 __CRTDLL_iob[fd]._file = fd;
1332 /* And free up the resources allocated by fopen, but
1333 * not the HANDLE we copied. */
1334 CRTDLL_free(__CRTDLL_files[fd]);
1335 __CRTDLL__free_fd(newfile->_file);
1336 return &__CRTDLL_iob[fd];
1339 WARN(":failed-last error (%ld)\n",GetLastError());
1340 CRTDLL_fclose(newfile);
1341 __CRTDLL__set_errno(GetLastError());
1346 /*********************************************************************
1347 * fsetpos (CRTDLL.380)
1349 INT __cdecl CRTDLL_fsetpos( CRTDLL_FILE* file, fpos_t *pos)
1351 return CRTDLL__lseek(file->_file,*pos,SEEK_SET);
1355 /*********************************************************************
1356 * fscanf (CRTDLL.381)
1358 INT __cdecl CRTDLL_fscanf( CRTDLL_FILE* file, LPSTR format, ... )
1363 if (!*format) return 0;
1364 WARN("%p (\"%s\"): semi-stub\n", file, format);
1365 nch = CRTDLL_fgetc(file);
1366 va_start(ap, format);
1368 if (*format == ' ') {
1369 /* skip whitespace */
1370 while ((nch!=EOF) && isspace(nch))
1371 nch = CRTDLL_fgetc(file);
1373 else if (*format == '%') {
1377 case 'd': { /* read an integer */
1378 int*val = va_arg(ap, int*);
1380 /* skip initial whitespace */
1381 while ((nch!=EOF) && isspace(nch))
1382 nch = CRTDLL_fgetc(file);
1383 /* get sign and first digit */
1385 nch = CRTDLL_fgetc(file);
1394 nch = CRTDLL_fgetc(file);
1395 /* read until no more digits */
1396 while ((nch!=EOF) && isdigit(nch)) {
1397 cur = cur*10 + (nch - '0');
1398 nch = CRTDLL_fgetc(file);
1404 case 'f': { /* read a float */
1405 float*val = va_arg(ap, float*);
1407 /* skip initial whitespace */
1408 while ((nch!=EOF) && isspace(nch))
1409 nch = CRTDLL_fgetc(file);
1410 /* get sign and first digit */
1412 nch = CRTDLL_fgetc(file);
1421 /* read until no more digits */
1422 while ((nch!=EOF) && isdigit(nch)) {
1423 cur = cur*10 + (nch - '0');
1424 nch = CRTDLL_fgetc(file);
1427 /* handle decimals */
1429 nch = CRTDLL_fgetc(file);
1430 while ((nch!=EOF) && isdigit(nch)) {
1432 cur += dec * (nch - '0');
1433 nch = CRTDLL_fgetc(file);
1440 case 's': { /* read a word */
1441 char*str = va_arg(ap, char*);
1443 /* skip initial whitespace */
1444 while ((nch!=EOF) && isspace(nch))
1445 nch = CRTDLL_fgetc(file);
1446 /* read until whitespace */
1447 while ((nch!=EOF) && !isspace(nch)) {
1448 *sptr++ = nch; st++;
1449 nch = CRTDLL_fgetc(file);
1453 TRACE("read word: %s\n", str);
1456 default: FIXME("unhandled: %%%c\n", *format);
1462 /* check for character match */
1464 nch = CRTDLL_fgetc(file);
1471 WARN("need ungetch\n");
1473 TRACE("returning %d\n", rd);
1478 /*********************************************************************
1479 * fseek (CRTDLL.382)
1481 LONG __cdecl CRTDLL_fseek( CRTDLL_FILE* file, LONG offset, INT whence)
1483 return CRTDLL__lseek(file->_file,offset,whence);
1487 /*********************************************************************
1488 * ftell (CRTDLL.381)
1490 LONG __cdecl CRTDLL_ftell( CRTDLL_FILE* file )
1492 return CRTDLL__tell(file->_file);
1496 /*********************************************************************
1497 * fwrite (CRTDLL.383)
1499 UINT __cdecl CRTDLL_fwrite( LPCVOID ptr, INT size, INT nmemb, CRTDLL_FILE* file )
1501 UINT written = CRTDLL__write(file->_file, ptr, size * nmemb);
1504 return written / size;
1508 /*********************************************************************
1509 * getchar (CRTDLL.386)
1511 INT __cdecl CRTDLL_getchar( VOID )
1513 return CRTDLL_fgetc(CRTDLL_stdin);
1517 /*********************************************************************
1520 INT __cdecl CRTDLL_getc( CRTDLL_FILE* file )
1522 return CRTDLL_fgetc( file );
1526 /*********************************************************************
1529 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
1532 LPSTR buf_start = buf;
1534 /* BAD, for the whole WINE process blocks... just done this way to test
1535 * windows95's ftp.exe.
1536 * JG 19/9/00: Is this still true, now we are using ReadFile?
1538 for(cc = CRTDLL_fgetc(CRTDLL_stdin); cc != EOF && cc != '\n';
1539 cc = CRTDLL_fgetc(CRTDLL_stdin))
1540 if(cc != '\r') *buf++ = (char)cc;
1544 TRACE("got '%s'\n", buf_start);
1549 /*********************************************************************
1552 INT __cdecl CRTDLL_putc( INT c, CRTDLL_FILE* file )
1554 return CRTDLL_fputc( c, file );
1558 /*********************************************************************
1559 * putchar (CRTDLL.442)
1561 void __cdecl CRTDLL_putchar( INT c )
1563 CRTDLL_fputc(c, CRTDLL_stdout);
1567 /*********************************************************************
1570 INT __cdecl CRTDLL_puts(LPCSTR s)
1572 return CRTDLL_fputs(s, CRTDLL_stdout);
1576 /*********************************************************************
1577 * rewind (CRTDLL.447)
1579 * Set the file pointer to the start of a file and clear any error
1582 VOID __cdecl CRTDLL_rewind(CRTDLL_FILE* file)
1584 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1585 CRTDLL__lseek(file->_file,0,SEEK_SET);
1586 file->_flag &= ~(_IOEOF | _IOERR);
1590 /*********************************************************************
1591 * remove (CRTDLL.448)
1593 INT __cdecl CRTDLL_remove(LPCSTR path)
1595 TRACE(":path (%s)\n",path);
1596 if (DeleteFileA(path))
1598 TRACE(":failed-last error (%ld)\n",GetLastError());
1599 __CRTDLL__set_errno(GetLastError());
1604 /*********************************************************************
1605 * rename (CRTDLL.449)
1607 INT __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1609 TRACE(":from %s to %s\n",oldpath,newpath);
1610 if (MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1612 TRACE(":failed-last error (%ld)\n",GetLastError());
1613 __CRTDLL__set_errno(GetLastError());
1618 /*********************************************************************
1619 * setbuf (CRTDLL.452)
1621 INT __cdecl CRTDLL_setbuf(CRTDLL_FILE* file, LPSTR buf)
1623 TRACE(":file (%p) fd (%d) buf (%p)\n", file, file->_file,buf);
1625 WARN(":user buffer will not be used!\n");
1626 /* FIXME: no buffering for now */
1631 /*********************************************************************
1632 * tmpnam (CRTDLL.490)
1634 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1637 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1639 char tmpbuf[MAX_PATH];
1640 char* prefix = "TMP";
1641 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
1642 !GetTempFileNameA(tmpbuf,prefix,0,CRTDLL_tmpname))
1644 TRACE(":failed-last error (%ld)\n",GetLastError());
1647 TRACE(":got tmpnam %s\n",CRTDLL_tmpname);
1653 /*********************************************************************
1654 * vfprintf (CRTDLL.494)
1656 * Write formatted output to a file.
1658 /* we have avoided libc stdio.h so far, lets not start now */
1659 extern int vsprintf(void *, const void *, va_list);
1661 INT __cdecl CRTDLL_vfprintf( CRTDLL_FILE* file, LPCSTR format, va_list args )
1663 /* FIXME: We should parse the format string, calculate the maximum,
1664 * length of each arg, malloc a buffer, print to it, and fwrite that.
1665 * Yes this sucks, but not as much as crashing 1/2 way through an
1666 * app writing to a file :-(
1669 TRACE(":file (%p) fd (%d) fmt (%s)\n",file,file->_file,format);
1671 vsprintf( buffer, format, args );
1672 return CRTDLL_fwrite( buffer, 1, strlen(buffer), file );