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 /* INTERNAL: Flush all stream buffer */
738 static int flush_all_buffers(int mask
)
740 int i
, num_flushed
= 0;
744 for (i
= 3; i
< stream_idx
; i
++) {
749 if(file
->_flag
& mask
) {
757 TRACE(":flushed (%d) handles\n",num_flushed
);
761 /*********************************************************************
762 * _flushall (MSVCRT.@)
764 int CDECL
_flushall(void)
766 return flush_all_buffers(_IOWRT
| _IOREAD
);
769 /*********************************************************************
772 int CDECL
fflush(FILE* file
)
775 flush_all_buffers(_IOWRT
);
776 } else if(file
->_flag
& _IOWRT
) {
780 res
= flush_buffer(file
);
782 if(!res && (file->_flag & _IOCOMMIT))
783 res = _commit(file->_file) ? EOF : 0;
788 } else if(file
->_flag
& _IOREAD
) {
791 file
->_ptr
= file
->_base
;
799 /*********************************************************************
802 int CDECL
_close(int fd
)
809 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
810 if (hand
== INVALID_HANDLE_VALUE
)
812 else if (!CloseHandle(hand
))
814 WARN(":failed-last error (%d)\n",GetLastError());
815 _dosmaperr(GetLastError());
828 /*********************************************************************
831 int CDECL
_commit(int fd
)
833 HANDLE hand
= fdtoh(fd
);
835 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
836 if (hand
== INVALID_HANDLE_VALUE
)
839 if (!FlushFileBuffers(hand
))
841 if (GetLastError() == ERROR_INVALID_HANDLE
)
843 /* FlushFileBuffers fails for console handles
844 * so we ignore this error.
848 TRACE(":failed-last error (%d)\n",GetLastError());
849 _dosmaperr(GetLastError());
856 /*********************************************************************
859 * MSDN isn't clear on this point, but the remarks for _pipe
860 * indicate file descriptors duplicated with _dup and _dup2 are always
863 int CDECL
_dup2(int od
, int nd
)
867 TRACE("(od=%d, nd=%d)\n", od
, nd
);
869 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
873 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
874 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
876 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
880 ret
= alloc_fd_from(handle
, wxflag
, nd
);
888 /* _dup2 returns 0, not nd, on success */
895 _dosmaperr(GetLastError());
907 /*********************************************************************
910 int CDECL
_dup(int od
)
916 if (_dup2(od
, fd
) == 0)
924 /*********************************************************************
927 int CDECL
_eof(int fd
)
930 LONG hcurpos
,hendpos
;
931 HANDLE hand
= fdtoh(fd
);
933 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
935 if (hand
== INVALID_HANDLE_VALUE
)
938 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
940 /* Otherwise we do it the hard way */
941 hcurpos
= hendpos
= 0;
942 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
943 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
945 if (curpos
== endpos
&& hcurpos
== hendpos
)
947 /* FIXME: shouldn't WX_ATEOF be set here? */
951 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
955 /*********************************************************************
956 * _fcloseall (MSVCRT.@)
958 int CDECL
_fcloseall(void)
960 int num_closed
= 0, i
;
964 for (i
= 3; i
< stream_idx
; i
++) {
967 if (file
->_flag
&& !fclose(file
))
972 TRACE(":closed (%d) handles\n",num_closed
);
976 /* free everything on process exit */
977 void msvcrt_free_io(void)
982 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
983 * stdout, and stderr (unlike GNU), so we need to fclose() them here
984 * or they won't get flushed.
990 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
993 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
996 file_cs
.DebugInfo
->Spare
[0] = 0;
997 DeleteCriticalSection(&file_cs
);
1000 /*********************************************************************
1001 * _lseeki64 (MSVCRT.@)
1003 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1005 HANDLE hand
= fdtoh(fd
);
1008 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1009 if (hand
== INVALID_HANDLE_VALUE
)
1012 if (whence
< 0 || whence
> 2)
1018 TRACE(":fd (%d) to %s pos %s\n",
1019 fd
,wine_dbgstr_longlong(offset
),
1020 (whence
==SEEK_SET
)?"SEEK_SET":
1021 (whence
==SEEK_CUR
)?"SEEK_CUR":
1022 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1024 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1025 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1026 ofs
.QuadPart
= offset
;
1027 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1028 GetLastError() == ERROR_SUCCESS
)
1030 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1031 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1033 return ofs
.QuadPart
;
1035 TRACE(":error-last error (%d)\n",GetLastError());
1036 _dosmaperr(GetLastError());
1040 /*********************************************************************
1043 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1045 return (LONG
)_lseeki64(fd
, offset
, whence
);
1048 /*********************************************************************
1049 * _lock_file (MSVCRT.@)
1051 void CDECL
_lock_file(FILE *file
)
1053 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1054 _lock(_STREAM_LOCKS
+(file
-_iob
));
1055 /* ReactOS: string streams dont need to be locked */
1056 else if(!(file
->_flag
& _IOSTRG
))
1057 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1060 /*********************************************************************
1061 * _unlock_file (MSVCRT.@)
1063 void CDECL
_unlock_file(FILE *file
)
1065 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1066 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1067 /* ReactOS: string streams dont need to be locked */
1068 else if(!(file
->_flag
& _IOSTRG
))
1069 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1073 /*********************************************************************
1074 * _locking (MSVCRT.@)
1076 * This is untested; the underlying LockFile doesn't work yet.
1078 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1082 HANDLE hand
= fdtoh(fd
);
1084 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1085 if (hand
== INVALID_HANDLE_VALUE
)
1088 if (mode
< 0 || mode
> 4)
1094 TRACE(":fd (%d) by 0x%08x mode %s\n",
1095 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1096 (mode
==_LK_LOCK
)?"_LK_LOCK":
1097 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1098 (mode
==_LK_RLCK
)?"_LK_RLCK":
1099 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1102 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1104 FIXME ("Seek failed\n");
1105 *_errno() = EINVAL
; /* FIXME */
1108 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1111 ret
= 1; /* just to satisfy gcc */
1114 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1119 else if (mode
== _LK_UNLCK
)
1120 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1122 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1123 /* FIXME - what about error settings? */
1124 return ret
? 0 : -1;
1127 /*********************************************************************
1128 * _fseeki64 (MSVCRT.@)
1130 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1135 /* Flush output if needed */
1136 if(file
->_flag
& _IOWRT
)
1139 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1140 offset
-= file
->_cnt
;
1141 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1142 /* Black magic correction for CR removal */
1144 for (i
=0; i
<file
->_cnt
; i
++) {
1145 if (file
->_ptr
[i
] == '\n')
1148 /* Black magic when reading CR at buffer boundary*/
1149 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1153 /* Discard buffered input */
1155 file
->_ptr
= file
->_base
;
1156 /* Reset direction of i/o */
1157 if(file
->_flag
& _IORW
) {
1158 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1160 /* Clear end of file flag */
1161 file
->_flag
&= ~_IOEOF
;
1162 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1168 /*********************************************************************
1171 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1173 return _fseeki64( file
, offset
, whence
);
1176 /*********************************************************************
1177 * _chsize (MSVCRT.@)
1179 int CDECL
_chsize(int fd
, long size
)
1185 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1190 if (handle
!= INVALID_HANDLE_VALUE
)
1192 /* save the current file pointer */
1193 cur
= _lseek(fd
, 0, SEEK_CUR
);
1196 pos
= _lseek(fd
, size
, SEEK_SET
);
1199 ret
= SetEndOfFile(handle
);
1200 if (!ret
) _dosmaperr(GetLastError());
1203 /* restore the file pointer */
1204 _lseek(fd
, cur
, SEEK_SET
);
1209 return ret
? 0 : -1;
1212 /*********************************************************************
1213 * clearerr (MSVCRT.@)
1215 void CDECL
clearerr(FILE* file
)
1217 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1220 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1224 /*********************************************************************
1227 void CDECL
rewind(FILE* file
)
1229 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1232 fseek(file
, 0L, SEEK_SET
);
1237 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1239 int plus
= strchrW(mode
, '+') != NULL
;
1244 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1245 *stream_flags
= plus
? _IORW
: _IOREAD
;
1248 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1249 *stream_flags
= plus
? _IORW
: _IOWRT
;
1252 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1253 *stream_flags
= plus
? _IORW
: _IOWRT
;
1256 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1265 *open_flags
|= _O_BINARY
;
1266 *open_flags
&= ~_O_TEXT
;
1269 *open_flags
|= _O_TEXT
;
1270 *open_flags
&= ~_O_BINARY
;
1276 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1281 /*********************************************************************
1282 * _fdopen (MSVCRT.@)
1284 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1287 wchar_t *modeW
= NULL
;
1289 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1291 ret
= _wfdopen(fd
, modeW
);
1297 /*********************************************************************
1298 * _wfdopen (MSVCRT.@)
1300 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1302 int open_flags
, stream_flags
;
1305 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1308 if (!(file
= alloc_fp()))
1310 else if (init_fp(file
, fd
, stream_flags
) == -1)
1315 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1321 /*********************************************************************
1322 * _filelength (MSVCRT.@)
1324 LONG CDECL
_filelength(int fd
)
1326 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1329 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1332 if (endPos
!= curPos
)
1333 _lseek(fd
, curPos
, SEEK_SET
);
1340 /*********************************************************************
1341 * _filelengthi64 (MSVCRT.@)
1343 __int64 CDECL
_filelengthi64(int fd
)
1345 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1348 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1351 if (endPos
!= curPos
)
1352 _lseeki64(fd
, curPos
, SEEK_SET
);
1359 /*********************************************************************
1360 * _fileno (MSVCRT.@)
1362 int CDECL
_fileno(FILE* file
)
1364 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1368 /*********************************************************************
1369 * _get_osfhandle (MSVCRT.@)
1371 intptr_t CDECL
_get_osfhandle(int fd
)
1373 HANDLE hand
= fdtoh(fd
);
1374 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1376 return (intptr_t)hand
;
1379 /*********************************************************************
1380 * _isatty (MSVCRT.@)
1382 int CDECL
_isatty(int fd
)
1384 HANDLE hand
= fdtoh(fd
);
1386 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1387 if (hand
== INVALID_HANDLE_VALUE
)
1390 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1393 /*********************************************************************
1394 * _mktemp (MSVCRT.@)
1396 char * CDECL
_mktemp(char *pattern
)
1399 char *retVal
= pattern
;
1404 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1408 id
= GetCurrentProcessId();
1412 int tempNum
= id
/ 10;
1413 *pattern
-- = id
- (tempNum
* 10) + '0';
1419 *pattern
= letter
++;
1420 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1421 GetLastError() == ERROR_FILE_NOT_FOUND
)
1423 } while(letter
<= 'z');
1427 /*********************************************************************
1428 * _wmktemp (MSVCRT.@)
1430 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1433 wchar_t *retVal
= pattern
;
1435 wchar_t letter
= 'a';
1438 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1442 id
= GetCurrentProcessId();
1446 int tempNum
= id
/ 10;
1447 *pattern
-- = id
- (tempNum
* 10) + '0';
1453 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1454 GetLastError() == ERROR_FILE_NOT_FOUND
)
1456 *pattern
= letter
++;
1457 } while(letter
!= '|');
1461 /*static*/ unsigned split_oflags(unsigned oflags
)
1464 unsigned unsupp
; /* until we support everything */
1466 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1467 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1468 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1469 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1470 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1471 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1473 if ((unsupp
= oflags
& ~(
1474 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1475 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1476 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1478 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1480 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1485 /*********************************************************************
1488 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1491 SECURITY_ATTRIBUTES sa
;
1492 HANDLE readHandle
, writeHandle
;
1500 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1501 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1502 sa
.lpSecurityDescriptor
= NULL
;
1503 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1505 unsigned int wxflags
= split_oflags(textmode
);
1509 fd
= alloc_fd(readHandle
, wxflags
);
1513 fd
= alloc_fd(writeHandle
, wxflags
);
1522 CloseHandle(writeHandle
);
1528 CloseHandle(readHandle
);
1529 CloseHandle(writeHandle
);
1535 _dosmaperr(GetLastError());
1540 /*********************************************************************
1541 * _sopen_s (MSVCRT.@)
1543 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1545 DWORD access
= 0, creation
= 0, attrib
;
1549 SECURITY_ATTRIBUTES sa
;
1551 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1552 fd
, path
, oflags
, shflags
, pmode
);
1556 MSVCRT_INVALID_PMT("null out fd pointer");
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");
1681 wxflag
= split_oflags(oflags
);
1682 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1684 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1685 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1686 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1689 if (oflags
& _O_CREAT
)
1691 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1692 FIXME(": pmode 0x%04x ignored\n", pmode
);
1694 WARN(": pmode 0x%04x ignored\n", pmode
);
1696 if (oflags
& _O_EXCL
)
1697 creation
= CREATE_NEW
;
1698 else if (oflags
& _O_TRUNC
)
1699 creation
= CREATE_ALWAYS
;
1701 creation
= OPEN_ALWAYS
;
1703 else /* no _O_CREAT */
1705 if (oflags
& _O_TRUNC
)
1706 creation
= TRUNCATE_EXISTING
;
1708 creation
= OPEN_EXISTING
;
1717 sharing
= FILE_SHARE_READ
;
1720 sharing
= FILE_SHARE_WRITE
;
1723 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1726 ERR( "Unhandled shflags 0x%x\n", shflags
);
1729 attrib
= FILE_ATTRIBUTE_NORMAL
;
1731 if (oflags
& _O_TEMPORARY
)
1733 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1735 sharing
|= FILE_SHARE_DELETE
;
1738 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1739 sa
.lpSecurityDescriptor
= NULL
;
1740 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1742 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1744 if (hand
== INVALID_HANDLE_VALUE
) {
1745 WARN(":failed-last error (%d)\n",GetLastError());
1746 _dosmaperr(GetLastError());
1750 *fd
= alloc_fd(hand
, wxflag
);
1752 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1756 /*********************************************************************
1757 * _wsopen (MSVCRT.@)
1759 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1764 if (oflags
& _O_CREAT
)
1768 va_start(ap
, shflags
);
1769 pmode
= va_arg(ap
, int);
1775 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1779 /*********************************************************************
1782 int CDECL
_open( const char *path
, int flags
, ... )
1786 if (flags
& _O_CREAT
)
1789 va_start(ap
, flags
);
1790 pmode
= va_arg(ap
, int);
1792 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1795 return _sopen( path
, flags
, _SH_DENYNO
);
1798 /*********************************************************************
1801 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1805 if (flags
& _O_CREAT
)
1808 va_start(ap
, flags
);
1809 pmode
= va_arg(ap
, int);
1811 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1814 return _wsopen( path
, flags
, _SH_DENYNO
);
1817 /*********************************************************************
1820 int CDECL
_creat(const char *path
, int flags
)
1822 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1823 return _open(path
, usedFlags
);
1826 /*********************************************************************
1827 * _wcreat (MSVCRT.@)
1829 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1831 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1832 return _wopen(path
, usedFlags
);
1835 /*********************************************************************
1836 * _open_osfhandle (MSVCRT.@)
1838 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1842 /* _O_RDONLY (0) always matches, so set the read flag
1843 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1844 * file, so set the write flag. It also only sets _O_TEXT if it wants
1845 * text - it never sets _O_BINARY.
1847 /* don't let split_oflags() decide the mode if no mode is passed */
1848 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1849 oflags
|= _O_BINARY
;
1851 fd
= alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1852 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1856 /*********************************************************************
1859 int CDECL
_rmtmp(void)
1861 int num_removed
= 0, i
;
1865 for (i
= 3; i
< stream_idx
; i
++) {
1868 if (file
->_tmpfname
)
1877 TRACE(":removed (%d) temp files\n",num_removed
);
1881 /*********************************************************************
1884 * When reading \r as last character in text mode, read() positions
1885 * the file pointer on the \r character while getc() goes on to
1888 static int read_i(int fd
, void *buf
, unsigned int count
)
1891 char *bufstart
= buf
;
1892 HANDLE hand
= fdtoh(fd
);
1893 ioinfo
*fdinfo
= get_ioinfo(fd
);
1898 if (fdinfo
->wxflag
& WX_READEOF
) {
1899 fdinfo
->wxflag
|= WX_ATEOF
;
1900 TRACE("already at EOF, returning 0\n");
1903 /* Don't trace small reads, it gets *very* annoying */
1905 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1906 if (hand
== INVALID_HANDLE_VALUE
)
1909 /* Reading single bytes in O_TEXT mode makes things slow
1910 * So read big chunks
1912 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1914 if (count
!= 0 && num_read
== 0)
1916 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1917 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1919 else if (fdinfo
->wxflag
& WX_TEXT
)
1922 if (bufstart
[num_read
-1] == '\r')
1926 fdinfo
->wxflag
&= ~WX_READCR
;
1927 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1931 fdinfo
->wxflag
|= WX_READCR
;
1936 fdinfo
->wxflag
&= ~WX_READCR
;
1937 for (i
=0, j
=0; i
<num_read
; i
++)
1939 /* in text mode, a ctrl-z signals EOF */
1940 if (bufstart
[i
] == 0x1a)
1942 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1943 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1946 /* in text mode, strip \r if followed by \n.
1947 * BUG: should save state across calls somehow, so CR LF that
1948 * straddles buffer boundary gets recognized properly?
1950 if ((bufstart
[i
] != '\r')
1951 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1952 bufstart
[j
++] = bufstart
[i
];
1959 if (GetLastError() == ERROR_BROKEN_PIPE
)
1961 TRACE(":end-of-pipe\n");
1962 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1967 TRACE(":failed-last error (%d)\n",GetLastError());
1973 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1977 /*********************************************************************
1980 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
1983 num_read
= read_i(fd
, buf
, count
);
1987 /*********************************************************************
1988 * _setmode (MSVCRT.@)
1990 int CDECL
_setmode(int fd
,int mode
)
1992 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
1993 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1994 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1995 if ((mode
& _O_TEXT
) == _O_TEXT
)
1996 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
1998 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2002 /*********************************************************************
2005 long CDECL
_tell(int fd
)
2007 return _lseek(fd
, 0, SEEK_CUR
);
2010 /*********************************************************************
2011 * _telli64 (MSVCRT.@)
2013 __int64 CDECL
_telli64(int fd
)
2015 return _lseeki64(fd
, 0, SEEK_CUR
);
2018 /*********************************************************************
2019 * _tempnam (MSVCRT.@)
2021 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2023 char tmpbuf
[MAX_PATH
];
2024 const char *tmp_dir
= getenv("TMP");
2026 if (tmp_dir
) dir
= tmp_dir
;
2028 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2029 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2031 TRACE("got name (%s)\n",tmpbuf
);
2032 DeleteFileA(tmpbuf
);
2033 return _strdup(tmpbuf
);
2035 TRACE("failed (%d)\n",GetLastError());
2039 /*********************************************************************
2040 * _wtempnam (MSVCRT.@)
2042 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2044 wchar_t tmpbuf
[MAX_PATH
];
2046 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2047 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2049 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2050 DeleteFileW(tmpbuf
);
2051 return _wcsdup(tmpbuf
);
2053 TRACE("failed (%d)\n",GetLastError());
2057 /*********************************************************************
2060 int CDECL
_umask(int umask
)
2062 int old_umask
= umask
;
2063 TRACE("(%d)\n",umask
);
2064 MSVCRT_umask
= umask
;
2068 /*********************************************************************
2071 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2074 HANDLE hand
= fdtoh(fd
);
2076 /* Don't trace small writes, it gets *very* annoying */
2079 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2081 if (hand
== INVALID_HANDLE_VALUE
)
2087 /* If appending, go to EOF */
2088 if (get_ioinfo(fd
)->wxflag
& WX_APPEND
)
2089 _lseek(fd
, 0, FILE_END
);
2091 if (!(get_ioinfo(fd
)->wxflag
& WX_TEXT
))
2093 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2094 && (num_written
== count
))
2096 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2097 hand
, GetLastError());
2102 unsigned int i
, j
, nr_lf
;
2105 const char *s
= buf
, *buf_start
= buf
;
2106 /* find number of \n ( without preceding \r ) */
2107 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2112 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2117 if ((q
= p
= malloc(count
+ nr_lf
)))
2119 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2124 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2131 FIXME("Malloc failed\n");
2139 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2141 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2142 fd
, hand
, GetLastError(), num_written
);
2146 return s
- buf_start
;
2158 /*********************************************************************
2161 int CDECL
_putw(int val
, FILE* file
)
2166 len
= _write(file
->_file
, &val
, sizeof(val
));
2167 if (len
== sizeof(val
)) {
2172 file
->_flag
|= _IOERR
;
2177 /*********************************************************************
2180 int CDECL
fclose(FILE* file
)
2186 free(file
->_tmpfname
);
2187 file
->_tmpfname
= NULL
;
2188 /* flush stdio buffers */
2189 if(file
->_flag
& _IOWRT
)
2191 if(file
->_flag
& _IOMYBUF
)
2194 r
=_close(file
->_file
);
2198 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2199 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2201 if(file
== get_file(stream_idx
-1)) {
2202 while(stream_idx
>3 && !file
->_flag
) {
2204 file
= get_file(stream_idx
-1);
2208 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2211 /*********************************************************************
2214 int CDECL
feof(FILE* file
)
2219 ret
= file
->_flag
& _IOEOF
;
2225 /*********************************************************************
2228 int CDECL
ferror(FILE* file
)
2233 ret
= file
->_flag
& _IOERR
;
2239 /*********************************************************************
2240 * _filbuf (MSVCRT.@)
2242 int CDECL
_filbuf(FILE* file
)
2247 /* Allocate buffer if needed */
2248 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2251 if(!(file
->_flag
& _IOREAD
)) {
2252 if(file
->_flag
& _IORW
)
2253 file
->_flag
|= _IOREAD
;
2260 if(file
->_flag
& _IONBF
) {
2262 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2263 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2271 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2273 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2280 file
->_ptr
= file
->_base
+1;
2281 c
= *(unsigned char *)file
->_base
;
2287 /*********************************************************************
2290 int CDECL
fgetc(FILE* file
)
2298 i
= (unsigned char *)file
->_ptr
++;
2307 /*********************************************************************
2308 * _fgetchar (MSVCRT.@)
2310 int CDECL
_fgetchar(void)
2312 return fgetc(stdin
);
2315 /*********************************************************************
2318 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2321 char * buf_start
= s
;
2323 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2324 file
,file
->_file
,s
,size
);
2328 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2333 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2335 TRACE(":nothing read\n");
2339 if ((cc
!= EOF
) && (size
> 1))
2342 TRACE(":got %s\n", debugstr_a(buf_start
));
2347 /*********************************************************************
2350 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2351 * the CR from CR/LF combinations
2353 wint_t CDECL
fgetwc(FILE* file
)
2358 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2365 for(i
=0; i
<sizeof(wc
); i
++)
2378 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2393 if ((__mb_cur_max
> 1) && isleadbyte(c
))
2395 FIXME("Treat Multibyte characters\n");
2405 /*********************************************************************
2408 int CDECL
_getw(FILE* file
)
2416 for (j
=0; j
<sizeof(int); j
++) {
2419 file
->_flag
|= _IOEOF
;
2430 /*********************************************************************
2433 wint_t CDECL
getwc(FILE* file
)
2435 return fgetwc(file
);
2438 /*********************************************************************
2439 * _fgetwchar (MSVCRT.@)
2441 wint_t CDECL
_fgetwchar(void)
2443 return fgetwc(stdin
);
2446 /*********************************************************************
2447 * getwchar (MSVCRT.@)
2449 wint_t CDECL
getwchar(void)
2451 return _fgetwchar();
2454 /*********************************************************************
2457 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2460 wchar_t * buf_start
= s
;
2462 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2463 file
,file
->_file
,s
,size
);
2467 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2472 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2474 TRACE(":nothing read\n");
2478 if ((cc
!= WEOF
) && (size
> 1))
2481 TRACE(":got %s\n", debugstr_w(buf_start
));
2486 /*********************************************************************
2489 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2491 size_t wrcnt
=size
* nmemb
;
2498 int pcnt
=((unsigned)file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2499 memcpy(file
->_ptr
, ptr
, pcnt
);
2504 ptr
= (const char*)ptr
+ pcnt
;
2505 } else if(!(file
->_flag
& _IOWRT
)) {
2506 if(file
->_flag
& _IORW
) {
2507 file
->_flag
|= _IOWRT
;
2515 int res
=flush_buffer(file
);
2517 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2520 file
->_flag
|= _IOERR
;
2523 written
+= pwritten
;
2528 return written
/ size
;
2531 /*********************************************************************
2533 * FORKED for ReactOS, don't sync with Wine!
2535 * - http://jira.reactos.org/browse/CORE-6495
2536 * - http://bugs.winehq.org/show_bug.cgi?id=8598
2538 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2540 /* If this is a real file stream (and not some temporary one for
2541 sprintf-like functions), check whether it is opened in text mode.
2542 In this case, we have to perform an implicit conversion to ANSI. */
2543 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo(stream
->_file
)->wxflag
& WX_TEXT
)
2545 /* Convert to multibyte in text mode */
2546 char mbc
[MB_LEN_MAX
];
2549 mb_return
= wctomb(mbc
, c
);
2554 /* Output all characters */
2555 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2560 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2567 /*********************************************************************
2568 * _fputwchar (MSVCRT.@)
2570 wint_t CDECL
_fputwchar(wint_t wc
)
2572 return fputwc(wc
, stdout
);
2575 /*********************************************************************
2576 * _wfsopen (MSVCRT.@)
2578 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2581 int open_flags
, stream_flags
, fd
;
2583 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2585 /* map mode string to open() flags. "man fopen" for possibilities. */
2586 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2590 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2593 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2595 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2602 TRACE(":got (%p)\n",file
);
2603 if (fd
>= 0 && !file
)
2609 /*********************************************************************
2610 * _fsopen (MSVCRT.@)
2612 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2615 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2617 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2618 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2622 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2625 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2630 ret
= _wfsopen(pathW
, modeW
, share
);
2637 /*********************************************************************
2640 FILE * CDECL
fopen(const char *path
, const char *mode
)
2642 return _fsopen( path
, mode
, _SH_DENYNO
);
2645 /*********************************************************************
2646 * fopen_s (MSVCRT.@)
2648 int CDECL
fopen_s(FILE** pFile
,
2649 const char *filename
, const char *mode
)
2651 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2652 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2657 *pFile
= fopen(filename
, mode
);
2664 /*********************************************************************
2665 * _wfopen (MSVCRT.@)
2667 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2669 return _wfsopen( path
, mode
, _SH_DENYNO
);
2672 /*********************************************************************
2673 * _wfopen_s (MSVCRT.@)
2675 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
2676 const wchar_t *mode
)
2678 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2679 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2684 *pFile
= _wfopen(filename
, mode
);
2691 /* fputc calls _flsbuf which calls fputc */
2692 int CDECL
_flsbuf(int c
, FILE* file
);
2694 /*********************************************************************
2697 int CDECL
fputc(int c
, FILE* file
)
2707 res
= flush_buffer(file
);
2709 return res
? res
: c
;
2716 res
= _flsbuf(c
, file
);
2722 /*********************************************************************
2723 * _fputchar (MSVCRT.@)
2725 int CDECL
_fputchar(int c
)
2727 return fputc(c
, stdout
);
2730 /*********************************************************************
2733 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2735 size_t rcnt
=size
* nmemb
;
2744 /* first buffered data */
2746 int pcnt
= (rcnt
>(unsigned int)file
->_cnt
)? file
->_cnt
:rcnt
;
2747 memcpy(ptr
, file
->_ptr
, pcnt
);
2752 ptr
= (char*)ptr
+ pcnt
;
2753 } else if(!(file
->_flag
& _IOREAD
)) {
2754 if(file
->_flag
& _IORW
) {
2755 file
->_flag
|= _IOREAD
;
2764 /* Fill the buffer on small reads.
2765 * TODO: Use a better buffering strategy.
2767 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2768 if (file
->_bufsiz
== 0) {
2771 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2772 file
->_ptr
= file
->_base
;
2773 i
= ((unsigned int)file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2774 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2775 if (i
> 0 && i
< file
->_cnt
) {
2776 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
2777 file
->_flag
&= ~_IOEOF
;
2780 memcpy(ptr
, file
->_ptr
, i
);
2785 i
= _read(file
->_file
,ptr
, rcnt
);
2789 ptr
= (char *)ptr
+i
;
2790 /* expose feof condition in the flags
2791 * MFC tests file->_flag for feof, and doesn't call feof())
2793 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
2794 file
->_flag
|= _IOEOF
;
2797 file
->_flag
|= _IOERR
;
2808 /*********************************************************************
2809 * _wfreopen (MSVCRT.@)
2812 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
2814 int open_flags
, stream_flags
, fd
;
2816 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2819 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2824 /* map mode string to open() flags. "man fopen" for possibilities. */
2825 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2829 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2832 else if (init_fp(file
, fd
, stream_flags
) == -1)
2835 WARN(":failed-last error (%d)\n",GetLastError());
2836 _dosmaperr(GetLastError());
2845 /*********************************************************************
2846 * freopen (MSVCRT.@)
2849 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
2852 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2854 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2855 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2861 ret
= _wfreopen(pathW
, modeW
, file
);
2868 /*********************************************************************
2869 * fsetpos (MSVCRT.@)
2871 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2876 /* Note that all this has been lifted 'as is' from fseek */
2877 if(file
->_flag
& _IOWRT
)
2880 /* Discard buffered input */
2882 file
->_ptr
= file
->_base
;
2884 /* Reset direction of i/o */
2885 if(file
->_flag
& _IORW
) {
2886 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2889 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2894 /*********************************************************************
2895 * _ftelli64 (MSVCRT.@)
2897 __int64 CDECL
_ftelli64(FILE* file
)
2899 /* TODO: just call fgetpos and return lower half of result */
2904 pos
= _telli64(file
->_file
);
2910 if( file
->_flag
& _IOWRT
) {
2911 off
= file
->_ptr
- file
->_base
;
2914 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2915 /* Black magic correction for CR removal */
2917 for (i
=0; i
<file
->_cnt
; i
++) {
2918 if (file
->_ptr
[i
] == '\n')
2921 /* Black magic when reading CR at buffer boundary*/
2922 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
2932 /*********************************************************************
2935 LONG CDECL
ftell(FILE* file
)
2937 return (LONG
)_ftelli64(file
);
2940 /*********************************************************************
2941 * fgetpos (MSVCRT.@)
2943 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2948 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
2954 if( file
->_flag
& _IOWRT
) {
2955 off
= file
->_ptr
- file
->_base
;
2958 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2959 /* Black magic correction for CR removal */
2961 for (i
=0; i
<file
->_cnt
; i
++) {
2962 if (file
->_ptr
[i
] == '\n')
2965 /* Black magic when reading CR at buffer boundary*/
2966 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
2976 /*********************************************************************
2979 int CDECL
fputs(const char *s
, FILE* file
)
2981 size_t i
, len
= strlen(s
);
2985 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2986 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2990 for (i
=0; i
<len
; i
++)
2991 if (fputc(s
[i
], file
) == EOF
) {
3000 /*********************************************************************
3003 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3005 size_t i
, len
= strlenW(s
);
3009 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3010 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3014 for (i
=0; i
<len
; i
++) {
3015 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
3016 || fputwc(s
[i
], file
) == WEOF
) {
3026 /*********************************************************************
3027 * getchar (MSVCRT.@)
3029 int CDECL
getchar(void)
3031 return fgetc(stdin
);
3034 /*********************************************************************
3037 int CDECL
getc(FILE* file
)
3042 /*********************************************************************
3045 char * CDECL
gets(char *buf
)
3048 char * buf_start
= buf
;
3051 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3053 if(cc
!= '\r') *buf
++ = (char)cc
;
3057 TRACE("got '%s'\n", buf_start
);
3058 _unlock_file(stdin
);
3062 /*********************************************************************
3065 wchar_t* CDECL
_getws(wchar_t* buf
)
3071 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3075 *buf
++ = (wchar_t)cc
;
3079 TRACE("got %s\n", debugstr_w(ws
));
3080 _unlock_file(stdin
);
3084 /*********************************************************************
3087 int CDECL
putc(int c
, FILE* file
)
3089 return fputc(c
, file
);
3092 /*********************************************************************
3093 * putchar (MSVCRT.@)
3095 int CDECL
putchar(int c
)
3097 return fputc(c
, stdout
);
3100 /*********************************************************************
3101 * _putwch (MSVCRT.@)
3103 wint_t CDECL
_putwch(wchar_t c
)
3105 return fputwc(c
, stdout
);
3108 /*********************************************************************
3111 int CDECL
puts(const char *s
)
3113 size_t len
= strlen(s
);
3117 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3118 _unlock_file(stdout
);
3122 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3123 _unlock_file(stdout
);
3127 /*********************************************************************
3130 int CDECL
_putws(const wchar_t *s
)
3132 static const wchar_t nl
= '\n';
3133 size_t len
= strlenW(s
);
3137 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3138 _unlock_file(stdout
);
3142 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3143 _unlock_file(stdout
);
3147 /*********************************************************************
3150 int CDECL
remove(const char *path
)
3152 TRACE("(%s)\n",path
);
3153 if (DeleteFileA(path
))
3155 TRACE(":failed (%d)\n",GetLastError());
3156 _dosmaperr(GetLastError());
3160 /*********************************************************************
3161 * _wremove (MSVCRT.@)
3163 int CDECL
_wremove(const wchar_t *path
)
3165 TRACE("(%s)\n",debugstr_w(path
));
3166 if (DeleteFileW(path
))
3168 TRACE(":failed (%d)\n",GetLastError());
3169 _dosmaperr(GetLastError());
3173 /*********************************************************************
3176 int CDECL
rename(const char *oldpath
,const char *newpath
)
3178 TRACE(":from %s to %s\n",oldpath
,newpath
);
3179 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3181 TRACE(":failed (%d)\n",GetLastError());
3182 _dosmaperr(GetLastError());
3186 /*********************************************************************
3187 * _wrename (MSVCRT.@)
3189 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3191 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3192 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3194 TRACE(":failed (%d)\n",GetLastError());
3195 _dosmaperr(GetLastError());
3199 /*********************************************************************
3200 * setvbuf (MSVCRT.@)
3202 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3210 if(mode
== _IOFBF
) {
3211 file
->_flag
&= ~_IONBF
;
3212 file
->_base
= file
->_ptr
= buf
;
3214 file
->_bufsiz
= size
;
3217 file
->_flag
|= _IONBF
;
3223 /*********************************************************************
3226 void CDECL
setbuf(FILE* file
, char *buf
)
3228 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3231 /*********************************************************************
3234 char * CDECL
tmpnam(char *s
)