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_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
87 #define WX_READCR 0x08 /* underlying file is at \r */
88 #define WX_DONTINHERIT 0x10
89 #define WX_APPEND 0x20
92 /* FIXME: this should be allocated dynamically */
93 #define MAX_FILES 2048
94 #define FD_BLOCK_SIZE 64
101 CRITICAL_SECTION crit
;
104 /*********************************************************************
105 * __pioinfo (MSVCRT.@)
106 * array of pointers to ioinfo arrays [64]
108 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
110 /*********************************************************************
111 * __badioinfo (MSVCRT.@)
113 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
115 static int fdstart
= 3; /* first unallocated fd */
116 static int fdend
= 3; /* highest allocated fd */
120 CRITICAL_SECTION crit
;
123 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
124 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
] = { NULL
};
125 static int max_streams
= 512, stream_idx
;
127 /* INTERNAL: process umask */
128 static int MSVCRT_umask
= 0;
130 /* INTERNAL: static data for tmpnam and _wtmpname functions */
131 static int tmpnam_unique
;
133 /* This critical section protects the tables __pioinfo and fstreams,
134 * and their related indexes, fdstart, fdend,
135 * and stream_idx, from race conditions.
136 * It doesn't protect against race conditions manipulating the underlying files
137 * or flags; doing so would probably be better accomplished with per-file
138 * protection, rather than locking the whole table for every change.
140 static CRITICAL_SECTION file_cs
;
141 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
142 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
144 static inline ioinfo
* get_ioinfo(int fd
)
148 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
152 return ret
+ (fd
%FD_BLOCK_SIZE
);
155 static inline FILE* get_file(int i
)
165 ret
= fstream
[i
/FD_BLOCK_SIZE
];
167 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
168 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
169 ERR("out of memory\n");
174 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
176 ret
+= i
%FD_BLOCK_SIZE
;
181 static inline BOOL
is_valid_fd(int fd
)
183 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
186 /* INTERNAL: Get the HANDLE for a fd
187 * This doesn't lock the table, because a failure will result in
188 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
189 * it returns a valid handle which is about to be closed, a subsequent call
190 * will fail, most likely in a sane way.
192 /*static*/ HANDLE
fdtoh(int fd
)
194 if (!is_valid_fd(fd
))
196 WARN(":fd (%d) - no handle!\n",fd
);
199 return INVALID_HANDLE_VALUE
;
201 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
202 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
203 return get_ioinfo(fd
)->handle
;
206 /* INTERNAL: free a file entry fd */
207 static void free_fd(int fd
)
213 fdinfo
= get_ioinfo(fd
);
214 old_handle
= fdinfo
->handle
;
215 if(fdinfo
!= &__badioinfo
)
217 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
220 TRACE(":fd (%d) freed\n",fd
);
221 if (fd
< 3) /* don't use 0,1,2 for user files */
226 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
229 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
232 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
246 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
247 /* caller must hold the files lock */
248 static int alloc_fd_from(HANDLE hand
, int flag
, int fd
)
254 WARN(":files exhausted!\n");
259 fdinfo
= get_ioinfo(fd
);
260 if(fdinfo
== &__badioinfo
) {
263 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
264 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
265 WARN(":out of memory!\n");
270 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
271 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
273 fdinfo
= get_ioinfo(fd
);
276 fdinfo
->handle
= hand
;
277 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
279 /* locate next free slot */
280 if (fd
== fdstart
&& fd
== fdend
)
283 while (fdstart
< fdend
&&
284 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
286 /* update last fd in use */
289 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
293 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
294 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
295 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
301 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
302 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
307 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
308 ret
= alloc_fd_from(hand
, flag
, fdstart
);
313 /* INTERNAL: Allocate a FILE* for an fd slot */
314 /* caller must hold the files lock */
315 static FILE* alloc_fp(void)
320 for (i
= 3; i
< (unsigned int)max_streams
; i
++)
326 if (file
->_flag
== 0)
328 if (i
== stream_idx
) stream_idx
++;
336 /* INTERNAL: initialize a FILE* from an open fd */
337 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
339 TRACE(":fd (%d) allocating FILE*\n",fd
);
340 if (!is_valid_fd(fd
))
342 WARN(":invalid fd %d\n",fd
);
347 memset(file
, 0, sizeof(*file
));
349 file
->_flag
= stream_flags
;
351 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
352 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
354 TRACE(":got FILE* (%p)\n",file
);
358 /* INTERNAL: Create an inheritance data block (for spawned process)
359 * The inheritance block is made of:
360 * 00 int nb of file descriptor (NBFD)
361 * 04 char file flags (wxflag): repeated for each fd
362 * 4+NBFD HANDLE file handle: repeated for each fd
364 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
371 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
372 *block
= calloc(*size
, 1);
378 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
379 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
381 *(unsigned*)*block
= fdend
;
382 for (fd
= 0; fd
< fdend
; fd
++)
384 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
385 fdinfo
= get_ioinfo(fd
);
386 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
388 *wxflag_ptr
= fdinfo
->wxflag
;
389 *handle_ptr
= fdinfo
->handle
;
394 *handle_ptr
= INVALID_HANDLE_VALUE
;
396 wxflag_ptr
++; handle_ptr
++;
401 /* INTERNAL: Set up all file descriptors,
402 * as well as default streams (stdin, stderr and stdout)
404 void msvcrt_init_io(void)
410 InitializeCriticalSection(&file_cs
);
411 file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_cs");
412 GetStartupInfoA(&si
);
413 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
419 count
= *(unsigned*)si
.lpReserved2
;
420 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
421 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
423 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
424 count
= min(count
, MAX_FILES
);
425 for (i
= 0; i
< count
; i
++)
427 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
428 alloc_fd_from(*handle_ptr
, *wxflag_ptr
, i
);
430 wxflag_ptr
++; handle_ptr
++;
432 fdend
= max( 3, count
);
433 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
434 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
438 alloc_fd_from(INVALID_HANDLE_VALUE
, 0, 3);
440 fdinfo
= get_ioinfo(0);
441 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
443 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
445 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
446 GetCurrentProcess(), &fdinfo
->handle
,
447 0, TRUE
, DUPLICATE_SAME_ACCESS
))
449 fdinfo
->handle
= std
;
451 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
454 fdinfo
= get_ioinfo(1);
455 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
457 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
459 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
460 GetCurrentProcess(), &fdinfo
->handle
,
461 0, TRUE
, DUPLICATE_SAME_ACCESS
))
463 fdinfo
->handle
= std
;
465 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
468 fdinfo
= get_ioinfo(2);
469 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
471 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
473 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
474 GetCurrentProcess(), &fdinfo
->handle
,
475 0, TRUE
, DUPLICATE_SAME_ACCESS
))
477 fdinfo
->handle
= std
;
479 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
482 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(0)->handle
,
483 get_ioinfo(1)->handle
, get_ioinfo(2)->handle
);
485 memset(_iob
,0,3*sizeof(FILE));
486 for (i
= 0; i
< 3; i
++)
488 /* FILE structs for stdin/out/err are static and never deleted */
490 _iob
[i
]._tmpfname
= NULL
;
491 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
496 /* INTERNAL: Flush stdio file buffer */
497 static int flush_buffer(FILE* file
)
500 int cnt
=file
->_ptr
-file
->_base
;
501 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
502 file
->_flag
|= _IOERR
;
505 file
->_ptr
=file
->_base
;
506 file
->_cnt
=file
->_bufsiz
;
511 /* INTERNAL: Allocate stdio file buffer */
512 /*static*/ void alloc_buffer(FILE* file
)
514 file
->_base
= calloc(BUFSIZ
,1);
516 file
->_bufsiz
= BUFSIZ
;
517 file
->_flag
|= _IOMYBUF
;
519 file
->_base
= (char*)(&file
->_charbuf
);
521 file
->_bufsiz
= sizeof(file
->_charbuf
);
523 file
->_ptr
= file
->_base
;
527 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
528 static int int_to_base32(int num
, char *str
)
543 *p
= (num
& 31) + '0';
545 *p
+= ('a' - '0' - 10);
552 /* INTERNAL: wide character version of int_to_base32 */
553 static int int_to_base32_w(int num
, wchar_t *str
)
568 *p
= (num
& 31) + '0';
570 *p
+= ('a' - '0' - 10);
577 /* INTERNAL: Create a wide string from an ascii string */
578 wchar_t *msvcrt_wstrdupa(const char *str
)
580 const unsigned int len
= strlen(str
) + 1 ;
581 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
584 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
588 /*********************************************************************
589 * __iob_func(MSVCRT.@)
591 FILE * CDECL
__iob_func(void)
596 /*********************************************************************
599 int CDECL
_access(const char *filename
, int mode
)
601 DWORD attr
= GetFileAttributesA(filename
);
603 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
605 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
607 _dosmaperr(GetLastError());
610 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
612 _set_errno(ERROR_ACCESS_DENIED
);
618 /*********************************************************************
619 * _access_s (MSVCRT.@)
621 int CDECL
_access_s(const char *filename
, int mode
)
623 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
624 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
630 return _access(filename
, mode
);
633 /*********************************************************************
634 * _waccess (MSVCRT.@)
636 int CDECL
_waccess(const wchar_t *filename
, int mode
)
638 DWORD attr
= GetFileAttributesW(filename
);
640 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
642 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
644 _dosmaperr(GetLastError());
647 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
649 _set_errno(ERROR_ACCESS_DENIED
);
655 /*********************************************************************
656 * _waccess_s (MSVCRT.@)
658 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
660 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
661 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
667 return _waccess(filename
, mode
);
670 /*********************************************************************
673 int CDECL
_chmod(const char *path
, int flags
)
675 DWORD oldFlags
= GetFileAttributesA(path
);
677 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
679 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
680 oldFlags
| FILE_ATTRIBUTE_READONLY
;
682 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
685 _dosmaperr(GetLastError());
689 /*********************************************************************
692 int CDECL
_wchmod(const wchar_t *path
, int flags
)
694 DWORD oldFlags
= GetFileAttributesW(path
);
696 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
698 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
699 oldFlags
| FILE_ATTRIBUTE_READONLY
;
701 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
704 _dosmaperr(GetLastError());
708 /*********************************************************************
711 int CDECL
_unlink(const char *path
)
713 TRACE("%s\n",debugstr_a(path
));
714 if(DeleteFileA(path
))
716 TRACE("failed (%d)\n",GetLastError());
717 _dosmaperr(GetLastError());
721 /*********************************************************************
722 * _wunlink (MSVCRT.@)
724 int CDECL
_wunlink(const wchar_t *path
)
726 TRACE("(%s)\n",debugstr_w(path
));
727 if(DeleteFileW(path
))
729 TRACE("failed (%d)\n",GetLastError());
730 _dosmaperr(GetLastError());
734 /* _flushall calls fflush which calls _flushall */
735 int CDECL
fflush(FILE* file
);
737 /*********************************************************************
738 * _flushall (MSVCRT.@)
740 int CDECL
_flushall(void)
742 int i
, num_flushed
= 0;
746 for (i
= 3; i
< stream_idx
; i
++) {
751 if(file
->_flag
& _IOWRT
) {
759 TRACE(":flushed (%d) handles\n",num_flushed
);
763 /*********************************************************************
766 int CDECL
fflush(FILE* file
)
770 } else if(file
->_flag
& _IOWRT
) {
774 res
= flush_buffer(file
);
782 /*********************************************************************
785 int CDECL
_close(int fd
)
792 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
793 if (hand
== INVALID_HANDLE_VALUE
)
795 else if (!CloseHandle(hand
))
797 WARN(":failed-last error (%d)\n",GetLastError());
798 _dosmaperr(GetLastError());
811 /*********************************************************************
814 int CDECL
_commit(int fd
)
816 HANDLE hand
= fdtoh(fd
);
818 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
819 if (hand
== INVALID_HANDLE_VALUE
)
822 if (!FlushFileBuffers(hand
))
824 if (GetLastError() == ERROR_INVALID_HANDLE
)
826 /* FlushFileBuffers fails for console handles
827 * so we ignore this error.
831 TRACE(":failed-last error (%d)\n",GetLastError());
832 _dosmaperr(GetLastError());
839 /*********************************************************************
842 * MSDN isn't clear on this point, but the remarks for _pipe
843 * indicate file descriptors duplicated with _dup and _dup2 are always
846 int CDECL
_dup2(int od
, int nd
)
850 TRACE("(od=%d, nd=%d)\n", od
, nd
);
852 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
856 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
857 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
859 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
863 ret
= alloc_fd_from(handle
, wxflag
, nd
);
871 /* _dup2 returns 0, not nd, on success */
878 _dosmaperr(GetLastError());
890 /*********************************************************************
893 int CDECL
_dup(int od
)
899 if (_dup2(od
, fd
) == 0)
907 /*********************************************************************
910 int CDECL
_eof(int fd
)
913 LONG hcurpos
,hendpos
;
914 HANDLE hand
= fdtoh(fd
);
916 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
918 if (hand
== INVALID_HANDLE_VALUE
)
921 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
923 /* Otherwise we do it the hard way */
924 hcurpos
= hendpos
= 0;
925 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
926 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
928 if (curpos
== endpos
&& hcurpos
== hendpos
)
930 /* FIXME: shouldn't WX_ATEOF be set here? */
934 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
938 /*********************************************************************
939 * _fcloseall (MSVCRT.@)
941 int CDECL
_fcloseall(void)
943 int num_closed
= 0, i
;
947 for (i
= 3; i
< stream_idx
; i
++) {
950 if (file
->_flag
&& !fclose(file
))
955 TRACE(":closed (%d) handles\n",num_closed
);
959 /* free everything on process exit */
960 void msvcrt_free_io(void)
965 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
966 * stdout, and stderr (unlike GNU), so we need to fclose() them here
967 * or they won't get flushed.
973 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
976 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
979 file_cs
.DebugInfo
->Spare
[0] = 0;
980 DeleteCriticalSection(&file_cs
);
983 /*********************************************************************
984 * _lseeki64 (MSVCRT.@)
986 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
988 HANDLE hand
= fdtoh(fd
);
991 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
992 if (hand
== INVALID_HANDLE_VALUE
)
995 if (whence
< 0 || whence
> 2)
1001 TRACE(":fd (%d) to %s pos %s\n",
1002 fd
,wine_dbgstr_longlong(offset
),
1003 (whence
==SEEK_SET
)?"SEEK_SET":
1004 (whence
==SEEK_CUR
)?"SEEK_CUR":
1005 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1007 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1008 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1009 ofs
.QuadPart
= offset
;
1010 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1011 GetLastError() == ERROR_SUCCESS
)
1013 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1014 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1016 return ofs
.QuadPart
;
1018 TRACE(":error-last error (%d)\n",GetLastError());
1019 _dosmaperr(GetLastError());
1023 /*********************************************************************
1026 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1028 return (LONG
)_lseeki64(fd
, offset
, whence
);
1031 /*********************************************************************
1032 * _lock_file (MSVCRT.@)
1034 void CDECL
_lock_file(FILE *file
)
1036 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1037 _lock(_STREAM_LOCKS
+(file
-_iob
));
1038 /* ReactOS: string streams dont need to be locked */
1039 else if(!(file
->_flag
& _IOSTRG
))
1040 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1043 /*********************************************************************
1044 * _unlock_file (MSVCRT.@)
1046 void CDECL
_unlock_file(FILE *file
)
1048 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1049 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1050 /* ReactOS: string streams dont need to be locked */
1051 else if(!(file
->_flag
& _IOSTRG
))
1052 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1056 /*********************************************************************
1057 * _locking (MSVCRT.@)
1059 * This is untested; the underlying LockFile doesn't work yet.
1061 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1065 HANDLE hand
= fdtoh(fd
);
1067 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1068 if (hand
== INVALID_HANDLE_VALUE
)
1071 if (mode
< 0 || mode
> 4)
1077 TRACE(":fd (%d) by 0x%08x mode %s\n",
1078 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1079 (mode
==_LK_LOCK
)?"_LK_LOCK":
1080 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1081 (mode
==_LK_RLCK
)?"_LK_RLCK":
1082 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1085 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1087 FIXME ("Seek failed\n");
1088 *_errno() = EINVAL
; /* FIXME */
1091 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1094 ret
= 1; /* just to satisfy gcc */
1097 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1102 else if (mode
== _LK_UNLCK
)
1103 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1105 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1106 /* FIXME - what about error settings? */
1107 return ret
? 0 : -1;
1110 /*********************************************************************
1111 * _fseeki64 (MSVCRT.@)
1113 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1118 /* Flush output if needed */
1119 if(file
->_flag
& _IOWRT
)
1122 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1123 offset
-= file
->_cnt
;
1124 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1125 /* Black magic correction for CR removal */
1127 for (i
=0; i
<file
->_cnt
; i
++) {
1128 if (file
->_ptr
[i
] == '\n')
1131 /* Black magic when reading CR at buffer boundary*/
1132 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1136 /* Discard buffered input */
1138 file
->_ptr
= file
->_base
;
1139 /* Reset direction of i/o */
1140 if(file
->_flag
& _IORW
) {
1141 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1143 /* Clear end of file flag */
1144 file
->_flag
&= ~_IOEOF
;
1145 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1151 /*********************************************************************
1154 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1156 return _fseeki64( file
, offset
, whence
);
1159 /*********************************************************************
1160 * _chsize (MSVCRT.@)
1162 int CDECL
_chsize(int fd
, long size
)
1168 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1173 if (handle
!= INVALID_HANDLE_VALUE
)
1175 /* save the current file pointer */
1176 cur
= _lseek(fd
, 0, SEEK_CUR
);
1179 pos
= _lseek(fd
, size
, SEEK_SET
);
1182 ret
= SetEndOfFile(handle
);
1183 if (!ret
) _dosmaperr(GetLastError());
1186 /* restore the file pointer */
1187 _lseek(fd
, cur
, SEEK_SET
);
1192 return ret
? 0 : -1;
1195 /*********************************************************************
1196 * clearerr (MSVCRT.@)
1198 void CDECL
clearerr(FILE* file
)
1200 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1203 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1207 /*********************************************************************
1210 void CDECL
rewind(FILE* file
)
1212 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1215 fseek(file
, 0L, SEEK_SET
);
1220 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1222 int plus
= strchrW(mode
, '+') != NULL
;
1227 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1228 *stream_flags
= plus
? _IORW
: _IOREAD
;
1231 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1232 *stream_flags
= plus
? _IORW
: _IOWRT
;
1235 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1236 *stream_flags
= plus
? _IORW
: _IOWRT
;
1239 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1248 *open_flags
|= _O_BINARY
;
1249 *open_flags
&= ~_O_TEXT
;
1252 *open_flags
|= _O_TEXT
;
1253 *open_flags
&= ~_O_BINARY
;
1259 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1264 /*********************************************************************
1265 * _fdopen (MSVCRT.@)
1267 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1270 wchar_t *modeW
= NULL
;
1272 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1274 ret
= _wfdopen(fd
, modeW
);
1280 /*********************************************************************
1281 * _wfdopen (MSVCRT.@)
1283 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1285 int open_flags
, stream_flags
;
1288 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1291 if (!(file
= alloc_fp()))
1293 else if (init_fp(file
, fd
, stream_flags
) == -1)
1298 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1304 /*********************************************************************
1305 * _filelength (MSVCRT.@)
1307 LONG CDECL
_filelength(int fd
)
1309 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1312 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1315 if (endPos
!= curPos
)
1316 _lseek(fd
, curPos
, SEEK_SET
);
1323 /*********************************************************************
1324 * _filelengthi64 (MSVCRT.@)
1326 __int64 CDECL
_filelengthi64(int fd
)
1328 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1331 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1334 if (endPos
!= curPos
)
1335 _lseeki64(fd
, curPos
, SEEK_SET
);
1342 /*********************************************************************
1343 * _fileno (MSVCRT.@)
1345 int CDECL
_fileno(FILE* file
)
1347 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1351 /*********************************************************************
1352 * _get_osfhandle (MSVCRT.@)
1354 intptr_t CDECL
_get_osfhandle(int fd
)
1356 HANDLE hand
= fdtoh(fd
);
1357 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1359 return (intptr_t)hand
;
1362 /*********************************************************************
1363 * _isatty (MSVCRT.@)
1365 int CDECL
_isatty(int fd
)
1367 HANDLE hand
= fdtoh(fd
);
1369 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1370 if (hand
== INVALID_HANDLE_VALUE
)
1373 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1376 /*********************************************************************
1377 * _mktemp (MSVCRT.@)
1379 char * CDECL
_mktemp(char *pattern
)
1382 char *retVal
= pattern
;
1387 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1391 id
= GetCurrentProcessId();
1395 int tempNum
= id
/ 10;
1396 *pattern
-- = id
- (tempNum
* 10) + '0';
1402 *pattern
= letter
++;
1403 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1404 GetLastError() == ERROR_FILE_NOT_FOUND
)
1406 } while(letter
<= 'z');
1410 /*********************************************************************
1411 * _wmktemp (MSVCRT.@)
1413 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1416 wchar_t *retVal
= pattern
;
1418 wchar_t letter
= 'a';
1421 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1425 id
= GetCurrentProcessId();
1429 int tempNum
= id
/ 10;
1430 *pattern
-- = id
- (tempNum
* 10) + '0';
1436 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1437 GetLastError() == ERROR_FILE_NOT_FOUND
)
1439 *pattern
= letter
++;
1440 } while(letter
!= '|');
1444 /*static*/ unsigned split_oflags(unsigned oflags
)
1447 unsigned unsupp
; /* until we support everything */
1449 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1450 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1451 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1452 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1453 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1454 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1456 if ((unsupp
= oflags
& ~(
1457 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1458 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1459 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1461 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1463 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1468 /*********************************************************************
1471 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1474 SECURITY_ATTRIBUTES sa
;
1475 HANDLE readHandle
, writeHandle
;
1483 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1484 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1485 sa
.lpSecurityDescriptor
= NULL
;
1486 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1488 unsigned int wxflags
= split_oflags(textmode
);
1492 fd
= alloc_fd(readHandle
, wxflags
);
1496 fd
= alloc_fd(writeHandle
, wxflags
);
1505 CloseHandle(writeHandle
);
1511 CloseHandle(readHandle
);
1512 CloseHandle(writeHandle
);
1518 _dosmaperr(GetLastError());
1523 /*********************************************************************
1524 * _sopen_s (MSVCRT.@)
1526 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1528 DWORD access
= 0, creation
= 0, attrib
;
1532 SECURITY_ATTRIBUTES sa
;
1534 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1535 fd
, path
, oflags
, shflags
, pmode
);
1539 MSVCRT_INVALID_PMT("null out fd pointer");
1545 wxflag
= split_oflags(oflags
);
1546 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1548 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1549 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1550 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1553 if (oflags
& _O_CREAT
)
1555 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1556 FIXME(": pmode 0x%04x ignored\n", pmode
);
1558 WARN(": pmode 0x%04x ignored\n", pmode
);
1560 if (oflags
& _O_EXCL
)
1561 creation
= CREATE_NEW
;
1562 else if (oflags
& _O_TRUNC
)
1563 creation
= CREATE_ALWAYS
;
1565 creation
= OPEN_ALWAYS
;
1567 else /* no _O_CREAT */
1569 if (oflags
& _O_TRUNC
)
1570 creation
= TRUNCATE_EXISTING
;
1572 creation
= OPEN_EXISTING
;
1581 sharing
= FILE_SHARE_READ
;
1584 sharing
= FILE_SHARE_WRITE
;
1587 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1590 ERR( "Unhandled shflags 0x%x\n", shflags
);
1593 attrib
= FILE_ATTRIBUTE_NORMAL
;
1595 if (oflags
& _O_TEMPORARY
)
1597 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1599 sharing
|= FILE_SHARE_DELETE
;
1602 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1603 sa
.lpSecurityDescriptor
= NULL
;
1604 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1606 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1607 if (hand
== INVALID_HANDLE_VALUE
) {
1608 WARN(":failed-last error (%d)\n", GetLastError());
1609 _dosmaperr(GetLastError());
1613 *fd
= alloc_fd(hand
, wxflag
);
1615 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1619 /*********************************************************************
1622 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1627 if (oflags
& _O_CREAT
)
1631 va_start(ap
, shflags
);
1632 pmode
= va_arg(ap
, int);
1638 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1642 /*********************************************************************
1643 * _wsopen_s (MSVCRT.@)
1645 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1647 DWORD access
= 0, creation
= 0, attrib
;
1648 SECURITY_ATTRIBUTES sa
;
1653 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1654 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1658 MSVCRT_INVALID_PMT("null out fd pointer");
1664 wxflag
= split_oflags(oflags
);
1665 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1667 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1668 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1669 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1672 if (oflags
& _O_CREAT
)
1674 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1675 FIXME(": pmode 0x%04x ignored\n", pmode
);
1677 WARN(": pmode 0x%04x ignored\n", pmode
);
1679 if (oflags
& _O_EXCL
)
1680 creation
= CREATE_NEW
;
1681 else if (oflags
& _O_TRUNC
)
1682 creation
= CREATE_ALWAYS
;
1684 creation
= OPEN_ALWAYS
;
1686 else /* no _O_CREAT */
1688 if (oflags
& _O_TRUNC
)
1689 creation
= TRUNCATE_EXISTING
;
1691 creation
= OPEN_EXISTING
;
1700 sharing
= FILE_SHARE_READ
;
1703 sharing
= FILE_SHARE_WRITE
;
1706 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1709 ERR( "Unhandled shflags 0x%x\n", shflags
);
1712 attrib
= FILE_ATTRIBUTE_NORMAL
;
1714 if (oflags
& _O_TEMPORARY
)
1716 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1718 sharing
|= FILE_SHARE_DELETE
;
1721 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1722 sa
.lpSecurityDescriptor
= NULL
;
1723 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1725 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1727 if (hand
== INVALID_HANDLE_VALUE
) {
1728 WARN(":failed-last error (%d)\n",GetLastError());
1729 _dosmaperr(GetLastError());
1733 *fd
= alloc_fd(hand
, wxflag
);
1735 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1739 /*********************************************************************
1740 * _wsopen (MSVCRT.@)
1742 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1747 if (oflags
& _O_CREAT
)
1751 va_start(ap
, shflags
);
1752 pmode
= va_arg(ap
, int);
1758 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1762 /*********************************************************************
1765 int CDECL
_open( const char *path
, int flags
, ... )
1769 if (flags
& _O_CREAT
)
1772 va_start(ap
, flags
);
1773 pmode
= va_arg(ap
, int);
1775 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1778 return _sopen( path
, flags
, _SH_DENYNO
);
1781 /*********************************************************************
1784 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1788 if (flags
& _O_CREAT
)
1791 va_start(ap
, flags
);
1792 pmode
= va_arg(ap
, int);
1794 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1797 return _wsopen( path
, flags
, _SH_DENYNO
);
1800 /*********************************************************************
1803 int CDECL
_creat(const char *path
, int flags
)
1805 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1806 return _open(path
, usedFlags
);
1809 /*********************************************************************
1810 * _wcreat (MSVCRT.@)
1812 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1814 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1815 return _wopen(path
, usedFlags
);
1818 /*********************************************************************
1819 * _open_osfhandle (MSVCRT.@)
1821 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1825 /* _O_RDONLY (0) always matches, so set the read flag
1826 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1827 * file, so set the write flag. It also only sets _O_TEXT if it wants
1828 * text - it never sets _O_BINARY.
1830 /* don't let split_oflags() decide the mode if no mode is passed */
1831 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1832 oflags
|= _O_BINARY
;
1834 fd
= alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1835 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1839 /*********************************************************************
1842 int CDECL
_rmtmp(void)
1844 int num_removed
= 0, i
;
1848 for (i
= 3; i
< stream_idx
; i
++) {
1851 if (file
->_tmpfname
)
1860 TRACE(":removed (%d) temp files\n",num_removed
);
1864 /*********************************************************************
1867 * When reading \r as last character in text mode, read() positions
1868 * the file pointer on the \r character while getc() goes on to
1871 static int read_i(int fd
, void *buf
, unsigned int count
)
1874 char *bufstart
= buf
;
1875 HANDLE hand
= fdtoh(fd
);
1876 ioinfo
*fdinfo
= get_ioinfo(fd
);
1881 if (fdinfo
->wxflag
& WX_READEOF
) {
1882 fdinfo
->wxflag
|= WX_ATEOF
;
1883 TRACE("already at EOF, returning 0\n");
1886 /* Don't trace small reads, it gets *very* annoying */
1888 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1889 if (hand
== INVALID_HANDLE_VALUE
)
1892 /* Reading single bytes in O_TEXT mode makes things slow
1893 * So read big chunks
1895 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1897 if (count
!= 0 && num_read
== 0)
1899 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1900 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1902 else if (fdinfo
->wxflag
& WX_TEXT
)
1905 if (bufstart
[num_read
-1] == '\r')
1909 fdinfo
->wxflag
&= ~WX_READCR
;
1910 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1914 fdinfo
->wxflag
|= WX_READCR
;
1919 fdinfo
->wxflag
&= ~WX_READCR
;
1920 for (i
=0, j
=0; i
<num_read
; i
++)
1922 /* in text mode, a ctrl-z signals EOF */
1923 if (bufstart
[i
] == 0x1a)
1925 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1926 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1929 /* in text mode, strip \r if followed by \n.
1930 * BUG: should save state across calls somehow, so CR LF that
1931 * straddles buffer boundary gets recognized properly?
1933 if ((bufstart
[i
] != '\r')
1934 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1935 bufstart
[j
++] = bufstart
[i
];
1942 if (GetLastError() == ERROR_BROKEN_PIPE
)
1944 TRACE(":end-of-pipe\n");
1945 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1950 TRACE(":failed-last error (%d)\n",GetLastError());
1956 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1960 /*********************************************************************
1963 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
1966 num_read
= read_i(fd
, buf
, count
);
1970 /*********************************************************************
1971 * _setmode (MSVCRT.@)
1973 int CDECL
_setmode(int fd
,int mode
)
1975 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
1976 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1977 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1978 if ((mode
& _O_TEXT
) == _O_TEXT
)
1979 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
1981 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
1985 /*********************************************************************
1988 long CDECL
_tell(int fd
)
1990 return _lseek(fd
, 0, SEEK_CUR
);
1993 /*********************************************************************
1994 * _telli64 (MSVCRT.@)
1996 __int64 CDECL
_telli64(int fd
)
1998 return _lseeki64(fd
, 0, SEEK_CUR
);
2001 /*********************************************************************
2002 * _tempnam (MSVCRT.@)
2004 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2006 char tmpbuf
[MAX_PATH
];
2007 const char *tmp_dir
= getenv("TMP");
2009 if (tmp_dir
) dir
= tmp_dir
;
2011 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2012 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2014 TRACE("got name (%s)\n",tmpbuf
);
2015 DeleteFileA(tmpbuf
);
2016 return _strdup(tmpbuf
);
2018 TRACE("failed (%d)\n",GetLastError());
2022 /*********************************************************************
2023 * _wtempnam (MSVCRT.@)
2025 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2027 wchar_t tmpbuf
[MAX_PATH
];
2029 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2030 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2032 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2033 DeleteFileW(tmpbuf
);
2034 return _wcsdup(tmpbuf
);
2036 TRACE("failed (%d)\n",GetLastError());
2040 /*********************************************************************
2043 int CDECL
_umask(int umask
)
2045 int old_umask
= umask
;
2046 TRACE("(%d)\n",umask
);
2047 MSVCRT_umask
= umask
;
2051 /*********************************************************************
2054 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2057 HANDLE hand
= fdtoh(fd
);
2059 /* Don't trace small writes, it gets *very* annoying */
2062 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2064 if (hand
== INVALID_HANDLE_VALUE
)
2070 /* If appending, go to EOF */
2071 if (get_ioinfo(fd
)->wxflag
& WX_APPEND
)
2072 _lseek(fd
, 0, FILE_END
);
2074 if (!(get_ioinfo(fd
)->wxflag
& WX_TEXT
))
2076 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2077 && (num_written
== count
))
2079 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2080 hand
, GetLastError());
2085 unsigned int i
, j
, nr_lf
;
2088 const char *s
= buf
, *buf_start
= buf
;
2089 /* find number of \n ( without preceding \r ) */
2090 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2095 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2100 if ((q
= p
= malloc(count
+ nr_lf
)))
2102 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2107 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2114 FIXME("Malloc failed\n");
2122 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2124 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2125 fd
, hand
, GetLastError(), num_written
);
2129 return s
- buf_start
;
2141 /*********************************************************************
2144 int CDECL
_putw(int val
, FILE* file
)
2149 len
= _write(file
->_file
, &val
, sizeof(val
));
2150 if (len
== sizeof(val
)) {
2155 file
->_flag
|= _IOERR
;
2160 /*********************************************************************
2163 int CDECL
fclose(FILE* file
)
2169 free(file
->_tmpfname
);
2170 file
->_tmpfname
= NULL
;
2171 /* flush stdio buffers */
2172 if(file
->_flag
& _IOWRT
)
2174 if(file
->_flag
& _IOMYBUF
)
2177 r
=_close(file
->_file
);
2181 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2182 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2184 if(file
== get_file(stream_idx
-1)) {
2185 while(stream_idx
>3 && !file
->_flag
) {
2187 file
= get_file(stream_idx
-1);
2191 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2194 /*********************************************************************
2197 int CDECL
feof(FILE* file
)
2202 ret
= file
->_flag
& _IOEOF
;
2208 /*********************************************************************
2211 int CDECL
ferror(FILE* file
)
2216 ret
= file
->_flag
& _IOERR
;
2222 /*********************************************************************
2223 * _filbuf (MSVCRT.@)
2225 int CDECL
_filbuf(FILE* file
)
2230 /* Allocate buffer if needed */
2231 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2234 if(!(file
->_flag
& _IOREAD
)) {
2235 if(file
->_flag
& _IORW
)
2236 file
->_flag
|= _IOREAD
;
2243 if(file
->_flag
& _IONBF
) {
2245 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2246 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2254 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2256 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2263 file
->_ptr
= file
->_base
+1;
2264 c
= *(unsigned char *)file
->_base
;
2270 /*********************************************************************
2273 int CDECL
fgetc(FILE* file
)
2281 i
= (unsigned char *)file
->_ptr
++;
2290 /*********************************************************************
2291 * _fgetchar (MSVCRT.@)
2293 int CDECL
_fgetchar(void)
2295 return fgetc(stdin
);
2298 /*********************************************************************
2301 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2304 char * buf_start
= s
;
2306 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2307 file
,file
->_file
,s
,size
);
2311 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2316 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2318 TRACE(":nothing read\n");
2322 if ((cc
!= EOF
) && (size
> 1))
2325 TRACE(":got %s\n", debugstr_a(buf_start
));
2330 /*********************************************************************
2333 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2334 * the CR from CR/LF combinations
2336 wint_t CDECL
fgetwc(FILE* file
)
2341 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2348 for(i
=0; i
<sizeof(wc
); i
++)
2361 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2376 if ((__mb_cur_max
> 1) && isleadbyte(c
))
2378 FIXME("Treat Multibyte characters\n");
2388 /*********************************************************************
2391 int CDECL
_getw(FILE* file
)
2399 for (j
=0; j
<sizeof(int); j
++) {
2402 file
->_flag
|= _IOEOF
;
2413 /*********************************************************************
2416 wint_t CDECL
getwc(FILE* file
)
2418 return fgetwc(file
);
2421 /*********************************************************************
2422 * _fgetwchar (MSVCRT.@)
2424 wint_t CDECL
_fgetwchar(void)
2426 return fgetwc(stdin
);
2429 /*********************************************************************
2430 * getwchar (MSVCRT.@)
2432 wint_t CDECL
getwchar(void)
2434 return _fgetwchar();
2437 /*********************************************************************
2440 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2443 wchar_t * buf_start
= s
;
2445 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2446 file
,file
->_file
,s
,size
);
2450 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2455 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2457 TRACE(":nothing read\n");
2461 if ((cc
!= WEOF
) && (size
> 1))
2464 TRACE(":got %s\n", debugstr_w(buf_start
));
2469 /*********************************************************************
2472 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2474 size_t wrcnt
=size
* nmemb
;
2481 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2482 memcpy(file
->_ptr
, ptr
, pcnt
);
2487 ptr
= (const char*)ptr
+ pcnt
;
2488 } else if(!(file
->_flag
& _IOWRT
)) {
2489 if(file
->_flag
& _IORW
) {
2490 file
->_flag
|= _IOWRT
;
2498 int res
=flush_buffer(file
);
2500 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2503 file
->_flag
|= _IOERR
;
2506 written
+= pwritten
;
2511 return written
/ size
;
2514 /*********************************************************************
2516 * FORKED for ReactOS, don't sync with Wine!
2518 * - http://jira.reactos.org/browse/CORE-6495
2519 * - http://bugs.winehq.org/show_bug.cgi?id=8598
2521 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2523 /* If this is a real file stream (and not some temporary one for
2524 sprintf-like functions), check whether it is opened in text mode.
2525 In this case, we have to perform an implicit conversion to ANSI. */
2526 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
2528 /* Convert to multibyte in text mode */
2529 char mbc
[MB_LEN_MAX
];
2532 mb_return
= wctomb(mbc
, c
);
2537 /* Output all characters */
2538 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2543 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2550 /*********************************************************************
2551 * _fputwchar (MSVCRT.@)
2553 wint_t CDECL
_fputwchar(wint_t wc
)
2555 return fputwc(wc
, stdout
);
2558 /*********************************************************************
2559 * _wfsopen (MSVCRT.@)
2561 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2564 int open_flags
, stream_flags
, fd
;
2566 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2568 /* map mode string to open() flags. "man fopen" for possibilities. */
2569 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2573 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2576 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2578 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2585 TRACE(":got (%p)\n",file
);
2586 if (fd
>= 0 && !file
)
2592 /*********************************************************************
2593 * _fsopen (MSVCRT.@)
2595 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2598 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2600 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2601 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2605 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2608 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2613 ret
= _wfsopen(pathW
, modeW
, share
);
2620 /*********************************************************************
2623 FILE * CDECL
fopen(const char *path
, const char *mode
)
2625 return _fsopen( path
, mode
, _SH_DENYNO
);
2628 /*********************************************************************
2629 * fopen_s (MSVCRT.@)
2631 int CDECL
fopen_s(FILE** pFile
,
2632 const char *filename
, const char *mode
)
2634 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2635 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2640 *pFile
= fopen(filename
, mode
);
2647 /*********************************************************************
2648 * _wfopen (MSVCRT.@)
2650 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2652 return _wfsopen( path
, mode
, _SH_DENYNO
);
2655 /*********************************************************************
2656 * _wfopen_s (MSVCRT.@)
2658 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
2659 const wchar_t *mode
)
2661 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2662 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2667 *pFile
= _wfopen(filename
, mode
);
2674 /* fputc calls _flsbuf which calls fputc */
2675 int CDECL
_flsbuf(int c
, FILE* file
);
2677 /*********************************************************************
2680 int CDECL
fputc(int c
, FILE* file
)
2690 res
= flush_buffer(file
);
2692 return res
? res
: c
;
2699 res
= _flsbuf(c
, file
);
2705 /*********************************************************************
2706 * _fputchar (MSVCRT.@)
2708 int CDECL
_fputchar(int c
)
2710 return fputc(c
, stdout
);
2713 /*********************************************************************
2716 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2718 size_t rcnt
=size
* nmemb
;
2727 /* first buffered data */
2729 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
2730 memcpy(ptr
, file
->_ptr
, pcnt
);
2735 ptr
= (char*)ptr
+ pcnt
;
2736 } else if(!(file
->_flag
& _IOREAD
)) {
2737 if(file
->_flag
& _IORW
) {
2738 file
->_flag
|= _IOREAD
;
2747 /* Fill the buffer on small reads.
2748 * TODO: Use a better buffering strategy.
2750 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2751 if (file
->_bufsiz
== 0) {
2754 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2755 file
->_ptr
= file
->_base
;
2756 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2757 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2758 if (i
> 0 && i
< file
->_cnt
) {
2759 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
2760 file
->_flag
&= ~_IOEOF
;
2763 memcpy(ptr
, file
->_ptr
, i
);
2768 i
= _read(file
->_file
,ptr
, rcnt
);
2772 ptr
= (char *)ptr
+i
;
2773 /* expose feof condition in the flags
2774 * MFC tests file->_flag for feof, and doesn't call feof())
2776 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
2777 file
->_flag
|= _IOEOF
;
2780 file
->_flag
|= _IOERR
;
2791 /*********************************************************************
2792 * _wfreopen (MSVCRT.@)
2795 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
2797 int open_flags
, stream_flags
, fd
;
2799 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2802 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2807 /* map mode string to open() flags. "man fopen" for possibilities. */
2808 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2812 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2815 else if (init_fp(file
, fd
, stream_flags
) == -1)
2818 WARN(":failed-last error (%d)\n",GetLastError());
2819 _dosmaperr(GetLastError());
2828 /*********************************************************************
2829 * freopen (MSVCRT.@)
2832 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
2835 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2837 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2838 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2844 ret
= _wfreopen(pathW
, modeW
, file
);
2851 /*********************************************************************
2852 * fsetpos (MSVCRT.@)
2854 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2859 /* Note that all this has been lifted 'as is' from fseek */
2860 if(file
->_flag
& _IOWRT
)
2863 /* Discard buffered input */
2865 file
->_ptr
= file
->_base
;
2867 /* Reset direction of i/o */
2868 if(file
->_flag
& _IORW
) {
2869 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2872 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2877 /*********************************************************************
2878 * _ftelli64 (MSVCRT.@)
2880 __int64 CDECL
_ftelli64(FILE* file
)
2882 /* TODO: just call fgetpos and return lower half of result */
2887 pos
= _telli64(file
->_file
);
2893 if( file
->_flag
& _IOWRT
) {
2894 off
= file
->_ptr
- file
->_base
;
2897 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2898 /* Black magic correction for CR removal */
2900 for (i
=0; i
<file
->_cnt
; i
++) {
2901 if (file
->_ptr
[i
] == '\n')
2904 /* Black magic when reading CR at buffer boundary*/
2905 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
2915 /*********************************************************************
2918 LONG CDECL
ftell(FILE* file
)
2920 return (LONG
)_ftelli64(file
);
2923 /*********************************************************************
2924 * fgetpos (MSVCRT.@)
2926 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2931 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
2937 if( file
->_flag
& _IOWRT
) {
2938 off
= file
->_ptr
- file
->_base
;
2941 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2942 /* Black magic correction for CR removal */
2944 for (i
=0; i
<file
->_cnt
; i
++) {
2945 if (file
->_ptr
[i
] == '\n')
2948 /* Black magic when reading CR at buffer boundary*/
2949 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
2959 /*********************************************************************
2962 int CDECL
fputs(const char *s
, FILE* file
)
2964 size_t i
, len
= strlen(s
);
2968 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2969 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2973 for (i
=0; i
<len
; i
++)
2974 if (fputc(s
[i
], file
) == EOF
) {
2983 /*********************************************************************
2986 int CDECL
fputws(const wchar_t *s
, FILE* file
)
2988 size_t i
, len
= strlenW(s
);
2992 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2993 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2997 for (i
=0; i
<len
; i
++) {
2998 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
2999 || fputwc(s
[i
], file
) == WEOF
) {
3009 /*********************************************************************
3010 * getchar (MSVCRT.@)
3012 int CDECL
getchar(void)
3014 return fgetc(stdin
);
3017 /*********************************************************************
3020 int CDECL
getc(FILE* file
)
3025 /*********************************************************************
3028 char * CDECL
gets(char *buf
)
3031 char * buf_start
= buf
;
3034 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3036 if(cc
!= '\r') *buf
++ = (char)cc
;
3040 TRACE("got '%s'\n", buf_start
);
3041 _unlock_file(stdin
);
3045 /*********************************************************************
3048 wchar_t* CDECL
_getws(wchar_t* buf
)
3054 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3058 *buf
++ = (wchar_t)cc
;
3062 TRACE("got %s\n", debugstr_w(ws
));
3063 _unlock_file(stdin
);
3067 /*********************************************************************
3070 int CDECL
putc(int c
, FILE* file
)
3072 return fputc(c
, file
);
3075 /*********************************************************************
3076 * putchar (MSVCRT.@)
3078 int CDECL
putchar(int c
)
3080 return fputc(c
, stdout
);
3083 /*********************************************************************
3084 * _putwch (MSVCRT.@)
3086 wint_t CDECL
_putwch(wchar_t c
)
3088 return fputwc(c
, stdout
);
3091 /*********************************************************************
3094 int CDECL
puts(const char *s
)
3096 size_t len
= strlen(s
);
3100 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3101 _unlock_file(stdout
);
3105 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3106 _unlock_file(stdout
);
3110 /*********************************************************************
3113 int CDECL
_putws(const wchar_t *s
)
3115 static const wchar_t nl
= '\n';
3116 size_t len
= strlenW(s
);
3120 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3121 _unlock_file(stdout
);
3125 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3126 _unlock_file(stdout
);
3130 /*********************************************************************
3133 int CDECL
remove(const char *path
)
3135 TRACE("(%s)\n",path
);
3136 if (DeleteFileA(path
))
3138 TRACE(":failed (%d)\n",GetLastError());
3139 _dosmaperr(GetLastError());
3143 /*********************************************************************
3144 * _wremove (MSVCRT.@)
3146 int CDECL
_wremove(const wchar_t *path
)
3148 TRACE("(%s)\n",debugstr_w(path
));
3149 if (DeleteFileW(path
))
3151 TRACE(":failed (%d)\n",GetLastError());
3152 _dosmaperr(GetLastError());
3156 /*********************************************************************
3159 int CDECL
rename(const char *oldpath
,const char *newpath
)
3161 TRACE(":from %s to %s\n",oldpath
,newpath
);
3162 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3164 TRACE(":failed (%d)\n",GetLastError());
3165 _dosmaperr(GetLastError());
3169 /*********************************************************************
3170 * _wrename (MSVCRT.@)
3172 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3174 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3175 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3177 TRACE(":failed (%d)\n",GetLastError());
3178 _dosmaperr(GetLastError());
3182 /*********************************************************************
3183 * setvbuf (MSVCRT.@)
3185 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3193 if(mode
== _IOFBF
) {
3194 file
->_flag
&= ~_IONBF
;
3195 file
->_base
= file
->_ptr
= buf
;
3197 file
->_bufsiz
= size
;
3200 file
->_flag
|= _IONBF
;
3206 /*********************************************************************
3209 void CDECL
setbuf(FILE* file
, char *buf
)
3211 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3214 /*********************************************************************
3217 char * CDECL
tmpnam(char *s
)
3224 thread_data_t
*data
= msvcrt_get_thread_data();
3226 if(!data
->tmpnam_buffer
)
3227 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3229 s
= data
->tmpnam_buffer
;
3232 int_to_base32(GetCurrentProcessId(), tmpstr
);
3233 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3234 for (count
= 0; count
< TMP_MAX
; count
++)
3236 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3237 memcpy(p
, tmpstr
, size
);
3238 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3239 GetLastError() == ERROR_FILE_NOT_FOUND
)
3245 /*********************************************************************
3246 * _wtmpnam (MSVCRT.@)
3248 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3250 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3255 thread_data_t
*data
= msvcrt_get_thread_data();
3257 if(!data
->wtmpnam_buffer
)
3258 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3260 s
= data
->wtmpnam_buffer
;
3263 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3264 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3265 for (count
= 0; count
< TMP_MAX
; count
++)
3267 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3268 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3269 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3270 GetLastError() == ERROR_FILE_NOT_FOUND
)
3276 /*********************************************************************
3277 * tmpfile (MSVCRT.@)
3279 FILE* CDECL
tmpfile(void)
3281 char *filename
= tmpnam(NULL
);
3286 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
3287 if (fd
!= -1 && (file
= alloc_fp()))
3289 if (init_fp(file
, fd
, _O_RDWR
) == -1)
3294 else file
->_tmpfname
= _strdup(filename
);
3300 /*********************************************************************
3303 int CDECL
ungetc(int c
, FILE * file
)
3309 if(file
->_bufsiz
== 0) {
3313 if(file
->_ptr
>file
->_base
) {
3326 /*********************************************************************
3327 * ungetwc (MSVCRT.@)
3329 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3332 char * pp
= (char *)&mwc
;
3336 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3337 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3349 /*********************************************************************
3350 * _getmaxstdio (MSVCRT.@)
3352 int CDECL
_getmaxstdio(void)
3357 /*********************************************************************
3358 * _setmaxstdio (MSVCRT.@)
3360 int CDECL
_setmaxstdio(int newmax
)
3362 TRACE("%d\n", newmax
);
3364 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3367 max_streams
= newmax
;