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 /*********************************************************************
790 * __acrt_iob_func(UCRTBASE.@)
792 MSVCRT_FILE
* CDECL
MSVCRT___acrt_iob_func(unsigned idx
)
794 return &MSVCRT__iob
[idx
];
797 /*********************************************************************
800 int CDECL
MSVCRT__access(const char *filename
, int mode
)
802 DWORD attr
= GetFileAttributesA(filename
);
804 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
806 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
808 msvcrt_set_errno(GetLastError());
811 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
813 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
819 /*********************************************************************
820 * _access_s (MSVCRT.@)
822 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
824 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
825 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
827 if (MSVCRT__access(filename
, mode
) == -1)
828 return *MSVCRT__errno();
832 /*********************************************************************
833 * _waccess (MSVCRT.@)
835 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
837 DWORD attr
= GetFileAttributesW(filename
);
839 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
841 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
843 msvcrt_set_errno(GetLastError());
846 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
848 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
854 /*********************************************************************
855 * _waccess_s (MSVCRT.@)
857 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
859 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
860 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
862 if (MSVCRT__waccess(filename
, mode
) == -1)
863 return *MSVCRT__errno();
867 /*********************************************************************
870 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
872 DWORD oldFlags
= GetFileAttributesA(path
);
874 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
876 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
877 oldFlags
| FILE_ATTRIBUTE_READONLY
;
879 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
882 msvcrt_set_errno(GetLastError());
886 /*********************************************************************
889 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
891 DWORD oldFlags
= GetFileAttributesW(path
);
893 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
895 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
896 oldFlags
| FILE_ATTRIBUTE_READONLY
;
898 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
901 msvcrt_set_errno(GetLastError());
905 /*********************************************************************
908 int CDECL
MSVCRT__unlink(const char *path
)
910 TRACE("%s\n",debugstr_a(path
));
911 if(DeleteFileA(path
))
913 TRACE("failed (%d)\n",GetLastError());
914 msvcrt_set_errno(GetLastError());
918 /*********************************************************************
919 * _wunlink (MSVCRT.@)
921 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
923 TRACE("(%s)\n",debugstr_w(path
));
924 if(DeleteFileW(path
))
926 TRACE("failed (%d)\n",GetLastError());
927 msvcrt_set_errno(GetLastError());
931 /*********************************************************************
934 int CDECL
MSVCRT__commit(int fd
)
936 ioinfo
*info
= get_ioinfo(fd
);
939 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
941 if (info
->handle
== INVALID_HANDLE_VALUE
)
943 else if (!FlushFileBuffers(info
->handle
))
945 if (GetLastError() == ERROR_INVALID_HANDLE
)
947 /* FlushFileBuffers fails for console handles
948 * so we ignore this error.
954 TRACE(":failed-last error (%d)\n",GetLastError());
955 msvcrt_set_errno(GetLastError());
965 release_ioinfo(info
);
969 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
970 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
972 /* INTERNAL: Flush all stream buffer */
973 static int msvcrt_flush_all_buffers(int mask
)
975 int i
, num_flushed
= 0;
979 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
980 file
= msvcrt_get_file(i
);
984 if(file
->_flag
& mask
) {
992 TRACE(":flushed (%d) handles\n",num_flushed
);
996 /*********************************************************************
997 * _flushall (MSVCRT.@)
999 int CDECL
MSVCRT__flushall(void)
1001 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
1004 /*********************************************************************
1007 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
1012 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1015 MSVCRT__lock_file(file
);
1016 ret
= MSVCRT__fflush_nolock(file
);
1017 MSVCRT__unlock_file(file
);
1023 /*********************************************************************
1024 * _fflush_nolock (MSVCRT.@)
1026 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
1031 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1035 res
= msvcrt_flush_buffer(file
);
1036 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1037 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
1041 /*********************************************************************
1044 int CDECL
MSVCRT__close(int fd
)
1046 ioinfo
*info
= get_ioinfo(fd
);
1049 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1051 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1052 *MSVCRT__errno() = MSVCRT_EBADF
;
1054 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, MSVCRT_EBADF
)) {
1056 } else if (fd
== MSVCRT_STDOUT_FILENO
&&
1057 info
->handle
== get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
) {
1060 } else if (fd
== MSVCRT_STDERR_FILENO
&&
1061 info
->handle
== get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
) {
1065 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1068 WARN(":failed-last error (%d)\n",GetLastError());
1069 msvcrt_set_errno(GetLastError());
1072 release_ioinfo(info
);
1076 /*********************************************************************
1079 * MSDN isn't clear on this point, but the remarks for _pipe
1080 * indicate file descriptors duplicated with _dup and _dup2 are always
1083 int CDECL
MSVCRT__dup2(int od
, int nd
)
1085 ioinfo
*info_od
, *info_nd
;
1088 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1092 info_od
= get_ioinfo(od
);
1093 info_nd
= get_ioinfo_alloc_fd(nd
);
1097 info_nd
= get_ioinfo_alloc_fd(nd
);
1098 info_od
= get_ioinfo(od
);
1101 if (info_nd
== &MSVCRT___badioinfo
)
1105 else if (info_od
->wxflag
& WX_OPEN
)
1109 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1110 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1112 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1114 if (info_nd
->wxflag
& WX_OPEN
)
1117 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1118 /* _dup2 returns 0, not nd, on success */
1124 msvcrt_set_errno(GetLastError());
1129 *MSVCRT__errno() = MSVCRT_EBADF
;
1133 release_ioinfo(info_od
);
1134 release_ioinfo(info_nd
);
1138 /*********************************************************************
1141 int CDECL
MSVCRT__dup(int od
)
1144 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1146 if (MSVCRT__dup2(od
, fd
) == 0)
1150 release_ioinfo(info
);
1154 /*********************************************************************
1157 int CDECL
MSVCRT__eof(int fd
)
1159 ioinfo
*info
= get_ioinfo(fd
);
1160 DWORD curpos
,endpos
;
1161 LONG hcurpos
,hendpos
;
1163 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1165 if (info
->handle
== INVALID_HANDLE_VALUE
)
1167 release_ioinfo(info
);
1171 if (info
->wxflag
& WX_ATEOF
)
1173 release_ioinfo(info
);
1177 /* Otherwise we do it the hard way */
1178 hcurpos
= hendpos
= 0;
1179 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1180 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1182 if (curpos
== endpos
&& hcurpos
== hendpos
)
1184 /* FIXME: shouldn't WX_ATEOF be set here? */
1185 release_ioinfo(info
);
1189 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1190 release_ioinfo(info
);
1194 /*********************************************************************
1195 * _fcloseall (MSVCRT.@)
1197 int CDECL
MSVCRT__fcloseall(void)
1199 int num_closed
= 0, i
;
1203 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1204 file
= msvcrt_get_file(i
);
1206 if (file
->_flag
&& !MSVCRT_fclose(file
))
1211 TRACE(":closed (%d) handles\n",num_closed
);
1215 /* free everything on process exit */
1216 void msvcrt_free_io(void)
1222 MSVCRT__fcloseall();
1224 for(i
=0; i
<ARRAY_SIZE(MSVCRT___pioinfo
); i
++)
1226 if(!MSVCRT___pioinfo
[i
])
1229 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1231 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1232 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1234 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1237 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1239 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1240 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1242 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1243 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1247 for(i
=0; i
<ARRAY_SIZE(MSVCRT_fstream
); i
++)
1248 MSVCRT_free(MSVCRT_fstream
[i
]);
1251 /*********************************************************************
1252 * _lseeki64 (MSVCRT.@)
1254 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1256 ioinfo
*info
= get_ioinfo(fd
);
1259 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1261 if (info
->handle
== INVALID_HANDLE_VALUE
)
1263 release_ioinfo(info
);
1267 if (whence
< 0 || whence
> 2)
1269 release_ioinfo(info
);
1270 *MSVCRT__errno() = MSVCRT_EINVAL
;
1274 TRACE(":fd (%d) to %s pos %s\n",
1275 fd
,wine_dbgstr_longlong(offset
),
1276 (whence
==MSVCRT_SEEK_SET
)?"SEEK_SET":
1277 (whence
==MSVCRT_SEEK_CUR
)?"SEEK_CUR":
1278 (whence
==MSVCRT_SEEK_END
)?"SEEK_END":"UNKNOWN");
1280 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1281 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1282 ofs
.QuadPart
= offset
;
1283 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1284 GetLastError() == ERROR_SUCCESS
)
1286 info
->wxflag
&= ~WX_ATEOF
;
1287 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1289 release_ioinfo(info
);
1290 return ofs
.QuadPart
;
1292 release_ioinfo(info
);
1293 TRACE(":error-last error (%d)\n",GetLastError());
1294 msvcrt_set_errno(GetLastError());
1298 /*********************************************************************
1301 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1303 return MSVCRT__lseeki64(fd
, offset
, whence
);
1306 /*********************************************************************
1307 * _lock_file (MSVCRT.@)
1309 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1311 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1312 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1314 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1317 /*********************************************************************
1318 * _unlock_file (MSVCRT.@)
1320 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1322 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1323 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1325 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1328 /*********************************************************************
1329 * _locking (MSVCRT.@)
1331 * This is untested; the underlying LockFile doesn't work yet.
1333 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1335 ioinfo
*info
= get_ioinfo(fd
);
1339 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1340 if (info
->handle
== INVALID_HANDLE_VALUE
)
1342 release_ioinfo(info
);
1346 if (mode
< 0 || mode
> 4)
1348 release_ioinfo(info
);
1349 *MSVCRT__errno() = MSVCRT_EINVAL
;
1353 TRACE(":fd (%d) by 0x%08x mode %s\n",
1354 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1355 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1356 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1357 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1358 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1361 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1363 release_ioinfo(info
);
1364 FIXME ("Seek failed\n");
1365 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1368 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1371 ret
= 1; /* just to satisfy gcc */
1374 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1379 else if (mode
== MSVCRT__LK_UNLCK
)
1380 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1382 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1383 /* FIXME - what about error settings? */
1384 release_ioinfo(info
);
1385 return ret
? 0 : -1;
1388 /*********************************************************************
1389 * _fseeki64 (MSVCRT.@)
1391 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1395 MSVCRT__lock_file(file
);
1396 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1397 MSVCRT__unlock_file(file
);
1402 /*********************************************************************
1403 * _fseeki64_nolock (MSVCRT.@)
1405 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1409 if(whence
== MSVCRT_SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1410 whence
= MSVCRT_SEEK_SET
;
1411 offset
+= MSVCRT__ftelli64_nolock(file
);
1414 /* Flush output if needed */
1415 msvcrt_flush_buffer(file
);
1416 /* Reset direction of i/o */
1417 if(file
->_flag
& MSVCRT__IORW
) {
1418 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1420 /* Clear end of file flag */
1421 file
->_flag
&= ~MSVCRT__IOEOF
;
1422 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1427 /*********************************************************************
1430 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1432 return MSVCRT__fseeki64( file
, offset
, whence
);
1435 /*********************************************************************
1436 * _fseek_nolock (MSVCRT.@)
1438 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1440 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1443 /*********************************************************************
1444 * _chsize_s (MSVCRT.@)
1446 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1452 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1454 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1457 info
= get_ioinfo(fd
);
1458 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1460 /* save the current file pointer */
1461 cur
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1464 pos
= MSVCRT__lseeki64(fd
, size
, MSVCRT_SEEK_SET
);
1467 ret
= SetEndOfFile(info
->handle
);
1468 if (!ret
) msvcrt_set_errno(GetLastError());
1471 /* restore the file pointer */
1472 MSVCRT__lseeki64(fd
, cur
, MSVCRT_SEEK_SET
);
1476 release_ioinfo(info
);
1477 return ret
? 0 : *MSVCRT__errno();
1480 /*********************************************************************
1481 * _chsize (MSVCRT.@)
1483 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1485 /* _chsize_s returns errno on failure but _chsize should return -1 */
1486 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1489 /*********************************************************************
1490 * clearerr (MSVCRT.@)
1492 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1494 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1496 MSVCRT__lock_file(file
);
1497 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1498 MSVCRT__unlock_file(file
);
1501 /*********************************************************************
1502 * clearerr_s (MSVCRT.@)
1504 int CDECL
MSVCRT_clearerr_s(MSVCRT_FILE
* file
)
1506 TRACE(":file (%p)\n",file
);
1508 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
1510 MSVCRT__lock_file(file
);
1511 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1512 MSVCRT__unlock_file(file
);
1516 /*********************************************************************
1519 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1521 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1523 MSVCRT__lock_file(file
);
1524 MSVCRT__fseek_nolock(file
, 0L, MSVCRT_SEEK_SET
);
1525 MSVCRT_clearerr(file
);
1526 MSVCRT__unlock_file(file
);
1529 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1531 int plus
= strchrW(mode
, '+') != NULL
;
1533 TRACE("%s\n", debugstr_w(mode
));
1535 while(*mode
== ' ') mode
++;
1540 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1541 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1544 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1545 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1548 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1549 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1552 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1556 *stream_flags
|= MSVCRT__commode
;
1558 while (*mode
&& *mode
!=',')
1562 *open_flags
|= MSVCRT__O_BINARY
;
1563 *open_flags
&= ~MSVCRT__O_TEXT
;
1566 *open_flags
|= MSVCRT__O_TEXT
;
1567 *open_flags
&= ~MSVCRT__O_BINARY
;
1570 *open_flags
|= MSVCRT__O_TEMPORARY
;
1573 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1576 *stream_flags
|= MSVCRT__IOCOMMIT
;
1579 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1582 *open_flags
|= MSVCRT__O_NOINHERIT
;
1591 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1594 ERR("incorrect mode flag: %c\n", mode
[-1]);
1600 static const WCHAR ccs
[] = {'c','c','s'};
1601 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1602 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1603 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1606 while(*mode
== ' ') mode
++;
1607 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, ARRAY_SIZE(ccs
))))
1609 mode
+= ARRAY_SIZE(ccs
);
1610 while(*mode
== ' ') mode
++;
1611 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1614 while(*mode
== ' ') mode
++;
1616 if(!strncmpiW(utf8
, mode
, ARRAY_SIZE(utf8
)))
1618 *open_flags
|= MSVCRT__O_U8TEXT
;
1619 mode
+= ARRAY_SIZE(utf8
);
1621 else if(!strncmpiW(utf16le
, mode
, ARRAY_SIZE(utf16le
)))
1623 *open_flags
|= MSVCRT__O_U16TEXT
;
1624 mode
+= ARRAY_SIZE(utf16le
);
1626 else if(!strncmpiW(unicode
, mode
, ARRAY_SIZE(unicode
)))
1628 *open_flags
|= MSVCRT__O_WTEXT
;
1629 mode
+= ARRAY_SIZE(unicode
);
1633 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1637 while(*mode
== ' ') mode
++;
1640 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1645 /*********************************************************************
1646 * _fdopen (MSVCRT.@)
1648 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1651 MSVCRT_wchar_t
*modeW
= NULL
;
1653 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1655 ret
= MSVCRT__wfdopen(fd
, modeW
);
1661 /*********************************************************************
1662 * _wfdopen (MSVCRT.@)
1664 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1666 int open_flags
, stream_flags
;
1669 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1672 if (!(file
= msvcrt_alloc_fp()))
1674 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1679 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1685 /*********************************************************************
1686 * _filelength (MSVCRT.@)
1688 LONG CDECL
MSVCRT__filelength(int fd
)
1690 LONG curPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
1693 LONG endPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_END
);
1696 if (endPos
!= curPos
)
1697 MSVCRT__lseek(fd
, curPos
, MSVCRT_SEEK_SET
);
1704 /*********************************************************************
1705 * _filelengthi64 (MSVCRT.@)
1707 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1709 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1712 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_END
);
1715 if (endPos
!= curPos
)
1716 MSVCRT__lseeki64(fd
, curPos
, MSVCRT_SEEK_SET
);
1723 /*********************************************************************
1724 * _fileno (MSVCRT.@)
1726 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1728 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1732 /*********************************************************************
1733 * _fstat64 (MSVCRT.@)
1735 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1737 ioinfo
*info
= get_ioinfo(fd
);
1740 BY_HANDLE_FILE_INFORMATION hfi
;
1742 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1743 if (info
->handle
== INVALID_HANDLE_VALUE
)
1745 release_ioinfo(info
);
1751 WARN(":failed-NULL buf\n");
1752 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1753 release_ioinfo(info
);
1757 memset(&hfi
, 0, sizeof(hfi
));
1758 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1759 type
= GetFileType(info
->handle
);
1760 if (type
== FILE_TYPE_PIPE
)
1762 buf
->st_dev
= buf
->st_rdev
= fd
;
1763 buf
->st_mode
= MSVCRT__S_IFIFO
;
1766 else if (type
== FILE_TYPE_CHAR
)
1768 buf
->st_dev
= buf
->st_rdev
= fd
;
1769 buf
->st_mode
= MSVCRT__S_IFCHR
;
1772 else /* FILE_TYPE_DISK etc. */
1774 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1776 WARN(":failed-last error (%d)\n",GetLastError());
1777 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1778 release_ioinfo(info
);
1781 buf
->st_mode
= MSVCRT__S_IFREG
| 0444;
1782 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1783 buf
->st_mode
|= 0222;
1784 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1785 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1787 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1788 buf
->st_mtime
= buf
->st_ctime
= dw
;
1789 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1791 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1793 release_ioinfo(info
);
1797 /*********************************************************************
1798 * _fstati64 (MSVCRT.@)
1800 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1803 struct MSVCRT__stat64 buf64
;
1805 ret
= MSVCRT__fstat64(fd
, &buf64
);
1807 msvcrt_stat64_to_stati64(&buf64
, buf
);
1811 /*********************************************************************
1814 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1816 struct MSVCRT__stat64 buf64
;
1818 ret
= MSVCRT__fstat64(fd
, &buf64
);
1820 msvcrt_stat64_to_stat(&buf64
, buf
);
1824 /*********************************************************************
1825 * _fstat32 (MSVCR80.@)
1827 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1830 struct MSVCRT__stat64 buf64
;
1832 ret
= MSVCRT__fstat64(fd
, &buf64
);
1834 msvcrt_stat64_to_stat32(&buf64
, buf
);
1838 /*********************************************************************
1839 * _fstat32i64 (MSVCR80.@)
1841 int CDECL
MSVCRT__fstat32i64(int fd
, struct MSVCRT__stat32i64
* buf
)
1844 struct MSVCRT__stat64 buf64
;
1846 ret
= MSVCRT__fstat64(fd
, &buf64
);
1848 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1852 /*********************************************************************
1853 * _fstat64i32 (MSVCR80.@)
1855 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1858 struct MSVCRT__stat64 buf64
;
1860 ret
= MSVCRT__fstat64(fd
, &buf64
);
1862 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1866 /*********************************************************************
1867 * _futime64 (MSVCRT.@)
1869 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1871 ioinfo
*info
= get_ioinfo(fd
);
1876 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1881 time_to_filetime( t
->actime
, &at
);
1882 time_to_filetime( t
->modtime
, &wt
);
1885 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1887 release_ioinfo(info
);
1888 msvcrt_set_errno(GetLastError());
1891 release_ioinfo(info
);
1895 /*********************************************************************
1896 * _futime32 (MSVCRT.@)
1898 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1902 struct MSVCRT___utimbuf64 t64
;
1903 t64
.actime
= t
->actime
;
1904 t64
.modtime
= t
->modtime
;
1905 return _futime64( fd
, &t64
);
1908 return _futime64( fd
, NULL
);
1911 /*********************************************************************
1912 * _futime (MSVCRT.@)
1915 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1917 return _futime64( fd
, t
);
1920 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1922 return _futime32( fd
, t
);
1926 /*********************************************************************
1927 * _get_osfhandle (MSVCRT.@)
1929 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1931 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1932 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1934 if(hand
== INVALID_HANDLE_VALUE
)
1935 *MSVCRT__errno() = MSVCRT_EBADF
;
1936 return (MSVCRT_intptr_t
)hand
;
1939 /*********************************************************************
1940 * _mktemp_s (MSVCRT.@)
1942 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1946 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1947 return MSVCRT_EINVAL
;
1949 for(len
=0; len
<size
; len
++)
1952 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1955 return MSVCRT_EINVAL
;
1958 for(xno
=1; xno
<=6; xno
++)
1959 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1960 return MSVCRT_EINVAL
;
1962 id
= GetCurrentProcessId();
1963 for(xno
=1; xno
<6; xno
++) {
1964 pattern
[len
-xno
] = id
%10 + '0';
1968 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1969 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1974 *MSVCRT__errno() = MSVCRT_EEXIST
;
1975 return MSVCRT_EEXIST
;
1978 /*********************************************************************
1979 * _mktemp (MSVCRT.@)
1981 char * CDECL
MSVCRT__mktemp(char *pattern
)
1984 char *retVal
= pattern
;
1992 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1996 id
= GetCurrentProcessId();
2000 int tempNum
= id
/ 10;
2001 *pattern
-- = id
- (tempNum
* 10) + '0';
2007 *pattern
= letter
++;
2008 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2010 } while(letter
<= 'z');
2014 /*********************************************************************
2015 * _wmktemp_s (MSVCRT.@)
2017 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
2021 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2022 return MSVCRT_EINVAL
;
2024 for(len
=0; len
<size
; len
++)
2027 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2030 return MSVCRT_EINVAL
;
2033 for(xno
=1; xno
<=6; xno
++)
2034 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2035 return MSVCRT_EINVAL
;
2037 id
= GetCurrentProcessId();
2038 for(xno
=1; xno
<6; xno
++) {
2039 pattern
[len
-xno
] = id
%10 + '0';
2043 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2044 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2049 *MSVCRT__errno() = MSVCRT_EEXIST
;
2050 return MSVCRT_EEXIST
;
2053 /*********************************************************************
2054 * _wmktemp (MSVCRT.@)
2056 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
2059 MSVCRT_wchar_t
*retVal
= pattern
;
2061 MSVCRT_wchar_t letter
= 'a';
2067 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2071 id
= GetCurrentProcessId();
2075 int tempNum
= id
/ 10;
2076 *pattern
-- = id
- (tempNum
* 10) + '0';
2082 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2084 *pattern
= letter
++;
2085 } while(letter
!= '|');
2089 static unsigned split_oflags(unsigned oflags
)
2092 unsigned unsupp
; /* until we support everything */
2094 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2095 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2096 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2097 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2098 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2099 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2100 else if (*MSVCRT___p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2101 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2102 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2104 if ((unsupp
= oflags
& ~(
2105 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2106 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2107 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2108 MSVCRT__O_NOINHERIT
|
2109 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2110 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2112 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2117 /*********************************************************************
2120 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2123 SECURITY_ATTRIBUTES sa
;
2124 HANDLE readHandle
, writeHandle
;
2128 *MSVCRT__errno() = MSVCRT_EINVAL
;
2132 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2133 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2134 sa
.lpSecurityDescriptor
= NULL
;
2135 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2137 unsigned int wxflags
= split_oflags(textmode
);
2140 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2144 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2152 MSVCRT__close(pfds
[0]);
2153 CloseHandle(writeHandle
);
2154 *MSVCRT__errno() = MSVCRT_EMFILE
;
2159 CloseHandle(readHandle
);
2160 CloseHandle(writeHandle
);
2161 *MSVCRT__errno() = MSVCRT_EMFILE
;
2165 msvcrt_set_errno(GetLastError());
2170 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2172 char bom
[sizeof(utf8_bom
)];
2175 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2177 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2180 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2181 oflags
|= MSVCRT__O_U8TEXT
;
2182 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2184 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2185 oflags
|= MSVCRT__O_U16TEXT
;
2187 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2193 /*********************************************************************
2194 * _wsopen_dispatch (UCRTBASE.@)
2196 int CDECL
MSVCRT__wsopen_dispatch( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
,
2197 int *fd
, int secure
)
2199 DWORD access
= 0, creation
= 0, attrib
;
2200 SECURITY_ATTRIBUTES sa
;
2201 DWORD sharing
, type
;
2205 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2206 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2208 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2211 wxflag
= split_oflags(oflags
);
2212 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2214 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2215 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2216 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2219 if (oflags
& MSVCRT__O_CREAT
)
2221 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))))
2222 return MSVCRT_EINVAL
;
2224 if (oflags
& MSVCRT__O_EXCL
)
2225 creation
= CREATE_NEW
;
2226 else if (oflags
& MSVCRT__O_TRUNC
)
2227 creation
= CREATE_ALWAYS
;
2229 creation
= OPEN_ALWAYS
;
2231 else /* no MSVCRT__O_CREAT */
2233 if (oflags
& MSVCRT__O_TRUNC
)
2234 creation
= TRUNCATE_EXISTING
;
2236 creation
= OPEN_EXISTING
;
2241 case MSVCRT__SH_DENYRW
:
2244 case MSVCRT__SH_DENYWR
:
2245 sharing
= FILE_SHARE_READ
;
2247 case MSVCRT__SH_DENYRD
:
2248 sharing
= FILE_SHARE_WRITE
;
2250 case MSVCRT__SH_DENYNO
:
2251 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2254 ERR( "Unhandled shflags 0x%x\n", shflags
);
2255 return MSVCRT_EINVAL
;
2258 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2259 attrib
= FILE_ATTRIBUTE_READONLY
;
2261 attrib
= FILE_ATTRIBUTE_NORMAL
;
2263 if (oflags
& MSVCRT__O_TEMPORARY
)
2265 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2267 sharing
|= FILE_SHARE_DELETE
;
2270 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2271 sa
.lpSecurityDescriptor
= NULL
;
2272 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2274 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2275 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2276 && !(access
&GENERIC_READ
))
2278 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2279 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2280 if (hand
!= INVALID_HANDLE_VALUE
)
2282 oflags
= check_bom(hand
, oflags
, FALSE
);
2286 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2289 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2290 if (hand
== INVALID_HANDLE_VALUE
) {
2291 WARN(":failed-last error (%d)\n",GetLastError());
2292 msvcrt_set_errno(GetLastError());
2293 return *MSVCRT__errno();
2296 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2298 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2299 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2300 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2302 if (oflags
& MSVCRT__O_U8TEXT
)
2304 DWORD written
= 0, tmp
;
2306 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2307 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2309 if (written
!= sizeof(utf8_bom
)) {
2310 WARN("error writing BOM\n");
2312 msvcrt_set_errno(GetLastError());
2313 return *MSVCRT__errno();
2318 DWORD written
= 0, tmp
;
2320 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2321 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2323 if (written
!= sizeof(utf16_bom
))
2325 WARN("error writing BOM\n");
2327 msvcrt_set_errno(GetLastError());
2328 return *MSVCRT__errno();
2332 else if (access
& GENERIC_READ
)
2333 oflags
= check_bom(hand
, oflags
, TRUE
);
2336 type
= GetFileType(hand
);
2337 if (type
== FILE_TYPE_CHAR
)
2339 else if (type
== FILE_TYPE_PIPE
)
2342 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2344 return *MSVCRT__errno();
2346 if (oflags
& MSVCRT__O_WTEXT
)
2347 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2348 else if (oflags
& MSVCRT__O_U16TEXT
)
2349 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2350 else if (oflags
& MSVCRT__O_U8TEXT
)
2351 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2353 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2358 /*********************************************************************
2359 * _wsopen_s (MSVCRT.@)
2361 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2363 return MSVCRT__wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2366 /*********************************************************************
2367 * _wsopen (MSVCRT.@)
2369 int WINAPIV
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2374 if (oflags
& MSVCRT__O_CREAT
)
2378 __ms_va_start(ap
, shflags
);
2379 pmode
= va_arg(ap
, int);
2385 return MSVCRT__wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2389 /*********************************************************************
2390 * _sopen_dispatch (UCRTBASE.@)
2392 int CDECL
MSVCRT__sopen_dispatch( const char *path
, int oflags
, int shflags
,
2393 int pmode
, int *fd
, int secure
)
2395 MSVCRT_wchar_t
*pathW
;
2398 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2399 return MSVCRT_EINVAL
;
2401 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2402 return MSVCRT_EINVAL
;
2404 ret
= MSVCRT__wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2409 /*********************************************************************
2410 * _sopen_s (MSVCRT.@)
2412 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2414 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2417 /*********************************************************************
2420 int WINAPIV
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2425 if (oflags
& MSVCRT__O_CREAT
)
2429 __ms_va_start(ap
, shflags
);
2430 pmode
= va_arg(ap
, int);
2436 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2439 /*********************************************************************
2442 int WINAPIV
MSVCRT__open( const char *path
, int flags
, ... )
2446 if (flags
& MSVCRT__O_CREAT
)
2449 __ms_va_start(ap
, flags
);
2450 pmode
= va_arg(ap
, int);
2452 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2455 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2458 /*********************************************************************
2461 int WINAPIV
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2465 if (flags
& MSVCRT__O_CREAT
)
2468 __ms_va_start(ap
, flags
);
2469 pmode
= va_arg(ap
, int);
2471 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2474 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2477 /*********************************************************************
2480 int CDECL
MSVCRT__creat(const char *path
, int pmode
)
2482 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2483 return MSVCRT__open(path
, flags
, pmode
);
2486 /*********************************************************************
2487 * _wcreat (MSVCRT.@)
2489 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int pmode
)
2491 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2492 return MSVCRT__wopen(path
, flags
, pmode
);
2495 /*********************************************************************
2496 * _open_osfhandle (MSVCRT.@)
2498 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2503 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2504 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2505 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2506 * text - it never sets MSVCRT__O_BINARY.
2508 /* don't let split_oflags() decide the mode if no mode is passed */
2509 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2510 oflags
|= MSVCRT__O_BINARY
;
2512 flags
= GetFileType((HANDLE
)handle
);
2513 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2515 msvcrt_set_errno(GetLastError());
2519 if (flags
== FILE_TYPE_CHAR
)
2521 else if (flags
== FILE_TYPE_PIPE
)
2525 flags
|= split_oflags(oflags
);
2527 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2528 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2532 /*********************************************************************
2535 int CDECL
MSVCRT__rmtmp(void)
2537 int num_removed
= 0, i
;
2541 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2542 file
= msvcrt_get_file(i
);
2544 if (file
->_tmpfname
)
2546 MSVCRT_fclose(file
);
2553 TRACE(":removed (%d) temp files\n",num_removed
);
2557 static inline int get_utf8_char_len(char ch
)
2559 if((ch
&0xf8) == 0xf0)
2561 else if((ch
&0xf0) == 0xe0)
2563 else if((ch
&0xe0) == 0xc0)
2568 /*********************************************************************
2569 * (internal) read_utf8
2571 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2573 HANDLE hand
= fdinfo
->handle
;
2574 char min_buf
[4], *readbuf
, lookahead
;
2575 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2577 /* make the buffer big enough to hold at least one character */
2578 /* read bytes have to fit to output and lookahead buffers */
2580 readbuf_size
= count
< 4 ? 4 : count
;
2581 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2586 if(fdinfo
->lookahead
[0] != '\n') {
2587 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2588 fdinfo
->lookahead
[0] = '\n';
2590 if(fdinfo
->lookahead
[1] != '\n') {
2591 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2592 fdinfo
->lookahead
[1] = '\n';
2594 if(fdinfo
->lookahead
[2] != '\n') {
2595 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2596 fdinfo
->lookahead
[2] = '\n';
2601 /* NOTE: this case is broken in native dll, reading
2602 * sometimes fails when small buffer is passed
2605 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2606 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2607 fdinfo
->wxflag
|= WX_ATEOF
;
2610 msvcrt_set_errno(GetLastError());
2613 }else if(!num_read
) {
2614 fdinfo
->wxflag
|= WX_ATEOF
;
2620 char_len
= get_utf8_char_len(readbuf
[0]);
2622 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2626 if(readbuf
[0] == '\n')
2627 fdinfo
->wxflag
|= WX_READNL
;
2629 fdinfo
->wxflag
&= ~WX_READNL
;
2631 if(readbuf
[0] == 0x1a) {
2632 fdinfo
->wxflag
|= WX_ATEOF
;
2636 if(readbuf
[0] == '\r') {
2637 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2639 else if(lookahead
== '\n')
2643 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2644 fdinfo
->lookahead
[0] = lookahead
;
2646 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2651 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2652 msvcrt_set_errno(GetLastError());
2659 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2662 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2663 fdinfo
->wxflag
|= WX_ATEOF
;
2664 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2667 msvcrt_set_errno(GetLastError());
2668 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2671 }else if(!pos
&& !num_read
) {
2672 fdinfo
->wxflag
|= WX_ATEOF
;
2673 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2678 if(readbuf
[0] == '\n')
2679 fdinfo
->wxflag
|= WX_READNL
;
2681 fdinfo
->wxflag
&= ~WX_READNL
;
2683 /* Find first byte of last character (may be incomplete) */
2684 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2685 if((readbuf
[i
]&0xc0) != 0x80)
2687 char_len
= get_utf8_char_len(readbuf
[i
]);
2688 if(char_len
+i
<= pos
)
2691 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2693 fdinfo
->lookahead
[0] = readbuf
[i
];
2695 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2697 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2699 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2703 for(i
=0, j
=0; i
<pos
; i
++) {
2704 if(readbuf
[i
] == 0x1a) {
2705 fdinfo
->wxflag
|= WX_ATEOF
;
2709 /* strip '\r' if followed by '\n' */
2710 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2711 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2712 readbuf
[j
++] = '\r';
2713 }else if(lookahead
== '\n' && j
==0) {
2714 readbuf
[j
++] = '\n';
2716 if(lookahead
!= '\n')
2717 readbuf
[j
++] = '\r';
2719 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2720 fdinfo
->lookahead
[0] = lookahead
;
2722 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2724 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2725 readbuf
[j
++] = readbuf
[i
];
2730 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2731 msvcrt_set_errno(GetLastError());
2732 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2736 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2740 /*********************************************************************
2743 * When reading \r as last character in text mode, read() positions
2744 * the file pointer on the \r character while getc() goes on to
2747 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2749 DWORD num_read
, utf16
;
2750 char *bufstart
= buf
;
2755 if (fdinfo
->wxflag
& WX_ATEOF
) {
2756 TRACE("already at EOF, returning 0\n");
2759 /* Don't trace small reads, it gets *very* annoying */
2761 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2762 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2764 *MSVCRT__errno() = MSVCRT_EBADF
;
2768 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2769 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2771 *MSVCRT__errno() = MSVCRT_EINVAL
;
2775 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2776 return read_utf8(fdinfo
, buf
, count
);
2778 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2780 if (fdinfo
->lookahead
[0] != '\n')
2782 bufstart
[0] = fdinfo
->lookahead
[0];
2783 fdinfo
->lookahead
[0] = '\n';
2787 bufstart
[1] = fdinfo
->lookahead
[1];
2788 fdinfo
->lookahead
[1] = '\n';
2791 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2792 num_read
+= 1+utf16
;
2797 if(utf16
&& (num_read
&1))
2799 /* msvcr90 uses uninitialized value from the buffer in this case */
2800 /* msvcrt ignores additional data */
2801 ERR("got odd number of bytes in UTF16 mode\n");
2805 if (count
!= 0 && num_read
== 0)
2807 fdinfo
->wxflag
|= WX_ATEOF
;
2808 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2810 else if (fdinfo
->wxflag
& WX_TEXT
)
2814 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2815 fdinfo
->wxflag
|= WX_READNL
;
2817 fdinfo
->wxflag
&= ~WX_READNL
;
2819 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2821 /* in text mode, a ctrl-z signals EOF */
2822 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2824 fdinfo
->wxflag
|= WX_ATEOF
;
2825 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2829 /* in text mode, strip \r if followed by \n */
2830 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2835 lookahead
[1] = '\n';
2836 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2838 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2840 bufstart
[j
++] = '\n';
2841 if(utf16
) bufstart
[j
++] = 0;
2845 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2847 bufstart
[j
++] = '\r';
2848 if(utf16
) bufstart
[j
++] = 0;
2851 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2853 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2855 bufstart
[j
++] = '\n';
2856 if (utf16
) bufstart
[j
++] = 0;
2860 fdinfo
->lookahead
[0] = lookahead
[0];
2861 fdinfo
->lookahead
[1] = lookahead
[1];
2865 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2870 bufstart
[j
++] = '\r';
2871 if(utf16
) bufstart
[j
++] = 0;
2874 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2875 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2877 bufstart
[j
++] = bufstart
[i
];
2878 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2886 if (GetLastError() == ERROR_BROKEN_PIPE
)
2888 TRACE(":end-of-pipe\n");
2889 fdinfo
->wxflag
|= WX_ATEOF
;
2894 TRACE(":failed-last error (%d)\n",GetLastError());
2895 msvcrt_set_errno(GetLastError());
2901 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2905 /*********************************************************************
2908 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2913 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
2914 *MSVCRT__errno() = MSVCRT_EBADF
;
2918 info
= get_ioinfo(fd
);
2919 num_read
= read_i(fd
, info
, buf
, count
);
2920 release_ioinfo(info
);
2924 /*********************************************************************
2925 * _setmode (MSVCRT.@)
2927 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2929 ioinfo
*info
= get_ioinfo(fd
);
2930 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2931 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2932 ret
= MSVCRT__O_WTEXT
;
2934 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2935 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2936 *MSVCRT__errno() = MSVCRT_EINVAL
;
2937 release_ioinfo(info
);
2941 if(info
== &MSVCRT___badioinfo
) {
2942 *MSVCRT__errno() = MSVCRT_EBADF
;
2946 if(mode
== MSVCRT__O_BINARY
) {
2947 info
->wxflag
&= ~WX_TEXT
;
2948 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2949 release_ioinfo(info
);
2953 info
->wxflag
|= WX_TEXT
;
2954 if(mode
== MSVCRT__O_TEXT
)
2955 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2956 else if(mode
== MSVCRT__O_U8TEXT
)
2957 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2959 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2961 release_ioinfo(info
);
2965 /*********************************************************************
2966 * _stat64 (MSVCRT.@)
2968 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2971 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2972 unsigned short mode
= ALL_S_IREAD
;
2975 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2977 plen
= strlen(path
);
2978 while (plen
&& path
[plen
-1]==' ')
2981 if (plen
==2 && path
[1]==':')
2983 *MSVCRT__errno() = MSVCRT_ENOENT
;
2988 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2990 *MSVCRT__errno() = MSVCRT_ENOENT
;
2995 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2997 TRACE("failed (%d)\n",GetLastError());
2998 *MSVCRT__errno() = MSVCRT_ENOENT
;
3002 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3004 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3005 Bon 011120: This FIXME seems incorrect
3006 Also a letter as first char isn't enough to be classified
3009 if (isalpha(*path
)&& (*(path
+1)==':'))
3010 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
3012 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3014 /* Dir, or regular file? */
3015 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3016 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3019 mode
|= MSVCRT__S_IFREG
;
3021 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3023 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
3024 (tolower(path
[plen
-3]) << 16);
3025 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3026 mode
|= ALL_S_IEXEC
;
3030 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3031 mode
|= ALL_S_IWRITE
;
3033 buf
->st_mode
= mode
;
3035 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3036 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3038 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3039 buf
->st_mtime
= buf
->st_ctime
= dw
;
3040 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3041 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3042 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3046 /*********************************************************************
3047 * _stati64 (MSVCRT.@)
3049 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
3052 struct MSVCRT__stat64 buf64
;
3054 ret
= MSVCRT_stat64(path
, &buf64
);
3056 msvcrt_stat64_to_stati64(&buf64
, buf
);
3060 /*********************************************************************
3063 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
3066 struct MSVCRT__stat64 buf64
;
3068 ret
= MSVCRT_stat64( path
, &buf64
);
3070 msvcrt_stat64_to_stat(&buf64
, buf
);
3074 #if _MSVCR_VER >= 80
3076 /*********************************************************************
3077 * _stat32 (MSVCR80.@)
3079 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
3082 struct MSVCRT__stat64 buf64
;
3084 ret
= MSVCRT_stat64(path
, &buf64
);
3086 msvcrt_stat64_to_stat32(&buf64
, buf
);
3090 /*********************************************************************
3091 * _stat32i64 (MSVCR80.@)
3093 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
3096 struct MSVCRT__stat64 buf64
;
3098 ret
= MSVCRT_stat64(path
, &buf64
);
3100 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3104 /*********************************************************************
3105 * _stat64i32 (MSVCR80.@)
3107 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
3110 struct MSVCRT__stat64 buf64
;
3112 ret
= MSVCRT_stat64(path
, &buf64
);
3114 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3118 #endif /* _MSVCR_VER >= 80 */
3120 /*********************************************************************
3121 * _wstat64 (MSVCRT.@)
3123 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
3126 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3127 unsigned short mode
= ALL_S_IREAD
;
3130 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3132 plen
= strlenW(path
);
3133 while (plen
&& path
[plen
-1]==' ')
3136 if (plen
==2 && path
[1]==':')
3138 *MSVCRT__errno() = MSVCRT_ENOENT
;
3143 if (plen
>=2 && path
[plen
-2]!=':' && (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3145 *MSVCRT__errno() = MSVCRT_ENOENT
;
3150 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3152 TRACE("failed (%d)\n",GetLastError());
3153 *MSVCRT__errno() = MSVCRT_ENOENT
;
3157 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3159 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3160 if (MSVCRT_iswalpha(*path
))
3161 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3163 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3165 /* Dir, or regular file? */
3166 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3167 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3170 mode
|= MSVCRT__S_IFREG
;
3172 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3174 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3175 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3176 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3177 mode
|= ALL_S_IEXEC
;
3181 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3182 mode
|= ALL_S_IWRITE
;
3184 buf
->st_mode
= mode
;
3186 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3187 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3189 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3190 buf
->st_mtime
= buf
->st_ctime
= dw
;
3191 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3192 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3193 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3197 /*********************************************************************
3198 * _wstati64 (MSVCRT.@)
3200 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3203 struct MSVCRT__stat64 buf64
;
3205 ret
= MSVCRT__wstat64(path
, &buf64
);
3207 msvcrt_stat64_to_stati64(&buf64
, buf
);
3211 /*********************************************************************
3214 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3217 struct MSVCRT__stat64 buf64
;
3219 ret
= MSVCRT__wstat64( path
, &buf64
);
3220 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3224 #if _MSVCR_VER >= 80
3226 /*********************************************************************
3227 * _wstat32 (MSVCR80.@)
3229 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3232 struct MSVCRT__stat64 buf64
;
3234 ret
= MSVCRT__wstat64(path
, &buf64
);
3236 msvcrt_stat64_to_stat32(&buf64
, buf
);
3240 /*********************************************************************
3241 * _wstat32i64 (MSVCR80.@)
3243 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3246 struct MSVCRT__stat64 buf64
;
3248 ret
= MSVCRT__wstat64(path
, &buf64
);
3250 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3254 /*********************************************************************
3255 * _wstat64i32 (MSVCR80.@)
3257 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3260 struct MSVCRT__stat64 buf64
;
3262 ret
= MSVCRT__wstat64(path
, &buf64
);
3264 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3268 #endif /* _MSVCR_VER >= 80 */
3270 /*********************************************************************
3273 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3275 return MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
3278 /*********************************************************************
3279 * _telli64 (MSVCRT.@)
3281 __int64 CDECL
_telli64(int fd
)
3283 return MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
3286 /*********************************************************************
3287 * _tempnam (MSVCRT.@)
3289 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3291 char tmpbuf
[MAX_PATH
];
3292 const char *tmp_dir
= MSVCRT_getenv("TMP");
3294 if (tmp_dir
) dir
= tmp_dir
;
3296 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3297 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3299 TRACE("got name (%s)\n",tmpbuf
);
3300 DeleteFileA(tmpbuf
);
3301 return MSVCRT__strdup(tmpbuf
);
3303 TRACE("failed (%d)\n",GetLastError());
3307 /*********************************************************************
3308 * _wtempnam (MSVCRT.@)
3310 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3312 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3313 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3314 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3316 if (tmp_dir
) dir
= tmp_dir
;
3318 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3319 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3321 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3322 DeleteFileW(tmpbuf
);
3323 return MSVCRT__wcsdup(tmpbuf
);
3325 TRACE("failed (%d)\n",GetLastError());
3329 /*********************************************************************
3332 int CDECL
MSVCRT__umask(int umask
)
3334 int old_umask
= MSVCRT_umask
;
3335 TRACE("(%d)\n",umask
);
3336 MSVCRT_umask
= umask
;
3340 /*********************************************************************
3341 * _utime64 (MSVCRT.@)
3343 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3345 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3349 int retVal
= _futime64(fd
, t
);
3356 /*********************************************************************
3357 * _utime32 (MSVCRT.@)
3359 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3363 struct MSVCRT___utimbuf64 t64
;
3364 t64
.actime
= t
->actime
;
3365 t64
.modtime
= t
->modtime
;
3366 return _utime64( path
, &t64
);
3369 return _utime64( path
, NULL
);
3372 /*********************************************************************
3376 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3378 return _utime64( path
, t
);
3381 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3383 return _utime32( path
, t
);
3387 /*********************************************************************
3388 * _wutime64 (MSVCRT.@)
3390 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3392 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3396 int retVal
= _futime64(fd
, t
);
3403 /*********************************************************************
3404 * _wutime32 (MSVCRT.@)
3406 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3410 struct MSVCRT___utimbuf64 t64
;
3411 t64
.actime
= t
->actime
;
3412 t64
.modtime
= t
->modtime
;
3413 return _wutime64( path
, &t64
);
3416 return _wutime64( path
, NULL
);
3419 /*********************************************************************
3420 * _wutime (MSVCRT.@)
3423 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3425 return _wutime64( path
, t
);
3428 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3430 return _wutime32( path
, t
);
3434 /*********************************************************************
3437 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3440 ioinfo
*info
= get_ioinfo(fd
);
3441 HANDLE hand
= info
->handle
;
3443 /* Don't trace small writes, it gets *very* annoying */
3446 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3448 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3450 *MSVCRT__errno() = MSVCRT_EBADF
;
3451 release_ioinfo(info
);
3455 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3457 *MSVCRT__errno() = MSVCRT_EINVAL
;
3458 release_ioinfo(info
);
3462 /* If appending, go to EOF */
3463 if (info
->wxflag
& WX_APPEND
)
3464 MSVCRT__lseek(fd
, 0, FILE_END
);
3466 if (!(info
->wxflag
& WX_TEXT
))
3468 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3469 && (num_written
== count
))
3471 release_ioinfo(info
);
3474 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3475 hand
, GetLastError());
3476 msvcrt_set_errno(GetLastError());
3480 unsigned int i
, j
, nr_lf
, size
;
3483 const char *s
= buf
;
3485 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3487 /* find number of \n */
3488 for (nr_lf
=0, i
=0; i
<count
; i
++)
3494 if ((q
= p
= MSVCRT_malloc(size
)))
3496 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3505 FIXME("Malloc failed\n");
3517 else if (info
->exflag
& EF_UTF16
)
3519 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3520 if (s
[i
]=='\n' && s
[i
+1]==0)
3525 if ((q
= p
= MSVCRT_malloc(size
)))
3527 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3529 if (s
[i
]=='\n' && s
[i
+1]==0)
3540 FIXME("Malloc failed\n");
3556 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3557 if (s
[i
]=='\n' && s
[i
+1]==0)
3560 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3562 msvcrt_set_errno(GetLastError());
3564 release_ioinfo(info
);
3568 size
= conv_len
+nr_lf
;
3569 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3571 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3573 if (s
[i
]=='\n' && s
[i
+1]==0)
3581 q
= p
+count
+nr_lf
*2;
3582 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3583 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3587 FIXME("Malloc failed\n");
3594 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3596 release_ioinfo(info
);
3598 if (num_written
!= size
)
3600 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3601 fd
, hand
, GetLastError(), num_written
);
3602 msvcrt_set_errno(GetLastError());
3608 release_ioinfo(info
);
3612 /*********************************************************************
3615 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3619 MSVCRT__lock_file(file
);
3620 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3621 if (len
== sizeof(val
)) {
3622 MSVCRT__unlock_file(file
);
3626 file
->_flag
|= MSVCRT__IOERR
;
3627 MSVCRT__unlock_file(file
);
3631 /*********************************************************************
3634 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3638 MSVCRT__lock_file(file
);
3639 ret
= MSVCRT__fclose_nolock(file
);
3640 MSVCRT__unlock_file(file
);
3645 /*********************************************************************
3646 * _fclose_nolock (MSVCRT.@)
3648 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3652 if(!(file
->_flag
& (MSVCRT__IOREAD
| MSVCRT__IOWRT
| MSVCRT__IORW
)))
3659 MSVCRT_free(file
->_tmpfname
);
3660 file
->_tmpfname
= NULL
;
3661 /* flush stdio buffers */
3662 if(file
->_flag
& MSVCRT__IOWRT
)
3663 MSVCRT__fflush_nolock(file
);
3664 if(file
->_flag
& MSVCRT__IOMYBUF
)
3665 MSVCRT_free(file
->_base
);
3667 r
=MSVCRT__close(file
->_file
);
3670 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3673 /*********************************************************************
3676 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3678 return file
->_flag
& MSVCRT__IOEOF
;
3681 /*********************************************************************
3684 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3686 return file
->_flag
& MSVCRT__IOERR
;
3689 /*********************************************************************
3690 * _filbuf (MSVCRT.@)
3692 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3696 if(file
->_flag
& MSVCRT__IOSTRG
)
3699 /* Allocate buffer if needed */
3700 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3701 msvcrt_alloc_buffer(file
);
3703 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3704 if(file
->_flag
& MSVCRT__IORW
)
3705 file
->_flag
|= MSVCRT__IOREAD
;
3710 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3712 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3713 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3719 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3721 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3727 file
->_ptr
= file
->_base
+1;
3728 c
= *(unsigned char *)file
->_base
;
3733 /*********************************************************************
3736 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3740 MSVCRT__lock_file(file
);
3741 ret
= MSVCRT__fgetc_nolock(file
);
3742 MSVCRT__unlock_file(file
);
3747 /*********************************************************************
3748 * _fgetc_nolock (MSVCRT.@)
3750 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3757 i
= (unsigned char *)file
->_ptr
++;
3760 j
= MSVCRT__filbuf(file
);
3765 /*********************************************************************
3766 * _fgetchar (MSVCRT.@)
3768 int CDECL
MSVCRT__fgetchar(void)
3770 return MSVCRT_fgetc(MSVCRT_stdin
);
3773 /*********************************************************************
3776 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3778 int cc
= MSVCRT_EOF
;
3779 char * buf_start
= s
;
3781 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3782 file
,file
->_file
,s
,size
);
3784 MSVCRT__lock_file(file
);
3786 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3791 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3793 TRACE(":nothing read\n");
3794 MSVCRT__unlock_file(file
);
3797 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3800 TRACE(":got %s\n", debugstr_a(buf_start
));
3801 MSVCRT__unlock_file(file
);
3805 /*********************************************************************
3808 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3812 MSVCRT__lock_file(file
);
3813 ret
= MSVCRT__fgetwc_nolock(file
);
3814 MSVCRT__unlock_file(file
);
3819 /*********************************************************************
3820 * _fgetwc_nolock (MSVCRT.@)
3822 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3827 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3828 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3831 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3832 ch
= MSVCRT__fgetc_nolock(file
);
3833 if(ch
== MSVCRT_EOF
) {
3840 char mbs
[MSVCRT_MB_LEN_MAX
];
3843 ch
= MSVCRT__fgetc_nolock(file
);
3844 if(ch
!= MSVCRT_EOF
) {
3846 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3847 ch
= MSVCRT__fgetc_nolock(file
);
3848 if(ch
!= MSVCRT_EOF
) {
3857 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3864 /*********************************************************************
3867 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3874 MSVCRT__lock_file(file
);
3875 for (j
=0; j
<sizeof(int); j
++) {
3876 k
= MSVCRT__fgetc_nolock(file
);
3877 if (k
== MSVCRT_EOF
) {
3878 file
->_flag
|= MSVCRT__IOEOF
;
3879 MSVCRT__unlock_file(file
);
3885 MSVCRT__unlock_file(file
);
3889 /*********************************************************************
3892 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3894 return MSVCRT_fgetwc(file
);
3897 /*********************************************************************
3898 * _fgetwchar (MSVCRT.@)
3900 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3902 return MSVCRT_fgetwc(MSVCRT_stdin
);
3905 /*********************************************************************
3906 * getwchar (MSVCRT.@)
3908 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3910 return MSVCRT__fgetwchar();
3913 /*********************************************************************
3916 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3918 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3919 MSVCRT_wchar_t
* buf_start
= s
;
3921 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3922 file
,file
->_file
,s
,size
);
3924 MSVCRT__lock_file(file
);
3926 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3931 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3933 TRACE(":nothing read\n");
3934 MSVCRT__unlock_file(file
);
3937 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3940 TRACE(":got %s\n", debugstr_w(buf_start
));
3941 MSVCRT__unlock_file(file
);
3945 /*********************************************************************
3946 * _flsbuf (MSVCRT.@)
3948 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3950 /* Flush output buffer */
3951 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3952 msvcrt_alloc_buffer(file
);
3955 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3956 if(!(file
->_flag
& MSVCRT__IORW
)) {
3957 file
->_flag
|= MSVCRT__IOERR
;
3958 *MSVCRT__errno() = MSVCRT_EBADF
;
3961 file
->_flag
|= MSVCRT__IOWRT
;
3963 if(file
->_flag
& MSVCRT__IOREAD
) {
3964 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3965 file
->_flag
|= MSVCRT__IOERR
;
3969 file
->_ptr
= file
->_base
;
3970 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3973 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3976 if(file
->_cnt
<= 0) {
3977 res
= msvcrt_flush_buffer(file
);
3980 file
->_flag
|= MSVCRT__IOWRT
;
3981 file
->_cnt
=file
->_bufsiz
;
3989 /* set _cnt to 0 for unbuffered FILEs */
3991 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3994 file
->_flag
|= MSVCRT__IOERR
;
3999 /*********************************************************************
4002 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4006 MSVCRT__lock_file(file
);
4007 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
4008 MSVCRT__unlock_file(file
);
4013 /*********************************************************************
4014 * _fwrite_nolock (MSVCRT.@)
4016 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4018 MSVCRT_size_t wrcnt
=size
* nmemb
;
4024 if(file
->_cnt
< 0) {
4025 WARN("negative file->_cnt value in %p\n", file
);
4026 file
->_flag
|= MSVCRT__IOERR
;
4028 } else if(file
->_cnt
) {
4029 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
4030 memcpy(file
->_ptr
, ptr
, pcnt
);
4035 ptr
= (const char*)ptr
+ pcnt
;
4036 } else if((file
->_flag
& MSVCRT__IONBF
)
4037 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4038 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4042 if(file
->_flag
& MSVCRT__IONBF
)
4044 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4045 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4047 bufsiz
= file
->_bufsiz
;
4049 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4051 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
4054 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
4055 file
->_flag
|= MSVCRT__IOERR
;
4060 ptr
= (const char*)ptr
+ pcnt
;
4062 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
4066 ptr
= (const char*)ptr
+ 1;
4070 return written
/ size
;
4073 /*********************************************************************
4076 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4080 MSVCRT__lock_file(file
);
4081 ret
= MSVCRT__fputwc_nolock(wc
, file
);
4082 MSVCRT__unlock_file(file
);
4087 /*********************************************************************
4088 * _fputwc_nolock (MSVCRT.@)
4090 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4092 MSVCRT_wchar_t mwc
=wc
;
4096 fdinfo
= get_ioinfo_nolock(file
->_file
);
4098 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
4099 char buf
[MSVCRT_MB_LEN_MAX
];
4102 char_len
= MSVCRT_wctomb(buf
, mwc
);
4103 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
4107 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4116 /*********************************************************************
4117 * _fputwchar (MSVCRT.@)
4119 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
4121 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
4124 /*********************************************************************
4125 * _wfsopen (MSVCRT.@)
4127 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
4130 int open_flags
, stream_flags
, fd
;
4132 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4134 /* map mode string to open() flags. "man fopen" for possibilities. */
4135 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4139 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4142 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4144 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4151 TRACE(":got (%p)\n",file
);
4152 if (fd
>= 0 && !file
)
4158 /*********************************************************************
4159 * _fsopen (MSVCRT.@)
4161 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4164 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4166 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4167 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4168 *MSVCRT__errno() = MSVCRT_EINVAL
;
4171 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4174 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4175 *MSVCRT__errno() = MSVCRT_EINVAL
;
4179 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4186 /*********************************************************************
4189 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4191 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4194 /*********************************************************************
4195 * fopen_s (MSVCRT.@)
4197 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4198 const char *filename
, const char *mode
)
4200 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4201 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4202 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4204 *pFile
= MSVCRT_fopen(filename
, mode
);
4207 return *MSVCRT__errno();
4211 /*********************************************************************
4212 * _wfopen (MSVCRT.@)
4214 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4216 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4219 /*********************************************************************
4220 * _wfopen_s (MSVCRT.@)
4222 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4223 const MSVCRT_wchar_t
*mode
)
4225 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4226 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4227 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4229 *pFile
= MSVCRT__wfopen(filename
, mode
);
4232 return *MSVCRT__errno();
4236 /*********************************************************************
4239 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4243 MSVCRT__lock_file(file
);
4244 ret
= MSVCRT__fputc_nolock(c
, file
);
4245 MSVCRT__unlock_file(file
);
4250 /*********************************************************************
4251 * _fputc_nolock (MSVCRT.@)
4253 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4262 res
= msvcrt_flush_buffer(file
);
4263 return res
? res
: c
;
4269 res
= MSVCRT__flsbuf(c
, file
);
4274 /*********************************************************************
4275 * _fputchar (MSVCRT.@)
4277 int CDECL
MSVCRT__fputchar(int c
)
4279 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4282 /*********************************************************************
4285 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4289 MSVCRT__lock_file(file
);
4290 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4291 MSVCRT__unlock_file(file
);
4296 /*********************************************************************
4297 * _fread_nolock (MSVCRT.@)
4299 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4301 MSVCRT_size_t rcnt
=size
* nmemb
;
4302 MSVCRT_size_t read
=0;
4303 MSVCRT_size_t pread
=0;
4308 /* first buffered data */
4310 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4311 memcpy(ptr
, file
->_ptr
, pcnt
);
4316 ptr
= (char*)ptr
+ pcnt
;
4317 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4318 if(file
->_flag
& MSVCRT__IORW
) {
4319 file
->_flag
|= MSVCRT__IOREAD
;
4325 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4326 msvcrt_alloc_buffer(file
);
4331 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4332 i
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4333 file
->_ptr
= file
->_base
;
4336 if (i
> rcnt
) i
= rcnt
;
4338 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4339 if (i
> 0 && i
< file
->_cnt
) {
4340 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4341 file
->_flag
&= ~MSVCRT__IOEOF
;
4344 memcpy(ptr
, file
->_ptr
, i
);
4348 } else if (rcnt
> INT_MAX
) {
4349 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4350 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4351 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4353 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4357 ptr
= (char *)ptr
+i
;
4358 /* expose feof condition in the flags
4359 * MFC tests file->_flag for feof, and doesn't call feof())
4361 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4362 file
->_flag
|= MSVCRT__IOEOF
;
4365 file
->_flag
|= MSVCRT__IOERR
;
4375 #if _MSVCR_VER >= 80
4377 /*********************************************************************
4378 * fread_s (MSVCR80.@)
4380 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4381 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4385 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4387 memset(buf
, 0, buf_size
);
4390 if(!elem_size
|| !count
) return 0;
4392 MSVCRT__lock_file(stream
);
4393 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4394 MSVCRT__unlock_file(stream
);
4399 /*********************************************************************
4400 * _fread_nolock_s (MSVCR80.@)
4402 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4403 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4405 size_t bytes_left
, buf_pos
;
4407 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4409 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4411 memset(buf
, 0, buf_size
);
4414 if(!elem_size
|| !count
) return 0;
4415 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4416 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4418 bytes_left
= elem_size
*count
;
4421 if(stream
->_cnt
> 0) {
4422 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4424 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4425 memset(buf
, 0, buf_size
);
4429 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4433 int c
= MSVCRT__filbuf(stream
);
4438 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4439 memset(buf
, 0, buf_size
);
4443 ((char*)buf
)[buf_pos
++] = c
;
4448 return buf_pos
/elem_size
;
4451 #endif /* _MSVCR_VER >= 80 */
4453 /*********************************************************************
4454 * _wfreopen (MSVCRT.@)
4457 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4459 int open_flags
, stream_flags
, fd
;
4461 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4464 if (!file
|| ((fd
= file
->_file
) < 0))
4468 MSVCRT_fclose(file
);
4469 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4471 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4473 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4483 /*********************************************************************
4484 * _wfreopen_s (MSVCRT.@)
4486 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4487 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4489 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4490 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4491 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4492 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4494 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4497 return *MSVCRT__errno();
4501 /*********************************************************************
4502 * freopen (MSVCRT.@)
4505 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4508 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4510 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4511 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4517 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4524 /*********************************************************************
4525 * freopen_s (MSVCRT.@)
4527 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4528 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4530 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4531 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4532 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4533 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4535 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4538 return *MSVCRT__errno();
4542 /*********************************************************************
4543 * fsetpos (MSVCRT.@)
4545 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4549 MSVCRT__lock_file(file
);
4550 msvcrt_flush_buffer(file
);
4552 /* Reset direction of i/o */
4553 if(file
->_flag
& MSVCRT__IORW
) {
4554 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4557 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,MSVCRT_SEEK_SET
) == -1) ? -1 : 0;
4558 MSVCRT__unlock_file(file
);
4562 /*********************************************************************
4563 * _ftelli64 (MSVCRT.@)
4565 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4569 MSVCRT__lock_file(file
);
4570 ret
= MSVCRT__ftelli64_nolock(file
);
4571 MSVCRT__unlock_file(file
);
4576 /*********************************************************************
4577 * _ftelli64_nolock (MSVCRT.@)
4579 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4583 pos
= _telli64(file
->_file
);
4586 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4587 if(file
->_flag
& MSVCRT__IOWRT
) {
4588 pos
+= file
->_ptr
- file
->_base
;
4590 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4593 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4597 } else if(!file
->_cnt
) { /* nothing to do */
4598 } else if(MSVCRT__lseeki64(file
->_file
, 0, MSVCRT_SEEK_END
)==pos
) {
4602 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4603 for(i
=0; i
<file
->_cnt
; i
++)
4604 if(file
->_ptr
[i
] == '\n')
4610 if(MSVCRT__lseeki64(file
->_file
, pos
, MSVCRT_SEEK_SET
) != pos
)
4613 pos
-= file
->_bufsiz
;
4614 pos
+= file
->_ptr
- file
->_base
;
4616 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4617 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4620 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4630 /*********************************************************************
4633 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4635 return MSVCRT__ftelli64(file
);
4638 #if _MSVCR_VER >= 80
4639 /*********************************************************************
4640 * _ftell_nolock (MSVCR80.@)
4642 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4644 return MSVCRT__ftelli64_nolock(file
);
4648 /*********************************************************************
4649 * fgetpos (MSVCRT.@)
4651 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4653 *pos
= MSVCRT__ftelli64(file
);
4659 /*********************************************************************
4662 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4664 MSVCRT_size_t len
= strlen(s
);
4667 MSVCRT__lock_file(file
);
4668 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4669 MSVCRT__unlock_file(file
);
4673 /*********************************************************************
4676 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4678 MSVCRT_size_t i
, len
= strlenW(s
);
4682 MSVCRT__lock_file(file
);
4683 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4684 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4685 MSVCRT__unlock_file(file
);
4689 tmp_buf
= add_std_buffer(file
);
4690 for (i
=0; i
<len
; i
++) {
4691 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4692 if(tmp_buf
) remove_std_buffer(file
);
4693 MSVCRT__unlock_file(file
);
4698 if(tmp_buf
) remove_std_buffer(file
);
4699 MSVCRT__unlock_file(file
);
4703 /*********************************************************************
4704 * getchar (MSVCRT.@)
4706 int CDECL
MSVCRT_getchar(void)
4708 return MSVCRT_fgetc(MSVCRT_stdin
);
4711 /*********************************************************************
4714 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4716 return MSVCRT_fgetc(file
);
4719 /*********************************************************************
4720 * gets_s (MSVCR80.@)
4722 char * CDECL
MSVCRT_gets_s(char *buf
, MSVCRT_size_t len
)
4724 char *buf_start
= buf
;
4727 if (!MSVCRT_CHECK_PMT(buf
!= NULL
)) return NULL
;
4728 if (!MSVCRT_CHECK_PMT(len
!= 0)) return NULL
;
4730 MSVCRT__lock_file(MSVCRT_stdin
);
4731 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
);
4732 len
!= 0 && cc
!= MSVCRT_EOF
&& cc
!= '\n';
4733 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4741 MSVCRT__unlock_file(MSVCRT_stdin
);
4746 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4750 if ((cc
== MSVCRT_EOF
) && (buf_start
== buf
))
4752 TRACE(":nothing read\n");
4757 TRACE("got '%s'\n", buf_start
);
4761 /*********************************************************************
4764 char * CDECL
MSVCRT_gets(char *buf
)
4766 return MSVCRT_gets_s(buf
, -1);
4769 /*********************************************************************
4772 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4775 MSVCRT_wchar_t
* ws
= buf
;
4777 MSVCRT__lock_file(MSVCRT_stdin
);
4778 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4779 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4782 *buf
++ = (MSVCRT_wchar_t
)cc
;
4784 MSVCRT__unlock_file(MSVCRT_stdin
);
4786 if ((cc
== MSVCRT_WEOF
) && (ws
== buf
))
4788 TRACE(":nothing read\n");
4793 TRACE("got %s\n", debugstr_w(ws
));
4797 /*********************************************************************
4800 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4802 return MSVCRT_fputc(c
, file
);
4805 /*********************************************************************
4806 * putchar (MSVCRT.@)
4808 int CDECL
MSVCRT_putchar(int c
)
4810 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4813 /*********************************************************************
4816 int CDECL
MSVCRT_puts(const char *s
)
4818 MSVCRT_size_t len
= strlen(s
);
4821 MSVCRT__lock_file(MSVCRT_stdout
);
4822 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4823 MSVCRT__unlock_file(MSVCRT_stdout
);
4827 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4828 MSVCRT__unlock_file(MSVCRT_stdout
);
4832 /*********************************************************************
4835 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4837 static const MSVCRT_wchar_t nl
= '\n';
4838 MSVCRT_size_t len
= strlenW(s
);
4841 MSVCRT__lock_file(MSVCRT_stdout
);
4842 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4843 MSVCRT__unlock_file(MSVCRT_stdout
);
4847 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4848 MSVCRT__unlock_file(MSVCRT_stdout
);
4852 /*********************************************************************
4855 int CDECL
MSVCRT_remove(const char *path
)
4857 TRACE("(%s)\n",path
);
4858 if (DeleteFileA(path
))
4860 TRACE(":failed (%d)\n",GetLastError());
4861 msvcrt_set_errno(GetLastError());
4865 /*********************************************************************
4866 * _wremove (MSVCRT.@)
4868 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4870 TRACE("(%s)\n",debugstr_w(path
));
4871 if (DeleteFileW(path
))
4873 TRACE(":failed (%d)\n",GetLastError());
4874 msvcrt_set_errno(GetLastError());
4878 /*********************************************************************
4881 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4883 TRACE(":from %s to %s\n",oldpath
,newpath
);
4884 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4886 TRACE(":failed (%d)\n",GetLastError());
4887 msvcrt_set_errno(GetLastError());
4891 /*********************************************************************
4892 * _wrename (MSVCRT.@)
4894 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4896 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4897 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4899 TRACE(":failed (%d)\n",GetLastError());
4900 msvcrt_set_errno(GetLastError());
4904 /*********************************************************************
4905 * setvbuf (MSVCRT.@)
4907 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4909 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4910 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4911 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4913 MSVCRT__lock_file(file
);
4915 MSVCRT__fflush_nolock(file
);
4916 if(file
->_flag
& MSVCRT__IOMYBUF
)
4917 MSVCRT_free(file
->_base
);
4918 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4921 if(mode
== MSVCRT__IONBF
) {
4922 file
->_flag
|= MSVCRT__IONBF
;
4923 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4926 file
->_base
= file
->_ptr
= buf
;
4927 file
->_flag
|= MSVCRT__USERBUF
;
4928 file
->_bufsiz
= size
;
4930 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4933 MSVCRT__unlock_file(file
);
4937 file
->_flag
|= MSVCRT__IOMYBUF
;
4938 file
->_bufsiz
= size
;
4940 MSVCRT__unlock_file(file
);
4944 /*********************************************************************
4947 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4949 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4952 static int tmpnam_helper(char *s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
4958 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
4962 *MSVCRT__errno() = MSVCRT_ERANGE
;
4963 return MSVCRT_ERANGE
;
4968 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4969 if (digits
+1 > size
) {
4971 *MSVCRT__errno() = MSVCRT_ERANGE
;
4972 return MSVCRT_ERANGE
;
4974 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4980 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4981 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4985 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4986 if (digits
+1 > size
) {
4988 *MSVCRT__errno() = MSVCRT_ERANGE
;
4989 return MSVCRT_ERANGE
;
4991 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4994 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4995 GetLastError() == ERROR_FILE_NOT_FOUND
)
5001 int CDECL
MSVCRT_tmpnam_s(char *s
, MSVCRT_size_t size
)
5003 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5006 /*********************************************************************
5009 char * CDECL
MSVCRT_tmpnam(char *s
)
5012 thread_data_t
*data
= msvcrt_get_thread_data();
5014 if(!data
->tmpnam_buffer
)
5015 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
5017 s
= data
->tmpnam_buffer
;
5020 return tmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5023 static int wtmpnam_helper(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
5025 MSVCRT_wchar_t tmpstr
[8];
5026 MSVCRT_wchar_t
*p
= s
;
5029 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
5033 *MSVCRT__errno() = MSVCRT_ERANGE
;
5034 return MSVCRT_ERANGE
;
5039 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5040 if (digits
+1 > size
) {
5042 *MSVCRT__errno() = MSVCRT_ERANGE
;
5043 return MSVCRT_ERANGE
;
5045 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5051 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5052 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5056 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5057 if (digits
+1 > size
) {
5059 *MSVCRT__errno() = MSVCRT_ERANGE
;
5060 return MSVCRT_ERANGE
;
5062 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5065 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5066 GetLastError() == ERROR_FILE_NOT_FOUND
)
5072 /*********************************************************************
5073 * _wtmpnam_s (MSVCRT.@)
5075 int CDECL
MSVCRT__wtmpnam_s(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
)
5077 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5080 /*********************************************************************
5081 * _wtmpnam (MSVCRT.@)
5083 MSVCRT_wchar_t
* CDECL
MSVCRT__wtmpnam(MSVCRT_wchar_t
*s
)
5086 thread_data_t
*data
= msvcrt_get_thread_data();
5088 if(!data
->wtmpnam_buffer
)
5089 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
5091 s
= data
->wtmpnam_buffer
;
5094 return wtmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5097 /*********************************************************************
5098 * tmpfile (MSVCRT.@)
5100 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
5102 char *filename
= MSVCRT__tempnam(",", "t");
5104 MSVCRT_FILE
* file
= NULL
;
5107 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
5108 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
5109 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5111 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
5116 else file
->_tmpfname
= MSVCRT__strdup(filename
);
5119 if(fd
!= -1 && !file
)
5121 MSVCRT_free(filename
);
5126 /*********************************************************************
5127 * tmpfile_s (MSVCRT.@)
5129 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
5131 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
5133 *file
= MSVCRT_tmpfile();
5137 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5139 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
5142 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
5146 MSVCRT__lock_file(file
);
5148 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5149 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
5150 MSVCRT__unlock_file(file
);
5154 for(i
=0; i
<len
; i
++) {
5155 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
5156 MSVCRT__unlock_file(file
);
5161 MSVCRT__unlock_file(file
);
5165 static int vfprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const char *format
,
5166 MSVCRT__locale_t locale
, __ms_va_list valist
)
5168 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5172 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5173 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5175 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5176 memset(args_ctx
, 0, sizeof(args_ctx
));
5177 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5179 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5180 *MSVCRT__errno() = MSVCRT_EINVAL
;
5183 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5186 MSVCRT__lock_file(file
);
5187 tmp_buf
= add_std_buffer(file
);
5188 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5189 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5190 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5191 if(tmp_buf
) remove_std_buffer(file
);
5192 MSVCRT__unlock_file(file
);
5197 static int vfwprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5198 MSVCRT__locale_t locale
, __ms_va_list valist
)
5200 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5204 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5205 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5207 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5208 memset(args_ctx
, 0, sizeof(args_ctx
));
5209 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5211 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5212 *MSVCRT__errno() = MSVCRT_EINVAL
;
5215 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5218 MSVCRT__lock_file(file
);
5219 tmp_buf
= add_std_buffer(file
);
5220 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5221 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5222 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5223 if(tmp_buf
) remove_std_buffer(file
);
5224 MSVCRT__unlock_file(file
);
5229 /*********************************************************************
5230 * _vfprintf_s_l (MSVCRT.@)
5232 int CDECL
MSVCRT__vfprintf_s_l(MSVCRT_FILE
* file
, const char *format
,
5233 MSVCRT__locale_t locale
, __ms_va_list valist
)
5235 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5238 /*********************************************************************
5239 * _vfwprintf_s_l (MSVCRT.@)
5241 int CDECL
MSVCRT__vfwprintf_s_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5242 MSVCRT__locale_t locale
, __ms_va_list valist
)
5244 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5247 /*********************************************************************
5248 * vfprintf (MSVCRT.@)
5250 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5252 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5255 /*********************************************************************
5256 * vfprintf_s (MSVCRT.@)
5258 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5260 return MSVCRT__vfprintf_s_l(file
, format
, NULL
, valist
);
5263 /*********************************************************************
5264 * vfwprintf (MSVCRT.@)
5266 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5268 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5271 /*********************************************************************
5272 * vfwprintf_s (MSVCRT.@)
5274 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5276 return MSVCRT__vfwprintf_s_l(file
, format
, NULL
, valist
);
5279 #if _MSVCR_VER >= 140
5281 /*********************************************************************
5282 * __stdio_common_vfprintf (UCRTBASE.@)
5284 int CDECL
MSVCRT__stdio_common_vfprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5285 MSVCRT__locale_t locale
, __ms_va_list valist
)
5287 if (options
& ~UCRTBASE_PRINTF_MASK
)
5288 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5290 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5293 /*********************************************************************
5294 * __stdio_common_vfprintf_s (UCRTBASE.@)
5296 int CDECL
MSVCRT__stdio_common_vfprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5297 MSVCRT__locale_t locale
, __ms_va_list valist
)
5299 if (options
& ~UCRTBASE_PRINTF_MASK
)
5300 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5302 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5303 file
, format
, locale
, valist
);
5306 /*********************************************************************
5307 * __stdio_common_vfwprintf (UCRTBASE.@)
5309 int CDECL
MSVCRT__stdio_common_vfwprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5310 MSVCRT__locale_t locale
, __ms_va_list valist
)
5312 if (options
& ~UCRTBASE_PRINTF_MASK
)
5313 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5315 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5318 /*********************************************************************
5319 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5321 int CDECL
MSVCRT__stdio_common_vfwprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5322 MSVCRT__locale_t locale
, __ms_va_list valist
)
5324 if (options
& ~UCRTBASE_PRINTF_MASK
)
5325 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5327 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5328 file
, format
, locale
, valist
);
5331 #endif /* _MSVCR_VER >= 140 */
5333 /*********************************************************************
5334 * _vfprintf_l (MSVCRT.@)
5336 int CDECL
MSVCRT__vfprintf_l(MSVCRT_FILE
* file
, const char *format
,
5337 MSVCRT__locale_t locale
, __ms_va_list valist
)
5339 return vfprintf_helper(0, file
, format
, locale
, valist
);
5342 /*********************************************************************
5343 * _vfwprintf_l (MSVCRT.@)
5345 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5346 MSVCRT__locale_t locale
, __ms_va_list valist
)
5348 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5351 /*********************************************************************
5352 * _vfprintf_p_l (MSVCRT.@)
5354 int CDECL
MSVCRT__vfprintf_p_l(MSVCRT_FILE
* file
, const char *format
,
5355 MSVCRT__locale_t locale
, __ms_va_list valist
)
5357 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5358 file
, format
, locale
, valist
);
5361 /*********************************************************************
5362 * _vfprintf_p (MSVCRT.@)
5364 int CDECL
MSVCRT__vfprintf_p(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5366 return MSVCRT__vfprintf_p_l(file
, format
, NULL
, valist
);
5369 /*********************************************************************
5370 * _vfwprintf_p_l (MSVCRT.@)
5372 int CDECL
MSVCRT__vfwprintf_p_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5373 MSVCRT__locale_t locale
, __ms_va_list valist
)
5375 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5376 file
, format
, locale
, valist
);
5379 /*********************************************************************
5380 * _vfwprintf_p (MSVCRT.@)
5382 int CDECL
MSVCRT__vfwprintf_p(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5384 return MSVCRT__vfwprintf_p_l(file
, format
, NULL
, valist
);
5387 /*********************************************************************
5388 * vprintf (MSVCRT.@)
5390 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5392 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5395 /*********************************************************************
5396 * vprintf_s (MSVCRT.@)
5398 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5400 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5403 /*********************************************************************
5404 * vwprintf (MSVCRT.@)
5406 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5408 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5411 /*********************************************************************
5412 * vwprintf_s (MSVCRT.@)
5414 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5416 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5419 /*********************************************************************
5420 * fprintf (MSVCRT.@)
5422 int WINAPIV
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5424 __ms_va_list valist
;
5426 __ms_va_start(valist
, format
);
5427 res
= MSVCRT_vfprintf(file
, format
, valist
);
5428 __ms_va_end(valist
);
5432 /*********************************************************************
5433 * fprintf_s (MSVCRT.@)
5435 int WINAPIV
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5437 __ms_va_list valist
;
5439 __ms_va_start(valist
, format
);
5440 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5441 __ms_va_end(valist
);
5445 /*********************************************************************
5446 * fwprintf (MSVCRT.@)
5448 int WINAPIV
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5450 __ms_va_list valist
;
5452 __ms_va_start(valist
, format
);
5453 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5454 __ms_va_end(valist
);
5458 /*********************************************************************
5459 * fwprintf_s (MSVCRT.@)
5461 int WINAPIV
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5463 __ms_va_list valist
;
5465 __ms_va_start(valist
, format
);
5466 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5467 __ms_va_end(valist
);
5471 /*********************************************************************
5472 * _fwprintf_l (MSVCRT.@)
5474 int WINAPIV
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5476 __ms_va_list valist
;
5478 __ms_va_start(valist
, locale
);
5479 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5480 __ms_va_end(valist
);
5484 /*********************************************************************
5487 int WINAPIV
MSVCRT_printf(const char *format
, ...)
5489 __ms_va_list valist
;
5491 __ms_va_start(valist
, format
);
5492 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5493 __ms_va_end(valist
);
5497 /*********************************************************************
5498 * printf_s (MSVCRT.@)
5500 int WINAPIV
MSVCRT_printf_s(const char *format
, ...)
5502 __ms_va_list valist
;
5504 __ms_va_start(valist
, format
);
5505 res
= MSVCRT_vprintf_s(format
, valist
);
5506 __ms_va_end(valist
);
5510 /*********************************************************************
5513 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5517 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5519 MSVCRT__lock_file(file
);
5520 ret
= MSVCRT__ungetc_nolock(c
, file
);
5521 MSVCRT__unlock_file(file
);
5526 /*********************************************************************
5527 * _ungetc_nolock (MSVCRT.@)
5529 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5531 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5533 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5534 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5537 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5538 && msvcrt_alloc_buffer(file
))
5539 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5542 if(file
->_ptr
>file
->_base
) {
5544 if(file
->_flag
& MSVCRT__IOSTRG
) {
5545 if(*file
->_ptr
!= c
) {
5553 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5554 file
->_flag
|= MSVCRT__IOREAD
;
5561 /*********************************************************************
5562 * ungetwc (MSVCRT.@)
5564 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5568 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5570 MSVCRT__lock_file(file
);
5571 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5572 MSVCRT__unlock_file(file
);
5577 /*********************************************************************
5578 * _ungetwc_nolock (MSVCRT.@)
5580 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5582 MSVCRT_wchar_t mwc
= wc
;
5584 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5585 if (wc
== MSVCRT_WEOF
)
5588 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5589 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5590 unsigned char * pp
= (unsigned char *)&mwc
;
5593 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5594 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5598 char mbs
[MSVCRT_MB_LEN_MAX
];
5601 len
= MSVCRT_wctomb(mbs
, mwc
);
5605 for(len
--; len
>=0; len
--) {
5606 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5614 /*********************************************************************
5615 * wprintf (MSVCRT.@)
5617 int WINAPIV
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5619 __ms_va_list valist
;
5621 __ms_va_start(valist
, format
);
5622 res
= MSVCRT_vwprintf(format
, valist
);
5623 __ms_va_end(valist
);
5627 /*********************************************************************
5628 * wprintf_s (MSVCRT.@)
5630 int WINAPIV
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5632 __ms_va_list valist
;
5634 __ms_va_start(valist
, format
);
5635 res
= MSVCRT_vwprintf_s(format
, valist
);
5636 __ms_va_end(valist
);
5640 /*********************************************************************
5641 * _getmaxstdio (MSVCRT.@)
5643 int CDECL
MSVCRT__getmaxstdio(void)
5645 return MSVCRT_max_streams
;
5648 /*********************************************************************
5649 * _setmaxstdio (MSVCRT.@)
5651 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5653 TRACE("%d\n", newmax
);
5655 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5658 MSVCRT_max_streams
= newmax
;
5659 return MSVCRT_max_streams
;
5662 #if _MSVCR_VER >= 140
5663 /*********************************************************************
5664 * _get_stream_buffer_pointers (UCRTBASE.@)
5666 int CDECL
MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE
*file
, char*** base
,
5667 char*** ptr
, int** count
)
5670 *base
= &file
->_base
;
5674 *count
= &file
->_cnt
;