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 #define __iob_func mingw___iob_func
33 #include <sys/types.h>
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
48 /* for stat mode, permissions apply to all,owner and group */
49 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
50 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
51 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
53 /* _access() bit flags FIXME: incomplete */
54 #define MSVCRT_W_OK 0x02
55 #define MSVCRT_R_OK 0x04
57 /* values for wxflag in file descriptor */
60 #define WX_READNL 0x04 /* read started with \n */
62 #define WX_DONTINHERIT 0x10
63 #define WX_APPEND 0x20
67 /* values for exflag - it's used differently in msvcr90.dll*/
70 #define EF_CRIT_INIT 0x04
71 #define EF_UNK_UNICODE 0x08
73 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
74 static char utf16_bom
[2] = { 0xff, 0xfe };
76 /* FIXME: this should be allocated dynamically */
77 #define MSVCRT_MAX_FILES 2048
78 #define MSVCRT_FD_BLOCK_SIZE 32
80 #define MSVCRT_INTERNAL_BUFSIZ 4096
82 /* ioinfo structure size is different in msvcrXX.dll's */
88 CRITICAL_SECTION crit
;
94 BOOL utf8translations
;
102 /*********************************************************************
103 * __pioinfo (MSVCRT.@)
104 * array of pointers to ioinfo arrays [32]
106 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
108 /*********************************************************************
109 * __badioinfo (MSVCRT.@)
111 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
115 CRITICAL_SECTION crit
;
118 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
119 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
120 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
122 /* INTERNAL: process umask */
123 static int MSVCRT_umask
= 0;
125 /* INTERNAL: static data for tmpnam and _wtmpname functions */
126 static int tmpnam_unique
;
127 static int tmpnam_s_unique
;
129 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
130 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
131 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
132 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
134 #define TOUL(x) (ULONGLONG)(x)
135 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
136 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
137 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
138 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
140 /* This critical section protects the MSVCRT_fstreams table
141 * and MSVCRT_stream_idx from race conditions. It also
142 * protects fd critical sections creation code.
144 static CRITICAL_SECTION MSVCRT_file_cs
;
145 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
147 0, 0, &MSVCRT_file_cs
,
148 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
149 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
151 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
152 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
153 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
155 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
157 buf
->st_dev
= buf64
->st_dev
;
158 buf
->st_ino
= buf64
->st_ino
;
159 buf
->st_mode
= buf64
->st_mode
;
160 buf
->st_nlink
= buf64
->st_nlink
;
161 buf
->st_uid
= buf64
->st_uid
;
162 buf
->st_gid
= buf64
->st_gid
;
163 buf
->st_rdev
= buf64
->st_rdev
;
164 buf
->st_size
= buf64
->st_size
;
165 buf
->st_atime
= buf64
->st_atime
;
166 buf
->st_mtime
= buf64
->st_mtime
;
167 buf
->st_ctime
= buf64
->st_ctime
;
170 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
172 buf
->st_dev
= buf64
->st_dev
;
173 buf
->st_ino
= buf64
->st_ino
;
174 buf
->st_mode
= buf64
->st_mode
;
175 buf
->st_nlink
= buf64
->st_nlink
;
176 buf
->st_uid
= buf64
->st_uid
;
177 buf
->st_gid
= buf64
->st_gid
;
178 buf
->st_rdev
= buf64
->st_rdev
;
179 buf
->st_size
= buf64
->st_size
;
180 buf
->st_atime
= buf64
->st_atime
;
181 buf
->st_mtime
= buf64
->st_mtime
;
182 buf
->st_ctime
= buf64
->st_ctime
;
185 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
187 buf
->st_dev
= buf64
->st_dev
;
188 buf
->st_ino
= buf64
->st_ino
;
189 buf
->st_mode
= buf64
->st_mode
;
190 buf
->st_nlink
= buf64
->st_nlink
;
191 buf
->st_uid
= buf64
->st_uid
;
192 buf
->st_gid
= buf64
->st_gid
;
193 buf
->st_rdev
= buf64
->st_rdev
;
194 buf
->st_size
= buf64
->st_size
;
195 buf
->st_atime
= buf64
->st_atime
;
196 buf
->st_mtime
= buf64
->st_mtime
;
197 buf
->st_ctime
= buf64
->st_ctime
;
200 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
202 buf
->st_dev
= buf64
->st_dev
;
203 buf
->st_ino
= buf64
->st_ino
;
204 buf
->st_mode
= buf64
->st_mode
;
205 buf
->st_nlink
= buf64
->st_nlink
;
206 buf
->st_uid
= buf64
->st_uid
;
207 buf
->st_gid
= buf64
->st_gid
;
208 buf
->st_rdev
= buf64
->st_rdev
;
209 buf
->st_size
= buf64
->st_size
;
210 buf
->st_atime
= buf64
->st_atime
;
211 buf
->st_mtime
= buf64
->st_mtime
;
212 buf
->st_ctime
= buf64
->st_ctime
;
215 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
217 buf
->st_dev
= buf64
->st_dev
;
218 buf
->st_ino
= buf64
->st_ino
;
219 buf
->st_mode
= buf64
->st_mode
;
220 buf
->st_nlink
= buf64
->st_nlink
;
221 buf
->st_uid
= buf64
->st_uid
;
222 buf
->st_gid
= buf64
->st_gid
;
223 buf
->st_rdev
= buf64
->st_rdev
;
224 buf
->st_size
= buf64
->st_size
;
225 buf
->st_atime
= buf64
->st_atime
;
226 buf
->st_mtime
= buf64
->st_mtime
;
227 buf
->st_ctime
= buf64
->st_ctime
;
230 static void time_to_filetime( __time64_t time
, FILETIME
*ft
)
232 /* 1601 to 1970 is 369 years plus 89 leap days */
233 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
235 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
236 ft
->dwHighDateTime
= ticks
>> 32;
237 ft
->dwLowDateTime
= ticks
;
240 static inline ioinfo
* get_ioinfo_nolock(int fd
)
243 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
244 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
246 return &MSVCRT___badioinfo
;
248 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
251 static inline void init_ioinfo_cs(ioinfo
*info
)
253 if(!(info
->exflag
& EF_CRIT_INIT
)) {
255 if(!(info
->exflag
& EF_CRIT_INIT
)) {
256 InitializeCriticalSection(&info
->crit
);
257 info
->exflag
|= EF_CRIT_INIT
;
263 static inline ioinfo
* get_ioinfo(int fd
)
265 ioinfo
*ret
= get_ioinfo_nolock(fd
);
266 if(ret
== &MSVCRT___badioinfo
)
269 EnterCriticalSection(&ret
->crit
);
273 static inline BOOL
alloc_pioinfo_block(int fd
)
278 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
280 *MSVCRT__errno() = MSVCRT_ENFILE
;
284 block
= MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
287 WARN(":out of memory!\n");
288 *MSVCRT__errno() = MSVCRT_ENOMEM
;
291 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
292 block
[i
].handle
= INVALID_HANDLE_VALUE
;
293 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
298 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
302 ret
= get_ioinfo(fd
);
303 if(ret
!= &MSVCRT___badioinfo
)
306 if(!alloc_pioinfo_block(fd
))
307 return &MSVCRT___badioinfo
;
309 return get_ioinfo(fd
);
312 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
317 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
319 ioinfo
*info
= get_ioinfo_nolock(i
);
321 if(info
== &MSVCRT___badioinfo
)
323 if(!alloc_pioinfo_block(i
))
324 return &MSVCRT___badioinfo
;
325 info
= get_ioinfo_nolock(i
);
328 init_ioinfo_cs(info
);
329 if(TryEnterCriticalSection(&info
->crit
))
331 if(info
->handle
== INVALID_HANDLE_VALUE
)
336 LeaveCriticalSection(&info
->crit
);
340 WARN(":files exhausted!\n");
341 *MSVCRT__errno() = MSVCRT_ENFILE
;
342 return &MSVCRT___badioinfo
;
345 static inline void release_ioinfo(ioinfo
*info
)
347 if(info
!=&MSVCRT___badioinfo
&& info
->exflag
& EF_CRIT_INIT
)
348 LeaveCriticalSection(&info
->crit
);
351 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
355 if(i
>= MSVCRT_max_streams
)
359 return &MSVCRT__iob
[i
];
361 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
363 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
364 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
365 ERR("out of memory\n");
366 *MSVCRT__errno() = MSVCRT_ENOMEM
;
370 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
372 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
377 /* INTERNAL: free a file entry fd */
378 static void msvcrt_free_fd(int fd
)
380 ioinfo
*fdinfo
= get_ioinfo(fd
);
382 if(fdinfo
!= &MSVCRT___badioinfo
)
384 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
387 TRACE(":fd (%d) freed\n",fd
);
394 SetStdHandle(STD_INPUT_HANDLE
, 0);
397 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
400 SetStdHandle(STD_ERROR_HANDLE
, 0);
404 release_ioinfo(fdinfo
);
407 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
409 fdinfo
->handle
= hand
;
410 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
411 fdinfo
->lookahead
[0] = '\n';
412 fdinfo
->lookahead
[1] = '\n';
413 fdinfo
->lookahead
[2] = '\n';
414 fdinfo
->exflag
&= EF_CRIT_INIT
;
416 if (hand
== MSVCRT_NO_CONSOLE
) hand
= 0;
417 switch (fdinfo
-MSVCRT___pioinfo
[0])
419 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
420 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
421 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
425 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
426 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
429 ioinfo
*info
= get_ioinfo_alloc(&fd
);
431 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
433 if(info
== &MSVCRT___badioinfo
)
436 msvcrt_set_fd(info
, hand
, flag
);
437 release_ioinfo(info
);
441 /* INTERNAL: Allocate a FILE* for an fd slot */
442 /* caller must hold the files lock */
443 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
448 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
450 file
= msvcrt_get_file(i
);
454 if (file
->_flag
== 0)
456 if (i
== MSVCRT_stream_idx
)
458 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
460 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
461 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
472 /* INTERNAL: initialize a FILE* from an open fd */
473 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
475 TRACE(":fd (%d) allocating FILE*\n",fd
);
476 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
478 WARN(":invalid fd %d\n",fd
);
479 *MSVCRT___doserrno() = 0;
480 *MSVCRT__errno() = MSVCRT_EBADF
;
483 file
->_ptr
= file
->_base
= NULL
;
486 file
->_flag
= stream_flags
;
487 file
->_tmpfname
= NULL
;
489 TRACE(":got FILE* (%p)\n",file
);
493 /* INTERNAL: Create an inheritance data block (for spawned process)
494 * The inheritance block is made of:
495 * 00 int nb of file descriptor (NBFD)
496 * 04 char file flags (wxflag): repeated for each fd
497 * 4+NBFD HANDLE file handle: repeated for each fd
499 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
506 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
507 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
511 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
512 *block
= MSVCRT_calloc(1, *size
);
518 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
519 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
521 *(unsigned*)*block
= last_fd
;
522 for (fd
= 0; fd
< last_fd
; fd
++)
524 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
525 fdinfo
= get_ioinfo(fd
);
526 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
528 *wxflag_ptr
= fdinfo
->wxflag
;
529 *handle_ptr
= fdinfo
->handle
;
534 *handle_ptr
= INVALID_HANDLE_VALUE
;
536 release_ioinfo(fdinfo
);
537 wxflag_ptr
++; handle_ptr
++;
542 /* INTERNAL: Set up all file descriptors,
543 * as well as default streams (stdin, stderr and stdout)
545 void msvcrt_init_io(void)
551 GetStartupInfoA(&si
);
552 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
558 count
= *(unsigned*)si
.lpReserved2
;
559 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
560 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
562 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
563 count
= min(count
, MSVCRT_MAX_FILES
);
564 for (i
= 0; i
< count
; i
++)
566 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
568 fdinfo
= get_ioinfo_alloc_fd(i
);
569 if(fdinfo
!= &MSVCRT___badioinfo
)
570 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
571 release_ioinfo(fdinfo
);
574 wxflag_ptr
++; handle_ptr
++;
578 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDIN_FILENO
);
579 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
580 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
581 DWORD flags
= WX_OPEN
| WX_TEXT
;
582 DWORD type
= GetFileType(h
);
584 if (type
== FILE_TYPE_UNKNOWN
) {
585 h
= MSVCRT_NO_CONSOLE
;
587 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
589 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
593 msvcrt_set_fd(fdinfo
, h
, flags
);
595 release_ioinfo(fdinfo
);
597 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO
);
598 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
599 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
600 DWORD flags
= WX_OPEN
| WX_TEXT
;
601 DWORD type
= GetFileType(h
);
603 if (type
== FILE_TYPE_UNKNOWN
) {
604 h
= MSVCRT_NO_CONSOLE
;
606 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
608 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
612 msvcrt_set_fd(fdinfo
, h
, flags
);
614 release_ioinfo(fdinfo
);
616 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO
);
617 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
618 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
619 DWORD flags
= WX_OPEN
| WX_TEXT
;
620 DWORD type
= GetFileType(h
);
622 if (type
== FILE_TYPE_UNKNOWN
) {
623 h
= MSVCRT_NO_CONSOLE
;
625 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
627 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
631 msvcrt_set_fd(fdinfo
, h
, flags
);
633 release_ioinfo(fdinfo
);
635 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
636 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
637 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
639 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
640 for (i
= 0; i
< 3; i
++)
642 /* FILE structs for stdin/out/err are static and never deleted */
643 MSVCRT__iob
[i
]._file
= get_ioinfo_nolock(i
)->handle
== MSVCRT_NO_CONSOLE
?
644 MSVCRT_NO_CONSOLE_FD
: i
;
645 MSVCRT__iob
[i
]._tmpfname
= NULL
;
646 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
648 MSVCRT_stream_idx
= 3;
651 /* INTERNAL: Flush stdio file buffer */
652 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
656 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
657 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
658 int cnt
=file
->_ptr
-file
->_base
;
659 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
660 file
->_flag
|= MSVCRT__IOERR
;
662 } else if(file
->_flag
& MSVCRT__IORW
) {
663 file
->_flag
&= ~MSVCRT__IOWRT
;
667 file
->_ptr
=file
->_base
;
672 /*********************************************************************
675 int CDECL
MSVCRT__isatty(int fd
)
677 TRACE(":fd (%d)\n",fd
);
679 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
682 /* INTERNAL: Allocate stdio file buffer */
683 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
685 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
686 && MSVCRT__isatty(file
->_file
))
689 file
->_base
= MSVCRT_calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
691 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
692 file
->_flag
|= MSVCRT__IOMYBUF
;
694 file
->_base
= (char*)(&file
->_charbuf
);
696 file
->_flag
|= MSVCRT__IONBF
;
698 file
->_ptr
= file
->_base
;
703 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
704 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
706 static char buffers
[2][MSVCRT_BUFSIZ
];
708 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
709 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
710 || !MSVCRT__isatty(file
->_file
))
713 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
714 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
715 file
->_flag
|= MSVCRT__USERBUF
;
719 /* INTERNAL: Removes temporary buffer from stdout or stderr */
720 /* Only call this function when add_std_buffer returned TRUE */
721 static void remove_std_buffer(MSVCRT_FILE
*file
)
723 msvcrt_flush_buffer(file
);
724 file
->_ptr
= file
->_base
= NULL
;
725 file
->_bufsiz
= file
->_cnt
= 0;
726 file
->_flag
&= ~MSVCRT__USERBUF
;
729 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
730 static int msvcrt_int_to_base32(int num
, char *str
)
745 *p
= (num
& 31) + '0';
747 *p
+= ('a' - '0' - 10);
754 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
755 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
770 *p
= (num
& 31) + '0';
772 *p
+= ('a' - '0' - 10);
779 /*********************************************************************
780 * __iob_func (MSVCRT.@)
783 MSVCRT_FILE
* CDECL
__iob_func(void)
785 return &MSVCRT__iob
[0];
788 #if _MSVCR_VER >= 140
789 /*********************************************************************
790 * __acrt_iob_func(UCRTBASE.@)
792 MSVCRT_FILE
* CDECL
MSVCRT___acrt_iob_func(unsigned idx
)
794 return &MSVCRT__iob
[idx
];
798 /*********************************************************************
801 int CDECL
MSVCRT__access(const char *filename
, int mode
)
803 DWORD attr
= GetFileAttributesA(filename
);
805 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
807 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
809 msvcrt_set_errno(GetLastError());
812 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
814 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
820 /*********************************************************************
821 * _access_s (MSVCRT.@)
823 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
825 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
826 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
828 if (MSVCRT__access(filename
, mode
) == -1)
829 return *MSVCRT__errno();
833 /*********************************************************************
834 * _waccess (MSVCRT.@)
836 int CDECL
MSVCRT__waccess(const wchar_t *filename
, int mode
)
838 DWORD attr
= GetFileAttributesW(filename
);
840 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
842 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
844 msvcrt_set_errno(GetLastError());
847 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
849 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
855 /*********************************************************************
856 * _waccess_s (MSVCRT.@)
858 int CDECL
MSVCRT__waccess_s(const wchar_t *filename
, int mode
)
860 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
861 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
863 if (MSVCRT__waccess(filename
, mode
) == -1)
864 return *MSVCRT__errno();
868 /*********************************************************************
871 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
873 DWORD oldFlags
= GetFileAttributesA(path
);
875 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
877 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
878 oldFlags
| FILE_ATTRIBUTE_READONLY
;
880 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
883 msvcrt_set_errno(GetLastError());
887 /*********************************************************************
890 int CDECL
MSVCRT__wchmod(const wchar_t *path
, int flags
)
892 DWORD oldFlags
= GetFileAttributesW(path
);
894 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
896 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
897 oldFlags
| FILE_ATTRIBUTE_READONLY
;
899 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
902 msvcrt_set_errno(GetLastError());
906 /*********************************************************************
909 int CDECL
MSVCRT__unlink(const char *path
)
911 TRACE("%s\n",debugstr_a(path
));
912 if(DeleteFileA(path
))
914 TRACE("failed (%d)\n",GetLastError());
915 msvcrt_set_errno(GetLastError());
919 /*********************************************************************
920 * _wunlink (MSVCRT.@)
922 int CDECL
MSVCRT__wunlink(const wchar_t *path
)
924 TRACE("(%s)\n",debugstr_w(path
));
925 if(DeleteFileW(path
))
927 TRACE("failed (%d)\n",GetLastError());
928 msvcrt_set_errno(GetLastError());
932 /*********************************************************************
935 int CDECL
MSVCRT__commit(int fd
)
937 ioinfo
*info
= get_ioinfo(fd
);
940 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
942 if (info
->handle
== INVALID_HANDLE_VALUE
)
944 else if (!FlushFileBuffers(info
->handle
))
946 if (GetLastError() == ERROR_INVALID_HANDLE
)
948 /* FlushFileBuffers fails for console handles
949 * so we ignore this error.
955 TRACE(":failed-last error (%d)\n",GetLastError());
956 msvcrt_set_errno(GetLastError());
966 release_ioinfo(info
);
970 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
971 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
973 /* INTERNAL: Flush all stream buffer */
974 static int msvcrt_flush_all_buffers(int mask
)
976 int i
, num_flushed
= 0;
980 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
981 file
= msvcrt_get_file(i
);
985 if(file
->_flag
& mask
) {
993 TRACE(":flushed (%d) handles\n",num_flushed
);
997 /*********************************************************************
998 * _flushall (MSVCRT.@)
1000 int CDECL
MSVCRT__flushall(void)
1002 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
1005 /*********************************************************************
1008 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
1013 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1016 MSVCRT__lock_file(file
);
1017 ret
= MSVCRT__fflush_nolock(file
);
1018 MSVCRT__unlock_file(file
);
1024 /*********************************************************************
1025 * _fflush_nolock (MSVCRT.@)
1027 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
1032 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1036 res
= msvcrt_flush_buffer(file
);
1037 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1038 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
1042 /*********************************************************************
1045 int CDECL
MSVCRT__close(int fd
)
1047 ioinfo
*info
= get_ioinfo(fd
);
1050 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1052 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1053 *MSVCRT__errno() = MSVCRT_EBADF
;
1055 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, MSVCRT_EBADF
)) {
1057 } else if (fd
== MSVCRT_STDOUT_FILENO
&&
1058 info
->handle
== get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
) {
1061 } else if (fd
== MSVCRT_STDERR_FILENO
&&
1062 info
->handle
== get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
) {
1066 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1069 WARN(":failed-last error (%d)\n",GetLastError());
1070 msvcrt_set_errno(GetLastError());
1073 release_ioinfo(info
);
1077 /*********************************************************************
1080 * MSDN isn't clear on this point, but the remarks for _pipe
1081 * indicate file descriptors duplicated with _dup and _dup2 are always
1084 int CDECL
MSVCRT__dup2(int od
, int nd
)
1086 ioinfo
*info_od
, *info_nd
;
1089 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1093 info_od
= get_ioinfo(od
);
1094 info_nd
= get_ioinfo_alloc_fd(nd
);
1098 info_nd
= get_ioinfo_alloc_fd(nd
);
1099 info_od
= get_ioinfo(od
);
1102 if (info_nd
== &MSVCRT___badioinfo
)
1106 else if (info_od
->wxflag
& WX_OPEN
)
1110 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1111 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1113 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1115 if (info_nd
->wxflag
& WX_OPEN
)
1118 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1119 /* _dup2 returns 0, not nd, on success */
1125 msvcrt_set_errno(GetLastError());
1130 *MSVCRT__errno() = MSVCRT_EBADF
;
1134 release_ioinfo(info_od
);
1135 release_ioinfo(info_nd
);
1139 /*********************************************************************
1142 int CDECL
MSVCRT__dup(int od
)
1145 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1147 if (MSVCRT__dup2(od
, fd
) == 0)
1151 release_ioinfo(info
);
1155 /*********************************************************************
1158 int CDECL
MSVCRT__eof(int fd
)
1160 ioinfo
*info
= get_ioinfo(fd
);
1161 DWORD curpos
,endpos
;
1162 LONG hcurpos
,hendpos
;
1164 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1166 if (info
->handle
== INVALID_HANDLE_VALUE
)
1168 release_ioinfo(info
);
1172 if (info
->wxflag
& WX_ATEOF
)
1174 release_ioinfo(info
);
1178 /* Otherwise we do it the hard way */
1179 hcurpos
= hendpos
= 0;
1180 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1181 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1183 if (curpos
== endpos
&& hcurpos
== hendpos
)
1185 /* FIXME: shouldn't WX_ATEOF be set here? */
1186 release_ioinfo(info
);
1190 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1191 release_ioinfo(info
);
1195 /*********************************************************************
1196 * _fcloseall (MSVCRT.@)
1198 int CDECL
MSVCRT__fcloseall(void)
1200 int num_closed
= 0, i
;
1204 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1205 file
= msvcrt_get_file(i
);
1207 if (file
->_flag
&& !MSVCRT_fclose(file
))
1212 TRACE(":closed (%d) handles\n",num_closed
);
1216 /* free everything on process exit */
1217 void msvcrt_free_io(void)
1223 MSVCRT__fcloseall();
1225 for(i
=0; i
<ARRAY_SIZE(MSVCRT___pioinfo
); i
++)
1227 if(!MSVCRT___pioinfo
[i
])
1230 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1232 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1233 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1235 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1238 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1240 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1241 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1243 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1244 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1248 for(i
=0; i
<ARRAY_SIZE(MSVCRT_fstream
); i
++)
1249 MSVCRT_free(MSVCRT_fstream
[i
]);
1252 /*********************************************************************
1253 * _lseeki64 (MSVCRT.@)
1255 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1257 ioinfo
*info
= get_ioinfo(fd
);
1260 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1262 if (info
->handle
== INVALID_HANDLE_VALUE
)
1264 *MSVCRT__errno() = MSVCRT_EBADF
;
1265 release_ioinfo(info
);
1269 if (whence
< 0 || whence
> 2)
1271 release_ioinfo(info
);
1272 *MSVCRT__errno() = MSVCRT_EINVAL
;
1276 TRACE(":fd (%d) to %s pos %s\n",
1277 fd
,wine_dbgstr_longlong(offset
),
1278 (whence
==MSVCRT_SEEK_SET
)?"SEEK_SET":
1279 (whence
==MSVCRT_SEEK_CUR
)?"SEEK_CUR":
1280 (whence
==MSVCRT_SEEK_END
)?"SEEK_END":"UNKNOWN");
1282 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1283 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1284 ofs
.QuadPart
= offset
;
1285 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1286 GetLastError() == ERROR_SUCCESS
)
1288 info
->wxflag
&= ~WX_ATEOF
;
1289 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1291 release_ioinfo(info
);
1292 return ofs
.QuadPart
;
1294 release_ioinfo(info
);
1295 TRACE(":error-last error (%d)\n",GetLastError());
1296 msvcrt_set_errno(GetLastError());
1300 /*********************************************************************
1303 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1305 return MSVCRT__lseeki64(fd
, offset
, whence
);
1308 /*********************************************************************
1309 * _lock_file (MSVCRT.@)
1311 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1313 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1314 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1316 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1319 /*********************************************************************
1320 * _unlock_file (MSVCRT.@)
1322 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1324 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1325 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1327 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1330 /*********************************************************************
1331 * _locking (MSVCRT.@)
1333 * This is untested; the underlying LockFile doesn't work yet.
1335 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1337 ioinfo
*info
= get_ioinfo(fd
);
1341 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1342 if (info
->handle
== INVALID_HANDLE_VALUE
)
1344 release_ioinfo(info
);
1348 if (mode
< 0 || mode
> 4)
1350 release_ioinfo(info
);
1351 *MSVCRT__errno() = MSVCRT_EINVAL
;
1355 TRACE(":fd (%d) by 0x%08x mode %s\n",
1356 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1357 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1358 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1359 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1360 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1363 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1365 release_ioinfo(info
);
1366 FIXME ("Seek failed\n");
1367 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1370 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1373 ret
= 1; /* just to satisfy gcc */
1376 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1381 else if (mode
== MSVCRT__LK_UNLCK
)
1382 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1384 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1385 /* FIXME - what about error settings? */
1386 release_ioinfo(info
);
1387 return ret
? 0 : -1;
1390 /*********************************************************************
1391 * _fseeki64 (MSVCRT.@)
1393 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1397 MSVCRT__lock_file(file
);
1398 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1399 MSVCRT__unlock_file(file
);
1404 /*********************************************************************
1405 * _fseeki64_nolock (MSVCRT.@)
1407 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1411 if(whence
== MSVCRT_SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1412 whence
= MSVCRT_SEEK_SET
;
1413 offset
+= MSVCRT__ftelli64_nolock(file
);
1416 /* Flush output if needed */
1417 msvcrt_flush_buffer(file
);
1418 /* Reset direction of i/o */
1419 if(file
->_flag
& MSVCRT__IORW
) {
1420 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1422 /* Clear end of file flag */
1423 file
->_flag
&= ~MSVCRT__IOEOF
;
1424 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1429 /*********************************************************************
1432 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, __msvcrt_long offset
, int whence
)
1434 return MSVCRT__fseeki64( file
, offset
, whence
);
1437 /*********************************************************************
1438 * _fseek_nolock (MSVCRT.@)
1440 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, __msvcrt_long offset
, int whence
)
1442 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1445 /*********************************************************************
1446 * _chsize_s (MSVCRT.@)
1448 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1454 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1456 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1459 info
= get_ioinfo(fd
);
1460 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1462 /* save the current file pointer */
1463 cur
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1466 pos
= MSVCRT__lseeki64(fd
, size
, MSVCRT_SEEK_SET
);
1469 ret
= SetEndOfFile(info
->handle
);
1470 if (!ret
) msvcrt_set_errno(GetLastError());
1473 /* restore the file pointer */
1474 MSVCRT__lseeki64(fd
, cur
, MSVCRT_SEEK_SET
);
1478 release_ioinfo(info
);
1479 return ret
? 0 : *MSVCRT__errno();
1482 /*********************************************************************
1483 * _chsize (MSVCRT.@)
1485 int CDECL
MSVCRT__chsize(int fd
, __msvcrt_long size
)
1487 /* _chsize_s returns errno on failure but _chsize should return -1 */
1488 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1491 /*********************************************************************
1492 * clearerr (MSVCRT.@)
1494 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1496 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1498 MSVCRT__lock_file(file
);
1499 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1500 MSVCRT__unlock_file(file
);
1503 /*********************************************************************
1504 * clearerr_s (MSVCRT.@)
1506 int CDECL
MSVCRT_clearerr_s(MSVCRT_FILE
* file
)
1508 TRACE(":file (%p)\n",file
);
1510 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
1512 MSVCRT__lock_file(file
);
1513 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1514 MSVCRT__unlock_file(file
);
1518 /*********************************************************************
1521 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1523 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1525 MSVCRT__lock_file(file
);
1526 MSVCRT__fseek_nolock(file
, 0L, MSVCRT_SEEK_SET
);
1527 MSVCRT_clearerr(file
);
1528 MSVCRT__unlock_file(file
);
1531 static int msvcrt_get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1533 int plus
= MSVCRT_wcschr(mode
, '+') != NULL
;
1535 TRACE("%s\n", debugstr_w(mode
));
1537 while(*mode
== ' ') mode
++;
1542 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1543 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1546 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1547 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1550 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1551 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1554 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1558 *stream_flags
|= MSVCRT__commode
;
1560 while (*mode
&& *mode
!=',')
1564 *open_flags
|= MSVCRT__O_BINARY
;
1565 *open_flags
&= ~MSVCRT__O_TEXT
;
1568 *open_flags
|= MSVCRT__O_TEXT
;
1569 *open_flags
&= ~MSVCRT__O_BINARY
;
1572 *open_flags
|= MSVCRT__O_TEMPORARY
;
1575 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1578 *stream_flags
|= MSVCRT__IOCOMMIT
;
1581 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1584 *open_flags
|= MSVCRT__O_NOINHERIT
;
1593 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1596 ERR("incorrect mode flag: %c\n", mode
[-1]);
1603 while(*mode
== ' ') mode
++;
1604 if(!MSVCRT_CHECK_PMT(!MSVCRT_wcsncmp(L
"ccs", mode
, 3)))
1607 while(*mode
== ' ') mode
++;
1608 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1611 while(*mode
== ' ') mode
++;
1613 if(!MSVCRT__wcsnicmp(L
"utf-8", mode
, 5))
1615 *open_flags
|= MSVCRT__O_U8TEXT
;
1618 else if(!MSVCRT__wcsnicmp(L
"utf-16le", mode
, 8))
1620 *open_flags
|= MSVCRT__O_U16TEXT
;
1623 else if(!MSVCRT__wcsnicmp(L
"unicode", mode
, 7))
1625 *open_flags
|= MSVCRT__O_WTEXT
;
1630 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1634 while(*mode
== ' ') mode
++;
1637 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1642 /*********************************************************************
1643 * _fdopen (MSVCRT.@)
1645 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1648 wchar_t *modeW
= NULL
;
1650 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1652 ret
= MSVCRT__wfdopen(fd
, modeW
);
1658 /*********************************************************************
1659 * _wfdopen (MSVCRT.@)
1661 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const wchar_t *mode
)
1663 int open_flags
, stream_flags
;
1666 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1669 if (!(file
= msvcrt_alloc_fp()))
1671 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1676 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1682 /*********************************************************************
1683 * _filelength (MSVCRT.@)
1685 LONG CDECL
MSVCRT__filelength(int fd
)
1687 LONG curPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
1690 LONG endPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_END
);
1693 if (endPos
!= curPos
)
1694 MSVCRT__lseek(fd
, curPos
, MSVCRT_SEEK_SET
);
1701 /*********************************************************************
1702 * _filelengthi64 (MSVCRT.@)
1704 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1706 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1709 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_END
);
1712 if (endPos
!= curPos
)
1713 MSVCRT__lseeki64(fd
, curPos
, MSVCRT_SEEK_SET
);
1720 /*********************************************************************
1721 * _fileno (MSVCRT.@)
1723 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1725 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1729 /*********************************************************************
1730 * _fstat64 (MSVCRT.@)
1732 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1734 ioinfo
*info
= get_ioinfo(fd
);
1737 BY_HANDLE_FILE_INFORMATION hfi
;
1739 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1740 if (info
->handle
== INVALID_HANDLE_VALUE
)
1742 release_ioinfo(info
);
1748 WARN(":failed-NULL buf\n");
1749 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1750 release_ioinfo(info
);
1754 memset(&hfi
, 0, sizeof(hfi
));
1755 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1756 type
= GetFileType(info
->handle
);
1757 if (type
== FILE_TYPE_PIPE
)
1759 buf
->st_dev
= buf
->st_rdev
= fd
;
1760 buf
->st_mode
= MSVCRT__S_IFIFO
;
1763 else if (type
== FILE_TYPE_CHAR
)
1765 buf
->st_dev
= buf
->st_rdev
= fd
;
1766 buf
->st_mode
= MSVCRT__S_IFCHR
;
1769 else /* FILE_TYPE_DISK etc. */
1771 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1773 WARN(":failed-last error (%d)\n",GetLastError());
1774 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1775 release_ioinfo(info
);
1778 buf
->st_mode
= MSVCRT__S_IFREG
| 0444;
1779 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1780 buf
->st_mode
|= 0222;
1781 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1782 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1784 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1785 buf
->st_mtime
= buf
->st_ctime
= dw
;
1786 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1788 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1790 release_ioinfo(info
);
1794 /*********************************************************************
1795 * _fstati64 (MSVCRT.@)
1797 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1800 struct MSVCRT__stat64 buf64
;
1802 ret
= MSVCRT__fstat64(fd
, &buf64
);
1804 msvcrt_stat64_to_stati64(&buf64
, buf
);
1808 /*********************************************************************
1811 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1813 struct MSVCRT__stat64 buf64
;
1815 ret
= MSVCRT__fstat64(fd
, &buf64
);
1817 msvcrt_stat64_to_stat(&buf64
, buf
);
1821 /*********************************************************************
1822 * _fstat32 (MSVCR80.@)
1824 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1827 struct MSVCRT__stat64 buf64
;
1829 ret
= MSVCRT__fstat64(fd
, &buf64
);
1831 msvcrt_stat64_to_stat32(&buf64
, buf
);
1835 /*********************************************************************
1836 * _fstat32i64 (MSVCR80.@)
1838 int CDECL
MSVCRT__fstat32i64(int fd
, struct MSVCRT__stat32i64
* buf
)
1841 struct MSVCRT__stat64 buf64
;
1843 ret
= MSVCRT__fstat64(fd
, &buf64
);
1845 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1849 /*********************************************************************
1850 * _fstat64i32 (MSVCR80.@)
1852 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1855 struct MSVCRT__stat64 buf64
;
1857 ret
= MSVCRT__fstat64(fd
, &buf64
);
1859 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1863 /*********************************************************************
1864 * _futime64 (MSVCRT.@)
1866 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1868 ioinfo
*info
= get_ioinfo(fd
);
1873 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1878 time_to_filetime( t
->actime
, &at
);
1879 time_to_filetime( t
->modtime
, &wt
);
1882 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1884 release_ioinfo(info
);
1885 msvcrt_set_errno(GetLastError());
1888 release_ioinfo(info
);
1892 /*********************************************************************
1893 * _futime32 (MSVCRT.@)
1895 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1899 struct MSVCRT___utimbuf64 t64
;
1900 t64
.actime
= t
->actime
;
1901 t64
.modtime
= t
->modtime
;
1902 return _futime64( fd
, &t64
);
1905 return _futime64( fd
, NULL
);
1908 /*********************************************************************
1909 * _futime (MSVCRT.@)
1912 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1914 return _futime64( fd
, t
);
1917 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1919 return _futime32( fd
, t
);
1923 /*********************************************************************
1924 * _get_osfhandle (MSVCRT.@)
1926 intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1928 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1929 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1931 if(hand
== INVALID_HANDLE_VALUE
)
1932 *MSVCRT__errno() = MSVCRT_EBADF
;
1933 return (intptr_t)hand
;
1936 /*********************************************************************
1937 * _mktemp_s (MSVCRT.@)
1939 int CDECL
MSVCRT__mktemp_s(char *pattern
, size_t size
)
1943 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1944 return MSVCRT_EINVAL
;
1946 for(len
=0; len
<size
; len
++)
1949 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1952 return MSVCRT_EINVAL
;
1955 for(xno
=1; xno
<=6; xno
++)
1956 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1957 return MSVCRT_EINVAL
;
1959 id
= GetCurrentProcessId();
1960 for(xno
=1; xno
<6; xno
++) {
1961 pattern
[len
-xno
] = id
%10 + '0';
1965 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1966 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1971 *MSVCRT__errno() = MSVCRT_EEXIST
;
1972 return MSVCRT_EEXIST
;
1975 /*********************************************************************
1976 * _mktemp (MSVCRT.@)
1978 char * CDECL
MSVCRT__mktemp(char *pattern
)
1981 char *retVal
= pattern
;
1989 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1993 id
= GetCurrentProcessId();
1997 int tempNum
= id
/ 10;
1998 *pattern
-- = id
- (tempNum
* 10) + '0';
2004 *pattern
= letter
++;
2005 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2007 } while(letter
<= 'z');
2011 /*********************************************************************
2012 * _wmktemp_s (MSVCRT.@)
2014 int CDECL
MSVCRT__wmktemp_s(wchar_t *pattern
, size_t size
)
2018 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2019 return MSVCRT_EINVAL
;
2021 for(len
=0; len
<size
; len
++)
2024 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2027 return MSVCRT_EINVAL
;
2030 for(xno
=1; xno
<=6; xno
++)
2031 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2032 return MSVCRT_EINVAL
;
2034 id
= GetCurrentProcessId();
2035 for(xno
=1; xno
<6; xno
++) {
2036 pattern
[len
-xno
] = id
%10 + '0';
2040 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2041 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2046 *MSVCRT__errno() = MSVCRT_EEXIST
;
2047 return MSVCRT_EEXIST
;
2050 /*********************************************************************
2051 * _wmktemp (MSVCRT.@)
2053 wchar_t * CDECL
MSVCRT__wmktemp(wchar_t *pattern
)
2056 wchar_t *retVal
= pattern
;
2058 wchar_t letter
= 'a';
2064 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2068 id
= GetCurrentProcessId();
2072 int tempNum
= id
/ 10;
2073 *pattern
-- = id
- (tempNum
* 10) + '0';
2079 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2081 *pattern
= letter
++;
2082 } while(letter
!= '|');
2086 static unsigned split_oflags(unsigned oflags
)
2089 unsigned unsupp
; /* until we support everything */
2091 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2092 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2093 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2094 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2095 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2096 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2097 else if (*MSVCRT___p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2098 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2099 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2101 if ((unsupp
= oflags
& ~(
2102 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2103 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2104 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2105 MSVCRT__O_NOINHERIT
|
2106 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2107 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2109 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2114 /*********************************************************************
2117 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2120 SECURITY_ATTRIBUTES sa
;
2121 HANDLE readHandle
, writeHandle
;
2125 *MSVCRT__errno() = MSVCRT_EINVAL
;
2129 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2130 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2131 sa
.lpSecurityDescriptor
= NULL
;
2132 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2134 unsigned int wxflags
= split_oflags(textmode
);
2137 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2141 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2149 MSVCRT__close(pfds
[0]);
2150 CloseHandle(writeHandle
);
2151 *MSVCRT__errno() = MSVCRT_EMFILE
;
2156 CloseHandle(readHandle
);
2157 CloseHandle(writeHandle
);
2158 *MSVCRT__errno() = MSVCRT_EMFILE
;
2162 msvcrt_set_errno(GetLastError());
2167 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2169 char bom
[sizeof(utf8_bom
)];
2172 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2175 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2176 oflags
= (oflags
& ~(MSVCRT__O_WTEXT
| MSVCRT__O_U16TEXT
)) | MSVCRT__O_U8TEXT
;
2177 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2179 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2180 oflags
= (oflags
& ~(MSVCRT__O_WTEXT
| MSVCRT__O_U8TEXT
)) | MSVCRT__O_U16TEXT
;
2182 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2188 /*********************************************************************
2189 * _wsopen_dispatch (UCRTBASE.@)
2191 int CDECL
MSVCRT__wsopen_dispatch( const wchar_t* path
, int oflags
, int shflags
, int pmode
,
2192 int *fd
, int secure
)
2194 DWORD access
= 0, creation
= 0, attrib
;
2195 SECURITY_ATTRIBUTES sa
;
2196 DWORD sharing
, type
;
2200 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2201 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2203 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2206 wxflag
= split_oflags(oflags
);
2207 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2209 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2210 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2211 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2214 if (oflags
& MSVCRT__O_CREAT
)
2216 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))))
2217 return MSVCRT_EINVAL
;
2219 if (oflags
& MSVCRT__O_EXCL
)
2220 creation
= CREATE_NEW
;
2221 else if (oflags
& MSVCRT__O_TRUNC
)
2222 creation
= CREATE_ALWAYS
;
2224 creation
= OPEN_ALWAYS
;
2226 else /* no MSVCRT__O_CREAT */
2228 if (oflags
& MSVCRT__O_TRUNC
)
2229 creation
= TRUNCATE_EXISTING
;
2231 creation
= OPEN_EXISTING
;
2236 case MSVCRT__SH_DENYRW
:
2239 case MSVCRT__SH_DENYWR
:
2240 sharing
= FILE_SHARE_READ
;
2242 case MSVCRT__SH_DENYRD
:
2243 sharing
= FILE_SHARE_WRITE
;
2245 case MSVCRT__SH_DENYNO
:
2246 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2249 ERR( "Unhandled shflags 0x%x\n", shflags
);
2250 return MSVCRT_EINVAL
;
2253 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2254 attrib
= FILE_ATTRIBUTE_READONLY
;
2256 attrib
= FILE_ATTRIBUTE_NORMAL
;
2258 if (oflags
& MSVCRT__O_TEMPORARY
)
2260 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2262 sharing
|= FILE_SHARE_DELETE
;
2265 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2266 sa
.lpSecurityDescriptor
= NULL
;
2267 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2269 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2270 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2271 && !(access
&GENERIC_READ
))
2273 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2274 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2275 if (hand
!= INVALID_HANDLE_VALUE
)
2277 oflags
= check_bom(hand
, oflags
, FALSE
);
2282 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2283 if (hand
== INVALID_HANDLE_VALUE
) {
2284 WARN(":failed-last error (%d)\n",GetLastError());
2285 msvcrt_set_errno(GetLastError());
2286 return *MSVCRT__errno();
2289 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2291 LARGE_INTEGER size
= {{0}};
2293 if ((access
& GENERIC_WRITE
) && (creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
))
2294 GetFileSizeEx(hand
, &size
);
2296 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2297 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2298 || ((creation
==OPEN_EXISTING
|| creation
==OPEN_ALWAYS
) && !size
.QuadPart
)))
2300 if (oflags
& MSVCRT__O_U8TEXT
)
2302 DWORD written
= 0, tmp
;
2304 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2305 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2307 if (written
!= sizeof(utf8_bom
)) {
2308 WARN("error writing BOM\n");
2310 msvcrt_set_errno(GetLastError());
2311 return *MSVCRT__errno();
2316 DWORD written
= 0, tmp
;
2318 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2319 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2321 if (written
!= sizeof(utf16_bom
))
2323 WARN("error writing BOM\n");
2325 msvcrt_set_errno(GetLastError());
2326 return *MSVCRT__errno();
2330 else if (access
& GENERIC_READ
)
2331 oflags
= check_bom(hand
, oflags
, TRUE
);
2334 type
= GetFileType(hand
);
2335 if (type
== FILE_TYPE_CHAR
)
2337 else if (type
== FILE_TYPE_PIPE
)
2340 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2342 return *MSVCRT__errno();
2344 if (oflags
& MSVCRT__O_WTEXT
)
2345 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2346 else if (oflags
& MSVCRT__O_U16TEXT
)
2347 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2348 else if (oflags
& MSVCRT__O_U8TEXT
)
2349 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2351 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2356 /*********************************************************************
2357 * _wsopen_s (MSVCRT.@)
2359 int CDECL
MSVCRT__wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
2361 return MSVCRT__wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2364 /*********************************************************************
2365 * _wsopen (MSVCRT.@)
2367 int WINAPIV
MSVCRT__wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
2372 if (oflags
& MSVCRT__O_CREAT
)
2376 __ms_va_start(ap
, shflags
);
2377 pmode
= va_arg(ap
, int);
2383 return MSVCRT__wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2387 /*********************************************************************
2388 * _sopen_dispatch (UCRTBASE.@)
2390 int CDECL
MSVCRT__sopen_dispatch( const char *path
, int oflags
, int shflags
,
2391 int pmode
, int *fd
, int secure
)
2396 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2397 return MSVCRT_EINVAL
;
2399 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2400 return MSVCRT_EINVAL
;
2402 ret
= MSVCRT__wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2407 /*********************************************************************
2408 * _sopen_s (MSVCRT.@)
2410 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2412 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2415 /*********************************************************************
2418 int WINAPIV
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2423 if (oflags
& MSVCRT__O_CREAT
)
2427 __ms_va_start(ap
, shflags
);
2428 pmode
= va_arg(ap
, int);
2434 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2437 /*********************************************************************
2440 int WINAPIV
MSVCRT__open( const char *path
, int flags
, ... )
2444 if (flags
& MSVCRT__O_CREAT
)
2447 __ms_va_start(ap
, flags
);
2448 pmode
= va_arg(ap
, int);
2450 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2453 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2456 /*********************************************************************
2459 int WINAPIV
MSVCRT__wopen(const wchar_t *path
,int flags
,...)
2463 if (flags
& MSVCRT__O_CREAT
)
2466 __ms_va_start(ap
, flags
);
2467 pmode
= va_arg(ap
, int);
2469 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2472 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2475 /*********************************************************************
2478 int CDECL
MSVCRT__creat(const char *path
, int pmode
)
2480 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2481 return MSVCRT__open(path
, flags
, pmode
);
2484 /*********************************************************************
2485 * _wcreat (MSVCRT.@)
2487 int CDECL
MSVCRT__wcreat(const wchar_t *path
, int pmode
)
2489 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2490 return MSVCRT__wopen(path
, flags
, pmode
);
2493 /*********************************************************************
2494 * _open_osfhandle (MSVCRT.@)
2496 int CDECL
MSVCRT__open_osfhandle(intptr_t handle
, int oflags
)
2501 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2502 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2503 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2504 * text - it never sets MSVCRT__O_BINARY.
2506 /* don't let split_oflags() decide the mode if no mode is passed */
2507 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2508 oflags
|= MSVCRT__O_BINARY
;
2510 flags
= GetFileType((HANDLE
)handle
);
2511 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2513 msvcrt_set_errno(GetLastError());
2517 if (flags
== FILE_TYPE_CHAR
)
2519 else if (flags
== FILE_TYPE_PIPE
)
2523 flags
|= split_oflags(oflags
);
2525 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2526 TRACE(":handle (%Iu) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2530 /*********************************************************************
2533 int CDECL
MSVCRT__rmtmp(void)
2535 int num_removed
= 0, i
;
2539 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2540 file
= msvcrt_get_file(i
);
2542 if (file
->_tmpfname
)
2544 MSVCRT_fclose(file
);
2551 TRACE(":removed (%d) temp files\n",num_removed
);
2555 static inline int get_utf8_char_len(char ch
)
2557 if((ch
&0xf8) == 0xf0)
2559 else if((ch
&0xf0) == 0xe0)
2561 else if((ch
&0xe0) == 0xc0)
2566 /*********************************************************************
2567 * (internal) read_utf8
2569 static int read_utf8(ioinfo
*fdinfo
, wchar_t *buf
, unsigned int count
)
2571 HANDLE hand
= fdinfo
->handle
;
2572 char min_buf
[4], *readbuf
, lookahead
;
2573 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2575 /* make the buffer big enough to hold at least one character */
2576 /* read bytes have to fit to output and lookahead buffers */
2578 readbuf_size
= count
< 4 ? 4 : count
;
2579 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2584 if(fdinfo
->lookahead
[0] != '\n') {
2585 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2586 fdinfo
->lookahead
[0] = '\n';
2588 if(fdinfo
->lookahead
[1] != '\n') {
2589 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2590 fdinfo
->lookahead
[1] = '\n';
2592 if(fdinfo
->lookahead
[2] != '\n') {
2593 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2594 fdinfo
->lookahead
[2] = '\n';
2599 /* NOTE: this case is broken in native dll, reading
2600 * sometimes fails when small buffer is passed
2603 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2604 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2605 fdinfo
->wxflag
|= WX_ATEOF
;
2608 msvcrt_set_errno(GetLastError());
2611 }else if(!num_read
) {
2612 fdinfo
->wxflag
|= WX_ATEOF
;
2618 char_len
= get_utf8_char_len(readbuf
[0]);
2620 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2624 if(readbuf
[0] == '\n')
2625 fdinfo
->wxflag
|= WX_READNL
;
2627 fdinfo
->wxflag
&= ~WX_READNL
;
2629 if(readbuf
[0] == 0x1a) {
2630 fdinfo
->wxflag
|= WX_ATEOF
;
2634 if(readbuf
[0] == '\r') {
2635 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2637 else if(lookahead
== '\n')
2641 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2642 fdinfo
->lookahead
[0] = lookahead
;
2644 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2649 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2650 msvcrt_set_errno(GetLastError());
2657 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2660 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2661 fdinfo
->wxflag
|= WX_ATEOF
;
2662 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2665 msvcrt_set_errno(GetLastError());
2666 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2669 }else if(!pos
&& !num_read
) {
2670 fdinfo
->wxflag
|= WX_ATEOF
;
2671 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2676 if(readbuf
[0] == '\n')
2677 fdinfo
->wxflag
|= WX_READNL
;
2679 fdinfo
->wxflag
&= ~WX_READNL
;
2681 /* Find first byte of last character (may be incomplete) */
2682 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2683 if((readbuf
[i
]&0xc0) != 0x80)
2685 char_len
= get_utf8_char_len(readbuf
[i
]);
2686 if(char_len
+i
<= pos
)
2689 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2691 fdinfo
->lookahead
[0] = readbuf
[i
];
2693 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2695 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2697 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2701 for(i
=0, j
=0; i
<pos
; i
++) {
2702 if(readbuf
[i
] == 0x1a) {
2703 fdinfo
->wxflag
|= WX_ATEOF
;
2707 /* strip '\r' if followed by '\n' */
2708 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2709 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2710 readbuf
[j
++] = '\r';
2711 }else if(lookahead
== '\n' && j
==0) {
2712 readbuf
[j
++] = '\n';
2714 if(lookahead
!= '\n')
2715 readbuf
[j
++] = '\r';
2717 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2718 fdinfo
->lookahead
[0] = lookahead
;
2720 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2722 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2723 readbuf
[j
++] = readbuf
[i
];
2728 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2729 msvcrt_set_errno(GetLastError());
2730 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2734 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2738 /*********************************************************************
2741 * When reading \r as last character in text mode, read() positions
2742 * the file pointer on the \r character while getc() goes on to
2745 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2747 DWORD num_read
, utf16
;
2748 char *bufstart
= buf
;
2753 if (fdinfo
->wxflag
& WX_ATEOF
) {
2754 TRACE("already at EOF, returning 0\n");
2757 /* Don't trace small reads, it gets *very* annoying */
2759 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2760 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2762 *MSVCRT__errno() = MSVCRT_EBADF
;
2766 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2767 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2769 *MSVCRT__errno() = MSVCRT_EINVAL
;
2773 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2774 return read_utf8(fdinfo
, buf
, count
);
2776 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2778 if (fdinfo
->lookahead
[0] != '\n')
2780 bufstart
[0] = fdinfo
->lookahead
[0];
2781 fdinfo
->lookahead
[0] = '\n';
2785 bufstart
[1] = fdinfo
->lookahead
[1];
2786 fdinfo
->lookahead
[1] = '\n';
2789 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2790 num_read
+= 1+utf16
;
2795 if(utf16
&& (num_read
&1))
2797 /* msvcr90 uses uninitialized value from the buffer in this case */
2798 /* msvcrt ignores additional data */
2799 ERR("got odd number of bytes in UTF16 mode\n");
2803 if (count
!= 0 && num_read
== 0)
2805 fdinfo
->wxflag
|= WX_ATEOF
;
2806 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2808 else if (fdinfo
->wxflag
& WX_TEXT
)
2812 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2813 fdinfo
->wxflag
|= WX_READNL
;
2815 fdinfo
->wxflag
&= ~WX_READNL
;
2817 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2819 /* in text mode, a ctrl-z signals EOF */
2820 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2822 fdinfo
->wxflag
|= WX_ATEOF
;
2823 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2827 /* in text mode, strip \r if followed by \n */
2828 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2833 lookahead
[1] = '\n';
2834 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2836 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2838 bufstart
[j
++] = '\n';
2839 if(utf16
) bufstart
[j
++] = 0;
2843 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2845 bufstart
[j
++] = '\r';
2846 if(utf16
) bufstart
[j
++] = 0;
2849 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2851 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2853 bufstart
[j
++] = '\n';
2854 if (utf16
) bufstart
[j
++] = 0;
2858 fdinfo
->lookahead
[0] = lookahead
[0];
2859 fdinfo
->lookahead
[1] = lookahead
[1];
2863 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2868 bufstart
[j
++] = '\r';
2869 if(utf16
) bufstart
[j
++] = 0;
2872 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2873 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2875 bufstart
[j
++] = bufstart
[i
];
2876 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2884 if (GetLastError() == ERROR_BROKEN_PIPE
)
2886 TRACE(":end-of-pipe\n");
2887 fdinfo
->wxflag
|= WX_ATEOF
;
2892 TRACE(":failed-last error (%d)\n",GetLastError());
2893 msvcrt_set_errno(GetLastError());
2899 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2903 /*********************************************************************
2906 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2911 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
2912 *MSVCRT__errno() = MSVCRT_EBADF
;
2916 info
= get_ioinfo(fd
);
2917 num_read
= read_i(fd
, info
, buf
, count
);
2918 release_ioinfo(info
);
2922 /*********************************************************************
2923 * _setmode (MSVCRT.@)
2925 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2927 ioinfo
*info
= get_ioinfo(fd
);
2928 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2929 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2930 ret
= MSVCRT__O_WTEXT
;
2932 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2933 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2934 *MSVCRT__errno() = MSVCRT_EINVAL
;
2935 release_ioinfo(info
);
2939 if(info
== &MSVCRT___badioinfo
) {
2940 *MSVCRT__errno() = MSVCRT_EBADF
;
2944 if(mode
== MSVCRT__O_BINARY
) {
2945 info
->wxflag
&= ~WX_TEXT
;
2946 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2947 release_ioinfo(info
);
2951 info
->wxflag
|= WX_TEXT
;
2952 if(mode
== MSVCRT__O_TEXT
)
2953 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2954 else if(mode
== MSVCRT__O_U8TEXT
)
2955 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2957 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2959 release_ioinfo(info
);
2963 /*********************************************************************
2964 * _stat64 (MSVCRT.@)
2966 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2969 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2970 unsigned short mode
= ALL_S_IREAD
;
2973 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2975 plen
= strlen(path
);
2976 while (plen
&& path
[plen
-1]==' ')
2979 if (plen
==2 && path
[1]==':')
2981 *MSVCRT__errno() = MSVCRT_ENOENT
;
2986 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2988 *MSVCRT__errno() = MSVCRT_ENOENT
;
2993 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2995 TRACE("failed (%d)\n",GetLastError());
2996 *MSVCRT__errno() = MSVCRT_ENOENT
;
3000 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3002 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3003 Bon 011120: This FIXME seems incorrect
3004 Also a letter as first char isn't enough to be classified
3007 if (MSVCRT_isalpha(*path
)&& (*(path
+1)==':'))
3008 buf
->st_dev
= buf
->st_rdev
= MSVCRT__toupper_l(*path
, NULL
) - 'A'; /* drive num */
3010 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3012 /* Dir, or regular file? */
3013 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3014 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3017 mode
|= MSVCRT__S_IFREG
;
3019 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3021 unsigned int ext
= MSVCRT__tolower_l(path
[plen
-1], NULL
) |
3022 (MSVCRT__tolower_l(path
[plen
-2], NULL
) << 8) |
3023 (MSVCRT__tolower_l(path
[plen
-3], NULL
) << 16);
3024 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3025 mode
|= ALL_S_IEXEC
;
3029 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3030 mode
|= ALL_S_IWRITE
;
3032 buf
->st_mode
= mode
;
3034 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3035 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3037 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3038 buf
->st_mtime
= buf
->st_ctime
= dw
;
3039 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3040 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3041 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3045 /*********************************************************************
3046 * _stati64 (MSVCRT.@)
3048 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
3051 struct MSVCRT__stat64 buf64
;
3053 ret
= MSVCRT_stat64(path
, &buf64
);
3055 msvcrt_stat64_to_stati64(&buf64
, buf
);
3059 /*********************************************************************
3062 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
3065 struct MSVCRT__stat64 buf64
;
3067 ret
= MSVCRT_stat64( path
, &buf64
);
3069 msvcrt_stat64_to_stat(&buf64
, buf
);
3073 #if _MSVCR_VER >= 80
3075 /*********************************************************************
3076 * _stat32 (MSVCR80.@)
3078 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
3081 struct MSVCRT__stat64 buf64
;
3083 ret
= MSVCRT_stat64(path
, &buf64
);
3085 msvcrt_stat64_to_stat32(&buf64
, buf
);
3089 /*********************************************************************
3090 * _stat32i64 (MSVCR80.@)
3092 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
3095 struct MSVCRT__stat64 buf64
;
3097 ret
= MSVCRT_stat64(path
, &buf64
);
3099 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3103 /*********************************************************************
3104 * _stat64i32 (MSVCR80.@)
3106 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
3109 struct MSVCRT__stat64 buf64
;
3111 ret
= MSVCRT_stat64(path
, &buf64
);
3113 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3117 #endif /* _MSVCR_VER >= 80 */
3119 /*********************************************************************
3120 * _wstat64 (MSVCRT.@)
3122 int CDECL
MSVCRT__wstat64(const wchar_t* path
, struct MSVCRT__stat64
* buf
)
3125 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3126 unsigned short mode
= ALL_S_IREAD
;
3129 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3131 plen
= MSVCRT_wcslen(path
);
3132 while (plen
&& path
[plen
-1]==' ')
3135 if (plen
==2 && path
[1]==':')
3137 *MSVCRT__errno() = MSVCRT_ENOENT
;
3142 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3144 *MSVCRT__errno() = MSVCRT_ENOENT
;
3149 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3151 TRACE("failed (%d)\n",GetLastError());
3152 *MSVCRT__errno() = MSVCRT_ENOENT
;
3156 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3158 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3159 if (MSVCRT_iswalpha(*path
) && path
[1] == ':')
3160 buf
->st_dev
= buf
->st_rdev
= MSVCRT_towupper(*path
) - 'A'; /* drive num */
3162 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3164 /* Dir, or regular file? */
3165 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3166 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3169 mode
|= MSVCRT__S_IFREG
;
3171 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3173 ULONGLONG ext
= MSVCRT_towlower(path
[plen
-1]) | (MSVCRT_towlower(path
[plen
-2]) << 16) |
3174 ((ULONGLONG
)MSVCRT_towlower(path
[plen
-3]) << 32);
3175 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3176 mode
|= ALL_S_IEXEC
;
3180 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3181 mode
|= ALL_S_IWRITE
;
3183 buf
->st_mode
= mode
;
3185 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3186 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3188 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3189 buf
->st_mtime
= buf
->st_ctime
= dw
;
3190 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3191 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3192 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3196 /*********************************************************************
3197 * _wstati64 (MSVCRT.@)
3199 int CDECL
MSVCRT__wstati64(const wchar_t* path
, struct MSVCRT__stati64
* buf
)
3202 struct MSVCRT__stat64 buf64
;
3204 ret
= MSVCRT__wstat64(path
, &buf64
);
3206 msvcrt_stat64_to_stati64(&buf64
, buf
);
3210 /*********************************************************************
3213 int CDECL
MSVCRT__wstat(const wchar_t* path
, struct MSVCRT__stat
* buf
)
3216 struct MSVCRT__stat64 buf64
;
3218 ret
= MSVCRT__wstat64( path
, &buf64
);
3219 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3223 #if _MSVCR_VER >= 80
3225 /*********************************************************************
3226 * _wstat32 (MSVCR80.@)
3228 int CDECL
MSVCRT__wstat32(const wchar_t *path
, struct MSVCRT__stat32
*buf
)
3231 struct MSVCRT__stat64 buf64
;
3233 ret
= MSVCRT__wstat64(path
, &buf64
);
3235 msvcrt_stat64_to_stat32(&buf64
, buf
);
3239 /*********************************************************************
3240 * _wstat32i64 (MSVCR80.@)
3242 int CDECL
MSVCRT__wstat32i64(const wchar_t *path
, struct MSVCRT__stat32i64
*buf
)
3245 struct MSVCRT__stat64 buf64
;
3247 ret
= MSVCRT__wstat64(path
, &buf64
);
3249 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3253 /*********************************************************************
3254 * _wstat64i32 (MSVCR80.@)
3256 int CDECL
MSVCRT__wstat64i32(const wchar_t *path
, struct MSVCRT__stat64i32
*buf
)
3259 struct MSVCRT__stat64 buf64
;
3261 ret
= MSVCRT__wstat64(path
, &buf64
);
3263 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3267 #endif /* _MSVCR_VER >= 80 */
3269 /*********************************************************************
3272 __msvcrt_long CDECL
MSVCRT__tell(int fd
)
3274 return MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
3277 /*********************************************************************
3278 * _telli64 (MSVCRT.@)
3280 __int64 CDECL
_telli64(int fd
)
3282 return MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
3285 /*********************************************************************
3286 * _tempnam (MSVCRT.@)
3288 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3290 char tmpbuf
[MAX_PATH
];
3291 const char *tmp_dir
= MSVCRT_getenv("TMP");
3293 if (tmp_dir
) dir
= tmp_dir
;
3295 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3296 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3298 TRACE("got name (%s)\n",tmpbuf
);
3299 DeleteFileA(tmpbuf
);
3300 return MSVCRT__strdup(tmpbuf
);
3302 TRACE("failed (%d)\n",GetLastError());
3306 /*********************************************************************
3307 * _wtempnam (MSVCRT.@)
3309 wchar_t * CDECL
MSVCRT__wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
3311 wchar_t tmpbuf
[MAX_PATH
];
3312 const wchar_t *tmp_dir
= MSVCRT__wgetenv(L
"TMP");
3314 if (tmp_dir
) dir
= tmp_dir
;
3316 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3317 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3319 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3320 DeleteFileW(tmpbuf
);
3321 return MSVCRT__wcsdup(tmpbuf
);
3323 TRACE("failed (%d)\n",GetLastError());
3327 /*********************************************************************
3330 int CDECL
MSVCRT__umask(int umask
)
3332 int old_umask
= MSVCRT_umask
;
3333 TRACE("(%d)\n",umask
);
3334 MSVCRT_umask
= umask
;
3338 /*********************************************************************
3339 * _utime64 (MSVCRT.@)
3341 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3343 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3347 int retVal
= _futime64(fd
, t
);
3354 /*********************************************************************
3355 * _utime32 (MSVCRT.@)
3357 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3361 struct MSVCRT___utimbuf64 t64
;
3362 t64
.actime
= t
->actime
;
3363 t64
.modtime
= t
->modtime
;
3364 return _utime64( path
, &t64
);
3367 return _utime64( path
, NULL
);
3370 /*********************************************************************
3374 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3376 return _utime64( path
, t
);
3379 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3381 return _utime32( path
, t
);
3385 /*********************************************************************
3386 * _wutime64 (MSVCRT.@)
3388 int CDECL
_wutime64(const wchar_t* path
, struct MSVCRT___utimbuf64
*t
)
3390 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3394 int retVal
= _futime64(fd
, t
);
3401 /*********************************************************************
3402 * _wutime32 (MSVCRT.@)
3404 int CDECL
_wutime32(const wchar_t* path
, struct MSVCRT___utimbuf32
*t
)
3408 struct MSVCRT___utimbuf64 t64
;
3409 t64
.actime
= t
->actime
;
3410 t64
.modtime
= t
->modtime
;
3411 return _wutime64( path
, &t64
);
3414 return _wutime64( path
, NULL
);
3417 /*********************************************************************
3418 * _wutime (MSVCRT.@)
3421 int CDECL
_wutime(const wchar_t* path
, struct MSVCRT___utimbuf64
*t
)
3423 return _wutime64( path
, t
);
3426 int CDECL
_wutime(const wchar_t* path
, struct MSVCRT___utimbuf32
*t
)
3428 return _wutime32( path
, t
);
3432 /*********************************************************************
3435 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3437 ioinfo
*info
= get_ioinfo(fd
);
3438 HANDLE hand
= info
->handle
;
3439 DWORD num_written
, i
;
3440 BOOL console
= FALSE
;
3442 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3444 *MSVCRT__errno() = MSVCRT_EBADF
;
3445 release_ioinfo(info
);
3449 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3451 *MSVCRT__errno() = MSVCRT_EINVAL
;
3452 release_ioinfo(info
);
3456 /* If appending, go to EOF */
3457 if (info
->wxflag
& WX_APPEND
)
3458 MSVCRT__lseek(fd
, 0, FILE_END
);
3460 if (!(info
->wxflag
& WX_TEXT
))
3462 if (!WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3463 || num_written
!= count
)
3465 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3466 hand
, GetLastError());
3467 msvcrt_set_errno(GetLastError());
3471 release_ioinfo(info
);
3475 if (MSVCRT__isatty(fd
)) console
= VerifyConsoleIoHandle(hand
);
3476 for (i
= 0; i
< count
;)
3478 const char *s
= buf
;
3482 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)) && console
)
3484 char conv
[sizeof(lfbuf
)];
3487 #if _MSVCR_VER >= 90
3488 if (info
->dbcsBufferUsed
)
3490 conv
[j
++] = info
->dbcsBuffer
;
3491 info
->dbcsBufferUsed
= FALSE
;
3497 for (; i
< count
&& j
< sizeof(conv
)-1 &&
3498 len
< (sizeof(lfbuf
) - 1) / sizeof(WCHAR
); i
++, j
++, len
++)
3500 if (MSVCRT_isleadbyte((unsigned char)s
[i
]))
3506 #if _MSVCR_VER >= 90
3507 info
->dbcsBuffer
= conv
[j
-1];
3508 info
->dbcsBufferUsed
= TRUE
;
3511 *MSVCRT__errno() = MSVCRT_EINVAL
;
3512 release_ioinfo(info
);
3517 else if (s
[i
] == '\n')
3525 len
= MSVCRT_mbstowcs((WCHAR
*)lfbuf
, conv
, len
);
3528 msvcrt_set_errno(GetLastError());
3529 release_ioinfo(info
);
3534 else if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3536 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-1; i
++, j
++)
3543 else if (info
->exflag
& EF_UTF16
|| console
)
3545 for (j
= 0; i
< count
&& j
< sizeof(lfbuf
)-3; i
++, j
++)
3547 if (s
[i
] == '\n' && !s
[i
+1])
3552 lfbuf
[j
++] = s
[i
++];
3558 char conv
[sizeof(lfbuf
)/4];
3560 for (j
= 0; i
< count
&& j
< sizeof(conv
)-3; i
++, j
++)
3562 if (s
[i
] == '\n' && !s
[i
+1])
3571 j
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)conv
, j
/2, lfbuf
, sizeof(lfbuf
), NULL
, NULL
);
3574 msvcrt_set_errno(GetLastError());
3575 release_ioinfo(info
);
3583 if (!WriteConsoleW(hand
, lfbuf
, j
, &num_written
, NULL
))
3586 else if (!WriteFile(hand
, lfbuf
, j
, &num_written
, NULL
))
3591 if (num_written
!= j
)
3593 TRACE("WriteFile/WriteConsoleW (fd %d, hand %p) failed-last error (%d)\n", fd
,
3594 hand
, GetLastError());
3595 msvcrt_set_errno(GetLastError());
3596 release_ioinfo(info
);
3601 release_ioinfo(info
);
3605 /*********************************************************************
3608 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3612 MSVCRT__lock_file(file
);
3613 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3614 if (len
== sizeof(val
)) {
3615 MSVCRT__unlock_file(file
);
3619 file
->_flag
|= MSVCRT__IOERR
;
3620 MSVCRT__unlock_file(file
);
3624 /*********************************************************************
3627 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3631 MSVCRT__lock_file(file
);
3632 ret
= MSVCRT__fclose_nolock(file
);
3633 MSVCRT__unlock_file(file
);
3638 /*********************************************************************
3639 * _fclose_nolock (MSVCRT.@)
3641 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3645 if(!(file
->_flag
& (MSVCRT__IOREAD
| MSVCRT__IOWRT
| MSVCRT__IORW
)))
3652 MSVCRT_free(file
->_tmpfname
);
3653 file
->_tmpfname
= NULL
;
3654 /* flush stdio buffers */
3655 if(file
->_flag
& MSVCRT__IOWRT
)
3656 MSVCRT__fflush_nolock(file
);
3657 if(file
->_flag
& MSVCRT__IOMYBUF
)
3658 MSVCRT_free(file
->_base
);
3660 r
=MSVCRT__close(file
->_file
);
3663 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3666 /*********************************************************************
3669 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3671 return file
->_flag
& MSVCRT__IOEOF
;
3674 /*********************************************************************
3677 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3679 return file
->_flag
& MSVCRT__IOERR
;
3682 /*********************************************************************
3683 * _filbuf (MSVCRT.@)
3685 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3689 if(file
->_flag
& MSVCRT__IOSTRG
)
3692 /* Allocate buffer if needed */
3693 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3694 msvcrt_alloc_buffer(file
);
3696 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3697 if(file
->_flag
& MSVCRT__IORW
)
3698 file
->_flag
|= MSVCRT__IOREAD
;
3703 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3705 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3706 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3712 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3714 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3720 file
->_ptr
= file
->_base
+1;
3721 c
= *(unsigned char *)file
->_base
;
3726 /*********************************************************************
3729 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3733 MSVCRT__lock_file(file
);
3734 ret
= MSVCRT__fgetc_nolock(file
);
3735 MSVCRT__unlock_file(file
);
3740 /*********************************************************************
3741 * _fgetc_nolock (MSVCRT.@)
3743 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3750 i
= (unsigned char *)file
->_ptr
++;
3753 j
= MSVCRT__filbuf(file
);
3758 /*********************************************************************
3759 * _fgetchar (MSVCRT.@)
3761 int CDECL
MSVCRT__fgetchar(void)
3763 return MSVCRT_fgetc(MSVCRT_stdin
);
3766 /*********************************************************************
3769 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3771 int cc
= MSVCRT_EOF
;
3772 char * buf_start
= s
;
3774 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3775 file
,file
->_file
,s
,size
);
3777 MSVCRT__lock_file(file
);
3779 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3784 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3786 TRACE(":nothing read\n");
3787 MSVCRT__unlock_file(file
);
3790 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3793 TRACE(":got %s\n", debugstr_a(buf_start
));
3794 MSVCRT__unlock_file(file
);
3798 /*********************************************************************
3801 wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3805 MSVCRT__lock_file(file
);
3806 ret
= MSVCRT__fgetwc_nolock(file
);
3807 MSVCRT__unlock_file(file
);
3812 /*********************************************************************
3813 * _fgetwc_nolock (MSVCRT.@)
3815 wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3820 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3821 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3824 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
3825 ch
= MSVCRT__fgetc_nolock(file
);
3826 if(ch
== MSVCRT_EOF
) {
3833 char mbs
[MB_LEN_MAX
];
3836 ch
= MSVCRT__fgetc_nolock(file
);
3837 if(ch
!= MSVCRT_EOF
) {
3839 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3840 ch
= MSVCRT__fgetc_nolock(file
);
3841 if(ch
!= MSVCRT_EOF
) {
3850 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3857 /*********************************************************************
3860 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3867 MSVCRT__lock_file(file
);
3868 for (j
=0; j
<sizeof(int); j
++) {
3869 k
= MSVCRT__fgetc_nolock(file
);
3870 if (k
== MSVCRT_EOF
) {
3871 file
->_flag
|= MSVCRT__IOEOF
;
3872 MSVCRT__unlock_file(file
);
3878 MSVCRT__unlock_file(file
);
3882 /*********************************************************************
3885 wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3887 return MSVCRT_fgetwc(file
);
3890 /*********************************************************************
3891 * _fgetwchar (MSVCRT.@)
3893 wint_t CDECL
MSVCRT__fgetwchar(void)
3895 return MSVCRT_fgetwc(MSVCRT_stdin
);
3898 /*********************************************************************
3899 * getwchar (MSVCRT.@)
3901 wint_t CDECL
MSVCRT_getwchar(void)
3903 return MSVCRT__fgetwchar();
3906 /*********************************************************************
3909 wchar_t * CDECL
MSVCRT_fgetws(wchar_t *s
, int size
, MSVCRT_FILE
* file
)
3912 wchar_t * buf_start
= s
;
3914 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3915 file
,file
->_file
,s
,size
);
3917 MSVCRT__lock_file(file
);
3919 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != WEOF
&& cc
!= '\n')
3924 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3926 TRACE(":nothing read\n");
3927 MSVCRT__unlock_file(file
);
3930 if ((cc
!= WEOF
) && (size
> 1))
3933 TRACE(":got %s\n", debugstr_w(buf_start
));
3934 MSVCRT__unlock_file(file
);
3938 /*********************************************************************
3939 * _flsbuf (MSVCRT.@)
3941 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3943 /* Flush output buffer */
3944 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3945 msvcrt_alloc_buffer(file
);
3948 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3949 if(!(file
->_flag
& MSVCRT__IORW
)) {
3950 file
->_flag
|= MSVCRT__IOERR
;
3951 *MSVCRT__errno() = MSVCRT_EBADF
;
3954 file
->_flag
|= MSVCRT__IOWRT
;
3956 if(file
->_flag
& MSVCRT__IOREAD
) {
3957 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3958 file
->_flag
|= MSVCRT__IOERR
;
3962 file
->_ptr
= file
->_base
;
3963 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3966 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3969 if(file
->_cnt
<= 0) {
3970 res
= msvcrt_flush_buffer(file
);
3973 file
->_flag
|= MSVCRT__IOWRT
;
3974 file
->_cnt
=file
->_bufsiz
;
3982 /* set _cnt to 0 for unbuffered FILEs */
3984 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3987 file
->_flag
|= MSVCRT__IOERR
;
3992 /*********************************************************************
3995 size_t CDECL
MSVCRT_fwrite(const void *ptr
, size_t size
, size_t nmemb
, MSVCRT_FILE
* file
)
3999 MSVCRT__lock_file(file
);
4000 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
4001 MSVCRT__unlock_file(file
);
4006 /*********************************************************************
4007 * _fwrite_nolock (MSVCRT.@)
4009 size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, size_t size
, size_t nmemb
, MSVCRT_FILE
* file
)
4011 size_t wrcnt
=size
* nmemb
;
4017 if(file
->_cnt
< 0) {
4018 WARN("negative file->_cnt value in %p\n", file
);
4019 file
->_flag
|= MSVCRT__IOERR
;
4021 } else if(file
->_cnt
) {
4022 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
4023 memcpy(file
->_ptr
, ptr
, pcnt
);
4028 ptr
= (const char*)ptr
+ pcnt
;
4029 } else if((file
->_flag
& MSVCRT__IONBF
)
4030 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4031 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4035 if(file
->_flag
& MSVCRT__IONBF
)
4037 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4038 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4040 bufsiz
= file
->_bufsiz
;
4042 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4044 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
4047 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
4048 file
->_flag
|= MSVCRT__IOERR
;
4053 ptr
= (const char*)ptr
+ pcnt
;
4055 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
4059 ptr
= (const char*)ptr
+ 1;
4063 return written
/ size
;
4066 /*********************************************************************
4069 wint_t CDECL
MSVCRT_fputwc(wint_t wc
, MSVCRT_FILE
* file
)
4073 MSVCRT__lock_file(file
);
4074 ret
= MSVCRT__fputwc_nolock(wc
, file
);
4075 MSVCRT__unlock_file(file
);
4080 /*********************************************************************
4081 * _fputwc_nolock (MSVCRT.@)
4083 wint_t CDECL
MSVCRT__fputwc_nolock(wint_t wc
, MSVCRT_FILE
* file
)
4089 fdinfo
= get_ioinfo_nolock(file
->_file
);
4091 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
4092 char buf
[MB_LEN_MAX
];
4095 char_len
= MSVCRT_wctomb(buf
, mwc
);
4096 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
4100 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4109 /*********************************************************************
4110 * _fputwchar (MSVCRT.@)
4112 wint_t CDECL
MSVCRT__fputwchar(wint_t wc
)
4114 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
4117 /*********************************************************************
4118 * _wfsopen (MSVCRT.@)
4120 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
4123 int open_flags
, stream_flags
, fd
;
4125 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4127 /* map mode string to open() flags. "man fopen" for possibilities. */
4128 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4132 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4135 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4137 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4144 TRACE(":got (%p)\n",file
);
4145 if (fd
>= 0 && !file
)
4151 /*********************************************************************
4152 * _fsopen (MSVCRT.@)
4154 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4157 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4159 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4160 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4161 *MSVCRT__errno() = MSVCRT_EINVAL
;
4164 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4167 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4168 *MSVCRT__errno() = MSVCRT_EINVAL
;
4172 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4179 /*********************************************************************
4182 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4184 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4187 /*********************************************************************
4188 * fopen_s (MSVCRT.@)
4190 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4191 const char *filename
, const char *mode
)
4193 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4194 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4195 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4197 *pFile
= MSVCRT_fopen(filename
, mode
);
4200 return *MSVCRT__errno();
4204 /*********************************************************************
4205 * _wfopen (MSVCRT.@)
4207 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const wchar_t *path
, const wchar_t *mode
)
4209 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4212 /*********************************************************************
4213 * _wfopen_s (MSVCRT.@)
4215 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const wchar_t *filename
,
4216 const wchar_t *mode
)
4218 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4219 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4220 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4222 *pFile
= MSVCRT__wfopen(filename
, mode
);
4225 return *MSVCRT__errno();
4229 /*********************************************************************
4232 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4236 MSVCRT__lock_file(file
);
4237 ret
= MSVCRT__fputc_nolock(c
, file
);
4238 MSVCRT__unlock_file(file
);
4243 /*********************************************************************
4244 * _fputc_nolock (MSVCRT.@)
4246 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4255 res
= msvcrt_flush_buffer(file
);
4256 return res
? res
: c
;
4262 res
= MSVCRT__flsbuf(c
, file
);
4267 /*********************************************************************
4268 * _fputchar (MSVCRT.@)
4270 int CDECL
MSVCRT__fputchar(int c
)
4272 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4275 /*********************************************************************
4278 size_t CDECL
MSVCRT_fread(void *ptr
, size_t size
, size_t nmemb
, MSVCRT_FILE
* file
)
4282 MSVCRT__lock_file(file
);
4283 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4284 MSVCRT__unlock_file(file
);
4289 /*********************************************************************
4290 * _fread_nolock (MSVCRT.@)
4292 size_t CDECL
MSVCRT__fread_nolock(void *ptr
, size_t size
, size_t nmemb
, MSVCRT_FILE
* file
)
4294 size_t rcnt
=size
* nmemb
;
4301 /* first buffered data */
4303 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4304 memcpy(ptr
, file
->_ptr
, pcnt
);
4309 ptr
= (char*)ptr
+ pcnt
;
4310 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4311 if(file
->_flag
& MSVCRT__IORW
) {
4312 file
->_flag
|= MSVCRT__IOREAD
;
4318 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4319 msvcrt_alloc_buffer(file
);
4324 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4325 i
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4326 file
->_ptr
= file
->_base
;
4329 if (i
> rcnt
) i
= rcnt
;
4331 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4332 if (i
> 0 && i
< file
->_cnt
) {
4333 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4334 file
->_flag
&= ~MSVCRT__IOEOF
;
4337 memcpy(ptr
, file
->_ptr
, i
);
4341 } else if (rcnt
> INT_MAX
) {
4342 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4343 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4344 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4346 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4350 ptr
= (char *)ptr
+i
;
4351 /* expose feof condition in the flags
4352 * MFC tests file->_flag for feof, and doesn't call feof())
4354 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4355 file
->_flag
|= MSVCRT__IOEOF
;
4358 file
->_flag
|= MSVCRT__IOERR
;
4368 #if _MSVCR_VER >= 80
4370 /*********************************************************************
4371 * fread_s (MSVCR80.@)
4373 size_t CDECL
MSVCRT_fread_s(void *buf
, size_t buf_size
, size_t elem_size
,
4374 size_t count
, MSVCRT_FILE
*stream
)
4378 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4380 memset(buf
, 0, buf_size
);
4383 if(!elem_size
|| !count
) return 0;
4385 MSVCRT__lock_file(stream
);
4386 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4387 MSVCRT__unlock_file(stream
);
4392 /*********************************************************************
4393 * _fread_nolock_s (MSVCR80.@)
4395 size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, size_t buf_size
, size_t elem_size
,
4396 size_t count
, MSVCRT_FILE
*stream
)
4398 size_t bytes_left
, buf_pos
;
4400 TRACE("(%p %Iu %Iu %Iu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4402 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4404 memset(buf
, 0, buf_size
);
4407 if(!elem_size
|| !count
) return 0;
4408 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4409 if(!MSVCRT_CHECK_PMT(SIZE_MAX
/count
>= elem_size
)) return 0;
4411 bytes_left
= elem_size
*count
;
4414 if(stream
->_cnt
> 0) {
4415 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4417 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4418 memset(buf
, 0, buf_size
);
4422 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4426 int c
= MSVCRT__filbuf(stream
);
4431 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4432 memset(buf
, 0, buf_size
);
4436 ((char*)buf
)[buf_pos
++] = c
;
4441 return buf_pos
/elem_size
;
4444 #endif /* _MSVCR_VER >= 80 */
4446 /*********************************************************************
4447 * _wfreopen (MSVCRT.@)
4450 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const wchar_t *path
, const wchar_t *mode
, MSVCRT_FILE
* file
)
4452 int open_flags
, stream_flags
, fd
;
4454 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4457 if (!file
|| ((fd
= file
->_file
) < 0))
4461 MSVCRT_fclose(file
);
4462 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4464 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4466 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4476 /*********************************************************************
4477 * _wfreopen_s (MSVCRT.@)
4479 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4480 const wchar_t *path
, const wchar_t *mode
, MSVCRT_FILE
* file
)
4482 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4483 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4484 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4485 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4487 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4490 return *MSVCRT__errno();
4494 /*********************************************************************
4495 * freopen (MSVCRT.@)
4498 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4501 wchar_t *pathW
= NULL
, *modeW
= NULL
;
4503 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4504 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4510 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4517 /*********************************************************************
4518 * freopen_s (MSVCRT.@)
4520 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4521 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4523 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4524 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4525 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4526 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4528 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4531 return *MSVCRT__errno();
4535 /*********************************************************************
4536 * fsetpos (MSVCRT.@)
4538 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, fpos_t *pos
)
4542 MSVCRT__lock_file(file
);
4543 msvcrt_flush_buffer(file
);
4545 /* Reset direction of i/o */
4546 if(file
->_flag
& MSVCRT__IORW
) {
4547 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4550 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,MSVCRT_SEEK_SET
) == -1) ? -1 : 0;
4551 MSVCRT__unlock_file(file
);
4555 /*********************************************************************
4556 * _ftelli64 (MSVCRT.@)
4558 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4562 MSVCRT__lock_file(file
);
4563 ret
= MSVCRT__ftelli64_nolock(file
);
4564 MSVCRT__unlock_file(file
);
4569 /*********************************************************************
4570 * _ftelli64_nolock (MSVCRT.@)
4572 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4576 pos
= _telli64(file
->_file
);
4579 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4580 if(file
->_flag
& MSVCRT__IOWRT
) {
4581 pos
+= file
->_ptr
- file
->_base
;
4583 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4586 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4590 } else if(!file
->_cnt
) { /* nothing to do */
4591 } else if(MSVCRT__lseeki64(file
->_file
, 0, MSVCRT_SEEK_END
)==pos
) {
4595 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4596 for(i
=0; i
<file
->_cnt
; i
++)
4597 if(file
->_ptr
[i
] == '\n')
4603 if(MSVCRT__lseeki64(file
->_file
, pos
, MSVCRT_SEEK_SET
) != pos
)
4606 pos
-= file
->_bufsiz
;
4607 pos
+= file
->_ptr
- file
->_base
;
4609 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4610 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4613 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4623 /*********************************************************************
4626 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4628 return MSVCRT__ftelli64(file
);
4631 #if _MSVCR_VER >= 80
4632 /*********************************************************************
4633 * _ftell_nolock (MSVCR80.@)
4635 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4637 return MSVCRT__ftelli64_nolock(file
);
4641 /*********************************************************************
4642 * fgetpos (MSVCRT.@)
4644 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, fpos_t *pos
)
4646 *pos
= MSVCRT__ftelli64(file
);
4652 /*********************************************************************
4655 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4657 size_t len
= strlen(s
);
4660 MSVCRT__lock_file(file
);
4661 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4662 MSVCRT__unlock_file(file
);
4666 /*********************************************************************
4669 int CDECL
MSVCRT_fputws(const wchar_t *s
, MSVCRT_FILE
* file
)
4671 size_t i
, len
= MSVCRT_wcslen(s
);
4675 MSVCRT__lock_file(file
);
4676 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4677 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4678 MSVCRT__unlock_file(file
);
4682 tmp_buf
= add_std_buffer(file
);
4683 for (i
=0; i
<len
; i
++) {
4684 if(MSVCRT__fputwc_nolock(s
[i
], file
) == WEOF
) {
4685 if(tmp_buf
) remove_std_buffer(file
);
4686 MSVCRT__unlock_file(file
);
4691 if(tmp_buf
) remove_std_buffer(file
);
4692 MSVCRT__unlock_file(file
);
4696 /*********************************************************************
4697 * getchar (MSVCRT.@)
4699 int CDECL
MSVCRT_getchar(void)
4701 return MSVCRT_fgetc(MSVCRT_stdin
);
4704 /*********************************************************************
4707 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4709 return MSVCRT_fgetc(file
);
4712 /*********************************************************************
4713 * gets_s (MSVCR80.@)
4715 char * CDECL
MSVCRT_gets_s(char *buf
, size_t len
)
4717 char *buf_start
= buf
;
4720 if (!MSVCRT_CHECK_PMT(buf
!= NULL
)) return NULL
;
4721 if (!MSVCRT_CHECK_PMT(len
!= 0)) return NULL
;
4723 MSVCRT__lock_file(MSVCRT_stdin
);
4724 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
);
4725 len
!= 0 && cc
!= MSVCRT_EOF
&& cc
!= '\n';
4726 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4734 MSVCRT__unlock_file(MSVCRT_stdin
);
4739 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4743 if ((cc
== MSVCRT_EOF
) && (buf_start
== buf
))
4745 TRACE(":nothing read\n");
4750 TRACE("got '%s'\n", buf_start
);
4754 /*********************************************************************
4757 char * CDECL
MSVCRT_gets(char *buf
)
4759 return MSVCRT_gets_s(buf
, -1);
4762 /*********************************************************************
4765 wchar_t* CDECL
MSVCRT__getws(wchar_t* buf
)
4770 MSVCRT__lock_file(MSVCRT_stdin
);
4771 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= WEOF
&& cc
!= '\n';
4772 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4775 *buf
++ = (wchar_t)cc
;
4777 MSVCRT__unlock_file(MSVCRT_stdin
);
4779 if ((cc
== WEOF
) && (ws
== buf
))
4781 TRACE(":nothing read\n");
4786 TRACE("got %s\n", debugstr_w(ws
));
4790 /*********************************************************************
4793 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4795 return MSVCRT_fputc(c
, file
);
4798 /*********************************************************************
4799 * putchar (MSVCRT.@)
4801 int CDECL
MSVCRT_putchar(int c
)
4803 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4806 /*********************************************************************
4809 int CDECL
MSVCRT_puts(const char *s
)
4811 size_t len
= strlen(s
);
4814 MSVCRT__lock_file(MSVCRT_stdout
);
4815 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4816 MSVCRT__unlock_file(MSVCRT_stdout
);
4820 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4821 MSVCRT__unlock_file(MSVCRT_stdout
);
4825 /*********************************************************************
4828 int CDECL
MSVCRT__putws(const wchar_t *s
)
4832 MSVCRT__lock_file(MSVCRT_stdout
);
4833 ret
= MSVCRT_fputws(s
, MSVCRT_stdout
);
4835 ret
= MSVCRT__fputwc_nolock('\n', MSVCRT_stdout
);
4836 MSVCRT__unlock_file(MSVCRT_stdout
);
4837 return ret
>= 0 ? 0 : WEOF
;
4840 /*********************************************************************
4843 int CDECL
MSVCRT_remove(const char *path
)
4845 TRACE("(%s)\n",path
);
4846 if (DeleteFileA(path
))
4848 TRACE(":failed (%d)\n",GetLastError());
4849 msvcrt_set_errno(GetLastError());
4853 /*********************************************************************
4854 * _wremove (MSVCRT.@)
4856 int CDECL
MSVCRT__wremove(const wchar_t *path
)
4858 TRACE("(%s)\n",debugstr_w(path
));
4859 if (DeleteFileW(path
))
4861 TRACE(":failed (%d)\n",GetLastError());
4862 msvcrt_set_errno(GetLastError());
4866 /*********************************************************************
4869 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4871 TRACE(":from %s to %s\n",oldpath
,newpath
);
4872 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4874 TRACE(":failed (%d)\n",GetLastError());
4875 msvcrt_set_errno(GetLastError());
4879 /*********************************************************************
4880 * _wrename (MSVCRT.@)
4882 int CDECL
MSVCRT__wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
4884 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4885 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4887 TRACE(":failed (%d)\n",GetLastError());
4888 msvcrt_set_errno(GetLastError());
4892 /*********************************************************************
4893 * setvbuf (MSVCRT.@)
4895 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, size_t size
)
4897 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4898 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4899 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4901 MSVCRT__lock_file(file
);
4903 MSVCRT__fflush_nolock(file
);
4904 if(file
->_flag
& MSVCRT__IOMYBUF
)
4905 MSVCRT_free(file
->_base
);
4906 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4909 if(mode
== MSVCRT__IONBF
) {
4910 file
->_flag
|= MSVCRT__IONBF
;
4911 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4914 file
->_base
= file
->_ptr
= buf
;
4915 file
->_flag
|= MSVCRT__USERBUF
;
4916 file
->_bufsiz
= size
;
4918 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4921 MSVCRT__unlock_file(file
);
4925 file
->_flag
|= MSVCRT__IOMYBUF
;
4926 file
->_bufsiz
= size
;
4928 MSVCRT__unlock_file(file
);
4932 /*********************************************************************
4935 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4937 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4940 static int tmpnam_helper(char *s
, size_t size
, int *tmpnam_unique
, int tmp_max
)
4946 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
4950 *MSVCRT__errno() = MSVCRT_ERANGE
;
4951 return MSVCRT_ERANGE
;
4956 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4957 if (digits
+1 > size
) {
4959 *MSVCRT__errno() = MSVCRT_ERANGE
;
4960 return MSVCRT_ERANGE
;
4962 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4968 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4969 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4973 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4974 if (digits
+1 > size
) {
4976 *MSVCRT__errno() = MSVCRT_ERANGE
;
4977 return MSVCRT_ERANGE
;
4979 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4982 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4983 GetLastError() == ERROR_FILE_NOT_FOUND
)
4989 int CDECL
MSVCRT_tmpnam_s(char *s
, size_t size
)
4991 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
4994 /*********************************************************************
4997 char * CDECL
MSVCRT_tmpnam(char *s
)
5000 thread_data_t
*data
= msvcrt_get_thread_data();
5002 if(!data
->tmpnam_buffer
)
5003 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
5005 s
= data
->tmpnam_buffer
;
5008 return tmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5011 static int wtmpnam_helper(wchar_t *s
, size_t size
, int *tmpnam_unique
, int tmp_max
)
5017 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
5021 *MSVCRT__errno() = MSVCRT_ERANGE
;
5022 return MSVCRT_ERANGE
;
5027 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5028 if (digits
+1 > size
) {
5030 *MSVCRT__errno() = MSVCRT_ERANGE
;
5031 return MSVCRT_ERANGE
;
5033 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5039 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5040 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5044 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5045 if (digits
+1 > size
) {
5047 *MSVCRT__errno() = MSVCRT_ERANGE
;
5048 return MSVCRT_ERANGE
;
5050 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5053 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5054 GetLastError() == ERROR_FILE_NOT_FOUND
)
5060 /*********************************************************************
5061 * _wtmpnam_s (MSVCRT.@)
5063 int CDECL
MSVCRT__wtmpnam_s(wchar_t *s
, size_t size
)
5065 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5068 /*********************************************************************
5069 * _wtmpnam (MSVCRT.@)
5071 wchar_t * CDECL
MSVCRT__wtmpnam(wchar_t *s
)
5074 thread_data_t
*data
= msvcrt_get_thread_data();
5076 if(!data
->wtmpnam_buffer
)
5077 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(wchar_t[MAX_PATH
]));
5079 s
= data
->wtmpnam_buffer
;
5082 return wtmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5085 /*********************************************************************
5086 * tmpfile (MSVCRT.@)
5088 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
5090 char *filename
= MSVCRT__tempnam(",", "t");
5092 MSVCRT_FILE
* file
= NULL
;
5095 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
5096 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
5097 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5099 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
5104 else file
->_tmpfname
= MSVCRT__strdup(filename
);
5107 if(fd
!= -1 && !file
)
5109 MSVCRT_free(filename
);
5114 /*********************************************************************
5115 * tmpfile_s (MSVCRT.@)
5117 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
5119 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
5121 *file
= MSVCRT_tmpfile();
5125 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5127 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
5130 static int puts_clbk_file_w(void *file
, int len
, const wchar_t *str
)
5134 MSVCRT__lock_file(file
);
5136 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5137 ret
= MSVCRT__fwrite_nolock(str
, sizeof(wchar_t), len
, file
);
5138 MSVCRT__unlock_file(file
);
5142 for(i
=0; i
<len
; i
++) {
5143 if(MSVCRT__fputwc_nolock(str
[i
], file
) == WEOF
) {
5144 MSVCRT__unlock_file(file
);
5149 MSVCRT__unlock_file(file
);
5153 static int vfprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const char *format
,
5154 _locale_t locale
, __ms_va_list valist
)
5156 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5160 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5161 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5163 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5164 memset(args_ctx
, 0, sizeof(args_ctx
));
5165 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5167 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5168 *MSVCRT__errno() = MSVCRT_EINVAL
;
5171 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5174 MSVCRT__lock_file(file
);
5175 tmp_buf
= add_std_buffer(file
);
5176 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5177 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5178 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5179 if(tmp_buf
) remove_std_buffer(file
);
5180 MSVCRT__unlock_file(file
);
5185 static int vfwprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const wchar_t *format
,
5186 _locale_t locale
, __ms_va_list valist
)
5188 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5192 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5193 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5195 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5196 memset(args_ctx
, 0, sizeof(args_ctx
));
5197 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5199 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5200 *MSVCRT__errno() = MSVCRT_EINVAL
;
5203 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5206 MSVCRT__lock_file(file
);
5207 tmp_buf
= add_std_buffer(file
);
5208 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5209 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5210 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5211 if(tmp_buf
) remove_std_buffer(file
);
5212 MSVCRT__unlock_file(file
);
5217 /*********************************************************************
5218 * _vfprintf_s_l (MSVCRT.@)
5220 int CDECL
MSVCRT__vfprintf_s_l(MSVCRT_FILE
* file
, const char *format
,
5221 _locale_t locale
, __ms_va_list valist
)
5223 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5226 /*********************************************************************
5227 * _vfwprintf_s_l (MSVCRT.@)
5229 int CDECL
MSVCRT__vfwprintf_s_l(MSVCRT_FILE
* file
, const wchar_t *format
,
5230 _locale_t locale
, __ms_va_list valist
)
5232 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5235 /*********************************************************************
5236 * vfprintf (MSVCRT.@)
5238 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5240 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5243 /*********************************************************************
5244 * vfprintf_s (MSVCRT.@)
5246 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5248 return MSVCRT__vfprintf_s_l(file
, format
, NULL
, valist
);
5251 /*********************************************************************
5252 * vfwprintf (MSVCRT.@)
5254 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const wchar_t *format
, __ms_va_list valist
)
5256 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5259 /*********************************************************************
5260 * vfwprintf_s (MSVCRT.@)
5262 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const wchar_t *format
, __ms_va_list valist
)
5264 return MSVCRT__vfwprintf_s_l(file
, format
, NULL
, valist
);
5267 #if _MSVCR_VER >= 140
5269 /*********************************************************************
5270 * __stdio_common_vfprintf (UCRTBASE.@)
5272 int CDECL
MSVCRT__stdio_common_vfprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5273 _locale_t locale
, __ms_va_list valist
)
5275 if (options
& ~UCRTBASE_PRINTF_MASK
)
5276 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5278 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5281 /*********************************************************************
5282 * __stdio_common_vfprintf_s (UCRTBASE.@)
5284 int CDECL
MSVCRT__stdio_common_vfprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5285 _locale_t locale
, __ms_va_list valist
)
5287 if (options
& ~UCRTBASE_PRINTF_MASK
)
5288 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5290 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5291 file
, format
, locale
, valist
);
5294 /*********************************************************************
5295 * __stdio_common_vfwprintf (UCRTBASE.@)
5297 int CDECL
MSVCRT__stdio_common_vfwprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const wchar_t *format
,
5298 _locale_t locale
, __ms_va_list valist
)
5300 if (options
& ~UCRTBASE_PRINTF_MASK
)
5301 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5303 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5306 /*********************************************************************
5307 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5309 int CDECL
MSVCRT__stdio_common_vfwprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const wchar_t *format
,
5310 _locale_t locale
, __ms_va_list valist
)
5312 if (options
& ~UCRTBASE_PRINTF_MASK
)
5313 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5315 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5316 file
, format
, locale
, valist
);
5319 #endif /* _MSVCR_VER >= 140 */
5321 /*********************************************************************
5322 * _vfprintf_l (MSVCRT.@)
5324 int CDECL
MSVCRT__vfprintf_l(MSVCRT_FILE
* file
, const char *format
,
5325 _locale_t locale
, __ms_va_list valist
)
5327 return vfprintf_helper(0, file
, format
, locale
, valist
);
5330 /*********************************************************************
5331 * _vfwprintf_l (MSVCRT.@)
5333 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const wchar_t *format
,
5334 _locale_t locale
, __ms_va_list valist
)
5336 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5339 /*********************************************************************
5340 * _vfprintf_p_l (MSVCRT.@)
5342 int CDECL
MSVCRT__vfprintf_p_l(MSVCRT_FILE
* file
, const char *format
,
5343 _locale_t locale
, __ms_va_list valist
)
5345 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5346 file
, format
, locale
, valist
);
5349 /*********************************************************************
5350 * _vfprintf_p (MSVCRT.@)
5352 int CDECL
MSVCRT__vfprintf_p(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5354 return MSVCRT__vfprintf_p_l(file
, format
, NULL
, valist
);
5357 /*********************************************************************
5358 * _vfwprintf_p_l (MSVCRT.@)
5360 int CDECL
MSVCRT__vfwprintf_p_l(MSVCRT_FILE
* file
, const wchar_t *format
,
5361 _locale_t locale
, __ms_va_list valist
)
5363 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5364 file
, format
, locale
, valist
);
5367 /*********************************************************************
5368 * _vfwprintf_p (MSVCRT.@)
5370 int CDECL
MSVCRT__vfwprintf_p(MSVCRT_FILE
* file
, const wchar_t *format
, __ms_va_list valist
)
5372 return MSVCRT__vfwprintf_p_l(file
, format
, NULL
, valist
);
5375 /*********************************************************************
5376 * vprintf (MSVCRT.@)
5378 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5380 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5383 /*********************************************************************
5384 * vprintf_s (MSVCRT.@)
5386 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5388 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5391 /*********************************************************************
5392 * vwprintf (MSVCRT.@)
5394 int CDECL
MSVCRT_vwprintf(const wchar_t *format
, __ms_va_list valist
)
5396 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5399 /*********************************************************************
5400 * vwprintf_s (MSVCRT.@)
5402 int CDECL
MSVCRT_vwprintf_s(const wchar_t *format
, __ms_va_list valist
)
5404 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5407 /*********************************************************************
5408 * fprintf (MSVCRT.@)
5410 int WINAPIV
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5412 __ms_va_list valist
;
5414 __ms_va_start(valist
, format
);
5415 res
= MSVCRT_vfprintf(file
, format
, valist
);
5416 __ms_va_end(valist
);
5420 /*********************************************************************
5421 * fprintf_s (MSVCRT.@)
5423 int WINAPIV
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5425 __ms_va_list valist
;
5427 __ms_va_start(valist
, format
);
5428 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5429 __ms_va_end(valist
);
5433 /*********************************************************************
5434 * fwprintf (MSVCRT.@)
5436 int WINAPIV
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const wchar_t *format
, ...)
5438 __ms_va_list valist
;
5440 __ms_va_start(valist
, format
);
5441 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5442 __ms_va_end(valist
);
5446 /*********************************************************************
5447 * fwprintf_s (MSVCRT.@)
5449 int WINAPIV
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const wchar_t *format
, ...)
5451 __ms_va_list valist
;
5453 __ms_va_start(valist
, format
);
5454 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5455 __ms_va_end(valist
);
5459 /*********************************************************************
5460 * _fwprintf_l (MSVCRT.@)
5462 int WINAPIV
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const wchar_t *format
, _locale_t locale
, ...)
5464 __ms_va_list valist
;
5466 __ms_va_start(valist
, locale
);
5467 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5468 __ms_va_end(valist
);
5472 /*********************************************************************
5475 int WINAPIV
MSVCRT_printf(const char *format
, ...)
5477 __ms_va_list valist
;
5479 __ms_va_start(valist
, format
);
5480 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5481 __ms_va_end(valist
);
5485 /*********************************************************************
5486 * printf_s (MSVCRT.@)
5488 int WINAPIV
MSVCRT_printf_s(const char *format
, ...)
5490 __ms_va_list valist
;
5492 __ms_va_start(valist
, format
);
5493 res
= MSVCRT_vprintf_s(format
, valist
);
5494 __ms_va_end(valist
);
5498 /*********************************************************************
5501 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5505 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5507 MSVCRT__lock_file(file
);
5508 ret
= MSVCRT__ungetc_nolock(c
, file
);
5509 MSVCRT__unlock_file(file
);
5514 /*********************************************************************
5515 * _ungetc_nolock (MSVCRT.@)
5517 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5519 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5521 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5522 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5525 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5526 && msvcrt_alloc_buffer(file
))
5527 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5530 if(file
->_ptr
>file
->_base
) {
5532 if(file
->_flag
& MSVCRT__IOSTRG
) {
5533 if(*file
->_ptr
!= c
) {
5541 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5542 file
->_flag
|= MSVCRT__IOREAD
;
5549 /*********************************************************************
5550 * ungetwc (MSVCRT.@)
5552 wint_t CDECL
MSVCRT_ungetwc(wint_t wc
, MSVCRT_FILE
* file
)
5556 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5558 MSVCRT__lock_file(file
);
5559 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5560 MSVCRT__unlock_file(file
);
5565 /*********************************************************************
5566 * _ungetwc_nolock (MSVCRT.@)
5568 wint_t CDECL
MSVCRT__ungetwc_nolock(wint_t wc
, MSVCRT_FILE
* file
)
5572 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return WEOF
;
5576 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5577 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5578 unsigned char * pp
= (unsigned char *)&mwc
;
5581 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
5582 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5586 char mbs
[MB_LEN_MAX
];
5589 len
= MSVCRT_wctomb(mbs
, mwc
);
5593 for(len
--; len
>=0; len
--) {
5594 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5602 /*********************************************************************
5603 * wprintf (MSVCRT.@)
5605 int WINAPIV
MSVCRT_wprintf(const wchar_t *format
, ...)
5607 __ms_va_list valist
;
5609 __ms_va_start(valist
, format
);
5610 res
= MSVCRT_vwprintf(format
, valist
);
5611 __ms_va_end(valist
);
5615 /*********************************************************************
5616 * wprintf_s (MSVCRT.@)
5618 int WINAPIV
MSVCRT_wprintf_s(const wchar_t *format
, ...)
5620 __ms_va_list valist
;
5622 __ms_va_start(valist
, format
);
5623 res
= MSVCRT_vwprintf_s(format
, valist
);
5624 __ms_va_end(valist
);
5628 /*********************************************************************
5629 * _getmaxstdio (MSVCRT.@)
5631 int CDECL
MSVCRT__getmaxstdio(void)
5633 return MSVCRT_max_streams
;
5636 /*********************************************************************
5637 * _setmaxstdio (MSVCRT.@)
5639 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5641 TRACE("%d\n", newmax
);
5643 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5646 MSVCRT_max_streams
= newmax
;
5647 return MSVCRT_max_streams
;
5650 #if _MSVCR_VER >= 140
5651 /*********************************************************************
5652 * _get_stream_buffer_pointers (UCRTBASE.@)
5654 int CDECL
MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE
*file
, char*** base
,
5655 char*** ptr
, int** count
)
5658 *base
= &file
->_base
;
5662 *count
= &file
->_cnt
;