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
)
264 EnterCriticalSection(&ret
->crit
);
268 static inline void release_ioinfo(ioinfo
*info
)
270 if(info
->exflag
& EF_CRIT_INIT
)
271 LeaveCriticalSection(&info
->crit
);
274 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
278 if(i
>= MSVCRT_max_streams
)
282 return &MSVCRT__iob
[i
];
284 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
286 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
287 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
288 ERR("out of memory\n");
289 *MSVCRT__errno() = MSVCRT_ENOMEM
;
293 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
295 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
300 static inline BOOL
msvcrt_is_valid_fd(int fd
)
302 return fd
>= 0 && fd
< MSVCRT_fdend
&& (get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
);
305 /* INTERNAL: Get the HANDLE for a fd
306 * This doesn't lock the table, because a failure will result in
307 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
308 * it returns a valid handle which is about to be closed, a subsequent call
309 * will fail, most likely in a sane way.
311 static HANDLE
msvcrt_fdtoh(int fd
)
313 if (!msvcrt_is_valid_fd(fd
))
315 WARN(":fd (%d) - no handle!\n",fd
);
316 *MSVCRT___doserrno() = 0;
317 *MSVCRT__errno() = MSVCRT_EBADF
;
318 return INVALID_HANDLE_VALUE
;
320 if (get_ioinfo_nolock(fd
)->handle
== INVALID_HANDLE_VALUE
)
321 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd
);
322 return get_ioinfo_nolock(fd
)->handle
;
325 /* INTERNAL: free a file entry fd */
326 static void msvcrt_free_fd(int fd
)
331 fdinfo
= get_ioinfo_nolock(fd
);
332 if(fdinfo
!= &MSVCRT___badioinfo
)
334 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
337 TRACE(":fd (%d) freed\n",fd
);
344 SetStdHandle(STD_INPUT_HANDLE
, 0);
347 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
350 SetStdHandle(STD_ERROR_HANDLE
, 0);
355 if (fd
== MSVCRT_fdend
- 1)
357 if (fd
< MSVCRT_fdstart
)
362 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
363 /* caller must hold the files lock */
364 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
368 if (fd
>= MSVCRT_MAX_FILES
)
370 WARN(":files exhausted!\n");
371 *MSVCRT__errno() = MSVCRT_ENFILE
;
375 fdinfo
= get_ioinfo_nolock(fd
);
376 if(fdinfo
== &MSVCRT___badioinfo
) {
379 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
380 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
381 WARN(":out of memory!\n");
382 *MSVCRT__errno() = MSVCRT_ENOMEM
;
386 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
387 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
389 fdinfo
= get_ioinfo_nolock(fd
);
392 fdinfo
->handle
= hand
;
393 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
394 fdinfo
->lookahead
[0] = '\n';
395 fdinfo
->lookahead
[1] = '\n';
396 fdinfo
->lookahead
[2] = '\n';
397 if(!(fdinfo
->exflag
& EF_CRIT_INIT
))
398 InitializeCriticalSection(&fdinfo
->crit
);
399 fdinfo
->exflag
= EF_CRIT_INIT
;
401 /* locate next free slot */
402 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
403 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
405 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
406 get_ioinfo_nolock(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
408 /* update last fd in use */
409 if (fd
>= MSVCRT_fdend
)
410 MSVCRT_fdend
= fd
+ 1;
411 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
415 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
416 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
417 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
423 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
424 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
429 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
430 ret
= msvcrt_set_fd(hand
, flag
, MSVCRT_fdstart
);
435 /* INTERNAL: Allocate a FILE* for an fd slot */
436 /* caller must hold the files lock */
437 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
442 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
444 file
= msvcrt_get_file(i
);
448 if (file
->_flag
== 0)
450 if (i
== MSVCRT_stream_idx
)
452 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
454 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
455 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
466 /* INTERNAL: initialize a FILE* from an open fd */
467 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
469 TRACE(":fd (%d) allocating FILE*\n",fd
);
470 if (!msvcrt_is_valid_fd(fd
))
472 WARN(":invalid fd %d\n",fd
);
473 *MSVCRT___doserrno() = 0;
474 *MSVCRT__errno() = MSVCRT_EBADF
;
477 file
->_ptr
= file
->_base
= NULL
;
480 file
->_flag
= stream_flags
;
481 file
->_tmpfname
= NULL
;
483 TRACE(":got FILE* (%p)\n",file
);
487 /* INTERNAL: Create an inheritance data block (for spawned process)
488 * The inheritance block is made of:
489 * 00 int nb of file descriptor (NBFD)
490 * 04 char file flags (wxflag): repeated for each fd
491 * 4+NBFD HANDLE file handle: repeated for each fd
493 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
500 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
501 *block
= MSVCRT_calloc(*size
, 1);
507 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
508 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
510 *(unsigned*)*block
= MSVCRT_fdend
;
511 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
513 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
514 fdinfo
= get_ioinfo_nolock(fd
);
515 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
517 *wxflag_ptr
= fdinfo
->wxflag
;
518 *handle_ptr
= fdinfo
->handle
;
523 *handle_ptr
= INVALID_HANDLE_VALUE
;
525 wxflag_ptr
++; handle_ptr
++;
530 /* INTERNAL: Set up all file descriptors,
531 * as well as default streams (stdin, stderr and stdout)
533 void msvcrt_init_io(void)
539 GetStartupInfoA(&si
);
540 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
546 count
= *(unsigned*)si
.lpReserved2
;
547 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
548 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
550 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
551 count
= min(count
, MSVCRT_MAX_FILES
);
552 for (i
= 0; i
< count
; i
++)
554 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
555 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
557 wxflag_ptr
++; handle_ptr
++;
559 MSVCRT_fdend
= max( 3, count
);
560 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
561 if (get_ioinfo_nolock(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
564 fdinfo
= get_ioinfo_nolock(MSVCRT_STDIN_FILENO
);
565 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
566 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
567 DWORD type
= GetFileType(h
);
569 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
570 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDIN_FILENO
);
573 fdinfo
= get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
);
574 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
575 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
576 DWORD type
= GetFileType(h
);
578 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
579 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDOUT_FILENO
);
582 fdinfo
= get_ioinfo_nolock(MSVCRT_STDERR_FILENO
);
583 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
584 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
585 DWORD type
= GetFileType(h
);
587 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
588 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDERR_FILENO
);
591 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
592 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
593 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
595 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
596 for (i
= 0; i
< 3; i
++)
598 /* FILE structs for stdin/out/err are static and never deleted */
599 MSVCRT__iob
[i
]._file
= i
;
600 MSVCRT__iob
[i
]._tmpfname
= NULL
;
601 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
603 MSVCRT_stream_idx
= 3;
606 /* INTERNAL: Flush stdio file buffer */
607 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
609 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
610 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
611 int cnt
=file
->_ptr
-file
->_base
;
612 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
613 file
->_flag
|= MSVCRT__IOERR
;
617 if(file
->_flag
& MSVCRT__IORW
)
618 file
->_flag
&= ~MSVCRT__IOWRT
;
621 file
->_ptr
=file
->_base
;
626 /*********************************************************************
629 int CDECL
MSVCRT__isatty(int fd
)
631 TRACE(":fd (%d)\n",fd
);
633 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
636 /* INTERNAL: Allocate stdio file buffer */
637 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
639 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
640 && MSVCRT__isatty(file
->_file
))
643 file
->_base
= MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
645 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
646 file
->_flag
|= MSVCRT__IOMYBUF
;
648 file
->_base
= (char*)(&file
->_charbuf
);
650 file
->_flag
|= MSVCRT__IONBF
;
652 file
->_ptr
= file
->_base
;
657 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
658 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
660 static char buffers
[2][MSVCRT_BUFSIZ
];
662 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
663 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
664 || !MSVCRT__isatty(file
->_file
))
667 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
668 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
669 file
->_flag
|= MSVCRT__USERBUF
;
673 /* INTERNAL: Removes temporary buffer from stdout or stderr */
674 /* Only call this function when add_std_buffer returned TRUE */
675 static void remove_std_buffer(MSVCRT_FILE
*file
)
677 msvcrt_flush_buffer(file
);
678 file
->_ptr
= file
->_base
= NULL
;
679 file
->_bufsiz
= file
->_cnt
= 0;
680 file
->_flag
&= ~MSVCRT__USERBUF
;
683 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
684 static int msvcrt_int_to_base32(int num
, char *str
)
699 *p
= (num
& 31) + '0';
701 *p
+= ('a' - '0' - 10);
708 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
709 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
724 *p
= (num
& 31) + '0';
726 *p
+= ('a' - '0' - 10);
733 /*********************************************************************
734 * __iob_func(MSVCRT.@)
736 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
738 return &MSVCRT__iob
[0];
741 /*********************************************************************
744 int CDECL
MSVCRT__access(const char *filename
, int mode
)
746 DWORD attr
= GetFileAttributesA(filename
);
748 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
750 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
752 msvcrt_set_errno(GetLastError());
755 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
757 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
763 /*********************************************************************
764 * _access_s (MSVCRT.@)
766 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
768 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
769 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
771 if (MSVCRT__access(filename
, mode
) == -1)
772 return *MSVCRT__errno();
776 /*********************************************************************
777 * _waccess (MSVCRT.@)
779 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
781 DWORD attr
= GetFileAttributesW(filename
);
783 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
785 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
787 msvcrt_set_errno(GetLastError());
790 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
792 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
798 /*********************************************************************
799 * _waccess_s (MSVCRT.@)
801 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
803 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
804 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
806 if (MSVCRT__waccess(filename
, mode
) == -1)
807 return *MSVCRT__errno();
811 /*********************************************************************
814 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
816 DWORD oldFlags
= GetFileAttributesA(path
);
818 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
820 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
821 oldFlags
| FILE_ATTRIBUTE_READONLY
;
823 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
826 msvcrt_set_errno(GetLastError());
830 /*********************************************************************
833 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
835 DWORD oldFlags
= GetFileAttributesW(path
);
837 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
839 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
840 oldFlags
| FILE_ATTRIBUTE_READONLY
;
842 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
845 msvcrt_set_errno(GetLastError());
849 /*********************************************************************
852 int CDECL
MSVCRT__unlink(const char *path
)
854 TRACE("%s\n",debugstr_a(path
));
855 if(DeleteFileA(path
))
857 TRACE("failed (%d)\n",GetLastError());
858 msvcrt_set_errno(GetLastError());
862 /*********************************************************************
863 * _wunlink (MSVCRT.@)
865 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
867 TRACE("(%s)\n",debugstr_w(path
));
868 if(DeleteFileW(path
))
870 TRACE("failed (%d)\n",GetLastError());
871 msvcrt_set_errno(GetLastError());
875 /*********************************************************************
878 int CDECL
MSVCRT__commit(int fd
)
880 ioinfo
*info
= get_ioinfo(fd
);
883 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
885 if (info
->handle
== INVALID_HANDLE_VALUE
)
887 else if (!FlushFileBuffers(info
->handle
))
889 if (GetLastError() == ERROR_INVALID_HANDLE
)
891 /* FlushFileBuffers fails for console handles
892 * so we ignore this error.
898 TRACE(":failed-last error (%d)\n",GetLastError());
899 msvcrt_set_errno(GetLastError());
909 release_ioinfo(info
);
913 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
914 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
916 /* INTERNAL: Flush all stream buffer */
917 static int msvcrt_flush_all_buffers(int mask
)
919 int i
, num_flushed
= 0;
923 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
924 file
= msvcrt_get_file(i
);
928 if(file
->_flag
& mask
) {
936 TRACE(":flushed (%d) handles\n",num_flushed
);
940 /*********************************************************************
941 * _flushall (MSVCRT.@)
943 int CDECL
MSVCRT__flushall(void)
945 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
948 /*********************************************************************
951 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
956 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
959 MSVCRT__lock_file(file
);
960 ret
= MSVCRT__fflush_nolock(file
);
961 MSVCRT__unlock_file(file
);
967 /*********************************************************************
968 * _fflush_nolock (MSVCRT.@)
970 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
975 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
979 res
= msvcrt_flush_buffer(file
);
980 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
981 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
985 /*********************************************************************
988 int CDECL
MSVCRT__close(int fd
)
990 ioinfo
*info
= get_ioinfo(fd
);
994 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
995 if (!(info
->wxflag
& WX_OPEN
)) {
998 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1001 WARN(":failed-last error (%d)\n",GetLastError());
1002 msvcrt_set_errno(GetLastError());
1006 release_ioinfo(info
);
1010 /*********************************************************************
1013 * MSDN isn't clear on this point, but the remarks for _pipe
1014 * indicate file descriptors duplicated with _dup and _dup2 are always
1017 int CDECL
MSVCRT__dup2(int od
, int nd
)
1021 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1023 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
1027 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od
)->handle
,
1028 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1030 int wxflag
= get_ioinfo_nolock(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
1032 if (msvcrt_is_valid_fd(nd
))
1034 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
1037 CloseHandle(handle
);
1038 *MSVCRT__errno() = MSVCRT_EMFILE
;
1042 /* _dup2 returns 0, not nd, on success */
1049 msvcrt_set_errno(GetLastError());
1054 *MSVCRT__errno() = MSVCRT_EBADF
;
1061 /*********************************************************************
1064 int CDECL
MSVCRT__dup(int od
)
1069 fd
= MSVCRT_fdstart
;
1070 if (MSVCRT__dup2(od
, fd
) == 0)
1078 /*********************************************************************
1081 int CDECL
MSVCRT__eof(int fd
)
1083 ioinfo
*info
= get_ioinfo(fd
);
1084 DWORD curpos
,endpos
;
1085 LONG hcurpos
,hendpos
;
1087 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1089 if (info
->handle
== INVALID_HANDLE_VALUE
)
1091 release_ioinfo(info
);
1095 if (info
->wxflag
& WX_ATEOF
)
1097 release_ioinfo(info
);
1101 /* Otherwise we do it the hard way */
1102 hcurpos
= hendpos
= 0;
1103 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1104 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1106 if (curpos
== endpos
&& hcurpos
== hendpos
)
1108 /* FIXME: shouldn't WX_ATEOF be set here? */
1109 release_ioinfo(info
);
1113 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1114 release_ioinfo(info
);
1118 /*********************************************************************
1119 * _fcloseall (MSVCRT.@)
1121 int CDECL
MSVCRT__fcloseall(void)
1123 int num_closed
= 0, i
;
1127 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1128 file
= msvcrt_get_file(i
);
1130 if (file
->_flag
&& !MSVCRT_fclose(file
))
1135 TRACE(":closed (%d) handles\n",num_closed
);
1139 /* free everything on process exit */
1140 void msvcrt_free_io(void)
1146 MSVCRT__fcloseall();
1148 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1150 if(!MSVCRT___pioinfo
[i
])
1153 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1155 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1156 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1158 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1161 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1163 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1164 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1166 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1167 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1171 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1172 MSVCRT_free(MSVCRT_fstream
[i
]);
1174 DeleteCriticalSection(&MSVCRT_file_cs
);
1177 /*********************************************************************
1178 * _lseeki64 (MSVCRT.@)
1180 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1182 ioinfo
*info
= get_ioinfo(fd
);
1185 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1187 if (info
->handle
== INVALID_HANDLE_VALUE
)
1189 release_ioinfo(info
);
1193 if (whence
< 0 || whence
> 2)
1195 release_ioinfo(info
);
1196 *MSVCRT__errno() = MSVCRT_EINVAL
;
1200 TRACE(":fd (%d) to %s pos %s\n",
1201 fd
,wine_dbgstr_longlong(offset
),
1202 (whence
==SEEK_SET
)?"SEEK_SET":
1203 (whence
==SEEK_CUR
)?"SEEK_CUR":
1204 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1206 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1207 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1208 ofs
.QuadPart
= offset
;
1209 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1210 GetLastError() == ERROR_SUCCESS
)
1212 info
->wxflag
&= ~WX_ATEOF
;
1213 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1215 release_ioinfo(info
);
1216 return ofs
.QuadPart
;
1218 release_ioinfo(info
);
1219 TRACE(":error-last error (%d)\n",GetLastError());
1220 msvcrt_set_errno(GetLastError());
1224 /*********************************************************************
1227 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1229 return MSVCRT__lseeki64(fd
, offset
, whence
);
1232 /*********************************************************************
1233 * _lock_file (MSVCRT.@)
1235 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1237 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1238 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1240 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1243 /*********************************************************************
1244 * _unlock_file (MSVCRT.@)
1246 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1248 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1249 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1251 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1254 /*********************************************************************
1255 * _locking (MSVCRT.@)
1257 * This is untested; the underlying LockFile doesn't work yet.
1259 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1261 ioinfo
*info
= get_ioinfo(fd
);
1265 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1266 if (info
->handle
== INVALID_HANDLE_VALUE
)
1268 release_ioinfo(info
);
1272 if (mode
< 0 || mode
> 4)
1274 release_ioinfo(info
);
1275 *MSVCRT__errno() = MSVCRT_EINVAL
;
1279 TRACE(":fd (%d) by 0x%08x mode %s\n",
1280 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1281 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1282 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1283 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1284 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1287 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1289 release_ioinfo(info
);
1290 FIXME ("Seek failed\n");
1291 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1294 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1297 ret
= 1; /* just to satisfy gcc */
1300 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1305 else if (mode
== MSVCRT__LK_UNLCK
)
1306 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1308 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1309 /* FIXME - what about error settings? */
1310 release_ioinfo(info
);
1311 return ret
? 0 : -1;
1314 /*********************************************************************
1315 * _fseeki64 (MSVCRT.@)
1317 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1321 MSVCRT__lock_file(file
);
1322 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1323 MSVCRT__unlock_file(file
);
1328 /*********************************************************************
1329 * _fseeki64_nolock (MSVCRT.@)
1331 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1335 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1337 offset
+= MSVCRT__ftelli64_nolock(file
);
1340 /* Flush output if needed */
1341 msvcrt_flush_buffer(file
);
1342 /* Reset direction of i/o */
1343 if(file
->_flag
& MSVCRT__IORW
) {
1344 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1346 /* Clear end of file flag */
1347 file
->_flag
&= ~MSVCRT__IOEOF
;
1348 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1353 /*********************************************************************
1356 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1358 return MSVCRT__fseeki64( file
, offset
, whence
);
1361 /*********************************************************************
1362 * _fseek_nolock (MSVCRT.@)
1364 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1366 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1369 /*********************************************************************
1370 * _chsize_s (MSVCRT.@)
1372 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1378 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1380 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1383 info
= get_ioinfo(fd
);
1384 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1386 /* save the current file pointer */
1387 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1390 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1393 ret
= SetEndOfFile(info
->handle
);
1394 if (!ret
) msvcrt_set_errno(GetLastError());
1397 /* restore the file pointer */
1398 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1402 release_ioinfo(info
);
1403 return ret
? 0 : *MSVCRT__errno();
1406 /*********************************************************************
1407 * _chsize (MSVCRT.@)
1409 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1411 /* _chsize_s returns errno on failure but _chsize should return -1 */
1412 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1415 /*********************************************************************
1416 * clearerr (MSVCRT.@)
1418 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1420 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1422 MSVCRT__lock_file(file
);
1423 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1424 MSVCRT__unlock_file(file
);
1427 /*********************************************************************
1430 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1432 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1434 MSVCRT__lock_file(file
);
1435 MSVCRT__fseek_nolock(file
, 0L, SEEK_SET
);
1436 MSVCRT_clearerr(file
);
1437 MSVCRT__unlock_file(file
);
1440 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1442 int plus
= strchrW(mode
, '+') != NULL
;
1444 TRACE("%s\n", debugstr_w(mode
));
1446 while(*mode
== ' ') mode
++;
1451 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1452 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1455 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1456 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1459 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1460 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1463 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1467 *stream_flags
|= MSVCRT__commode
;
1469 while (*mode
&& *mode
!=',')
1473 *open_flags
|= MSVCRT__O_BINARY
;
1474 *open_flags
&= ~MSVCRT__O_TEXT
;
1477 *open_flags
|= MSVCRT__O_TEXT
;
1478 *open_flags
&= ~MSVCRT__O_BINARY
;
1481 *open_flags
|= MSVCRT__O_TEMPORARY
;
1484 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1487 *stream_flags
|= MSVCRT__IOCOMMIT
;
1490 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1493 *open_flags
|= MSVCRT__O_NOINHERIT
;
1502 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1505 ERR("incorrect mode flag: %c\n", mode
[-1]);
1511 static const WCHAR ccs
[] = {'c','c','s'};
1512 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1513 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1514 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1517 while(*mode
== ' ') mode
++;
1518 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1520 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1521 while(*mode
== ' ') mode
++;
1522 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1525 while(*mode
== ' ') mode
++;
1527 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1529 *open_flags
|= MSVCRT__O_U8TEXT
;
1530 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1532 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1534 *open_flags
|= MSVCRT__O_U16TEXT
;
1535 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1537 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1539 *open_flags
|= MSVCRT__O_WTEXT
;
1540 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1544 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1548 while(*mode
== ' ') mode
++;
1551 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1556 /*********************************************************************
1557 * _fdopen (MSVCRT.@)
1559 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1562 MSVCRT_wchar_t
*modeW
= NULL
;
1564 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1566 ret
= MSVCRT__wfdopen(fd
, modeW
);
1572 /*********************************************************************
1573 * _wfdopen (MSVCRT.@)
1575 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1577 int open_flags
, stream_flags
;
1580 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1583 if (!(file
= msvcrt_alloc_fp()))
1585 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1590 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1596 /*********************************************************************
1597 * _filelength (MSVCRT.@)
1599 LONG CDECL
MSVCRT__filelength(int fd
)
1601 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1604 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1607 if (endPos
!= curPos
)
1608 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1615 /*********************************************************************
1616 * _filelengthi64 (MSVCRT.@)
1618 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1620 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1623 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1626 if (endPos
!= curPos
)
1627 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1634 /*********************************************************************
1635 * _fileno (MSVCRT.@)
1637 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1639 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1643 /*********************************************************************
1644 * _fstat64 (MSVCRT.@)
1646 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1648 ioinfo
*info
= get_ioinfo(fd
);
1651 BY_HANDLE_FILE_INFORMATION hfi
;
1653 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1654 if (info
->handle
== INVALID_HANDLE_VALUE
)
1656 release_ioinfo(info
);
1662 WARN(":failed-NULL buf\n");
1663 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1664 release_ioinfo(info
);
1668 memset(&hfi
, 0, sizeof(hfi
));
1669 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1670 type
= GetFileType(info
->handle
);
1671 if (type
== FILE_TYPE_PIPE
)
1673 buf
->st_dev
= buf
->st_rdev
= fd
;
1674 buf
->st_mode
= S_IFIFO
;
1677 else if (type
== FILE_TYPE_CHAR
)
1679 buf
->st_dev
= buf
->st_rdev
= fd
;
1680 buf
->st_mode
= S_IFCHR
;
1683 else /* FILE_TYPE_DISK etc. */
1685 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1687 WARN(":failed-last error (%d)\n",GetLastError());
1688 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1689 release_ioinfo(info
);
1692 buf
->st_mode
= S_IFREG
| 0444;
1693 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1694 buf
->st_mode
|= 0222;
1695 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1696 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1698 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1699 buf
->st_mtime
= buf
->st_ctime
= dw
;
1700 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1702 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1704 release_ioinfo(info
);
1708 /*********************************************************************
1709 * _fstati64 (MSVCRT.@)
1711 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1714 struct MSVCRT__stat64 buf64
;
1716 ret
= MSVCRT__fstat64(fd
, &buf64
);
1718 msvcrt_stat64_to_stati64(&buf64
, buf
);
1722 /*********************************************************************
1725 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1727 struct MSVCRT__stat64 buf64
;
1729 ret
= MSVCRT__fstat64(fd
, &buf64
);
1731 msvcrt_stat64_to_stat(&buf64
, buf
);
1735 /*********************************************************************
1736 * _fstat32 (MSVCR80.@)
1738 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1741 struct MSVCRT__stat64 buf64
;
1743 ret
= MSVCRT__fstat64(fd
, &buf64
);
1745 msvcrt_stat64_to_stat32(&buf64
, buf
);
1749 /*********************************************************************
1750 * _fstat64i32 (MSVCR80.@)
1752 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1755 struct MSVCRT__stat64 buf64
;
1757 ret
= MSVCRT__fstat64(fd
, &buf64
);
1759 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1763 /*********************************************************************
1764 * _futime64 (MSVCRT.@)
1766 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1768 HANDLE hand
= msvcrt_fdtoh(fd
);
1773 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1778 time_to_filetime( t
->actime
, &at
);
1779 time_to_filetime( t
->modtime
, &wt
);
1782 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1784 msvcrt_set_errno(GetLastError());
1790 /*********************************************************************
1791 * _futime32 (MSVCRT.@)
1793 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1797 struct MSVCRT___utimbuf64 t64
;
1798 t64
.actime
= t
->actime
;
1799 t64
.modtime
= t
->modtime
;
1800 return _futime64( fd
, &t64
);
1803 return _futime64( fd
, NULL
);
1806 /*********************************************************************
1807 * _futime (MSVCRT.@)
1810 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1812 return _futime64( fd
, t
);
1815 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1817 return _futime32( fd
, t
);
1821 /*********************************************************************
1822 * _get_osfhandle (MSVCRT.@)
1824 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1826 HANDLE hand
= msvcrt_fdtoh(fd
);
1827 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1829 return (MSVCRT_intptr_t
)hand
;
1832 /*********************************************************************
1833 * _mktemp_s (MSVCRT.@)
1835 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1839 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1840 return MSVCRT_EINVAL
;
1842 for(len
=0; len
<size
; len
++)
1845 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1848 return MSVCRT_EINVAL
;
1851 for(xno
=1; xno
<=6; xno
++)
1852 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1853 return MSVCRT_EINVAL
;
1855 id
= GetCurrentProcessId();
1856 for(xno
=1; xno
<6; xno
++) {
1857 pattern
[len
-xno
] = id
%10 + '0';
1861 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1862 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1867 *MSVCRT__errno() = MSVCRT_EEXIST
;
1868 return MSVCRT_EEXIST
;
1871 /*********************************************************************
1872 * _mktemp (MSVCRT.@)
1874 char * CDECL
MSVCRT__mktemp(char *pattern
)
1877 char *retVal
= pattern
;
1885 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1889 id
= GetCurrentProcessId();
1893 int tempNum
= id
/ 10;
1894 *pattern
-- = id
- (tempNum
* 10) + '0';
1900 *pattern
= letter
++;
1901 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1903 } while(letter
<= 'z');
1907 /*********************************************************************
1908 * _wmktemp_s (MSVCRT.@)
1910 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1914 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1915 return MSVCRT_EINVAL
;
1917 for(len
=0; len
<size
; len
++)
1920 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1923 return MSVCRT_EINVAL
;
1926 for(xno
=1; xno
<=6; xno
++)
1927 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1928 return MSVCRT_EINVAL
;
1930 id
= GetCurrentProcessId();
1931 for(xno
=1; xno
<6; xno
++) {
1932 pattern
[len
-xno
] = id
%10 + '0';
1936 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1937 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1942 *MSVCRT__errno() = MSVCRT_EEXIST
;
1943 return MSVCRT_EEXIST
;
1946 /*********************************************************************
1947 * _wmktemp (MSVCRT.@)
1949 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1952 MSVCRT_wchar_t
*retVal
= pattern
;
1954 MSVCRT_wchar_t letter
= 'a';
1960 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1964 id
= GetCurrentProcessId();
1968 int tempNum
= id
/ 10;
1969 *pattern
-- = id
- (tempNum
* 10) + '0';
1975 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1977 *pattern
= letter
++;
1978 } while(letter
!= '|');
1982 static unsigned split_oflags(unsigned oflags
)
1985 unsigned unsupp
; /* until we support everything */
1987 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1988 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1989 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1990 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1991 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1992 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1993 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1994 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1995 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1997 if ((unsupp
= oflags
& ~(
1998 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1999 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2000 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2001 MSVCRT__O_NOINHERIT
|
2002 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2003 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2005 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2010 /*********************************************************************
2013 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2016 SECURITY_ATTRIBUTES sa
;
2017 HANDLE readHandle
, writeHandle
;
2021 *MSVCRT__errno() = MSVCRT_EINVAL
;
2025 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2026 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2027 sa
.lpSecurityDescriptor
= NULL
;
2028 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2030 unsigned int wxflags
= split_oflags(textmode
);
2034 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2038 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2046 MSVCRT__close(pfds
[0]);
2047 CloseHandle(writeHandle
);
2048 *MSVCRT__errno() = MSVCRT_EMFILE
;
2053 CloseHandle(readHandle
);
2054 CloseHandle(writeHandle
);
2055 *MSVCRT__errno() = MSVCRT_EMFILE
;
2060 msvcrt_set_errno(GetLastError());
2065 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2067 char bom
[sizeof(utf8_bom
)];
2070 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2072 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2075 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2076 oflags
|= MSVCRT__O_U8TEXT
;
2077 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2079 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2080 oflags
|= MSVCRT__O_U16TEXT
;
2082 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2088 /*********************************************************************
2089 * _wsopen_s (MSVCRT.@)
2091 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2093 DWORD access
= 0, creation
= 0, attrib
;
2094 SECURITY_ATTRIBUTES sa
;
2095 DWORD sharing
, type
;
2099 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2100 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2102 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2105 wxflag
= split_oflags(oflags
);
2106 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2108 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2109 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2110 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2113 if (oflags
& MSVCRT__O_CREAT
)
2115 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2116 FIXME(": pmode 0x%04x ignored\n", pmode
);
2118 WARN(": pmode 0x%04x ignored\n", pmode
);
2120 if (oflags
& MSVCRT__O_EXCL
)
2121 creation
= CREATE_NEW
;
2122 else if (oflags
& MSVCRT__O_TRUNC
)
2123 creation
= CREATE_ALWAYS
;
2125 creation
= OPEN_ALWAYS
;
2127 else /* no MSVCRT__O_CREAT */
2129 if (oflags
& MSVCRT__O_TRUNC
)
2130 creation
= TRUNCATE_EXISTING
;
2132 creation
= OPEN_EXISTING
;
2137 case MSVCRT__SH_DENYRW
:
2140 case MSVCRT__SH_DENYWR
:
2141 sharing
= FILE_SHARE_READ
;
2143 case MSVCRT__SH_DENYRD
:
2144 sharing
= FILE_SHARE_WRITE
;
2146 case MSVCRT__SH_DENYNO
:
2147 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2150 ERR( "Unhandled shflags 0x%x\n", shflags
);
2151 return MSVCRT_EINVAL
;
2153 attrib
= FILE_ATTRIBUTE_NORMAL
;
2155 if (oflags
& MSVCRT__O_TEMPORARY
)
2157 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2159 sharing
|= FILE_SHARE_DELETE
;
2162 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2163 sa
.lpSecurityDescriptor
= NULL
;
2164 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2166 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2167 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2168 && !(access
&GENERIC_READ
))
2170 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2171 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2172 if (hand
!= INVALID_HANDLE_VALUE
)
2174 oflags
= check_bom(hand
, oflags
, FALSE
);
2178 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2181 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2182 if (hand
== INVALID_HANDLE_VALUE
) {
2183 WARN(":failed-last error (%d)\n",GetLastError());
2184 msvcrt_set_errno(GetLastError());
2185 return *MSVCRT__errno();
2188 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2190 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2191 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2192 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2194 if (oflags
& MSVCRT__O_U8TEXT
)
2196 DWORD written
= 0, tmp
;
2198 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2199 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2201 if (written
!= sizeof(utf8_bom
)) {
2202 WARN("error writing BOM\n");
2204 msvcrt_set_errno(GetLastError());
2205 return *MSVCRT__errno();
2210 DWORD written
= 0, tmp
;
2212 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2213 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2215 if (written
!= sizeof(utf16_bom
))
2217 WARN("error writing BOM\n");
2219 msvcrt_set_errno(GetLastError());
2220 return *MSVCRT__errno();
2224 else if (access
& GENERIC_READ
)
2225 oflags
= check_bom(hand
, oflags
, TRUE
);
2228 type
= GetFileType(hand
);
2229 if (type
== FILE_TYPE_CHAR
)
2231 else if (type
== FILE_TYPE_PIPE
)
2234 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2236 return *MSVCRT__errno();
2238 if (oflags
& MSVCRT__O_WTEXT
)
2239 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2240 else if (oflags
& MSVCRT__O_U16TEXT
)
2241 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2242 else if (oflags
& MSVCRT__O_U8TEXT
)
2243 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2245 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2249 /*********************************************************************
2250 * _wsopen (MSVCRT.@)
2252 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2257 if (oflags
& MSVCRT__O_CREAT
)
2261 __ms_va_start(ap
, shflags
);
2262 pmode
= va_arg(ap
, int);
2268 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2272 /*********************************************************************
2273 * _sopen_s (MSVCRT.@)
2275 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2277 MSVCRT_wchar_t
*pathW
;
2280 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2281 return MSVCRT_EINVAL
;
2283 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2284 return MSVCRT_EINVAL
;
2286 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2291 /*********************************************************************
2294 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2299 if (oflags
& MSVCRT__O_CREAT
)
2303 __ms_va_start(ap
, shflags
);
2304 pmode
= va_arg(ap
, int);
2310 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2314 /*********************************************************************
2317 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2321 if (flags
& MSVCRT__O_CREAT
)
2324 __ms_va_start(ap
, flags
);
2325 pmode
= va_arg(ap
, int);
2327 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2330 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2333 /*********************************************************************
2336 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2340 if (flags
& MSVCRT__O_CREAT
)
2343 __ms_va_start(ap
, flags
);
2344 pmode
= va_arg(ap
, int);
2346 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2349 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2352 /*********************************************************************
2355 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2357 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2358 return MSVCRT__open(path
, usedFlags
);
2361 /*********************************************************************
2362 * _wcreat (MSVCRT.@)
2364 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2366 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2367 return MSVCRT__wopen(path
, usedFlags
);
2370 /*********************************************************************
2371 * _open_osfhandle (MSVCRT.@)
2373 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2378 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2379 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2380 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2381 * text - it never sets MSVCRT__O_BINARY.
2383 /* don't let split_oflags() decide the mode if no mode is passed */
2384 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2385 oflags
|= MSVCRT__O_BINARY
;
2387 flags
= GetFileType((HANDLE
)handle
);
2388 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2390 msvcrt_set_errno(GetLastError());
2394 if (flags
== FILE_TYPE_CHAR
)
2396 else if (flags
== FILE_TYPE_PIPE
)
2400 flags
|= split_oflags(oflags
);
2402 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2403 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2407 /*********************************************************************
2410 int CDECL
MSVCRT__rmtmp(void)
2412 int num_removed
= 0, i
;
2416 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2417 file
= msvcrt_get_file(i
);
2419 if (file
->_tmpfname
)
2421 MSVCRT_fclose(file
);
2428 TRACE(":removed (%d) temp files\n",num_removed
);
2432 static inline int get_utf8_char_len(char ch
)
2434 if((ch
&0xf8) == 0xf0)
2436 else if((ch
&0xf0) == 0xe0)
2438 else if((ch
&0xe0) == 0xc0)
2443 /*********************************************************************
2444 * (internal) read_utf8
2446 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2448 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2449 HANDLE hand
= fdinfo
->handle
;
2450 char min_buf
[4], *readbuf
, lookahead
;
2451 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2453 /* make the buffer big enough to hold at least one character */
2454 /* read bytes have to fit to output and lookahead buffers */
2456 readbuf_size
= count
< 4 ? 4 : count
;
2457 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2462 if(fdinfo
->lookahead
[0] != '\n') {
2463 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2464 fdinfo
->lookahead
[0] = '\n';
2466 if(fdinfo
->lookahead
[1] != '\n') {
2467 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2468 fdinfo
->lookahead
[1] = '\n';
2470 if(fdinfo
->lookahead
[2] != '\n') {
2471 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2472 fdinfo
->lookahead
[2] = '\n';
2477 /* NOTE: this case is broken in native dll, reading
2478 * sometimes fails when small buffer is passed
2481 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2482 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2483 fdinfo
->wxflag
|= WX_ATEOF
;
2486 msvcrt_set_errno(GetLastError());
2489 }else if(!num_read
) {
2490 fdinfo
->wxflag
|= WX_ATEOF
;
2496 char_len
= get_utf8_char_len(readbuf
[0]);
2498 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2502 if(readbuf
[0] == '\n')
2503 fdinfo
->wxflag
|= WX_READNL
;
2505 fdinfo
->wxflag
&= ~WX_READNL
;
2507 if(readbuf
[0] == 0x1a) {
2508 fdinfo
->wxflag
|= WX_ATEOF
;
2512 if(readbuf
[0] == '\r') {
2513 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2515 else if(lookahead
== '\n')
2519 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2520 fdinfo
->lookahead
[0] = lookahead
;
2522 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2527 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2528 msvcrt_set_errno(GetLastError());
2535 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2538 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2539 fdinfo
->wxflag
|= WX_ATEOF
;
2540 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2543 msvcrt_set_errno(GetLastError());
2544 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2547 }else if(!pos
&& !num_read
) {
2548 fdinfo
->wxflag
|= WX_ATEOF
;
2549 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2554 if(readbuf
[0] == '\n')
2555 fdinfo
->wxflag
|= WX_READNL
;
2557 fdinfo
->wxflag
&= ~WX_READNL
;
2559 /* Find first byte of last character (may be incomplete) */
2560 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2561 if((readbuf
[i
]&0xc0) != 0x80)
2563 char_len
= get_utf8_char_len(readbuf
[i
]);
2564 if(char_len
+i
<= pos
)
2567 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2569 fdinfo
->lookahead
[0] = readbuf
[i
];
2571 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2573 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2575 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2579 for(i
=0, j
=0; i
<pos
; i
++) {
2580 if(readbuf
[i
] == 0x1a) {
2581 fdinfo
->wxflag
|= WX_ATEOF
;
2585 /* strip '\r' if followed by '\n' */
2586 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2587 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2588 readbuf
[j
++] = '\r';
2589 }else if(lookahead
== '\n' && j
==0) {
2590 readbuf
[j
++] = '\n';
2592 if(lookahead
!= '\n')
2593 readbuf
[j
++] = '\r';
2595 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2596 fdinfo
->lookahead
[0] = lookahead
;
2598 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2600 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2601 readbuf
[j
++] = readbuf
[i
];
2606 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2607 msvcrt_set_errno(GetLastError());
2608 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2612 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2616 /*********************************************************************
2619 * When reading \r as last character in text mode, read() positions
2620 * the file pointer on the \r character while getc() goes on to
2623 static int read_i(int fd
, void *buf
, unsigned int count
)
2625 DWORD num_read
, utf16
;
2626 char *bufstart
= buf
;
2627 HANDLE hand
= msvcrt_fdtoh(fd
);
2628 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2633 if (fdinfo
->wxflag
& WX_ATEOF
) {
2634 TRACE("already at EOF, returning 0\n");
2637 /* Don't trace small reads, it gets *very* annoying */
2639 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2640 if (hand
== INVALID_HANDLE_VALUE
)
2642 *MSVCRT__errno() = MSVCRT_EBADF
;
2646 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2647 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2649 *MSVCRT__errno() = MSVCRT_EINVAL
;
2653 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2654 return read_utf8(fd
, buf
, count
);
2656 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2658 if (fdinfo
->lookahead
[0] != '\n')
2660 bufstart
[0] = fdinfo
->lookahead
[0];
2661 fdinfo
->lookahead
[0] = '\n';
2665 bufstart
[1] = fdinfo
->lookahead
[1];
2666 fdinfo
->lookahead
[1] = '\n';
2669 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2670 num_read
+= 1+utf16
;
2675 if(utf16
&& (num_read
&1))
2677 /* msvcr90 uses uninitialized value from the buffer in this case */
2678 /* msvcrt ignores additional data */
2679 ERR("got odd number of bytes in UTF16 mode\n");
2683 if (count
!= 0 && num_read
== 0)
2685 fdinfo
->wxflag
|= WX_ATEOF
;
2686 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2688 else if (fdinfo
->wxflag
& WX_TEXT
)
2692 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2693 fdinfo
->wxflag
|= WX_READNL
;
2695 fdinfo
->wxflag
&= ~WX_READNL
;
2697 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2699 /* in text mode, a ctrl-z signals EOF */
2700 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2702 fdinfo
->wxflag
|= WX_ATEOF
;
2703 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2707 /* in text mode, strip \r if followed by \n */
2708 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2713 lookahead
[1] = '\n';
2714 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2716 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2718 bufstart
[j
++] = '\n';
2719 if(utf16
) bufstart
[j
++] = 0;
2723 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2725 bufstart
[j
++] = '\r';
2726 if(utf16
) bufstart
[j
++] = 0;
2729 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2731 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2733 bufstart
[j
++] = '\n';
2734 if (utf16
) bufstart
[j
++] = 0;
2738 fdinfo
->lookahead
[0] = lookahead
[0];
2739 fdinfo
->lookahead
[1] = lookahead
[1];
2743 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2748 bufstart
[j
++] = '\r';
2749 if(utf16
) bufstart
[j
++] = 0;
2752 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2753 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2755 bufstart
[j
++] = bufstart
[i
];
2756 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2764 if (GetLastError() == ERROR_BROKEN_PIPE
)
2766 TRACE(":end-of-pipe\n");
2767 fdinfo
->wxflag
|= WX_ATEOF
;
2772 TRACE(":failed-last error (%d)\n",GetLastError());
2778 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2782 /*********************************************************************
2785 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2788 num_read
= read_i(fd
, buf
, count
);
2792 /*********************************************************************
2793 * _setmode (MSVCRT.@)
2795 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2797 int ret
= get_ioinfo_nolock(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2798 if(ret
==MSVCRT__O_TEXT
&& (get_ioinfo_nolock(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2799 ret
= MSVCRT__O_WTEXT
;
2801 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2802 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2803 *MSVCRT__errno() = MSVCRT_EINVAL
;
2807 if(mode
== MSVCRT__O_BINARY
) {
2808 get_ioinfo_nolock(fd
)->wxflag
&= ~WX_TEXT
;
2809 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2813 get_ioinfo_nolock(fd
)->wxflag
|= WX_TEXT
;
2814 if(mode
== MSVCRT__O_TEXT
)
2815 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2816 else if(mode
== MSVCRT__O_U8TEXT
)
2817 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2819 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2824 /*********************************************************************
2825 * _stat64 (MSVCRT.@)
2827 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2830 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2831 unsigned short mode
= ALL_S_IREAD
;
2834 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2836 plen
= strlen(path
);
2837 while (plen
&& path
[plen
-1]==' ')
2840 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2841 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2843 *MSVCRT__errno() = MSVCRT_ENOENT
;
2847 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2849 TRACE("failed (%d)\n",GetLastError());
2850 *MSVCRT__errno() = MSVCRT_ENOENT
;
2854 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2856 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2857 Bon 011120: This FIXME seems incorrect
2858 Also a letter as first char isn't enough to be classified
2861 if (isalpha(*path
)&& (*(path
+1)==':'))
2862 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2864 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2866 /* Dir, or regular file? */
2867 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2868 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2871 mode
|= MSVCRT__S_IFREG
;
2873 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2875 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2876 (tolower(path
[plen
-3]) << 16);
2877 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2878 mode
|= ALL_S_IEXEC
;
2882 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2883 mode
|= ALL_S_IWRITE
;
2885 buf
->st_mode
= mode
;
2887 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2888 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2890 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2891 buf
->st_mtime
= buf
->st_ctime
= dw
;
2892 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2893 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2894 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2898 /*********************************************************************
2899 * _stati64 (MSVCRT.@)
2901 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2904 struct MSVCRT__stat64 buf64
;
2906 ret
= MSVCRT_stat64(path
, &buf64
);
2908 msvcrt_stat64_to_stati64(&buf64
, buf
);
2912 /*********************************************************************
2915 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2918 struct MSVCRT__stat64 buf64
;
2920 ret
= MSVCRT_stat64( path
, &buf64
);
2922 msvcrt_stat64_to_stat(&buf64
, buf
);
2926 /*********************************************************************
2927 * _stat32 (MSVCR100.@)
2929 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2932 struct MSVCRT__stat64 buf64
;
2934 ret
= MSVCRT_stat64(path
, &buf64
);
2936 msvcrt_stat64_to_stat32(&buf64
, buf
);
2940 /*********************************************************************
2941 * _stat32i64 (MSVCR100.@)
2943 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2946 struct MSVCRT__stat64 buf64
;
2948 ret
= MSVCRT_stat64(path
, &buf64
);
2950 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2954 /*********************************************************************
2955 * _stat64i32 (MSVCR100.@)
2957 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2960 struct MSVCRT__stat64 buf64
;
2962 ret
= MSVCRT_stat64(path
, &buf64
);
2964 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2968 /*********************************************************************
2969 * _wstat64 (MSVCRT.@)
2971 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2974 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2975 unsigned short mode
= ALL_S_IREAD
;
2978 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2980 plen
= strlenW(path
);
2981 while (plen
&& path
[plen
-1]==' ')
2984 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2985 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2987 *MSVCRT__errno() = MSVCRT_ENOENT
;
2991 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2993 TRACE("failed (%d)\n",GetLastError());
2994 *MSVCRT__errno() = MSVCRT_ENOENT
;
2998 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3000 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3001 if (MSVCRT_iswalpha(*path
))
3002 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3004 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3006 /* Dir, or regular file? */
3007 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3008 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3011 mode
|= MSVCRT__S_IFREG
;
3013 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3015 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3016 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3017 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3018 mode
|= ALL_S_IEXEC
;
3022 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3023 mode
|= ALL_S_IWRITE
;
3025 buf
->st_mode
= mode
;
3027 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3028 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3030 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3031 buf
->st_mtime
= buf
->st_ctime
= dw
;
3032 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3033 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3034 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3038 /*********************************************************************
3039 * _wstati64 (MSVCRT.@)
3041 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3044 struct MSVCRT__stat64 buf64
;
3046 ret
= MSVCRT__wstat64(path
, &buf64
);
3048 msvcrt_stat64_to_stati64(&buf64
, buf
);
3052 /*********************************************************************
3055 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3058 struct MSVCRT__stat64 buf64
;
3060 ret
= MSVCRT__wstat64( path
, &buf64
);
3061 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3065 /*********************************************************************
3066 * _wstat32 (MSVCR100.@)
3068 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3071 struct MSVCRT__stat64 buf64
;
3073 ret
= MSVCRT__wstat64(path
, &buf64
);
3075 msvcrt_stat64_to_stat32(&buf64
, buf
);
3079 /*********************************************************************
3080 * _wstat32i64 (MSVCR100.@)
3082 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3085 struct MSVCRT__stat64 buf64
;
3087 ret
= MSVCRT__wstat64(path
, &buf64
);
3089 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3093 /*********************************************************************
3094 * _wstat64i32 (MSVCR100.@)
3096 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3099 struct MSVCRT__stat64 buf64
;
3101 ret
= MSVCRT__wstat64(path
, &buf64
);
3103 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3107 /*********************************************************************
3110 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3112 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3115 /*********************************************************************
3116 * _telli64 (MSVCRT.@)
3118 __int64 CDECL
_telli64(int fd
)
3120 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3123 /*********************************************************************
3124 * _tempnam (MSVCRT.@)
3126 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3128 char tmpbuf
[MAX_PATH
];
3129 const char *tmp_dir
= MSVCRT_getenv("TMP");
3131 if (tmp_dir
) dir
= tmp_dir
;
3133 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3134 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3136 TRACE("got name (%s)\n",tmpbuf
);
3137 DeleteFileA(tmpbuf
);
3138 return MSVCRT__strdup(tmpbuf
);
3140 TRACE("failed (%d)\n",GetLastError());
3144 /*********************************************************************
3145 * _wtempnam (MSVCRT.@)
3147 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3149 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3150 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3151 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3153 if (tmp_dir
) dir
= tmp_dir
;
3155 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3156 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3158 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3159 DeleteFileW(tmpbuf
);
3160 return MSVCRT__wcsdup(tmpbuf
);
3162 TRACE("failed (%d)\n",GetLastError());
3166 /*********************************************************************
3169 int CDECL
MSVCRT__umask(int umask
)
3171 int old_umask
= MSVCRT_umask
;
3172 TRACE("(%d)\n",umask
);
3173 MSVCRT_umask
= umask
;
3177 /*********************************************************************
3178 * _utime64 (MSVCRT.@)
3180 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3182 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3186 int retVal
= _futime64(fd
, t
);
3193 /*********************************************************************
3194 * _utime32 (MSVCRT.@)
3196 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3200 struct MSVCRT___utimbuf64 t64
;
3201 t64
.actime
= t
->actime
;
3202 t64
.modtime
= t
->modtime
;
3203 return _utime64( path
, &t64
);
3206 return _utime64( path
, NULL
);
3209 /*********************************************************************
3213 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3215 return _utime64( path
, t
);
3218 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3220 return _utime32( path
, t
);
3224 /*********************************************************************
3225 * _wutime64 (MSVCRT.@)
3227 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3229 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3233 int retVal
= _futime64(fd
, t
);
3240 /*********************************************************************
3241 * _wutime32 (MSVCRT.@)
3243 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3247 struct MSVCRT___utimbuf64 t64
;
3248 t64
.actime
= t
->actime
;
3249 t64
.modtime
= t
->modtime
;
3250 return _wutime64( path
, &t64
);
3253 return _wutime64( path
, NULL
);
3256 /*********************************************************************
3257 * _wutime (MSVCRT.@)
3260 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3262 return _wutime64( path
, t
);
3265 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3267 return _wutime32( path
, t
);
3271 /*********************************************************************
3274 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3277 ioinfo
*info
= get_ioinfo_nolock(fd
);
3278 HANDLE hand
= info
->handle
;
3280 /* Don't trace small writes, it gets *very* annoying */
3283 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3285 if (hand
== INVALID_HANDLE_VALUE
)
3287 *MSVCRT__errno() = MSVCRT_EBADF
;
3291 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3293 *MSVCRT__errno() = MSVCRT_EINVAL
;
3297 /* If appending, go to EOF */
3298 if (info
->wxflag
& WX_APPEND
)
3299 MSVCRT__lseek(fd
, 0, FILE_END
);
3301 if (!(info
->wxflag
& WX_TEXT
))
3303 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3304 && (num_written
== count
))
3306 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3307 hand
, GetLastError());
3308 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3312 unsigned int i
, j
, nr_lf
, size
;
3315 const char *s
= buf
, *buf_start
= buf
;
3317 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3319 /* find number of \n */
3320 for (nr_lf
=0, i
=0; i
<count
; i
++)
3326 if ((q
= p
= MSVCRT_malloc(size
)))
3328 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3337 FIXME("Malloc failed\n");
3349 else if (info
->exflag
& EF_UTF16
)
3351 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3352 if (s
[i
]=='\n' && s
[i
+1]==0)
3357 if ((q
= p
= MSVCRT_malloc(size
)))
3359 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3361 if (s
[i
]=='\n' && s
[i
+1]==0)
3372 FIXME("Malloc failed\n");
3388 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3389 if (s
[i
]=='\n' && s
[i
+1]==0)
3392 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3394 msvcrt_set_errno(GetLastError());
3399 size
= conv_len
+nr_lf
;
3400 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3402 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3404 if (s
[i
]=='\n' && s
[i
+1]==0)
3412 q
= p
+count
+nr_lf
*2;
3413 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3414 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3418 FIXME("Malloc failed\n");
3425 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3429 if (num_written
!= size
)
3431 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3432 fd
, hand
, GetLastError(), num_written
);
3433 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3434 return s
- buf_start
;
3442 /*********************************************************************
3445 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3449 MSVCRT__lock_file(file
);
3450 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3451 if (len
== sizeof(val
)) {
3452 MSVCRT__unlock_file(file
);
3456 file
->_flag
|= MSVCRT__IOERR
;
3457 MSVCRT__unlock_file(file
);
3461 /*********************************************************************
3464 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3468 MSVCRT__lock_file(file
);
3469 ret
= MSVCRT__fclose_nolock(file
);
3470 MSVCRT__unlock_file(file
);
3475 /*********************************************************************
3476 * _fclose_nolock (MSVCRT.@)
3478 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3483 MSVCRT_free(file
->_tmpfname
);
3484 file
->_tmpfname
= NULL
;
3485 /* flush stdio buffers */
3486 if(file
->_flag
& MSVCRT__IOWRT
)
3487 MSVCRT__fflush_nolock(file
);
3488 if(file
->_flag
& MSVCRT__IOMYBUF
)
3489 MSVCRT_free(file
->_base
);
3491 r
=MSVCRT__close(file
->_file
);
3494 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3497 /*********************************************************************
3500 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3502 return file
->_flag
& MSVCRT__IOEOF
;
3505 /*********************************************************************
3508 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3510 return file
->_flag
& MSVCRT__IOERR
;
3513 /*********************************************************************
3514 * _filbuf (MSVCRT.@)
3516 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3520 if(file
->_flag
& MSVCRT__IOSTRG
)
3523 /* Allocate buffer if needed */
3524 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3525 msvcrt_alloc_buffer(file
);
3527 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3528 if(file
->_flag
& MSVCRT__IORW
)
3529 file
->_flag
|= MSVCRT__IOREAD
;
3534 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3536 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3537 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3543 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3545 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3551 file
->_ptr
= file
->_base
+1;
3552 c
= *(unsigned char *)file
->_base
;
3557 /*********************************************************************
3560 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3564 MSVCRT__lock_file(file
);
3565 ret
= MSVCRT__fgetc_nolock(file
);
3566 MSVCRT__unlock_file(file
);
3571 /*********************************************************************
3572 * _fgetc_nolock (MSVCRT.@)
3574 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3581 i
= (unsigned char *)file
->_ptr
++;
3584 j
= MSVCRT__filbuf(file
);
3589 /*********************************************************************
3590 * _fgetchar (MSVCRT.@)
3592 int CDECL
MSVCRT__fgetchar(void)
3594 return MSVCRT_fgetc(MSVCRT_stdin
);
3597 /*********************************************************************
3600 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3602 int cc
= MSVCRT_EOF
;
3603 char * buf_start
= s
;
3605 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3606 file
,file
->_file
,s
,size
);
3608 MSVCRT__lock_file(file
);
3610 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3615 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3617 TRACE(":nothing read\n");
3618 MSVCRT__unlock_file(file
);
3621 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3624 TRACE(":got %s\n", debugstr_a(buf_start
));
3625 MSVCRT__unlock_file(file
);
3629 /*********************************************************************
3632 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3636 MSVCRT__lock_file(file
);
3637 ret
= MSVCRT__fgetwc_nolock(file
);
3638 MSVCRT__unlock_file(file
);
3643 /*********************************************************************
3644 * _fgetwc_nolock (MSVCRT.@)
3646 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3651 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3652 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3655 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3656 ch
= MSVCRT__fgetc_nolock(file
);
3657 if(ch
== MSVCRT_EOF
) {
3664 char mbs
[MSVCRT_MB_LEN_MAX
];
3667 ch
= MSVCRT__fgetc_nolock(file
);
3668 if(ch
!= MSVCRT_EOF
) {
3670 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3671 ch
= MSVCRT__fgetc_nolock(file
);
3672 if(ch
!= MSVCRT_EOF
) {
3681 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3688 /*********************************************************************
3691 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3698 MSVCRT__lock_file(file
);
3699 for (j
=0; j
<sizeof(int); j
++) {
3700 k
= MSVCRT__fgetc_nolock(file
);
3701 if (k
== MSVCRT_EOF
) {
3702 file
->_flag
|= MSVCRT__IOEOF
;
3703 MSVCRT__unlock_file(file
);
3709 MSVCRT__unlock_file(file
);
3713 /*********************************************************************
3716 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3718 return MSVCRT_fgetwc(file
);
3721 /*********************************************************************
3722 * _fgetwchar (MSVCRT.@)
3724 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3726 return MSVCRT_fgetwc(MSVCRT_stdin
);
3729 /*********************************************************************
3730 * getwchar (MSVCRT.@)
3732 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3734 return MSVCRT__fgetwchar();
3737 /*********************************************************************
3740 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3742 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3743 MSVCRT_wchar_t
* buf_start
= s
;
3745 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3746 file
,file
->_file
,s
,size
);
3748 MSVCRT__lock_file(file
);
3750 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3755 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3757 TRACE(":nothing read\n");
3758 MSVCRT__unlock_file(file
);
3761 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3764 TRACE(":got %s\n", debugstr_w(buf_start
));
3765 MSVCRT__unlock_file(file
);
3769 /*********************************************************************
3770 * _flsbuf (MSVCRT.@)
3772 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3774 /* Flush output buffer */
3775 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3776 msvcrt_alloc_buffer(file
);
3779 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3780 if(!(file
->_flag
& MSVCRT__IORW
)) {
3781 file
->_flag
|= MSVCRT__IOERR
;
3784 file
->_flag
|= MSVCRT__IOWRT
;
3786 if(file
->_flag
& MSVCRT__IOREAD
) {
3787 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3788 file
->_flag
|= MSVCRT__IOERR
;
3792 file
->_ptr
= file
->_base
;
3793 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3796 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3799 if(file
->_cnt
<= 0) {
3800 res
= msvcrt_flush_buffer(file
);
3803 file
->_flag
|= MSVCRT__IOWRT
;
3804 file
->_cnt
=file
->_bufsiz
;
3812 /* set _cnt to 0 for unbuffered FILEs */
3814 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3817 file
->_flag
|= MSVCRT__IOERR
;
3822 /*********************************************************************
3825 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3829 MSVCRT__lock_file(file
);
3830 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3831 MSVCRT__unlock_file(file
);
3836 /*********************************************************************
3837 * _fwrite_nolock (MSVCRT.@)
3839 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3841 MSVCRT_size_t wrcnt
=size
* nmemb
;
3847 if(file
->_cnt
< 0) {
3848 WARN("negative file->_cnt value in %p\n", file
);
3849 file
->_flag
|= MSVCRT__IOERR
;
3851 } else if(file
->_cnt
) {
3852 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3853 memcpy(file
->_ptr
, ptr
, pcnt
);
3858 ptr
= (const char*)ptr
+ pcnt
;
3859 } else if((file
->_flag
& MSVCRT__IONBF
)
3860 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3861 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3865 if(file
->_flag
& MSVCRT__IONBF
)
3867 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3868 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3870 bufsiz
= file
->_bufsiz
;
3872 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3874 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3877 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3878 file
->_flag
|= MSVCRT__IOERR
;
3883 ptr
= (const char*)ptr
+ pcnt
;
3885 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3889 ptr
= (const char*)ptr
+ 1;
3893 return written
/ size
;
3896 /*********************************************************************
3899 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3903 MSVCRT__lock_file(file
);
3904 ret
= MSVCRT__fputwc_nolock(wc
, file
);
3905 MSVCRT__unlock_file(file
);
3910 /*********************************************************************
3911 * _fputwc_nolock (MSVCRT.@)
3913 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3915 MSVCRT_wchar_t mwc
=wc
;
3919 fdinfo
= get_ioinfo_nolock(file
->_file
);
3921 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3922 char buf
[MSVCRT_MB_LEN_MAX
];
3925 char_len
= MSVCRT_wctomb(buf
, mwc
);
3926 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
3930 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3939 /*********************************************************************
3940 * _fputwchar (MSVCRT.@)
3942 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3944 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3947 /*********************************************************************
3948 * _wfsopen (MSVCRT.@)
3950 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3953 int open_flags
, stream_flags
, fd
;
3955 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3957 /* map mode string to open() flags. "man fopen" for possibilities. */
3958 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3962 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3965 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3967 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3974 TRACE(":got (%p)\n",file
);
3975 if (fd
>= 0 && !file
)
3981 /*********************************************************************
3982 * _fsopen (MSVCRT.@)
3984 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3987 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3989 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3990 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3991 *MSVCRT__errno() = MSVCRT_EINVAL
;
3994 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3997 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3998 *MSVCRT__errno() = MSVCRT_EINVAL
;
4002 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4009 /*********************************************************************
4012 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4014 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4017 /*********************************************************************
4018 * fopen_s (MSVCRT.@)
4020 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4021 const char *filename
, const char *mode
)
4023 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4024 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4025 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4027 *pFile
= MSVCRT_fopen(filename
, mode
);
4030 return *MSVCRT__errno();
4034 /*********************************************************************
4035 * _wfopen (MSVCRT.@)
4037 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4039 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4042 /*********************************************************************
4043 * _wfopen_s (MSVCRT.@)
4045 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4046 const MSVCRT_wchar_t
*mode
)
4048 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4049 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4050 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4052 *pFile
= MSVCRT__wfopen(filename
, mode
);
4055 return *MSVCRT__errno();
4059 /*********************************************************************
4062 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4066 MSVCRT__lock_file(file
);
4067 ret
= MSVCRT__fputc_nolock(c
, file
);
4068 MSVCRT__unlock_file(file
);
4073 /*********************************************************************
4074 * _fputc_nolock (MSVCRT.@)
4076 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4085 res
= msvcrt_flush_buffer(file
);
4086 return res
? res
: c
;
4092 res
= MSVCRT__flsbuf(c
, file
);
4097 /*********************************************************************
4098 * _fputchar (MSVCRT.@)
4100 int CDECL
MSVCRT__fputchar(int c
)
4102 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4105 /*********************************************************************
4108 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4112 MSVCRT__lock_file(file
);
4113 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4114 MSVCRT__unlock_file(file
);
4119 /*********************************************************************
4120 * _fread_nolock (MSVCRT.@)
4122 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4124 MSVCRT_size_t rcnt
=size
* nmemb
;
4125 MSVCRT_size_t read
=0;
4126 MSVCRT_size_t pread
=0;
4131 /* first buffered data */
4133 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4134 memcpy(ptr
, file
->_ptr
, pcnt
);
4139 ptr
= (char*)ptr
+ pcnt
;
4140 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4141 if(file
->_flag
& MSVCRT__IORW
) {
4142 file
->_flag
|= MSVCRT__IOREAD
;
4148 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4149 msvcrt_alloc_buffer(file
);
4154 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4155 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4156 file
->_ptr
= file
->_base
;
4157 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4158 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4159 if (i
> 0 && i
< file
->_cnt
) {
4160 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4161 file
->_flag
&= ~MSVCRT__IOEOF
;
4164 memcpy(ptr
, file
->_ptr
, i
);
4168 } else if (rcnt
> INT_MAX
) {
4169 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4170 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4171 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4173 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4177 ptr
= (char *)ptr
+i
;
4178 /* expose feof condition in the flags
4179 * MFC tests file->_flag for feof, and doesn't call feof())
4181 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4182 file
->_flag
|= MSVCRT__IOEOF
;
4185 file
->_flag
|= MSVCRT__IOERR
;
4196 /*********************************************************************
4197 * fread_s (MSVCR80.@)
4199 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4200 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4204 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4206 memset(buf
, 0, buf_size
);
4209 if(!elem_size
|| !count
) return 0;
4211 MSVCRT__lock_file(stream
);
4212 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4213 MSVCRT__unlock_file(stream
);
4218 /*********************************************************************
4219 * _fread_nolock_s (MSVCR80.@)
4221 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4222 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4224 size_t bytes_left
, buf_pos
;
4226 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4228 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4230 memset(buf
, 0, buf_size
);
4233 if(!elem_size
|| !count
) return 0;
4234 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4235 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4237 bytes_left
= elem_size
*count
;
4240 if(stream
->_cnt
> 0) {
4241 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4243 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4244 memset(buf
, 0, buf_size
);
4248 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4252 int c
= MSVCRT__filbuf(stream
);
4257 if(!MSVCRT_CHECK_PMT_ERR(buf_size
-buf_pos
> 0, MSVCRT_ERANGE
)) {
4258 memset(buf
, 0, buf_size
);
4262 ((char*)buf
)[buf_pos
++] = c
;
4267 return buf_pos
/elem_size
;
4270 /*********************************************************************
4271 * _wfreopen (MSVCRT.@)
4274 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4276 int open_flags
, stream_flags
, fd
;
4278 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4281 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4285 MSVCRT_fclose(file
);
4286 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4288 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4290 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4300 /*********************************************************************
4301 * _wfreopen_s (MSVCRT.@)
4303 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4304 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4306 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4307 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4308 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4309 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4311 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4314 return *MSVCRT__errno();
4318 /*********************************************************************
4319 * freopen (MSVCRT.@)
4322 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4325 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4327 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4328 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4334 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4341 /*********************************************************************
4342 * freopen_s (MSVCRT.@)
4344 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4345 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4347 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4348 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4349 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4350 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4352 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4355 return *MSVCRT__errno();
4359 /*********************************************************************
4360 * fsetpos (MSVCRT.@)
4362 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4366 MSVCRT__lock_file(file
);
4367 msvcrt_flush_buffer(file
);
4369 /* Reset direction of i/o */
4370 if(file
->_flag
& MSVCRT__IORW
) {
4371 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4374 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4375 MSVCRT__unlock_file(file
);
4379 /*********************************************************************
4380 * _ftelli64 (MSVCRT.@)
4382 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4386 MSVCRT__lock_file(file
);
4387 ret
= MSVCRT__ftelli64_nolock(file
);
4388 MSVCRT__unlock_file(file
);
4393 /*********************************************************************
4394 * _ftelli64_nolock (MSVCRT.@)
4396 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4400 pos
= _telli64(file
->_file
);
4403 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4404 if(file
->_flag
& MSVCRT__IOWRT
) {
4405 pos
+= file
->_ptr
- file
->_base
;
4407 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4410 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4414 } else if(!file
->_cnt
) { /* nothing to do */
4415 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4419 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4420 for(i
=0; i
<file
->_cnt
; i
++)
4421 if(file
->_ptr
[i
] == '\n')
4427 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4430 pos
-= file
->_bufsiz
;
4431 pos
+= file
->_ptr
- file
->_base
;
4433 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4434 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4437 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4447 /*********************************************************************
4450 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4452 return MSVCRT__ftelli64(file
);
4455 /*********************************************************************
4456 * _ftell_nolock (MSVCRT.@)
4458 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4460 return MSVCRT__ftelli64_nolock(file
);
4463 /*********************************************************************
4464 * fgetpos (MSVCRT.@)
4466 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4468 *pos
= MSVCRT__ftelli64(file
);
4474 /*********************************************************************
4477 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4479 MSVCRT_size_t len
= strlen(s
);
4482 MSVCRT__lock_file(file
);
4483 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4484 MSVCRT__unlock_file(file
);
4488 /*********************************************************************
4491 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4493 MSVCRT_size_t i
, len
= strlenW(s
);
4497 MSVCRT__lock_file(file
);
4498 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4499 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4500 MSVCRT__unlock_file(file
);
4504 tmp_buf
= add_std_buffer(file
);
4505 for (i
=0; i
<len
; i
++) {
4506 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4507 if(tmp_buf
) remove_std_buffer(file
);
4508 MSVCRT__unlock_file(file
);
4513 if(tmp_buf
) remove_std_buffer(file
);
4514 MSVCRT__unlock_file(file
);
4518 /*********************************************************************
4519 * getchar (MSVCRT.@)
4521 int CDECL
MSVCRT_getchar(void)
4523 return MSVCRT_fgetc(MSVCRT_stdin
);
4526 /*********************************************************************
4529 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4531 return MSVCRT_fgetc(file
);
4534 /*********************************************************************
4537 char * CDECL
MSVCRT_gets(char *buf
)
4540 char * buf_start
= buf
;
4542 MSVCRT__lock_file(MSVCRT_stdin
);
4543 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4544 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4545 if(cc
!= '\r') *buf
++ = (char)cc
;
4549 TRACE("got '%s'\n", buf_start
);
4550 MSVCRT__unlock_file(MSVCRT_stdin
);
4554 /*********************************************************************
4557 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4560 MSVCRT_wchar_t
* ws
= buf
;
4562 MSVCRT__lock_file(MSVCRT_stdin
);
4563 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4564 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4567 *buf
++ = (MSVCRT_wchar_t
)cc
;
4571 TRACE("got %s\n", debugstr_w(ws
));
4572 MSVCRT__unlock_file(MSVCRT_stdin
);
4576 /*********************************************************************
4579 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4581 return MSVCRT_fputc(c
, file
);
4584 /*********************************************************************
4585 * putchar (MSVCRT.@)
4587 int CDECL
MSVCRT_putchar(int c
)
4589 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4592 /*********************************************************************
4593 * _putwch (MSVCRT.@)
4595 int CDECL
MSVCRT__putwch(int c
)
4597 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4600 /*********************************************************************
4603 int CDECL
MSVCRT_puts(const char *s
)
4605 MSVCRT_size_t len
= strlen(s
);
4608 MSVCRT__lock_file(MSVCRT_stdout
);
4609 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4610 MSVCRT__unlock_file(MSVCRT_stdout
);
4614 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4615 MSVCRT__unlock_file(MSVCRT_stdout
);
4619 /*********************************************************************
4622 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4624 static const MSVCRT_wchar_t nl
= '\n';
4625 MSVCRT_size_t len
= strlenW(s
);
4628 MSVCRT__lock_file(MSVCRT_stdout
);
4629 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4630 MSVCRT__unlock_file(MSVCRT_stdout
);
4634 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4635 MSVCRT__unlock_file(MSVCRT_stdout
);
4639 /*********************************************************************
4642 int CDECL
MSVCRT_remove(const char *path
)
4644 TRACE("(%s)\n",path
);
4645 if (DeleteFileA(path
))
4647 TRACE(":failed (%d)\n",GetLastError());
4648 msvcrt_set_errno(GetLastError());
4652 /*********************************************************************
4653 * _wremove (MSVCRT.@)
4655 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4657 TRACE("(%s)\n",debugstr_w(path
));
4658 if (DeleteFileW(path
))
4660 TRACE(":failed (%d)\n",GetLastError());
4661 msvcrt_set_errno(GetLastError());
4665 /*********************************************************************
4668 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4670 TRACE(":from %s to %s\n",oldpath
,newpath
);
4671 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4673 TRACE(":failed (%d)\n",GetLastError());
4674 msvcrt_set_errno(GetLastError());
4678 /*********************************************************************
4679 * _wrename (MSVCRT.@)
4681 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4683 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4684 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4686 TRACE(":failed (%d)\n",GetLastError());
4687 msvcrt_set_errno(GetLastError());
4691 /*********************************************************************
4692 * setvbuf (MSVCRT.@)
4694 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4696 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4697 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4698 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4700 MSVCRT__lock_file(file
);
4702 MSVCRT__fflush_nolock(file
);
4703 if(file
->_flag
& MSVCRT__IOMYBUF
)
4704 MSVCRT_free(file
->_base
);
4705 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4708 if(mode
== MSVCRT__IONBF
) {
4709 file
->_flag
|= MSVCRT__IONBF
;
4710 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4713 file
->_base
= file
->_ptr
= buf
;
4714 file
->_flag
|= MSVCRT__USERBUF
;
4715 file
->_bufsiz
= size
;
4717 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4720 MSVCRT__unlock_file(file
);
4724 file
->_flag
|= MSVCRT__IOMYBUF
;
4725 file
->_bufsiz
= size
;
4727 MSVCRT__unlock_file(file
);
4731 /*********************************************************************
4734 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4736 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4739 /*********************************************************************
4742 char * CDECL
MSVCRT_tmpnam(char *s
)
4749 thread_data_t
*data
= msvcrt_get_thread_data();
4751 if(!data
->tmpnam_buffer
)
4752 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4754 s
= data
->tmpnam_buffer
;
4757 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4758 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4759 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4761 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4762 memcpy(p
, tmpstr
, size
);
4764 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4765 GetLastError() == ERROR_FILE_NOT_FOUND
)
4771 /*********************************************************************
4772 * _wtmpnam (MSVCRT.@)
4774 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4776 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4777 MSVCRT_wchar_t tmpstr
[16];
4781 thread_data_t
*data
= msvcrt_get_thread_data();
4783 if(!data
->wtmpnam_buffer
)
4784 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4786 s
= data
->wtmpnam_buffer
;
4789 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4790 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4791 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4793 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4794 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4796 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4797 GetLastError() == ERROR_FILE_NOT_FOUND
)
4803 /*********************************************************************
4804 * tmpfile (MSVCRT.@)
4806 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4808 char *filename
= MSVCRT_tmpnam(NULL
);
4810 MSVCRT_FILE
* file
= NULL
;
4813 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4814 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4815 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4817 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4822 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4825 if(fd
!= -1 && !file
)
4831 /*********************************************************************
4832 * tmpfile_s (MSVCRT.@)
4834 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4836 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4838 *file
= MSVCRT_tmpfile();
4842 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4844 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4847 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4851 MSVCRT__lock_file(file
);
4853 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4854 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4855 MSVCRT__unlock_file(file
);
4859 for(i
=0; i
<len
; i
++) {
4860 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
4861 MSVCRT__unlock_file(file
);
4866 MSVCRT__unlock_file(file
);
4870 /*********************************************************************
4871 * vfprintf (MSVCRT.@)
4873 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4878 MSVCRT__lock_file(file
);
4879 tmp_buf
= add_std_buffer(file
);
4880 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4881 if(tmp_buf
) remove_std_buffer(file
);
4882 MSVCRT__unlock_file(file
);
4887 /*********************************************************************
4888 * vfprintf_s (MSVCRT.@)
4890 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4895 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4897 MSVCRT__lock_file(file
);
4898 tmp_buf
= add_std_buffer(file
);
4899 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4900 if(tmp_buf
) remove_std_buffer(file
);
4901 MSVCRT__unlock_file(file
);
4906 /*********************************************************************
4907 * vfwprintf (MSVCRT.@)
4909 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4914 MSVCRT__lock_file(file
);
4915 tmp_buf
= add_std_buffer(file
);
4916 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4917 if(tmp_buf
) remove_std_buffer(file
);
4918 MSVCRT__unlock_file(file
);
4923 /*********************************************************************
4924 * vfwprintf_s (MSVCRT.@)
4926 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4931 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4933 MSVCRT__lock_file(file
);
4934 tmp_buf
= add_std_buffer(file
);
4935 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4936 if(tmp_buf
) remove_std_buffer(file
);
4937 MSVCRT__unlock_file(file
);
4942 /*********************************************************************
4943 * _vfwprintf_l (MSVCRT.@)
4945 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4946 MSVCRT__locale_t locale
, __ms_va_list valist
)
4951 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4953 MSVCRT__lock_file(file
);
4954 tmp_buf
= add_std_buffer(file
);
4955 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4956 if(tmp_buf
) remove_std_buffer(file
);
4957 MSVCRT__unlock_file(file
);
4962 /*********************************************************************
4963 * vprintf (MSVCRT.@)
4965 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4967 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4970 /*********************************************************************
4971 * vprintf_s (MSVCRT.@)
4973 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4975 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4978 /*********************************************************************
4979 * vwprintf (MSVCRT.@)
4981 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4983 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4986 /*********************************************************************
4987 * vwprintf_s (MSVCRT.@)
4989 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4991 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4994 /*********************************************************************
4995 * fprintf (MSVCRT.@)
4997 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4999 __ms_va_list valist
;
5001 __ms_va_start(valist
, format
);
5002 res
= MSVCRT_vfprintf(file
, format
, valist
);
5003 __ms_va_end(valist
);
5007 /*********************************************************************
5008 * fprintf_s (MSVCRT.@)
5010 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5012 __ms_va_list valist
;
5014 __ms_va_start(valist
, format
);
5015 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5016 __ms_va_end(valist
);
5020 /*********************************************************************
5021 * fwprintf (MSVCRT.@)
5023 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5025 __ms_va_list valist
;
5027 __ms_va_start(valist
, format
);
5028 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5029 __ms_va_end(valist
);
5033 /*********************************************************************
5034 * fwprintf_s (MSVCRT.@)
5036 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5038 __ms_va_list valist
;
5040 __ms_va_start(valist
, format
);
5041 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5042 __ms_va_end(valist
);
5046 /*********************************************************************
5047 * _fwprintf_l (MSVCRT.@)
5049 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5051 __ms_va_list valist
;
5053 __ms_va_start(valist
, locale
);
5054 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5055 __ms_va_end(valist
);
5059 /*********************************************************************
5062 int CDECL
MSVCRT_printf(const char *format
, ...)
5064 __ms_va_list valist
;
5066 __ms_va_start(valist
, format
);
5067 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5068 __ms_va_end(valist
);
5072 /*********************************************************************
5073 * printf_s (MSVCRT.@)
5075 int CDECL
MSVCRT_printf_s(const char *format
, ...)
5077 __ms_va_list valist
;
5079 __ms_va_start(valist
, format
);
5080 res
= MSVCRT_vprintf_s(format
, valist
);
5081 __ms_va_end(valist
);
5085 /*********************************************************************
5088 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5092 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5094 MSVCRT__lock_file(file
);
5095 ret
= MSVCRT__ungetc_nolock(c
, file
);
5096 MSVCRT__unlock_file(file
);
5101 /*********************************************************************
5102 * _ungetc_nolock (MSVCRT.@)
5104 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5106 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5108 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5109 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5112 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5113 && msvcrt_alloc_buffer(file
))
5114 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5117 if(file
->_ptr
>file
->_base
) {
5119 if(file
->_flag
& MSVCRT__IOSTRG
) {
5120 if(*file
->_ptr
!= c
) {
5128 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5129 file
->_flag
|= MSVCRT__IOREAD
;
5136 /*********************************************************************
5137 * ungetwc (MSVCRT.@)
5139 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5143 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5145 MSVCRT__lock_file(file
);
5146 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5147 MSVCRT__unlock_file(file
);
5152 /*********************************************************************
5153 * _ungetwc_nolock (MSVCRT.@)
5155 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5157 MSVCRT_wchar_t mwc
= wc
;
5159 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5160 if (wc
== MSVCRT_WEOF
)
5163 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5164 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5165 unsigned char * pp
= (unsigned char *)&mwc
;
5168 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5169 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5173 char mbs
[MSVCRT_MB_LEN_MAX
];
5176 len
= MSVCRT_wctomb(mbs
, mwc
);
5180 for(len
--; len
>=0; len
--) {
5181 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5189 /*********************************************************************
5190 * wprintf (MSVCRT.@)
5192 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5194 __ms_va_list valist
;
5196 __ms_va_start(valist
, format
);
5197 res
= MSVCRT_vwprintf(format
, valist
);
5198 __ms_va_end(valist
);
5202 /*********************************************************************
5203 * wprintf_s (MSVCRT.@)
5205 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5207 __ms_va_list valist
;
5209 __ms_va_start(valist
, format
);
5210 res
= MSVCRT_vwprintf_s(format
, valist
);
5211 __ms_va_end(valist
);
5215 /*********************************************************************
5216 * _getmaxstdio (MSVCRT.@)
5218 int CDECL
MSVCRT__getmaxstdio(void)
5220 return MSVCRT_max_streams
;
5223 /*********************************************************************
5224 * _setmaxstdio (MSVCRT.@)
5226 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5228 TRACE("%d\n", newmax
);
5230 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5233 MSVCRT_max_streams
= newmax
;
5234 return MSVCRT_max_streams
;