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
14 DEFAULT_DEBUG_CHANNEL(msvcrt
);
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
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))
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 */
44 /* _access() bit flags FIXME: incomplete */
47 typedef struct _crtfile
59 /* file._flag flags */
60 #define _IOREAD 0x0001
65 #define _IOAPPEND 0x0200
77 #define MSVCRT_stdin (&MSVCRT__iob[0])
78 #define MSVCRT_stdout (&MSVCRT__iob[1])
79 #define MSVCRT_stderr (&MSVCRT__iob[2])
83 unsigned short st_dev
;
84 unsigned short st_ino
;
85 unsigned short st_mode
;
96 typedef long MSVCRT_fpos_t
;
104 /* FIXME: Make this dynamic */
105 #define MSVCRT_MAX_FILES 257
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];
113 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
114 static int MSVCRT_fdend
= 3; /* highest allocated fd */
116 /* INTERNAL: process umask */
117 static int MSVCRT_umask
= 0;
119 /* INTERNAL: Static buffer for temp file name */
120 static char MSVCRT_tmpname
[MAX_PATH
];
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';
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');
133 extern CRITICAL_SECTION MSVCRT_file_cs
;
134 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
135 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
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);
146 /* INTERNAL: Get the HANDLE for a fd */
147 static HANDLE
MSVCRT__fdtoh(int fd
)
149 if (fd
< 0 || fd
>= MSVCRT_fdend
||
150 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
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
;
157 return MSVCRT_handles
[fd
];
160 /* INTERNAL: free a file entry fd */
161 static void MSVCRT__free_fd(int fd
)
163 MSVCRT_handles
[fd
] = INVALID_HANDLE_VALUE
;
164 MSVCRT_files
[fd
] = 0;
165 MSVCRT_flags
[fd
] = 0;
166 TRACE(":fd (%d) freed\n",fd
);
168 return; /* dont use 0,1,2 for user files */
169 if (fd
== MSVCRT_fdend
- 1)
171 if (fd
< MSVCRT_fdstart
)
175 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
176 static int MSVCRT__alloc_fd(HANDLE hand
, int flag
)
178 int fd
= MSVCRT_fdstart
;
180 TRACE(":handle (%d) allocating fd (%d)\n",hand
,fd
);
181 if (fd
>= MSVCRT_MAX_FILES
)
183 WARN(":files exhausted!\n");
186 MSVCRT_handles
[fd
] = hand
;
187 MSVCRT_flags
[fd
] = flag
;
189 /* locate next free slot */
190 if (fd
== MSVCRT_fdend
)
191 MSVCRT_fdstart
= ++MSVCRT_fdend
;
193 while(MSVCRT_fdstart
< MSVCRT_fdend
&&
194 MSVCRT_handles
[MSVCRT_fdstart
] != INVALID_HANDLE_VALUE
)
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).
204 static MSVCRT_FILE
* MSVCRT__alloc_fp(int fd
)
206 TRACE(":fd (%d) allocating FILE*\n",fd
);
207 if (fd
< 0 || fd
>= MSVCRT_fdend
||
208 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
210 WARN(":invalid fd %d\n",fd
);
211 SET_THREAD_VAR(doserrno
,0);
212 SET_THREAD_VAR(errno
,MSVCRT_EBADF
);
215 if (!MSVCRT_files
[fd
])
217 if ((MSVCRT_files
[fd
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
219 MSVCRT_files
[fd
]->_file
= fd
;
220 MSVCRT_files
[fd
]->_flag
= MSVCRT_flags
[fd
];
221 MSVCRT_files
[fd
]->_flag
&= ~_IOAPPEND
; /* mask out, see above */
224 TRACE(":got FILE* (%p)\n",MSVCRT_files
[fd
]);
225 return MSVCRT_files
[fd
];
229 /* INTERNAL: Set up stdin, stderr and stdout */
230 void MSVCRT_init_io(void)
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
;
241 TRACE(":handles (%d)(%d)(%d)\n",MSVCRT_handles
[0],
242 MSVCRT_handles
[1],MSVCRT_handles
[2]);
244 for (i
= 0; i
< 3; i
++)
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
;
253 /*********************************************************************
256 MSVCRT_FILE
*MSVCRT___p__iob(void)
258 return &MSVCRT__iob
[0];
261 /*********************************************************************
264 int __cdecl
MSVCRT__access(const char *filename
, int mode
)
266 DWORD attr
= GetFileAttributesA(filename
);
268 TRACE("(%s,%d) %ld\n",filename
,mode
,attr
);
270 if (!filename
|| attr
== 0xffffffff)
272 MSVCRT__set_errno(GetLastError());
275 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
277 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
283 /*********************************************************************
284 * _waccess (MSVCRT.@)
286 int __cdecl
MSVCRT__waccess(const WCHAR
*filename
, int mode
)
288 DWORD attr
= GetFileAttributesW(filename
);
290 TRACE("(%s,%d) %ld\n",debugstr_w(filename
),mode
,attr
);
292 if (!filename
|| attr
== 0xffffffff)
294 MSVCRT__set_errno(GetLastError());
297 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
299 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
305 /*********************************************************************
308 int __cdecl
MSVCRT__chmod(const char *path
, int flags
)
310 DWORD oldFlags
= GetFileAttributesA(path
);
312 if (oldFlags
!= 0x0FFFFFFFF)
314 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
315 oldFlags
| FILE_ATTRIBUTE_READONLY
;
317 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
320 MSVCRT__set_errno(GetLastError());
324 /*********************************************************************
327 int __cdecl
MSVCRT__wchmod(const WCHAR
*path
, int flags
)
329 DWORD oldFlags
= GetFileAttributesW(path
);
331 if (oldFlags
!= 0x0FFFFFFFF)
333 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
334 oldFlags
| FILE_ATTRIBUTE_READONLY
;
336 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
339 MSVCRT__set_errno(GetLastError());
343 /*********************************************************************
346 int __cdecl
MSVCRT__unlink(const char *path
)
348 TRACE("(%s)\n",path
);
349 if(DeleteFileA(path
))
351 TRACE("failed (%ld)\n",GetLastError());
352 MSVCRT__set_errno(GetLastError());
356 /*********************************************************************
357 * _wunlink (MSVCRT.@)
359 int __cdecl
MSVCRT__wunlink(const WCHAR
*path
)
361 TRACE("(%s)\n",debugstr_w(path
));
362 if(DeleteFileW(path
))
364 TRACE("failed (%ld)\n",GetLastError());
365 MSVCRT__set_errno(GetLastError());
369 /*********************************************************************
372 int __cdecl
MSVCRT__close(int fd
)
374 HANDLE hand
= MSVCRT__fdtoh(fd
);
376 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
377 if (hand
== INVALID_HANDLE_VALUE
)
380 /* Dont free std FILE*'s, they are not dynamic */
381 if (fd
> 2 && MSVCRT_files
[fd
])
382 MSVCRT_free(MSVCRT_files
[fd
]);
386 if (!CloseHandle(hand
))
388 WARN(":failed-last error (%ld)\n",GetLastError());
389 MSVCRT__set_errno(GetLastError());
392 if (MSVCRT_tempfiles
[fd
])
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
;
404 /*********************************************************************
407 int __cdecl
MSVCRT__commit(int fd
)
409 HANDLE hand
= MSVCRT__fdtoh(fd
);
411 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
412 if (hand
== INVALID_HANDLE_VALUE
)
415 if (!FlushFileBuffers(hand
))
417 if (GetLastError() == ERROR_INVALID_HANDLE
)
419 /* FlushFileBuffers fails for console handles
420 * so we ignore this error.
424 TRACE(":failed-last error (%ld)\n",GetLastError());
425 MSVCRT__set_errno(GetLastError());
432 /*********************************************************************
435 int __cdecl
MSVCRT__eof(int fd
)
438 HANDLE hand
= MSVCRT__fdtoh(fd
);
440 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
442 if (hand
== INVALID_HANDLE_VALUE
)
445 /* If we have a FILE* for this file, the EOF flag
446 * will be set by the read()/write() functions.
448 if (MSVCRT_files
[fd
])
449 return MSVCRT_files
[fd
]->_flag
& _IOEOF
;
451 /* Otherwise we do it the hard way */
452 curpos
= SetFilePointer(hand
, 0, NULL
, SEEK_CUR
);
453 endpos
= SetFilePointer(hand
, 0, NULL
, FILE_END
);
455 if (curpos
== endpos
)
458 SetFilePointer(hand
, curpos
, 0, FILE_BEGIN
);
462 /*********************************************************************
463 * _fcloseall (MSVCRT.@)
465 int __cdecl
MSVCRT__fcloseall(void)
467 int num_closed
= 0, i
;
469 for (i
= 3; i
< MSVCRT_fdend
; i
++)
470 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
476 TRACE(":closed (%d) handles\n",num_closed
);
480 /*********************************************************************
483 LONG __cdecl
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
486 HANDLE hand
= MSVCRT__fdtoh(fd
);
488 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
489 if (hand
== INVALID_HANDLE_VALUE
)
492 if (whence
< 0 || whence
> 2)
494 SET_THREAD_VAR(errno
,MSVCRT_EINVAL
);
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");
503 if ((ret
= SetFilePointer(hand
, offset
, NULL
, whence
)) != 0xffffffff)
505 if (MSVCRT_files
[fd
])
506 MSVCRT_files
[fd
]->_flag
&= ~_IOEOF
;
507 /* FIXME: What if we seek _to_ EOF - is EOF set? */
510 TRACE(":error-last error (%ld)\n",GetLastError());
511 if (MSVCRT_files
[fd
])
512 switch(GetLastError())
514 case ERROR_NEGATIVE_SEEK
:
515 case ERROR_SEEK_ON_DEVICE
:
516 MSVCRT__set_errno(GetLastError());
517 MSVCRT_files
[fd
]->_flag
|= _IOERR
;
525 /*********************************************************************
528 void __cdecl
MSVCRT_rewind(MSVCRT_FILE
* file
)
530 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
531 MSVCRT__lseek(file
->_file
,0,SEEK_SET
);
532 file
->_flag
&= ~(_IOEOF
| _IOERR
);
535 /*********************************************************************
538 MSVCRT_FILE
* __cdecl
MSVCRT__fdopen(int fd
, const char *mode
)
540 MSVCRT_FILE
* file
= MSVCRT__alloc_fp(fd
);
542 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
549 /*********************************************************************
550 * _wfdopen (MSVCRT.@)
552 MSVCRT_FILE
* __cdecl
MSVCRT__wfdopen(int fd
, const WCHAR
*mode
)
554 MSVCRT_FILE
* file
= MSVCRT__alloc_fp(fd
);
556 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
563 /*********************************************************************
564 * _filelength (MSVCRT.@)
566 LONG __cdecl
MSVCRT__filelength(int fd
)
568 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
571 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
574 if (endPos
!= curPos
)
575 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
582 /*********************************************************************
585 int __cdecl
MSVCRT__fileno(MSVCRT_FILE
* file
)
587 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
591 /*********************************************************************
592 * _flushall (MSVCRT.@)
594 int __cdecl
MSVCRT__flushall(void)
596 int num_flushed
= 0, i
= 3;
598 while(i
< MSVCRT_fdend
)
599 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
601 if (MSVCRT__commit(i
) == -1)
603 MSVCRT_files
[i
]->_flag
|= _IOERR
;
607 TRACE(":flushed (%d) handles\n",num_flushed
);
611 /*********************************************************************
614 int __cdecl
MSVCRT__fstat(int fd
, struct _stat
* buf
)
617 BY_HANDLE_FILE_INFORMATION hfi
;
618 HANDLE hand
= MSVCRT__fdtoh(fd
);
620 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
621 if (hand
== INVALID_HANDLE_VALUE
)
626 WARN(":failed-NULL buf\n");
627 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
631 memset(&hfi
, 0, sizeof(hfi
));
632 memset(buf
, 0, sizeof(struct _stat
));
633 if (!GetFileInformationByHandle(hand
, &hfi
))
635 WARN(":failed-last error (%ld)\n",GetLastError());
636 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
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
);
644 RtlTimeToSecondsSince1970(&hfi
.ftLastWriteTime
, &dw
);
645 buf
->st_mtime
= buf
->st_ctime
= dw
;
649 /*********************************************************************
652 int __cdecl
MSVCRT__futime(int fd
, struct _utimbuf
*t
)
654 HANDLE hand
= MSVCRT__fdtoh(fd
);
660 MSVCRT_time(&currTime
);
661 RtlSecondsSince1970ToTime(currTime
, &at
);
662 memcpy(&wt
, &at
, sizeof(wt
));
666 RtlSecondsSince1970ToTime(t
->actime
, &at
);
667 if (t
->actime
== t
->modtime
)
668 memcpy(&wt
, &at
, sizeof(wt
));
670 RtlSecondsSince1970ToTime(t
->modtime
, &wt
);
673 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
675 MSVCRT__set_errno(GetLastError());
681 /*********************************************************************
682 * _get_osfhandle (MSVCRT.@)
684 HANDLE
MSVCRT__get_osfhandle(int fd
)
686 HANDLE hand
= MSVCRT__fdtoh(fd
);
687 HANDLE newhand
= hand
;
688 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
690 if (hand
!= INVALID_HANDLE_VALUE
)
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.
701 DuplicateHandle(GetCurrentProcess(),hand
,GetCurrentProcess(),
702 &newhand
,0,TRUE
,DUPLICATE_SAME_ACCESS
);
707 /*********************************************************************
710 int __cdecl
MSVCRT__isatty(int fd
)
712 HANDLE hand
= MSVCRT__fdtoh(fd
);
714 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
715 if (hand
== INVALID_HANDLE_VALUE
)
718 return GetFileType(fd
) == FILE_TYPE_CHAR
? 1 : 0;
721 /*********************************************************************
724 char *__cdecl
MSVCRT__mktemp(char *pattern
)
727 char *retVal
= pattern
;
732 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
736 id
= GetCurrentProcessId();
740 int tempNum
= id
/ 10;
741 *pattern
-- = id
- (tempNum
* 10) + '0';
747 if (GetFileAttributesA(retVal
) == 0xFFFFFFFF &&
748 GetLastError() == ERROR_FILE_NOT_FOUND
)
751 } while(letter
!= '|');
755 /*********************************************************************
756 * _wmktemp (MSVCRT.@)
758 WCHAR
*__cdecl
MSVCRT__wmktemp(WCHAR
*pattern
)
761 WCHAR
*retVal
= pattern
;
763 WCHAR letter
= (WCHAR
)L
'a';
766 numX
= (*pattern
++ == (WCHAR
)L
'X')? numX
+ 1 : 0;
770 id
= GetCurrentProcessId();
774 int tempNum
= id
/ 10;
775 *pattern
-- = id
- (tempNum
* 10) + (WCHAR
)L
'0';
781 if (GetFileAttributesW(retVal
) == 0xFFFFFFFF &&
782 GetLastError() == ERROR_FILE_NOT_FOUND
)
785 } while(letter
!= (WCHAR
)L
'|');
789 /*********************************************************************
792 int __cdecl
MSVCRT__open(const char *path
,int flags
)
794 DWORD access
= 0, creation
= 0;
798 TRACE(":file (%s) mode 0x%04x\n",path
,flags
);
800 switch(flags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
803 access
|= GENERIC_READ
;
807 access
|= GENERIC_WRITE
;
811 access
|= GENERIC_WRITE
| GENERIC_READ
;
816 if (flags
& _O_CREAT
)
819 creation
= CREATE_NEW
;
820 else if (flags
& _O_TRUNC
)
821 creation
= CREATE_ALWAYS
;
823 creation
= OPEN_ALWAYS
;
825 else /* no _O_CREAT */
827 if (flags
& _O_TRUNC
)
828 creation
= TRUNCATE_EXISTING
;
830 creation
= OPEN_EXISTING
;
832 if (flags
& _O_APPEND
)
836 flags
|= _O_BINARY
; /* FIXME: Default to text */
840 /* Dont warn when writing */
841 if (ioflag
& GENERIC_READ
)
842 FIXME(":TEXT node not implemented\n");
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
);
850 hand
= CreateFileA(path
, access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
851 NULL
, creation
, FILE_ATTRIBUTE_NORMAL
, 0);
853 if (hand
== INVALID_HANDLE_VALUE
)
855 WARN(":failed-last error (%ld)\n",GetLastError());
856 MSVCRT__set_errno(GetLastError());
860 fd
= MSVCRT__alloc_fd(hand
, ioflag
);
862 TRACE(":fd (%d) handle (%d)\n",fd
, hand
);
866 if (flags
& _O_TEMPORARY
)
867 MSVCRT_tempfiles
[fd
] = MSVCRT__strdup(path
);
868 if (ioflag
& _IOAPPEND
)
869 MSVCRT__lseek(fd
, 0, FILE_END
);
875 /*********************************************************************
878 int __cdecl
MSVCRT__wopen(const WCHAR
*path
,int flags
)
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
))
884 int retval
= MSVCRT__open(patha
,flags
);
889 MSVCRT__set_errno(GetLastError());
893 /*********************************************************************
896 int __cdecl
MSVCRT__sopen(const char *path
,int oflags
,int shflags
)
898 return MSVCRT__open(path
, oflags
| shflags
);
901 /*********************************************************************
904 int __cdecl
MSVCRT__creat(const char *path
, int flags
)
906 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
907 return MSVCRT__open(path
, usedFlags
);
910 /*********************************************************************
913 int __cdecl
MSVCRT__wcreat(const WCHAR
*path
, int flags
)
915 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
916 return MSVCRT__wopen(path
, usedFlags
);
919 /*********************************************************************
920 * _open_osfhandle (MSVCRT.@)
922 int __cdecl
MSVCRT__open_osfhandle(HANDLE hand
, int flags
)
924 int fd
= MSVCRT__alloc_fd(hand
,flags
);
925 TRACE(":handle (%d) fd (%d)\n",hand
,fd
);
929 /*********************************************************************
932 int __cdecl
MSVCRT__rmtmp(void)
934 int num_removed
= 0, i
;
936 for (i
= 3; i
< MSVCRT_fdend
; i
++)
937 if (MSVCRT_tempfiles
[i
])
944 TRACE(":removed (%d) temp files\n",num_removed
);
948 /*********************************************************************
951 int __cdecl
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
954 HANDLE hand
= MSVCRT__fdtoh(fd
);
956 /* Dont trace small reads, it gets *very* annoying */
958 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
959 if (hand
== INVALID_HANDLE_VALUE
)
962 /* Set _cnt to 0 so optimised binaries will call our implementation
963 * of putc/getc. See _filbuf/_flsbuf comments.
965 if (MSVCRT_files
[fd
])
966 MSVCRT_files
[fd
]->_cnt
= 0;
968 if (ReadFile(hand
, buf
, count
, &num_read
, NULL
))
970 if (num_read
!= count
&& MSVCRT_files
[fd
])
973 MSVCRT_files
[fd
]->_flag
|= _IOEOF
;
977 TRACE(":failed-last error (%ld)\n",GetLastError());
978 if (MSVCRT_files
[fd
])
979 MSVCRT_files
[fd
]->_flag
|= _IOERR
;
983 /*********************************************************************
986 int __cdecl
MSVCRT__getw(MSVCRT_FILE
* file
)
989 if (MSVCRT__read(file
->_file
, &i
, sizeof(int)) != 1)
994 /*********************************************************************
995 * _setmode (MSVCRT.@)
997 int __cdecl
MSVCRT__setmode(int fd
,int mode
)
1000 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd
,mode
);
1004 /*********************************************************************
1007 int __cdecl
MSVCRT__stat(const char* path
, struct _stat
* buf
)
1010 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1011 unsigned short mode
= MSVCRT_S_IREAD
;
1014 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1016 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1018 TRACE("failed (%ld)\n",GetLastError());
1019 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1023 memset(buf
,0,sizeof(struct _stat
));
1025 /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
1027 buf
->st_dev
= buf
->st_rdev
= toupper(*path
- 'A'); /* drive num */
1029 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
1031 plen
= strlen(path
);
1033 /* Dir, or regular file? */
1034 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1035 (path
[plen
-1] == '\\'))
1036 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1041 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1043 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1044 (tolower(path
[plen
-3]) << 16);
1045 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1046 mode
|= MSVCRT_S_IEXEC
;
1050 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1051 mode
|= MSVCRT_S_IWRITE
;
1053 buf
->st_mode
= mode
;
1055 buf
->st_size
= hfi
.nFileSizeLow
;
1056 RtlTimeToSecondsSince1970(&hfi
.ftLastAccessTime
, &dw
);
1058 RtlTimeToSecondsSince1970(&hfi
.ftLastWriteTime
, &dw
);
1059 buf
->st_mtime
= buf
->st_ctime
= dw
;
1060 TRACE("\n%d %d %d %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,buf
->st_size
,
1061 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1065 /*********************************************************************
1068 int __cdecl
MSVCRT__wstat(const WCHAR
* path
, struct _stat
* buf
)
1071 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1072 unsigned short mode
= MSVCRT_S_IREAD
;
1075 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1077 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1079 TRACE("failed (%ld)\n",GetLastError());
1080 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1084 memset(buf
,0,sizeof(struct _stat
));
1086 /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
1087 if (iswalpha(*path
))
1088 buf
->st_dev
= buf
->st_rdev
= towupper(*path
- (WCHAR
)L
'A'); /* drive num */
1090 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
1092 plen
= wcslen(path
);
1094 /* Dir, or regular file? */
1095 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1096 (path
[plen
-1] == (WCHAR
)L
'\\'))
1097 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1102 if (plen
> 6 && path
[plen
-4] == (WCHAR
)L
'.') /* shortest exe: "\x.exe" */
1104 ULONGLONG ext
= towlower(path
[plen
-1]) | (towlower(path
[plen
-2]) << 16) |
1105 ((ULONGLONG
)towlower(path
[plen
-3]) << 32);
1106 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1107 mode
|= MSVCRT_S_IEXEC
;
1111 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1112 mode
|= MSVCRT_S_IWRITE
;
1114 buf
->st_mode
= mode
;
1116 buf
->st_size
= hfi
.nFileSizeLow
;
1117 RtlTimeToSecondsSince1970(&hfi
.ftLastAccessTime
, &dw
);
1119 RtlTimeToSecondsSince1970(&hfi
.ftLastWriteTime
, &dw
);
1120 buf
->st_mtime
= buf
->st_ctime
= dw
;
1121 TRACE("\n%d %d %d %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,buf
->st_size
,
1122 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1126 /*********************************************************************
1129 LONG __cdecl
MSVCRT__tell(int fd
)
1131 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1134 /*********************************************************************
1135 * _tempnam (MSVCRT.@)
1137 char *__cdecl
MSVCRT__tempnam(const char *dir
, const char *prefix
)
1139 char tmpbuf
[MAX_PATH
];
1141 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1142 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1144 TRACE("got name (%s)\n",tmpbuf
);
1145 return MSVCRT__strdup(tmpbuf
);
1147 TRACE("failed (%ld)\n",GetLastError());
1151 /*********************************************************************
1152 * _wtempnam (MSVCRT.@)
1154 WCHAR
*__cdecl
MSVCRT__wtempnam(const WCHAR
*dir
, const WCHAR
*prefix
)
1156 WCHAR tmpbuf
[MAX_PATH
];
1158 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1159 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1161 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1162 return MSVCRT__wcsdup(tmpbuf
);
1164 TRACE("failed (%ld)\n",GetLastError());
1168 /*********************************************************************
1171 int __cdecl
MSVCRT__umask(int umask
)
1173 int old_umask
= MSVCRT_umask
;
1174 TRACE("(%d)\n",umask
);
1175 MSVCRT_umask
= umask
;
1179 /*********************************************************************
1182 int __cdecl
MSVCRT__utime(const char *path
, struct _utimbuf
*t
)
1184 int fd
= MSVCRT__open(path
, _O_WRONLY
| _O_BINARY
);
1188 int retVal
= MSVCRT__futime(fd
, t
);
1195 /*********************************************************************
1196 * _wutime (MSVCRT.@)
1198 int __cdecl
MSVCRT__wutime(const WCHAR
*path
, struct _utimbuf
*t
)
1200 int fd
= MSVCRT__wopen(path
, _O_WRONLY
| _O_BINARY
);
1204 int retVal
= MSVCRT__futime(fd
, t
);
1211 /*********************************************************************
1214 unsigned int __cdecl
MSVCRT__write(int fd
, const void *buf
, unsigned int count
)
1217 HANDLE hand
= MSVCRT__fdtoh(fd
);
1219 /* Dont trace small writes, it gets *very* annoying */
1221 // TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1222 if (hand
== INVALID_HANDLE_VALUE
)
1225 /* If appending, go to EOF */
1226 if (MSVCRT_flags
[fd
] & _IOAPPEND
)
1227 MSVCRT__lseek(fd
, 0, FILE_END
);
1229 /* Set _cnt to 0 so optimised binaries will call our implementation
1232 if (MSVCRT_files
[fd
])
1233 MSVCRT_files
[fd
]->_cnt
= 0;
1235 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1236 && (num_written
== count
))
1239 TRACE(":failed-last error (%ld)\n",GetLastError());
1240 if (MSVCRT_files
[fd
])
1241 MSVCRT_files
[fd
]->_flag
|= _IOERR
;
1246 /*********************************************************************
1249 int __cdecl
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
1251 return MSVCRT__write(file
->_file
, &val
, sizeof(val
)) == 1? val
: MSVCRT_EOF
;
1254 /*********************************************************************
1255 * clearerr (MSVCRT.@)
1257 void __cdecl
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1259 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1260 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1263 /*********************************************************************
1266 int __cdecl
MSVCRT_fclose(MSVCRT_FILE
* file
)
1268 return MSVCRT__close(file
->_file
);
1271 /*********************************************************************
1274 int __cdecl
MSVCRT_feof(MSVCRT_FILE
* file
)
1276 return file
->_flag
& _IOEOF
;
1279 /*********************************************************************
1282 int __cdecl
MSVCRT_ferror(MSVCRT_FILE
* file
)
1284 return file
->_flag
& _IOERR
;
1287 /*********************************************************************
1290 int __cdecl
MSVCRT_fflush(MSVCRT_FILE
* file
)
1292 return MSVCRT__commit(file
->_file
);
1295 /*********************************************************************
1298 int __cdecl
MSVCRT_fgetc(MSVCRT_FILE
* file
)
1301 if (MSVCRT__read(file
->_file
,&c
,1) != 1)
1306 /*********************************************************************
1307 * _fgetchar (MSVCRT.@)
1309 int __cdecl
MSVCRT__fgetchar(void)
1311 return MSVCRT_fgetc(MSVCRT_stdin
);
1314 /*********************************************************************
1315 * _filbuf (MSVCRT.@)
1317 int __cdecl
MSVCRT__filbuf(MSVCRT_FILE
* file
)
1319 return MSVCRT_fgetc(file
);
1322 /*********************************************************************
1323 * fgetpos (MSVCRT.@)
1325 int __cdecl
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
1327 *pos
= MSVCRT__tell(file
->_file
);
1328 return (*pos
== -1? -1 : 0);
1331 /*********************************************************************
1334 char *__cdecl
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
1337 char * buf_start
= s
;
1339 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1340 file
,file
->_file
,s
,size
);
1342 /* BAD, for the whole WINE process blocks... just done this way to test
1343 * windows95's ftp.exe.
1344 * JG - Is this true now we use ReadFile() on stdin too?
1346 for(cc
= MSVCRT_fgetc(file
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
1347 cc
= MSVCRT_fgetc(file
))
1350 if (--size
<= 0) break;
1353 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1355 TRACE(":nothing read\n");
1362 TRACE(":got '%s'\n", buf_start
);
1366 /*********************************************************************
1369 WCHAR __cdecl
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
1372 if (MSVCRT__read(file
->_file
, &wc
, sizeof(wc
)) != sizeof(wc
))
1377 /*********************************************************************
1380 WCHAR __cdecl
MSVCRT_getwc(MSVCRT_FILE
* file
)
1382 return MSVCRT_fgetwc(file
);
1385 /*********************************************************************
1386 * _fgetwchar (MSVCRT.@)
1388 WCHAR __cdecl
MSVCRT__fgetwchar(void)
1390 return MSVCRT_fgetwc(MSVCRT_stdin
);
1393 /*********************************************************************
1394 * getwchar (MSVCRT.@)
1396 WCHAR __cdecl
MSVCRT_getwchar(void)
1398 return MSVCRT__fgetwchar();
1401 /*********************************************************************
1404 WCHAR __cdecl
MSVCRT_fputwc(WCHAR wc
, MSVCRT_FILE
* file
)
1406 if (MSVCRT__write(file
->_file
, &wc
, sizeof(wc
)) != sizeof(wc
))
1411 /*********************************************************************
1412 * _fputwchar (MSVCRT.@)
1414 WCHAR __cdecl
MSVCRT__fputwchar(WCHAR wc
)
1416 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
1419 /*********************************************************************
1422 MSVCRT_FILE
* __cdecl
MSVCRT_fopen(const char *path
, const char *mode
)
1425 int flags
= 0, plus
= 0, fd
;
1426 const char* search
= mode
;
1428 TRACE("(%s,%s)\n",path
,mode
);
1431 if (*search
++ == '+')
1434 /* map mode string to open() flags. "man fopen" for possibilities. */
1438 flags
= (plus
? _O_RDWR
: _O_RDONLY
);
1441 flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1444 flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1459 flags
&= ~_O_BINARY
;
1464 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1467 fd
= MSVCRT__open(path
, flags
);
1472 file
= MSVCRT__alloc_fp(fd
);
1473 TRACE(":got (%p)\n",file
);
1480 /*********************************************************************
1481 * _wfopen (MSVCRT.@)
1483 MSVCRT_FILE
*__cdecl
MSVCRT__wfopen(const WCHAR
*path
, const WCHAR
*mode
)
1485 const unsigned int plen
= wcslen(path
), mlen
= wcslen(mode
);
1486 char *patha
= MSVCRT_calloc(plen
+ 1, 1);
1487 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
1489 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
1491 if (patha
&& modea
&&
1492 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
1493 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
1495 MSVCRT_FILE
*retval
= MSVCRT_fopen(patha
,modea
);
1501 MSVCRT__set_errno(GetLastError());
1505 /*********************************************************************
1506 * _fsopen (MSVCRT.@)
1508 MSVCRT_FILE
* __cdecl
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
1510 FIXME(":(%s,%s,%d),ignoring share mode!\n",path
,mode
,share
);
1511 return MSVCRT_fopen(path
,mode
);
1514 /*********************************************************************
1515 * _wfsopen (MSVCRT.@)
1517 MSVCRT_FILE
* __cdecl
MSVCRT__wfsopen(const WCHAR
*path
, const WCHAR
*mode
, int share
)
1519 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1520 debugstr_w(path
),debugstr_w(mode
),share
);
1521 return MSVCRT__wfopen(path
,mode
);
1524 /*********************************************************************
1527 int __cdecl
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
1529 return MSVCRT__write(file
->_file
, &c
, 1) == 1? c
: MSVCRT_EOF
;
1532 /*********************************************************************
1533 * _flsbuf (MSVCRT.@)
1535 int __cdecl
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
1537 return MSVCRT_fputc(c
,file
);
1540 /*********************************************************************
1541 * _fputchar (MSVCRT.@)
1543 int __cdecl
MSVCRT__fputchar(int c
)
1545 return MSVCRT_fputc(c
, MSVCRT_stdout
);
1548 /*********************************************************************
1551 DWORD __cdecl
MSVCRT_fread(void *ptr
, int size
, int nmemb
, MSVCRT_FILE
* file
)
1553 DWORD read
= MSVCRT__read(file
->_file
,ptr
, size
* nmemb
);
1559 /*********************************************************************
1560 * freopen (MSVCRT.@)
1563 MSVCRT_FILE
* __cdecl
MSVCRT_freopen(const char *path
, const char *mode
,MSVCRT_FILE
* file
)
1565 MSVCRT_FILE
* newfile
;
1568 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
1569 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
1574 FIXME(":reopen on user file not implemented!\n");
1575 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED
);
1579 /* first, create the new file */
1580 if ((newfile
= MSVCRT_fopen(path
,mode
)) == NULL
)
1583 if (fd
< 3 && SetStdHandle(fd
== 0 ? STD_INPUT_HANDLE
:
1584 (fd
== 1? STD_OUTPUT_HANDLE
: STD_ERROR_HANDLE
),
1585 MSVCRT_handles
[newfile
->_file
]))
1587 /* Redirecting std handle to file , copy over.. */
1588 MSVCRT_handles
[fd
] = MSVCRT_handles
[newfile
->_file
];
1589 MSVCRT_flags
[fd
] = MSVCRT_flags
[newfile
->_file
];
1590 memcpy(&MSVCRT__iob
[fd
], newfile
, sizeof (MSVCRT_FILE
));
1591 MSVCRT__iob
[fd
]._file
= fd
;
1592 /* And free up the resources allocated by fopen, but
1593 * not the HANDLE we copied. */
1594 MSVCRT_free(MSVCRT_files
[fd
]);
1595 MSVCRT__free_fd(newfile
->_file
);
1596 return &MSVCRT__iob
[fd
];
1599 WARN(":failed-last error (%ld)\n",GetLastError());
1600 MSVCRT_fclose(newfile
);
1601 MSVCRT__set_errno(GetLastError());
1605 /*********************************************************************
1606 * fsetpos (MSVCRT.@)
1608 int __cdecl
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
1610 return MSVCRT__lseek(file
->_file
,*pos
,SEEK_SET
);
1613 /*********************************************************************
1616 int __cdecl
MSVCRT_fscanf(MSVCRT_FILE
* file
, const char *format
, ...)
1618 /* NOTE: If you extend this function, extend MSVCRT__cscanf in console.c too */
1622 if (!*format
) return 0;
1623 WARN("%p (\"%s\"): semi-stub\n", file
, format
);
1624 nch
= MSVCRT_fgetc(file
);
1625 va_start(ap
, format
);
1627 if (*format
== ' ') {
1628 /* skip whitespace */
1629 while ((nch
!=MSVCRT_EOF
) && isspace(nch
))
1630 nch
= MSVCRT_fgetc(file
);
1632 else if (*format
== '%') {
1636 case 'd': { /* read an integer */
1637 int*val
= va_arg(ap
, int*);
1639 /* skip initial whitespace */
1640 while ((nch
!=MSVCRT_EOF
) && isspace(nch
))
1641 nch
= MSVCRT_fgetc(file
);
1642 /* get sign and first digit */
1644 nch
= MSVCRT_fgetc(file
);
1653 nch
= MSVCRT_fgetc(file
);
1654 /* read until no more digits */
1655 while ((nch
!=MSVCRT_EOF
) && isdigit(nch
)) {
1656 cur
= cur
*10 + (nch
- '0');
1657 nch
= MSVCRT_fgetc(file
);
1663 case 'f': { /* read a float */
1664 float*val
= va_arg(ap
, float*);
1666 /* skip initial whitespace */
1667 while ((nch
!=MSVCRT_EOF
) && isspace(nch
))
1668 nch
= MSVCRT_fgetc(file
);
1669 /* get sign and first digit */
1671 nch
= MSVCRT_fgetc(file
);
1680 /* read until no more digits */
1681 while ((nch
!=MSVCRT_EOF
) && isdigit(nch
)) {
1682 cur
= cur
*10 + (nch
- '0');
1683 nch
= MSVCRT_fgetc(file
);
1686 /* handle decimals */
1688 nch
= MSVCRT_fgetc(file
);
1689 while ((nch
!=MSVCRT_EOF
) && isdigit(nch
)) {
1691 cur
+= dec
* (nch
- '0');
1692 nch
= MSVCRT_fgetc(file
);
1699 case 's': { /* read a word */
1700 char*str
= va_arg(ap
, char*);
1702 /* skip initial whitespace */
1703 while ((nch
!=MSVCRT_EOF
) && isspace(nch
))
1704 nch
= MSVCRT_fgetc(file
);
1705 /* read until whitespace */
1706 while ((nch
!=MSVCRT_EOF
) && !isspace(nch
)) {
1707 *sptr
++ = nch
; st
++;
1708 nch
= MSVCRT_fgetc(file
);
1712 TRACE("read word: %s\n", str
);
1715 default: FIXME("unhandled: %%%c\n", *format
);
1721 /* check for character match */
1723 nch
= MSVCRT_fgetc(file
);
1729 if (nch
!=MSVCRT_EOF
) {
1730 WARN("need ungetch\n");
1732 TRACE("returning %d\n", rd
);
1736 /*********************************************************************
1739 LONG __cdecl
MSVCRT_fseek(MSVCRT_FILE
* file
, LONG offset
, int whence
)
1741 return MSVCRT__lseek(file
->_file
,offset
,whence
);
1744 /*********************************************************************
1747 LONG __cdecl
MSVCRT_ftell(MSVCRT_FILE
* file
)
1749 return MSVCRT__tell(file
->_file
);
1752 /*********************************************************************
1755 unsigned int __cdecl
MSVCRT_fwrite(const void *ptr
, int size
, int nmemb
, MSVCRT_FILE
* file
)
1757 unsigned int written
= MSVCRT__write(file
->_file
, ptr
, size
* nmemb
);
1760 return written
/ size
;
1763 /*********************************************************************
1766 int __cdecl
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
1768 return MSVCRT_fwrite(s
,strlen(s
),1,file
) == 1 ? 0 : MSVCRT_EOF
;
1771 /*********************************************************************
1774 int __cdecl
MSVCRT_fputws(const WCHAR
*s
, MSVCRT_FILE
* file
)
1776 return MSVCRT_fwrite(s
,wcslen(s
),1,file
) == 1 ? 0 : MSVCRT_EOF
;
1779 /*********************************************************************
1780 * getchar (MSVCRT.@)
1782 int __cdecl
MSVCRT_getchar(void)
1784 return MSVCRT_fgetc(MSVCRT_stdin
);
1787 /*********************************************************************
1790 int __cdecl
MSVCRT_getc(MSVCRT_FILE
* file
)
1792 return MSVCRT_fgetc(file
);
1795 /*********************************************************************
1798 char *__cdecl
MSVCRT_gets(char *buf
)
1801 char * buf_start
= buf
;
1803 /* BAD, for the whole WINE process blocks... just done this way to test
1804 * windows95's ftp.exe.
1805 * JG 19/9/00: Is this still true, now we are using ReadFile?
1807 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
1808 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
1809 if(cc
!= '\r') *buf
++ = (char)cc
;
1813 TRACE("got '%s'\n", buf_start
);
1817 /*********************************************************************
1820 int __cdecl
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
1822 return MSVCRT_fputc(c
, file
);
1825 /*********************************************************************
1826 * putchar (MSVCRT.@)
1828 void __cdecl
MSVCRT_putchar(int c
)
1830 MSVCRT_fputc(c
, MSVCRT_stdout
);
1833 /*********************************************************************
1836 int __cdecl
MSVCRT_puts(const char *s
)
1838 int retval
= MSVCRT_EOF
;
1839 if (MSVCRT_fwrite(s
,strlen(s
),1,MSVCRT_stdout
) == 1)
1840 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
1844 /*********************************************************************
1847 int __cdecl
MSVCRT__putws(const WCHAR
*s
)
1849 static const WCHAR nl
= (WCHAR
)L
'\n';
1850 if (MSVCRT_fwrite(s
,wcslen(s
),1,MSVCRT_stdout
) == 1)
1851 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
1855 /*********************************************************************
1858 int __cdecl
MSVCRT_remove(const char *path
)
1860 TRACE("(%s)\n",path
);
1861 if (DeleteFileA(path
))
1863 TRACE(":failed (%ld)\n",GetLastError());
1864 MSVCRT__set_errno(GetLastError());
1868 /*********************************************************************
1869 * _wremove (MSVCRT.@)
1871 int __cdecl
MSVCRT__wremove(const WCHAR
*path
)
1873 TRACE("(%s)\n",debugstr_w(path
));
1874 if (DeleteFileW(path
))
1876 TRACE(":failed (%ld)\n",GetLastError());
1877 MSVCRT__set_errno(GetLastError());
1881 /*********************************************************************
1884 int __cdecl
MSVCRT_scanf(const char *format
, ...)
1889 va_start(valist
, format
);
1890 res
= MSVCRT_fscanf(MSVCRT_stdin
, format
, valist
);
1895 /*********************************************************************
1898 int __cdecl
MSVCRT_rename(const char *oldpath
,const char *newpath
)
1900 TRACE(":from %s to %s\n",oldpath
,newpath
);
1901 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_REPLACE_EXISTING
))
1903 TRACE(":failed (%ld)\n",GetLastError());
1904 MSVCRT__set_errno(GetLastError());
1908 /*********************************************************************
1909 * _wrename (MSVCRT.@)
1911 int __cdecl
MSVCRT__wrename(const WCHAR
*oldpath
,const WCHAR
*newpath
)
1913 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
1914 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_REPLACE_EXISTING
))
1916 TRACE(":failed (%ld)\n",GetLastError());
1917 MSVCRT__set_errno(GetLastError());
1921 /*********************************************************************
1922 * setvbuf (MSVCRT.@)
1924 void MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, unsigned int size
)
1926 FIXME("(%p,%p,%d,%d)stub\n",file
, buf
, mode
, size
);
1929 /*********************************************************************
1932 void __cdecl
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
1934 MSVCRT_setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
1937 /*********************************************************************
1940 char *__cdecl
MSVCRT_tmpnam(char *s
)
1942 char tmpbuf
[MAX_PATH
];
1943 char* prefix
= "TMP";
1944 if (!GetTempPathA(MAX_PATH
,tmpbuf
) ||
1945 !GetTempFileNameA(tmpbuf
,prefix
,0,MSVCRT_tmpname
))
1947 TRACE(":failed-last error (%ld)\n",GetLastError());
1950 TRACE(":got tmpnam %s\n",MSVCRT_tmpname
);
1955 /*********************************************************************
1956 * tmpfile (MSVCRT.@)
1958 MSVCRT_FILE
* __cdecl
MSVCRT_tmpfile(void)
1960 char *filename
= MSVCRT_tmpnam(NULL
);
1962 fd
= MSVCRT__open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
1964 return MSVCRT__alloc_fp(fd
);
1967 extern int vsnprintf(void *, unsigned int, const void*, va_list);
1969 /*********************************************************************
1970 * vfprintf (MSVCRT.@)
1972 int __cdecl
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, va_list valist
)
1974 char buf
[2048], *mem
= buf
;
1975 int written
, resize
= sizeof(buf
), retval
;
1976 /* There are two conventions for vsnprintf failing:
1977 * Return -1 if we truncated, or
1978 * Return the number of bytes that would have been written
1979 * The code below handles both cases
1981 while ((written
= vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
1984 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
1987 if (!(mem
= (char *)MSVCRT_malloc(resize
)))
1990 retval
= MSVCRT_fwrite(mem
, 1, written
, file
);
1996 /*********************************************************************
1997 * vfwprintf (MSVCRT.@)
1999 int __cdecl
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const WCHAR
*format
, va_list valist
)
2001 WCHAR buf
[2048], *mem
= buf
;
2002 int written
, resize
= sizeof(buf
) / sizeof(WCHAR
), retval
;
2003 /* See vfprintf comments */
2004 while ((written
= MSVCRT__vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
2007 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(WCHAR
));
2010 if (!(mem
= (WCHAR
*)MSVCRT_malloc(resize
)))
2013 retval
= MSVCRT_fwrite(mem
, 1, written
* sizeof (WCHAR
), file
);
2019 /*********************************************************************
2020 * vprintf (MSVCRT.@)
2022 int __cdecl
MSVCRT_vprintf(const char *format
, va_list valist
)
2024 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
2027 /*********************************************************************
2028 * vwprintf (MSVCRT.@)
2030 int __cdecl
MSVCRT_vwprintf(const WCHAR
*format
, va_list valist
)
2032 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
2035 /*********************************************************************
2036 * fprintf (MSVCRT.@)
2038 int __cdecl
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
2042 va_start(valist
, format
);
2043 res
= MSVCRT_vfprintf(file
, format
, valist
);
2048 /*********************************************************************
2049 * fwprintf (MSVCRT.@)
2051 int __cdecl
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const WCHAR
*format
, ...)
2055 va_start(valist
, format
);
2056 res
= MSVCRT_vfwprintf(file
, format
, valist
);
2061 /*********************************************************************
2064 int __cdecl
MSVCRT_printf(const char *format
, ...)
2068 va_start(valist
, format
);
2069 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
2074 /*********************************************************************
2075 * wprintf (MSVCRT.@)
2077 int __cdecl
MSVCRT_wprintf(const WCHAR
*format
, ...)
2081 va_start(valist
, format
);
2082 res
= MSVCRT_vwprintf(format
, valist
);