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 */
88 #define WX_READCR 0x08 /* underlying file is at \r */
89 #define WX_DONTINHERIT 0x10
90 #define WX_APPEND 0x20
93 /* FIXME: this should be allocated dynamically */
94 #define MAX_FILES 2048
95 #define FD_BLOCK_SIZE 64
102 CRITICAL_SECTION crit
;
105 /*********************************************************************
106 * __pioinfo (MSVCRT.@)
107 * array of pointers to ioinfo arrays [64]
109 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
111 /*********************************************************************
112 * __badioinfo (MSVCRT.@)
114 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
116 static int fdstart
= 3; /* first unallocated fd */
117 static int fdend
= 3; /* highest allocated fd */
121 CRITICAL_SECTION crit
;
124 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
125 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
126 static int max_streams
= 512, stream_idx
;
128 /* INTERNAL: process umask */
129 static int MSVCRT_umask
= 0;
131 /* INTERNAL: static data for tmpnam and _wtmpname functions */
132 static int tmpnam_unique
;
134 /* This critical section protects the tables __pioinfo and fstreams,
135 * and their related indexes, fdstart, fdend,
136 * and stream_idx, from race conditions.
137 * It doesn't protect against race conditions manipulating the underlying files
138 * or flags; doing so would probably be better accomplished with per-file
139 * protection, rather than locking the whole table for every change.
141 static CRITICAL_SECTION file_cs
;
142 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
143 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
145 static inline ioinfo
* get_ioinfo(int fd
)
149 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
153 return ret
+ (fd
%FD_BLOCK_SIZE
);
156 static inline FILE* get_file(int i
)
166 ret
= fstream
[i
/FD_BLOCK_SIZE
];
168 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
169 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
170 ERR("out of memory\n");
175 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
177 ret
+= i
%FD_BLOCK_SIZE
;
182 static inline BOOL
is_valid_fd(int fd
)
184 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
187 /* INTERNAL: Get the HANDLE for a fd
188 * This doesn't lock the table, because a failure will result in
189 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
190 * it returns a valid handle which is about to be closed, a subsequent call
191 * will fail, most likely in a sane way.
193 /*static*/ HANDLE
fdtoh(int fd
)
195 if (!is_valid_fd(fd
))
197 WARN(":fd (%d) - no handle!\n",fd
);
200 return INVALID_HANDLE_VALUE
;
202 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
203 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
204 return get_ioinfo(fd
)->handle
;
207 /* INTERNAL: free a file entry fd */
208 static void free_fd(int fd
)
214 fdinfo
= get_ioinfo(fd
);
215 old_handle
= fdinfo
->handle
;
216 if(fdinfo
!= &__badioinfo
)
218 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
221 TRACE(":fd (%d) freed\n",fd
);
222 if (fd
< 3) /* don't use 0,1,2 for user files */
227 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
230 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
233 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
247 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
248 /* caller must hold the files lock */
249 static int alloc_fd_from(HANDLE hand
, int flag
, int fd
)
255 WARN(":files exhausted!\n");
260 fdinfo
= get_ioinfo(fd
);
261 if(fdinfo
== &__badioinfo
) {
264 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
265 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
266 WARN(":out of memory!\n");
271 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
272 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
274 fdinfo
= get_ioinfo(fd
);
277 fdinfo
->handle
= hand
;
278 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
280 /* locate next free slot */
281 if (fd
== fdstart
&& fd
== fdend
)
284 while (fdstart
< fdend
&&
285 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
287 /* update last fd in use */
290 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
294 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
295 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
296 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
302 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
303 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
308 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
309 ret
= alloc_fd_from(hand
, flag
, fdstart
);
314 /* INTERNAL: Allocate a FILE* for an fd slot */
315 /* caller must hold the files lock */
316 static FILE* alloc_fp(void)
321 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
327 if (file
->_flag
== 0)
329 if (i
== stream_idx
) stream_idx
++;
337 /* INTERNAL: initialize a FILE* from an open fd */
338 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
340 TRACE(":fd (%d) allocating FILE*\n",fd
);
341 if (!is_valid_fd(fd
))
343 WARN(":invalid fd %d\n",fd
);
348 memset(file
, 0, sizeof(*file
));
350 file
->_flag
= stream_flags
;
352 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
353 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
355 TRACE(":got FILE* (%p)\n",file
);
359 /* INTERNAL: Create an inheritance data block (for spawned process)
360 * The inheritance block is made of:
361 * 00 int nb of file descriptor (NBFD)
362 * 04 char file flags (wxflag): repeated for each fd
363 * 4+NBFD HANDLE file handle: repeated for each fd
365 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
372 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
373 *block
= calloc(*size
, 1);
379 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
380 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
382 *(unsigned*)*block
= fdend
;
383 for (fd
= 0; fd
< fdend
; fd
++)
385 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
386 fdinfo
= get_ioinfo(fd
);
387 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
389 *wxflag_ptr
= fdinfo
->wxflag
;
390 *handle_ptr
= fdinfo
->handle
;
395 *handle_ptr
= INVALID_HANDLE_VALUE
;
397 wxflag_ptr
++; handle_ptr
++;
402 /* INTERNAL: Set up all file descriptors,
403 * as well as default streams (stdin, stderr and stdout)
405 void msvcrt_init_io(void)
411 InitializeCriticalSection(&file_cs
);
412 file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_cs");
413 GetStartupInfoA(&si
);
414 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
420 count
= *(unsigned*)si
.lpReserved2
;
421 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
422 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
424 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
425 count
= min(count
, MAX_FILES
);
426 for (i
= 0; i
< count
; i
++)
428 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
429 alloc_fd_from(*handle_ptr
, *wxflag_ptr
, i
);
431 wxflag_ptr
++; handle_ptr
++;
433 fdend
= max( 3, count
);
434 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
435 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
439 alloc_fd_from(INVALID_HANDLE_VALUE
, 0, 3);
441 fdinfo
= get_ioinfo(0);
442 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
444 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
446 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
447 GetCurrentProcess(), &fdinfo
->handle
,
448 0, TRUE
, DUPLICATE_SAME_ACCESS
))
450 fdinfo
->handle
= std
;
452 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
455 fdinfo
= get_ioinfo(1);
456 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
458 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
460 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
461 GetCurrentProcess(), &fdinfo
->handle
,
462 0, TRUE
, DUPLICATE_SAME_ACCESS
))
464 fdinfo
->handle
= std
;
466 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
469 fdinfo
= get_ioinfo(2);
470 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
472 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
474 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
475 GetCurrentProcess(), &fdinfo
->handle
,
476 0, TRUE
, DUPLICATE_SAME_ACCESS
))
478 fdinfo
->handle
= std
;
480 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
483 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(0)->handle
,
484 get_ioinfo(1)->handle
, get_ioinfo(2)->handle
);
486 memset(_iob
,0,3*sizeof(FILE));
487 for (i
= 0; i
< 3; i
++)
489 /* FILE structs for stdin/out/err are static and never deleted */
491 _iob
[i
]._tmpfname
= NULL
;
492 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
497 /* INTERNAL: Flush stdio file buffer */
498 static int flush_buffer(FILE* file
)
501 int cnt
=file
->_ptr
-file
->_base
;
502 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
503 file
->_flag
|= _IOERR
;
506 file
->_ptr
=file
->_base
;
507 file
->_cnt
=file
->_bufsiz
;
512 /* INTERNAL: Allocate stdio file buffer */
513 /*static*/ void alloc_buffer(FILE* file
)
515 file
->_base
= calloc(BUFSIZ
,1);
517 file
->_bufsiz
= BUFSIZ
;
518 file
->_flag
|= _IOMYBUF
;
520 file
->_base
= (char*)(&file
->_charbuf
);
522 file
->_bufsiz
= sizeof(file
->_charbuf
);
524 file
->_ptr
= file
->_base
;
528 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
529 static int int_to_base32(int num
, char *str
)
544 *p
= (num
& 31) + '0';
546 *p
+= ('a' - '0' - 10);
553 /* INTERNAL: wide character version of int_to_base32 */
554 static int int_to_base32_w(int num
, wchar_t *str
)
569 *p
= (num
& 31) + '0';
571 *p
+= ('a' - '0' - 10);
578 /* INTERNAL: Create a wide string from an ascii string */
579 wchar_t *msvcrt_wstrdupa(const char *str
)
581 const unsigned int len
= strlen(str
) + 1 ;
582 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
585 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
589 /*********************************************************************
590 * __iob_func(MSVCRT.@)
592 FILE * CDECL
__iob_func(void)
597 /*********************************************************************
600 int CDECL
_access(const char *filename
, int mode
)
602 DWORD attr
= GetFileAttributesA(filename
);
604 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
606 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
608 _dosmaperr(GetLastError());
611 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
613 _set_errno(ERROR_ACCESS_DENIED
);
619 /*********************************************************************
620 * _access_s (MSVCRT.@)
622 int CDECL
_access_s(const char *filename
, int mode
)
624 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
625 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
631 return _access(filename
, mode
);
634 /*********************************************************************
635 * _waccess (MSVCRT.@)
637 int CDECL
_waccess(const wchar_t *filename
, int mode
)
639 DWORD attr
= GetFileAttributesW(filename
);
641 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
643 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
645 _dosmaperr(GetLastError());
648 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
650 _set_errno(ERROR_ACCESS_DENIED
);
656 /*********************************************************************
657 * _waccess_s (MSVCRT.@)
659 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
661 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
662 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
668 return _waccess(filename
, mode
);
671 /*********************************************************************
674 int CDECL
_chmod(const char *path
, int flags
)
676 DWORD oldFlags
= GetFileAttributesA(path
);
678 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
680 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
681 oldFlags
| FILE_ATTRIBUTE_READONLY
;
683 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
686 _dosmaperr(GetLastError());
690 /*********************************************************************
693 int CDECL
_wchmod(const wchar_t *path
, int flags
)
695 DWORD oldFlags
= GetFileAttributesW(path
);
697 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
699 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
700 oldFlags
| FILE_ATTRIBUTE_READONLY
;
702 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
705 _dosmaperr(GetLastError());
709 /*********************************************************************
712 int CDECL
_unlink(const char *path
)
714 TRACE("%s\n",debugstr_a(path
));
715 if(DeleteFileA(path
))
717 TRACE("failed (%d)\n",GetLastError());
718 _dosmaperr(GetLastError());
722 /*********************************************************************
723 * _wunlink (MSVCRT.@)
725 int CDECL
_wunlink(const wchar_t *path
)
727 TRACE("(%s)\n",debugstr_w(path
));
728 if(DeleteFileW(path
))
730 TRACE("failed (%d)\n",GetLastError());
731 _dosmaperr(GetLastError());
735 /* _flushall calls fflush which calls _flushall */
736 int CDECL
fflush(FILE* file
);
738 /* INTERNAL: Flush all stream buffer */
739 static int flush_all_buffers(int mask
)
741 int i
, num_flushed
= 0;
745 for (i
= 3; i
< stream_idx
; i
++) {
750 if(file
->_flag
& mask
) {
758 TRACE(":flushed (%d) handles\n",num_flushed
);
762 /*********************************************************************
763 * _flushall (MSVCRT.@)
765 int CDECL
_flushall(void)
767 return flush_all_buffers(_IOWRT
| _IOREAD
);
770 /*********************************************************************
773 int CDECL
fflush(FILE* file
)
776 flush_all_buffers(_IOWRT
);
777 } else if(file
->_flag
& _IOWRT
) {
781 res
= flush_buffer(file
);
783 if(!res && (file->_flag & _IOCOMMIT))
784 res = _commit(file->_file) ? EOF : 0;
789 } else if(file
->_flag
& _IOREAD
) {
792 file
->_ptr
= file
->_base
;
800 /*********************************************************************
803 int CDECL
_close(int fd
)
810 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
811 if (hand
== INVALID_HANDLE_VALUE
)
813 else if (!CloseHandle(hand
))
815 WARN(":failed-last error (%d)\n",GetLastError());
816 _dosmaperr(GetLastError());
829 /*********************************************************************
832 int CDECL
_commit(int fd
)
834 HANDLE hand
= fdtoh(fd
);
836 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
837 if (hand
== INVALID_HANDLE_VALUE
)
840 if (!FlushFileBuffers(hand
))
842 if (GetLastError() == ERROR_INVALID_HANDLE
)
844 /* FlushFileBuffers fails for console handles
845 * so we ignore this error.
849 TRACE(":failed-last error (%d)\n",GetLastError());
850 _dosmaperr(GetLastError());
857 /*********************************************************************
860 * MSDN isn't clear on this point, but the remarks for _pipe
861 * indicate file descriptors duplicated with _dup and _dup2 are always
864 int CDECL
_dup2(int od
, int nd
)
868 TRACE("(od=%d, nd=%d)\n", od
, nd
);
870 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
874 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
875 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
877 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
881 ret
= alloc_fd_from(handle
, wxflag
, nd
);
889 /* _dup2 returns 0, not nd, on success */
896 _dosmaperr(GetLastError());
908 /*********************************************************************
911 int CDECL
_dup(int od
)
917 if (_dup2(od
, fd
) == 0)
925 /*********************************************************************
928 int CDECL
_eof(int fd
)
931 LONG hcurpos
,hendpos
;
932 HANDLE hand
= fdtoh(fd
);
934 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
936 if (hand
== INVALID_HANDLE_VALUE
)
939 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
941 /* Otherwise we do it the hard way */
942 hcurpos
= hendpos
= 0;
943 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
944 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
946 if (curpos
== endpos
&& hcurpos
== hendpos
)
948 /* FIXME: shouldn't WX_ATEOF be set here? */
952 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
956 /*********************************************************************
957 * _fcloseall (MSVCRT.@)
959 int CDECL
_fcloseall(void)
961 int num_closed
= 0, i
;
965 for (i
= 3; i
< stream_idx
; i
++) {
968 if (file
->_flag
&& !fclose(file
))
973 TRACE(":closed (%d) handles\n",num_closed
);
977 /* free everything on process exit */
978 void msvcrt_free_io(void)
983 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
984 * stdout, and stderr (unlike GNU), so we need to fclose() them here
985 * or they won't get flushed.
991 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
994 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
997 file_cs
.DebugInfo
->Spare
[0] = 0;
998 DeleteCriticalSection(&file_cs
);
1001 /*********************************************************************
1002 * _lseeki64 (MSVCRT.@)
1004 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1006 HANDLE hand
= fdtoh(fd
);
1009 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1010 if (hand
== INVALID_HANDLE_VALUE
)
1013 if (whence
< 0 || whence
> 2)
1019 TRACE(":fd (%d) to %s pos %s\n",
1020 fd
,wine_dbgstr_longlong(offset
),
1021 (whence
==SEEK_SET
)?"SEEK_SET":
1022 (whence
==SEEK_CUR
)?"SEEK_CUR":
1023 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1025 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1026 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1027 ofs
.QuadPart
= offset
;
1028 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1029 GetLastError() == ERROR_SUCCESS
)
1031 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1032 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1034 return ofs
.QuadPart
;
1036 TRACE(":error-last error (%d)\n",GetLastError());
1037 _dosmaperr(GetLastError());
1041 /*********************************************************************
1044 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1046 return (LONG
)_lseeki64(fd
, offset
, whence
);
1049 /*********************************************************************
1050 * _lock_file (MSVCRT.@)
1052 void CDECL
_lock_file(FILE *file
)
1054 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1055 _lock(_STREAM_LOCKS
+(file
-_iob
));
1056 /* ReactOS: string streams dont need to be locked */
1057 else if(!(file
->_flag
& _IOSTRG
))
1058 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1061 /*********************************************************************
1062 * _unlock_file (MSVCRT.@)
1064 void CDECL
_unlock_file(FILE *file
)
1066 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1067 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1068 /* ReactOS: string streams dont need to be locked */
1069 else if(!(file
->_flag
& _IOSTRG
))
1070 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1074 /*********************************************************************
1075 * _locking (MSVCRT.@)
1077 * This is untested; the underlying LockFile doesn't work yet.
1079 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1083 HANDLE hand
= fdtoh(fd
);
1085 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1086 if (hand
== INVALID_HANDLE_VALUE
)
1089 if (mode
< 0 || mode
> 4)
1095 TRACE(":fd (%d) by 0x%08x mode %s\n",
1096 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1097 (mode
==_LK_LOCK
)?"_LK_LOCK":
1098 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1099 (mode
==_LK_RLCK
)?"_LK_RLCK":
1100 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1103 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1105 FIXME ("Seek failed\n");
1106 *_errno() = EINVAL
; /* FIXME */
1109 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1112 ret
= 1; /* just to satisfy gcc */
1115 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1120 else if (mode
== _LK_UNLCK
)
1121 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1123 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1124 /* FIXME - what about error settings? */
1125 return ret
? 0 : -1;
1128 /*********************************************************************
1129 * _fseeki64 (MSVCRT.@)
1131 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1136 /* Flush output if needed */
1137 if(file
->_flag
& _IOWRT
)
1140 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1141 offset
-= file
->_cnt
;
1142 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1143 /* Black magic correction for CR removal */
1145 for (i
=0; i
<file
->_cnt
; i
++) {
1146 if (file
->_ptr
[i
] == '\n')
1149 /* Black magic when reading CR at buffer boundary*/
1150 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1154 /* Discard buffered input */
1156 file
->_ptr
= file
->_base
;
1157 /* Reset direction of i/o */
1158 if(file
->_flag
& _IORW
) {
1159 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1161 /* Clear end of file flag */
1162 file
->_flag
&= ~_IOEOF
;
1163 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1169 /*********************************************************************
1172 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1174 return _fseeki64( file
, offset
, whence
);
1177 /*********************************************************************
1178 * _chsize (MSVCRT.@)
1180 int CDECL
_chsize(int fd
, long size
)
1186 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1191 if (handle
!= INVALID_HANDLE_VALUE
)
1193 /* save the current file pointer */
1194 cur
= _lseek(fd
, 0, SEEK_CUR
);
1197 pos
= _lseek(fd
, size
, SEEK_SET
);
1200 ret
= SetEndOfFile(handle
);
1201 if (!ret
) _dosmaperr(GetLastError());
1204 /* restore the file pointer */
1205 _lseek(fd
, cur
, SEEK_SET
);
1210 return ret
? 0 : -1;
1213 /*********************************************************************
1214 * clearerr (MSVCRT.@)
1216 void CDECL
clearerr(FILE* file
)
1218 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1221 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1225 /*********************************************************************
1228 void CDECL
rewind(FILE* file
)
1230 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1233 fseek(file
, 0L, SEEK_SET
);
1238 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1240 int plus
= strchrW(mode
, '+') != NULL
;
1245 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1246 *stream_flags
= plus
? _IORW
: _IOREAD
;
1249 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1250 *stream_flags
= plus
? _IORW
: _IOWRT
;
1253 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1254 *stream_flags
= plus
? _IORW
: _IOWRT
;
1257 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1266 *open_flags
|= _O_BINARY
;
1267 *open_flags
&= ~_O_TEXT
;
1270 *open_flags
|= _O_TEXT
;
1271 *open_flags
&= ~_O_BINARY
;
1277 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1282 /*********************************************************************
1283 * _fdopen (MSVCRT.@)
1285 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1288 wchar_t *modeW
= NULL
;
1290 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1292 ret
= _wfdopen(fd
, modeW
);
1298 /*********************************************************************
1299 * _wfdopen (MSVCRT.@)
1301 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1303 int open_flags
, stream_flags
;
1306 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1309 if (!(file
= alloc_fp()))
1311 else if (init_fp(file
, fd
, stream_flags
) == -1)
1316 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1322 /*********************************************************************
1323 * _filelength (MSVCRT.@)
1325 LONG CDECL
_filelength(int fd
)
1327 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1330 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1333 if (endPos
!= curPos
)
1334 _lseek(fd
, curPos
, SEEK_SET
);
1341 /*********************************************************************
1342 * _filelengthi64 (MSVCRT.@)
1344 __int64 CDECL
_filelengthi64(int fd
)
1346 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1349 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1352 if (endPos
!= curPos
)
1353 _lseeki64(fd
, curPos
, SEEK_SET
);
1360 /*********************************************************************
1361 * _fileno (MSVCRT.@)
1363 int CDECL
_fileno(FILE* file
)
1365 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1369 /*********************************************************************
1370 * _get_osfhandle (MSVCRT.@)
1372 intptr_t CDECL
_get_osfhandle(int fd
)
1374 HANDLE hand
= fdtoh(fd
);
1375 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1377 return (intptr_t)hand
;
1380 /*********************************************************************
1381 * _isatty (MSVCRT.@)
1383 int CDECL
_isatty(int fd
)
1385 HANDLE hand
= fdtoh(fd
);
1387 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1388 if (hand
== INVALID_HANDLE_VALUE
)
1391 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1394 /*********************************************************************
1395 * _mktemp (MSVCRT.@)
1397 char * CDECL
_mktemp(char *pattern
)
1400 char *retVal
= pattern
;
1405 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1409 id
= GetCurrentProcessId();
1413 int tempNum
= id
/ 10;
1414 *pattern
-- = id
- (tempNum
* 10) + '0';
1420 *pattern
= letter
++;
1421 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1422 GetLastError() == ERROR_FILE_NOT_FOUND
)
1424 } while(letter
<= 'z');
1428 /*********************************************************************
1429 * _wmktemp (MSVCRT.@)
1431 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1434 wchar_t *retVal
= pattern
;
1436 wchar_t letter
= 'a';
1439 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1443 id
= GetCurrentProcessId();
1447 int tempNum
= id
/ 10;
1448 *pattern
-- = id
- (tempNum
* 10) + '0';
1454 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1455 GetLastError() == ERROR_FILE_NOT_FOUND
)
1457 *pattern
= letter
++;
1458 } while(letter
!= '|');
1462 /*static*/ unsigned split_oflags(unsigned oflags
)
1465 unsigned unsupp
; /* until we support everything */
1467 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1468 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1469 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1470 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1471 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1472 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1474 if ((unsupp
= oflags
& ~(
1475 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1476 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1477 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1479 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1481 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1486 /*********************************************************************
1489 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1492 SECURITY_ATTRIBUTES sa
;
1493 HANDLE readHandle
, writeHandle
;
1501 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1502 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1503 sa
.lpSecurityDescriptor
= NULL
;
1504 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1506 unsigned int wxflags
= split_oflags(textmode
);
1510 fd
= alloc_fd(readHandle
, wxflags
);
1514 fd
= alloc_fd(writeHandle
, wxflags
);
1523 CloseHandle(writeHandle
);
1529 CloseHandle(readHandle
);
1530 CloseHandle(writeHandle
);
1536 _dosmaperr(GetLastError());
1541 /*********************************************************************
1542 * _sopen_s (MSVCRT.@)
1544 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1546 DWORD access
= 0, creation
= 0, attrib
;
1550 SECURITY_ATTRIBUTES sa
;
1552 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1553 fd
, path
, oflags
, shflags
, pmode
);
1557 MSVCRT_INVALID_PMT("null out fd pointer", EINVAL
);
1562 wxflag
= split_oflags(oflags
);
1563 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1565 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1566 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1567 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1570 if (oflags
& _O_CREAT
)
1572 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1573 FIXME(": pmode 0x%04x ignored\n", pmode
);
1575 WARN(": pmode 0x%04x ignored\n", pmode
);
1577 if (oflags
& _O_EXCL
)
1578 creation
= CREATE_NEW
;
1579 else if (oflags
& _O_TRUNC
)
1580 creation
= CREATE_ALWAYS
;
1582 creation
= OPEN_ALWAYS
;
1584 else /* no _O_CREAT */
1586 if (oflags
& _O_TRUNC
)
1587 creation
= TRUNCATE_EXISTING
;
1589 creation
= OPEN_EXISTING
;
1598 sharing
= FILE_SHARE_READ
;
1601 sharing
= FILE_SHARE_WRITE
;
1604 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1607 ERR( "Unhandled shflags 0x%x\n", shflags
);
1610 attrib
= FILE_ATTRIBUTE_NORMAL
;
1612 if (oflags
& _O_TEMPORARY
)
1614 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1616 sharing
|= FILE_SHARE_DELETE
;
1619 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1620 sa
.lpSecurityDescriptor
= NULL
;
1621 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1623 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1624 if (hand
== INVALID_HANDLE_VALUE
) {
1625 WARN(":failed-last error (%d)\n", GetLastError());
1626 _dosmaperr(GetLastError());
1630 *fd
= alloc_fd(hand
, wxflag
);
1632 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1636 /*********************************************************************
1639 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1644 if (oflags
& _O_CREAT
)
1648 va_start(ap
, shflags
);
1649 pmode
= va_arg(ap
, int);
1655 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1659 /*********************************************************************
1660 * _wsopen_s (MSVCRT.@)
1662 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1664 DWORD access
= 0, creation
= 0, attrib
;
1665 SECURITY_ATTRIBUTES sa
;
1670 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1671 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1675 MSVCRT_INVALID_PMT("null out fd pointer", EINVAL
);
1680 wxflag
= split_oflags(oflags
);
1681 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1683 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1684 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1685 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1688 if (oflags
& _O_CREAT
)
1690 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1691 FIXME(": pmode 0x%04x ignored\n", pmode
);
1693 WARN(": pmode 0x%04x ignored\n", pmode
);
1695 if (oflags
& _O_EXCL
)
1696 creation
= CREATE_NEW
;
1697 else if (oflags
& _O_TRUNC
)
1698 creation
= CREATE_ALWAYS
;
1700 creation
= OPEN_ALWAYS
;
1702 else /* no _O_CREAT */
1704 if (oflags
& _O_TRUNC
)
1705 creation
= TRUNCATE_EXISTING
;
1707 creation
= OPEN_EXISTING
;
1716 sharing
= FILE_SHARE_READ
;
1719 sharing
= FILE_SHARE_WRITE
;
1722 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1725 ERR( "Unhandled shflags 0x%x\n", shflags
);
1728 attrib
= FILE_ATTRIBUTE_NORMAL
;
1730 if (oflags
& _O_TEMPORARY
)
1732 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1734 sharing
|= FILE_SHARE_DELETE
;
1737 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1738 sa
.lpSecurityDescriptor
= NULL
;
1739 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1741 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1743 if (hand
== INVALID_HANDLE_VALUE
) {
1744 WARN(":failed-last error (%d)\n",GetLastError());
1745 _dosmaperr(GetLastError());
1749 *fd
= alloc_fd(hand
, wxflag
);
1751 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1755 /*********************************************************************
1756 * _wsopen (MSVCRT.@)
1758 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1763 if (oflags
& _O_CREAT
)
1767 va_start(ap
, shflags
);
1768 pmode
= va_arg(ap
, int);
1774 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1778 /*********************************************************************
1781 int CDECL
_open( const char *path
, int flags
, ... )
1785 if (flags
& _O_CREAT
)
1788 va_start(ap
, flags
);
1789 pmode
= va_arg(ap
, int);
1791 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1794 return _sopen( path
, flags
, _SH_DENYNO
);
1797 /*********************************************************************
1800 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1804 if (flags
& _O_CREAT
)
1807 va_start(ap
, flags
);
1808 pmode
= va_arg(ap
, int);
1810 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1813 return _wsopen( path
, flags
, _SH_DENYNO
);
1816 /*********************************************************************
1819 int CDECL
_creat(const char *path
, int flags
)
1821 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1822 return _open(path
, usedFlags
);
1825 /*********************************************************************
1826 * _wcreat (MSVCRT.@)
1828 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1830 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1831 return _wopen(path
, usedFlags
);
1834 /*********************************************************************
1835 * _open_osfhandle (MSVCRT.@)
1837 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1841 /* _O_RDONLY (0) always matches, so set the read flag
1842 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1843 * file, so set the write flag. It also only sets _O_TEXT if it wants
1844 * text - it never sets _O_BINARY.
1846 /* don't let split_oflags() decide the mode if no mode is passed */
1847 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1848 oflags
|= _O_BINARY
;
1850 fd
= alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1851 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1855 /*********************************************************************
1858 int CDECL
_rmtmp(void)
1860 int num_removed
= 0, i
;
1864 for (i
= 3; i
< stream_idx
; i
++) {
1867 if (file
->_tmpfname
)
1876 TRACE(":removed (%d) temp files\n",num_removed
);
1880 /*********************************************************************
1883 * When reading \r as last character in text mode, read() positions
1884 * the file pointer on the \r character while getc() goes on to
1887 static int read_i(int fd
, void *buf
, unsigned int count
)
1890 char *bufstart
= buf
;
1891 HANDLE hand
= fdtoh(fd
);
1892 ioinfo
*fdinfo
= get_ioinfo(fd
);
1897 if (fdinfo
->wxflag
& WX_READEOF
) {
1898 fdinfo
->wxflag
|= WX_ATEOF
;
1899 TRACE("already at EOF, returning 0\n");
1902 /* Don't trace small reads, it gets *very* annoying */
1904 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1905 if (hand
== INVALID_HANDLE_VALUE
)
1908 /* Reading single bytes in O_TEXT mode makes things slow
1909 * So read big chunks
1911 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1913 if (count
!= 0 && num_read
== 0)
1915 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1916 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1918 else if (fdinfo
->wxflag
& WX_TEXT
)
1921 if (bufstart
[num_read
-1] == '\r')
1925 fdinfo
->wxflag
&= ~WX_READCR
;
1926 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1930 fdinfo
->wxflag
|= WX_READCR
;
1935 fdinfo
->wxflag
&= ~WX_READCR
;
1936 for (i
=0, j
=0; i
<num_read
; i
++)
1938 /* in text mode, a ctrl-z signals EOF */
1939 if (bufstart
[i
] == 0x1a)
1941 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1942 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1945 /* in text mode, strip \r if followed by \n.
1946 * BUG: should save state across calls somehow, so CR LF that
1947 * straddles buffer boundary gets recognized properly?
1949 if ((bufstart
[i
] != '\r')
1950 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1951 bufstart
[j
++] = bufstart
[i
];
1958 if (GetLastError() == ERROR_BROKEN_PIPE
)
1960 TRACE(":end-of-pipe\n");
1961 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1966 TRACE(":failed-last error (%d)\n",GetLastError());
1972 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1976 /*********************************************************************
1979 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
1982 num_read
= read_i(fd
, buf
, count
);
1986 /*********************************************************************
1987 * _setmode (MSVCRT.@)
1989 int CDECL
_setmode(int fd
,int mode
)
1991 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
1992 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1993 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1994 if ((mode
& _O_TEXT
) == _O_TEXT
)
1995 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
1997 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2001 /*********************************************************************
2004 long CDECL
_tell(int fd
)
2006 return _lseek(fd
, 0, SEEK_CUR
);
2009 /*********************************************************************
2010 * _telli64 (MSVCRT.@)
2012 __int64 CDECL
_telli64(int fd
)
2014 return _lseeki64(fd
, 0, SEEK_CUR
);
2017 /*********************************************************************
2018 * _tempnam (MSVCRT.@)
2020 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2022 char tmpbuf
[MAX_PATH
];
2023 const char *tmp_dir
= getenv("TMP");
2025 if (tmp_dir
) dir
= tmp_dir
;
2027 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2028 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2030 TRACE("got name (%s)\n",tmpbuf
);
2031 DeleteFileA(tmpbuf
);
2032 return _strdup(tmpbuf
);
2034 TRACE("failed (%d)\n",GetLastError());
2038 /*********************************************************************
2039 * _wtempnam (MSVCRT.@)
2041 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2043 wchar_t tmpbuf
[MAX_PATH
];
2045 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2046 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2048 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2049 DeleteFileW(tmpbuf
);
2050 return _wcsdup(tmpbuf
);
2052 TRACE("failed (%d)\n",GetLastError());
2056 /*********************************************************************
2059 int CDECL
_umask(int umask
)
2061 int old_umask
= umask
;
2062 TRACE("(%d)\n",umask
);
2063 MSVCRT_umask
= umask
;
2067 /*********************************************************************
2070 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2073 HANDLE hand
= fdtoh(fd
);
2075 /* Don't trace small writes, it gets *very* annoying */
2078 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2080 if (hand
== INVALID_HANDLE_VALUE
)
2086 /* If appending, go to EOF */
2087 if (get_ioinfo(fd
)->wxflag
& WX_APPEND
)
2088 _lseek(fd
, 0, FILE_END
);
2090 if (!(get_ioinfo(fd
)->wxflag
& WX_TEXT
))
2092 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2093 && (num_written
== count
))
2095 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2096 hand
, GetLastError());
2101 unsigned int i
, j
, nr_lf
;
2104 const char *s
= buf
, *buf_start
= buf
;
2105 /* find number of \n ( without preceding \r ) */
2106 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2111 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2116 if ((q
= p
= malloc(count
+ nr_lf
)))
2118 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2123 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2130 FIXME("Malloc failed\n");
2138 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2140 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2141 fd
, hand
, GetLastError(), num_written
);
2145 return s
- buf_start
;
2157 /*********************************************************************
2160 int CDECL
_putw(int val
, FILE* file
)
2165 len
= _write(file
->_file
, &val
, sizeof(val
));
2166 if (len
== sizeof(val
)) {
2171 file
->_flag
|= _IOERR
;
2176 /*********************************************************************
2179 int CDECL
fclose(FILE* file
)
2185 free(file
->_tmpfname
);
2186 file
->_tmpfname
= NULL
;
2187 /* flush stdio buffers */
2188 if(file
->_flag
& _IOWRT
)
2190 if(file
->_flag
& _IOMYBUF
)
2193 r
=_close(file
->_file
);
2197 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2198 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2200 if(file
== get_file(stream_idx
-1)) {
2201 while(stream_idx
>3 && !file
->_flag
) {
2203 file
= get_file(stream_idx
-1);
2207 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2210 /*********************************************************************
2213 int CDECL
feof(FILE* file
)
2218 ret
= file
->_flag
& _IOEOF
;
2224 /*********************************************************************
2227 int CDECL
ferror(FILE* file
)
2232 ret
= file
->_flag
& _IOERR
;
2238 /*********************************************************************
2239 * _filbuf (MSVCRT.@)
2241 int CDECL
_filbuf(FILE* file
)
2246 /* Allocate buffer if needed */
2247 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2250 if(!(file
->_flag
& _IOREAD
)) {
2251 if(file
->_flag
& _IORW
)
2252 file
->_flag
|= _IOREAD
;
2259 if(file
->_flag
& _IONBF
) {
2261 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2262 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2270 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2272 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2279 file
->_ptr
= file
->_base
+1;
2280 c
= *(unsigned char *)file
->_base
;
2286 /*********************************************************************
2289 int CDECL
fgetc(FILE* file
)
2297 i
= (unsigned char *)file
->_ptr
++;
2306 /*********************************************************************
2307 * _fgetchar (MSVCRT.@)
2309 int CDECL
_fgetchar(void)
2311 return fgetc(stdin
);
2314 /*********************************************************************
2317 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2320 char * buf_start
= s
;
2322 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2323 file
,file
->_file
,s
,size
);
2327 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2332 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2334 TRACE(":nothing read\n");
2338 if ((cc
!= EOF
) && (size
> 1))
2341 TRACE(":got %s\n", debugstr_a(buf_start
));
2346 /*********************************************************************
2349 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2350 * the CR from CR/LF combinations
2352 wint_t CDECL
fgetwc(FILE* file
)
2357 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2364 for(i
=0; i
<sizeof(wc
); i
++)
2377 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2392 if ((__mb_cur_max
> 1) && isleadbyte(c
))
2394 FIXME("Treat Multibyte characters\n");
2404 /*********************************************************************
2407 int CDECL
_getw(FILE* file
)
2415 for (j
=0; j
<sizeof(int); j
++) {
2418 file
->_flag
|= _IOEOF
;
2429 /*********************************************************************
2432 wint_t CDECL
getwc(FILE* file
)
2434 return fgetwc(file
);
2437 /*********************************************************************
2438 * _fgetwchar (MSVCRT.@)
2440 wint_t CDECL
_fgetwchar(void)
2442 return fgetwc(stdin
);
2445 /*********************************************************************
2446 * getwchar (MSVCRT.@)
2448 wint_t CDECL
getwchar(void)
2450 return _fgetwchar();
2453 /*********************************************************************
2456 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2459 wchar_t * buf_start
= s
;
2461 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2462 file
,file
->_file
,s
,size
);
2466 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2471 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2473 TRACE(":nothing read\n");
2477 if ((cc
!= WEOF
) && (size
> 1))
2480 TRACE(":got %s\n", debugstr_w(buf_start
));
2485 /*********************************************************************
2488 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2490 size_t wrcnt
=size
* nmemb
;
2497 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2498 memcpy(file
->_ptr
, ptr
, pcnt
);
2503 ptr
= (const char*)ptr
+ pcnt
;
2504 } else if(!(file
->_flag
& _IOWRT
)) {
2505 if(file
->_flag
& _IORW
) {
2506 file
->_flag
|= _IOWRT
;
2514 int res
=flush_buffer(file
);
2516 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2519 file
->_flag
|= _IOERR
;
2522 written
+= pwritten
;
2527 return written
/ size
;
2530 /*********************************************************************
2532 * FORKED for ReactOS, don't sync with Wine!
2534 * - http://jira.reactos.org/browse/CORE-6495
2535 * - http://bugs.winehq.org/show_bug.cgi?id=8598
2537 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2539 /* If this is a real file stream (and not some temporary one for
2540 sprintf-like functions), check whether it is opened in text mode.
2541 In this case, we have to perform an implicit conversion to ANSI. */
2542 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
2544 /* Convert to multibyte in text mode */
2545 char mbc
[MB_LEN_MAX
];
2548 mb_return
= wctomb(mbc
, c
);
2553 /* Output all characters */
2554 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2559 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2566 /*********************************************************************
2567 * _fputwchar (MSVCRT.@)
2569 wint_t CDECL
_fputwchar(wint_t wc
)
2571 return fputwc(wc
, stdout
);
2574 /*********************************************************************
2575 * _wfsopen (MSVCRT.@)
2577 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2580 int open_flags
, stream_flags
, fd
;
2582 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2584 /* map mode string to open() flags. "man fopen" for possibilities. */
2585 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2589 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2592 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2594 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2601 TRACE(":got (%p)\n",file
);
2602 if (fd
>= 0 && !file
)
2608 /*********************************************************************
2609 * _fsopen (MSVCRT.@)
2611 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2614 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2616 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2617 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2621 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2624 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2629 ret
= _wfsopen(pathW
, modeW
, share
);
2636 /*********************************************************************
2639 FILE * CDECL
fopen(const char *path
, const char *mode
)
2641 return _fsopen( path
, mode
, _SH_DENYNO
);
2644 /*********************************************************************
2645 * fopen_s (MSVCRT.@)
2647 int CDECL
fopen_s(FILE** pFile
,
2648 const char *filename
, const char *mode
)
2650 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2651 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2656 *pFile
= fopen(filename
, mode
);
2663 /*********************************************************************
2664 * _wfopen (MSVCRT.@)
2666 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2668 return _wfsopen( path
, mode
, _SH_DENYNO
);
2671 /*********************************************************************
2672 * _wfopen_s (MSVCRT.@)
2674 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
2675 const wchar_t *mode
)
2677 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2678 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2683 *pFile
= _wfopen(filename
, mode
);
2690 /* fputc calls _flsbuf which calls fputc */
2691 int CDECL
_flsbuf(int c
, FILE* file
);
2693 /*********************************************************************
2696 int CDECL
fputc(int c
, FILE* file
)
2706 res
= flush_buffer(file
);
2708 return res
? res
: c
;
2715 res
= _flsbuf(c
, file
);
2721 /*********************************************************************
2722 * _fputchar (MSVCRT.@)
2724 int CDECL
_fputchar(int c
)
2726 return fputc(c
, stdout
);
2729 /*********************************************************************
2732 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2734 size_t rcnt
=size
* nmemb
;
2743 /* first buffered data */
2745 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
2746 memcpy(ptr
, file
->_ptr
, pcnt
);
2751 ptr
= (char*)ptr
+ pcnt
;
2752 } else if(!(file
->_flag
& _IOREAD
)) {
2753 if(file
->_flag
& _IORW
) {
2754 file
->_flag
|= _IOREAD
;
2763 /* Fill the buffer on small reads.
2764 * TODO: Use a better buffering strategy.
2766 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2767 if (file
->_bufsiz
== 0) {
2770 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2771 file
->_ptr
= file
->_base
;
2772 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2773 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2774 if (i
> 0 && i
< file
->_cnt
) {
2775 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
2776 file
->_flag
&= ~_IOEOF
;
2779 memcpy(ptr
, file
->_ptr
, i
);
2784 i
= _read(file
->_file
,ptr
, rcnt
);
2788 ptr
= (char *)ptr
+i
;
2789 /* expose feof condition in the flags
2790 * MFC tests file->_flag for feof, and doesn't call feof())
2792 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
2793 file
->_flag
|= _IOEOF
;
2796 file
->_flag
|= _IOERR
;
2807 /*********************************************************************
2808 * _wfreopen (MSVCRT.@)
2811 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
2813 int open_flags
, stream_flags
, fd
;
2815 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
2818 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2823 /* map mode string to open() flags. "man fopen" for possibilities. */
2824 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2828 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2831 else if (init_fp(file
, fd
, stream_flags
) == -1)
2834 WARN(":failed-last error (%d)\n",GetLastError());
2835 _dosmaperr(GetLastError());
2844 /*********************************************************************
2845 * freopen (MSVCRT.@)
2848 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
2851 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2853 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2854 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2860 ret
= _wfreopen(pathW
, modeW
, file
);
2867 /*********************************************************************
2868 * fsetpos (MSVCRT.@)
2870 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2875 /* Note that all this has been lifted 'as is' from fseek */
2876 if(file
->_flag
& _IOWRT
)
2879 /* Discard buffered input */
2881 file
->_ptr
= file
->_base
;
2883 /* Reset direction of i/o */
2884 if(file
->_flag
& _IORW
) {
2885 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2888 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2893 /*********************************************************************
2894 * _ftelli64 (MSVCRT.@)
2896 __int64 CDECL
_ftelli64(FILE* file
)
2901 pos
= _telli64(file
->_file
);
2907 if(file
->_flag
& _IOWRT
) {
2908 pos
+= file
->_ptr
- file
->_base
;
2910 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2913 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
2917 } else if(!file
->_cnt
) { /* nothing to do */
2918 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
2922 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2923 for(i
=0; i
<file
->_cnt
; i
++)
2924 if(file
->_ptr
[i
] == '\n')
2930 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
2935 pos
-= file
->_bufsiz
;
2936 pos
+= file
->_ptr
- file
->_base
;
2938 if(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2939 if(get_ioinfo(file
->_file
)->wxflag
& WX_READNL
)
2942 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
2953 /*********************************************************************
2956 LONG CDECL
ftell(FILE* file
)
2958 return (LONG
)_ftelli64(file
);
2961 /*********************************************************************
2962 * fgetpos (MSVCRT.@)
2964 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2969 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
2975 if( file
->_flag
& _IOWRT
) {
2976 off
= file
->_ptr
- file
->_base
;
2979 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2980 /* Black magic correction for CR removal */
2982 for (i
=0; i
<file
->_cnt
; i
++) {
2983 if (file
->_ptr
[i
] == '\n')
2986 /* Black magic when reading CR at buffer boundary*/
2987 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
2997 /*********************************************************************
3000 int CDECL
fputs(const char *s
, FILE* file
)
3002 size_t i
, len
= strlen(s
);
3006 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3007 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3011 for (i
=0; i
<len
; i
++)
3012 if (fputc(s
[i
], file
) == EOF
) {
3021 /*********************************************************************
3024 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3026 size_t i
, len
= strlenW(s
);
3030 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3031 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3035 for (i
=0; i
<len
; i
++) {
3036 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
3037 || fputwc(s
[i
], file
) == WEOF
) {
3047 /*********************************************************************
3048 * getchar (MSVCRT.@)
3050 int CDECL
getchar(void)
3052 return fgetc(stdin
);
3055 /*********************************************************************
3058 int CDECL
getc(FILE* file
)
3063 /*********************************************************************
3066 char * CDECL
gets(char *buf
)
3069 char * buf_start
= buf
;
3072 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3074 if(cc
!= '\r') *buf
++ = (char)cc
;
3078 TRACE("got '%s'\n", buf_start
);
3079 _unlock_file(stdin
);
3083 /*********************************************************************
3086 wchar_t* CDECL
_getws(wchar_t* buf
)
3092 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3096 *buf
++ = (wchar_t)cc
;
3100 TRACE("got %s\n", debugstr_w(ws
));
3101 _unlock_file(stdin
);
3105 /*********************************************************************
3108 int CDECL
putc(int c
, FILE* file
)
3110 return fputc(c
, file
);
3113 /*********************************************************************
3114 * putchar (MSVCRT.@)
3116 int CDECL
putchar(int c
)
3118 return fputc(c
, stdout
);
3121 /*********************************************************************
3122 * _putwch (MSVCRT.@)
3124 wint_t CDECL
_putwch(wchar_t c
)
3126 return fputwc(c
, stdout
);
3129 /*********************************************************************
3132 int CDECL
puts(const char *s
)
3134 size_t len
= strlen(s
);
3138 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3139 _unlock_file(stdout
);
3143 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3144 _unlock_file(stdout
);
3148 /*********************************************************************
3151 int CDECL
_putws(const wchar_t *s
)
3153 static const wchar_t nl
= '\n';
3154 size_t len
= strlenW(s
);
3158 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3159 _unlock_file(stdout
);
3163 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3164 _unlock_file(stdout
);
3168 /*********************************************************************
3171 int CDECL
remove(const char *path
)
3173 TRACE("(%s)\n",path
);
3174 if (DeleteFileA(path
))
3176 TRACE(":failed (%d)\n",GetLastError());
3177 _dosmaperr(GetLastError());
3181 /*********************************************************************
3182 * _wremove (MSVCRT.@)
3184 int CDECL
_wremove(const wchar_t *path
)
3186 TRACE("(%s)\n",debugstr_w(path
));
3187 if (DeleteFileW(path
))
3189 TRACE(":failed (%d)\n",GetLastError());
3190 _dosmaperr(GetLastError());
3194 /*********************************************************************
3197 int CDECL
rename(const char *oldpath
,const char *newpath
)
3199 TRACE(":from %s to %s\n",oldpath
,newpath
);
3200 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3202 TRACE(":failed (%d)\n",GetLastError());
3203 _dosmaperr(GetLastError());
3207 /*********************************************************************
3208 * _wrename (MSVCRT.@)
3210 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3212 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3213 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3215 TRACE(":failed (%d)\n",GetLastError());
3216 _dosmaperr(GetLastError());
3220 /*********************************************************************
3221 * setvbuf (MSVCRT.@)
3223 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3231 if(mode
== _IOFBF
) {
3232 file
->_flag
&= ~_IONBF
;
3233 file
->_base
= file
->_ptr
= buf
;
3235 file
->_bufsiz
= size
;
3238 file
->_flag
|= _IONBF
;
3244 /*********************************************************************
3247 void CDECL
setbuf(FILE* file
, char *buf
)
3249 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3252 /*********************************************************************
3255 char * CDECL
tmpnam(char *s
)
3262 thread_data_t
*data
= msvcrt_get_thread_data();
3264 if(!data
->tmpnam_buffer
)
3265 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3267 s
= data
->tmpnam_buffer
;
3270 int_to_base32(GetCurrentProcessId(), tmpstr
);
3271 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3272 for (count
= 0; count
< TMP_MAX
; count
++)
3274 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3275 memcpy(p
, tmpstr
, size
);
3276 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3277 GetLastError() == ERROR_FILE_NOT_FOUND
)
3283 /*********************************************************************
3284 * _wtmpnam (MSVCRT.@)
3286 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3288 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3293 thread_data_t
*data
= msvcrt_get_thread_data();
3295 if(!data
->wtmpnam_buffer
)
3296 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3298 s
= data
->wtmpnam_buffer
;
3301 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3302 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3303 for (count
= 0; count
< TMP_MAX
; count
++)
3305 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3306 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3307 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3308 GetLastError() == ERROR_FILE_NOT_FOUND
)
3314 /*********************************************************************
3315 * tmpfile (MSVCRT.@)
3317 FILE* CDECL
tmpfile(void)
3319 char *filename
= tmpnam(NULL
);
3324 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
3325 if (fd
!= -1 && (file
= alloc_fp()))
3327 if (init_fp(file
, fd
, _O_RDWR
) == -1)
3332 else file
->_tmpfname
= _strdup(filename
);
3338 /*********************************************************************
3341 int CDECL
ungetc(int c
, FILE * file
)
3347 if(file
->_bufsiz
== 0) {
3351 if(file
->_ptr
>file
->_base
) {
3364 /*********************************************************************
3365 * ungetwc (MSVCRT.@)
3367 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3370 char * pp
= (char *)&mwc
;
3374 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3375 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3387 /*********************************************************************
3388 * _getmaxstdio (MSVCRT.@)
3390 int CDECL
_getmaxstdio(void)
3395 /*********************************************************************
3396 * _setmaxstdio (MSVCRT.@)
3398 int CDECL
_setmaxstdio(int newmax
)
3400 TRACE("%d\n", newmax
);
3402 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3405 max_streams
= newmax
;