2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READNL 0x04 /* read started with \n */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
71 /* values for exflag - it's used differently in msvcr90.dll*/
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom
[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
92 CRITICAL_SECTION crit
;
98 BOOL utf8translations
;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
119 CRITICAL_SECTION crit
;
122 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
123 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
124 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
126 /* INTERNAL: process umask */
127 static int MSVCRT_umask
= 0;
129 /* INTERNAL: static data for tmpnam and _wtmpname functions */
130 static int tmpnam_unique
;
131 static int tmpnam_s_unique
;
133 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
134 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
135 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
136 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
138 #define TOUL(x) (ULONGLONG)(x)
139 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
140 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
141 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
142 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
144 /* This critical section protects the MSVCRT_fstreams table
145 * and MSVCRT_stream_idx from race conditions. It also
146 * protects fd critical sections creation code.
148 static CRITICAL_SECTION MSVCRT_file_cs
;
149 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
151 0, 0, &MSVCRT_file_cs
,
152 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
153 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
155 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
156 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
157 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
159 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
161 buf
->st_dev
= buf64
->st_dev
;
162 buf
->st_ino
= buf64
->st_ino
;
163 buf
->st_mode
= buf64
->st_mode
;
164 buf
->st_nlink
= buf64
->st_nlink
;
165 buf
->st_uid
= buf64
->st_uid
;
166 buf
->st_gid
= buf64
->st_gid
;
167 buf
->st_rdev
= buf64
->st_rdev
;
168 buf
->st_size
= buf64
->st_size
;
169 buf
->st_atime
= buf64
->st_atime
;
170 buf
->st_mtime
= buf64
->st_mtime
;
171 buf
->st_ctime
= buf64
->st_ctime
;
174 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
176 buf
->st_dev
= buf64
->st_dev
;
177 buf
->st_ino
= buf64
->st_ino
;
178 buf
->st_mode
= buf64
->st_mode
;
179 buf
->st_nlink
= buf64
->st_nlink
;
180 buf
->st_uid
= buf64
->st_uid
;
181 buf
->st_gid
= buf64
->st_gid
;
182 buf
->st_rdev
= buf64
->st_rdev
;
183 buf
->st_size
= buf64
->st_size
;
184 buf
->st_atime
= buf64
->st_atime
;
185 buf
->st_mtime
= buf64
->st_mtime
;
186 buf
->st_ctime
= buf64
->st_ctime
;
189 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
191 buf
->st_dev
= buf64
->st_dev
;
192 buf
->st_ino
= buf64
->st_ino
;
193 buf
->st_mode
= buf64
->st_mode
;
194 buf
->st_nlink
= buf64
->st_nlink
;
195 buf
->st_uid
= buf64
->st_uid
;
196 buf
->st_gid
= buf64
->st_gid
;
197 buf
->st_rdev
= buf64
->st_rdev
;
198 buf
->st_size
= buf64
->st_size
;
199 buf
->st_atime
= buf64
->st_atime
;
200 buf
->st_mtime
= buf64
->st_mtime
;
201 buf
->st_ctime
= buf64
->st_ctime
;
204 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
206 buf
->st_dev
= buf64
->st_dev
;
207 buf
->st_ino
= buf64
->st_ino
;
208 buf
->st_mode
= buf64
->st_mode
;
209 buf
->st_nlink
= buf64
->st_nlink
;
210 buf
->st_uid
= buf64
->st_uid
;
211 buf
->st_gid
= buf64
->st_gid
;
212 buf
->st_rdev
= buf64
->st_rdev
;
213 buf
->st_size
= buf64
->st_size
;
214 buf
->st_atime
= buf64
->st_atime
;
215 buf
->st_mtime
= buf64
->st_mtime
;
216 buf
->st_ctime
= buf64
->st_ctime
;
219 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
221 buf
->st_dev
= buf64
->st_dev
;
222 buf
->st_ino
= buf64
->st_ino
;
223 buf
->st_mode
= buf64
->st_mode
;
224 buf
->st_nlink
= buf64
->st_nlink
;
225 buf
->st_uid
= buf64
->st_uid
;
226 buf
->st_gid
= buf64
->st_gid
;
227 buf
->st_rdev
= buf64
->st_rdev
;
228 buf
->st_size
= buf64
->st_size
;
229 buf
->st_atime
= buf64
->st_atime
;
230 buf
->st_mtime
= buf64
->st_mtime
;
231 buf
->st_ctime
= buf64
->st_ctime
;
234 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
236 /* 1601 to 1970 is 369 years plus 89 leap days */
237 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
239 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
240 ft
->dwHighDateTime
= ticks
>> 32;
241 ft
->dwLowDateTime
= ticks
;
244 static inline ioinfo
* get_ioinfo_nolock(int fd
)
247 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
248 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
250 return &MSVCRT___badioinfo
;
252 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
255 static inline void init_ioinfo_cs(ioinfo
*info
)
257 if(!(info
->exflag
& EF_CRIT_INIT
)) {
259 if(!(info
->exflag
& EF_CRIT_INIT
)) {
260 InitializeCriticalSection(&info
->crit
);
261 info
->exflag
|= EF_CRIT_INIT
;
267 static inline ioinfo
* get_ioinfo(int fd
)
269 ioinfo
*ret
= get_ioinfo_nolock(fd
);
270 if(ret
== &MSVCRT___badioinfo
)
273 EnterCriticalSection(&ret
->crit
);
277 static inline BOOL
alloc_pioinfo_block(int fd
)
282 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
284 *MSVCRT__errno() = MSVCRT_ENFILE
;
288 block
= MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
291 WARN(":out of memory!\n");
292 *MSVCRT__errno() = MSVCRT_ENOMEM
;
295 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
296 block
[i
].handle
= INVALID_HANDLE_VALUE
;
297 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
302 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
306 ret
= get_ioinfo(fd
);
307 if(ret
!= &MSVCRT___badioinfo
)
310 if(!alloc_pioinfo_block(fd
))
311 return &MSVCRT___badioinfo
;
313 return get_ioinfo(fd
);
316 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
321 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
323 ioinfo
*info
= get_ioinfo_nolock(i
);
325 if(info
== &MSVCRT___badioinfo
)
327 if(!alloc_pioinfo_block(i
))
328 return &MSVCRT___badioinfo
;
329 info
= get_ioinfo_nolock(i
);
332 init_ioinfo_cs(info
);
333 if(TryEnterCriticalSection(&info
->crit
))
335 if(info
->handle
== INVALID_HANDLE_VALUE
)
340 LeaveCriticalSection(&info
->crit
);
344 WARN(":files exhausted!\n");
345 *MSVCRT__errno() = MSVCRT_ENFILE
;
346 return &MSVCRT___badioinfo
;
349 static inline void release_ioinfo(ioinfo
*info
)
351 if(info
!=&MSVCRT___badioinfo
&& info
->exflag
& EF_CRIT_INIT
)
352 LeaveCriticalSection(&info
->crit
);
355 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
359 if(i
>= MSVCRT_max_streams
)
363 return &MSVCRT__iob
[i
];
365 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
367 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
368 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
369 ERR("out of memory\n");
370 *MSVCRT__errno() = MSVCRT_ENOMEM
;
374 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
376 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
381 /* INTERNAL: free a file entry fd */
382 static void msvcrt_free_fd(int fd
)
384 ioinfo
*fdinfo
= get_ioinfo(fd
);
386 if(fdinfo
!= &MSVCRT___badioinfo
)
388 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
391 TRACE(":fd (%d) freed\n",fd
);
398 SetStdHandle(STD_INPUT_HANDLE
, 0);
401 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
404 SetStdHandle(STD_ERROR_HANDLE
, 0);
408 release_ioinfo(fdinfo
);
411 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
413 fdinfo
->handle
= hand
;
414 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
415 fdinfo
->lookahead
[0] = '\n';
416 fdinfo
->lookahead
[1] = '\n';
417 fdinfo
->lookahead
[2] = '\n';
418 fdinfo
->exflag
&= EF_CRIT_INIT
;
420 switch (fdinfo
-MSVCRT___pioinfo
[0])
422 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
423 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
424 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
428 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
429 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
432 ioinfo
*info
= get_ioinfo_alloc(&fd
);
434 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
436 if(info
== &MSVCRT___badioinfo
)
439 msvcrt_set_fd(info
, hand
, flag
);
440 release_ioinfo(info
);
444 /* INTERNAL: Allocate a FILE* for an fd slot */
445 /* caller must hold the files lock */
446 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
451 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
453 file
= msvcrt_get_file(i
);
457 if (file
->_flag
== 0)
459 if (i
== MSVCRT_stream_idx
)
461 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
463 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
464 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
475 /* INTERNAL: initialize a FILE* from an open fd */
476 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
478 TRACE(":fd (%d) allocating FILE*\n",fd
);
479 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
481 WARN(":invalid fd %d\n",fd
);
482 *MSVCRT___doserrno() = 0;
483 *MSVCRT__errno() = MSVCRT_EBADF
;
486 file
->_ptr
= file
->_base
= NULL
;
489 file
->_flag
= stream_flags
;
490 file
->_tmpfname
= NULL
;
492 TRACE(":got FILE* (%p)\n",file
);
496 /* INTERNAL: Create an inheritance data block (for spawned process)
497 * The inheritance block is made of:
498 * 00 int nb of file descriptor (NBFD)
499 * 04 char file flags (wxflag): repeated for each fd
500 * 4+NBFD HANDLE file handle: repeated for each fd
502 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
509 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
510 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
514 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
515 *block
= MSVCRT_calloc(1, *size
);
521 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
522 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
524 *(unsigned*)*block
= last_fd
;
525 for (fd
= 0; fd
< last_fd
; fd
++)
527 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
528 fdinfo
= get_ioinfo(fd
);
529 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
531 *wxflag_ptr
= fdinfo
->wxflag
;
532 *handle_ptr
= fdinfo
->handle
;
537 *handle_ptr
= INVALID_HANDLE_VALUE
;
539 release_ioinfo(fdinfo
);
540 wxflag_ptr
++; handle_ptr
++;
545 /* INTERNAL: Set up all file descriptors,
546 * as well as default streams (stdin, stderr and stdout)
548 void msvcrt_init_io(void)
554 GetStartupInfoA(&si
);
555 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
561 count
= *(unsigned*)si
.lpReserved2
;
562 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
563 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
565 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
566 count
= min(count
, MSVCRT_MAX_FILES
);
567 for (i
= 0; i
< count
; i
++)
569 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
571 fdinfo
= get_ioinfo_alloc_fd(i
);
572 if(fdinfo
!= &MSVCRT___badioinfo
)
573 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
574 release_ioinfo(fdinfo
);
577 wxflag_ptr
++; handle_ptr
++;
581 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDIN_FILENO
);
582 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
583 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
584 DWORD flags
= WX_OPEN
| WX_TEXT
;
585 DWORD type
= GetFileType(h
);
587 if (type
== FILE_TYPE_UNKNOWN
) {
588 h
= MSVCRT_NO_CONSOLE
;
590 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
592 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
596 msvcrt_set_fd(fdinfo
, h
, flags
);
598 release_ioinfo(fdinfo
);
600 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO
);
601 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
602 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
603 DWORD flags
= WX_OPEN
| WX_TEXT
;
604 DWORD type
= GetFileType(h
);
606 if (type
== FILE_TYPE_UNKNOWN
) {
607 h
= MSVCRT_NO_CONSOLE
;
609 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
611 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
615 msvcrt_set_fd(fdinfo
, h
, flags
);
617 release_ioinfo(fdinfo
);
619 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO
);
620 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
621 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
622 DWORD flags
= WX_OPEN
| WX_TEXT
;
623 DWORD type
= GetFileType(h
);
625 if (type
== FILE_TYPE_UNKNOWN
) {
626 h
= MSVCRT_NO_CONSOLE
;
628 } else if ((type
& 0xf) == FILE_TYPE_CHAR
) {
630 } else if ((type
& 0xf) == FILE_TYPE_PIPE
) {
634 msvcrt_set_fd(fdinfo
, h
, flags
);
636 release_ioinfo(fdinfo
);
638 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
639 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
640 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
642 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
643 for (i
= 0; i
< 3; i
++)
645 /* FILE structs for stdin/out/err are static and never deleted */
646 MSVCRT__iob
[i
]._file
= get_ioinfo_nolock(i
)->handle
== MSVCRT_NO_CONSOLE
?
647 MSVCRT_NO_CONSOLE_FD
: i
;
648 MSVCRT__iob
[i
]._tmpfname
= NULL
;
649 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
651 MSVCRT_stream_idx
= 3;
654 /* INTERNAL: Flush stdio file buffer */
655 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
657 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
658 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
659 int cnt
=file
->_ptr
-file
->_base
;
660 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
661 file
->_flag
|= MSVCRT__IOERR
;
665 if(file
->_flag
& MSVCRT__IORW
)
666 file
->_flag
&= ~MSVCRT__IOWRT
;
669 file
->_ptr
=file
->_base
;
674 /*********************************************************************
677 int CDECL
MSVCRT__isatty(int fd
)
679 TRACE(":fd (%d)\n",fd
);
681 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
684 /* INTERNAL: Allocate stdio file buffer */
685 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
687 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
688 && MSVCRT__isatty(file
->_file
))
691 file
->_base
= MSVCRT_calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
693 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
694 file
->_flag
|= MSVCRT__IOMYBUF
;
696 file
->_base
= (char*)(&file
->_charbuf
);
698 file
->_flag
|= MSVCRT__IONBF
;
700 file
->_ptr
= file
->_base
;
705 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
706 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
708 static char buffers
[2][MSVCRT_BUFSIZ
];
710 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
711 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
712 || !MSVCRT__isatty(file
->_file
))
715 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
716 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
717 file
->_flag
|= MSVCRT__USERBUF
;
721 /* INTERNAL: Removes temporary buffer from stdout or stderr */
722 /* Only call this function when add_std_buffer returned TRUE */
723 static void remove_std_buffer(MSVCRT_FILE
*file
)
725 msvcrt_flush_buffer(file
);
726 file
->_ptr
= file
->_base
= NULL
;
727 file
->_bufsiz
= file
->_cnt
= 0;
728 file
->_flag
&= ~MSVCRT__USERBUF
;
731 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
732 static int msvcrt_int_to_base32(int num
, char *str
)
747 *p
= (num
& 31) + '0';
749 *p
+= ('a' - '0' - 10);
756 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
757 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
772 *p
= (num
& 31) + '0';
774 *p
+= ('a' - '0' - 10);
781 /*********************************************************************
782 * __iob_func(MSVCRT.@)
784 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
786 return &MSVCRT__iob
[0];
789 #if _MSVCR_VER >= 140
790 /*********************************************************************
791 * __acrt_iob_func(UCRTBASE.@)
793 MSVCRT_FILE
* CDECL
MSVCRT___acrt_iob_func(unsigned idx
)
795 return &MSVCRT__iob
[idx
];
799 /*********************************************************************
802 int CDECL
MSVCRT__access(const char *filename
, int mode
)
804 DWORD attr
= GetFileAttributesA(filename
);
806 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
808 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
810 msvcrt_set_errno(GetLastError());
813 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
815 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
821 /*********************************************************************
822 * _access_s (MSVCRT.@)
824 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
826 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
827 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
829 if (MSVCRT__access(filename
, mode
) == -1)
830 return *MSVCRT__errno();
834 /*********************************************************************
835 * _waccess (MSVCRT.@)
837 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
839 DWORD attr
= GetFileAttributesW(filename
);
841 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
843 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
845 msvcrt_set_errno(GetLastError());
848 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
850 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
856 /*********************************************************************
857 * _waccess_s (MSVCRT.@)
859 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
861 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
862 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
864 if (MSVCRT__waccess(filename
, mode
) == -1)
865 return *MSVCRT__errno();
869 /*********************************************************************
872 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
874 DWORD oldFlags
= GetFileAttributesA(path
);
876 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
878 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
879 oldFlags
| FILE_ATTRIBUTE_READONLY
;
881 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
884 msvcrt_set_errno(GetLastError());
888 /*********************************************************************
891 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
893 DWORD oldFlags
= GetFileAttributesW(path
);
895 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
897 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
898 oldFlags
| FILE_ATTRIBUTE_READONLY
;
900 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
903 msvcrt_set_errno(GetLastError());
907 /*********************************************************************
910 int CDECL
MSVCRT__unlink(const char *path
)
912 TRACE("%s\n",debugstr_a(path
));
913 if(DeleteFileA(path
))
915 TRACE("failed (%d)\n",GetLastError());
916 msvcrt_set_errno(GetLastError());
920 /*********************************************************************
921 * _wunlink (MSVCRT.@)
923 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
925 TRACE("(%s)\n",debugstr_w(path
));
926 if(DeleteFileW(path
))
928 TRACE("failed (%d)\n",GetLastError());
929 msvcrt_set_errno(GetLastError());
933 /*********************************************************************
936 int CDECL
MSVCRT__commit(int fd
)
938 ioinfo
*info
= get_ioinfo(fd
);
941 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
943 if (info
->handle
== INVALID_HANDLE_VALUE
)
945 else if (!FlushFileBuffers(info
->handle
))
947 if (GetLastError() == ERROR_INVALID_HANDLE
)
949 /* FlushFileBuffers fails for console handles
950 * so we ignore this error.
956 TRACE(":failed-last error (%d)\n",GetLastError());
957 msvcrt_set_errno(GetLastError());
967 release_ioinfo(info
);
971 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
972 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
974 /* INTERNAL: Flush all stream buffer */
975 static int msvcrt_flush_all_buffers(int mask
)
977 int i
, num_flushed
= 0;
981 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
982 file
= msvcrt_get_file(i
);
986 if(file
->_flag
& mask
) {
994 TRACE(":flushed (%d) handles\n",num_flushed
);
998 /*********************************************************************
999 * _flushall (MSVCRT.@)
1001 int CDECL
MSVCRT__flushall(void)
1003 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
1006 /*********************************************************************
1009 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
1014 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1017 MSVCRT__lock_file(file
);
1018 ret
= MSVCRT__fflush_nolock(file
);
1019 MSVCRT__unlock_file(file
);
1025 /*********************************************************************
1026 * _fflush_nolock (MSVCRT.@)
1028 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
1033 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1037 res
= msvcrt_flush_buffer(file
);
1038 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1039 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
1043 /*********************************************************************
1046 int CDECL
MSVCRT__close(int fd
)
1048 ioinfo
*info
= get_ioinfo(fd
);
1051 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1053 if (fd
== MSVCRT_NO_CONSOLE_FD
) {
1054 *MSVCRT__errno() = MSVCRT_EBADF
;
1056 } else if (!MSVCRT_CHECK_PMT_ERR(info
->wxflag
& WX_OPEN
, MSVCRT_EBADF
)) {
1058 } else if (fd
== MSVCRT_STDOUT_FILENO
&&
1059 info
->handle
== get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
) {
1062 } else if (fd
== MSVCRT_STDERR_FILENO
&&
1063 info
->handle
== get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
) {
1067 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1070 WARN(":failed-last error (%d)\n",GetLastError());
1071 msvcrt_set_errno(GetLastError());
1074 release_ioinfo(info
);
1078 /*********************************************************************
1081 * MSDN isn't clear on this point, but the remarks for _pipe
1082 * indicate file descriptors duplicated with _dup and _dup2 are always
1085 int CDECL
MSVCRT__dup2(int od
, int nd
)
1087 ioinfo
*info_od
, *info_nd
;
1090 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1094 info_od
= get_ioinfo(od
);
1095 info_nd
= get_ioinfo_alloc_fd(nd
);
1099 info_nd
= get_ioinfo_alloc_fd(nd
);
1100 info_od
= get_ioinfo(od
);
1103 if (info_nd
== &MSVCRT___badioinfo
)
1107 else if (info_od
->wxflag
& WX_OPEN
)
1111 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1112 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1114 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1116 if (info_nd
->wxflag
& WX_OPEN
)
1119 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1120 /* _dup2 returns 0, not nd, on success */
1126 msvcrt_set_errno(GetLastError());
1131 *MSVCRT__errno() = MSVCRT_EBADF
;
1135 release_ioinfo(info_od
);
1136 release_ioinfo(info_nd
);
1140 /*********************************************************************
1143 int CDECL
MSVCRT__dup(int od
)
1146 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1148 if (MSVCRT__dup2(od
, fd
) == 0)
1152 release_ioinfo(info
);
1156 /*********************************************************************
1159 int CDECL
MSVCRT__eof(int fd
)
1161 ioinfo
*info
= get_ioinfo(fd
);
1162 DWORD curpos
,endpos
;
1163 LONG hcurpos
,hendpos
;
1165 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1167 if (info
->handle
== INVALID_HANDLE_VALUE
)
1169 release_ioinfo(info
);
1173 if (info
->wxflag
& WX_ATEOF
)
1175 release_ioinfo(info
);
1179 /* Otherwise we do it the hard way */
1180 hcurpos
= hendpos
= 0;
1181 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1182 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1184 if (curpos
== endpos
&& hcurpos
== hendpos
)
1186 /* FIXME: shouldn't WX_ATEOF be set here? */
1187 release_ioinfo(info
);
1191 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1192 release_ioinfo(info
);
1196 /*********************************************************************
1197 * _fcloseall (MSVCRT.@)
1199 int CDECL
MSVCRT__fcloseall(void)
1201 int num_closed
= 0, i
;
1205 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1206 file
= msvcrt_get_file(i
);
1208 if (file
->_flag
&& !MSVCRT_fclose(file
))
1213 TRACE(":closed (%d) handles\n",num_closed
);
1217 /* free everything on process exit */
1218 void msvcrt_free_io(void)
1224 MSVCRT__fcloseall();
1226 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1228 if(!MSVCRT___pioinfo
[i
])
1231 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1233 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1234 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1236 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1239 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1241 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1242 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1244 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1245 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1249 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1250 MSVCRT_free(MSVCRT_fstream
[i
]);
1253 /*********************************************************************
1254 * _lseeki64 (MSVCRT.@)
1256 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1258 ioinfo
*info
= get_ioinfo(fd
);
1261 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1263 if (info
->handle
== INVALID_HANDLE_VALUE
)
1265 release_ioinfo(info
);
1269 if (whence
< 0 || whence
> 2)
1271 release_ioinfo(info
);
1272 *MSVCRT__errno() = MSVCRT_EINVAL
;
1276 TRACE(":fd (%d) to %s pos %s\n",
1277 fd
,wine_dbgstr_longlong(offset
),
1278 (whence
==MSVCRT_SEEK_SET
)?"SEEK_SET":
1279 (whence
==MSVCRT_SEEK_CUR
)?"SEEK_CUR":
1280 (whence
==MSVCRT_SEEK_END
)?"SEEK_END":"UNKNOWN");
1282 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1283 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1284 ofs
.QuadPart
= offset
;
1285 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1286 GetLastError() == ERROR_SUCCESS
)
1288 info
->wxflag
&= ~WX_ATEOF
;
1289 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1291 release_ioinfo(info
);
1292 return ofs
.QuadPart
;
1294 release_ioinfo(info
);
1295 TRACE(":error-last error (%d)\n",GetLastError());
1296 msvcrt_set_errno(GetLastError());
1300 /*********************************************************************
1303 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1305 return MSVCRT__lseeki64(fd
, offset
, whence
);
1308 /*********************************************************************
1309 * _lock_file (MSVCRT.@)
1311 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1313 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1314 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1316 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1319 /*********************************************************************
1320 * _unlock_file (MSVCRT.@)
1322 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1324 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1325 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1327 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1330 /*********************************************************************
1331 * _locking (MSVCRT.@)
1333 * This is untested; the underlying LockFile doesn't work yet.
1335 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1337 ioinfo
*info
= get_ioinfo(fd
);
1341 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1342 if (info
->handle
== INVALID_HANDLE_VALUE
)
1344 release_ioinfo(info
);
1348 if (mode
< 0 || mode
> 4)
1350 release_ioinfo(info
);
1351 *MSVCRT__errno() = MSVCRT_EINVAL
;
1355 TRACE(":fd (%d) by 0x%08x mode %s\n",
1356 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1357 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1358 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1359 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1360 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1363 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1365 release_ioinfo(info
);
1366 FIXME ("Seek failed\n");
1367 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1370 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1373 ret
= 1; /* just to satisfy gcc */
1376 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1381 else if (mode
== MSVCRT__LK_UNLCK
)
1382 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1384 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1385 /* FIXME - what about error settings? */
1386 release_ioinfo(info
);
1387 return ret
? 0 : -1;
1390 /*********************************************************************
1391 * _fseeki64 (MSVCRT.@)
1393 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1397 MSVCRT__lock_file(file
);
1398 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1399 MSVCRT__unlock_file(file
);
1404 /*********************************************************************
1405 * _fseeki64_nolock (MSVCRT.@)
1407 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1411 if(whence
== MSVCRT_SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1412 whence
= MSVCRT_SEEK_SET
;
1413 offset
+= MSVCRT__ftelli64_nolock(file
);
1416 /* Flush output if needed */
1417 msvcrt_flush_buffer(file
);
1418 /* Reset direction of i/o */
1419 if(file
->_flag
& MSVCRT__IORW
) {
1420 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1422 /* Clear end of file flag */
1423 file
->_flag
&= ~MSVCRT__IOEOF
;
1424 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1429 /*********************************************************************
1432 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1434 return MSVCRT__fseeki64( file
, offset
, whence
);
1437 /*********************************************************************
1438 * _fseek_nolock (MSVCRT.@)
1440 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1442 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1445 /*********************************************************************
1446 * _chsize_s (MSVCRT.@)
1448 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1454 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1456 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1459 info
= get_ioinfo(fd
);
1460 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1462 /* save the current file pointer */
1463 cur
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1466 pos
= MSVCRT__lseeki64(fd
, size
, MSVCRT_SEEK_SET
);
1469 ret
= SetEndOfFile(info
->handle
);
1470 if (!ret
) msvcrt_set_errno(GetLastError());
1473 /* restore the file pointer */
1474 MSVCRT__lseeki64(fd
, cur
, MSVCRT_SEEK_SET
);
1478 release_ioinfo(info
);
1479 return ret
? 0 : *MSVCRT__errno();
1482 /*********************************************************************
1483 * _chsize (MSVCRT.@)
1485 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1487 /* _chsize_s returns errno on failure but _chsize should return -1 */
1488 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1491 /*********************************************************************
1492 * clearerr (MSVCRT.@)
1494 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1496 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1498 MSVCRT__lock_file(file
);
1499 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1500 MSVCRT__unlock_file(file
);
1503 /*********************************************************************
1504 * clearerr_s (MSVCRT.@)
1506 int CDECL
MSVCRT_clearerr_s(MSVCRT_FILE
* file
)
1508 TRACE(":file (%p)\n",file
);
1510 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
1512 MSVCRT__lock_file(file
);
1513 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1514 MSVCRT__unlock_file(file
);
1518 /*********************************************************************
1521 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1523 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1525 MSVCRT__lock_file(file
);
1526 MSVCRT__fseek_nolock(file
, 0L, MSVCRT_SEEK_SET
);
1527 MSVCRT_clearerr(file
);
1528 MSVCRT__unlock_file(file
);
1531 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1533 int plus
= strchrW(mode
, '+') != NULL
;
1535 TRACE("%s\n", debugstr_w(mode
));
1537 while(*mode
== ' ') mode
++;
1542 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1543 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1546 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1547 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1550 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1551 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1554 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1558 *stream_flags
|= MSVCRT__commode
;
1560 while (*mode
&& *mode
!=',')
1564 *open_flags
|= MSVCRT__O_BINARY
;
1565 *open_flags
&= ~MSVCRT__O_TEXT
;
1568 *open_flags
|= MSVCRT__O_TEXT
;
1569 *open_flags
&= ~MSVCRT__O_BINARY
;
1572 *open_flags
|= MSVCRT__O_TEMPORARY
;
1575 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1578 *stream_flags
|= MSVCRT__IOCOMMIT
;
1581 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1584 *open_flags
|= MSVCRT__O_NOINHERIT
;
1593 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1596 ERR("incorrect mode flag: %c\n", mode
[-1]);
1602 static const WCHAR ccs
[] = {'c','c','s'};
1603 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1604 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1605 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1608 while(*mode
== ' ') mode
++;
1609 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1611 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1612 while(*mode
== ' ') mode
++;
1613 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1616 while(*mode
== ' ') mode
++;
1618 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1620 *open_flags
|= MSVCRT__O_U8TEXT
;
1621 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1623 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1625 *open_flags
|= MSVCRT__O_U16TEXT
;
1626 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1628 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1630 *open_flags
|= MSVCRT__O_WTEXT
;
1631 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1635 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1639 while(*mode
== ' ') mode
++;
1642 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1647 /*********************************************************************
1648 * _fdopen (MSVCRT.@)
1650 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1653 MSVCRT_wchar_t
*modeW
= NULL
;
1655 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1657 ret
= MSVCRT__wfdopen(fd
, modeW
);
1663 /*********************************************************************
1664 * _wfdopen (MSVCRT.@)
1666 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1668 int open_flags
, stream_flags
;
1671 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1674 if (!(file
= msvcrt_alloc_fp()))
1676 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1681 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1687 /*********************************************************************
1688 * _filelength (MSVCRT.@)
1690 LONG CDECL
MSVCRT__filelength(int fd
)
1692 LONG curPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
1695 LONG endPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_END
);
1698 if (endPos
!= curPos
)
1699 MSVCRT__lseek(fd
, curPos
, MSVCRT_SEEK_SET
);
1706 /*********************************************************************
1707 * _filelengthi64 (MSVCRT.@)
1709 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1711 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1714 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_END
);
1717 if (endPos
!= curPos
)
1718 MSVCRT__lseeki64(fd
, curPos
, MSVCRT_SEEK_SET
);
1725 /*********************************************************************
1726 * _fileno (MSVCRT.@)
1728 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1730 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1734 /*********************************************************************
1735 * _fstat64 (MSVCRT.@)
1737 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1739 ioinfo
*info
= get_ioinfo(fd
);
1742 BY_HANDLE_FILE_INFORMATION hfi
;
1744 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1745 if (info
->handle
== INVALID_HANDLE_VALUE
)
1747 release_ioinfo(info
);
1753 WARN(":failed-NULL buf\n");
1754 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1755 release_ioinfo(info
);
1759 memset(&hfi
, 0, sizeof(hfi
));
1760 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1761 type
= GetFileType(info
->handle
);
1762 if (type
== FILE_TYPE_PIPE
)
1764 buf
->st_dev
= buf
->st_rdev
= fd
;
1765 buf
->st_mode
= MSVCRT__S_IFIFO
;
1768 else if (type
== FILE_TYPE_CHAR
)
1770 buf
->st_dev
= buf
->st_rdev
= fd
;
1771 buf
->st_mode
= MSVCRT__S_IFCHR
;
1774 else /* FILE_TYPE_DISK etc. */
1776 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1778 WARN(":failed-last error (%d)\n",GetLastError());
1779 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1780 release_ioinfo(info
);
1783 buf
->st_mode
= MSVCRT__S_IFREG
| 0444;
1784 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1785 buf
->st_mode
|= 0222;
1786 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1787 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1789 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1790 buf
->st_mtime
= buf
->st_ctime
= dw
;
1791 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1793 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1795 release_ioinfo(info
);
1799 /*********************************************************************
1800 * _fstati64 (MSVCRT.@)
1802 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1805 struct MSVCRT__stat64 buf64
;
1807 ret
= MSVCRT__fstat64(fd
, &buf64
);
1809 msvcrt_stat64_to_stati64(&buf64
, buf
);
1813 /*********************************************************************
1816 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1818 struct MSVCRT__stat64 buf64
;
1820 ret
= MSVCRT__fstat64(fd
, &buf64
);
1822 msvcrt_stat64_to_stat(&buf64
, buf
);
1826 /*********************************************************************
1827 * _fstat32 (MSVCR80.@)
1829 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1832 struct MSVCRT__stat64 buf64
;
1834 ret
= MSVCRT__fstat64(fd
, &buf64
);
1836 msvcrt_stat64_to_stat32(&buf64
, buf
);
1840 /*********************************************************************
1841 * _fstat32i64 (MSVCR80.@)
1843 int CDECL
MSVCRT__fstat32i64(int fd
, struct MSVCRT__stat32i64
* buf
)
1846 struct MSVCRT__stat64 buf64
;
1848 ret
= MSVCRT__fstat64(fd
, &buf64
);
1850 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
1854 /*********************************************************************
1855 * _fstat64i32 (MSVCR80.@)
1857 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1860 struct MSVCRT__stat64 buf64
;
1862 ret
= MSVCRT__fstat64(fd
, &buf64
);
1864 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1868 /*********************************************************************
1869 * _futime64 (MSVCRT.@)
1871 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1873 ioinfo
*info
= get_ioinfo(fd
);
1878 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1883 time_to_filetime( t
->actime
, &at
);
1884 time_to_filetime( t
->modtime
, &wt
);
1887 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1889 release_ioinfo(info
);
1890 msvcrt_set_errno(GetLastError());
1893 release_ioinfo(info
);
1897 /*********************************************************************
1898 * _futime32 (MSVCRT.@)
1900 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1904 struct MSVCRT___utimbuf64 t64
;
1905 t64
.actime
= t
->actime
;
1906 t64
.modtime
= t
->modtime
;
1907 return _futime64( fd
, &t64
);
1910 return _futime64( fd
, NULL
);
1913 /*********************************************************************
1914 * _futime (MSVCRT.@)
1917 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1919 return _futime64( fd
, t
);
1922 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1924 return _futime32( fd
, t
);
1928 /*********************************************************************
1929 * _get_osfhandle (MSVCRT.@)
1931 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1933 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1934 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1936 if(hand
== INVALID_HANDLE_VALUE
)
1937 *MSVCRT__errno() = MSVCRT_EBADF
;
1938 return (MSVCRT_intptr_t
)hand
;
1941 /*********************************************************************
1942 * _mktemp_s (MSVCRT.@)
1944 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1948 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1949 return MSVCRT_EINVAL
;
1951 for(len
=0; len
<size
; len
++)
1954 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1957 return MSVCRT_EINVAL
;
1960 for(xno
=1; xno
<=6; xno
++)
1961 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1962 return MSVCRT_EINVAL
;
1964 id
= GetCurrentProcessId();
1965 for(xno
=1; xno
<6; xno
++) {
1966 pattern
[len
-xno
] = id
%10 + '0';
1970 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1971 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1976 *MSVCRT__errno() = MSVCRT_EEXIST
;
1977 return MSVCRT_EEXIST
;
1980 /*********************************************************************
1981 * _mktemp (MSVCRT.@)
1983 char * CDECL
MSVCRT__mktemp(char *pattern
)
1986 char *retVal
= pattern
;
1994 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1998 id
= GetCurrentProcessId();
2002 int tempNum
= id
/ 10;
2003 *pattern
-- = id
- (tempNum
* 10) + '0';
2009 *pattern
= letter
++;
2010 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
2012 } while(letter
<= 'z');
2016 /*********************************************************************
2017 * _wmktemp_s (MSVCRT.@)
2019 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
2023 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
2024 return MSVCRT_EINVAL
;
2026 for(len
=0; len
<size
; len
++)
2029 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
2032 return MSVCRT_EINVAL
;
2035 for(xno
=1; xno
<=6; xno
++)
2036 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
2037 return MSVCRT_EINVAL
;
2039 id
= GetCurrentProcessId();
2040 for(xno
=1; xno
<6; xno
++) {
2041 pattern
[len
-xno
] = id
%10 + '0';
2045 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
2046 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
2051 *MSVCRT__errno() = MSVCRT_EEXIST
;
2052 return MSVCRT_EEXIST
;
2055 /*********************************************************************
2056 * _wmktemp (MSVCRT.@)
2058 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
2061 MSVCRT_wchar_t
*retVal
= pattern
;
2063 MSVCRT_wchar_t letter
= 'a';
2069 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2073 id
= GetCurrentProcessId();
2077 int tempNum
= id
/ 10;
2078 *pattern
-- = id
- (tempNum
* 10) + '0';
2084 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2086 *pattern
= letter
++;
2087 } while(letter
!= '|');
2091 static unsigned split_oflags(unsigned oflags
)
2094 unsigned unsupp
; /* until we support everything */
2096 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2097 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2098 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2099 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2100 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2101 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2102 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2103 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2104 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2106 if ((unsupp
= oflags
& ~(
2107 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2108 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2109 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2110 MSVCRT__O_NOINHERIT
|
2111 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2112 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2114 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2119 /*********************************************************************
2122 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2125 SECURITY_ATTRIBUTES sa
;
2126 HANDLE readHandle
, writeHandle
;
2130 *MSVCRT__errno() = MSVCRT_EINVAL
;
2134 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2135 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2136 sa
.lpSecurityDescriptor
= NULL
;
2137 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2139 unsigned int wxflags
= split_oflags(textmode
);
2142 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2146 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2154 MSVCRT__close(pfds
[0]);
2155 CloseHandle(writeHandle
);
2156 *MSVCRT__errno() = MSVCRT_EMFILE
;
2161 CloseHandle(readHandle
);
2162 CloseHandle(writeHandle
);
2163 *MSVCRT__errno() = MSVCRT_EMFILE
;
2167 msvcrt_set_errno(GetLastError());
2172 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2174 char bom
[sizeof(utf8_bom
)];
2177 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2179 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2182 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2183 oflags
|= MSVCRT__O_U8TEXT
;
2184 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2186 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2187 oflags
|= MSVCRT__O_U16TEXT
;
2189 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2195 /*********************************************************************
2196 * _wsopen_dispatch (UCRTBASE.@)
2198 int CDECL
MSVCRT__wsopen_dispatch( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
,
2199 int *fd
, int secure
)
2201 DWORD access
= 0, creation
= 0, attrib
;
2202 SECURITY_ATTRIBUTES sa
;
2203 DWORD sharing
, type
;
2207 TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
2208 debugstr_w(path
), oflags
, shflags
, pmode
, fd
, secure
);
2210 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2213 wxflag
= split_oflags(oflags
);
2214 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2216 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2217 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2218 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2221 if (oflags
& MSVCRT__O_CREAT
)
2223 if (secure
&& !MSVCRT_CHECK_PMT(!(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))))
2224 return MSVCRT_EINVAL
;
2226 if (oflags
& MSVCRT__O_EXCL
)
2227 creation
= CREATE_NEW
;
2228 else if (oflags
& MSVCRT__O_TRUNC
)
2229 creation
= CREATE_ALWAYS
;
2231 creation
= OPEN_ALWAYS
;
2233 else /* no MSVCRT__O_CREAT */
2235 if (oflags
& MSVCRT__O_TRUNC
)
2236 creation
= TRUNCATE_EXISTING
;
2238 creation
= OPEN_EXISTING
;
2243 case MSVCRT__SH_DENYRW
:
2246 case MSVCRT__SH_DENYWR
:
2247 sharing
= FILE_SHARE_READ
;
2249 case MSVCRT__SH_DENYRD
:
2250 sharing
= FILE_SHARE_WRITE
;
2252 case MSVCRT__SH_DENYNO
:
2253 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2256 ERR( "Unhandled shflags 0x%x\n", shflags
);
2257 return MSVCRT_EINVAL
;
2260 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2261 attrib
= FILE_ATTRIBUTE_READONLY
;
2263 attrib
= FILE_ATTRIBUTE_NORMAL
;
2265 if (oflags
& MSVCRT__O_TEMPORARY
)
2267 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2269 sharing
|= FILE_SHARE_DELETE
;
2272 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2273 sa
.lpSecurityDescriptor
= NULL
;
2274 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2276 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2277 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2278 && !(access
&GENERIC_READ
))
2280 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2281 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2282 if (hand
!= INVALID_HANDLE_VALUE
)
2284 oflags
= check_bom(hand
, oflags
, FALSE
);
2288 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2291 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2292 if (hand
== INVALID_HANDLE_VALUE
) {
2293 WARN(":failed-last error (%d)\n",GetLastError());
2294 msvcrt_set_errno(GetLastError());
2295 return *MSVCRT__errno();
2298 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2300 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2301 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2302 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2304 if (oflags
& MSVCRT__O_U8TEXT
)
2306 DWORD written
= 0, tmp
;
2308 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2309 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2311 if (written
!= sizeof(utf8_bom
)) {
2312 WARN("error writing BOM\n");
2314 msvcrt_set_errno(GetLastError());
2315 return *MSVCRT__errno();
2320 DWORD written
= 0, tmp
;
2322 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2323 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2325 if (written
!= sizeof(utf16_bom
))
2327 WARN("error writing BOM\n");
2329 msvcrt_set_errno(GetLastError());
2330 return *MSVCRT__errno();
2334 else if (access
& GENERIC_READ
)
2335 oflags
= check_bom(hand
, oflags
, TRUE
);
2338 type
= GetFileType(hand
);
2339 if (type
== FILE_TYPE_CHAR
)
2341 else if (type
== FILE_TYPE_PIPE
)
2344 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2346 return *MSVCRT__errno();
2348 if (oflags
& MSVCRT__O_WTEXT
)
2349 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2350 else if (oflags
& MSVCRT__O_U16TEXT
)
2351 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2352 else if (oflags
& MSVCRT__O_U8TEXT
)
2353 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2355 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2360 /*********************************************************************
2361 * _wsopen_s (MSVCRT.@)
2363 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2365 return MSVCRT__wsopen_dispatch( path
, oflags
, shflags
, pmode
, fd
, 1 );
2368 /*********************************************************************
2369 * _wsopen (MSVCRT.@)
2371 int WINAPIV
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2376 if (oflags
& MSVCRT__O_CREAT
)
2380 __ms_va_start(ap
, shflags
);
2381 pmode
= va_arg(ap
, int);
2387 return MSVCRT__wsopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2391 /*********************************************************************
2392 * _sopen_dispatch (UCRTBASE.@)
2394 int CDECL
MSVCRT__sopen_dispatch( const char *path
, int oflags
, int shflags
,
2395 int pmode
, int *fd
, int secure
)
2397 MSVCRT_wchar_t
*pathW
;
2400 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2401 return MSVCRT_EINVAL
;
2403 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2404 return MSVCRT_EINVAL
;
2406 ret
= MSVCRT__wsopen_dispatch(pathW
, oflags
, shflags
, pmode
, fd
, secure
);
2411 /*********************************************************************
2412 * _sopen_s (MSVCRT.@)
2414 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2416 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, fd
, 1);
2419 /*********************************************************************
2422 int WINAPIV
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2427 if (oflags
& MSVCRT__O_CREAT
)
2431 __ms_va_start(ap
, shflags
);
2432 pmode
= va_arg(ap
, int);
2438 return MSVCRT__sopen_dispatch(path
, oflags
, shflags
, pmode
, &fd
, 0) ? -1 : fd
;
2441 /*********************************************************************
2444 int WINAPIV
MSVCRT__open( const char *path
, int flags
, ... )
2448 if (flags
& MSVCRT__O_CREAT
)
2451 __ms_va_start(ap
, flags
);
2452 pmode
= va_arg(ap
, int);
2454 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2457 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2460 /*********************************************************************
2463 int WINAPIV
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2467 if (flags
& MSVCRT__O_CREAT
)
2470 __ms_va_start(ap
, flags
);
2471 pmode
= va_arg(ap
, int);
2473 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2476 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2479 /*********************************************************************
2482 int CDECL
MSVCRT__creat(const char *path
, int pmode
)
2484 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2485 return MSVCRT__open(path
, flags
, pmode
);
2488 /*********************************************************************
2489 * _wcreat (MSVCRT.@)
2491 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int pmode
)
2493 int flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| MSVCRT__O_RDWR
;
2494 return MSVCRT__wopen(path
, flags
, pmode
);
2497 /*********************************************************************
2498 * _open_osfhandle (MSVCRT.@)
2500 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2505 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2506 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2507 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2508 * text - it never sets MSVCRT__O_BINARY.
2510 /* don't let split_oflags() decide the mode if no mode is passed */
2511 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2512 oflags
|= MSVCRT__O_BINARY
;
2514 flags
= GetFileType((HANDLE
)handle
);
2515 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2517 msvcrt_set_errno(GetLastError());
2521 if (flags
== FILE_TYPE_CHAR
)
2523 else if (flags
== FILE_TYPE_PIPE
)
2527 flags
|= split_oflags(oflags
);
2529 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2530 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2534 /*********************************************************************
2537 int CDECL
MSVCRT__rmtmp(void)
2539 int num_removed
= 0, i
;
2543 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2544 file
= msvcrt_get_file(i
);
2546 if (file
->_tmpfname
)
2548 MSVCRT_fclose(file
);
2555 TRACE(":removed (%d) temp files\n",num_removed
);
2559 static inline int get_utf8_char_len(char ch
)
2561 if((ch
&0xf8) == 0xf0)
2563 else if((ch
&0xf0) == 0xe0)
2565 else if((ch
&0xe0) == 0xc0)
2570 /*********************************************************************
2571 * (internal) read_utf8
2573 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2575 HANDLE hand
= fdinfo
->handle
;
2576 char min_buf
[4], *readbuf
, lookahead
;
2577 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2579 /* make the buffer big enough to hold at least one character */
2580 /* read bytes have to fit to output and lookahead buffers */
2582 readbuf_size
= count
< 4 ? 4 : count
;
2583 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2588 if(fdinfo
->lookahead
[0] != '\n') {
2589 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2590 fdinfo
->lookahead
[0] = '\n';
2592 if(fdinfo
->lookahead
[1] != '\n') {
2593 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2594 fdinfo
->lookahead
[1] = '\n';
2596 if(fdinfo
->lookahead
[2] != '\n') {
2597 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2598 fdinfo
->lookahead
[2] = '\n';
2603 /* NOTE: this case is broken in native dll, reading
2604 * sometimes fails when small buffer is passed
2607 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2608 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2609 fdinfo
->wxflag
|= WX_ATEOF
;
2612 msvcrt_set_errno(GetLastError());
2615 }else if(!num_read
) {
2616 fdinfo
->wxflag
|= WX_ATEOF
;
2622 char_len
= get_utf8_char_len(readbuf
[0]);
2624 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2628 if(readbuf
[0] == '\n')
2629 fdinfo
->wxflag
|= WX_READNL
;
2631 fdinfo
->wxflag
&= ~WX_READNL
;
2633 if(readbuf
[0] == 0x1a) {
2634 fdinfo
->wxflag
|= WX_ATEOF
;
2638 if(readbuf
[0] == '\r') {
2639 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2641 else if(lookahead
== '\n')
2645 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2646 fdinfo
->lookahead
[0] = lookahead
;
2648 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2653 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2654 msvcrt_set_errno(GetLastError());
2661 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2664 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2665 fdinfo
->wxflag
|= WX_ATEOF
;
2666 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2669 msvcrt_set_errno(GetLastError());
2670 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2673 }else if(!pos
&& !num_read
) {
2674 fdinfo
->wxflag
|= WX_ATEOF
;
2675 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2680 if(readbuf
[0] == '\n')
2681 fdinfo
->wxflag
|= WX_READNL
;
2683 fdinfo
->wxflag
&= ~WX_READNL
;
2685 /* Find first byte of last character (may be incomplete) */
2686 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2687 if((readbuf
[i
]&0xc0) != 0x80)
2689 char_len
= get_utf8_char_len(readbuf
[i
]);
2690 if(char_len
+i
<= pos
)
2693 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2695 fdinfo
->lookahead
[0] = readbuf
[i
];
2697 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2699 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2701 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2705 for(i
=0, j
=0; i
<pos
; i
++) {
2706 if(readbuf
[i
] == 0x1a) {
2707 fdinfo
->wxflag
|= WX_ATEOF
;
2711 /* strip '\r' if followed by '\n' */
2712 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2713 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2714 readbuf
[j
++] = '\r';
2715 }else if(lookahead
== '\n' && j
==0) {
2716 readbuf
[j
++] = '\n';
2718 if(lookahead
!= '\n')
2719 readbuf
[j
++] = '\r';
2721 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2722 fdinfo
->lookahead
[0] = lookahead
;
2724 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2726 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2727 readbuf
[j
++] = readbuf
[i
];
2732 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2733 msvcrt_set_errno(GetLastError());
2734 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2738 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2742 /*********************************************************************
2745 * When reading \r as last character in text mode, read() positions
2746 * the file pointer on the \r character while getc() goes on to
2749 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2751 DWORD num_read
, utf16
;
2752 char *bufstart
= buf
;
2757 if (fdinfo
->wxflag
& WX_ATEOF
) {
2758 TRACE("already at EOF, returning 0\n");
2761 /* Don't trace small reads, it gets *very* annoying */
2763 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2764 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2766 *MSVCRT__errno() = MSVCRT_EBADF
;
2770 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2771 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2773 *MSVCRT__errno() = MSVCRT_EINVAL
;
2777 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2778 return read_utf8(fdinfo
, buf
, count
);
2780 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2782 if (fdinfo
->lookahead
[0] != '\n')
2784 bufstart
[0] = fdinfo
->lookahead
[0];
2785 fdinfo
->lookahead
[0] = '\n';
2789 bufstart
[1] = fdinfo
->lookahead
[1];
2790 fdinfo
->lookahead
[1] = '\n';
2793 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2794 num_read
+= 1+utf16
;
2799 if(utf16
&& (num_read
&1))
2801 /* msvcr90 uses uninitialized value from the buffer in this case */
2802 /* msvcrt ignores additional data */
2803 ERR("got odd number of bytes in UTF16 mode\n");
2807 if (count
!= 0 && num_read
== 0)
2809 fdinfo
->wxflag
|= WX_ATEOF
;
2810 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2812 else if (fdinfo
->wxflag
& WX_TEXT
)
2816 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2817 fdinfo
->wxflag
|= WX_READNL
;
2819 fdinfo
->wxflag
&= ~WX_READNL
;
2821 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2823 /* in text mode, a ctrl-z signals EOF */
2824 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2826 fdinfo
->wxflag
|= WX_ATEOF
;
2827 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2831 /* in text mode, strip \r if followed by \n */
2832 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2837 lookahead
[1] = '\n';
2838 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2840 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2842 bufstart
[j
++] = '\n';
2843 if(utf16
) bufstart
[j
++] = 0;
2847 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2849 bufstart
[j
++] = '\r';
2850 if(utf16
) bufstart
[j
++] = 0;
2853 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2855 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2857 bufstart
[j
++] = '\n';
2858 if (utf16
) bufstart
[j
++] = 0;
2862 fdinfo
->lookahead
[0] = lookahead
[0];
2863 fdinfo
->lookahead
[1] = lookahead
[1];
2867 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2872 bufstart
[j
++] = '\r';
2873 if(utf16
) bufstart
[j
++] = 0;
2876 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2877 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2879 bufstart
[j
++] = bufstart
[i
];
2880 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2888 if (GetLastError() == ERROR_BROKEN_PIPE
)
2890 TRACE(":end-of-pipe\n");
2891 fdinfo
->wxflag
|= WX_ATEOF
;
2896 TRACE(":failed-last error (%d)\n",GetLastError());
2897 msvcrt_set_errno(GetLastError());
2903 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2907 /*********************************************************************
2910 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2915 if(fd
== MSVCRT_NO_CONSOLE_FD
) {
2916 *MSVCRT__errno() = MSVCRT_EBADF
;
2920 info
= get_ioinfo(fd
);
2921 num_read
= read_i(fd
, info
, buf
, count
);
2922 release_ioinfo(info
);
2926 /*********************************************************************
2927 * _setmode (MSVCRT.@)
2929 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2931 ioinfo
*info
= get_ioinfo(fd
);
2932 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2933 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2934 ret
= MSVCRT__O_WTEXT
;
2936 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2937 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2938 *MSVCRT__errno() = MSVCRT_EINVAL
;
2939 release_ioinfo(info
);
2943 if(info
== &MSVCRT___badioinfo
) {
2944 *MSVCRT__errno() = MSVCRT_EBADF
;
2948 if(mode
== MSVCRT__O_BINARY
) {
2949 info
->wxflag
&= ~WX_TEXT
;
2950 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2951 release_ioinfo(info
);
2955 info
->wxflag
|= WX_TEXT
;
2956 if(mode
== MSVCRT__O_TEXT
)
2957 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2958 else if(mode
== MSVCRT__O_U8TEXT
)
2959 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2961 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2963 release_ioinfo(info
);
2967 /*********************************************************************
2968 * _stat64 (MSVCRT.@)
2970 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2973 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2974 unsigned short mode
= ALL_S_IREAD
;
2977 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2979 plen
= strlen(path
);
2980 while (plen
&& path
[plen
-1]==' ')
2983 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2984 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2986 *MSVCRT__errno() = MSVCRT_ENOENT
;
2990 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2992 TRACE("failed (%d)\n",GetLastError());
2993 *MSVCRT__errno() = MSVCRT_ENOENT
;
2997 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2999 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
3000 Bon 011120: This FIXME seems incorrect
3001 Also a letter as first char isn't enough to be classified
3004 if (isalpha(*path
)&& (*(path
+1)==':'))
3005 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
3007 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3009 /* Dir, or regular file? */
3010 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3011 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3014 mode
|= MSVCRT__S_IFREG
;
3016 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3018 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
3019 (tolower(path
[plen
-3]) << 16);
3020 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
3021 mode
|= ALL_S_IEXEC
;
3025 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3026 mode
|= ALL_S_IWRITE
;
3028 buf
->st_mode
= mode
;
3030 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3031 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3033 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3034 buf
->st_mtime
= buf
->st_ctime
= dw
;
3035 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3036 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3037 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3041 /*********************************************************************
3042 * _stati64 (MSVCRT.@)
3044 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
3047 struct MSVCRT__stat64 buf64
;
3049 ret
= MSVCRT_stat64(path
, &buf64
);
3051 msvcrt_stat64_to_stati64(&buf64
, buf
);
3055 /*********************************************************************
3058 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
3061 struct MSVCRT__stat64 buf64
;
3063 ret
= MSVCRT_stat64( path
, &buf64
);
3065 msvcrt_stat64_to_stat(&buf64
, buf
);
3069 #if _MSVCR_VER >= 80
3071 /*********************************************************************
3072 * _stat32 (MSVCR80.@)
3074 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
3077 struct MSVCRT__stat64 buf64
;
3079 ret
= MSVCRT_stat64(path
, &buf64
);
3081 msvcrt_stat64_to_stat32(&buf64
, buf
);
3085 /*********************************************************************
3086 * _stat32i64 (MSVCR80.@)
3088 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
3091 struct MSVCRT__stat64 buf64
;
3093 ret
= MSVCRT_stat64(path
, &buf64
);
3095 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3099 /*********************************************************************
3100 * _stat64i32 (MSVCR80.@)
3102 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
3105 struct MSVCRT__stat64 buf64
;
3107 ret
= MSVCRT_stat64(path
, &buf64
);
3109 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3113 #endif /* _MSVCR_VER >= 80 */
3115 /*********************************************************************
3116 * _wstat64 (MSVCRT.@)
3118 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
3121 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3122 unsigned short mode
= ALL_S_IREAD
;
3125 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3127 plen
= strlenW(path
);
3128 while (plen
&& path
[plen
-1]==' ')
3131 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
3132 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3134 *MSVCRT__errno() = MSVCRT_ENOENT
;
3138 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3140 TRACE("failed (%d)\n",GetLastError());
3141 *MSVCRT__errno() = MSVCRT_ENOENT
;
3145 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3147 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3148 if (MSVCRT_iswalpha(*path
))
3149 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3151 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3153 /* Dir, or regular file? */
3154 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3155 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3158 mode
|= MSVCRT__S_IFREG
;
3160 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3162 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3163 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3164 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3165 mode
|= ALL_S_IEXEC
;
3169 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3170 mode
|= ALL_S_IWRITE
;
3172 buf
->st_mode
= mode
;
3174 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3175 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3177 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3178 buf
->st_mtime
= buf
->st_ctime
= dw
;
3179 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3180 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3181 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3185 /*********************************************************************
3186 * _wstati64 (MSVCRT.@)
3188 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3191 struct MSVCRT__stat64 buf64
;
3193 ret
= MSVCRT__wstat64(path
, &buf64
);
3195 msvcrt_stat64_to_stati64(&buf64
, buf
);
3199 /*********************************************************************
3202 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3205 struct MSVCRT__stat64 buf64
;
3207 ret
= MSVCRT__wstat64( path
, &buf64
);
3208 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3212 #if _MSVCR_VER >= 80
3214 /*********************************************************************
3215 * _wstat32 (MSVCR80.@)
3217 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3220 struct MSVCRT__stat64 buf64
;
3222 ret
= MSVCRT__wstat64(path
, &buf64
);
3224 msvcrt_stat64_to_stat32(&buf64
, buf
);
3228 /*********************************************************************
3229 * _wstat32i64 (MSVCR80.@)
3231 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3234 struct MSVCRT__stat64 buf64
;
3236 ret
= MSVCRT__wstat64(path
, &buf64
);
3238 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3242 /*********************************************************************
3243 * _wstat64i32 (MSVCR80.@)
3245 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3248 struct MSVCRT__stat64 buf64
;
3250 ret
= MSVCRT__wstat64(path
, &buf64
);
3252 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3256 #endif /* _MSVCR_VER >= 80 */
3258 /*********************************************************************
3261 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3263 return MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
3266 /*********************************************************************
3267 * _telli64 (MSVCRT.@)
3269 __int64 CDECL
_telli64(int fd
)
3271 return MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
3274 /*********************************************************************
3275 * _tempnam (MSVCRT.@)
3277 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3279 char tmpbuf
[MAX_PATH
];
3280 const char *tmp_dir
= MSVCRT_getenv("TMP");
3282 if (tmp_dir
) dir
= tmp_dir
;
3284 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3285 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3287 TRACE("got name (%s)\n",tmpbuf
);
3288 DeleteFileA(tmpbuf
);
3289 return MSVCRT__strdup(tmpbuf
);
3291 TRACE("failed (%d)\n",GetLastError());
3295 /*********************************************************************
3296 * _wtempnam (MSVCRT.@)
3298 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3300 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3301 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3302 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3304 if (tmp_dir
) dir
= tmp_dir
;
3306 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3307 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3309 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3310 DeleteFileW(tmpbuf
);
3311 return MSVCRT__wcsdup(tmpbuf
);
3313 TRACE("failed (%d)\n",GetLastError());
3317 /*********************************************************************
3320 int CDECL
MSVCRT__umask(int umask
)
3322 int old_umask
= MSVCRT_umask
;
3323 TRACE("(%d)\n",umask
);
3324 MSVCRT_umask
= umask
;
3328 /*********************************************************************
3329 * _utime64 (MSVCRT.@)
3331 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3333 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3337 int retVal
= _futime64(fd
, t
);
3344 /*********************************************************************
3345 * _utime32 (MSVCRT.@)
3347 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3351 struct MSVCRT___utimbuf64 t64
;
3352 t64
.actime
= t
->actime
;
3353 t64
.modtime
= t
->modtime
;
3354 return _utime64( path
, &t64
);
3357 return _utime64( path
, NULL
);
3360 /*********************************************************************
3364 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3366 return _utime64( path
, t
);
3369 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3371 return _utime32( path
, t
);
3375 /*********************************************************************
3376 * _wutime64 (MSVCRT.@)
3378 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3380 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3384 int retVal
= _futime64(fd
, t
);
3391 /*********************************************************************
3392 * _wutime32 (MSVCRT.@)
3394 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3398 struct MSVCRT___utimbuf64 t64
;
3399 t64
.actime
= t
->actime
;
3400 t64
.modtime
= t
->modtime
;
3401 return _wutime64( path
, &t64
);
3404 return _wutime64( path
, NULL
);
3407 /*********************************************************************
3408 * _wutime (MSVCRT.@)
3411 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3413 return _wutime64( path
, t
);
3416 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3418 return _wutime32( path
, t
);
3422 /*********************************************************************
3425 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3428 ioinfo
*info
= get_ioinfo(fd
);
3429 HANDLE hand
= info
->handle
;
3431 /* Don't trace small writes, it gets *very* annoying */
3434 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3436 if (hand
== INVALID_HANDLE_VALUE
|| fd
== MSVCRT_NO_CONSOLE_FD
)
3438 *MSVCRT__errno() = MSVCRT_EBADF
;
3439 release_ioinfo(info
);
3443 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3445 *MSVCRT__errno() = MSVCRT_EINVAL
;
3446 release_ioinfo(info
);
3450 /* If appending, go to EOF */
3451 if (info
->wxflag
& WX_APPEND
)
3452 MSVCRT__lseek(fd
, 0, FILE_END
);
3454 if (!(info
->wxflag
& WX_TEXT
))
3456 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3457 && (num_written
== count
))
3459 release_ioinfo(info
);
3462 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3463 hand
, GetLastError());
3464 msvcrt_set_errno(GetLastError());
3468 unsigned int i
, j
, nr_lf
, size
;
3471 const char *s
= buf
;
3473 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3475 /* find number of \n */
3476 for (nr_lf
=0, i
=0; i
<count
; i
++)
3482 if ((q
= p
= MSVCRT_malloc(size
)))
3484 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3493 FIXME("Malloc failed\n");
3505 else if (info
->exflag
& EF_UTF16
)
3507 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3508 if (s
[i
]=='\n' && s
[i
+1]==0)
3513 if ((q
= p
= MSVCRT_malloc(size
)))
3515 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3517 if (s
[i
]=='\n' && s
[i
+1]==0)
3528 FIXME("Malloc failed\n");
3544 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3545 if (s
[i
]=='\n' && s
[i
+1]==0)
3548 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3550 msvcrt_set_errno(GetLastError());
3552 release_ioinfo(info
);
3556 size
= conv_len
+nr_lf
;
3557 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3559 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3561 if (s
[i
]=='\n' && s
[i
+1]==0)
3569 q
= p
+count
+nr_lf
*2;
3570 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3571 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3575 FIXME("Malloc failed\n");
3582 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3584 release_ioinfo(info
);
3586 if (num_written
!= size
)
3588 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3589 fd
, hand
, GetLastError(), num_written
);
3590 msvcrt_set_errno(GetLastError());
3596 release_ioinfo(info
);
3600 /*********************************************************************
3603 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3607 MSVCRT__lock_file(file
);
3608 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3609 if (len
== sizeof(val
)) {
3610 MSVCRT__unlock_file(file
);
3614 file
->_flag
|= MSVCRT__IOERR
;
3615 MSVCRT__unlock_file(file
);
3619 /*********************************************************************
3622 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3626 MSVCRT__lock_file(file
);
3627 ret
= MSVCRT__fclose_nolock(file
);
3628 MSVCRT__unlock_file(file
);
3633 /*********************************************************************
3634 * _fclose_nolock (MSVCRT.@)
3636 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3640 if(!(file
->_flag
& (MSVCRT__IOREAD
| MSVCRT__IOWRT
| MSVCRT__IORW
)))
3647 MSVCRT_free(file
->_tmpfname
);
3648 file
->_tmpfname
= NULL
;
3649 /* flush stdio buffers */
3650 if(file
->_flag
& MSVCRT__IOWRT
)
3651 MSVCRT__fflush_nolock(file
);
3652 if(file
->_flag
& MSVCRT__IOMYBUF
)
3653 MSVCRT_free(file
->_base
);
3655 r
=MSVCRT__close(file
->_file
);
3658 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3661 /*********************************************************************
3664 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3666 return file
->_flag
& MSVCRT__IOEOF
;
3669 /*********************************************************************
3672 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3674 return file
->_flag
& MSVCRT__IOERR
;
3677 /*********************************************************************
3678 * _filbuf (MSVCRT.@)
3680 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3684 if(file
->_flag
& MSVCRT__IOSTRG
)
3687 /* Allocate buffer if needed */
3688 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3689 msvcrt_alloc_buffer(file
);
3691 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3692 if(file
->_flag
& MSVCRT__IORW
)
3693 file
->_flag
|= MSVCRT__IOREAD
;
3698 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3700 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3701 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3707 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3709 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3715 file
->_ptr
= file
->_base
+1;
3716 c
= *(unsigned char *)file
->_base
;
3721 /*********************************************************************
3724 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3728 MSVCRT__lock_file(file
);
3729 ret
= MSVCRT__fgetc_nolock(file
);
3730 MSVCRT__unlock_file(file
);
3735 /*********************************************************************
3736 * _fgetc_nolock (MSVCRT.@)
3738 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3745 i
= (unsigned char *)file
->_ptr
++;
3748 j
= MSVCRT__filbuf(file
);
3753 /*********************************************************************
3754 * _fgetchar (MSVCRT.@)
3756 int CDECL
MSVCRT__fgetchar(void)
3758 return MSVCRT_fgetc(MSVCRT_stdin
);
3761 /*********************************************************************
3764 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3766 int cc
= MSVCRT_EOF
;
3767 char * buf_start
= s
;
3769 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3770 file
,file
->_file
,s
,size
);
3772 MSVCRT__lock_file(file
);
3774 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3779 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3781 TRACE(":nothing read\n");
3782 MSVCRT__unlock_file(file
);
3785 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3788 TRACE(":got %s\n", debugstr_a(buf_start
));
3789 MSVCRT__unlock_file(file
);
3793 /*********************************************************************
3796 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3800 MSVCRT__lock_file(file
);
3801 ret
= MSVCRT__fgetwc_nolock(file
);
3802 MSVCRT__unlock_file(file
);
3807 /*********************************************************************
3808 * _fgetwc_nolock (MSVCRT.@)
3810 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3815 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3816 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3819 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3820 ch
= MSVCRT__fgetc_nolock(file
);
3821 if(ch
== MSVCRT_EOF
) {
3828 char mbs
[MSVCRT_MB_LEN_MAX
];
3831 ch
= MSVCRT__fgetc_nolock(file
);
3832 if(ch
!= MSVCRT_EOF
) {
3834 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3835 ch
= MSVCRT__fgetc_nolock(file
);
3836 if(ch
!= MSVCRT_EOF
) {
3845 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3852 /*********************************************************************
3855 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3862 MSVCRT__lock_file(file
);
3863 for (j
=0; j
<sizeof(int); j
++) {
3864 k
= MSVCRT__fgetc_nolock(file
);
3865 if (k
== MSVCRT_EOF
) {
3866 file
->_flag
|= MSVCRT__IOEOF
;
3867 MSVCRT__unlock_file(file
);
3873 MSVCRT__unlock_file(file
);
3877 /*********************************************************************
3880 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3882 return MSVCRT_fgetwc(file
);
3885 /*********************************************************************
3886 * _fgetwchar (MSVCRT.@)
3888 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3890 return MSVCRT_fgetwc(MSVCRT_stdin
);
3893 /*********************************************************************
3894 * getwchar (MSVCRT.@)
3896 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3898 return MSVCRT__fgetwchar();
3901 /*********************************************************************
3904 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3906 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3907 MSVCRT_wchar_t
* buf_start
= s
;
3909 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3910 file
,file
->_file
,s
,size
);
3912 MSVCRT__lock_file(file
);
3914 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3919 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3921 TRACE(":nothing read\n");
3922 MSVCRT__unlock_file(file
);
3925 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3928 TRACE(":got %s\n", debugstr_w(buf_start
));
3929 MSVCRT__unlock_file(file
);
3933 /*********************************************************************
3934 * _flsbuf (MSVCRT.@)
3936 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3938 /* Flush output buffer */
3939 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3940 msvcrt_alloc_buffer(file
);
3943 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3944 if(!(file
->_flag
& MSVCRT__IORW
)) {
3945 file
->_flag
|= MSVCRT__IOERR
;
3946 *MSVCRT__errno() = MSVCRT_EBADF
;
3949 file
->_flag
|= MSVCRT__IOWRT
;
3951 if(file
->_flag
& MSVCRT__IOREAD
) {
3952 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3953 file
->_flag
|= MSVCRT__IOERR
;
3957 file
->_ptr
= file
->_base
;
3958 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3961 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3964 if(file
->_cnt
<= 0) {
3965 res
= msvcrt_flush_buffer(file
);
3968 file
->_flag
|= MSVCRT__IOWRT
;
3969 file
->_cnt
=file
->_bufsiz
;
3977 /* set _cnt to 0 for unbuffered FILEs */
3979 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3982 file
->_flag
|= MSVCRT__IOERR
;
3987 /*********************************************************************
3990 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3994 MSVCRT__lock_file(file
);
3995 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3996 MSVCRT__unlock_file(file
);
4001 /*********************************************************************
4002 * _fwrite_nolock (MSVCRT.@)
4004 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4006 MSVCRT_size_t wrcnt
=size
* nmemb
;
4012 if(file
->_cnt
< 0) {
4013 WARN("negative file->_cnt value in %p\n", file
);
4014 file
->_flag
|= MSVCRT__IOERR
;
4016 } else if(file
->_cnt
) {
4017 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
4018 memcpy(file
->_ptr
, ptr
, pcnt
);
4023 ptr
= (const char*)ptr
+ pcnt
;
4024 } else if((file
->_flag
& MSVCRT__IONBF
)
4025 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
4026 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
4030 if(file
->_flag
& MSVCRT__IONBF
)
4032 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4033 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
4035 bufsiz
= file
->_bufsiz
;
4037 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
4039 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
4042 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
4043 file
->_flag
|= MSVCRT__IOERR
;
4048 ptr
= (const char*)ptr
+ pcnt
;
4050 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
4054 ptr
= (const char*)ptr
+ 1;
4058 return written
/ size
;
4061 /*********************************************************************
4064 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4068 MSVCRT__lock_file(file
);
4069 ret
= MSVCRT__fputwc_nolock(wc
, file
);
4070 MSVCRT__unlock_file(file
);
4075 /*********************************************************************
4076 * _fputwc_nolock (MSVCRT.@)
4078 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4080 MSVCRT_wchar_t mwc
=wc
;
4084 fdinfo
= get_ioinfo_nolock(file
->_file
);
4086 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
4087 char buf
[MSVCRT_MB_LEN_MAX
];
4090 char_len
= MSVCRT_wctomb(buf
, mwc
);
4091 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
4095 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
4104 /*********************************************************************
4105 * _fputwchar (MSVCRT.@)
4107 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
4109 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
4112 /*********************************************************************
4113 * _wfsopen (MSVCRT.@)
4115 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
4118 int open_flags
, stream_flags
, fd
;
4120 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4122 /* map mode string to open() flags. "man fopen" for possibilities. */
4123 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4127 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4130 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4132 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4139 TRACE(":got (%p)\n",file
);
4140 if (fd
>= 0 && !file
)
4146 /*********************************************************************
4147 * _fsopen (MSVCRT.@)
4149 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4152 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4154 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4155 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4156 *MSVCRT__errno() = MSVCRT_EINVAL
;
4159 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4162 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4163 *MSVCRT__errno() = MSVCRT_EINVAL
;
4167 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4174 /*********************************************************************
4177 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4179 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4182 /*********************************************************************
4183 * fopen_s (MSVCRT.@)
4185 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4186 const char *filename
, const char *mode
)
4188 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4189 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4190 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4192 *pFile
= MSVCRT_fopen(filename
, mode
);
4195 return *MSVCRT__errno();
4199 /*********************************************************************
4200 * _wfopen (MSVCRT.@)
4202 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4204 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4207 /*********************************************************************
4208 * _wfopen_s (MSVCRT.@)
4210 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4211 const MSVCRT_wchar_t
*mode
)
4213 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4214 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4215 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4217 *pFile
= MSVCRT__wfopen(filename
, mode
);
4220 return *MSVCRT__errno();
4224 /*********************************************************************
4227 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4231 MSVCRT__lock_file(file
);
4232 ret
= MSVCRT__fputc_nolock(c
, file
);
4233 MSVCRT__unlock_file(file
);
4238 /*********************************************************************
4239 * _fputc_nolock (MSVCRT.@)
4241 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4250 res
= msvcrt_flush_buffer(file
);
4251 return res
? res
: c
;
4257 res
= MSVCRT__flsbuf(c
, file
);
4262 /*********************************************************************
4263 * _fputchar (MSVCRT.@)
4265 int CDECL
MSVCRT__fputchar(int c
)
4267 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4270 /*********************************************************************
4273 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4277 MSVCRT__lock_file(file
);
4278 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4279 MSVCRT__unlock_file(file
);
4284 /*********************************************************************
4285 * _fread_nolock (MSVCRT.@)
4287 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4289 MSVCRT_size_t rcnt
=size
* nmemb
;
4290 MSVCRT_size_t read
=0;
4291 MSVCRT_size_t pread
=0;
4296 /* first buffered data */
4298 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4299 memcpy(ptr
, file
->_ptr
, pcnt
);
4304 ptr
= (char*)ptr
+ pcnt
;
4305 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4306 if(file
->_flag
& MSVCRT__IORW
) {
4307 file
->_flag
|= MSVCRT__IOREAD
;
4313 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4314 msvcrt_alloc_buffer(file
);
4319 if (!file
->_cnt
&& rcnt
<file
->_bufsiz
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4320 i
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4321 file
->_ptr
= file
->_base
;
4324 if (i
> rcnt
) i
= rcnt
;
4326 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4327 if (i
> 0 && i
< file
->_cnt
) {
4328 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4329 file
->_flag
&= ~MSVCRT__IOEOF
;
4332 memcpy(ptr
, file
->_ptr
, i
);
4336 } else if (rcnt
> INT_MAX
) {
4337 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4338 } else if (rcnt
< (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
)) {
4339 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4341 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- rcnt
% (file
->_bufsiz
? file
->_bufsiz
: MSVCRT_INTERNAL_BUFSIZ
));
4345 ptr
= (char *)ptr
+i
;
4346 /* expose feof condition in the flags
4347 * MFC tests file->_flag for feof, and doesn't call feof())
4349 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4350 file
->_flag
|= MSVCRT__IOEOF
;
4353 file
->_flag
|= MSVCRT__IOERR
;
4363 #if _MSVCR_VER >= 80
4365 /*********************************************************************
4366 * fread_s (MSVCR80.@)
4368 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4369 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4373 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4375 memset(buf
, 0, buf_size
);
4378 if(!elem_size
|| !count
) return 0;
4380 MSVCRT__lock_file(stream
);
4381 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4382 MSVCRT__unlock_file(stream
);
4387 /*********************************************************************
4388 * _fread_nolock_s (MSVCR80.@)
4390 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4391 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4393 size_t bytes_left
, buf_pos
;
4395 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4397 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4399 memset(buf
, 0, buf_size
);
4402 if(!elem_size
|| !count
) return 0;
4403 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4404 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4406 bytes_left
= elem_size
*count
;
4409 if(stream
->_cnt
> 0) {
4410 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4412 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4413 memset(buf
, 0, buf_size
);
4417 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4421 int c
= MSVCRT__filbuf(stream
);
4426 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4427 memset(buf
, 0, buf_size
);
4431 ((char*)buf
)[buf_pos
++] = c
;
4436 return buf_pos
/elem_size
;
4439 #endif /* _MSVCR_VER >= 80 */
4441 /*********************************************************************
4442 * _wfreopen (MSVCRT.@)
4445 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4447 int open_flags
, stream_flags
, fd
;
4449 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4452 if (!file
|| ((fd
= file
->_file
) < 0))
4456 MSVCRT_fclose(file
);
4457 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4459 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4461 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4471 /*********************************************************************
4472 * _wfreopen_s (MSVCRT.@)
4474 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4475 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4477 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4478 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4479 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4480 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4482 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4485 return *MSVCRT__errno();
4489 /*********************************************************************
4490 * freopen (MSVCRT.@)
4493 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4496 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4498 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4499 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4505 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4512 /*********************************************************************
4513 * freopen_s (MSVCRT.@)
4515 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4516 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4518 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4519 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4520 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4521 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4523 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4526 return *MSVCRT__errno();
4530 /*********************************************************************
4531 * fsetpos (MSVCRT.@)
4533 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4537 MSVCRT__lock_file(file
);
4538 msvcrt_flush_buffer(file
);
4540 /* Reset direction of i/o */
4541 if(file
->_flag
& MSVCRT__IORW
) {
4542 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4545 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,MSVCRT_SEEK_SET
) == -1) ? -1 : 0;
4546 MSVCRT__unlock_file(file
);
4550 /*********************************************************************
4551 * _ftelli64 (MSVCRT.@)
4553 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4557 MSVCRT__lock_file(file
);
4558 ret
= MSVCRT__ftelli64_nolock(file
);
4559 MSVCRT__unlock_file(file
);
4564 /*********************************************************************
4565 * _ftelli64_nolock (MSVCRT.@)
4567 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4571 pos
= _telli64(file
->_file
);
4574 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4575 if(file
->_flag
& MSVCRT__IOWRT
) {
4576 pos
+= file
->_ptr
- file
->_base
;
4578 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4581 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4585 } else if(!file
->_cnt
) { /* nothing to do */
4586 } else if(MSVCRT__lseeki64(file
->_file
, 0, MSVCRT_SEEK_END
)==pos
) {
4590 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4591 for(i
=0; i
<file
->_cnt
; i
++)
4592 if(file
->_ptr
[i
] == '\n')
4598 if(MSVCRT__lseeki64(file
->_file
, pos
, MSVCRT_SEEK_SET
) != pos
)
4601 pos
-= file
->_bufsiz
;
4602 pos
+= file
->_ptr
- file
->_base
;
4604 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4605 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4608 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4618 /*********************************************************************
4621 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4623 return MSVCRT__ftelli64(file
);
4626 #if _MSVCR_VER >= 80
4627 /*********************************************************************
4628 * _ftell_nolock (MSVCR80.@)
4630 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4632 return MSVCRT__ftelli64_nolock(file
);
4636 /*********************************************************************
4637 * fgetpos (MSVCRT.@)
4639 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4641 *pos
= MSVCRT__ftelli64(file
);
4647 /*********************************************************************
4650 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4652 MSVCRT_size_t len
= strlen(s
);
4655 MSVCRT__lock_file(file
);
4656 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4657 MSVCRT__unlock_file(file
);
4661 /*********************************************************************
4664 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4666 MSVCRT_size_t i
, len
= strlenW(s
);
4670 MSVCRT__lock_file(file
);
4671 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4672 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4673 MSVCRT__unlock_file(file
);
4677 tmp_buf
= add_std_buffer(file
);
4678 for (i
=0; i
<len
; i
++) {
4679 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4680 if(tmp_buf
) remove_std_buffer(file
);
4681 MSVCRT__unlock_file(file
);
4686 if(tmp_buf
) remove_std_buffer(file
);
4687 MSVCRT__unlock_file(file
);
4691 /*********************************************************************
4692 * getchar (MSVCRT.@)
4694 int CDECL
MSVCRT_getchar(void)
4696 return MSVCRT_fgetc(MSVCRT_stdin
);
4699 /*********************************************************************
4702 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4704 return MSVCRT_fgetc(file
);
4707 /*********************************************************************
4710 char * CDECL
MSVCRT_gets(char *buf
)
4713 char * buf_start
= buf
;
4715 MSVCRT__lock_file(MSVCRT_stdin
);
4716 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4717 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4722 MSVCRT__unlock_file(MSVCRT_stdin
);
4724 if ((cc
== MSVCRT_EOF
) && (buf_start
== buf
))
4726 TRACE(":nothing read\n");
4731 TRACE("got '%s'\n", buf_start
);
4735 /*********************************************************************
4738 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4741 MSVCRT_wchar_t
* ws
= buf
;
4743 MSVCRT__lock_file(MSVCRT_stdin
);
4744 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4745 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4748 *buf
++ = (MSVCRT_wchar_t
)cc
;
4750 MSVCRT__unlock_file(MSVCRT_stdin
);
4752 if ((cc
== MSVCRT_WEOF
) && (ws
== buf
))
4754 TRACE(":nothing read\n");
4759 TRACE("got %s\n", debugstr_w(ws
));
4763 /*********************************************************************
4766 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4768 return MSVCRT_fputc(c
, file
);
4771 /*********************************************************************
4772 * putchar (MSVCRT.@)
4774 int CDECL
MSVCRT_putchar(int c
)
4776 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4779 /*********************************************************************
4782 int CDECL
MSVCRT_puts(const char *s
)
4784 MSVCRT_size_t len
= strlen(s
);
4787 MSVCRT__lock_file(MSVCRT_stdout
);
4788 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4789 MSVCRT__unlock_file(MSVCRT_stdout
);
4793 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4794 MSVCRT__unlock_file(MSVCRT_stdout
);
4798 /*********************************************************************
4801 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4803 static const MSVCRT_wchar_t nl
= '\n';
4804 MSVCRT_size_t len
= strlenW(s
);
4807 MSVCRT__lock_file(MSVCRT_stdout
);
4808 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4809 MSVCRT__unlock_file(MSVCRT_stdout
);
4813 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4814 MSVCRT__unlock_file(MSVCRT_stdout
);
4818 /*********************************************************************
4821 int CDECL
MSVCRT_remove(const char *path
)
4823 TRACE("(%s)\n",path
);
4824 if (DeleteFileA(path
))
4826 TRACE(":failed (%d)\n",GetLastError());
4827 msvcrt_set_errno(GetLastError());
4831 /*********************************************************************
4832 * _wremove (MSVCRT.@)
4834 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4836 TRACE("(%s)\n",debugstr_w(path
));
4837 if (DeleteFileW(path
))
4839 TRACE(":failed (%d)\n",GetLastError());
4840 msvcrt_set_errno(GetLastError());
4844 /*********************************************************************
4847 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4849 TRACE(":from %s to %s\n",oldpath
,newpath
);
4850 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4852 TRACE(":failed (%d)\n",GetLastError());
4853 msvcrt_set_errno(GetLastError());
4857 /*********************************************************************
4858 * _wrename (MSVCRT.@)
4860 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4862 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4863 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4865 TRACE(":failed (%d)\n",GetLastError());
4866 msvcrt_set_errno(GetLastError());
4870 /*********************************************************************
4871 * setvbuf (MSVCRT.@)
4873 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4875 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4876 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4877 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4879 MSVCRT__lock_file(file
);
4881 MSVCRT__fflush_nolock(file
);
4882 if(file
->_flag
& MSVCRT__IOMYBUF
)
4883 MSVCRT_free(file
->_base
);
4884 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4887 if(mode
== MSVCRT__IONBF
) {
4888 file
->_flag
|= MSVCRT__IONBF
;
4889 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4892 file
->_base
= file
->_ptr
= buf
;
4893 file
->_flag
|= MSVCRT__USERBUF
;
4894 file
->_bufsiz
= size
;
4896 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4899 MSVCRT__unlock_file(file
);
4903 file
->_flag
|= MSVCRT__IOMYBUF
;
4904 file
->_bufsiz
= size
;
4906 MSVCRT__unlock_file(file
);
4910 /*********************************************************************
4913 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4915 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4918 static int tmpnam_helper(char *s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
4924 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
4928 *MSVCRT__errno() = MSVCRT_ERANGE
;
4929 return MSVCRT_ERANGE
;
4934 digits
= msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4935 if (digits
+1 > size
) {
4937 *MSVCRT__errno() = MSVCRT_ERANGE
;
4938 return MSVCRT_ERANGE
;
4940 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4946 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
4947 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
4951 digits
= msvcrt_int_to_base32(digits
, tmpstr
);
4952 if (digits
+1 > size
) {
4954 *MSVCRT__errno() = MSVCRT_ERANGE
;
4955 return MSVCRT_ERANGE
;
4957 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
4960 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4961 GetLastError() == ERROR_FILE_NOT_FOUND
)
4967 int CDECL
MSVCRT_tmpnam_s(char *s
, MSVCRT_size_t size
)
4969 return tmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
4972 /*********************************************************************
4975 char * CDECL
MSVCRT_tmpnam(char *s
)
4978 thread_data_t
*data
= msvcrt_get_thread_data();
4980 if(!data
->tmpnam_buffer
)
4981 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4983 s
= data
->tmpnam_buffer
;
4986 return tmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
4989 static int wtmpnam_helper(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
, int *tmpnam_unique
, int tmp_max
)
4991 MSVCRT_wchar_t tmpstr
[8];
4992 MSVCRT_wchar_t
*p
= s
;
4995 if (!MSVCRT_CHECK_PMT(s
!= NULL
)) return MSVCRT_EINVAL
;
4999 *MSVCRT__errno() = MSVCRT_ERANGE
;
5000 return MSVCRT_ERANGE
;
5005 digits
= msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
5006 if (digits
+1 > size
) {
5008 *MSVCRT__errno() = MSVCRT_ERANGE
;
5009 return MSVCRT_ERANGE
;
5011 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5017 while ((digits
= *tmpnam_unique
)+1 < tmp_max
) {
5018 if (InterlockedCompareExchange(tmpnam_unique
, digits
+1, digits
) == digits
)
5022 digits
= msvcrt_int_to_base32_w(digits
, tmpstr
);
5023 if (digits
+1 > size
) {
5025 *MSVCRT__errno() = MSVCRT_ERANGE
;
5026 return MSVCRT_ERANGE
;
5028 memcpy(p
, tmpstr
, digits
*sizeof(tmpstr
[0]));
5031 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
5032 GetLastError() == ERROR_FILE_NOT_FOUND
)
5038 /*********************************************************************
5039 * _wtmpnam_s (MSVCRT.@)
5041 int CDECL
MSVCRT__wtmpnam_s(MSVCRT_wchar_t
*s
, MSVCRT_size_t size
)
5043 return wtmpnam_helper(s
, size
, &tmpnam_s_unique
, MSVCRT_TMP_MAX_S
);
5046 /*********************************************************************
5047 * _wtmpnam (MSVCRT.@)
5049 MSVCRT_wchar_t
* CDECL
MSVCRT__wtmpnam(MSVCRT_wchar_t
*s
)
5052 thread_data_t
*data
= msvcrt_get_thread_data();
5054 if(!data
->wtmpnam_buffer
)
5055 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
5057 s
= data
->wtmpnam_buffer
;
5060 return wtmpnam_helper(s
, -1, &tmpnam_unique
, MSVCRT_TMP_MAX
) ? NULL
: s
;
5063 /*********************************************************************
5064 * tmpfile (MSVCRT.@)
5066 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
5068 char *filename
= MSVCRT__tempnam(",", "t");
5070 MSVCRT_FILE
* file
= NULL
;
5073 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
5074 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
5075 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
5077 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
5082 else file
->_tmpfname
= MSVCRT__strdup(filename
);
5085 if(fd
!= -1 && !file
)
5087 MSVCRT_free(filename
);
5092 /*********************************************************************
5093 * tmpfile_s (MSVCRT.@)
5095 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
5097 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
5099 *file
= MSVCRT_tmpfile();
5103 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
5105 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
5108 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
5112 MSVCRT__lock_file(file
);
5114 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
5115 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
5116 MSVCRT__unlock_file(file
);
5120 for(i
=0; i
<len
; i
++) {
5121 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
5122 MSVCRT__unlock_file(file
);
5127 MSVCRT__unlock_file(file
);
5131 static int vfprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const char *format
,
5132 MSVCRT__locale_t locale
, __ms_va_list valist
)
5134 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5138 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5139 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5141 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5142 memset(args_ctx
, 0, sizeof(args_ctx
));
5143 ret
= create_positional_ctx_a(args_ctx
, format
, valist
);
5145 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5146 *MSVCRT__errno() = MSVCRT_EINVAL
;
5149 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5152 MSVCRT__lock_file(file
);
5153 tmp_buf
= add_std_buffer(file
);
5154 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
, options
,
5155 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5156 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5157 if(tmp_buf
) remove_std_buffer(file
);
5158 MSVCRT__unlock_file(file
);
5163 static int vfwprintf_helper(DWORD options
, MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5164 MSVCRT__locale_t locale
, __ms_va_list valist
)
5166 printf_arg args_ctx
[MSVCRT__ARGMAX
+1];
5170 if(!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5171 if(!MSVCRT_CHECK_PMT( format
!= NULL
)) return -1;
5173 if(options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
) {
5174 memset(args_ctx
, 0, sizeof(args_ctx
));
5175 ret
= create_positional_ctx_w(args_ctx
, format
, valist
);
5177 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
5178 *MSVCRT__errno() = MSVCRT_EINVAL
;
5181 options
&= ~MSVCRT_PRINTF_POSITIONAL_PARAMS
;
5184 MSVCRT__lock_file(file
);
5185 tmp_buf
= add_std_buffer(file
);
5186 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, options
,
5187 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? arg_clbk_positional
: arg_clbk_valist
,
5188 options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
? args_ctx
: NULL
, &valist
);
5189 if(tmp_buf
) remove_std_buffer(file
);
5190 MSVCRT__unlock_file(file
);
5195 /*********************************************************************
5196 * _vfprintf_s_l (MSVCRT.@)
5198 int CDECL
MSVCRT__vfprintf_s_l(MSVCRT_FILE
* file
, const char *format
,
5199 MSVCRT__locale_t locale
, __ms_va_list valist
)
5201 return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5204 /*********************************************************************
5205 * _vfwprintf_s_l (MSVCRT.@)
5207 int CDECL
MSVCRT__vfwprintf_s_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5208 MSVCRT__locale_t locale
, __ms_va_list valist
)
5210 return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, file
, format
, locale
, valist
);
5213 /*********************************************************************
5214 * vfprintf (MSVCRT.@)
5216 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5218 return vfprintf_helper(0, file
, format
, NULL
, valist
);
5221 /*********************************************************************
5222 * vfprintf_s (MSVCRT.@)
5224 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5226 return MSVCRT__vfprintf_s_l(file
, format
, NULL
, valist
);
5229 /*********************************************************************
5230 * vfwprintf (MSVCRT.@)
5232 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5234 return vfwprintf_helper(0, file
, format
, NULL
, valist
);
5237 /*********************************************************************
5238 * vfwprintf_s (MSVCRT.@)
5240 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5242 return MSVCRT__vfwprintf_s_l(file
, format
, NULL
, valist
);
5245 #if _MSVCR_VER >= 140
5247 /*********************************************************************
5248 * __stdio_common_vfprintf (UCRTBASE.@)
5250 int CDECL
MSVCRT__stdio_common_vfprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5251 MSVCRT__locale_t locale
, __ms_va_list valist
)
5253 if (options
& ~UCRTBASE_PRINTF_MASK
)
5254 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5256 return vfprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5259 /*********************************************************************
5260 * __stdio_common_vfprintf_s (UCRTBASE.@)
5262 int CDECL
MSVCRT__stdio_common_vfprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
5263 MSVCRT__locale_t locale
, __ms_va_list valist
)
5265 if (options
& ~UCRTBASE_PRINTF_MASK
)
5266 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5268 return vfprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5269 file
, format
, locale
, valist
);
5272 /*********************************************************************
5273 * __stdio_common_vfwprintf (UCRTBASE.@)
5275 int CDECL
MSVCRT__stdio_common_vfwprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5276 MSVCRT__locale_t locale
, __ms_va_list valist
)
5278 if (options
& ~UCRTBASE_PRINTF_MASK
)
5279 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5281 return vfwprintf_helper(options
& UCRTBASE_PRINTF_MASK
, file
, format
, locale
, valist
);
5284 /*********************************************************************
5285 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5287 int CDECL
MSVCRT__stdio_common_vfwprintf_s(unsigned __int64 options
, MSVCRT_FILE
*file
, const MSVCRT_wchar_t
*format
,
5288 MSVCRT__locale_t locale
, __ms_va_list valist
)
5290 if (options
& ~UCRTBASE_PRINTF_MASK
)
5291 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5293 return vfwprintf_helper((options
& UCRTBASE_PRINTF_MASK
) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5294 file
, format
, locale
, valist
);
5297 #endif /* _MSVCR_VER >= 140 */
5299 /*********************************************************************
5300 * _vfprintf_l (MSVCRT.@)
5302 int CDECL
MSVCRT__vfprintf_l(MSVCRT_FILE
* file
, const char *format
,
5303 MSVCRT__locale_t locale
, __ms_va_list valist
)
5305 return vfprintf_helper(0, file
, format
, locale
, valist
);
5308 /*********************************************************************
5309 * _vfwprintf_l (MSVCRT.@)
5311 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5312 MSVCRT__locale_t locale
, __ms_va_list valist
)
5314 return vfwprintf_helper(0, file
, format
, locale
, valist
);
5317 /*********************************************************************
5318 * _vfprintf_p_l (MSVCRT.@)
5320 int CDECL
MSVCRT__vfprintf_p_l(MSVCRT_FILE
* file
, const char *format
,
5321 MSVCRT__locale_t locale
, __ms_va_list valist
)
5323 return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5324 file
, format
, locale
, valist
);
5327 /*********************************************************************
5328 * _vfprintf_p (MSVCRT.@)
5330 int CDECL
MSVCRT__vfprintf_p(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
5332 return MSVCRT__vfprintf_p_l(file
, format
, NULL
, valist
);
5335 /*********************************************************************
5336 * _vfwprintf_p_l (MSVCRT.@)
5338 int CDECL
MSVCRT__vfwprintf_p_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5339 MSVCRT__locale_t locale
, __ms_va_list valist
)
5341 return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
,
5342 file
, format
, locale
, valist
);
5345 /*********************************************************************
5346 * _vfwprintf_p (MSVCRT.@)
5348 int CDECL
MSVCRT__vfwprintf_p(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5350 return MSVCRT__vfwprintf_p_l(file
, format
, NULL
, valist
);
5353 /*********************************************************************
5354 * vprintf (MSVCRT.@)
5356 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5358 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5361 /*********************************************************************
5362 * vprintf_s (MSVCRT.@)
5364 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5366 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5369 /*********************************************************************
5370 * vwprintf (MSVCRT.@)
5372 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5374 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5377 /*********************************************************************
5378 * vwprintf_s (MSVCRT.@)
5380 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5382 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5385 /*********************************************************************
5386 * fprintf (MSVCRT.@)
5388 int WINAPIV
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5390 __ms_va_list valist
;
5392 __ms_va_start(valist
, format
);
5393 res
= MSVCRT_vfprintf(file
, format
, valist
);
5394 __ms_va_end(valist
);
5398 /*********************************************************************
5399 * fprintf_s (MSVCRT.@)
5401 int WINAPIV
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5403 __ms_va_list valist
;
5405 __ms_va_start(valist
, format
);
5406 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5407 __ms_va_end(valist
);
5411 /*********************************************************************
5412 * fwprintf (MSVCRT.@)
5414 int WINAPIV
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5416 __ms_va_list valist
;
5418 __ms_va_start(valist
, format
);
5419 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5420 __ms_va_end(valist
);
5424 /*********************************************************************
5425 * fwprintf_s (MSVCRT.@)
5427 int WINAPIV
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5429 __ms_va_list valist
;
5431 __ms_va_start(valist
, format
);
5432 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5433 __ms_va_end(valist
);
5437 /*********************************************************************
5438 * _fwprintf_l (MSVCRT.@)
5440 int WINAPIV
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5442 __ms_va_list valist
;
5444 __ms_va_start(valist
, locale
);
5445 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5446 __ms_va_end(valist
);
5450 /*********************************************************************
5453 int WINAPIV
MSVCRT_printf(const char *format
, ...)
5455 __ms_va_list valist
;
5457 __ms_va_start(valist
, format
);
5458 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5459 __ms_va_end(valist
);
5463 /*********************************************************************
5464 * printf_s (MSVCRT.@)
5466 int WINAPIV
MSVCRT_printf_s(const char *format
, ...)
5468 __ms_va_list valist
;
5470 __ms_va_start(valist
, format
);
5471 res
= MSVCRT_vprintf_s(format
, valist
);
5472 __ms_va_end(valist
);
5476 /*********************************************************************
5479 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5483 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5485 MSVCRT__lock_file(file
);
5486 ret
= MSVCRT__ungetc_nolock(c
, file
);
5487 MSVCRT__unlock_file(file
);
5492 /*********************************************************************
5493 * _ungetc_nolock (MSVCRT.@)
5495 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5497 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5499 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5500 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5503 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5504 && msvcrt_alloc_buffer(file
))
5505 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5508 if(file
->_ptr
>file
->_base
) {
5510 if(file
->_flag
& MSVCRT__IOSTRG
) {
5511 if(*file
->_ptr
!= c
) {
5519 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5520 file
->_flag
|= MSVCRT__IOREAD
;
5527 /*********************************************************************
5528 * ungetwc (MSVCRT.@)
5530 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5534 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5536 MSVCRT__lock_file(file
);
5537 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5538 MSVCRT__unlock_file(file
);
5543 /*********************************************************************
5544 * _ungetwc_nolock (MSVCRT.@)
5546 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5548 MSVCRT_wchar_t mwc
= wc
;
5550 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5551 if (wc
== MSVCRT_WEOF
)
5554 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5555 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5556 unsigned char * pp
= (unsigned char *)&mwc
;
5559 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5560 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5564 char mbs
[MSVCRT_MB_LEN_MAX
];
5567 len
= MSVCRT_wctomb(mbs
, mwc
);
5571 for(len
--; len
>=0; len
--) {
5572 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5580 /*********************************************************************
5581 * wprintf (MSVCRT.@)
5583 int WINAPIV
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5585 __ms_va_list valist
;
5587 __ms_va_start(valist
, format
);
5588 res
= MSVCRT_vwprintf(format
, valist
);
5589 __ms_va_end(valist
);
5593 /*********************************************************************
5594 * wprintf_s (MSVCRT.@)
5596 int WINAPIV
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5598 __ms_va_list valist
;
5600 __ms_va_start(valist
, format
);
5601 res
= MSVCRT_vwprintf_s(format
, valist
);
5602 __ms_va_end(valist
);
5606 /*********************************************************************
5607 * _getmaxstdio (MSVCRT.@)
5609 int CDECL
MSVCRT__getmaxstdio(void)
5611 return MSVCRT_max_streams
;
5614 /*********************************************************************
5615 * _setmaxstdio (MSVCRT.@)
5617 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5619 TRACE("%d\n", newmax
);
5621 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5624 MSVCRT_max_streams
= newmax
;
5625 return MSVCRT_max_streams
;
5628 #if _MSVCR_VER >= 140
5629 /*********************************************************************
5630 * _get_stream_buffer_pointers (UCRTBASE.@)
5632 int CDECL
MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE
*file
, char*** base
,
5633 char*** ptr
, int** count
)
5636 *base
= &file
->_base
;
5640 *count
= &file
->_cnt
;