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
98 #define WX_NOSEEK 0x40
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
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
145 static int max_streams
= 512, 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 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 file_cs
;
161 static CRITICAL_SECTION_DEBUG file_cs_debug
=
164 { &file_cs_debug
.ProcessLocksList
, &file_cs_debug
.ProcessLocksList
},
165 0, 0, { (DWORD_PTR
)(__FILE__
": file_cs") }
167 static CRITICAL_SECTION file_cs
= { &file_cs_debug
, -1, 0, 0, 0, 0 };
168 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
169 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
171 static inline ioinfo
* get_ioinfo(int fd
)
175 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
179 return ret
+ (fd
%FD_BLOCK_SIZE
);
182 static inline FILE* get_file(int i
)
192 ret
= fstream
[i
/FD_BLOCK_SIZE
];
194 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
195 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
196 ERR("out of memory\n");
201 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
203 ret
+= i
%FD_BLOCK_SIZE
;
208 static inline BOOL
is_valid_fd(int fd
)
210 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
213 /* INTERNAL: Get the HANDLE for a fd
214 * This doesn't lock the table, because a failure will result in
215 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
216 * it returns a valid handle which is about to be closed, a subsequent call
217 * will fail, most likely in a sane way.
219 /*static*/ HANDLE
fdtoh(int fd
)
221 if (!is_valid_fd(fd
))
223 WARN(":fd (%d) - no handle!\n",fd
);
226 return INVALID_HANDLE_VALUE
;
228 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
229 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
230 return get_ioinfo(fd
)->handle
;
233 /* INTERNAL: free a file entry fd */
234 static void free_fd(int fd
)
240 fdinfo
= get_ioinfo(fd
);
241 old_handle
= fdinfo
->handle
;
242 if(fdinfo
!= &__badioinfo
)
244 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
247 TRACE(":fd (%d) freed\n",fd
);
248 if (fd
< 3) /* don't use 0,1,2 for user files */
253 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
256 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
259 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
273 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
274 /* caller must hold the files lock */
275 static int set_fd(HANDLE hand
, int flag
, int fd
)
281 WARN(":files exhausted!\n");
286 fdinfo
= get_ioinfo(fd
);
287 if(fdinfo
== &__badioinfo
) {
290 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
291 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
292 WARN(":out of memory!\n");
297 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
298 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
300 fdinfo
= get_ioinfo(fd
);
303 fdinfo
->handle
= hand
;
304 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_NOSEEK
));
305 fdinfo
->lookahead
[0] = '\n';
306 fdinfo
->lookahead
[1] = '\n';
307 fdinfo
->lookahead
[2] = '\n';
310 /* locate next free slot */
311 if (fd
== fdstart
&& fd
== fdend
)
314 while (fdstart
< fdend
&&
315 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
317 /* update last fd in use */
320 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
324 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
325 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
326 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
332 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
333 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
338 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
339 ret
= set_fd(hand
, flag
, fdstart
);
344 /* INTERNAL: Allocate a FILE* for an fd slot */
345 /* caller must hold the files lock */
346 static FILE* alloc_fp(void)
351 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
357 if (file
->_flag
== 0)
359 if (i
== stream_idx
) stream_idx
++;
367 /* INTERNAL: initialize a FILE* from an open fd */
368 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
370 TRACE(":fd (%d) allocating FILE*\n",fd
);
371 if (!is_valid_fd(fd
))
373 WARN(":invalid fd %d\n",fd
);
378 file
->_ptr
= file
->_base
= NULL
;
381 file
->_flag
= stream_flags
;
382 file
->_tmpfname
= NULL
;
384 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
385 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
387 TRACE(":got FILE* (%p)\n",file
);
391 /* INTERNAL: Create an inheritance data block (for spawned process)
392 * The inheritance block is made of:
393 * 00 int nb of file descriptor (NBFD)
394 * 04 char file flags (wxflag): repeated for each fd
395 * 4+NBFD HANDLE file handle: repeated for each fd
397 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
404 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
405 *block
= calloc(*size
, 1);
411 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
412 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
414 *(unsigned*)*block
= fdend
;
415 for (fd
= 0; fd
< fdend
; fd
++)
417 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
418 fdinfo
= get_ioinfo(fd
);
419 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
421 *wxflag_ptr
= fdinfo
->wxflag
;
422 *handle_ptr
= fdinfo
->handle
;
427 *handle_ptr
= INVALID_HANDLE_VALUE
;
429 wxflag_ptr
++; handle_ptr
++;
434 /* INTERNAL: Set up all file descriptors,
435 * as well as default streams (stdin, stderr and stdout)
437 void msvcrt_init_io(void)
443 GetStartupInfoA(&si
);
444 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
450 count
= *(unsigned*)si
.lpReserved2
;
451 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
452 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
454 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
455 count
= min(count
, MAX_FILES
);
456 for (i
= 0; i
< count
; i
++)
458 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
459 set_fd(*handle_ptr
, *wxflag_ptr
, i
);
461 wxflag_ptr
++; handle_ptr
++;
463 fdend
= max( 3, count
);
464 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
465 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
468 fdinfo
= get_ioinfo(STDIN_FILENO
);
469 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
470 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
471 DWORD type
= GetFileType(h
);
473 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
474 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDIN_FILENO
);
477 fdinfo
= get_ioinfo(STDOUT_FILENO
);
478 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
479 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
480 DWORD type
= GetFileType(h
);
482 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
483 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDOUT_FILENO
);
486 fdinfo
= get_ioinfo(STDERR_FILENO
);
487 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
488 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
489 DWORD type
= GetFileType(h
);
491 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
492 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDERR_FILENO
);
495 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(STDIN_FILENO
)->handle
,
496 get_ioinfo(STDOUT_FILENO
)->handle
,
497 get_ioinfo(STDERR_FILENO
)->handle
);
499 memset(_iob
,0,3*sizeof(FILE));
500 for (i
= 0; i
< 3; i
++)
502 /* FILE structs for stdin/out/err are static and never deleted */
504 _iob
[i
]._tmpfname
= NULL
;
505 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
510 /* INTERNAL: Flush stdio file buffer */
511 static int flush_buffer(FILE* file
)
513 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
514 int cnt
=file
->_ptr
-file
->_base
;
515 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
516 file
->_flag
|= _IOERR
;
519 file
->_ptr
=file
->_base
;
525 /*********************************************************************
528 int CDECL
_isatty(int fd
)
530 HANDLE hand
= fdtoh(fd
);
532 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
533 if (hand
== INVALID_HANDLE_VALUE
)
536 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
539 /* INTERNAL: Allocate stdio file buffer */
540 /*static*/ BOOL
alloc_buffer(FILE* file
)
542 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
543 && _isatty(file
->_file
))
546 file
->_base
= calloc(MSVCRT_INTERNAL_BUFSIZ
,1);
548 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
549 file
->_flag
|= _IOMYBUF
;
551 file
->_base
= (char*)(&file
->_charbuf
);
553 file
->_flag
|= _IONBF
;
555 file
->_ptr
= file
->_base
;
560 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
561 static BOOL
add_std_buffer(FILE *file
)
563 static char buffers
[2][BUFSIZ
];
565 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
566 || !_isatty(file
->_file
) || file
->_bufsiz
)
569 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
570 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
574 /* INTERNAL: Removes temporary buffer from stdout or stderr */
575 /* Only call this function when add_std_buffer returned TRUE */
576 static void remove_std_buffer(FILE *file
)
579 file
->_ptr
= file
->_base
= NULL
;
580 file
->_bufsiz
= file
->_cnt
= 0;
583 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
584 static int int_to_base32(int num
, char *str
)
599 *p
= (num
& 31) + '0';
601 *p
+= ('a' - '0' - 10);
608 /* INTERNAL: wide character version of int_to_base32 */
609 static int int_to_base32_w(int num
, wchar_t *str
)
624 *p
= (num
& 31) + '0';
626 *p
+= ('a' - '0' - 10);
633 /* INTERNAL: Create a wide string from an ascii string */
634 wchar_t *msvcrt_wstrdupa(const char *str
)
636 const unsigned int len
= strlen(str
) + 1 ;
637 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
640 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
644 /*********************************************************************
645 * __iob_func(MSVCRT.@)
647 FILE * CDECL
__iob_func(void)
652 /*********************************************************************
655 int CDECL
_access(const char *filename
, int mode
)
657 DWORD attr
= GetFileAttributesA(filename
);
659 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
661 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
663 _dosmaperr(GetLastError());
666 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
668 _set_errno(ERROR_ACCESS_DENIED
);
674 /*********************************************************************
675 * _access_s (MSVCRT.@)
677 int CDECL
_access_s(const char *filename
, int mode
)
679 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
680 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
686 return _access(filename
, mode
);
689 /*********************************************************************
690 * _waccess (MSVCRT.@)
692 int CDECL
_waccess(const wchar_t *filename
, int mode
)
694 DWORD attr
= GetFileAttributesW(filename
);
696 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
698 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
700 _dosmaperr(GetLastError());
703 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
705 _set_errno(ERROR_ACCESS_DENIED
);
711 /*********************************************************************
712 * _waccess_s (MSVCRT.@)
714 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
716 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
717 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
723 return _waccess(filename
, mode
);
726 /*********************************************************************
729 int CDECL
_chmod(const char *path
, int flags
)
731 DWORD oldFlags
= GetFileAttributesA(path
);
733 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
735 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
736 oldFlags
| FILE_ATTRIBUTE_READONLY
;
738 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
741 _dosmaperr(GetLastError());
745 /*********************************************************************
748 int CDECL
_wchmod(const wchar_t *path
, int flags
)
750 DWORD oldFlags
= GetFileAttributesW(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
|| SetFileAttributesW(path
, newFlags
))
760 _dosmaperr(GetLastError());
764 /*********************************************************************
767 int CDECL
_unlink(const char *path
)
769 TRACE("%s\n",debugstr_a(path
));
770 if(DeleteFileA(path
))
772 TRACE("failed (%d)\n",GetLastError());
773 _dosmaperr(GetLastError());
777 /*********************************************************************
778 * _wunlink (MSVCRT.@)
780 int CDECL
_wunlink(const wchar_t *path
)
782 TRACE("(%s)\n",debugstr_w(path
));
783 if(DeleteFileW(path
))
785 TRACE("failed (%d)\n",GetLastError());
786 _dosmaperr(GetLastError());
790 /* _flushall calls fflush which calls _flushall */
791 int CDECL
fflush(FILE* file
);
793 /* INTERNAL: Flush all stream buffer */
794 static int flush_all_buffers(int mask
)
796 int i
, num_flushed
= 0;
800 for (i
= 0; i
< stream_idx
; i
++) {
805 if(file
->_flag
& mask
) {
813 TRACE(":flushed (%d) handles\n",num_flushed
);
817 /*********************************************************************
818 * _flushall (MSVCRT.@)
820 int CDECL
_flushall(void)
822 return flush_all_buffers(_IOWRT
| _IOREAD
);
825 /*********************************************************************
828 int CDECL
fflush(FILE* file
)
831 flush_all_buffers(_IOWRT
);
832 } else if(file
->_flag
& _IOWRT
) {
836 res
= flush_buffer(file
);
838 if(!res && (file->_flag & _IOCOMMIT))
839 res = _commit(file->_file) ? EOF : 0;
844 } else if(file
->_flag
& _IOREAD
) {
847 file
->_ptr
= file
->_base
;
855 /*********************************************************************
858 int CDECL
_close(int fd
)
865 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
866 if (hand
== INVALID_HANDLE_VALUE
)
868 else if (!CloseHandle(hand
))
870 WARN(":failed-last error (%d)\n",GetLastError());
871 _dosmaperr(GetLastError());
884 /*********************************************************************
887 int CDECL
_commit(int fd
)
889 HANDLE hand
= fdtoh(fd
);
891 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
892 if (hand
== INVALID_HANDLE_VALUE
)
895 if (!FlushFileBuffers(hand
))
897 if (GetLastError() == ERROR_INVALID_HANDLE
)
899 /* FlushFileBuffers fails for console handles
900 * so we ignore this error.
904 TRACE(":failed-last error (%d)\n",GetLastError());
905 _dosmaperr(GetLastError());
912 /*********************************************************************
915 * MSDN isn't clear on this point, but the remarks for _pipe
916 * indicate file descriptors duplicated with _dup and _dup2 are always
919 int CDECL
_dup2(int od
, int nd
)
923 TRACE("(od=%d, nd=%d)\n", od
, nd
);
925 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
929 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
930 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
932 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
936 ret
= set_fd(handle
, wxflag
, nd
);
944 /* _dup2 returns 0, not nd, on success */
951 _dosmaperr(GetLastError());
963 /*********************************************************************
966 int CDECL
_dup(int od
)
972 if (_dup2(od
, fd
) == 0)
980 /*********************************************************************
983 int CDECL
_eof(int fd
)
986 LONG hcurpos
,hendpos
;
987 HANDLE hand
= fdtoh(fd
);
989 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
991 if (hand
== INVALID_HANDLE_VALUE
)
994 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
996 /* Otherwise we do it the hard way */
997 hcurpos
= hendpos
= 0;
998 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
999 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
1001 if (curpos
== endpos
&& hcurpos
== hendpos
)
1003 /* FIXME: shouldn't WX_ATEOF be set here? */
1007 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1011 /*********************************************************************
1012 * _fcloseall (MSVCRT.@)
1014 int CDECL
_fcloseall(void)
1016 int num_closed
= 0, i
;
1020 for (i
= 3; i
< stream_idx
; i
++) {
1023 if (file
->_flag
&& !fclose(file
))
1028 TRACE(":closed (%d) handles\n",num_closed
);
1032 /* free everything on process exit */
1033 void msvcrt_free_io(void)
1041 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1044 for(j
=0; j
<stream_idx
; j
++)
1046 FILE *file
= get_file(j
);
1047 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1049 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1050 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1054 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
1057 DeleteCriticalSection(&file_cs
);
1060 /*********************************************************************
1061 * _lseeki64 (MSVCRT.@)
1063 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1065 HANDLE hand
= fdtoh(fd
);
1068 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1069 if (hand
== INVALID_HANDLE_VALUE
)
1072 if (whence
< 0 || whence
> 2)
1078 TRACE(":fd (%d) to %s pos %s\n",
1079 fd
,wine_dbgstr_longlong(offset
),
1080 (whence
==SEEK_SET
)?"SEEK_SET":
1081 (whence
==SEEK_CUR
)?"SEEK_CUR":
1082 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1084 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1085 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1086 ofs
.QuadPart
= offset
;
1087 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1088 GetLastError() == ERROR_SUCCESS
)
1090 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1091 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1093 return ofs
.QuadPart
;
1095 TRACE(":error-last error (%d)\n",GetLastError());
1096 _dosmaperr(GetLastError());
1100 /*********************************************************************
1103 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1105 return (LONG
)_lseeki64(fd
, offset
, whence
);
1108 /*********************************************************************
1109 * _lock_file (MSVCRT.@)
1111 void CDECL
_lock_file(FILE *file
)
1113 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1114 _lock(_STREAM_LOCKS
+(file
-_iob
));
1115 /* ReactOS: string streams dont need to be locked */
1116 else if(!(file
->_flag
& _IOSTRG
))
1117 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1120 /*********************************************************************
1121 * _unlock_file (MSVCRT.@)
1123 void CDECL
_unlock_file(FILE *file
)
1125 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1126 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1127 /* ReactOS: string streams dont need to be locked */
1128 else if(!(file
->_flag
& _IOSTRG
))
1129 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1133 /*********************************************************************
1134 * _locking (MSVCRT.@)
1136 * This is untested; the underlying LockFile doesn't work yet.
1138 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1142 HANDLE hand
= fdtoh(fd
);
1144 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1145 if (hand
== INVALID_HANDLE_VALUE
)
1148 if (mode
< 0 || mode
> 4)
1154 TRACE(":fd (%d) by 0x%08x mode %s\n",
1155 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1156 (mode
==_LK_LOCK
)?"_LK_LOCK":
1157 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1158 (mode
==_LK_RLCK
)?"_LK_RLCK":
1159 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1162 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1164 FIXME ("Seek failed\n");
1165 *_errno() = EINVAL
; /* FIXME */
1168 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1171 ret
= 1; /* just to satisfy gcc */
1174 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1179 else if (mode
== _LK_UNLCK
)
1180 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1182 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1183 /* FIXME - what about error settings? */
1184 return ret
? 0 : -1;
1187 /*********************************************************************
1188 * _fseeki64 (MSVCRT.@)
1190 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1195 /* Flush output if needed */
1196 if(file
->_flag
& _IOWRT
)
1199 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1201 offset
+= _ftelli64(file
);
1204 /* Discard buffered input */
1206 file
->_ptr
= file
->_base
;
1207 /* Reset direction of i/o */
1208 if(file
->_flag
& _IORW
) {
1209 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1211 /* Clear end of file flag */
1212 file
->_flag
&= ~_IOEOF
;
1213 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1219 /*********************************************************************
1222 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1224 return _fseeki64( file
, offset
, whence
);
1227 /*********************************************************************
1228 * _chsize_s (MSVCRT.@)
1230 int CDECL
_chsize_s(int fd
, __int64 size
)
1236 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1238 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1243 if (handle
!= INVALID_HANDLE_VALUE
)
1245 /* save the current file pointer */
1246 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1249 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1252 ret
= SetEndOfFile(handle
);
1253 if (!ret
) _dosmaperr(GetLastError());
1256 /* restore the file pointer */
1257 _lseeki64(fd
, cur
, SEEK_SET
);
1262 return ret
? 0 : *_errno();
1265 /*********************************************************************
1266 * _chsize (MSVCRT.@)
1268 int CDECL
_chsize(int fd
, long size
)
1270 /* _chsize_s returns errno on failure but _chsize should return -1 */
1271 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1274 /*********************************************************************
1275 * clearerr (MSVCRT.@)
1277 void CDECL
clearerr(FILE* file
)
1279 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1282 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1286 /*********************************************************************
1289 void CDECL
rewind(FILE* file
)
1291 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1294 fseek(file
, 0L, SEEK_SET
);
1299 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1301 int plus
= strchrW(mode
, '+') != NULL
;
1303 TRACE("%s\n", debugstr_w(mode
));
1305 while(*mode
== ' ') mode
++;
1310 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1311 *stream_flags
= plus
? _IORW
: _IOREAD
;
1314 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1315 *stream_flags
= plus
? _IORW
: _IOWRT
;
1318 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1319 *stream_flags
= plus
? _IORW
: _IOWRT
;
1322 MSVCRT_INVALID_PMT(0, EINVAL
);
1326 *stream_flags
|= _commode
;
1328 while (*mode
&& *mode
!=',')
1332 *open_flags
|= _O_BINARY
;
1333 *open_flags
&= ~_O_TEXT
;
1336 *open_flags
|= _O_TEXT
;
1337 *open_flags
&= ~_O_BINARY
;
1340 *open_flags
|= _O_TEMPORARY
;
1343 *open_flags
|= _O_SHORT_LIVED
;
1346 *stream_flags
|= _IOCOMMIT
;
1349 *stream_flags
&= ~_IOCOMMIT
;
1352 *open_flags
|= _O_NOINHERIT
;
1361 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1364 ERR("incorrect mode flag: %c\n", mode
[-1]);
1370 static const WCHAR ccs
[] = {'c','c','s'};
1371 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1372 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1373 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1376 while(*mode
== ' ') mode
++;
1377 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1379 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1380 while(*mode
== ' ') mode
++;
1381 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1384 while(*mode
== ' ') mode
++;
1386 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1388 *open_flags
|= _O_U8TEXT
;
1389 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1391 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1393 *open_flags
|= _O_U16TEXT
;
1394 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1396 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1398 *open_flags
|= _O_WTEXT
;
1399 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1403 MSVCRT_INVALID_PMT(0, EINVAL
);
1407 while(*mode
== ' ') mode
++;
1410 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1415 /*********************************************************************
1416 * _fdopen (MSVCRT.@)
1418 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1421 wchar_t *modeW
= NULL
;
1423 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1425 ret
= _wfdopen(fd
, modeW
);
1431 /*********************************************************************
1432 * _wfdopen (MSVCRT.@)
1434 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1436 int open_flags
, stream_flags
;
1439 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1442 if (!(file
= alloc_fp()))
1444 else if (init_fp(file
, fd
, stream_flags
) == -1)
1449 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1455 /*********************************************************************
1456 * _filelength (MSVCRT.@)
1458 LONG CDECL
_filelength(int fd
)
1460 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1463 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1466 if (endPos
!= curPos
)
1467 _lseek(fd
, curPos
, SEEK_SET
);
1474 /*********************************************************************
1475 * _filelengthi64 (MSVCRT.@)
1477 __int64 CDECL
_filelengthi64(int fd
)
1479 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1482 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1485 if (endPos
!= curPos
)
1486 _lseeki64(fd
, curPos
, SEEK_SET
);
1493 /*********************************************************************
1494 * _fileno (MSVCRT.@)
1496 int CDECL
_fileno(FILE* file
)
1498 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1502 /*********************************************************************
1503 * _get_osfhandle (MSVCRT.@)
1505 intptr_t CDECL
_get_osfhandle(int fd
)
1507 HANDLE hand
= fdtoh(fd
);
1508 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1510 return (intptr_t)hand
;
1513 /*********************************************************************
1514 * _mktemp (MSVCRT.@)
1516 char * CDECL
_mktemp(char *pattern
)
1519 char *retVal
= pattern
;
1527 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1531 id
= GetCurrentProcessId();
1535 int tempNum
= id
/ 10;
1536 *pattern
-- = id
- (tempNum
* 10) + '0';
1542 *pattern
= letter
++;
1543 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1545 } while(letter
<= 'z');
1549 /*********************************************************************
1550 * _wmktemp (MSVCRT.@)
1552 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1555 wchar_t *retVal
= pattern
;
1557 wchar_t letter
= 'a';
1560 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1564 id
= GetCurrentProcessId();
1568 int tempNum
= id
/ 10;
1569 *pattern
-- = id
- (tempNum
* 10) + '0';
1575 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1576 GetLastError() == ERROR_FILE_NOT_FOUND
)
1578 *pattern
= letter
++;
1579 } while(letter
!= '|');
1583 /*static*/ unsigned split_oflags(unsigned oflags
)
1586 unsigned unsupp
; /* until we support everything */
1588 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1589 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1590 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1591 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1592 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1593 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1594 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1595 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1596 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1598 if ((unsupp
= oflags
& ~(
1599 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1600 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1601 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1603 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1604 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1606 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1611 /*********************************************************************
1614 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1617 SECURITY_ATTRIBUTES sa
;
1618 HANDLE readHandle
, writeHandle
;
1626 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1627 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1628 sa
.lpSecurityDescriptor
= NULL
;
1629 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1631 unsigned int wxflags
= split_oflags(textmode
);
1635 fd
= alloc_fd(readHandle
, wxflags
);
1639 fd
= alloc_fd(writeHandle
, wxflags
);
1648 CloseHandle(writeHandle
);
1654 CloseHandle(readHandle
);
1655 CloseHandle(writeHandle
);
1661 _dosmaperr(GetLastError());
1666 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1668 char bom
[sizeof(utf8_bom
)];
1671 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1673 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1676 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1677 oflags
|= _O_U8TEXT
;
1678 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1680 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1681 oflags
|= _O_U16TEXT
;
1683 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1689 /*********************************************************************
1690 * _wsopen_s (MSVCRT.@)
1692 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1694 DWORD access
= 0, creation
= 0, attrib
;
1695 SECURITY_ATTRIBUTES sa
;
1700 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1701 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1703 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1706 wxflag
= split_oflags(oflags
);
1707 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1709 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1710 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1711 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1714 if (oflags
& _O_CREAT
)
1716 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1717 FIXME(": pmode 0x%04x ignored\n", pmode
);
1719 WARN(": pmode 0x%04x ignored\n", pmode
);
1721 if (oflags
& _O_EXCL
)
1722 creation
= CREATE_NEW
;
1723 else if (oflags
& _O_TRUNC
)
1724 creation
= CREATE_ALWAYS
;
1726 creation
= OPEN_ALWAYS
;
1728 else /* no _O_CREAT */
1730 if (oflags
& _O_TRUNC
)
1731 creation
= TRUNCATE_EXISTING
;
1733 creation
= OPEN_EXISTING
;
1742 sharing
= FILE_SHARE_READ
;
1745 sharing
= FILE_SHARE_WRITE
;
1748 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1751 ERR( "Unhandled shflags 0x%x\n", shflags
);
1754 attrib
= FILE_ATTRIBUTE_NORMAL
;
1756 if (oflags
& _O_TEMPORARY
)
1758 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1760 sharing
|= FILE_SHARE_DELETE
;
1763 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1764 sa
.lpSecurityDescriptor
= NULL
;
1765 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1767 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1768 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1769 && !(access
&GENERIC_READ
))
1771 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1772 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1773 if (hand
!= INVALID_HANDLE_VALUE
)
1775 oflags
= check_bom(hand
, oflags
, FALSE
);
1779 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1782 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1783 if (hand
== INVALID_HANDLE_VALUE
) {
1784 WARN(":failed-last error (%d)\n",GetLastError());
1785 _dosmaperr(GetLastError());
1789 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1791 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1792 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1793 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1795 if (oflags
& _O_U8TEXT
)
1797 DWORD written
= 0, tmp
;
1799 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1800 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1802 if (written
!= sizeof(utf8_bom
)) {
1803 WARN("error writing BOM\n");
1805 _dosmaperr(GetLastError());
1811 DWORD written
= 0, tmp
;
1813 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1814 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1816 if (written
!= sizeof(utf16_bom
))
1818 WARN("error writing BOM\n");
1820 _dosmaperr(GetLastError());
1825 else if (access
& GENERIC_READ
)
1826 oflags
= check_bom(hand
, oflags
, TRUE
);
1829 *fd
= alloc_fd(hand
, wxflag
);
1833 if (oflags
& _O_WTEXT
)
1834 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1835 else if (oflags
& _O_U16TEXT
)
1836 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1837 else if (oflags
& _O_U8TEXT
)
1838 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1840 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1844 /*********************************************************************
1845 * _wsopen (MSVCRT.@)
1847 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1852 if (oflags
& _O_CREAT
)
1856 __ms_va_start(ap
, shflags
);
1857 pmode
= va_arg(ap
, int);
1863 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1867 /*********************************************************************
1868 * _sopen_s (MSVCRT.@)
1870 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1875 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1878 ret
= _wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1883 /*********************************************************************
1886 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1891 if (oflags
& _O_CREAT
)
1895 va_start(ap
, shflags
);
1896 pmode
= va_arg(ap
, int);
1902 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1906 /*********************************************************************
1909 int CDECL
_open( const char *path
, int flags
, ... )
1913 if (flags
& _O_CREAT
)
1916 va_start(ap
, flags
);
1917 pmode
= va_arg(ap
, int);
1919 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1922 return _sopen( path
, flags
, _SH_DENYNO
);
1925 /*********************************************************************
1928 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1932 if (flags
& _O_CREAT
)
1935 va_start(ap
, flags
);
1936 pmode
= va_arg(ap
, int);
1938 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1941 return _wsopen( path
, flags
, _SH_DENYNO
);
1944 /*********************************************************************
1947 int CDECL
_creat(const char *path
, int flags
)
1949 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1950 return _open(path
, usedFlags
);
1953 /*********************************************************************
1954 * _wcreat (MSVCRT.@)
1956 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1958 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1959 return _wopen(path
, usedFlags
);
1962 /*********************************************************************
1963 * _open_osfhandle (MSVCRT.@)
1965 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1970 /* _O_RDONLY (0) always matches, so set the read flag
1971 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1972 * file, so set the write flag. It also only sets _O_TEXT if it wants
1973 * text - it never sets _O_BINARY.
1975 /* don't let split_oflags() decide the mode if no mode is passed */
1976 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1977 oflags
|= _O_BINARY
;
1979 flags
= GetFileType((HANDLE
)handle
);
1980 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
1982 _dosmaperr(GetLastError());
1986 if (flags
== FILE_TYPE_CHAR
)
1988 else if (flags
== FILE_TYPE_PIPE
)
1992 flags
|= split_oflags(oflags
);
1994 fd
= alloc_fd((HANDLE
)handle
, flags
);
1995 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
1999 /*********************************************************************
2002 int CDECL
_rmtmp(void)
2004 int num_removed
= 0, i
;
2008 for (i
= 3; i
< stream_idx
; i
++) {
2011 if (file
->_tmpfname
)
2020 TRACE(":removed (%d) temp files\n",num_removed
);
2024 static inline int get_utf8_char_len(char ch
)
2026 if((ch
&0xf8) == 0xf0)
2028 else if((ch
&0xf0) == 0xe0)
2030 else if((ch
&0xe0) == 0xc0)
2035 /*********************************************************************
2036 * (internal) read_utf8
2038 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2040 ioinfo
*fdinfo
= get_ioinfo(fd
);
2041 HANDLE hand
= fdinfo
->handle
;
2042 char min_buf
[4], *readbuf
, lookahead
;
2043 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2045 /* make the buffer big enough to hold at least one character */
2046 /* read bytes have to fit to output and lookahead buffers */
2048 readbuf_size
= count
< 4 ? 4 : count
;
2049 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2054 if(fdinfo
->lookahead
[0] != '\n') {
2055 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2056 fdinfo
->lookahead
[0] = '\n';
2058 if(fdinfo
->lookahead
[1] != '\n') {
2059 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2060 fdinfo
->lookahead
[1] = '\n';
2062 if(fdinfo
->lookahead
[2] != '\n') {
2063 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2064 fdinfo
->lookahead
[2] = '\n';
2069 /* NOTE: this case is broken in native dll, reading
2070 * sometimes fails when small buffer is passed
2073 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2074 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2075 fdinfo
->wxflag
|= WX_ATEOF
;
2078 _dosmaperr(GetLastError());
2081 }else if(!num_read
) {
2082 fdinfo
->wxflag
|= WX_ATEOF
;
2088 char_len
= get_utf8_char_len(readbuf
[0]);
2090 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2094 if(readbuf
[0] == '\n')
2095 fdinfo
->wxflag
|= WX_READNL
;
2097 fdinfo
->wxflag
&= ~WX_READNL
;
2099 if(readbuf
[0] == 0x1a) {
2100 fdinfo
->wxflag
|= WX_ATEOF
;
2104 if(readbuf
[0] == '\r') {
2105 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2107 else if(lookahead
== '\n')
2111 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2112 fdinfo
->lookahead
[0] = lookahead
;
2114 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2119 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2120 _dosmaperr(GetLastError());
2127 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2130 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2131 fdinfo
->wxflag
|= WX_ATEOF
;
2132 if (readbuf
!= min_buf
) free(readbuf
);
2135 _dosmaperr(GetLastError());
2136 if (readbuf
!= min_buf
) free(readbuf
);
2139 }else if(!pos
&& !num_read
) {
2140 fdinfo
->wxflag
|= WX_ATEOF
;
2141 if (readbuf
!= min_buf
) free(readbuf
);
2146 if(readbuf
[0] == '\n')
2147 fdinfo
->wxflag
|= WX_READNL
;
2149 fdinfo
->wxflag
&= ~WX_READNL
;
2151 /* Find first byte of last character (may be incomplete) */
2152 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2153 if((readbuf
[i
]&0xc0) != 0x80)
2155 char_len
= get_utf8_char_len(readbuf
[i
]);
2156 if(char_len
+i
<= pos
)
2159 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
)) {
2161 fdinfo
->lookahead
[0] = readbuf
[i
];
2163 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2165 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2167 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2171 for(i
=0, j
=0; i
<pos
; i
++) {
2172 if(readbuf
[i
] == 0x1a) {
2173 fdinfo
->wxflag
|= WX_ATEOF
;
2177 /* strip '\r' if followed by '\n' */
2178 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2179 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2180 readbuf
[j
++] = '\r';
2181 }else if(lookahead
== '\n' && j
==0) {
2182 readbuf
[j
++] = '\n';
2184 if(lookahead
!= '\n')
2185 readbuf
[j
++] = '\r';
2187 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2188 fdinfo
->lookahead
[0] = lookahead
;
2190 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2192 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2193 readbuf
[j
++] = readbuf
[i
];
2198 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2199 _dosmaperr(GetLastError());
2200 if (readbuf
!= min_buf
) free(readbuf
);
2204 if (readbuf
!= min_buf
) free(readbuf
);
2208 /*********************************************************************
2211 * When reading \r as last character in text mode, read() positions
2212 * the file pointer on the \r character while getc() goes on to
2215 static int read_i(int fd
, void *buf
, unsigned int count
)
2217 DWORD num_read
, utf16
;
2218 char *bufstart
= buf
;
2219 HANDLE hand
= fdtoh(fd
);
2220 ioinfo
*fdinfo
= get_ioinfo(fd
);
2225 if (fdinfo
->wxflag
& WX_ATEOF
) {
2226 TRACE("already at EOF, returning 0\n");
2229 /* Don't trace small reads, it gets *very* annoying */
2231 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2232 if (hand
== INVALID_HANDLE_VALUE
)
2238 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2239 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2245 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2246 return read_utf8(fd
, buf
, count
);
2248 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2250 if (fdinfo
->lookahead
[0] != '\n')
2252 bufstart
[0] = fdinfo
->lookahead
[0];
2253 fdinfo
->lookahead
[0] = '\n';
2257 bufstart
[1] = fdinfo
->lookahead
[1];
2258 fdinfo
->lookahead
[1] = '\n';
2261 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2262 num_read
+= 1+utf16
;
2267 if(utf16
&& (num_read
&1))
2269 /* msvcr90 uses uninitialized value from the buffer in this case */
2270 /* msvcrt ignores additional data */
2271 ERR("got odd number of bytes in UTF16 mode\n");
2275 if (count
!= 0 && num_read
== 0)
2277 fdinfo
->wxflag
|= WX_ATEOF
;
2278 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2280 else if (fdinfo
->wxflag
& WX_TEXT
)
2284 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2285 fdinfo
->wxflag
|= WX_READNL
;
2287 fdinfo
->wxflag
&= ~WX_READNL
;
2289 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2291 /* in text mode, a ctrl-z signals EOF */
2292 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2294 fdinfo
->wxflag
|= WX_ATEOF
;
2295 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2299 /* in text mode, strip \r if followed by \n */
2300 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2305 lookahead
[1] = '\n';
2306 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2308 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2310 bufstart
[j
++] = '\n';
2311 if(utf16
) bufstart
[j
++] = 0;
2315 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2317 bufstart
[j
++] = '\r';
2318 if(utf16
) bufstart
[j
++] = 0;
2321 if (fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2323 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2325 bufstart
[j
++] = '\n';
2326 if (utf16
) bufstart
[j
++] = 0;
2330 fdinfo
->lookahead
[0] = lookahead
[0];
2331 fdinfo
->lookahead
[1] = lookahead
[1];
2335 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2340 bufstart
[j
++] = '\r';
2341 if(utf16
) bufstart
[j
++] = 0;
2344 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2345 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2347 bufstart
[j
++] = bufstart
[i
];
2348 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2356 if (GetLastError() == ERROR_BROKEN_PIPE
)
2358 TRACE(":end-of-pipe\n");
2359 fdinfo
->wxflag
|= WX_ATEOF
;
2364 TRACE(":failed-last error (%d)\n",GetLastError());
2370 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2374 /*********************************************************************
2377 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2380 num_read
= read_i(fd
, buf
, count
);
2384 /*********************************************************************
2385 * _setmode (MSVCRT.@)
2387 int CDECL
_setmode(int fd
,int mode
)
2389 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2390 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2393 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2394 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2399 if(mode
== _O_BINARY
) {
2400 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2401 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2405 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2407 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2408 else if(mode
== _O_U8TEXT
)
2409 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2411 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2417 /*********************************************************************
2420 long CDECL
_tell(int fd
)
2422 return _lseek(fd
, 0, SEEK_CUR
);
2425 /*********************************************************************
2426 * _telli64 (MSVCRT.@)
2428 __int64 CDECL
_telli64(int fd
)
2430 return _lseeki64(fd
, 0, SEEK_CUR
);
2433 /*********************************************************************
2434 * _tempnam (MSVCRT.@)
2436 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2438 char tmpbuf
[MAX_PATH
];
2439 const char *tmp_dir
= getenv("TMP");
2441 if (tmp_dir
) dir
= tmp_dir
;
2443 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2444 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2446 TRACE("got name (%s)\n",tmpbuf
);
2447 DeleteFileA(tmpbuf
);
2448 return _strdup(tmpbuf
);
2450 TRACE("failed (%d)\n",GetLastError());
2454 /*********************************************************************
2455 * _wtempnam (MSVCRT.@)
2457 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2459 wchar_t tmpbuf
[MAX_PATH
];
2461 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2462 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2464 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2465 DeleteFileW(tmpbuf
);
2466 return _wcsdup(tmpbuf
);
2468 TRACE("failed (%d)\n",GetLastError());
2472 /*********************************************************************
2475 int CDECL
_umask(int umask
)
2477 int old_umask
= umask
;
2478 TRACE("(%d)\n",umask
);
2479 MSVCRT_umask
= umask
;
2483 /*********************************************************************
2486 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2489 ioinfo
*info
= get_ioinfo(fd
);
2490 HANDLE hand
= info
->handle
;
2492 /* Don't trace small writes, it gets *very* annoying */
2495 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2497 if (hand
== INVALID_HANDLE_VALUE
)
2503 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2509 /* If appending, go to EOF */
2510 if (info
->wxflag
& WX_APPEND
)
2511 _lseek(fd
, 0, FILE_END
);
2513 if (!(info
->wxflag
& WX_TEXT
))
2515 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2516 && (num_written
== count
))
2518 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2519 hand
, GetLastError());
2524 unsigned int i
, j
, nr_lf
, size
;
2527 const char *s
= buf
, *buf_start
= buf
;
2529 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2531 /* find number of \n */
2532 for (nr_lf
=0, i
=0; i
<count
; i
++)
2538 if ((q
= p
= malloc(size
)))
2540 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2549 FIXME("Malloc failed\n");
2561 else if (info
->exflag
& EF_UTF16
)
2563 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2564 if (s
[i
]=='\n' && s
[i
+1]==0)
2569 if ((q
= p
= malloc(size
)))
2571 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2573 if (s
[i
]=='\n' && s
[i
+1]==0)
2584 FIXME("Malloc failed\n");
2600 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2601 if (s
[i
]=='\n' && s
[i
+1]==0)
2604 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2606 _dosmaperr(GetLastError());
2611 size
= conv_len
+nr_lf
;
2612 if((p
= malloc(count
+nr_lf
*2+size
)))
2614 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2616 if (s
[i
]=='\n' && s
[i
+1]==0)
2624 q
= p
+count
+nr_lf
*2;
2625 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2626 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2630 FIXME("Malloc failed\n");
2637 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2641 if (num_written
!= size
)
2643 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2644 fd
, hand
, GetLastError(), num_written
);
2646 return s
- buf_start
;
2654 /*********************************************************************
2657 int CDECL
_putw(int val
, FILE* file
)
2662 len
= _write(file
->_file
, &val
, sizeof(val
));
2663 if (len
== sizeof(val
)) {
2668 file
->_flag
|= _IOERR
;
2673 /*********************************************************************
2676 int CDECL
fclose(FILE* file
)
2682 free(file
->_tmpfname
);
2683 file
->_tmpfname
= NULL
;
2684 /* flush stdio buffers */
2685 if(file
->_flag
& _IOWRT
)
2687 if(file
->_flag
& _IOMYBUF
)
2690 r
=_close(file
->_file
);
2694 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2695 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2697 if(file
== get_file(stream_idx
-1)) {
2698 while(stream_idx
>3 && !file
->_flag
) {
2700 file
= get_file(stream_idx
-1);
2704 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2707 /*********************************************************************
2710 int CDECL
feof(FILE* file
)
2712 return file
->_flag
& _IOEOF
;
2715 /*********************************************************************
2718 int CDECL
ferror(FILE* file
)
2720 return file
->_flag
& _IOERR
;
2723 /*********************************************************************
2724 * _filbuf (MSVCRT.@)
2726 int CDECL
_filbuf(FILE* file
)
2731 if(file
->_flag
& _IOSTRG
) {
2736 /* Allocate buffer if needed */
2737 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
2740 if(!(file
->_flag
& _IOREAD
)) {
2741 if(file
->_flag
& _IORW
)
2742 file
->_flag
|= _IOREAD
;
2749 if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
2751 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2752 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2760 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2762 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2769 file
->_ptr
= file
->_base
+1;
2770 c
= *(unsigned char *)file
->_base
;
2776 /*********************************************************************
2779 int CDECL
fgetc(FILE* file
)
2787 i
= (unsigned char *)file
->_ptr
++;
2796 /*********************************************************************
2797 * _fgetchar (MSVCRT.@)
2799 int CDECL
_fgetchar(void)
2801 return fgetc(stdin
);
2804 /*********************************************************************
2807 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2810 char * buf_start
= s
;
2812 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2813 file
,file
->_file
,s
,size
);
2817 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2822 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2824 TRACE(":nothing read\n");
2828 if ((cc
!= EOF
) && (size
> 1))
2831 TRACE(":got %s\n", debugstr_a(buf_start
));
2836 /*********************************************************************
2839 wint_t CDECL
fgetwc(FILE* file
)
2846 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2847 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2850 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2859 char mbs
[MB_LEN_MAX
];
2865 if(isleadbyte((unsigned char)mbs
[0])) {
2876 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2884 /*********************************************************************
2887 int CDECL
_getw(FILE* file
)
2895 for (j
=0; j
<sizeof(int); j
++) {
2898 file
->_flag
|= _IOEOF
;
2909 /*********************************************************************
2912 wint_t CDECL
getwc(FILE* file
)
2914 return fgetwc(file
);
2917 /*********************************************************************
2918 * _fgetwchar (MSVCRT.@)
2920 wint_t CDECL
_fgetwchar(void)
2922 return fgetwc(stdin
);
2925 /*********************************************************************
2926 * getwchar (MSVCRT.@)
2928 wint_t CDECL
getwchar(void)
2930 return _fgetwchar();
2933 /*********************************************************************
2936 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2939 wchar_t * buf_start
= s
;
2941 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2942 file
,file
->_file
,s
,size
);
2946 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2951 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2953 TRACE(":nothing read\n");
2957 if ((cc
!= WEOF
) && (size
> 1))
2960 TRACE(":got %s\n", debugstr_w(buf_start
));
2965 /*********************************************************************
2968 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2970 size_t wrcnt
=size
* nmemb
;
2979 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2980 memcpy(file
->_ptr
, ptr
, pcnt
);
2985 ptr
= (const char*)ptr
+ pcnt
;
2986 } else if(!file
->_bufsiz
&& (file
->_flag
& _IONBF
)) {
2987 if(!(file
->_flag
& _IOWRT
)) {
2988 if(file
->_flag
& _IORW
)
2989 file
->_flag
|= _IOWRT
;
2994 if(_write(file
->_file
, ptr
, wrcnt
) <= 0) {
2995 file
->_flag
|= _IOERR
;
3001 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3005 ptr
= (const char*)ptr
+ 1;
3010 return written
/ size
;
3013 /*********************************************************************
3015 * FORKED for ReactOS, don't sync with Wine!
3017 * - http://jira.reactos.org/browse/CORE-6495
3018 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3020 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3022 /* If this is a real file stream (and not some temporary one for
3023 sprintf-like functions), check whether it is opened in text mode.
3024 In this case, we have to perform an implicit conversion to ANSI. */
3025 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
3027 /* Convert to multibyte in text mode */
3028 char mbc
[MB_LEN_MAX
];
3031 mb_return
= wctomb(mbc
, c
);
3036 /* Output all characters */
3037 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3042 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3049 /*********************************************************************
3050 * _fputwchar (MSVCRT.@)
3052 wint_t CDECL
_fputwchar(wint_t wc
)
3054 return fputwc(wc
, stdout
);
3057 /*********************************************************************
3058 * _wfsopen (MSVCRT.@)
3060 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3063 int open_flags
, stream_flags
, fd
;
3065 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3067 /* map mode string to open() flags. "man fopen" for possibilities. */
3068 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3072 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3075 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
3077 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3084 TRACE(":got (%p)\n",file
);
3085 if (fd
>= 0 && !file
)
3091 /*********************************************************************
3092 * _fsopen (MSVCRT.@)
3094 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3097 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3099 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3100 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3104 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3107 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3112 ret
= _wfsopen(pathW
, modeW
, share
);
3119 /*********************************************************************
3122 FILE * CDECL
fopen(const char *path
, const char *mode
)
3124 return _fsopen( path
, mode
, _SH_DENYNO
);
3127 /*********************************************************************
3128 * fopen_s (MSVCRT.@)
3130 int CDECL
fopen_s(FILE** pFile
,
3131 const char *filename
, const char *mode
)
3133 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
3134 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
3135 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
3137 *pFile
= fopen(filename
, mode
);
3144 /*********************************************************************
3145 * _wfopen (MSVCRT.@)
3147 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3149 return _wfsopen( path
, mode
, _SH_DENYNO
);
3152 /*********************************************************************
3153 * _wfopen_s (MSVCRT.@)
3155 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3156 const wchar_t *mode
)
3158 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3159 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3164 *pFile
= _wfopen(filename
, mode
);
3171 /* fputc calls _flsbuf which calls fputc */
3172 int CDECL
_flsbuf(int c
, FILE* file
);
3174 /*********************************************************************
3177 int CDECL
fputc(int c
, FILE* file
)
3187 res
= flush_buffer(file
);
3189 return res
? res
: c
;
3196 res
= _flsbuf(c
, file
);
3202 /*********************************************************************
3203 * _fputchar (MSVCRT.@)
3205 int CDECL
_fputchar(int c
)
3207 return fputc(c
, stdout
);
3210 /*********************************************************************
3213 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3215 size_t rcnt
=size
* nmemb
;
3224 /* first buffered data */
3226 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3227 memcpy(ptr
, file
->_ptr
, pcnt
);
3232 ptr
= (char*)ptr
+ pcnt
;
3233 } else if(!(file
->_flag
& _IOREAD
)) {
3234 if(file
->_flag
& _IORW
) {
3235 file
->_flag
|= _IOREAD
;
3242 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
3248 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& (file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
3249 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3250 file
->_ptr
= file
->_base
;
3251 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3252 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3253 if (i
> 0 && i
< file
->_cnt
) {
3254 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3255 file
->_flag
&= ~_IOEOF
;
3258 memcpy(ptr
, file
->_ptr
, i
);
3262 } else if (rcnt
> INT_MAX
) {
3263 i
= _read(file
->_file
, ptr
, INT_MAX
);
3264 } else if (rcnt
< BUFSIZ
) {
3265 i
= _read(file
->_file
, ptr
, rcnt
);
3267 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3271 ptr
= (char *)ptr
+i
;
3272 /* expose feof condition in the flags
3273 * MFC tests file->_flag for feof, and doesn't call feof())
3275 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3276 file
->_flag
|= _IOEOF
;
3279 file
->_flag
|= _IOERR
;
3290 /*********************************************************************
3291 * _wfreopen (MSVCRT.@)
3294 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3296 int open_flags
, stream_flags
, fd
;
3298 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3301 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3306 /* map mode string to open() flags. "man fopen" for possibilities. */
3307 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3311 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3314 else if (init_fp(file
, fd
, stream_flags
) == -1)
3317 WARN(":failed-last error (%d)\n",GetLastError());
3318 _dosmaperr(GetLastError());
3327 /*********************************************************************
3328 * freopen (MSVCRT.@)
3331 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3334 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3336 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3337 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3343 ret
= _wfreopen(pathW
, modeW
, file
);
3350 /*********************************************************************
3351 * fsetpos (MSVCRT.@)
3353 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3358 /* Note that all this has been lifted 'as is' from fseek */
3359 if(file
->_flag
& _IOWRT
)
3362 /* Discard buffered input */
3364 file
->_ptr
= file
->_base
;
3366 /* Reset direction of i/o */
3367 if(file
->_flag
& _IORW
) {
3368 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3371 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3376 /*********************************************************************
3377 * _ftelli64 (MSVCRT.@)
3379 __int64 CDECL
_ftelli64(FILE* file
)
3384 pos
= _telli64(file
->_file
);
3389 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
3390 if(file
->_flag
& _IOWRT
) {
3391 pos
+= file
->_ptr
- file
->_base
;
3393 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3396 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3400 } else if(!file
->_cnt
) { /* nothing to do */
3401 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3405 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3406 for(i
=0; i
<file
->_cnt
; i
++)
3407 if(file
->_ptr
[i
] == '\n')
3413 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3418 pos
-= file
->_bufsiz
;
3419 pos
+= file
->_ptr
- file
->_base
;
3421 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3422 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3425 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3436 /*********************************************************************
3439 LONG CDECL
ftell(FILE* file
)
3441 return (LONG
)_ftelli64(file
);
3444 /*********************************************************************
3445 * fgetpos (MSVCRT.@)
3447 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3449 *pos
= _ftelli64(file
);
3455 /*********************************************************************
3458 int CDECL
fputs(const char *s
, FILE* file
)
3460 size_t len
= strlen(s
);
3464 ret
= fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
3469 /*********************************************************************
3472 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3474 size_t i
, len
= strlenW(s
);
3479 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3480 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3485 tmp_buf
= add_std_buffer(file
);
3486 for (i
=0; i
<len
; i
++) {
3487 if(fputwc(s
[i
], file
) == WEOF
) {
3488 if(tmp_buf
) remove_std_buffer(file
);
3494 if(tmp_buf
) remove_std_buffer(file
);
3499 /*********************************************************************
3500 * getchar (MSVCRT.@)
3502 int CDECL
getchar(void)
3504 return fgetc(stdin
);
3507 /*********************************************************************
3510 int CDECL
getc(FILE* file
)
3515 /*********************************************************************
3518 char * CDECL
gets(char *buf
)
3521 char * buf_start
= buf
;
3524 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3526 if(cc
!= '\r') *buf
++ = (char)cc
;
3530 TRACE("got '%s'\n", buf_start
);
3531 _unlock_file(stdin
);
3535 /*********************************************************************
3538 wchar_t* CDECL
_getws(wchar_t* buf
)
3544 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3548 *buf
++ = (wchar_t)cc
;
3552 TRACE("got %s\n", debugstr_w(ws
));
3553 _unlock_file(stdin
);
3557 /*********************************************************************
3560 int CDECL
putc(int c
, FILE* file
)
3562 return fputc(c
, file
);
3565 /*********************************************************************
3566 * putchar (MSVCRT.@)
3568 int CDECL
putchar(int c
)
3570 return fputc(c
, stdout
);
3573 /*********************************************************************
3574 * _putwch (MSVCRT.@)
3576 wint_t CDECL
_putwch(wchar_t c
)
3578 return fputwc(c
, stdout
);
3581 /*********************************************************************
3584 int CDECL
puts(const char *s
)
3586 size_t len
= strlen(s
);
3590 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3591 _unlock_file(stdout
);
3595 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3596 _unlock_file(stdout
);
3600 /*********************************************************************
3603 int CDECL
_putws(const wchar_t *s
)
3605 static const wchar_t nl
= '\n';
3606 size_t len
= strlenW(s
);
3610 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3611 _unlock_file(stdout
);
3615 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3616 _unlock_file(stdout
);
3620 /*********************************************************************
3623 int CDECL
remove(const char *path
)
3625 TRACE("(%s)\n",path
);
3626 if (DeleteFileA(path
))
3628 TRACE(":failed (%d)\n",GetLastError());
3629 _dosmaperr(GetLastError());
3633 /*********************************************************************
3634 * _wremove (MSVCRT.@)
3636 int CDECL
_wremove(const wchar_t *path
)
3638 TRACE("(%s)\n",debugstr_w(path
));
3639 if (DeleteFileW(path
))
3641 TRACE(":failed (%d)\n",GetLastError());
3642 _dosmaperr(GetLastError());
3646 /*********************************************************************
3649 int CDECL
rename(const char *oldpath
,const char *newpath
)
3651 TRACE(":from %s to %s\n",oldpath
,newpath
);
3652 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3654 TRACE(":failed (%d)\n",GetLastError());
3655 _dosmaperr(GetLastError());
3659 /*********************************************************************
3660 * _wrename (MSVCRT.@)
3662 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3664 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3665 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3667 TRACE(":failed (%d)\n",GetLastError());
3668 _dosmaperr(GetLastError());
3672 /*********************************************************************
3673 * setvbuf (MSVCRT.@)
3675 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3683 if(mode
== _IOFBF
) {
3684 file
->_flag
&= ~_IONBF
;
3685 file
->_base
= file
->_ptr
= buf
;
3687 file
->_bufsiz
= size
;
3690 file
->_flag
|= _IONBF
;
3696 /*********************************************************************
3699 void CDECL
setbuf(FILE* file
, char *buf
)
3701 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3704 /*********************************************************************
3707 char * CDECL
tmpnam(char *s
)
3714 thread_data_t
*data
= msvcrt_get_thread_data();
3716 if(!data
->tmpnam_buffer
)
3717 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3719 s
= data
->tmpnam_buffer
;
3722 int_to_base32(GetCurrentProcessId(), tmpstr
);
3723 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3724 for (count
= 0; count
< TMP_MAX
; count
++)
3726 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3727 memcpy(p
, tmpstr
, size
);
3729 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3730 GetLastError() == ERROR_FILE_NOT_FOUND
)
3736 /*********************************************************************
3737 * _wtmpnam (MSVCRT.@)
3739 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3741 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3746 thread_data_t
*data
= msvcrt_get_thread_data();
3748 if(!data
->wtmpnam_buffer
)
3749 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3751 s
= data
->wtmpnam_buffer
;
3754 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3755 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3756 for (count
= 0; count
< TMP_MAX
; count
++)
3758 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3759 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3761 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3762 GetLastError() == ERROR_FILE_NOT_FOUND
)
3768 /*********************************************************************
3769 * tmpfile (MSVCRT.@)
3771 FILE* CDECL
tmpfile(void)
3773 char *filename
= tmpnam(NULL
);
3778 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3779 _S_IREAD
| _S_IWRITE
);
3780 if (fd
!= -1 && (file
= alloc_fp()))
3782 if (init_fp(file
, fd
, _IORW
) == -1)
3787 else file
->_tmpfname
= _strdup(filename
);
3790 if(fd
!= -1 && !file
)
3796 /*********************************************************************
3799 int CDECL
ungetc(int c
, FILE * file
)
3805 if(file
->_bufsiz
== 0 && alloc_buffer(file
))
3807 if(file
->_ptr
>file
->_base
) {
3809 if(file
->_flag
& _IOSTRG
) {
3810 if(*file
->_ptr
!= c
) {
3828 /*********************************************************************
3829 * ungetwc (MSVCRT.@)
3831 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3840 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3841 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3842 unsigned char * pp
= (unsigned char *)&mwc
;
3845 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3846 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3852 char mbs
[MB_LEN_MAX
];
3855 len
= wctomb(mbs
, mwc
);
3861 for(len
--; len
>=0; len
--) {
3862 if(mbs
[len
] != ungetc(mbs
[len
], file
)) {
3875 /*********************************************************************
3876 * _getmaxstdio (MSVCRT.@)
3878 int CDECL
_getmaxstdio(void)
3883 /*********************************************************************
3884 * _setmaxstdio (MSVCRT.@)
3886 int CDECL
_setmaxstdio(int newmax
)
3888 TRACE("%d\n", newmax
);
3890 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3893 max_streams
= newmax
;