2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
39 #include "msvcrt/errno.h"
41 #include "wine/unicode.h"
42 #include "msvcrt/direct.h"
43 #include "msvcrt/fcntl.h"
44 #include "msvcrt/io.h"
45 #include "msvcrt/sys/locking.h"
46 #include "msvcrt/stdio.h"
47 #include "msvcrt/stdlib.h"
48 #include "msvcrt/string.h"
49 #include "msvcrt/sys/stat.h"
50 #include "msvcrt/sys/utime.h"
51 #include "msvcrt/time.h"
52 #include "msvcrt/share.h"
53 #include "msvcrt/wctype.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
59 /* for stat mode, permissions apply to all,owner and group */
60 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
61 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
62 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
64 /* _access() bit flags FIXME: incomplete */
65 #define MSVCRT_W_OK 0x02
68 /* FIXME: Make this dynamic */
69 #define MSVCRT_MAX_FILES 257
71 HANDLE MSVCRT_handles
[MSVCRT_MAX_FILES
];
72 MSVCRT_FILE
* MSVCRT_files
[MSVCRT_MAX_FILES
];
73 int MSVCRT_flags
[MSVCRT_MAX_FILES
];
74 char *MSVCRT_tempfiles
[MSVCRT_MAX_FILES
];
75 MSVCRT_FILE MSVCRT__iob
[3];
76 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
77 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
78 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
80 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
81 static int MSVCRT_fdend
= 3; /* highest allocated fd */
83 /* INTERNAL: process umask */
84 static int MSVCRT_umask
= 0;
86 /* INTERNAL: Static buffer for temp file name */
87 static char MSVCRT_tmpname
[MAX_PATH
];
89 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
90 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
91 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
92 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
94 #define TOUL(x) (ULONGLONG)(x)
95 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
96 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
97 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
98 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
100 extern CRITICAL_SECTION MSVCRT_file_cs
;
101 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
102 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
104 static void msvcrt_cp_from_stati64(const struct _stati64
*bufi64
, struct _stat
*buf
)
106 buf
->st_dev
= bufi64
->st_dev
;
107 buf
->st_ino
= bufi64
->st_ino
;
108 buf
->st_mode
= bufi64
->st_mode
;
109 buf
->st_nlink
= bufi64
->st_nlink
;
110 buf
->st_uid
= bufi64
->st_uid
;
111 buf
->st_gid
= bufi64
->st_gid
;
112 buf
->st_rdev
= bufi64
->st_rdev
;
113 buf
->st_size
= bufi64
->st_size
;
114 buf
->st_atime
= bufi64
->st_atime
;
115 buf
->st_mtime
= bufi64
->st_mtime
;
116 buf
->st_ctime
= bufi64
->st_ctime
;
119 /* INTERNAL: Get the HANDLE for a fd */
120 static HANDLE
msvcrt_fdtoh(int fd
)
122 if (fd
< 0 || fd
>= MSVCRT_fdend
||
123 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
125 WARN(":fd (%d) - no handle!\n",fd
);
127 *MSVCRT__errno() = MSVCRT_EBADF
;
128 return INVALID_HANDLE_VALUE
;
130 return MSVCRT_handles
[fd
];
133 /* INTERNAL: free a file entry fd */
134 static void msvcrt_free_fd(int fd
)
136 MSVCRT_handles
[fd
] = INVALID_HANDLE_VALUE
;
137 MSVCRT_files
[fd
] = 0;
138 MSVCRT_flags
[fd
] = 0;
139 TRACE(":fd (%d) freed\n",fd
);
141 return; /* dont use 0,1,2 for user files */
142 if (fd
== MSVCRT_fdend
- 1)
144 if (fd
< MSVCRT_fdstart
)
148 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
149 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
151 int fd
= MSVCRT_fdstart
;
153 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fd
);
154 if (fd
>= MSVCRT_MAX_FILES
)
156 WARN(":files exhausted!\n");
159 MSVCRT_handles
[fd
] = hand
;
160 MSVCRT_flags
[fd
] = flag
;
162 /* locate next free slot */
163 if (fd
== MSVCRT_fdend
)
164 MSVCRT_fdstart
= ++MSVCRT_fdend
;
166 while(MSVCRT_fdstart
< MSVCRT_fdend
&&
167 MSVCRT_handles
[MSVCRT_fdstart
] != INVALID_HANDLE_VALUE
)
173 /* INTERNAL: Allocate a FILE* for an fd slot
174 * This is done lazily to avoid memory wastage for low level open/write
175 * usage when a FILE* is not requested (but may be later).
177 static MSVCRT_FILE
* msvcrt_alloc_fp(int fd
)
179 TRACE(":fd (%d) allocating FILE*\n",fd
);
180 if (fd
< 0 || fd
>= MSVCRT_fdend
||
181 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
183 WARN(":invalid fd %d\n",fd
);
185 *MSVCRT__errno() = MSVCRT_EBADF
;
188 if (!MSVCRT_files
[fd
])
190 if ((MSVCRT_files
[fd
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
192 MSVCRT_files
[fd
]->_file
= fd
;
193 MSVCRT_files
[fd
]->_flag
= MSVCRT_flags
[fd
];
194 MSVCRT_files
[fd
]->_flag
&= ~MSVCRT__IOAPPEND
; /* mask out, see above */
197 TRACE(":got FILE* (%p)\n",MSVCRT_files
[fd
]);
198 return MSVCRT_files
[fd
];
202 /* INTERNAL: Set up stdin, stderr and stdout */
203 void msvcrt_init_io(void)
206 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
207 MSVCRT_handles
[0] = GetStdHandle(STD_INPUT_HANDLE
);
208 MSVCRT_flags
[0] = MSVCRT__iob
[0]._flag
= MSVCRT__IOREAD
;
209 MSVCRT_handles
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
210 MSVCRT_flags
[1] = MSVCRT__iob
[1]._flag
= MSVCRT__IOWRT
;
211 MSVCRT_handles
[2] = GetStdHandle(STD_ERROR_HANDLE
);
212 MSVCRT_flags
[2] = MSVCRT__iob
[2]._flag
= MSVCRT__IOWRT
;
214 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles
[0],
215 MSVCRT_handles
[1],MSVCRT_handles
[2]);
217 for (i
= 0; i
< 3; i
++)
219 /* FILE structs for stdin/out/err are static and never deleted */
220 MSVCRT_files
[i
] = &MSVCRT__iob
[i
];
221 MSVCRT__iob
[i
]._file
= i
;
222 MSVCRT_tempfiles
[i
] = NULL
;
226 /* free everything on process exit */
227 void msvcrt_free_io(void)
235 /* INTERNAL: Flush stdio file buffer */
236 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
239 int cnt
=file
->_ptr
-file
->_base
;
240 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
243 file
->_ptr
=file
->_base
;
244 file
->_cnt
=file
->_bufsiz
;
249 /* INTERNAL: Allocate stdio file buffer */
250 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
252 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
254 file
->_bufsiz
= MSVCRT_BUFSIZ
;
255 file
->_flag
|= MSVCRT__IOMYBUF
;
257 file
->_base
= (unsigned char *)(&file
->_charbuf
);
259 file
->_bufsiz
= sizeof(file
->_charbuf
);
261 file
->_ptr
= file
->_base
;
265 /*********************************************************************
268 MSVCRT_FILE
*__p__iob(void)
270 return &MSVCRT__iob
[0];
273 /*********************************************************************
276 int _access(const char *filename
, int mode
)
278 DWORD attr
= GetFileAttributesA(filename
);
280 TRACE("(%s,%d) %ld\n",filename
,mode
,attr
);
282 if (!filename
|| attr
== 0xffffffff)
284 MSVCRT__set_errno(GetLastError());
287 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
289 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
295 /*********************************************************************
296 * _waccess (MSVCRT.@)
298 int _waccess(const MSVCRT_wchar_t
*filename
, int mode
)
300 DWORD attr
= GetFileAttributesW(filename
);
302 TRACE("(%s,%d) %ld\n",debugstr_w(filename
),mode
,attr
);
304 if (!filename
|| attr
== 0xffffffff)
306 MSVCRT__set_errno(GetLastError());
309 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
311 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
317 /*********************************************************************
320 int _chmod(const char *path
, int flags
)
322 DWORD oldFlags
= GetFileAttributesA(path
);
324 if (oldFlags
!= 0x0FFFFFFFF)
326 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
327 oldFlags
| FILE_ATTRIBUTE_READONLY
;
329 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
332 MSVCRT__set_errno(GetLastError());
336 /*********************************************************************
339 int _wchmod(const MSVCRT_wchar_t
*path
, int flags
)
341 DWORD oldFlags
= GetFileAttributesW(path
);
343 if (oldFlags
!= 0x0FFFFFFFF)
345 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
346 oldFlags
| FILE_ATTRIBUTE_READONLY
;
348 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
351 MSVCRT__set_errno(GetLastError());
355 /*********************************************************************
358 int _chsize(int fd
, long size
)
360 FIXME("(fd=%d, size=%ld): stub\n", fd
, size
);
364 /*********************************************************************
369 FIXME("(od=%d): stub\n", od
);
373 /*********************************************************************
376 int _dup2(int od
, int nd
)
378 FIXME("(od=%d, nd=%d): stub\n", od
, nd
);
382 /*********************************************************************
385 int _unlink(const char *path
)
387 TRACE("(%s)\n",path
);
388 if(DeleteFileA(path
))
390 TRACE("failed (%ld)\n",GetLastError());
391 MSVCRT__set_errno(GetLastError());
395 /*********************************************************************
396 * _wunlink (MSVCRT.@)
398 int _wunlink(const MSVCRT_wchar_t
*path
)
400 TRACE("(%s)\n",debugstr_w(path
));
401 if(DeleteFileW(path
))
403 TRACE("failed (%ld)\n",GetLastError());
404 MSVCRT__set_errno(GetLastError());
408 /*********************************************************************
413 HANDLE hand
= msvcrt_fdtoh(fd
);
415 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
416 if (hand
== INVALID_HANDLE_VALUE
)
418 /* flush stdio buffers */
419 if(MSVCRT_files
[fd
]) {
420 if(MSVCRT_files
[fd
]->_flag
& MSVCRT__IOWRT
)
421 MSVCRT_fflush(MSVCRT_files
[fd
]);
423 if(MSVCRT_files
[fd
]->_flag
& MSVCRT__IOMYBUF
)
424 MSVCRT_free(MSVCRT_files
[fd
]->_base
);
427 /* Dont free std FILE*'s, they are not dynamic */
428 if (fd
> 2 && MSVCRT_files
[fd
])
429 MSVCRT_free(MSVCRT_files
[fd
]);
433 if (!CloseHandle(hand
))
435 WARN(":failed-last error (%ld)\n",GetLastError());
436 MSVCRT__set_errno(GetLastError());
439 if (MSVCRT_tempfiles
[fd
])
441 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles
[fd
]);
442 _unlink(MSVCRT_tempfiles
[fd
]);
443 MSVCRT_free(MSVCRT_tempfiles
[fd
]);
444 MSVCRT_tempfiles
[fd
] = NULL
;
451 /*********************************************************************
456 HANDLE hand
= msvcrt_fdtoh(fd
);
458 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
459 if (hand
== INVALID_HANDLE_VALUE
)
462 if (!FlushFileBuffers(hand
))
464 if (GetLastError() == ERROR_INVALID_HANDLE
)
466 /* FlushFileBuffers fails for console handles
467 * so we ignore this error.
471 TRACE(":failed-last error (%ld)\n",GetLastError());
472 MSVCRT__set_errno(GetLastError());
479 /*********************************************************************
485 HANDLE hand
= msvcrt_fdtoh(fd
);
487 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
489 if (hand
== INVALID_HANDLE_VALUE
)
492 /* If we have a FILE* for this file, the EOF flag
493 * will be set by the read()/write() functions.
495 if (MSVCRT_files
[fd
])
496 return MSVCRT_flags
[fd
] & MSVCRT__IOEOF
;
498 /* Otherwise we do it the hard way */
499 curpos
= SetFilePointer(hand
, 0, NULL
, SEEK_CUR
);
500 endpos
= SetFilePointer(hand
, 0, NULL
, FILE_END
);
502 if (curpos
== endpos
)
505 SetFilePointer(hand
, curpos
, 0, FILE_BEGIN
);
509 /*********************************************************************
510 * _fcloseall (MSVCRT.@)
514 int num_closed
= 0, i
;
516 for (i
= 3; i
< MSVCRT_fdend
; i
++)
517 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
523 TRACE(":closed (%d) handles\n",num_closed
);
527 /*********************************************************************
530 __int64
_lseeki64(int fd
, __int64 offset
, int whence
)
532 DWORD ret
, hoffset
= (DWORD
) (offset
>> 32);
533 HANDLE hand
= msvcrt_fdtoh(fd
);
535 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
536 if (hand
== INVALID_HANDLE_VALUE
)
539 if (whence
< 0 || whence
> 2)
541 *MSVCRT__errno() = MSVCRT_EINVAL
;
545 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
546 fd
,hoffset
,(long)offset
,
547 (whence
==SEEK_SET
)?"SEEK_SET":
548 (whence
==SEEK_CUR
)?"SEEK_CUR":
549 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
551 if (((ret
= SetFilePointer(hand
, (long)offset
, &hoffset
,
552 whence
)) != INVALID_SET_FILE_POINTER
) || !GetLastError())
554 if (MSVCRT_files
[fd
])
555 MSVCRT_files
[fd
]->_flag
&= ~MSVCRT__IOEOF
;
556 /* FIXME: What if we seek _to_ EOF - is EOF set? */
558 return ((__int64
)hoffset
<< 32) | ret
;
560 TRACE(":error-last error (%ld)\n",GetLastError());
561 if (MSVCRT_files
[fd
])
562 switch(GetLastError())
564 case ERROR_NEGATIVE_SEEK
:
565 case ERROR_SEEK_ON_DEVICE
:
566 MSVCRT__set_errno(GetLastError());
567 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
575 /*********************************************************************
578 LONG
_lseek(int fd
, LONG offset
, int whence
)
580 return _lseeki64(fd
, offset
, whence
);
583 /*********************************************************************
584 * _locking (MSVCRT.@)
586 * This is untested; the underlying LockFile doesn't work yet.
588 int _locking(int fd
, int mode
, LONG nbytes
)
592 HANDLE hand
= msvcrt_fdtoh(fd
);
594 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
595 if (hand
== INVALID_HANDLE_VALUE
)
598 if (mode
< 0 || mode
> 4)
600 *MSVCRT__errno() = MSVCRT_EINVAL
;
604 TRACE(":fd (%d) by 0x%08lx mode %s\n",
605 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
606 (mode
==_LK_LOCK
)?"_LK_LOCK":
607 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
608 (mode
==_LK_RLCK
)?"_LK_RLCK":
609 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
612 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
614 FIXME ("Seek failed\n");
615 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
618 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
621 ret
= 1; /* just to satisfy gcc */
624 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
629 else if (mode
== _LK_UNLCK
)
630 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
632 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
633 /* FIXME - what about error settings? */
637 /*********************************************************************
640 void MSVCRT_rewind(MSVCRT_FILE
* file
)
642 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
643 MSVCRT_fseek(file
, 0L, SEEK_SET
);
644 MSVCRT_clearerr(file
);
647 /*********************************************************************
650 MSVCRT_FILE
* _fdopen(int fd
, const char *mode
)
652 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
654 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
659 /*********************************************************************
660 * _wfdopen (MSVCRT.@)
662 MSVCRT_FILE
* _wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
664 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
666 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
673 /*********************************************************************
674 * _filelength (MSVCRT.@)
676 LONG
_filelength(int fd
)
678 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
681 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
684 if (endPos
!= curPos
)
685 _lseek(fd
, curPos
, SEEK_SET
);
692 /*********************************************************************
695 int _fileno(MSVCRT_FILE
* file
)
697 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
701 /*********************************************************************
702 * _flushall (MSVCRT.@)
706 int num_flushed
= 0, i
= 3;
708 while(i
< MSVCRT_fdend
)
709 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
712 /* FIXME: flush, do not commit */
713 if (_commit(i
) == -1)
715 MSVCRT_files
[i
]->_flag
|= MSVCRT__IOERR
;
717 if(MSVCRT_files
[i
] && MSVCRT_files
[i
]->_flag
& MSVCRT__IOWRT
) {
718 MSVCRT_fflush(MSVCRT_files
[i
]);
723 TRACE(":flushed (%d) handles\n",num_flushed
);
727 /*********************************************************************
728 * _fstati64 (MSVCRT.@)
730 int _fstati64(int fd
, struct _stati64
* buf
)
733 BY_HANDLE_FILE_INFORMATION hfi
;
734 HANDLE hand
= msvcrt_fdtoh(fd
);
736 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
737 if (hand
== INVALID_HANDLE_VALUE
)
742 WARN(":failed-NULL buf\n");
743 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
747 memset(&hfi
, 0, sizeof(hfi
));
748 memset(buf
, 0, sizeof(struct _stati64
));
749 if (!GetFileInformationByHandle(hand
, &hfi
))
751 WARN(":failed-last error (%ld)\n",GetLastError());
752 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
755 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi
.dwFileAttributes
);
756 buf
->st_nlink
= hfi
.nNumberOfLinks
;
757 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
758 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
760 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
761 buf
->st_mtime
= buf
->st_ctime
= dw
;
765 /*********************************************************************
768 int MSVCRT__fstat(int fd
, struct _stat
* buf
)
770 struct _stati64 bufi64
;
772 ret
= _fstati64(fd
, &bufi64
);
774 msvcrt_cp_from_stati64(&bufi64
, buf
);
778 /*********************************************************************
781 int _futime(int fd
, struct _utimbuf
*t
)
783 HANDLE hand
= msvcrt_fdtoh(fd
);
788 MSVCRT_time_t currTime
;
789 MSVCRT_time(&currTime
);
790 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
791 memcpy(&wt
, &at
, sizeof(wt
));
795 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
796 if (t
->actime
== t
->modtime
)
797 memcpy(&wt
, &at
, sizeof(wt
));
799 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
802 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
804 MSVCRT__set_errno(GetLastError());
810 /*********************************************************************
811 * _get_osfhandle (MSVCRT.@)
813 long _get_osfhandle(int fd
)
815 HANDLE hand
= msvcrt_fdtoh(fd
);
816 HANDLE newhand
= hand
;
817 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
819 if (hand
!= INVALID_HANDLE_VALUE
)
821 /* FIXME: I'm not convinced that I should be copying the
822 * handle here - it may be leaked if the app doesn't
823 * close it (and the API docs dont say that it should)
824 * Not duplicating it means that it can't be inherited
825 * and so lcc's wedit doesn't cope when it passes it to
826 * child processes. I've an idea that it should either
827 * be copied by CreateProcess, or marked as inheritable
828 * when initialised, or maybe both? JG 21-9-00.
830 DuplicateHandle(GetCurrentProcess(),hand
,GetCurrentProcess(),
831 &newhand
,0,TRUE
,DUPLICATE_SAME_ACCESS
);
833 return (long)newhand
;
836 /*********************************************************************
841 HANDLE hand
= msvcrt_fdtoh(fd
);
843 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
844 if (hand
== INVALID_HANDLE_VALUE
)
847 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
850 /*********************************************************************
853 char *_mktemp(char *pattern
)
856 char *retVal
= pattern
;
861 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
865 id
= GetCurrentProcessId();
869 int tempNum
= id
/ 10;
870 *pattern
-- = id
- (tempNum
* 10) + '0';
876 if (GetFileAttributesA(retVal
) == 0xFFFFFFFF &&
877 GetLastError() == ERROR_FILE_NOT_FOUND
)
880 } while(letter
!= '|');
884 /*********************************************************************
885 * _wmktemp (MSVCRT.@)
887 MSVCRT_wchar_t
*_wmktemp(MSVCRT_wchar_t
*pattern
)
890 MSVCRT_wchar_t
*retVal
= pattern
;
892 MSVCRT_wchar_t letter
= 'a';
895 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
899 id
= GetCurrentProcessId();
903 int tempNum
= id
/ 10;
904 *pattern
-- = id
- (tempNum
* 10) + '0';
910 if (GetFileAttributesW(retVal
) == 0xFFFFFFFF &&
911 GetLastError() == ERROR_FILE_NOT_FOUND
)
914 } while(letter
!= '|');
918 /*********************************************************************
921 int MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
925 DWORD access
= 0, creation
= 0;
929 SECURITY_ATTRIBUTES sa
;
932 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
933 path
, oflags
, shflags
);
935 switch(oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
938 access
|= GENERIC_READ
;
939 ioflag
|= MSVCRT__IOREAD
;
942 access
|= GENERIC_WRITE
;
943 ioflag
|= MSVCRT__IOWRT
;
946 access
|= GENERIC_WRITE
| GENERIC_READ
;
947 ioflag
|= MSVCRT__IORW
;
951 if (oflags
& _O_CREAT
)
953 va_start(ap
, shflags
);
954 pmode
= va_arg(ap
, int);
957 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
958 FIXME(": pmode 0x%04x ignored\n", pmode
);
960 WARN(": pmode 0x%04x ignored\n", pmode
);
962 if (oflags
& _O_EXCL
)
963 creation
= CREATE_NEW
;
964 else if (oflags
& _O_TRUNC
)
965 creation
= CREATE_ALWAYS
;
967 creation
= OPEN_ALWAYS
;
969 else /* no _O_CREAT */
971 if (oflags
& _O_TRUNC
)
972 creation
= TRUNCATE_EXISTING
;
974 creation
= OPEN_EXISTING
;
976 if (oflags
& _O_APPEND
)
977 ioflag
|= MSVCRT__IOAPPEND
;
979 if (oflags
& _O_BINARY
)
981 else if (oflags
& _O_TEXT
)
983 else if (*__p__fmode() & _O_BINARY
)
986 ioflag
|= _O_TEXT
; /* default to TEXT*/
994 sharing
= FILE_SHARE_READ
;
997 sharing
= FILE_SHARE_WRITE
;
1000 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1003 ERR( "Unhandled shflags 0x%x\n", shflags
);
1007 if (oflags
& ~(_O_BINARY
|_O_TEXT
|_O_APPEND
|_O_TRUNC
|_O_EXCL
1008 |_O_CREAT
|_O_RDWR
|_O_TEMPORARY
|_O_NOINHERIT
))
1009 TRACE(":unsupported oflags 0x%04x\n",oflags
);
1011 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1012 sa
.lpSecurityDescriptor
= NULL
;
1013 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1015 hand
= CreateFileA(path
, access
, sharing
,
1016 &sa
, creation
, FILE_ATTRIBUTE_NORMAL
, 0);
1018 if (hand
== INVALID_HANDLE_VALUE
) {
1019 WARN(":failed-last error (%ld)\n",GetLastError());
1020 MSVCRT__set_errno(GetLastError());
1024 fd
= msvcrt_alloc_fd(hand
, ioflag
);
1026 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1030 if (oflags
& _O_TEMPORARY
)
1031 MSVCRT_tempfiles
[fd
] = _strdup(path
);
1032 if (ioflag
& MSVCRT__IOAPPEND
)
1033 _lseek(fd
, 0, FILE_END
);
1039 /*********************************************************************
1040 * _wsopen (MSVCRT.@)
1042 int MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1044 const unsigned int len
= strlenW(path
);
1045 char *patha
= MSVCRT_calloc(len
+ 1,1);
1049 va_start(ap
, shflags
);
1050 pmode
= va_arg(ap
, int);
1053 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1055 int retval
= MSVCRT__sopen(patha
,oflags
,shflags
,pmode
);
1060 MSVCRT__set_errno(GetLastError());
1064 /*********************************************************************
1067 int _open( const char *path
, int flags
, ... )
1071 if (flags
& _O_CREAT
)
1074 va_start(ap
, flags
);
1075 pmode
= va_arg(ap
, int);
1077 return MSVCRT__sopen( path
, flags
, _SH_DENYNO
, pmode
);
1080 return MSVCRT__sopen( path
, flags
, _SH_DENYNO
);
1083 /*********************************************************************
1086 int _wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1088 const unsigned int len
= strlenW(path
);
1089 char *patha
= MSVCRT_calloc(len
+ 1,1);
1093 va_start(ap
, flags
);
1094 pmode
= va_arg(ap
, int);
1097 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1099 int retval
= _open(patha
,flags
,pmode
);
1104 MSVCRT__set_errno(GetLastError());
1108 /*********************************************************************
1111 int _creat(const char *path
, int flags
)
1113 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1114 return _open(path
, usedFlags
);
1117 /*********************************************************************
1118 * _wcreat (MSVCRT.@)
1120 int _wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1122 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1123 return _wopen(path
, usedFlags
);
1126 /*********************************************************************
1127 * _open_osfhandle (MSVCRT.@)
1129 int _open_osfhandle(long hand
, int flags
)
1133 /* _O_RDONLY (0) always matches, so set the read flag
1134 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1135 * file, so set the write flag. It also only sets _O_TEXT if it wants
1136 * text - it never sets _O_BINARY.
1138 /* FIXME: handle more flags */
1139 flags
|= MSVCRT__IOREAD
|MSVCRT__IOWRT
;
1140 if ( !( flags
& _O_TEXT
) ) flags
|= _O_BINARY
;
1142 fd
= msvcrt_alloc_fd((HANDLE
)hand
,flags
);
1143 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand
,fd
, flags
);
1147 /*********************************************************************
1152 int num_removed
= 0, i
;
1154 for (i
= 3; i
< MSVCRT_fdend
; i
++)
1155 if (MSVCRT_tempfiles
[i
])
1162 TRACE(":removed (%d) temp files\n",num_removed
);
1166 /*********************************************************************
1167 * (internal) remove_cr
1169 * Remove all \r inplace.
1170 * return the number of \r removed
1172 static unsigned int remove_cr(char *buf
, unsigned int count
)
1176 for (i
= 0; i
< count
; i
++) if (buf
[i
] == '\r') break;
1177 for (j
= i
+ 1; j
< count
; j
++) if (buf
[j
] != '\r') buf
[i
++] = buf
[j
];
1181 /*********************************************************************
1184 int _read(int fd
, void *buf
, unsigned int count
)
1186 DWORD num_read
, all_read
=0;
1187 char *bufstart
= buf
;
1188 HANDLE hand
= msvcrt_fdtoh(fd
);
1190 /* Dont trace small reads, it gets *very* annoying */
1192 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1193 if (hand
== INVALID_HANDLE_VALUE
)
1196 /* Reading single bytes in O_TEXT mode makes things slow
1197 * So read big chunks, then remove the \r in memory and try reading
1198 * the rest until the request is satisfied or EOF is met
1200 while ( all_read
< count
)
1202 if (ReadFile(hand
, bufstart
+all_read
, count
- all_read
, &num_read
, NULL
))
1204 if (num_read
!= (count
- all_read
))
1207 if ( MSVCRT_files
[fd
])
1209 MSVCRT_flags
[fd
] |= MSVCRT__IOEOF
;
1211 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1214 if ((MSVCRT_flags
[fd
]& _O_BINARY
) != _O_BINARY
)
1215 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1216 all_read
+= num_read
;
1218 TRACE("%s\n",debugstr_an(buf
,all_read
));
1221 if ((MSVCRT_flags
[fd
]& _O_BINARY
) != _O_BINARY
)
1223 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1225 all_read
+= num_read
;
1229 TRACE(":failed-last error (%ld)\n",GetLastError());
1230 if (MSVCRT_files
[fd
])
1231 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1237 TRACE("%s\n",debugstr_an(buf
, all_read
));
1241 /*********************************************************************
1244 int _getw(MSVCRT_FILE
* file
)
1247 if (_read(file
->_file
, &i
, sizeof(int)) != 1)
1252 /*********************************************************************
1253 * _setmode (MSVCRT.@)
1255 int _setmode(int fd
,int mode
)
1257 int ret
= MSVCRT_flags
[fd
] & (_O_TEXT
| _O_BINARY
);
1258 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1259 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1260 MSVCRT_flags
[fd
] &= ~(_O_TEXT
|_O_BINARY
);
1261 MSVCRT_flags
[fd
] |= mode
& (_O_TEXT
| _O_BINARY
);
1265 /*********************************************************************
1266 * _stati64 (MSVCRT.@)
1268 int _stati64(const char* path
, struct _stati64
* buf
)
1271 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1272 unsigned short mode
= MSVCRT_S_IREAD
;
1275 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1277 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1279 TRACE("failed (%ld)\n",GetLastError());
1280 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1284 memset(buf
,0,sizeof(struct _stati64
));
1286 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1287 Bon 011120: This FIXME seems incorrect
1288 Also a letter as first char isn't enough to be classify
1291 if (isalpha(*path
)&& (*(path
+1)==':'))
1292 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1294 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1296 plen
= strlen(path
);
1298 /* Dir, or regular file? */
1299 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1300 (path
[plen
-1] == '\\'))
1301 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1306 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1308 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1309 (tolower(path
[plen
-3]) << 16);
1310 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1311 mode
|= MSVCRT_S_IEXEC
;
1315 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1316 mode
|= MSVCRT_S_IWRITE
;
1318 buf
->st_mode
= mode
;
1320 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1321 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1323 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1324 buf
->st_mtime
= buf
->st_ctime
= dw
;
1325 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1326 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1327 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1331 /*********************************************************************
1334 int MSVCRT__stat(const char* path
, struct _stat
* buf
)
1336 struct _stati64 bufi64
;
1338 ret
= _stati64( path
, &bufi64
);
1340 msvcrt_cp_from_stati64(&bufi64
, buf
);
1344 /*********************************************************************
1347 int _wstat(const MSVCRT_wchar_t
* path
, struct _stat
* buf
)
1350 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1351 unsigned short mode
= MSVCRT_S_IREAD
;
1354 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1356 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1358 TRACE("failed (%ld)\n",GetLastError());
1359 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1363 memset(buf
,0,sizeof(struct _stat
));
1365 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1366 if (MSVCRT_iswalpha(*path
))
1367 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1369 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1371 plen
= strlenW(path
);
1373 /* Dir, or regular file? */
1374 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1375 (path
[plen
-1] == '\\'))
1376 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1381 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1383 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1384 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1385 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1386 mode
|= MSVCRT_S_IEXEC
;
1390 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1391 mode
|= MSVCRT_S_IWRITE
;
1393 buf
->st_mode
= mode
;
1395 buf
->st_size
= hfi
.nFileSizeLow
;
1396 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1398 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1399 buf
->st_mtime
= buf
->st_ctime
= dw
;
1400 TRACE("\n%d %d %d %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,buf
->st_size
,
1401 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1405 /*********************************************************************
1410 return _lseek(fd
, 0, SEEK_CUR
);
1413 /*********************************************************************
1414 * _tempnam (MSVCRT.@)
1416 char *_tempnam(const char *dir
, const char *prefix
)
1418 char tmpbuf
[MAX_PATH
];
1420 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1421 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1423 TRACE("got name (%s)\n",tmpbuf
);
1424 DeleteFileA(tmpbuf
);
1425 return _strdup(tmpbuf
);
1427 TRACE("failed (%ld)\n",GetLastError());
1431 /*********************************************************************
1432 * _wtempnam (MSVCRT.@)
1434 MSVCRT_wchar_t
*_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
1436 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
1438 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1439 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1441 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1442 DeleteFileW(tmpbuf
);
1443 return _wcsdup(tmpbuf
);
1445 TRACE("failed (%ld)\n",GetLastError());
1449 /*********************************************************************
1452 int _umask(int umask
)
1454 int old_umask
= MSVCRT_umask
;
1455 TRACE("(%d)\n",umask
);
1456 MSVCRT_umask
= umask
;
1460 /*********************************************************************
1463 int _utime(const char* path
, struct _utimbuf
*t
)
1465 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
1469 int retVal
= _futime(fd
, t
);
1476 /*********************************************************************
1477 * _wutime (MSVCRT.@)
1479 int _wutime(const MSVCRT_wchar_t
* path
, struct _utimbuf
*t
)
1481 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
1485 int retVal
= _futime(fd
, t
);
1492 /*********************************************************************
1495 int _write(int fd
, const void* buf
, unsigned int count
)
1498 HANDLE hand
= msvcrt_fdtoh(fd
);
1500 /* Dont trace small writes, it gets *very* annoying */
1503 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1505 if (hand
== INVALID_HANDLE_VALUE
)
1507 *MSVCRT__errno() = MSVCRT_EBADF
;
1511 /* If appending, go to EOF */
1512 if (MSVCRT_flags
[fd
] & MSVCRT__IOAPPEND
)
1513 _lseek(fd
, 0, FILE_END
);
1515 if (MSVCRT_flags
[fd
] & _O_BINARY
)
1517 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1518 && (num_written
== count
))
1520 TRACE(":failed-last error (%ld)\n",GetLastError());
1521 if (MSVCRT_files
[fd
])
1523 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1524 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1529 unsigned int i
, j
, nr_lf
;
1530 char *s
=(char*)buf
, *buf_start
=(char*)buf
, *p
;
1531 /* find number of \n ( without preceeding \r */
1532 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
1537 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1542 if ((p
= MSVCRT_malloc(count
+ nr_lf
)))
1544 for(s
=(char*)buf
, i
=0, j
=0; i
<count
; i
++)
1549 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1556 FIXME("Malloc failed\n");
1564 if ((WriteFile(hand
, p
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
1566 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written
);
1567 if (MSVCRT_files
[fd
])
1569 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1570 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1573 return s
- buf_start
;
1586 /*********************************************************************
1589 int _putw(int val
, MSVCRT_FILE
* file
)
1591 return _write(file
->_file
, &val
, sizeof(val
)) == 1? val
: MSVCRT_EOF
;
1594 /*********************************************************************
1595 * clearerr (MSVCRT.@)
1597 void MSVCRT_clearerr(MSVCRT_FILE
* file
)
1599 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1600 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1603 /*********************************************************************
1606 int MSVCRT_fclose(MSVCRT_FILE
* file
)
1611 r
=_close(file
->_file
);
1612 return ((r
==MSVCRT_EOF
) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
1615 /*********************************************************************
1618 int MSVCRT_feof(MSVCRT_FILE
* file
)
1620 return file
->_flag
& MSVCRT__IOEOF
;
1623 /*********************************************************************
1626 int MSVCRT_ferror(MSVCRT_FILE
* file
)
1628 return file
->_flag
& MSVCRT__IOERR
;
1631 /*********************************************************************
1634 int MSVCRT_fflush(MSVCRT_FILE
* file
)
1640 int res
=msvcrt_flush_buffer(file
);
1645 /*********************************************************************
1648 int MSVCRT_fgetc(MSVCRT_FILE
* file
)
1652 return *(unsigned char *)file
->_ptr
++;
1654 return _filbuf(file
);
1658 /*********************************************************************
1659 * _fgetchar (MSVCRT.@)
1663 return MSVCRT_fgetc(MSVCRT_stdin
);
1666 /*********************************************************************
1667 * _filbuf (MSVCRT.@)
1669 int _filbuf(MSVCRT_FILE
* file
)
1672 /* Allocate buffer if needed */
1673 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
1674 msvcrt_alloc_buffer(file
);
1676 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
1677 if(file
->_flag
& MSVCRT__IORW
) {
1678 file
->_flag
|= MSVCRT__IOREAD
;
1683 if(file
->_flag
& MSVCRT__IONBF
) {
1685 if (_read(file
->_file
,&c
,1) != 1) {
1686 file
->_flag
|= MSVCRT__IOEOF
;
1691 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
1692 if(file
->_cnt
<0) file
->_cnt
= 0;
1694 file
->_flag
|= MSVCRT__IOEOF
;
1698 file
->_ptr
= file
->_base
+1;
1699 return *(unsigned char *)file
->_base
;
1703 /*********************************************************************
1704 * fgetpos (MSVCRT.@)
1706 int MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
1708 *pos
= MSVCRT_ftell(file
);
1709 return (*pos
== -1? -1 : 0);
1712 /*********************************************************************
1715 char *MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
1718 char * buf_start
= s
;
1720 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1721 file
,file
->_file
,s
,size
);
1723 for(cc
= MSVCRT_fgetc(file
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
1724 cc
= MSVCRT_fgetc(file
))
1725 /* _read already handled the translation */
1727 if (--size
<= 0) break;
1730 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1732 TRACE(":nothing read\n");
1739 TRACE(":got '%s'\n", debugstr_a(buf_start
));
1743 /*********************************************************************
1746 * In _O_TEXT mode, bultibyte characters are read from the file, dropping
1747 * the CR from CR/LF combinations
1749 MSVCRT_wint_t
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
1753 if (file
->_flag
& _O_BINARY
)
1756 if (_read(file
->_file
, &wc
, sizeof(wc
)) != sizeof(wc
))
1760 c
= MSVCRT_fgetc(file
);
1761 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c
))
1763 FIXME("Treat Multibyte characters\n");
1765 if (c
== MSVCRT_EOF
)
1768 return (MSVCRT_wint_t
)c
;
1771 /*********************************************************************
1774 MSVCRT_wint_t
MSVCRT_getwc(MSVCRT_FILE
* file
)
1776 return MSVCRT_fgetwc(file
);
1779 /*********************************************************************
1780 * _fgetwchar (MSVCRT.@)
1782 MSVCRT_wint_t
_fgetwchar(void)
1784 return MSVCRT_fgetwc(MSVCRT_stdin
);
1787 /*********************************************************************
1788 * getwchar (MSVCRT.@)
1790 MSVCRT_wint_t
MSVCRT_getwchar(void)
1792 return _fgetwchar();
1795 /*********************************************************************
1798 MSVCRT_wchar_t
*MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
1801 MSVCRT_wchar_t
* buf_start
= s
;
1803 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1804 file
,file
->_file
,s
,size
);
1806 for(cc
= MSVCRT_fgetwc(file
); cc
!= MSVCRT_WEOF
&& cc
!= L
'\n';
1807 cc
= MSVCRT_fgetwc(file
))
1808 /* _read already handled the translation */
1810 if (--size
<= 0) break;
1813 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1815 TRACE(":nothing read\n");
1822 /* TRACE(":got '%s'\n", buf_start); */
1827 /*********************************************************************
1830 MSVCRT_wint_t
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
1832 MSVCRT_wchar_t mwc
=wc
;
1833 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
1838 /*********************************************************************
1839 * _fputwchar (MSVCRT.@)
1841 MSVCRT_wint_t
_fputwchar(MSVCRT_wint_t wc
)
1843 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
1846 /*********************************************************************
1849 MSVCRT_FILE
* MSVCRT_fopen(const char *path
, const char *mode
)
1852 int flags
= 0, plus
= 0, fd
;
1853 const char* search
= mode
;
1855 TRACE("(%s,%s)\n",path
,mode
);
1858 if (*search
++ == '+')
1861 /* map mode string to open() flags. "man fopen" for possibilities. */
1865 flags
= (plus
? _O_RDWR
: _O_RDONLY
);
1868 flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1871 flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1886 flags
&= ~_O_BINARY
;
1891 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1894 fd
= _open(path
, flags
);
1899 file
= msvcrt_alloc_fp(fd
);
1900 TRACE(":got (%p)\n",file
);
1907 /*********************************************************************
1908 * _wfopen (MSVCRT.@)
1910 MSVCRT_FILE
*_wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
1912 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
1913 char *patha
= MSVCRT_calloc(plen
+ 1, 1);
1914 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
1916 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
1918 if (patha
&& modea
&&
1919 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
1920 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
1922 MSVCRT_FILE
*retval
= MSVCRT_fopen(patha
,modea
);
1928 MSVCRT__set_errno(GetLastError());
1932 /*********************************************************************
1933 * _fsopen (MSVCRT.@)
1935 MSVCRT_FILE
* _fsopen(const char *path
, const char *mode
, int share
)
1937 FIXME(":(%s,%s,%d),ignoring share mode!\n",path
,mode
,share
);
1938 return MSVCRT_fopen(path
,mode
);
1941 /*********************************************************************
1942 * _wfsopen (MSVCRT.@)
1944 MSVCRT_FILE
* _wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
1946 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1947 debugstr_w(path
),debugstr_w(mode
),share
);
1948 return _wfopen(path
,mode
);
1951 /*********************************************************************
1954 int MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
1961 return _flsbuf(c
, file
);
1965 /*********************************************************************
1966 * _flsbuf (MSVCRT.@)
1968 int _flsbuf(int c
, MSVCRT_FILE
* file
)
1970 /* Flush output buffer */
1971 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
1972 msvcrt_alloc_buffer(file
);
1974 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
1975 if(file
->_flag
& MSVCRT__IORW
) {
1976 file
->_flag
|= MSVCRT__IOWRT
;
1982 int res
=msvcrt_flush_buffer(file
);
1983 return res
?res
: MSVCRT_fputc(c
, file
);
1986 return _write(file
->_file
, &cc
, 1) == 1? c
: MSVCRT_EOF
;
1990 /*********************************************************************
1991 * _fputchar (MSVCRT.@)
1993 int _fputchar(int c
)
1995 return MSVCRT_fputc(c
, MSVCRT_stdout
);
1998 /*********************************************************************
2001 MSVCRT_size_t
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2002 { MSVCRT_size_t rcnt
=size
* nmemb
;
2003 MSVCRT_size_t read
=0;
2005 /* first buffered data */
2007 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2008 memcpy(ptr
, file
->_ptr
, pcnt
);
2013 ptr
= (char*)ptr
+ pcnt
;
2014 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2015 if(file
->_flag
& MSVCRT__IORW
) {
2016 file
->_flag
|= MSVCRT__IOREAD
;
2020 if(rcnt
) pread
= _read(file
->_file
,ptr
, rcnt
);
2021 if (MSVCRT_flags
[file
->_file
] & MSVCRT__IOEOF
)
2022 /* expose feof condition in the flags
2023 MFC tests file->_flag for feof, and doesn't not call feof())
2025 file
->_flag
|= MSVCRT__IOEOF
;
2032 /*********************************************************************
2033 * freopen (MSVCRT.@)
2036 MSVCRT_FILE
* MSVCRT_freopen(const char *path
, const char *mode
,MSVCRT_FILE
* file
)
2038 MSVCRT_FILE
* newfile
;
2041 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2042 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2048 FIXME(":reopen on user file not implemented!\n");
2049 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED
);
2052 if(MSVCRT_fclose(file
))
2054 return MSVCRT_fopen(path
, mode
);
2057 /* first, create the new file */
2058 if ((newfile
= MSVCRT_fopen(path
,mode
)) == NULL
)
2061 if (fd
< 3 && SetStdHandle(fd
== 0 ? STD_INPUT_HANDLE
:
2062 (fd
== 1? STD_OUTPUT_HANDLE
: STD_ERROR_HANDLE
),
2063 MSVCRT_handles
[newfile
->_file
]))
2065 /* Redirecting std handle to file , copy over.. */
2066 MSVCRT_handles
[fd
] = MSVCRT_handles
[newfile
->_file
];
2067 MSVCRT_flags
[fd
] = MSVCRT_flags
[newfile
->_file
];
2068 memcpy(&MSVCRT__iob
[fd
], newfile
, sizeof (MSVCRT_FILE
));
2069 MSVCRT__iob
[fd
]._file
= fd
;
2070 /* And free up the resources allocated by fopen, but
2071 * not the HANDLE we copied. */
2072 MSVCRT_free(MSVCRT_files
[fd
]);
2073 msvcrt_free_fd(newfile
->_file
);
2074 return &MSVCRT__iob
[fd
];
2077 WARN(":failed-last error (%ld)\n",GetLastError());
2078 MSVCRT_fclose(newfile
);
2079 MSVCRT__set_errno(GetLastError());
2083 /*********************************************************************
2084 * fsetpos (MSVCRT.@)
2086 int MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2088 return _lseek(file
->_file
,*pos
,SEEK_SET
);
2091 /*********************************************************************
2094 int MSVCRT_fseek(MSVCRT_FILE
* file
, long offset
, int whence
)
2096 /* Flush output if needed */
2097 if(file
->_flag
& MSVCRT__IOWRT
)
2098 msvcrt_flush_buffer(file
);
2100 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
2101 offset
-= file
->_cnt
;
2103 /* Discard buffered input */
2105 file
->_ptr
= file
->_base
;
2106 /* Reset direction of i/o */
2107 if(file
->_flag
& MSVCRT__IORW
) {
2108 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
2110 return (_lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
2113 /*********************************************************************
2116 LONG
MSVCRT_ftell(MSVCRT_FILE
* file
)
2121 if( file
->_flag
& MSVCRT__IOWRT
) {
2122 off
= file
->_ptr
- file
->_base
;
2127 pos
= _tell(file
->_file
);
2128 if(pos
== -1) return pos
;
2132 /*********************************************************************
2135 MSVCRT_size_t
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2137 MSVCRT_size_t wrcnt
=size
* nmemb
;
2142 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2143 memcpy(file
->_ptr
, ptr
, pcnt
);
2148 ptr
= (char*)ptr
+ pcnt
;
2149 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2150 if(file
->_flag
& MSVCRT__IORW
) {
2151 file
->_flag
|= MSVCRT__IOWRT
;
2157 int res
=msvcrt_flush_buffer(file
);
2159 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2160 if (pwritten
<= 0) pwritten
=0;
2161 written
+= pwritten
;
2164 return written
/ size
;
2167 /*********************************************************************
2170 int MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
2172 size_t i
, len
= strlen(s
);
2173 if (file
->_flag
& _O_BINARY
)
2174 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2175 for (i
=0; i
<len
; i
++)
2176 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
2181 /*********************************************************************
2184 int MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
2186 size_t i
, len
= strlenW(s
);
2187 if (file
->_flag
& _O_BINARY
)
2188 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2189 for (i
=0; i
<len
; i
++)
2191 if ((s
[i
] == L
'\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
2193 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
2199 /*********************************************************************
2200 * getchar (MSVCRT.@)
2202 int MSVCRT_getchar(void)
2204 return MSVCRT_fgetc(MSVCRT_stdin
);
2207 /*********************************************************************
2210 int MSVCRT_getc(MSVCRT_FILE
* file
)
2212 return MSVCRT_fgetc(file
);
2215 /*********************************************************************
2218 char *MSVCRT_gets(char *buf
)
2221 char * buf_start
= buf
;
2223 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
2224 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
2225 if(cc
!= '\r') *buf
++ = (char)cc
;
2229 TRACE("got '%s'\n", buf_start
);
2233 /*********************************************************************
2236 MSVCRT_wchar_t
* MSVCRT__getws(MSVCRT_wchar_t
* buf
)
2239 MSVCRT_wchar_t
* ws
= buf
;
2241 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
2242 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
2245 *buf
++ = (MSVCRT_wchar_t
)cc
;
2249 TRACE("got '%s'\n", debugstr_w(ws
));
2253 /*********************************************************************
2256 int MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
2258 return MSVCRT_fputc(c
, file
);
2261 /*********************************************************************
2262 * putchar (MSVCRT.@)
2264 int MSVCRT_putchar(int c
)
2266 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2269 /*********************************************************************
2272 int MSVCRT_puts(const char *s
)
2274 size_t len
= strlen(s
);
2275 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2276 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2279 /*********************************************************************
2282 int _putws(const MSVCRT_wchar_t
*s
)
2284 static const MSVCRT_wchar_t nl
= '\n';
2285 size_t len
= strlenW(s
);
2286 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2287 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2290 /*********************************************************************
2293 int MSVCRT_remove(const char *path
)
2295 TRACE("(%s)\n",path
);
2296 if (DeleteFileA(path
))
2298 TRACE(":failed (%ld)\n",GetLastError());
2299 MSVCRT__set_errno(GetLastError());
2303 /*********************************************************************
2304 * _wremove (MSVCRT.@)
2306 int _wremove(const MSVCRT_wchar_t
*path
)
2308 TRACE("(%s)\n",debugstr_w(path
));
2309 if (DeleteFileW(path
))
2311 TRACE(":failed (%ld)\n",GetLastError());
2312 MSVCRT__set_errno(GetLastError());
2316 /*********************************************************************
2319 int MSVCRT_scanf(const char *format
, ...)
2324 va_start(valist
, format
);
2325 res
= MSVCRT_fscanf(MSVCRT_stdin
, format
, valist
);
2330 /*********************************************************************
2333 int MSVCRT_wscanf(const MSVCRT_wchar_t
*format
, ...)
2338 va_start(valist
, format
);
2339 res
= MSVCRT_fwscanf(MSVCRT_stdin
, format
, valist
);
2344 /*********************************************************************
2347 int MSVCRT_rename(const char *oldpath
,const char *newpath
)
2349 TRACE(":from %s to %s\n",oldpath
,newpath
);
2350 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2352 TRACE(":failed (%ld)\n",GetLastError());
2353 MSVCRT__set_errno(GetLastError());
2357 /*********************************************************************
2358 * _wrename (MSVCRT.@)
2360 int _wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
2362 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2363 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2365 TRACE(":failed (%ld)\n",GetLastError());
2366 MSVCRT__set_errno(GetLastError());
2370 /*********************************************************************
2371 * setvbuf (MSVCRT.@)
2373 int MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
2375 /* TODO: Check if file busy */
2377 MSVCRT_free(file
->_base
);
2381 if(mode
== MSVCRT__IOFBF
) {
2382 file
->_flag
&= ~MSVCRT__IONBF
;
2383 file
->_base
= file
->_ptr
= buf
;
2385 file
->_bufsiz
= size
;
2388 file
->_flag
|= MSVCRT__IONBF
;
2393 /*********************************************************************
2396 void MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
2398 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
2401 /*********************************************************************
2404 char *MSVCRT_tmpnam(char *s
)
2406 char tmpbuf
[MAX_PATH
];
2407 char* prefix
= "TMP";
2408 if (!GetTempPathA(MAX_PATH
,tmpbuf
) ||
2409 !GetTempFileNameA(tmpbuf
,prefix
,0,MSVCRT_tmpname
))
2411 TRACE(":failed-last error (%ld)\n",GetLastError());
2414 TRACE(":got tmpnam %s\n",MSVCRT_tmpname
);
2419 /*********************************************************************
2420 * tmpfile (MSVCRT.@)
2422 MSVCRT_FILE
* MSVCRT_tmpfile(void)
2424 char *filename
= MSVCRT_tmpnam(NULL
);
2426 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
2428 return msvcrt_alloc_fp(fd
);
2432 /*********************************************************************
2433 * vfprintf (MSVCRT.@)
2435 int MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, va_list valist
)
2437 char buf
[2048], *mem
= buf
;
2438 int written
, resize
= sizeof(buf
), retval
;
2439 /* There are two conventions for vsnprintf failing:
2440 * Return -1 if we truncated, or
2441 * Return the number of bytes that would have been written
2442 * The code below handles both cases
2444 while ((written
= vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
2447 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
2450 if (!(mem
= (char *)MSVCRT_malloc(resize
)))
2453 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2459 /*********************************************************************
2460 * vfwprintf (MSVCRT.@)
2462 * Is final char included in written (then resize is too big) or not
2463 * (then we must test for equality too)?
2465 int MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, va_list valist
)
2467 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
2468 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
2469 /* See vfprintf comments */
2470 while ((written
= _vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
2473 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
2476 if (!(mem
= (MSVCRT_wchar_t
*)MSVCRT_malloc(resize
*sizeof(*mem
))))
2479 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2485 /*********************************************************************
2486 * vprintf (MSVCRT.@)
2488 int MSVCRT_vprintf(const char *format
, va_list valist
)
2490 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
2493 /*********************************************************************
2494 * vwprintf (MSVCRT.@)
2496 int MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, va_list valist
)
2498 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
2501 /*********************************************************************
2502 * fprintf (MSVCRT.@)
2504 int MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
2508 va_start(valist
, format
);
2509 res
= MSVCRT_vfprintf(file
, format
, valist
);
2514 /*********************************************************************
2515 * fwprintf (MSVCRT.@)
2517 int MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
2521 va_start(valist
, format
);
2522 res
= MSVCRT_vfwprintf(file
, format
, valist
);
2527 /*********************************************************************
2530 int MSVCRT_printf(const char *format
, ...)
2534 va_start(valist
, format
);
2535 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
2540 /*********************************************************************
2543 int MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
2545 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2546 msvcrt_alloc_buffer(file
);
2549 if(file
->_ptr
>file
->_base
) {
2558 /*********************************************************************
2559 * ungetwc (MSVCRT.@)
2561 MSVCRT_wint_t
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2563 MSVCRT_wchar_t mwc
= wc
;
2564 char * pp
= (char *)&mwc
;
2566 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
2567 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
2573 /*********************************************************************
2574 * wprintf (MSVCRT.@)
2576 int MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
2580 va_start(valist
, format
);
2581 res
= MSVCRT_vwprintf(format
, valist
);