2 * PROJECT: ReactOS CRT library
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: lib/sdk/crt/stdio/file.c
5 * PURPOSE: File CRT functions
6 * PROGRAMMERS: Wine team
7 * Ported to ReactOS by Aleksey Bragin (aleksey@reactos.org)
10 /*********************************************
11 * This file contains ReactOS changes!!
12 * Don't blindly sync it with Wine code!
14 * If you break Unicode output on the console again, please update this counter:
15 * int hours_wasted_on_this = 42;
16 *********************************************/
19 * msvcrt.dll file functions
21 * Copyright 1996,1998 Marcus Meissner
22 * Copyright 1996 Jukka Iivonen
23 * Copyright 1997,2000 Uwe Bonnes
24 * Copyright 2000 Jon Griffiths
25 * Copyright 2004 Eric Pouech
26 * Copyright 2004 Juan Lang
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Lesser General Public
30 * License as published by the Free Software Foundation; either
31 * version 2.1 of the License, or (at your option) any later version.
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Lesser General Public License for more details.
38 * You should have received a copy of the GNU Lesser General Public
39 * License along with this library; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
43 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
47 #include "wine/unicode.h"
49 #include <sys/utime.h>
52 int *__p__fmode(void);
53 int *__p___mb_cur_max(void);
58 #define _IOCOMMIT 0x4000
86 /* _access() bit flags FIXME: incomplete */
87 /* defined in crt/io.h */
89 /* values for wxflag in file descriptor */
92 #define WX_READNL 0x04 /* read started with \n */
93 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
95 #define WX_READCR 0x08 /* underlying file is at \r */
96 #define WX_DONTINHERIT 0x10
97 #define WX_APPEND 0x20
101 /* values for exflag - it's used differently in msvcr90.dll*/
103 #define EF_UTF16 0x02
104 #define EF_UNK_UNICODE 0x08
106 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
107 static char utf16_bom
[2] = { 0xff, 0xfe };
109 /* FIXME: this should be allocated dynamically */
110 #define MAX_FILES 2048
111 #define FD_BLOCK_SIZE 64
113 #define MSVCRT_INTERNAL_BUFSIZ 4096
115 /* ioinfo structure size is different in msvcrXX.dll's */
118 unsigned char wxflag
;
121 CRITICAL_SECTION crit
;
124 /*********************************************************************
125 * __pioinfo (MSVCRT.@)
126 * array of pointers to ioinfo arrays [64]
128 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
130 /*********************************************************************
131 * __badioinfo (MSVCRT.@)
133 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
135 static int fdstart
= 3; /* first unallocated fd */
136 static int fdend
= 3; /* highest allocated fd */
140 CRITICAL_SECTION crit
;
143 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
144 static file_crit
* MSVCRT_fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
145 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
147 /* INTERNAL: process umask */
148 static int MSVCRT_umask
= 0;
150 /* INTERNAL: static data for tmpnam and _wtmpname functions */
151 static int tmpnam_unique
;
153 /* This critical section protects the tables __pioinfo and fstreams,
154 * and their related indexes, fdstart, fdend,
155 * and MSVCRT_stream_idx, from race conditions.
156 * It doesn't protect against race conditions manipulating the underlying files
157 * or flags; doing so would probably be better accomplished with per-file
158 * protection, rather than locking the whole table for every change.
160 static CRITICAL_SECTION MSVCRT_file_cs
;
161 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
163 0, 0, &MSVCRT_file_cs
,
164 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
165 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
167 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
168 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
169 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
171 static inline ioinfo
* get_ioinfo_nolock(int fd
)
174 if(fd
>=0 && fd
<MAX_FILES
)
175 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
179 return ret
+ (fd
%FD_BLOCK_SIZE
);
182 static inline ioinfo
* get_ioinfo(int fd
)
186 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
190 return ret
+ (fd
%FD_BLOCK_SIZE
);
193 static inline FILE* msvcrt_get_file(int i
)
197 if(i
>= MSVCRT_max_streams
)
203 ret
= MSVCRT_fstream
[i
/FD_BLOCK_SIZE
];
205 MSVCRT_fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
206 if(!MSVCRT_fstream
[i
/FD_BLOCK_SIZE
]) {
207 ERR("out of memory\n");
212 ret
= MSVCRT_fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
214 ret
+= i
%FD_BLOCK_SIZE
;
219 static inline BOOL
is_valid_fd(int fd
)
221 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
224 /* INTERNAL: Get the HANDLE for a fd
225 * This doesn't lock the table, because a failure will result in
226 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
227 * it returns a valid handle which is about to be closed, a subsequent call
228 * will fail, most likely in a sane way.
230 /*static*/ HANDLE
fdtoh(int fd
)
232 if (!is_valid_fd(fd
))
234 WARN(":fd (%d) - no handle!\n",fd
);
237 return INVALID_HANDLE_VALUE
;
239 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
240 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
241 return get_ioinfo(fd
)->handle
;
244 /* INTERNAL: free a file entry fd */
245 static void msvcrt_free_fd(int fd
)
250 fdinfo
= get_ioinfo(fd
);
251 if(fdinfo
!= &__badioinfo
)
253 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
256 TRACE(":fd (%d) freed\n",fd
);
263 SetStdHandle(STD_INPUT_HANDLE
, 0);
266 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
269 SetStdHandle(STD_ERROR_HANDLE
, 0);
281 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
282 /* caller must hold the files lock */
283 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
289 WARN(":files exhausted!\n");
294 fdinfo
= get_ioinfo(fd
);
295 if(fdinfo
== &__badioinfo
) {
298 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
299 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
300 WARN(":out of memory!\n");
305 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
306 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
308 fdinfo
= get_ioinfo(fd
);
311 fdinfo
->handle
= hand
;
312 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
313 fdinfo
->lookahead
[0] = '\n';
314 fdinfo
->lookahead
[1] = '\n';
315 fdinfo
->lookahead
[2] = '\n';
318 /* locate next free slot */
319 if (fd
== fdstart
&& fd
== fdend
)
322 while (fdstart
< fdend
&&
323 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
325 /* update last fd in use */
328 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
332 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
333 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
334 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
340 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
341 /*static*/ int msvcrt_alloc_fd(HANDLE hand
, int flag
)
346 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
347 ret
= msvcrt_set_fd(hand
, flag
, fdstart
);
352 /* INTERNAL: Allocate a FILE* for an fd slot */
353 /* caller must hold the files lock */
354 static FILE* msvcrt_alloc_fp(void)
359 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
361 file
= msvcrt_get_file(i
);
365 if (file
->_flag
== 0)
367 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
375 /* INTERNAL: initialize a FILE* from an open fd */
376 static int msvcrt_init_fp(FILE* file
, int fd
, unsigned stream_flags
)
378 TRACE(":fd (%d) allocating FILE*\n",fd
);
379 if (!is_valid_fd(fd
))
381 WARN(":invalid fd %d\n",fd
);
386 file
->_ptr
= file
->_base
= NULL
;
389 file
->_flag
= stream_flags
;
390 file
->_tmpfname
= NULL
;
392 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
393 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
395 TRACE(":got FILE* (%p)\n",file
);
399 /* INTERNAL: Create an inheritance data block (for spawned process)
400 * The inheritance block is made of:
401 * 00 int nb of file descriptor (NBFD)
402 * 04 char file flags (wxflag): repeated for each fd
403 * 4+NBFD HANDLE file handle: repeated for each fd
405 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
412 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
413 *block
= calloc(1, *size
);
419 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
420 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
422 *(unsigned*)*block
= fdend
;
423 for (fd
= 0; fd
< fdend
; fd
++)
425 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
426 fdinfo
= get_ioinfo(fd
);
427 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
429 *wxflag_ptr
= fdinfo
->wxflag
;
430 *handle_ptr
= fdinfo
->handle
;
435 *handle_ptr
= INVALID_HANDLE_VALUE
;
437 wxflag_ptr
++; handle_ptr
++;
442 /* INTERNAL: Set up all file descriptors,
443 * as well as default streams (stdin, stderr and stdout)
445 void msvcrt_init_io(void)
451 GetStartupInfoA(&si
);
452 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
458 count
= *(unsigned*)si
.lpReserved2
;
459 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
460 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
462 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
463 count
= min(count
, MAX_FILES
);
464 for (i
= 0; i
< count
; i
++)
466 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
467 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
469 wxflag_ptr
++; handle_ptr
++;
471 fdend
= max( 3, count
);
472 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
473 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
476 fdinfo
= get_ioinfo(STDIN_FILENO
);
477 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
478 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
479 DWORD type
= GetFileType(h
);
481 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
482 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDIN_FILENO
);
485 fdinfo
= get_ioinfo(STDOUT_FILENO
);
486 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
487 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
488 DWORD type
= GetFileType(h
);
490 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
491 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDOUT_FILENO
);
494 fdinfo
= get_ioinfo(STDERR_FILENO
);
495 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
496 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
497 DWORD type
= GetFileType(h
);
499 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
500 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDERR_FILENO
);
503 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(STDIN_FILENO
)->handle
,
504 get_ioinfo(STDOUT_FILENO
)->handle
,
505 get_ioinfo(STDERR_FILENO
)->handle
);
507 memset(_iob
,0,3*sizeof(FILE));
508 for (i
= 0; i
< 3; i
++)
510 /* FILE structs for stdin/out/err are static and never deleted */
512 _iob
[i
]._tmpfname
= NULL
;
513 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
515 MSVCRT_stream_idx
= 3;
518 /* INTERNAL: Flush stdio file buffer */
519 static int msvcrt_flush_buffer(FILE* file
)
521 if((file
->_flag
& (_IOREAD
|_IOWRT
)) == _IOWRT
&&
522 file
->_flag
& (_IOMYBUF
|_USERBUF
)) {
523 int cnt
=file
->_ptr
-file
->_base
;
524 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
525 file
->_flag
|= _IOERR
;
529 if(file
->_flag
& _IORW
)
530 file
->_flag
&= ~_IOWRT
;
532 #ifdef __REACTOS__ /* CORE-11949 */
533 file
->_ptr
=file
->_base
;
538 #ifndef __REACTOS__ /* CORE-11949 */
539 file
->_ptr
=file
->_base
;
545 /*********************************************************************
548 int CDECL
_isatty(int fd
)
550 TRACE(":fd (%d)\n",fd
);
552 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
555 /* INTERNAL: Allocate stdio file buffer */
556 /*static*/ BOOL
alloc_buffer(FILE* file
)
558 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
559 && _isatty(file
->_file
))
562 file
->_base
= calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
564 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
565 file
->_flag
|= _IOMYBUF
;
567 file
->_base
= (char*)(&file
->_charbuf
);
569 file
->_flag
|= _IONBF
;
571 file
->_ptr
= file
->_base
;
576 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
577 static BOOL
add_std_buffer(FILE *file
)
579 static char buffers
[2][BUFSIZ
];
581 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
582 || (file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
583 || !_isatty(file
->_file
))
586 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
587 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
588 file
->_flag
|= _USERBUF
;
592 /* INTERNAL: Removes temporary buffer from stdout or stderr */
593 /* Only call this function when add_std_buffer returned TRUE */
594 static void remove_std_buffer(FILE *file
)
596 msvcrt_flush_buffer(file
);
597 file
->_ptr
= file
->_base
= NULL
;
598 file
->_bufsiz
= file
->_cnt
= 0;
599 file
->_flag
&= ~_USERBUF
;
602 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
603 static int msvcrt_int_to_base32(int num
, char *str
)
618 *p
= (num
& 31) + '0';
620 *p
+= ('a' - '0' - 10);
627 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
628 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
643 *p
= (num
& 31) + '0';
645 *p
+= ('a' - '0' - 10);
652 /* INTERNAL: Create a wide string from an ascii string */
653 wchar_t *msvcrt_wstrdupa(const char *str
)
655 const unsigned int len
= strlen(str
) + 1 ;
656 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
659 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
663 /*********************************************************************
664 * __iob_func(MSVCRT.@)
666 FILE * CDECL
__iob_func(void)
671 /*********************************************************************
674 int CDECL
_access(const char *filename
, int mode
)
676 DWORD attr
= GetFileAttributesA(filename
);
678 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
680 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
682 _dosmaperr(GetLastError());
685 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
687 _set_errno(ERROR_ACCESS_DENIED
);
693 /*********************************************************************
694 * _access_s (MSVCRT.@)
696 int CDECL
_access_s(const char *filename
, int mode
)
698 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
699 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
705 return _access(filename
, mode
);
708 /*********************************************************************
709 * _waccess (MSVCRT.@)
711 int CDECL
_waccess(const wchar_t *filename
, int mode
)
713 DWORD attr
= GetFileAttributesW(filename
);
715 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
717 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
719 _dosmaperr(GetLastError());
722 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
724 _set_errno(ERROR_ACCESS_DENIED
);
730 /*********************************************************************
731 * _waccess_s (MSVCRT.@)
733 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
735 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
736 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
742 return _waccess(filename
, mode
);
745 /*********************************************************************
748 int CDECL
_chmod(const char *path
, int flags
)
750 DWORD oldFlags
= GetFileAttributesA(path
);
752 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
754 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
755 oldFlags
| FILE_ATTRIBUTE_READONLY
;
757 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
760 _dosmaperr(GetLastError());
764 /*********************************************************************
767 int CDECL
_wchmod(const wchar_t *path
, int flags
)
769 DWORD oldFlags
= GetFileAttributesW(path
);
771 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
773 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
774 oldFlags
| FILE_ATTRIBUTE_READONLY
;
776 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
779 _dosmaperr(GetLastError());
783 /*********************************************************************
786 int CDECL
_unlink(const char *path
)
788 TRACE("%s\n",debugstr_a(path
));
789 if(DeleteFileA(path
))
791 TRACE("failed (%d)\n",GetLastError());
792 _dosmaperr(GetLastError());
796 /*********************************************************************
797 * _wunlink (MSVCRT.@)
799 int CDECL
_wunlink(const wchar_t *path
)
801 TRACE("(%s)\n",debugstr_w(path
));
802 if(DeleteFileW(path
))
804 TRACE("failed (%d)\n",GetLastError());
805 _dosmaperr(GetLastError());
809 /*********************************************************************
812 int CDECL
_commit(int fd
)
814 HANDLE hand
= fdtoh(fd
);
816 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
817 if (hand
== INVALID_HANDLE_VALUE
)
820 if (!FlushFileBuffers(hand
))
822 if (GetLastError() == ERROR_INVALID_HANDLE
)
824 /* FlushFileBuffers fails for console handles
825 * so we ignore this error.
829 TRACE(":failed-last error (%d)\n",GetLastError());
830 _dosmaperr(GetLastError());
837 /* _flushall calls fflush which calls _flushall */
838 int CDECL
fflush(FILE* file
);
840 /* INTERNAL: Flush all stream buffer */
841 static int msvcrt_flush_all_buffers(int mask
)
843 int i
, num_flushed
= 0;
847 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
848 file
= msvcrt_get_file(i
);
852 if(file
->_flag
& mask
) {
860 TRACE(":flushed (%d) handles\n",num_flushed
);
864 /*********************************************************************
865 * _flushall (MSVCRT.@)
867 int CDECL
_flushall(void)
869 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
872 /*********************************************************************
875 int CDECL
fflush(FILE* file
)
878 msvcrt_flush_all_buffers(_IOWRT
);
879 } else if(file
->_flag
& _IOWRT
) {
883 res
= msvcrt_flush_buffer(file
);
885 if(!res && (file->_flag & _IOCOMMIT))
886 res = _commit(file->_file) ? EOF : 0;
891 } else if(file
->_flag
& _IOREAD
) {
894 file
->_ptr
= file
->_base
;
902 /*********************************************************************
905 int CDECL
_close(int fd
)
912 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
913 if (hand
== INVALID_HANDLE_VALUE
)
915 else if (!CloseHandle(hand
))
917 WARN(":failed-last error (%d)\n",GetLastError());
918 _dosmaperr(GetLastError());
931 /*********************************************************************
934 * MSDN isn't clear on this point, but the remarks for _pipe
935 * indicate file descriptors duplicated with _dup and _dup2 are always
938 int CDECL
_dup2(int od
, int nd
)
942 TRACE("(od=%d, nd=%d)\n", od
, nd
);
944 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
948 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
949 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
951 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
955 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
963 /* _dup2 returns 0, not nd, on success */
970 _dosmaperr(GetLastError());
982 /*********************************************************************
985 int CDECL
_dup(int od
)
991 if (_dup2(od
, fd
) == 0)
999 /*********************************************************************
1002 int CDECL
_eof(int fd
)
1004 DWORD curpos
,endpos
;
1005 LONG hcurpos
,hendpos
;
1006 HANDLE hand
= fdtoh(fd
);
1008 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1010 if (hand
== INVALID_HANDLE_VALUE
)
1013 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
1015 /* Otherwise we do it the hard way */
1016 hcurpos
= hendpos
= 0;
1017 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
1018 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1020 if (curpos
== endpos
&& hcurpos
== hendpos
)
1022 /* FIXME: shouldn't WX_ATEOF be set here? */
1026 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1030 /*********************************************************************
1031 * _fcloseall (MSVCRT.@)
1033 int CDECL
_fcloseall(void)
1035 int num_closed
= 0, i
;
1039 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1040 file
= msvcrt_get_file(i
);
1042 if (file
->_flag
&& !fclose(file
))
1047 TRACE(":closed (%d) handles\n",num_closed
);
1051 /* free everything on process exit */
1052 void msvcrt_free_io(void)
1060 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1063 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1065 FILE *file
= msvcrt_get_file(j
);
1066 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1068 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1069 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1073 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1074 free(MSVCRT_fstream
[i
]);
1077 /*********************************************************************
1078 * _lseeki64 (MSVCRT.@)
1080 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1082 HANDLE hand
= fdtoh(fd
);
1085 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1086 if (hand
== INVALID_HANDLE_VALUE
)
1089 if (whence
< 0 || whence
> 2)
1095 TRACE(":fd (%d) to %s pos %s\n",
1096 fd
,wine_dbgstr_longlong(offset
),
1097 (whence
==SEEK_SET
)?"SEEK_SET":
1098 (whence
==SEEK_CUR
)?"SEEK_CUR":
1099 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1101 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1102 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1103 ofs
.QuadPart
= offset
;
1104 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1105 GetLastError() == ERROR_SUCCESS
)
1107 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1108 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1110 return ofs
.QuadPart
;
1112 TRACE(":error-last error (%d)\n",GetLastError());
1113 _dosmaperr(GetLastError());
1117 /*********************************************************************
1120 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1122 return (LONG
)_lseeki64(fd
, offset
, whence
);
1125 /*********************************************************************
1126 * _lock_file (MSVCRT.@)
1128 void CDECL
_lock_file(FILE *file
)
1130 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1131 _lock(_STREAM_LOCKS
+(file
-_iob
));
1132 /* ReactOS: string streams dont need to be locked */
1133 else if(!(file
->_flag
& _IOSTRG
))
1134 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1137 /*********************************************************************
1138 * _unlock_file (MSVCRT.@)
1140 void CDECL
_unlock_file(FILE *file
)
1142 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1143 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1144 /* ReactOS: string streams dont need to be locked */
1145 else if(!(file
->_flag
& _IOSTRG
))
1146 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1150 /*********************************************************************
1151 * _locking (MSVCRT.@)
1153 * This is untested; the underlying LockFile doesn't work yet.
1155 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1159 HANDLE hand
= fdtoh(fd
);
1161 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1162 if (hand
== INVALID_HANDLE_VALUE
)
1165 if (mode
< 0 || mode
> 4)
1171 TRACE(":fd (%d) by 0x%08x mode %s\n",
1172 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1173 (mode
==_LK_LOCK
)?"_LK_LOCK":
1174 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1175 (mode
==_LK_RLCK
)?"_LK_RLCK":
1176 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1179 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1181 FIXME ("Seek failed\n");
1182 *_errno() = EINVAL
; /* FIXME */
1185 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1188 ret
= 1; /* just to satisfy gcc */
1191 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1196 else if (mode
== _LK_UNLCK
)
1197 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1199 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1200 /* FIXME - what about error settings? */
1201 return ret
? 0 : -1;
1204 /*********************************************************************
1205 * _fseeki64 (MSVCRT.@)
1207 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1212 /* Flush output if needed */
1213 if(file
->_flag
& _IOWRT
)
1214 msvcrt_flush_buffer(file
);
1216 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1218 offset
+= _ftelli64(file
);
1221 /* Discard buffered input */
1223 file
->_ptr
= file
->_base
;
1224 /* Reset direction of i/o */
1225 if(file
->_flag
& _IORW
) {
1226 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1228 /* Clear end of file flag */
1229 file
->_flag
&= ~_IOEOF
;
1230 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1236 /*********************************************************************
1239 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1241 return _fseeki64( file
, offset
, whence
);
1244 /*********************************************************************
1245 * _chsize_s (MSVCRT.@)
1247 int CDECL
_chsize_s(int fd
, __int64 size
)
1253 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1255 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1260 if (handle
!= INVALID_HANDLE_VALUE
)
1262 /* save the current file pointer */
1263 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1266 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1269 ret
= SetEndOfFile(handle
);
1270 if (!ret
) _dosmaperr(GetLastError());
1273 /* restore the file pointer */
1274 _lseeki64(fd
, cur
, SEEK_SET
);
1279 return ret
? 0 : *_errno();
1282 /*********************************************************************
1283 * _chsize (MSVCRT.@)
1285 int CDECL
_chsize(int fd
, long size
)
1287 /* _chsize_s returns errno on failure but _chsize should return -1 */
1288 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1291 /*********************************************************************
1292 * clearerr (MSVCRT.@)
1294 void CDECL
clearerr(FILE* file
)
1296 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1299 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1303 /*********************************************************************
1306 void CDECL
rewind(FILE* file
)
1308 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1311 fseek(file
, 0L, SEEK_SET
);
1316 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1318 int plus
= strchrW(mode
, '+') != NULL
;
1320 TRACE("%s\n", debugstr_w(mode
));
1322 while(*mode
== ' ') mode
++;
1327 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1328 *stream_flags
= plus
? _IORW
: _IOREAD
;
1331 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1332 *stream_flags
= plus
? _IORW
: _IOWRT
;
1335 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1336 *stream_flags
= plus
? _IORW
: _IOWRT
;
1339 MSVCRT_INVALID_PMT(0, EINVAL
);
1343 *stream_flags
|= _commode
;
1345 while (*mode
&& *mode
!=',')
1349 *open_flags
|= _O_BINARY
;
1350 *open_flags
&= ~_O_TEXT
;
1353 *open_flags
|= _O_TEXT
;
1354 *open_flags
&= ~_O_BINARY
;
1357 *open_flags
|= _O_TEMPORARY
;
1360 *open_flags
|= _O_SHORT_LIVED
;
1363 *stream_flags
|= _IOCOMMIT
;
1366 *stream_flags
&= ~_IOCOMMIT
;
1369 *open_flags
|= _O_NOINHERIT
;
1378 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1381 ERR("incorrect mode flag: %c\n", mode
[-1]);
1387 static const WCHAR ccs
[] = {'c','c','s'};
1388 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1389 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1390 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1393 while(*mode
== ' ') mode
++;
1394 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1396 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1397 while(*mode
== ' ') mode
++;
1398 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1401 while(*mode
== ' ') mode
++;
1403 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1405 *open_flags
|= _O_U8TEXT
;
1406 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1408 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1410 *open_flags
|= _O_U16TEXT
;
1411 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1413 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1415 *open_flags
|= _O_WTEXT
;
1416 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1420 MSVCRT_INVALID_PMT(0, EINVAL
);
1424 while(*mode
== ' ') mode
++;
1427 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1432 /*********************************************************************
1433 * _fdopen (MSVCRT.@)
1435 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1438 wchar_t *modeW
= NULL
;
1440 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1442 ret
= _wfdopen(fd
, modeW
);
1448 /*********************************************************************
1449 * _wfdopen (MSVCRT.@)
1451 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1453 int open_flags
, stream_flags
;
1456 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1459 if (!(file
= msvcrt_alloc_fp()))
1461 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1466 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1472 /*********************************************************************
1473 * _filelength (MSVCRT.@)
1475 LONG CDECL
_filelength(int fd
)
1477 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1480 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1483 if (endPos
!= curPos
)
1484 _lseek(fd
, curPos
, SEEK_SET
);
1491 /*********************************************************************
1492 * _filelengthi64 (MSVCRT.@)
1494 __int64 CDECL
_filelengthi64(int fd
)
1496 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1499 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1502 if (endPos
!= curPos
)
1503 _lseeki64(fd
, curPos
, SEEK_SET
);
1510 /*********************************************************************
1511 * _fileno (MSVCRT.@)
1513 int CDECL
_fileno(FILE* file
)
1515 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1519 /*********************************************************************
1520 * _get_osfhandle (MSVCRT.@)
1522 intptr_t CDECL
_get_osfhandle(int fd
)
1524 HANDLE hand
= fdtoh(fd
);
1525 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1527 return (intptr_t)hand
;
1530 /*********************************************************************
1531 * _mktemp (MSVCRT.@)
1533 char * CDECL
_mktemp(char *pattern
)
1536 char *retVal
= pattern
;
1544 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1548 id
= GetCurrentProcessId();
1552 int tempNum
= id
/ 10;
1553 *pattern
-- = id
- (tempNum
* 10) + '0';
1559 *pattern
= letter
++;
1560 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1562 } while(letter
<= 'z');
1566 /*********************************************************************
1567 * _wmktemp (MSVCRT.@)
1569 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1572 wchar_t *retVal
= pattern
;
1574 wchar_t letter
= 'a';
1577 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1581 id
= GetCurrentProcessId();
1585 int tempNum
= id
/ 10;
1586 *pattern
-- = id
- (tempNum
* 10) + '0';
1592 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1593 GetLastError() == ERROR_FILE_NOT_FOUND
)
1595 *pattern
= letter
++;
1596 } while(letter
!= '|');
1600 /*static*/ unsigned split_oflags(unsigned oflags
)
1603 unsigned unsupp
; /* until we support everything */
1605 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1606 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1607 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1608 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1609 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1610 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1611 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1612 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1613 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1615 if ((unsupp
= oflags
& ~(
1616 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1617 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1618 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1620 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1621 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1623 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1628 /*********************************************************************
1631 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1634 SECURITY_ATTRIBUTES sa
;
1635 HANDLE readHandle
, writeHandle
;
1643 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1644 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1645 sa
.lpSecurityDescriptor
= NULL
;
1646 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1648 unsigned int wxflags
= split_oflags(textmode
);
1652 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1656 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1665 CloseHandle(writeHandle
);
1671 CloseHandle(readHandle
);
1672 CloseHandle(writeHandle
);
1678 _dosmaperr(GetLastError());
1683 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1685 char bom
[sizeof(utf8_bom
)];
1688 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1690 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1693 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1694 oflags
|= _O_U8TEXT
;
1695 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1697 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1698 oflags
|= _O_U16TEXT
;
1700 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1706 /*********************************************************************
1707 * _wsopen_s (MSVCRT.@)
1709 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1711 DWORD access
= 0, creation
= 0, attrib
;
1712 SECURITY_ATTRIBUTES sa
;
1713 DWORD sharing
, type
;
1717 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1718 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1720 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1723 wxflag
= split_oflags(oflags
);
1724 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1726 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1727 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1728 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1731 if (oflags
& _O_CREAT
)
1733 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1734 FIXME(": pmode 0x%04x ignored\n", pmode
);
1736 WARN(": pmode 0x%04x ignored\n", pmode
);
1738 if (oflags
& _O_EXCL
)
1739 creation
= CREATE_NEW
;
1740 else if (oflags
& _O_TRUNC
)
1741 creation
= CREATE_ALWAYS
;
1743 creation
= OPEN_ALWAYS
;
1745 else /* no _O_CREAT */
1747 if (oflags
& _O_TRUNC
)
1748 creation
= TRUNCATE_EXISTING
;
1750 creation
= OPEN_EXISTING
;
1759 sharing
= FILE_SHARE_READ
;
1762 sharing
= FILE_SHARE_WRITE
;
1765 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1768 ERR( "Unhandled shflags 0x%x\n", shflags
);
1771 attrib
= FILE_ATTRIBUTE_NORMAL
;
1773 if (oflags
& _O_TEMPORARY
)
1775 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1777 sharing
|= FILE_SHARE_DELETE
;
1780 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1781 sa
.lpSecurityDescriptor
= NULL
;
1782 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1784 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1785 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1786 && !(access
&GENERIC_READ
))
1788 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1789 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1790 if (hand
!= INVALID_HANDLE_VALUE
)
1792 oflags
= check_bom(hand
, oflags
, FALSE
);
1796 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1799 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1800 if (hand
== INVALID_HANDLE_VALUE
) {
1801 WARN(":failed-last error (%d)\n",GetLastError());
1802 _dosmaperr(GetLastError());
1806 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1808 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1809 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1810 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1812 if (oflags
& _O_U8TEXT
)
1814 DWORD written
= 0, tmp
;
1816 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1817 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1819 if (written
!= sizeof(utf8_bom
)) {
1820 WARN("error writing BOM\n");
1822 _dosmaperr(GetLastError());
1828 DWORD written
= 0, tmp
;
1830 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1831 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1833 if (written
!= sizeof(utf16_bom
))
1835 WARN("error writing BOM\n");
1837 _dosmaperr(GetLastError());
1842 else if (access
& GENERIC_READ
)
1843 oflags
= check_bom(hand
, oflags
, TRUE
);
1845 type
= GetFileType(hand
);
1846 if (type
== FILE_TYPE_CHAR
)
1848 else if (type
== FILE_TYPE_PIPE
)
1851 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1855 if (oflags
& _O_WTEXT
)
1856 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1857 else if (oflags
& _O_U16TEXT
)
1858 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1859 else if (oflags
& _O_U8TEXT
)
1860 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1862 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1866 /*********************************************************************
1867 * _wsopen (MSVCRT.@)
1869 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1874 if (oflags
& _O_CREAT
)
1878 __ms_va_start(ap
, shflags
);
1879 pmode
= va_arg(ap
, int);
1885 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1889 /*********************************************************************
1890 * _sopen_s (MSVCRT.@)
1892 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1897 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1900 ret
= _wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1905 /*********************************************************************
1908 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1913 if (oflags
& _O_CREAT
)
1917 va_start(ap
, shflags
);
1918 pmode
= va_arg(ap
, int);
1924 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1928 /*********************************************************************
1931 int CDECL
_open( const char *path
, int flags
, ... )
1935 if (flags
& _O_CREAT
)
1938 va_start(ap
, flags
);
1939 pmode
= va_arg(ap
, int);
1941 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1944 return _sopen( path
, flags
, _SH_DENYNO
);
1947 /*********************************************************************
1950 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1954 if (flags
& _O_CREAT
)
1957 va_start(ap
, flags
);
1958 pmode
= va_arg(ap
, int);
1960 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1963 return _wsopen( path
, flags
, _SH_DENYNO
);
1966 /*********************************************************************
1969 int CDECL
_creat(const char *path
, int flags
)
1971 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1972 return _open(path
, usedFlags
);
1975 /*********************************************************************
1976 * _wcreat (MSVCRT.@)
1978 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1980 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1981 return _wopen(path
, usedFlags
);
1984 /*********************************************************************
1985 * _open_osfhandle (MSVCRT.@)
1987 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1992 /* _O_RDONLY (0) always matches, so set the read flag
1993 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1994 * file, so set the write flag. It also only sets _O_TEXT if it wants
1995 * text - it never sets _O_BINARY.
1997 /* don't let split_oflags() decide the mode if no mode is passed */
1998 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1999 oflags
|= _O_BINARY
;
2001 flags
= GetFileType((HANDLE
)handle
);
2002 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2004 _dosmaperr(GetLastError());
2008 if (flags
== FILE_TYPE_CHAR
)
2010 else if (flags
== FILE_TYPE_PIPE
)
2014 flags
|= split_oflags(oflags
);
2016 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2017 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2021 /*********************************************************************
2024 int CDECL
_rmtmp(void)
2026 int num_removed
= 0, i
;
2030 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2031 file
= msvcrt_get_file(i
);
2033 if (file
->_tmpfname
)
2042 TRACE(":removed (%d) temp files\n",num_removed
);
2046 static inline int get_utf8_char_len(char ch
)
2048 if((ch
&0xf8) == 0xf0)
2050 else if((ch
&0xf0) == 0xe0)
2052 else if((ch
&0xe0) == 0xc0)
2057 /*********************************************************************
2058 * (internal) read_utf8
2060 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2062 ioinfo
*fdinfo
= get_ioinfo(fd
);
2063 HANDLE hand
= fdinfo
->handle
;
2064 char min_buf
[4], *readbuf
, lookahead
;
2065 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2067 /* make the buffer big enough to hold at least one character */
2068 /* read bytes have to fit to output and lookahead buffers */
2070 readbuf_size
= count
< 4 ? 4 : count
;
2071 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2076 if(fdinfo
->lookahead
[0] != '\n') {
2077 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2078 fdinfo
->lookahead
[0] = '\n';
2080 if(fdinfo
->lookahead
[1] != '\n') {
2081 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2082 fdinfo
->lookahead
[1] = '\n';
2084 if(fdinfo
->lookahead
[2] != '\n') {
2085 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2086 fdinfo
->lookahead
[2] = '\n';
2091 /* NOTE: this case is broken in native dll, reading
2092 * sometimes fails when small buffer is passed
2095 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2096 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2097 fdinfo
->wxflag
|= WX_ATEOF
;
2100 _dosmaperr(GetLastError());
2103 }else if(!num_read
) {
2104 fdinfo
->wxflag
|= WX_ATEOF
;
2110 char_len
= get_utf8_char_len(readbuf
[0]);
2112 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2116 if(readbuf
[0] == '\n')
2117 fdinfo
->wxflag
|= WX_READNL
;
2119 fdinfo
->wxflag
&= ~WX_READNL
;
2121 if(readbuf
[0] == 0x1a) {
2122 fdinfo
->wxflag
|= WX_ATEOF
;
2126 if(readbuf
[0] == '\r') {
2127 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2129 else if(lookahead
== '\n')
2133 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2134 fdinfo
->lookahead
[0] = lookahead
;
2136 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2141 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2142 _dosmaperr(GetLastError());
2149 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2152 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2153 fdinfo
->wxflag
|= WX_ATEOF
;
2154 if (readbuf
!= min_buf
) free(readbuf
);
2157 _dosmaperr(GetLastError());
2158 if (readbuf
!= min_buf
) free(readbuf
);
2161 }else if(!pos
&& !num_read
) {
2162 fdinfo
->wxflag
|= WX_ATEOF
;
2163 if (readbuf
!= min_buf
) free(readbuf
);
2168 if(readbuf
[0] == '\n')
2169 fdinfo
->wxflag
|= WX_READNL
;
2171 fdinfo
->wxflag
&= ~WX_READNL
;
2173 /* Find first byte of last character (may be incomplete) */
2174 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2175 if((readbuf
[i
]&0xc0) != 0x80)
2177 char_len
= get_utf8_char_len(readbuf
[i
]);
2178 if(char_len
+i
<= pos
)
2181 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2183 fdinfo
->lookahead
[0] = readbuf
[i
];
2185 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2187 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2189 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2193 for(i
=0, j
=0; i
<pos
; i
++) {
2194 if(readbuf
[i
] == 0x1a) {
2195 fdinfo
->wxflag
|= WX_ATEOF
;
2199 /* strip '\r' if followed by '\n' */
2200 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2201 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2202 readbuf
[j
++] = '\r';
2203 }else if(lookahead
== '\n' && j
==0) {
2204 readbuf
[j
++] = '\n';
2206 if(lookahead
!= '\n')
2207 readbuf
[j
++] = '\r';
2209 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2210 fdinfo
->lookahead
[0] = lookahead
;
2212 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2214 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2215 readbuf
[j
++] = readbuf
[i
];
2220 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2221 _dosmaperr(GetLastError());
2222 if (readbuf
!= min_buf
) free(readbuf
);
2226 if (readbuf
!= min_buf
) free(readbuf
);
2230 /*********************************************************************
2233 * When reading \r as last character in text mode, read() positions
2234 * the file pointer on the \r character while getc() goes on to
2237 static int read_i(int fd
, void *buf
, unsigned int count
)
2239 DWORD num_read
, utf16
;
2240 char *bufstart
= buf
;
2241 HANDLE hand
= fdtoh(fd
);
2242 ioinfo
*fdinfo
= get_ioinfo(fd
);
2247 if (fdinfo
->wxflag
& WX_ATEOF
) {
2248 TRACE("already at EOF, returning 0\n");
2251 /* Don't trace small reads, it gets *very* annoying */
2253 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2254 if (hand
== INVALID_HANDLE_VALUE
)
2260 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2261 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2267 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2268 return read_utf8(fd
, buf
, count
);
2270 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2272 if (fdinfo
->lookahead
[0] != '\n')
2274 bufstart
[0] = fdinfo
->lookahead
[0];
2275 fdinfo
->lookahead
[0] = '\n';
2279 bufstart
[1] = fdinfo
->lookahead
[1];
2280 fdinfo
->lookahead
[1] = '\n';
2283 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2284 num_read
+= 1+utf16
;
2289 if(utf16
&& (num_read
&1))
2291 /* msvcr90 uses uninitialized value from the buffer in this case */
2292 /* msvcrt ignores additional data */
2293 ERR("got odd number of bytes in UTF16 mode\n");
2297 if (count
!= 0 && num_read
== 0)
2299 fdinfo
->wxflag
|= WX_ATEOF
;
2300 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2302 else if (fdinfo
->wxflag
& WX_TEXT
)
2306 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2307 fdinfo
->wxflag
|= WX_READNL
;
2309 fdinfo
->wxflag
&= ~WX_READNL
;
2311 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2313 /* in text mode, a ctrl-z signals EOF */
2314 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2316 fdinfo
->wxflag
|= WX_ATEOF
;
2317 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2321 /* in text mode, strip \r if followed by \n */
2322 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2327 lookahead
[1] = '\n';
2328 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2330 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2332 bufstart
[j
++] = '\n';
2333 if(utf16
) bufstart
[j
++] = 0;
2337 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2339 bufstart
[j
++] = '\r';
2340 if(utf16
) bufstart
[j
++] = 0;
2343 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2345 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2347 bufstart
[j
++] = '\n';
2348 if (utf16
) bufstart
[j
++] = 0;
2352 fdinfo
->lookahead
[0] = lookahead
[0];
2353 fdinfo
->lookahead
[1] = lookahead
[1];
2357 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2362 bufstart
[j
++] = '\r';
2363 if(utf16
) bufstart
[j
++] = 0;
2366 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2367 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2369 bufstart
[j
++] = bufstart
[i
];
2370 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2378 if (GetLastError() == ERROR_BROKEN_PIPE
)
2380 TRACE(":end-of-pipe\n");
2381 fdinfo
->wxflag
|= WX_ATEOF
;
2386 TRACE(":failed-last error (%d)\n",GetLastError());
2392 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2396 /*********************************************************************
2399 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2402 num_read
= read_i(fd
, buf
, count
);
2406 /*********************************************************************
2407 * _setmode (MSVCRT.@)
2409 int CDECL
_setmode(int fd
,int mode
)
2411 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2412 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2415 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2416 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2421 if(mode
== _O_BINARY
) {
2422 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2423 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2427 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2429 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2430 else if(mode
== _O_U8TEXT
)
2431 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2433 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2439 /*********************************************************************
2442 long CDECL
_tell(int fd
)
2444 return _lseek(fd
, 0, SEEK_CUR
);
2447 /*********************************************************************
2448 * _telli64 (MSVCRT.@)
2450 __int64 CDECL
_telli64(int fd
)
2452 return _lseeki64(fd
, 0, SEEK_CUR
);
2455 /*********************************************************************
2456 * _tempnam (MSVCRT.@)
2458 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2460 char tmpbuf
[MAX_PATH
];
2461 const char *tmp_dir
= getenv("TMP");
2463 if (tmp_dir
) dir
= tmp_dir
;
2465 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2466 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2468 TRACE("got name (%s)\n",tmpbuf
);
2469 DeleteFileA(tmpbuf
);
2470 return _strdup(tmpbuf
);
2472 TRACE("failed (%d)\n",GetLastError());
2476 /*********************************************************************
2477 * _wtempnam (MSVCRT.@)
2479 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2481 wchar_t tmpbuf
[MAX_PATH
];
2483 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2484 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2486 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2487 DeleteFileW(tmpbuf
);
2488 return _wcsdup(tmpbuf
);
2490 TRACE("failed (%d)\n",GetLastError());
2494 /*********************************************************************
2497 int CDECL
_umask(int umask
)
2499 int old_umask
= MSVCRT_umask
;
2500 TRACE("(%d)\n",umask
);
2501 MSVCRT_umask
= umask
;
2505 /*********************************************************************
2508 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2511 ioinfo
*info
= get_ioinfo(fd
);
2512 HANDLE hand
= info
->handle
;
2514 /* Don't trace small writes, it gets *very* annoying */
2517 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2519 if (hand
== INVALID_HANDLE_VALUE
)
2525 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2531 /* If appending, go to EOF */
2532 if (info
->wxflag
& WX_APPEND
)
2533 _lseek(fd
, 0, FILE_END
);
2535 if (!(info
->wxflag
& WX_TEXT
))
2537 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2538 && (num_written
== count
))
2540 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2541 hand
, GetLastError());
2546 unsigned int i
, j
, nr_lf
, size
;
2549 const char *s
= buf
, *buf_start
= buf
;
2551 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2553 /* find number of \n */
2554 for (nr_lf
=0, i
=0; i
<count
; i
++)
2560 if ((q
= p
= malloc(size
)))
2562 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2571 FIXME("Malloc failed\n");
2583 else if (info
->exflag
& EF_UTF16
)
2585 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2586 if (s
[i
]=='\n' && s
[i
+1]==0)
2591 if ((q
= p
= malloc(size
)))
2593 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2595 if (s
[i
]=='\n' && s
[i
+1]==0)
2606 FIXME("Malloc failed\n");
2622 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2623 if (s
[i
]=='\n' && s
[i
+1]==0)
2626 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2628 _dosmaperr(GetLastError());
2633 size
= conv_len
+nr_lf
;
2634 if((p
= malloc(count
+nr_lf
*2+size
)))
2636 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2638 if (s
[i
]=='\n' && s
[i
+1]==0)
2646 q
= p
+count
+nr_lf
*2;
2647 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2648 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2652 FIXME("Malloc failed\n");
2659 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2663 if (num_written
!= size
)
2665 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2666 fd
, hand
, GetLastError(), num_written
);
2668 return s
- buf_start
;
2676 /*********************************************************************
2679 int CDECL
_putw(int val
, FILE* file
)
2684 len
= _write(file
->_file
, &val
, sizeof(val
));
2685 if (len
== sizeof(val
)) {
2690 file
->_flag
|= _IOERR
;
2695 /*********************************************************************
2698 int CDECL
fclose(FILE* file
)
2704 free(file
->_tmpfname
);
2705 file
->_tmpfname
= NULL
;
2706 /* flush stdio buffers */
2707 if(file
->_flag
& _IOWRT
)
2709 if(file
->_flag
& _IOMYBUF
)
2712 r
=_close(file
->_file
);
2716 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2717 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2719 if(file
== msvcrt_get_file(MSVCRT_stream_idx
-1)) {
2720 while(MSVCRT_stream_idx
>3 && !file
->_flag
) {
2721 MSVCRT_stream_idx
--;
2722 file
= msvcrt_get_file(MSVCRT_stream_idx
-1);
2726 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2729 /*********************************************************************
2732 int CDECL
feof(FILE* file
)
2734 return file
->_flag
& _IOEOF
;
2737 /*********************************************************************
2740 int CDECL
ferror(FILE* file
)
2742 return file
->_flag
& _IOERR
;
2745 /*********************************************************************
2746 * _filbuf (MSVCRT.@)
2748 int CDECL
_filbuf(FILE* file
)
2753 if(file
->_flag
& _IOSTRG
) {
2758 /* Allocate buffer if needed */
2759 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
2762 if(!(file
->_flag
& _IOREAD
)) {
2763 if(file
->_flag
& _IORW
)
2764 file
->_flag
|= _IOREAD
;
2771 if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
2773 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2774 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2782 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2784 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2791 file
->_ptr
= file
->_base
+1;
2792 c
= *(unsigned char *)file
->_base
;
2798 /*********************************************************************
2801 int CDECL
fgetc(FILE* file
)
2809 i
= (unsigned char *)file
->_ptr
++;
2818 /*********************************************************************
2819 * _fgetchar (MSVCRT.@)
2821 int CDECL
_fgetchar(void)
2823 return fgetc(stdin
);
2826 /*********************************************************************
2829 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2832 char * buf_start
= s
;
2834 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2835 file
,file
->_file
,s
,size
);
2839 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2844 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2846 TRACE(":nothing read\n");
2850 if ((cc
!= EOF
) && (size
> 1))
2853 TRACE(":got %s\n", debugstr_a(buf_start
));
2858 /*********************************************************************
2861 wint_t CDECL
fgetwc(FILE* file
)
2868 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2869 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2872 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2881 char mbs
[MB_LEN_MAX
];
2887 if(isleadbyte((unsigned char)mbs
[0])) {
2898 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2906 /*********************************************************************
2909 int CDECL
_getw(FILE* file
)
2917 for (j
=0; j
<sizeof(int); j
++) {
2920 file
->_flag
|= _IOEOF
;
2931 /*********************************************************************
2934 wint_t CDECL
getwc(FILE* file
)
2936 return fgetwc(file
);
2939 /*********************************************************************
2940 * _fgetwchar (MSVCRT.@)
2942 wint_t CDECL
_fgetwchar(void)
2944 return fgetwc(stdin
);
2947 /*********************************************************************
2948 * getwchar (MSVCRT.@)
2950 wint_t CDECL
getwchar(void)
2952 return _fgetwchar();
2955 /*********************************************************************
2958 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2961 wchar_t * buf_start
= s
;
2963 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2964 file
,file
->_file
,s
,size
);
2968 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2973 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2975 TRACE(":nothing read\n");
2979 if ((cc
!= WEOF
) && (size
> 1))
2982 TRACE(":got %s\n", debugstr_w(buf_start
));
2987 /*********************************************************************
2990 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2992 size_t wrcnt
=size
* nmemb
;
3001 if(file
->_cnt
< 0) {
3002 WARN("negative file->_cnt value in %p\n", file
);
3003 file
->_flag
|= MSVCRT__IOERR
;
3008 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3009 memcpy(file
->_ptr
, ptr
, pcnt
);
3014 ptr
= (const char*)ptr
+ pcnt
;
3015 } else if((file
->_flag
& _IONBF
)
3016 || ((file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3017 || (!(file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3021 if(file
->_flag
& _IONBF
)
3023 else if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
)))
3024 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3026 bufsiz
= file
->_bufsiz
;
3028 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3030 if(msvcrt_flush_buffer(file
) == EOF
)
3033 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
3034 file
->_flag
|= _IOERR
;
3039 ptr
= (const char*)ptr
+ pcnt
;
3041 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3045 ptr
= (const char*)ptr
+ 1;
3050 return written
/ size
;
3053 /*********************************************************************
3055 * FORKED for ReactOS, don't sync with Wine!
3057 * - http://jira.reactos.org/browse/CORE-6495
3058 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3060 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3062 /* If this is a real file stream (and not some temporary one for
3063 sprintf-like functions), check whether it is opened in text mode.
3064 In this case, we have to perform an implicit conversion to ANSI. */
3065 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
3067 /* Convert to multibyte in text mode */
3068 char mbc
[MB_LEN_MAX
];
3071 mb_return
= wctomb(mbc
, c
);
3076 /* Output all characters */
3077 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3082 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3089 /*********************************************************************
3090 * _fputwchar (MSVCRT.@)
3092 wint_t CDECL
_fputwchar(wint_t wc
)
3094 return fputwc(wc
, stdout
);
3097 /*********************************************************************
3098 * _wfsopen (MSVCRT.@)
3100 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3103 int open_flags
, stream_flags
, fd
;
3105 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3107 /* map mode string to open() flags. "man fopen" for possibilities. */
3108 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3112 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3115 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3117 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3124 TRACE(":got (%p)\n",file
);
3125 if (fd
>= 0 && !file
)
3131 /*********************************************************************
3132 * _fsopen (MSVCRT.@)
3134 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3137 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3139 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3140 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3144 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3147 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3152 ret
= _wfsopen(pathW
, modeW
, share
);
3159 /*********************************************************************
3162 FILE * CDECL
fopen(const char *path
, const char *mode
)
3164 return _fsopen( path
, mode
, _SH_DENYNO
);