2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READNL 0x04 /* read started with \n */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
71 /* values for exflag - it's used differently in msvcr90.dll*/
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom
[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
92 CRITICAL_SECTION crit
;
98 BOOL utf8translations
;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
117 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
118 static int MSVCRT_fdend
= 3; /* highest allocated fd */
122 CRITICAL_SECTION crit
;
125 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
126 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
127 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
129 /* INTERNAL: process umask */
130 static int MSVCRT_umask
= 0;
132 /* INTERNAL: static data for tmpnam and _wtmpname functions */
133 static int tmpnam_unique
;
135 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
136 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
137 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
138 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
140 #define TOUL(x) (ULONGLONG)(x)
141 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
142 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
143 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
144 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
146 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
147 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
148 * and MSVCRT_stream_idx, from race conditions.
149 * It doesn't protect against race conditions manipulating the underlying files
150 * or flags; doing so would probably be better accomplished with per-file
151 * protection, rather than locking the whole table for every change.
153 static CRITICAL_SECTION MSVCRT_file_cs
;
154 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
156 0, 0, &MSVCRT_file_cs
,
157 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
158 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
160 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
161 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
162 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
164 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
166 buf
->st_dev
= buf64
->st_dev
;
167 buf
->st_ino
= buf64
->st_ino
;
168 buf
->st_mode
= buf64
->st_mode
;
169 buf
->st_nlink
= buf64
->st_nlink
;
170 buf
->st_uid
= buf64
->st_uid
;
171 buf
->st_gid
= buf64
->st_gid
;
172 buf
->st_rdev
= buf64
->st_rdev
;
173 buf
->st_size
= buf64
->st_size
;
174 buf
->st_atime
= buf64
->st_atime
;
175 buf
->st_mtime
= buf64
->st_mtime
;
176 buf
->st_ctime
= buf64
->st_ctime
;
179 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
181 buf
->st_dev
= buf64
->st_dev
;
182 buf
->st_ino
= buf64
->st_ino
;
183 buf
->st_mode
= buf64
->st_mode
;
184 buf
->st_nlink
= buf64
->st_nlink
;
185 buf
->st_uid
= buf64
->st_uid
;
186 buf
->st_gid
= buf64
->st_gid
;
187 buf
->st_rdev
= buf64
->st_rdev
;
188 buf
->st_size
= buf64
->st_size
;
189 buf
->st_atime
= buf64
->st_atime
;
190 buf
->st_mtime
= buf64
->st_mtime
;
191 buf
->st_ctime
= buf64
->st_ctime
;
194 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
196 buf
->st_dev
= buf64
->st_dev
;
197 buf
->st_ino
= buf64
->st_ino
;
198 buf
->st_mode
= buf64
->st_mode
;
199 buf
->st_nlink
= buf64
->st_nlink
;
200 buf
->st_uid
= buf64
->st_uid
;
201 buf
->st_gid
= buf64
->st_gid
;
202 buf
->st_rdev
= buf64
->st_rdev
;
203 buf
->st_size
= buf64
->st_size
;
204 buf
->st_atime
= buf64
->st_atime
;
205 buf
->st_mtime
= buf64
->st_mtime
;
206 buf
->st_ctime
= buf64
->st_ctime
;
209 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
211 buf
->st_dev
= buf64
->st_dev
;
212 buf
->st_ino
= buf64
->st_ino
;
213 buf
->st_mode
= buf64
->st_mode
;
214 buf
->st_nlink
= buf64
->st_nlink
;
215 buf
->st_uid
= buf64
->st_uid
;
216 buf
->st_gid
= buf64
->st_gid
;
217 buf
->st_rdev
= buf64
->st_rdev
;
218 buf
->st_size
= buf64
->st_size
;
219 buf
->st_atime
= buf64
->st_atime
;
220 buf
->st_mtime
= buf64
->st_mtime
;
221 buf
->st_ctime
= buf64
->st_ctime
;
224 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
226 buf
->st_dev
= buf64
->st_dev
;
227 buf
->st_ino
= buf64
->st_ino
;
228 buf
->st_mode
= buf64
->st_mode
;
229 buf
->st_nlink
= buf64
->st_nlink
;
230 buf
->st_uid
= buf64
->st_uid
;
231 buf
->st_gid
= buf64
->st_gid
;
232 buf
->st_rdev
= buf64
->st_rdev
;
233 buf
->st_size
= buf64
->st_size
;
234 buf
->st_atime
= buf64
->st_atime
;
235 buf
->st_mtime
= buf64
->st_mtime
;
236 buf
->st_ctime
= buf64
->st_ctime
;
239 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
241 /* 1601 to 1970 is 369 years plus 89 leap days */
242 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
244 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
245 ft
->dwHighDateTime
= ticks
>> 32;
246 ft
->dwLowDateTime
= ticks
;
249 static inline ioinfo
* get_ioinfo_nolock(int fd
)
252 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
253 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
255 return &MSVCRT___badioinfo
;
257 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
260 static inline ioinfo
* get_ioinfo(int fd
)
262 ioinfo
*ret
= get_ioinfo_nolock(fd
);
263 if(ret
->exflag
& EF_CRIT_INIT
)
264 EnterCriticalSection(&ret
->crit
);
268 static inline void release_ioinfo(ioinfo
*info
)
270 if(info
->exflag
& EF_CRIT_INIT
)
271 LeaveCriticalSection(&info
->crit
);
274 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
278 if(i
>= MSVCRT_max_streams
)
282 return &MSVCRT__iob
[i
];
284 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
286 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
287 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
288 ERR("out of memory\n");
289 *MSVCRT__errno() = MSVCRT_ENOMEM
;
293 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
295 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
300 static inline BOOL
msvcrt_is_valid_fd(int fd
)
302 return fd
>= 0 && fd
< MSVCRT_fdend
&& (get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
);
305 /* INTERNAL: Get the HANDLE for a fd
306 * This doesn't lock the table, because a failure will result in
307 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
308 * it returns a valid handle which is about to be closed, a subsequent call
309 * will fail, most likely in a sane way.
311 static HANDLE
msvcrt_fdtoh(int fd
)
313 if (!msvcrt_is_valid_fd(fd
))
315 WARN(":fd (%d) - no handle!\n",fd
);
316 *MSVCRT___doserrno() = 0;
317 *MSVCRT__errno() = MSVCRT_EBADF
;
318 return INVALID_HANDLE_VALUE
;
320 if (get_ioinfo_nolock(fd
)->handle
== INVALID_HANDLE_VALUE
)
321 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd
);
322 return get_ioinfo_nolock(fd
)->handle
;
325 /* INTERNAL: free a file entry fd */
326 static void msvcrt_free_fd(int fd
)
331 fdinfo
= get_ioinfo_nolock(fd
);
332 if(fdinfo
!= &MSVCRT___badioinfo
)
334 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
337 TRACE(":fd (%d) freed\n",fd
);
344 SetStdHandle(STD_INPUT_HANDLE
, 0);
347 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
350 SetStdHandle(STD_ERROR_HANDLE
, 0);
355 if (fd
== MSVCRT_fdend
- 1)
357 if (fd
< MSVCRT_fdstart
)
362 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
363 /* caller must hold the files lock */
364 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
368 if (fd
>= MSVCRT_MAX_FILES
)
370 WARN(":files exhausted!\n");
371 *MSVCRT__errno() = MSVCRT_ENFILE
;
375 fdinfo
= get_ioinfo_nolock(fd
);
376 if(fdinfo
== &MSVCRT___badioinfo
) {
379 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
380 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
381 WARN(":out of memory!\n");
382 *MSVCRT__errno() = MSVCRT_ENOMEM
;
386 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
387 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
389 fdinfo
= get_ioinfo_nolock(fd
);
392 fdinfo
->handle
= hand
;
393 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
394 fdinfo
->lookahead
[0] = '\n';
395 fdinfo
->lookahead
[1] = '\n';
396 fdinfo
->lookahead
[2] = '\n';
397 if(!(fdinfo
->exflag
& EF_CRIT_INIT
))
398 InitializeCriticalSection(&fdinfo
->crit
);
399 fdinfo
->exflag
= EF_CRIT_INIT
;
401 /* locate next free slot */
402 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
403 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
405 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
406 get_ioinfo_nolock(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
408 /* update last fd in use */
409 if (fd
>= MSVCRT_fdend
)
410 MSVCRT_fdend
= fd
+ 1;
411 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
415 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
416 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
417 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
423 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
424 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
429 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
430 ret
= msvcrt_set_fd(hand
, flag
, MSVCRT_fdstart
);
435 /* INTERNAL: Allocate a FILE* for an fd slot */
436 /* caller must hold the files lock */
437 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
442 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
444 file
= msvcrt_get_file(i
);
448 if (file
->_flag
== 0)
450 if (i
== MSVCRT_stream_idx
)
452 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
454 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
455 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
466 /* INTERNAL: initialize a FILE* from an open fd */
467 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
469 TRACE(":fd (%d) allocating FILE*\n",fd
);
470 if (!msvcrt_is_valid_fd(fd
))
472 WARN(":invalid fd %d\n",fd
);
473 *MSVCRT___doserrno() = 0;
474 *MSVCRT__errno() = MSVCRT_EBADF
;
477 file
->_ptr
= file
->_base
= NULL
;
480 file
->_flag
= stream_flags
;
481 file
->_tmpfname
= NULL
;
483 TRACE(":got FILE* (%p)\n",file
);
487 /* INTERNAL: Create an inheritance data block (for spawned process)
488 * The inheritance block is made of:
489 * 00 int nb of file descriptor (NBFD)
490 * 04 char file flags (wxflag): repeated for each fd
491 * 4+NBFD HANDLE file handle: repeated for each fd
493 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
500 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
501 *block
= MSVCRT_calloc(*size
, 1);
507 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
508 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
510 *(unsigned*)*block
= MSVCRT_fdend
;
511 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
513 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
514 fdinfo
= get_ioinfo_nolock(fd
);
515 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
517 *wxflag_ptr
= fdinfo
->wxflag
;
518 *handle_ptr
= fdinfo
->handle
;
523 *handle_ptr
= INVALID_HANDLE_VALUE
;
525 wxflag_ptr
++; handle_ptr
++;
530 /* INTERNAL: Set up all file descriptors,
531 * as well as default streams (stdin, stderr and stdout)
533 void msvcrt_init_io(void)
539 GetStartupInfoA(&si
);
540 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
546 count
= *(unsigned*)si
.lpReserved2
;
547 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
548 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
550 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
551 count
= min(count
, MSVCRT_MAX_FILES
);
552 for (i
= 0; i
< count
; i
++)
554 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
555 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
557 wxflag_ptr
++; handle_ptr
++;
559 MSVCRT_fdend
= max( 3, count
);
560 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
561 if (get_ioinfo_nolock(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
564 fdinfo
= get_ioinfo_nolock(MSVCRT_STDIN_FILENO
);
565 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
566 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
567 DWORD type
= GetFileType(h
);
569 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
570 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDIN_FILENO
);
573 fdinfo
= get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
);
574 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
575 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
576 DWORD type
= GetFileType(h
);
578 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
579 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDOUT_FILENO
);
582 fdinfo
= get_ioinfo_nolock(MSVCRT_STDERR_FILENO
);
583 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
584 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
585 DWORD type
= GetFileType(h
);
587 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
588 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDERR_FILENO
);
591 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
592 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
593 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
595 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
596 for (i
= 0; i
< 3; i
++)
598 /* FILE structs for stdin/out/err are static and never deleted */
599 MSVCRT__iob
[i
]._file
= i
;
600 MSVCRT__iob
[i
]._tmpfname
= NULL
;
601 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
603 MSVCRT_stream_idx
= 3;
606 /* INTERNAL: Flush stdio file buffer */
607 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
609 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
610 int cnt
=file
->_ptr
-file
->_base
;
611 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
612 file
->_flag
|= MSVCRT__IOERR
;
615 file
->_ptr
=file
->_base
;
621 /*********************************************************************
624 int CDECL
MSVCRT__isatty(int fd
)
626 TRACE(":fd (%d)\n",fd
);
628 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
631 /* INTERNAL: Allocate stdio file buffer */
632 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
634 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
635 && MSVCRT__isatty(file
->_file
))
638 file
->_base
= MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
640 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
641 file
->_flag
|= MSVCRT__IOMYBUF
;
643 file
->_base
= (char*)(&file
->_charbuf
);
645 file
->_flag
|= MSVCRT__IONBF
;
647 file
->_ptr
= file
->_base
;
652 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
653 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
655 static char buffers
[2][MSVCRT_BUFSIZ
];
657 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
658 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
659 || !MSVCRT__isatty(file
->_file
))
662 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
663 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
664 file
->_flag
|= MSVCRT__USERBUF
;
668 /* INTERNAL: Removes temporary buffer from stdout or stderr */
669 /* Only call this function when add_std_buffer returned TRUE */
670 static void remove_std_buffer(MSVCRT_FILE
*file
)
672 msvcrt_flush_buffer(file
);
673 file
->_ptr
= file
->_base
= NULL
;
674 file
->_bufsiz
= file
->_cnt
= 0;
675 file
->_flag
&= ~MSVCRT__USERBUF
;
678 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
679 static int msvcrt_int_to_base32(int num
, char *str
)
694 *p
= (num
& 31) + '0';
696 *p
+= ('a' - '0' - 10);
703 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
704 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
719 *p
= (num
& 31) + '0';
721 *p
+= ('a' - '0' - 10);
728 /*********************************************************************
729 * __iob_func(MSVCRT.@)
731 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
733 return &MSVCRT__iob
[0];
736 /*********************************************************************
739 int CDECL
MSVCRT__access(const char *filename
, int mode
)
741 DWORD attr
= GetFileAttributesA(filename
);
743 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
745 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
747 msvcrt_set_errno(GetLastError());
750 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
752 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
758 /*********************************************************************
759 * _access_s (MSVCRT.@)
761 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
763 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
764 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
766 if (MSVCRT__access(filename
, mode
) == -1)
767 return *MSVCRT__errno();
771 /*********************************************************************
772 * _waccess (MSVCRT.@)
774 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
776 DWORD attr
= GetFileAttributesW(filename
);
778 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
780 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
782 msvcrt_set_errno(GetLastError());
785 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
787 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
793 /*********************************************************************
794 * _waccess_s (MSVCRT.@)
796 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
798 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
799 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
801 if (MSVCRT__waccess(filename
, mode
) == -1)
802 return *MSVCRT__errno();
806 /*********************************************************************
809 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
811 DWORD oldFlags
= GetFileAttributesA(path
);
813 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
815 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
816 oldFlags
| FILE_ATTRIBUTE_READONLY
;
818 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
821 msvcrt_set_errno(GetLastError());
825 /*********************************************************************
828 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
830 DWORD oldFlags
= GetFileAttributesW(path
);
832 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
834 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
835 oldFlags
| FILE_ATTRIBUTE_READONLY
;
837 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
840 msvcrt_set_errno(GetLastError());
844 /*********************************************************************
847 int CDECL
MSVCRT__unlink(const char *path
)
849 TRACE("%s\n",debugstr_a(path
));
850 if(DeleteFileA(path
))
852 TRACE("failed (%d)\n",GetLastError());
853 msvcrt_set_errno(GetLastError());
857 /*********************************************************************
858 * _wunlink (MSVCRT.@)
860 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
862 TRACE("(%s)\n",debugstr_w(path
));
863 if(DeleteFileW(path
))
865 TRACE("failed (%d)\n",GetLastError());
866 msvcrt_set_errno(GetLastError());
870 /*********************************************************************
873 int CDECL
MSVCRT__commit(int fd
)
875 ioinfo
*info
= get_ioinfo(fd
);
878 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
880 if (info
->handle
== INVALID_HANDLE_VALUE
)
882 else if (!FlushFileBuffers(info
->handle
))
884 if (GetLastError() == ERROR_INVALID_HANDLE
)
886 /* FlushFileBuffers fails for console handles
887 * so we ignore this error.
893 TRACE(":failed-last error (%d)\n",GetLastError());
894 msvcrt_set_errno(GetLastError());
904 release_ioinfo(info
);
908 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
909 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
911 /* INTERNAL: Flush all stream buffer */
912 static int msvcrt_flush_all_buffers(int mask
)
914 int i
, num_flushed
= 0;
918 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
919 file
= msvcrt_get_file(i
);
923 if(file
->_flag
& mask
) {
931 TRACE(":flushed (%d) handles\n",num_flushed
);
935 /*********************************************************************
936 * _flushall (MSVCRT.@)
938 int CDECL
MSVCRT__flushall(void)
940 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
943 /*********************************************************************
946 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
951 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
954 MSVCRT__lock_file(file
);
955 ret
= MSVCRT__fflush_nolock(file
);
956 MSVCRT__unlock_file(file
);
962 /*********************************************************************
963 * _fflush_nolock (MSVCRT.@)
965 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
968 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
969 } else if(file
->_flag
& MSVCRT__IOWRT
) {
972 res
= msvcrt_flush_buffer(file
);
973 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
974 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
977 } else if(file
->_flag
& MSVCRT__IOREAD
) {
979 file
->_ptr
= file
->_base
;
986 /*********************************************************************
989 int CDECL
MSVCRT__close(int fd
)
991 ioinfo
*info
= get_ioinfo(fd
);
995 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
996 if (!(info
->wxflag
& WX_OPEN
)) {
999 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1002 WARN(":failed-last error (%d)\n",GetLastError());
1003 msvcrt_set_errno(GetLastError());
1007 release_ioinfo(info
);
1011 /*********************************************************************
1014 * MSDN isn't clear on this point, but the remarks for _pipe
1015 * indicate file descriptors duplicated with _dup and _dup2 are always
1018 int CDECL
MSVCRT__dup2(int od
, int nd
)
1022 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1024 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
1028 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od
)->handle
,
1029 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1031 int wxflag
= get_ioinfo_nolock(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
1033 if (msvcrt_is_valid_fd(nd
))
1035 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
1038 CloseHandle(handle
);
1039 *MSVCRT__errno() = MSVCRT_EMFILE
;
1043 /* _dup2 returns 0, not nd, on success */
1050 msvcrt_set_errno(GetLastError());
1055 *MSVCRT__errno() = MSVCRT_EBADF
;
1062 /*********************************************************************
1065 int CDECL
MSVCRT__dup(int od
)
1070 fd
= MSVCRT_fdstart
;
1071 if (MSVCRT__dup2(od
, fd
) == 0)
1079 /*********************************************************************
1082 int CDECL
MSVCRT__eof(int fd
)
1084 DWORD curpos
,endpos
;
1085 LONG hcurpos
,hendpos
;
1086 HANDLE hand
= msvcrt_fdtoh(fd
);
1088 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1090 if (hand
== INVALID_HANDLE_VALUE
)
1093 if (get_ioinfo_nolock(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1095 /* Otherwise we do it the hard way */
1096 hcurpos
= hendpos
= 0;
1097 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1098 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1100 if (curpos
== endpos
&& hcurpos
== hendpos
)
1102 /* FIXME: shouldn't WX_ATEOF be set here? */
1106 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1110 /*********************************************************************
1111 * _fcloseall (MSVCRT.@)
1113 int CDECL
MSVCRT__fcloseall(void)
1115 int num_closed
= 0, i
;
1119 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1120 file
= msvcrt_get_file(i
);
1122 if (file
->_flag
&& !MSVCRT_fclose(file
))
1127 TRACE(":closed (%d) handles\n",num_closed
);
1131 /* free everything on process exit */
1132 void msvcrt_free_io(void)
1138 MSVCRT__fcloseall();
1140 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1142 if(!MSVCRT___pioinfo
[i
])
1145 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1147 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1148 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1150 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1153 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1155 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1156 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1158 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1159 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1163 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1164 MSVCRT_free(MSVCRT_fstream
[i
]);
1166 DeleteCriticalSection(&MSVCRT_file_cs
);
1169 /*********************************************************************
1170 * _lseeki64 (MSVCRT.@)
1172 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1174 HANDLE hand
= msvcrt_fdtoh(fd
);
1177 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1178 if (hand
== INVALID_HANDLE_VALUE
)
1181 if (whence
< 0 || whence
> 2)
1183 *MSVCRT__errno() = MSVCRT_EINVAL
;
1187 TRACE(":fd (%d) to %s pos %s\n",
1188 fd
,wine_dbgstr_longlong(offset
),
1189 (whence
==SEEK_SET
)?"SEEK_SET":
1190 (whence
==SEEK_CUR
)?"SEEK_CUR":
1191 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1193 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1194 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1195 ofs
.QuadPart
= offset
;
1196 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1197 GetLastError() == ERROR_SUCCESS
)
1199 get_ioinfo_nolock(fd
)->wxflag
&= ~WX_ATEOF
;
1200 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1202 return ofs
.QuadPart
;
1204 TRACE(":error-last error (%d)\n",GetLastError());
1205 msvcrt_set_errno(GetLastError());
1209 /*********************************************************************
1212 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1214 return MSVCRT__lseeki64(fd
, offset
, whence
);
1217 /*********************************************************************
1218 * _lock_file (MSVCRT.@)
1220 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1222 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1223 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1225 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1228 /*********************************************************************
1229 * _unlock_file (MSVCRT.@)
1231 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1233 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1234 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1236 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1239 /*********************************************************************
1240 * _locking (MSVCRT.@)
1242 * This is untested; the underlying LockFile doesn't work yet.
1244 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1248 HANDLE hand
= msvcrt_fdtoh(fd
);
1250 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1251 if (hand
== INVALID_HANDLE_VALUE
)
1254 if (mode
< 0 || mode
> 4)
1256 *MSVCRT__errno() = MSVCRT_EINVAL
;
1260 TRACE(":fd (%d) by 0x%08x mode %s\n",
1261 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1262 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1263 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1264 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1265 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1268 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1270 FIXME ("Seek failed\n");
1271 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1274 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1277 ret
= 1; /* just to satisfy gcc */
1280 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1285 else if (mode
== MSVCRT__LK_UNLCK
)
1286 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1288 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1289 /* FIXME - what about error settings? */
1290 return ret
? 0 : -1;
1293 /*********************************************************************
1294 * _fseeki64 (MSVCRT.@)
1296 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1300 MSVCRT__lock_file(file
);
1301 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1302 MSVCRT__unlock_file(file
);
1307 /*********************************************************************
1308 * _fseeki64_nolock (MSVCRT.@)
1310 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1314 /* Flush output if needed */
1315 if(file
->_flag
& MSVCRT__IOWRT
)
1316 msvcrt_flush_buffer(file
);
1318 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1320 offset
+= MSVCRT__ftelli64_nolock(file
);
1323 /* Discard buffered input */
1325 file
->_ptr
= file
->_base
;
1326 /* Reset direction of i/o */
1327 if(file
->_flag
& MSVCRT__IORW
) {
1328 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1330 /* Clear end of file flag */
1331 file
->_flag
&= ~MSVCRT__IOEOF
;
1332 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1337 /*********************************************************************
1340 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1342 return MSVCRT__fseeki64( file
, offset
, whence
);
1345 /*********************************************************************
1346 * _fseek_nolock (MSVCRT.@)
1348 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1350 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1353 /*********************************************************************
1354 * _chsize_s (MSVCRT.@)
1356 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1362 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1364 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1368 handle
= msvcrt_fdtoh(fd
);
1369 if (handle
!= INVALID_HANDLE_VALUE
)
1371 /* save the current file pointer */
1372 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1375 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1378 ret
= SetEndOfFile(handle
);
1379 if (!ret
) msvcrt_set_errno(GetLastError());
1382 /* restore the file pointer */
1383 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1388 return ret
? 0 : *MSVCRT__errno();
1391 /*********************************************************************
1392 * _chsize (MSVCRT.@)
1394 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1396 /* _chsize_s returns errno on failure but _chsize should return -1 */
1397 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1400 /*********************************************************************
1401 * clearerr (MSVCRT.@)
1403 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1405 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1407 MSVCRT__lock_file(file
);
1408 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1409 MSVCRT__unlock_file(file
);
1412 /*********************************************************************
1415 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1417 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1419 MSVCRT__lock_file(file
);
1420 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1421 MSVCRT_clearerr(file
);
1422 MSVCRT__unlock_file(file
);
1425 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1427 int plus
= strchrW(mode
, '+') != NULL
;
1429 TRACE("%s\n", debugstr_w(mode
));
1431 while(*mode
== ' ') mode
++;
1436 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1437 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1440 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1441 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1444 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1445 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1448 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1452 *stream_flags
|= MSVCRT__commode
;
1454 while (*mode
&& *mode
!=',')
1458 *open_flags
|= MSVCRT__O_BINARY
;
1459 *open_flags
&= ~MSVCRT__O_TEXT
;
1462 *open_flags
|= MSVCRT__O_TEXT
;
1463 *open_flags
&= ~MSVCRT__O_BINARY
;
1466 *open_flags
|= MSVCRT__O_TEMPORARY
;
1469 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1472 *stream_flags
|= MSVCRT__IOCOMMIT
;
1475 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1478 *open_flags
|= MSVCRT__O_NOINHERIT
;
1487 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1490 ERR("incorrect mode flag: %c\n", mode
[-1]);
1496 static const WCHAR ccs
[] = {'c','c','s'};
1497 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1498 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1499 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1502 while(*mode
== ' ') mode
++;
1503 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1505 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1506 while(*mode
== ' ') mode
++;
1507 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1510 while(*mode
== ' ') mode
++;
1512 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1514 *open_flags
|= MSVCRT__O_U8TEXT
;
1515 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1517 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1519 *open_flags
|= MSVCRT__O_U16TEXT
;
1520 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1522 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1524 *open_flags
|= MSVCRT__O_WTEXT
;
1525 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1529 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1533 while(*mode
== ' ') mode
++;
1536 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1541 /*********************************************************************
1542 * _fdopen (MSVCRT.@)
1544 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1547 MSVCRT_wchar_t
*modeW
= NULL
;
1549 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1551 ret
= MSVCRT__wfdopen(fd
, modeW
);
1557 /*********************************************************************
1558 * _wfdopen (MSVCRT.@)
1560 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1562 int open_flags
, stream_flags
;
1565 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1568 if (!(file
= msvcrt_alloc_fp()))
1570 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1575 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1581 /*********************************************************************
1582 * _filelength (MSVCRT.@)
1584 LONG CDECL
MSVCRT__filelength(int fd
)
1586 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1589 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1592 if (endPos
!= curPos
)
1593 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1600 /*********************************************************************
1601 * _filelengthi64 (MSVCRT.@)
1603 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1605 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1608 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1611 if (endPos
!= curPos
)
1612 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1619 /*********************************************************************
1620 * _fileno (MSVCRT.@)
1622 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1624 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1628 /*********************************************************************
1629 * _fstat64 (MSVCRT.@)
1631 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1635 BY_HANDLE_FILE_INFORMATION hfi
;
1636 HANDLE hand
= msvcrt_fdtoh(fd
);
1638 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1639 if (hand
== INVALID_HANDLE_VALUE
)
1644 WARN(":failed-NULL buf\n");
1645 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1649 memset(&hfi
, 0, sizeof(hfi
));
1650 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1651 type
= GetFileType(hand
);
1652 if (type
== FILE_TYPE_PIPE
)
1654 buf
->st_dev
= buf
->st_rdev
= fd
;
1655 buf
->st_mode
= S_IFIFO
;
1658 else if (type
== FILE_TYPE_CHAR
)
1660 buf
->st_dev
= buf
->st_rdev
= fd
;
1661 buf
->st_mode
= S_IFCHR
;
1664 else /* FILE_TYPE_DISK etc. */
1666 if (!GetFileInformationByHandle(hand
, &hfi
))
1668 WARN(":failed-last error (%d)\n",GetLastError());
1669 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1672 buf
->st_mode
= S_IFREG
| 0444;
1673 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1674 buf
->st_mode
|= 0222;
1675 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1676 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1678 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1679 buf
->st_mtime
= buf
->st_ctime
= dw
;
1680 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1682 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1687 /*********************************************************************
1688 * _fstati64 (MSVCRT.@)
1690 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1693 struct MSVCRT__stat64 buf64
;
1695 ret
= MSVCRT__fstat64(fd
, &buf64
);
1697 msvcrt_stat64_to_stati64(&buf64
, buf
);
1701 /*********************************************************************
1704 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1706 struct MSVCRT__stat64 buf64
;
1708 ret
= MSVCRT__fstat64(fd
, &buf64
);
1710 msvcrt_stat64_to_stat(&buf64
, buf
);
1714 /*********************************************************************
1715 * _fstat32 (MSVCR80.@)
1717 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1720 struct MSVCRT__stat64 buf64
;
1722 ret
= MSVCRT__fstat64(fd
, &buf64
);
1724 msvcrt_stat64_to_stat32(&buf64
, buf
);
1728 /*********************************************************************
1729 * _fstat64i32 (MSVCR80.@)
1731 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1734 struct MSVCRT__stat64 buf64
;
1736 ret
= MSVCRT__fstat64(fd
, &buf64
);
1738 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1742 /*********************************************************************
1743 * _futime64 (MSVCRT.@)
1745 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1747 HANDLE hand
= msvcrt_fdtoh(fd
);
1752 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1757 time_to_filetime( t
->actime
, &at
);
1758 time_to_filetime( t
->modtime
, &wt
);
1761 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1763 msvcrt_set_errno(GetLastError());
1769 /*********************************************************************
1770 * _futime32 (MSVCRT.@)
1772 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1776 struct MSVCRT___utimbuf64 t64
;
1777 t64
.actime
= t
->actime
;
1778 t64
.modtime
= t
->modtime
;
1779 return _futime64( fd
, &t64
);
1782 return _futime64( fd
, NULL
);
1785 /*********************************************************************
1786 * _futime (MSVCRT.@)
1789 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1791 return _futime64( fd
, t
);
1794 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1796 return _futime32( fd
, t
);
1800 /*********************************************************************
1801 * _get_osfhandle (MSVCRT.@)
1803 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1805 HANDLE hand
= msvcrt_fdtoh(fd
);
1806 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1808 return (MSVCRT_intptr_t
)hand
;
1811 /*********************************************************************
1812 * _mktemp_s (MSVCRT.@)
1814 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1818 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1819 return MSVCRT_EINVAL
;
1821 for(len
=0; len
<size
; len
++)
1824 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1827 return MSVCRT_EINVAL
;
1830 for(xno
=1; xno
<=6; xno
++)
1831 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1832 return MSVCRT_EINVAL
;
1834 id
= GetCurrentProcessId();
1835 for(xno
=1; xno
<6; xno
++) {
1836 pattern
[len
-xno
] = id
%10 + '0';
1840 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1841 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1846 *MSVCRT__errno() = MSVCRT_EEXIST
;
1847 return MSVCRT_EEXIST
;
1850 /*********************************************************************
1851 * _mktemp (MSVCRT.@)
1853 char * CDECL
MSVCRT__mktemp(char *pattern
)
1856 char *retVal
= pattern
;
1864 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1868 id
= GetCurrentProcessId();
1872 int tempNum
= id
/ 10;
1873 *pattern
-- = id
- (tempNum
* 10) + '0';
1879 *pattern
= letter
++;
1880 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1882 } while(letter
<= 'z');
1886 /*********************************************************************
1887 * _wmktemp_s (MSVCRT.@)
1889 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1893 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1894 return MSVCRT_EINVAL
;
1896 for(len
=0; len
<size
; len
++)
1899 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1902 return MSVCRT_EINVAL
;
1905 for(xno
=1; xno
<=6; xno
++)
1906 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1907 return MSVCRT_EINVAL
;
1909 id
= GetCurrentProcessId();
1910 for(xno
=1; xno
<6; xno
++) {
1911 pattern
[len
-xno
] = id
%10 + '0';
1915 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1916 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1921 *MSVCRT__errno() = MSVCRT_EEXIST
;
1922 return MSVCRT_EEXIST
;
1925 /*********************************************************************
1926 * _wmktemp (MSVCRT.@)
1928 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1931 MSVCRT_wchar_t
*retVal
= pattern
;
1933 MSVCRT_wchar_t letter
= 'a';
1939 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1943 id
= GetCurrentProcessId();
1947 int tempNum
= id
/ 10;
1948 *pattern
-- = id
- (tempNum
* 10) + '0';
1954 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1956 *pattern
= letter
++;
1957 } while(letter
!= '|');
1961 static unsigned split_oflags(unsigned oflags
)
1964 unsigned unsupp
; /* until we support everything */
1966 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1967 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1968 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1969 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1970 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1971 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1972 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1973 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1974 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1976 if ((unsupp
= oflags
& ~(
1977 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1978 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1979 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1980 MSVCRT__O_NOINHERIT
|
1981 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
1982 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
1984 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1989 /*********************************************************************
1992 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1995 SECURITY_ATTRIBUTES sa
;
1996 HANDLE readHandle
, writeHandle
;
2000 *MSVCRT__errno() = MSVCRT_EINVAL
;
2004 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2005 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2006 sa
.lpSecurityDescriptor
= NULL
;
2007 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2009 unsigned int wxflags
= split_oflags(textmode
);
2013 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2017 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2025 MSVCRT__close(pfds
[0]);
2026 CloseHandle(writeHandle
);
2027 *MSVCRT__errno() = MSVCRT_EMFILE
;
2032 CloseHandle(readHandle
);
2033 CloseHandle(writeHandle
);
2034 *MSVCRT__errno() = MSVCRT_EMFILE
;
2039 msvcrt_set_errno(GetLastError());
2044 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2046 char bom
[sizeof(utf8_bom
)];
2049 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2051 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2054 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2055 oflags
|= MSVCRT__O_U8TEXT
;
2056 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2058 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2059 oflags
|= MSVCRT__O_U16TEXT
;
2061 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2067 /*********************************************************************
2068 * _wsopen_s (MSVCRT.@)
2070 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2072 DWORD access
= 0, creation
= 0, attrib
;
2073 SECURITY_ATTRIBUTES sa
;
2074 DWORD sharing
, type
;
2078 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2079 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2081 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2084 wxflag
= split_oflags(oflags
);
2085 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2087 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2088 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2089 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2092 if (oflags
& MSVCRT__O_CREAT
)
2094 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2095 FIXME(": pmode 0x%04x ignored\n", pmode
);
2097 WARN(": pmode 0x%04x ignored\n", pmode
);
2099 if (oflags
& MSVCRT__O_EXCL
)
2100 creation
= CREATE_NEW
;
2101 else if (oflags
& MSVCRT__O_TRUNC
)
2102 creation
= CREATE_ALWAYS
;
2104 creation
= OPEN_ALWAYS
;
2106 else /* no MSVCRT__O_CREAT */
2108 if (oflags
& MSVCRT__O_TRUNC
)
2109 creation
= TRUNCATE_EXISTING
;
2111 creation
= OPEN_EXISTING
;
2116 case MSVCRT__SH_DENYRW
:
2119 case MSVCRT__SH_DENYWR
:
2120 sharing
= FILE_SHARE_READ
;
2122 case MSVCRT__SH_DENYRD
:
2123 sharing
= FILE_SHARE_WRITE
;
2125 case MSVCRT__SH_DENYNO
:
2126 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2129 ERR( "Unhandled shflags 0x%x\n", shflags
);
2130 return MSVCRT_EINVAL
;
2132 attrib
= FILE_ATTRIBUTE_NORMAL
;
2134 if (oflags
& MSVCRT__O_TEMPORARY
)
2136 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2138 sharing
|= FILE_SHARE_DELETE
;
2141 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2142 sa
.lpSecurityDescriptor
= NULL
;
2143 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2145 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2146 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2147 && !(access
&GENERIC_READ
))
2149 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2150 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2151 if (hand
!= INVALID_HANDLE_VALUE
)
2153 oflags
= check_bom(hand
, oflags
, FALSE
);
2157 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2160 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2161 if (hand
== INVALID_HANDLE_VALUE
) {
2162 WARN(":failed-last error (%d)\n",GetLastError());
2163 msvcrt_set_errno(GetLastError());
2164 return *MSVCRT__errno();
2167 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2169 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2170 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2171 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2173 if (oflags
& MSVCRT__O_U8TEXT
)
2175 DWORD written
= 0, tmp
;
2177 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2178 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2180 if (written
!= sizeof(utf8_bom
)) {
2181 WARN("error writing BOM\n");
2183 msvcrt_set_errno(GetLastError());
2184 return *MSVCRT__errno();
2189 DWORD written
= 0, tmp
;
2191 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2192 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2194 if (written
!= sizeof(utf16_bom
))
2196 WARN("error writing BOM\n");
2198 msvcrt_set_errno(GetLastError());
2199 return *MSVCRT__errno();
2203 else if (access
& GENERIC_READ
)
2204 oflags
= check_bom(hand
, oflags
, TRUE
);
2207 type
= GetFileType(hand
);
2208 if (type
== FILE_TYPE_CHAR
)
2210 else if (type
== FILE_TYPE_PIPE
)
2213 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2215 return *MSVCRT__errno();
2217 if (oflags
& MSVCRT__O_WTEXT
)
2218 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2219 else if (oflags
& MSVCRT__O_U16TEXT
)
2220 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2221 else if (oflags
& MSVCRT__O_U8TEXT
)
2222 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2224 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2228 /*********************************************************************
2229 * _wsopen (MSVCRT.@)
2231 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2236 if (oflags
& MSVCRT__O_CREAT
)
2240 __ms_va_start(ap
, shflags
);
2241 pmode
= va_arg(ap
, int);
2247 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2251 /*********************************************************************
2252 * _sopen_s (MSVCRT.@)
2254 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2256 MSVCRT_wchar_t
*pathW
;
2259 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2260 return MSVCRT_EINVAL
;
2262 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2263 return MSVCRT_EINVAL
;
2265 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2270 /*********************************************************************
2273 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2278 if (oflags
& MSVCRT__O_CREAT
)
2282 __ms_va_start(ap
, shflags
);
2283 pmode
= va_arg(ap
, int);
2289 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2293 /*********************************************************************
2296 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2300 if (flags
& MSVCRT__O_CREAT
)
2303 __ms_va_start(ap
, flags
);
2304 pmode
= va_arg(ap
, int);
2306 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2309 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2312 /*********************************************************************
2315 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2319 if (flags
& MSVCRT__O_CREAT
)
2322 __ms_va_start(ap
, flags
);
2323 pmode
= va_arg(ap
, int);
2325 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2328 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2331 /*********************************************************************
2334 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2336 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2337 return MSVCRT__open(path
, usedFlags
);
2340 /*********************************************************************
2341 * _wcreat (MSVCRT.@)
2343 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2345 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2346 return MSVCRT__wopen(path
, usedFlags
);
2349 /*********************************************************************
2350 * _open_osfhandle (MSVCRT.@)
2352 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2357 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2358 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2359 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2360 * text - it never sets MSVCRT__O_BINARY.
2362 /* don't let split_oflags() decide the mode if no mode is passed */
2363 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2364 oflags
|= MSVCRT__O_BINARY
;
2366 flags
= GetFileType((HANDLE
)handle
);
2367 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2369 msvcrt_set_errno(GetLastError());
2373 if (flags
== FILE_TYPE_CHAR
)
2375 else if (flags
== FILE_TYPE_PIPE
)
2379 flags
|= split_oflags(oflags
);
2381 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2382 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2386 /*********************************************************************
2389 int CDECL
MSVCRT__rmtmp(void)
2391 int num_removed
= 0, i
;
2395 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2396 file
= msvcrt_get_file(i
);
2398 if (file
->_tmpfname
)
2400 MSVCRT_fclose(file
);
2407 TRACE(":removed (%d) temp files\n",num_removed
);
2411 static inline int get_utf8_char_len(char ch
)
2413 if((ch
&0xf8) == 0xf0)
2415 else if((ch
&0xf0) == 0xe0)
2417 else if((ch
&0xe0) == 0xc0)
2422 /*********************************************************************
2423 * (internal) read_utf8
2425 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2427 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2428 HANDLE hand
= fdinfo
->handle
;
2429 char min_buf
[4], *readbuf
, lookahead
;
2430 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2432 /* make the buffer big enough to hold at least one character */
2433 /* read bytes have to fit to output and lookahead buffers */
2435 readbuf_size
= count
< 4 ? 4 : count
;
2436 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2441 if(fdinfo
->lookahead
[0] != '\n') {
2442 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2443 fdinfo
->lookahead
[0] = '\n';
2445 if(fdinfo
->lookahead
[1] != '\n') {
2446 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2447 fdinfo
->lookahead
[1] = '\n';
2449 if(fdinfo
->lookahead
[2] != '\n') {
2450 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2451 fdinfo
->lookahead
[2] = '\n';
2456 /* NOTE: this case is broken in native dll, reading
2457 * sometimes fails when small buffer is passed
2460 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2461 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2462 fdinfo
->wxflag
|= WX_ATEOF
;
2465 msvcrt_set_errno(GetLastError());
2468 }else if(!num_read
) {
2469 fdinfo
->wxflag
|= WX_ATEOF
;
2475 char_len
= get_utf8_char_len(readbuf
[0]);
2477 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2481 if(readbuf
[0] == '\n')
2482 fdinfo
->wxflag
|= WX_READNL
;
2484 fdinfo
->wxflag
&= ~WX_READNL
;
2486 if(readbuf
[0] == 0x1a) {
2487 fdinfo
->wxflag
|= WX_ATEOF
;
2491 if(readbuf
[0] == '\r') {
2492 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2494 else if(lookahead
== '\n')
2498 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2499 fdinfo
->lookahead
[0] = lookahead
;
2501 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2506 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2507 msvcrt_set_errno(GetLastError());
2514 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2517 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2518 fdinfo
->wxflag
|= WX_ATEOF
;
2519 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2522 msvcrt_set_errno(GetLastError());
2523 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2526 }else if(!pos
&& !num_read
) {
2527 fdinfo
->wxflag
|= WX_ATEOF
;
2528 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2533 if(readbuf
[0] == '\n')
2534 fdinfo
->wxflag
|= WX_READNL
;
2536 fdinfo
->wxflag
&= ~WX_READNL
;
2538 /* Find first byte of last character (may be incomplete) */
2539 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2540 if((readbuf
[i
]&0xc0) != 0x80)
2542 char_len
= get_utf8_char_len(readbuf
[i
]);
2543 if(char_len
+i
<= pos
)
2546 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2548 fdinfo
->lookahead
[0] = readbuf
[i
];
2550 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2552 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2554 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2558 for(i
=0, j
=0; i
<pos
; i
++) {
2559 if(readbuf
[i
] == 0x1a) {
2560 fdinfo
->wxflag
|= WX_ATEOF
;
2564 /* strip '\r' if followed by '\n' */
2565 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2566 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2567 readbuf
[j
++] = '\r';
2568 }else if(lookahead
== '\n' && j
==0) {
2569 readbuf
[j
++] = '\n';
2571 if(lookahead
!= '\n')
2572 readbuf
[j
++] = '\r';
2574 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2575 fdinfo
->lookahead
[0] = lookahead
;
2577 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2579 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2580 readbuf
[j
++] = readbuf
[i
];
2585 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2586 msvcrt_set_errno(GetLastError());
2587 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2591 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2595 /*********************************************************************
2598 * When reading \r as last character in text mode, read() positions
2599 * the file pointer on the \r character while getc() goes on to
2602 static int read_i(int fd
, void *buf
, unsigned int count
)
2604 DWORD num_read
, utf16
;
2605 char *bufstart
= buf
;
2606 HANDLE hand
= msvcrt_fdtoh(fd
);
2607 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2612 if (fdinfo
->wxflag
& WX_ATEOF
) {
2613 TRACE("already at EOF, returning 0\n");
2616 /* Don't trace small reads, it gets *very* annoying */
2618 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2619 if (hand
== INVALID_HANDLE_VALUE
)
2621 *MSVCRT__errno() = MSVCRT_EBADF
;
2625 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2626 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2628 *MSVCRT__errno() = MSVCRT_EINVAL
;
2632 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2633 return read_utf8(fd
, buf
, count
);
2635 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2637 if (fdinfo
->lookahead
[0] != '\n')
2639 bufstart
[0] = fdinfo
->lookahead
[0];
2640 fdinfo
->lookahead
[0] = '\n';
2644 bufstart
[1] = fdinfo
->lookahead
[1];
2645 fdinfo
->lookahead
[1] = '\n';
2648 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2649 num_read
+= 1+utf16
;
2654 if(utf16
&& (num_read
&1))
2656 /* msvcr90 uses uninitialized value from the buffer in this case */
2657 /* msvcrt ignores additional data */
2658 ERR("got odd number of bytes in UTF16 mode\n");
2662 if (count
!= 0 && num_read
== 0)
2664 fdinfo
->wxflag
|= WX_ATEOF
;
2665 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2667 else if (fdinfo
->wxflag
& WX_TEXT
)
2671 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2672 fdinfo
->wxflag
|= WX_READNL
;
2674 fdinfo
->wxflag
&= ~WX_READNL
;
2676 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2678 /* in text mode, a ctrl-z signals EOF */
2679 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2681 fdinfo
->wxflag
|= WX_ATEOF
;
2682 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2686 /* in text mode, strip \r if followed by \n */
2687 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2692 lookahead
[1] = '\n';
2693 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2695 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2697 bufstart
[j
++] = '\n';
2698 if(utf16
) bufstart
[j
++] = 0;
2702 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2704 bufstart
[j
++] = '\r';
2705 if(utf16
) bufstart
[j
++] = 0;
2708 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2710 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2712 bufstart
[j
++] = '\n';
2713 if (utf16
) bufstart
[j
++] = 0;
2717 fdinfo
->lookahead
[0] = lookahead
[0];
2718 fdinfo
->lookahead
[1] = lookahead
[1];
2722 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2727 bufstart
[j
++] = '\r';
2728 if(utf16
) bufstart
[j
++] = 0;
2731 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2732 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2734 bufstart
[j
++] = bufstart
[i
];
2735 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2743 if (GetLastError() == ERROR_BROKEN_PIPE
)
2745 TRACE(":end-of-pipe\n");
2746 fdinfo
->wxflag
|= WX_ATEOF
;
2751 TRACE(":failed-last error (%d)\n",GetLastError());
2757 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2761 /*********************************************************************
2764 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2767 num_read
= read_i(fd
, buf
, count
);
2771 /*********************************************************************
2772 * _setmode (MSVCRT.@)
2774 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2776 int ret
= get_ioinfo_nolock(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2777 if(ret
==MSVCRT__O_TEXT
&& (get_ioinfo_nolock(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2778 ret
= MSVCRT__O_WTEXT
;
2780 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2781 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2782 *MSVCRT__errno() = MSVCRT_EINVAL
;
2786 if(mode
== MSVCRT__O_BINARY
) {
2787 get_ioinfo_nolock(fd
)->wxflag
&= ~WX_TEXT
;
2788 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2792 get_ioinfo_nolock(fd
)->wxflag
|= WX_TEXT
;
2793 if(mode
== MSVCRT__O_TEXT
)
2794 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2795 else if(mode
== MSVCRT__O_U8TEXT
)
2796 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2798 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2803 /*********************************************************************
2804 * _stat64 (MSVCRT.@)
2806 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2809 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2810 unsigned short mode
= ALL_S_IREAD
;
2813 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2815 plen
= strlen(path
);
2816 while (plen
&& path
[plen
-1]==' ')
2819 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2820 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2822 *MSVCRT__errno() = MSVCRT_ENOENT
;
2826 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2828 TRACE("failed (%d)\n",GetLastError());
2829 *MSVCRT__errno() = MSVCRT_ENOENT
;
2833 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2835 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2836 Bon 011120: This FIXME seems incorrect
2837 Also a letter as first char isn't enough to be classified
2840 if (isalpha(*path
)&& (*(path
+1)==':'))
2841 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2843 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2845 /* Dir, or regular file? */
2846 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2847 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2850 mode
|= MSVCRT__S_IFREG
;
2852 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2854 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2855 (tolower(path
[plen
-3]) << 16);
2856 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2857 mode
|= ALL_S_IEXEC
;
2861 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2862 mode
|= ALL_S_IWRITE
;
2864 buf
->st_mode
= mode
;
2866 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2867 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2869 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2870 buf
->st_mtime
= buf
->st_ctime
= dw
;
2871 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2872 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2873 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2877 /*********************************************************************
2878 * _stati64 (MSVCRT.@)
2880 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2883 struct MSVCRT__stat64 buf64
;
2885 ret
= MSVCRT_stat64(path
, &buf64
);
2887 msvcrt_stat64_to_stati64(&buf64
, buf
);
2891 /*********************************************************************
2894 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2897 struct MSVCRT__stat64 buf64
;
2899 ret
= MSVCRT_stat64( path
, &buf64
);
2901 msvcrt_stat64_to_stat(&buf64
, buf
);
2905 /*********************************************************************
2906 * _stat32 (MSVCR100.@)
2908 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2911 struct MSVCRT__stat64 buf64
;
2913 ret
= MSVCRT_stat64(path
, &buf64
);
2915 msvcrt_stat64_to_stat32(&buf64
, buf
);
2919 /*********************************************************************
2920 * _stat32i64 (MSVCR100.@)
2922 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2925 struct MSVCRT__stat64 buf64
;
2927 ret
= MSVCRT_stat64(path
, &buf64
);
2929 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2933 /*********************************************************************
2934 * _stat64i32 (MSVCR100.@)
2936 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2939 struct MSVCRT__stat64 buf64
;
2941 ret
= MSVCRT_stat64(path
, &buf64
);
2943 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2947 /*********************************************************************
2948 * _wstat64 (MSVCRT.@)
2950 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2953 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2954 unsigned short mode
= ALL_S_IREAD
;
2957 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2959 plen
= strlenW(path
);
2960 while (plen
&& path
[plen
-1]==' ')
2963 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2964 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2966 *MSVCRT__errno() = MSVCRT_ENOENT
;
2970 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2972 TRACE("failed (%d)\n",GetLastError());
2973 *MSVCRT__errno() = MSVCRT_ENOENT
;
2977 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2979 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2980 if (MSVCRT_iswalpha(*path
))
2981 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2983 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2985 /* Dir, or regular file? */
2986 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2987 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2990 mode
|= MSVCRT__S_IFREG
;
2992 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2994 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2995 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2996 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2997 mode
|= ALL_S_IEXEC
;
3001 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3002 mode
|= ALL_S_IWRITE
;
3004 buf
->st_mode
= mode
;
3006 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3007 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3009 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3010 buf
->st_mtime
= buf
->st_ctime
= dw
;
3011 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3012 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3013 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3017 /*********************************************************************
3018 * _wstati64 (MSVCRT.@)
3020 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3023 struct MSVCRT__stat64 buf64
;
3025 ret
= MSVCRT__wstat64(path
, &buf64
);
3027 msvcrt_stat64_to_stati64(&buf64
, buf
);
3031 /*********************************************************************
3034 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3037 struct MSVCRT__stat64 buf64
;
3039 ret
= MSVCRT__wstat64( path
, &buf64
);
3040 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3044 /*********************************************************************
3045 * _wstat32 (MSVCR100.@)
3047 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3050 struct MSVCRT__stat64 buf64
;
3052 ret
= MSVCRT__wstat64(path
, &buf64
);
3054 msvcrt_stat64_to_stat32(&buf64
, buf
);
3058 /*********************************************************************
3059 * _wstat32i64 (MSVCR100.@)
3061 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3064 struct MSVCRT__stat64 buf64
;
3066 ret
= MSVCRT__wstat64(path
, &buf64
);
3068 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3072 /*********************************************************************
3073 * _wstat64i32 (MSVCR100.@)
3075 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3078 struct MSVCRT__stat64 buf64
;
3080 ret
= MSVCRT__wstat64(path
, &buf64
);
3082 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3086 /*********************************************************************
3089 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3091 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3094 /*********************************************************************
3095 * _telli64 (MSVCRT.@)
3097 __int64 CDECL
_telli64(int fd
)
3099 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3102 /*********************************************************************
3103 * _tempnam (MSVCRT.@)
3105 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3107 char tmpbuf
[MAX_PATH
];
3108 const char *tmp_dir
= MSVCRT_getenv("TMP");
3110 if (tmp_dir
) dir
= tmp_dir
;
3112 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3113 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3115 TRACE("got name (%s)\n",tmpbuf
);
3116 DeleteFileA(tmpbuf
);
3117 return MSVCRT__strdup(tmpbuf
);
3119 TRACE("failed (%d)\n",GetLastError());
3123 /*********************************************************************
3124 * _wtempnam (MSVCRT.@)
3126 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3128 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3129 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3130 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3132 if (tmp_dir
) dir
= tmp_dir
;
3134 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3135 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3137 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3138 DeleteFileW(tmpbuf
);
3139 return MSVCRT__wcsdup(tmpbuf
);
3141 TRACE("failed (%d)\n",GetLastError());
3145 /*********************************************************************
3148 int CDECL
MSVCRT__umask(int umask
)
3150 int old_umask
= MSVCRT_umask
;
3151 TRACE("(%d)\n",umask
);
3152 MSVCRT_umask
= umask
;
3156 /*********************************************************************
3157 * _utime64 (MSVCRT.@)
3159 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3161 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3165 int retVal
= _futime64(fd
, t
);
3172 /*********************************************************************
3173 * _utime32 (MSVCRT.@)
3175 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3179 struct MSVCRT___utimbuf64 t64
;
3180 t64
.actime
= t
->actime
;
3181 t64
.modtime
= t
->modtime
;
3182 return _utime64( path
, &t64
);
3185 return _utime64( path
, NULL
);
3188 /*********************************************************************
3192 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3194 return _utime64( path
, t
);
3197 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3199 return _utime32( path
, t
);
3203 /*********************************************************************
3204 * _wutime64 (MSVCRT.@)
3206 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3208 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3212 int retVal
= _futime64(fd
, t
);
3219 /*********************************************************************
3220 * _wutime32 (MSVCRT.@)
3222 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3226 struct MSVCRT___utimbuf64 t64
;
3227 t64
.actime
= t
->actime
;
3228 t64
.modtime
= t
->modtime
;
3229 return _wutime64( path
, &t64
);
3232 return _wutime64( path
, NULL
);
3235 /*********************************************************************
3236 * _wutime (MSVCRT.@)
3239 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3241 return _wutime64( path
, t
);
3244 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3246 return _wutime32( path
, t
);
3250 /*********************************************************************
3253 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3256 ioinfo
*info
= get_ioinfo_nolock(fd
);
3257 HANDLE hand
= info
->handle
;
3259 /* Don't trace small writes, it gets *very* annoying */
3262 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3264 if (hand
== INVALID_HANDLE_VALUE
)
3266 *MSVCRT__errno() = MSVCRT_EBADF
;
3270 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3272 *MSVCRT__errno() = MSVCRT_EINVAL
;
3276 /* If appending, go to EOF */
3277 if (info
->wxflag
& WX_APPEND
)
3278 MSVCRT__lseek(fd
, 0, FILE_END
);
3280 if (!(info
->wxflag
& WX_TEXT
))
3282 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3283 && (num_written
== count
))
3285 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3286 hand
, GetLastError());
3287 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3291 unsigned int i
, j
, nr_lf
, size
;
3294 const char *s
= buf
, *buf_start
= buf
;
3296 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3298 /* find number of \n */
3299 for (nr_lf
=0, i
=0; i
<count
; i
++)
3305 if ((q
= p
= MSVCRT_malloc(size
)))
3307 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3316 FIXME("Malloc failed\n");
3328 else if (info
->exflag
& EF_UTF16
)
3330 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3331 if (s
[i
]=='\n' && s
[i
+1]==0)
3336 if ((q
= p
= MSVCRT_malloc(size
)))
3338 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3340 if (s
[i
]=='\n' && s
[i
+1]==0)
3351 FIXME("Malloc failed\n");
3367 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3368 if (s
[i
]=='\n' && s
[i
+1]==0)
3371 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3373 msvcrt_set_errno(GetLastError());
3378 size
= conv_len
+nr_lf
;
3379 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3381 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3383 if (s
[i
]=='\n' && s
[i
+1]==0)
3391 q
= p
+count
+nr_lf
*2;
3392 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3393 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3397 FIXME("Malloc failed\n");
3404 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3408 if (num_written
!= size
)
3410 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3411 fd
, hand
, GetLastError(), num_written
);
3412 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3413 return s
- buf_start
;
3421 /*********************************************************************
3424 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3428 MSVCRT__lock_file(file
);
3429 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3430 if (len
== sizeof(val
)) {
3431 MSVCRT__unlock_file(file
);
3435 file
->_flag
|= MSVCRT__IOERR
;
3436 MSVCRT__unlock_file(file
);
3440 /*********************************************************************
3443 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3447 MSVCRT__lock_file(file
);
3448 ret
= MSVCRT__fclose_nolock(file
);
3449 MSVCRT__unlock_file(file
);
3454 /*********************************************************************
3455 * _fclose_nolock (MSVCRT.@)
3457 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3462 MSVCRT_free(file
->_tmpfname
);
3463 file
->_tmpfname
= NULL
;
3464 /* flush stdio buffers */
3465 if(file
->_flag
& MSVCRT__IOWRT
)
3466 MSVCRT__fflush_nolock(file
);
3467 if(file
->_flag
& MSVCRT__IOMYBUF
)
3468 MSVCRT_free(file
->_base
);
3470 r
=MSVCRT__close(file
->_file
);
3473 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3476 /*********************************************************************
3479 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3481 return file
->_flag
& MSVCRT__IOEOF
;
3484 /*********************************************************************
3487 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3489 return file
->_flag
& MSVCRT__IOERR
;
3492 /*********************************************************************
3493 * _filbuf (MSVCRT.@)
3495 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3499 if(file
->_flag
& MSVCRT__IOSTRG
)
3502 /* Allocate buffer if needed */
3503 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3504 msvcrt_alloc_buffer(file
);
3506 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3507 if(file
->_flag
& MSVCRT__IORW
)
3508 file
->_flag
|= MSVCRT__IOREAD
;
3513 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3515 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3516 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3522 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3524 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3530 file
->_ptr
= file
->_base
+1;
3531 c
= *(unsigned char *)file
->_base
;
3536 /*********************************************************************
3539 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3543 MSVCRT__lock_file(file
);
3544 ret
= MSVCRT__fgetc_nolock(file
);
3545 MSVCRT__unlock_file(file
);
3550 /*********************************************************************
3551 * _fgetc_nolock (MSVCRT.@)
3553 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3560 i
= (unsigned char *)file
->_ptr
++;
3563 j
= MSVCRT__filbuf(file
);
3568 /*********************************************************************
3569 * _fgetchar (MSVCRT.@)
3571 int CDECL
MSVCRT__fgetchar(void)
3573 return MSVCRT_fgetc(MSVCRT_stdin
);
3576 /*********************************************************************
3579 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3581 int cc
= MSVCRT_EOF
;
3582 char * buf_start
= s
;
3584 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3585 file
,file
->_file
,s
,size
);
3587 MSVCRT__lock_file(file
);
3589 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3594 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3596 TRACE(":nothing read\n");
3597 MSVCRT__unlock_file(file
);
3600 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3603 TRACE(":got %s\n", debugstr_a(buf_start
));
3604 MSVCRT__unlock_file(file
);
3608 /*********************************************************************
3611 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3615 MSVCRT__lock_file(file
);
3616 ret
= MSVCRT__fgetwc_nolock(file
);
3617 MSVCRT__unlock_file(file
);
3622 /*********************************************************************
3623 * _fgetwc_nolock (MSVCRT.@)
3625 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3630 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3631 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3634 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3635 ch
= MSVCRT__fgetc_nolock(file
);
3636 if(ch
== MSVCRT_EOF
) {
3643 char mbs
[MSVCRT_MB_LEN_MAX
];
3646 ch
= MSVCRT__fgetc_nolock(file
);
3647 if(ch
!= MSVCRT_EOF
) {
3649 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3650 ch
= MSVCRT__fgetc_nolock(file
);
3651 if(ch
!= MSVCRT_EOF
) {
3660 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3667 /*********************************************************************
3670 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3677 MSVCRT__lock_file(file
);
3678 for (j
=0; j
<sizeof(int); j
++) {
3679 k
= MSVCRT_fgetc(file
);
3680 if (k
== MSVCRT_EOF
) {
3681 file
->_flag
|= MSVCRT__IOEOF
;
3682 MSVCRT__unlock_file(file
);
3688 MSVCRT__unlock_file(file
);
3692 /*********************************************************************
3695 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3697 return MSVCRT_fgetwc(file
);
3700 /*********************************************************************
3701 * _fgetwchar (MSVCRT.@)
3703 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3705 return MSVCRT_fgetwc(MSVCRT_stdin
);
3708 /*********************************************************************
3709 * getwchar (MSVCRT.@)
3711 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3713 return MSVCRT__fgetwchar();
3716 /*********************************************************************
3719 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3721 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3722 MSVCRT_wchar_t
* buf_start
= s
;
3724 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3725 file
,file
->_file
,s
,size
);
3727 MSVCRT__lock_file(file
);
3729 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3734 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3736 TRACE(":nothing read\n");
3737 MSVCRT__unlock_file(file
);
3740 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3743 TRACE(":got %s\n", debugstr_w(buf_start
));
3744 MSVCRT__unlock_file(file
);
3748 /*********************************************************************
3749 * _flsbuf (MSVCRT.@)
3751 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3753 /* Flush output buffer */
3754 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3755 msvcrt_alloc_buffer(file
);
3757 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3758 if(file
->_flag
& MSVCRT__IORW
)
3759 file
->_flag
|= MSVCRT__IOWRT
;
3763 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3766 if(file
->_cnt
<= 0) {
3767 res
= msvcrt_flush_buffer(file
);
3770 file
->_cnt
=file
->_bufsiz
;
3778 /* set _cnt to 0 for unbuffered FILEs */
3780 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3783 file
->_flag
|= MSVCRT__IOERR
;
3788 /*********************************************************************
3791 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3795 MSVCRT__lock_file(file
);
3796 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3797 MSVCRT__unlock_file(file
);
3802 /*********************************************************************
3803 * _fwrite_nolock (MSVCRT.@)
3805 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3807 MSVCRT_size_t wrcnt
=size
* nmemb
;
3813 if(file
->_cnt
< 0) {
3814 WARN("negative file->_cnt value in %p\n", file
);
3815 file
->_flag
|= MSVCRT__IOERR
;
3817 } else if(file
->_cnt
) {
3818 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3819 memcpy(file
->_ptr
, ptr
, pcnt
);
3824 ptr
= (const char*)ptr
+ pcnt
;
3825 } else if((file
->_flag
& MSVCRT__IONBF
)
3826 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3827 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3831 if(file
->_flag
& MSVCRT__IONBF
)
3833 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3834 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3836 bufsiz
= file
->_bufsiz
;
3838 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3840 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3843 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3844 file
->_flag
|= MSVCRT__IOERR
;
3849 ptr
= (const char*)ptr
+ pcnt
;
3851 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3855 ptr
= (const char*)ptr
+ 1;
3859 return written
/ size
;
3862 /*********************************************************************
3865 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3869 MSVCRT__lock_file(file
);
3870 ret
= MSVCRT__fputwc_nolock(wc
, file
);
3871 MSVCRT__unlock_file(file
);
3876 /*********************************************************************
3877 * _fputwc_nolock (MSVCRT.@)
3879 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3881 MSVCRT_wchar_t mwc
=wc
;
3885 fdinfo
= get_ioinfo_nolock(file
->_file
);
3887 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3888 char buf
[MSVCRT_MB_LEN_MAX
];
3891 char_len
= MSVCRT_wctomb(buf
, mwc
);
3892 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
3896 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3905 /*********************************************************************
3906 * _fputwchar (MSVCRT.@)
3908 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3910 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3913 /*********************************************************************
3914 * _wfsopen (MSVCRT.@)
3916 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3919 int open_flags
, stream_flags
, fd
;
3921 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3923 /* map mode string to open() flags. "man fopen" for possibilities. */
3924 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3928 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3931 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3933 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3940 TRACE(":got (%p)\n",file
);
3941 if (fd
>= 0 && !file
)
3947 /*********************************************************************
3948 * _fsopen (MSVCRT.@)
3950 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3953 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3955 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3956 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3957 *MSVCRT__errno() = MSVCRT_EINVAL
;
3960 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3963 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3964 *MSVCRT__errno() = MSVCRT_EINVAL
;
3968 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
3975 /*********************************************************************
3978 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
3980 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3983 /*********************************************************************
3984 * fopen_s (MSVCRT.@)
3986 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
3987 const char *filename
, const char *mode
)
3989 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3990 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3991 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3993 *pFile
= MSVCRT_fopen(filename
, mode
);
3996 return *MSVCRT__errno();
4000 /*********************************************************************
4001 * _wfopen (MSVCRT.@)
4003 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4005 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4008 /*********************************************************************
4009 * _wfopen_s (MSVCRT.@)
4011 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4012 const MSVCRT_wchar_t
*mode
)
4014 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4015 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4016 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4018 *pFile
= MSVCRT__wfopen(filename
, mode
);
4021 return *MSVCRT__errno();
4025 /*********************************************************************
4028 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4032 MSVCRT__lock_file(file
);
4033 ret
= MSVCRT__fputc_nolock(c
, file
);
4034 MSVCRT__unlock_file(file
);
4039 /*********************************************************************
4040 * _fputc_nolock (MSVCRT.@)
4042 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4051 res
= msvcrt_flush_buffer(file
);
4052 return res
? res
: c
;
4058 res
= MSVCRT__flsbuf(c
, file
);
4063 /*********************************************************************
4064 * _fputchar (MSVCRT.@)
4066 int CDECL
MSVCRT__fputchar(int c
)
4068 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4071 /*********************************************************************
4074 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4078 MSVCRT__lock_file(file
);
4079 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4080 MSVCRT__unlock_file(file
);
4085 /*********************************************************************
4086 * _fread_nolock (MSVCRT.@)
4088 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4090 MSVCRT_size_t rcnt
=size
* nmemb
;
4091 MSVCRT_size_t read
=0;
4092 MSVCRT_size_t pread
=0;
4097 /* first buffered data */
4099 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4100 memcpy(ptr
, file
->_ptr
, pcnt
);
4105 ptr
= (char*)ptr
+ pcnt
;
4106 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4107 if(file
->_flag
& MSVCRT__IORW
) {
4108 file
->_flag
|= MSVCRT__IOREAD
;
4114 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4115 msvcrt_alloc_buffer(file
);
4120 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4121 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4122 file
->_ptr
= file
->_base
;
4123 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4124 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4125 if (i
> 0 && i
< file
->_cnt
) {
4126 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4127 file
->_flag
&= ~MSVCRT__IOEOF
;
4130 memcpy(ptr
, file
->_ptr
, i
);
4134 } else if (rcnt
> INT_MAX
) {
4135 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4136 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4137 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4139 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4143 ptr
= (char *)ptr
+i
;
4144 /* expose feof condition in the flags
4145 * MFC tests file->_flag for feof, and doesn't call feof())
4147 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4148 file
->_flag
|= MSVCRT__IOEOF
;
4151 file
->_flag
|= MSVCRT__IOERR
;
4162 /*********************************************************************
4163 * fread_s (MSVCR80.@)
4165 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4166 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4170 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4172 memset(buf
, 0, buf_size
);
4175 if(!elem_size
|| !count
) return 0;
4177 MSVCRT__lock_file(stream
);
4178 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4179 MSVCRT__unlock_file(stream
);
4184 /*********************************************************************
4185 * _fread_nolock_s (MSVCR80.@)
4187 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4188 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4190 size_t bytes_left
, buf_pos
;
4192 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4194 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4196 memset(buf
, 0, buf_size
);
4199 if(!elem_size
|| !count
) return 0;
4200 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4201 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4203 bytes_left
= elem_size
*count
;
4206 if(stream
->_cnt
> 0) {
4207 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4209 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4210 memset(buf
, 0, buf_size
);
4214 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4218 int c
= MSVCRT__filbuf(stream
);
4223 if(!MSVCRT_CHECK_PMT_ERR(buf_size
-buf_pos
> 0, MSVCRT_ERANGE
)) {
4224 memset(buf
, 0, buf_size
);
4228 ((char*)buf
)[buf_pos
++] = c
;
4233 return buf_pos
/elem_size
;
4236 /*********************************************************************
4237 * _wfreopen (MSVCRT.@)
4240 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4242 int open_flags
, stream_flags
, fd
;
4244 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4247 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4251 MSVCRT_fclose(file
);
4252 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4254 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4256 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4266 /*********************************************************************
4267 * _wfreopen_s (MSVCRT.@)
4269 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4270 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4272 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4273 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4274 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4275 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4277 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4280 return *MSVCRT__errno();
4284 /*********************************************************************
4285 * freopen (MSVCRT.@)
4288 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4291 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4293 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4294 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4300 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4307 /*********************************************************************
4308 * freopen_s (MSVCRT.@)
4310 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4311 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4313 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4314 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4315 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4316 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4318 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4321 return *MSVCRT__errno();
4325 /*********************************************************************
4326 * fsetpos (MSVCRT.@)
4328 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4332 MSVCRT__lock_file(file
);
4333 /* Note that all this has been lifted 'as is' from fseek */
4334 if(file
->_flag
& MSVCRT__IOWRT
)
4335 msvcrt_flush_buffer(file
);
4337 /* Discard buffered input */
4339 file
->_ptr
= file
->_base
;
4341 /* Reset direction of i/o */
4342 if(file
->_flag
& MSVCRT__IORW
) {
4343 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4346 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4347 MSVCRT__unlock_file(file
);
4351 /*********************************************************************
4352 * _ftelli64 (MSVCRT.@)
4354 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4358 MSVCRT__lock_file(file
);
4359 ret
= MSVCRT__ftelli64_nolock(file
);
4360 MSVCRT__unlock_file(file
);
4365 /*********************************************************************
4366 * _ftelli64_nolock (MSVCRT.@)
4368 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4372 pos
= _telli64(file
->_file
);
4375 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4376 if(file
->_flag
& MSVCRT__IOWRT
) {
4377 pos
+= file
->_ptr
- file
->_base
;
4379 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4382 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4386 } else if(!file
->_cnt
) { /* nothing to do */
4387 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4391 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4392 for(i
=0; i
<file
->_cnt
; i
++)
4393 if(file
->_ptr
[i
] == '\n')
4399 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4402 pos
-= file
->_bufsiz
;
4403 pos
+= file
->_ptr
- file
->_base
;
4405 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4406 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4409 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4419 /*********************************************************************
4422 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4424 return MSVCRT__ftelli64(file
);
4427 /*********************************************************************
4428 * _ftell_nolock (MSVCRT.@)
4430 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4432 return MSVCRT__ftelli64_nolock(file
);
4435 /*********************************************************************
4436 * fgetpos (MSVCRT.@)
4438 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4440 *pos
= MSVCRT__ftelli64(file
);
4446 /*********************************************************************
4449 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4451 MSVCRT_size_t len
= strlen(s
);
4454 MSVCRT__lock_file(file
);
4455 ret
= MSVCRT_fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4456 MSVCRT__unlock_file(file
);
4460 /*********************************************************************
4463 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4465 MSVCRT_size_t i
, len
= strlenW(s
);
4469 MSVCRT__lock_file(file
);
4470 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4471 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4472 MSVCRT__unlock_file(file
);
4476 tmp_buf
= add_std_buffer(file
);
4477 for (i
=0; i
<len
; i
++) {
4478 if(MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
) {
4479 if(tmp_buf
) remove_std_buffer(file
);
4480 MSVCRT__unlock_file(file
);
4485 if(tmp_buf
) remove_std_buffer(file
);
4486 MSVCRT__unlock_file(file
);
4490 /*********************************************************************
4491 * getchar (MSVCRT.@)
4493 int CDECL
MSVCRT_getchar(void)
4495 return MSVCRT_fgetc(MSVCRT_stdin
);
4498 /*********************************************************************
4501 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4503 return MSVCRT_fgetc(file
);
4506 /*********************************************************************
4509 char * CDECL
MSVCRT_gets(char *buf
)
4512 char * buf_start
= buf
;
4514 MSVCRT__lock_file(MSVCRT_stdin
);
4515 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4516 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
4517 if(cc
!= '\r') *buf
++ = (char)cc
;
4521 TRACE("got '%s'\n", buf_start
);
4522 MSVCRT__unlock_file(MSVCRT_stdin
);
4526 /*********************************************************************
4529 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4532 MSVCRT_wchar_t
* ws
= buf
;
4534 MSVCRT__lock_file(MSVCRT_stdin
);
4535 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4536 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
4539 *buf
++ = (MSVCRT_wchar_t
)cc
;
4543 TRACE("got %s\n", debugstr_w(ws
));
4544 MSVCRT__unlock_file(MSVCRT_stdin
);
4548 /*********************************************************************
4551 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4553 return MSVCRT_fputc(c
, file
);
4556 /*********************************************************************
4557 * putchar (MSVCRT.@)
4559 int CDECL
MSVCRT_putchar(int c
)
4561 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4564 /*********************************************************************
4565 * _putwch (MSVCRT.@)
4567 int CDECL
MSVCRT__putwch(int c
)
4569 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4572 /*********************************************************************
4575 int CDECL
MSVCRT_puts(const char *s
)
4577 MSVCRT_size_t len
= strlen(s
);
4580 MSVCRT__lock_file(MSVCRT_stdout
);
4581 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4582 MSVCRT__unlock_file(MSVCRT_stdout
);
4586 ret
= MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4587 MSVCRT__unlock_file(MSVCRT_stdout
);
4591 /*********************************************************************
4594 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4596 static const MSVCRT_wchar_t nl
= '\n';
4597 MSVCRT_size_t len
= strlenW(s
);
4600 MSVCRT__lock_file(MSVCRT_stdout
);
4601 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4602 MSVCRT__unlock_file(MSVCRT_stdout
);
4606 ret
= MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4607 MSVCRT__unlock_file(MSVCRT_stdout
);
4611 /*********************************************************************
4614 int CDECL
MSVCRT_remove(const char *path
)
4616 TRACE("(%s)\n",path
);
4617 if (DeleteFileA(path
))
4619 TRACE(":failed (%d)\n",GetLastError());
4620 msvcrt_set_errno(GetLastError());
4624 /*********************************************************************
4625 * _wremove (MSVCRT.@)
4627 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4629 TRACE("(%s)\n",debugstr_w(path
));
4630 if (DeleteFileW(path
))
4632 TRACE(":failed (%d)\n",GetLastError());
4633 msvcrt_set_errno(GetLastError());
4637 /*********************************************************************
4640 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4642 TRACE(":from %s to %s\n",oldpath
,newpath
);
4643 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4645 TRACE(":failed (%d)\n",GetLastError());
4646 msvcrt_set_errno(GetLastError());
4650 /*********************************************************************
4651 * _wrename (MSVCRT.@)
4653 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4655 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4656 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4658 TRACE(":failed (%d)\n",GetLastError());
4659 msvcrt_set_errno(GetLastError());
4663 /*********************************************************************
4664 * setvbuf (MSVCRT.@)
4666 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4668 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4669 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4670 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4672 MSVCRT__lock_file(file
);
4674 MSVCRT_fflush(file
);
4675 if(file
->_flag
& MSVCRT__IOMYBUF
)
4676 MSVCRT_free(file
->_base
);
4677 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4680 if(mode
== MSVCRT__IONBF
) {
4681 file
->_flag
|= MSVCRT__IONBF
;
4682 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4685 file
->_base
= file
->_ptr
= buf
;
4686 file
->_flag
|= MSVCRT__USERBUF
;
4687 file
->_bufsiz
= size
;
4689 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4692 MSVCRT__unlock_file(file
);
4696 file
->_flag
|= MSVCRT__IOMYBUF
;
4697 file
->_bufsiz
= size
;
4699 MSVCRT__unlock_file(file
);
4703 /*********************************************************************
4706 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4708 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4711 /*********************************************************************
4714 char * CDECL
MSVCRT_tmpnam(char *s
)
4721 thread_data_t
*data
= msvcrt_get_thread_data();
4723 if(!data
->tmpnam_buffer
)
4724 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4726 s
= data
->tmpnam_buffer
;
4729 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4730 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4731 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4733 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4734 memcpy(p
, tmpstr
, size
);
4736 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4737 GetLastError() == ERROR_FILE_NOT_FOUND
)
4743 /*********************************************************************
4744 * _wtmpnam (MSVCRT.@)
4746 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4748 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4749 MSVCRT_wchar_t tmpstr
[16];
4753 thread_data_t
*data
= msvcrt_get_thread_data();
4755 if(!data
->wtmpnam_buffer
)
4756 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4758 s
= data
->wtmpnam_buffer
;
4761 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4762 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4763 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4765 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4766 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4768 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4769 GetLastError() == ERROR_FILE_NOT_FOUND
)
4775 /*********************************************************************
4776 * tmpfile (MSVCRT.@)
4778 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4780 char *filename
= MSVCRT_tmpnam(NULL
);
4782 MSVCRT_FILE
* file
= NULL
;
4785 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4786 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4787 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4789 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4794 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4797 if(fd
!= -1 && !file
)
4803 /*********************************************************************
4804 * tmpfile_s (MSVCRT.@)
4806 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4808 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4810 *file
= MSVCRT_tmpfile();
4814 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4816 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4819 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4823 MSVCRT__lock_file(file
);
4825 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4826 ret
= MSVCRT_fwrite(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4827 MSVCRT__unlock_file(file
);
4831 for(i
=0; i
<len
; i
++) {
4832 if(MSVCRT_fputwc(str
[i
], file
) == MSVCRT_WEOF
) {
4833 MSVCRT__unlock_file(file
);
4838 MSVCRT__unlock_file(file
);
4842 /*********************************************************************
4843 * vfprintf (MSVCRT.@)
4845 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4850 MSVCRT__lock_file(file
);
4851 tmp_buf
= add_std_buffer(file
);
4852 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4853 if(tmp_buf
) remove_std_buffer(file
);
4854 MSVCRT__unlock_file(file
);
4859 /*********************************************************************
4860 * vfprintf_s (MSVCRT.@)
4862 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4867 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4869 MSVCRT__lock_file(file
);
4870 tmp_buf
= add_std_buffer(file
);
4871 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4872 if(tmp_buf
) remove_std_buffer(file
);
4873 MSVCRT__unlock_file(file
);
4878 /*********************************************************************
4879 * vfwprintf (MSVCRT.@)
4881 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4886 MSVCRT__lock_file(file
);
4887 tmp_buf
= add_std_buffer(file
);
4888 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4889 if(tmp_buf
) remove_std_buffer(file
);
4890 MSVCRT__unlock_file(file
);
4895 /*********************************************************************
4896 * vfwprintf_s (MSVCRT.@)
4898 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4903 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4905 MSVCRT__lock_file(file
);
4906 tmp_buf
= add_std_buffer(file
);
4907 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4908 if(tmp_buf
) remove_std_buffer(file
);
4909 MSVCRT__unlock_file(file
);
4914 /*********************************************************************
4915 * _vfwprintf_l (MSVCRT.@)
4917 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4918 MSVCRT__locale_t locale
, __ms_va_list valist
)
4923 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4925 MSVCRT__lock_file(file
);
4926 tmp_buf
= add_std_buffer(file
);
4927 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4928 if(tmp_buf
) remove_std_buffer(file
);
4929 MSVCRT__unlock_file(file
);
4934 /*********************************************************************
4935 * vprintf (MSVCRT.@)
4937 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4939 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4942 /*********************************************************************
4943 * vprintf_s (MSVCRT.@)
4945 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4947 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4950 /*********************************************************************
4951 * vwprintf (MSVCRT.@)
4953 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4955 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4958 /*********************************************************************
4959 * vwprintf_s (MSVCRT.@)
4961 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4963 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4966 /*********************************************************************
4967 * fprintf (MSVCRT.@)
4969 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4971 __ms_va_list valist
;
4973 __ms_va_start(valist
, format
);
4974 res
= MSVCRT_vfprintf(file
, format
, valist
);
4975 __ms_va_end(valist
);
4979 /*********************************************************************
4980 * fprintf_s (MSVCRT.@)
4982 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
4984 __ms_va_list valist
;
4986 __ms_va_start(valist
, format
);
4987 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
4988 __ms_va_end(valist
);
4992 /*********************************************************************
4993 * fwprintf (MSVCRT.@)
4995 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4997 __ms_va_list valist
;
4999 __ms_va_start(valist
, format
);
5000 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5001 __ms_va_end(valist
);
5005 /*********************************************************************
5006 * fwprintf_s (MSVCRT.@)
5008 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5010 __ms_va_list valist
;
5012 __ms_va_start(valist
, format
);
5013 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5014 __ms_va_end(valist
);
5018 /*********************************************************************
5019 * _fwprintf_l (MSVCRT.@)
5021 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5023 __ms_va_list valist
;
5025 __ms_va_start(valist
, locale
);
5026 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5027 __ms_va_end(valist
);
5031 /*********************************************************************
5034 int CDECL
MSVCRT_printf(const char *format
, ...)
5036 __ms_va_list valist
;
5038 __ms_va_start(valist
, format
);
5039 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5040 __ms_va_end(valist
);
5044 /*********************************************************************
5045 * printf_s (MSVCRT.@)
5047 int CDECL
MSVCRT_printf_s(const char *format
, ...)
5049 __ms_va_list valist
;
5051 __ms_va_start(valist
, format
);
5052 res
= MSVCRT_vprintf_s(format
, valist
);
5053 __ms_va_end(valist
);
5057 /*********************************************************************
5060 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5064 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5066 MSVCRT__lock_file(file
);
5067 ret
= MSVCRT__ungetc_nolock(c
, file
);
5068 MSVCRT__unlock_file(file
);
5073 /*********************************************************************
5074 * _ungetc_nolock (MSVCRT.@)
5076 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5078 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5080 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5081 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5084 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5085 && msvcrt_alloc_buffer(file
))
5086 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5089 if(file
->_ptr
>file
->_base
) {
5091 if(file
->_flag
& MSVCRT__IOSTRG
) {
5092 if(*file
->_ptr
!= c
) {
5100 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5101 file
->_flag
|= MSVCRT__IOREAD
;
5108 /*********************************************************************
5109 * ungetwc (MSVCRT.@)
5111 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5115 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5117 MSVCRT__lock_file(file
);
5118 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5119 MSVCRT__unlock_file(file
);
5124 /*********************************************************************
5125 * _ungetwc_nolock (MSVCRT.@)
5127 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5129 MSVCRT_wchar_t mwc
= wc
;
5131 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5132 if (wc
== MSVCRT_WEOF
)
5135 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5136 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5137 unsigned char * pp
= (unsigned char *)&mwc
;
5140 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5141 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5145 char mbs
[MSVCRT_MB_LEN_MAX
];
5148 len
= MSVCRT_wctomb(mbs
, mwc
);
5152 for(len
--; len
>=0; len
--) {
5153 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5161 /*********************************************************************
5162 * wprintf (MSVCRT.@)
5164 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5166 __ms_va_list valist
;
5168 __ms_va_start(valist
, format
);
5169 res
= MSVCRT_vwprintf(format
, valist
);
5170 __ms_va_end(valist
);
5174 /*********************************************************************
5175 * wprintf_s (MSVCRT.@)
5177 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5179 __ms_va_list valist
;
5181 __ms_va_start(valist
, format
);
5182 res
= MSVCRT_vwprintf_s(format
, valist
);
5183 __ms_va_end(valist
);
5187 /*********************************************************************
5188 * _getmaxstdio (MSVCRT.@)
5190 int CDECL
MSVCRT__getmaxstdio(void)
5192 return MSVCRT_max_streams
;
5195 /*********************************************************************
5196 * _setmaxstdio (MSVCRT.@)
5198 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5200 TRACE("%d\n", newmax
);
5202 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5205 MSVCRT_max_streams
= newmax
;
5206 return MSVCRT_max_streams
;