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
17 #include "wine/unicode.h"
18 #include "msvcrt/direct.h"
19 #include "msvcrt/fcntl.h"
20 #include "msvcrt/io.h"
21 #include "msvcrt/stdio.h"
22 #include "msvcrt/stdlib.h"
23 #include "msvcrt/string.h"
24 #include "msvcrt/sys/stat.h"
25 #include "msvcrt/sys/utime.h"
26 #include "msvcrt/time.h"
28 DEFAULT_DEBUG_CHANNEL(msvcrt
);
30 /* for stat mode, permissions apply to all,owner and group */
31 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
32 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
33 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
35 /* _access() bit flags FIXME: incomplete */
39 /* FIXME: Make this dynamic */
40 #define MSVCRT_MAX_FILES 257
42 HANDLE MSVCRT_handles
[MSVCRT_MAX_FILES
];
43 MSVCRT_FILE
* MSVCRT_files
[MSVCRT_MAX_FILES
];
44 int MSVCRT_flags
[MSVCRT_MAX_FILES
];
45 char *MSVCRT_tempfiles
[MSVCRT_MAX_FILES
];
46 MSVCRT_FILE MSVCRT__iob
[3];
47 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
48 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
49 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
51 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
52 static int MSVCRT_fdend
= 3; /* highest allocated fd */
54 /* INTERNAL: process umask */
55 static int MSVCRT_umask
= 0;
57 /* INTERNAL: Static buffer for temp file name */
58 static char MSVCRT_tmpname
[MAX_PATH
];
60 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
61 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
62 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
63 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
65 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
66 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
67 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
68 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
69 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
71 extern CRITICAL_SECTION MSVCRT_file_cs
;
72 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
73 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
76 /* INTERNAL: Get the HANDLE for a fd */
77 static HANDLE
msvcrt_fdtoh(int fd
)
79 if (fd
< 0 || fd
>= MSVCRT_fdend
||
80 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
82 WARN(":fd (%d) - no handle!\n",fd
);
83 SET_THREAD_VAR(doserrno
,0);
84 SET_THREAD_VAR(errno
,MSVCRT_EBADF
);
85 return INVALID_HANDLE_VALUE
;
87 return MSVCRT_handles
[fd
];
90 /* INTERNAL: free a file entry fd */
91 static void msvcrt_free_fd(int fd
)
93 MSVCRT_handles
[fd
] = INVALID_HANDLE_VALUE
;
96 TRACE(":fd (%d) freed\n",fd
);
98 return; /* dont use 0,1,2 for user files */
99 if (fd
== MSVCRT_fdend
- 1)
101 if (fd
< MSVCRT_fdstart
)
105 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
106 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
108 int fd
= MSVCRT_fdstart
;
110 TRACE(":handle (%d) allocating fd (%d)\n",hand
,fd
);
111 if (fd
>= MSVCRT_MAX_FILES
)
113 WARN(":files exhausted!\n");
116 MSVCRT_handles
[fd
] = hand
;
117 MSVCRT_flags
[fd
] = flag
;
119 /* locate next free slot */
120 if (fd
== MSVCRT_fdend
)
121 MSVCRT_fdstart
= ++MSVCRT_fdend
;
123 while(MSVCRT_fdstart
< MSVCRT_fdend
&&
124 MSVCRT_handles
[MSVCRT_fdstart
] != INVALID_HANDLE_VALUE
)
130 /* INTERNAL: Allocate a FILE* for an fd slot
131 * This is done lazily to avoid memory wastage for low level open/write
132 * usage when a FILE* is not requested (but may be later).
134 static MSVCRT_FILE
* msvcrt_alloc_fp(int fd
)
136 TRACE(":fd (%d) allocating FILE*\n",fd
);
137 if (fd
< 0 || fd
>= MSVCRT_fdend
||
138 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
140 WARN(":invalid fd %d\n",fd
);
141 SET_THREAD_VAR(doserrno
,0);
142 SET_THREAD_VAR(errno
,MSVCRT_EBADF
);
145 if (!MSVCRT_files
[fd
])
147 if ((MSVCRT_files
[fd
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
149 MSVCRT_files
[fd
]->_file
= fd
;
150 MSVCRT_files
[fd
]->_flag
= MSVCRT_flags
[fd
];
151 MSVCRT_files
[fd
]->_flag
&= ~_IOAPPEND
; /* mask out, see above */
154 TRACE(":got FILE* (%p)\n",MSVCRT_files
[fd
]);
155 return MSVCRT_files
[fd
];
159 /* INTERNAL: Set up stdin, stderr and stdout */
160 void msvcrt_init_io(void)
163 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
164 MSVCRT_handles
[0] = GetStdHandle(STD_INPUT_HANDLE
);
165 MSVCRT_flags
[0] = MSVCRT__iob
[0]._flag
= _IOREAD
;
166 MSVCRT_handles
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
167 MSVCRT_flags
[1] = MSVCRT__iob
[1]._flag
= _IOWRT
;
168 MSVCRT_handles
[2] = GetStdHandle(STD_ERROR_HANDLE
);
169 MSVCRT_flags
[2] = MSVCRT__iob
[2]._flag
= _IOWRT
;
171 TRACE(":handles (%d)(%d)(%d)\n",MSVCRT_handles
[0],
172 MSVCRT_handles
[1],MSVCRT_handles
[2]);
174 for (i
= 0; i
< 3; i
++)
176 /* FILE structs for stdin/out/err are static and never deleted */
177 MSVCRT_files
[i
] = &MSVCRT__iob
[i
];
178 MSVCRT__iob
[i
]._file
= i
;
179 MSVCRT_tempfiles
[i
] = NULL
;
183 /*********************************************************************
186 MSVCRT_FILE
*__p__iob(void)
188 return &MSVCRT__iob
[0];
191 /*********************************************************************
194 int _access(const char *filename
, int mode
)
196 DWORD attr
= GetFileAttributesA(filename
);
198 TRACE("(%s,%d) %ld\n",filename
,mode
,attr
);
200 if (!filename
|| attr
== 0xffffffff)
202 MSVCRT__set_errno(GetLastError());
205 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
207 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
213 /*********************************************************************
214 * _waccess (MSVCRT.@)
216 int _waccess(const WCHAR
*filename
, int mode
)
218 DWORD attr
= GetFileAttributesW(filename
);
220 TRACE("(%s,%d) %ld\n",debugstr_w(filename
),mode
,attr
);
222 if (!filename
|| attr
== 0xffffffff)
224 MSVCRT__set_errno(GetLastError());
227 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
229 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
235 /*********************************************************************
238 int _chmod(const char *path
, int flags
)
240 DWORD oldFlags
= GetFileAttributesA(path
);
242 if (oldFlags
!= 0x0FFFFFFFF)
244 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
245 oldFlags
| FILE_ATTRIBUTE_READONLY
;
247 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
250 MSVCRT__set_errno(GetLastError());
254 /*********************************************************************
257 int _wchmod(const WCHAR
*path
, int flags
)
259 DWORD oldFlags
= GetFileAttributesW(path
);
261 if (oldFlags
!= 0x0FFFFFFFF)
263 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
264 oldFlags
| FILE_ATTRIBUTE_READONLY
;
266 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
269 MSVCRT__set_errno(GetLastError());
273 /*********************************************************************
276 int _unlink(const char *path
)
278 TRACE("(%s)\n",path
);
279 if(DeleteFileA(path
))
281 TRACE("failed (%ld)\n",GetLastError());
282 MSVCRT__set_errno(GetLastError());
286 /*********************************************************************
287 * _wunlink (MSVCRT.@)
289 int _wunlink(const WCHAR
*path
)
291 TRACE("(%s)\n",debugstr_w(path
));
292 if(DeleteFileW(path
))
294 TRACE("failed (%ld)\n",GetLastError());
295 MSVCRT__set_errno(GetLastError());
299 /*********************************************************************
304 HANDLE hand
= msvcrt_fdtoh(fd
);
306 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
307 if (hand
== INVALID_HANDLE_VALUE
)
310 /* Dont free std FILE*'s, they are not dynamic */
311 if (fd
> 2 && MSVCRT_files
[fd
])
312 MSVCRT_free(MSVCRT_files
[fd
]);
316 if (!CloseHandle(hand
))
318 WARN(":failed-last error (%ld)\n",GetLastError());
319 MSVCRT__set_errno(GetLastError());
322 if (MSVCRT_tempfiles
[fd
])
324 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles
[fd
]);
325 _unlink(MSVCRT_tempfiles
[fd
]);
326 MSVCRT_free(MSVCRT_tempfiles
[fd
]);
327 MSVCRT_tempfiles
[fd
] = NULL
;
334 /*********************************************************************
339 HANDLE hand
= msvcrt_fdtoh(fd
);
341 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
342 if (hand
== INVALID_HANDLE_VALUE
)
345 if (!FlushFileBuffers(hand
))
347 if (GetLastError() == ERROR_INVALID_HANDLE
)
349 /* FlushFileBuffers fails for console handles
350 * so we ignore this error.
354 TRACE(":failed-last error (%ld)\n",GetLastError());
355 MSVCRT__set_errno(GetLastError());
362 /*********************************************************************
368 HANDLE hand
= msvcrt_fdtoh(fd
);
370 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
372 if (hand
== INVALID_HANDLE_VALUE
)
375 /* If we have a FILE* for this file, the EOF flag
376 * will be set by the read()/write() functions.
378 if (MSVCRT_files
[fd
])
379 return MSVCRT_files
[fd
]->_flag
& _IOEOF
;
381 /* Otherwise we do it the hard way */
382 curpos
= SetFilePointer(hand
, 0, NULL
, SEEK_CUR
);
383 endpos
= SetFilePointer(hand
, 0, NULL
, FILE_END
);
385 if (curpos
== endpos
)
388 SetFilePointer(hand
, curpos
, 0, FILE_BEGIN
);
392 /*********************************************************************
393 * _fcloseall (MSVCRT.@)
397 int num_closed
= 0, i
;
399 for (i
= 3; i
< MSVCRT_fdend
; i
++)
400 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
406 TRACE(":closed (%d) handles\n",num_closed
);
410 /*********************************************************************
413 LONG
_lseek(int fd
, LONG offset
, int whence
)
416 HANDLE hand
= msvcrt_fdtoh(fd
);
418 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
419 if (hand
== INVALID_HANDLE_VALUE
)
422 if (whence
< 0 || whence
> 2)
424 SET_THREAD_VAR(errno
,MSVCRT_EINVAL
);
428 TRACE(":fd (%d) to 0x%08lx pos %s\n",
429 fd
,offset
,(whence
==SEEK_SET
)?"SEEK_SET":
430 (whence
==SEEK_CUR
)?"SEEK_CUR":
431 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
433 if ((ret
= SetFilePointer(hand
, offset
, NULL
, whence
)) != 0xffffffff)
435 if (MSVCRT_files
[fd
])
436 MSVCRT_files
[fd
]->_flag
&= ~_IOEOF
;
437 /* FIXME: What if we seek _to_ EOF - is EOF set? */
440 TRACE(":error-last error (%ld)\n",GetLastError());
441 if (MSVCRT_files
[fd
])
442 switch(GetLastError())
444 case ERROR_NEGATIVE_SEEK
:
445 case ERROR_SEEK_ON_DEVICE
:
446 MSVCRT__set_errno(GetLastError());
447 MSVCRT_files
[fd
]->_flag
|= _IOERR
;
455 /*********************************************************************
458 void MSVCRT_rewind(MSVCRT_FILE
* file
)
460 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
461 _lseek(file
->_file
,0,SEEK_SET
);
462 file
->_flag
&= ~(_IOEOF
| _IOERR
);
465 /*********************************************************************
468 MSVCRT_FILE
* _fdopen(int fd
, const char *mode
)
470 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
472 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
479 /*********************************************************************
480 * _wfdopen (MSVCRT.@)
482 MSVCRT_FILE
* _wfdopen(int fd
, const WCHAR
*mode
)
484 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
486 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
493 /*********************************************************************
494 * _filelength (MSVCRT.@)
496 LONG
_filelength(int fd
)
498 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
501 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
504 if (endPos
!= curPos
)
505 _lseek(fd
, curPos
, SEEK_SET
);
512 /*********************************************************************
515 int _fileno(MSVCRT_FILE
* file
)
517 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
521 /*********************************************************************
522 * _flushall (MSVCRT.@)
526 int num_flushed
= 0, i
= 3;
528 while(i
< MSVCRT_fdend
)
529 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
531 if (_commit(i
) == -1)
533 MSVCRT_files
[i
]->_flag
|= _IOERR
;
537 TRACE(":flushed (%d) handles\n",num_flushed
);
541 /*********************************************************************
544 int _fstat(int fd
, struct _stat
* buf
)
547 BY_HANDLE_FILE_INFORMATION hfi
;
548 HANDLE hand
= msvcrt_fdtoh(fd
);
550 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
551 if (hand
== INVALID_HANDLE_VALUE
)
556 WARN(":failed-NULL buf\n");
557 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
561 memset(&hfi
, 0, sizeof(hfi
));
562 memset(buf
, 0, sizeof(struct _stat
));
563 if (!GetFileInformationByHandle(hand
, &hfi
))
565 WARN(":failed-last error (%ld)\n",GetLastError());
566 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
569 FIXME(":dwFileAttributes = %d, mode set to 0\n",hfi
.dwFileAttributes
);
570 buf
->st_nlink
= hfi
.nNumberOfLinks
;
571 buf
->st_size
= hfi
.nFileSizeLow
;
572 RtlTimeToSecondsSince1970(&hfi
.ftLastAccessTime
, &dw
);
574 RtlTimeToSecondsSince1970(&hfi
.ftLastWriteTime
, &dw
);
575 buf
->st_mtime
= buf
->st_ctime
= dw
;
579 /*********************************************************************
582 int _futime(int fd
, struct _utimbuf
*t
)
584 HANDLE hand
= msvcrt_fdtoh(fd
);
589 MSVCRT_time_t currTime
;
590 MSVCRT_time(&currTime
);
591 RtlSecondsSince1970ToTime(currTime
, &at
);
592 memcpy(&wt
, &at
, sizeof(wt
));
596 RtlSecondsSince1970ToTime(t
->actime
, &at
);
597 if (t
->actime
== t
->modtime
)
598 memcpy(&wt
, &at
, sizeof(wt
));
600 RtlSecondsSince1970ToTime(t
->modtime
, &wt
);
603 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
605 MSVCRT__set_errno(GetLastError());
611 /*********************************************************************
612 * _get_osfhandle (MSVCRT.@)
614 long _get_osfhandle(int fd
)
616 HANDLE hand
= msvcrt_fdtoh(fd
);
617 HANDLE newhand
= hand
;
618 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
620 if (hand
!= INVALID_HANDLE_VALUE
)
622 /* FIXME: I'm not convinced that I should be copying the
623 * handle here - it may be leaked if the app doesn't
624 * close it (and the API docs dont say that it should)
625 * Not duplicating it means that it can't be inherited
626 * and so lcc's wedit doesn't cope when it passes it to
627 * child processes. I've an idea that it should either
628 * be copied by CreateProcess, or marked as inheritable
629 * when initialised, or maybe both? JG 21-9-00.
631 DuplicateHandle(GetCurrentProcess(),hand
,GetCurrentProcess(),
632 &newhand
,0,TRUE
,DUPLICATE_SAME_ACCESS
);
637 /*********************************************************************
642 HANDLE hand
= msvcrt_fdtoh(fd
);
644 TRACE(":fd (%d) handle (%d)\n",fd
,hand
);
645 if (hand
== INVALID_HANDLE_VALUE
)
648 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
651 /*********************************************************************
654 char *_mktemp(char *pattern
)
657 char *retVal
= pattern
;
662 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
666 id
= GetCurrentProcessId();
670 int tempNum
= id
/ 10;
671 *pattern
-- = id
- (tempNum
* 10) + '0';
677 if (GetFileAttributesA(retVal
) == 0xFFFFFFFF &&
678 GetLastError() == ERROR_FILE_NOT_FOUND
)
681 } while(letter
!= '|');
685 /*********************************************************************
686 * _wmktemp (MSVCRT.@)
688 WCHAR
*_wmktemp(WCHAR
*pattern
)
691 WCHAR
*retVal
= pattern
;
693 WCHAR letter
= (WCHAR
)L
'a';
696 numX
= (*pattern
++ == (WCHAR
)L
'X')? numX
+ 1 : 0;
700 id
= GetCurrentProcessId();
704 int tempNum
= id
/ 10;
705 *pattern
-- = id
- (tempNum
* 10) + (WCHAR
)L
'0';
711 if (GetFileAttributesW(retVal
) == 0xFFFFFFFF &&
712 GetLastError() == ERROR_FILE_NOT_FOUND
)
715 } while(letter
!= (WCHAR
)L
'|');
719 /*********************************************************************
722 int _open(const char *path
,int flags
,...)
724 DWORD access
= 0, creation
= 0;
727 SECURITY_ATTRIBUTES sa
;
729 TRACE(":file (%s) mode 0x%04x\n",path
,flags
);
731 switch(flags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
734 access
|= GENERIC_READ
;
738 access
|= GENERIC_WRITE
;
742 access
|= GENERIC_WRITE
| GENERIC_READ
;
747 if (flags
& _O_CREAT
)
750 creation
= CREATE_NEW
;
751 else if (flags
& _O_TRUNC
)
752 creation
= CREATE_ALWAYS
;
754 creation
= OPEN_ALWAYS
;
756 else /* no _O_CREAT */
758 if (flags
& _O_TRUNC
)
759 creation
= TRUNCATE_EXISTING
;
761 creation
= OPEN_EXISTING
;
763 if (flags
& _O_APPEND
)
767 flags
|= _O_BINARY
; /* FIXME: Default to text */
771 /* Dont warn when writing */
772 if (ioflag
& GENERIC_READ
)
773 FIXME(":TEXT node not implemented\n");
777 if (flags
& ~(_O_BINARY
|_O_TEXT
|_O_APPEND
|_O_TRUNC
|_O_EXCL
778 |_O_CREAT
|_O_RDWR
|_O_TEMPORARY
))
779 TRACE(":unsupported flags 0x%04x\n",flags
);
781 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
782 sa
.lpSecurityDescriptor
= NULL
;
783 sa
.bInheritHandle
= TRUE
;
785 hand
= CreateFileA(path
, access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
786 &sa
, creation
, FILE_ATTRIBUTE_NORMAL
, 0);
788 if (hand
== INVALID_HANDLE_VALUE
)
790 WARN(":failed-last error (%ld)\n",GetLastError());
791 MSVCRT__set_errno(GetLastError());
795 fd
= msvcrt_alloc_fd(hand
, ioflag
);
797 TRACE(":fd (%d) handle (%d)\n",fd
, hand
);
801 if (flags
& _O_TEMPORARY
)
802 MSVCRT_tempfiles
[fd
] = _strdup(path
);
803 if (ioflag
& _IOAPPEND
)
804 _lseek(fd
, 0, FILE_END
);
810 /*********************************************************************
813 int _wopen(const WCHAR
*path
,int flags
,...)
815 const unsigned int len
= strlenW(path
);
816 char *patha
= MSVCRT_calloc(len
+ 1,1);
817 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
819 int retval
= _open(patha
,flags
);
824 MSVCRT__set_errno(GetLastError());
828 /*********************************************************************
831 int MSVCRT__sopen(const char *path
,int oflags
,int shflags
)
833 return _open(path
, oflags
| shflags
);
836 /*********************************************************************
839 int _creat(const char *path
, int flags
)
841 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
842 return _open(path
, usedFlags
);
845 /*********************************************************************
848 int _wcreat(const WCHAR
*path
, int flags
)
850 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
851 return _wopen(path
, usedFlags
);
854 /*********************************************************************
855 * _open_osfhandle (MSVCRT.@)
857 int _open_osfhandle(long hand
, int flags
)
859 int fd
= msvcrt_alloc_fd(hand
,flags
);
860 TRACE(":handle (%ld) fd (%d)\n",hand
,fd
);
864 /*********************************************************************
869 int num_removed
= 0, i
;
871 for (i
= 3; i
< MSVCRT_fdend
; i
++)
872 if (MSVCRT_tempfiles
[i
])
879 TRACE(":removed (%d) temp files\n",num_removed
);
883 /*********************************************************************
886 int _read(int fd
, void *buf
, unsigned int count
)
889 HANDLE hand
= msvcrt_fdtoh(fd
);
891 /* Dont trace small reads, it gets *very* annoying */
893 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
894 if (hand
== INVALID_HANDLE_VALUE
)
897 /* Set _cnt to 0 so optimised binaries will call our implementation
898 * of putc/getc. See _filbuf/_flsbuf comments.
900 if (MSVCRT_files
[fd
])
901 MSVCRT_files
[fd
]->_cnt
= 0;
903 if (ReadFile(hand
, buf
, count
, &num_read
, NULL
))
905 if (num_read
!= count
&& MSVCRT_files
[fd
])
908 MSVCRT_files
[fd
]->_flag
|= _IOEOF
;
912 TRACE(":failed-last error (%ld)\n",GetLastError());
913 if (MSVCRT_files
[fd
])
914 MSVCRT_files
[fd
]->_flag
|= _IOERR
;
918 /*********************************************************************
921 int _getw(MSVCRT_FILE
* file
)
924 if (_read(file
->_file
, &i
, sizeof(int)) != 1)
929 /*********************************************************************
930 * _setmode (MSVCRT.@)
932 int _setmode(int fd
,int mode
)
935 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd
,mode
);
939 /*********************************************************************
942 int _stat(const char* path
, struct _stat
* buf
)
945 WIN32_FILE_ATTRIBUTE_DATA hfi
;
946 unsigned short mode
= MSVCRT_S_IREAD
;
949 TRACE(":file (%s) buf(%p)\n",path
,buf
);
951 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
953 TRACE("failed (%ld)\n",GetLastError());
954 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
958 memset(buf
,0,sizeof(struct _stat
));
960 /* FIXME: rdev isnt drive num,despite what the docs say-what is it? */
962 buf
->st_dev
= buf
->st_rdev
= toupper(*path
- 'A'); /* drive num */
964 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
968 /* Dir, or regular file? */
969 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
970 (path
[plen
-1] == '\\'))
971 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
976 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
978 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
979 (tolower(path
[plen
-3]) << 16);
980 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
981 mode
|= MSVCRT_S_IEXEC
;
985 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
986 mode
|= MSVCRT_S_IWRITE
;
990 buf
->st_size
= hfi
.nFileSizeLow
;
991 RtlTimeToSecondsSince1970(&hfi
.ftLastAccessTime
, &dw
);
993 RtlTimeToSecondsSince1970(&hfi
.ftLastWriteTime
, &dw
);
994 buf
->st_mtime
= buf
->st_ctime
= dw
;
995 TRACE("\n%d %d %d %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,buf
->st_size
,
996 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1000 /*********************************************************************
1003 int _wstat(const WCHAR
* path
, struct _stat
* buf
)
1006 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1007 unsigned short mode
= MSVCRT_S_IREAD
;
1010 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1012 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1014 TRACE("failed (%ld)\n",GetLastError());
1015 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1019 memset(buf
,0,sizeof(struct _stat
));
1021 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1022 if (MSVCRT_iswalpha(*path
))
1023 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- (WCHAR
)L
'A'); /* drive num */
1025 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1027 plen
= strlenW(path
);
1029 /* Dir, or regular file? */
1030 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1031 (path
[plen
-1] == (WCHAR
)L
'\\'))
1032 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1037 if (plen
> 6 && path
[plen
-4] == (WCHAR
)L
'.') /* shortest exe: "\x.exe" */
1039 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1040 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1041 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1042 mode
|= MSVCRT_S_IEXEC
;
1046 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1047 mode
|= MSVCRT_S_IWRITE
;
1049 buf
->st_mode
= mode
;
1051 buf
->st_size
= hfi
.nFileSizeLow
;
1052 RtlTimeToSecondsSince1970(&hfi
.ftLastAccessTime
, &dw
);
1054 RtlTimeToSecondsSince1970(&hfi
.ftLastWriteTime
, &dw
);
1055 buf
->st_mtime
= buf
->st_ctime
= dw
;
1056 TRACE("\n%d %d %d %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,buf
->st_size
,
1057 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1061 /*********************************************************************
1066 return _lseek(fd
, 0, SEEK_CUR
);
1069 /*********************************************************************
1070 * _tempnam (MSVCRT.@)
1072 char *_tempnam(const char *dir
, const char *prefix
)
1074 char tmpbuf
[MAX_PATH
];
1076 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1077 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1079 TRACE("got name (%s)\n",tmpbuf
);
1080 return _strdup(tmpbuf
);
1082 TRACE("failed (%ld)\n",GetLastError());
1086 /*********************************************************************
1087 * _wtempnam (MSVCRT.@)
1089 WCHAR
*_wtempnam(const WCHAR
*dir
, const WCHAR
*prefix
)
1091 WCHAR tmpbuf
[MAX_PATH
];
1093 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1094 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1096 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1097 return _wcsdup(tmpbuf
);
1099 TRACE("failed (%ld)\n",GetLastError());
1103 /*********************************************************************
1106 int _umask(int umask
)
1108 int old_umask
= MSVCRT_umask
;
1109 TRACE("(%d)\n",umask
);
1110 MSVCRT_umask
= umask
;
1114 /*********************************************************************
1117 int _utime(const char* path
, struct _utimbuf
*t
)
1119 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
1123 int retVal
= _futime(fd
, t
);
1130 /*********************************************************************
1131 * _wutime (MSVCRT.@)
1133 int _wutime(const WCHAR
* path
, struct _utimbuf
*t
)
1135 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
1139 int retVal
= _futime(fd
, t
);
1146 /*********************************************************************
1149 int _write(int fd
, const void* buf
, unsigned int count
)
1152 HANDLE hand
= msvcrt_fdtoh(fd
);
1154 /* Dont trace small writes, it gets *very* annoying */
1156 // TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1157 if (hand
== INVALID_HANDLE_VALUE
)
1160 /* If appending, go to EOF */
1161 if (MSVCRT_flags
[fd
] & _IOAPPEND
)
1162 _lseek(fd
, 0, FILE_END
);
1164 /* Set _cnt to 0 so optimised binaries will call our implementation
1167 if (MSVCRT_files
[fd
])
1168 MSVCRT_files
[fd
]->_cnt
= 0;
1170 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1171 && (num_written
== count
))
1174 TRACE(":failed-last error (%ld)\n",GetLastError());
1175 if (MSVCRT_files
[fd
])
1176 MSVCRT_files
[fd
]->_flag
|= _IOERR
;
1181 /*********************************************************************
1184 int _putw(int val
, MSVCRT_FILE
* file
)
1186 return _write(file
->_file
, &val
, sizeof(val
)) == 1? val
: MSVCRT_EOF
;
1189 /*********************************************************************
1190 * clearerr (MSVCRT.@)
1192 void MSVCRT_clearerr(MSVCRT_FILE
* file
)
1194 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1195 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1198 /*********************************************************************
1201 int MSVCRT_fclose(MSVCRT_FILE
* file
)
1204 r
=_close(file
->_file
);
1205 return ((r
==MSVCRT_EOF
) || (file
->_flag
& _IOERR
) ? MSVCRT_EOF
: 0);
1208 /*********************************************************************
1211 int MSVCRT_feof(MSVCRT_FILE
* file
)
1213 return file
->_flag
& _IOEOF
;
1216 /*********************************************************************
1219 int MSVCRT_ferror(MSVCRT_FILE
* file
)
1221 return file
->_flag
& _IOERR
;
1224 /*********************************************************************
1227 int MSVCRT_fflush(MSVCRT_FILE
* file
)
1229 return _commit(file
->_file
);
1232 /*********************************************************************
1235 int MSVCRT_fgetc(MSVCRT_FILE
* file
)
1238 if (_read(file
->_file
,&c
,1) != 1)
1243 /*********************************************************************
1244 * _fgetchar (MSVCRT.@)
1248 return MSVCRT_fgetc(MSVCRT_stdin
);
1251 /*********************************************************************
1252 * _filbuf (MSVCRT.@)
1254 int _filbuf(MSVCRT_FILE
* file
)
1256 return MSVCRT_fgetc(file
);
1259 /*********************************************************************
1260 * fgetpos (MSVCRT.@)
1262 int MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
1264 *pos
= _tell(file
->_file
);
1265 return (*pos
== -1? -1 : 0);
1268 /*********************************************************************
1271 char *MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
1274 char * buf_start
= s
;
1276 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1277 file
,file
->_file
,s
,size
);
1279 /* BAD, for the whole WINE process blocks... just done this way to test
1280 * windows95's ftp.exe.
1281 * JG - Is this true now we use ReadFile() on stdin too?
1283 for(cc
= MSVCRT_fgetc(file
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
1284 cc
= MSVCRT_fgetc(file
))
1287 if (--size
<= 0) break;
1290 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1292 TRACE(":nothing read\n");
1299 TRACE(":got '%s'\n", buf_start
);
1303 /*********************************************************************
1306 MSVCRT_wint_t
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
1309 if (_read(file
->_file
, &wc
, sizeof(wc
)) != sizeof(wc
))
1314 /*********************************************************************
1317 MSVCRT_wint_t
MSVCRT_getwc(MSVCRT_FILE
* file
)
1319 return MSVCRT_fgetwc(file
);
1322 /*********************************************************************
1323 * _fgetwchar (MSVCRT.@)
1325 MSVCRT_wint_t
_fgetwchar(void)
1327 return MSVCRT_fgetwc(MSVCRT_stdin
);
1330 /*********************************************************************
1331 * getwchar (MSVCRT.@)
1333 MSVCRT_wint_t
MSVCRT_getwchar(void)
1335 return _fgetwchar();
1338 /*********************************************************************
1341 MSVCRT_wint_t
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
1343 if (_write(file
->_file
, &wc
, sizeof(wc
)) != sizeof(wc
))
1348 /*********************************************************************
1349 * _fputwchar (MSVCRT.@)
1351 MSVCRT_wint_t
_fputwchar(MSVCRT_wint_t wc
)
1353 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
1356 /*********************************************************************
1359 MSVCRT_FILE
* MSVCRT_fopen(const char *path
, const char *mode
)
1362 int flags
= 0, plus
= 0, fd
;
1363 const char* search
= mode
;
1365 TRACE("(%s,%s)\n",path
,mode
);
1368 if (*search
++ == '+')
1371 /* map mode string to open() flags. "man fopen" for possibilities. */
1375 flags
= (plus
? _O_RDWR
: _O_RDONLY
);
1378 flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1381 flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1396 flags
&= ~_O_BINARY
;
1401 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1404 fd
= _open(path
, flags
);
1409 file
= msvcrt_alloc_fp(fd
);
1410 TRACE(":got (%p)\n",file
);
1417 /*********************************************************************
1418 * _wfopen (MSVCRT.@)
1420 MSVCRT_FILE
*_wfopen(const WCHAR
*path
, const WCHAR
*mode
)
1422 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
1423 char *patha
= MSVCRT_calloc(plen
+ 1, 1);
1424 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
1426 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
1428 if (patha
&& modea
&&
1429 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
1430 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
1432 MSVCRT_FILE
*retval
= MSVCRT_fopen(patha
,modea
);
1438 MSVCRT__set_errno(GetLastError());
1442 /*********************************************************************
1443 * _fsopen (MSVCRT.@)
1445 MSVCRT_FILE
* _fsopen(const char *path
, const char *mode
, int share
)
1447 FIXME(":(%s,%s,%d),ignoring share mode!\n",path
,mode
,share
);
1448 return MSVCRT_fopen(path
,mode
);
1451 /*********************************************************************
1452 * _wfsopen (MSVCRT.@)
1454 MSVCRT_FILE
* _wfsopen(const WCHAR
*path
, const WCHAR
*mode
, int share
)
1456 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1457 debugstr_w(path
),debugstr_w(mode
),share
);
1458 return _wfopen(path
,mode
);
1461 /*********************************************************************
1464 int MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
1466 return _write(file
->_file
, &c
, 1) == 1? c
: MSVCRT_EOF
;
1469 /*********************************************************************
1470 * _flsbuf (MSVCRT.@)
1472 int _flsbuf(int c
, MSVCRT_FILE
* file
)
1474 return MSVCRT_fputc(c
,file
);
1477 /*********************************************************************
1478 * _fputchar (MSVCRT.@)
1480 int _fputchar(int c
)
1482 return MSVCRT_fputc(c
, MSVCRT_stdout
);
1485 /*********************************************************************
1488 MSVCRT_size_t
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
1490 int read
= _read(file
->_file
,ptr
, size
* nmemb
);
1496 /*********************************************************************
1497 * freopen (MSVCRT.@)
1500 MSVCRT_FILE
* MSVCRT_freopen(const char *path
, const char *mode
,MSVCRT_FILE
* file
)
1502 MSVCRT_FILE
* newfile
;
1505 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
1506 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
1511 FIXME(":reopen on user file not implemented!\n");
1512 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED
);
1516 /* first, create the new file */
1517 if ((newfile
= MSVCRT_fopen(path
,mode
)) == NULL
)
1520 if (fd
< 3 && SetStdHandle(fd
== 0 ? STD_INPUT_HANDLE
:
1521 (fd
== 1? STD_OUTPUT_HANDLE
: STD_ERROR_HANDLE
),
1522 MSVCRT_handles
[newfile
->_file
]))
1524 /* Redirecting std handle to file , copy over.. */
1525 MSVCRT_handles
[fd
] = MSVCRT_handles
[newfile
->_file
];
1526 MSVCRT_flags
[fd
] = MSVCRT_flags
[newfile
->_file
];
1527 memcpy(&MSVCRT__iob
[fd
], newfile
, sizeof (MSVCRT_FILE
));
1528 MSVCRT__iob
[fd
]._file
= fd
;
1529 /* And free up the resources allocated by fopen, but
1530 * not the HANDLE we copied. */
1531 MSVCRT_free(MSVCRT_files
[fd
]);
1532 msvcrt_free_fd(newfile
->_file
);
1533 return &MSVCRT__iob
[fd
];
1536 WARN(":failed-last error (%ld)\n",GetLastError());
1537 MSVCRT_fclose(newfile
);
1538 MSVCRT__set_errno(GetLastError());
1542 /*********************************************************************
1543 * fsetpos (MSVCRT.@)
1545 int MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
1547 return _lseek(file
->_file
,*pos
,SEEK_SET
);
1550 /*********************************************************************
1553 int MSVCRT_fscanf(MSVCRT_FILE
* file
, const char *format
, ...)
1555 /* NOTE: If you extend this function, extend MSVCRT__cscanf in console.c too */
1559 if (!*format
) return 0;
1560 WARN("%p (\"%s\"): semi-stub\n", file
, format
);
1561 nch
= MSVCRT_fgetc(file
);
1562 va_start(ap
, format
);
1564 if (*format
== ' ') {
1565 /* skip whitespace */
1566 while ((nch
!=MSVCRT_EOF
) && isspace(nch
))
1567 nch
= MSVCRT_fgetc(file
);
1569 else if (*format
== '%') {
1573 case 'd': { /* read an integer */
1574 int*val
= va_arg(ap
, int*);
1576 /* skip initial whitespace */
1577 while ((nch
!=MSVCRT_EOF
) && isspace(nch
))
1578 nch
= MSVCRT_fgetc(file
);
1579 /* get sign and first digit */
1581 nch
= MSVCRT_fgetc(file
);
1590 nch
= MSVCRT_fgetc(file
);
1591 /* read until no more digits */
1592 while ((nch
!=MSVCRT_EOF
) && isdigit(nch
)) {
1593 cur
= cur
*10 + (nch
- '0');
1594 nch
= MSVCRT_fgetc(file
);
1600 case 'f': { /* read a float */
1601 float*val
= va_arg(ap
, float*);
1603 /* skip initial whitespace */
1604 while ((nch
!=MSVCRT_EOF
) && isspace(nch
))
1605 nch
= MSVCRT_fgetc(file
);
1606 /* get sign and first digit */
1608 nch
= MSVCRT_fgetc(file
);
1617 /* read until no more digits */
1618 while ((nch
!=MSVCRT_EOF
) && isdigit(nch
)) {
1619 cur
= cur
*10 + (nch
- '0');
1620 nch
= MSVCRT_fgetc(file
);
1623 /* handle decimals */
1625 nch
= MSVCRT_fgetc(file
);
1626 while ((nch
!=MSVCRT_EOF
) && isdigit(nch
)) {
1628 cur
+= dec
* (nch
- '0');
1629 nch
= MSVCRT_fgetc(file
);
1636 case 's': { /* read a word */
1637 char*str
= va_arg(ap
, char*);
1639 /* skip initial whitespace */
1640 while ((nch
!=MSVCRT_EOF
) && isspace(nch
))
1641 nch
= MSVCRT_fgetc(file
);
1642 /* read until whitespace */
1643 while ((nch
!=MSVCRT_EOF
) && !isspace(nch
)) {
1644 *sptr
++ = nch
; st
++;
1645 nch
= MSVCRT_fgetc(file
);
1649 TRACE("read word: %s\n", str
);
1652 default: FIXME("unhandled: %%%c\n", *format
);
1658 /* check for character match */
1660 nch
= MSVCRT_fgetc(file
);
1666 if (nch
!=MSVCRT_EOF
) {
1667 WARN("need ungetch\n");
1669 TRACE("returning %d\n", rd
);
1673 /*********************************************************************
1676 int MSVCRT_fseek(MSVCRT_FILE
* file
, long offset
, int whence
)
1678 return _lseek(file
->_file
,offset
,whence
);
1681 /*********************************************************************
1684 LONG
MSVCRT_ftell(MSVCRT_FILE
* file
)
1686 return _tell(file
->_file
);
1689 /*********************************************************************
1692 MSVCRT_size_t
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
1694 int written
= _write(file
->_file
, ptr
, size
* nmemb
);
1697 return written
/ size
;
1700 /*********************************************************************
1703 int MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
1705 return MSVCRT_fwrite(s
,strlen(s
),1,file
) == 1 ? 0 : MSVCRT_EOF
;
1708 /*********************************************************************
1711 int MSVCRT_fputws(const WCHAR
*s
, MSVCRT_FILE
* file
)
1713 return MSVCRT_fwrite(s
,strlenW(s
),1,file
) == 1 ? 0 : MSVCRT_EOF
;
1716 /*********************************************************************
1717 * getchar (MSVCRT.@)
1719 int MSVCRT_getchar(void)
1721 return MSVCRT_fgetc(MSVCRT_stdin
);
1724 /*********************************************************************
1727 int MSVCRT_getc(MSVCRT_FILE
* file
)
1729 return MSVCRT_fgetc(file
);
1732 /*********************************************************************
1735 char *MSVCRT_gets(char *buf
)
1738 char * buf_start
= buf
;
1740 /* BAD, for the whole WINE process blocks... just done this way to test
1741 * windows95's ftp.exe.
1742 * JG 19/9/00: Is this still true, now we are using ReadFile?
1744 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
1745 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
1746 if(cc
!= '\r') *buf
++ = (char)cc
;
1750 TRACE("got '%s'\n", buf_start
);
1754 /*********************************************************************
1757 int MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
1759 return MSVCRT_fputc(c
, file
);
1762 /*********************************************************************
1763 * putchar (MSVCRT.@)
1765 int MSVCRT_putchar(int c
)
1767 return MSVCRT_fputc(c
, MSVCRT_stdout
);
1770 /*********************************************************************
1773 int MSVCRT_puts(const char *s
)
1775 int retval
= MSVCRT_EOF
;
1776 if (MSVCRT_fwrite(s
,strlen(s
),1,MSVCRT_stdout
) == 1)
1777 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
1781 /*********************************************************************
1784 int _putws(const WCHAR
*s
)
1786 static const WCHAR nl
= (WCHAR
)L
'\n';
1787 if (MSVCRT_fwrite(s
,strlenW(s
),1,MSVCRT_stdout
) == 1)
1788 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
1792 /*********************************************************************
1795 int MSVCRT_remove(const char *path
)
1797 TRACE("(%s)\n",path
);
1798 if (DeleteFileA(path
))
1800 TRACE(":failed (%ld)\n",GetLastError());
1801 MSVCRT__set_errno(GetLastError());
1805 /*********************************************************************
1806 * _wremove (MSVCRT.@)
1808 int _wremove(const WCHAR
*path
)
1810 TRACE("(%s)\n",debugstr_w(path
));
1811 if (DeleteFileW(path
))
1813 TRACE(":failed (%ld)\n",GetLastError());
1814 MSVCRT__set_errno(GetLastError());
1818 /*********************************************************************
1821 int MSVCRT_scanf(const char *format
, ...)
1826 va_start(valist
, format
);
1827 res
= MSVCRT_fscanf(MSVCRT_stdin
, format
, valist
);
1832 /*********************************************************************
1835 int MSVCRT_rename(const char *oldpath
,const char *newpath
)
1837 TRACE(":from %s to %s\n",oldpath
,newpath
);
1838 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_REPLACE_EXISTING
))
1840 TRACE(":failed (%ld)\n",GetLastError());
1841 MSVCRT__set_errno(GetLastError());
1845 /*********************************************************************
1846 * _wrename (MSVCRT.@)
1848 int _wrename(const WCHAR
*oldpath
,const WCHAR
*newpath
)
1850 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
1851 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_REPLACE_EXISTING
))
1853 TRACE(":failed (%ld)\n",GetLastError());
1854 MSVCRT__set_errno(GetLastError());
1858 /*********************************************************************
1859 * setvbuf (MSVCRT.@)
1861 int MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
1863 FIXME("(%p,%p,%d,%d)stub\n",file
, buf
, mode
, size
);
1867 /*********************************************************************
1870 void MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
1872 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, BUFSIZ
);
1875 /*********************************************************************
1878 char *MSVCRT_tmpnam(char *s
)
1880 char tmpbuf
[MAX_PATH
];
1881 char* prefix
= "TMP";
1882 if (!GetTempPathA(MAX_PATH
,tmpbuf
) ||
1883 !GetTempFileNameA(tmpbuf
,prefix
,0,MSVCRT_tmpname
))
1885 TRACE(":failed-last error (%ld)\n",GetLastError());
1888 TRACE(":got tmpnam %s\n",MSVCRT_tmpname
);
1893 /*********************************************************************
1894 * tmpfile (MSVCRT.@)
1896 MSVCRT_FILE
* MSVCRT_tmpfile(void)
1898 char *filename
= MSVCRT_tmpnam(NULL
);
1900 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
1902 return msvcrt_alloc_fp(fd
);
1906 /*********************************************************************
1907 * vfprintf (MSVCRT.@)
1909 int MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, va_list valist
)
1911 char buf
[2048], *mem
= buf
;
1912 int written
, resize
= sizeof(buf
), retval
;
1913 /* There are two conventions for vsnprintf failing:
1914 * Return -1 if we truncated, or
1915 * Return the number of bytes that would have been written
1916 * The code below handles both cases
1918 while ((written
= vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
1921 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
1924 if (!(mem
= (char *)MSVCRT_malloc(resize
)))
1927 retval
= MSVCRT_fwrite(mem
, 1, written
, file
);
1933 /*********************************************************************
1934 * vfwprintf (MSVCRT.@)
1936 int MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const WCHAR
*format
, va_list valist
)
1938 WCHAR buf
[2048], *mem
= buf
;
1939 int written
, resize
= sizeof(buf
) / sizeof(WCHAR
), retval
;
1940 /* See vfprintf comments */
1941 while ((written
= _vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
1944 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(WCHAR
));
1947 if (!(mem
= (WCHAR
*)MSVCRT_malloc(resize
)))
1950 retval
= MSVCRT_fwrite(mem
, 1, written
* sizeof (WCHAR
), file
);
1956 /*********************************************************************
1957 * vprintf (MSVCRT.@)
1959 int MSVCRT_vprintf(const char *format
, va_list valist
)
1961 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
1964 /*********************************************************************
1965 * vwprintf (MSVCRT.@)
1967 int MSVCRT_vwprintf(const WCHAR
*format
, va_list valist
)
1969 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
1972 /*********************************************************************
1973 * fprintf (MSVCRT.@)
1975 int MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
1979 va_start(valist
, format
);
1980 res
= MSVCRT_vfprintf(file
, format
, valist
);
1985 /*********************************************************************
1986 * fwprintf (MSVCRT.@)
1988 int MSVCRT_fwprintf(MSVCRT_FILE
* file
, const WCHAR
*format
, ...)
1992 va_start(valist
, format
);
1993 res
= MSVCRT_vfwprintf(file
, format
, valist
);
1998 /*********************************************************************
2001 int MSVCRT_printf(const char *format
, ...)
2005 va_start(valist
, format
);
2006 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
2011 /*********************************************************************
2012 * wprintf (MSVCRT.@)
2014 int MSVCRT_wprintf(const WCHAR
*format
, ...)
2018 va_start(valist
, format
);
2019 res
= MSVCRT_vwprintf(format
, valist
);