Converted implementation of _stat and _fstat to _stati64 and
[wine/multimedia.git] / dlls / msvcrt / file.c
blob3b165cebab786082f0d3bd3aac8baf62c691be73
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 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
33 #include "winternl.h"
34 #include "msvcrt.h"
35 #include "msvcrt/errno.h"
37 #include "wine/unicode.h"
38 #include "msvcrt/direct.h"
39 #include "msvcrt/fcntl.h"
40 #include "msvcrt/io.h"
41 #include "msvcrt/sys/locking.h"
42 #include "msvcrt/stdio.h"
43 #include "msvcrt/stdlib.h"
44 #include "msvcrt/string.h"
45 #include "msvcrt/sys/stat.h"
46 #include "msvcrt/sys/utime.h"
47 #include "msvcrt/time.h"
48 #include "msvcrt/share.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
54 /* for stat mode, permissions apply to all,owner and group */
55 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
56 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
57 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
59 /* _access() bit flags FIXME: incomplete */
60 #define MSVCRT_W_OK 0x02
63 /* FIXME: Make this dynamic */
64 #define MSVCRT_MAX_FILES 257
66 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
67 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
68 int MSVCRT_flags[MSVCRT_MAX_FILES];
69 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
70 MSVCRT_FILE MSVCRT__iob[3];
71 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
72 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
73 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
75 static int MSVCRT_fdstart = 3; /* first unallocated fd */
76 static int MSVCRT_fdend = 3; /* highest allocated fd */
78 /* INTERNAL: process umask */
79 static int MSVCRT_umask = 0;
81 /* INTERNAL: Static buffer for temp file name */
82 static char MSVCRT_tmpname[MAX_PATH];
84 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
85 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
86 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
87 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
89 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
90 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
91 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
92 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
93 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
95 extern CRITICAL_SECTION MSVCRT_file_cs;
96 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
97 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
99 static void msvcrt_cp_from_stati64(const struct _stati64 *bufi64, struct _stat *buf)
101 buf->st_dev = bufi64->st_dev;
102 buf->st_ino = bufi64->st_ino;
103 buf->st_mode = bufi64->st_mode;
104 buf->st_nlink = bufi64->st_nlink;
105 buf->st_uid = bufi64->st_uid;
106 buf->st_gid = bufi64->st_gid;
107 buf->st_rdev = bufi64->st_rdev;
108 buf->st_size = bufi64->st_size;
109 buf->st_atime = bufi64->st_atime;
110 buf->st_mtime = bufi64->st_mtime;
111 buf->st_ctime = bufi64->st_ctime;
114 /* INTERNAL: Get the HANDLE for a fd */
115 static HANDLE msvcrt_fdtoh(int fd)
117 if (fd < 0 || fd >= MSVCRT_fdend ||
118 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
120 WARN(":fd (%d) - no handle!\n",fd);
121 *__doserrno() = 0;
122 *MSVCRT__errno() = MSVCRT_EBADF;
123 return INVALID_HANDLE_VALUE;
125 return MSVCRT_handles[fd];
128 /* INTERNAL: free a file entry fd */
129 static void msvcrt_free_fd(int fd)
131 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
132 MSVCRT_files[fd] = 0;
133 MSVCRT_flags[fd] = 0;
134 TRACE(":fd (%d) freed\n",fd);
135 if (fd < 3)
136 return; /* dont use 0,1,2 for user files */
137 if (fd == MSVCRT_fdend - 1)
138 MSVCRT_fdend--;
139 if (fd < MSVCRT_fdstart)
140 MSVCRT_fdstart = fd;
143 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
144 static int msvcrt_alloc_fd(HANDLE hand, int flag)
146 int fd = MSVCRT_fdstart;
148 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
149 if (fd >= MSVCRT_MAX_FILES)
151 WARN(":files exhausted!\n");
152 return -1;
154 MSVCRT_handles[fd] = hand;
155 MSVCRT_flags[fd] = flag;
157 /* locate next free slot */
158 if (fd == MSVCRT_fdend)
159 MSVCRT_fdstart = ++MSVCRT_fdend;
160 else
161 while(MSVCRT_fdstart < MSVCRT_fdend &&
162 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
163 MSVCRT_fdstart++;
165 return fd;
168 /* INTERNAL: Allocate a FILE* for an fd slot
169 * This is done lazily to avoid memory wastage for low level open/write
170 * usage when a FILE* is not requested (but may be later).
172 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
174 TRACE(":fd (%d) allocating FILE*\n",fd);
175 if (fd < 0 || fd >= MSVCRT_fdend ||
176 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
178 WARN(":invalid fd %d\n",fd);
179 *__doserrno() = 0;
180 *MSVCRT__errno() = MSVCRT_EBADF;
181 return NULL;
183 if (!MSVCRT_files[fd])
185 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
187 MSVCRT_files[fd]->_file = fd;
188 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
189 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
192 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
193 return MSVCRT_files[fd];
197 /* INTERNAL: Set up stdin, stderr and stdout */
198 void msvcrt_init_io(void)
200 int i;
201 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
202 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
203 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
204 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
205 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
206 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
207 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
209 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles[0],
210 MSVCRT_handles[1],MSVCRT_handles[2]);
212 for (i = 0; i < 3; i++)
214 /* FILE structs for stdin/out/err are static and never deleted */
215 MSVCRT_files[i] = &MSVCRT__iob[i];
216 MSVCRT__iob[i]._file = i;
217 MSVCRT_tempfiles[i] = NULL;
221 /* free everything on process exit */
222 void msvcrt_free_io(void)
224 _fcloseall();
225 _close(0);
226 _close(1);
227 _close(2);
230 /* INTERNAL: Flush stdio file buffer */
231 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
233 if(file->_bufsiz) {
234 int cnt=file->_ptr-file->_base;
235 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
236 return MSVCRT_EOF;
238 file->_ptr=file->_base;
239 file->_cnt=file->_bufsiz;
241 return 0;
244 /* INTERNAL: Allocate stdio file buffer */
245 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
247 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
248 if(file->_base) {
249 file->_bufsiz = MSVCRT_BUFSIZ;
250 file->_flag |= MSVCRT__IOMYBUF;
251 } else {
252 file->_base = (unsigned char *)(&file->_charbuf);
253 /* put here 2 ??? */
254 file->_bufsiz = sizeof(file->_charbuf);
256 file->_ptr = file->_base;
257 file->_cnt = 0;
260 /*********************************************************************
261 * __p__iob(MSVCRT.@)
263 MSVCRT_FILE *__p__iob(void)
265 return &MSVCRT__iob[0];
268 /*********************************************************************
269 * _access (MSVCRT.@)
271 int _access(const char *filename, int mode)
273 DWORD attr = GetFileAttributesA(filename);
275 TRACE("(%s,%d) %ld\n",filename,mode,attr);
277 if (!filename || attr == 0xffffffff)
279 MSVCRT__set_errno(GetLastError());
280 return -1;
282 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
284 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
285 return -1;
287 return 0;
290 /*********************************************************************
291 * _waccess (MSVCRT.@)
293 int _waccess(const WCHAR *filename, int mode)
295 DWORD attr = GetFileAttributesW(filename);
297 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
299 if (!filename || attr == 0xffffffff)
301 MSVCRT__set_errno(GetLastError());
302 return -1;
304 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
306 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
307 return -1;
309 return 0;
312 /*********************************************************************
313 * _chmod (MSVCRT.@)
315 int _chmod(const char *path, int flags)
317 DWORD oldFlags = GetFileAttributesA(path);
319 if (oldFlags != 0x0FFFFFFFF)
321 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
322 oldFlags | FILE_ATTRIBUTE_READONLY;
324 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
325 return 0;
327 MSVCRT__set_errno(GetLastError());
328 return -1;
331 /*********************************************************************
332 * _wchmod (MSVCRT.@)
334 int _wchmod(const WCHAR *path, int flags)
336 DWORD oldFlags = GetFileAttributesW(path);
338 if (oldFlags != 0x0FFFFFFFF)
340 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
341 oldFlags | FILE_ATTRIBUTE_READONLY;
343 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
344 return 0;
346 MSVCRT__set_errno(GetLastError());
347 return -1;
350 /*********************************************************************
351 * _unlink (MSVCRT.@)
353 int _unlink(const char *path)
355 TRACE("(%s)\n",path);
356 if(DeleteFileA(path))
357 return 0;
358 TRACE("failed (%ld)\n",GetLastError());
359 MSVCRT__set_errno(GetLastError());
360 return -1;
363 /*********************************************************************
364 * _wunlink (MSVCRT.@)
366 int _wunlink(const WCHAR *path)
368 TRACE("(%s)\n",debugstr_w(path));
369 if(DeleteFileW(path))
370 return 0;
371 TRACE("failed (%ld)\n",GetLastError());
372 MSVCRT__set_errno(GetLastError());
373 return -1;
376 /*********************************************************************
377 * _close (MSVCRT.@)
379 int _close(int fd)
381 HANDLE hand = msvcrt_fdtoh(fd);
383 TRACE(":fd (%d) handle (%p)\n",fd,hand);
384 if (hand == INVALID_HANDLE_VALUE)
385 return -1;
386 /* flush stdio buffers */
387 if(MSVCRT_files[fd]) {
388 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
389 MSVCRT_fflush(MSVCRT_files[fd]);
391 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
392 MSVCRT_free(MSVCRT_files[fd]->_base);
395 /* Dont free std FILE*'s, they are not dynamic */
396 if (fd > 2 && MSVCRT_files[fd])
397 MSVCRT_free(MSVCRT_files[fd]);
399 msvcrt_free_fd(fd);
401 if (!CloseHandle(hand))
403 WARN(":failed-last error (%ld)\n",GetLastError());
404 MSVCRT__set_errno(GetLastError());
405 return -1;
407 if (MSVCRT_tempfiles[fd])
409 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
410 _unlink(MSVCRT_tempfiles[fd]);
411 MSVCRT_free(MSVCRT_tempfiles[fd]);
412 MSVCRT_tempfiles[fd] = NULL;
415 TRACE(":ok\n");
416 return 0;
419 /*********************************************************************
420 * _commit (MSVCRT.@)
422 int _commit(int fd)
424 HANDLE hand = msvcrt_fdtoh(fd);
426 TRACE(":fd (%d) handle (%p)\n",fd,hand);
427 if (hand == INVALID_HANDLE_VALUE)
428 return -1;
430 if (!FlushFileBuffers(hand))
432 if (GetLastError() == ERROR_INVALID_HANDLE)
434 /* FlushFileBuffers fails for console handles
435 * so we ignore this error.
437 return 0;
439 TRACE(":failed-last error (%ld)\n",GetLastError());
440 MSVCRT__set_errno(GetLastError());
441 return -1;
443 TRACE(":ok\n");
444 return 0;
447 /*********************************************************************
448 * _eof (MSVCRT.@)
450 int _eof(int fd)
452 DWORD curpos,endpos;
453 HANDLE hand = msvcrt_fdtoh(fd);
455 TRACE(":fd (%d) handle (%p)\n",fd,hand);
457 if (hand == INVALID_HANDLE_VALUE)
458 return -1;
460 /* If we have a FILE* for this file, the EOF flag
461 * will be set by the read()/write() functions.
463 if (MSVCRT_files[fd])
464 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
466 /* Otherwise we do it the hard way */
467 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
468 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
470 if (curpos == endpos)
471 return TRUE;
473 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
474 return FALSE;
477 /*********************************************************************
478 * _fcloseall (MSVCRT.@)
480 int _fcloseall(void)
482 int num_closed = 0, i;
484 for (i = 3; i < MSVCRT_fdend; i++)
485 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
487 _close(i);
488 num_closed++;
491 TRACE(":closed (%d) handles\n",num_closed);
492 return num_closed;
495 /*********************************************************************
496 * _lseek (MSVCRT.@)
498 LONG _lseek(int fd, LONG offset, int whence)
500 DWORD ret;
501 HANDLE hand = msvcrt_fdtoh(fd);
503 TRACE(":fd (%d) handle (%p)\n",fd,hand);
504 if (hand == INVALID_HANDLE_VALUE)
505 return -1;
507 if (whence < 0 || whence > 2)
509 *MSVCRT__errno() = MSVCRT_EINVAL;
510 return -1;
513 TRACE(":fd (%d) to 0x%08lx pos %s\n",
514 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
515 (whence==SEEK_CUR)?"SEEK_CUR":
516 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
518 if ((ret = SetFilePointer(hand, offset, NULL, whence)) != 0xffffffff)
520 if (MSVCRT_files[fd])
521 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
522 /* FIXME: What if we seek _to_ EOF - is EOF set? */
523 return ret;
525 TRACE(":error-last error (%ld)\n",GetLastError());
526 if (MSVCRT_files[fd])
527 switch(GetLastError())
529 case ERROR_NEGATIVE_SEEK:
530 case ERROR_SEEK_ON_DEVICE:
531 MSVCRT__set_errno(GetLastError());
532 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
533 break;
534 default:
535 break;
537 return -1;
540 /*********************************************************************
541 * _locking (MSVCRT.@)
543 * This is untested; the underlying LockFile doesn't work yet.
545 int _locking(int fd, int mode, LONG nbytes)
547 BOOL ret;
548 DWORD cur_locn;
549 HANDLE hand = msvcrt_fdtoh(fd);
551 TRACE(":fd (%d) handle (%p)\n",fd,hand);
552 if (hand == INVALID_HANDLE_VALUE)
553 return -1;
555 if (mode < 0 || mode > 4)
557 *MSVCRT__errno() = MSVCRT_EINVAL;
558 return -1;
561 TRACE(":fd (%d) by 0x%08lx mode %s\n",
562 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
563 (mode==_LK_LOCK)?"_LK_LOCK":
564 (mode==_LK_NBLCK)?"_LK_NBLCK":
565 (mode==_LK_RLCK)?"_LK_RLCK":
566 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
567 "UNKNOWN");
569 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == 0xffffffff)
571 FIXME ("Seek failed\n");
572 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
573 return -1;
575 if (mode == _LK_LOCK || mode == _LK_RLCK)
577 int nretry = 10;
578 ret = 1; /* just to satisfy gcc */
579 while (nretry--)
581 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
582 if (ret) break;
583 sleep (1);
586 else if (mode == _LK_UNLCK)
587 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
588 else
589 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
590 /* FIXME - what about error settings? */
591 return ret ? 0 : -1;
594 /*********************************************************************
595 * rewind (MSVCRT.@)
597 void MSVCRT_rewind(MSVCRT_FILE* file)
599 TRACE(":file (%p) fd (%d)\n",file,file->_file);
600 MSVCRT_fseek(file, 0L, SEEK_SET);
601 MSVCRT_clearerr(file);
604 /*********************************************************************
605 * _fdopen (MSVCRT.@)
607 MSVCRT_FILE* _fdopen(int fd, const char *mode)
609 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
611 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
612 if (file)
613 MSVCRT_rewind(file);
615 return file;
618 /*********************************************************************
619 * _wfdopen (MSVCRT.@)
621 MSVCRT_FILE* _wfdopen(int fd, const WCHAR *mode)
623 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
625 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
626 if (file)
627 MSVCRT_rewind(file);
629 return file;
632 /*********************************************************************
633 * _filelength (MSVCRT.@)
635 LONG _filelength(int fd)
637 LONG curPos = _lseek(fd, 0, SEEK_CUR);
638 if (curPos != -1)
640 LONG endPos = _lseek(fd, 0, SEEK_END);
641 if (endPos != -1)
643 if (endPos != curPos)
644 _lseek(fd, curPos, SEEK_SET);
645 return endPos;
648 return -1;
651 /*********************************************************************
652 * _fileno (MSVCRT.@)
654 int _fileno(MSVCRT_FILE* file)
656 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
657 return file->_file;
660 /*********************************************************************
661 * _flushall (MSVCRT.@)
663 int _flushall(void)
665 int num_flushed = 0, i = 3;
667 while(i < MSVCRT_fdend)
668 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
670 #if 0
671 /* FIXME: flush, do not commit */
672 if (_commit(i) == -1)
673 if (MSVCRT_files[i])
674 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
675 #endif
676 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
677 MSVCRT_fflush(MSVCRT_files[i]);
678 num_flushed++;
682 TRACE(":flushed (%d) handles\n",num_flushed);
683 return num_flushed;
686 /*********************************************************************
687 * _fstati64 (MSVCRT.@)
689 int _fstati64(int fd, struct _stati64* buf)
691 DWORD dw;
692 BY_HANDLE_FILE_INFORMATION hfi;
693 HANDLE hand = msvcrt_fdtoh(fd);
695 TRACE(":fd (%d) stat (%p)\n",fd,buf);
696 if (hand == INVALID_HANDLE_VALUE)
697 return -1;
699 if (!buf)
701 WARN(":failed-NULL buf\n");
702 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
703 return -1;
706 memset(&hfi, 0, sizeof(hfi));
707 memset(buf, 0, sizeof(struct _stati64));
708 if (!GetFileInformationByHandle(hand, &hfi))
710 WARN(":failed-last error (%ld)\n",GetLastError());
711 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
712 return -1;
714 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
715 buf->st_nlink = hfi.nNumberOfLinks;
716 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
717 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
718 buf->st_atime = dw;
719 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
720 buf->st_mtime = buf->st_ctime = dw;
721 return 0;
724 /*********************************************************************
725 * _fstat (MSVCRT.@)
727 int MSVCRT__fstat(int fd, struct _stat* buf)
728 { int ret;
729 struct _stati64 bufi64;
731 ret = _fstati64(fd, &bufi64);
732 if (!ret)
733 msvcrt_cp_from_stati64(&bufi64, buf);
734 return ret;
737 /*********************************************************************
738 * _futime (MSVCRT.@)
740 int _futime(int fd, struct _utimbuf *t)
742 HANDLE hand = msvcrt_fdtoh(fd);
743 FILETIME at, wt;
745 if (!t)
747 MSVCRT_time_t currTime;
748 MSVCRT_time(&currTime);
749 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
750 memcpy(&wt, &at, sizeof(wt));
752 else
754 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
755 if (t->actime == t->modtime)
756 memcpy(&wt, &at, sizeof(wt));
757 else
758 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
761 if (!SetFileTime(hand, NULL, &at, &wt))
763 MSVCRT__set_errno(GetLastError());
764 return -1 ;
766 return 0;
769 /*********************************************************************
770 * _get_osfhandle (MSVCRT.@)
772 long _get_osfhandle(int fd)
774 HANDLE hand = msvcrt_fdtoh(fd);
775 HANDLE newhand = hand;
776 TRACE(":fd (%d) handle (%p)\n",fd,hand);
778 if (hand != INVALID_HANDLE_VALUE)
780 /* FIXME: I'm not convinced that I should be copying the
781 * handle here - it may be leaked if the app doesn't
782 * close it (and the API docs dont say that it should)
783 * Not duplicating it means that it can't be inherited
784 * and so lcc's wedit doesn't cope when it passes it to
785 * child processes. I've an idea that it should either
786 * be copied by CreateProcess, or marked as inheritable
787 * when initialised, or maybe both? JG 21-9-00.
789 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
790 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
792 return (long)newhand;
795 /*********************************************************************
796 * _isatty (MSVCRT.@)
798 int _isatty(int fd)
800 HANDLE hand = msvcrt_fdtoh(fd);
802 TRACE(":fd (%d) handle (%p)\n",fd,hand);
803 if (hand == INVALID_HANDLE_VALUE)
804 return 0;
806 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
809 /*********************************************************************
810 * _mktemp (MSVCRT.@)
812 char *_mktemp(char *pattern)
814 int numX = 0;
815 char *retVal = pattern;
816 int id;
817 char letter = 'a';
819 while(*pattern)
820 numX = (*pattern++ == 'X')? numX + 1 : 0;
821 if (numX < 5)
822 return NULL;
823 pattern--;
824 id = GetCurrentProcessId();
825 numX = 6;
826 while(numX--)
828 int tempNum = id / 10;
829 *pattern-- = id - (tempNum * 10) + '0';
830 id = tempNum;
832 pattern++;
835 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
836 GetLastError() == ERROR_FILE_NOT_FOUND)
837 return retVal;
838 *pattern = letter++;
839 } while(letter != '|');
840 return NULL;
843 /*********************************************************************
844 * _wmktemp (MSVCRT.@)
846 WCHAR *_wmktemp(WCHAR *pattern)
848 int numX = 0;
849 WCHAR *retVal = pattern;
850 int id;
851 WCHAR letter = (WCHAR)L'a';
853 while(*pattern)
854 numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
855 if (numX < 5)
856 return NULL;
857 pattern--;
858 id = GetCurrentProcessId();
859 numX = 6;
860 while(numX--)
862 int tempNum = id / 10;
863 *pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
864 id = tempNum;
866 pattern++;
869 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
870 GetLastError() == ERROR_FILE_NOT_FOUND)
871 return retVal;
872 *pattern = letter++;
873 } while(letter != (WCHAR)L'|');
874 return NULL;
877 /*********************************************************************
878 * _sopen (MSVCRT.@)
880 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
882 va_list ap;
883 int pmode;
884 DWORD access = 0, creation = 0;
885 DWORD sharing;
886 int ioflag = 0, fd;
887 HANDLE hand;
888 SECURITY_ATTRIBUTES sa;
891 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
892 path, oflags, shflags);
894 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
896 case _O_RDONLY:
897 access |= GENERIC_READ;
898 ioflag |= MSVCRT__IOREAD;
899 break;
900 case _O_WRONLY:
901 access |= GENERIC_WRITE;
902 ioflag |= MSVCRT__IOWRT;
903 break;
904 case _O_RDWR:
905 access |= GENERIC_WRITE | GENERIC_READ;
906 ioflag |= MSVCRT__IORW;
907 break;
910 if (oflags & _O_CREAT)
912 va_start(ap, shflags);
913 pmode = va_arg(ap, int);
914 va_end(ap);
916 FIXME(": pmode 0x%04x ignored\n", pmode);
918 if (oflags & _O_EXCL)
919 creation = CREATE_NEW;
920 else if (oflags & _O_TRUNC)
921 creation = CREATE_ALWAYS;
922 else
923 creation = OPEN_ALWAYS;
925 else /* no _O_CREAT */
927 if (oflags & _O_TRUNC)
928 creation = TRUNCATE_EXISTING;
929 else
930 creation = OPEN_EXISTING;
932 if (oflags & _O_APPEND)
933 ioflag |= MSVCRT__IOAPPEND;
936 oflags |= _O_BINARY; /* FIXME: Default to text */
938 if (oflags & _O_TEXT)
940 /* Dont warn when writing */
941 if (ioflag & GENERIC_READ)
942 FIXME(":TEXT node not implemented\n");
943 oflags &= ~_O_TEXT;
946 switch( shflags )
948 case _SH_DENYRW:
949 sharing = 0L;
950 break;
951 case _SH_DENYWR:
952 sharing = FILE_SHARE_READ;
953 break;
954 case _SH_DENYRD:
955 sharing = FILE_SHARE_WRITE;
956 break;
957 case _SH_DENYNO:
958 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
959 break;
960 default:
961 ERR( "Unhandled shflags 0x%x\n", shflags );
962 return -1;
965 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
966 |_O_CREAT|_O_RDWR|_O_TEMPORARY|_O_NOINHERIT))
967 TRACE(":unsupported oflags 0x%04x\n",oflags);
969 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
970 sa.lpSecurityDescriptor = NULL;
971 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
973 hand = CreateFileA(path, access, sharing,
974 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
976 if (hand == INVALID_HANDLE_VALUE) {
977 WARN(":failed-last error (%ld)\n",GetLastError());
978 MSVCRT__set_errno(GetLastError());
979 return -1;
982 fd = msvcrt_alloc_fd(hand, ioflag);
984 TRACE(":fd (%d) handle (%p)\n",fd, hand);
986 if (fd > 0)
988 if (oflags & _O_TEMPORARY)
989 MSVCRT_tempfiles[fd] = _strdup(path);
990 if (ioflag & MSVCRT__IOAPPEND)
991 _lseek(fd, 0, FILE_END);
994 return fd;
997 /*********************************************************************
998 * _wsopen (MSVCRT.@)
1000 int MSVCRT__wsopen( const WCHAR* path, int oflags, int shflags, ... )
1002 const unsigned int len = strlenW(path);
1003 char *patha = MSVCRT_calloc(len + 1,1);
1004 va_list ap;
1005 int pmode;
1007 va_start(ap, shflags);
1008 pmode = va_arg(ap, int);
1009 va_end(ap);
1011 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1013 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1014 MSVCRT_free(patha);
1015 return retval;
1018 MSVCRT__set_errno(GetLastError());
1019 return -1;
1022 /*********************************************************************
1023 * _open (MSVCRT.@)
1025 int _open( const char *path, int flags, ... )
1027 va_list ap;
1028 int pmode;
1030 va_start(ap, flags);
1031 pmode = va_arg(ap, int);
1032 va_end(ap);
1034 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1037 /*********************************************************************
1038 * _wopen (MSVCRT.@)
1040 int _wopen(const WCHAR *path,int flags,...)
1042 const unsigned int len = strlenW(path);
1043 char *patha = MSVCRT_calloc(len + 1,1);
1044 va_list ap;
1045 int pmode;
1047 va_start(ap, flags);
1048 pmode = va_arg(ap, int);
1049 va_end(ap);
1051 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1053 int retval = _open(patha,flags,pmode);
1054 MSVCRT_free(patha);
1055 return retval;
1058 MSVCRT__set_errno(GetLastError());
1059 return -1;
1062 /*********************************************************************
1063 * _creat (MSVCRT.@)
1065 int _creat(const char *path, int flags)
1067 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1068 return _open(path, usedFlags);
1071 /*********************************************************************
1072 * _wcreat (MSVCRT.@)
1074 int _wcreat(const WCHAR *path, int flags)
1076 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1077 return _wopen(path, usedFlags);
1080 /*********************************************************************
1081 * _open_osfhandle (MSVCRT.@)
1083 int _open_osfhandle(long hand, int flags)
1085 /* _O_RDONLY (0) always matches, so set the read flag*/
1086 /* FIXME: handle more flags */
1087 int fd= msvcrt_alloc_fd((HANDLE)hand,flags|MSVCRT__IOREAD);
1088 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd, flags |MSVCRT__IOREAD);
1089 return fd;
1092 /*********************************************************************
1093 * _rmtmp (MSVCRT.@)
1095 int _rmtmp(void)
1097 int num_removed = 0, i;
1099 for (i = 3; i < MSVCRT_fdend; i++)
1100 if (MSVCRT_tempfiles[i])
1102 _close(i);
1103 num_removed++;
1106 if (num_removed)
1107 TRACE(":removed (%d) temp files\n",num_removed);
1108 return num_removed;
1111 /*********************************************************************
1112 * _read (MSVCRT.@)
1114 int _read(int fd, void *buf, unsigned int count)
1116 DWORD num_read;
1117 HANDLE hand = msvcrt_fdtoh(fd);
1119 /* Dont trace small reads, it gets *very* annoying */
1120 if (count > 4)
1121 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1122 if (hand == INVALID_HANDLE_VALUE)
1123 return -1;
1125 if (ReadFile(hand, buf, count, &num_read, NULL))
1127 if (num_read != count && MSVCRT_files[fd])
1129 TRACE(":EOF\n");
1130 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1132 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1135 return num_read;
1137 TRACE(":failed-last error (%ld)\n",GetLastError());
1138 if (MSVCRT_files[fd])
1139 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1140 return -1;
1143 /*********************************************************************
1144 * _getw (MSVCRT.@)
1146 int _getw(MSVCRT_FILE* file)
1148 int i;
1149 if (_read(file->_file, &i, sizeof(int)) != 1)
1150 return MSVCRT_EOF;
1151 return i;
1154 /*********************************************************************
1155 * _setmode (MSVCRT.@)
1157 int _setmode(int fd,int mode)
1159 if (mode & _O_TEXT)
1160 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
1161 return 0;
1164 /*********************************************************************
1165 * _stati64 (MSVCRT.@)
1167 int _stati64(const char* path, struct _stati64 * buf)
1169 DWORD dw;
1170 WIN32_FILE_ATTRIBUTE_DATA hfi;
1171 unsigned short mode = MSVCRT_S_IREAD;
1172 int plen;
1174 TRACE(":file (%s) buf(%p)\n",path,buf);
1176 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1178 TRACE("failed (%ld)\n",GetLastError());
1179 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1180 return -1;
1183 memset(buf,0,sizeof(struct _stati64));
1185 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1186 Bon 011120: This FIXME seems incorrect
1187 Also a letter as first char isn't enough to be classify
1188 as drive letter
1190 if (isalpha(*path)&& (*(path+1)==':'))
1191 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1192 else
1193 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1195 plen = strlen(path);
1197 /* Dir, or regular file? */
1198 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1199 (path[plen-1] == '\\'))
1200 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1201 else
1203 mode |= _S_IFREG;
1204 /* executable? */
1205 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1207 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1208 (tolower(path[plen-3]) << 16);
1209 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1210 mode |= MSVCRT_S_IEXEC;
1214 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1215 mode |= MSVCRT_S_IWRITE;
1217 buf->st_mode = mode;
1218 buf->st_nlink = 1;
1219 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1220 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1221 buf->st_atime = dw;
1222 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1223 buf->st_mtime = buf->st_ctime = dw;
1224 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1225 (long)(buf->st_size >> 32),(long)buf->st_size,
1226 buf->st_atime,buf->st_mtime, buf->st_ctime);
1227 return 0;
1230 /*********************************************************************
1231 * _stat (MSVCRT.@)
1233 int MSVCRT__stat(const char* path, struct _stat * buf)
1234 { int ret;
1235 struct _stati64 bufi64;
1237 ret = _stati64( path, &bufi64);
1238 if (!ret)
1239 msvcrt_cp_from_stati64(&bufi64, buf);
1240 return ret;
1243 /*********************************************************************
1244 * _wstat (MSVCRT.@)
1246 int _wstat(const WCHAR* path, struct _stat * buf)
1248 DWORD dw;
1249 WIN32_FILE_ATTRIBUTE_DATA hfi;
1250 unsigned short mode = MSVCRT_S_IREAD;
1251 int plen;
1253 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1255 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1257 TRACE("failed (%ld)\n",GetLastError());
1258 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1259 return -1;
1262 memset(buf,0,sizeof(struct _stat));
1264 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1265 if (MSVCRT_iswalpha(*path))
1266 buf->st_dev = buf->st_rdev = toupperW(*path - (WCHAR)L'A'); /* drive num */
1267 else
1268 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1270 plen = strlenW(path);
1272 /* Dir, or regular file? */
1273 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1274 (path[plen-1] == (WCHAR)L'\\'))
1275 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1276 else
1278 mode |= _S_IFREG;
1279 /* executable? */
1280 if (plen > 6 && path[plen-4] == (WCHAR)L'.') /* shortest exe: "\x.exe" */
1282 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1283 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1284 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1285 mode |= MSVCRT_S_IEXEC;
1289 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1290 mode |= MSVCRT_S_IWRITE;
1292 buf->st_mode = mode;
1293 buf->st_nlink = 1;
1294 buf->st_size = hfi.nFileSizeLow;
1295 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1296 buf->st_atime = dw;
1297 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1298 buf->st_mtime = buf->st_ctime = dw;
1299 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1300 buf->st_atime,buf->st_mtime, buf->st_ctime);
1301 return 0;
1304 /*********************************************************************
1305 * _tell (MSVCRT.@)
1307 LONG _tell(int fd)
1309 return _lseek(fd, 0, SEEK_CUR);
1312 /*********************************************************************
1313 * _tempnam (MSVCRT.@)
1315 char *_tempnam(const char *dir, const char *prefix)
1317 char tmpbuf[MAX_PATH];
1319 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1320 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1322 TRACE("got name (%s)\n",tmpbuf);
1323 return _strdup(tmpbuf);
1325 TRACE("failed (%ld)\n",GetLastError());
1326 return NULL;
1329 /*********************************************************************
1330 * _wtempnam (MSVCRT.@)
1332 WCHAR *_wtempnam(const WCHAR *dir, const WCHAR *prefix)
1334 WCHAR tmpbuf[MAX_PATH];
1336 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1337 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1339 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1340 return _wcsdup(tmpbuf);
1342 TRACE("failed (%ld)\n",GetLastError());
1343 return NULL;
1346 /*********************************************************************
1347 * _umask (MSVCRT.@)
1349 int _umask(int umask)
1351 int old_umask = MSVCRT_umask;
1352 TRACE("(%d)\n",umask);
1353 MSVCRT_umask = umask;
1354 return old_umask;
1357 /*********************************************************************
1358 * _utime (MSVCRT.@)
1360 int _utime(const char* path, struct _utimbuf *t)
1362 int fd = _open(path, _O_WRONLY | _O_BINARY);
1364 if (fd > 0)
1366 int retVal = _futime(fd, t);
1367 _close(fd);
1368 return retVal;
1370 return -1;
1373 /*********************************************************************
1374 * _wutime (MSVCRT.@)
1376 int _wutime(const WCHAR* path, struct _utimbuf *t)
1378 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1380 if (fd > 0)
1382 int retVal = _futime(fd, t);
1383 _close(fd);
1384 return retVal;
1386 return -1;
1389 /*********************************************************************
1390 * _write (MSVCRT.@)
1392 int _write(int fd, const void* buf, unsigned int count)
1394 DWORD num_written;
1395 HANDLE hand = msvcrt_fdtoh(fd);
1397 /* Dont trace small writes, it gets *very* annoying */
1398 #if 0
1399 if (count > 32)
1400 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1401 #endif
1402 if (hand == INVALID_HANDLE_VALUE)
1403 return -1;
1405 /* If appending, go to EOF */
1406 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1407 _lseek(fd, 0, FILE_END);
1409 if (WriteFile(hand, buf, count, &num_written, NULL)
1410 && (num_written == count))
1411 return num_written;
1413 TRACE(":failed-last error (%ld)\n",GetLastError());
1414 if (MSVCRT_files[fd])
1415 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1417 return -1;
1420 /*********************************************************************
1421 * _putw (MSVCRT.@)
1423 int _putw(int val, MSVCRT_FILE* file)
1425 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1428 /*********************************************************************
1429 * clearerr (MSVCRT.@)
1431 void MSVCRT_clearerr(MSVCRT_FILE* file)
1433 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1434 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1437 /*********************************************************************
1438 * fclose (MSVCRT.@)
1440 int MSVCRT_fclose(MSVCRT_FILE* file)
1442 int r;
1443 r=_close(file->_file);
1444 return ((r==MSVCRT_EOF) || (file->_flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1447 /*********************************************************************
1448 * feof (MSVCRT.@)
1450 int MSVCRT_feof(MSVCRT_FILE* file)
1452 return file->_flag & MSVCRT__IOEOF;
1455 /*********************************************************************
1456 * ferror (MSVCRT.@)
1458 int MSVCRT_ferror(MSVCRT_FILE* file)
1460 return file->_flag & MSVCRT__IOERR;
1463 /*********************************************************************
1464 * fflush (MSVCRT.@)
1466 int MSVCRT_fflush(MSVCRT_FILE* file)
1468 if(!file) {
1469 _flushall();
1470 return 0;
1471 } else {
1472 int res=msvcrt_flush_buffer(file);
1473 return res;
1477 /*********************************************************************
1478 * fgetc (MSVCRT.@)
1480 int MSVCRT_fgetc(MSVCRT_FILE* file)
1482 if (file->_cnt>0) {
1483 file->_cnt--;
1484 return *(unsigned char *)file->_ptr++;
1485 } else {
1486 return _filbuf(file);
1490 /*********************************************************************
1491 * _fgetchar (MSVCRT.@)
1493 int _fgetchar(void)
1495 return MSVCRT_fgetc(MSVCRT_stdin);
1498 /*********************************************************************
1499 * _filbuf (MSVCRT.@)
1501 int _filbuf(MSVCRT_FILE* file)
1504 /* Allocate buffer if needed */
1505 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1506 msvcrt_alloc_buffer(file);
1508 if(!(file->_flag & MSVCRT__IOREAD)) {
1509 if(file->_flag & MSVCRT__IORW) {
1510 file->_flag |= MSVCRT__IOREAD;
1511 } else {
1512 return MSVCRT_EOF;
1515 if(file->_flag & MSVCRT__IONBF) {
1516 unsigned char c;
1517 if (_read(file->_file,&c,1) != 1) {
1518 file->_flag |= MSVCRT__IOEOF;
1519 return MSVCRT_EOF;
1521 return c;
1522 } else {
1523 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1524 if(file->_cnt<0) file->_cnt = 0;
1525 if(!file->_cnt) {
1526 file->_flag |= MSVCRT__IOEOF;
1527 return MSVCRT_EOF;
1529 file->_cnt--;
1530 file->_ptr = file->_base+1;
1531 return *(unsigned char *)file->_base;
1535 /*********************************************************************
1536 * fgetpos (MSVCRT.@)
1538 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1540 *pos = MSVCRT_ftell(file);
1541 return (*pos == -1? -1 : 0);
1544 /*********************************************************************
1545 * fgets (MSVCRT.@)
1547 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1549 int cc;
1550 char * buf_start = s;
1552 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1553 file,file->_file,s,size);
1555 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1556 cc = MSVCRT_fgetc(file))
1557 if (cc != '\r')
1559 if (--size <= 0) break;
1560 *s++ = (char)cc;
1562 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1564 TRACE(":nothing read\n");
1565 return 0;
1567 if (cc == '\n')
1568 if (--size > 0)
1569 *s++ = '\n';
1570 *s = '\0';
1571 TRACE(":got '%s'\n", buf_start);
1572 return buf_start;
1575 /*********************************************************************
1576 * fgetwc (MSVCRT.@)
1578 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1580 WCHAR wc;
1581 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1582 return MSVCRT_WEOF;
1583 return wc;
1586 /*********************************************************************
1587 * getwc (MSVCRT.@)
1589 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1591 return MSVCRT_fgetwc(file);
1594 /*********************************************************************
1595 * _fgetwchar (MSVCRT.@)
1597 MSVCRT_wint_t _fgetwchar(void)
1599 return MSVCRT_fgetwc(MSVCRT_stdin);
1602 /*********************************************************************
1603 * getwchar (MSVCRT.@)
1605 MSVCRT_wint_t MSVCRT_getwchar(void)
1607 return _fgetwchar();
1610 /*********************************************************************
1611 * fgetws (MSVCRT.@)
1613 WCHAR *MSVCRT_fgetws(WCHAR *s, int size, MSVCRT_FILE* file)
1615 int cc;
1616 WCHAR * buf_start = s;
1618 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1619 file,file->_file,s,size);
1621 for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n';
1622 cc = MSVCRT_fgetwc(file))
1623 if (cc != L'\r')
1625 if (--size <= 0) break;
1626 *s++ = cc;
1628 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1630 TRACE(":nothing read\n");
1631 return 0;
1633 if (cc == L'\n')
1634 if (--size > 0)
1635 *s++ = '\n';
1636 *s = '\0';
1637 /* TRACE(":got '%s'\n", buf_start); */
1638 return buf_start;
1642 /*********************************************************************
1643 * fputwc (MSVCRT.@)
1645 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1647 WCHAR mwc=wc;
1648 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
1649 return MSVCRT_WEOF;
1650 return wc;
1653 /*********************************************************************
1654 * _fputwchar (MSVCRT.@)
1656 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1658 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1661 /*********************************************************************
1662 * fopen (MSVCRT.@)
1664 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1666 MSVCRT_FILE* file;
1667 int flags = 0, plus = 0, fd;
1668 const char* search = mode;
1670 TRACE("(%s,%s)\n",path,mode);
1672 while (*search)
1673 if (*search++ == '+')
1674 plus = 1;
1676 /* map mode string to open() flags. "man fopen" for possibilities. */
1677 switch(*mode++)
1679 case 'R': case 'r':
1680 flags = (plus ? _O_RDWR : _O_RDONLY);
1681 break;
1682 case 'W': case 'w':
1683 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1684 break;
1685 case 'A': case 'a':
1686 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1687 break;
1688 default:
1689 return NULL;
1692 while (*mode)
1693 switch (*mode++)
1695 case 'B': case 'b':
1696 flags |= _O_BINARY;
1697 flags &= ~_O_TEXT;
1698 break;
1699 case 'T': case 't':
1700 flags |= _O_TEXT;
1701 flags &= ~_O_BINARY;
1702 break;
1703 case '+':
1704 break;
1705 default:
1706 FIXME(":unknown flag %c not supported\n",mode[-1]);
1709 fd = _open(path, flags);
1711 if (fd < 0)
1712 return NULL;
1714 file = msvcrt_alloc_fp(fd);
1715 TRACE(":got (%p)\n",file);
1716 if (!file)
1717 _close(fd);
1719 return file;
1722 /*********************************************************************
1723 * _wfopen (MSVCRT.@)
1725 MSVCRT_FILE *_wfopen(const WCHAR *path, const WCHAR *mode)
1727 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1728 char *patha = MSVCRT_calloc(plen + 1, 1);
1729 char *modea = MSVCRT_calloc(mlen + 1, 1);
1731 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1733 if (patha && modea &&
1734 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1735 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1737 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1738 MSVCRT_free(patha);
1739 MSVCRT_free(modea);
1740 return retval;
1743 MSVCRT__set_errno(GetLastError());
1744 return NULL;
1747 /*********************************************************************
1748 * _fsopen (MSVCRT.@)
1750 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1752 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1753 return MSVCRT_fopen(path,mode);
1756 /*********************************************************************
1757 * _wfsopen (MSVCRT.@)
1759 MSVCRT_FILE* _wfsopen(const WCHAR *path, const WCHAR *mode, int share)
1761 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1762 debugstr_w(path),debugstr_w(mode),share);
1763 return _wfopen(path,mode);
1766 /*********************************************************************
1767 * fputc (MSVCRT.@)
1769 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1771 if(file->_cnt>0) {
1772 *file->_ptr++=c;
1773 file->_cnt--;
1774 return c;
1775 } else {
1776 return _flsbuf(c, file);
1780 /*********************************************************************
1781 * _flsbuf (MSVCRT.@)
1783 int _flsbuf(int c, MSVCRT_FILE* file)
1785 /* Flush output buffer */
1786 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
1787 msvcrt_alloc_buffer(file);
1789 if(!(file->_flag & MSVCRT__IOWRT)) {
1790 if(file->_flag & MSVCRT__IORW) {
1791 file->_flag |= MSVCRT__IOWRT;
1792 } else {
1793 return MSVCRT_EOF;
1796 if(file->_bufsiz) {
1797 int res=msvcrt_flush_buffer(file);
1798 return res?res : MSVCRT_fputc(c, file);
1799 } else {
1800 unsigned char cc=c;
1801 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
1805 /*********************************************************************
1806 * _fputchar (MSVCRT.@)
1808 int _fputchar(int c)
1810 return MSVCRT_fputc(c, MSVCRT_stdout);
1813 /*********************************************************************
1814 * fread (MSVCRT.@)
1816 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1817 { MSVCRT_size_t rcnt=size * nmemb;
1818 MSVCRT_size_t read=0;
1819 int pread=0;
1820 /* first buffered data */
1821 if(file->_cnt>0) {
1822 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
1823 memcpy(ptr, file->_ptr, pcnt);
1824 file->_cnt -= pcnt;
1825 file->_ptr += pcnt;
1826 read += pcnt ;
1827 rcnt -= pcnt ;
1828 ptr = (char*)ptr + pcnt;
1829 } else if(!(file->_flag & MSVCRT__IOREAD )) {
1830 if(file->_flag & MSVCRT__IORW) {
1831 file->_flag |= MSVCRT__IOREAD;
1832 } else
1833 return 0;
1835 if(rcnt) pread = _read(file->_file,ptr, rcnt);
1836 if (MSVCRT_flags[file->_file] & MSVCRT__IOEOF)
1837 /* expose feof condition in the flags
1838 MFC tests file->_flag for feof, and doesn't not call feof())
1840 file->_flag |= MSVCRT__IOEOF;
1841 if (pread <= 0)
1842 pread = 0;
1843 read+=pread;
1844 return read / size;
1847 /*********************************************************************
1848 * freopen (MSVCRT.@)
1851 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
1853 MSVCRT_FILE* newfile;
1854 int fd;
1856 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1857 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
1858 return NULL;
1860 if (fd > 2)
1862 #if 0
1863 FIXME(":reopen on user file not implemented!\n");
1864 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1865 return NULL;
1866 #endif
1867 if(MSVCRT_fclose(file))
1868 return NULL;
1869 return MSVCRT_fopen(path, mode);
1872 /* first, create the new file */
1873 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
1874 return NULL;
1876 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1877 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1878 MSVCRT_handles[newfile->_file]))
1880 /* Redirecting std handle to file , copy over.. */
1881 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
1882 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
1883 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
1884 MSVCRT__iob[fd]._file = fd;
1885 /* And free up the resources allocated by fopen, but
1886 * not the HANDLE we copied. */
1887 MSVCRT_free(MSVCRT_files[fd]);
1888 msvcrt_free_fd(newfile->_file);
1889 return &MSVCRT__iob[fd];
1892 WARN(":failed-last error (%ld)\n",GetLastError());
1893 MSVCRT_fclose(newfile);
1894 MSVCRT__set_errno(GetLastError());
1895 return NULL;
1898 /*********************************************************************
1899 * fsetpos (MSVCRT.@)
1901 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1903 return _lseek(file->_file,*pos,SEEK_SET);
1906 /*********************************************************************
1907 * fseek (MSVCRT.@)
1909 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
1911 /* Flush output if needed */
1912 if(file->_flag & MSVCRT__IOWRT)
1913 msvcrt_flush_buffer(file);
1915 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
1916 offset -= file->_cnt;
1918 /* Discard buffered input */
1919 file->_cnt = 0;
1920 file->_ptr = file->_base;
1921 /* Reset direction of i/o */
1922 if(file->_flag & MSVCRT__IORW) {
1923 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
1925 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
1928 /*********************************************************************
1929 * ftell (MSVCRT.@)
1931 LONG MSVCRT_ftell(MSVCRT_FILE* file)
1933 int off=0;
1934 long pos;
1935 if(file->_bufsiz) {
1936 if( file->_flag & MSVCRT__IOWRT ) {
1937 off = file->_ptr - file->_base;
1938 } else {
1939 off = -file->_cnt;
1942 pos = _tell(file->_file);
1943 if(pos == -1) return pos;
1944 return off + pos;
1947 /*********************************************************************
1948 * fwrite (MSVCRT.@)
1950 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1952 MSVCRT_size_t wrcnt=size * nmemb;
1953 int written = 0;
1954 if (size == 0)
1955 return 0;
1956 if(file->_cnt) {
1957 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
1958 memcpy(file->_ptr, ptr, pcnt);
1959 file->_cnt -= pcnt;
1960 file->_ptr += pcnt;
1961 written = pcnt;
1962 wrcnt -= pcnt;
1963 ptr = (char*)ptr + pcnt;
1964 } else if(!(file->_flag & MSVCRT__IOWRT)) {
1965 if(file->_flag & MSVCRT__IORW) {
1966 file->_flag |= MSVCRT__IOWRT;
1967 } else
1968 return 0;
1970 if(wrcnt) {
1971 /* Flush buffer */
1972 int res=msvcrt_flush_buffer(file);
1973 if(!res) {
1974 int pwritten = _write(file->_file, ptr, wrcnt);
1975 if (pwritten <= 0) pwritten=0;
1976 written += pwritten;
1979 return written / size;
1982 /*********************************************************************
1983 * fputs (MSVCRT.@)
1985 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
1987 size_t len = strlen(s);
1988 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
1991 /*********************************************************************
1992 * fputws (MSVCRT.@)
1994 int MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
1996 size_t len = strlenW(s);
1997 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2000 /*********************************************************************
2001 * getchar (MSVCRT.@)
2003 int MSVCRT_getchar(void)
2005 return MSVCRT_fgetc(MSVCRT_stdin);
2008 /*********************************************************************
2009 * getc (MSVCRT.@)
2011 int MSVCRT_getc(MSVCRT_FILE* file)
2013 return MSVCRT_fgetc(file);
2016 /*********************************************************************
2017 * gets (MSVCRT.@)
2019 char *MSVCRT_gets(char *buf)
2021 int cc;
2022 char * buf_start = buf;
2024 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2025 cc = MSVCRT_fgetc(MSVCRT_stdin))
2026 if(cc != '\r') *buf++ = (char)cc;
2028 *buf = '\0';
2030 TRACE("got '%s'\n", buf_start);
2031 return buf_start;
2034 /*********************************************************************
2035 * _getws (MSVCRT.@)
2037 WCHAR* MSVCRT__getws(WCHAR* buf)
2039 MSVCRT_wint_t cc;
2040 WCHAR* ws = buf;
2042 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2043 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2045 if (cc != '\r')
2046 *buf++ = (WCHAR)cc;
2048 *buf = '\0';
2050 TRACE("got '%s'\n", debugstr_w(ws));
2051 return ws;
2054 /*********************************************************************
2055 * putc (MSVCRT.@)
2057 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2059 return MSVCRT_fputc(c, file);
2062 /*********************************************************************
2063 * putchar (MSVCRT.@)
2065 int MSVCRT_putchar(int c)
2067 return MSVCRT_fputc(c, MSVCRT_stdout);
2070 /*********************************************************************
2071 * puts (MSVCRT.@)
2073 int MSVCRT_puts(const char *s)
2075 size_t len = strlen(s);
2076 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2077 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2080 /*********************************************************************
2081 * _putws (MSVCRT.@)
2083 int _putws(const WCHAR *s)
2085 static const WCHAR nl = '\n';
2086 size_t len = strlenW(s);
2087 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2088 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2091 /*********************************************************************
2092 * remove (MSVCRT.@)
2094 int MSVCRT_remove(const char *path)
2096 TRACE("(%s)\n",path);
2097 if (DeleteFileA(path))
2098 return 0;
2099 TRACE(":failed (%ld)\n",GetLastError());
2100 MSVCRT__set_errno(GetLastError());
2101 return -1;
2104 /*********************************************************************
2105 * _wremove (MSVCRT.@)
2107 int _wremove(const WCHAR *path)
2109 TRACE("(%s)\n",debugstr_w(path));
2110 if (DeleteFileW(path))
2111 return 0;
2112 TRACE(":failed (%ld)\n",GetLastError());
2113 MSVCRT__set_errno(GetLastError());
2114 return -1;
2117 /*********************************************************************
2118 * scanf (MSVCRT.@)
2120 int MSVCRT_scanf(const char *format, ...)
2122 va_list valist;
2123 int res;
2125 va_start(valist, format);
2126 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
2127 va_end(valist);
2128 return res;
2131 /*********************************************************************
2132 * wscanf (MSVCRT.@)
2134 int MSVCRT_wscanf(const WCHAR *format, ...)
2136 va_list valist;
2137 int res;
2139 va_start(valist, format);
2140 res = MSVCRT_fwscanf(MSVCRT_stdin, format, valist);
2141 va_end(valist);
2142 return res;
2145 /*********************************************************************
2146 * rename (MSVCRT.@)
2148 int MSVCRT_rename(const char *oldpath,const char *newpath)
2150 TRACE(":from %s to %s\n",oldpath,newpath);
2151 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2152 return 0;
2153 TRACE(":failed (%ld)\n",GetLastError());
2154 MSVCRT__set_errno(GetLastError());
2155 return -1;
2158 /*********************************************************************
2159 * _wrename (MSVCRT.@)
2161 int _wrename(const WCHAR *oldpath,const WCHAR *newpath)
2163 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2164 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2165 return 0;
2166 TRACE(":failed (%ld)\n",GetLastError());
2167 MSVCRT__set_errno(GetLastError());
2168 return -1;
2171 /*********************************************************************
2172 * setvbuf (MSVCRT.@)
2174 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2176 /* TODO: Check if file busy */
2177 if(file->_bufsiz) {
2178 MSVCRT_free(file->_base);
2179 file->_bufsiz = 0;
2180 file->_cnt = 0;
2182 if(mode == MSVCRT__IOFBF) {
2183 file->_flag &= ~MSVCRT__IONBF;
2184 file->_base = file->_ptr = buf;
2185 if(buf) {
2186 file->_bufsiz = size;
2188 } else {
2189 file->_flag |= MSVCRT__IONBF;
2191 return 0;
2194 /*********************************************************************
2195 * setbuf (MSVCRT.@)
2197 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2199 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2202 /*********************************************************************
2203 * tmpnam (MSVCRT.@)
2205 char *MSVCRT_tmpnam(char *s)
2207 char tmpbuf[MAX_PATH];
2208 char* prefix = "TMP";
2209 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
2210 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
2212 TRACE(":failed-last error (%ld)\n",GetLastError());
2213 return NULL;
2215 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
2216 s = MSVCRT_tmpname;
2217 return s;
2220 /*********************************************************************
2221 * tmpfile (MSVCRT.@)
2223 MSVCRT_FILE* MSVCRT_tmpfile(void)
2225 char *filename = MSVCRT_tmpnam(NULL);
2226 int fd;
2227 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2228 if (fd != -1)
2229 return msvcrt_alloc_fp(fd);
2230 return NULL;
2233 /*********************************************************************
2234 * vfprintf (MSVCRT.@)
2236 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2238 char buf[2048], *mem = buf;
2239 int written, resize = sizeof(buf), retval;
2240 /* There are two conventions for vsnprintf failing:
2241 * Return -1 if we truncated, or
2242 * Return the number of bytes that would have been written
2243 * The code below handles both cases
2245 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2246 written > resize)
2248 resize = (written == -1 ? resize * 2 : written + 1);
2249 if (mem != buf)
2250 MSVCRT_free (mem);
2251 if (!(mem = (char *)MSVCRT_malloc(resize)))
2252 return MSVCRT_EOF;
2254 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2255 if (mem != buf)
2256 MSVCRT_free (mem);
2257 return retval;
2260 /*********************************************************************
2261 * vfwprintf (MSVCRT.@)
2262 * FIXME:
2263 * Is final char included in written (then resize is too big) or not
2264 * (then we must test for equality too)?
2266 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
2268 WCHAR buf[2048], *mem = buf;
2269 int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
2270 /* See vfprintf comments */
2271 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2272 written > resize)
2274 resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
2275 if (mem != buf)
2276 MSVCRT_free (mem);
2277 if (!(mem = (WCHAR *)MSVCRT_malloc(resize*sizeof(*mem))))
2278 return MSVCRT_EOF;
2280 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2281 if (mem != buf)
2282 MSVCRT_free (mem);
2283 return retval;
2286 /*********************************************************************
2287 * vprintf (MSVCRT.@)
2289 int MSVCRT_vprintf(const char *format, va_list valist)
2291 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2294 /*********************************************************************
2295 * vwprintf (MSVCRT.@)
2297 int MSVCRT_vwprintf(const WCHAR *format, va_list valist)
2299 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2302 /*********************************************************************
2303 * fprintf (MSVCRT.@)
2305 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2307 va_list valist;
2308 int res;
2309 va_start(valist, format);
2310 res = MSVCRT_vfprintf(file, format, valist);
2311 va_end(valist);
2312 return res;
2315 /*********************************************************************
2316 * fwprintf (MSVCRT.@)
2318 int MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
2320 va_list valist;
2321 int res;
2322 va_start(valist, format);
2323 res = MSVCRT_vfwprintf(file, format, valist);
2324 va_end(valist);
2325 return res;
2328 /*********************************************************************
2329 * printf (MSVCRT.@)
2331 int MSVCRT_printf(const char *format, ...)
2333 va_list valist;
2334 int res;
2335 va_start(valist, format);
2336 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2337 va_end(valist);
2338 return res;
2341 /*********************************************************************
2342 * ungetc (MSVCRT.@)
2344 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2346 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2347 msvcrt_alloc_buffer(file);
2348 file->_ptr++;
2350 if(file->_ptr>file->_base) {
2351 file->_ptr--;
2352 *file->_ptr=c;
2353 file->_cnt++;
2354 return c;
2356 return MSVCRT_EOF;
2359 /*********************************************************************
2360 * ungetwc (MSVCRT.@)
2362 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2364 WCHAR mwc = wc;
2365 char * pp = (char *)&mwc;
2366 int i;
2367 for(i=sizeof(WCHAR)-1;i>=0;i--) {
2368 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2369 return MSVCRT_WEOF;
2371 return mwc;
2374 /*********************************************************************
2375 * wprintf (MSVCRT.@)
2377 int MSVCRT_wprintf(const WCHAR *format, ...)
2379 va_list valist;
2380 int res;
2381 va_start(valist, format);
2382 res = MSVCRT_vwprintf(format, valist);
2383 va_end(valist);
2384 return res;