2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
32 #define __iob_func mingw___iob_func
38 #include <sys/types.h>
47 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
53 /* for stat mode, permissions apply to all,owner and group */
54 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
55 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
56 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
58 /* _access() bit flags FIXME: incomplete */
59 #define MSVCRT_W_OK 0x02
60 #define MSVCRT_R_OK 0x04
62 /* values for wxflag in file descriptor */
65 #define WX_READNL 0x04 /* read started with \n */
67 #define WX_DONTINHERIT 0x10
68 #define WX_APPEND 0x20
72 /* values for exflag - it's used differently in msvcr90.dll*/
75 #define EF_CRIT_INIT 0x04
76 #define EF_UNK_UNICODE 0x08
78 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
79 static char utf16_bom
[2] = { 0xff, 0xfe };
81 /* FIXME: this should be allocated dynamically */
82 #define MSVCRT_MAX_FILES 2048
83 #define MSVCRT_FD_BLOCK_SIZE 32
85 #define MSVCRT_INTERNAL_BUFSIZ 4096
87 /* ioinfo structure size is different in msvcrXX.dll's */
93 CRITICAL_SECTION crit
;
99 BOOL utf8translations
;
107 /*********************************************************************
108 * __pioinfo (MSVCRT.@)
109 * array of pointers to ioinfo arrays [32]
111 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
113 /*********************************************************************
114 * __badioinfo (MSVCRT.@)
116 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
120 CRITICAL_SECTION crit
;
123 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
124 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
125 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
127 /* INTERNAL: process umask */
128 static int MSVCRT_umask
= 0;
130 /* INTERNAL: static data for tmpnam and _wtmpname functions */
131 static int tmpnam_unique
;
132 static int tmpnam_s_unique
;
134 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
135 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
136 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
137 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
139 #define TOUL(x) (ULONGLONG)(x)
140 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
141 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
142 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
143 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
145 /* This critical section protects the MSVCRT_fstreams table
146 * and MSVCRT_stream_idx from race conditions. It also
147 * protects fd critical sections creation code.
149 static CRITICAL_SECTION MSVCRT_file_cs
;
150 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
152 0, 0, &MSVCRT_file_cs
,
153 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
154 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
156 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
157 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
158 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
160 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
162 buf
->st_dev
= buf64
->st_dev
;
163 buf
->st_ino
= buf64
->st_ino
;
164 buf
->st_mode
= buf64
->st_mode
;
165 buf
->st_nlink
= buf64
->st_nlink
;
166 buf
->st_uid
= buf64
->st_uid
;
167 buf
->st_gid
= buf64
->st_gid
;
168 buf
->st_rdev
= buf64
->st_rdev
;
169 buf
->st_size
= buf64
->st_size
;
170 buf
->st_atime
= buf64
->st_atime
;
171 buf
->st_mtime
= buf64
->st_mtime
;
172 buf
->st_ctime
= buf64
->st_ctime
;
175 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
177 buf
->st_dev
= buf64
->st_dev
;
178 buf
->st_ino
= buf64
->st_ino
;
179 buf
->st_mode
= buf64
->st_mode
;
180 buf
->st_nlink
= buf64
->st_nlink
;
181 buf
->st_uid
= buf64
->st_uid
;
182 buf
->st_gid
= buf64
->st_gid
;
183 buf
->st_rdev
= buf64
->st_rdev
;
184 buf
->st_size
= buf64
->st_size
;
185 buf
->st_atime
= buf64
->st_atime
;
186 buf
->st_mtime
= buf64
->st_mtime
;
187 buf
->st_ctime
= buf64
->st_ctime
;
190 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
192 buf
->st_dev
= buf64
->st_dev
;
193 buf
->st_ino
= buf64
->st_ino
;
194 buf
->st_mode
= buf64
->st_mode
;
195 buf
->st_nlink
= buf64
->st_nlink
;
196 buf
->st_uid
= buf64
->st_uid
;
197 buf
->st_gid
= buf64
->st_gid
;
198 buf
->st_rdev
= buf64
->st_rdev
;
199 buf
->st_size
= buf64
->st_size
;
200 buf
->st_atime
= buf64
->st_atime
;
201 buf
->st_mtime
= buf64
->st_mtime
;
202 buf
->st_ctime
= buf64
->st_ctime
;
205 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
207 buf
->st_dev
= buf64
->st_dev
;
208 buf
->st_ino
= buf64
->st_ino
;
209 buf
->st_mode
= buf64
->st_mode
;
210 buf
->st_nlink
= buf64
->st_nlink
;
211 buf
->st_uid
= buf64
->st_uid
;
212 buf
->st_gid
= buf64
->st_gid
;
213 buf
->st_rdev
= buf64
->st_rdev
;
214 buf
->st_size
= buf64
->st_size
;
215 buf
->st_atime
= buf64
->st_atime
;
216 buf
->st_mtime
= buf64
->st_mtime
;
217 buf
->st_ctime
= buf64
->st_ctime
;
220 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
222 buf
->st_dev
= buf64
->st_dev
;
223 buf
->st_ino
= buf64
->st_ino
;
224 buf
->st_mode
= buf64
->st_mode
;
225 buf
->st_nlink
= buf64
->st_nlink
;
226 buf
->st_uid
= buf64
->st_uid
;
227 buf
->st_gid
= buf64
->st_gid
;
228 buf
->st_rdev
= buf64
->st_rdev
;
229 buf
->st_size
= buf64
->st_size
;
230 buf
->st_atime
= buf64
->st_atime
;
231 buf
->st_mtime
= buf64
->st_mtime
;
232 buf
->st_ctime
= buf64
->st_ctime
;
235 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
237 /* 1601 to 1970 is 369 years plus 89 leap days */
238 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
240 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
241 ft
->dwHighDateTime
= ticks
>> 32;
242 ft
->dwLowDateTime
= ticks
;
245 static inline ioinfo
* get_ioinfo_nolock(int fd
)
248 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
249 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
251 return &MSVCRT___badioinfo
;
253 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
256 static inline void init_ioinfo_cs(ioinfo
*info
)
258 if(!(info
->exflag
& EF_CRIT_INIT
)) {
260 if(!(info
->exflag
& EF_CRIT_INIT
)) {
261 InitializeCriticalSection(&info
->crit
);
262 info
->exflag
|= EF_CRIT_INIT
;
268 static inline ioinfo
* get_ioinfo(int fd
)
270 ioinfo
*ret
= get_ioinfo_nolock(fd
);
271 if(ret
== &MSVCRT___badioinfo
)
274 EnterCriticalSection(&ret
->crit
);
278 static inline BOOL
alloc_pioinfo_block(int fd
)
283 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
285 *MSVCRT__errno() = MSVCRT_ENFILE
;
289 block
= MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
292 WARN(":out of memory!\n");
293 *MSVCRT__errno() = MSVCRT_ENOMEM
;
296 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
297 block
[i
].handle
= INVALID_HANDLE_VALUE
;
298 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
303 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
307 ret
= get_ioinfo(fd
);
308 if(ret
!= &MSVCRT___badioinfo
)
311 if(!alloc_pioinfo_block(fd
))
312 return &MSVCRT___badioinfo
;
314 return get_ioinfo(fd
);
317 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
322 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
324 ioinfo
*info
= get_ioinfo_nolock(i
);
326 if(info
== &MSVCRT___badioinfo
)
328 if(!alloc_pioinfo_block(i
))
329 return &MSVCRT___badioinfo
;
330 info
= get_ioinfo_nolock(i
);
333 init_ioinfo_cs(info
);
334 if(TryEnterCriticalSection(&info
->crit
))
336 if(info
->handle
== INVALID_HANDLE_VALUE
)
341 LeaveCriticalSection(&info
->crit
);
345 WARN(":files exhausted!\n");
346 *MSVCRT__errno() = MSVCRT_ENFILE
;
347 return &MSVCRT___badioinfo
;
350 static inline void release_ioinfo(ioinfo
*info
)
352 if(info
!=&MSVCRT___badioinfo
&& info
->exflag
& EF_CRIT_INIT
)
353 LeaveCriticalSection(&info
->crit
);
356 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
360 if(i
>= MSVCRT_max_streams
)
364 return &MSVCRT__iob
[i
];
366 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
368 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
369 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
370 ERR("out of memory\n");
371 *MSVCRT__errno() = MSVCRT_ENOMEM
;
375 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
377 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
382 /* INTERNAL: free a file entry fd */
383 static void msvcrt_free_fd(int fd
)
385 ioinfo
*fdinfo
= get_ioinfo(fd
);
387 if(fdinfo
!= &MSVCRT___badioinfo
)
389 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
392 TRACE(":fd (%d) freed\n",fd
);
399 SetStdHandle(STD_INPUT_HANDLE
, 0);
402 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
405 SetStdHandle(STD_ERROR_HANDLE
, 0);
409 release_ioinfo(fdinfo
);
412 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
414 fdinfo
->handle
= hand
;
415 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
416 fdinfo
->lookahead
[0] = '\n';
417 fdinfo
->lookahead
[1] = '\n';
418 fdinfo
->lookahead
[2] = '\n';
419 fdinfo
->exflag
&= EF_CRIT_INIT
;
421 if (hand
== MSVCRT_NO_CONSOLE
) hand
= 0;
422 switch (fdinfo
-MSVCRT___pioinfo
[0])
424 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
425 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
426 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
430 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
431 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
434 ioinfo
*info
= get_ioinfo_alloc(&fd
);
436 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
438 if(info
== &MSVCRT___badioinfo
)
441 msvcrt_set_fd(info
, hand
, flag
);
442 release_ioinfo(info
);
446 /* INTERNAL: Allocate a FILE* for an fd slot */
447 /* caller must hold the files lock */
448 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
453 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
455 file
= msvcrt_get_file(i
);
459 if (file
->_flag
== 0)
461 if (i
== MSVCRT_stream_idx
)
463 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
465 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
466 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
477 /* INTERNAL: initialize a FILE* from an open fd */
478 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
480 TRACE(":fd (%d) allocating FILE*\n",fd
);
481 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
483 WARN(":invalid fd %d\n",fd
);
484 *MSVCRT___doserrno() = 0;
485 *MSVCRT__errno() = MSVCRT_EBADF
;
488 file
->_ptr
= file
->_base
= NULL
;
491 file
->_flag
= stream_flags
;
492 file
->_tmpfname
= NULL
;
494 TRACE(":got FILE* (%p)\n",file
);
498 /* INTERNAL: Create an inheritance data block (for spawned process)
499 * The inheritance block is made of:
500 * 00 int nb of file descriptor (NBFD)
501 * 04 char file flags (wxflag): repeated for each fd
502 * 4+NBFD HANDLE file handle: repeated for each fd
504 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
511 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
512 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
516 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
517 *block
= MSVCRT_calloc(1, *size
);
523 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
524 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
526 *(unsigned*)*block
= last_fd
;
527 for (fd
= 0; fd
< last_fd
; fd
++)
529 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
530 fdinfo
= get_ioinfo(fd
);
531 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
533 *wxflag_ptr
= fdinfo
->wxflag
;
534 *handle_ptr
= fdinfo
->handle
;
539 *handle_ptr
= INVALID_HANDLE_VALUE
;
541 release_ioinfo(fdinfo
);
542 wxflag_ptr
++; handle_ptr
++;
547 /* INTERNAL: Set up all file descriptors,
548 * as well as default streams (stdin, stderr and stdout)
550 void msvcrt_init_io(void)
556 GetStartupInfoA(&si
);
557 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
563 count
= *(unsigned*)si
.lpReserved2
;
564 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
565 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
567 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
568 count
= min(count
, MSVCRT_MAX_FILES
);
569 for (i
= 0; i
< count
; i
++)
571 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
573 fdinfo
= get_ioinfo_alloc_fd(i
);
574 if(fdinfo
!= &MSVCRT___badioinfo
)
575 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
576 release_ioinfo(fdinfo
);
579 wxflag_ptr
++; handle_ptr
++;
583 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDIN_FILENO
);
584 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
585 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
586 DWORD flags
= WX_OPEN
| WX_TEXT
;
587 DWORD type
= GetFileType(h
);
589 if (type
== FILE_TYPE_UNKNOWN
) {
590 h
= MSVCRT_NO_CONSOLE
;
592 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
594 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
598 msvcrt_set_fd(fdinfo
, h
, flags
);
600 release_ioinfo(fdinfo
);
602 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO
);
603 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
604 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
605 DWORD flags
= WX_OPEN
| WX_TEXT
;
606 DWORD type
= GetFileType(h
);
608 if (type
== FILE_TYPE_UNKNOWN
) {
609 h
= MSVCRT_NO_CONSOLE
;
611 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
613 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
617 msvcrt_set_fd(fdinfo
, h
, flags
);
619 release_ioinfo(fdinfo
);
621 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO
);
622 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
623 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
624 DWORD flags
= WX_OPEN
| WX_TEXT
;
625 DWORD type
= GetFileType(h
);
627 if (type
== FILE_TYPE_UNKNOWN
) {
628 h
= MSVCRT_NO_CONSOLE
;
630 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
632 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
636 msvcrt_set_fd(fdinfo
, h
, flags
);
638 release_ioinfo(fdinfo
);
640 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
641 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
642 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
644 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
645 for (i
= 0; i
< 3; i
++)
647 /* FILE structs for stdin/out/err are static and never deleted */
648 MSVCRT__iob
[i
]._file
= get_ioinfo_nolock(i
)->handle
== MSVCRT_NO_CONSOLE
?
649 MSVCRT_NO_CONSOLE_FD
: i
;
650 MSVCRT__iob
[i
]._tmpfname
= NULL
;
651 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
653 MSVCRT_stream_idx
= 3;
656 /* INTERNAL: Flush stdio file buffer */
657 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
661 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
662 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
663 int cnt
=file
->_ptr
-file
->_base
;
664 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
665 file
->_flag
|= MSVCRT__IOERR
;
667 } else if(file
->_flag
& MSVCRT__IORW
) {
668 file
->_flag
&= ~MSVCRT__IOWRT
;
672 file
->_ptr
=file
->_base
;
677 /*********************************************************************
680 int CDECL
MSVCRT__isatty(int fd
)
682 TRACE(":fd (%d)\n",fd
);
684 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
687 /* INTERNAL: Allocate stdio file buffer */
688 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
690 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
691 && MSVCRT__isatty(file
->_file
))
694 file
->_base
= MSVCRT_calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
696 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
697 file
->_flag
|= MSVCRT__IOMYBUF
;
699 file
->_base
= (char*)(&file
->_charbuf
);
701 file
->_flag
|= MSVCRT__IONBF
;
703 file
->_ptr
= file
->_base
;
708 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
709 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
711 static char buffers
[2][MSVCRT_BUFSIZ
];
713 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
714 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
715 || !MSVCRT__isatty(file
->_file
))
718 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
719 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
720 file
->_flag
|= MSVCRT__USERBUF
;
724 /* INTERNAL: Removes temporary buffer from stdout or stderr */
725 /* Only call this function when add_std_buffer returned TRUE */
726 static void remove_std_buffer(MSVCRT_FILE
*file
)
728 msvcrt_flush_buffer(file
);
729 file
->_ptr
= file
->_base
= NULL
;
730 file
->_bufsiz
= file
->_cnt
= 0;
731 file
->_flag
&= ~MSVCRT__USERBUF
;
734 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
735 static int msvcrt_int_to_base32(int num
, char *str
)
750 *p
= (num
& 31) + '0';
752 *p
+= ('a' - '0' - 10);
759 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
760 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
775 *p
= (num
& 31) + '0';
777 *p
+= ('a' - '0' - 10);
784 /*********************************************************************
785 * __iob_func (MSVCRT.@)
788 MSVCRT_FILE
* CDECL
__iob_func(void)
790 return &MSVCRT__iob
[0];
793 #if _MSVCR_VER >= 140
794 /*********************************************************************
795 * __acrt_iob_func(UCRTBASE.@)
797 MSVCRT_FILE
* CDECL
MSVCRT___acrt_iob_func(unsigned idx
)
799 return &MSVCRT__iob
[idx
];
803 /*********************************************************************
806 int CDECL
MSVCRT__access(const char *filename
, int mode
)
808 DWORD attr
= GetFileAttributesA(filename
);
810 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
812 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
814 msvcrt_set_errno(GetLastError());
817 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
819 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
825 /*********************************************************************
826 * _access_s (MSVCRT.@)
828 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
830 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
831 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
833 if (MSVCRT__access(filename
, mode
) == -1)
834 return *MSVCRT__errno();
838 /*********************************************************************
839 * _waccess (MSVCRT.@)
841 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
843 DWORD attr
= GetFileAttributesW(filename
);
845 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
847 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
849 msvcrt_set_errno(GetLastError());
852 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
854 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
860 /*********************************************************************
861 * _waccess_s (MSVCRT.@)
863 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
865 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
866 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
868 if (MSVCRT__waccess(filename
, mode
) == -1)
869 return *MSVCRT__errno();
873 /*********************************************************************
876 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
878 DWORD oldFlags
= GetFileAttributesA(path
);
880 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
882 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
883 oldFlags
| FILE_ATTRIBUTE_READONLY
;
885 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
888 msvcrt_set_errno(GetLastError());
892 /*********************************************************************
895 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
897 DWORD oldFlags
= GetFileAttributesW(path
);
899 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
901 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
902 oldFlags
| FILE_ATTRIBUTE_READONLY
;
904 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
907 msvcrt_set_errno(GetLastError());
911 /*********************************************************************
914 int CDECL
MSVCRT__unlink(const char *path
)
916 TRACE("%s\n",debugstr_a(path
));
917 if(DeleteFileA(path
))
919 TRACE("failed (%d)\n",GetLastError());
920 msvcrt_set_errno(GetLastError());
924 /*********************************************************************
925 * _wunlink (MSVCRT.@)
927 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
929 TRACE("(%s)\n",debugstr_w(path
));
930 if(DeleteFileW(path
))
932 TRACE("failed (%d)\n",GetLastError());
933 msvcrt_set_errno(GetLastError());
937 /*********************************************************************
940 int CDECL
MSVCRT__commit(int fd
)
942 ioinfo
*info
= get_ioinfo(fd
);
945 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
947 if (info
->handle
== INVALID_HANDLE_VALUE
)
949 else if (!FlushFileBuffers(info
->handle
))
951 if (GetLastError() == ERROR_INVALID_HANDLE
)
953 /* FlushFileBuffers fails for console handles
954 * so we ignore this error.
960 TRACE(":failed-last error (%d)\n",GetLastError());
961 msvcrt_set_errno(GetLastError());
971 release_ioinfo(info
);
975 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
976 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
978 /* INTERNAL: Flush all stream buffer */
979 static int msvcrt_flush_all_buffers(int mask
)
981 int i
, num_flushed
= 0;
985 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
986 file
= msvcrt_get_file(i
);
990 if(file
->_flag
& mask
) {
998 TRACE(":flushed (%d) handles\n",num_flushed
);
1002 /*********************************************************************
1003 * _flushall (MSVCRT.@)
1005 int CDECL
MSVCRT__flushall(void)
1007 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
1010 /*********************************************************************
1013 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
1018 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1021 MSVCRT__lock_file(file
);
1022 ret
= MSVCRT__fflush_nolock(file
);
1023 MSVCRT__unlock_file(file
);
1029 /*********************************************************************
1030 * _fflush_nolock (MSVCRT.@)
1032 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
1037 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1041 res
= msvcrt_flush_buffer(file
);
1042 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1043 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
1047 /*********************************************************************
1050 int CDECL
MSVCRT__close(int fd
)
1052 ioinfo
*info
= get_ioinfo(fd
);
1055 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1057 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1058 *MSVCRT__errno() = MSVCRT_EBADF
;
1060 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, MSVCRT_EBADF
)) {
1062 } else if (fd
== MSVCRT_STDOUT_FILENO
&&
1063 info
->handle
== get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
) {
1066 } else if (fd
== MSVCRT_STDERR_FILENO
&&
1067 info
->handle
== get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
) {
1071 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1074 WARN(":failed-last error (%d)\n",GetLastError());
1075 msvcrt_set_errno(GetLastError());
1078 release_ioinfo(info
);
1082 /*********************************************************************
1085 * MSDN isn't clear on this point, but the remarks for _pipe
1086 * indicate file descriptors duplicated with _dup and _dup2 are always
1089 int CDECL
MSVCRT__dup2(int od
, int nd
)
1091 ioinfo
*info_od
, *info_nd
;
1094 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1098 info_od
= get_ioinfo(od
);
1099 info_nd
= get_ioinfo_alloc_fd(nd
);
1103 info_nd
= get_ioinfo_alloc_fd(nd
);
1104 info_od
= get_ioinfo(od
);
1107 if (info_nd
== &MSVCRT___badioinfo
)
1111 else if (info_od
->wxflag
& WX_OPEN
)
1115 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1116 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1118 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1120 if (info_nd
->wxflag
& WX_OPEN
)
1123 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1124 /* _dup2 returns 0, not nd, on success */
1130 msvcrt_set_errno(GetLastError());
1135 *MSVCRT__errno() = MSVCRT_EBADF
;
1139 release_ioinfo(info_od
);
1140 release_ioinfo(info_nd
);
1144 /*********************************************************************
1147 int CDECL
MSVCRT__dup(int od
)
1150 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1152 if (MSVCRT__dup2(od
, fd
) == 0)
1156 release_ioinfo(info
);
1160 /*********************************************************************
1163 int CDECL
MSVCRT__eof(int fd
)
1165 ioinfo
*info
= get_ioinfo(fd
);
1166 DWORD curpos
,endpos
;
1167 LONG hcurpos
,hendpos
;
1169 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1171 if (info
->handle
== INVALID_HANDLE_VALUE
)
1173 release_ioinfo(info
);
1177 if (info
->wxflag
& WX_ATEOF
)
1179 release_ioinfo(info
);
1183 /* Otherwise we do it the hard way */
1184 hcurpos
= hendpos
= 0;
1185 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1186 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1188 if (curpos
== endpos
&& hcurpos
== hendpos
)
1190 /* FIXME: shouldn't WX_ATEOF be set here? */
1191 release_ioinfo(info
);
1195 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1196 release_ioinfo(info
);
1200 /*********************************************************************
1201 * _fcloseall (MSVCRT.@)
1203 int CDECL
MSVCRT__fcloseall(void)
1205 int num_closed
= 0, i
;
1209 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1210 file
= msvcrt_get_file(i
);
1212 if (file
->_flag
&& !MSVCRT_fclose(file
))
1217 TRACE(":closed (%d) handles\n",num_closed
);
1221 /* free everything on process exit */
1222 void msvcrt_free_io(void)
1228 MSVCRT__fcloseall();
1230 for(i
=0; i
<ARRAY_SIZE(MSVCRT___pioinfo
); i
++)
1232 if(!MSVCRT___pioinfo
[i
])
1235 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1237 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1238 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1240 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1243 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1245 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1246 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1248 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1249 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1253 for(i
=0; i
<ARRAY_SIZE(MSVCRT_fstream
); i
++)
1254 MSVCRT_free(MSVCRT_fstream
[i
]);
1257 /*********************************************************************
1258 * _lseeki64 (MSVCRT.@)
1260 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1262 ioinfo
*info
= get_ioinfo(fd
);
1265 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1267 if (info
->handle
== INVALID_HANDLE_VALUE
)
1269 *MSVCRT__errno() = MSVCRT_EBADF
;
1270 release_ioinfo(info
);
1274 if (whence
< 0 || whence
> 2)
1276 release_ioinfo(info
);
1277 *MSVCRT__errno() = MSVCRT_EINVAL
;
1281 TRACE(":fd (%d) to %s pos %s\n",
1282 fd
,wine_dbgstr_longlong(offset
),
1283 (whence
==MSVCRT_SEEK_SET
)?"SEEK_SET":
1284 (whence
==MSVCRT_SEEK_CUR
)?"SEEK_CUR":
1285 (whence
==MSVCRT_SEEK_END
)?"SEEK_END":"UNKNOWN");
1287 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1288 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1289 ofs
.QuadPart
= offset
;
1290 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1291 GetLastError() == ERROR_SUCCESS
)
1293 info
->wxflag
&= ~WX_ATEOF
;
1294 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1296 release_ioinfo(info
);
1297 return ofs
.QuadPart
;
1299 release_ioinfo(info
);
1300 TRACE(":error-last error (%d)\n",GetLastError());
1301 msvcrt_set_errno(GetLastError());
1305 /*********************************************************************
1308 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1310 return MSVCRT__lseeki64(fd
, offset
, whence
);
1313 /*********************************************************************
1314 * _lock_file (MSVCRT.@)
1316 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1318 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1319 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1321 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1324 /*********************************************************************
1325 * _unlock_file (MSVCRT.@)
1327 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1329 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1330 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1332 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1335 /*********************************************************************
1336 * _locking (MSVCRT.@)
1338 * This is untested; the underlying LockFile doesn't work yet.
1340 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1342 ioinfo
*info
= get_ioinfo(fd
);
1346 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1347 if (info
->handle
== INVALID_HANDLE_VALUE
)
1349 release_ioinfo(info
);
1353 if (mode
< 0 || mode
> 4)
1355 release_ioinfo(info
);
1356 *MSVCRT__errno() = MSVCRT_EINVAL
;
1360 TRACE(":fd (%d) by 0x%08x mode %s\n",
1361 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1362 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1363 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1364 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1365 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1368 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1370 release_ioinfo(info
);
1371 FIXME ("Seek failed\n");
1372 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1375 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1378 ret
= 1; /* just to satisfy gcc */
1381 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1386 else if (mode
== MSVCRT__LK_UNLCK
)
1387 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1389 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1390 /* FIXME - what about error settings? */
1391 release_ioinfo(info
);
1392 return ret
? 0 : -1;
1395 /*********************************************************************
1396 * _fseeki64 (MSVCRT.@)
1398 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1402 MSVCRT__lock_file(file
);
1403 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1404 MSVCRT__unlock_file(file
);
1409 /*********************************************************************
1410 * _fseeki64_nolock (MSVCRT.@)
1412 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1416 if(whence
== MSVCRT_SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1417 whence
= MSVCRT_SEEK_SET
;
1418 offset
+= MSVCRT__ftelli64_nolock(file
);
1421 /* Flush output if needed */
1422 msvcrt_flush_buffer(file
);
1423 /* Reset direction of i/o */
1424 if(file
->_flag
& MSVCRT__IORW
) {
1425 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1427 /* Clear end of file flag */
1428 file
->_flag
&= ~MSVCRT__IOEOF
;
1429 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1434 /*********************************************************************
1437 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1439 return MSVCRT__fseeki64( file
, offset
, whence
);
1442 /*********************************************************************
1443 * _fseek_nolock (MSVCRT.@)
1445 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1447 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1450 /*********************************************************************
1451 * _chsize_s (MSVCRT.@)
1453 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1459 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1461 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1464 info
= get_ioinfo(fd
);
1465 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1467 /* save the current file pointer */
1468 cur
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1471 pos
= MSVCRT__lseeki64(fd
, size
, MSVCRT_SEEK_SET
);
1474 ret
= SetEndOfFile(info
->handle
);
1475 if (!ret
) msvcrt_set_errno(GetLastError());
1478 /* restore the file pointer */
1479 MSVCRT__lseeki64(fd
, cur
, MSVCRT_SEEK_SET
);
1483 release_ioinfo(info
);
1484 return ret
? 0 : *MSVCRT__errno();
1487 /*********************************************************************
1488 * _chsize (MSVCRT.@)
1490 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1492 /* _chsize_s returns errno on failure but _chsize should return -1 */
1493 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1496 /*********************************************************************
1497 * clearerr (MSVCRT.@)
1499 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1501 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1503 MSVCRT__lock_file(file
);
1504 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1505 MSVCRT__unlock_file(file
);
1508 /*********************************************************************
1509 * clearerr_s (MSVCRT.@)
1511 int CDECL
MSVCRT_clearerr_s(MSVCRT_FILE
* file
)
1513 TRACE(":file (%p)\n",file
);
1515 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
1517 MSVCRT__lock_file(file
);
1518 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1519 MSVCRT__unlock_file(file
);
1523 /*********************************************************************
1526 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1528 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1530 MSVCRT__lock_file(file
);
1531 MSVCRT__fseek_nolock(file
, 0L, MSVCRT_SEEK_SET
);
1532 MSVCRT_clearerr(file
);
1533 MSVCRT__unlock_file(file
);
1536 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1538 int plus
= strchrW(mode
, '+') != NULL
;
1540 TRACE("%s\n", debugstr_w(mode
));
1542 while(*mode
== ' ') mode
++;
1547 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1548 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1551 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1552 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1555 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1556 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1559 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1563 *stream_flags
|= MSVCRT__commode
;
1565 while (*mode
&& *mode
!=',')
1569 *open_flags
|= MSVCRT__O_BINARY
;
1570 *open_flags
&= ~MSVCRT__O_TEXT
;
1573 *open_flags
|= MSVCRT__O_TEXT
;
1574 *open_flags
&= ~MSVCRT__O_BINARY
;
1577 *open_flags
|= MSVCRT__O_TEMPORARY
;
1580 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1583 *stream_flags
|= MSVCRT__IOCOMMIT
;
1586 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1589 *open_flags
|= MSVCRT__O_NOINHERIT
;
1598 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1601 ERR("incorrect mode flag: %c\n", mode
[-1]);
1607 static const WCHAR ccs
[] = {'c','c','s'};
1608 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1609 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1610 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1613 while(*mode
== ' ') mode
++;
1614 if(!MSVCRT_CHECK_PMT(!MSVCRT_wcsncmp(ccs
, mode
, ARRAY_SIZE(ccs
))))
1616 mode
+= ARRAY_SIZE(ccs
);
1617 while(*mode
== ' ') mode
++;
1618 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1621 while(*mode
== ' ') mode
++;
1623 if(!MSVCRT__wcsnicmp(utf8
, mode
, ARRAY_SIZE(utf8
)))
1625 *open_flags
|= MSVCRT__O_U8TEXT
;
1626 mode
+= ARRAY_SIZE(utf8
);
1628 else if(!MSVCRT__wcsnicmp(utf16le
, mode
, ARRAY_SIZE(utf16le
)))
1630 *open_flags
|= MSVCRT__O_U16TEXT
;
1631 mode
+= ARRAY_SIZE(utf16le
);
1633 else if(!MSVCRT__wcsnicmp(unicode
, mode
, ARRAY_SIZE(unicode
)))
1635 *open_flags
|= MSVCRT__O_WTEXT
;
1636 mode
+= ARRAY_SIZE(unicode
);
1640 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1644 while(*mode
== ' ') mode
++;
1647 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1652 /*********************************************************************
1653 * _fdopen (MSVCRT.@)
1655 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1658 MSVCRT_wchar_t
*modeW
= NULL
;
1660 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1662 ret
= MSVCRT__wfdopen(fd
, modeW
);
1668 /*********************************************************************
1669 * _wfdopen (MSVCRT.@)
1671 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1673 int open_flags
, stream_flags
;
1676 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1679 if (!(file
= msvcrt_alloc_fp()))
1681 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1686 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1692 /*********************************************************************
1693 * _filelength (MSVCRT.@)
1695 LONG CDECL
MSVCRT__filelength(int fd
)
1697 LONG curPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
1700 LONG endPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_END
);
1703 if (endPos
!= curPos
)
1704 MSVCRT__lseek(fd
, curPos
, MSVCRT_SEEK_SET
);
1711 /*********************************************************************
1712 * _filelengthi64 (MSVCRT.@)
1714 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1716 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1719 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_END
);
1722 if (endPos
!= curPos
)
1723 MSVCRT__lseeki64(fd
, curPos
, MSVCRT_SEEK_SET
);
1730 /*********************************************************************
1731 * _fileno (MSVCRT.@)
1733 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1735 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1739 /*********************************************************************
1740 * _fstat64 (MSVCRT.@)
1742 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1744 ioinfo
*info
= get_ioinfo(fd
);
1747 BY_HANDLE_FILE_INFORMATION hfi
;
1749 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1750 if (info
->handle
== INVALID_HANDLE_VALUE
)
1752 release_ioinfo(info
);
1758 WARN(":failed-NULL buf\n");
1759 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1760 release_ioinfo(info
);
1764 memset(&hfi
, 0, sizeof(hfi
));
1765 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1766 type
= GetFileType(info
->handle
);
1767 if (type
== FILE_TYPE_PIPE
)
1769 buf
->st_dev
= buf
->st_rdev
= fd
;
1770 buf
->st_mode
= MSVCRT__S_IFIFO
;
1773 else if (type
== FILE_TYPE_CHAR
)
1775 buf
->st_dev
= buf
->st_rdev
= fd
;
1776 buf
->st_mode
= MSVCRT__S_IFCHR
;
1779 else /* FILE_TYPE_DISK etc. */
1781 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1783 WARN(":failed-last error (%d)\n",GetLastError());
1784 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1785 release_ioinfo(info
);
1788 buf
->st_mode
= MSVCRT__S_IFREG
| 0444;
1789 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1790 buf
->st_mode
|= 0222;
1791 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1792 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1794 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1795 buf
->st_mtime
= buf
->st_ctime
= dw
;
1796 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1798 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1800 release_ioinfo(info
);
1804 /*********************************************************************
1805 * _fstati64 (MSVCRT.@)
1807 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1810 struct MSVCRT__stat64 buf64
;
1812 ret
= MSVCRT__fstat64(fd
, &buf64
);
1814 msvcrt_stat64_to_stati64(&buf64
, buf
);
1818 /*********************************************************************
1821 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1823 struct MSVCRT__stat64 buf64
;
1825 ret
= MSVCRT__fstat64(fd
, &buf64
);
1827 msvcrt_stat64_to_stat(&buf64
, buf
);
1831 /*********************************************************************
1832 * _fstat32 (MSVCR80.@)
1834 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1837 struct MSVCRT__stat64 buf64
;
1839 ret
= MSVCRT__fstat64(fd
, &buf64
);
1841 msvcrt_stat64_to_stat32(&buf64
, buf
);
1845 /*********************************************************************
1846 * _fstat32i64 (MSVCR80.@)
1848 int CDECL
MSVCRT__fstat32i64(int fd
, struct MSVCRT__stat32i64
* buf
)
1851 struct MSVCRT__stat64 buf64
;
1853 ret
= MSVCRT__fstat64(fd
, &buf64
);
1855 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1859 /*********************************************************************
1860 * _fstat64i32 (MSVCR80.@)
1862 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1865 struct MSVCRT__stat64 buf64
;
1867 ret
= MSVCRT__fstat64(fd
, &buf64
);
1869 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1873 /*********************************************************************
1874 * _futime64 (MSVCRT.@)
1876 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1878 ioinfo
*info
= get_ioinfo(fd
);
1883 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1888 time_to_filetime( t
->actime
, &at
);
1889 time_to_filetime( t
->modtime
, &wt
);
1892 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1894 release_ioinfo(info
);
1895 msvcrt_set_errno(GetLastError());
1898 release_ioinfo(info
);
1902 /*********************************************************************
1903 * _futime32 (MSVCRT.@)
1905 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1909 struct MSVCRT___utimbuf64 t64
;
1910 t64
.actime
= t
->actime
;
1911 t64
.modtime
= t
->modtime
;
1912 return _futime64( fd
, &t64
);
1915 return _futime64( fd
, NULL
);
1918 /*********************************************************************
1919 * _futime (MSVCRT.@)
1922 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1924 return _futime64( fd
, t
);
1927 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1929 return _futime32( fd
, t
);
1933 /*********************************************************************
1934 * _get_osfhandle (MSVCRT.@)
1936 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1938 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1939 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1941 if(hand
== INVALID_HANDLE_VALUE
)
1942 *MSVCRT__errno() = MSVCRT_EBADF
;
1943 return (MSVCRT_intptr_t
)hand
;
1946 /*********************************************************************
1947 * _mktemp_s (MSVCRT.@)
1949 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1953 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1954 return MSVCRT_EINVAL
;
1956 for(len
=0; len
<size
; len
++)
1959 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1962 return MSVCRT_EINVAL
;
1965 for(xno
=1; xno
<=6; xno
++)
1966 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1967 return MSVCRT_EINVAL
;
1969 id
= GetCurrentProcessId();
1970 for(xno
=1; xno
<6; xno
++) {
1971 pattern
[len
-xno
] = id
%10 + '0';
1975 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1976 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1981 *MSVCRT__errno() = MSVCRT_EEXIST
;
1982 return MSVCRT_EEXIST
;
1985 /*********************************************************************
1986 * _mktemp (MSVCRT.@)
1988 char * CDECL
MSVCRT__mktemp(char *pattern
)
1991 char *retVal
= pattern
;
1999 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2003 id
= GetCurrentProcessId();
2007 int tempNum
= id
/ 10;
2008 *pattern
-- = id
- (tempNum
* 10) + '0';
2014 *pattern
= letter
++;
2015 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2017 } while(letter
<= 'z');
2021 /*********************************************************************
2022 * _wmktemp_s (MSVCRT.@)
2024 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
2028 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2029 return MSVCRT_EINVAL
;
2031 for(len
=0; len
<size
; len
++)
2034 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2037 return MSVCRT_EINVAL
;
2040 for(xno
=1; xno
<=6; xno
++)
2041 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2042 return MSVCRT_EINVAL
;
2044 id
= GetCurrentProcessId();
2045 for(xno
=1; xno
<6; xno
++) {
2046 pattern
[len
-xno
] = id
%10 + '0';
2050 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2051 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2056 *MSVCRT__errno() = MSVCRT_EEXIST
;
2057 return MSVCRT_EEXIST
;
2060 /*********************************************************************
2061 * _wmktemp (MSVCRT.@)
2063 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
2066 MSVCRT_wchar_t
*retVal
= pattern
;
2068 MSVCRT_wchar_t letter
= 'a';
2074 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2078 id
= GetCurrentProcessId();
2082 int tempNum
= id
/ 10;
2083 *pattern
-- = id
- (tempNum
* 10) + '0';
2089 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2091 *pattern
= letter
++;
2092 } while(letter
!= '|');
2096 static unsigned split_oflags(unsigned oflags
)
2099 unsigned unsupp
; /* until we support everything */
2101 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2102 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2103 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2104 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2105 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2106 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2107 else if (*MSVCRT___p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2108 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2109 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2111 if ((unsupp
= oflags
& ~(
2112 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2113 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2114 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2115 MSVCRT__O_NOINHERIT
|
2116 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2117 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2119 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2124 /*********************************************************************
2127 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2130 SECURITY_ATTRIBUTES sa
;
2131 HANDLE readHandle
, writeHandle
;
2135 *MSVCRT__errno() = MSVCRT_EINVAL
;
2139 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2140 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2141 sa
.lpSecurityDescriptor
= NULL
;
2142 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2144 unsigned int wxflags
= split_oflags(textmode
);
2147 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2151 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2159 MSVCRT__close(pfds
[0]);
2160 CloseHandle(writeHandle
);
2161 *MSVCRT__errno() = MSVCRT_EMFILE
;
2166 CloseHandle(readHandle
);
2167 CloseHandle(writeHandle
);
2168 *MSVCRT__errno() = MSVCRT_EMFILE
;
2172 msvcrt_set_errno(GetLastError());
2177 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2179 char bom
[sizeof(utf8_bom
)];
2182 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2184 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2187 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2188 oflags
|= MSVCRT__O_U8TEXT
;
2189 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2191 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2192 oflags
|= MSVCRT__O_U16TEXT
;
2194 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2200 /*********************************************************************
2201 * _wsopen_dispatch (UCRTBASE.@)
2203 int CDECL
MSVCRT__wsopen_dispatch( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
,
2204 int *fd
, int secure
)
2206 DWORD access
= 0, creation
= 0, attrib
;
2207 SECURITY_ATTRIBUTES sa
;
2208 DWORD sharing
, type
;
2212 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2213 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2215 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2218 wxflag
= split_oflags(oflags
);
2219 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2221 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2222 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2223 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2226 if (oflags
& MSVCRT__O_CREAT
)
2228 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))))
2229 return MSVCRT_EINVAL
;
2231 if (oflags
& MSVCRT__O_EXCL
)
2232 creation
= CREATE_NEW
;
2233 else if (oflags
& MSVCRT__O_TRUNC
)
2234 creation
= CREATE_ALWAYS
;
2236 creation
= OPEN_ALWAYS
;
2238 else /* no MSVCRT__O_CREAT */
2240 if (oflags
& MSVCRT__O_TRUNC
)
2241 creation
= TRUNCATE_EXISTING
;
2243 creation
= OPEN_EXISTING
;
2248 case MSVCRT__SH_DENYRW
:
2251 case MSVCRT__SH_DENYWR
:
2252 sharing
= FILE_SHARE_READ
;
2254 case MSVCRT__SH_DENYRD
:
2255 sharing
= FILE_SHARE_WRITE
;
2257 case MSVCRT__SH_DENYNO
:
2258 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2261 ERR( "Unhandled shflags 0x%x\n", shflags
);
2262 return MSVCRT_EINVAL
;
2265 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2266 attrib
= FILE_ATTRIBUTE_READONLY
;
2268 attrib
= FILE_ATTRIBUTE_NORMAL
;
2270 if (oflags
& MSVCRT__O_TEMPORARY
)
2272 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2274 sharing
|= FILE_SHARE_DELETE
;
2277 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2278 sa
.lpSecurityDescriptor
= NULL
;
2279 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2281 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2282 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2283 && !(access
&GENERIC_READ
))
2285 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2286 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2287 if (hand
!= INVALID_HANDLE_VALUE
)
2289 oflags
= check_bom(hand
, oflags
, FALSE
);
2293 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2296 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2297 if (hand
== INVALID_HANDLE_VALUE
) {
2298 WARN(":failed-last error (%d)\n",GetLastError());
2299 msvcrt_set_errno(GetLastError());
2300 return *MSVCRT__errno();
2303 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2305 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2306 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2307 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2309 if (oflags
& MSVCRT__O_U8TEXT
)
2311 DWORD written
= 0, tmp
;
2313 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2314 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2316 if (written
!= sizeof(utf8_bom
)) {
2317 WARN("error writing BOM\n");
2319 msvcrt_set_errno(GetLastError());
2320 return *MSVCRT__errno();
2325 DWORD written
= 0, tmp
;
2327 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2328 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2330 if (written
!= sizeof(utf16_bom
))
2332 WARN("error writing BOM\n");
2334 msvcrt_set_errno(GetLastError());
2335 return *MSVCRT__errno();
2339 else if (access
& GENERIC_READ
)
2340 oflags
= check_bom(hand
, oflags
, TRUE
);
2343 type
= GetFileType(hand
);
2344 if (type
== FILE_TYPE_CHAR
)
2346 else if (type
== FILE_TYPE_PIPE
)
2349 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2351 return *MSVCRT__errno();
2353 if (oflags
& MSVCRT__O_WTEXT
)
2354 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2355 else if (oflags
& MSVCRT__O_U16TEXT
)
2356 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2357 else if (oflags
& MSVCRT__O_U8TEXT
)
2358 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2360 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2365 /*********************************************************************
2366 * _wsopen_s (MSVCRT.@)
2368 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2370 return MSVCRT__wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2373 /*********************************************************************
2374 * _wsopen (MSVCRT.@)
2376 int WINAPIV
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2381 if (oflags
& MSVCRT__O_CREAT
)
2385 __ms_va_start(ap
, shflags
);
2386 pmode
= va_arg(ap
, int);
2392 return MSVCRT__wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2396 /*********************************************************************
2397 * _sopen_dispatch (UCRTBASE.@)
2399 int CDECL
MSVCRT__sopen_dispatch( const char *path
, int oflags
, int shflags
,
2400 int pmode
, int *fd
, int secure
)
2402 MSVCRT_wchar_t
*pathW
;
2405 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2406 return MSVCRT_EINVAL
;
2408 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2409 return MSVCRT_EINVAL
;
2411 ret
= MSVCRT__wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2416 /*********************************************************************
2417 * _sopen_s (MSVCRT.@)
2419 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2421 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2424 /*********************************************************************
2427 int WINAPIV
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2432 if (oflags
& MSVCRT__O_CREAT
)
2436 __ms_va_start(ap
, shflags
);
2437 pmode
= va_arg(ap
, int);
2443 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2446 /*********************************************************************
2449 int WINAPIV
MSVCRT__open( const char *path
, int flags
, ... )
2453 if (flags
& MSVCRT__O_CREAT
)
2456 __ms_va_start(ap
, flags
);
2457 pmode
= va_arg(ap
, int);
2459 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2462 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2465 /*********************************************************************
2468 int WINAPIV
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2472 if (flags
& MSVCRT__O_CREAT
)
2475 __ms_va_start(ap
, flags
);
2476 pmode
= va_arg(ap
, int);
2478 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2481 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2484 /*********************************************************************
2487 int CDECL
MSVCRT__creat(const char *path
, int pmode
)
2489 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2490 return MSVCRT__open(path
, flags
, pmode
);
2493 /*********************************************************************
2494 * _wcreat (MSVCRT.@)
2496 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int pmode
)
2498 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2499 return MSVCRT__wopen(path
, flags
, pmode
);
2502 /*********************************************************************
2503 * _open_osfhandle (MSVCRT.@)
2505 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2510 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2511 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2512 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2513 * text - it never sets MSVCRT__O_BINARY.
2515 /* don't let split_oflags() decide the mode if no mode is passed */
2516 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2517 oflags
|= MSVCRT__O_BINARY
;
2519 flags
= GetFileType((HANDLE
)handle
);
2520 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2522 msvcrt_set_errno(GetLastError());
2526 if (flags
== FILE_TYPE_CHAR
)
2528 else if (flags
== FILE_TYPE_PIPE
)
2532 flags
|= split_oflags(oflags
);
2534 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2535 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2539 /*********************************************************************
2542 int CDECL
MSVCRT__rmtmp(void)
2544 int num_removed
= 0, i
;
2548 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2549 file
= msvcrt_get_file(i
);
2551 if (file
->_tmpfname
)
2553 MSVCRT_fclose(file
);
2560 TRACE(":removed (%d) temp files\n",num_removed
);
2564 static inline int get_utf8_char_len(char ch
)
2566 if((ch
&0xf8) == 0xf0)
2568 else if((ch
&0xf0) == 0xe0)
2570 else if((ch
&0xe0) == 0xc0)
2575 /*********************************************************************
2576 * (internal) read_utf8
2578 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2580 HANDLE hand
= fdinfo
->handle
;
2581 char min_buf
[4], *readbuf
, lookahead
;
2582 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2584 /* make the buffer big enough to hold at least one character */
2585 /* read bytes have to fit to output and lookahead buffers */
2587 readbuf_size
= count
< 4 ? 4 : count
;
2588 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2593 if(fdinfo
->lookahead
[0] != '\n') {
2594 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2595 fdinfo
->lookahead
[0] = '\n';
2597 if(fdinfo
->lookahead
[1] != '\n') {
2598 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2599 fdinfo
->lookahead
[1] = '\n';
2601 if(fdinfo
->lookahead
[2] != '\n') {
2602 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2603 fdinfo
->lookahead
[2] = '\n';
2608 /* NOTE: this case is broken in native dll, reading
2609 * sometimes fails when small buffer is passed
2612 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2613 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2614 fdinfo
->wxflag
|= WX_ATEOF
;
2617 msvcrt_set_errno(GetLastError());
2620 }else if(!num_read
) {
2621 fdinfo
->wxflag
|= WX_ATEOF
;
2627 char_len
= get_utf8_char_len(readbuf
[0]);
2629 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2633 if(readbuf
[0] == '\n')
2634 fdinfo
->wxflag
|= WX_READNL
;
2636 fdinfo
->wxflag
&= ~WX_READNL
;
2638 if(readbuf
[0] == 0x1a) {
2639 fdinfo
->wxflag
|= WX_ATEOF
;
2643 if(readbuf
[0] == '\r') {
2644 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2646 else if(lookahead
== '\n')
2650 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2651 fdinfo
->lookahead
[0] = lookahead
;
2653 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2658 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2659 msvcrt_set_errno(GetLastError());
2666 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2669 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2670 fdinfo
->wxflag
|= WX_ATEOF
;
2671 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2674 msvcrt_set_errno(GetLastError());
2675 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2678 }else if(!pos
&& !num_read
) {
2679 fdinfo
->wxflag
|= WX_ATEOF
;
2680 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2685 if(readbuf
[0] == '\n')
2686 fdinfo
->wxflag
|= WX_READNL
;
2688 fdinfo
->wxflag
&= ~WX_READNL
;
2690 /* Find first byte of last character (may be incomplete) */
2691 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2692 if((readbuf
[i
]&0xc0) != 0x80)
2694 char_len
= get_utf8_char_len(readbuf
[i
]);
2695 if(char_len
+i
<= pos
)
2698 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2700 fdinfo
->lookahead
[0] = readbuf
[i
];
2702 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2704 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2706 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2710 for(i
=0, j
=0; i
<pos
; i
++) {
2711 if(readbuf
[i
] == 0x1a) {
2712 fdinfo
->wxflag
|= WX_ATEOF
;
2716 /* strip '\r' if followed by '\n' */
2717 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2718 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2719 readbuf
[j
++] = '\r';
2720 }else if(lookahead
== '\n' && j
==0) {
2721 readbuf
[j
++] = '\n';
2723 if(lookahead
!= '\n')
2724 readbuf
[j
++] = '\r';
2726 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2727 fdinfo
->lookahead
[0] = lookahead
;
2729 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2731 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2732 readbuf
[j
++] = readbuf
[i
];
2737 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2738 msvcrt_set_errno(GetLastError());
2739 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2743 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2747 /*********************************************************************
2750 * When reading \r as last character in text mode, read() positions
2751 * the file pointer on the \r character while getc() goes on to
2754 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2756 DWORD num_read
, utf16
;
2757 char *bufstart
= buf
;
2762 if (fdinfo
->wxflag
& WX_ATEOF
) {
2763 TRACE("already at EOF, returning 0\n");
2766 /* Don't trace small reads, it gets *very* annoying */
2768 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2769 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2771 *MSVCRT__errno() = MSVCRT_EBADF
;
2775 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2776 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2778 *MSVCRT__errno() = MSVCRT_EINVAL
;
2782 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2783 return read_utf8(fdinfo
, buf
, count
);
2785 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2787 if (fdinfo
->lookahead
[0] != '\n')
2789 bufstart
[0] = fdinfo
->lookahead
[0];
2790 fdinfo
->lookahead
[0] = '\n';
2794 bufstart
[1] = fdinfo
->lookahead
[1];
2795 fdinfo
->lookahead
[1] = '\n';
2798 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2799 num_read
+= 1+utf16
;
2804 if(utf16
&& (num_read
&1))
2806 /* msvcr90 uses uninitialized value from the buffer in this case */
2807 /* msvcrt ignores additional data */
2808 ERR("got odd number of bytes in UTF16 mode\n");
2812 if (count
!= 0 && num_read
== 0)
2814 fdinfo
->wxflag
|= WX_ATEOF
;
2815 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2817 else if (fdinfo
->wxflag
& WX_TEXT
)
2821 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2822 fdinfo
->wxflag
|= WX_READNL
;
2824 fdinfo
->wxflag
&= ~WX_READNL
;
2826 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2828 /* in text mode, a ctrl-z signals EOF */
2829 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2831 fdinfo
->wxflag
|= WX_ATEOF
;
2832 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2836 /* in text mode, strip \r if followed by \n */
2837 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2842 lookahead
[1] = '\n';
2843 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2845 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2847 bufstart
[j
++] = '\n';
2848 if(utf16
) bufstart
[j
++] = 0;
2852 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2854 bufstart
[j
++] = '\r';
2855 if(utf16
) bufstart
[j
++] = 0;
2858 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2860 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2862 bufstart
[j
++] = '\n';
2863 if (utf16
) bufstart
[j
++] = 0;
2867 fdinfo
->lookahead
[0] = lookahead
[0];
2868 fdinfo
->lookahead
[1] = lookahead
[1];
2872 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2877 bufstart
[j
++] = '\r';
2878 if(utf16
) bufstart
[j
++] = 0;
2881 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2882 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2884 bufstart
[j
++] = bufstart
[i
];
2885 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2893 if (GetLastError() == ERROR_BROKEN_PIPE
)
2895 TRACE(":end-of-pipe\n");
2896 fdinfo
->wxflag
|= WX_ATEOF
;
2901 TRACE(":failed-last error (%d)\n",GetLastError());
2902 msvcrt_set_errno(GetLastError());
2908 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2912 /*********************************************************************
2915 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2920 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
2921 *MSVCRT__errno() = MSVCRT_EBADF
;
2925 info
= get_ioinfo(fd
);
2926 num_read
= read_i(fd
, info
, buf
, count
);
2927 release_ioinfo(info
);
2931 /*********************************************************************
2932 * _setmode (MSVCRT.@)
2934 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2936 ioinfo
*info
= get_ioinfo(fd
);
2937 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2938 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2939 ret
= MSVCRT__O_WTEXT
;
2941 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2942 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2943 *MSVCRT__errno() = MSVCRT_EINVAL
;
2944 release_ioinfo(info
);
2948 if(info
== &MSVCRT___badioinfo
) {
2949 *MSVCRT__errno() = MSVCRT_EBADF
;
2953 if(mode
== MSVCRT__O_BINARY
) {
2954 info
->wxflag
&= ~WX_TEXT
;
2955 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2956 release_ioinfo(info
);
2960 info
->wxflag
|= WX_TEXT
;
2961 if(mode
== MSVCRT__O_TEXT
)
2962 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2963 else if(mode
== MSVCRT__O_U8TEXT
)
2964 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2966 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2968 release_ioinfo(info
);
2972 /*********************************************************************
2973 * _stat64 (MSVCRT.@)
2975 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2978 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2979 unsigned short mode
= ALL_S_IREAD
;
2982 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2984 plen
= strlen(path
);
2985 while (plen
&& path
[plen
-1]==' ')
2988 if (plen
==2 && path
[1]==':')
2990 *MSVCRT__errno() = MSVCRT_ENOENT
;
2995 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2997 *MSVCRT__errno() = MSVCRT_ENOENT
;
3002 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
3004 TRACE("failed (%d)\n",GetLastError());
3005 *MSVCRT__errno() = MSVCRT_ENOENT
;
3009 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3011 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3012 Bon 011120: This FIXME seems incorrect
3013 Also a letter as first char isn't enough to be classified
3016 if (isalpha(*path
)&& (*(path
+1)==':'))
3017 buf
->st_dev
= buf
->st_rdev
= MSVCRT__toupper_l(*path
, NULL
) - 'A'; /* drive num */
3019 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3021 /* Dir, or regular file? */
3022 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3023 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3026 mode
|= MSVCRT__S_IFREG
;
3028 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3030 unsigned int ext
= MSVCRT__tolower_l(path
[plen
-1], NULL
) |
3031 (MSVCRT__tolower_l(path
[plen
-2], NULL
) << 8) |
3032 (MSVCRT__tolower_l(path
[plen
-3], NULL
) << 16);
3033 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3034 mode
|= ALL_S_IEXEC
;
3038 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3039 mode
|= ALL_S_IWRITE
;
3041 buf
->st_mode
= mode
;
3043 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3044 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3046 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3047 buf
->st_mtime
= buf
->st_ctime
= dw
;
3048 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3049 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3050 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3054 /*********************************************************************
3055 * _stati64 (MSVCRT.@)
3057 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
3060 struct MSVCRT__stat64 buf64
;
3062 ret
= MSVCRT_stat64(path
, &buf64
);
3064 msvcrt_stat64_to_stati64(&buf64
, buf
);
3068 /*********************************************************************
3071 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
3074 struct MSVCRT__stat64 buf64
;
3076 ret
= MSVCRT_stat64( path
, &buf64
);
3078 msvcrt_stat64_to_stat(&buf64
, buf
);
3082 #if _MSVCR_VER >= 80
3084 /*********************************************************************
3085 * _stat32 (MSVCR80.@)
3087 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
3090 struct MSVCRT__stat64 buf64
;
3092 ret
= MSVCRT_stat64(path
, &buf64
);
3094 msvcrt_stat64_to_stat32(&buf64
, buf
);
3098 /*********************************************************************
3099 * _stat32i64 (MSVCR80.@)
3101 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
3104 struct MSVCRT__stat64 buf64
;
3106 ret
= MSVCRT_stat64(path
, &buf64
);
3108 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3112 /*********************************************************************
3113 * _stat64i32 (MSVCR80.@)
3115 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
3118 struct MSVCRT__stat64 buf64
;
3120 ret
= MSVCRT_stat64(path
, &buf64
);
3122 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3126 #endif /* _MSVCR_VER >= 80 */
3128 /*********************************************************************
3129 * _wstat64 (MSVCRT.@)
3131 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
3134 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3135 unsigned short mode
= ALL_S_IREAD
;
3138 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3140 plen
= strlenW(path
);
3141 while (plen
&& path
[plen
-1]==' ')
3144 if (plen
==2 && path
[1]==':')
3146 *MSVCRT__errno() = MSVCRT_ENOENT
;
3151 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3153 *MSVCRT__errno() = MSVCRT_ENOENT
;
3158 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3160 TRACE("failed (%d)\n",GetLastError());
3161 *MSVCRT__errno() = MSVCRT_ENOENT
;
3165 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3167 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3168 if (MSVCRT_iswalpha(*path
) && path
[1] == ':')
3169 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
) - 'A'; /* drive num */
3171 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3173 /* Dir, or regular file? */
3174 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3175 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3178 mode
|= MSVCRT__S_IFREG
;
3180 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3182 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3183 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3184 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3185 mode
|= ALL_S_IEXEC
;
3189 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3190 mode
|= ALL_S_IWRITE
;
3192 buf
->st_mode
= mode
;
3194 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3195 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3197 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3198 buf
->st_mtime
= buf
->st_ctime
= dw
;
3199 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3200 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3201 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3205 /*********************************************************************
3206 * _wstati64 (MSVCRT.@)
3208 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3211 struct MSVCRT__stat64 buf64
;
3213 ret
= MSVCRT__wstat64(path
, &buf64
);
3215 msvcrt_stat64_to_stati64(&buf64
, buf
);
3219 /*********************************************************************
3222 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3225 struct MSVCRT__stat64 buf64
;
3227 ret
= MSVCRT__wstat64( path
, &buf64
);
3228 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3232 #if _MSVCR_VER >= 80
3234 /*********************************************************************
3235 * _wstat32 (MSVCR80.@)
3237 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3240 struct MSVCRT__stat64 buf64
;
3242 ret
= MSVCRT__wstat64(path
, &buf64
);
3244 msvcrt_stat64_to_stat32(&buf64
, buf
);
3248 /*********************************************************************
3249 * _wstat32i64 (MSVCR80.@)
3251 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3254 struct MSVCRT__stat64 buf64
;
3256 ret
= MSVCRT__wstat64(path
, &buf64
);
3258 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3262 /*********************************************************************
3263 * _wstat64i32 (MSVCR80.@)
3265 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3268 struct MSVCRT__stat64 buf64
;
3270 ret
= MSVCRT__wstat64(path
, &buf64
);
3272 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3276 #endif /* _MSVCR_VER >= 80 */
3278 /*********************************************************************
3281 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3283 return MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
3286 /*********************************************************************
3287 * _telli64 (MSVCRT.@)
3289 __int64 CDECL
_telli64(int fd
)
3291 return MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
3294 /*********************************************************************
3295 * _tempnam (MSVCRT.@)
3297 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3299 char tmpbuf
[MAX_PATH
];
3300 const char *tmp_dir
= MSVCRT_getenv("TMP");
3302 if (tmp_dir
) dir
= tmp_dir
;
3304 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3305 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3307 TRACE("got name (%s)\n",tmpbuf
);
3308 DeleteFileA(tmpbuf
);
3309 return MSVCRT__strdup(tmpbuf
);
3311 TRACE("failed (%d)\n",GetLastError());
3315 /*********************************************************************
3316 * _wtempnam (MSVCRT.@)
3318 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3320 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3321 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3322 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3324 if (tmp_dir
) dir
= tmp_dir
;
3326 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3327 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3329 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3330 DeleteFileW(tmpbuf
);
3331 return MSVCRT__wcsdup(tmpbuf
);
3333 TRACE("failed (%d)\n",GetLastError());
3337 /*********************************************************************
3340 int CDECL
MSVCRT__umask(int umask
)
3342 int old_umask
= MSVCRT_umask
;
3343 TRACE("(%d)\n",umask
);
3344 MSVCRT_umask
= umask
;
3348 /*********************************************************************
3349 * _utime64 (MSVCRT.@)
3351 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3353 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3357 int retVal
= _futime64(fd
, t
);
3364 /*********************************************************************
3365 * _utime32 (MSVCRT.@)
3367 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3371 struct MSVCRT___utimbuf64 t64
;
3372 t64
.actime
= t
->actime
;
3373 t64
.modtime
= t
->modtime
;
3374 return _utime64( path
, &t64
);
3377 return _utime64( path
, NULL
);
3380 /*********************************************************************
3384 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3386 return _utime64( path
, t
);
3389 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3391 return _utime32( path
, t
);
3395 /*********************************************************************
3396 * _wutime64 (MSVCRT.@)
3398 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3400 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3404 int retVal
= _futime64(fd
, t
);
3411 /*********************************************************************
3412 * _wutime32 (MSVCRT.@)
3414 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3418 struct MSVCRT___utimbuf64 t64
;
3419 t64
.actime
= t
->actime
;
3420 t64
.modtime
= t
->modtime
;
3421 return _wutime64( path
, &t64
);
3424 return _wutime64( path
, NULL
);
3427 /*********************************************************************
3428 * _wutime (MSVCRT.@)
3431 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3433 return _wutime64( path
, t
);
3436 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3438 return _wutime32( path
, t
);
3442 /*********************************************************************
3445 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3448 ioinfo
*info
= get_ioinfo(fd
);
3449 HANDLE hand
= info
->handle
;
3451 /* Don't trace small writes, it gets *very* annoying */
3454 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3456 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3458 *MSVCRT__errno() = MSVCRT_EBADF
;
3459 release_ioinfo(info
);
3463 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3465 *MSVCRT__errno() = MSVCRT_EINVAL
;
3466 release_ioinfo(info
);
3470 /* If appending, go to EOF */
3471 if (info
->wxflag
& WX_APPEND
)
3472 MSVCRT__lseek(fd
, 0, FILE_END
);
3474 if (!(info
->wxflag
& WX_TEXT
))
3476 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3477 && (num_written
== count
))
3479 release_ioinfo(info
);
3482 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3483 hand
, GetLastError());
3484 msvcrt_set_errno(GetLastError());
3488 unsigned int i
, j
, nr_lf
, size
;
3491 const char *s
= buf
;
3493 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3495 /* find number of \n */
3496 for (nr_lf
=0, i
=0; i
<count
; i
++)
3502 if ((q
= p
= MSVCRT_malloc(size
)))
3504 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3513 FIXME("Malloc failed\n");
3525 else if (info
->exflag
& EF_UTF16
)
3527 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3528 if (s
[i
]=='\n' && s
[i
+1]==0)
3533 if ((q
= p
= MSVCRT_malloc(size
)))
3535 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3537 if (s
[i
]=='\n' && s
[i
+1]==0)
3548 FIXME("Malloc failed\n");
3564 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3565 if (s
[i
]=='\n' && s
[i
+1]==0)
3568 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3570 msvcrt_set_errno(GetLastError());
3572 release_ioinfo(info
);
3576 size
= conv_len
+nr_lf
;
3577 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3579 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3581 if (s
[i
]=='\n' && s
[i
+1]==0)
3589 q
= p
+count
+nr_lf
*2;
3590 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3591 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3595 FIXME("Malloc failed\n");
3602 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3604 release_ioinfo(info
);
3606 if (num_written
!= size
)
3608 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3609 fd
, hand
, GetLastError(), num_written
);
3610 msvcrt_set_errno(GetLastError());
3616 release_ioinfo(info
);
3620 /*********************************************************************
3623 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3627 MSVCRT__lock_file(file
);
3628 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3629 if (len
== sizeof(val
)) {
3630 MSVCRT__unlock_file(file
);
3634 file
->_flag
|= MSVCRT__IOERR
;
3635 MSVCRT__unlock_file(file
);
3639 /*********************************************************************
3642 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3646 MSVCRT__lock_file(file
);
3647 ret
= MSVCRT__fclose_nolock(file
);
3648 MSVCRT__unlock_file(file
);
3653 /*********************************************************************
3654 * _fclose_nolock (MSVCRT.@)
3656 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3660 if(!(file
->_flag
& (MSVCRT__IOREAD
| MSVCRT__IOWRT
| MSVCRT__IORW
)))
3667 MSVCRT_free(file
->_tmpfname
);
3668 file
->_tmpfname
= NULL
;
3669 /* flush stdio buffers */
3670 if(file
->_flag
& MSVCRT__IOWRT
)
3671 MSVCRT__fflush_nolock(file
);
3672 if(file
->_flag
& MSVCRT__IOMYBUF
)
3673 MSVCRT_free(file
->_base
);
3675 r
=MSVCRT__close(file
->_file
);
3678 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3681 /*********************************************************************
3684 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3686 return file
->_flag
& MSVCRT__IOEOF
;
3689 /*********************************************************************
3692 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3694 return file
->_flag
& MSVCRT__IOERR
;
3697 /*********************************************************************
3698 * _filbuf (MSVCRT.@)
3700 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3704 if(file
->_flag
& MSVCRT__IOSTRG
)
3707 /* Allocate buffer if needed */
3708 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3709 msvcrt_alloc_buffer(file
);
3711 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3712 if(file
->_flag
& MSVCRT__IORW
)
3713 file
->_flag
|= MSVCRT__IOREAD
;
3718 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3720 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3721 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3727 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3729 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3735 file
->_ptr
= file
->_base
+1;
3736 c
= *(unsigned char *)file
->_base
;
3741 /*********************************************************************
3744 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3748 MSVCRT__lock_file(file
);
3749 ret
= MSVCRT__fgetc_nolock(file
);
3750 MSVCRT__unlock_file(file
);
3755 /*********************************************************************
3756 * _fgetc_nolock (MSVCRT.@)
3758 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3765 i
= (unsigned char *)file
->_ptr
++;
3768 j
= MSVCRT__filbuf(file
);
3773 /*********************************************************************
3774 * _fgetchar (MSVCRT.@)
3776 int CDECL
MSVCRT__fgetchar(void)
3778 return MSVCRT_fgetc(MSVCRT_stdin
);
3781 /*********************************************************************
3784 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3786 int cc
= MSVCRT_EOF
;
3787 char * buf_start
= s
;
3789 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3790 file
,file
->_file
,s
,size
);
3792 MSVCRT__lock_file(file
);
3794 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3799 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3801 TRACE(":nothing read\n");
3802 MSVCRT__unlock_file(file
);
3805 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3808 TRACE(":got %s\n", debugstr_a(buf_start
));
3809 MSVCRT__unlock_file(file
);
3813 /*********************************************************************
3816 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3820 MSVCRT__lock_file(file
);
3821 ret
= MSVCRT__fgetwc_nolock(file
);
3822 MSVCRT__unlock_file(file
);
3827 /*********************************************************************
3828 * _fgetwc_nolock (MSVCRT.@)
3830 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3835 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3836 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3839 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3840 ch
= MSVCRT__fgetc_nolock(file
);
3841 if(ch
== MSVCRT_EOF
) {
3848 char mbs
[MSVCRT_MB_LEN_MAX
];
3851 ch
= MSVCRT__fgetc_nolock(file
);
3852 if(ch
!= MSVCRT_EOF
) {
3854 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3855 ch
= MSVCRT__fgetc_nolock(file
);
3856 if(ch
!= MSVCRT_EOF
) {
3865 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3872 /*********************************************************************
3875 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3882 MSVCRT__lock_file(file
);
3883 for (j
=0; j
<sizeof(int); j
++) {
3884 k
= MSVCRT__fgetc_nolock(file
);
3885 if (k
== MSVCRT_EOF
) {
3886 file
->_flag
|= MSVCRT__IOEOF
;
3887 MSVCRT__unlock_file(file
);
3893 MSVCRT__unlock_file(file
);
3897 /*********************************************************************
3900 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3902 return MSVCRT_fgetwc(file
);
3905 /*********************************************************************
3906 * _fgetwchar (MSVCRT.@)
3908 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3910 return MSVCRT_fgetwc(MSVCRT_stdin
);
3913 /*********************************************************************
3914 * getwchar (MSVCRT.@)
3916 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3918 return MSVCRT__fgetwchar();
3921 /*********************************************************************
3924 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3926 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3927 MSVCRT_wchar_t
* buf_start
= s
;
3929 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3930 file
,file
->_file
,s
,size
);
3932 MSVCRT__lock_file(file
);
3934 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3939 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3941 TRACE(":nothing read\n");
3942 MSVCRT__unlock_file(file
);
3945 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3948 TRACE(":got %s\n", debugstr_w(buf_start
));
3949 MSVCRT__unlock_file(file
);
3953 /*********************************************************************
3954 * _flsbuf (MSVCRT.@)
3956 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3958 /* Flush output buffer */
3959 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3960 msvcrt_alloc_buffer(file
);
3963 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3964 if(!(file
->_flag
& MSVCRT__IORW
)) {
3965 file
->_flag
|= MSVCRT__IOERR
;
3966 *MSVCRT__errno() = MSVCRT_EBADF
;
3969 file
->_flag
|= MSVCRT__IOWRT
;
3971 if(file
->_flag
& MSVCRT__IOREAD
) {
3972 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3973 file
->_flag
|= MSVCRT__IOERR
;
3977 file
->_ptr
= file
->_base
;
3978 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3981 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3984 if(file
->_cnt
<= 0) {
3985 res
= msvcrt_flush_buffer(file
);
3988 file
->_flag
|= MSVCRT__IOWRT
;
3989 file
->_cnt
=file
->_bufsiz
;
3997 /* set _cnt to 0 for unbuffered FILEs */
3999 len
= MSVCRT__write(file
->_file
, &cc
, 1);
4002 file
->_flag
|= MSVCRT__IOERR
;
4007 /*********************************************************************
4010 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4014 MSVCRT__lock_file(file
);
4015 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
4016 MSVCRT__unlock_file(file
);
4021 /*********************************************************************
4022 * _fwrite_nolock (MSVCRT.@)
4024 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4026 MSVCRT_size_t wrcnt
=size
* nmemb
;
4032 if(file
->_cnt
< 0) {
4033 WARN("negative file->_cnt value in %p\n", file
);
4034 file
->_flag
|= MSVCRT__IOERR
;
4036 } else if(file
->_cnt
) {
4037 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
4038 memcpy(file
->_ptr
, ptr
, pcnt
);
4043 ptr
= (const char*)ptr
+ pcnt
;
4044 } else if((file
->_flag
& MSVCRT__IONBF
)
4045 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4046 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4050 if(file
->_flag
& MSVCRT__IONBF
)
4052 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4053 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4055 bufsiz
= file
->_bufsiz
;
4057 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4059 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
4062 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
4063 file
->_flag
|= MSVCRT__IOERR
;
4068 ptr
= (const char*)ptr
+ pcnt
;
4070 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
4074 ptr
= (const char*)ptr
+ 1;
4078 return written
/ size
;
4081 /*********************************************************************
4084 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4088 MSVCRT__lock_file(file
);
4089 ret
= MSVCRT__fputwc_nolock(wc
, file
);
4090 MSVCRT__unlock_file(file
);
4095 /*********************************************************************
4096 * _fputwc_nolock (MSVCRT.@)
4098 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4100 MSVCRT_wchar_t mwc
=wc
;
4104 fdinfo
= get_ioinfo_nolock(file
->_file
);
4106 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
4107 char buf
[MSVCRT_MB_LEN_MAX
];
4110 char_len
= MSVCRT_wctomb(buf
, mwc
);
4111 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
4115 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4124 /*********************************************************************
4125 * _fputwchar (MSVCRT.@)
4127 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
4129 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
4132 /*********************************************************************
4133 * _wfsopen (MSVCRT.@)
4135 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
4138 int open_flags
, stream_flags
, fd
;
4140 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4142 /* map mode string to open() flags. "man fopen" for possibilities. */
4143 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4147 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4150 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4152 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4159 TRACE(":got (%p)\n",file
);
4160 if (fd
>= 0 && !file
)
4166 /*********************************************************************
4167 * _fsopen (MSVCRT.@)
4169 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4172 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4174 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4175 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4176 *MSVCRT__errno() = MSVCRT_EINVAL
;
4179 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4182 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4183 *MSVCRT__errno() = MSVCRT_EINVAL
;
4187 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4194 /*********************************************************************
4197 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4199 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4202 /*********************************************************************
4203 * fopen_s (MSVCRT.@)
4205 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4206 const char *filename
, const char *mode
)
4208 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4209 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4210 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4212 *pFile
= MSVCRT_fopen(filename
, mode
);
4215 return *MSVCRT__errno();
4219 /*********************************************************************
4220 * _wfopen (MSVCRT.@)
4222 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4224 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4227 /*********************************************************************
4228 * _wfopen_s (MSVCRT.@)
4230 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4231 const MSVCRT_wchar_t
*mode
)
4233 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4234 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4235 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4237 *pFile
= MSVCRT__wfopen(filename
, mode
);
4240 return *MSVCRT__errno();
4244 /*********************************************************************
4247 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4251 MSVCRT__lock_file(file
);
4252 ret
= MSVCRT__fputc_nolock(c
, file
);
4253 MSVCRT__unlock_file(file
);
4258 /*********************************************************************
4259 * _fputc_nolock (MSVCRT.@)
4261 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4270 res
= msvcrt_flush_buffer(file
);
4271 return res
? res
: c
;
4277 res
= MSVCRT__flsbuf(c
, file
);
4282 /*********************************************************************
4283 * _fputchar (MSVCRT.@)
4285 int CDECL
MSVCRT__fputchar(int c
)
4287 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4290 /*********************************************************************
4293 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4297 MSVCRT__lock_file(file
);
4298 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4299 MSVCRT__unlock_file(file
);
4304 /*********************************************************************
4305 * _fread_nolock (MSVCRT.@)
4307 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4309 MSVCRT_size_t rcnt
=size
* nmemb
;
4310 MSVCRT_size_t read
=0;
4311 MSVCRT_size_t pread
=0;
4316 /* first buffered data */
4318 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4319 memcpy(ptr
, file
->_ptr
, pcnt
);
4324 ptr
= (char*)ptr
+ pcnt
;
4325 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4326 if(file
->_flag
& MSVCRT__IORW
) {
4327 file
->_flag
|= MSVCRT__IOREAD
;
4333 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4334 msvcrt_alloc_buffer(file
);
4339 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4340 i
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4341 file
->_ptr
= file
->_base
;
4344 if (i
> rcnt
) i
= rcnt
;
4346 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4347 if (i
> 0 && i
< file
->_cnt
) {
4348 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4349 file
->_flag
&= ~MSVCRT__IOEOF
;
4352 memcpy(ptr
, file
->_ptr
, i
);
4356 } else if (rcnt
> INT_MAX
) {
4357 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4358 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4359 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4361 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4365 ptr
= (char *)ptr
+i
;
4366 /* expose feof condition in the flags
4367 * MFC tests file->_flag for feof, and doesn't call feof())
4369 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4370 file
->_flag
|= MSVCRT__IOEOF
;
4373 file
->_flag
|= MSVCRT__IOERR
;
4383 #if _MSVCR_VER >= 80
4385 /*********************************************************************
4386 * fread_s (MSVCR80.@)
4388 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4389 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4393 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4395 memset(buf
, 0, buf_size
);
4398 if(!elem_size
|| !count
) return 0;
4400 MSVCRT__lock_file(stream
);
4401 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4402 MSVCRT__unlock_file(stream
);
4407 /*********************************************************************
4408 * _fread_nolock_s (MSVCR80.@)
4410 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4411 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4413 size_t bytes_left
, buf_pos
;
4415 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4417 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4419 memset(buf
, 0, buf_size
);
4422 if(!elem_size
|| !count
) return 0;
4423 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4424 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4426 bytes_left
= elem_size
*count
;
4429 if(stream
->_cnt
> 0) {
4430 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4432 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4433 memset(buf
, 0, buf_size
);
4437 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4441 int c
= MSVCRT__filbuf(stream
);
4446 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4447 memset(buf
, 0, buf_size
);
4451 ((char*)buf
)[buf_pos
++] = c
;
4456 return buf_pos
/elem_size
;
4459 #endif /* _MSVCR_VER >= 80 */
4461 /*********************************************************************
4462 * _wfreopen (MSVCRT.@)
4465 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4467 int open_flags
, stream_flags
, fd
;
4469 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4472 if (!file
|| ((fd
= file
->_file
) < 0))
4476 MSVCRT_fclose(file
);
4477 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4479 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4481 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4491 /*********************************************************************
4492 * _wfreopen_s (MSVCRT.@)
4494 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4495 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4497 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4498 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4499 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4500 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4502 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4505 return *MSVCRT__errno();
4509 /*********************************************************************
4510 * freopen (MSVCRT.@)
4513 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4516 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4518 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4519 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4525 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4532 /*********************************************************************
4533 * freopen_s (MSVCRT.@)
4535 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4536 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4538 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4539 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4540 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4541 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4543 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4546 return *MSVCRT__errno();
4550 /*********************************************************************
4551 * fsetpos (MSVCRT.@)
4553 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4557 MSVCRT__lock_file(file
);
4558 msvcrt_flush_buffer(file
);
4560 /* Reset direction of i/o */
4561 if(file
->_flag
& MSVCRT__IORW
) {
4562 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4565 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,MSVCRT_SEEK_SET
) == -1) ? -1 : 0;
4566 MSVCRT__unlock_file(file
);
4570 /*********************************************************************
4571 * _ftelli64 (MSVCRT.@)
4573 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4577 MSVCRT__lock_file(file
);
4578 ret
= MSVCRT__ftelli64_nolock(file
);
4579 MSVCRT__unlock_file(file
);
4584 /*********************************************************************
4585 * _ftelli64_nolock (MSVCRT.@)
4587 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4591 pos
= _telli64(file
->_file
);
4594 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4595 if(file
->_flag
& MSVCRT__IOWRT
) {
4596 pos
+= file
->_ptr
- file
->_base
;
4598 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4601 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4605 } else if(!file
->_cnt
) { /* nothing to do */
4606 } else if(MSVCRT__lseeki64(file
->_file
, 0, MSVCRT_SEEK_END
)==pos
) {
4610 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4611 for(i
=0; i
<file
->_cnt
; i
++)
4612 if(file
->_ptr
[i
] == '\n')
4618 if(MSVCRT__lseeki64(file
->_file
, pos
, MSVCRT_SEEK_SET
) != pos
)
4621 pos
-= file
->_bufsiz
;
4622 pos
+= file
->_ptr
- file
->_base
;
4624 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4625 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4628 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4638 /*********************************************************************
4641 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4643 return MSVCRT__ftelli64(file
);
4646 #if _MSVCR_VER >= 80
4647 /*********************************************************************
4648 * _ftell_nolock (MSVCR80.@)
4650 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4652 return MSVCRT__ftelli64_nolock(file
);
4656 /*********************************************************************
4657 * fgetpos (MSVCRT.@)
4659 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4661 *pos
= MSVCRT__ftelli64(file
);
4667 /*********************************************************************
4670 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4672 MSVCRT_size_t len
= strlen(s
);
4675 MSVCRT__lock_file(file
);
4676 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4677 MSVCRT__unlock_file(file
);
4681 /*********************************************************************
4684 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4686 MSVCRT_size_t i
, len
= strlenW(s
);
4690 MSVCRT__lock_file(file
);
4691 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4692 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4693 MSVCRT__unlock_file(file
);
4697 tmp_buf
= add_std_buffer(file
);
4698 for (i
=0; i
<len
; i
++) {
4699 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4700 if(tmp_buf
) remove_std_buffer(file
);
4701 MSVCRT__unlock_file(file
);
4706 if(tmp_buf
) remove_std_buffer(file
);
4707 MSVCRT__unlock_file(file
);
4711 /*********************************************************************
4712 * getchar (MSVCRT.@)
4714 int CDECL
MSVCRT_getchar(void)
4716 return MSVCRT_fgetc(MSVCRT_stdin
);
4719 /*********************************************************************
4722 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4724 return MSVCRT_fgetc(file
);
4727 /*********************************************************************
4728 * gets_s (MSVCR80.@)
4730 char * CDECL
MSVCRT_gets_s(char *buf
, MSVCRT_size_t len
)
4732 char *buf_start
= buf
;
4735 if (!MSVCRT_CHECK_PMT(buf
!= NULL
)) return NULL
;
4736 if (!MSVCRT_CHECK_PMT(len
!= 0)) return NULL
;
4738 MSVCRT__lock_file(MSVCRT_stdin
);
4739 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
);
4740 len
!= 0 && cc
!= MSVCRT_EOF
&& cc
!= '\n';
4741 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4749 MSVCRT__unlock_file(MSVCRT_stdin
);
4754 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4758 if ((cc
== MSVCRT_EOF
) && (buf_start
== buf
))
4760 TRACE(":nothing read\n");
4765 TRACE("got '%s'\n", buf_start
);
4769 /*********************************************************************
4772 char * CDECL
MSVCRT_gets(char *buf
)
4774 return MSVCRT_gets_s(buf
, -1);
4777 /*********************************************************************
4780 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4783 MSVCRT_wchar_t
* ws
= buf
;
4785 MSVCRT__lock_file(MSVCRT_stdin
);
4786 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4787 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4790 *buf
++ = (MSVCRT_wchar_t
)cc
;
4792 MSVCRT__unlock_file(MSVCRT_stdin
);
4794 if ((cc
== MSVCRT_WEOF
) && (ws
== buf
))
4796 TRACE(":nothing read\n");
4801 TRACE("got %s\n", debugstr_w(ws
));
4805 /*********************************************************************
4808 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4810 return MSVCRT_fputc(c
, file
);
4813 /*********************************************************************
4814 * putchar (MSVCRT.@)
4816 int CDECL
MSVCRT_putchar(int c
)
4818 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4821 /*********************************************************************
4824 int CDECL
MSVCRT_puts(const char *s
)
4826 MSVCRT_size_t len
= strlen(s
);
4829 MSVCRT__lock_file(MSVCRT_stdout
);
4830 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4831 MSVCRT__unlock_file(MSVCRT_stdout
);
4835 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4836 MSVCRT__unlock_file(MSVCRT_stdout
);
4840 /*********************************************************************
4843 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4847 MSVCRT__lock_file(MSVCRT_stdout
);
4848 ret
= MSVCRT_fputws(s
, MSVCRT_stdout
);
4850 ret
= MSVCRT__fputwc_nolock('\n', MSVCRT_stdout
);
4851 MSVCRT__unlock_file(MSVCRT_stdout
);
4852 return ret
>= 0 ? 0 : MSVCRT_WEOF
;
4855 /*********************************************************************
4858 int CDECL
MSVCRT_remove(const char *path
)
4860 TRACE("(%s)\n",path
);
4861 if (DeleteFileA(path
))
4863 TRACE(":failed (%d)\n",GetLastError());
4864 msvcrt_set_errno(GetLastError());
4868 /*********************************************************************
4869 * _wremove (MSVCRT.@)
4871 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4873 TRACE("(%s)\n",debugstr_w(path
));
4874 if (DeleteFileW(path
))
4876 TRACE(":failed (%d)\n",GetLastError());
4877 msvcrt_set_errno(GetLastError());
4881 /*********************************************************************
4884 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4886 TRACE(":from %s to %s\n",oldpath
,newpath
);
4887 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4889 TRACE(":failed (%d)\n",GetLastError());
4890 msvcrt_set_errno(GetLastError());
4894 /*********************************************************************
4895 * _wrename (MSVCRT.@)
4897 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4899 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4900 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4902 TRACE(":failed (%d)\n",GetLastError());
4903 msvcrt_set_errno(GetLastError());
4907 /*********************************************************************
4908 * setvbuf (MSVCRT.@)
4910 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4912 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4913 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4914 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4916 MSVCRT__lock_file(file
);
4918 MSVCRT__fflush_nolock(file
);
4919 if(file
->_flag
& MSVCRT__IOMYBUF
)
4920 MSVCRT_free(file
->_base
);
4921 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4924 if(mode
== MSVCRT__IONBF
) {
4925 file
->_flag
|= MSVCRT__IONBF
;
4926 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4929 file
->_base
= file
->_ptr
= buf
;
4930 file
->_flag
|= MSVCRT__USERBUF
;
4931 file
->_bufsiz
= size
;
4933 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4936 MSVCRT__unlock_file(file
);
4940 file
->_flag
|= MSVCRT__IOMYBUF
;
4941 file
->_bufsiz
= size
;
4943 MSVCRT__unlock_file(file
);
4947 /*********************************************************************
4950 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4952 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4955 static int tmpnam_helper(char *s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
4961 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
4965 *MSVCRT__errno() = MSVCRT_ERANGE
;
4966 return MSVCRT_ERANGE
;
4971 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4972 if (digits
+1 > size
) {
4974 *MSVCRT__errno() = MSVCRT_ERANGE
;
4975 return MSVCRT_ERANGE
;
4977 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4983 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4984 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4988 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4989 if (digits
+1 > size
) {
4991 *MSVCRT__errno() = MSVCRT_ERANGE
;
4992 return MSVCRT_ERANGE
;
4994 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4997 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4998 GetLastError() == ERROR_FILE_NOT_FOUND
)
5004 int CDECL
MSVCRT_tmpnam_s(char *s
, MSVCRT_size_t size
)
5006 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5009 /*********************************************************************
5012 char * CDECL
MSVCRT_tmpnam(char *s
)
5015 thread_data_t
*data
= msvcrt_get_thread_data();
5017 if(!data
->tmpnam_buffer
)
5018 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
5020 s
= data
->tmpnam_buffer
;
5023 return tmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5026 static int wtmpnam_helper(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
5028 MSVCRT_wchar_t tmpstr
[8];
5029 MSVCRT_wchar_t
*p
= s
;
5032 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
5036 *MSVCRT__errno() = MSVCRT_ERANGE
;
5037 return MSVCRT_ERANGE
;
5042 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5043 if (digits
+1 > size
) {
5045 *MSVCRT__errno() = MSVCRT_ERANGE
;
5046 return MSVCRT_ERANGE
;
5048 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5054 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5055 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5059 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5060 if (digits
+1 > size
) {
5062 *MSVCRT__errno() = MSVCRT_ERANGE
;
5063 return MSVCRT_ERANGE
;
5065 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5068 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5069 GetLastError() == ERROR_FILE_NOT_FOUND
)
5075 /*********************************************************************
5076 * _wtmpnam_s (MSVCRT.@)
5078 int CDECL
MSVCRT__wtmpnam_s(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
)
5080 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5083 /*********************************************************************
5084 * _wtmpnam (MSVCRT.@)
5086 MSVCRT_wchar_t
* CDECL
MSVCRT__wtmpnam(MSVCRT_wchar_t
*s
)
5089 thread_data_t
*data
= msvcrt_get_thread_data();
5091 if(!data
->wtmpnam_buffer
)
5092 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
5094 s
= data
->wtmpnam_buffer
;
5097 return wtmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5100 /*********************************************************************
5101 * tmpfile (MSVCRT.@)
5103 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
5105 char *filename
= MSVCRT__tempnam(",", "t");
5107 MSVCRT_FILE
* file
= NULL
;
5110 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
5111 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
5112 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5114 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
5119 else file
->_tmpfname
= MSVCRT__strdup(filename
);
5122 if(fd
!= -1 && !file
)
5124 MSVCRT_free(filename
);
5129 /*********************************************************************
5130 * tmpfile_s (MSVCRT.@)
5132 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
5134 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
5136 *file
= MSVCRT_tmpfile();
5140 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5142 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
5145 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
5149 MSVCRT__lock_file(file
);
5151 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5152 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
5153 MSVCRT__unlock_file(file
);
5157 for(i
=0; i
<len
; i
++) {
5158 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
5159 MSVCRT__unlock_file(file
);
5164 MSVCRT__unlock_file(file
);
5168 static int vfprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const char *format
,
5169 MSVCRT__locale_t locale
, __ms_va_list valist
)
5171 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5175 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5176 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5178 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5179 memset(args_ctx
, 0, sizeof(args_ctx
));
5180 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5182 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5183 *MSVCRT__errno() = MSVCRT_EINVAL
;
5186 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5189 MSVCRT__lock_file(file
);
5190 tmp_buf
= add_std_buffer(file
);
5191 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5192 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5193 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5194 if(tmp_buf
) remove_std_buffer(file
);
5195 MSVCRT__unlock_file(file
);
5200 static int vfwprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5201 MSVCRT__locale_t locale
, __ms_va_list valist
)
5203 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5207 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5208 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5210 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5211 memset(args_ctx
, 0, sizeof(args_ctx
));
5212 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5214 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5215 *MSVCRT__errno() = MSVCRT_EINVAL
;
5218 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5221 MSVCRT__lock_file(file
);
5222 tmp_buf
= add_std_buffer(file
);
5223 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5224 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5225 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5226 if(tmp_buf
) remove_std_buffer(file
);
5227 MSVCRT__unlock_file(file
);
5232 /*********************************************************************
5233 * _vfprintf_s_l (MSVCRT.@)
5235 int CDECL
MSVCRT__vfprintf_s_l(MSVCRT_FILE
* file
, const char *format
,
5236 MSVCRT__locale_t locale
, __ms_va_list valist
)
5238 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5241 /*********************************************************************
5242 * _vfwprintf_s_l (MSVCRT.@)
5244 int CDECL
MSVCRT__vfwprintf_s_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5245 MSVCRT__locale_t locale
, __ms_va_list valist
)
5247 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5250 /*********************************************************************
5251 * vfprintf (MSVCRT.@)
5253 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5255 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5258 /*********************************************************************
5259 * vfprintf_s (MSVCRT.@)
5261 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5263 return MSVCRT__vfprintf_s_l(file
, format
, NULL
, valist
);
5266 /*********************************************************************
5267 * vfwprintf (MSVCRT.@)
5269 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5271 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5274 /*********************************************************************
5275 * vfwprintf_s (MSVCRT.@)
5277 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5279 return MSVCRT__vfwprintf_s_l(file
, format
, NULL
, valist
);
5282 #if _MSVCR_VER >= 140
5284 /*********************************************************************
5285 * __stdio_common_vfprintf (UCRTBASE.@)
5287 int CDECL
MSVCRT__stdio_common_vfprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5288 MSVCRT__locale_t locale
, __ms_va_list valist
)
5290 if (options
& ~UCRTBASE_PRINTF_MASK
)
5291 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5293 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5296 /*********************************************************************
5297 * __stdio_common_vfprintf_s (UCRTBASE.@)
5299 int CDECL
MSVCRT__stdio_common_vfprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5300 MSVCRT__locale_t locale
, __ms_va_list valist
)
5302 if (options
& ~UCRTBASE_PRINTF_MASK
)
5303 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5305 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5306 file
, format
, locale
, valist
);
5309 /*********************************************************************
5310 * __stdio_common_vfwprintf (UCRTBASE.@)
5312 int CDECL
MSVCRT__stdio_common_vfwprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5313 MSVCRT__locale_t locale
, __ms_va_list valist
)
5315 if (options
& ~UCRTBASE_PRINTF_MASK
)
5316 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5318 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5321 /*********************************************************************
5322 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5324 int CDECL
MSVCRT__stdio_common_vfwprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5325 MSVCRT__locale_t locale
, __ms_va_list valist
)
5327 if (options
& ~UCRTBASE_PRINTF_MASK
)
5328 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5330 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5331 file
, format
, locale
, valist
);
5334 #endif /* _MSVCR_VER >= 140 */
5336 /*********************************************************************
5337 * _vfprintf_l (MSVCRT.@)
5339 int CDECL
MSVCRT__vfprintf_l(MSVCRT_FILE
* file
, const char *format
,
5340 MSVCRT__locale_t locale
, __ms_va_list valist
)
5342 return vfprintf_helper(0, file
, format
, locale
, valist
);
5345 /*********************************************************************
5346 * _vfwprintf_l (MSVCRT.@)
5348 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5349 MSVCRT__locale_t locale
, __ms_va_list valist
)
5351 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5354 /*********************************************************************
5355 * _vfprintf_p_l (MSVCRT.@)
5357 int CDECL
MSVCRT__vfprintf_p_l(MSVCRT_FILE
* file
, const char *format
,
5358 MSVCRT__locale_t locale
, __ms_va_list valist
)
5360 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5361 file
, format
, locale
, valist
);
5364 /*********************************************************************
5365 * _vfprintf_p (MSVCRT.@)
5367 int CDECL
MSVCRT__vfprintf_p(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5369 return MSVCRT__vfprintf_p_l(file
, format
, NULL
, valist
);
5372 /*********************************************************************
5373 * _vfwprintf_p_l (MSVCRT.@)
5375 int CDECL
MSVCRT__vfwprintf_p_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5376 MSVCRT__locale_t locale
, __ms_va_list valist
)
5378 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5379 file
, format
, locale
, valist
);
5382 /*********************************************************************
5383 * _vfwprintf_p (MSVCRT.@)
5385 int CDECL
MSVCRT__vfwprintf_p(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5387 return MSVCRT__vfwprintf_p_l(file
, format
, NULL
, valist
);
5390 /*********************************************************************
5391 * vprintf (MSVCRT.@)
5393 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5395 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5398 /*********************************************************************
5399 * vprintf_s (MSVCRT.@)
5401 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5403 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5406 /*********************************************************************
5407 * vwprintf (MSVCRT.@)
5409 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5411 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5414 /*********************************************************************
5415 * vwprintf_s (MSVCRT.@)
5417 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5419 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5422 /*********************************************************************
5423 * fprintf (MSVCRT.@)
5425 int WINAPIV
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5427 __ms_va_list valist
;
5429 __ms_va_start(valist
, format
);
5430 res
= MSVCRT_vfprintf(file
, format
, valist
);
5431 __ms_va_end(valist
);
5435 /*********************************************************************
5436 * fprintf_s (MSVCRT.@)
5438 int WINAPIV
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5440 __ms_va_list valist
;
5442 __ms_va_start(valist
, format
);
5443 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5444 __ms_va_end(valist
);
5448 /*********************************************************************
5449 * fwprintf (MSVCRT.@)
5451 int WINAPIV
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5453 __ms_va_list valist
;
5455 __ms_va_start(valist
, format
);
5456 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5457 __ms_va_end(valist
);
5461 /*********************************************************************
5462 * fwprintf_s (MSVCRT.@)
5464 int WINAPIV
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5466 __ms_va_list valist
;
5468 __ms_va_start(valist
, format
);
5469 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5470 __ms_va_end(valist
);
5474 /*********************************************************************
5475 * _fwprintf_l (MSVCRT.@)
5477 int WINAPIV
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5479 __ms_va_list valist
;
5481 __ms_va_start(valist
, locale
);
5482 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5483 __ms_va_end(valist
);
5487 /*********************************************************************
5490 int WINAPIV
MSVCRT_printf(const char *format
, ...)
5492 __ms_va_list valist
;
5494 __ms_va_start(valist
, format
);
5495 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5496 __ms_va_end(valist
);
5500 /*********************************************************************
5501 * printf_s (MSVCRT.@)
5503 int WINAPIV
MSVCRT_printf_s(const char *format
, ...)
5505 __ms_va_list valist
;
5507 __ms_va_start(valist
, format
);
5508 res
= MSVCRT_vprintf_s(format
, valist
);
5509 __ms_va_end(valist
);
5513 /*********************************************************************
5516 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5520 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5522 MSVCRT__lock_file(file
);
5523 ret
= MSVCRT__ungetc_nolock(c
, file
);
5524 MSVCRT__unlock_file(file
);
5529 /*********************************************************************
5530 * _ungetc_nolock (MSVCRT.@)
5532 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5534 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5536 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5537 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5540 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5541 && msvcrt_alloc_buffer(file
))
5542 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5545 if(file
->_ptr
>file
->_base
) {
5547 if(file
->_flag
& MSVCRT__IOSTRG
) {
5548 if(*file
->_ptr
!= c
) {
5556 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5557 file
->_flag
|= MSVCRT__IOREAD
;
5564 /*********************************************************************
5565 * ungetwc (MSVCRT.@)
5567 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5571 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5573 MSVCRT__lock_file(file
);
5574 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5575 MSVCRT__unlock_file(file
);
5580 /*********************************************************************
5581 * _ungetwc_nolock (MSVCRT.@)
5583 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5585 MSVCRT_wchar_t mwc
= wc
;
5587 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5588 if (wc
== MSVCRT_WEOF
)
5591 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5592 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5593 unsigned char * pp
= (unsigned char *)&mwc
;
5596 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5597 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5601 char mbs
[MSVCRT_MB_LEN_MAX
];
5604 len
= MSVCRT_wctomb(mbs
, mwc
);
5608 for(len
--; len
>=0; len
--) {
5609 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5617 /*********************************************************************
5618 * wprintf (MSVCRT.@)
5620 int WINAPIV
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5622 __ms_va_list valist
;
5624 __ms_va_start(valist
, format
);
5625 res
= MSVCRT_vwprintf(format
, valist
);
5626 __ms_va_end(valist
);
5630 /*********************************************************************
5631 * wprintf_s (MSVCRT.@)
5633 int WINAPIV
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5635 __ms_va_list valist
;
5637 __ms_va_start(valist
, format
);
5638 res
= MSVCRT_vwprintf_s(format
, valist
);
5639 __ms_va_end(valist
);
5643 /*********************************************************************
5644 * _getmaxstdio (MSVCRT.@)
5646 int CDECL
MSVCRT__getmaxstdio(void)
5648 return MSVCRT_max_streams
;
5651 /*********************************************************************
5652 * _setmaxstdio (MSVCRT.@)
5654 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5656 TRACE("%d\n", newmax
);
5658 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5661 MSVCRT_max_streams
= newmax
;
5662 return MSVCRT_max_streams
;
5665 #if _MSVCR_VER >= 140
5666 /*********************************************************************
5667 * _get_stream_buffer_pointers (UCRTBASE.@)
5669 int CDECL
MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE
*file
, char*** base
,
5670 char*** ptr
, int** count
)
5673 *base
= &file
->_base
;
5677 *count
= &file
->_cnt
;