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 /*********************************************************************
761 * __acrt_iob_func(MSVCRT.@)
763 MSVCRT_FILE
* CDECL
MSVCRT___acrt_iob_func(unsigned idx
)
765 return &MSVCRT__iob
[idx
];
768 /*********************************************************************
771 int CDECL
MSVCRT__access(const char *filename
, int mode
)
773 DWORD attr
= GetFileAttributesA(filename
);
775 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
777 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
779 msvcrt_set_errno(GetLastError());
782 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
784 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
790 /*********************************************************************
791 * _access_s (MSVCRT.@)
793 int CDECL
MSVCRT__access_s(const char *filename
, int mode
)
795 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
796 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
798 if (MSVCRT__access(filename
, mode
) == -1)
799 return *MSVCRT__errno();
803 /*********************************************************************
804 * _waccess (MSVCRT.@)
806 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
808 DWORD attr
= GetFileAttributesW(filename
);
810 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
812 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
814 msvcrt_set_errno(GetLastError());
817 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
819 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
825 /*********************************************************************
826 * _waccess_s (MSVCRT.@)
828 int CDECL
MSVCRT__waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
830 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return *MSVCRT__errno();
831 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return *MSVCRT__errno();
833 if (MSVCRT__waccess(filename
, mode
) == -1)
834 return *MSVCRT__errno();
838 /*********************************************************************
841 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
843 DWORD oldFlags
= GetFileAttributesA(path
);
845 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
847 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
848 oldFlags
| FILE_ATTRIBUTE_READONLY
;
850 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
853 msvcrt_set_errno(GetLastError());
857 /*********************************************************************
860 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
862 DWORD oldFlags
= GetFileAttributesW(path
);
864 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
866 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
867 oldFlags
| FILE_ATTRIBUTE_READONLY
;
869 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
872 msvcrt_set_errno(GetLastError());
876 /*********************************************************************
879 int CDECL
MSVCRT__unlink(const char *path
)
881 TRACE("%s\n",debugstr_a(path
));
882 if(DeleteFileA(path
))
884 TRACE("failed (%d)\n",GetLastError());
885 msvcrt_set_errno(GetLastError());
889 /*********************************************************************
890 * _wunlink (MSVCRT.@)
892 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
894 TRACE("(%s)\n",debugstr_w(path
));
895 if(DeleteFileW(path
))
897 TRACE("failed (%d)\n",GetLastError());
898 msvcrt_set_errno(GetLastError());
902 /*********************************************************************
905 int CDECL
MSVCRT__commit(int fd
)
907 ioinfo
*info
= get_ioinfo(fd
);
910 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
912 if (info
->handle
== INVALID_HANDLE_VALUE
)
914 else if (!FlushFileBuffers(info
->handle
))
916 if (GetLastError() == ERROR_INVALID_HANDLE
)
918 /* FlushFileBuffers fails for console handles
919 * so we ignore this error.
925 TRACE(":failed-last error (%d)\n",GetLastError());
926 msvcrt_set_errno(GetLastError());
936 release_ioinfo(info
);
940 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
941 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
943 /* INTERNAL: Flush all stream buffer */
944 static int msvcrt_flush_all_buffers(int mask
)
946 int i
, num_flushed
= 0;
950 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
951 file
= msvcrt_get_file(i
);
955 if(file
->_flag
& mask
) {
963 TRACE(":flushed (%d) handles\n",num_flushed
);
967 /*********************************************************************
968 * _flushall (MSVCRT.@)
970 int CDECL
MSVCRT__flushall(void)
972 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
975 /*********************************************************************
978 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
983 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
986 MSVCRT__lock_file(file
);
987 ret
= MSVCRT__fflush_nolock(file
);
988 MSVCRT__unlock_file(file
);
994 /*********************************************************************
995 * _fflush_nolock (MSVCRT.@)
997 int CDECL
MSVCRT__fflush_nolock(MSVCRT_FILE
* file
)
1002 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
1006 res
= msvcrt_flush_buffer(file
);
1007 if(!res
&& (file
->_flag
& MSVCRT__IOCOMMIT
))
1008 res
= MSVCRT__commit(file
->_file
) ? MSVCRT_EOF
: 0;
1012 /*********************************************************************
1015 int CDECL
MSVCRT__close(int fd
)
1017 ioinfo
*info
= get_ioinfo(fd
);
1020 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1021 if (!(info
->wxflag
& WX_OPEN
)) {
1024 ret
= CloseHandle(info
->handle
) ? 0 : -1;
1027 WARN(":failed-last error (%d)\n",GetLastError());
1028 msvcrt_set_errno(GetLastError());
1031 release_ioinfo(info
);
1035 /*********************************************************************
1038 * MSDN isn't clear on this point, but the remarks for _pipe
1039 * indicate file descriptors duplicated with _dup and _dup2 are always
1042 int CDECL
MSVCRT__dup2(int od
, int nd
)
1044 ioinfo
*info_od
, *info_nd
;
1047 TRACE("(od=%d, nd=%d)\n", od
, nd
);
1051 info_od
= get_ioinfo(od
);
1052 info_nd
= get_ioinfo_alloc_fd(nd
);
1056 info_nd
= get_ioinfo_alloc_fd(nd
);
1057 info_od
= get_ioinfo(od
);
1060 if (info_nd
== &MSVCRT___badioinfo
)
1064 else if (info_od
->wxflag
& WX_OPEN
)
1068 if (DuplicateHandle(GetCurrentProcess(), info_od
->handle
,
1069 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1071 int wxflag
= info_od
->wxflag
& ~MSVCRT__O_NOINHERIT
;
1073 if (info_nd
->wxflag
& WX_OPEN
)
1076 msvcrt_set_fd(info_nd
, handle
, wxflag
);
1077 /* _dup2 returns 0, not nd, on success */
1083 msvcrt_set_errno(GetLastError());
1088 *MSVCRT__errno() = MSVCRT_EBADF
;
1092 release_ioinfo(info_od
);
1093 release_ioinfo(info_nd
);
1097 /*********************************************************************
1100 int CDECL
MSVCRT__dup(int od
)
1103 ioinfo
*info
= get_ioinfo_alloc(&fd
);
1105 if (MSVCRT__dup2(od
, fd
) == 0)
1109 release_ioinfo(info
);
1113 /*********************************************************************
1116 int CDECL
MSVCRT__eof(int fd
)
1118 ioinfo
*info
= get_ioinfo(fd
);
1119 DWORD curpos
,endpos
;
1120 LONG hcurpos
,hendpos
;
1122 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1124 if (info
->handle
== INVALID_HANDLE_VALUE
)
1126 release_ioinfo(info
);
1130 if (info
->wxflag
& WX_ATEOF
)
1132 release_ioinfo(info
);
1136 /* Otherwise we do it the hard way */
1137 hcurpos
= hendpos
= 0;
1138 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1139 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1141 if (curpos
== endpos
&& hcurpos
== hendpos
)
1143 /* FIXME: shouldn't WX_ATEOF be set here? */
1144 release_ioinfo(info
);
1148 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1149 release_ioinfo(info
);
1153 /*********************************************************************
1154 * _fcloseall (MSVCRT.@)
1156 int CDECL
MSVCRT__fcloseall(void)
1158 int num_closed
= 0, i
;
1162 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1163 file
= msvcrt_get_file(i
);
1165 if (file
->_flag
&& !MSVCRT_fclose(file
))
1170 TRACE(":closed (%d) handles\n",num_closed
);
1174 /* free everything on process exit */
1175 void msvcrt_free_io(void)
1181 MSVCRT__fcloseall();
1183 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1185 if(!MSVCRT___pioinfo
[i
])
1188 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1190 if(MSVCRT___pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1191 DeleteCriticalSection(&MSVCRT___pioinfo
[i
][j
].crit
);
1193 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1196 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1198 MSVCRT_FILE
*file
= msvcrt_get_file(j
);
1199 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1201 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1202 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1206 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1207 MSVCRT_free(MSVCRT_fstream
[i
]);
1210 /*********************************************************************
1211 * _lseeki64 (MSVCRT.@)
1213 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1215 ioinfo
*info
= get_ioinfo(fd
);
1218 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1220 if (info
->handle
== INVALID_HANDLE_VALUE
)
1222 release_ioinfo(info
);
1226 if (whence
< 0 || whence
> 2)
1228 release_ioinfo(info
);
1229 *MSVCRT__errno() = MSVCRT_EINVAL
;
1233 TRACE(":fd (%d) to %s pos %s\n",
1234 fd
,wine_dbgstr_longlong(offset
),
1235 (whence
==MSVCRT_SEEK_SET
)?"SEEK_SET":
1236 (whence
==MSVCRT_SEEK_CUR
)?"SEEK_CUR":
1237 (whence
==MSVCRT_SEEK_END
)?"SEEK_END":"UNKNOWN");
1239 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1240 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1241 ofs
.QuadPart
= offset
;
1242 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1243 GetLastError() == ERROR_SUCCESS
)
1245 info
->wxflag
&= ~WX_ATEOF
;
1246 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1248 release_ioinfo(info
);
1249 return ofs
.QuadPart
;
1251 release_ioinfo(info
);
1252 TRACE(":error-last error (%d)\n",GetLastError());
1253 msvcrt_set_errno(GetLastError());
1257 /*********************************************************************
1260 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1262 return MSVCRT__lseeki64(fd
, offset
, whence
);
1265 /*********************************************************************
1266 * _lock_file (MSVCRT.@)
1268 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1270 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1271 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1273 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1276 /*********************************************************************
1277 * _unlock_file (MSVCRT.@)
1279 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1281 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1282 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1284 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1287 /*********************************************************************
1288 * _locking (MSVCRT.@)
1290 * This is untested; the underlying LockFile doesn't work yet.
1292 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1294 ioinfo
*info
= get_ioinfo(fd
);
1298 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1299 if (info
->handle
== INVALID_HANDLE_VALUE
)
1301 release_ioinfo(info
);
1305 if (mode
< 0 || mode
> 4)
1307 release_ioinfo(info
);
1308 *MSVCRT__errno() = MSVCRT_EINVAL
;
1312 TRACE(":fd (%d) by 0x%08x mode %s\n",
1313 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1314 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1315 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1316 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1317 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1320 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, FILE_CURRENT
)) == INVALID_SET_FILE_POINTER
)
1322 release_ioinfo(info
);
1323 FIXME ("Seek failed\n");
1324 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1327 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1330 ret
= 1; /* just to satisfy gcc */
1333 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1338 else if (mode
== MSVCRT__LK_UNLCK
)
1339 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1341 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1342 /* FIXME - what about error settings? */
1343 release_ioinfo(info
);
1344 return ret
? 0 : -1;
1347 /*********************************************************************
1348 * _fseeki64 (MSVCRT.@)
1350 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1354 MSVCRT__lock_file(file
);
1355 ret
= MSVCRT__fseeki64_nolock(file
, offset
, whence
);
1356 MSVCRT__unlock_file(file
);
1361 /*********************************************************************
1362 * _fseeki64_nolock (MSVCRT.@)
1364 int CDECL
MSVCRT__fseeki64_nolock(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1368 if(whence
== MSVCRT_SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1369 whence
= MSVCRT_SEEK_SET
;
1370 offset
+= MSVCRT__ftelli64_nolock(file
);
1373 /* Flush output if needed */
1374 msvcrt_flush_buffer(file
);
1375 /* Reset direction of i/o */
1376 if(file
->_flag
& MSVCRT__IORW
) {
1377 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1379 /* Clear end of file flag */
1380 file
->_flag
&= ~MSVCRT__IOEOF
;
1381 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1386 /*********************************************************************
1389 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1391 return MSVCRT__fseeki64( file
, offset
, whence
);
1394 /*********************************************************************
1395 * _fseek_nolock (MSVCRT.@)
1397 int CDECL
MSVCRT__fseek_nolock(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1399 return MSVCRT__fseeki64_nolock( file
, offset
, whence
);
1402 /*********************************************************************
1403 * _chsize_s (MSVCRT.@)
1405 int CDECL
MSVCRT__chsize_s(int fd
, __int64 size
)
1411 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1413 if (!MSVCRT_CHECK_PMT(size
>= 0)) return MSVCRT_EINVAL
;
1416 info
= get_ioinfo(fd
);
1417 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1419 /* save the current file pointer */
1420 cur
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1423 pos
= MSVCRT__lseeki64(fd
, size
, MSVCRT_SEEK_SET
);
1426 ret
= SetEndOfFile(info
->handle
);
1427 if (!ret
) msvcrt_set_errno(GetLastError());
1430 /* restore the file pointer */
1431 MSVCRT__lseeki64(fd
, cur
, MSVCRT_SEEK_SET
);
1435 release_ioinfo(info
);
1436 return ret
? 0 : *MSVCRT__errno();
1439 /*********************************************************************
1440 * _chsize (MSVCRT.@)
1442 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1444 /* _chsize_s returns errno on failure but _chsize should return -1 */
1445 return MSVCRT__chsize_s( fd
, size
) == 0 ? 0 : -1;
1448 /*********************************************************************
1449 * clearerr (MSVCRT.@)
1451 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1453 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1455 MSVCRT__lock_file(file
);
1456 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1457 MSVCRT__unlock_file(file
);
1460 /*********************************************************************
1463 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1465 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1467 MSVCRT__lock_file(file
);
1468 MSVCRT__fseek_nolock(file
, 0L, MSVCRT_SEEK_SET
);
1469 MSVCRT_clearerr(file
);
1470 MSVCRT__unlock_file(file
);
1473 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1475 int plus
= strchrW(mode
, '+') != NULL
;
1477 TRACE("%s\n", debugstr_w(mode
));
1479 while(*mode
== ' ') mode
++;
1484 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1485 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1488 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1489 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1492 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1493 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1496 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1500 *stream_flags
|= MSVCRT__commode
;
1502 while (*mode
&& *mode
!=',')
1506 *open_flags
|= MSVCRT__O_BINARY
;
1507 *open_flags
&= ~MSVCRT__O_TEXT
;
1510 *open_flags
|= MSVCRT__O_TEXT
;
1511 *open_flags
&= ~MSVCRT__O_BINARY
;
1514 *open_flags
|= MSVCRT__O_TEMPORARY
;
1517 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1520 *stream_flags
|= MSVCRT__IOCOMMIT
;
1523 *stream_flags
&= ~MSVCRT__IOCOMMIT
;
1526 *open_flags
|= MSVCRT__O_NOINHERIT
;
1535 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1538 ERR("incorrect mode flag: %c\n", mode
[-1]);
1544 static const WCHAR ccs
[] = {'c','c','s'};
1545 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1546 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1547 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1550 while(*mode
== ' ') mode
++;
1551 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1553 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1554 while(*mode
== ' ') mode
++;
1555 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1558 while(*mode
== ' ') mode
++;
1560 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1562 *open_flags
|= MSVCRT__O_U8TEXT
;
1563 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1565 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1567 *open_flags
|= MSVCRT__O_U16TEXT
;
1568 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1570 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1572 *open_flags
|= MSVCRT__O_WTEXT
;
1573 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1577 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1581 while(*mode
== ' ') mode
++;
1584 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1589 /*********************************************************************
1590 * _fdopen (MSVCRT.@)
1592 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1595 MSVCRT_wchar_t
*modeW
= NULL
;
1597 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1599 ret
= MSVCRT__wfdopen(fd
, modeW
);
1605 /*********************************************************************
1606 * _wfdopen (MSVCRT.@)
1608 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1610 int open_flags
, stream_flags
;
1613 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1616 if (!(file
= msvcrt_alloc_fp()))
1618 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1623 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1629 /*********************************************************************
1630 * _filelength (MSVCRT.@)
1632 LONG CDECL
MSVCRT__filelength(int fd
)
1634 LONG curPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
1637 LONG endPos
= MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_END
);
1640 if (endPos
!= curPos
)
1641 MSVCRT__lseek(fd
, curPos
, MSVCRT_SEEK_SET
);
1648 /*********************************************************************
1649 * _filelengthi64 (MSVCRT.@)
1651 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1653 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
1656 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_END
);
1659 if (endPos
!= curPos
)
1660 MSVCRT__lseeki64(fd
, curPos
, MSVCRT_SEEK_SET
);
1667 /*********************************************************************
1668 * _fileno (MSVCRT.@)
1670 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1672 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1676 /*********************************************************************
1677 * _fstat64 (MSVCRT.@)
1679 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1681 ioinfo
*info
= get_ioinfo(fd
);
1684 BY_HANDLE_FILE_INFORMATION hfi
;
1686 TRACE(":fd (%d) stat (%p)\n", fd
, buf
);
1687 if (info
->handle
== INVALID_HANDLE_VALUE
)
1689 release_ioinfo(info
);
1695 WARN(":failed-NULL buf\n");
1696 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1697 release_ioinfo(info
);
1701 memset(&hfi
, 0, sizeof(hfi
));
1702 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1703 type
= GetFileType(info
->handle
);
1704 if (type
== FILE_TYPE_PIPE
)
1706 buf
->st_dev
= buf
->st_rdev
= fd
;
1707 buf
->st_mode
= MSVCRT__S_IFIFO
;
1710 else if (type
== FILE_TYPE_CHAR
)
1712 buf
->st_dev
= buf
->st_rdev
= fd
;
1713 buf
->st_mode
= MSVCRT__S_IFCHR
;
1716 else /* FILE_TYPE_DISK etc. */
1718 if (!GetFileInformationByHandle(info
->handle
, &hfi
))
1720 WARN(":failed-last error (%d)\n",GetLastError());
1721 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1722 release_ioinfo(info
);
1725 buf
->st_mode
= MSVCRT__S_IFREG
| 0444;
1726 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1727 buf
->st_mode
|= 0222;
1728 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1729 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1731 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1732 buf
->st_mtime
= buf
->st_ctime
= dw
;
1733 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1735 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1737 release_ioinfo(info
);
1741 /*********************************************************************
1742 * _fstati64 (MSVCRT.@)
1744 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1747 struct MSVCRT__stat64 buf64
;
1749 ret
= MSVCRT__fstat64(fd
, &buf64
);
1751 msvcrt_stat64_to_stati64(&buf64
, buf
);
1755 /*********************************************************************
1758 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1760 struct MSVCRT__stat64 buf64
;
1762 ret
= MSVCRT__fstat64(fd
, &buf64
);
1764 msvcrt_stat64_to_stat(&buf64
, buf
);
1768 /*********************************************************************
1769 * _fstat32 (MSVCR80.@)
1771 int CDECL
MSVCRT__fstat32(int fd
, struct MSVCRT__stat32
* buf
)
1774 struct MSVCRT__stat64 buf64
;
1776 ret
= MSVCRT__fstat64(fd
, &buf64
);
1778 msvcrt_stat64_to_stat32(&buf64
, buf
);
1782 /*********************************************************************
1783 * _fstat64i32 (MSVCR80.@)
1785 int CDECL
MSVCRT__fstat64i32(int fd
, struct MSVCRT__stat64i32
* buf
)
1788 struct MSVCRT__stat64 buf64
;
1790 ret
= MSVCRT__fstat64(fd
, &buf64
);
1792 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
1796 /*********************************************************************
1797 * _futime64 (MSVCRT.@)
1799 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1801 ioinfo
*info
= get_ioinfo(fd
);
1806 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1811 time_to_filetime( t
->actime
, &at
);
1812 time_to_filetime( t
->modtime
, &wt
);
1815 if (!SetFileTime(info
->handle
, NULL
, &at
, &wt
))
1817 release_ioinfo(info
);
1818 msvcrt_set_errno(GetLastError());
1821 release_ioinfo(info
);
1825 /*********************************************************************
1826 * _futime32 (MSVCRT.@)
1828 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1832 struct MSVCRT___utimbuf64 t64
;
1833 t64
.actime
= t
->actime
;
1834 t64
.modtime
= t
->modtime
;
1835 return _futime64( fd
, &t64
);
1838 return _futime64( fd
, NULL
);
1841 /*********************************************************************
1842 * _futime (MSVCRT.@)
1845 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1847 return _futime64( fd
, t
);
1850 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1852 return _futime32( fd
, t
);
1856 /*********************************************************************
1857 * _get_osfhandle (MSVCRT.@)
1859 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1861 HANDLE hand
= get_ioinfo_nolock(fd
)->handle
;
1862 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1864 if(hand
== INVALID_HANDLE_VALUE
)
1865 *MSVCRT__errno() = MSVCRT_EBADF
;
1866 return (MSVCRT_intptr_t
)hand
;
1869 /*********************************************************************
1870 * _mktemp_s (MSVCRT.@)
1872 int CDECL
MSVCRT__mktemp_s(char *pattern
, MSVCRT_size_t size
)
1876 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1877 return MSVCRT_EINVAL
;
1879 for(len
=0; len
<size
; len
++)
1882 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1885 return MSVCRT_EINVAL
;
1888 for(xno
=1; xno
<=6; xno
++)
1889 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1890 return MSVCRT_EINVAL
;
1892 id
= GetCurrentProcessId();
1893 for(xno
=1; xno
<6; xno
++) {
1894 pattern
[len
-xno
] = id
%10 + '0';
1898 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1899 if(GetFileAttributesA(pattern
) == INVALID_FILE_ATTRIBUTES
)
1904 *MSVCRT__errno() = MSVCRT_EEXIST
;
1905 return MSVCRT_EEXIST
;
1908 /*********************************************************************
1909 * _mktemp (MSVCRT.@)
1911 char * CDECL
MSVCRT__mktemp(char *pattern
)
1914 char *retVal
= pattern
;
1922 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1926 id
= GetCurrentProcessId();
1930 int tempNum
= id
/ 10;
1931 *pattern
-- = id
- (tempNum
* 10) + '0';
1937 *pattern
= letter
++;
1938 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1940 } while(letter
<= 'z');
1944 /*********************************************************************
1945 * _wmktemp_s (MSVCRT.@)
1947 int CDECL
MSVCRT__wmktemp_s(MSVCRT_wchar_t
*pattern
, MSVCRT_size_t size
)
1951 if(!MSVCRT_CHECK_PMT(pattern
!=NULL
))
1952 return MSVCRT_EINVAL
;
1954 for(len
=0; len
<size
; len
++)
1957 if(!MSVCRT_CHECK_PMT(len
!=size
&& len
>=6)) {
1960 return MSVCRT_EINVAL
;
1963 for(xno
=1; xno
<=6; xno
++)
1964 if(!MSVCRT_CHECK_PMT(pattern
[len
-xno
] == 'X'))
1965 return MSVCRT_EINVAL
;
1967 id
= GetCurrentProcessId();
1968 for(xno
=1; xno
<6; xno
++) {
1969 pattern
[len
-xno
] = id
%10 + '0';
1973 for(pattern
[len
-6]='a'; pattern
[len
-6]<='z'; pattern
[len
-6]++) {
1974 if(GetFileAttributesW(pattern
) == INVALID_FILE_ATTRIBUTES
)
1979 *MSVCRT__errno() = MSVCRT_EEXIST
;
1980 return MSVCRT_EEXIST
;
1983 /*********************************************************************
1984 * _wmktemp (MSVCRT.@)
1986 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1989 MSVCRT_wchar_t
*retVal
= pattern
;
1991 MSVCRT_wchar_t letter
= 'a';
1997 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
2001 id
= GetCurrentProcessId();
2005 int tempNum
= id
/ 10;
2006 *pattern
-- = id
- (tempNum
* 10) + '0';
2012 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
)
2014 *pattern
= letter
++;
2015 } while(letter
!= '|');
2019 static unsigned split_oflags(unsigned oflags
)
2022 unsigned unsupp
; /* until we support everything */
2024 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
2025 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
2026 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
2027 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
2028 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
2029 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
2030 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
2031 else wxflags
|= WX_TEXT
; /* default to TEXT*/
2032 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
2034 if ((unsupp
= oflags
& ~(
2035 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
2036 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
2037 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
2038 MSVCRT__O_NOINHERIT
|
2039 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
2040 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
2042 ERR(":unsupported oflags 0x%04x\n",unsupp
);
2047 /*********************************************************************
2050 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
2053 SECURITY_ATTRIBUTES sa
;
2054 HANDLE readHandle
, writeHandle
;
2058 *MSVCRT__errno() = MSVCRT_EINVAL
;
2062 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
2063 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
2064 sa
.lpSecurityDescriptor
= NULL
;
2065 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
2067 unsigned int wxflags
= split_oflags(textmode
);
2070 fd
= msvcrt_alloc_fd(readHandle
, wxflags
|WX_PIPE
);
2074 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
|WX_PIPE
);
2082 MSVCRT__close(pfds
[0]);
2083 CloseHandle(writeHandle
);
2084 *MSVCRT__errno() = MSVCRT_EMFILE
;
2089 CloseHandle(readHandle
);
2090 CloseHandle(writeHandle
);
2091 *MSVCRT__errno() = MSVCRT_EMFILE
;
2095 msvcrt_set_errno(GetLastError());
2100 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
2102 char bom
[sizeof(utf8_bom
)];
2105 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2107 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
2110 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
2111 oflags
|= MSVCRT__O_U8TEXT
;
2112 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
2114 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
2115 oflags
|= MSVCRT__O_U16TEXT
;
2117 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
2123 /*********************************************************************
2124 * _wsopen_s (MSVCRT.@)
2126 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
2128 DWORD access
= 0, creation
= 0, attrib
;
2129 SECURITY_ATTRIBUTES sa
;
2130 DWORD sharing
, type
;
2134 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
2135 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
2137 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
2140 wxflag
= split_oflags(oflags
);
2141 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
2143 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
2144 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
2145 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
2148 if (oflags
& MSVCRT__O_CREAT
)
2150 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
2151 FIXME(": pmode 0x%04x ignored\n", pmode
);
2153 if (oflags
& MSVCRT__O_EXCL
)
2154 creation
= CREATE_NEW
;
2155 else if (oflags
& MSVCRT__O_TRUNC
)
2156 creation
= CREATE_ALWAYS
;
2158 creation
= OPEN_ALWAYS
;
2160 else /* no MSVCRT__O_CREAT */
2162 if (oflags
& MSVCRT__O_TRUNC
)
2163 creation
= TRUNCATE_EXISTING
;
2165 creation
= OPEN_EXISTING
;
2170 case MSVCRT__SH_DENYRW
:
2173 case MSVCRT__SH_DENYWR
:
2174 sharing
= FILE_SHARE_READ
;
2176 case MSVCRT__SH_DENYRD
:
2177 sharing
= FILE_SHARE_WRITE
;
2179 case MSVCRT__SH_DENYNO
:
2180 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
2183 ERR( "Unhandled shflags 0x%x\n", shflags
);
2184 return MSVCRT_EINVAL
;
2187 if (!(pmode
& ~MSVCRT_umask
& MSVCRT__S_IWRITE
))
2188 attrib
= FILE_ATTRIBUTE_READONLY
;
2190 attrib
= FILE_ATTRIBUTE_NORMAL
;
2192 if (oflags
& MSVCRT__O_TEMPORARY
)
2194 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
2196 sharing
|= FILE_SHARE_DELETE
;
2199 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
2200 sa
.lpSecurityDescriptor
= NULL
;
2201 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
2203 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2204 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
2205 && !(access
&GENERIC_READ
))
2207 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2208 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
2209 if (hand
!= INVALID_HANDLE_VALUE
)
2211 oflags
= check_bom(hand
, oflags
, FALSE
);
2215 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
2218 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
2219 if (hand
== INVALID_HANDLE_VALUE
) {
2220 WARN(":failed-last error (%d)\n",GetLastError());
2221 msvcrt_set_errno(GetLastError());
2222 return *MSVCRT__errno();
2225 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
2227 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
2228 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
2229 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
2231 if (oflags
& MSVCRT__O_U8TEXT
)
2233 DWORD written
= 0, tmp
;
2235 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
2236 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
2238 if (written
!= sizeof(utf8_bom
)) {
2239 WARN("error writing BOM\n");
2241 msvcrt_set_errno(GetLastError());
2242 return *MSVCRT__errno();
2247 DWORD written
= 0, tmp
;
2249 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
2250 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
2252 if (written
!= sizeof(utf16_bom
))
2254 WARN("error writing BOM\n");
2256 msvcrt_set_errno(GetLastError());
2257 return *MSVCRT__errno();
2261 else if (access
& GENERIC_READ
)
2262 oflags
= check_bom(hand
, oflags
, TRUE
);
2265 type
= GetFileType(hand
);
2266 if (type
== FILE_TYPE_CHAR
)
2268 else if (type
== FILE_TYPE_PIPE
)
2271 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
2273 return *MSVCRT__errno();
2275 if (oflags
& MSVCRT__O_WTEXT
)
2276 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
2277 else if (oflags
& MSVCRT__O_U16TEXT
)
2278 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
2279 else if (oflags
& MSVCRT__O_U8TEXT
)
2280 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
2282 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
2286 /*********************************************************************
2287 * _wsopen (MSVCRT.@)
2289 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
2294 if (oflags
& MSVCRT__O_CREAT
)
2298 __ms_va_start(ap
, shflags
);
2299 pmode
= va_arg(ap
, int);
2305 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2309 /*********************************************************************
2310 * _sopen_s (MSVCRT.@)
2312 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
2314 MSVCRT_wchar_t
*pathW
;
2317 if (!MSVCRT_CHECK_PMT(fd
!= NULL
))
2318 return MSVCRT_EINVAL
;
2320 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
2321 return MSVCRT_EINVAL
;
2323 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
2328 /*********************************************************************
2331 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
2336 if (oflags
& MSVCRT__O_CREAT
)
2340 __ms_va_start(ap
, shflags
);
2341 pmode
= va_arg(ap
, int);
2347 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2351 /*********************************************************************
2354 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2358 if (flags
& MSVCRT__O_CREAT
)
2361 __ms_va_start(ap
, flags
);
2362 pmode
= va_arg(ap
, int);
2364 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2367 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2370 /*********************************************************************
2373 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2377 if (flags
& MSVCRT__O_CREAT
)
2380 __ms_va_start(ap
, flags
);
2381 pmode
= va_arg(ap
, int);
2383 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2386 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2389 /*********************************************************************
2392 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2394 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2395 return MSVCRT__open(path
, usedFlags
);
2398 /*********************************************************************
2399 * _wcreat (MSVCRT.@)
2401 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2403 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2404 return MSVCRT__wopen(path
, usedFlags
);
2407 /*********************************************************************
2408 * _open_osfhandle (MSVCRT.@)
2410 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2415 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2416 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2417 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2418 * text - it never sets MSVCRT__O_BINARY.
2420 /* don't let split_oflags() decide the mode if no mode is passed */
2421 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2422 oflags
|= MSVCRT__O_BINARY
;
2424 flags
= GetFileType((HANDLE
)handle
);
2425 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2427 msvcrt_set_errno(GetLastError());
2431 if (flags
== FILE_TYPE_CHAR
)
2433 else if (flags
== FILE_TYPE_PIPE
)
2437 flags
|= split_oflags(oflags
);
2439 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2440 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2444 /*********************************************************************
2447 int CDECL
MSVCRT__rmtmp(void)
2449 int num_removed
= 0, i
;
2453 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2454 file
= msvcrt_get_file(i
);
2456 if (file
->_tmpfname
)
2458 MSVCRT_fclose(file
);
2465 TRACE(":removed (%d) temp files\n",num_removed
);
2469 static inline int get_utf8_char_len(char ch
)
2471 if((ch
&0xf8) == 0xf0)
2473 else if((ch
&0xf0) == 0xe0)
2475 else if((ch
&0xe0) == 0xc0)
2480 /*********************************************************************
2481 * (internal) read_utf8
2483 static int read_utf8(ioinfo
*fdinfo
, MSVCRT_wchar_t
*buf
, unsigned int count
)
2485 HANDLE hand
= fdinfo
->handle
;
2486 char min_buf
[4], *readbuf
, lookahead
;
2487 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2489 /* make the buffer big enough to hold at least one character */
2490 /* read bytes have to fit to output and lookahead buffers */
2492 readbuf_size
= count
< 4 ? 4 : count
;
2493 if(readbuf_size
<=4 || !(readbuf
= MSVCRT_malloc(readbuf_size
))) {
2498 if(fdinfo
->lookahead
[0] != '\n') {
2499 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2500 fdinfo
->lookahead
[0] = '\n';
2502 if(fdinfo
->lookahead
[1] != '\n') {
2503 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2504 fdinfo
->lookahead
[1] = '\n';
2506 if(fdinfo
->lookahead
[2] != '\n') {
2507 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2508 fdinfo
->lookahead
[2] = '\n';
2513 /* NOTE: this case is broken in native dll, reading
2514 * sometimes fails when small buffer is passed
2517 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2518 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2519 fdinfo
->wxflag
|= WX_ATEOF
;
2522 msvcrt_set_errno(GetLastError());
2525 }else if(!num_read
) {
2526 fdinfo
->wxflag
|= WX_ATEOF
;
2532 char_len
= get_utf8_char_len(readbuf
[0]);
2534 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2538 if(readbuf
[0] == '\n')
2539 fdinfo
->wxflag
|= WX_READNL
;
2541 fdinfo
->wxflag
&= ~WX_READNL
;
2543 if(readbuf
[0] == 0x1a) {
2544 fdinfo
->wxflag
|= WX_ATEOF
;
2548 if(readbuf
[0] == '\r') {
2549 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2551 else if(lookahead
== '\n')
2555 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2556 fdinfo
->lookahead
[0] = lookahead
;
2558 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2563 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2564 msvcrt_set_errno(GetLastError());
2571 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2574 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2575 fdinfo
->wxflag
|= WX_ATEOF
;
2576 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2579 msvcrt_set_errno(GetLastError());
2580 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2583 }else if(!pos
&& !num_read
) {
2584 fdinfo
->wxflag
|= WX_ATEOF
;
2585 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2590 if(readbuf
[0] == '\n')
2591 fdinfo
->wxflag
|= WX_READNL
;
2593 fdinfo
->wxflag
&= ~WX_READNL
;
2595 /* Find first byte of last character (may be incomplete) */
2596 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2597 if((readbuf
[i
]&0xc0) != 0x80)
2599 char_len
= get_utf8_char_len(readbuf
[i
]);
2600 if(char_len
+i
<= pos
)
2603 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2605 fdinfo
->lookahead
[0] = readbuf
[i
];
2607 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2609 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2611 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2615 for(i
=0, j
=0; i
<pos
; i
++) {
2616 if(readbuf
[i
] == 0x1a) {
2617 fdinfo
->wxflag
|= WX_ATEOF
;
2621 /* strip '\r' if followed by '\n' */
2622 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2623 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2624 readbuf
[j
++] = '\r';
2625 }else if(lookahead
== '\n' && j
==0) {
2626 readbuf
[j
++] = '\n';
2628 if(lookahead
!= '\n')
2629 readbuf
[j
++] = '\r';
2631 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2632 fdinfo
->lookahead
[0] = lookahead
;
2634 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2636 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2637 readbuf
[j
++] = readbuf
[i
];
2642 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2643 msvcrt_set_errno(GetLastError());
2644 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2648 if (readbuf
!= min_buf
) MSVCRT_free(readbuf
);
2652 /*********************************************************************
2655 * When reading \r as last character in text mode, read() positions
2656 * the file pointer on the \r character while getc() goes on to
2659 static int read_i(int fd
, ioinfo
*fdinfo
, void *buf
, unsigned int count
)
2661 DWORD num_read
, utf16
;
2662 char *bufstart
= buf
;
2667 if (fdinfo
->wxflag
& WX_ATEOF
) {
2668 TRACE("already at EOF, returning 0\n");
2671 /* Don't trace small reads, it gets *very* annoying */
2673 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd
, fdinfo
->handle
, buf
, count
);
2674 if (fdinfo
->handle
== INVALID_HANDLE_VALUE
)
2676 *MSVCRT__errno() = MSVCRT_EBADF
;
2680 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2681 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2683 *MSVCRT__errno() = MSVCRT_EINVAL
;
2687 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2688 return read_utf8(fdinfo
, buf
, count
);
2690 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(fdinfo
->handle
, bufstart
, count
, &num_read
, NULL
))
2692 if (fdinfo
->lookahead
[0] != '\n')
2694 bufstart
[0] = fdinfo
->lookahead
[0];
2695 fdinfo
->lookahead
[0] = '\n';
2699 bufstart
[1] = fdinfo
->lookahead
[1];
2700 fdinfo
->lookahead
[1] = '\n';
2703 if(count
>1+utf16
&& ReadFile(fdinfo
->handle
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2704 num_read
+= 1+utf16
;
2709 if(utf16
&& (num_read
&1))
2711 /* msvcr90 uses uninitialized value from the buffer in this case */
2712 /* msvcrt ignores additional data */
2713 ERR("got odd number of bytes in UTF16 mode\n");
2717 if (count
!= 0 && num_read
== 0)
2719 fdinfo
->wxflag
|= WX_ATEOF
;
2720 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2722 else if (fdinfo
->wxflag
& WX_TEXT
)
2726 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2727 fdinfo
->wxflag
|= WX_READNL
;
2729 fdinfo
->wxflag
&= ~WX_READNL
;
2731 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2733 /* in text mode, a ctrl-z signals EOF */
2734 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2736 fdinfo
->wxflag
|= WX_ATEOF
;
2737 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2741 /* in text mode, strip \r if followed by \n */
2742 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2747 lookahead
[1] = '\n';
2748 if (ReadFile(fdinfo
->handle
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2750 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2752 bufstart
[j
++] = '\n';
2753 if(utf16
) bufstart
[j
++] = 0;
2757 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2759 bufstart
[j
++] = '\r';
2760 if(utf16
) bufstart
[j
++] = 0;
2763 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2765 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2767 bufstart
[j
++] = '\n';
2768 if (utf16
) bufstart
[j
++] = 0;
2772 fdinfo
->lookahead
[0] = lookahead
[0];
2773 fdinfo
->lookahead
[1] = lookahead
[1];
2777 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2782 bufstart
[j
++] = '\r';
2783 if(utf16
) bufstart
[j
++] = 0;
2786 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2787 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2789 bufstart
[j
++] = bufstart
[i
];
2790 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2798 if (GetLastError() == ERROR_BROKEN_PIPE
)
2800 TRACE(":end-of-pipe\n");
2801 fdinfo
->wxflag
|= WX_ATEOF
;
2806 TRACE(":failed-last error (%d)\n",GetLastError());
2812 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2816 /*********************************************************************
2819 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2821 ioinfo
*info
= get_ioinfo(fd
);
2822 int num_read
= read_i(fd
, info
, buf
, count
);
2823 release_ioinfo(info
);
2827 /*********************************************************************
2828 * _setmode (MSVCRT.@)
2830 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2832 ioinfo
*info
= get_ioinfo(fd
);
2833 int ret
= info
->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2834 if(ret
==MSVCRT__O_TEXT
&& (info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2835 ret
= MSVCRT__O_WTEXT
;
2837 if(mode
!=MSVCRT__O_TEXT
&& mode
!=MSVCRT__O_BINARY
&& mode
!=MSVCRT__O_WTEXT
2838 && mode
!=MSVCRT__O_U16TEXT
&& mode
!=MSVCRT__O_U8TEXT
) {
2839 *MSVCRT__errno() = MSVCRT_EINVAL
;
2840 release_ioinfo(info
);
2844 if(info
== &MSVCRT___badioinfo
) {
2845 *MSVCRT__errno() = MSVCRT_EBADF
;
2849 if(mode
== MSVCRT__O_BINARY
) {
2850 info
->wxflag
&= ~WX_TEXT
;
2851 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2852 release_ioinfo(info
);
2856 info
->wxflag
|= WX_TEXT
;
2857 if(mode
== MSVCRT__O_TEXT
)
2858 info
->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2859 else if(mode
== MSVCRT__O_U8TEXT
)
2860 info
->exflag
= (info
->exflag
& ~EF_UTF16
) | EF_UTF8
;
2862 info
->exflag
= (info
->exflag
& ~EF_UTF8
) | EF_UTF16
;
2864 release_ioinfo(info
);
2868 /*********************************************************************
2869 * _stat64 (MSVCRT.@)
2871 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2874 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2875 unsigned short mode
= ALL_S_IREAD
;
2878 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2880 plen
= strlen(path
);
2881 while (plen
&& path
[plen
-1]==' ')
2884 if (plen
&& (plen
<2 || path
[plen
-2]!=':') &&
2885 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2887 *MSVCRT__errno() = MSVCRT_ENOENT
;
2891 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2893 TRACE("failed (%d)\n",GetLastError());
2894 *MSVCRT__errno() = MSVCRT_ENOENT
;
2898 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2900 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2901 Bon 011120: This FIXME seems incorrect
2902 Also a letter as first char isn't enough to be classified
2905 if (isalpha(*path
)&& (*(path
+1)==':'))
2906 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2908 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2910 /* Dir, or regular file? */
2911 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2912 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2915 mode
|= MSVCRT__S_IFREG
;
2917 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2919 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2920 (tolower(path
[plen
-3]) << 16);
2921 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2922 mode
|= ALL_S_IEXEC
;
2926 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2927 mode
|= ALL_S_IWRITE
;
2929 buf
->st_mode
= mode
;
2931 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2932 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2934 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2935 buf
->st_mtime
= buf
->st_ctime
= dw
;
2936 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2937 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2938 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2942 /*********************************************************************
2943 * _stati64 (MSVCRT.@)
2945 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2948 struct MSVCRT__stat64 buf64
;
2950 ret
= MSVCRT_stat64(path
, &buf64
);
2952 msvcrt_stat64_to_stati64(&buf64
, buf
);
2956 /*********************************************************************
2959 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2962 struct MSVCRT__stat64 buf64
;
2964 ret
= MSVCRT_stat64( path
, &buf64
);
2966 msvcrt_stat64_to_stat(&buf64
, buf
);
2970 /*********************************************************************
2971 * _stat32 (MSVCR100.@)
2973 int CDECL
MSVCRT__stat32(const char *path
, struct MSVCRT__stat32
*buf
)
2976 struct MSVCRT__stat64 buf64
;
2978 ret
= MSVCRT_stat64(path
, &buf64
);
2980 msvcrt_stat64_to_stat32(&buf64
, buf
);
2984 /*********************************************************************
2985 * _stat32i64 (MSVCR100.@)
2987 int CDECL
MSVCRT__stat32i64(const char *path
, struct MSVCRT__stat32i64
*buf
)
2990 struct MSVCRT__stat64 buf64
;
2992 ret
= MSVCRT_stat64(path
, &buf64
);
2994 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
2998 /*********************************************************************
2999 * _stat64i32 (MSVCR100.@)
3001 int CDECL
MSVCRT__stat64i32(const char* path
, struct MSVCRT__stat64i32
*buf
)
3004 struct MSVCRT__stat64 buf64
;
3006 ret
= MSVCRT_stat64(path
, &buf64
);
3008 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3012 /*********************************************************************
3013 * _wstat64 (MSVCRT.@)
3015 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
3018 WIN32_FILE_ATTRIBUTE_DATA hfi
;
3019 unsigned short mode
= ALL_S_IREAD
;
3022 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
3024 plen
= strlenW(path
);
3025 while (plen
&& path
[plen
-1]==' ')
3028 if(plen
&& (plen
<2 || path
[plen
-2]!=':') &&
3029 (path
[plen
-1]==':' || path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
3031 *MSVCRT__errno() = MSVCRT_ENOENT
;
3035 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
3037 TRACE("failed (%d)\n",GetLastError());
3038 *MSVCRT__errno() = MSVCRT_ENOENT
;
3042 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
3044 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3045 if (MSVCRT_iswalpha(*path
))
3046 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
3048 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
3050 /* Dir, or regular file? */
3051 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
3052 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
3055 mode
|= MSVCRT__S_IFREG
;
3057 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
3059 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
3060 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
3061 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
3062 mode
|= ALL_S_IEXEC
;
3066 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
3067 mode
|= ALL_S_IWRITE
;
3069 buf
->st_mode
= mode
;
3071 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
3072 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
3074 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
3075 buf
->st_mtime
= buf
->st_ctime
= dw
;
3076 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
3077 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
3078 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
3082 /*********************************************************************
3083 * _wstati64 (MSVCRT.@)
3085 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
3088 struct MSVCRT__stat64 buf64
;
3090 ret
= MSVCRT__wstat64(path
, &buf64
);
3092 msvcrt_stat64_to_stati64(&buf64
, buf
);
3096 /*********************************************************************
3099 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
3102 struct MSVCRT__stat64 buf64
;
3104 ret
= MSVCRT__wstat64( path
, &buf64
);
3105 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
3109 /*********************************************************************
3110 * _wstat32 (MSVCR100.@)
3112 int CDECL
MSVCRT__wstat32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32
*buf
)
3115 struct MSVCRT__stat64 buf64
;
3117 ret
= MSVCRT__wstat64(path
, &buf64
);
3119 msvcrt_stat64_to_stat32(&buf64
, buf
);
3123 /*********************************************************************
3124 * _wstat32i64 (MSVCR100.@)
3126 int CDECL
MSVCRT__wstat32i64(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat32i64
*buf
)
3129 struct MSVCRT__stat64 buf64
;
3131 ret
= MSVCRT__wstat64(path
, &buf64
);
3133 msvcrt_stat64_to_stat32i64(&buf64
, buf
);
3137 /*********************************************************************
3138 * _wstat64i32 (MSVCR100.@)
3140 int CDECL
MSVCRT__wstat64i32(const MSVCRT_wchar_t
*path
, struct MSVCRT__stat64i32
*buf
)
3143 struct MSVCRT__stat64 buf64
;
3145 ret
= MSVCRT__wstat64(path
, &buf64
);
3147 msvcrt_stat64_to_stat64i32(&buf64
, buf
);
3151 /*********************************************************************
3154 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
3156 return MSVCRT__lseek(fd
, 0, MSVCRT_SEEK_CUR
);
3159 /*********************************************************************
3160 * _telli64 (MSVCRT.@)
3162 __int64 CDECL
_telli64(int fd
)
3164 return MSVCRT__lseeki64(fd
, 0, MSVCRT_SEEK_CUR
);
3167 /*********************************************************************
3168 * _tempnam (MSVCRT.@)
3170 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
3172 char tmpbuf
[MAX_PATH
];
3173 const char *tmp_dir
= MSVCRT_getenv("TMP");
3175 if (tmp_dir
) dir
= tmp_dir
;
3177 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
3178 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
3180 TRACE("got name (%s)\n",tmpbuf
);
3181 DeleteFileA(tmpbuf
);
3182 return MSVCRT__strdup(tmpbuf
);
3184 TRACE("failed (%d)\n",GetLastError());
3188 /*********************************************************************
3189 * _wtempnam (MSVCRT.@)
3191 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
3193 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
3194 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
3195 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
3197 if (tmp_dir
) dir
= tmp_dir
;
3199 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
3200 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
3202 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
3203 DeleteFileW(tmpbuf
);
3204 return MSVCRT__wcsdup(tmpbuf
);
3206 TRACE("failed (%d)\n",GetLastError());
3210 /*********************************************************************
3213 int CDECL
MSVCRT__umask(int umask
)
3215 int old_umask
= MSVCRT_umask
;
3216 TRACE("(%d)\n",umask
);
3217 MSVCRT_umask
= umask
;
3221 /*********************************************************************
3222 * _utime64 (MSVCRT.@)
3224 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
3226 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3230 int retVal
= _futime64(fd
, t
);
3237 /*********************************************************************
3238 * _utime32 (MSVCRT.@)
3240 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
3244 struct MSVCRT___utimbuf64 t64
;
3245 t64
.actime
= t
->actime
;
3246 t64
.modtime
= t
->modtime
;
3247 return _utime64( path
, &t64
);
3250 return _utime64( path
, NULL
);
3253 /*********************************************************************
3257 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
3259 return _utime64( path
, t
);
3262 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
3264 return _utime32( path
, t
);
3268 /*********************************************************************
3269 * _wutime64 (MSVCRT.@)
3271 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3273 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
3277 int retVal
= _futime64(fd
, t
);
3284 /*********************************************************************
3285 * _wutime32 (MSVCRT.@)
3287 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3291 struct MSVCRT___utimbuf64 t64
;
3292 t64
.actime
= t
->actime
;
3293 t64
.modtime
= t
->modtime
;
3294 return _wutime64( path
, &t64
);
3297 return _wutime64( path
, NULL
);
3300 /*********************************************************************
3301 * _wutime (MSVCRT.@)
3304 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
3306 return _wutime64( path
, t
);
3309 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
3311 return _wutime32( path
, t
);
3315 /*********************************************************************
3318 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
3321 ioinfo
*info
= get_ioinfo(fd
);
3322 HANDLE hand
= info
->handle
;
3324 /* Don't trace small writes, it gets *very* annoying */
3327 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
3329 if (hand
== INVALID_HANDLE_VALUE
)
3331 *MSVCRT__errno() = MSVCRT_EBADF
;
3332 release_ioinfo(info
);
3336 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
3338 *MSVCRT__errno() = MSVCRT_EINVAL
;
3339 release_ioinfo(info
);
3343 /* If appending, go to EOF */
3344 if (info
->wxflag
& WX_APPEND
)
3345 MSVCRT__lseek(fd
, 0, FILE_END
);
3347 if (!(info
->wxflag
& WX_TEXT
))
3349 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
3350 && (num_written
== count
))
3352 release_ioinfo(info
);
3355 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
3356 hand
, GetLastError());
3357 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3361 unsigned int i
, j
, nr_lf
, size
;
3364 const char *s
= buf
, *buf_start
= buf
;
3366 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
3368 /* find number of \n */
3369 for (nr_lf
=0, i
=0; i
<count
; i
++)
3375 if ((q
= p
= MSVCRT_malloc(size
)))
3377 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
3386 FIXME("Malloc failed\n");
3398 else if (info
->exflag
& EF_UTF16
)
3400 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
3401 if (s
[i
]=='\n' && s
[i
+1]==0)
3406 if ((q
= p
= MSVCRT_malloc(size
)))
3408 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3410 if (s
[i
]=='\n' && s
[i
+1]==0)
3421 FIXME("Malloc failed\n");
3437 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
3438 if (s
[i
]=='\n' && s
[i
+1]==0)
3441 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
3443 msvcrt_set_errno(GetLastError());
3445 release_ioinfo(info
);
3449 size
= conv_len
+nr_lf
;
3450 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
3452 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
3454 if (s
[i
]=='\n' && s
[i
+1]==0)
3462 q
= p
+count
+nr_lf
*2;
3463 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
3464 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
3468 FIXME("Malloc failed\n");
3475 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
3477 release_ioinfo(info
);
3480 if (num_written
!= size
)
3482 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
3483 fd
, hand
, GetLastError(), num_written
);
3484 *MSVCRT__errno() = MSVCRT_ENOSPC
;
3485 return s
- buf_start
;
3490 release_ioinfo(info
);
3494 /*********************************************************************
3497 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
3501 MSVCRT__lock_file(file
);
3502 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
3503 if (len
== sizeof(val
)) {
3504 MSVCRT__unlock_file(file
);
3508 file
->_flag
|= MSVCRT__IOERR
;
3509 MSVCRT__unlock_file(file
);
3513 /*********************************************************************
3516 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
3520 MSVCRT__lock_file(file
);
3521 ret
= MSVCRT__fclose_nolock(file
);
3522 MSVCRT__unlock_file(file
);
3527 /*********************************************************************
3528 * _fclose_nolock (MSVCRT.@)
3530 int CDECL
MSVCRT__fclose_nolock(MSVCRT_FILE
* file
)
3535 MSVCRT_free(file
->_tmpfname
);
3536 file
->_tmpfname
= NULL
;
3537 /* flush stdio buffers */
3538 if(file
->_flag
& MSVCRT__IOWRT
)
3539 MSVCRT__fflush_nolock(file
);
3540 if(file
->_flag
& MSVCRT__IOMYBUF
)
3541 MSVCRT_free(file
->_base
);
3543 r
=MSVCRT__close(file
->_file
);
3546 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
3549 /*********************************************************************
3552 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
3554 return file
->_flag
& MSVCRT__IOEOF
;
3557 /*********************************************************************
3560 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
3562 return file
->_flag
& MSVCRT__IOERR
;
3565 /*********************************************************************
3566 * _filbuf (MSVCRT.@)
3568 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
3572 if(file
->_flag
& MSVCRT__IOSTRG
)
3575 /* Allocate buffer if needed */
3576 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3577 msvcrt_alloc_buffer(file
);
3579 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3580 if(file
->_flag
& MSVCRT__IORW
)
3581 file
->_flag
|= MSVCRT__IOREAD
;
3586 if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3588 if ((r
= MSVCRT__read(file
->_file
,&c
,1)) != 1) {
3589 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3595 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3597 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
3603 file
->_ptr
= file
->_base
+1;
3604 c
= *(unsigned char *)file
->_base
;
3609 /*********************************************************************
3612 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
3616 MSVCRT__lock_file(file
);
3617 ret
= MSVCRT__fgetc_nolock(file
);
3618 MSVCRT__unlock_file(file
);
3623 /*********************************************************************
3624 * _fgetc_nolock (MSVCRT.@)
3626 int CDECL
MSVCRT__fgetc_nolock(MSVCRT_FILE
* file
)
3633 i
= (unsigned char *)file
->_ptr
++;
3636 j
= MSVCRT__filbuf(file
);
3641 /*********************************************************************
3642 * _fgetchar (MSVCRT.@)
3644 int CDECL
MSVCRT__fgetchar(void)
3646 return MSVCRT_fgetc(MSVCRT_stdin
);
3649 /*********************************************************************
3652 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
3654 int cc
= MSVCRT_EOF
;
3655 char * buf_start
= s
;
3657 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3658 file
,file
->_file
,s
,size
);
3660 MSVCRT__lock_file(file
);
3662 while ((size
>1) && (cc
= MSVCRT__fgetc_nolock(file
)) != MSVCRT_EOF
&& cc
!= '\n')
3667 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3669 TRACE(":nothing read\n");
3670 MSVCRT__unlock_file(file
);
3673 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
3676 TRACE(":got %s\n", debugstr_a(buf_start
));
3677 MSVCRT__unlock_file(file
);
3681 /*********************************************************************
3684 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
3688 MSVCRT__lock_file(file
);
3689 ret
= MSVCRT__fgetwc_nolock(file
);
3690 MSVCRT__unlock_file(file
);
3695 /*********************************************************************
3696 * _fgetwc_nolock (MSVCRT.@)
3698 MSVCRT_wint_t CDECL
MSVCRT__fgetwc_nolock(MSVCRT_FILE
* file
)
3703 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3704 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3707 for(p
=(char*)&ret
; (MSVCRT_wint_t
*)p
<&ret
+1; p
++) {
3708 ch
= MSVCRT__fgetc_nolock(file
);
3709 if(ch
== MSVCRT_EOF
) {
3716 char mbs
[MSVCRT_MB_LEN_MAX
];
3719 ch
= MSVCRT__fgetc_nolock(file
);
3720 if(ch
!= MSVCRT_EOF
) {
3722 if(MSVCRT_isleadbyte((unsigned char)mbs
[0])) {
3723 ch
= MSVCRT__fgetc_nolock(file
);
3724 if(ch
!= MSVCRT_EOF
) {
3733 if(!len
|| MSVCRT_mbtowc(&ret
, mbs
, len
)==-1)
3740 /*********************************************************************
3743 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
3750 MSVCRT__lock_file(file
);
3751 for (j
=0; j
<sizeof(int); j
++) {
3752 k
= MSVCRT__fgetc_nolock(file
);
3753 if (k
== MSVCRT_EOF
) {
3754 file
->_flag
|= MSVCRT__IOEOF
;
3755 MSVCRT__unlock_file(file
);
3761 MSVCRT__unlock_file(file
);
3765 /*********************************************************************
3768 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3770 return MSVCRT_fgetwc(file
);
3773 /*********************************************************************
3774 * _fgetwchar (MSVCRT.@)
3776 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3778 return MSVCRT_fgetwc(MSVCRT_stdin
);
3781 /*********************************************************************
3782 * getwchar (MSVCRT.@)
3784 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3786 return MSVCRT__fgetwchar();
3789 /*********************************************************************
3792 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3794 MSVCRT_wint_t cc
= MSVCRT_WEOF
;
3795 MSVCRT_wchar_t
* buf_start
= s
;
3797 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3798 file
,file
->_file
,s
,size
);
3800 MSVCRT__lock_file(file
);
3802 while ((size
>1) && (cc
= MSVCRT__fgetwc_nolock(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3807 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3809 TRACE(":nothing read\n");
3810 MSVCRT__unlock_file(file
);
3813 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3816 TRACE(":got %s\n", debugstr_w(buf_start
));
3817 MSVCRT__unlock_file(file
);
3821 /*********************************************************************
3822 * _flsbuf (MSVCRT.@)
3824 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3826 /* Flush output buffer */
3827 if(!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
3828 msvcrt_alloc_buffer(file
);
3831 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3832 if(!(file
->_flag
& MSVCRT__IORW
)) {
3833 file
->_flag
|= MSVCRT__IOERR
;
3836 file
->_flag
|= MSVCRT__IOWRT
;
3838 if(file
->_flag
& MSVCRT__IOREAD
) {
3839 if(!(file
->_flag
& MSVCRT__IOEOF
)) {
3840 file
->_flag
|= MSVCRT__IOERR
;
3844 file
->_ptr
= file
->_base
;
3845 file
->_flag
&= ~(MSVCRT__IOREAD
| MSVCRT__IOEOF
);
3848 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
3851 if(file
->_cnt
<= 0) {
3852 res
= msvcrt_flush_buffer(file
);
3855 file
->_flag
|= MSVCRT__IOWRT
;
3856 file
->_cnt
=file
->_bufsiz
;
3864 /* set _cnt to 0 for unbuffered FILEs */
3866 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3869 file
->_flag
|= MSVCRT__IOERR
;
3874 /*********************************************************************
3877 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3881 MSVCRT__lock_file(file
);
3882 ret
= MSVCRT__fwrite_nolock(ptr
, size
, nmemb
, file
);
3883 MSVCRT__unlock_file(file
);
3888 /*********************************************************************
3889 * _fwrite_nolock (MSVCRT.@)
3891 MSVCRT_size_t CDECL
MSVCRT__fwrite_nolock(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3893 MSVCRT_size_t wrcnt
=size
* nmemb
;
3899 if(file
->_cnt
< 0) {
3900 WARN("negative file->_cnt value in %p\n", file
);
3901 file
->_flag
|= MSVCRT__IOERR
;
3903 } else if(file
->_cnt
) {
3904 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3905 memcpy(file
->_ptr
, ptr
, pcnt
);
3910 ptr
= (const char*)ptr
+ pcnt
;
3911 } else if((file
->_flag
& MSVCRT__IONBF
)
3912 || ((file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3913 || (!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3917 if(file
->_flag
& MSVCRT__IONBF
)
3919 else if(!(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
3920 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3922 bufsiz
= file
->_bufsiz
;
3924 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3926 if(msvcrt_flush_buffer(file
) == MSVCRT_EOF
)
3929 if(MSVCRT__write(file
->_file
, ptr
, pcnt
) <= 0) {
3930 file
->_flag
|= MSVCRT__IOERR
;
3935 ptr
= (const char*)ptr
+ pcnt
;
3937 if(MSVCRT__flsbuf(*(const char*)ptr
, file
) == MSVCRT_EOF
)
3941 ptr
= (const char*)ptr
+ 1;
3945 return written
/ size
;
3948 /*********************************************************************
3951 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3955 MSVCRT__lock_file(file
);
3956 ret
= MSVCRT__fputwc_nolock(wc
, file
);
3957 MSVCRT__unlock_file(file
);
3962 /*********************************************************************
3963 * _fputwc_nolock (MSVCRT.@)
3965 MSVCRT_wint_t CDECL
MSVCRT__fputwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3967 MSVCRT_wchar_t mwc
=wc
;
3971 fdinfo
= get_ioinfo_nolock(file
->_file
);
3973 if((fdinfo
->wxflag
&WX_TEXT
) && !(fdinfo
->exflag
&(EF_UTF8
|EF_UTF16
))) {
3974 char buf
[MSVCRT_MB_LEN_MAX
];
3977 char_len
= MSVCRT_wctomb(buf
, mwc
);
3978 if(char_len
!=-1 && MSVCRT__fwrite_nolock(buf
, char_len
, 1, file
)==1)
3982 }else if(MSVCRT__fwrite_nolock(&mwc
, sizeof(mwc
), 1, file
) == 1) {
3991 /*********************************************************************
3992 * _fputwchar (MSVCRT.@)
3994 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3996 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3999 /*********************************************************************
4000 * _wfsopen (MSVCRT.@)
4002 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
4005 int open_flags
, stream_flags
, fd
;
4007 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
4009 /* map mode string to open() flags. "man fopen" for possibilities. */
4010 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4014 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4017 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
4019 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
4026 TRACE(":got (%p)\n",file
);
4027 if (fd
>= 0 && !file
)
4033 /*********************************************************************
4034 * _fsopen (MSVCRT.@)
4036 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
4039 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4041 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
4042 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4043 *MSVCRT__errno() = MSVCRT_EINVAL
;
4046 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4049 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
4050 *MSVCRT__errno() = MSVCRT_EINVAL
;
4054 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
4061 /*********************************************************************
4064 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
4066 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4069 /*********************************************************************
4070 * fopen_s (MSVCRT.@)
4072 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
4073 const char *filename
, const char *mode
)
4075 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4076 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4077 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4079 *pFile
= MSVCRT_fopen(filename
, mode
);
4082 return *MSVCRT__errno();
4086 /*********************************************************************
4087 * _wfopen (MSVCRT.@)
4089 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
4091 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
4094 /*********************************************************************
4095 * _wfopen_s (MSVCRT.@)
4097 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
4098 const MSVCRT_wchar_t
*mode
)
4100 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4101 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
4102 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4104 *pFile
= MSVCRT__wfopen(filename
, mode
);
4107 return *MSVCRT__errno();
4111 /*********************************************************************
4114 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
4118 MSVCRT__lock_file(file
);
4119 ret
= MSVCRT__fputc_nolock(c
, file
);
4120 MSVCRT__unlock_file(file
);
4125 /*********************************************************************
4126 * _fputc_nolock (MSVCRT.@)
4128 int CDECL
MSVCRT__fputc_nolock(int c
, MSVCRT_FILE
* file
)
4137 res
= msvcrt_flush_buffer(file
);
4138 return res
? res
: c
;
4144 res
= MSVCRT__flsbuf(c
, file
);
4149 /*********************************************************************
4150 * _fputchar (MSVCRT.@)
4152 int CDECL
MSVCRT__fputchar(int c
)
4154 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4157 /*********************************************************************
4160 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4164 MSVCRT__lock_file(file
);
4165 ret
= MSVCRT__fread_nolock(ptr
, size
, nmemb
, file
);
4166 MSVCRT__unlock_file(file
);
4171 /*********************************************************************
4172 * _fread_nolock (MSVCRT.@)
4174 MSVCRT_size_t CDECL
MSVCRT__fread_nolock(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
4176 MSVCRT_size_t rcnt
=size
* nmemb
;
4177 MSVCRT_size_t read
=0;
4178 MSVCRT_size_t pread
=0;
4183 /* first buffered data */
4185 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
4186 memcpy(ptr
, file
->_ptr
, pcnt
);
4191 ptr
= (char*)ptr
+ pcnt
;
4192 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
4193 if(file
->_flag
& MSVCRT__IORW
) {
4194 file
->_flag
|= MSVCRT__IOREAD
;
4200 if(rcnt
>0 && !(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)))
4201 msvcrt_alloc_buffer(file
);
4206 if (!file
->_cnt
&& rcnt
<MSVCRT_BUFSIZ
&& (file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))) {
4207 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
4208 file
->_ptr
= file
->_base
;
4209 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
4210 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4211 if (i
> 0 && i
< file
->_cnt
) {
4212 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
4213 file
->_flag
&= ~MSVCRT__IOEOF
;
4216 memcpy(ptr
, file
->_ptr
, i
);
4220 } else if (rcnt
> INT_MAX
) {
4221 i
= MSVCRT__read(file
->_file
, ptr
, INT_MAX
);
4222 } else if (rcnt
< MSVCRT_BUFSIZ
) {
4223 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
);
4225 i
= MSVCRT__read(file
->_file
, ptr
, rcnt
- MSVCRT_BUFSIZ
/2);
4229 ptr
= (char *)ptr
+i
;
4230 /* expose feof condition in the flags
4231 * MFC tests file->_flag for feof, and doesn't call feof())
4233 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
4234 file
->_flag
|= MSVCRT__IOEOF
;
4237 file
->_flag
|= MSVCRT__IOERR
;
4248 /*********************************************************************
4249 * fread_s (MSVCR80.@)
4251 MSVCRT_size_t CDECL
MSVCRT_fread_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4252 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4256 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4258 memset(buf
, 0, buf_size
);
4261 if(!elem_size
|| !count
) return 0;
4263 MSVCRT__lock_file(stream
);
4264 ret
= MSVCRT__fread_nolock_s(buf
, buf_size
, elem_size
, count
, stream
);
4265 MSVCRT__unlock_file(stream
);
4270 /*********************************************************************
4271 * _fread_nolock_s (MSVCR80.@)
4273 MSVCRT_size_t CDECL
MSVCRT__fread_nolock_s(void *buf
, MSVCRT_size_t buf_size
, MSVCRT_size_t elem_size
,
4274 MSVCRT_size_t count
, MSVCRT_FILE
*stream
)
4276 size_t bytes_left
, buf_pos
;
4278 TRACE("(%p %lu %lu %lu %p)\n", buf
, buf_size
, elem_size
, count
, stream
);
4280 if(!MSVCRT_CHECK_PMT(stream
!= NULL
)) {
4282 memset(buf
, 0, buf_size
);
4285 if(!elem_size
|| !count
) return 0;
4286 if(!MSVCRT_CHECK_PMT(buf
!= NULL
)) return 0;
4287 if(!MSVCRT_CHECK_PMT(MSVCRT_SIZE_MAX
/count
>= elem_size
)) return 0;
4289 bytes_left
= elem_size
*count
;
4292 if(stream
->_cnt
> 0) {
4293 size_t size
= bytes_left
<stream
->_cnt
? bytes_left
: stream
->_cnt
;
4295 if(!MSVCRT_CHECK_PMT_ERR(size
<= buf_size
-buf_pos
, MSVCRT_ERANGE
)) {
4296 memset(buf
, 0, buf_size
);
4300 MSVCRT__fread_nolock((char*)buf
+buf_pos
, 1, size
, stream
);
4304 int c
= MSVCRT__filbuf(stream
);
4309 if(!MSVCRT_CHECK_PMT_ERR(buf_size
!= buf_pos
, MSVCRT_ERANGE
)) {
4310 memset(buf
, 0, buf_size
);
4314 ((char*)buf
)[buf_pos
++] = c
;
4319 return buf_pos
/elem_size
;
4322 /*********************************************************************
4323 * _wfreopen (MSVCRT.@)
4326 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4328 int open_flags
, stream_flags
, fd
;
4330 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
4333 if (!file
|| ((fd
= file
->_file
) < 0))
4337 MSVCRT_fclose(file
);
4338 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
4340 else if((fd
= MSVCRT__wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
)) < 0)
4342 else if(msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
4352 /*********************************************************************
4353 * _wfreopen_s (MSVCRT.@)
4355 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
4356 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
4358 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4359 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4360 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4361 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4363 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
4366 return *MSVCRT__errno();
4370 /*********************************************************************
4371 * freopen (MSVCRT.@)
4374 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4377 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
4379 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
4380 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
4386 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
4393 /*********************************************************************
4394 * freopen_s (MSVCRT.@)
4396 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
4397 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
4399 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
4400 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
4401 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
4402 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4404 *pFile
= MSVCRT_freopen(path
, mode
, file
);
4407 return *MSVCRT__errno();
4411 /*********************************************************************
4412 * fsetpos (MSVCRT.@)
4414 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4418 MSVCRT__lock_file(file
);
4419 msvcrt_flush_buffer(file
);
4421 /* Reset direction of i/o */
4422 if(file
->_flag
& MSVCRT__IORW
) {
4423 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
4426 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,MSVCRT_SEEK_SET
) == -1) ? -1 : 0;
4427 MSVCRT__unlock_file(file
);
4431 /*********************************************************************
4432 * _ftelli64 (MSVCRT.@)
4434 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
4438 MSVCRT__lock_file(file
);
4439 ret
= MSVCRT__ftelli64_nolock(file
);
4440 MSVCRT__unlock_file(file
);
4445 /*********************************************************************
4446 * _ftelli64_nolock (MSVCRT.@)
4448 __int64 CDECL
MSVCRT__ftelli64_nolock(MSVCRT_FILE
* file
)
4452 pos
= _telli64(file
->_file
);
4455 if(file
->_flag
& (MSVCRT__IOMYBUF
| MSVCRT__USERBUF
)) {
4456 if(file
->_flag
& MSVCRT__IOWRT
) {
4457 pos
+= file
->_ptr
- file
->_base
;
4459 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4462 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4466 } else if(!file
->_cnt
) { /* nothing to do */
4467 } else if(MSVCRT__lseeki64(file
->_file
, 0, MSVCRT_SEEK_END
)==pos
) {
4471 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4472 for(i
=0; i
<file
->_cnt
; i
++)
4473 if(file
->_ptr
[i
] == '\n')
4479 if(MSVCRT__lseeki64(file
->_file
, pos
, MSVCRT_SEEK_SET
) != pos
)
4482 pos
-= file
->_bufsiz
;
4483 pos
+= file
->_ptr
- file
->_base
;
4485 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
4486 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
4489 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
4499 /*********************************************************************
4502 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
4504 return MSVCRT__ftelli64(file
);
4507 /*********************************************************************
4508 * _ftell_nolock (MSVCRT.@)
4510 LONG CDECL
MSVCRT__ftell_nolock(MSVCRT_FILE
* file
)
4512 return MSVCRT__ftelli64_nolock(file
);
4515 /*********************************************************************
4516 * fgetpos (MSVCRT.@)
4518 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
4520 *pos
= MSVCRT__ftelli64(file
);
4526 /*********************************************************************
4529 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
4531 MSVCRT_size_t len
= strlen(s
);
4534 MSVCRT__lock_file(file
);
4535 ret
= MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
4536 MSVCRT__unlock_file(file
);
4540 /*********************************************************************
4543 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
4545 MSVCRT_size_t i
, len
= strlenW(s
);
4549 MSVCRT__lock_file(file
);
4550 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
4551 ret
= MSVCRT__fwrite_nolock(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
4552 MSVCRT__unlock_file(file
);
4556 tmp_buf
= add_std_buffer(file
);
4557 for (i
=0; i
<len
; i
++) {
4558 if(MSVCRT__fputwc_nolock(s
[i
], file
) == MSVCRT_WEOF
) {
4559 if(tmp_buf
) remove_std_buffer(file
);
4560 MSVCRT__unlock_file(file
);
4565 if(tmp_buf
) remove_std_buffer(file
);
4566 MSVCRT__unlock_file(file
);
4570 /*********************************************************************
4571 * getchar (MSVCRT.@)
4573 int CDECL
MSVCRT_getchar(void)
4575 return MSVCRT_fgetc(MSVCRT_stdin
);
4578 /*********************************************************************
4581 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
4583 return MSVCRT_fgetc(file
);
4586 /*********************************************************************
4589 char * CDECL
MSVCRT_gets(char *buf
)
4592 char * buf_start
= buf
;
4594 MSVCRT__lock_file(MSVCRT_stdin
);
4595 for(cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
4596 cc
= MSVCRT__fgetc_nolock(MSVCRT_stdin
))
4597 if(cc
!= '\r') *buf
++ = (char)cc
;
4601 TRACE("got '%s'\n", buf_start
);
4602 MSVCRT__unlock_file(MSVCRT_stdin
);
4606 /*********************************************************************
4609 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
4612 MSVCRT_wchar_t
* ws
= buf
;
4614 MSVCRT__lock_file(MSVCRT_stdin
);
4615 for (cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
4616 cc
= MSVCRT__fgetwc_nolock(MSVCRT_stdin
))
4619 *buf
++ = (MSVCRT_wchar_t
)cc
;
4623 TRACE("got %s\n", debugstr_w(ws
));
4624 MSVCRT__unlock_file(MSVCRT_stdin
);
4628 /*********************************************************************
4631 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
4633 return MSVCRT_fputc(c
, file
);
4636 /*********************************************************************
4637 * putchar (MSVCRT.@)
4639 int CDECL
MSVCRT_putchar(int c
)
4641 return MSVCRT_fputc(c
, MSVCRT_stdout
);
4644 /*********************************************************************
4647 int CDECL
MSVCRT_puts(const char *s
)
4649 MSVCRT_size_t len
= strlen(s
);
4652 MSVCRT__lock_file(MSVCRT_stdout
);
4653 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4654 MSVCRT__unlock_file(MSVCRT_stdout
);
4658 ret
= MSVCRT__fwrite_nolock("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4659 MSVCRT__unlock_file(MSVCRT_stdout
);
4663 /*********************************************************************
4666 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
4668 static const MSVCRT_wchar_t nl
= '\n';
4669 MSVCRT_size_t len
= strlenW(s
);
4672 MSVCRT__lock_file(MSVCRT_stdout
);
4673 if(MSVCRT__fwrite_nolock(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
4674 MSVCRT__unlock_file(MSVCRT_stdout
);
4678 ret
= MSVCRT__fwrite_nolock(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
4679 MSVCRT__unlock_file(MSVCRT_stdout
);
4683 /*********************************************************************
4686 int CDECL
MSVCRT_remove(const char *path
)
4688 TRACE("(%s)\n",path
);
4689 if (DeleteFileA(path
))
4691 TRACE(":failed (%d)\n",GetLastError());
4692 msvcrt_set_errno(GetLastError());
4696 /*********************************************************************
4697 * _wremove (MSVCRT.@)
4699 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
4701 TRACE("(%s)\n",debugstr_w(path
));
4702 if (DeleteFileW(path
))
4704 TRACE(":failed (%d)\n",GetLastError());
4705 msvcrt_set_errno(GetLastError());
4709 /*********************************************************************
4712 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
4714 TRACE(":from %s to %s\n",oldpath
,newpath
);
4715 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4717 TRACE(":failed (%d)\n",GetLastError());
4718 msvcrt_set_errno(GetLastError());
4722 /*********************************************************************
4723 * _wrename (MSVCRT.@)
4725 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
4727 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
4728 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
4730 TRACE(":failed (%d)\n",GetLastError());
4731 msvcrt_set_errno(GetLastError());
4735 /*********************************************************************
4736 * setvbuf (MSVCRT.@)
4738 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
4740 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4741 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| mode
==MSVCRT__IOFBF
|| mode
==MSVCRT__IOLBF
)) return -1;
4742 if(!MSVCRT_CHECK_PMT(mode
==MSVCRT__IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
4744 MSVCRT__lock_file(file
);
4746 MSVCRT__fflush_nolock(file
);
4747 if(file
->_flag
& MSVCRT__IOMYBUF
)
4748 MSVCRT_free(file
->_base
);
4749 file
->_flag
&= ~(MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
);
4752 if(mode
== MSVCRT__IONBF
) {
4753 file
->_flag
|= MSVCRT__IONBF
;
4754 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
4757 file
->_base
= file
->_ptr
= buf
;
4758 file
->_flag
|= MSVCRT__USERBUF
;
4759 file
->_bufsiz
= size
;
4761 file
->_base
= file
->_ptr
= MSVCRT_malloc(size
);
4764 MSVCRT__unlock_file(file
);
4768 file
->_flag
|= MSVCRT__IOMYBUF
;
4769 file
->_bufsiz
= size
;
4771 MSVCRT__unlock_file(file
);
4775 /*********************************************************************
4778 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
4780 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
4783 /*********************************************************************
4786 char * CDECL
MSVCRT_tmpnam(char *s
)
4793 thread_data_t
*data
= msvcrt_get_thread_data();
4795 if(!data
->tmpnam_buffer
)
4796 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
4798 s
= data
->tmpnam_buffer
;
4801 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
4802 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
4803 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4805 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
4806 memcpy(p
, tmpstr
, size
);
4808 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
4809 GetLastError() == ERROR_FILE_NOT_FOUND
)
4815 /*********************************************************************
4816 * _wtmpnam (MSVCRT.@)
4818 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
4820 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
4821 MSVCRT_wchar_t tmpstr
[16];
4825 thread_data_t
*data
= msvcrt_get_thread_data();
4827 if(!data
->wtmpnam_buffer
)
4828 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
4830 s
= data
->wtmpnam_buffer
;
4833 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
4834 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
4835 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
4837 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
4838 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
4840 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
4841 GetLastError() == ERROR_FILE_NOT_FOUND
)
4847 /*********************************************************************
4848 * tmpfile (MSVCRT.@)
4850 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
4852 char *filename
= MSVCRT_tmpnam(NULL
);
4854 MSVCRT_FILE
* file
= NULL
;
4857 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
4858 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
4859 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
4861 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
4866 else file
->_tmpfname
= MSVCRT__strdup(filename
);
4869 if(fd
!= -1 && !file
)
4875 /*********************************************************************
4876 * tmpfile_s (MSVCRT.@)
4878 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
4880 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
4882 *file
= MSVCRT_tmpfile();
4886 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
4888 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
4891 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
4895 MSVCRT__lock_file(file
);
4897 if(!(get_ioinfo_nolock(((MSVCRT_FILE
*)file
)->_file
)->wxflag
& WX_TEXT
)) {
4898 ret
= MSVCRT__fwrite_nolock(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
4899 MSVCRT__unlock_file(file
);
4903 for(i
=0; i
<len
; i
++) {
4904 if(MSVCRT__fputwc_nolock(str
[i
], file
) == MSVCRT_WEOF
) {
4905 MSVCRT__unlock_file(file
);
4910 MSVCRT__unlock_file(file
);
4914 /*********************************************************************
4915 * vfprintf (MSVCRT.@)
4917 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4922 MSVCRT__lock_file(file
);
4923 tmp_buf
= add_std_buffer(file
);
4924 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, 0, arg_clbk_valist
, NULL
, &valist
);
4925 if(tmp_buf
) remove_std_buffer(file
);
4926 MSVCRT__unlock_file(file
);
4931 /*********************************************************************
4932 * vfprintf_s (MSVCRT.@)
4934 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
4939 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
4941 MSVCRT__lock_file(file
);
4942 tmp_buf
= add_std_buffer(file
);
4943 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
,
4944 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, arg_clbk_valist
, NULL
, &valist
);
4945 if(tmp_buf
) remove_std_buffer(file
);
4946 MSVCRT__unlock_file(file
);
4951 /*********************************************************************
4952 * vfwprintf (MSVCRT.@)
4954 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4959 MSVCRT__lock_file(file
);
4960 tmp_buf
= add_std_buffer(file
);
4961 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, 0, arg_clbk_valist
, NULL
, &valist
);
4962 if(tmp_buf
) remove_std_buffer(file
);
4963 MSVCRT__unlock_file(file
);
4968 /*********************************************************************
4969 * vfwprintf_s (MSVCRT.@)
4971 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4976 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4978 MSVCRT__lock_file(file
);
4979 tmp_buf
= add_std_buffer(file
);
4980 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
,
4981 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
, arg_clbk_valist
, NULL
, &valist
);
4982 if(tmp_buf
) remove_std_buffer(file
);
4983 MSVCRT__unlock_file(file
);
4988 /*********************************************************************
4989 * __stdio_common_vfprintf (MSVCRT.@)
4991 int CDECL
MSVCRT__stdio_common_vfprintf(unsigned __int64 options
, MSVCRT_FILE
*file
, const char *format
,
4992 MSVCRT__locale_t locale
, __ms_va_list valist
)
4997 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4999 MSVCRT__lock_file(file
);
5000 tmp_buf
= add_std_buffer(file
);
5002 if (options
& ~UCRTBASE_PRINTF_MASK
)
5003 FIXME("options %s not handled\n", wine_dbgstr_longlong(options
));
5004 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, locale
,
5005 options
& UCRTBASE_PRINTF_MASK
, arg_clbk_valist
, NULL
, &valist
);
5006 if(tmp_buf
) remove_std_buffer(file
);
5007 MSVCRT__unlock_file(file
);
5012 /*********************************************************************
5013 * _vfwprintf_l (MSVCRT.@)
5015 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
5016 MSVCRT__locale_t locale
, __ms_va_list valist
)
5021 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
5023 MSVCRT__lock_file(file
);
5024 tmp_buf
= add_std_buffer(file
);
5025 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, 0, arg_clbk_valist
, NULL
, &valist
);
5026 if(tmp_buf
) remove_std_buffer(file
);
5027 MSVCRT__unlock_file(file
);
5032 /*********************************************************************
5033 * vprintf (MSVCRT.@)
5035 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
5037 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
5040 /*********************************************************************
5041 * vprintf_s (MSVCRT.@)
5043 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
5045 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
5048 /*********************************************************************
5049 * vwprintf (MSVCRT.@)
5051 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5053 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
5056 /*********************************************************************
5057 * vwprintf_s (MSVCRT.@)
5059 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
5061 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
5064 /*********************************************************************
5065 * fprintf (MSVCRT.@)
5067 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
5069 __ms_va_list valist
;
5071 __ms_va_start(valist
, format
);
5072 res
= MSVCRT_vfprintf(file
, format
, valist
);
5073 __ms_va_end(valist
);
5077 /*********************************************************************
5078 * fprintf_s (MSVCRT.@)
5080 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
5082 __ms_va_list valist
;
5084 __ms_va_start(valist
, format
);
5085 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
5086 __ms_va_end(valist
);
5090 /*********************************************************************
5091 * fwprintf (MSVCRT.@)
5093 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5095 __ms_va_list valist
;
5097 __ms_va_start(valist
, format
);
5098 res
= MSVCRT_vfwprintf(file
, format
, valist
);
5099 __ms_va_end(valist
);
5103 /*********************************************************************
5104 * fwprintf_s (MSVCRT.@)
5106 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
5108 __ms_va_list valist
;
5110 __ms_va_start(valist
, format
);
5111 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
5112 __ms_va_end(valist
);
5116 /*********************************************************************
5117 * _fwprintf_l (MSVCRT.@)
5119 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
5121 __ms_va_list valist
;
5123 __ms_va_start(valist
, locale
);
5124 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
5125 __ms_va_end(valist
);
5129 /*********************************************************************
5132 int CDECL
MSVCRT_printf(const char *format
, ...)
5134 __ms_va_list valist
;
5136 __ms_va_start(valist
, format
);
5137 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
5138 __ms_va_end(valist
);
5142 /*********************************************************************
5143 * printf_s (MSVCRT.@)
5145 int CDECL
MSVCRT_printf_s(const char *format
, ...)
5147 __ms_va_list valist
;
5149 __ms_va_start(valist
, format
);
5150 res
= MSVCRT_vprintf_s(format
, valist
);
5151 __ms_va_end(valist
);
5155 /*********************************************************************
5158 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
5162 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5164 MSVCRT__lock_file(file
);
5165 ret
= MSVCRT__ungetc_nolock(c
, file
);
5166 MSVCRT__unlock_file(file
);
5171 /*********************************************************************
5172 * _ungetc_nolock (MSVCRT.@)
5174 int CDECL
MSVCRT__ungetc_nolock(int c
, MSVCRT_FILE
* file
)
5176 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EOF
;
5178 if (c
== MSVCRT_EOF
|| !(file
->_flag
&MSVCRT__IOREAD
||
5179 (file
->_flag
&MSVCRT__IORW
&& !(file
->_flag
&MSVCRT__IOWRT
))))
5182 if((!(file
->_flag
& (MSVCRT__IONBF
| MSVCRT__IOMYBUF
| MSVCRT__USERBUF
))
5183 && msvcrt_alloc_buffer(file
))
5184 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
5187 if(file
->_ptr
>file
->_base
) {
5189 if(file
->_flag
& MSVCRT__IOSTRG
) {
5190 if(*file
->_ptr
!= c
) {
5198 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
5199 file
->_flag
|= MSVCRT__IOREAD
;
5206 /*********************************************************************
5207 * ungetwc (MSVCRT.@)
5209 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5213 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5215 MSVCRT__lock_file(file
);
5216 ret
= MSVCRT__ungetwc_nolock(wc
, file
);
5217 MSVCRT__unlock_file(file
);
5222 /*********************************************************************
5223 * _ungetwc_nolock (MSVCRT.@)
5225 MSVCRT_wint_t CDECL
MSVCRT__ungetwc_nolock(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
5227 MSVCRT_wchar_t mwc
= wc
;
5229 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_WEOF
;
5230 if (wc
== MSVCRT_WEOF
)
5233 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
5234 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
5235 unsigned char * pp
= (unsigned char *)&mwc
;
5238 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
5239 if(pp
[i
] != MSVCRT__ungetc_nolock(pp
[i
],file
))
5243 char mbs
[MSVCRT_MB_LEN_MAX
];
5246 len
= MSVCRT_wctomb(mbs
, mwc
);
5250 for(len
--; len
>=0; len
--) {
5251 if(mbs
[len
] != MSVCRT__ungetc_nolock(mbs
[len
], file
))
5259 /*********************************************************************
5260 * wprintf (MSVCRT.@)
5262 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
5264 __ms_va_list valist
;
5266 __ms_va_start(valist
, format
);
5267 res
= MSVCRT_vwprintf(format
, valist
);
5268 __ms_va_end(valist
);
5272 /*********************************************************************
5273 * wprintf_s (MSVCRT.@)
5275 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
5277 __ms_va_list valist
;
5279 __ms_va_start(valist
, format
);
5280 res
= MSVCRT_vwprintf_s(format
, valist
);
5281 __ms_va_end(valist
);
5285 /*********************************************************************
5286 * _getmaxstdio (MSVCRT.@)
5288 int CDECL
MSVCRT__getmaxstdio(void)
5290 return MSVCRT_max_streams
;
5293 /*********************************************************************
5294 * _setmaxstdio (MSVCRT.@)
5296 int CDECL
MSVCRT__setmaxstdio(int newmax
)
5298 TRACE("%d\n", newmax
);
5300 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
5303 MSVCRT_max_streams
= newmax
;
5304 return MSVCRT_max_streams
;
5307 /*********************************************************************
5308 * _get_stream_buffer_pointers (UCRTBASE.@)
5310 int CDECL
MSVCRT__get_stream_buffer_pointers(MSVCRT_FILE
*file
, char*** base
,
5311 char*** ptr
, int** count
)
5314 *base
= &file
->_base
;
5318 *count
= &file
->_cnt
;