2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READNL 0x04 /* read started with \n */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
68 #define WX_NOSEEK 0x40
71 /* values for exflag - it's used differently in msvcr90.dll*/
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom
[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #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_NOSEEK
));
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_NOSEEK
: 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_NOSEEK
: 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_NOSEEK
: 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 HANDLE hand
= msvcrt_fdtoh(fd
);
612 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
613 if (hand
== INVALID_HANDLE_VALUE
)
616 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
619 /* INTERNAL: Allocate stdio file buffer */
620 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
622 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
623 && MSVCRT__isatty(file
->_file
))
626 file
->_base
= MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
628 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
629 file
->_flag
|= MSVCRT__IOMYBUF
;
631 file
->_base
= (char*)(&file
->_charbuf
);
633 file
->_flag
|= MSVCRT__IONBF
;
635 file
->_ptr
= file
->_base
;
640 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
641 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
643 static char buffers
[2][MSVCRT_BUFSIZ
];
645 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
646 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
647 || !MSVCRT__isatty(file
->_file
))
650 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
651 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
652 file
->_flag
|= MSVCRT__USERBUF
;
656 /* INTERNAL: Removes temporary buffer from stdout or stderr */
657 /* Only call this function when add_std_buffer returned TRUE */
658 static void remove_std_buffer(MSVCRT_FILE
*file
)
660 msvcrt_flush_buffer(file
);
661 file
->_ptr
= file
->_base
= NULL
;
662 file
->_bufsiz
= file
->_cnt
= 0;
663 file
->_flag
&= ~MSVCRT__USERBUF
;
666 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
667 static int msvcrt_int_to_base32(int num
, char *str
)
682 *p
= (num
& 31) + '0';
684 *p
+= ('a' - '0' - 10);
691 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
692 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
707 *p
= (num
& 31) + '0';
709 *p
+= ('a' - '0' - 10);
716 /*********************************************************************
717 * __iob_func(MSVCRT.@)
719 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
721 return &MSVCRT__iob
[0];
724 /*********************************************************************
727 int CDECL
MSVCRT__access(const char *filename
, int mode
)
729 DWORD attr
= GetFileAttributesA(filename
);
731 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
733 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
735 msvcrt_set_errno(GetLastError());
738 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
740 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
746 /*********************************************************************
747 * _access_s (MSVCRT.@)
749 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
751 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
752 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
754 if (MSVCRT__access(filename
, mode
) == -1)
755 return *MSVCRT__errno();
759 /*********************************************************************
760 * _waccess (MSVCRT.@)
762 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
764 DWORD attr
= GetFileAttributesW(filename
);
766 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
768 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
770 msvcrt_set_errno(GetLastError());
773 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
775 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
781 /*********************************************************************
782 * _waccess_s (MSVCRT.@)
784 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
786 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
787 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
789 if (MSVCRT__waccess(filename
, mode
) == -1)
790 return *MSVCRT__errno();
794 /*********************************************************************
797 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
799 DWORD oldFlags
= GetFileAttributesA(path
);
801 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
803 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
804 oldFlags
| FILE_ATTRIBUTE_READONLY
;
806 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
809 msvcrt_set_errno(GetLastError());
813 /*********************************************************************
816 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
818 DWORD oldFlags
= GetFileAttributesW(path
);
820 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
822 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
823 oldFlags
| FILE_ATTRIBUTE_READONLY
;
825 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
828 msvcrt_set_errno(GetLastError());
832 /*********************************************************************
835 int CDECL
MSVCRT__unlink(const char *path
)
837 TRACE("%s\n",debugstr_a(path
));
838 if(DeleteFileA(path
))
840 TRACE("failed (%d)\n",GetLastError());
841 msvcrt_set_errno(GetLastError());
845 /*********************************************************************
846 * _wunlink (MSVCRT.@)
848 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
850 TRACE("(%s)\n",debugstr_w(path
));
851 if(DeleteFileW(path
))
853 TRACE("failed (%d)\n",GetLastError());
854 msvcrt_set_errno(GetLastError());
858 /*********************************************************************
861 int CDECL
MSVCRT__commit(int fd
)
863 HANDLE hand
= msvcrt_fdtoh(fd
);
865 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
866 if (hand
== INVALID_HANDLE_VALUE
)
869 if (!FlushFileBuffers(hand
))
871 if (GetLastError() == ERROR_INVALID_HANDLE
)
873 /* FlushFileBuffers fails for console handles
874 * so we ignore this error.
878 TRACE(":failed-last error (%d)\n",GetLastError());
879 msvcrt_set_errno(GetLastError());
886 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
887 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
889 /* INTERNAL: Flush all stream buffer */
890 static int msvcrt_flush_all_buffers(int mask
)
892 int i
, num_flushed
= 0;
896 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
897 file
= msvcrt_get_file(i
);
901 if(file
->_flag
& mask
) {
909 TRACE(":flushed (%d) handles\n",num_flushed
);
913 /*********************************************************************
914 * _flushall (MSVCRT.@)
916 int CDECL
MSVCRT__flushall(void)
918 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
921 /*********************************************************************
924 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
927 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
928 } else if(file
->_flag
& MSVCRT__IOWRT
) {
931 MSVCRT__lock_file(file
);
932 res
= msvcrt_flush_buffer(file
);
934 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
935 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
936 MSVCRT__unlock_file(file
);
939 } else if(file
->_flag
& MSVCRT__IOREAD
) {
940 MSVCRT__lock_file(file
);
942 file
->_ptr
= file
->_base
;
943 MSVCRT__unlock_file(file
);
950 /*********************************************************************
953 int CDECL
MSVCRT__close(int fd
)
959 hand
= msvcrt_fdtoh(fd
);
960 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
961 if (!msvcrt_is_valid_fd(fd
)) {
965 ret
= CloseHandle(hand
) ? 0 : -1;
967 WARN(":failed-last error (%d)\n",GetLastError());
968 msvcrt_set_errno(GetLastError());
976 /*********************************************************************
979 * MSDN isn't clear on this point, but the remarks for _pipe
980 * indicate file descriptors duplicated with _dup and _dup2 are always
983 int CDECL
MSVCRT__dup2(int od
, int nd
)
987 TRACE("(od=%d, nd=%d)\n", od
, nd
);
989 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
993 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od
)->handle
,
994 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
996 int wxflag
= msvcrt_get_ioinfo(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
998 if (msvcrt_is_valid_fd(nd
))
1000 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
1003 CloseHandle(handle
);
1004 *MSVCRT__errno() = MSVCRT_EMFILE
;
1008 /* _dup2 returns 0, not nd, on success */
1015 msvcrt_set_errno(GetLastError());
1020 *MSVCRT__errno() = MSVCRT_EBADF
;
1027 /*********************************************************************
1030 int CDECL
MSVCRT__dup(int od
)
1035 fd
= MSVCRT_fdstart
;
1036 if (MSVCRT__dup2(od
, fd
) == 0)
1044 /*********************************************************************
1047 int CDECL
MSVCRT__eof(int fd
)
1049 DWORD curpos
,endpos
;
1050 LONG hcurpos
,hendpos
;
1051 HANDLE hand
= msvcrt_fdtoh(fd
);
1053 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1055 if (hand
== INVALID_HANDLE_VALUE
)
1058 if (msvcrt_get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1060 /* Otherwise we do it the hard way */
1061 hcurpos
= hendpos
= 0;
1062 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1063 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1065 if (curpos
== endpos
&& hcurpos
== hendpos
)
1067 /* FIXME: shouldn't WX_ATEOF be set here? */
1071 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1075 /*********************************************************************
1076 * _fcloseall (MSVCRT.@)
1078 int CDECL
MSVCRT__fcloseall(void)
1080 int num_closed
= 0, i
;
1084 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1085 file
= msvcrt_get_file(i
);
1087 if (file
->_flag
&& !MSVCRT_fclose(file
))
1092 TRACE(":closed (%d) handles\n",num_closed
);
1096 /* free everything on process exit */
1097 void msvcrt_free_io(void)
1103 MSVCRT__fcloseall();
1105 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1106 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1108 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1110 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1111 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1113 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1114 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1118 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1119 MSVCRT_free(MSVCRT_fstream
[i
]);
1121 DeleteCriticalSection(&MSVCRT_file_cs
);
1124 /*********************************************************************
1125 * _lseeki64 (MSVCRT.@)
1127 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1129 HANDLE hand
= msvcrt_fdtoh(fd
);
1132 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1133 if (hand
== INVALID_HANDLE_VALUE
)
1136 if (whence
< 0 || whence
> 2)
1138 *MSVCRT__errno() = MSVCRT_EINVAL
;
1142 TRACE(":fd (%d) to %s pos %s\n",
1143 fd
,wine_dbgstr_longlong(offset
),
1144 (whence
==SEEK_SET
)?"SEEK_SET":
1145 (whence
==SEEK_CUR
)?"SEEK_CUR":
1146 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1148 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1149 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1150 ofs
.QuadPart
= offset
;
1151 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1152 GetLastError() == ERROR_SUCCESS
)
1154 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_ATEOF
;
1155 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1157 return ofs
.QuadPart
;
1159 TRACE(":error-last error (%d)\n",GetLastError());
1160 msvcrt_set_errno(GetLastError());
1164 /*********************************************************************
1167 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1169 return MSVCRT__lseeki64(fd
, offset
, whence
);
1172 /*********************************************************************
1173 * _lock_file (MSVCRT.@)
1175 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1177 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1178 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1180 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1183 /*********************************************************************
1184 * _unlock_file (MSVCRT.@)
1186 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1188 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1189 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1191 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1194 /*********************************************************************
1195 * _locking (MSVCRT.@)
1197 * This is untested; the underlying LockFile doesn't work yet.
1199 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1203 HANDLE hand
= msvcrt_fdtoh(fd
);
1205 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1206 if (hand
== INVALID_HANDLE_VALUE
)
1209 if (mode
< 0 || mode
> 4)
1211 *MSVCRT__errno() = MSVCRT_EINVAL
;
1215 TRACE(":fd (%d) by 0x%08x mode %s\n",
1216 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1217 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1218 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1219 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1220 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1223 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1225 FIXME ("Seek failed\n");
1226 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1229 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1232 ret
= 1; /* just to satisfy gcc */
1235 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1240 else if (mode
== MSVCRT__LK_UNLCK
)
1241 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1243 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1244 /* FIXME - what about error settings? */
1245 return ret
? 0 : -1;
1248 /*********************************************************************
1249 * _fseeki64 (MSVCRT.@)
1251 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1255 MSVCRT__lock_file(file
);
1256 /* Flush output if needed */
1257 if(file
->_flag
& MSVCRT__IOWRT
)
1258 msvcrt_flush_buffer(file
);
1260 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1262 offset
+= MSVCRT__ftelli64(file
);
1265 /* Discard buffered input */
1267 file
->_ptr
= file
->_base
;
1268 /* Reset direction of i/o */
1269 if(file
->_flag
& MSVCRT__IORW
) {
1270 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1272 /* Clear end of file flag */
1273 file
->_flag
&= ~MSVCRT__IOEOF
;
1274 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1276 MSVCRT__unlock_file(file
);
1280 /*********************************************************************
1283 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1285 return MSVCRT__fseeki64( file
, offset
, whence
);
1288 /*********************************************************************
1289 * _chsize_s (MSVCRT.@)
1291 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1297 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1299 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1303 handle
= msvcrt_fdtoh(fd
);
1304 if (handle
!= INVALID_HANDLE_VALUE
)
1306 /* save the current file pointer */
1307 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1310 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1313 ret
= SetEndOfFile(handle
);
1314 if (!ret
) msvcrt_set_errno(GetLastError());
1317 /* restore the file pointer */
1318 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1323 return ret
? 0 : *MSVCRT__errno();
1326 /*********************************************************************
1327 * _chsize (MSVCRT.@)
1329 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1331 /* _chsize_s returns errno on failure but _chsize should return -1 */
1332 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1335 /*********************************************************************
1336 * clearerr (MSVCRT.@)
1338 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1340 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1342 MSVCRT__lock_file(file
);
1343 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1344 MSVCRT__unlock_file(file
);
1347 /*********************************************************************
1350 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1352 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1354 MSVCRT__lock_file(file
);
1355 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1356 MSVCRT_clearerr(file
);
1357 MSVCRT__unlock_file(file
);
1360 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1362 int plus
= strchrW(mode
, '+') != NULL
;
1364 TRACE("%s\n", debugstr_w(mode
));
1366 while(*mode
== ' ') mode
++;
1371 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1372 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1375 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1376 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1379 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1380 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1383 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1387 *stream_flags
|= MSVCRT__commode
;
1389 while (*mode
&& *mode
!=',')
1393 *open_flags
|= MSVCRT__O_BINARY
;
1394 *open_flags
&= ~MSVCRT__O_TEXT
;
1397 *open_flags
|= MSVCRT__O_TEXT
;
1398 *open_flags
&= ~MSVCRT__O_BINARY
;
1401 *open_flags
|= MSVCRT__O_TEMPORARY
;
1404 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1407 *stream_flags
|= MSVCRT__IOCOMMIT
;
1410 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1413 *open_flags
|= MSVCRT__O_NOINHERIT
;
1422 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1425 ERR("incorrect mode flag: %c\n", mode
[-1]);
1431 static const WCHAR ccs
[] = {'c','c','s'};
1432 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1433 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1434 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1437 while(*mode
== ' ') mode
++;
1438 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1440 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1441 while(*mode
== ' ') mode
++;
1442 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1445 while(*mode
== ' ') mode
++;
1447 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1449 *open_flags
|= MSVCRT__O_U8TEXT
;
1450 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1452 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1454 *open_flags
|= MSVCRT__O_U16TEXT
;
1455 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1457 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1459 *open_flags
|= MSVCRT__O_WTEXT
;
1460 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1464 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1468 while(*mode
== ' ') mode
++;
1471 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1476 /*********************************************************************
1477 * _fdopen (MSVCRT.@)
1479 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1482 MSVCRT_wchar_t
*modeW
= NULL
;
1484 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1486 ret
= MSVCRT__wfdopen(fd
, modeW
);
1492 /*********************************************************************
1493 * _wfdopen (MSVCRT.@)
1495 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1497 int open_flags
, stream_flags
;
1500 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1503 if (!(file
= msvcrt_alloc_fp()))
1505 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1510 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1516 /*********************************************************************
1517 * _filelength (MSVCRT.@)
1519 LONG CDECL
MSVCRT__filelength(int fd
)
1521 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1524 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1527 if (endPos
!= curPos
)
1528 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1535 /*********************************************************************
1536 * _filelengthi64 (MSVCRT.@)
1538 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1540 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1543 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1546 if (endPos
!= curPos
)
1547 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1554 /*********************************************************************
1555 * _fileno (MSVCRT.@)
1557 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1559 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1563 /*********************************************************************
1564 * _fstat64 (MSVCRT.@)
1566 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1570 BY_HANDLE_FILE_INFORMATION hfi
;
1571 HANDLE hand
= msvcrt_fdtoh(fd
);
1573 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1574 if (hand
== INVALID_HANDLE_VALUE
)
1579 WARN(":failed-NULL buf\n");
1580 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1584 memset(&hfi
, 0, sizeof(hfi
));
1585 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1586 type
= GetFileType(hand
);
1587 if (type
== FILE_TYPE_PIPE
)
1589 buf
->st_dev
= buf
->st_rdev
= fd
;
1590 buf
->st_mode
= S_IFIFO
;
1593 else if (type
== FILE_TYPE_CHAR
)
1595 buf
->st_dev
= buf
->st_rdev
= fd
;
1596 buf
->st_mode
= S_IFCHR
;
1599 else /* FILE_TYPE_DISK etc. */
1601 if (!GetFileInformationByHandle(hand
, &hfi
))
1603 WARN(":failed-last error (%d)\n",GetLastError());
1604 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1607 buf
->st_mode
= S_IFREG
| 0444;
1608 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1609 buf
->st_mode
|= 0222;
1610 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1611 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1613 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1614 buf
->st_mtime
= buf
->st_ctime
= dw
;
1615 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1617 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1622 /*********************************************************************
1623 * _fstati64 (MSVCRT.@)
1625 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1628 struct MSVCRT__stat64 buf64
;
1630 ret
= MSVCRT__fstat64(fd
, &buf64
);
1632 msvcrt_stat64_to_stati64(&buf64
, buf
);
1636 /*********************************************************************
1639 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1641 struct MSVCRT__stat64 buf64
;
1643 ret
= MSVCRT__fstat64(fd
, &buf64
);
1645 msvcrt_stat64_to_stat(&buf64
, buf
);
1649 /*********************************************************************
1650 * _fstat32 (MSVCR80.@)
1652 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1655 struct MSVCRT__stat64 buf64
;
1657 ret
= MSVCRT__fstat64(fd
, &buf64
);
1659 msvcrt_stat64_to_stat32(&buf64
, buf
);
1663 /*********************************************************************
1664 * _fstat64i32 (MSVCR80.@)
1666 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1669 struct MSVCRT__stat64 buf64
;
1671 ret
= MSVCRT__fstat64(fd
, &buf64
);
1673 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1677 /*********************************************************************
1678 * _futime64 (MSVCRT.@)
1680 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1682 HANDLE hand
= msvcrt_fdtoh(fd
);
1687 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1692 time_to_filetime( t
->actime
, &at
);
1693 time_to_filetime( t
->modtime
, &wt
);
1696 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1698 msvcrt_set_errno(GetLastError());
1704 /*********************************************************************
1705 * _futime32 (MSVCRT.@)
1707 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1711 struct MSVCRT___utimbuf64 t64
;
1712 t64
.actime
= t
->actime
;
1713 t64
.modtime
= t
->modtime
;
1714 return _futime64( fd
, &t64
);
1717 return _futime64( fd
, NULL
);
1720 /*********************************************************************
1721 * _futime (MSVCRT.@)
1724 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1726 return _futime64( fd
, t
);
1729 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1731 return _futime32( fd
, t
);
1735 /*********************************************************************
1736 * _get_osfhandle (MSVCRT.@)
1738 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1740 HANDLE hand
= msvcrt_fdtoh(fd
);
1741 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1743 return (MSVCRT_intptr_t
)hand
;
1746 /*********************************************************************
1747 * _mktemp_s (MSVCRT.@)
1749 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1753 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1754 return MSVCRT_EINVAL
;
1756 for(len
=0; len
<size
; len
++)
1759 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1762 return MSVCRT_EINVAL
;
1765 for(xno
=1; xno
<=6; xno
++)
1766 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1767 return MSVCRT_EINVAL
;
1769 id
= GetCurrentProcessId();
1770 for(xno
=1; xno
<6; xno
++) {
1771 pattern
[len
-xno
] = id
%10 + '0';
1775 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1776 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1781 *MSVCRT__errno() = MSVCRT_EEXIST
;
1782 return MSVCRT_EEXIST
;
1785 /*********************************************************************
1786 * _mktemp (MSVCRT.@)
1788 char * CDECL
MSVCRT__mktemp(char *pattern
)
1791 char *retVal
= pattern
;
1799 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1803 id
= GetCurrentProcessId();
1807 int tempNum
= id
/ 10;
1808 *pattern
-- = id
- (tempNum
* 10) + '0';
1814 *pattern
= letter
++;
1815 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1817 } while(letter
<= 'z');
1821 /*********************************************************************
1822 * _wmktemp_s (MSVCRT.@)
1824 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1828 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1829 return MSVCRT_EINVAL
;
1831 for(len
=0; len
<size
; len
++)
1834 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1837 return MSVCRT_EINVAL
;
1840 for(xno
=1; xno
<=6; xno
++)
1841 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1842 return MSVCRT_EINVAL
;
1844 id
= GetCurrentProcessId();
1845 for(xno
=1; xno
<6; xno
++) {
1846 pattern
[len
-xno
] = id
%10 + '0';
1850 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1851 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1856 *MSVCRT__errno() = MSVCRT_EEXIST
;
1857 return MSVCRT_EEXIST
;
1860 /*********************************************************************
1861 * _wmktemp (MSVCRT.@)
1863 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1866 MSVCRT_wchar_t
*retVal
= pattern
;
1868 MSVCRT_wchar_t letter
= 'a';
1874 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1878 id
= GetCurrentProcessId();
1882 int tempNum
= id
/ 10;
1883 *pattern
-- = id
- (tempNum
* 10) + '0';
1889 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1891 *pattern
= letter
++;
1892 } while(letter
!= '|');
1896 static unsigned split_oflags(unsigned oflags
)
1899 unsigned unsupp
; /* until we support everything */
1901 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1902 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1903 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1904 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1905 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1906 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1907 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1908 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1909 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1911 if ((unsupp
= oflags
& ~(
1912 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1913 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1914 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1915 MSVCRT__O_NOINHERIT
|
1916 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
1917 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
1919 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1924 /*********************************************************************
1927 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1930 SECURITY_ATTRIBUTES sa
;
1931 HANDLE readHandle
, writeHandle
;
1935 *MSVCRT__errno() = MSVCRT_EINVAL
;
1939 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1940 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1941 sa
.lpSecurityDescriptor
= NULL
;
1942 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1944 unsigned int wxflags
= split_oflags(textmode
);
1948 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
1952 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
1960 MSVCRT__close(pfds
[0]);
1961 CloseHandle(writeHandle
);
1962 *MSVCRT__errno() = MSVCRT_EMFILE
;
1967 CloseHandle(readHandle
);
1968 CloseHandle(writeHandle
);
1969 *MSVCRT__errno() = MSVCRT_EMFILE
;
1974 msvcrt_set_errno(GetLastError());
1979 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1981 char bom
[sizeof(utf8_bom
)];
1984 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
1986 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1989 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1990 oflags
|= MSVCRT__O_U8TEXT
;
1991 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1993 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1994 oflags
|= MSVCRT__O_U16TEXT
;
1996 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2002 /*********************************************************************
2003 * _wsopen_s (MSVCRT.@)
2005 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2007 DWORD access
= 0, creation
= 0, attrib
;
2008 SECURITY_ATTRIBUTES sa
;
2013 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2014 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2016 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2019 wxflag
= split_oflags(oflags
);
2020 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2022 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2023 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2024 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2027 if (oflags
& MSVCRT__O_CREAT
)
2029 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2030 FIXME(": pmode 0x%04x ignored\n", pmode
);
2032 WARN(": pmode 0x%04x ignored\n", pmode
);
2034 if (oflags
& MSVCRT__O_EXCL
)
2035 creation
= CREATE_NEW
;
2036 else if (oflags
& MSVCRT__O_TRUNC
)
2037 creation
= CREATE_ALWAYS
;
2039 creation
= OPEN_ALWAYS
;
2041 else /* no MSVCRT__O_CREAT */
2043 if (oflags
& MSVCRT__O_TRUNC
)
2044 creation
= TRUNCATE_EXISTING
;
2046 creation
= OPEN_EXISTING
;
2051 case MSVCRT__SH_DENYRW
:
2054 case MSVCRT__SH_DENYWR
:
2055 sharing
= FILE_SHARE_READ
;
2057 case MSVCRT__SH_DENYRD
:
2058 sharing
= FILE_SHARE_WRITE
;
2060 case MSVCRT__SH_DENYNO
:
2061 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2064 ERR( "Unhandled shflags 0x%x\n", shflags
);
2065 return MSVCRT_EINVAL
;
2067 attrib
= FILE_ATTRIBUTE_NORMAL
;
2069 if (oflags
& MSVCRT__O_TEMPORARY
)
2071 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2073 sharing
|= FILE_SHARE_DELETE
;
2076 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2077 sa
.lpSecurityDescriptor
= NULL
;
2078 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2080 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2081 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2082 && !(access
&GENERIC_READ
))
2084 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2085 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2086 if (hand
!= INVALID_HANDLE_VALUE
)
2088 oflags
= check_bom(hand
, oflags
, FALSE
);
2092 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2095 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2096 if (hand
== INVALID_HANDLE_VALUE
) {
2097 WARN(":failed-last error (%d)\n",GetLastError());
2098 msvcrt_set_errno(GetLastError());
2099 return *MSVCRT__errno();
2102 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2104 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2105 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2106 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2108 if (oflags
& MSVCRT__O_U8TEXT
)
2110 DWORD written
= 0, tmp
;
2112 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2113 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2115 if (written
!= sizeof(utf8_bom
)) {
2116 WARN("error writing BOM\n");
2118 msvcrt_set_errno(GetLastError());
2119 return *MSVCRT__errno();
2124 DWORD written
= 0, tmp
;
2126 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2127 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2129 if (written
!= sizeof(utf16_bom
))
2131 WARN("error writing BOM\n");
2133 msvcrt_set_errno(GetLastError());
2134 return *MSVCRT__errno();
2138 else if (access
& GENERIC_READ
)
2139 oflags
= check_bom(hand
, oflags
, TRUE
);
2142 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2144 return *MSVCRT__errno();
2146 if (oflags
& MSVCRT__O_WTEXT
)
2147 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2148 else if (oflags
& MSVCRT__O_U16TEXT
)
2149 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
2150 else if (oflags
& MSVCRT__O_U8TEXT
)
2151 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
2153 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2157 /*********************************************************************
2158 * _wsopen (MSVCRT.@)
2160 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2165 if (oflags
& MSVCRT__O_CREAT
)
2169 __ms_va_start(ap
, shflags
);
2170 pmode
= va_arg(ap
, int);
2176 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2180 /*********************************************************************
2181 * _sopen_s (MSVCRT.@)
2183 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2185 MSVCRT_wchar_t
*pathW
;
2188 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2189 return MSVCRT_EINVAL
;
2191 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2192 return MSVCRT_EINVAL
;
2194 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2199 /*********************************************************************
2202 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2207 if (oflags
& MSVCRT__O_CREAT
)
2211 __ms_va_start(ap
, shflags
);
2212 pmode
= va_arg(ap
, int);
2218 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2222 /*********************************************************************
2225 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2229 if (flags
& MSVCRT__O_CREAT
)
2232 __ms_va_start(ap
, flags
);
2233 pmode
= va_arg(ap
, int);
2235 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2238 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2241 /*********************************************************************
2244 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2248 if (flags
& MSVCRT__O_CREAT
)
2251 __ms_va_start(ap
, flags
);
2252 pmode
= va_arg(ap
, int);
2254 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2257 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2260 /*********************************************************************
2263 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2265 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2266 return MSVCRT__open(path
, usedFlags
);
2269 /*********************************************************************
2270 * _wcreat (MSVCRT.@)
2272 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2274 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2275 return MSVCRT__wopen(path
, usedFlags
);
2278 /*********************************************************************
2279 * _open_osfhandle (MSVCRT.@)
2281 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2286 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2287 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2288 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2289 * text - it never sets MSVCRT__O_BINARY.
2291 /* don't let split_oflags() decide the mode if no mode is passed */
2292 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2293 oflags
|= MSVCRT__O_BINARY
;
2295 flags
= GetFileType((HANDLE
)handle
);
2296 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2298 msvcrt_set_errno(GetLastError());
2302 if (flags
== FILE_TYPE_CHAR
)
2304 else if (flags
== FILE_TYPE_PIPE
)
2308 flags
|= split_oflags(oflags
);
2310 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2311 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2315 /*********************************************************************
2318 int CDECL
MSVCRT__rmtmp(void)
2320 int num_removed
= 0, i
;
2324 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2325 file
= msvcrt_get_file(i
);
2327 if (file
->_tmpfname
)
2329 MSVCRT_fclose(file
);
2336 TRACE(":removed (%d) temp files\n",num_removed
);
2340 static inline int get_utf8_char_len(char ch
)
2342 if((ch
&0xf8) == 0xf0)
2344 else if((ch
&0xf0) == 0xe0)
2346 else if((ch
&0xe0) == 0xc0)
2351 /*********************************************************************
2352 * (internal) read_utf8
2354 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2356 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2357 HANDLE hand
= fdinfo
->handle
;
2358 char min_buf
[4], *readbuf
, lookahead
;
2359 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2361 /* make the buffer big enough to hold at least one character */
2362 /* read bytes have to fit to output and lookahead buffers */
2364 readbuf_size
= count
< 4 ? 4 : count
;
2365 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2370 if(fdinfo
->lookahead
[0] != '\n') {
2371 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2372 fdinfo
->lookahead
[0] = '\n';
2374 if(fdinfo
->lookahead
[1] != '\n') {
2375 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2376 fdinfo
->lookahead
[1] = '\n';
2378 if(fdinfo
->lookahead
[2] != '\n') {
2379 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2380 fdinfo
->lookahead
[2] = '\n';
2385 /* NOTE: this case is broken in native dll, reading
2386 * sometimes fails when small buffer is passed
2389 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2390 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2391 fdinfo
->wxflag
|= WX_ATEOF
;
2394 msvcrt_set_errno(GetLastError());
2397 }else if(!num_read
) {
2398 fdinfo
->wxflag
|= WX_ATEOF
;
2404 char_len
= get_utf8_char_len(readbuf
[0]);
2406 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2410 if(readbuf
[0] == '\n')
2411 fdinfo
->wxflag
|= WX_READNL
;
2413 fdinfo
->wxflag
&= ~WX_READNL
;
2415 if(readbuf
[0] == 0x1a) {
2416 fdinfo
->wxflag
|= WX_ATEOF
;
2420 if(readbuf
[0] == '\r') {
2421 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2423 else if(lookahead
== '\n')
2427 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2428 fdinfo
->lookahead
[0] = lookahead
;
2430 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2435 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2436 msvcrt_set_errno(GetLastError());
2443 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2446 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2447 fdinfo
->wxflag
|= WX_ATEOF
;
2448 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2451 msvcrt_set_errno(GetLastError());
2452 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2455 }else if(!pos
&& !num_read
) {
2456 fdinfo
->wxflag
|= WX_ATEOF
;
2457 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2462 if(readbuf
[0] == '\n')
2463 fdinfo
->wxflag
|= WX_READNL
;
2465 fdinfo
->wxflag
&= ~WX_READNL
;
2467 /* Find first byte of last character (may be incomplete) */
2468 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2469 if((readbuf
[i
]&0xc0) != 0x80)
2471 char_len
= get_utf8_char_len(readbuf
[i
]);
2472 if(char_len
+i
<= pos
)
2475 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
)) {
2477 fdinfo
->lookahead
[0] = readbuf
[i
];
2479 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2481 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2483 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2487 for(i
=0, j
=0; i
<pos
; i
++) {
2488 if(readbuf
[i
] == 0x1a) {
2489 fdinfo
->wxflag
|= WX_ATEOF
;
2493 /* strip '\r' if followed by '\n' */
2494 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2495 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2496 readbuf
[j
++] = '\r';
2497 }else if(lookahead
== '\n' && j
==0) {
2498 readbuf
[j
++] = '\n';
2500 if(lookahead
!= '\n')
2501 readbuf
[j
++] = '\r';
2503 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2504 fdinfo
->lookahead
[0] = lookahead
;
2506 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2508 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2509 readbuf
[j
++] = readbuf
[i
];
2514 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2515 msvcrt_set_errno(GetLastError());
2516 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2520 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2524 /*********************************************************************
2527 * When reading \r as last character in text mode, read() positions
2528 * the file pointer on the \r character while getc() goes on to
2531 static int read_i(int fd
, void *buf
, unsigned int count
)
2533 DWORD num_read
, utf16
;
2534 char *bufstart
= buf
;
2535 HANDLE hand
= msvcrt_fdtoh(fd
);
2536 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2541 if (fdinfo
->wxflag
& WX_ATEOF
) {
2542 TRACE("already at EOF, returning 0\n");
2545 /* Don't trace small reads, it gets *very* annoying */
2547 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2548 if (hand
== INVALID_HANDLE_VALUE
)
2550 *MSVCRT__errno() = MSVCRT_EBADF
;
2554 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2555 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2557 *MSVCRT__errno() = MSVCRT_EINVAL
;
2561 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2562 return read_utf8(fd
, buf
, count
);
2564 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2566 if (fdinfo
->lookahead
[0] != '\n')
2568 bufstart
[0] = fdinfo
->lookahead
[0];
2569 fdinfo
->lookahead
[0] = '\n';
2573 bufstart
[1] = fdinfo
->lookahead
[1];
2574 fdinfo
->lookahead
[1] = '\n';
2577 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2578 num_read
+= 1+utf16
;
2583 if(utf16
&& (num_read
&1))
2585 /* msvcr90 uses uninitialized value from the buffer in this case */
2586 /* msvcrt ignores additional data */
2587 ERR("got odd number of bytes in UTF16 mode\n");
2591 if (count
!= 0 && num_read
== 0)
2593 fdinfo
->wxflag
|= WX_ATEOF
;
2594 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2596 else if (fdinfo
->wxflag
& WX_TEXT
)
2600 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2601 fdinfo
->wxflag
|= WX_READNL
;
2603 fdinfo
->wxflag
&= ~WX_READNL
;
2605 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2607 /* in text mode, a ctrl-z signals EOF */
2608 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2610 fdinfo
->wxflag
|= WX_ATEOF
;
2611 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2615 /* in text mode, strip \r if followed by \n */
2616 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2621 lookahead
[1] = '\n';
2622 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2624 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2626 bufstart
[j
++] = '\n';
2627 if(utf16
) bufstart
[j
++] = 0;
2631 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2633 bufstart
[j
++] = '\r';
2634 if(utf16
) bufstart
[j
++] = 0;
2637 if (fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2639 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2641 bufstart
[j
++] = '\n';
2642 if (utf16
) bufstart
[j
++] = 0;
2646 fdinfo
->lookahead
[0] = lookahead
[0];
2647 fdinfo
->lookahead
[1] = lookahead
[1];
2651 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2656 bufstart
[j
++] = '\r';
2657 if(utf16
) bufstart
[j
++] = 0;
2660 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2661 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2663 bufstart
[j
++] = bufstart
[i
];
2664 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2672 if (GetLastError() == ERROR_BROKEN_PIPE
)
2674 TRACE(":end-of-pipe\n");
2675 fdinfo
->wxflag
|= WX_ATEOF
;
2680 TRACE(":failed-last error (%d)\n",GetLastError());
2686 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2690 /*********************************************************************
2693 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2696 num_read
= read_i(fd
, buf
, count
);
2700 /*********************************************************************
2701 * _setmode (MSVCRT.@)
2703 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2705 int ret
= msvcrt_get_ioinfo(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2706 if(ret
==MSVCRT__O_TEXT
&& (msvcrt_get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2707 ret
= MSVCRT__O_WTEXT
;
2709 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2710 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2711 *MSVCRT__errno() = MSVCRT_EINVAL
;
2715 if(mode
== MSVCRT__O_BINARY
) {
2716 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2717 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2721 msvcrt_get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2722 if(mode
== MSVCRT__O_TEXT
)
2723 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2724 else if(mode
== MSVCRT__O_U8TEXT
)
2725 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2727 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2732 /*********************************************************************
2733 * _stat64 (MSVCRT.@)
2735 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2738 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2739 unsigned short mode
= ALL_S_IREAD
;
2742 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2744 plen
= strlen(path
);
2745 while (plen
&& path
[plen
-1]==' ')
2748 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2749 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2751 *MSVCRT__errno() = MSVCRT_ENOENT
;
2755 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2757 TRACE("failed (%d)\n",GetLastError());
2758 *MSVCRT__errno() = MSVCRT_ENOENT
;
2762 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2764 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2765 Bon 011120: This FIXME seems incorrect
2766 Also a letter as first char isn't enough to be classified
2769 if (isalpha(*path
)&& (*(path
+1)==':'))
2770 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2772 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2774 /* Dir, or regular file? */
2775 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2776 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2779 mode
|= MSVCRT__S_IFREG
;
2781 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2783 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2784 (tolower(path
[plen
-3]) << 16);
2785 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2786 mode
|= ALL_S_IEXEC
;
2790 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2791 mode
|= ALL_S_IWRITE
;
2793 buf
->st_mode
= mode
;
2795 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2796 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2798 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2799 buf
->st_mtime
= buf
->st_ctime
= dw
;
2800 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2801 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2802 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2806 /*********************************************************************
2807 * _stati64 (MSVCRT.@)
2809 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2812 struct MSVCRT__stat64 buf64
;
2814 ret
= MSVCRT_stat64(path
, &buf64
);
2816 msvcrt_stat64_to_stati64(&buf64
, buf
);
2820 /*********************************************************************
2823 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2826 struct MSVCRT__stat64 buf64
;
2828 ret
= MSVCRT_stat64( path
, &buf64
);
2830 msvcrt_stat64_to_stat(&buf64
, buf
);
2834 /*********************************************************************
2835 * _stat32 (MSVCR100.@)
2837 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2840 struct MSVCRT__stat64 buf64
;
2842 ret
= MSVCRT_stat64(path
, &buf64
);
2844 msvcrt_stat64_to_stat32(&buf64
, buf
);
2848 /*********************************************************************
2849 * _stat32i64 (MSVCR100.@)
2851 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2854 struct MSVCRT__stat64 buf64
;
2856 ret
= MSVCRT_stat64(path
, &buf64
);
2858 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2862 /*********************************************************************
2863 * _stat64i32 (MSVCR100.@)
2865 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2868 struct MSVCRT__stat64 buf64
;
2870 ret
= MSVCRT_stat64(path
, &buf64
);
2872 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2876 /*********************************************************************
2877 * _wstat64 (MSVCRT.@)
2879 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2882 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2883 unsigned short mode
= ALL_S_IREAD
;
2886 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2888 plen
= strlenW(path
);
2889 while (plen
&& path
[plen
-1]==' ')
2892 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2893 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2895 *MSVCRT__errno() = MSVCRT_ENOENT
;
2899 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2901 TRACE("failed (%d)\n",GetLastError());
2902 *MSVCRT__errno() = MSVCRT_ENOENT
;
2906 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2908 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2909 if (MSVCRT_iswalpha(*path
))
2910 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2912 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2914 /* Dir, or regular file? */
2915 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2916 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2919 mode
|= MSVCRT__S_IFREG
;
2921 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2923 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2924 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2925 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2926 mode
|= ALL_S_IEXEC
;
2930 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2931 mode
|= ALL_S_IWRITE
;
2933 buf
->st_mode
= mode
;
2935 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2936 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2938 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2939 buf
->st_mtime
= buf
->st_ctime
= dw
;
2940 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2941 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2942 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2946 /*********************************************************************
2947 * _wstati64 (MSVCRT.@)
2949 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2952 struct MSVCRT__stat64 buf64
;
2954 ret
= MSVCRT__wstat64(path
, &buf64
);
2956 msvcrt_stat64_to_stati64(&buf64
, buf
);
2960 /*********************************************************************
2963 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2966 struct MSVCRT__stat64 buf64
;
2968 ret
= MSVCRT__wstat64( path
, &buf64
);
2969 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2973 /*********************************************************************
2974 * _wstat32 (MSVCR100.@)
2976 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
2979 struct MSVCRT__stat64 buf64
;
2981 ret
= MSVCRT__wstat64(path
, &buf64
);
2983 msvcrt_stat64_to_stat32(&buf64
, buf
);
2987 /*********************************************************************
2988 * _wstat32i64 (MSVCR100.@)
2990 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
2993 struct MSVCRT__stat64 buf64
;
2995 ret
= MSVCRT__wstat64(path
, &buf64
);
2997 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3001 /*********************************************************************
3002 * _wstat64i32 (MSVCR100.@)
3004 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3007 struct MSVCRT__stat64 buf64
;
3009 ret
= MSVCRT__wstat64(path
, &buf64
);
3011 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3015 /*********************************************************************
3018 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3020 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3023 /*********************************************************************
3024 * _telli64 (MSVCRT.@)
3026 __int64 CDECL
_telli64(int fd
)
3028 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3031 /*********************************************************************
3032 * _tempnam (MSVCRT.@)
3034 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3036 char tmpbuf
[MAX_PATH
];
3037 const char *tmp_dir
= MSVCRT_getenv("TMP");
3039 if (tmp_dir
) dir
= tmp_dir
;
3041 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3042 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3044 TRACE("got name (%s)\n",tmpbuf
);
3045 DeleteFileA(tmpbuf
);
3046 return MSVCRT__strdup(tmpbuf
);
3048 TRACE("failed (%d)\n",GetLastError());
3052 /*********************************************************************
3053 * _wtempnam (MSVCRT.@)
3055 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3057 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3058 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3059 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3061 if (tmp_dir
) dir
= tmp_dir
;
3063 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3064 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3066 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3067 DeleteFileW(tmpbuf
);
3068 return MSVCRT__wcsdup(tmpbuf
);
3070 TRACE("failed (%d)\n",GetLastError());
3074 /*********************************************************************
3077 int CDECL
MSVCRT__umask(int umask
)
3079 int old_umask
= MSVCRT_umask
;
3080 TRACE("(%d)\n",umask
);
3081 MSVCRT_umask
= umask
;
3085 /*********************************************************************
3086 * _utime64 (MSVCRT.@)
3088 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3090 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3094 int retVal
= _futime64(fd
, t
);
3101 /*********************************************************************
3102 * _utime32 (MSVCRT.@)
3104 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3108 struct MSVCRT___utimbuf64 t64
;
3109 t64
.actime
= t
->actime
;
3110 t64
.modtime
= t
->modtime
;
3111 return _utime64( path
, &t64
);
3114 return _utime64( path
, NULL
);
3117 /*********************************************************************
3121 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3123 return _utime64( path
, t
);
3126 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3128 return _utime32( path
, t
);
3132 /*********************************************************************
3133 * _wutime64 (MSVCRT.@)
3135 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3137 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3141 int retVal
= _futime64(fd
, t
);
3148 /*********************************************************************
3149 * _wutime32 (MSVCRT.@)
3151 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3155 struct MSVCRT___utimbuf64 t64
;
3156 t64
.actime
= t
->actime
;
3157 t64
.modtime
= t
->modtime
;
3158 return _wutime64( path
, &t64
);
3161 return _wutime64( path
, NULL
);
3164 /*********************************************************************
3165 * _wutime (MSVCRT.@)
3168 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3170 return _wutime64( path
, t
);
3173 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3175 return _wutime32( path
, t
);
3179 /*********************************************************************
3182 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3185 ioinfo
*info
= msvcrt_get_ioinfo(fd
);
3186 HANDLE hand
= info
->handle
;
3188 /* Don't trace small writes, it gets *very* annoying */
3191 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3193 if (hand
== INVALID_HANDLE_VALUE
)
3195 *MSVCRT__errno() = MSVCRT_EBADF
;
3199 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3201 *MSVCRT__errno() = MSVCRT_EINVAL
;
3205 /* If appending, go to EOF */
3206 if (info
->wxflag
& WX_APPEND
)
3207 MSVCRT__lseek(fd
, 0, FILE_END
);
3209 if (!(info
->wxflag
& WX_TEXT
))
3211 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3212 && (num_written
== count
))
3214 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3215 hand
, GetLastError());
3216 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3220 unsigned int i
, j
, nr_lf
, size
;
3223 const char *s
= buf
, *buf_start
= buf
;
3225 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3227 /* find number of \n */
3228 for (nr_lf
=0, i
=0; i
<count
; i
++)
3234 if ((q
= p
= MSVCRT_malloc(size
)))
3236 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3245 FIXME("Malloc failed\n");
3257 else if (info
->exflag
& EF_UTF16
)
3259 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3260 if (s
[i
]=='\n' && s
[i
+1]==0)
3265 if ((q
= p
= MSVCRT_malloc(size
)))
3267 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3269 if (s
[i
]=='\n' && s
[i
+1]==0)
3280 FIXME("Malloc failed\n");
3296 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3297 if (s
[i
]=='\n' && s
[i
+1]==0)
3300 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3302 msvcrt_set_errno(GetLastError());
3307 size
= conv_len
+nr_lf
;
3308 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3310 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3312 if (s
[i
]=='\n' && s
[i
+1]==0)
3320 q
= p
+count
+nr_lf
*2;
3321 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3322 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3326 FIXME("Malloc failed\n");
3333 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3337 if (num_written
!= size
)
3339 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3340 fd
, hand
, GetLastError(), num_written
);
3341 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3342 return s
- buf_start
;
3350 /*********************************************************************
3353 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3357 MSVCRT__lock_file(file
);
3358 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3359 if (len
== sizeof(val
)) {
3360 MSVCRT__unlock_file(file
);
3364 file
->_flag
|= MSVCRT__IOERR
;
3365 MSVCRT__unlock_file(file
);
3369 /*********************************************************************
3372 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3376 MSVCRT__lock_file(file
);
3378 MSVCRT_free(file
->_tmpfname
);
3379 file
->_tmpfname
= NULL
;
3380 /* flush stdio buffers */
3381 if(file
->_flag
& MSVCRT__IOWRT
)
3382 MSVCRT_fflush(file
);
3383 if(file
->_flag
& MSVCRT__IOMYBUF
)
3384 MSVCRT_free(file
->_base
);
3386 r
=MSVCRT__close(file
->_file
);
3389 MSVCRT__unlock_file(file
);
3391 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3394 /*********************************************************************
3397 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3399 return file
->_flag
& MSVCRT__IOEOF
;
3402 /*********************************************************************
3405 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3407 return file
->_flag
& MSVCRT__IOERR
;
3410 /*********************************************************************
3411 * _filbuf (MSVCRT.@)
3413 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3416 MSVCRT__lock_file(file
);
3418 if(file
->_flag
& MSVCRT__IOSTRG
) {
3419 MSVCRT__unlock_file(file
);
3423 /* Allocate buffer if needed */
3424 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3425 msvcrt_alloc_buffer(file
);
3427 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3428 if(file
->_flag
& MSVCRT__IORW
)
3429 file
->_flag
|= MSVCRT__IOREAD
;
3431 MSVCRT__unlock_file(file
);
3436 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3438 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3439 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3440 MSVCRT__unlock_file(file
);
3444 MSVCRT__unlock_file(file
);
3447 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3449 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3451 MSVCRT__unlock_file(file
);
3456 file
->_ptr
= file
->_base
+1;
3457 c
= *(unsigned char *)file
->_base
;
3458 MSVCRT__unlock_file(file
);
3463 /*********************************************************************
3466 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3471 MSVCRT__lock_file(file
);
3474 i
= (unsigned char *)file
->_ptr
++;
3477 j
= MSVCRT__filbuf(file
);
3479 MSVCRT__unlock_file(file
);
3483 /*********************************************************************
3484 * _fgetchar (MSVCRT.@)
3486 int CDECL
MSVCRT__fgetchar(void)
3488 return MSVCRT_fgetc(MSVCRT_stdin
);
3491 /*********************************************************************
3494 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3496 int cc
= MSVCRT_EOF
;
3497 char * buf_start
= s
;
3499 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3500 file
,file
->_file
,s
,size
);
3502 MSVCRT__lock_file(file
);
3504 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3509 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3511 TRACE(":nothing read\n");
3512 MSVCRT__unlock_file(file
);
3515 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3518 TRACE(":got %s\n", debugstr_a(buf_start
));
3519 MSVCRT__unlock_file(file
);
3523 /*********************************************************************
3526 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3531 MSVCRT__lock_file(file
);
3533 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3534 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3537 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3538 ch
= MSVCRT_fgetc(file
);
3539 if(ch
== MSVCRT_EOF
) {
3546 char mbs
[MSVCRT_MB_LEN_MAX
];
3549 ch
= MSVCRT_fgetc(file
);
3550 if(ch
!= MSVCRT_EOF
) {
3552 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3553 ch
= MSVCRT_fgetc(file
);
3554 if(ch
!= MSVCRT_EOF
) {
3563 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3567 MSVCRT__unlock_file(file
);
3571 /*********************************************************************
3574 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3581 MSVCRT__lock_file(file
);
3582 for (j
=0; j
<sizeof(int); j
++) {
3583 k
= MSVCRT_fgetc(file
);
3584 if (k
== MSVCRT_EOF
) {
3585 file
->_flag
|= MSVCRT__IOEOF
;
3586 MSVCRT__unlock_file(file
);
3592 MSVCRT__unlock_file(file
);
3596 /*********************************************************************
3599 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3601 return MSVCRT_fgetwc(file
);
3604 /*********************************************************************
3605 * _fgetwchar (MSVCRT.@)
3607 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3609 return MSVCRT_fgetwc(MSVCRT_stdin
);
3612 /*********************************************************************
3613 * getwchar (MSVCRT.@)
3615 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3617 return MSVCRT__fgetwchar();
3620 /*********************************************************************
3623 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3625 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3626 MSVCRT_wchar_t
* buf_start
= s
;
3628 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3629 file
,file
->_file
,s
,size
);
3631 MSVCRT__lock_file(file
);
3633 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3638 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3640 TRACE(":nothing read\n");
3641 MSVCRT__unlock_file(file
);
3644 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3647 TRACE(":got %s\n", debugstr_w(buf_start
));
3648 MSVCRT__unlock_file(file
);
3652 /*********************************************************************
3653 * _flsbuf (MSVCRT.@)
3655 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3657 /* Flush output buffer */
3658 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3659 msvcrt_alloc_buffer(file
);
3661 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3662 if(file
->_flag
& MSVCRT__IORW
)
3663 file
->_flag
|= MSVCRT__IOWRT
;
3667 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3670 if(file
->_cnt
<= 0) {
3671 res
= msvcrt_flush_buffer(file
);
3674 file
->_cnt
=file
->_bufsiz
;
3682 /* set _cnt to 0 for unbuffered FILEs */
3684 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3687 file
->_flag
|= MSVCRT__IOERR
;
3692 /*********************************************************************
3695 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3697 MSVCRT_size_t wrcnt
=size
* nmemb
;
3702 MSVCRT__lock_file(file
);
3705 if(file
->_cnt
< 0) {
3706 WARN("negative file->_cnt value in %p\n", file
);
3707 file
->_flag
|= MSVCRT__IOERR
;
3709 } else if(file
->_cnt
) {
3710 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3711 memcpy(file
->_ptr
, ptr
, pcnt
);
3716 ptr
= (const char*)ptr
+ pcnt
;
3717 } else if((file
->_flag
& MSVCRT__IONBF
)
3718 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3719 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3723 if(file
->_flag
& MSVCRT__IONBF
)
3725 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3726 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3728 bufsiz
= file
->_bufsiz
;
3730 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3732 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3735 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3736 file
->_flag
|= MSVCRT__IOERR
;
3741 ptr
= (const char*)ptr
+ pcnt
;
3743 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3747 ptr
= (const char*)ptr
+ 1;
3751 MSVCRT__unlock_file(file
);
3752 return written
/ size
;
3755 /*********************************************************************
3758 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3760 MSVCRT_wchar_t mwc
=wc
;
3764 MSVCRT__lock_file(file
);
3765 fdinfo
= msvcrt_get_ioinfo(file
->_file
);
3767 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3768 char buf
[MSVCRT_MB_LEN_MAX
];
3771 char_len
= MSVCRT_wctomb(buf
, mwc
);
3772 if(char_len
!=-1 && MSVCRT_fwrite(buf
, char_len
, 1, file
)==1)
3776 }else if(MSVCRT_fwrite(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3782 MSVCRT__unlock_file(file
);
3786 /*********************************************************************
3787 * _fputwchar (MSVCRT.@)
3789 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3791 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3794 /*********************************************************************
3795 * _wfsopen (MSVCRT.@)
3797 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3800 int open_flags
, stream_flags
, fd
;
3802 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3804 /* map mode string to open() flags. "man fopen" for possibilities. */
3805 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3809 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3812 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3814 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3821 TRACE(":got (%p)\n",file
);
3822 if (fd
>= 0 && !file
)
3828 /*********************************************************************
3829 * _fsopen (MSVCRT.@)
3831 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3834 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3836 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3837 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3838 *MSVCRT__errno() = MSVCRT_EINVAL
;
3841 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3844 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3845 *MSVCRT__errno() = MSVCRT_EINVAL
;
3849 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
3856 /*********************************************************************
3859 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
3861 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3864 /*********************************************************************
3865 * fopen_s (MSVCRT.@)
3867 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
3868 const char *filename
, const char *mode
)
3870 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3871 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3872 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3874 *pFile
= MSVCRT_fopen(filename
, mode
);
3877 return *MSVCRT__errno();
3881 /*********************************************************************
3882 * _wfopen (MSVCRT.@)
3884 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
3886 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3889 /*********************************************************************
3890 * _wfopen_s (MSVCRT.@)
3892 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
3893 const MSVCRT_wchar_t
*mode
)
3895 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3896 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3897 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3899 *pFile
= MSVCRT__wfopen(filename
, mode
);
3902 return *MSVCRT__errno();
3906 /*********************************************************************
3909 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
3913 MSVCRT__lock_file(file
);
3919 res
= msvcrt_flush_buffer(file
);
3920 MSVCRT__unlock_file(file
);
3921 return res
? res
: c
;
3924 MSVCRT__unlock_file(file
);
3928 res
= MSVCRT__flsbuf(c
, file
);
3929 MSVCRT__unlock_file(file
);
3934 /*********************************************************************
3935 * _fputchar (MSVCRT.@)
3937 int CDECL
MSVCRT__fputchar(int c
)
3939 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3942 /*********************************************************************
3945 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3947 MSVCRT_size_t rcnt
=size
* nmemb
;
3948 MSVCRT_size_t read
=0;
3949 MSVCRT_size_t pread
=0;
3954 MSVCRT__lock_file(file
);
3956 /* first buffered data */
3958 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3959 memcpy(ptr
, file
->_ptr
, pcnt
);
3964 ptr
= (char*)ptr
+ pcnt
;
3965 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3966 if(file
->_flag
& MSVCRT__IORW
) {
3967 file
->_flag
|= MSVCRT__IOREAD
;
3969 MSVCRT__unlock_file(file
);
3974 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3975 msvcrt_alloc_buffer(file
);
3980 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3981 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3982 file
->_ptr
= file
->_base
;
3983 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3984 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3985 if (i
> 0 && i
< file
->_cnt
) {
3986 msvcrt_get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3987 file
->_flag
&= ~MSVCRT__IOEOF
;
3990 memcpy(ptr
, file
->_ptr
, i
);
3994 } else if (rcnt
> INT_MAX
) {
3995 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
3996 } else if (rcnt
< MSVCRT_BUFSIZ
) {
3997 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
3999 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4003 ptr
= (char *)ptr
+i
;
4004 /* expose feof condition in the flags
4005 * MFC tests file->_flag for feof, and doesn't call feof())
4007 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
4008 file
->_flag
|= MSVCRT__IOEOF
;
4011 file
->_flag
|= MSVCRT__IOERR
;
4018 MSVCRT__unlock_file(file
);
4023 /*********************************************************************
4024 * fread_s (MSVCR80.@)
4026 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4027 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4029 size_t bytes_left
, buf_pos
;
4031 TRACE("(%p %lu %lu %lu %p\n", buf
, buf_size
, elem_size
, count
, stream
);
4033 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4035 memset(buf
, 0, buf_size
);
4038 if(!elem_size
|| !count
) return 0;
4039 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4040 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4042 bytes_left
= elem_size
*count
;
4045 if(stream
->_cnt
> 0) {
4046 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4048 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4049 memset(buf
, 0, buf_size
);
4053 MSVCRT_fread((char*)buf
+buf_pos
, 1, size
, stream
);
4057 int c
= MSVCRT__filbuf(stream
);
4062 if(!MSVCRT_CHECK_PMT_ERR(buf_size
-buf_pos
> 0, MSVCRT_ERANGE
)) {
4063 memset(buf
, 0, buf_size
);
4067 ((char*)buf
)[buf_pos
++] = c
;
4072 return buf_pos
/elem_size
;
4075 /*********************************************************************
4076 * _wfreopen (MSVCRT.@)
4079 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4081 int open_flags
, stream_flags
, fd
;
4083 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4086 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4090 MSVCRT_fclose(file
);
4091 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4093 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4095 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4105 /*********************************************************************
4106 * _wfreopen_s (MSVCRT.@)
4108 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4109 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4111 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4112 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4113 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4114 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4116 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4119 return *MSVCRT__errno();
4123 /*********************************************************************
4124 * freopen (MSVCRT.@)
4127 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4130 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4132 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4133 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4139 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4146 /*********************************************************************
4147 * freopen_s (MSVCRT.@)
4149 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4150 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4152 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4153 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4154 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4155 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4157 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4160 return *MSVCRT__errno();
4164 /*********************************************************************
4165 * fsetpos (MSVCRT.@)
4167 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4171 MSVCRT__lock_file(file
);
4172 /* Note that all this has been lifted 'as is' from fseek */
4173 if(file
->_flag
& MSVCRT__IOWRT
)
4174 msvcrt_flush_buffer(file
);
4176 /* Discard buffered input */
4178 file
->_ptr
= file
->_base
;
4180 /* Reset direction of i/o */
4181 if(file
->_flag
& MSVCRT__IORW
) {
4182 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4185 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4186 MSVCRT__unlock_file(file
);
4190 /*********************************************************************
4191 * _ftelli64 (MSVCRT.@)
4193 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4197 MSVCRT__lock_file(file
);
4198 pos
= _telli64(file
->_file
);
4200 MSVCRT__unlock_file(file
);
4203 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4204 if(file
->_flag
& MSVCRT__IOWRT
) {
4205 pos
+= file
->_ptr
- file
->_base
;
4207 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4210 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4214 } else if(!file
->_cnt
) { /* nothing to do */
4215 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4219 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4220 for(i
=0; i
<file
->_cnt
; i
++)
4221 if(file
->_ptr
[i
] == '\n')
4227 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
4228 MSVCRT__unlock_file(file
);
4232 pos
-= file
->_bufsiz
;
4233 pos
+= file
->_ptr
- file
->_base
;
4235 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4236 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
4239 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4246 MSVCRT__unlock_file(file
);
4250 /*********************************************************************
4253 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4255 return MSVCRT__ftelli64(file
);
4258 /*********************************************************************
4259 * fgetpos (MSVCRT.@)
4261 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4263 *pos
= MSVCRT__ftelli64(file
);
4269 /*********************************************************************
4272 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4274 MSVCRT_size_t len
= strlen(s
);
4277 MSVCRT__lock_file(file
);
4278 ret
= MSVCRT_fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4279 MSVCRT__unlock_file(file
);
4283 /*********************************************************************
4286 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4288 MSVCRT_size_t i
, len
= strlenW(s
);
4292 MSVCRT__lock_file(file
);
4293 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4294 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4295 MSVCRT__unlock_file(file
);
4299 tmp_buf
= add_std_buffer(file
);
4300 for (i
=0; i
<len
; i
++) {
4301 if(MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
) {
4302 if(tmp_buf
) remove_std_buffer(file
);
4303 MSVCRT__unlock_file(file
);
4308 if(tmp_buf
) remove_std_buffer(file
);
4309 MSVCRT__unlock_file(file
);
4313 /*********************************************************************
4314 * getchar (MSVCRT.@)
4316 int CDECL
MSVCRT_getchar(void)
4318 return MSVCRT_fgetc(MSVCRT_stdin
);
4321 /*********************************************************************
4324 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4326 return MSVCRT_fgetc(file
);
4329 /*********************************************************************
4332 char * CDECL
MSVCRT_gets(char *buf
)
4335 char * buf_start
= buf
;
4337 MSVCRT__lock_file(MSVCRT_stdin
);
4338 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4339 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
4340 if(cc
!= '\r') *buf
++ = (char)cc
;
4344 TRACE("got '%s'\n", buf_start
);
4345 MSVCRT__unlock_file(MSVCRT_stdin
);
4349 /*********************************************************************
4352 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4355 MSVCRT_wchar_t
* ws
= buf
;
4357 MSVCRT__lock_file(MSVCRT_stdin
);
4358 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4359 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
4362 *buf
++ = (MSVCRT_wchar_t
)cc
;
4366 TRACE("got %s\n", debugstr_w(ws
));
4367 MSVCRT__unlock_file(MSVCRT_stdin
);
4371 /*********************************************************************
4374 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4376 return MSVCRT_fputc(c
, file
);
4379 /*********************************************************************
4380 * putchar (MSVCRT.@)
4382 int CDECL
MSVCRT_putchar(int c
)
4384 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4387 /*********************************************************************
4388 * _putwch (MSVCRT.@)
4390 int CDECL
MSVCRT__putwch(int c
)
4392 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4395 /*********************************************************************
4398 int CDECL
MSVCRT_puts(const char *s
)
4400 MSVCRT_size_t len
= strlen(s
);
4403 MSVCRT__lock_file(MSVCRT_stdout
);
4404 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4405 MSVCRT__unlock_file(MSVCRT_stdout
);
4409 ret
= MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4410 MSVCRT__unlock_file(MSVCRT_stdout
);
4414 /*********************************************************************
4417 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4419 static const MSVCRT_wchar_t nl
= '\n';
4420 MSVCRT_size_t len
= strlenW(s
);
4423 MSVCRT__lock_file(MSVCRT_stdout
);
4424 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4425 MSVCRT__unlock_file(MSVCRT_stdout
);
4429 ret
= MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4430 MSVCRT__unlock_file(MSVCRT_stdout
);
4434 /*********************************************************************
4437 int CDECL
MSVCRT_remove(const char *path
)
4439 TRACE("(%s)\n",path
);
4440 if (DeleteFileA(path
))
4442 TRACE(":failed (%d)\n",GetLastError());
4443 msvcrt_set_errno(GetLastError());
4447 /*********************************************************************
4448 * _wremove (MSVCRT.@)
4450 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4452 TRACE("(%s)\n",debugstr_w(path
));
4453 if (DeleteFileW(path
))
4455 TRACE(":failed (%d)\n",GetLastError());
4456 msvcrt_set_errno(GetLastError());
4460 /*********************************************************************
4463 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4465 TRACE(":from %s to %s\n",oldpath
,newpath
);
4466 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4468 TRACE(":failed (%d)\n",GetLastError());
4469 msvcrt_set_errno(GetLastError());
4473 /*********************************************************************
4474 * _wrename (MSVCRT.@)
4476 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4478 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4479 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4481 TRACE(":failed (%d)\n",GetLastError());
4482 msvcrt_set_errno(GetLastError());
4486 /*********************************************************************
4487 * setvbuf (MSVCRT.@)
4489 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4491 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4492 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4493 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4495 MSVCRT__lock_file(file
);
4497 MSVCRT_fflush(file
);
4498 if(file
->_flag
& MSVCRT__IOMYBUF
)
4499 MSVCRT_free(file
->_base
);
4500 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4503 if(mode
== MSVCRT__IONBF
) {
4504 file
->_flag
|= MSVCRT__IONBF
;
4505 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4508 file
->_base
= file
->_ptr
= buf
;
4509 file
->_flag
|= MSVCRT__USERBUF
;
4510 file
->_bufsiz
= size
;
4512 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4515 MSVCRT__unlock_file(file
);
4519 file
->_flag
|= MSVCRT__IOMYBUF
;
4520 file
->_bufsiz
= size
;
4522 MSVCRT__unlock_file(file
);
4526 /*********************************************************************
4529 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4531 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4534 /*********************************************************************
4537 char * CDECL
MSVCRT_tmpnam(char *s
)
4544 thread_data_t
*data
= msvcrt_get_thread_data();
4546 if(!data
->tmpnam_buffer
)
4547 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4549 s
= data
->tmpnam_buffer
;
4552 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4553 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4554 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4556 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4557 memcpy(p
, tmpstr
, size
);
4559 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4560 GetLastError() == ERROR_FILE_NOT_FOUND
)
4566 /*********************************************************************
4567 * _wtmpnam (MSVCRT.@)
4569 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4571 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4572 MSVCRT_wchar_t tmpstr
[16];
4576 thread_data_t
*data
= msvcrt_get_thread_data();
4578 if(!data
->wtmpnam_buffer
)
4579 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4581 s
= data
->wtmpnam_buffer
;
4584 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4585 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4586 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4588 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4589 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4591 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4592 GetLastError() == ERROR_FILE_NOT_FOUND
)
4598 /*********************************************************************
4599 * tmpfile (MSVCRT.@)
4601 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4603 char *filename
= MSVCRT_tmpnam(NULL
);
4605 MSVCRT_FILE
* file
= NULL
;
4608 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4609 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4610 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4612 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4617 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4620 if(fd
!= -1 && !file
)
4626 /*********************************************************************
4627 * tmpfile_s (MSVCRT.@)
4629 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4631 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4633 *file
= MSVCRT_tmpfile();
4637 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4639 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4642 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4646 MSVCRT__lock_file(file
);
4648 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4649 ret
= MSVCRT_fwrite(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4650 MSVCRT__unlock_file(file
);
4654 for(i
=0; i
<len
; i
++) {
4655 if(MSVCRT_fputwc(str
[i
], file
) == MSVCRT_WEOF
) {
4656 MSVCRT__unlock_file(file
);
4661 MSVCRT__unlock_file(file
);
4665 /*********************************************************************
4666 * vfprintf (MSVCRT.@)
4668 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4673 MSVCRT__lock_file(file
);
4674 tmp_buf
= add_std_buffer(file
);
4675 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4676 if(tmp_buf
) remove_std_buffer(file
);
4677 MSVCRT__unlock_file(file
);
4682 /*********************************************************************
4683 * vfprintf_s (MSVCRT.@)
4685 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4690 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4692 MSVCRT__lock_file(file
);
4693 tmp_buf
= add_std_buffer(file
);
4694 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4695 if(tmp_buf
) remove_std_buffer(file
);
4696 MSVCRT__unlock_file(file
);
4701 /*********************************************************************
4702 * vfwprintf (MSVCRT.@)
4704 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4709 MSVCRT__lock_file(file
);
4710 tmp_buf
= add_std_buffer(file
);
4711 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4712 if(tmp_buf
) remove_std_buffer(file
);
4713 MSVCRT__unlock_file(file
);
4718 /*********************************************************************
4719 * vfwprintf_s (MSVCRT.@)
4721 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4726 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4728 MSVCRT__lock_file(file
);
4729 tmp_buf
= add_std_buffer(file
);
4730 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4731 if(tmp_buf
) remove_std_buffer(file
);
4732 MSVCRT__unlock_file(file
);
4737 /*********************************************************************
4738 * _vfwprintf_l (MSVCRT.@)
4740 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4741 MSVCRT__locale_t locale
, __ms_va_list valist
)
4746 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4748 MSVCRT__lock_file(file
);
4749 tmp_buf
= add_std_buffer(file
);
4750 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4751 if(tmp_buf
) remove_std_buffer(file
);
4752 MSVCRT__unlock_file(file
);
4757 /*********************************************************************
4758 * vprintf (MSVCRT.@)
4760 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4762 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4765 /*********************************************************************
4766 * vprintf_s (MSVCRT.@)
4768 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4770 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4773 /*********************************************************************
4774 * vwprintf (MSVCRT.@)
4776 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4778 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4781 /*********************************************************************
4782 * vwprintf_s (MSVCRT.@)
4784 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4786 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4789 /*********************************************************************
4790 * fprintf (MSVCRT.@)
4792 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4794 __ms_va_list valist
;
4796 __ms_va_start(valist
, format
);
4797 res
= MSVCRT_vfprintf(file
, format
, valist
);
4798 __ms_va_end(valist
);
4802 /*********************************************************************
4803 * fprintf_s (MSVCRT.@)
4805 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
4807 __ms_va_list valist
;
4809 __ms_va_start(valist
, format
);
4810 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
4811 __ms_va_end(valist
);
4815 /*********************************************************************
4816 * fwprintf (MSVCRT.@)
4818 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4820 __ms_va_list valist
;
4822 __ms_va_start(valist
, format
);
4823 res
= MSVCRT_vfwprintf(file
, format
, valist
);
4824 __ms_va_end(valist
);
4828 /*********************************************************************
4829 * fwprintf_s (MSVCRT.@)
4831 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4833 __ms_va_list valist
;
4835 __ms_va_start(valist
, format
);
4836 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
4837 __ms_va_end(valist
);
4841 /*********************************************************************
4842 * _fwprintf_l (MSVCRT.@)
4844 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
4846 __ms_va_list valist
;
4848 __ms_va_start(valist
, locale
);
4849 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
4850 __ms_va_end(valist
);
4854 /*********************************************************************
4857 int CDECL
MSVCRT_printf(const char *format
, ...)
4859 __ms_va_list valist
;
4861 __ms_va_start(valist
, format
);
4862 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
4863 __ms_va_end(valist
);
4867 /*********************************************************************
4868 * printf_s (MSVCRT.@)
4870 int CDECL
MSVCRT_printf_s(const char *format
, ...)
4872 __ms_va_list valist
;
4874 __ms_va_start(valist
, format
);
4875 res
= MSVCRT_vprintf_s(format
, valist
);
4876 __ms_va_end(valist
);
4880 /*********************************************************************
4883 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
4885 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
4887 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
4888 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
4891 MSVCRT__lock_file(file
);
4892 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
4893 && msvcrt_alloc_buffer(file
))
4894 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
4897 if(file
->_ptr
>file
->_base
) {
4899 if(file
->_flag
& MSVCRT__IOSTRG
) {
4900 if(*file
->_ptr
!= c
) {
4902 MSVCRT__unlock_file(file
);
4909 MSVCRT_clearerr(file
);
4910 file
->_flag
|= MSVCRT__IOREAD
;
4911 MSVCRT__unlock_file(file
);
4915 MSVCRT__unlock_file(file
);
4919 /*********************************************************************
4920 * ungetwc (MSVCRT.@)
4922 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4924 MSVCRT_wchar_t mwc
= wc
;
4926 if (wc
== MSVCRT_WEOF
)
4929 MSVCRT__lock_file(file
);
4931 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
4932 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4933 unsigned char * pp
= (unsigned char *)&mwc
;
4936 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
4937 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
)) {
4938 MSVCRT__unlock_file(file
);
4943 char mbs
[MSVCRT_MB_LEN_MAX
];
4946 len
= MSVCRT_wctomb(mbs
, mwc
);
4948 MSVCRT__unlock_file(file
);
4952 for(len
--; len
>=0; len
--) {
4953 if(mbs
[len
] != MSVCRT_ungetc(mbs
[len
], file
)) {
4954 MSVCRT__unlock_file(file
);
4960 MSVCRT__unlock_file(file
);
4964 /*********************************************************************
4965 * wprintf (MSVCRT.@)
4967 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
4969 __ms_va_list valist
;
4971 __ms_va_start(valist
, format
);
4972 res
= MSVCRT_vwprintf(format
, valist
);
4973 __ms_va_end(valist
);
4977 /*********************************************************************
4978 * wprintf_s (MSVCRT.@)
4980 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
4982 __ms_va_list valist
;
4984 __ms_va_start(valist
, format
);
4985 res
= MSVCRT_vwprintf_s(format
, valist
);
4986 __ms_va_end(valist
);
4990 /*********************************************************************
4991 * _getmaxstdio (MSVCRT.@)
4993 int CDECL
MSVCRT__getmaxstdio(void)
4995 return MSVCRT_max_streams
;
4998 /*********************************************************************
4999 * _setmaxstdio (MSVCRT.@)
5001 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5003 TRACE("%d\n", newmax
);
5005 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5008 MSVCRT_max_streams
= newmax
;
5009 return MSVCRT_max_streams
;