2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READNL 0x04 /* read started with \n */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
68 #define WX_NOSEEK 0x40
71 /* values for exflag - it's used differently in msvcr90.dll*/
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom
[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 /* ioinfo structure size is different in msvcrXX.dll's */
90 CRITICAL_SECTION crit
;
93 /*********************************************************************
94 * __pioinfo (MSVCRT.@)
95 * array of pointers to ioinfo arrays [32]
97 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
99 /*********************************************************************
100 * __badioinfo (MSVCRT.@)
102 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
104 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
105 static int MSVCRT_fdend
= 3; /* highest allocated fd */
109 CRITICAL_SECTION crit
;
112 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
113 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
114 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
116 /* INTERNAL: process umask */
117 static int MSVCRT_umask
= 0;
119 /* INTERNAL: static data for tmpnam and _wtmpname functions */
120 static int tmpnam_unique
;
122 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
123 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
124 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
125 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
127 #define TOUL(x) (ULONGLONG)(x)
128 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
129 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
130 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
131 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
133 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
134 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
135 * and MSVCRT_stream_idx, from race conditions.
136 * It doesn't protect against race conditions manipulating the underlying files
137 * or flags; doing so would probably be better accomplished with per-file
138 * protection, rather than locking the whole table for every change.
140 static CRITICAL_SECTION MSVCRT_file_cs
;
141 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
143 0, 0, &MSVCRT_file_cs
,
144 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
145 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
147 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
148 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
149 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
151 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
153 buf
->st_dev
= buf64
->st_dev
;
154 buf
->st_ino
= buf64
->st_ino
;
155 buf
->st_mode
= buf64
->st_mode
;
156 buf
->st_nlink
= buf64
->st_nlink
;
157 buf
->st_uid
= buf64
->st_uid
;
158 buf
->st_gid
= buf64
->st_gid
;
159 buf
->st_rdev
= buf64
->st_rdev
;
160 buf
->st_size
= buf64
->st_size
;
161 buf
->st_atime
= buf64
->st_atime
;
162 buf
->st_mtime
= buf64
->st_mtime
;
163 buf
->st_ctime
= buf64
->st_ctime
;
166 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
168 buf
->st_dev
= buf64
->st_dev
;
169 buf
->st_ino
= buf64
->st_ino
;
170 buf
->st_mode
= buf64
->st_mode
;
171 buf
->st_nlink
= buf64
->st_nlink
;
172 buf
->st_uid
= buf64
->st_uid
;
173 buf
->st_gid
= buf64
->st_gid
;
174 buf
->st_rdev
= buf64
->st_rdev
;
175 buf
->st_size
= buf64
->st_size
;
176 buf
->st_atime
= buf64
->st_atime
;
177 buf
->st_mtime
= buf64
->st_mtime
;
178 buf
->st_ctime
= buf64
->st_ctime
;
181 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
183 buf
->st_dev
= buf64
->st_dev
;
184 buf
->st_ino
= buf64
->st_ino
;
185 buf
->st_mode
= buf64
->st_mode
;
186 buf
->st_nlink
= buf64
->st_nlink
;
187 buf
->st_uid
= buf64
->st_uid
;
188 buf
->st_gid
= buf64
->st_gid
;
189 buf
->st_rdev
= buf64
->st_rdev
;
190 buf
->st_size
= buf64
->st_size
;
191 buf
->st_atime
= buf64
->st_atime
;
192 buf
->st_mtime
= buf64
->st_mtime
;
193 buf
->st_ctime
= buf64
->st_ctime
;
196 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
198 buf
->st_dev
= buf64
->st_dev
;
199 buf
->st_ino
= buf64
->st_ino
;
200 buf
->st_mode
= buf64
->st_mode
;
201 buf
->st_nlink
= buf64
->st_nlink
;
202 buf
->st_uid
= buf64
->st_uid
;
203 buf
->st_gid
= buf64
->st_gid
;
204 buf
->st_rdev
= buf64
->st_rdev
;
205 buf
->st_size
= buf64
->st_size
;
206 buf
->st_atime
= buf64
->st_atime
;
207 buf
->st_mtime
= buf64
->st_mtime
;
208 buf
->st_ctime
= buf64
->st_ctime
;
211 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
213 /* 1601 to 1970 is 369 years plus 89 leap days */
214 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
216 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
217 ft
->dwHighDateTime
= ticks
>> 32;
218 ft
->dwLowDateTime
= ticks
;
221 static inline ioinfo
* msvcrt_get_ioinfo(int fd
)
224 if(fd
< MSVCRT_MAX_FILES
)
225 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
227 return &MSVCRT___badioinfo
;
229 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
232 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
236 if(i
>= MSVCRT_max_streams
)
240 return &MSVCRT__iob
[i
];
242 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
244 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
245 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
246 ERR("out of memory\n");
247 *MSVCRT__errno() = MSVCRT_ENOMEM
;
251 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
253 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
258 static inline BOOL
msvcrt_is_valid_fd(int fd
)
260 return fd
>= 0 && fd
< MSVCRT_fdend
&& (msvcrt_get_ioinfo(fd
)->wxflag
& WX_OPEN
);
263 /* INTERNAL: Get the HANDLE for a fd
264 * This doesn't lock the table, because a failure will result in
265 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
266 * it returns a valid handle which is about to be closed, a subsequent call
267 * will fail, most likely in a sane way.
269 static HANDLE
msvcrt_fdtoh(int fd
)
271 if (!msvcrt_is_valid_fd(fd
))
273 WARN(":fd (%d) - no handle!\n",fd
);
274 *MSVCRT___doserrno() = 0;
275 *MSVCRT__errno() = MSVCRT_EBADF
;
276 return INVALID_HANDLE_VALUE
;
278 if (msvcrt_get_ioinfo(fd
)->handle
== INVALID_HANDLE_VALUE
)
279 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd
);
280 return msvcrt_get_ioinfo(fd
)->handle
;
283 /* INTERNAL: free a file entry fd */
284 static void msvcrt_free_fd(int fd
)
289 fdinfo
= msvcrt_get_ioinfo(fd
);
290 if(fdinfo
!= &MSVCRT___badioinfo
)
292 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
295 TRACE(":fd (%d) freed\n",fd
);
302 SetStdHandle(STD_INPUT_HANDLE
, 0);
305 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
308 SetStdHandle(STD_ERROR_HANDLE
, 0);
313 if (fd
== MSVCRT_fdend
- 1)
315 if (fd
< MSVCRT_fdstart
)
320 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
321 /* caller must hold the files lock */
322 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
326 if (fd
>= MSVCRT_MAX_FILES
)
328 WARN(":files exhausted!\n");
329 *MSVCRT__errno() = MSVCRT_ENFILE
;
333 fdinfo
= msvcrt_get_ioinfo(fd
);
334 if(fdinfo
== &MSVCRT___badioinfo
) {
337 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
338 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
339 WARN(":out of memory!\n");
340 *MSVCRT__errno() = MSVCRT_ENOMEM
;
344 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
345 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
347 fdinfo
= msvcrt_get_ioinfo(fd
);
350 fdinfo
->handle
= hand
;
351 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_NOSEEK
));
352 fdinfo
->lookahead
[0] = '\n';
353 fdinfo
->lookahead
[1] = '\n';
354 fdinfo
->lookahead
[2] = '\n';
357 /* locate next free slot */
358 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
359 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
361 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
362 msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
364 /* update last fd in use */
365 if (fd
>= MSVCRT_fdend
)
366 MSVCRT_fdend
= fd
+ 1;
367 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
371 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
372 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
373 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
379 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
380 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
385 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
386 ret
= msvcrt_set_fd(hand
, flag
, MSVCRT_fdstart
);
391 /* INTERNAL: Allocate a FILE* for an fd slot */
392 /* caller must hold the files lock */
393 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
398 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
400 file
= msvcrt_get_file(i
);
404 if (file
->_flag
== 0)
406 if (i
== MSVCRT_stream_idx
)
408 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
410 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
411 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
422 /* INTERNAL: initialize a FILE* from an open fd */
423 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
425 TRACE(":fd (%d) allocating FILE*\n",fd
);
426 if (!msvcrt_is_valid_fd(fd
))
428 WARN(":invalid fd %d\n",fd
);
429 *MSVCRT___doserrno() = 0;
430 *MSVCRT__errno() = MSVCRT_EBADF
;
433 memset(file
, 0, sizeof(*file
));
435 file
->_flag
= stream_flags
;
437 TRACE(":got FILE* (%p)\n",file
);
441 /* INTERNAL: Create an inheritance data block (for spawned process)
442 * The inheritance block is made of:
443 * 00 int nb of file descriptor (NBFD)
444 * 04 char file flags (wxflag): repeated for each fd
445 * 4+NBFD HANDLE file handle: repeated for each fd
447 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
454 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
455 *block
= MSVCRT_calloc(*size
, 1);
461 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
462 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
464 *(unsigned*)*block
= MSVCRT_fdend
;
465 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
467 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
468 fdinfo
= msvcrt_get_ioinfo(fd
);
469 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
471 *wxflag_ptr
= fdinfo
->wxflag
;
472 *handle_ptr
= fdinfo
->handle
;
477 *handle_ptr
= INVALID_HANDLE_VALUE
;
479 wxflag_ptr
++; handle_ptr
++;
484 /* INTERNAL: Set up all file descriptors,
485 * as well as default streams (stdin, stderr and stdout)
487 void msvcrt_init_io(void)
493 GetStartupInfoA(&si
);
494 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
500 count
= *(unsigned*)si
.lpReserved2
;
501 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
502 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
504 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
505 count
= min(count
, MSVCRT_MAX_FILES
);
506 for (i
= 0; i
< count
; i
++)
508 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
509 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
511 wxflag_ptr
++; handle_ptr
++;
513 MSVCRT_fdend
= max( 3, count
);
514 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
515 if (msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
518 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO
);
519 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
520 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
521 DWORD type
= GetFileType(h
);
523 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
524 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDIN_FILENO
);
527 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO
);
528 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
529 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
530 DWORD type
= GetFileType(h
);
532 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
533 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDOUT_FILENO
);
536 fdinfo
= msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO
);
537 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
538 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
539 DWORD type
= GetFileType(h
);
541 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
542 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), MSVCRT_STDERR_FILENO
);
545 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(MSVCRT_STDIN_FILENO
)->handle
,
546 msvcrt_get_ioinfo(MSVCRT_STDOUT_FILENO
)->handle
,
547 msvcrt_get_ioinfo(MSVCRT_STDERR_FILENO
)->handle
);
549 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
550 for (i
= 0; i
< 3; i
++)
552 /* FILE structs for stdin/out/err are static and never deleted */
553 MSVCRT__iob
[i
]._file
= i
;
554 MSVCRT__iob
[i
]._tmpfname
= NULL
;
555 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
557 MSVCRT_stream_idx
= 3;
560 /* INTERNAL: Flush stdio file buffer */
561 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
564 int cnt
=file
->_ptr
-file
->_base
;
565 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
566 file
->_flag
|= MSVCRT__IOERR
;
569 file
->_ptr
=file
->_base
;
570 file
->_cnt
=file
->_bufsiz
;
575 /*********************************************************************
578 int CDECL
MSVCRT__isatty(int fd
)
580 HANDLE hand
= msvcrt_fdtoh(fd
);
582 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
583 if (hand
== INVALID_HANDLE_VALUE
)
586 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
589 /* INTERNAL: Allocate stdio file buffer */
590 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
592 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
593 && MSVCRT__isatty(file
->_file
))
596 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
598 file
->_bufsiz
= MSVCRT_BUFSIZ
;
599 file
->_flag
|= MSVCRT__IOMYBUF
;
601 file
->_base
= (char*)(&file
->_charbuf
);
603 file
->_bufsiz
= sizeof(file
->_charbuf
);
605 file
->_ptr
= file
->_base
;
610 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
611 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
613 static char buffers
[2][MSVCRT_BUFSIZ
];
615 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
616 || !MSVCRT__isatty(file
->_file
) || file
->_bufsiz
)
619 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
620 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
624 /* INTERNAL: Removes temporary buffer from stdout or stderr */
625 /* Only call this function when add_std_buffer returned TRUE */
626 static void remove_std_buffer(MSVCRT_FILE
*file
)
628 msvcrt_flush_buffer(file
);
629 file
->_ptr
= file
->_base
= NULL
;
630 file
->_bufsiz
= file
->_cnt
= 0;
633 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
634 static int msvcrt_int_to_base32(int num
, char *str
)
649 *p
= (num
& 31) + '0';
651 *p
+= ('a' - '0' - 10);
658 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
659 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
674 *p
= (num
& 31) + '0';
676 *p
+= ('a' - '0' - 10);
683 /*********************************************************************
684 * __iob_func(MSVCRT.@)
686 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
688 return &MSVCRT__iob
[0];
691 /*********************************************************************
694 int CDECL
MSVCRT__access(const char *filename
, int mode
)
696 DWORD attr
= GetFileAttributesA(filename
);
698 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
700 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
702 msvcrt_set_errno(GetLastError());
705 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
707 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
713 /*********************************************************************
714 * _access_s (MSVCRT.@)
716 int CDECL
_access_s(const char *filename
, int mode
)
718 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
719 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
721 if (MSVCRT__access(filename
, mode
) == -1)
722 return *MSVCRT__errno();
726 /*********************************************************************
727 * _waccess (MSVCRT.@)
729 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
731 DWORD attr
= GetFileAttributesW(filename
);
733 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
735 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
737 msvcrt_set_errno(GetLastError());
740 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
742 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
748 /*********************************************************************
749 * _waccess_s (MSVCRT.@)
751 int CDECL
_waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
753 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
754 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
756 if (MSVCRT__waccess(filename
, mode
) == -1)
757 return *MSVCRT__errno();
761 /*********************************************************************
764 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
766 DWORD oldFlags
= GetFileAttributesA(path
);
768 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
770 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
771 oldFlags
| FILE_ATTRIBUTE_READONLY
;
773 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
776 msvcrt_set_errno(GetLastError());
780 /*********************************************************************
783 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
785 DWORD oldFlags
= GetFileAttributesW(path
);
787 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
789 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
790 oldFlags
| FILE_ATTRIBUTE_READONLY
;
792 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
795 msvcrt_set_errno(GetLastError());
799 /*********************************************************************
802 int CDECL
MSVCRT__unlink(const char *path
)
804 TRACE("%s\n",debugstr_a(path
));
805 if(DeleteFileA(path
))
807 TRACE("failed (%d)\n",GetLastError());
808 msvcrt_set_errno(GetLastError());
812 /*********************************************************************
813 * _wunlink (MSVCRT.@)
815 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
817 TRACE("(%s)\n",debugstr_w(path
));
818 if(DeleteFileW(path
))
820 TRACE("failed (%d)\n",GetLastError());
821 msvcrt_set_errno(GetLastError());
825 /*********************************************************************
828 int CDECL
MSVCRT__commit(int fd
)
830 HANDLE hand
= msvcrt_fdtoh(fd
);
832 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
833 if (hand
== INVALID_HANDLE_VALUE
)
836 if (!FlushFileBuffers(hand
))
838 if (GetLastError() == ERROR_INVALID_HANDLE
)
840 /* FlushFileBuffers fails for console handles
841 * so we ignore this error.
845 TRACE(":failed-last error (%d)\n",GetLastError());
846 msvcrt_set_errno(GetLastError());
853 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
854 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
856 /* INTERNAL: Flush all stream buffer */
857 static int msvcrt_flush_all_buffers(int mask
)
859 int i
, num_flushed
= 0;
863 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
864 file
= msvcrt_get_file(i
);
868 if(file
->_flag
& mask
) {
876 TRACE(":flushed (%d) handles\n",num_flushed
);
880 /*********************************************************************
881 * _flushall (MSVCRT.@)
883 int CDECL
MSVCRT__flushall(void)
885 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
888 /*********************************************************************
891 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
894 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
895 } else if(file
->_flag
& MSVCRT__IOWRT
) {
898 MSVCRT__lock_file(file
);
899 res
= msvcrt_flush_buffer(file
);
901 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
902 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
903 MSVCRT__unlock_file(file
);
906 } else if(file
->_flag
& MSVCRT__IOREAD
) {
907 MSVCRT__lock_file(file
);
909 file
->_ptr
= file
->_base
;
910 MSVCRT__unlock_file(file
);
917 /*********************************************************************
920 int CDECL
MSVCRT__close(int fd
)
926 hand
= msvcrt_fdtoh(fd
);
927 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
928 if (!msvcrt_is_valid_fd(fd
)) {
932 ret
= CloseHandle(hand
) ? 0 : -1;
934 WARN(":failed-last error (%d)\n",GetLastError());
935 msvcrt_set_errno(GetLastError());
943 /*********************************************************************
946 * MSDN isn't clear on this point, but the remarks for _pipe
947 * indicate file descriptors duplicated with _dup and _dup2 are always
950 int CDECL
MSVCRT__dup2(int od
, int nd
)
954 TRACE("(od=%d, nd=%d)\n", od
, nd
);
956 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
960 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od
)->handle
,
961 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
963 int wxflag
= msvcrt_get_ioinfo(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
965 if (msvcrt_is_valid_fd(nd
))
967 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
971 *MSVCRT__errno() = MSVCRT_EMFILE
;
975 /* _dup2 returns 0, not nd, on success */
982 msvcrt_set_errno(GetLastError());
987 *MSVCRT__errno() = MSVCRT_EBADF
;
994 /*********************************************************************
997 int CDECL
MSVCRT__dup(int od
)
1002 fd
= MSVCRT_fdstart
;
1003 if (MSVCRT__dup2(od
, fd
) == 0)
1011 /*********************************************************************
1014 int CDECL
MSVCRT__eof(int fd
)
1016 DWORD curpos
,endpos
;
1017 LONG hcurpos
,hendpos
;
1018 HANDLE hand
= msvcrt_fdtoh(fd
);
1020 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1022 if (hand
== INVALID_HANDLE_VALUE
)
1025 if (msvcrt_get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1027 /* Otherwise we do it the hard way */
1028 hcurpos
= hendpos
= 0;
1029 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1030 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1032 if (curpos
== endpos
&& hcurpos
== hendpos
)
1034 /* FIXME: shouldn't WX_ATEOF be set here? */
1038 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1042 /*********************************************************************
1043 * _fcloseall (MSVCRT.@)
1045 int CDECL
MSVCRT__fcloseall(void)
1047 int num_closed
= 0, i
;
1051 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1052 file
= msvcrt_get_file(i
);
1054 if (file
->_flag
&& !MSVCRT_fclose(file
))
1059 TRACE(":closed (%d) handles\n",num_closed
);
1063 /* free everything on process exit */
1064 void msvcrt_free_io(void)
1070 MSVCRT__fcloseall();
1072 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1073 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1075 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1077 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1078 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1080 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1081 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1085 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1086 MSVCRT_free(MSVCRT_fstream
[i
]);
1088 DeleteCriticalSection(&MSVCRT_file_cs
);
1091 /*********************************************************************
1092 * _lseeki64 (MSVCRT.@)
1094 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1096 HANDLE hand
= msvcrt_fdtoh(fd
);
1099 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1100 if (hand
== INVALID_HANDLE_VALUE
)
1103 if (whence
< 0 || whence
> 2)
1105 *MSVCRT__errno() = MSVCRT_EINVAL
;
1109 TRACE(":fd (%d) to %s pos %s\n",
1110 fd
,wine_dbgstr_longlong(offset
),
1111 (whence
==SEEK_SET
)?"SEEK_SET":
1112 (whence
==SEEK_CUR
)?"SEEK_CUR":
1113 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1115 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1116 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1117 ofs
.QuadPart
= offset
;
1118 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1119 GetLastError() == ERROR_SUCCESS
)
1121 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_ATEOF
;
1122 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1124 return ofs
.QuadPart
;
1126 TRACE(":error-last error (%d)\n",GetLastError());
1127 msvcrt_set_errno(GetLastError());
1131 /*********************************************************************
1134 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1136 return MSVCRT__lseeki64(fd
, offset
, whence
);
1139 /*********************************************************************
1140 * _lock_file (MSVCRT.@)
1142 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1144 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1145 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1147 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1150 /*********************************************************************
1151 * _unlock_file (MSVCRT.@)
1153 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1155 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1156 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1158 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1161 /*********************************************************************
1162 * _locking (MSVCRT.@)
1164 * This is untested; the underlying LockFile doesn't work yet.
1166 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1170 HANDLE hand
= msvcrt_fdtoh(fd
);
1172 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1173 if (hand
== INVALID_HANDLE_VALUE
)
1176 if (mode
< 0 || mode
> 4)
1178 *MSVCRT__errno() = MSVCRT_EINVAL
;
1182 TRACE(":fd (%d) by 0x%08x mode %s\n",
1183 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1184 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1185 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1186 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1187 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1190 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1192 FIXME ("Seek failed\n");
1193 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1196 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1199 ret
= 1; /* just to satisfy gcc */
1202 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1207 else if (mode
== MSVCRT__LK_UNLCK
)
1208 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1210 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1211 /* FIXME - what about error settings? */
1212 return ret
? 0 : -1;
1215 /*********************************************************************
1216 * _fseeki64 (MSVCRT.@)
1218 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1222 MSVCRT__lock_file(file
);
1223 /* Flush output if needed */
1224 if(file
->_flag
& MSVCRT__IOWRT
)
1225 msvcrt_flush_buffer(file
);
1227 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1229 offset
+= MSVCRT__ftelli64(file
);
1232 /* Discard buffered input */
1234 file
->_ptr
= file
->_base
;
1235 /* Reset direction of i/o */
1236 if(file
->_flag
& MSVCRT__IORW
) {
1237 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1239 /* Clear end of file flag */
1240 file
->_flag
&= ~MSVCRT__IOEOF
;
1241 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1243 MSVCRT__unlock_file(file
);
1247 /*********************************************************************
1250 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1252 return MSVCRT__fseeki64( file
, offset
, whence
);
1255 /*********************************************************************
1256 * _chsize_s (MSVCRT.@)
1258 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1264 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1266 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1270 handle
= msvcrt_fdtoh(fd
);
1271 if (handle
!= INVALID_HANDLE_VALUE
)
1273 /* save the current file pointer */
1274 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1277 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1280 ret
= SetEndOfFile(handle
);
1281 if (!ret
) msvcrt_set_errno(GetLastError());
1284 /* restore the file pointer */
1285 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1290 return ret
? 0 : *MSVCRT__errno();
1293 /*********************************************************************
1294 * _chsize (MSVCRT.@)
1296 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1298 /* _chsize_s returns errno on failure but _chsize should return -1 */
1299 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1302 /*********************************************************************
1303 * clearerr (MSVCRT.@)
1305 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1307 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1309 MSVCRT__lock_file(file
);
1310 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1311 MSVCRT__unlock_file(file
);
1314 /*********************************************************************
1317 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1319 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1321 MSVCRT__lock_file(file
);
1322 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1323 MSVCRT_clearerr(file
);
1324 MSVCRT__unlock_file(file
);
1327 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1329 int plus
= strchrW(mode
, '+') != NULL
;
1331 TRACE("%s\n", debugstr_w(mode
));
1333 while(*mode
== ' ') mode
++;
1338 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1339 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1342 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1343 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1346 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1347 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1350 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1354 *stream_flags
|= MSVCRT__commode
;
1356 while (*mode
&& *mode
!=',')
1360 *open_flags
|= MSVCRT__O_BINARY
;
1361 *open_flags
&= ~MSVCRT__O_TEXT
;
1364 *open_flags
|= MSVCRT__O_TEXT
;
1365 *open_flags
&= ~MSVCRT__O_BINARY
;
1368 *open_flags
|= MSVCRT__O_TEMPORARY
;
1371 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1374 *stream_flags
|= MSVCRT__IOCOMMIT
;
1377 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1380 *open_flags
|= MSVCRT__O_NOINHERIT
;
1389 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1392 ERR("incorrect mode flag: %c\n", mode
[-1]);
1398 static const WCHAR ccs
[] = {'c','c','s'};
1399 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1400 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1401 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1404 while(*mode
== ' ') mode
++;
1405 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1407 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1408 while(*mode
== ' ') mode
++;
1409 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1412 while(*mode
== ' ') mode
++;
1414 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1416 *open_flags
|= MSVCRT__O_U8TEXT
;
1417 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1419 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1421 *open_flags
|= MSVCRT__O_U16TEXT
;
1422 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1424 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1426 *open_flags
|= MSVCRT__O_WTEXT
;
1427 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1431 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1435 while(*mode
== ' ') mode
++;
1438 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1443 /*********************************************************************
1444 * _fdopen (MSVCRT.@)
1446 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1449 MSVCRT_wchar_t
*modeW
= NULL
;
1451 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1453 ret
= MSVCRT__wfdopen(fd
, modeW
);
1459 /*********************************************************************
1460 * _wfdopen (MSVCRT.@)
1462 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1464 int open_flags
, stream_flags
;
1467 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1470 if (!(file
= msvcrt_alloc_fp()))
1472 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1477 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1483 /*********************************************************************
1484 * _filelength (MSVCRT.@)
1486 LONG CDECL
MSVCRT__filelength(int fd
)
1488 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1491 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1494 if (endPos
!= curPos
)
1495 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1502 /*********************************************************************
1503 * _filelengthi64 (MSVCRT.@)
1505 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1507 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1510 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1513 if (endPos
!= curPos
)
1514 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1521 /*********************************************************************
1522 * _fileno (MSVCRT.@)
1524 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1526 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1530 /*********************************************************************
1531 * _fstat64 (MSVCRT.@)
1533 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1537 BY_HANDLE_FILE_INFORMATION hfi
;
1538 HANDLE hand
= msvcrt_fdtoh(fd
);
1540 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1541 if (hand
== INVALID_HANDLE_VALUE
)
1546 WARN(":failed-NULL buf\n");
1547 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1551 memset(&hfi
, 0, sizeof(hfi
));
1552 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1553 type
= GetFileType(hand
);
1554 if (type
== FILE_TYPE_PIPE
)
1556 buf
->st_dev
= buf
->st_rdev
= fd
;
1557 buf
->st_mode
= S_IFIFO
;
1560 else if (type
== FILE_TYPE_CHAR
)
1562 buf
->st_dev
= buf
->st_rdev
= fd
;
1563 buf
->st_mode
= S_IFCHR
;
1566 else /* FILE_TYPE_DISK etc. */
1568 if (!GetFileInformationByHandle(hand
, &hfi
))
1570 WARN(":failed-last error (%d)\n",GetLastError());
1571 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1574 buf
->st_mode
= S_IFREG
| 0444;
1575 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1576 buf
->st_mode
|= 0222;
1577 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1578 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1580 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1581 buf
->st_mtime
= buf
->st_ctime
= dw
;
1582 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1584 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1589 /*********************************************************************
1590 * _fstati64 (MSVCRT.@)
1592 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1595 struct MSVCRT__stat64 buf64
;
1597 ret
= MSVCRT__fstat64(fd
, &buf64
);
1599 msvcrt_stat64_to_stati64(&buf64
, buf
);
1603 /*********************************************************************
1606 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1608 struct MSVCRT__stat64 buf64
;
1610 ret
= MSVCRT__fstat64(fd
, &buf64
);
1612 msvcrt_stat64_to_stat(&buf64
, buf
);
1616 /* _fstat32 - not exported in native msvcrt */
1617 int CDECL
_fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1620 struct MSVCRT__stat64 buf64
;
1622 ret
= MSVCRT__fstat64(fd
, &buf64
);
1624 msvcrt_stat64_to_stat32(&buf64
, buf
);
1628 /* _fstat64i32 - not exported in native msvcrt */
1629 int CDECL
_fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1632 struct MSVCRT__stat64 buf64
;
1634 ret
= MSVCRT__fstat64(fd
, &buf64
);
1636 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1640 /*********************************************************************
1641 * _futime64 (MSVCRT.@)
1643 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1645 HANDLE hand
= msvcrt_fdtoh(fd
);
1650 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1655 time_to_filetime( t
->actime
, &at
);
1656 time_to_filetime( t
->modtime
, &wt
);
1659 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1661 msvcrt_set_errno(GetLastError());
1667 /*********************************************************************
1668 * _futime32 (MSVCRT.@)
1670 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1674 struct MSVCRT___utimbuf64 t64
;
1675 t64
.actime
= t
->actime
;
1676 t64
.modtime
= t
->modtime
;
1677 return _futime64( fd
, &t64
);
1680 return _futime64( fd
, NULL
);
1683 /*********************************************************************
1684 * _futime (MSVCRT.@)
1687 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1689 return _futime64( fd
, t
);
1692 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1694 return _futime32( fd
, t
);
1698 /*********************************************************************
1699 * _get_osfhandle (MSVCRT.@)
1701 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1703 HANDLE hand
= msvcrt_fdtoh(fd
);
1704 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1706 return (MSVCRT_intptr_t
)hand
;
1709 /*********************************************************************
1710 * _mktemp_s (MSVCRT.@)
1712 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1716 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1717 return MSVCRT_EINVAL
;
1719 for(len
=0; len
<size
; len
++)
1722 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1725 return MSVCRT_EINVAL
;
1728 for(xno
=1; xno
<=6; xno
++)
1729 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1730 return MSVCRT_EINVAL
;
1732 id
= GetCurrentProcessId();
1733 for(xno
=1; xno
<6; xno
++) {
1734 pattern
[len
-xno
] = id
%10 + '0';
1738 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1739 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1744 *MSVCRT__errno() = MSVCRT_EEXIST
;
1745 return MSVCRT_EEXIST
;
1748 /*********************************************************************
1749 * _mktemp (MSVCRT.@)
1751 char * CDECL
MSVCRT__mktemp(char *pattern
)
1754 char *retVal
= pattern
;
1762 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1766 id
= GetCurrentProcessId();
1770 int tempNum
= id
/ 10;
1771 *pattern
-- = id
- (tempNum
* 10) + '0';
1777 *pattern
= letter
++;
1778 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1780 } while(letter
<= 'z');
1784 /*********************************************************************
1785 * _wmktemp_s (MSVCRT.@)
1787 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1791 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1792 return MSVCRT_EINVAL
;
1794 for(len
=0; len
<size
; len
++)
1797 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1800 return MSVCRT_EINVAL
;
1803 for(xno
=1; xno
<=6; xno
++)
1804 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1805 return MSVCRT_EINVAL
;
1807 id
= GetCurrentProcessId();
1808 for(xno
=1; xno
<6; xno
++) {
1809 pattern
[len
-xno
] = id
%10 + '0';
1813 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1814 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1819 *MSVCRT__errno() = MSVCRT_EEXIST
;
1820 return MSVCRT_EEXIST
;
1823 /*********************************************************************
1824 * _wmktemp (MSVCRT.@)
1826 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1829 MSVCRT_wchar_t
*retVal
= pattern
;
1831 MSVCRT_wchar_t letter
= 'a';
1837 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1841 id
= GetCurrentProcessId();
1845 int tempNum
= id
/ 10;
1846 *pattern
-- = id
- (tempNum
* 10) + '0';
1852 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
1854 *pattern
= letter
++;
1855 } while(letter
!= '|');
1859 static unsigned split_oflags(unsigned oflags
)
1862 unsigned unsupp
; /* until we support everything */
1864 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1865 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1866 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1867 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1868 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1869 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1870 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1871 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1872 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1874 if ((unsupp
= oflags
& ~(
1875 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1876 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1877 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1878 MSVCRT__O_NOINHERIT
|
1879 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
1880 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
1882 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1887 /*********************************************************************
1890 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1893 SECURITY_ATTRIBUTES sa
;
1894 HANDLE readHandle
, writeHandle
;
1898 *MSVCRT__errno() = MSVCRT_EINVAL
;
1902 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1903 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1904 sa
.lpSecurityDescriptor
= NULL
;
1905 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1907 unsigned int wxflags
= split_oflags(textmode
);
1911 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
1915 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
1923 MSVCRT__close(pfds
[0]);
1924 CloseHandle(writeHandle
);
1925 *MSVCRT__errno() = MSVCRT_EMFILE
;
1930 CloseHandle(readHandle
);
1931 CloseHandle(writeHandle
);
1932 *MSVCRT__errno() = MSVCRT_EMFILE
;
1937 msvcrt_set_errno(GetLastError());
1942 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1944 char bom
[sizeof(utf8_bom
)];
1947 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
1949 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1952 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1953 oflags
|= MSVCRT__O_U8TEXT
;
1954 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1956 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1957 oflags
|= MSVCRT__O_U16TEXT
;
1959 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1965 /*********************************************************************
1966 * _wsopen_s (MSVCRT.@)
1968 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
1970 DWORD access
= 0, creation
= 0, attrib
;
1971 SECURITY_ATTRIBUTES sa
;
1976 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1977 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1979 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
1982 wxflag
= split_oflags(oflags
);
1983 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1985 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1986 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1987 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1990 if (oflags
& MSVCRT__O_CREAT
)
1992 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1993 FIXME(": pmode 0x%04x ignored\n", pmode
);
1995 WARN(": pmode 0x%04x ignored\n", pmode
);
1997 if (oflags
& MSVCRT__O_EXCL
)
1998 creation
= CREATE_NEW
;
1999 else if (oflags
& MSVCRT__O_TRUNC
)
2000 creation
= CREATE_ALWAYS
;
2002 creation
= OPEN_ALWAYS
;
2004 else /* no MSVCRT__O_CREAT */
2006 if (oflags
& MSVCRT__O_TRUNC
)
2007 creation
= TRUNCATE_EXISTING
;
2009 creation
= OPEN_EXISTING
;
2014 case MSVCRT__SH_DENYRW
:
2017 case MSVCRT__SH_DENYWR
:
2018 sharing
= FILE_SHARE_READ
;
2020 case MSVCRT__SH_DENYRD
:
2021 sharing
= FILE_SHARE_WRITE
;
2023 case MSVCRT__SH_DENYNO
:
2024 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2027 ERR( "Unhandled shflags 0x%x\n", shflags
);
2028 return MSVCRT_EINVAL
;
2030 attrib
= FILE_ATTRIBUTE_NORMAL
;
2032 if (oflags
& MSVCRT__O_TEMPORARY
)
2034 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2036 sharing
|= FILE_SHARE_DELETE
;
2039 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2040 sa
.lpSecurityDescriptor
= NULL
;
2041 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2043 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2044 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2045 && !(access
&GENERIC_READ
))
2047 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2048 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2049 if (hand
!= INVALID_HANDLE_VALUE
)
2051 oflags
= check_bom(hand
, oflags
, FALSE
);
2055 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2058 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2059 if (hand
== INVALID_HANDLE_VALUE
) {
2060 WARN(":failed-last error (%d)\n",GetLastError());
2061 msvcrt_set_errno(GetLastError());
2062 return *MSVCRT__errno();
2065 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2067 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2068 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2069 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2071 if (oflags
& MSVCRT__O_U8TEXT
)
2073 DWORD written
= 0, tmp
;
2075 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2076 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2078 if (written
!= sizeof(utf8_bom
)) {
2079 WARN("error writing BOM\n");
2081 msvcrt_set_errno(GetLastError());
2082 return *MSVCRT__errno();
2087 DWORD written
= 0, tmp
;
2089 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2090 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2092 if (written
!= sizeof(utf16_bom
))
2094 WARN("error writing BOM\n");
2096 msvcrt_set_errno(GetLastError());
2097 return *MSVCRT__errno();
2101 else if (access
& GENERIC_READ
)
2102 oflags
= check_bom(hand
, oflags
, TRUE
);
2105 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2107 return *MSVCRT__errno();
2109 if (oflags
& MSVCRT__O_WTEXT
)
2110 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2111 else if (oflags
& MSVCRT__O_U16TEXT
)
2112 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
2113 else if (oflags
& MSVCRT__O_U8TEXT
)
2114 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
2116 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2120 /*********************************************************************
2121 * _wsopen (MSVCRT.@)
2123 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2128 if (oflags
& MSVCRT__O_CREAT
)
2132 __ms_va_start(ap
, shflags
);
2133 pmode
= va_arg(ap
, int);
2139 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2143 /*********************************************************************
2144 * _sopen_s (MSVCRT.@)
2146 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2148 MSVCRT_wchar_t
*pathW
;
2151 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2152 return MSVCRT_EINVAL
;
2154 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2159 /*********************************************************************
2162 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2167 if (oflags
& MSVCRT__O_CREAT
)
2171 __ms_va_start(ap
, shflags
);
2172 pmode
= va_arg(ap
, int);
2178 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2182 /*********************************************************************
2185 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2189 if (flags
& MSVCRT__O_CREAT
)
2192 __ms_va_start(ap
, flags
);
2193 pmode
= va_arg(ap
, int);
2195 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2198 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2201 /*********************************************************************
2204 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2208 if (flags
& MSVCRT__O_CREAT
)
2211 __ms_va_start(ap
, flags
);
2212 pmode
= va_arg(ap
, int);
2214 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2217 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2220 /*********************************************************************
2223 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2225 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2226 return MSVCRT__open(path
, usedFlags
);
2229 /*********************************************************************
2230 * _wcreat (MSVCRT.@)
2232 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2234 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2235 return MSVCRT__wopen(path
, usedFlags
);
2238 /*********************************************************************
2239 * _open_osfhandle (MSVCRT.@)
2241 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2246 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2247 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2248 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2249 * text - it never sets MSVCRT__O_BINARY.
2251 /* don't let split_oflags() decide the mode if no mode is passed */
2252 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2253 oflags
|= MSVCRT__O_BINARY
;
2255 flags
= GetFileType((HANDLE
)handle
);
2256 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2258 msvcrt_set_errno(GetLastError());
2262 if (flags
== FILE_TYPE_CHAR
)
2264 else if (flags
== FILE_TYPE_PIPE
)
2268 flags
|= split_oflags(oflags
);
2270 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2271 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2275 /*********************************************************************
2278 int CDECL
MSVCRT__rmtmp(void)
2280 int num_removed
= 0, i
;
2284 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2285 file
= msvcrt_get_file(i
);
2287 if (file
->_tmpfname
)
2289 MSVCRT_fclose(file
);
2296 TRACE(":removed (%d) temp files\n",num_removed
);
2300 static inline int get_utf8_char_len(char ch
)
2302 if((ch
&0xf8) == 0xf0)
2304 else if((ch
&0xf0) == 0xe0)
2306 else if((ch
&0xe0) == 0xc0)
2311 /*********************************************************************
2312 * (internal) read_utf8
2314 static int read_utf8(int fd
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2316 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2317 HANDLE hand
= fdinfo
->handle
;
2318 char min_buf
[4], *readbuf
, lookahead
;
2319 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2321 /* make the buffer big enough to hold at least one character */
2322 /* read bytes have to fit to output and lookahead buffers */
2324 readbuf_size
= count
< 4 ? 4 : count
;
2325 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2330 if(fdinfo
->lookahead
[0] != '\n') {
2331 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2332 fdinfo
->lookahead
[0] = '\n';
2334 if(fdinfo
->lookahead
[1] != '\n') {
2335 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2336 fdinfo
->lookahead
[1] = '\n';
2338 if(fdinfo
->lookahead
[2] != '\n') {
2339 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2340 fdinfo
->lookahead
[2] = '\n';
2345 /* NOTE: this case is broken in native dll, reading
2346 * sometimes fails when small buffer is passed
2349 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2350 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2351 fdinfo
->wxflag
|= WX_ATEOF
;
2354 msvcrt_set_errno(GetLastError());
2357 }else if(!num_read
) {
2358 fdinfo
->wxflag
|= WX_ATEOF
;
2364 char_len
= get_utf8_char_len(readbuf
[0]);
2366 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2370 if(readbuf
[0] == '\n')
2371 fdinfo
->wxflag
|= WX_READNL
;
2373 fdinfo
->wxflag
&= ~WX_READNL
;
2375 if(readbuf
[0] == 0x1a) {
2376 fdinfo
->wxflag
|= WX_ATEOF
;
2380 if(readbuf
[0] == '\r') {
2381 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2383 else if(lookahead
== '\n')
2387 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2388 fdinfo
->lookahead
[0] = lookahead
;
2390 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2395 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2396 msvcrt_set_errno(GetLastError());
2403 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2406 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2407 fdinfo
->wxflag
|= WX_ATEOF
;
2408 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2411 msvcrt_set_errno(GetLastError());
2412 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2415 }else if(!pos
&& !num_read
) {
2416 fdinfo
->wxflag
|= WX_ATEOF
;
2417 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2422 if(readbuf
[0] == '\n')
2423 fdinfo
->wxflag
|= WX_READNL
;
2425 fdinfo
->wxflag
&= ~WX_READNL
;
2427 /* Find first byte of last character (may be incomplete) */
2428 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2429 if((readbuf
[i
]&0xc0) != 0x80)
2431 char_len
= get_utf8_char_len(readbuf
[i
]);
2432 if(char_len
+i
<= pos
)
2435 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
)) {
2437 fdinfo
->lookahead
[0] = readbuf
[i
];
2439 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2441 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2443 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2447 for(i
=0, j
=0; i
<pos
; i
++) {
2448 if(readbuf
[i
] == 0x1a) {
2449 fdinfo
->wxflag
|= WX_ATEOF
;
2453 /* strip '\r' if followed by '\n' */
2454 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2455 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2456 readbuf
[j
++] = '\r';
2457 }else if(lookahead
== '\n' && j
==0) {
2458 readbuf
[j
++] = '\n';
2460 if(lookahead
!= '\n')
2461 readbuf
[j
++] = '\r';
2463 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2464 fdinfo
->lookahead
[0] = lookahead
;
2466 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2468 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2469 readbuf
[j
++] = readbuf
[i
];
2474 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2475 msvcrt_set_errno(GetLastError());
2476 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2480 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2484 /*********************************************************************
2487 * When reading \r as last character in text mode, read() positions
2488 * the file pointer on the \r character while getc() goes on to
2491 static int read_i(int fd
, void *buf
, unsigned int count
)
2493 DWORD num_read
, utf16
;
2494 char *bufstart
= buf
;
2495 HANDLE hand
= msvcrt_fdtoh(fd
);
2496 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2501 if (fdinfo
->wxflag
& WX_ATEOF
) {
2502 TRACE("already at EOF, returning 0\n");
2505 /* Don't trace small reads, it gets *very* annoying */
2507 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2508 if (hand
== INVALID_HANDLE_VALUE
)
2510 *MSVCRT__errno() = MSVCRT_EBADF
;
2514 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2515 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2517 *MSVCRT__errno() = MSVCRT_EINVAL
;
2521 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2522 return read_utf8(fd
, buf
, count
);
2524 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2526 if (fdinfo
->lookahead
[0] != '\n')
2528 bufstart
[0] = fdinfo
->lookahead
[0];
2529 fdinfo
->lookahead
[0] = '\n';
2533 bufstart
[1] = fdinfo
->lookahead
[1];
2534 fdinfo
->lookahead
[1] = '\n';
2537 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2538 num_read
+= 1+utf16
;
2543 if(utf16
&& (num_read
&1))
2545 /* msvcr90 uses uninitialized value from the buffer in this case */
2546 /* msvcrt ignores additional data */
2547 ERR("got odd number of bytes in UTF16 mode\n");
2551 if (count
!= 0 && num_read
== 0)
2553 fdinfo
->wxflag
|= WX_ATEOF
;
2554 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2556 else if (fdinfo
->wxflag
& WX_TEXT
)
2560 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2561 fdinfo
->wxflag
|= WX_READNL
;
2563 fdinfo
->wxflag
&= ~WX_READNL
;
2565 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2567 /* in text mode, a ctrl-z signals EOF */
2568 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2570 fdinfo
->wxflag
|= WX_ATEOF
;
2571 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2575 /* in text mode, strip \r if followed by \n */
2576 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2581 lookahead
[1] = '\n';
2582 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2584 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2586 bufstart
[j
++] = '\n';
2587 if(utf16
) bufstart
[j
++] = 0;
2591 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2593 bufstart
[j
++] = '\r';
2594 if(utf16
) bufstart
[j
++] = 0;
2597 if (fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2599 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2601 bufstart
[j
++] = '\n';
2602 if (utf16
) bufstart
[j
++] = 0;
2606 fdinfo
->lookahead
[0] = lookahead
[0];
2607 fdinfo
->lookahead
[1] = lookahead
[1];
2611 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2616 bufstart
[j
++] = '\r';
2617 if(utf16
) bufstart
[j
++] = 0;
2620 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2621 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2623 bufstart
[j
++] = bufstart
[i
];
2624 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2632 if (GetLastError() == ERROR_BROKEN_PIPE
)
2634 TRACE(":end-of-pipe\n");
2635 fdinfo
->wxflag
|= WX_ATEOF
;
2640 TRACE(":failed-last error (%d)\n",GetLastError());
2646 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2650 /*********************************************************************
2653 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2656 num_read
= read_i(fd
, buf
, count
);
2660 /*********************************************************************
2661 * _setmode (MSVCRT.@)
2663 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2665 int ret
= msvcrt_get_ioinfo(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2666 if(ret
==MSVCRT__O_TEXT
&& (msvcrt_get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2667 ret
= MSVCRT__O_WTEXT
;
2669 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2670 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2671 *MSVCRT__errno() = MSVCRT_EINVAL
;
2675 if(mode
== MSVCRT__O_BINARY
) {
2676 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2677 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2681 msvcrt_get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2682 if(mode
== MSVCRT__O_TEXT
)
2683 msvcrt_get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2684 else if(mode
== MSVCRT__O_U8TEXT
)
2685 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2687 msvcrt_get_ioinfo(fd
)->exflag
= (msvcrt_get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2692 /*********************************************************************
2693 * _stat64 (MSVCRT.@)
2695 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2698 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2699 unsigned short mode
= ALL_S_IREAD
;
2702 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2704 plen
= strlen(path
);
2705 while (plen
&& path
[plen
-1]==' ')
2708 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2709 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2711 *MSVCRT__errno() = MSVCRT_ENOENT
;
2715 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2717 TRACE("failed (%d)\n",GetLastError());
2718 *MSVCRT__errno() = MSVCRT_ENOENT
;
2722 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2724 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2725 Bon 011120: This FIXME seems incorrect
2726 Also a letter as first char isn't enough to be classified
2729 if (isalpha(*path
)&& (*(path
+1)==':'))
2730 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2732 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2734 /* Dir, or regular file? */
2735 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2736 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2739 mode
|= MSVCRT__S_IFREG
;
2741 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2743 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2744 (tolower(path
[plen
-3]) << 16);
2745 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2746 mode
|= ALL_S_IEXEC
;
2750 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2751 mode
|= ALL_S_IWRITE
;
2753 buf
->st_mode
= mode
;
2755 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2756 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2758 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2759 buf
->st_mtime
= buf
->st_ctime
= dw
;
2760 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2761 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2762 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2766 /*********************************************************************
2767 * _stati64 (MSVCRT.@)
2769 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2772 struct MSVCRT__stat64 buf64
;
2774 ret
= MSVCRT_stat64(path
, &buf64
);
2776 msvcrt_stat64_to_stati64(&buf64
, buf
);
2780 /*********************************************************************
2783 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2785 struct MSVCRT__stat64 buf64
;
2787 ret
= MSVCRT_stat64( path
, &buf64
);
2789 msvcrt_stat64_to_stat(&buf64
, buf
);
2793 /*********************************************************************
2794 * _wstat64 (MSVCRT.@)
2796 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2799 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2800 unsigned short mode
= ALL_S_IREAD
;
2803 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2805 plen
= strlenW(path
);
2806 while (plen
&& path
[plen
-1]==' ')
2809 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2810 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2812 *MSVCRT__errno() = MSVCRT_ENOENT
;
2816 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2818 TRACE("failed (%d)\n",GetLastError());
2819 *MSVCRT__errno() = MSVCRT_ENOENT
;
2823 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2825 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2826 if (MSVCRT_iswalpha(*path
))
2827 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2829 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2831 /* Dir, or regular file? */
2832 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2833 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2836 mode
|= MSVCRT__S_IFREG
;
2838 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2840 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2841 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2842 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2843 mode
|= ALL_S_IEXEC
;
2847 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2848 mode
|= ALL_S_IWRITE
;
2850 buf
->st_mode
= mode
;
2852 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2853 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2855 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2856 buf
->st_mtime
= buf
->st_ctime
= dw
;
2857 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2858 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2859 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2863 /*********************************************************************
2864 * _wstati64 (MSVCRT.@)
2866 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2869 struct MSVCRT__stat64 buf64
;
2871 ret
= MSVCRT__wstat64(path
, &buf64
);
2873 msvcrt_stat64_to_stati64(&buf64
, buf
);
2877 /*********************************************************************
2880 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2883 struct MSVCRT__stat64 buf64
;
2885 ret
= MSVCRT__wstat64( path
, &buf64
);
2886 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2890 /*********************************************************************
2893 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
2895 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
2898 /*********************************************************************
2899 * _telli64 (MSVCRT.@)
2901 __int64 CDECL
_telli64(int fd
)
2903 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
2906 /*********************************************************************
2907 * _tempnam (MSVCRT.@)
2909 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
2911 char tmpbuf
[MAX_PATH
];
2912 const char *tmp_dir
= MSVCRT_getenv("TMP");
2914 if (tmp_dir
) dir
= tmp_dir
;
2916 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2917 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2919 TRACE("got name (%s)\n",tmpbuf
);
2920 DeleteFileA(tmpbuf
);
2921 return MSVCRT__strdup(tmpbuf
);
2923 TRACE("failed (%d)\n",GetLastError());
2927 /*********************************************************************
2928 * _wtempnam (MSVCRT.@)
2930 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
2932 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
2933 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
2934 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
2936 if (tmp_dir
) dir
= tmp_dir
;
2938 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2939 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2941 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2942 DeleteFileW(tmpbuf
);
2943 return MSVCRT__wcsdup(tmpbuf
);
2945 TRACE("failed (%d)\n",GetLastError());
2949 /*********************************************************************
2952 int CDECL
MSVCRT__umask(int umask
)
2954 int old_umask
= MSVCRT_umask
;
2955 TRACE("(%d)\n",umask
);
2956 MSVCRT_umask
= umask
;
2960 /*********************************************************************
2961 * _utime64 (MSVCRT.@)
2963 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
2965 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2969 int retVal
= _futime64(fd
, t
);
2976 /*********************************************************************
2977 * _utime32 (MSVCRT.@)
2979 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
2983 struct MSVCRT___utimbuf64 t64
;
2984 t64
.actime
= t
->actime
;
2985 t64
.modtime
= t
->modtime
;
2986 return _utime64( path
, &t64
);
2989 return _utime64( path
, NULL
);
2992 /*********************************************************************
2996 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
2998 return _utime64( path
, t
);
3001 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3003 return _utime32( path
, t
);
3007 /*********************************************************************
3008 * _wutime64 (MSVCRT.@)
3010 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3012 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3016 int retVal
= _futime64(fd
, t
);
3023 /*********************************************************************
3024 * _wutime32 (MSVCRT.@)
3026 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3030 struct MSVCRT___utimbuf64 t64
;
3031 t64
.actime
= t
->actime
;
3032 t64
.modtime
= t
->modtime
;
3033 return _wutime64( path
, &t64
);
3036 return _wutime64( path
, NULL
);
3039 /*********************************************************************
3040 * _wutime (MSVCRT.@)
3043 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3045 return _wutime64( path
, t
);
3048 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3050 return _wutime32( path
, t
);
3054 /*********************************************************************
3057 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3060 ioinfo
*info
= msvcrt_get_ioinfo(fd
);
3061 HANDLE hand
= info
->handle
;
3063 /* Don't trace small writes, it gets *very* annoying */
3066 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3068 if (hand
== INVALID_HANDLE_VALUE
)
3070 *MSVCRT__errno() = MSVCRT_EBADF
;
3074 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3076 *MSVCRT__errno() = MSVCRT_EINVAL
;
3080 /* If appending, go to EOF */
3081 if (info
->wxflag
& WX_APPEND
)
3082 MSVCRT__lseek(fd
, 0, FILE_END
);
3084 if (!(info
->wxflag
& WX_TEXT
))
3086 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3087 && (num_written
== count
))
3089 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3090 hand
, GetLastError());
3091 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3095 unsigned int i
, j
, nr_lf
, size
;
3098 const char *s
= buf
, *buf_start
= buf
;
3100 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3102 /* find number of \n */
3103 for (nr_lf
=0, i
=0; i
<count
; i
++)
3109 if ((q
= p
= MSVCRT_malloc(size
)))
3111 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3120 FIXME("Malloc failed\n");
3132 else if (info
->exflag
& EF_UTF16
)
3134 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3135 if (s
[i
]=='\n' && s
[i
+1]==0)
3140 if ((q
= p
= MSVCRT_malloc(size
)))
3142 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3144 if (s
[i
]=='\n' && s
[i
+1]==0)
3155 FIXME("Malloc failed\n");
3171 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3172 if (s
[i
]=='\n' && s
[i
+1]==0)
3175 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3177 msvcrt_set_errno(GetLastError());
3182 size
= conv_len
+nr_lf
;
3183 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3185 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3187 if (s
[i
]=='\n' && s
[i
+1]==0)
3195 q
= p
+count
+nr_lf
*2;
3196 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3197 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3201 FIXME("Malloc failed\n");
3208 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3212 if (num_written
!= size
)
3214 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3215 fd
, hand
, GetLastError(), num_written
);
3216 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3217 return s
- buf_start
;
3225 /*********************************************************************
3228 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3232 MSVCRT__lock_file(file
);
3233 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3234 if (len
== sizeof(val
)) {
3235 MSVCRT__unlock_file(file
);
3239 file
->_flag
|= MSVCRT__IOERR
;
3240 MSVCRT__unlock_file(file
);
3244 /*********************************************************************
3247 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3251 MSVCRT__lock_file(file
);
3253 MSVCRT_free(file
->_tmpfname
);
3254 file
->_tmpfname
= NULL
;
3255 /* flush stdio buffers */
3256 if(file
->_flag
& MSVCRT__IOWRT
)
3257 MSVCRT_fflush(file
);
3258 if(file
->_flag
& MSVCRT__IOMYBUF
)
3259 MSVCRT_free(file
->_base
);
3261 r
=MSVCRT__close(file
->_file
);
3264 MSVCRT__unlock_file(file
);
3266 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3269 /*********************************************************************
3272 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3274 return file
->_flag
& MSVCRT__IOEOF
;
3277 /*********************************************************************
3280 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3282 return file
->_flag
& MSVCRT__IOERR
;
3285 /*********************************************************************
3286 * _filbuf (MSVCRT.@)
3288 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3291 MSVCRT__lock_file(file
);
3293 if(file
->_flag
& MSVCRT__IOSTRG
) {
3294 MSVCRT__unlock_file(file
);
3298 /* Allocate buffer if needed */
3299 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
))
3300 msvcrt_alloc_buffer(file
);
3302 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3303 if(file
->_flag
& MSVCRT__IORW
)
3304 file
->_flag
|= MSVCRT__IOREAD
;
3306 MSVCRT__unlock_file(file
);
3311 if(file
->_flag
& MSVCRT__IONBF
) {
3313 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
3314 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3315 MSVCRT__unlock_file(file
);
3319 MSVCRT__unlock_file(file
);
3322 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
3324 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3326 MSVCRT__unlock_file(file
);
3331 file
->_ptr
= file
->_base
+1;
3332 c
= *(unsigned char *)file
->_base
;
3333 MSVCRT__unlock_file(file
);
3338 /*********************************************************************
3341 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3346 MSVCRT__lock_file(file
);
3349 i
= (unsigned char *)file
->_ptr
++;
3352 j
= MSVCRT__filbuf(file
);
3354 MSVCRT__unlock_file(file
);
3358 /*********************************************************************
3359 * _fgetchar (MSVCRT.@)
3361 int CDECL
MSVCRT__fgetchar(void)
3363 return MSVCRT_fgetc(MSVCRT_stdin
);
3366 /*********************************************************************
3369 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3371 int cc
= MSVCRT_EOF
;
3372 char * buf_start
= s
;
3374 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3375 file
,file
->_file
,s
,size
);
3377 MSVCRT__lock_file(file
);
3379 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3384 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3386 TRACE(":nothing read\n");
3387 MSVCRT__unlock_file(file
);
3390 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3393 TRACE(":got %s\n", debugstr_a(buf_start
));
3394 MSVCRT__unlock_file(file
);
3398 /*********************************************************************
3401 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3406 MSVCRT__lock_file(file
);
3408 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3409 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3412 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3413 ch
= MSVCRT_fgetc(file
);
3414 if(ch
== MSVCRT_EOF
) {
3421 char mbs
[MSVCRT_MB_LEN_MAX
];
3424 ch
= MSVCRT_fgetc(file
);
3425 if(ch
!= MSVCRT_EOF
) {
3427 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3428 ch
= MSVCRT_fgetc(file
);
3429 if(ch
!= MSVCRT_EOF
) {
3438 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3442 MSVCRT__unlock_file(file
);
3446 /*********************************************************************
3449 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3456 MSVCRT__lock_file(file
);
3457 for (j
=0; j
<sizeof(int); j
++) {
3458 k
= MSVCRT_fgetc(file
);
3459 if (k
== MSVCRT_EOF
) {
3460 file
->_flag
|= MSVCRT__IOEOF
;
3461 MSVCRT__unlock_file(file
);
3467 MSVCRT__unlock_file(file
);
3471 /*********************************************************************
3474 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3476 return MSVCRT_fgetwc(file
);
3479 /*********************************************************************
3480 * _fgetwchar (MSVCRT.@)
3482 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3484 return MSVCRT_fgetwc(MSVCRT_stdin
);
3487 /*********************************************************************
3488 * getwchar (MSVCRT.@)
3490 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3492 return MSVCRT__fgetwchar();
3495 /*********************************************************************
3498 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3500 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3501 MSVCRT_wchar_t
* buf_start
= s
;
3503 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3504 file
,file
->_file
,s
,size
);
3506 MSVCRT__lock_file(file
);
3508 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3513 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3515 TRACE(":nothing read\n");
3516 MSVCRT__unlock_file(file
);
3519 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3522 TRACE(":got %s\n", debugstr_w(buf_start
));
3523 MSVCRT__unlock_file(file
);
3527 /*********************************************************************
3528 * _flsbuf (MSVCRT.@)
3530 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3532 /* Flush output buffer */
3533 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3534 msvcrt_alloc_buffer(file
);
3536 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3537 if(file
->_flag
& MSVCRT__IORW
)
3538 file
->_flag
|= MSVCRT__IOWRT
;
3546 res
= msvcrt_flush_buffer(file
);
3552 return res
? res
: c
&0xff;
3556 /* set _cnt to 0 for unbuffered FILEs */
3558 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3561 file
->_flag
|= MSVCRT__IOERR
;
3566 /*********************************************************************
3569 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3571 MSVCRT_size_t wrcnt
=size
* nmemb
;
3576 MSVCRT__lock_file(file
);
3580 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3581 memcpy(file
->_ptr
, ptr
, pcnt
);
3586 ptr
= (const char*)ptr
+ pcnt
;
3587 } else if(!file
->_bufsiz
&& (file
->_flag
& MSVCRT__IONBF
)) {
3588 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3589 if(file
->_flag
& MSVCRT__IORW
)
3590 file
->_flag
|= MSVCRT__IOWRT
;
3595 if(MSVCRT__write(file
->_file
, ptr
, wrcnt
) <= 0) {
3596 file
->_flag
|= MSVCRT__IOERR
;
3602 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3606 ptr
= (const char*)ptr
+ 1;
3610 MSVCRT__unlock_file(file
);
3611 return written
/ size
;
3614 /*********************************************************************
3617 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3619 MSVCRT_wchar_t mwc
=wc
;
3623 MSVCRT__lock_file(file
);
3624 fdinfo
= msvcrt_get_ioinfo(file
->_file
);
3626 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3627 char buf
[MSVCRT_MB_LEN_MAX
];
3630 char_len
= MSVCRT_wctomb(buf
, mwc
);
3631 if(char_len
!=-1 && MSVCRT_fwrite(buf
, char_len
, 1, file
)==1)
3635 }else if(MSVCRT_fwrite(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3641 MSVCRT__unlock_file(file
);
3645 /*********************************************************************
3646 * _fputwchar (MSVCRT.@)
3648 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3650 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3653 /*********************************************************************
3654 * _wfsopen (MSVCRT.@)
3656 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3659 int open_flags
, stream_flags
, fd
;
3661 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3663 /* map mode string to open() flags. "man fopen" for possibilities. */
3664 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3668 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3671 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3673 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3680 TRACE(":got (%p)\n",file
);
3681 if (fd
>= 0 && !file
)
3687 /*********************************************************************
3688 * _fsopen (MSVCRT.@)
3690 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3693 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3695 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3696 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3697 *MSVCRT__errno() = MSVCRT_EINVAL
;
3700 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3703 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3704 *MSVCRT__errno() = MSVCRT_EINVAL
;
3708 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
3715 /*********************************************************************
3718 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
3720 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3723 /*********************************************************************
3724 * fopen_s (MSVCRT.@)
3726 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
3727 const char *filename
, const char *mode
)
3729 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3730 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3731 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3733 *pFile
= MSVCRT_fopen(filename
, mode
);
3736 return *MSVCRT__errno();
3740 /*********************************************************************
3741 * _wfopen (MSVCRT.@)
3743 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
3745 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3748 /*********************************************************************
3749 * _wfopen_s (MSVCRT.@)
3751 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
3752 const MSVCRT_wchar_t
*mode
)
3754 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3755 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3756 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3758 *pFile
= MSVCRT__wfopen(filename
, mode
);
3761 return *MSVCRT__errno();
3765 /*********************************************************************
3768 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
3772 MSVCRT__lock_file(file
);
3778 res
= msvcrt_flush_buffer(file
);
3779 MSVCRT__unlock_file(file
);
3780 return res
? res
: c
;
3783 MSVCRT__unlock_file(file
);
3787 res
= MSVCRT__flsbuf(c
, file
);
3788 MSVCRT__unlock_file(file
);
3793 /*********************************************************************
3794 * _fputchar (MSVCRT.@)
3796 int CDECL
MSVCRT__fputchar(int c
)
3798 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3801 /*********************************************************************
3804 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3806 MSVCRT_size_t rcnt
=size
* nmemb
;
3807 MSVCRT_size_t read
=0;
3813 MSVCRT__lock_file(file
);
3815 /* first buffered data */
3817 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3818 memcpy(ptr
, file
->_ptr
, pcnt
);
3823 ptr
= (char*)ptr
+ pcnt
;
3824 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3825 if(file
->_flag
& MSVCRT__IORW
) {
3826 file
->_flag
|= MSVCRT__IOREAD
;
3828 MSVCRT__unlock_file(file
);
3835 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& !(file
->_flag
& MSVCRT__IONBF
)
3836 && (file
->_bufsiz
!= 0 || msvcrt_alloc_buffer(file
))) {
3837 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3838 file
->_ptr
= file
->_base
;
3839 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3840 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3841 if (i
> 0 && i
< file
->_cnt
) {
3842 msvcrt_get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3843 file
->_flag
&= ~MSVCRT__IOEOF
;
3846 memcpy(ptr
, file
->_ptr
, i
);
3850 } else if (rcnt
> UINT_MAX
) {
3851 i
= MSVCRT__read(file
->_file
, ptr
, UINT_MAX
);
3852 } else if (rcnt
< MSVCRT_BUFSIZ
) {
3853 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
3855 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
3859 ptr
= (char *)ptr
+i
;
3860 /* expose feof condition in the flags
3861 * MFC tests file->_flag for feof, and doesn't call feof())
3863 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3864 file
->_flag
|= MSVCRT__IOEOF
;
3867 file
->_flag
|= MSVCRT__IOERR
;
3874 MSVCRT__unlock_file(file
);
3879 /* fread_s - not exported in native msvcrt */
3880 MSVCRT_size_t CDECL
fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
3881 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
3883 size_t bytes_left
, buf_pos
;
3885 TRACE("(%p %lu %lu %lu %p\n", buf
, buf_size
, elem_size
, count
, stream
);
3887 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
3889 memset(buf
, 0, buf_size
);
3892 if(!elem_size
|| !count
) return 0;
3893 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
3894 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
3896 bytes_left
= elem_size
*count
;
3899 if(stream
->_cnt
> 0) {
3900 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
3902 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
3903 memset(buf
, 0, buf_size
);
3907 MSVCRT_fread((char*)buf
+buf_pos
, 1, size
, stream
);
3911 int c
= MSVCRT__filbuf(stream
);
3916 if(!MSVCRT_CHECK_PMT_ERR(buf_size
-buf_pos
> 0, MSVCRT_ERANGE
)) {
3917 memset(buf
, 0, buf_size
);
3921 ((char*)buf
)[buf_pos
++] = c
;
3926 return buf_pos
/elem_size
;
3929 /*********************************************************************
3930 * _wfreopen (MSVCRT.@)
3933 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
3935 int open_flags
, stream_flags
, fd
;
3937 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
3940 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
3944 MSVCRT_fclose(file
);
3945 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3947 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
3949 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
3959 /*********************************************************************
3960 * _wfreopen_s (MSVCRT.@)
3962 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
3963 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
3965 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3966 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
3967 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3968 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
3970 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
3973 return *MSVCRT__errno();
3977 /*********************************************************************
3978 * freopen (MSVCRT.@)
3981 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
3984 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3986 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3987 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3993 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4000 /*********************************************************************
4001 * freopen_s (MSVCRT.@)
4003 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4004 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4006 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4007 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4008 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4009 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4011 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4014 return *MSVCRT__errno();
4018 /*********************************************************************
4019 * fsetpos (MSVCRT.@)
4021 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4025 MSVCRT__lock_file(file
);
4026 /* Note that all this has been lifted 'as is' from fseek */
4027 if(file
->_flag
& MSVCRT__IOWRT
)
4028 msvcrt_flush_buffer(file
);
4030 /* Discard buffered input */
4032 file
->_ptr
= file
->_base
;
4034 /* Reset direction of i/o */
4035 if(file
->_flag
& MSVCRT__IORW
) {
4036 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4039 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4040 MSVCRT__unlock_file(file
);
4044 /*********************************************************************
4045 * _ftelli64 (MSVCRT.@)
4047 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4051 MSVCRT__lock_file(file
);
4052 pos
= _telli64(file
->_file
);
4054 MSVCRT__unlock_file(file
);
4058 if(file
->_flag
& MSVCRT__IOWRT
) {
4059 pos
+= file
->_ptr
- file
->_base
;
4061 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4064 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4068 } else if(!file
->_cnt
) { /* nothing to do */
4069 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4073 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4074 for(i
=0; i
<file
->_cnt
; i
++)
4075 if(file
->_ptr
[i
] == '\n')
4081 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
4082 MSVCRT__unlock_file(file
);
4086 pos
-= file
->_bufsiz
;
4087 pos
+= file
->_ptr
- file
->_base
;
4089 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
4090 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
4093 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4100 MSVCRT__unlock_file(file
);
4104 /*********************************************************************
4107 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4109 return MSVCRT__ftelli64(file
);
4112 /*********************************************************************
4113 * fgetpos (MSVCRT.@)
4115 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4117 *pos
= MSVCRT__ftelli64(file
);
4123 /*********************************************************************
4126 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4128 MSVCRT_size_t len
= strlen(s
);
4131 MSVCRT__lock_file(file
);
4132 ret
= MSVCRT_fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4133 MSVCRT__unlock_file(file
);
4137 /*********************************************************************
4140 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4142 MSVCRT_size_t i
, len
= strlenW(s
);
4146 MSVCRT__lock_file(file
);
4147 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4148 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4149 MSVCRT__unlock_file(file
);
4153 tmp_buf
= add_std_buffer(file
);
4154 for (i
=0; i
<len
; i
++) {
4155 if(MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
) {
4156 if(tmp_buf
) remove_std_buffer(file
);
4157 MSVCRT__unlock_file(file
);
4162 if(tmp_buf
) remove_std_buffer(file
);
4163 MSVCRT__unlock_file(file
);
4167 /*********************************************************************
4168 * getchar (MSVCRT.@)
4170 int CDECL
MSVCRT_getchar(void)
4172 return MSVCRT_fgetc(MSVCRT_stdin
);
4175 /*********************************************************************
4178 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4180 return MSVCRT_fgetc(file
);
4183 /*********************************************************************
4186 char * CDECL
MSVCRT_gets(char *buf
)
4189 char * buf_start
= buf
;
4191 MSVCRT__lock_file(MSVCRT_stdin
);
4192 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4193 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
4194 if(cc
!= '\r') *buf
++ = (char)cc
;
4198 TRACE("got '%s'\n", buf_start
);
4199 MSVCRT__unlock_file(MSVCRT_stdin
);
4203 /*********************************************************************
4206 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4209 MSVCRT_wchar_t
* ws
= buf
;
4211 MSVCRT__lock_file(MSVCRT_stdin
);
4212 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4213 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
4216 *buf
++ = (MSVCRT_wchar_t
)cc
;
4220 TRACE("got %s\n", debugstr_w(ws
));
4221 MSVCRT__unlock_file(MSVCRT_stdin
);
4225 /*********************************************************************
4228 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4230 return MSVCRT_fputc(c
, file
);
4233 /*********************************************************************
4234 * putchar (MSVCRT.@)
4236 int CDECL
MSVCRT_putchar(int c
)
4238 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4241 /*********************************************************************
4242 * _putwch (MSVCRT.@)
4244 int CDECL
MSVCRT__putwch(int c
)
4246 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
4249 /*********************************************************************
4252 int CDECL
MSVCRT_puts(const char *s
)
4254 MSVCRT_size_t len
= strlen(s
);
4257 MSVCRT__lock_file(MSVCRT_stdout
);
4258 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4259 MSVCRT__unlock_file(MSVCRT_stdout
);
4263 ret
= MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4264 MSVCRT__unlock_file(MSVCRT_stdout
);
4268 /*********************************************************************
4271 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4273 static const MSVCRT_wchar_t nl
= '\n';
4274 MSVCRT_size_t len
= strlenW(s
);
4277 MSVCRT__lock_file(MSVCRT_stdout
);
4278 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4279 MSVCRT__unlock_file(MSVCRT_stdout
);
4283 ret
= MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4284 MSVCRT__unlock_file(MSVCRT_stdout
);
4288 /*********************************************************************
4291 int CDECL
MSVCRT_remove(const char *path
)
4293 TRACE("(%s)\n",path
);
4294 if (DeleteFileA(path
))
4296 TRACE(":failed (%d)\n",GetLastError());
4297 msvcrt_set_errno(GetLastError());
4301 /*********************************************************************
4302 * _wremove (MSVCRT.@)
4304 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4306 TRACE("(%s)\n",debugstr_w(path
));
4307 if (DeleteFileW(path
))
4309 TRACE(":failed (%d)\n",GetLastError());
4310 msvcrt_set_errno(GetLastError());
4314 /*********************************************************************
4317 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4319 TRACE(":from %s to %s\n",oldpath
,newpath
);
4320 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4322 TRACE(":failed (%d)\n",GetLastError());
4323 msvcrt_set_errno(GetLastError());
4327 /*********************************************************************
4328 * _wrename (MSVCRT.@)
4330 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4332 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4333 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4335 TRACE(":failed (%d)\n",GetLastError());
4336 msvcrt_set_errno(GetLastError());
4340 /*********************************************************************
4341 * setvbuf (MSVCRT.@)
4343 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4345 MSVCRT__lock_file(file
);
4347 MSVCRT_free(file
->_base
);
4351 if(mode
== MSVCRT__IOFBF
) {
4352 file
->_flag
&= ~MSVCRT__IONBF
;
4353 file
->_base
= file
->_ptr
= buf
;
4355 file
->_bufsiz
= size
;
4358 file
->_flag
|= MSVCRT__IONBF
;
4360 MSVCRT__unlock_file(file
);
4364 /*********************************************************************
4367 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4369 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4372 /*********************************************************************
4375 char * CDECL
MSVCRT_tmpnam(char *s
)
4382 thread_data_t
*data
= msvcrt_get_thread_data();
4384 if(!data
->tmpnam_buffer
)
4385 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4387 s
= data
->tmpnam_buffer
;
4390 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4391 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4392 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4394 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4395 memcpy(p
, tmpstr
, size
);
4397 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4398 GetLastError() == ERROR_FILE_NOT_FOUND
)
4404 /*********************************************************************
4405 * _wtmpnam (MSVCRT.@)
4407 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4409 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4410 MSVCRT_wchar_t tmpstr
[16];
4414 thread_data_t
*data
= msvcrt_get_thread_data();
4416 if(!data
->wtmpnam_buffer
)
4417 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4419 s
= data
->wtmpnam_buffer
;
4422 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4423 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4424 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4426 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4427 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4429 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4430 GetLastError() == ERROR_FILE_NOT_FOUND
)
4436 /*********************************************************************
4437 * tmpfile (MSVCRT.@)
4439 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4441 char *filename
= MSVCRT_tmpnam(NULL
);
4443 MSVCRT_FILE
* file
= NULL
;
4446 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4447 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4448 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4450 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4455 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4458 if(fd
!= -1 && !file
)
4464 /*********************************************************************
4465 * tmpfile_s (MSVCRT.@)
4467 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4469 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4471 *file
= MSVCRT_tmpfile();
4475 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4477 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4480 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4484 MSVCRT__lock_file(file
);
4486 if(!(msvcrt_get_ioinfo(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4487 ret
= MSVCRT_fwrite(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4488 MSVCRT__unlock_file(file
);
4492 for(i
=0; i
<len
; i
++) {
4493 if(MSVCRT_fputwc(str
[i
], file
) == MSVCRT_WEOF
) {
4494 MSVCRT__unlock_file(file
);
4499 MSVCRT__unlock_file(file
);
4503 /*********************************************************************
4504 * vfprintf (MSVCRT.@)
4506 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4511 MSVCRT__lock_file(file
);
4512 tmp_buf
= add_std_buffer(file
);
4513 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4514 if(tmp_buf
) remove_std_buffer(file
);
4515 MSVCRT__unlock_file(file
);
4520 /*********************************************************************
4521 * vfprintf_s (MSVCRT.@)
4523 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4528 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4530 MSVCRT__lock_file(file
);
4531 tmp_buf
= add_std_buffer(file
);
4532 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4533 if(tmp_buf
) remove_std_buffer(file
);
4534 MSVCRT__unlock_file(file
);
4539 /*********************************************************************
4540 * vfwprintf (MSVCRT.@)
4542 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4547 MSVCRT__lock_file(file
);
4548 tmp_buf
= add_std_buffer(file
);
4549 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4550 if(tmp_buf
) remove_std_buffer(file
);
4551 MSVCRT__unlock_file(file
);
4556 /*********************************************************************
4557 * vfwprintf_s (MSVCRT.@)
4559 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4564 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4566 MSVCRT__lock_file(file
);
4567 tmp_buf
= add_std_buffer(file
);
4568 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4569 if(tmp_buf
) remove_std_buffer(file
);
4570 MSVCRT__unlock_file(file
);
4575 /*********************************************************************
4576 * _vfwprintf_l (MSVCRT.@)
4578 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4579 MSVCRT__locale_t locale
, __ms_va_list valist
)
4584 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4586 MSVCRT__lock_file(file
);
4587 tmp_buf
= add_std_buffer(file
);
4588 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4589 if(tmp_buf
) remove_std_buffer(file
);
4590 MSVCRT__unlock_file(file
);
4595 /*********************************************************************
4596 * vprintf (MSVCRT.@)
4598 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4600 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4603 /*********************************************************************
4604 * vprintf_s (MSVCRT.@)
4606 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4608 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4611 /*********************************************************************
4612 * vwprintf (MSVCRT.@)
4614 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4616 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4619 /*********************************************************************
4620 * vwprintf_s (MSVCRT.@)
4622 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4624 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4627 /*********************************************************************
4628 * fprintf (MSVCRT.@)
4630 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4632 __ms_va_list valist
;
4634 __ms_va_start(valist
, format
);
4635 res
= MSVCRT_vfprintf(file
, format
, valist
);
4636 __ms_va_end(valist
);
4640 /*********************************************************************
4641 * fprintf_s (MSVCRT.@)
4643 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
4645 __ms_va_list valist
;
4647 __ms_va_start(valist
, format
);
4648 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
4649 __ms_va_end(valist
);
4653 /*********************************************************************
4654 * fwprintf (MSVCRT.@)
4656 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4658 __ms_va_list valist
;
4660 __ms_va_start(valist
, format
);
4661 res
= MSVCRT_vfwprintf(file
, format
, valist
);
4662 __ms_va_end(valist
);
4666 /*********************************************************************
4667 * fwprintf_s (MSVCRT.@)
4669 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4671 __ms_va_list valist
;
4673 __ms_va_start(valist
, format
);
4674 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
4675 __ms_va_end(valist
);
4679 /*********************************************************************
4680 * _fwprintf_l (MSVCRT.@)
4682 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
4684 __ms_va_list valist
;
4686 __ms_va_start(valist
, locale
);
4687 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
4688 __ms_va_end(valist
);
4692 /*********************************************************************
4695 int CDECL
MSVCRT_printf(const char *format
, ...)
4697 __ms_va_list valist
;
4699 __ms_va_start(valist
, format
);
4700 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
4701 __ms_va_end(valist
);
4705 /*********************************************************************
4706 * printf_s (MSVCRT.@)
4708 int CDECL
MSVCRT_printf_s(const char *format
, ...)
4710 __ms_va_list valist
;
4712 __ms_va_start(valist
, format
);
4713 res
= MSVCRT_vprintf_s(format
, valist
);
4714 __ms_va_end(valist
);
4718 /*********************************************************************
4721 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
4723 if (c
== MSVCRT_EOF
)
4726 MSVCRT__lock_file(file
);
4727 if(file
->_bufsiz
== 0 && msvcrt_alloc_buffer(file
))
4729 if(file
->_ptr
>file
->_base
) {
4731 if(file
->_flag
& MSVCRT__IOSTRG
) {
4732 if(*file
->_ptr
!= c
) {
4734 MSVCRT__unlock_file(file
);
4741 MSVCRT_clearerr(file
);
4742 MSVCRT__unlock_file(file
);
4746 MSVCRT__unlock_file(file
);
4750 /*********************************************************************
4751 * ungetwc (MSVCRT.@)
4753 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4755 MSVCRT_wchar_t mwc
= wc
;
4757 if (wc
== MSVCRT_WEOF
)
4760 MSVCRT__lock_file(file
);
4762 if((msvcrt_get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
4763 || !(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
4764 unsigned char * pp
= (unsigned char *)&mwc
;
4767 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
4768 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
)) {
4769 MSVCRT__unlock_file(file
);
4774 char mbs
[MSVCRT_MB_LEN_MAX
];
4777 len
= MSVCRT_wctomb(mbs
, mwc
);
4779 MSVCRT__unlock_file(file
);
4783 for(len
--; len
>=0; len
--) {
4784 if(mbs
[len
] != MSVCRT_ungetc(mbs
[len
], file
)) {
4785 MSVCRT__unlock_file(file
);
4791 MSVCRT__unlock_file(file
);
4795 /*********************************************************************
4796 * wprintf (MSVCRT.@)
4798 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
4800 __ms_va_list valist
;
4802 __ms_va_start(valist
, format
);
4803 res
= MSVCRT_vwprintf(format
, valist
);
4804 __ms_va_end(valist
);
4808 /*********************************************************************
4809 * wprintf_s (MSVCRT.@)
4811 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
4813 __ms_va_list valist
;
4815 __ms_va_start(valist
, format
);
4816 res
= MSVCRT_vwprintf_s(format
, valist
);
4817 __ms_va_end(valist
);
4821 /*********************************************************************
4822 * _getmaxstdio (MSVCRT.@)
4824 int CDECL
MSVCRT__getmaxstdio(void)
4826 return MSVCRT_max_streams
;
4829 /*********************************************************************
4830 * _setmaxstdio (MSVCRT.@)
4832 int CDECL
MSVCRT__setmaxstdio(int newmax
)
4834 TRACE("%d\n", newmax
);
4836 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
4839 MSVCRT_max_streams
= newmax
;
4840 return MSVCRT_max_streams
;