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
};
117 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
118 static int MSVCRT_fdend
= 3; /* highest allocated fd */
122 CRITICAL_SECTION crit
;
125 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
126 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
127 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
129 /* INTERNAL: process umask */
130 static int MSVCRT_umask
= 0;
132 /* INTERNAL: static data for tmpnam and _wtmpname functions */
133 static int tmpnam_unique
;
135 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
136 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
137 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
138 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
140 #define TOUL(x) (ULONGLONG)(x)
141 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
142 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
143 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
144 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
146 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
147 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
148 * and MSVCRT_stream_idx, from race conditions.
149 * It doesn't protect against race conditions manipulating the underlying files
150 * or flags; doing so would probably be better accomplished with per-file
151 * protection, rather than locking the whole table for every change.
153 static CRITICAL_SECTION MSVCRT_file_cs
;
154 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
156 0, 0, &MSVCRT_file_cs
,
157 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
158 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
160 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
161 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
162 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
164 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
166 buf
->st_dev
= buf64
->st_dev
;
167 buf
->st_ino
= buf64
->st_ino
;
168 buf
->st_mode
= buf64
->st_mode
;
169 buf
->st_nlink
= buf64
->st_nlink
;
170 buf
->st_uid
= buf64
->st_uid
;
171 buf
->st_gid
= buf64
->st_gid
;
172 buf
->st_rdev
= buf64
->st_rdev
;
173 buf
->st_size
= buf64
->st_size
;
174 buf
->st_atime
= buf64
->st_atime
;
175 buf
->st_mtime
= buf64
->st_mtime
;
176 buf
->st_ctime
= buf64
->st_ctime
;
179 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
181 buf
->st_dev
= buf64
->st_dev
;
182 buf
->st_ino
= buf64
->st_ino
;
183 buf
->st_mode
= buf64
->st_mode
;
184 buf
->st_nlink
= buf64
->st_nlink
;
185 buf
->st_uid
= buf64
->st_uid
;
186 buf
->st_gid
= buf64
->st_gid
;
187 buf
->st_rdev
= buf64
->st_rdev
;
188 buf
->st_size
= buf64
->st_size
;
189 buf
->st_atime
= buf64
->st_atime
;
190 buf
->st_mtime
= buf64
->st_mtime
;
191 buf
->st_ctime
= buf64
->st_ctime
;
194 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
196 buf
->st_dev
= buf64
->st_dev
;
197 buf
->st_ino
= buf64
->st_ino
;
198 buf
->st_mode
= buf64
->st_mode
;
199 buf
->st_nlink
= buf64
->st_nlink
;
200 buf
->st_uid
= buf64
->st_uid
;
201 buf
->st_gid
= buf64
->st_gid
;
202 buf
->st_rdev
= buf64
->st_rdev
;
203 buf
->st_size
= buf64
->st_size
;
204 buf
->st_atime
= buf64
->st_atime
;
205 buf
->st_mtime
= buf64
->st_mtime
;
206 buf
->st_ctime
= buf64
->st_ctime
;
209 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
211 buf
->st_dev
= buf64
->st_dev
;
212 buf
->st_ino
= buf64
->st_ino
;
213 buf
->st_mode
= buf64
->st_mode
;
214 buf
->st_nlink
= buf64
->st_nlink
;
215 buf
->st_uid
= buf64
->st_uid
;
216 buf
->st_gid
= buf64
->st_gid
;
217 buf
->st_rdev
= buf64
->st_rdev
;
218 buf
->st_size
= buf64
->st_size
;
219 buf
->st_atime
= buf64
->st_atime
;
220 buf
->st_mtime
= buf64
->st_mtime
;
221 buf
->st_ctime
= buf64
->st_ctime
;
224 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
226 buf
->st_dev
= buf64
->st_dev
;
227 buf
->st_ino
= buf64
->st_ino
;
228 buf
->st_mode
= buf64
->st_mode
;
229 buf
->st_nlink
= buf64
->st_nlink
;
230 buf
->st_uid
= buf64
->st_uid
;
231 buf
->st_gid
= buf64
->st_gid
;
232 buf
->st_rdev
= buf64
->st_rdev
;
233 buf
->st_size
= buf64
->st_size
;
234 buf
->st_atime
= buf64
->st_atime
;
235 buf
->st_mtime
= buf64
->st_mtime
;
236 buf
->st_ctime
= buf64
->st_ctime
;
239 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
241 /* 1601 to 1970 is 369 years plus 89 leap days */
242 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
244 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
245 ft
->dwHighDateTime
= ticks
>> 32;
246 ft
->dwLowDateTime
= ticks
;
249 static inline ioinfo
* get_ioinfo_nolock(int fd
)
252 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
253 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
255 return &MSVCRT___badioinfo
;
257 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
260 static inline ioinfo
* get_ioinfo(int fd
)
262 ioinfo
*ret
= get_ioinfo_nolock(fd
);
263 if(!(ret
->exflag
& EF_CRIT_INIT
)) {
265 if(!(ret
->exflag
& EF_CRIT_INIT
)) {
266 InitializeCriticalSection(&ret
->crit
);
267 ret
->exflag
|= EF_CRIT_INIT
;
271 EnterCriticalSection(&ret
->crit
);
275 static inline void release_ioinfo(ioinfo
*info
)
277 if(info
->exflag
& EF_CRIT_INIT
)
278 LeaveCriticalSection(&info
->crit
);
281 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
285 if(i
>= MSVCRT_max_streams
)
289 return &MSVCRT__iob
[i
];
291 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
293 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
294 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
295 ERR("out of memory\n");
296 *MSVCRT__errno() = MSVCRT_ENOMEM
;
300 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
302 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
307 static inline BOOL
msvcrt_is_valid_fd(int fd
)
309 return fd
>= 0 && fd
< MSVCRT_fdend
&& (get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
);
312 /* INTERNAL: free a file entry fd */
313 static void msvcrt_free_fd(int fd
)
315 ioinfo
*fdinfo
= get_ioinfo(fd
);
317 if(fdinfo
!= &MSVCRT___badioinfo
)
319 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
322 TRACE(":fd (%d) freed\n",fd
);
329 SetStdHandle(STD_INPUT_HANDLE
, 0);
332 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
335 SetStdHandle(STD_ERROR_HANDLE
, 0);
339 release_ioinfo(fdinfo
);
342 if (fd
== MSVCRT_fdend
- 1)
344 if (fd
< MSVCRT_fdstart
)
349 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
350 /* caller must hold the files lock */
351 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
355 if (fd
>= MSVCRT_MAX_FILES
)
357 WARN(":files exhausted!\n");
358 *MSVCRT__errno() = MSVCRT_ENFILE
;
362 fdinfo
= get_ioinfo_nolock(fd
);
363 if(fdinfo
== &MSVCRT___badioinfo
) {
366 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
367 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
368 WARN(":out of memory!\n");
369 *MSVCRT__errno() = MSVCRT_ENOMEM
;
373 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
374 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
376 fdinfo
= get_ioinfo_nolock(fd
);
379 fdinfo
->handle
= hand
;
380 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
381 fdinfo
->lookahead
[0] = '\n';
382 fdinfo
->lookahead
[1] = '\n';
383 fdinfo
->lookahead
[2] = '\n';
384 if(!(fdinfo
->exflag
& EF_CRIT_INIT
))
385 InitializeCriticalSection(&fdinfo
->crit
);
386 fdinfo
->exflag
= EF_CRIT_INIT
;
388 /* locate next free slot */
389 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
390 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
392 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
393 get_ioinfo_nolock(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
395 /* update last fd in use */
396 if (fd
>= MSVCRT_fdend
)
397 MSVCRT_fdend
= fd
+ 1;
398 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
402 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
403 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
404 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
410 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
411 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
416 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
417 ret
= msvcrt_set_fd(hand
, flag
, MSVCRT_fdstart
);
422 /* INTERNAL: Allocate a FILE* for an fd slot */
423 /* caller must hold the files lock */
424 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
429 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
431 file
= msvcrt_get_file(i
);
435 if (file
->_flag
== 0)
437 if (i
== MSVCRT_stream_idx
)
439 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
441 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
442 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
453 /* INTERNAL: initialize a FILE* from an open fd */
454 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
456 TRACE(":fd (%d) allocating FILE*\n",fd
);
457 if (!msvcrt_is_valid_fd(fd
))
459 WARN(":invalid fd %d\n",fd
);
460 *MSVCRT___doserrno() = 0;
461 *MSVCRT__errno() = MSVCRT_EBADF
;
464 file
->_ptr
= file
->_base
= NULL
;
467 file
->_flag
= stream_flags
;
468 file
->_tmpfname
= NULL
;
470 TRACE(":got FILE* (%p)\n",file
);
474 /* INTERNAL: Create an inheritance data block (for spawned process)
475 * The inheritance block is made of:
476 * 00 int nb of file descriptor (NBFD)
477 * 04 char file flags (wxflag): repeated for each fd
478 * 4+NBFD HANDLE file handle: repeated for each fd
480 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
487 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
488 *block
= MSVCRT_calloc(*size
, 1);
494 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
495 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
497 *(unsigned*)*block
= MSVCRT_fdend
;
498 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
500 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
501 fdinfo
= get_ioinfo(fd
);
502 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
504 *wxflag_ptr
= fdinfo
->wxflag
;
505 *handle_ptr
= fdinfo
->handle
;
510 *handle_ptr
= INVALID_HANDLE_VALUE
;
512 release_ioinfo(fdinfo
);
513 wxflag_ptr
++; handle_ptr
++;
518 /* INTERNAL: Set up all file descriptors,
519 * as well as default streams (stdin, stderr and stdout)
521 void msvcrt_init_io(void)
527 GetStartupInfoA(&si
);
528 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
534 count
= *(unsigned*)si
.lpReserved2
;
535 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
536 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
538 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
539 count
= min(count
, MSVCRT_MAX_FILES
);
540 for (i
= 0; i
< count
; i
++)
542 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
543 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
545 wxflag_ptr
++; handle_ptr
++;
547 MSVCRT_fdend
= max( 3, count
);
548 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
549 if (get_ioinfo_nolock(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
552 fdinfo
= get_ioinfo_nolock(MSVCRT_STDIN_FILENO
);
553 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
554 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
555 DWORD type
= GetFileType(h
);
557 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
558 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDIN_FILENO
);
561 fdinfo
= get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
);
562 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
563 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
564 DWORD type
= GetFileType(h
);
566 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
567 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDOUT_FILENO
);
570 fdinfo
= get_ioinfo_nolock(MSVCRT_STDERR_FILENO
);
571 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
572 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
573 DWORD type
= GetFileType(h
);
575 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
576 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDERR_FILENO
);
579 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
580 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
581 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
583 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
584 for (i
= 0; i
< 3; i
++)
586 /* FILE structs for stdin/out/err are static and never deleted */
587 MSVCRT__iob
[i
]._file
= i
;
588 MSVCRT__iob
[i
]._tmpfname
= NULL
;
589 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
591 MSVCRT_stream_idx
= 3;
594 /* INTERNAL: Flush stdio file buffer */
595 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
597 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
598 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
599 int cnt
=file
->_ptr
-file
->_base
;
600 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
601 file
->_flag
|= MSVCRT__IOERR
;
605 if(file
->_flag
& MSVCRT__IORW
)
606 file
->_flag
&= ~MSVCRT__IOWRT
;
609 file
->_ptr
=file
->_base
;
614 /*********************************************************************
617 int CDECL
MSVCRT__isatty(int fd
)
619 TRACE(":fd (%d)\n",fd
);
621 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
624 /* INTERNAL: Allocate stdio file buffer */
625 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
627 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
628 && MSVCRT__isatty(file
->_file
))
631 file
->_base
= MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
633 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
634 file
->_flag
|= MSVCRT__IOMYBUF
;
636 file
->_base
= (char*)(&file
->_charbuf
);
638 file
->_flag
|= MSVCRT__IONBF
;
640 file
->_ptr
= file
->_base
;
645 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
646 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
648 static char buffers
[2][MSVCRT_BUFSIZ
];
650 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
651 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
652 || !MSVCRT__isatty(file
->_file
))
655 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
656 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
657 file
->_flag
|= MSVCRT__USERBUF
;
661 /* INTERNAL: Removes temporary buffer from stdout or stderr */
662 /* Only call this function when add_std_buffer returned TRUE */
663 static void remove_std_buffer(MSVCRT_FILE
*file
)
665 msvcrt_flush_buffer(file
);
666 file
->_ptr
= file
->_base
= NULL
;
667 file
->_bufsiz
= file
->_cnt
= 0;
668 file
->_flag
&= ~MSVCRT__USERBUF
;
671 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
672 static int msvcrt_int_to_base32(int num
, char *str
)
687 *p
= (num
& 31) + '0';
689 *p
+= ('a' - '0' - 10);
696 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
697 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
712 *p
= (num
& 31) + '0';
714 *p
+= ('a' - '0' - 10);
721 /*********************************************************************
722 * __iob_func(MSVCRT.@)
724 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
726 return &MSVCRT__iob
[0];
729 /*********************************************************************
732 int CDECL
MSVCRT__access(const char *filename
, int mode
)
734 DWORD attr
= GetFileAttributesA(filename
);
736 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
738 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
740 msvcrt_set_errno(GetLastError());
743 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
745 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
751 /*********************************************************************
752 * _access_s (MSVCRT.@)
754 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
756 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
757 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
759 if (MSVCRT__access(filename
, mode
) == -1)
760 return *MSVCRT__errno();
764 /*********************************************************************
765 * _waccess (MSVCRT.@)
767 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
769 DWORD attr
= GetFileAttributesW(filename
);
771 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
773 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
775 msvcrt_set_errno(GetLastError());
778 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
780 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
786 /*********************************************************************
787 * _waccess_s (MSVCRT.@)
789 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
791 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
792 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
794 if (MSVCRT__waccess(filename
, mode
) == -1)
795 return *MSVCRT__errno();
799 /*********************************************************************
802 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
804 DWORD oldFlags
= GetFileAttributesA(path
);
806 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
808 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
809 oldFlags
| FILE_ATTRIBUTE_READONLY
;
811 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
814 msvcrt_set_errno(GetLastError());
818 /*********************************************************************
821 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
823 DWORD oldFlags
= GetFileAttributesW(path
);
825 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
827 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
828 oldFlags
| FILE_ATTRIBUTE_READONLY
;
830 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
833 msvcrt_set_errno(GetLastError());
837 /*********************************************************************
840 int CDECL
MSVCRT__unlink(const char *path
)
842 TRACE("%s\n",debugstr_a(path
));
843 if(DeleteFileA(path
))
845 TRACE("failed (%d)\n",GetLastError());
846 msvcrt_set_errno(GetLastError());
850 /*********************************************************************
851 * _wunlink (MSVCRT.@)
853 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
855 TRACE("(%s)\n",debugstr_w(path
));
856 if(DeleteFileW(path
))
858 TRACE("failed (%d)\n",GetLastError());
859 msvcrt_set_errno(GetLastError());
863 /*********************************************************************
866 int CDECL
MSVCRT__commit(int fd
)
868 ioinfo
*info
= get_ioinfo(fd
);
871 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
873 if (info
->handle
== INVALID_HANDLE_VALUE
)
875 else if (!FlushFileBuffers(info
->handle
))
877 if (GetLastError() == ERROR_INVALID_HANDLE
)
879 /* FlushFileBuffers fails for console handles
880 * so we ignore this error.
886 TRACE(":failed-last error (%d)\n",GetLastError());
887 msvcrt_set_errno(GetLastError());
897 release_ioinfo(info
);
901 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
902 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
904 /* INTERNAL: Flush all stream buffer */
905 static int msvcrt_flush_all_buffers(int mask
)
907 int i
, num_flushed
= 0;
911 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
912 file
= msvcrt_get_file(i
);
916 if(file
->_flag
& mask
) {
924 TRACE(":flushed (%d) handles\n",num_flushed
);
928 /*********************************************************************
929 * _flushall (MSVCRT.@)
931 int CDECL
MSVCRT__flushall(void)
933 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
936 /*********************************************************************
939 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
944 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
947 MSVCRT__lock_file(file
);
948 ret
= MSVCRT__fflush_nolock(file
);
949 MSVCRT__unlock_file(file
);
955 /*********************************************************************
956 * _fflush_nolock (MSVCRT.@)
958 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
963 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
967 res
= msvcrt_flush_buffer(file
);
968 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
969 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
973 /*********************************************************************
976 int CDECL
MSVCRT__close(int fd
)
978 ioinfo
*info
= get_ioinfo(fd
);
982 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
983 if (!(info
->wxflag
& WX_OPEN
)) {
986 ret
= CloseHandle(info
->handle
) ? 0 : -1;
989 WARN(":failed-last error (%d)\n",GetLastError());
990 msvcrt_set_errno(GetLastError());
994 release_ioinfo(info
);
998 /*********************************************************************
1001 * MSDN isn't clear on this point, but the remarks for _pipe
1002 * indicate file descriptors duplicated with _dup and _dup2 are always
1005 int CDECL
MSVCRT__dup2(int od
, int nd
)
1007 ioinfo
*info_od
, *info_nd
;
1010 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1015 info_od
= get_ioinfo(od
);
1016 info_nd
= get_ioinfo(nd
);
1020 info_nd
= get_ioinfo(nd
);
1021 info_od
= get_ioinfo(od
);
1024 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && (info_od
->wxflag
& WX_OPEN
))
1028 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1029 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1031 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1033 if (info_nd
->wxflag
& WX_OPEN
)
1035 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
1038 CloseHandle(handle
);
1039 *MSVCRT__errno() = MSVCRT_EMFILE
;
1043 /* _dup2 returns 0, not nd, on success */
1050 msvcrt_set_errno(GetLastError());
1055 *MSVCRT__errno() = MSVCRT_EBADF
;
1059 release_ioinfo(info_od
);
1060 release_ioinfo(info_nd
);
1065 /*********************************************************************
1068 int CDECL
MSVCRT__dup(int od
)
1073 fd
= MSVCRT_fdstart
;
1074 if (MSVCRT__dup2(od
, fd
) == 0)
1082 /*********************************************************************
1085 int CDECL
MSVCRT__eof(int fd
)
1087 ioinfo
*info
= get_ioinfo(fd
);
1088 DWORD curpos
,endpos
;
1089 LONG hcurpos
,hendpos
;
1091 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1093 if (info
->handle
== INVALID_HANDLE_VALUE
)
1095 release_ioinfo(info
);
1099 if (info
->wxflag
& WX_ATEOF
)
1101 release_ioinfo(info
);
1105 /* Otherwise we do it the hard way */
1106 hcurpos
= hendpos
= 0;
1107 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1108 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1110 if (curpos
== endpos
&& hcurpos
== hendpos
)
1112 /* FIXME: shouldn't WX_ATEOF be set here? */
1113 release_ioinfo(info
);
1117 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1118 release_ioinfo(info
);
1122 /*********************************************************************
1123 * _fcloseall (MSVCRT.@)
1125 int CDECL
MSVCRT__fcloseall(void)
1127 int num_closed
= 0, i
;
1131 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1132 file
= msvcrt_get_file(i
);
1134 if (file
->_flag
&& !MSVCRT_fclose(file
))
1139 TRACE(":closed (%d) handles\n",num_closed
);
1143 /* free everything on process exit */
1144 void msvcrt_free_io(void)
1150 MSVCRT__fcloseall();
1152 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1154 if(!MSVCRT___pioinfo
[i
])
1157 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1159 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1160 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1162 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1165 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1167 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1168 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1170 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1171 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1175 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1176 MSVCRT_free(MSVCRT_fstream
[i
]);
1178 DeleteCriticalSection(&MSVCRT_file_cs
);
1181 /*********************************************************************
1182 * _lseeki64 (MSVCRT.@)
1184 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1186 ioinfo
*info
= get_ioinfo(fd
);
1189 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1191 if (info
->handle
== INVALID_HANDLE_VALUE
)
1193 release_ioinfo(info
);
1197 if (whence
< 0 || whence
> 2)
1199 release_ioinfo(info
);
1200 *MSVCRT__errno() = MSVCRT_EINVAL
;
1204 TRACE(":fd (%d) to %s pos %s\n",
1205 fd
,wine_dbgstr_longlong(offset
),
1206 (whence
==SEEK_SET
)?"SEEK_SET":
1207 (whence
==SEEK_CUR
)?"SEEK_CUR":
1208 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1210 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1211 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1212 ofs
.QuadPart
= offset
;
1213 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1214 GetLastError() == ERROR_SUCCESS
)
1216 info
->wxflag
&= ~WX_ATEOF
;
1217 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1219 release_ioinfo(info
);
1220 return ofs
.QuadPart
;
1222 release_ioinfo(info
);
1223 TRACE(":error-last error (%d)\n",GetLastError());
1224 msvcrt_set_errno(GetLastError());
1228 /*********************************************************************
1231 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1233 return MSVCRT__lseeki64(fd
, offset
, whence
);
1236 /*********************************************************************
1237 * _lock_file (MSVCRT.@)
1239 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1241 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1242 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1244 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1247 /*********************************************************************
1248 * _unlock_file (MSVCRT.@)
1250 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1252 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1253 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1255 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1258 /*********************************************************************
1259 * _locking (MSVCRT.@)
1261 * This is untested; the underlying LockFile doesn't work yet.
1263 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1265 ioinfo
*info
= get_ioinfo(fd
);
1269 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1270 if (info
->handle
== INVALID_HANDLE_VALUE
)
1272 release_ioinfo(info
);
1276 if (mode
< 0 || mode
> 4)
1278 release_ioinfo(info
);
1279 *MSVCRT__errno() = MSVCRT_EINVAL
;
1283 TRACE(":fd (%d) by 0x%08x mode %s\n",
1284 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1285 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1286 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1287 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1288 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1291 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1293 release_ioinfo(info
);
1294 FIXME ("Seek failed\n");
1295 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1298 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1301 ret
= 1; /* just to satisfy gcc */
1304 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1309 else if (mode
== MSVCRT__LK_UNLCK
)
1310 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1312 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1313 /* FIXME - what about error settings? */
1314 release_ioinfo(info
);
1315 return ret
? 0 : -1;
1318 /*********************************************************************
1319 * _fseeki64 (MSVCRT.@)
1321 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1325 MSVCRT__lock_file(file
);
1326 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1327 MSVCRT__unlock_file(file
);
1332 /*********************************************************************
1333 * _fseeki64_nolock (MSVCRT.@)
1335 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1339 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1341 offset
+= MSVCRT__ftelli64_nolock(file
);
1344 /* Flush output if needed */
1345 msvcrt_flush_buffer(file
);
1346 /* Reset direction of i/o */
1347 if(file
->_flag
& MSVCRT__IORW
) {
1348 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1350 /* Clear end of file flag */
1351 file
->_flag
&= ~MSVCRT__IOEOF
;
1352 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1357 /*********************************************************************
1360 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1362 return MSVCRT__fseeki64( file
, offset
, whence
);
1365 /*********************************************************************
1366 * _fseek_nolock (MSVCRT.@)
1368 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1370 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1373 /*********************************************************************
1374 * _chsize_s (MSVCRT.@)
1376 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1382 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1384 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1387 info
= get_ioinfo(fd
);
1388 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1390 /* save the current file pointer */
1391 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1394 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1397 ret
= SetEndOfFile(info
->handle
);
1398 if (!ret
) msvcrt_set_errno(GetLastError());
1401 /* restore the file pointer */
1402 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1406 release_ioinfo(info
);
1407 return ret
? 0 : *MSVCRT__errno();
1410 /*********************************************************************
1411 * _chsize (MSVCRT.@)
1413 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1415 /* _chsize_s returns errno on failure but _chsize should return -1 */
1416 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1419 /*********************************************************************
1420 * clearerr (MSVCRT.@)
1422 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1424 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1426 MSVCRT__lock_file(file
);
1427 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1428 MSVCRT__unlock_file(file
);
1431 /*********************************************************************
1434 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1436 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1438 MSVCRT__lock_file(file
);
1439 MSVCRT__fseek_nolock(file
, 0L, SEEK_SET
);
1440 MSVCRT_clearerr(file
);
1441 MSVCRT__unlock_file(file
);
1444 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1446 int plus
= strchrW(mode
, '+') != NULL
;
1448 TRACE("%s\n", debugstr_w(mode
));
1450 while(*mode
== ' ') mode
++;
1455 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1456 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1459 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1460 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1463 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1464 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1467 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1471 *stream_flags
|= MSVCRT__commode
;
1473 while (*mode
&& *mode
!=',')
1477 *open_flags
|= MSVCRT__O_BINARY
;
1478 *open_flags
&= ~MSVCRT__O_TEXT
;
1481 *open_flags
|= MSVCRT__O_TEXT
;
1482 *open_flags
&= ~MSVCRT__O_BINARY
;
1485 *open_flags
|= MSVCRT__O_TEMPORARY
;
1488 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1491 *stream_flags
|= MSVCRT__IOCOMMIT
;
1494 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1497 *open_flags
|= MSVCRT__O_NOINHERIT
;
1506 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1509 ERR("incorrect mode flag: %c\n", mode
[-1]);
1515 static const WCHAR ccs
[] = {'c','c','s'};
1516 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1517 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1518 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1521 while(*mode
== ' ') mode
++;
1522 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1524 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1525 while(*mode
== ' ') mode
++;
1526 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1529 while(*mode
== ' ') mode
++;
1531 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1533 *open_flags
|= MSVCRT__O_U8TEXT
;
1534 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1536 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1538 *open_flags
|= MSVCRT__O_U16TEXT
;
1539 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1541 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1543 *open_flags
|= MSVCRT__O_WTEXT
;
1544 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1548 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1552 while(*mode
== ' ') mode
++;
1555 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1560 /*********************************************************************
1561 * _fdopen (MSVCRT.@)
1563 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1566 MSVCRT_wchar_t
*modeW
= NULL
;
1568 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1570 ret
= MSVCRT__wfdopen(fd
, modeW
);
1576 /*********************************************************************
1577 * _wfdopen (MSVCRT.@)
1579 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1581 int open_flags
, stream_flags
;
1584 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1587 if (!(file
= msvcrt_alloc_fp()))
1589 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1594 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1600 /*********************************************************************
1601 * _filelength (MSVCRT.@)
1603 LONG CDECL
MSVCRT__filelength(int fd
)
1605 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1608 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1611 if (endPos
!= curPos
)
1612 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1619 /*********************************************************************
1620 * _filelengthi64 (MSVCRT.@)
1622 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1624 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1627 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1630 if (endPos
!= curPos
)
1631 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1638 /*********************************************************************
1639 * _fileno (MSVCRT.@)
1641 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1643 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1647 /*********************************************************************
1648 * _fstat64 (MSVCRT.@)
1650 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1652 ioinfo
*info
= get_ioinfo(fd
);
1655 BY_HANDLE_FILE_INFORMATION hfi
;
1657 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1658 if (info
->handle
== INVALID_HANDLE_VALUE
)
1660 release_ioinfo(info
);
1666 WARN(":failed-NULL buf\n");
1667 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1668 release_ioinfo(info
);
1672 memset(&hfi
, 0, sizeof(hfi
));
1673 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1674 type
= GetFileType(info
->handle
);
1675 if (type
== FILE_TYPE_PIPE
)
1677 buf
->st_dev
= buf
->st_rdev
= fd
;
1678 buf
->st_mode
= S_IFIFO
;
1681 else if (type
== FILE_TYPE_CHAR
)
1683 buf
->st_dev
= buf
->st_rdev
= fd
;
1684 buf
->st_mode
= S_IFCHR
;
1687 else /* FILE_TYPE_DISK etc. */
1689 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1691 WARN(":failed-last error (%d)\n",GetLastError());
1692 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1693 release_ioinfo(info
);
1696 buf
->st_mode
= S_IFREG
| 0444;
1697 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1698 buf
->st_mode
|= 0222;
1699 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1700 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1702 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1703 buf
->st_mtime
= buf
->st_ctime
= dw
;
1704 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1706 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1708 release_ioinfo(info
);
1712 /*********************************************************************
1713 * _fstati64 (MSVCRT.@)
1715 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1718 struct MSVCRT__stat64 buf64
;
1720 ret
= MSVCRT__fstat64(fd
, &buf64
);
1722 msvcrt_stat64_to_stati64(&buf64
, buf
);
1726 /*********************************************************************
1729 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1731 struct MSVCRT__stat64 buf64
;
1733 ret
= MSVCRT__fstat64(fd
, &buf64
);
1735 msvcrt_stat64_to_stat(&buf64
, buf
);
1739 /*********************************************************************
1740 * _fstat32 (MSVCR80.@)
1742 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1745 struct MSVCRT__stat64 buf64
;
1747 ret
= MSVCRT__fstat64(fd
, &buf64
);
1749 msvcrt_stat64_to_stat32(&buf64
, buf
);
1753 /*********************************************************************
1754 * _fstat64i32 (MSVCR80.@)
1756 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1759 struct MSVCRT__stat64 buf64
;
1761 ret
= MSVCRT__fstat64(fd
, &buf64
);
1763 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1767 /*********************************************************************
1768 * _futime64 (MSVCRT.@)
1770 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1772 ioinfo
*info
= get_ioinfo(fd
);
1777 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1782 time_to_filetime( t
->actime
, &at
);
1783 time_to_filetime( t
->modtime
, &wt
);
1786 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1788 release_ioinfo(info
);
1789 msvcrt_set_errno(GetLastError());
1792 release_ioinfo(info
);
1796 /*********************************************************************
1797 * _futime32 (MSVCRT.@)
1799 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1803 struct MSVCRT___utimbuf64 t64
;
1804 t64
.actime
= t
->actime
;
1805 t64
.modtime
= t
->modtime
;
1806 return _futime64( fd
, &t64
);
1809 return _futime64( fd
, NULL
);
1812 /*********************************************************************
1813 * _futime (MSVCRT.@)
1816 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1818 return _futime64( fd
, t
);
1821 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1823 return _futime32( fd
, t
);
1827 /*********************************************************************
1828 * _get_osfhandle (MSVCRT.@)
1830 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1832 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1833 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1835 if(hand
== INVALID_HANDLE_VALUE
)
1836 *MSVCRT__errno() = MSVCRT_EBADF
;
1837 return (MSVCRT_intptr_t
)hand
;
1840 /*********************************************************************
1841 * _mktemp_s (MSVCRT.@)
1843 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1847 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1848 return MSVCRT_EINVAL
;
1850 for(len
=0; len
<size
; len
++)
1853 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1856 return MSVCRT_EINVAL
;
1859 for(xno
=1; xno
<=6; xno
++)
1860 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1861 return MSVCRT_EINVAL
;
1863 id
= GetCurrentProcessId();
1864 for(xno
=1; xno
<6; xno
++) {
1865 pattern
[len
-xno
] = id
%10 + '0';
1869 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1870 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1875 *MSVCRT__errno() = MSVCRT_EEXIST
;
1876 return MSVCRT_EEXIST
;
1879 /*********************************************************************
1880 * _mktemp (MSVCRT.@)
1882 char * CDECL
MSVCRT__mktemp(char *pattern
)
1885 char *retVal
= pattern
;
1893 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1897 id
= GetCurrentProcessId();
1901 int tempNum
= id
/ 10;
1902 *pattern
-- = id
- (tempNum
* 10) + '0';
1908 *pattern
= letter
++;
1909 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1911 } while(letter
<= 'z');
1915 /*********************************************************************
1916 * _wmktemp_s (MSVCRT.@)
1918 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1922 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1923 return MSVCRT_EINVAL
;
1925 for(len
=0; len
<size
; len
++)
1928 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1931 return MSVCRT_EINVAL
;
1934 for(xno
=1; xno
<=6; xno
++)
1935 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1936 return MSVCRT_EINVAL
;
1938 id
= GetCurrentProcessId();
1939 for(xno
=1; xno
<6; xno
++) {
1940 pattern
[len
-xno
] = id
%10 + '0';
1944 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1945 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1950 *MSVCRT__errno() = MSVCRT_EEXIST
;
1951 return MSVCRT_EEXIST
;
1954 /*********************************************************************
1955 * _wmktemp (MSVCRT.@)
1957 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1960 MSVCRT_wchar_t
*retVal
= pattern
;
1962 MSVCRT_wchar_t letter
= 'a';
1968 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1972 id
= GetCurrentProcessId();
1976 int tempNum
= id
/ 10;
1977 *pattern
-- = id
- (tempNum
* 10) + '0';
1983 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1985 *pattern
= letter
++;
1986 } while(letter
!= '|');
1990 static unsigned split_oflags(unsigned oflags
)
1993 unsigned unsupp
; /* until we support everything */
1995 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1996 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1997 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1998 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1999 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2000 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2001 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2002 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2003 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2005 if ((unsupp
= oflags
& ~(
2006 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2007 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2008 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2009 MSVCRT__O_NOINHERIT
|
2010 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2011 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2013 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2018 /*********************************************************************
2021 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2024 SECURITY_ATTRIBUTES sa
;
2025 HANDLE readHandle
, writeHandle
;
2029 *MSVCRT__errno() = MSVCRT_EINVAL
;
2033 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2034 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2035 sa
.lpSecurityDescriptor
= NULL
;
2036 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2038 unsigned int wxflags
= split_oflags(textmode
);
2042 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2046 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2054 MSVCRT__close(pfds
[0]);
2055 CloseHandle(writeHandle
);
2056 *MSVCRT__errno() = MSVCRT_EMFILE
;
2061 CloseHandle(readHandle
);
2062 CloseHandle(writeHandle
);
2063 *MSVCRT__errno() = MSVCRT_EMFILE
;
2068 msvcrt_set_errno(GetLastError());
2073 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2075 char bom
[sizeof(utf8_bom
)];
2078 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2080 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2083 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2084 oflags
|= MSVCRT__O_U8TEXT
;
2085 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2087 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2088 oflags
|= MSVCRT__O_U16TEXT
;
2090 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2096 /*********************************************************************
2097 * _wsopen_s (MSVCRT.@)
2099 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2101 DWORD access
= 0, creation
= 0, attrib
;
2102 SECURITY_ATTRIBUTES sa
;
2103 DWORD sharing
, type
;
2107 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2108 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2110 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2113 wxflag
= split_oflags(oflags
);
2114 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2116 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2117 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2118 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2121 if (oflags
& MSVCRT__O_CREAT
)
2123 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2124 FIXME(": pmode 0x%04x ignored\n", pmode
);
2126 if (oflags
& MSVCRT__O_EXCL
)
2127 creation
= CREATE_NEW
;
2128 else if (oflags
& MSVCRT__O_TRUNC
)
2129 creation
= CREATE_ALWAYS
;
2131 creation
= OPEN_ALWAYS
;
2133 else /* no MSVCRT__O_CREAT */
2135 if (oflags
& MSVCRT__O_TRUNC
)
2136 creation
= TRUNCATE_EXISTING
;
2138 creation
= OPEN_EXISTING
;
2143 case MSVCRT__SH_DENYRW
:
2146 case MSVCRT__SH_DENYWR
:
2147 sharing
= FILE_SHARE_READ
;
2149 case MSVCRT__SH_DENYRD
:
2150 sharing
= FILE_SHARE_WRITE
;
2152 case MSVCRT__SH_DENYNO
:
2153 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2156 ERR( "Unhandled shflags 0x%x\n", shflags
);
2157 return MSVCRT_EINVAL
;
2160 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2161 attrib
= FILE_ATTRIBUTE_READONLY
;
2163 attrib
= FILE_ATTRIBUTE_NORMAL
;
2165 if (oflags
& MSVCRT__O_TEMPORARY
)
2167 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2169 sharing
|= FILE_SHARE_DELETE
;
2172 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2173 sa
.lpSecurityDescriptor
= NULL
;
2174 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2176 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2177 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2178 && !(access
&GENERIC_READ
))
2180 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2181 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2182 if (hand
!= INVALID_HANDLE_VALUE
)
2184 oflags
= check_bom(hand
, oflags
, FALSE
);
2188 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2191 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2192 if (hand
== INVALID_HANDLE_VALUE
) {
2193 WARN(":failed-last error (%d)\n",GetLastError());
2194 msvcrt_set_errno(GetLastError());
2195 return *MSVCRT__errno();
2198 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2200 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2201 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2202 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2204 if (oflags
& MSVCRT__O_U8TEXT
)
2206 DWORD written
= 0, tmp
;
2208 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2209 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2211 if (written
!= sizeof(utf8_bom
)) {
2212 WARN("error writing BOM\n");
2214 msvcrt_set_errno(GetLastError());
2215 return *MSVCRT__errno();
2220 DWORD written
= 0, tmp
;
2222 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2223 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2225 if (written
!= sizeof(utf16_bom
))
2227 WARN("error writing BOM\n");
2229 msvcrt_set_errno(GetLastError());
2230 return *MSVCRT__errno();
2234 else if (access
& GENERIC_READ
)
2235 oflags
= check_bom(hand
, oflags
, TRUE
);
2238 type
= GetFileType(hand
);
2239 if (type
== FILE_TYPE_CHAR
)
2241 else if (type
== FILE_TYPE_PIPE
)
2244 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2246 return *MSVCRT__errno();
2248 if (oflags
& MSVCRT__O_WTEXT
)
2249 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2250 else if (oflags
& MSVCRT__O_U16TEXT
)
2251 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2252 else if (oflags
& MSVCRT__O_U8TEXT
)
2253 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2255 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2259 /*********************************************************************
2260 * _wsopen (MSVCRT.@)
2262 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2267 if (oflags
& MSVCRT__O_CREAT
)
2271 __ms_va_start(ap
, shflags
);
2272 pmode
= va_arg(ap
, int);
2278 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2282 /*********************************************************************
2283 * _sopen_s (MSVCRT.@)
2285 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2287 MSVCRT_wchar_t
*pathW
;
2290 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2291 return MSVCRT_EINVAL
;
2293 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2294 return MSVCRT_EINVAL
;
2296 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2301 /*********************************************************************
2304 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2309 if (oflags
& MSVCRT__O_CREAT
)
2313 __ms_va_start(ap
, shflags
);
2314 pmode
= va_arg(ap
, int);
2320 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2324 /*********************************************************************
2327 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2331 if (flags
& MSVCRT__O_CREAT
)
2334 __ms_va_start(ap
, flags
);
2335 pmode
= va_arg(ap
, int);
2337 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2340 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2343 /*********************************************************************
2346 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2350 if (flags
& MSVCRT__O_CREAT
)
2353 __ms_va_start(ap
, flags
);
2354 pmode
= va_arg(ap
, int);
2356 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2359 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2362 /*********************************************************************
2365 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2367 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2368 return MSVCRT__open(path
, usedFlags
);
2371 /*********************************************************************
2372 * _wcreat (MSVCRT.@)
2374 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2376 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2377 return MSVCRT__wopen(path
, usedFlags
);
2380 /*********************************************************************
2381 * _open_osfhandle (MSVCRT.@)
2383 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2388 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2389 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2390 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2391 * text - it never sets MSVCRT__O_BINARY.
2393 /* don't let split_oflags() decide the mode if no mode is passed */
2394 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2395 oflags
|= MSVCRT__O_BINARY
;
2397 flags
= GetFileType((HANDLE
)handle
);
2398 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2400 msvcrt_set_errno(GetLastError());
2404 if (flags
== FILE_TYPE_CHAR
)
2406 else if (flags
== FILE_TYPE_PIPE
)
2410 flags
|= split_oflags(oflags
);
2412 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2413 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2417 /*********************************************************************
2420 int CDECL
MSVCRT__rmtmp(void)
2422 int num_removed
= 0, i
;
2426 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2427 file
= msvcrt_get_file(i
);
2429 if (file
->_tmpfname
)
2431 MSVCRT_fclose(file
);
2438 TRACE(":removed (%d) temp files\n",num_removed
);
2442 static inline int get_utf8_char_len(char ch
)
2444 if((ch
&0xf8) == 0xf0)
2446 else if((ch
&0xf0) == 0xe0)
2448 else if((ch
&0xe0) == 0xc0)
2453 /*********************************************************************
2454 * (internal) read_utf8
2456 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2458 HANDLE hand
= fdinfo
->handle
;
2459 char min_buf
[4], *readbuf
, lookahead
;
2460 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2462 /* make the buffer big enough to hold at least one character */
2463 /* read bytes have to fit to output and lookahead buffers */
2465 readbuf_size
= count
< 4 ? 4 : count
;
2466 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2471 if(fdinfo
->lookahead
[0] != '\n') {
2472 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2473 fdinfo
->lookahead
[0] = '\n';
2475 if(fdinfo
->lookahead
[1] != '\n') {
2476 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2477 fdinfo
->lookahead
[1] = '\n';
2479 if(fdinfo
->lookahead
[2] != '\n') {
2480 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2481 fdinfo
->lookahead
[2] = '\n';
2486 /* NOTE: this case is broken in native dll, reading
2487 * sometimes fails when small buffer is passed
2490 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2491 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2492 fdinfo
->wxflag
|= WX_ATEOF
;
2495 msvcrt_set_errno(GetLastError());
2498 }else if(!num_read
) {
2499 fdinfo
->wxflag
|= WX_ATEOF
;
2505 char_len
= get_utf8_char_len(readbuf
[0]);
2507 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2511 if(readbuf
[0] == '\n')
2512 fdinfo
->wxflag
|= WX_READNL
;
2514 fdinfo
->wxflag
&= ~WX_READNL
;
2516 if(readbuf
[0] == 0x1a) {
2517 fdinfo
->wxflag
|= WX_ATEOF
;
2521 if(readbuf
[0] == '\r') {
2522 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2524 else if(lookahead
== '\n')
2528 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2529 fdinfo
->lookahead
[0] = lookahead
;
2531 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2536 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2537 msvcrt_set_errno(GetLastError());
2544 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2547 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2548 fdinfo
->wxflag
|= WX_ATEOF
;
2549 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2552 msvcrt_set_errno(GetLastError());
2553 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2556 }else if(!pos
&& !num_read
) {
2557 fdinfo
->wxflag
|= WX_ATEOF
;
2558 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2563 if(readbuf
[0] == '\n')
2564 fdinfo
->wxflag
|= WX_READNL
;
2566 fdinfo
->wxflag
&= ~WX_READNL
;
2568 /* Find first byte of last character (may be incomplete) */
2569 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2570 if((readbuf
[i
]&0xc0) != 0x80)
2572 char_len
= get_utf8_char_len(readbuf
[i
]);
2573 if(char_len
+i
<= pos
)
2576 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2578 fdinfo
->lookahead
[0] = readbuf
[i
];
2580 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2582 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2584 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2588 for(i
=0, j
=0; i
<pos
; i
++) {
2589 if(readbuf
[i
] == 0x1a) {
2590 fdinfo
->wxflag
|= WX_ATEOF
;
2594 /* strip '\r' if followed by '\n' */
2595 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2596 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2597 readbuf
[j
++] = '\r';
2598 }else if(lookahead
== '\n' && j
==0) {
2599 readbuf
[j
++] = '\n';
2601 if(lookahead
!= '\n')
2602 readbuf
[j
++] = '\r';
2604 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2605 fdinfo
->lookahead
[0] = lookahead
;
2607 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2609 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2610 readbuf
[j
++] = readbuf
[i
];
2615 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2616 msvcrt_set_errno(GetLastError());
2617 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2621 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2625 /*********************************************************************
2628 * When reading \r as last character in text mode, read() positions
2629 * the file pointer on the \r character while getc() goes on to
2632 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2634 DWORD num_read
, utf16
;
2635 char *bufstart
= buf
;
2640 if (fdinfo
->wxflag
& WX_ATEOF
) {
2641 TRACE("already at EOF, returning 0\n");
2644 /* Don't trace small reads, it gets *very* annoying */
2646 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2647 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2649 *MSVCRT__errno() = MSVCRT_EBADF
;
2653 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2654 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2656 *MSVCRT__errno() = MSVCRT_EINVAL
;
2660 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2661 return read_utf8(fdinfo
, buf
, count
);
2663 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2665 if (fdinfo
->lookahead
[0] != '\n')
2667 bufstart
[0] = fdinfo
->lookahead
[0];
2668 fdinfo
->lookahead
[0] = '\n';
2672 bufstart
[1] = fdinfo
->lookahead
[1];
2673 fdinfo
->lookahead
[1] = '\n';
2676 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2677 num_read
+= 1+utf16
;
2682 if(utf16
&& (num_read
&1))
2684 /* msvcr90 uses uninitialized value from the buffer in this case */
2685 /* msvcrt ignores additional data */
2686 ERR("got odd number of bytes in UTF16 mode\n");
2690 if (count
!= 0 && num_read
== 0)
2692 fdinfo
->wxflag
|= WX_ATEOF
;
2693 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2695 else if (fdinfo
->wxflag
& WX_TEXT
)
2699 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2700 fdinfo
->wxflag
|= WX_READNL
;
2702 fdinfo
->wxflag
&= ~WX_READNL
;
2704 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2706 /* in text mode, a ctrl-z signals EOF */
2707 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2709 fdinfo
->wxflag
|= WX_ATEOF
;
2710 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2714 /* in text mode, strip \r if followed by \n */
2715 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2720 lookahead
[1] = '\n';
2721 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2723 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2725 bufstart
[j
++] = '\n';
2726 if(utf16
) bufstart
[j
++] = 0;
2730 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2732 bufstart
[j
++] = '\r';
2733 if(utf16
) bufstart
[j
++] = 0;
2736 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2738 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2740 bufstart
[j
++] = '\n';
2741 if (utf16
) bufstart
[j
++] = 0;
2745 fdinfo
->lookahead
[0] = lookahead
[0];
2746 fdinfo
->lookahead
[1] = lookahead
[1];
2750 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2755 bufstart
[j
++] = '\r';
2756 if(utf16
) bufstart
[j
++] = 0;
2759 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2760 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2762 bufstart
[j
++] = bufstart
[i
];
2763 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2771 if (GetLastError() == ERROR_BROKEN_PIPE
)
2773 TRACE(":end-of-pipe\n");
2774 fdinfo
->wxflag
|= WX_ATEOF
;
2779 TRACE(":failed-last error (%d)\n",GetLastError());
2785 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2789 /*********************************************************************
2792 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2794 ioinfo
*info
= get_ioinfo(fd
);
2795 int num_read
= read_i(fd
, info
, buf
, count
);
2796 release_ioinfo(info
);
2800 /*********************************************************************
2801 * _setmode (MSVCRT.@)
2803 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2805 int ret
= get_ioinfo_nolock(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2806 if(ret
==MSVCRT__O_TEXT
&& (get_ioinfo_nolock(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2807 ret
= MSVCRT__O_WTEXT
;
2809 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2810 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2811 *MSVCRT__errno() = MSVCRT_EINVAL
;
2815 if(mode
== MSVCRT__O_BINARY
) {
2816 get_ioinfo_nolock(fd
)->wxflag
&= ~WX_TEXT
;
2817 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2821 get_ioinfo_nolock(fd
)->wxflag
|= WX_TEXT
;
2822 if(mode
== MSVCRT__O_TEXT
)
2823 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2824 else if(mode
== MSVCRT__O_U8TEXT
)
2825 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2827 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2832 /*********************************************************************
2833 * _stat64 (MSVCRT.@)
2835 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2838 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2839 unsigned short mode
= ALL_S_IREAD
;
2842 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2844 plen
= strlen(path
);
2845 while (plen
&& path
[plen
-1]==' ')
2848 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2849 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2851 *MSVCRT__errno() = MSVCRT_ENOENT
;
2855 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2857 TRACE("failed (%d)\n",GetLastError());
2858 *MSVCRT__errno() = MSVCRT_ENOENT
;
2862 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2864 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2865 Bon 011120: This FIXME seems incorrect
2866 Also a letter as first char isn't enough to be classified
2869 if (isalpha(*path
)&& (*(path
+1)==':'))
2870 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2872 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2874 /* Dir, or regular file? */
2875 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2876 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2879 mode
|= MSVCRT__S_IFREG
;
2881 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2883 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2884 (tolower(path
[plen
-3]) << 16);
2885 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2886 mode
|= ALL_S_IEXEC
;
2890 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2891 mode
|= ALL_S_IWRITE
;
2893 buf
->st_mode
= mode
;
2895 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2896 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2898 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2899 buf
->st_mtime
= buf
->st_ctime
= dw
;
2900 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2901 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2902 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2906 /*********************************************************************
2907 * _stati64 (MSVCRT.@)
2909 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2912 struct MSVCRT__stat64 buf64
;
2914 ret
= MSVCRT_stat64(path
, &buf64
);
2916 msvcrt_stat64_to_stati64(&buf64
, buf
);
2920 /*********************************************************************
2923 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2926 struct MSVCRT__stat64 buf64
;
2928 ret
= MSVCRT_stat64( path
, &buf64
);
2930 msvcrt_stat64_to_stat(&buf64
, buf
);
2934 /*********************************************************************
2935 * _stat32 (MSVCR100.@)
2937 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2940 struct MSVCRT__stat64 buf64
;
2942 ret
= MSVCRT_stat64(path
, &buf64
);
2944 msvcrt_stat64_to_stat32(&buf64
, buf
);
2948 /*********************************************************************
2949 * _stat32i64 (MSVCR100.@)
2951 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2954 struct MSVCRT__stat64 buf64
;
2956 ret
= MSVCRT_stat64(path
, &buf64
);
2958 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2962 /*********************************************************************
2963 * _stat64i32 (MSVCR100.@)
2965 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2968 struct MSVCRT__stat64 buf64
;
2970 ret
= MSVCRT_stat64(path
, &buf64
);
2972 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2976 /*********************************************************************
2977 * _wstat64 (MSVCRT.@)
2979 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2982 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2983 unsigned short mode
= ALL_S_IREAD
;
2986 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2988 plen
= strlenW(path
);
2989 while (plen
&& path
[plen
-1]==' ')
2992 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2993 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2995 *MSVCRT__errno() = MSVCRT_ENOENT
;
2999 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3001 TRACE("failed (%d)\n",GetLastError());
3002 *MSVCRT__errno() = MSVCRT_ENOENT
;
3006 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3008 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3009 if (MSVCRT_iswalpha(*path
))
3010 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3012 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3014 /* Dir, or regular file? */
3015 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3016 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3019 mode
|= MSVCRT__S_IFREG
;
3021 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3023 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3024 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3025 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3026 mode
|= ALL_S_IEXEC
;
3030 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3031 mode
|= ALL_S_IWRITE
;
3033 buf
->st_mode
= mode
;
3035 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3036 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3038 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3039 buf
->st_mtime
= buf
->st_ctime
= dw
;
3040 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3041 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3042 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3046 /*********************************************************************
3047 * _wstati64 (MSVCRT.@)
3049 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3052 struct MSVCRT__stat64 buf64
;
3054 ret
= MSVCRT__wstat64(path
, &buf64
);
3056 msvcrt_stat64_to_stati64(&buf64
, buf
);
3060 /*********************************************************************
3063 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3066 struct MSVCRT__stat64 buf64
;
3068 ret
= MSVCRT__wstat64( path
, &buf64
);
3069 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3073 /*********************************************************************
3074 * _wstat32 (MSVCR100.@)
3076 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3079 struct MSVCRT__stat64 buf64
;
3081 ret
= MSVCRT__wstat64(path
, &buf64
);
3083 msvcrt_stat64_to_stat32(&buf64
, buf
);
3087 /*********************************************************************
3088 * _wstat32i64 (MSVCR100.@)
3090 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3093 struct MSVCRT__stat64 buf64
;
3095 ret
= MSVCRT__wstat64(path
, &buf64
);
3097 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3101 /*********************************************************************
3102 * _wstat64i32 (MSVCR100.@)
3104 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3107 struct MSVCRT__stat64 buf64
;
3109 ret
= MSVCRT__wstat64(path
, &buf64
);
3111 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3115 /*********************************************************************
3118 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3120 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3123 /*********************************************************************
3124 * _telli64 (MSVCRT.@)
3126 __int64 CDECL
_telli64(int fd
)
3128 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3131 /*********************************************************************
3132 * _tempnam (MSVCRT.@)
3134 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3136 char tmpbuf
[MAX_PATH
];
3137 const char *tmp_dir
= MSVCRT_getenv("TMP");
3139 if (tmp_dir
) dir
= tmp_dir
;
3141 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3142 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3144 TRACE("got name (%s)\n",tmpbuf
);
3145 DeleteFileA(tmpbuf
);
3146 return MSVCRT__strdup(tmpbuf
);
3148 TRACE("failed (%d)\n",GetLastError());
3152 /*********************************************************************
3153 * _wtempnam (MSVCRT.@)
3155 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3157 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3158 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3159 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3161 if (tmp_dir
) dir
= tmp_dir
;
3163 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3164 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3166 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3167 DeleteFileW(tmpbuf
);
3168 return MSVCRT__wcsdup(tmpbuf
);
3170 TRACE("failed (%d)\n",GetLastError());
3174 /*********************************************************************
3177 int CDECL
MSVCRT__umask(int umask
)
3179 int old_umask
= MSVCRT_umask
;
3180 TRACE("(%d)\n",umask
);
3181 MSVCRT_umask
= umask
;
3185 /*********************************************************************
3186 * _utime64 (MSVCRT.@)
3188 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3190 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3194 int retVal
= _futime64(fd
, t
);
3201 /*********************************************************************
3202 * _utime32 (MSVCRT.@)
3204 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3208 struct MSVCRT___utimbuf64 t64
;
3209 t64
.actime
= t
->actime
;
3210 t64
.modtime
= t
->modtime
;
3211 return _utime64( path
, &t64
);
3214 return _utime64( path
, NULL
);
3217 /*********************************************************************
3221 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3223 return _utime64( path
, t
);
3226 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3228 return _utime32( path
, t
);
3232 /*********************************************************************
3233 * _wutime64 (MSVCRT.@)
3235 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3237 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3241 int retVal
= _futime64(fd
, t
);
3248 /*********************************************************************
3249 * _wutime32 (MSVCRT.@)
3251 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3255 struct MSVCRT___utimbuf64 t64
;
3256 t64
.actime
= t
->actime
;
3257 t64
.modtime
= t
->modtime
;
3258 return _wutime64( path
, &t64
);
3261 return _wutime64( path
, NULL
);
3264 /*********************************************************************
3265 * _wutime (MSVCRT.@)
3268 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3270 return _wutime64( path
, t
);
3273 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3275 return _wutime32( path
, t
);
3279 /*********************************************************************
3282 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3285 ioinfo
*info
= get_ioinfo_nolock(fd
);
3286 HANDLE hand
= info
->handle
;
3288 /* Don't trace small writes, it gets *very* annoying */
3291 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3293 if (hand
== INVALID_HANDLE_VALUE
)
3295 *MSVCRT__errno() = MSVCRT_EBADF
;
3299 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3301 *MSVCRT__errno() = MSVCRT_EINVAL
;
3305 /* If appending, go to EOF */
3306 if (info
->wxflag
& WX_APPEND
)
3307 MSVCRT__lseek(fd
, 0, FILE_END
);
3309 if (!(info
->wxflag
& WX_TEXT
))
3311 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3312 && (num_written
== count
))
3314 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3315 hand
, GetLastError());
3316 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3320 unsigned int i
, j
, nr_lf
, size
;
3323 const char *s
= buf
, *buf_start
= buf
;
3325 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3327 /* find number of \n */
3328 for (nr_lf
=0, i
=0; i
<count
; i
++)
3334 if ((q
= p
= MSVCRT_malloc(size
)))
3336 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3345 FIXME("Malloc failed\n");
3357 else if (info
->exflag
& EF_UTF16
)
3359 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3360 if (s
[i
]=='\n' && s
[i
+1]==0)
3365 if ((q
= p
= MSVCRT_malloc(size
)))
3367 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3369 if (s
[i
]=='\n' && s
[i
+1]==0)
3380 FIXME("Malloc failed\n");
3396 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3397 if (s
[i
]=='\n' && s
[i
+1]==0)
3400 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3402 msvcrt_set_errno(GetLastError());
3407 size
= conv_len
+nr_lf
;
3408 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3410 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3412 if (s
[i
]=='\n' && s
[i
+1]==0)
3420 q
= p
+count
+nr_lf
*2;
3421 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3422 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3426 FIXME("Malloc failed\n");
3433 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3437 if (num_written
!= size
)
3439 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3440 fd
, hand
, GetLastError(), num_written
);
3441 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3442 return s
- buf_start
;
3450 /*********************************************************************
3453 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3457 MSVCRT__lock_file(file
);
3458 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3459 if (len
== sizeof(val
)) {
3460 MSVCRT__unlock_file(file
);
3464 file
->_flag
|= MSVCRT__IOERR
;
3465 MSVCRT__unlock_file(file
);
3469 /*********************************************************************
3472 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3476 MSVCRT__lock_file(file
);
3477 ret
= MSVCRT__fclose_nolock(file
);
3478 MSVCRT__unlock_file(file
);
3483 /*********************************************************************
3484 * _fclose_nolock (MSVCRT.@)
3486 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3491 MSVCRT_free(file
->_tmpfname
);
3492 file
->_tmpfname
= NULL
;
3493 /* flush stdio buffers */
3494 if(file
->_flag
& MSVCRT__IOWRT
)
3495 MSVCRT__fflush_nolock(file
);
3496 if(file
->_flag
& MSVCRT__IOMYBUF
)
3497 MSVCRT_free(file
->_base
);
3499 r
=MSVCRT__close(file
->_file
);
3502 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3505 /*********************************************************************
3508 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3510 return file
->_flag
& MSVCRT__IOEOF
;
3513 /*********************************************************************
3516 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3518 return file
->_flag
& MSVCRT__IOERR
;
3521 /*********************************************************************
3522 * _filbuf (MSVCRT.@)
3524 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3528 if(file
->_flag
& MSVCRT__IOSTRG
)
3531 /* Allocate buffer if needed */
3532 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3533 msvcrt_alloc_buffer(file
);
3535 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3536 if(file
->_flag
& MSVCRT__IORW
)
3537 file
->_flag
|= MSVCRT__IOREAD
;
3542 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3544 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3545 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3551 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3553 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3559 file
->_ptr
= file
->_base
+1;
3560 c
= *(unsigned char *)file
->_base
;
3565 /*********************************************************************
3568 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3572 MSVCRT__lock_file(file
);
3573 ret
= MSVCRT__fgetc_nolock(file
);
3574 MSVCRT__unlock_file(file
);
3579 /*********************************************************************
3580 * _fgetc_nolock (MSVCRT.@)
3582 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3589 i
= (unsigned char *)file
->_ptr
++;
3592 j
= MSVCRT__filbuf(file
);
3597 /*********************************************************************
3598 * _fgetchar (MSVCRT.@)
3600 int CDECL
MSVCRT__fgetchar(void)
3602 return MSVCRT_fgetc(MSVCRT_stdin
);
3605 /*********************************************************************
3608 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3610 int cc
= MSVCRT_EOF
;
3611 char * buf_start
= s
;
3613 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3614 file
,file
->_file
,s
,size
);
3616 MSVCRT__lock_file(file
);
3618 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3623 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3625 TRACE(":nothing read\n");
3626 MSVCRT__unlock_file(file
);
3629 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3632 TRACE(":got %s\n", debugstr_a(buf_start
));
3633 MSVCRT__unlock_file(file
);
3637 /*********************************************************************
3640 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3644 MSVCRT__lock_file(file
);
3645 ret
= MSVCRT__fgetwc_nolock(file
);
3646 MSVCRT__unlock_file(file
);
3651 /*********************************************************************
3652 * _fgetwc_nolock (MSVCRT.@)
3654 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3659 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3660 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3663 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3664 ch
= MSVCRT__fgetc_nolock(file
);
3665 if(ch
== MSVCRT_EOF
) {
3672 char mbs
[MSVCRT_MB_LEN_MAX
];
3675 ch
= MSVCRT__fgetc_nolock(file
);
3676 if(ch
!= MSVCRT_EOF
) {
3678 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3679 ch
= MSVCRT__fgetc_nolock(file
);
3680 if(ch
!= MSVCRT_EOF
) {
3689 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3696 /*********************************************************************
3699 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3706 MSVCRT__lock_file(file
);
3707 for (j
=0; j
<sizeof(int); j
++) {
3708 k
= MSVCRT__fgetc_nolock(file
);
3709 if (k
== MSVCRT_EOF
) {
3710 file
->_flag
|= MSVCRT__IOEOF
;
3711 MSVCRT__unlock_file(file
);
3717 MSVCRT__unlock_file(file
);
3721 /*********************************************************************
3724 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3726 return MSVCRT_fgetwc(file
);
3729 /*********************************************************************
3730 * _fgetwchar (MSVCRT.@)
3732 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3734 return MSVCRT_fgetwc(MSVCRT_stdin
);
3737 /*********************************************************************
3738 * getwchar (MSVCRT.@)
3740 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3742 return MSVCRT__fgetwchar();
3745 /*********************************************************************
3748 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3750 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3751 MSVCRT_wchar_t
* buf_start
= s
;
3753 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3754 file
,file
->_file
,s
,size
);
3756 MSVCRT__lock_file(file
);
3758 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3763 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3765 TRACE(":nothing read\n");
3766 MSVCRT__unlock_file(file
);
3769 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3772 TRACE(":got %s\n", debugstr_w(buf_start
));
3773 MSVCRT__unlock_file(file
);
3777 /*********************************************************************
3778 * _flsbuf (MSVCRT.@)
3780 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3782 /* Flush output buffer */
3783 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3784 msvcrt_alloc_buffer(file
);
3787 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3788 if(!(file
->_flag
& MSVCRT__IORW
)) {
3789 file
->_flag
|= MSVCRT__IOERR
;
3792 file
->_flag
|= MSVCRT__IOWRT
;
3794 if(file
->_flag
& MSVCRT__IOREAD
) {
3795 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3796 file
->_flag
|= MSVCRT__IOERR
;
3800 file
->_ptr
= file
->_base
;
3801 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3804 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3807 if(file
->_cnt
<= 0) {
3808 res
= msvcrt_flush_buffer(file
);
3811 file
->_flag
|= MSVCRT__IOWRT
;
3812 file
->_cnt
=file
->_bufsiz
;
3820 /* set _cnt to 0 for unbuffered FILEs */
3822 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3825 file
->_flag
|= MSVCRT__IOERR
;
3830 /*********************************************************************
3833 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3837 MSVCRT__lock_file(file
);
3838 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3839 MSVCRT__unlock_file(file
);
3844 /*********************************************************************
3845 * _fwrite_nolock (MSVCRT.@)
3847 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3849 MSVCRT_size_t wrcnt
=size
* nmemb
;
3855 if(file
->_cnt
< 0) {
3856 WARN("negative file->_cnt value in %p\n", file
);
3857 file
->_flag
|= MSVCRT__IOERR
;
3859 } else if(file
->_cnt
) {
3860 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3861 memcpy(file
->_ptr
, ptr
, pcnt
);
3866 ptr
= (const char*)ptr
+ pcnt
;
3867 } else if((file
->_flag
& MSVCRT__IONBF
)
3868 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3869 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3873 if(file
->_flag
& MSVCRT__IONBF
)
3875 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3876 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3878 bufsiz
= file
->_bufsiz
;
3880 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3882 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3885 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3886 file
->_flag
|= MSVCRT__IOERR
;
3891 ptr
= (const char*)ptr
+ pcnt
;
3893 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3897 ptr
= (const char*)ptr
+ 1;
3901 return written
/ size
;
3904 /*********************************************************************
3907 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3911 MSVCRT__lock_file(file
);
3912 ret
= MSVCRT__fputwc_nolock(wc
, file
);
3913 MSVCRT__unlock_file(file
);
3918 /*********************************************************************
3919 * _fputwc_nolock (MSVCRT.@)
3921 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3923 MSVCRT_wchar_t mwc
=wc
;
3927 fdinfo
= get_ioinfo_nolock(file
->_file
);
3929 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3930 char buf
[MSVCRT_MB_LEN_MAX
];
3933 char_len
= MSVCRT_wctomb(buf
, mwc
);
3934 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
3938 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3947 /*********************************************************************
3948 * _fputwchar (MSVCRT.@)
3950 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3952 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3955 /*********************************************************************
3956 * _wfsopen (MSVCRT.@)
3958 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3961 int open_flags
, stream_flags
, fd
;
3963 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3965 /* map mode string to open() flags. "man fopen" for possibilities. */
3966 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3970 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3973 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3975 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3982 TRACE(":got (%p)\n",file
);
3983 if (fd
>= 0 && !file
)
3989 /*********************************************************************
3990 * _fsopen (MSVCRT.@)
3992 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3995 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3997 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3998 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3999 *MSVCRT__errno() = MSVCRT_EINVAL
;
4002 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4005 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4006 *MSVCRT__errno() = MSVCRT_EINVAL
;
4010 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4017 /*********************************************************************
4020 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4022 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4025 /*********************************************************************
4026 * fopen_s (MSVCRT.@)
4028 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4029 const char *filename
, const char *mode
)
4031 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4032 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4033 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4035 *pFile
= MSVCRT_fopen(filename
, mode
);
4038 return *MSVCRT__errno();
4042 /*********************************************************************
4043 * _wfopen (MSVCRT.@)
4045 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4047 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4050 /*********************************************************************
4051 * _wfopen_s (MSVCRT.@)
4053 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4054 const MSVCRT_wchar_t
*mode
)
4056 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4057 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4058 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4060 *pFile
= MSVCRT__wfopen(filename
, mode
);
4063 return *MSVCRT__errno();
4067 /*********************************************************************
4070 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4074 MSVCRT__lock_file(file
);
4075 ret
= MSVCRT__fputc_nolock(c
, file
);
4076 MSVCRT__unlock_file(file
);
4081 /*********************************************************************
4082 * _fputc_nolock (MSVCRT.@)
4084 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4093 res
= msvcrt_flush_buffer(file
);
4094 return res
? res
: c
;
4100 res
= MSVCRT__flsbuf(c
, file
);
4105 /*********************************************************************
4106 * _fputchar (MSVCRT.@)
4108 int CDECL
MSVCRT__fputchar(int c
)
4110 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4113 /*********************************************************************
4116 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4120 MSVCRT__lock_file(file
);
4121 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4122 MSVCRT__unlock_file(file
);
4127 /*********************************************************************
4128 * _fread_nolock (MSVCRT.@)
4130 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4132 MSVCRT_size_t rcnt
=size
* nmemb
;
4133 MSVCRT_size_t read
=0;
4134 MSVCRT_size_t pread
=0;
4139 /* first buffered data */
4141 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4142 memcpy(ptr
, file
->_ptr
, pcnt
);
4147 ptr
= (char*)ptr
+ pcnt
;
4148 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4149 if(file
->_flag
& MSVCRT__IORW
) {
4150 file
->_flag
|= MSVCRT__IOREAD
;
4156 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4157 msvcrt_alloc_buffer(file
);
4162 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4163 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4164 file
->_ptr
= file
->_base
;
4165 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4166 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4167 if (i
> 0 && i
< file
->_cnt
) {
4168 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4169 file
->_flag
&= ~MSVCRT__IOEOF
;
4172 memcpy(ptr
, file
->_ptr
, i
);
4176 } else if (rcnt
> INT_MAX
) {
4177 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4178 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4179 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4181 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4185 ptr
= (char *)ptr
+i
;
4186 /* expose feof condition in the flags
4187 * MFC tests file->_flag for feof, and doesn't call feof())
4189 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4190 file
->_flag
|= MSVCRT__IOEOF
;
4193 file
->_flag
|= MSVCRT__IOERR
;
4204 /*********************************************************************
4205 * fread_s (MSVCR80.@)
4207 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4208 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4212 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4214 memset(buf
, 0, buf_size
);
4217 if(!elem_size
|| !count
) return 0;
4219 MSVCRT__lock_file(stream
);
4220 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4221 MSVCRT__unlock_file(stream
);
4226 /*********************************************************************
4227 * _fread_nolock_s (MSVCR80.@)
4229 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4230 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4232 size_t bytes_left
, buf_pos
;
4234 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4236 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4238 memset(buf
, 0, buf_size
);
4241 if(!elem_size
|| !count
) return 0;
4242 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4243 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4245 bytes_left
= elem_size
*count
;
4248 if(stream
->_cnt
> 0) {
4249 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4251 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4252 memset(buf
, 0, buf_size
);
4256 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4260 int c
= MSVCRT__filbuf(stream
);
4265 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4266 memset(buf
, 0, buf_size
);
4270 ((char*)buf
)[buf_pos
++] = c
;
4275 return buf_pos
/elem_size
;
4278 /*********************************************************************
4279 * _wfreopen (MSVCRT.@)
4282 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4284 int open_flags
, stream_flags
, fd
;
4286 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4289 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4293 MSVCRT_fclose(file
);
4294 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4296 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4298 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4308 /*********************************************************************
4309 * _wfreopen_s (MSVCRT.@)
4311 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4312 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4314 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4315 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4316 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4317 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4319 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4322 return *MSVCRT__errno();
4326 /*********************************************************************
4327 * freopen (MSVCRT.@)
4330 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4333 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4335 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4336 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4342 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4349 /*********************************************************************
4350 * freopen_s (MSVCRT.@)
4352 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4353 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4355 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4356 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4357 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4358 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4360 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4363 return *MSVCRT__errno();
4367 /*********************************************************************
4368 * fsetpos (MSVCRT.@)
4370 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4374 MSVCRT__lock_file(file
);
4375 msvcrt_flush_buffer(file
);
4377 /* Reset direction of i/o */
4378 if(file
->_flag
& MSVCRT__IORW
) {
4379 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4382 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4383 MSVCRT__unlock_file(file
);
4387 /*********************************************************************
4388 * _ftelli64 (MSVCRT.@)
4390 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4394 MSVCRT__lock_file(file
);
4395 ret
= MSVCRT__ftelli64_nolock(file
);
4396 MSVCRT__unlock_file(file
);
4401 /*********************************************************************
4402 * _ftelli64_nolock (MSVCRT.@)
4404 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4408 pos
= _telli64(file
->_file
);
4411 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4412 if(file
->_flag
& MSVCRT__IOWRT
) {
4413 pos
+= file
->_ptr
- file
->_base
;
4415 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4418 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4422 } else if(!file
->_cnt
) { /* nothing to do */
4423 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4427 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4428 for(i
=0; i
<file
->_cnt
; i
++)
4429 if(file
->_ptr
[i
] == '\n')
4435 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4438 pos
-= file
->_bufsiz
;
4439 pos
+= file
->_ptr
- file
->_base
;
4441 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4442 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4445 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4455 /*********************************************************************
4458 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4460 return MSVCRT__ftelli64(file
);
4463 /*********************************************************************
4464 * _ftell_nolock (MSVCRT.@)
4466 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4468 return MSVCRT__ftelli64_nolock(file
);
4471 /*********************************************************************
4472 * fgetpos (MSVCRT.@)
4474 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4476 *pos
= MSVCRT__ftelli64(file
);
4482 /*********************************************************************
4485 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4487 MSVCRT_size_t len
= strlen(s
);
4490 MSVCRT__lock_file(file
);
4491 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4492 MSVCRT__unlock_file(file
);
4496 /*********************************************************************
4499 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4501 MSVCRT_size_t i
, len
= strlenW(s
);
4505 MSVCRT__lock_file(file
);
4506 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4507 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4508 MSVCRT__unlock_file(file
);
4512 tmp_buf
= add_std_buffer(file
);
4513 for (i
=0; i
<len
; i
++) {
4514 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4515 if(tmp_buf
) remove_std_buffer(file
);
4516 MSVCRT__unlock_file(file
);
4521 if(tmp_buf
) remove_std_buffer(file
);
4522 MSVCRT__unlock_file(file
);
4526 /*********************************************************************
4527 * getchar (MSVCRT.@)
4529 int CDECL
MSVCRT_getchar(void)
4531 return MSVCRT_fgetc(MSVCRT_stdin
);
4534 /*********************************************************************
4537 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4539 return MSVCRT_fgetc(file
);
4542 /*********************************************************************
4545 char * CDECL
MSVCRT_gets(char *buf
)
4548 char * buf_start
= buf
;
4550 MSVCRT__lock_file(MSVCRT_stdin
);
4551 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4552 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4553 if(cc
!= '\r') *buf
++ = (char)cc
;
4557 TRACE("got '%s'\n", buf_start
);
4558 MSVCRT__unlock_file(MSVCRT_stdin
);
4562 /*********************************************************************
4565 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4568 MSVCRT_wchar_t
* ws
= buf
;
4570 MSVCRT__lock_file(MSVCRT_stdin
);
4571 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4572 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4575 *buf
++ = (MSVCRT_wchar_t
)cc
;
4579 TRACE("got %s\n", debugstr_w(ws
));
4580 MSVCRT__unlock_file(MSVCRT_stdin
);
4584 /*********************************************************************
4587 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4589 return MSVCRT_fputc(c
, file
);
4592 /*********************************************************************
4593 * putchar (MSVCRT.@)
4595 int CDECL
MSVCRT_putchar(int c
)
4597 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4600 /*********************************************************************
4601 * _putwch (MSVCRT.@)
4603 int CDECL
MSVCRT__putwch(int c
)
4605 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4608 /*********************************************************************
4611 int CDECL
MSVCRT_puts(const char *s
)
4613 MSVCRT_size_t len
= strlen(s
);
4616 MSVCRT__lock_file(MSVCRT_stdout
);
4617 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4618 MSVCRT__unlock_file(MSVCRT_stdout
);
4622 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4623 MSVCRT__unlock_file(MSVCRT_stdout
);
4627 /*********************************************************************
4630 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4632 static const MSVCRT_wchar_t nl
= '\n';
4633 MSVCRT_size_t len
= strlenW(s
);
4636 MSVCRT__lock_file(MSVCRT_stdout
);
4637 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4638 MSVCRT__unlock_file(MSVCRT_stdout
);
4642 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4643 MSVCRT__unlock_file(MSVCRT_stdout
);
4647 /*********************************************************************
4650 int CDECL
MSVCRT_remove(const char *path
)
4652 TRACE("(%s)\n",path
);
4653 if (DeleteFileA(path
))
4655 TRACE(":failed (%d)\n",GetLastError());
4656 msvcrt_set_errno(GetLastError());
4660 /*********************************************************************
4661 * _wremove (MSVCRT.@)
4663 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4665 TRACE("(%s)\n",debugstr_w(path
));
4666 if (DeleteFileW(path
))
4668 TRACE(":failed (%d)\n",GetLastError());
4669 msvcrt_set_errno(GetLastError());
4673 /*********************************************************************
4676 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4678 TRACE(":from %s to %s\n",oldpath
,newpath
);
4679 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4681 TRACE(":failed (%d)\n",GetLastError());
4682 msvcrt_set_errno(GetLastError());
4686 /*********************************************************************
4687 * _wrename (MSVCRT.@)
4689 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4691 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4692 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4694 TRACE(":failed (%d)\n",GetLastError());
4695 msvcrt_set_errno(GetLastError());
4699 /*********************************************************************
4700 * setvbuf (MSVCRT.@)
4702 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4704 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4705 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4706 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4708 MSVCRT__lock_file(file
);
4710 MSVCRT__fflush_nolock(file
);
4711 if(file
->_flag
& MSVCRT__IOMYBUF
)
4712 MSVCRT_free(file
->_base
);
4713 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4716 if(mode
== MSVCRT__IONBF
) {
4717 file
->_flag
|= MSVCRT__IONBF
;
4718 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4721 file
->_base
= file
->_ptr
= buf
;
4722 file
->_flag
|= MSVCRT__USERBUF
;
4723 file
->_bufsiz
= size
;
4725 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4728 MSVCRT__unlock_file(file
);
4732 file
->_flag
|= MSVCRT__IOMYBUF
;
4733 file
->_bufsiz
= size
;
4735 MSVCRT__unlock_file(file
);
4739 /*********************************************************************
4742 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4744 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4747 /*********************************************************************
4750 char * CDECL
MSVCRT_tmpnam(char *s
)
4757 thread_data_t
*data
= msvcrt_get_thread_data();
4759 if(!data
->tmpnam_buffer
)
4760 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4762 s
= data
->tmpnam_buffer
;
4765 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4766 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4767 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4769 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4770 memcpy(p
, tmpstr
, size
);
4772 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4773 GetLastError() == ERROR_FILE_NOT_FOUND
)
4779 /*********************************************************************
4780 * _wtmpnam (MSVCRT.@)
4782 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4784 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4785 MSVCRT_wchar_t tmpstr
[16];
4789 thread_data_t
*data
= msvcrt_get_thread_data();
4791 if(!data
->wtmpnam_buffer
)
4792 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4794 s
= data
->wtmpnam_buffer
;
4797 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4798 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4799 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4801 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4802 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4804 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4805 GetLastError() == ERROR_FILE_NOT_FOUND
)
4811 /*********************************************************************
4812 * tmpfile (MSVCRT.@)
4814 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4816 char *filename
= MSVCRT_tmpnam(NULL
);
4818 MSVCRT_FILE
* file
= NULL
;
4821 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4822 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4823 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4825 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4830 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4833 if(fd
!= -1 && !file
)
4839 /*********************************************************************
4840 * tmpfile_s (MSVCRT.@)
4842 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4844 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4846 *file
= MSVCRT_tmpfile();
4850 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4852 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4855 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4859 MSVCRT__lock_file(file
);
4861 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4862 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4863 MSVCRT__unlock_file(file
);
4867 for(i
=0; i
<len
; i
++) {
4868 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
4869 MSVCRT__unlock_file(file
);
4874 MSVCRT__unlock_file(file
);
4878 /*********************************************************************
4879 * vfprintf (MSVCRT.@)
4881 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4886 MSVCRT__lock_file(file
);
4887 tmp_buf
= add_std_buffer(file
);
4888 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4889 if(tmp_buf
) remove_std_buffer(file
);
4890 MSVCRT__unlock_file(file
);
4895 /*********************************************************************
4896 * vfprintf_s (MSVCRT.@)
4898 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4903 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4905 MSVCRT__lock_file(file
);
4906 tmp_buf
= add_std_buffer(file
);
4907 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4908 if(tmp_buf
) remove_std_buffer(file
);
4909 MSVCRT__unlock_file(file
);
4914 /*********************************************************************
4915 * vfwprintf (MSVCRT.@)
4917 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4922 MSVCRT__lock_file(file
);
4923 tmp_buf
= add_std_buffer(file
);
4924 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4925 if(tmp_buf
) remove_std_buffer(file
);
4926 MSVCRT__unlock_file(file
);
4931 /*********************************************************************
4932 * vfwprintf_s (MSVCRT.@)
4934 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4939 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4941 MSVCRT__lock_file(file
);
4942 tmp_buf
= add_std_buffer(file
);
4943 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4944 if(tmp_buf
) remove_std_buffer(file
);
4945 MSVCRT__unlock_file(file
);
4950 /*********************************************************************
4951 * _vfwprintf_l (MSVCRT.@)
4953 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4954 MSVCRT__locale_t locale
, __ms_va_list valist
)
4959 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4961 MSVCRT__lock_file(file
);
4962 tmp_buf
= add_std_buffer(file
);
4963 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4964 if(tmp_buf
) remove_std_buffer(file
);
4965 MSVCRT__unlock_file(file
);
4970 /*********************************************************************
4971 * vprintf (MSVCRT.@)
4973 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4975 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4978 /*********************************************************************
4979 * vprintf_s (MSVCRT.@)
4981 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4983 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4986 /*********************************************************************
4987 * vwprintf (MSVCRT.@)
4989 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4991 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4994 /*********************************************************************
4995 * vwprintf_s (MSVCRT.@)
4997 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4999 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5002 /*********************************************************************
5003 * fprintf (MSVCRT.@)
5005 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5007 __ms_va_list valist
;
5009 __ms_va_start(valist
, format
);
5010 res
= MSVCRT_vfprintf(file
, format
, valist
);
5011 __ms_va_end(valist
);
5015 /*********************************************************************
5016 * fprintf_s (MSVCRT.@)
5018 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5020 __ms_va_list valist
;
5022 __ms_va_start(valist
, format
);
5023 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5024 __ms_va_end(valist
);
5028 /*********************************************************************
5029 * fwprintf (MSVCRT.@)
5031 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5033 __ms_va_list valist
;
5035 __ms_va_start(valist
, format
);
5036 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5037 __ms_va_end(valist
);
5041 /*********************************************************************
5042 * fwprintf_s (MSVCRT.@)
5044 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5046 __ms_va_list valist
;
5048 __ms_va_start(valist
, format
);
5049 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5050 __ms_va_end(valist
);
5054 /*********************************************************************
5055 * _fwprintf_l (MSVCRT.@)
5057 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5059 __ms_va_list valist
;
5061 __ms_va_start(valist
, locale
);
5062 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5063 __ms_va_end(valist
);
5067 /*********************************************************************
5070 int CDECL
MSVCRT_printf(const char *format
, ...)
5072 __ms_va_list valist
;
5074 __ms_va_start(valist
, format
);
5075 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5076 __ms_va_end(valist
);
5080 /*********************************************************************
5081 * printf_s (MSVCRT.@)
5083 int CDECL
MSVCRT_printf_s(const char *format
, ...)
5085 __ms_va_list valist
;
5087 __ms_va_start(valist
, format
);
5088 res
= MSVCRT_vprintf_s(format
, valist
);
5089 __ms_va_end(valist
);
5093 /*********************************************************************
5096 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5100 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5102 MSVCRT__lock_file(file
);
5103 ret
= MSVCRT__ungetc_nolock(c
, file
);
5104 MSVCRT__unlock_file(file
);
5109 /*********************************************************************
5110 * _ungetc_nolock (MSVCRT.@)
5112 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5114 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5116 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5117 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5120 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5121 && msvcrt_alloc_buffer(file
))
5122 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5125 if(file
->_ptr
>file
->_base
) {
5127 if(file
->_flag
& MSVCRT__IOSTRG
) {
5128 if(*file
->_ptr
!= c
) {
5136 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5137 file
->_flag
|= MSVCRT__IOREAD
;
5144 /*********************************************************************
5145 * ungetwc (MSVCRT.@)
5147 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5151 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5153 MSVCRT__lock_file(file
);
5154 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5155 MSVCRT__unlock_file(file
);
5160 /*********************************************************************
5161 * _ungetwc_nolock (MSVCRT.@)
5163 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5165 MSVCRT_wchar_t mwc
= wc
;
5167 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5168 if (wc
== MSVCRT_WEOF
)
5171 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5172 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5173 unsigned char * pp
= (unsigned char *)&mwc
;
5176 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5177 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5181 char mbs
[MSVCRT_MB_LEN_MAX
];
5184 len
= MSVCRT_wctomb(mbs
, mwc
);
5188 for(len
--; len
>=0; len
--) {
5189 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5197 /*********************************************************************
5198 * wprintf (MSVCRT.@)
5200 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5202 __ms_va_list valist
;
5204 __ms_va_start(valist
, format
);
5205 res
= MSVCRT_vwprintf(format
, valist
);
5206 __ms_va_end(valist
);
5210 /*********************************************************************
5211 * wprintf_s (MSVCRT.@)
5213 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5215 __ms_va_list valist
;
5217 __ms_va_start(valist
, format
);
5218 res
= MSVCRT_vwprintf_s(format
, valist
);
5219 __ms_va_end(valist
);
5223 /*********************************************************************
5224 * _getmaxstdio (MSVCRT.@)
5226 int CDECL
MSVCRT__getmaxstdio(void)
5228 return MSVCRT_max_streams
;
5231 /*********************************************************************
5232 * _setmaxstdio (MSVCRT.@)
5234 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5236 TRACE("%d\n", newmax
);
5238 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5241 MSVCRT_max_streams
= newmax
;
5242 return MSVCRT_max_streams
;