2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READNL 0x04 /* read started with \n */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
71 /* values for exflag - it's used differently in msvcr90.dll*/
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom
[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
92 CRITICAL_SECTION crit
;
98 BOOL utf8translations
;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
119 CRITICAL_SECTION crit
;
122 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
123 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
124 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
126 /* INTERNAL: process umask */
127 static int MSVCRT_umask
= 0;
129 /* INTERNAL: static data for tmpnam and _wtmpname functions */
130 static int tmpnam_unique
;
131 static int tmpnam_s_unique
;
133 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
134 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
135 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
136 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
138 #define TOUL(x) (ULONGLONG)(x)
139 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
140 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
141 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
142 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
144 /* This critical section protects the MSVCRT_fstreams table
145 * and MSVCRT_stream_idx from race conditions. It also
146 * protects fd critical sections creation code.
148 static CRITICAL_SECTION MSVCRT_file_cs
;
149 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
151 0, 0, &MSVCRT_file_cs
,
152 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
153 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
155 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
156 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
157 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
159 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
161 buf
->st_dev
= buf64
->st_dev
;
162 buf
->st_ino
= buf64
->st_ino
;
163 buf
->st_mode
= buf64
->st_mode
;
164 buf
->st_nlink
= buf64
->st_nlink
;
165 buf
->st_uid
= buf64
->st_uid
;
166 buf
->st_gid
= buf64
->st_gid
;
167 buf
->st_rdev
= buf64
->st_rdev
;
168 buf
->st_size
= buf64
->st_size
;
169 buf
->st_atime
= buf64
->st_atime
;
170 buf
->st_mtime
= buf64
->st_mtime
;
171 buf
->st_ctime
= buf64
->st_ctime
;
174 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
176 buf
->st_dev
= buf64
->st_dev
;
177 buf
->st_ino
= buf64
->st_ino
;
178 buf
->st_mode
= buf64
->st_mode
;
179 buf
->st_nlink
= buf64
->st_nlink
;
180 buf
->st_uid
= buf64
->st_uid
;
181 buf
->st_gid
= buf64
->st_gid
;
182 buf
->st_rdev
= buf64
->st_rdev
;
183 buf
->st_size
= buf64
->st_size
;
184 buf
->st_atime
= buf64
->st_atime
;
185 buf
->st_mtime
= buf64
->st_mtime
;
186 buf
->st_ctime
= buf64
->st_ctime
;
189 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
191 buf
->st_dev
= buf64
->st_dev
;
192 buf
->st_ino
= buf64
->st_ino
;
193 buf
->st_mode
= buf64
->st_mode
;
194 buf
->st_nlink
= buf64
->st_nlink
;
195 buf
->st_uid
= buf64
->st_uid
;
196 buf
->st_gid
= buf64
->st_gid
;
197 buf
->st_rdev
= buf64
->st_rdev
;
198 buf
->st_size
= buf64
->st_size
;
199 buf
->st_atime
= buf64
->st_atime
;
200 buf
->st_mtime
= buf64
->st_mtime
;
201 buf
->st_ctime
= buf64
->st_ctime
;
204 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
206 buf
->st_dev
= buf64
->st_dev
;
207 buf
->st_ino
= buf64
->st_ino
;
208 buf
->st_mode
= buf64
->st_mode
;
209 buf
->st_nlink
= buf64
->st_nlink
;
210 buf
->st_uid
= buf64
->st_uid
;
211 buf
->st_gid
= buf64
->st_gid
;
212 buf
->st_rdev
= buf64
->st_rdev
;
213 buf
->st_size
= buf64
->st_size
;
214 buf
->st_atime
= buf64
->st_atime
;
215 buf
->st_mtime
= buf64
->st_mtime
;
216 buf
->st_ctime
= buf64
->st_ctime
;
219 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
221 buf
->st_dev
= buf64
->st_dev
;
222 buf
->st_ino
= buf64
->st_ino
;
223 buf
->st_mode
= buf64
->st_mode
;
224 buf
->st_nlink
= buf64
->st_nlink
;
225 buf
->st_uid
= buf64
->st_uid
;
226 buf
->st_gid
= buf64
->st_gid
;
227 buf
->st_rdev
= buf64
->st_rdev
;
228 buf
->st_size
= buf64
->st_size
;
229 buf
->st_atime
= buf64
->st_atime
;
230 buf
->st_mtime
= buf64
->st_mtime
;
231 buf
->st_ctime
= buf64
->st_ctime
;
234 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
236 /* 1601 to 1970 is 369 years plus 89 leap days */
237 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
239 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
240 ft
->dwHighDateTime
= ticks
>> 32;
241 ft
->dwLowDateTime
= ticks
;
244 static inline ioinfo
* get_ioinfo_nolock(int fd
)
247 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
248 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
250 return &MSVCRT___badioinfo
;
252 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
255 static inline void init_ioinfo_cs(ioinfo
*info
)
257 if(!(info
->exflag
& EF_CRIT_INIT
)) {
259 if(!(info
->exflag
& EF_CRIT_INIT
)) {
260 InitializeCriticalSection(&info
->crit
);
261 info
->exflag
|= EF_CRIT_INIT
;
267 static inline ioinfo
* get_ioinfo(int fd
)
269 ioinfo
*ret
= get_ioinfo_nolock(fd
);
270 if(ret
== &MSVCRT___badioinfo
)
273 EnterCriticalSection(&ret
->crit
);
277 static inline BOOL
alloc_pioinfo_block(int fd
)
282 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
284 *MSVCRT__errno() = MSVCRT_ENFILE
;
288 block
= MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
291 WARN(":out of memory!\n");
292 *MSVCRT__errno() = MSVCRT_ENOMEM
;
295 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
296 block
[i
].handle
= INVALID_HANDLE_VALUE
;
297 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
302 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
306 ret
= get_ioinfo(fd
);
307 if(ret
!= &MSVCRT___badioinfo
)
310 if(!alloc_pioinfo_block(fd
))
311 return &MSVCRT___badioinfo
;
313 return get_ioinfo(fd
);
316 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
321 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
323 ioinfo
*info
= get_ioinfo_nolock(i
);
325 if(info
== &MSVCRT___badioinfo
)
327 if(!alloc_pioinfo_block(i
))
328 return &MSVCRT___badioinfo
;
329 info
= get_ioinfo_nolock(i
);
332 init_ioinfo_cs(info
);
333 if(TryEnterCriticalSection(&info
->crit
))
335 if(info
->handle
== INVALID_HANDLE_VALUE
)
340 LeaveCriticalSection(&info
->crit
);
344 WARN(":files exhausted!\n");
345 *MSVCRT__errno() = MSVCRT_ENFILE
;
346 return &MSVCRT___badioinfo
;
349 static inline void release_ioinfo(ioinfo
*info
)
351 if(info
!=&MSVCRT___badioinfo
&& info
->exflag
& EF_CRIT_INIT
)
352 LeaveCriticalSection(&info
->crit
);
355 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
359 if(i
>= MSVCRT_max_streams
)
363 return &MSVCRT__iob
[i
];
365 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
367 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
368 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
369 ERR("out of memory\n");
370 *MSVCRT__errno() = MSVCRT_ENOMEM
;
374 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
376 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
381 /* INTERNAL: free a file entry fd */
382 static void msvcrt_free_fd(int fd
)
384 ioinfo
*fdinfo
= get_ioinfo(fd
);
386 if(fdinfo
!= &MSVCRT___badioinfo
)
388 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
391 TRACE(":fd (%d) freed\n",fd
);
398 SetStdHandle(STD_INPUT_HANDLE
, 0);
401 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
404 SetStdHandle(STD_ERROR_HANDLE
, 0);
408 release_ioinfo(fdinfo
);
411 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
413 fdinfo
->handle
= hand
;
414 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
415 fdinfo
->lookahead
[0] = '\n';
416 fdinfo
->lookahead
[1] = '\n';
417 fdinfo
->lookahead
[2] = '\n';
418 fdinfo
->exflag
&= EF_CRIT_INIT
;
420 switch (fdinfo
-MSVCRT___pioinfo
[0])
422 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
423 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
424 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
428 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
429 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
432 ioinfo
*info
= get_ioinfo_alloc(&fd
);
434 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
436 if(info
== &MSVCRT___badioinfo
)
439 msvcrt_set_fd(info
, hand
, flag
);
440 release_ioinfo(info
);
444 /* INTERNAL: Allocate a FILE* for an fd slot */
445 /* caller must hold the files lock */
446 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
451 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
453 file
= msvcrt_get_file(i
);
457 if (file
->_flag
== 0)
459 if (i
== MSVCRT_stream_idx
)
461 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
463 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
464 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
475 /* INTERNAL: initialize a FILE* from an open fd */
476 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
478 TRACE(":fd (%d) allocating FILE*\n",fd
);
479 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
481 WARN(":invalid fd %d\n",fd
);
482 *MSVCRT___doserrno() = 0;
483 *MSVCRT__errno() = MSVCRT_EBADF
;
486 file
->_ptr
= file
->_base
= NULL
;
489 file
->_flag
= stream_flags
;
490 file
->_tmpfname
= NULL
;
492 TRACE(":got FILE* (%p)\n",file
);
496 /* INTERNAL: Create an inheritance data block (for spawned process)
497 * The inheritance block is made of:
498 * 00 int nb of file descriptor (NBFD)
499 * 04 char file flags (wxflag): repeated for each fd
500 * 4+NBFD HANDLE file handle: repeated for each fd
502 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
509 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
510 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
514 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
515 *block
= MSVCRT_calloc(1, *size
);
521 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
522 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
524 *(unsigned*)*block
= last_fd
;
525 for (fd
= 0; fd
< last_fd
; fd
++)
527 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
528 fdinfo
= get_ioinfo(fd
);
529 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
531 *wxflag_ptr
= fdinfo
->wxflag
;
532 *handle_ptr
= fdinfo
->handle
;
537 *handle_ptr
= INVALID_HANDLE_VALUE
;
539 release_ioinfo(fdinfo
);
540 wxflag_ptr
++; handle_ptr
++;
545 /* INTERNAL: Set up all file descriptors,
546 * as well as default streams (stdin, stderr and stdout)
548 void msvcrt_init_io(void)
554 GetStartupInfoA(&si
);
555 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
561 count
= *(unsigned*)si
.lpReserved2
;
562 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
563 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
565 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
566 count
= min(count
, MSVCRT_MAX_FILES
);
567 for (i
= 0; i
< count
; i
++)
569 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
571 fdinfo
= get_ioinfo_alloc_fd(i
);
572 if(fdinfo
!= &MSVCRT___badioinfo
)
573 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
574 release_ioinfo(fdinfo
);
577 wxflag_ptr
++; handle_ptr
++;
581 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDIN_FILENO
);
582 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
583 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
584 DWORD flags
= WX_OPEN
| WX_TEXT
;
585 DWORD type
= GetFileType(h
);
587 if (type
== FILE_TYPE_UNKNOWN
) {
588 h
= MSVCRT_NO_CONSOLE
;
590 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
592 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
596 msvcrt_set_fd(fdinfo
, h
, flags
);
598 release_ioinfo(fdinfo
);
600 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO
);
601 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
602 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
603 DWORD flags
= WX_OPEN
| WX_TEXT
;
604 DWORD type
= GetFileType(h
);
606 if (type
== FILE_TYPE_UNKNOWN
) {
607 h
= MSVCRT_NO_CONSOLE
;
609 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
611 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
615 msvcrt_set_fd(fdinfo
, h
, flags
);
617 release_ioinfo(fdinfo
);
619 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO
);
620 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
621 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
622 DWORD flags
= WX_OPEN
| WX_TEXT
;
623 DWORD type
= GetFileType(h
);
625 if (type
== FILE_TYPE_UNKNOWN
) {
626 h
= MSVCRT_NO_CONSOLE
;
628 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
630 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
634 msvcrt_set_fd(fdinfo
, h
, flags
);
636 release_ioinfo(fdinfo
);
638 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
639 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
640 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
642 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
643 for (i
= 0; i
< 3; i
++)
645 /* FILE structs for stdin/out/err are static and never deleted */
646 MSVCRT__iob
[i
]._file
= get_ioinfo_nolock(i
)->handle
== MSVCRT_NO_CONSOLE
?
647 MSVCRT_NO_CONSOLE_FD
: i
;
648 MSVCRT__iob
[i
]._tmpfname
= NULL
;
649 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
651 MSVCRT_stream_idx
= 3;
654 /* INTERNAL: Flush stdio file buffer */
655 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
657 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
658 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
659 int cnt
=file
->_ptr
-file
->_base
;
660 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
661 file
->_flag
|= MSVCRT__IOERR
;
665 if(file
->_flag
& MSVCRT__IORW
)
666 file
->_flag
&= ~MSVCRT__IOWRT
;
669 file
->_ptr
=file
->_base
;
674 /*********************************************************************
677 int CDECL
MSVCRT__isatty(int fd
)
679 TRACE(":fd (%d)\n",fd
);
681 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
684 /* INTERNAL: Allocate stdio file buffer */
685 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
687 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
688 && MSVCRT__isatty(file
->_file
))
691 file
->_base
= MSVCRT_calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
693 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
694 file
->_flag
|= MSVCRT__IOMYBUF
;
696 file
->_base
= (char*)(&file
->_charbuf
);
698 file
->_flag
|= MSVCRT__IONBF
;
700 file
->_ptr
= file
->_base
;
705 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
706 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
708 static char buffers
[2][MSVCRT_BUFSIZ
];
710 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
711 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
712 || !MSVCRT__isatty(file
->_file
))
715 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
716 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
717 file
->_flag
|= MSVCRT__USERBUF
;
721 /* INTERNAL: Removes temporary buffer from stdout or stderr */
722 /* Only call this function when add_std_buffer returned TRUE */
723 static void remove_std_buffer(MSVCRT_FILE
*file
)
725 msvcrt_flush_buffer(file
);
726 file
->_ptr
= file
->_base
= NULL
;
727 file
->_bufsiz
= file
->_cnt
= 0;
728 file
->_flag
&= ~MSVCRT__USERBUF
;
731 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
732 static int msvcrt_int_to_base32(int num
, char *str
)
747 *p
= (num
& 31) + '0';
749 *p
+= ('a' - '0' - 10);
756 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
757 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
772 *p
= (num
& 31) + '0';
774 *p
+= ('a' - '0' - 10);
781 /*********************************************************************
782 * __iob_func(MSVCRT.@)
784 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
786 return &MSVCRT__iob
[0];
789 #if _MSVCR_VER >= 140
790 /*********************************************************************
791 * __acrt_iob_func(UCRTBASE.@)
793 MSVCRT_FILE
* CDECL
MSVCRT___acrt_iob_func(unsigned idx
)
795 return &MSVCRT__iob
[idx
];
799 /*********************************************************************
802 int CDECL
MSVCRT__access(const char *filename
, int mode
)
804 DWORD attr
= GetFileAttributesA(filename
);
806 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
808 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
810 msvcrt_set_errno(GetLastError());
813 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
815 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
821 /*********************************************************************
822 * _access_s (MSVCRT.@)
824 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
826 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
827 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
829 if (MSVCRT__access(filename
, mode
) == -1)
830 return *MSVCRT__errno();
834 /*********************************************************************
835 * _waccess (MSVCRT.@)
837 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
839 DWORD attr
= GetFileAttributesW(filename
);
841 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
843 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
845 msvcrt_set_errno(GetLastError());
848 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
850 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
856 /*********************************************************************
857 * _waccess_s (MSVCRT.@)
859 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
861 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
862 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
864 if (MSVCRT__waccess(filename
, mode
) == -1)
865 return *MSVCRT__errno();
869 /*********************************************************************
872 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
874 DWORD oldFlags
= GetFileAttributesA(path
);
876 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
878 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
879 oldFlags
| FILE_ATTRIBUTE_READONLY
;
881 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
884 msvcrt_set_errno(GetLastError());
888 /*********************************************************************
891 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
893 DWORD oldFlags
= GetFileAttributesW(path
);
895 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
897 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
898 oldFlags
| FILE_ATTRIBUTE_READONLY
;
900 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
903 msvcrt_set_errno(GetLastError());
907 /*********************************************************************
910 int CDECL
MSVCRT__unlink(const char *path
)
912 TRACE("%s\n",debugstr_a(path
));
913 if(DeleteFileA(path
))
915 TRACE("failed (%d)\n",GetLastError());
916 msvcrt_set_errno(GetLastError());
920 /*********************************************************************
921 * _wunlink (MSVCRT.@)
923 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
925 TRACE("(%s)\n",debugstr_w(path
));
926 if(DeleteFileW(path
))
928 TRACE("failed (%d)\n",GetLastError());
929 msvcrt_set_errno(GetLastError());
933 /*********************************************************************
936 int CDECL
MSVCRT__commit(int fd
)
938 ioinfo
*info
= get_ioinfo(fd
);
941 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
943 if (info
->handle
== INVALID_HANDLE_VALUE
)
945 else if (!FlushFileBuffers(info
->handle
))
947 if (GetLastError() == ERROR_INVALID_HANDLE
)
949 /* FlushFileBuffers fails for console handles
950 * so we ignore this error.
956 TRACE(":failed-last error (%d)\n",GetLastError());
957 msvcrt_set_errno(GetLastError());
967 release_ioinfo(info
);
971 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
972 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
974 /* INTERNAL: Flush all stream buffer */
975 static int msvcrt_flush_all_buffers(int mask
)
977 int i
, num_flushed
= 0;
981 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
982 file
= msvcrt_get_file(i
);
986 if(file
->_flag
& mask
) {
994 TRACE(":flushed (%d) handles\n",num_flushed
);
998 /*********************************************************************
999 * _flushall (MSVCRT.@)
1001 int CDECL
MSVCRT__flushall(void)
1003 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
1006 /*********************************************************************
1009 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
1014 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1017 MSVCRT__lock_file(file
);
1018 ret
= MSVCRT__fflush_nolock(file
);
1019 MSVCRT__unlock_file(file
);
1025 /*********************************************************************
1026 * _fflush_nolock (MSVCRT.@)
1028 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
1033 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1037 res
= msvcrt_flush_buffer(file
);
1038 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1039 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
1043 /*********************************************************************
1046 int CDECL
MSVCRT__close(int fd
)
1048 ioinfo
*info
= get_ioinfo(fd
);
1051 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1053 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1054 *MSVCRT__errno() = MSVCRT_EBADF
;
1056 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, MSVCRT_EBADF
)) {
1058 } else if (fd
== MSVCRT_STDOUT_FILENO
&&
1059 info
->handle
== get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
) {
1062 } else if (fd
== MSVCRT_STDERR_FILENO
&&
1063 info
->handle
== get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
) {
1067 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1070 WARN(":failed-last error (%d)\n",GetLastError());
1071 msvcrt_set_errno(GetLastError());
1074 release_ioinfo(info
);
1078 /*********************************************************************
1081 * MSDN isn't clear on this point, but the remarks for _pipe
1082 * indicate file descriptors duplicated with _dup and _dup2 are always
1085 int CDECL
MSVCRT__dup2(int od
, int nd
)
1087 ioinfo
*info_od
, *info_nd
;
1090 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1094 info_od
= get_ioinfo(od
);
1095 info_nd
= get_ioinfo_alloc_fd(nd
);
1099 info_nd
= get_ioinfo_alloc_fd(nd
);
1100 info_od
= get_ioinfo(od
);
1103 if (info_nd
== &MSVCRT___badioinfo
)
1107 else if (info_od
->wxflag
& WX_OPEN
)
1111 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1112 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1114 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1116 if (info_nd
->wxflag
& WX_OPEN
)
1119 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1120 /* _dup2 returns 0, not nd, on success */
1126 msvcrt_set_errno(GetLastError());
1131 *MSVCRT__errno() = MSVCRT_EBADF
;
1135 release_ioinfo(info_od
);
1136 release_ioinfo(info_nd
);
1140 /*********************************************************************
1143 int CDECL
MSVCRT__dup(int od
)
1146 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1148 if (MSVCRT__dup2(od
, fd
) == 0)
1152 release_ioinfo(info
);
1156 /*********************************************************************
1159 int CDECL
MSVCRT__eof(int fd
)
1161 ioinfo
*info
= get_ioinfo(fd
);
1162 DWORD curpos
,endpos
;
1163 LONG hcurpos
,hendpos
;
1165 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1167 if (info
->handle
== INVALID_HANDLE_VALUE
)
1169 release_ioinfo(info
);
1173 if (info
->wxflag
& WX_ATEOF
)
1175 release_ioinfo(info
);
1179 /* Otherwise we do it the hard way */
1180 hcurpos
= hendpos
= 0;
1181 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1182 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1184 if (curpos
== endpos
&& hcurpos
== hendpos
)
1186 /* FIXME: shouldn't WX_ATEOF be set here? */
1187 release_ioinfo(info
);
1191 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1192 release_ioinfo(info
);
1196 /*********************************************************************
1197 * _fcloseall (MSVCRT.@)
1199 int CDECL
MSVCRT__fcloseall(void)
1201 int num_closed
= 0, i
;
1205 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1206 file
= msvcrt_get_file(i
);
1208 if (file
->_flag
&& !MSVCRT_fclose(file
))
1213 TRACE(":closed (%d) handles\n",num_closed
);
1217 /* free everything on process exit */
1218 void msvcrt_free_io(void)
1224 MSVCRT__fcloseall();
1226 for(i
=0; i
<ARRAY_SIZE(MSVCRT___pioinfo
); i
++)
1228 if(!MSVCRT___pioinfo
[i
])
1231 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1233 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1234 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1236 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1239 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1241 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1242 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1244 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1245 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1249 for(i
=0; i
<ARRAY_SIZE(MSVCRT_fstream
); i
++)
1250 MSVCRT_free(MSVCRT_fstream
[i
]);
1253 /*********************************************************************
1254 * _lseeki64 (MSVCRT.@)
1256 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1258 ioinfo
*info
= get_ioinfo(fd
);
1261 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1263 if (info
->handle
== INVALID_HANDLE_VALUE
)
1265 release_ioinfo(info
);
1269 if (whence
< 0 || whence
> 2)
1271 release_ioinfo(info
);
1272 *MSVCRT__errno() = MSVCRT_EINVAL
;
1276 TRACE(":fd (%d) to %s pos %s\n",
1277 fd
,wine_dbgstr_longlong(offset
),
1278 (whence
==MSVCRT_SEEK_SET
)?"SEEK_SET":
1279 (whence
==MSVCRT_SEEK_CUR
)?"SEEK_CUR":
1280 (whence
==MSVCRT_SEEK_END
)?"SEEK_END":"UNKNOWN");
1282 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1283 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1284 ofs
.QuadPart
= offset
;
1285 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1286 GetLastError() == ERROR_SUCCESS
)
1288 info
->wxflag
&= ~WX_ATEOF
;
1289 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1291 release_ioinfo(info
);
1292 return ofs
.QuadPart
;
1294 release_ioinfo(info
);
1295 TRACE(":error-last error (%d)\n",GetLastError());
1296 msvcrt_set_errno(GetLastError());
1300 /*********************************************************************
1303 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1305 return MSVCRT__lseeki64(fd
, offset
, whence
);
1308 /*********************************************************************
1309 * _lock_file (MSVCRT.@)
1311 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1313 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1314 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1316 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1319 /*********************************************************************
1320 * _unlock_file (MSVCRT.@)
1322 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1324 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1325 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1327 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1330 /*********************************************************************
1331 * _locking (MSVCRT.@)
1333 * This is untested; the underlying LockFile doesn't work yet.
1335 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1337 ioinfo
*info
= get_ioinfo(fd
);
1341 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1342 if (info
->handle
== INVALID_HANDLE_VALUE
)
1344 release_ioinfo(info
);
1348 if (mode
< 0 || mode
> 4)
1350 release_ioinfo(info
);
1351 *MSVCRT__errno() = MSVCRT_EINVAL
;
1355 TRACE(":fd (%d) by 0x%08x mode %s\n",
1356 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1357 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1358 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1359 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1360 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1363 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1365 release_ioinfo(info
);
1366 FIXME ("Seek failed\n");
1367 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1370 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1373 ret
= 1; /* just to satisfy gcc */
1376 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1381 else if (mode
== MSVCRT__LK_UNLCK
)
1382 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1384 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1385 /* FIXME - what about error settings? */
1386 release_ioinfo(info
);
1387 return ret
? 0 : -1;
1390 /*********************************************************************
1391 * _fseeki64 (MSVCRT.@)
1393 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1397 MSVCRT__lock_file(file
);
1398 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1399 MSVCRT__unlock_file(file
);
1404 /*********************************************************************
1405 * _fseeki64_nolock (MSVCRT.@)
1407 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1411 if(whence
== MSVCRT_SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1412 whence
= MSVCRT_SEEK_SET
;
1413 offset
+= MSVCRT__ftelli64_nolock(file
);
1416 /* Flush output if needed */
1417 msvcrt_flush_buffer(file
);
1418 /* Reset direction of i/o */
1419 if(file
->_flag
& MSVCRT__IORW
) {
1420 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1422 /* Clear end of file flag */
1423 file
->_flag
&= ~MSVCRT__IOEOF
;
1424 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1429 /*********************************************************************
1432 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1434 return MSVCRT__fseeki64( file
, offset
, whence
);
1437 /*********************************************************************
1438 * _fseek_nolock (MSVCRT.@)
1440 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1442 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1445 /*********************************************************************
1446 * _chsize_s (MSVCRT.@)
1448 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1454 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1456 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1459 info
= get_ioinfo(fd
);
1460 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1462 /* save the current file pointer */
1463 cur
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1466 pos
= MSVCRT__lseeki64(fd
, size
, MSVCRT_SEEK_SET
);
1469 ret
= SetEndOfFile(info
->handle
);
1470 if (!ret
) msvcrt_set_errno(GetLastError());
1473 /* restore the file pointer */
1474 MSVCRT__lseeki64(fd
, cur
, MSVCRT_SEEK_SET
);
1478 release_ioinfo(info
);
1479 return ret
? 0 : *MSVCRT__errno();
1482 /*********************************************************************
1483 * _chsize (MSVCRT.@)
1485 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1487 /* _chsize_s returns errno on failure but _chsize should return -1 */
1488 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1491 /*********************************************************************
1492 * clearerr (MSVCRT.@)
1494 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1496 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1498 MSVCRT__lock_file(file
);
1499 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1500 MSVCRT__unlock_file(file
);
1503 /*********************************************************************
1504 * clearerr_s (MSVCRT.@)
1506 int CDECL
MSVCRT_clearerr_s(MSVCRT_FILE
* file
)
1508 TRACE(":file (%p)\n",file
);
1510 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
1512 MSVCRT__lock_file(file
);
1513 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1514 MSVCRT__unlock_file(file
);
1518 /*********************************************************************
1521 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1523 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1525 MSVCRT__lock_file(file
);
1526 MSVCRT__fseek_nolock(file
, 0L, MSVCRT_SEEK_SET
);
1527 MSVCRT_clearerr(file
);
1528 MSVCRT__unlock_file(file
);
1531 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1533 int plus
= strchrW(mode
, '+') != NULL
;
1535 TRACE("%s\n", debugstr_w(mode
));
1537 while(*mode
== ' ') mode
++;
1542 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1543 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1546 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1547 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1550 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1551 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1554 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1558 *stream_flags
|= MSVCRT__commode
;
1560 while (*mode
&& *mode
!=',')
1564 *open_flags
|= MSVCRT__O_BINARY
;
1565 *open_flags
&= ~MSVCRT__O_TEXT
;
1568 *open_flags
|= MSVCRT__O_TEXT
;
1569 *open_flags
&= ~MSVCRT__O_BINARY
;
1572 *open_flags
|= MSVCRT__O_TEMPORARY
;
1575 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1578 *stream_flags
|= MSVCRT__IOCOMMIT
;
1581 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1584 *open_flags
|= MSVCRT__O_NOINHERIT
;
1593 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1596 ERR("incorrect mode flag: %c\n", mode
[-1]);
1602 static const WCHAR ccs
[] = {'c','c','s'};
1603 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1604 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1605 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1608 while(*mode
== ' ') mode
++;
1609 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, ARRAY_SIZE(ccs
))))
1611 mode
+= ARRAY_SIZE(ccs
);
1612 while(*mode
== ' ') mode
++;
1613 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1616 while(*mode
== ' ') mode
++;
1618 if(!strncmpiW(utf8
, mode
, ARRAY_SIZE(utf8
)))
1620 *open_flags
|= MSVCRT__O_U8TEXT
;
1621 mode
+= ARRAY_SIZE(utf8
);
1623 else if(!strncmpiW(utf16le
, mode
, ARRAY_SIZE(utf16le
)))
1625 *open_flags
|= MSVCRT__O_U16TEXT
;
1626 mode
+= ARRAY_SIZE(utf16le
);
1628 else if(!strncmpiW(unicode
, mode
, ARRAY_SIZE(unicode
)))
1630 *open_flags
|= MSVCRT__O_WTEXT
;
1631 mode
+= ARRAY_SIZE(unicode
);
1635 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1639 while(*mode
== ' ') mode
++;
1642 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1647 /*********************************************************************
1648 * _fdopen (MSVCRT.@)
1650 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1653 MSVCRT_wchar_t
*modeW
= NULL
;
1655 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1657 ret
= MSVCRT__wfdopen(fd
, modeW
);
1663 /*********************************************************************
1664 * _wfdopen (MSVCRT.@)
1666 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1668 int open_flags
, stream_flags
;
1671 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1674 if (!(file
= msvcrt_alloc_fp()))
1676 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1681 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1687 /*********************************************************************
1688 * _filelength (MSVCRT.@)
1690 LONG CDECL
MSVCRT__filelength(int fd
)
1692 LONG curPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
1695 LONG endPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_END
);
1698 if (endPos
!= curPos
)
1699 MSVCRT__lseek(fd
, curPos
, MSVCRT_SEEK_SET
);
1706 /*********************************************************************
1707 * _filelengthi64 (MSVCRT.@)
1709 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1711 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1714 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_END
);
1717 if (endPos
!= curPos
)
1718 MSVCRT__lseeki64(fd
, curPos
, MSVCRT_SEEK_SET
);
1725 /*********************************************************************
1726 * _fileno (MSVCRT.@)
1728 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1730 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1734 /*********************************************************************
1735 * _fstat64 (MSVCRT.@)
1737 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1739 ioinfo
*info
= get_ioinfo(fd
);
1742 BY_HANDLE_FILE_INFORMATION hfi
;
1744 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1745 if (info
->handle
== INVALID_HANDLE_VALUE
)
1747 release_ioinfo(info
);
1753 WARN(":failed-NULL buf\n");
1754 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1755 release_ioinfo(info
);
1759 memset(&hfi
, 0, sizeof(hfi
));
1760 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1761 type
= GetFileType(info
->handle
);
1762 if (type
== FILE_TYPE_PIPE
)
1764 buf
->st_dev
= buf
->st_rdev
= fd
;
1765 buf
->st_mode
= MSVCRT__S_IFIFO
;
1768 else if (type
== FILE_TYPE_CHAR
)
1770 buf
->st_dev
= buf
->st_rdev
= fd
;
1771 buf
->st_mode
= MSVCRT__S_IFCHR
;
1774 else /* FILE_TYPE_DISK etc. */
1776 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1778 WARN(":failed-last error (%d)\n",GetLastError());
1779 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1780 release_ioinfo(info
);
1783 buf
->st_mode
= MSVCRT__S_IFREG
| 0444;
1784 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1785 buf
->st_mode
|= 0222;
1786 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1787 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1789 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1790 buf
->st_mtime
= buf
->st_ctime
= dw
;
1791 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1793 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1795 release_ioinfo(info
);
1799 /*********************************************************************
1800 * _fstati64 (MSVCRT.@)
1802 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1805 struct MSVCRT__stat64 buf64
;
1807 ret
= MSVCRT__fstat64(fd
, &buf64
);
1809 msvcrt_stat64_to_stati64(&buf64
, buf
);
1813 /*********************************************************************
1816 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1818 struct MSVCRT__stat64 buf64
;
1820 ret
= MSVCRT__fstat64(fd
, &buf64
);
1822 msvcrt_stat64_to_stat(&buf64
, buf
);
1826 /*********************************************************************
1827 * _fstat32 (MSVCR80.@)
1829 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1832 struct MSVCRT__stat64 buf64
;
1834 ret
= MSVCRT__fstat64(fd
, &buf64
);
1836 msvcrt_stat64_to_stat32(&buf64
, buf
);
1840 /*********************************************************************
1841 * _fstat32i64 (MSVCR80.@)
1843 int CDECL
MSVCRT__fstat32i64(int fd
, struct MSVCRT__stat32i64
* buf
)
1846 struct MSVCRT__stat64 buf64
;
1848 ret
= MSVCRT__fstat64(fd
, &buf64
);
1850 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1854 /*********************************************************************
1855 * _fstat64i32 (MSVCR80.@)
1857 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1860 struct MSVCRT__stat64 buf64
;
1862 ret
= MSVCRT__fstat64(fd
, &buf64
);
1864 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1868 /*********************************************************************
1869 * _futime64 (MSVCRT.@)
1871 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1873 ioinfo
*info
= get_ioinfo(fd
);
1878 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1883 time_to_filetime( t
->actime
, &at
);
1884 time_to_filetime( t
->modtime
, &wt
);
1887 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1889 release_ioinfo(info
);
1890 msvcrt_set_errno(GetLastError());
1893 release_ioinfo(info
);
1897 /*********************************************************************
1898 * _futime32 (MSVCRT.@)
1900 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1904 struct MSVCRT___utimbuf64 t64
;
1905 t64
.actime
= t
->actime
;
1906 t64
.modtime
= t
->modtime
;
1907 return _futime64( fd
, &t64
);
1910 return _futime64( fd
, NULL
);
1913 /*********************************************************************
1914 * _futime (MSVCRT.@)
1917 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1919 return _futime64( fd
, t
);
1922 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1924 return _futime32( fd
, t
);
1928 /*********************************************************************
1929 * _get_osfhandle (MSVCRT.@)
1931 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1933 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1934 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1936 if(hand
== INVALID_HANDLE_VALUE
)
1937 *MSVCRT__errno() = MSVCRT_EBADF
;
1938 return (MSVCRT_intptr_t
)hand
;
1941 /*********************************************************************
1942 * _mktemp_s (MSVCRT.@)
1944 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1948 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1949 return MSVCRT_EINVAL
;
1951 for(len
=0; len
<size
; len
++)
1954 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1957 return MSVCRT_EINVAL
;
1960 for(xno
=1; xno
<=6; xno
++)
1961 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1962 return MSVCRT_EINVAL
;
1964 id
= GetCurrentProcessId();
1965 for(xno
=1; xno
<6; xno
++) {
1966 pattern
[len
-xno
] = id
%10 + '0';
1970 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1971 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1976 *MSVCRT__errno() = MSVCRT_EEXIST
;
1977 return MSVCRT_EEXIST
;
1980 /*********************************************************************
1981 * _mktemp (MSVCRT.@)
1983 char * CDECL
MSVCRT__mktemp(char *pattern
)
1986 char *retVal
= pattern
;
1994 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1998 id
= GetCurrentProcessId();
2002 int tempNum
= id
/ 10;
2003 *pattern
-- = id
- (tempNum
* 10) + '0';
2009 *pattern
= letter
++;
2010 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2012 } while(letter
<= 'z');
2016 /*********************************************************************
2017 * _wmktemp_s (MSVCRT.@)
2019 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
2023 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2024 return MSVCRT_EINVAL
;
2026 for(len
=0; len
<size
; len
++)
2029 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2032 return MSVCRT_EINVAL
;
2035 for(xno
=1; xno
<=6; xno
++)
2036 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2037 return MSVCRT_EINVAL
;
2039 id
= GetCurrentProcessId();
2040 for(xno
=1; xno
<6; xno
++) {
2041 pattern
[len
-xno
] = id
%10 + '0';
2045 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2046 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2051 *MSVCRT__errno() = MSVCRT_EEXIST
;
2052 return MSVCRT_EEXIST
;
2055 /*********************************************************************
2056 * _wmktemp (MSVCRT.@)
2058 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
2061 MSVCRT_wchar_t
*retVal
= pattern
;
2063 MSVCRT_wchar_t letter
= 'a';
2069 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2073 id
= GetCurrentProcessId();
2077 int tempNum
= id
/ 10;
2078 *pattern
-- = id
- (tempNum
* 10) + '0';
2084 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2086 *pattern
= letter
++;
2087 } while(letter
!= '|');
2091 static unsigned split_oflags(unsigned oflags
)
2094 unsigned unsupp
; /* until we support everything */
2096 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2097 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2098 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2099 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2100 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2101 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2102 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2103 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2104 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2106 if ((unsupp
= oflags
& ~(
2107 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2108 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2109 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2110 MSVCRT__O_NOINHERIT
|
2111 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2112 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2114 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2119 /*********************************************************************
2122 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2125 SECURITY_ATTRIBUTES sa
;
2126 HANDLE readHandle
, writeHandle
;
2130 *MSVCRT__errno() = MSVCRT_EINVAL
;
2134 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2135 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2136 sa
.lpSecurityDescriptor
= NULL
;
2137 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2139 unsigned int wxflags
= split_oflags(textmode
);
2142 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2146 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2154 MSVCRT__close(pfds
[0]);
2155 CloseHandle(writeHandle
);
2156 *MSVCRT__errno() = MSVCRT_EMFILE
;
2161 CloseHandle(readHandle
);
2162 CloseHandle(writeHandle
);
2163 *MSVCRT__errno() = MSVCRT_EMFILE
;
2167 msvcrt_set_errno(GetLastError());
2172 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2174 char bom
[sizeof(utf8_bom
)];
2177 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2179 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2182 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2183 oflags
|= MSVCRT__O_U8TEXT
;
2184 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2186 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2187 oflags
|= MSVCRT__O_U16TEXT
;
2189 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2195 /*********************************************************************
2196 * _wsopen_dispatch (UCRTBASE.@)
2198 int CDECL
MSVCRT__wsopen_dispatch( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
,
2199 int *fd
, int secure
)
2201 DWORD access
= 0, creation
= 0, attrib
;
2202 SECURITY_ATTRIBUTES sa
;
2203 DWORD sharing
, type
;
2207 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2208 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2210 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2213 wxflag
= split_oflags(oflags
);
2214 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2216 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2217 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2218 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2221 if (oflags
& MSVCRT__O_CREAT
)
2223 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))))
2224 return MSVCRT_EINVAL
;
2226 if (oflags
& MSVCRT__O_EXCL
)
2227 creation
= CREATE_NEW
;
2228 else if (oflags
& MSVCRT__O_TRUNC
)
2229 creation
= CREATE_ALWAYS
;
2231 creation
= OPEN_ALWAYS
;
2233 else /* no MSVCRT__O_CREAT */
2235 if (oflags
& MSVCRT__O_TRUNC
)
2236 creation
= TRUNCATE_EXISTING
;
2238 creation
= OPEN_EXISTING
;
2243 case MSVCRT__SH_DENYRW
:
2246 case MSVCRT__SH_DENYWR
:
2247 sharing
= FILE_SHARE_READ
;
2249 case MSVCRT__SH_DENYRD
:
2250 sharing
= FILE_SHARE_WRITE
;
2252 case MSVCRT__SH_DENYNO
:
2253 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2256 ERR( "Unhandled shflags 0x%x\n", shflags
);
2257 return MSVCRT_EINVAL
;
2260 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2261 attrib
= FILE_ATTRIBUTE_READONLY
;
2263 attrib
= FILE_ATTRIBUTE_NORMAL
;
2265 if (oflags
& MSVCRT__O_TEMPORARY
)
2267 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2269 sharing
|= FILE_SHARE_DELETE
;
2272 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2273 sa
.lpSecurityDescriptor
= NULL
;
2274 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2276 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2277 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2278 && !(access
&GENERIC_READ
))
2280 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2281 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2282 if (hand
!= INVALID_HANDLE_VALUE
)
2284 oflags
= check_bom(hand
, oflags
, FALSE
);
2288 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2291 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2292 if (hand
== INVALID_HANDLE_VALUE
) {
2293 WARN(":failed-last error (%d)\n",GetLastError());
2294 msvcrt_set_errno(GetLastError());
2295 return *MSVCRT__errno();
2298 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2300 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2301 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2302 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2304 if (oflags
& MSVCRT__O_U8TEXT
)
2306 DWORD written
= 0, tmp
;
2308 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2309 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2311 if (written
!= sizeof(utf8_bom
)) {
2312 WARN("error writing BOM\n");
2314 msvcrt_set_errno(GetLastError());
2315 return *MSVCRT__errno();
2320 DWORD written
= 0, tmp
;
2322 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2323 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2325 if (written
!= sizeof(utf16_bom
))
2327 WARN("error writing BOM\n");
2329 msvcrt_set_errno(GetLastError());
2330 return *MSVCRT__errno();
2334 else if (access
& GENERIC_READ
)
2335 oflags
= check_bom(hand
, oflags
, TRUE
);
2338 type
= GetFileType(hand
);
2339 if (type
== FILE_TYPE_CHAR
)
2341 else if (type
== FILE_TYPE_PIPE
)
2344 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2346 return *MSVCRT__errno();
2348 if (oflags
& MSVCRT__O_WTEXT
)
2349 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2350 else if (oflags
& MSVCRT__O_U16TEXT
)
2351 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2352 else if (oflags
& MSVCRT__O_U8TEXT
)
2353 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2355 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2360 /*********************************************************************
2361 * _wsopen_s (MSVCRT.@)
2363 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2365 return MSVCRT__wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2368 /*********************************************************************
2369 * _wsopen (MSVCRT.@)
2371 int WINAPIV
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2376 if (oflags
& MSVCRT__O_CREAT
)
2380 __ms_va_start(ap
, shflags
);
2381 pmode
= va_arg(ap
, int);
2387 return MSVCRT__wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2391 /*********************************************************************
2392 * _sopen_dispatch (UCRTBASE.@)
2394 int CDECL
MSVCRT__sopen_dispatch( const char *path
, int oflags
, int shflags
,
2395 int pmode
, int *fd
, int secure
)
2397 MSVCRT_wchar_t
*pathW
;
2400 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2401 return MSVCRT_EINVAL
;
2403 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2404 return MSVCRT_EINVAL
;
2406 ret
= MSVCRT__wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2411 /*********************************************************************
2412 * _sopen_s (MSVCRT.@)
2414 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2416 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2419 /*********************************************************************
2422 int WINAPIV
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2427 if (oflags
& MSVCRT__O_CREAT
)
2431 __ms_va_start(ap
, shflags
);
2432 pmode
= va_arg(ap
, int);
2438 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2441 /*********************************************************************
2444 int WINAPIV
MSVCRT__open( const char *path
, int flags
, ... )
2448 if (flags
& MSVCRT__O_CREAT
)
2451 __ms_va_start(ap
, flags
);
2452 pmode
= va_arg(ap
, int);
2454 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2457 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2460 /*********************************************************************
2463 int WINAPIV
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2467 if (flags
& MSVCRT__O_CREAT
)
2470 __ms_va_start(ap
, flags
);
2471 pmode
= va_arg(ap
, int);
2473 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2476 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2479 /*********************************************************************
2482 int CDECL
MSVCRT__creat(const char *path
, int pmode
)
2484 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2485 return MSVCRT__open(path
, flags
, pmode
);
2488 /*********************************************************************
2489 * _wcreat (MSVCRT.@)
2491 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int pmode
)
2493 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2494 return MSVCRT__wopen(path
, flags
, pmode
);
2497 /*********************************************************************
2498 * _open_osfhandle (MSVCRT.@)
2500 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2505 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2506 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2507 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2508 * text - it never sets MSVCRT__O_BINARY.
2510 /* don't let split_oflags() decide the mode if no mode is passed */
2511 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2512 oflags
|= MSVCRT__O_BINARY
;
2514 flags
= GetFileType((HANDLE
)handle
);
2515 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2517 msvcrt_set_errno(GetLastError());
2521 if (flags
== FILE_TYPE_CHAR
)
2523 else if (flags
== FILE_TYPE_PIPE
)
2527 flags
|= split_oflags(oflags
);
2529 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2530 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2534 /*********************************************************************
2537 int CDECL
MSVCRT__rmtmp(void)
2539 int num_removed
= 0, i
;
2543 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2544 file
= msvcrt_get_file(i
);
2546 if (file
->_tmpfname
)
2548 MSVCRT_fclose(file
);
2555 TRACE(":removed (%d) temp files\n",num_removed
);
2559 static inline int get_utf8_char_len(char ch
)
2561 if((ch
&0xf8) == 0xf0)
2563 else if((ch
&0xf0) == 0xe0)
2565 else if((ch
&0xe0) == 0xc0)
2570 /*********************************************************************
2571 * (internal) read_utf8
2573 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2575 HANDLE hand
= fdinfo
->handle
;
2576 char min_buf
[4], *readbuf
, lookahead
;
2577 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2579 /* make the buffer big enough to hold at least one character */
2580 /* read bytes have to fit to output and lookahead buffers */
2582 readbuf_size
= count
< 4 ? 4 : count
;
2583 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2588 if(fdinfo
->lookahead
[0] != '\n') {
2589 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2590 fdinfo
->lookahead
[0] = '\n';
2592 if(fdinfo
->lookahead
[1] != '\n') {
2593 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2594 fdinfo
->lookahead
[1] = '\n';
2596 if(fdinfo
->lookahead
[2] != '\n') {
2597 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2598 fdinfo
->lookahead
[2] = '\n';
2603 /* NOTE: this case is broken in native dll, reading
2604 * sometimes fails when small buffer is passed
2607 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2608 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2609 fdinfo
->wxflag
|= WX_ATEOF
;
2612 msvcrt_set_errno(GetLastError());
2615 }else if(!num_read
) {
2616 fdinfo
->wxflag
|= WX_ATEOF
;
2622 char_len
= get_utf8_char_len(readbuf
[0]);
2624 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2628 if(readbuf
[0] == '\n')
2629 fdinfo
->wxflag
|= WX_READNL
;
2631 fdinfo
->wxflag
&= ~WX_READNL
;
2633 if(readbuf
[0] == 0x1a) {
2634 fdinfo
->wxflag
|= WX_ATEOF
;
2638 if(readbuf
[0] == '\r') {
2639 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2641 else if(lookahead
== '\n')
2645 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2646 fdinfo
->lookahead
[0] = lookahead
;
2648 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2653 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2654 msvcrt_set_errno(GetLastError());
2661 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2664 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2665 fdinfo
->wxflag
|= WX_ATEOF
;
2666 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2669 msvcrt_set_errno(GetLastError());
2670 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2673 }else if(!pos
&& !num_read
) {
2674 fdinfo
->wxflag
|= WX_ATEOF
;
2675 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2680 if(readbuf
[0] == '\n')
2681 fdinfo
->wxflag
|= WX_READNL
;
2683 fdinfo
->wxflag
&= ~WX_READNL
;
2685 /* Find first byte of last character (may be incomplete) */
2686 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2687 if((readbuf
[i
]&0xc0) != 0x80)
2689 char_len
= get_utf8_char_len(readbuf
[i
]);
2690 if(char_len
+i
<= pos
)
2693 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2695 fdinfo
->lookahead
[0] = readbuf
[i
];
2697 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2699 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2701 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2705 for(i
=0, j
=0; i
<pos
; i
++) {
2706 if(readbuf
[i
] == 0x1a) {
2707 fdinfo
->wxflag
|= WX_ATEOF
;
2711 /* strip '\r' if followed by '\n' */
2712 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2713 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2714 readbuf
[j
++] = '\r';
2715 }else if(lookahead
== '\n' && j
==0) {
2716 readbuf
[j
++] = '\n';
2718 if(lookahead
!= '\n')
2719 readbuf
[j
++] = '\r';
2721 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2722 fdinfo
->lookahead
[0] = lookahead
;
2724 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2726 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2727 readbuf
[j
++] = readbuf
[i
];
2732 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2733 msvcrt_set_errno(GetLastError());
2734 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2738 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2742 /*********************************************************************
2745 * When reading \r as last character in text mode, read() positions
2746 * the file pointer on the \r character while getc() goes on to
2749 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2751 DWORD num_read
, utf16
;
2752 char *bufstart
= buf
;
2757 if (fdinfo
->wxflag
& WX_ATEOF
) {
2758 TRACE("already at EOF, returning 0\n");
2761 /* Don't trace small reads, it gets *very* annoying */
2763 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2764 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2766 *MSVCRT__errno() = MSVCRT_EBADF
;
2770 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2771 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2773 *MSVCRT__errno() = MSVCRT_EINVAL
;
2777 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2778 return read_utf8(fdinfo
, buf
, count
);
2780 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2782 if (fdinfo
->lookahead
[0] != '\n')
2784 bufstart
[0] = fdinfo
->lookahead
[0];
2785 fdinfo
->lookahead
[0] = '\n';
2789 bufstart
[1] = fdinfo
->lookahead
[1];
2790 fdinfo
->lookahead
[1] = '\n';
2793 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2794 num_read
+= 1+utf16
;
2799 if(utf16
&& (num_read
&1))
2801 /* msvcr90 uses uninitialized value from the buffer in this case */
2802 /* msvcrt ignores additional data */
2803 ERR("got odd number of bytes in UTF16 mode\n");
2807 if (count
!= 0 && num_read
== 0)
2809 fdinfo
->wxflag
|= WX_ATEOF
;
2810 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2812 else if (fdinfo
->wxflag
& WX_TEXT
)
2816 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2817 fdinfo
->wxflag
|= WX_READNL
;
2819 fdinfo
->wxflag
&= ~WX_READNL
;
2821 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2823 /* in text mode, a ctrl-z signals EOF */
2824 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2826 fdinfo
->wxflag
|= WX_ATEOF
;
2827 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2831 /* in text mode, strip \r if followed by \n */
2832 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2837 lookahead
[1] = '\n';
2838 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2840 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2842 bufstart
[j
++] = '\n';
2843 if(utf16
) bufstart
[j
++] = 0;
2847 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2849 bufstart
[j
++] = '\r';
2850 if(utf16
) bufstart
[j
++] = 0;
2853 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2855 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2857 bufstart
[j
++] = '\n';
2858 if (utf16
) bufstart
[j
++] = 0;
2862 fdinfo
->lookahead
[0] = lookahead
[0];
2863 fdinfo
->lookahead
[1] = lookahead
[1];
2867 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2872 bufstart
[j
++] = '\r';
2873 if(utf16
) bufstart
[j
++] = 0;
2876 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2877 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2879 bufstart
[j
++] = bufstart
[i
];
2880 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2888 if (GetLastError() == ERROR_BROKEN_PIPE
)
2890 TRACE(":end-of-pipe\n");
2891 fdinfo
->wxflag
|= WX_ATEOF
;
2896 TRACE(":failed-last error (%d)\n",GetLastError());
2897 msvcrt_set_errno(GetLastError());
2903 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2907 /*********************************************************************
2910 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2915 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
2916 *MSVCRT__errno() = MSVCRT_EBADF
;
2920 info
= get_ioinfo(fd
);
2921 num_read
= read_i(fd
, info
, buf
, count
);
2922 release_ioinfo(info
);
2926 /*********************************************************************
2927 * _setmode (MSVCRT.@)
2929 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2931 ioinfo
*info
= get_ioinfo(fd
);
2932 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2933 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2934 ret
= MSVCRT__O_WTEXT
;
2936 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2937 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2938 *MSVCRT__errno() = MSVCRT_EINVAL
;
2939 release_ioinfo(info
);
2943 if(info
== &MSVCRT___badioinfo
) {
2944 *MSVCRT__errno() = MSVCRT_EBADF
;
2948 if(mode
== MSVCRT__O_BINARY
) {
2949 info
->wxflag
&= ~WX_TEXT
;
2950 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2951 release_ioinfo(info
);
2955 info
->wxflag
|= WX_TEXT
;
2956 if(mode
== MSVCRT__O_TEXT
)
2957 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2958 else if(mode
== MSVCRT__O_U8TEXT
)
2959 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2961 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2963 release_ioinfo(info
);
2967 /*********************************************************************
2968 * _stat64 (MSVCRT.@)
2970 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2973 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2974 unsigned short mode
= ALL_S_IREAD
;
2977 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2979 plen
= strlen(path
);
2980 while (plen
&& path
[plen
-1]==' ')
2983 if (plen
==2 && path
[1]==':')
2985 *MSVCRT__errno() = MSVCRT_ENOENT
;
2990 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2992 *MSVCRT__errno() = MSVCRT_ENOENT
;
2997 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2999 TRACE("failed (%d)\n",GetLastError());
3000 *MSVCRT__errno() = MSVCRT_ENOENT
;
3004 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3006 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3007 Bon 011120: This FIXME seems incorrect
3008 Also a letter as first char isn't enough to be classified
3011 if (isalpha(*path
)&& (*(path
+1)==':'))
3012 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
3014 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3016 /* Dir, or regular file? */
3017 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3018 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3021 mode
|= MSVCRT__S_IFREG
;
3023 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3025 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
3026 (tolower(path
[plen
-3]) << 16);
3027 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3028 mode
|= ALL_S_IEXEC
;
3032 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3033 mode
|= ALL_S_IWRITE
;
3035 buf
->st_mode
= mode
;
3037 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3038 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3040 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3041 buf
->st_mtime
= buf
->st_ctime
= dw
;
3042 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3043 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3044 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3048 /*********************************************************************
3049 * _stati64 (MSVCRT.@)
3051 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
3054 struct MSVCRT__stat64 buf64
;
3056 ret
= MSVCRT_stat64(path
, &buf64
);
3058 msvcrt_stat64_to_stati64(&buf64
, buf
);
3062 /*********************************************************************
3065 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
3068 struct MSVCRT__stat64 buf64
;
3070 ret
= MSVCRT_stat64( path
, &buf64
);
3072 msvcrt_stat64_to_stat(&buf64
, buf
);
3076 #if _MSVCR_VER >= 80
3078 /*********************************************************************
3079 * _stat32 (MSVCR80.@)
3081 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
3084 struct MSVCRT__stat64 buf64
;
3086 ret
= MSVCRT_stat64(path
, &buf64
);
3088 msvcrt_stat64_to_stat32(&buf64
, buf
);
3092 /*********************************************************************
3093 * _stat32i64 (MSVCR80.@)
3095 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
3098 struct MSVCRT__stat64 buf64
;
3100 ret
= MSVCRT_stat64(path
, &buf64
);
3102 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3106 /*********************************************************************
3107 * _stat64i32 (MSVCR80.@)
3109 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
3112 struct MSVCRT__stat64 buf64
;
3114 ret
= MSVCRT_stat64(path
, &buf64
);
3116 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3120 #endif /* _MSVCR_VER >= 80 */
3122 /*********************************************************************
3123 * _wstat64 (MSVCRT.@)
3125 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
3128 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3129 unsigned short mode
= ALL_S_IREAD
;
3132 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3134 plen
= strlenW(path
);
3135 while (plen
&& path
[plen
-1]==' ')
3138 if (plen
==2 && path
[1]==':')
3140 *MSVCRT__errno() = MSVCRT_ENOENT
;
3145 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3147 *MSVCRT__errno() = MSVCRT_ENOENT
;
3152 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3154 TRACE("failed (%d)\n",GetLastError());
3155 *MSVCRT__errno() = MSVCRT_ENOENT
;
3159 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3161 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3162 if (MSVCRT_iswalpha(*path
))
3163 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3165 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3167 /* Dir, or regular file? */
3168 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3169 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3172 mode
|= MSVCRT__S_IFREG
;
3174 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3176 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3177 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3178 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3179 mode
|= ALL_S_IEXEC
;
3183 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3184 mode
|= ALL_S_IWRITE
;
3186 buf
->st_mode
= mode
;
3188 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3189 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3191 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3192 buf
->st_mtime
= buf
->st_ctime
= dw
;
3193 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3194 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3195 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3199 /*********************************************************************
3200 * _wstati64 (MSVCRT.@)
3202 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3205 struct MSVCRT__stat64 buf64
;
3207 ret
= MSVCRT__wstat64(path
, &buf64
);
3209 msvcrt_stat64_to_stati64(&buf64
, buf
);
3213 /*********************************************************************
3216 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3219 struct MSVCRT__stat64 buf64
;
3221 ret
= MSVCRT__wstat64( path
, &buf64
);
3222 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3226 #if _MSVCR_VER >= 80
3228 /*********************************************************************
3229 * _wstat32 (MSVCR80.@)
3231 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3234 struct MSVCRT__stat64 buf64
;
3236 ret
= MSVCRT__wstat64(path
, &buf64
);
3238 msvcrt_stat64_to_stat32(&buf64
, buf
);
3242 /*********************************************************************
3243 * _wstat32i64 (MSVCR80.@)
3245 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3248 struct MSVCRT__stat64 buf64
;
3250 ret
= MSVCRT__wstat64(path
, &buf64
);
3252 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3256 /*********************************************************************
3257 * _wstat64i32 (MSVCR80.@)
3259 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3262 struct MSVCRT__stat64 buf64
;
3264 ret
= MSVCRT__wstat64(path
, &buf64
);
3266 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3270 #endif /* _MSVCR_VER >= 80 */
3272 /*********************************************************************
3275 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3277 return MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
3280 /*********************************************************************
3281 * _telli64 (MSVCRT.@)
3283 __int64 CDECL
_telli64(int fd
)
3285 return MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
3288 /*********************************************************************
3289 * _tempnam (MSVCRT.@)
3291 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3293 char tmpbuf
[MAX_PATH
];
3294 const char *tmp_dir
= MSVCRT_getenv("TMP");
3296 if (tmp_dir
) dir
= tmp_dir
;
3298 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3299 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3301 TRACE("got name (%s)\n",tmpbuf
);
3302 DeleteFileA(tmpbuf
);
3303 return MSVCRT__strdup(tmpbuf
);
3305 TRACE("failed (%d)\n",GetLastError());
3309 /*********************************************************************
3310 * _wtempnam (MSVCRT.@)
3312 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3314 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3315 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3316 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3318 if (tmp_dir
) dir
= tmp_dir
;
3320 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3321 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3323 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3324 DeleteFileW(tmpbuf
);
3325 return MSVCRT__wcsdup(tmpbuf
);
3327 TRACE("failed (%d)\n",GetLastError());
3331 /*********************************************************************
3334 int CDECL
MSVCRT__umask(int umask
)
3336 int old_umask
= MSVCRT_umask
;
3337 TRACE("(%d)\n",umask
);
3338 MSVCRT_umask
= umask
;
3342 /*********************************************************************
3343 * _utime64 (MSVCRT.@)
3345 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3347 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3351 int retVal
= _futime64(fd
, t
);
3358 /*********************************************************************
3359 * _utime32 (MSVCRT.@)
3361 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3365 struct MSVCRT___utimbuf64 t64
;
3366 t64
.actime
= t
->actime
;
3367 t64
.modtime
= t
->modtime
;
3368 return _utime64( path
, &t64
);
3371 return _utime64( path
, NULL
);
3374 /*********************************************************************
3378 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3380 return _utime64( path
, t
);
3383 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3385 return _utime32( path
, t
);
3389 /*********************************************************************
3390 * _wutime64 (MSVCRT.@)
3392 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3394 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3398 int retVal
= _futime64(fd
, t
);
3405 /*********************************************************************
3406 * _wutime32 (MSVCRT.@)
3408 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3412 struct MSVCRT___utimbuf64 t64
;
3413 t64
.actime
= t
->actime
;
3414 t64
.modtime
= t
->modtime
;
3415 return _wutime64( path
, &t64
);
3418 return _wutime64( path
, NULL
);
3421 /*********************************************************************
3422 * _wutime (MSVCRT.@)
3425 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3427 return _wutime64( path
, t
);
3430 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3432 return _wutime32( path
, t
);
3436 /*********************************************************************
3439 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3442 ioinfo
*info
= get_ioinfo(fd
);
3443 HANDLE hand
= info
->handle
;
3445 /* Don't trace small writes, it gets *very* annoying */
3448 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3450 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3452 *MSVCRT__errno() = MSVCRT_EBADF
;
3453 release_ioinfo(info
);
3457 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3459 *MSVCRT__errno() = MSVCRT_EINVAL
;
3460 release_ioinfo(info
);
3464 /* If appending, go to EOF */
3465 if (info
->wxflag
& WX_APPEND
)
3466 MSVCRT__lseek(fd
, 0, FILE_END
);
3468 if (!(info
->wxflag
& WX_TEXT
))
3470 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3471 && (num_written
== count
))
3473 release_ioinfo(info
);
3476 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3477 hand
, GetLastError());
3478 msvcrt_set_errno(GetLastError());
3482 unsigned int i
, j
, nr_lf
, size
;
3485 const char *s
= buf
;
3487 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3489 /* find number of \n */
3490 for (nr_lf
=0, i
=0; i
<count
; i
++)
3496 if ((q
= p
= MSVCRT_malloc(size
)))
3498 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3507 FIXME("Malloc failed\n");
3519 else if (info
->exflag
& EF_UTF16
)
3521 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3522 if (s
[i
]=='\n' && s
[i
+1]==0)
3527 if ((q
= p
= MSVCRT_malloc(size
)))
3529 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3531 if (s
[i
]=='\n' && s
[i
+1]==0)
3542 FIXME("Malloc failed\n");
3558 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3559 if (s
[i
]=='\n' && s
[i
+1]==0)
3562 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3564 msvcrt_set_errno(GetLastError());
3566 release_ioinfo(info
);
3570 size
= conv_len
+nr_lf
;
3571 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3573 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3575 if (s
[i
]=='\n' && s
[i
+1]==0)
3583 q
= p
+count
+nr_lf
*2;
3584 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3585 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3589 FIXME("Malloc failed\n");
3596 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3598 release_ioinfo(info
);
3600 if (num_written
!= size
)
3602 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3603 fd
, hand
, GetLastError(), num_written
);
3604 msvcrt_set_errno(GetLastError());
3610 release_ioinfo(info
);
3614 /*********************************************************************
3617 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3621 MSVCRT__lock_file(file
);
3622 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3623 if (len
== sizeof(val
)) {
3624 MSVCRT__unlock_file(file
);
3628 file
->_flag
|= MSVCRT__IOERR
;
3629 MSVCRT__unlock_file(file
);
3633 /*********************************************************************
3636 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3640 MSVCRT__lock_file(file
);
3641 ret
= MSVCRT__fclose_nolock(file
);
3642 MSVCRT__unlock_file(file
);
3647 /*********************************************************************
3648 * _fclose_nolock (MSVCRT.@)
3650 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3654 if(!(file
->_flag
& (MSVCRT__IOREAD
| MSVCRT__IOWRT
| MSVCRT__IORW
)))
3661 MSVCRT_free(file
->_tmpfname
);
3662 file
->_tmpfname
= NULL
;
3663 /* flush stdio buffers */
3664 if(file
->_flag
& MSVCRT__IOWRT
)
3665 MSVCRT__fflush_nolock(file
);
3666 if(file
->_flag
& MSVCRT__IOMYBUF
)
3667 MSVCRT_free(file
->_base
);
3669 r
=MSVCRT__close(file
->_file
);
3672 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3675 /*********************************************************************
3678 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3680 return file
->_flag
& MSVCRT__IOEOF
;
3683 /*********************************************************************
3686 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3688 return file
->_flag
& MSVCRT__IOERR
;
3691 /*********************************************************************
3692 * _filbuf (MSVCRT.@)
3694 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3698 if(file
->_flag
& MSVCRT__IOSTRG
)
3701 /* Allocate buffer if needed */
3702 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3703 msvcrt_alloc_buffer(file
);
3705 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3706 if(file
->_flag
& MSVCRT__IORW
)
3707 file
->_flag
|= MSVCRT__IOREAD
;
3712 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3714 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3715 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3721 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3723 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3729 file
->_ptr
= file
->_base
+1;
3730 c
= *(unsigned char *)file
->_base
;
3735 /*********************************************************************
3738 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3742 MSVCRT__lock_file(file
);
3743 ret
= MSVCRT__fgetc_nolock(file
);
3744 MSVCRT__unlock_file(file
);
3749 /*********************************************************************
3750 * _fgetc_nolock (MSVCRT.@)
3752 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3759 i
= (unsigned char *)file
->_ptr
++;
3762 j
= MSVCRT__filbuf(file
);
3767 /*********************************************************************
3768 * _fgetchar (MSVCRT.@)
3770 int CDECL
MSVCRT__fgetchar(void)
3772 return MSVCRT_fgetc(MSVCRT_stdin
);
3775 /*********************************************************************
3778 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3780 int cc
= MSVCRT_EOF
;
3781 char * buf_start
= s
;
3783 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3784 file
,file
->_file
,s
,size
);
3786 MSVCRT__lock_file(file
);
3788 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3793 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3795 TRACE(":nothing read\n");
3796 MSVCRT__unlock_file(file
);
3799 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3802 TRACE(":got %s\n", debugstr_a(buf_start
));
3803 MSVCRT__unlock_file(file
);
3807 /*********************************************************************
3810 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3814 MSVCRT__lock_file(file
);
3815 ret
= MSVCRT__fgetwc_nolock(file
);
3816 MSVCRT__unlock_file(file
);
3821 /*********************************************************************
3822 * _fgetwc_nolock (MSVCRT.@)
3824 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3829 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3830 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3833 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3834 ch
= MSVCRT__fgetc_nolock(file
);
3835 if(ch
== MSVCRT_EOF
) {
3842 char mbs
[MSVCRT_MB_LEN_MAX
];
3845 ch
= MSVCRT__fgetc_nolock(file
);
3846 if(ch
!= MSVCRT_EOF
) {
3848 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3849 ch
= MSVCRT__fgetc_nolock(file
);
3850 if(ch
!= MSVCRT_EOF
) {
3859 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3866 /*********************************************************************
3869 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3876 MSVCRT__lock_file(file
);
3877 for (j
=0; j
<sizeof(int); j
++) {
3878 k
= MSVCRT__fgetc_nolock(file
);
3879 if (k
== MSVCRT_EOF
) {
3880 file
->_flag
|= MSVCRT__IOEOF
;
3881 MSVCRT__unlock_file(file
);
3887 MSVCRT__unlock_file(file
);
3891 /*********************************************************************
3894 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3896 return MSVCRT_fgetwc(file
);
3899 /*********************************************************************
3900 * _fgetwchar (MSVCRT.@)
3902 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3904 return MSVCRT_fgetwc(MSVCRT_stdin
);
3907 /*********************************************************************
3908 * getwchar (MSVCRT.@)
3910 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3912 return MSVCRT__fgetwchar();
3915 /*********************************************************************
3918 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3920 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3921 MSVCRT_wchar_t
* buf_start
= s
;
3923 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3924 file
,file
->_file
,s
,size
);
3926 MSVCRT__lock_file(file
);
3928 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3933 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3935 TRACE(":nothing read\n");
3936 MSVCRT__unlock_file(file
);
3939 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3942 TRACE(":got %s\n", debugstr_w(buf_start
));
3943 MSVCRT__unlock_file(file
);
3947 /*********************************************************************
3948 * _flsbuf (MSVCRT.@)
3950 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3952 /* Flush output buffer */
3953 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3954 msvcrt_alloc_buffer(file
);
3957 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3958 if(!(file
->_flag
& MSVCRT__IORW
)) {
3959 file
->_flag
|= MSVCRT__IOERR
;
3960 *MSVCRT__errno() = MSVCRT_EBADF
;
3963 file
->_flag
|= MSVCRT__IOWRT
;
3965 if(file
->_flag
& MSVCRT__IOREAD
) {
3966 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3967 file
->_flag
|= MSVCRT__IOERR
;
3971 file
->_ptr
= file
->_base
;
3972 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3975 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3978 if(file
->_cnt
<= 0) {
3979 res
= msvcrt_flush_buffer(file
);
3982 file
->_flag
|= MSVCRT__IOWRT
;
3983 file
->_cnt
=file
->_bufsiz
;
3991 /* set _cnt to 0 for unbuffered FILEs */
3993 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3996 file
->_flag
|= MSVCRT__IOERR
;
4001 /*********************************************************************
4004 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4008 MSVCRT__lock_file(file
);
4009 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
4010 MSVCRT__unlock_file(file
);
4015 /*********************************************************************
4016 * _fwrite_nolock (MSVCRT.@)
4018 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4020 MSVCRT_size_t wrcnt
=size
* nmemb
;
4026 if(file
->_cnt
< 0) {
4027 WARN("negative file->_cnt value in %p\n", file
);
4028 file
->_flag
|= MSVCRT__IOERR
;
4030 } else if(file
->_cnt
) {
4031 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
4032 memcpy(file
->_ptr
, ptr
, pcnt
);
4037 ptr
= (const char*)ptr
+ pcnt
;
4038 } else if((file
->_flag
& MSVCRT__IONBF
)
4039 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4040 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4044 if(file
->_flag
& MSVCRT__IONBF
)
4046 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4047 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4049 bufsiz
= file
->_bufsiz
;
4051 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4053 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
4056 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
4057 file
->_flag
|= MSVCRT__IOERR
;
4062 ptr
= (const char*)ptr
+ pcnt
;
4064 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
4068 ptr
= (const char*)ptr
+ 1;
4072 return written
/ size
;
4075 /*********************************************************************
4078 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4082 MSVCRT__lock_file(file
);
4083 ret
= MSVCRT__fputwc_nolock(wc
, file
);
4084 MSVCRT__unlock_file(file
);
4089 /*********************************************************************
4090 * _fputwc_nolock (MSVCRT.@)
4092 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4094 MSVCRT_wchar_t mwc
=wc
;
4098 fdinfo
= get_ioinfo_nolock(file
->_file
);
4100 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
4101 char buf
[MSVCRT_MB_LEN_MAX
];
4104 char_len
= MSVCRT_wctomb(buf
, mwc
);
4105 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
4109 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4118 /*********************************************************************
4119 * _fputwchar (MSVCRT.@)
4121 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
4123 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
4126 /*********************************************************************
4127 * _wfsopen (MSVCRT.@)
4129 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
4132 int open_flags
, stream_flags
, fd
;
4134 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4136 /* map mode string to open() flags. "man fopen" for possibilities. */
4137 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4141 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4144 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4146 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4153 TRACE(":got (%p)\n",file
);
4154 if (fd
>= 0 && !file
)
4160 /*********************************************************************
4161 * _fsopen (MSVCRT.@)
4163 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4166 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4168 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4169 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4170 *MSVCRT__errno() = MSVCRT_EINVAL
;
4173 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4176 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4177 *MSVCRT__errno() = MSVCRT_EINVAL
;
4181 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4188 /*********************************************************************
4191 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4193 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4196 /*********************************************************************
4197 * fopen_s (MSVCRT.@)
4199 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4200 const char *filename
, const char *mode
)
4202 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4203 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4204 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4206 *pFile
= MSVCRT_fopen(filename
, mode
);
4209 return *MSVCRT__errno();
4213 /*********************************************************************
4214 * _wfopen (MSVCRT.@)
4216 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4218 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4221 /*********************************************************************
4222 * _wfopen_s (MSVCRT.@)
4224 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4225 const MSVCRT_wchar_t
*mode
)
4227 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4228 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4229 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4231 *pFile
= MSVCRT__wfopen(filename
, mode
);
4234 return *MSVCRT__errno();
4238 /*********************************************************************
4241 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4245 MSVCRT__lock_file(file
);
4246 ret
= MSVCRT__fputc_nolock(c
, file
);
4247 MSVCRT__unlock_file(file
);
4252 /*********************************************************************
4253 * _fputc_nolock (MSVCRT.@)
4255 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4264 res
= msvcrt_flush_buffer(file
);
4265 return res
? res
: c
;
4271 res
= MSVCRT__flsbuf(c
, file
);
4276 /*********************************************************************
4277 * _fputchar (MSVCRT.@)
4279 int CDECL
MSVCRT__fputchar(int c
)
4281 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4284 /*********************************************************************
4287 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4291 MSVCRT__lock_file(file
);
4292 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4293 MSVCRT__unlock_file(file
);
4298 /*********************************************************************
4299 * _fread_nolock (MSVCRT.@)
4301 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4303 MSVCRT_size_t rcnt
=size
* nmemb
;
4304 MSVCRT_size_t read
=0;
4305 MSVCRT_size_t pread
=0;
4310 /* first buffered data */
4312 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4313 memcpy(ptr
, file
->_ptr
, pcnt
);
4318 ptr
= (char*)ptr
+ pcnt
;
4319 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4320 if(file
->_flag
& MSVCRT__IORW
) {
4321 file
->_flag
|= MSVCRT__IOREAD
;
4327 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4328 msvcrt_alloc_buffer(file
);
4333 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4334 i
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4335 file
->_ptr
= file
->_base
;
4338 if (i
> rcnt
) i
= rcnt
;
4340 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4341 if (i
> 0 && i
< file
->_cnt
) {
4342 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4343 file
->_flag
&= ~MSVCRT__IOEOF
;
4346 memcpy(ptr
, file
->_ptr
, i
);
4350 } else if (rcnt
> INT_MAX
) {
4351 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4352 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4353 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4355 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4359 ptr
= (char *)ptr
+i
;
4360 /* expose feof condition in the flags
4361 * MFC tests file->_flag for feof, and doesn't call feof())
4363 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4364 file
->_flag
|= MSVCRT__IOEOF
;
4367 file
->_flag
|= MSVCRT__IOERR
;
4377 #if _MSVCR_VER >= 80
4379 /*********************************************************************
4380 * fread_s (MSVCR80.@)
4382 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4383 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4387 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4389 memset(buf
, 0, buf_size
);
4392 if(!elem_size
|| !count
) return 0;
4394 MSVCRT__lock_file(stream
);
4395 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4396 MSVCRT__unlock_file(stream
);
4401 /*********************************************************************
4402 * _fread_nolock_s (MSVCR80.@)
4404 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4405 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4407 size_t bytes_left
, buf_pos
;
4409 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4411 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4413 memset(buf
, 0, buf_size
);
4416 if(!elem_size
|| !count
) return 0;
4417 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4418 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4420 bytes_left
= elem_size
*count
;
4423 if(stream
->_cnt
> 0) {
4424 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4426 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4427 memset(buf
, 0, buf_size
);
4431 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4435 int c
= MSVCRT__filbuf(stream
);
4440 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4441 memset(buf
, 0, buf_size
);
4445 ((char*)buf
)[buf_pos
++] = c
;
4450 return buf_pos
/elem_size
;
4453 #endif /* _MSVCR_VER >= 80 */
4455 /*********************************************************************
4456 * _wfreopen (MSVCRT.@)
4459 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4461 int open_flags
, stream_flags
, fd
;
4463 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4466 if (!file
|| ((fd
= file
->_file
) < 0))
4470 MSVCRT_fclose(file
);
4471 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4473 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4475 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4485 /*********************************************************************
4486 * _wfreopen_s (MSVCRT.@)
4488 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4489 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4491 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4492 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4493 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4494 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4496 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4499 return *MSVCRT__errno();
4503 /*********************************************************************
4504 * freopen (MSVCRT.@)
4507 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4510 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4512 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4513 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4519 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4526 /*********************************************************************
4527 * freopen_s (MSVCRT.@)
4529 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4530 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4532 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4533 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4534 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4535 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4537 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4540 return *MSVCRT__errno();
4544 /*********************************************************************
4545 * fsetpos (MSVCRT.@)
4547 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4551 MSVCRT__lock_file(file
);
4552 msvcrt_flush_buffer(file
);
4554 /* Reset direction of i/o */
4555 if(file
->_flag
& MSVCRT__IORW
) {
4556 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4559 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,MSVCRT_SEEK_SET
) == -1) ? -1 : 0;
4560 MSVCRT__unlock_file(file
);
4564 /*********************************************************************
4565 * _ftelli64 (MSVCRT.@)
4567 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4571 MSVCRT__lock_file(file
);
4572 ret
= MSVCRT__ftelli64_nolock(file
);
4573 MSVCRT__unlock_file(file
);
4578 /*********************************************************************
4579 * _ftelli64_nolock (MSVCRT.@)
4581 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4585 pos
= _telli64(file
->_file
);
4588 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4589 if(file
->_flag
& MSVCRT__IOWRT
) {
4590 pos
+= file
->_ptr
- file
->_base
;
4592 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4595 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4599 } else if(!file
->_cnt
) { /* nothing to do */
4600 } else if(MSVCRT__lseeki64(file
->_file
, 0, MSVCRT_SEEK_END
)==pos
) {
4604 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4605 for(i
=0; i
<file
->_cnt
; i
++)
4606 if(file
->_ptr
[i
] == '\n')
4612 if(MSVCRT__lseeki64(file
->_file
, pos
, MSVCRT_SEEK_SET
) != pos
)
4615 pos
-= file
->_bufsiz
;
4616 pos
+= file
->_ptr
- file
->_base
;
4618 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4619 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4622 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4632 /*********************************************************************
4635 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4637 return MSVCRT__ftelli64(file
);
4640 #if _MSVCR_VER >= 80
4641 /*********************************************************************
4642 * _ftell_nolock (MSVCR80.@)
4644 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4646 return MSVCRT__ftelli64_nolock(file
);
4650 /*********************************************************************
4651 * fgetpos (MSVCRT.@)
4653 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4655 *pos
= MSVCRT__ftelli64(file
);
4661 /*********************************************************************
4664 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4666 MSVCRT_size_t len
= strlen(s
);
4669 MSVCRT__lock_file(file
);
4670 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4671 MSVCRT__unlock_file(file
);
4675 /*********************************************************************
4678 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4680 MSVCRT_size_t i
, len
= strlenW(s
);
4684 MSVCRT__lock_file(file
);
4685 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4686 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4687 MSVCRT__unlock_file(file
);
4691 tmp_buf
= add_std_buffer(file
);
4692 for (i
=0; i
<len
; i
++) {
4693 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4694 if(tmp_buf
) remove_std_buffer(file
);
4695 MSVCRT__unlock_file(file
);
4700 if(tmp_buf
) remove_std_buffer(file
);
4701 MSVCRT__unlock_file(file
);
4705 /*********************************************************************
4706 * getchar (MSVCRT.@)
4708 int CDECL
MSVCRT_getchar(void)
4710 return MSVCRT_fgetc(MSVCRT_stdin
);
4713 /*********************************************************************
4716 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4718 return MSVCRT_fgetc(file
);
4721 /*********************************************************************
4722 * gets_s (MSVCR80.@)
4724 char * CDECL
MSVCRT_gets_s(char *buf
, MSVCRT_size_t len
)
4726 char *buf_start
= buf
;
4729 if (!MSVCRT_CHECK_PMT(buf
!= NULL
)) return NULL
;
4730 if (!MSVCRT_CHECK_PMT(len
!= 0)) return NULL
;
4732 MSVCRT__lock_file(MSVCRT_stdin
);
4733 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
);
4734 len
!= 0 && cc
!= MSVCRT_EOF
&& cc
!= '\n';
4735 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4743 MSVCRT__unlock_file(MSVCRT_stdin
);
4748 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4752 if ((cc
== MSVCRT_EOF
) && (buf_start
== buf
))
4754 TRACE(":nothing read\n");
4759 TRACE("got '%s'\n", buf_start
);
4763 /*********************************************************************
4766 char * CDECL
MSVCRT_gets(char *buf
)
4768 return MSVCRT_gets_s(buf
, -1);
4771 /*********************************************************************
4774 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4777 MSVCRT_wchar_t
* ws
= buf
;
4779 MSVCRT__lock_file(MSVCRT_stdin
);
4780 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4781 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4784 *buf
++ = (MSVCRT_wchar_t
)cc
;
4786 MSVCRT__unlock_file(MSVCRT_stdin
);
4788 if ((cc
== MSVCRT_WEOF
) && (ws
== buf
))
4790 TRACE(":nothing read\n");
4795 TRACE("got %s\n", debugstr_w(ws
));
4799 /*********************************************************************
4802 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4804 return MSVCRT_fputc(c
, file
);
4807 /*********************************************************************
4808 * putchar (MSVCRT.@)
4810 int CDECL
MSVCRT_putchar(int c
)
4812 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4815 /*********************************************************************
4818 int CDECL
MSVCRT_puts(const char *s
)
4820 MSVCRT_size_t len
= strlen(s
);
4823 MSVCRT__lock_file(MSVCRT_stdout
);
4824 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4825 MSVCRT__unlock_file(MSVCRT_stdout
);
4829 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4830 MSVCRT__unlock_file(MSVCRT_stdout
);
4834 /*********************************************************************
4837 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4839 static const MSVCRT_wchar_t nl
= '\n';
4840 MSVCRT_size_t len
= strlenW(s
);
4843 MSVCRT__lock_file(MSVCRT_stdout
);
4844 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4845 MSVCRT__unlock_file(MSVCRT_stdout
);
4849 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4850 MSVCRT__unlock_file(MSVCRT_stdout
);
4854 /*********************************************************************
4857 int CDECL
MSVCRT_remove(const char *path
)
4859 TRACE("(%s)\n",path
);
4860 if (DeleteFileA(path
))
4862 TRACE(":failed (%d)\n",GetLastError());
4863 msvcrt_set_errno(GetLastError());
4867 /*********************************************************************
4868 * _wremove (MSVCRT.@)
4870 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4872 TRACE("(%s)\n",debugstr_w(path
));
4873 if (DeleteFileW(path
))
4875 TRACE(":failed (%d)\n",GetLastError());
4876 msvcrt_set_errno(GetLastError());
4880 /*********************************************************************
4883 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4885 TRACE(":from %s to %s\n",oldpath
,newpath
);
4886 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4888 TRACE(":failed (%d)\n",GetLastError());
4889 msvcrt_set_errno(GetLastError());
4893 /*********************************************************************
4894 * _wrename (MSVCRT.@)
4896 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4898 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4899 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4901 TRACE(":failed (%d)\n",GetLastError());
4902 msvcrt_set_errno(GetLastError());
4906 /*********************************************************************
4907 * setvbuf (MSVCRT.@)
4909 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4911 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4912 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4913 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4915 MSVCRT__lock_file(file
);
4917 MSVCRT__fflush_nolock(file
);
4918 if(file
->_flag
& MSVCRT__IOMYBUF
)
4919 MSVCRT_free(file
->_base
);
4920 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4923 if(mode
== MSVCRT__IONBF
) {
4924 file
->_flag
|= MSVCRT__IONBF
;
4925 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4928 file
->_base
= file
->_ptr
= buf
;
4929 file
->_flag
|= MSVCRT__USERBUF
;
4930 file
->_bufsiz
= size
;
4932 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4935 MSVCRT__unlock_file(file
);
4939 file
->_flag
|= MSVCRT__IOMYBUF
;
4940 file
->_bufsiz
= size
;
4942 MSVCRT__unlock_file(file
);
4946 /*********************************************************************
4949 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4951 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4954 static int tmpnam_helper(char *s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
4960 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
4964 *MSVCRT__errno() = MSVCRT_ERANGE
;
4965 return MSVCRT_ERANGE
;
4970 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4971 if (digits
+1 > size
) {
4973 *MSVCRT__errno() = MSVCRT_ERANGE
;
4974 return MSVCRT_ERANGE
;
4976 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4982 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4983 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4987 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4988 if (digits
+1 > size
) {
4990 *MSVCRT__errno() = MSVCRT_ERANGE
;
4991 return MSVCRT_ERANGE
;
4993 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4996 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4997 GetLastError() == ERROR_FILE_NOT_FOUND
)
5003 int CDECL
MSVCRT_tmpnam_s(char *s
, MSVCRT_size_t size
)
5005 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5008 /*********************************************************************
5011 char * CDECL
MSVCRT_tmpnam(char *s
)
5014 thread_data_t
*data
= msvcrt_get_thread_data();
5016 if(!data
->tmpnam_buffer
)
5017 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
5019 s
= data
->tmpnam_buffer
;
5022 return tmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5025 static int wtmpnam_helper(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
5027 MSVCRT_wchar_t tmpstr
[8];
5028 MSVCRT_wchar_t
*p
= s
;
5031 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
5035 *MSVCRT__errno() = MSVCRT_ERANGE
;
5036 return MSVCRT_ERANGE
;
5041 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5042 if (digits
+1 > size
) {
5044 *MSVCRT__errno() = MSVCRT_ERANGE
;
5045 return MSVCRT_ERANGE
;
5047 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5053 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5054 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5058 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5059 if (digits
+1 > size
) {
5061 *MSVCRT__errno() = MSVCRT_ERANGE
;
5062 return MSVCRT_ERANGE
;
5064 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5067 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5068 GetLastError() == ERROR_FILE_NOT_FOUND
)
5074 /*********************************************************************
5075 * _wtmpnam_s (MSVCRT.@)
5077 int CDECL
MSVCRT__wtmpnam_s(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
)
5079 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5082 /*********************************************************************
5083 * _wtmpnam (MSVCRT.@)
5085 MSVCRT_wchar_t
* CDECL
MSVCRT__wtmpnam(MSVCRT_wchar_t
*s
)
5088 thread_data_t
*data
= msvcrt_get_thread_data();
5090 if(!data
->wtmpnam_buffer
)
5091 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
5093 s
= data
->wtmpnam_buffer
;
5096 return wtmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5099 /*********************************************************************
5100 * tmpfile (MSVCRT.@)
5102 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
5104 char *filename
= MSVCRT__tempnam(",", "t");
5106 MSVCRT_FILE
* file
= NULL
;
5109 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
5110 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
5111 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5113 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
5118 else file
->_tmpfname
= MSVCRT__strdup(filename
);
5121 if(fd
!= -1 && !file
)
5123 MSVCRT_free(filename
);
5128 /*********************************************************************
5129 * tmpfile_s (MSVCRT.@)
5131 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
5133 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
5135 *file
= MSVCRT_tmpfile();
5139 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5141 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
5144 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
5148 MSVCRT__lock_file(file
);
5150 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5151 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
5152 MSVCRT__unlock_file(file
);
5156 for(i
=0; i
<len
; i
++) {
5157 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
5158 MSVCRT__unlock_file(file
);
5163 MSVCRT__unlock_file(file
);
5167 static int vfprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const char *format
,
5168 MSVCRT__locale_t locale
, __ms_va_list valist
)
5170 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5174 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5175 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5177 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5178 memset(args_ctx
, 0, sizeof(args_ctx
));
5179 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5181 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5182 *MSVCRT__errno() = MSVCRT_EINVAL
;
5185 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5188 MSVCRT__lock_file(file
);
5189 tmp_buf
= add_std_buffer(file
);
5190 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5191 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5192 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5193 if(tmp_buf
) remove_std_buffer(file
);
5194 MSVCRT__unlock_file(file
);
5199 static int vfwprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5200 MSVCRT__locale_t locale
, __ms_va_list valist
)
5202 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5206 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5207 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5209 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5210 memset(args_ctx
, 0, sizeof(args_ctx
));
5211 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5213 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5214 *MSVCRT__errno() = MSVCRT_EINVAL
;
5217 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5220 MSVCRT__lock_file(file
);
5221 tmp_buf
= add_std_buffer(file
);
5222 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5223 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5224 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5225 if(tmp_buf
) remove_std_buffer(file
);
5226 MSVCRT__unlock_file(file
);
5231 /*********************************************************************
5232 * _vfprintf_s_l (MSVCRT.@)
5234 int CDECL
MSVCRT__vfprintf_s_l(MSVCRT_FILE
* file
, const char *format
,
5235 MSVCRT__locale_t locale
, __ms_va_list valist
)
5237 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5240 /*********************************************************************
5241 * _vfwprintf_s_l (MSVCRT.@)
5243 int CDECL
MSVCRT__vfwprintf_s_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5244 MSVCRT__locale_t locale
, __ms_va_list valist
)
5246 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5249 /*********************************************************************
5250 * vfprintf (MSVCRT.@)
5252 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5254 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5257 /*********************************************************************
5258 * vfprintf_s (MSVCRT.@)
5260 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5262 return MSVCRT__vfprintf_s_l(file
, format
, NULL
, valist
);
5265 /*********************************************************************
5266 * vfwprintf (MSVCRT.@)
5268 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5270 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5273 /*********************************************************************
5274 * vfwprintf_s (MSVCRT.@)
5276 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5278 return MSVCRT__vfwprintf_s_l(file
, format
, NULL
, valist
);
5281 #if _MSVCR_VER >= 140
5283 /*********************************************************************
5284 * __stdio_common_vfprintf (UCRTBASE.@)
5286 int CDECL
MSVCRT__stdio_common_vfprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5287 MSVCRT__locale_t locale
, __ms_va_list valist
)
5289 if (options
& ~UCRTBASE_PRINTF_MASK
)
5290 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5292 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5295 /*********************************************************************
5296 * __stdio_common_vfprintf_s (UCRTBASE.@)
5298 int CDECL
MSVCRT__stdio_common_vfprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5299 MSVCRT__locale_t locale
, __ms_va_list valist
)
5301 if (options
& ~UCRTBASE_PRINTF_MASK
)
5302 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5304 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5305 file
, format
, locale
, valist
);
5308 /*********************************************************************
5309 * __stdio_common_vfwprintf (UCRTBASE.@)
5311 int CDECL
MSVCRT__stdio_common_vfwprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5312 MSVCRT__locale_t locale
, __ms_va_list valist
)
5314 if (options
& ~UCRTBASE_PRINTF_MASK
)
5315 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5317 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5320 /*********************************************************************
5321 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5323 int CDECL
MSVCRT__stdio_common_vfwprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5324 MSVCRT__locale_t locale
, __ms_va_list valist
)
5326 if (options
& ~UCRTBASE_PRINTF_MASK
)
5327 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5329 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5330 file
, format
, locale
, valist
);
5333 #endif /* _MSVCR_VER >= 140 */
5335 /*********************************************************************
5336 * _vfprintf_l (MSVCRT.@)
5338 int CDECL
MSVCRT__vfprintf_l(MSVCRT_FILE
* file
, const char *format
,
5339 MSVCRT__locale_t locale
, __ms_va_list valist
)
5341 return vfprintf_helper(0, file
, format
, locale
, valist
);
5344 /*********************************************************************
5345 * _vfwprintf_l (MSVCRT.@)
5347 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5348 MSVCRT__locale_t locale
, __ms_va_list valist
)
5350 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5353 /*********************************************************************
5354 * _vfprintf_p_l (MSVCRT.@)
5356 int CDECL
MSVCRT__vfprintf_p_l(MSVCRT_FILE
* file
, const char *format
,
5357 MSVCRT__locale_t locale
, __ms_va_list valist
)
5359 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5360 file
, format
, locale
, valist
);
5363 /*********************************************************************
5364 * _vfprintf_p (MSVCRT.@)
5366 int CDECL
MSVCRT__vfprintf_p(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5368 return MSVCRT__vfprintf_p_l(file
, format
, NULL
, valist
);
5371 /*********************************************************************
5372 * _vfwprintf_p_l (MSVCRT.@)
5374 int CDECL
MSVCRT__vfwprintf_p_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5375 MSVCRT__locale_t locale
, __ms_va_list valist
)
5377 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5378 file
, format
, locale
, valist
);
5381 /*********************************************************************
5382 * _vfwprintf_p (MSVCRT.@)
5384 int CDECL
MSVCRT__vfwprintf_p(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5386 return MSVCRT__vfwprintf_p_l(file
, format
, NULL
, valist
);
5389 /*********************************************************************
5390 * vprintf (MSVCRT.@)
5392 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5394 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5397 /*********************************************************************
5398 * vprintf_s (MSVCRT.@)
5400 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5402 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5405 /*********************************************************************
5406 * vwprintf (MSVCRT.@)
5408 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5410 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5413 /*********************************************************************
5414 * vwprintf_s (MSVCRT.@)
5416 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5418 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5421 /*********************************************************************
5422 * fprintf (MSVCRT.@)
5424 int WINAPIV
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5426 __ms_va_list valist
;
5428 __ms_va_start(valist
, format
);
5429 res
= MSVCRT_vfprintf(file
, format
, valist
);
5430 __ms_va_end(valist
);
5434 /*********************************************************************
5435 * fprintf_s (MSVCRT.@)
5437 int WINAPIV
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5439 __ms_va_list valist
;
5441 __ms_va_start(valist
, format
);
5442 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5443 __ms_va_end(valist
);
5447 /*********************************************************************
5448 * fwprintf (MSVCRT.@)
5450 int WINAPIV
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5452 __ms_va_list valist
;
5454 __ms_va_start(valist
, format
);
5455 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5456 __ms_va_end(valist
);
5460 /*********************************************************************
5461 * fwprintf_s (MSVCRT.@)
5463 int WINAPIV
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5465 __ms_va_list valist
;
5467 __ms_va_start(valist
, format
);
5468 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5469 __ms_va_end(valist
);
5473 /*********************************************************************
5474 * _fwprintf_l (MSVCRT.@)
5476 int WINAPIV
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5478 __ms_va_list valist
;
5480 __ms_va_start(valist
, locale
);
5481 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5482 __ms_va_end(valist
);
5486 /*********************************************************************
5489 int WINAPIV
MSVCRT_printf(const char *format
, ...)
5491 __ms_va_list valist
;
5493 __ms_va_start(valist
, format
);
5494 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5495 __ms_va_end(valist
);
5499 /*********************************************************************
5500 * printf_s (MSVCRT.@)
5502 int WINAPIV
MSVCRT_printf_s(const char *format
, ...)
5504 __ms_va_list valist
;
5506 __ms_va_start(valist
, format
);
5507 res
= MSVCRT_vprintf_s(format
, valist
);
5508 __ms_va_end(valist
);
5512 /*********************************************************************
5515 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5519 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5521 MSVCRT__lock_file(file
);
5522 ret
= MSVCRT__ungetc_nolock(c
, file
);
5523 MSVCRT__unlock_file(file
);
5528 /*********************************************************************
5529 * _ungetc_nolock (MSVCRT.@)
5531 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5533 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5535 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5536 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5539 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5540 && msvcrt_alloc_buffer(file
))
5541 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5544 if(file
->_ptr
>file
->_base
) {
5546 if(file
->_flag
& MSVCRT__IOSTRG
) {
5547 if(*file
->_ptr
!= c
) {
5555 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5556 file
->_flag
|= MSVCRT__IOREAD
;
5563 /*********************************************************************
5564 * ungetwc (MSVCRT.@)
5566 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5570 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5572 MSVCRT__lock_file(file
);
5573 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5574 MSVCRT__unlock_file(file
);
5579 /*********************************************************************
5580 * _ungetwc_nolock (MSVCRT.@)
5582 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5584 MSVCRT_wchar_t mwc
= wc
;
5586 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5587 if (wc
== MSVCRT_WEOF
)
5590 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5591 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5592 unsigned char * pp
= (unsigned char *)&mwc
;
5595 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5596 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5600 char mbs
[MSVCRT_MB_LEN_MAX
];
5603 len
= MSVCRT_wctomb(mbs
, mwc
);
5607 for(len
--; len
>=0; len
--) {
5608 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5616 /*********************************************************************
5617 * wprintf (MSVCRT.@)
5619 int WINAPIV
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5621 __ms_va_list valist
;
5623 __ms_va_start(valist
, format
);
5624 res
= MSVCRT_vwprintf(format
, valist
);
5625 __ms_va_end(valist
);
5629 /*********************************************************************
5630 * wprintf_s (MSVCRT.@)
5632 int WINAPIV
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5634 __ms_va_list valist
;
5636 __ms_va_start(valist
, format
);
5637 res
= MSVCRT_vwprintf_s(format
, valist
);
5638 __ms_va_end(valist
);
5642 /*********************************************************************
5643 * _getmaxstdio (MSVCRT.@)
5645 int CDECL
MSVCRT__getmaxstdio(void)
5647 return MSVCRT_max_streams
;
5650 /*********************************************************************
5651 * _setmaxstdio (MSVCRT.@)
5653 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5655 TRACE("%d\n", newmax
);
5657 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5660 MSVCRT_max_streams
= newmax
;
5661 return MSVCRT_max_streams
;
5664 #if _MSVCR_VER >= 140
5665 /*********************************************************************
5666 * _get_stream_buffer_pointers (UCRTBASE.@)
5668 int CDECL
MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE
*file
, char*** base
,
5669 char*** ptr
, int** count
)
5672 *base
= &file
->_base
;
5676 *count
= &file
->_cnt
;