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 /* ioinfo structure size is different in msvcrXX.dll's */
116 unsigned char wxflag
;
119 CRITICAL_SECTION crit
;
122 /*********************************************************************
123 * __pioinfo (MSVCRT.@)
124 * array of pointers to ioinfo arrays [64]
126 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
128 /*********************************************************************
129 * __badioinfo (MSVCRT.@)
131 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
133 static int fdstart
= 3; /* first unallocated fd */
134 static int fdend
= 3; /* highest allocated fd */
138 CRITICAL_SECTION crit
;
141 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
142 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
143 static int max_streams
= 512, stream_idx
;
145 /* INTERNAL: process umask */
146 static int MSVCRT_umask
= 0;
148 /* INTERNAL: static data for tmpnam and _wtmpname functions */
149 static int tmpnam_unique
;
151 /* This critical section protects the tables __pioinfo and fstreams,
152 * and their related indexes, fdstart, fdend,
153 * and stream_idx, from race conditions.
154 * It doesn't protect against race conditions manipulating the underlying files
155 * or flags; doing so would probably be better accomplished with per-file
156 * protection, rather than locking the whole table for every change.
158 static CRITICAL_SECTION file_cs
;
159 static CRITICAL_SECTION_DEBUG file_cs_debug
=
162 { &file_cs_debug
.ProcessLocksList
, &file_cs_debug
.ProcessLocksList
},
163 0, 0, { (DWORD_PTR
)(__FILE__
": file_cs") }
165 static CRITICAL_SECTION file_cs
= { &file_cs_debug
, -1, 0, 0, 0, 0 };
166 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
167 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
169 static inline ioinfo
* get_ioinfo(int fd
)
173 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
177 return ret
+ (fd
%FD_BLOCK_SIZE
);
180 static inline FILE* get_file(int i
)
190 ret
= fstream
[i
/FD_BLOCK_SIZE
];
192 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
193 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
194 ERR("out of memory\n");
199 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
201 ret
+= i
%FD_BLOCK_SIZE
;
206 static inline BOOL
is_valid_fd(int fd
)
208 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
211 /* INTERNAL: Get the HANDLE for a fd
212 * This doesn't lock the table, because a failure will result in
213 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
214 * it returns a valid handle which is about to be closed, a subsequent call
215 * will fail, most likely in a sane way.
217 /*static*/ HANDLE
fdtoh(int fd
)
219 if (!is_valid_fd(fd
))
221 WARN(":fd (%d) - no handle!\n",fd
);
224 return INVALID_HANDLE_VALUE
;
226 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
227 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
228 return get_ioinfo(fd
)->handle
;
231 /* INTERNAL: free a file entry fd */
232 static void free_fd(int fd
)
238 fdinfo
= get_ioinfo(fd
);
239 old_handle
= fdinfo
->handle
;
240 if(fdinfo
!= &__badioinfo
)
242 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
245 TRACE(":fd (%d) freed\n",fd
);
246 if (fd
< 3) /* don't use 0,1,2 for user files */
251 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
254 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
257 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
271 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
272 /* caller must hold the files lock */
273 static int set_fd(HANDLE hand
, int flag
, int fd
)
279 WARN(":files exhausted!\n");
284 fdinfo
= get_ioinfo(fd
);
285 if(fdinfo
== &__badioinfo
) {
288 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
289 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
290 WARN(":out of memory!\n");
295 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
296 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
298 fdinfo
= get_ioinfo(fd
);
301 fdinfo
->handle
= hand
;
302 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_NOSEEK
));
303 fdinfo
->lookahead
[0] = '\n';
304 fdinfo
->lookahead
[1] = '\n';
305 fdinfo
->lookahead
[2] = '\n';
308 /* locate next free slot */
309 if (fd
== fdstart
&& fd
== fdend
)
312 while (fdstart
< fdend
&&
313 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
315 /* update last fd in use */
318 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
322 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
323 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
324 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
330 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
331 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
336 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
337 ret
= set_fd(hand
, flag
, fdstart
);
342 /* INTERNAL: Allocate a FILE* for an fd slot */
343 /* caller must hold the files lock */
344 static FILE* alloc_fp(void)
349 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
355 if (file
->_flag
== 0)
357 if (i
== stream_idx
) stream_idx
++;
365 /* INTERNAL: initialize a FILE* from an open fd */
366 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
368 TRACE(":fd (%d) allocating FILE*\n",fd
);
369 if (!is_valid_fd(fd
))
371 WARN(":invalid fd %d\n",fd
);
376 memset(file
, 0, sizeof(*file
));
378 file
->_flag
= stream_flags
;
380 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
381 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
383 TRACE(":got FILE* (%p)\n",file
);
387 /* INTERNAL: Create an inheritance data block (for spawned process)
388 * The inheritance block is made of:
389 * 00 int nb of file descriptor (NBFD)
390 * 04 char file flags (wxflag): repeated for each fd
391 * 4+NBFD HANDLE file handle: repeated for each fd
393 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
400 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
401 *block
= calloc(*size
, 1);
407 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
408 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
410 *(unsigned*)*block
= fdend
;
411 for (fd
= 0; fd
< fdend
; fd
++)
413 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
414 fdinfo
= get_ioinfo(fd
);
415 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
417 *wxflag_ptr
= fdinfo
->wxflag
;
418 *handle_ptr
= fdinfo
->handle
;
423 *handle_ptr
= INVALID_HANDLE_VALUE
;
425 wxflag_ptr
++; handle_ptr
++;
430 /* INTERNAL: Set up all file descriptors,
431 * as well as default streams (stdin, stderr and stdout)
433 void msvcrt_init_io(void)
439 GetStartupInfoA(&si
);
440 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
446 count
= *(unsigned*)si
.lpReserved2
;
447 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
448 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
450 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
451 count
= min(count
, MAX_FILES
);
452 for (i
= 0; i
< count
; i
++)
454 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
455 set_fd(*handle_ptr
, *wxflag_ptr
, i
);
457 wxflag_ptr
++; handle_ptr
++;
459 fdend
= max( 3, count
);
460 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
461 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
464 fdinfo
= get_ioinfo(STDIN_FILENO
);
465 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
466 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
467 DWORD type
= GetFileType(h
);
469 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
470 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDIN_FILENO
);
473 fdinfo
= get_ioinfo(STDOUT_FILENO
);
474 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
475 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
476 DWORD type
= GetFileType(h
);
478 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
479 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDOUT_FILENO
);
482 fdinfo
= get_ioinfo(STDERR_FILENO
);
483 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
484 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
485 DWORD type
= GetFileType(h
);
487 set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_NOSEEK
: 0)
488 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDERR_FILENO
);
491 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(STDIN_FILENO
)->handle
,
492 get_ioinfo(STDOUT_FILENO
)->handle
,
493 get_ioinfo(STDERR_FILENO
)->handle
);
495 memset(_iob
,0,3*sizeof(FILE));
496 for (i
= 0; i
< 3; i
++)
498 /* FILE structs for stdin/out/err are static and never deleted */
500 _iob
[i
]._tmpfname
= NULL
;
501 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
506 /* INTERNAL: Flush stdio file buffer */
507 static int flush_buffer(FILE* file
)
510 int cnt
=file
->_ptr
-file
->_base
;
511 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
512 file
->_flag
|= _IOERR
;
515 file
->_ptr
=file
->_base
;
516 file
->_cnt
=file
->_bufsiz
;
521 /*********************************************************************
524 int CDECL
_isatty(int fd
)
526 HANDLE hand
= fdtoh(fd
);
528 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
529 if (hand
== INVALID_HANDLE_VALUE
)
532 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
535 /* INTERNAL: Allocate stdio file buffer */
536 /*static*/ BOOL
alloc_buffer(FILE* file
)
538 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
539 && _isatty(file
->_file
))
542 file
->_base
= calloc(BUFSIZ
,1);
544 file
->_bufsiz
= BUFSIZ
;
545 file
->_flag
|= _IOMYBUF
;
547 file
->_base
= (char*)(&file
->_charbuf
);
549 file
->_bufsiz
= sizeof(file
->_charbuf
);
551 file
->_ptr
= file
->_base
;
556 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
557 static BOOL
add_std_buffer(FILE *file
)
559 static char buffers
[2][BUFSIZ
];
561 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
562 || !_isatty(file
->_file
) || file
->_bufsiz
)
565 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
566 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
570 /* INTERNAL: Removes temporary buffer from stdout or stderr */
571 /* Only call this function when add_std_buffer returned TRUE */
572 static void remove_std_buffer(FILE *file
)
575 file
->_ptr
= file
->_base
= NULL
;
576 file
->_bufsiz
= file
->_cnt
= 0;
579 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
580 static int int_to_base32(int num
, char *str
)
595 *p
= (num
& 31) + '0';
597 *p
+= ('a' - '0' - 10);
604 /* INTERNAL: wide character version of int_to_base32 */
605 static int int_to_base32_w(int num
, wchar_t *str
)
620 *p
= (num
& 31) + '0';
622 *p
+= ('a' - '0' - 10);
629 /* INTERNAL: Create a wide string from an ascii string */
630 wchar_t *msvcrt_wstrdupa(const char *str
)
632 const unsigned int len
= strlen(str
) + 1 ;
633 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
636 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
640 /*********************************************************************
641 * __iob_func(MSVCRT.@)
643 FILE * CDECL
__iob_func(void)
648 /*********************************************************************
651 int CDECL
_access(const char *filename
, int mode
)
653 DWORD attr
= GetFileAttributesA(filename
);
655 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
657 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
659 _dosmaperr(GetLastError());
662 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
664 _set_errno(ERROR_ACCESS_DENIED
);
670 /*********************************************************************
671 * _access_s (MSVCRT.@)
673 int CDECL
_access_s(const char *filename
, int mode
)
675 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
676 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
682 return _access(filename
, mode
);
685 /*********************************************************************
686 * _waccess (MSVCRT.@)
688 int CDECL
_waccess(const wchar_t *filename
, int mode
)
690 DWORD attr
= GetFileAttributesW(filename
);
692 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
694 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
696 _dosmaperr(GetLastError());
699 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
701 _set_errno(ERROR_ACCESS_DENIED
);
707 /*********************************************************************
708 * _waccess_s (MSVCRT.@)
710 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
712 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
713 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
719 return _waccess(filename
, mode
);
722 /*********************************************************************
725 int CDECL
_chmod(const char *path
, int flags
)
727 DWORD oldFlags
= GetFileAttributesA(path
);
729 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
731 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
732 oldFlags
| FILE_ATTRIBUTE_READONLY
;
734 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
737 _dosmaperr(GetLastError());
741 /*********************************************************************
744 int CDECL
_wchmod(const wchar_t *path
, int flags
)
746 DWORD oldFlags
= GetFileAttributesW(path
);
748 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
750 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
751 oldFlags
| FILE_ATTRIBUTE_READONLY
;
753 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
756 _dosmaperr(GetLastError());
760 /*********************************************************************
763 int CDECL
_unlink(const char *path
)
765 TRACE("%s\n",debugstr_a(path
));
766 if(DeleteFileA(path
))
768 TRACE("failed (%d)\n",GetLastError());
769 _dosmaperr(GetLastError());
773 /*********************************************************************
774 * _wunlink (MSVCRT.@)
776 int CDECL
_wunlink(const wchar_t *path
)
778 TRACE("(%s)\n",debugstr_w(path
));
779 if(DeleteFileW(path
))
781 TRACE("failed (%d)\n",GetLastError());
782 _dosmaperr(GetLastError());
786 /* _flushall calls fflush which calls _flushall */
787 int CDECL
fflush(FILE* file
);
789 /* INTERNAL: Flush all stream buffer */
790 static int flush_all_buffers(int mask
)
792 int i
, num_flushed
= 0;
796 for (i
= 3; i
< stream_idx
; i
++) {
801 if(file
->_flag
& mask
) {
809 TRACE(":flushed (%d) handles\n",num_flushed
);
813 /*********************************************************************
814 * _flushall (MSVCRT.@)
816 int CDECL
_flushall(void)
818 return flush_all_buffers(_IOWRT
| _IOREAD
);
821 /*********************************************************************
824 int CDECL
fflush(FILE* file
)
827 flush_all_buffers(_IOWRT
);
828 } else if(file
->_flag
& _IOWRT
) {
832 res
= flush_buffer(file
);
834 if(!res && (file->_flag & _IOCOMMIT))
835 res = _commit(file->_file) ? EOF : 0;
840 } else if(file
->_flag
& _IOREAD
) {
843 file
->_ptr
= file
->_base
;
851 /*********************************************************************
854 int CDECL
_close(int fd
)
861 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
862 if (hand
== INVALID_HANDLE_VALUE
)
864 else if (!CloseHandle(hand
))
866 WARN(":failed-last error (%d)\n",GetLastError());
867 _dosmaperr(GetLastError());
880 /*********************************************************************
883 int CDECL
_commit(int fd
)
885 HANDLE hand
= fdtoh(fd
);
887 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
888 if (hand
== INVALID_HANDLE_VALUE
)
891 if (!FlushFileBuffers(hand
))
893 if (GetLastError() == ERROR_INVALID_HANDLE
)
895 /* FlushFileBuffers fails for console handles
896 * so we ignore this error.
900 TRACE(":failed-last error (%d)\n",GetLastError());
901 _dosmaperr(GetLastError());
908 /*********************************************************************
911 * MSDN isn't clear on this point, but the remarks for _pipe
912 * indicate file descriptors duplicated with _dup and _dup2 are always
915 int CDECL
_dup2(int od
, int nd
)
919 TRACE("(od=%d, nd=%d)\n", od
, nd
);
921 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
925 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
926 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
928 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
932 ret
= set_fd(handle
, wxflag
, nd
);
940 /* _dup2 returns 0, not nd, on success */
947 _dosmaperr(GetLastError());
959 /*********************************************************************
962 int CDECL
_dup(int od
)
968 if (_dup2(od
, fd
) == 0)
976 /*********************************************************************
979 int CDECL
_eof(int fd
)
982 LONG hcurpos
,hendpos
;
983 HANDLE hand
= fdtoh(fd
);
985 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
987 if (hand
== INVALID_HANDLE_VALUE
)
990 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
992 /* Otherwise we do it the hard way */
993 hcurpos
= hendpos
= 0;
994 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
995 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
997 if (curpos
== endpos
&& hcurpos
== hendpos
)
999 /* FIXME: shouldn't WX_ATEOF be set here? */
1003 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
1007 /*********************************************************************
1008 * _fcloseall (MSVCRT.@)
1010 int CDECL
_fcloseall(void)
1012 int num_closed
= 0, i
;
1016 for (i
= 3; i
< stream_idx
; i
++) {
1019 if (file
->_flag
&& !fclose(file
))
1024 TRACE(":closed (%d) handles\n",num_closed
);
1028 /* free everything on process exit */
1029 void msvcrt_free_io(void)
1037 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1040 for(j
=0; j
<stream_idx
; j
++)
1042 FILE *file
= get_file(j
);
1043 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1045 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1046 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1050 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
1053 DeleteCriticalSection(&file_cs
);
1056 /*********************************************************************
1057 * _lseeki64 (MSVCRT.@)
1059 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1061 HANDLE hand
= fdtoh(fd
);
1064 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1065 if (hand
== INVALID_HANDLE_VALUE
)
1068 if (whence
< 0 || whence
> 2)
1074 TRACE(":fd (%d) to %s pos %s\n",
1075 fd
,wine_dbgstr_longlong(offset
),
1076 (whence
==SEEK_SET
)?"SEEK_SET":
1077 (whence
==SEEK_CUR
)?"SEEK_CUR":
1078 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1080 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1081 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1082 ofs
.QuadPart
= offset
;
1083 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1084 GetLastError() == ERROR_SUCCESS
)
1086 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1087 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1089 return ofs
.QuadPart
;
1091 TRACE(":error-last error (%d)\n",GetLastError());
1092 _dosmaperr(GetLastError());
1096 /*********************************************************************
1099 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1101 return (LONG
)_lseeki64(fd
, offset
, whence
);
1104 /*********************************************************************
1105 * _lock_file (MSVCRT.@)
1107 void CDECL
_lock_file(FILE *file
)
1109 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1110 _lock(_STREAM_LOCKS
+(file
-_iob
));
1111 /* ReactOS: string streams dont need to be locked */
1112 else if(!(file
->_flag
& _IOSTRG
))
1113 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1116 /*********************************************************************
1117 * _unlock_file (MSVCRT.@)
1119 void CDECL
_unlock_file(FILE *file
)
1121 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1122 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1123 /* ReactOS: string streams dont need to be locked */
1124 else if(!(file
->_flag
& _IOSTRG
))
1125 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1129 /*********************************************************************
1130 * _locking (MSVCRT.@)
1132 * This is untested; the underlying LockFile doesn't work yet.
1134 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1138 HANDLE hand
= fdtoh(fd
);
1140 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1141 if (hand
== INVALID_HANDLE_VALUE
)
1144 if (mode
< 0 || mode
> 4)
1150 TRACE(":fd (%d) by 0x%08x mode %s\n",
1151 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1152 (mode
==_LK_LOCK
)?"_LK_LOCK":
1153 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1154 (mode
==_LK_RLCK
)?"_LK_RLCK":
1155 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1158 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1160 FIXME ("Seek failed\n");
1161 *_errno() = EINVAL
; /* FIXME */
1164 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1167 ret
= 1; /* just to satisfy gcc */
1170 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1175 else if (mode
== _LK_UNLCK
)
1176 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1178 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1179 /* FIXME - what about error settings? */
1180 return ret
? 0 : -1;
1183 /*********************************************************************
1184 * _fseeki64 (MSVCRT.@)
1186 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1191 /* Flush output if needed */
1192 if(file
->_flag
& _IOWRT
)
1195 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1197 offset
+= _ftelli64(file
);
1200 /* Discard buffered input */
1202 file
->_ptr
= file
->_base
;
1203 /* Reset direction of i/o */
1204 if(file
->_flag
& _IORW
) {
1205 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1207 /* Clear end of file flag */
1208 file
->_flag
&= ~_IOEOF
;
1209 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1215 /*********************************************************************
1218 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1220 return _fseeki64( file
, offset
, whence
);
1223 /*********************************************************************
1224 * _chsize_s (MSVCRT.@)
1226 int CDECL
_chsize_s(int fd
, __int64 size
)
1232 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1234 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1239 if (handle
!= INVALID_HANDLE_VALUE
)
1241 /* save the current file pointer */
1242 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1245 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1248 ret
= SetEndOfFile(handle
);
1249 if (!ret
) _dosmaperr(GetLastError());
1252 /* restore the file pointer */
1253 _lseeki64(fd
, cur
, SEEK_SET
);
1258 return ret
? 0 : *_errno();
1261 /*********************************************************************
1262 * _chsize (MSVCRT.@)
1264 int CDECL
_chsize(int fd
, long size
)
1266 /* _chsize_s returns errno on failure but _chsize should return -1 */
1267 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1270 /*********************************************************************
1271 * clearerr (MSVCRT.@)
1273 void CDECL
clearerr(FILE* file
)
1275 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1278 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1282 /*********************************************************************
1285 void CDECL
rewind(FILE* file
)
1287 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1290 fseek(file
, 0L, SEEK_SET
);
1295 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1297 int plus
= strchrW(mode
, '+') != NULL
;
1299 TRACE("%s\n", debugstr_w(mode
));
1301 while(*mode
== ' ') mode
++;
1306 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1307 *stream_flags
= plus
? _IORW
: _IOREAD
;
1310 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1311 *stream_flags
= plus
? _IORW
: _IOWRT
;
1314 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1315 *stream_flags
= plus
? _IORW
: _IOWRT
;
1318 MSVCRT_INVALID_PMT(0, EINVAL
);
1322 *stream_flags
|= _commode
;
1324 while (*mode
&& *mode
!=',')
1328 *open_flags
|= _O_BINARY
;
1329 *open_flags
&= ~_O_TEXT
;
1332 *open_flags
|= _O_TEXT
;
1333 *open_flags
&= ~_O_BINARY
;
1336 *open_flags
|= _O_TEMPORARY
;
1339 *open_flags
|= _O_SHORT_LIVED
;
1342 *stream_flags
|= _IOCOMMIT
;
1345 *stream_flags
&= ~_IOCOMMIT
;
1348 *open_flags
|= _O_NOINHERIT
;
1357 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1360 ERR("incorrect mode flag: %c\n", mode
[-1]);
1366 static const WCHAR ccs
[] = {'c','c','s'};
1367 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1368 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1369 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1372 while(*mode
== ' ') mode
++;
1373 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1375 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1376 while(*mode
== ' ') mode
++;
1377 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1380 while(*mode
== ' ') mode
++;
1382 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1384 *open_flags
|= _O_U8TEXT
;
1385 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1387 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1389 *open_flags
|= _O_U16TEXT
;
1390 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1392 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1394 *open_flags
|= _O_WTEXT
;
1395 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1399 MSVCRT_INVALID_PMT(0, EINVAL
);
1403 while(*mode
== ' ') mode
++;
1406 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1411 /*********************************************************************
1412 * _fdopen (MSVCRT.@)
1414 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1417 wchar_t *modeW
= NULL
;
1419 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1421 ret
= _wfdopen(fd
, modeW
);
1427 /*********************************************************************
1428 * _wfdopen (MSVCRT.@)
1430 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1432 int open_flags
, stream_flags
;
1435 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1438 if (!(file
= alloc_fp()))
1440 else if (init_fp(file
, fd
, stream_flags
) == -1)
1445 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1451 /*********************************************************************
1452 * _filelength (MSVCRT.@)
1454 LONG CDECL
_filelength(int fd
)
1456 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1459 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1462 if (endPos
!= curPos
)
1463 _lseek(fd
, curPos
, SEEK_SET
);
1470 /*********************************************************************
1471 * _filelengthi64 (MSVCRT.@)
1473 __int64 CDECL
_filelengthi64(int fd
)
1475 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1478 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1481 if (endPos
!= curPos
)
1482 _lseeki64(fd
, curPos
, SEEK_SET
);
1489 /*********************************************************************
1490 * _fileno (MSVCRT.@)
1492 int CDECL
_fileno(FILE* file
)
1494 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1498 /*********************************************************************
1499 * _get_osfhandle (MSVCRT.@)
1501 intptr_t CDECL
_get_osfhandle(int fd
)
1503 HANDLE hand
= fdtoh(fd
);
1504 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1506 return (intptr_t)hand
;
1509 /*********************************************************************
1510 * _mktemp (MSVCRT.@)
1512 char * CDECL
_mktemp(char *pattern
)
1515 char *retVal
= pattern
;
1523 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1527 id
= GetCurrentProcessId();
1531 int tempNum
= id
/ 10;
1532 *pattern
-- = id
- (tempNum
* 10) + '0';
1538 *pattern
= letter
++;
1539 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1541 } while(letter
<= 'z');
1545 /*********************************************************************
1546 * _wmktemp (MSVCRT.@)
1548 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1551 wchar_t *retVal
= pattern
;
1553 wchar_t letter
= 'a';
1556 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1560 id
= GetCurrentProcessId();
1564 int tempNum
= id
/ 10;
1565 *pattern
-- = id
- (tempNum
* 10) + '0';
1571 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1572 GetLastError() == ERROR_FILE_NOT_FOUND
)
1574 *pattern
= letter
++;
1575 } while(letter
!= '|');
1579 /*static*/ unsigned split_oflags(unsigned oflags
)
1582 unsigned unsupp
; /* until we support everything */
1584 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1585 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1586 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1587 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1588 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1589 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1590 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1591 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1592 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1594 if ((unsupp
= oflags
& ~(
1595 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1596 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1597 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1599 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1600 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1602 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1607 /*********************************************************************
1610 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1613 SECURITY_ATTRIBUTES sa
;
1614 HANDLE readHandle
, writeHandle
;
1622 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1623 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1624 sa
.lpSecurityDescriptor
= NULL
;
1625 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1627 unsigned int wxflags
= split_oflags(textmode
);
1631 fd
= alloc_fd(readHandle
, wxflags
);
1635 fd
= alloc_fd(writeHandle
, wxflags
);
1644 CloseHandle(writeHandle
);
1650 CloseHandle(readHandle
);
1651 CloseHandle(writeHandle
);
1657 _dosmaperr(GetLastError());
1662 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1664 char bom
[sizeof(utf8_bom
)];
1667 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1669 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1672 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1673 oflags
|= _O_U8TEXT
;
1674 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1676 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1677 oflags
|= _O_U16TEXT
;
1679 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1685 /*********************************************************************
1686 * _wsopen_s (MSVCRT.@)
1688 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1690 DWORD access
= 0, creation
= 0, attrib
;
1691 SECURITY_ATTRIBUTES sa
;
1696 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1697 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1699 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1702 wxflag
= split_oflags(oflags
);
1703 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1705 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1706 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1707 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1710 if (oflags
& _O_CREAT
)
1712 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1713 FIXME(": pmode 0x%04x ignored\n", pmode
);
1715 WARN(": pmode 0x%04x ignored\n", pmode
);
1717 if (oflags
& _O_EXCL
)
1718 creation
= CREATE_NEW
;
1719 else if (oflags
& _O_TRUNC
)
1720 creation
= CREATE_ALWAYS
;
1722 creation
= OPEN_ALWAYS
;
1724 else /* no _O_CREAT */
1726 if (oflags
& _O_TRUNC
)
1727 creation
= TRUNCATE_EXISTING
;
1729 creation
= OPEN_EXISTING
;
1738 sharing
= FILE_SHARE_READ
;
1741 sharing
= FILE_SHARE_WRITE
;
1744 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1747 ERR( "Unhandled shflags 0x%x\n", shflags
);
1750 attrib
= FILE_ATTRIBUTE_NORMAL
;
1752 if (oflags
& _O_TEMPORARY
)
1754 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1756 sharing
|= FILE_SHARE_DELETE
;
1759 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1760 sa
.lpSecurityDescriptor
= NULL
;
1761 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1763 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1764 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1765 && !(access
&GENERIC_READ
))
1767 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1768 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1769 if (hand
!= INVALID_HANDLE_VALUE
)
1771 oflags
= check_bom(hand
, oflags
, FALSE
);
1775 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1778 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1779 if (hand
== INVALID_HANDLE_VALUE
) {
1780 WARN(":failed-last error (%d)\n",GetLastError());
1781 _dosmaperr(GetLastError());
1785 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1787 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1788 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1789 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1791 if (oflags
& _O_U8TEXT
)
1793 DWORD written
= 0, tmp
;
1795 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1796 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1798 if (written
!= sizeof(utf8_bom
)) {
1799 WARN("error writing BOM\n");
1801 _dosmaperr(GetLastError());
1807 DWORD written
= 0, tmp
;
1809 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1810 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1812 if (written
!= sizeof(utf16_bom
))
1814 WARN("error writing BOM\n");
1816 _dosmaperr(GetLastError());
1821 else if (access
& GENERIC_READ
)
1822 oflags
= check_bom(hand
, oflags
, TRUE
);
1825 *fd
= alloc_fd(hand
, wxflag
);
1829 if (oflags
& _O_WTEXT
)
1830 get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1831 else if (oflags
& _O_U16TEXT
)
1832 get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1833 else if (oflags
& _O_U8TEXT
)
1834 get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1836 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1840 /*********************************************************************
1841 * _wsopen (MSVCRT.@)
1843 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1848 if (oflags
& _O_CREAT
)
1852 __ms_va_start(ap
, shflags
);
1853 pmode
= va_arg(ap
, int);
1859 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1863 /*********************************************************************
1864 * _sopen_s (MSVCRT.@)
1866 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1871 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1874 ret
= _wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1879 /*********************************************************************
1882 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1887 if (oflags
& _O_CREAT
)
1891 va_start(ap
, shflags
);
1892 pmode
= va_arg(ap
, int);
1898 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1902 /*********************************************************************
1905 int CDECL
_open( const char *path
, int flags
, ... )
1909 if (flags
& _O_CREAT
)
1912 va_start(ap
, flags
);
1913 pmode
= va_arg(ap
, int);
1915 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1918 return _sopen( path
, flags
, _SH_DENYNO
);
1921 /*********************************************************************
1924 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1928 if (flags
& _O_CREAT
)
1931 va_start(ap
, flags
);
1932 pmode
= va_arg(ap
, int);
1934 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1937 return _wsopen( path
, flags
, _SH_DENYNO
);
1940 /*********************************************************************
1943 int CDECL
_creat(const char *path
, int flags
)
1945 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1946 return _open(path
, usedFlags
);
1949 /*********************************************************************
1950 * _wcreat (MSVCRT.@)
1952 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1954 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1955 return _wopen(path
, usedFlags
);
1958 /*********************************************************************
1959 * _open_osfhandle (MSVCRT.@)
1961 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1966 /* _O_RDONLY (0) always matches, so set the read flag
1967 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1968 * file, so set the write flag. It also only sets _O_TEXT if it wants
1969 * text - it never sets _O_BINARY.
1971 /* don't let split_oflags() decide the mode if no mode is passed */
1972 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1973 oflags
|= _O_BINARY
;
1975 flags
= GetFileType((HANDLE
)handle
);
1976 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
1978 _dosmaperr(GetLastError());
1982 if (flags
== FILE_TYPE_CHAR
)
1984 else if (flags
== FILE_TYPE_PIPE
)
1988 flags
|= split_oflags(oflags
);
1990 fd
= alloc_fd((HANDLE
)handle
, flags
);
1991 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
1995 /*********************************************************************
1998 int CDECL
_rmtmp(void)
2000 int num_removed
= 0, i
;
2004 for (i
= 3; i
< stream_idx
; i
++) {
2007 if (file
->_tmpfname
)
2016 TRACE(":removed (%d) temp files\n",num_removed
);
2020 static inline int get_utf8_char_len(char ch
)
2022 if((ch
&0xf8) == 0xf0)
2024 else if((ch
&0xf0) == 0xe0)
2026 else if((ch
&0xe0) == 0xc0)
2031 /*********************************************************************
2032 * (internal) read_utf8
2034 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2036 ioinfo
*fdinfo
= get_ioinfo(fd
);
2037 HANDLE hand
= fdinfo
->handle
;
2038 char min_buf
[4], *readbuf
, lookahead
;
2039 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2041 /* make the buffer big enough to hold at least one character */
2042 /* read bytes have to fit to output and lookahead buffers */
2044 readbuf_size
= count
< 4 ? 4 : count
;
2045 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2050 if(fdinfo
->lookahead
[0] != '\n') {
2051 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2052 fdinfo
->lookahead
[0] = '\n';
2054 if(fdinfo
->lookahead
[1] != '\n') {
2055 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2056 fdinfo
->lookahead
[1] = '\n';
2058 if(fdinfo
->lookahead
[2] != '\n') {
2059 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2060 fdinfo
->lookahead
[2] = '\n';
2065 /* NOTE: this case is broken in native dll, reading
2066 * sometimes fails when small buffer is passed
2069 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2070 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2071 fdinfo
->wxflag
|= WX_ATEOF
;
2074 _dosmaperr(GetLastError());
2077 }else if(!num_read
) {
2078 fdinfo
->wxflag
|= WX_ATEOF
;
2084 char_len
= get_utf8_char_len(readbuf
[0]);
2086 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2090 if(readbuf
[0] == '\n')
2091 fdinfo
->wxflag
|= WX_READNL
;
2093 fdinfo
->wxflag
&= ~WX_READNL
;
2095 if(readbuf
[0] == 0x1a) {
2096 fdinfo
->wxflag
|= WX_ATEOF
;
2100 if(readbuf
[0] == '\r') {
2101 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2103 else if(lookahead
== '\n')
2107 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2108 fdinfo
->lookahead
[0] = lookahead
;
2110 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2115 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2116 _dosmaperr(GetLastError());
2123 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2126 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2127 fdinfo
->wxflag
|= WX_ATEOF
;
2128 if (readbuf
!= min_buf
) free(readbuf
);
2131 _dosmaperr(GetLastError());
2132 if (readbuf
!= min_buf
) free(readbuf
);
2135 }else if(!pos
&& !num_read
) {
2136 fdinfo
->wxflag
|= WX_ATEOF
;
2137 if (readbuf
!= min_buf
) free(readbuf
);
2142 if(readbuf
[0] == '\n')
2143 fdinfo
->wxflag
|= WX_READNL
;
2145 fdinfo
->wxflag
&= ~WX_READNL
;
2147 /* Find first byte of last character (may be incomplete) */
2148 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2149 if((readbuf
[i
]&0xc0) != 0x80)
2151 char_len
= get_utf8_char_len(readbuf
[i
]);
2152 if(char_len
+i
<= pos
)
2155 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
)) {
2157 fdinfo
->lookahead
[0] = readbuf
[i
];
2159 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2161 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2163 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2167 for(i
=0, j
=0; i
<pos
; i
++) {
2168 if(readbuf
[i
] == 0x1a) {
2169 fdinfo
->wxflag
|= WX_ATEOF
;
2173 /* strip '\r' if followed by '\n' */
2174 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2175 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2176 readbuf
[j
++] = '\r';
2177 }else if(lookahead
== '\n' && j
==0) {
2178 readbuf
[j
++] = '\n';
2180 if(lookahead
!= '\n')
2181 readbuf
[j
++] = '\r';
2183 if(fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2184 fdinfo
->lookahead
[0] = lookahead
;
2186 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2188 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2189 readbuf
[j
++] = readbuf
[i
];
2194 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2195 _dosmaperr(GetLastError());
2196 if (readbuf
!= min_buf
) free(readbuf
);
2200 if (readbuf
!= min_buf
) free(readbuf
);
2204 /*********************************************************************
2207 * When reading \r as last character in text mode, read() positions
2208 * the file pointer on the \r character while getc() goes on to
2211 static int read_i(int fd
, void *buf
, unsigned int count
)
2213 DWORD num_read
, utf16
;
2214 char *bufstart
= buf
;
2215 HANDLE hand
= fdtoh(fd
);
2216 ioinfo
*fdinfo
= get_ioinfo(fd
);
2221 if (fdinfo
->wxflag
& WX_ATEOF
) {
2222 TRACE("already at EOF, returning 0\n");
2225 /* Don't trace small reads, it gets *very* annoying */
2227 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2228 if (hand
== INVALID_HANDLE_VALUE
)
2234 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2235 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2241 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2242 return read_utf8(fd
, buf
, count
);
2244 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2246 if (fdinfo
->lookahead
[0] != '\n')
2248 bufstart
[0] = fdinfo
->lookahead
[0];
2249 fdinfo
->lookahead
[0] = '\n';
2253 bufstart
[1] = fdinfo
->lookahead
[1];
2254 fdinfo
->lookahead
[1] = '\n';
2257 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2258 num_read
+= 1+utf16
;
2263 if(utf16
&& (num_read
&1))
2265 /* msvcr90 uses uninitialized value from the buffer in this case */
2266 /* msvcrt ignores additional data */
2267 ERR("got odd number of bytes in UTF16 mode\n");
2271 if (count
!= 0 && num_read
== 0)
2273 fdinfo
->wxflag
|= WX_ATEOF
;
2274 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2276 else if (fdinfo
->wxflag
& WX_TEXT
)
2280 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2281 fdinfo
->wxflag
|= WX_READNL
;
2283 fdinfo
->wxflag
&= ~WX_READNL
;
2285 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2287 /* in text mode, a ctrl-z signals EOF */
2288 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2290 fdinfo
->wxflag
|= WX_ATEOF
;
2291 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2295 /* in text mode, strip \r if followed by \n */
2296 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2301 lookahead
[1] = '\n';
2302 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2304 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2306 bufstart
[j
++] = '\n';
2307 if(utf16
) bufstart
[j
++] = 0;
2311 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2313 bufstart
[j
++] = '\r';
2314 if(utf16
) bufstart
[j
++] = 0;
2317 if (fdinfo
->wxflag
& (WX_PIPE
| WX_NOSEEK
))
2319 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2321 bufstart
[j
++] = '\n';
2322 if (utf16
) bufstart
[j
++] = 0;
2326 fdinfo
->lookahead
[0] = lookahead
[0];
2327 fdinfo
->lookahead
[1] = lookahead
[1];
2331 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2336 bufstart
[j
++] = '\r';
2337 if(utf16
) bufstart
[j
++] = 0;
2340 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2341 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2343 bufstart
[j
++] = bufstart
[i
];
2344 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2352 if (GetLastError() == ERROR_BROKEN_PIPE
)
2354 TRACE(":end-of-pipe\n");
2355 fdinfo
->wxflag
|= WX_ATEOF
;
2360 TRACE(":failed-last error (%d)\n",GetLastError());
2366 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2370 /*********************************************************************
2373 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2376 num_read
= read_i(fd
, buf
, count
);
2380 /*********************************************************************
2381 * _setmode (MSVCRT.@)
2383 int CDECL
_setmode(int fd
,int mode
)
2385 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2386 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2389 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2390 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2395 if(mode
== _O_BINARY
) {
2396 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2397 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2401 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2403 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2404 else if(mode
== _O_U8TEXT
)
2405 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2407 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2413 /*********************************************************************
2416 long CDECL
_tell(int fd
)
2418 return _lseek(fd
, 0, SEEK_CUR
);
2421 /*********************************************************************
2422 * _telli64 (MSVCRT.@)
2424 __int64 CDECL
_telli64(int fd
)
2426 return _lseeki64(fd
, 0, SEEK_CUR
);
2429 /*********************************************************************
2430 * _tempnam (MSVCRT.@)
2432 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2434 char tmpbuf
[MAX_PATH
];
2435 const char *tmp_dir
= getenv("TMP");
2437 if (tmp_dir
) dir
= tmp_dir
;
2439 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2440 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2442 TRACE("got name (%s)\n",tmpbuf
);
2443 DeleteFileA(tmpbuf
);
2444 return _strdup(tmpbuf
);
2446 TRACE("failed (%d)\n",GetLastError());
2450 /*********************************************************************
2451 * _wtempnam (MSVCRT.@)
2453 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2455 wchar_t tmpbuf
[MAX_PATH
];
2457 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2458 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2460 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2461 DeleteFileW(tmpbuf
);
2462 return _wcsdup(tmpbuf
);
2464 TRACE("failed (%d)\n",GetLastError());
2468 /*********************************************************************
2471 int CDECL
_umask(int umask
)
2473 int old_umask
= umask
;
2474 TRACE("(%d)\n",umask
);
2475 MSVCRT_umask
= umask
;
2479 /*********************************************************************
2482 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2485 ioinfo
*info
= get_ioinfo(fd
);
2486 HANDLE hand
= info
->handle
;
2488 /* Don't trace small writes, it gets *very* annoying */
2491 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2493 if (hand
== INVALID_HANDLE_VALUE
)
2499 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2505 /* If appending, go to EOF */
2506 if (info
->wxflag
& WX_APPEND
)
2507 _lseek(fd
, 0, FILE_END
);
2509 if (!(info
->wxflag
& WX_TEXT
))
2511 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2512 && (num_written
== count
))
2514 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2515 hand
, GetLastError());
2520 unsigned int i
, j
, nr_lf
, size
;
2523 const char *s
= buf
, *buf_start
= buf
;
2525 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2527 /* find number of \n */
2528 for (nr_lf
=0, i
=0; i
<count
; i
++)
2534 if ((q
= p
= malloc(size
)))
2536 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2545 FIXME("Malloc failed\n");
2557 else if (info
->exflag
& EF_UTF16
)
2559 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2560 if (s
[i
]=='\n' && s
[i
+1]==0)
2565 if ((q
= p
= malloc(size
)))
2567 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2569 if (s
[i
]=='\n' && s
[i
+1]==0)
2580 FIXME("Malloc failed\n");
2596 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2597 if (s
[i
]=='\n' && s
[i
+1]==0)
2600 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2602 _dosmaperr(GetLastError());
2607 size
= conv_len
+nr_lf
;
2608 if((p
= malloc(count
+nr_lf
*2+size
)))
2610 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2612 if (s
[i
]=='\n' && s
[i
+1]==0)
2620 q
= p
+count
+nr_lf
*2;
2621 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2622 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2626 FIXME("Malloc failed\n");
2633 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2637 if (num_written
!= size
)
2639 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2640 fd
, hand
, GetLastError(), num_written
);
2642 return s
- buf_start
;
2650 /*********************************************************************
2653 int CDECL
_putw(int val
, FILE* file
)
2658 len
= _write(file
->_file
, &val
, sizeof(val
));
2659 if (len
== sizeof(val
)) {
2664 file
->_flag
|= _IOERR
;
2669 /*********************************************************************
2672 int CDECL
fclose(FILE* file
)
2678 free(file
->_tmpfname
);
2679 file
->_tmpfname
= NULL
;
2680 /* flush stdio buffers */
2681 if(file
->_flag
& _IOWRT
)
2683 if(file
->_flag
& _IOMYBUF
)
2686 r
=_close(file
->_file
);
2690 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2691 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2693 if(file
== get_file(stream_idx
-1)) {
2694 while(stream_idx
>3 && !file
->_flag
) {
2696 file
= get_file(stream_idx
-1);
2700 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2703 /*********************************************************************
2706 int CDECL
feof(FILE* file
)
2708 return file
->_flag
& _IOEOF
;
2711 /*********************************************************************
2714 int CDECL
ferror(FILE* file
)
2716 return file
->_flag
& _IOERR
;
2719 /*********************************************************************
2720 * _filbuf (MSVCRT.@)
2722 int CDECL
_filbuf(FILE* file
)
2727 if(file
->_flag
& _IOSTRG
) {
2732 /* Allocate buffer if needed */
2733 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2736 if(!(file
->_flag
& _IOREAD
)) {
2737 if(file
->_flag
& _IORW
)
2738 file
->_flag
|= _IOREAD
;
2745 if(file
->_flag
& _IONBF
) {
2747 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2748 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2756 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2758 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2765 file
->_ptr
= file
->_base
+1;
2766 c
= *(unsigned char *)file
->_base
;
2772 /*********************************************************************
2775 int CDECL
fgetc(FILE* file
)
2783 i
= (unsigned char *)file
->_ptr
++;
2792 /*********************************************************************
2793 * _fgetchar (MSVCRT.@)
2795 int CDECL
_fgetchar(void)
2797 return fgetc(stdin
);
2800 /*********************************************************************
2803 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2806 char * buf_start
= s
;
2808 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2809 file
,file
->_file
,s
,size
);
2813 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2818 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2820 TRACE(":nothing read\n");
2824 if ((cc
!= EOF
) && (size
> 1))
2827 TRACE(":got %s\n", debugstr_a(buf_start
));
2832 /*********************************************************************
2835 wint_t CDECL
fgetwc(FILE* file
)
2842 if((get_ioinfo(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2843 || !(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2846 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2855 char mbs
[MB_LEN_MAX
];
2861 if(isleadbyte((unsigned char)mbs
[0])) {
2872 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2880 /*********************************************************************
2883 int CDECL
_getw(FILE* file
)
2891 for (j
=0; j
<sizeof(int); j
++) {
2894 file
->_flag
|= _IOEOF
;
2905 /*********************************************************************
2908 wint_t CDECL
getwc(FILE* file
)
2910 return fgetwc(file
);
2913 /*********************************************************************
2914 * _fgetwchar (MSVCRT.@)
2916 wint_t CDECL
_fgetwchar(void)
2918 return fgetwc(stdin
);
2921 /*********************************************************************
2922 * getwchar (MSVCRT.@)
2924 wint_t CDECL
getwchar(void)
2926 return _fgetwchar();
2929 /*********************************************************************
2932 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2935 wchar_t * buf_start
= s
;
2937 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2938 file
,file
->_file
,s
,size
);
2942 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2947 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2949 TRACE(":nothing read\n");
2953 if ((cc
!= WEOF
) && (size
> 1))
2956 TRACE(":got %s\n", debugstr_w(buf_start
));
2961 /*********************************************************************
2964 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2966 size_t wrcnt
=size
* nmemb
;
2975 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2976 memcpy(file
->_ptr
, ptr
, pcnt
);
2981 ptr
= (const char*)ptr
+ pcnt
;
2982 } else if(!file
->_bufsiz
&& (file
->_flag
& _IONBF
)) {
2983 if(!(file
->_flag
& _IOWRT
)) {
2984 if(file
->_flag
& _IORW
)
2985 file
->_flag
|= _IOWRT
;
2990 if(_write(file
->_file
, ptr
, wrcnt
) <= 0) {
2991 file
->_flag
|= _IOERR
;
2997 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3001 ptr
= (const char*)ptr
+ 1;
3006 return written
/ size
;
3009 /*********************************************************************
3011 * FORKED for ReactOS, don't sync with Wine!
3013 * - http://jira.reactos.org/browse/CORE-6495
3014 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3016 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3018 /* If this is a real file stream (and not some temporary one for
3019 sprintf-like functions), check whether it is opened in text mode.
3020 In this case, we have to perform an implicit conversion to ANSI. */
3021 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
3023 /* Convert to multibyte in text mode */
3024 char mbc
[MB_LEN_MAX
];
3027 mb_return
= wctomb(mbc
, c
);
3032 /* Output all characters */
3033 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3038 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3045 /*********************************************************************
3046 * _fputwchar (MSVCRT.@)
3048 wint_t CDECL
_fputwchar(wint_t wc
)
3050 return fputwc(wc
, stdout
);
3053 /*********************************************************************
3054 * _wfsopen (MSVCRT.@)
3056 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3059 int open_flags
, stream_flags
, fd
;
3061 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3063 /* map mode string to open() flags. "man fopen" for possibilities. */
3064 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3068 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3071 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
3073 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3080 TRACE(":got (%p)\n",file
);
3081 if (fd
>= 0 && !file
)
3087 /*********************************************************************
3088 * _fsopen (MSVCRT.@)
3090 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3093 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3095 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3096 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3100 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3103 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3108 ret
= _wfsopen(pathW
, modeW
, share
);
3115 /*********************************************************************
3118 FILE * CDECL
fopen(const char *path
, const char *mode
)
3120 return _fsopen( path
, mode
, _SH_DENYNO
);
3123 /*********************************************************************
3124 * fopen_s (MSVCRT.@)
3126 int CDECL
fopen_s(FILE** pFile
,
3127 const char *filename
, const char *mode
)
3129 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
3130 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
3131 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
3133 *pFile
= fopen(filename
, mode
);
3140 /*********************************************************************
3141 * _wfopen (MSVCRT.@)
3143 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3145 return _wfsopen( path
, mode
, _SH_DENYNO
);
3148 /*********************************************************************
3149 * _wfopen_s (MSVCRT.@)
3151 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3152 const wchar_t *mode
)
3154 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3155 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3160 *pFile
= _wfopen(filename
, mode
);
3167 /* fputc calls _flsbuf which calls fputc */
3168 int CDECL
_flsbuf(int c
, FILE* file
);
3170 /*********************************************************************
3173 int CDECL
fputc(int c
, FILE* file
)
3183 res
= flush_buffer(file
);
3185 return res
? res
: c
;
3192 res
= _flsbuf(c
, file
);
3198 /*********************************************************************
3199 * _fputchar (MSVCRT.@)
3201 int CDECL
_fputchar(int c
)
3203 return fputc(c
, stdout
);
3206 /*********************************************************************
3209 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3211 size_t rcnt
=size
* nmemb
;
3220 /* first buffered data */
3222 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
3223 memcpy(ptr
, file
->_ptr
, pcnt
);
3228 ptr
= (char*)ptr
+ pcnt
;
3229 } else if(!(file
->_flag
& _IOREAD
)) {
3230 if(file
->_flag
& _IORW
) {
3231 file
->_flag
|= _IOREAD
;
3240 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& !(file
->_flag
& _IONBF
)
3241 && (file
->_bufsiz
!= 0 || alloc_buffer(file
))) {
3242 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3243 file
->_ptr
= file
->_base
;
3244 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3245 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3246 if (i
> 0 && i
< file
->_cnt
) {
3247 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3248 file
->_flag
&= ~_IOEOF
;
3251 memcpy(ptr
, file
->_ptr
, i
);
3255 } else if (rcnt
> UINT_MAX
) {
3256 i
= _read(file
->_file
, ptr
, UINT_MAX
);
3257 } else if (rcnt
< BUFSIZ
) {
3258 i
= _read(file
->_file
, ptr
, rcnt
);
3260 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3264 ptr
= (char *)ptr
+i
;
3265 /* expose feof condition in the flags
3266 * MFC tests file->_flag for feof, and doesn't call feof())
3268 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3269 file
->_flag
|= _IOEOF
;
3272 file
->_flag
|= _IOERR
;
3283 /*********************************************************************
3284 * _wfreopen (MSVCRT.@)
3287 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3289 int open_flags
, stream_flags
, fd
;
3291 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3294 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3299 /* map mode string to open() flags. "man fopen" for possibilities. */
3300 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3304 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3307 else if (init_fp(file
, fd
, stream_flags
) == -1)
3310 WARN(":failed-last error (%d)\n",GetLastError());
3311 _dosmaperr(GetLastError());
3320 /*********************************************************************
3321 * freopen (MSVCRT.@)
3324 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3327 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3329 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3330 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3336 ret
= _wfreopen(pathW
, modeW
, file
);
3343 /*********************************************************************
3344 * fsetpos (MSVCRT.@)
3346 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3351 /* Note that all this has been lifted 'as is' from fseek */
3352 if(file
->_flag
& _IOWRT
)
3355 /* Discard buffered input */
3357 file
->_ptr
= file
->_base
;
3359 /* Reset direction of i/o */
3360 if(file
->_flag
& _IORW
) {
3361 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3364 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3369 /*********************************************************************
3370 * _ftelli64 (MSVCRT.@)
3372 __int64 CDECL
_ftelli64(FILE* file
)
3377 pos
= _telli64(file
->_file
);
3383 if(file
->_flag
& _IOWRT
) {
3384 pos
+= file
->_ptr
- file
->_base
;
3386 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3389 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3393 } else if(!file
->_cnt
) { /* nothing to do */
3394 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3398 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3399 for(i
=0; i
<file
->_cnt
; i
++)
3400 if(file
->_ptr
[i
] == '\n')
3406 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3411 pos
-= file
->_bufsiz
;
3412 pos
+= file
->_ptr
- file
->_base
;
3414 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3415 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3418 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3429 /*********************************************************************
3432 LONG CDECL
ftell(FILE* file
)
3434 return (LONG
)_ftelli64(file
);
3437 /*********************************************************************
3438 * fgetpos (MSVCRT.@)
3440 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3442 *pos
= _ftelli64(file
);
3448 /*********************************************************************
3451 int CDECL
fputs(const char *s
, FILE* file
)
3453 size_t len
= strlen(s
);
3457 ret
= fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
3462 /*********************************************************************
3465 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3467 size_t i
, len
= strlenW(s
);
3472 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3473 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3478 tmp_buf
= add_std_buffer(file
);
3479 for (i
=0; i
<len
; i
++) {
3480 if(fputwc(s
[i
], file
) == WEOF
) {
3481 if(tmp_buf
) remove_std_buffer(file
);
3487 if(tmp_buf
) remove_std_buffer(file
);
3492 /*********************************************************************
3493 * getchar (MSVCRT.@)
3495 int CDECL
getchar(void)
3497 return fgetc(stdin
);
3500 /*********************************************************************
3503 int CDECL
getc(FILE* file
)
3508 /*********************************************************************
3511 char * CDECL
gets(char *buf
)
3514 char * buf_start
= buf
;
3517 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3519 if(cc
!= '\r') *buf
++ = (char)cc
;
3523 TRACE("got '%s'\n", buf_start
);
3524 _unlock_file(stdin
);
3528 /*********************************************************************
3531 wchar_t* CDECL
_getws(wchar_t* buf
)
3537 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3541 *buf
++ = (wchar_t)cc
;
3545 TRACE("got %s\n", debugstr_w(ws
));
3546 _unlock_file(stdin
);
3550 /*********************************************************************
3553 int CDECL
putc(int c
, FILE* file
)
3555 return fputc(c
, file
);
3558 /*********************************************************************
3559 * putchar (MSVCRT.@)
3561 int CDECL
putchar(int c
)
3563 return fputc(c
, stdout
);
3566 /*********************************************************************
3567 * _putwch (MSVCRT.@)
3569 wint_t CDECL
_putwch(wchar_t c
)
3571 return fputwc(c
, stdout
);
3574 /*********************************************************************
3577 int CDECL
puts(const char *s
)
3579 size_t len
= strlen(s
);
3583 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3584 _unlock_file(stdout
);
3588 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3589 _unlock_file(stdout
);
3593 /*********************************************************************
3596 int CDECL
_putws(const wchar_t *s
)
3598 static const wchar_t nl
= '\n';
3599 size_t len
= strlenW(s
);
3603 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3604 _unlock_file(stdout
);
3608 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3609 _unlock_file(stdout
);
3613 /*********************************************************************
3616 int CDECL
remove(const char *path
)
3618 TRACE("(%s)\n",path
);
3619 if (DeleteFileA(path
))
3621 TRACE(":failed (%d)\n",GetLastError());
3622 _dosmaperr(GetLastError());
3626 /*********************************************************************
3627 * _wremove (MSVCRT.@)
3629 int CDECL
_wremove(const wchar_t *path
)
3631 TRACE("(%s)\n",debugstr_w(path
));
3632 if (DeleteFileW(path
))
3634 TRACE(":failed (%d)\n",GetLastError());
3635 _dosmaperr(GetLastError());
3639 /*********************************************************************
3642 int CDECL
rename(const char *oldpath
,const char *newpath
)
3644 TRACE(":from %s to %s\n",oldpath
,newpath
);
3645 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3647 TRACE(":failed (%d)\n",GetLastError());
3648 _dosmaperr(GetLastError());
3652 /*********************************************************************
3653 * _wrename (MSVCRT.@)
3655 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3657 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3658 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3660 TRACE(":failed (%d)\n",GetLastError());
3661 _dosmaperr(GetLastError());
3665 /*********************************************************************
3666 * setvbuf (MSVCRT.@)
3668 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3676 if(mode
== _IOFBF
) {
3677 file
->_flag
&= ~_IONBF
;
3678 file
->_base
= file
->_ptr
= buf
;
3680 file
->_bufsiz
= size
;
3683 file
->_flag
|= _IONBF
;
3689 /*********************************************************************
3692 void CDECL
setbuf(FILE* file
, char *buf
)
3694 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3697 /*********************************************************************
3700 char * CDECL
tmpnam(char *s
)
3707 thread_data_t
*data
= msvcrt_get_thread_data();
3709 if(!data
->tmpnam_buffer
)
3710 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3712 s
= data
->tmpnam_buffer
;
3715 int_to_base32(GetCurrentProcessId(), tmpstr
);
3716 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3717 for (count
= 0; count
< TMP_MAX
; count
++)
3719 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3720 memcpy(p
, tmpstr
, size
);
3722 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3723 GetLastError() == ERROR_FILE_NOT_FOUND
)
3729 /*********************************************************************
3730 * _wtmpnam (MSVCRT.@)
3732 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3734 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3739 thread_data_t
*data
= msvcrt_get_thread_data();
3741 if(!data
->wtmpnam_buffer
)
3742 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3744 s
= data
->wtmpnam_buffer
;
3747 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3748 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3749 for (count
= 0; count
< TMP_MAX
; count
++)
3751 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3752 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3754 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3755 GetLastError() == ERROR_FILE_NOT_FOUND
)
3761 /*********************************************************************
3762 * tmpfile (MSVCRT.@)
3764 FILE* CDECL
tmpfile(void)
3766 char *filename
= tmpnam(NULL
);
3771 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3772 _S_IREAD
| _S_IWRITE
);
3773 if (fd
!= -1 && (file
= alloc_fp()))
3775 if (init_fp(file
, fd
, _IORW
) == -1)
3780 else file
->_tmpfname
= _strdup(filename
);
3783 if(fd
!= -1 && !file
)
3789 /*********************************************************************
3792 int CDECL
ungetc(int c
, FILE * file
)
3798 if(file
->_bufsiz
== 0 && alloc_buffer(file
))
3800 if(file
->_ptr
>file
->_base
) {
3802 if(file
->_flag
& _IOSTRG
) {
3803 if(*file
->_ptr
!= c
) {
3821 /*********************************************************************
3822 * ungetwc (MSVCRT.@)
3824 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3827 char * pp
= (char *)&mwc
;
3831 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3832 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3844 /*********************************************************************
3845 * _getmaxstdio (MSVCRT.@)
3847 int CDECL
_getmaxstdio(void)
3852 /*********************************************************************
3853 * _setmaxstdio (MSVCRT.@)
3855 int CDECL
_setmaxstdio(int newmax
)
3857 TRACE("%d\n", newmax
);
3859 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3862 max_streams
= newmax
;