Flush stdio on exit.
[wine/multimedia.git] / dlls / msvcrt / file.c
blobd9fa5e7054cbf2774ff5e380e136647e55191667
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 <stdio.h>
29 #include <unistd.h>
31 #include "ntddk.h"
32 #include "msvcrt.h"
33 #include "ms_errno.h"
35 #include "wine/unicode.h"
36 #include "msvcrt/direct.h"
37 #include "msvcrt/fcntl.h"
38 #include "msvcrt/io.h"
39 #include "msvcrt/sys/locking.h"
40 #include "msvcrt/stdio.h"
41 #include "msvcrt/stdlib.h"
42 #include "msvcrt/string.h"
43 #include "msvcrt/sys/stat.h"
44 #include "msvcrt/sys/utime.h"
45 #include "msvcrt/time.h"
46 #include "msvcrt/share.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
54 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
55 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
61 /* FIXME: Make this dynamic */
62 #define MSVCRT_MAX_FILES 257
64 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
65 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
66 int MSVCRT_flags[MSVCRT_MAX_FILES];
67 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
68 MSVCRT_FILE MSVCRT__iob[3];
69 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
70 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
71 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
73 static int MSVCRT_fdstart = 3; /* first unallocated fd */
74 static int MSVCRT_fdend = 3; /* highest allocated fd */
76 /* INTERNAL: process umask */
77 static int MSVCRT_umask = 0;
79 /* INTERNAL: Static buffer for temp file name */
80 static char MSVCRT_tmpname[MAX_PATH];
82 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
83 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
84 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
85 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
87 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
88 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
89 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
90 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
91 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
93 extern CRITICAL_SECTION MSVCRT_file_cs;
94 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
95 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
98 /* INTERNAL: Get the HANDLE for a fd */
99 static HANDLE msvcrt_fdtoh(int fd)
101 if (fd < 0 || fd >= MSVCRT_fdend ||
102 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
104 WARN(":fd (%d) - no handle!\n",fd);
105 SET_THREAD_VAR(doserrno,0);
106 SET_THREAD_VAR(errno,MSVCRT_EBADF);
107 return INVALID_HANDLE_VALUE;
109 return MSVCRT_handles[fd];
112 /* INTERNAL: free a file entry fd */
113 static void msvcrt_free_fd(int fd)
115 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
116 MSVCRT_files[fd] = 0;
117 MSVCRT_flags[fd] = 0;
118 TRACE(":fd (%d) freed\n",fd);
119 if (fd < 3)
120 return; /* dont use 0,1,2 for user files */
121 if (fd == MSVCRT_fdend - 1)
122 MSVCRT_fdend--;
123 if (fd < MSVCRT_fdstart)
124 MSVCRT_fdstart = fd;
127 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
128 static int msvcrt_alloc_fd(HANDLE hand, int flag)
130 int fd = MSVCRT_fdstart;
132 TRACE(":handle (%d) allocating fd (%d)\n",hand,fd);
133 if (fd >= MSVCRT_MAX_FILES)
135 WARN(":files exhausted!\n");
136 return -1;
138 MSVCRT_handles[fd] = hand;
139 MSVCRT_flags[fd] = flag;
141 /* locate next free slot */
142 if (fd == MSVCRT_fdend)
143 MSVCRT_fdstart = ++MSVCRT_fdend;
144 else
145 while(MSVCRT_fdstart < MSVCRT_fdend &&
146 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
147 MSVCRT_fdstart++;
149 return fd;
152 /* INTERNAL: Allocate a FILE* for an fd slot
153 * This is done lazily to avoid memory wastage for low level open/write
154 * usage when a FILE* is not requested (but may be later).
156 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
158 TRACE(":fd (%d) allocating FILE*\n",fd);
159 if (fd < 0 || fd >= MSVCRT_fdend ||
160 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
162 WARN(":invalid fd %d\n",fd);
163 SET_THREAD_VAR(doserrno,0);
164 SET_THREAD_VAR(errno,MSVCRT_EBADF);
165 return NULL;
167 if (!MSVCRT_files[fd])
169 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
171 MSVCRT_files[fd]->_file = fd;
172 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
173 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
176 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
177 return MSVCRT_files[fd];
181 /* INTERNAL: Set up stdin, stderr and stdout */
182 void msvcrt_init_io(void)
184 int i;
185 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
186 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
187 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
188 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
189 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
190 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
191 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
193 TRACE(":handles (%d)(%d)(%d)\n",MSVCRT_handles[0],
194 MSVCRT_handles[1],MSVCRT_handles[2]);
196 for (i = 0; i < 3; i++)
198 /* FILE structs for stdin/out/err are static and never deleted */
199 MSVCRT_files[i] = &MSVCRT__iob[i];
200 MSVCRT__iob[i]._file = i;
201 MSVCRT_tempfiles[i] = NULL;
205 /* free everything on process exit */
206 void msvcrt_free_io(void)
208 _fcloseall();
209 _close(0);
210 _close(1);
211 _close(2);
214 /* INTERNAL: Flush stdio file buffer */
215 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
217 if(file->_bufsiz) {
218 int cnt=file->_ptr-file->_base;
219 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
220 return MSVCRT_EOF;
222 file->_ptr=file->_base;
223 file->_cnt=file->_bufsiz;
225 return 0;
228 /* INTERNAL: Allocate stdio file buffer */
229 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
231 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
232 if(file->_base) {
233 file->_bufsiz = MSVCRT_BUFSIZ;
234 file->_flag |= MSVCRT__IOMYBUF;
235 } else {
236 file->_base = (unsigned char *)(&file->_charbuf);
237 /* put here 2 ??? */
238 file->_bufsiz = sizeof(file->_charbuf);
240 file->_ptr = file->_base;
241 file->_cnt = 0;
244 /*********************************************************************
245 * __p__iob(MSVCRT.@)
247 MSVCRT_FILE *__p__iob(void)
249 return &MSVCRT__iob[0];
252 /*********************************************************************
253 * _access (MSVCRT.@)
255 int _access(const char *filename, int mode)
257 DWORD attr = GetFileAttributesA(filename);
259 TRACE("(%s,%d) %ld\n",filename,mode,attr);
261 if (!filename || attr == 0xffffffff)
263 MSVCRT__set_errno(GetLastError());
264 return -1;
266 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
268 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
269 return -1;
271 return 0;
274 /*********************************************************************
275 * _waccess (MSVCRT.@)
277 int _waccess(const WCHAR *filename, int mode)
279 DWORD attr = GetFileAttributesW(filename);
281 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
283 if (!filename || attr == 0xffffffff)
285 MSVCRT__set_errno(GetLastError());
286 return -1;
288 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
290 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
291 return -1;
293 return 0;
296 /*********************************************************************
297 * _chmod (MSVCRT.@)
299 int _chmod(const char *path, int flags)
301 DWORD oldFlags = GetFileAttributesA(path);
303 if (oldFlags != 0x0FFFFFFFF)
305 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
306 oldFlags | FILE_ATTRIBUTE_READONLY;
308 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
309 return 0;
311 MSVCRT__set_errno(GetLastError());
312 return -1;
315 /*********************************************************************
316 * _wchmod (MSVCRT.@)
318 int _wchmod(const WCHAR *path, int flags)
320 DWORD oldFlags = GetFileAttributesW(path);
322 if (oldFlags != 0x0FFFFFFFF)
324 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
325 oldFlags | FILE_ATTRIBUTE_READONLY;
327 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
328 return 0;
330 MSVCRT__set_errno(GetLastError());
331 return -1;
334 /*********************************************************************
335 * _unlink (MSVCRT.@)
337 int _unlink(const char *path)
339 TRACE("(%s)\n",path);
340 if(DeleteFileA(path))
341 return 0;
342 TRACE("failed (%ld)\n",GetLastError());
343 MSVCRT__set_errno(GetLastError());
344 return -1;
347 /*********************************************************************
348 * _wunlink (MSVCRT.@)
350 int _wunlink(const WCHAR *path)
352 TRACE("(%s)\n",debugstr_w(path));
353 if(DeleteFileW(path))
354 return 0;
355 TRACE("failed (%ld)\n",GetLastError());
356 MSVCRT__set_errno(GetLastError());
357 return -1;
360 /*********************************************************************
361 * _close (MSVCRT.@)
363 int _close(int fd)
365 HANDLE hand = msvcrt_fdtoh(fd);
367 TRACE(":fd (%d) handle (%d)\n",fd,hand);
368 if (hand == INVALID_HANDLE_VALUE)
369 return -1;
370 /* flush stdio buffers */
371 if(MSVCRT_files[fd]) {
372 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
373 MSVCRT_fflush(MSVCRT_files[fd]);
375 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
376 MSVCRT_free(MSVCRT_files[fd]->_base);
379 /* Dont free std FILE*'s, they are not dynamic */
380 if (fd > 2 && MSVCRT_files[fd])
381 MSVCRT_free(MSVCRT_files[fd]);
383 msvcrt_free_fd(fd);
385 if (!CloseHandle(hand))
387 WARN(":failed-last error (%ld)\n",GetLastError());
388 MSVCRT__set_errno(GetLastError());
389 return -1;
391 if (MSVCRT_tempfiles[fd])
393 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
394 _unlink(MSVCRT_tempfiles[fd]);
395 MSVCRT_free(MSVCRT_tempfiles[fd]);
396 MSVCRT_tempfiles[fd] = NULL;
399 TRACE(":ok\n");
400 return 0;
403 /*********************************************************************
404 * _commit (MSVCRT.@)
406 int _commit(int fd)
408 HANDLE hand = msvcrt_fdtoh(fd);
410 TRACE(":fd (%d) handle (%d)\n",fd,hand);
411 if (hand == INVALID_HANDLE_VALUE)
412 return -1;
414 if (!FlushFileBuffers(hand))
416 if (GetLastError() == ERROR_INVALID_HANDLE)
418 /* FlushFileBuffers fails for console handles
419 * so we ignore this error.
421 return 0;
423 TRACE(":failed-last error (%ld)\n",GetLastError());
424 MSVCRT__set_errno(GetLastError());
425 return -1;
427 TRACE(":ok\n");
428 return 0;
431 /*********************************************************************
432 * _eof (MSVCRT.@)
434 int _eof(int fd)
436 DWORD curpos,endpos;
437 HANDLE hand = msvcrt_fdtoh(fd);
439 TRACE(":fd (%d) handle (%d)\n",fd,hand);
441 if (hand == INVALID_HANDLE_VALUE)
442 return -1;
444 /* If we have a FILE* for this file, the EOF flag
445 * will be set by the read()/write() functions.
447 if (MSVCRT_files[fd])
448 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
450 /* Otherwise we do it the hard way */
451 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
452 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
454 if (curpos == endpos)
455 return TRUE;
457 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
458 return FALSE;
461 /*********************************************************************
462 * _fcloseall (MSVCRT.@)
464 int _fcloseall(void)
466 int num_closed = 0, i;
468 for (i = 3; i < MSVCRT_fdend; i++)
469 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
471 _close(i);
472 num_closed++;
475 TRACE(":closed (%d) handles\n",num_closed);
476 return num_closed;
479 /*********************************************************************
480 * _lseek (MSVCRT.@)
482 LONG _lseek(int fd, LONG offset, int whence)
484 DWORD ret;
485 HANDLE hand = msvcrt_fdtoh(fd);
487 TRACE(":fd (%d) handle (%d)\n",fd,hand);
488 if (hand == INVALID_HANDLE_VALUE)
489 return -1;
491 if (whence < 0 || whence > 2)
493 SET_THREAD_VAR(errno,MSVCRT_EINVAL);
494 return -1;
497 TRACE(":fd (%d) to 0x%08lx pos %s\n",
498 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
499 (whence==SEEK_CUR)?"SEEK_CUR":
500 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
502 if ((ret = SetFilePointer(hand, offset, NULL, whence)) != 0xffffffff)
504 if (MSVCRT_files[fd])
505 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
506 /* FIXME: What if we seek _to_ EOF - is EOF set? */
507 return ret;
509 TRACE(":error-last error (%ld)\n",GetLastError());
510 if (MSVCRT_files[fd])
511 switch(GetLastError())
513 case ERROR_NEGATIVE_SEEK:
514 case ERROR_SEEK_ON_DEVICE:
515 MSVCRT__set_errno(GetLastError());
516 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
517 break;
518 default:
519 break;
521 return -1;
524 /*********************************************************************
525 * _locking (MSVCRT.@)
527 * This is untested; the underlying LockFile doesn't work yet.
529 int _locking(int fd, int mode, LONG nbytes)
531 BOOL ret;
532 DWORD cur_locn;
533 HANDLE hand = msvcrt_fdtoh(fd);
535 TRACE(":fd (%d) handle (%d)\n",fd,hand);
536 if (hand == INVALID_HANDLE_VALUE)
537 return -1;
539 if (mode < 0 || mode > 4)
541 SET_THREAD_VAR(errno,MSVCRT_EINVAL);
542 return -1;
545 TRACE(":fd (%d) by 0x%08lx mode %s\n",
546 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
547 (mode==_LK_LOCK)?"_LK_LOCK":
548 (mode==_LK_NBLCK)?"_LK_NBLCK":
549 (mode==_LK_RLCK)?"_LK_RLCK":
550 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
551 "UNKNOWN");
553 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == 0xffffffff)
555 FIXME ("Seek failed\n");
556 SET_THREAD_VAR(errno,MSVCRT_EINVAL); /* FIXME */
557 return -1;
559 if (mode == _LK_LOCK || mode == _LK_RLCK)
561 int nretry = 10;
562 ret = 1; /* just to satisfy gcc */
563 while (nretry--)
565 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
566 if (ret) break;
567 sleep (1);
570 else if (mode == _LK_UNLCK)
571 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
572 else
573 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
574 /* FIXME - what about error settings? */
575 return ret ? 0 : -1;
578 /*********************************************************************
579 * rewind (MSVCRT.@)
581 void MSVCRT_rewind(MSVCRT_FILE* file)
583 TRACE(":file (%p) fd (%d)\n",file,file->_file);
584 MSVCRT_fseek(file, 0L, SEEK_SET);
585 MSVCRT_clearerr(file);
588 /*********************************************************************
589 * _fdopen (MSVCRT.@)
591 MSVCRT_FILE* _fdopen(int fd, const char *mode)
593 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
595 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
596 if (file)
597 MSVCRT_rewind(file);
599 return file;
602 /*********************************************************************
603 * _wfdopen (MSVCRT.@)
605 MSVCRT_FILE* _wfdopen(int fd, const WCHAR *mode)
607 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
609 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
610 if (file)
611 MSVCRT_rewind(file);
613 return file;
616 /*********************************************************************
617 * _filelength (MSVCRT.@)
619 LONG _filelength(int fd)
621 LONG curPos = _lseek(fd, 0, SEEK_CUR);
622 if (curPos != -1)
624 LONG endPos = _lseek(fd, 0, SEEK_END);
625 if (endPos != -1)
627 if (endPos != curPos)
628 _lseek(fd, curPos, SEEK_SET);
629 return endPos;
632 return -1;
635 /*********************************************************************
636 * _fileno (MSVCRT.@)
638 int _fileno(MSVCRT_FILE* file)
640 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
641 return file->_file;
644 /*********************************************************************
645 * _flushall (MSVCRT.@)
647 int _flushall(void)
649 int num_flushed = 0, i = 3;
651 while(i < MSVCRT_fdend)
652 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
654 #if 0
655 /* FIXME: flush, do not commit */
656 if (_commit(i) == -1)
657 if (MSVCRT_files[i])
658 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
659 #endif
660 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
661 MSVCRT_fflush(MSVCRT_files[i]);
662 num_flushed++;
666 TRACE(":flushed (%d) handles\n",num_flushed);
667 return num_flushed;
670 /*********************************************************************
671 * _fstat (MSVCRT.@)
673 int _fstat(int fd, struct _stat* buf)
675 DWORD dw;
676 BY_HANDLE_FILE_INFORMATION hfi;
677 HANDLE hand = msvcrt_fdtoh(fd);
679 TRACE(":fd (%d) stat (%p)\n",fd,buf);
680 if (hand == INVALID_HANDLE_VALUE)
681 return -1;
683 if (!buf)
685 WARN(":failed-NULL buf\n");
686 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
687 return -1;
690 memset(&hfi, 0, sizeof(hfi));
691 memset(buf, 0, sizeof(struct _stat));
692 if (!GetFileInformationByHandle(hand, &hfi))
694 WARN(":failed-last error (%ld)\n",GetLastError());
695 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
696 return -1;
698 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
699 buf->st_nlink = hfi.nNumberOfLinks;
700 buf->st_size = hfi.nFileSizeLow;
701 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
702 buf->st_atime = dw;
703 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
704 buf->st_mtime = buf->st_ctime = dw;
705 return 0;
708 /*********************************************************************
709 * _futime (MSVCRT.@)
711 int _futime(int fd, struct _utimbuf *t)
713 HANDLE hand = msvcrt_fdtoh(fd);
714 FILETIME at, wt;
716 if (!t)
718 MSVCRT_time_t currTime;
719 MSVCRT_time(&currTime);
720 RtlSecondsSince1970ToTime(currTime, &at);
721 memcpy(&wt, &at, sizeof(wt));
723 else
725 RtlSecondsSince1970ToTime(t->actime, &at);
726 if (t->actime == t->modtime)
727 memcpy(&wt, &at, sizeof(wt));
728 else
729 RtlSecondsSince1970ToTime(t->modtime, &wt);
732 if (!SetFileTime(hand, NULL, &at, &wt))
734 MSVCRT__set_errno(GetLastError());
735 return -1 ;
737 return 0;
740 /*********************************************************************
741 * _get_osfhandle (MSVCRT.@)
743 long _get_osfhandle(int fd)
745 HANDLE hand = msvcrt_fdtoh(fd);
746 HANDLE newhand = hand;
747 TRACE(":fd (%d) handle (%d)\n",fd,hand);
749 if (hand != INVALID_HANDLE_VALUE)
751 /* FIXME: I'm not convinced that I should be copying the
752 * handle here - it may be leaked if the app doesn't
753 * close it (and the API docs dont say that it should)
754 * Not duplicating it means that it can't be inherited
755 * and so lcc's wedit doesn't cope when it passes it to
756 * child processes. I've an idea that it should either
757 * be copied by CreateProcess, or marked as inheritable
758 * when initialised, or maybe both? JG 21-9-00.
760 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
761 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
763 return newhand;
766 /*********************************************************************
767 * _isatty (MSVCRT.@)
769 int _isatty(int fd)
771 HANDLE hand = msvcrt_fdtoh(fd);
773 TRACE(":fd (%d) handle (%d)\n",fd,hand);
774 if (hand == INVALID_HANDLE_VALUE)
775 return 0;
777 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
780 /*********************************************************************
781 * _mktemp (MSVCRT.@)
783 char *_mktemp(char *pattern)
785 int numX = 0;
786 char *retVal = pattern;
787 int id;
788 char letter = 'a';
790 while(*pattern)
791 numX = (*pattern++ == 'X')? numX + 1 : 0;
792 if (numX < 5)
793 return NULL;
794 pattern--;
795 id = GetCurrentProcessId();
796 numX = 6;
797 while(numX--)
799 int tempNum = id / 10;
800 *pattern-- = id - (tempNum * 10) + '0';
801 id = tempNum;
803 pattern++;
806 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
807 GetLastError() == ERROR_FILE_NOT_FOUND)
808 return retVal;
809 *pattern = letter++;
810 } while(letter != '|');
811 return NULL;
814 /*********************************************************************
815 * _wmktemp (MSVCRT.@)
817 WCHAR *_wmktemp(WCHAR *pattern)
819 int numX = 0;
820 WCHAR *retVal = pattern;
821 int id;
822 WCHAR letter = (WCHAR)L'a';
824 while(*pattern)
825 numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
826 if (numX < 5)
827 return NULL;
828 pattern--;
829 id = GetCurrentProcessId();
830 numX = 6;
831 while(numX--)
833 int tempNum = id / 10;
834 *pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
835 id = tempNum;
837 pattern++;
840 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
841 GetLastError() == ERROR_FILE_NOT_FOUND)
842 return retVal;
843 *pattern = letter++;
844 } while(letter != (WCHAR)L'|');
845 return NULL;
848 /*********************************************************************
849 * _sopen (MSVCRT.@)
851 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
853 va_list ap;
854 int pmode;
855 DWORD access = 0, creation = 0;
856 DWORD sharing;
857 int ioflag = 0, fd;
858 HANDLE hand;
859 SECURITY_ATTRIBUTES sa;
862 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
863 path, oflags, shflags);
865 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
867 case _O_RDONLY:
868 access |= GENERIC_READ;
869 ioflag |= MSVCRT__IOREAD;
870 break;
871 case _O_WRONLY:
872 access |= GENERIC_WRITE;
873 ioflag |= MSVCRT__IOWRT;
874 break;
875 case _O_RDWR:
876 access |= GENERIC_WRITE | GENERIC_READ;
877 ioflag |= MSVCRT__IORW;
878 break;
881 if (oflags & _O_CREAT)
883 va_start(ap, shflags);
884 pmode = va_arg(ap, int);
885 va_end(ap);
887 FIXME(": pmode 0x%04x ignored\n", pmode);
889 if (oflags & _O_EXCL)
890 creation = CREATE_NEW;
891 else if (oflags & _O_TRUNC)
892 creation = CREATE_ALWAYS;
893 else
894 creation = OPEN_ALWAYS;
896 else /* no _O_CREAT */
898 if (oflags & _O_TRUNC)
899 creation = TRUNCATE_EXISTING;
900 else
901 creation = OPEN_EXISTING;
903 if (oflags & _O_APPEND)
904 ioflag |= MSVCRT__IOAPPEND;
907 oflags |= _O_BINARY; /* FIXME: Default to text */
909 if (oflags & _O_TEXT)
911 /* Dont warn when writing */
912 if (ioflag & GENERIC_READ)
913 FIXME(":TEXT node not implemented\n");
914 oflags &= ~_O_TEXT;
917 switch( shflags )
919 case _SH_DENYRW:
920 sharing = 0L;
921 break;
922 case _SH_DENYWR:
923 sharing = FILE_SHARE_READ;
924 break;
925 case _SH_DENYRD:
926 sharing = FILE_SHARE_WRITE;
927 break;
928 case _SH_DENYNO:
929 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
930 break;
931 default:
932 ERR( "Unhandled shflags 0x%x\n", shflags );
933 return -1;
936 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
937 |_O_CREAT|_O_RDWR|_O_TEMPORARY|_O_NOINHERIT))
938 TRACE(":unsupported oflags 0x%04x\n",oflags);
940 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
941 sa.lpSecurityDescriptor = NULL;
942 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
944 hand = CreateFileA(path, access, sharing,
945 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
947 if (hand == INVALID_HANDLE_VALUE) {
948 WARN(":failed-last error (%ld)\n",GetLastError());
949 MSVCRT__set_errno(GetLastError());
950 return -1;
953 fd = msvcrt_alloc_fd(hand, ioflag);
955 TRACE(":fd (%d) handle (%d)\n",fd, hand);
957 if (fd > 0)
959 if (oflags & _O_TEMPORARY)
960 MSVCRT_tempfiles[fd] = _strdup(path);
961 if (ioflag & MSVCRT__IOAPPEND)
962 _lseek(fd, 0, FILE_END);
965 return fd;
968 /*********************************************************************
969 * _wsopen (MSVCRT.@)
971 int MSVCRT__wsopen( const WCHAR* path, int oflags, int shflags, ... )
973 const unsigned int len = strlenW(path);
974 char *patha = MSVCRT_calloc(len + 1,1);
975 va_list ap;
976 int pmode;
978 va_start(ap, shflags);
979 pmode = va_arg(ap, int);
980 va_end(ap);
982 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
984 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
985 MSVCRT_free(patha);
986 return retval;
989 MSVCRT__set_errno(GetLastError());
990 return -1;
993 /*********************************************************************
994 * _open (MSVCRT.@)
996 int _open( const char *path, int flags, ... )
998 va_list ap;
999 int pmode;
1001 va_start(ap, flags);
1002 pmode = va_arg(ap, int);
1003 va_end(ap);
1005 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1008 /*********************************************************************
1009 * _wopen (MSVCRT.@)
1011 int _wopen(const WCHAR *path,int flags,...)
1013 const unsigned int len = strlenW(path);
1014 char *patha = MSVCRT_calloc(len + 1,1);
1015 va_list ap;
1016 int pmode;
1018 va_start(ap, flags);
1019 pmode = va_arg(ap, int);
1020 va_end(ap);
1022 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1024 int retval = _open(patha,flags,pmode);
1025 MSVCRT_free(patha);
1026 return retval;
1029 MSVCRT__set_errno(GetLastError());
1030 return -1;
1033 /*********************************************************************
1034 * _creat (MSVCRT.@)
1036 int _creat(const char *path, int flags)
1038 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1039 return _open(path, usedFlags);
1042 /*********************************************************************
1043 * _wcreat (MSVCRT.@)
1045 int _wcreat(const WCHAR *path, int flags)
1047 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1048 return _wopen(path, usedFlags);
1051 /*********************************************************************
1052 * _open_osfhandle (MSVCRT.@)
1054 int _open_osfhandle(long hand, int flags)
1056 int fd = msvcrt_alloc_fd(hand,flags);
1057 TRACE(":handle (%ld) fd (%d)\n",hand,fd);
1058 return fd;
1061 /*********************************************************************
1062 * _rmtmp (MSVCRT.@)
1064 int _rmtmp(void)
1066 int num_removed = 0, i;
1068 for (i = 3; i < MSVCRT_fdend; i++)
1069 if (MSVCRT_tempfiles[i])
1071 _close(i);
1072 num_removed++;
1075 if (num_removed)
1076 TRACE(":removed (%d) temp files\n",num_removed);
1077 return num_removed;
1080 /*********************************************************************
1081 * _read (MSVCRT.@)
1083 int _read(int fd, void *buf, unsigned int count)
1085 DWORD num_read;
1086 HANDLE hand = msvcrt_fdtoh(fd);
1088 /* Dont trace small reads, it gets *very* annoying */
1089 if (count > 4)
1090 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1091 if (hand == INVALID_HANDLE_VALUE)
1092 return -1;
1094 if (ReadFile(hand, buf, count, &num_read, NULL))
1096 if (num_read != count && MSVCRT_files[fd])
1098 TRACE(":EOF\n");
1099 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1101 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1104 return num_read;
1106 TRACE(":failed-last error (%ld)\n",GetLastError());
1107 if (MSVCRT_files[fd])
1108 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1109 return -1;
1112 /*********************************************************************
1113 * _getw (MSVCRT.@)
1115 int _getw(MSVCRT_FILE* file)
1117 int i;
1118 if (_read(file->_file, &i, sizeof(int)) != 1)
1119 return MSVCRT_EOF;
1120 return i;
1123 /*********************************************************************
1124 * _setmode (MSVCRT.@)
1126 int _setmode(int fd,int mode)
1128 if (mode & _O_TEXT)
1129 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
1130 return 0;
1133 /*********************************************************************
1134 * _stat (MSVCRT.@)
1136 int _stat(const char* path, struct _stat * buf)
1138 DWORD dw;
1139 WIN32_FILE_ATTRIBUTE_DATA hfi;
1140 unsigned short mode = MSVCRT_S_IREAD;
1141 int plen;
1143 TRACE(":file (%s) buf(%p)\n",path,buf);
1145 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1147 TRACE("failed (%ld)\n",GetLastError());
1148 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1149 return -1;
1152 memset(buf,0,sizeof(struct _stat));
1154 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1155 Bon 011120: This FIXME seems incorrect
1156 Also a letter as first char isn't enough to be classify
1157 as drive letter
1159 if (isalpha(*path)&& (*(path+1)==':'))
1160 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1161 else
1162 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1164 plen = strlen(path);
1166 /* Dir, or regular file? */
1167 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1168 (path[plen-1] == '\\'))
1169 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1170 else
1172 mode |= _S_IFREG;
1173 /* executable? */
1174 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1176 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1177 (tolower(path[plen-3]) << 16);
1178 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1179 mode |= MSVCRT_S_IEXEC;
1183 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1184 mode |= MSVCRT_S_IWRITE;
1186 buf->st_mode = mode;
1187 buf->st_nlink = 1;
1188 buf->st_size = hfi.nFileSizeLow;
1189 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1190 buf->st_atime = dw;
1191 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1192 buf->st_mtime = buf->st_ctime = dw;
1193 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1194 buf->st_atime,buf->st_mtime, buf->st_ctime);
1195 return 0;
1198 /*********************************************************************
1199 * _wstat (MSVCRT.@)
1201 int _wstat(const WCHAR* path, struct _stat * buf)
1203 DWORD dw;
1204 WIN32_FILE_ATTRIBUTE_DATA hfi;
1205 unsigned short mode = MSVCRT_S_IREAD;
1206 int plen;
1208 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1210 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1212 TRACE("failed (%ld)\n",GetLastError());
1213 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1214 return -1;
1217 memset(buf,0,sizeof(struct _stat));
1219 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1220 if (MSVCRT_iswalpha(*path))
1221 buf->st_dev = buf->st_rdev = toupperW(*path - (WCHAR)L'A'); /* drive num */
1222 else
1223 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1225 plen = strlenW(path);
1227 /* Dir, or regular file? */
1228 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1229 (path[plen-1] == (WCHAR)L'\\'))
1230 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1231 else
1233 mode |= _S_IFREG;
1234 /* executable? */
1235 if (plen > 6 && path[plen-4] == (WCHAR)L'.') /* shortest exe: "\x.exe" */
1237 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1238 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1239 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1240 mode |= MSVCRT_S_IEXEC;
1244 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1245 mode |= MSVCRT_S_IWRITE;
1247 buf->st_mode = mode;
1248 buf->st_nlink = 1;
1249 buf->st_size = hfi.nFileSizeLow;
1250 RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1251 buf->st_atime = dw;
1252 RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1253 buf->st_mtime = buf->st_ctime = dw;
1254 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1255 buf->st_atime,buf->st_mtime, buf->st_ctime);
1256 return 0;
1259 /*********************************************************************
1260 * _tell (MSVCRT.@)
1262 LONG _tell(int fd)
1264 return _lseek(fd, 0, SEEK_CUR);
1267 /*********************************************************************
1268 * _tempnam (MSVCRT.@)
1270 char *_tempnam(const char *dir, const char *prefix)
1272 char tmpbuf[MAX_PATH];
1274 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1275 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1277 TRACE("got name (%s)\n",tmpbuf);
1278 return _strdup(tmpbuf);
1280 TRACE("failed (%ld)\n",GetLastError());
1281 return NULL;
1284 /*********************************************************************
1285 * _wtempnam (MSVCRT.@)
1287 WCHAR *_wtempnam(const WCHAR *dir, const WCHAR *prefix)
1289 WCHAR tmpbuf[MAX_PATH];
1291 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1292 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1294 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1295 return _wcsdup(tmpbuf);
1297 TRACE("failed (%ld)\n",GetLastError());
1298 return NULL;
1301 /*********************************************************************
1302 * _umask (MSVCRT.@)
1304 int _umask(int umask)
1306 int old_umask = MSVCRT_umask;
1307 TRACE("(%d)\n",umask);
1308 MSVCRT_umask = umask;
1309 return old_umask;
1312 /*********************************************************************
1313 * _utime (MSVCRT.@)
1315 int _utime(const char* path, struct _utimbuf *t)
1317 int fd = _open(path, _O_WRONLY | _O_BINARY);
1319 if (fd > 0)
1321 int retVal = _futime(fd, t);
1322 _close(fd);
1323 return retVal;
1325 return -1;
1328 /*********************************************************************
1329 * _wutime (MSVCRT.@)
1331 int _wutime(const WCHAR* path, struct _utimbuf *t)
1333 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1335 if (fd > 0)
1337 int retVal = _futime(fd, t);
1338 _close(fd);
1339 return retVal;
1341 return -1;
1344 /*********************************************************************
1345 * _write (MSVCRT.@)
1347 int _write(int fd, const void* buf, unsigned int count)
1349 DWORD num_written;
1350 HANDLE hand = msvcrt_fdtoh(fd);
1352 /* Dont trace small writes, it gets *very* annoying */
1353 #if 0
1354 if (count > 32)
1355 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1356 #endif
1357 if (hand == INVALID_HANDLE_VALUE)
1358 return -1;
1360 /* If appending, go to EOF */
1361 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1362 _lseek(fd, 0, FILE_END);
1364 if (WriteFile(hand, buf, count, &num_written, NULL)
1365 && (num_written == count))
1366 return num_written;
1368 TRACE(":failed-last error (%ld)\n",GetLastError());
1369 if (MSVCRT_files[fd])
1370 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1372 return -1;
1375 /*********************************************************************
1376 * _putw (MSVCRT.@)
1378 int _putw(int val, MSVCRT_FILE* file)
1380 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1383 /*********************************************************************
1384 * clearerr (MSVCRT.@)
1386 void MSVCRT_clearerr(MSVCRT_FILE* file)
1388 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1389 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1392 /*********************************************************************
1393 * fclose (MSVCRT.@)
1395 int MSVCRT_fclose(MSVCRT_FILE* file)
1397 int r;
1398 r=_close(file->_file);
1399 return ((r==MSVCRT_EOF) || (file->_flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1402 /*********************************************************************
1403 * feof (MSVCRT.@)
1405 int MSVCRT_feof(MSVCRT_FILE* file)
1407 return file->_flag & MSVCRT__IOEOF;
1410 /*********************************************************************
1411 * ferror (MSVCRT.@)
1413 int MSVCRT_ferror(MSVCRT_FILE* file)
1415 return file->_flag & MSVCRT__IOERR;
1418 /*********************************************************************
1419 * fflush (MSVCRT.@)
1421 int MSVCRT_fflush(MSVCRT_FILE* file)
1423 if(!file) {
1424 _flushall();
1425 return 0;
1426 } else {
1427 int res=msvcrt_flush_buffer(file);
1428 return res;
1432 /*********************************************************************
1433 * fgetc (MSVCRT.@)
1435 int MSVCRT_fgetc(MSVCRT_FILE* file)
1437 if (file->_cnt>0) {
1438 file->_cnt--;
1439 return *(unsigned char *)file->_ptr++;
1440 } else {
1441 return _filbuf(file);
1445 /*********************************************************************
1446 * _fgetchar (MSVCRT.@)
1448 int _fgetchar(void)
1450 return MSVCRT_fgetc(MSVCRT_stdin);
1453 /*********************************************************************
1454 * _filbuf (MSVCRT.@)
1456 int _filbuf(MSVCRT_FILE* file)
1459 /* Allocate buffer if needed */
1460 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1461 msvcrt_alloc_buffer(file);
1463 if(!(file->_flag & MSVCRT__IOREAD)) {
1464 if(file->_flag & MSVCRT__IORW) {
1465 file->_flag |= MSVCRT__IOREAD;
1466 } else {
1467 return MSVCRT_EOF;
1470 if(file->_flag & MSVCRT__IONBF) {
1471 unsigned char c;
1472 if (_read(file->_file,&c,1) != 1) {
1473 file->_flag |= MSVCRT__IOEOF;
1474 return MSVCRT_EOF;
1476 return c;
1477 } else {
1478 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1479 if(file->_cnt<0) file->_cnt = 0;
1480 if(!file->_cnt) {
1481 file->_flag |= MSVCRT__IOEOF;
1482 return MSVCRT_EOF;
1484 file->_cnt--;
1485 file->_ptr = file->_base+1;
1486 return *(unsigned char *)file->_base;
1490 /*********************************************************************
1491 * fgetpos (MSVCRT.@)
1493 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1495 *pos = MSVCRT_ftell(file);
1496 return (*pos == -1? -1 : 0);
1499 /*********************************************************************
1500 * fgets (MSVCRT.@)
1502 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1504 int cc;
1505 char * buf_start = s;
1507 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1508 file,file->_file,s,size);
1510 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1511 cc = MSVCRT_fgetc(file))
1512 if (cc != '\r')
1514 if (--size <= 0) break;
1515 *s++ = (char)cc;
1517 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1519 TRACE(":nothing read\n");
1520 return 0;
1522 if (cc == '\n')
1523 if (--size > 0)
1524 *s++ = '\n';
1525 *s = '\0';
1526 TRACE(":got '%s'\n", buf_start);
1527 return buf_start;
1530 /*********************************************************************
1531 * fgetwc (MSVCRT.@)
1533 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1535 WCHAR wc;
1536 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1537 return MSVCRT_WEOF;
1538 return wc;
1541 /*********************************************************************
1542 * getwc (MSVCRT.@)
1544 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1546 return MSVCRT_fgetwc(file);
1549 /*********************************************************************
1550 * _fgetwchar (MSVCRT.@)
1552 MSVCRT_wint_t _fgetwchar(void)
1554 return MSVCRT_fgetwc(MSVCRT_stdin);
1557 /*********************************************************************
1558 * getwchar (MSVCRT.@)
1560 MSVCRT_wint_t MSVCRT_getwchar(void)
1562 return _fgetwchar();
1565 /*********************************************************************
1566 * fgetws (MSVCRT.@)
1568 WCHAR *MSVCRT_fgetws(WCHAR *s, int size, MSVCRT_FILE* file)
1570 int cc;
1571 WCHAR * buf_start = s;
1573 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1574 file,file->_file,s,size);
1576 for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n';
1577 cc = MSVCRT_fgetwc(file))
1578 if (cc != L'\r')
1580 if (--size <= 0) break;
1581 *s++ = cc;
1583 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1585 TRACE(":nothing read\n");
1586 return 0;
1588 if (cc == L'\n')
1589 if (--size > 0)
1590 *s++ = '\n';
1591 *s = '\0';
1592 /* TRACE(":got '%s'\n", buf_start); */
1593 return buf_start;
1597 /*********************************************************************
1598 * fputwc (MSVCRT.@)
1600 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1602 WCHAR mwc=wc;
1603 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
1604 return MSVCRT_WEOF;
1605 return wc;
1608 /*********************************************************************
1609 * _fputwchar (MSVCRT.@)
1611 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1613 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1616 /*********************************************************************
1617 * fopen (MSVCRT.@)
1619 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1621 MSVCRT_FILE* file;
1622 int flags = 0, plus = 0, fd;
1623 const char* search = mode;
1625 TRACE("(%s,%s)\n",path,mode);
1627 while (*search)
1628 if (*search++ == '+')
1629 plus = 1;
1631 /* map mode string to open() flags. "man fopen" for possibilities. */
1632 switch(*mode++)
1634 case 'R': case 'r':
1635 flags = (plus ? _O_RDWR : _O_RDONLY);
1636 break;
1637 case 'W': case 'w':
1638 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1639 break;
1640 case 'A': case 'a':
1641 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1642 break;
1643 default:
1644 return NULL;
1647 while (*mode)
1648 switch (*mode++)
1650 case 'B': case 'b':
1651 flags |= _O_BINARY;
1652 flags &= ~_O_TEXT;
1653 break;
1654 case 'T': case 't':
1655 flags |= _O_TEXT;
1656 flags &= ~_O_BINARY;
1657 break;
1658 case '+':
1659 break;
1660 default:
1661 FIXME(":unknown flag %c not supported\n",mode[-1]);
1664 fd = _open(path, flags);
1666 if (fd < 0)
1667 return NULL;
1669 file = msvcrt_alloc_fp(fd);
1670 TRACE(":got (%p)\n",file);
1671 if (!file)
1672 _close(fd);
1674 return file;
1677 /*********************************************************************
1678 * _wfopen (MSVCRT.@)
1680 MSVCRT_FILE *_wfopen(const WCHAR *path, const WCHAR *mode)
1682 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1683 char *patha = MSVCRT_calloc(plen + 1, 1);
1684 char *modea = MSVCRT_calloc(mlen + 1, 1);
1686 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1688 if (patha && modea &&
1689 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1690 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1692 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1693 MSVCRT_free(patha);
1694 MSVCRT_free(modea);
1695 return retval;
1698 MSVCRT__set_errno(GetLastError());
1699 return NULL;
1702 /*********************************************************************
1703 * _fsopen (MSVCRT.@)
1705 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1707 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1708 return MSVCRT_fopen(path,mode);
1711 /*********************************************************************
1712 * _wfsopen (MSVCRT.@)
1714 MSVCRT_FILE* _wfsopen(const WCHAR *path, const WCHAR *mode, int share)
1716 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1717 debugstr_w(path),debugstr_w(mode),share);
1718 return _wfopen(path,mode);
1721 /*********************************************************************
1722 * fputc (MSVCRT.@)
1724 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1726 if(file->_cnt>0) {
1727 *file->_ptr++=c;
1728 file->_cnt--;
1729 return c;
1730 } else {
1731 return _flsbuf(c, file);
1735 /*********************************************************************
1736 * _flsbuf (MSVCRT.@)
1738 int _flsbuf(int c, MSVCRT_FILE* file)
1740 /* Flush output buffer */
1741 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
1742 msvcrt_alloc_buffer(file);
1744 if(!(file->_flag & MSVCRT__IOWRT)) {
1745 if(file->_flag & MSVCRT__IORW) {
1746 file->_flag |= MSVCRT__IOWRT;
1747 } else {
1748 return MSVCRT_EOF;
1751 if(file->_bufsiz) {
1752 int res=msvcrt_flush_buffer(file);
1753 return res?res : MSVCRT_fputc(c, file);
1754 } else {
1755 unsigned char cc=c;
1756 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
1760 /*********************************************************************
1761 * _fputchar (MSVCRT.@)
1763 int _fputchar(int c)
1765 return MSVCRT_fputc(c, MSVCRT_stdout);
1768 /*********************************************************************
1769 * fread (MSVCRT.@)
1771 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1772 { MSVCRT_size_t rcnt=size * nmemb;
1773 MSVCRT_size_t read=0;
1774 int pread=0;
1775 /* first buffered data */
1776 if(file->_cnt>0) {
1777 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
1778 memcpy(ptr, file->_ptr, pcnt);
1779 file->_cnt -= pcnt;
1780 file->_ptr += pcnt;
1781 read += pcnt ;
1782 rcnt -= pcnt ;
1783 ptr = (char*)ptr + pcnt;
1784 } else if(!(file->_flag & MSVCRT__IOREAD )) {
1785 if(file->_flag & MSVCRT__IORW) {
1786 file->_flag |= MSVCRT__IOREAD;
1787 } else
1788 return 0;
1790 if(rcnt) pread = _read(file->_file,ptr, rcnt);
1791 if (pread <= 0)
1792 pread = 0;
1793 read+=pread;
1794 return read / size;
1797 /*********************************************************************
1798 * freopen (MSVCRT.@)
1801 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
1803 MSVCRT_FILE* newfile;
1804 int fd;
1806 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1807 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
1808 return NULL;
1810 if (fd > 2)
1812 #if 0
1813 FIXME(":reopen on user file not implemented!\n");
1814 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1815 return NULL;
1816 #endif
1817 if(MSVCRT_fclose(file))
1818 return NULL;
1819 return MSVCRT_fopen(path, mode);
1822 /* first, create the new file */
1823 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
1824 return NULL;
1826 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1827 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1828 MSVCRT_handles[newfile->_file]))
1830 /* Redirecting std handle to file , copy over.. */
1831 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
1832 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
1833 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
1834 MSVCRT__iob[fd]._file = fd;
1835 /* And free up the resources allocated by fopen, but
1836 * not the HANDLE we copied. */
1837 MSVCRT_free(MSVCRT_files[fd]);
1838 msvcrt_free_fd(newfile->_file);
1839 return &MSVCRT__iob[fd];
1842 WARN(":failed-last error (%ld)\n",GetLastError());
1843 MSVCRT_fclose(newfile);
1844 MSVCRT__set_errno(GetLastError());
1845 return NULL;
1848 /*********************************************************************
1849 * fsetpos (MSVCRT.@)
1851 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1853 return _lseek(file->_file,*pos,SEEK_SET);
1856 /* helper function for fscanf. Returns the value of character c in the
1857 * given base, or -1 if the given character is not a digit of the base.
1859 static int char2digit(char c, int base) {
1860 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
1861 if (base<=10) return -1;
1862 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
1863 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
1864 return -1;
1867 /*********************************************************************
1868 * fscanf (MSVCRT.@)
1869 * Implemented based on
1870 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
1871 * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
1872 * more types of format spec.
1874 int MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
1876 /* NOTE: If you extend this function, extend MSVCRT__cscanf in console.c too */
1877 int rd = 0;
1878 int nch;
1879 va_list ap;
1880 if (!*format) return 0;
1881 WARN("%p (\"%s\"): semi-stub\n", file, format);
1882 nch = MSVCRT_fgetc(file);
1883 va_start(ap, format);
1884 while (*format) {
1885 /* a whitespace character in the format string causes scanf to read,
1886 * but not store, all consecutive white-space characters in the input
1887 * up to the next non-white-space character. One white space character
1888 * in the input matches any number (including zero) and combination of
1889 * white-space characters in the input. */
1890 if (isspace(*format)) {
1891 /* skip whitespace */
1892 while ((nch!=MSVCRT_EOF) && isspace(nch))
1893 nch = MSVCRT_fgetc(file);
1895 /* a format specification causes scanf to read and convert characters
1896 * in the input into values of a specified type. The value is assigned
1897 * to an argument in the argument list. Format specifications have
1898 * the form %[*][width][{h | l | I64 | L}]type */
1899 /* FIXME: unimplemented: h/l/I64/L modifiers and some type specs. */
1900 else if (*format == '%') {
1901 int st = 0; int suppress = 0; int width = 0;
1902 int base, number_signed;
1903 format++;
1904 /* look for leading asterisk, which means 'suppress assignment of
1905 * this field'. */
1906 if (*format=='*') {
1907 format++;
1908 suppress=1;
1910 /* look for width specification */
1911 while (isdigit(*format)) {
1912 width*=10;
1913 width+=*format++ - '0';
1915 if (width==0) width=-1; /* no width spec seen */
1916 switch(*format) {
1917 case '%': /* read a percent symbol */
1918 if (nch!='%') break;
1919 nch = MSVCRT_fgetc(file);
1920 break;
1921 case 'x':
1922 case 'X': /* hexadecimal integer. */
1923 base = 16; number_signed = 0;
1924 goto number;
1925 case 'o': /* octal integer */
1926 base = 8; number_signed = 0;
1927 goto number;
1928 case 'u': /* unsigned decimal integer */
1929 base = 10; number_signed = 0;
1930 goto number;
1931 case 'd': /* signed decimal integer */
1932 base = 10; number_signed = 1;
1933 goto number;
1934 case 'i': /* generic integer */
1935 base = 0; number_signed = 1;
1936 number: {
1937 /* read an integer */
1938 int*val = suppress ? NULL : va_arg(ap, int*);
1939 int cur = 0; int negative = 0; int seendigit=0;
1940 /* skip initial whitespace */
1941 while ((nch!=MSVCRT_EOF) && isspace(nch))
1942 nch = MSVCRT_fgetc(file);
1943 /* get sign */
1944 if (number_signed && (nch == '-' || nch == '+')) {
1945 negative = (nch=='-');
1946 nch = MSVCRT_fgetc(file);
1947 if (width>0) width--;
1949 /* look for leading indication of base */
1950 if (width!=0 && nch == '0') {
1951 nch = MSVCRT_fgetc(file);
1952 if (width>0) width--;
1953 seendigit=1;
1954 if (width!=0 && (nch=='x' || nch=='X')) {
1955 if (base==0)
1956 base=16;
1957 if (base==16) {
1958 nch = MSVCRT_fgetc(file);
1959 if (width>0) width--;
1960 seendigit=0;
1962 } else if (base==0)
1963 base = 8;
1965 if (base==0)
1966 base=10;
1967 /* throw away leading zeros */
1968 while (width!=0 && nch=='0') {
1969 nch = MSVCRT_fgetc(file);
1970 if (width>0) width--;
1971 seendigit=1;
1973 /* get first digit. Keep working copy negative, as the
1974 * range of negative numbers in two's complement notation
1975 * is one larger than the range of positive numbers. */
1976 if (width!=0 && char2digit(nch, base)!=-1) {
1977 cur = -char2digit(nch, base);
1978 nch = MSVCRT_fgetc(file);
1979 if (width>0) width--;
1980 seendigit=1;
1982 /* read until no more digits */
1983 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
1984 cur = cur*base + char2digit(nch, base);
1985 nch = MSVCRT_fgetc(file);
1986 if (width>0) width--;
1987 seendigit=1;
1989 /* negate parsed number if non-negative */
1990 if (!negative) cur=-cur;
1991 /* okay, done! */
1992 if (!seendigit) break; /* not a valid number */
1993 st = 1;
1994 if (!suppress) *val = cur;
1996 break;
1997 case 'e':
1998 case 'E':
1999 case 'f':
2000 case 'g':
2001 case 'G': { /* read a float */
2002 float*val = suppress ? NULL : va_arg(ap, float*);
2003 float cur = 0;
2004 int negative = 0;
2005 /* skip initial whitespace */
2006 while ((nch!=MSVCRT_EOF) && isspace(nch))
2007 nch = MSVCRT_fgetc(file);
2008 /* get sign. */
2009 if (nch == '-' || nch == '+') {
2010 negative = (nch=='-');
2011 if (width>0) width--;
2012 if (width==0) break;
2013 nch = MSVCRT_fgetc(file);
2015 /* get first digit. */
2016 if (!isdigit(nch)) break;
2017 cur = (nch - '0') * (negative ? -1 : 1);
2018 nch = MSVCRT_fgetc(file);
2019 if (width>0) width--;
2020 /* read until no more digits */
2021 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
2022 cur = cur*10 + (nch - '0');
2023 nch = MSVCRT_fgetc(file);
2024 if (width>0) width--;
2026 /* handle decimals */
2027 if (width!=0 && nch == '.') {
2028 float dec = 1;
2029 nch = MSVCRT_fgetc(file);
2030 if (width>0) width--;
2031 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
2032 dec /= 10;
2033 cur += dec * (nch - '0');
2034 nch = MSVCRT_fgetc(file);
2035 if (width>0) width--;
2038 /* handle exponent */
2039 if (width!=0 && (nch == 'e' || nch == 'E')) {
2040 int exponent = 0, negexp = 0;
2041 float expcnt;
2042 nch = MSVCRT_fgetc(file);
2043 if (width>0) width--;
2044 /* possible sign on the exponent */
2045 if (width!=0 && (nch=='+' || nch=='-')) {
2046 negexp = (nch=='-');
2047 nch = MSVCRT_fgetc(file);
2048 if (width>0) width--;
2050 /* exponent digits */
2051 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
2052 exponent *= 10;
2053 exponent += (nch - '0');
2054 nch = MSVCRT_fgetc(file);
2055 if (width>0) width--;
2057 /* update 'cur' with this exponent. */
2058 expcnt = negexp ? .1 : 10;
2059 while (exponent!=0) {
2060 if (exponent&1)
2061 cur*=expcnt;
2062 exponent/=2;
2063 expcnt=expcnt*expcnt;
2066 st = 1;
2067 if (!suppress) *val = cur;
2069 break;
2070 case 's': { /* read a word */
2071 char*str = suppress ? NULL : va_arg(ap, char*);
2072 char*sptr = str;
2073 /* skip initial whitespace */
2074 while ((nch!=MSVCRT_EOF) && isspace(nch))
2075 nch = MSVCRT_fgetc(file);
2076 /* read until whitespace */
2077 while (width!=0 && (nch!=MSVCRT_EOF) && !isspace(nch)) {
2078 if (!suppress) *sptr++ = nch;
2079 st++;
2080 nch = MSVCRT_fgetc(file);
2081 if (width>0) width--;
2083 /* terminate */
2084 if (!suppress) *sptr = 0;
2085 TRACE("read word: %s\n", str);
2087 break;
2088 default: FIXME("unhandled: %%%c\n", *format);
2089 /* From spec: "if a percent sign is followed by a character
2090 * that has no meaning as a format-control character, that
2091 * character and the following characters are treated as
2092 * an ordinary sequence of characters, that is, a sequence
2093 * of characters that must match the input. For example,
2094 * to specify that a percent-sign character is to be input,
2095 * use %%."
2096 * LEAVING AS-IS because we catch bugs better that way. */
2098 if (st && !suppress) rd++;
2099 else break;
2101 /* a non-white-space character causes scanf to read, but not store,
2102 * a matching non-white-space character. */
2103 else {
2104 /* check for character match */
2105 if (nch == *format)
2106 nch = MSVCRT_fgetc(file);
2107 else break;
2109 format++;
2111 if (nch!=MSVCRT_EOF) {
2112 FIXME("need ungetch\n");
2114 va_end(ap);
2115 TRACE("returning %d\n", rd);
2116 return rd;
2119 /*********************************************************************
2120 * fseek (MSVCRT.@)
2122 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
2124 /* Flush output if needed */
2125 if(file->_flag & MSVCRT__IOWRT)
2126 msvcrt_flush_buffer(file);
2128 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
2129 offset -= file->_cnt;
2131 /* Discard buffered input */
2132 file->_cnt = 0;
2133 file->_ptr = file->_base;
2134 /* Reset direction of i/o */
2135 if(file->_flag & MSVCRT__IORW) {
2136 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2138 return _lseek(file->_file,offset,whence);
2141 /*********************************************************************
2142 * ftell (MSVCRT.@)
2144 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2146 int off=0;
2147 long pos;
2148 if(file->_bufsiz) {
2149 if( file->_flag & MSVCRT__IOWRT ) {
2150 off = file->_ptr - file->_base;
2151 } else {
2152 off = -file->_cnt;
2155 pos = _tell(file->_file);
2156 if(pos == -1) return pos;
2157 return off + pos;
2160 /*********************************************************************
2161 * fwrite (MSVCRT.@)
2163 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2165 MSVCRT_size_t wrcnt=size * nmemb;
2166 int written = 0;
2167 if (size == 0)
2168 return 0;
2169 if(file->_cnt) {
2170 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2171 memcpy(file->_ptr, ptr, pcnt);
2172 file->_cnt -= pcnt;
2173 file->_ptr += pcnt;
2174 written = pcnt;
2175 wrcnt -= pcnt;
2176 ptr = (char*)ptr + pcnt;
2177 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2178 if(file->_flag & MSVCRT__IORW) {
2179 file->_flag |= MSVCRT__IOWRT;
2180 } else
2181 return 0;
2183 if(wrcnt) {
2184 /* Flush buffer */
2185 int res=msvcrt_flush_buffer(file);
2186 if(!res) {
2187 int pwritten = _write(file->_file, ptr, wrcnt);
2188 if (pwritten <= 0) pwritten=0;
2189 written += pwritten;
2192 return written / size;
2195 /*********************************************************************
2196 * fputs (MSVCRT.@)
2198 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2200 size_t len = strlen(s);
2201 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2204 /*********************************************************************
2205 * fputws (MSVCRT.@)
2207 int MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
2209 size_t len = strlenW(s);
2210 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2213 /*********************************************************************
2214 * getchar (MSVCRT.@)
2216 int MSVCRT_getchar(void)
2218 return MSVCRT_fgetc(MSVCRT_stdin);
2221 /*********************************************************************
2222 * getc (MSVCRT.@)
2224 int MSVCRT_getc(MSVCRT_FILE* file)
2226 return MSVCRT_fgetc(file);
2229 /*********************************************************************
2230 * gets (MSVCRT.@)
2232 char *MSVCRT_gets(char *buf)
2234 int cc;
2235 char * buf_start = buf;
2237 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2238 cc = MSVCRT_fgetc(MSVCRT_stdin))
2239 if(cc != '\r') *buf++ = (char)cc;
2241 *buf = '\0';
2243 TRACE("got '%s'\n", buf_start);
2244 return buf_start;
2247 /*********************************************************************
2248 * _getws (MSVCRT.@)
2250 WCHAR* MSVCRT__getws(WCHAR* buf)
2252 MSVCRT_wint_t cc;
2253 WCHAR* ws = buf;
2255 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2256 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2258 if (cc != '\r')
2259 *buf++ = (WCHAR)cc;
2261 *buf = '\0';
2263 TRACE("got '%s'\n", debugstr_w(ws));
2264 return ws;
2267 /*********************************************************************
2268 * putc (MSVCRT.@)
2270 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2272 return MSVCRT_fputc(c, file);
2275 /*********************************************************************
2276 * putchar (MSVCRT.@)
2278 int MSVCRT_putchar(int c)
2280 return MSVCRT_fputc(c, MSVCRT_stdout);
2283 /*********************************************************************
2284 * puts (MSVCRT.@)
2286 int MSVCRT_puts(const char *s)
2288 size_t len = strlen(s);
2289 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2290 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2293 /*********************************************************************
2294 * _putws (MSVCRT.@)
2296 int _putws(const WCHAR *s)
2298 static const WCHAR nl = '\n';
2299 size_t len = strlenW(s);
2300 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2301 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2304 /*********************************************************************
2305 * remove (MSVCRT.@)
2307 int MSVCRT_remove(const char *path)
2309 TRACE("(%s)\n",path);
2310 if (DeleteFileA(path))
2311 return 0;
2312 TRACE(":failed (%ld)\n",GetLastError());
2313 MSVCRT__set_errno(GetLastError());
2314 return -1;
2317 /*********************************************************************
2318 * _wremove (MSVCRT.@)
2320 int _wremove(const WCHAR *path)
2322 TRACE("(%s)\n",debugstr_w(path));
2323 if (DeleteFileW(path))
2324 return 0;
2325 TRACE(":failed (%ld)\n",GetLastError());
2326 MSVCRT__set_errno(GetLastError());
2327 return -1;
2330 /*********************************************************************
2331 * scanf (MSVCRT.@)
2333 int MSVCRT_scanf(const char *format, ...)
2335 va_list valist;
2336 int res;
2338 va_start(valist, format);
2339 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
2340 va_end(valist);
2341 return res;
2344 /*********************************************************************
2345 * rename (MSVCRT.@)
2347 int MSVCRT_rename(const char *oldpath,const char *newpath)
2349 TRACE(":from %s to %s\n",oldpath,newpath);
2350 if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
2351 return 0;
2352 TRACE(":failed (%ld)\n",GetLastError());
2353 MSVCRT__set_errno(GetLastError());
2354 return -1;
2357 /*********************************************************************
2358 * _wrename (MSVCRT.@)
2360 int _wrename(const WCHAR *oldpath,const WCHAR *newpath)
2362 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2363 if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
2364 return 0;
2365 TRACE(":failed (%ld)\n",GetLastError());
2366 MSVCRT__set_errno(GetLastError());
2367 return -1;
2370 /*********************************************************************
2371 * setvbuf (MSVCRT.@)
2373 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2375 /* TODO: Check if file busy */
2376 if(file->_bufsiz) {
2377 MSVCRT_free(file->_base);
2378 file->_bufsiz = 0;
2379 file->_cnt = 0;
2381 if(mode == MSVCRT__IOFBF) {
2382 file->_flag &= ~MSVCRT__IONBF;
2383 file->_base = file->_ptr = buf;
2384 if(buf) {
2385 file->_bufsiz = size;
2387 } else {
2388 file->_flag |= MSVCRT__IONBF;
2390 return 0;
2393 /*********************************************************************
2394 * setbuf (MSVCRT.@)
2396 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2398 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2401 /*********************************************************************
2402 * tmpnam (MSVCRT.@)
2404 char *MSVCRT_tmpnam(char *s)
2406 char tmpbuf[MAX_PATH];
2407 char* prefix = "TMP";
2408 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
2409 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
2411 TRACE(":failed-last error (%ld)\n",GetLastError());
2412 return NULL;
2414 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
2415 s = MSVCRT_tmpname;
2416 return s;
2419 /*********************************************************************
2420 * tmpfile (MSVCRT.@)
2422 MSVCRT_FILE* MSVCRT_tmpfile(void)
2424 char *filename = MSVCRT_tmpnam(NULL);
2425 int fd;
2426 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2427 if (fd != -1)
2428 return msvcrt_alloc_fp(fd);
2429 return NULL;
2432 /*********************************************************************
2433 * vfprintf (MSVCRT.@)
2435 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2437 char buf[2048], *mem = buf;
2438 int written, resize = sizeof(buf), retval;
2439 /* There are two conventions for vsnprintf failing:
2440 * Return -1 if we truncated, or
2441 * Return the number of bytes that would have been written
2442 * The code below handles both cases
2444 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2445 written > resize)
2447 resize = (written == -1 ? resize * 2 : written + 1);
2448 if (mem != buf)
2449 MSVCRT_free (mem);
2450 if (!(mem = (char *)MSVCRT_malloc(resize)))
2451 return MSVCRT_EOF;
2453 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2454 if (mem != buf)
2455 MSVCRT_free (mem);
2456 return retval;
2459 /*********************************************************************
2460 * vfwprintf (MSVCRT.@)
2461 * FIXME:
2462 * Is final char included in written (then resize is too big) or not
2463 * (then we must test for equality too)?
2465 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
2467 WCHAR buf[2048], *mem = buf;
2468 int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
2469 /* See vfprintf comments */
2470 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2471 written > resize)
2473 resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
2474 if (mem != buf)
2475 MSVCRT_free (mem);
2476 if (!(mem = (WCHAR *)MSVCRT_malloc(resize*sizeof(*mem))))
2477 return MSVCRT_EOF;
2479 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2480 if (mem != buf)
2481 MSVCRT_free (mem);
2482 return retval;
2485 /*********************************************************************
2486 * vprintf (MSVCRT.@)
2488 int MSVCRT_vprintf(const char *format, va_list valist)
2490 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2493 /*********************************************************************
2494 * vwprintf (MSVCRT.@)
2496 int MSVCRT_vwprintf(const WCHAR *format, va_list valist)
2498 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2501 /*********************************************************************
2502 * fprintf (MSVCRT.@)
2504 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2506 va_list valist;
2507 int res;
2508 va_start(valist, format);
2509 res = MSVCRT_vfprintf(file, format, valist);
2510 va_end(valist);
2511 return res;
2514 /*********************************************************************
2515 * fwprintf (MSVCRT.@)
2517 int MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
2519 va_list valist;
2520 int res;
2521 va_start(valist, format);
2522 res = MSVCRT_vfwprintf(file, format, valist);
2523 va_end(valist);
2524 return res;
2527 /*********************************************************************
2528 * printf (MSVCRT.@)
2530 int MSVCRT_printf(const char *format, ...)
2532 va_list valist;
2533 int res;
2534 va_start(valist, format);
2535 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2536 va_end(valist);
2537 return res;
2540 /*********************************************************************
2541 * ungetc (MSVCRT.@)
2543 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2545 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2546 msvcrt_alloc_buffer(file);
2547 file->_ptr++;
2549 if(file->_ptr>file->_base) {
2550 file->_ptr--;
2551 *file->_ptr=c;
2552 file->_cnt++;
2553 return c;
2555 return MSVCRT_EOF;
2558 /*********************************************************************
2559 * ungetwc (MSVCRT.@)
2561 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2563 WCHAR mwc = wc;
2564 char * pp = (char *)&mwc;
2565 int i;
2566 for(i=sizeof(WCHAR)-1;i>=0;i--) {
2567 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2568 return MSVCRT_WEOF;
2570 return mwc;
2573 /*********************************************************************
2574 * wprintf (MSVCRT.@)
2576 int MSVCRT_wprintf(const WCHAR *format, ...)
2578 va_list valist;
2579 int res;
2580 va_start(valist, format);
2581 res = MSVCRT_vwprintf(format, valist);
2582 va_end(valist);
2583 return res;