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"
37 #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_READNL 0x04 /* read started with \n */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
71 /* values for exflag - it's used differently in msvcr90.dll*/
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom
[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
92 CRITICAL_SECTION crit
;
98 BOOL utf8translations
;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
119 CRITICAL_SECTION crit
;
122 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
123 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
124 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
126 /* INTERNAL: process umask */
127 static int MSVCRT_umask
= 0;
129 /* INTERNAL: static data for tmpnam and _wtmpname functions */
130 static int tmpnam_unique
;
131 static int tmpnam_s_unique
;
133 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
134 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
135 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
136 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
138 #define TOUL(x) (ULONGLONG)(x)
139 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
140 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
141 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
142 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
144 /* This critical section protects the MSVCRT_fstreams table
145 * and MSVCRT_stream_idx from race conditions. It also
146 * protects fd critical sections creation code.
148 static CRITICAL_SECTION MSVCRT_file_cs
;
149 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
151 0, 0, &MSVCRT_file_cs
,
152 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
153 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
155 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
156 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
157 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
159 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
161 buf
->st_dev
= buf64
->st_dev
;
162 buf
->st_ino
= buf64
->st_ino
;
163 buf
->st_mode
= buf64
->st_mode
;
164 buf
->st_nlink
= buf64
->st_nlink
;
165 buf
->st_uid
= buf64
->st_uid
;
166 buf
->st_gid
= buf64
->st_gid
;
167 buf
->st_rdev
= buf64
->st_rdev
;
168 buf
->st_size
= buf64
->st_size
;
169 buf
->st_atime
= buf64
->st_atime
;
170 buf
->st_mtime
= buf64
->st_mtime
;
171 buf
->st_ctime
= buf64
->st_ctime
;
174 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
176 buf
->st_dev
= buf64
->st_dev
;
177 buf
->st_ino
= buf64
->st_ino
;
178 buf
->st_mode
= buf64
->st_mode
;
179 buf
->st_nlink
= buf64
->st_nlink
;
180 buf
->st_uid
= buf64
->st_uid
;
181 buf
->st_gid
= buf64
->st_gid
;
182 buf
->st_rdev
= buf64
->st_rdev
;
183 buf
->st_size
= buf64
->st_size
;
184 buf
->st_atime
= buf64
->st_atime
;
185 buf
->st_mtime
= buf64
->st_mtime
;
186 buf
->st_ctime
= buf64
->st_ctime
;
189 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
191 buf
->st_dev
= buf64
->st_dev
;
192 buf
->st_ino
= buf64
->st_ino
;
193 buf
->st_mode
= buf64
->st_mode
;
194 buf
->st_nlink
= buf64
->st_nlink
;
195 buf
->st_uid
= buf64
->st_uid
;
196 buf
->st_gid
= buf64
->st_gid
;
197 buf
->st_rdev
= buf64
->st_rdev
;
198 buf
->st_size
= buf64
->st_size
;
199 buf
->st_atime
= buf64
->st_atime
;
200 buf
->st_mtime
= buf64
->st_mtime
;
201 buf
->st_ctime
= buf64
->st_ctime
;
204 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
206 buf
->st_dev
= buf64
->st_dev
;
207 buf
->st_ino
= buf64
->st_ino
;
208 buf
->st_mode
= buf64
->st_mode
;
209 buf
->st_nlink
= buf64
->st_nlink
;
210 buf
->st_uid
= buf64
->st_uid
;
211 buf
->st_gid
= buf64
->st_gid
;
212 buf
->st_rdev
= buf64
->st_rdev
;
213 buf
->st_size
= buf64
->st_size
;
214 buf
->st_atime
= buf64
->st_atime
;
215 buf
->st_mtime
= buf64
->st_mtime
;
216 buf
->st_ctime
= buf64
->st_ctime
;
219 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
221 buf
->st_dev
= buf64
->st_dev
;
222 buf
->st_ino
= buf64
->st_ino
;
223 buf
->st_mode
= buf64
->st_mode
;
224 buf
->st_nlink
= buf64
->st_nlink
;
225 buf
->st_uid
= buf64
->st_uid
;
226 buf
->st_gid
= buf64
->st_gid
;
227 buf
->st_rdev
= buf64
->st_rdev
;
228 buf
->st_size
= buf64
->st_size
;
229 buf
->st_atime
= buf64
->st_atime
;
230 buf
->st_mtime
= buf64
->st_mtime
;
231 buf
->st_ctime
= buf64
->st_ctime
;
234 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
236 /* 1601 to 1970 is 369 years plus 89 leap days */
237 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
239 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
240 ft
->dwHighDateTime
= ticks
>> 32;
241 ft
->dwLowDateTime
= ticks
;
244 static inline ioinfo
* get_ioinfo_nolock(int fd
)
247 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
248 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
250 return &MSVCRT___badioinfo
;
252 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
255 static inline void init_ioinfo_cs(ioinfo
*info
)
257 if(!(info
->exflag
& EF_CRIT_INIT
)) {
259 if(!(info
->exflag
& EF_CRIT_INIT
)) {
260 InitializeCriticalSection(&info
->crit
);
261 info
->exflag
|= EF_CRIT_INIT
;
267 static inline ioinfo
* get_ioinfo(int fd
)
269 ioinfo
*ret
= get_ioinfo_nolock(fd
);
270 if(ret
== &MSVCRT___badioinfo
)
273 EnterCriticalSection(&ret
->crit
);
277 static inline BOOL
alloc_pioinfo_block(int fd
)
282 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
284 *MSVCRT__errno() = MSVCRT_ENFILE
;
288 block
= MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
291 WARN(":out of memory!\n");
292 *MSVCRT__errno() = MSVCRT_ENOMEM
;
295 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
296 block
[i
].handle
= INVALID_HANDLE_VALUE
;
297 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
302 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
306 ret
= get_ioinfo(fd
);
307 if(ret
!= &MSVCRT___badioinfo
)
310 if(!alloc_pioinfo_block(fd
))
311 return &MSVCRT___badioinfo
;
313 return get_ioinfo(fd
);
316 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
321 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
323 ioinfo
*info
= get_ioinfo_nolock(i
);
325 if(info
== &MSVCRT___badioinfo
)
327 if(!alloc_pioinfo_block(i
))
328 return &MSVCRT___badioinfo
;
329 info
= get_ioinfo_nolock(i
);
332 init_ioinfo_cs(info
);
333 if(TryEnterCriticalSection(&info
->crit
))
335 if(info
->handle
== INVALID_HANDLE_VALUE
)
340 LeaveCriticalSection(&info
->crit
);
344 WARN(":files exhausted!\n");
345 *MSVCRT__errno() = MSVCRT_ENFILE
;
346 return &MSVCRT___badioinfo
;
349 static inline void release_ioinfo(ioinfo
*info
)
351 if(info
!=&MSVCRT___badioinfo
&& info
->exflag
& EF_CRIT_INIT
)
352 LeaveCriticalSection(&info
->crit
);
355 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
359 if(i
>= MSVCRT_max_streams
)
363 return &MSVCRT__iob
[i
];
365 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
367 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
368 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
369 ERR("out of memory\n");
370 *MSVCRT__errno() = MSVCRT_ENOMEM
;
374 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
376 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
381 /* INTERNAL: free a file entry fd */
382 static void msvcrt_free_fd(int fd
)
384 ioinfo
*fdinfo
= get_ioinfo(fd
);
386 if(fdinfo
!= &MSVCRT___badioinfo
)
388 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
391 TRACE(":fd (%d) freed\n",fd
);
398 SetStdHandle(STD_INPUT_HANDLE
, 0);
401 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
404 SetStdHandle(STD_ERROR_HANDLE
, 0);
408 release_ioinfo(fdinfo
);
411 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
413 fdinfo
->handle
= hand
;
414 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
415 fdinfo
->lookahead
[0] = '\n';
416 fdinfo
->lookahead
[1] = '\n';
417 fdinfo
->lookahead
[2] = '\n';
418 fdinfo
->exflag
&= EF_CRIT_INIT
;
420 if (hand
== MSVCRT_NO_CONSOLE
) hand
= 0;
421 switch (fdinfo
-MSVCRT___pioinfo
[0])
423 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
424 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
425 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
429 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
430 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
433 ioinfo
*info
= get_ioinfo_alloc(&fd
);
435 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
437 if(info
== &MSVCRT___badioinfo
)
440 msvcrt_set_fd(info
, hand
, flag
);
441 release_ioinfo(info
);
445 /* INTERNAL: Allocate a FILE* for an fd slot */
446 /* caller must hold the files lock */
447 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
452 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
454 file
= msvcrt_get_file(i
);
458 if (file
->_flag
== 0)
460 if (i
== MSVCRT_stream_idx
)
462 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
464 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
465 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
476 /* INTERNAL: initialize a FILE* from an open fd */
477 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
479 TRACE(":fd (%d) allocating FILE*\n",fd
);
480 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
482 WARN(":invalid fd %d\n",fd
);
483 *MSVCRT___doserrno() = 0;
484 *MSVCRT__errno() = MSVCRT_EBADF
;
487 file
->_ptr
= file
->_base
= NULL
;
490 file
->_flag
= stream_flags
;
491 file
->_tmpfname
= NULL
;
493 TRACE(":got FILE* (%p)\n",file
);
497 /* INTERNAL: Create an inheritance data block (for spawned process)
498 * The inheritance block is made of:
499 * 00 int nb of file descriptor (NBFD)
500 * 04 char file flags (wxflag): repeated for each fd
501 * 4+NBFD HANDLE file handle: repeated for each fd
503 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
510 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
511 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
515 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
516 *block
= MSVCRT_calloc(1, *size
);
522 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
523 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
525 *(unsigned*)*block
= last_fd
;
526 for (fd
= 0; fd
< last_fd
; fd
++)
528 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
529 fdinfo
= get_ioinfo(fd
);
530 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
532 *wxflag_ptr
= fdinfo
->wxflag
;
533 *handle_ptr
= fdinfo
->handle
;
538 *handle_ptr
= INVALID_HANDLE_VALUE
;
540 release_ioinfo(fdinfo
);
541 wxflag_ptr
++; handle_ptr
++;
546 /* INTERNAL: Set up all file descriptors,
547 * as well as default streams (stdin, stderr and stdout)
549 void msvcrt_init_io(void)
555 GetStartupInfoA(&si
);
556 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
562 count
= *(unsigned*)si
.lpReserved2
;
563 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
564 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
566 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
567 count
= min(count
, MSVCRT_MAX_FILES
);
568 for (i
= 0; i
< count
; i
++)
570 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
572 fdinfo
= get_ioinfo_alloc_fd(i
);
573 if(fdinfo
!= &MSVCRT___badioinfo
)
574 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
575 release_ioinfo(fdinfo
);
578 wxflag_ptr
++; handle_ptr
++;
582 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDIN_FILENO
);
583 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
584 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
585 DWORD flags
= WX_OPEN
| WX_TEXT
;
586 DWORD type
= GetFileType(h
);
588 if (type
== FILE_TYPE_UNKNOWN
) {
589 h
= MSVCRT_NO_CONSOLE
;
591 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
593 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
597 msvcrt_set_fd(fdinfo
, h
, flags
);
599 release_ioinfo(fdinfo
);
601 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO
);
602 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
603 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
604 DWORD flags
= WX_OPEN
| WX_TEXT
;
605 DWORD type
= GetFileType(h
);
607 if (type
== FILE_TYPE_UNKNOWN
) {
608 h
= MSVCRT_NO_CONSOLE
;
610 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
612 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
616 msvcrt_set_fd(fdinfo
, h
, flags
);
618 release_ioinfo(fdinfo
);
620 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO
);
621 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
622 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
623 DWORD flags
= WX_OPEN
| WX_TEXT
;
624 DWORD type
= GetFileType(h
);
626 if (type
== FILE_TYPE_UNKNOWN
) {
627 h
= MSVCRT_NO_CONSOLE
;
629 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
631 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
635 msvcrt_set_fd(fdinfo
, h
, flags
);
637 release_ioinfo(fdinfo
);
639 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
640 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
641 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
643 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
644 for (i
= 0; i
< 3; i
++)
646 /* FILE structs for stdin/out/err are static and never deleted */
647 MSVCRT__iob
[i
]._file
= get_ioinfo_nolock(i
)->handle
== MSVCRT_NO_CONSOLE
?
648 MSVCRT_NO_CONSOLE_FD
: i
;
649 MSVCRT__iob
[i
]._tmpfname
= NULL
;
650 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
652 MSVCRT_stream_idx
= 3;
655 /* INTERNAL: Flush stdio file buffer */
656 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
658 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
659 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
660 int cnt
=file
->_ptr
-file
->_base
;
661 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
662 file
->_flag
|= MSVCRT__IOERR
;
666 if(file
->_flag
& MSVCRT__IORW
)
667 file
->_flag
&= ~MSVCRT__IOWRT
;
670 file
->_ptr
=file
->_base
;
675 /*********************************************************************
678 int CDECL
MSVCRT__isatty(int fd
)
680 TRACE(":fd (%d)\n",fd
);
682 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
685 /* INTERNAL: Allocate stdio file buffer */
686 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
688 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
689 && MSVCRT__isatty(file
->_file
))
692 file
->_base
= MSVCRT_calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
694 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
695 file
->_flag
|= MSVCRT__IOMYBUF
;
697 file
->_base
= (char*)(&file
->_charbuf
);
699 file
->_flag
|= MSVCRT__IONBF
;
701 file
->_ptr
= file
->_base
;
706 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
707 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
709 static char buffers
[2][MSVCRT_BUFSIZ
];
711 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
712 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
713 || !MSVCRT__isatty(file
->_file
))
716 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
717 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
718 file
->_flag
|= MSVCRT__USERBUF
;
722 /* INTERNAL: Removes temporary buffer from stdout or stderr */
723 /* Only call this function when add_std_buffer returned TRUE */
724 static void remove_std_buffer(MSVCRT_FILE
*file
)
726 msvcrt_flush_buffer(file
);
727 file
->_ptr
= file
->_base
= NULL
;
728 file
->_bufsiz
= file
->_cnt
= 0;
729 file
->_flag
&= ~MSVCRT__USERBUF
;
732 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
733 static int msvcrt_int_to_base32(int num
, char *str
)
748 *p
= (num
& 31) + '0';
750 *p
+= ('a' - '0' - 10);
757 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
758 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
773 *p
= (num
& 31) + '0';
775 *p
+= ('a' - '0' - 10);
782 /*********************************************************************
783 * __p__iob (MSVCRT.@)
785 MSVCRT_FILE
* CDECL
__p__iob(void)
787 return &MSVCRT__iob
[0];
790 #if _MSVCR_VER >= 140
791 /*********************************************************************
792 * __acrt_iob_func(UCRTBASE.@)
794 MSVCRT_FILE
* CDECL
MSVCRT___acrt_iob_func(unsigned idx
)
796 return &MSVCRT__iob
[idx
];
800 /*********************************************************************
803 int CDECL
MSVCRT__access(const char *filename
, int mode
)
805 DWORD attr
= GetFileAttributesA(filename
);
807 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
809 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
811 msvcrt_set_errno(GetLastError());
814 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
816 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
822 /*********************************************************************
823 * _access_s (MSVCRT.@)
825 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
827 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
828 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
830 if (MSVCRT__access(filename
, mode
) == -1)
831 return *MSVCRT__errno();
835 /*********************************************************************
836 * _waccess (MSVCRT.@)
838 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
840 DWORD attr
= GetFileAttributesW(filename
);
842 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
844 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
846 msvcrt_set_errno(GetLastError());
849 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
851 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
857 /*********************************************************************
858 * _waccess_s (MSVCRT.@)
860 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
862 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
863 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
865 if (MSVCRT__waccess(filename
, mode
) == -1)
866 return *MSVCRT__errno();
870 /*********************************************************************
873 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
875 DWORD oldFlags
= GetFileAttributesA(path
);
877 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
879 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
880 oldFlags
| FILE_ATTRIBUTE_READONLY
;
882 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
885 msvcrt_set_errno(GetLastError());
889 /*********************************************************************
892 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
894 DWORD oldFlags
= GetFileAttributesW(path
);
896 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
898 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
899 oldFlags
| FILE_ATTRIBUTE_READONLY
;
901 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
904 msvcrt_set_errno(GetLastError());
908 /*********************************************************************
911 int CDECL
MSVCRT__unlink(const char *path
)
913 TRACE("%s\n",debugstr_a(path
));
914 if(DeleteFileA(path
))
916 TRACE("failed (%d)\n",GetLastError());
917 msvcrt_set_errno(GetLastError());
921 /*********************************************************************
922 * _wunlink (MSVCRT.@)
924 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
926 TRACE("(%s)\n",debugstr_w(path
));
927 if(DeleteFileW(path
))
929 TRACE("failed (%d)\n",GetLastError());
930 msvcrt_set_errno(GetLastError());
934 /*********************************************************************
937 int CDECL
MSVCRT__commit(int fd
)
939 ioinfo
*info
= get_ioinfo(fd
);
942 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
944 if (info
->handle
== INVALID_HANDLE_VALUE
)
946 else if (!FlushFileBuffers(info
->handle
))
948 if (GetLastError() == ERROR_INVALID_HANDLE
)
950 /* FlushFileBuffers fails for console handles
951 * so we ignore this error.
957 TRACE(":failed-last error (%d)\n",GetLastError());
958 msvcrt_set_errno(GetLastError());
968 release_ioinfo(info
);
972 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
973 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
975 /* INTERNAL: Flush all stream buffer */
976 static int msvcrt_flush_all_buffers(int mask
)
978 int i
, num_flushed
= 0;
982 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
983 file
= msvcrt_get_file(i
);
987 if(file
->_flag
& mask
) {
995 TRACE(":flushed (%d) handles\n",num_flushed
);
999 /*********************************************************************
1000 * _flushall (MSVCRT.@)
1002 int CDECL
MSVCRT__flushall(void)
1004 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
1007 /*********************************************************************
1010 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
1015 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1018 MSVCRT__lock_file(file
);
1019 ret
= MSVCRT__fflush_nolock(file
);
1020 MSVCRT__unlock_file(file
);
1026 /*********************************************************************
1027 * _fflush_nolock (MSVCRT.@)
1029 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
1034 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1038 res
= msvcrt_flush_buffer(file
);
1039 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1040 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
1044 /*********************************************************************
1047 int CDECL
MSVCRT__close(int fd
)
1049 ioinfo
*info
= get_ioinfo(fd
);
1052 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1054 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1055 *MSVCRT__errno() = MSVCRT_EBADF
;
1057 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, MSVCRT_EBADF
)) {
1059 } else if (fd
== MSVCRT_STDOUT_FILENO
&&
1060 info
->handle
== get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
) {
1063 } else if (fd
== MSVCRT_STDERR_FILENO
&&
1064 info
->handle
== get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
) {
1068 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1071 WARN(":failed-last error (%d)\n",GetLastError());
1072 msvcrt_set_errno(GetLastError());
1075 release_ioinfo(info
);
1079 /*********************************************************************
1082 * MSDN isn't clear on this point, but the remarks for _pipe
1083 * indicate file descriptors duplicated with _dup and _dup2 are always
1086 int CDECL
MSVCRT__dup2(int od
, int nd
)
1088 ioinfo
*info_od
, *info_nd
;
1091 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1095 info_od
= get_ioinfo(od
);
1096 info_nd
= get_ioinfo_alloc_fd(nd
);
1100 info_nd
= get_ioinfo_alloc_fd(nd
);
1101 info_od
= get_ioinfo(od
);
1104 if (info_nd
== &MSVCRT___badioinfo
)
1108 else if (info_od
->wxflag
& WX_OPEN
)
1112 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1113 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1115 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1117 if (info_nd
->wxflag
& WX_OPEN
)
1120 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1121 /* _dup2 returns 0, not nd, on success */
1127 msvcrt_set_errno(GetLastError());
1132 *MSVCRT__errno() = MSVCRT_EBADF
;
1136 release_ioinfo(info_od
);
1137 release_ioinfo(info_nd
);
1141 /*********************************************************************
1144 int CDECL
MSVCRT__dup(int od
)
1147 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1149 if (MSVCRT__dup2(od
, fd
) == 0)
1153 release_ioinfo(info
);
1157 /*********************************************************************
1160 int CDECL
MSVCRT__eof(int fd
)
1162 ioinfo
*info
= get_ioinfo(fd
);
1163 DWORD curpos
,endpos
;
1164 LONG hcurpos
,hendpos
;
1166 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1168 if (info
->handle
== INVALID_HANDLE_VALUE
)
1170 release_ioinfo(info
);
1174 if (info
->wxflag
& WX_ATEOF
)
1176 release_ioinfo(info
);
1180 /* Otherwise we do it the hard way */
1181 hcurpos
= hendpos
= 0;
1182 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1183 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1185 if (curpos
== endpos
&& hcurpos
== hendpos
)
1187 /* FIXME: shouldn't WX_ATEOF be set here? */
1188 release_ioinfo(info
);
1192 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1193 release_ioinfo(info
);
1197 /*********************************************************************
1198 * _fcloseall (MSVCRT.@)
1200 int CDECL
MSVCRT__fcloseall(void)
1202 int num_closed
= 0, i
;
1206 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1207 file
= msvcrt_get_file(i
);
1209 if (file
->_flag
&& !MSVCRT_fclose(file
))
1214 TRACE(":closed (%d) handles\n",num_closed
);
1218 /* free everything on process exit */
1219 void msvcrt_free_io(void)
1225 MSVCRT__fcloseall();
1227 for(i
=0; i
<ARRAY_SIZE(MSVCRT___pioinfo
); i
++)
1229 if(!MSVCRT___pioinfo
[i
])
1232 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1234 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1235 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1237 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1240 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1242 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1243 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1245 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1246 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1250 for(i
=0; i
<ARRAY_SIZE(MSVCRT_fstream
); i
++)
1251 MSVCRT_free(MSVCRT_fstream
[i
]);
1254 /*********************************************************************
1255 * _lseeki64 (MSVCRT.@)
1257 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1259 ioinfo
*info
= get_ioinfo(fd
);
1262 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1264 if (info
->handle
== INVALID_HANDLE_VALUE
)
1266 release_ioinfo(info
);
1270 if (whence
< 0 || whence
> 2)
1272 release_ioinfo(info
);
1273 *MSVCRT__errno() = MSVCRT_EINVAL
;
1277 TRACE(":fd (%d) to %s pos %s\n",
1278 fd
,wine_dbgstr_longlong(offset
),
1279 (whence
==MSVCRT_SEEK_SET
)?"SEEK_SET":
1280 (whence
==MSVCRT_SEEK_CUR
)?"SEEK_CUR":
1281 (whence
==MSVCRT_SEEK_END
)?"SEEK_END":"UNKNOWN");
1283 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1284 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1285 ofs
.QuadPart
= offset
;
1286 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1287 GetLastError() == ERROR_SUCCESS
)
1289 info
->wxflag
&= ~WX_ATEOF
;
1290 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1292 release_ioinfo(info
);
1293 return ofs
.QuadPart
;
1295 release_ioinfo(info
);
1296 TRACE(":error-last error (%d)\n",GetLastError());
1297 msvcrt_set_errno(GetLastError());
1301 /*********************************************************************
1304 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1306 return MSVCRT__lseeki64(fd
, offset
, whence
);
1309 /*********************************************************************
1310 * _lock_file (MSVCRT.@)
1312 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1314 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1315 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1317 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1320 /*********************************************************************
1321 * _unlock_file (MSVCRT.@)
1323 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1325 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1326 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1328 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1331 /*********************************************************************
1332 * _locking (MSVCRT.@)
1334 * This is untested; the underlying LockFile doesn't work yet.
1336 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1338 ioinfo
*info
= get_ioinfo(fd
);
1342 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1343 if (info
->handle
== INVALID_HANDLE_VALUE
)
1345 release_ioinfo(info
);
1349 if (mode
< 0 || mode
> 4)
1351 release_ioinfo(info
);
1352 *MSVCRT__errno() = MSVCRT_EINVAL
;
1356 TRACE(":fd (%d) by 0x%08x mode %s\n",
1357 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1358 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1359 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1360 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1361 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1364 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1366 release_ioinfo(info
);
1367 FIXME ("Seek failed\n");
1368 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1371 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1374 ret
= 1; /* just to satisfy gcc */
1377 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1382 else if (mode
== MSVCRT__LK_UNLCK
)
1383 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1385 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1386 /* FIXME - what about error settings? */
1387 release_ioinfo(info
);
1388 return ret
? 0 : -1;
1391 /*********************************************************************
1392 * _fseeki64 (MSVCRT.@)
1394 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1398 MSVCRT__lock_file(file
);
1399 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1400 MSVCRT__unlock_file(file
);
1405 /*********************************************************************
1406 * _fseeki64_nolock (MSVCRT.@)
1408 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1412 if(whence
== MSVCRT_SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1413 whence
= MSVCRT_SEEK_SET
;
1414 offset
+= MSVCRT__ftelli64_nolock(file
);
1417 /* Flush output if needed */
1418 msvcrt_flush_buffer(file
);
1419 /* Reset direction of i/o */
1420 if(file
->_flag
& MSVCRT__IORW
) {
1421 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1423 /* Clear end of file flag */
1424 file
->_flag
&= ~MSVCRT__IOEOF
;
1425 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1430 /*********************************************************************
1433 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1435 return MSVCRT__fseeki64( file
, offset
, whence
);
1438 /*********************************************************************
1439 * _fseek_nolock (MSVCRT.@)
1441 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1443 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1446 /*********************************************************************
1447 * _chsize_s (MSVCRT.@)
1449 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1455 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1457 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1460 info
= get_ioinfo(fd
);
1461 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1463 /* save the current file pointer */
1464 cur
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1467 pos
= MSVCRT__lseeki64(fd
, size
, MSVCRT_SEEK_SET
);
1470 ret
= SetEndOfFile(info
->handle
);
1471 if (!ret
) msvcrt_set_errno(GetLastError());
1474 /* restore the file pointer */
1475 MSVCRT__lseeki64(fd
, cur
, MSVCRT_SEEK_SET
);
1479 release_ioinfo(info
);
1480 return ret
? 0 : *MSVCRT__errno();
1483 /*********************************************************************
1484 * _chsize (MSVCRT.@)
1486 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1488 /* _chsize_s returns errno on failure but _chsize should return -1 */
1489 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1492 /*********************************************************************
1493 * clearerr (MSVCRT.@)
1495 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1497 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1499 MSVCRT__lock_file(file
);
1500 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1501 MSVCRT__unlock_file(file
);
1504 /*********************************************************************
1505 * clearerr_s (MSVCRT.@)
1507 int CDECL
MSVCRT_clearerr_s(MSVCRT_FILE
* file
)
1509 TRACE(":file (%p)\n",file
);
1511 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
1513 MSVCRT__lock_file(file
);
1514 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1515 MSVCRT__unlock_file(file
);
1519 /*********************************************************************
1522 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1524 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1526 MSVCRT__lock_file(file
);
1527 MSVCRT__fseek_nolock(file
, 0L, MSVCRT_SEEK_SET
);
1528 MSVCRT_clearerr(file
);
1529 MSVCRT__unlock_file(file
);
1532 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1534 int plus
= strchrW(mode
, '+') != NULL
;
1536 TRACE("%s\n", debugstr_w(mode
));
1538 while(*mode
== ' ') mode
++;
1543 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1544 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1547 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1548 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1551 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1552 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1555 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1559 *stream_flags
|= MSVCRT__commode
;
1561 while (*mode
&& *mode
!=',')
1565 *open_flags
|= MSVCRT__O_BINARY
;
1566 *open_flags
&= ~MSVCRT__O_TEXT
;
1569 *open_flags
|= MSVCRT__O_TEXT
;
1570 *open_flags
&= ~MSVCRT__O_BINARY
;
1573 *open_flags
|= MSVCRT__O_TEMPORARY
;
1576 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1579 *stream_flags
|= MSVCRT__IOCOMMIT
;
1582 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1585 *open_flags
|= MSVCRT__O_NOINHERIT
;
1594 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1597 ERR("incorrect mode flag: %c\n", mode
[-1]);
1603 static const WCHAR ccs
[] = {'c','c','s'};
1604 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1605 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1606 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1609 while(*mode
== ' ') mode
++;
1610 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, ARRAY_SIZE(ccs
))))
1612 mode
+= ARRAY_SIZE(ccs
);
1613 while(*mode
== ' ') mode
++;
1614 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1617 while(*mode
== ' ') mode
++;
1619 if(!strncmpiW(utf8
, mode
, ARRAY_SIZE(utf8
)))
1621 *open_flags
|= MSVCRT__O_U8TEXT
;
1622 mode
+= ARRAY_SIZE(utf8
);
1624 else if(!strncmpiW(utf16le
, mode
, ARRAY_SIZE(utf16le
)))
1626 *open_flags
|= MSVCRT__O_U16TEXT
;
1627 mode
+= ARRAY_SIZE(utf16le
);
1629 else if(!strncmpiW(unicode
, mode
, ARRAY_SIZE(unicode
)))
1631 *open_flags
|= MSVCRT__O_WTEXT
;
1632 mode
+= ARRAY_SIZE(unicode
);
1636 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1640 while(*mode
== ' ') mode
++;
1643 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1648 /*********************************************************************
1649 * _fdopen (MSVCRT.@)
1651 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1654 MSVCRT_wchar_t
*modeW
= NULL
;
1656 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1658 ret
= MSVCRT__wfdopen(fd
, modeW
);
1664 /*********************************************************************
1665 * _wfdopen (MSVCRT.@)
1667 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1669 int open_flags
, stream_flags
;
1672 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1675 if (!(file
= msvcrt_alloc_fp()))
1677 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1682 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1688 /*********************************************************************
1689 * _filelength (MSVCRT.@)
1691 LONG CDECL
MSVCRT__filelength(int fd
)
1693 LONG curPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
1696 LONG endPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_END
);
1699 if (endPos
!= curPos
)
1700 MSVCRT__lseek(fd
, curPos
, MSVCRT_SEEK_SET
);
1707 /*********************************************************************
1708 * _filelengthi64 (MSVCRT.@)
1710 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1712 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1715 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_END
);
1718 if (endPos
!= curPos
)
1719 MSVCRT__lseeki64(fd
, curPos
, MSVCRT_SEEK_SET
);
1726 /*********************************************************************
1727 * _fileno (MSVCRT.@)
1729 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1731 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1735 /*********************************************************************
1736 * _fstat64 (MSVCRT.@)
1738 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1740 ioinfo
*info
= get_ioinfo(fd
);
1743 BY_HANDLE_FILE_INFORMATION hfi
;
1745 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1746 if (info
->handle
== INVALID_HANDLE_VALUE
)
1748 release_ioinfo(info
);
1754 WARN(":failed-NULL buf\n");
1755 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1756 release_ioinfo(info
);
1760 memset(&hfi
, 0, sizeof(hfi
));
1761 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1762 type
= GetFileType(info
->handle
);
1763 if (type
== FILE_TYPE_PIPE
)
1765 buf
->st_dev
= buf
->st_rdev
= fd
;
1766 buf
->st_mode
= MSVCRT__S_IFIFO
;
1769 else if (type
== FILE_TYPE_CHAR
)
1771 buf
->st_dev
= buf
->st_rdev
= fd
;
1772 buf
->st_mode
= MSVCRT__S_IFCHR
;
1775 else /* FILE_TYPE_DISK etc. */
1777 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1779 WARN(":failed-last error (%d)\n",GetLastError());
1780 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1781 release_ioinfo(info
);
1784 buf
->st_mode
= MSVCRT__S_IFREG
| 0444;
1785 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1786 buf
->st_mode
|= 0222;
1787 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1788 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1790 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1791 buf
->st_mtime
= buf
->st_ctime
= dw
;
1792 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1794 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1796 release_ioinfo(info
);
1800 /*********************************************************************
1801 * _fstati64 (MSVCRT.@)
1803 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1806 struct MSVCRT__stat64 buf64
;
1808 ret
= MSVCRT__fstat64(fd
, &buf64
);
1810 msvcrt_stat64_to_stati64(&buf64
, buf
);
1814 /*********************************************************************
1817 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1819 struct MSVCRT__stat64 buf64
;
1821 ret
= MSVCRT__fstat64(fd
, &buf64
);
1823 msvcrt_stat64_to_stat(&buf64
, buf
);
1827 /*********************************************************************
1828 * _fstat32 (MSVCR80.@)
1830 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1833 struct MSVCRT__stat64 buf64
;
1835 ret
= MSVCRT__fstat64(fd
, &buf64
);
1837 msvcrt_stat64_to_stat32(&buf64
, buf
);
1841 /*********************************************************************
1842 * _fstat32i64 (MSVCR80.@)
1844 int CDECL
MSVCRT__fstat32i64(int fd
, struct MSVCRT__stat32i64
* buf
)
1847 struct MSVCRT__stat64 buf64
;
1849 ret
= MSVCRT__fstat64(fd
, &buf64
);
1851 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1855 /*********************************************************************
1856 * _fstat64i32 (MSVCR80.@)
1858 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1861 struct MSVCRT__stat64 buf64
;
1863 ret
= MSVCRT__fstat64(fd
, &buf64
);
1865 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1869 /*********************************************************************
1870 * _futime64 (MSVCRT.@)
1872 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1874 ioinfo
*info
= get_ioinfo(fd
);
1879 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1884 time_to_filetime( t
->actime
, &at
);
1885 time_to_filetime( t
->modtime
, &wt
);
1888 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1890 release_ioinfo(info
);
1891 msvcrt_set_errno(GetLastError());
1894 release_ioinfo(info
);
1898 /*********************************************************************
1899 * _futime32 (MSVCRT.@)
1901 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1905 struct MSVCRT___utimbuf64 t64
;
1906 t64
.actime
= t
->actime
;
1907 t64
.modtime
= t
->modtime
;
1908 return _futime64( fd
, &t64
);
1911 return _futime64( fd
, NULL
);
1914 /*********************************************************************
1915 * _futime (MSVCRT.@)
1918 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1920 return _futime64( fd
, t
);
1923 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1925 return _futime32( fd
, t
);
1929 /*********************************************************************
1930 * _get_osfhandle (MSVCRT.@)
1932 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1934 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1935 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1937 if(hand
== INVALID_HANDLE_VALUE
)
1938 *MSVCRT__errno() = MSVCRT_EBADF
;
1939 return (MSVCRT_intptr_t
)hand
;
1942 /*********************************************************************
1943 * _mktemp_s (MSVCRT.@)
1945 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1949 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1950 return MSVCRT_EINVAL
;
1952 for(len
=0; len
<size
; len
++)
1955 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1958 return MSVCRT_EINVAL
;
1961 for(xno
=1; xno
<=6; xno
++)
1962 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1963 return MSVCRT_EINVAL
;
1965 id
= GetCurrentProcessId();
1966 for(xno
=1; xno
<6; xno
++) {
1967 pattern
[len
-xno
] = id
%10 + '0';
1971 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1972 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1977 *MSVCRT__errno() = MSVCRT_EEXIST
;
1978 return MSVCRT_EEXIST
;
1981 /*********************************************************************
1982 * _mktemp (MSVCRT.@)
1984 char * CDECL
MSVCRT__mktemp(char *pattern
)
1987 char *retVal
= pattern
;
1995 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1999 id
= GetCurrentProcessId();
2003 int tempNum
= id
/ 10;
2004 *pattern
-- = id
- (tempNum
* 10) + '0';
2010 *pattern
= letter
++;
2011 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2013 } while(letter
<= 'z');
2017 /*********************************************************************
2018 * _wmktemp_s (MSVCRT.@)
2020 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
2024 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2025 return MSVCRT_EINVAL
;
2027 for(len
=0; len
<size
; len
++)
2030 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2033 return MSVCRT_EINVAL
;
2036 for(xno
=1; xno
<=6; xno
++)
2037 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2038 return MSVCRT_EINVAL
;
2040 id
= GetCurrentProcessId();
2041 for(xno
=1; xno
<6; xno
++) {
2042 pattern
[len
-xno
] = id
%10 + '0';
2046 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2047 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2052 *MSVCRT__errno() = MSVCRT_EEXIST
;
2053 return MSVCRT_EEXIST
;
2056 /*********************************************************************
2057 * _wmktemp (MSVCRT.@)
2059 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
2062 MSVCRT_wchar_t
*retVal
= pattern
;
2064 MSVCRT_wchar_t letter
= 'a';
2070 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2074 id
= GetCurrentProcessId();
2078 int tempNum
= id
/ 10;
2079 *pattern
-- = id
- (tempNum
* 10) + '0';
2085 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2087 *pattern
= letter
++;
2088 } while(letter
!= '|');
2092 static unsigned split_oflags(unsigned oflags
)
2095 unsigned unsupp
; /* until we support everything */
2097 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2098 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2099 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2100 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2101 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2102 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2103 else if (*MSVCRT___p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2104 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2105 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2107 if ((unsupp
= oflags
& ~(
2108 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2109 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2110 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2111 MSVCRT__O_NOINHERIT
|
2112 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2113 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2115 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2120 /*********************************************************************
2123 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2126 SECURITY_ATTRIBUTES sa
;
2127 HANDLE readHandle
, writeHandle
;
2131 *MSVCRT__errno() = MSVCRT_EINVAL
;
2135 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2136 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2137 sa
.lpSecurityDescriptor
= NULL
;
2138 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2140 unsigned int wxflags
= split_oflags(textmode
);
2143 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2147 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2155 MSVCRT__close(pfds
[0]);
2156 CloseHandle(writeHandle
);
2157 *MSVCRT__errno() = MSVCRT_EMFILE
;
2162 CloseHandle(readHandle
);
2163 CloseHandle(writeHandle
);
2164 *MSVCRT__errno() = MSVCRT_EMFILE
;
2168 msvcrt_set_errno(GetLastError());
2173 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2175 char bom
[sizeof(utf8_bom
)];
2178 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2180 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2183 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2184 oflags
|= MSVCRT__O_U8TEXT
;
2185 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2187 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2188 oflags
|= MSVCRT__O_U16TEXT
;
2190 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2196 /*********************************************************************
2197 * _wsopen_dispatch (UCRTBASE.@)
2199 int CDECL
MSVCRT__wsopen_dispatch( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
,
2200 int *fd
, int secure
)
2202 DWORD access
= 0, creation
= 0, attrib
;
2203 SECURITY_ATTRIBUTES sa
;
2204 DWORD sharing
, type
;
2208 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2209 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2211 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2214 wxflag
= split_oflags(oflags
);
2215 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2217 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2218 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2219 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2222 if (oflags
& MSVCRT__O_CREAT
)
2224 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))))
2225 return MSVCRT_EINVAL
;
2227 if (oflags
& MSVCRT__O_EXCL
)
2228 creation
= CREATE_NEW
;
2229 else if (oflags
& MSVCRT__O_TRUNC
)
2230 creation
= CREATE_ALWAYS
;
2232 creation
= OPEN_ALWAYS
;
2234 else /* no MSVCRT__O_CREAT */
2236 if (oflags
& MSVCRT__O_TRUNC
)
2237 creation
= TRUNCATE_EXISTING
;
2239 creation
= OPEN_EXISTING
;
2244 case MSVCRT__SH_DENYRW
:
2247 case MSVCRT__SH_DENYWR
:
2248 sharing
= FILE_SHARE_READ
;
2250 case MSVCRT__SH_DENYRD
:
2251 sharing
= FILE_SHARE_WRITE
;
2253 case MSVCRT__SH_DENYNO
:
2254 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2257 ERR( "Unhandled shflags 0x%x\n", shflags
);
2258 return MSVCRT_EINVAL
;
2261 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2262 attrib
= FILE_ATTRIBUTE_READONLY
;
2264 attrib
= FILE_ATTRIBUTE_NORMAL
;
2266 if (oflags
& MSVCRT__O_TEMPORARY
)
2268 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2270 sharing
|= FILE_SHARE_DELETE
;
2273 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2274 sa
.lpSecurityDescriptor
= NULL
;
2275 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2277 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2278 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2279 && !(access
&GENERIC_READ
))
2281 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2282 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2283 if (hand
!= INVALID_HANDLE_VALUE
)
2285 oflags
= check_bom(hand
, oflags
, FALSE
);
2289 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2292 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2293 if (hand
== INVALID_HANDLE_VALUE
) {
2294 WARN(":failed-last error (%d)\n",GetLastError());
2295 msvcrt_set_errno(GetLastError());
2296 return *MSVCRT__errno();
2299 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2301 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2302 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2303 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2305 if (oflags
& MSVCRT__O_U8TEXT
)
2307 DWORD written
= 0, tmp
;
2309 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2310 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2312 if (written
!= sizeof(utf8_bom
)) {
2313 WARN("error writing BOM\n");
2315 msvcrt_set_errno(GetLastError());
2316 return *MSVCRT__errno();
2321 DWORD written
= 0, tmp
;
2323 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2324 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2326 if (written
!= sizeof(utf16_bom
))
2328 WARN("error writing BOM\n");
2330 msvcrt_set_errno(GetLastError());
2331 return *MSVCRT__errno();
2335 else if (access
& GENERIC_READ
)
2336 oflags
= check_bom(hand
, oflags
, TRUE
);
2339 type
= GetFileType(hand
);
2340 if (type
== FILE_TYPE_CHAR
)
2342 else if (type
== FILE_TYPE_PIPE
)
2345 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2347 return *MSVCRT__errno();
2349 if (oflags
& MSVCRT__O_WTEXT
)
2350 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2351 else if (oflags
& MSVCRT__O_U16TEXT
)
2352 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2353 else if (oflags
& MSVCRT__O_U8TEXT
)
2354 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2356 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2361 /*********************************************************************
2362 * _wsopen_s (MSVCRT.@)
2364 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2366 return MSVCRT__wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2369 /*********************************************************************
2370 * _wsopen (MSVCRT.@)
2372 int WINAPIV
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2377 if (oflags
& MSVCRT__O_CREAT
)
2381 __ms_va_start(ap
, shflags
);
2382 pmode
= va_arg(ap
, int);
2388 return MSVCRT__wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2392 /*********************************************************************
2393 * _sopen_dispatch (UCRTBASE.@)
2395 int CDECL
MSVCRT__sopen_dispatch( const char *path
, int oflags
, int shflags
,
2396 int pmode
, int *fd
, int secure
)
2398 MSVCRT_wchar_t
*pathW
;
2401 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2402 return MSVCRT_EINVAL
;
2404 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2405 return MSVCRT_EINVAL
;
2407 ret
= MSVCRT__wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2412 /*********************************************************************
2413 * _sopen_s (MSVCRT.@)
2415 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2417 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2420 /*********************************************************************
2423 int WINAPIV
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2428 if (oflags
& MSVCRT__O_CREAT
)
2432 __ms_va_start(ap
, shflags
);
2433 pmode
= va_arg(ap
, int);
2439 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2442 /*********************************************************************
2445 int WINAPIV
MSVCRT__open( const char *path
, int flags
, ... )
2449 if (flags
& MSVCRT__O_CREAT
)
2452 __ms_va_start(ap
, flags
);
2453 pmode
= va_arg(ap
, int);
2455 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2458 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2461 /*********************************************************************
2464 int WINAPIV
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2468 if (flags
& MSVCRT__O_CREAT
)
2471 __ms_va_start(ap
, flags
);
2472 pmode
= va_arg(ap
, int);
2474 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2477 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2480 /*********************************************************************
2483 int CDECL
MSVCRT__creat(const char *path
, int pmode
)
2485 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2486 return MSVCRT__open(path
, flags
, pmode
);
2489 /*********************************************************************
2490 * _wcreat (MSVCRT.@)
2492 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int pmode
)
2494 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2495 return MSVCRT__wopen(path
, flags
, pmode
);
2498 /*********************************************************************
2499 * _open_osfhandle (MSVCRT.@)
2501 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2506 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2507 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2508 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2509 * text - it never sets MSVCRT__O_BINARY.
2511 /* don't let split_oflags() decide the mode if no mode is passed */
2512 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2513 oflags
|= MSVCRT__O_BINARY
;
2515 flags
= GetFileType((HANDLE
)handle
);
2516 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2518 msvcrt_set_errno(GetLastError());
2522 if (flags
== FILE_TYPE_CHAR
)
2524 else if (flags
== FILE_TYPE_PIPE
)
2528 flags
|= split_oflags(oflags
);
2530 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2531 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2535 /*********************************************************************
2538 int CDECL
MSVCRT__rmtmp(void)
2540 int num_removed
= 0, i
;
2544 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2545 file
= msvcrt_get_file(i
);
2547 if (file
->_tmpfname
)
2549 MSVCRT_fclose(file
);
2556 TRACE(":removed (%d) temp files\n",num_removed
);
2560 static inline int get_utf8_char_len(char ch
)
2562 if((ch
&0xf8) == 0xf0)
2564 else if((ch
&0xf0) == 0xe0)
2566 else if((ch
&0xe0) == 0xc0)
2571 /*********************************************************************
2572 * (internal) read_utf8
2574 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2576 HANDLE hand
= fdinfo
->handle
;
2577 char min_buf
[4], *readbuf
, lookahead
;
2578 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2580 /* make the buffer big enough to hold at least one character */
2581 /* read bytes have to fit to output and lookahead buffers */
2583 readbuf_size
= count
< 4 ? 4 : count
;
2584 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2589 if(fdinfo
->lookahead
[0] != '\n') {
2590 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2591 fdinfo
->lookahead
[0] = '\n';
2593 if(fdinfo
->lookahead
[1] != '\n') {
2594 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2595 fdinfo
->lookahead
[1] = '\n';
2597 if(fdinfo
->lookahead
[2] != '\n') {
2598 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2599 fdinfo
->lookahead
[2] = '\n';
2604 /* NOTE: this case is broken in native dll, reading
2605 * sometimes fails when small buffer is passed
2608 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2609 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2610 fdinfo
->wxflag
|= WX_ATEOF
;
2613 msvcrt_set_errno(GetLastError());
2616 }else if(!num_read
) {
2617 fdinfo
->wxflag
|= WX_ATEOF
;
2623 char_len
= get_utf8_char_len(readbuf
[0]);
2625 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2629 if(readbuf
[0] == '\n')
2630 fdinfo
->wxflag
|= WX_READNL
;
2632 fdinfo
->wxflag
&= ~WX_READNL
;
2634 if(readbuf
[0] == 0x1a) {
2635 fdinfo
->wxflag
|= WX_ATEOF
;
2639 if(readbuf
[0] == '\r') {
2640 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2642 else if(lookahead
== '\n')
2646 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2647 fdinfo
->lookahead
[0] = lookahead
;
2649 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2654 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2655 msvcrt_set_errno(GetLastError());
2662 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2665 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2666 fdinfo
->wxflag
|= WX_ATEOF
;
2667 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2670 msvcrt_set_errno(GetLastError());
2671 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2674 }else if(!pos
&& !num_read
) {
2675 fdinfo
->wxflag
|= WX_ATEOF
;
2676 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2681 if(readbuf
[0] == '\n')
2682 fdinfo
->wxflag
|= WX_READNL
;
2684 fdinfo
->wxflag
&= ~WX_READNL
;
2686 /* Find first byte of last character (may be incomplete) */
2687 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2688 if((readbuf
[i
]&0xc0) != 0x80)
2690 char_len
= get_utf8_char_len(readbuf
[i
]);
2691 if(char_len
+i
<= pos
)
2694 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2696 fdinfo
->lookahead
[0] = readbuf
[i
];
2698 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2700 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2702 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2706 for(i
=0, j
=0; i
<pos
; i
++) {
2707 if(readbuf
[i
] == 0x1a) {
2708 fdinfo
->wxflag
|= WX_ATEOF
;
2712 /* strip '\r' if followed by '\n' */
2713 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2714 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2715 readbuf
[j
++] = '\r';
2716 }else if(lookahead
== '\n' && j
==0) {
2717 readbuf
[j
++] = '\n';
2719 if(lookahead
!= '\n')
2720 readbuf
[j
++] = '\r';
2722 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2723 fdinfo
->lookahead
[0] = lookahead
;
2725 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2727 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2728 readbuf
[j
++] = readbuf
[i
];
2733 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2734 msvcrt_set_errno(GetLastError());
2735 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2739 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2743 /*********************************************************************
2746 * When reading \r as last character in text mode, read() positions
2747 * the file pointer on the \r character while getc() goes on to
2750 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2752 DWORD num_read
, utf16
;
2753 char *bufstart
= buf
;
2758 if (fdinfo
->wxflag
& WX_ATEOF
) {
2759 TRACE("already at EOF, returning 0\n");
2762 /* Don't trace small reads, it gets *very* annoying */
2764 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2765 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2767 *MSVCRT__errno() = MSVCRT_EBADF
;
2771 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2772 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2774 *MSVCRT__errno() = MSVCRT_EINVAL
;
2778 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2779 return read_utf8(fdinfo
, buf
, count
);
2781 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2783 if (fdinfo
->lookahead
[0] != '\n')
2785 bufstart
[0] = fdinfo
->lookahead
[0];
2786 fdinfo
->lookahead
[0] = '\n';
2790 bufstart
[1] = fdinfo
->lookahead
[1];
2791 fdinfo
->lookahead
[1] = '\n';
2794 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2795 num_read
+= 1+utf16
;
2800 if(utf16
&& (num_read
&1))
2802 /* msvcr90 uses uninitialized value from the buffer in this case */
2803 /* msvcrt ignores additional data */
2804 ERR("got odd number of bytes in UTF16 mode\n");
2808 if (count
!= 0 && num_read
== 0)
2810 fdinfo
->wxflag
|= WX_ATEOF
;
2811 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2813 else if (fdinfo
->wxflag
& WX_TEXT
)
2817 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2818 fdinfo
->wxflag
|= WX_READNL
;
2820 fdinfo
->wxflag
&= ~WX_READNL
;
2822 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2824 /* in text mode, a ctrl-z signals EOF */
2825 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2827 fdinfo
->wxflag
|= WX_ATEOF
;
2828 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2832 /* in text mode, strip \r if followed by \n */
2833 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2838 lookahead
[1] = '\n';
2839 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2841 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2843 bufstart
[j
++] = '\n';
2844 if(utf16
) bufstart
[j
++] = 0;
2848 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2850 bufstart
[j
++] = '\r';
2851 if(utf16
) bufstart
[j
++] = 0;
2854 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2856 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2858 bufstart
[j
++] = '\n';
2859 if (utf16
) bufstart
[j
++] = 0;
2863 fdinfo
->lookahead
[0] = lookahead
[0];
2864 fdinfo
->lookahead
[1] = lookahead
[1];
2868 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2873 bufstart
[j
++] = '\r';
2874 if(utf16
) bufstart
[j
++] = 0;
2877 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2878 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2880 bufstart
[j
++] = bufstart
[i
];
2881 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2889 if (GetLastError() == ERROR_BROKEN_PIPE
)
2891 TRACE(":end-of-pipe\n");
2892 fdinfo
->wxflag
|= WX_ATEOF
;
2897 TRACE(":failed-last error (%d)\n",GetLastError());
2898 msvcrt_set_errno(GetLastError());
2904 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2908 /*********************************************************************
2911 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2916 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
2917 *MSVCRT__errno() = MSVCRT_EBADF
;
2921 info
= get_ioinfo(fd
);
2922 num_read
= read_i(fd
, info
, buf
, count
);
2923 release_ioinfo(info
);
2927 /*********************************************************************
2928 * _setmode (MSVCRT.@)
2930 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2932 ioinfo
*info
= get_ioinfo(fd
);
2933 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2934 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2935 ret
= MSVCRT__O_WTEXT
;
2937 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2938 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2939 *MSVCRT__errno() = MSVCRT_EINVAL
;
2940 release_ioinfo(info
);
2944 if(info
== &MSVCRT___badioinfo
) {
2945 *MSVCRT__errno() = MSVCRT_EBADF
;
2949 if(mode
== MSVCRT__O_BINARY
) {
2950 info
->wxflag
&= ~WX_TEXT
;
2951 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2952 release_ioinfo(info
);
2956 info
->wxflag
|= WX_TEXT
;
2957 if(mode
== MSVCRT__O_TEXT
)
2958 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2959 else if(mode
== MSVCRT__O_U8TEXT
)
2960 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2962 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2964 release_ioinfo(info
);
2968 /*********************************************************************
2969 * _stat64 (MSVCRT.@)
2971 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2974 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2975 unsigned short mode
= ALL_S_IREAD
;
2978 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2980 plen
= strlen(path
);
2981 while (plen
&& path
[plen
-1]==' ')
2984 if (plen
==2 && path
[1]==':')
2986 *MSVCRT__errno() = MSVCRT_ENOENT
;
2991 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2993 *MSVCRT__errno() = MSVCRT_ENOENT
;
2998 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
3000 TRACE("failed (%d)\n",GetLastError());
3001 *MSVCRT__errno() = MSVCRT_ENOENT
;
3005 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3007 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3008 Bon 011120: This FIXME seems incorrect
3009 Also a letter as first char isn't enough to be classified
3012 if (isalpha(*path
)&& (*(path
+1)==':'))
3013 buf
->st_dev
= buf
->st_rdev
= MSVCRT__toupper_l(*path
, NULL
) - 'A'; /* drive num */
3015 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3017 /* Dir, or regular file? */
3018 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3019 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3022 mode
|= MSVCRT__S_IFREG
;
3024 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3026 unsigned int ext
= MSVCRT__tolower_l(path
[plen
-1], NULL
) |
3027 (MSVCRT__tolower_l(path
[plen
-2], NULL
) << 8) |
3028 (MSVCRT__tolower_l(path
[plen
-3], NULL
) << 16);
3029 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3030 mode
|= ALL_S_IEXEC
;
3034 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3035 mode
|= ALL_S_IWRITE
;
3037 buf
->st_mode
= mode
;
3039 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3040 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3042 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3043 buf
->st_mtime
= buf
->st_ctime
= dw
;
3044 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3045 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3046 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3050 /*********************************************************************
3051 * _stati64 (MSVCRT.@)
3053 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
3056 struct MSVCRT__stat64 buf64
;
3058 ret
= MSVCRT_stat64(path
, &buf64
);
3060 msvcrt_stat64_to_stati64(&buf64
, buf
);
3064 /*********************************************************************
3067 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
3070 struct MSVCRT__stat64 buf64
;
3072 ret
= MSVCRT_stat64( path
, &buf64
);
3074 msvcrt_stat64_to_stat(&buf64
, buf
);
3078 #if _MSVCR_VER >= 80
3080 /*********************************************************************
3081 * _stat32 (MSVCR80.@)
3083 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
3086 struct MSVCRT__stat64 buf64
;
3088 ret
= MSVCRT_stat64(path
, &buf64
);
3090 msvcrt_stat64_to_stat32(&buf64
, buf
);
3094 /*********************************************************************
3095 * _stat32i64 (MSVCR80.@)
3097 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
3100 struct MSVCRT__stat64 buf64
;
3102 ret
= MSVCRT_stat64(path
, &buf64
);
3104 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3108 /*********************************************************************
3109 * _stat64i32 (MSVCR80.@)
3111 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
3114 struct MSVCRT__stat64 buf64
;
3116 ret
= MSVCRT_stat64(path
, &buf64
);
3118 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3122 #endif /* _MSVCR_VER >= 80 */
3124 /*********************************************************************
3125 * _wstat64 (MSVCRT.@)
3127 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
3130 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3131 unsigned short mode
= ALL_S_IREAD
;
3134 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3136 plen
= strlenW(path
);
3137 while (plen
&& path
[plen
-1]==' ')
3140 if (plen
==2 && path
[1]==':')
3142 *MSVCRT__errno() = MSVCRT_ENOENT
;
3147 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3149 *MSVCRT__errno() = MSVCRT_ENOENT
;
3154 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3156 TRACE("failed (%d)\n",GetLastError());
3157 *MSVCRT__errno() = MSVCRT_ENOENT
;
3161 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3163 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3164 if (MSVCRT_iswalpha(*path
) && path
[1] == ':')
3165 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
) - 'A'; /* drive num */
3167 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3169 /* Dir, or regular file? */
3170 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3171 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3174 mode
|= MSVCRT__S_IFREG
;
3176 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3178 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3179 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3180 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3181 mode
|= ALL_S_IEXEC
;
3185 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3186 mode
|= ALL_S_IWRITE
;
3188 buf
->st_mode
= mode
;
3190 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3191 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3193 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3194 buf
->st_mtime
= buf
->st_ctime
= dw
;
3195 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3196 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3197 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3201 /*********************************************************************
3202 * _wstati64 (MSVCRT.@)
3204 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3207 struct MSVCRT__stat64 buf64
;
3209 ret
= MSVCRT__wstat64(path
, &buf64
);
3211 msvcrt_stat64_to_stati64(&buf64
, buf
);
3215 /*********************************************************************
3218 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3221 struct MSVCRT__stat64 buf64
;
3223 ret
= MSVCRT__wstat64( path
, &buf64
);
3224 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3228 #if _MSVCR_VER >= 80
3230 /*********************************************************************
3231 * _wstat32 (MSVCR80.@)
3233 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3236 struct MSVCRT__stat64 buf64
;
3238 ret
= MSVCRT__wstat64(path
, &buf64
);
3240 msvcrt_stat64_to_stat32(&buf64
, buf
);
3244 /*********************************************************************
3245 * _wstat32i64 (MSVCR80.@)
3247 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3250 struct MSVCRT__stat64 buf64
;
3252 ret
= MSVCRT__wstat64(path
, &buf64
);
3254 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3258 /*********************************************************************
3259 * _wstat64i32 (MSVCR80.@)
3261 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3264 struct MSVCRT__stat64 buf64
;
3266 ret
= MSVCRT__wstat64(path
, &buf64
);
3268 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3272 #endif /* _MSVCR_VER >= 80 */
3274 /*********************************************************************
3277 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3279 return MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
3282 /*********************************************************************
3283 * _telli64 (MSVCRT.@)
3285 __int64 CDECL
_telli64(int fd
)
3287 return MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
3290 /*********************************************************************
3291 * _tempnam (MSVCRT.@)
3293 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3295 char tmpbuf
[MAX_PATH
];
3296 const char *tmp_dir
= MSVCRT_getenv("TMP");
3298 if (tmp_dir
) dir
= tmp_dir
;
3300 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3301 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3303 TRACE("got name (%s)\n",tmpbuf
);
3304 DeleteFileA(tmpbuf
);
3305 return MSVCRT__strdup(tmpbuf
);
3307 TRACE("failed (%d)\n",GetLastError());
3311 /*********************************************************************
3312 * _wtempnam (MSVCRT.@)
3314 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3316 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3317 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3318 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3320 if (tmp_dir
) dir
= tmp_dir
;
3322 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3323 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3325 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3326 DeleteFileW(tmpbuf
);
3327 return MSVCRT__wcsdup(tmpbuf
);
3329 TRACE("failed (%d)\n",GetLastError());
3333 /*********************************************************************
3336 int CDECL
MSVCRT__umask(int umask
)
3338 int old_umask
= MSVCRT_umask
;
3339 TRACE("(%d)\n",umask
);
3340 MSVCRT_umask
= umask
;
3344 /*********************************************************************
3345 * _utime64 (MSVCRT.@)
3347 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3349 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3353 int retVal
= _futime64(fd
, t
);
3360 /*********************************************************************
3361 * _utime32 (MSVCRT.@)
3363 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3367 struct MSVCRT___utimbuf64 t64
;
3368 t64
.actime
= t
->actime
;
3369 t64
.modtime
= t
->modtime
;
3370 return _utime64( path
, &t64
);
3373 return _utime64( path
, NULL
);
3376 /*********************************************************************
3380 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3382 return _utime64( path
, t
);
3385 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3387 return _utime32( path
, t
);
3391 /*********************************************************************
3392 * _wutime64 (MSVCRT.@)
3394 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3396 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3400 int retVal
= _futime64(fd
, t
);
3407 /*********************************************************************
3408 * _wutime32 (MSVCRT.@)
3410 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3414 struct MSVCRT___utimbuf64 t64
;
3415 t64
.actime
= t
->actime
;
3416 t64
.modtime
= t
->modtime
;
3417 return _wutime64( path
, &t64
);
3420 return _wutime64( path
, NULL
);
3423 /*********************************************************************
3424 * _wutime (MSVCRT.@)
3427 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3429 return _wutime64( path
, t
);
3432 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3434 return _wutime32( path
, t
);
3438 /*********************************************************************
3441 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3444 ioinfo
*info
= get_ioinfo(fd
);
3445 HANDLE hand
= info
->handle
;
3447 /* Don't trace small writes, it gets *very* annoying */
3450 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3452 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3454 *MSVCRT__errno() = MSVCRT_EBADF
;
3455 release_ioinfo(info
);
3459 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3461 *MSVCRT__errno() = MSVCRT_EINVAL
;
3462 release_ioinfo(info
);
3466 /* If appending, go to EOF */
3467 if (info
->wxflag
& WX_APPEND
)
3468 MSVCRT__lseek(fd
, 0, FILE_END
);
3470 if (!(info
->wxflag
& WX_TEXT
))
3472 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3473 && (num_written
== count
))
3475 release_ioinfo(info
);
3478 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3479 hand
, GetLastError());
3480 msvcrt_set_errno(GetLastError());
3484 unsigned int i
, j
, nr_lf
, size
;
3487 const char *s
= buf
;
3489 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3491 /* find number of \n */
3492 for (nr_lf
=0, i
=0; i
<count
; i
++)
3498 if ((q
= p
= MSVCRT_malloc(size
)))
3500 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3509 FIXME("Malloc failed\n");
3521 else if (info
->exflag
& EF_UTF16
)
3523 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3524 if (s
[i
]=='\n' && s
[i
+1]==0)
3529 if ((q
= p
= MSVCRT_malloc(size
)))
3531 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3533 if (s
[i
]=='\n' && s
[i
+1]==0)
3544 FIXME("Malloc failed\n");
3560 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3561 if (s
[i
]=='\n' && s
[i
+1]==0)
3564 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3566 msvcrt_set_errno(GetLastError());
3568 release_ioinfo(info
);
3572 size
= conv_len
+nr_lf
;
3573 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3575 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3577 if (s
[i
]=='\n' && s
[i
+1]==0)
3585 q
= p
+count
+nr_lf
*2;
3586 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3587 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3591 FIXME("Malloc failed\n");
3598 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3600 release_ioinfo(info
);
3602 if (num_written
!= size
)
3604 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3605 fd
, hand
, GetLastError(), num_written
);
3606 msvcrt_set_errno(GetLastError());
3612 release_ioinfo(info
);
3616 /*********************************************************************
3619 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3623 MSVCRT__lock_file(file
);
3624 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3625 if (len
== sizeof(val
)) {
3626 MSVCRT__unlock_file(file
);
3630 file
->_flag
|= MSVCRT__IOERR
;
3631 MSVCRT__unlock_file(file
);
3635 /*********************************************************************
3638 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3642 MSVCRT__lock_file(file
);
3643 ret
= MSVCRT__fclose_nolock(file
);
3644 MSVCRT__unlock_file(file
);
3649 /*********************************************************************
3650 * _fclose_nolock (MSVCRT.@)
3652 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3656 if(!(file
->_flag
& (MSVCRT__IOREAD
| MSVCRT__IOWRT
| MSVCRT__IORW
)))
3663 MSVCRT_free(file
->_tmpfname
);
3664 file
->_tmpfname
= NULL
;
3665 /* flush stdio buffers */
3666 if(file
->_flag
& MSVCRT__IOWRT
)
3667 MSVCRT__fflush_nolock(file
);
3668 if(file
->_flag
& MSVCRT__IOMYBUF
)
3669 MSVCRT_free(file
->_base
);
3671 r
=MSVCRT__close(file
->_file
);
3674 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3677 /*********************************************************************
3680 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3682 return file
->_flag
& MSVCRT__IOEOF
;
3685 /*********************************************************************
3688 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3690 return file
->_flag
& MSVCRT__IOERR
;
3693 /*********************************************************************
3694 * _filbuf (MSVCRT.@)
3696 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3700 if(file
->_flag
& MSVCRT__IOSTRG
)
3703 /* Allocate buffer if needed */
3704 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3705 msvcrt_alloc_buffer(file
);
3707 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3708 if(file
->_flag
& MSVCRT__IORW
)
3709 file
->_flag
|= MSVCRT__IOREAD
;
3714 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3716 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3717 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3723 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3725 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3731 file
->_ptr
= file
->_base
+1;
3732 c
= *(unsigned char *)file
->_base
;
3737 /*********************************************************************
3740 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3744 MSVCRT__lock_file(file
);
3745 ret
= MSVCRT__fgetc_nolock(file
);
3746 MSVCRT__unlock_file(file
);
3751 /*********************************************************************
3752 * _fgetc_nolock (MSVCRT.@)
3754 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3761 i
= (unsigned char *)file
->_ptr
++;
3764 j
= MSVCRT__filbuf(file
);
3769 /*********************************************************************
3770 * _fgetchar (MSVCRT.@)
3772 int CDECL
MSVCRT__fgetchar(void)
3774 return MSVCRT_fgetc(MSVCRT_stdin
);
3777 /*********************************************************************
3780 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3782 int cc
= MSVCRT_EOF
;
3783 char * buf_start
= s
;
3785 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3786 file
,file
->_file
,s
,size
);
3788 MSVCRT__lock_file(file
);
3790 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3795 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3797 TRACE(":nothing read\n");
3798 MSVCRT__unlock_file(file
);
3801 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3804 TRACE(":got %s\n", debugstr_a(buf_start
));
3805 MSVCRT__unlock_file(file
);
3809 /*********************************************************************
3812 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3816 MSVCRT__lock_file(file
);
3817 ret
= MSVCRT__fgetwc_nolock(file
);
3818 MSVCRT__unlock_file(file
);
3823 /*********************************************************************
3824 * _fgetwc_nolock (MSVCRT.@)
3826 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3831 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3832 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3835 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3836 ch
= MSVCRT__fgetc_nolock(file
);
3837 if(ch
== MSVCRT_EOF
) {
3844 char mbs
[MSVCRT_MB_LEN_MAX
];
3847 ch
= MSVCRT__fgetc_nolock(file
);
3848 if(ch
!= MSVCRT_EOF
) {
3850 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3851 ch
= MSVCRT__fgetc_nolock(file
);
3852 if(ch
!= MSVCRT_EOF
) {
3861 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3868 /*********************************************************************
3871 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3878 MSVCRT__lock_file(file
);
3879 for (j
=0; j
<sizeof(int); j
++) {
3880 k
= MSVCRT__fgetc_nolock(file
);
3881 if (k
== MSVCRT_EOF
) {
3882 file
->_flag
|= MSVCRT__IOEOF
;
3883 MSVCRT__unlock_file(file
);
3889 MSVCRT__unlock_file(file
);
3893 /*********************************************************************
3896 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3898 return MSVCRT_fgetwc(file
);
3901 /*********************************************************************
3902 * _fgetwchar (MSVCRT.@)
3904 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3906 return MSVCRT_fgetwc(MSVCRT_stdin
);
3909 /*********************************************************************
3910 * getwchar (MSVCRT.@)
3912 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3914 return MSVCRT__fgetwchar();
3917 /*********************************************************************
3920 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3922 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3923 MSVCRT_wchar_t
* buf_start
= s
;
3925 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3926 file
,file
->_file
,s
,size
);
3928 MSVCRT__lock_file(file
);
3930 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3935 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3937 TRACE(":nothing read\n");
3938 MSVCRT__unlock_file(file
);
3941 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3944 TRACE(":got %s\n", debugstr_w(buf_start
));
3945 MSVCRT__unlock_file(file
);
3949 /*********************************************************************
3950 * _flsbuf (MSVCRT.@)
3952 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3954 /* Flush output buffer */
3955 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3956 msvcrt_alloc_buffer(file
);
3959 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3960 if(!(file
->_flag
& MSVCRT__IORW
)) {
3961 file
->_flag
|= MSVCRT__IOERR
;
3962 *MSVCRT__errno() = MSVCRT_EBADF
;
3965 file
->_flag
|= MSVCRT__IOWRT
;
3967 if(file
->_flag
& MSVCRT__IOREAD
) {
3968 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3969 file
->_flag
|= MSVCRT__IOERR
;
3973 file
->_ptr
= file
->_base
;
3974 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3977 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3980 if(file
->_cnt
<= 0) {
3981 res
= msvcrt_flush_buffer(file
);
3984 file
->_flag
|= MSVCRT__IOWRT
;
3985 file
->_cnt
=file
->_bufsiz
;
3993 /* set _cnt to 0 for unbuffered FILEs */
3995 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3998 file
->_flag
|= MSVCRT__IOERR
;
4003 /*********************************************************************
4006 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4010 MSVCRT__lock_file(file
);
4011 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
4012 MSVCRT__unlock_file(file
);
4017 /*********************************************************************
4018 * _fwrite_nolock (MSVCRT.@)
4020 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4022 MSVCRT_size_t wrcnt
=size
* nmemb
;
4028 if(file
->_cnt
< 0) {
4029 WARN("negative file->_cnt value in %p\n", file
);
4030 file
->_flag
|= MSVCRT__IOERR
;
4032 } else if(file
->_cnt
) {
4033 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
4034 memcpy(file
->_ptr
, ptr
, pcnt
);
4039 ptr
= (const char*)ptr
+ pcnt
;
4040 } else if((file
->_flag
& MSVCRT__IONBF
)
4041 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4042 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4046 if(file
->_flag
& MSVCRT__IONBF
)
4048 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4049 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4051 bufsiz
= file
->_bufsiz
;
4053 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4055 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
4058 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
4059 file
->_flag
|= MSVCRT__IOERR
;
4064 ptr
= (const char*)ptr
+ pcnt
;
4066 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
4070 ptr
= (const char*)ptr
+ 1;
4074 return written
/ size
;
4077 /*********************************************************************
4080 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4084 MSVCRT__lock_file(file
);
4085 ret
= MSVCRT__fputwc_nolock(wc
, file
);
4086 MSVCRT__unlock_file(file
);
4091 /*********************************************************************
4092 * _fputwc_nolock (MSVCRT.@)
4094 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4096 MSVCRT_wchar_t mwc
=wc
;
4100 fdinfo
= get_ioinfo_nolock(file
->_file
);
4102 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
4103 char buf
[MSVCRT_MB_LEN_MAX
];
4106 char_len
= MSVCRT_wctomb(buf
, mwc
);
4107 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
4111 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4120 /*********************************************************************
4121 * _fputwchar (MSVCRT.@)
4123 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
4125 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
4128 /*********************************************************************
4129 * _wfsopen (MSVCRT.@)
4131 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
4134 int open_flags
, stream_flags
, fd
;
4136 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4138 /* map mode string to open() flags. "man fopen" for possibilities. */
4139 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4143 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4146 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4148 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4155 TRACE(":got (%p)\n",file
);
4156 if (fd
>= 0 && !file
)
4162 /*********************************************************************
4163 * _fsopen (MSVCRT.@)
4165 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4168 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4170 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4171 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4172 *MSVCRT__errno() = MSVCRT_EINVAL
;
4175 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4178 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4179 *MSVCRT__errno() = MSVCRT_EINVAL
;
4183 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4190 /*********************************************************************
4193 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4195 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4198 /*********************************************************************
4199 * fopen_s (MSVCRT.@)
4201 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4202 const char *filename
, const char *mode
)
4204 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4205 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4206 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4208 *pFile
= MSVCRT_fopen(filename
, mode
);
4211 return *MSVCRT__errno();
4215 /*********************************************************************
4216 * _wfopen (MSVCRT.@)
4218 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4220 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4223 /*********************************************************************
4224 * _wfopen_s (MSVCRT.@)
4226 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4227 const MSVCRT_wchar_t
*mode
)
4229 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4230 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4231 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4233 *pFile
= MSVCRT__wfopen(filename
, mode
);
4236 return *MSVCRT__errno();
4240 /*********************************************************************
4243 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4247 MSVCRT__lock_file(file
);
4248 ret
= MSVCRT__fputc_nolock(c
, file
);
4249 MSVCRT__unlock_file(file
);
4254 /*********************************************************************
4255 * _fputc_nolock (MSVCRT.@)
4257 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4266 res
= msvcrt_flush_buffer(file
);
4267 return res
? res
: c
;
4273 res
= MSVCRT__flsbuf(c
, file
);
4278 /*********************************************************************
4279 * _fputchar (MSVCRT.@)
4281 int CDECL
MSVCRT__fputchar(int c
)
4283 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4286 /*********************************************************************
4289 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4293 MSVCRT__lock_file(file
);
4294 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4295 MSVCRT__unlock_file(file
);
4300 /*********************************************************************
4301 * _fread_nolock (MSVCRT.@)
4303 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4305 MSVCRT_size_t rcnt
=size
* nmemb
;
4306 MSVCRT_size_t read
=0;
4307 MSVCRT_size_t pread
=0;
4312 /* first buffered data */
4314 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4315 memcpy(ptr
, file
->_ptr
, pcnt
);
4320 ptr
= (char*)ptr
+ pcnt
;
4321 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4322 if(file
->_flag
& MSVCRT__IORW
) {
4323 file
->_flag
|= MSVCRT__IOREAD
;
4329 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4330 msvcrt_alloc_buffer(file
);
4335 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4336 i
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4337 file
->_ptr
= file
->_base
;
4340 if (i
> rcnt
) i
= rcnt
;
4342 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4343 if (i
> 0 && i
< file
->_cnt
) {
4344 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4345 file
->_flag
&= ~MSVCRT__IOEOF
;
4348 memcpy(ptr
, file
->_ptr
, i
);
4352 } else if (rcnt
> INT_MAX
) {
4353 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4354 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4355 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4357 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4361 ptr
= (char *)ptr
+i
;
4362 /* expose feof condition in the flags
4363 * MFC tests file->_flag for feof, and doesn't call feof())
4365 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4366 file
->_flag
|= MSVCRT__IOEOF
;
4369 file
->_flag
|= MSVCRT__IOERR
;
4379 #if _MSVCR_VER >= 80
4381 /*********************************************************************
4382 * fread_s (MSVCR80.@)
4384 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4385 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4389 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4391 memset(buf
, 0, buf_size
);
4394 if(!elem_size
|| !count
) return 0;
4396 MSVCRT__lock_file(stream
);
4397 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4398 MSVCRT__unlock_file(stream
);
4403 /*********************************************************************
4404 * _fread_nolock_s (MSVCR80.@)
4406 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4407 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4409 size_t bytes_left
, buf_pos
;
4411 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4413 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4415 memset(buf
, 0, buf_size
);
4418 if(!elem_size
|| !count
) return 0;
4419 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4420 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4422 bytes_left
= elem_size
*count
;
4425 if(stream
->_cnt
> 0) {
4426 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4428 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4429 memset(buf
, 0, buf_size
);
4433 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4437 int c
= MSVCRT__filbuf(stream
);
4442 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4443 memset(buf
, 0, buf_size
);
4447 ((char*)buf
)[buf_pos
++] = c
;
4452 return buf_pos
/elem_size
;
4455 #endif /* _MSVCR_VER >= 80 */
4457 /*********************************************************************
4458 * _wfreopen (MSVCRT.@)
4461 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4463 int open_flags
, stream_flags
, fd
;
4465 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4468 if (!file
|| ((fd
= file
->_file
) < 0))
4472 MSVCRT_fclose(file
);
4473 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4475 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4477 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4487 /*********************************************************************
4488 * _wfreopen_s (MSVCRT.@)
4490 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4491 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4493 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4494 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4495 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4496 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4498 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4501 return *MSVCRT__errno();
4505 /*********************************************************************
4506 * freopen (MSVCRT.@)
4509 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4512 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4514 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4515 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4521 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4528 /*********************************************************************
4529 * freopen_s (MSVCRT.@)
4531 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4532 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4534 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4535 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4536 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4537 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4539 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4542 return *MSVCRT__errno();
4546 /*********************************************************************
4547 * fsetpos (MSVCRT.@)
4549 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4553 MSVCRT__lock_file(file
);
4554 msvcrt_flush_buffer(file
);
4556 /* Reset direction of i/o */
4557 if(file
->_flag
& MSVCRT__IORW
) {
4558 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4561 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,MSVCRT_SEEK_SET
) == -1) ? -1 : 0;
4562 MSVCRT__unlock_file(file
);
4566 /*********************************************************************
4567 * _ftelli64 (MSVCRT.@)
4569 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4573 MSVCRT__lock_file(file
);
4574 ret
= MSVCRT__ftelli64_nolock(file
);
4575 MSVCRT__unlock_file(file
);
4580 /*********************************************************************
4581 * _ftelli64_nolock (MSVCRT.@)
4583 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4587 pos
= _telli64(file
->_file
);
4590 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4591 if(file
->_flag
& MSVCRT__IOWRT
) {
4592 pos
+= file
->_ptr
- file
->_base
;
4594 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4597 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4601 } else if(!file
->_cnt
) { /* nothing to do */
4602 } else if(MSVCRT__lseeki64(file
->_file
, 0, MSVCRT_SEEK_END
)==pos
) {
4606 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4607 for(i
=0; i
<file
->_cnt
; i
++)
4608 if(file
->_ptr
[i
] == '\n')
4614 if(MSVCRT__lseeki64(file
->_file
, pos
, MSVCRT_SEEK_SET
) != pos
)
4617 pos
-= file
->_bufsiz
;
4618 pos
+= file
->_ptr
- file
->_base
;
4620 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4621 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4624 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4634 /*********************************************************************
4637 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4639 return MSVCRT__ftelli64(file
);
4642 #if _MSVCR_VER >= 80
4643 /*********************************************************************
4644 * _ftell_nolock (MSVCR80.@)
4646 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4648 return MSVCRT__ftelli64_nolock(file
);
4652 /*********************************************************************
4653 * fgetpos (MSVCRT.@)
4655 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4657 *pos
= MSVCRT__ftelli64(file
);
4663 /*********************************************************************
4666 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4668 MSVCRT_size_t len
= strlen(s
);
4671 MSVCRT__lock_file(file
);
4672 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4673 MSVCRT__unlock_file(file
);
4677 /*********************************************************************
4680 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4682 MSVCRT_size_t i
, len
= strlenW(s
);
4686 MSVCRT__lock_file(file
);
4687 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4688 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4689 MSVCRT__unlock_file(file
);
4693 tmp_buf
= add_std_buffer(file
);
4694 for (i
=0; i
<len
; i
++) {
4695 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4696 if(tmp_buf
) remove_std_buffer(file
);
4697 MSVCRT__unlock_file(file
);
4702 if(tmp_buf
) remove_std_buffer(file
);
4703 MSVCRT__unlock_file(file
);
4707 /*********************************************************************
4708 * getchar (MSVCRT.@)
4710 int CDECL
MSVCRT_getchar(void)
4712 return MSVCRT_fgetc(MSVCRT_stdin
);
4715 /*********************************************************************
4718 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4720 return MSVCRT_fgetc(file
);
4723 /*********************************************************************
4724 * gets_s (MSVCR80.@)
4726 char * CDECL
MSVCRT_gets_s(char *buf
, MSVCRT_size_t len
)
4728 char *buf_start
= buf
;
4731 if (!MSVCRT_CHECK_PMT(buf
!= NULL
)) return NULL
;
4732 if (!MSVCRT_CHECK_PMT(len
!= 0)) return NULL
;
4734 MSVCRT__lock_file(MSVCRT_stdin
);
4735 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
);
4736 len
!= 0 && cc
!= MSVCRT_EOF
&& cc
!= '\n';
4737 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4745 MSVCRT__unlock_file(MSVCRT_stdin
);
4750 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4754 if ((cc
== MSVCRT_EOF
) && (buf_start
== buf
))
4756 TRACE(":nothing read\n");
4761 TRACE("got '%s'\n", buf_start
);
4765 /*********************************************************************
4768 char * CDECL
MSVCRT_gets(char *buf
)
4770 return MSVCRT_gets_s(buf
, -1);
4773 /*********************************************************************
4776 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4779 MSVCRT_wchar_t
* ws
= buf
;
4781 MSVCRT__lock_file(MSVCRT_stdin
);
4782 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4783 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4786 *buf
++ = (MSVCRT_wchar_t
)cc
;
4788 MSVCRT__unlock_file(MSVCRT_stdin
);
4790 if ((cc
== MSVCRT_WEOF
) && (ws
== buf
))
4792 TRACE(":nothing read\n");
4797 TRACE("got %s\n", debugstr_w(ws
));
4801 /*********************************************************************
4804 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4806 return MSVCRT_fputc(c
, file
);
4809 /*********************************************************************
4810 * putchar (MSVCRT.@)
4812 int CDECL
MSVCRT_putchar(int c
)
4814 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4817 /*********************************************************************
4820 int CDECL
MSVCRT_puts(const char *s
)
4822 MSVCRT_size_t len
= strlen(s
);
4825 MSVCRT__lock_file(MSVCRT_stdout
);
4826 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4827 MSVCRT__unlock_file(MSVCRT_stdout
);
4831 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4832 MSVCRT__unlock_file(MSVCRT_stdout
);
4836 /*********************************************************************
4839 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4841 static const MSVCRT_wchar_t nl
= '\n';
4842 MSVCRT_size_t len
= strlenW(s
);
4845 MSVCRT__lock_file(MSVCRT_stdout
);
4846 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4847 MSVCRT__unlock_file(MSVCRT_stdout
);
4851 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4852 MSVCRT__unlock_file(MSVCRT_stdout
);
4856 /*********************************************************************
4859 int CDECL
MSVCRT_remove(const char *path
)
4861 TRACE("(%s)\n",path
);
4862 if (DeleteFileA(path
))
4864 TRACE(":failed (%d)\n",GetLastError());
4865 msvcrt_set_errno(GetLastError());
4869 /*********************************************************************
4870 * _wremove (MSVCRT.@)
4872 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4874 TRACE("(%s)\n",debugstr_w(path
));
4875 if (DeleteFileW(path
))
4877 TRACE(":failed (%d)\n",GetLastError());
4878 msvcrt_set_errno(GetLastError());
4882 /*********************************************************************
4885 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4887 TRACE(":from %s to %s\n",oldpath
,newpath
);
4888 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4890 TRACE(":failed (%d)\n",GetLastError());
4891 msvcrt_set_errno(GetLastError());
4895 /*********************************************************************
4896 * _wrename (MSVCRT.@)
4898 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4900 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4901 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4903 TRACE(":failed (%d)\n",GetLastError());
4904 msvcrt_set_errno(GetLastError());
4908 /*********************************************************************
4909 * setvbuf (MSVCRT.@)
4911 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4913 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4914 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4915 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4917 MSVCRT__lock_file(file
);
4919 MSVCRT__fflush_nolock(file
);
4920 if(file
->_flag
& MSVCRT__IOMYBUF
)
4921 MSVCRT_free(file
->_base
);
4922 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4925 if(mode
== MSVCRT__IONBF
) {
4926 file
->_flag
|= MSVCRT__IONBF
;
4927 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4930 file
->_base
= file
->_ptr
= buf
;
4931 file
->_flag
|= MSVCRT__USERBUF
;
4932 file
->_bufsiz
= size
;
4934 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4937 MSVCRT__unlock_file(file
);
4941 file
->_flag
|= MSVCRT__IOMYBUF
;
4942 file
->_bufsiz
= size
;
4944 MSVCRT__unlock_file(file
);
4948 /*********************************************************************
4951 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4953 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4956 static int tmpnam_helper(char *s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
4962 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
4966 *MSVCRT__errno() = MSVCRT_ERANGE
;
4967 return MSVCRT_ERANGE
;
4972 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4973 if (digits
+1 > size
) {
4975 *MSVCRT__errno() = MSVCRT_ERANGE
;
4976 return MSVCRT_ERANGE
;
4978 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4984 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4985 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4989 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4990 if (digits
+1 > size
) {
4992 *MSVCRT__errno() = MSVCRT_ERANGE
;
4993 return MSVCRT_ERANGE
;
4995 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4998 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4999 GetLastError() == ERROR_FILE_NOT_FOUND
)
5005 int CDECL
MSVCRT_tmpnam_s(char *s
, MSVCRT_size_t size
)
5007 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5010 /*********************************************************************
5013 char * CDECL
MSVCRT_tmpnam(char *s
)
5016 thread_data_t
*data
= msvcrt_get_thread_data();
5018 if(!data
->tmpnam_buffer
)
5019 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
5021 s
= data
->tmpnam_buffer
;
5024 return tmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5027 static int wtmpnam_helper(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
5029 MSVCRT_wchar_t tmpstr
[8];
5030 MSVCRT_wchar_t
*p
= s
;
5033 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
5037 *MSVCRT__errno() = MSVCRT_ERANGE
;
5038 return MSVCRT_ERANGE
;
5043 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5044 if (digits
+1 > size
) {
5046 *MSVCRT__errno() = MSVCRT_ERANGE
;
5047 return MSVCRT_ERANGE
;
5049 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5055 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5056 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5060 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5061 if (digits
+1 > size
) {
5063 *MSVCRT__errno() = MSVCRT_ERANGE
;
5064 return MSVCRT_ERANGE
;
5066 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5069 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5070 GetLastError() == ERROR_FILE_NOT_FOUND
)
5076 /*********************************************************************
5077 * _wtmpnam_s (MSVCRT.@)
5079 int CDECL
MSVCRT__wtmpnam_s(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
)
5081 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5084 /*********************************************************************
5085 * _wtmpnam (MSVCRT.@)
5087 MSVCRT_wchar_t
* CDECL
MSVCRT__wtmpnam(MSVCRT_wchar_t
*s
)
5090 thread_data_t
*data
= msvcrt_get_thread_data();
5092 if(!data
->wtmpnam_buffer
)
5093 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
5095 s
= data
->wtmpnam_buffer
;
5098 return wtmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5101 /*********************************************************************
5102 * tmpfile (MSVCRT.@)
5104 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
5106 char *filename
= MSVCRT__tempnam(",", "t");
5108 MSVCRT_FILE
* file
= NULL
;
5111 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
5112 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
5113 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5115 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
5120 else file
->_tmpfname
= MSVCRT__strdup(filename
);
5123 if(fd
!= -1 && !file
)
5125 MSVCRT_free(filename
);
5130 /*********************************************************************
5131 * tmpfile_s (MSVCRT.@)
5133 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
5135 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
5137 *file
= MSVCRT_tmpfile();
5141 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5143 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
5146 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
5150 MSVCRT__lock_file(file
);
5152 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5153 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
5154 MSVCRT__unlock_file(file
);
5158 for(i
=0; i
<len
; i
++) {
5159 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
5160 MSVCRT__unlock_file(file
);
5165 MSVCRT__unlock_file(file
);
5169 static int vfprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const char *format
,
5170 MSVCRT__locale_t locale
, __ms_va_list valist
)
5172 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5176 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5177 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5179 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5180 memset(args_ctx
, 0, sizeof(args_ctx
));
5181 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5183 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5184 *MSVCRT__errno() = MSVCRT_EINVAL
;
5187 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5190 MSVCRT__lock_file(file
);
5191 tmp_buf
= add_std_buffer(file
);
5192 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5193 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5194 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5195 if(tmp_buf
) remove_std_buffer(file
);
5196 MSVCRT__unlock_file(file
);
5201 static int vfwprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5202 MSVCRT__locale_t locale
, __ms_va_list valist
)
5204 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5208 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5209 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5211 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5212 memset(args_ctx
, 0, sizeof(args_ctx
));
5213 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5215 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5216 *MSVCRT__errno() = MSVCRT_EINVAL
;
5219 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5222 MSVCRT__lock_file(file
);
5223 tmp_buf
= add_std_buffer(file
);
5224 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5225 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5226 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5227 if(tmp_buf
) remove_std_buffer(file
);
5228 MSVCRT__unlock_file(file
);
5233 /*********************************************************************
5234 * _vfprintf_s_l (MSVCRT.@)
5236 int CDECL
MSVCRT__vfprintf_s_l(MSVCRT_FILE
* file
, const char *format
,
5237 MSVCRT__locale_t locale
, __ms_va_list valist
)
5239 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5242 /*********************************************************************
5243 * _vfwprintf_s_l (MSVCRT.@)
5245 int CDECL
MSVCRT__vfwprintf_s_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5246 MSVCRT__locale_t locale
, __ms_va_list valist
)
5248 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5251 /*********************************************************************
5252 * vfprintf (MSVCRT.@)
5254 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5256 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5259 /*********************************************************************
5260 * vfprintf_s (MSVCRT.@)
5262 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5264 return MSVCRT__vfprintf_s_l(file
, format
, NULL
, valist
);
5267 /*********************************************************************
5268 * vfwprintf (MSVCRT.@)
5270 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5272 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5275 /*********************************************************************
5276 * vfwprintf_s (MSVCRT.@)
5278 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5280 return MSVCRT__vfwprintf_s_l(file
, format
, NULL
, valist
);
5283 #if _MSVCR_VER >= 140
5285 /*********************************************************************
5286 * __stdio_common_vfprintf (UCRTBASE.@)
5288 int CDECL
MSVCRT__stdio_common_vfprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5289 MSVCRT__locale_t locale
, __ms_va_list valist
)
5291 if (options
& ~UCRTBASE_PRINTF_MASK
)
5292 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5294 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5297 /*********************************************************************
5298 * __stdio_common_vfprintf_s (UCRTBASE.@)
5300 int CDECL
MSVCRT__stdio_common_vfprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5301 MSVCRT__locale_t locale
, __ms_va_list valist
)
5303 if (options
& ~UCRTBASE_PRINTF_MASK
)
5304 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5306 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5307 file
, format
, locale
, valist
);
5310 /*********************************************************************
5311 * __stdio_common_vfwprintf (UCRTBASE.@)
5313 int CDECL
MSVCRT__stdio_common_vfwprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5314 MSVCRT__locale_t locale
, __ms_va_list valist
)
5316 if (options
& ~UCRTBASE_PRINTF_MASK
)
5317 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5319 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5322 /*********************************************************************
5323 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5325 int CDECL
MSVCRT__stdio_common_vfwprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5326 MSVCRT__locale_t locale
, __ms_va_list valist
)
5328 if (options
& ~UCRTBASE_PRINTF_MASK
)
5329 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5331 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5332 file
, format
, locale
, valist
);
5335 #endif /* _MSVCR_VER >= 140 */
5337 /*********************************************************************
5338 * _vfprintf_l (MSVCRT.@)
5340 int CDECL
MSVCRT__vfprintf_l(MSVCRT_FILE
* file
, const char *format
,
5341 MSVCRT__locale_t locale
, __ms_va_list valist
)
5343 return vfprintf_helper(0, file
, format
, locale
, valist
);
5346 /*********************************************************************
5347 * _vfwprintf_l (MSVCRT.@)
5349 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5350 MSVCRT__locale_t locale
, __ms_va_list valist
)
5352 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5355 /*********************************************************************
5356 * _vfprintf_p_l (MSVCRT.@)
5358 int CDECL
MSVCRT__vfprintf_p_l(MSVCRT_FILE
* file
, const char *format
,
5359 MSVCRT__locale_t locale
, __ms_va_list valist
)
5361 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5362 file
, format
, locale
, valist
);
5365 /*********************************************************************
5366 * _vfprintf_p (MSVCRT.@)
5368 int CDECL
MSVCRT__vfprintf_p(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5370 return MSVCRT__vfprintf_p_l(file
, format
, NULL
, valist
);
5373 /*********************************************************************
5374 * _vfwprintf_p_l (MSVCRT.@)
5376 int CDECL
MSVCRT__vfwprintf_p_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5377 MSVCRT__locale_t locale
, __ms_va_list valist
)
5379 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5380 file
, format
, locale
, valist
);
5383 /*********************************************************************
5384 * _vfwprintf_p (MSVCRT.@)
5386 int CDECL
MSVCRT__vfwprintf_p(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5388 return MSVCRT__vfwprintf_p_l(file
, format
, NULL
, valist
);
5391 /*********************************************************************
5392 * vprintf (MSVCRT.@)
5394 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5396 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5399 /*********************************************************************
5400 * vprintf_s (MSVCRT.@)
5402 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5404 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5407 /*********************************************************************
5408 * vwprintf (MSVCRT.@)
5410 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5412 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5415 /*********************************************************************
5416 * vwprintf_s (MSVCRT.@)
5418 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5420 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5423 /*********************************************************************
5424 * fprintf (MSVCRT.@)
5426 int WINAPIV
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5428 __ms_va_list valist
;
5430 __ms_va_start(valist
, format
);
5431 res
= MSVCRT_vfprintf(file
, format
, valist
);
5432 __ms_va_end(valist
);
5436 /*********************************************************************
5437 * fprintf_s (MSVCRT.@)
5439 int WINAPIV
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5441 __ms_va_list valist
;
5443 __ms_va_start(valist
, format
);
5444 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5445 __ms_va_end(valist
);
5449 /*********************************************************************
5450 * fwprintf (MSVCRT.@)
5452 int WINAPIV
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5454 __ms_va_list valist
;
5456 __ms_va_start(valist
, format
);
5457 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5458 __ms_va_end(valist
);
5462 /*********************************************************************
5463 * fwprintf_s (MSVCRT.@)
5465 int WINAPIV
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5467 __ms_va_list valist
;
5469 __ms_va_start(valist
, format
);
5470 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5471 __ms_va_end(valist
);
5475 /*********************************************************************
5476 * _fwprintf_l (MSVCRT.@)
5478 int WINAPIV
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5480 __ms_va_list valist
;
5482 __ms_va_start(valist
, locale
);
5483 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5484 __ms_va_end(valist
);
5488 /*********************************************************************
5491 int WINAPIV
MSVCRT_printf(const char *format
, ...)
5493 __ms_va_list valist
;
5495 __ms_va_start(valist
, format
);
5496 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5497 __ms_va_end(valist
);
5501 /*********************************************************************
5502 * printf_s (MSVCRT.@)
5504 int WINAPIV
MSVCRT_printf_s(const char *format
, ...)
5506 __ms_va_list valist
;
5508 __ms_va_start(valist
, format
);
5509 res
= MSVCRT_vprintf_s(format
, valist
);
5510 __ms_va_end(valist
);
5514 /*********************************************************************
5517 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5521 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5523 MSVCRT__lock_file(file
);
5524 ret
= MSVCRT__ungetc_nolock(c
, file
);
5525 MSVCRT__unlock_file(file
);
5530 /*********************************************************************
5531 * _ungetc_nolock (MSVCRT.@)
5533 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5535 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5537 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5538 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5541 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5542 && msvcrt_alloc_buffer(file
))
5543 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5546 if(file
->_ptr
>file
->_base
) {
5548 if(file
->_flag
& MSVCRT__IOSTRG
) {
5549 if(*file
->_ptr
!= c
) {
5557 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5558 file
->_flag
|= MSVCRT__IOREAD
;
5565 /*********************************************************************
5566 * ungetwc (MSVCRT.@)
5568 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5572 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5574 MSVCRT__lock_file(file
);
5575 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5576 MSVCRT__unlock_file(file
);
5581 /*********************************************************************
5582 * _ungetwc_nolock (MSVCRT.@)
5584 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5586 MSVCRT_wchar_t mwc
= wc
;
5588 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5589 if (wc
== MSVCRT_WEOF
)
5592 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5593 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5594 unsigned char * pp
= (unsigned char *)&mwc
;
5597 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5598 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5602 char mbs
[MSVCRT_MB_LEN_MAX
];
5605 len
= MSVCRT_wctomb(mbs
, mwc
);
5609 for(len
--; len
>=0; len
--) {
5610 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5618 /*********************************************************************
5619 * wprintf (MSVCRT.@)
5621 int WINAPIV
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5623 __ms_va_list valist
;
5625 __ms_va_start(valist
, format
);
5626 res
= MSVCRT_vwprintf(format
, valist
);
5627 __ms_va_end(valist
);
5631 /*********************************************************************
5632 * wprintf_s (MSVCRT.@)
5634 int WINAPIV
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5636 __ms_va_list valist
;
5638 __ms_va_start(valist
, format
);
5639 res
= MSVCRT_vwprintf_s(format
, valist
);
5640 __ms_va_end(valist
);
5644 /*********************************************************************
5645 * _getmaxstdio (MSVCRT.@)
5647 int CDECL
MSVCRT__getmaxstdio(void)
5649 return MSVCRT_max_streams
;
5652 /*********************************************************************
5653 * _setmaxstdio (MSVCRT.@)
5655 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5657 TRACE("%d\n", newmax
);
5659 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5662 MSVCRT_max_streams
= newmax
;
5663 return MSVCRT_max_streams
;
5666 #if _MSVCR_VER >= 140
5667 /*********************************************************************
5668 * _get_stream_buffer_pointers (UCRTBASE.@)
5670 int CDECL
MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE
*file
, char*** base
,
5671 char*** ptr
, int** count
)
5674 *base
= &file
->_base
;
5678 *count
= &file
->_cnt
;