2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
59 #define MSVCRT_R_OK 0x04
61 /* values for wxflag in file descriptor */
64 #define WX_READNL 0x04 /* read started with \n */
66 #define WX_DONTINHERIT 0x10
67 #define WX_APPEND 0x20
71 /* values for exflag - it's used differently in msvcr90.dll*/
74 #define EF_CRIT_INIT 0x04
75 #define EF_UNK_UNICODE 0x08
77 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
78 static char utf16_bom
[2] = { 0xff, 0xfe };
80 /* FIXME: this should be allocated dynamically */
81 #define MSVCRT_MAX_FILES 2048
82 #define MSVCRT_FD_BLOCK_SIZE 32
84 #define MSVCRT_INTERNAL_BUFSIZ 4096
86 /* ioinfo structure size is different in msvcrXX.dll's */
92 CRITICAL_SECTION crit
;
98 BOOL utf8translations
;
106 /*********************************************************************
107 * __pioinfo (MSVCRT.@)
108 * array of pointers to ioinfo arrays [32]
110 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
112 /*********************************************************************
113 * __badioinfo (MSVCRT.@)
115 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
119 CRITICAL_SECTION crit
;
122 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
123 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
124 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
126 /* INTERNAL: process umask */
127 static int MSVCRT_umask
= 0;
129 /* INTERNAL: static data for tmpnam and _wtmpname functions */
130 static int tmpnam_unique
;
132 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
133 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
134 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
135 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
137 #define TOUL(x) (ULONGLONG)(x)
138 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
139 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
140 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
141 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
143 /* This critical section protects the MSVCRT_fstreams table
144 * and MSVCRT_stream_idx from race conditions. It also
145 * protects fd critical sections creation code.
147 static CRITICAL_SECTION MSVCRT_file_cs
;
148 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
150 0, 0, &MSVCRT_file_cs
,
151 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
152 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
154 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
155 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
156 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
158 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
160 buf
->st_dev
= buf64
->st_dev
;
161 buf
->st_ino
= buf64
->st_ino
;
162 buf
->st_mode
= buf64
->st_mode
;
163 buf
->st_nlink
= buf64
->st_nlink
;
164 buf
->st_uid
= buf64
->st_uid
;
165 buf
->st_gid
= buf64
->st_gid
;
166 buf
->st_rdev
= buf64
->st_rdev
;
167 buf
->st_size
= buf64
->st_size
;
168 buf
->st_atime
= buf64
->st_atime
;
169 buf
->st_mtime
= buf64
->st_mtime
;
170 buf
->st_ctime
= buf64
->st_ctime
;
173 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
175 buf
->st_dev
= buf64
->st_dev
;
176 buf
->st_ino
= buf64
->st_ino
;
177 buf
->st_mode
= buf64
->st_mode
;
178 buf
->st_nlink
= buf64
->st_nlink
;
179 buf
->st_uid
= buf64
->st_uid
;
180 buf
->st_gid
= buf64
->st_gid
;
181 buf
->st_rdev
= buf64
->st_rdev
;
182 buf
->st_size
= buf64
->st_size
;
183 buf
->st_atime
= buf64
->st_atime
;
184 buf
->st_mtime
= buf64
->st_mtime
;
185 buf
->st_ctime
= buf64
->st_ctime
;
188 static void msvcrt_stat64_to_stat32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32
*buf
)
190 buf
->st_dev
= buf64
->st_dev
;
191 buf
->st_ino
= buf64
->st_ino
;
192 buf
->st_mode
= buf64
->st_mode
;
193 buf
->st_nlink
= buf64
->st_nlink
;
194 buf
->st_uid
= buf64
->st_uid
;
195 buf
->st_gid
= buf64
->st_gid
;
196 buf
->st_rdev
= buf64
->st_rdev
;
197 buf
->st_size
= buf64
->st_size
;
198 buf
->st_atime
= buf64
->st_atime
;
199 buf
->st_mtime
= buf64
->st_mtime
;
200 buf
->st_ctime
= buf64
->st_ctime
;
203 static void msvcrt_stat64_to_stat64i32(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat64i32
*buf
)
205 buf
->st_dev
= buf64
->st_dev
;
206 buf
->st_ino
= buf64
->st_ino
;
207 buf
->st_mode
= buf64
->st_mode
;
208 buf
->st_nlink
= buf64
->st_nlink
;
209 buf
->st_uid
= buf64
->st_uid
;
210 buf
->st_gid
= buf64
->st_gid
;
211 buf
->st_rdev
= buf64
->st_rdev
;
212 buf
->st_size
= buf64
->st_size
;
213 buf
->st_atime
= buf64
->st_atime
;
214 buf
->st_mtime
= buf64
->st_mtime
;
215 buf
->st_ctime
= buf64
->st_ctime
;
218 static void msvcrt_stat64_to_stat32i64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat32i64
*buf
)
220 buf
->st_dev
= buf64
->st_dev
;
221 buf
->st_ino
= buf64
->st_ino
;
222 buf
->st_mode
= buf64
->st_mode
;
223 buf
->st_nlink
= buf64
->st_nlink
;
224 buf
->st_uid
= buf64
->st_uid
;
225 buf
->st_gid
= buf64
->st_gid
;
226 buf
->st_rdev
= buf64
->st_rdev
;
227 buf
->st_size
= buf64
->st_size
;
228 buf
->st_atime
= buf64
->st_atime
;
229 buf
->st_mtime
= buf64
->st_mtime
;
230 buf
->st_ctime
= buf64
->st_ctime
;
233 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
235 /* 1601 to 1970 is 369 years plus 89 leap days */
236 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
238 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
239 ft
->dwHighDateTime
= ticks
>> 32;
240 ft
->dwLowDateTime
= ticks
;
243 static inline ioinfo
* get_ioinfo_nolock(int fd
)
246 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
247 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
249 return &MSVCRT___badioinfo
;
251 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
254 static inline void init_ioinfo_cs(ioinfo
*info
)
256 if(!(info
->exflag
& EF_CRIT_INIT
)) {
258 if(!(info
->exflag
& EF_CRIT_INIT
)) {
259 InitializeCriticalSection(&info
->crit
);
260 info
->exflag
|= EF_CRIT_INIT
;
266 static inline ioinfo
* get_ioinfo(int fd
)
268 ioinfo
*ret
= get_ioinfo_nolock(fd
);
269 if(ret
== &MSVCRT___badioinfo
)
272 EnterCriticalSection(&ret
->crit
);
276 static inline BOOL
alloc_pioinfo_block(int fd
)
281 if(fd
<0 || fd
>=MSVCRT_MAX_FILES
)
283 *MSVCRT__errno() = MSVCRT_ENFILE
;
287 block
= MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
290 WARN(":out of memory!\n");
291 *MSVCRT__errno() = MSVCRT_ENOMEM
;
294 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
295 block
[i
].handle
= INVALID_HANDLE_VALUE
;
296 if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
], block
, NULL
))
301 static inline ioinfo
* get_ioinfo_alloc_fd(int fd
)
305 ret
= get_ioinfo(fd
);
306 if(ret
!= &MSVCRT___badioinfo
)
309 if(!alloc_pioinfo_block(fd
))
310 return &MSVCRT___badioinfo
;
312 return get_ioinfo(fd
);
315 static inline ioinfo
* get_ioinfo_alloc(int *fd
)
320 for(i
=0; i
<MSVCRT_MAX_FILES
; i
++)
322 ioinfo
*info
= get_ioinfo_nolock(i
);
324 if(info
== &MSVCRT___badioinfo
)
326 if(!alloc_pioinfo_block(i
))
327 return &MSVCRT___badioinfo
;
328 info
= get_ioinfo_nolock(i
);
331 init_ioinfo_cs(info
);
332 if(TryEnterCriticalSection(&info
->crit
))
334 if(info
->handle
== INVALID_HANDLE_VALUE
)
339 LeaveCriticalSection(&info
->crit
);
343 WARN(":files exhausted!\n");
344 *MSVCRT__errno() = MSVCRT_ENFILE
;
345 return &MSVCRT___badioinfo
;
348 static inline void release_ioinfo(ioinfo
*info
)
350 if(info
!=&MSVCRT___badioinfo
&& info
->exflag
& EF_CRIT_INIT
)
351 LeaveCriticalSection(&info
->crit
);
354 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
358 if(i
>= MSVCRT_max_streams
)
362 return &MSVCRT__iob
[i
];
364 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
366 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
367 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
368 ERR("out of memory\n");
369 *MSVCRT__errno() = MSVCRT_ENOMEM
;
373 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
375 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
380 /* INTERNAL: free a file entry fd */
381 static void msvcrt_free_fd(int fd
)
383 ioinfo
*fdinfo
= get_ioinfo(fd
);
385 if(fdinfo
!= &MSVCRT___badioinfo
)
387 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
390 TRACE(":fd (%d) freed\n",fd
);
397 SetStdHandle(STD_INPUT_HANDLE
, 0);
400 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
403 SetStdHandle(STD_ERROR_HANDLE
, 0);
407 release_ioinfo(fdinfo
);
410 static void msvcrt_set_fd(ioinfo
*fdinfo
, HANDLE hand
, int flag
)
412 fdinfo
->handle
= hand
;
413 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
414 fdinfo
->lookahead
[0] = '\n';
415 fdinfo
->lookahead
[1] = '\n';
416 fdinfo
->lookahead
[2] = '\n';
417 fdinfo
->exflag
&= EF_CRIT_INIT
;
419 switch (fdinfo
-MSVCRT___pioinfo
[0])
421 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
422 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
423 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
427 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
428 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
431 ioinfo
*info
= get_ioinfo_alloc(&fd
);
433 TRACE(":handle (%p) allocating fd (%d)\n", hand
, fd
);
435 if(info
== &MSVCRT___badioinfo
)
438 msvcrt_set_fd(info
, hand
, flag
);
439 release_ioinfo(info
);
443 /* INTERNAL: Allocate a FILE* for an fd slot */
444 /* caller must hold the files lock */
445 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
450 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
452 file
= msvcrt_get_file(i
);
456 if (file
->_flag
== 0)
458 if (i
== MSVCRT_stream_idx
)
460 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
462 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
463 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
474 /* INTERNAL: initialize a FILE* from an open fd */
475 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
477 TRACE(":fd (%d) allocating FILE*\n",fd
);
478 if (!(get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
))
480 WARN(":invalid fd %d\n",fd
);
481 *MSVCRT___doserrno() = 0;
482 *MSVCRT__errno() = MSVCRT_EBADF
;
485 file
->_ptr
= file
->_base
= NULL
;
488 file
->_flag
= stream_flags
;
489 file
->_tmpfname
= NULL
;
491 TRACE(":got FILE* (%p)\n",file
);
495 /* INTERNAL: Create an inheritance data block (for spawned process)
496 * The inheritance block is made of:
497 * 00 int nb of file descriptor (NBFD)
498 * 04 char file flags (wxflag): repeated for each fd
499 * 4+NBFD HANDLE file handle: repeated for each fd
501 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
508 for (last_fd
=MSVCRT_MAX_FILES
-1; last_fd
>=0; last_fd
--)
509 if (get_ioinfo_nolock(last_fd
)->handle
!= INVALID_HANDLE_VALUE
)
513 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * last_fd
;
514 *block
= MSVCRT_calloc(*size
, 1);
520 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
521 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ last_fd
);
523 *(unsigned*)*block
= last_fd
;
524 for (fd
= 0; fd
< last_fd
; fd
++)
526 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
527 fdinfo
= get_ioinfo(fd
);
528 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
530 *wxflag_ptr
= fdinfo
->wxflag
;
531 *handle_ptr
= fdinfo
->handle
;
536 *handle_ptr
= INVALID_HANDLE_VALUE
;
538 release_ioinfo(fdinfo
);
539 wxflag_ptr
++; handle_ptr
++;
544 /* INTERNAL: Set up all file descriptors,
545 * as well as default streams (stdin, stderr and stdout)
547 void msvcrt_init_io(void)
553 GetStartupInfoA(&si
);
554 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
560 count
= *(unsigned*)si
.lpReserved2
;
561 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
562 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
564 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
565 count
= min(count
, MSVCRT_MAX_FILES
);
566 for (i
= 0; i
< count
; i
++)
568 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
570 fdinfo
= get_ioinfo_alloc_fd(i
);
571 if(fdinfo
!= &MSVCRT___badioinfo
)
572 msvcrt_set_fd(fdinfo
, *handle_ptr
, *wxflag_ptr
);
573 release_ioinfo(fdinfo
);
576 wxflag_ptr
++; handle_ptr
++;
580 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDIN_FILENO
);
581 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
582 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
583 DWORD type
= GetFileType(h
);
585 msvcrt_set_fd(fdinfo
, h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
586 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0));
588 release_ioinfo(fdinfo
);
590 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDOUT_FILENO
);
591 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
592 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
593 DWORD type
= GetFileType(h
);
595 msvcrt_set_fd(fdinfo
, h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
596 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0));
598 release_ioinfo(fdinfo
);
600 fdinfo
= get_ioinfo_alloc_fd(MSVCRT_STDERR_FILENO
);
601 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
602 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
603 DWORD type
= GetFileType(h
);
605 msvcrt_set_fd(fdinfo
, h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
606 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0));
608 release_ioinfo(fdinfo
);
610 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(MSVCRT_STDIN_FILENO
)->handle
,
611 get_ioinfo_nolock(MSVCRT_STDOUT_FILENO
)->handle
,
612 get_ioinfo_nolock(MSVCRT_STDERR_FILENO
)->handle
);
614 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
615 for (i
= 0; i
< 3; i
++)
617 /* FILE structs for stdin/out/err are static and never deleted */
618 MSVCRT__iob
[i
]._file
= i
;
619 MSVCRT__iob
[i
]._tmpfname
= NULL
;
620 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
622 MSVCRT_stream_idx
= 3;
625 /* INTERNAL: Flush stdio file buffer */
626 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
628 if((file
->_flag
& (MSVCRT__IOREAD
|MSVCRT__IOWRT
)) == MSVCRT__IOWRT
&&
629 file
->_flag
& (MSVCRT__IOMYBUF
|MSVCRT__USERBUF
)) {
630 int cnt
=file
->_ptr
-file
->_base
;
631 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
632 file
->_flag
|= MSVCRT__IOERR
;
636 if(file
->_flag
& MSVCRT__IORW
)
637 file
->_flag
&= ~MSVCRT__IOWRT
;
640 file
->_ptr
=file
->_base
;
645 /*********************************************************************
648 int CDECL
MSVCRT__isatty(int fd
)
650 TRACE(":fd (%d)\n",fd
);
652 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
655 /* INTERNAL: Allocate stdio file buffer */
656 static BOOL
msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
658 if((file
->_file
==MSVCRT_STDOUT_FILENO
|| file
->_file
==MSVCRT_STDERR_FILENO
)
659 && MSVCRT__isatty(file
->_file
))
662 file
->_base
= MSVCRT_calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
664 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
665 file
->_flag
|= MSVCRT__IOMYBUF
;
667 file
->_base
= (char*)(&file
->_charbuf
);
669 file
->_flag
|= MSVCRT__IONBF
;
671 file
->_ptr
= file
->_base
;
676 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
677 static BOOL
add_std_buffer(MSVCRT_FILE
*file
)
679 static char buffers
[2][MSVCRT_BUFSIZ
];
681 if((file
->_file
!=MSVCRT_STDOUT_FILENO
&& file
->_file
!=MSVCRT_STDERR_FILENO
)
682 || (file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
683 || !MSVCRT__isatty(file
->_file
))
686 file
->_ptr
= file
->_base
= buffers
[file
->_file
== MSVCRT_STDOUT_FILENO
? 0 : 1];
687 file
->_bufsiz
= file
->_cnt
= MSVCRT_BUFSIZ
;
688 file
->_flag
|= MSVCRT__USERBUF
;
692 /* INTERNAL: Removes temporary buffer from stdout or stderr */
693 /* Only call this function when add_std_buffer returned TRUE */
694 static void remove_std_buffer(MSVCRT_FILE
*file
)
696 msvcrt_flush_buffer(file
);
697 file
->_ptr
= file
->_base
= NULL
;
698 file
->_bufsiz
= file
->_cnt
= 0;
699 file
->_flag
&= ~MSVCRT__USERBUF
;
702 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
703 static int msvcrt_int_to_base32(int num
, char *str
)
718 *p
= (num
& 31) + '0';
720 *p
+= ('a' - '0' - 10);
727 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
728 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
743 *p
= (num
& 31) + '0';
745 *p
+= ('a' - '0' - 10);
752 /*********************************************************************
753 * __iob_func(MSVCRT.@)
755 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
757 return &MSVCRT__iob
[0];
760 /*********************************************************************
763 int CDECL
MSVCRT__access(const char *filename
, int mode
)
765 DWORD attr
= GetFileAttributesA(filename
);
767 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
769 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
771 msvcrt_set_errno(GetLastError());
774 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
776 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
782 /*********************************************************************
783 * _access_s (MSVCRT.@)
785 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
787 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
788 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
790 if (MSVCRT__access(filename
, mode
) == -1)
791 return *MSVCRT__errno();
795 /*********************************************************************
796 * _waccess (MSVCRT.@)
798 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
800 DWORD attr
= GetFileAttributesW(filename
);
802 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
804 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
806 msvcrt_set_errno(GetLastError());
809 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
811 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
817 /*********************************************************************
818 * _waccess_s (MSVCRT.@)
820 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
822 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
823 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
825 if (MSVCRT__waccess(filename
, mode
) == -1)
826 return *MSVCRT__errno();
830 /*********************************************************************
833 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
835 DWORD oldFlags
= GetFileAttributesA(path
);
837 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
839 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
840 oldFlags
| FILE_ATTRIBUTE_READONLY
;
842 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
845 msvcrt_set_errno(GetLastError());
849 /*********************************************************************
852 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
854 DWORD oldFlags
= GetFileAttributesW(path
);
856 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
858 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
859 oldFlags
| FILE_ATTRIBUTE_READONLY
;
861 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
864 msvcrt_set_errno(GetLastError());
868 /*********************************************************************
871 int CDECL
MSVCRT__unlink(const char *path
)
873 TRACE("%s\n",debugstr_a(path
));
874 if(DeleteFileA(path
))
876 TRACE("failed (%d)\n",GetLastError());
877 msvcrt_set_errno(GetLastError());
881 /*********************************************************************
882 * _wunlink (MSVCRT.@)
884 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
886 TRACE("(%s)\n",debugstr_w(path
));
887 if(DeleteFileW(path
))
889 TRACE("failed (%d)\n",GetLastError());
890 msvcrt_set_errno(GetLastError());
894 /*********************************************************************
897 int CDECL
MSVCRT__commit(int fd
)
899 ioinfo
*info
= get_ioinfo(fd
);
902 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
904 if (info
->handle
== INVALID_HANDLE_VALUE
)
906 else if (!FlushFileBuffers(info
->handle
))
908 if (GetLastError() == ERROR_INVALID_HANDLE
)
910 /* FlushFileBuffers fails for console handles
911 * so we ignore this error.
917 TRACE(":failed-last error (%d)\n",GetLastError());
918 msvcrt_set_errno(GetLastError());
928 release_ioinfo(info
);
932 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
933 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
935 /* INTERNAL: Flush all stream buffer */
936 static int msvcrt_flush_all_buffers(int mask
)
938 int i
, num_flushed
= 0;
942 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
943 file
= msvcrt_get_file(i
);
947 if(file
->_flag
& mask
) {
955 TRACE(":flushed (%d) handles\n",num_flushed
);
959 /*********************************************************************
960 * _flushall (MSVCRT.@)
962 int CDECL
MSVCRT__flushall(void)
964 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
967 /*********************************************************************
970 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
975 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
978 MSVCRT__lock_file(file
);
979 ret
= MSVCRT__fflush_nolock(file
);
980 MSVCRT__unlock_file(file
);
986 /*********************************************************************
987 * _fflush_nolock (MSVCRT.@)
989 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
994 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
998 res
= msvcrt_flush_buffer(file
);
999 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1000 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
1004 /*********************************************************************
1007 int CDECL
MSVCRT__close(int fd
)
1009 ioinfo
*info
= get_ioinfo(fd
);
1012 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1013 if (!(info
->wxflag
& WX_OPEN
)) {
1016 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1019 WARN(":failed-last error (%d)\n",GetLastError());
1020 msvcrt_set_errno(GetLastError());
1023 release_ioinfo(info
);
1027 /*********************************************************************
1030 * MSDN isn't clear on this point, but the remarks for _pipe
1031 * indicate file descriptors duplicated with _dup and _dup2 are always
1034 int CDECL
MSVCRT__dup2(int od
, int nd
)
1036 ioinfo
*info_od
, *info_nd
;
1039 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1043 info_od
= get_ioinfo(od
);
1044 info_nd
= get_ioinfo_alloc_fd(nd
);
1048 info_nd
= get_ioinfo_alloc_fd(nd
);
1049 info_od
= get_ioinfo(od
);
1052 if (info_nd
== &MSVCRT___badioinfo
)
1056 else if (info_od
->wxflag
& WX_OPEN
)
1060 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1061 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1063 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1065 if (info_nd
->wxflag
& WX_OPEN
)
1068 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1069 /* _dup2 returns 0, not nd, on success */
1075 msvcrt_set_errno(GetLastError());
1080 *MSVCRT__errno() = MSVCRT_EBADF
;
1084 release_ioinfo(info_od
);
1085 release_ioinfo(info_nd
);
1089 /*********************************************************************
1092 int CDECL
MSVCRT__dup(int od
)
1095 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1097 if (MSVCRT__dup2(od
, fd
) == 0)
1101 release_ioinfo(info
);
1105 /*********************************************************************
1108 int CDECL
MSVCRT__eof(int fd
)
1110 ioinfo
*info
= get_ioinfo(fd
);
1111 DWORD curpos
,endpos
;
1112 LONG hcurpos
,hendpos
;
1114 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1116 if (info
->handle
== INVALID_HANDLE_VALUE
)
1118 release_ioinfo(info
);
1122 if (info
->wxflag
& WX_ATEOF
)
1124 release_ioinfo(info
);
1128 /* Otherwise we do it the hard way */
1129 hcurpos
= hendpos
= 0;
1130 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1131 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1133 if (curpos
== endpos
&& hcurpos
== hendpos
)
1135 /* FIXME: shouldn't WX_ATEOF be set here? */
1136 release_ioinfo(info
);
1140 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1141 release_ioinfo(info
);
1145 /*********************************************************************
1146 * _fcloseall (MSVCRT.@)
1148 int CDECL
MSVCRT__fcloseall(void)
1150 int num_closed
= 0, i
;
1154 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1155 file
= msvcrt_get_file(i
);
1157 if (file
->_flag
&& !MSVCRT_fclose(file
))
1162 TRACE(":closed (%d) handles\n",num_closed
);
1166 /* free everything on process exit */
1167 void msvcrt_free_io(void)
1173 MSVCRT__fcloseall();
1175 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1177 if(!MSVCRT___pioinfo
[i
])
1180 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1182 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1183 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1185 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1188 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1190 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1191 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1193 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1194 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1198 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1199 MSVCRT_free(MSVCRT_fstream
[i
]);
1201 DeleteCriticalSection(&MSVCRT_file_cs
);
1204 /*********************************************************************
1205 * _lseeki64 (MSVCRT.@)
1207 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1209 ioinfo
*info
= get_ioinfo(fd
);
1212 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1214 if (info
->handle
== INVALID_HANDLE_VALUE
)
1216 release_ioinfo(info
);
1220 if (whence
< 0 || whence
> 2)
1222 release_ioinfo(info
);
1223 *MSVCRT__errno() = MSVCRT_EINVAL
;
1227 TRACE(":fd (%d) to %s pos %s\n",
1228 fd
,wine_dbgstr_longlong(offset
),
1229 (whence
==SEEK_SET
)?"SEEK_SET":
1230 (whence
==SEEK_CUR
)?"SEEK_CUR":
1231 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1233 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1234 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1235 ofs
.QuadPart
= offset
;
1236 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1237 GetLastError() == ERROR_SUCCESS
)
1239 info
->wxflag
&= ~WX_ATEOF
;
1240 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1242 release_ioinfo(info
);
1243 return ofs
.QuadPart
;
1245 release_ioinfo(info
);
1246 TRACE(":error-last error (%d)\n",GetLastError());
1247 msvcrt_set_errno(GetLastError());
1251 /*********************************************************************
1254 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1256 return MSVCRT__lseeki64(fd
, offset
, whence
);
1259 /*********************************************************************
1260 * _lock_file (MSVCRT.@)
1262 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1264 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1265 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1267 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1270 /*********************************************************************
1271 * _unlock_file (MSVCRT.@)
1273 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1275 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1276 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1278 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1281 /*********************************************************************
1282 * _locking (MSVCRT.@)
1284 * This is untested; the underlying LockFile doesn't work yet.
1286 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1288 ioinfo
*info
= get_ioinfo(fd
);
1292 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1293 if (info
->handle
== INVALID_HANDLE_VALUE
)
1295 release_ioinfo(info
);
1299 if (mode
< 0 || mode
> 4)
1301 release_ioinfo(info
);
1302 *MSVCRT__errno() = MSVCRT_EINVAL
;
1306 TRACE(":fd (%d) by 0x%08x mode %s\n",
1307 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1308 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1309 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1310 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1311 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1314 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1316 release_ioinfo(info
);
1317 FIXME ("Seek failed\n");
1318 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1321 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1324 ret
= 1; /* just to satisfy gcc */
1327 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1332 else if (mode
== MSVCRT__LK_UNLCK
)
1333 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1335 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1336 /* FIXME - what about error settings? */
1337 release_ioinfo(info
);
1338 return ret
? 0 : -1;
1341 /*********************************************************************
1342 * _fseeki64 (MSVCRT.@)
1344 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1348 MSVCRT__lock_file(file
);
1349 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1350 MSVCRT__unlock_file(file
);
1355 /*********************************************************************
1356 * _fseeki64_nolock (MSVCRT.@)
1358 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1362 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1364 offset
+= MSVCRT__ftelli64_nolock(file
);
1367 /* Flush output if needed */
1368 msvcrt_flush_buffer(file
);
1369 /* Reset direction of i/o */
1370 if(file
->_flag
& MSVCRT__IORW
) {
1371 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1373 /* Clear end of file flag */
1374 file
->_flag
&= ~MSVCRT__IOEOF
;
1375 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1380 /*********************************************************************
1383 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1385 return MSVCRT__fseeki64( file
, offset
, whence
);
1388 /*********************************************************************
1389 * _fseek_nolock (MSVCRT.@)
1391 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1393 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1396 /*********************************************************************
1397 * _chsize_s (MSVCRT.@)
1399 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1405 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1407 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1410 info
= get_ioinfo(fd
);
1411 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1413 /* save the current file pointer */
1414 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1417 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1420 ret
= SetEndOfFile(info
->handle
);
1421 if (!ret
) msvcrt_set_errno(GetLastError());
1424 /* restore the file pointer */
1425 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1429 release_ioinfo(info
);
1430 return ret
? 0 : *MSVCRT__errno();
1433 /*********************************************************************
1434 * _chsize (MSVCRT.@)
1436 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1438 /* _chsize_s returns errno on failure but _chsize should return -1 */
1439 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1442 /*********************************************************************
1443 * clearerr (MSVCRT.@)
1445 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1447 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1449 MSVCRT__lock_file(file
);
1450 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1451 MSVCRT__unlock_file(file
);
1454 /*********************************************************************
1457 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1459 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1461 MSVCRT__lock_file(file
);
1462 MSVCRT__fseek_nolock(file
, 0L, SEEK_SET
);
1463 MSVCRT_clearerr(file
);
1464 MSVCRT__unlock_file(file
);
1467 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1469 int plus
= strchrW(mode
, '+') != NULL
;
1471 TRACE("%s\n", debugstr_w(mode
));
1473 while(*mode
== ' ') mode
++;
1478 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1479 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1482 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1483 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1486 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1487 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1490 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1494 *stream_flags
|= MSVCRT__commode
;
1496 while (*mode
&& *mode
!=',')
1500 *open_flags
|= MSVCRT__O_BINARY
;
1501 *open_flags
&= ~MSVCRT__O_TEXT
;
1504 *open_flags
|= MSVCRT__O_TEXT
;
1505 *open_flags
&= ~MSVCRT__O_BINARY
;
1508 *open_flags
|= MSVCRT__O_TEMPORARY
;
1511 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1514 *stream_flags
|= MSVCRT__IOCOMMIT
;
1517 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1520 *open_flags
|= MSVCRT__O_NOINHERIT
;
1529 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1532 ERR("incorrect mode flag: %c\n", mode
[-1]);
1538 static const WCHAR ccs
[] = {'c','c','s'};
1539 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1540 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1541 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1544 while(*mode
== ' ') mode
++;
1545 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1547 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1548 while(*mode
== ' ') mode
++;
1549 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1552 while(*mode
== ' ') mode
++;
1554 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1556 *open_flags
|= MSVCRT__O_U8TEXT
;
1557 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1559 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1561 *open_flags
|= MSVCRT__O_U16TEXT
;
1562 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1564 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1566 *open_flags
|= MSVCRT__O_WTEXT
;
1567 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1571 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1575 while(*mode
== ' ') mode
++;
1578 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1583 /*********************************************************************
1584 * _fdopen (MSVCRT.@)
1586 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1589 MSVCRT_wchar_t
*modeW
= NULL
;
1591 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1593 ret
= MSVCRT__wfdopen(fd
, modeW
);
1599 /*********************************************************************
1600 * _wfdopen (MSVCRT.@)
1602 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1604 int open_flags
, stream_flags
;
1607 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1610 if (!(file
= msvcrt_alloc_fp()))
1612 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1617 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1623 /*********************************************************************
1624 * _filelength (MSVCRT.@)
1626 LONG CDECL
MSVCRT__filelength(int fd
)
1628 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1631 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1634 if (endPos
!= curPos
)
1635 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1642 /*********************************************************************
1643 * _filelengthi64 (MSVCRT.@)
1645 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1647 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1650 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1653 if (endPos
!= curPos
)
1654 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1661 /*********************************************************************
1662 * _fileno (MSVCRT.@)
1664 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1666 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1670 /*********************************************************************
1671 * _fstat64 (MSVCRT.@)
1673 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1675 ioinfo
*info
= get_ioinfo(fd
);
1678 BY_HANDLE_FILE_INFORMATION hfi
;
1680 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1681 if (info
->handle
== INVALID_HANDLE_VALUE
)
1683 release_ioinfo(info
);
1689 WARN(":failed-NULL buf\n");
1690 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1691 release_ioinfo(info
);
1695 memset(&hfi
, 0, sizeof(hfi
));
1696 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1697 type
= GetFileType(info
->handle
);
1698 if (type
== FILE_TYPE_PIPE
)
1700 buf
->st_dev
= buf
->st_rdev
= fd
;
1701 buf
->st_mode
= S_IFIFO
;
1704 else if (type
== FILE_TYPE_CHAR
)
1706 buf
->st_dev
= buf
->st_rdev
= fd
;
1707 buf
->st_mode
= S_IFCHR
;
1710 else /* FILE_TYPE_DISK etc. */
1712 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1714 WARN(":failed-last error (%d)\n",GetLastError());
1715 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1716 release_ioinfo(info
);
1719 buf
->st_mode
= S_IFREG
| 0444;
1720 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1721 buf
->st_mode
|= 0222;
1722 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1723 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1725 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1726 buf
->st_mtime
= buf
->st_ctime
= dw
;
1727 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1729 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1731 release_ioinfo(info
);
1735 /*********************************************************************
1736 * _fstati64 (MSVCRT.@)
1738 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1741 struct MSVCRT__stat64 buf64
;
1743 ret
= MSVCRT__fstat64(fd
, &buf64
);
1745 msvcrt_stat64_to_stati64(&buf64
, buf
);
1749 /*********************************************************************
1752 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1754 struct MSVCRT__stat64 buf64
;
1756 ret
= MSVCRT__fstat64(fd
, &buf64
);
1758 msvcrt_stat64_to_stat(&buf64
, buf
);
1762 /*********************************************************************
1763 * _fstat32 (MSVCR80.@)
1765 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1768 struct MSVCRT__stat64 buf64
;
1770 ret
= MSVCRT__fstat64(fd
, &buf64
);
1772 msvcrt_stat64_to_stat32(&buf64
, buf
);
1776 /*********************************************************************
1777 * _fstat64i32 (MSVCR80.@)
1779 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1782 struct MSVCRT__stat64 buf64
;
1784 ret
= MSVCRT__fstat64(fd
, &buf64
);
1786 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1790 /*********************************************************************
1791 * _futime64 (MSVCRT.@)
1793 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1795 ioinfo
*info
= get_ioinfo(fd
);
1800 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1805 time_to_filetime( t
->actime
, &at
);
1806 time_to_filetime( t
->modtime
, &wt
);
1809 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1811 release_ioinfo(info
);
1812 msvcrt_set_errno(GetLastError());
1815 release_ioinfo(info
);
1819 /*********************************************************************
1820 * _futime32 (MSVCRT.@)
1822 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1826 struct MSVCRT___utimbuf64 t64
;
1827 t64
.actime
= t
->actime
;
1828 t64
.modtime
= t
->modtime
;
1829 return _futime64( fd
, &t64
);
1832 return _futime64( fd
, NULL
);
1835 /*********************************************************************
1836 * _futime (MSVCRT.@)
1839 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1841 return _futime64( fd
, t
);
1844 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1846 return _futime32( fd
, t
);
1850 /*********************************************************************
1851 * _get_osfhandle (MSVCRT.@)
1853 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1855 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1856 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1858 if(hand
== INVALID_HANDLE_VALUE
)
1859 *MSVCRT__errno() = MSVCRT_EBADF
;
1860 return (MSVCRT_intptr_t
)hand
;
1863 /*********************************************************************
1864 * _mktemp_s (MSVCRT.@)
1866 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1870 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1871 return MSVCRT_EINVAL
;
1873 for(len
=0; len
<size
; len
++)
1876 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1879 return MSVCRT_EINVAL
;
1882 for(xno
=1; xno
<=6; xno
++)
1883 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1884 return MSVCRT_EINVAL
;
1886 id
= GetCurrentProcessId();
1887 for(xno
=1; xno
<6; xno
++) {
1888 pattern
[len
-xno
] = id
%10 + '0';
1892 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1893 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1898 *MSVCRT__errno() = MSVCRT_EEXIST
;
1899 return MSVCRT_EEXIST
;
1902 /*********************************************************************
1903 * _mktemp (MSVCRT.@)
1905 char * CDECL
MSVCRT__mktemp(char *pattern
)
1908 char *retVal
= pattern
;
1916 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1920 id
= GetCurrentProcessId();
1924 int tempNum
= id
/ 10;
1925 *pattern
-- = id
- (tempNum
* 10) + '0';
1931 *pattern
= letter
++;
1932 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1934 } while(letter
<= 'z');
1938 /*********************************************************************
1939 * _wmktemp_s (MSVCRT.@)
1941 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1945 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1946 return MSVCRT_EINVAL
;
1948 for(len
=0; len
<size
; len
++)
1951 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1954 return MSVCRT_EINVAL
;
1957 for(xno
=1; xno
<=6; xno
++)
1958 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1959 return MSVCRT_EINVAL
;
1961 id
= GetCurrentProcessId();
1962 for(xno
=1; xno
<6; xno
++) {
1963 pattern
[len
-xno
] = id
%10 + '0';
1967 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1968 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1973 *MSVCRT__errno() = MSVCRT_EEXIST
;
1974 return MSVCRT_EEXIST
;
1977 /*********************************************************************
1978 * _wmktemp (MSVCRT.@)
1980 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1983 MSVCRT_wchar_t
*retVal
= pattern
;
1985 MSVCRT_wchar_t letter
= 'a';
1991 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1995 id
= GetCurrentProcessId();
1999 int tempNum
= id
/ 10;
2000 *pattern
-- = id
- (tempNum
* 10) + '0';
2006 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2008 *pattern
= letter
++;
2009 } while(letter
!= '|');
2013 static unsigned split_oflags(unsigned oflags
)
2016 unsigned unsupp
; /* until we support everything */
2018 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2019 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2020 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2021 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2022 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2023 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2024 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2025 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2026 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2028 if ((unsupp
= oflags
& ~(
2029 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2030 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2031 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2032 MSVCRT__O_NOINHERIT
|
2033 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2034 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2036 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2041 /*********************************************************************
2044 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2047 SECURITY_ATTRIBUTES sa
;
2048 HANDLE readHandle
, writeHandle
;
2052 *MSVCRT__errno() = MSVCRT_EINVAL
;
2056 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2057 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2058 sa
.lpSecurityDescriptor
= NULL
;
2059 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2061 unsigned int wxflags
= split_oflags(textmode
);
2064 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2068 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2076 MSVCRT__close(pfds
[0]);
2077 CloseHandle(writeHandle
);
2078 *MSVCRT__errno() = MSVCRT_EMFILE
;
2083 CloseHandle(readHandle
);
2084 CloseHandle(writeHandle
);
2085 *MSVCRT__errno() = MSVCRT_EMFILE
;
2089 msvcrt_set_errno(GetLastError());
2094 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2096 char bom
[sizeof(utf8_bom
)];
2099 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2101 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2104 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2105 oflags
|= MSVCRT__O_U8TEXT
;
2106 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2108 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2109 oflags
|= MSVCRT__O_U16TEXT
;
2111 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2117 /*********************************************************************
2118 * _wsopen_s (MSVCRT.@)
2120 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2122 DWORD access
= 0, creation
= 0, attrib
;
2123 SECURITY_ATTRIBUTES sa
;
2124 DWORD sharing
, type
;
2128 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2129 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2131 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2134 wxflag
= split_oflags(oflags
);
2135 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2137 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2138 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2139 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2142 if (oflags
& MSVCRT__O_CREAT
)
2144 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2145 FIXME(": pmode 0x%04x ignored\n", pmode
);
2147 if (oflags
& MSVCRT__O_EXCL
)
2148 creation
= CREATE_NEW
;
2149 else if (oflags
& MSVCRT__O_TRUNC
)
2150 creation
= CREATE_ALWAYS
;
2152 creation
= OPEN_ALWAYS
;
2154 else /* no MSVCRT__O_CREAT */
2156 if (oflags
& MSVCRT__O_TRUNC
)
2157 creation
= TRUNCATE_EXISTING
;
2159 creation
= OPEN_EXISTING
;
2164 case MSVCRT__SH_DENYRW
:
2167 case MSVCRT__SH_DENYWR
:
2168 sharing
= FILE_SHARE_READ
;
2170 case MSVCRT__SH_DENYRD
:
2171 sharing
= FILE_SHARE_WRITE
;
2173 case MSVCRT__SH_DENYNO
:
2174 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2177 ERR( "Unhandled shflags 0x%x\n", shflags
);
2178 return MSVCRT_EINVAL
;
2181 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2182 attrib
= FILE_ATTRIBUTE_READONLY
;
2184 attrib
= FILE_ATTRIBUTE_NORMAL
;
2186 if (oflags
& MSVCRT__O_TEMPORARY
)
2188 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2190 sharing
|= FILE_SHARE_DELETE
;
2193 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2194 sa
.lpSecurityDescriptor
= NULL
;
2195 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2197 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2198 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2199 && !(access
&GENERIC_READ
))
2201 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2202 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2203 if (hand
!= INVALID_HANDLE_VALUE
)
2205 oflags
= check_bom(hand
, oflags
, FALSE
);
2209 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2212 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2213 if (hand
== INVALID_HANDLE_VALUE
) {
2214 WARN(":failed-last error (%d)\n",GetLastError());
2215 msvcrt_set_errno(GetLastError());
2216 return *MSVCRT__errno();
2219 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2221 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2222 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2223 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2225 if (oflags
& MSVCRT__O_U8TEXT
)
2227 DWORD written
= 0, tmp
;
2229 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2230 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2232 if (written
!= sizeof(utf8_bom
)) {
2233 WARN("error writing BOM\n");
2235 msvcrt_set_errno(GetLastError());
2236 return *MSVCRT__errno();
2241 DWORD written
= 0, tmp
;
2243 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2244 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2246 if (written
!= sizeof(utf16_bom
))
2248 WARN("error writing BOM\n");
2250 msvcrt_set_errno(GetLastError());
2251 return *MSVCRT__errno();
2255 else if (access
& GENERIC_READ
)
2256 oflags
= check_bom(hand
, oflags
, TRUE
);
2259 type
= GetFileType(hand
);
2260 if (type
== FILE_TYPE_CHAR
)
2262 else if (type
== FILE_TYPE_PIPE
)
2265 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2267 return *MSVCRT__errno();
2269 if (oflags
& MSVCRT__O_WTEXT
)
2270 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2271 else if (oflags
& MSVCRT__O_U16TEXT
)
2272 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2273 else if (oflags
& MSVCRT__O_U8TEXT
)
2274 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2276 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2280 /*********************************************************************
2281 * _wsopen (MSVCRT.@)
2283 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2288 if (oflags
& MSVCRT__O_CREAT
)
2292 __ms_va_start(ap
, shflags
);
2293 pmode
= va_arg(ap
, int);
2299 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2303 /*********************************************************************
2304 * _sopen_s (MSVCRT.@)
2306 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2308 MSVCRT_wchar_t
*pathW
;
2311 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2312 return MSVCRT_EINVAL
;
2314 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2315 return MSVCRT_EINVAL
;
2317 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2322 /*********************************************************************
2325 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2330 if (oflags
& MSVCRT__O_CREAT
)
2334 __ms_va_start(ap
, shflags
);
2335 pmode
= va_arg(ap
, int);
2341 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2345 /*********************************************************************
2348 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2352 if (flags
& MSVCRT__O_CREAT
)
2355 __ms_va_start(ap
, flags
);
2356 pmode
= va_arg(ap
, int);
2358 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2361 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2364 /*********************************************************************
2367 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2371 if (flags
& MSVCRT__O_CREAT
)
2374 __ms_va_start(ap
, flags
);
2375 pmode
= va_arg(ap
, int);
2377 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2380 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2383 /*********************************************************************
2386 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2388 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2389 return MSVCRT__open(path
, usedFlags
);
2392 /*********************************************************************
2393 * _wcreat (MSVCRT.@)
2395 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2397 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2398 return MSVCRT__wopen(path
, usedFlags
);
2401 /*********************************************************************
2402 * _open_osfhandle (MSVCRT.@)
2404 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2409 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2410 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2411 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2412 * text - it never sets MSVCRT__O_BINARY.
2414 /* don't let split_oflags() decide the mode if no mode is passed */
2415 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2416 oflags
|= MSVCRT__O_BINARY
;
2418 flags
= GetFileType((HANDLE
)handle
);
2419 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2421 msvcrt_set_errno(GetLastError());
2425 if (flags
== FILE_TYPE_CHAR
)
2427 else if (flags
== FILE_TYPE_PIPE
)
2431 flags
|= split_oflags(oflags
);
2433 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2434 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2438 /*********************************************************************
2441 int CDECL
MSVCRT__rmtmp(void)
2443 int num_removed
= 0, i
;
2447 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2448 file
= msvcrt_get_file(i
);
2450 if (file
->_tmpfname
)
2452 MSVCRT_fclose(file
);
2459 TRACE(":removed (%d) temp files\n",num_removed
);
2463 static inline int get_utf8_char_len(char ch
)
2465 if((ch
&0xf8) == 0xf0)
2467 else if((ch
&0xf0) == 0xe0)
2469 else if((ch
&0xe0) == 0xc0)
2474 /*********************************************************************
2475 * (internal) read_utf8
2477 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2479 HANDLE hand
= fdinfo
->handle
;
2480 char min_buf
[4], *readbuf
, lookahead
;
2481 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2483 /* make the buffer big enough to hold at least one character */
2484 /* read bytes have to fit to output and lookahead buffers */
2486 readbuf_size
= count
< 4 ? 4 : count
;
2487 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2492 if(fdinfo
->lookahead
[0] != '\n') {
2493 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2494 fdinfo
->lookahead
[0] = '\n';
2496 if(fdinfo
->lookahead
[1] != '\n') {
2497 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2498 fdinfo
->lookahead
[1] = '\n';
2500 if(fdinfo
->lookahead
[2] != '\n') {
2501 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2502 fdinfo
->lookahead
[2] = '\n';
2507 /* NOTE: this case is broken in native dll, reading
2508 * sometimes fails when small buffer is passed
2511 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2512 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2513 fdinfo
->wxflag
|= WX_ATEOF
;
2516 msvcrt_set_errno(GetLastError());
2519 }else if(!num_read
) {
2520 fdinfo
->wxflag
|= WX_ATEOF
;
2526 char_len
= get_utf8_char_len(readbuf
[0]);
2528 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2532 if(readbuf
[0] == '\n')
2533 fdinfo
->wxflag
|= WX_READNL
;
2535 fdinfo
->wxflag
&= ~WX_READNL
;
2537 if(readbuf
[0] == 0x1a) {
2538 fdinfo
->wxflag
|= WX_ATEOF
;
2542 if(readbuf
[0] == '\r') {
2543 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2545 else if(lookahead
== '\n')
2549 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2550 fdinfo
->lookahead
[0] = lookahead
;
2552 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2557 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2558 msvcrt_set_errno(GetLastError());
2565 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2568 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2569 fdinfo
->wxflag
|= WX_ATEOF
;
2570 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2573 msvcrt_set_errno(GetLastError());
2574 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2577 }else if(!pos
&& !num_read
) {
2578 fdinfo
->wxflag
|= WX_ATEOF
;
2579 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2584 if(readbuf
[0] == '\n')
2585 fdinfo
->wxflag
|= WX_READNL
;
2587 fdinfo
->wxflag
&= ~WX_READNL
;
2589 /* Find first byte of last character (may be incomplete) */
2590 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2591 if((readbuf
[i
]&0xc0) != 0x80)
2593 char_len
= get_utf8_char_len(readbuf
[i
]);
2594 if(char_len
+i
<= pos
)
2597 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2599 fdinfo
->lookahead
[0] = readbuf
[i
];
2601 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2603 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2605 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2609 for(i
=0, j
=0; i
<pos
; i
++) {
2610 if(readbuf
[i
] == 0x1a) {
2611 fdinfo
->wxflag
|= WX_ATEOF
;
2615 /* strip '\r' if followed by '\n' */
2616 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2617 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2618 readbuf
[j
++] = '\r';
2619 }else if(lookahead
== '\n' && j
==0) {
2620 readbuf
[j
++] = '\n';
2622 if(lookahead
!= '\n')
2623 readbuf
[j
++] = '\r';
2625 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2626 fdinfo
->lookahead
[0] = lookahead
;
2628 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2630 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2631 readbuf
[j
++] = readbuf
[i
];
2636 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2637 msvcrt_set_errno(GetLastError());
2638 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2642 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2646 /*********************************************************************
2649 * When reading \r as last character in text mode, read() positions
2650 * the file pointer on the \r character while getc() goes on to
2653 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2655 DWORD num_read
, utf16
;
2656 char *bufstart
= buf
;
2661 if (fdinfo
->wxflag
& WX_ATEOF
) {
2662 TRACE("already at EOF, returning 0\n");
2665 /* Don't trace small reads, it gets *very* annoying */
2667 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2668 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2670 *MSVCRT__errno() = MSVCRT_EBADF
;
2674 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2675 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2677 *MSVCRT__errno() = MSVCRT_EINVAL
;
2681 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2682 return read_utf8(fdinfo
, buf
, count
);
2684 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2686 if (fdinfo
->lookahead
[0] != '\n')
2688 bufstart
[0] = fdinfo
->lookahead
[0];
2689 fdinfo
->lookahead
[0] = '\n';
2693 bufstart
[1] = fdinfo
->lookahead
[1];
2694 fdinfo
->lookahead
[1] = '\n';
2697 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2698 num_read
+= 1+utf16
;
2703 if(utf16
&& (num_read
&1))
2705 /* msvcr90 uses uninitialized value from the buffer in this case */
2706 /* msvcrt ignores additional data */
2707 ERR("got odd number of bytes in UTF16 mode\n");
2711 if (count
!= 0 && num_read
== 0)
2713 fdinfo
->wxflag
|= WX_ATEOF
;
2714 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2716 else if (fdinfo
->wxflag
& WX_TEXT
)
2720 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2721 fdinfo
->wxflag
|= WX_READNL
;
2723 fdinfo
->wxflag
&= ~WX_READNL
;
2725 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2727 /* in text mode, a ctrl-z signals EOF */
2728 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2730 fdinfo
->wxflag
|= WX_ATEOF
;
2731 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2735 /* in text mode, strip \r if followed by \n */
2736 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2741 lookahead
[1] = '\n';
2742 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2744 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2746 bufstart
[j
++] = '\n';
2747 if(utf16
) bufstart
[j
++] = 0;
2751 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2753 bufstart
[j
++] = '\r';
2754 if(utf16
) bufstart
[j
++] = 0;
2757 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2759 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2761 bufstart
[j
++] = '\n';
2762 if (utf16
) bufstart
[j
++] = 0;
2766 fdinfo
->lookahead
[0] = lookahead
[0];
2767 fdinfo
->lookahead
[1] = lookahead
[1];
2771 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2776 bufstart
[j
++] = '\r';
2777 if(utf16
) bufstart
[j
++] = 0;
2780 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2781 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2783 bufstart
[j
++] = bufstart
[i
];
2784 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2792 if (GetLastError() == ERROR_BROKEN_PIPE
)
2794 TRACE(":end-of-pipe\n");
2795 fdinfo
->wxflag
|= WX_ATEOF
;
2800 TRACE(":failed-last error (%d)\n",GetLastError());
2806 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2810 /*********************************************************************
2813 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2815 ioinfo
*info
= get_ioinfo(fd
);
2816 int num_read
= read_i(fd
, info
, buf
, count
);
2817 release_ioinfo(info
);
2821 /*********************************************************************
2822 * _setmode (MSVCRT.@)
2824 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2826 ioinfo
*info
= get_ioinfo(fd
);
2827 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2828 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2829 ret
= MSVCRT__O_WTEXT
;
2831 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2832 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2833 *MSVCRT__errno() = MSVCRT_EINVAL
;
2834 release_ioinfo(info
);
2838 if(mode
== MSVCRT__O_BINARY
) {
2839 info
->wxflag
&= ~WX_TEXT
;
2840 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2841 release_ioinfo(info
);
2845 info
->wxflag
|= WX_TEXT
;
2846 if(mode
== MSVCRT__O_TEXT
)
2847 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2848 else if(mode
== MSVCRT__O_U8TEXT
)
2849 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2851 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2853 release_ioinfo(info
);
2857 /*********************************************************************
2858 * _stat64 (MSVCRT.@)
2860 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2863 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2864 unsigned short mode
= ALL_S_IREAD
;
2867 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2869 plen
= strlen(path
);
2870 while (plen
&& path
[plen
-1]==' ')
2873 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2874 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2876 *MSVCRT__errno() = MSVCRT_ENOENT
;
2880 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2882 TRACE("failed (%d)\n",GetLastError());
2883 *MSVCRT__errno() = MSVCRT_ENOENT
;
2887 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2889 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2890 Bon 011120: This FIXME seems incorrect
2891 Also a letter as first char isn't enough to be classified
2894 if (isalpha(*path
)&& (*(path
+1)==':'))
2895 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2897 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2899 /* Dir, or regular file? */
2900 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2901 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2904 mode
|= MSVCRT__S_IFREG
;
2906 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2908 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2909 (tolower(path
[plen
-3]) << 16);
2910 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2911 mode
|= ALL_S_IEXEC
;
2915 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2916 mode
|= ALL_S_IWRITE
;
2918 buf
->st_mode
= mode
;
2920 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2921 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2923 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2924 buf
->st_mtime
= buf
->st_ctime
= dw
;
2925 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2926 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2927 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2931 /*********************************************************************
2932 * _stati64 (MSVCRT.@)
2934 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2937 struct MSVCRT__stat64 buf64
;
2939 ret
= MSVCRT_stat64(path
, &buf64
);
2941 msvcrt_stat64_to_stati64(&buf64
, buf
);
2945 /*********************************************************************
2948 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2951 struct MSVCRT__stat64 buf64
;
2953 ret
= MSVCRT_stat64( path
, &buf64
);
2955 msvcrt_stat64_to_stat(&buf64
, buf
);
2959 /*********************************************************************
2960 * _stat32 (MSVCR100.@)
2962 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2965 struct MSVCRT__stat64 buf64
;
2967 ret
= MSVCRT_stat64(path
, &buf64
);
2969 msvcrt_stat64_to_stat32(&buf64
, buf
);
2973 /*********************************************************************
2974 * _stat32i64 (MSVCR100.@)
2976 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2979 struct MSVCRT__stat64 buf64
;
2981 ret
= MSVCRT_stat64(path
, &buf64
);
2983 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2987 /*********************************************************************
2988 * _stat64i32 (MSVCR100.@)
2990 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2993 struct MSVCRT__stat64 buf64
;
2995 ret
= MSVCRT_stat64(path
, &buf64
);
2997 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3001 /*********************************************************************
3002 * _wstat64 (MSVCRT.@)
3004 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
3007 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3008 unsigned short mode
= ALL_S_IREAD
;
3011 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3013 plen
= strlenW(path
);
3014 while (plen
&& path
[plen
-1]==' ')
3017 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
3018 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3020 *MSVCRT__errno() = MSVCRT_ENOENT
;
3024 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3026 TRACE("failed (%d)\n",GetLastError());
3027 *MSVCRT__errno() = MSVCRT_ENOENT
;
3031 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3033 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3034 if (MSVCRT_iswalpha(*path
))
3035 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3037 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3039 /* Dir, or regular file? */
3040 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3041 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3044 mode
|= MSVCRT__S_IFREG
;
3046 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3048 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3049 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3050 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3051 mode
|= ALL_S_IEXEC
;
3055 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3056 mode
|= ALL_S_IWRITE
;
3058 buf
->st_mode
= mode
;
3060 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3061 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3063 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3064 buf
->st_mtime
= buf
->st_ctime
= dw
;
3065 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3066 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3067 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3071 /*********************************************************************
3072 * _wstati64 (MSVCRT.@)
3074 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3077 struct MSVCRT__stat64 buf64
;
3079 ret
= MSVCRT__wstat64(path
, &buf64
);
3081 msvcrt_stat64_to_stati64(&buf64
, buf
);
3085 /*********************************************************************
3088 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3091 struct MSVCRT__stat64 buf64
;
3093 ret
= MSVCRT__wstat64( path
, &buf64
);
3094 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3098 /*********************************************************************
3099 * _wstat32 (MSVCR100.@)
3101 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3104 struct MSVCRT__stat64 buf64
;
3106 ret
= MSVCRT__wstat64(path
, &buf64
);
3108 msvcrt_stat64_to_stat32(&buf64
, buf
);
3112 /*********************************************************************
3113 * _wstat32i64 (MSVCR100.@)
3115 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3118 struct MSVCRT__stat64 buf64
;
3120 ret
= MSVCRT__wstat64(path
, &buf64
);
3122 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3126 /*********************************************************************
3127 * _wstat64i32 (MSVCR100.@)
3129 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3132 struct MSVCRT__stat64 buf64
;
3134 ret
= MSVCRT__wstat64(path
, &buf64
);
3136 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3140 /*********************************************************************
3143 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3145 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3148 /*********************************************************************
3149 * _telli64 (MSVCRT.@)
3151 __int64 CDECL
_telli64(int fd
)
3153 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3156 /*********************************************************************
3157 * _tempnam (MSVCRT.@)
3159 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3161 char tmpbuf
[MAX_PATH
];
3162 const char *tmp_dir
= MSVCRT_getenv("TMP");
3164 if (tmp_dir
) dir
= tmp_dir
;
3166 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3167 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3169 TRACE("got name (%s)\n",tmpbuf
);
3170 DeleteFileA(tmpbuf
);
3171 return MSVCRT__strdup(tmpbuf
);
3173 TRACE("failed (%d)\n",GetLastError());
3177 /*********************************************************************
3178 * _wtempnam (MSVCRT.@)
3180 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3182 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3183 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3184 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3186 if (tmp_dir
) dir
= tmp_dir
;
3188 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3189 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3191 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3192 DeleteFileW(tmpbuf
);
3193 return MSVCRT__wcsdup(tmpbuf
);
3195 TRACE("failed (%d)\n",GetLastError());
3199 /*********************************************************************
3202 int CDECL
MSVCRT__umask(int umask
)
3204 int old_umask
= MSVCRT_umask
;
3205 TRACE("(%d)\n",umask
);
3206 MSVCRT_umask
= umask
;
3210 /*********************************************************************
3211 * _utime64 (MSVCRT.@)
3213 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3215 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3219 int retVal
= _futime64(fd
, t
);
3226 /*********************************************************************
3227 * _utime32 (MSVCRT.@)
3229 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3233 struct MSVCRT___utimbuf64 t64
;
3234 t64
.actime
= t
->actime
;
3235 t64
.modtime
= t
->modtime
;
3236 return _utime64( path
, &t64
);
3239 return _utime64( path
, NULL
);
3242 /*********************************************************************
3246 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3248 return _utime64( path
, t
);
3251 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3253 return _utime32( path
, t
);
3257 /*********************************************************************
3258 * _wutime64 (MSVCRT.@)
3260 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3262 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3266 int retVal
= _futime64(fd
, t
);
3273 /*********************************************************************
3274 * _wutime32 (MSVCRT.@)
3276 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3280 struct MSVCRT___utimbuf64 t64
;
3281 t64
.actime
= t
->actime
;
3282 t64
.modtime
= t
->modtime
;
3283 return _wutime64( path
, &t64
);
3286 return _wutime64( path
, NULL
);
3289 /*********************************************************************
3290 * _wutime (MSVCRT.@)
3293 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3295 return _wutime64( path
, t
);
3298 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3300 return _wutime32( path
, t
);
3304 /*********************************************************************
3307 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3310 ioinfo
*info
= get_ioinfo(fd
);
3311 HANDLE hand
= info
->handle
;
3313 /* Don't trace small writes, it gets *very* annoying */
3316 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3318 if (hand
== INVALID_HANDLE_VALUE
)
3320 *MSVCRT__errno() = MSVCRT_EBADF
;
3321 release_ioinfo(info
);
3325 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3327 *MSVCRT__errno() = MSVCRT_EINVAL
;
3328 release_ioinfo(info
);
3332 /* If appending, go to EOF */
3333 if (info
->wxflag
& WX_APPEND
)
3334 MSVCRT__lseek(fd
, 0, FILE_END
);
3336 if (!(info
->wxflag
& WX_TEXT
))
3338 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3339 && (num_written
== count
))
3341 release_ioinfo(info
);
3344 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3345 hand
, GetLastError());
3346 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3350 unsigned int i
, j
, nr_lf
, size
;
3353 const char *s
= buf
, *buf_start
= buf
;
3355 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3357 /* find number of \n */
3358 for (nr_lf
=0, i
=0; i
<count
; i
++)
3364 if ((q
= p
= MSVCRT_malloc(size
)))
3366 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3375 FIXME("Malloc failed\n");
3387 else if (info
->exflag
& EF_UTF16
)
3389 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3390 if (s
[i
]=='\n' && s
[i
+1]==0)
3395 if ((q
= p
= MSVCRT_malloc(size
)))
3397 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3399 if (s
[i
]=='\n' && s
[i
+1]==0)
3410 FIXME("Malloc failed\n");
3426 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3427 if (s
[i
]=='\n' && s
[i
+1]==0)
3430 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3432 msvcrt_set_errno(GetLastError());
3434 release_ioinfo(info
);
3438 size
= conv_len
+nr_lf
;
3439 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3441 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3443 if (s
[i
]=='\n' && s
[i
+1]==0)
3451 q
= p
+count
+nr_lf
*2;
3452 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3453 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3457 FIXME("Malloc failed\n");
3464 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3466 release_ioinfo(info
);
3469 if (num_written
!= size
)
3471 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3472 fd
, hand
, GetLastError(), num_written
);
3473 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3474 return s
- buf_start
;
3479 release_ioinfo(info
);
3483 /*********************************************************************
3486 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3490 MSVCRT__lock_file(file
);
3491 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3492 if (len
== sizeof(val
)) {
3493 MSVCRT__unlock_file(file
);
3497 file
->_flag
|= MSVCRT__IOERR
;
3498 MSVCRT__unlock_file(file
);
3502 /*********************************************************************
3505 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3509 MSVCRT__lock_file(file
);
3510 ret
= MSVCRT__fclose_nolock(file
);
3511 MSVCRT__unlock_file(file
);
3516 /*********************************************************************
3517 * _fclose_nolock (MSVCRT.@)
3519 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3524 MSVCRT_free(file
->_tmpfname
);
3525 file
->_tmpfname
= NULL
;
3526 /* flush stdio buffers */
3527 if(file
->_flag
& MSVCRT__IOWRT
)
3528 MSVCRT__fflush_nolock(file
);
3529 if(file
->_flag
& MSVCRT__IOMYBUF
)
3530 MSVCRT_free(file
->_base
);
3532 r
=MSVCRT__close(file
->_file
);
3535 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3538 /*********************************************************************
3541 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3543 return file
->_flag
& MSVCRT__IOEOF
;
3546 /*********************************************************************
3549 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3551 return file
->_flag
& MSVCRT__IOERR
;
3554 /*********************************************************************
3555 * _filbuf (MSVCRT.@)
3557 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3561 if(file
->_flag
& MSVCRT__IOSTRG
)
3564 /* Allocate buffer if needed */
3565 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3566 msvcrt_alloc_buffer(file
);
3568 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3569 if(file
->_flag
& MSVCRT__IORW
)
3570 file
->_flag
|= MSVCRT__IOREAD
;
3575 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3577 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3578 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3584 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3586 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3592 file
->_ptr
= file
->_base
+1;
3593 c
= *(unsigned char *)file
->_base
;
3598 /*********************************************************************
3601 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3605 MSVCRT__lock_file(file
);
3606 ret
= MSVCRT__fgetc_nolock(file
);
3607 MSVCRT__unlock_file(file
);
3612 /*********************************************************************
3613 * _fgetc_nolock (MSVCRT.@)
3615 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3622 i
= (unsigned char *)file
->_ptr
++;
3625 j
= MSVCRT__filbuf(file
);
3630 /*********************************************************************
3631 * _fgetchar (MSVCRT.@)
3633 int CDECL
MSVCRT__fgetchar(void)
3635 return MSVCRT_fgetc(MSVCRT_stdin
);
3638 /*********************************************************************
3641 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3643 int cc
= MSVCRT_EOF
;
3644 char * buf_start
= s
;
3646 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3647 file
,file
->_file
,s
,size
);
3649 MSVCRT__lock_file(file
);
3651 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3656 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3658 TRACE(":nothing read\n");
3659 MSVCRT__unlock_file(file
);
3662 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3665 TRACE(":got %s\n", debugstr_a(buf_start
));
3666 MSVCRT__unlock_file(file
);
3670 /*********************************************************************
3673 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3677 MSVCRT__lock_file(file
);
3678 ret
= MSVCRT__fgetwc_nolock(file
);
3679 MSVCRT__unlock_file(file
);
3684 /*********************************************************************
3685 * _fgetwc_nolock (MSVCRT.@)
3687 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3692 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3693 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3696 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3697 ch
= MSVCRT__fgetc_nolock(file
);
3698 if(ch
== MSVCRT_EOF
) {
3705 char mbs
[MSVCRT_MB_LEN_MAX
];
3708 ch
= MSVCRT__fgetc_nolock(file
);
3709 if(ch
!= MSVCRT_EOF
) {
3711 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3712 ch
= MSVCRT__fgetc_nolock(file
);
3713 if(ch
!= MSVCRT_EOF
) {
3722 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3729 /*********************************************************************
3732 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3739 MSVCRT__lock_file(file
);
3740 for (j
=0; j
<sizeof(int); j
++) {
3741 k
= MSVCRT__fgetc_nolock(file
);
3742 if (k
== MSVCRT_EOF
) {
3743 file
->_flag
|= MSVCRT__IOEOF
;
3744 MSVCRT__unlock_file(file
);
3750 MSVCRT__unlock_file(file
);
3754 /*********************************************************************
3757 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3759 return MSVCRT_fgetwc(file
);
3762 /*********************************************************************
3763 * _fgetwchar (MSVCRT.@)
3765 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3767 return MSVCRT_fgetwc(MSVCRT_stdin
);
3770 /*********************************************************************
3771 * getwchar (MSVCRT.@)
3773 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3775 return MSVCRT__fgetwchar();
3778 /*********************************************************************
3781 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3783 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3784 MSVCRT_wchar_t
* buf_start
= s
;
3786 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3787 file
,file
->_file
,s
,size
);
3789 MSVCRT__lock_file(file
);
3791 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3796 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3798 TRACE(":nothing read\n");
3799 MSVCRT__unlock_file(file
);
3802 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3805 TRACE(":got %s\n", debugstr_w(buf_start
));
3806 MSVCRT__unlock_file(file
);
3810 /*********************************************************************
3811 * _flsbuf (MSVCRT.@)
3813 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3815 /* Flush output buffer */
3816 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3817 msvcrt_alloc_buffer(file
);
3820 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3821 if(!(file
->_flag
& MSVCRT__IORW
)) {
3822 file
->_flag
|= MSVCRT__IOERR
;
3825 file
->_flag
|= MSVCRT__IOWRT
;
3827 if(file
->_flag
& MSVCRT__IOREAD
) {
3828 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3829 file
->_flag
|= MSVCRT__IOERR
;
3833 file
->_ptr
= file
->_base
;
3834 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3837 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3840 if(file
->_cnt
<= 0) {
3841 res
= msvcrt_flush_buffer(file
);
3844 file
->_flag
|= MSVCRT__IOWRT
;
3845 file
->_cnt
=file
->_bufsiz
;
3853 /* set _cnt to 0 for unbuffered FILEs */
3855 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3858 file
->_flag
|= MSVCRT__IOERR
;
3863 /*********************************************************************
3866 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3870 MSVCRT__lock_file(file
);
3871 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3872 MSVCRT__unlock_file(file
);
3877 /*********************************************************************
3878 * _fwrite_nolock (MSVCRT.@)
3880 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3882 MSVCRT_size_t wrcnt
=size
* nmemb
;
3888 if(file
->_cnt
< 0) {
3889 WARN("negative file->_cnt value in %p\n", file
);
3890 file
->_flag
|= MSVCRT__IOERR
;
3892 } else if(file
->_cnt
) {
3893 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3894 memcpy(file
->_ptr
, ptr
, pcnt
);
3899 ptr
= (const char*)ptr
+ pcnt
;
3900 } else if((file
->_flag
& MSVCRT__IONBF
)
3901 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3902 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3906 if(file
->_flag
& MSVCRT__IONBF
)
3908 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3909 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3911 bufsiz
= file
->_bufsiz
;
3913 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3915 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3918 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3919 file
->_flag
|= MSVCRT__IOERR
;
3924 ptr
= (const char*)ptr
+ pcnt
;
3926 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3930 ptr
= (const char*)ptr
+ 1;
3934 return written
/ size
;
3937 /*********************************************************************
3940 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3944 MSVCRT__lock_file(file
);
3945 ret
= MSVCRT__fputwc_nolock(wc
, file
);
3946 MSVCRT__unlock_file(file
);
3951 /*********************************************************************
3952 * _fputwc_nolock (MSVCRT.@)
3954 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3956 MSVCRT_wchar_t mwc
=wc
;
3960 fdinfo
= get_ioinfo_nolock(file
->_file
);
3962 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3963 char buf
[MSVCRT_MB_LEN_MAX
];
3966 char_len
= MSVCRT_wctomb(buf
, mwc
);
3967 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
3971 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3980 /*********************************************************************
3981 * _fputwchar (MSVCRT.@)
3983 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3985 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3988 /*********************************************************************
3989 * _wfsopen (MSVCRT.@)
3991 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3994 int open_flags
, stream_flags
, fd
;
3996 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3998 /* map mode string to open() flags. "man fopen" for possibilities. */
3999 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4003 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4006 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4008 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4015 TRACE(":got (%p)\n",file
);
4016 if (fd
>= 0 && !file
)
4022 /*********************************************************************
4023 * _fsopen (MSVCRT.@)
4025 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4028 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4030 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4031 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4032 *MSVCRT__errno() = MSVCRT_EINVAL
;
4035 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4038 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4039 *MSVCRT__errno() = MSVCRT_EINVAL
;
4043 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4050 /*********************************************************************
4053 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4055 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4058 /*********************************************************************
4059 * fopen_s (MSVCRT.@)
4061 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4062 const char *filename
, const char *mode
)
4064 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4065 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4066 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4068 *pFile
= MSVCRT_fopen(filename
, mode
);
4071 return *MSVCRT__errno();
4075 /*********************************************************************
4076 * _wfopen (MSVCRT.@)
4078 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4080 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4083 /*********************************************************************
4084 * _wfopen_s (MSVCRT.@)
4086 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4087 const MSVCRT_wchar_t
*mode
)
4089 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4090 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4091 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4093 *pFile
= MSVCRT__wfopen(filename
, mode
);
4096 return *MSVCRT__errno();
4100 /*********************************************************************
4103 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4107 MSVCRT__lock_file(file
);
4108 ret
= MSVCRT__fputc_nolock(c
, file
);
4109 MSVCRT__unlock_file(file
);
4114 /*********************************************************************
4115 * _fputc_nolock (MSVCRT.@)
4117 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4126 res
= msvcrt_flush_buffer(file
);
4127 return res
? res
: c
;
4133 res
= MSVCRT__flsbuf(c
, file
);
4138 /*********************************************************************
4139 * _fputchar (MSVCRT.@)
4141 int CDECL
MSVCRT__fputchar(int c
)
4143 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4146 /*********************************************************************
4149 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4153 MSVCRT__lock_file(file
);
4154 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4155 MSVCRT__unlock_file(file
);
4160 /*********************************************************************
4161 * _fread_nolock (MSVCRT.@)
4163 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4165 MSVCRT_size_t rcnt
=size
* nmemb
;
4166 MSVCRT_size_t read
=0;
4167 MSVCRT_size_t pread
=0;
4172 /* first buffered data */
4174 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4175 memcpy(ptr
, file
->_ptr
, pcnt
);
4180 ptr
= (char*)ptr
+ pcnt
;
4181 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4182 if(file
->_flag
& MSVCRT__IORW
) {
4183 file
->_flag
|= MSVCRT__IOREAD
;
4189 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4190 msvcrt_alloc_buffer(file
);
4195 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4196 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4197 file
->_ptr
= file
->_base
;
4198 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4199 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4200 if (i
> 0 && i
< file
->_cnt
) {
4201 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4202 file
->_flag
&= ~MSVCRT__IOEOF
;
4205 memcpy(ptr
, file
->_ptr
, i
);
4209 } else if (rcnt
> INT_MAX
) {
4210 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4211 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4212 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4214 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4218 ptr
= (char *)ptr
+i
;
4219 /* expose feof condition in the flags
4220 * MFC tests file->_flag for feof, and doesn't call feof())
4222 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4223 file
->_flag
|= MSVCRT__IOEOF
;
4226 file
->_flag
|= MSVCRT__IOERR
;
4237 /*********************************************************************
4238 * fread_s (MSVCR80.@)
4240 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4241 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4245 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4247 memset(buf
, 0, buf_size
);
4250 if(!elem_size
|| !count
) return 0;
4252 MSVCRT__lock_file(stream
);
4253 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4254 MSVCRT__unlock_file(stream
);
4259 /*********************************************************************
4260 * _fread_nolock_s (MSVCR80.@)
4262 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4263 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4265 size_t bytes_left
, buf_pos
;
4267 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4269 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4271 memset(buf
, 0, buf_size
);
4274 if(!elem_size
|| !count
) return 0;
4275 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4276 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4278 bytes_left
= elem_size
*count
;
4281 if(stream
->_cnt
> 0) {
4282 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4284 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4285 memset(buf
, 0, buf_size
);
4289 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4293 int c
= MSVCRT__filbuf(stream
);
4298 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4299 memset(buf
, 0, buf_size
);
4303 ((char*)buf
)[buf_pos
++] = c
;
4308 return buf_pos
/elem_size
;
4311 /*********************************************************************
4312 * _wfreopen (MSVCRT.@)
4315 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4317 int open_flags
, stream_flags
, fd
;
4319 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4322 if (!file
|| ((fd
= file
->_file
) < 0))
4326 MSVCRT_fclose(file
);
4327 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4329 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4331 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4341 /*********************************************************************
4342 * _wfreopen_s (MSVCRT.@)
4344 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4345 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4347 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4348 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4349 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4350 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4352 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4355 return *MSVCRT__errno();
4359 /*********************************************************************
4360 * freopen (MSVCRT.@)
4363 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4366 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4368 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4369 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4375 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4382 /*********************************************************************
4383 * freopen_s (MSVCRT.@)
4385 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4386 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4388 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4389 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4390 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4391 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4393 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4396 return *MSVCRT__errno();
4400 /*********************************************************************
4401 * fsetpos (MSVCRT.@)
4403 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4407 MSVCRT__lock_file(file
);
4408 msvcrt_flush_buffer(file
);
4410 /* Reset direction of i/o */
4411 if(file
->_flag
& MSVCRT__IORW
) {
4412 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4415 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4416 MSVCRT__unlock_file(file
);
4420 /*********************************************************************
4421 * _ftelli64 (MSVCRT.@)
4423 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4427 MSVCRT__lock_file(file
);
4428 ret
= MSVCRT__ftelli64_nolock(file
);
4429 MSVCRT__unlock_file(file
);
4434 /*********************************************************************
4435 * _ftelli64_nolock (MSVCRT.@)
4437 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4441 pos
= _telli64(file
->_file
);
4444 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4445 if(file
->_flag
& MSVCRT__IOWRT
) {
4446 pos
+= file
->_ptr
- file
->_base
;
4448 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4451 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4455 } else if(!file
->_cnt
) { /* nothing to do */
4456 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4460 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4461 for(i
=0; i
<file
->_cnt
; i
++)
4462 if(file
->_ptr
[i
] == '\n')
4468 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4471 pos
-= file
->_bufsiz
;
4472 pos
+= file
->_ptr
- file
->_base
;
4474 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4475 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4478 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4488 /*********************************************************************
4491 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4493 return MSVCRT__ftelli64(file
);
4496 /*********************************************************************
4497 * _ftell_nolock (MSVCRT.@)
4499 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4501 return MSVCRT__ftelli64_nolock(file
);
4504 /*********************************************************************
4505 * fgetpos (MSVCRT.@)
4507 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4509 *pos
= MSVCRT__ftelli64(file
);
4515 /*********************************************************************
4518 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4520 MSVCRT_size_t len
= strlen(s
);
4523 MSVCRT__lock_file(file
);
4524 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4525 MSVCRT__unlock_file(file
);
4529 /*********************************************************************
4532 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4534 MSVCRT_size_t i
, len
= strlenW(s
);
4538 MSVCRT__lock_file(file
);
4539 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4540 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4541 MSVCRT__unlock_file(file
);
4545 tmp_buf
= add_std_buffer(file
);
4546 for (i
=0; i
<len
; i
++) {
4547 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4548 if(tmp_buf
) remove_std_buffer(file
);
4549 MSVCRT__unlock_file(file
);
4554 if(tmp_buf
) remove_std_buffer(file
);
4555 MSVCRT__unlock_file(file
);
4559 /*********************************************************************
4560 * getchar (MSVCRT.@)
4562 int CDECL
MSVCRT_getchar(void)
4564 return MSVCRT_fgetc(MSVCRT_stdin
);
4567 /*********************************************************************
4570 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4572 return MSVCRT_fgetc(file
);
4575 /*********************************************************************
4578 char * CDECL
MSVCRT_gets(char *buf
)
4581 char * buf_start
= buf
;
4583 MSVCRT__lock_file(MSVCRT_stdin
);
4584 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4585 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4586 if(cc
!= '\r') *buf
++ = (char)cc
;
4590 TRACE("got '%s'\n", buf_start
);
4591 MSVCRT__unlock_file(MSVCRT_stdin
);
4595 /*********************************************************************
4598 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4601 MSVCRT_wchar_t
* ws
= buf
;
4603 MSVCRT__lock_file(MSVCRT_stdin
);
4604 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4605 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4608 *buf
++ = (MSVCRT_wchar_t
)cc
;
4612 TRACE("got %s\n", debugstr_w(ws
));
4613 MSVCRT__unlock_file(MSVCRT_stdin
);
4617 /*********************************************************************
4620 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4622 return MSVCRT_fputc(c
, file
);
4625 /*********************************************************************
4626 * putchar (MSVCRT.@)
4628 int CDECL
MSVCRT_putchar(int c
)
4630 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4633 /*********************************************************************
4636 int CDECL
MSVCRT_puts(const char *s
)
4638 MSVCRT_size_t len
= strlen(s
);
4641 MSVCRT__lock_file(MSVCRT_stdout
);
4642 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4643 MSVCRT__unlock_file(MSVCRT_stdout
);
4647 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4648 MSVCRT__unlock_file(MSVCRT_stdout
);
4652 /*********************************************************************
4655 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4657 static const MSVCRT_wchar_t nl
= '\n';
4658 MSVCRT_size_t len
= strlenW(s
);
4661 MSVCRT__lock_file(MSVCRT_stdout
);
4662 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4663 MSVCRT__unlock_file(MSVCRT_stdout
);
4667 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4668 MSVCRT__unlock_file(MSVCRT_stdout
);
4672 /*********************************************************************
4675 int CDECL
MSVCRT_remove(const char *path
)
4677 TRACE("(%s)\n",path
);
4678 if (DeleteFileA(path
))
4680 TRACE(":failed (%d)\n",GetLastError());
4681 msvcrt_set_errno(GetLastError());
4685 /*********************************************************************
4686 * _wremove (MSVCRT.@)
4688 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4690 TRACE("(%s)\n",debugstr_w(path
));
4691 if (DeleteFileW(path
))
4693 TRACE(":failed (%d)\n",GetLastError());
4694 msvcrt_set_errno(GetLastError());
4698 /*********************************************************************
4701 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4703 TRACE(":from %s to %s\n",oldpath
,newpath
);
4704 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4706 TRACE(":failed (%d)\n",GetLastError());
4707 msvcrt_set_errno(GetLastError());
4711 /*********************************************************************
4712 * _wrename (MSVCRT.@)
4714 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4716 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4717 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4719 TRACE(":failed (%d)\n",GetLastError());
4720 msvcrt_set_errno(GetLastError());
4724 /*********************************************************************
4725 * setvbuf (MSVCRT.@)
4727 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4729 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4730 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4731 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4733 MSVCRT__lock_file(file
);
4735 MSVCRT__fflush_nolock(file
);
4736 if(file
->_flag
& MSVCRT__IOMYBUF
)
4737 MSVCRT_free(file
->_base
);
4738 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4741 if(mode
== MSVCRT__IONBF
) {
4742 file
->_flag
|= MSVCRT__IONBF
;
4743 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4746 file
->_base
= file
->_ptr
= buf
;
4747 file
->_flag
|= MSVCRT__USERBUF
;
4748 file
->_bufsiz
= size
;
4750 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4753 MSVCRT__unlock_file(file
);
4757 file
->_flag
|= MSVCRT__IOMYBUF
;
4758 file
->_bufsiz
= size
;
4760 MSVCRT__unlock_file(file
);
4764 /*********************************************************************
4767 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4769 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4772 /*********************************************************************
4775 char * CDECL
MSVCRT_tmpnam(char *s
)
4782 thread_data_t
*data
= msvcrt_get_thread_data();
4784 if(!data
->tmpnam_buffer
)
4785 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4787 s
= data
->tmpnam_buffer
;
4790 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4791 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4792 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4794 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4795 memcpy(p
, tmpstr
, size
);
4797 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4798 GetLastError() == ERROR_FILE_NOT_FOUND
)
4804 /*********************************************************************
4805 * _wtmpnam (MSVCRT.@)
4807 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4809 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4810 MSVCRT_wchar_t tmpstr
[16];
4814 thread_data_t
*data
= msvcrt_get_thread_data();
4816 if(!data
->wtmpnam_buffer
)
4817 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4819 s
= data
->wtmpnam_buffer
;
4822 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4823 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4824 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4826 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4827 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4829 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4830 GetLastError() == ERROR_FILE_NOT_FOUND
)
4836 /*********************************************************************
4837 * tmpfile (MSVCRT.@)
4839 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4841 char *filename
= MSVCRT_tmpnam(NULL
);
4843 MSVCRT_FILE
* file
= NULL
;
4846 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4847 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4848 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4850 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4855 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4858 if(fd
!= -1 && !file
)
4864 /*********************************************************************
4865 * tmpfile_s (MSVCRT.@)
4867 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4869 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4871 *file
= MSVCRT_tmpfile();
4875 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4877 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4880 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4884 MSVCRT__lock_file(file
);
4886 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4887 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4888 MSVCRT__unlock_file(file
);
4892 for(i
=0; i
<len
; i
++) {
4893 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
4894 MSVCRT__unlock_file(file
);
4899 MSVCRT__unlock_file(file
);
4903 /*********************************************************************
4904 * vfprintf (MSVCRT.@)
4906 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4911 MSVCRT__lock_file(file
);
4912 tmp_buf
= add_std_buffer(file
);
4913 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4914 if(tmp_buf
) remove_std_buffer(file
);
4915 MSVCRT__unlock_file(file
);
4920 /*********************************************************************
4921 * vfprintf_s (MSVCRT.@)
4923 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4928 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4930 MSVCRT__lock_file(file
);
4931 tmp_buf
= add_std_buffer(file
);
4932 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4933 if(tmp_buf
) remove_std_buffer(file
);
4934 MSVCRT__unlock_file(file
);
4939 /*********************************************************************
4940 * vfwprintf (MSVCRT.@)
4942 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4947 MSVCRT__lock_file(file
);
4948 tmp_buf
= add_std_buffer(file
);
4949 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4950 if(tmp_buf
) remove_std_buffer(file
);
4951 MSVCRT__unlock_file(file
);
4956 /*********************************************************************
4957 * vfwprintf_s (MSVCRT.@)
4959 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4964 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4966 MSVCRT__lock_file(file
);
4967 tmp_buf
= add_std_buffer(file
);
4968 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4969 if(tmp_buf
) remove_std_buffer(file
);
4970 MSVCRT__unlock_file(file
);
4975 /*********************************************************************
4976 * _vfwprintf_l (MSVCRT.@)
4978 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4979 MSVCRT__locale_t locale
, __ms_va_list valist
)
4984 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4986 MSVCRT__lock_file(file
);
4987 tmp_buf
= add_std_buffer(file
);
4988 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4989 if(tmp_buf
) remove_std_buffer(file
);
4990 MSVCRT__unlock_file(file
);
4995 /*********************************************************************
4996 * vprintf (MSVCRT.@)
4998 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5000 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5003 /*********************************************************************
5004 * vprintf_s (MSVCRT.@)
5006 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5008 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5011 /*********************************************************************
5012 * vwprintf (MSVCRT.@)
5014 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5016 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5019 /*********************************************************************
5020 * vwprintf_s (MSVCRT.@)
5022 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5024 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5027 /*********************************************************************
5028 * fprintf (MSVCRT.@)
5030 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5032 __ms_va_list valist
;
5034 __ms_va_start(valist
, format
);
5035 res
= MSVCRT_vfprintf(file
, format
, valist
);
5036 __ms_va_end(valist
);
5040 /*********************************************************************
5041 * fprintf_s (MSVCRT.@)
5043 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5045 __ms_va_list valist
;
5047 __ms_va_start(valist
, format
);
5048 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5049 __ms_va_end(valist
);
5053 /*********************************************************************
5054 * fwprintf (MSVCRT.@)
5056 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5058 __ms_va_list valist
;
5060 __ms_va_start(valist
, format
);
5061 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5062 __ms_va_end(valist
);
5066 /*********************************************************************
5067 * fwprintf_s (MSVCRT.@)
5069 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5071 __ms_va_list valist
;
5073 __ms_va_start(valist
, format
);
5074 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5075 __ms_va_end(valist
);
5079 /*********************************************************************
5080 * _fwprintf_l (MSVCRT.@)
5082 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5084 __ms_va_list valist
;
5086 __ms_va_start(valist
, locale
);
5087 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5088 __ms_va_end(valist
);
5092 /*********************************************************************
5095 int CDECL
MSVCRT_printf(const char *format
, ...)
5097 __ms_va_list valist
;
5099 __ms_va_start(valist
, format
);
5100 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5101 __ms_va_end(valist
);
5105 /*********************************************************************
5106 * printf_s (MSVCRT.@)
5108 int CDECL
MSVCRT_printf_s(const char *format
, ...)
5110 __ms_va_list valist
;
5112 __ms_va_start(valist
, format
);
5113 res
= MSVCRT_vprintf_s(format
, valist
);
5114 __ms_va_end(valist
);
5118 /*********************************************************************
5121 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5125 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5127 MSVCRT__lock_file(file
);
5128 ret
= MSVCRT__ungetc_nolock(c
, file
);
5129 MSVCRT__unlock_file(file
);
5134 /*********************************************************************
5135 * _ungetc_nolock (MSVCRT.@)
5137 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5139 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5141 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5142 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5145 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5146 && msvcrt_alloc_buffer(file
))
5147 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5150 if(file
->_ptr
>file
->_base
) {
5152 if(file
->_flag
& MSVCRT__IOSTRG
) {
5153 if(*file
->_ptr
!= c
) {
5161 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5162 file
->_flag
|= MSVCRT__IOREAD
;
5169 /*********************************************************************
5170 * ungetwc (MSVCRT.@)
5172 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5176 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5178 MSVCRT__lock_file(file
);
5179 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5180 MSVCRT__unlock_file(file
);
5185 /*********************************************************************
5186 * _ungetwc_nolock (MSVCRT.@)
5188 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5190 MSVCRT_wchar_t mwc
= wc
;
5192 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5193 if (wc
== MSVCRT_WEOF
)
5196 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5197 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5198 unsigned char * pp
= (unsigned char *)&mwc
;
5201 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5202 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5206 char mbs
[MSVCRT_MB_LEN_MAX
];
5209 len
= MSVCRT_wctomb(mbs
, mwc
);
5213 for(len
--; len
>=0; len
--) {
5214 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5222 /*********************************************************************
5223 * wprintf (MSVCRT.@)
5225 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5227 __ms_va_list valist
;
5229 __ms_va_start(valist
, format
);
5230 res
= MSVCRT_vwprintf(format
, valist
);
5231 __ms_va_end(valist
);
5235 /*********************************************************************
5236 * wprintf_s (MSVCRT.@)
5238 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5240 __ms_va_list valist
;
5242 __ms_va_start(valist
, format
);
5243 res
= MSVCRT_vwprintf_s(format
, valist
);
5244 __ms_va_end(valist
);
5248 /*********************************************************************
5249 * _getmaxstdio (MSVCRT.@)
5251 int CDECL
MSVCRT__getmaxstdio(void)
5253 return MSVCRT_max_streams
;
5256 /*********************************************************************
5257 * _setmaxstdio (MSVCRT.@)
5259 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5261 TRACE("%d\n", newmax
);
5263 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5266 MSVCRT_max_streams
= newmax
;
5267 return MSVCRT_max_streams
;