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)
11 * msvcrt.dll file functions
13 * Copyright 1996,1998 Marcus Meissner
14 * Copyright 1996 Jukka Iivonen
15 * Copyright 1997,2000 Uwe Bonnes
16 * Copyright 2000 Jon Griffiths
17 * Copyright 2004 Eric Pouech
18 * Copyright 2004 Juan Lang
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
39 #include "wine/unicode.h"
41 #include <sys/utime.h>
44 int *__p__fmode(void);
45 int *__p___mb_cur_max(void);
72 /* _access() bit flags FIXME: incomplete */
73 /* defined in crt/io.h */
75 /* values for wxflag in file descriptor */
78 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
79 #define WX_READCR 0x08 /* underlying file is at \r */
80 #define WX_DONTINHERIT 0x10
81 #define WX_APPEND 0x20
84 /* FIXME: this should be allocated dynamically */
85 #define MAX_FILES 2048
86 #define FD_BLOCK_SIZE 64
93 CRITICAL_SECTION crit
;
96 /*********************************************************************
97 * __pioinfo (MSVCRT.@)
98 * array of pointers to ioinfo arrays [64]
100 ioinfo
* __pioinfo
[MAX_FILES
/FD_BLOCK_SIZE
] = { 0 };
102 /*********************************************************************
103 * __badioinfo (MSVCRT.@)
105 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
107 static int fdstart
= 3; /* first unallocated fd */
108 static int fdend
= 3; /* highest allocated fd */
112 CRITICAL_SECTION crit
;
115 FILE _iob
[3] = { { 0 } };
116 static file_crit
* fstream
[MAX_FILES
/FD_BLOCK_SIZE
];
117 static int max_streams
= 512, stream_idx
;
119 /* INTERNAL: process umask */
120 static int MSVCRT_umask
= 0;
122 /* INTERNAL: static data for tmpnam and _wtmpname functions */
123 static int tmpnam_unique
;
125 /* This critical section protects the tables __pioinfo and fstreams,
126 * and their related indexes, fdstart, fdend,
127 * and stream_idx, from race conditions.
128 * It doesn't protect against race conditions manipulating the underlying files
129 * or flags; doing so would probably be better accomplished with per-file
130 * protection, rather than locking the whole table for every change.
132 static CRITICAL_SECTION file_cs
;
133 #define LOCK_FILES() do { EnterCriticalSection(&file_cs); } while (0)
134 #define UNLOCK_FILES() do { LeaveCriticalSection(&file_cs); } while (0)
136 static inline ioinfo
* get_ioinfo(int fd
)
140 ret
= __pioinfo
[fd
/FD_BLOCK_SIZE
];
144 return ret
+ (fd
%FD_BLOCK_SIZE
);
147 static inline FILE* get_file(int i
)
157 ret
= fstream
[i
/FD_BLOCK_SIZE
];
159 fstream
[i
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(file_crit
));
160 if(!fstream
[i
/FD_BLOCK_SIZE
]) {
161 ERR("out of memory\n");
166 ret
= fstream
[i
/FD_BLOCK_SIZE
] + (i
%FD_BLOCK_SIZE
);
168 ret
+= i
%FD_BLOCK_SIZE
;
173 static inline BOOL
is_valid_fd(int fd
)
175 return fd
>= 0 && fd
< fdend
&& (get_ioinfo(fd
)->wxflag
& WX_OPEN
);
178 /* INTERNAL: Get the HANDLE for a fd
179 * This doesn't lock the table, because a failure will result in
180 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
181 * it returns a valid handle which is about to be closed, a subsequent call
182 * will fail, most likely in a sane way.
184 /*static*/ HANDLE
fdtoh(int fd
)
186 if (!is_valid_fd(fd
))
188 WARN(":fd (%d) - no handle!\n",fd
);
191 return INVALID_HANDLE_VALUE
;
193 //if (get_ioinfo(fd)->handle == INVALID_HANDLE_VALUE)
194 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
195 return get_ioinfo(fd
)->handle
;
198 /* INTERNAL: free a file entry fd */
199 static void free_fd(int fd
)
205 fdinfo
= get_ioinfo(fd
);
206 old_handle
= fdinfo
->handle
;
207 if(fdinfo
!= &__badioinfo
)
209 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
212 TRACE(":fd (%d) freed\n",fd
);
213 if (fd
< 3) /* don't use 0,1,2 for user files */
218 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
221 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
224 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
238 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
239 /* caller must hold the files lock */
240 static int alloc_fd_from(HANDLE hand
, int flag
, int fd
)
246 WARN(":files exhausted!\n");
251 fdinfo
= get_ioinfo(fd
);
252 if(fdinfo
== &__badioinfo
) {
255 __pioinfo
[fd
/FD_BLOCK_SIZE
] = calloc(FD_BLOCK_SIZE
, sizeof(ioinfo
));
256 if(!__pioinfo
[fd
/FD_BLOCK_SIZE
]) {
257 WARN(":out of memory!\n");
262 for(i
=0; i
<FD_BLOCK_SIZE
; i
++)
263 __pioinfo
[fd
/FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
265 fdinfo
= get_ioinfo(fd
);
268 fdinfo
->handle
= hand
;
269 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
271 /* locate next free slot */
272 if (fd
== fdstart
&& fd
== fdend
)
275 while (fdstart
< fdend
&&
276 get_ioinfo(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
278 /* update last fd in use */
281 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
285 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
286 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
287 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
293 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
294 /*static*/ int alloc_fd(HANDLE hand
, int flag
)
299 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
300 ret
= alloc_fd_from(hand
, flag
, fdstart
);
305 /* INTERNAL: Allocate a FILE* for an fd slot */
306 /* caller must hold the files lock */
307 static FILE* alloc_fp(void)
312 for (i
= 3; i
< max_streams
; i
++)
318 if (file
->_flag
== 0)
320 if (i
== stream_idx
) stream_idx
++;
328 /* INTERNAL: initialize a FILE* from an open fd */
329 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
331 TRACE(":fd (%d) allocating FILE*\n",fd
);
332 if (!is_valid_fd(fd
))
334 WARN(":invalid fd %d\n",fd
);
339 memset(file
, 0, sizeof(*file
));
341 file
->_flag
= stream_flags
;
343 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
344 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
346 TRACE(":got FILE* (%p)\n",file
);
350 /* INTERNAL: Create an inheritance data block (for spawned process)
351 * The inheritance block is made of:
352 * 00 int nb of file descriptor (NBFD)
353 * 04 char file flags (wxflag): repeated for each fd
354 * 4+NBFD HANDLE file handle: repeated for each fd
356 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
363 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
364 *block
= calloc(*size
, 1);
370 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
371 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
373 *(unsigned*)*block
= fdend
;
374 for (fd
= 0; fd
< fdend
; fd
++)
376 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
377 fdinfo
= get_ioinfo(fd
);
378 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
380 *wxflag_ptr
= fdinfo
->wxflag
;
381 *handle_ptr
= fdinfo
->handle
;
386 *handle_ptr
= INVALID_HANDLE_VALUE
;
388 wxflag_ptr
++; handle_ptr
++;
393 /* INTERNAL: Set up all file descriptors,
394 * as well as default streams (stdin, stderr and stdout)
396 void msvcrt_init_io(void)
402 InitializeCriticalSection(&file_cs
);
403 file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_cs");
404 GetStartupInfoA(&si
);
405 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
411 count
= *(unsigned*)si
.lpReserved2
;
412 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
413 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
415 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
416 count
= min(count
, MAX_FILES
);
417 for (i
= 0; i
< count
; i
++)
419 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
420 alloc_fd_from(*handle_ptr
, *wxflag_ptr
, i
);
422 wxflag_ptr
++; handle_ptr
++;
424 fdend
= max( 3, count
);
425 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
426 if (get_ioinfo(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
430 alloc_fd_from(INVALID_HANDLE_VALUE
, 0, 3);
432 fdinfo
= get_ioinfo(0);
433 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
435 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
437 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
438 GetCurrentProcess(), &fdinfo
->handle
,
439 0, TRUE
, DUPLICATE_SAME_ACCESS
))
441 fdinfo
->handle
= std
;
443 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
446 fdinfo
= get_ioinfo(1);
447 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
449 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
451 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
452 GetCurrentProcess(), &fdinfo
->handle
,
453 0, TRUE
, DUPLICATE_SAME_ACCESS
))
455 fdinfo
->handle
= std
;
457 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
460 fdinfo
= get_ioinfo(2);
461 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
463 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
465 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
466 GetCurrentProcess(), &fdinfo
->handle
,
467 0, TRUE
, DUPLICATE_SAME_ACCESS
))
469 fdinfo
->handle
= std
;
471 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
474 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo(0)->handle
,
475 get_ioinfo(1)->handle
, get_ioinfo(2)->handle
);
477 memset(_iob
,0,3*sizeof(FILE));
478 for (i
= 0; i
< 3; i
++)
480 /* FILE structs for stdin/out/err are static and never deleted */
482 _iob
[i
]._tmpfname
= NULL
;
483 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
488 /* INTERNAL: Flush stdio file buffer */
489 static int flush_buffer(FILE* file
)
492 int cnt
=file
->_ptr
-file
->_base
;
493 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
494 file
->_flag
|= _IOERR
;
497 file
->_ptr
=file
->_base
;
498 file
->_cnt
=file
->_bufsiz
;
503 /* INTERNAL: Allocate stdio file buffer */
504 /*static*/ void alloc_buffer(FILE* file
)
506 file
->_base
= calloc(BUFSIZ
,1);
508 file
->_bufsiz
= BUFSIZ
;
509 file
->_flag
|= _IOMYBUF
;
511 file
->_base
= (char*)(&file
->_charbuf
);
513 file
->_bufsiz
= sizeof(file
->_charbuf
);
515 file
->_ptr
= file
->_base
;
519 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
520 static int int_to_base32(int num
, char *str
)
535 *p
= (num
& 31) + '0';
537 *p
+= ('a' - '0' - 10);
544 /* INTERNAL: wide character version of int_to_base32 */
545 static int int_to_base32_w(int num
, wchar_t *str
)
560 *p
= (num
& 31) + '0';
562 *p
+= ('a' - '0' - 10);
569 /* INTERNAL: Create a wide string from an ascii string */
570 wchar_t *msvcrt_wstrdupa(const char *str
)
572 const unsigned int len
= strlen(str
) + 1 ;
573 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
576 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
580 /*********************************************************************
581 * __iob_func(MSVCRT.@)
583 FILE * CDECL
__iob_func(void)
588 /*********************************************************************
591 int CDECL
_access(const char *filename
, int mode
)
593 DWORD attr
= GetFileAttributesA(filename
);
595 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
597 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
599 _dosmaperr(GetLastError());
602 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
604 _set_errno(ERROR_ACCESS_DENIED
);
610 /*********************************************************************
611 * _access_s (MSVCRT.@)
613 int CDECL
_access_s(const char *filename
, int mode
)
615 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
616 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
622 return _access(filename
, mode
);
625 /*********************************************************************
626 * _waccess (MSVCRT.@)
628 int CDECL
_waccess(const wchar_t *filename
, int mode
)
630 DWORD attr
= GetFileAttributesW(filename
);
632 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
634 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
636 _dosmaperr(GetLastError());
639 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
641 _set_errno(ERROR_ACCESS_DENIED
);
647 /*********************************************************************
648 * _waccess_s (MSVCRT.@)
650 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
652 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
653 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
659 return _waccess(filename
, mode
);
662 /*********************************************************************
665 int CDECL
_chmod(const char *path
, int flags
)
667 DWORD oldFlags
= GetFileAttributesA(path
);
669 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
671 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
672 oldFlags
| FILE_ATTRIBUTE_READONLY
;
674 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
677 _dosmaperr(GetLastError());
681 /*********************************************************************
684 int CDECL
_wchmod(const wchar_t *path
, int flags
)
686 DWORD oldFlags
= GetFileAttributesW(path
);
688 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
690 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
691 oldFlags
| FILE_ATTRIBUTE_READONLY
;
693 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
696 _dosmaperr(GetLastError());
700 /*********************************************************************
703 int CDECL
_unlink(const char *path
)
705 TRACE("%s\n",debugstr_a(path
));
706 if(DeleteFileA(path
))
708 TRACE("failed (%d)\n",GetLastError());
709 _dosmaperr(GetLastError());
713 /*********************************************************************
714 * _wunlink (MSVCRT.@)
716 int CDECL
_wunlink(const wchar_t *path
)
718 TRACE("(%s)\n",debugstr_w(path
));
719 if(DeleteFileW(path
))
721 TRACE("failed (%d)\n",GetLastError());
722 _dosmaperr(GetLastError());
726 /* _flushall calls fflush which calls _flushall */
727 int CDECL
fflush(FILE* file
);
729 /*********************************************************************
730 * _flushall (MSVCRT.@)
732 int CDECL
_flushall(void)
734 int i
, num_flushed
= 0;
738 for (i
= 3; i
< stream_idx
; i
++) {
743 if(file
->_flag
& _IOWRT
) {
751 TRACE(":flushed (%d) handles\n",num_flushed
);
755 /*********************************************************************
758 int CDECL
fflush(FILE* file
)
762 } else if(file
->_flag
& _IOWRT
) {
766 res
= flush_buffer(file
);
774 /*********************************************************************
777 int CDECL
_close(int fd
)
784 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
785 if (hand
== INVALID_HANDLE_VALUE
)
787 else if (!CloseHandle(hand
))
789 WARN(":failed-last error (%d)\n",GetLastError());
790 _dosmaperr(GetLastError());
803 /*********************************************************************
806 int CDECL
_commit(int fd
)
808 HANDLE hand
= fdtoh(fd
);
810 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
811 if (hand
== INVALID_HANDLE_VALUE
)
814 if (!FlushFileBuffers(hand
))
816 if (GetLastError() == ERROR_INVALID_HANDLE
)
818 /* FlushFileBuffers fails for console handles
819 * so we ignore this error.
823 TRACE(":failed-last error (%d)\n",GetLastError());
824 _dosmaperr(GetLastError());
831 /*********************************************************************
834 * MSDN isn't clear on this point, but the remarks for _pipe
835 * indicate file descriptors duplicated with _dup and _dup2 are always
838 int CDECL
_dup2(int od
, int nd
)
842 TRACE("(od=%d, nd=%d)\n", od
, nd
);
844 if (nd
< MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
848 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo(od
)->handle
,
849 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
851 int wxflag
= get_ioinfo(od
)->wxflag
& ~_O_NOINHERIT
;
855 ret
= alloc_fd_from(handle
, wxflag
, nd
);
863 /* _dup2 returns 0, not nd, on success */
870 _dosmaperr(GetLastError());
882 /*********************************************************************
885 int CDECL
_dup(int od
)
891 if (_dup2(od
, fd
) == 0)
899 /*********************************************************************
902 int CDECL
_eof(int fd
)
905 LONG hcurpos
,hendpos
;
906 HANDLE hand
= fdtoh(fd
);
908 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
910 if (hand
== INVALID_HANDLE_VALUE
)
913 if (get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
915 /* Otherwise we do it the hard way */
916 hcurpos
= hendpos
= 0;
917 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
918 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
920 if (curpos
== endpos
&& hcurpos
== hendpos
)
922 /* FIXME: shouldn't WX_ATEOF be set here? */
926 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
930 /*********************************************************************
931 * _fcloseall (MSVCRT.@)
933 int CDECL
_fcloseall(void)
935 int num_closed
= 0, i
;
939 for (i
= 3; i
< stream_idx
; i
++) {
942 if (file
->_flag
&& !fclose(file
))
947 TRACE(":closed (%d) handles\n",num_closed
);
951 /* free everything on process exit */
952 void msvcrt_free_io(void)
957 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
958 * stdout, and stderr (unlike GNU), so we need to fclose() them here
959 * or they won't get flushed.
965 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
968 for(i
=0; i
<sizeof(fstream
)/sizeof(fstream
[0]); i
++)
971 file_cs
.DebugInfo
->Spare
[0] = 0;
972 DeleteCriticalSection(&file_cs
);
975 /*********************************************************************
976 * _lseeki64 (MSVCRT.@)
978 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
980 HANDLE hand
= fdtoh(fd
);
983 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
984 if (hand
== INVALID_HANDLE_VALUE
)
987 if (whence
< 0 || whence
> 2)
993 TRACE(":fd (%d) to %s pos %s\n",
994 fd
,wine_dbgstr_longlong(offset
),
995 (whence
==SEEK_SET
)?"SEEK_SET":
996 (whence
==SEEK_CUR
)?"SEEK_CUR":
997 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
999 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1000 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1001 ofs
.QuadPart
= offset
;
1002 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1003 GetLastError() == ERROR_SUCCESS
)
1005 get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1006 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1008 return ofs
.QuadPart
;
1010 TRACE(":error-last error (%d)\n",GetLastError());
1011 _dosmaperr(GetLastError());
1015 /*********************************************************************
1018 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1020 return _lseeki64(fd
, offset
, whence
);
1023 /*********************************************************************
1024 * _lock_file (MSVCRT.@)
1026 void CDECL
_lock_file(FILE *file
)
1028 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1029 _lock(_STREAM_LOCKS
+(file
-_iob
));
1030 /* ReactOS: string streams dont need to be locked */
1031 else if(!(file
->_flag
& _IOSTRG
))
1032 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1035 /*********************************************************************
1036 * _unlock_file (MSVCRT.@)
1038 void CDECL
_unlock_file(FILE *file
)
1040 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1041 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1042 /* ReactOS: string streams dont need to be locked */
1043 else if(!(file
->_flag
& _IOSTRG
))
1044 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1048 /*********************************************************************
1049 * _locking (MSVCRT.@)
1051 * This is untested; the underlying LockFile doesn't work yet.
1053 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1057 HANDLE hand
= fdtoh(fd
);
1059 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1060 if (hand
== INVALID_HANDLE_VALUE
)
1063 if (mode
< 0 || mode
> 4)
1069 TRACE(":fd (%d) by 0x%08x mode %s\n",
1070 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1071 (mode
==_LK_LOCK
)?"_LK_LOCK":
1072 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1073 (mode
==_LK_RLCK
)?"_LK_RLCK":
1074 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1077 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1079 FIXME ("Seek failed\n");
1080 *_errno() = EINVAL
; /* FIXME */
1083 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1086 ret
= 1; /* just to satisfy gcc */
1089 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1094 else if (mode
== _LK_UNLCK
)
1095 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1097 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1098 /* FIXME - what about error settings? */
1099 return ret
? 0 : -1;
1102 /*********************************************************************
1103 * _fseeki64 (MSVCRT.@)
1105 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1110 /* Flush output if needed */
1111 if(file
->_flag
& _IOWRT
)
1114 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1115 offset
-= file
->_cnt
;
1116 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1117 /* Black magic correction for CR removal */
1119 for (i
=0; i
<file
->_cnt
; i
++) {
1120 if (file
->_ptr
[i
] == '\n')
1123 /* Black magic when reading CR at buffer boundary*/
1124 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1128 /* Discard buffered input */
1130 file
->_ptr
= file
->_base
;
1131 /* Reset direction of i/o */
1132 if(file
->_flag
& _IORW
) {
1133 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1135 /* Clear end of file flag */
1136 file
->_flag
&= ~_IOEOF
;
1137 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1143 /*********************************************************************
1146 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1148 return _fseeki64( file
, offset
, whence
);
1151 /*********************************************************************
1152 * _chsize (MSVCRT.@)
1154 int CDECL
_chsize(int fd
, long size
)
1160 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1165 if (handle
!= INVALID_HANDLE_VALUE
)
1167 /* save the current file pointer */
1168 cur
= _lseek(fd
, 0, SEEK_CUR
);
1171 pos
= _lseek(fd
, size
, SEEK_SET
);
1174 ret
= SetEndOfFile(handle
);
1175 if (!ret
) _dosmaperr(GetLastError());
1178 /* restore the file pointer */
1179 _lseek(fd
, cur
, SEEK_SET
);
1184 return ret
? 0 : -1;
1187 /*********************************************************************
1188 * clearerr (MSVCRT.@)
1190 void CDECL
clearerr(FILE* file
)
1192 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1195 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1199 /*********************************************************************
1202 void CDECL
rewind(FILE* file
)
1204 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1207 fseek(file
, 0L, SEEK_SET
);
1212 static int get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1214 int plus
= strchrW(mode
, '+') != NULL
;
1219 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1220 *stream_flags
= plus
? _IORW
: _IOREAD
;
1223 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1224 *stream_flags
= plus
? _IORW
: _IOWRT
;
1227 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1228 *stream_flags
= plus
? _IORW
: _IOWRT
;
1231 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1240 *open_flags
|= _O_BINARY
;
1241 *open_flags
&= ~_O_TEXT
;
1244 *open_flags
|= _O_TEXT
;
1245 *open_flags
&= ~_O_BINARY
;
1251 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1256 /*********************************************************************
1257 * _fdopen (MSVCRT.@)
1259 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1262 wchar_t *modeW
= NULL
;
1264 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1266 ret
= _wfdopen(fd
, modeW
);
1272 /*********************************************************************
1273 * _wfdopen (MSVCRT.@)
1275 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1277 int open_flags
, stream_flags
;
1280 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1283 if (!(file
= alloc_fp()))
1285 else if (init_fp(file
, fd
, stream_flags
) == -1)
1290 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1296 /*********************************************************************
1297 * _filelength (MSVCRT.@)
1299 LONG CDECL
_filelength(int fd
)
1301 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1304 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1307 if (endPos
!= curPos
)
1308 _lseek(fd
, curPos
, SEEK_SET
);
1315 /*********************************************************************
1316 * _filelengthi64 (MSVCRT.@)
1318 __int64 CDECL
_filelengthi64(int fd
)
1320 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1323 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1326 if (endPos
!= curPos
)
1327 _lseeki64(fd
, curPos
, SEEK_SET
);
1334 /*********************************************************************
1335 * _fileno (MSVCRT.@)
1337 int CDECL
_fileno(FILE* file
)
1339 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1343 /*********************************************************************
1344 * _get_osfhandle (MSVCRT.@)
1346 intptr_t CDECL
_get_osfhandle(int fd
)
1348 HANDLE hand
= fdtoh(fd
);
1349 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1351 return (intptr_t)hand
;
1354 /*********************************************************************
1355 * _isatty (MSVCRT.@)
1357 int CDECL
_isatty(int fd
)
1359 HANDLE hand
= fdtoh(fd
);
1361 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1362 if (hand
== INVALID_HANDLE_VALUE
)
1365 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1368 /*********************************************************************
1369 * _mktemp (MSVCRT.@)
1371 char * CDECL
_mktemp(char *pattern
)
1374 char *retVal
= pattern
;
1379 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1383 id
= GetCurrentProcessId();
1387 int tempNum
= id
/ 10;
1388 *pattern
-- = id
- (tempNum
* 10) + '0';
1394 *pattern
= letter
++;
1395 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1396 GetLastError() == ERROR_FILE_NOT_FOUND
)
1398 } while(letter
<= 'z');
1402 /*********************************************************************
1403 * _wmktemp (MSVCRT.@)
1405 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1408 wchar_t *retVal
= pattern
;
1410 wchar_t letter
= 'a';
1413 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1417 id
= GetCurrentProcessId();
1421 int tempNum
= id
/ 10;
1422 *pattern
-- = id
- (tempNum
* 10) + '0';
1428 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1429 GetLastError() == ERROR_FILE_NOT_FOUND
)
1431 *pattern
= letter
++;
1432 } while(letter
!= '|');
1436 /*static*/ unsigned split_oflags(unsigned oflags
)
1439 unsigned unsupp
; /* until we support everything */
1441 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1442 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1443 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1444 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1445 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1446 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1448 if ((unsupp
= oflags
& ~(
1449 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1450 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1451 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1453 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1455 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1460 /*********************************************************************
1463 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1466 SECURITY_ATTRIBUTES sa
;
1467 HANDLE readHandle
, writeHandle
;
1475 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1476 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1477 sa
.lpSecurityDescriptor
= NULL
;
1478 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1480 unsigned int wxflags
= split_oflags(textmode
);
1484 fd
= alloc_fd(readHandle
, wxflags
);
1488 fd
= alloc_fd(writeHandle
, wxflags
);
1497 CloseHandle(writeHandle
);
1503 CloseHandle(readHandle
);
1504 CloseHandle(writeHandle
);
1510 _dosmaperr(GetLastError());
1515 /*********************************************************************
1516 * _sopen_s (MSVCRT.@)
1518 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1520 DWORD access
= 0, creation
= 0, attrib
;
1524 SECURITY_ATTRIBUTES sa
;
1526 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1527 fd
, path
, oflags
, shflags
, pmode
);
1531 MSVCRT_INVALID_PMT("null out fd pointer");
1537 wxflag
= split_oflags(oflags
);
1538 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1540 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1541 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1542 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1545 if (oflags
& _O_CREAT
)
1547 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1548 FIXME(": pmode 0x%04x ignored\n", pmode
);
1550 WARN(": pmode 0x%04x ignored\n", pmode
);
1552 if (oflags
& _O_EXCL
)
1553 creation
= CREATE_NEW
;
1554 else if (oflags
& _O_TRUNC
)
1555 creation
= CREATE_ALWAYS
;
1557 creation
= OPEN_ALWAYS
;
1559 else /* no _O_CREAT */
1561 if (oflags
& _O_TRUNC
)
1562 creation
= TRUNCATE_EXISTING
;
1564 creation
= OPEN_EXISTING
;
1573 sharing
= FILE_SHARE_READ
;
1576 sharing
= FILE_SHARE_WRITE
;
1579 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1582 ERR( "Unhandled shflags 0x%x\n", shflags
);
1585 attrib
= FILE_ATTRIBUTE_NORMAL
;
1587 if (oflags
& _O_TEMPORARY
)
1589 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1591 sharing
|= FILE_SHARE_DELETE
;
1594 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1595 sa
.lpSecurityDescriptor
= NULL
;
1596 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1598 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1599 if (hand
== INVALID_HANDLE_VALUE
) {
1600 WARN(":failed-last error (%d)\n", GetLastError());
1601 _dosmaperr(GetLastError());
1605 *fd
= alloc_fd(hand
, wxflag
);
1607 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1611 /*********************************************************************
1614 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1619 if (oflags
& _O_CREAT
)
1623 va_start(ap
, shflags
);
1624 pmode
= va_arg(ap
, int);
1630 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1634 /*********************************************************************
1635 * _wsopen_s (MSVCRT.@)
1637 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1639 DWORD access
= 0, creation
= 0, attrib
;
1640 SECURITY_ATTRIBUTES sa
;
1645 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1646 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1650 MSVCRT_INVALID_PMT("null out fd pointer");
1656 wxflag
= split_oflags(oflags
);
1657 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1659 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1660 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1661 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1664 if (oflags
& _O_CREAT
)
1666 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1667 FIXME(": pmode 0x%04x ignored\n", pmode
);
1669 WARN(": pmode 0x%04x ignored\n", pmode
);
1671 if (oflags
& _O_EXCL
)
1672 creation
= CREATE_NEW
;
1673 else if (oflags
& _O_TRUNC
)
1674 creation
= CREATE_ALWAYS
;
1676 creation
= OPEN_ALWAYS
;
1678 else /* no _O_CREAT */
1680 if (oflags
& _O_TRUNC
)
1681 creation
= TRUNCATE_EXISTING
;
1683 creation
= OPEN_EXISTING
;
1692 sharing
= FILE_SHARE_READ
;
1695 sharing
= FILE_SHARE_WRITE
;
1698 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1701 ERR( "Unhandled shflags 0x%x\n", shflags
);
1704 attrib
= FILE_ATTRIBUTE_NORMAL
;
1706 if (oflags
& _O_TEMPORARY
)
1708 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1710 sharing
|= FILE_SHARE_DELETE
;
1713 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1714 sa
.lpSecurityDescriptor
= NULL
;
1715 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1717 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1719 if (hand
== INVALID_HANDLE_VALUE
) {
1720 WARN(":failed-last error (%d)\n",GetLastError());
1721 _dosmaperr(GetLastError());
1725 *fd
= alloc_fd(hand
, wxflag
);
1727 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1731 /*********************************************************************
1732 * _wsopen (MSVCRT.@)
1734 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1739 if (oflags
& _O_CREAT
)
1743 va_start(ap
, shflags
);
1744 pmode
= va_arg(ap
, int);
1750 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1754 /*********************************************************************
1757 int CDECL
_open( const char *path
, int flags
, ... )
1761 if (flags
& _O_CREAT
)
1764 va_start(ap
, flags
);
1765 pmode
= va_arg(ap
, int);
1767 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1770 return _sopen( path
, flags
, _SH_DENYNO
);
1773 /*********************************************************************
1776 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1780 if (flags
& _O_CREAT
)
1783 va_start(ap
, flags
);
1784 pmode
= va_arg(ap
, int);
1786 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1789 return _wsopen( path
, flags
, _SH_DENYNO
);
1792 /*********************************************************************
1795 int CDECL
_creat(const char *path
, int flags
)
1797 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1798 return _open(path
, usedFlags
);
1801 /*********************************************************************
1802 * _wcreat (MSVCRT.@)
1804 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1806 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1807 return _wopen(path
, usedFlags
);
1810 /*********************************************************************
1811 * _open_osfhandle (MSVCRT.@)
1813 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1817 /* _O_RDONLY (0) always matches, so set the read flag
1818 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1819 * file, so set the write flag. It also only sets _O_TEXT if it wants
1820 * text - it never sets _O_BINARY.
1822 /* don't let split_oflags() decide the mode if no mode is passed */
1823 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1824 oflags
|= _O_BINARY
;
1826 fd
= alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1827 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1831 /*********************************************************************
1834 int CDECL
_rmtmp(void)
1836 int num_removed
= 0, i
;
1840 for (i
= 3; i
< stream_idx
; i
++) {
1843 if (file
->_tmpfname
)
1852 TRACE(":removed (%d) temp files\n",num_removed
);
1856 /*********************************************************************
1859 * When reading \r as last character in text mode, read() positions
1860 * the file pointer on the \r character while getc() goes on to
1863 static int read_i(int fd
, void *buf
, unsigned int count
)
1866 char *bufstart
= buf
;
1867 HANDLE hand
= fdtoh(fd
);
1868 ioinfo
*fdinfo
= get_ioinfo(fd
);
1873 if (fdinfo
->wxflag
& WX_READEOF
) {
1874 fdinfo
->wxflag
|= WX_ATEOF
;
1875 TRACE("already at EOF, returning 0\n");
1878 /* Don't trace small reads, it gets *very* annoying */
1880 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1881 if (hand
== INVALID_HANDLE_VALUE
)
1884 /* Reading single bytes in O_TEXT mode makes things slow
1885 * So read big chunks
1887 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1889 if (count
!= 0 && num_read
== 0)
1891 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1892 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1894 else if (fdinfo
->wxflag
& WX_TEXT
)
1897 if (bufstart
[num_read
-1] == '\r')
1901 fdinfo
->wxflag
&= ~WX_READCR
;
1902 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1906 fdinfo
->wxflag
|= WX_READCR
;
1911 fdinfo
->wxflag
&= ~WX_READCR
;
1912 for (i
=0, j
=0; i
<num_read
; i
++)
1914 /* in text mode, a ctrl-z signals EOF */
1915 if (bufstart
[i
] == 0x1a)
1917 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1918 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1921 /* in text mode, strip \r if followed by \n.
1922 * BUG: should save state across calls somehow, so CR LF that
1923 * straddles buffer boundary gets recognized properly?
1925 if ((bufstart
[i
] != '\r')
1926 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1927 bufstart
[j
++] = bufstart
[i
];
1934 if (GetLastError() == ERROR_BROKEN_PIPE
)
1936 TRACE(":end-of-pipe\n");
1937 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
1942 TRACE(":failed-last error (%d)\n",GetLastError());
1948 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1952 /*********************************************************************
1955 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
1958 num_read
= read_i(fd
, buf
, count
);
1962 /*********************************************************************
1963 * _setmode (MSVCRT.@)
1965 int CDECL
_setmode(int fd
,int mode
)
1967 int ret
= get_ioinfo(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
1968 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1969 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1970 if ((mode
& _O_TEXT
) == _O_TEXT
)
1971 get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
1973 get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
1977 /*********************************************************************
1980 long CDECL
_tell(int fd
)
1982 return _lseek(fd
, 0, SEEK_CUR
);
1985 /*********************************************************************
1986 * _telli64 (MSVCRT.@)
1988 __int64 CDECL
_telli64(int fd
)
1990 return _lseeki64(fd
, 0, SEEK_CUR
);
1993 /*********************************************************************
1994 * _tempnam (MSVCRT.@)
1996 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
1998 char tmpbuf
[MAX_PATH
];
1999 const char *tmp_dir
= getenv("TMP");
2001 if (tmp_dir
) dir
= tmp_dir
;
2003 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2004 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2006 TRACE("got name (%s)\n",tmpbuf
);
2007 DeleteFileA(tmpbuf
);
2008 return _strdup(tmpbuf
);
2010 TRACE("failed (%d)\n",GetLastError());
2014 /*********************************************************************
2015 * _wtempnam (MSVCRT.@)
2017 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2019 wchar_t tmpbuf
[MAX_PATH
];
2021 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2022 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2024 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2025 DeleteFileW(tmpbuf
);
2026 return _wcsdup(tmpbuf
);
2028 TRACE("failed (%d)\n",GetLastError());
2032 /*********************************************************************
2035 int CDECL
_umask(int umask
)
2037 int old_umask
= umask
;
2038 TRACE("(%d)\n",umask
);
2039 MSVCRT_umask
= umask
;
2043 /*********************************************************************
2046 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2049 HANDLE hand
= fdtoh(fd
);
2051 /* Don't trace small writes, it gets *very* annoying */
2054 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2056 if (hand
== INVALID_HANDLE_VALUE
)
2062 /* If appending, go to EOF */
2063 if (get_ioinfo(fd
)->wxflag
& WX_APPEND
)
2064 _lseek(fd
, 0, FILE_END
);
2066 if (!(get_ioinfo(fd
)->wxflag
& WX_TEXT
))
2068 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2069 && (num_written
== count
))
2071 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2072 hand
, GetLastError());
2077 unsigned int i
, j
, nr_lf
;
2080 const char *s
= buf
, *buf_start
= buf
;
2081 /* find number of \n ( without preceding \r ) */
2082 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2087 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2092 if ((q
= p
= malloc(count
+ nr_lf
)))
2094 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2099 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2106 FIXME("Malloc failed\n");
2114 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2116 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2117 fd
, hand
, GetLastError(), num_written
);
2121 return s
- buf_start
;
2133 /*********************************************************************
2136 int CDECL
_putw(int val
, FILE* file
)
2141 len
= _write(file
->_file
, &val
, sizeof(val
));
2142 if (len
== sizeof(val
)) {
2147 file
->_flag
|= _IOERR
;
2152 /*********************************************************************
2155 int CDECL
fclose(FILE* file
)
2161 free(file
->_tmpfname
);
2162 file
->_tmpfname
= NULL
;
2163 /* flush stdio buffers */
2164 if(file
->_flag
& _IOWRT
)
2166 if(file
->_flag
& _IOMYBUF
)
2169 r
=_close(file
->_file
);
2173 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2174 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2176 if(file
== get_file(stream_idx
-1)) {
2177 while(stream_idx
>3 && !file
->_flag
) {
2179 file
= get_file(stream_idx
-1);
2183 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2186 /*********************************************************************
2189 int CDECL
feof(FILE* file
)
2194 ret
= file
->_flag
& _IOEOF
;
2200 /*********************************************************************
2203 int CDECL
ferror(FILE* file
)
2208 ret
= file
->_flag
& _IOERR
;
2214 /*********************************************************************
2215 * _filbuf (MSVCRT.@)
2217 int CDECL
_filbuf(FILE* file
)
2222 /* Allocate buffer if needed */
2223 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
))
2226 if(!(file
->_flag
& _IOREAD
)) {
2227 if(file
->_flag
& _IORW
)
2228 file
->_flag
|= _IOREAD
;
2235 if(file
->_flag
& _IONBF
) {
2237 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2238 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2246 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2248 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2255 file
->_ptr
= file
->_base
+1;
2256 c
= *(unsigned char *)file
->_base
;
2262 /*********************************************************************
2265 int CDECL
fgetc(FILE* file
)
2273 i
= (unsigned char *)file
->_ptr
++;
2282 /*********************************************************************
2283 * _fgetchar (MSVCRT.@)
2285 int CDECL
_fgetchar(void)
2287 return fgetc(stdin
);
2290 /*********************************************************************
2293 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2296 char * buf_start
= s
;
2298 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2299 file
,file
->_file
,s
,size
);
2303 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2308 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2310 TRACE(":nothing read\n");
2314 if ((cc
!= EOF
) && (size
> 1))
2317 TRACE(":got %s\n", debugstr_a(buf_start
));
2322 /*********************************************************************
2325 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2326 * the CR from CR/LF combinations
2328 wint_t CDECL
fgetwc(FILE* file
)
2333 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2340 for(i
=0; i
<sizeof(wc
); i
++)
2353 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2368 if ((__mb_cur_max
> 1) && isleadbyte(c
))
2370 FIXME("Treat Multibyte characters\n");
2380 /*********************************************************************
2383 int CDECL
_getw(FILE* file
)
2391 for (j
=0; j
<sizeof(int); j
++) {
2394 file
->_flag
|= _IOEOF
;
2405 /*********************************************************************
2408 wint_t CDECL
getwc(FILE* file
)
2410 return fgetwc(file
);
2413 /*********************************************************************
2414 * _fgetwchar (MSVCRT.@)
2416 wint_t CDECL
_fgetwchar(void)
2418 return fgetwc(stdin
);
2421 /*********************************************************************
2422 * getwchar (MSVCRT.@)
2424 wint_t CDECL
getwchar(void)
2426 return _fgetwchar();
2429 /*********************************************************************
2432 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2435 wchar_t * buf_start
= s
;
2437 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2438 file
,file
->_file
,s
,size
);
2442 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2447 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2449 TRACE(":nothing read\n");
2453 if ((cc
!= WEOF
) && (size
> 1))
2456 TRACE(":got %s\n", debugstr_w(buf_start
));
2461 /*********************************************************************
2464 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2466 size_t wrcnt
=size
* nmemb
;
2473 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2474 memcpy(file
->_ptr
, ptr
, pcnt
);
2479 ptr
= (const char*)ptr
+ pcnt
;
2480 } else if(!(file
->_flag
& _IOWRT
)) {
2481 if(file
->_flag
& _IORW
) {
2482 file
->_flag
|= _IOWRT
;
2490 int res
=flush_buffer(file
);
2492 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2495 file
->_flag
|= _IOERR
;
2498 written
+= pwritten
;
2503 return written
/ size
;
2506 /*********************************************************************
2509 wint_t CDECL
fputwc(wint_t wc
, FILE* file
)
2512 if (fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2517 /*********************************************************************
2518 * _fputwchar (MSVCRT.@)
2520 wint_t CDECL
_fputwchar(wint_t wc
)
2522 return fputwc(wc
, stdout
);
2525 /*********************************************************************
2526 * _wfsopen (MSVCRT.@)
2528 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2531 int open_flags
, stream_flags
, fd
;
2533 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2535 /* map mode string to open() flags. "man fopen" for possibilities. */
2536 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2540 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2543 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2545 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2552 TRACE(":got (%p)\n",file
);
2553 if (fd
>= 0 && !file
)
2559 /*********************************************************************
2560 * _fsopen (MSVCRT.@)
2562 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2565 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2567 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2568 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2572 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2575 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2580 ret
= _wfsopen(pathW
, modeW
, share
);
2587 /*********************************************************************
2590 FILE * CDECL
fopen(const char *path
, const char *mode
)
2592 return _fsopen( path
, mode
, _SH_DENYNO
);
2595 /*********************************************************************
2596 * fopen_s (MSVCRT.@)
2598 int CDECL
fopen_s(FILE** pFile
,
2599 const char *filename
, const char *mode
)
2601 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2602 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2607 *pFile
= fopen(filename
, mode
);
2614 /*********************************************************************
2615 * _wfopen (MSVCRT.@)
2617 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2619 return _wfsopen( path
, mode
, _SH_DENYNO
);
2622 /*********************************************************************
2623 * _wfopen_s (MSVCRT.@)
2625 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
2626 const wchar_t *mode
)
2628 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2629 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2634 *pFile
= _wfopen(filename
, mode
);
2641 /* fputc calls _flsbuf which calls fputc */
2642 int CDECL
_flsbuf(int c
, FILE* file
);
2644 /*********************************************************************
2647 int CDECL
fputc(int c
, FILE* file
)
2657 res
= flush_buffer(file
);
2659 return res
? res
: c
;
2666 res
= _flsbuf(c
, file
);
2672 /*********************************************************************
2673 * _fputchar (MSVCRT.@)
2675 int CDECL
_fputchar(int c
)
2677 return fputc(c
, stdout
);
2680 /*********************************************************************
2683 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2685 size_t rcnt
=size
* nmemb
;
2694 /* first buffered data */
2696 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2697 memcpy(ptr
, file
->_ptr
, pcnt
);
2702 ptr
= (char*)ptr
+ pcnt
;
2703 } else if(!(file
->_flag
& _IOREAD
)) {
2704 if(file
->_flag
& _IORW
) {
2705 file
->_flag
|= _IOREAD
;
2714 /* Fill the buffer on small reads.
2715 * TODO: Use a better buffering strategy.
2717 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2718 if (file
->_bufsiz
== 0) {
2721 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2722 file
->_ptr
= file
->_base
;
2723 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2724 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2725 if (i
> 0 && i
< file
->_cnt
) {
2726 get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
2727 file
->_flag
&= ~_IOEOF
;
2730 memcpy(ptr
, file
->_ptr
, i
);
2735 i
= _read(file
->_file
,ptr
, rcnt
);
2739 ptr
= (char *)ptr
+i
;
2740 /* expose feof condition in the flags
2741 * MFC tests file->_flag for feof, and doesn't call feof())
2743 if (get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
2744 file
->_flag
|= _IOEOF
;
2747 file
->_flag
|= _IOERR
;
2758 /*********************************************************************
2759 * _wfreopen (MSVCRT.@)
2762 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
2764 int open_flags
, stream_flags
, fd
;
2766 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2769 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2774 /* map mode string to open() flags. "man fopen" for possibilities. */
2775 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2779 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2782 else if (init_fp(file
, fd
, stream_flags
) == -1)
2785 WARN(":failed-last error (%d)\n",GetLastError());
2786 _dosmaperr(GetLastError());
2795 /*********************************************************************
2796 * freopen (MSVCRT.@)
2799 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
2802 wchar_t *pathW
= NULL
, *modeW
= NULL
;
2804 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2805 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2811 ret
= _wfreopen(pathW
, modeW
, file
);
2818 /*********************************************************************
2819 * fsetpos (MSVCRT.@)
2821 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2826 /* Note that all this has been lifted 'as is' from fseek */
2827 if(file
->_flag
& _IOWRT
)
2830 /* Discard buffered input */
2832 file
->_ptr
= file
->_base
;
2834 /* Reset direction of i/o */
2835 if(file
->_flag
& _IORW
) {
2836 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2839 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2844 /*********************************************************************
2845 * _ftelli64 (MSVCRT.@)
2847 __int64 CDECL
_ftelli64(FILE* file
)
2849 /* TODO: just call fgetpos and return lower half of result */
2854 pos
= _telli64(file
->_file
);
2860 if( file
->_flag
& _IOWRT
) {
2861 off
= file
->_ptr
- file
->_base
;
2864 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2865 /* Black magic correction for CR removal */
2867 for (i
=0; i
<file
->_cnt
; i
++) {
2868 if (file
->_ptr
[i
] == '\n')
2871 /* Black magic when reading CR at buffer boundary*/
2872 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
2882 /*********************************************************************
2885 LONG CDECL
ftell(FILE* file
)
2887 return _ftelli64(file
);
2890 /*********************************************************************
2891 * fgetpos (MSVCRT.@)
2893 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2898 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
2904 if( file
->_flag
& _IOWRT
) {
2905 off
= file
->_ptr
- file
->_base
;
2908 if (get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
2909 /* Black magic correction for CR removal */
2911 for (i
=0; i
<file
->_cnt
; i
++) {
2912 if (file
->_ptr
[i
] == '\n')
2915 /* Black magic when reading CR at buffer boundary*/
2916 if(get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
2926 /*********************************************************************
2929 int CDECL
fputs(const char *s
, FILE* file
)
2931 size_t i
, len
= strlen(s
);
2935 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2936 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2940 for (i
=0; i
<len
; i
++)
2941 if (fputc(s
[i
], file
) == EOF
) {
2950 /*********************************************************************
2953 int CDECL
fputws(const wchar_t *s
, FILE* file
)
2955 size_t i
, len
= strlenW(s
);
2959 if (!(get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
2960 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2964 for (i
=0; i
<len
; i
++) {
2965 if (((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
2966 || fputwc(s
[i
], file
) == WEOF
) {
2976 /*********************************************************************
2977 * getchar (MSVCRT.@)
2979 int CDECL
getchar(void)
2981 return fgetc(stdin
);
2984 /*********************************************************************
2987 int CDECL
getc(FILE* file
)
2992 /*********************************************************************
2995 char * CDECL
gets(char *buf
)
2998 char * buf_start
= buf
;
3001 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3003 if(cc
!= '\r') *buf
++ = (char)cc
;
3007 TRACE("got '%s'\n", buf_start
);
3008 _unlock_file(stdin
);
3012 /*********************************************************************
3015 wchar_t* CDECL
_getws(wchar_t* buf
)
3021 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3025 *buf
++ = (wchar_t)cc
;
3029 TRACE("got %s\n", debugstr_w(ws
));
3030 _unlock_file(stdin
);
3034 /*********************************************************************
3037 int CDECL
putc(int c
, FILE* file
)
3039 return fputc(c
, file
);
3042 /*********************************************************************
3043 * putchar (MSVCRT.@)
3045 int CDECL
putchar(int c
)
3047 return fputc(c
, stdout
);
3050 /*********************************************************************
3051 * _putwch (MSVCRT.@)
3053 wint_t CDECL
_putwch(wchar_t c
)
3055 return fputwc(c
, stdout
);
3058 /*********************************************************************
3061 int CDECL
puts(const char *s
)
3063 size_t len
= strlen(s
);
3067 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3068 _unlock_file(stdout
);
3072 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3073 _unlock_file(stdout
);
3077 /*********************************************************************
3080 int CDECL
_putws(const wchar_t *s
)
3082 static const wchar_t nl
= '\n';
3083 size_t len
= strlenW(s
);
3087 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3088 _unlock_file(stdout
);
3092 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3093 _unlock_file(stdout
);
3097 /*********************************************************************
3100 int CDECL
remove(const char *path
)
3102 TRACE("(%s)\n",path
);
3103 if (DeleteFileA(path
))
3105 TRACE(":failed (%d)\n",GetLastError());
3106 _dosmaperr(GetLastError());
3110 /*********************************************************************
3111 * _wremove (MSVCRT.@)
3113 int CDECL
_wremove(const wchar_t *path
)
3115 TRACE("(%s)\n",debugstr_w(path
));
3116 if (DeleteFileW(path
))
3118 TRACE(":failed (%d)\n",GetLastError());
3119 _dosmaperr(GetLastError());
3123 /*********************************************************************
3126 int CDECL
rename(const char *oldpath
,const char *newpath
)
3128 TRACE(":from %s to %s\n",oldpath
,newpath
);
3129 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3131 TRACE(":failed (%d)\n",GetLastError());
3132 _dosmaperr(GetLastError());
3136 /*********************************************************************
3137 * _wrename (MSVCRT.@)
3139 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3141 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3142 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3144 TRACE(":failed (%d)\n",GetLastError());
3145 _dosmaperr(GetLastError());
3149 /*********************************************************************
3150 * setvbuf (MSVCRT.@)
3152 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3160 if(mode
== _IOFBF
) {
3161 file
->_flag
&= ~_IONBF
;
3162 file
->_base
= file
->_ptr
= buf
;
3164 file
->_bufsiz
= size
;
3167 file
->_flag
|= _IONBF
;
3173 /*********************************************************************
3176 void CDECL
setbuf(FILE* file
, char *buf
)
3178 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3181 /*********************************************************************
3184 char * CDECL
tmpnam(char *s
)
3191 thread_data_t
*data
= msvcrt_get_thread_data();
3193 if(!data
->tmpnam_buffer
)
3194 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3196 s
= data
->tmpnam_buffer
;
3199 int_to_base32(GetCurrentProcessId(), tmpstr
);
3200 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3201 for (count
= 0; count
< TMP_MAX
; count
++)
3203 size
= int_to_base32(tmpnam_unique
++, tmpstr
);
3204 memcpy(p
, tmpstr
, size
);
3205 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3206 GetLastError() == ERROR_FILE_NOT_FOUND
)
3212 /*********************************************************************
3213 * _wtmpnam (MSVCRT.@)
3215 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3217 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3222 thread_data_t
*data
= msvcrt_get_thread_data();
3224 if(!data
->wtmpnam_buffer
)
3225 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3227 s
= data
->wtmpnam_buffer
;
3230 int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3231 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3232 for (count
= 0; count
< TMP_MAX
; count
++)
3234 size
= int_to_base32_w(tmpnam_unique
++, tmpstr
);
3235 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3236 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3237 GetLastError() == ERROR_FILE_NOT_FOUND
)
3243 /*********************************************************************
3244 * tmpfile (MSVCRT.@)
3246 FILE* CDECL
tmpfile(void)
3248 char *filename
= tmpnam(NULL
);
3253 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
3254 if (fd
!= -1 && (file
= alloc_fp()))
3256 if (init_fp(file
, fd
, _O_RDWR
) == -1)
3261 else file
->_tmpfname
= _strdup(filename
);
3267 /*********************************************************************
3270 int CDECL
ungetc(int c
, FILE * file
)
3276 if(file
->_bufsiz
== 0) {
3280 if(file
->_ptr
>file
->_base
) {
3293 /*********************************************************************
3294 * ungetwc (MSVCRT.@)
3296 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3299 char * pp
= (char *)&mwc
;
3303 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3304 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3316 /*********************************************************************
3317 * _getmaxstdio (MSVCRT.@)
3319 int CDECL
_getmaxstdio(void)
3324 /*********************************************************************
3325 * _setmaxstdio (MSVCRT.@)
3327 int CDECL
_setmaxstdio(int newmax
)
3329 TRACE("%d\n", newmax
);
3331 if(newmax
<_IOB_ENTRIES
|| newmax
>MAX_FILES
|| newmax
<stream_idx
)
3334 max_streams
= newmax
;