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 type
= GetFileType(h
);
586 msvcrt_set_fd(fdinfo
, h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
587 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0));
589 release_ioinfo(fdinfo
);
591 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO
);
592 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
593 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
594 DWORD type
= GetFileType(h
);
596 msvcrt_set_fd(fdinfo
, h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
597 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0));
599 release_ioinfo(fdinfo
);
601 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO
);
602 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
603 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
604 DWORD type
= GetFileType(h
);
606 msvcrt_set_fd(fdinfo
, h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
607 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0));
609 release_ioinfo(fdinfo
);
611 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
612 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
613 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
615 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
616 for (i
= 0; i
< 3; i
++)
618 /* FILE structs for stdin/out/err are static and never deleted */
619 MSVCRT__iob
[i
]._file
= i
;
620 MSVCRT__iob
[i
]._tmpfname
= NULL
;
621 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
623 MSVCRT_stream_idx
= 3;
626 /* INTERNAL: Flush stdio file buffer */
627 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
629 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
630 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
631 int cnt
=file
->_ptr
-file
->_base
;
632 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
633 file
->_flag
|= MSVCRT__IOERR
;
637 if(file
->_flag
& MSVCRT__IORW
)
638 file
->_flag
&= ~MSVCRT__IOWRT
;
641 file
->_ptr
=file
->_base
;
646 /*********************************************************************
649 int CDECL
MSVCRT__isatty(int fd
)
651 TRACE(":fd (%d)\n",fd
);
653 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
656 /* INTERNAL: Allocate stdio file buffer */
657 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
659 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
660 && MSVCRT__isatty(file
->_file
))
663 file
->_base
= MSVCRT_calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
665 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
666 file
->_flag
|= MSVCRT__IOMYBUF
;
668 file
->_base
= (char*)(&file
->_charbuf
);
670 file
->_flag
|= MSVCRT__IONBF
;
672 file
->_ptr
= file
->_base
;
677 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
678 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
680 static char buffers
[2][MSVCRT_BUFSIZ
];
682 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
683 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
684 || !MSVCRT__isatty(file
->_file
))
687 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
688 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
689 file
->_flag
|= MSVCRT__USERBUF
;
693 /* INTERNAL: Removes temporary buffer from stdout or stderr */
694 /* Only call this function when add_std_buffer returned TRUE */
695 static void remove_std_buffer(MSVCRT_FILE
*file
)
697 msvcrt_flush_buffer(file
);
698 file
->_ptr
= file
->_base
= NULL
;
699 file
->_bufsiz
= file
->_cnt
= 0;
700 file
->_flag
&= ~MSVCRT__USERBUF
;
703 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
704 static int msvcrt_int_to_base32(int num
, char *str
)
719 *p
= (num
& 31) + '0';
721 *p
+= ('a' - '0' - 10);
728 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
729 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
744 *p
= (num
& 31) + '0';
746 *p
+= ('a' - '0' - 10);
753 /*********************************************************************
754 * __iob_func(MSVCRT.@)
756 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
758 return &MSVCRT__iob
[0];
761 /*********************************************************************
762 * __acrt_iob_func(MSVCRT.@)
764 MSVCRT_FILE
* CDECL
MSVCRT___acrt_iob_func(unsigned idx
)
766 return &MSVCRT__iob
[idx
];
769 /*********************************************************************
772 int CDECL
MSVCRT__access(const char *filename
, int mode
)
774 DWORD attr
= GetFileAttributesA(filename
);
776 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
778 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
780 msvcrt_set_errno(GetLastError());
783 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
785 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
791 /*********************************************************************
792 * _access_s (MSVCRT.@)
794 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
796 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
797 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
799 if (MSVCRT__access(filename
, mode
) == -1)
800 return *MSVCRT__errno();
804 /*********************************************************************
805 * _waccess (MSVCRT.@)
807 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
809 DWORD attr
= GetFileAttributesW(filename
);
811 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
813 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
815 msvcrt_set_errno(GetLastError());
818 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
820 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
826 /*********************************************************************
827 * _waccess_s (MSVCRT.@)
829 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
831 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
832 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
834 if (MSVCRT__waccess(filename
, mode
) == -1)
835 return *MSVCRT__errno();
839 /*********************************************************************
842 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
844 DWORD oldFlags
= GetFileAttributesA(path
);
846 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
848 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
849 oldFlags
| FILE_ATTRIBUTE_READONLY
;
851 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
854 msvcrt_set_errno(GetLastError());
858 /*********************************************************************
861 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
863 DWORD oldFlags
= GetFileAttributesW(path
);
865 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
867 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
868 oldFlags
| FILE_ATTRIBUTE_READONLY
;
870 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
873 msvcrt_set_errno(GetLastError());
877 /*********************************************************************
880 int CDECL
MSVCRT__unlink(const char *path
)
882 TRACE("%s\n",debugstr_a(path
));
883 if(DeleteFileA(path
))
885 TRACE("failed (%d)\n",GetLastError());
886 msvcrt_set_errno(GetLastError());
890 /*********************************************************************
891 * _wunlink (MSVCRT.@)
893 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
895 TRACE("(%s)\n",debugstr_w(path
));
896 if(DeleteFileW(path
))
898 TRACE("failed (%d)\n",GetLastError());
899 msvcrt_set_errno(GetLastError());
903 /*********************************************************************
906 int CDECL
MSVCRT__commit(int fd
)
908 ioinfo
*info
= get_ioinfo(fd
);
911 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
913 if (info
->handle
== INVALID_HANDLE_VALUE
)
915 else if (!FlushFileBuffers(info
->handle
))
917 if (GetLastError() == ERROR_INVALID_HANDLE
)
919 /* FlushFileBuffers fails for console handles
920 * so we ignore this error.
926 TRACE(":failed-last error (%d)\n",GetLastError());
927 msvcrt_set_errno(GetLastError());
937 release_ioinfo(info
);
941 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
942 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
944 /* INTERNAL: Flush all stream buffer */
945 static int msvcrt_flush_all_buffers(int mask
)
947 int i
, num_flushed
= 0;
951 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
952 file
= msvcrt_get_file(i
);
956 if(file
->_flag
& mask
) {
964 TRACE(":flushed (%d) handles\n",num_flushed
);
968 /*********************************************************************
969 * _flushall (MSVCRT.@)
971 int CDECL
MSVCRT__flushall(void)
973 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
976 /*********************************************************************
979 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
984 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
987 MSVCRT__lock_file(file
);
988 ret
= MSVCRT__fflush_nolock(file
);
989 MSVCRT__unlock_file(file
);
995 /*********************************************************************
996 * _fflush_nolock (MSVCRT.@)
998 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
1003 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1007 res
= msvcrt_flush_buffer(file
);
1008 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1009 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
1013 /*********************************************************************
1016 int CDECL
MSVCRT__close(int fd
)
1018 ioinfo
*info
= get_ioinfo(fd
);
1021 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1022 if (!(info
->wxflag
& WX_OPEN
)) {
1024 } else if (fd
== MSVCRT_STDOUT_FILENO
&&
1025 info
->handle
== get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
) {
1028 } else if (fd
== MSVCRT_STDERR_FILENO
&&
1029 info
->handle
== get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
) {
1033 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1036 WARN(":failed-last error (%d)\n",GetLastError());
1037 msvcrt_set_errno(GetLastError());
1040 release_ioinfo(info
);
1044 /*********************************************************************
1047 * MSDN isn't clear on this point, but the remarks for _pipe
1048 * indicate file descriptors duplicated with _dup and _dup2 are always
1051 int CDECL
MSVCRT__dup2(int od
, int nd
)
1053 ioinfo
*info_od
, *info_nd
;
1056 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1060 info_od
= get_ioinfo(od
);
1061 info_nd
= get_ioinfo_alloc_fd(nd
);
1065 info_nd
= get_ioinfo_alloc_fd(nd
);
1066 info_od
= get_ioinfo(od
);
1069 if (info_nd
== &MSVCRT___badioinfo
)
1073 else if (info_od
->wxflag
& WX_OPEN
)
1077 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1078 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1080 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1082 if (info_nd
->wxflag
& WX_OPEN
)
1085 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1086 /* _dup2 returns 0, not nd, on success */
1092 msvcrt_set_errno(GetLastError());
1097 *MSVCRT__errno() = MSVCRT_EBADF
;
1101 release_ioinfo(info_od
);
1102 release_ioinfo(info_nd
);
1106 /*********************************************************************
1109 int CDECL
MSVCRT__dup(int od
)
1112 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1114 if (MSVCRT__dup2(od
, fd
) == 0)
1118 release_ioinfo(info
);
1122 /*********************************************************************
1125 int CDECL
MSVCRT__eof(int fd
)
1127 ioinfo
*info
= get_ioinfo(fd
);
1128 DWORD curpos
,endpos
;
1129 LONG hcurpos
,hendpos
;
1131 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1133 if (info
->handle
== INVALID_HANDLE_VALUE
)
1135 release_ioinfo(info
);
1139 if (info
->wxflag
& WX_ATEOF
)
1141 release_ioinfo(info
);
1145 /* Otherwise we do it the hard way */
1146 hcurpos
= hendpos
= 0;
1147 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1148 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1150 if (curpos
== endpos
&& hcurpos
== hendpos
)
1152 /* FIXME: shouldn't WX_ATEOF be set here? */
1153 release_ioinfo(info
);
1157 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1158 release_ioinfo(info
);
1162 /*********************************************************************
1163 * _fcloseall (MSVCRT.@)
1165 int CDECL
MSVCRT__fcloseall(void)
1167 int num_closed
= 0, i
;
1171 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1172 file
= msvcrt_get_file(i
);
1174 if (file
->_flag
&& !MSVCRT_fclose(file
))
1179 TRACE(":closed (%d) handles\n",num_closed
);
1183 /* free everything on process exit */
1184 void msvcrt_free_io(void)
1190 MSVCRT__fcloseall();
1192 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1194 if(!MSVCRT___pioinfo
[i
])
1197 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1199 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1200 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1202 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1205 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1207 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1208 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1210 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1211 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1215 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1216 MSVCRT_free(MSVCRT_fstream
[i
]);
1219 /*********************************************************************
1220 * _lseeki64 (MSVCRT.@)
1222 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1224 ioinfo
*info
= get_ioinfo(fd
);
1227 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1229 if (info
->handle
== INVALID_HANDLE_VALUE
)
1231 release_ioinfo(info
);
1235 if (whence
< 0 || whence
> 2)
1237 release_ioinfo(info
);
1238 *MSVCRT__errno() = MSVCRT_EINVAL
;
1242 TRACE(":fd (%d) to %s pos %s\n",
1243 fd
,wine_dbgstr_longlong(offset
),
1244 (whence
==MSVCRT_SEEK_SET
)?"SEEK_SET":
1245 (whence
==MSVCRT_SEEK_CUR
)?"SEEK_CUR":
1246 (whence
==MSVCRT_SEEK_END
)?"SEEK_END":"UNKNOWN");
1248 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1249 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1250 ofs
.QuadPart
= offset
;
1251 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1252 GetLastError() == ERROR_SUCCESS
)
1254 info
->wxflag
&= ~WX_ATEOF
;
1255 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1257 release_ioinfo(info
);
1258 return ofs
.QuadPart
;
1260 release_ioinfo(info
);
1261 TRACE(":error-last error (%d)\n",GetLastError());
1262 msvcrt_set_errno(GetLastError());
1266 /*********************************************************************
1269 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1271 return MSVCRT__lseeki64(fd
, offset
, whence
);
1274 /*********************************************************************
1275 * _lock_file (MSVCRT.@)
1277 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1279 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1280 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1282 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1285 /*********************************************************************
1286 * _unlock_file (MSVCRT.@)
1288 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1290 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1291 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1293 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1296 /*********************************************************************
1297 * _locking (MSVCRT.@)
1299 * This is untested; the underlying LockFile doesn't work yet.
1301 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1303 ioinfo
*info
= get_ioinfo(fd
);
1307 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1308 if (info
->handle
== INVALID_HANDLE_VALUE
)
1310 release_ioinfo(info
);
1314 if (mode
< 0 || mode
> 4)
1316 release_ioinfo(info
);
1317 *MSVCRT__errno() = MSVCRT_EINVAL
;
1321 TRACE(":fd (%d) by 0x%08x mode %s\n",
1322 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1323 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1324 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1325 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1326 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1329 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1331 release_ioinfo(info
);
1332 FIXME ("Seek failed\n");
1333 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1336 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1339 ret
= 1; /* just to satisfy gcc */
1342 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1347 else if (mode
== MSVCRT__LK_UNLCK
)
1348 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1350 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1351 /* FIXME - what about error settings? */
1352 release_ioinfo(info
);
1353 return ret
? 0 : -1;
1356 /*********************************************************************
1357 * _fseeki64 (MSVCRT.@)
1359 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1363 MSVCRT__lock_file(file
);
1364 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1365 MSVCRT__unlock_file(file
);
1370 /*********************************************************************
1371 * _fseeki64_nolock (MSVCRT.@)
1373 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1377 if(whence
== MSVCRT_SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1378 whence
= MSVCRT_SEEK_SET
;
1379 offset
+= MSVCRT__ftelli64_nolock(file
);
1382 /* Flush output if needed */
1383 msvcrt_flush_buffer(file
);
1384 /* Reset direction of i/o */
1385 if(file
->_flag
& MSVCRT__IORW
) {
1386 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1388 /* Clear end of file flag */
1389 file
->_flag
&= ~MSVCRT__IOEOF
;
1390 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1395 /*********************************************************************
1398 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1400 return MSVCRT__fseeki64( file
, offset
, whence
);
1403 /*********************************************************************
1404 * _fseek_nolock (MSVCRT.@)
1406 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1408 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1411 /*********************************************************************
1412 * _chsize_s (MSVCRT.@)
1414 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1420 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1422 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1425 info
= get_ioinfo(fd
);
1426 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1428 /* save the current file pointer */
1429 cur
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1432 pos
= MSVCRT__lseeki64(fd
, size
, MSVCRT_SEEK_SET
);
1435 ret
= SetEndOfFile(info
->handle
);
1436 if (!ret
) msvcrt_set_errno(GetLastError());
1439 /* restore the file pointer */
1440 MSVCRT__lseeki64(fd
, cur
, MSVCRT_SEEK_SET
);
1444 release_ioinfo(info
);
1445 return ret
? 0 : *MSVCRT__errno();
1448 /*********************************************************************
1449 * _chsize (MSVCRT.@)
1451 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1453 /* _chsize_s returns errno on failure but _chsize should return -1 */
1454 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1457 /*********************************************************************
1458 * clearerr (MSVCRT.@)
1460 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1462 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1464 MSVCRT__lock_file(file
);
1465 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1466 MSVCRT__unlock_file(file
);
1469 /*********************************************************************
1472 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1474 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1476 MSVCRT__lock_file(file
);
1477 MSVCRT__fseek_nolock(file
, 0L, MSVCRT_SEEK_SET
);
1478 MSVCRT_clearerr(file
);
1479 MSVCRT__unlock_file(file
);
1482 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1484 int plus
= strchrW(mode
, '+') != NULL
;
1486 TRACE("%s\n", debugstr_w(mode
));
1488 while(*mode
== ' ') mode
++;
1493 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1494 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1497 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1498 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1501 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1502 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1505 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1509 *stream_flags
|= MSVCRT__commode
;
1511 while (*mode
&& *mode
!=',')
1515 *open_flags
|= MSVCRT__O_BINARY
;
1516 *open_flags
&= ~MSVCRT__O_TEXT
;
1519 *open_flags
|= MSVCRT__O_TEXT
;
1520 *open_flags
&= ~MSVCRT__O_BINARY
;
1523 *open_flags
|= MSVCRT__O_TEMPORARY
;
1526 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1529 *stream_flags
|= MSVCRT__IOCOMMIT
;
1532 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1535 *open_flags
|= MSVCRT__O_NOINHERIT
;
1544 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1547 ERR("incorrect mode flag: %c\n", mode
[-1]);
1553 static const WCHAR ccs
[] = {'c','c','s'};
1554 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1555 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1556 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1559 while(*mode
== ' ') mode
++;
1560 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1562 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1563 while(*mode
== ' ') mode
++;
1564 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1567 while(*mode
== ' ') mode
++;
1569 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1571 *open_flags
|= MSVCRT__O_U8TEXT
;
1572 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1574 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1576 *open_flags
|= MSVCRT__O_U16TEXT
;
1577 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1579 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1581 *open_flags
|= MSVCRT__O_WTEXT
;
1582 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1586 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1590 while(*mode
== ' ') mode
++;
1593 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1598 /*********************************************************************
1599 * _fdopen (MSVCRT.@)
1601 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1604 MSVCRT_wchar_t
*modeW
= NULL
;
1606 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1608 ret
= MSVCRT__wfdopen(fd
, modeW
);
1614 /*********************************************************************
1615 * _wfdopen (MSVCRT.@)
1617 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1619 int open_flags
, stream_flags
;
1622 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1625 if (!(file
= msvcrt_alloc_fp()))
1627 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1632 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1638 /*********************************************************************
1639 * _filelength (MSVCRT.@)
1641 LONG CDECL
MSVCRT__filelength(int fd
)
1643 LONG curPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
1646 LONG endPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_END
);
1649 if (endPos
!= curPos
)
1650 MSVCRT__lseek(fd
, curPos
, MSVCRT_SEEK_SET
);
1657 /*********************************************************************
1658 * _filelengthi64 (MSVCRT.@)
1660 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1662 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1665 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_END
);
1668 if (endPos
!= curPos
)
1669 MSVCRT__lseeki64(fd
, curPos
, MSVCRT_SEEK_SET
);
1676 /*********************************************************************
1677 * _fileno (MSVCRT.@)
1679 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1681 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1685 /*********************************************************************
1686 * _fstat64 (MSVCRT.@)
1688 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1690 ioinfo
*info
= get_ioinfo(fd
);
1693 BY_HANDLE_FILE_INFORMATION hfi
;
1695 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1696 if (info
->handle
== INVALID_HANDLE_VALUE
)
1698 release_ioinfo(info
);
1704 WARN(":failed-NULL buf\n");
1705 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1706 release_ioinfo(info
);
1710 memset(&hfi
, 0, sizeof(hfi
));
1711 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1712 type
= GetFileType(info
->handle
);
1713 if (type
== FILE_TYPE_PIPE
)
1715 buf
->st_dev
= buf
->st_rdev
= fd
;
1716 buf
->st_mode
= MSVCRT__S_IFIFO
;
1719 else if (type
== FILE_TYPE_CHAR
)
1721 buf
->st_dev
= buf
->st_rdev
= fd
;
1722 buf
->st_mode
= MSVCRT__S_IFCHR
;
1725 else /* FILE_TYPE_DISK etc. */
1727 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1729 WARN(":failed-last error (%d)\n",GetLastError());
1730 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1731 release_ioinfo(info
);
1734 buf
->st_mode
= MSVCRT__S_IFREG
| 0444;
1735 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1736 buf
->st_mode
|= 0222;
1737 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1738 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1740 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1741 buf
->st_mtime
= buf
->st_ctime
= dw
;
1742 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1744 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1746 release_ioinfo(info
);
1750 /*********************************************************************
1751 * _fstati64 (MSVCRT.@)
1753 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1756 struct MSVCRT__stat64 buf64
;
1758 ret
= MSVCRT__fstat64(fd
, &buf64
);
1760 msvcrt_stat64_to_stati64(&buf64
, buf
);
1764 /*********************************************************************
1767 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1769 struct MSVCRT__stat64 buf64
;
1771 ret
= MSVCRT__fstat64(fd
, &buf64
);
1773 msvcrt_stat64_to_stat(&buf64
, buf
);
1777 /*********************************************************************
1778 * _fstat32 (MSVCR80.@)
1780 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1783 struct MSVCRT__stat64 buf64
;
1785 ret
= MSVCRT__fstat64(fd
, &buf64
);
1787 msvcrt_stat64_to_stat32(&buf64
, buf
);
1791 /*********************************************************************
1792 * _fstat32i64 (MSVCR80.@)
1794 int CDECL
MSVCRT__fstat32i64(int fd
, struct MSVCRT__stat32i64
* buf
)
1797 struct MSVCRT__stat64 buf64
;
1799 ret
= MSVCRT__fstat64(fd
, &buf64
);
1801 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1805 /*********************************************************************
1806 * _fstat64i32 (MSVCR80.@)
1808 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1811 struct MSVCRT__stat64 buf64
;
1813 ret
= MSVCRT__fstat64(fd
, &buf64
);
1815 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1819 /*********************************************************************
1820 * _futime64 (MSVCRT.@)
1822 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1824 ioinfo
*info
= get_ioinfo(fd
);
1829 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1834 time_to_filetime( t
->actime
, &at
);
1835 time_to_filetime( t
->modtime
, &wt
);
1838 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1840 release_ioinfo(info
);
1841 msvcrt_set_errno(GetLastError());
1844 release_ioinfo(info
);
1848 /*********************************************************************
1849 * _futime32 (MSVCRT.@)
1851 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1855 struct MSVCRT___utimbuf64 t64
;
1856 t64
.actime
= t
->actime
;
1857 t64
.modtime
= t
->modtime
;
1858 return _futime64( fd
, &t64
);
1861 return _futime64( fd
, NULL
);
1864 /*********************************************************************
1865 * _futime (MSVCRT.@)
1868 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1870 return _futime64( fd
, t
);
1873 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1875 return _futime32( fd
, t
);
1879 /*********************************************************************
1880 * _get_osfhandle (MSVCRT.@)
1882 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1884 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1885 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1887 if(hand
== INVALID_HANDLE_VALUE
)
1888 *MSVCRT__errno() = MSVCRT_EBADF
;
1889 return (MSVCRT_intptr_t
)hand
;
1892 /*********************************************************************
1893 * _mktemp_s (MSVCRT.@)
1895 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1899 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1900 return MSVCRT_EINVAL
;
1902 for(len
=0; len
<size
; len
++)
1905 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1908 return MSVCRT_EINVAL
;
1911 for(xno
=1; xno
<=6; xno
++)
1912 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1913 return MSVCRT_EINVAL
;
1915 id
= GetCurrentProcessId();
1916 for(xno
=1; xno
<6; xno
++) {
1917 pattern
[len
-xno
] = id
%10 + '0';
1921 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1922 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1927 *MSVCRT__errno() = MSVCRT_EEXIST
;
1928 return MSVCRT_EEXIST
;
1931 /*********************************************************************
1932 * _mktemp (MSVCRT.@)
1934 char * CDECL
MSVCRT__mktemp(char *pattern
)
1937 char *retVal
= pattern
;
1945 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1949 id
= GetCurrentProcessId();
1953 int tempNum
= id
/ 10;
1954 *pattern
-- = id
- (tempNum
* 10) + '0';
1960 *pattern
= letter
++;
1961 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1963 } while(letter
<= 'z');
1967 /*********************************************************************
1968 * _wmktemp_s (MSVCRT.@)
1970 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1974 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1975 return MSVCRT_EINVAL
;
1977 for(len
=0; len
<size
; len
++)
1980 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1983 return MSVCRT_EINVAL
;
1986 for(xno
=1; xno
<=6; xno
++)
1987 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1988 return MSVCRT_EINVAL
;
1990 id
= GetCurrentProcessId();
1991 for(xno
=1; xno
<6; xno
++) {
1992 pattern
[len
-xno
] = id
%10 + '0';
1996 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1997 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2002 *MSVCRT__errno() = MSVCRT_EEXIST
;
2003 return MSVCRT_EEXIST
;
2006 /*********************************************************************
2007 * _wmktemp (MSVCRT.@)
2009 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
2012 MSVCRT_wchar_t
*retVal
= pattern
;
2014 MSVCRT_wchar_t letter
= 'a';
2020 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2024 id
= GetCurrentProcessId();
2028 int tempNum
= id
/ 10;
2029 *pattern
-- = id
- (tempNum
* 10) + '0';
2035 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2037 *pattern
= letter
++;
2038 } while(letter
!= '|');
2042 static unsigned split_oflags(unsigned oflags
)
2045 unsigned unsupp
; /* until we support everything */
2047 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2048 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2049 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2050 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2051 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2052 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2053 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2054 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2055 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2057 if ((unsupp
= oflags
& ~(
2058 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2059 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2060 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2061 MSVCRT__O_NOINHERIT
|
2062 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2063 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2065 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2070 /*********************************************************************
2073 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2076 SECURITY_ATTRIBUTES sa
;
2077 HANDLE readHandle
, writeHandle
;
2081 *MSVCRT__errno() = MSVCRT_EINVAL
;
2085 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2086 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2087 sa
.lpSecurityDescriptor
= NULL
;
2088 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2090 unsigned int wxflags
= split_oflags(textmode
);
2093 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2097 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2105 MSVCRT__close(pfds
[0]);
2106 CloseHandle(writeHandle
);
2107 *MSVCRT__errno() = MSVCRT_EMFILE
;
2112 CloseHandle(readHandle
);
2113 CloseHandle(writeHandle
);
2114 *MSVCRT__errno() = MSVCRT_EMFILE
;
2118 msvcrt_set_errno(GetLastError());
2123 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2125 char bom
[sizeof(utf8_bom
)];
2128 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2130 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2133 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2134 oflags
|= MSVCRT__O_U8TEXT
;
2135 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2137 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2138 oflags
|= MSVCRT__O_U16TEXT
;
2140 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2146 /*********************************************************************
2147 * _wsopen_s (MSVCRT.@)
2149 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2151 DWORD access
= 0, creation
= 0, attrib
;
2152 SECURITY_ATTRIBUTES sa
;
2153 DWORD sharing
, type
;
2157 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2158 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2160 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2163 wxflag
= split_oflags(oflags
);
2164 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2166 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2167 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2168 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2171 if (oflags
& MSVCRT__O_CREAT
)
2173 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2174 FIXME(": pmode 0x%04x ignored\n", pmode
);
2176 if (oflags
& MSVCRT__O_EXCL
)
2177 creation
= CREATE_NEW
;
2178 else if (oflags
& MSVCRT__O_TRUNC
)
2179 creation
= CREATE_ALWAYS
;
2181 creation
= OPEN_ALWAYS
;
2183 else /* no MSVCRT__O_CREAT */
2185 if (oflags
& MSVCRT__O_TRUNC
)
2186 creation
= TRUNCATE_EXISTING
;
2188 creation
= OPEN_EXISTING
;
2193 case MSVCRT__SH_DENYRW
:
2196 case MSVCRT__SH_DENYWR
:
2197 sharing
= FILE_SHARE_READ
;
2199 case MSVCRT__SH_DENYRD
:
2200 sharing
= FILE_SHARE_WRITE
;
2202 case MSVCRT__SH_DENYNO
:
2203 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2206 ERR( "Unhandled shflags 0x%x\n", shflags
);
2207 return MSVCRT_EINVAL
;
2210 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2211 attrib
= FILE_ATTRIBUTE_READONLY
;
2213 attrib
= FILE_ATTRIBUTE_NORMAL
;
2215 if (oflags
& MSVCRT__O_TEMPORARY
)
2217 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2219 sharing
|= FILE_SHARE_DELETE
;
2222 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2223 sa
.lpSecurityDescriptor
= NULL
;
2224 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2226 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2227 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2228 && !(access
&GENERIC_READ
))
2230 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2231 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2232 if (hand
!= INVALID_HANDLE_VALUE
)
2234 oflags
= check_bom(hand
, oflags
, FALSE
);
2238 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2241 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2242 if (hand
== INVALID_HANDLE_VALUE
) {
2243 WARN(":failed-last error (%d)\n",GetLastError());
2244 msvcrt_set_errno(GetLastError());
2245 return *MSVCRT__errno();
2248 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2250 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2251 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2252 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2254 if (oflags
& MSVCRT__O_U8TEXT
)
2256 DWORD written
= 0, tmp
;
2258 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2259 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2261 if (written
!= sizeof(utf8_bom
)) {
2262 WARN("error writing BOM\n");
2264 msvcrt_set_errno(GetLastError());
2265 return *MSVCRT__errno();
2270 DWORD written
= 0, tmp
;
2272 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2273 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2275 if (written
!= sizeof(utf16_bom
))
2277 WARN("error writing BOM\n");
2279 msvcrt_set_errno(GetLastError());
2280 return *MSVCRT__errno();
2284 else if (access
& GENERIC_READ
)
2285 oflags
= check_bom(hand
, oflags
, TRUE
);
2288 type
= GetFileType(hand
);
2289 if (type
== FILE_TYPE_CHAR
)
2291 else if (type
== FILE_TYPE_PIPE
)
2294 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2296 return *MSVCRT__errno();
2298 if (oflags
& MSVCRT__O_WTEXT
)
2299 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2300 else if (oflags
& MSVCRT__O_U16TEXT
)
2301 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2302 else if (oflags
& MSVCRT__O_U8TEXT
)
2303 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2305 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2309 /*********************************************************************
2310 * _wsopen (MSVCRT.@)
2312 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2317 if (oflags
& MSVCRT__O_CREAT
)
2321 __ms_va_start(ap
, shflags
);
2322 pmode
= va_arg(ap
, int);
2328 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2332 /*********************************************************************
2333 * _sopen_s (MSVCRT.@)
2335 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2337 MSVCRT_wchar_t
*pathW
;
2340 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2341 return MSVCRT_EINVAL
;
2343 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2344 return MSVCRT_EINVAL
;
2346 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2351 /*********************************************************************
2354 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2359 if (oflags
& MSVCRT__O_CREAT
)
2363 __ms_va_start(ap
, shflags
);
2364 pmode
= va_arg(ap
, int);
2370 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2374 /*********************************************************************
2377 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2381 if (flags
& MSVCRT__O_CREAT
)
2384 __ms_va_start(ap
, flags
);
2385 pmode
= va_arg(ap
, int);
2387 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2390 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2393 /*********************************************************************
2396 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2400 if (flags
& MSVCRT__O_CREAT
)
2403 __ms_va_start(ap
, flags
);
2404 pmode
= va_arg(ap
, int);
2406 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2409 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2412 /*********************************************************************
2415 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2417 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2418 return MSVCRT__open(path
, usedFlags
);
2421 /*********************************************************************
2422 * _wcreat (MSVCRT.@)
2424 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2426 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2427 return MSVCRT__wopen(path
, usedFlags
);
2430 /*********************************************************************
2431 * _open_osfhandle (MSVCRT.@)
2433 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2438 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2439 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2440 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2441 * text - it never sets MSVCRT__O_BINARY.
2443 /* don't let split_oflags() decide the mode if no mode is passed */
2444 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2445 oflags
|= MSVCRT__O_BINARY
;
2447 flags
= GetFileType((HANDLE
)handle
);
2448 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2450 msvcrt_set_errno(GetLastError());
2454 if (flags
== FILE_TYPE_CHAR
)
2456 else if (flags
== FILE_TYPE_PIPE
)
2460 flags
|= split_oflags(oflags
);
2462 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2463 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2467 /*********************************************************************
2470 int CDECL
MSVCRT__rmtmp(void)
2472 int num_removed
= 0, i
;
2476 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2477 file
= msvcrt_get_file(i
);
2479 if (file
->_tmpfname
)
2481 MSVCRT_fclose(file
);
2488 TRACE(":removed (%d) temp files\n",num_removed
);
2492 static inline int get_utf8_char_len(char ch
)
2494 if((ch
&0xf8) == 0xf0)
2496 else if((ch
&0xf0) == 0xe0)
2498 else if((ch
&0xe0) == 0xc0)
2503 /*********************************************************************
2504 * (internal) read_utf8
2506 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2508 HANDLE hand
= fdinfo
->handle
;
2509 char min_buf
[4], *readbuf
, lookahead
;
2510 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2512 /* make the buffer big enough to hold at least one character */
2513 /* read bytes have to fit to output and lookahead buffers */
2515 readbuf_size
= count
< 4 ? 4 : count
;
2516 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2521 if(fdinfo
->lookahead
[0] != '\n') {
2522 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2523 fdinfo
->lookahead
[0] = '\n';
2525 if(fdinfo
->lookahead
[1] != '\n') {
2526 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2527 fdinfo
->lookahead
[1] = '\n';
2529 if(fdinfo
->lookahead
[2] != '\n') {
2530 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2531 fdinfo
->lookahead
[2] = '\n';
2536 /* NOTE: this case is broken in native dll, reading
2537 * sometimes fails when small buffer is passed
2540 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2541 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2542 fdinfo
->wxflag
|= WX_ATEOF
;
2545 msvcrt_set_errno(GetLastError());
2548 }else if(!num_read
) {
2549 fdinfo
->wxflag
|= WX_ATEOF
;
2555 char_len
= get_utf8_char_len(readbuf
[0]);
2557 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2561 if(readbuf
[0] == '\n')
2562 fdinfo
->wxflag
|= WX_READNL
;
2564 fdinfo
->wxflag
&= ~WX_READNL
;
2566 if(readbuf
[0] == 0x1a) {
2567 fdinfo
->wxflag
|= WX_ATEOF
;
2571 if(readbuf
[0] == '\r') {
2572 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2574 else if(lookahead
== '\n')
2578 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2579 fdinfo
->lookahead
[0] = lookahead
;
2581 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2586 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2587 msvcrt_set_errno(GetLastError());
2594 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2597 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2598 fdinfo
->wxflag
|= WX_ATEOF
;
2599 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2602 msvcrt_set_errno(GetLastError());
2603 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2606 }else if(!pos
&& !num_read
) {
2607 fdinfo
->wxflag
|= WX_ATEOF
;
2608 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2613 if(readbuf
[0] == '\n')
2614 fdinfo
->wxflag
|= WX_READNL
;
2616 fdinfo
->wxflag
&= ~WX_READNL
;
2618 /* Find first byte of last character (may be incomplete) */
2619 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2620 if((readbuf
[i
]&0xc0) != 0x80)
2622 char_len
= get_utf8_char_len(readbuf
[i
]);
2623 if(char_len
+i
<= pos
)
2626 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2628 fdinfo
->lookahead
[0] = readbuf
[i
];
2630 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2632 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2634 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2638 for(i
=0, j
=0; i
<pos
; i
++) {
2639 if(readbuf
[i
] == 0x1a) {
2640 fdinfo
->wxflag
|= WX_ATEOF
;
2644 /* strip '\r' if followed by '\n' */
2645 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2646 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2647 readbuf
[j
++] = '\r';
2648 }else if(lookahead
== '\n' && j
==0) {
2649 readbuf
[j
++] = '\n';
2651 if(lookahead
!= '\n')
2652 readbuf
[j
++] = '\r';
2654 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2655 fdinfo
->lookahead
[0] = lookahead
;
2657 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2659 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2660 readbuf
[j
++] = readbuf
[i
];
2665 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2666 msvcrt_set_errno(GetLastError());
2667 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2671 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2675 /*********************************************************************
2678 * When reading \r as last character in text mode, read() positions
2679 * the file pointer on the \r character while getc() goes on to
2682 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2684 DWORD num_read
, utf16
;
2685 char *bufstart
= buf
;
2690 if (fdinfo
->wxflag
& WX_ATEOF
) {
2691 TRACE("already at EOF, returning 0\n");
2694 /* Don't trace small reads, it gets *very* annoying */
2696 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2697 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2699 *MSVCRT__errno() = MSVCRT_EBADF
;
2703 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2704 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2706 *MSVCRT__errno() = MSVCRT_EINVAL
;
2710 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2711 return read_utf8(fdinfo
, buf
, count
);
2713 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2715 if (fdinfo
->lookahead
[0] != '\n')
2717 bufstart
[0] = fdinfo
->lookahead
[0];
2718 fdinfo
->lookahead
[0] = '\n';
2722 bufstart
[1] = fdinfo
->lookahead
[1];
2723 fdinfo
->lookahead
[1] = '\n';
2726 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2727 num_read
+= 1+utf16
;
2732 if(utf16
&& (num_read
&1))
2734 /* msvcr90 uses uninitialized value from the buffer in this case */
2735 /* msvcrt ignores additional data */
2736 ERR("got odd number of bytes in UTF16 mode\n");
2740 if (count
!= 0 && num_read
== 0)
2742 fdinfo
->wxflag
|= WX_ATEOF
;
2743 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2745 else if (fdinfo
->wxflag
& WX_TEXT
)
2749 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2750 fdinfo
->wxflag
|= WX_READNL
;
2752 fdinfo
->wxflag
&= ~WX_READNL
;
2754 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2756 /* in text mode, a ctrl-z signals EOF */
2757 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2759 fdinfo
->wxflag
|= WX_ATEOF
;
2760 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2764 /* in text mode, strip \r if followed by \n */
2765 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2770 lookahead
[1] = '\n';
2771 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2773 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2775 bufstart
[j
++] = '\n';
2776 if(utf16
) bufstart
[j
++] = 0;
2780 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2782 bufstart
[j
++] = '\r';
2783 if(utf16
) bufstart
[j
++] = 0;
2786 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2788 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2790 bufstart
[j
++] = '\n';
2791 if (utf16
) bufstart
[j
++] = 0;
2795 fdinfo
->lookahead
[0] = lookahead
[0];
2796 fdinfo
->lookahead
[1] = lookahead
[1];
2800 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2805 bufstart
[j
++] = '\r';
2806 if(utf16
) bufstart
[j
++] = 0;
2809 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2810 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2812 bufstart
[j
++] = bufstart
[i
];
2813 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2821 if (GetLastError() == ERROR_BROKEN_PIPE
)
2823 TRACE(":end-of-pipe\n");
2824 fdinfo
->wxflag
|= WX_ATEOF
;
2829 TRACE(":failed-last error (%d)\n",GetLastError());
2835 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2839 /*********************************************************************
2842 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2844 ioinfo
*info
= get_ioinfo(fd
);
2845 int num_read
= read_i(fd
, info
, buf
, count
);
2846 release_ioinfo(info
);
2850 /*********************************************************************
2851 * _setmode (MSVCRT.@)
2853 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2855 ioinfo
*info
= get_ioinfo(fd
);
2856 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2857 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2858 ret
= MSVCRT__O_WTEXT
;
2860 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2861 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2862 *MSVCRT__errno() = MSVCRT_EINVAL
;
2863 release_ioinfo(info
);
2867 if(info
== &MSVCRT___badioinfo
) {
2868 *MSVCRT__errno() = MSVCRT_EBADF
;
2872 if(mode
== MSVCRT__O_BINARY
) {
2873 info
->wxflag
&= ~WX_TEXT
;
2874 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2875 release_ioinfo(info
);
2879 info
->wxflag
|= WX_TEXT
;
2880 if(mode
== MSVCRT__O_TEXT
)
2881 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2882 else if(mode
== MSVCRT__O_U8TEXT
)
2883 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2885 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2887 release_ioinfo(info
);
2891 /*********************************************************************
2892 * _stat64 (MSVCRT.@)
2894 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2897 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2898 unsigned short mode
= ALL_S_IREAD
;
2901 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2903 plen
= strlen(path
);
2904 while (plen
&& path
[plen
-1]==' ')
2907 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2908 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2910 *MSVCRT__errno() = MSVCRT_ENOENT
;
2914 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2916 TRACE("failed (%d)\n",GetLastError());
2917 *MSVCRT__errno() = MSVCRT_ENOENT
;
2921 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2923 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2924 Bon 011120: This FIXME seems incorrect
2925 Also a letter as first char isn't enough to be classified
2928 if (isalpha(*path
)&& (*(path
+1)==':'))
2929 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2931 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2933 /* Dir, or regular file? */
2934 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2935 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2938 mode
|= MSVCRT__S_IFREG
;
2940 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2942 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2943 (tolower(path
[plen
-3]) << 16);
2944 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2945 mode
|= ALL_S_IEXEC
;
2949 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2950 mode
|= ALL_S_IWRITE
;
2952 buf
->st_mode
= mode
;
2954 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2955 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2957 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2958 buf
->st_mtime
= buf
->st_ctime
= dw
;
2959 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2960 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2961 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2965 /*********************************************************************
2966 * _stati64 (MSVCRT.@)
2968 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2971 struct MSVCRT__stat64 buf64
;
2973 ret
= MSVCRT_stat64(path
, &buf64
);
2975 msvcrt_stat64_to_stati64(&buf64
, buf
);
2979 /*********************************************************************
2982 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2985 struct MSVCRT__stat64 buf64
;
2987 ret
= MSVCRT_stat64( path
, &buf64
);
2989 msvcrt_stat64_to_stat(&buf64
, buf
);
2993 /*********************************************************************
2994 * _stat32 (MSVCR100.@)
2996 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2999 struct MSVCRT__stat64 buf64
;
3001 ret
= MSVCRT_stat64(path
, &buf64
);
3003 msvcrt_stat64_to_stat32(&buf64
, buf
);
3007 /*********************************************************************
3008 * _stat32i64 (MSVCR100.@)
3010 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
3013 struct MSVCRT__stat64 buf64
;
3015 ret
= MSVCRT_stat64(path
, &buf64
);
3017 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3021 /*********************************************************************
3022 * _stat64i32 (MSVCR100.@)
3024 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
3027 struct MSVCRT__stat64 buf64
;
3029 ret
= MSVCRT_stat64(path
, &buf64
);
3031 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3035 /*********************************************************************
3036 * _wstat64 (MSVCRT.@)
3038 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
3041 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3042 unsigned short mode
= ALL_S_IREAD
;
3045 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3047 plen
= strlenW(path
);
3048 while (plen
&& path
[plen
-1]==' ')
3051 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
3052 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3054 *MSVCRT__errno() = MSVCRT_ENOENT
;
3058 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3060 TRACE("failed (%d)\n",GetLastError());
3061 *MSVCRT__errno() = MSVCRT_ENOENT
;
3065 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3067 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3068 if (MSVCRT_iswalpha(*path
))
3069 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3071 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3073 /* Dir, or regular file? */
3074 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3075 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3078 mode
|= MSVCRT__S_IFREG
;
3080 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3082 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3083 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3084 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3085 mode
|= ALL_S_IEXEC
;
3089 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3090 mode
|= ALL_S_IWRITE
;
3092 buf
->st_mode
= mode
;
3094 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3095 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3097 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3098 buf
->st_mtime
= buf
->st_ctime
= dw
;
3099 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3100 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3101 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3105 /*********************************************************************
3106 * _wstati64 (MSVCRT.@)
3108 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3111 struct MSVCRT__stat64 buf64
;
3113 ret
= MSVCRT__wstat64(path
, &buf64
);
3115 msvcrt_stat64_to_stati64(&buf64
, buf
);
3119 /*********************************************************************
3122 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3125 struct MSVCRT__stat64 buf64
;
3127 ret
= MSVCRT__wstat64( path
, &buf64
);
3128 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3132 /*********************************************************************
3133 * _wstat32 (MSVCR100.@)
3135 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3138 struct MSVCRT__stat64 buf64
;
3140 ret
= MSVCRT__wstat64(path
, &buf64
);
3142 msvcrt_stat64_to_stat32(&buf64
, buf
);
3146 /*********************************************************************
3147 * _wstat32i64 (MSVCR100.@)
3149 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3152 struct MSVCRT__stat64 buf64
;
3154 ret
= MSVCRT__wstat64(path
, &buf64
);
3156 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3160 /*********************************************************************
3161 * _wstat64i32 (MSVCR100.@)
3163 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3166 struct MSVCRT__stat64 buf64
;
3168 ret
= MSVCRT__wstat64(path
, &buf64
);
3170 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3174 /*********************************************************************
3177 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3179 return MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
3182 /*********************************************************************
3183 * _telli64 (MSVCRT.@)
3185 __int64 CDECL
_telli64(int fd
)
3187 return MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
3190 /*********************************************************************
3191 * _tempnam (MSVCRT.@)
3193 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3195 char tmpbuf
[MAX_PATH
];
3196 const char *tmp_dir
= MSVCRT_getenv("TMP");
3198 if (tmp_dir
) dir
= tmp_dir
;
3200 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3201 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3203 TRACE("got name (%s)\n",tmpbuf
);
3204 DeleteFileA(tmpbuf
);
3205 return MSVCRT__strdup(tmpbuf
);
3207 TRACE("failed (%d)\n",GetLastError());
3211 /*********************************************************************
3212 * _wtempnam (MSVCRT.@)
3214 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3216 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3217 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3218 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3220 if (tmp_dir
) dir
= tmp_dir
;
3222 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3223 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3225 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3226 DeleteFileW(tmpbuf
);
3227 return MSVCRT__wcsdup(tmpbuf
);
3229 TRACE("failed (%d)\n",GetLastError());
3233 /*********************************************************************
3236 int CDECL
MSVCRT__umask(int umask
)
3238 int old_umask
= MSVCRT_umask
;
3239 TRACE("(%d)\n",umask
);
3240 MSVCRT_umask
= umask
;
3244 /*********************************************************************
3245 * _utime64 (MSVCRT.@)
3247 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3249 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3253 int retVal
= _futime64(fd
, t
);
3260 /*********************************************************************
3261 * _utime32 (MSVCRT.@)
3263 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3267 struct MSVCRT___utimbuf64 t64
;
3268 t64
.actime
= t
->actime
;
3269 t64
.modtime
= t
->modtime
;
3270 return _utime64( path
, &t64
);
3273 return _utime64( path
, NULL
);
3276 /*********************************************************************
3280 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3282 return _utime64( path
, t
);
3285 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3287 return _utime32( path
, t
);
3291 /*********************************************************************
3292 * _wutime64 (MSVCRT.@)
3294 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3296 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3300 int retVal
= _futime64(fd
, t
);
3307 /*********************************************************************
3308 * _wutime32 (MSVCRT.@)
3310 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3314 struct MSVCRT___utimbuf64 t64
;
3315 t64
.actime
= t
->actime
;
3316 t64
.modtime
= t
->modtime
;
3317 return _wutime64( path
, &t64
);
3320 return _wutime64( path
, NULL
);
3323 /*********************************************************************
3324 * _wutime (MSVCRT.@)
3327 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3329 return _wutime64( path
, t
);
3332 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3334 return _wutime32( path
, t
);
3338 /*********************************************************************
3341 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3344 ioinfo
*info
= get_ioinfo(fd
);
3345 HANDLE hand
= info
->handle
;
3347 /* Don't trace small writes, it gets *very* annoying */
3350 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3352 if (hand
== INVALID_HANDLE_VALUE
)
3354 *MSVCRT__errno() = MSVCRT_EBADF
;
3355 release_ioinfo(info
);
3359 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3361 *MSVCRT__errno() = MSVCRT_EINVAL
;
3362 release_ioinfo(info
);
3366 /* If appending, go to EOF */
3367 if (info
->wxflag
& WX_APPEND
)
3368 MSVCRT__lseek(fd
, 0, FILE_END
);
3370 if (!(info
->wxflag
& WX_TEXT
))
3372 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3373 && (num_written
== count
))
3375 release_ioinfo(info
);
3378 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3379 hand
, GetLastError());
3380 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3384 unsigned int i
, j
, nr_lf
, size
;
3387 const char *s
= buf
, *buf_start
= buf
;
3389 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3391 /* find number of \n */
3392 for (nr_lf
=0, i
=0; i
<count
; i
++)
3398 if ((q
= p
= MSVCRT_malloc(size
)))
3400 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3409 FIXME("Malloc failed\n");
3421 else if (info
->exflag
& EF_UTF16
)
3423 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3424 if (s
[i
]=='\n' && s
[i
+1]==0)
3429 if ((q
= p
= MSVCRT_malloc(size
)))
3431 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3433 if (s
[i
]=='\n' && s
[i
+1]==0)
3444 FIXME("Malloc failed\n");
3460 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3461 if (s
[i
]=='\n' && s
[i
+1]==0)
3464 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3466 msvcrt_set_errno(GetLastError());
3468 release_ioinfo(info
);
3472 size
= conv_len
+nr_lf
;
3473 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3475 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3477 if (s
[i
]=='\n' && s
[i
+1]==0)
3485 q
= p
+count
+nr_lf
*2;
3486 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3487 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3491 FIXME("Malloc failed\n");
3498 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3500 release_ioinfo(info
);
3502 if (num_written
!= size
)
3504 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3505 fd
, hand
, GetLastError(), num_written
);
3506 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3507 return s
- buf_start
;
3512 release_ioinfo(info
);
3516 /*********************************************************************
3519 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3523 MSVCRT__lock_file(file
);
3524 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3525 if (len
== sizeof(val
)) {
3526 MSVCRT__unlock_file(file
);
3530 file
->_flag
|= MSVCRT__IOERR
;
3531 MSVCRT__unlock_file(file
);
3535 /*********************************************************************
3538 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3542 MSVCRT__lock_file(file
);
3543 ret
= MSVCRT__fclose_nolock(file
);
3544 MSVCRT__unlock_file(file
);
3549 /*********************************************************************
3550 * _fclose_nolock (MSVCRT.@)
3552 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3557 MSVCRT_free(file
->_tmpfname
);
3558 file
->_tmpfname
= NULL
;
3559 /* flush stdio buffers */
3560 if(file
->_flag
& MSVCRT__IOWRT
)
3561 MSVCRT__fflush_nolock(file
);
3562 if(file
->_flag
& MSVCRT__IOMYBUF
)
3563 MSVCRT_free(file
->_base
);
3565 r
=MSVCRT__close(file
->_file
);
3568 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3571 /*********************************************************************
3574 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3576 return file
->_flag
& MSVCRT__IOEOF
;
3579 /*********************************************************************
3582 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3584 return file
->_flag
& MSVCRT__IOERR
;
3587 /*********************************************************************
3588 * _filbuf (MSVCRT.@)
3590 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3594 if(file
->_flag
& MSVCRT__IOSTRG
)
3597 /* Allocate buffer if needed */
3598 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3599 msvcrt_alloc_buffer(file
);
3601 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3602 if(file
->_flag
& MSVCRT__IORW
)
3603 file
->_flag
|= MSVCRT__IOREAD
;
3608 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3610 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3611 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3617 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3619 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3625 file
->_ptr
= file
->_base
+1;
3626 c
= *(unsigned char *)file
->_base
;
3631 /*********************************************************************
3634 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3638 MSVCRT__lock_file(file
);
3639 ret
= MSVCRT__fgetc_nolock(file
);
3640 MSVCRT__unlock_file(file
);
3645 /*********************************************************************
3646 * _fgetc_nolock (MSVCRT.@)
3648 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3655 i
= (unsigned char *)file
->_ptr
++;
3658 j
= MSVCRT__filbuf(file
);
3663 /*********************************************************************
3664 * _fgetchar (MSVCRT.@)
3666 int CDECL
MSVCRT__fgetchar(void)
3668 return MSVCRT_fgetc(MSVCRT_stdin
);
3671 /*********************************************************************
3674 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3676 int cc
= MSVCRT_EOF
;
3677 char * buf_start
= s
;
3679 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3680 file
,file
->_file
,s
,size
);
3682 MSVCRT__lock_file(file
);
3684 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3689 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3691 TRACE(":nothing read\n");
3692 MSVCRT__unlock_file(file
);
3695 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3698 TRACE(":got %s\n", debugstr_a(buf_start
));
3699 MSVCRT__unlock_file(file
);
3703 /*********************************************************************
3706 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3710 MSVCRT__lock_file(file
);
3711 ret
= MSVCRT__fgetwc_nolock(file
);
3712 MSVCRT__unlock_file(file
);
3717 /*********************************************************************
3718 * _fgetwc_nolock (MSVCRT.@)
3720 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3725 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3726 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3729 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3730 ch
= MSVCRT__fgetc_nolock(file
);
3731 if(ch
== MSVCRT_EOF
) {
3738 char mbs
[MSVCRT_MB_LEN_MAX
];
3741 ch
= MSVCRT__fgetc_nolock(file
);
3742 if(ch
!= MSVCRT_EOF
) {
3744 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3745 ch
= MSVCRT__fgetc_nolock(file
);
3746 if(ch
!= MSVCRT_EOF
) {
3755 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3762 /*********************************************************************
3765 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3772 MSVCRT__lock_file(file
);
3773 for (j
=0; j
<sizeof(int); j
++) {
3774 k
= MSVCRT__fgetc_nolock(file
);
3775 if (k
== MSVCRT_EOF
) {
3776 file
->_flag
|= MSVCRT__IOEOF
;
3777 MSVCRT__unlock_file(file
);
3783 MSVCRT__unlock_file(file
);
3787 /*********************************************************************
3790 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3792 return MSVCRT_fgetwc(file
);
3795 /*********************************************************************
3796 * _fgetwchar (MSVCRT.@)
3798 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3800 return MSVCRT_fgetwc(MSVCRT_stdin
);
3803 /*********************************************************************
3804 * getwchar (MSVCRT.@)
3806 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3808 return MSVCRT__fgetwchar();
3811 /*********************************************************************
3814 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3816 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3817 MSVCRT_wchar_t
* buf_start
= s
;
3819 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3820 file
,file
->_file
,s
,size
);
3822 MSVCRT__lock_file(file
);
3824 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3829 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3831 TRACE(":nothing read\n");
3832 MSVCRT__unlock_file(file
);
3835 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3838 TRACE(":got %s\n", debugstr_w(buf_start
));
3839 MSVCRT__unlock_file(file
);
3843 /*********************************************************************
3844 * _flsbuf (MSVCRT.@)
3846 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3848 /* Flush output buffer */
3849 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3850 msvcrt_alloc_buffer(file
);
3853 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3854 if(!(file
->_flag
& MSVCRT__IORW
)) {
3855 file
->_flag
|= MSVCRT__IOERR
;
3858 file
->_flag
|= MSVCRT__IOWRT
;
3860 if(file
->_flag
& MSVCRT__IOREAD
) {
3861 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3862 file
->_flag
|= MSVCRT__IOERR
;
3866 file
->_ptr
= file
->_base
;
3867 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3870 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3873 if(file
->_cnt
<= 0) {
3874 res
= msvcrt_flush_buffer(file
);
3877 file
->_flag
|= MSVCRT__IOWRT
;
3878 file
->_cnt
=file
->_bufsiz
;
3886 /* set _cnt to 0 for unbuffered FILEs */
3888 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3891 file
->_flag
|= MSVCRT__IOERR
;
3896 /*********************************************************************
3899 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3903 MSVCRT__lock_file(file
);
3904 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3905 MSVCRT__unlock_file(file
);
3910 /*********************************************************************
3911 * _fwrite_nolock (MSVCRT.@)
3913 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3915 MSVCRT_size_t wrcnt
=size
* nmemb
;
3921 if(file
->_cnt
< 0) {
3922 WARN("negative file->_cnt value in %p\n", file
);
3923 file
->_flag
|= MSVCRT__IOERR
;
3925 } else if(file
->_cnt
) {
3926 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3927 memcpy(file
->_ptr
, ptr
, pcnt
);
3932 ptr
= (const char*)ptr
+ pcnt
;
3933 } else if((file
->_flag
& MSVCRT__IONBF
)
3934 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3935 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3939 if(file
->_flag
& MSVCRT__IONBF
)
3941 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3942 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3944 bufsiz
= file
->_bufsiz
;
3946 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3948 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3951 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3952 file
->_flag
|= MSVCRT__IOERR
;
3957 ptr
= (const char*)ptr
+ pcnt
;
3959 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3963 ptr
= (const char*)ptr
+ 1;
3967 return written
/ size
;
3970 /*********************************************************************
3973 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3977 MSVCRT__lock_file(file
);
3978 ret
= MSVCRT__fputwc_nolock(wc
, file
);
3979 MSVCRT__unlock_file(file
);
3984 /*********************************************************************
3985 * _fputwc_nolock (MSVCRT.@)
3987 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3989 MSVCRT_wchar_t mwc
=wc
;
3993 fdinfo
= get_ioinfo_nolock(file
->_file
);
3995 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3996 char buf
[MSVCRT_MB_LEN_MAX
];
3999 char_len
= MSVCRT_wctomb(buf
, mwc
);
4000 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
4004 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4013 /*********************************************************************
4014 * _fputwchar (MSVCRT.@)
4016 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
4018 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
4021 /*********************************************************************
4022 * _wfsopen (MSVCRT.@)
4024 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
4027 int open_flags
, stream_flags
, fd
;
4029 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4031 /* map mode string to open() flags. "man fopen" for possibilities. */
4032 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4036 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4039 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4041 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4048 TRACE(":got (%p)\n",file
);
4049 if (fd
>= 0 && !file
)
4055 /*********************************************************************
4056 * _fsopen (MSVCRT.@)
4058 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4061 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4063 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4064 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4065 *MSVCRT__errno() = MSVCRT_EINVAL
;
4068 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4071 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4072 *MSVCRT__errno() = MSVCRT_EINVAL
;
4076 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4083 /*********************************************************************
4086 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4088 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4091 /*********************************************************************
4092 * fopen_s (MSVCRT.@)
4094 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4095 const char *filename
, const char *mode
)
4097 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4098 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4099 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4101 *pFile
= MSVCRT_fopen(filename
, mode
);
4104 return *MSVCRT__errno();
4108 /*********************************************************************
4109 * _wfopen (MSVCRT.@)
4111 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4113 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4116 /*********************************************************************
4117 * _wfopen_s (MSVCRT.@)
4119 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4120 const MSVCRT_wchar_t
*mode
)
4122 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4123 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4124 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4126 *pFile
= MSVCRT__wfopen(filename
, mode
);
4129 return *MSVCRT__errno();
4133 /*********************************************************************
4136 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4140 MSVCRT__lock_file(file
);
4141 ret
= MSVCRT__fputc_nolock(c
, file
);
4142 MSVCRT__unlock_file(file
);
4147 /*********************************************************************
4148 * _fputc_nolock (MSVCRT.@)
4150 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4159 res
= msvcrt_flush_buffer(file
);
4160 return res
? res
: c
;
4166 res
= MSVCRT__flsbuf(c
, file
);
4171 /*********************************************************************
4172 * _fputchar (MSVCRT.@)
4174 int CDECL
MSVCRT__fputchar(int c
)
4176 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4179 /*********************************************************************
4182 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4186 MSVCRT__lock_file(file
);
4187 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4188 MSVCRT__unlock_file(file
);
4193 /*********************************************************************
4194 * _fread_nolock (MSVCRT.@)
4196 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4198 MSVCRT_size_t rcnt
=size
* nmemb
;
4199 MSVCRT_size_t read
=0;
4200 MSVCRT_size_t pread
=0;
4205 /* first buffered data */
4207 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4208 memcpy(ptr
, file
->_ptr
, pcnt
);
4213 ptr
= (char*)ptr
+ pcnt
;
4214 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4215 if(file
->_flag
& MSVCRT__IORW
) {
4216 file
->_flag
|= MSVCRT__IOREAD
;
4222 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4223 msvcrt_alloc_buffer(file
);
4228 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4229 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4230 file
->_ptr
= file
->_base
;
4231 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4232 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4233 if (i
> 0 && i
< file
->_cnt
) {
4234 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4235 file
->_flag
&= ~MSVCRT__IOEOF
;
4238 memcpy(ptr
, file
->_ptr
, i
);
4242 } else if (rcnt
> INT_MAX
) {
4243 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4244 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4245 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4247 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4251 ptr
= (char *)ptr
+i
;
4252 /* expose feof condition in the flags
4253 * MFC tests file->_flag for feof, and doesn't call feof())
4255 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4256 file
->_flag
|= MSVCRT__IOEOF
;
4259 file
->_flag
|= MSVCRT__IOERR
;
4270 /*********************************************************************
4271 * fread_s (MSVCR80.@)
4273 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4274 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4278 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4280 memset(buf
, 0, buf_size
);
4283 if(!elem_size
|| !count
) return 0;
4285 MSVCRT__lock_file(stream
);
4286 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4287 MSVCRT__unlock_file(stream
);
4292 /*********************************************************************
4293 * _fread_nolock_s (MSVCR80.@)
4295 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4296 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4298 size_t bytes_left
, buf_pos
;
4300 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4302 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4304 memset(buf
, 0, buf_size
);
4307 if(!elem_size
|| !count
) return 0;
4308 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4309 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4311 bytes_left
= elem_size
*count
;
4314 if(stream
->_cnt
> 0) {
4315 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4317 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4318 memset(buf
, 0, buf_size
);
4322 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4326 int c
= MSVCRT__filbuf(stream
);
4331 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4332 memset(buf
, 0, buf_size
);
4336 ((char*)buf
)[buf_pos
++] = c
;
4341 return buf_pos
/elem_size
;
4344 /*********************************************************************
4345 * _wfreopen (MSVCRT.@)
4348 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4350 int open_flags
, stream_flags
, fd
;
4352 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4355 if (!file
|| ((fd
= file
->_file
) < 0))
4359 MSVCRT_fclose(file
);
4360 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4362 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4364 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4374 /*********************************************************************
4375 * _wfreopen_s (MSVCRT.@)
4377 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4378 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4380 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4381 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4382 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4383 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4385 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4388 return *MSVCRT__errno();
4392 /*********************************************************************
4393 * freopen (MSVCRT.@)
4396 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4399 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4401 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4402 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4408 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4415 /*********************************************************************
4416 * freopen_s (MSVCRT.@)
4418 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4419 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4421 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4422 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4423 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4424 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4426 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4429 return *MSVCRT__errno();
4433 /*********************************************************************
4434 * fsetpos (MSVCRT.@)
4436 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4440 MSVCRT__lock_file(file
);
4441 msvcrt_flush_buffer(file
);
4443 /* Reset direction of i/o */
4444 if(file
->_flag
& MSVCRT__IORW
) {
4445 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4448 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,MSVCRT_SEEK_SET
) == -1) ? -1 : 0;
4449 MSVCRT__unlock_file(file
);
4453 /*********************************************************************
4454 * _ftelli64 (MSVCRT.@)
4456 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4460 MSVCRT__lock_file(file
);
4461 ret
= MSVCRT__ftelli64_nolock(file
);
4462 MSVCRT__unlock_file(file
);
4467 /*********************************************************************
4468 * _ftelli64_nolock (MSVCRT.@)
4470 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4474 pos
= _telli64(file
->_file
);
4477 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4478 if(file
->_flag
& MSVCRT__IOWRT
) {
4479 pos
+= file
->_ptr
- file
->_base
;
4481 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4484 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4488 } else if(!file
->_cnt
) { /* nothing to do */
4489 } else if(MSVCRT__lseeki64(file
->_file
, 0, MSVCRT_SEEK_END
)==pos
) {
4493 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4494 for(i
=0; i
<file
->_cnt
; i
++)
4495 if(file
->_ptr
[i
] == '\n')
4501 if(MSVCRT__lseeki64(file
->_file
, pos
, MSVCRT_SEEK_SET
) != pos
)
4504 pos
-= file
->_bufsiz
;
4505 pos
+= file
->_ptr
- file
->_base
;
4507 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4508 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4511 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4521 /*********************************************************************
4524 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4526 return MSVCRT__ftelli64(file
);
4529 /*********************************************************************
4530 * _ftell_nolock (MSVCRT.@)
4532 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4534 return MSVCRT__ftelli64_nolock(file
);
4537 /*********************************************************************
4538 * fgetpos (MSVCRT.@)
4540 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4542 *pos
= MSVCRT__ftelli64(file
);
4548 /*********************************************************************
4551 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4553 MSVCRT_size_t len
= strlen(s
);
4556 MSVCRT__lock_file(file
);
4557 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4558 MSVCRT__unlock_file(file
);
4562 /*********************************************************************
4565 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4567 MSVCRT_size_t i
, len
= strlenW(s
);
4571 MSVCRT__lock_file(file
);
4572 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4573 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4574 MSVCRT__unlock_file(file
);
4578 tmp_buf
= add_std_buffer(file
);
4579 for (i
=0; i
<len
; i
++) {
4580 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4581 if(tmp_buf
) remove_std_buffer(file
);
4582 MSVCRT__unlock_file(file
);
4587 if(tmp_buf
) remove_std_buffer(file
);
4588 MSVCRT__unlock_file(file
);
4592 /*********************************************************************
4593 * getchar (MSVCRT.@)
4595 int CDECL
MSVCRT_getchar(void)
4597 return MSVCRT_fgetc(MSVCRT_stdin
);
4600 /*********************************************************************
4603 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4605 return MSVCRT_fgetc(file
);
4608 /*********************************************************************
4611 char * CDECL
MSVCRT_gets(char *buf
)
4614 char * buf_start
= buf
;
4616 MSVCRT__lock_file(MSVCRT_stdin
);
4617 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4618 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4619 if(cc
!= '\r') *buf
++ = (char)cc
;
4623 TRACE("got '%s'\n", buf_start
);
4624 MSVCRT__unlock_file(MSVCRT_stdin
);
4628 /*********************************************************************
4631 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4634 MSVCRT_wchar_t
* ws
= buf
;
4636 MSVCRT__lock_file(MSVCRT_stdin
);
4637 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4638 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4641 *buf
++ = (MSVCRT_wchar_t
)cc
;
4645 TRACE("got %s\n", debugstr_w(ws
));
4646 MSVCRT__unlock_file(MSVCRT_stdin
);
4650 /*********************************************************************
4653 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4655 return MSVCRT_fputc(c
, file
);
4658 /*********************************************************************
4659 * putchar (MSVCRT.@)
4661 int CDECL
MSVCRT_putchar(int c
)
4663 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4666 /*********************************************************************
4669 int CDECL
MSVCRT_puts(const char *s
)
4671 MSVCRT_size_t len
= strlen(s
);
4674 MSVCRT__lock_file(MSVCRT_stdout
);
4675 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4676 MSVCRT__unlock_file(MSVCRT_stdout
);
4680 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4681 MSVCRT__unlock_file(MSVCRT_stdout
);
4685 /*********************************************************************
4688 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4690 static const MSVCRT_wchar_t nl
= '\n';
4691 MSVCRT_size_t len
= strlenW(s
);
4694 MSVCRT__lock_file(MSVCRT_stdout
);
4695 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4696 MSVCRT__unlock_file(MSVCRT_stdout
);
4700 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4701 MSVCRT__unlock_file(MSVCRT_stdout
);
4705 /*********************************************************************
4708 int CDECL
MSVCRT_remove(const char *path
)
4710 TRACE("(%s)\n",path
);
4711 if (DeleteFileA(path
))
4713 TRACE(":failed (%d)\n",GetLastError());
4714 msvcrt_set_errno(GetLastError());
4718 /*********************************************************************
4719 * _wremove (MSVCRT.@)
4721 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4723 TRACE("(%s)\n",debugstr_w(path
));
4724 if (DeleteFileW(path
))
4726 TRACE(":failed (%d)\n",GetLastError());
4727 msvcrt_set_errno(GetLastError());
4731 /*********************************************************************
4734 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4736 TRACE(":from %s to %s\n",oldpath
,newpath
);
4737 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4739 TRACE(":failed (%d)\n",GetLastError());
4740 msvcrt_set_errno(GetLastError());
4744 /*********************************************************************
4745 * _wrename (MSVCRT.@)
4747 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4749 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4750 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4752 TRACE(":failed (%d)\n",GetLastError());
4753 msvcrt_set_errno(GetLastError());
4757 /*********************************************************************
4758 * setvbuf (MSVCRT.@)
4760 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4762 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4763 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4764 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4766 MSVCRT__lock_file(file
);
4768 MSVCRT__fflush_nolock(file
);
4769 if(file
->_flag
& MSVCRT__IOMYBUF
)
4770 MSVCRT_free(file
->_base
);
4771 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4774 if(mode
== MSVCRT__IONBF
) {
4775 file
->_flag
|= MSVCRT__IONBF
;
4776 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4779 file
->_base
= file
->_ptr
= buf
;
4780 file
->_flag
|= MSVCRT__USERBUF
;
4781 file
->_bufsiz
= size
;
4783 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4786 MSVCRT__unlock_file(file
);
4790 file
->_flag
|= MSVCRT__IOMYBUF
;
4791 file
->_bufsiz
= size
;
4793 MSVCRT__unlock_file(file
);
4797 /*********************************************************************
4800 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4802 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4805 static int tmpnam_helper(char *s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
4811 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
4815 *MSVCRT__errno() = MSVCRT_ERANGE
;
4816 return MSVCRT_ERANGE
;
4821 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4822 if (digits
+1 > size
) {
4824 *MSVCRT__errno() = MSVCRT_ERANGE
;
4825 return MSVCRT_ERANGE
;
4827 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4833 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4834 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4838 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4839 if (digits
+1 > size
) {
4841 *MSVCRT__errno() = MSVCRT_ERANGE
;
4842 return MSVCRT_ERANGE
;
4844 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4847 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4848 GetLastError() == ERROR_FILE_NOT_FOUND
)
4854 int CDECL
MSVCRT_tmpnam_s(char *s
, MSVCRT_size_t size
)
4856 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
4859 /*********************************************************************
4862 char * CDECL
MSVCRT_tmpnam(char *s
)
4865 thread_data_t
*data
= msvcrt_get_thread_data();
4867 if(!data
->tmpnam_buffer
)
4868 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4870 s
= data
->tmpnam_buffer
;
4873 return tmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
4876 static int wtmpnam_helper(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
4878 MSVCRT_wchar_t tmpstr
[8];
4879 MSVCRT_wchar_t
*p
= s
;
4882 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
4886 *MSVCRT__errno() = MSVCRT_ERANGE
;
4887 return MSVCRT_ERANGE
;
4892 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4893 if (digits
+1 > size
) {
4895 *MSVCRT__errno() = MSVCRT_ERANGE
;
4896 return MSVCRT_ERANGE
;
4898 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4904 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4905 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4909 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
4910 if (digits
+1 > size
) {
4912 *MSVCRT__errno() = MSVCRT_ERANGE
;
4913 return MSVCRT_ERANGE
;
4915 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4918 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4919 GetLastError() == ERROR_FILE_NOT_FOUND
)
4925 /*********************************************************************
4926 * _wtmpnam_s (MSVCRT.@)
4928 int CDECL
MSVCRT__wtmpnam_s(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
)
4930 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
4933 /*********************************************************************
4934 * _wtmpnam (MSVCRT.@)
4936 MSVCRT_wchar_t
* CDECL
MSVCRT__wtmpnam(MSVCRT_wchar_t
*s
)
4939 thread_data_t
*data
= msvcrt_get_thread_data();
4941 if(!data
->wtmpnam_buffer
)
4942 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4944 s
= data
->wtmpnam_buffer
;
4947 return wtmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
4950 /*********************************************************************
4951 * tmpfile (MSVCRT.@)
4953 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4955 char *filename
= MSVCRT_tmpnam(NULL
);
4957 MSVCRT_FILE
* file
= NULL
;
4960 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4961 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4962 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4964 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4969 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4972 if(fd
!= -1 && !file
)
4978 /*********************************************************************
4979 * tmpfile_s (MSVCRT.@)
4981 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4983 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4985 *file
= MSVCRT_tmpfile();
4989 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4991 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4994 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4998 MSVCRT__lock_file(file
);
5000 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5001 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
5002 MSVCRT__unlock_file(file
);
5006 for(i
=0; i
<len
; i
++) {
5007 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
5008 MSVCRT__unlock_file(file
);
5013 MSVCRT__unlock_file(file
);
5017 /*********************************************************************
5018 * vfprintf (MSVCRT.@)
5020 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5025 MSVCRT__lock_file(file
);
5026 tmp_buf
= add_std_buffer(file
);
5027 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, 0, arg_clbk_valist
, NULL
, &valist
);
5028 if(tmp_buf
) remove_std_buffer(file
);
5029 MSVCRT__unlock_file(file
);
5034 /*********************************************************************
5035 * vfprintf_s (MSVCRT.@)
5037 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5042 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
5044 MSVCRT__lock_file(file
);
5045 tmp_buf
= add_std_buffer(file
);
5046 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
,
5047 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, arg_clbk_valist
, NULL
, &valist
);
5048 if(tmp_buf
) remove_std_buffer(file
);
5049 MSVCRT__unlock_file(file
);
5054 /*********************************************************************
5055 * vfwprintf (MSVCRT.@)
5057 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5062 MSVCRT__lock_file(file
);
5063 tmp_buf
= add_std_buffer(file
);
5064 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, 0, arg_clbk_valist
, NULL
, &valist
);
5065 if(tmp_buf
) remove_std_buffer(file
);
5066 MSVCRT__unlock_file(file
);
5071 /*********************************************************************
5072 * vfwprintf_s (MSVCRT.@)
5074 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5079 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5081 MSVCRT__lock_file(file
);
5082 tmp_buf
= add_std_buffer(file
);
5083 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
,
5084 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, arg_clbk_valist
, NULL
, &valist
);
5085 if(tmp_buf
) remove_std_buffer(file
);
5086 MSVCRT__unlock_file(file
);
5091 /*********************************************************************
5092 * __stdio_common_vfprintf (MSVCRT.@)
5094 int CDECL
MSVCRT__stdio_common_vfprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5095 MSVCRT__locale_t locale
, __ms_va_list valist
)
5100 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5102 MSVCRT__lock_file(file
);
5103 tmp_buf
= add_std_buffer(file
);
5105 if (options
& ~UCRTBASE_PRINTF_MASK
)
5106 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5107 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
,
5108 options
& UCRTBASE_PRINTF_MASK
, arg_clbk_valist
, NULL
, &valist
);
5109 if(tmp_buf
) remove_std_buffer(file
);
5110 MSVCRT__unlock_file(file
);
5115 /*********************************************************************
5116 * __stdio_common_vfwprintf (MSVCRT.@)
5118 int CDECL
MSVCRT__stdio_common_vfwprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5119 MSVCRT__locale_t locale
, __ms_va_list valist
)
5124 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5125 if (!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5127 MSVCRT__lock_file(file
);
5128 tmp_buf
= add_std_buffer(file
);
5130 if (options
& ~UCRTBASE_PRINTF_MASK
)
5131 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5133 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
,
5134 options
& UCRTBASE_PRINTF_MASK
, arg_clbk_valist
, NULL
, &valist
);
5136 if(tmp_buf
) remove_std_buffer(file
);
5137 MSVCRT__unlock_file(file
);
5143 /*********************************************************************
5144 * _vfwprintf_l (MSVCRT.@)
5146 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5147 MSVCRT__locale_t locale
, __ms_va_list valist
)
5152 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5154 MSVCRT__lock_file(file
);
5155 tmp_buf
= add_std_buffer(file
);
5156 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, 0, arg_clbk_valist
, NULL
, &valist
);
5157 if(tmp_buf
) remove_std_buffer(file
);
5158 MSVCRT__unlock_file(file
);
5163 /*********************************************************************
5164 * vprintf (MSVCRT.@)
5166 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5168 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5171 /*********************************************************************
5172 * vprintf_s (MSVCRT.@)
5174 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5176 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5179 /*********************************************************************
5180 * vwprintf (MSVCRT.@)
5182 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5184 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5187 /*********************************************************************
5188 * vwprintf_s (MSVCRT.@)
5190 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5192 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5195 /*********************************************************************
5196 * fprintf (MSVCRT.@)
5198 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5200 __ms_va_list valist
;
5202 __ms_va_start(valist
, format
);
5203 res
= MSVCRT_vfprintf(file
, format
, valist
);
5204 __ms_va_end(valist
);
5208 /*********************************************************************
5209 * fprintf_s (MSVCRT.@)
5211 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5213 __ms_va_list valist
;
5215 __ms_va_start(valist
, format
);
5216 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5217 __ms_va_end(valist
);
5221 /*********************************************************************
5222 * fwprintf (MSVCRT.@)
5224 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5226 __ms_va_list valist
;
5228 __ms_va_start(valist
, format
);
5229 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5230 __ms_va_end(valist
);
5234 /*********************************************************************
5235 * fwprintf_s (MSVCRT.@)
5237 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5239 __ms_va_list valist
;
5241 __ms_va_start(valist
, format
);
5242 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5243 __ms_va_end(valist
);
5247 /*********************************************************************
5248 * _fwprintf_l (MSVCRT.@)
5250 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5252 __ms_va_list valist
;
5254 __ms_va_start(valist
, locale
);
5255 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5256 __ms_va_end(valist
);
5260 /*********************************************************************
5263 int CDECL
MSVCRT_printf(const char *format
, ...)
5265 __ms_va_list valist
;
5267 __ms_va_start(valist
, format
);
5268 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5269 __ms_va_end(valist
);
5273 /*********************************************************************
5274 * printf_s (MSVCRT.@)
5276 int CDECL
MSVCRT_printf_s(const char *format
, ...)
5278 __ms_va_list valist
;
5280 __ms_va_start(valist
, format
);
5281 res
= MSVCRT_vprintf_s(format
, valist
);
5282 __ms_va_end(valist
);
5286 /*********************************************************************
5289 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5293 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5295 MSVCRT__lock_file(file
);
5296 ret
= MSVCRT__ungetc_nolock(c
, file
);
5297 MSVCRT__unlock_file(file
);
5302 /*********************************************************************
5303 * _ungetc_nolock (MSVCRT.@)
5305 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5307 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5309 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5310 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5313 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5314 && msvcrt_alloc_buffer(file
))
5315 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5318 if(file
->_ptr
>file
->_base
) {
5320 if(file
->_flag
& MSVCRT__IOSTRG
) {
5321 if(*file
->_ptr
!= c
) {
5329 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5330 file
->_flag
|= MSVCRT__IOREAD
;
5337 /*********************************************************************
5338 * ungetwc (MSVCRT.@)
5340 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5344 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5346 MSVCRT__lock_file(file
);
5347 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5348 MSVCRT__unlock_file(file
);
5353 /*********************************************************************
5354 * _ungetwc_nolock (MSVCRT.@)
5356 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5358 MSVCRT_wchar_t mwc
= wc
;
5360 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5361 if (wc
== MSVCRT_WEOF
)
5364 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5365 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5366 unsigned char * pp
= (unsigned char *)&mwc
;
5369 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5370 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5374 char mbs
[MSVCRT_MB_LEN_MAX
];
5377 len
= MSVCRT_wctomb(mbs
, mwc
);
5381 for(len
--; len
>=0; len
--) {
5382 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5390 /*********************************************************************
5391 * wprintf (MSVCRT.@)
5393 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5395 __ms_va_list valist
;
5397 __ms_va_start(valist
, format
);
5398 res
= MSVCRT_vwprintf(format
, valist
);
5399 __ms_va_end(valist
);
5403 /*********************************************************************
5404 * wprintf_s (MSVCRT.@)
5406 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5408 __ms_va_list valist
;
5410 __ms_va_start(valist
, format
);
5411 res
= MSVCRT_vwprintf_s(format
, valist
);
5412 __ms_va_end(valist
);
5416 /*********************************************************************
5417 * _getmaxstdio (MSVCRT.@)
5419 int CDECL
MSVCRT__getmaxstdio(void)
5421 return MSVCRT_max_streams
;
5424 /*********************************************************************
5425 * _setmaxstdio (MSVCRT.@)
5427 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5429 TRACE("%d\n", newmax
);
5431 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5434 MSVCRT_max_streams
= newmax
;
5435 return MSVCRT_max_streams
;
5438 /*********************************************************************
5439 * _get_stream_buffer_pointers (UCRTBASE.@)
5441 int CDECL
MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE
*file
, char*** base
,
5442 char*** ptr
, int** count
)
5445 *base
= &file
->_base
;
5449 *count
= &file
->_cnt
;