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
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
38 #include <sys/types.h>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
65 #define WX_READCR 0x08 /* underlying file is at \r */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
70 /* FIXME: this should be allocated dynamically */
71 #define MSVCRT_MAX_FILES 2048
72 #define MSVCRT_FD_BLOCK_SIZE 32
74 /* ioinfo structure size is different in msvcrXX.dll's */
80 CRITICAL_SECTION crit
;
83 /*********************************************************************
84 * __pioinfo (MSVCRT.@)
85 * array of pointers to ioinfo arrays [32]
87 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
89 /*********************************************************************
90 * __badioinfo (MSVCRT.@)
92 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
94 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
95 static int MSVCRT_fdend
= 3; /* highest allocated fd */
99 CRITICAL_SECTION crit
;
102 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
103 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
104 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
106 /* INTERNAL: process umask */
107 static int MSVCRT_umask
= 0;
109 /* INTERNAL: static data for tmpnam and _wtmpname functions */
110 static int tmpnam_unique
;
112 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
113 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
114 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
115 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
117 #define TOUL(x) (ULONGLONG)(x)
118 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
119 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
120 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
121 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
123 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
124 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
125 * and MSVCRT_stream_idx, from race conditions.
126 * It doesn't protect against race conditions manipulating the underlying files
127 * or flags; doing so would probably be better accomplished with per-file
128 * protection, rather than locking the whole table for every change.
130 static CRITICAL_SECTION MSVCRT_file_cs
;
131 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
133 0, 0, &MSVCRT_file_cs
,
134 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
135 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
137 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
138 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
139 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
141 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
143 buf
->st_dev
= buf64
->st_dev
;
144 buf
->st_ino
= buf64
->st_ino
;
145 buf
->st_mode
= buf64
->st_mode
;
146 buf
->st_nlink
= buf64
->st_nlink
;
147 buf
->st_uid
= buf64
->st_uid
;
148 buf
->st_gid
= buf64
->st_gid
;
149 buf
->st_rdev
= buf64
->st_rdev
;
150 buf
->st_size
= buf64
->st_size
;
151 buf
->st_atime
= buf64
->st_atime
;
152 buf
->st_mtime
= buf64
->st_mtime
;
153 buf
->st_ctime
= buf64
->st_ctime
;
156 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
158 buf
->st_dev
= buf64
->st_dev
;
159 buf
->st_ino
= buf64
->st_ino
;
160 buf
->st_mode
= buf64
->st_mode
;
161 buf
->st_nlink
= buf64
->st_nlink
;
162 buf
->st_uid
= buf64
->st_uid
;
163 buf
->st_gid
= buf64
->st_gid
;
164 buf
->st_rdev
= buf64
->st_rdev
;
165 buf
->st_size
= buf64
->st_size
;
166 buf
->st_atime
= buf64
->st_atime
;
167 buf
->st_mtime
= buf64
->st_mtime
;
168 buf
->st_ctime
= buf64
->st_ctime
;
171 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
173 /* 1601 to 1970 is 369 years plus 89 leap days */
174 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
176 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
177 ft
->dwHighDateTime
= ticks
>> 32;
178 ft
->dwLowDateTime
= ticks
;
181 static inline ioinfo
* msvcrt_get_ioinfo(int fd
)
184 if(fd
< MSVCRT_MAX_FILES
)
185 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
187 return &MSVCRT___badioinfo
;
189 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
192 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
196 if(i
>= MSVCRT_max_streams
)
200 return &MSVCRT__iob
[i
];
202 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
204 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
205 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
206 ERR("out of memory\n");
207 *MSVCRT__errno() = MSVCRT_ENOMEM
;
211 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
213 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
218 static inline BOOL
msvcrt_is_valid_fd(int fd
)
220 return fd
>= 0 && fd
< MSVCRT_fdend
&& (msvcrt_get_ioinfo(fd
)->wxflag
& WX_OPEN
);
223 /* INTERNAL: Get the HANDLE for a fd
224 * This doesn't lock the table, because a failure will result in
225 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
226 * it returns a valid handle which is about to be closed, a subsequent call
227 * will fail, most likely in a sane way.
229 static HANDLE
msvcrt_fdtoh(int fd
)
231 if (!msvcrt_is_valid_fd(fd
))
233 WARN(":fd (%d) - no handle!\n",fd
);
234 *MSVCRT___doserrno() = 0;
235 *MSVCRT__errno() = MSVCRT_EBADF
;
236 return INVALID_HANDLE_VALUE
;
238 if (msvcrt_get_ioinfo(fd
)->handle
== INVALID_HANDLE_VALUE
)
239 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd
);
240 return msvcrt_get_ioinfo(fd
)->handle
;
243 /* INTERNAL: free a file entry fd */
244 static void msvcrt_free_fd(int fd
)
250 fdinfo
= msvcrt_get_ioinfo(fd
);
251 old_handle
= fdinfo
->handle
;
252 if(fdinfo
!= &MSVCRT___badioinfo
)
254 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
257 TRACE(":fd (%d) freed\n",fd
);
258 if (fd
< 3) /* don't use 0,1,2 for user files */
263 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
266 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
269 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
275 if (fd
== MSVCRT_fdend
- 1)
277 if (fd
< MSVCRT_fdstart
)
283 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
284 /* caller must hold the files lock */
285 static int msvcrt_alloc_fd_from(HANDLE hand
, int flag
, int fd
)
289 if (fd
>= MSVCRT_MAX_FILES
)
291 WARN(":files exhausted!\n");
292 *MSVCRT__errno() = MSVCRT_ENFILE
;
296 fdinfo
= msvcrt_get_ioinfo(fd
);
297 if(fdinfo
== &MSVCRT___badioinfo
) {
300 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
301 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
302 WARN(":out of memory!\n");
303 *MSVCRT__errno() = MSVCRT_ENOMEM
;
307 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
308 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
310 fdinfo
= msvcrt_get_ioinfo(fd
);
313 fdinfo
->handle
= hand
;
314 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
316 /* locate next free slot */
317 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
318 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
320 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
321 msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
323 /* update last fd in use */
324 if (fd
>= MSVCRT_fdend
)
325 MSVCRT_fdend
= fd
+ 1;
326 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
330 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
331 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
332 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
338 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
339 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
344 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
345 ret
= msvcrt_alloc_fd_from(hand
, flag
, MSVCRT_fdstart
);
350 /* INTERNAL: Allocate a FILE* for an fd slot */
351 /* caller must hold the files lock */
352 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
357 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
359 file
= msvcrt_get_file(i
);
363 if (file
->_flag
== 0)
365 if (i
== MSVCRT_stream_idx
)
367 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
369 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
370 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
381 /* INTERNAL: initialize a FILE* from an open fd */
382 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
384 TRACE(":fd (%d) allocating FILE*\n",fd
);
385 if (!msvcrt_is_valid_fd(fd
))
387 WARN(":invalid fd %d\n",fd
);
388 *MSVCRT___doserrno() = 0;
389 *MSVCRT__errno() = MSVCRT_EBADF
;
392 memset(file
, 0, sizeof(*file
));
394 file
->_flag
= stream_flags
;
396 TRACE(":got FILE* (%p)\n",file
);
400 /* INTERNAL: Create an inheritance data block (for spawned process)
401 * The inheritance block is made of:
402 * 00 int nb of file descriptor (NBFD)
403 * 04 char file flags (wxflag): repeated for each fd
404 * 4+NBFD HANDLE file handle: repeated for each fd
406 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
413 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
414 *block
= MSVCRT_calloc(*size
, 1);
420 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
421 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
423 *(unsigned*)*block
= MSVCRT_fdend
;
424 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
426 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
427 fdinfo
= msvcrt_get_ioinfo(fd
);
428 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
430 *wxflag_ptr
= fdinfo
->wxflag
;
431 *handle_ptr
= fdinfo
->handle
;
436 *handle_ptr
= INVALID_HANDLE_VALUE
;
438 wxflag_ptr
++; handle_ptr
++;
443 /* INTERNAL: Set up all file descriptors,
444 * as well as default streams (stdin, stderr and stdout)
446 void msvcrt_init_io(void)
452 GetStartupInfoA(&si
);
453 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
459 count
= *(unsigned*)si
.lpReserved2
;
460 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
461 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
463 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
464 count
= min(count
, MSVCRT_MAX_FILES
);
465 for (i
= 0; i
< count
; i
++)
467 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
468 msvcrt_alloc_fd_from(*handle_ptr
, *wxflag_ptr
, i
);
470 wxflag_ptr
++; handle_ptr
++;
472 MSVCRT_fdend
= max( 3, count
);
473 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
474 if (msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
477 if(!MSVCRT___pioinfo
[0])
478 msvcrt_alloc_fd_from(INVALID_HANDLE_VALUE
, 0, 3);
480 fdinfo
= msvcrt_get_ioinfo(0);
481 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
483 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
484 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
485 GetCurrentProcess(), &fdinfo
->handle
,
486 0, TRUE
, DUPLICATE_SAME_ACCESS
))
487 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
490 fdinfo
= msvcrt_get_ioinfo(1);
491 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
493 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
494 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
495 GetCurrentProcess(), &fdinfo
->handle
,
496 0, TRUE
, DUPLICATE_SAME_ACCESS
))
497 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
500 fdinfo
= msvcrt_get_ioinfo(2);
501 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
503 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
504 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
505 GetCurrentProcess(), &fdinfo
->handle
,
506 0, TRUE
, DUPLICATE_SAME_ACCESS
))
507 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
510 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(0)->handle
,
511 msvcrt_get_ioinfo(1)->handle
, msvcrt_get_ioinfo(2)->handle
);
513 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
514 for (i
= 0; i
< 3; i
++)
516 /* FILE structs for stdin/out/err are static and never deleted */
517 MSVCRT__iob
[i
]._file
= i
;
518 MSVCRT__iob
[i
]._tmpfname
= NULL
;
519 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
521 MSVCRT_stream_idx
= 3;
524 /* INTERNAL: Flush stdio file buffer */
525 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
528 int cnt
=file
->_ptr
-file
->_base
;
529 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
530 file
->_flag
|= MSVCRT__IOERR
;
533 file
->_ptr
=file
->_base
;
534 file
->_cnt
=file
->_bufsiz
;
539 /* INTERNAL: Allocate stdio file buffer */
540 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
542 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
544 file
->_bufsiz
= MSVCRT_BUFSIZ
;
545 file
->_flag
|= MSVCRT__IOMYBUF
;
547 file
->_base
= (char*)(&file
->_charbuf
);
549 file
->_bufsiz
= sizeof(file
->_charbuf
);
551 file
->_ptr
= file
->_base
;
555 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
556 static int msvcrt_int_to_base32(int num
, char *str
)
571 *p
= (num
& 31) + '0';
573 *p
+= ('a' - '0' - 10);
580 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
581 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
596 *p
= (num
& 31) + '0';
598 *p
+= ('a' - '0' - 10);
605 /*********************************************************************
606 * __iob_func(MSVCRT.@)
608 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
610 return &MSVCRT__iob
[0];
613 /*********************************************************************
616 int CDECL
MSVCRT__access(const char *filename
, int mode
)
618 DWORD attr
= GetFileAttributesA(filename
);
620 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
622 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
624 msvcrt_set_errno(GetLastError());
627 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
629 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
635 /*********************************************************************
636 * _access_s (MSVCRT.@)
638 int CDECL
_access_s(const char *filename
, int mode
)
640 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return -1;
641 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return -1;
643 return MSVCRT__access(filename
, mode
);
646 /*********************************************************************
647 * _waccess (MSVCRT.@)
649 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
651 DWORD attr
= GetFileAttributesW(filename
);
653 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
655 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
657 msvcrt_set_errno(GetLastError());
660 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
662 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
668 /*********************************************************************
669 * _waccess_s (MSVCRT.@)
671 int CDECL
_waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
673 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return -1;
674 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return -1;
676 return MSVCRT__waccess(filename
, mode
);
679 /*********************************************************************
682 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
684 DWORD oldFlags
= GetFileAttributesA(path
);
686 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
688 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
689 oldFlags
| FILE_ATTRIBUTE_READONLY
;
691 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
694 msvcrt_set_errno(GetLastError());
698 /*********************************************************************
701 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
703 DWORD oldFlags
= GetFileAttributesW(path
);
705 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
707 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
708 oldFlags
| FILE_ATTRIBUTE_READONLY
;
710 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
713 msvcrt_set_errno(GetLastError());
717 /*********************************************************************
720 int CDECL
MSVCRT__unlink(const char *path
)
722 TRACE("%s\n",debugstr_a(path
));
723 if(DeleteFileA(path
))
725 TRACE("failed (%d)\n",GetLastError());
726 msvcrt_set_errno(GetLastError());
730 /*********************************************************************
731 * _wunlink (MSVCRT.@)
733 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
735 TRACE("(%s)\n",debugstr_w(path
));
736 if(DeleteFileW(path
))
738 TRACE("failed (%d)\n",GetLastError());
739 msvcrt_set_errno(GetLastError());
743 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
744 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
746 /* INTERNAL: Flush all stream buffer */
747 static int msvcrt_flush_all_buffers(int mask
)
749 int i
, num_flushed
= 0;
753 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
754 file
= msvcrt_get_file(i
);
758 if(file
->_flag
& mask
) {
766 TRACE(":flushed (%d) handles\n",num_flushed
);
770 /*********************************************************************
771 * _flushall (MSVCRT.@)
773 int CDECL
MSVCRT__flushall(void)
775 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
778 /*********************************************************************
781 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
784 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
785 } else if(file
->_flag
& MSVCRT__IOWRT
) {
788 MSVCRT__lock_file(file
);
789 res
= msvcrt_flush_buffer(file
);
790 MSVCRT__unlock_file(file
);
793 } else if(file
->_flag
& MSVCRT__IOREAD
) {
794 MSVCRT__lock_file(file
);
796 file
->_ptr
= file
->_base
;
797 MSVCRT__unlock_file(file
);
804 /*********************************************************************
807 int CDECL
MSVCRT__close(int fd
)
813 hand
= msvcrt_fdtoh(fd
);
814 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
815 if (!msvcrt_is_valid_fd(fd
)) {
819 ret
= CloseHandle(hand
) ? 0 : -1;
821 WARN(":failed-last error (%d)\n",GetLastError());
822 msvcrt_set_errno(GetLastError());
830 /*********************************************************************
833 int CDECL
MSVCRT__commit(int fd
)
835 HANDLE hand
= msvcrt_fdtoh(fd
);
837 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
838 if (hand
== INVALID_HANDLE_VALUE
)
841 if (!FlushFileBuffers(hand
))
843 if (GetLastError() == ERROR_INVALID_HANDLE
)
845 /* FlushFileBuffers fails for console handles
846 * so we ignore this error.
850 TRACE(":failed-last error (%d)\n",GetLastError());
851 msvcrt_set_errno(GetLastError());
858 /*********************************************************************
861 * MSDN isn't clear on this point, but the remarks for _pipe
862 * indicate file descriptors duplicated with _dup and _dup2 are always
865 int CDECL
MSVCRT__dup2(int od
, int nd
)
869 TRACE("(od=%d, nd=%d)\n", od
, nd
);
871 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
875 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od
)->handle
,
876 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
878 int wxflag
= msvcrt_get_ioinfo(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
880 if (msvcrt_is_valid_fd(nd
))
882 ret
= msvcrt_alloc_fd_from(handle
, wxflag
, nd
);
886 *MSVCRT__errno() = MSVCRT_EMFILE
;
890 /* _dup2 returns 0, not nd, on success */
897 msvcrt_set_errno(GetLastError());
902 *MSVCRT__errno() = MSVCRT_EBADF
;
909 /*********************************************************************
912 int CDECL
MSVCRT__dup(int od
)
918 if (MSVCRT__dup2(od
, fd
) == 0)
926 /*********************************************************************
929 int CDECL
MSVCRT__eof(int fd
)
932 LONG hcurpos
,hendpos
;
933 HANDLE hand
= msvcrt_fdtoh(fd
);
935 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
937 if (hand
== INVALID_HANDLE_VALUE
)
940 if (msvcrt_get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
942 /* Otherwise we do it the hard way */
943 hcurpos
= hendpos
= 0;
944 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
945 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
947 if (curpos
== endpos
&& hcurpos
== hendpos
)
949 /* FIXME: shouldn't WX_ATEOF be set here? */
953 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
957 /*********************************************************************
958 * _fcloseall (MSVCRT.@)
960 int CDECL
MSVCRT__fcloseall(void)
962 int num_closed
= 0, i
;
966 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
967 file
= msvcrt_get_file(i
);
969 if (file
->_flag
&& !MSVCRT_fclose(file
))
974 TRACE(":closed (%d) handles\n",num_closed
);
978 /* free everything on process exit */
979 void msvcrt_free_io(void)
984 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
985 * stdout, and stderr (unlike GNU), so we need to fclose() them here
986 * or they won't get flushed.
988 MSVCRT_fclose(&MSVCRT__iob
[0]);
989 MSVCRT_fclose(&MSVCRT__iob
[1]);
990 MSVCRT_fclose(&MSVCRT__iob
[2]);
992 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
993 MSVCRT_free(MSVCRT___pioinfo
[i
]);
995 for(i
=0; i
<MSVCRT_stream_idx
; i
++)
997 MSVCRT_FILE
*file
= msvcrt_get_file(i
);
998 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1000 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1001 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1005 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1006 MSVCRT_free(MSVCRT_fstream
[i
]);
1008 DeleteCriticalSection(&MSVCRT_file_cs
);
1011 /*********************************************************************
1012 * _lseeki64 (MSVCRT.@)
1014 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1016 HANDLE hand
= msvcrt_fdtoh(fd
);
1019 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1020 if (hand
== INVALID_HANDLE_VALUE
)
1023 if (whence
< 0 || whence
> 2)
1025 *MSVCRT__errno() = MSVCRT_EINVAL
;
1029 TRACE(":fd (%d) to %s pos %s\n",
1030 fd
,wine_dbgstr_longlong(offset
),
1031 (whence
==SEEK_SET
)?"SEEK_SET":
1032 (whence
==SEEK_CUR
)?"SEEK_CUR":
1033 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1035 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1036 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1037 ofs
.QuadPart
= offset
;
1038 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1039 GetLastError() == ERROR_SUCCESS
)
1041 msvcrt_get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1042 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1044 return ofs
.QuadPart
;
1046 TRACE(":error-last error (%d)\n",GetLastError());
1047 msvcrt_set_errno(GetLastError());
1051 /*********************************************************************
1054 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1056 return MSVCRT__lseeki64(fd
, offset
, whence
);
1059 /*********************************************************************
1060 * _lock_file (MSVCRT.@)
1062 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1064 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1065 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1067 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1070 /*********************************************************************
1071 * _unlock_file (MSVCRT.@)
1073 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1075 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1076 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1078 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1081 /*********************************************************************
1082 * _locking (MSVCRT.@)
1084 * This is untested; the underlying LockFile doesn't work yet.
1086 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1090 HANDLE hand
= msvcrt_fdtoh(fd
);
1092 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1093 if (hand
== INVALID_HANDLE_VALUE
)
1096 if (mode
< 0 || mode
> 4)
1098 *MSVCRT__errno() = MSVCRT_EINVAL
;
1102 TRACE(":fd (%d) by 0x%08x mode %s\n",
1103 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1104 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1105 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1106 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1107 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1110 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1112 FIXME ("Seek failed\n");
1113 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1116 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1119 ret
= 1; /* just to satisfy gcc */
1122 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1127 else if (mode
== MSVCRT__LK_UNLCK
)
1128 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1130 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1131 /* FIXME - what about error settings? */
1132 return ret
? 0 : -1;
1135 /*********************************************************************
1136 * _fseeki64 (MSVCRT.@)
1138 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1142 MSVCRT__lock_file(file
);
1143 /* Flush output if needed */
1144 if(file
->_flag
& MSVCRT__IOWRT
)
1145 msvcrt_flush_buffer(file
);
1147 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1148 offset
-= file
->_cnt
;
1149 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1150 /* Black magic correction for CR removal */
1152 for (i
=0; i
<file
->_cnt
; i
++) {
1153 if (file
->_ptr
[i
] == '\n')
1156 /* Black magic when reading CR at buffer boundary*/
1157 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1161 /* Discard buffered input */
1163 file
->_ptr
= file
->_base
;
1164 /* Reset direction of i/o */
1165 if(file
->_flag
& MSVCRT__IORW
) {
1166 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1168 /* Clear end of file flag */
1169 file
->_flag
&= ~MSVCRT__IOEOF
;
1170 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1172 MSVCRT__unlock_file(file
);
1176 /*********************************************************************
1179 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1181 return MSVCRT__fseeki64( file
, offset
, whence
);
1184 /*********************************************************************
1185 * _chsize (MSVCRT.@)
1187 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1193 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1197 handle
= msvcrt_fdtoh(fd
);
1198 if (handle
!= INVALID_HANDLE_VALUE
)
1200 /* save the current file pointer */
1201 cur
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1204 pos
= MSVCRT__lseek(fd
, size
, SEEK_SET
);
1207 ret
= SetEndOfFile(handle
);
1208 if (!ret
) msvcrt_set_errno(GetLastError());
1211 /* restore the file pointer */
1212 MSVCRT__lseek(fd
, cur
, SEEK_SET
);
1217 return ret
? 0 : -1;
1220 /*********************************************************************
1221 * clearerr (MSVCRT.@)
1223 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1225 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1227 MSVCRT__lock_file(file
);
1228 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1229 MSVCRT__unlock_file(file
);
1232 /*********************************************************************
1235 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1237 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1239 MSVCRT__lock_file(file
);
1240 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1241 MSVCRT_clearerr(file
);
1242 MSVCRT__unlock_file(file
);
1245 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1247 int plus
= strchrW(mode
, '+') != NULL
;
1252 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1253 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1256 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1257 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1260 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1261 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1264 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1265 *MSVCRT__errno() = MSVCRT_EINVAL
;
1273 *open_flags
|= MSVCRT__O_BINARY
;
1274 *open_flags
&= ~MSVCRT__O_TEXT
;
1277 *open_flags
|= MSVCRT__O_TEXT
;
1278 *open_flags
&= ~MSVCRT__O_BINARY
;
1281 *open_flags
|= MSVCRT__O_TEMPORARY
;
1284 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1290 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1295 /*********************************************************************
1296 * _fdopen (MSVCRT.@)
1298 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1301 MSVCRT_wchar_t
*modeW
= NULL
;
1303 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1305 ret
= MSVCRT__wfdopen(fd
, modeW
);
1311 /*********************************************************************
1312 * _wfdopen (MSVCRT.@)
1314 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1316 int open_flags
, stream_flags
;
1319 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1322 if (!(file
= msvcrt_alloc_fp()))
1324 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1329 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1335 /*********************************************************************
1336 * _filelength (MSVCRT.@)
1338 LONG CDECL
MSVCRT__filelength(int fd
)
1340 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1343 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1346 if (endPos
!= curPos
)
1347 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1354 /*********************************************************************
1355 * _filelengthi64 (MSVCRT.@)
1357 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1359 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1362 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1365 if (endPos
!= curPos
)
1366 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1373 /*********************************************************************
1374 * _fileno (MSVCRT.@)
1376 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1378 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1382 /*********************************************************************
1383 * _fstat64 (MSVCRT.@)
1385 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1389 BY_HANDLE_FILE_INFORMATION hfi
;
1390 HANDLE hand
= msvcrt_fdtoh(fd
);
1392 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1393 if (hand
== INVALID_HANDLE_VALUE
)
1398 WARN(":failed-NULL buf\n");
1399 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1403 memset(&hfi
, 0, sizeof(hfi
));
1404 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1405 type
= GetFileType(hand
);
1406 if (type
== FILE_TYPE_PIPE
)
1408 buf
->st_dev
= buf
->st_rdev
= fd
;
1409 buf
->st_mode
= S_IFIFO
;
1412 else if (type
== FILE_TYPE_CHAR
)
1414 buf
->st_dev
= buf
->st_rdev
= fd
;
1415 buf
->st_mode
= S_IFCHR
;
1418 else /* FILE_TYPE_DISK etc. */
1420 if (!GetFileInformationByHandle(hand
, &hfi
))
1422 WARN(":failed-last error (%d)\n",GetLastError());
1423 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1426 buf
->st_mode
= S_IFREG
| 0444;
1427 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1428 buf
->st_mode
|= 0222;
1429 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1430 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1432 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1433 buf
->st_mtime
= buf
->st_ctime
= dw
;
1434 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1436 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1441 /*********************************************************************
1442 * _fstati64 (MSVCRT.@)
1444 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1447 struct MSVCRT__stat64 buf64
;
1449 ret
= MSVCRT__fstat64(fd
, &buf64
);
1451 msvcrt_stat64_to_stati64(&buf64
, buf
);
1455 /*********************************************************************
1458 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1460 struct MSVCRT__stat64 buf64
;
1462 ret
= MSVCRT__fstat64(fd
, &buf64
);
1464 msvcrt_stat64_to_stat(&buf64
, buf
);
1468 /*********************************************************************
1469 * _futime64 (MSVCRT.@)
1471 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1473 HANDLE hand
= msvcrt_fdtoh(fd
);
1478 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1483 time_to_filetime( t
->actime
, &at
);
1484 time_to_filetime( t
->modtime
, &wt
);
1487 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1489 msvcrt_set_errno(GetLastError());
1495 /*********************************************************************
1496 * _futime32 (MSVCRT.@)
1498 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1502 struct MSVCRT___utimbuf64 t64
;
1503 t64
.actime
= t
->actime
;
1504 t64
.modtime
= t
->modtime
;
1505 return _futime64( fd
, &t64
);
1508 return _futime64( fd
, NULL
);
1511 /*********************************************************************
1512 * _futime (MSVCRT.@)
1515 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1517 return _futime64( fd
, t
);
1520 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1522 return _futime32( fd
, t
);
1526 /*********************************************************************
1527 * _get_osfhandle (MSVCRT.@)
1529 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1531 HANDLE hand
= msvcrt_fdtoh(fd
);
1532 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1534 return (MSVCRT_intptr_t
)hand
;
1537 /*********************************************************************
1538 * _isatty (MSVCRT.@)
1540 int CDECL
MSVCRT__isatty(int fd
)
1542 HANDLE hand
= msvcrt_fdtoh(fd
);
1544 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1545 if (hand
== INVALID_HANDLE_VALUE
)
1548 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1551 /*********************************************************************
1552 * _mktemp (MSVCRT.@)
1554 char * CDECL
MSVCRT__mktemp(char *pattern
)
1557 char *retVal
= pattern
;
1562 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1566 id
= GetCurrentProcessId();
1570 int tempNum
= id
/ 10;
1571 *pattern
-- = id
- (tempNum
* 10) + '0';
1577 *pattern
= letter
++;
1578 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1579 GetLastError() == ERROR_FILE_NOT_FOUND
)
1581 } while(letter
<= 'z');
1585 /*********************************************************************
1586 * _wmktemp (MSVCRT.@)
1588 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1591 MSVCRT_wchar_t
*retVal
= pattern
;
1593 MSVCRT_wchar_t letter
= 'a';
1596 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1600 id
= GetCurrentProcessId();
1604 int tempNum
= id
/ 10;
1605 *pattern
-- = id
- (tempNum
* 10) + '0';
1611 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1612 GetLastError() == ERROR_FILE_NOT_FOUND
)
1614 *pattern
= letter
++;
1615 } while(letter
!= '|');
1619 static unsigned split_oflags(unsigned oflags
)
1622 unsigned unsupp
; /* until we support everything */
1624 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1625 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1626 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1627 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1628 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1629 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1631 if ((unsupp
= oflags
& ~(
1632 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1633 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1634 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1635 MSVCRT__O_NOINHERIT
|
1636 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
1638 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1643 /*********************************************************************
1646 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1649 SECURITY_ATTRIBUTES sa
;
1650 HANDLE readHandle
, writeHandle
;
1654 *MSVCRT__errno() = MSVCRT_EINVAL
;
1658 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1659 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1660 sa
.lpSecurityDescriptor
= NULL
;
1661 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1663 unsigned int wxflags
= split_oflags(textmode
);
1667 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1671 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1679 MSVCRT__close(pfds
[0]);
1680 CloseHandle(writeHandle
);
1681 *MSVCRT__errno() = MSVCRT_EMFILE
;
1686 CloseHandle(readHandle
);
1687 CloseHandle(writeHandle
);
1688 *MSVCRT__errno() = MSVCRT_EMFILE
;
1693 msvcrt_set_errno(GetLastError());
1698 /*********************************************************************
1699 * _sopen_s (MSVCRT.@)
1701 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1703 DWORD access
= 0, creation
= 0, attrib
;
1707 SECURITY_ATTRIBUTES sa
;
1709 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1710 fd
, path
, oflags
, shflags
, pmode
);
1712 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
1715 wxflag
= split_oflags(oflags
);
1716 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1718 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1719 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1720 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1723 if (oflags
& MSVCRT__O_CREAT
)
1725 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1726 FIXME(": pmode 0x%04x ignored\n", pmode
);
1728 WARN(": pmode 0x%04x ignored\n", pmode
);
1730 if (oflags
& MSVCRT__O_EXCL
)
1731 creation
= CREATE_NEW
;
1732 else if (oflags
& MSVCRT__O_TRUNC
)
1733 creation
= CREATE_ALWAYS
;
1735 creation
= OPEN_ALWAYS
;
1737 else /* no MSVCRT__O_CREAT */
1739 if (oflags
& MSVCRT__O_TRUNC
)
1740 creation
= TRUNCATE_EXISTING
;
1742 creation
= OPEN_EXISTING
;
1747 case MSVCRT__SH_DENYRW
:
1750 case MSVCRT__SH_DENYWR
:
1751 sharing
= FILE_SHARE_READ
;
1753 case MSVCRT__SH_DENYRD
:
1754 sharing
= FILE_SHARE_WRITE
;
1756 case MSVCRT__SH_DENYNO
:
1757 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1760 ERR( "Unhandled shflags 0x%x\n", shflags
);
1761 return MSVCRT_EINVAL
;
1763 attrib
= FILE_ATTRIBUTE_NORMAL
;
1765 if (oflags
& MSVCRT__O_TEMPORARY
)
1767 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1769 sharing
|= FILE_SHARE_DELETE
;
1772 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1773 sa
.lpSecurityDescriptor
= NULL
;
1774 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1776 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1777 if (hand
== INVALID_HANDLE_VALUE
) {
1778 WARN(":failed-last error (%d)\n", GetLastError());
1779 msvcrt_set_errno(GetLastError());
1780 return *MSVCRT__errno();
1783 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1785 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1789 /*********************************************************************
1792 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1797 if (oflags
& MSVCRT__O_CREAT
)
1801 __ms_va_start(ap
, shflags
);
1802 pmode
= va_arg(ap
, int);
1808 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1812 /*********************************************************************
1813 * _wsopen_s (MSVCRT.@)
1815 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
1817 DWORD access
= 0, creation
= 0, attrib
;
1818 SECURITY_ATTRIBUTES sa
;
1823 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1824 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1826 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
1829 wxflag
= split_oflags(oflags
);
1830 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1832 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1833 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1834 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1837 if (oflags
& MSVCRT__O_CREAT
)
1839 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1840 FIXME(": pmode 0x%04x ignored\n", pmode
);
1842 WARN(": pmode 0x%04x ignored\n", pmode
);
1844 if (oflags
& MSVCRT__O_EXCL
)
1845 creation
= CREATE_NEW
;
1846 else if (oflags
& MSVCRT__O_TRUNC
)
1847 creation
= CREATE_ALWAYS
;
1849 creation
= OPEN_ALWAYS
;
1851 else /* no MSVCRT__O_CREAT */
1853 if (oflags
& MSVCRT__O_TRUNC
)
1854 creation
= TRUNCATE_EXISTING
;
1856 creation
= OPEN_EXISTING
;
1861 case MSVCRT__SH_DENYRW
:
1864 case MSVCRT__SH_DENYWR
:
1865 sharing
= FILE_SHARE_READ
;
1867 case MSVCRT__SH_DENYRD
:
1868 sharing
= FILE_SHARE_WRITE
;
1870 case MSVCRT__SH_DENYNO
:
1871 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1874 ERR( "Unhandled shflags 0x%x\n", shflags
);
1875 return MSVCRT_EINVAL
;
1877 attrib
= FILE_ATTRIBUTE_NORMAL
;
1879 if (oflags
& MSVCRT__O_TEMPORARY
)
1881 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1883 sharing
|= FILE_SHARE_DELETE
;
1886 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1887 sa
.lpSecurityDescriptor
= NULL
;
1888 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1890 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1892 if (hand
== INVALID_HANDLE_VALUE
) {
1893 WARN(":failed-last error (%d)\n",GetLastError());
1894 msvcrt_set_errno(GetLastError());
1895 msvcrt_set_errno(GetLastError());
1896 return *MSVCRT__errno();
1899 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1901 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1905 /*********************************************************************
1906 * _wsopen (MSVCRT.@)
1908 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
1913 if (oflags
& MSVCRT__O_CREAT
)
1917 __ms_va_start(ap
, shflags
);
1918 pmode
= va_arg(ap
, int);
1924 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1928 /*********************************************************************
1931 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
1935 if (flags
& MSVCRT__O_CREAT
)
1938 __ms_va_start(ap
, flags
);
1939 pmode
= va_arg(ap
, int);
1941 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1944 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
1947 /*********************************************************************
1950 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1954 if (flags
& MSVCRT__O_CREAT
)
1957 __ms_va_start(ap
, flags
);
1958 pmode
= va_arg(ap
, int);
1960 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1963 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
1966 /*********************************************************************
1969 int CDECL
MSVCRT__creat(const char *path
, int flags
)
1971 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1972 return MSVCRT__open(path
, usedFlags
);
1975 /*********************************************************************
1976 * _wcreat (MSVCRT.@)
1978 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1980 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1981 return MSVCRT__wopen(path
, usedFlags
);
1984 /*********************************************************************
1985 * _open_osfhandle (MSVCRT.@)
1987 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
1991 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1992 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1993 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1994 * text - it never sets MSVCRT__O_BINARY.
1996 /* don't let split_oflags() decide the mode if no mode is passed */
1997 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
1998 oflags
|= MSVCRT__O_BINARY
;
2000 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
2001 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
2005 /*********************************************************************
2008 int CDECL
MSVCRT__rmtmp(void)
2010 int num_removed
= 0, i
;
2014 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2015 file
= msvcrt_get_file(i
);
2017 if (file
->_tmpfname
)
2019 MSVCRT_fclose(file
);
2026 TRACE(":removed (%d) temp files\n",num_removed
);
2030 /*********************************************************************
2033 * When reading \r as last character in text mode, read() positions
2034 * the file pointer on the \r character while getc() goes on to
2037 static int read_i(int fd
, void *buf
, unsigned int count
)
2040 char *bufstart
= buf
;
2041 HANDLE hand
= msvcrt_fdtoh(fd
);
2042 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2047 if (fdinfo
->wxflag
& WX_READEOF
) {
2048 fdinfo
->wxflag
|= WX_ATEOF
;
2049 TRACE("already at EOF, returning 0\n");
2052 /* Don't trace small reads, it gets *very* annoying */
2054 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2055 if (hand
== INVALID_HANDLE_VALUE
)
2058 /* Reading single bytes in O_TEXT mode makes things slow
2059 * So read big chunks
2061 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2063 if (count
!= 0 && num_read
== 0)
2065 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2066 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2068 else if (fdinfo
->wxflag
& WX_TEXT
)
2071 if (bufstart
[num_read
-1] == '\r')
2075 fdinfo
->wxflag
&= ~WX_READCR
;
2076 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
2080 fdinfo
->wxflag
|= WX_READCR
;
2085 fdinfo
->wxflag
&= ~WX_READCR
;
2086 for (i
=0, j
=0; i
<num_read
; i
++)
2088 /* in text mode, a ctrl-z signals EOF */
2089 if (bufstart
[i
] == 0x1a)
2091 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2092 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2095 /* in text mode, strip \r if followed by \n.
2096 * BUG: should save state across calls somehow, so CR LF that
2097 * straddles buffer boundary gets recognized properly?
2099 if ((bufstart
[i
] != '\r')
2100 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
2101 bufstart
[j
++] = bufstart
[i
];
2108 if (GetLastError() == ERROR_BROKEN_PIPE
)
2110 TRACE(":end-of-pipe\n");
2111 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2116 TRACE(":failed-last error (%d)\n",GetLastError());
2122 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2126 /*********************************************************************
2129 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2132 num_read
= read_i(fd
, buf
, count
);
2136 /*********************************************************************
2137 * _setmode (MSVCRT.@)
2139 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2141 int ret
= msvcrt_get_ioinfo(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2142 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
2143 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
2144 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
2145 msvcrt_get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2147 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2151 /*********************************************************************
2152 * _stat64 (MSVCRT.@)
2154 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2157 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2158 unsigned short mode
= ALL_S_IREAD
;
2161 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2163 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2165 TRACE("failed (%d)\n",GetLastError());
2166 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
2170 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2172 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2173 Bon 011120: This FIXME seems incorrect
2174 Also a letter as first char isn't enough to be classified
2177 if (isalpha(*path
)&& (*(path
+1)==':'))
2178 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2180 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2182 plen
= strlen(path
);
2184 /* Dir, or regular file? */
2185 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
2186 (path
[plen
-1] == '\\'))
2187 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2190 mode
|= MSVCRT__S_IFREG
;
2192 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2194 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2195 (tolower(path
[plen
-3]) << 16);
2196 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2197 mode
|= ALL_S_IEXEC
;
2201 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2202 mode
|= ALL_S_IWRITE
;
2204 buf
->st_mode
= mode
;
2206 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2207 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2209 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2210 buf
->st_mtime
= buf
->st_ctime
= dw
;
2211 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2212 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2213 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2217 /*********************************************************************
2218 * _stati64 (MSVCRT.@)
2220 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2223 struct MSVCRT__stat64 buf64
;
2225 ret
= MSVCRT_stat64(path
, &buf64
);
2227 msvcrt_stat64_to_stati64(&buf64
, buf
);
2231 /*********************************************************************
2234 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2236 struct MSVCRT__stat64 buf64
;
2238 ret
= MSVCRT_stat64( path
, &buf64
);
2240 msvcrt_stat64_to_stat(&buf64
, buf
);
2244 /*********************************************************************
2245 * _wstat64 (MSVCRT.@)
2247 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2250 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2251 unsigned short mode
= ALL_S_IREAD
;
2254 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2256 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2258 TRACE("failed (%d)\n",GetLastError());
2259 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
2263 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2265 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2266 if (MSVCRT_iswalpha(*path
))
2267 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2269 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2271 plen
= strlenW(path
);
2273 /* Dir, or regular file? */
2274 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
2275 (path
[plen
-1] == '\\'))
2276 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2279 mode
|= MSVCRT__S_IFREG
;
2281 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2283 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2284 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2285 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2286 mode
|= ALL_S_IEXEC
;
2290 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2291 mode
|= ALL_S_IWRITE
;
2293 buf
->st_mode
= mode
;
2295 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2296 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2298 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2299 buf
->st_mtime
= buf
->st_ctime
= dw
;
2300 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2301 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2302 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2306 /*********************************************************************
2307 * _wstati64 (MSVCRT.@)
2309 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2312 struct MSVCRT__stat64 buf64
;
2314 ret
= MSVCRT__wstat64(path
, &buf64
);
2316 msvcrt_stat64_to_stati64(&buf64
, buf
);
2320 /*********************************************************************
2323 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2326 struct MSVCRT__stat64 buf64
;
2328 ret
= MSVCRT__wstat64( path
, &buf64
);
2329 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2333 /*********************************************************************
2336 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
2338 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
2341 /*********************************************************************
2342 * _telli64 (MSVCRT.@)
2344 __int64 CDECL
_telli64(int fd
)
2346 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
2349 /*********************************************************************
2350 * _tempnam (MSVCRT.@)
2352 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
2354 char tmpbuf
[MAX_PATH
];
2355 const char *tmp_dir
= MSVCRT_getenv("TMP");
2357 if (tmp_dir
) dir
= tmp_dir
;
2359 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2360 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2362 TRACE("got name (%s)\n",tmpbuf
);
2363 DeleteFileA(tmpbuf
);
2364 return MSVCRT__strdup(tmpbuf
);
2366 TRACE("failed (%d)\n",GetLastError());
2370 /*********************************************************************
2371 * _wtempnam (MSVCRT.@)
2373 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
2375 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
2376 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
2377 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
2379 if (tmp_dir
) dir
= tmp_dir
;
2381 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2382 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2384 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2385 DeleteFileW(tmpbuf
);
2386 return MSVCRT__wcsdup(tmpbuf
);
2388 TRACE("failed (%d)\n",GetLastError());
2392 /*********************************************************************
2395 int CDECL
MSVCRT__umask(int umask
)
2397 int old_umask
= MSVCRT_umask
;
2398 TRACE("(%d)\n",umask
);
2399 MSVCRT_umask
= umask
;
2403 /*********************************************************************
2404 * _utime64 (MSVCRT.@)
2406 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
2408 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2412 int retVal
= _futime64(fd
, t
);
2419 /*********************************************************************
2420 * _utime32 (MSVCRT.@)
2422 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
2426 struct MSVCRT___utimbuf64 t64
;
2427 t64
.actime
= t
->actime
;
2428 t64
.modtime
= t
->modtime
;
2429 return _utime64( path
, &t64
);
2432 return _utime64( path
, NULL
);
2435 /*********************************************************************
2439 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
2441 return _utime64( path
, t
);
2444 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
2446 return _utime32( path
, t
);
2450 /*********************************************************************
2451 * _wutime64 (MSVCRT.@)
2453 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2455 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2459 int retVal
= _futime64(fd
, t
);
2466 /*********************************************************************
2467 * _wutime32 (MSVCRT.@)
2469 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2473 struct MSVCRT___utimbuf64 t64
;
2474 t64
.actime
= t
->actime
;
2475 t64
.modtime
= t
->modtime
;
2476 return _wutime64( path
, &t64
);
2479 return _wutime64( path
, NULL
);
2482 /*********************************************************************
2483 * _wutime (MSVCRT.@)
2486 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2488 return _wutime64( path
, t
);
2491 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2493 return _wutime32( path
, t
);
2497 /*********************************************************************
2500 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
2503 HANDLE hand
= msvcrt_fdtoh(fd
);
2505 /* Don't trace small writes, it gets *very* annoying */
2508 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2510 if (hand
== INVALID_HANDLE_VALUE
)
2512 *MSVCRT__errno() = MSVCRT_EBADF
;
2516 /* If appending, go to EOF */
2517 if (msvcrt_get_ioinfo(fd
)->wxflag
& WX_APPEND
)
2518 MSVCRT__lseek(fd
, 0, FILE_END
);
2520 if (!(msvcrt_get_ioinfo(fd
)->wxflag
& WX_TEXT
))
2522 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2523 && (num_written
== count
))
2525 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2526 hand
, GetLastError());
2527 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2531 unsigned int i
, j
, nr_lf
;
2534 const char *s
= buf
, *buf_start
= buf
;
2535 /* find number of \n ( without preceding \r ) */
2536 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2541 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2546 if ((q
= p
= MSVCRT_malloc(count
+ nr_lf
)))
2548 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2553 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2560 FIXME("Malloc failed\n");
2568 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2570 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2571 fd
, hand
, GetLastError(), num_written
);
2572 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2575 return s
- buf_start
;
2587 /*********************************************************************
2590 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
2594 MSVCRT__lock_file(file
);
2595 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
2596 if (len
== sizeof(val
)) {
2597 MSVCRT__unlock_file(file
);
2601 file
->_flag
|= MSVCRT__IOERR
;
2602 MSVCRT__unlock_file(file
);
2606 /*********************************************************************
2609 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
2613 MSVCRT__lock_file(file
);
2615 MSVCRT_free(file
->_tmpfname
);
2616 file
->_tmpfname
= NULL
;
2617 /* flush stdio buffers */
2618 if(file
->_flag
& MSVCRT__IOWRT
)
2619 MSVCRT_fflush(file
);
2620 if(file
->_flag
& MSVCRT__IOMYBUF
)
2621 MSVCRT_free(file
->_base
);
2623 r
=MSVCRT__close(file
->_file
);
2626 MSVCRT__unlock_file(file
);
2628 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
2631 /*********************************************************************
2634 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
2636 return file
->_flag
& MSVCRT__IOEOF
;
2639 /*********************************************************************
2642 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
2644 return file
->_flag
& MSVCRT__IOERR
;
2647 /*********************************************************************
2648 * _filbuf (MSVCRT.@)
2650 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
2653 MSVCRT__lock_file(file
);
2655 /* Allocate buffer if needed */
2656 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
))
2657 msvcrt_alloc_buffer(file
);
2659 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2660 if(file
->_flag
& MSVCRT__IORW
)
2661 file
->_flag
|= MSVCRT__IOREAD
;
2663 MSVCRT__unlock_file(file
);
2668 if(file
->_flag
& MSVCRT__IONBF
) {
2670 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2671 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2672 MSVCRT__unlock_file(file
);
2676 MSVCRT__unlock_file(file
);
2679 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2681 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2683 MSVCRT__unlock_file(file
);
2688 file
->_ptr
= file
->_base
+1;
2689 c
= *(unsigned char *)file
->_base
;
2690 MSVCRT__unlock_file(file
);
2695 /*********************************************************************
2698 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
2703 MSVCRT__lock_file(file
);
2706 i
= (unsigned char *)file
->_ptr
++;
2709 j
= MSVCRT__filbuf(file
);
2711 MSVCRT__unlock_file(file
);
2715 /*********************************************************************
2716 * _fgetchar (MSVCRT.@)
2718 int CDECL
MSVCRT__fgetchar(void)
2720 return MSVCRT_fgetc(MSVCRT_stdin
);
2723 /*********************************************************************
2726 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
2728 int cc
= MSVCRT_EOF
;
2729 char * buf_start
= s
;
2731 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2732 file
,file
->_file
,s
,size
);
2734 MSVCRT__lock_file(file
);
2736 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
2741 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2743 TRACE(":nothing read\n");
2744 MSVCRT__unlock_file(file
);
2747 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
2750 TRACE(":got %s\n", debugstr_a(buf_start
));
2751 MSVCRT__unlock_file(file
);
2755 /*********************************************************************
2758 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2759 * the CR from CR/LF combinations
2761 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
2765 MSVCRT__lock_file(file
);
2766 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2773 for(i
=0; i
<sizeof(wc
); i
++)
2783 j
= MSVCRT__filbuf(file
);
2786 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2789 MSVCRT__unlock_file(file
);
2796 MSVCRT__unlock_file(file
);
2800 c
= MSVCRT_fgetc(file
);
2801 if ((get_locinfo()->mb_cur_max
> 1) && MSVCRT_isleadbyte(c
))
2803 FIXME("Treat Multibyte characters\n");
2806 MSVCRT__unlock_file(file
);
2807 if (c
== MSVCRT_EOF
)
2810 return (MSVCRT_wint_t
)c
;
2813 /*********************************************************************
2816 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
2823 MSVCRT__lock_file(file
);
2824 for (j
=0; j
<sizeof(int); j
++) {
2825 k
= MSVCRT_fgetc(file
);
2826 if (k
== MSVCRT_EOF
) {
2827 file
->_flag
|= MSVCRT__IOEOF
;
2828 MSVCRT__unlock_file(file
);
2834 MSVCRT__unlock_file(file
);
2838 /*********************************************************************
2841 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
2843 return MSVCRT_fgetwc(file
);
2846 /*********************************************************************
2847 * _fgetwchar (MSVCRT.@)
2849 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
2851 return MSVCRT_fgetwc(MSVCRT_stdin
);
2854 /*********************************************************************
2855 * getwchar (MSVCRT.@)
2857 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
2859 return MSVCRT__fgetwchar();
2862 /*********************************************************************
2865 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
2867 int cc
= MSVCRT_WEOF
;
2868 MSVCRT_wchar_t
* buf_start
= s
;
2870 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2871 file
,file
->_file
,s
,size
);
2873 MSVCRT__lock_file(file
);
2875 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
2880 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2882 TRACE(":nothing read\n");
2883 MSVCRT__unlock_file(file
);
2886 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
2889 TRACE(":got %s\n", debugstr_w(buf_start
));
2890 MSVCRT__unlock_file(file
);
2894 /*********************************************************************
2897 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2899 MSVCRT_size_t wrcnt
=size
* nmemb
;
2904 MSVCRT__lock_file(file
);
2906 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2907 memcpy(file
->_ptr
, ptr
, pcnt
);
2912 ptr
= (const char*)ptr
+ pcnt
;
2913 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2914 if(file
->_flag
& MSVCRT__IORW
) {
2915 file
->_flag
|= MSVCRT__IOWRT
;
2917 MSVCRT__unlock_file(file
);
2923 int res
=msvcrt_flush_buffer(file
);
2925 int pwritten
= MSVCRT__write(file
->_file
, ptr
, wrcnt
);
2928 file
->_flag
|= MSVCRT__IOERR
;
2931 written
+= pwritten
;
2935 MSVCRT__unlock_file(file
);
2936 return written
/ size
;
2939 /*********************************************************************
2942 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2944 MSVCRT_wchar_t mwc
=wc
;
2945 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2950 /*********************************************************************
2951 * _fputwchar (MSVCRT.@)
2953 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
2955 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
2958 /*********************************************************************
2959 * _wfsopen (MSVCRT.@)
2961 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2964 int open_flags
, stream_flags
, fd
;
2966 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2968 /* map mode string to open() flags. "man fopen" for possibilities. */
2969 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2973 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2976 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
2978 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2985 TRACE(":got (%p)\n",file
);
2986 if (fd
>= 0 && !file
)
2992 /*********************************************************************
2993 * _fsopen (MSVCRT.@)
2995 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
2998 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3000 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3001 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3002 *MSVCRT__errno() = MSVCRT_EINVAL
;
3005 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3008 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3009 *MSVCRT__errno() = MSVCRT_EINVAL
;
3013 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
3020 /*********************************************************************
3023 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
3025 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3028 /*********************************************************************
3029 * fopen_s (MSVCRT.@)
3031 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
3032 const char *filename
, const char *mode
)
3034 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3035 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3036 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3038 *pFile
= MSVCRT_fopen(filename
, mode
);
3041 return *MSVCRT__errno();
3045 /*********************************************************************
3046 * _wfopen (MSVCRT.@)
3048 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
3050 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3053 /*********************************************************************
3054 * _wfopen_s (MSVCRT.@)
3056 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
3057 const MSVCRT_wchar_t
*mode
)
3059 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3060 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3061 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3063 *pFile
= MSVCRT__wfopen(filename
, mode
);
3066 return *MSVCRT__errno();
3070 /*********************************************************************
3071 * _flsbuf (MSVCRT.@)
3073 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3075 /* Flush output buffer */
3076 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3077 msvcrt_alloc_buffer(file
);
3079 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3080 if(file
->_flag
& MSVCRT__IORW
)
3081 file
->_flag
|= MSVCRT__IOWRT
;
3089 res
= msvcrt_flush_buffer(file
);
3093 res
= msvcrt_flush_buffer(file
);
3096 return res
? res
: c
&0xff;
3100 /* set _cnt to 0 for unbuffered FILEs */
3102 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3105 file
->_flag
|= MSVCRT__IOERR
;
3110 /*********************************************************************
3113 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
3117 MSVCRT__lock_file(file
);
3123 res
= msvcrt_flush_buffer(file
);
3124 MSVCRT__unlock_file(file
);
3125 return res
? res
: c
;
3128 MSVCRT__unlock_file(file
);
3132 res
= MSVCRT__flsbuf(c
, file
);
3133 MSVCRT__unlock_file(file
);
3138 /*********************************************************************
3139 * _fputchar (MSVCRT.@)
3141 int CDECL
MSVCRT__fputchar(int c
)
3143 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3146 /*********************************************************************
3149 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3151 MSVCRT_size_t rcnt
=size
* nmemb
;
3152 MSVCRT_size_t read
=0;
3158 MSVCRT__lock_file(file
);
3160 /* first buffered data */
3162 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3163 memcpy(ptr
, file
->_ptr
, pcnt
);
3168 ptr
= (char*)ptr
+ pcnt
;
3169 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3170 if(file
->_flag
& MSVCRT__IORW
) {
3171 file
->_flag
|= MSVCRT__IOREAD
;
3173 MSVCRT__unlock_file(file
);
3180 /* Fill the buffer on small reads.
3181 * TODO: Use a better buffering strategy.
3183 if (!file
->_cnt
&& size
*nmemb
<= MSVCRT_BUFSIZ
/2 && !(file
->_flag
& MSVCRT__IONBF
)) {
3184 if (file
->_bufsiz
== 0) {
3185 msvcrt_alloc_buffer(file
);
3187 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3188 file
->_ptr
= file
->_base
;
3189 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3190 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3191 if (i
> 0 && i
< file
->_cnt
) {
3192 msvcrt_get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3193 file
->_flag
&= ~MSVCRT__IOEOF
;
3196 memcpy(ptr
, file
->_ptr
, i
);
3201 i
= MSVCRT__read(file
->_file
,ptr
, rcnt
);
3205 ptr
= (char *)ptr
+i
;
3206 /* expose feof condition in the flags
3207 * MFC tests file->_flag for feof, and doesn't call feof())
3209 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3210 file
->_flag
|= MSVCRT__IOEOF
;
3213 file
->_flag
|= MSVCRT__IOERR
;
3220 MSVCRT__unlock_file(file
);
3224 /*********************************************************************
3225 * _wfreopen (MSVCRT.@)
3228 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
3230 int open_flags
, stream_flags
, fd
;
3232 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
3235 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
3239 MSVCRT_fclose(file
);
3240 /* map mode string to open() flags. "man fopen" for possibilities. */
3241 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3245 fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3248 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
3251 WARN(":failed-last error (%d)\n",GetLastError());
3252 msvcrt_set_errno(GetLastError());
3261 /*********************************************************************
3262 * _wfreopen_s (MSVCRT.@)
3264 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
3265 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
3267 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3268 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
3269 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3270 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
3272 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
3275 return *MSVCRT__errno();
3279 /*********************************************************************
3280 * freopen (MSVCRT.@)
3283 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
3286 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3288 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3289 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3295 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
3302 /*********************************************************************
3303 * freopen_s (MSVCRT.@)
3305 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
3306 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
3308 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3309 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
3310 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3311 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
3313 *pFile
= MSVCRT_freopen(path
, mode
, file
);
3316 return *MSVCRT__errno();
3320 /*********************************************************************
3321 * fsetpos (MSVCRT.@)
3323 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
3327 MSVCRT__lock_file(file
);
3328 /* Note that all this has been lifted 'as is' from fseek */
3329 if(file
->_flag
& MSVCRT__IOWRT
)
3330 msvcrt_flush_buffer(file
);
3332 /* Discard buffered input */
3334 file
->_ptr
= file
->_base
;
3336 /* Reset direction of i/o */
3337 if(file
->_flag
& MSVCRT__IORW
) {
3338 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
3341 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3342 MSVCRT__unlock_file(file
);
3346 /*********************************************************************
3347 * _ftelli64 (MSVCRT.@)
3349 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
3351 /* TODO: just call fgetpos and return lower half of result */
3355 MSVCRT__lock_file(file
);
3356 pos
= _telli64(file
->_file
);
3358 MSVCRT__unlock_file(file
);
3362 if( file
->_flag
& MSVCRT__IOWRT
) {
3363 off
= file
->_ptr
- file
->_base
;
3366 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3367 /* Black magic correction for CR removal */
3369 for (i
=0; i
<file
->_cnt
; i
++) {
3370 if (file
->_ptr
[i
] == '\n')
3373 /* Black magic when reading CR at buffer boundary*/
3374 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3380 MSVCRT__unlock_file(file
);
3384 /*********************************************************************
3387 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
3389 return MSVCRT__ftelli64(file
);
3392 /*********************************************************************
3393 * fgetpos (MSVCRT.@)
3395 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
3399 MSVCRT__lock_file(file
);
3400 *pos
= MSVCRT__lseeki64(file
->_file
,0,SEEK_CUR
);
3402 MSVCRT__unlock_file(file
);
3406 if( file
->_flag
& MSVCRT__IOWRT
) {
3407 off
= file
->_ptr
- file
->_base
;
3410 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3411 /* Black magic correction for CR removal */
3413 for (i
=0; i
<file
->_cnt
; i
++) {
3414 if (file
->_ptr
[i
] == '\n')
3417 /* Black magic when reading CR at buffer boundary*/
3418 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3424 MSVCRT__unlock_file(file
);
3428 /*********************************************************************
3431 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
3433 MSVCRT_size_t i
, len
= strlen(s
);
3436 MSVCRT__lock_file(file
);
3437 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3438 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
3439 MSVCRT__unlock_file(file
);
3442 for (i
=0; i
<len
; i
++)
3443 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
) {
3444 MSVCRT__unlock_file(file
);
3448 MSVCRT__unlock_file(file
);
3452 /*********************************************************************
3455 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
3457 MSVCRT_size_t i
, len
= strlenW(s
);
3460 MSVCRT__lock_file(file
);
3461 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3462 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
3463 MSVCRT__unlock_file(file
);
3466 for (i
=0; i
<len
; i
++) {
3467 if (((s
[i
] == '\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
3468 || MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
) {
3469 MSVCRT__unlock_file(file
);
3474 MSVCRT__unlock_file(file
);
3478 /*********************************************************************
3479 * getchar (MSVCRT.@)
3481 int CDECL
MSVCRT_getchar(void)
3483 return MSVCRT_fgetc(MSVCRT_stdin
);
3486 /*********************************************************************
3489 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
3491 return MSVCRT_fgetc(file
);
3494 /*********************************************************************
3497 char * CDECL
MSVCRT_gets(char *buf
)
3500 char * buf_start
= buf
;
3502 MSVCRT__lock_file(MSVCRT_stdin
);
3503 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
3504 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
3505 if(cc
!= '\r') *buf
++ = (char)cc
;
3509 TRACE("got '%s'\n", buf_start
);
3510 MSVCRT__unlock_file(MSVCRT_stdin
);
3514 /*********************************************************************
3517 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
3520 MSVCRT_wchar_t
* ws
= buf
;
3522 MSVCRT__lock_file(MSVCRT_stdin
);
3523 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
3524 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
3527 *buf
++ = (MSVCRT_wchar_t
)cc
;
3531 TRACE("got %s\n", debugstr_w(ws
));
3532 MSVCRT__unlock_file(MSVCRT_stdin
);
3536 /*********************************************************************
3539 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
3541 return MSVCRT_fputc(c
, file
);
3544 /*********************************************************************
3545 * putchar (MSVCRT.@)
3547 int CDECL
MSVCRT_putchar(int c
)
3549 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3552 /*********************************************************************
3553 * _putwch (MSVCRT.@)
3555 int CDECL
MSVCRT__putwch(int c
)
3557 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
3560 /*********************************************************************
3563 int CDECL
MSVCRT_puts(const char *s
)
3565 MSVCRT_size_t len
= strlen(s
);
3568 MSVCRT__lock_file(MSVCRT_stdout
);
3569 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
3570 MSVCRT__unlock_file(MSVCRT_stdout
);
3574 ret
= MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3575 MSVCRT__unlock_file(MSVCRT_stdout
);
3579 /*********************************************************************
3582 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
3584 static const MSVCRT_wchar_t nl
= '\n';
3585 MSVCRT_size_t len
= strlenW(s
);
3588 MSVCRT__lock_file(MSVCRT_stdout
);
3589 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
3590 MSVCRT__unlock_file(MSVCRT_stdout
);
3594 ret
= MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3595 MSVCRT__unlock_file(MSVCRT_stdout
);
3599 /*********************************************************************
3602 int CDECL
MSVCRT_remove(const char *path
)
3604 TRACE("(%s)\n",path
);
3605 if (DeleteFileA(path
))
3607 TRACE(":failed (%d)\n",GetLastError());
3608 msvcrt_set_errno(GetLastError());
3612 /*********************************************************************
3613 * _wremove (MSVCRT.@)
3615 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
3617 TRACE("(%s)\n",debugstr_w(path
));
3618 if (DeleteFileW(path
))
3620 TRACE(":failed (%d)\n",GetLastError());
3621 msvcrt_set_errno(GetLastError());
3625 /*********************************************************************
3628 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
3630 TRACE(":from %s to %s\n",oldpath
,newpath
);
3631 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3633 TRACE(":failed (%d)\n",GetLastError());
3634 msvcrt_set_errno(GetLastError());
3638 /*********************************************************************
3639 * _wrename (MSVCRT.@)
3641 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
3643 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3644 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3646 TRACE(":failed (%d)\n",GetLastError());
3647 msvcrt_set_errno(GetLastError());
3651 /*********************************************************************
3652 * setvbuf (MSVCRT.@)
3654 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
3656 MSVCRT__lock_file(file
);
3658 MSVCRT_free(file
->_base
);
3662 if(mode
== MSVCRT__IOFBF
) {
3663 file
->_flag
&= ~MSVCRT__IONBF
;
3664 file
->_base
= file
->_ptr
= buf
;
3666 file
->_bufsiz
= size
;
3669 file
->_flag
|= MSVCRT__IONBF
;
3671 MSVCRT__unlock_file(file
);
3675 /*********************************************************************
3678 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
3680 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
3683 /*********************************************************************
3686 char * CDECL
MSVCRT_tmpnam(char *s
)
3693 thread_data_t
*data
= msvcrt_get_thread_data();
3695 if(!data
->tmpnam_buffer
)
3696 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
3698 s
= data
->tmpnam_buffer
;
3701 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3702 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3703 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3705 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
3706 memcpy(p
, tmpstr
, size
);
3708 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3709 GetLastError() == ERROR_FILE_NOT_FOUND
)
3715 /*********************************************************************
3716 * _wtmpnam (MSVCRT.@)
3718 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
3720 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
3721 MSVCRT_wchar_t tmpstr
[16];
3725 thread_data_t
*data
= msvcrt_get_thread_data();
3727 if(!data
->wtmpnam_buffer
)
3728 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
3730 s
= data
->wtmpnam_buffer
;
3733 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3734 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3735 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3737 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
3738 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
3740 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3741 GetLastError() == ERROR_FILE_NOT_FOUND
)
3747 /*********************************************************************
3748 * tmpfile (MSVCRT.@)
3750 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
3752 char *filename
= MSVCRT_tmpnam(NULL
);
3754 MSVCRT_FILE
* file
= NULL
;
3757 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
);
3758 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3760 if (msvcrt_init_fp(file
, fd
, MSVCRT__O_RDWR
) == -1)
3765 else file
->_tmpfname
= MSVCRT__strdup(filename
);
3771 /*********************************************************************
3772 * tmpfile_s (MSVCRT.@)
3774 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
3776 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
3778 *file
= MSVCRT_tmpfile();
3782 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
3784 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
3787 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
3789 return MSVCRT_fwrite(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
3792 /*********************************************************************
3793 * vfprintf (MSVCRT.@)
3795 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3799 MSVCRT__lock_file(file
);
3800 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
3801 MSVCRT__unlock_file(file
);
3806 /*********************************************************************
3807 * vfprintf_s (MSVCRT.@)
3809 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3813 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
3815 MSVCRT__lock_file(file
);
3816 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
3817 MSVCRT__unlock_file(file
);
3822 /*********************************************************************
3823 * vfwprintf (MSVCRT.@)
3825 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3829 MSVCRT__lock_file(file
);
3830 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
3831 MSVCRT__unlock_file(file
);
3836 /*********************************************************************
3837 * vfwprintf_s (MSVCRT.@)
3839 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3843 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
3845 MSVCRT__lock_file(file
);
3846 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
3847 MSVCRT__unlock_file(file
);
3852 /*********************************************************************
3853 * vprintf (MSVCRT.@)
3855 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
3857 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
3860 /*********************************************************************
3861 * vprintf_s (MSVCRT.@)
3863 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
3865 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
3868 /*********************************************************************
3869 * vwprintf (MSVCRT.@)
3871 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3873 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
3876 /*********************************************************************
3877 * vwprintf_s (MSVCRT.@)
3879 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3881 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
3884 /*********************************************************************
3885 * fprintf (MSVCRT.@)
3887 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
3889 __ms_va_list valist
;
3891 __ms_va_start(valist
, format
);
3892 res
= MSVCRT_vfprintf(file
, format
, valist
);
3893 __ms_va_end(valist
);
3897 /*********************************************************************
3898 * fprintf_s (MSVCRT.@)
3900 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
3902 __ms_va_list valist
;
3904 __ms_va_start(valist
, format
);
3905 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
3906 __ms_va_end(valist
);
3910 /*********************************************************************
3911 * fwprintf (MSVCRT.@)
3913 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
3915 __ms_va_list valist
;
3917 __ms_va_start(valist
, format
);
3918 res
= MSVCRT_vfwprintf(file
, format
, valist
);
3919 __ms_va_end(valist
);
3923 /*********************************************************************
3924 * fwprintf_s (MSVCRT.@)
3926 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
3928 __ms_va_list valist
;
3930 __ms_va_start(valist
, format
);
3931 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
3932 __ms_va_end(valist
);
3936 /*********************************************************************
3939 int CDECL
MSVCRT_printf(const char *format
, ...)
3941 __ms_va_list valist
;
3943 __ms_va_start(valist
, format
);
3944 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
3945 __ms_va_end(valist
);
3949 /*********************************************************************
3950 * printf_s (MSVCRT.@)
3952 int CDECL
MSVCRT_printf_s(const char *format
, ...)
3954 __ms_va_list valist
;
3956 __ms_va_start(valist
, format
);
3957 res
= MSVCRT_vprintf_s(format
, valist
);
3958 __ms_va_end(valist
);
3962 /*********************************************************************
3965 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
3967 if (c
== MSVCRT_EOF
)
3970 MSVCRT__lock_file(file
);
3971 if(file
->_bufsiz
== 0) {
3972 msvcrt_alloc_buffer(file
);
3975 if(file
->_ptr
>file
->_base
) {
3979 MSVCRT_clearerr(file
);
3980 MSVCRT__unlock_file(file
);
3984 MSVCRT__unlock_file(file
);
3988 /*********************************************************************
3989 * ungetwc (MSVCRT.@)
3991 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3993 MSVCRT_wchar_t mwc
= wc
;
3994 char * pp
= (char *)&mwc
;
3997 MSVCRT__lock_file(file
);
3998 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
3999 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
)) {
4000 MSVCRT__unlock_file(file
);
4005 MSVCRT__unlock_file(file
);
4009 /*********************************************************************
4010 * wprintf (MSVCRT.@)
4012 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
4014 __ms_va_list valist
;
4016 __ms_va_start(valist
, format
);
4017 res
= MSVCRT_vwprintf(format
, valist
);
4018 __ms_va_end(valist
);
4022 /*********************************************************************
4023 * wprintf_s (MSVCRT.@)
4025 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
4027 __ms_va_list valist
;
4029 __ms_va_start(valist
, format
);
4030 res
= MSVCRT_vwprintf_s(format
, valist
);
4031 __ms_va_end(valist
);
4035 /*********************************************************************
4036 * _getmaxstdio (MSVCRT.@)
4038 int CDECL
MSVCRT__getmaxstdio(void)
4040 return MSVCRT_max_streams
;
4043 /*********************************************************************
4044 * _setmaxstdio (MSVCRT.@)
4046 int CDECL
MSVCRT__setmaxstdio(int newmax
)
4048 TRACE("%d\n", newmax
);
4050 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
4053 MSVCRT_max_streams
= newmax
;
4054 return MSVCRT_max_streams
;