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
* msvcrt_get_ioinfo(int fd
)
252 if(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 MSVCRT_FILE
* msvcrt_get_file(int i
)
264 if(i
>= MSVCRT_max_streams
)
268 return &MSVCRT__iob
[i
];
270 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
272 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
273 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
274 ERR("out of memory\n");
275 *MSVCRT__errno() = MSVCRT_ENOMEM
;
279 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
281 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
286 static inline BOOL
msvcrt_is_valid_fd(int fd
)
288 return fd
>= 0 && fd
< MSVCRT_fdend
&& (msvcrt_get_ioinfo(fd
)->wxflag
& WX_OPEN
);
291 /* INTERNAL: Get the HANDLE for a fd
292 * This doesn't lock the table, because a failure will result in
293 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
294 * it returns a valid handle which is about to be closed, a subsequent call
295 * will fail, most likely in a sane way.
297 static HANDLE
msvcrt_fdtoh(int fd
)
299 if (!msvcrt_is_valid_fd(fd
))
301 WARN(":fd (%d) - no handle!\n",fd
);
302 *MSVCRT___doserrno() = 0;
303 *MSVCRT__errno() = MSVCRT_EBADF
;
304 return INVALID_HANDLE_VALUE
;
306 if (msvcrt_get_ioinfo(fd
)->handle
== INVALID_HANDLE_VALUE
)
307 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd
);
308 return msvcrt_get_ioinfo(fd
)->handle
;
311 /* INTERNAL: free a file entry fd */
312 static void msvcrt_free_fd(int fd
)
317 fdinfo
= msvcrt_get_ioinfo(fd
);
318 if(fdinfo
!= &MSVCRT___badioinfo
)
320 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
323 TRACE(":fd (%d) freed\n",fd
);
330 SetStdHandle(STD_INPUT_HANDLE
, 0);
333 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
336 SetStdHandle(STD_ERROR_HANDLE
, 0);
341 if (fd
== MSVCRT_fdend
- 1)
343 if (fd
< MSVCRT_fdstart
)
348 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
349 /* caller must hold the files lock */
350 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
354 if (fd
>= MSVCRT_MAX_FILES
)
356 WARN(":files exhausted!\n");
357 *MSVCRT__errno() = MSVCRT_ENFILE
;
361 fdinfo
= msvcrt_get_ioinfo(fd
);
362 if(fdinfo
== &MSVCRT___badioinfo
) {
365 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
366 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
367 WARN(":out of memory!\n");
368 *MSVCRT__errno() = MSVCRT_ENOMEM
;
372 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
373 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
375 fdinfo
= msvcrt_get_ioinfo(fd
);
378 fdinfo
->handle
= hand
;
379 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
380 fdinfo
->lookahead
[0] = '\n';
381 fdinfo
->lookahead
[1] = '\n';
382 fdinfo
->lookahead
[2] = '\n';
385 /* locate next free slot */
386 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
387 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
389 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
390 msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
392 /* update last fd in use */
393 if (fd
>= MSVCRT_fdend
)
394 MSVCRT_fdend
= fd
+ 1;
395 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
399 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
400 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
401 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
407 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
408 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
413 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
414 ret
= msvcrt_set_fd(hand
, flag
, MSVCRT_fdstart
);
419 /* INTERNAL: Allocate a FILE* for an fd slot */
420 /* caller must hold the files lock */
421 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
426 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
428 file
= msvcrt_get_file(i
);
432 if (file
->_flag
== 0)
434 if (i
== MSVCRT_stream_idx
)
436 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
438 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
439 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
450 /* INTERNAL: initialize a FILE* from an open fd */
451 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
453 TRACE(":fd (%d) allocating FILE*\n",fd
);
454 if (!msvcrt_is_valid_fd(fd
))
456 WARN(":invalid fd %d\n",fd
);
457 *MSVCRT___doserrno() = 0;
458 *MSVCRT__errno() = MSVCRT_EBADF
;
461 file
->_ptr
= file
->_base
= NULL
;
464 file
->_flag
= stream_flags
;
465 file
->_tmpfname
= NULL
;
467 TRACE(":got FILE* (%p)\n",file
);
471 /* INTERNAL: Create an inheritance data block (for spawned process)
472 * The inheritance block is made of:
473 * 00 int nb of file descriptor (NBFD)
474 * 04 char file flags (wxflag): repeated for each fd
475 * 4+NBFD HANDLE file handle: repeated for each fd
477 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
484 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
485 *block
= MSVCRT_calloc(*size
, 1);
491 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
492 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
494 *(unsigned*)*block
= MSVCRT_fdend
;
495 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
497 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
498 fdinfo
= msvcrt_get_ioinfo(fd
);
499 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
501 *wxflag_ptr
= fdinfo
->wxflag
;
502 *handle_ptr
= fdinfo
->handle
;
507 *handle_ptr
= INVALID_HANDLE_VALUE
;
509 wxflag_ptr
++; handle_ptr
++;
514 /* INTERNAL: Set up all file descriptors,
515 * as well as default streams (stdin, stderr and stdout)
517 void msvcrt_init_io(void)
523 GetStartupInfoA(&si
);
524 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
530 count
= *(unsigned*)si
.lpReserved2
;
531 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
532 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
534 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
535 count
= min(count
, MSVCRT_MAX_FILES
);
536 for (i
= 0; i
< count
; i
++)
538 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
539 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
541 wxflag_ptr
++; handle_ptr
++;
543 MSVCRT_fdend
= max( 3, count
);
544 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
545 if (msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
548 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO
);
549 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
550 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
551 DWORD type
= GetFileType(h
);
553 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
554 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDIN_FILENO
);
557 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO
);
558 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
559 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
560 DWORD type
= GetFileType(h
);
562 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
563 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDOUT_FILENO
);
566 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO
);
567 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
568 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
569 DWORD type
= GetFileType(h
);
571 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
572 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDERR_FILENO
);
575 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO
)->handle
,
576 msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO
)->handle
,
577 msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO
)->handle
);
579 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
580 for (i
= 0; i
< 3; i
++)
582 /* FILE structs for stdin/out/err are static and never deleted */
583 MSVCRT__iob
[i
]._file
= i
;
584 MSVCRT__iob
[i
]._tmpfname
= NULL
;
585 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
587 MSVCRT_stream_idx
= 3;
590 /* INTERNAL: Flush stdio file buffer */
591 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
593 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
594 int cnt
=file
->_ptr
-file
->_base
;
595 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
596 file
->_flag
|= MSVCRT__IOERR
;
599 file
->_ptr
=file
->_base
;
605 /*********************************************************************
608 int CDECL
MSVCRT__isatty(int fd
)
610 TRACE(":fd (%d)\n",fd
);
612 return msvcrt_get_ioinfo(fd
)->wxflag
& WX_TTY
;
615 /* INTERNAL: Allocate stdio file buffer */
616 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
618 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
619 && MSVCRT__isatty(file
->_file
))
622 file
->_base
= MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
624 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
625 file
->_flag
|= MSVCRT__IOMYBUF
;
627 file
->_base
= (char*)(&file
->_charbuf
);
629 file
->_flag
|= MSVCRT__IONBF
;
631 file
->_ptr
= file
->_base
;
636 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
637 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
639 static char buffers
[2][MSVCRT_BUFSIZ
];
641 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
642 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
643 || !MSVCRT__isatty(file
->_file
))
646 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
647 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
648 file
->_flag
|= MSVCRT__USERBUF
;
652 /* INTERNAL: Removes temporary buffer from stdout or stderr */
653 /* Only call this function when add_std_buffer returned TRUE */
654 static void remove_std_buffer(MSVCRT_FILE
*file
)
656 msvcrt_flush_buffer(file
);
657 file
->_ptr
= file
->_base
= NULL
;
658 file
->_bufsiz
= file
->_cnt
= 0;
659 file
->_flag
&= ~MSVCRT__USERBUF
;
662 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
663 static int msvcrt_int_to_base32(int num
, char *str
)
678 *p
= (num
& 31) + '0';
680 *p
+= ('a' - '0' - 10);
687 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
688 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
703 *p
= (num
& 31) + '0';
705 *p
+= ('a' - '0' - 10);
712 /*********************************************************************
713 * __iob_func(MSVCRT.@)
715 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
717 return &MSVCRT__iob
[0];
720 /*********************************************************************
723 int CDECL
MSVCRT__access(const char *filename
, int mode
)
725 DWORD attr
= GetFileAttributesA(filename
);
727 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
729 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
731 msvcrt_set_errno(GetLastError());
734 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
736 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
742 /*********************************************************************
743 * _access_s (MSVCRT.@)
745 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
747 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
748 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
750 if (MSVCRT__access(filename
, mode
) == -1)
751 return *MSVCRT__errno();
755 /*********************************************************************
756 * _waccess (MSVCRT.@)
758 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
760 DWORD attr
= GetFileAttributesW(filename
);
762 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
764 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
766 msvcrt_set_errno(GetLastError());
769 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
771 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
777 /*********************************************************************
778 * _waccess_s (MSVCRT.@)
780 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
782 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
783 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
785 if (MSVCRT__waccess(filename
, mode
) == -1)
786 return *MSVCRT__errno();
790 /*********************************************************************
793 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
795 DWORD oldFlags
= GetFileAttributesA(path
);
797 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
799 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
800 oldFlags
| FILE_ATTRIBUTE_READONLY
;
802 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
805 msvcrt_set_errno(GetLastError());
809 /*********************************************************************
812 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
814 DWORD oldFlags
= GetFileAttributesW(path
);
816 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
818 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
819 oldFlags
| FILE_ATTRIBUTE_READONLY
;
821 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
824 msvcrt_set_errno(GetLastError());
828 /*********************************************************************
831 int CDECL
MSVCRT__unlink(const char *path
)
833 TRACE("%s\n",debugstr_a(path
));
834 if(DeleteFileA(path
))
836 TRACE("failed (%d)\n",GetLastError());
837 msvcrt_set_errno(GetLastError());
841 /*********************************************************************
842 * _wunlink (MSVCRT.@)
844 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
846 TRACE("(%s)\n",debugstr_w(path
));
847 if(DeleteFileW(path
))
849 TRACE("failed (%d)\n",GetLastError());
850 msvcrt_set_errno(GetLastError());
854 /*********************************************************************
857 int CDECL
MSVCRT__commit(int fd
)
859 HANDLE hand
= msvcrt_fdtoh(fd
);
861 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
862 if (hand
== INVALID_HANDLE_VALUE
)
865 if (!FlushFileBuffers(hand
))
867 if (GetLastError() == ERROR_INVALID_HANDLE
)
869 /* FlushFileBuffers fails for console handles
870 * so we ignore this error.
874 TRACE(":failed-last error (%d)\n",GetLastError());
875 msvcrt_set_errno(GetLastError());
882 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
883 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
885 /* INTERNAL: Flush all stream buffer */
886 static int msvcrt_flush_all_buffers(int mask
)
888 int i
, num_flushed
= 0;
892 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
893 file
= msvcrt_get_file(i
);
897 if(file
->_flag
& mask
) {
905 TRACE(":flushed (%d) handles\n",num_flushed
);
909 /*********************************************************************
910 * _flushall (MSVCRT.@)
912 int CDECL
MSVCRT__flushall(void)
914 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
917 /*********************************************************************
920 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
923 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
924 } else if(file
->_flag
& MSVCRT__IOWRT
) {
927 MSVCRT__lock_file(file
);
928 res
= msvcrt_flush_buffer(file
);
930 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
931 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
932 MSVCRT__unlock_file(file
);
935 } else if(file
->_flag
& MSVCRT__IOREAD
) {
936 MSVCRT__lock_file(file
);
938 file
->_ptr
= file
->_base
;
939 MSVCRT__unlock_file(file
);
946 /*********************************************************************
949 int CDECL
MSVCRT__close(int fd
)
955 hand
= msvcrt_fdtoh(fd
);
956 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
957 if (!msvcrt_is_valid_fd(fd
)) {
961 ret
= CloseHandle(hand
) ? 0 : -1;
963 WARN(":failed-last error (%d)\n",GetLastError());
964 msvcrt_set_errno(GetLastError());
972 /*********************************************************************
975 * MSDN isn't clear on this point, but the remarks for _pipe
976 * indicate file descriptors duplicated with _dup and _dup2 are always
979 int CDECL
MSVCRT__dup2(int od
, int nd
)
983 TRACE("(od=%d, nd=%d)\n", od
, nd
);
985 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
989 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od
)->handle
,
990 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
992 int wxflag
= msvcrt_get_ioinfo(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
994 if (msvcrt_is_valid_fd(nd
))
996 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
1000 *MSVCRT__errno() = MSVCRT_EMFILE
;
1004 /* _dup2 returns 0, not nd, on success */
1011 msvcrt_set_errno(GetLastError());
1016 *MSVCRT__errno() = MSVCRT_EBADF
;
1023 /*********************************************************************
1026 int CDECL
MSVCRT__dup(int od
)
1031 fd
= MSVCRT_fdstart
;
1032 if (MSVCRT__dup2(od
, fd
) == 0)
1040 /*********************************************************************
1043 int CDECL
MSVCRT__eof(int fd
)
1045 DWORD curpos
,endpos
;
1046 LONG hcurpos
,hendpos
;
1047 HANDLE hand
= msvcrt_fdtoh(fd
);
1049 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1051 if (hand
== INVALID_HANDLE_VALUE
)
1054 if (msvcrt_get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1056 /* Otherwise we do it the hard way */
1057 hcurpos
= hendpos
= 0;
1058 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1059 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1061 if (curpos
== endpos
&& hcurpos
== hendpos
)
1063 /* FIXME: shouldn't WX_ATEOF be set here? */
1067 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1071 /*********************************************************************
1072 * _fcloseall (MSVCRT.@)
1074 int CDECL
MSVCRT__fcloseall(void)
1076 int num_closed
= 0, i
;
1080 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1081 file
= msvcrt_get_file(i
);
1083 if (file
->_flag
&& !MSVCRT_fclose(file
))
1088 TRACE(":closed (%d) handles\n",num_closed
);
1092 /* free everything on process exit */
1093 void msvcrt_free_io(void)
1099 MSVCRT__fcloseall();
1101 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1102 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1104 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1106 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1107 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1109 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1110 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1114 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1115 MSVCRT_free(MSVCRT_fstream
[i
]);
1117 DeleteCriticalSection(&MSVCRT_file_cs
);
1120 /*********************************************************************
1121 * _lseeki64 (MSVCRT.@)
1123 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1125 HANDLE hand
= msvcrt_fdtoh(fd
);
1128 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1129 if (hand
== INVALID_HANDLE_VALUE
)
1132 if (whence
< 0 || whence
> 2)
1134 *MSVCRT__errno() = MSVCRT_EINVAL
;
1138 TRACE(":fd (%d) to %s pos %s\n",
1139 fd
,wine_dbgstr_longlong(offset
),
1140 (whence
==SEEK_SET
)?"SEEK_SET":
1141 (whence
==SEEK_CUR
)?"SEEK_CUR":
1142 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1144 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1145 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1146 ofs
.QuadPart
= offset
;
1147 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1148 GetLastError() == ERROR_SUCCESS
)
1150 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_ATEOF
;
1151 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1153 return ofs
.QuadPart
;
1155 TRACE(":error-last error (%d)\n",GetLastError());
1156 msvcrt_set_errno(GetLastError());
1160 /*********************************************************************
1163 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1165 return MSVCRT__lseeki64(fd
, offset
, whence
);
1168 /*********************************************************************
1169 * _lock_file (MSVCRT.@)
1171 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1173 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1174 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1176 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1179 /*********************************************************************
1180 * _unlock_file (MSVCRT.@)
1182 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1184 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1185 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1187 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1190 /*********************************************************************
1191 * _locking (MSVCRT.@)
1193 * This is untested; the underlying LockFile doesn't work yet.
1195 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1199 HANDLE hand
= msvcrt_fdtoh(fd
);
1201 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1202 if (hand
== INVALID_HANDLE_VALUE
)
1205 if (mode
< 0 || mode
> 4)
1207 *MSVCRT__errno() = MSVCRT_EINVAL
;
1211 TRACE(":fd (%d) by 0x%08x mode %s\n",
1212 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1213 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1214 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1215 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1216 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1219 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1221 FIXME ("Seek failed\n");
1222 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1225 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1228 ret
= 1; /* just to satisfy gcc */
1231 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1236 else if (mode
== MSVCRT__LK_UNLCK
)
1237 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1239 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1240 /* FIXME - what about error settings? */
1241 return ret
? 0 : -1;
1244 /*********************************************************************
1245 * _fseeki64 (MSVCRT.@)
1247 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1251 MSVCRT__lock_file(file
);
1252 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1253 MSVCRT__unlock_file(file
);
1258 /*********************************************************************
1259 * _fseeki64_nolock (MSVCRT.@)
1261 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1265 /* Flush output if needed */
1266 if(file
->_flag
& MSVCRT__IOWRT
)
1267 msvcrt_flush_buffer(file
);
1269 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1271 offset
+= MSVCRT__ftelli64_nolock(file
);
1274 /* Discard buffered input */
1276 file
->_ptr
= file
->_base
;
1277 /* Reset direction of i/o */
1278 if(file
->_flag
& MSVCRT__IORW
) {
1279 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1281 /* Clear end of file flag */
1282 file
->_flag
&= ~MSVCRT__IOEOF
;
1283 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1288 /*********************************************************************
1291 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1293 return MSVCRT__fseeki64( file
, offset
, whence
);
1296 /*********************************************************************
1297 * _fseek_nolock (MSVCRT.@)
1299 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1301 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1304 /*********************************************************************
1305 * _chsize_s (MSVCRT.@)
1307 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1313 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1315 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1319 handle
= msvcrt_fdtoh(fd
);
1320 if (handle
!= INVALID_HANDLE_VALUE
)
1322 /* save the current file pointer */
1323 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1326 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1329 ret
= SetEndOfFile(handle
);
1330 if (!ret
) msvcrt_set_errno(GetLastError());
1333 /* restore the file pointer */
1334 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1339 return ret
? 0 : *MSVCRT__errno();
1342 /*********************************************************************
1343 * _chsize (MSVCRT.@)
1345 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1347 /* _chsize_s returns errno on failure but _chsize should return -1 */
1348 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1351 /*********************************************************************
1352 * clearerr (MSVCRT.@)
1354 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1356 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1358 MSVCRT__lock_file(file
);
1359 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1360 MSVCRT__unlock_file(file
);
1363 /*********************************************************************
1366 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1368 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1370 MSVCRT__lock_file(file
);
1371 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1372 MSVCRT_clearerr(file
);
1373 MSVCRT__unlock_file(file
);
1376 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1378 int plus
= strchrW(mode
, '+') != NULL
;
1380 TRACE("%s\n", debugstr_w(mode
));
1382 while(*mode
== ' ') mode
++;
1387 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1388 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1391 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1392 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1395 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1396 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1399 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1403 *stream_flags
|= MSVCRT__commode
;
1405 while (*mode
&& *mode
!=',')
1409 *open_flags
|= MSVCRT__O_BINARY
;
1410 *open_flags
&= ~MSVCRT__O_TEXT
;
1413 *open_flags
|= MSVCRT__O_TEXT
;
1414 *open_flags
&= ~MSVCRT__O_BINARY
;
1417 *open_flags
|= MSVCRT__O_TEMPORARY
;
1420 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1423 *stream_flags
|= MSVCRT__IOCOMMIT
;
1426 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1429 *open_flags
|= MSVCRT__O_NOINHERIT
;
1438 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1441 ERR("incorrect mode flag: %c\n", mode
[-1]);
1447 static const WCHAR ccs
[] = {'c','c','s'};
1448 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1449 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1450 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1453 while(*mode
== ' ') mode
++;
1454 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1456 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1457 while(*mode
== ' ') mode
++;
1458 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1461 while(*mode
== ' ') mode
++;
1463 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1465 *open_flags
|= MSVCRT__O_U8TEXT
;
1466 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1468 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1470 *open_flags
|= MSVCRT__O_U16TEXT
;
1471 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1473 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1475 *open_flags
|= MSVCRT__O_WTEXT
;
1476 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1480 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1484 while(*mode
== ' ') mode
++;
1487 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1492 /*********************************************************************
1493 * _fdopen (MSVCRT.@)
1495 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1498 MSVCRT_wchar_t
*modeW
= NULL
;
1500 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1502 ret
= MSVCRT__wfdopen(fd
, modeW
);
1508 /*********************************************************************
1509 * _wfdopen (MSVCRT.@)
1511 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1513 int open_flags
, stream_flags
;
1516 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1519 if (!(file
= msvcrt_alloc_fp()))
1521 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1526 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1532 /*********************************************************************
1533 * _filelength (MSVCRT.@)
1535 LONG CDECL
MSVCRT__filelength(int fd
)
1537 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1540 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1543 if (endPos
!= curPos
)
1544 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1551 /*********************************************************************
1552 * _filelengthi64 (MSVCRT.@)
1554 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1556 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1559 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1562 if (endPos
!= curPos
)
1563 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1570 /*********************************************************************
1571 * _fileno (MSVCRT.@)
1573 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1575 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1579 /*********************************************************************
1580 * _fstat64 (MSVCRT.@)
1582 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1586 BY_HANDLE_FILE_INFORMATION hfi
;
1587 HANDLE hand
= msvcrt_fdtoh(fd
);
1589 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1590 if (hand
== INVALID_HANDLE_VALUE
)
1595 WARN(":failed-NULL buf\n");
1596 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1600 memset(&hfi
, 0, sizeof(hfi
));
1601 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1602 type
= GetFileType(hand
);
1603 if (type
== FILE_TYPE_PIPE
)
1605 buf
->st_dev
= buf
->st_rdev
= fd
;
1606 buf
->st_mode
= S_IFIFO
;
1609 else if (type
== FILE_TYPE_CHAR
)
1611 buf
->st_dev
= buf
->st_rdev
= fd
;
1612 buf
->st_mode
= S_IFCHR
;
1615 else /* FILE_TYPE_DISK etc. */
1617 if (!GetFileInformationByHandle(hand
, &hfi
))
1619 WARN(":failed-last error (%d)\n",GetLastError());
1620 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1623 buf
->st_mode
= S_IFREG
| 0444;
1624 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1625 buf
->st_mode
|= 0222;
1626 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1627 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1629 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1630 buf
->st_mtime
= buf
->st_ctime
= dw
;
1631 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1633 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1638 /*********************************************************************
1639 * _fstati64 (MSVCRT.@)
1641 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1644 struct MSVCRT__stat64 buf64
;
1646 ret
= MSVCRT__fstat64(fd
, &buf64
);
1648 msvcrt_stat64_to_stati64(&buf64
, buf
);
1652 /*********************************************************************
1655 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1657 struct MSVCRT__stat64 buf64
;
1659 ret
= MSVCRT__fstat64(fd
, &buf64
);
1661 msvcrt_stat64_to_stat(&buf64
, buf
);
1665 /*********************************************************************
1666 * _fstat32 (MSVCR80.@)
1668 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1671 struct MSVCRT__stat64 buf64
;
1673 ret
= MSVCRT__fstat64(fd
, &buf64
);
1675 msvcrt_stat64_to_stat32(&buf64
, buf
);
1679 /*********************************************************************
1680 * _fstat64i32 (MSVCR80.@)
1682 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1685 struct MSVCRT__stat64 buf64
;
1687 ret
= MSVCRT__fstat64(fd
, &buf64
);
1689 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1693 /*********************************************************************
1694 * _futime64 (MSVCRT.@)
1696 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1698 HANDLE hand
= msvcrt_fdtoh(fd
);
1703 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1708 time_to_filetime( t
->actime
, &at
);
1709 time_to_filetime( t
->modtime
, &wt
);
1712 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1714 msvcrt_set_errno(GetLastError());
1720 /*********************************************************************
1721 * _futime32 (MSVCRT.@)
1723 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1727 struct MSVCRT___utimbuf64 t64
;
1728 t64
.actime
= t
->actime
;
1729 t64
.modtime
= t
->modtime
;
1730 return _futime64( fd
, &t64
);
1733 return _futime64( fd
, NULL
);
1736 /*********************************************************************
1737 * _futime (MSVCRT.@)
1740 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1742 return _futime64( fd
, t
);
1745 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1747 return _futime32( fd
, t
);
1751 /*********************************************************************
1752 * _get_osfhandle (MSVCRT.@)
1754 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1756 HANDLE hand
= msvcrt_fdtoh(fd
);
1757 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1759 return (MSVCRT_intptr_t
)hand
;
1762 /*********************************************************************
1763 * _mktemp_s (MSVCRT.@)
1765 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1769 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1770 return MSVCRT_EINVAL
;
1772 for(len
=0; len
<size
; len
++)
1775 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1778 return MSVCRT_EINVAL
;
1781 for(xno
=1; xno
<=6; xno
++)
1782 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1783 return MSVCRT_EINVAL
;
1785 id
= GetCurrentProcessId();
1786 for(xno
=1; xno
<6; xno
++) {
1787 pattern
[len
-xno
] = id
%10 + '0';
1791 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1792 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1797 *MSVCRT__errno() = MSVCRT_EEXIST
;
1798 return MSVCRT_EEXIST
;
1801 /*********************************************************************
1802 * _mktemp (MSVCRT.@)
1804 char * CDECL
MSVCRT__mktemp(char *pattern
)
1807 char *retVal
= pattern
;
1815 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1819 id
= GetCurrentProcessId();
1823 int tempNum
= id
/ 10;
1824 *pattern
-- = id
- (tempNum
* 10) + '0';
1830 *pattern
= letter
++;
1831 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1833 } while(letter
<= 'z');
1837 /*********************************************************************
1838 * _wmktemp_s (MSVCRT.@)
1840 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1844 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1845 return MSVCRT_EINVAL
;
1847 for(len
=0; len
<size
; len
++)
1850 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1853 return MSVCRT_EINVAL
;
1856 for(xno
=1; xno
<=6; xno
++)
1857 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1858 return MSVCRT_EINVAL
;
1860 id
= GetCurrentProcessId();
1861 for(xno
=1; xno
<6; xno
++) {
1862 pattern
[len
-xno
] = id
%10 + '0';
1866 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1867 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1872 *MSVCRT__errno() = MSVCRT_EEXIST
;
1873 return MSVCRT_EEXIST
;
1876 /*********************************************************************
1877 * _wmktemp (MSVCRT.@)
1879 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1882 MSVCRT_wchar_t
*retVal
= pattern
;
1884 MSVCRT_wchar_t letter
= 'a';
1890 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1894 id
= GetCurrentProcessId();
1898 int tempNum
= id
/ 10;
1899 *pattern
-- = id
- (tempNum
* 10) + '0';
1905 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1907 *pattern
= letter
++;
1908 } while(letter
!= '|');
1912 static unsigned split_oflags(unsigned oflags
)
1915 unsigned unsupp
; /* until we support everything */
1917 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1918 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1919 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1920 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1921 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1922 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1923 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1924 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1925 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1927 if ((unsupp
= oflags
& ~(
1928 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1929 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1930 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1931 MSVCRT__O_NOINHERIT
|
1932 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
1933 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
1935 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1940 /*********************************************************************
1943 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1946 SECURITY_ATTRIBUTES sa
;
1947 HANDLE readHandle
, writeHandle
;
1951 *MSVCRT__errno() = MSVCRT_EINVAL
;
1955 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1956 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1957 sa
.lpSecurityDescriptor
= NULL
;
1958 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1960 unsigned int wxflags
= split_oflags(textmode
);
1964 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
1968 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
1976 MSVCRT__close(pfds
[0]);
1977 CloseHandle(writeHandle
);
1978 *MSVCRT__errno() = MSVCRT_EMFILE
;
1983 CloseHandle(readHandle
);
1984 CloseHandle(writeHandle
);
1985 *MSVCRT__errno() = MSVCRT_EMFILE
;
1990 msvcrt_set_errno(GetLastError());
1995 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1997 char bom
[sizeof(utf8_bom
)];
2000 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2002 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2005 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2006 oflags
|= MSVCRT__O_U8TEXT
;
2007 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2009 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2010 oflags
|= MSVCRT__O_U16TEXT
;
2012 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2018 /*********************************************************************
2019 * _wsopen_s (MSVCRT.@)
2021 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2023 DWORD access
= 0, creation
= 0, attrib
;
2024 SECURITY_ATTRIBUTES sa
;
2025 DWORD sharing
, type
;
2029 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2030 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2032 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2035 wxflag
= split_oflags(oflags
);
2036 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2038 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2039 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2040 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2043 if (oflags
& MSVCRT__O_CREAT
)
2045 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2046 FIXME(": pmode 0x%04x ignored\n", pmode
);
2048 WARN(": pmode 0x%04x ignored\n", pmode
);
2050 if (oflags
& MSVCRT__O_EXCL
)
2051 creation
= CREATE_NEW
;
2052 else if (oflags
& MSVCRT__O_TRUNC
)
2053 creation
= CREATE_ALWAYS
;
2055 creation
= OPEN_ALWAYS
;
2057 else /* no MSVCRT__O_CREAT */
2059 if (oflags
& MSVCRT__O_TRUNC
)
2060 creation
= TRUNCATE_EXISTING
;
2062 creation
= OPEN_EXISTING
;
2067 case MSVCRT__SH_DENYRW
:
2070 case MSVCRT__SH_DENYWR
:
2071 sharing
= FILE_SHARE_READ
;
2073 case MSVCRT__SH_DENYRD
:
2074 sharing
= FILE_SHARE_WRITE
;
2076 case MSVCRT__SH_DENYNO
:
2077 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2080 ERR( "Unhandled shflags 0x%x\n", shflags
);
2081 return MSVCRT_EINVAL
;
2083 attrib
= FILE_ATTRIBUTE_NORMAL
;
2085 if (oflags
& MSVCRT__O_TEMPORARY
)
2087 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2089 sharing
|= FILE_SHARE_DELETE
;
2092 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2093 sa
.lpSecurityDescriptor
= NULL
;
2094 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2096 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2097 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2098 && !(access
&GENERIC_READ
))
2100 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2101 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2102 if (hand
!= INVALID_HANDLE_VALUE
)
2104 oflags
= check_bom(hand
, oflags
, FALSE
);
2108 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2111 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2112 if (hand
== INVALID_HANDLE_VALUE
) {
2113 WARN(":failed-last error (%d)\n",GetLastError());
2114 msvcrt_set_errno(GetLastError());
2115 return *MSVCRT__errno();
2118 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2120 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2121 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2122 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2124 if (oflags
& MSVCRT__O_U8TEXT
)
2126 DWORD written
= 0, tmp
;
2128 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2129 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2131 if (written
!= sizeof(utf8_bom
)) {
2132 WARN("error writing BOM\n");
2134 msvcrt_set_errno(GetLastError());
2135 return *MSVCRT__errno();
2140 DWORD written
= 0, tmp
;
2142 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2143 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2145 if (written
!= sizeof(utf16_bom
))
2147 WARN("error writing BOM\n");
2149 msvcrt_set_errno(GetLastError());
2150 return *MSVCRT__errno();
2154 else if (access
& GENERIC_READ
)
2155 oflags
= check_bom(hand
, oflags
, TRUE
);
2158 type
= GetFileType(hand
);
2159 if (type
== FILE_TYPE_CHAR
)
2161 else if (type
== FILE_TYPE_PIPE
)
2164 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2166 return *MSVCRT__errno();
2168 if (oflags
& MSVCRT__O_WTEXT
)
2169 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2170 else if (oflags
& MSVCRT__O_U16TEXT
)
2171 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
2172 else if (oflags
& MSVCRT__O_U8TEXT
)
2173 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
2175 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2179 /*********************************************************************
2180 * _wsopen (MSVCRT.@)
2182 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2187 if (oflags
& MSVCRT__O_CREAT
)
2191 __ms_va_start(ap
, shflags
);
2192 pmode
= va_arg(ap
, int);
2198 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2202 /*********************************************************************
2203 * _sopen_s (MSVCRT.@)
2205 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2207 MSVCRT_wchar_t
*pathW
;
2210 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2211 return MSVCRT_EINVAL
;
2213 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2214 return MSVCRT_EINVAL
;
2216 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2221 /*********************************************************************
2224 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2229 if (oflags
& MSVCRT__O_CREAT
)
2233 __ms_va_start(ap
, shflags
);
2234 pmode
= va_arg(ap
, int);
2240 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2244 /*********************************************************************
2247 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2251 if (flags
& MSVCRT__O_CREAT
)
2254 __ms_va_start(ap
, flags
);
2255 pmode
= va_arg(ap
, int);
2257 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2260 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2263 /*********************************************************************
2266 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2270 if (flags
& MSVCRT__O_CREAT
)
2273 __ms_va_start(ap
, flags
);
2274 pmode
= va_arg(ap
, int);
2276 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2279 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2282 /*********************************************************************
2285 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2287 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2288 return MSVCRT__open(path
, usedFlags
);
2291 /*********************************************************************
2292 * _wcreat (MSVCRT.@)
2294 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2296 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2297 return MSVCRT__wopen(path
, usedFlags
);
2300 /*********************************************************************
2301 * _open_osfhandle (MSVCRT.@)
2303 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2308 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2309 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2310 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2311 * text - it never sets MSVCRT__O_BINARY.
2313 /* don't let split_oflags() decide the mode if no mode is passed */
2314 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2315 oflags
|= MSVCRT__O_BINARY
;
2317 flags
= GetFileType((HANDLE
)handle
);
2318 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2320 msvcrt_set_errno(GetLastError());
2324 if (flags
== FILE_TYPE_CHAR
)
2326 else if (flags
== FILE_TYPE_PIPE
)
2330 flags
|= split_oflags(oflags
);
2332 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2333 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2337 /*********************************************************************
2340 int CDECL
MSVCRT__rmtmp(void)
2342 int num_removed
= 0, i
;
2346 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2347 file
= msvcrt_get_file(i
);
2349 if (file
->_tmpfname
)
2351 MSVCRT_fclose(file
);
2358 TRACE(":removed (%d) temp files\n",num_removed
);
2362 static inline int get_utf8_char_len(char ch
)
2364 if((ch
&0xf8) == 0xf0)
2366 else if((ch
&0xf0) == 0xe0)
2368 else if((ch
&0xe0) == 0xc0)
2373 /*********************************************************************
2374 * (internal) read_utf8
2376 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2378 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2379 HANDLE hand
= fdinfo
->handle
;
2380 char min_buf
[4], *readbuf
, lookahead
;
2381 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2383 /* make the buffer big enough to hold at least one character */
2384 /* read bytes have to fit to output and lookahead buffers */
2386 readbuf_size
= count
< 4 ? 4 : count
;
2387 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2392 if(fdinfo
->lookahead
[0] != '\n') {
2393 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2394 fdinfo
->lookahead
[0] = '\n';
2396 if(fdinfo
->lookahead
[1] != '\n') {
2397 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2398 fdinfo
->lookahead
[1] = '\n';
2400 if(fdinfo
->lookahead
[2] != '\n') {
2401 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2402 fdinfo
->lookahead
[2] = '\n';
2407 /* NOTE: this case is broken in native dll, reading
2408 * sometimes fails when small buffer is passed
2411 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2412 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2413 fdinfo
->wxflag
|= WX_ATEOF
;
2416 msvcrt_set_errno(GetLastError());
2419 }else if(!num_read
) {
2420 fdinfo
->wxflag
|= WX_ATEOF
;
2426 char_len
= get_utf8_char_len(readbuf
[0]);
2428 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2432 if(readbuf
[0] == '\n')
2433 fdinfo
->wxflag
|= WX_READNL
;
2435 fdinfo
->wxflag
&= ~WX_READNL
;
2437 if(readbuf
[0] == 0x1a) {
2438 fdinfo
->wxflag
|= WX_ATEOF
;
2442 if(readbuf
[0] == '\r') {
2443 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2445 else if(lookahead
== '\n')
2449 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2450 fdinfo
->lookahead
[0] = lookahead
;
2452 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2457 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2458 msvcrt_set_errno(GetLastError());
2465 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2468 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2469 fdinfo
->wxflag
|= WX_ATEOF
;
2470 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2473 msvcrt_set_errno(GetLastError());
2474 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2477 }else if(!pos
&& !num_read
) {
2478 fdinfo
->wxflag
|= WX_ATEOF
;
2479 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2484 if(readbuf
[0] == '\n')
2485 fdinfo
->wxflag
|= WX_READNL
;
2487 fdinfo
->wxflag
&= ~WX_READNL
;
2489 /* Find first byte of last character (may be incomplete) */
2490 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2491 if((readbuf
[i
]&0xc0) != 0x80)
2493 char_len
= get_utf8_char_len(readbuf
[i
]);
2494 if(char_len
+i
<= pos
)
2497 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2499 fdinfo
->lookahead
[0] = readbuf
[i
];
2501 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2503 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2505 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2509 for(i
=0, j
=0; i
<pos
; i
++) {
2510 if(readbuf
[i
] == 0x1a) {
2511 fdinfo
->wxflag
|= WX_ATEOF
;
2515 /* strip '\r' if followed by '\n' */
2516 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2517 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2518 readbuf
[j
++] = '\r';
2519 }else if(lookahead
== '\n' && j
==0) {
2520 readbuf
[j
++] = '\n';
2522 if(lookahead
!= '\n')
2523 readbuf
[j
++] = '\r';
2525 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2526 fdinfo
->lookahead
[0] = lookahead
;
2528 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2530 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2531 readbuf
[j
++] = readbuf
[i
];
2536 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2537 msvcrt_set_errno(GetLastError());
2538 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2542 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2546 /*********************************************************************
2549 * When reading \r as last character in text mode, read() positions
2550 * the file pointer on the \r character while getc() goes on to
2553 static int read_i(int fd
, void *buf
, unsigned int count
)
2555 DWORD num_read
, utf16
;
2556 char *bufstart
= buf
;
2557 HANDLE hand
= msvcrt_fdtoh(fd
);
2558 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2563 if (fdinfo
->wxflag
& WX_ATEOF
) {
2564 TRACE("already at EOF, returning 0\n");
2567 /* Don't trace small reads, it gets *very* annoying */
2569 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2570 if (hand
== INVALID_HANDLE_VALUE
)
2572 *MSVCRT__errno() = MSVCRT_EBADF
;
2576 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2577 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2579 *MSVCRT__errno() = MSVCRT_EINVAL
;
2583 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2584 return read_utf8(fd
, buf
, count
);
2586 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2588 if (fdinfo
->lookahead
[0] != '\n')
2590 bufstart
[0] = fdinfo
->lookahead
[0];
2591 fdinfo
->lookahead
[0] = '\n';
2595 bufstart
[1] = fdinfo
->lookahead
[1];
2596 fdinfo
->lookahead
[1] = '\n';
2599 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2600 num_read
+= 1+utf16
;
2605 if(utf16
&& (num_read
&1))
2607 /* msvcr90 uses uninitialized value from the buffer in this case */
2608 /* msvcrt ignores additional data */
2609 ERR("got odd number of bytes in UTF16 mode\n");
2613 if (count
!= 0 && num_read
== 0)
2615 fdinfo
->wxflag
|= WX_ATEOF
;
2616 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2618 else if (fdinfo
->wxflag
& WX_TEXT
)
2622 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2623 fdinfo
->wxflag
|= WX_READNL
;
2625 fdinfo
->wxflag
&= ~WX_READNL
;
2627 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2629 /* in text mode, a ctrl-z signals EOF */
2630 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2632 fdinfo
->wxflag
|= WX_ATEOF
;
2633 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2637 /* in text mode, strip \r if followed by \n */
2638 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2643 lookahead
[1] = '\n';
2644 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2646 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2648 bufstart
[j
++] = '\n';
2649 if(utf16
) bufstart
[j
++] = 0;
2653 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2655 bufstart
[j
++] = '\r';
2656 if(utf16
) bufstart
[j
++] = 0;
2659 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2661 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2663 bufstart
[j
++] = '\n';
2664 if (utf16
) bufstart
[j
++] = 0;
2668 fdinfo
->lookahead
[0] = lookahead
[0];
2669 fdinfo
->lookahead
[1] = lookahead
[1];
2673 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2678 bufstart
[j
++] = '\r';
2679 if(utf16
) bufstart
[j
++] = 0;
2682 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2683 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2685 bufstart
[j
++] = bufstart
[i
];
2686 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2694 if (GetLastError() == ERROR_BROKEN_PIPE
)
2696 TRACE(":end-of-pipe\n");
2697 fdinfo
->wxflag
|= WX_ATEOF
;
2702 TRACE(":failed-last error (%d)\n",GetLastError());
2708 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2712 /*********************************************************************
2715 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2718 num_read
= read_i(fd
, buf
, count
);
2722 /*********************************************************************
2723 * _setmode (MSVCRT.@)
2725 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2727 int ret
= msvcrt_get_ioinfo(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2728 if(ret
==MSVCRT__O_TEXT
&& (msvcrt_get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2729 ret
= MSVCRT__O_WTEXT
;
2731 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2732 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2733 *MSVCRT__errno() = MSVCRT_EINVAL
;
2737 if(mode
== MSVCRT__O_BINARY
) {
2738 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2739 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2743 msvcrt_get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2744 if(mode
== MSVCRT__O_TEXT
)
2745 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2746 else if(mode
== MSVCRT__O_U8TEXT
)
2747 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2749 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2754 /*********************************************************************
2755 * _stat64 (MSVCRT.@)
2757 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2760 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2761 unsigned short mode
= ALL_S_IREAD
;
2764 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2766 plen
= strlen(path
);
2767 while (plen
&& path
[plen
-1]==' ')
2770 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2771 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2773 *MSVCRT__errno() = MSVCRT_ENOENT
;
2777 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2779 TRACE("failed (%d)\n",GetLastError());
2780 *MSVCRT__errno() = MSVCRT_ENOENT
;
2784 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2786 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2787 Bon 011120: This FIXME seems incorrect
2788 Also a letter as first char isn't enough to be classified
2791 if (isalpha(*path
)&& (*(path
+1)==':'))
2792 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2794 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2796 /* Dir, or regular file? */
2797 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2798 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2801 mode
|= MSVCRT__S_IFREG
;
2803 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2805 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2806 (tolower(path
[plen
-3]) << 16);
2807 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2808 mode
|= ALL_S_IEXEC
;
2812 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2813 mode
|= ALL_S_IWRITE
;
2815 buf
->st_mode
= mode
;
2817 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2818 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2820 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2821 buf
->st_mtime
= buf
->st_ctime
= dw
;
2822 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2823 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2824 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2828 /*********************************************************************
2829 * _stati64 (MSVCRT.@)
2831 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2834 struct MSVCRT__stat64 buf64
;
2836 ret
= MSVCRT_stat64(path
, &buf64
);
2838 msvcrt_stat64_to_stati64(&buf64
, buf
);
2842 /*********************************************************************
2845 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2848 struct MSVCRT__stat64 buf64
;
2850 ret
= MSVCRT_stat64( path
, &buf64
);
2852 msvcrt_stat64_to_stat(&buf64
, buf
);
2856 /*********************************************************************
2857 * _stat32 (MSVCR100.@)
2859 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2862 struct MSVCRT__stat64 buf64
;
2864 ret
= MSVCRT_stat64(path
, &buf64
);
2866 msvcrt_stat64_to_stat32(&buf64
, buf
);
2870 /*********************************************************************
2871 * _stat32i64 (MSVCR100.@)
2873 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2876 struct MSVCRT__stat64 buf64
;
2878 ret
= MSVCRT_stat64(path
, &buf64
);
2880 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2884 /*********************************************************************
2885 * _stat64i32 (MSVCR100.@)
2887 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2890 struct MSVCRT__stat64 buf64
;
2892 ret
= MSVCRT_stat64(path
, &buf64
);
2894 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2898 /*********************************************************************
2899 * _wstat64 (MSVCRT.@)
2901 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2904 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2905 unsigned short mode
= ALL_S_IREAD
;
2908 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2910 plen
= strlenW(path
);
2911 while (plen
&& path
[plen
-1]==' ')
2914 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2915 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2917 *MSVCRT__errno() = MSVCRT_ENOENT
;
2921 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2923 TRACE("failed (%d)\n",GetLastError());
2924 *MSVCRT__errno() = MSVCRT_ENOENT
;
2928 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2930 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2931 if (MSVCRT_iswalpha(*path
))
2932 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2934 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2936 /* Dir, or regular file? */
2937 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2938 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2941 mode
|= MSVCRT__S_IFREG
;
2943 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2945 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2946 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2947 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2948 mode
|= ALL_S_IEXEC
;
2952 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2953 mode
|= ALL_S_IWRITE
;
2955 buf
->st_mode
= mode
;
2957 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2958 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2960 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2961 buf
->st_mtime
= buf
->st_ctime
= dw
;
2962 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2963 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2964 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2968 /*********************************************************************
2969 * _wstati64 (MSVCRT.@)
2971 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2974 struct MSVCRT__stat64 buf64
;
2976 ret
= MSVCRT__wstat64(path
, &buf64
);
2978 msvcrt_stat64_to_stati64(&buf64
, buf
);
2982 /*********************************************************************
2985 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2988 struct MSVCRT__stat64 buf64
;
2990 ret
= MSVCRT__wstat64( path
, &buf64
);
2991 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2995 /*********************************************************************
2996 * _wstat32 (MSVCR100.@)
2998 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3001 struct MSVCRT__stat64 buf64
;
3003 ret
= MSVCRT__wstat64(path
, &buf64
);
3005 msvcrt_stat64_to_stat32(&buf64
, buf
);
3009 /*********************************************************************
3010 * _wstat32i64 (MSVCR100.@)
3012 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3015 struct MSVCRT__stat64 buf64
;
3017 ret
= MSVCRT__wstat64(path
, &buf64
);
3019 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3023 /*********************************************************************
3024 * _wstat64i32 (MSVCR100.@)
3026 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3029 struct MSVCRT__stat64 buf64
;
3031 ret
= MSVCRT__wstat64(path
, &buf64
);
3033 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3037 /*********************************************************************
3040 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3042 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3045 /*********************************************************************
3046 * _telli64 (MSVCRT.@)
3048 __int64 CDECL
_telli64(int fd
)
3050 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3053 /*********************************************************************
3054 * _tempnam (MSVCRT.@)
3056 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3058 char tmpbuf
[MAX_PATH
];
3059 const char *tmp_dir
= MSVCRT_getenv("TMP");
3061 if (tmp_dir
) dir
= tmp_dir
;
3063 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3064 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3066 TRACE("got name (%s)\n",tmpbuf
);
3067 DeleteFileA(tmpbuf
);
3068 return MSVCRT__strdup(tmpbuf
);
3070 TRACE("failed (%d)\n",GetLastError());
3074 /*********************************************************************
3075 * _wtempnam (MSVCRT.@)
3077 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3079 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3080 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3081 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3083 if (tmp_dir
) dir
= tmp_dir
;
3085 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3086 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3088 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3089 DeleteFileW(tmpbuf
);
3090 return MSVCRT__wcsdup(tmpbuf
);
3092 TRACE("failed (%d)\n",GetLastError());
3096 /*********************************************************************
3099 int CDECL
MSVCRT__umask(int umask
)
3101 int old_umask
= MSVCRT_umask
;
3102 TRACE("(%d)\n",umask
);
3103 MSVCRT_umask
= umask
;
3107 /*********************************************************************
3108 * _utime64 (MSVCRT.@)
3110 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3112 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3116 int retVal
= _futime64(fd
, t
);
3123 /*********************************************************************
3124 * _utime32 (MSVCRT.@)
3126 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3130 struct MSVCRT___utimbuf64 t64
;
3131 t64
.actime
= t
->actime
;
3132 t64
.modtime
= t
->modtime
;
3133 return _utime64( path
, &t64
);
3136 return _utime64( path
, NULL
);
3139 /*********************************************************************
3143 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3145 return _utime64( path
, t
);
3148 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3150 return _utime32( path
, t
);
3154 /*********************************************************************
3155 * _wutime64 (MSVCRT.@)
3157 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3159 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3163 int retVal
= _futime64(fd
, t
);
3170 /*********************************************************************
3171 * _wutime32 (MSVCRT.@)
3173 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3177 struct MSVCRT___utimbuf64 t64
;
3178 t64
.actime
= t
->actime
;
3179 t64
.modtime
= t
->modtime
;
3180 return _wutime64( path
, &t64
);
3183 return _wutime64( path
, NULL
);
3186 /*********************************************************************
3187 * _wutime (MSVCRT.@)
3190 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3192 return _wutime64( path
, t
);
3195 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3197 return _wutime32( path
, t
);
3201 /*********************************************************************
3204 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3207 ioinfo
*info
= msvcrt_get_ioinfo(fd
);
3208 HANDLE hand
= info
->handle
;
3210 /* Don't trace small writes, it gets *very* annoying */
3213 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3215 if (hand
== INVALID_HANDLE_VALUE
)
3217 *MSVCRT__errno() = MSVCRT_EBADF
;
3221 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3223 *MSVCRT__errno() = MSVCRT_EINVAL
;
3227 /* If appending, go to EOF */
3228 if (info
->wxflag
& WX_APPEND
)
3229 MSVCRT__lseek(fd
, 0, FILE_END
);
3231 if (!(info
->wxflag
& WX_TEXT
))
3233 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3234 && (num_written
== count
))
3236 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3237 hand
, GetLastError());
3238 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3242 unsigned int i
, j
, nr_lf
, size
;
3245 const char *s
= buf
, *buf_start
= buf
;
3247 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3249 /* find number of \n */
3250 for (nr_lf
=0, i
=0; i
<count
; i
++)
3256 if ((q
= p
= MSVCRT_malloc(size
)))
3258 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3267 FIXME("Malloc failed\n");
3279 else if (info
->exflag
& EF_UTF16
)
3281 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3282 if (s
[i
]=='\n' && s
[i
+1]==0)
3287 if ((q
= p
= MSVCRT_malloc(size
)))
3289 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3291 if (s
[i
]=='\n' && s
[i
+1]==0)
3302 FIXME("Malloc failed\n");
3318 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3319 if (s
[i
]=='\n' && s
[i
+1]==0)
3322 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3324 msvcrt_set_errno(GetLastError());
3329 size
= conv_len
+nr_lf
;
3330 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3332 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3334 if (s
[i
]=='\n' && s
[i
+1]==0)
3342 q
= p
+count
+nr_lf
*2;
3343 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3344 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3348 FIXME("Malloc failed\n");
3355 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3359 if (num_written
!= size
)
3361 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3362 fd
, hand
, GetLastError(), num_written
);
3363 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3364 return s
- buf_start
;
3372 /*********************************************************************
3375 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3379 MSVCRT__lock_file(file
);
3380 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3381 if (len
== sizeof(val
)) {
3382 MSVCRT__unlock_file(file
);
3386 file
->_flag
|= MSVCRT__IOERR
;
3387 MSVCRT__unlock_file(file
);
3391 /*********************************************************************
3394 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3398 MSVCRT__lock_file(file
);
3399 ret
= MSVCRT__fclose_nolock(file
);
3400 MSVCRT__unlock_file(file
);
3405 /*********************************************************************
3406 * _fclose_nolock (MSVCRT.@)
3408 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3413 MSVCRT_free(file
->_tmpfname
);
3414 file
->_tmpfname
= NULL
;
3415 /* flush stdio buffers */
3416 if(file
->_flag
& MSVCRT__IOWRT
)
3417 MSVCRT_fflush(file
);
3418 if(file
->_flag
& MSVCRT__IOMYBUF
)
3419 MSVCRT_free(file
->_base
);
3421 r
=MSVCRT__close(file
->_file
);
3424 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3427 /*********************************************************************
3430 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3432 return file
->_flag
& MSVCRT__IOEOF
;
3435 /*********************************************************************
3438 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3440 return file
->_flag
& MSVCRT__IOERR
;
3443 /*********************************************************************
3444 * _filbuf (MSVCRT.@)
3446 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3449 MSVCRT__lock_file(file
);
3451 if(file
->_flag
& MSVCRT__IOSTRG
) {
3452 MSVCRT__unlock_file(file
);
3456 /* Allocate buffer if needed */
3457 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3458 msvcrt_alloc_buffer(file
);
3460 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3461 if(file
->_flag
& MSVCRT__IORW
)
3462 file
->_flag
|= MSVCRT__IOREAD
;
3464 MSVCRT__unlock_file(file
);
3469 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3471 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3472 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3473 MSVCRT__unlock_file(file
);
3477 MSVCRT__unlock_file(file
);
3480 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3482 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3484 MSVCRT__unlock_file(file
);
3489 file
->_ptr
= file
->_base
+1;
3490 c
= *(unsigned char *)file
->_base
;
3491 MSVCRT__unlock_file(file
);
3496 /*********************************************************************
3499 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3504 MSVCRT__lock_file(file
);
3507 i
= (unsigned char *)file
->_ptr
++;
3510 j
= MSVCRT__filbuf(file
);
3512 MSVCRT__unlock_file(file
);
3516 /*********************************************************************
3517 * _fgetchar (MSVCRT.@)
3519 int CDECL
MSVCRT__fgetchar(void)
3521 return MSVCRT_fgetc(MSVCRT_stdin
);
3524 /*********************************************************************
3527 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3529 int cc
= MSVCRT_EOF
;
3530 char * buf_start
= s
;
3532 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3533 file
,file
->_file
,s
,size
);
3535 MSVCRT__lock_file(file
);
3537 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3542 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3544 TRACE(":nothing read\n");
3545 MSVCRT__unlock_file(file
);
3548 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3551 TRACE(":got %s\n", debugstr_a(buf_start
));
3552 MSVCRT__unlock_file(file
);
3556 /*********************************************************************
3559 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3564 MSVCRT__lock_file(file
);
3566 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3567 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3570 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3571 ch
= MSVCRT_fgetc(file
);
3572 if(ch
== MSVCRT_EOF
) {
3579 char mbs
[MSVCRT_MB_LEN_MAX
];
3582 ch
= MSVCRT_fgetc(file
);
3583 if(ch
!= MSVCRT_EOF
) {
3585 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3586 ch
= MSVCRT_fgetc(file
);
3587 if(ch
!= MSVCRT_EOF
) {
3596 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3600 MSVCRT__unlock_file(file
);
3604 /*********************************************************************
3607 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3614 MSVCRT__lock_file(file
);
3615 for (j
=0; j
<sizeof(int); j
++) {
3616 k
= MSVCRT_fgetc(file
);
3617 if (k
== MSVCRT_EOF
) {
3618 file
->_flag
|= MSVCRT__IOEOF
;
3619 MSVCRT__unlock_file(file
);
3625 MSVCRT__unlock_file(file
);
3629 /*********************************************************************
3632 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3634 return MSVCRT_fgetwc(file
);
3637 /*********************************************************************
3638 * _fgetwchar (MSVCRT.@)
3640 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3642 return MSVCRT_fgetwc(MSVCRT_stdin
);
3645 /*********************************************************************
3646 * getwchar (MSVCRT.@)
3648 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3650 return MSVCRT__fgetwchar();
3653 /*********************************************************************
3656 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3658 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3659 MSVCRT_wchar_t
* buf_start
= s
;
3661 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3662 file
,file
->_file
,s
,size
);
3664 MSVCRT__lock_file(file
);
3666 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3671 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3673 TRACE(":nothing read\n");
3674 MSVCRT__unlock_file(file
);
3677 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3680 TRACE(":got %s\n", debugstr_w(buf_start
));
3681 MSVCRT__unlock_file(file
);
3685 /*********************************************************************
3686 * _flsbuf (MSVCRT.@)
3688 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3690 /* Flush output buffer */
3691 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3692 msvcrt_alloc_buffer(file
);
3694 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3695 if(file
->_flag
& MSVCRT__IORW
)
3696 file
->_flag
|= MSVCRT__IOWRT
;
3700 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3703 if(file
->_cnt
<= 0) {
3704 res
= msvcrt_flush_buffer(file
);
3707 file
->_cnt
=file
->_bufsiz
;
3715 /* set _cnt to 0 for unbuffered FILEs */
3717 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3720 file
->_flag
|= MSVCRT__IOERR
;
3725 /*********************************************************************
3728 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3732 MSVCRT__lock_file(file
);
3733 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3734 MSVCRT__unlock_file(file
);
3739 /*********************************************************************
3740 * _fwrite_nolock (MSVCRT.@)
3742 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3744 MSVCRT_size_t wrcnt
=size
* nmemb
;
3750 if(file
->_cnt
< 0) {
3751 WARN("negative file->_cnt value in %p\n", file
);
3752 file
->_flag
|= MSVCRT__IOERR
;
3754 } else if(file
->_cnt
) {
3755 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3756 memcpy(file
->_ptr
, ptr
, pcnt
);
3761 ptr
= (const char*)ptr
+ pcnt
;
3762 } else if((file
->_flag
& MSVCRT__IONBF
)
3763 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3764 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3768 if(file
->_flag
& MSVCRT__IONBF
)
3770 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3771 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3773 bufsiz
= file
->_bufsiz
;
3775 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3777 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3780 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3781 file
->_flag
|= MSVCRT__IOERR
;
3786 ptr
= (const char*)ptr
+ pcnt
;
3788 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3792 ptr
= (const char*)ptr
+ 1;
3796 return written
/ size
;
3799 /*********************************************************************
3802 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3804 MSVCRT_wchar_t mwc
=wc
;
3808 MSVCRT__lock_file(file
);
3809 fdinfo
= msvcrt_get_ioinfo(file
->_file
);
3811 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3812 char buf
[MSVCRT_MB_LEN_MAX
];
3815 char_len
= MSVCRT_wctomb(buf
, mwc
);
3816 if(char_len
!=-1 && MSVCRT_fwrite(buf
, char_len
, 1, file
)==1)
3820 }else if(MSVCRT_fwrite(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3826 MSVCRT__unlock_file(file
);
3830 /*********************************************************************
3831 * _fputwchar (MSVCRT.@)
3833 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3835 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3838 /*********************************************************************
3839 * _wfsopen (MSVCRT.@)
3841 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3844 int open_flags
, stream_flags
, fd
;
3846 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3848 /* map mode string to open() flags. "man fopen" for possibilities. */
3849 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3853 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3856 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3858 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3865 TRACE(":got (%p)\n",file
);
3866 if (fd
>= 0 && !file
)
3872 /*********************************************************************
3873 * _fsopen (MSVCRT.@)
3875 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3878 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3880 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3881 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3882 *MSVCRT__errno() = MSVCRT_EINVAL
;
3885 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3888 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3889 *MSVCRT__errno() = MSVCRT_EINVAL
;
3893 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
3900 /*********************************************************************
3903 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
3905 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3908 /*********************************************************************
3909 * fopen_s (MSVCRT.@)
3911 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
3912 const char *filename
, const char *mode
)
3914 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3915 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3916 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3918 *pFile
= MSVCRT_fopen(filename
, mode
);
3921 return *MSVCRT__errno();
3925 /*********************************************************************
3926 * _wfopen (MSVCRT.@)
3928 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
3930 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3933 /*********************************************************************
3934 * _wfopen_s (MSVCRT.@)
3936 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
3937 const MSVCRT_wchar_t
*mode
)
3939 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3940 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3941 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3943 *pFile
= MSVCRT__wfopen(filename
, mode
);
3946 return *MSVCRT__errno();
3950 /*********************************************************************
3953 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
3957 MSVCRT__lock_file(file
);
3963 res
= msvcrt_flush_buffer(file
);
3964 MSVCRT__unlock_file(file
);
3965 return res
? res
: c
;
3968 MSVCRT__unlock_file(file
);
3972 res
= MSVCRT__flsbuf(c
, file
);
3973 MSVCRT__unlock_file(file
);
3978 /*********************************************************************
3979 * _fputchar (MSVCRT.@)
3981 int CDECL
MSVCRT__fputchar(int c
)
3983 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3986 /*********************************************************************
3989 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3993 MSVCRT__lock_file(file
);
3994 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
3995 MSVCRT__unlock_file(file
);
4000 /*********************************************************************
4001 * _fread_nolock (MSVCRT.@)
4003 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4005 MSVCRT_size_t rcnt
=size
* nmemb
;
4006 MSVCRT_size_t read
=0;
4007 MSVCRT_size_t pread
=0;
4012 /* first buffered data */
4014 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4015 memcpy(ptr
, file
->_ptr
, pcnt
);
4020 ptr
= (char*)ptr
+ pcnt
;
4021 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4022 if(file
->_flag
& MSVCRT__IORW
) {
4023 file
->_flag
|= MSVCRT__IOREAD
;
4029 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4030 msvcrt_alloc_buffer(file
);
4035 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4036 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4037 file
->_ptr
= file
->_base
;
4038 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4039 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4040 if (i
> 0 && i
< file
->_cnt
) {
4041 msvcrt_get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4042 file
->_flag
&= ~MSVCRT__IOEOF
;
4045 memcpy(ptr
, file
->_ptr
, i
);
4049 } else if (rcnt
> INT_MAX
) {
4050 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4051 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4052 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4054 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4058 ptr
= (char *)ptr
+i
;
4059 /* expose feof condition in the flags
4060 * MFC tests file->_flag for feof, and doesn't call feof())
4062 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
4063 file
->_flag
|= MSVCRT__IOEOF
;
4066 file
->_flag
|= MSVCRT__IOERR
;
4077 /*********************************************************************
4078 * fread_s (MSVCR80.@)
4080 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4081 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4083 size_t bytes_left
, buf_pos
;
4085 TRACE("(%p %lu %lu %lu %p\n", buf
, buf_size
, elem_size
, count
, stream
);
4087 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4089 memset(buf
, 0, buf_size
);
4092 if(!elem_size
|| !count
) return 0;
4093 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4094 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4096 bytes_left
= elem_size
*count
;
4099 if(stream
->_cnt
> 0) {
4100 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4102 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4103 memset(buf
, 0, buf_size
);
4107 MSVCRT_fread((char*)buf
+buf_pos
, 1, size
, stream
);
4111 int c
= MSVCRT__filbuf(stream
);
4116 if(!MSVCRT_CHECK_PMT_ERR(buf_size
-buf_pos
> 0, MSVCRT_ERANGE
)) {
4117 memset(buf
, 0, buf_size
);
4121 ((char*)buf
)[buf_pos
++] = c
;
4126 return buf_pos
/elem_size
;
4129 /*********************************************************************
4130 * _wfreopen (MSVCRT.@)
4133 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4135 int open_flags
, stream_flags
, fd
;
4137 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4140 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4144 MSVCRT_fclose(file
);
4145 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4147 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4149 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4159 /*********************************************************************
4160 * _wfreopen_s (MSVCRT.@)
4162 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4163 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4165 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4166 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4167 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4168 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4170 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4173 return *MSVCRT__errno();
4177 /*********************************************************************
4178 * freopen (MSVCRT.@)
4181 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4184 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4186 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4187 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4193 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4200 /*********************************************************************
4201 * freopen_s (MSVCRT.@)
4203 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4204 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4206 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4207 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4208 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4209 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4211 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4214 return *MSVCRT__errno();
4218 /*********************************************************************
4219 * fsetpos (MSVCRT.@)
4221 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4225 MSVCRT__lock_file(file
);
4226 /* Note that all this has been lifted 'as is' from fseek */
4227 if(file
->_flag
& MSVCRT__IOWRT
)
4228 msvcrt_flush_buffer(file
);
4230 /* Discard buffered input */
4232 file
->_ptr
= file
->_base
;
4234 /* Reset direction of i/o */
4235 if(file
->_flag
& MSVCRT__IORW
) {
4236 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4239 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4240 MSVCRT__unlock_file(file
);
4244 /*********************************************************************
4245 * _ftelli64 (MSVCRT.@)
4247 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4251 MSVCRT__lock_file(file
);
4252 ret
= MSVCRT__ftelli64_nolock(file
);
4253 MSVCRT__unlock_file(file
);
4258 /*********************************************************************
4259 * _ftelli64_nolock (MSVCRT.@)
4261 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4265 pos
= _telli64(file
->_file
);
4268 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4269 if(file
->_flag
& MSVCRT__IOWRT
) {
4270 pos
+= file
->_ptr
- file
->_base
;
4272 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4275 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4279 } else if(!file
->_cnt
) { /* nothing to do */
4280 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4284 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4285 for(i
=0; i
<file
->_cnt
; i
++)
4286 if(file
->_ptr
[i
] == '\n')
4292 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4295 pos
-= file
->_bufsiz
;
4296 pos
+= file
->_ptr
- file
->_base
;
4298 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4299 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
4302 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4312 /*********************************************************************
4315 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4317 return MSVCRT__ftelli64(file
);
4320 /*********************************************************************
4321 * _ftell_nolock (MSVCRT.@)
4323 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4325 return MSVCRT__ftelli64_nolock(file
);
4328 /*********************************************************************
4329 * fgetpos (MSVCRT.@)
4331 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4333 *pos
= MSVCRT__ftelli64(file
);
4339 /*********************************************************************
4342 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4344 MSVCRT_size_t len
= strlen(s
);
4347 MSVCRT__lock_file(file
);
4348 ret
= MSVCRT_fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4349 MSVCRT__unlock_file(file
);
4353 /*********************************************************************
4356 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4358 MSVCRT_size_t i
, len
= strlenW(s
);
4362 MSVCRT__lock_file(file
);
4363 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4364 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4365 MSVCRT__unlock_file(file
);
4369 tmp_buf
= add_std_buffer(file
);
4370 for (i
=0; i
<len
; i
++) {
4371 if(MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
) {
4372 if(tmp_buf
) remove_std_buffer(file
);
4373 MSVCRT__unlock_file(file
);
4378 if(tmp_buf
) remove_std_buffer(file
);
4379 MSVCRT__unlock_file(file
);
4383 /*********************************************************************
4384 * getchar (MSVCRT.@)
4386 int CDECL
MSVCRT_getchar(void)
4388 return MSVCRT_fgetc(MSVCRT_stdin
);
4391 /*********************************************************************
4394 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4396 return MSVCRT_fgetc(file
);
4399 /*********************************************************************
4402 char * CDECL
MSVCRT_gets(char *buf
)
4405 char * buf_start
= buf
;
4407 MSVCRT__lock_file(MSVCRT_stdin
);
4408 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4409 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
4410 if(cc
!= '\r') *buf
++ = (char)cc
;
4414 TRACE("got '%s'\n", buf_start
);
4415 MSVCRT__unlock_file(MSVCRT_stdin
);
4419 /*********************************************************************
4422 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4425 MSVCRT_wchar_t
* ws
= buf
;
4427 MSVCRT__lock_file(MSVCRT_stdin
);
4428 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4429 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
4432 *buf
++ = (MSVCRT_wchar_t
)cc
;
4436 TRACE("got %s\n", debugstr_w(ws
));
4437 MSVCRT__unlock_file(MSVCRT_stdin
);
4441 /*********************************************************************
4444 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4446 return MSVCRT_fputc(c
, file
);
4449 /*********************************************************************
4450 * putchar (MSVCRT.@)
4452 int CDECL
MSVCRT_putchar(int c
)
4454 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4457 /*********************************************************************
4458 * _putwch (MSVCRT.@)
4460 int CDECL
MSVCRT__putwch(int c
)
4462 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4465 /*********************************************************************
4468 int CDECL
MSVCRT_puts(const char *s
)
4470 MSVCRT_size_t len
= strlen(s
);
4473 MSVCRT__lock_file(MSVCRT_stdout
);
4474 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4475 MSVCRT__unlock_file(MSVCRT_stdout
);
4479 ret
= MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4480 MSVCRT__unlock_file(MSVCRT_stdout
);
4484 /*********************************************************************
4487 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4489 static const MSVCRT_wchar_t nl
= '\n';
4490 MSVCRT_size_t len
= strlenW(s
);
4493 MSVCRT__lock_file(MSVCRT_stdout
);
4494 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4495 MSVCRT__unlock_file(MSVCRT_stdout
);
4499 ret
= MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4500 MSVCRT__unlock_file(MSVCRT_stdout
);
4504 /*********************************************************************
4507 int CDECL
MSVCRT_remove(const char *path
)
4509 TRACE("(%s)\n",path
);
4510 if (DeleteFileA(path
))
4512 TRACE(":failed (%d)\n",GetLastError());
4513 msvcrt_set_errno(GetLastError());
4517 /*********************************************************************
4518 * _wremove (MSVCRT.@)
4520 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4522 TRACE("(%s)\n",debugstr_w(path
));
4523 if (DeleteFileW(path
))
4525 TRACE(":failed (%d)\n",GetLastError());
4526 msvcrt_set_errno(GetLastError());
4530 /*********************************************************************
4533 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4535 TRACE(":from %s to %s\n",oldpath
,newpath
);
4536 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4538 TRACE(":failed (%d)\n",GetLastError());
4539 msvcrt_set_errno(GetLastError());
4543 /*********************************************************************
4544 * _wrename (MSVCRT.@)
4546 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4548 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4549 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4551 TRACE(":failed (%d)\n",GetLastError());
4552 msvcrt_set_errno(GetLastError());
4556 /*********************************************************************
4557 * setvbuf (MSVCRT.@)
4559 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4561 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4562 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4563 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4565 MSVCRT__lock_file(file
);
4567 MSVCRT_fflush(file
);
4568 if(file
->_flag
& MSVCRT__IOMYBUF
)
4569 MSVCRT_free(file
->_base
);
4570 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4573 if(mode
== MSVCRT__IONBF
) {
4574 file
->_flag
|= MSVCRT__IONBF
;
4575 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4578 file
->_base
= file
->_ptr
= buf
;
4579 file
->_flag
|= MSVCRT__USERBUF
;
4580 file
->_bufsiz
= size
;
4582 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4585 MSVCRT__unlock_file(file
);
4589 file
->_flag
|= MSVCRT__IOMYBUF
;
4590 file
->_bufsiz
= size
;
4592 MSVCRT__unlock_file(file
);
4596 /*********************************************************************
4599 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4601 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4604 /*********************************************************************
4607 char * CDECL
MSVCRT_tmpnam(char *s
)
4614 thread_data_t
*data
= msvcrt_get_thread_data();
4616 if(!data
->tmpnam_buffer
)
4617 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4619 s
= data
->tmpnam_buffer
;
4622 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4623 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4624 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4626 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4627 memcpy(p
, tmpstr
, size
);
4629 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4630 GetLastError() == ERROR_FILE_NOT_FOUND
)
4636 /*********************************************************************
4637 * _wtmpnam (MSVCRT.@)
4639 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4641 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4642 MSVCRT_wchar_t tmpstr
[16];
4646 thread_data_t
*data
= msvcrt_get_thread_data();
4648 if(!data
->wtmpnam_buffer
)
4649 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4651 s
= data
->wtmpnam_buffer
;
4654 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4655 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4656 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4658 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4659 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4661 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4662 GetLastError() == ERROR_FILE_NOT_FOUND
)
4668 /*********************************************************************
4669 * tmpfile (MSVCRT.@)
4671 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4673 char *filename
= MSVCRT_tmpnam(NULL
);
4675 MSVCRT_FILE
* file
= NULL
;
4678 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4679 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4680 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4682 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4687 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4690 if(fd
!= -1 && !file
)
4696 /*********************************************************************
4697 * tmpfile_s (MSVCRT.@)
4699 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4701 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4703 *file
= MSVCRT_tmpfile();
4707 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4709 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4712 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4716 MSVCRT__lock_file(file
);
4718 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4719 ret
= MSVCRT_fwrite(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4720 MSVCRT__unlock_file(file
);
4724 for(i
=0; i
<len
; i
++) {
4725 if(MSVCRT_fputwc(str
[i
], file
) == MSVCRT_WEOF
) {
4726 MSVCRT__unlock_file(file
);
4731 MSVCRT__unlock_file(file
);
4735 /*********************************************************************
4736 * vfprintf (MSVCRT.@)
4738 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4743 MSVCRT__lock_file(file
);
4744 tmp_buf
= add_std_buffer(file
);
4745 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4746 if(tmp_buf
) remove_std_buffer(file
);
4747 MSVCRT__unlock_file(file
);
4752 /*********************************************************************
4753 * vfprintf_s (MSVCRT.@)
4755 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4760 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4762 MSVCRT__lock_file(file
);
4763 tmp_buf
= add_std_buffer(file
);
4764 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4765 if(tmp_buf
) remove_std_buffer(file
);
4766 MSVCRT__unlock_file(file
);
4771 /*********************************************************************
4772 * vfwprintf (MSVCRT.@)
4774 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4779 MSVCRT__lock_file(file
);
4780 tmp_buf
= add_std_buffer(file
);
4781 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4782 if(tmp_buf
) remove_std_buffer(file
);
4783 MSVCRT__unlock_file(file
);
4788 /*********************************************************************
4789 * vfwprintf_s (MSVCRT.@)
4791 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4796 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4798 MSVCRT__lock_file(file
);
4799 tmp_buf
= add_std_buffer(file
);
4800 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4801 if(tmp_buf
) remove_std_buffer(file
);
4802 MSVCRT__unlock_file(file
);
4807 /*********************************************************************
4808 * _vfwprintf_l (MSVCRT.@)
4810 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4811 MSVCRT__locale_t locale
, __ms_va_list valist
)
4816 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4818 MSVCRT__lock_file(file
);
4819 tmp_buf
= add_std_buffer(file
);
4820 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4821 if(tmp_buf
) remove_std_buffer(file
);
4822 MSVCRT__unlock_file(file
);
4827 /*********************************************************************
4828 * vprintf (MSVCRT.@)
4830 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4832 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4835 /*********************************************************************
4836 * vprintf_s (MSVCRT.@)
4838 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4840 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4843 /*********************************************************************
4844 * vwprintf (MSVCRT.@)
4846 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4848 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4851 /*********************************************************************
4852 * vwprintf_s (MSVCRT.@)
4854 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4856 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4859 /*********************************************************************
4860 * fprintf (MSVCRT.@)
4862 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4864 __ms_va_list valist
;
4866 __ms_va_start(valist
, format
);
4867 res
= MSVCRT_vfprintf(file
, format
, valist
);
4868 __ms_va_end(valist
);
4872 /*********************************************************************
4873 * fprintf_s (MSVCRT.@)
4875 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
4877 __ms_va_list valist
;
4879 __ms_va_start(valist
, format
);
4880 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
4881 __ms_va_end(valist
);
4885 /*********************************************************************
4886 * fwprintf (MSVCRT.@)
4888 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4890 __ms_va_list valist
;
4892 __ms_va_start(valist
, format
);
4893 res
= MSVCRT_vfwprintf(file
, format
, valist
);
4894 __ms_va_end(valist
);
4898 /*********************************************************************
4899 * fwprintf_s (MSVCRT.@)
4901 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4903 __ms_va_list valist
;
4905 __ms_va_start(valist
, format
);
4906 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
4907 __ms_va_end(valist
);
4911 /*********************************************************************
4912 * _fwprintf_l (MSVCRT.@)
4914 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
4916 __ms_va_list valist
;
4918 __ms_va_start(valist
, locale
);
4919 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
4920 __ms_va_end(valist
);
4924 /*********************************************************************
4927 int CDECL
MSVCRT_printf(const char *format
, ...)
4929 __ms_va_list valist
;
4931 __ms_va_start(valist
, format
);
4932 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
4933 __ms_va_end(valist
);
4937 /*********************************************************************
4938 * printf_s (MSVCRT.@)
4940 int CDECL
MSVCRT_printf_s(const char *format
, ...)
4942 __ms_va_list valist
;
4944 __ms_va_start(valist
, format
);
4945 res
= MSVCRT_vprintf_s(format
, valist
);
4946 __ms_va_end(valist
);
4950 /*********************************************************************
4953 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
4955 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
4957 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
4958 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
4961 MSVCRT__lock_file(file
);
4962 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
4963 && msvcrt_alloc_buffer(file
))
4964 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
4967 if(file
->_ptr
>file
->_base
) {
4969 if(file
->_flag
& MSVCRT__IOSTRG
) {
4970 if(*file
->_ptr
!= c
) {
4972 MSVCRT__unlock_file(file
);
4979 MSVCRT_clearerr(file
);
4980 file
->_flag
|= MSVCRT__IOREAD
;
4981 MSVCRT__unlock_file(file
);
4985 MSVCRT__unlock_file(file
);
4989 /*********************************************************************
4990 * ungetwc (MSVCRT.@)
4992 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4994 MSVCRT_wchar_t mwc
= wc
;
4996 if (wc
== MSVCRT_WEOF
)
4999 MSVCRT__lock_file(file
);
5001 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5002 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
5003 unsigned char * pp
= (unsigned char *)&mwc
;
5006 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5007 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
)) {
5008 MSVCRT__unlock_file(file
);
5013 char mbs
[MSVCRT_MB_LEN_MAX
];
5016 len
= MSVCRT_wctomb(mbs
, mwc
);
5018 MSVCRT__unlock_file(file
);
5022 for(len
--; len
>=0; len
--) {
5023 if(mbs
[len
] != MSVCRT_ungetc(mbs
[len
], file
)) {
5024 MSVCRT__unlock_file(file
);
5030 MSVCRT__unlock_file(file
);
5034 /*********************************************************************
5035 * wprintf (MSVCRT.@)
5037 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5039 __ms_va_list valist
;
5041 __ms_va_start(valist
, format
);
5042 res
= MSVCRT_vwprintf(format
, valist
);
5043 __ms_va_end(valist
);
5047 /*********************************************************************
5048 * wprintf_s (MSVCRT.@)
5050 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5052 __ms_va_list valist
;
5054 __ms_va_start(valist
, format
);
5055 res
= MSVCRT_vwprintf_s(format
, valist
);
5056 __ms_va_end(valist
);
5060 /*********************************************************************
5061 * _getmaxstdio (MSVCRT.@)
5063 int CDECL
MSVCRT__getmaxstdio(void)
5065 return MSVCRT_max_streams
;
5068 /*********************************************************************
5069 * _setmaxstdio (MSVCRT.@)
5071 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5073 TRACE("%d\n", newmax
);
5075 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5078 MSVCRT_max_streams
= newmax
;
5079 return MSVCRT_max_streams
;