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__IOMYBUF
| MSVCRT__USERBUF
)) {
610 int cnt
=file
->_ptr
-file
->_base
;
611 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
612 file
->_flag
|= MSVCRT__IOERR
;
615 file
->_ptr
=file
->_base
;
621 /*********************************************************************
624 int CDECL
MSVCRT__isatty(int fd
)
626 TRACE(":fd (%d)\n",fd
);
628 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
631 /* INTERNAL: Allocate stdio file buffer */
632 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
634 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
635 && MSVCRT__isatty(file
->_file
))
638 file
->_base
= MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
640 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
641 file
->_flag
|= MSVCRT__IOMYBUF
;
643 file
->_base
= (char*)(&file
->_charbuf
);
645 file
->_flag
|= MSVCRT__IONBF
;
647 file
->_ptr
= file
->_base
;
652 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
653 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
655 static char buffers
[2][MSVCRT_BUFSIZ
];
657 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
658 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
659 || !MSVCRT__isatty(file
->_file
))
662 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
663 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
664 file
->_flag
|= MSVCRT__USERBUF
;
668 /* INTERNAL: Removes temporary buffer from stdout or stderr */
669 /* Only call this function when add_std_buffer returned TRUE */
670 static void remove_std_buffer(MSVCRT_FILE
*file
)
672 msvcrt_flush_buffer(file
);
673 file
->_ptr
= file
->_base
= NULL
;
674 file
->_bufsiz
= file
->_cnt
= 0;
675 file
->_flag
&= ~MSVCRT__USERBUF
;
678 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
679 static int msvcrt_int_to_base32(int num
, char *str
)
694 *p
= (num
& 31) + '0';
696 *p
+= ('a' - '0' - 10);
703 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
704 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
719 *p
= (num
& 31) + '0';
721 *p
+= ('a' - '0' - 10);
728 /*********************************************************************
729 * __iob_func(MSVCRT.@)
731 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
733 return &MSVCRT__iob
[0];
736 /*********************************************************************
739 int CDECL
MSVCRT__access(const char *filename
, int mode
)
741 DWORD attr
= GetFileAttributesA(filename
);
743 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
745 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
747 msvcrt_set_errno(GetLastError());
750 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
752 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
758 /*********************************************************************
759 * _access_s (MSVCRT.@)
761 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
763 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
764 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
766 if (MSVCRT__access(filename
, mode
) == -1)
767 return *MSVCRT__errno();
771 /*********************************************************************
772 * _waccess (MSVCRT.@)
774 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
776 DWORD attr
= GetFileAttributesW(filename
);
778 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
780 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
782 msvcrt_set_errno(GetLastError());
785 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
787 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
793 /*********************************************************************
794 * _waccess_s (MSVCRT.@)
796 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
798 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
799 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
801 if (MSVCRT__waccess(filename
, mode
) == -1)
802 return *MSVCRT__errno();
806 /*********************************************************************
809 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
811 DWORD oldFlags
= GetFileAttributesA(path
);
813 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
815 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
816 oldFlags
| FILE_ATTRIBUTE_READONLY
;
818 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
821 msvcrt_set_errno(GetLastError());
825 /*********************************************************************
828 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
830 DWORD oldFlags
= GetFileAttributesW(path
);
832 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
834 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
835 oldFlags
| FILE_ATTRIBUTE_READONLY
;
837 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
840 msvcrt_set_errno(GetLastError());
844 /*********************************************************************
847 int CDECL
MSVCRT__unlink(const char *path
)
849 TRACE("%s\n",debugstr_a(path
));
850 if(DeleteFileA(path
))
852 TRACE("failed (%d)\n",GetLastError());
853 msvcrt_set_errno(GetLastError());
857 /*********************************************************************
858 * _wunlink (MSVCRT.@)
860 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
862 TRACE("(%s)\n",debugstr_w(path
));
863 if(DeleteFileW(path
))
865 TRACE("failed (%d)\n",GetLastError());
866 msvcrt_set_errno(GetLastError());
870 /*********************************************************************
873 int CDECL
MSVCRT__commit(int fd
)
875 ioinfo
*info
= get_ioinfo(fd
);
878 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
880 if (info
->handle
== INVALID_HANDLE_VALUE
)
882 else if (!FlushFileBuffers(info
->handle
))
884 if (GetLastError() == ERROR_INVALID_HANDLE
)
886 /* FlushFileBuffers fails for console handles
887 * so we ignore this error.
893 TRACE(":failed-last error (%d)\n",GetLastError());
894 msvcrt_set_errno(GetLastError());
904 release_ioinfo(info
);
908 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
909 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
911 /* INTERNAL: Flush all stream buffer */
912 static int msvcrt_flush_all_buffers(int mask
)
914 int i
, num_flushed
= 0;
918 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
919 file
= msvcrt_get_file(i
);
923 if(file
->_flag
& mask
) {
931 TRACE(":flushed (%d) handles\n",num_flushed
);
935 /*********************************************************************
936 * _flushall (MSVCRT.@)
938 int CDECL
MSVCRT__flushall(void)
940 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
943 /*********************************************************************
946 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
951 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
954 MSVCRT__lock_file(file
);
955 ret
= MSVCRT__fflush_nolock(file
);
956 MSVCRT__unlock_file(file
);
962 /*********************************************************************
963 * _fflush_nolock (MSVCRT.@)
965 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
968 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
969 } else if(file
->_flag
& MSVCRT__IOWRT
) {
972 res
= msvcrt_flush_buffer(file
);
973 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
974 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
977 } else if(file
->_flag
& MSVCRT__IOREAD
) {
979 file
->_ptr
= file
->_base
;
986 /*********************************************************************
989 int CDECL
MSVCRT__close(int fd
)
991 ioinfo
*info
= get_ioinfo(fd
);
995 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
996 if (!(info
->wxflag
& WX_OPEN
)) {
999 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1002 WARN(":failed-last error (%d)\n",GetLastError());
1003 msvcrt_set_errno(GetLastError());
1007 release_ioinfo(info
);
1011 /*********************************************************************
1014 * MSDN isn't clear on this point, but the remarks for _pipe
1015 * indicate file descriptors duplicated with _dup and _dup2 are always
1018 int CDECL
MSVCRT__dup2(int od
, int nd
)
1022 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1024 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
1028 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od
)->handle
,
1029 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1031 int wxflag
= get_ioinfo_nolock(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
1033 if (msvcrt_is_valid_fd(nd
))
1035 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
1038 CloseHandle(handle
);
1039 *MSVCRT__errno() = MSVCRT_EMFILE
;
1043 /* _dup2 returns 0, not nd, on success */
1050 msvcrt_set_errno(GetLastError());
1055 *MSVCRT__errno() = MSVCRT_EBADF
;
1062 /*********************************************************************
1065 int CDECL
MSVCRT__dup(int od
)
1070 fd
= MSVCRT_fdstart
;
1071 if (MSVCRT__dup2(od
, fd
) == 0)
1079 /*********************************************************************
1082 int CDECL
MSVCRT__eof(int fd
)
1084 ioinfo
*info
= get_ioinfo(fd
);
1085 DWORD curpos
,endpos
;
1086 LONG hcurpos
,hendpos
;
1088 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1090 if (info
->handle
== INVALID_HANDLE_VALUE
)
1092 release_ioinfo(info
);
1096 if (info
->wxflag
& WX_ATEOF
)
1098 release_ioinfo(info
);
1102 /* Otherwise we do it the hard way */
1103 hcurpos
= hendpos
= 0;
1104 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1105 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1107 if (curpos
== endpos
&& hcurpos
== hendpos
)
1109 /* FIXME: shouldn't WX_ATEOF be set here? */
1110 release_ioinfo(info
);
1114 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1115 release_ioinfo(info
);
1119 /*********************************************************************
1120 * _fcloseall (MSVCRT.@)
1122 int CDECL
MSVCRT__fcloseall(void)
1124 int num_closed
= 0, i
;
1128 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1129 file
= msvcrt_get_file(i
);
1131 if (file
->_flag
&& !MSVCRT_fclose(file
))
1136 TRACE(":closed (%d) handles\n",num_closed
);
1140 /* free everything on process exit */
1141 void msvcrt_free_io(void)
1147 MSVCRT__fcloseall();
1149 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1151 if(!MSVCRT___pioinfo
[i
])
1154 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1156 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1157 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1159 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1162 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1164 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1165 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1167 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1168 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1172 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1173 MSVCRT_free(MSVCRT_fstream
[i
]);
1175 DeleteCriticalSection(&MSVCRT_file_cs
);
1178 /*********************************************************************
1179 * _lseeki64 (MSVCRT.@)
1181 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1183 ioinfo
*info
= get_ioinfo(fd
);
1186 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1188 if (info
->handle
== INVALID_HANDLE_VALUE
)
1190 release_ioinfo(info
);
1194 if (whence
< 0 || whence
> 2)
1196 release_ioinfo(info
);
1197 *MSVCRT__errno() = MSVCRT_EINVAL
;
1201 TRACE(":fd (%d) to %s pos %s\n",
1202 fd
,wine_dbgstr_longlong(offset
),
1203 (whence
==SEEK_SET
)?"SEEK_SET":
1204 (whence
==SEEK_CUR
)?"SEEK_CUR":
1205 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1207 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1208 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1209 ofs
.QuadPart
= offset
;
1210 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1211 GetLastError() == ERROR_SUCCESS
)
1213 info
->wxflag
&= ~WX_ATEOF
;
1214 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1216 release_ioinfo(info
);
1217 return ofs
.QuadPart
;
1219 release_ioinfo(info
);
1220 TRACE(":error-last error (%d)\n",GetLastError());
1221 msvcrt_set_errno(GetLastError());
1225 /*********************************************************************
1228 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1230 return MSVCRT__lseeki64(fd
, offset
, whence
);
1233 /*********************************************************************
1234 * _lock_file (MSVCRT.@)
1236 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1238 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1239 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1241 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1244 /*********************************************************************
1245 * _unlock_file (MSVCRT.@)
1247 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1249 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1250 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1252 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1255 /*********************************************************************
1256 * _locking (MSVCRT.@)
1258 * This is untested; the underlying LockFile doesn't work yet.
1260 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1262 ioinfo
*info
= get_ioinfo(fd
);
1266 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1267 if (info
->handle
== INVALID_HANDLE_VALUE
)
1269 release_ioinfo(info
);
1273 if (mode
< 0 || mode
> 4)
1275 release_ioinfo(info
);
1276 *MSVCRT__errno() = MSVCRT_EINVAL
;
1280 TRACE(":fd (%d) by 0x%08x mode %s\n",
1281 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1282 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1283 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1284 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1285 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1288 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1290 release_ioinfo(info
);
1291 FIXME ("Seek failed\n");
1292 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1295 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1298 ret
= 1; /* just to satisfy gcc */
1301 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1306 else if (mode
== MSVCRT__LK_UNLCK
)
1307 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1309 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1310 /* FIXME - what about error settings? */
1311 release_ioinfo(info
);
1312 return ret
? 0 : -1;
1315 /*********************************************************************
1316 * _fseeki64 (MSVCRT.@)
1318 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1322 MSVCRT__lock_file(file
);
1323 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1324 MSVCRT__unlock_file(file
);
1329 /*********************************************************************
1330 * _fseeki64_nolock (MSVCRT.@)
1332 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1336 /* Flush output if needed */
1337 if(file
->_flag
& MSVCRT__IOWRT
)
1338 msvcrt_flush_buffer(file
);
1340 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1342 offset
+= MSVCRT__ftelli64_nolock(file
);
1345 /* Discard buffered input */
1347 file
->_ptr
= file
->_base
;
1348 /* Reset direction of i/o */
1349 if(file
->_flag
& MSVCRT__IORW
) {
1350 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1352 /* Clear end of file flag */
1353 file
->_flag
&= ~MSVCRT__IOEOF
;
1354 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1359 /*********************************************************************
1362 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1364 return MSVCRT__fseeki64( file
, offset
, whence
);
1367 /*********************************************************************
1368 * _fseek_nolock (MSVCRT.@)
1370 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1372 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1375 /*********************************************************************
1376 * _chsize_s (MSVCRT.@)
1378 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1384 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1386 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1389 info
= get_ioinfo(fd
);
1390 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1392 /* save the current file pointer */
1393 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1396 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1399 ret
= SetEndOfFile(info
->handle
);
1400 if (!ret
) msvcrt_set_errno(GetLastError());
1403 /* restore the file pointer */
1404 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1408 release_ioinfo(info
);
1409 return ret
? 0 : *MSVCRT__errno();
1412 /*********************************************************************
1413 * _chsize (MSVCRT.@)
1415 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1417 /* _chsize_s returns errno on failure but _chsize should return -1 */
1418 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1421 /*********************************************************************
1422 * clearerr (MSVCRT.@)
1424 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1426 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1428 MSVCRT__lock_file(file
);
1429 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1430 MSVCRT__unlock_file(file
);
1433 /*********************************************************************
1436 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1438 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1440 MSVCRT__lock_file(file
);
1441 MSVCRT__fseek_nolock(file
, 0L, SEEK_SET
);
1442 MSVCRT_clearerr(file
);
1443 MSVCRT__unlock_file(file
);
1446 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1448 int plus
= strchrW(mode
, '+') != NULL
;
1450 TRACE("%s\n", debugstr_w(mode
));
1452 while(*mode
== ' ') mode
++;
1457 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1458 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1461 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1462 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1465 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1466 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1469 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1473 *stream_flags
|= MSVCRT__commode
;
1475 while (*mode
&& *mode
!=',')
1479 *open_flags
|= MSVCRT__O_BINARY
;
1480 *open_flags
&= ~MSVCRT__O_TEXT
;
1483 *open_flags
|= MSVCRT__O_TEXT
;
1484 *open_flags
&= ~MSVCRT__O_BINARY
;
1487 *open_flags
|= MSVCRT__O_TEMPORARY
;
1490 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1493 *stream_flags
|= MSVCRT__IOCOMMIT
;
1496 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1499 *open_flags
|= MSVCRT__O_NOINHERIT
;
1508 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1511 ERR("incorrect mode flag: %c\n", mode
[-1]);
1517 static const WCHAR ccs
[] = {'c','c','s'};
1518 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1519 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1520 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1523 while(*mode
== ' ') mode
++;
1524 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1526 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1527 while(*mode
== ' ') mode
++;
1528 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1531 while(*mode
== ' ') mode
++;
1533 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1535 *open_flags
|= MSVCRT__O_U8TEXT
;
1536 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1538 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1540 *open_flags
|= MSVCRT__O_U16TEXT
;
1541 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1543 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1545 *open_flags
|= MSVCRT__O_WTEXT
;
1546 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1550 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1554 while(*mode
== ' ') mode
++;
1557 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1562 /*********************************************************************
1563 * _fdopen (MSVCRT.@)
1565 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1568 MSVCRT_wchar_t
*modeW
= NULL
;
1570 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1572 ret
= MSVCRT__wfdopen(fd
, modeW
);
1578 /*********************************************************************
1579 * _wfdopen (MSVCRT.@)
1581 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1583 int open_flags
, stream_flags
;
1586 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1589 if (!(file
= msvcrt_alloc_fp()))
1591 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1596 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1602 /*********************************************************************
1603 * _filelength (MSVCRT.@)
1605 LONG CDECL
MSVCRT__filelength(int fd
)
1607 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1610 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1613 if (endPos
!= curPos
)
1614 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1621 /*********************************************************************
1622 * _filelengthi64 (MSVCRT.@)
1624 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1626 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1629 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1632 if (endPos
!= curPos
)
1633 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1640 /*********************************************************************
1641 * _fileno (MSVCRT.@)
1643 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1645 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1649 /*********************************************************************
1650 * _fstat64 (MSVCRT.@)
1652 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1654 ioinfo
*info
= get_ioinfo(fd
);
1657 BY_HANDLE_FILE_INFORMATION hfi
;
1659 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1660 if (info
->handle
== INVALID_HANDLE_VALUE
)
1662 release_ioinfo(info
);
1668 WARN(":failed-NULL buf\n");
1669 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1670 release_ioinfo(info
);
1674 memset(&hfi
, 0, sizeof(hfi
));
1675 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1676 type
= GetFileType(info
->handle
);
1677 if (type
== FILE_TYPE_PIPE
)
1679 buf
->st_dev
= buf
->st_rdev
= fd
;
1680 buf
->st_mode
= S_IFIFO
;
1683 else if (type
== FILE_TYPE_CHAR
)
1685 buf
->st_dev
= buf
->st_rdev
= fd
;
1686 buf
->st_mode
= S_IFCHR
;
1689 else /* FILE_TYPE_DISK etc. */
1691 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1693 WARN(":failed-last error (%d)\n",GetLastError());
1694 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1695 release_ioinfo(info
);
1698 buf
->st_mode
= S_IFREG
| 0444;
1699 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1700 buf
->st_mode
|= 0222;
1701 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1702 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1704 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1705 buf
->st_mtime
= buf
->st_ctime
= dw
;
1706 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1708 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1710 release_ioinfo(info
);
1714 /*********************************************************************
1715 * _fstati64 (MSVCRT.@)
1717 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1720 struct MSVCRT__stat64 buf64
;
1722 ret
= MSVCRT__fstat64(fd
, &buf64
);
1724 msvcrt_stat64_to_stati64(&buf64
, buf
);
1728 /*********************************************************************
1731 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1733 struct MSVCRT__stat64 buf64
;
1735 ret
= MSVCRT__fstat64(fd
, &buf64
);
1737 msvcrt_stat64_to_stat(&buf64
, buf
);
1741 /*********************************************************************
1742 * _fstat32 (MSVCR80.@)
1744 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1747 struct MSVCRT__stat64 buf64
;
1749 ret
= MSVCRT__fstat64(fd
, &buf64
);
1751 msvcrt_stat64_to_stat32(&buf64
, buf
);
1755 /*********************************************************************
1756 * _fstat64i32 (MSVCR80.@)
1758 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1761 struct MSVCRT__stat64 buf64
;
1763 ret
= MSVCRT__fstat64(fd
, &buf64
);
1765 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1769 /*********************************************************************
1770 * _futime64 (MSVCRT.@)
1772 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1774 HANDLE hand
= msvcrt_fdtoh(fd
);
1779 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1784 time_to_filetime( t
->actime
, &at
);
1785 time_to_filetime( t
->modtime
, &wt
);
1788 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1790 msvcrt_set_errno(GetLastError());
1796 /*********************************************************************
1797 * _futime32 (MSVCRT.@)
1799 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1803 struct MSVCRT___utimbuf64 t64
;
1804 t64
.actime
= t
->actime
;
1805 t64
.modtime
= t
->modtime
;
1806 return _futime64( fd
, &t64
);
1809 return _futime64( fd
, NULL
);
1812 /*********************************************************************
1813 * _futime (MSVCRT.@)
1816 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1818 return _futime64( fd
, t
);
1821 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1823 return _futime32( fd
, t
);
1827 /*********************************************************************
1828 * _get_osfhandle (MSVCRT.@)
1830 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1832 HANDLE hand
= msvcrt_fdtoh(fd
);
1833 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1835 return (MSVCRT_intptr_t
)hand
;
1838 /*********************************************************************
1839 * _mktemp_s (MSVCRT.@)
1841 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1845 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1846 return MSVCRT_EINVAL
;
1848 for(len
=0; len
<size
; len
++)
1851 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1854 return MSVCRT_EINVAL
;
1857 for(xno
=1; xno
<=6; xno
++)
1858 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1859 return MSVCRT_EINVAL
;
1861 id
= GetCurrentProcessId();
1862 for(xno
=1; xno
<6; xno
++) {
1863 pattern
[len
-xno
] = id
%10 + '0';
1867 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1868 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1873 *MSVCRT__errno() = MSVCRT_EEXIST
;
1874 return MSVCRT_EEXIST
;
1877 /*********************************************************************
1878 * _mktemp (MSVCRT.@)
1880 char * CDECL
MSVCRT__mktemp(char *pattern
)
1883 char *retVal
= pattern
;
1891 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1895 id
= GetCurrentProcessId();
1899 int tempNum
= id
/ 10;
1900 *pattern
-- = id
- (tempNum
* 10) + '0';
1906 *pattern
= letter
++;
1907 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1909 } while(letter
<= 'z');
1913 /*********************************************************************
1914 * _wmktemp_s (MSVCRT.@)
1916 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1920 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1921 return MSVCRT_EINVAL
;
1923 for(len
=0; len
<size
; len
++)
1926 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1929 return MSVCRT_EINVAL
;
1932 for(xno
=1; xno
<=6; xno
++)
1933 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1934 return MSVCRT_EINVAL
;
1936 id
= GetCurrentProcessId();
1937 for(xno
=1; xno
<6; xno
++) {
1938 pattern
[len
-xno
] = id
%10 + '0';
1942 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1943 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1948 *MSVCRT__errno() = MSVCRT_EEXIST
;
1949 return MSVCRT_EEXIST
;
1952 /*********************************************************************
1953 * _wmktemp (MSVCRT.@)
1955 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1958 MSVCRT_wchar_t
*retVal
= pattern
;
1960 MSVCRT_wchar_t letter
= 'a';
1966 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1970 id
= GetCurrentProcessId();
1974 int tempNum
= id
/ 10;
1975 *pattern
-- = id
- (tempNum
* 10) + '0';
1981 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1983 *pattern
= letter
++;
1984 } while(letter
!= '|');
1988 static unsigned split_oflags(unsigned oflags
)
1991 unsigned unsupp
; /* until we support everything */
1993 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1994 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1995 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1996 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1997 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1998 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1999 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2000 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2001 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2003 if ((unsupp
= oflags
& ~(
2004 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2005 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2006 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2007 MSVCRT__O_NOINHERIT
|
2008 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2009 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2011 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2016 /*********************************************************************
2019 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2022 SECURITY_ATTRIBUTES sa
;
2023 HANDLE readHandle
, writeHandle
;
2027 *MSVCRT__errno() = MSVCRT_EINVAL
;
2031 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2032 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2033 sa
.lpSecurityDescriptor
= NULL
;
2034 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2036 unsigned int wxflags
= split_oflags(textmode
);
2040 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2044 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2052 MSVCRT__close(pfds
[0]);
2053 CloseHandle(writeHandle
);
2054 *MSVCRT__errno() = MSVCRT_EMFILE
;
2059 CloseHandle(readHandle
);
2060 CloseHandle(writeHandle
);
2061 *MSVCRT__errno() = MSVCRT_EMFILE
;
2066 msvcrt_set_errno(GetLastError());
2071 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2073 char bom
[sizeof(utf8_bom
)];
2076 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2078 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2081 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2082 oflags
|= MSVCRT__O_U8TEXT
;
2083 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2085 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2086 oflags
|= MSVCRT__O_U16TEXT
;
2088 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2094 /*********************************************************************
2095 * _wsopen_s (MSVCRT.@)
2097 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2099 DWORD access
= 0, creation
= 0, attrib
;
2100 SECURITY_ATTRIBUTES sa
;
2101 DWORD sharing
, type
;
2105 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2106 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2108 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2111 wxflag
= split_oflags(oflags
);
2112 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2114 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2115 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2116 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2119 if (oflags
& MSVCRT__O_CREAT
)
2121 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2122 FIXME(": pmode 0x%04x ignored\n", pmode
);
2124 WARN(": pmode 0x%04x ignored\n", pmode
);
2126 if (oflags
& MSVCRT__O_EXCL
)
2127 creation
= CREATE_NEW
;
2128 else if (oflags
& MSVCRT__O_TRUNC
)
2129 creation
= CREATE_ALWAYS
;
2131 creation
= OPEN_ALWAYS
;
2133 else /* no MSVCRT__O_CREAT */
2135 if (oflags
& MSVCRT__O_TRUNC
)
2136 creation
= TRUNCATE_EXISTING
;
2138 creation
= OPEN_EXISTING
;
2143 case MSVCRT__SH_DENYRW
:
2146 case MSVCRT__SH_DENYWR
:
2147 sharing
= FILE_SHARE_READ
;
2149 case MSVCRT__SH_DENYRD
:
2150 sharing
= FILE_SHARE_WRITE
;
2152 case MSVCRT__SH_DENYNO
:
2153 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2156 ERR( "Unhandled shflags 0x%x\n", shflags
);
2157 return MSVCRT_EINVAL
;
2159 attrib
= FILE_ATTRIBUTE_NORMAL
;
2161 if (oflags
& MSVCRT__O_TEMPORARY
)
2163 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2165 sharing
|= FILE_SHARE_DELETE
;
2168 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2169 sa
.lpSecurityDescriptor
= NULL
;
2170 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2172 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2173 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2174 && !(access
&GENERIC_READ
))
2176 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2177 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2178 if (hand
!= INVALID_HANDLE_VALUE
)
2180 oflags
= check_bom(hand
, oflags
, FALSE
);
2184 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2187 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2188 if (hand
== INVALID_HANDLE_VALUE
) {
2189 WARN(":failed-last error (%d)\n",GetLastError());
2190 msvcrt_set_errno(GetLastError());
2191 return *MSVCRT__errno();
2194 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2196 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2197 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2198 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2200 if (oflags
& MSVCRT__O_U8TEXT
)
2202 DWORD written
= 0, tmp
;
2204 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2205 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2207 if (written
!= sizeof(utf8_bom
)) {
2208 WARN("error writing BOM\n");
2210 msvcrt_set_errno(GetLastError());
2211 return *MSVCRT__errno();
2216 DWORD written
= 0, tmp
;
2218 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2219 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2221 if (written
!= sizeof(utf16_bom
))
2223 WARN("error writing BOM\n");
2225 msvcrt_set_errno(GetLastError());
2226 return *MSVCRT__errno();
2230 else if (access
& GENERIC_READ
)
2231 oflags
= check_bom(hand
, oflags
, TRUE
);
2234 type
= GetFileType(hand
);
2235 if (type
== FILE_TYPE_CHAR
)
2237 else if (type
== FILE_TYPE_PIPE
)
2240 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2242 return *MSVCRT__errno();
2244 if (oflags
& MSVCRT__O_WTEXT
)
2245 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2246 else if (oflags
& MSVCRT__O_U16TEXT
)
2247 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2248 else if (oflags
& MSVCRT__O_U8TEXT
)
2249 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2251 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2255 /*********************************************************************
2256 * _wsopen (MSVCRT.@)
2258 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2263 if (oflags
& MSVCRT__O_CREAT
)
2267 __ms_va_start(ap
, shflags
);
2268 pmode
= va_arg(ap
, int);
2274 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2278 /*********************************************************************
2279 * _sopen_s (MSVCRT.@)
2281 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2283 MSVCRT_wchar_t
*pathW
;
2286 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2287 return MSVCRT_EINVAL
;
2289 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2290 return MSVCRT_EINVAL
;
2292 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2297 /*********************************************************************
2300 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2305 if (oflags
& MSVCRT__O_CREAT
)
2309 __ms_va_start(ap
, shflags
);
2310 pmode
= va_arg(ap
, int);
2316 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2320 /*********************************************************************
2323 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2327 if (flags
& MSVCRT__O_CREAT
)
2330 __ms_va_start(ap
, flags
);
2331 pmode
= va_arg(ap
, int);
2333 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2336 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2339 /*********************************************************************
2342 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2346 if (flags
& MSVCRT__O_CREAT
)
2349 __ms_va_start(ap
, flags
);
2350 pmode
= va_arg(ap
, int);
2352 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2355 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2358 /*********************************************************************
2361 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2363 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2364 return MSVCRT__open(path
, usedFlags
);
2367 /*********************************************************************
2368 * _wcreat (MSVCRT.@)
2370 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2372 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2373 return MSVCRT__wopen(path
, usedFlags
);
2376 /*********************************************************************
2377 * _open_osfhandle (MSVCRT.@)
2379 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2384 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2385 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2386 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2387 * text - it never sets MSVCRT__O_BINARY.
2389 /* don't let split_oflags() decide the mode if no mode is passed */
2390 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2391 oflags
|= MSVCRT__O_BINARY
;
2393 flags
= GetFileType((HANDLE
)handle
);
2394 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2396 msvcrt_set_errno(GetLastError());
2400 if (flags
== FILE_TYPE_CHAR
)
2402 else if (flags
== FILE_TYPE_PIPE
)
2406 flags
|= split_oflags(oflags
);
2408 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2409 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2413 /*********************************************************************
2416 int CDECL
MSVCRT__rmtmp(void)
2418 int num_removed
= 0, i
;
2422 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2423 file
= msvcrt_get_file(i
);
2425 if (file
->_tmpfname
)
2427 MSVCRT_fclose(file
);
2434 TRACE(":removed (%d) temp files\n",num_removed
);
2438 static inline int get_utf8_char_len(char ch
)
2440 if((ch
&0xf8) == 0xf0)
2442 else if((ch
&0xf0) == 0xe0)
2444 else if((ch
&0xe0) == 0xc0)
2449 /*********************************************************************
2450 * (internal) read_utf8
2452 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2454 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2455 HANDLE hand
= fdinfo
->handle
;
2456 char min_buf
[4], *readbuf
, lookahead
;
2457 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2459 /* make the buffer big enough to hold at least one character */
2460 /* read bytes have to fit to output and lookahead buffers */
2462 readbuf_size
= count
< 4 ? 4 : count
;
2463 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2468 if(fdinfo
->lookahead
[0] != '\n') {
2469 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2470 fdinfo
->lookahead
[0] = '\n';
2472 if(fdinfo
->lookahead
[1] != '\n') {
2473 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2474 fdinfo
->lookahead
[1] = '\n';
2476 if(fdinfo
->lookahead
[2] != '\n') {
2477 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2478 fdinfo
->lookahead
[2] = '\n';
2483 /* NOTE: this case is broken in native dll, reading
2484 * sometimes fails when small buffer is passed
2487 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2488 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2489 fdinfo
->wxflag
|= WX_ATEOF
;
2492 msvcrt_set_errno(GetLastError());
2495 }else if(!num_read
) {
2496 fdinfo
->wxflag
|= WX_ATEOF
;
2502 char_len
= get_utf8_char_len(readbuf
[0]);
2504 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2508 if(readbuf
[0] == '\n')
2509 fdinfo
->wxflag
|= WX_READNL
;
2511 fdinfo
->wxflag
&= ~WX_READNL
;
2513 if(readbuf
[0] == 0x1a) {
2514 fdinfo
->wxflag
|= WX_ATEOF
;
2518 if(readbuf
[0] == '\r') {
2519 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2521 else if(lookahead
== '\n')
2525 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2526 fdinfo
->lookahead
[0] = lookahead
;
2528 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2533 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2534 msvcrt_set_errno(GetLastError());
2541 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2544 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2545 fdinfo
->wxflag
|= WX_ATEOF
;
2546 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2549 msvcrt_set_errno(GetLastError());
2550 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2553 }else if(!pos
&& !num_read
) {
2554 fdinfo
->wxflag
|= WX_ATEOF
;
2555 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2560 if(readbuf
[0] == '\n')
2561 fdinfo
->wxflag
|= WX_READNL
;
2563 fdinfo
->wxflag
&= ~WX_READNL
;
2565 /* Find first byte of last character (may be incomplete) */
2566 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2567 if((readbuf
[i
]&0xc0) != 0x80)
2569 char_len
= get_utf8_char_len(readbuf
[i
]);
2570 if(char_len
+i
<= pos
)
2573 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2575 fdinfo
->lookahead
[0] = readbuf
[i
];
2577 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2579 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2581 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2585 for(i
=0, j
=0; i
<pos
; i
++) {
2586 if(readbuf
[i
] == 0x1a) {
2587 fdinfo
->wxflag
|= WX_ATEOF
;
2591 /* strip '\r' if followed by '\n' */
2592 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2593 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2594 readbuf
[j
++] = '\r';
2595 }else if(lookahead
== '\n' && j
==0) {
2596 readbuf
[j
++] = '\n';
2598 if(lookahead
!= '\n')
2599 readbuf
[j
++] = '\r';
2601 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2602 fdinfo
->lookahead
[0] = lookahead
;
2604 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2606 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2607 readbuf
[j
++] = readbuf
[i
];
2612 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2613 msvcrt_set_errno(GetLastError());
2614 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2618 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2622 /*********************************************************************
2625 * When reading \r as last character in text mode, read() positions
2626 * the file pointer on the \r character while getc() goes on to
2629 static int read_i(int fd
, void *buf
, unsigned int count
)
2631 DWORD num_read
, utf16
;
2632 char *bufstart
= buf
;
2633 HANDLE hand
= msvcrt_fdtoh(fd
);
2634 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2639 if (fdinfo
->wxflag
& WX_ATEOF
) {
2640 TRACE("already at EOF, returning 0\n");
2643 /* Don't trace small reads, it gets *very* annoying */
2645 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2646 if (hand
== INVALID_HANDLE_VALUE
)
2648 *MSVCRT__errno() = MSVCRT_EBADF
;
2652 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2653 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2655 *MSVCRT__errno() = MSVCRT_EINVAL
;
2659 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2660 return read_utf8(fd
, buf
, count
);
2662 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2664 if (fdinfo
->lookahead
[0] != '\n')
2666 bufstart
[0] = fdinfo
->lookahead
[0];
2667 fdinfo
->lookahead
[0] = '\n';
2671 bufstart
[1] = fdinfo
->lookahead
[1];
2672 fdinfo
->lookahead
[1] = '\n';
2675 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2676 num_read
+= 1+utf16
;
2681 if(utf16
&& (num_read
&1))
2683 /* msvcr90 uses uninitialized value from the buffer in this case */
2684 /* msvcrt ignores additional data */
2685 ERR("got odd number of bytes in UTF16 mode\n");
2689 if (count
!= 0 && num_read
== 0)
2691 fdinfo
->wxflag
|= WX_ATEOF
;
2692 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2694 else if (fdinfo
->wxflag
& WX_TEXT
)
2698 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2699 fdinfo
->wxflag
|= WX_READNL
;
2701 fdinfo
->wxflag
&= ~WX_READNL
;
2703 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2705 /* in text mode, a ctrl-z signals EOF */
2706 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2708 fdinfo
->wxflag
|= WX_ATEOF
;
2709 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2713 /* in text mode, strip \r if followed by \n */
2714 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2719 lookahead
[1] = '\n';
2720 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2722 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2724 bufstart
[j
++] = '\n';
2725 if(utf16
) bufstart
[j
++] = 0;
2729 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2731 bufstart
[j
++] = '\r';
2732 if(utf16
) bufstart
[j
++] = 0;
2735 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2737 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2739 bufstart
[j
++] = '\n';
2740 if (utf16
) bufstart
[j
++] = 0;
2744 fdinfo
->lookahead
[0] = lookahead
[0];
2745 fdinfo
->lookahead
[1] = lookahead
[1];
2749 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2754 bufstart
[j
++] = '\r';
2755 if(utf16
) bufstart
[j
++] = 0;
2758 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2759 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2761 bufstart
[j
++] = bufstart
[i
];
2762 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2770 if (GetLastError() == ERROR_BROKEN_PIPE
)
2772 TRACE(":end-of-pipe\n");
2773 fdinfo
->wxflag
|= WX_ATEOF
;
2778 TRACE(":failed-last error (%d)\n",GetLastError());
2784 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2788 /*********************************************************************
2791 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2794 num_read
= read_i(fd
, buf
, count
);
2798 /*********************************************************************
2799 * _setmode (MSVCRT.@)
2801 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2803 int ret
= get_ioinfo_nolock(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2804 if(ret
==MSVCRT__O_TEXT
&& (get_ioinfo_nolock(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2805 ret
= MSVCRT__O_WTEXT
;
2807 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2808 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2809 *MSVCRT__errno() = MSVCRT_EINVAL
;
2813 if(mode
== MSVCRT__O_BINARY
) {
2814 get_ioinfo_nolock(fd
)->wxflag
&= ~WX_TEXT
;
2815 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2819 get_ioinfo_nolock(fd
)->wxflag
|= WX_TEXT
;
2820 if(mode
== MSVCRT__O_TEXT
)
2821 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2822 else if(mode
== MSVCRT__O_U8TEXT
)
2823 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2825 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2830 /*********************************************************************
2831 * _stat64 (MSVCRT.@)
2833 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2836 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2837 unsigned short mode
= ALL_S_IREAD
;
2840 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2842 plen
= strlen(path
);
2843 while (plen
&& path
[plen
-1]==' ')
2846 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2847 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2849 *MSVCRT__errno() = MSVCRT_ENOENT
;
2853 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2855 TRACE("failed (%d)\n",GetLastError());
2856 *MSVCRT__errno() = MSVCRT_ENOENT
;
2860 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2862 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2863 Bon 011120: This FIXME seems incorrect
2864 Also a letter as first char isn't enough to be classified
2867 if (isalpha(*path
)&& (*(path
+1)==':'))
2868 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2870 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2872 /* Dir, or regular file? */
2873 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2874 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2877 mode
|= MSVCRT__S_IFREG
;
2879 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2881 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2882 (tolower(path
[plen
-3]) << 16);
2883 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2884 mode
|= ALL_S_IEXEC
;
2888 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2889 mode
|= ALL_S_IWRITE
;
2891 buf
->st_mode
= mode
;
2893 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2894 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2896 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2897 buf
->st_mtime
= buf
->st_ctime
= dw
;
2898 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2899 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2900 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2904 /*********************************************************************
2905 * _stati64 (MSVCRT.@)
2907 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2910 struct MSVCRT__stat64 buf64
;
2912 ret
= MSVCRT_stat64(path
, &buf64
);
2914 msvcrt_stat64_to_stati64(&buf64
, buf
);
2918 /*********************************************************************
2921 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2924 struct MSVCRT__stat64 buf64
;
2926 ret
= MSVCRT_stat64( path
, &buf64
);
2928 msvcrt_stat64_to_stat(&buf64
, buf
);
2932 /*********************************************************************
2933 * _stat32 (MSVCR100.@)
2935 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2938 struct MSVCRT__stat64 buf64
;
2940 ret
= MSVCRT_stat64(path
, &buf64
);
2942 msvcrt_stat64_to_stat32(&buf64
, buf
);
2946 /*********************************************************************
2947 * _stat32i64 (MSVCR100.@)
2949 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2952 struct MSVCRT__stat64 buf64
;
2954 ret
= MSVCRT_stat64(path
, &buf64
);
2956 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2960 /*********************************************************************
2961 * _stat64i32 (MSVCR100.@)
2963 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2966 struct MSVCRT__stat64 buf64
;
2968 ret
= MSVCRT_stat64(path
, &buf64
);
2970 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2974 /*********************************************************************
2975 * _wstat64 (MSVCRT.@)
2977 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2980 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2981 unsigned short mode
= ALL_S_IREAD
;
2984 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2986 plen
= strlenW(path
);
2987 while (plen
&& path
[plen
-1]==' ')
2990 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2991 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2993 *MSVCRT__errno() = MSVCRT_ENOENT
;
2997 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2999 TRACE("failed (%d)\n",GetLastError());
3000 *MSVCRT__errno() = MSVCRT_ENOENT
;
3004 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3006 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3007 if (MSVCRT_iswalpha(*path
))
3008 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3010 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3012 /* Dir, or regular file? */
3013 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3014 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3017 mode
|= MSVCRT__S_IFREG
;
3019 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3021 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3022 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3023 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3024 mode
|= ALL_S_IEXEC
;
3028 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3029 mode
|= ALL_S_IWRITE
;
3031 buf
->st_mode
= mode
;
3033 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3034 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3036 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3037 buf
->st_mtime
= buf
->st_ctime
= dw
;
3038 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3039 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3040 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3044 /*********************************************************************
3045 * _wstati64 (MSVCRT.@)
3047 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3050 struct MSVCRT__stat64 buf64
;
3052 ret
= MSVCRT__wstat64(path
, &buf64
);
3054 msvcrt_stat64_to_stati64(&buf64
, buf
);
3058 /*********************************************************************
3061 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3064 struct MSVCRT__stat64 buf64
;
3066 ret
= MSVCRT__wstat64( path
, &buf64
);
3067 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3071 /*********************************************************************
3072 * _wstat32 (MSVCR100.@)
3074 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3077 struct MSVCRT__stat64 buf64
;
3079 ret
= MSVCRT__wstat64(path
, &buf64
);
3081 msvcrt_stat64_to_stat32(&buf64
, buf
);
3085 /*********************************************************************
3086 * _wstat32i64 (MSVCR100.@)
3088 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3091 struct MSVCRT__stat64 buf64
;
3093 ret
= MSVCRT__wstat64(path
, &buf64
);
3095 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3099 /*********************************************************************
3100 * _wstat64i32 (MSVCR100.@)
3102 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3105 struct MSVCRT__stat64 buf64
;
3107 ret
= MSVCRT__wstat64(path
, &buf64
);
3109 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3113 /*********************************************************************
3116 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3118 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3121 /*********************************************************************
3122 * _telli64 (MSVCRT.@)
3124 __int64 CDECL
_telli64(int fd
)
3126 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3129 /*********************************************************************
3130 * _tempnam (MSVCRT.@)
3132 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3134 char tmpbuf
[MAX_PATH
];
3135 const char *tmp_dir
= MSVCRT_getenv("TMP");
3137 if (tmp_dir
) dir
= tmp_dir
;
3139 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3140 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3142 TRACE("got name (%s)\n",tmpbuf
);
3143 DeleteFileA(tmpbuf
);
3144 return MSVCRT__strdup(tmpbuf
);
3146 TRACE("failed (%d)\n",GetLastError());
3150 /*********************************************************************
3151 * _wtempnam (MSVCRT.@)
3153 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3155 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3156 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3157 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3159 if (tmp_dir
) dir
= tmp_dir
;
3161 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3162 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3164 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3165 DeleteFileW(tmpbuf
);
3166 return MSVCRT__wcsdup(tmpbuf
);
3168 TRACE("failed (%d)\n",GetLastError());
3172 /*********************************************************************
3175 int CDECL
MSVCRT__umask(int umask
)
3177 int old_umask
= MSVCRT_umask
;
3178 TRACE("(%d)\n",umask
);
3179 MSVCRT_umask
= umask
;
3183 /*********************************************************************
3184 * _utime64 (MSVCRT.@)
3186 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3188 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3192 int retVal
= _futime64(fd
, t
);
3199 /*********************************************************************
3200 * _utime32 (MSVCRT.@)
3202 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3206 struct MSVCRT___utimbuf64 t64
;
3207 t64
.actime
= t
->actime
;
3208 t64
.modtime
= t
->modtime
;
3209 return _utime64( path
, &t64
);
3212 return _utime64( path
, NULL
);
3215 /*********************************************************************
3219 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3221 return _utime64( path
, t
);
3224 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3226 return _utime32( path
, t
);
3230 /*********************************************************************
3231 * _wutime64 (MSVCRT.@)
3233 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3235 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3239 int retVal
= _futime64(fd
, t
);
3246 /*********************************************************************
3247 * _wutime32 (MSVCRT.@)
3249 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3253 struct MSVCRT___utimbuf64 t64
;
3254 t64
.actime
= t
->actime
;
3255 t64
.modtime
= t
->modtime
;
3256 return _wutime64( path
, &t64
);
3259 return _wutime64( path
, NULL
);
3262 /*********************************************************************
3263 * _wutime (MSVCRT.@)
3266 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3268 return _wutime64( path
, t
);
3271 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3273 return _wutime32( path
, t
);
3277 /*********************************************************************
3280 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3283 ioinfo
*info
= get_ioinfo_nolock(fd
);
3284 HANDLE hand
= info
->handle
;
3286 /* Don't trace small writes, it gets *very* annoying */
3289 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3291 if (hand
== INVALID_HANDLE_VALUE
)
3293 *MSVCRT__errno() = MSVCRT_EBADF
;
3297 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3299 *MSVCRT__errno() = MSVCRT_EINVAL
;
3303 /* If appending, go to EOF */
3304 if (info
->wxflag
& WX_APPEND
)
3305 MSVCRT__lseek(fd
, 0, FILE_END
);
3307 if (!(info
->wxflag
& WX_TEXT
))
3309 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3310 && (num_written
== count
))
3312 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3313 hand
, GetLastError());
3314 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3318 unsigned int i
, j
, nr_lf
, size
;
3321 const char *s
= buf
, *buf_start
= buf
;
3323 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3325 /* find number of \n */
3326 for (nr_lf
=0, i
=0; i
<count
; i
++)
3332 if ((q
= p
= MSVCRT_malloc(size
)))
3334 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3343 FIXME("Malloc failed\n");
3355 else if (info
->exflag
& EF_UTF16
)
3357 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3358 if (s
[i
]=='\n' && s
[i
+1]==0)
3363 if ((q
= p
= MSVCRT_malloc(size
)))
3365 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3367 if (s
[i
]=='\n' && s
[i
+1]==0)
3378 FIXME("Malloc failed\n");
3394 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3395 if (s
[i
]=='\n' && s
[i
+1]==0)
3398 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3400 msvcrt_set_errno(GetLastError());
3405 size
= conv_len
+nr_lf
;
3406 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3408 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3410 if (s
[i
]=='\n' && s
[i
+1]==0)
3418 q
= p
+count
+nr_lf
*2;
3419 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3420 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3424 FIXME("Malloc failed\n");
3431 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3435 if (num_written
!= size
)
3437 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3438 fd
, hand
, GetLastError(), num_written
);
3439 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3440 return s
- buf_start
;
3448 /*********************************************************************
3451 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3455 MSVCRT__lock_file(file
);
3456 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3457 if (len
== sizeof(val
)) {
3458 MSVCRT__unlock_file(file
);
3462 file
->_flag
|= MSVCRT__IOERR
;
3463 MSVCRT__unlock_file(file
);
3467 /*********************************************************************
3470 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3474 MSVCRT__lock_file(file
);
3475 ret
= MSVCRT__fclose_nolock(file
);
3476 MSVCRT__unlock_file(file
);
3481 /*********************************************************************
3482 * _fclose_nolock (MSVCRT.@)
3484 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3489 MSVCRT_free(file
->_tmpfname
);
3490 file
->_tmpfname
= NULL
;
3491 /* flush stdio buffers */
3492 if(file
->_flag
& MSVCRT__IOWRT
)
3493 MSVCRT__fflush_nolock(file
);
3494 if(file
->_flag
& MSVCRT__IOMYBUF
)
3495 MSVCRT_free(file
->_base
);
3497 r
=MSVCRT__close(file
->_file
);
3500 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3503 /*********************************************************************
3506 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3508 return file
->_flag
& MSVCRT__IOEOF
;
3511 /*********************************************************************
3514 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3516 return file
->_flag
& MSVCRT__IOERR
;
3519 /*********************************************************************
3520 * _filbuf (MSVCRT.@)
3522 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3526 if(file
->_flag
& MSVCRT__IOSTRG
)
3529 /* Allocate buffer if needed */
3530 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3531 msvcrt_alloc_buffer(file
);
3533 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3534 if(file
->_flag
& MSVCRT__IORW
)
3535 file
->_flag
|= MSVCRT__IOREAD
;
3540 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3542 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3543 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3549 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3551 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3557 file
->_ptr
= file
->_base
+1;
3558 c
= *(unsigned char *)file
->_base
;
3563 /*********************************************************************
3566 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3570 MSVCRT__lock_file(file
);
3571 ret
= MSVCRT__fgetc_nolock(file
);
3572 MSVCRT__unlock_file(file
);
3577 /*********************************************************************
3578 * _fgetc_nolock (MSVCRT.@)
3580 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3587 i
= (unsigned char *)file
->_ptr
++;
3590 j
= MSVCRT__filbuf(file
);
3595 /*********************************************************************
3596 * _fgetchar (MSVCRT.@)
3598 int CDECL
MSVCRT__fgetchar(void)
3600 return MSVCRT_fgetc(MSVCRT_stdin
);
3603 /*********************************************************************
3606 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3608 int cc
= MSVCRT_EOF
;
3609 char * buf_start
= s
;
3611 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3612 file
,file
->_file
,s
,size
);
3614 MSVCRT__lock_file(file
);
3616 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3621 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3623 TRACE(":nothing read\n");
3624 MSVCRT__unlock_file(file
);
3627 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3630 TRACE(":got %s\n", debugstr_a(buf_start
));
3631 MSVCRT__unlock_file(file
);
3635 /*********************************************************************
3638 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3642 MSVCRT__lock_file(file
);
3643 ret
= MSVCRT__fgetwc_nolock(file
);
3644 MSVCRT__unlock_file(file
);
3649 /*********************************************************************
3650 * _fgetwc_nolock (MSVCRT.@)
3652 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3657 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3658 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3661 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3662 ch
= MSVCRT__fgetc_nolock(file
);
3663 if(ch
== MSVCRT_EOF
) {
3670 char mbs
[MSVCRT_MB_LEN_MAX
];
3673 ch
= MSVCRT__fgetc_nolock(file
);
3674 if(ch
!= MSVCRT_EOF
) {
3676 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3677 ch
= MSVCRT__fgetc_nolock(file
);
3678 if(ch
!= MSVCRT_EOF
) {
3687 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3694 /*********************************************************************
3697 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3704 MSVCRT__lock_file(file
);
3705 for (j
=0; j
<sizeof(int); j
++) {
3706 k
= MSVCRT__fgetc_nolock(file
);
3707 if (k
== MSVCRT_EOF
) {
3708 file
->_flag
|= MSVCRT__IOEOF
;
3709 MSVCRT__unlock_file(file
);
3715 MSVCRT__unlock_file(file
);
3719 /*********************************************************************
3722 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3724 return MSVCRT_fgetwc(file
);
3727 /*********************************************************************
3728 * _fgetwchar (MSVCRT.@)
3730 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3732 return MSVCRT_fgetwc(MSVCRT_stdin
);
3735 /*********************************************************************
3736 * getwchar (MSVCRT.@)
3738 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3740 return MSVCRT__fgetwchar();
3743 /*********************************************************************
3746 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3748 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3749 MSVCRT_wchar_t
* buf_start
= s
;
3751 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3752 file
,file
->_file
,s
,size
);
3754 MSVCRT__lock_file(file
);
3756 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3761 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3763 TRACE(":nothing read\n");
3764 MSVCRT__unlock_file(file
);
3767 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3770 TRACE(":got %s\n", debugstr_w(buf_start
));
3771 MSVCRT__unlock_file(file
);
3775 /*********************************************************************
3776 * _flsbuf (MSVCRT.@)
3778 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3780 /* Flush output buffer */
3781 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3782 msvcrt_alloc_buffer(file
);
3784 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3785 if(file
->_flag
& MSVCRT__IORW
)
3786 file
->_flag
|= MSVCRT__IOWRT
;
3790 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3793 if(file
->_cnt
<= 0) {
3794 res
= msvcrt_flush_buffer(file
);
3797 file
->_cnt
=file
->_bufsiz
;
3805 /* set _cnt to 0 for unbuffered FILEs */
3807 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3810 file
->_flag
|= MSVCRT__IOERR
;
3815 /*********************************************************************
3818 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3822 MSVCRT__lock_file(file
);
3823 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3824 MSVCRT__unlock_file(file
);
3829 /*********************************************************************
3830 * _fwrite_nolock (MSVCRT.@)
3832 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3834 MSVCRT_size_t wrcnt
=size
* nmemb
;
3840 if(file
->_cnt
< 0) {
3841 WARN("negative file->_cnt value in %p\n", file
);
3842 file
->_flag
|= MSVCRT__IOERR
;
3844 } else if(file
->_cnt
) {
3845 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3846 memcpy(file
->_ptr
, ptr
, pcnt
);
3851 ptr
= (const char*)ptr
+ pcnt
;
3852 } else if((file
->_flag
& MSVCRT__IONBF
)
3853 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3854 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3858 if(file
->_flag
& MSVCRT__IONBF
)
3860 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3861 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3863 bufsiz
= file
->_bufsiz
;
3865 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3867 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3870 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3871 file
->_flag
|= MSVCRT__IOERR
;
3876 ptr
= (const char*)ptr
+ pcnt
;
3878 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3882 ptr
= (const char*)ptr
+ 1;
3886 return written
/ size
;
3889 /*********************************************************************
3892 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3896 MSVCRT__lock_file(file
);
3897 ret
= MSVCRT__fputwc_nolock(wc
, file
);
3898 MSVCRT__unlock_file(file
);
3903 /*********************************************************************
3904 * _fputwc_nolock (MSVCRT.@)
3906 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3908 MSVCRT_wchar_t mwc
=wc
;
3912 fdinfo
= get_ioinfo_nolock(file
->_file
);
3914 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3915 char buf
[MSVCRT_MB_LEN_MAX
];
3918 char_len
= MSVCRT_wctomb(buf
, mwc
);
3919 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
3923 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3932 /*********************************************************************
3933 * _fputwchar (MSVCRT.@)
3935 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3937 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3940 /*********************************************************************
3941 * _wfsopen (MSVCRT.@)
3943 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3946 int open_flags
, stream_flags
, fd
;
3948 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3950 /* map mode string to open() flags. "man fopen" for possibilities. */
3951 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3955 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3958 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3960 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3967 TRACE(":got (%p)\n",file
);
3968 if (fd
>= 0 && !file
)
3974 /*********************************************************************
3975 * _fsopen (MSVCRT.@)
3977 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3980 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3982 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3983 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3984 *MSVCRT__errno() = MSVCRT_EINVAL
;
3987 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3990 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3991 *MSVCRT__errno() = MSVCRT_EINVAL
;
3995 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4002 /*********************************************************************
4005 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4007 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4010 /*********************************************************************
4011 * fopen_s (MSVCRT.@)
4013 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4014 const char *filename
, const char *mode
)
4016 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4017 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4018 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4020 *pFile
= MSVCRT_fopen(filename
, mode
);
4023 return *MSVCRT__errno();
4027 /*********************************************************************
4028 * _wfopen (MSVCRT.@)
4030 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4032 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4035 /*********************************************************************
4036 * _wfopen_s (MSVCRT.@)
4038 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4039 const MSVCRT_wchar_t
*mode
)
4041 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4042 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4043 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4045 *pFile
= MSVCRT__wfopen(filename
, mode
);
4048 return *MSVCRT__errno();
4052 /*********************************************************************
4055 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4059 MSVCRT__lock_file(file
);
4060 ret
= MSVCRT__fputc_nolock(c
, file
);
4061 MSVCRT__unlock_file(file
);
4066 /*********************************************************************
4067 * _fputc_nolock (MSVCRT.@)
4069 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4078 res
= msvcrt_flush_buffer(file
);
4079 return res
? res
: c
;
4085 res
= MSVCRT__flsbuf(c
, file
);
4090 /*********************************************************************
4091 * _fputchar (MSVCRT.@)
4093 int CDECL
MSVCRT__fputchar(int c
)
4095 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4098 /*********************************************************************
4101 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4105 MSVCRT__lock_file(file
);
4106 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4107 MSVCRT__unlock_file(file
);
4112 /*********************************************************************
4113 * _fread_nolock (MSVCRT.@)
4115 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4117 MSVCRT_size_t rcnt
=size
* nmemb
;
4118 MSVCRT_size_t read
=0;
4119 MSVCRT_size_t pread
=0;
4124 /* first buffered data */
4126 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4127 memcpy(ptr
, file
->_ptr
, pcnt
);
4132 ptr
= (char*)ptr
+ pcnt
;
4133 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4134 if(file
->_flag
& MSVCRT__IORW
) {
4135 file
->_flag
|= MSVCRT__IOREAD
;
4141 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4142 msvcrt_alloc_buffer(file
);
4147 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4148 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4149 file
->_ptr
= file
->_base
;
4150 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4151 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4152 if (i
> 0 && i
< file
->_cnt
) {
4153 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4154 file
->_flag
&= ~MSVCRT__IOEOF
;
4157 memcpy(ptr
, file
->_ptr
, i
);
4161 } else if (rcnt
> INT_MAX
) {
4162 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4163 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4164 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4166 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4170 ptr
= (char *)ptr
+i
;
4171 /* expose feof condition in the flags
4172 * MFC tests file->_flag for feof, and doesn't call feof())
4174 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4175 file
->_flag
|= MSVCRT__IOEOF
;
4178 file
->_flag
|= MSVCRT__IOERR
;
4189 /*********************************************************************
4190 * fread_s (MSVCR80.@)
4192 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4193 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4197 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4199 memset(buf
, 0, buf_size
);
4202 if(!elem_size
|| !count
) return 0;
4204 MSVCRT__lock_file(stream
);
4205 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4206 MSVCRT__unlock_file(stream
);
4211 /*********************************************************************
4212 * _fread_nolock_s (MSVCR80.@)
4214 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4215 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4217 size_t bytes_left
, buf_pos
;
4219 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4221 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4223 memset(buf
, 0, buf_size
);
4226 if(!elem_size
|| !count
) return 0;
4227 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4228 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4230 bytes_left
= elem_size
*count
;
4233 if(stream
->_cnt
> 0) {
4234 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4236 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4237 memset(buf
, 0, buf_size
);
4241 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4245 int c
= MSVCRT__filbuf(stream
);
4250 if(!MSVCRT_CHECK_PMT_ERR(buf_size
-buf_pos
> 0, MSVCRT_ERANGE
)) {
4251 memset(buf
, 0, buf_size
);
4255 ((char*)buf
)[buf_pos
++] = c
;
4260 return buf_pos
/elem_size
;
4263 /*********************************************************************
4264 * _wfreopen (MSVCRT.@)
4267 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4269 int open_flags
, stream_flags
, fd
;
4271 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4274 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4278 MSVCRT_fclose(file
);
4279 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4281 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4283 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4293 /*********************************************************************
4294 * _wfreopen_s (MSVCRT.@)
4296 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4297 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4299 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4300 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4301 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4302 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4304 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4307 return *MSVCRT__errno();
4311 /*********************************************************************
4312 * freopen (MSVCRT.@)
4315 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4318 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4320 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4321 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4327 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4334 /*********************************************************************
4335 * freopen_s (MSVCRT.@)
4337 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4338 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4340 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4341 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4342 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4343 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4345 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4348 return *MSVCRT__errno();
4352 /*********************************************************************
4353 * fsetpos (MSVCRT.@)
4355 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4359 MSVCRT__lock_file(file
);
4360 /* Note that all this has been lifted 'as is' from fseek */
4361 if(file
->_flag
& MSVCRT__IOWRT
)
4362 msvcrt_flush_buffer(file
);
4364 /* Discard buffered input */
4366 file
->_ptr
= file
->_base
;
4368 /* Reset direction of i/o */
4369 if(file
->_flag
& MSVCRT__IORW
) {
4370 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4373 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4374 MSVCRT__unlock_file(file
);
4378 /*********************************************************************
4379 * _ftelli64 (MSVCRT.@)
4381 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4385 MSVCRT__lock_file(file
);
4386 ret
= MSVCRT__ftelli64_nolock(file
);
4387 MSVCRT__unlock_file(file
);
4392 /*********************************************************************
4393 * _ftelli64_nolock (MSVCRT.@)
4395 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4399 pos
= _telli64(file
->_file
);
4402 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4403 if(file
->_flag
& MSVCRT__IOWRT
) {
4404 pos
+= file
->_ptr
- file
->_base
;
4406 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4409 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4413 } else if(!file
->_cnt
) { /* nothing to do */
4414 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4418 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4419 for(i
=0; i
<file
->_cnt
; i
++)
4420 if(file
->_ptr
[i
] == '\n')
4426 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4429 pos
-= file
->_bufsiz
;
4430 pos
+= file
->_ptr
- file
->_base
;
4432 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4433 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4436 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4446 /*********************************************************************
4449 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4451 return MSVCRT__ftelli64(file
);
4454 /*********************************************************************
4455 * _ftell_nolock (MSVCRT.@)
4457 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4459 return MSVCRT__ftelli64_nolock(file
);
4462 /*********************************************************************
4463 * fgetpos (MSVCRT.@)
4465 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4467 *pos
= MSVCRT__ftelli64(file
);
4473 /*********************************************************************
4476 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4478 MSVCRT_size_t len
= strlen(s
);
4481 MSVCRT__lock_file(file
);
4482 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4483 MSVCRT__unlock_file(file
);
4487 /*********************************************************************
4490 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4492 MSVCRT_size_t i
, len
= strlenW(s
);
4496 MSVCRT__lock_file(file
);
4497 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4498 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4499 MSVCRT__unlock_file(file
);
4503 tmp_buf
= add_std_buffer(file
);
4504 for (i
=0; i
<len
; i
++) {
4505 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4506 if(tmp_buf
) remove_std_buffer(file
);
4507 MSVCRT__unlock_file(file
);
4512 if(tmp_buf
) remove_std_buffer(file
);
4513 MSVCRT__unlock_file(file
);
4517 /*********************************************************************
4518 * getchar (MSVCRT.@)
4520 int CDECL
MSVCRT_getchar(void)
4522 return MSVCRT_fgetc(MSVCRT_stdin
);
4525 /*********************************************************************
4528 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4530 return MSVCRT_fgetc(file
);
4533 /*********************************************************************
4536 char * CDECL
MSVCRT_gets(char *buf
)
4539 char * buf_start
= buf
;
4541 MSVCRT__lock_file(MSVCRT_stdin
);
4542 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4543 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4544 if(cc
!= '\r') *buf
++ = (char)cc
;
4548 TRACE("got '%s'\n", buf_start
);
4549 MSVCRT__unlock_file(MSVCRT_stdin
);
4553 /*********************************************************************
4556 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4559 MSVCRT_wchar_t
* ws
= buf
;
4561 MSVCRT__lock_file(MSVCRT_stdin
);
4562 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4563 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4566 *buf
++ = (MSVCRT_wchar_t
)cc
;
4570 TRACE("got %s\n", debugstr_w(ws
));
4571 MSVCRT__unlock_file(MSVCRT_stdin
);
4575 /*********************************************************************
4578 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4580 return MSVCRT_fputc(c
, file
);
4583 /*********************************************************************
4584 * putchar (MSVCRT.@)
4586 int CDECL
MSVCRT_putchar(int c
)
4588 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4591 /*********************************************************************
4592 * _putwch (MSVCRT.@)
4594 int CDECL
MSVCRT__putwch(int c
)
4596 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4599 /*********************************************************************
4602 int CDECL
MSVCRT_puts(const char *s
)
4604 MSVCRT_size_t len
= strlen(s
);
4607 MSVCRT__lock_file(MSVCRT_stdout
);
4608 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4609 MSVCRT__unlock_file(MSVCRT_stdout
);
4613 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4614 MSVCRT__unlock_file(MSVCRT_stdout
);
4618 /*********************************************************************
4621 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4623 static const MSVCRT_wchar_t nl
= '\n';
4624 MSVCRT_size_t len
= strlenW(s
);
4627 MSVCRT__lock_file(MSVCRT_stdout
);
4628 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4629 MSVCRT__unlock_file(MSVCRT_stdout
);
4633 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4634 MSVCRT__unlock_file(MSVCRT_stdout
);
4638 /*********************************************************************
4641 int CDECL
MSVCRT_remove(const char *path
)
4643 TRACE("(%s)\n",path
);
4644 if (DeleteFileA(path
))
4646 TRACE(":failed (%d)\n",GetLastError());
4647 msvcrt_set_errno(GetLastError());
4651 /*********************************************************************
4652 * _wremove (MSVCRT.@)
4654 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4656 TRACE("(%s)\n",debugstr_w(path
));
4657 if (DeleteFileW(path
))
4659 TRACE(":failed (%d)\n",GetLastError());
4660 msvcrt_set_errno(GetLastError());
4664 /*********************************************************************
4667 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4669 TRACE(":from %s to %s\n",oldpath
,newpath
);
4670 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4672 TRACE(":failed (%d)\n",GetLastError());
4673 msvcrt_set_errno(GetLastError());
4677 /*********************************************************************
4678 * _wrename (MSVCRT.@)
4680 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4682 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4683 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4685 TRACE(":failed (%d)\n",GetLastError());
4686 msvcrt_set_errno(GetLastError());
4690 /*********************************************************************
4691 * setvbuf (MSVCRT.@)
4693 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4695 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4696 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4697 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4699 MSVCRT__lock_file(file
);
4701 MSVCRT__fflush_nolock(file
);
4702 if(file
->_flag
& MSVCRT__IOMYBUF
)
4703 MSVCRT_free(file
->_base
);
4704 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4707 if(mode
== MSVCRT__IONBF
) {
4708 file
->_flag
|= MSVCRT__IONBF
;
4709 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4712 file
->_base
= file
->_ptr
= buf
;
4713 file
->_flag
|= MSVCRT__USERBUF
;
4714 file
->_bufsiz
= size
;
4716 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4719 MSVCRT__unlock_file(file
);
4723 file
->_flag
|= MSVCRT__IOMYBUF
;
4724 file
->_bufsiz
= size
;
4726 MSVCRT__unlock_file(file
);
4730 /*********************************************************************
4733 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4735 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4738 /*********************************************************************
4741 char * CDECL
MSVCRT_tmpnam(char *s
)
4748 thread_data_t
*data
= msvcrt_get_thread_data();
4750 if(!data
->tmpnam_buffer
)
4751 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4753 s
= data
->tmpnam_buffer
;
4756 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4757 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4758 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4760 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4761 memcpy(p
, tmpstr
, size
);
4763 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4764 GetLastError() == ERROR_FILE_NOT_FOUND
)
4770 /*********************************************************************
4771 * _wtmpnam (MSVCRT.@)
4773 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4775 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4776 MSVCRT_wchar_t tmpstr
[16];
4780 thread_data_t
*data
= msvcrt_get_thread_data();
4782 if(!data
->wtmpnam_buffer
)
4783 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4785 s
= data
->wtmpnam_buffer
;
4788 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4789 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4790 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4792 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4793 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4795 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4796 GetLastError() == ERROR_FILE_NOT_FOUND
)
4802 /*********************************************************************
4803 * tmpfile (MSVCRT.@)
4805 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4807 char *filename
= MSVCRT_tmpnam(NULL
);
4809 MSVCRT_FILE
* file
= NULL
;
4812 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4813 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4814 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4816 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4821 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4824 if(fd
!= -1 && !file
)
4830 /*********************************************************************
4831 * tmpfile_s (MSVCRT.@)
4833 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4835 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4837 *file
= MSVCRT_tmpfile();
4841 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4843 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4846 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4850 MSVCRT__lock_file(file
);
4852 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4853 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4854 MSVCRT__unlock_file(file
);
4858 for(i
=0; i
<len
; i
++) {
4859 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
4860 MSVCRT__unlock_file(file
);
4865 MSVCRT__unlock_file(file
);
4869 /*********************************************************************
4870 * vfprintf (MSVCRT.@)
4872 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4877 MSVCRT__lock_file(file
);
4878 tmp_buf
= add_std_buffer(file
);
4879 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4880 if(tmp_buf
) remove_std_buffer(file
);
4881 MSVCRT__unlock_file(file
);
4886 /*********************************************************************
4887 * vfprintf_s (MSVCRT.@)
4889 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4894 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4896 MSVCRT__lock_file(file
);
4897 tmp_buf
= add_std_buffer(file
);
4898 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4899 if(tmp_buf
) remove_std_buffer(file
);
4900 MSVCRT__unlock_file(file
);
4905 /*********************************************************************
4906 * vfwprintf (MSVCRT.@)
4908 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4913 MSVCRT__lock_file(file
);
4914 tmp_buf
= add_std_buffer(file
);
4915 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4916 if(tmp_buf
) remove_std_buffer(file
);
4917 MSVCRT__unlock_file(file
);
4922 /*********************************************************************
4923 * vfwprintf_s (MSVCRT.@)
4925 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4930 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4932 MSVCRT__lock_file(file
);
4933 tmp_buf
= add_std_buffer(file
);
4934 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4935 if(tmp_buf
) remove_std_buffer(file
);
4936 MSVCRT__unlock_file(file
);
4941 /*********************************************************************
4942 * _vfwprintf_l (MSVCRT.@)
4944 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4945 MSVCRT__locale_t locale
, __ms_va_list valist
)
4950 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4952 MSVCRT__lock_file(file
);
4953 tmp_buf
= add_std_buffer(file
);
4954 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4955 if(tmp_buf
) remove_std_buffer(file
);
4956 MSVCRT__unlock_file(file
);
4961 /*********************************************************************
4962 * vprintf (MSVCRT.@)
4964 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4966 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4969 /*********************************************************************
4970 * vprintf_s (MSVCRT.@)
4972 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4974 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4977 /*********************************************************************
4978 * vwprintf (MSVCRT.@)
4980 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4982 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4985 /*********************************************************************
4986 * vwprintf_s (MSVCRT.@)
4988 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4990 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4993 /*********************************************************************
4994 * fprintf (MSVCRT.@)
4996 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4998 __ms_va_list valist
;
5000 __ms_va_start(valist
, format
);
5001 res
= MSVCRT_vfprintf(file
, format
, valist
);
5002 __ms_va_end(valist
);
5006 /*********************************************************************
5007 * fprintf_s (MSVCRT.@)
5009 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5011 __ms_va_list valist
;
5013 __ms_va_start(valist
, format
);
5014 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5015 __ms_va_end(valist
);
5019 /*********************************************************************
5020 * fwprintf (MSVCRT.@)
5022 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5024 __ms_va_list valist
;
5026 __ms_va_start(valist
, format
);
5027 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5028 __ms_va_end(valist
);
5032 /*********************************************************************
5033 * fwprintf_s (MSVCRT.@)
5035 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5037 __ms_va_list valist
;
5039 __ms_va_start(valist
, format
);
5040 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5041 __ms_va_end(valist
);
5045 /*********************************************************************
5046 * _fwprintf_l (MSVCRT.@)
5048 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5050 __ms_va_list valist
;
5052 __ms_va_start(valist
, locale
);
5053 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5054 __ms_va_end(valist
);
5058 /*********************************************************************
5061 int CDECL
MSVCRT_printf(const char *format
, ...)
5063 __ms_va_list valist
;
5065 __ms_va_start(valist
, format
);
5066 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5067 __ms_va_end(valist
);
5071 /*********************************************************************
5072 * printf_s (MSVCRT.@)
5074 int CDECL
MSVCRT_printf_s(const char *format
, ...)
5076 __ms_va_list valist
;
5078 __ms_va_start(valist
, format
);
5079 res
= MSVCRT_vprintf_s(format
, valist
);
5080 __ms_va_end(valist
);
5084 /*********************************************************************
5087 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5091 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5093 MSVCRT__lock_file(file
);
5094 ret
= MSVCRT__ungetc_nolock(c
, file
);
5095 MSVCRT__unlock_file(file
);
5100 /*********************************************************************
5101 * _ungetc_nolock (MSVCRT.@)
5103 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5105 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5107 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5108 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5111 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5112 && msvcrt_alloc_buffer(file
))
5113 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5116 if(file
->_ptr
>file
->_base
) {
5118 if(file
->_flag
& MSVCRT__IOSTRG
) {
5119 if(*file
->_ptr
!= c
) {
5127 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5128 file
->_flag
|= MSVCRT__IOREAD
;
5135 /*********************************************************************
5136 * ungetwc (MSVCRT.@)
5138 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5142 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5144 MSVCRT__lock_file(file
);
5145 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5146 MSVCRT__unlock_file(file
);
5151 /*********************************************************************
5152 * _ungetwc_nolock (MSVCRT.@)
5154 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5156 MSVCRT_wchar_t mwc
= wc
;
5158 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5159 if (wc
== MSVCRT_WEOF
)
5162 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5163 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5164 unsigned char * pp
= (unsigned char *)&mwc
;
5167 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5168 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5172 char mbs
[MSVCRT_MB_LEN_MAX
];
5175 len
= MSVCRT_wctomb(mbs
, mwc
);
5179 for(len
--; len
>=0; len
--) {
5180 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5188 /*********************************************************************
5189 * wprintf (MSVCRT.@)
5191 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5193 __ms_va_list valist
;
5195 __ms_va_start(valist
, format
);
5196 res
= MSVCRT_vwprintf(format
, valist
);
5197 __ms_va_end(valist
);
5201 /*********************************************************************
5202 * wprintf_s (MSVCRT.@)
5204 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5206 __ms_va_list valist
;
5208 __ms_va_start(valist
, format
);
5209 res
= MSVCRT_vwprintf_s(format
, valist
);
5210 __ms_va_end(valist
);
5214 /*********************************************************************
5215 * _getmaxstdio (MSVCRT.@)
5217 int CDECL
MSVCRT__getmaxstdio(void)
5219 return MSVCRT_max_streams
;
5222 /*********************************************************************
5223 * _setmaxstdio (MSVCRT.@)
5225 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5227 TRACE("%d\n", newmax
);
5229 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5232 MSVCRT_max_streams
= newmax
;
5233 return MSVCRT_max_streams
;