2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READNL 0x04 /* read started with \n */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
71 /* values for exflag - it's used differently in msvcr90.dll*/
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom
[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
92 CRITICAL_SECTION crit
;
98 BOOL utf8translations
;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
117 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
118 static int MSVCRT_fdend
= 3; /* highest allocated fd */
122 CRITICAL_SECTION crit
;
125 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
126 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
127 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
129 /* INTERNAL: process umask */
130 static int MSVCRT_umask
= 0;
132 /* INTERNAL: static data for tmpnam and _wtmpname functions */
133 static int tmpnam_unique
;
135 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
136 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
137 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
138 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
140 #define TOUL(x) (ULONGLONG)(x)
141 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
142 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
143 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
144 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
146 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
147 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
148 * and MSVCRT_stream_idx, from race conditions.
149 * It doesn't protect against race conditions manipulating the underlying files
150 * or flags; doing so would probably be better accomplished with per-file
151 * protection, rather than locking the whole table for every change.
153 static CRITICAL_SECTION MSVCRT_file_cs
;
154 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
156 0, 0, &MSVCRT_file_cs
,
157 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
158 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
160 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
161 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
162 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
164 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
166 buf
->st_dev
= buf64
->st_dev
;
167 buf
->st_ino
= buf64
->st_ino
;
168 buf
->st_mode
= buf64
->st_mode
;
169 buf
->st_nlink
= buf64
->st_nlink
;
170 buf
->st_uid
= buf64
->st_uid
;
171 buf
->st_gid
= buf64
->st_gid
;
172 buf
->st_rdev
= buf64
->st_rdev
;
173 buf
->st_size
= buf64
->st_size
;
174 buf
->st_atime
= buf64
->st_atime
;
175 buf
->st_mtime
= buf64
->st_mtime
;
176 buf
->st_ctime
= buf64
->st_ctime
;
179 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
181 buf
->st_dev
= buf64
->st_dev
;
182 buf
->st_ino
= buf64
->st_ino
;
183 buf
->st_mode
= buf64
->st_mode
;
184 buf
->st_nlink
= buf64
->st_nlink
;
185 buf
->st_uid
= buf64
->st_uid
;
186 buf
->st_gid
= buf64
->st_gid
;
187 buf
->st_rdev
= buf64
->st_rdev
;
188 buf
->st_size
= buf64
->st_size
;
189 buf
->st_atime
= buf64
->st_atime
;
190 buf
->st_mtime
= buf64
->st_mtime
;
191 buf
->st_ctime
= buf64
->st_ctime
;
194 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
196 buf
->st_dev
= buf64
->st_dev
;
197 buf
->st_ino
= buf64
->st_ino
;
198 buf
->st_mode
= buf64
->st_mode
;
199 buf
->st_nlink
= buf64
->st_nlink
;
200 buf
->st_uid
= buf64
->st_uid
;
201 buf
->st_gid
= buf64
->st_gid
;
202 buf
->st_rdev
= buf64
->st_rdev
;
203 buf
->st_size
= buf64
->st_size
;
204 buf
->st_atime
= buf64
->st_atime
;
205 buf
->st_mtime
= buf64
->st_mtime
;
206 buf
->st_ctime
= buf64
->st_ctime
;
209 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
211 buf
->st_dev
= buf64
->st_dev
;
212 buf
->st_ino
= buf64
->st_ino
;
213 buf
->st_mode
= buf64
->st_mode
;
214 buf
->st_nlink
= buf64
->st_nlink
;
215 buf
->st_uid
= buf64
->st_uid
;
216 buf
->st_gid
= buf64
->st_gid
;
217 buf
->st_rdev
= buf64
->st_rdev
;
218 buf
->st_size
= buf64
->st_size
;
219 buf
->st_atime
= buf64
->st_atime
;
220 buf
->st_mtime
= buf64
->st_mtime
;
221 buf
->st_ctime
= buf64
->st_ctime
;
224 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
226 buf
->st_dev
= buf64
->st_dev
;
227 buf
->st_ino
= buf64
->st_ino
;
228 buf
->st_mode
= buf64
->st_mode
;
229 buf
->st_nlink
= buf64
->st_nlink
;
230 buf
->st_uid
= buf64
->st_uid
;
231 buf
->st_gid
= buf64
->st_gid
;
232 buf
->st_rdev
= buf64
->st_rdev
;
233 buf
->st_size
= buf64
->st_size
;
234 buf
->st_atime
= buf64
->st_atime
;
235 buf
->st_mtime
= buf64
->st_mtime
;
236 buf
->st_ctime
= buf64
->st_ctime
;
239 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
241 /* 1601 to 1970 is 369 years plus 89 leap days */
242 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
244 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
245 ft
->dwHighDateTime
= ticks
>> 32;
246 ft
->dwLowDateTime
= ticks
;
249 static inline ioinfo
* get_ioinfo_nolock(int fd
)
252 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
253 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
255 return &MSVCRT___badioinfo
;
257 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
260 static inline ioinfo
* get_ioinfo(int fd
)
262 ioinfo
*ret
= get_ioinfo_nolock(fd
);
263 if(ret
->exflag
& EF_CRIT_INIT
)
264 EnterCriticalSection(&ret
->crit
);
268 static inline void release_ioinfo(ioinfo
*info
)
270 if(info
->exflag
& EF_CRIT_INIT
)
271 LeaveCriticalSection(&info
->crit
);
274 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
278 if(i
>= MSVCRT_max_streams
)
282 return &MSVCRT__iob
[i
];
284 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
286 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
287 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
288 ERR("out of memory\n");
289 *MSVCRT__errno() = MSVCRT_ENOMEM
;
293 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
295 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
300 static inline BOOL
msvcrt_is_valid_fd(int fd
)
302 return fd
>= 0 && fd
< MSVCRT_fdend
&& (get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
);
305 /* INTERNAL: Get the HANDLE for a fd
306 * This doesn't lock the table, because a failure will result in
307 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
308 * it returns a valid handle which is about to be closed, a subsequent call
309 * will fail, most likely in a sane way.
311 static HANDLE
msvcrt_fdtoh(int fd
)
313 if (!msvcrt_is_valid_fd(fd
))
315 WARN(":fd (%d) - no handle!\n",fd
);
316 *MSVCRT___doserrno() = 0;
317 *MSVCRT__errno() = MSVCRT_EBADF
;
318 return INVALID_HANDLE_VALUE
;
320 if (get_ioinfo_nolock(fd
)->handle
== INVALID_HANDLE_VALUE
)
321 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd
);
322 return get_ioinfo_nolock(fd
)->handle
;
325 /* INTERNAL: free a file entry fd */
326 static void msvcrt_free_fd(int fd
)
331 fdinfo
= get_ioinfo_nolock(fd
);
332 if(fdinfo
!= &MSVCRT___badioinfo
)
334 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
337 TRACE(":fd (%d) freed\n",fd
);
344 SetStdHandle(STD_INPUT_HANDLE
, 0);
347 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
350 SetStdHandle(STD_ERROR_HANDLE
, 0);
355 if (fd
== MSVCRT_fdend
- 1)
357 if (fd
< MSVCRT_fdstart
)
362 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
363 /* caller must hold the files lock */
364 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
368 if (fd
>= MSVCRT_MAX_FILES
)
370 WARN(":files exhausted!\n");
371 *MSVCRT__errno() = MSVCRT_ENFILE
;
375 fdinfo
= get_ioinfo_nolock(fd
);
376 if(fdinfo
== &MSVCRT___badioinfo
) {
379 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
380 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
381 WARN(":out of memory!\n");
382 *MSVCRT__errno() = MSVCRT_ENOMEM
;
386 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
387 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
389 fdinfo
= get_ioinfo_nolock(fd
);
392 fdinfo
->handle
= hand
;
393 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
394 fdinfo
->lookahead
[0] = '\n';
395 fdinfo
->lookahead
[1] = '\n';
396 fdinfo
->lookahead
[2] = '\n';
397 if(!(fdinfo
->exflag
& EF_CRIT_INIT
))
398 InitializeCriticalSection(&fdinfo
->crit
);
399 fdinfo
->exflag
= EF_CRIT_INIT
;
401 /* locate next free slot */
402 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
403 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
405 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
406 get_ioinfo_nolock(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
408 /* update last fd in use */
409 if (fd
>= MSVCRT_fdend
)
410 MSVCRT_fdend
= fd
+ 1;
411 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
415 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
416 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
417 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
423 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
424 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
429 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
430 ret
= msvcrt_set_fd(hand
, flag
, MSVCRT_fdstart
);
435 /* INTERNAL: Allocate a FILE* for an fd slot */
436 /* caller must hold the files lock */
437 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
442 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
444 file
= msvcrt_get_file(i
);
448 if (file
->_flag
== 0)
450 if (i
== MSVCRT_stream_idx
)
452 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
454 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
455 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
466 /* INTERNAL: initialize a FILE* from an open fd */
467 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
469 TRACE(":fd (%d) allocating FILE*\n",fd
);
470 if (!msvcrt_is_valid_fd(fd
))
472 WARN(":invalid fd %d\n",fd
);
473 *MSVCRT___doserrno() = 0;
474 *MSVCRT__errno() = MSVCRT_EBADF
;
477 file
->_ptr
= file
->_base
= NULL
;
480 file
->_flag
= stream_flags
;
481 file
->_tmpfname
= NULL
;
483 TRACE(":got FILE* (%p)\n",file
);
487 /* INTERNAL: Create an inheritance data block (for spawned process)
488 * The inheritance block is made of:
489 * 00 int nb of file descriptor (NBFD)
490 * 04 char file flags (wxflag): repeated for each fd
491 * 4+NBFD HANDLE file handle: repeated for each fd
493 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
500 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
501 *block
= MSVCRT_calloc(*size
, 1);
507 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
508 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
510 *(unsigned*)*block
= MSVCRT_fdend
;
511 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
513 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
514 fdinfo
= get_ioinfo_nolock(fd
);
515 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
517 *wxflag_ptr
= fdinfo
->wxflag
;
518 *handle_ptr
= fdinfo
->handle
;
523 *handle_ptr
= INVALID_HANDLE_VALUE
;
525 wxflag_ptr
++; handle_ptr
++;
530 /* INTERNAL: Set up all file descriptors,
531 * as well as default streams (stdin, stderr and stdout)
533 void msvcrt_init_io(void)
539 GetStartupInfoA(&si
);
540 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
546 count
= *(unsigned*)si
.lpReserved2
;
547 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
548 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
550 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
551 count
= min(count
, MSVCRT_MAX_FILES
);
552 for (i
= 0; i
< count
; i
++)
554 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
555 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
557 wxflag_ptr
++; handle_ptr
++;
559 MSVCRT_fdend
= max( 3, count
);
560 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
561 if (get_ioinfo_nolock(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
564 fdinfo
= get_ioinfo_nolock(MSVCRT_STDIN_FILENO
);
565 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
566 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
567 DWORD type
= GetFileType(h
);
569 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
570 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDIN_FILENO
);
573 fdinfo
= get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
);
574 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
575 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
576 DWORD type
= GetFileType(h
);
578 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
579 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDOUT_FILENO
);
582 fdinfo
= get_ioinfo_nolock(MSVCRT_STDERR_FILENO
);
583 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
584 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
585 DWORD type
= GetFileType(h
);
587 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
588 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDERR_FILENO
);
591 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
592 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
593 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
595 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
596 for (i
= 0; i
< 3; i
++)
598 /* FILE structs for stdin/out/err are static and never deleted */
599 MSVCRT__iob
[i
]._file
= i
;
600 MSVCRT__iob
[i
]._tmpfname
= NULL
;
601 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
603 MSVCRT_stream_idx
= 3;
606 /* INTERNAL: Flush stdio file buffer */
607 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
609 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
610 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
611 int cnt
=file
->_ptr
-file
->_base
;
612 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
613 file
->_flag
|= MSVCRT__IOERR
;
617 if(file
->_flag
& MSVCRT__IORW
)
618 file
->_flag
&= ~MSVCRT__IOWRT
;
621 file
->_ptr
=file
->_base
;
626 /*********************************************************************
629 int CDECL
MSVCRT__isatty(int fd
)
631 TRACE(":fd (%d)\n",fd
);
633 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
636 /* INTERNAL: Allocate stdio file buffer */
637 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
639 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
640 && MSVCRT__isatty(file
->_file
))
643 file
->_base
= MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
645 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
646 file
->_flag
|= MSVCRT__IOMYBUF
;
648 file
->_base
= (char*)(&file
->_charbuf
);
650 file
->_flag
|= MSVCRT__IONBF
;
652 file
->_ptr
= file
->_base
;
657 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
658 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
660 static char buffers
[2][MSVCRT_BUFSIZ
];
662 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
663 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
664 || !MSVCRT__isatty(file
->_file
))
667 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
668 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
669 file
->_flag
|= MSVCRT__USERBUF
;
673 /* INTERNAL: Removes temporary buffer from stdout or stderr */
674 /* Only call this function when add_std_buffer returned TRUE */
675 static void remove_std_buffer(MSVCRT_FILE
*file
)
677 msvcrt_flush_buffer(file
);
678 file
->_ptr
= file
->_base
= NULL
;
679 file
->_bufsiz
= file
->_cnt
= 0;
680 file
->_flag
&= ~MSVCRT__USERBUF
;
683 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
684 static int msvcrt_int_to_base32(int num
, char *str
)
699 *p
= (num
& 31) + '0';
701 *p
+= ('a' - '0' - 10);
708 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
709 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
724 *p
= (num
& 31) + '0';
726 *p
+= ('a' - '0' - 10);
733 /*********************************************************************
734 * __iob_func(MSVCRT.@)
736 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
738 return &MSVCRT__iob
[0];
741 /*********************************************************************
744 int CDECL
MSVCRT__access(const char *filename
, int mode
)
746 DWORD attr
= GetFileAttributesA(filename
);
748 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
750 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
752 msvcrt_set_errno(GetLastError());
755 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
757 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
763 /*********************************************************************
764 * _access_s (MSVCRT.@)
766 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
768 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
769 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
771 if (MSVCRT__access(filename
, mode
) == -1)
772 return *MSVCRT__errno();
776 /*********************************************************************
777 * _waccess (MSVCRT.@)
779 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
781 DWORD attr
= GetFileAttributesW(filename
);
783 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
785 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
787 msvcrt_set_errno(GetLastError());
790 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
792 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
798 /*********************************************************************
799 * _waccess_s (MSVCRT.@)
801 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
803 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
804 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
806 if (MSVCRT__waccess(filename
, mode
) == -1)
807 return *MSVCRT__errno();
811 /*********************************************************************
814 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
816 DWORD oldFlags
= GetFileAttributesA(path
);
818 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
820 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
821 oldFlags
| FILE_ATTRIBUTE_READONLY
;
823 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
826 msvcrt_set_errno(GetLastError());
830 /*********************************************************************
833 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
835 DWORD oldFlags
= GetFileAttributesW(path
);
837 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
839 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
840 oldFlags
| FILE_ATTRIBUTE_READONLY
;
842 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
845 msvcrt_set_errno(GetLastError());
849 /*********************************************************************
852 int CDECL
MSVCRT__unlink(const char *path
)
854 TRACE("%s\n",debugstr_a(path
));
855 if(DeleteFileA(path
))
857 TRACE("failed (%d)\n",GetLastError());
858 msvcrt_set_errno(GetLastError());
862 /*********************************************************************
863 * _wunlink (MSVCRT.@)
865 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
867 TRACE("(%s)\n",debugstr_w(path
));
868 if(DeleteFileW(path
))
870 TRACE("failed (%d)\n",GetLastError());
871 msvcrt_set_errno(GetLastError());
875 /*********************************************************************
878 int CDECL
MSVCRT__commit(int fd
)
880 ioinfo
*info
= get_ioinfo(fd
);
883 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
885 if (info
->handle
== INVALID_HANDLE_VALUE
)
887 else if (!FlushFileBuffers(info
->handle
))
889 if (GetLastError() == ERROR_INVALID_HANDLE
)
891 /* FlushFileBuffers fails for console handles
892 * so we ignore this error.
898 TRACE(":failed-last error (%d)\n",GetLastError());
899 msvcrt_set_errno(GetLastError());
909 release_ioinfo(info
);
913 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
914 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
916 /* INTERNAL: Flush all stream buffer */
917 static int msvcrt_flush_all_buffers(int mask
)
919 int i
, num_flushed
= 0;
923 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
924 file
= msvcrt_get_file(i
);
928 if(file
->_flag
& mask
) {
936 TRACE(":flushed (%d) handles\n",num_flushed
);
940 /*********************************************************************
941 * _flushall (MSVCRT.@)
943 int CDECL
MSVCRT__flushall(void)
945 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
948 /*********************************************************************
951 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
956 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
959 MSVCRT__lock_file(file
);
960 ret
= MSVCRT__fflush_nolock(file
);
961 MSVCRT__unlock_file(file
);
967 /*********************************************************************
968 * _fflush_nolock (MSVCRT.@)
970 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
975 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
979 res
= msvcrt_flush_buffer(file
);
980 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
981 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
985 /*********************************************************************
988 int CDECL
MSVCRT__close(int fd
)
990 ioinfo
*info
= get_ioinfo(fd
);
994 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
995 if (!(info
->wxflag
& WX_OPEN
)) {
998 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1001 WARN(":failed-last error (%d)\n",GetLastError());
1002 msvcrt_set_errno(GetLastError());
1006 release_ioinfo(info
);
1010 /*********************************************************************
1013 * MSDN isn't clear on this point, but the remarks for _pipe
1014 * indicate file descriptors duplicated with _dup and _dup2 are always
1017 int CDECL
MSVCRT__dup2(int od
, int nd
)
1021 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1023 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
1027 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od
)->handle
,
1028 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1030 int wxflag
= get_ioinfo_nolock(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
1032 if (msvcrt_is_valid_fd(nd
))
1034 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
1037 CloseHandle(handle
);
1038 *MSVCRT__errno() = MSVCRT_EMFILE
;
1042 /* _dup2 returns 0, not nd, on success */
1049 msvcrt_set_errno(GetLastError());
1054 *MSVCRT__errno() = MSVCRT_EBADF
;
1061 /*********************************************************************
1064 int CDECL
MSVCRT__dup(int od
)
1069 fd
= MSVCRT_fdstart
;
1070 if (MSVCRT__dup2(od
, fd
) == 0)
1078 /*********************************************************************
1081 int CDECL
MSVCRT__eof(int fd
)
1083 ioinfo
*info
= get_ioinfo(fd
);
1084 DWORD curpos
,endpos
;
1085 LONG hcurpos
,hendpos
;
1087 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1089 if (info
->handle
== INVALID_HANDLE_VALUE
)
1091 release_ioinfo(info
);
1095 if (info
->wxflag
& WX_ATEOF
)
1097 release_ioinfo(info
);
1101 /* Otherwise we do it the hard way */
1102 hcurpos
= hendpos
= 0;
1103 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1104 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1106 if (curpos
== endpos
&& hcurpos
== hendpos
)
1108 /* FIXME: shouldn't WX_ATEOF be set here? */
1109 release_ioinfo(info
);
1113 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1114 release_ioinfo(info
);
1118 /*********************************************************************
1119 * _fcloseall (MSVCRT.@)
1121 int CDECL
MSVCRT__fcloseall(void)
1123 int num_closed
= 0, i
;
1127 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1128 file
= msvcrt_get_file(i
);
1130 if (file
->_flag
&& !MSVCRT_fclose(file
))
1135 TRACE(":closed (%d) handles\n",num_closed
);
1139 /* free everything on process exit */
1140 void msvcrt_free_io(void)
1146 MSVCRT__fcloseall();
1148 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1150 if(!MSVCRT___pioinfo
[i
])
1153 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1155 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1156 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1158 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1161 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1163 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1164 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1166 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1167 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1171 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1172 MSVCRT_free(MSVCRT_fstream
[i
]);
1174 DeleteCriticalSection(&MSVCRT_file_cs
);
1177 /*********************************************************************
1178 * _lseeki64 (MSVCRT.@)
1180 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1182 ioinfo
*info
= get_ioinfo(fd
);
1185 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1187 if (info
->handle
== INVALID_HANDLE_VALUE
)
1189 release_ioinfo(info
);
1193 if (whence
< 0 || whence
> 2)
1195 release_ioinfo(info
);
1196 *MSVCRT__errno() = MSVCRT_EINVAL
;
1200 TRACE(":fd (%d) to %s pos %s\n",
1201 fd
,wine_dbgstr_longlong(offset
),
1202 (whence
==SEEK_SET
)?"SEEK_SET":
1203 (whence
==SEEK_CUR
)?"SEEK_CUR":
1204 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1206 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1207 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1208 ofs
.QuadPart
= offset
;
1209 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1210 GetLastError() == ERROR_SUCCESS
)
1212 info
->wxflag
&= ~WX_ATEOF
;
1213 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1215 release_ioinfo(info
);
1216 return ofs
.QuadPart
;
1218 release_ioinfo(info
);
1219 TRACE(":error-last error (%d)\n",GetLastError());
1220 msvcrt_set_errno(GetLastError());
1224 /*********************************************************************
1227 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1229 return MSVCRT__lseeki64(fd
, offset
, whence
);
1232 /*********************************************************************
1233 * _lock_file (MSVCRT.@)
1235 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1237 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1238 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1240 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1243 /*********************************************************************
1244 * _unlock_file (MSVCRT.@)
1246 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1248 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1249 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1251 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1254 /*********************************************************************
1255 * _locking (MSVCRT.@)
1257 * This is untested; the underlying LockFile doesn't work yet.
1259 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1261 ioinfo
*info
= get_ioinfo(fd
);
1265 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1266 if (info
->handle
== INVALID_HANDLE_VALUE
)
1268 release_ioinfo(info
);
1272 if (mode
< 0 || mode
> 4)
1274 release_ioinfo(info
);
1275 *MSVCRT__errno() = MSVCRT_EINVAL
;
1279 TRACE(":fd (%d) by 0x%08x mode %s\n",
1280 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1281 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1282 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1283 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1284 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1287 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1289 release_ioinfo(info
);
1290 FIXME ("Seek failed\n");
1291 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1294 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1297 ret
= 1; /* just to satisfy gcc */
1300 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1305 else if (mode
== MSVCRT__LK_UNLCK
)
1306 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1308 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1309 /* FIXME - what about error settings? */
1310 release_ioinfo(info
);
1311 return ret
? 0 : -1;
1314 /*********************************************************************
1315 * _fseeki64 (MSVCRT.@)
1317 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1321 MSVCRT__lock_file(file
);
1322 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1323 MSVCRT__unlock_file(file
);
1328 /*********************************************************************
1329 * _fseeki64_nolock (MSVCRT.@)
1331 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1335 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1337 offset
+= MSVCRT__ftelli64_nolock(file
);
1340 /* Flush output if needed */
1341 msvcrt_flush_buffer(file
);
1342 /* Reset direction of i/o */
1343 if(file
->_flag
& MSVCRT__IORW
) {
1344 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1346 /* Clear end of file flag */
1347 file
->_flag
&= ~MSVCRT__IOEOF
;
1348 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1353 /*********************************************************************
1356 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1358 return MSVCRT__fseeki64( file
, offset
, whence
);
1361 /*********************************************************************
1362 * _fseek_nolock (MSVCRT.@)
1364 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1366 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1369 /*********************************************************************
1370 * _chsize_s (MSVCRT.@)
1372 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1378 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1380 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1383 info
= get_ioinfo(fd
);
1384 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1386 /* save the current file pointer */
1387 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1390 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1393 ret
= SetEndOfFile(info
->handle
);
1394 if (!ret
) msvcrt_set_errno(GetLastError());
1397 /* restore the file pointer */
1398 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1402 release_ioinfo(info
);
1403 return ret
? 0 : *MSVCRT__errno();
1406 /*********************************************************************
1407 * _chsize (MSVCRT.@)
1409 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1411 /* _chsize_s returns errno on failure but _chsize should return -1 */
1412 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1415 /*********************************************************************
1416 * clearerr (MSVCRT.@)
1418 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1420 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1422 MSVCRT__lock_file(file
);
1423 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1424 MSVCRT__unlock_file(file
);
1427 /*********************************************************************
1430 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1432 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1434 MSVCRT__lock_file(file
);
1435 MSVCRT__fseek_nolock(file
, 0L, SEEK_SET
);
1436 MSVCRT_clearerr(file
);
1437 MSVCRT__unlock_file(file
);
1440 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1442 int plus
= strchrW(mode
, '+') != NULL
;
1444 TRACE("%s\n", debugstr_w(mode
));
1446 while(*mode
== ' ') mode
++;
1451 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1452 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1455 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1456 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1459 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1460 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1463 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1467 *stream_flags
|= MSVCRT__commode
;
1469 while (*mode
&& *mode
!=',')
1473 *open_flags
|= MSVCRT__O_BINARY
;
1474 *open_flags
&= ~MSVCRT__O_TEXT
;
1477 *open_flags
|= MSVCRT__O_TEXT
;
1478 *open_flags
&= ~MSVCRT__O_BINARY
;
1481 *open_flags
|= MSVCRT__O_TEMPORARY
;
1484 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1487 *stream_flags
|= MSVCRT__IOCOMMIT
;
1490 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1493 *open_flags
|= MSVCRT__O_NOINHERIT
;
1502 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1505 ERR("incorrect mode flag: %c\n", mode
[-1]);
1511 static const WCHAR ccs
[] = {'c','c','s'};
1512 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1513 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1514 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1517 while(*mode
== ' ') mode
++;
1518 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1520 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1521 while(*mode
== ' ') mode
++;
1522 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1525 while(*mode
== ' ') mode
++;
1527 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1529 *open_flags
|= MSVCRT__O_U8TEXT
;
1530 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1532 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1534 *open_flags
|= MSVCRT__O_U16TEXT
;
1535 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1537 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1539 *open_flags
|= MSVCRT__O_WTEXT
;
1540 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1544 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1548 while(*mode
== ' ') mode
++;
1551 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1556 /*********************************************************************
1557 * _fdopen (MSVCRT.@)
1559 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1562 MSVCRT_wchar_t
*modeW
= NULL
;
1564 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1566 ret
= MSVCRT__wfdopen(fd
, modeW
);
1572 /*********************************************************************
1573 * _wfdopen (MSVCRT.@)
1575 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1577 int open_flags
, stream_flags
;
1580 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1583 if (!(file
= msvcrt_alloc_fp()))
1585 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1590 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1596 /*********************************************************************
1597 * _filelength (MSVCRT.@)
1599 LONG CDECL
MSVCRT__filelength(int fd
)
1601 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1604 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1607 if (endPos
!= curPos
)
1608 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1615 /*********************************************************************
1616 * _filelengthi64 (MSVCRT.@)
1618 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1620 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1623 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1626 if (endPos
!= curPos
)
1627 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1634 /*********************************************************************
1635 * _fileno (MSVCRT.@)
1637 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1639 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1643 /*********************************************************************
1644 * _fstat64 (MSVCRT.@)
1646 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1648 ioinfo
*info
= get_ioinfo(fd
);
1651 BY_HANDLE_FILE_INFORMATION hfi
;
1653 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1654 if (info
->handle
== INVALID_HANDLE_VALUE
)
1656 release_ioinfo(info
);
1662 WARN(":failed-NULL buf\n");
1663 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1664 release_ioinfo(info
);
1668 memset(&hfi
, 0, sizeof(hfi
));
1669 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1670 type
= GetFileType(info
->handle
);
1671 if (type
== FILE_TYPE_PIPE
)
1673 buf
->st_dev
= buf
->st_rdev
= fd
;
1674 buf
->st_mode
= S_IFIFO
;
1677 else if (type
== FILE_TYPE_CHAR
)
1679 buf
->st_dev
= buf
->st_rdev
= fd
;
1680 buf
->st_mode
= S_IFCHR
;
1683 else /* FILE_TYPE_DISK etc. */
1685 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1687 WARN(":failed-last error (%d)\n",GetLastError());
1688 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1689 release_ioinfo(info
);
1692 buf
->st_mode
= S_IFREG
| 0444;
1693 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1694 buf
->st_mode
|= 0222;
1695 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1696 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1698 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1699 buf
->st_mtime
= buf
->st_ctime
= dw
;
1700 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1702 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1704 release_ioinfo(info
);
1708 /*********************************************************************
1709 * _fstati64 (MSVCRT.@)
1711 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1714 struct MSVCRT__stat64 buf64
;
1716 ret
= MSVCRT__fstat64(fd
, &buf64
);
1718 msvcrt_stat64_to_stati64(&buf64
, buf
);
1722 /*********************************************************************
1725 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1727 struct MSVCRT__stat64 buf64
;
1729 ret
= MSVCRT__fstat64(fd
, &buf64
);
1731 msvcrt_stat64_to_stat(&buf64
, buf
);
1735 /*********************************************************************
1736 * _fstat32 (MSVCR80.@)
1738 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1741 struct MSVCRT__stat64 buf64
;
1743 ret
= MSVCRT__fstat64(fd
, &buf64
);
1745 msvcrt_stat64_to_stat32(&buf64
, buf
);
1749 /*********************************************************************
1750 * _fstat64i32 (MSVCR80.@)
1752 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1755 struct MSVCRT__stat64 buf64
;
1757 ret
= MSVCRT__fstat64(fd
, &buf64
);
1759 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1763 /*********************************************************************
1764 * _futime64 (MSVCRT.@)
1766 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1768 HANDLE hand
= msvcrt_fdtoh(fd
);
1773 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1778 time_to_filetime( t
->actime
, &at
);
1779 time_to_filetime( t
->modtime
, &wt
);
1782 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1784 msvcrt_set_errno(GetLastError());
1790 /*********************************************************************
1791 * _futime32 (MSVCRT.@)
1793 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1797 struct MSVCRT___utimbuf64 t64
;
1798 t64
.actime
= t
->actime
;
1799 t64
.modtime
= t
->modtime
;
1800 return _futime64( fd
, &t64
);
1803 return _futime64( fd
, NULL
);
1806 /*********************************************************************
1807 * _futime (MSVCRT.@)
1810 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1812 return _futime64( fd
, t
);
1815 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1817 return _futime32( fd
, t
);
1821 /*********************************************************************
1822 * _get_osfhandle (MSVCRT.@)
1824 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1826 HANDLE hand
= msvcrt_fdtoh(fd
);
1827 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1829 return (MSVCRT_intptr_t
)hand
;
1832 /*********************************************************************
1833 * _mktemp_s (MSVCRT.@)
1835 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1839 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1840 return MSVCRT_EINVAL
;
1842 for(len
=0; len
<size
; len
++)
1845 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1848 return MSVCRT_EINVAL
;
1851 for(xno
=1; xno
<=6; xno
++)
1852 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1853 return MSVCRT_EINVAL
;
1855 id
= GetCurrentProcessId();
1856 for(xno
=1; xno
<6; xno
++) {
1857 pattern
[len
-xno
] = id
%10 + '0';
1861 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1862 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1867 *MSVCRT__errno() = MSVCRT_EEXIST
;
1868 return MSVCRT_EEXIST
;
1871 /*********************************************************************
1872 * _mktemp (MSVCRT.@)
1874 char * CDECL
MSVCRT__mktemp(char *pattern
)
1877 char *retVal
= pattern
;
1885 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1889 id
= GetCurrentProcessId();
1893 int tempNum
= id
/ 10;
1894 *pattern
-- = id
- (tempNum
* 10) + '0';
1900 *pattern
= letter
++;
1901 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1903 } while(letter
<= 'z');
1907 /*********************************************************************
1908 * _wmktemp_s (MSVCRT.@)
1910 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1914 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1915 return MSVCRT_EINVAL
;
1917 for(len
=0; len
<size
; len
++)
1920 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1923 return MSVCRT_EINVAL
;
1926 for(xno
=1; xno
<=6; xno
++)
1927 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1928 return MSVCRT_EINVAL
;
1930 id
= GetCurrentProcessId();
1931 for(xno
=1; xno
<6; xno
++) {
1932 pattern
[len
-xno
] = id
%10 + '0';
1936 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1937 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1942 *MSVCRT__errno() = MSVCRT_EEXIST
;
1943 return MSVCRT_EEXIST
;
1946 /*********************************************************************
1947 * _wmktemp (MSVCRT.@)
1949 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1952 MSVCRT_wchar_t
*retVal
= pattern
;
1954 MSVCRT_wchar_t letter
= 'a';
1960 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1964 id
= GetCurrentProcessId();
1968 int tempNum
= id
/ 10;
1969 *pattern
-- = id
- (tempNum
* 10) + '0';
1975 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1977 *pattern
= letter
++;
1978 } while(letter
!= '|');
1982 static unsigned split_oflags(unsigned oflags
)
1985 unsigned unsupp
; /* until we support everything */
1987 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1988 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1989 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1990 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1991 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1992 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1993 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1994 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1995 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1997 if ((unsupp
= oflags
& ~(
1998 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1999 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2000 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2001 MSVCRT__O_NOINHERIT
|
2002 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2003 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2005 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2010 /*********************************************************************
2013 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2016 SECURITY_ATTRIBUTES sa
;
2017 HANDLE readHandle
, writeHandle
;
2021 *MSVCRT__errno() = MSVCRT_EINVAL
;
2025 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2026 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2027 sa
.lpSecurityDescriptor
= NULL
;
2028 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2030 unsigned int wxflags
= split_oflags(textmode
);
2034 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2038 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2046 MSVCRT__close(pfds
[0]);
2047 CloseHandle(writeHandle
);
2048 *MSVCRT__errno() = MSVCRT_EMFILE
;
2053 CloseHandle(readHandle
);
2054 CloseHandle(writeHandle
);
2055 *MSVCRT__errno() = MSVCRT_EMFILE
;
2060 msvcrt_set_errno(GetLastError());
2065 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2067 char bom
[sizeof(utf8_bom
)];
2070 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2072 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2075 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2076 oflags
|= MSVCRT__O_U8TEXT
;
2077 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2079 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2080 oflags
|= MSVCRT__O_U16TEXT
;
2082 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2088 /*********************************************************************
2089 * _wsopen_s (MSVCRT.@)
2091 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2093 DWORD access
= 0, creation
= 0, attrib
;
2094 SECURITY_ATTRIBUTES sa
;
2095 DWORD sharing
, type
;
2099 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2100 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2102 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2105 wxflag
= split_oflags(oflags
);
2106 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2108 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2109 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2110 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2113 if (oflags
& MSVCRT__O_CREAT
)
2115 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2116 FIXME(": pmode 0x%04x ignored\n", pmode
);
2118 if (oflags
& MSVCRT__O_EXCL
)
2119 creation
= CREATE_NEW
;
2120 else if (oflags
& MSVCRT__O_TRUNC
)
2121 creation
= CREATE_ALWAYS
;
2123 creation
= OPEN_ALWAYS
;
2125 else /* no MSVCRT__O_CREAT */
2127 if (oflags
& MSVCRT__O_TRUNC
)
2128 creation
= TRUNCATE_EXISTING
;
2130 creation
= OPEN_EXISTING
;
2135 case MSVCRT__SH_DENYRW
:
2138 case MSVCRT__SH_DENYWR
:
2139 sharing
= FILE_SHARE_READ
;
2141 case MSVCRT__SH_DENYRD
:
2142 sharing
= FILE_SHARE_WRITE
;
2144 case MSVCRT__SH_DENYNO
:
2145 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2148 ERR( "Unhandled shflags 0x%x\n", shflags
);
2149 return MSVCRT_EINVAL
;
2152 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2153 attrib
= FILE_ATTRIBUTE_READONLY
;
2155 attrib
= FILE_ATTRIBUTE_NORMAL
;
2157 if (oflags
& MSVCRT__O_TEMPORARY
)
2159 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2161 sharing
|= FILE_SHARE_DELETE
;
2164 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2165 sa
.lpSecurityDescriptor
= NULL
;
2166 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2168 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2169 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2170 && !(access
&GENERIC_READ
))
2172 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2173 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2174 if (hand
!= INVALID_HANDLE_VALUE
)
2176 oflags
= check_bom(hand
, oflags
, FALSE
);
2180 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2183 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2184 if (hand
== INVALID_HANDLE_VALUE
) {
2185 WARN(":failed-last error (%d)\n",GetLastError());
2186 msvcrt_set_errno(GetLastError());
2187 return *MSVCRT__errno();
2190 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2192 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2193 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2194 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2196 if (oflags
& MSVCRT__O_U8TEXT
)
2198 DWORD written
= 0, tmp
;
2200 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2201 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2203 if (written
!= sizeof(utf8_bom
)) {
2204 WARN("error writing BOM\n");
2206 msvcrt_set_errno(GetLastError());
2207 return *MSVCRT__errno();
2212 DWORD written
= 0, tmp
;
2214 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2215 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2217 if (written
!= sizeof(utf16_bom
))
2219 WARN("error writing BOM\n");
2221 msvcrt_set_errno(GetLastError());
2222 return *MSVCRT__errno();
2226 else if (access
& GENERIC_READ
)
2227 oflags
= check_bom(hand
, oflags
, TRUE
);
2230 type
= GetFileType(hand
);
2231 if (type
== FILE_TYPE_CHAR
)
2233 else if (type
== FILE_TYPE_PIPE
)
2236 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2238 return *MSVCRT__errno();
2240 if (oflags
& MSVCRT__O_WTEXT
)
2241 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2242 else if (oflags
& MSVCRT__O_U16TEXT
)
2243 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2244 else if (oflags
& MSVCRT__O_U8TEXT
)
2245 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2247 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2251 /*********************************************************************
2252 * _wsopen (MSVCRT.@)
2254 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2259 if (oflags
& MSVCRT__O_CREAT
)
2263 __ms_va_start(ap
, shflags
);
2264 pmode
= va_arg(ap
, int);
2270 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2274 /*********************************************************************
2275 * _sopen_s (MSVCRT.@)
2277 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2279 MSVCRT_wchar_t
*pathW
;
2282 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2283 return MSVCRT_EINVAL
;
2285 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2286 return MSVCRT_EINVAL
;
2288 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2293 /*********************************************************************
2296 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2301 if (oflags
& MSVCRT__O_CREAT
)
2305 __ms_va_start(ap
, shflags
);
2306 pmode
= va_arg(ap
, int);
2312 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2316 /*********************************************************************
2319 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2323 if (flags
& MSVCRT__O_CREAT
)
2326 __ms_va_start(ap
, flags
);
2327 pmode
= va_arg(ap
, int);
2329 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2332 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2335 /*********************************************************************
2338 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2342 if (flags
& MSVCRT__O_CREAT
)
2345 __ms_va_start(ap
, flags
);
2346 pmode
= va_arg(ap
, int);
2348 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2351 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2354 /*********************************************************************
2357 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2359 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2360 return MSVCRT__open(path
, usedFlags
);
2363 /*********************************************************************
2364 * _wcreat (MSVCRT.@)
2366 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2368 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2369 return MSVCRT__wopen(path
, usedFlags
);
2372 /*********************************************************************
2373 * _open_osfhandle (MSVCRT.@)
2375 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2380 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2381 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2382 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2383 * text - it never sets MSVCRT__O_BINARY.
2385 /* don't let split_oflags() decide the mode if no mode is passed */
2386 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2387 oflags
|= MSVCRT__O_BINARY
;
2389 flags
= GetFileType((HANDLE
)handle
);
2390 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2392 msvcrt_set_errno(GetLastError());
2396 if (flags
== FILE_TYPE_CHAR
)
2398 else if (flags
== FILE_TYPE_PIPE
)
2402 flags
|= split_oflags(oflags
);
2404 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2405 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2409 /*********************************************************************
2412 int CDECL
MSVCRT__rmtmp(void)
2414 int num_removed
= 0, i
;
2418 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2419 file
= msvcrt_get_file(i
);
2421 if (file
->_tmpfname
)
2423 MSVCRT_fclose(file
);
2430 TRACE(":removed (%d) temp files\n",num_removed
);
2434 static inline int get_utf8_char_len(char ch
)
2436 if((ch
&0xf8) == 0xf0)
2438 else if((ch
&0xf0) == 0xe0)
2440 else if((ch
&0xe0) == 0xc0)
2445 /*********************************************************************
2446 * (internal) read_utf8
2448 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2450 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2451 HANDLE hand
= fdinfo
->handle
;
2452 char min_buf
[4], *readbuf
, lookahead
;
2453 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2455 /* make the buffer big enough to hold at least one character */
2456 /* read bytes have to fit to output and lookahead buffers */
2458 readbuf_size
= count
< 4 ? 4 : count
;
2459 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2464 if(fdinfo
->lookahead
[0] != '\n') {
2465 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2466 fdinfo
->lookahead
[0] = '\n';
2468 if(fdinfo
->lookahead
[1] != '\n') {
2469 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2470 fdinfo
->lookahead
[1] = '\n';
2472 if(fdinfo
->lookahead
[2] != '\n') {
2473 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2474 fdinfo
->lookahead
[2] = '\n';
2479 /* NOTE: this case is broken in native dll, reading
2480 * sometimes fails when small buffer is passed
2483 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2484 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2485 fdinfo
->wxflag
|= WX_ATEOF
;
2488 msvcrt_set_errno(GetLastError());
2491 }else if(!num_read
) {
2492 fdinfo
->wxflag
|= WX_ATEOF
;
2498 char_len
= get_utf8_char_len(readbuf
[0]);
2500 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2504 if(readbuf
[0] == '\n')
2505 fdinfo
->wxflag
|= WX_READNL
;
2507 fdinfo
->wxflag
&= ~WX_READNL
;
2509 if(readbuf
[0] == 0x1a) {
2510 fdinfo
->wxflag
|= WX_ATEOF
;
2514 if(readbuf
[0] == '\r') {
2515 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2517 else if(lookahead
== '\n')
2521 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2522 fdinfo
->lookahead
[0] = lookahead
;
2524 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2529 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2530 msvcrt_set_errno(GetLastError());
2537 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2540 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2541 fdinfo
->wxflag
|= WX_ATEOF
;
2542 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2545 msvcrt_set_errno(GetLastError());
2546 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2549 }else if(!pos
&& !num_read
) {
2550 fdinfo
->wxflag
|= WX_ATEOF
;
2551 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2556 if(readbuf
[0] == '\n')
2557 fdinfo
->wxflag
|= WX_READNL
;
2559 fdinfo
->wxflag
&= ~WX_READNL
;
2561 /* Find first byte of last character (may be incomplete) */
2562 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2563 if((readbuf
[i
]&0xc0) != 0x80)
2565 char_len
= get_utf8_char_len(readbuf
[i
]);
2566 if(char_len
+i
<= pos
)
2569 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2571 fdinfo
->lookahead
[0] = readbuf
[i
];
2573 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2575 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2577 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2581 for(i
=0, j
=0; i
<pos
; i
++) {
2582 if(readbuf
[i
] == 0x1a) {
2583 fdinfo
->wxflag
|= WX_ATEOF
;
2587 /* strip '\r' if followed by '\n' */
2588 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2589 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2590 readbuf
[j
++] = '\r';
2591 }else if(lookahead
== '\n' && j
==0) {
2592 readbuf
[j
++] = '\n';
2594 if(lookahead
!= '\n')
2595 readbuf
[j
++] = '\r';
2597 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2598 fdinfo
->lookahead
[0] = lookahead
;
2600 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2602 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2603 readbuf
[j
++] = readbuf
[i
];
2608 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2609 msvcrt_set_errno(GetLastError());
2610 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2614 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2618 /*********************************************************************
2621 * When reading \r as last character in text mode, read() positions
2622 * the file pointer on the \r character while getc() goes on to
2625 static int read_i(int fd
, void *buf
, unsigned int count
)
2627 DWORD num_read
, utf16
;
2628 char *bufstart
= buf
;
2629 HANDLE hand
= msvcrt_fdtoh(fd
);
2630 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2635 if (fdinfo
->wxflag
& WX_ATEOF
) {
2636 TRACE("already at EOF, returning 0\n");
2639 /* Don't trace small reads, it gets *very* annoying */
2641 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2642 if (hand
== INVALID_HANDLE_VALUE
)
2644 *MSVCRT__errno() = MSVCRT_EBADF
;
2648 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2649 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2651 *MSVCRT__errno() = MSVCRT_EINVAL
;
2655 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2656 return read_utf8(fd
, buf
, count
);
2658 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2660 if (fdinfo
->lookahead
[0] != '\n')
2662 bufstart
[0] = fdinfo
->lookahead
[0];
2663 fdinfo
->lookahead
[0] = '\n';
2667 bufstart
[1] = fdinfo
->lookahead
[1];
2668 fdinfo
->lookahead
[1] = '\n';
2671 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2672 num_read
+= 1+utf16
;
2677 if(utf16
&& (num_read
&1))
2679 /* msvcr90 uses uninitialized value from the buffer in this case */
2680 /* msvcrt ignores additional data */
2681 ERR("got odd number of bytes in UTF16 mode\n");
2685 if (count
!= 0 && num_read
== 0)
2687 fdinfo
->wxflag
|= WX_ATEOF
;
2688 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2690 else if (fdinfo
->wxflag
& WX_TEXT
)
2694 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2695 fdinfo
->wxflag
|= WX_READNL
;
2697 fdinfo
->wxflag
&= ~WX_READNL
;
2699 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2701 /* in text mode, a ctrl-z signals EOF */
2702 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2704 fdinfo
->wxflag
|= WX_ATEOF
;
2705 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2709 /* in text mode, strip \r if followed by \n */
2710 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2715 lookahead
[1] = '\n';
2716 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2718 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2720 bufstart
[j
++] = '\n';
2721 if(utf16
) bufstart
[j
++] = 0;
2725 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2727 bufstart
[j
++] = '\r';
2728 if(utf16
) bufstart
[j
++] = 0;
2731 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2733 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2735 bufstart
[j
++] = '\n';
2736 if (utf16
) bufstart
[j
++] = 0;
2740 fdinfo
->lookahead
[0] = lookahead
[0];
2741 fdinfo
->lookahead
[1] = lookahead
[1];
2745 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2750 bufstart
[j
++] = '\r';
2751 if(utf16
) bufstart
[j
++] = 0;
2754 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2755 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2757 bufstart
[j
++] = bufstart
[i
];
2758 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2766 if (GetLastError() == ERROR_BROKEN_PIPE
)
2768 TRACE(":end-of-pipe\n");
2769 fdinfo
->wxflag
|= WX_ATEOF
;
2774 TRACE(":failed-last error (%d)\n",GetLastError());
2780 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2784 /*********************************************************************
2787 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2790 num_read
= read_i(fd
, buf
, count
);
2794 /*********************************************************************
2795 * _setmode (MSVCRT.@)
2797 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2799 int ret
= get_ioinfo_nolock(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2800 if(ret
==MSVCRT__O_TEXT
&& (get_ioinfo_nolock(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2801 ret
= MSVCRT__O_WTEXT
;
2803 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2804 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2805 *MSVCRT__errno() = MSVCRT_EINVAL
;
2809 if(mode
== MSVCRT__O_BINARY
) {
2810 get_ioinfo_nolock(fd
)->wxflag
&= ~WX_TEXT
;
2811 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2815 get_ioinfo_nolock(fd
)->wxflag
|= WX_TEXT
;
2816 if(mode
== MSVCRT__O_TEXT
)
2817 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2818 else if(mode
== MSVCRT__O_U8TEXT
)
2819 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2821 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2826 /*********************************************************************
2827 * _stat64 (MSVCRT.@)
2829 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2832 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2833 unsigned short mode
= ALL_S_IREAD
;
2836 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2838 plen
= strlen(path
);
2839 while (plen
&& path
[plen
-1]==' ')
2842 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2843 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2845 *MSVCRT__errno() = MSVCRT_ENOENT
;
2849 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2851 TRACE("failed (%d)\n",GetLastError());
2852 *MSVCRT__errno() = MSVCRT_ENOENT
;
2856 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2858 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2859 Bon 011120: This FIXME seems incorrect
2860 Also a letter as first char isn't enough to be classified
2863 if (isalpha(*path
)&& (*(path
+1)==':'))
2864 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2866 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2868 /* Dir, or regular file? */
2869 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2870 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2873 mode
|= MSVCRT__S_IFREG
;
2875 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2877 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2878 (tolower(path
[plen
-3]) << 16);
2879 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2880 mode
|= ALL_S_IEXEC
;
2884 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2885 mode
|= ALL_S_IWRITE
;
2887 buf
->st_mode
= mode
;
2889 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2890 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2892 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2893 buf
->st_mtime
= buf
->st_ctime
= dw
;
2894 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2895 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2896 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2900 /*********************************************************************
2901 * _stati64 (MSVCRT.@)
2903 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2906 struct MSVCRT__stat64 buf64
;
2908 ret
= MSVCRT_stat64(path
, &buf64
);
2910 msvcrt_stat64_to_stati64(&buf64
, buf
);
2914 /*********************************************************************
2917 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2920 struct MSVCRT__stat64 buf64
;
2922 ret
= MSVCRT_stat64( path
, &buf64
);
2924 msvcrt_stat64_to_stat(&buf64
, buf
);
2928 /*********************************************************************
2929 * _stat32 (MSVCR100.@)
2931 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2934 struct MSVCRT__stat64 buf64
;
2936 ret
= MSVCRT_stat64(path
, &buf64
);
2938 msvcrt_stat64_to_stat32(&buf64
, buf
);
2942 /*********************************************************************
2943 * _stat32i64 (MSVCR100.@)
2945 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2948 struct MSVCRT__stat64 buf64
;
2950 ret
= MSVCRT_stat64(path
, &buf64
);
2952 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2956 /*********************************************************************
2957 * _stat64i32 (MSVCR100.@)
2959 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2962 struct MSVCRT__stat64 buf64
;
2964 ret
= MSVCRT_stat64(path
, &buf64
);
2966 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2970 /*********************************************************************
2971 * _wstat64 (MSVCRT.@)
2973 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2976 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2977 unsigned short mode
= ALL_S_IREAD
;
2980 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2982 plen
= strlenW(path
);
2983 while (plen
&& path
[plen
-1]==' ')
2986 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2987 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2989 *MSVCRT__errno() = MSVCRT_ENOENT
;
2993 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2995 TRACE("failed (%d)\n",GetLastError());
2996 *MSVCRT__errno() = MSVCRT_ENOENT
;
3000 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3002 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3003 if (MSVCRT_iswalpha(*path
))
3004 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3006 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3008 /* Dir, or regular file? */
3009 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3010 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3013 mode
|= MSVCRT__S_IFREG
;
3015 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3017 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3018 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3019 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3020 mode
|= ALL_S_IEXEC
;
3024 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3025 mode
|= ALL_S_IWRITE
;
3027 buf
->st_mode
= mode
;
3029 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3030 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3032 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3033 buf
->st_mtime
= buf
->st_ctime
= dw
;
3034 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3035 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3036 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3040 /*********************************************************************
3041 * _wstati64 (MSVCRT.@)
3043 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3046 struct MSVCRT__stat64 buf64
;
3048 ret
= MSVCRT__wstat64(path
, &buf64
);
3050 msvcrt_stat64_to_stati64(&buf64
, buf
);
3054 /*********************************************************************
3057 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3060 struct MSVCRT__stat64 buf64
;
3062 ret
= MSVCRT__wstat64( path
, &buf64
);
3063 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3067 /*********************************************************************
3068 * _wstat32 (MSVCR100.@)
3070 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3073 struct MSVCRT__stat64 buf64
;
3075 ret
= MSVCRT__wstat64(path
, &buf64
);
3077 msvcrt_stat64_to_stat32(&buf64
, buf
);
3081 /*********************************************************************
3082 * _wstat32i64 (MSVCR100.@)
3084 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3087 struct MSVCRT__stat64 buf64
;
3089 ret
= MSVCRT__wstat64(path
, &buf64
);
3091 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3095 /*********************************************************************
3096 * _wstat64i32 (MSVCR100.@)
3098 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3101 struct MSVCRT__stat64 buf64
;
3103 ret
= MSVCRT__wstat64(path
, &buf64
);
3105 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3109 /*********************************************************************
3112 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3114 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3117 /*********************************************************************
3118 * _telli64 (MSVCRT.@)
3120 __int64 CDECL
_telli64(int fd
)
3122 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3125 /*********************************************************************
3126 * _tempnam (MSVCRT.@)
3128 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3130 char tmpbuf
[MAX_PATH
];
3131 const char *tmp_dir
= MSVCRT_getenv("TMP");
3133 if (tmp_dir
) dir
= tmp_dir
;
3135 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3136 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3138 TRACE("got name (%s)\n",tmpbuf
);
3139 DeleteFileA(tmpbuf
);
3140 return MSVCRT__strdup(tmpbuf
);
3142 TRACE("failed (%d)\n",GetLastError());
3146 /*********************************************************************
3147 * _wtempnam (MSVCRT.@)
3149 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3151 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3152 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3153 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3155 if (tmp_dir
) dir
= tmp_dir
;
3157 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3158 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3160 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3161 DeleteFileW(tmpbuf
);
3162 return MSVCRT__wcsdup(tmpbuf
);
3164 TRACE("failed (%d)\n",GetLastError());
3168 /*********************************************************************
3171 int CDECL
MSVCRT__umask(int umask
)
3173 int old_umask
= MSVCRT_umask
;
3174 TRACE("(%d)\n",umask
);
3175 MSVCRT_umask
= umask
;
3179 /*********************************************************************
3180 * _utime64 (MSVCRT.@)
3182 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3184 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3188 int retVal
= _futime64(fd
, t
);
3195 /*********************************************************************
3196 * _utime32 (MSVCRT.@)
3198 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3202 struct MSVCRT___utimbuf64 t64
;
3203 t64
.actime
= t
->actime
;
3204 t64
.modtime
= t
->modtime
;
3205 return _utime64( path
, &t64
);
3208 return _utime64( path
, NULL
);
3211 /*********************************************************************
3215 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3217 return _utime64( path
, t
);
3220 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3222 return _utime32( path
, t
);
3226 /*********************************************************************
3227 * _wutime64 (MSVCRT.@)
3229 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3231 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3235 int retVal
= _futime64(fd
, t
);
3242 /*********************************************************************
3243 * _wutime32 (MSVCRT.@)
3245 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3249 struct MSVCRT___utimbuf64 t64
;
3250 t64
.actime
= t
->actime
;
3251 t64
.modtime
= t
->modtime
;
3252 return _wutime64( path
, &t64
);
3255 return _wutime64( path
, NULL
);
3258 /*********************************************************************
3259 * _wutime (MSVCRT.@)
3262 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3264 return _wutime64( path
, t
);
3267 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3269 return _wutime32( path
, t
);
3273 /*********************************************************************
3276 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3279 ioinfo
*info
= get_ioinfo_nolock(fd
);
3280 HANDLE hand
= info
->handle
;
3282 /* Don't trace small writes, it gets *very* annoying */
3285 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3287 if (hand
== INVALID_HANDLE_VALUE
)
3289 *MSVCRT__errno() = MSVCRT_EBADF
;
3293 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3295 *MSVCRT__errno() = MSVCRT_EINVAL
;
3299 /* If appending, go to EOF */
3300 if (info
->wxflag
& WX_APPEND
)
3301 MSVCRT__lseek(fd
, 0, FILE_END
);
3303 if (!(info
->wxflag
& WX_TEXT
))
3305 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3306 && (num_written
== count
))
3308 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3309 hand
, GetLastError());
3310 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3314 unsigned int i
, j
, nr_lf
, size
;
3317 const char *s
= buf
, *buf_start
= buf
;
3319 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3321 /* find number of \n */
3322 for (nr_lf
=0, i
=0; i
<count
; i
++)
3328 if ((q
= p
= MSVCRT_malloc(size
)))
3330 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3339 FIXME("Malloc failed\n");
3351 else if (info
->exflag
& EF_UTF16
)
3353 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3354 if (s
[i
]=='\n' && s
[i
+1]==0)
3359 if ((q
= p
= MSVCRT_malloc(size
)))
3361 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3363 if (s
[i
]=='\n' && s
[i
+1]==0)
3374 FIXME("Malloc failed\n");
3390 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3391 if (s
[i
]=='\n' && s
[i
+1]==0)
3394 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3396 msvcrt_set_errno(GetLastError());
3401 size
= conv_len
+nr_lf
;
3402 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3404 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3406 if (s
[i
]=='\n' && s
[i
+1]==0)
3414 q
= p
+count
+nr_lf
*2;
3415 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3416 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3420 FIXME("Malloc failed\n");
3427 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3431 if (num_written
!= size
)
3433 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3434 fd
, hand
, GetLastError(), num_written
);
3435 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3436 return s
- buf_start
;
3444 /*********************************************************************
3447 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3451 MSVCRT__lock_file(file
);
3452 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3453 if (len
== sizeof(val
)) {
3454 MSVCRT__unlock_file(file
);
3458 file
->_flag
|= MSVCRT__IOERR
;
3459 MSVCRT__unlock_file(file
);
3463 /*********************************************************************
3466 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3470 MSVCRT__lock_file(file
);
3471 ret
= MSVCRT__fclose_nolock(file
);
3472 MSVCRT__unlock_file(file
);
3477 /*********************************************************************
3478 * _fclose_nolock (MSVCRT.@)
3480 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3485 MSVCRT_free(file
->_tmpfname
);
3486 file
->_tmpfname
= NULL
;
3487 /* flush stdio buffers */
3488 if(file
->_flag
& MSVCRT__IOWRT
)
3489 MSVCRT__fflush_nolock(file
);
3490 if(file
->_flag
& MSVCRT__IOMYBUF
)
3491 MSVCRT_free(file
->_base
);
3493 r
=MSVCRT__close(file
->_file
);
3496 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3499 /*********************************************************************
3502 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3504 return file
->_flag
& MSVCRT__IOEOF
;
3507 /*********************************************************************
3510 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3512 return file
->_flag
& MSVCRT__IOERR
;
3515 /*********************************************************************
3516 * _filbuf (MSVCRT.@)
3518 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3522 if(file
->_flag
& MSVCRT__IOSTRG
)
3525 /* Allocate buffer if needed */
3526 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3527 msvcrt_alloc_buffer(file
);
3529 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3530 if(file
->_flag
& MSVCRT__IORW
)
3531 file
->_flag
|= MSVCRT__IOREAD
;
3536 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3538 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3539 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3545 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3547 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3553 file
->_ptr
= file
->_base
+1;
3554 c
= *(unsigned char *)file
->_base
;
3559 /*********************************************************************
3562 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3566 MSVCRT__lock_file(file
);
3567 ret
= MSVCRT__fgetc_nolock(file
);
3568 MSVCRT__unlock_file(file
);
3573 /*********************************************************************
3574 * _fgetc_nolock (MSVCRT.@)
3576 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3583 i
= (unsigned char *)file
->_ptr
++;
3586 j
= MSVCRT__filbuf(file
);
3591 /*********************************************************************
3592 * _fgetchar (MSVCRT.@)
3594 int CDECL
MSVCRT__fgetchar(void)
3596 return MSVCRT_fgetc(MSVCRT_stdin
);
3599 /*********************************************************************
3602 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3604 int cc
= MSVCRT_EOF
;
3605 char * buf_start
= s
;
3607 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3608 file
,file
->_file
,s
,size
);
3610 MSVCRT__lock_file(file
);
3612 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3617 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3619 TRACE(":nothing read\n");
3620 MSVCRT__unlock_file(file
);
3623 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3626 TRACE(":got %s\n", debugstr_a(buf_start
));
3627 MSVCRT__unlock_file(file
);
3631 /*********************************************************************
3634 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3638 MSVCRT__lock_file(file
);
3639 ret
= MSVCRT__fgetwc_nolock(file
);
3640 MSVCRT__unlock_file(file
);
3645 /*********************************************************************
3646 * _fgetwc_nolock (MSVCRT.@)
3648 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3653 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3654 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3657 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3658 ch
= MSVCRT__fgetc_nolock(file
);
3659 if(ch
== MSVCRT_EOF
) {
3666 char mbs
[MSVCRT_MB_LEN_MAX
];
3669 ch
= MSVCRT__fgetc_nolock(file
);
3670 if(ch
!= MSVCRT_EOF
) {
3672 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3673 ch
= MSVCRT__fgetc_nolock(file
);
3674 if(ch
!= MSVCRT_EOF
) {
3683 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3690 /*********************************************************************
3693 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3700 MSVCRT__lock_file(file
);
3701 for (j
=0; j
<sizeof(int); j
++) {
3702 k
= MSVCRT__fgetc_nolock(file
);
3703 if (k
== MSVCRT_EOF
) {
3704 file
->_flag
|= MSVCRT__IOEOF
;
3705 MSVCRT__unlock_file(file
);
3711 MSVCRT__unlock_file(file
);
3715 /*********************************************************************
3718 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3720 return MSVCRT_fgetwc(file
);
3723 /*********************************************************************
3724 * _fgetwchar (MSVCRT.@)
3726 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3728 return MSVCRT_fgetwc(MSVCRT_stdin
);
3731 /*********************************************************************
3732 * getwchar (MSVCRT.@)
3734 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3736 return MSVCRT__fgetwchar();
3739 /*********************************************************************
3742 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3744 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3745 MSVCRT_wchar_t
* buf_start
= s
;
3747 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3748 file
,file
->_file
,s
,size
);
3750 MSVCRT__lock_file(file
);
3752 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3757 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3759 TRACE(":nothing read\n");
3760 MSVCRT__unlock_file(file
);
3763 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3766 TRACE(":got %s\n", debugstr_w(buf_start
));
3767 MSVCRT__unlock_file(file
);
3771 /*********************************************************************
3772 * _flsbuf (MSVCRT.@)
3774 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3776 /* Flush output buffer */
3777 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3778 msvcrt_alloc_buffer(file
);
3781 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3782 if(!(file
->_flag
& MSVCRT__IORW
)) {
3783 file
->_flag
|= MSVCRT__IOERR
;
3786 file
->_flag
|= MSVCRT__IOWRT
;
3788 if(file
->_flag
& MSVCRT__IOREAD
) {
3789 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3790 file
->_flag
|= MSVCRT__IOERR
;
3794 file
->_ptr
= file
->_base
;
3795 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3798 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3801 if(file
->_cnt
<= 0) {
3802 res
= msvcrt_flush_buffer(file
);
3805 file
->_flag
|= MSVCRT__IOWRT
;
3806 file
->_cnt
=file
->_bufsiz
;
3814 /* set _cnt to 0 for unbuffered FILEs */
3816 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3819 file
->_flag
|= MSVCRT__IOERR
;
3824 /*********************************************************************
3827 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3831 MSVCRT__lock_file(file
);
3832 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3833 MSVCRT__unlock_file(file
);
3838 /*********************************************************************
3839 * _fwrite_nolock (MSVCRT.@)
3841 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3843 MSVCRT_size_t wrcnt
=size
* nmemb
;
3849 if(file
->_cnt
< 0) {
3850 WARN("negative file->_cnt value in %p\n", file
);
3851 file
->_flag
|= MSVCRT__IOERR
;
3853 } else if(file
->_cnt
) {
3854 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3855 memcpy(file
->_ptr
, ptr
, pcnt
);
3860 ptr
= (const char*)ptr
+ pcnt
;
3861 } else if((file
->_flag
& MSVCRT__IONBF
)
3862 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3863 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3867 if(file
->_flag
& MSVCRT__IONBF
)
3869 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3870 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3872 bufsiz
= file
->_bufsiz
;
3874 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3876 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3879 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3880 file
->_flag
|= MSVCRT__IOERR
;
3885 ptr
= (const char*)ptr
+ pcnt
;
3887 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3891 ptr
= (const char*)ptr
+ 1;
3895 return written
/ size
;
3898 /*********************************************************************
3901 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3905 MSVCRT__lock_file(file
);
3906 ret
= MSVCRT__fputwc_nolock(wc
, file
);
3907 MSVCRT__unlock_file(file
);
3912 /*********************************************************************
3913 * _fputwc_nolock (MSVCRT.@)
3915 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3917 MSVCRT_wchar_t mwc
=wc
;
3921 fdinfo
= get_ioinfo_nolock(file
->_file
);
3923 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3924 char buf
[MSVCRT_MB_LEN_MAX
];
3927 char_len
= MSVCRT_wctomb(buf
, mwc
);
3928 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
3932 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3941 /*********************************************************************
3942 * _fputwchar (MSVCRT.@)
3944 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3946 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3949 /*********************************************************************
3950 * _wfsopen (MSVCRT.@)
3952 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3955 int open_flags
, stream_flags
, fd
;
3957 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3959 /* map mode string to open() flags. "man fopen" for possibilities. */
3960 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3964 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3967 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3969 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3976 TRACE(":got (%p)\n",file
);
3977 if (fd
>= 0 && !file
)
3983 /*********************************************************************
3984 * _fsopen (MSVCRT.@)
3986 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3989 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3991 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3992 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3993 *MSVCRT__errno() = MSVCRT_EINVAL
;
3996 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3999 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4000 *MSVCRT__errno() = MSVCRT_EINVAL
;
4004 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4011 /*********************************************************************
4014 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4016 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4019 /*********************************************************************
4020 * fopen_s (MSVCRT.@)
4022 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4023 const char *filename
, const char *mode
)
4025 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4026 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4027 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4029 *pFile
= MSVCRT_fopen(filename
, mode
);
4032 return *MSVCRT__errno();
4036 /*********************************************************************
4037 * _wfopen (MSVCRT.@)
4039 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4041 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4044 /*********************************************************************
4045 * _wfopen_s (MSVCRT.@)
4047 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4048 const MSVCRT_wchar_t
*mode
)
4050 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4051 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4052 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4054 *pFile
= MSVCRT__wfopen(filename
, mode
);
4057 return *MSVCRT__errno();
4061 /*********************************************************************
4064 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4068 MSVCRT__lock_file(file
);
4069 ret
= MSVCRT__fputc_nolock(c
, file
);
4070 MSVCRT__unlock_file(file
);
4075 /*********************************************************************
4076 * _fputc_nolock (MSVCRT.@)
4078 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4087 res
= msvcrt_flush_buffer(file
);
4088 return res
? res
: c
;
4094 res
= MSVCRT__flsbuf(c
, file
);
4099 /*********************************************************************
4100 * _fputchar (MSVCRT.@)
4102 int CDECL
MSVCRT__fputchar(int c
)
4104 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4107 /*********************************************************************
4110 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4114 MSVCRT__lock_file(file
);
4115 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4116 MSVCRT__unlock_file(file
);
4121 /*********************************************************************
4122 * _fread_nolock (MSVCRT.@)
4124 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4126 MSVCRT_size_t rcnt
=size
* nmemb
;
4127 MSVCRT_size_t read
=0;
4128 MSVCRT_size_t pread
=0;
4133 /* first buffered data */
4135 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4136 memcpy(ptr
, file
->_ptr
, pcnt
);
4141 ptr
= (char*)ptr
+ pcnt
;
4142 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4143 if(file
->_flag
& MSVCRT__IORW
) {
4144 file
->_flag
|= MSVCRT__IOREAD
;
4150 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4151 msvcrt_alloc_buffer(file
);
4156 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4157 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4158 file
->_ptr
= file
->_base
;
4159 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4160 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4161 if (i
> 0 && i
< file
->_cnt
) {
4162 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4163 file
->_flag
&= ~MSVCRT__IOEOF
;
4166 memcpy(ptr
, file
->_ptr
, i
);
4170 } else if (rcnt
> INT_MAX
) {
4171 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4172 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4173 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4175 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4179 ptr
= (char *)ptr
+i
;
4180 /* expose feof condition in the flags
4181 * MFC tests file->_flag for feof, and doesn't call feof())
4183 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4184 file
->_flag
|= MSVCRT__IOEOF
;
4187 file
->_flag
|= MSVCRT__IOERR
;
4198 /*********************************************************************
4199 * fread_s (MSVCR80.@)
4201 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4202 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4206 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4208 memset(buf
, 0, buf_size
);
4211 if(!elem_size
|| !count
) return 0;
4213 MSVCRT__lock_file(stream
);
4214 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4215 MSVCRT__unlock_file(stream
);
4220 /*********************************************************************
4221 * _fread_nolock_s (MSVCR80.@)
4223 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4224 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4226 size_t bytes_left
, buf_pos
;
4228 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4230 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4232 memset(buf
, 0, buf_size
);
4235 if(!elem_size
|| !count
) return 0;
4236 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4237 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4239 bytes_left
= elem_size
*count
;
4242 if(stream
->_cnt
> 0) {
4243 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4245 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4246 memset(buf
, 0, buf_size
);
4250 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4254 int c
= MSVCRT__filbuf(stream
);
4259 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4260 memset(buf
, 0, buf_size
);
4264 ((char*)buf
)[buf_pos
++] = c
;
4269 return buf_pos
/elem_size
;
4272 /*********************************************************************
4273 * _wfreopen (MSVCRT.@)
4276 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4278 int open_flags
, stream_flags
, fd
;
4280 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4283 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4287 MSVCRT_fclose(file
);
4288 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4290 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4292 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4302 /*********************************************************************
4303 * _wfreopen_s (MSVCRT.@)
4305 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4306 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4308 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4309 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4310 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4311 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4313 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4316 return *MSVCRT__errno();
4320 /*********************************************************************
4321 * freopen (MSVCRT.@)
4324 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4327 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4329 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4330 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4336 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4343 /*********************************************************************
4344 * freopen_s (MSVCRT.@)
4346 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4347 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4349 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4350 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4351 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4352 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4354 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4357 return *MSVCRT__errno();
4361 /*********************************************************************
4362 * fsetpos (MSVCRT.@)
4364 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4368 MSVCRT__lock_file(file
);
4369 msvcrt_flush_buffer(file
);
4371 /* Reset direction of i/o */
4372 if(file
->_flag
& MSVCRT__IORW
) {
4373 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4376 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4377 MSVCRT__unlock_file(file
);
4381 /*********************************************************************
4382 * _ftelli64 (MSVCRT.@)
4384 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4388 MSVCRT__lock_file(file
);
4389 ret
= MSVCRT__ftelli64_nolock(file
);
4390 MSVCRT__unlock_file(file
);
4395 /*********************************************************************
4396 * _ftelli64_nolock (MSVCRT.@)
4398 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4402 pos
= _telli64(file
->_file
);
4405 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4406 if(file
->_flag
& MSVCRT__IOWRT
) {
4407 pos
+= file
->_ptr
- file
->_base
;
4409 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4412 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4416 } else if(!file
->_cnt
) { /* nothing to do */
4417 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4421 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4422 for(i
=0; i
<file
->_cnt
; i
++)
4423 if(file
->_ptr
[i
] == '\n')
4429 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4432 pos
-= file
->_bufsiz
;
4433 pos
+= file
->_ptr
- file
->_base
;
4435 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4436 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4439 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4449 /*********************************************************************
4452 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4454 return MSVCRT__ftelli64(file
);
4457 /*********************************************************************
4458 * _ftell_nolock (MSVCRT.@)
4460 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4462 return MSVCRT__ftelli64_nolock(file
);
4465 /*********************************************************************
4466 * fgetpos (MSVCRT.@)
4468 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4470 *pos
= MSVCRT__ftelli64(file
);
4476 /*********************************************************************
4479 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4481 MSVCRT_size_t len
= strlen(s
);
4484 MSVCRT__lock_file(file
);
4485 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4486 MSVCRT__unlock_file(file
);
4490 /*********************************************************************
4493 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4495 MSVCRT_size_t i
, len
= strlenW(s
);
4499 MSVCRT__lock_file(file
);
4500 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4501 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4502 MSVCRT__unlock_file(file
);
4506 tmp_buf
= add_std_buffer(file
);
4507 for (i
=0; i
<len
; i
++) {
4508 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4509 if(tmp_buf
) remove_std_buffer(file
);
4510 MSVCRT__unlock_file(file
);
4515 if(tmp_buf
) remove_std_buffer(file
);
4516 MSVCRT__unlock_file(file
);
4520 /*********************************************************************
4521 * getchar (MSVCRT.@)
4523 int CDECL
MSVCRT_getchar(void)
4525 return MSVCRT_fgetc(MSVCRT_stdin
);
4528 /*********************************************************************
4531 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4533 return MSVCRT_fgetc(file
);
4536 /*********************************************************************
4539 char * CDECL
MSVCRT_gets(char *buf
)
4542 char * buf_start
= buf
;
4544 MSVCRT__lock_file(MSVCRT_stdin
);
4545 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4546 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4547 if(cc
!= '\r') *buf
++ = (char)cc
;
4551 TRACE("got '%s'\n", buf_start
);
4552 MSVCRT__unlock_file(MSVCRT_stdin
);
4556 /*********************************************************************
4559 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4562 MSVCRT_wchar_t
* ws
= buf
;
4564 MSVCRT__lock_file(MSVCRT_stdin
);
4565 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4566 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4569 *buf
++ = (MSVCRT_wchar_t
)cc
;
4573 TRACE("got %s\n", debugstr_w(ws
));
4574 MSVCRT__unlock_file(MSVCRT_stdin
);
4578 /*********************************************************************
4581 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4583 return MSVCRT_fputc(c
, file
);
4586 /*********************************************************************
4587 * putchar (MSVCRT.@)
4589 int CDECL
MSVCRT_putchar(int c
)
4591 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4594 /*********************************************************************
4595 * _putwch (MSVCRT.@)
4597 int CDECL
MSVCRT__putwch(int c
)
4599 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4602 /*********************************************************************
4605 int CDECL
MSVCRT_puts(const char *s
)
4607 MSVCRT_size_t len
= strlen(s
);
4610 MSVCRT__lock_file(MSVCRT_stdout
);
4611 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4612 MSVCRT__unlock_file(MSVCRT_stdout
);
4616 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4617 MSVCRT__unlock_file(MSVCRT_stdout
);
4621 /*********************************************************************
4624 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4626 static const MSVCRT_wchar_t nl
= '\n';
4627 MSVCRT_size_t len
= strlenW(s
);
4630 MSVCRT__lock_file(MSVCRT_stdout
);
4631 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4632 MSVCRT__unlock_file(MSVCRT_stdout
);
4636 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4637 MSVCRT__unlock_file(MSVCRT_stdout
);
4641 /*********************************************************************
4644 int CDECL
MSVCRT_remove(const char *path
)
4646 TRACE("(%s)\n",path
);
4647 if (DeleteFileA(path
))
4649 TRACE(":failed (%d)\n",GetLastError());
4650 msvcrt_set_errno(GetLastError());
4654 /*********************************************************************
4655 * _wremove (MSVCRT.@)
4657 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4659 TRACE("(%s)\n",debugstr_w(path
));
4660 if (DeleteFileW(path
))
4662 TRACE(":failed (%d)\n",GetLastError());
4663 msvcrt_set_errno(GetLastError());
4667 /*********************************************************************
4670 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4672 TRACE(":from %s to %s\n",oldpath
,newpath
);
4673 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4675 TRACE(":failed (%d)\n",GetLastError());
4676 msvcrt_set_errno(GetLastError());
4680 /*********************************************************************
4681 * _wrename (MSVCRT.@)
4683 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4685 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4686 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4688 TRACE(":failed (%d)\n",GetLastError());
4689 msvcrt_set_errno(GetLastError());
4693 /*********************************************************************
4694 * setvbuf (MSVCRT.@)
4696 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4698 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4699 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4700 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4702 MSVCRT__lock_file(file
);
4704 MSVCRT__fflush_nolock(file
);
4705 if(file
->_flag
& MSVCRT__IOMYBUF
)
4706 MSVCRT_free(file
->_base
);
4707 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4710 if(mode
== MSVCRT__IONBF
) {
4711 file
->_flag
|= MSVCRT__IONBF
;
4712 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4715 file
->_base
= file
->_ptr
= buf
;
4716 file
->_flag
|= MSVCRT__USERBUF
;
4717 file
->_bufsiz
= size
;
4719 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4722 MSVCRT__unlock_file(file
);
4726 file
->_flag
|= MSVCRT__IOMYBUF
;
4727 file
->_bufsiz
= size
;
4729 MSVCRT__unlock_file(file
);
4733 /*********************************************************************
4736 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4738 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4741 /*********************************************************************
4744 char * CDECL
MSVCRT_tmpnam(char *s
)
4751 thread_data_t
*data
= msvcrt_get_thread_data();
4753 if(!data
->tmpnam_buffer
)
4754 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4756 s
= data
->tmpnam_buffer
;
4759 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4760 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4761 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4763 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4764 memcpy(p
, tmpstr
, size
);
4766 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4767 GetLastError() == ERROR_FILE_NOT_FOUND
)
4773 /*********************************************************************
4774 * _wtmpnam (MSVCRT.@)
4776 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4778 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4779 MSVCRT_wchar_t tmpstr
[16];
4783 thread_data_t
*data
= msvcrt_get_thread_data();
4785 if(!data
->wtmpnam_buffer
)
4786 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4788 s
= data
->wtmpnam_buffer
;
4791 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4792 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4793 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4795 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4796 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4798 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4799 GetLastError() == ERROR_FILE_NOT_FOUND
)
4805 /*********************************************************************
4806 * tmpfile (MSVCRT.@)
4808 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4810 char *filename
= MSVCRT_tmpnam(NULL
);
4812 MSVCRT_FILE
* file
= NULL
;
4815 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4816 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4817 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4819 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4824 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4827 if(fd
!= -1 && !file
)
4833 /*********************************************************************
4834 * tmpfile_s (MSVCRT.@)
4836 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4838 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4840 *file
= MSVCRT_tmpfile();
4844 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4846 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4849 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4853 MSVCRT__lock_file(file
);
4855 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4856 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4857 MSVCRT__unlock_file(file
);
4861 for(i
=0; i
<len
; i
++) {
4862 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
4863 MSVCRT__unlock_file(file
);
4868 MSVCRT__unlock_file(file
);
4872 /*********************************************************************
4873 * vfprintf (MSVCRT.@)
4875 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4880 MSVCRT__lock_file(file
);
4881 tmp_buf
= add_std_buffer(file
);
4882 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4883 if(tmp_buf
) remove_std_buffer(file
);
4884 MSVCRT__unlock_file(file
);
4889 /*********************************************************************
4890 * vfprintf_s (MSVCRT.@)
4892 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4897 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4899 MSVCRT__lock_file(file
);
4900 tmp_buf
= add_std_buffer(file
);
4901 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4902 if(tmp_buf
) remove_std_buffer(file
);
4903 MSVCRT__unlock_file(file
);
4908 /*********************************************************************
4909 * vfwprintf (MSVCRT.@)
4911 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4916 MSVCRT__lock_file(file
);
4917 tmp_buf
= add_std_buffer(file
);
4918 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4919 if(tmp_buf
) remove_std_buffer(file
);
4920 MSVCRT__unlock_file(file
);
4925 /*********************************************************************
4926 * vfwprintf_s (MSVCRT.@)
4928 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4933 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4935 MSVCRT__lock_file(file
);
4936 tmp_buf
= add_std_buffer(file
);
4937 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4938 if(tmp_buf
) remove_std_buffer(file
);
4939 MSVCRT__unlock_file(file
);
4944 /*********************************************************************
4945 * _vfwprintf_l (MSVCRT.@)
4947 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4948 MSVCRT__locale_t locale
, __ms_va_list valist
)
4953 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4955 MSVCRT__lock_file(file
);
4956 tmp_buf
= add_std_buffer(file
);
4957 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4958 if(tmp_buf
) remove_std_buffer(file
);
4959 MSVCRT__unlock_file(file
);
4964 /*********************************************************************
4965 * vprintf (MSVCRT.@)
4967 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4969 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4972 /*********************************************************************
4973 * vprintf_s (MSVCRT.@)
4975 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4977 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4980 /*********************************************************************
4981 * vwprintf (MSVCRT.@)
4983 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4985 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4988 /*********************************************************************
4989 * vwprintf_s (MSVCRT.@)
4991 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4993 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4996 /*********************************************************************
4997 * fprintf (MSVCRT.@)
4999 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5001 __ms_va_list valist
;
5003 __ms_va_start(valist
, format
);
5004 res
= MSVCRT_vfprintf(file
, format
, valist
);
5005 __ms_va_end(valist
);
5009 /*********************************************************************
5010 * fprintf_s (MSVCRT.@)
5012 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5014 __ms_va_list valist
;
5016 __ms_va_start(valist
, format
);
5017 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5018 __ms_va_end(valist
);
5022 /*********************************************************************
5023 * fwprintf (MSVCRT.@)
5025 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5027 __ms_va_list valist
;
5029 __ms_va_start(valist
, format
);
5030 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5031 __ms_va_end(valist
);
5035 /*********************************************************************
5036 * fwprintf_s (MSVCRT.@)
5038 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5040 __ms_va_list valist
;
5042 __ms_va_start(valist
, format
);
5043 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5044 __ms_va_end(valist
);
5048 /*********************************************************************
5049 * _fwprintf_l (MSVCRT.@)
5051 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5053 __ms_va_list valist
;
5055 __ms_va_start(valist
, locale
);
5056 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5057 __ms_va_end(valist
);
5061 /*********************************************************************
5064 int CDECL
MSVCRT_printf(const char *format
, ...)
5066 __ms_va_list valist
;
5068 __ms_va_start(valist
, format
);
5069 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5070 __ms_va_end(valist
);
5074 /*********************************************************************
5075 * printf_s (MSVCRT.@)
5077 int CDECL
MSVCRT_printf_s(const char *format
, ...)
5079 __ms_va_list valist
;
5081 __ms_va_start(valist
, format
);
5082 res
= MSVCRT_vprintf_s(format
, valist
);
5083 __ms_va_end(valist
);
5087 /*********************************************************************
5090 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5094 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5096 MSVCRT__lock_file(file
);
5097 ret
= MSVCRT__ungetc_nolock(c
, file
);
5098 MSVCRT__unlock_file(file
);
5103 /*********************************************************************
5104 * _ungetc_nolock (MSVCRT.@)
5106 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5108 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5110 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5111 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5114 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5115 && msvcrt_alloc_buffer(file
))
5116 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5119 if(file
->_ptr
>file
->_base
) {
5121 if(file
->_flag
& MSVCRT__IOSTRG
) {
5122 if(*file
->_ptr
!= c
) {
5130 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5131 file
->_flag
|= MSVCRT__IOREAD
;
5138 /*********************************************************************
5139 * ungetwc (MSVCRT.@)
5141 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5145 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5147 MSVCRT__lock_file(file
);
5148 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5149 MSVCRT__unlock_file(file
);
5154 /*********************************************************************
5155 * _ungetwc_nolock (MSVCRT.@)
5157 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5159 MSVCRT_wchar_t mwc
= wc
;
5161 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5162 if (wc
== MSVCRT_WEOF
)
5165 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5166 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5167 unsigned char * pp
= (unsigned char *)&mwc
;
5170 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5171 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5175 char mbs
[MSVCRT_MB_LEN_MAX
];
5178 len
= MSVCRT_wctomb(mbs
, mwc
);
5182 for(len
--; len
>=0; len
--) {
5183 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5191 /*********************************************************************
5192 * wprintf (MSVCRT.@)
5194 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5196 __ms_va_list valist
;
5198 __ms_va_start(valist
, format
);
5199 res
= MSVCRT_vwprintf(format
, valist
);
5200 __ms_va_end(valist
);
5204 /*********************************************************************
5205 * wprintf_s (MSVCRT.@)
5207 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5209 __ms_va_list valist
;
5211 __ms_va_start(valist
, format
);
5212 res
= MSVCRT_vwprintf_s(format
, valist
);
5213 __ms_va_end(valist
);
5217 /*********************************************************************
5218 * _getmaxstdio (MSVCRT.@)
5220 int CDECL
MSVCRT__getmaxstdio(void)
5222 return MSVCRT_max_streams
;
5225 /*********************************************************************
5226 * _setmaxstdio (MSVCRT.@)
5228 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5230 TRACE("%d\n", newmax
);
5232 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5235 MSVCRT_max_streams
= newmax
;
5236 return MSVCRT_max_streams
;