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
105 /* FIXME: this should be allocated dynamically */
106 #define MAX_FILES 2048
107 #define FD_BLOCK_SIZE 64
109 /* ioinfo structure size is different in msvcrXX.dll's */
112 unsigned char wxflag
;
115 CRITICAL_SECTION crit
;
118 /*********************************************************************
119 * __pioinfo (MSVCRT.@)
120 * array of pointers to ioinfo arrays [64]
122 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
124 /*********************************************************************
125 * __badioinfo (MSVCRT.@)
127 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
129 static int fdstart
= 3; /* first unallocated fd */
130 static int fdend
= 3; /* highest allocated fd */
134 CRITICAL_SECTION crit
;
137 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
138 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
139 static int max_streams
= 512, stream_idx
;
141 /* INTERNAL: process umask */
142 static int MSVCRT_umask
= 0;
144 /* INTERNAL: static data for tmpnam and _wtmpname functions */
145 static int tmpnam_unique
;
147 /* This critical section protects the tables __pioinfo and fstreams,
148 * and their related indexes, fdstart, fdend,
149 * and stream_idx, from race conditions.
150 * It doesn't protect against race conditions manipulating the underlying files
151 * or flags; doing so would probably be better accomplished with per-file
152 * protection, rather than locking the whole table for every change.
154 static CRITICAL_SECTION file_cs
;
155 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
156 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
158 static inline ioinfo
* get_ioinfo(int fd
)
162 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
166 return ret
+ (fd
%FD_BLOCK_SIZE
);
169 static inline FILE* get_file(int i
)
179 ret
= fstream
[i
/FD_BLOCK_SIZE
];
181 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
182 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
183 ERR("out of memory\n");
188 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
190 ret
+= i
%FD_BLOCK_SIZE
;
195 static inline BOOL
is_valid_fd(int fd
)
197 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
200 /* INTERNAL: Get the HANDLE for a fd
201 * This doesn't lock the table, because a failure will result in
202 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
203 * it returns a valid handle which is about to be closed, a subsequent call
204 * will fail, most likely in a sane way.
206 /*static*/ HANDLE
fdtoh(int fd
)
208 if (!is_valid_fd(fd
))
210 WARN(":fd (%d) - no handle!\n",fd
);
213 return INVALID_HANDLE_VALUE
;
215 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
216 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
217 return get_ioinfo(fd
)->handle
;
220 /* INTERNAL: free a file entry fd */
221 static void free_fd(int fd
)
227 fdinfo
= get_ioinfo(fd
);
228 old_handle
= fdinfo
->handle
;
229 if(fdinfo
!= &__badioinfo
)
231 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
234 TRACE(":fd (%d) freed\n",fd
);
235 if (fd
< 3) /* don't use 0,1,2 for user files */
240 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
243 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
246 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
260 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
261 /* caller must hold the files lock */
262 static int set_fd(HANDLE hand
, int flag
, int fd
)
268 WARN(":files exhausted!\n");
273 fdinfo
= get_ioinfo(fd
);
274 if(fdinfo
== &__badioinfo
) {
277 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
278 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
279 WARN(":out of memory!\n");
284 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
285 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
287 fdinfo
= get_ioinfo(fd
);
290 fdinfo
->handle
= hand
;
291 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_NOSEEK
));
292 fdinfo
->lookahead
[0] = '\n';
293 fdinfo
->lookahead
[1] = '\n';
294 fdinfo
->lookahead
[2] = '\n';
297 /* locate next free slot */
298 if (fd
== fdstart
&& fd
== fdend
)
301 while (fdstart
< fdend
&&
302 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
304 /* update last fd in use */
307 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
311 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
312 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
313 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
319 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
320 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
325 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
326 ret
= set_fd(hand
, flag
, fdstart
);
331 /* INTERNAL: Allocate a FILE* for an fd slot */
332 /* caller must hold the files lock */
333 static FILE* alloc_fp(void)
338 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
344 if (file
->_flag
== 0)
346 if (i
== stream_idx
) stream_idx
++;
354 /* INTERNAL: initialize a FILE* from an open fd */
355 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
357 TRACE(":fd (%d) allocating FILE*\n",fd
);
358 if (!is_valid_fd(fd
))
360 WARN(":invalid fd %d\n",fd
);
365 memset(file
, 0, sizeof(*file
));
367 file
->_flag
= stream_flags
;
369 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
370 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
372 TRACE(":got FILE* (%p)\n",file
);
376 /* INTERNAL: Create an inheritance data block (for spawned process)
377 * The inheritance block is made of:
378 * 00 int nb of file descriptor (NBFD)
379 * 04 char file flags (wxflag): repeated for each fd
380 * 4+NBFD HANDLE file handle: repeated for each fd
382 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
389 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
390 *block
= calloc(*size
, 1);
396 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
397 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
399 *(unsigned*)*block
= fdend
;
400 for (fd
= 0; fd
< fdend
; fd
++)
402 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
403 fdinfo
= get_ioinfo(fd
);
404 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
406 *wxflag_ptr
= fdinfo
->wxflag
;
407 *handle_ptr
= fdinfo
->handle
;
412 *handle_ptr
= INVALID_HANDLE_VALUE
;
414 wxflag_ptr
++; handle_ptr
++;
419 /* INTERNAL: Set up all file descriptors,
420 * as well as default streams (stdin, stderr and stdout)
422 void msvcrt_init_io(void)
428 InitializeCriticalSection(&file_cs
);
429 file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_cs");
430 GetStartupInfoA(&si
);
431 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
437 count
= *(unsigned*)si
.lpReserved2
;
438 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
439 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
441 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
442 count
= min(count
, MAX_FILES
);
443 for (i
= 0; i
< count
; i
++)
445 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
446 set_fd(*handle_ptr
, *wxflag_ptr
, i
);
448 wxflag_ptr
++; handle_ptr
++;
450 fdend
= max( 3, count
);
451 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
452 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
456 set_fd(INVALID_HANDLE_VALUE
, 0, 3);
458 fdinfo
= get_ioinfo(0);
459 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
461 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
463 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
464 GetCurrentProcess(), &fdinfo
->handle
,
465 0, TRUE
, DUPLICATE_SAME_ACCESS
))
467 fdinfo
->handle
= std
;
469 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
472 fdinfo
= get_ioinfo(1);
473 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
475 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
477 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
478 GetCurrentProcess(), &fdinfo
->handle
,
479 0, TRUE
, DUPLICATE_SAME_ACCESS
))
481 fdinfo
->handle
= std
;
483 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
486 fdinfo
= get_ioinfo(2);
487 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
489 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
491 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
492 GetCurrentProcess(), &fdinfo
->handle
,
493 0, TRUE
, DUPLICATE_SAME_ACCESS
))
495 fdinfo
->handle
= std
;
497 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
500 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(0)->handle
,
501 get_ioinfo(1)->handle
, get_ioinfo(2)->handle
);
503 memset(_iob
,0,3*sizeof(FILE));
504 for (i
= 0; i
< 3; i
++)
506 /* FILE structs for stdin/out/err are static and never deleted */
508 _iob
[i
]._tmpfname
= NULL
;
509 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
514 /* INTERNAL: Flush stdio file buffer */
515 static int flush_buffer(FILE* file
)
518 int cnt
=file
->_ptr
-file
->_base
;
519 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
520 file
->_flag
|= _IOERR
;
523 file
->_ptr
=file
->_base
;
524 file
->_cnt
=file
->_bufsiz
;
529 /* INTERNAL: Allocate stdio file buffer */
530 /*static*/ void alloc_buffer(FILE* file
)
532 file
->_base
= calloc(BUFSIZ
,1);
534 file
->_bufsiz
= BUFSIZ
;
535 file
->_flag
|= _IOMYBUF
;
537 file
->_base
= (char*)(&file
->_charbuf
);
539 file
->_bufsiz
= sizeof(file
->_charbuf
);
541 file
->_ptr
= file
->_base
;
545 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
546 static int int_to_base32(int num
, char *str
)
561 *p
= (num
& 31) + '0';
563 *p
+= ('a' - '0' - 10);
570 /* INTERNAL: wide character version of int_to_base32 */
571 static int int_to_base32_w(int num
, wchar_t *str
)
586 *p
= (num
& 31) + '0';
588 *p
+= ('a' - '0' - 10);
595 /* INTERNAL: Create a wide string from an ascii string */
596 wchar_t *msvcrt_wstrdupa(const char *str
)
598 const unsigned int len
= strlen(str
) + 1 ;
599 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
602 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
606 /*********************************************************************
607 * __iob_func(MSVCRT.@)
609 FILE * CDECL
__iob_func(void)
614 /*********************************************************************
617 int CDECL
_access(const char *filename
, int mode
)
619 DWORD attr
= GetFileAttributesA(filename
);
621 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
623 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
625 _dosmaperr(GetLastError());
628 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
630 _set_errno(ERROR_ACCESS_DENIED
);
636 /*********************************************************************
637 * _access_s (MSVCRT.@)
639 int CDECL
_access_s(const char *filename
, int mode
)
641 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
642 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
648 return _access(filename
, mode
);
651 /*********************************************************************
652 * _waccess (MSVCRT.@)
654 int CDECL
_waccess(const wchar_t *filename
, int mode
)
656 DWORD attr
= GetFileAttributesW(filename
);
658 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
660 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
662 _dosmaperr(GetLastError());
665 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
667 _set_errno(ERROR_ACCESS_DENIED
);
673 /*********************************************************************
674 * _waccess_s (MSVCRT.@)
676 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
678 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
679 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
685 return _waccess(filename
, mode
);
688 /*********************************************************************
691 int CDECL
_chmod(const char *path
, int flags
)
693 DWORD oldFlags
= GetFileAttributesA(path
);
695 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
697 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
698 oldFlags
| FILE_ATTRIBUTE_READONLY
;
700 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
703 _dosmaperr(GetLastError());
707 /*********************************************************************
710 int CDECL
_wchmod(const wchar_t *path
, int flags
)
712 DWORD oldFlags
= GetFileAttributesW(path
);
714 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
716 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
717 oldFlags
| FILE_ATTRIBUTE_READONLY
;
719 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
722 _dosmaperr(GetLastError());
726 /*********************************************************************
729 int CDECL
_unlink(const char *path
)
731 TRACE("%s\n",debugstr_a(path
));
732 if(DeleteFileA(path
))
734 TRACE("failed (%d)\n",GetLastError());
735 _dosmaperr(GetLastError());
739 /*********************************************************************
740 * _wunlink (MSVCRT.@)
742 int CDECL
_wunlink(const wchar_t *path
)
744 TRACE("(%s)\n",debugstr_w(path
));
745 if(DeleteFileW(path
))
747 TRACE("failed (%d)\n",GetLastError());
748 _dosmaperr(GetLastError());
752 /* _flushall calls fflush which calls _flushall */
753 int CDECL
fflush(FILE* file
);
755 /* INTERNAL: Flush all stream buffer */
756 static int flush_all_buffers(int mask
)
758 int i
, num_flushed
= 0;
762 for (i
= 3; i
< stream_idx
; i
++) {
767 if(file
->_flag
& mask
) {
775 TRACE(":flushed (%d) handles\n",num_flushed
);
779 /*********************************************************************
780 * _flushall (MSVCRT.@)
782 int CDECL
_flushall(void)
784 return flush_all_buffers(_IOWRT
| _IOREAD
);
787 /*********************************************************************
790 int CDECL
fflush(FILE* file
)
793 flush_all_buffers(_IOWRT
);
794 } else if(file
->_flag
& _IOWRT
) {
798 res
= flush_buffer(file
);
800 if(!res && (file->_flag & _IOCOMMIT))
801 res = _commit(file->_file) ? EOF : 0;
806 } else if(file
->_flag
& _IOREAD
) {
809 file
->_ptr
= file
->_base
;
817 /*********************************************************************
820 int CDECL
_close(int fd
)
827 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
828 if (hand
== INVALID_HANDLE_VALUE
)
830 else if (!CloseHandle(hand
))
832 WARN(":failed-last error (%d)\n",GetLastError());
833 _dosmaperr(GetLastError());
846 /*********************************************************************
849 int CDECL
_commit(int fd
)
851 HANDLE hand
= fdtoh(fd
);
853 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
854 if (hand
== INVALID_HANDLE_VALUE
)
857 if (!FlushFileBuffers(hand
))
859 if (GetLastError() == ERROR_INVALID_HANDLE
)
861 /* FlushFileBuffers fails for console handles
862 * so we ignore this error.
866 TRACE(":failed-last error (%d)\n",GetLastError());
867 _dosmaperr(GetLastError());
874 /*********************************************************************
877 * MSDN isn't clear on this point, but the remarks for _pipe
878 * indicate file descriptors duplicated with _dup and _dup2 are always
881 int CDECL
_dup2(int od
, int nd
)
885 TRACE("(od=%d, nd=%d)\n", od
, nd
);
887 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
891 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
892 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
894 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
898 ret
= set_fd(handle
, wxflag
, nd
);
906 /* _dup2 returns 0, not nd, on success */
913 _dosmaperr(GetLastError());
925 /*********************************************************************
928 int CDECL
_dup(int od
)
934 if (_dup2(od
, fd
) == 0)
942 /*********************************************************************
945 int CDECL
_eof(int fd
)
948 LONG hcurpos
,hendpos
;
949 HANDLE hand
= fdtoh(fd
);
951 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
953 if (hand
== INVALID_HANDLE_VALUE
)
956 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
958 /* Otherwise we do it the hard way */
959 hcurpos
= hendpos
= 0;
960 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
961 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
963 if (curpos
== endpos
&& hcurpos
== hendpos
)
965 /* FIXME: shouldn't WX_ATEOF be set here? */
969 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
973 /*********************************************************************
974 * _fcloseall (MSVCRT.@)
976 int CDECL
_fcloseall(void)
978 int num_closed
= 0, i
;
982 for (i
= 3; i
< stream_idx
; i
++) {
985 if (file
->_flag
&& !fclose(file
))
990 TRACE(":closed (%d) handles\n",num_closed
);
994 /* free everything on process exit */
995 void msvcrt_free_io(void)
1000 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
1001 * stdout, and stderr (unlike GNU), so we need to fclose() them here
1002 * or they won't get flushed.
1008 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1011 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
1014 file_cs
.DebugInfo
->Spare
[0] = 0;
1015 DeleteCriticalSection(&file_cs
);
1018 /*********************************************************************
1019 * _lseeki64 (MSVCRT.@)
1021 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1023 HANDLE hand
= fdtoh(fd
);
1026 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1027 if (hand
== INVALID_HANDLE_VALUE
)
1030 if (whence
< 0 || whence
> 2)
1036 TRACE(":fd (%d) to %s pos %s\n",
1037 fd
,wine_dbgstr_longlong(offset
),
1038 (whence
==SEEK_SET
)?"SEEK_SET":
1039 (whence
==SEEK_CUR
)?"SEEK_CUR":
1040 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1042 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1043 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1044 ofs
.QuadPart
= offset
;
1045 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1046 GetLastError() == ERROR_SUCCESS
)
1048 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1049 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1051 return ofs
.QuadPart
;
1053 TRACE(":error-last error (%d)\n",GetLastError());
1054 _dosmaperr(GetLastError());
1058 /*********************************************************************
1061 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1063 return (LONG
)_lseeki64(fd
, offset
, whence
);
1066 /*********************************************************************
1067 * _lock_file (MSVCRT.@)
1069 void CDECL
_lock_file(FILE *file
)
1071 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1072 _lock(_STREAM_LOCKS
+(file
-_iob
));
1073 /* ReactOS: string streams dont need to be locked */
1074 else if(!(file
->_flag
& _IOSTRG
))
1075 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1078 /*********************************************************************
1079 * _unlock_file (MSVCRT.@)
1081 void CDECL
_unlock_file(FILE *file
)
1083 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1084 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1085 /* ReactOS: string streams dont need to be locked */
1086 else if(!(file
->_flag
& _IOSTRG
))
1087 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1091 /*********************************************************************
1092 * _locking (MSVCRT.@)
1094 * This is untested; the underlying LockFile doesn't work yet.
1096 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1100 HANDLE hand
= fdtoh(fd
);
1102 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1103 if (hand
== INVALID_HANDLE_VALUE
)
1106 if (mode
< 0 || mode
> 4)
1112 TRACE(":fd (%d) by 0x%08x mode %s\n",
1113 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1114 (mode
==_LK_LOCK
)?"_LK_LOCK":
1115 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1116 (mode
==_LK_RLCK
)?"_LK_RLCK":
1117 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1120 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1122 FIXME ("Seek failed\n");
1123 *_errno() = EINVAL
; /* FIXME */
1126 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1129 ret
= 1; /* just to satisfy gcc */
1132 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1137 else if (mode
== _LK_UNLCK
)
1138 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1140 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1141 /* FIXME - what about error settings? */
1142 return ret
? 0 : -1;
1145 /*********************************************************************
1146 * _fseeki64 (MSVCRT.@)
1148 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1153 /* Flush output if needed */
1154 if(file
->_flag
& _IOWRT
)
1157 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1158 offset
-= file
->_cnt
;
1159 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1160 /* Black magic correction for CR removal */
1162 for (i
=0; i
<file
->_cnt
; i
++) {
1163 if (file
->_ptr
[i
] == '\n')
1166 /* Black magic when reading CR at buffer boundary*/
1167 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1171 /* Discard buffered input */
1173 file
->_ptr
= file
->_base
;
1174 /* Reset direction of i/o */
1175 if(file
->_flag
& _IORW
) {
1176 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1178 /* Clear end of file flag */
1179 file
->_flag
&= ~_IOEOF
;
1180 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1186 /*********************************************************************
1189 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1191 return _fseeki64( file
, offset
, whence
);
1194 /*********************************************************************
1195 * _chsize (MSVCRT.@)
1197 int CDECL
_chsize(int fd
, long size
)
1203 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1208 if (handle
!= INVALID_HANDLE_VALUE
)
1210 /* save the current file pointer */
1211 cur
= _lseek(fd
, 0, SEEK_CUR
);
1214 pos
= _lseek(fd
, size
, SEEK_SET
);
1217 ret
= SetEndOfFile(handle
);
1218 if (!ret
) _dosmaperr(GetLastError());
1221 /* restore the file pointer */
1222 _lseek(fd
, cur
, SEEK_SET
);
1227 return ret
? 0 : -1;
1230 /*********************************************************************
1231 * clearerr (MSVCRT.@)
1233 void CDECL
clearerr(FILE* file
)
1235 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1238 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1242 /*********************************************************************
1245 void CDECL
rewind(FILE* file
)
1247 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1250 fseek(file
, 0L, SEEK_SET
);
1255 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1257 int plus
= strchrW(mode
, '+') != NULL
;
1259 TRACE("%s\n", debugstr_w(mode
));
1261 while(*mode
== ' ') mode
++;
1266 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1267 *stream_flags
= plus
? _IORW
: _IOREAD
;
1270 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1271 *stream_flags
= plus
? _IORW
: _IOWRT
;
1274 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1275 *stream_flags
= plus
? _IORW
: _IOWRT
;
1278 MSVCRT_INVALID_PMT(0, EINVAL
);
1282 *stream_flags
|= _commode
;
1284 while (*mode
&& *mode
!=',')
1288 *open_flags
|= _O_BINARY
;
1289 *open_flags
&= ~_O_TEXT
;
1292 *open_flags
|= _O_TEXT
;
1293 *open_flags
&= ~_O_BINARY
;
1296 *open_flags
|= _O_TEMPORARY
;
1299 *open_flags
|= _O_SHORT_LIVED
;
1302 *stream_flags
|= _IOCOMMIT
;
1305 *stream_flags
&= ~_IOCOMMIT
;
1308 *open_flags
|= _O_NOINHERIT
;
1317 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1320 ERR("incorrect mode flag: %c\n", mode
[-1]);
1326 static const WCHAR ccs
[] = {'c','c','s'};
1327 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1328 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1329 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1332 while(*mode
== ' ') mode
++;
1333 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1335 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1336 while(*mode
== ' ') mode
++;
1337 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1340 while(*mode
== ' ') mode
++;
1342 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1344 *open_flags
|= _O_U8TEXT
;
1345 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1347 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1349 *open_flags
|= _O_U16TEXT
;
1350 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1352 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1354 *open_flags
|= _O_WTEXT
;
1355 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1359 MSVCRT_INVALID_PMT(0, EINVAL
);
1363 while(*mode
== ' ') mode
++;
1366 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1371 /*********************************************************************
1372 * _fdopen (MSVCRT.@)
1374 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1377 wchar_t *modeW
= NULL
;
1379 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1381 ret
= _wfdopen(fd
, modeW
);
1387 /*********************************************************************
1388 * _wfdopen (MSVCRT.@)
1390 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1392 int open_flags
, stream_flags
;
1395 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1398 if (!(file
= alloc_fp()))
1400 else if (init_fp(file
, fd
, stream_flags
) == -1)
1405 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1411 /*********************************************************************
1412 * _filelength (MSVCRT.@)
1414 LONG CDECL
_filelength(int fd
)
1416 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1419 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1422 if (endPos
!= curPos
)
1423 _lseek(fd
, curPos
, SEEK_SET
);
1430 /*********************************************************************
1431 * _filelengthi64 (MSVCRT.@)
1433 __int64 CDECL
_filelengthi64(int fd
)
1435 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1438 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1441 if (endPos
!= curPos
)
1442 _lseeki64(fd
, curPos
, SEEK_SET
);
1449 /*********************************************************************
1450 * _fileno (MSVCRT.@)
1452 int CDECL
_fileno(FILE* file
)
1454 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1458 /*********************************************************************
1459 * _get_osfhandle (MSVCRT.@)
1461 intptr_t CDECL
_get_osfhandle(int fd
)
1463 HANDLE hand
= fdtoh(fd
);
1464 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1466 return (intptr_t)hand
;
1469 /*********************************************************************
1470 * _isatty (MSVCRT.@)
1472 int CDECL
_isatty(int fd
)
1474 HANDLE hand
= fdtoh(fd
);
1476 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1477 if (hand
== INVALID_HANDLE_VALUE
)
1480 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1483 /*********************************************************************
1484 * _mktemp (MSVCRT.@)
1486 char * CDECL
_mktemp(char *pattern
)
1489 char *retVal
= pattern
;
1497 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1501 id
= GetCurrentProcessId();
1505 int tempNum
= id
/ 10;
1506 *pattern
-- = id
- (tempNum
* 10) + '0';
1512 *pattern
= letter
++;
1513 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1515 } while(letter
<= 'z');
1519 /*********************************************************************
1520 * _wmktemp (MSVCRT.@)
1522 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1525 wchar_t *retVal
= pattern
;
1527 wchar_t letter
= 'a';
1530 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1534 id
= GetCurrentProcessId();
1538 int tempNum
= id
/ 10;
1539 *pattern
-- = id
- (tempNum
* 10) + '0';
1545 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1546 GetLastError() == ERROR_FILE_NOT_FOUND
)
1548 *pattern
= letter
++;
1549 } while(letter
!= '|');
1553 /*static*/ unsigned split_oflags(unsigned oflags
)
1556 unsigned unsupp
; /* until we support everything */
1558 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1559 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1560 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1561 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1562 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1563 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1565 if ((unsupp
= oflags
& ~(
1566 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1567 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1568 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1570 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1572 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1577 /*********************************************************************
1580 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1583 SECURITY_ATTRIBUTES sa
;
1584 HANDLE readHandle
, writeHandle
;
1592 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1593 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1594 sa
.lpSecurityDescriptor
= NULL
;
1595 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1597 unsigned int wxflags
= split_oflags(textmode
);
1601 fd
= alloc_fd(readHandle
, wxflags
);
1605 fd
= alloc_fd(writeHandle
, wxflags
);
1614 CloseHandle(writeHandle
);
1620 CloseHandle(readHandle
);
1621 CloseHandle(writeHandle
);
1627 _dosmaperr(GetLastError());
1632 /*********************************************************************
1633 * _sopen_s (MSVCRT.@)
1635 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1637 DWORD access
= 0, creation
= 0, attrib
;
1641 SECURITY_ATTRIBUTES sa
;
1643 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1644 fd
, path
, oflags
, shflags
, pmode
);
1648 MSVCRT_INVALID_PMT("null out fd pointer", EINVAL
);
1653 wxflag
= split_oflags(oflags
);
1654 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1656 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1657 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1658 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1661 if (oflags
& _O_CREAT
)
1663 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1664 FIXME(": pmode 0x%04x ignored\n", pmode
);
1666 WARN(": pmode 0x%04x ignored\n", pmode
);
1668 if (oflags
& _O_EXCL
)
1669 creation
= CREATE_NEW
;
1670 else if (oflags
& _O_TRUNC
)
1671 creation
= CREATE_ALWAYS
;
1673 creation
= OPEN_ALWAYS
;
1675 else /* no _O_CREAT */
1677 if (oflags
& _O_TRUNC
)
1678 creation
= TRUNCATE_EXISTING
;
1680 creation
= OPEN_EXISTING
;
1689 sharing
= FILE_SHARE_READ
;
1692 sharing
= FILE_SHARE_WRITE
;
1695 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1698 ERR( "Unhandled shflags 0x%x\n", shflags
);
1701 attrib
= FILE_ATTRIBUTE_NORMAL
;
1703 if (oflags
& _O_TEMPORARY
)
1705 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1707 sharing
|= FILE_SHARE_DELETE
;
1710 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1711 sa
.lpSecurityDescriptor
= NULL
;
1712 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1714 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1715 if (hand
== INVALID_HANDLE_VALUE
) {
1716 WARN(":failed-last error (%d)\n", GetLastError());
1717 _dosmaperr(GetLastError());
1721 *fd
= alloc_fd(hand
, wxflag
);
1723 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1727 /*********************************************************************
1730 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1735 if (oflags
& _O_CREAT
)
1739 va_start(ap
, shflags
);
1740 pmode
= va_arg(ap
, int);
1746 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1750 /*********************************************************************
1751 * _wsopen_s (MSVCRT.@)
1753 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1755 DWORD access
= 0, creation
= 0, attrib
;
1756 SECURITY_ATTRIBUTES sa
;
1761 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1762 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1766 MSVCRT_INVALID_PMT("null out fd pointer", EINVAL
);
1771 wxflag
= split_oflags(oflags
);
1772 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1774 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1775 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1776 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1779 if (oflags
& _O_CREAT
)
1781 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1782 FIXME(": pmode 0x%04x ignored\n", pmode
);
1784 WARN(": pmode 0x%04x ignored\n", pmode
);
1786 if (oflags
& _O_EXCL
)
1787 creation
= CREATE_NEW
;
1788 else if (oflags
& _O_TRUNC
)
1789 creation
= CREATE_ALWAYS
;
1791 creation
= OPEN_ALWAYS
;
1793 else /* no _O_CREAT */
1795 if (oflags
& _O_TRUNC
)
1796 creation
= TRUNCATE_EXISTING
;
1798 creation
= OPEN_EXISTING
;
1807 sharing
= FILE_SHARE_READ
;
1810 sharing
= FILE_SHARE_WRITE
;
1813 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1816 ERR( "Unhandled shflags 0x%x\n", shflags
);
1819 attrib
= FILE_ATTRIBUTE_NORMAL
;
1821 if (oflags
& _O_TEMPORARY
)
1823 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1825 sharing
|= FILE_SHARE_DELETE
;
1828 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1829 sa
.lpSecurityDescriptor
= NULL
;
1830 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1832 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1834 if (hand
== INVALID_HANDLE_VALUE
) {
1835 WARN(":failed-last error (%d)\n",GetLastError());
1836 _dosmaperr(GetLastError());
1840 *fd
= alloc_fd(hand
, wxflag
);
1842 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1846 /*********************************************************************
1847 * _wsopen (MSVCRT.@)
1849 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1854 if (oflags
& _O_CREAT
)
1858 va_start(ap
, shflags
);
1859 pmode
= va_arg(ap
, int);
1865 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1869 /*********************************************************************
1872 int CDECL
_open( const char *path
, int flags
, ... )
1876 if (flags
& _O_CREAT
)
1879 va_start(ap
, flags
);
1880 pmode
= va_arg(ap
, int);
1882 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1885 return _sopen( path
, flags
, _SH_DENYNO
);
1888 /*********************************************************************
1891 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1895 if (flags
& _O_CREAT
)
1898 va_start(ap
, flags
);
1899 pmode
= va_arg(ap
, int);
1901 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1904 return _wsopen( path
, flags
, _SH_DENYNO
);
1907 /*********************************************************************
1910 int CDECL
_creat(const char *path
, int flags
)
1912 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1913 return _open(path
, usedFlags
);
1916 /*********************************************************************
1917 * _wcreat (MSVCRT.@)
1919 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1921 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1922 return _wopen(path
, usedFlags
);
1925 /*********************************************************************
1926 * _open_osfhandle (MSVCRT.@)
1928 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1933 /* _O_RDONLY (0) always matches, so set the read flag
1934 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1935 * file, so set the write flag. It also only sets _O_TEXT if it wants
1936 * text - it never sets _O_BINARY.
1938 /* don't let split_oflags() decide the mode if no mode is passed */
1939 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1940 oflags
|= _O_BINARY
;
1942 flags
= GetFileType((HANDLE
)handle
);
1943 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
1945 _dosmaperr(GetLastError());
1949 if (flags
== FILE_TYPE_CHAR
)
1951 else if (flags
== FILE_TYPE_PIPE
)
1955 flags
|= split_oflags(oflags
);
1957 fd
= alloc_fd((HANDLE
)handle
, flags
);
1958 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
1962 /*********************************************************************
1965 int CDECL
_rmtmp(void)
1967 int num_removed
= 0, i
;
1971 for (i
= 3; i
< stream_idx
; i
++) {
1974 if (file
->_tmpfname
)
1983 TRACE(":removed (%d) temp files\n",num_removed
);
1987 /*********************************************************************
1990 * When reading \r as last character in text mode, read() positions
1991 * the file pointer on the \r character while getc() goes on to
1994 static int read_i(int fd
, void *buf
, unsigned int count
)
1997 char *bufstart
= buf
;
1998 HANDLE hand
= fdtoh(fd
);
1999 ioinfo
*fdinfo
= get_ioinfo(fd
);
2004 if (fdinfo
->wxflag
& WX_READEOF
) {
2005 fdinfo
->wxflag
|= WX_ATEOF
;
2006 TRACE("already at EOF, returning 0\n");
2009 /* Don't trace small reads, it gets *very* annoying */
2011 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2012 if (hand
== INVALID_HANDLE_VALUE
)
2015 /* Reading single bytes in O_TEXT mode makes things slow
2016 * So read big chunks
2018 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2020 if (count
!= 0 && num_read
== 0)
2022 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2023 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2025 else if (fdinfo
->wxflag
& WX_TEXT
)
2028 if (bufstart
[num_read
-1] == '\r')
2032 fdinfo
->wxflag
&= ~WX_READCR
;
2033 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
2037 fdinfo
->wxflag
|= WX_READCR
;
2042 fdinfo
->wxflag
&= ~WX_READCR
;
2043 for (i
=0, j
=0; i
<num_read
; i
++)
2045 /* in text mode, a ctrl-z signals EOF */
2046 if (bufstart
[i
] == 0x1a)
2048 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2049 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2052 /* in text mode, strip \r if followed by \n.
2053 * BUG: should save state across calls somehow, so CR LF that
2054 * straddles buffer boundary gets recognized properly?
2056 if ((bufstart
[i
] != '\r')
2057 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
2058 bufstart
[j
++] = bufstart
[i
];
2065 if (GetLastError() == ERROR_BROKEN_PIPE
)
2067 TRACE(":end-of-pipe\n");
2068 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2073 TRACE(":failed-last error (%d)\n",GetLastError());
2079 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2083 /*********************************************************************
2086 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2089 num_read
= read_i(fd
, buf
, count
);
2093 /*********************************************************************
2094 * _setmode (MSVCRT.@)
2096 int CDECL
_setmode(int fd
,int mode
)
2098 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2099 if(ret
==_O_TEXT
&& (get_ioinfo(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2102 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2103 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2108 if(mode
== _O_BINARY
) {
2109 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2110 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2114 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2116 get_ioinfo(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2117 else if(mode
== _O_U8TEXT
)
2118 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2120 get_ioinfo(fd
)->exflag
= (get_ioinfo(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2126 /*********************************************************************
2129 long CDECL
_tell(int fd
)
2131 return _lseek(fd
, 0, SEEK_CUR
);
2134 /*********************************************************************
2135 * _telli64 (MSVCRT.@)
2137 __int64 CDECL
_telli64(int fd
)
2139 return _lseeki64(fd
, 0, SEEK_CUR
);
2142 /*********************************************************************
2143 * _tempnam (MSVCRT.@)
2145 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2147 char tmpbuf
[MAX_PATH
];
2148 const char *tmp_dir
= getenv("TMP");
2150 if (tmp_dir
) dir
= tmp_dir
;
2152 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2153 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2155 TRACE("got name (%s)\n",tmpbuf
);
2156 DeleteFileA(tmpbuf
);
2157 return _strdup(tmpbuf
);
2159 TRACE("failed (%d)\n",GetLastError());
2163 /*********************************************************************
2164 * _wtempnam (MSVCRT.@)
2166 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2168 wchar_t tmpbuf
[MAX_PATH
];
2170 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2171 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2173 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2174 DeleteFileW(tmpbuf
);
2175 return _wcsdup(tmpbuf
);
2177 TRACE("failed (%d)\n",GetLastError());
2181 /*********************************************************************
2184 int CDECL
_umask(int umask
)
2186 int old_umask
= umask
;
2187 TRACE("(%d)\n",umask
);
2188 MSVCRT_umask
= umask
;
2192 /*********************************************************************
2195 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2198 HANDLE hand
= fdtoh(fd
);
2200 /* Don't trace small writes, it gets *very* annoying */
2203 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2205 if (hand
== INVALID_HANDLE_VALUE
)
2211 /* If appending, go to EOF */
2212 if (get_ioinfo(fd
)->wxflag
& WX_APPEND
)
2213 _lseek(fd
, 0, FILE_END
);
2215 if (!(get_ioinfo(fd
)->wxflag
& WX_TEXT
))
2217 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2218 && (num_written
== count
))
2220 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2221 hand
, GetLastError());
2226 unsigned int i
, j
, nr_lf
;
2229 const char *s
= buf
, *buf_start
= buf
;
2230 /* find number of \n ( without preceding \r ) */
2231 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2236 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2241 if ((q
= p
= malloc(count
+ nr_lf
)))
2243 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2248 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2255 FIXME("Malloc failed\n");
2263 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2265 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2266 fd
, hand
, GetLastError(), num_written
);
2270 return s
- buf_start
;
2282 /*********************************************************************
2285 int CDECL
_putw(int val
, FILE* file
)
2290 len
= _write(file
->_file
, &val
, sizeof(val
));
2291 if (len
== sizeof(val
)) {
2296 file
->_flag
|= _IOERR
;
2301 /*********************************************************************
2304 int CDECL
fclose(FILE* file
)
2310 free(file
->_tmpfname
);
2311 file
->_tmpfname
= NULL
;
2312 /* flush stdio buffers */
2313 if(file
->_flag
& _IOWRT
)
2315 if(file
->_flag
& _IOMYBUF
)
2318 r
=_close(file
->_file
);
2322 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2323 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2325 if(file
== get_file(stream_idx
-1)) {
2326 while(stream_idx
>3 && !file
->_flag
) {
2328 file
= get_file(stream_idx
-1);
2332 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2335 /*********************************************************************
2338 int CDECL
feof(FILE* file
)
2340 return file
->_flag
& _IOEOF
;
2343 /*********************************************************************
2346 int CDECL
ferror(FILE* file
)
2348 return file
->_flag
& _IOERR
;
2351 /*********************************************************************
2352 * _filbuf (MSVCRT.@)
2354 int CDECL
_filbuf(FILE* file
)
2359 if(file
->_flag
& _IOSTRG
) {
2364 /* Allocate buffer if needed */
2365 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2368 if(!(file
->_flag
& _IOREAD
)) {
2369 if(file
->_flag
& _IORW
)
2370 file
->_flag
|= _IOREAD
;
2377 if(file
->_flag
& _IONBF
) {
2379 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2380 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2388 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2390 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2397 file
->_ptr
= file
->_base
+1;
2398 c
= *(unsigned char *)file
->_base
;
2404 /*********************************************************************
2407 int CDECL
fgetc(FILE* file
)
2415 i
= (unsigned char *)file
->_ptr
++;
2424 /*********************************************************************
2425 * _fgetchar (MSVCRT.@)
2427 int CDECL
_fgetchar(void)
2429 return fgetc(stdin
);
2432 /*********************************************************************
2435 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2438 char * buf_start
= s
;
2440 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2441 file
,file
->_file
,s
,size
);
2445 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2450 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2452 TRACE(":nothing read\n");
2456 if ((cc
!= EOF
) && (size
> 1))
2459 TRACE(":got %s\n", debugstr_a(buf_start
));
2464 /*********************************************************************
2467 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2468 * the CR from CR/LF combinations
2470 wint_t CDECL
fgetwc(FILE* file
)
2475 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2482 for(i
=0; i
<sizeof(wc
); i
++)
2495 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2510 if ((__mb_cur_max
> 1) && isleadbyte(c
))
2512 FIXME("Treat Multibyte characters\n");
2522 /*********************************************************************
2525 int CDECL
_getw(FILE* file
)
2533 for (j
=0; j
<sizeof(int); j
++) {
2536 file
->_flag
|= _IOEOF
;
2547 /*********************************************************************
2550 wint_t CDECL
getwc(FILE* file
)
2552 return fgetwc(file
);
2555 /*********************************************************************
2556 * _fgetwchar (MSVCRT.@)
2558 wint_t CDECL
_fgetwchar(void)
2560 return fgetwc(stdin
);
2563 /*********************************************************************
2564 * getwchar (MSVCRT.@)
2566 wint_t CDECL
getwchar(void)
2568 return _fgetwchar();
2571 /*********************************************************************
2574 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2577 wchar_t * buf_start
= s
;
2579 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2580 file
,file
->_file
,s
,size
);
2584 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2589 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2591 TRACE(":nothing read\n");
2595 if ((cc
!= WEOF
) && (size
> 1))
2598 TRACE(":got %s\n", debugstr_w(buf_start
));
2603 /*********************************************************************
2606 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2608 size_t wrcnt
=size
* nmemb
;
2617 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2618 memcpy(file
->_ptr
, ptr
, pcnt
);
2623 ptr
= (const char*)ptr
+ pcnt
;
2624 } else if(!file
->_bufsiz
&& (file
->_flag
& _IONBF
)) {
2625 if(!(file
->_flag
& _IOWRT
)) {
2626 if(file
->_flag
& _IORW
)
2627 file
->_flag
|= _IOWRT
;
2632 if(_write(file
->_file
, ptr
, wrcnt
) <= 0) {
2633 file
->_flag
|= _IOERR
;
2639 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
2643 ptr
= (const char*)ptr
+ 1;
2648 return written
/ size
;
2651 /*********************************************************************
2653 * FORKED for ReactOS, don't sync with Wine!
2655 * - http://jira.reactos.org/browse/CORE-6495
2656 * - http://bugs.winehq.org/show_bug.cgi?id=8598
2658 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2660 /* If this is a real file stream (and not some temporary one for
2661 sprintf-like functions), check whether it is opened in text mode.
2662 In this case, we have to perform an implicit conversion to ANSI. */
2663 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
2665 /* Convert to multibyte in text mode */
2666 char mbc
[MB_LEN_MAX
];
2669 mb_return
= wctomb(mbc
, c
);
2674 /* Output all characters */
2675 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2680 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2687 /*********************************************************************
2688 * _fputwchar (MSVCRT.@)
2690 wint_t CDECL
_fputwchar(wint_t wc
)
2692 return fputwc(wc
, stdout
);
2695 /*********************************************************************
2696 * _wfsopen (MSVCRT.@)
2698 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2701 int open_flags
, stream_flags
, fd
;
2703 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2705 /* map mode string to open() flags. "man fopen" for possibilities. */
2706 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2710 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2713 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2715 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2722 TRACE(":got (%p)\n",file
);
2723 if (fd
>= 0 && !file
)
2729 /*********************************************************************
2730 * _fsopen (MSVCRT.@)
2732 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2735 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2737 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2738 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2742 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2745 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2750 ret
= _wfsopen(pathW
, modeW
, share
);
2757 /*********************************************************************
2760 FILE * CDECL
fopen(const char *path
, const char *mode
)
2762 return _fsopen( path
, mode
, _SH_DENYNO
);
2765 /*********************************************************************
2766 * fopen_s (MSVCRT.@)
2768 int CDECL
fopen_s(FILE** pFile
,
2769 const char *filename
, const char *mode
)
2771 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2772 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2777 *pFile
= fopen(filename
, mode
);
2784 /*********************************************************************
2785 * _wfopen (MSVCRT.@)
2787 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2789 return _wfsopen( path
, mode
, _SH_DENYNO
);
2792 /*********************************************************************
2793 * _wfopen_s (MSVCRT.@)
2795 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
2796 const wchar_t *mode
)
2798 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2799 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2804 *pFile
= _wfopen(filename
, mode
);
2811 /* fputc calls _flsbuf which calls fputc */
2812 int CDECL
_flsbuf(int c
, FILE* file
);
2814 /*********************************************************************
2817 int CDECL
fputc(int c
, FILE* file
)
2827 res
= flush_buffer(file
);
2829 return res
? res
: c
;
2836 res
= _flsbuf(c
, file
);
2842 /*********************************************************************
2843 * _fputchar (MSVCRT.@)
2845 int CDECL
_fputchar(int c
)
2847 return fputc(c
, stdout
);
2850 /*********************************************************************
2853 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2855 size_t rcnt
=size
* nmemb
;
2864 /* first buffered data */
2866 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
2867 memcpy(ptr
, file
->_ptr
, pcnt
);
2872 ptr
= (char*)ptr
+ pcnt
;
2873 } else if(!(file
->_flag
& _IOREAD
)) {
2874 if(file
->_flag
& _IORW
) {
2875 file
->_flag
|= _IOREAD
;
2884 /* Fill the buffer on small reads.
2885 * TODO: Use a better buffering strategy.
2887 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2888 if (file
->_bufsiz
== 0) {
2891 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2892 file
->_ptr
= file
->_base
;
2893 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2894 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2895 if (i
> 0 && i
< file
->_cnt
) {
2896 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
2897 file
->_flag
&= ~_IOEOF
;
2900 memcpy(ptr
, file
->_ptr
, i
);
2905 i
= _read(file
->_file
,ptr
, rcnt
);
2909 ptr
= (char *)ptr
+i
;
2910 /* expose feof condition in the flags
2911 * MFC tests file->_flag for feof, and doesn't call feof())
2913 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
2914 file
->_flag
|= _IOEOF
;
2917 file
->_flag
|= _IOERR
;
2928 /*********************************************************************
2929 * _wfreopen (MSVCRT.@)
2932 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
2934 int open_flags
, stream_flags
, fd
;
2936 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
2939 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2944 /* map mode string to open() flags. "man fopen" for possibilities. */
2945 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2949 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2952 else if (init_fp(file
, fd
, stream_flags
) == -1)
2955 WARN(":failed-last error (%d)\n",GetLastError());
2956 _dosmaperr(GetLastError());
2965 /*********************************************************************
2966 * freopen (MSVCRT.@)
2969 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
2972 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2974 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2975 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2981 ret
= _wfreopen(pathW
, modeW
, file
);
2988 /*********************************************************************
2989 * fsetpos (MSVCRT.@)
2991 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2996 /* Note that all this has been lifted 'as is' from fseek */
2997 if(file
->_flag
& _IOWRT
)
3000 /* Discard buffered input */
3002 file
->_ptr
= file
->_base
;
3004 /* Reset direction of i/o */
3005 if(file
->_flag
& _IORW
) {
3006 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3009 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3014 /*********************************************************************
3015 * _ftelli64 (MSVCRT.@)
3017 __int64 CDECL
_ftelli64(FILE* file
)
3022 pos
= _telli64(file
->_file
);
3028 if(file
->_flag
& _IOWRT
) {
3029 pos
+= file
->_ptr
- file
->_base
;
3031 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3034 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3038 } else if(!file
->_cnt
) { /* nothing to do */
3039 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3043 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3044 for(i
=0; i
<file
->_cnt
; i
++)
3045 if(file
->_ptr
[i
] == '\n')
3051 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3056 pos
-= file
->_bufsiz
;
3057 pos
+= file
->_ptr
- file
->_base
;
3059 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3060 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
3063 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3074 /*********************************************************************
3077 LONG CDECL
ftell(FILE* file
)
3079 return (LONG
)_ftelli64(file
);
3082 /*********************************************************************
3083 * fgetpos (MSVCRT.@)
3085 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3090 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
3096 if( file
->_flag
& _IOWRT
) {
3097 off
= file
->_ptr
- file
->_base
;
3100 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3101 /* Black magic correction for CR removal */
3103 for (i
=0; i
<file
->_cnt
; i
++) {
3104 if (file
->_ptr
[i
] == '\n')
3107 /* Black magic when reading CR at buffer boundary*/
3108 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3118 /*********************************************************************
3121 int CDECL
fputs(const char *s
, FILE* file
)
3123 size_t i
, len
= strlen(s
);
3127 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3128 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3132 for (i
=0; i
<len
; i
++)
3133 if (fputc(s
[i
], file
) == EOF
) {
3142 /*********************************************************************
3145 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3147 size_t i
, len
= strlenW(s
);
3151 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3152 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3156 for (i
=0; i
<len
; i
++) {
3157 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
3158 || fputwc(s
[i
], file
) == WEOF
) {
3168 /*********************************************************************
3169 * getchar (MSVCRT.@)
3171 int CDECL
getchar(void)
3173 return fgetc(stdin
);
3176 /*********************************************************************
3179 int CDECL
getc(FILE* file
)
3184 /*********************************************************************
3187 char * CDECL
gets(char *buf
)
3190 char * buf_start
= buf
;
3193 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3195 if(cc
!= '\r') *buf
++ = (char)cc
;
3199 TRACE("got '%s'\n", buf_start
);
3200 _unlock_file(stdin
);
3204 /*********************************************************************
3207 wchar_t* CDECL
_getws(wchar_t* buf
)
3213 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3217 *buf
++ = (wchar_t)cc
;
3221 TRACE("got %s\n", debugstr_w(ws
));
3222 _unlock_file(stdin
);
3226 /*********************************************************************
3229 int CDECL
putc(int c
, FILE* file
)
3231 return fputc(c
, file
);
3234 /*********************************************************************
3235 * putchar (MSVCRT.@)
3237 int CDECL
putchar(int c
)
3239 return fputc(c
, stdout
);
3242 /*********************************************************************
3243 * _putwch (MSVCRT.@)
3245 wint_t CDECL
_putwch(wchar_t c
)
3247 return fputwc(c
, stdout
);
3250 /*********************************************************************
3253 int CDECL
puts(const char *s
)
3255 size_t len
= strlen(s
);
3259 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3260 _unlock_file(stdout
);
3264 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3265 _unlock_file(stdout
);
3269 /*********************************************************************
3272 int CDECL
_putws(const wchar_t *s
)
3274 static const wchar_t nl
= '\n';
3275 size_t len
= strlenW(s
);
3279 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3280 _unlock_file(stdout
);
3284 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3285 _unlock_file(stdout
);
3289 /*********************************************************************
3292 int CDECL
remove(const char *path
)
3294 TRACE("(%s)\n",path
);
3295 if (DeleteFileA(path
))
3297 TRACE(":failed (%d)\n",GetLastError());
3298 _dosmaperr(GetLastError());
3302 /*********************************************************************
3303 * _wremove (MSVCRT.@)
3305 int CDECL
_wremove(const wchar_t *path
)
3307 TRACE("(%s)\n",debugstr_w(path
));
3308 if (DeleteFileW(path
))
3310 TRACE(":failed (%d)\n",GetLastError());
3311 _dosmaperr(GetLastError());
3315 /*********************************************************************
3318 int CDECL
rename(const char *oldpath
,const char *newpath
)
3320 TRACE(":from %s to %s\n",oldpath
,newpath
);
3321 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3323 TRACE(":failed (%d)\n",GetLastError());
3324 _dosmaperr(GetLastError());
3328 /*********************************************************************
3329 * _wrename (MSVCRT.@)
3331 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3333 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3334 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3336 TRACE(":failed (%d)\n",GetLastError());
3337 _dosmaperr(GetLastError());
3341 /*********************************************************************
3342 * setvbuf (MSVCRT.@)
3344 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3352 if(mode
== _IOFBF
) {
3353 file
->_flag
&= ~_IONBF
;
3354 file
->_base
= file
->_ptr
= buf
;
3356 file
->_bufsiz
= size
;
3359 file
->_flag
|= _IONBF
;
3365 /*********************************************************************
3368 void CDECL
setbuf(FILE* file
, char *buf
)
3370 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3373 /*********************************************************************
3376 char * CDECL
tmpnam(char *s
)
3383 thread_data_t
*data
= msvcrt_get_thread_data();
3385 if(!data
->tmpnam_buffer
)
3386 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3388 s
= data
->tmpnam_buffer
;
3391 int_to_base32(GetCurrentProcessId(), tmpstr
);
3392 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3393 for (count
= 0; count
< TMP_MAX
; count
++)
3395 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3396 memcpy(p
, tmpstr
, size
);
3397 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3398 GetLastError() == ERROR_FILE_NOT_FOUND
)
3404 /*********************************************************************
3405 * _wtmpnam (MSVCRT.@)
3407 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3409 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3414 thread_data_t
*data
= msvcrt_get_thread_data();
3416 if(!data
->wtmpnam_buffer
)
3417 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3419 s
= data
->wtmpnam_buffer
;
3422 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3423 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3424 for (count
= 0; count
< TMP_MAX
; count
++)
3426 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3427 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3428 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3429 GetLastError() == ERROR_FILE_NOT_FOUND
)
3435 /*********************************************************************
3436 * tmpfile (MSVCRT.@)
3438 FILE* CDECL
tmpfile(void)
3440 char *filename
= tmpnam(NULL
);
3445 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3446 _S_IREAD
| _S_IWRITE
);
3447 if (fd
!= -1 && (file
= alloc_fp()))
3449 if (init_fp(file
, fd
, _IORW
) == -1)
3454 else file
->_tmpfname
= _strdup(filename
);
3457 if(fd
!= -1 && !file
)
3463 /*********************************************************************
3466 int CDECL
ungetc(int c
, FILE * file
)
3472 if(file
->_bufsiz
== 0) {
3476 if(file
->_ptr
>file
->_base
) {
3489 /*********************************************************************
3490 * ungetwc (MSVCRT.@)
3492 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3495 char * pp
= (char *)&mwc
;
3499 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3500 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3512 /*********************************************************************
3513 * _getmaxstdio (MSVCRT.@)
3515 int CDECL
_getmaxstdio(void)
3520 /*********************************************************************
3521 * _setmaxstdio (MSVCRT.@)
3523 int CDECL
_setmaxstdio(int newmax
)
3525 TRACE("%d\n", newmax
);
3527 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3530 max_streams
= newmax
;