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
]);
1202 /*********************************************************************
1203 * _lseeki64 (MSVCRT.@)
1205 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1207 ioinfo
*info
= get_ioinfo(fd
);
1210 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1212 if (info
->handle
== INVALID_HANDLE_VALUE
)
1214 release_ioinfo(info
);
1218 if (whence
< 0 || whence
> 2)
1220 release_ioinfo(info
);
1221 *MSVCRT__errno() = MSVCRT_EINVAL
;
1225 TRACE(":fd (%d) to %s pos %s\n",
1226 fd
,wine_dbgstr_longlong(offset
),
1227 (whence
==SEEK_SET
)?"SEEK_SET":
1228 (whence
==SEEK_CUR
)?"SEEK_CUR":
1229 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1231 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1232 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1233 ofs
.QuadPart
= offset
;
1234 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1235 GetLastError() == ERROR_SUCCESS
)
1237 info
->wxflag
&= ~WX_ATEOF
;
1238 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1240 release_ioinfo(info
);
1241 return ofs
.QuadPart
;
1243 release_ioinfo(info
);
1244 TRACE(":error-last error (%d)\n",GetLastError());
1245 msvcrt_set_errno(GetLastError());
1249 /*********************************************************************
1252 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1254 return MSVCRT__lseeki64(fd
, offset
, whence
);
1257 /*********************************************************************
1258 * _lock_file (MSVCRT.@)
1260 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1262 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1263 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1265 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1268 /*********************************************************************
1269 * _unlock_file (MSVCRT.@)
1271 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1273 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1274 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1276 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1279 /*********************************************************************
1280 * _locking (MSVCRT.@)
1282 * This is untested; the underlying LockFile doesn't work yet.
1284 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1286 ioinfo
*info
= get_ioinfo(fd
);
1290 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1291 if (info
->handle
== INVALID_HANDLE_VALUE
)
1293 release_ioinfo(info
);
1297 if (mode
< 0 || mode
> 4)
1299 release_ioinfo(info
);
1300 *MSVCRT__errno() = MSVCRT_EINVAL
;
1304 TRACE(":fd (%d) by 0x%08x mode %s\n",
1305 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1306 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1307 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1308 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1309 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1312 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1314 release_ioinfo(info
);
1315 FIXME ("Seek failed\n");
1316 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1319 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1322 ret
= 1; /* just to satisfy gcc */
1325 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1330 else if (mode
== MSVCRT__LK_UNLCK
)
1331 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1333 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1334 /* FIXME - what about error settings? */
1335 release_ioinfo(info
);
1336 return ret
? 0 : -1;
1339 /*********************************************************************
1340 * _fseeki64 (MSVCRT.@)
1342 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1346 MSVCRT__lock_file(file
);
1347 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1348 MSVCRT__unlock_file(file
);
1353 /*********************************************************************
1354 * _fseeki64_nolock (MSVCRT.@)
1356 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1360 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1362 offset
+= MSVCRT__ftelli64_nolock(file
);
1365 /* Flush output if needed */
1366 msvcrt_flush_buffer(file
);
1367 /* Reset direction of i/o */
1368 if(file
->_flag
& MSVCRT__IORW
) {
1369 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1371 /* Clear end of file flag */
1372 file
->_flag
&= ~MSVCRT__IOEOF
;
1373 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1378 /*********************************************************************
1381 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1383 return MSVCRT__fseeki64( file
, offset
, whence
);
1386 /*********************************************************************
1387 * _fseek_nolock (MSVCRT.@)
1389 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1391 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1394 /*********************************************************************
1395 * _chsize_s (MSVCRT.@)
1397 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1403 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1405 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1408 info
= get_ioinfo(fd
);
1409 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1411 /* save the current file pointer */
1412 cur
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1415 pos
= MSVCRT__lseeki64(fd
, size
, SEEK_SET
);
1418 ret
= SetEndOfFile(info
->handle
);
1419 if (!ret
) msvcrt_set_errno(GetLastError());
1422 /* restore the file pointer */
1423 MSVCRT__lseeki64(fd
, cur
, SEEK_SET
);
1427 release_ioinfo(info
);
1428 return ret
? 0 : *MSVCRT__errno();
1431 /*********************************************************************
1432 * _chsize (MSVCRT.@)
1434 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1436 /* _chsize_s returns errno on failure but _chsize should return -1 */
1437 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1440 /*********************************************************************
1441 * clearerr (MSVCRT.@)
1443 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1445 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1447 MSVCRT__lock_file(file
);
1448 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1449 MSVCRT__unlock_file(file
);
1452 /*********************************************************************
1455 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1457 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1459 MSVCRT__lock_file(file
);
1460 MSVCRT__fseek_nolock(file
, 0L, SEEK_SET
);
1461 MSVCRT_clearerr(file
);
1462 MSVCRT__unlock_file(file
);
1465 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1467 int plus
= strchrW(mode
, '+') != NULL
;
1469 TRACE("%s\n", debugstr_w(mode
));
1471 while(*mode
== ' ') mode
++;
1476 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1477 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1480 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1481 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1484 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1485 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1488 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1492 *stream_flags
|= MSVCRT__commode
;
1494 while (*mode
&& *mode
!=',')
1498 *open_flags
|= MSVCRT__O_BINARY
;
1499 *open_flags
&= ~MSVCRT__O_TEXT
;
1502 *open_flags
|= MSVCRT__O_TEXT
;
1503 *open_flags
&= ~MSVCRT__O_BINARY
;
1506 *open_flags
|= MSVCRT__O_TEMPORARY
;
1509 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1512 *stream_flags
|= MSVCRT__IOCOMMIT
;
1515 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1518 *open_flags
|= MSVCRT__O_NOINHERIT
;
1527 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1530 ERR("incorrect mode flag: %c\n", mode
[-1]);
1536 static const WCHAR ccs
[] = {'c','c','s'};
1537 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1538 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1539 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1542 while(*mode
== ' ') mode
++;
1543 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1545 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1546 while(*mode
== ' ') mode
++;
1547 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1550 while(*mode
== ' ') mode
++;
1552 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1554 *open_flags
|= MSVCRT__O_U8TEXT
;
1555 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1557 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1559 *open_flags
|= MSVCRT__O_U16TEXT
;
1560 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1562 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1564 *open_flags
|= MSVCRT__O_WTEXT
;
1565 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1569 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1573 while(*mode
== ' ') mode
++;
1576 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1581 /*********************************************************************
1582 * _fdopen (MSVCRT.@)
1584 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1587 MSVCRT_wchar_t
*modeW
= NULL
;
1589 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1591 ret
= MSVCRT__wfdopen(fd
, modeW
);
1597 /*********************************************************************
1598 * _wfdopen (MSVCRT.@)
1600 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1602 int open_flags
, stream_flags
;
1605 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1608 if (!(file
= msvcrt_alloc_fp()))
1610 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1615 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1621 /*********************************************************************
1622 * _filelength (MSVCRT.@)
1624 LONG CDECL
MSVCRT__filelength(int fd
)
1626 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1629 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1632 if (endPos
!= curPos
)
1633 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1640 /*********************************************************************
1641 * _filelengthi64 (MSVCRT.@)
1643 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1645 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1648 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1651 if (endPos
!= curPos
)
1652 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1659 /*********************************************************************
1660 * _fileno (MSVCRT.@)
1662 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1664 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1668 /*********************************************************************
1669 * _fstat64 (MSVCRT.@)
1671 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1673 ioinfo
*info
= get_ioinfo(fd
);
1676 BY_HANDLE_FILE_INFORMATION hfi
;
1678 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1679 if (info
->handle
== INVALID_HANDLE_VALUE
)
1681 release_ioinfo(info
);
1687 WARN(":failed-NULL buf\n");
1688 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1689 release_ioinfo(info
);
1693 memset(&hfi
, 0, sizeof(hfi
));
1694 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1695 type
= GetFileType(info
->handle
);
1696 if (type
== FILE_TYPE_PIPE
)
1698 buf
->st_dev
= buf
->st_rdev
= fd
;
1699 buf
->st_mode
= S_IFIFO
;
1702 else if (type
== FILE_TYPE_CHAR
)
1704 buf
->st_dev
= buf
->st_rdev
= fd
;
1705 buf
->st_mode
= S_IFCHR
;
1708 else /* FILE_TYPE_DISK etc. */
1710 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1712 WARN(":failed-last error (%d)\n",GetLastError());
1713 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1714 release_ioinfo(info
);
1717 buf
->st_mode
= S_IFREG
| 0444;
1718 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1719 buf
->st_mode
|= 0222;
1720 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1721 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1723 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1724 buf
->st_mtime
= buf
->st_ctime
= dw
;
1725 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1727 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1729 release_ioinfo(info
);
1733 /*********************************************************************
1734 * _fstati64 (MSVCRT.@)
1736 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1739 struct MSVCRT__stat64 buf64
;
1741 ret
= MSVCRT__fstat64(fd
, &buf64
);
1743 msvcrt_stat64_to_stati64(&buf64
, buf
);
1747 /*********************************************************************
1750 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1752 struct MSVCRT__stat64 buf64
;
1754 ret
= MSVCRT__fstat64(fd
, &buf64
);
1756 msvcrt_stat64_to_stat(&buf64
, buf
);
1760 /*********************************************************************
1761 * _fstat32 (MSVCR80.@)
1763 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1766 struct MSVCRT__stat64 buf64
;
1768 ret
= MSVCRT__fstat64(fd
, &buf64
);
1770 msvcrt_stat64_to_stat32(&buf64
, buf
);
1774 /*********************************************************************
1775 * _fstat64i32 (MSVCR80.@)
1777 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1780 struct MSVCRT__stat64 buf64
;
1782 ret
= MSVCRT__fstat64(fd
, &buf64
);
1784 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1788 /*********************************************************************
1789 * _futime64 (MSVCRT.@)
1791 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1793 ioinfo
*info
= get_ioinfo(fd
);
1798 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1803 time_to_filetime( t
->actime
, &at
);
1804 time_to_filetime( t
->modtime
, &wt
);
1807 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1809 release_ioinfo(info
);
1810 msvcrt_set_errno(GetLastError());
1813 release_ioinfo(info
);
1817 /*********************************************************************
1818 * _futime32 (MSVCRT.@)
1820 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1824 struct MSVCRT___utimbuf64 t64
;
1825 t64
.actime
= t
->actime
;
1826 t64
.modtime
= t
->modtime
;
1827 return _futime64( fd
, &t64
);
1830 return _futime64( fd
, NULL
);
1833 /*********************************************************************
1834 * _futime (MSVCRT.@)
1837 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1839 return _futime64( fd
, t
);
1842 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1844 return _futime32( fd
, t
);
1848 /*********************************************************************
1849 * _get_osfhandle (MSVCRT.@)
1851 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1853 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1854 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1856 if(hand
== INVALID_HANDLE_VALUE
)
1857 *MSVCRT__errno() = MSVCRT_EBADF
;
1858 return (MSVCRT_intptr_t
)hand
;
1861 /*********************************************************************
1862 * _mktemp_s (MSVCRT.@)
1864 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1868 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1869 return MSVCRT_EINVAL
;
1871 for(len
=0; len
<size
; len
++)
1874 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1877 return MSVCRT_EINVAL
;
1880 for(xno
=1; xno
<=6; xno
++)
1881 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1882 return MSVCRT_EINVAL
;
1884 id
= GetCurrentProcessId();
1885 for(xno
=1; xno
<6; xno
++) {
1886 pattern
[len
-xno
] = id
%10 + '0';
1890 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1891 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1896 *MSVCRT__errno() = MSVCRT_EEXIST
;
1897 return MSVCRT_EEXIST
;
1900 /*********************************************************************
1901 * _mktemp (MSVCRT.@)
1903 char * CDECL
MSVCRT__mktemp(char *pattern
)
1906 char *retVal
= pattern
;
1914 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1918 id
= GetCurrentProcessId();
1922 int tempNum
= id
/ 10;
1923 *pattern
-- = id
- (tempNum
* 10) + '0';
1929 *pattern
= letter
++;
1930 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1932 } while(letter
<= 'z');
1936 /*********************************************************************
1937 * _wmktemp_s (MSVCRT.@)
1939 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1943 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1944 return MSVCRT_EINVAL
;
1946 for(len
=0; len
<size
; len
++)
1949 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1952 return MSVCRT_EINVAL
;
1955 for(xno
=1; xno
<=6; xno
++)
1956 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1957 return MSVCRT_EINVAL
;
1959 id
= GetCurrentProcessId();
1960 for(xno
=1; xno
<6; xno
++) {
1961 pattern
[len
-xno
] = id
%10 + '0';
1965 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1966 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1971 *MSVCRT__errno() = MSVCRT_EEXIST
;
1972 return MSVCRT_EEXIST
;
1975 /*********************************************************************
1976 * _wmktemp (MSVCRT.@)
1978 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1981 MSVCRT_wchar_t
*retVal
= pattern
;
1983 MSVCRT_wchar_t letter
= 'a';
1989 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1993 id
= GetCurrentProcessId();
1997 int tempNum
= id
/ 10;
1998 *pattern
-- = id
- (tempNum
* 10) + '0';
2004 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2006 *pattern
= letter
++;
2007 } while(letter
!= '|');
2011 static unsigned split_oflags(unsigned oflags
)
2014 unsigned unsupp
; /* until we support everything */
2016 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2017 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2018 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2019 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2020 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2021 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2022 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2023 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2024 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2026 if ((unsupp
= oflags
& ~(
2027 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2028 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2029 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2030 MSVCRT__O_NOINHERIT
|
2031 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2032 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2034 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2039 /*********************************************************************
2042 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2045 SECURITY_ATTRIBUTES sa
;
2046 HANDLE readHandle
, writeHandle
;
2050 *MSVCRT__errno() = MSVCRT_EINVAL
;
2054 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2055 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2056 sa
.lpSecurityDescriptor
= NULL
;
2057 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2059 unsigned int wxflags
= split_oflags(textmode
);
2062 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2066 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2074 MSVCRT__close(pfds
[0]);
2075 CloseHandle(writeHandle
);
2076 *MSVCRT__errno() = MSVCRT_EMFILE
;
2081 CloseHandle(readHandle
);
2082 CloseHandle(writeHandle
);
2083 *MSVCRT__errno() = MSVCRT_EMFILE
;
2087 msvcrt_set_errno(GetLastError());
2092 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2094 char bom
[sizeof(utf8_bom
)];
2097 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2099 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2102 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2103 oflags
|= MSVCRT__O_U8TEXT
;
2104 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2106 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2107 oflags
|= MSVCRT__O_U16TEXT
;
2109 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2115 /*********************************************************************
2116 * _wsopen_s (MSVCRT.@)
2118 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2120 DWORD access
= 0, creation
= 0, attrib
;
2121 SECURITY_ATTRIBUTES sa
;
2122 DWORD sharing
, type
;
2126 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2127 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2129 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2132 wxflag
= split_oflags(oflags
);
2133 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2135 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2136 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2137 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2140 if (oflags
& MSVCRT__O_CREAT
)
2142 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2143 FIXME(": pmode 0x%04x ignored\n", pmode
);
2145 if (oflags
& MSVCRT__O_EXCL
)
2146 creation
= CREATE_NEW
;
2147 else if (oflags
& MSVCRT__O_TRUNC
)
2148 creation
= CREATE_ALWAYS
;
2150 creation
= OPEN_ALWAYS
;
2152 else /* no MSVCRT__O_CREAT */
2154 if (oflags
& MSVCRT__O_TRUNC
)
2155 creation
= TRUNCATE_EXISTING
;
2157 creation
= OPEN_EXISTING
;
2162 case MSVCRT__SH_DENYRW
:
2165 case MSVCRT__SH_DENYWR
:
2166 sharing
= FILE_SHARE_READ
;
2168 case MSVCRT__SH_DENYRD
:
2169 sharing
= FILE_SHARE_WRITE
;
2171 case MSVCRT__SH_DENYNO
:
2172 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2175 ERR( "Unhandled shflags 0x%x\n", shflags
);
2176 return MSVCRT_EINVAL
;
2179 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2180 attrib
= FILE_ATTRIBUTE_READONLY
;
2182 attrib
= FILE_ATTRIBUTE_NORMAL
;
2184 if (oflags
& MSVCRT__O_TEMPORARY
)
2186 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2188 sharing
|= FILE_SHARE_DELETE
;
2191 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2192 sa
.lpSecurityDescriptor
= NULL
;
2193 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2195 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2196 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2197 && !(access
&GENERIC_READ
))
2199 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2200 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2201 if (hand
!= INVALID_HANDLE_VALUE
)
2203 oflags
= check_bom(hand
, oflags
, FALSE
);
2207 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2210 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2211 if (hand
== INVALID_HANDLE_VALUE
) {
2212 WARN(":failed-last error (%d)\n",GetLastError());
2213 msvcrt_set_errno(GetLastError());
2214 return *MSVCRT__errno();
2217 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2219 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2220 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2221 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2223 if (oflags
& MSVCRT__O_U8TEXT
)
2225 DWORD written
= 0, tmp
;
2227 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2228 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2230 if (written
!= sizeof(utf8_bom
)) {
2231 WARN("error writing BOM\n");
2233 msvcrt_set_errno(GetLastError());
2234 return *MSVCRT__errno();
2239 DWORD written
= 0, tmp
;
2241 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2242 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2244 if (written
!= sizeof(utf16_bom
))
2246 WARN("error writing BOM\n");
2248 msvcrt_set_errno(GetLastError());
2249 return *MSVCRT__errno();
2253 else if (access
& GENERIC_READ
)
2254 oflags
= check_bom(hand
, oflags
, TRUE
);
2257 type
= GetFileType(hand
);
2258 if (type
== FILE_TYPE_CHAR
)
2260 else if (type
== FILE_TYPE_PIPE
)
2263 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2265 return *MSVCRT__errno();
2267 if (oflags
& MSVCRT__O_WTEXT
)
2268 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2269 else if (oflags
& MSVCRT__O_U16TEXT
)
2270 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2271 else if (oflags
& MSVCRT__O_U8TEXT
)
2272 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2274 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2278 /*********************************************************************
2279 * _wsopen (MSVCRT.@)
2281 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2286 if (oflags
& MSVCRT__O_CREAT
)
2290 __ms_va_start(ap
, shflags
);
2291 pmode
= va_arg(ap
, int);
2297 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2301 /*********************************************************************
2302 * _sopen_s (MSVCRT.@)
2304 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2306 MSVCRT_wchar_t
*pathW
;
2309 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2310 return MSVCRT_EINVAL
;
2312 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2313 return MSVCRT_EINVAL
;
2315 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2320 /*********************************************************************
2323 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2328 if (oflags
& MSVCRT__O_CREAT
)
2332 __ms_va_start(ap
, shflags
);
2333 pmode
= va_arg(ap
, int);
2339 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2343 /*********************************************************************
2346 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2350 if (flags
& MSVCRT__O_CREAT
)
2353 __ms_va_start(ap
, flags
);
2354 pmode
= va_arg(ap
, int);
2356 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2359 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2362 /*********************************************************************
2365 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2369 if (flags
& MSVCRT__O_CREAT
)
2372 __ms_va_start(ap
, flags
);
2373 pmode
= va_arg(ap
, int);
2375 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2378 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2381 /*********************************************************************
2384 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2386 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2387 return MSVCRT__open(path
, usedFlags
);
2390 /*********************************************************************
2391 * _wcreat (MSVCRT.@)
2393 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2395 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2396 return MSVCRT__wopen(path
, usedFlags
);
2399 /*********************************************************************
2400 * _open_osfhandle (MSVCRT.@)
2402 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2407 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2408 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2409 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2410 * text - it never sets MSVCRT__O_BINARY.
2412 /* don't let split_oflags() decide the mode if no mode is passed */
2413 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2414 oflags
|= MSVCRT__O_BINARY
;
2416 flags
= GetFileType((HANDLE
)handle
);
2417 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2419 msvcrt_set_errno(GetLastError());
2423 if (flags
== FILE_TYPE_CHAR
)
2425 else if (flags
== FILE_TYPE_PIPE
)
2429 flags
|= split_oflags(oflags
);
2431 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2432 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2436 /*********************************************************************
2439 int CDECL
MSVCRT__rmtmp(void)
2441 int num_removed
= 0, i
;
2445 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2446 file
= msvcrt_get_file(i
);
2448 if (file
->_tmpfname
)
2450 MSVCRT_fclose(file
);
2457 TRACE(":removed (%d) temp files\n",num_removed
);
2461 static inline int get_utf8_char_len(char ch
)
2463 if((ch
&0xf8) == 0xf0)
2465 else if((ch
&0xf0) == 0xe0)
2467 else if((ch
&0xe0) == 0xc0)
2472 /*********************************************************************
2473 * (internal) read_utf8
2475 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2477 HANDLE hand
= fdinfo
->handle
;
2478 char min_buf
[4], *readbuf
, lookahead
;
2479 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2481 /* make the buffer big enough to hold at least one character */
2482 /* read bytes have to fit to output and lookahead buffers */
2484 readbuf_size
= count
< 4 ? 4 : count
;
2485 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2490 if(fdinfo
->lookahead
[0] != '\n') {
2491 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2492 fdinfo
->lookahead
[0] = '\n';
2494 if(fdinfo
->lookahead
[1] != '\n') {
2495 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2496 fdinfo
->lookahead
[1] = '\n';
2498 if(fdinfo
->lookahead
[2] != '\n') {
2499 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2500 fdinfo
->lookahead
[2] = '\n';
2505 /* NOTE: this case is broken in native dll, reading
2506 * sometimes fails when small buffer is passed
2509 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2510 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2511 fdinfo
->wxflag
|= WX_ATEOF
;
2514 msvcrt_set_errno(GetLastError());
2517 }else if(!num_read
) {
2518 fdinfo
->wxflag
|= WX_ATEOF
;
2524 char_len
= get_utf8_char_len(readbuf
[0]);
2526 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2530 if(readbuf
[0] == '\n')
2531 fdinfo
->wxflag
|= WX_READNL
;
2533 fdinfo
->wxflag
&= ~WX_READNL
;
2535 if(readbuf
[0] == 0x1a) {
2536 fdinfo
->wxflag
|= WX_ATEOF
;
2540 if(readbuf
[0] == '\r') {
2541 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2543 else if(lookahead
== '\n')
2547 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2548 fdinfo
->lookahead
[0] = lookahead
;
2550 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2555 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2556 msvcrt_set_errno(GetLastError());
2563 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2566 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2567 fdinfo
->wxflag
|= WX_ATEOF
;
2568 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2571 msvcrt_set_errno(GetLastError());
2572 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2575 }else if(!pos
&& !num_read
) {
2576 fdinfo
->wxflag
|= WX_ATEOF
;
2577 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2582 if(readbuf
[0] == '\n')
2583 fdinfo
->wxflag
|= WX_READNL
;
2585 fdinfo
->wxflag
&= ~WX_READNL
;
2587 /* Find first byte of last character (may be incomplete) */
2588 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2589 if((readbuf
[i
]&0xc0) != 0x80)
2591 char_len
= get_utf8_char_len(readbuf
[i
]);
2592 if(char_len
+i
<= pos
)
2595 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2597 fdinfo
->lookahead
[0] = readbuf
[i
];
2599 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2601 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2603 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2607 for(i
=0, j
=0; i
<pos
; i
++) {
2608 if(readbuf
[i
] == 0x1a) {
2609 fdinfo
->wxflag
|= WX_ATEOF
;
2613 /* strip '\r' if followed by '\n' */
2614 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2615 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2616 readbuf
[j
++] = '\r';
2617 }else if(lookahead
== '\n' && j
==0) {
2618 readbuf
[j
++] = '\n';
2620 if(lookahead
!= '\n')
2621 readbuf
[j
++] = '\r';
2623 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2624 fdinfo
->lookahead
[0] = lookahead
;
2626 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2628 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2629 readbuf
[j
++] = readbuf
[i
];
2634 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2635 msvcrt_set_errno(GetLastError());
2636 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2640 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2644 /*********************************************************************
2647 * When reading \r as last character in text mode, read() positions
2648 * the file pointer on the \r character while getc() goes on to
2651 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2653 DWORD num_read
, utf16
;
2654 char *bufstart
= buf
;
2659 if (fdinfo
->wxflag
& WX_ATEOF
) {
2660 TRACE("already at EOF, returning 0\n");
2663 /* Don't trace small reads, it gets *very* annoying */
2665 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2666 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2668 *MSVCRT__errno() = MSVCRT_EBADF
;
2672 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2673 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2675 *MSVCRT__errno() = MSVCRT_EINVAL
;
2679 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2680 return read_utf8(fdinfo
, buf
, count
);
2682 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2684 if (fdinfo
->lookahead
[0] != '\n')
2686 bufstart
[0] = fdinfo
->lookahead
[0];
2687 fdinfo
->lookahead
[0] = '\n';
2691 bufstart
[1] = fdinfo
->lookahead
[1];
2692 fdinfo
->lookahead
[1] = '\n';
2695 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2696 num_read
+= 1+utf16
;
2701 if(utf16
&& (num_read
&1))
2703 /* msvcr90 uses uninitialized value from the buffer in this case */
2704 /* msvcrt ignores additional data */
2705 ERR("got odd number of bytes in UTF16 mode\n");
2709 if (count
!= 0 && num_read
== 0)
2711 fdinfo
->wxflag
|= WX_ATEOF
;
2712 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2714 else if (fdinfo
->wxflag
& WX_TEXT
)
2718 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2719 fdinfo
->wxflag
|= WX_READNL
;
2721 fdinfo
->wxflag
&= ~WX_READNL
;
2723 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2725 /* in text mode, a ctrl-z signals EOF */
2726 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2728 fdinfo
->wxflag
|= WX_ATEOF
;
2729 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2733 /* in text mode, strip \r if followed by \n */
2734 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2739 lookahead
[1] = '\n';
2740 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2742 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2744 bufstart
[j
++] = '\n';
2745 if(utf16
) bufstart
[j
++] = 0;
2749 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2751 bufstart
[j
++] = '\r';
2752 if(utf16
) bufstart
[j
++] = 0;
2755 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2757 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2759 bufstart
[j
++] = '\n';
2760 if (utf16
) bufstart
[j
++] = 0;
2764 fdinfo
->lookahead
[0] = lookahead
[0];
2765 fdinfo
->lookahead
[1] = lookahead
[1];
2769 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2774 bufstart
[j
++] = '\r';
2775 if(utf16
) bufstart
[j
++] = 0;
2778 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2779 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2781 bufstart
[j
++] = bufstart
[i
];
2782 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2790 if (GetLastError() == ERROR_BROKEN_PIPE
)
2792 TRACE(":end-of-pipe\n");
2793 fdinfo
->wxflag
|= WX_ATEOF
;
2798 TRACE(":failed-last error (%d)\n",GetLastError());
2804 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2808 /*********************************************************************
2811 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2813 ioinfo
*info
= get_ioinfo(fd
);
2814 int num_read
= read_i(fd
, info
, buf
, count
);
2815 release_ioinfo(info
);
2819 /*********************************************************************
2820 * _setmode (MSVCRT.@)
2822 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2824 ioinfo
*info
= get_ioinfo(fd
);
2825 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2826 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2827 ret
= MSVCRT__O_WTEXT
;
2829 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2830 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2831 *MSVCRT__errno() = MSVCRT_EINVAL
;
2832 release_ioinfo(info
);
2836 if(info
== &MSVCRT___badioinfo
) {
2837 *MSVCRT__errno() = MSVCRT_EBADF
;
2841 if(mode
== MSVCRT__O_BINARY
) {
2842 info
->wxflag
&= ~WX_TEXT
;
2843 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2844 release_ioinfo(info
);
2848 info
->wxflag
|= WX_TEXT
;
2849 if(mode
== MSVCRT__O_TEXT
)
2850 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2851 else if(mode
== MSVCRT__O_U8TEXT
)
2852 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2854 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2856 release_ioinfo(info
);
2860 /*********************************************************************
2861 * _stat64 (MSVCRT.@)
2863 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2866 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2867 unsigned short mode
= ALL_S_IREAD
;
2870 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2872 plen
= strlen(path
);
2873 while (plen
&& path
[plen
-1]==' ')
2876 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2877 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2879 *MSVCRT__errno() = MSVCRT_ENOENT
;
2883 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2885 TRACE("failed (%d)\n",GetLastError());
2886 *MSVCRT__errno() = MSVCRT_ENOENT
;
2890 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2892 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2893 Bon 011120: This FIXME seems incorrect
2894 Also a letter as first char isn't enough to be classified
2897 if (isalpha(*path
)&& (*(path
+1)==':'))
2898 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2900 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2902 /* Dir, or regular file? */
2903 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2904 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2907 mode
|= MSVCRT__S_IFREG
;
2909 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2911 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2912 (tolower(path
[plen
-3]) << 16);
2913 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2914 mode
|= ALL_S_IEXEC
;
2918 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2919 mode
|= ALL_S_IWRITE
;
2921 buf
->st_mode
= mode
;
2923 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2924 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2926 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2927 buf
->st_mtime
= buf
->st_ctime
= dw
;
2928 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2929 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2930 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2934 /*********************************************************************
2935 * _stati64 (MSVCRT.@)
2937 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2940 struct MSVCRT__stat64 buf64
;
2942 ret
= MSVCRT_stat64(path
, &buf64
);
2944 msvcrt_stat64_to_stati64(&buf64
, buf
);
2948 /*********************************************************************
2951 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2954 struct MSVCRT__stat64 buf64
;
2956 ret
= MSVCRT_stat64( path
, &buf64
);
2958 msvcrt_stat64_to_stat(&buf64
, buf
);
2962 /*********************************************************************
2963 * _stat32 (MSVCR100.@)
2965 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2968 struct MSVCRT__stat64 buf64
;
2970 ret
= MSVCRT_stat64(path
, &buf64
);
2972 msvcrt_stat64_to_stat32(&buf64
, buf
);
2976 /*********************************************************************
2977 * _stat32i64 (MSVCR100.@)
2979 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2982 struct MSVCRT__stat64 buf64
;
2984 ret
= MSVCRT_stat64(path
, &buf64
);
2986 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2990 /*********************************************************************
2991 * _stat64i32 (MSVCR100.@)
2993 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
2996 struct MSVCRT__stat64 buf64
;
2998 ret
= MSVCRT_stat64(path
, &buf64
);
3000 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3004 /*********************************************************************
3005 * _wstat64 (MSVCRT.@)
3007 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
3010 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3011 unsigned short mode
= ALL_S_IREAD
;
3014 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3016 plen
= strlenW(path
);
3017 while (plen
&& path
[plen
-1]==' ')
3020 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
3021 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3023 *MSVCRT__errno() = MSVCRT_ENOENT
;
3027 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3029 TRACE("failed (%d)\n",GetLastError());
3030 *MSVCRT__errno() = MSVCRT_ENOENT
;
3034 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3036 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3037 if (MSVCRT_iswalpha(*path
))
3038 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3040 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3042 /* Dir, or regular file? */
3043 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3044 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3047 mode
|= MSVCRT__S_IFREG
;
3049 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3051 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3052 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3053 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3054 mode
|= ALL_S_IEXEC
;
3058 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3059 mode
|= ALL_S_IWRITE
;
3061 buf
->st_mode
= mode
;
3063 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3064 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3066 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3067 buf
->st_mtime
= buf
->st_ctime
= dw
;
3068 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3069 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3070 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3074 /*********************************************************************
3075 * _wstati64 (MSVCRT.@)
3077 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3080 struct MSVCRT__stat64 buf64
;
3082 ret
= MSVCRT__wstat64(path
, &buf64
);
3084 msvcrt_stat64_to_stati64(&buf64
, buf
);
3088 /*********************************************************************
3091 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3094 struct MSVCRT__stat64 buf64
;
3096 ret
= MSVCRT__wstat64( path
, &buf64
);
3097 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3101 /*********************************************************************
3102 * _wstat32 (MSVCR100.@)
3104 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3107 struct MSVCRT__stat64 buf64
;
3109 ret
= MSVCRT__wstat64(path
, &buf64
);
3111 msvcrt_stat64_to_stat32(&buf64
, buf
);
3115 /*********************************************************************
3116 * _wstat32i64 (MSVCR100.@)
3118 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3121 struct MSVCRT__stat64 buf64
;
3123 ret
= MSVCRT__wstat64(path
, &buf64
);
3125 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3129 /*********************************************************************
3130 * _wstat64i32 (MSVCR100.@)
3132 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3135 struct MSVCRT__stat64 buf64
;
3137 ret
= MSVCRT__wstat64(path
, &buf64
);
3139 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3143 /*********************************************************************
3146 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3148 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
3151 /*********************************************************************
3152 * _telli64 (MSVCRT.@)
3154 __int64 CDECL
_telli64(int fd
)
3156 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
3159 /*********************************************************************
3160 * _tempnam (MSVCRT.@)
3162 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3164 char tmpbuf
[MAX_PATH
];
3165 const char *tmp_dir
= MSVCRT_getenv("TMP");
3167 if (tmp_dir
) dir
= tmp_dir
;
3169 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3170 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3172 TRACE("got name (%s)\n",tmpbuf
);
3173 DeleteFileA(tmpbuf
);
3174 return MSVCRT__strdup(tmpbuf
);
3176 TRACE("failed (%d)\n",GetLastError());
3180 /*********************************************************************
3181 * _wtempnam (MSVCRT.@)
3183 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3185 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3186 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3187 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3189 if (tmp_dir
) dir
= tmp_dir
;
3191 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3192 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3194 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3195 DeleteFileW(tmpbuf
);
3196 return MSVCRT__wcsdup(tmpbuf
);
3198 TRACE("failed (%d)\n",GetLastError());
3202 /*********************************************************************
3205 int CDECL
MSVCRT__umask(int umask
)
3207 int old_umask
= MSVCRT_umask
;
3208 TRACE("(%d)\n",umask
);
3209 MSVCRT_umask
= umask
;
3213 /*********************************************************************
3214 * _utime64 (MSVCRT.@)
3216 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3218 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3222 int retVal
= _futime64(fd
, t
);
3229 /*********************************************************************
3230 * _utime32 (MSVCRT.@)
3232 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3236 struct MSVCRT___utimbuf64 t64
;
3237 t64
.actime
= t
->actime
;
3238 t64
.modtime
= t
->modtime
;
3239 return _utime64( path
, &t64
);
3242 return _utime64( path
, NULL
);
3245 /*********************************************************************
3249 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3251 return _utime64( path
, t
);
3254 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3256 return _utime32( path
, t
);
3260 /*********************************************************************
3261 * _wutime64 (MSVCRT.@)
3263 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3265 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3269 int retVal
= _futime64(fd
, t
);
3276 /*********************************************************************
3277 * _wutime32 (MSVCRT.@)
3279 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3283 struct MSVCRT___utimbuf64 t64
;
3284 t64
.actime
= t
->actime
;
3285 t64
.modtime
= t
->modtime
;
3286 return _wutime64( path
, &t64
);
3289 return _wutime64( path
, NULL
);
3292 /*********************************************************************
3293 * _wutime (MSVCRT.@)
3296 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3298 return _wutime64( path
, t
);
3301 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3303 return _wutime32( path
, t
);
3307 /*********************************************************************
3310 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3313 ioinfo
*info
= get_ioinfo(fd
);
3314 HANDLE hand
= info
->handle
;
3316 /* Don't trace small writes, it gets *very* annoying */
3319 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3321 if (hand
== INVALID_HANDLE_VALUE
)
3323 *MSVCRT__errno() = MSVCRT_EBADF
;
3324 release_ioinfo(info
);
3328 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3330 *MSVCRT__errno() = MSVCRT_EINVAL
;
3331 release_ioinfo(info
);
3335 /* If appending, go to EOF */
3336 if (info
->wxflag
& WX_APPEND
)
3337 MSVCRT__lseek(fd
, 0, FILE_END
);
3339 if (!(info
->wxflag
& WX_TEXT
))
3341 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3342 && (num_written
== count
))
3344 release_ioinfo(info
);
3347 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3348 hand
, GetLastError());
3349 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3353 unsigned int i
, j
, nr_lf
, size
;
3356 const char *s
= buf
, *buf_start
= buf
;
3358 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3360 /* find number of \n */
3361 for (nr_lf
=0, i
=0; i
<count
; i
++)
3367 if ((q
= p
= MSVCRT_malloc(size
)))
3369 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3378 FIXME("Malloc failed\n");
3390 else if (info
->exflag
& EF_UTF16
)
3392 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3393 if (s
[i
]=='\n' && s
[i
+1]==0)
3398 if ((q
= p
= MSVCRT_malloc(size
)))
3400 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3402 if (s
[i
]=='\n' && s
[i
+1]==0)
3413 FIXME("Malloc failed\n");
3429 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3430 if (s
[i
]=='\n' && s
[i
+1]==0)
3433 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3435 msvcrt_set_errno(GetLastError());
3437 release_ioinfo(info
);
3441 size
= conv_len
+nr_lf
;
3442 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3444 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3446 if (s
[i
]=='\n' && s
[i
+1]==0)
3454 q
= p
+count
+nr_lf
*2;
3455 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3456 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3460 FIXME("Malloc failed\n");
3467 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3469 release_ioinfo(info
);
3472 if (num_written
!= size
)
3474 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3475 fd
, hand
, GetLastError(), num_written
);
3476 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3477 return s
- buf_start
;
3482 release_ioinfo(info
);
3486 /*********************************************************************
3489 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3493 MSVCRT__lock_file(file
);
3494 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3495 if (len
== sizeof(val
)) {
3496 MSVCRT__unlock_file(file
);
3500 file
->_flag
|= MSVCRT__IOERR
;
3501 MSVCRT__unlock_file(file
);
3505 /*********************************************************************
3508 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3512 MSVCRT__lock_file(file
);
3513 ret
= MSVCRT__fclose_nolock(file
);
3514 MSVCRT__unlock_file(file
);
3519 /*********************************************************************
3520 * _fclose_nolock (MSVCRT.@)
3522 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3527 MSVCRT_free(file
->_tmpfname
);
3528 file
->_tmpfname
= NULL
;
3529 /* flush stdio buffers */
3530 if(file
->_flag
& MSVCRT__IOWRT
)
3531 MSVCRT__fflush_nolock(file
);
3532 if(file
->_flag
& MSVCRT__IOMYBUF
)
3533 MSVCRT_free(file
->_base
);
3535 r
=MSVCRT__close(file
->_file
);
3538 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3541 /*********************************************************************
3544 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3546 return file
->_flag
& MSVCRT__IOEOF
;
3549 /*********************************************************************
3552 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3554 return file
->_flag
& MSVCRT__IOERR
;
3557 /*********************************************************************
3558 * _filbuf (MSVCRT.@)
3560 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3564 if(file
->_flag
& MSVCRT__IOSTRG
)
3567 /* Allocate buffer if needed */
3568 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3569 msvcrt_alloc_buffer(file
);
3571 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3572 if(file
->_flag
& MSVCRT__IORW
)
3573 file
->_flag
|= MSVCRT__IOREAD
;
3578 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3580 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3581 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3587 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3589 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3595 file
->_ptr
= file
->_base
+1;
3596 c
= *(unsigned char *)file
->_base
;
3601 /*********************************************************************
3604 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3608 MSVCRT__lock_file(file
);
3609 ret
= MSVCRT__fgetc_nolock(file
);
3610 MSVCRT__unlock_file(file
);
3615 /*********************************************************************
3616 * _fgetc_nolock (MSVCRT.@)
3618 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3625 i
= (unsigned char *)file
->_ptr
++;
3628 j
= MSVCRT__filbuf(file
);
3633 /*********************************************************************
3634 * _fgetchar (MSVCRT.@)
3636 int CDECL
MSVCRT__fgetchar(void)
3638 return MSVCRT_fgetc(MSVCRT_stdin
);
3641 /*********************************************************************
3644 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3646 int cc
= MSVCRT_EOF
;
3647 char * buf_start
= s
;
3649 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3650 file
,file
->_file
,s
,size
);
3652 MSVCRT__lock_file(file
);
3654 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3659 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3661 TRACE(":nothing read\n");
3662 MSVCRT__unlock_file(file
);
3665 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3668 TRACE(":got %s\n", debugstr_a(buf_start
));
3669 MSVCRT__unlock_file(file
);
3673 /*********************************************************************
3676 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3680 MSVCRT__lock_file(file
);
3681 ret
= MSVCRT__fgetwc_nolock(file
);
3682 MSVCRT__unlock_file(file
);
3687 /*********************************************************************
3688 * _fgetwc_nolock (MSVCRT.@)
3690 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3695 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3696 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3699 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3700 ch
= MSVCRT__fgetc_nolock(file
);
3701 if(ch
== MSVCRT_EOF
) {
3708 char mbs
[MSVCRT_MB_LEN_MAX
];
3711 ch
= MSVCRT__fgetc_nolock(file
);
3712 if(ch
!= MSVCRT_EOF
) {
3714 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3715 ch
= MSVCRT__fgetc_nolock(file
);
3716 if(ch
!= MSVCRT_EOF
) {
3725 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3732 /*********************************************************************
3735 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3742 MSVCRT__lock_file(file
);
3743 for (j
=0; j
<sizeof(int); j
++) {
3744 k
= MSVCRT__fgetc_nolock(file
);
3745 if (k
== MSVCRT_EOF
) {
3746 file
->_flag
|= MSVCRT__IOEOF
;
3747 MSVCRT__unlock_file(file
);
3753 MSVCRT__unlock_file(file
);
3757 /*********************************************************************
3760 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3762 return MSVCRT_fgetwc(file
);
3765 /*********************************************************************
3766 * _fgetwchar (MSVCRT.@)
3768 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3770 return MSVCRT_fgetwc(MSVCRT_stdin
);
3773 /*********************************************************************
3774 * getwchar (MSVCRT.@)
3776 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3778 return MSVCRT__fgetwchar();
3781 /*********************************************************************
3784 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3786 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3787 MSVCRT_wchar_t
* buf_start
= s
;
3789 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3790 file
,file
->_file
,s
,size
);
3792 MSVCRT__lock_file(file
);
3794 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3799 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3801 TRACE(":nothing read\n");
3802 MSVCRT__unlock_file(file
);
3805 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3808 TRACE(":got %s\n", debugstr_w(buf_start
));
3809 MSVCRT__unlock_file(file
);
3813 /*********************************************************************
3814 * _flsbuf (MSVCRT.@)
3816 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3818 /* Flush output buffer */
3819 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3820 msvcrt_alloc_buffer(file
);
3823 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3824 if(!(file
->_flag
& MSVCRT__IORW
)) {
3825 file
->_flag
|= MSVCRT__IOERR
;
3828 file
->_flag
|= MSVCRT__IOWRT
;
3830 if(file
->_flag
& MSVCRT__IOREAD
) {
3831 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3832 file
->_flag
|= MSVCRT__IOERR
;
3836 file
->_ptr
= file
->_base
;
3837 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3840 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3843 if(file
->_cnt
<= 0) {
3844 res
= msvcrt_flush_buffer(file
);
3847 file
->_flag
|= MSVCRT__IOWRT
;
3848 file
->_cnt
=file
->_bufsiz
;
3856 /* set _cnt to 0 for unbuffered FILEs */
3858 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3861 file
->_flag
|= MSVCRT__IOERR
;
3866 /*********************************************************************
3869 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3873 MSVCRT__lock_file(file
);
3874 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3875 MSVCRT__unlock_file(file
);
3880 /*********************************************************************
3881 * _fwrite_nolock (MSVCRT.@)
3883 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3885 MSVCRT_size_t wrcnt
=size
* nmemb
;
3891 if(file
->_cnt
< 0) {
3892 WARN("negative file->_cnt value in %p\n", file
);
3893 file
->_flag
|= MSVCRT__IOERR
;
3895 } else if(file
->_cnt
) {
3896 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3897 memcpy(file
->_ptr
, ptr
, pcnt
);
3902 ptr
= (const char*)ptr
+ pcnt
;
3903 } else if((file
->_flag
& MSVCRT__IONBF
)
3904 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3905 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3909 if(file
->_flag
& MSVCRT__IONBF
)
3911 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3912 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3914 bufsiz
= file
->_bufsiz
;
3916 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3918 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3921 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3922 file
->_flag
|= MSVCRT__IOERR
;
3927 ptr
= (const char*)ptr
+ pcnt
;
3929 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3933 ptr
= (const char*)ptr
+ 1;
3937 return written
/ size
;
3940 /*********************************************************************
3943 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3947 MSVCRT__lock_file(file
);
3948 ret
= MSVCRT__fputwc_nolock(wc
, file
);
3949 MSVCRT__unlock_file(file
);
3954 /*********************************************************************
3955 * _fputwc_nolock (MSVCRT.@)
3957 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3959 MSVCRT_wchar_t mwc
=wc
;
3963 fdinfo
= get_ioinfo_nolock(file
->_file
);
3965 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3966 char buf
[MSVCRT_MB_LEN_MAX
];
3969 char_len
= MSVCRT_wctomb(buf
, mwc
);
3970 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
3974 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3983 /*********************************************************************
3984 * _fputwchar (MSVCRT.@)
3986 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3988 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3991 /*********************************************************************
3992 * _wfsopen (MSVCRT.@)
3994 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3997 int open_flags
, stream_flags
, fd
;
3999 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4001 /* map mode string to open() flags. "man fopen" for possibilities. */
4002 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4006 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4009 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4011 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4018 TRACE(":got (%p)\n",file
);
4019 if (fd
>= 0 && !file
)
4025 /*********************************************************************
4026 * _fsopen (MSVCRT.@)
4028 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4031 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4033 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4034 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4035 *MSVCRT__errno() = MSVCRT_EINVAL
;
4038 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4041 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4042 *MSVCRT__errno() = MSVCRT_EINVAL
;
4046 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4053 /*********************************************************************
4056 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4058 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4061 /*********************************************************************
4062 * fopen_s (MSVCRT.@)
4064 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4065 const char *filename
, const char *mode
)
4067 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4068 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4069 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4071 *pFile
= MSVCRT_fopen(filename
, mode
);
4074 return *MSVCRT__errno();
4078 /*********************************************************************
4079 * _wfopen (MSVCRT.@)
4081 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4083 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4086 /*********************************************************************
4087 * _wfopen_s (MSVCRT.@)
4089 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4090 const MSVCRT_wchar_t
*mode
)
4092 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4093 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4094 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4096 *pFile
= MSVCRT__wfopen(filename
, mode
);
4099 return *MSVCRT__errno();
4103 /*********************************************************************
4106 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4110 MSVCRT__lock_file(file
);
4111 ret
= MSVCRT__fputc_nolock(c
, file
);
4112 MSVCRT__unlock_file(file
);
4117 /*********************************************************************
4118 * _fputc_nolock (MSVCRT.@)
4120 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4129 res
= msvcrt_flush_buffer(file
);
4130 return res
? res
: c
;
4136 res
= MSVCRT__flsbuf(c
, file
);
4141 /*********************************************************************
4142 * _fputchar (MSVCRT.@)
4144 int CDECL
MSVCRT__fputchar(int c
)
4146 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4149 /*********************************************************************
4152 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4156 MSVCRT__lock_file(file
);
4157 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4158 MSVCRT__unlock_file(file
);
4163 /*********************************************************************
4164 * _fread_nolock (MSVCRT.@)
4166 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4168 MSVCRT_size_t rcnt
=size
* nmemb
;
4169 MSVCRT_size_t read
=0;
4170 MSVCRT_size_t pread
=0;
4175 /* first buffered data */
4177 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4178 memcpy(ptr
, file
->_ptr
, pcnt
);
4183 ptr
= (char*)ptr
+ pcnt
;
4184 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4185 if(file
->_flag
& MSVCRT__IORW
) {
4186 file
->_flag
|= MSVCRT__IOREAD
;
4192 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4193 msvcrt_alloc_buffer(file
);
4198 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4199 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4200 file
->_ptr
= file
->_base
;
4201 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4202 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4203 if (i
> 0 && i
< file
->_cnt
) {
4204 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4205 file
->_flag
&= ~MSVCRT__IOEOF
;
4208 memcpy(ptr
, file
->_ptr
, i
);
4212 } else if (rcnt
> INT_MAX
) {
4213 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4214 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4215 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4217 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4221 ptr
= (char *)ptr
+i
;
4222 /* expose feof condition in the flags
4223 * MFC tests file->_flag for feof, and doesn't call feof())
4225 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4226 file
->_flag
|= MSVCRT__IOEOF
;
4229 file
->_flag
|= MSVCRT__IOERR
;
4240 /*********************************************************************
4241 * fread_s (MSVCR80.@)
4243 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4244 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4248 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4250 memset(buf
, 0, buf_size
);
4253 if(!elem_size
|| !count
) return 0;
4255 MSVCRT__lock_file(stream
);
4256 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4257 MSVCRT__unlock_file(stream
);
4262 /*********************************************************************
4263 * _fread_nolock_s (MSVCR80.@)
4265 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4266 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4268 size_t bytes_left
, buf_pos
;
4270 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4272 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4274 memset(buf
, 0, buf_size
);
4277 if(!elem_size
|| !count
) return 0;
4278 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4279 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4281 bytes_left
= elem_size
*count
;
4284 if(stream
->_cnt
> 0) {
4285 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4287 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4288 memset(buf
, 0, buf_size
);
4292 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4296 int c
= MSVCRT__filbuf(stream
);
4301 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4302 memset(buf
, 0, buf_size
);
4306 ((char*)buf
)[buf_pos
++] = c
;
4311 return buf_pos
/elem_size
;
4314 /*********************************************************************
4315 * _wfreopen (MSVCRT.@)
4318 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4320 int open_flags
, stream_flags
, fd
;
4322 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4325 if (!file
|| ((fd
= file
->_file
) < 0))
4329 MSVCRT_fclose(file
);
4330 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4332 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4334 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4344 /*********************************************************************
4345 * _wfreopen_s (MSVCRT.@)
4347 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4348 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4350 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4351 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4352 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4353 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4355 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4358 return *MSVCRT__errno();
4362 /*********************************************************************
4363 * freopen (MSVCRT.@)
4366 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4369 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4371 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4372 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4378 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4385 /*********************************************************************
4386 * freopen_s (MSVCRT.@)
4388 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4389 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4391 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4392 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4393 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4394 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4396 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4399 return *MSVCRT__errno();
4403 /*********************************************************************
4404 * fsetpos (MSVCRT.@)
4406 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4410 MSVCRT__lock_file(file
);
4411 msvcrt_flush_buffer(file
);
4413 /* Reset direction of i/o */
4414 if(file
->_flag
& MSVCRT__IORW
) {
4415 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4418 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
4419 MSVCRT__unlock_file(file
);
4423 /*********************************************************************
4424 * _ftelli64 (MSVCRT.@)
4426 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4430 MSVCRT__lock_file(file
);
4431 ret
= MSVCRT__ftelli64_nolock(file
);
4432 MSVCRT__unlock_file(file
);
4437 /*********************************************************************
4438 * _ftelli64_nolock (MSVCRT.@)
4440 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4444 pos
= _telli64(file
->_file
);
4447 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4448 if(file
->_flag
& MSVCRT__IOWRT
) {
4449 pos
+= file
->_ptr
- file
->_base
;
4451 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4454 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4458 } else if(!file
->_cnt
) { /* nothing to do */
4459 } else if(MSVCRT__lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
4463 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4464 for(i
=0; i
<file
->_cnt
; i
++)
4465 if(file
->_ptr
[i
] == '\n')
4471 if(MSVCRT__lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
)
4474 pos
-= file
->_bufsiz
;
4475 pos
+= file
->_ptr
- file
->_base
;
4477 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4478 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4481 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4491 /*********************************************************************
4494 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4496 return MSVCRT__ftelli64(file
);
4499 /*********************************************************************
4500 * _ftell_nolock (MSVCRT.@)
4502 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4504 return MSVCRT__ftelli64_nolock(file
);
4507 /*********************************************************************
4508 * fgetpos (MSVCRT.@)
4510 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4512 *pos
= MSVCRT__ftelli64(file
);
4518 /*********************************************************************
4521 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4523 MSVCRT_size_t len
= strlen(s
);
4526 MSVCRT__lock_file(file
);
4527 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4528 MSVCRT__unlock_file(file
);
4532 /*********************************************************************
4535 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4537 MSVCRT_size_t i
, len
= strlenW(s
);
4541 MSVCRT__lock_file(file
);
4542 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4543 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4544 MSVCRT__unlock_file(file
);
4548 tmp_buf
= add_std_buffer(file
);
4549 for (i
=0; i
<len
; i
++) {
4550 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4551 if(tmp_buf
) remove_std_buffer(file
);
4552 MSVCRT__unlock_file(file
);
4557 if(tmp_buf
) remove_std_buffer(file
);
4558 MSVCRT__unlock_file(file
);
4562 /*********************************************************************
4563 * getchar (MSVCRT.@)
4565 int CDECL
MSVCRT_getchar(void)
4567 return MSVCRT_fgetc(MSVCRT_stdin
);
4570 /*********************************************************************
4573 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4575 return MSVCRT_fgetc(file
);
4578 /*********************************************************************
4581 char * CDECL
MSVCRT_gets(char *buf
)
4584 char * buf_start
= buf
;
4586 MSVCRT__lock_file(MSVCRT_stdin
);
4587 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4588 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4589 if(cc
!= '\r') *buf
++ = (char)cc
;
4593 TRACE("got '%s'\n", buf_start
);
4594 MSVCRT__unlock_file(MSVCRT_stdin
);
4598 /*********************************************************************
4601 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4604 MSVCRT_wchar_t
* ws
= buf
;
4606 MSVCRT__lock_file(MSVCRT_stdin
);
4607 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4608 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4611 *buf
++ = (MSVCRT_wchar_t
)cc
;
4615 TRACE("got %s\n", debugstr_w(ws
));
4616 MSVCRT__unlock_file(MSVCRT_stdin
);
4620 /*********************************************************************
4623 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4625 return MSVCRT_fputc(c
, file
);
4628 /*********************************************************************
4629 * putchar (MSVCRT.@)
4631 int CDECL
MSVCRT_putchar(int c
)
4633 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4636 /*********************************************************************
4639 int CDECL
MSVCRT_puts(const char *s
)
4641 MSVCRT_size_t len
= strlen(s
);
4644 MSVCRT__lock_file(MSVCRT_stdout
);
4645 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4646 MSVCRT__unlock_file(MSVCRT_stdout
);
4650 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4651 MSVCRT__unlock_file(MSVCRT_stdout
);
4655 /*********************************************************************
4658 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4660 static const MSVCRT_wchar_t nl
= '\n';
4661 MSVCRT_size_t len
= strlenW(s
);
4664 MSVCRT__lock_file(MSVCRT_stdout
);
4665 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4666 MSVCRT__unlock_file(MSVCRT_stdout
);
4670 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4671 MSVCRT__unlock_file(MSVCRT_stdout
);
4675 /*********************************************************************
4678 int CDECL
MSVCRT_remove(const char *path
)
4680 TRACE("(%s)\n",path
);
4681 if (DeleteFileA(path
))
4683 TRACE(":failed (%d)\n",GetLastError());
4684 msvcrt_set_errno(GetLastError());
4688 /*********************************************************************
4689 * _wremove (MSVCRT.@)
4691 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4693 TRACE("(%s)\n",debugstr_w(path
));
4694 if (DeleteFileW(path
))
4696 TRACE(":failed (%d)\n",GetLastError());
4697 msvcrt_set_errno(GetLastError());
4701 /*********************************************************************
4704 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4706 TRACE(":from %s to %s\n",oldpath
,newpath
);
4707 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4709 TRACE(":failed (%d)\n",GetLastError());
4710 msvcrt_set_errno(GetLastError());
4714 /*********************************************************************
4715 * _wrename (MSVCRT.@)
4717 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4719 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4720 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4722 TRACE(":failed (%d)\n",GetLastError());
4723 msvcrt_set_errno(GetLastError());
4727 /*********************************************************************
4728 * setvbuf (MSVCRT.@)
4730 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4732 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4733 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4734 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4736 MSVCRT__lock_file(file
);
4738 MSVCRT__fflush_nolock(file
);
4739 if(file
->_flag
& MSVCRT__IOMYBUF
)
4740 MSVCRT_free(file
->_base
);
4741 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4744 if(mode
== MSVCRT__IONBF
) {
4745 file
->_flag
|= MSVCRT__IONBF
;
4746 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4749 file
->_base
= file
->_ptr
= buf
;
4750 file
->_flag
|= MSVCRT__USERBUF
;
4751 file
->_bufsiz
= size
;
4753 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4756 MSVCRT__unlock_file(file
);
4760 file
->_flag
|= MSVCRT__IOMYBUF
;
4761 file
->_bufsiz
= size
;
4763 MSVCRT__unlock_file(file
);
4767 /*********************************************************************
4770 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4772 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4775 /*********************************************************************
4778 char * CDECL
MSVCRT_tmpnam(char *s
)
4785 thread_data_t
*data
= msvcrt_get_thread_data();
4787 if(!data
->tmpnam_buffer
)
4788 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4790 s
= data
->tmpnam_buffer
;
4793 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4794 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4795 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4797 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4798 memcpy(p
, tmpstr
, size
);
4800 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4801 GetLastError() == ERROR_FILE_NOT_FOUND
)
4807 /*********************************************************************
4808 * _wtmpnam (MSVCRT.@)
4810 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4812 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4813 MSVCRT_wchar_t tmpstr
[16];
4817 thread_data_t
*data
= msvcrt_get_thread_data();
4819 if(!data
->wtmpnam_buffer
)
4820 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4822 s
= data
->wtmpnam_buffer
;
4825 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4826 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4827 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4829 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4830 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4832 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4833 GetLastError() == ERROR_FILE_NOT_FOUND
)
4839 /*********************************************************************
4840 * tmpfile (MSVCRT.@)
4842 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4844 char *filename
= MSVCRT_tmpnam(NULL
);
4846 MSVCRT_FILE
* file
= NULL
;
4849 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4850 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4851 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4853 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4858 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4861 if(fd
!= -1 && !file
)
4867 /*********************************************************************
4868 * tmpfile_s (MSVCRT.@)
4870 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4872 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4874 *file
= MSVCRT_tmpfile();
4878 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4880 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4883 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4887 MSVCRT__lock_file(file
);
4889 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4890 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4891 MSVCRT__unlock_file(file
);
4895 for(i
=0; i
<len
; i
++) {
4896 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
4897 MSVCRT__unlock_file(file
);
4902 MSVCRT__unlock_file(file
);
4906 /*********************************************************************
4907 * vfprintf (MSVCRT.@)
4909 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4914 MSVCRT__lock_file(file
);
4915 tmp_buf
= add_std_buffer(file
);
4916 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4917 if(tmp_buf
) remove_std_buffer(file
);
4918 MSVCRT__unlock_file(file
);
4923 /*********************************************************************
4924 * vfprintf_s (MSVCRT.@)
4926 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4931 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4933 MSVCRT__lock_file(file
);
4934 tmp_buf
= add_std_buffer(file
);
4935 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4936 if(tmp_buf
) remove_std_buffer(file
);
4937 MSVCRT__unlock_file(file
);
4942 /*********************************************************************
4943 * vfwprintf (MSVCRT.@)
4945 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4950 MSVCRT__lock_file(file
);
4951 tmp_buf
= add_std_buffer(file
);
4952 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4953 if(tmp_buf
) remove_std_buffer(file
);
4954 MSVCRT__unlock_file(file
);
4959 /*********************************************************************
4960 * vfwprintf_s (MSVCRT.@)
4962 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4967 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4969 MSVCRT__lock_file(file
);
4970 tmp_buf
= add_std_buffer(file
);
4971 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
4972 if(tmp_buf
) remove_std_buffer(file
);
4973 MSVCRT__unlock_file(file
);
4978 /*********************************************************************
4979 * _vfwprintf_l (MSVCRT.@)
4981 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4982 MSVCRT__locale_t locale
, __ms_va_list valist
)
4987 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4989 MSVCRT__lock_file(file
);
4990 tmp_buf
= add_std_buffer(file
);
4991 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4992 if(tmp_buf
) remove_std_buffer(file
);
4993 MSVCRT__unlock_file(file
);
4998 /*********************************************************************
4999 * vprintf (MSVCRT.@)
5001 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5003 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5006 /*********************************************************************
5007 * vprintf_s (MSVCRT.@)
5009 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5011 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5014 /*********************************************************************
5015 * vwprintf (MSVCRT.@)
5017 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5019 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5022 /*********************************************************************
5023 * vwprintf_s (MSVCRT.@)
5025 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5027 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5030 /*********************************************************************
5031 * fprintf (MSVCRT.@)
5033 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5035 __ms_va_list valist
;
5037 __ms_va_start(valist
, format
);
5038 res
= MSVCRT_vfprintf(file
, format
, valist
);
5039 __ms_va_end(valist
);
5043 /*********************************************************************
5044 * fprintf_s (MSVCRT.@)
5046 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5048 __ms_va_list valist
;
5050 __ms_va_start(valist
, format
);
5051 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5052 __ms_va_end(valist
);
5056 /*********************************************************************
5057 * fwprintf (MSVCRT.@)
5059 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5061 __ms_va_list valist
;
5063 __ms_va_start(valist
, format
);
5064 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5065 __ms_va_end(valist
);
5069 /*********************************************************************
5070 * fwprintf_s (MSVCRT.@)
5072 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5074 __ms_va_list valist
;
5076 __ms_va_start(valist
, format
);
5077 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5078 __ms_va_end(valist
);
5082 /*********************************************************************
5083 * _fwprintf_l (MSVCRT.@)
5085 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5087 __ms_va_list valist
;
5089 __ms_va_start(valist
, locale
);
5090 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5091 __ms_va_end(valist
);
5095 /*********************************************************************
5098 int CDECL
MSVCRT_printf(const char *format
, ...)
5100 __ms_va_list valist
;
5102 __ms_va_start(valist
, format
);
5103 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5104 __ms_va_end(valist
);
5108 /*********************************************************************
5109 * printf_s (MSVCRT.@)
5111 int CDECL
MSVCRT_printf_s(const char *format
, ...)
5113 __ms_va_list valist
;
5115 __ms_va_start(valist
, format
);
5116 res
= MSVCRT_vprintf_s(format
, valist
);
5117 __ms_va_end(valist
);
5121 /*********************************************************************
5124 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5128 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5130 MSVCRT__lock_file(file
);
5131 ret
= MSVCRT__ungetc_nolock(c
, file
);
5132 MSVCRT__unlock_file(file
);
5137 /*********************************************************************
5138 * _ungetc_nolock (MSVCRT.@)
5140 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5142 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5144 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5145 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5148 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5149 && msvcrt_alloc_buffer(file
))
5150 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5153 if(file
->_ptr
>file
->_base
) {
5155 if(file
->_flag
& MSVCRT__IOSTRG
) {
5156 if(*file
->_ptr
!= c
) {
5164 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5165 file
->_flag
|= MSVCRT__IOREAD
;
5172 /*********************************************************************
5173 * ungetwc (MSVCRT.@)
5175 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5179 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5181 MSVCRT__lock_file(file
);
5182 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5183 MSVCRT__unlock_file(file
);
5188 /*********************************************************************
5189 * _ungetwc_nolock (MSVCRT.@)
5191 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5193 MSVCRT_wchar_t mwc
= wc
;
5195 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5196 if (wc
== MSVCRT_WEOF
)
5199 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5200 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5201 unsigned char * pp
= (unsigned char *)&mwc
;
5204 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5205 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5209 char mbs
[MSVCRT_MB_LEN_MAX
];
5212 len
= MSVCRT_wctomb(mbs
, mwc
);
5216 for(len
--; len
>=0; len
--) {
5217 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5225 /*********************************************************************
5226 * wprintf (MSVCRT.@)
5228 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5230 __ms_va_list valist
;
5232 __ms_va_start(valist
, format
);
5233 res
= MSVCRT_vwprintf(format
, valist
);
5234 __ms_va_end(valist
);
5238 /*********************************************************************
5239 * wprintf_s (MSVCRT.@)
5241 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5243 __ms_va_list valist
;
5245 __ms_va_start(valist
, format
);
5246 res
= MSVCRT_vwprintf_s(format
, valist
);
5247 __ms_va_end(valist
);
5251 /*********************************************************************
5252 * _getmaxstdio (MSVCRT.@)
5254 int CDECL
MSVCRT__getmaxstdio(void)
5256 return MSVCRT_max_streams
;
5259 /*********************************************************************
5260 * _setmaxstdio (MSVCRT.@)
5262 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5264 TRACE("%d\n", newmax
);
5266 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5269 MSVCRT_max_streams
= newmax
;
5270 return MSVCRT_max_streams
;