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);
80 /* _access() bit flags FIXME: incomplete */
81 /* defined in crt/io.h */
83 /* values for wxflag in file descriptor */
86 #define WX_READNL 0x04 /* read started with \n */
87 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
89 #define WX_READCR 0x08 /* underlying file is at \r */
90 #define WX_DONTINHERIT 0x10
91 #define WX_APPEND 0x20
92 #define WX_NOSEEK 0x40
95 /* FIXME: this should be allocated dynamically */
96 #define MAX_FILES 2048
97 #define FD_BLOCK_SIZE 64
101 unsigned char wxflag
;
104 CRITICAL_SECTION crit
;
107 /*********************************************************************
108 * __pioinfo (MSVCRT.@)
109 * array of pointers to ioinfo arrays [64]
111 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
113 /*********************************************************************
114 * __badioinfo (MSVCRT.@)
116 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
118 static int fdstart
= 3; /* first unallocated fd */
119 static int fdend
= 3; /* highest allocated fd */
123 CRITICAL_SECTION crit
;
126 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
127 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
128 static int max_streams
= 512, stream_idx
;
130 /* INTERNAL: process umask */
131 static int MSVCRT_umask
= 0;
133 /* INTERNAL: static data for tmpnam and _wtmpname functions */
134 static int tmpnam_unique
;
136 /* This critical section protects the tables __pioinfo and fstreams,
137 * and their related indexes, fdstart, fdend,
138 * and stream_idx, from race conditions.
139 * It doesn't protect against race conditions manipulating the underlying files
140 * or flags; doing so would probably be better accomplished with per-file
141 * protection, rather than locking the whole table for every change.
143 static CRITICAL_SECTION file_cs
;
144 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
145 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
147 static inline ioinfo
* get_ioinfo(int fd
)
151 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
155 return ret
+ (fd
%FD_BLOCK_SIZE
);
158 static inline FILE* get_file(int i
)
168 ret
= fstream
[i
/FD_BLOCK_SIZE
];
170 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
171 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
172 ERR("out of memory\n");
177 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
179 ret
+= i
%FD_BLOCK_SIZE
;
184 static inline BOOL
is_valid_fd(int fd
)
186 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
189 /* INTERNAL: Get the HANDLE for a fd
190 * This doesn't lock the table, because a failure will result in
191 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
192 * it returns a valid handle which is about to be closed, a subsequent call
193 * will fail, most likely in a sane way.
195 /*static*/ HANDLE
fdtoh(int fd
)
197 if (!is_valid_fd(fd
))
199 WARN(":fd (%d) - no handle!\n",fd
);
202 return INVALID_HANDLE_VALUE
;
204 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
205 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
206 return get_ioinfo(fd
)->handle
;
209 /* INTERNAL: free a file entry fd */
210 static void free_fd(int fd
)
216 fdinfo
= get_ioinfo(fd
);
217 old_handle
= fdinfo
->handle
;
218 if(fdinfo
!= &__badioinfo
)
220 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
223 TRACE(":fd (%d) freed\n",fd
);
224 if (fd
< 3) /* don't use 0,1,2 for user files */
229 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
232 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
235 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
249 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
250 /* caller must hold the files lock */
251 static int alloc_fd_from(HANDLE hand
, int flag
, int fd
)
257 WARN(":files exhausted!\n");
262 fdinfo
= get_ioinfo(fd
);
263 if(fdinfo
== &__badioinfo
) {
266 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
267 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
268 WARN(":out of memory!\n");
273 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
274 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
276 fdinfo
= get_ioinfo(fd
);
279 fdinfo
->handle
= hand
;
280 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
282 /* locate next free slot */
283 if (fd
== fdstart
&& fd
== fdend
)
286 while (fdstart
< fdend
&&
287 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
289 /* update last fd in use */
292 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
296 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
297 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
298 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
304 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
305 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
310 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
311 ret
= alloc_fd_from(hand
, flag
, fdstart
);
316 /* INTERNAL: Allocate a FILE* for an fd slot */
317 /* caller must hold the files lock */
318 static FILE* alloc_fp(void)
323 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
329 if (file
->_flag
== 0)
331 if (i
== stream_idx
) stream_idx
++;
339 /* INTERNAL: initialize a FILE* from an open fd */
340 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
342 TRACE(":fd (%d) allocating FILE*\n",fd
);
343 if (!is_valid_fd(fd
))
345 WARN(":invalid fd %d\n",fd
);
350 memset(file
, 0, sizeof(*file
));
352 file
->_flag
= stream_flags
;
354 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
355 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
357 TRACE(":got FILE* (%p)\n",file
);
361 /* INTERNAL: Create an inheritance data block (for spawned process)
362 * The inheritance block is made of:
363 * 00 int nb of file descriptor (NBFD)
364 * 04 char file flags (wxflag): repeated for each fd
365 * 4+NBFD HANDLE file handle: repeated for each fd
367 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
374 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
375 *block
= calloc(*size
, 1);
381 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
382 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
384 *(unsigned*)*block
= fdend
;
385 for (fd
= 0; fd
< fdend
; fd
++)
387 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
388 fdinfo
= get_ioinfo(fd
);
389 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
391 *wxflag_ptr
= fdinfo
->wxflag
;
392 *handle_ptr
= fdinfo
->handle
;
397 *handle_ptr
= INVALID_HANDLE_VALUE
;
399 wxflag_ptr
++; handle_ptr
++;
404 /* INTERNAL: Set up all file descriptors,
405 * as well as default streams (stdin, stderr and stdout)
407 void msvcrt_init_io(void)
413 InitializeCriticalSection(&file_cs
);
414 file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_cs");
415 GetStartupInfoA(&si
);
416 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
422 count
= *(unsigned*)si
.lpReserved2
;
423 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
424 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
426 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
427 count
= min(count
, MAX_FILES
);
428 for (i
= 0; i
< count
; i
++)
430 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
431 alloc_fd_from(*handle_ptr
, *wxflag_ptr
, i
);
433 wxflag_ptr
++; handle_ptr
++;
435 fdend
= max( 3, count
);
436 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
437 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
441 alloc_fd_from(INVALID_HANDLE_VALUE
, 0, 3);
443 fdinfo
= get_ioinfo(0);
444 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
446 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
448 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
449 GetCurrentProcess(), &fdinfo
->handle
,
450 0, TRUE
, DUPLICATE_SAME_ACCESS
))
452 fdinfo
->handle
= std
;
454 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
457 fdinfo
= get_ioinfo(1);
458 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
460 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
462 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
463 GetCurrentProcess(), &fdinfo
->handle
,
464 0, TRUE
, DUPLICATE_SAME_ACCESS
))
466 fdinfo
->handle
= std
;
468 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
471 fdinfo
= get_ioinfo(2);
472 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
474 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
476 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
477 GetCurrentProcess(), &fdinfo
->handle
,
478 0, TRUE
, DUPLICATE_SAME_ACCESS
))
480 fdinfo
->handle
= std
;
482 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
485 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(0)->handle
,
486 get_ioinfo(1)->handle
, get_ioinfo(2)->handle
);
488 memset(_iob
,0,3*sizeof(FILE));
489 for (i
= 0; i
< 3; i
++)
491 /* FILE structs for stdin/out/err are static and never deleted */
493 _iob
[i
]._tmpfname
= NULL
;
494 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
499 /* INTERNAL: Flush stdio file buffer */
500 static int flush_buffer(FILE* file
)
503 int cnt
=file
->_ptr
-file
->_base
;
504 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
505 file
->_flag
|= _IOERR
;
508 file
->_ptr
=file
->_base
;
509 file
->_cnt
=file
->_bufsiz
;
514 /* INTERNAL: Allocate stdio file buffer */
515 /*static*/ void alloc_buffer(FILE* file
)
517 file
->_base
= calloc(BUFSIZ
,1);
519 file
->_bufsiz
= BUFSIZ
;
520 file
->_flag
|= _IOMYBUF
;
522 file
->_base
= (char*)(&file
->_charbuf
);
524 file
->_bufsiz
= sizeof(file
->_charbuf
);
526 file
->_ptr
= file
->_base
;
530 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
531 static int int_to_base32(int num
, char *str
)
546 *p
= (num
& 31) + '0';
548 *p
+= ('a' - '0' - 10);
555 /* INTERNAL: wide character version of int_to_base32 */
556 static int int_to_base32_w(int num
, wchar_t *str
)
571 *p
= (num
& 31) + '0';
573 *p
+= ('a' - '0' - 10);
580 /* INTERNAL: Create a wide string from an ascii string */
581 wchar_t *msvcrt_wstrdupa(const char *str
)
583 const unsigned int len
= strlen(str
) + 1 ;
584 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
587 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
591 /*********************************************************************
592 * __iob_func(MSVCRT.@)
594 FILE * CDECL
__iob_func(void)
599 /*********************************************************************
602 int CDECL
_access(const char *filename
, int mode
)
604 DWORD attr
= GetFileAttributesA(filename
);
606 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
608 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
610 _dosmaperr(GetLastError());
613 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
615 _set_errno(ERROR_ACCESS_DENIED
);
621 /*********************************************************************
622 * _access_s (MSVCRT.@)
624 int CDECL
_access_s(const char *filename
, int mode
)
626 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
627 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
633 return _access(filename
, mode
);
636 /*********************************************************************
637 * _waccess (MSVCRT.@)
639 int CDECL
_waccess(const wchar_t *filename
, int mode
)
641 DWORD attr
= GetFileAttributesW(filename
);
643 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
645 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
647 _dosmaperr(GetLastError());
650 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
652 _set_errno(ERROR_ACCESS_DENIED
);
658 /*********************************************************************
659 * _waccess_s (MSVCRT.@)
661 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
663 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
664 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
670 return _waccess(filename
, mode
);
673 /*********************************************************************
676 int CDECL
_chmod(const char *path
, int flags
)
678 DWORD oldFlags
= GetFileAttributesA(path
);
680 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
682 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
683 oldFlags
| FILE_ATTRIBUTE_READONLY
;
685 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
688 _dosmaperr(GetLastError());
692 /*********************************************************************
695 int CDECL
_wchmod(const wchar_t *path
, int flags
)
697 DWORD oldFlags
= GetFileAttributesW(path
);
699 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
701 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
702 oldFlags
| FILE_ATTRIBUTE_READONLY
;
704 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
707 _dosmaperr(GetLastError());
711 /*********************************************************************
714 int CDECL
_unlink(const char *path
)
716 TRACE("%s\n",debugstr_a(path
));
717 if(DeleteFileA(path
))
719 TRACE("failed (%d)\n",GetLastError());
720 _dosmaperr(GetLastError());
724 /*********************************************************************
725 * _wunlink (MSVCRT.@)
727 int CDECL
_wunlink(const wchar_t *path
)
729 TRACE("(%s)\n",debugstr_w(path
));
730 if(DeleteFileW(path
))
732 TRACE("failed (%d)\n",GetLastError());
733 _dosmaperr(GetLastError());
737 /* _flushall calls fflush which calls _flushall */
738 int CDECL
fflush(FILE* file
);
740 /* INTERNAL: Flush all stream buffer */
741 static int flush_all_buffers(int mask
)
743 int i
, num_flushed
= 0;
747 for (i
= 3; i
< stream_idx
; i
++) {
752 if(file
->_flag
& mask
) {
760 TRACE(":flushed (%d) handles\n",num_flushed
);
764 /*********************************************************************
765 * _flushall (MSVCRT.@)
767 int CDECL
_flushall(void)
769 return flush_all_buffers(_IOWRT
| _IOREAD
);
772 /*********************************************************************
775 int CDECL
fflush(FILE* file
)
778 flush_all_buffers(_IOWRT
);
779 } else if(file
->_flag
& _IOWRT
) {
783 res
= flush_buffer(file
);
785 if(!res && (file->_flag & _IOCOMMIT))
786 res = _commit(file->_file) ? EOF : 0;
791 } else if(file
->_flag
& _IOREAD
) {
794 file
->_ptr
= file
->_base
;
802 /*********************************************************************
805 int CDECL
_close(int fd
)
812 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
813 if (hand
== INVALID_HANDLE_VALUE
)
815 else if (!CloseHandle(hand
))
817 WARN(":failed-last error (%d)\n",GetLastError());
818 _dosmaperr(GetLastError());
831 /*********************************************************************
834 int CDECL
_commit(int fd
)
836 HANDLE hand
= fdtoh(fd
);
838 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
839 if (hand
== INVALID_HANDLE_VALUE
)
842 if (!FlushFileBuffers(hand
))
844 if (GetLastError() == ERROR_INVALID_HANDLE
)
846 /* FlushFileBuffers fails for console handles
847 * so we ignore this error.
851 TRACE(":failed-last error (%d)\n",GetLastError());
852 _dosmaperr(GetLastError());
859 /*********************************************************************
862 * MSDN isn't clear on this point, but the remarks for _pipe
863 * indicate file descriptors duplicated with _dup and _dup2 are always
866 int CDECL
_dup2(int od
, int nd
)
870 TRACE("(od=%d, nd=%d)\n", od
, nd
);
872 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
876 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
877 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
879 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
883 ret
= alloc_fd_from(handle
, wxflag
, nd
);
891 /* _dup2 returns 0, not nd, on success */
898 _dosmaperr(GetLastError());
910 /*********************************************************************
913 int CDECL
_dup(int od
)
919 if (_dup2(od
, fd
) == 0)
927 /*********************************************************************
930 int CDECL
_eof(int fd
)
933 LONG hcurpos
,hendpos
;
934 HANDLE hand
= fdtoh(fd
);
936 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
938 if (hand
== INVALID_HANDLE_VALUE
)
941 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
943 /* Otherwise we do it the hard way */
944 hcurpos
= hendpos
= 0;
945 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
946 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
948 if (curpos
== endpos
&& hcurpos
== hendpos
)
950 /* FIXME: shouldn't WX_ATEOF be set here? */
954 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
958 /*********************************************************************
959 * _fcloseall (MSVCRT.@)
961 int CDECL
_fcloseall(void)
963 int num_closed
= 0, i
;
967 for (i
= 3; i
< stream_idx
; i
++) {
970 if (file
->_flag
&& !fclose(file
))
975 TRACE(":closed (%d) handles\n",num_closed
);
979 /* free everything on process exit */
980 void msvcrt_free_io(void)
985 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
986 * stdout, and stderr (unlike GNU), so we need to fclose() them here
987 * or they won't get flushed.
993 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
996 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
999 file_cs
.DebugInfo
->Spare
[0] = 0;
1000 DeleteCriticalSection(&file_cs
);
1003 /*********************************************************************
1004 * _lseeki64 (MSVCRT.@)
1006 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1008 HANDLE hand
= fdtoh(fd
);
1011 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1012 if (hand
== INVALID_HANDLE_VALUE
)
1015 if (whence
< 0 || whence
> 2)
1021 TRACE(":fd (%d) to %s pos %s\n",
1022 fd
,wine_dbgstr_longlong(offset
),
1023 (whence
==SEEK_SET
)?"SEEK_SET":
1024 (whence
==SEEK_CUR
)?"SEEK_CUR":
1025 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1027 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1028 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1029 ofs
.QuadPart
= offset
;
1030 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1031 GetLastError() == ERROR_SUCCESS
)
1033 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1034 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1036 return ofs
.QuadPart
;
1038 TRACE(":error-last error (%d)\n",GetLastError());
1039 _dosmaperr(GetLastError());
1043 /*********************************************************************
1046 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1048 return (LONG
)_lseeki64(fd
, offset
, whence
);
1051 /*********************************************************************
1052 * _lock_file (MSVCRT.@)
1054 void CDECL
_lock_file(FILE *file
)
1056 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1057 _lock(_STREAM_LOCKS
+(file
-_iob
));
1058 /* ReactOS: string streams dont need to be locked */
1059 else if(!(file
->_flag
& _IOSTRG
))
1060 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1063 /*********************************************************************
1064 * _unlock_file (MSVCRT.@)
1066 void CDECL
_unlock_file(FILE *file
)
1068 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1069 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1070 /* ReactOS: string streams dont need to be locked */
1071 else if(!(file
->_flag
& _IOSTRG
))
1072 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1076 /*********************************************************************
1077 * _locking (MSVCRT.@)
1079 * This is untested; the underlying LockFile doesn't work yet.
1081 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1085 HANDLE hand
= fdtoh(fd
);
1087 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1088 if (hand
== INVALID_HANDLE_VALUE
)
1091 if (mode
< 0 || mode
> 4)
1097 TRACE(":fd (%d) by 0x%08x mode %s\n",
1098 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1099 (mode
==_LK_LOCK
)?"_LK_LOCK":
1100 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1101 (mode
==_LK_RLCK
)?"_LK_RLCK":
1102 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1105 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1107 FIXME ("Seek failed\n");
1108 *_errno() = EINVAL
; /* FIXME */
1111 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1114 ret
= 1; /* just to satisfy gcc */
1117 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1122 else if (mode
== _LK_UNLCK
)
1123 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1125 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1126 /* FIXME - what about error settings? */
1127 return ret
? 0 : -1;
1130 /*********************************************************************
1131 * _fseeki64 (MSVCRT.@)
1133 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1138 /* Flush output if needed */
1139 if(file
->_flag
& _IOWRT
)
1142 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1143 offset
-= file
->_cnt
;
1144 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1145 /* Black magic correction for CR removal */
1147 for (i
=0; i
<file
->_cnt
; i
++) {
1148 if (file
->_ptr
[i
] == '\n')
1151 /* Black magic when reading CR at buffer boundary*/
1152 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1156 /* Discard buffered input */
1158 file
->_ptr
= file
->_base
;
1159 /* Reset direction of i/o */
1160 if(file
->_flag
& _IORW
) {
1161 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1163 /* Clear end of file flag */
1164 file
->_flag
&= ~_IOEOF
;
1165 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1171 /*********************************************************************
1174 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1176 return _fseeki64( file
, offset
, whence
);
1179 /*********************************************************************
1180 * _chsize (MSVCRT.@)
1182 int CDECL
_chsize(int fd
, long size
)
1188 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1193 if (handle
!= INVALID_HANDLE_VALUE
)
1195 /* save the current file pointer */
1196 cur
= _lseek(fd
, 0, SEEK_CUR
);
1199 pos
= _lseek(fd
, size
, SEEK_SET
);
1202 ret
= SetEndOfFile(handle
);
1203 if (!ret
) _dosmaperr(GetLastError());
1206 /* restore the file pointer */
1207 _lseek(fd
, cur
, SEEK_SET
);
1212 return ret
? 0 : -1;
1215 /*********************************************************************
1216 * clearerr (MSVCRT.@)
1218 void CDECL
clearerr(FILE* file
)
1220 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1223 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1227 /*********************************************************************
1230 void CDECL
rewind(FILE* file
)
1232 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1235 fseek(file
, 0L, SEEK_SET
);
1240 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1242 int plus
= strchrW(mode
, '+') != NULL
;
1247 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1248 *stream_flags
= plus
? _IORW
: _IOREAD
;
1251 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1252 *stream_flags
= plus
? _IORW
: _IOWRT
;
1255 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1256 *stream_flags
= plus
? _IORW
: _IOWRT
;
1259 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1268 *open_flags
|= _O_BINARY
;
1269 *open_flags
&= ~_O_TEXT
;
1272 *open_flags
|= _O_TEXT
;
1273 *open_flags
&= ~_O_BINARY
;
1279 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1284 /*********************************************************************
1285 * _fdopen (MSVCRT.@)
1287 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1290 wchar_t *modeW
= NULL
;
1292 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1294 ret
= _wfdopen(fd
, modeW
);
1300 /*********************************************************************
1301 * _wfdopen (MSVCRT.@)
1303 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1305 int open_flags
, stream_flags
;
1308 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1311 if (!(file
= alloc_fp()))
1313 else if (init_fp(file
, fd
, stream_flags
) == -1)
1318 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1324 /*********************************************************************
1325 * _filelength (MSVCRT.@)
1327 LONG CDECL
_filelength(int fd
)
1329 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1332 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1335 if (endPos
!= curPos
)
1336 _lseek(fd
, curPos
, SEEK_SET
);
1343 /*********************************************************************
1344 * _filelengthi64 (MSVCRT.@)
1346 __int64 CDECL
_filelengthi64(int fd
)
1348 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1351 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1354 if (endPos
!= curPos
)
1355 _lseeki64(fd
, curPos
, SEEK_SET
);
1362 /*********************************************************************
1363 * _fileno (MSVCRT.@)
1365 int CDECL
_fileno(FILE* file
)
1367 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1371 /*********************************************************************
1372 * _get_osfhandle (MSVCRT.@)
1374 intptr_t CDECL
_get_osfhandle(int fd
)
1376 HANDLE hand
= fdtoh(fd
);
1377 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1379 return (intptr_t)hand
;
1382 /*********************************************************************
1383 * _isatty (MSVCRT.@)
1385 int CDECL
_isatty(int fd
)
1387 HANDLE hand
= fdtoh(fd
);
1389 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1390 if (hand
== INVALID_HANDLE_VALUE
)
1393 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1396 /*********************************************************************
1397 * _mktemp (MSVCRT.@)
1399 char * CDECL
_mktemp(char *pattern
)
1402 char *retVal
= pattern
;
1410 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1414 id
= GetCurrentProcessId();
1418 int tempNum
= id
/ 10;
1419 *pattern
-- = id
- (tempNum
* 10) + '0';
1425 *pattern
= letter
++;
1426 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1428 } while(letter
<= 'z');
1432 /*********************************************************************
1433 * _wmktemp (MSVCRT.@)
1435 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1438 wchar_t *retVal
= pattern
;
1440 wchar_t letter
= 'a';
1443 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1447 id
= GetCurrentProcessId();
1451 int tempNum
= id
/ 10;
1452 *pattern
-- = id
- (tempNum
* 10) + '0';
1458 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1459 GetLastError() == ERROR_FILE_NOT_FOUND
)
1461 *pattern
= letter
++;
1462 } while(letter
!= '|');
1466 /*static*/ unsigned split_oflags(unsigned oflags
)
1469 unsigned unsupp
; /* until we support everything */
1471 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1472 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1473 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1474 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1475 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1476 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1478 if ((unsupp
= oflags
& ~(
1479 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1480 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1481 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1483 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1485 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1490 /*********************************************************************
1493 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1496 SECURITY_ATTRIBUTES sa
;
1497 HANDLE readHandle
, writeHandle
;
1505 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1506 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1507 sa
.lpSecurityDescriptor
= NULL
;
1508 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1510 unsigned int wxflags
= split_oflags(textmode
);
1514 fd
= alloc_fd(readHandle
, wxflags
);
1518 fd
= alloc_fd(writeHandle
, wxflags
);
1527 CloseHandle(writeHandle
);
1533 CloseHandle(readHandle
);
1534 CloseHandle(writeHandle
);
1540 _dosmaperr(GetLastError());
1545 /*********************************************************************
1546 * _sopen_s (MSVCRT.@)
1548 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1550 DWORD access
= 0, creation
= 0, attrib
;
1554 SECURITY_ATTRIBUTES sa
;
1556 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1557 fd
, path
, oflags
, shflags
, pmode
);
1561 MSVCRT_INVALID_PMT("null out fd pointer", EINVAL
);
1566 wxflag
= split_oflags(oflags
);
1567 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1569 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1570 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1571 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1574 if (oflags
& _O_CREAT
)
1576 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1577 FIXME(": pmode 0x%04x ignored\n", pmode
);
1579 WARN(": pmode 0x%04x ignored\n", pmode
);
1581 if (oflags
& _O_EXCL
)
1582 creation
= CREATE_NEW
;
1583 else if (oflags
& _O_TRUNC
)
1584 creation
= CREATE_ALWAYS
;
1586 creation
= OPEN_ALWAYS
;
1588 else /* no _O_CREAT */
1590 if (oflags
& _O_TRUNC
)
1591 creation
= TRUNCATE_EXISTING
;
1593 creation
= OPEN_EXISTING
;
1602 sharing
= FILE_SHARE_READ
;
1605 sharing
= FILE_SHARE_WRITE
;
1608 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1611 ERR( "Unhandled shflags 0x%x\n", shflags
);
1614 attrib
= FILE_ATTRIBUTE_NORMAL
;
1616 if (oflags
& _O_TEMPORARY
)
1618 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1620 sharing
|= FILE_SHARE_DELETE
;
1623 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1624 sa
.lpSecurityDescriptor
= NULL
;
1625 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1627 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1628 if (hand
== INVALID_HANDLE_VALUE
) {
1629 WARN(":failed-last error (%d)\n", GetLastError());
1630 _dosmaperr(GetLastError());
1634 *fd
= alloc_fd(hand
, wxflag
);
1636 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1640 /*********************************************************************
1643 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1648 if (oflags
& _O_CREAT
)
1652 va_start(ap
, shflags
);
1653 pmode
= va_arg(ap
, int);
1659 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1663 /*********************************************************************
1664 * _wsopen_s (MSVCRT.@)
1666 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1668 DWORD access
= 0, creation
= 0, attrib
;
1669 SECURITY_ATTRIBUTES sa
;
1674 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1675 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1679 MSVCRT_INVALID_PMT("null out fd pointer", EINVAL
);
1684 wxflag
= split_oflags(oflags
);
1685 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1687 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1688 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1689 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1692 if (oflags
& _O_CREAT
)
1694 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1695 FIXME(": pmode 0x%04x ignored\n", pmode
);
1697 WARN(": pmode 0x%04x ignored\n", pmode
);
1699 if (oflags
& _O_EXCL
)
1700 creation
= CREATE_NEW
;
1701 else if (oflags
& _O_TRUNC
)
1702 creation
= CREATE_ALWAYS
;
1704 creation
= OPEN_ALWAYS
;
1706 else /* no _O_CREAT */
1708 if (oflags
& _O_TRUNC
)
1709 creation
= TRUNCATE_EXISTING
;
1711 creation
= OPEN_EXISTING
;
1720 sharing
= FILE_SHARE_READ
;
1723 sharing
= FILE_SHARE_WRITE
;
1726 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1729 ERR( "Unhandled shflags 0x%x\n", shflags
);
1732 attrib
= FILE_ATTRIBUTE_NORMAL
;
1734 if (oflags
& _O_TEMPORARY
)
1736 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1738 sharing
|= FILE_SHARE_DELETE
;
1741 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1742 sa
.lpSecurityDescriptor
= NULL
;
1743 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1745 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1747 if (hand
== INVALID_HANDLE_VALUE
) {
1748 WARN(":failed-last error (%d)\n",GetLastError());
1749 _dosmaperr(GetLastError());
1753 *fd
= alloc_fd(hand
, wxflag
);
1755 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1759 /*********************************************************************
1760 * _wsopen (MSVCRT.@)
1762 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1767 if (oflags
& _O_CREAT
)
1771 va_start(ap
, shflags
);
1772 pmode
= va_arg(ap
, int);
1778 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1782 /*********************************************************************
1785 int CDECL
_open( const char *path
, int flags
, ... )
1789 if (flags
& _O_CREAT
)
1792 va_start(ap
, flags
);
1793 pmode
= va_arg(ap
, int);
1795 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1798 return _sopen( path
, flags
, _SH_DENYNO
);
1801 /*********************************************************************
1804 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1808 if (flags
& _O_CREAT
)
1811 va_start(ap
, flags
);
1812 pmode
= va_arg(ap
, int);
1814 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1817 return _wsopen( path
, flags
, _SH_DENYNO
);
1820 /*********************************************************************
1823 int CDECL
_creat(const char *path
, int flags
)
1825 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1826 return _open(path
, usedFlags
);
1829 /*********************************************************************
1830 * _wcreat (MSVCRT.@)
1832 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1834 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1835 return _wopen(path
, usedFlags
);
1838 /*********************************************************************
1839 * _open_osfhandle (MSVCRT.@)
1841 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1846 /* _O_RDONLY (0) always matches, so set the read flag
1847 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1848 * file, so set the write flag. It also only sets _O_TEXT if it wants
1849 * text - it never sets _O_BINARY.
1851 /* don't let split_oflags() decide the mode if no mode is passed */
1852 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1853 oflags
|= _O_BINARY
;
1855 flags
= GetFileType((HANDLE
)handle
);
1856 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
1858 _dosmaperr(GetLastError());
1862 if (flags
== FILE_TYPE_CHAR
)
1864 else if (flags
== FILE_TYPE_PIPE
)
1868 flags
|= split_oflags(oflags
);
1870 fd
= alloc_fd((HANDLE
)handle
, flags
);
1871 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
1875 /*********************************************************************
1878 int CDECL
_rmtmp(void)
1880 int num_removed
= 0, i
;
1884 for (i
= 3; i
< stream_idx
; i
++) {
1887 if (file
->_tmpfname
)
1896 TRACE(":removed (%d) temp files\n",num_removed
);
1900 /*********************************************************************
1903 * When reading \r as last character in text mode, read() positions
1904 * the file pointer on the \r character while getc() goes on to
1907 static int read_i(int fd
, void *buf
, unsigned int count
)
1910 char *bufstart
= buf
;
1911 HANDLE hand
= fdtoh(fd
);
1912 ioinfo
*fdinfo
= get_ioinfo(fd
);
1917 if (fdinfo
->wxflag
& WX_READEOF
) {
1918 fdinfo
->wxflag
|= WX_ATEOF
;
1919 TRACE("already at EOF, returning 0\n");
1922 /* Don't trace small reads, it gets *very* annoying */
1924 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1925 if (hand
== INVALID_HANDLE_VALUE
)
1928 /* Reading single bytes in O_TEXT mode makes things slow
1929 * So read big chunks
1931 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1933 if (count
!= 0 && num_read
== 0)
1935 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1936 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1938 else if (fdinfo
->wxflag
& WX_TEXT
)
1941 if (bufstart
[num_read
-1] == '\r')
1945 fdinfo
->wxflag
&= ~WX_READCR
;
1946 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1950 fdinfo
->wxflag
|= WX_READCR
;
1955 fdinfo
->wxflag
&= ~WX_READCR
;
1956 for (i
=0, j
=0; i
<num_read
; i
++)
1958 /* in text mode, a ctrl-z signals EOF */
1959 if (bufstart
[i
] == 0x1a)
1961 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1962 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1965 /* in text mode, strip \r if followed by \n.
1966 * BUG: should save state across calls somehow, so CR LF that
1967 * straddles buffer boundary gets recognized properly?
1969 if ((bufstart
[i
] != '\r')
1970 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1971 bufstart
[j
++] = bufstart
[i
];
1978 if (GetLastError() == ERROR_BROKEN_PIPE
)
1980 TRACE(":end-of-pipe\n");
1981 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1986 TRACE(":failed-last error (%d)\n",GetLastError());
1992 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1996 /*********************************************************************
1999 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2002 num_read
= read_i(fd
, buf
, count
);
2006 /*********************************************************************
2007 * _setmode (MSVCRT.@)
2009 int CDECL
_setmode(int fd
,int mode
)
2011 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2012 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
2013 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
2014 if ((mode
& _O_TEXT
) == _O_TEXT
)
2015 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2017 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2021 /*********************************************************************
2024 long CDECL
_tell(int fd
)
2026 return _lseek(fd
, 0, SEEK_CUR
);
2029 /*********************************************************************
2030 * _telli64 (MSVCRT.@)
2032 __int64 CDECL
_telli64(int fd
)
2034 return _lseeki64(fd
, 0, SEEK_CUR
);
2037 /*********************************************************************
2038 * _tempnam (MSVCRT.@)
2040 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2042 char tmpbuf
[MAX_PATH
];
2043 const char *tmp_dir
= getenv("TMP");
2045 if (tmp_dir
) dir
= tmp_dir
;
2047 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2048 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2050 TRACE("got name (%s)\n",tmpbuf
);
2051 DeleteFileA(tmpbuf
);
2052 return _strdup(tmpbuf
);
2054 TRACE("failed (%d)\n",GetLastError());
2058 /*********************************************************************
2059 * _wtempnam (MSVCRT.@)
2061 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2063 wchar_t tmpbuf
[MAX_PATH
];
2065 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2066 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2068 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2069 DeleteFileW(tmpbuf
);
2070 return _wcsdup(tmpbuf
);
2072 TRACE("failed (%d)\n",GetLastError());
2076 /*********************************************************************
2079 int CDECL
_umask(int umask
)
2081 int old_umask
= umask
;
2082 TRACE("(%d)\n",umask
);
2083 MSVCRT_umask
= umask
;
2087 /*********************************************************************
2090 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2093 HANDLE hand
= fdtoh(fd
);
2095 /* Don't trace small writes, it gets *very* annoying */
2098 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2100 if (hand
== INVALID_HANDLE_VALUE
)
2106 /* If appending, go to EOF */
2107 if (get_ioinfo(fd
)->wxflag
& WX_APPEND
)
2108 _lseek(fd
, 0, FILE_END
);
2110 if (!(get_ioinfo(fd
)->wxflag
& WX_TEXT
))
2112 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2113 && (num_written
== count
))
2115 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2116 hand
, GetLastError());
2121 unsigned int i
, j
, nr_lf
;
2124 const char *s
= buf
, *buf_start
= buf
;
2125 /* find number of \n ( without preceding \r ) */
2126 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2131 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2136 if ((q
= p
= malloc(count
+ nr_lf
)))
2138 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2143 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2150 FIXME("Malloc failed\n");
2158 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2160 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2161 fd
, hand
, GetLastError(), num_written
);
2165 return s
- buf_start
;
2177 /*********************************************************************
2180 int CDECL
_putw(int val
, FILE* file
)
2185 len
= _write(file
->_file
, &val
, sizeof(val
));
2186 if (len
== sizeof(val
)) {
2191 file
->_flag
|= _IOERR
;
2196 /*********************************************************************
2199 int CDECL
fclose(FILE* file
)
2205 free(file
->_tmpfname
);
2206 file
->_tmpfname
= NULL
;
2207 /* flush stdio buffers */
2208 if(file
->_flag
& _IOWRT
)
2210 if(file
->_flag
& _IOMYBUF
)
2213 r
=_close(file
->_file
);
2217 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2218 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2220 if(file
== get_file(stream_idx
-1)) {
2221 while(stream_idx
>3 && !file
->_flag
) {
2223 file
= get_file(stream_idx
-1);
2227 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2230 /*********************************************************************
2233 int CDECL
feof(FILE* file
)
2238 ret
= file
->_flag
& _IOEOF
;
2244 /*********************************************************************
2247 int CDECL
ferror(FILE* file
)
2252 ret
= file
->_flag
& _IOERR
;
2258 /*********************************************************************
2259 * _filbuf (MSVCRT.@)
2261 int CDECL
_filbuf(FILE* file
)
2266 if(file
->_flag
& _IOSTRG
) {
2271 /* Allocate buffer if needed */
2272 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2275 if(!(file
->_flag
& _IOREAD
)) {
2276 if(file
->_flag
& _IORW
)
2277 file
->_flag
|= _IOREAD
;
2284 if(file
->_flag
& _IONBF
) {
2286 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2287 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2295 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2297 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2304 file
->_ptr
= file
->_base
+1;
2305 c
= *(unsigned char *)file
->_base
;
2311 /*********************************************************************
2314 int CDECL
fgetc(FILE* file
)
2322 i
= (unsigned char *)file
->_ptr
++;
2331 /*********************************************************************
2332 * _fgetchar (MSVCRT.@)
2334 int CDECL
_fgetchar(void)
2336 return fgetc(stdin
);
2339 /*********************************************************************
2342 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2345 char * buf_start
= s
;
2347 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2348 file
,file
->_file
,s
,size
);
2352 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2357 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2359 TRACE(":nothing read\n");
2363 if ((cc
!= EOF
) && (size
> 1))
2366 TRACE(":got %s\n", debugstr_a(buf_start
));
2371 /*********************************************************************
2374 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2375 * the CR from CR/LF combinations
2377 wint_t CDECL
fgetwc(FILE* file
)
2382 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2389 for(i
=0; i
<sizeof(wc
); i
++)
2402 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2417 if ((__mb_cur_max
> 1) && isleadbyte(c
))
2419 FIXME("Treat Multibyte characters\n");
2429 /*********************************************************************
2432 int CDECL
_getw(FILE* file
)
2440 for (j
=0; j
<sizeof(int); j
++) {
2443 file
->_flag
|= _IOEOF
;
2454 /*********************************************************************
2457 wint_t CDECL
getwc(FILE* file
)
2459 return fgetwc(file
);
2462 /*********************************************************************
2463 * _fgetwchar (MSVCRT.@)
2465 wint_t CDECL
_fgetwchar(void)
2467 return fgetwc(stdin
);
2470 /*********************************************************************
2471 * getwchar (MSVCRT.@)
2473 wint_t CDECL
getwchar(void)
2475 return _fgetwchar();
2478 /*********************************************************************
2481 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2484 wchar_t * buf_start
= s
;
2486 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2487 file
,file
->_file
,s
,size
);
2491 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2496 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2498 TRACE(":nothing read\n");
2502 if ((cc
!= WEOF
) && (size
> 1))
2505 TRACE(":got %s\n", debugstr_w(buf_start
));
2510 /*********************************************************************
2513 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2515 size_t wrcnt
=size
* nmemb
;
2522 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2523 memcpy(file
->_ptr
, ptr
, pcnt
);
2528 ptr
= (const char*)ptr
+ pcnt
;
2529 } else if(!(file
->_flag
& _IOWRT
)) {
2530 if(file
->_flag
& _IORW
) {
2531 file
->_flag
|= _IOWRT
;
2539 int res
=flush_buffer(file
);
2541 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2544 file
->_flag
|= _IOERR
;
2547 written
+= pwritten
;
2552 return written
/ size
;
2555 /*********************************************************************
2557 * FORKED for ReactOS, don't sync with Wine!
2559 * - http://jira.reactos.org/browse/CORE-6495
2560 * - http://bugs.winehq.org/show_bug.cgi?id=8598
2562 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2564 /* If this is a real file stream (and not some temporary one for
2565 sprintf-like functions), check whether it is opened in text mode.
2566 In this case, we have to perform an implicit conversion to ANSI. */
2567 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
2569 /* Convert to multibyte in text mode */
2570 char mbc
[MB_LEN_MAX
];
2573 mb_return
= wctomb(mbc
, c
);
2578 /* Output all characters */
2579 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2584 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2591 /*********************************************************************
2592 * _fputwchar (MSVCRT.@)
2594 wint_t CDECL
_fputwchar(wint_t wc
)
2596 return fputwc(wc
, stdout
);
2599 /*********************************************************************
2600 * _wfsopen (MSVCRT.@)
2602 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2605 int open_flags
, stream_flags
, fd
;
2607 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2609 /* map mode string to open() flags. "man fopen" for possibilities. */
2610 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2614 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2617 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2619 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2626 TRACE(":got (%p)\n",file
);
2627 if (fd
>= 0 && !file
)
2633 /*********************************************************************
2634 * _fsopen (MSVCRT.@)
2636 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2639 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2641 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2642 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2646 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2649 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2654 ret
= _wfsopen(pathW
, modeW
, share
);
2661 /*********************************************************************
2664 FILE * CDECL
fopen(const char *path
, const char *mode
)
2666 return _fsopen( path
, mode
, _SH_DENYNO
);
2669 /*********************************************************************
2670 * fopen_s (MSVCRT.@)
2672 int CDECL
fopen_s(FILE** pFile
,
2673 const char *filename
, const char *mode
)
2675 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2676 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2681 *pFile
= fopen(filename
, mode
);
2688 /*********************************************************************
2689 * _wfopen (MSVCRT.@)
2691 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2693 return _wfsopen( path
, mode
, _SH_DENYNO
);
2696 /*********************************************************************
2697 * _wfopen_s (MSVCRT.@)
2699 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
2700 const wchar_t *mode
)
2702 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2703 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2708 *pFile
= _wfopen(filename
, mode
);
2715 /* fputc calls _flsbuf which calls fputc */
2716 int CDECL
_flsbuf(int c
, FILE* file
);
2718 /*********************************************************************
2721 int CDECL
fputc(int c
, FILE* file
)
2731 res
= flush_buffer(file
);
2733 return res
? res
: c
;
2740 res
= _flsbuf(c
, file
);
2746 /*********************************************************************
2747 * _fputchar (MSVCRT.@)
2749 int CDECL
_fputchar(int c
)
2751 return fputc(c
, stdout
);
2754 /*********************************************************************
2757 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2759 size_t rcnt
=size
* nmemb
;
2768 /* first buffered data */
2770 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
2771 memcpy(ptr
, file
->_ptr
, pcnt
);
2776 ptr
= (char*)ptr
+ pcnt
;
2777 } else if(!(file
->_flag
& _IOREAD
)) {
2778 if(file
->_flag
& _IORW
) {
2779 file
->_flag
|= _IOREAD
;
2788 /* Fill the buffer on small reads.
2789 * TODO: Use a better buffering strategy.
2791 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2792 if (file
->_bufsiz
== 0) {
2795 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2796 file
->_ptr
= file
->_base
;
2797 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2798 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2799 if (i
> 0 && i
< file
->_cnt
) {
2800 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
2801 file
->_flag
&= ~_IOEOF
;
2804 memcpy(ptr
, file
->_ptr
, i
);
2809 i
= _read(file
->_file
,ptr
, rcnt
);
2813 ptr
= (char *)ptr
+i
;
2814 /* expose feof condition in the flags
2815 * MFC tests file->_flag for feof, and doesn't call feof())
2817 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
2818 file
->_flag
|= _IOEOF
;
2821 file
->_flag
|= _IOERR
;
2832 /*********************************************************************
2833 * _wfreopen (MSVCRT.@)
2836 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
2838 int open_flags
, stream_flags
, fd
;
2840 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
2843 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2848 /* map mode string to open() flags. "man fopen" for possibilities. */
2849 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2853 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2856 else if (init_fp(file
, fd
, stream_flags
) == -1)
2859 WARN(":failed-last error (%d)\n",GetLastError());
2860 _dosmaperr(GetLastError());
2869 /*********************************************************************
2870 * freopen (MSVCRT.@)
2873 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
2876 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2878 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2879 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2885 ret
= _wfreopen(pathW
, modeW
, file
);
2892 /*********************************************************************
2893 * fsetpos (MSVCRT.@)
2895 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2900 /* Note that all this has been lifted 'as is' from fseek */
2901 if(file
->_flag
& _IOWRT
)
2904 /* Discard buffered input */
2906 file
->_ptr
= file
->_base
;
2908 /* Reset direction of i/o */
2909 if(file
->_flag
& _IORW
) {
2910 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2913 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2918 /*********************************************************************
2919 * _ftelli64 (MSVCRT.@)
2921 __int64 CDECL
_ftelli64(FILE* file
)
2926 pos
= _telli64(file
->_file
);
2932 if(file
->_flag
& _IOWRT
) {
2933 pos
+= file
->_ptr
- file
->_base
;
2935 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2938 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
2942 } else if(!file
->_cnt
) { /* nothing to do */
2943 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
2947 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2948 for(i
=0; i
<file
->_cnt
; i
++)
2949 if(file
->_ptr
[i
] == '\n')
2955 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
2960 pos
-= file
->_bufsiz
;
2961 pos
+= file
->_ptr
- file
->_base
;
2963 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2964 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
2967 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
2978 /*********************************************************************
2981 LONG CDECL
ftell(FILE* file
)
2983 return (LONG
)_ftelli64(file
);
2986 /*********************************************************************
2987 * fgetpos (MSVCRT.@)
2989 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2994 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
3000 if( file
->_flag
& _IOWRT
) {
3001 off
= file
->_ptr
- file
->_base
;
3004 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3005 /* Black magic correction for CR removal */
3007 for (i
=0; i
<file
->_cnt
; i
++) {
3008 if (file
->_ptr
[i
] == '\n')
3011 /* Black magic when reading CR at buffer boundary*/
3012 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3022 /*********************************************************************
3025 int CDECL
fputs(const char *s
, FILE* file
)
3027 size_t i
, len
= strlen(s
);
3031 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3032 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3036 for (i
=0; i
<len
; i
++)
3037 if (fputc(s
[i
], file
) == EOF
) {
3046 /*********************************************************************
3049 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3051 size_t i
, len
= strlenW(s
);
3055 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3056 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3060 for (i
=0; i
<len
; i
++) {
3061 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
3062 || fputwc(s
[i
], file
) == WEOF
) {
3072 /*********************************************************************
3073 * getchar (MSVCRT.@)
3075 int CDECL
getchar(void)
3077 return fgetc(stdin
);
3080 /*********************************************************************
3083 int CDECL
getc(FILE* file
)
3088 /*********************************************************************
3091 char * CDECL
gets(char *buf
)
3094 char * buf_start
= buf
;
3097 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3099 if(cc
!= '\r') *buf
++ = (char)cc
;
3103 TRACE("got '%s'\n", buf_start
);
3104 _unlock_file(stdin
);
3108 /*********************************************************************
3111 wchar_t* CDECL
_getws(wchar_t* buf
)
3117 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3121 *buf
++ = (wchar_t)cc
;
3125 TRACE("got %s\n", debugstr_w(ws
));
3126 _unlock_file(stdin
);
3130 /*********************************************************************
3133 int CDECL
putc(int c
, FILE* file
)
3135 return fputc(c
, file
);
3138 /*********************************************************************
3139 * putchar (MSVCRT.@)
3141 int CDECL
putchar(int c
)
3143 return fputc(c
, stdout
);
3146 /*********************************************************************
3147 * _putwch (MSVCRT.@)
3149 wint_t CDECL
_putwch(wchar_t c
)
3151 return fputwc(c
, stdout
);
3154 /*********************************************************************
3157 int CDECL
puts(const char *s
)
3159 size_t len
= strlen(s
);
3163 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3164 _unlock_file(stdout
);
3168 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3169 _unlock_file(stdout
);
3173 /*********************************************************************
3176 int CDECL
_putws(const wchar_t *s
)
3178 static const wchar_t nl
= '\n';
3179 size_t len
= strlenW(s
);
3183 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3184 _unlock_file(stdout
);
3188 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3189 _unlock_file(stdout
);
3193 /*********************************************************************
3196 int CDECL
remove(const char *path
)
3198 TRACE("(%s)\n",path
);
3199 if (DeleteFileA(path
))
3201 TRACE(":failed (%d)\n",GetLastError());
3202 _dosmaperr(GetLastError());
3206 /*********************************************************************
3207 * _wremove (MSVCRT.@)
3209 int CDECL
_wremove(const wchar_t *path
)
3211 TRACE("(%s)\n",debugstr_w(path
));
3212 if (DeleteFileW(path
))
3214 TRACE(":failed (%d)\n",GetLastError());
3215 _dosmaperr(GetLastError());
3219 /*********************************************************************
3222 int CDECL
rename(const char *oldpath
,const char *newpath
)
3224 TRACE(":from %s to %s\n",oldpath
,newpath
);
3225 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3227 TRACE(":failed (%d)\n",GetLastError());
3228 _dosmaperr(GetLastError());
3232 /*********************************************************************
3233 * _wrename (MSVCRT.@)
3235 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3237 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3238 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3240 TRACE(":failed (%d)\n",GetLastError());
3241 _dosmaperr(GetLastError());
3245 /*********************************************************************
3246 * setvbuf (MSVCRT.@)
3248 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3256 if(mode
== _IOFBF
) {
3257 file
->_flag
&= ~_IONBF
;
3258 file
->_base
= file
->_ptr
= buf
;
3260 file
->_bufsiz
= size
;
3263 file
->_flag
|= _IONBF
;
3269 /*********************************************************************
3272 void CDECL
setbuf(FILE* file
, char *buf
)
3274 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3277 /*********************************************************************
3280 char * CDECL
tmpnam(char *s
)
3287 thread_data_t
*data
= msvcrt_get_thread_data();
3289 if(!data
->tmpnam_buffer
)
3290 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3292 s
= data
->tmpnam_buffer
;
3295 int_to_base32(GetCurrentProcessId(), tmpstr
);
3296 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3297 for (count
= 0; count
< TMP_MAX
; count
++)
3299 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3300 memcpy(p
, tmpstr
, size
);
3301 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3302 GetLastError() == ERROR_FILE_NOT_FOUND
)
3308 /*********************************************************************
3309 * _wtmpnam (MSVCRT.@)
3311 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3313 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3318 thread_data_t
*data
= msvcrt_get_thread_data();
3320 if(!data
->wtmpnam_buffer
)
3321 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3323 s
= data
->wtmpnam_buffer
;
3326 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3327 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3328 for (count
= 0; count
< TMP_MAX
; count
++)
3330 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3331 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3332 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3333 GetLastError() == ERROR_FILE_NOT_FOUND
)
3339 /*********************************************************************
3340 * tmpfile (MSVCRT.@)
3342 FILE* CDECL
tmpfile(void)
3344 char *filename
= tmpnam(NULL
);
3349 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
3350 if (fd
!= -1 && (file
= alloc_fp()))
3352 if (init_fp(file
, fd
, _O_RDWR
) == -1)
3357 else file
->_tmpfname
= _strdup(filename
);
3363 /*********************************************************************
3366 int CDECL
ungetc(int c
, FILE * file
)
3372 if(file
->_bufsiz
== 0) {
3376 if(file
->_ptr
>file
->_base
) {
3389 /*********************************************************************
3390 * ungetwc (MSVCRT.@)
3392 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3395 char * pp
= (char *)&mwc
;
3399 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3400 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3412 /*********************************************************************
3413 * _getmaxstdio (MSVCRT.@)
3415 int CDECL
_getmaxstdio(void)
3420 /*********************************************************************
3421 * _setmaxstdio (MSVCRT.@)
3423 int CDECL
_setmaxstdio(int newmax
)
3425 TRACE("%d\n", newmax
);
3427 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3430 max_streams
= newmax
;