Implemented the remaining 64-bit file functions, and added a few other
[wine/multimedia.git] / dlls / msvcrt / file.c
bloba9287011611a498124ecc6b2c7c7fd7bca0d04bb
1 /*
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <time.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "winternl.h"
38 #include "msvcrt.h"
39 #include "msvcrt/errno.h"
41 #include "wine/unicode.h"
42 #include "msvcrt/direct.h"
43 #include "msvcrt/fcntl.h"
44 #include "msvcrt/io.h"
45 #include "msvcrt/sys/locking.h"
46 #include "msvcrt/stdio.h"
47 #include "msvcrt/stdlib.h"
48 #include "msvcrt/string.h"
49 #include "msvcrt/sys/stat.h"
50 #include "msvcrt/sys/utime.h"
51 #include "msvcrt/time.h"
52 #include "msvcrt/share.h"
53 #include "msvcrt/wctype.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
59 /* for stat mode, permissions apply to all,owner and group */
60 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
61 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
62 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
64 /* _access() bit flags FIXME: incomplete */
65 #define MSVCRT_W_OK 0x02
68 /* FIXME: Make this dynamic */
69 #define MSVCRT_MAX_FILES 257
71 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
72 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
73 int MSVCRT_flags[MSVCRT_MAX_FILES];
74 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
75 MSVCRT_FILE MSVCRT__iob[3];
76 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
77 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
78 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
80 static int MSVCRT_fdstart = 3; /* first unallocated fd */
81 static int MSVCRT_fdend = 3; /* highest allocated fd */
83 /* INTERNAL: process umask */
84 static int MSVCRT_umask = 0;
86 /* INTERNAL: Static buffer for temp file name */
87 static char MSVCRT_tmpname[MAX_PATH];
89 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
90 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
91 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
92 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
94 #define TOUL(x) (ULONGLONG)(x)
95 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
96 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
97 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
98 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
100 extern CRITICAL_SECTION MSVCRT_file_cs;
101 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
102 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
104 static void msvcrt_cp_from_stati64(const struct _stati64 *bufi64, struct _stat *buf)
106 buf->st_dev = bufi64->st_dev;
107 buf->st_ino = bufi64->st_ino;
108 buf->st_mode = bufi64->st_mode;
109 buf->st_nlink = bufi64->st_nlink;
110 buf->st_uid = bufi64->st_uid;
111 buf->st_gid = bufi64->st_gid;
112 buf->st_rdev = bufi64->st_rdev;
113 buf->st_size = bufi64->st_size;
114 buf->st_atime = bufi64->st_atime;
115 buf->st_mtime = bufi64->st_mtime;
116 buf->st_ctime = bufi64->st_ctime;
119 /* INTERNAL: Get the HANDLE for a fd */
120 static HANDLE msvcrt_fdtoh(int fd)
122 if (fd < 0 || fd >= MSVCRT_fdend ||
123 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
125 WARN(":fd (%d) - no handle!\n",fd);
126 *__doserrno() = 0;
127 *MSVCRT__errno() = MSVCRT_EBADF;
128 return INVALID_HANDLE_VALUE;
130 return MSVCRT_handles[fd];
133 /* INTERNAL: free a file entry fd */
134 static void msvcrt_free_fd(int fd)
136 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
137 MSVCRT_files[fd] = 0;
138 MSVCRT_flags[fd] = 0;
139 TRACE(":fd (%d) freed\n",fd);
140 if (fd < 3)
141 return; /* don't use 0,1,2 for user files */
142 if (fd == MSVCRT_fdend - 1)
143 MSVCRT_fdend--;
144 if (fd < MSVCRT_fdstart)
145 MSVCRT_fdstart = fd;
148 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
149 static int msvcrt_alloc_fd(HANDLE hand, int flag)
151 int fd = MSVCRT_fdstart;
153 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
154 if (fd >= MSVCRT_MAX_FILES)
156 WARN(":files exhausted!\n");
157 return -1;
159 MSVCRT_handles[fd] = hand;
160 MSVCRT_flags[fd] = flag;
162 /* locate next free slot */
163 if (fd == MSVCRT_fdend)
164 MSVCRT_fdstart = ++MSVCRT_fdend;
165 else
166 while(MSVCRT_fdstart < MSVCRT_fdend &&
167 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
168 MSVCRT_fdstart++;
170 return fd;
173 /* INTERNAL: Allocate a FILE* for an fd slot
174 * This is done lazily to avoid memory wastage for low level open/write
175 * usage when a FILE* is not requested (but may be later).
177 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
179 TRACE(":fd (%d) allocating FILE*\n",fd);
180 if (fd < 0 || fd >= MSVCRT_fdend ||
181 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
183 WARN(":invalid fd %d\n",fd);
184 *__doserrno() = 0;
185 *MSVCRT__errno() = MSVCRT_EBADF;
186 return NULL;
188 if (!MSVCRT_files[fd])
190 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
192 MSVCRT_files[fd]->_file = fd;
193 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
194 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
197 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
198 return MSVCRT_files[fd];
202 /* INTERNAL: Set up stdin, stderr and stdout */
203 void msvcrt_init_io(void)
205 int i;
206 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
207 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
208 GetCurrentProcess(), &MSVCRT_handles[0], 0, FALSE, DUPLICATE_SAME_ACCESS);
209 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
210 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
211 GetCurrentProcess(), &MSVCRT_handles[1], 0, FALSE, DUPLICATE_SAME_ACCESS);
212 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
213 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
214 GetCurrentProcess(), &MSVCRT_handles[2], 0, FALSE, DUPLICATE_SAME_ACCESS);
215 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
217 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles[0],
218 MSVCRT_handles[1],MSVCRT_handles[2]);
220 for (i = 0; i < 3; i++)
222 /* FILE structs for stdin/out/err are static and never deleted */
223 MSVCRT_files[i] = &MSVCRT__iob[i];
224 MSVCRT__iob[i]._file = i;
225 MSVCRT_tempfiles[i] = NULL;
229 /* free everything on process exit */
230 void msvcrt_free_io(void)
232 _fcloseall();
233 _close(0);
234 _close(1);
235 _close(2);
238 /* INTERNAL: Flush stdio file buffer */
239 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
241 if(file->_bufsiz) {
242 int cnt=file->_ptr-file->_base;
243 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
244 return MSVCRT_EOF;
246 file->_ptr=file->_base;
247 file->_cnt=file->_bufsiz;
249 return 0;
252 /* INTERNAL: Allocate stdio file buffer */
253 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
255 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
256 if(file->_base) {
257 file->_bufsiz = MSVCRT_BUFSIZ;
258 file->_flag |= MSVCRT__IOMYBUF;
259 } else {
260 file->_base = (unsigned char *)(&file->_charbuf);
261 /* put here 2 ??? */
262 file->_bufsiz = sizeof(file->_charbuf);
264 file->_ptr = file->_base;
265 file->_cnt = 0;
268 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
269 static void msvcrt_int_to_base32(int num, char *str)
271 char *p;
272 int n = num;
273 int digits = 0;
275 while (n != 0)
277 n >>= 5;
278 digits++;
280 p = str + digits;
281 *p = 0;
282 while (--p >= str)
284 *p = (num & 31) + '0';
285 if (*p > '9')
286 *p += ('a' - '0' - 10);
287 num >>= 5;
291 /*********************************************************************
292 * __p__iob(MSVCRT.@)
294 MSVCRT_FILE *__p__iob(void)
296 return &MSVCRT__iob[0];
299 /*********************************************************************
300 * _access (MSVCRT.@)
302 int _access(const char *filename, int mode)
304 DWORD attr = GetFileAttributesA(filename);
306 TRACE("(%s,%d) %ld\n",filename,mode,attr);
308 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
310 MSVCRT__set_errno(GetLastError());
311 return -1;
313 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
315 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
316 return -1;
318 return 0;
321 /*********************************************************************
322 * _waccess (MSVCRT.@)
324 int _waccess(const MSVCRT_wchar_t *filename, int mode)
326 DWORD attr = GetFileAttributesW(filename);
328 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
330 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
332 MSVCRT__set_errno(GetLastError());
333 return -1;
335 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
337 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
338 return -1;
340 return 0;
343 /*********************************************************************
344 * _chmod (MSVCRT.@)
346 int _chmod(const char *path, int flags)
348 DWORD oldFlags = GetFileAttributesA(path);
350 if (oldFlags != INVALID_FILE_ATTRIBUTES)
352 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
353 oldFlags | FILE_ATTRIBUTE_READONLY;
355 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
356 return 0;
358 MSVCRT__set_errno(GetLastError());
359 return -1;
362 /*********************************************************************
363 * _wchmod (MSVCRT.@)
365 int _wchmod(const MSVCRT_wchar_t *path, int flags)
367 DWORD oldFlags = GetFileAttributesW(path);
369 if (oldFlags != INVALID_FILE_ATTRIBUTES)
371 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
372 oldFlags | FILE_ATTRIBUTE_READONLY;
374 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
375 return 0;
377 MSVCRT__set_errno(GetLastError());
378 return -1;
381 /*********************************************************************
382 * _chsize (MSVCRT.@)
384 int _chsize(int fd, long size)
386 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
387 return -1;
390 /*********************************************************************
391 * _dup (MSVCRT.@)
393 int _dup(int od)
395 FIXME("(od=%d): stub\n", od);
396 return -1;
399 /*********************************************************************
400 * _dup2 (MSVCRT.@)
402 int _dup2(int od, int nd)
404 FIXME("(od=%d, nd=%d): stub\n", od, nd);
405 return -1;
408 /*********************************************************************
409 * _unlink (MSVCRT.@)
411 int _unlink(const char *path)
413 TRACE("(%s)\n",path);
414 if(DeleteFileA(path))
415 return 0;
416 TRACE("failed (%ld)\n",GetLastError());
417 MSVCRT__set_errno(GetLastError());
418 return -1;
421 /*********************************************************************
422 * _wunlink (MSVCRT.@)
424 int _wunlink(const MSVCRT_wchar_t *path)
426 TRACE("(%s)\n",debugstr_w(path));
427 if(DeleteFileW(path))
428 return 0;
429 TRACE("failed (%ld)\n",GetLastError());
430 MSVCRT__set_errno(GetLastError());
431 return -1;
434 /*********************************************************************
435 * _close (MSVCRT.@)
437 int _close(int fd)
439 HANDLE hand = msvcrt_fdtoh(fd);
441 TRACE(":fd (%d) handle (%p)\n",fd,hand);
442 if (hand == INVALID_HANDLE_VALUE)
443 return -1;
444 /* flush stdio buffers */
445 if(MSVCRT_files[fd]) {
446 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
447 MSVCRT_fflush(MSVCRT_files[fd]);
449 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
450 MSVCRT_free(MSVCRT_files[fd]->_base);
453 /* Don't free std FILE*'s, they are not dynamic */
454 if (fd > 2 && MSVCRT_files[fd])
455 MSVCRT_free(MSVCRT_files[fd]);
457 msvcrt_free_fd(fd);
459 if (!CloseHandle(hand))
461 WARN(":failed-last error (%ld)\n",GetLastError());
462 MSVCRT__set_errno(GetLastError());
463 return -1;
465 if (MSVCRT_tempfiles[fd])
467 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
468 _unlink(MSVCRT_tempfiles[fd]);
469 MSVCRT_free(MSVCRT_tempfiles[fd]);
470 MSVCRT_tempfiles[fd] = NULL;
473 TRACE(":ok\n");
474 return 0;
477 /*********************************************************************
478 * _commit (MSVCRT.@)
480 int _commit(int fd)
482 HANDLE hand = msvcrt_fdtoh(fd);
484 TRACE(":fd (%d) handle (%p)\n",fd,hand);
485 if (hand == INVALID_HANDLE_VALUE)
486 return -1;
488 if (!FlushFileBuffers(hand))
490 if (GetLastError() == ERROR_INVALID_HANDLE)
492 /* FlushFileBuffers fails for console handles
493 * so we ignore this error.
495 return 0;
497 TRACE(":failed-last error (%ld)\n",GetLastError());
498 MSVCRT__set_errno(GetLastError());
499 return -1;
501 TRACE(":ok\n");
502 return 0;
505 /*********************************************************************
506 * _eof (MSVCRT.@)
508 int _eof(int fd)
510 DWORD curpos,endpos;
511 HANDLE hand = msvcrt_fdtoh(fd);
513 TRACE(":fd (%d) handle (%p)\n",fd,hand);
515 if (hand == INVALID_HANDLE_VALUE)
516 return -1;
518 /* If we have a FILE* for this file, the EOF flag
519 * will be set by the read()/write() functions.
521 if (MSVCRT_files[fd])
522 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
524 /* Otherwise we do it the hard way */
525 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
526 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
528 if (curpos == endpos)
529 return TRUE;
531 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
532 return FALSE;
535 /*********************************************************************
536 * _fcloseall (MSVCRT.@)
538 int _fcloseall(void)
540 int num_closed = 0, i;
542 for (i = 3; i < MSVCRT_fdend; i++)
543 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
545 _close(i);
546 num_closed++;
549 TRACE(":closed (%d) handles\n",num_closed);
550 return num_closed;
553 /*********************************************************************
554 * _lseeki64 (MSVCRT.@)
556 __int64 _lseeki64(int fd, __int64 offset, int whence)
558 DWORD ret, hoffset = (DWORD) (offset >> 32);
559 HANDLE hand = msvcrt_fdtoh(fd);
561 TRACE(":fd (%d) handle (%p)\n",fd,hand);
562 if (hand == INVALID_HANDLE_VALUE)
563 return -1;
565 if (whence < 0 || whence > 2)
567 *MSVCRT__errno() = MSVCRT_EINVAL;
568 return -1;
571 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
572 fd,hoffset,(long)offset,
573 (whence==SEEK_SET)?"SEEK_SET":
574 (whence==SEEK_CUR)?"SEEK_CUR":
575 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
577 if (((ret = SetFilePointer(hand, (long)offset, &hoffset,
578 whence)) != INVALID_SET_FILE_POINTER) || !GetLastError())
580 if (MSVCRT_files[fd])
581 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
582 /* FIXME: What if we seek _to_ EOF - is EOF set? */
584 return ((__int64)hoffset << 32) | ret;
586 TRACE(":error-last error (%ld)\n",GetLastError());
587 if (MSVCRT_files[fd])
588 switch(GetLastError())
590 case ERROR_NEGATIVE_SEEK:
591 case ERROR_SEEK_ON_DEVICE:
592 MSVCRT__set_errno(GetLastError());
593 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
594 break;
595 default:
596 break;
598 return -1;
601 /*********************************************************************
602 * _lseek (MSVCRT.@)
604 LONG _lseek(int fd, LONG offset, int whence)
606 return _lseeki64(fd, offset, whence);
609 /*********************************************************************
610 * _locking (MSVCRT.@)
612 * This is untested; the underlying LockFile doesn't work yet.
614 int _locking(int fd, int mode, LONG nbytes)
616 BOOL ret;
617 DWORD cur_locn;
618 HANDLE hand = msvcrt_fdtoh(fd);
620 TRACE(":fd (%d) handle (%p)\n",fd,hand);
621 if (hand == INVALID_HANDLE_VALUE)
622 return -1;
624 if (mode < 0 || mode > 4)
626 *MSVCRT__errno() = MSVCRT_EINVAL;
627 return -1;
630 TRACE(":fd (%d) by 0x%08lx mode %s\n",
631 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
632 (mode==_LK_LOCK)?"_LK_LOCK":
633 (mode==_LK_NBLCK)?"_LK_NBLCK":
634 (mode==_LK_RLCK)?"_LK_RLCK":
635 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
636 "UNKNOWN");
638 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
640 FIXME ("Seek failed\n");
641 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
642 return -1;
644 if (mode == _LK_LOCK || mode == _LK_RLCK)
646 int nretry = 10;
647 ret = 1; /* just to satisfy gcc */
648 while (nretry--)
650 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
651 if (ret) break;
652 sleep (1);
655 else if (mode == _LK_UNLCK)
656 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
657 else
658 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
659 /* FIXME - what about error settings? */
660 return ret ? 0 : -1;
663 /*********************************************************************
664 * rewind (MSVCRT.@)
666 void MSVCRT_rewind(MSVCRT_FILE* file)
668 TRACE(":file (%p) fd (%d)\n",file,file->_file);
669 MSVCRT_fseek(file, 0L, SEEK_SET);
670 MSVCRT_clearerr(file);
673 /*********************************************************************
674 * _fdopen (MSVCRT.@)
676 MSVCRT_FILE* _fdopen(int fd, const char *mode)
678 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
680 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
682 return file;
685 /*********************************************************************
686 * _wfdopen (MSVCRT.@)
688 MSVCRT_FILE* _wfdopen(int fd, const MSVCRT_wchar_t *mode)
690 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
692 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
693 if (file)
694 MSVCRT_rewind(file);
696 return file;
699 /*********************************************************************
700 * _filelength (MSVCRT.@)
702 LONG _filelength(int fd)
704 LONG curPos = _lseek(fd, 0, SEEK_CUR);
705 if (curPos != -1)
707 LONG endPos = _lseek(fd, 0, SEEK_END);
708 if (endPos != -1)
710 if (endPos != curPos)
711 _lseek(fd, curPos, SEEK_SET);
712 return endPos;
715 return -1;
718 /*********************************************************************
719 * _filelengthi64 (MSVCRT.@)
721 __int64 _filelengthi64(int fd)
723 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
724 if (curPos != -1)
726 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
727 if (endPos != -1)
729 if (endPos != curPos)
730 _lseeki64(fd, curPos, SEEK_SET);
731 return endPos;
734 return -1;
737 /*********************************************************************
738 * _fileno (MSVCRT.@)
740 int _fileno(MSVCRT_FILE* file)
742 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
743 return file->_file;
746 /*********************************************************************
747 * _flushall (MSVCRT.@)
749 int _flushall(void)
751 int num_flushed = 0, i = 3;
753 while(i < MSVCRT_fdend)
754 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
756 #if 0
757 /* FIXME: flush, do not commit */
758 if (_commit(i) == -1)
759 if (MSVCRT_files[i])
760 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
761 #endif
762 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
763 MSVCRT_fflush(MSVCRT_files[i]);
764 num_flushed++;
768 TRACE(":flushed (%d) handles\n",num_flushed);
769 return num_flushed;
772 /*********************************************************************
773 * _fstati64 (MSVCRT.@)
775 int _fstati64(int fd, struct _stati64* buf)
777 DWORD dw;
778 BY_HANDLE_FILE_INFORMATION hfi;
779 HANDLE hand = msvcrt_fdtoh(fd);
781 TRACE(":fd (%d) stat (%p)\n",fd,buf);
782 if (hand == INVALID_HANDLE_VALUE)
783 return -1;
785 if (!buf)
787 WARN(":failed-NULL buf\n");
788 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
789 return -1;
792 memset(&hfi, 0, sizeof(hfi));
793 memset(buf, 0, sizeof(struct _stati64));
794 if (!GetFileInformationByHandle(hand, &hfi))
796 WARN(":failed-last error (%ld)\n",GetLastError());
797 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
798 return -1;
800 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
801 buf->st_nlink = hfi.nNumberOfLinks;
802 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
803 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
804 buf->st_atime = dw;
805 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
806 buf->st_mtime = buf->st_ctime = dw;
807 return 0;
810 /*********************************************************************
811 * _fstat (MSVCRT.@)
813 int MSVCRT__fstat(int fd, struct _stat* buf)
814 { int ret;
815 struct _stati64 bufi64;
817 ret = _fstati64(fd, &bufi64);
818 if (!ret)
819 msvcrt_cp_from_stati64(&bufi64, buf);
820 return ret;
823 /*********************************************************************
824 * _futime (MSVCRT.@)
826 int _futime(int fd, struct _utimbuf *t)
828 HANDLE hand = msvcrt_fdtoh(fd);
829 FILETIME at, wt;
831 if (!t)
833 MSVCRT_time_t currTime;
834 MSVCRT_time(&currTime);
835 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
836 memcpy(&wt, &at, sizeof(wt));
838 else
840 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
841 if (t->actime == t->modtime)
842 memcpy(&wt, &at, sizeof(wt));
843 else
844 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
847 if (!SetFileTime(hand, NULL, &at, &wt))
849 MSVCRT__set_errno(GetLastError());
850 return -1 ;
852 return 0;
855 /*********************************************************************
856 * _get_osfhandle (MSVCRT.@)
858 long _get_osfhandle(int fd)
860 HANDLE hand = msvcrt_fdtoh(fd);
861 HANDLE newhand = hand;
862 TRACE(":fd (%d) handle (%p)\n",fd,hand);
864 if (hand != INVALID_HANDLE_VALUE)
866 /* FIXME: I'm not convinced that I should be copying the
867 * handle here - it may be leaked if the app doesn't
868 * close it (and the API docs don't say that it should)
869 * Not duplicating it means that it can't be inherited
870 * and so lcc's wedit doesn't cope when it passes it to
871 * child processes. I've an idea that it should either
872 * be copied by CreateProcess, or marked as inheritable
873 * when initialised, or maybe both? JG 21-9-00.
875 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
876 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
878 return (long)newhand;
881 /*********************************************************************
882 * _isatty (MSVCRT.@)
884 int _isatty(int fd)
886 HANDLE hand = msvcrt_fdtoh(fd);
888 TRACE(":fd (%d) handle (%p)\n",fd,hand);
889 if (hand == INVALID_HANDLE_VALUE)
890 return 0;
892 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
895 /*********************************************************************
896 * _mktemp (MSVCRT.@)
898 char *_mktemp(char *pattern)
900 int numX = 0;
901 char *retVal = pattern;
902 int id;
903 char letter = 'a';
905 while(*pattern)
906 numX = (*pattern++ == 'X')? numX + 1 : 0;
907 if (numX < 5)
908 return NULL;
909 pattern--;
910 id = GetCurrentProcessId();
911 numX = 6;
912 while(numX--)
914 int tempNum = id / 10;
915 *pattern-- = id - (tempNum * 10) + '0';
916 id = tempNum;
918 pattern++;
921 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
922 GetLastError() == ERROR_FILE_NOT_FOUND)
923 return retVal;
924 *pattern = letter++;
925 } while(letter != '|');
926 return NULL;
929 /*********************************************************************
930 * _wmktemp (MSVCRT.@)
932 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
934 int numX = 0;
935 MSVCRT_wchar_t *retVal = pattern;
936 int id;
937 MSVCRT_wchar_t letter = 'a';
939 while(*pattern)
940 numX = (*pattern++ == 'X')? numX + 1 : 0;
941 if (numX < 5)
942 return NULL;
943 pattern--;
944 id = GetCurrentProcessId();
945 numX = 6;
946 while(numX--)
948 int tempNum = id / 10;
949 *pattern-- = id - (tempNum * 10) + '0';
950 id = tempNum;
952 pattern++;
955 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
956 GetLastError() == ERROR_FILE_NOT_FOUND)
957 return retVal;
958 *pattern = letter++;
959 } while(letter != '|');
960 return NULL;
963 /*********************************************************************
964 * _sopen (MSVCRT.@)
966 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
968 va_list ap;
969 int pmode;
970 DWORD access = 0, creation = 0;
971 DWORD sharing;
972 int ioflag = 0, fd;
973 HANDLE hand;
974 SECURITY_ATTRIBUTES sa;
977 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
978 path, oflags, shflags);
980 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
982 case _O_RDONLY:
983 access |= GENERIC_READ;
984 ioflag |= MSVCRT__IOREAD;
985 break;
986 case _O_WRONLY:
987 access |= GENERIC_WRITE;
988 ioflag |= MSVCRT__IOWRT;
989 break;
990 case _O_RDWR:
991 access |= GENERIC_WRITE | GENERIC_READ;
992 ioflag |= MSVCRT__IORW;
993 break;
996 if (oflags & _O_CREAT)
998 va_start(ap, shflags);
999 pmode = va_arg(ap, int);
1000 va_end(ap);
1002 if(pmode & ~(_S_IREAD | _S_IWRITE))
1003 FIXME(": pmode 0x%04x ignored\n", pmode);
1004 else
1005 WARN(": pmode 0x%04x ignored\n", pmode);
1007 if (oflags & _O_EXCL)
1008 creation = CREATE_NEW;
1009 else if (oflags & _O_TRUNC)
1010 creation = CREATE_ALWAYS;
1011 else
1012 creation = OPEN_ALWAYS;
1014 else /* no _O_CREAT */
1016 if (oflags & _O_TRUNC)
1017 creation = TRUNCATE_EXISTING;
1018 else
1019 creation = OPEN_EXISTING;
1021 if (oflags & _O_APPEND)
1022 ioflag |= MSVCRT__IOAPPEND;
1024 if (oflags & _O_BINARY)
1025 ioflag |= _O_BINARY;
1026 else if (oflags & _O_TEXT)
1027 ioflag |= _O_TEXT;
1028 else if (*__p__fmode() & _O_BINARY)
1029 ioflag |= _O_BINARY;
1030 else
1031 ioflag |= _O_TEXT; /* default to TEXT*/
1033 switch( shflags )
1035 case _SH_DENYRW:
1036 sharing = 0L;
1037 break;
1038 case _SH_DENYWR:
1039 sharing = FILE_SHARE_READ;
1040 break;
1041 case _SH_DENYRD:
1042 sharing = FILE_SHARE_WRITE;
1043 break;
1044 case _SH_DENYNO:
1045 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1046 break;
1047 default:
1048 ERR( "Unhandled shflags 0x%x\n", shflags );
1049 return -1;
1052 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
1053 |_O_CREAT|_O_RDWR|_O_WRONLY|_O_TEMPORARY|_O_NOINHERIT))
1054 ERR(":unsupported oflags 0x%04x\n",oflags);
1056 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1057 sa.lpSecurityDescriptor = NULL;
1058 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
1060 hand = CreateFileA(path, access, sharing,
1061 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
1063 if (hand == INVALID_HANDLE_VALUE) {
1064 WARN(":failed-last error (%ld)\n",GetLastError());
1065 MSVCRT__set_errno(GetLastError());
1066 return -1;
1069 fd = msvcrt_alloc_fd(hand, ioflag);
1071 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1073 if (fd > 0)
1075 if (oflags & _O_TEMPORARY)
1076 MSVCRT_tempfiles[fd] = _strdup(path);
1077 if (ioflag & MSVCRT__IOAPPEND)
1078 _lseek(fd, 0, FILE_END);
1081 return fd;
1084 /*********************************************************************
1085 * _wsopen (MSVCRT.@)
1087 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1089 const unsigned int len = strlenW(path);
1090 char *patha = MSVCRT_calloc(len + 1,1);
1091 va_list ap;
1092 int pmode;
1094 va_start(ap, shflags);
1095 pmode = va_arg(ap, int);
1096 va_end(ap);
1098 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1100 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1101 MSVCRT_free(patha);
1102 return retval;
1105 MSVCRT__set_errno(GetLastError());
1106 return -1;
1109 /*********************************************************************
1110 * _open (MSVCRT.@)
1112 int _open( const char *path, int flags, ... )
1114 va_list ap;
1116 if (flags & _O_CREAT)
1118 int pmode;
1119 va_start(ap, flags);
1120 pmode = va_arg(ap, int);
1121 va_end(ap);
1122 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1124 else
1125 return MSVCRT__sopen( path, flags, _SH_DENYNO);
1128 /*********************************************************************
1129 * _wopen (MSVCRT.@)
1131 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1133 const unsigned int len = strlenW(path);
1134 char *patha = MSVCRT_calloc(len + 1,1);
1135 va_list ap;
1136 int pmode;
1138 va_start(ap, flags);
1139 pmode = va_arg(ap, int);
1140 va_end(ap);
1142 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1144 int retval = _open(patha,flags,pmode);
1145 MSVCRT_free(patha);
1146 return retval;
1149 MSVCRT__set_errno(GetLastError());
1150 return -1;
1153 /*********************************************************************
1154 * _creat (MSVCRT.@)
1156 int _creat(const char *path, int flags)
1158 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1159 return _open(path, usedFlags);
1162 /*********************************************************************
1163 * _wcreat (MSVCRT.@)
1165 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1167 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1168 return _wopen(path, usedFlags);
1171 /*********************************************************************
1172 * _open_osfhandle (MSVCRT.@)
1174 int _open_osfhandle(long hand, int flags)
1176 int fd;
1178 /* _O_RDONLY (0) always matches, so set the read flag
1179 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1180 * file, so set the write flag. It also only sets _O_TEXT if it wants
1181 * text - it never sets _O_BINARY.
1183 /* FIXME: handle more flags */
1184 flags |= MSVCRT__IOREAD|MSVCRT__IOWRT;
1185 if ( !( flags & _O_TEXT ) ) flags |= _O_BINARY;
1187 fd = msvcrt_alloc_fd((HANDLE)hand,flags);
1188 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd, flags);
1189 return fd;
1192 /*********************************************************************
1193 * _rmtmp (MSVCRT.@)
1195 int _rmtmp(void)
1197 int num_removed = 0, i;
1199 for (i = 3; i < MSVCRT_fdend; i++)
1200 if (MSVCRT_tempfiles[i])
1202 _close(i);
1203 num_removed++;
1206 if (num_removed)
1207 TRACE(":removed (%d) temp files\n",num_removed);
1208 return num_removed;
1211 /*********************************************************************
1212 * (internal) remove_cr
1214 * Remove all \r inplace.
1215 * return the number of \r removed
1217 static unsigned int remove_cr(char *buf, unsigned int count)
1219 unsigned int i, j;
1221 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1222 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1223 return count - i;
1226 /*********************************************************************
1227 * _read (MSVCRT.@)
1229 int _read(int fd, void *buf, unsigned int count)
1231 DWORD num_read, all_read =0;
1232 char *bufstart = buf;
1233 HANDLE hand = msvcrt_fdtoh(fd);
1235 /* Don't trace small reads, it gets *very* annoying */
1236 if (count > 4)
1237 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1238 if (hand == INVALID_HANDLE_VALUE)
1239 return -1;
1241 /* Reading single bytes in O_TEXT mode makes things slow
1242 * So read big chunks, then remove the \r in memory and try reading
1243 * the rest until the request is satisfied or EOF is met
1245 while ( all_read < count)
1247 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1249 if (num_read != (count- all_read))
1251 TRACE(":EOF\n");
1252 if ( MSVCRT_files[fd])
1254 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1256 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1259 if ((MSVCRT_flags[fd]& _O_BINARY ) != _O_BINARY )
1260 num_read -= remove_cr(bufstart+all_read,num_read);
1261 all_read += num_read;
1262 if (count > 4)
1263 TRACE("%s\n",debugstr_an(buf,all_read));
1264 return all_read;
1266 if ((MSVCRT_flags[fd]& _O_BINARY ) != _O_BINARY )
1268 num_read -= remove_cr(bufstart+all_read,num_read);
1270 all_read += num_read;
1272 else
1274 TRACE(":failed-last error (%ld)\n",GetLastError());
1275 if (MSVCRT_files[fd])
1276 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1277 return -1;
1281 if (count > 4)
1282 TRACE("%s\n",debugstr_an(buf, all_read));
1283 return all_read;
1286 /*********************************************************************
1287 * _getw (MSVCRT.@)
1289 int _getw(MSVCRT_FILE* file)
1291 int i;
1292 if (_read(file->_file, &i, sizeof(int)) != 1)
1293 return MSVCRT_EOF;
1294 return i;
1297 /*********************************************************************
1298 * _setmode (MSVCRT.@)
1300 int _setmode(int fd,int mode)
1302 int ret = MSVCRT_flags[fd] & (_O_TEXT | _O_BINARY);
1303 if (mode & (~(_O_TEXT|_O_BINARY)))
1304 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1305 MSVCRT_flags[fd] &= ~(_O_TEXT|_O_BINARY);
1306 MSVCRT_flags[fd] |= mode & (_O_TEXT | _O_BINARY);
1307 return ret;
1310 /*********************************************************************
1311 * _stati64 (MSVCRT.@)
1313 int _stati64(const char* path, struct _stati64 * buf)
1315 DWORD dw;
1316 WIN32_FILE_ATTRIBUTE_DATA hfi;
1317 unsigned short mode = MSVCRT_S_IREAD;
1318 int plen;
1320 TRACE(":file (%s) buf(%p)\n",path,buf);
1322 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1324 TRACE("failed (%ld)\n",GetLastError());
1325 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1326 return -1;
1329 memset(buf,0,sizeof(struct _stati64));
1331 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1332 Bon 011120: This FIXME seems incorrect
1333 Also a letter as first char isn't enough to be classified
1334 as a drive letter
1336 if (isalpha(*path)&& (*(path+1)==':'))
1337 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1338 else
1339 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1341 plen = strlen(path);
1343 /* Dir, or regular file? */
1344 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1345 (path[plen-1] == '\\'))
1346 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1347 else
1349 mode |= _S_IFREG;
1350 /* executable? */
1351 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1353 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1354 (tolower(path[plen-3]) << 16);
1355 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1356 mode |= MSVCRT_S_IEXEC;
1360 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1361 mode |= MSVCRT_S_IWRITE;
1363 buf->st_mode = mode;
1364 buf->st_nlink = 1;
1365 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1366 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1367 buf->st_atime = dw;
1368 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1369 buf->st_mtime = buf->st_ctime = dw;
1370 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1371 (long)(buf->st_size >> 32),(long)buf->st_size,
1372 buf->st_atime,buf->st_mtime, buf->st_ctime);
1373 return 0;
1376 /*********************************************************************
1377 * _stat (MSVCRT.@)
1379 int MSVCRT__stat(const char* path, struct _stat * buf)
1380 { int ret;
1381 struct _stati64 bufi64;
1383 ret = _stati64( path, &bufi64);
1384 if (!ret)
1385 msvcrt_cp_from_stati64(&bufi64, buf);
1386 return ret;
1389 /*********************************************************************
1390 * _wstati64 (MSVCRT.@)
1392 int _wstati64(const MSVCRT_wchar_t* path, struct _stati64 * buf)
1394 DWORD dw;
1395 WIN32_FILE_ATTRIBUTE_DATA hfi;
1396 unsigned short mode = MSVCRT_S_IREAD;
1397 int plen;
1399 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1401 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1403 TRACE("failed (%ld)\n",GetLastError());
1404 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1405 return -1;
1408 memset(buf,0,sizeof(struct _stat));
1410 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1411 if (MSVCRT_iswalpha(*path))
1412 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1413 else
1414 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1416 plen = strlenW(path);
1418 /* Dir, or regular file? */
1419 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1420 (path[plen-1] == '\\'))
1421 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1422 else
1424 mode |= _S_IFREG;
1425 /* executable? */
1426 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1428 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1429 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1430 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1431 mode |= MSVCRT_S_IEXEC;
1435 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1436 mode |= MSVCRT_S_IWRITE;
1438 buf->st_mode = mode;
1439 buf->st_nlink = 1;
1440 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1441 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1442 buf->st_atime = dw;
1443 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1444 buf->st_mtime = buf->st_ctime = dw;
1445 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1446 (long)(buf->st_size >> 32),(long)buf->st_size,
1447 buf->st_atime,buf->st_mtime, buf->st_ctime);
1448 return 0;
1451 /*********************************************************************
1452 * _wstat (MSVCRT.@)
1454 int _wstat(const MSVCRT_wchar_t* path, struct _stat * buf)
1456 int ret;
1457 struct _stati64 bufi64;
1459 ret = _wstati64( path, &bufi64 );
1460 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1461 return ret;
1464 /*********************************************************************
1465 * _tell (MSVCRT.@)
1467 long _tell(int fd)
1469 return _lseek(fd, 0, SEEK_CUR);
1472 /*********************************************************************
1473 * _telli64 (MSVCRT.@)
1475 __int64 _telli64(int fd)
1477 return _lseeki64(fd, 0, SEEK_CUR);
1480 /*********************************************************************
1481 * _tempnam (MSVCRT.@)
1483 char *_tempnam(const char *dir, const char *prefix)
1485 char tmpbuf[MAX_PATH];
1487 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1488 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1490 TRACE("got name (%s)\n",tmpbuf);
1491 DeleteFileA(tmpbuf);
1492 return _strdup(tmpbuf);
1494 TRACE("failed (%ld)\n",GetLastError());
1495 return NULL;
1498 /*********************************************************************
1499 * _wtempnam (MSVCRT.@)
1501 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1503 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1505 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1506 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1508 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1509 DeleteFileW(tmpbuf);
1510 return _wcsdup(tmpbuf);
1512 TRACE("failed (%ld)\n",GetLastError());
1513 return NULL;
1516 /*********************************************************************
1517 * _umask (MSVCRT.@)
1519 int _umask(int umask)
1521 int old_umask = MSVCRT_umask;
1522 TRACE("(%d)\n",umask);
1523 MSVCRT_umask = umask;
1524 return old_umask;
1527 /*********************************************************************
1528 * _utime (MSVCRT.@)
1530 int _utime(const char* path, struct _utimbuf *t)
1532 int fd = _open(path, _O_WRONLY | _O_BINARY);
1534 if (fd > 0)
1536 int retVal = _futime(fd, t);
1537 _close(fd);
1538 return retVal;
1540 return -1;
1543 /*********************************************************************
1544 * _wutime (MSVCRT.@)
1546 int _wutime(const MSVCRT_wchar_t* path, struct _utimbuf *t)
1548 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1550 if (fd > 0)
1552 int retVal = _futime(fd, t);
1553 _close(fd);
1554 return retVal;
1556 return -1;
1559 /*********************************************************************
1560 * _write (MSVCRT.@)
1562 int _write(int fd, const void* buf, unsigned int count)
1564 DWORD num_written;
1565 HANDLE hand = msvcrt_fdtoh(fd);
1567 /* Don't trace small writes, it gets *very* annoying */
1568 #if 0
1569 if (count > 32)
1570 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1571 #endif
1572 if (hand == INVALID_HANDLE_VALUE)
1574 *MSVCRT__errno() = MSVCRT_EBADF;
1575 return -1;
1578 /* If appending, go to EOF */
1579 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1580 _lseek(fd, 0, FILE_END);
1582 if (MSVCRT_flags[fd] & _O_BINARY)
1584 if (WriteFile(hand, buf, count, &num_written, NULL)
1585 && (num_written == count))
1586 return num_written;
1587 TRACE(":failed-last error (%ld)\n",GetLastError());
1588 if (MSVCRT_files[fd])
1590 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1591 *MSVCRT__errno() = MSVCRT_ENOSPC;
1594 else
1596 unsigned int i, j, nr_lf;
1597 char *s=(char*)buf, *buf_start=(char*)buf, *p;
1598 /* find number of \n ( without preceeding \r */
1599 for ( nr_lf=0,i = 0; i <count; i++)
1601 if (s[i]== '\n')
1603 nr_lf++;
1604 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1607 if (nr_lf)
1609 if ((p = MSVCRT_malloc(count + nr_lf)))
1611 for(s=(char*)buf, i=0, j=0; i<count; i++)
1613 if (s[i]== '\n')
1615 p[j++] = '\r';
1616 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1618 p[j++] = s[i];
1621 else
1623 FIXME("Malloc failed\n");
1624 nr_lf =0;
1625 p = (char*)buf;
1628 else
1629 p = (char*)buf;
1631 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
1633 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
1634 if (MSVCRT_files[fd])
1636 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1637 *MSVCRT__errno() = MSVCRT_ENOSPC;
1638 if(nr_lf)
1639 MSVCRT_free(p);
1640 return s - buf_start;
1643 else
1645 if(nr_lf)
1646 MSVCRT_free(p);
1647 return count;
1650 return -1;
1653 /*********************************************************************
1654 * _putw (MSVCRT.@)
1656 int _putw(int val, MSVCRT_FILE* file)
1658 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1661 /*********************************************************************
1662 * clearerr (MSVCRT.@)
1664 void MSVCRT_clearerr(MSVCRT_FILE* file)
1666 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1667 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1670 /*********************************************************************
1671 * fclose (MSVCRT.@)
1673 int MSVCRT_fclose(MSVCRT_FILE* file)
1675 int r, flag;
1677 flag = file->_flag;
1678 r=_close(file->_file);
1679 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1682 /*********************************************************************
1683 * feof (MSVCRT.@)
1685 int MSVCRT_feof(MSVCRT_FILE* file)
1687 return file->_flag & MSVCRT__IOEOF;
1690 /*********************************************************************
1691 * ferror (MSVCRT.@)
1693 int MSVCRT_ferror(MSVCRT_FILE* file)
1695 return file->_flag & MSVCRT__IOERR;
1698 /*********************************************************************
1699 * fflush (MSVCRT.@)
1701 int MSVCRT_fflush(MSVCRT_FILE* file)
1703 if(!file) {
1704 _flushall();
1705 return 0;
1706 } else {
1707 int res=msvcrt_flush_buffer(file);
1708 return res;
1712 /*********************************************************************
1713 * fgetc (MSVCRT.@)
1715 int MSVCRT_fgetc(MSVCRT_FILE* file)
1717 if (file->_cnt>0) {
1718 file->_cnt--;
1719 return *(unsigned char *)file->_ptr++;
1720 } else {
1721 return _filbuf(file);
1725 /*********************************************************************
1726 * _fgetchar (MSVCRT.@)
1728 int _fgetchar(void)
1730 return MSVCRT_fgetc(MSVCRT_stdin);
1733 /*********************************************************************
1734 * _filbuf (MSVCRT.@)
1736 int _filbuf(MSVCRT_FILE* file)
1739 /* Allocate buffer if needed */
1740 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1741 msvcrt_alloc_buffer(file);
1743 if(!(file->_flag & MSVCRT__IOREAD)) {
1744 if(file->_flag & MSVCRT__IORW) {
1745 file->_flag |= MSVCRT__IOREAD;
1746 } else {
1747 return MSVCRT_EOF;
1750 if(file->_flag & MSVCRT__IONBF) {
1751 unsigned char c;
1752 if (_read(file->_file,&c,1) != 1) {
1753 file->_flag |= MSVCRT__IOEOF;
1754 return MSVCRT_EOF;
1756 return c;
1757 } else {
1758 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1759 if(file->_cnt<0) file->_cnt = 0;
1760 if(!file->_cnt) {
1761 file->_flag |= MSVCRT__IOEOF;
1762 return MSVCRT_EOF;
1764 file->_cnt--;
1765 file->_ptr = file->_base+1;
1766 return *(unsigned char *)file->_base;
1770 /*********************************************************************
1771 * fgetpos (MSVCRT.@)
1773 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1775 *pos = MSVCRT_ftell(file);
1776 return (*pos == -1? -1 : 0);
1779 /*********************************************************************
1780 * fgets (MSVCRT.@)
1782 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1784 int cc = MSVCRT_EOF;
1785 char * buf_start = s;
1787 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1788 file,file->_file,s,size);
1790 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
1792 *s++ = (char)cc;
1793 size --;
1795 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1797 TRACE(":nothing read\n");
1798 return NULL;
1800 if ((cc != MSVCRT_EOF) && (size > 1))
1801 *s++ = cc;
1802 *s = '\0';
1803 TRACE(":got '%s'\n", debugstr_a(buf_start));
1804 return buf_start;
1807 /*********************************************************************
1808 * fgetwc (MSVCRT.@)
1810 * In _O_TEXT mode, bultibyte characters are read from the file, dropping
1811 * the CR from CR/LF combinations
1813 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1815 char c;
1817 if (file->_flag & _O_BINARY)
1819 MSVCRT_wchar_t wc;
1820 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1821 return MSVCRT_WEOF;
1822 return wc;
1824 c = MSVCRT_fgetc(file);
1825 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
1827 FIXME("Treat Multibyte characters\n");
1829 if (c == MSVCRT_EOF)
1830 return MSVCRT_WEOF;
1831 else
1832 return (MSVCRT_wint_t)c;
1835 /*********************************************************************
1836 * getwc (MSVCRT.@)
1838 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1840 return MSVCRT_fgetwc(file);
1843 /*********************************************************************
1844 * _fgetwchar (MSVCRT.@)
1846 MSVCRT_wint_t _fgetwchar(void)
1848 return MSVCRT_fgetwc(MSVCRT_stdin);
1851 /*********************************************************************
1852 * getwchar (MSVCRT.@)
1854 MSVCRT_wint_t MSVCRT_getwchar(void)
1856 return _fgetwchar();
1859 /*********************************************************************
1860 * fgetws (MSVCRT.@)
1862 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
1864 int cc = MSVCRT_WEOF;
1865 MSVCRT_wchar_t * buf_start = s;
1867 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1868 file,file->_file,s,size);
1870 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
1872 *s++ = (char)cc;
1873 size --;
1875 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
1877 TRACE(":nothing read\n");
1878 return NULL;
1880 if ((cc != MSVCRT_WEOF) && (size > 1))
1881 *s++ = cc;
1882 *s = 0;
1883 TRACE(":got %s\n", debugstr_w(buf_start));
1884 return buf_start;
1888 /*********************************************************************
1889 * fputwc (MSVCRT.@)
1891 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1893 MSVCRT_wchar_t mwc=wc;
1894 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
1895 return MSVCRT_WEOF;
1896 return wc;
1899 /*********************************************************************
1900 * _fputwchar (MSVCRT.@)
1902 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1904 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1907 /*********************************************************************
1908 * fopen (MSVCRT.@)
1910 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1912 MSVCRT_FILE* file;
1913 int flags = 0, plus = 0, fd;
1914 const char* search = mode;
1916 TRACE("(%s,%s)\n",path,mode);
1918 while (*search)
1919 if (*search++ == '+')
1920 plus = 1;
1922 /* map mode string to open() flags. "man fopen" for possibilities. */
1923 switch(*mode++)
1925 case 'R': case 'r':
1926 flags = (plus ? _O_RDWR : _O_RDONLY);
1927 break;
1928 case 'W': case 'w':
1929 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1930 break;
1931 case 'A': case 'a':
1932 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1933 break;
1934 default:
1935 return NULL;
1938 while (*mode)
1939 switch (*mode++)
1941 case 'B': case 'b':
1942 flags |= _O_BINARY;
1943 flags &= ~_O_TEXT;
1944 break;
1945 case 'T': case 't':
1946 flags |= _O_TEXT;
1947 flags &= ~_O_BINARY;
1948 break;
1949 case '+':
1950 break;
1951 default:
1952 FIXME(":unknown flag %c not supported\n",mode[-1]);
1955 fd = _open(path, flags, _S_IREAD | _S_IWRITE);
1957 if (fd < 0)
1958 return NULL;
1960 file = msvcrt_alloc_fp(fd);
1961 TRACE(":got (%p)\n",file);
1962 if (!file)
1963 _close(fd);
1965 return file;
1968 /*********************************************************************
1969 * _wfopen (MSVCRT.@)
1971 MSVCRT_FILE *_wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
1973 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1974 char *patha = MSVCRT_calloc(plen + 1, 1);
1975 char *modea = MSVCRT_calloc(mlen + 1, 1);
1977 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1979 if (patha && modea &&
1980 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1981 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1983 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1984 MSVCRT_free(patha);
1985 MSVCRT_free(modea);
1986 return retval;
1989 MSVCRT__set_errno(GetLastError());
1990 return NULL;
1993 /*********************************************************************
1994 * _fsopen (MSVCRT.@)
1996 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1998 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1999 return MSVCRT_fopen(path,mode);
2002 /*********************************************************************
2003 * _wfsopen (MSVCRT.@)
2005 MSVCRT_FILE* _wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2007 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2008 debugstr_w(path),debugstr_w(mode),share);
2009 return _wfopen(path,mode);
2012 /*********************************************************************
2013 * fputc (MSVCRT.@)
2015 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2017 if(file->_cnt>0) {
2018 *file->_ptr++=c;
2019 file->_cnt--;
2020 return c;
2021 } else {
2022 return _flsbuf(c, file);
2026 /*********************************************************************
2027 * _flsbuf (MSVCRT.@)
2029 int _flsbuf(int c, MSVCRT_FILE* file)
2031 /* Flush output buffer */
2032 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2033 msvcrt_alloc_buffer(file);
2035 if(!(file->_flag & MSVCRT__IOWRT)) {
2036 if(file->_flag & MSVCRT__IORW) {
2037 file->_flag |= MSVCRT__IOWRT;
2038 } else {
2039 return MSVCRT_EOF;
2042 if(file->_bufsiz) {
2043 int res=msvcrt_flush_buffer(file);
2044 return res?res : MSVCRT_fputc(c, file);
2045 } else {
2046 unsigned char cc=c;
2047 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
2051 /*********************************************************************
2052 * _fputchar (MSVCRT.@)
2054 int _fputchar(int c)
2056 return MSVCRT_fputc(c, MSVCRT_stdout);
2059 /*********************************************************************
2060 * fread (MSVCRT.@)
2062 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2063 { MSVCRT_size_t rcnt=size * nmemb;
2064 MSVCRT_size_t read=0;
2065 int pread=0;
2066 /* first buffered data */
2067 if(file->_cnt>0) {
2068 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2069 memcpy(ptr, file->_ptr, pcnt);
2070 file->_cnt -= pcnt;
2071 file->_ptr += pcnt;
2072 read += pcnt ;
2073 rcnt -= pcnt ;
2074 ptr = (char*)ptr + pcnt;
2075 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2076 if(file->_flag & MSVCRT__IORW) {
2077 file->_flag |= MSVCRT__IOREAD;
2078 } else
2079 return 0;
2081 if(rcnt) pread = _read(file->_file,ptr, rcnt);
2082 if (MSVCRT_flags[file->_file] & MSVCRT__IOEOF)
2083 /* expose feof condition in the flags
2084 MFC tests file->_flag for feof, and doesn't not call feof())
2086 file->_flag |= MSVCRT__IOEOF;
2087 if (pread <= 0)
2088 pread = 0;
2089 read+=pread;
2090 return read / size;
2093 /*********************************************************************
2094 * freopen (MSVCRT.@)
2097 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2099 MSVCRT_FILE* newfile;
2100 int fd;
2102 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2103 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2104 return NULL;
2106 if (fd > 2)
2108 #if 0
2109 FIXME(":reopen on user file not implemented!\n");
2110 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
2111 return NULL;
2112 #endif
2113 if(MSVCRT_fclose(file))
2114 return NULL;
2115 return MSVCRT_fopen(path, mode);
2118 /* first, create the new file */
2119 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
2120 return NULL;
2122 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
2123 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
2124 MSVCRT_handles[newfile->_file]))
2126 /* Redirecting std handle to file , copy over.. */
2127 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
2128 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
2129 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
2130 MSVCRT__iob[fd]._file = fd;
2131 /* And free up the resources allocated by fopen, but
2132 * not the HANDLE we copied. */
2133 MSVCRT_free(MSVCRT_files[fd]);
2134 msvcrt_free_fd(newfile->_file);
2135 return &MSVCRT__iob[fd];
2138 WARN(":failed-last error (%ld)\n",GetLastError());
2139 MSVCRT_fclose(newfile);
2140 MSVCRT__set_errno(GetLastError());
2141 return NULL;
2144 /*********************************************************************
2145 * fsetpos (MSVCRT.@)
2147 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2149 return _lseek(file->_file,*pos,SEEK_SET);
2152 /*********************************************************************
2153 * fseek (MSVCRT.@)
2155 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
2157 /* Flush output if needed */
2158 if(file->_flag & MSVCRT__IOWRT)
2159 msvcrt_flush_buffer(file);
2161 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
2162 offset -= file->_cnt;
2164 /* Discard buffered input */
2165 file->_cnt = 0;
2166 file->_ptr = file->_base;
2167 /* Reset direction of i/o */
2168 if(file->_flag & MSVCRT__IORW) {
2169 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2171 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
2174 /*********************************************************************
2175 * ftell (MSVCRT.@)
2177 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2179 int off=0;
2180 long pos;
2181 if(file->_bufsiz) {
2182 if( file->_flag & MSVCRT__IOWRT ) {
2183 off = file->_ptr - file->_base;
2184 } else {
2185 off = -file->_cnt;
2188 pos = _tell(file->_file);
2189 if(pos == -1) return pos;
2190 return off + pos;
2193 /*********************************************************************
2194 * fwrite (MSVCRT.@)
2196 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2198 MSVCRT_size_t wrcnt=size * nmemb;
2199 int written = 0;
2200 if (size == 0)
2201 return 0;
2202 if(file->_cnt) {
2203 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2204 memcpy(file->_ptr, ptr, pcnt);
2205 file->_cnt -= pcnt;
2206 file->_ptr += pcnt;
2207 written = pcnt;
2208 wrcnt -= pcnt;
2209 ptr = (char*)ptr + pcnt;
2210 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2211 if(file->_flag & MSVCRT__IORW) {
2212 file->_flag |= MSVCRT__IOWRT;
2213 } else
2214 return 0;
2216 if(wrcnt) {
2217 /* Flush buffer */
2218 int res=msvcrt_flush_buffer(file);
2219 if(!res) {
2220 int pwritten = _write(file->_file, ptr, wrcnt);
2221 if (pwritten <= 0) pwritten=0;
2222 written += pwritten;
2225 return written / size;
2228 /*********************************************************************
2229 * fputs (MSVCRT.@)
2231 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2233 size_t i, len = strlen(s);
2234 if (file->_flag & _O_BINARY)
2235 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2236 for (i=0; i<len; i++)
2237 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2238 return MSVCRT_EOF;
2239 return 0;
2242 /*********************************************************************
2243 * fputws (MSVCRT.@)
2245 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2247 size_t i, len = strlenW(s);
2248 if (file->_flag & _O_BINARY)
2249 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2250 for (i=0; i<len; i++)
2252 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2253 return MSVCRT_WEOF;
2254 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2255 return MSVCRT_WEOF;
2257 return 0;
2260 /*********************************************************************
2261 * getchar (MSVCRT.@)
2263 int MSVCRT_getchar(void)
2265 return MSVCRT_fgetc(MSVCRT_stdin);
2268 /*********************************************************************
2269 * getc (MSVCRT.@)
2271 int MSVCRT_getc(MSVCRT_FILE* file)
2273 return MSVCRT_fgetc(file);
2276 /*********************************************************************
2277 * gets (MSVCRT.@)
2279 char *MSVCRT_gets(char *buf)
2281 int cc;
2282 char * buf_start = buf;
2284 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2285 cc = MSVCRT_fgetc(MSVCRT_stdin))
2286 if(cc != '\r') *buf++ = (char)cc;
2288 *buf = '\0';
2290 TRACE("got '%s'\n", buf_start);
2291 return buf_start;
2294 /*********************************************************************
2295 * _getws (MSVCRT.@)
2297 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2299 MSVCRT_wint_t cc;
2300 MSVCRT_wchar_t* ws = buf;
2302 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2303 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2305 if (cc != '\r')
2306 *buf++ = (MSVCRT_wchar_t)cc;
2308 *buf = '\0';
2310 TRACE("got '%s'\n", debugstr_w(ws));
2311 return ws;
2314 /*********************************************************************
2315 * putc (MSVCRT.@)
2317 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2319 return MSVCRT_fputc(c, file);
2322 /*********************************************************************
2323 * putchar (MSVCRT.@)
2325 int MSVCRT_putchar(int c)
2327 return MSVCRT_fputc(c, MSVCRT_stdout);
2330 /*********************************************************************
2331 * puts (MSVCRT.@)
2333 int MSVCRT_puts(const char *s)
2335 size_t len = strlen(s);
2336 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2337 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2340 /*********************************************************************
2341 * _putws (MSVCRT.@)
2343 int _putws(const MSVCRT_wchar_t *s)
2345 static const MSVCRT_wchar_t nl = '\n';
2346 size_t len = strlenW(s);
2347 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2348 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2351 /*********************************************************************
2352 * remove (MSVCRT.@)
2354 int MSVCRT_remove(const char *path)
2356 TRACE("(%s)\n",path);
2357 if (DeleteFileA(path))
2358 return 0;
2359 TRACE(":failed (%ld)\n",GetLastError());
2360 MSVCRT__set_errno(GetLastError());
2361 return -1;
2364 /*********************************************************************
2365 * _wremove (MSVCRT.@)
2367 int _wremove(const MSVCRT_wchar_t *path)
2369 TRACE("(%s)\n",debugstr_w(path));
2370 if (DeleteFileW(path))
2371 return 0;
2372 TRACE(":failed (%ld)\n",GetLastError());
2373 MSVCRT__set_errno(GetLastError());
2374 return -1;
2377 /*********************************************************************
2378 * scanf (MSVCRT.@)
2380 int MSVCRT_scanf(const char *format, ...)
2382 va_list valist;
2383 int res;
2385 va_start(valist, format);
2386 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
2387 va_end(valist);
2388 return res;
2391 /*********************************************************************
2392 * wscanf (MSVCRT.@)
2394 int MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...)
2396 va_list valist;
2397 int res;
2399 va_start(valist, format);
2400 res = MSVCRT_fwscanf(MSVCRT_stdin, format, valist);
2401 va_end(valist);
2402 return res;
2405 /*********************************************************************
2406 * rename (MSVCRT.@)
2408 int MSVCRT_rename(const char *oldpath,const char *newpath)
2410 TRACE(":from %s to %s\n",oldpath,newpath);
2411 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2412 return 0;
2413 TRACE(":failed (%ld)\n",GetLastError());
2414 MSVCRT__set_errno(GetLastError());
2415 return -1;
2418 /*********************************************************************
2419 * _wrename (MSVCRT.@)
2421 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2423 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2424 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2425 return 0;
2426 TRACE(":failed (%ld)\n",GetLastError());
2427 MSVCRT__set_errno(GetLastError());
2428 return -1;
2431 /*********************************************************************
2432 * setvbuf (MSVCRT.@)
2434 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2436 /* TODO: Check if file busy */
2437 if(file->_bufsiz) {
2438 MSVCRT_free(file->_base);
2439 file->_bufsiz = 0;
2440 file->_cnt = 0;
2442 if(mode == MSVCRT__IOFBF) {
2443 file->_flag &= ~MSVCRT__IONBF;
2444 file->_base = file->_ptr = buf;
2445 if(buf) {
2446 file->_bufsiz = size;
2448 } else {
2449 file->_flag |= MSVCRT__IONBF;
2451 return 0;
2454 /*********************************************************************
2455 * setbuf (MSVCRT.@)
2457 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2459 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2462 /*********************************************************************
2463 * tmpnam (MSVCRT.@)
2465 char *MSVCRT_tmpnam(char *s)
2467 static int unique;
2468 char tmpstr[16];
2469 char *p;
2470 int count;
2471 if (s == 0)
2472 s = MSVCRT_tmpname;
2473 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2474 p = s + sprintf(s, "\\s%s.", tmpstr);
2475 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2477 msvcrt_int_to_base32(unique++, tmpstr);
2478 strcpy(p, tmpstr);
2479 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2480 GetLastError() == ERROR_FILE_NOT_FOUND)
2481 break;
2483 return s;
2486 /*********************************************************************
2487 * tmpfile (MSVCRT.@)
2489 MSVCRT_FILE* MSVCRT_tmpfile(void)
2491 char *filename = MSVCRT_tmpnam(NULL);
2492 int fd;
2493 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2494 if (fd != -1)
2495 return msvcrt_alloc_fp(fd);
2496 return NULL;
2499 /*********************************************************************
2500 * vfprintf (MSVCRT.@)
2502 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2504 char buf[2048], *mem = buf;
2505 int written, resize = sizeof(buf), retval;
2506 /* There are two conventions for vsnprintf failing:
2507 * Return -1 if we truncated, or
2508 * Return the number of bytes that would have been written
2509 * The code below handles both cases
2511 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2512 written > resize)
2514 resize = (written == -1 ? resize * 2 : written + 1);
2515 if (mem != buf)
2516 MSVCRT_free (mem);
2517 if (!(mem = (char *)MSVCRT_malloc(resize)))
2518 return MSVCRT_EOF;
2520 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2521 if (mem != buf)
2522 MSVCRT_free (mem);
2523 return retval;
2526 /*********************************************************************
2527 * vfwprintf (MSVCRT.@)
2528 * FIXME:
2529 * Is final char included in written (then resize is too big) or not
2530 * (then we must test for equality too)?
2532 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2534 MSVCRT_wchar_t buf[2048], *mem = buf;
2535 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2536 /* See vfprintf comments */
2537 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2538 written > resize)
2540 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2541 if (mem != buf)
2542 MSVCRT_free (mem);
2543 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2544 return MSVCRT_EOF;
2546 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2547 if (mem != buf)
2548 MSVCRT_free (mem);
2549 return retval;
2552 /*********************************************************************
2553 * vprintf (MSVCRT.@)
2555 int MSVCRT_vprintf(const char *format, va_list valist)
2557 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2560 /*********************************************************************
2561 * vwprintf (MSVCRT.@)
2563 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2565 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2568 /*********************************************************************
2569 * fprintf (MSVCRT.@)
2571 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2573 va_list valist;
2574 int res;
2575 va_start(valist, format);
2576 res = MSVCRT_vfprintf(file, format, valist);
2577 va_end(valist);
2578 return res;
2581 /*********************************************************************
2582 * fwprintf (MSVCRT.@)
2584 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2586 va_list valist;
2587 int res;
2588 va_start(valist, format);
2589 res = MSVCRT_vfwprintf(file, format, valist);
2590 va_end(valist);
2591 return res;
2594 /*********************************************************************
2595 * printf (MSVCRT.@)
2597 int MSVCRT_printf(const char *format, ...)
2599 va_list valist;
2600 int res;
2601 va_start(valist, format);
2602 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2603 va_end(valist);
2604 return res;
2607 /*********************************************************************
2608 * ungetc (MSVCRT.@)
2610 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2612 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2613 msvcrt_alloc_buffer(file);
2614 file->_ptr++;
2616 if(file->_ptr>file->_base) {
2617 file->_ptr--;
2618 *file->_ptr=c;
2619 file->_cnt++;
2620 return c;
2622 return MSVCRT_EOF;
2625 /*********************************************************************
2626 * ungetwc (MSVCRT.@)
2628 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2630 MSVCRT_wchar_t mwc = wc;
2631 char * pp = (char *)&mwc;
2632 int i;
2633 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2634 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2635 return MSVCRT_WEOF;
2637 return mwc;
2640 /*********************************************************************
2641 * wprintf (MSVCRT.@)
2643 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2645 va_list valist;
2646 int res;
2647 va_start(valist, format);
2648 res = MSVCRT_vwprintf(format, valist);
2649 va_end(valist);
2650 return res;