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
)
925 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
928 MSVCRT__lock_file(file
);
929 ret
= MSVCRT__fflush_nolock(file
);
930 MSVCRT__unlock_file(file
);
936 /*********************************************************************
937 * _fflush_nolock (MSVCRT.@)
939 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
942 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
943 } else if(file
->_flag
& MSVCRT__IOWRT
) {
946 res
= msvcrt_flush_buffer(file
);
947 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
948 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
951 } else if(file
->_flag
& MSVCRT__IOREAD
) {
953 file
->_ptr
= file
->_base
;
960 /*********************************************************************
963 int CDECL
MSVCRT__close(int fd
)
969 hand
= msvcrt_fdtoh(fd
);
970 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
971 if (!msvcrt_is_valid_fd(fd
)) {
975 ret
= CloseHandle(hand
) ? 0 : -1;
977 WARN(":failed-last error (%d)\n",GetLastError());
978 msvcrt_set_errno(GetLastError());
986 /*********************************************************************
989 * MSDN isn't clear on this point, but the remarks for _pipe
990 * indicate file descriptors duplicated with _dup and _dup2 are always
993 int CDECL
MSVCRT__dup2(int od
, int nd
)
997 TRACE("(od=%d, nd=%d)\n", od
, nd
);
999 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
1003 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od
)->handle
,
1004 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1006 int wxflag
= msvcrt_get_ioinfo(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
1008 if (msvcrt_is_valid_fd(nd
))
1010 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
1013 CloseHandle(handle
);
1014 *MSVCRT__errno() = MSVCRT_EMFILE
;
1018 /* _dup2 returns 0, not nd, on success */
1025 msvcrt_set_errno(GetLastError());
1030 *MSVCRT__errno() = MSVCRT_EBADF
;
1037 /*********************************************************************
1040 int CDECL
MSVCRT__dup(int od
)
1045 fd
= MSVCRT_fdstart
;
1046 if (MSVCRT__dup2(od
, fd
) == 0)
1054 /*********************************************************************
1057 int CDECL
MSVCRT__eof(int fd
)
1059 DWORD curpos
,endpos
;
1060 LONG hcurpos
,hendpos
;
1061 HANDLE hand
= msvcrt_fdtoh(fd
);
1063 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1065 if (hand
== INVALID_HANDLE_VALUE
)
1068 if (msvcrt_get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1070 /* Otherwise we do it the hard way */
1071 hcurpos
= hendpos
= 0;
1072 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1073 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1075 if (curpos
== endpos
&& hcurpos
== hendpos
)
1077 /* FIXME: shouldn't WX_ATEOF be set here? */
1081 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1085 /*********************************************************************
1086 * _fcloseall (MSVCRT.@)
1088 int CDECL
MSVCRT__fcloseall(void)
1090 int num_closed
= 0, i
;
1094 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1095 file
= msvcrt_get_file(i
);
1097 if (file
->_flag
&& !MSVCRT_fclose(file
))
1102 TRACE(":closed (%d) handles\n",num_closed
);
1106 /* free everything on process exit */
1107 void msvcrt_free_io(void)
1113 MSVCRT__fcloseall();
1115 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1116 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1118 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1120 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1121 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1123 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1124 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1128 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1129 MSVCRT_free(MSVCRT_fstream
[i
]);
1131 DeleteCriticalSection(&MSVCRT_file_cs
);
1134 /*********************************************************************
1135 * _lseeki64 (MSVCRT.@)
1137 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1139 HANDLE hand
= msvcrt_fdtoh(fd
);
1142 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1143 if (hand
== INVALID_HANDLE_VALUE
)
1146 if (whence
< 0 || whence
> 2)
1148 *MSVCRT__errno() = MSVCRT_EINVAL
;
1152 TRACE(":fd (%d) to %s pos %s\n",
1153 fd
,wine_dbgstr_longlong(offset
),
1154 (whence
==SEEK_SET
)?"SEEK_SET":
1155 (whence
==SEEK_CUR
)?"SEEK_CUR":
1156 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1158 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1159 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1160 ofs
.QuadPart
= offset
;
1161 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1162 GetLastError() == ERROR_SUCCESS
)
1164 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_ATEOF
;
1165 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1167 return ofs
.QuadPart
;
1169 TRACE(":error-last error (%d)\n",GetLastError());
1170 msvcrt_set_errno(GetLastError());
1174 /*********************************************************************
1177 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1179 return MSVCRT__lseeki64(fd
, offset
, whence
);
1182 /*********************************************************************
1183 * _lock_file (MSVCRT.@)
1185 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1187 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1188 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1190 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1193 /*********************************************************************
1194 * _unlock_file (MSVCRT.@)
1196 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1198 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1199 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1201 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1204 /*********************************************************************
1205 * _locking (MSVCRT.@)
1207 * This is untested; the underlying LockFile doesn't work yet.
1209 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1213 HANDLE hand
= msvcrt_fdtoh(fd
);
1215 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1216 if (hand
== INVALID_HANDLE_VALUE
)
1219 if (mode
< 0 || mode
> 4)
1221 *MSVCRT__errno() = MSVCRT_EINVAL
;
1225 TRACE(":fd (%d) by 0x%08x mode %s\n",
1226 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1227 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1228 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1229 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1230 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1233 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1235 FIXME ("Seek failed\n");
1236 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1239 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1242 ret
= 1; /* just to satisfy gcc */
1245 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1250 else if (mode
== MSVCRT__LK_UNLCK
)
1251 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1253 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1254 /* FIXME - what about error settings? */
1255 return ret
? 0 : -1;
1258 /*********************************************************************
1259 * _fseeki64 (MSVCRT.@)
1261 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1265 MSVCRT__lock_file(file
);
1266 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1267 MSVCRT__unlock_file(file
);
1272 /*********************************************************************
1273 * _fseeki64_nolock (MSVCRT.@)
1275 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1279 /* Flush output if needed */
1280 if(file
->_flag
& MSVCRT__IOWRT
)
1281 msvcrt_flush_buffer(file
);
1283 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1285 offset
+= MSVCRT__ftelli64_nolock(file
);
1288 /* Discard buffered input */
1290 file
->_ptr
= file
->_base
;
1291 /* Reset direction of i/o */
1292 if(file
->_flag
& MSVCRT__IORW
) {
1293 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1295 /* Clear end of file flag */
1296 file
->_flag
&= ~MSVCRT__IOEOF
;
1297 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1302 /*********************************************************************
1305 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1307 return MSVCRT__fseeki64( file
, offset
, whence
);
1310 /*********************************************************************
1311 * _fseek_nolock (MSVCRT.@)
1313 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1315 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1318 /*********************************************************************
1319 * _chsize_s (MSVCRT.@)
1321 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1327 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1329 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1333 handle
= msvcrt_fdtoh(fd
);
1334 if (handle
!= INVALID_HANDLE_VALUE
)
1336 /* save the current file pointer */
1337 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1340 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1343 ret
= SetEndOfFile(handle
);
1344 if (!ret
) msvcrt_set_errno(GetLastError());
1347 /* restore the file pointer */
1348 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1353 return ret
? 0 : *MSVCRT__errno();
1356 /*********************************************************************
1357 * _chsize (MSVCRT.@)
1359 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1361 /* _chsize_s returns errno on failure but _chsize should return -1 */
1362 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1365 /*********************************************************************
1366 * clearerr (MSVCRT.@)
1368 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1370 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1372 MSVCRT__lock_file(file
);
1373 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1374 MSVCRT__unlock_file(file
);
1377 /*********************************************************************
1380 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1382 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1384 MSVCRT__lock_file(file
);
1385 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1386 MSVCRT_clearerr(file
);
1387 MSVCRT__unlock_file(file
);
1390 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1392 int plus
= strchrW(mode
, '+') != NULL
;
1394 TRACE("%s\n", debugstr_w(mode
));
1396 while(*mode
== ' ') mode
++;
1401 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1402 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1405 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1406 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1409 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1410 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1413 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1417 *stream_flags
|= MSVCRT__commode
;
1419 while (*mode
&& *mode
!=',')
1423 *open_flags
|= MSVCRT__O_BINARY
;
1424 *open_flags
&= ~MSVCRT__O_TEXT
;
1427 *open_flags
|= MSVCRT__O_TEXT
;
1428 *open_flags
&= ~MSVCRT__O_BINARY
;
1431 *open_flags
|= MSVCRT__O_TEMPORARY
;
1434 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1437 *stream_flags
|= MSVCRT__IOCOMMIT
;
1440 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1443 *open_flags
|= MSVCRT__O_NOINHERIT
;
1452 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1455 ERR("incorrect mode flag: %c\n", mode
[-1]);
1461 static const WCHAR ccs
[] = {'c','c','s'};
1462 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1463 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1464 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1467 while(*mode
== ' ') mode
++;
1468 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1470 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1471 while(*mode
== ' ') mode
++;
1472 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1475 while(*mode
== ' ') mode
++;
1477 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1479 *open_flags
|= MSVCRT__O_U8TEXT
;
1480 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1482 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1484 *open_flags
|= MSVCRT__O_U16TEXT
;
1485 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1487 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1489 *open_flags
|= MSVCRT__O_WTEXT
;
1490 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1494 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1498 while(*mode
== ' ') mode
++;
1501 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1506 /*********************************************************************
1507 * _fdopen (MSVCRT.@)
1509 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1512 MSVCRT_wchar_t
*modeW
= NULL
;
1514 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1516 ret
= MSVCRT__wfdopen(fd
, modeW
);
1522 /*********************************************************************
1523 * _wfdopen (MSVCRT.@)
1525 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1527 int open_flags
, stream_flags
;
1530 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1533 if (!(file
= msvcrt_alloc_fp()))
1535 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1540 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1546 /*********************************************************************
1547 * _filelength (MSVCRT.@)
1549 LONG CDECL
MSVCRT__filelength(int fd
)
1551 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1554 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1557 if (endPos
!= curPos
)
1558 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1565 /*********************************************************************
1566 * _filelengthi64 (MSVCRT.@)
1568 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1570 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1573 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1576 if (endPos
!= curPos
)
1577 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1584 /*********************************************************************
1585 * _fileno (MSVCRT.@)
1587 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1589 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1593 /*********************************************************************
1594 * _fstat64 (MSVCRT.@)
1596 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1600 BY_HANDLE_FILE_INFORMATION hfi
;
1601 HANDLE hand
= msvcrt_fdtoh(fd
);
1603 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1604 if (hand
== INVALID_HANDLE_VALUE
)
1609 WARN(":failed-NULL buf\n");
1610 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1614 memset(&hfi
, 0, sizeof(hfi
));
1615 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1616 type
= GetFileType(hand
);
1617 if (type
== FILE_TYPE_PIPE
)
1619 buf
->st_dev
= buf
->st_rdev
= fd
;
1620 buf
->st_mode
= S_IFIFO
;
1623 else if (type
== FILE_TYPE_CHAR
)
1625 buf
->st_dev
= buf
->st_rdev
= fd
;
1626 buf
->st_mode
= S_IFCHR
;
1629 else /* FILE_TYPE_DISK etc. */
1631 if (!GetFileInformationByHandle(hand
, &hfi
))
1633 WARN(":failed-last error (%d)\n",GetLastError());
1634 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1637 buf
->st_mode
= S_IFREG
| 0444;
1638 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1639 buf
->st_mode
|= 0222;
1640 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1641 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1643 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1644 buf
->st_mtime
= buf
->st_ctime
= dw
;
1645 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1647 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1652 /*********************************************************************
1653 * _fstati64 (MSVCRT.@)
1655 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1658 struct MSVCRT__stat64 buf64
;
1660 ret
= MSVCRT__fstat64(fd
, &buf64
);
1662 msvcrt_stat64_to_stati64(&buf64
, buf
);
1666 /*********************************************************************
1669 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1671 struct MSVCRT__stat64 buf64
;
1673 ret
= MSVCRT__fstat64(fd
, &buf64
);
1675 msvcrt_stat64_to_stat(&buf64
, buf
);
1679 /*********************************************************************
1680 * _fstat32 (MSVCR80.@)
1682 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1685 struct MSVCRT__stat64 buf64
;
1687 ret
= MSVCRT__fstat64(fd
, &buf64
);
1689 msvcrt_stat64_to_stat32(&buf64
, buf
);
1693 /*********************************************************************
1694 * _fstat64i32 (MSVCR80.@)
1696 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1699 struct MSVCRT__stat64 buf64
;
1701 ret
= MSVCRT__fstat64(fd
, &buf64
);
1703 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1707 /*********************************************************************
1708 * _futime64 (MSVCRT.@)
1710 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1712 HANDLE hand
= msvcrt_fdtoh(fd
);
1717 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1722 time_to_filetime( t
->actime
, &at
);
1723 time_to_filetime( t
->modtime
, &wt
);
1726 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1728 msvcrt_set_errno(GetLastError());
1734 /*********************************************************************
1735 * _futime32 (MSVCRT.@)
1737 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1741 struct MSVCRT___utimbuf64 t64
;
1742 t64
.actime
= t
->actime
;
1743 t64
.modtime
= t
->modtime
;
1744 return _futime64( fd
, &t64
);
1747 return _futime64( fd
, NULL
);
1750 /*********************************************************************
1751 * _futime (MSVCRT.@)
1754 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1756 return _futime64( fd
, t
);
1759 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1761 return _futime32( fd
, t
);
1765 /*********************************************************************
1766 * _get_osfhandle (MSVCRT.@)
1768 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1770 HANDLE hand
= msvcrt_fdtoh(fd
);
1771 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1773 return (MSVCRT_intptr_t
)hand
;
1776 /*********************************************************************
1777 * _mktemp_s (MSVCRT.@)
1779 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1783 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1784 return MSVCRT_EINVAL
;
1786 for(len
=0; len
<size
; len
++)
1789 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1792 return MSVCRT_EINVAL
;
1795 for(xno
=1; xno
<=6; xno
++)
1796 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1797 return MSVCRT_EINVAL
;
1799 id
= GetCurrentProcessId();
1800 for(xno
=1; xno
<6; xno
++) {
1801 pattern
[len
-xno
] = id
%10 + '0';
1805 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1806 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1811 *MSVCRT__errno() = MSVCRT_EEXIST
;
1812 return MSVCRT_EEXIST
;
1815 /*********************************************************************
1816 * _mktemp (MSVCRT.@)
1818 char * CDECL
MSVCRT__mktemp(char *pattern
)
1821 char *retVal
= pattern
;
1829 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1833 id
= GetCurrentProcessId();
1837 int tempNum
= id
/ 10;
1838 *pattern
-- = id
- (tempNum
* 10) + '0';
1844 *pattern
= letter
++;
1845 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1847 } while(letter
<= 'z');
1851 /*********************************************************************
1852 * _wmktemp_s (MSVCRT.@)
1854 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1858 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1859 return MSVCRT_EINVAL
;
1861 for(len
=0; len
<size
; len
++)
1864 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1867 return MSVCRT_EINVAL
;
1870 for(xno
=1; xno
<=6; xno
++)
1871 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1872 return MSVCRT_EINVAL
;
1874 id
= GetCurrentProcessId();
1875 for(xno
=1; xno
<6; xno
++) {
1876 pattern
[len
-xno
] = id
%10 + '0';
1880 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1881 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1886 *MSVCRT__errno() = MSVCRT_EEXIST
;
1887 return MSVCRT_EEXIST
;
1890 /*********************************************************************
1891 * _wmktemp (MSVCRT.@)
1893 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1896 MSVCRT_wchar_t
*retVal
= pattern
;
1898 MSVCRT_wchar_t letter
= 'a';
1904 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1908 id
= GetCurrentProcessId();
1912 int tempNum
= id
/ 10;
1913 *pattern
-- = id
- (tempNum
* 10) + '0';
1919 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1921 *pattern
= letter
++;
1922 } while(letter
!= '|');
1926 static unsigned split_oflags(unsigned oflags
)
1929 unsigned unsupp
; /* until we support everything */
1931 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1932 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1933 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1934 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1935 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1936 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1937 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1938 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1939 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1941 if ((unsupp
= oflags
& ~(
1942 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1943 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1944 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1945 MSVCRT__O_NOINHERIT
|
1946 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
1947 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
1949 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1954 /*********************************************************************
1957 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1960 SECURITY_ATTRIBUTES sa
;
1961 HANDLE readHandle
, writeHandle
;
1965 *MSVCRT__errno() = MSVCRT_EINVAL
;
1969 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1970 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1971 sa
.lpSecurityDescriptor
= NULL
;
1972 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1974 unsigned int wxflags
= split_oflags(textmode
);
1978 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
1982 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
1990 MSVCRT__close(pfds
[0]);
1991 CloseHandle(writeHandle
);
1992 *MSVCRT__errno() = MSVCRT_EMFILE
;
1997 CloseHandle(readHandle
);
1998 CloseHandle(writeHandle
);
1999 *MSVCRT__errno() = MSVCRT_EMFILE
;
2004 msvcrt_set_errno(GetLastError());
2009 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2011 char bom
[sizeof(utf8_bom
)];
2014 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2016 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2019 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2020 oflags
|= MSVCRT__O_U8TEXT
;
2021 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2023 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2024 oflags
|= MSVCRT__O_U16TEXT
;
2026 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2032 /*********************************************************************
2033 * _wsopen_s (MSVCRT.@)
2035 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2037 DWORD access
= 0, creation
= 0, attrib
;
2038 SECURITY_ATTRIBUTES sa
;
2039 DWORD sharing
, type
;
2043 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2044 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2046 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2049 wxflag
= split_oflags(oflags
);
2050 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2052 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2053 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2054 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2057 if (oflags
& MSVCRT__O_CREAT
)
2059 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2060 FIXME(": pmode 0x%04x ignored\n", pmode
);
2062 WARN(": pmode 0x%04x ignored\n", pmode
);
2064 if (oflags
& MSVCRT__O_EXCL
)
2065 creation
= CREATE_NEW
;
2066 else if (oflags
& MSVCRT__O_TRUNC
)
2067 creation
= CREATE_ALWAYS
;
2069 creation
= OPEN_ALWAYS
;
2071 else /* no MSVCRT__O_CREAT */
2073 if (oflags
& MSVCRT__O_TRUNC
)
2074 creation
= TRUNCATE_EXISTING
;
2076 creation
= OPEN_EXISTING
;
2081 case MSVCRT__SH_DENYRW
:
2084 case MSVCRT__SH_DENYWR
:
2085 sharing
= FILE_SHARE_READ
;
2087 case MSVCRT__SH_DENYRD
:
2088 sharing
= FILE_SHARE_WRITE
;
2090 case MSVCRT__SH_DENYNO
:
2091 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2094 ERR( "Unhandled shflags 0x%x\n", shflags
);
2095 return MSVCRT_EINVAL
;
2097 attrib
= FILE_ATTRIBUTE_NORMAL
;
2099 if (oflags
& MSVCRT__O_TEMPORARY
)
2101 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2103 sharing
|= FILE_SHARE_DELETE
;
2106 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2107 sa
.lpSecurityDescriptor
= NULL
;
2108 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2110 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2111 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2112 && !(access
&GENERIC_READ
))
2114 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2115 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2116 if (hand
!= INVALID_HANDLE_VALUE
)
2118 oflags
= check_bom(hand
, oflags
, FALSE
);
2122 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2125 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2126 if (hand
== INVALID_HANDLE_VALUE
) {
2127 WARN(":failed-last error (%d)\n",GetLastError());
2128 msvcrt_set_errno(GetLastError());
2129 return *MSVCRT__errno();
2132 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2134 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2135 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2136 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2138 if (oflags
& MSVCRT__O_U8TEXT
)
2140 DWORD written
= 0, tmp
;
2142 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2143 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2145 if (written
!= sizeof(utf8_bom
)) {
2146 WARN("error writing BOM\n");
2148 msvcrt_set_errno(GetLastError());
2149 return *MSVCRT__errno();
2154 DWORD written
= 0, tmp
;
2156 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2157 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2159 if (written
!= sizeof(utf16_bom
))
2161 WARN("error writing BOM\n");
2163 msvcrt_set_errno(GetLastError());
2164 return *MSVCRT__errno();
2168 else if (access
& GENERIC_READ
)
2169 oflags
= check_bom(hand
, oflags
, TRUE
);
2172 type
= GetFileType(hand
);
2173 if (type
== FILE_TYPE_CHAR
)
2175 else if (type
== FILE_TYPE_PIPE
)
2178 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2180 return *MSVCRT__errno();
2182 if (oflags
& MSVCRT__O_WTEXT
)
2183 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2184 else if (oflags
& MSVCRT__O_U16TEXT
)
2185 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
2186 else if (oflags
& MSVCRT__O_U8TEXT
)
2187 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
2189 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2193 /*********************************************************************
2194 * _wsopen (MSVCRT.@)
2196 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2201 if (oflags
& MSVCRT__O_CREAT
)
2205 __ms_va_start(ap
, shflags
);
2206 pmode
= va_arg(ap
, int);
2212 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2216 /*********************************************************************
2217 * _sopen_s (MSVCRT.@)
2219 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2221 MSVCRT_wchar_t
*pathW
;
2224 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2225 return MSVCRT_EINVAL
;
2227 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2228 return MSVCRT_EINVAL
;
2230 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2235 /*********************************************************************
2238 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2243 if (oflags
& MSVCRT__O_CREAT
)
2247 __ms_va_start(ap
, shflags
);
2248 pmode
= va_arg(ap
, int);
2254 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2258 /*********************************************************************
2261 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2265 if (flags
& MSVCRT__O_CREAT
)
2268 __ms_va_start(ap
, flags
);
2269 pmode
= va_arg(ap
, int);
2271 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2274 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2277 /*********************************************************************
2280 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2284 if (flags
& MSVCRT__O_CREAT
)
2287 __ms_va_start(ap
, flags
);
2288 pmode
= va_arg(ap
, int);
2290 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2293 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2296 /*********************************************************************
2299 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2301 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2302 return MSVCRT__open(path
, usedFlags
);
2305 /*********************************************************************
2306 * _wcreat (MSVCRT.@)
2308 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2310 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2311 return MSVCRT__wopen(path
, usedFlags
);
2314 /*********************************************************************
2315 * _open_osfhandle (MSVCRT.@)
2317 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2322 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2323 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2324 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2325 * text - it never sets MSVCRT__O_BINARY.
2327 /* don't let split_oflags() decide the mode if no mode is passed */
2328 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2329 oflags
|= MSVCRT__O_BINARY
;
2331 flags
= GetFileType((HANDLE
)handle
);
2332 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2334 msvcrt_set_errno(GetLastError());
2338 if (flags
== FILE_TYPE_CHAR
)
2340 else if (flags
== FILE_TYPE_PIPE
)
2344 flags
|= split_oflags(oflags
);
2346 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2347 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2351 /*********************************************************************
2354 int CDECL
MSVCRT__rmtmp(void)
2356 int num_removed
= 0, i
;
2360 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2361 file
= msvcrt_get_file(i
);
2363 if (file
->_tmpfname
)
2365 MSVCRT_fclose(file
);
2372 TRACE(":removed (%d) temp files\n",num_removed
);
2376 static inline int get_utf8_char_len(char ch
)
2378 if((ch
&0xf8) == 0xf0)
2380 else if((ch
&0xf0) == 0xe0)
2382 else if((ch
&0xe0) == 0xc0)
2387 /*********************************************************************
2388 * (internal) read_utf8
2390 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2392 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2393 HANDLE hand
= fdinfo
->handle
;
2394 char min_buf
[4], *readbuf
, lookahead
;
2395 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2397 /* make the buffer big enough to hold at least one character */
2398 /* read bytes have to fit to output and lookahead buffers */
2400 readbuf_size
= count
< 4 ? 4 : count
;
2401 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2406 if(fdinfo
->lookahead
[0] != '\n') {
2407 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2408 fdinfo
->lookahead
[0] = '\n';
2410 if(fdinfo
->lookahead
[1] != '\n') {
2411 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2412 fdinfo
->lookahead
[1] = '\n';
2414 if(fdinfo
->lookahead
[2] != '\n') {
2415 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2416 fdinfo
->lookahead
[2] = '\n';
2421 /* NOTE: this case is broken in native dll, reading
2422 * sometimes fails when small buffer is passed
2425 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2426 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2427 fdinfo
->wxflag
|= WX_ATEOF
;
2430 msvcrt_set_errno(GetLastError());
2433 }else if(!num_read
) {
2434 fdinfo
->wxflag
|= WX_ATEOF
;
2440 char_len
= get_utf8_char_len(readbuf
[0]);
2442 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2446 if(readbuf
[0] == '\n')
2447 fdinfo
->wxflag
|= WX_READNL
;
2449 fdinfo
->wxflag
&= ~WX_READNL
;
2451 if(readbuf
[0] == 0x1a) {
2452 fdinfo
->wxflag
|= WX_ATEOF
;
2456 if(readbuf
[0] == '\r') {
2457 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2459 else if(lookahead
== '\n')
2463 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2464 fdinfo
->lookahead
[0] = lookahead
;
2466 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2471 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2472 msvcrt_set_errno(GetLastError());
2479 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2482 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2483 fdinfo
->wxflag
|= WX_ATEOF
;
2484 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2487 msvcrt_set_errno(GetLastError());
2488 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2491 }else if(!pos
&& !num_read
) {
2492 fdinfo
->wxflag
|= WX_ATEOF
;
2493 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2498 if(readbuf
[0] == '\n')
2499 fdinfo
->wxflag
|= WX_READNL
;
2501 fdinfo
->wxflag
&= ~WX_READNL
;
2503 /* Find first byte of last character (may be incomplete) */
2504 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2505 if((readbuf
[i
]&0xc0) != 0x80)
2507 char_len
= get_utf8_char_len(readbuf
[i
]);
2508 if(char_len
+i
<= pos
)
2511 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2513 fdinfo
->lookahead
[0] = readbuf
[i
];
2515 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2517 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2519 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2523 for(i
=0, j
=0; i
<pos
; i
++) {
2524 if(readbuf
[i
] == 0x1a) {
2525 fdinfo
->wxflag
|= WX_ATEOF
;
2529 /* strip '\r' if followed by '\n' */
2530 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2531 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2532 readbuf
[j
++] = '\r';
2533 }else if(lookahead
== '\n' && j
==0) {
2534 readbuf
[j
++] = '\n';
2536 if(lookahead
!= '\n')
2537 readbuf
[j
++] = '\r';
2539 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2540 fdinfo
->lookahead
[0] = lookahead
;
2542 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2544 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2545 readbuf
[j
++] = readbuf
[i
];
2550 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2551 msvcrt_set_errno(GetLastError());
2552 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2556 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2560 /*********************************************************************
2563 * When reading \r as last character in text mode, read() positions
2564 * the file pointer on the \r character while getc() goes on to
2567 static int read_i(int fd
, void *buf
, unsigned int count
)
2569 DWORD num_read
, utf16
;
2570 char *bufstart
= buf
;
2571 HANDLE hand
= msvcrt_fdtoh(fd
);
2572 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2577 if (fdinfo
->wxflag
& WX_ATEOF
) {
2578 TRACE("already at EOF, returning 0\n");
2581 /* Don't trace small reads, it gets *very* annoying */
2583 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2584 if (hand
== INVALID_HANDLE_VALUE
)
2586 *MSVCRT__errno() = MSVCRT_EBADF
;
2590 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2591 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2593 *MSVCRT__errno() = MSVCRT_EINVAL
;
2597 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2598 return read_utf8(fd
, buf
, count
);
2600 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2602 if (fdinfo
->lookahead
[0] != '\n')
2604 bufstart
[0] = fdinfo
->lookahead
[0];
2605 fdinfo
->lookahead
[0] = '\n';
2609 bufstart
[1] = fdinfo
->lookahead
[1];
2610 fdinfo
->lookahead
[1] = '\n';
2613 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2614 num_read
+= 1+utf16
;
2619 if(utf16
&& (num_read
&1))
2621 /* msvcr90 uses uninitialized value from the buffer in this case */
2622 /* msvcrt ignores additional data */
2623 ERR("got odd number of bytes in UTF16 mode\n");
2627 if (count
!= 0 && num_read
== 0)
2629 fdinfo
->wxflag
|= WX_ATEOF
;
2630 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2632 else if (fdinfo
->wxflag
& WX_TEXT
)
2636 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2637 fdinfo
->wxflag
|= WX_READNL
;
2639 fdinfo
->wxflag
&= ~WX_READNL
;
2641 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2643 /* in text mode, a ctrl-z signals EOF */
2644 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2646 fdinfo
->wxflag
|= WX_ATEOF
;
2647 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2651 /* in text mode, strip \r if followed by \n */
2652 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2657 lookahead
[1] = '\n';
2658 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2660 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2662 bufstart
[j
++] = '\n';
2663 if(utf16
) bufstart
[j
++] = 0;
2667 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2669 bufstart
[j
++] = '\r';
2670 if(utf16
) bufstart
[j
++] = 0;
2673 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2675 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2677 bufstart
[j
++] = '\n';
2678 if (utf16
) bufstart
[j
++] = 0;
2682 fdinfo
->lookahead
[0] = lookahead
[0];
2683 fdinfo
->lookahead
[1] = lookahead
[1];
2687 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2692 bufstart
[j
++] = '\r';
2693 if(utf16
) bufstart
[j
++] = 0;
2696 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2697 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2699 bufstart
[j
++] = bufstart
[i
];
2700 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2708 if (GetLastError() == ERROR_BROKEN_PIPE
)
2710 TRACE(":end-of-pipe\n");
2711 fdinfo
->wxflag
|= WX_ATEOF
;
2716 TRACE(":failed-last error (%d)\n",GetLastError());
2722 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2726 /*********************************************************************
2729 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2732 num_read
= read_i(fd
, buf
, count
);
2736 /*********************************************************************
2737 * _setmode (MSVCRT.@)
2739 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2741 int ret
= msvcrt_get_ioinfo(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2742 if(ret
==MSVCRT__O_TEXT
&& (msvcrt_get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2743 ret
= MSVCRT__O_WTEXT
;
2745 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2746 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2747 *MSVCRT__errno() = MSVCRT_EINVAL
;
2751 if(mode
== MSVCRT__O_BINARY
) {
2752 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2753 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2757 msvcrt_get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2758 if(mode
== MSVCRT__O_TEXT
)
2759 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2760 else if(mode
== MSVCRT__O_U8TEXT
)
2761 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2763 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2768 /*********************************************************************
2769 * _stat64 (MSVCRT.@)
2771 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2774 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2775 unsigned short mode
= ALL_S_IREAD
;
2778 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2780 plen
= strlen(path
);
2781 while (plen
&& path
[plen
-1]==' ')
2784 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2785 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2787 *MSVCRT__errno() = MSVCRT_ENOENT
;
2791 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2793 TRACE("failed (%d)\n",GetLastError());
2794 *MSVCRT__errno() = MSVCRT_ENOENT
;
2798 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2800 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2801 Bon 011120: This FIXME seems incorrect
2802 Also a letter as first char isn't enough to be classified
2805 if (isalpha(*path
)&& (*(path
+1)==':'))
2806 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2808 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2810 /* Dir, or regular file? */
2811 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2812 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2815 mode
|= MSVCRT__S_IFREG
;
2817 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2819 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2820 (tolower(path
[plen
-3]) << 16);
2821 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2822 mode
|= ALL_S_IEXEC
;
2826 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2827 mode
|= ALL_S_IWRITE
;
2829 buf
->st_mode
= mode
;
2831 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2832 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2834 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2835 buf
->st_mtime
= buf
->st_ctime
= dw
;
2836 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2837 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2838 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2842 /*********************************************************************
2843 * _stati64 (MSVCRT.@)
2845 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2848 struct MSVCRT__stat64 buf64
;
2850 ret
= MSVCRT_stat64(path
, &buf64
);
2852 msvcrt_stat64_to_stati64(&buf64
, buf
);
2856 /*********************************************************************
2859 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2862 struct MSVCRT__stat64 buf64
;
2864 ret
= MSVCRT_stat64( path
, &buf64
);
2866 msvcrt_stat64_to_stat(&buf64
, buf
);
2870 /*********************************************************************
2871 * _stat32 (MSVCR100.@)
2873 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2876 struct MSVCRT__stat64 buf64
;
2878 ret
= MSVCRT_stat64(path
, &buf64
);
2880 msvcrt_stat64_to_stat32(&buf64
, buf
);
2884 /*********************************************************************
2885 * _stat32i64 (MSVCR100.@)
2887 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2890 struct MSVCRT__stat64 buf64
;
2892 ret
= MSVCRT_stat64(path
, &buf64
);
2894 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2898 /*********************************************************************
2899 * _stat64i32 (MSVCR100.@)
2901 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2904 struct MSVCRT__stat64 buf64
;
2906 ret
= MSVCRT_stat64(path
, &buf64
);
2908 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
2912 /*********************************************************************
2913 * _wstat64 (MSVCRT.@)
2915 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2918 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2919 unsigned short mode
= ALL_S_IREAD
;
2922 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2924 plen
= strlenW(path
);
2925 while (plen
&& path
[plen
-1]==' ')
2928 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2929 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2931 *MSVCRT__errno() = MSVCRT_ENOENT
;
2935 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2937 TRACE("failed (%d)\n",GetLastError());
2938 *MSVCRT__errno() = MSVCRT_ENOENT
;
2942 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2944 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2945 if (MSVCRT_iswalpha(*path
))
2946 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2948 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2950 /* Dir, or regular file? */
2951 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2952 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2955 mode
|= MSVCRT__S_IFREG
;
2957 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2959 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2960 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2961 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2962 mode
|= ALL_S_IEXEC
;
2966 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2967 mode
|= ALL_S_IWRITE
;
2969 buf
->st_mode
= mode
;
2971 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2972 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2974 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2975 buf
->st_mtime
= buf
->st_ctime
= dw
;
2976 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2977 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2978 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2982 /*********************************************************************
2983 * _wstati64 (MSVCRT.@)
2985 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2988 struct MSVCRT__stat64 buf64
;
2990 ret
= MSVCRT__wstat64(path
, &buf64
);
2992 msvcrt_stat64_to_stati64(&buf64
, buf
);
2996 /*********************************************************************
2999 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3002 struct MSVCRT__stat64 buf64
;
3004 ret
= MSVCRT__wstat64( path
, &buf64
);
3005 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3009 /*********************************************************************
3010 * _wstat32 (MSVCR100.@)
3012 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3015 struct MSVCRT__stat64 buf64
;
3017 ret
= MSVCRT__wstat64(path
, &buf64
);
3019 msvcrt_stat64_to_stat32(&buf64
, buf
);
3023 /*********************************************************************
3024 * _wstat32i64 (MSVCR100.@)
3026 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3029 struct MSVCRT__stat64 buf64
;
3031 ret
= MSVCRT__wstat64(path
, &buf64
);
3033 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3037 /*********************************************************************
3038 * _wstat64i32 (MSVCR100.@)
3040 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3043 struct MSVCRT__stat64 buf64
;
3045 ret
= MSVCRT__wstat64(path
, &buf64
);
3047 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3051 /*********************************************************************
3054 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3056 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3059 /*********************************************************************
3060 * _telli64 (MSVCRT.@)
3062 __int64 CDECL
_telli64(int fd
)
3064 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3067 /*********************************************************************
3068 * _tempnam (MSVCRT.@)
3070 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3072 char tmpbuf
[MAX_PATH
];
3073 const char *tmp_dir
= MSVCRT_getenv("TMP");
3075 if (tmp_dir
) dir
= tmp_dir
;
3077 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3078 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3080 TRACE("got name (%s)\n",tmpbuf
);
3081 DeleteFileA(tmpbuf
);
3082 return MSVCRT__strdup(tmpbuf
);
3084 TRACE("failed (%d)\n",GetLastError());
3088 /*********************************************************************
3089 * _wtempnam (MSVCRT.@)
3091 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3093 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3094 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3095 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3097 if (tmp_dir
) dir
= tmp_dir
;
3099 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3100 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3102 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3103 DeleteFileW(tmpbuf
);
3104 return MSVCRT__wcsdup(tmpbuf
);
3106 TRACE("failed (%d)\n",GetLastError());
3110 /*********************************************************************
3113 int CDECL
MSVCRT__umask(int umask
)
3115 int old_umask
= MSVCRT_umask
;
3116 TRACE("(%d)\n",umask
);
3117 MSVCRT_umask
= umask
;
3121 /*********************************************************************
3122 * _utime64 (MSVCRT.@)
3124 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3126 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3130 int retVal
= _futime64(fd
, t
);
3137 /*********************************************************************
3138 * _utime32 (MSVCRT.@)
3140 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3144 struct MSVCRT___utimbuf64 t64
;
3145 t64
.actime
= t
->actime
;
3146 t64
.modtime
= t
->modtime
;
3147 return _utime64( path
, &t64
);
3150 return _utime64( path
, NULL
);
3153 /*********************************************************************
3157 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3159 return _utime64( path
, t
);
3162 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3164 return _utime32( path
, t
);
3168 /*********************************************************************
3169 * _wutime64 (MSVCRT.@)
3171 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3173 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3177 int retVal
= _futime64(fd
, t
);
3184 /*********************************************************************
3185 * _wutime32 (MSVCRT.@)
3187 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3191 struct MSVCRT___utimbuf64 t64
;
3192 t64
.actime
= t
->actime
;
3193 t64
.modtime
= t
->modtime
;
3194 return _wutime64( path
, &t64
);
3197 return _wutime64( path
, NULL
);
3200 /*********************************************************************
3201 * _wutime (MSVCRT.@)
3204 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3206 return _wutime64( path
, t
);
3209 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3211 return _wutime32( path
, t
);
3215 /*********************************************************************
3218 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3221 ioinfo
*info
= msvcrt_get_ioinfo(fd
);
3222 HANDLE hand
= info
->handle
;
3224 /* Don't trace small writes, it gets *very* annoying */
3227 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3229 if (hand
== INVALID_HANDLE_VALUE
)
3231 *MSVCRT__errno() = MSVCRT_EBADF
;
3235 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3237 *MSVCRT__errno() = MSVCRT_EINVAL
;
3241 /* If appending, go to EOF */
3242 if (info
->wxflag
& WX_APPEND
)
3243 MSVCRT__lseek(fd
, 0, FILE_END
);
3245 if (!(info
->wxflag
& WX_TEXT
))
3247 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3248 && (num_written
== count
))
3250 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3251 hand
, GetLastError());
3252 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3256 unsigned int i
, j
, nr_lf
, size
;
3259 const char *s
= buf
, *buf_start
= buf
;
3261 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3263 /* find number of \n */
3264 for (nr_lf
=0, i
=0; i
<count
; i
++)
3270 if ((q
= p
= MSVCRT_malloc(size
)))
3272 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3281 FIXME("Malloc failed\n");
3293 else if (info
->exflag
& EF_UTF16
)
3295 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3296 if (s
[i
]=='\n' && s
[i
+1]==0)
3301 if ((q
= p
= MSVCRT_malloc(size
)))
3303 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3305 if (s
[i
]=='\n' && s
[i
+1]==0)
3316 FIXME("Malloc failed\n");
3332 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3333 if (s
[i
]=='\n' && s
[i
+1]==0)
3336 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3338 msvcrt_set_errno(GetLastError());
3343 size
= conv_len
+nr_lf
;
3344 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3346 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3348 if (s
[i
]=='\n' && s
[i
+1]==0)
3356 q
= p
+count
+nr_lf
*2;
3357 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3358 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3362 FIXME("Malloc failed\n");
3369 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3373 if (num_written
!= size
)
3375 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3376 fd
, hand
, GetLastError(), num_written
);
3377 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3378 return s
- buf_start
;
3386 /*********************************************************************
3389 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3393 MSVCRT__lock_file(file
);
3394 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3395 if (len
== sizeof(val
)) {
3396 MSVCRT__unlock_file(file
);
3400 file
->_flag
|= MSVCRT__IOERR
;
3401 MSVCRT__unlock_file(file
);
3405 /*********************************************************************
3408 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3412 MSVCRT__lock_file(file
);
3413 ret
= MSVCRT__fclose_nolock(file
);
3414 MSVCRT__unlock_file(file
);
3419 /*********************************************************************
3420 * _fclose_nolock (MSVCRT.@)
3422 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3427 MSVCRT_free(file
->_tmpfname
);
3428 file
->_tmpfname
= NULL
;
3429 /* flush stdio buffers */
3430 if(file
->_flag
& MSVCRT__IOWRT
)
3431 MSVCRT_fflush(file
);
3432 if(file
->_flag
& MSVCRT__IOMYBUF
)
3433 MSVCRT_free(file
->_base
);
3435 r
=MSVCRT__close(file
->_file
);
3438 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3441 /*********************************************************************
3444 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3446 return file
->_flag
& MSVCRT__IOEOF
;
3449 /*********************************************************************
3452 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3454 return file
->_flag
& MSVCRT__IOERR
;
3457 /*********************************************************************
3458 * _filbuf (MSVCRT.@)
3460 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3463 MSVCRT__lock_file(file
);
3465 if(file
->_flag
& MSVCRT__IOSTRG
) {
3466 MSVCRT__unlock_file(file
);
3470 /* Allocate buffer if needed */
3471 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3472 msvcrt_alloc_buffer(file
);
3474 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3475 if(file
->_flag
& MSVCRT__IORW
)
3476 file
->_flag
|= MSVCRT__IOREAD
;
3478 MSVCRT__unlock_file(file
);
3483 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3485 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3486 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3487 MSVCRT__unlock_file(file
);
3491 MSVCRT__unlock_file(file
);
3494 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3496 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3498 MSVCRT__unlock_file(file
);
3503 file
->_ptr
= file
->_base
+1;
3504 c
= *(unsigned char *)file
->_base
;
3505 MSVCRT__unlock_file(file
);
3510 /*********************************************************************
3513 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3518 MSVCRT__lock_file(file
);
3521 i
= (unsigned char *)file
->_ptr
++;
3524 j
= MSVCRT__filbuf(file
);
3526 MSVCRT__unlock_file(file
);
3530 /*********************************************************************
3531 * _fgetchar (MSVCRT.@)
3533 int CDECL
MSVCRT__fgetchar(void)
3535 return MSVCRT_fgetc(MSVCRT_stdin
);
3538 /*********************************************************************
3541 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3543 int cc
= MSVCRT_EOF
;
3544 char * buf_start
= s
;
3546 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3547 file
,file
->_file
,s
,size
);
3549 MSVCRT__lock_file(file
);
3551 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3556 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3558 TRACE(":nothing read\n");
3559 MSVCRT__unlock_file(file
);
3562 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3565 TRACE(":got %s\n", debugstr_a(buf_start
));
3566 MSVCRT__unlock_file(file
);
3570 /*********************************************************************
3573 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3578 MSVCRT__lock_file(file
);
3580 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3581 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3584 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3585 ch
= MSVCRT_fgetc(file
);
3586 if(ch
== MSVCRT_EOF
) {
3593 char mbs
[MSVCRT_MB_LEN_MAX
];
3596 ch
= MSVCRT_fgetc(file
);
3597 if(ch
!= MSVCRT_EOF
) {
3599 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3600 ch
= MSVCRT_fgetc(file
);
3601 if(ch
!= MSVCRT_EOF
) {
3610 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3614 MSVCRT__unlock_file(file
);
3618 /*********************************************************************
3621 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3628 MSVCRT__lock_file(file
);
3629 for (j
=0; j
<sizeof(int); j
++) {
3630 k
= MSVCRT_fgetc(file
);
3631 if (k
== MSVCRT_EOF
) {
3632 file
->_flag
|= MSVCRT__IOEOF
;
3633 MSVCRT__unlock_file(file
);
3639 MSVCRT__unlock_file(file
);
3643 /*********************************************************************
3646 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3648 return MSVCRT_fgetwc(file
);
3651 /*********************************************************************
3652 * _fgetwchar (MSVCRT.@)
3654 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3656 return MSVCRT_fgetwc(MSVCRT_stdin
);
3659 /*********************************************************************
3660 * getwchar (MSVCRT.@)
3662 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3664 return MSVCRT__fgetwchar();
3667 /*********************************************************************
3670 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3672 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3673 MSVCRT_wchar_t
* buf_start
= s
;
3675 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3676 file
,file
->_file
,s
,size
);
3678 MSVCRT__lock_file(file
);
3680 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3685 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3687 TRACE(":nothing read\n");
3688 MSVCRT__unlock_file(file
);
3691 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3694 TRACE(":got %s\n", debugstr_w(buf_start
));
3695 MSVCRT__unlock_file(file
);
3699 /*********************************************************************
3700 * _flsbuf (MSVCRT.@)
3702 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3704 /* Flush output buffer */
3705 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3706 msvcrt_alloc_buffer(file
);
3708 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3709 if(file
->_flag
& MSVCRT__IORW
)
3710 file
->_flag
|= MSVCRT__IOWRT
;
3714 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3717 if(file
->_cnt
<= 0) {
3718 res
= msvcrt_flush_buffer(file
);
3721 file
->_cnt
=file
->_bufsiz
;
3729 /* set _cnt to 0 for unbuffered FILEs */
3731 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3734 file
->_flag
|= MSVCRT__IOERR
;
3739 /*********************************************************************
3742 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3746 MSVCRT__lock_file(file
);
3747 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3748 MSVCRT__unlock_file(file
);
3753 /*********************************************************************
3754 * _fwrite_nolock (MSVCRT.@)
3756 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3758 MSVCRT_size_t wrcnt
=size
* nmemb
;
3764 if(file
->_cnt
< 0) {
3765 WARN("negative file->_cnt value in %p\n", file
);
3766 file
->_flag
|= MSVCRT__IOERR
;
3768 } else if(file
->_cnt
) {
3769 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3770 memcpy(file
->_ptr
, ptr
, pcnt
);
3775 ptr
= (const char*)ptr
+ pcnt
;
3776 } else if((file
->_flag
& MSVCRT__IONBF
)
3777 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3778 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3782 if(file
->_flag
& MSVCRT__IONBF
)
3784 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3785 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3787 bufsiz
= file
->_bufsiz
;
3789 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3791 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3794 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3795 file
->_flag
|= MSVCRT__IOERR
;
3800 ptr
= (const char*)ptr
+ pcnt
;
3802 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3806 ptr
= (const char*)ptr
+ 1;
3810 return written
/ size
;
3813 /*********************************************************************
3816 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3818 MSVCRT_wchar_t mwc
=wc
;
3822 MSVCRT__lock_file(file
);
3823 fdinfo
= msvcrt_get_ioinfo(file
->_file
);
3825 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3826 char buf
[MSVCRT_MB_LEN_MAX
];
3829 char_len
= MSVCRT_wctomb(buf
, mwc
);
3830 if(char_len
!=-1 && MSVCRT_fwrite(buf
, char_len
, 1, file
)==1)
3834 }else if(MSVCRT_fwrite(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3840 MSVCRT__unlock_file(file
);
3844 /*********************************************************************
3845 * _fputwchar (MSVCRT.@)
3847 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3849 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3852 /*********************************************************************
3853 * _wfsopen (MSVCRT.@)
3855 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3858 int open_flags
, stream_flags
, fd
;
3860 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3862 /* map mode string to open() flags. "man fopen" for possibilities. */
3863 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3867 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3870 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3872 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3879 TRACE(":got (%p)\n",file
);
3880 if (fd
>= 0 && !file
)
3886 /*********************************************************************
3887 * _fsopen (MSVCRT.@)
3889 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3892 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3894 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3895 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3896 *MSVCRT__errno() = MSVCRT_EINVAL
;
3899 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3902 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3903 *MSVCRT__errno() = MSVCRT_EINVAL
;
3907 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
3914 /*********************************************************************
3917 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
3919 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3922 /*********************************************************************
3923 * fopen_s (MSVCRT.@)
3925 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
3926 const char *filename
, const char *mode
)
3928 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3929 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3930 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3932 *pFile
= MSVCRT_fopen(filename
, mode
);
3935 return *MSVCRT__errno();
3939 /*********************************************************************
3940 * _wfopen (MSVCRT.@)
3942 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
3944 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3947 /*********************************************************************
3948 * _wfopen_s (MSVCRT.@)
3950 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
3951 const MSVCRT_wchar_t
*mode
)
3953 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3954 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3955 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3957 *pFile
= MSVCRT__wfopen(filename
, mode
);
3960 return *MSVCRT__errno();
3964 /*********************************************************************
3967 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
3971 MSVCRT__lock_file(file
);
3977 res
= msvcrt_flush_buffer(file
);
3978 MSVCRT__unlock_file(file
);
3979 return res
? res
: c
;
3982 MSVCRT__unlock_file(file
);
3986 res
= MSVCRT__flsbuf(c
, file
);
3987 MSVCRT__unlock_file(file
);
3992 /*********************************************************************
3993 * _fputchar (MSVCRT.@)
3995 int CDECL
MSVCRT__fputchar(int c
)
3997 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4000 /*********************************************************************
4003 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4007 MSVCRT__lock_file(file
);
4008 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4009 MSVCRT__unlock_file(file
);
4014 /*********************************************************************
4015 * _fread_nolock (MSVCRT.@)
4017 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4019 MSVCRT_size_t rcnt
=size
* nmemb
;
4020 MSVCRT_size_t read
=0;
4021 MSVCRT_size_t pread
=0;
4026 /* first buffered data */
4028 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4029 memcpy(ptr
, file
->_ptr
, pcnt
);
4034 ptr
= (char*)ptr
+ pcnt
;
4035 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4036 if(file
->_flag
& MSVCRT__IORW
) {
4037 file
->_flag
|= MSVCRT__IOREAD
;
4043 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4044 msvcrt_alloc_buffer(file
);
4049 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4050 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4051 file
->_ptr
= file
->_base
;
4052 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4053 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4054 if (i
> 0 && i
< file
->_cnt
) {
4055 msvcrt_get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4056 file
->_flag
&= ~MSVCRT__IOEOF
;
4059 memcpy(ptr
, file
->_ptr
, i
);
4063 } else if (rcnt
> INT_MAX
) {
4064 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4065 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4066 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4068 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4072 ptr
= (char *)ptr
+i
;
4073 /* expose feof condition in the flags
4074 * MFC tests file->_flag for feof, and doesn't call feof())
4076 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
4077 file
->_flag
|= MSVCRT__IOEOF
;
4080 file
->_flag
|= MSVCRT__IOERR
;
4091 /*********************************************************************
4092 * fread_s (MSVCR80.@)
4094 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4095 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4097 size_t bytes_left
, buf_pos
;
4099 TRACE("(%p %lu %lu %lu %p\n", buf
, buf_size
, elem_size
, count
, stream
);
4101 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4103 memset(buf
, 0, buf_size
);
4106 if(!elem_size
|| !count
) return 0;
4107 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4108 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4110 bytes_left
= elem_size
*count
;
4113 if(stream
->_cnt
> 0) {
4114 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4116 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4117 memset(buf
, 0, buf_size
);
4121 MSVCRT_fread((char*)buf
+buf_pos
, 1, size
, stream
);
4125 int c
= MSVCRT__filbuf(stream
);
4130 if(!MSVCRT_CHECK_PMT_ERR(buf_size
-buf_pos
> 0, MSVCRT_ERANGE
)) {
4131 memset(buf
, 0, buf_size
);
4135 ((char*)buf
)[buf_pos
++] = c
;
4140 return buf_pos
/elem_size
;
4143 /*********************************************************************
4144 * _wfreopen (MSVCRT.@)
4147 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4149 int open_flags
, stream_flags
, fd
;
4151 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4154 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
4158 MSVCRT_fclose(file
);
4159 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4161 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4163 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4173 /*********************************************************************
4174 * _wfreopen_s (MSVCRT.@)
4176 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4177 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4179 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4180 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4181 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4182 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4184 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4187 return *MSVCRT__errno();
4191 /*********************************************************************
4192 * freopen (MSVCRT.@)
4195 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4198 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4200 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4201 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4207 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4214 /*********************************************************************
4215 * freopen_s (MSVCRT.@)
4217 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4218 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4220 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4221 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4222 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4223 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4225 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4228 return *MSVCRT__errno();
4232 /*********************************************************************
4233 * fsetpos (MSVCRT.@)
4235 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4239 MSVCRT__lock_file(file
);
4240 /* Note that all this has been lifted 'as is' from fseek */
4241 if(file
->_flag
& MSVCRT__IOWRT
)
4242 msvcrt_flush_buffer(file
);
4244 /* Discard buffered input */
4246 file
->_ptr
= file
->_base
;
4248 /* Reset direction of i/o */
4249 if(file
->_flag
& MSVCRT__IORW
) {
4250 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4253 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4254 MSVCRT__unlock_file(file
);
4258 /*********************************************************************
4259 * _ftelli64 (MSVCRT.@)
4261 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4265 MSVCRT__lock_file(file
);
4266 ret
= MSVCRT__ftelli64_nolock(file
);
4267 MSVCRT__unlock_file(file
);
4272 /*********************************************************************
4273 * _ftelli64_nolock (MSVCRT.@)
4275 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4279 pos
= _telli64(file
->_file
);
4282 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4283 if(file
->_flag
& MSVCRT__IOWRT
) {
4284 pos
+= file
->_ptr
- file
->_base
;
4286 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4289 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4293 } else if(!file
->_cnt
) { /* nothing to do */
4294 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4298 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4299 for(i
=0; i
<file
->_cnt
; i
++)
4300 if(file
->_ptr
[i
] == '\n')
4306 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4309 pos
-= file
->_bufsiz
;
4310 pos
+= file
->_ptr
- file
->_base
;
4312 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4313 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
4316 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4326 /*********************************************************************
4329 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4331 return MSVCRT__ftelli64(file
);
4334 /*********************************************************************
4335 * _ftell_nolock (MSVCRT.@)
4337 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4339 return MSVCRT__ftelli64_nolock(file
);
4342 /*********************************************************************
4343 * fgetpos (MSVCRT.@)
4345 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4347 *pos
= MSVCRT__ftelli64(file
);
4353 /*********************************************************************
4356 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4358 MSVCRT_size_t len
= strlen(s
);
4361 MSVCRT__lock_file(file
);
4362 ret
= MSVCRT_fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4363 MSVCRT__unlock_file(file
);
4367 /*********************************************************************
4370 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4372 MSVCRT_size_t i
, len
= strlenW(s
);
4376 MSVCRT__lock_file(file
);
4377 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4378 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4379 MSVCRT__unlock_file(file
);
4383 tmp_buf
= add_std_buffer(file
);
4384 for (i
=0; i
<len
; i
++) {
4385 if(MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
) {
4386 if(tmp_buf
) remove_std_buffer(file
);
4387 MSVCRT__unlock_file(file
);
4392 if(tmp_buf
) remove_std_buffer(file
);
4393 MSVCRT__unlock_file(file
);
4397 /*********************************************************************
4398 * getchar (MSVCRT.@)
4400 int CDECL
MSVCRT_getchar(void)
4402 return MSVCRT_fgetc(MSVCRT_stdin
);
4405 /*********************************************************************
4408 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4410 return MSVCRT_fgetc(file
);
4413 /*********************************************************************
4416 char * CDECL
MSVCRT_gets(char *buf
)
4419 char * buf_start
= buf
;
4421 MSVCRT__lock_file(MSVCRT_stdin
);
4422 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4423 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
4424 if(cc
!= '\r') *buf
++ = (char)cc
;
4428 TRACE("got '%s'\n", buf_start
);
4429 MSVCRT__unlock_file(MSVCRT_stdin
);
4433 /*********************************************************************
4436 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4439 MSVCRT_wchar_t
* ws
= buf
;
4441 MSVCRT__lock_file(MSVCRT_stdin
);
4442 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4443 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
4446 *buf
++ = (MSVCRT_wchar_t
)cc
;
4450 TRACE("got %s\n", debugstr_w(ws
));
4451 MSVCRT__unlock_file(MSVCRT_stdin
);
4455 /*********************************************************************
4458 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4460 return MSVCRT_fputc(c
, file
);
4463 /*********************************************************************
4464 * putchar (MSVCRT.@)
4466 int CDECL
MSVCRT_putchar(int c
)
4468 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4471 /*********************************************************************
4472 * _putwch (MSVCRT.@)
4474 int CDECL
MSVCRT__putwch(int c
)
4476 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4479 /*********************************************************************
4482 int CDECL
MSVCRT_puts(const char *s
)
4484 MSVCRT_size_t len
= strlen(s
);
4487 MSVCRT__lock_file(MSVCRT_stdout
);
4488 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4489 MSVCRT__unlock_file(MSVCRT_stdout
);
4493 ret
= MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4494 MSVCRT__unlock_file(MSVCRT_stdout
);
4498 /*********************************************************************
4501 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4503 static const MSVCRT_wchar_t nl
= '\n';
4504 MSVCRT_size_t len
= strlenW(s
);
4507 MSVCRT__lock_file(MSVCRT_stdout
);
4508 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4509 MSVCRT__unlock_file(MSVCRT_stdout
);
4513 ret
= MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4514 MSVCRT__unlock_file(MSVCRT_stdout
);
4518 /*********************************************************************
4521 int CDECL
MSVCRT_remove(const char *path
)
4523 TRACE("(%s)\n",path
);
4524 if (DeleteFileA(path
))
4526 TRACE(":failed (%d)\n",GetLastError());
4527 msvcrt_set_errno(GetLastError());
4531 /*********************************************************************
4532 * _wremove (MSVCRT.@)
4534 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4536 TRACE("(%s)\n",debugstr_w(path
));
4537 if (DeleteFileW(path
))
4539 TRACE(":failed (%d)\n",GetLastError());
4540 msvcrt_set_errno(GetLastError());
4544 /*********************************************************************
4547 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4549 TRACE(":from %s to %s\n",oldpath
,newpath
);
4550 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4552 TRACE(":failed (%d)\n",GetLastError());
4553 msvcrt_set_errno(GetLastError());
4557 /*********************************************************************
4558 * _wrename (MSVCRT.@)
4560 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4562 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4563 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4565 TRACE(":failed (%d)\n",GetLastError());
4566 msvcrt_set_errno(GetLastError());
4570 /*********************************************************************
4571 * setvbuf (MSVCRT.@)
4573 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4575 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4576 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4577 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4579 MSVCRT__lock_file(file
);
4581 MSVCRT_fflush(file
);
4582 if(file
->_flag
& MSVCRT__IOMYBUF
)
4583 MSVCRT_free(file
->_base
);
4584 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4587 if(mode
== MSVCRT__IONBF
) {
4588 file
->_flag
|= MSVCRT__IONBF
;
4589 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4592 file
->_base
= file
->_ptr
= buf
;
4593 file
->_flag
|= MSVCRT__USERBUF
;
4594 file
->_bufsiz
= size
;
4596 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4599 MSVCRT__unlock_file(file
);
4603 file
->_flag
|= MSVCRT__IOMYBUF
;
4604 file
->_bufsiz
= size
;
4606 MSVCRT__unlock_file(file
);
4610 /*********************************************************************
4613 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4615 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4618 /*********************************************************************
4621 char * CDECL
MSVCRT_tmpnam(char *s
)
4628 thread_data_t
*data
= msvcrt_get_thread_data();
4630 if(!data
->tmpnam_buffer
)
4631 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4633 s
= data
->tmpnam_buffer
;
4636 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4637 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4638 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4640 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4641 memcpy(p
, tmpstr
, size
);
4643 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4644 GetLastError() == ERROR_FILE_NOT_FOUND
)
4650 /*********************************************************************
4651 * _wtmpnam (MSVCRT.@)
4653 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4655 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4656 MSVCRT_wchar_t tmpstr
[16];
4660 thread_data_t
*data
= msvcrt_get_thread_data();
4662 if(!data
->wtmpnam_buffer
)
4663 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4665 s
= data
->wtmpnam_buffer
;
4668 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4669 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4670 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4672 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4673 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4675 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4676 GetLastError() == ERROR_FILE_NOT_FOUND
)
4682 /*********************************************************************
4683 * tmpfile (MSVCRT.@)
4685 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4687 char *filename
= MSVCRT_tmpnam(NULL
);
4689 MSVCRT_FILE
* file
= NULL
;
4692 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4693 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4694 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4696 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4701 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4704 if(fd
!= -1 && !file
)
4710 /*********************************************************************
4711 * tmpfile_s (MSVCRT.@)
4713 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4715 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4717 *file
= MSVCRT_tmpfile();
4721 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4723 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4726 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4730 MSVCRT__lock_file(file
);
4732 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4733 ret
= MSVCRT_fwrite(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4734 MSVCRT__unlock_file(file
);
4738 for(i
=0; i
<len
; i
++) {
4739 if(MSVCRT_fputwc(str
[i
], file
) == MSVCRT_WEOF
) {
4740 MSVCRT__unlock_file(file
);
4745 MSVCRT__unlock_file(file
);
4749 /*********************************************************************
4750 * vfprintf (MSVCRT.@)
4752 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4757 MSVCRT__lock_file(file
);
4758 tmp_buf
= add_std_buffer(file
);
4759 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4760 if(tmp_buf
) remove_std_buffer(file
);
4761 MSVCRT__unlock_file(file
);
4766 /*********************************************************************
4767 * vfprintf_s (MSVCRT.@)
4769 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4774 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4776 MSVCRT__lock_file(file
);
4777 tmp_buf
= add_std_buffer(file
);
4778 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4779 if(tmp_buf
) remove_std_buffer(file
);
4780 MSVCRT__unlock_file(file
);
4785 /*********************************************************************
4786 * vfwprintf (MSVCRT.@)
4788 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4793 MSVCRT__lock_file(file
);
4794 tmp_buf
= add_std_buffer(file
);
4795 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4796 if(tmp_buf
) remove_std_buffer(file
);
4797 MSVCRT__unlock_file(file
);
4802 /*********************************************************************
4803 * vfwprintf_s (MSVCRT.@)
4805 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4810 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4812 MSVCRT__lock_file(file
);
4813 tmp_buf
= add_std_buffer(file
);
4814 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4815 if(tmp_buf
) remove_std_buffer(file
);
4816 MSVCRT__unlock_file(file
);
4821 /*********************************************************************
4822 * _vfwprintf_l (MSVCRT.@)
4824 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4825 MSVCRT__locale_t locale
, __ms_va_list valist
)
4830 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4832 MSVCRT__lock_file(file
);
4833 tmp_buf
= add_std_buffer(file
);
4834 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4835 if(tmp_buf
) remove_std_buffer(file
);
4836 MSVCRT__unlock_file(file
);
4841 /*********************************************************************
4842 * vprintf (MSVCRT.@)
4844 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4846 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4849 /*********************************************************************
4850 * vprintf_s (MSVCRT.@)
4852 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4854 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4857 /*********************************************************************
4858 * vwprintf (MSVCRT.@)
4860 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4862 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4865 /*********************************************************************
4866 * vwprintf_s (MSVCRT.@)
4868 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4870 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4873 /*********************************************************************
4874 * fprintf (MSVCRT.@)
4876 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4878 __ms_va_list valist
;
4880 __ms_va_start(valist
, format
);
4881 res
= MSVCRT_vfprintf(file
, format
, valist
);
4882 __ms_va_end(valist
);
4886 /*********************************************************************
4887 * fprintf_s (MSVCRT.@)
4889 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
4891 __ms_va_list valist
;
4893 __ms_va_start(valist
, format
);
4894 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
4895 __ms_va_end(valist
);
4899 /*********************************************************************
4900 * fwprintf (MSVCRT.@)
4902 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4904 __ms_va_list valist
;
4906 __ms_va_start(valist
, format
);
4907 res
= MSVCRT_vfwprintf(file
, format
, valist
);
4908 __ms_va_end(valist
);
4912 /*********************************************************************
4913 * fwprintf_s (MSVCRT.@)
4915 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4917 __ms_va_list valist
;
4919 __ms_va_start(valist
, format
);
4920 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
4921 __ms_va_end(valist
);
4925 /*********************************************************************
4926 * _fwprintf_l (MSVCRT.@)
4928 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
4930 __ms_va_list valist
;
4932 __ms_va_start(valist
, locale
);
4933 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
4934 __ms_va_end(valist
);
4938 /*********************************************************************
4941 int CDECL
MSVCRT_printf(const char *format
, ...)
4943 __ms_va_list valist
;
4945 __ms_va_start(valist
, format
);
4946 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
4947 __ms_va_end(valist
);
4951 /*********************************************************************
4952 * printf_s (MSVCRT.@)
4954 int CDECL
MSVCRT_printf_s(const char *format
, ...)
4956 __ms_va_list valist
;
4958 __ms_va_start(valist
, format
);
4959 res
= MSVCRT_vprintf_s(format
, valist
);
4960 __ms_va_end(valist
);
4964 /*********************************************************************
4967 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
4969 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
4971 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
4972 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
4975 MSVCRT__lock_file(file
);
4976 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
4977 && msvcrt_alloc_buffer(file
))
4978 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
4981 if(file
->_ptr
>file
->_base
) {
4983 if(file
->_flag
& MSVCRT__IOSTRG
) {
4984 if(*file
->_ptr
!= c
) {
4986 MSVCRT__unlock_file(file
);
4993 MSVCRT_clearerr(file
);
4994 file
->_flag
|= MSVCRT__IOREAD
;
4995 MSVCRT__unlock_file(file
);
4999 MSVCRT__unlock_file(file
);
5003 /*********************************************************************
5004 * ungetwc (MSVCRT.@)
5006 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5008 MSVCRT_wchar_t mwc
= wc
;
5010 if (wc
== MSVCRT_WEOF
)
5013 MSVCRT__lock_file(file
);
5015 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5016 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
5017 unsigned char * pp
= (unsigned char *)&mwc
;
5020 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5021 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
)) {
5022 MSVCRT__unlock_file(file
);
5027 char mbs
[MSVCRT_MB_LEN_MAX
];
5030 len
= MSVCRT_wctomb(mbs
, mwc
);
5032 MSVCRT__unlock_file(file
);
5036 for(len
--; len
>=0; len
--) {
5037 if(mbs
[len
] != MSVCRT_ungetc(mbs
[len
], file
)) {
5038 MSVCRT__unlock_file(file
);
5044 MSVCRT__unlock_file(file
);
5048 /*********************************************************************
5049 * wprintf (MSVCRT.@)
5051 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5053 __ms_va_list valist
;
5055 __ms_va_start(valist
, format
);
5056 res
= MSVCRT_vwprintf(format
, valist
);
5057 __ms_va_end(valist
);
5061 /*********************************************************************
5062 * wprintf_s (MSVCRT.@)
5064 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5066 __ms_va_list valist
;
5068 __ms_va_start(valist
, format
);
5069 res
= MSVCRT_vwprintf_s(format
, valist
);
5070 __ms_va_end(valist
);
5074 /*********************************************************************
5075 * _getmaxstdio (MSVCRT.@)
5077 int CDECL
MSVCRT__getmaxstdio(void)
5079 return MSVCRT_max_streams
;
5082 /*********************************************************************
5083 * _setmaxstdio (MSVCRT.@)
5085 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5087 TRACE("%d\n", newmax
);
5089 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5092 MSVCRT_max_streams
= newmax
;
5093 return MSVCRT_max_streams
;