Updated.
[wine] / dlls / msvcrt / file.c
1 /*
2  * msvcrt.dll file functions
3  *
4  * Copyright 1996,1998 Marcus Meissner
5  * Copyright 1996 Jukka Iivonen
6  * Copyright 1997,2000 Uwe Bonnes
7  * Copyright 2000 Jon Griffiths
8  */
9 #include <time.h>
10 #include "ntddk.h"
11 #include "msvcrt.h"
12 #include "ms_errno.h"
13
14 DEFAULT_DEBUG_CHANNEL(msvcrt);
15
16 /* stat() mode bits */
17 #define _S_IFMT   0xF000
18 #define _S_IFREG  0x8000
19 #define _S_IFDIR  0x4000
20 #define _S_IFCHR  0x2000
21 #define _S_IFIFO  0x1000
22 #define _S_IFBLK  0x3000
23 #define _S_IREAD  0x0100
24 #define _S_IWRITE 0x0080
25 #define _S_IEXEC  0x0040
26
27 /* for stat mode, permissions apply to all,owner and group */
28 #define MSVCRT_S_IREAD  (_S_IREAD  | (_S_IREAD  >> 3) | (_S_IREAD  >> 6))
29 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
30 #define MSVCRT_S_IEXEC  (_S_IEXEC  | (_S_IEXEC  >> 3) | (_S_IEXEC  >> 6))
31
32 /* _open modes */
33 #define _O_RDONLY 0x0000
34 #define _O_WRONLY 0x0001
35 #define _O_RDWR   0x0002
36 #define _O_APPEND 0x0008
37 #define _O_CREAT  0x0100
38 #define _O_TRUNC  0x0200
39 #define _O_EXCL   0x0400
40 #define _O_TEXT   0x4000
41 #define _O_BINARY 0x8000
42 #define _O_TEMPORARY 0x0040 /* Will be closed and deleted on exit */
43
44 /* _access() bit flags FIXME: incomplete */
45 #define W_OK      2
46
47 typedef struct _crtfile
48 {
49   char* _ptr;
50   int   _cnt;
51   char* _base;
52   int   _flag;
53   int   _file; /* fd */
54   int   _charbuf;
55   int   _bufsiz;
56   char *_tmpfname;
57 } MSVCRT_FILE;
58
59 /* file._flag flags */
60 #define _IOREAD   0x0001
61 #define _IOWRT    0x0002
62 #define _IOEOF    0x0010
63 #define _IOERR    0x0020
64 #define _IORW     0x0080
65 #define _IOAPPEND 0x0200
66
67 #define SEEK_SET    0
68 #define SEEK_CUR    1
69 #define SEEK_END    2
70
71 #define _IOFBF 0
72 #define _IOLBF 1
73 #define _IONBF 2
74
75 #define BUFSIZ 512
76
77 #define MSVCRT_stdin  (&MSVCRT__iob[0])
78 #define MSVCRT_stdout (&MSVCRT__iob[1])
79 #define MSVCRT_stderr (&MSVCRT__iob[2])
80
81 struct _stat
82 {
83     unsigned short st_dev;
84     unsigned short st_ino;
85     unsigned short st_mode;
86     short  st_nlink;
87     short  st_uid;
88     short  st_gid;
89     unsigned int   st_rdev;
90     int    st_size;
91     int    st_atime;
92     int    st_mtime;
93     int    st_ctime;
94 };
95
96 typedef long MSVCRT_fpos_t;
97
98 struct _utimbuf
99 {
100   time_t actime;
101   time_t modtime;
102 };
103
104 /* FIXME: Make this dynamic */
105 #define MSVCRT_MAX_FILES 257
106
107 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
108 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
109 int  MSVCRT_flags[MSVCRT_MAX_FILES];
110 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
111 MSVCRT_FILE MSVCRT__iob[3];
112
113 static int MSVCRT_fdstart = 3; /* first unallocated fd */
114 static int MSVCRT_fdend = 3; /* highest allocated fd */
115
116 /* INTERNAL: process umask */
117 static int MSVCRT_umask = 0;
118
119 /* INTERNAL: Static buffer for temp file name */
120 static char MSVCRT_tmpname[MAX_PATH];
121
122 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
123 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
124 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
125 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
126
127 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
128 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
129 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
130 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
131 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
132
133 extern CRITICAL_SECTION MSVCRT_file_cs;
134 #define LOCK_FILES     EnterCriticalSection(&MSVCRT_file_cs)
135 #define UNLOCK_FILES   LeaveCriticalSection(&MSVCRT_file_cs)
136
137 time_t __cdecl MSVCRT_time(time_t *);
138 int __cdecl MSVCRT__getdrive(void);
139 WCHAR *__cdecl MSVCRT__wcsdup(const WCHAR *);
140 unsigned int __cdecl wcslen(const WCHAR*);
141 int __cdecl iswalpha(WCHAR);
142 int __cdecl towupper(WCHAR);
143 int __cdecl towlower(WCHAR);
144 int __cdecl MSVCRT__vsnwprintf(WCHAR *,unsigned int,const WCHAR *,va_list);
145
146 /* INTERNAL: Get the HANDLE for a fd */
147 static HANDLE MSVCRT__fdtoh(int fd)
148 {
149   if (fd < 0 || fd >= MSVCRT_fdend ||
150       MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
151   {
152     WARN(":fd (%d) - no handle!\n",fd);
153     SET_THREAD_VAR(doserrno,0);
154     SET_THREAD_VAR(errno,MSVCRT_EBADF);
155    return INVALID_HANDLE_VALUE;
156   }
157   return MSVCRT_handles[fd];
158 }
159
160 /* INTERNAL: free a file entry fd */
161 static void MSVCRT__free_fd(int fd)
162 {
163   MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
164   MSVCRT_files[fd] = 0;
165   MSVCRT_flags[fd] = 0;
166   TRACE(":fd (%d) freed\n",fd);
167   if (fd < 3)
168     return; /* dont use 0,1,2 for user files */
169   if (fd == MSVCRT_fdend - 1)
170     MSVCRT_fdend--;
171   if (fd < MSVCRT_fdstart)
172     MSVCRT_fdstart = fd;
173 }
174
175 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
176 static int MSVCRT__alloc_fd(HANDLE hand, int flag)
177 {
178   int fd = MSVCRT_fdstart;
179
180   TRACE(":handle (%d) allocating fd (%d)\n",hand,fd);
181   if (fd >= MSVCRT_MAX_FILES)
182   {
183     WARN(":files exhausted!\n");
184     return -1;
185   }
186   MSVCRT_handles[fd] = hand;
187   MSVCRT_flags[fd] = flag;
188
189   /* locate next free slot */
190   if (fd == MSVCRT_fdend)
191     MSVCRT_fdstart = ++MSVCRT_fdend;
192   else
193     while(MSVCRT_fdstart < MSVCRT_fdend &&
194           MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
195       MSVCRT_fdstart++;
196
197   return fd;
198 }
199
200 /* INTERNAL: Allocate a FILE* for an fd slot
201  * This is done lazily to avoid memory wastage for low level open/write
202  * usage when a FILE* is not requested (but may be later).
203  */
204 static MSVCRT_FILE* MSVCRT__alloc_fp(int fd)
205 {
206   TRACE(":fd (%d) allocating FILE*\n",fd);
207   if (fd < 0 || fd >= MSVCRT_fdend ||
208       MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
209   {
210     WARN(":invalid fd %d\n",fd);
211     SET_THREAD_VAR(doserrno,0);
212     SET_THREAD_VAR(errno,MSVCRT_EBADF);
213     return NULL;
214   }
215   if (!MSVCRT_files[fd])
216   {
217     if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
218     {
219       MSVCRT_files[fd]->_file = fd;
220       MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
221       MSVCRT_files[fd]->_flag &= ~_IOAPPEND; /* mask out, see above */
222     }
223   }
224   TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
225   return MSVCRT_files[fd];
226 }
227
228
229 /* INTERNAL: Set up stdin, stderr and stdout */
230 void MSVCRT_init_io(void)
231 {
232   int i;
233   memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
234   MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
235   MSVCRT_flags[0] = MSVCRT__iob[0]._flag = _IOREAD;
236   MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
237   MSVCRT_flags[1] = MSVCRT__iob[1]._flag = _IOWRT;
238   MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
239   MSVCRT_flags[2] = MSVCRT__iob[2]._flag = _IOWRT;
240
241   TRACE(":handles (%d)(%d)(%d)\n",MSVCRT_handles[0],
242         MSVCRT_handles[1],MSVCRT_handles[2]);
243
244   for (i = 0; i < 3; i++)
245   {
246     /* FILE structs for stdin/out/err are static and never deleted */
247     MSVCRT_files[i] = &MSVCRT__iob[i];
248     MSVCRT__iob[i]._file = i;
249     MSVCRT_tempfiles[i] = NULL;
250   }
251 }
252
253 /*********************************************************************
254  *              __p__iob(MSVCRT.@)
255  */
256 MSVCRT_FILE *MSVCRT___p__iob(void)
257 {
258  return &MSVCRT__iob[0];
259 }
260
261 /*********************************************************************
262  *              _access (MSVCRT.@)
263  */
264 int __cdecl MSVCRT__access(const char *filename, int mode)
265 {
266   DWORD attr = GetFileAttributesA(filename);
267
268   TRACE("(%s,%d) %ld\n",filename,mode,attr);
269
270   if (!filename || attr == 0xffffffff)
271   {
272     MSVCRT__set_errno(GetLastError());
273     return -1;
274   }
275   if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
276   {
277     MSVCRT__set_errno(ERROR_ACCESS_DENIED);
278     return -1;
279   }
280   return 0;
281 }
282
283 /*********************************************************************
284  *              _waccess (MSVCRT.@)
285  */
286 int __cdecl MSVCRT__waccess(const WCHAR *filename, int mode)
287 {
288   DWORD attr = GetFileAttributesW(filename);
289
290   TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
291
292   if (!filename || attr == 0xffffffff)
293   {
294     MSVCRT__set_errno(GetLastError());
295     return -1;
296   }
297   if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
298   {
299     MSVCRT__set_errno(ERROR_ACCESS_DENIED);
300     return -1;
301   }
302   return 0;
303 }
304
305 /*********************************************************************
306  *              _chmod (MSVCRT.@)
307  */
308 int __cdecl MSVCRT__chmod(const char *path, int flags)
309 {
310   DWORD oldFlags = GetFileAttributesA(path);
311
312   if (oldFlags != 0x0FFFFFFFF)
313   {
314     DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
315       oldFlags | FILE_ATTRIBUTE_READONLY;
316
317     if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
318       return 0;
319   }
320   MSVCRT__set_errno(GetLastError());
321   return -1;
322 }
323
324 /*********************************************************************
325  *              _wchmod (MSVCRT.@)
326  */
327 int __cdecl MSVCRT__wchmod(const WCHAR *path, int flags)
328 {
329   DWORD oldFlags = GetFileAttributesW(path);
330
331   if (oldFlags != 0x0FFFFFFFF)
332   {
333     DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
334       oldFlags | FILE_ATTRIBUTE_READONLY;
335
336     if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
337       return 0;
338   }
339   MSVCRT__set_errno(GetLastError());
340   return -1;
341 }
342
343 /*********************************************************************
344  *              _unlink (MSVCRT.@)
345  */
346 int __cdecl MSVCRT__unlink(const char *path)
347 {
348   TRACE("(%s)\n",path);
349   if(DeleteFileA(path))
350     return 0;
351   TRACE("failed (%ld)\n",GetLastError());
352   MSVCRT__set_errno(GetLastError());
353   return -1;
354 }
355
356 /*********************************************************************
357  *              _wunlink (MSVCRT.@)
358  */
359 int __cdecl MSVCRT__wunlink(const WCHAR *path)
360 {
361   TRACE("(%s)\n",debugstr_w(path));
362   if(DeleteFileW(path))
363     return 0;
364   TRACE("failed (%ld)\n",GetLastError());
365   MSVCRT__set_errno(GetLastError());
366   return -1;
367 }
368
369 /*********************************************************************
370  *              _close (MSVCRT.@)
371  */
372 int __cdecl MSVCRT__close(int fd)
373 {
374   HANDLE hand = MSVCRT__fdtoh(fd);
375
376   TRACE(":fd (%d) handle (%d)\n",fd,hand);
377   if (hand == INVALID_HANDLE_VALUE)
378     return -1;
379
380   /* Dont free std FILE*'s, they are not dynamic */
381   if (fd > 2 && MSVCRT_files[fd])
382     MSVCRT_free(MSVCRT_files[fd]);
383
384   MSVCRT__free_fd(fd);
385
386   if (!CloseHandle(hand))
387   {
388     WARN(":failed-last error (%ld)\n",GetLastError());
389     MSVCRT__set_errno(GetLastError());
390     return -1;
391   }
392   if (MSVCRT_tempfiles[fd])
393   {
394     TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
395     MSVCRT__unlink(MSVCRT_tempfiles[fd]);
396     MSVCRT_free(MSVCRT_tempfiles[fd]);
397     MSVCRT_tempfiles[fd] = NULL;
398   }
399
400   TRACE(":ok\n");
401   return 0;
402 }
403
404 /*********************************************************************
405  *              _commit (MSVCRT.@)
406  */
407 int __cdecl MSVCRT__commit(int fd)
408 {
409   HANDLE hand = MSVCRT__fdtoh(fd);
410
411   TRACE(":fd (%d) handle (%d)\n",fd,hand);
412   if (hand == INVALID_HANDLE_VALUE)
413     return -1;
414
415   if (!FlushFileBuffers(hand))
416   {
417     if (GetLastError() == ERROR_INVALID_HANDLE)
418     {
419       /* FlushFileBuffers fails for console handles
420        * so we ignore this error.
421        */
422       return 0;
423     }
424     TRACE(":failed-last error (%ld)\n",GetLastError());
425     MSVCRT__set_errno(GetLastError());
426     return -1;
427   }
428   TRACE(":ok\n");
429   return 0;
430 }
431
432 /*********************************************************************
433  *              _eof (MSVCRT.@)
434  */
435 int __cdecl MSVCRT__eof(int fd)
436 {
437   DWORD curpos,endpos;
438   HANDLE hand = MSVCRT__fdtoh(fd);
439
440   TRACE(":fd (%d) handle (%d)\n",fd,hand);
441
442   if (hand == INVALID_HANDLE_VALUE)
443     return -1;
444
445   /* If we have a FILE* for this file, the EOF flag
446    * will be set by the read()/write() functions.
447    */
448   if (MSVCRT_files[fd])
449     return MSVCRT_files[fd]->_flag & _IOEOF;
450
451   /* Otherwise we do it the hard way */
452   curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
453   endpos = SetFilePointer(hand, 0, NULL, FILE_END);
454
455   if (curpos == endpos)
456     return TRUE;
457
458   SetFilePointer(hand, curpos, 0, FILE_BEGIN);
459   return FALSE;
460 }
461
462 /*********************************************************************
463  *              _fcloseall (MSVCRT.@)
464  */
465 int __cdecl MSVCRT__fcloseall(void)
466 {
467   int num_closed = 0, i;
468
469   for (i = 3; i < MSVCRT_fdend; i++)
470     if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
471     {
472       MSVCRT__close(i);
473       num_closed++;
474     }
475
476   TRACE(":closed (%d) handles\n",num_closed);
477   return num_closed;
478 }
479
480 /*********************************************************************
481  *              _lseek (MSVCRT.@)
482  */
483 LONG __cdecl MSVCRT__lseek(int fd, LONG offset, int whence)
484 {
485   DWORD ret;
486   HANDLE hand = MSVCRT__fdtoh(fd);
487
488   TRACE(":fd (%d) handle (%d)\n",fd,hand);
489   if (hand == INVALID_HANDLE_VALUE)
490     return -1;
491
492   if (whence < 0 || whence > 2)
493   {
494     SET_THREAD_VAR(errno,MSVCRT_EINVAL);
495     return -1;
496   }
497
498   TRACE(":fd (%d) to 0x%08lx pos %s\n",
499         fd,offset,(whence==SEEK_SET)?"SEEK_SET":
500         (whence==SEEK_CUR)?"SEEK_CUR":
501         (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
502
503   if ((ret = SetFilePointer(hand, offset, NULL, whence)) != 0xffffffff)
504   {
505     if (MSVCRT_files[fd])
506       MSVCRT_files[fd]->_flag &= ~_IOEOF;
507     /* FIXME: What if we seek _to_ EOF - is EOF set? */
508     return ret;
509   }
510   TRACE(":error-last error (%ld)\n",GetLastError());
511   if (MSVCRT_files[fd])
512     switch(GetLastError())
513     {
514     case ERROR_NEGATIVE_SEEK:
515     case ERROR_SEEK_ON_DEVICE:
516       MSVCRT__set_errno(GetLastError());
517       MSVCRT_files[fd]->_flag |= _IOERR;
518       break;
519     default:
520       break;
521     }
522   return -1;
523 }
524
525 /*********************************************************************
526  *              rewind (MSVCRT.@)
527  */
528 void __cdecl MSVCRT_rewind(MSVCRT_FILE* file)
529 {
530   TRACE(":file (%p) fd (%d)\n",file,file->_file);
531   MSVCRT__lseek(file->_file,0,SEEK_SET);
532   file->_flag &= ~(_IOEOF | _IOERR);
533 }
534
535 /*********************************************************************
536  *              _fdopen (MSVCRT.@)
537  */
538 MSVCRT_FILE* __cdecl MSVCRT__fdopen(int fd, const char *mode)
539 {
540   MSVCRT_FILE* file = MSVCRT__alloc_fp(fd);
541
542   TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
543   if (file)
544     MSVCRT_rewind(file);
545
546   return file;
547 }
548
549 /*********************************************************************
550  *              _wfdopen (MSVCRT.@)
551  */
552 MSVCRT_FILE* __cdecl MSVCRT__wfdopen(int fd, const WCHAR *mode)
553 {
554   MSVCRT_FILE* file = MSVCRT__alloc_fp(fd);
555
556   TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
557   if (file)
558     MSVCRT_rewind(file);
559
560   return file;
561 }
562
563 /*********************************************************************
564  *              _filelength (MSVCRT.@)
565  */
566 LONG __cdecl MSVCRT__filelength(int fd)
567 {
568   LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
569   if (curPos != -1)
570   {
571     LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
572     if (endPos != -1)
573     {
574       if (endPos != curPos)
575         MSVCRT__lseek(fd, curPos, SEEK_SET);
576       return endPos;
577     }
578   }
579   return -1;
580 }
581
582 /*********************************************************************
583  *              _fileno (MSVCRT.@)
584  */
585 int __cdecl MSVCRT__fileno(MSVCRT_FILE* file)
586 {
587   TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
588   return file->_file;
589 }
590
591 /*********************************************************************
592  *              _flushall (MSVCRT.@)
593  */
594 int __cdecl MSVCRT__flushall(void)
595 {
596   int num_flushed = 0, i = 3;
597
598   while(i < MSVCRT_fdend)
599     if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
600     {
601       if (MSVCRT__commit(i) == -1)
602         if (MSVCRT_files[i])
603           MSVCRT_files[i]->_flag |= _IOERR;
604       num_flushed++;
605     }
606
607   TRACE(":flushed (%d) handles\n",num_flushed);
608   return num_flushed;
609 }
610
611 /*********************************************************************
612  *              _fstat (MSVCRT.@)
613  */
614 int __cdecl MSVCRT__fstat(int fd, struct _stat* buf)
615 {
616   DWORD dw;
617   BY_HANDLE_FILE_INFORMATION hfi;
618   HANDLE hand = MSVCRT__fdtoh(fd);
619
620   TRACE(":fd (%d) stat (%p)\n",fd,buf);
621   if (hand == INVALID_HANDLE_VALUE)
622     return -1;
623
624   if (!buf)
625   {
626     WARN(":failed-NULL buf\n");
627     MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
628     return -1;
629   }
630
631   memset(&hfi, 0, sizeof(hfi));
632   memset(buf, 0, sizeof(struct _stat));
633   if (!GetFileInformationByHandle(hand, &hfi))
634   {
635     WARN(":failed-last error (%ld)\n",GetLastError());
636     MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
637     return -1;
638   }
639   FIXME(":dwFileAttributes = %d, mode set to 0",hfi.dwFileAttributes);
640   buf->st_nlink = hfi.nNumberOfLinks;
641   buf->st_size  = hfi.nFileSizeLow;
642   RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
643   buf->st_atime = dw;
644   RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
645   buf->st_mtime = buf->st_ctime = dw;
646   return 0;
647 }
648
649 /*********************************************************************
650  *              _futime (MSVCRT.@)
651  */
652 int __cdecl MSVCRT__futime(int fd, struct _utimbuf *t)
653 {
654   HANDLE hand = MSVCRT__fdtoh(fd);
655   FILETIME at, wt;
656
657   if (!t)
658   {
659     time_t currTime;
660     MSVCRT_time(&currTime);
661     RtlSecondsSince1970ToTime(currTime, &at);
662     memcpy(&wt, &at, sizeof(wt));
663   }
664   else
665   {
666     RtlSecondsSince1970ToTime(t->actime, &at);
667     if (t->actime == t->modtime)
668       memcpy(&wt, &at, sizeof(wt));
669     else
670       RtlSecondsSince1970ToTime(t->modtime, &wt);
671   }
672
673   if (!SetFileTime(hand, NULL, &at, &wt))
674   {
675     MSVCRT__set_errno(GetLastError());
676     return -1 ;
677   }
678   return 0;
679 }
680
681 /*********************************************************************
682  *              _get_osfhandle (MSVCRT.@)
683  */
684 HANDLE MSVCRT__get_osfhandle(int fd)
685 {
686   HANDLE hand = MSVCRT__fdtoh(fd);
687   HANDLE newhand = hand;
688   TRACE(":fd (%d) handle (%d)\n",fd,hand);
689
690   if (hand != INVALID_HANDLE_VALUE)
691   {
692     /* FIXME: I'm not convinced that I should be copying the
693      * handle here - it may be leaked if the app doesn't
694      * close it (and the API docs dont say that it should)
695      * Not duplicating it means that it can't be inherited
696      * and so lcc's wedit doesn't cope when it passes it to
697      * child processes. I've an idea that it should either
698      * be copied by CreateProcess, or marked as inheritable
699      * when initialised, or maybe both? JG 21-9-00.
700      */
701     DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
702                     &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
703   }
704   return newhand;
705 }
706
707 /*********************************************************************
708  *              _isatty (MSVCRT.@)
709  */
710 int __cdecl MSVCRT__isatty(int fd)
711 {
712   HANDLE hand = MSVCRT__fdtoh(fd);
713
714   TRACE(":fd (%d) handle (%d)\n",fd,hand);
715   if (hand == INVALID_HANDLE_VALUE)
716     return 0;
717
718   return GetFileType(fd) == FILE_TYPE_CHAR? 1 : 0;
719 }
720
721 /*********************************************************************
722  *              _mktemp (MSVCRT.@)
723  */
724 char *__cdecl MSVCRT__mktemp(char *pattern)
725 {
726   int numX = 0;
727   char *retVal = pattern;
728   int id;
729   char letter = 'a';
730
731   while(*pattern)
732     numX = (*pattern++ == 'X')? numX + 1 : 0;
733   if (numX < 5)
734     return NULL;
735   pattern--;
736   id = GetCurrentProcessId();
737   numX = 6;
738   while(numX--)
739   {
740     int tempNum = id / 10;
741     *pattern-- = id - (tempNum * 10) + '0';
742     id = tempNum;
743   }
744   pattern++;
745   do
746   {
747     if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
748         GetLastError() == ERROR_FILE_NOT_FOUND)
749       return retVal;
750     *pattern = letter++;
751   } while(letter != '|');
752   return NULL;
753 }
754
755 /*********************************************************************
756  *              _wmktemp (MSVCRT.@)
757  */
758 WCHAR *__cdecl MSVCRT__wmktemp(WCHAR *pattern)
759 {
760   int numX = 0;
761   WCHAR *retVal = pattern;
762   int id;
763   WCHAR letter = (WCHAR)L'a';
764
765   while(*pattern)
766     numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
767   if (numX < 5)
768     return NULL;
769   pattern--;
770   id = GetCurrentProcessId();
771   numX = 6;
772   while(numX--)
773   {
774     int tempNum = id / 10;
775     *pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
776     id = tempNum;
777   }
778   pattern++;
779   do
780   {
781     if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
782         GetLastError() == ERROR_FILE_NOT_FOUND)
783       return retVal;
784     *pattern = letter++;
785   } while(letter != (WCHAR)L'|');
786   return NULL;
787 }
788
789 /*********************************************************************
790  *              _open (MSVCRT.@)
791  */
792 int __cdecl MSVCRT__open(const char *path,int flags)
793 {
794   DWORD access = 0, creation = 0;
795   int ioflag = 0, fd;
796   HANDLE hand;
797
798   TRACE(":file (%s) mode 0x%04x\n",path,flags);
799
800   switch(flags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
801   {
802   case _O_RDONLY:
803     access |= GENERIC_READ;
804     ioflag |= _IOREAD;
805     break;
806   case _O_WRONLY:
807     access |= GENERIC_WRITE;
808     ioflag |= _IOWRT;
809     break;
810   case _O_RDWR:
811     access |= GENERIC_WRITE | GENERIC_READ;
812     ioflag |= _IORW;
813     break;
814   }
815
816   if (flags & _O_CREAT)
817   {
818     if (flags & _O_EXCL)
819       creation = CREATE_NEW;
820     else if (flags & _O_TRUNC)
821       creation = CREATE_ALWAYS;
822     else
823       creation = OPEN_ALWAYS;
824   }
825   else  /* no _O_CREAT */
826   {
827     if (flags & _O_TRUNC)
828       creation = TRUNCATE_EXISTING;
829     else
830       creation = OPEN_EXISTING;
831   }
832   if (flags & _O_APPEND)
833     ioflag |= _IOAPPEND;
834
835
836   flags |= _O_BINARY; /* FIXME: Default to text */
837
838   if (flags & _O_TEXT)
839   {
840     /* Dont warn when writing */
841     if (ioflag & GENERIC_READ)
842       FIXME(":TEXT node not implemented\n");
843     flags &= ~_O_TEXT;
844   }
845
846   if (flags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
847                 |_O_CREAT|_O_RDWR|_O_TEMPORARY))
848     TRACE(":unsupported flags 0x%04x\n",flags);
849
850   hand = CreateFileA(path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
851                       NULL, creation, FILE_ATTRIBUTE_NORMAL, 0);
852
853   if (hand == INVALID_HANDLE_VALUE)
854   {
855     WARN(":failed-last error (%ld)\n",GetLastError());
856     MSVCRT__set_errno(GetLastError());
857     return -1;
858   }
859
860   fd = MSVCRT__alloc_fd(hand, ioflag);
861
862   TRACE(":fd (%d) handle (%d)\n",fd, hand);
863
864   if (fd > 0)
865   {
866     if (flags & _O_TEMPORARY)
867       MSVCRT_tempfiles[fd] = MSVCRT__strdup(path);
868     if (ioflag & _IOAPPEND)
869       MSVCRT__lseek(fd, 0, FILE_END);
870   }
871
872   return fd;
873 }
874
875 /*********************************************************************
876  *              _wopen (MSVCRT.@)
877  */
878 int __cdecl MSVCRT__wopen(const WCHAR *path,int flags)
879 {
880   const unsigned int len = wcslen(path);
881   char *patha = MSVCRT_calloc(len + 1,1);
882   if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
883   {
884     int retval = MSVCRT__open(patha,flags);
885     MSVCRT_free(patha);
886     return retval;
887   }
888
889   MSVCRT__set_errno(GetLastError());
890   return -1;
891 }
892
893 /*********************************************************************
894  *              _creat (MSVCRT.@)
895  */
896 int __cdecl MSVCRT__creat(const char *path, int flags)
897 {
898   int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
899   return MSVCRT__open(path, usedFlags);
900 }
901
902 /*********************************************************************
903  *              _wcreat (MSVCRT.@)
904  */
905 int __cdecl MSVCRT__wcreat(const WCHAR *path, int flags)
906 {
907   int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
908   return MSVCRT__wopen(path, usedFlags);
909 }
910
911 /*********************************************************************
912  *              _open_osfhandle (MSVCRT.@)
913  */
914 int __cdecl MSVCRT__open_osfhandle(HANDLE hand, int flags)
915 {
916   int fd = MSVCRT__alloc_fd(hand,flags);
917   TRACE(":handle (%d) fd (%d)\n",hand,fd);
918   return fd;
919 }
920
921 /*********************************************************************
922  *              _rmtmp (MSVCRT.@)
923  */
924 int __cdecl MSVCRT__rmtmp(void)
925 {
926   int num_removed = 0, i;
927
928   for (i = 3; i < MSVCRT_fdend; i++)
929     if (MSVCRT_tempfiles[i])
930     {
931       MSVCRT__close(i);
932       num_removed++;
933     }
934
935   if (num_removed)
936     TRACE(":removed (%d) temp files\n",num_removed);
937   return num_removed;
938 }
939
940 /*********************************************************************
941  *              _read (MSVCRT.@)
942  */
943 int __cdecl MSVCRT__read(int fd, void *buf, unsigned int count)
944 {
945   DWORD num_read;
946   HANDLE hand = MSVCRT__fdtoh(fd);
947
948   /* Dont trace small reads, it gets *very* annoying */
949   if (count > 4)
950     TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
951   if (hand == INVALID_HANDLE_VALUE)
952     return -1;
953
954   /* Set _cnt to 0 so optimised binaries will call our implementation
955    * of putc/getc. See _filbuf/_flsbuf comments.
956    */
957   if (MSVCRT_files[fd])
958     MSVCRT_files[fd]->_cnt = 0;
959
960   if (ReadFile(hand, buf, count, &num_read, NULL))
961   {
962     if (num_read != count && MSVCRT_files[fd])
963     {
964       TRACE(":EOF\n");
965       MSVCRT_files[fd]->_flag |= _IOEOF;
966     }
967     return num_read;
968   }
969   TRACE(":failed-last error (%ld)\n",GetLastError());
970   if (MSVCRT_files[fd])
971      MSVCRT_files[fd]->_flag |= _IOERR;
972   return -1;
973 }
974
975 /*********************************************************************
976  *              _getw (MSVCRT.@)
977  */
978 int __cdecl MSVCRT__getw(MSVCRT_FILE* file)
979 {
980   int i;
981   if (MSVCRT__read(file->_file, &i, sizeof(int)) != 1)
982     return MSVCRT_EOF;
983   return i;
984 }
985
986 /*********************************************************************
987  *              _setmode (MSVCRT.@)
988  */
989 int __cdecl MSVCRT__setmode(int fd,int mode)
990 {
991   if (mode & _O_TEXT)
992     FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
993   return 0;
994 }
995
996 /*********************************************************************
997  *              _stat (MSVCRT.@)
998  */
999 int __cdecl MSVCRT__stat(const char* path, struct _stat * buf)
1000 {
1001   DWORD dw;
1002   WIN32_FILE_ATTRIBUTE_DATA hfi;
1003   unsigned short mode = MSVCRT_S_IREAD;
1004   int plen;
1005
1006   TRACE(":file (%s) buf(%p)\n",path,buf);
1007
1008   if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1009   {
1010       TRACE("failed (%ld)\n",GetLastError());
1011       MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1012       return -1;
1013   }
1014
1015   memset(buf,0,sizeof(struct _stat));
1016
1017   /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
1018   if (isalpha(*path))
1019     buf->st_dev = buf->st_rdev = toupper(*path - 'A'); /* drive num */
1020   else
1021     buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
1022
1023   plen = strlen(path);
1024
1025   /* Dir, or regular file? */
1026   if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1027       (path[plen-1] == '\\'))
1028     mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1029   else
1030   {
1031     mode |= _S_IFREG;
1032     /* executable? */
1033     if (plen > 6 && path[plen-4] == '.')  /* shortest exe: "\x.exe" */
1034     {
1035       unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1036                                  (tolower(path[plen-3]) << 16);
1037       if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1038           mode |= MSVCRT_S_IEXEC;
1039     }
1040   }
1041
1042   if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1043     mode |= MSVCRT_S_IWRITE;
1044
1045   buf->st_mode  = mode;
1046   buf->st_nlink = 1;
1047   buf->st_size  = hfi.nFileSizeLow;
1048   RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1049   buf->st_atime = dw;
1050   RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1051   buf->st_mtime = buf->st_ctime = dw;
1052   TRACE("\n%d %d %d %d %d %d\n", buf->st_mode,buf->st_nlink,buf->st_size,
1053     buf->st_atime,buf->st_mtime, buf->st_ctime);
1054   return 0;
1055 }
1056
1057 /*********************************************************************
1058  *              _wstat (MSVCRT.@)
1059  */
1060 int __cdecl MSVCRT__wstat(const WCHAR* path, struct _stat * buf)
1061 {
1062   DWORD dw;
1063   WIN32_FILE_ATTRIBUTE_DATA hfi;
1064   unsigned short mode = MSVCRT_S_IREAD;
1065   int plen;
1066
1067   TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1068
1069   if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1070   {
1071       TRACE("failed (%ld)\n",GetLastError());
1072       MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1073       return -1;
1074   }
1075
1076   memset(buf,0,sizeof(struct _stat));
1077
1078   /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
1079   if (iswalpha(*path))
1080     buf->st_dev = buf->st_rdev = towupper(*path - (WCHAR)L'A'); /* drive num */
1081   else
1082     buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1;
1083
1084   plen = wcslen(path);
1085
1086   /* Dir, or regular file? */
1087   if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1088       (path[plen-1] == (WCHAR)L'\\'))
1089     mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1090   else
1091   {
1092     mode |= _S_IFREG;
1093     /* executable? */
1094     if (plen > 6 && path[plen-4] == (WCHAR)L'.')  /* shortest exe: "\x.exe" */
1095     {
1096       ULONGLONG ext = towlower(path[plen-1]) | (towlower(path[plen-2]) << 16) |
1097                                ((ULONGLONG)towlower(path[plen-3]) << 32);
1098       if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1099         mode |= MSVCRT_S_IEXEC;
1100     }
1101   }
1102
1103   if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1104     mode |= MSVCRT_S_IWRITE;
1105
1106   buf->st_mode  = mode;
1107   buf->st_nlink = 1;
1108   buf->st_size  = hfi.nFileSizeLow;
1109   RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
1110   buf->st_atime = dw;
1111   RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
1112   buf->st_mtime = buf->st_ctime = dw;
1113   TRACE("\n%d %d %d %d %d %d\n", buf->st_mode,buf->st_nlink,buf->st_size,
1114         buf->st_atime,buf->st_mtime, buf->st_ctime);
1115   return 0;
1116 }
1117
1118 /*********************************************************************
1119  *              _tell (MSVCRT.@)
1120  */
1121 LONG __cdecl MSVCRT__tell(int fd)
1122 {
1123   return MSVCRT__lseek(fd, 0, SEEK_CUR);
1124 }
1125
1126 /*********************************************************************
1127  *              _tempnam (MSVCRT.@)
1128  */
1129 char *__cdecl MSVCRT__tempnam(const char *dir, const char *prefix)
1130 {
1131   char tmpbuf[MAX_PATH];
1132
1133   TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1134   if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1135   {
1136     TRACE("got name (%s)\n",tmpbuf);
1137     return MSVCRT__strdup(tmpbuf);
1138   }
1139   TRACE("failed (%ld)\n",GetLastError());
1140   return NULL;
1141 }
1142
1143 /*********************************************************************
1144  *              _wtempnam (MSVCRT.@)
1145  */
1146 WCHAR *__cdecl MSVCRT__wtempnam(const WCHAR *dir, const WCHAR *prefix)
1147 {
1148   WCHAR tmpbuf[MAX_PATH];
1149
1150   TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1151   if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1152   {
1153     TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1154     return MSVCRT__wcsdup(tmpbuf);
1155   }
1156   TRACE("failed (%ld)\n",GetLastError());
1157   return NULL;
1158 }
1159
1160 /*********************************************************************
1161  *              _umask (MSVCRT.@)
1162  */
1163 int __cdecl MSVCRT__umask(int umask)
1164 {
1165   int old_umask = MSVCRT_umask;
1166   TRACE("(%d)\n",umask);
1167   MSVCRT_umask = umask;
1168   return old_umask;
1169 }
1170
1171 /*********************************************************************
1172  *              _utime (MSVCRT.@)
1173  */
1174 int __cdecl MSVCRT__utime(const char *path, struct _utimbuf *t)
1175 {
1176   int fd = MSVCRT__open(path, _O_WRONLY | _O_BINARY);
1177
1178   if (fd > 0)
1179   {
1180     int retVal = MSVCRT__futime(fd, t);
1181     MSVCRT__close(fd);
1182     return retVal;
1183   }
1184   return -1;
1185 }
1186
1187 /*********************************************************************
1188  *              _wutime (MSVCRT.@)
1189  */
1190 int __cdecl MSVCRT__wutime(const WCHAR *path, struct _utimbuf *t)
1191 {
1192   int fd = MSVCRT__wopen(path, _O_WRONLY | _O_BINARY);
1193
1194   if (fd > 0)
1195   {
1196     int retVal = MSVCRT__futime(fd, t);
1197     MSVCRT__close(fd);
1198     return retVal;
1199   }
1200   return -1;
1201 }
1202
1203 /*********************************************************************
1204  *              _write (MSVCRT.@)
1205  */
1206 unsigned int __cdecl MSVCRT__write(int fd, const void *buf, unsigned int count)
1207 {
1208   DWORD num_written;
1209   HANDLE hand = MSVCRT__fdtoh(fd);
1210
1211   /* Dont trace small writes, it gets *very* annoying */
1212 //  if (count > 32)
1213 //    TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1214   if (hand == INVALID_HANDLE_VALUE)
1215     return -1;
1216
1217   /* If appending, go to EOF */
1218   if (MSVCRT_flags[fd] & _IOAPPEND)
1219     MSVCRT__lseek(fd, 0, FILE_END);
1220
1221   /* Set _cnt to 0 so optimised binaries will call our implementation
1222    * of putc/getc.
1223    */
1224   if (MSVCRT_files[fd])
1225     MSVCRT_files[fd]->_cnt = 0;
1226
1227   if (WriteFile(hand, buf, count, &num_written, NULL)
1228       &&  (num_written == count))
1229     return num_written;
1230
1231   TRACE(":failed-last error (%ld)\n",GetLastError());
1232   if (MSVCRT_files[fd])
1233      MSVCRT_files[fd]->_flag |= _IOERR;
1234
1235   return -1;
1236 }
1237
1238 /*********************************************************************
1239  *              _putw (MSVCRT.@)
1240  */
1241 int __cdecl MSVCRT__putw(int val, MSVCRT_FILE* file)
1242 {
1243   return MSVCRT__write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1244 }
1245
1246 /*********************************************************************
1247  *              clearerr (MSVCRT.@)
1248  */
1249 void __cdecl MSVCRT_clearerr(MSVCRT_FILE* file)
1250 {
1251   TRACE(":file (%p) fd (%d)\n",file,file->_file);
1252   file->_flag &= ~(_IOERR | _IOEOF);
1253 }
1254
1255 /*********************************************************************
1256  *              fclose (MSVCRT.@)
1257  */
1258 int __cdecl MSVCRT_fclose(MSVCRT_FILE* file)
1259 {
1260   return MSVCRT__close(file->_file);
1261 }
1262
1263 /*********************************************************************
1264  *              feof (MSVCRT.@)
1265  */
1266 int __cdecl MSVCRT_feof(MSVCRT_FILE* file)
1267 {
1268   return file->_flag & _IOEOF;
1269 }
1270
1271 /*********************************************************************
1272  *              ferror (MSVCRT.@)
1273  */
1274 int __cdecl MSVCRT_ferror(MSVCRT_FILE* file)
1275 {
1276   return file->_flag & _IOERR;
1277 }
1278
1279 /*********************************************************************
1280  *              fflush (MSVCRT.@)
1281  */
1282 int __cdecl MSVCRT_fflush(MSVCRT_FILE* file)
1283 {
1284   return MSVCRT__commit(file->_file);
1285 }
1286
1287 /*********************************************************************
1288  *              fgetc (MSVCRT.@)
1289  */
1290 int __cdecl MSVCRT_fgetc(MSVCRT_FILE* file)
1291 {
1292   char c;
1293   if (MSVCRT__read(file->_file,&c,1) != 1)
1294     return MSVCRT_EOF;
1295   return c;
1296 }
1297
1298 /*********************************************************************
1299  *              _fgetchar (MSVCRT.@)
1300  */
1301 int __cdecl MSVCRT__fgetchar(void)
1302 {
1303   return MSVCRT_fgetc(MSVCRT_stdin);
1304 }
1305
1306 /*********************************************************************
1307  *              _filbuf (MSVCRT.@)
1308  */
1309 int __cdecl MSVCRT__filbuf(MSVCRT_FILE* file)
1310 {
1311   return MSVCRT_fgetc(file);
1312 }
1313
1314 /*********************************************************************
1315  *              fgetpos (MSVCRT.@)
1316  */
1317 int __cdecl MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1318 {
1319   *pos = MSVCRT__tell(file->_file);
1320   return (*pos == -1? -1 : 0);
1321 }
1322
1323 /*********************************************************************
1324  *              fgets (MSVCRT.@)
1325  */
1326 char *__cdecl MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1327 {
1328   int    cc;
1329   char * buf_start = s;
1330
1331   TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1332         file,file->_file,s,size);
1333
1334   /* BAD, for the whole WINE process blocks... just done this way to test
1335    * windows95's ftp.exe.
1336    * JG - Is this true now we use ReadFile() on stdin too?
1337    */
1338   for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1339       cc = MSVCRT_fgetc(file))
1340     if (cc != '\r')
1341     {
1342       if (--size <= 0) break;
1343       *s++ = (char)cc;
1344     }
1345   if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1346   {
1347     TRACE(":nothing read\n");
1348     return 0;
1349   }
1350   if (cc == '\n')
1351     if (--size > 0)
1352       *s++ = '\n';
1353   *s = '\0';
1354   TRACE(":got '%s'\n", buf_start);
1355   return buf_start;
1356 }
1357
1358 /*********************************************************************
1359  *              fgetwc (MSVCRT.@)
1360  */
1361 WCHAR __cdecl MSVCRT_fgetwc(MSVCRT_FILE* file)
1362 {
1363   WCHAR wc;
1364   if (MSVCRT__read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1365     return MSVCRT_WEOF;
1366   return wc;
1367 }
1368
1369 /*********************************************************************
1370  *              getwc (MSVCRT.@)
1371  */
1372 WCHAR __cdecl MSVCRT_getwc(MSVCRT_FILE* file)
1373 {
1374   return MSVCRT_fgetwc(file);
1375 }
1376
1377 /*********************************************************************
1378  *              _fgetwchar (MSVCRT.@)
1379  */
1380 WCHAR __cdecl MSVCRT__fgetwchar(void)
1381 {
1382   return MSVCRT_fgetwc(MSVCRT_stdin);
1383 }
1384
1385 /*********************************************************************
1386  *              getwchar (MSVCRT.@)
1387  */
1388 WCHAR __cdecl MSVCRT_getwchar(void)
1389 {
1390   return MSVCRT__fgetwchar();
1391 }
1392
1393 /*********************************************************************
1394  *              fputwc (MSVCRT.@)
1395  */
1396 WCHAR __cdecl MSVCRT_fputwc(WCHAR wc, MSVCRT_FILE* file)
1397 {
1398   if (MSVCRT__write(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1399     return MSVCRT_WEOF;
1400   return wc;
1401 }
1402
1403 /*********************************************************************
1404  *              _fputwchar (MSVCRT.@)
1405  */
1406 WCHAR __cdecl MSVCRT__fputwchar(WCHAR wc)
1407 {
1408   return MSVCRT_fputwc(wc, MSVCRT_stdout);
1409 }
1410
1411 /*********************************************************************
1412  *              fopen (MSVCRT.@)
1413  */
1414 MSVCRT_FILE* __cdecl MSVCRT_fopen(const char *path, const char *mode)
1415 {
1416   MSVCRT_FILE* file;
1417   int flags = 0, plus = 0, fd;
1418   const char* search = mode;
1419
1420   TRACE("(%s,%s)\n",path,mode);
1421
1422   while (*search)
1423     if (*search++ == '+')
1424       plus = 1;
1425
1426   /* map mode string to open() flags. "man fopen" for possibilities. */
1427   switch(*mode++)
1428   {
1429   case 'R': case 'r':
1430     flags = (plus ? _O_RDWR : _O_RDONLY);
1431     break;
1432   case 'W': case 'w':
1433     flags = _O_CREAT | _O_TRUNC | (plus  ? _O_RDWR : _O_WRONLY);
1434     break;
1435   case 'A': case 'a':
1436     flags = _O_CREAT | _O_APPEND | (plus  ? _O_RDWR : _O_WRONLY);
1437     break;
1438   default:
1439     return NULL;
1440   }
1441
1442   while (*mode)
1443     switch (*mode++)
1444     {
1445     case 'B': case 'b':
1446       flags |=  _O_BINARY;
1447       flags &= ~_O_TEXT;
1448       break;
1449     case 'T': case 't':
1450       flags |=  _O_TEXT;
1451       flags &= ~_O_BINARY;
1452       break;
1453     case '+':
1454       break;
1455     default:
1456       FIXME(":unknown flag %c not supported\n",mode[-1]);
1457     }
1458
1459   fd = MSVCRT__open(path, flags);
1460
1461   if (fd < 0)
1462     return NULL;
1463
1464   file = MSVCRT__alloc_fp(fd);
1465   TRACE(":got (%p)\n",file);
1466   if (!file)
1467     MSVCRT__close(fd);
1468
1469   return file;
1470 }
1471
1472 /*********************************************************************
1473  *              _wfopen (MSVCRT.@)
1474  */
1475 MSVCRT_FILE *__cdecl MSVCRT__wfopen(const WCHAR *path, const WCHAR *mode)
1476 {
1477   const unsigned int plen = wcslen(path), mlen = wcslen(mode);
1478   char *patha = MSVCRT_calloc(plen + 1, 1);
1479   char *modea = MSVCRT_calloc(mlen + 1, 1);
1480
1481   TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1482
1483   if (patha && modea &&
1484       WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1485       WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1486   {
1487     MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1488     MSVCRT_free(patha);
1489     MSVCRT_free(modea);
1490     return retval;
1491   }
1492
1493   MSVCRT__set_errno(GetLastError());
1494   return NULL;
1495 }
1496
1497 /*********************************************************************
1498  *              _fsopen (MSVCRT.@)
1499  */
1500 MSVCRT_FILE*  __cdecl MSVCRT__fsopen(const char *path, const char *mode, int share)
1501 {
1502   FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1503   return MSVCRT_fopen(path,mode);
1504 }
1505
1506 /*********************************************************************
1507  *              _wfsopen (MSVCRT.@)
1508  */
1509 MSVCRT_FILE*  __cdecl MSVCRT__wfsopen(const WCHAR *path, const WCHAR *mode, int share)
1510 {
1511   FIXME(":(%s,%s,%d),ignoring share mode!\n",
1512         debugstr_w(path),debugstr_w(mode),share);
1513   return MSVCRT__wfopen(path,mode);
1514 }
1515
1516 /*********************************************************************
1517  *              fputc (MSVCRT.@)
1518  */
1519 int __cdecl MSVCRT_fputc(int c, MSVCRT_FILE* file)
1520 {
1521   return MSVCRT__write(file->_file, &c, 1) == 1? c : MSVCRT_EOF;
1522 }
1523
1524 /*********************************************************************
1525  *              _flsbuf (MSVCRT.@)
1526  */
1527 int __cdecl MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
1528 {
1529   return MSVCRT_fputc(c,file);
1530 }
1531
1532 /*********************************************************************
1533  *              _fputchar (MSVCRT.@)
1534  */
1535 int __cdecl MSVCRT__fputchar(int c)
1536 {
1537   return MSVCRT_fputc(c, MSVCRT_stdout);
1538 }
1539
1540 /*********************************************************************
1541  *              fread (MSVCRT.@)
1542  */
1543 DWORD __cdecl MSVCRT_fread(void *ptr, int size, int nmemb, MSVCRT_FILE* file)
1544 {
1545   DWORD read = MSVCRT__read(file->_file,ptr, size * nmemb);
1546   if (read <= 0)
1547     return 0;
1548   return read / size;
1549 }
1550
1551 /*********************************************************************
1552  *              freopen (MSVCRT.@)
1553  *
1554  */
1555 MSVCRT_FILE* __cdecl MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
1556 {
1557   MSVCRT_FILE* newfile;
1558   int fd;
1559
1560   TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
1561   if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
1562     return NULL;
1563
1564   if (fd > 2)
1565   {
1566     FIXME(":reopen on user file not implemented!\n");
1567     MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
1568     return NULL;
1569   }
1570
1571   /* first, create the new file */
1572   if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
1573     return NULL;
1574
1575   if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
1576      (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
1577       MSVCRT_handles[newfile->_file]))
1578   {
1579     /* Redirecting std handle to file , copy over.. */
1580     MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
1581     MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
1582     memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
1583     MSVCRT__iob[fd]._file = fd;
1584     /* And free up the resources allocated by fopen, but
1585      * not the HANDLE we copied. */
1586     MSVCRT_free(MSVCRT_files[fd]);
1587     MSVCRT__free_fd(newfile->_file);
1588     return &MSVCRT__iob[fd];
1589   }
1590
1591   WARN(":failed-last error (%ld)\n",GetLastError());
1592   MSVCRT_fclose(newfile);
1593   MSVCRT__set_errno(GetLastError());
1594   return NULL;
1595 }
1596
1597 /*********************************************************************
1598  *              fsetpos (MSVCRT.@)
1599  */
1600 int __cdecl MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1601 {
1602   return MSVCRT__lseek(file->_file,*pos,SEEK_SET);
1603 }
1604
1605 /*********************************************************************
1606  *              fscanf (MSVCRT.@)
1607  */
1608 int __cdecl MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
1609 {
1610     /* NOTE: If you extend this function, extend MSVCRT__cscanf in console.c too */
1611     int rd = 0;
1612     int nch;
1613     va_list ap;
1614     if (!*format) return 0;
1615     WARN("%p (\"%s\"): semi-stub\n", file, format);
1616     nch = MSVCRT_fgetc(file);
1617     va_start(ap, format);
1618     while (*format) {
1619         if (*format == ' ') {
1620             /* skip whitespace */
1621             while ((nch!=MSVCRT_EOF) && isspace(nch))
1622                 nch = MSVCRT_fgetc(file);
1623         }
1624         else if (*format == '%') {
1625             int st = 0;
1626             format++;
1627             switch(*format) {
1628             case 'd': { /* read an integer */
1629                     int*val = va_arg(ap, int*);
1630                     int cur = 0;
1631                     /* skip initial whitespace */
1632                     while ((nch!=MSVCRT_EOF) && isspace(nch))
1633                         nch = MSVCRT_fgetc(file);
1634                     /* get sign and first digit */
1635                     if (nch == '-') {
1636                         nch = MSVCRT_fgetc(file);
1637                         if (isdigit(nch))
1638                             cur = -(nch - '0');
1639                         else break;
1640                     } else {
1641                         if (isdigit(nch))
1642                             cur = nch - '0';
1643                         else break;
1644                     }
1645                     nch = MSVCRT_fgetc(file);
1646                     /* read until no more digits */
1647                     while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
1648                         cur = cur*10 + (nch - '0');
1649                         nch = MSVCRT_fgetc(file);
1650                     }
1651                     st = 1;
1652                     *val = cur;
1653                 }
1654                 break;
1655             case 'f': { /* read a float */
1656                     float*val = va_arg(ap, float*);
1657                     float cur = 0;
1658                     /* skip initial whitespace */
1659                     while ((nch!=MSVCRT_EOF) && isspace(nch))
1660                         nch = MSVCRT_fgetc(file);
1661                     /* get sign and first digit */
1662                     if (nch == '-') {
1663                         nch = MSVCRT_fgetc(file);
1664                         if (isdigit(nch))
1665                             cur = -(nch - '0');
1666                         else break;
1667                     } else {
1668                         if (isdigit(nch))
1669                             cur = nch - '0';
1670                         else break;
1671                     }
1672                     /* read until no more digits */
1673                     while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
1674                         cur = cur*10 + (nch - '0');
1675                         nch = MSVCRT_fgetc(file);
1676                     }
1677                     if (nch == '.') {
1678                         /* handle decimals */
1679                         float dec = 1;
1680                         nch = MSVCRT_fgetc(file);
1681                         while ((nch!=MSVCRT_EOF) && isdigit(nch)) {
1682                             dec /= 10;
1683                             cur += dec * (nch - '0');
1684                             nch = MSVCRT_fgetc(file);
1685                         }
1686                     }
1687                     st = 1;
1688                     *val = cur;
1689                 }
1690                 break;
1691             case 's': { /* read a word */
1692                     char*str = va_arg(ap, char*);
1693                     char*sptr = str;
1694                     /* skip initial whitespace */
1695                     while ((nch!=MSVCRT_EOF) && isspace(nch))
1696                         nch = MSVCRT_fgetc(file);
1697                     /* read until whitespace */
1698                     while ((nch!=MSVCRT_EOF) && !isspace(nch)) {
1699                         *sptr++ = nch; st++;
1700                         nch = MSVCRT_fgetc(file);
1701                     }
1702                     /* terminate */
1703                     *sptr = 0;
1704                     TRACE("read word: %s\n", str);
1705                 }
1706                 break;
1707             default: FIXME("unhandled: %%%c\n", *format);
1708             }
1709             if (st) rd++;
1710             else break;
1711         }
1712         else {
1713             /* check for character match */
1714             if (nch == *format)
1715                nch = MSVCRT_fgetc(file);
1716             else break;
1717         }
1718         format++;
1719     }
1720     va_end(ap);
1721     if (nch!=MSVCRT_EOF) {
1722         WARN("need ungetch\n");
1723     }
1724     TRACE("returning %d\n", rd);
1725     return rd;
1726 }
1727
1728 /*********************************************************************
1729  *              fseek (MSVCRT.@)
1730  */
1731 LONG __cdecl MSVCRT_fseek(MSVCRT_FILE* file, LONG offset, int whence)
1732 {
1733   return MSVCRT__lseek(file->_file,offset,whence);
1734 }
1735
1736 /*********************************************************************
1737  *              ftell (MSVCRT.@)
1738  */
1739 LONG __cdecl MSVCRT_ftell(MSVCRT_FILE* file)
1740 {
1741   return MSVCRT__tell(file->_file);
1742 }
1743
1744 /*********************************************************************
1745  *              fwrite (MSVCRT.@)
1746  */
1747 unsigned int __cdecl MSVCRT_fwrite(const void *ptr, int size, int nmemb, MSVCRT_FILE* file)
1748 {
1749   unsigned int written = MSVCRT__write(file->_file, ptr, size * nmemb);
1750   if (written <= 0)
1751     return 0;
1752   return written / size;
1753 }
1754
1755 /*********************************************************************
1756  *              fputs (MSVCRT.@)
1757  */
1758 int __cdecl MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
1759 {
1760   return MSVCRT_fwrite(s,strlen(s),1,file) == 1 ? 0 : MSVCRT_EOF;
1761 }
1762
1763 /*********************************************************************
1764  *              fputws (MSVCRT.@)
1765  */
1766 int __cdecl MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
1767 {
1768   return MSVCRT_fwrite(s,wcslen(s),1,file) == 1 ? 0 : MSVCRT_EOF;
1769 }
1770
1771 /*********************************************************************
1772  *              getchar (MSVCRT.@)
1773  */
1774 int __cdecl MSVCRT_getchar(void)
1775 {
1776   return MSVCRT_fgetc(MSVCRT_stdin);
1777 }
1778
1779 /*********************************************************************
1780  *              getc (MSVCRT.@)
1781  */
1782 int __cdecl MSVCRT_getc(MSVCRT_FILE* file)
1783 {
1784   return MSVCRT_fgetc(file);
1785 }
1786
1787 /*********************************************************************
1788  *              gets (MSVCRT.@)
1789  */
1790 char *__cdecl MSVCRT_gets(char *buf)
1791 {
1792   int    cc;
1793   char * buf_start = buf;
1794
1795   /* BAD, for the whole WINE process blocks... just done this way to test
1796    * windows95's ftp.exe.
1797    * JG 19/9/00: Is this still true, now we are using ReadFile?
1798    */
1799   for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
1800       cc = MSVCRT_fgetc(MSVCRT_stdin))
1801   if(cc != '\r') *buf++ = (char)cc;
1802
1803   *buf = '\0';
1804
1805   TRACE("got '%s'\n", buf_start);
1806   return buf_start;
1807 }
1808
1809 /*********************************************************************
1810  *              putc (MSVCRT.@)
1811  */
1812 int __cdecl MSVCRT_putc(int c, MSVCRT_FILE* file)
1813 {
1814   return MSVCRT_fputc(c, file);
1815 }
1816
1817 /*********************************************************************
1818  *              putchar (MSVCRT.@)
1819  */
1820 void __cdecl MSVCRT_putchar(int c)
1821 {
1822   MSVCRT_fputc(c, MSVCRT_stdout);
1823 }
1824
1825 /*********************************************************************
1826  *              puts (MSVCRT.@)
1827  */
1828 int __cdecl MSVCRT_puts(const char *s)
1829 {
1830   int retval = MSVCRT_EOF;
1831   if (MSVCRT_fwrite(s,strlen(s),1,MSVCRT_stdout) == 1)
1832     return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
1833   return retval;
1834 }
1835
1836 /*********************************************************************
1837  *              _putws (MSVCRT.@)
1838  */
1839 int __cdecl MSVCRT__putws(const WCHAR *s)
1840 {
1841   static const WCHAR nl = (WCHAR)L'\n';
1842   if (MSVCRT_fwrite(s,wcslen(s),1,MSVCRT_stdout) == 1)
1843     return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
1844   return MSVCRT_EOF;
1845 }
1846
1847 /*********************************************************************
1848  *              remove (MSVCRT.@)
1849  */
1850 int __cdecl MSVCRT_remove(const char *path)
1851 {
1852   TRACE("(%s)\n",path);
1853   if (DeleteFileA(path))
1854     return 0;
1855   TRACE(":failed (%ld)\n",GetLastError());
1856   MSVCRT__set_errno(GetLastError());
1857   return -1;
1858 }
1859
1860 /*********************************************************************
1861  *              _wremove (MSVCRT.@)
1862  */
1863 int __cdecl MSVCRT__wremove(const WCHAR *path)
1864 {
1865   TRACE("(%s)\n",debugstr_w(path));
1866   if (DeleteFileW(path))
1867     return 0;
1868   TRACE(":failed (%ld)\n",GetLastError());
1869   MSVCRT__set_errno(GetLastError());
1870   return -1;
1871 }
1872
1873 /*********************************************************************
1874  *              scanf (MSVCRT.@)
1875  */
1876 int __cdecl MSVCRT_scanf(const char *format, ...)
1877 {
1878   va_list valist;
1879   int res;
1880
1881   va_start(valist, format);
1882   res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
1883   va_end(valist);
1884   return res;
1885 }
1886
1887 /*********************************************************************
1888  *              rename (MSVCRT.@)
1889  */
1890 int __cdecl MSVCRT_rename(const char *oldpath,const char *newpath)
1891 {
1892   TRACE(":from %s to %s\n",oldpath,newpath);
1893   if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1894     return 0;
1895   TRACE(":failed (%ld)\n",GetLastError());
1896   MSVCRT__set_errno(GetLastError());
1897   return -1;
1898 }
1899
1900 /*********************************************************************
1901  *              _wrename (MSVCRT.@)
1902  */
1903 int __cdecl MSVCRT__wrename(const WCHAR *oldpath,const WCHAR *newpath)
1904 {
1905   TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
1906   if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
1907     return 0;
1908   TRACE(":failed (%ld)\n",GetLastError());
1909   MSVCRT__set_errno(GetLastError());
1910   return -1;
1911 }
1912
1913 /*********************************************************************
1914  *              setvbuf (MSVCRT.@)
1915  */
1916 void MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, unsigned int size)
1917 {
1918   FIXME("(%p,%p,%d,%d)stub\n",file, buf, mode, size);
1919 }
1920
1921 /*********************************************************************
1922  *              setbuf (MSVCRT.@)
1923  */
1924 void __cdecl MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
1925 {
1926   MSVCRT_setvbuf(file, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
1927 }
1928
1929 /*********************************************************************
1930  *              tmpnam (MSVCRT.@)
1931  */
1932 char *__cdecl MSVCRT_tmpnam(char *s)
1933 {
1934   char tmpbuf[MAX_PATH];
1935   char* prefix = "TMP";
1936   if (!GetTempPathA(MAX_PATH,tmpbuf) ||
1937       !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
1938   {
1939     TRACE(":failed-last error (%ld)\n",GetLastError());
1940     return NULL;
1941   }
1942   TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
1943   s = MSVCRT_tmpname;
1944   return s;
1945 }
1946
1947 /*********************************************************************
1948  *              tmpfile (MSVCRT.@)
1949  */
1950 MSVCRT_FILE* __cdecl MSVCRT_tmpfile(void)
1951 {
1952   char *filename = MSVCRT_tmpnam(NULL);
1953   int fd;
1954   fd = MSVCRT__open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
1955   if (fd != -1)
1956     return MSVCRT__alloc_fp(fd);
1957   return NULL;
1958 }
1959 extern int vsnprintf(void *, unsigned int, const void*, va_list);
1960
1961 /*********************************************************************
1962  *              vfprintf (MSVCRT.@)
1963  */
1964 int __cdecl MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
1965 {
1966   char buf[2048], *mem = buf;
1967   int written, resize = sizeof(buf), retval;
1968   /* There are two conventions for vsnprintf failing:
1969    * Return -1 if we truncated, or
1970    * Return the number of bytes that would have been written
1971    * The code below handles both cases
1972    */
1973   while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
1974           written > resize)
1975   {
1976     resize = (written == -1 ? resize * 2 : written + 1);
1977     if (mem != buf)
1978       MSVCRT_free (mem);
1979     if (!(mem = (char *)MSVCRT_malloc(resize)))
1980       return MSVCRT_EOF;
1981   }
1982   retval = MSVCRT_fwrite(mem, 1, written, file);
1983   if (mem != buf)
1984     MSVCRT_free (mem);
1985   return retval;
1986 }
1987
1988 /*********************************************************************
1989  *              vfwprintf (MSVCRT.@)
1990  */
1991 int __cdecl MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
1992 {
1993   WCHAR buf[2048], *mem = buf;
1994   int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
1995   /* See vfprintf comments */
1996   while ((written = MSVCRT__vsnwprintf(mem, resize, format, valist)) == -1 ||
1997           written > resize)
1998   {
1999     resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
2000     if (mem != buf)
2001       MSVCRT_free (mem);
2002     if (!(mem = (WCHAR *)MSVCRT_malloc(resize)))
2003       return MSVCRT_EOF;
2004   }
2005   retval = MSVCRT_fwrite(mem, 1, written * sizeof (WCHAR), file);
2006   if (mem != buf)
2007     MSVCRT_free (mem);
2008   return retval;
2009 }
2010
2011 /*********************************************************************
2012  *              vprintf (MSVCRT.@)
2013  */
2014 int __cdecl MSVCRT_vprintf(const char *format, va_list valist)
2015 {
2016   return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2017 }
2018
2019 /*********************************************************************
2020  *              vwprintf (MSVCRT.@)
2021  */
2022 int __cdecl MSVCRT_vwprintf(const WCHAR *format, va_list valist)
2023 {
2024   return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2025 }
2026
2027 /*********************************************************************
2028  *              fprintf (MSVCRT.@)
2029  */
2030 int __cdecl MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2031 {
2032     va_list valist;
2033     int res;
2034     va_start(valist, format);
2035     res = MSVCRT_vfprintf(file, format, valist);
2036     va_end(valist);
2037     return res;
2038 }
2039
2040 /*********************************************************************
2041  *              fwprintf (MSVCRT.@)
2042  */
2043 int __cdecl MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
2044 {
2045     va_list valist;
2046     int res;
2047     va_start(valist, format);
2048     res = MSVCRT_vfwprintf(file, format, valist);
2049     va_end(valist);
2050     return res;
2051 }
2052
2053 /*********************************************************************
2054  *              printf (MSVCRT.@)
2055  */
2056 int __cdecl MSVCRT_printf(const char *format, ...)
2057 {
2058     va_list valist;
2059     int res;
2060     va_start(valist, format);
2061     res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2062     va_end(valist);
2063     return res;
2064 }
2065
2066 /*********************************************************************
2067  *              wprintf (MSVCRT.@)
2068  */
2069 int __cdecl MSVCRT_wprintf(const WCHAR *format, ...)
2070 {
2071     va_list valist;
2072     int res;
2073     va_start(valist, format);
2074     res = MSVCRT_vwprintf(format, valist);
2075     va_end(valist);
2076     return res;
2077 }