2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READNL 0x04 /* read started with \n */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
71 /* values for exflag - it's used differently in msvcr90.dll*/
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom
[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
92 CRITICAL_SECTION crit
;
98 BOOL utf8translations
;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
117 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
118 static int MSVCRT_fdend
= 3; /* highest allocated fd */
122 CRITICAL_SECTION crit
;
125 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
126 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
127 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
129 /* INTERNAL: process umask */
130 static int MSVCRT_umask
= 0;
132 /* INTERNAL: static data for tmpnam and _wtmpname functions */
133 static int tmpnam_unique
;
135 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
136 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
137 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
138 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
140 #define TOUL(x) (ULONGLONG)(x)
141 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
142 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
143 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
144 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
146 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
147 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
148 * and MSVCRT_stream_idx, from race conditions.
149 * It doesn't protect against race conditions manipulating the underlying files
150 * or flags; doing so would probably be better accomplished with per-file
151 * protection, rather than locking the whole table for every change.
153 static CRITICAL_SECTION MSVCRT_file_cs
;
154 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
156 0, 0, &MSVCRT_file_cs
,
157 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
158 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
160 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
161 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
162 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
164 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
166 buf
->st_dev
= buf64
->st_dev
;
167 buf
->st_ino
= buf64
->st_ino
;
168 buf
->st_mode
= buf64
->st_mode
;
169 buf
->st_nlink
= buf64
->st_nlink
;
170 buf
->st_uid
= buf64
->st_uid
;
171 buf
->st_gid
= buf64
->st_gid
;
172 buf
->st_rdev
= buf64
->st_rdev
;
173 buf
->st_size
= buf64
->st_size
;
174 buf
->st_atime
= buf64
->st_atime
;
175 buf
->st_mtime
= buf64
->st_mtime
;
176 buf
->st_ctime
= buf64
->st_ctime
;
179 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
181 buf
->st_dev
= buf64
->st_dev
;
182 buf
->st_ino
= buf64
->st_ino
;
183 buf
->st_mode
= buf64
->st_mode
;
184 buf
->st_nlink
= buf64
->st_nlink
;
185 buf
->st_uid
= buf64
->st_uid
;
186 buf
->st_gid
= buf64
->st_gid
;
187 buf
->st_rdev
= buf64
->st_rdev
;
188 buf
->st_size
= buf64
->st_size
;
189 buf
->st_atime
= buf64
->st_atime
;
190 buf
->st_mtime
= buf64
->st_mtime
;
191 buf
->st_ctime
= buf64
->st_ctime
;
194 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
196 buf
->st_dev
= buf64
->st_dev
;
197 buf
->st_ino
= buf64
->st_ino
;
198 buf
->st_mode
= buf64
->st_mode
;
199 buf
->st_nlink
= buf64
->st_nlink
;
200 buf
->st_uid
= buf64
->st_uid
;
201 buf
->st_gid
= buf64
->st_gid
;
202 buf
->st_rdev
= buf64
->st_rdev
;
203 buf
->st_size
= buf64
->st_size
;
204 buf
->st_atime
= buf64
->st_atime
;
205 buf
->st_mtime
= buf64
->st_mtime
;
206 buf
->st_ctime
= buf64
->st_ctime
;
209 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
211 buf
->st_dev
= buf64
->st_dev
;
212 buf
->st_ino
= buf64
->st_ino
;
213 buf
->st_mode
= buf64
->st_mode
;
214 buf
->st_nlink
= buf64
->st_nlink
;
215 buf
->st_uid
= buf64
->st_uid
;
216 buf
->st_gid
= buf64
->st_gid
;
217 buf
->st_rdev
= buf64
->st_rdev
;
218 buf
->st_size
= buf64
->st_size
;
219 buf
->st_atime
= buf64
->st_atime
;
220 buf
->st_mtime
= buf64
->st_mtime
;
221 buf
->st_ctime
= buf64
->st_ctime
;
224 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
226 buf
->st_dev
= buf64
->st_dev
;
227 buf
->st_ino
= buf64
->st_ino
;
228 buf
->st_mode
= buf64
->st_mode
;
229 buf
->st_nlink
= buf64
->st_nlink
;
230 buf
->st_uid
= buf64
->st_uid
;
231 buf
->st_gid
= buf64
->st_gid
;
232 buf
->st_rdev
= buf64
->st_rdev
;
233 buf
->st_size
= buf64
->st_size
;
234 buf
->st_atime
= buf64
->st_atime
;
235 buf
->st_mtime
= buf64
->st_mtime
;
236 buf
->st_ctime
= buf64
->st_ctime
;
239 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
241 /* 1601 to 1970 is 369 years plus 89 leap days */
242 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
244 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
245 ft
->dwHighDateTime
= ticks
>> 32;
246 ft
->dwLowDateTime
= ticks
;
249 static inline ioinfo
* msvcrt_get_ioinfo(int fd
)
252 if(fd
< MSVCRT_MAX_FILES
)
253 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
255 return &MSVCRT___badioinfo
;
257 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
260 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
264 if(i
>= MSVCRT_max_streams
)
268 return &MSVCRT__iob
[i
];
270 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
272 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
273 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
274 ERR("out of memory\n");
275 *MSVCRT__errno() = MSVCRT_ENOMEM
;
279 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
281 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
286 static inline BOOL
msvcrt_is_valid_fd(int fd
)
288 return fd
>= 0 && fd
< MSVCRT_fdend
&& (msvcrt_get_ioinfo(fd
)->wxflag
& WX_OPEN
);
291 /* INTERNAL: Get the HANDLE for a fd
292 * This doesn't lock the table, because a failure will result in
293 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
294 * it returns a valid handle which is about to be closed, a subsequent call
295 * will fail, most likely in a sane way.
297 static HANDLE
msvcrt_fdtoh(int fd
)
299 if (!msvcrt_is_valid_fd(fd
))
301 WARN(":fd (%d) - no handle!\n",fd
);
302 *MSVCRT___doserrno() = 0;
303 *MSVCRT__errno() = MSVCRT_EBADF
;
304 return INVALID_HANDLE_VALUE
;
306 if (msvcrt_get_ioinfo(fd
)->handle
== INVALID_HANDLE_VALUE
)
307 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd
);
308 return msvcrt_get_ioinfo(fd
)->handle
;
311 /* INTERNAL: free a file entry fd */
312 static void msvcrt_free_fd(int fd
)
317 fdinfo
= msvcrt_get_ioinfo(fd
);
318 if(fdinfo
!= &MSVCRT___badioinfo
)
320 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
323 TRACE(":fd (%d) freed\n",fd
);
330 SetStdHandle(STD_INPUT_HANDLE
, 0);
333 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
336 SetStdHandle(STD_ERROR_HANDLE
, 0);
341 if (fd
== MSVCRT_fdend
- 1)
343 if (fd
< MSVCRT_fdstart
)
348 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
349 /* caller must hold the files lock */
350 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
354 if (fd
>= MSVCRT_MAX_FILES
)
356 WARN(":files exhausted!\n");
357 *MSVCRT__errno() = MSVCRT_ENFILE
;
361 fdinfo
= msvcrt_get_ioinfo(fd
);
362 if(fdinfo
== &MSVCRT___badioinfo
) {
365 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
366 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
367 WARN(":out of memory!\n");
368 *MSVCRT__errno() = MSVCRT_ENOMEM
;
372 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
373 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
375 fdinfo
= msvcrt_get_ioinfo(fd
);
378 fdinfo
->handle
= hand
;
379 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
380 fdinfo
->lookahead
[0] = '\n';
381 fdinfo
->lookahead
[1] = '\n';
382 fdinfo
->lookahead
[2] = '\n';
385 /* locate next free slot */
386 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
387 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
389 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
390 msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
392 /* update last fd in use */
393 if (fd
>= MSVCRT_fdend
)
394 MSVCRT_fdend
= fd
+ 1;
395 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
399 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
400 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
401 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
407 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
408 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
413 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
414 ret
= msvcrt_set_fd(hand
, flag
, MSVCRT_fdstart
);
419 /* INTERNAL: Allocate a FILE* for an fd slot */
420 /* caller must hold the files lock */
421 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
426 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
428 file
= msvcrt_get_file(i
);
432 if (file
->_flag
== 0)
434 if (i
== MSVCRT_stream_idx
)
436 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
438 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
439 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
450 /* INTERNAL: initialize a FILE* from an open fd */
451 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
453 TRACE(":fd (%d) allocating FILE*\n",fd
);
454 if (!msvcrt_is_valid_fd(fd
))
456 WARN(":invalid fd %d\n",fd
);
457 *MSVCRT___doserrno() = 0;
458 *MSVCRT__errno() = MSVCRT_EBADF
;
461 file
->_ptr
= file
->_base
= NULL
;
464 file
->_flag
= stream_flags
;
465 file
->_tmpfname
= NULL
;
467 TRACE(":got FILE* (%p)\n",file
);
471 /* INTERNAL: Create an inheritance data block (for spawned process)
472 * The inheritance block is made of:
473 * 00 int nb of file descriptor (NBFD)
474 * 04 char file flags (wxflag): repeated for each fd
475 * 4+NBFD HANDLE file handle: repeated for each fd
477 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
484 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
485 *block
= MSVCRT_calloc(*size
, 1);
491 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
492 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
494 *(unsigned*)*block
= MSVCRT_fdend
;
495 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
497 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
498 fdinfo
= msvcrt_get_ioinfo(fd
);
499 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
501 *wxflag_ptr
= fdinfo
->wxflag
;
502 *handle_ptr
= fdinfo
->handle
;
507 *handle_ptr
= INVALID_HANDLE_VALUE
;
509 wxflag_ptr
++; handle_ptr
++;
514 /* INTERNAL: Set up all file descriptors,
515 * as well as default streams (stdin, stderr and stdout)
517 void msvcrt_init_io(void)
523 GetStartupInfoA(&si
);
524 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
530 count
= *(unsigned*)si
.lpReserved2
;
531 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
532 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
534 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
535 count
= min(count
, MSVCRT_MAX_FILES
);
536 for (i
= 0; i
< count
; i
++)
538 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
539 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
541 wxflag_ptr
++; handle_ptr
++;
543 MSVCRT_fdend
= max( 3, count
);
544 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
545 if (msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
548 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO
);
549 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
550 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
551 DWORD type
= GetFileType(h
);
553 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
554 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDIN_FILENO
);
557 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO
);
558 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
559 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
560 DWORD type
= GetFileType(h
);
562 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
563 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDOUT_FILENO
);
566 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO
);
567 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
568 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
569 DWORD type
= GetFileType(h
);
571 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
572 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDERR_FILENO
);
575 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO
)->handle
,
576 msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO
)->handle
,
577 msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO
)->handle
);
579 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
580 for (i
= 0; i
< 3; i
++)
582 /* FILE structs for stdin/out/err are static and never deleted */
583 MSVCRT__iob
[i
]._file
= i
;
584 MSVCRT__iob
[i
]._tmpfname
= NULL
;
585 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
587 MSVCRT_stream_idx
= 3;
590 /* INTERNAL: Flush stdio file buffer */
591 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
593 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
594 int cnt
=file
->_ptr
-file
->_base
;
595 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
596 file
->_flag
|= MSVCRT__IOERR
;
599 file
->_ptr
=file
->_base
;
605 /*********************************************************************
608 int CDECL
MSVCRT__isatty(int fd
)
610 TRACE(":fd (%d)\n",fd
);
612 return msvcrt_get_ioinfo(fd
)->wxflag
& WX_TTY
;
615 /* INTERNAL: Allocate stdio file buffer */
616 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
618 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
619 && MSVCRT__isatty(file
->_file
))
622 file
->_base
= MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
624 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
625 file
->_flag
|= MSVCRT__IOMYBUF
;
627 file
->_base
= (char*)(&file
->_charbuf
);
629 file
->_flag
|= MSVCRT__IONBF
;
631 file
->_ptr
= file
->_base
;
636 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
637 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
639 static char buffers
[2][MSVCRT_BUFSIZ
];
641 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
642 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
643 || !MSVCRT__isatty(file
->_file
))
646 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
647 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
648 file
->_flag
|= MSVCRT__USERBUF
;
652 /* INTERNAL: Removes temporary buffer from stdout or stderr */
653 /* Only call this function when add_std_buffer returned TRUE */
654 static void remove_std_buffer(MSVCRT_FILE
*file
)
656 msvcrt_flush_buffer(file
);
657 file
->_ptr
= file
->_base
= NULL
;
658 file
->_bufsiz
= file
->_cnt
= 0;
659 file
->_flag
&= ~MSVCRT__USERBUF
;
662 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
663 static int msvcrt_int_to_base32(int num
, char *str
)
678 *p
= (num
& 31) + '0';
680 *p
+= ('a' - '0' - 10);
687 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
688 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
703 *p
= (num
& 31) + '0';
705 *p
+= ('a' - '0' - 10);
712 /*********************************************************************
713 * __iob_func(MSVCRT.@)
715 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
717 return &MSVCRT__iob
[0];
720 /*********************************************************************
723 int CDECL
MSVCRT__access(const char *filename
, int mode
)
725 DWORD attr
= GetFileAttributesA(filename
);
727 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
729 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
731 msvcrt_set_errno(GetLastError());
734 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
736 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
742 /*********************************************************************
743 * _access_s (MSVCRT.@)
745 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
747 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
748 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
750 if (MSVCRT__access(filename
, mode
) == -1)
751 return *MSVCRT__errno();
755 /*********************************************************************
756 * _waccess (MSVCRT.@)
758 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
760 DWORD attr
= GetFileAttributesW(filename
);
762 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
764 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
766 msvcrt_set_errno(GetLastError());
769 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
771 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
777 /*********************************************************************
778 * _waccess_s (MSVCRT.@)
780 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
782 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
783 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
785 if (MSVCRT__waccess(filename
, mode
) == -1)
786 return *MSVCRT__errno();
790 /*********************************************************************
793 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
795 DWORD oldFlags
= GetFileAttributesA(path
);
797 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
799 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
800 oldFlags
| FILE_ATTRIBUTE_READONLY
;
802 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
805 msvcrt_set_errno(GetLastError());
809 /*********************************************************************
812 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
814 DWORD oldFlags
= GetFileAttributesW(path
);
816 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
818 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
819 oldFlags
| FILE_ATTRIBUTE_READONLY
;
821 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
824 msvcrt_set_errno(GetLastError());
828 /*********************************************************************
831 int CDECL
MSVCRT__unlink(const char *path
)
833 TRACE("%s\n",debugstr_a(path
));
834 if(DeleteFileA(path
))
836 TRACE("failed (%d)\n",GetLastError());
837 msvcrt_set_errno(GetLastError());
841 /*********************************************************************
842 * _wunlink (MSVCRT.@)
844 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
846 TRACE("(%s)\n",debugstr_w(path
));
847 if(DeleteFileW(path
))
849 TRACE("failed (%d)\n",GetLastError());
850 msvcrt_set_errno(GetLastError());
854 /*********************************************************************
857 int CDECL
MSVCRT__commit(int fd
)
859 HANDLE hand
= msvcrt_fdtoh(fd
);
861 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
862 if (hand
== INVALID_HANDLE_VALUE
)
865 if (!FlushFileBuffers(hand
))
867 if (GetLastError() == ERROR_INVALID_HANDLE
)
869 /* FlushFileBuffers fails for console handles
870 * so we ignore this error.
874 TRACE(":failed-last error (%d)\n",GetLastError());
875 msvcrt_set_errno(GetLastError());
882 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
883 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
885 /* INTERNAL: Flush all stream buffer */
886 static int msvcrt_flush_all_buffers(int mask
)
888 int i
, num_flushed
= 0;
892 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
893 file
= msvcrt_get_file(i
);
897 if(file
->_flag
& mask
) {
905 TRACE(":flushed (%d) handles\n",num_flushed
);
909 /*********************************************************************
910 * _flushall (MSVCRT.@)
912 int CDECL
MSVCRT__flushall(void)
914 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
917 /*********************************************************************
920 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
923 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
924 } else if(file
->_flag
& MSVCRT__IOWRT
) {
927 MSVCRT__lock_file(file
);
928 res
= msvcrt_flush_buffer(file
);
930 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
931 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
932 MSVCRT__unlock_file(file
);
935 } else if(file
->_flag
& MSVCRT__IOREAD
) {
936 MSVCRT__lock_file(file
);
938 file
->_ptr
= file
->_base
;
939 MSVCRT__unlock_file(file
);
946 /*********************************************************************
949 int CDECL
MSVCRT__close(int fd
)
955 hand
= msvcrt_fdtoh(fd
);
956 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
957 if (!msvcrt_is_valid_fd(fd
)) {
961 ret
= CloseHandle(hand
) ? 0 : -1;
963 WARN(":failed-last error (%d)\n",GetLastError());
964 msvcrt_set_errno(GetLastError());
972 /*********************************************************************
975 * MSDN isn't clear on this point, but the remarks for _pipe
976 * indicate file descriptors duplicated with _dup and _dup2 are always
979 int CDECL
MSVCRT__dup2(int od
, int nd
)
983 TRACE("(od=%d, nd=%d)\n", od
, nd
);
985 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
989 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od
)->handle
,
990 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
992 int wxflag
= msvcrt_get_ioinfo(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
994 if (msvcrt_is_valid_fd(nd
))
996 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
1000 *MSVCRT__errno() = MSVCRT_EMFILE
;
1004 /* _dup2 returns 0, not nd, on success */
1011 msvcrt_set_errno(GetLastError());
1016 *MSVCRT__errno() = MSVCRT_EBADF
;
1023 /*********************************************************************
1026 int CDECL
MSVCRT__dup(int od
)
1031 fd
= MSVCRT_fdstart
;
1032 if (MSVCRT__dup2(od
, fd
) == 0)
1040 /*********************************************************************
1043 int CDECL
MSVCRT__eof(int fd
)
1045 DWORD curpos
,endpos
;
1046 LONG hcurpos
,hendpos
;
1047 HANDLE hand
= msvcrt_fdtoh(fd
);
1049 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1051 if (hand
== INVALID_HANDLE_VALUE
)
1054 if (msvcrt_get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1056 /* Otherwise we do it the hard way */
1057 hcurpos
= hendpos
= 0;
1058 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1059 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1061 if (curpos
== endpos
&& hcurpos
== hendpos
)
1063 /* FIXME: shouldn't WX_ATEOF be set here? */
1067 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1071 /*********************************************************************
1072 * _fcloseall (MSVCRT.@)
1074 int CDECL
MSVCRT__fcloseall(void)
1076 int num_closed
= 0, i
;
1080 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1081 file
= msvcrt_get_file(i
);
1083 if (file
->_flag
&& !MSVCRT_fclose(file
))
1088 TRACE(":closed (%d) handles\n",num_closed
);
1092 /* free everything on process exit */
1093 void msvcrt_free_io(void)
1099 MSVCRT__fcloseall();
1101 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1102 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1104 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1106 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1107 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1109 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1110 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1114 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1115 MSVCRT_free(MSVCRT_fstream
[i
]);
1117 DeleteCriticalSection(&MSVCRT_file_cs
);
1120 /*********************************************************************
1121 * _lseeki64 (MSVCRT.@)
1123 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1125 HANDLE hand
= msvcrt_fdtoh(fd
);
1128 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1129 if (hand
== INVALID_HANDLE_VALUE
)
1132 if (whence
< 0 || whence
> 2)
1134 *MSVCRT__errno() = MSVCRT_EINVAL
;
1138 TRACE(":fd (%d) to %s pos %s\n",
1139 fd
,wine_dbgstr_longlong(offset
),
1140 (whence
==SEEK_SET
)?"SEEK_SET":
1141 (whence
==SEEK_CUR
)?"SEEK_CUR":
1142 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1144 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1145 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1146 ofs
.QuadPart
= offset
;
1147 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1148 GetLastError() == ERROR_SUCCESS
)
1150 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_ATEOF
;
1151 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1153 return ofs
.QuadPart
;
1155 TRACE(":error-last error (%d)\n",GetLastError());
1156 msvcrt_set_errno(GetLastError());
1160 /*********************************************************************
1163 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1165 return MSVCRT__lseeki64(fd
, offset
, whence
);
1168 /*********************************************************************
1169 * _lock_file (MSVCRT.@)
1171 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1173 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1174 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1176 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1179 /*********************************************************************
1180 * _unlock_file (MSVCRT.@)
1182 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1184 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1185 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1187 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1190 /*********************************************************************
1191 * _locking (MSVCRT.@)
1193 * This is untested; the underlying LockFile doesn't work yet.
1195 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1199 HANDLE hand
= msvcrt_fdtoh(fd
);
1201 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1202 if (hand
== INVALID_HANDLE_VALUE
)
1205 if (mode
< 0 || mode
> 4)
1207 *MSVCRT__errno() = MSVCRT_EINVAL
;
1211 TRACE(":fd (%d) by 0x%08x mode %s\n",
1212 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1213 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1214 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1215 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1216 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1219 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1221 FIXME ("Seek failed\n");
1222 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1225 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1228 ret
= 1; /* just to satisfy gcc */
1231 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1236 else if (mode
== MSVCRT__LK_UNLCK
)
1237 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1239 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1240 /* FIXME - what about error settings? */
1241 return ret
? 0 : -1;
1244 /*********************************************************************
1245 * _fseeki64 (MSVCRT.@)
1247 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1251 MSVCRT__lock_file(file
);
1252 /* Flush output if needed */
1253 if(file
->_flag
& MSVCRT__IOWRT
)
1254 msvcrt_flush_buffer(file
);
1256 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1258 offset
+= MSVCRT__ftelli64(file
);
1261 /* Discard buffered input */
1263 file
->_ptr
= file
->_base
;
1264 /* Reset direction of i/o */
1265 if(file
->_flag
& MSVCRT__IORW
) {
1266 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1268 /* Clear end of file flag */
1269 file
->_flag
&= ~MSVCRT__IOEOF
;
1270 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1272 MSVCRT__unlock_file(file
);
1276 /*********************************************************************
1279 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1281 return MSVCRT__fseeki64( file
, offset
, whence
);
1284 /*********************************************************************
1285 * _chsize_s (MSVCRT.@)
1287 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1293 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1295 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1299 handle
= msvcrt_fdtoh(fd
);
1300 if (handle
!= INVALID_HANDLE_VALUE
)
1302 /* save the current file pointer */
1303 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1306 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1309 ret
= SetEndOfFile(handle
);
1310 if (!ret
) msvcrt_set_errno(GetLastError());
1313 /* restore the file pointer */
1314 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1319 return ret
? 0 : *MSVCRT__errno();
1322 /*********************************************************************
1323 * _chsize (MSVCRT.@)
1325 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1327 /* _chsize_s returns errno on failure but _chsize should return -1 */
1328 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1331 /*********************************************************************
1332 * clearerr (MSVCRT.@)
1334 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1336 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1338 MSVCRT__lock_file(file
);
1339 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1340 MSVCRT__unlock_file(file
);
1343 /*********************************************************************
1346 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1348 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1350 MSVCRT__lock_file(file
);
1351 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1352 MSVCRT_clearerr(file
);
1353 MSVCRT__unlock_file(file
);
1356 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1358 int plus
= strchrW(mode
, '+') != NULL
;
1360 TRACE("%s\n", debugstr_w(mode
));
1362 while(*mode
== ' ') mode
++;
1367 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1368 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1371 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1372 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1375 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1376 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1379 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1383 *stream_flags
|= MSVCRT__commode
;
1385 while (*mode
&& *mode
!=',')
1389 *open_flags
|= MSVCRT__O_BINARY
;
1390 *open_flags
&= ~MSVCRT__O_TEXT
;
1393 *open_flags
|= MSVCRT__O_TEXT
;
1394 *open_flags
&= ~MSVCRT__O_BINARY
;
1397 *open_flags
|= MSVCRT__O_TEMPORARY
;
1400 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1403 *stream_flags
|= MSVCRT__IOCOMMIT
;
1406 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1409 *open_flags
|= MSVCRT__O_NOINHERIT
;
1418 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1421 ERR("incorrect mode flag: %c\n", mode
[-1]);
1427 static const WCHAR ccs
[] = {'c','c','s'};
1428 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1429 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1430 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1433 while(*mode
== ' ') mode
++;
1434 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1436 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1437 while(*mode
== ' ') mode
++;
1438 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1441 while(*mode
== ' ') mode
++;
1443 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1445 *open_flags
|= MSVCRT__O_U8TEXT
;
1446 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1448 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1450 *open_flags
|= MSVCRT__O_U16TEXT
;
1451 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1453 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1455 *open_flags
|= MSVCRT__O_WTEXT
;
1456 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1460 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1464 while(*mode
== ' ') mode
++;
1467 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1472 /*********************************************************************
1473 * _fdopen (MSVCRT.@)
1475 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1478 MSVCRT_wchar_t
*modeW
= NULL
;
1480 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1482 ret
= MSVCRT__wfdopen(fd
, modeW
);
1488 /*********************************************************************
1489 * _wfdopen (MSVCRT.@)
1491 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1493 int open_flags
, stream_flags
;
1496 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1499 if (!(file
= msvcrt_alloc_fp()))
1501 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1506 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1512 /*********************************************************************
1513 * _filelength (MSVCRT.@)
1515 LONG CDECL
MSVCRT__filelength(int fd
)
1517 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1520 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1523 if (endPos
!= curPos
)
1524 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1531 /*********************************************************************
1532 * _filelengthi64 (MSVCRT.@)
1534 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1536 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1539 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1542 if (endPos
!= curPos
)
1543 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1550 /*********************************************************************
1551 * _fileno (MSVCRT.@)
1553 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1555 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1559 /*********************************************************************
1560 * _fstat64 (MSVCRT.@)
1562 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1566 BY_HANDLE_FILE_INFORMATION hfi
;
1567 HANDLE hand
= msvcrt_fdtoh(fd
);
1569 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1570 if (hand
== INVALID_HANDLE_VALUE
)
1575 WARN(":failed-NULL buf\n");
1576 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1580 memset(&hfi
, 0, sizeof(hfi
));
1581 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1582 type
= GetFileType(hand
);
1583 if (type
== FILE_TYPE_PIPE
)
1585 buf
->st_dev
= buf
->st_rdev
= fd
;
1586 buf
->st_mode
= S_IFIFO
;
1589 else if (type
== FILE_TYPE_CHAR
)
1591 buf
->st_dev
= buf
->st_rdev
= fd
;
1592 buf
->st_mode
= S_IFCHR
;
1595 else /* FILE_TYPE_DISK etc. */
1597 if (!GetFileInformationByHandle(hand
, &hfi
))
1599 WARN(":failed-last error (%d)\n",GetLastError());
1600 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1603 buf
->st_mode
= S_IFREG
| 0444;
1604 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1605 buf
->st_mode
|= 0222;
1606 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1607 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1609 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1610 buf
->st_mtime
= buf
->st_ctime
= dw
;
1611 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1613 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1618 /*********************************************************************
1619 * _fstati64 (MSVCRT.@)
1621 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1624 struct MSVCRT__stat64 buf64
;
1626 ret
= MSVCRT__fstat64(fd
, &buf64
);
1628 msvcrt_stat64_to_stati64(&buf64
, buf
);
1632 /*********************************************************************
1635 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1637 struct MSVCRT__stat64 buf64
;
1639 ret
= MSVCRT__fstat64(fd
, &buf64
);
1641 msvcrt_stat64_to_stat(&buf64
, buf
);
1645 /*********************************************************************
1646 * _fstat32 (MSVCR80.@)
1648 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1651 struct MSVCRT__stat64 buf64
;
1653 ret
= MSVCRT__fstat64(fd
, &buf64
);
1655 msvcrt_stat64_to_stat32(&buf64
, buf
);
1659 /*********************************************************************
1660 * _fstat64i32 (MSVCR80.@)
1662 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1665 struct MSVCRT__stat64 buf64
;
1667 ret
= MSVCRT__fstat64(fd
, &buf64
);
1669 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1673 /*********************************************************************
1674 * _futime64 (MSVCRT.@)
1676 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1678 HANDLE hand
= msvcrt_fdtoh(fd
);
1683 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1688 time_to_filetime( t
->actime
, &at
);
1689 time_to_filetime( t
->modtime
, &wt
);
1692 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1694 msvcrt_set_errno(GetLastError());
1700 /*********************************************************************
1701 * _futime32 (MSVCRT.@)
1703 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1707 struct MSVCRT___utimbuf64 t64
;
1708 t64
.actime
= t
->actime
;
1709 t64
.modtime
= t
->modtime
;
1710 return _futime64( fd
, &t64
);
1713 return _futime64( fd
, NULL
);
1716 /*********************************************************************
1717 * _futime (MSVCRT.@)
1720 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1722 return _futime64( fd
, t
);
1725 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1727 return _futime32( fd
, t
);
1731 /*********************************************************************
1732 * _get_osfhandle (MSVCRT.@)
1734 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1736 HANDLE hand
= msvcrt_fdtoh(fd
);
1737 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1739 return (MSVCRT_intptr_t
)hand
;
1742 /*********************************************************************
1743 * _mktemp_s (MSVCRT.@)
1745 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1749 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1750 return MSVCRT_EINVAL
;
1752 for(len
=0; len
<size
; len
++)
1755 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1758 return MSVCRT_EINVAL
;
1761 for(xno
=1; xno
<=6; xno
++)
1762 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1763 return MSVCRT_EINVAL
;
1765 id
= GetCurrentProcessId();
1766 for(xno
=1; xno
<6; xno
++) {
1767 pattern
[len
-xno
] = id
%10 + '0';
1771 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1772 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1777 *MSVCRT__errno() = MSVCRT_EEXIST
;
1778 return MSVCRT_EEXIST
;
1781 /*********************************************************************
1782 * _mktemp (MSVCRT.@)
1784 char * CDECL
MSVCRT__mktemp(char *pattern
)
1787 char *retVal
= pattern
;
1795 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1799 id
= GetCurrentProcessId();
1803 int tempNum
= id
/ 10;
1804 *pattern
-- = id
- (tempNum
* 10) + '0';
1810 *pattern
= letter
++;
1811 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1813 } while(letter
<= 'z');
1817 /*********************************************************************
1818 * _wmktemp_s (MSVCRT.@)
1820 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1824 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1825 return MSVCRT_EINVAL
;
1827 for(len
=0; len
<size
; len
++)
1830 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1833 return MSVCRT_EINVAL
;
1836 for(xno
=1; xno
<=6; xno
++)
1837 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1838 return MSVCRT_EINVAL
;
1840 id
= GetCurrentProcessId();
1841 for(xno
=1; xno
<6; xno
++) {
1842 pattern
[len
-xno
] = id
%10 + '0';
1846 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1847 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1852 *MSVCRT__errno() = MSVCRT_EEXIST
;
1853 return MSVCRT_EEXIST
;
1856 /*********************************************************************
1857 * _wmktemp (MSVCRT.@)
1859 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1862 MSVCRT_wchar_t
*retVal
= pattern
;
1864 MSVCRT_wchar_t letter
= 'a';
1870 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1874 id
= GetCurrentProcessId();
1878 int tempNum
= id
/ 10;
1879 *pattern
-- = id
- (tempNum
* 10) + '0';
1885 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1887 *pattern
= letter
++;
1888 } while(letter
!= '|');
1892 static unsigned split_oflags(unsigned oflags
)
1895 unsigned unsupp
; /* until we support everything */
1897 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1898 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1899 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1900 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1901 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1902 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1903 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1904 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1905 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1907 if ((unsupp
= oflags
& ~(
1908 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1909 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1910 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1911 MSVCRT__O_NOINHERIT
|
1912 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
1913 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
1915 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1920 /*********************************************************************
1923 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1926 SECURITY_ATTRIBUTES sa
;
1927 HANDLE readHandle
, writeHandle
;
1931 *MSVCRT__errno() = MSVCRT_EINVAL
;
1935 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1936 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1937 sa
.lpSecurityDescriptor
= NULL
;
1938 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1940 unsigned int wxflags
= split_oflags(textmode
);
1944 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
1948 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
1956 MSVCRT__close(pfds
[0]);
1957 CloseHandle(writeHandle
);
1958 *MSVCRT__errno() = MSVCRT_EMFILE
;
1963 CloseHandle(readHandle
);
1964 CloseHandle(writeHandle
);
1965 *MSVCRT__errno() = MSVCRT_EMFILE
;
1970 msvcrt_set_errno(GetLastError());
1975 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1977 char bom
[sizeof(utf8_bom
)];
1980 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
1982 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1985 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1986 oflags
|= MSVCRT__O_U8TEXT
;
1987 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1989 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1990 oflags
|= MSVCRT__O_U16TEXT
;
1992 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1998 /*********************************************************************
1999 * _wsopen_s (MSVCRT.@)
2001 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2003 DWORD access
= 0, creation
= 0, attrib
;
2004 SECURITY_ATTRIBUTES sa
;
2005 DWORD sharing
, type
;
2009 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2010 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2012 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2015 wxflag
= split_oflags(oflags
);
2016 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2018 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2019 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2020 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2023 if (oflags
& MSVCRT__O_CREAT
)
2025 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2026 FIXME(": pmode 0x%04x ignored\n", pmode
);
2028 WARN(": pmode 0x%04x ignored\n", pmode
);
2030 if (oflags
& MSVCRT__O_EXCL
)
2031 creation
= CREATE_NEW
;
2032 else if (oflags
& MSVCRT__O_TRUNC
)
2033 creation
= CREATE_ALWAYS
;
2035 creation
= OPEN_ALWAYS
;
2037 else /* no MSVCRT__O_CREAT */
2039 if (oflags
& MSVCRT__O_TRUNC
)
2040 creation
= TRUNCATE_EXISTING
;
2042 creation
= OPEN_EXISTING
;
2047 case MSVCRT__SH_DENYRW
:
2050 case MSVCRT__SH_DENYWR
:
2051 sharing
= FILE_SHARE_READ
;
2053 case MSVCRT__SH_DENYRD
:
2054 sharing
= FILE_SHARE_WRITE
;
2056 case MSVCRT__SH_DENYNO
:
2057 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2060 ERR( "Unhandled shflags 0x%x\n", shflags
);
2061 return MSVCRT_EINVAL
;
2063 attrib
= FILE_ATTRIBUTE_NORMAL
;
2065 if (oflags
& MSVCRT__O_TEMPORARY
)
2067 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2069 sharing
|= FILE_SHARE_DELETE
;
2072 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2073 sa
.lpSecurityDescriptor
= NULL
;
2074 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2076 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2077 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2078 && !(access
&GENERIC_READ
))
2080 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2081 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2082 if (hand
!= INVALID_HANDLE_VALUE
)
2084 oflags
= check_bom(hand
, oflags
, FALSE
);
2088 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2091 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2092 if (hand
== INVALID_HANDLE_VALUE
) {
2093 WARN(":failed-last error (%d)\n",GetLastError());
2094 msvcrt_set_errno(GetLastError());
2095 return *MSVCRT__errno();
2098 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2100 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2101 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2102 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2104 if (oflags
& MSVCRT__O_U8TEXT
)
2106 DWORD written
= 0, tmp
;
2108 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2109 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2111 if (written
!= sizeof(utf8_bom
)) {
2112 WARN("error writing BOM\n");
2114 msvcrt_set_errno(GetLastError());
2115 return *MSVCRT__errno();
2120 DWORD written
= 0, tmp
;
2122 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2123 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2125 if (written
!= sizeof(utf16_bom
))
2127 WARN("error writing BOM\n");
2129 msvcrt_set_errno(GetLastError());
2130 return *MSVCRT__errno();
2134 else if (access
& GENERIC_READ
)
2135 oflags
= check_bom(hand
, oflags
, TRUE
);
2138 type
= GetFileType(hand
);
2139 if (type
== FILE_TYPE_CHAR
)
2141 else if (type
== FILE_TYPE_PIPE
)
2144 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2146 return *MSVCRT__errno();
2148 if (oflags
& MSVCRT__O_WTEXT
)
2149 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2150 else if (oflags
& MSVCRT__O_U16TEXT
)
2151 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
2152 else if (oflags
& MSVCRT__O_U8TEXT
)
2153 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
2155 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2159 /*********************************************************************
2160 * _wsopen (MSVCRT.@)
2162 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2167 if (oflags
& MSVCRT__O_CREAT
)
2171 __ms_va_start(ap
, shflags
);
2172 pmode
= va_arg(ap
, int);
2178 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2182 /*********************************************************************
2183 * _sopen_s (MSVCRT.@)
2185 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2187 MSVCRT_wchar_t
*pathW
;
2190 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2191 return MSVCRT_EINVAL
;
2193 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2194 return MSVCRT_EINVAL
;
2196 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2201 /*********************************************************************
2204 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2209 if (oflags
& MSVCRT__O_CREAT
)
2213 __ms_va_start(ap
, shflags
);
2214 pmode
= va_arg(ap
, int);
2220 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2224 /*********************************************************************
2227 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2231 if (flags
& MSVCRT__O_CREAT
)
2234 __ms_va_start(ap
, flags
);
2235 pmode
= va_arg(ap
, int);
2237 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2240 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2243 /*********************************************************************
2246 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2250 if (flags
& MSVCRT__O_CREAT
)
2253 __ms_va_start(ap
, flags
);
2254 pmode
= va_arg(ap
, int);
2256 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2259 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2262 /*********************************************************************
2265 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2267 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2268 return MSVCRT__open(path
, usedFlags
);
2271 /*********************************************************************
2272 * _wcreat (MSVCRT.@)
2274 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2276 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2277 return MSVCRT__wopen(path
, usedFlags
);
2280 /*********************************************************************
2281 * _open_osfhandle (MSVCRT.@)
2283 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2288 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2289 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2290 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2291 * text - it never sets MSVCRT__O_BINARY.
2293 /* don't let split_oflags() decide the mode if no mode is passed */
2294 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2295 oflags
|= MSVCRT__O_BINARY
;
2297 flags
= GetFileType((HANDLE
)handle
);
2298 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2300 msvcrt_set_errno(GetLastError());
2304 if (flags
== FILE_TYPE_CHAR
)
2306 else if (flags
== FILE_TYPE_PIPE
)
2310 flags
|= split_oflags(oflags
);
2312 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2313 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2317 /*********************************************************************
2320 int CDECL
MSVCRT__rmtmp(void)
2322 int num_removed
= 0, i
;
2326 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2327 file
= msvcrt_get_file(i
);
2329 if (file
->_tmpfname
)
2331 MSVCRT_fclose(file
);
2338 TRACE(":removed (%d) temp files\n",num_removed
);
2342 static inline int get_utf8_char_len(char ch
)
2344 if((ch
&0xf8) == 0xf0)
2346 else if((ch
&0xf0) == 0xe0)
2348 else if((ch
&0xe0) == 0xc0)
2353 /*********************************************************************
2354 * (internal) read_utf8
2356 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2358 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2359 HANDLE hand
= fdinfo
->handle
;
2360 char min_buf
[4], *readbuf
, lookahead
;
2361 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2363 /* make the buffer big enough to hold at least one character */
2364 /* read bytes have to fit to output and lookahead buffers */
2366 readbuf_size
= count
< 4 ? 4 : count
;
2367 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2372 if(fdinfo
->lookahead
[0] != '\n') {
2373 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2374 fdinfo
->lookahead
[0] = '\n';
2376 if(fdinfo
->lookahead
[1] != '\n') {
2377 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2378 fdinfo
->lookahead
[1] = '\n';
2380 if(fdinfo
->lookahead
[2] != '\n') {
2381 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2382 fdinfo
->lookahead
[2] = '\n';
2387 /* NOTE: this case is broken in native dll, reading
2388 * sometimes fails when small buffer is passed
2391 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2392 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2393 fdinfo
->wxflag
|= WX_ATEOF
;
2396 msvcrt_set_errno(GetLastError());
2399 }else if(!num_read
) {
2400 fdinfo
->wxflag
|= WX_ATEOF
;
2406 char_len
= get_utf8_char_len(readbuf
[0]);
2408 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2412 if(readbuf
[0] == '\n')
2413 fdinfo
->wxflag
|= WX_READNL
;
2415 fdinfo
->wxflag
&= ~WX_READNL
;
2417 if(readbuf
[0] == 0x1a) {
2418 fdinfo
->wxflag
|= WX_ATEOF
;
2422 if(readbuf
[0] == '\r') {
2423 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2425 else if(lookahead
== '\n')
2429 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2430 fdinfo
->lookahead
[0] = lookahead
;
2432 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2437 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2438 msvcrt_set_errno(GetLastError());
2445 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2448 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2449 fdinfo
->wxflag
|= WX_ATEOF
;
2450 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2453 msvcrt_set_errno(GetLastError());
2454 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2457 }else if(!pos
&& !num_read
) {
2458 fdinfo
->wxflag
|= WX_ATEOF
;
2459 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2464 if(readbuf
[0] == '\n')
2465 fdinfo
->wxflag
|= WX_READNL
;
2467 fdinfo
->wxflag
&= ~WX_READNL
;
2469 /* Find first byte of last character (may be incomplete) */
2470 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2471 if((readbuf
[i
]&0xc0) != 0x80)
2473 char_len
= get_utf8_char_len(readbuf
[i
]);
2474 if(char_len
+i
<= pos
)
2477 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2479 fdinfo
->lookahead
[0] = readbuf
[i
];
2481 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2483 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2485 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2489 for(i
=0, j
=0; i
<pos
; i
++) {
2490 if(readbuf
[i
] == 0x1a) {
2491 fdinfo
->wxflag
|= WX_ATEOF
;
2495 /* strip '\r' if followed by '\n' */
2496 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2497 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2498 readbuf
[j
++] = '\r';
2499 }else if(lookahead
== '\n' && j
==0) {
2500 readbuf
[j
++] = '\n';
2502 if(lookahead
!= '\n')
2503 readbuf
[j
++] = '\r';
2505 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2506 fdinfo
->lookahead
[0] = lookahead
;
2508 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2510 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2511 readbuf
[j
++] = readbuf
[i
];
2516 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2517 msvcrt_set_errno(GetLastError());
2518 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2522 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2526 /*********************************************************************
2529 * When reading \r as last character in text mode, read() positions
2530 * the file pointer on the \r character while getc() goes on to
2533 static int read_i(int fd
, void *buf
, unsigned int count
)
2535 DWORD num_read
, utf16
;
2536 char *bufstart
= buf
;
2537 HANDLE hand
= msvcrt_fdtoh(fd
);
2538 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2543 if (fdinfo
->wxflag
& WX_ATEOF
) {
2544 TRACE("already at EOF, returning 0\n");
2547 /* Don't trace small reads, it gets *very* annoying */
2549 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2550 if (hand
== INVALID_HANDLE_VALUE
)
2552 *MSVCRT__errno() = MSVCRT_EBADF
;
2556 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2557 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2559 *MSVCRT__errno() = MSVCRT_EINVAL
;
2563 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2564 return read_utf8(fd
, buf
, count
);
2566 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2568 if (fdinfo
->lookahead
[0] != '\n')
2570 bufstart
[0] = fdinfo
->lookahead
[0];
2571 fdinfo
->lookahead
[0] = '\n';
2575 bufstart
[1] = fdinfo
->lookahead
[1];
2576 fdinfo
->lookahead
[1] = '\n';
2579 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2580 num_read
+= 1+utf16
;
2585 if(utf16
&& (num_read
&1))
2587 /* msvcr90 uses uninitialized value from the buffer in this case */
2588 /* msvcrt ignores additional data */
2589 ERR("got odd number of bytes in UTF16 mode\n");
2593 if (count
!= 0 && num_read
== 0)
2595 fdinfo
->wxflag
|= WX_ATEOF
;
2596 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2598 else if (fdinfo
->wxflag
& WX_TEXT
)
2602 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2603 fdinfo
->wxflag
|= WX_READNL
;
2605 fdinfo
->wxflag
&= ~WX_READNL
;
2607 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2609 /* in text mode, a ctrl-z signals EOF */
2610 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2612 fdinfo
->wxflag
|= WX_ATEOF
;
2613 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2617 /* in text mode, strip \r if followed by \n */
2618 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2623 lookahead
[1] = '\n';
2624 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2626 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2628 bufstart
[j
++] = '\n';
2629 if(utf16
) bufstart
[j
++] = 0;
2633 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2635 bufstart
[j
++] = '\r';
2636 if(utf16
) bufstart
[j
++] = 0;
2639 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2641 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2643 bufstart
[j
++] = '\n';
2644 if (utf16
) bufstart
[j
++] = 0;
2648 fdinfo
->lookahead
[0] = lookahead
[0];
2649 fdinfo
->lookahead
[1] = lookahead
[1];
2653 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2658 bufstart
[j
++] = '\r';
2659 if(utf16
) bufstart
[j
++] = 0;
2662 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2663 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2665 bufstart
[j
++] = bufstart
[i
];
2666 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2674 if (GetLastError() == ERROR_BROKEN_PIPE
)
2676 TRACE(":end-of-pipe\n");
2677 fdinfo
->wxflag
|= WX_ATEOF
;
2682 TRACE(":failed-last error (%d)\n",GetLastError());
2688 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2692 /*********************************************************************
2695 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2698 num_read
= read_i(fd
, buf
, count
);
2702 /*********************************************************************
2703 * _setmode (MSVCRT.@)
2705 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2707 int ret
= msvcrt_get_ioinfo(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2708 if(ret
==MSVCRT__O_TEXT
&& (msvcrt_get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2709 ret
= MSVCRT__O_WTEXT
;
2711 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2712 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2713 *MSVCRT__errno() = MSVCRT_EINVAL
;
2717 if(mode
== MSVCRT__O_BINARY
) {
2718 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2719 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2723 msvcrt_get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2724 if(mode
== MSVCRT__O_TEXT
)
2725 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2726 else if(mode
== MSVCRT__O_U8TEXT
)
2727 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2729 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2734 /*********************************************************************
2735 * _stat64 (MSVCRT.@)
2737 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2740 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2741 unsigned short mode
= ALL_S_IREAD
;
2744 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2746 plen
= strlen(path
);
2747 while (plen
&& path
[plen
-1]==' ')
2750 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2751 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2753 *MSVCRT__errno() = MSVCRT_ENOENT
;
2757 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2759 TRACE("failed (%d)\n",GetLastError());
2760 *MSVCRT__errno() = MSVCRT_ENOENT
;
2764 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2766 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2767 Bon 011120: This FIXME seems incorrect
2768 Also a letter as first char isn't enough to be classified
2771 if (isalpha(*path
)&& (*(path
+1)==':'))
2772 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2774 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2776 /* Dir, or regular file? */
2777 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2778 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2781 mode
|= MSVCRT__S_IFREG
;
2783 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2785 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2786 (tolower(path
[plen
-3]) << 16);
2787 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2788 mode
|= ALL_S_IEXEC
;
2792 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2793 mode
|= ALL_S_IWRITE
;
2795 buf
->st_mode
= mode
;
2797 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2798 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2800 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2801 buf
->st_mtime
= buf
->st_ctime
= dw
;
2802 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2803 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2804 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2808 /*********************************************************************
2809 * _stati64 (MSVCRT.@)
2811 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2814 struct MSVCRT__stat64 buf64
;
2816 ret
= MSVCRT_stat64(path
, &buf64
);
2818 msvcrt_stat64_to_stati64(&buf64
, buf
);
2822 /*********************************************************************
2825 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2828 struct MSVCRT__stat64 buf64
;
2830 ret
= MSVCRT_stat64( path
, &buf64
);
2832 msvcrt_stat64_to_stat(&buf64
, buf
);
2836 /*********************************************************************
2837 * _stat32 (MSVCR100.@)
2839 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2842 struct MSVCRT__stat64 buf64
;
2844 ret
= MSVCRT_stat64(path
, &buf64
);
2846 msvcrt_stat64_to_stat32(&buf64
, buf
);
2850 /*********************************************************************
2851 * _stat32i64 (MSVCR100.@)
2853 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2856 struct MSVCRT__stat64 buf64
;
2858 ret
= MSVCRT_stat64(path
, &buf64
);
2860 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2864 /*********************************************************************
2865 * _stat64i32 (MSVCR100.@)
2867 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2870 struct MSVCRT__stat64 buf64
;
2872 ret
= MSVCRT_stat64(path
, &buf64
);
2874 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2878 /*********************************************************************
2879 * _wstat64 (MSVCRT.@)
2881 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2884 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2885 unsigned short mode
= ALL_S_IREAD
;
2888 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2890 plen
= strlenW(path
);
2891 while (plen
&& path
[plen
-1]==' ')
2894 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2895 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2897 *MSVCRT__errno() = MSVCRT_ENOENT
;
2901 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2903 TRACE("failed (%d)\n",GetLastError());
2904 *MSVCRT__errno() = MSVCRT_ENOENT
;
2908 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2910 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2911 if (MSVCRT_iswalpha(*path
))
2912 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2914 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2916 /* Dir, or regular file? */
2917 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2918 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2921 mode
|= MSVCRT__S_IFREG
;
2923 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2925 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2926 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2927 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2928 mode
|= ALL_S_IEXEC
;
2932 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2933 mode
|= ALL_S_IWRITE
;
2935 buf
->st_mode
= mode
;
2937 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2938 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2940 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2941 buf
->st_mtime
= buf
->st_ctime
= dw
;
2942 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2943 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2944 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2948 /*********************************************************************
2949 * _wstati64 (MSVCRT.@)
2951 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2954 struct MSVCRT__stat64 buf64
;
2956 ret
= MSVCRT__wstat64(path
, &buf64
);
2958 msvcrt_stat64_to_stati64(&buf64
, buf
);
2962 /*********************************************************************
2965 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2968 struct MSVCRT__stat64 buf64
;
2970 ret
= MSVCRT__wstat64( path
, &buf64
);
2971 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2975 /*********************************************************************
2976 * _wstat32 (MSVCR100.@)
2978 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
2981 struct MSVCRT__stat64 buf64
;
2983 ret
= MSVCRT__wstat64(path
, &buf64
);
2985 msvcrt_stat64_to_stat32(&buf64
, buf
);
2989 /*********************************************************************
2990 * _wstat32i64 (MSVCR100.@)
2992 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
2995 struct MSVCRT__stat64 buf64
;
2997 ret
= MSVCRT__wstat64(path
, &buf64
);
2999 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3003 /*********************************************************************
3004 * _wstat64i32 (MSVCR100.@)
3006 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3009 struct MSVCRT__stat64 buf64
;
3011 ret
= MSVCRT__wstat64(path
, &buf64
);
3013 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3017 /*********************************************************************
3020 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3022 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3025 /*********************************************************************
3026 * _telli64 (MSVCRT.@)
3028 __int64 CDECL
_telli64(int fd
)
3030 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3033 /*********************************************************************
3034 * _tempnam (MSVCRT.@)
3036 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3038 char tmpbuf
[MAX_PATH
];
3039 const char *tmp_dir
= MSVCRT_getenv("TMP");
3041 if (tmp_dir
) dir
= tmp_dir
;
3043 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3044 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3046 TRACE("got name (%s)\n",tmpbuf
);
3047 DeleteFileA(tmpbuf
);
3048 return MSVCRT__strdup(tmpbuf
);
3050 TRACE("failed (%d)\n",GetLastError());
3054 /*********************************************************************
3055 * _wtempnam (MSVCRT.@)
3057 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3059 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3060 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3061 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3063 if (tmp_dir
) dir
= tmp_dir
;
3065 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3066 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3068 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3069 DeleteFileW(tmpbuf
);
3070 return MSVCRT__wcsdup(tmpbuf
);
3072 TRACE("failed (%d)\n",GetLastError());
3076 /*********************************************************************
3079 int CDECL
MSVCRT__umask(int umask
)
3081 int old_umask
= MSVCRT_umask
;
3082 TRACE("(%d)\n",umask
);
3083 MSVCRT_umask
= umask
;
3087 /*********************************************************************
3088 * _utime64 (MSVCRT.@)
3090 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3092 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3096 int retVal
= _futime64(fd
, t
);
3103 /*********************************************************************
3104 * _utime32 (MSVCRT.@)
3106 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3110 struct MSVCRT___utimbuf64 t64
;
3111 t64
.actime
= t
->actime
;
3112 t64
.modtime
= t
->modtime
;
3113 return _utime64( path
, &t64
);
3116 return _utime64( path
, NULL
);
3119 /*********************************************************************
3123 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3125 return _utime64( path
, t
);
3128 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3130 return _utime32( path
, t
);
3134 /*********************************************************************
3135 * _wutime64 (MSVCRT.@)
3137 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3139 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3143 int retVal
= _futime64(fd
, t
);
3150 /*********************************************************************
3151 * _wutime32 (MSVCRT.@)
3153 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3157 struct MSVCRT___utimbuf64 t64
;
3158 t64
.actime
= t
->actime
;
3159 t64
.modtime
= t
->modtime
;
3160 return _wutime64( path
, &t64
);
3163 return _wutime64( path
, NULL
);
3166 /*********************************************************************
3167 * _wutime (MSVCRT.@)
3170 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3172 return _wutime64( path
, t
);
3175 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3177 return _wutime32( path
, t
);
3181 /*********************************************************************
3184 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3187 ioinfo
*info
= msvcrt_get_ioinfo(fd
);
3188 HANDLE hand
= info
->handle
;
3190 /* Don't trace small writes, it gets *very* annoying */
3193 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3195 if (hand
== INVALID_HANDLE_VALUE
)
3197 *MSVCRT__errno() = MSVCRT_EBADF
;
3201 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3203 *MSVCRT__errno() = MSVCRT_EINVAL
;
3207 /* If appending, go to EOF */
3208 if (info
->wxflag
& WX_APPEND
)
3209 MSVCRT__lseek(fd
, 0, FILE_END
);
3211 if (!(info
->wxflag
& WX_TEXT
))
3213 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3214 && (num_written
== count
))
3216 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3217 hand
, GetLastError());
3218 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3222 unsigned int i
, j
, nr_lf
, size
;
3225 const char *s
= buf
, *buf_start
= buf
;
3227 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3229 /* find number of \n */
3230 for (nr_lf
=0, i
=0; i
<count
; i
++)
3236 if ((q
= p
= MSVCRT_malloc(size
)))
3238 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3247 FIXME("Malloc failed\n");
3259 else if (info
->exflag
& EF_UTF16
)
3261 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3262 if (s
[i
]=='\n' && s
[i
+1]==0)
3267 if ((q
= p
= MSVCRT_malloc(size
)))
3269 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3271 if (s
[i
]=='\n' && s
[i
+1]==0)
3282 FIXME("Malloc failed\n");
3298 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3299 if (s
[i
]=='\n' && s
[i
+1]==0)
3302 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3304 msvcrt_set_errno(GetLastError());
3309 size
= conv_len
+nr_lf
;
3310 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3312 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3314 if (s
[i
]=='\n' && s
[i
+1]==0)
3322 q
= p
+count
+nr_lf
*2;
3323 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3324 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3328 FIXME("Malloc failed\n");
3335 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3339 if (num_written
!= size
)
3341 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3342 fd
, hand
, GetLastError(), num_written
);
3343 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3344 return s
- buf_start
;
3352 /*********************************************************************
3355 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3359 MSVCRT__lock_file(file
);
3360 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3361 if (len
== sizeof(val
)) {
3362 MSVCRT__unlock_file(file
);
3366 file
->_flag
|= MSVCRT__IOERR
;
3367 MSVCRT__unlock_file(file
);
3371 /*********************************************************************
3374 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3378 MSVCRT__lock_file(file
);
3380 MSVCRT_free(file
->_tmpfname
);
3381 file
->_tmpfname
= NULL
;
3382 /* flush stdio buffers */
3383 if(file
->_flag
& MSVCRT__IOWRT
)
3384 MSVCRT_fflush(file
);
3385 if(file
->_flag
& MSVCRT__IOMYBUF
)
3386 MSVCRT_free(file
->_base
);
3388 r
=MSVCRT__close(file
->_file
);
3391 MSVCRT__unlock_file(file
);
3393 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3396 /*********************************************************************
3399 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3401 return file
->_flag
& MSVCRT__IOEOF
;
3404 /*********************************************************************
3407 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3409 return file
->_flag
& MSVCRT__IOERR
;
3412 /*********************************************************************
3413 * _filbuf (MSVCRT.@)
3415 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3418 MSVCRT__lock_file(file
);
3420 if(file
->_flag
& MSVCRT__IOSTRG
) {
3421 MSVCRT__unlock_file(file
);
3425 /* Allocate buffer if needed */
3426 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3427 msvcrt_alloc_buffer(file
);
3429 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3430 if(file
->_flag
& MSVCRT__IORW
)
3431 file
->_flag
|= MSVCRT__IOREAD
;
3433 MSVCRT__unlock_file(file
);
3438 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3440 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3441 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3442 MSVCRT__unlock_file(file
);
3446 MSVCRT__unlock_file(file
);
3449 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3451 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3453 MSVCRT__unlock_file(file
);
3458 file
->_ptr
= file
->_base
+1;
3459 c
= *(unsigned char *)file
->_base
;
3460 MSVCRT__unlock_file(file
);
3465 /*********************************************************************
3468 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3473 MSVCRT__lock_file(file
);
3476 i
= (unsigned char *)file
->_ptr
++;
3479 j
= MSVCRT__filbuf(file
);
3481 MSVCRT__unlock_file(file
);
3485 /*********************************************************************
3486 * _fgetchar (MSVCRT.@)
3488 int CDECL
MSVCRT__fgetchar(void)
3490 return MSVCRT_fgetc(MSVCRT_stdin
);
3493 /*********************************************************************
3496 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3498 int cc
= MSVCRT_EOF
;
3499 char * buf_start
= s
;
3501 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3502 file
,file
->_file
,s
,size
);
3504 MSVCRT__lock_file(file
);
3506 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3511 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3513 TRACE(":nothing read\n");
3514 MSVCRT__unlock_file(file
);
3517 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3520 TRACE(":got %s\n", debugstr_a(buf_start
));
3521 MSVCRT__unlock_file(file
);
3525 /*********************************************************************
3528 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3533 MSVCRT__lock_file(file
);
3535 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3536 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3539 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3540 ch
= MSVCRT_fgetc(file
);
3541 if(ch
== MSVCRT_EOF
) {
3548 char mbs
[MSVCRT_MB_LEN_MAX
];
3551 ch
= MSVCRT_fgetc(file
);
3552 if(ch
!= MSVCRT_EOF
) {
3554 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3555 ch
= MSVCRT_fgetc(file
);
3556 if(ch
!= MSVCRT_EOF
) {
3565 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3569 MSVCRT__unlock_file(file
);
3573 /*********************************************************************
3576 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3583 MSVCRT__lock_file(file
);
3584 for (j
=0; j
<sizeof(int); j
++) {
3585 k
= MSVCRT_fgetc(file
);
3586 if (k
== MSVCRT_EOF
) {
3587 file
->_flag
|= MSVCRT__IOEOF
;
3588 MSVCRT__unlock_file(file
);
3594 MSVCRT__unlock_file(file
);
3598 /*********************************************************************
3601 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3603 return MSVCRT_fgetwc(file
);
3606 /*********************************************************************
3607 * _fgetwchar (MSVCRT.@)
3609 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3611 return MSVCRT_fgetwc(MSVCRT_stdin
);
3614 /*********************************************************************
3615 * getwchar (MSVCRT.@)
3617 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3619 return MSVCRT__fgetwchar();
3622 /*********************************************************************
3625 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3627 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3628 MSVCRT_wchar_t
* buf_start
= s
;
3630 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3631 file
,file
->_file
,s
,size
);
3633 MSVCRT__lock_file(file
);
3635 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3640 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3642 TRACE(":nothing read\n");
3643 MSVCRT__unlock_file(file
);
3646 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3649 TRACE(":got %s\n", debugstr_w(buf_start
));
3650 MSVCRT__unlock_file(file
);
3654 /*********************************************************************
3655 * _flsbuf (MSVCRT.@)
3657 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3659 /* Flush output buffer */
3660 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3661 msvcrt_alloc_buffer(file
);
3663 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3664 if(file
->_flag
& MSVCRT__IORW
)
3665 file
->_flag
|= MSVCRT__IOWRT
;
3669 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3672 if(file
->_cnt
<= 0) {
3673 res
= msvcrt_flush_buffer(file
);
3676 file
->_cnt
=file
->_bufsiz
;
3684 /* set _cnt to 0 for unbuffered FILEs */
3686 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3689 file
->_flag
|= MSVCRT__IOERR
;
3694 /*********************************************************************
3697 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3699 MSVCRT_size_t wrcnt
=size
* nmemb
;
3704 MSVCRT__lock_file(file
);
3707 if(file
->_cnt
< 0) {
3708 WARN("negative file->_cnt value in %p\n", file
);
3709 file
->_flag
|= MSVCRT__IOERR
;
3711 } else if(file
->_cnt
) {
3712 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3713 memcpy(file
->_ptr
, ptr
, pcnt
);
3718 ptr
= (const char*)ptr
+ pcnt
;
3719 } else if((file
->_flag
& MSVCRT__IONBF
)
3720 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3721 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3725 if(file
->_flag
& MSVCRT__IONBF
)
3727 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3728 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3730 bufsiz
= file
->_bufsiz
;
3732 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3734 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3737 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3738 file
->_flag
|= MSVCRT__IOERR
;
3743 ptr
= (const char*)ptr
+ pcnt
;
3745 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3749 ptr
= (const char*)ptr
+ 1;
3753 MSVCRT__unlock_file(file
);
3754 return written
/ size
;
3757 /*********************************************************************
3760 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3762 MSVCRT_wchar_t mwc
=wc
;
3766 MSVCRT__lock_file(file
);
3767 fdinfo
= msvcrt_get_ioinfo(file
->_file
);
3769 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3770 char buf
[MSVCRT_MB_LEN_MAX
];
3773 char_len
= MSVCRT_wctomb(buf
, mwc
);
3774 if(char_len
!=-1 && MSVCRT_fwrite(buf
, char_len
, 1, file
)==1)
3778 }else if(MSVCRT_fwrite(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3784 MSVCRT__unlock_file(file
);
3788 /*********************************************************************
3789 * _fputwchar (MSVCRT.@)
3791 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3793 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3796 /*********************************************************************
3797 * _wfsopen (MSVCRT.@)
3799 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3802 int open_flags
, stream_flags
, fd
;
3804 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3806 /* map mode string to open() flags. "man fopen" for possibilities. */
3807 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3811 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3814 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3816 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3823 TRACE(":got (%p)\n",file
);
3824 if (fd
>= 0 && !file
)
3830 /*********************************************************************
3831 * _fsopen (MSVCRT.@)
3833 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3836 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3838 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3839 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3840 *MSVCRT__errno() = MSVCRT_EINVAL
;
3843 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3846 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3847 *MSVCRT__errno() = MSVCRT_EINVAL
;
3851 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
3858 /*********************************************************************
3861 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
3863 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3866 /*********************************************************************
3867 * fopen_s (MSVCRT.@)
3869 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
3870 const char *filename
, const char *mode
)
3872 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3873 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3874 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3876 *pFile
= MSVCRT_fopen(filename
, mode
);
3879 return *MSVCRT__errno();
3883 /*********************************************************************
3884 * _wfopen (MSVCRT.@)
3886 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
3888 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3891 /*********************************************************************
3892 * _wfopen_s (MSVCRT.@)
3894 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
3895 const MSVCRT_wchar_t
*mode
)
3897 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3898 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3899 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3901 *pFile
= MSVCRT__wfopen(filename
, mode
);
3904 return *MSVCRT__errno();
3908 /*********************************************************************
3911 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
3915 MSVCRT__lock_file(file
);
3921 res
= msvcrt_flush_buffer(file
);
3922 MSVCRT__unlock_file(file
);
3923 return res
? res
: c
;
3926 MSVCRT__unlock_file(file
);
3930 res
= MSVCRT__flsbuf(c
, file
);
3931 MSVCRT__unlock_file(file
);
3936 /*********************************************************************
3937 * _fputchar (MSVCRT.@)
3939 int CDECL
MSVCRT__fputchar(int c
)
3941 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3944 /*********************************************************************
3947 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3949 MSVCRT_size_t rcnt
=size
* nmemb
;
3950 MSVCRT_size_t read
=0;
3951 MSVCRT_size_t pread
=0;
3956 MSVCRT__lock_file(file
);
3958 /* first buffered data */
3960 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3961 memcpy(ptr
, file
->_ptr
, pcnt
);
3966 ptr
= (char*)ptr
+ pcnt
;
3967 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3968 if(file
->_flag
& MSVCRT__IORW
) {
3969 file
->_flag
|= MSVCRT__IOREAD
;
3971 MSVCRT__unlock_file(file
);
3976 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3977 msvcrt_alloc_buffer(file
);
3982 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3983 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3984 file
->_ptr
= file
->_base
;
3985 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3986 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3987 if (i
> 0 && i
< file
->_cnt
) {
3988 msvcrt_get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3989 file
->_flag
&= ~MSVCRT__IOEOF
;
3992 memcpy(ptr
, file
->_ptr
, i
);
3996 } else if (rcnt
> INT_MAX
) {
3997 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
3998 } else if (rcnt
< MSVCRT_BUFSIZ
) {
3999 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4001 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4005 ptr
= (char *)ptr
+i
;
4006 /* expose feof condition in the flags
4007 * MFC tests file->_flag for feof, and doesn't call feof())
4009 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
4010 file
->_flag
|= MSVCRT__IOEOF
;
4013 file
->_flag
|= MSVCRT__IOERR
;
4020 MSVCRT__unlock_file(file
);
4025 /*********************************************************************
4026 * fread_s (MSVCR80.@)
4028 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4029 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4031 size_t bytes_left
, buf_pos
;
4033 TRACE("(%p %lu %lu %lu %p\n", buf
, buf_size
, elem_size
, count
, stream
);
4035 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4037 memset(buf
, 0, buf_size
);
4040 if(!elem_size
|| !count
) return 0;
4041 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4042 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4044 bytes_left
= elem_size
*count
;
4047 if(stream
->_cnt
> 0) {
4048 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4050 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4051 memset(buf
, 0, buf_size
);
4055 MSVCRT_fread((char*)buf
+buf_pos
, 1, size
, stream
);
4059 int c
= MSVCRT__filbuf(stream
);
4064 if(!MSVCRT_CHECK_PMT_ERR(buf_size
-buf_pos
> 0, MSVCRT_ERANGE
)) {
4065 memset(buf
, 0, buf_size
);
4069 ((char*)buf
)[buf_pos
++] = c
;
4074 return buf_pos
/elem_size
;
4077 /*********************************************************************
4078 * _wfreopen (MSVCRT.@)
4081 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4083 int open_flags
, stream_flags
, fd
;
4085 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4088 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4092 MSVCRT_fclose(file
);
4093 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4095 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4097 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4107 /*********************************************************************
4108 * _wfreopen_s (MSVCRT.@)
4110 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4111 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4113 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4114 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4115 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4116 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4118 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4121 return *MSVCRT__errno();
4125 /*********************************************************************
4126 * freopen (MSVCRT.@)
4129 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4132 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4134 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4135 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4141 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4148 /*********************************************************************
4149 * freopen_s (MSVCRT.@)
4151 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4152 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4154 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4155 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4156 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4157 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4159 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4162 return *MSVCRT__errno();
4166 /*********************************************************************
4167 * fsetpos (MSVCRT.@)
4169 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4173 MSVCRT__lock_file(file
);
4174 /* Note that all this has been lifted 'as is' from fseek */
4175 if(file
->_flag
& MSVCRT__IOWRT
)
4176 msvcrt_flush_buffer(file
);
4178 /* Discard buffered input */
4180 file
->_ptr
= file
->_base
;
4182 /* Reset direction of i/o */
4183 if(file
->_flag
& MSVCRT__IORW
) {
4184 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4187 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4188 MSVCRT__unlock_file(file
);
4192 /*********************************************************************
4193 * _ftelli64 (MSVCRT.@)
4195 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4199 MSVCRT__lock_file(file
);
4200 pos
= _telli64(file
->_file
);
4202 MSVCRT__unlock_file(file
);
4205 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4206 if(file
->_flag
& MSVCRT__IOWRT
) {
4207 pos
+= file
->_ptr
- file
->_base
;
4209 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4212 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4216 } else if(!file
->_cnt
) { /* nothing to do */
4217 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4221 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4222 for(i
=0; i
<file
->_cnt
; i
++)
4223 if(file
->_ptr
[i
] == '\n')
4229 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
4230 MSVCRT__unlock_file(file
);
4234 pos
-= file
->_bufsiz
;
4235 pos
+= file
->_ptr
- file
->_base
;
4237 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4238 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
4241 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4248 MSVCRT__unlock_file(file
);
4252 /*********************************************************************
4255 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4257 return MSVCRT__ftelli64(file
);
4260 /*********************************************************************
4261 * fgetpos (MSVCRT.@)
4263 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4265 *pos
= MSVCRT__ftelli64(file
);
4271 /*********************************************************************
4274 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4276 MSVCRT_size_t len
= strlen(s
);
4279 MSVCRT__lock_file(file
);
4280 ret
= MSVCRT_fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4281 MSVCRT__unlock_file(file
);
4285 /*********************************************************************
4288 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4290 MSVCRT_size_t i
, len
= strlenW(s
);
4294 MSVCRT__lock_file(file
);
4295 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4296 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4297 MSVCRT__unlock_file(file
);
4301 tmp_buf
= add_std_buffer(file
);
4302 for (i
=0; i
<len
; i
++) {
4303 if(MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
) {
4304 if(tmp_buf
) remove_std_buffer(file
);
4305 MSVCRT__unlock_file(file
);
4310 if(tmp_buf
) remove_std_buffer(file
);
4311 MSVCRT__unlock_file(file
);
4315 /*********************************************************************
4316 * getchar (MSVCRT.@)
4318 int CDECL
MSVCRT_getchar(void)
4320 return MSVCRT_fgetc(MSVCRT_stdin
);
4323 /*********************************************************************
4326 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4328 return MSVCRT_fgetc(file
);
4331 /*********************************************************************
4334 char * CDECL
MSVCRT_gets(char *buf
)
4337 char * buf_start
= buf
;
4339 MSVCRT__lock_file(MSVCRT_stdin
);
4340 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4341 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
4342 if(cc
!= '\r') *buf
++ = (char)cc
;
4346 TRACE("got '%s'\n", buf_start
);
4347 MSVCRT__unlock_file(MSVCRT_stdin
);
4351 /*********************************************************************
4354 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4357 MSVCRT_wchar_t
* ws
= buf
;
4359 MSVCRT__lock_file(MSVCRT_stdin
);
4360 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4361 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
4364 *buf
++ = (MSVCRT_wchar_t
)cc
;
4368 TRACE("got %s\n", debugstr_w(ws
));
4369 MSVCRT__unlock_file(MSVCRT_stdin
);
4373 /*********************************************************************
4376 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4378 return MSVCRT_fputc(c
, file
);
4381 /*********************************************************************
4382 * putchar (MSVCRT.@)
4384 int CDECL
MSVCRT_putchar(int c
)
4386 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4389 /*********************************************************************
4390 * _putwch (MSVCRT.@)
4392 int CDECL
MSVCRT__putwch(int c
)
4394 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4397 /*********************************************************************
4400 int CDECL
MSVCRT_puts(const char *s
)
4402 MSVCRT_size_t len
= strlen(s
);
4405 MSVCRT__lock_file(MSVCRT_stdout
);
4406 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4407 MSVCRT__unlock_file(MSVCRT_stdout
);
4411 ret
= MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4412 MSVCRT__unlock_file(MSVCRT_stdout
);
4416 /*********************************************************************
4419 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4421 static const MSVCRT_wchar_t nl
= '\n';
4422 MSVCRT_size_t len
= strlenW(s
);
4425 MSVCRT__lock_file(MSVCRT_stdout
);
4426 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4427 MSVCRT__unlock_file(MSVCRT_stdout
);
4431 ret
= MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4432 MSVCRT__unlock_file(MSVCRT_stdout
);
4436 /*********************************************************************
4439 int CDECL
MSVCRT_remove(const char *path
)
4441 TRACE("(%s)\n",path
);
4442 if (DeleteFileA(path
))
4444 TRACE(":failed (%d)\n",GetLastError());
4445 msvcrt_set_errno(GetLastError());
4449 /*********************************************************************
4450 * _wremove (MSVCRT.@)
4452 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4454 TRACE("(%s)\n",debugstr_w(path
));
4455 if (DeleteFileW(path
))
4457 TRACE(":failed (%d)\n",GetLastError());
4458 msvcrt_set_errno(GetLastError());
4462 /*********************************************************************
4465 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4467 TRACE(":from %s to %s\n",oldpath
,newpath
);
4468 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4470 TRACE(":failed (%d)\n",GetLastError());
4471 msvcrt_set_errno(GetLastError());
4475 /*********************************************************************
4476 * _wrename (MSVCRT.@)
4478 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4480 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4481 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4483 TRACE(":failed (%d)\n",GetLastError());
4484 msvcrt_set_errno(GetLastError());
4488 /*********************************************************************
4489 * setvbuf (MSVCRT.@)
4491 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4493 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4494 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4495 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4497 MSVCRT__lock_file(file
);
4499 MSVCRT_fflush(file
);
4500 if(file
->_flag
& MSVCRT__IOMYBUF
)
4501 MSVCRT_free(file
->_base
);
4502 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4505 if(mode
== MSVCRT__IONBF
) {
4506 file
->_flag
|= MSVCRT__IONBF
;
4507 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4510 file
->_base
= file
->_ptr
= buf
;
4511 file
->_flag
|= MSVCRT__USERBUF
;
4512 file
->_bufsiz
= size
;
4514 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4517 MSVCRT__unlock_file(file
);
4521 file
->_flag
|= MSVCRT__IOMYBUF
;
4522 file
->_bufsiz
= size
;
4524 MSVCRT__unlock_file(file
);
4528 /*********************************************************************
4531 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4533 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4536 /*********************************************************************
4539 char * CDECL
MSVCRT_tmpnam(char *s
)
4546 thread_data_t
*data
= msvcrt_get_thread_data();
4548 if(!data
->tmpnam_buffer
)
4549 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4551 s
= data
->tmpnam_buffer
;
4554 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4555 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4556 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4558 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4559 memcpy(p
, tmpstr
, size
);
4561 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4562 GetLastError() == ERROR_FILE_NOT_FOUND
)
4568 /*********************************************************************
4569 * _wtmpnam (MSVCRT.@)
4571 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4573 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4574 MSVCRT_wchar_t tmpstr
[16];
4578 thread_data_t
*data
= msvcrt_get_thread_data();
4580 if(!data
->wtmpnam_buffer
)
4581 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4583 s
= data
->wtmpnam_buffer
;
4586 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4587 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4588 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4590 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4591 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4593 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4594 GetLastError() == ERROR_FILE_NOT_FOUND
)
4600 /*********************************************************************
4601 * tmpfile (MSVCRT.@)
4603 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4605 char *filename
= MSVCRT_tmpnam(NULL
);
4607 MSVCRT_FILE
* file
= NULL
;
4610 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4611 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4612 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4614 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4619 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4622 if(fd
!= -1 && !file
)
4628 /*********************************************************************
4629 * tmpfile_s (MSVCRT.@)
4631 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4633 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4635 *file
= MSVCRT_tmpfile();
4639 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4641 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4644 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4648 MSVCRT__lock_file(file
);
4650 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4651 ret
= MSVCRT_fwrite(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4652 MSVCRT__unlock_file(file
);
4656 for(i
=0; i
<len
; i
++) {
4657 if(MSVCRT_fputwc(str
[i
], file
) == MSVCRT_WEOF
) {
4658 MSVCRT__unlock_file(file
);
4663 MSVCRT__unlock_file(file
);
4667 /*********************************************************************
4668 * vfprintf (MSVCRT.@)
4670 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4675 MSVCRT__lock_file(file
);
4676 tmp_buf
= add_std_buffer(file
);
4677 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4678 if(tmp_buf
) remove_std_buffer(file
);
4679 MSVCRT__unlock_file(file
);
4684 /*********************************************************************
4685 * vfprintf_s (MSVCRT.@)
4687 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4692 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4694 MSVCRT__lock_file(file
);
4695 tmp_buf
= add_std_buffer(file
);
4696 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4697 if(tmp_buf
) remove_std_buffer(file
);
4698 MSVCRT__unlock_file(file
);
4703 /*********************************************************************
4704 * vfwprintf (MSVCRT.@)
4706 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4711 MSVCRT__lock_file(file
);
4712 tmp_buf
= add_std_buffer(file
);
4713 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4714 if(tmp_buf
) remove_std_buffer(file
);
4715 MSVCRT__unlock_file(file
);
4720 /*********************************************************************
4721 * vfwprintf_s (MSVCRT.@)
4723 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4728 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4730 MSVCRT__lock_file(file
);
4731 tmp_buf
= add_std_buffer(file
);
4732 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4733 if(tmp_buf
) remove_std_buffer(file
);
4734 MSVCRT__unlock_file(file
);
4739 /*********************************************************************
4740 * _vfwprintf_l (MSVCRT.@)
4742 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4743 MSVCRT__locale_t locale
, __ms_va_list valist
)
4748 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4750 MSVCRT__lock_file(file
);
4751 tmp_buf
= add_std_buffer(file
);
4752 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4753 if(tmp_buf
) remove_std_buffer(file
);
4754 MSVCRT__unlock_file(file
);
4759 /*********************************************************************
4760 * vprintf (MSVCRT.@)
4762 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4764 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4767 /*********************************************************************
4768 * vprintf_s (MSVCRT.@)
4770 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4772 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4775 /*********************************************************************
4776 * vwprintf (MSVCRT.@)
4778 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4780 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4783 /*********************************************************************
4784 * vwprintf_s (MSVCRT.@)
4786 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4788 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4791 /*********************************************************************
4792 * fprintf (MSVCRT.@)
4794 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4796 __ms_va_list valist
;
4798 __ms_va_start(valist
, format
);
4799 res
= MSVCRT_vfprintf(file
, format
, valist
);
4800 __ms_va_end(valist
);
4804 /*********************************************************************
4805 * fprintf_s (MSVCRT.@)
4807 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
4809 __ms_va_list valist
;
4811 __ms_va_start(valist
, format
);
4812 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
4813 __ms_va_end(valist
);
4817 /*********************************************************************
4818 * fwprintf (MSVCRT.@)
4820 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4822 __ms_va_list valist
;
4824 __ms_va_start(valist
, format
);
4825 res
= MSVCRT_vfwprintf(file
, format
, valist
);
4826 __ms_va_end(valist
);
4830 /*********************************************************************
4831 * fwprintf_s (MSVCRT.@)
4833 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4835 __ms_va_list valist
;
4837 __ms_va_start(valist
, format
);
4838 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
4839 __ms_va_end(valist
);
4843 /*********************************************************************
4844 * _fwprintf_l (MSVCRT.@)
4846 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
4848 __ms_va_list valist
;
4850 __ms_va_start(valist
, locale
);
4851 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
4852 __ms_va_end(valist
);
4856 /*********************************************************************
4859 int CDECL
MSVCRT_printf(const char *format
, ...)
4861 __ms_va_list valist
;
4863 __ms_va_start(valist
, format
);
4864 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
4865 __ms_va_end(valist
);
4869 /*********************************************************************
4870 * printf_s (MSVCRT.@)
4872 int CDECL
MSVCRT_printf_s(const char *format
, ...)
4874 __ms_va_list valist
;
4876 __ms_va_start(valist
, format
);
4877 res
= MSVCRT_vprintf_s(format
, valist
);
4878 __ms_va_end(valist
);
4882 /*********************************************************************
4885 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
4887 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
4889 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
4890 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
4893 MSVCRT__lock_file(file
);
4894 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
4895 && msvcrt_alloc_buffer(file
))
4896 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
4899 if(file
->_ptr
>file
->_base
) {
4901 if(file
->_flag
& MSVCRT__IOSTRG
) {
4902 if(*file
->_ptr
!= c
) {
4904 MSVCRT__unlock_file(file
);
4911 MSVCRT_clearerr(file
);
4912 file
->_flag
|= MSVCRT__IOREAD
;
4913 MSVCRT__unlock_file(file
);
4917 MSVCRT__unlock_file(file
);
4921 /*********************************************************************
4922 * ungetwc (MSVCRT.@)
4924 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4926 MSVCRT_wchar_t mwc
= wc
;
4928 if (wc
== MSVCRT_WEOF
)
4931 MSVCRT__lock_file(file
);
4933 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
4934 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4935 unsigned char * pp
= (unsigned char *)&mwc
;
4938 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
4939 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
)) {
4940 MSVCRT__unlock_file(file
);
4945 char mbs
[MSVCRT_MB_LEN_MAX
];
4948 len
= MSVCRT_wctomb(mbs
, mwc
);
4950 MSVCRT__unlock_file(file
);
4954 for(len
--; len
>=0; len
--) {
4955 if(mbs
[len
] != MSVCRT_ungetc(mbs
[len
], file
)) {
4956 MSVCRT__unlock_file(file
);
4962 MSVCRT__unlock_file(file
);
4966 /*********************************************************************
4967 * wprintf (MSVCRT.@)
4969 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
4971 __ms_va_list valist
;
4973 __ms_va_start(valist
, format
);
4974 res
= MSVCRT_vwprintf(format
, valist
);
4975 __ms_va_end(valist
);
4979 /*********************************************************************
4980 * wprintf_s (MSVCRT.@)
4982 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
4984 __ms_va_list valist
;
4986 __ms_va_start(valist
, format
);
4987 res
= MSVCRT_vwprintf_s(format
, valist
);
4988 __ms_va_end(valist
);
4992 /*********************************************************************
4993 * _getmaxstdio (MSVCRT.@)
4995 int CDECL
MSVCRT__getmaxstdio(void)
4997 return MSVCRT_max_streams
;
5000 /*********************************************************************
5001 * _setmaxstdio (MSVCRT.@)
5003 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5005 TRACE("%d\n", newmax
);
5007 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5010 MSVCRT_max_streams
= newmax
;
5011 return MSVCRT_max_streams
;