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
68 #define WX_NOSEEK 0x40
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 /* ioinfo structure size is different in msvcrXX.dll's */
90 CRITICAL_SECTION crit
;
96 BOOL utf8translations
;
104 /*********************************************************************
105 * __pioinfo (MSVCRT.@)
106 * array of pointers to ioinfo arrays [32]
108 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
110 /*********************************************************************
111 * __badioinfo (MSVCRT.@)
113 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
115 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
116 static int MSVCRT_fdend
= 3; /* highest allocated fd */
120 CRITICAL_SECTION crit
;
123 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
124 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
125 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
127 /* INTERNAL: process umask */
128 static int MSVCRT_umask
= 0;
130 /* INTERNAL: static data for tmpnam and _wtmpname functions */
131 static int tmpnam_unique
;
133 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
134 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
135 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
136 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
138 #define TOUL(x) (ULONGLONG)(x)
139 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
140 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
141 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
142 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
144 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
145 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
146 * and MSVCRT_stream_idx, from race conditions.
147 * It doesn't protect against race conditions manipulating the underlying files
148 * or flags; doing so would probably be better accomplished with per-file
149 * protection, rather than locking the whole table for every change.
151 static CRITICAL_SECTION MSVCRT_file_cs
;
152 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
154 0, 0, &MSVCRT_file_cs
,
155 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
156 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
158 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
159 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
160 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
162 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
164 buf
->st_dev
= buf64
->st_dev
;
165 buf
->st_ino
= buf64
->st_ino
;
166 buf
->st_mode
= buf64
->st_mode
;
167 buf
->st_nlink
= buf64
->st_nlink
;
168 buf
->st_uid
= buf64
->st_uid
;
169 buf
->st_gid
= buf64
->st_gid
;
170 buf
->st_rdev
= buf64
->st_rdev
;
171 buf
->st_size
= buf64
->st_size
;
172 buf
->st_atime
= buf64
->st_atime
;
173 buf
->st_mtime
= buf64
->st_mtime
;
174 buf
->st_ctime
= buf64
->st_ctime
;
177 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
179 buf
->st_dev
= buf64
->st_dev
;
180 buf
->st_ino
= buf64
->st_ino
;
181 buf
->st_mode
= buf64
->st_mode
;
182 buf
->st_nlink
= buf64
->st_nlink
;
183 buf
->st_uid
= buf64
->st_uid
;
184 buf
->st_gid
= buf64
->st_gid
;
185 buf
->st_rdev
= buf64
->st_rdev
;
186 buf
->st_size
= buf64
->st_size
;
187 buf
->st_atime
= buf64
->st_atime
;
188 buf
->st_mtime
= buf64
->st_mtime
;
189 buf
->st_ctime
= buf64
->st_ctime
;
192 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
194 buf
->st_dev
= buf64
->st_dev
;
195 buf
->st_ino
= buf64
->st_ino
;
196 buf
->st_mode
= buf64
->st_mode
;
197 buf
->st_nlink
= buf64
->st_nlink
;
198 buf
->st_uid
= buf64
->st_uid
;
199 buf
->st_gid
= buf64
->st_gid
;
200 buf
->st_rdev
= buf64
->st_rdev
;
201 buf
->st_size
= buf64
->st_size
;
202 buf
->st_atime
= buf64
->st_atime
;
203 buf
->st_mtime
= buf64
->st_mtime
;
204 buf
->st_ctime
= buf64
->st_ctime
;
207 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
209 buf
->st_dev
= buf64
->st_dev
;
210 buf
->st_ino
= buf64
->st_ino
;
211 buf
->st_mode
= buf64
->st_mode
;
212 buf
->st_nlink
= buf64
->st_nlink
;
213 buf
->st_uid
= buf64
->st_uid
;
214 buf
->st_gid
= buf64
->st_gid
;
215 buf
->st_rdev
= buf64
->st_rdev
;
216 buf
->st_size
= buf64
->st_size
;
217 buf
->st_atime
= buf64
->st_atime
;
218 buf
->st_mtime
= buf64
->st_mtime
;
219 buf
->st_ctime
= buf64
->st_ctime
;
222 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
224 buf
->st_dev
= buf64
->st_dev
;
225 buf
->st_ino
= buf64
->st_ino
;
226 buf
->st_mode
= buf64
->st_mode
;
227 buf
->st_nlink
= buf64
->st_nlink
;
228 buf
->st_uid
= buf64
->st_uid
;
229 buf
->st_gid
= buf64
->st_gid
;
230 buf
->st_rdev
= buf64
->st_rdev
;
231 buf
->st_size
= buf64
->st_size
;
232 buf
->st_atime
= buf64
->st_atime
;
233 buf
->st_mtime
= buf64
->st_mtime
;
234 buf
->st_ctime
= buf64
->st_ctime
;
237 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
239 /* 1601 to 1970 is 369 years plus 89 leap days */
240 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
242 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
243 ft
->dwHighDateTime
= ticks
>> 32;
244 ft
->dwLowDateTime
= ticks
;
247 static inline ioinfo
* msvcrt_get_ioinfo(int fd
)
250 if(fd
< MSVCRT_MAX_FILES
)
251 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
253 return &MSVCRT___badioinfo
;
255 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
258 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
262 if(i
>= MSVCRT_max_streams
)
266 return &MSVCRT__iob
[i
];
268 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
270 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
271 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
272 ERR("out of memory\n");
273 *MSVCRT__errno() = MSVCRT_ENOMEM
;
277 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
279 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
284 static inline BOOL
msvcrt_is_valid_fd(int fd
)
286 return fd
>= 0 && fd
< MSVCRT_fdend
&& (msvcrt_get_ioinfo(fd
)->wxflag
& WX_OPEN
);
289 /* INTERNAL: Get the HANDLE for a fd
290 * This doesn't lock the table, because a failure will result in
291 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
292 * it returns a valid handle which is about to be closed, a subsequent call
293 * will fail, most likely in a sane way.
295 static HANDLE
msvcrt_fdtoh(int fd
)
297 if (!msvcrt_is_valid_fd(fd
))
299 WARN(":fd (%d) - no handle!\n",fd
);
300 *MSVCRT___doserrno() = 0;
301 *MSVCRT__errno() = MSVCRT_EBADF
;
302 return INVALID_HANDLE_VALUE
;
304 if (msvcrt_get_ioinfo(fd
)->handle
== INVALID_HANDLE_VALUE
)
305 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd
);
306 return msvcrt_get_ioinfo(fd
)->handle
;
309 /* INTERNAL: free a file entry fd */
310 static void msvcrt_free_fd(int fd
)
315 fdinfo
= msvcrt_get_ioinfo(fd
);
316 if(fdinfo
!= &MSVCRT___badioinfo
)
318 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
321 TRACE(":fd (%d) freed\n",fd
);
328 SetStdHandle(STD_INPUT_HANDLE
, 0);
331 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
334 SetStdHandle(STD_ERROR_HANDLE
, 0);
339 if (fd
== MSVCRT_fdend
- 1)
341 if (fd
< MSVCRT_fdstart
)
346 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
347 /* caller must hold the files lock */
348 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
352 if (fd
>= MSVCRT_MAX_FILES
)
354 WARN(":files exhausted!\n");
355 *MSVCRT__errno() = MSVCRT_ENFILE
;
359 fdinfo
= msvcrt_get_ioinfo(fd
);
360 if(fdinfo
== &MSVCRT___badioinfo
) {
363 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
364 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
365 WARN(":out of memory!\n");
366 *MSVCRT__errno() = MSVCRT_ENOMEM
;
370 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
371 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
373 fdinfo
= msvcrt_get_ioinfo(fd
);
376 fdinfo
->handle
= hand
;
377 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_NOSEEK
));
378 fdinfo
->lookahead
[0] = '\n';
379 fdinfo
->lookahead
[1] = '\n';
380 fdinfo
->lookahead
[2] = '\n';
383 /* locate next free slot */
384 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
385 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
387 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
388 msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
390 /* update last fd in use */
391 if (fd
>= MSVCRT_fdend
)
392 MSVCRT_fdend
= fd
+ 1;
393 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
397 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
398 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
399 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
405 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
406 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
411 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
412 ret
= msvcrt_set_fd(hand
, flag
, MSVCRT_fdstart
);
417 /* INTERNAL: Allocate a FILE* for an fd slot */
418 /* caller must hold the files lock */
419 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
424 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
426 file
= msvcrt_get_file(i
);
430 if (file
->_flag
== 0)
432 if (i
== MSVCRT_stream_idx
)
434 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
436 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
437 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
448 /* INTERNAL: initialize a FILE* from an open fd */
449 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
451 TRACE(":fd (%d) allocating FILE*\n",fd
);
452 if (!msvcrt_is_valid_fd(fd
))
454 WARN(":invalid fd %d\n",fd
);
455 *MSVCRT___doserrno() = 0;
456 *MSVCRT__errno() = MSVCRT_EBADF
;
459 memset(file
, 0, sizeof(*file
));
461 file
->_flag
= stream_flags
;
463 TRACE(":got FILE* (%p)\n",file
);
467 /* INTERNAL: Create an inheritance data block (for spawned process)
468 * The inheritance block is made of:
469 * 00 int nb of file descriptor (NBFD)
470 * 04 char file flags (wxflag): repeated for each fd
471 * 4+NBFD HANDLE file handle: repeated for each fd
473 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
480 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
481 *block
= MSVCRT_calloc(*size
, 1);
487 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
488 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
490 *(unsigned*)*block
= MSVCRT_fdend
;
491 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
493 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
494 fdinfo
= msvcrt_get_ioinfo(fd
);
495 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
497 *wxflag_ptr
= fdinfo
->wxflag
;
498 *handle_ptr
= fdinfo
->handle
;
503 *handle_ptr
= INVALID_HANDLE_VALUE
;
505 wxflag_ptr
++; handle_ptr
++;
510 /* INTERNAL: Set up all file descriptors,
511 * as well as default streams (stdin, stderr and stdout)
513 void msvcrt_init_io(void)
519 GetStartupInfoA(&si
);
520 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
526 count
= *(unsigned*)si
.lpReserved2
;
527 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
528 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
530 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
531 count
= min(count
, MSVCRT_MAX_FILES
);
532 for (i
= 0; i
< count
; i
++)
534 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
535 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
537 wxflag_ptr
++; handle_ptr
++;
539 MSVCRT_fdend
= max( 3, count
);
540 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
541 if (msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
544 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO
);
545 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
546 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
547 DWORD type
= GetFileType(h
);
549 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
550 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDIN_FILENO
);
553 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO
);
554 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
555 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
556 DWORD type
= GetFileType(h
);
558 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
559 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDOUT_FILENO
);
562 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO
);
563 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
564 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
565 DWORD type
= GetFileType(h
);
567 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
568 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDERR_FILENO
);
571 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO
)->handle
,
572 msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO
)->handle
,
573 msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO
)->handle
);
575 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
576 for (i
= 0; i
< 3; i
++)
578 /* FILE structs for stdin/out/err are static and never deleted */
579 MSVCRT__iob
[i
]._file
= i
;
580 MSVCRT__iob
[i
]._tmpfname
= NULL
;
581 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
583 MSVCRT_stream_idx
= 3;
586 /* INTERNAL: Flush stdio file buffer */
587 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
590 int cnt
=file
->_ptr
-file
->_base
;
591 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
592 file
->_flag
|= MSVCRT__IOERR
;
595 file
->_ptr
=file
->_base
;
596 file
->_cnt
=file
->_bufsiz
;
601 /*********************************************************************
604 int CDECL
MSVCRT__isatty(int fd
)
606 HANDLE hand
= msvcrt_fdtoh(fd
);
608 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
609 if (hand
== INVALID_HANDLE_VALUE
)
612 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
615 /* INTERNAL: Allocate stdio file buffer */
616 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
618 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
619 && MSVCRT__isatty(file
->_file
))
622 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
624 file
->_bufsiz
= MSVCRT_BUFSIZ
;
625 file
->_flag
|= MSVCRT__IOMYBUF
;
627 file
->_base
= (char*)(&file
->_charbuf
);
629 file
->_bufsiz
= sizeof(file
->_charbuf
);
631 file
->_ptr
= file
->_base
;
636 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
637 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
639 static char buffers
[2][MSVCRT_BUFSIZ
];
641 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
642 || !MSVCRT__isatty(file
->_file
) || file
->_bufsiz
)
645 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
646 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
650 /* INTERNAL: Removes temporary buffer from stdout or stderr */
651 /* Only call this function when add_std_buffer returned TRUE */
652 static void remove_std_buffer(MSVCRT_FILE
*file
)
654 msvcrt_flush_buffer(file
);
655 file
->_ptr
= file
->_base
= NULL
;
656 file
->_bufsiz
= file
->_cnt
= 0;
659 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
660 static int msvcrt_int_to_base32(int num
, char *str
)
675 *p
= (num
& 31) + '0';
677 *p
+= ('a' - '0' - 10);
684 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
685 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
700 *p
= (num
& 31) + '0';
702 *p
+= ('a' - '0' - 10);
709 /*********************************************************************
710 * __iob_func(MSVCRT.@)
712 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
714 return &MSVCRT__iob
[0];
717 /*********************************************************************
720 int CDECL
MSVCRT__access(const char *filename
, int mode
)
722 DWORD attr
= GetFileAttributesA(filename
);
724 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
726 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
728 msvcrt_set_errno(GetLastError());
731 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
733 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
739 /*********************************************************************
740 * _access_s (MSVCRT.@)
742 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
744 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
745 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
747 if (MSVCRT__access(filename
, mode
) == -1)
748 return *MSVCRT__errno();
752 /*********************************************************************
753 * _waccess (MSVCRT.@)
755 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
757 DWORD attr
= GetFileAttributesW(filename
);
759 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
761 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
763 msvcrt_set_errno(GetLastError());
766 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
768 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
774 /*********************************************************************
775 * _waccess_s (MSVCRT.@)
777 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
779 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
780 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
782 if (MSVCRT__waccess(filename
, mode
) == -1)
783 return *MSVCRT__errno();
787 /*********************************************************************
790 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
792 DWORD oldFlags
= GetFileAttributesA(path
);
794 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
796 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
797 oldFlags
| FILE_ATTRIBUTE_READONLY
;
799 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
802 msvcrt_set_errno(GetLastError());
806 /*********************************************************************
809 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
811 DWORD oldFlags
= GetFileAttributesW(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
|| SetFileAttributesW(path
, newFlags
))
821 msvcrt_set_errno(GetLastError());
825 /*********************************************************************
828 int CDECL
MSVCRT__unlink(const char *path
)
830 TRACE("%s\n",debugstr_a(path
));
831 if(DeleteFileA(path
))
833 TRACE("failed (%d)\n",GetLastError());
834 msvcrt_set_errno(GetLastError());
838 /*********************************************************************
839 * _wunlink (MSVCRT.@)
841 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
843 TRACE("(%s)\n",debugstr_w(path
));
844 if(DeleteFileW(path
))
846 TRACE("failed (%d)\n",GetLastError());
847 msvcrt_set_errno(GetLastError());
851 /*********************************************************************
854 int CDECL
MSVCRT__commit(int fd
)
856 HANDLE hand
= msvcrt_fdtoh(fd
);
858 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
859 if (hand
== INVALID_HANDLE_VALUE
)
862 if (!FlushFileBuffers(hand
))
864 if (GetLastError() == ERROR_INVALID_HANDLE
)
866 /* FlushFileBuffers fails for console handles
867 * so we ignore this error.
871 TRACE(":failed-last error (%d)\n",GetLastError());
872 msvcrt_set_errno(GetLastError());
879 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
880 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
882 /* INTERNAL: Flush all stream buffer */
883 static int msvcrt_flush_all_buffers(int mask
)
885 int i
, num_flushed
= 0;
889 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
890 file
= msvcrt_get_file(i
);
894 if(file
->_flag
& mask
) {
902 TRACE(":flushed (%d) handles\n",num_flushed
);
906 /*********************************************************************
907 * _flushall (MSVCRT.@)
909 int CDECL
MSVCRT__flushall(void)
911 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
914 /*********************************************************************
917 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
920 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
921 } else if(file
->_flag
& MSVCRT__IOWRT
) {
924 MSVCRT__lock_file(file
);
925 res
= msvcrt_flush_buffer(file
);
927 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
928 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
929 MSVCRT__unlock_file(file
);
932 } else if(file
->_flag
& MSVCRT__IOREAD
) {
933 MSVCRT__lock_file(file
);
935 file
->_ptr
= file
->_base
;
936 MSVCRT__unlock_file(file
);
943 /*********************************************************************
946 int CDECL
MSVCRT__close(int fd
)
952 hand
= msvcrt_fdtoh(fd
);
953 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
954 if (!msvcrt_is_valid_fd(fd
)) {
958 ret
= CloseHandle(hand
) ? 0 : -1;
960 WARN(":failed-last error (%d)\n",GetLastError());
961 msvcrt_set_errno(GetLastError());
969 /*********************************************************************
972 * MSDN isn't clear on this point, but the remarks for _pipe
973 * indicate file descriptors duplicated with _dup and _dup2 are always
976 int CDECL
MSVCRT__dup2(int od
, int nd
)
980 TRACE("(od=%d, nd=%d)\n", od
, nd
);
982 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
986 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od
)->handle
,
987 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
989 int wxflag
= msvcrt_get_ioinfo(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
991 if (msvcrt_is_valid_fd(nd
))
993 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
997 *MSVCRT__errno() = MSVCRT_EMFILE
;
1001 /* _dup2 returns 0, not nd, on success */
1008 msvcrt_set_errno(GetLastError());
1013 *MSVCRT__errno() = MSVCRT_EBADF
;
1020 /*********************************************************************
1023 int CDECL
MSVCRT__dup(int od
)
1028 fd
= MSVCRT_fdstart
;
1029 if (MSVCRT__dup2(od
, fd
) == 0)
1037 /*********************************************************************
1040 int CDECL
MSVCRT__eof(int fd
)
1042 DWORD curpos
,endpos
;
1043 LONG hcurpos
,hendpos
;
1044 HANDLE hand
= msvcrt_fdtoh(fd
);
1046 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1048 if (hand
== INVALID_HANDLE_VALUE
)
1051 if (msvcrt_get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1053 /* Otherwise we do it the hard way */
1054 hcurpos
= hendpos
= 0;
1055 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1056 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1058 if (curpos
== endpos
&& hcurpos
== hendpos
)
1060 /* FIXME: shouldn't WX_ATEOF be set here? */
1064 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1068 /*********************************************************************
1069 * _fcloseall (MSVCRT.@)
1071 int CDECL
MSVCRT__fcloseall(void)
1073 int num_closed
= 0, i
;
1077 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1078 file
= msvcrt_get_file(i
);
1080 if (file
->_flag
&& !MSVCRT_fclose(file
))
1085 TRACE(":closed (%d) handles\n",num_closed
);
1089 /* free everything on process exit */
1090 void msvcrt_free_io(void)
1096 MSVCRT__fcloseall();
1098 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1099 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1101 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1103 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1104 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1106 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1107 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1111 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1112 MSVCRT_free(MSVCRT_fstream
[i
]);
1114 DeleteCriticalSection(&MSVCRT_file_cs
);
1117 /*********************************************************************
1118 * _lseeki64 (MSVCRT.@)
1120 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1122 HANDLE hand
= msvcrt_fdtoh(fd
);
1125 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1126 if (hand
== INVALID_HANDLE_VALUE
)
1129 if (whence
< 0 || whence
> 2)
1131 *MSVCRT__errno() = MSVCRT_EINVAL
;
1135 TRACE(":fd (%d) to %s pos %s\n",
1136 fd
,wine_dbgstr_longlong(offset
),
1137 (whence
==SEEK_SET
)?"SEEK_SET":
1138 (whence
==SEEK_CUR
)?"SEEK_CUR":
1139 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1141 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1142 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1143 ofs
.QuadPart
= offset
;
1144 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1145 GetLastError() == ERROR_SUCCESS
)
1147 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_ATEOF
;
1148 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1150 return ofs
.QuadPart
;
1152 TRACE(":error-last error (%d)\n",GetLastError());
1153 msvcrt_set_errno(GetLastError());
1157 /*********************************************************************
1160 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1162 return MSVCRT__lseeki64(fd
, offset
, whence
);
1165 /*********************************************************************
1166 * _lock_file (MSVCRT.@)
1168 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1170 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1171 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1173 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1176 /*********************************************************************
1177 * _unlock_file (MSVCRT.@)
1179 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1181 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1182 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1184 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1187 /*********************************************************************
1188 * _locking (MSVCRT.@)
1190 * This is untested; the underlying LockFile doesn't work yet.
1192 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1196 HANDLE hand
= msvcrt_fdtoh(fd
);
1198 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1199 if (hand
== INVALID_HANDLE_VALUE
)
1202 if (mode
< 0 || mode
> 4)
1204 *MSVCRT__errno() = MSVCRT_EINVAL
;
1208 TRACE(":fd (%d) by 0x%08x mode %s\n",
1209 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1210 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1211 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1212 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1213 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1216 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1218 FIXME ("Seek failed\n");
1219 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1222 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1225 ret
= 1; /* just to satisfy gcc */
1228 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1233 else if (mode
== MSVCRT__LK_UNLCK
)
1234 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1236 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1237 /* FIXME - what about error settings? */
1238 return ret
? 0 : -1;
1241 /*********************************************************************
1242 * _fseeki64 (MSVCRT.@)
1244 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1248 MSVCRT__lock_file(file
);
1249 /* Flush output if needed */
1250 if(file
->_flag
& MSVCRT__IOWRT
)
1251 msvcrt_flush_buffer(file
);
1253 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1255 offset
+= MSVCRT__ftelli64(file
);
1258 /* Discard buffered input */
1260 file
->_ptr
= file
->_base
;
1261 /* Reset direction of i/o */
1262 if(file
->_flag
& MSVCRT__IORW
) {
1263 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1265 /* Clear end of file flag */
1266 file
->_flag
&= ~MSVCRT__IOEOF
;
1267 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1269 MSVCRT__unlock_file(file
);
1273 /*********************************************************************
1276 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1278 return MSVCRT__fseeki64( file
, offset
, whence
);
1281 /*********************************************************************
1282 * _chsize_s (MSVCRT.@)
1284 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1290 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1292 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1296 handle
= msvcrt_fdtoh(fd
);
1297 if (handle
!= INVALID_HANDLE_VALUE
)
1299 /* save the current file pointer */
1300 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1303 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1306 ret
= SetEndOfFile(handle
);
1307 if (!ret
) msvcrt_set_errno(GetLastError());
1310 /* restore the file pointer */
1311 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1316 return ret
? 0 : *MSVCRT__errno();
1319 /*********************************************************************
1320 * _chsize (MSVCRT.@)
1322 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1324 /* _chsize_s returns errno on failure but _chsize should return -1 */
1325 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1328 /*********************************************************************
1329 * clearerr (MSVCRT.@)
1331 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1333 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1335 MSVCRT__lock_file(file
);
1336 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1337 MSVCRT__unlock_file(file
);
1340 /*********************************************************************
1343 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1345 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1347 MSVCRT__lock_file(file
);
1348 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1349 MSVCRT_clearerr(file
);
1350 MSVCRT__unlock_file(file
);
1353 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1355 int plus
= strchrW(mode
, '+') != NULL
;
1357 TRACE("%s\n", debugstr_w(mode
));
1359 while(*mode
== ' ') mode
++;
1364 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1365 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1368 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1369 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1372 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1373 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1376 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1380 *stream_flags
|= MSVCRT__commode
;
1382 while (*mode
&& *mode
!=',')
1386 *open_flags
|= MSVCRT__O_BINARY
;
1387 *open_flags
&= ~MSVCRT__O_TEXT
;
1390 *open_flags
|= MSVCRT__O_TEXT
;
1391 *open_flags
&= ~MSVCRT__O_BINARY
;
1394 *open_flags
|= MSVCRT__O_TEMPORARY
;
1397 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1400 *stream_flags
|= MSVCRT__IOCOMMIT
;
1403 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1406 *open_flags
|= MSVCRT__O_NOINHERIT
;
1415 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1418 ERR("incorrect mode flag: %c\n", mode
[-1]);
1424 static const WCHAR ccs
[] = {'c','c','s'};
1425 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1426 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1427 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1430 while(*mode
== ' ') mode
++;
1431 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1433 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1434 while(*mode
== ' ') mode
++;
1435 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1438 while(*mode
== ' ') mode
++;
1440 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1442 *open_flags
|= MSVCRT__O_U8TEXT
;
1443 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1445 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1447 *open_flags
|= MSVCRT__O_U16TEXT
;
1448 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1450 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1452 *open_flags
|= MSVCRT__O_WTEXT
;
1453 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1457 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1461 while(*mode
== ' ') mode
++;
1464 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1469 /*********************************************************************
1470 * _fdopen (MSVCRT.@)
1472 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1475 MSVCRT_wchar_t
*modeW
= NULL
;
1477 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1479 ret
= MSVCRT__wfdopen(fd
, modeW
);
1485 /*********************************************************************
1486 * _wfdopen (MSVCRT.@)
1488 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1490 int open_flags
, stream_flags
;
1493 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1496 if (!(file
= msvcrt_alloc_fp()))
1498 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1503 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1509 /*********************************************************************
1510 * _filelength (MSVCRT.@)
1512 LONG CDECL
MSVCRT__filelength(int fd
)
1514 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1517 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1520 if (endPos
!= curPos
)
1521 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1528 /*********************************************************************
1529 * _filelengthi64 (MSVCRT.@)
1531 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1533 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1536 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1539 if (endPos
!= curPos
)
1540 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1547 /*********************************************************************
1548 * _fileno (MSVCRT.@)
1550 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1552 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1556 /*********************************************************************
1557 * _fstat64 (MSVCRT.@)
1559 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1563 BY_HANDLE_FILE_INFORMATION hfi
;
1564 HANDLE hand
= msvcrt_fdtoh(fd
);
1566 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1567 if (hand
== INVALID_HANDLE_VALUE
)
1572 WARN(":failed-NULL buf\n");
1573 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1577 memset(&hfi
, 0, sizeof(hfi
));
1578 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1579 type
= GetFileType(hand
);
1580 if (type
== FILE_TYPE_PIPE
)
1582 buf
->st_dev
= buf
->st_rdev
= fd
;
1583 buf
->st_mode
= S_IFIFO
;
1586 else if (type
== FILE_TYPE_CHAR
)
1588 buf
->st_dev
= buf
->st_rdev
= fd
;
1589 buf
->st_mode
= S_IFCHR
;
1592 else /* FILE_TYPE_DISK etc. */
1594 if (!GetFileInformationByHandle(hand
, &hfi
))
1596 WARN(":failed-last error (%d)\n",GetLastError());
1597 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1600 buf
->st_mode
= S_IFREG
| 0444;
1601 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1602 buf
->st_mode
|= 0222;
1603 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1604 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1606 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1607 buf
->st_mtime
= buf
->st_ctime
= dw
;
1608 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1610 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1615 /*********************************************************************
1616 * _fstati64 (MSVCRT.@)
1618 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1621 struct MSVCRT__stat64 buf64
;
1623 ret
= MSVCRT__fstat64(fd
, &buf64
);
1625 msvcrt_stat64_to_stati64(&buf64
, buf
);
1629 /*********************************************************************
1632 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1634 struct MSVCRT__stat64 buf64
;
1636 ret
= MSVCRT__fstat64(fd
, &buf64
);
1638 msvcrt_stat64_to_stat(&buf64
, buf
);
1642 /*********************************************************************
1643 * _fstat32 (MSVCR80.@)
1645 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1648 struct MSVCRT__stat64 buf64
;
1650 ret
= MSVCRT__fstat64(fd
, &buf64
);
1652 msvcrt_stat64_to_stat32(&buf64
, buf
);
1656 /*********************************************************************
1657 * _fstat64i32 (MSVCR80.@)
1659 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1662 struct MSVCRT__stat64 buf64
;
1664 ret
= MSVCRT__fstat64(fd
, &buf64
);
1666 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1670 /*********************************************************************
1671 * _futime64 (MSVCRT.@)
1673 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1675 HANDLE hand
= msvcrt_fdtoh(fd
);
1680 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1685 time_to_filetime( t
->actime
, &at
);
1686 time_to_filetime( t
->modtime
, &wt
);
1689 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1691 msvcrt_set_errno(GetLastError());
1697 /*********************************************************************
1698 * _futime32 (MSVCRT.@)
1700 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1704 struct MSVCRT___utimbuf64 t64
;
1705 t64
.actime
= t
->actime
;
1706 t64
.modtime
= t
->modtime
;
1707 return _futime64( fd
, &t64
);
1710 return _futime64( fd
, NULL
);
1713 /*********************************************************************
1714 * _futime (MSVCRT.@)
1717 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1719 return _futime64( fd
, t
);
1722 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1724 return _futime32( fd
, t
);
1728 /*********************************************************************
1729 * _get_osfhandle (MSVCRT.@)
1731 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1733 HANDLE hand
= msvcrt_fdtoh(fd
);
1734 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1736 return (MSVCRT_intptr_t
)hand
;
1739 /*********************************************************************
1740 * _mktemp_s (MSVCRT.@)
1742 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1746 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1747 return MSVCRT_EINVAL
;
1749 for(len
=0; len
<size
; len
++)
1752 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1755 return MSVCRT_EINVAL
;
1758 for(xno
=1; xno
<=6; xno
++)
1759 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1760 return MSVCRT_EINVAL
;
1762 id
= GetCurrentProcessId();
1763 for(xno
=1; xno
<6; xno
++) {
1764 pattern
[len
-xno
] = id
%10 + '0';
1768 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1769 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1774 *MSVCRT__errno() = MSVCRT_EEXIST
;
1775 return MSVCRT_EEXIST
;
1778 /*********************************************************************
1779 * _mktemp (MSVCRT.@)
1781 char * CDECL
MSVCRT__mktemp(char *pattern
)
1784 char *retVal
= pattern
;
1792 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1796 id
= GetCurrentProcessId();
1800 int tempNum
= id
/ 10;
1801 *pattern
-- = id
- (tempNum
* 10) + '0';
1807 *pattern
= letter
++;
1808 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1810 } while(letter
<= 'z');
1814 /*********************************************************************
1815 * _wmktemp_s (MSVCRT.@)
1817 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1821 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1822 return MSVCRT_EINVAL
;
1824 for(len
=0; len
<size
; len
++)
1827 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1830 return MSVCRT_EINVAL
;
1833 for(xno
=1; xno
<=6; xno
++)
1834 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1835 return MSVCRT_EINVAL
;
1837 id
= GetCurrentProcessId();
1838 for(xno
=1; xno
<6; xno
++) {
1839 pattern
[len
-xno
] = id
%10 + '0';
1843 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1844 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1849 *MSVCRT__errno() = MSVCRT_EEXIST
;
1850 return MSVCRT_EEXIST
;
1853 /*********************************************************************
1854 * _wmktemp (MSVCRT.@)
1856 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1859 MSVCRT_wchar_t
*retVal
= pattern
;
1861 MSVCRT_wchar_t letter
= 'a';
1867 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1871 id
= GetCurrentProcessId();
1875 int tempNum
= id
/ 10;
1876 *pattern
-- = id
- (tempNum
* 10) + '0';
1882 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1884 *pattern
= letter
++;
1885 } while(letter
!= '|');
1889 static unsigned split_oflags(unsigned oflags
)
1892 unsigned unsupp
; /* until we support everything */
1894 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1895 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1896 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1897 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1898 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1899 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1900 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1901 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1902 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1904 if ((unsupp
= oflags
& ~(
1905 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1906 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1907 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1908 MSVCRT__O_NOINHERIT
|
1909 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
1910 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
1912 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1917 /*********************************************************************
1920 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1923 SECURITY_ATTRIBUTES sa
;
1924 HANDLE readHandle
, writeHandle
;
1928 *MSVCRT__errno() = MSVCRT_EINVAL
;
1932 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1933 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1934 sa
.lpSecurityDescriptor
= NULL
;
1935 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1937 unsigned int wxflags
= split_oflags(textmode
);
1941 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
1945 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
1953 MSVCRT__close(pfds
[0]);
1954 CloseHandle(writeHandle
);
1955 *MSVCRT__errno() = MSVCRT_EMFILE
;
1960 CloseHandle(readHandle
);
1961 CloseHandle(writeHandle
);
1962 *MSVCRT__errno() = MSVCRT_EMFILE
;
1967 msvcrt_set_errno(GetLastError());
1972 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1974 char bom
[sizeof(utf8_bom
)];
1977 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
1979 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1982 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1983 oflags
|= MSVCRT__O_U8TEXT
;
1984 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1986 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1987 oflags
|= MSVCRT__O_U16TEXT
;
1989 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1995 /*********************************************************************
1996 * _wsopen_s (MSVCRT.@)
1998 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2000 DWORD access
= 0, creation
= 0, attrib
;
2001 SECURITY_ATTRIBUTES sa
;
2006 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2007 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2009 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2012 wxflag
= split_oflags(oflags
);
2013 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2015 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2016 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2017 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2020 if (oflags
& MSVCRT__O_CREAT
)
2022 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2023 FIXME(": pmode 0x%04x ignored\n", pmode
);
2025 WARN(": pmode 0x%04x ignored\n", pmode
);
2027 if (oflags
& MSVCRT__O_EXCL
)
2028 creation
= CREATE_NEW
;
2029 else if (oflags
& MSVCRT__O_TRUNC
)
2030 creation
= CREATE_ALWAYS
;
2032 creation
= OPEN_ALWAYS
;
2034 else /* no MSVCRT__O_CREAT */
2036 if (oflags
& MSVCRT__O_TRUNC
)
2037 creation
= TRUNCATE_EXISTING
;
2039 creation
= OPEN_EXISTING
;
2044 case MSVCRT__SH_DENYRW
:
2047 case MSVCRT__SH_DENYWR
:
2048 sharing
= FILE_SHARE_READ
;
2050 case MSVCRT__SH_DENYRD
:
2051 sharing
= FILE_SHARE_WRITE
;
2053 case MSVCRT__SH_DENYNO
:
2054 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2057 ERR( "Unhandled shflags 0x%x\n", shflags
);
2058 return MSVCRT_EINVAL
;
2060 attrib
= FILE_ATTRIBUTE_NORMAL
;
2062 if (oflags
& MSVCRT__O_TEMPORARY
)
2064 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2066 sharing
|= FILE_SHARE_DELETE
;
2069 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2070 sa
.lpSecurityDescriptor
= NULL
;
2071 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2073 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2074 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2075 && !(access
&GENERIC_READ
))
2077 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2078 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2079 if (hand
!= INVALID_HANDLE_VALUE
)
2081 oflags
= check_bom(hand
, oflags
, FALSE
);
2085 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2088 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2089 if (hand
== INVALID_HANDLE_VALUE
) {
2090 WARN(":failed-last error (%d)\n",GetLastError());
2091 msvcrt_set_errno(GetLastError());
2092 return *MSVCRT__errno();
2095 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2097 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2098 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2099 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2101 if (oflags
& MSVCRT__O_U8TEXT
)
2103 DWORD written
= 0, tmp
;
2105 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2106 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2108 if (written
!= sizeof(utf8_bom
)) {
2109 WARN("error writing BOM\n");
2111 msvcrt_set_errno(GetLastError());
2112 return *MSVCRT__errno();
2117 DWORD written
= 0, tmp
;
2119 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2120 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2122 if (written
!= sizeof(utf16_bom
))
2124 WARN("error writing BOM\n");
2126 msvcrt_set_errno(GetLastError());
2127 return *MSVCRT__errno();
2131 else if (access
& GENERIC_READ
)
2132 oflags
= check_bom(hand
, oflags
, TRUE
);
2135 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2137 return *MSVCRT__errno();
2139 if (oflags
& MSVCRT__O_WTEXT
)
2140 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2141 else if (oflags
& MSVCRT__O_U16TEXT
)
2142 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
2143 else if (oflags
& MSVCRT__O_U8TEXT
)
2144 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
2146 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2150 /*********************************************************************
2151 * _wsopen (MSVCRT.@)
2153 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2158 if (oflags
& MSVCRT__O_CREAT
)
2162 __ms_va_start(ap
, shflags
);
2163 pmode
= va_arg(ap
, int);
2169 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2173 /*********************************************************************
2174 * _sopen_s (MSVCRT.@)
2176 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2178 MSVCRT_wchar_t
*pathW
;
2181 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2182 return MSVCRT_EINVAL
;
2184 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2189 /*********************************************************************
2192 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2197 if (oflags
& MSVCRT__O_CREAT
)
2201 __ms_va_start(ap
, shflags
);
2202 pmode
= va_arg(ap
, int);
2208 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2212 /*********************************************************************
2215 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2219 if (flags
& MSVCRT__O_CREAT
)
2222 __ms_va_start(ap
, flags
);
2223 pmode
= va_arg(ap
, int);
2225 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2228 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2231 /*********************************************************************
2234 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2238 if (flags
& MSVCRT__O_CREAT
)
2241 __ms_va_start(ap
, flags
);
2242 pmode
= va_arg(ap
, int);
2244 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2247 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2250 /*********************************************************************
2253 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2255 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2256 return MSVCRT__open(path
, usedFlags
);
2259 /*********************************************************************
2260 * _wcreat (MSVCRT.@)
2262 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2264 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2265 return MSVCRT__wopen(path
, usedFlags
);
2268 /*********************************************************************
2269 * _open_osfhandle (MSVCRT.@)
2271 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2276 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2277 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2278 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2279 * text - it never sets MSVCRT__O_BINARY.
2281 /* don't let split_oflags() decide the mode if no mode is passed */
2282 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2283 oflags
|= MSVCRT__O_BINARY
;
2285 flags
= GetFileType((HANDLE
)handle
);
2286 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2288 msvcrt_set_errno(GetLastError());
2292 if (flags
== FILE_TYPE_CHAR
)
2294 else if (flags
== FILE_TYPE_PIPE
)
2298 flags
|= split_oflags(oflags
);
2300 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2301 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2305 /*********************************************************************
2308 int CDECL
MSVCRT__rmtmp(void)
2310 int num_removed
= 0, i
;
2314 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2315 file
= msvcrt_get_file(i
);
2317 if (file
->_tmpfname
)
2319 MSVCRT_fclose(file
);
2326 TRACE(":removed (%d) temp files\n",num_removed
);
2330 static inline int get_utf8_char_len(char ch
)
2332 if((ch
&0xf8) == 0xf0)
2334 else if((ch
&0xf0) == 0xe0)
2336 else if((ch
&0xe0) == 0xc0)
2341 /*********************************************************************
2342 * (internal) read_utf8
2344 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2346 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2347 HANDLE hand
= fdinfo
->handle
;
2348 char min_buf
[4], *readbuf
, lookahead
;
2349 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2351 /* make the buffer big enough to hold at least one character */
2352 /* read bytes have to fit to output and lookahead buffers */
2354 readbuf_size
= count
< 4 ? 4 : count
;
2355 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2360 if(fdinfo
->lookahead
[0] != '\n') {
2361 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2362 fdinfo
->lookahead
[0] = '\n';
2364 if(fdinfo
->lookahead
[1] != '\n') {
2365 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2366 fdinfo
->lookahead
[1] = '\n';
2368 if(fdinfo
->lookahead
[2] != '\n') {
2369 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2370 fdinfo
->lookahead
[2] = '\n';
2375 /* NOTE: this case is broken in native dll, reading
2376 * sometimes fails when small buffer is passed
2379 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2380 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2381 fdinfo
->wxflag
|= WX_ATEOF
;
2384 msvcrt_set_errno(GetLastError());
2387 }else if(!num_read
) {
2388 fdinfo
->wxflag
|= WX_ATEOF
;
2394 char_len
= get_utf8_char_len(readbuf
[0]);
2396 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2400 if(readbuf
[0] == '\n')
2401 fdinfo
->wxflag
|= WX_READNL
;
2403 fdinfo
->wxflag
&= ~WX_READNL
;
2405 if(readbuf
[0] == 0x1a) {
2406 fdinfo
->wxflag
|= WX_ATEOF
;
2410 if(readbuf
[0] == '\r') {
2411 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2413 else if(lookahead
== '\n')
2417 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2418 fdinfo
->lookahead
[0] = lookahead
;
2420 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2425 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2426 msvcrt_set_errno(GetLastError());
2433 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2436 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2437 fdinfo
->wxflag
|= WX_ATEOF
;
2438 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2441 msvcrt_set_errno(GetLastError());
2442 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2445 }else if(!pos
&& !num_read
) {
2446 fdinfo
->wxflag
|= WX_ATEOF
;
2447 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2452 if(readbuf
[0] == '\n')
2453 fdinfo
->wxflag
|= WX_READNL
;
2455 fdinfo
->wxflag
&= ~WX_READNL
;
2457 /* Find first byte of last character (may be incomplete) */
2458 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2459 if((readbuf
[i
]&0xc0) != 0x80)
2461 char_len
= get_utf8_char_len(readbuf
[i
]);
2462 if(char_len
+i
<= pos
)
2465 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
)) {
2467 fdinfo
->lookahead
[0] = readbuf
[i
];
2469 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2471 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2473 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2477 for(i
=0, j
=0; i
<pos
; i
++) {
2478 if(readbuf
[i
] == 0x1a) {
2479 fdinfo
->wxflag
|= WX_ATEOF
;
2483 /* strip '\r' if followed by '\n' */
2484 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2485 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2486 readbuf
[j
++] = '\r';
2487 }else if(lookahead
== '\n' && j
==0) {
2488 readbuf
[j
++] = '\n';
2490 if(lookahead
!= '\n')
2491 readbuf
[j
++] = '\r';
2493 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2494 fdinfo
->lookahead
[0] = lookahead
;
2496 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2498 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2499 readbuf
[j
++] = readbuf
[i
];
2504 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2505 msvcrt_set_errno(GetLastError());
2506 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2510 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2514 /*********************************************************************
2517 * When reading \r as last character in text mode, read() positions
2518 * the file pointer on the \r character while getc() goes on to
2521 static int read_i(int fd
, void *buf
, unsigned int count
)
2523 DWORD num_read
, utf16
;
2524 char *bufstart
= buf
;
2525 HANDLE hand
= msvcrt_fdtoh(fd
);
2526 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2531 if (fdinfo
->wxflag
& WX_ATEOF
) {
2532 TRACE("already at EOF, returning 0\n");
2535 /* Don't trace small reads, it gets *very* annoying */
2537 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2538 if (hand
== INVALID_HANDLE_VALUE
)
2540 *MSVCRT__errno() = MSVCRT_EBADF
;
2544 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2545 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2547 *MSVCRT__errno() = MSVCRT_EINVAL
;
2551 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2552 return read_utf8(fd
, buf
, count
);
2554 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2556 if (fdinfo
->lookahead
[0] != '\n')
2558 bufstart
[0] = fdinfo
->lookahead
[0];
2559 fdinfo
->lookahead
[0] = '\n';
2563 bufstart
[1] = fdinfo
->lookahead
[1];
2564 fdinfo
->lookahead
[1] = '\n';
2567 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2568 num_read
+= 1+utf16
;
2573 if(utf16
&& (num_read
&1))
2575 /* msvcr90 uses uninitialized value from the buffer in this case */
2576 /* msvcrt ignores additional data */
2577 ERR("got odd number of bytes in UTF16 mode\n");
2581 if (count
!= 0 && num_read
== 0)
2583 fdinfo
->wxflag
|= WX_ATEOF
;
2584 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2586 else if (fdinfo
->wxflag
& WX_TEXT
)
2590 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2591 fdinfo
->wxflag
|= WX_READNL
;
2593 fdinfo
->wxflag
&= ~WX_READNL
;
2595 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2597 /* in text mode, a ctrl-z signals EOF */
2598 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2600 fdinfo
->wxflag
|= WX_ATEOF
;
2601 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2605 /* in text mode, strip \r if followed by \n */
2606 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2611 lookahead
[1] = '\n';
2612 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2614 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2616 bufstart
[j
++] = '\n';
2617 if(utf16
) bufstart
[j
++] = 0;
2621 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2623 bufstart
[j
++] = '\r';
2624 if(utf16
) bufstart
[j
++] = 0;
2627 if (fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2629 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2631 bufstart
[j
++] = '\n';
2632 if (utf16
) bufstart
[j
++] = 0;
2636 fdinfo
->lookahead
[0] = lookahead
[0];
2637 fdinfo
->lookahead
[1] = lookahead
[1];
2641 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2646 bufstart
[j
++] = '\r';
2647 if(utf16
) bufstart
[j
++] = 0;
2650 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2651 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2653 bufstart
[j
++] = bufstart
[i
];
2654 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2662 if (GetLastError() == ERROR_BROKEN_PIPE
)
2664 TRACE(":end-of-pipe\n");
2665 fdinfo
->wxflag
|= WX_ATEOF
;
2670 TRACE(":failed-last error (%d)\n",GetLastError());
2676 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2680 /*********************************************************************
2683 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2686 num_read
= read_i(fd
, buf
, count
);
2690 /*********************************************************************
2691 * _setmode (MSVCRT.@)
2693 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2695 int ret
= msvcrt_get_ioinfo(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2696 if(ret
==MSVCRT__O_TEXT
&& (msvcrt_get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2697 ret
= MSVCRT__O_WTEXT
;
2699 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2700 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2701 *MSVCRT__errno() = MSVCRT_EINVAL
;
2705 if(mode
== MSVCRT__O_BINARY
) {
2706 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2707 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2711 msvcrt_get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2712 if(mode
== MSVCRT__O_TEXT
)
2713 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2714 else if(mode
== MSVCRT__O_U8TEXT
)
2715 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2717 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2722 /*********************************************************************
2723 * _stat64 (MSVCRT.@)
2725 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2728 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2729 unsigned short mode
= ALL_S_IREAD
;
2732 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2734 plen
= strlen(path
);
2735 while (plen
&& path
[plen
-1]==' ')
2738 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2739 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2741 *MSVCRT__errno() = MSVCRT_ENOENT
;
2745 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2747 TRACE("failed (%d)\n",GetLastError());
2748 *MSVCRT__errno() = MSVCRT_ENOENT
;
2752 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2754 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2755 Bon 011120: This FIXME seems incorrect
2756 Also a letter as first char isn't enough to be classified
2759 if (isalpha(*path
)&& (*(path
+1)==':'))
2760 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2762 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2764 /* Dir, or regular file? */
2765 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2766 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2769 mode
|= MSVCRT__S_IFREG
;
2771 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2773 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2774 (tolower(path
[plen
-3]) << 16);
2775 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2776 mode
|= ALL_S_IEXEC
;
2780 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2781 mode
|= ALL_S_IWRITE
;
2783 buf
->st_mode
= mode
;
2785 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2786 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2788 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2789 buf
->st_mtime
= buf
->st_ctime
= dw
;
2790 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2791 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2792 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2796 /*********************************************************************
2797 * _stati64 (MSVCRT.@)
2799 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2802 struct MSVCRT__stat64 buf64
;
2804 ret
= MSVCRT_stat64(path
, &buf64
);
2806 msvcrt_stat64_to_stati64(&buf64
, buf
);
2810 /*********************************************************************
2813 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2816 struct MSVCRT__stat64 buf64
;
2818 ret
= MSVCRT_stat64( path
, &buf64
);
2820 msvcrt_stat64_to_stat(&buf64
, buf
);
2824 /*********************************************************************
2825 * _stat32 (MSVCR100.@)
2827 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2830 struct MSVCRT__stat64 buf64
;
2832 ret
= MSVCRT_stat64(path
, &buf64
);
2834 msvcrt_stat64_to_stat32(&buf64
, buf
);
2838 /*********************************************************************
2839 * _stat32i64 (MSVCR100.@)
2841 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2844 struct MSVCRT__stat64 buf64
;
2846 ret
= MSVCRT_stat64(path
, &buf64
);
2848 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2852 /*********************************************************************
2853 * _stat64i32 (MSVCR100.@)
2855 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2858 struct MSVCRT__stat64 buf64
;
2860 ret
= MSVCRT_stat64(path
, &buf64
);
2862 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2866 /*********************************************************************
2867 * _wstat64 (MSVCRT.@)
2869 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2872 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2873 unsigned short mode
= ALL_S_IREAD
;
2876 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2878 plen
= strlenW(path
);
2879 while (plen
&& path
[plen
-1]==' ')
2882 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2883 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2885 *MSVCRT__errno() = MSVCRT_ENOENT
;
2889 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2891 TRACE("failed (%d)\n",GetLastError());
2892 *MSVCRT__errno() = MSVCRT_ENOENT
;
2896 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2898 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2899 if (MSVCRT_iswalpha(*path
))
2900 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2902 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2904 /* Dir, or regular file? */
2905 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2906 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2909 mode
|= MSVCRT__S_IFREG
;
2911 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2913 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2914 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2915 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2916 mode
|= ALL_S_IEXEC
;
2920 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2921 mode
|= ALL_S_IWRITE
;
2923 buf
->st_mode
= mode
;
2925 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2926 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2928 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2929 buf
->st_mtime
= buf
->st_ctime
= dw
;
2930 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2931 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2932 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2936 /*********************************************************************
2937 * _wstati64 (MSVCRT.@)
2939 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2942 struct MSVCRT__stat64 buf64
;
2944 ret
= MSVCRT__wstat64(path
, &buf64
);
2946 msvcrt_stat64_to_stati64(&buf64
, buf
);
2950 /*********************************************************************
2953 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2956 struct MSVCRT__stat64 buf64
;
2958 ret
= MSVCRT__wstat64( path
, &buf64
);
2959 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2963 /*********************************************************************
2964 * _wstat32 (MSVCR100.@)
2966 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
2969 struct MSVCRT__stat64 buf64
;
2971 ret
= MSVCRT__wstat64(path
, &buf64
);
2973 msvcrt_stat64_to_stat32(&buf64
, buf
);
2977 /*********************************************************************
2978 * _wstat32i64 (MSVCR100.@)
2980 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
2983 struct MSVCRT__stat64 buf64
;
2985 ret
= MSVCRT__wstat64(path
, &buf64
);
2987 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2991 /*********************************************************************
2992 * _wstat64i32 (MSVCR100.@)
2994 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
2997 struct MSVCRT__stat64 buf64
;
2999 ret
= MSVCRT__wstat64(path
, &buf64
);
3001 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3005 /*********************************************************************
3008 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3010 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3013 /*********************************************************************
3014 * _telli64 (MSVCRT.@)
3016 __int64 CDECL
_telli64(int fd
)
3018 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3021 /*********************************************************************
3022 * _tempnam (MSVCRT.@)
3024 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3026 char tmpbuf
[MAX_PATH
];
3027 const char *tmp_dir
= MSVCRT_getenv("TMP");
3029 if (tmp_dir
) dir
= tmp_dir
;
3031 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3032 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3034 TRACE("got name (%s)\n",tmpbuf
);
3035 DeleteFileA(tmpbuf
);
3036 return MSVCRT__strdup(tmpbuf
);
3038 TRACE("failed (%d)\n",GetLastError());
3042 /*********************************************************************
3043 * _wtempnam (MSVCRT.@)
3045 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3047 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3048 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3049 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3051 if (tmp_dir
) dir
= tmp_dir
;
3053 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3054 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3056 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3057 DeleteFileW(tmpbuf
);
3058 return MSVCRT__wcsdup(tmpbuf
);
3060 TRACE("failed (%d)\n",GetLastError());
3064 /*********************************************************************
3067 int CDECL
MSVCRT__umask(int umask
)
3069 int old_umask
= MSVCRT_umask
;
3070 TRACE("(%d)\n",umask
);
3071 MSVCRT_umask
= umask
;
3075 /*********************************************************************
3076 * _utime64 (MSVCRT.@)
3078 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3080 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3084 int retVal
= _futime64(fd
, t
);
3091 /*********************************************************************
3092 * _utime32 (MSVCRT.@)
3094 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3098 struct MSVCRT___utimbuf64 t64
;
3099 t64
.actime
= t
->actime
;
3100 t64
.modtime
= t
->modtime
;
3101 return _utime64( path
, &t64
);
3104 return _utime64( path
, NULL
);
3107 /*********************************************************************
3111 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3113 return _utime64( path
, t
);
3116 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3118 return _utime32( path
, t
);
3122 /*********************************************************************
3123 * _wutime64 (MSVCRT.@)
3125 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3127 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3131 int retVal
= _futime64(fd
, t
);
3138 /*********************************************************************
3139 * _wutime32 (MSVCRT.@)
3141 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3145 struct MSVCRT___utimbuf64 t64
;
3146 t64
.actime
= t
->actime
;
3147 t64
.modtime
= t
->modtime
;
3148 return _wutime64( path
, &t64
);
3151 return _wutime64( path
, NULL
);
3154 /*********************************************************************
3155 * _wutime (MSVCRT.@)
3158 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3160 return _wutime64( path
, t
);
3163 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3165 return _wutime32( path
, t
);
3169 /*********************************************************************
3172 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3175 ioinfo
*info
= msvcrt_get_ioinfo(fd
);
3176 HANDLE hand
= info
->handle
;
3178 /* Don't trace small writes, it gets *very* annoying */
3181 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3183 if (hand
== INVALID_HANDLE_VALUE
)
3185 *MSVCRT__errno() = MSVCRT_EBADF
;
3189 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3191 *MSVCRT__errno() = MSVCRT_EINVAL
;
3195 /* If appending, go to EOF */
3196 if (info
->wxflag
& WX_APPEND
)
3197 MSVCRT__lseek(fd
, 0, FILE_END
);
3199 if (!(info
->wxflag
& WX_TEXT
))
3201 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3202 && (num_written
== count
))
3204 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3205 hand
, GetLastError());
3206 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3210 unsigned int i
, j
, nr_lf
, size
;
3213 const char *s
= buf
, *buf_start
= buf
;
3215 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3217 /* find number of \n */
3218 for (nr_lf
=0, i
=0; i
<count
; i
++)
3224 if ((q
= p
= MSVCRT_malloc(size
)))
3226 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3235 FIXME("Malloc failed\n");
3247 else if (info
->exflag
& EF_UTF16
)
3249 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3250 if (s
[i
]=='\n' && s
[i
+1]==0)
3255 if ((q
= p
= MSVCRT_malloc(size
)))
3257 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3259 if (s
[i
]=='\n' && s
[i
+1]==0)
3270 FIXME("Malloc failed\n");
3286 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3287 if (s
[i
]=='\n' && s
[i
+1]==0)
3290 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3292 msvcrt_set_errno(GetLastError());
3297 size
= conv_len
+nr_lf
;
3298 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3300 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3302 if (s
[i
]=='\n' && s
[i
+1]==0)
3310 q
= p
+count
+nr_lf
*2;
3311 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3312 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3316 FIXME("Malloc failed\n");
3323 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3327 if (num_written
!= size
)
3329 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3330 fd
, hand
, GetLastError(), num_written
);
3331 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3332 return s
- buf_start
;
3340 /*********************************************************************
3343 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3347 MSVCRT__lock_file(file
);
3348 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3349 if (len
== sizeof(val
)) {
3350 MSVCRT__unlock_file(file
);
3354 file
->_flag
|= MSVCRT__IOERR
;
3355 MSVCRT__unlock_file(file
);
3359 /*********************************************************************
3362 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3366 MSVCRT__lock_file(file
);
3368 MSVCRT_free(file
->_tmpfname
);
3369 file
->_tmpfname
= NULL
;
3370 /* flush stdio buffers */
3371 if(file
->_flag
& MSVCRT__IOWRT
)
3372 MSVCRT_fflush(file
);
3373 if(file
->_flag
& MSVCRT__IOMYBUF
)
3374 MSVCRT_free(file
->_base
);
3376 r
=MSVCRT__close(file
->_file
);
3379 MSVCRT__unlock_file(file
);
3381 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3384 /*********************************************************************
3387 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3389 return file
->_flag
& MSVCRT__IOEOF
;
3392 /*********************************************************************
3395 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3397 return file
->_flag
& MSVCRT__IOERR
;
3400 /*********************************************************************
3401 * _filbuf (MSVCRT.@)
3403 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3406 MSVCRT__lock_file(file
);
3408 if(file
->_flag
& MSVCRT__IOSTRG
) {
3409 MSVCRT__unlock_file(file
);
3413 /* Allocate buffer if needed */
3414 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
))
3415 msvcrt_alloc_buffer(file
);
3417 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3418 if(file
->_flag
& MSVCRT__IORW
)
3419 file
->_flag
|= MSVCRT__IOREAD
;
3421 MSVCRT__unlock_file(file
);
3426 if(file
->_flag
& MSVCRT__IONBF
) {
3428 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3429 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3430 MSVCRT__unlock_file(file
);
3434 MSVCRT__unlock_file(file
);
3437 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3439 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3441 MSVCRT__unlock_file(file
);
3446 file
->_ptr
= file
->_base
+1;
3447 c
= *(unsigned char *)file
->_base
;
3448 MSVCRT__unlock_file(file
);
3453 /*********************************************************************
3456 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3461 MSVCRT__lock_file(file
);
3464 i
= (unsigned char *)file
->_ptr
++;
3467 j
= MSVCRT__filbuf(file
);
3469 MSVCRT__unlock_file(file
);
3473 /*********************************************************************
3474 * _fgetchar (MSVCRT.@)
3476 int CDECL
MSVCRT__fgetchar(void)
3478 return MSVCRT_fgetc(MSVCRT_stdin
);
3481 /*********************************************************************
3484 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3486 int cc
= MSVCRT_EOF
;
3487 char * buf_start
= s
;
3489 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3490 file
,file
->_file
,s
,size
);
3492 MSVCRT__lock_file(file
);
3494 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3499 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3501 TRACE(":nothing read\n");
3502 MSVCRT__unlock_file(file
);
3505 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3508 TRACE(":got %s\n", debugstr_a(buf_start
));
3509 MSVCRT__unlock_file(file
);
3513 /*********************************************************************
3516 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3521 MSVCRT__lock_file(file
);
3523 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3524 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3527 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3528 ch
= MSVCRT_fgetc(file
);
3529 if(ch
== MSVCRT_EOF
) {
3536 char mbs
[MSVCRT_MB_LEN_MAX
];
3539 ch
= MSVCRT_fgetc(file
);
3540 if(ch
!= MSVCRT_EOF
) {
3542 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3543 ch
= MSVCRT_fgetc(file
);
3544 if(ch
!= MSVCRT_EOF
) {
3553 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3557 MSVCRT__unlock_file(file
);
3561 /*********************************************************************
3564 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3571 MSVCRT__lock_file(file
);
3572 for (j
=0; j
<sizeof(int); j
++) {
3573 k
= MSVCRT_fgetc(file
);
3574 if (k
== MSVCRT_EOF
) {
3575 file
->_flag
|= MSVCRT__IOEOF
;
3576 MSVCRT__unlock_file(file
);
3582 MSVCRT__unlock_file(file
);
3586 /*********************************************************************
3589 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3591 return MSVCRT_fgetwc(file
);
3594 /*********************************************************************
3595 * _fgetwchar (MSVCRT.@)
3597 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3599 return MSVCRT_fgetwc(MSVCRT_stdin
);
3602 /*********************************************************************
3603 * getwchar (MSVCRT.@)
3605 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3607 return MSVCRT__fgetwchar();
3610 /*********************************************************************
3613 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3615 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3616 MSVCRT_wchar_t
* buf_start
= s
;
3618 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3619 file
,file
->_file
,s
,size
);
3621 MSVCRT__lock_file(file
);
3623 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3628 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3630 TRACE(":nothing read\n");
3631 MSVCRT__unlock_file(file
);
3634 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3637 TRACE(":got %s\n", debugstr_w(buf_start
));
3638 MSVCRT__unlock_file(file
);
3642 /*********************************************************************
3643 * _flsbuf (MSVCRT.@)
3645 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3647 /* Flush output buffer */
3648 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3649 msvcrt_alloc_buffer(file
);
3651 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3652 if(file
->_flag
& MSVCRT__IORW
)
3653 file
->_flag
|= MSVCRT__IOWRT
;
3661 res
= msvcrt_flush_buffer(file
);
3667 return res
? res
: c
&0xff;
3671 /* set _cnt to 0 for unbuffered FILEs */
3673 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3676 file
->_flag
|= MSVCRT__IOERR
;
3681 /*********************************************************************
3684 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3686 MSVCRT_size_t wrcnt
=size
* nmemb
;
3691 MSVCRT__lock_file(file
);
3695 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3696 memcpy(file
->_ptr
, ptr
, pcnt
);
3701 ptr
= (const char*)ptr
+ pcnt
;
3702 } else if(!file
->_bufsiz
&& (file
->_flag
& MSVCRT__IONBF
)) {
3703 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3704 if(file
->_flag
& MSVCRT__IORW
)
3705 file
->_flag
|= MSVCRT__IOWRT
;
3710 if(MSVCRT__write(file
->_file
, ptr
, wrcnt
) <= 0) {
3711 file
->_flag
|= MSVCRT__IOERR
;
3717 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3721 ptr
= (const char*)ptr
+ 1;
3725 MSVCRT__unlock_file(file
);
3726 return written
/ size
;
3729 /*********************************************************************
3732 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3734 MSVCRT_wchar_t mwc
=wc
;
3738 MSVCRT__lock_file(file
);
3739 fdinfo
= msvcrt_get_ioinfo(file
->_file
);
3741 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3742 char buf
[MSVCRT_MB_LEN_MAX
];
3745 char_len
= MSVCRT_wctomb(buf
, mwc
);
3746 if(char_len
!=-1 && MSVCRT_fwrite(buf
, char_len
, 1, file
)==1)
3750 }else if(MSVCRT_fwrite(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3756 MSVCRT__unlock_file(file
);
3760 /*********************************************************************
3761 * _fputwchar (MSVCRT.@)
3763 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3765 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3768 /*********************************************************************
3769 * _wfsopen (MSVCRT.@)
3771 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3774 int open_flags
, stream_flags
, fd
;
3776 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3778 /* map mode string to open() flags. "man fopen" for possibilities. */
3779 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3783 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3786 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3788 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3795 TRACE(":got (%p)\n",file
);
3796 if (fd
>= 0 && !file
)
3802 /*********************************************************************
3803 * _fsopen (MSVCRT.@)
3805 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3808 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3810 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3811 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3812 *MSVCRT__errno() = MSVCRT_EINVAL
;
3815 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3818 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3819 *MSVCRT__errno() = MSVCRT_EINVAL
;
3823 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
3830 /*********************************************************************
3833 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
3835 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3838 /*********************************************************************
3839 * fopen_s (MSVCRT.@)
3841 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
3842 const char *filename
, const char *mode
)
3844 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3845 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3846 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3848 *pFile
= MSVCRT_fopen(filename
, mode
);
3851 return *MSVCRT__errno();
3855 /*********************************************************************
3856 * _wfopen (MSVCRT.@)
3858 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
3860 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3863 /*********************************************************************
3864 * _wfopen_s (MSVCRT.@)
3866 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
3867 const MSVCRT_wchar_t
*mode
)
3869 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3870 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3871 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3873 *pFile
= MSVCRT__wfopen(filename
, mode
);
3876 return *MSVCRT__errno();
3880 /*********************************************************************
3883 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
3887 MSVCRT__lock_file(file
);
3893 res
= msvcrt_flush_buffer(file
);
3894 MSVCRT__unlock_file(file
);
3895 return res
? res
: c
;
3898 MSVCRT__unlock_file(file
);
3902 res
= MSVCRT__flsbuf(c
, file
);
3903 MSVCRT__unlock_file(file
);
3908 /*********************************************************************
3909 * _fputchar (MSVCRT.@)
3911 int CDECL
MSVCRT__fputchar(int c
)
3913 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3916 /*********************************************************************
3919 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3921 MSVCRT_size_t rcnt
=size
* nmemb
;
3922 MSVCRT_size_t read
=0;
3928 MSVCRT__lock_file(file
);
3930 /* first buffered data */
3932 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3933 memcpy(ptr
, file
->_ptr
, pcnt
);
3938 ptr
= (char*)ptr
+ pcnt
;
3939 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3940 if(file
->_flag
& MSVCRT__IORW
) {
3941 file
->_flag
|= MSVCRT__IOREAD
;
3943 MSVCRT__unlock_file(file
);
3950 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& !(file
->_flag
& MSVCRT__IONBF
)
3951 && (file
->_bufsiz
!= 0 || msvcrt_alloc_buffer(file
))) {
3952 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3953 file
->_ptr
= file
->_base
;
3954 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3955 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3956 if (i
> 0 && i
< file
->_cnt
) {
3957 msvcrt_get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3958 file
->_flag
&= ~MSVCRT__IOEOF
;
3961 memcpy(ptr
, file
->_ptr
, i
);
3965 } else if (rcnt
> UINT_MAX
) {
3966 i
= MSVCRT__read(file
->_file
, ptr
, UINT_MAX
);
3967 } else if (rcnt
< MSVCRT_BUFSIZ
) {
3968 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
3970 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
3974 ptr
= (char *)ptr
+i
;
3975 /* expose feof condition in the flags
3976 * MFC tests file->_flag for feof, and doesn't call feof())
3978 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3979 file
->_flag
|= MSVCRT__IOEOF
;
3982 file
->_flag
|= MSVCRT__IOERR
;
3989 MSVCRT__unlock_file(file
);
3994 /*********************************************************************
3995 * fread_s (MSVCR80.@)
3997 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
3998 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4000 size_t bytes_left
, buf_pos
;
4002 TRACE("(%p %lu %lu %lu %p\n", buf
, buf_size
, elem_size
, count
, stream
);
4004 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4006 memset(buf
, 0, buf_size
);
4009 if(!elem_size
|| !count
) return 0;
4010 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4011 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4013 bytes_left
= elem_size
*count
;
4016 if(stream
->_cnt
> 0) {
4017 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4019 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4020 memset(buf
, 0, buf_size
);
4024 MSVCRT_fread((char*)buf
+buf_pos
, 1, size
, stream
);
4028 int c
= MSVCRT__filbuf(stream
);
4033 if(!MSVCRT_CHECK_PMT_ERR(buf_size
-buf_pos
> 0, MSVCRT_ERANGE
)) {
4034 memset(buf
, 0, buf_size
);
4038 ((char*)buf
)[buf_pos
++] = c
;
4043 return buf_pos
/elem_size
;
4046 /*********************************************************************
4047 * _wfreopen (MSVCRT.@)
4050 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4052 int open_flags
, stream_flags
, fd
;
4054 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4057 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4061 MSVCRT_fclose(file
);
4062 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4064 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4066 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4076 /*********************************************************************
4077 * _wfreopen_s (MSVCRT.@)
4079 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4080 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4082 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4083 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4084 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4085 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4087 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4090 return *MSVCRT__errno();
4094 /*********************************************************************
4095 * freopen (MSVCRT.@)
4098 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4101 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4103 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4104 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4110 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4117 /*********************************************************************
4118 * freopen_s (MSVCRT.@)
4120 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4121 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4123 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4124 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4125 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4126 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4128 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4131 return *MSVCRT__errno();
4135 /*********************************************************************
4136 * fsetpos (MSVCRT.@)
4138 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4142 MSVCRT__lock_file(file
);
4143 /* Note that all this has been lifted 'as is' from fseek */
4144 if(file
->_flag
& MSVCRT__IOWRT
)
4145 msvcrt_flush_buffer(file
);
4147 /* Discard buffered input */
4149 file
->_ptr
= file
->_base
;
4151 /* Reset direction of i/o */
4152 if(file
->_flag
& MSVCRT__IORW
) {
4153 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4156 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4157 MSVCRT__unlock_file(file
);
4161 /*********************************************************************
4162 * _ftelli64 (MSVCRT.@)
4164 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4168 MSVCRT__lock_file(file
);
4169 pos
= _telli64(file
->_file
);
4171 MSVCRT__unlock_file(file
);
4175 if(file
->_flag
& MSVCRT__IOWRT
) {
4176 pos
+= file
->_ptr
- file
->_base
;
4178 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4181 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4185 } else if(!file
->_cnt
) { /* nothing to do */
4186 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4190 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4191 for(i
=0; i
<file
->_cnt
; i
++)
4192 if(file
->_ptr
[i
] == '\n')
4198 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
4199 MSVCRT__unlock_file(file
);
4203 pos
-= file
->_bufsiz
;
4204 pos
+= file
->_ptr
- file
->_base
;
4206 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4207 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
4210 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4217 MSVCRT__unlock_file(file
);
4221 /*********************************************************************
4224 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4226 return MSVCRT__ftelli64(file
);
4229 /*********************************************************************
4230 * fgetpos (MSVCRT.@)
4232 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4234 *pos
= MSVCRT__ftelli64(file
);
4240 /*********************************************************************
4243 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4245 MSVCRT_size_t len
= strlen(s
);
4248 MSVCRT__lock_file(file
);
4249 ret
= MSVCRT_fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4250 MSVCRT__unlock_file(file
);
4254 /*********************************************************************
4257 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4259 MSVCRT_size_t i
, len
= strlenW(s
);
4263 MSVCRT__lock_file(file
);
4264 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4265 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4266 MSVCRT__unlock_file(file
);
4270 tmp_buf
= add_std_buffer(file
);
4271 for (i
=0; i
<len
; i
++) {
4272 if(MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
) {
4273 if(tmp_buf
) remove_std_buffer(file
);
4274 MSVCRT__unlock_file(file
);
4279 if(tmp_buf
) remove_std_buffer(file
);
4280 MSVCRT__unlock_file(file
);
4284 /*********************************************************************
4285 * getchar (MSVCRT.@)
4287 int CDECL
MSVCRT_getchar(void)
4289 return MSVCRT_fgetc(MSVCRT_stdin
);
4292 /*********************************************************************
4295 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4297 return MSVCRT_fgetc(file
);
4300 /*********************************************************************
4303 char * CDECL
MSVCRT_gets(char *buf
)
4306 char * buf_start
= buf
;
4308 MSVCRT__lock_file(MSVCRT_stdin
);
4309 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4310 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
4311 if(cc
!= '\r') *buf
++ = (char)cc
;
4315 TRACE("got '%s'\n", buf_start
);
4316 MSVCRT__unlock_file(MSVCRT_stdin
);
4320 /*********************************************************************
4323 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4326 MSVCRT_wchar_t
* ws
= buf
;
4328 MSVCRT__lock_file(MSVCRT_stdin
);
4329 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4330 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
4333 *buf
++ = (MSVCRT_wchar_t
)cc
;
4337 TRACE("got %s\n", debugstr_w(ws
));
4338 MSVCRT__unlock_file(MSVCRT_stdin
);
4342 /*********************************************************************
4345 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4347 return MSVCRT_fputc(c
, file
);
4350 /*********************************************************************
4351 * putchar (MSVCRT.@)
4353 int CDECL
MSVCRT_putchar(int c
)
4355 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4358 /*********************************************************************
4359 * _putwch (MSVCRT.@)
4361 int CDECL
MSVCRT__putwch(int c
)
4363 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4366 /*********************************************************************
4369 int CDECL
MSVCRT_puts(const char *s
)
4371 MSVCRT_size_t len
= strlen(s
);
4374 MSVCRT__lock_file(MSVCRT_stdout
);
4375 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4376 MSVCRT__unlock_file(MSVCRT_stdout
);
4380 ret
= MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4381 MSVCRT__unlock_file(MSVCRT_stdout
);
4385 /*********************************************************************
4388 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4390 static const MSVCRT_wchar_t nl
= '\n';
4391 MSVCRT_size_t len
= strlenW(s
);
4394 MSVCRT__lock_file(MSVCRT_stdout
);
4395 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4396 MSVCRT__unlock_file(MSVCRT_stdout
);
4400 ret
= MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4401 MSVCRT__unlock_file(MSVCRT_stdout
);
4405 /*********************************************************************
4408 int CDECL
MSVCRT_remove(const char *path
)
4410 TRACE("(%s)\n",path
);
4411 if (DeleteFileA(path
))
4413 TRACE(":failed (%d)\n",GetLastError());
4414 msvcrt_set_errno(GetLastError());
4418 /*********************************************************************
4419 * _wremove (MSVCRT.@)
4421 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4423 TRACE("(%s)\n",debugstr_w(path
));
4424 if (DeleteFileW(path
))
4426 TRACE(":failed (%d)\n",GetLastError());
4427 msvcrt_set_errno(GetLastError());
4431 /*********************************************************************
4434 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4436 TRACE(":from %s to %s\n",oldpath
,newpath
);
4437 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4439 TRACE(":failed (%d)\n",GetLastError());
4440 msvcrt_set_errno(GetLastError());
4444 /*********************************************************************
4445 * _wrename (MSVCRT.@)
4447 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4449 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4450 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4452 TRACE(":failed (%d)\n",GetLastError());
4453 msvcrt_set_errno(GetLastError());
4457 /*********************************************************************
4458 * setvbuf (MSVCRT.@)
4460 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4462 MSVCRT__lock_file(file
);
4464 MSVCRT_free(file
->_base
);
4468 if(mode
== MSVCRT__IOFBF
) {
4469 file
->_flag
&= ~MSVCRT__IONBF
;
4470 file
->_base
= file
->_ptr
= buf
;
4472 file
->_bufsiz
= size
;
4475 file
->_flag
|= MSVCRT__IONBF
;
4477 MSVCRT__unlock_file(file
);
4481 /*********************************************************************
4484 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4486 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4489 /*********************************************************************
4492 char * CDECL
MSVCRT_tmpnam(char *s
)
4499 thread_data_t
*data
= msvcrt_get_thread_data();
4501 if(!data
->tmpnam_buffer
)
4502 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4504 s
= data
->tmpnam_buffer
;
4507 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4508 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4509 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4511 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4512 memcpy(p
, tmpstr
, size
);
4514 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4515 GetLastError() == ERROR_FILE_NOT_FOUND
)
4521 /*********************************************************************
4522 * _wtmpnam (MSVCRT.@)
4524 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4526 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4527 MSVCRT_wchar_t tmpstr
[16];
4531 thread_data_t
*data
= msvcrt_get_thread_data();
4533 if(!data
->wtmpnam_buffer
)
4534 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4536 s
= data
->wtmpnam_buffer
;
4539 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4540 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4541 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4543 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4544 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4546 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4547 GetLastError() == ERROR_FILE_NOT_FOUND
)
4553 /*********************************************************************
4554 * tmpfile (MSVCRT.@)
4556 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4558 char *filename
= MSVCRT_tmpnam(NULL
);
4560 MSVCRT_FILE
* file
= NULL
;
4563 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4564 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4565 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4567 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4572 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4575 if(fd
!= -1 && !file
)
4581 /*********************************************************************
4582 * tmpfile_s (MSVCRT.@)
4584 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4586 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4588 *file
= MSVCRT_tmpfile();
4592 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4594 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4597 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4601 MSVCRT__lock_file(file
);
4603 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4604 ret
= MSVCRT_fwrite(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4605 MSVCRT__unlock_file(file
);
4609 for(i
=0; i
<len
; i
++) {
4610 if(MSVCRT_fputwc(str
[i
], file
) == MSVCRT_WEOF
) {
4611 MSVCRT__unlock_file(file
);
4616 MSVCRT__unlock_file(file
);
4620 /*********************************************************************
4621 * vfprintf (MSVCRT.@)
4623 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4628 MSVCRT__lock_file(file
);
4629 tmp_buf
= add_std_buffer(file
);
4630 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4631 if(tmp_buf
) remove_std_buffer(file
);
4632 MSVCRT__unlock_file(file
);
4637 /*********************************************************************
4638 * vfprintf_s (MSVCRT.@)
4640 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4645 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4647 MSVCRT__lock_file(file
);
4648 tmp_buf
= add_std_buffer(file
);
4649 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4650 if(tmp_buf
) remove_std_buffer(file
);
4651 MSVCRT__unlock_file(file
);
4656 /*********************************************************************
4657 * vfwprintf (MSVCRT.@)
4659 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4664 MSVCRT__lock_file(file
);
4665 tmp_buf
= add_std_buffer(file
);
4666 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4667 if(tmp_buf
) remove_std_buffer(file
);
4668 MSVCRT__unlock_file(file
);
4673 /*********************************************************************
4674 * vfwprintf_s (MSVCRT.@)
4676 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4681 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4683 MSVCRT__lock_file(file
);
4684 tmp_buf
= add_std_buffer(file
);
4685 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4686 if(tmp_buf
) remove_std_buffer(file
);
4687 MSVCRT__unlock_file(file
);
4692 /*********************************************************************
4693 * _vfwprintf_l (MSVCRT.@)
4695 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4696 MSVCRT__locale_t locale
, __ms_va_list valist
)
4701 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4703 MSVCRT__lock_file(file
);
4704 tmp_buf
= add_std_buffer(file
);
4705 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4706 if(tmp_buf
) remove_std_buffer(file
);
4707 MSVCRT__unlock_file(file
);
4712 /*********************************************************************
4713 * vprintf (MSVCRT.@)
4715 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4717 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4720 /*********************************************************************
4721 * vprintf_s (MSVCRT.@)
4723 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4725 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4728 /*********************************************************************
4729 * vwprintf (MSVCRT.@)
4731 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4733 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4736 /*********************************************************************
4737 * vwprintf_s (MSVCRT.@)
4739 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4741 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4744 /*********************************************************************
4745 * fprintf (MSVCRT.@)
4747 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4749 __ms_va_list valist
;
4751 __ms_va_start(valist
, format
);
4752 res
= MSVCRT_vfprintf(file
, format
, valist
);
4753 __ms_va_end(valist
);
4757 /*********************************************************************
4758 * fprintf_s (MSVCRT.@)
4760 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
4762 __ms_va_list valist
;
4764 __ms_va_start(valist
, format
);
4765 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
4766 __ms_va_end(valist
);
4770 /*********************************************************************
4771 * fwprintf (MSVCRT.@)
4773 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4775 __ms_va_list valist
;
4777 __ms_va_start(valist
, format
);
4778 res
= MSVCRT_vfwprintf(file
, format
, valist
);
4779 __ms_va_end(valist
);
4783 /*********************************************************************
4784 * fwprintf_s (MSVCRT.@)
4786 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4788 __ms_va_list valist
;
4790 __ms_va_start(valist
, format
);
4791 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
4792 __ms_va_end(valist
);
4796 /*********************************************************************
4797 * _fwprintf_l (MSVCRT.@)
4799 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
4801 __ms_va_list valist
;
4803 __ms_va_start(valist
, locale
);
4804 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
4805 __ms_va_end(valist
);
4809 /*********************************************************************
4812 int CDECL
MSVCRT_printf(const char *format
, ...)
4814 __ms_va_list valist
;
4816 __ms_va_start(valist
, format
);
4817 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
4818 __ms_va_end(valist
);
4822 /*********************************************************************
4823 * printf_s (MSVCRT.@)
4825 int CDECL
MSVCRT_printf_s(const char *format
, ...)
4827 __ms_va_list valist
;
4829 __ms_va_start(valist
, format
);
4830 res
= MSVCRT_vprintf_s(format
, valist
);
4831 __ms_va_end(valist
);
4835 /*********************************************************************
4838 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
4840 if (c
== MSVCRT_EOF
)
4843 MSVCRT__lock_file(file
);
4844 if(file
->_bufsiz
== 0 && msvcrt_alloc_buffer(file
))
4846 if(file
->_ptr
>file
->_base
) {
4848 if(file
->_flag
& MSVCRT__IOSTRG
) {
4849 if(*file
->_ptr
!= c
) {
4851 MSVCRT__unlock_file(file
);
4858 MSVCRT_clearerr(file
);
4859 MSVCRT__unlock_file(file
);
4863 MSVCRT__unlock_file(file
);
4867 /*********************************************************************
4868 * ungetwc (MSVCRT.@)
4870 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4872 MSVCRT_wchar_t mwc
= wc
;
4874 if (wc
== MSVCRT_WEOF
)
4877 MSVCRT__lock_file(file
);
4879 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
4880 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4881 unsigned char * pp
= (unsigned char *)&mwc
;
4884 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
4885 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
)) {
4886 MSVCRT__unlock_file(file
);
4891 char mbs
[MSVCRT_MB_LEN_MAX
];
4894 len
= MSVCRT_wctomb(mbs
, mwc
);
4896 MSVCRT__unlock_file(file
);
4900 for(len
--; len
>=0; len
--) {
4901 if(mbs
[len
] != MSVCRT_ungetc(mbs
[len
], file
)) {
4902 MSVCRT__unlock_file(file
);
4908 MSVCRT__unlock_file(file
);
4912 /*********************************************************************
4913 * wprintf (MSVCRT.@)
4915 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
4917 __ms_va_list valist
;
4919 __ms_va_start(valist
, format
);
4920 res
= MSVCRT_vwprintf(format
, valist
);
4921 __ms_va_end(valist
);
4925 /*********************************************************************
4926 * wprintf_s (MSVCRT.@)
4928 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
4930 __ms_va_list valist
;
4932 __ms_va_start(valist
, format
);
4933 res
= MSVCRT_vwprintf_s(format
, valist
);
4934 __ms_va_end(valist
);
4938 /*********************************************************************
4939 * _getmaxstdio (MSVCRT.@)
4941 int CDECL
MSVCRT__getmaxstdio(void)
4943 return MSVCRT_max_streams
;
4946 /*********************************************************************
4947 * _setmaxstdio (MSVCRT.@)
4949 int CDECL
MSVCRT__setmaxstdio(int newmax
)
4951 TRACE("%d\n", newmax
);
4953 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
4956 MSVCRT_max_streams
= newmax
;
4957 return MSVCRT_max_streams
;