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
87 /* ReactOS: Use _FDINFO instead! */
91 DWORD unkn
[7]; /* critical section and init flag */
94 ioinfo fdesc
[MAX_FILES
];
96 FILE _iob
[3] = { { 0 } };
98 static int fdstart
= 3; /* first unallocated fd */
99 static int fdend
= 3; /* highest allocated fd */
101 static FILE* fstreams
[2048];
102 static int stream_idx
;
104 /* INTERNAL: process umask */
105 static int MSVCRT_umask
= 0;
107 /* INTERNAL: Static buffer for temp file name */
108 static char tmpname
[MAX_PATH
];
110 /* This critical section protects the tables fdesc and fstreams,
111 * and their related indexes, fdstart, fdend,
112 * and stream_idx, from race conditions.
113 * It doesn't protect against race conditions manipulating the underlying files
114 * or flags; doing so would probably be better accomplished with per-file
115 * protection, rather than locking the whole table for every change.
117 static CRITICAL_SECTION FILE_cs
;
118 #define LOCK_FILES() do { EnterCriticalSection(&FILE_cs); } while (0)
119 #define UNLOCK_FILES() do { LeaveCriticalSection(&FILE_cs); } while (0)
121 static inline BOOL
is_valid_fd(int fd
)
123 return fd
>= 0 && fd
< fdend
&& (fdesc
[fd
].wxflag
& WX_OPEN
);
126 /* INTERNAL: Get the HANDLE for a fd
127 * This doesn't lock the table, because a failure will result in
128 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
129 * it returns a valid handle which is about to be closed, a subsequent call
130 * will fail, most likely in a sane way.
134 if (!is_valid_fd(fd
))
136 WARN(":fd (%d) - no handle!\n",fd
);
139 return INVALID_HANDLE_VALUE
;
141 //if (fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
142 return fdesc
[fd
].handle
;
145 /* INTERNAL: free a file entry fd */
146 static void free_fd(int fd
)
149 fdesc
[fd
].handle
= INVALID_HANDLE_VALUE
;
150 fdesc
[fd
].wxflag
= 0;
151 TRACE(":fd (%d) freed\n",fd
);
152 if (fd
< 3) /* don't use 0,1,2 for user files */
156 case 0: SetStdHandle(STD_INPUT_HANDLE
, NULL
); break;
157 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, NULL
); break;
158 case 2: SetStdHandle(STD_ERROR_HANDLE
, NULL
); break;
171 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
172 /* caller must hold the files lock */
173 static int alloc_fd_from(HANDLE hand
, int flag
, int fd
)
177 WARN(":files exhausted!\n");
180 fdesc
[fd
].handle
= hand
;
181 fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
183 /* locate next free slot */
184 if (fd
== fdstart
&& fd
== fdend
)
187 while (fdstart
< fdend
&&
188 fdesc
[fdstart
].handle
!= INVALID_HANDLE_VALUE
)
190 /* update last fd in use */
193 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
197 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
198 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
199 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
205 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
206 /*static */int alloc_fd(HANDLE hand
, int flag
)
211 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
212 ret
= alloc_fd_from(hand
, flag
, fdstart
);
217 /* INTERNAL: Allocate a FILE* for an fd slot */
218 /* caller must hold the files lock */
219 static FILE* alloc_fp(void)
223 for (i
= 3; i
< sizeof(fstreams
) / sizeof(fstreams
[0]); i
++)
225 if (!fstreams
[i
] || fstreams
[i
]->_flag
== 0)
229 if (!(fstreams
[i
] = calloc(sizeof(FILE),1)))
231 if (i
== stream_idx
) stream_idx
++;
239 /* INTERNAL: initialize a FILE* from an open fd */
240 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
242 TRACE(":fd (%d) allocating FILE*\n",fd
);
243 if (!is_valid_fd(fd
))
245 WARN(":invalid fd %d\n",fd
);
250 memset(file
, 0, sizeof(*file
));
252 file
->_flag
= stream_flags
;
254 TRACE(":got FILE* (%p)\n",file
);
258 /* INTERNAL: Create an inheritance data block (for spawned process)
259 * The inheritance block is made of:
260 * 00 int nb of file descriptor (NBFD)
261 * 04 char file flags (wxflag): repeated for each fd
262 * 4+NBFD HANDLE file handle: repeated for each fd
264 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
270 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
271 *block
= calloc(*size
, 1);
277 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
278 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
280 *(unsigned*)*block
= fdend
;
281 for (fd
= 0; fd
< fdend
; fd
++)
283 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
284 if ((fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
286 *wxflag_ptr
= fdesc
[fd
].wxflag
;
287 *handle_ptr
= fdesc
[fd
].handle
;
292 *handle_ptr
= INVALID_HANDLE_VALUE
;
294 wxflag_ptr
++; handle_ptr
++;
299 /* INTERNAL: Set up all file descriptors,
300 * as well as default streams (stdin, stderr and stdout)
302 void msvcrt_init_io(void)
307 InitializeCriticalSection(&FILE_cs
);
308 FILE_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FILE_cs");
309 GetStartupInfoA(&si
);
310 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
316 count
= *(unsigned*)si
.lpReserved2
;
317 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
318 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
320 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
321 count
= min(count
, sizeof(fdesc
) / sizeof(fdesc
[0]));
322 for (i
= 0; i
< count
; i
++)
324 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
326 fdesc
[i
].wxflag
= *wxflag_ptr
;
327 fdesc
[i
].handle
= *handle_ptr
;
332 fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
334 wxflag_ptr
++; handle_ptr
++;
336 fdend
= max( 3, count
);
337 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
338 if (fdesc
[fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
341 if (!(fdesc
[0].wxflag
& WX_OPEN
) || fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
344 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE
),
345 GetCurrentProcess(), &fdesc
[0].handle
, 0, TRUE
,
346 DUPLICATE_SAME_ACCESS
);
348 fdesc
[0].handle
= GetStdHandle(STD_INPUT_HANDLE
);
349 if (fdesc
[0].handle
== NULL
)
350 fdesc
[0].handle
= INVALID_HANDLE_VALUE
;
352 fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
354 if (!(fdesc
[1].wxflag
& WX_OPEN
) || fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
357 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE
),
358 GetCurrentProcess(), &fdesc
[1].handle
, 0, TRUE
,
359 DUPLICATE_SAME_ACCESS
);
361 fdesc
[1].handle
= GetStdHandle(STD_OUTPUT_HANDLE
);
362 if (fdesc
[1].handle
== NULL
)
363 fdesc
[1].handle
= INVALID_HANDLE_VALUE
;
365 fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
367 if (!(fdesc
[2].wxflag
& WX_OPEN
) || fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
370 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE
),
371 GetCurrentProcess(), &fdesc
[2].handle
, 0, TRUE
,
372 DUPLICATE_SAME_ACCESS
);
374 fdesc
[2].handle
= GetStdHandle(STD_ERROR_HANDLE
);
375 if (fdesc
[2].handle
== NULL
)
376 fdesc
[2].handle
= INVALID_HANDLE_VALUE
;
378 fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
381 TRACE(":handles (%p)(%p)(%p)\n",fdesc
[0].handle
,
382 fdesc
[1].handle
,fdesc
[2].handle
);
384 memset(_iob
,0,3*sizeof(FILE));
385 for (i
= 0; i
< 3; i
++)
387 /* FILE structs for stdin/out/err are static and never deleted */
388 fstreams
[i
] = &_iob
[i
];
390 _iob
[i
]._tmpfname
= NULL
;
391 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
396 /* INTERNAL: Flush stdio file buffer */
397 static int flush_buffer(FILE* file
)
400 int cnt
=file
->_ptr
-file
->_base
;
401 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
402 file
->_flag
|= _IOERR
;
405 file
->_ptr
=file
->_base
;
406 file
->_cnt
=file
->_bufsiz
;
411 /* INTERNAL: Allocate stdio file buffer */
412 void alloc_buffer(FILE* file
)
414 file
->_base
= calloc(BUFSIZ
,1);
416 file
->_bufsiz
= BUFSIZ
;
417 file
->_flag
|= _IOMYBUF
;
419 file
->_base
= (char*)(&file
->_charbuf
);
421 file
->_bufsiz
= sizeof(file
->_charbuf
);
423 file
->_ptr
= file
->_base
;
427 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
428 static void int_to_base32(int num
, char *str
)
443 *p
= (num
& 31) + '0';
445 *p
+= ('a' - '0' - 10);
450 /*********************************************************************
453 FILE * CDECL
__p__iob(void)
458 /*********************************************************************
461 int CDECL
_access(const char *filename
, int mode
)
463 DWORD attr
= GetFileAttributesA(filename
);
465 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
467 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
469 _dosmaperr(GetLastError());
472 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
474 _dosmaperr(ERROR_ACCESS_DENIED
);
480 /*********************************************************************
481 * _waccess (MSVCRT.@)
483 int CDECL
_waccess(const wchar_t *filename
, int mode
)
485 DWORD attr
= GetFileAttributesW(filename
);
487 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
489 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
491 _dosmaperr(GetLastError());
494 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
496 _dosmaperr(ERROR_ACCESS_DENIED
);
502 /*********************************************************************
505 int CDECL
_chmod(const char *path
, int flags
)
507 DWORD oldFlags
= GetFileAttributesA(path
);
509 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
511 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
512 oldFlags
| FILE_ATTRIBUTE_READONLY
;
514 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
517 _dosmaperr(GetLastError());
521 /*********************************************************************
524 int CDECL
_wchmod(const wchar_t *path
, int flags
)
526 DWORD oldFlags
= GetFileAttributesW(path
);
528 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
530 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
531 oldFlags
| FILE_ATTRIBUTE_READONLY
;
533 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
536 _dosmaperr(GetLastError());
540 /*********************************************************************
543 int CDECL
_unlink(const char *path
)
545 TRACE("%s\n",debugstr_a(path
));
546 if(DeleteFileA(path
))
548 TRACE("failed (%d)\n",GetLastError());
549 _dosmaperr(GetLastError());
553 /*********************************************************************
554 * _wunlink (MSVCRT.@)
556 int CDECL
_wunlink(const wchar_t *path
)
558 TRACE("(%s)\n",debugstr_w(path
));
559 if(DeleteFileW(path
))
561 TRACE("failed (%d)\n",GetLastError());
562 _dosmaperr(GetLastError());
566 /* _flushall calls fflush which calls _flushall */
567 int CDECL
fflush(FILE* file
);
569 /*********************************************************************
570 * _flushall (MSVCRT.@)
572 int CDECL
_flushall(void)
574 int i
, num_flushed
= 0;
577 for (i
= 3; i
< stream_idx
; i
++)
578 if (fstreams
[i
] && fstreams
[i
]->_flag
)
581 /* FIXME: flush, do not commit */
582 if (_commit(i
) == -1)
584 fstreams
[i
]->_flag
|= _IOERR
;
586 if(fstreams
[i
]->_flag
& _IOWRT
) {
593 TRACE(":flushed (%d) handles\n",num_flushed
);
597 /*********************************************************************
600 int CDECL
fflush(FILE* file
)
604 } else if(file
->_flag
& _IOWRT
) {
605 int res
=flush_buffer(file
);
611 /*********************************************************************
614 int CDECL
_close(int fd
)
621 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
622 if (hand
== INVALID_HANDLE_VALUE
)
624 else if (!CloseHandle(hand
))
626 WARN(":failed-last error (%d)\n",GetLastError());
627 _dosmaperr(GetLastError());
640 /*********************************************************************
643 int CDECL
_commit(int fd
)
645 HANDLE hand
= fdtoh(fd
);
647 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
648 if (hand
== INVALID_HANDLE_VALUE
)
651 if (!FlushFileBuffers(hand
))
653 if (GetLastError() == ERROR_INVALID_HANDLE
)
655 /* FlushFileBuffers fails for console handles
656 * so we ignore this error.
660 TRACE(":failed-last error (%d)\n",GetLastError());
661 _dosmaperr(GetLastError());
668 /*********************************************************************
671 * MSDN isn't clear on this point, but the remarks for _pipe
672 * indicate file descriptors duplicated with _dup and _dup2 are always
675 int CDECL
_dup2(int od
, int nd
)
679 TRACE("(od=%d, nd=%d)\n", od
, nd
);
681 if (nd
< MAX_FILES
&& is_valid_fd(od
))
685 if (DuplicateHandle(GetCurrentProcess(), fdesc
[od
].handle
,
686 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
688 int wxflag
= fdesc
[od
].wxflag
& ~_O_NOINHERIT
;
692 ret
= alloc_fd_from(handle
, wxflag
, nd
);
700 /* _dup2 returns 0, not nd, on success */
707 _dosmaperr(GetLastError());
719 /*********************************************************************
722 int CDECL
_dup(int od
)
728 if (_dup2(od
, fd
) == 0)
736 /*********************************************************************
739 int CDECL
_eof(int fd
)
742 LONG hcurpos
,hendpos
;
743 HANDLE hand
= fdtoh(fd
);
745 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
747 if (hand
== INVALID_HANDLE_VALUE
)
750 if (fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
752 /* Otherwise we do it the hard way */
753 hcurpos
= hendpos
= 0;
754 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
755 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
757 if (curpos
== endpos
&& hcurpos
== hendpos
)
759 /* FIXME: shouldn't WX_ATEOF be set here? */
763 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
767 /*********************************************************************
768 * _fcloseall (MSVCRT.@)
770 int CDECL
_fcloseall(void)
772 int num_closed
= 0, i
;
775 for (i
= 3; i
< stream_idx
; i
++)
776 if (fstreams
[i
] && fstreams
[i
]->_flag
&&
777 !fclose(fstreams
[i
]))
781 TRACE(":closed (%d) handles\n",num_closed
);
785 /* free everything on process exit */
786 void msvcrt_free_io(void)
789 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
790 * stdout, and stderr (unlike GNU), so we need to fclose() them here
791 * or they won't get flushed.
796 FILE_cs
.DebugInfo
->Spare
[0] = 0;
797 DeleteCriticalSection(&FILE_cs
);
800 /*********************************************************************
801 * _lseeki64 (MSVCRT.@)
803 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
805 HANDLE hand
= fdtoh(fd
);
806 LARGE_INTEGER ofs
, ret
;
808 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
809 if (hand
== INVALID_HANDLE_VALUE
)
812 if (whence
< 0 || whence
> 2)
818 TRACE(":fd (%d) to %s pos %s\n",
819 fd
,wine_dbgstr_longlong(offset
),
820 (whence
==SEEK_SET
)?"SEEK_SET":
821 (whence
==SEEK_CUR
)?"SEEK_CUR":
822 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
824 ofs
.QuadPart
= offset
;
825 if (SetFilePointerEx(hand
, ofs
, &ret
, whence
))
827 fdesc
[fd
].wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
828 /* FIXME: What if we seek _to_ EOF - is EOF set? */
832 TRACE(":error-last error (%d)\n",GetLastError());
833 _dosmaperr(GetLastError());
837 /*********************************************************************
840 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
842 return _lseeki64(fd
, offset
, whence
);
845 /*********************************************************************
846 * _locking (MSVCRT.@)
848 * This is untested; the underlying LockFile doesn't work yet.
850 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
854 HANDLE hand
= fdtoh(fd
);
856 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
857 if (hand
== INVALID_HANDLE_VALUE
)
860 if (mode
< 0 || mode
> 4)
866 TRACE(":fd (%d) by 0x%08x mode %s\n",
867 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
868 (mode
==_LK_LOCK
)?"_LK_LOCK":
869 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
870 (mode
==_LK_RLCK
)?"_LK_RLCK":
871 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
874 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
876 FIXME ("Seek failed\n");
877 *_errno() = EINVAL
; /* FIXME */
880 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
883 ret
= 1; /* just to satisfy gcc */
886 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
891 else if (mode
== _LK_UNLCK
)
892 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
894 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
895 /* FIXME - what about error settings? */
899 /*********************************************************************
902 int CDECL
fseek(FILE* file
, long offset
, int whence
)
904 /* Flush output if needed */
905 if(file
->_flag
& _IOWRT
)
908 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
909 offset
-= file
->_cnt
;
910 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
911 /* Black magic correction for CR removal */
913 for (i
=0; i
<file
->_cnt
; i
++) {
914 if (file
->_ptr
[i
] == '\n')
917 /* Black magic when reading CR at buffer boundary*/
918 if(fdesc
[file
->_file
].wxflag
& WX_READCR
)
922 /* Discard buffered input */
924 file
->_ptr
= file
->_base
;
925 /* Reset direction of i/o */
926 if(file
->_flag
& _IORW
) {
927 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
929 /* Clear end of file flag */
930 file
->_flag
&= ~_IOEOF
;
931 return (_lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
934 /*********************************************************************
937 int CDECL
_chsize(int fd
, long size
)
943 TRACE("(fd=%d, size=%ld)\n", fd
, size
);
948 if (handle
!= INVALID_HANDLE_VALUE
)
950 /* save the current file pointer */
951 cur
= _lseek(fd
, 0, SEEK_CUR
);
954 pos
= _lseek(fd
, size
, SEEK_SET
);
957 ret
= SetEndOfFile(handle
);
958 if (!ret
) _dosmaperr(GetLastError());
961 /* restore the file pointer */
962 _lseek(fd
, cur
, SEEK_SET
);
970 /*********************************************************************
971 * clearerr (MSVCRT.@)
973 void CDECL
clearerr(FILE* file
)
975 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
976 file
->_flag
&= ~(_IOERR
| _IOEOF
);
979 /*********************************************************************
982 void CDECL
rewind(FILE* file
)
984 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
985 fseek(file
, 0L, SEEK_SET
);
989 static int get_flags(const char* mode
, int *open_flags
, int* stream_flags
)
991 int plus
= strchr(mode
, '+') != NULL
;
996 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
997 *stream_flags
= plus
? _IORW
: _IOREAD
;
1000 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1001 *stream_flags
= plus
? _IORW
: _IOWRT
;
1004 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1005 *stream_flags
= plus
? _IORW
: _IOWRT
;
1015 *open_flags
|= _O_BINARY
;
1016 *open_flags
&= ~_O_TEXT
;
1019 *open_flags
|= _O_TEXT
;
1020 *open_flags
&= ~_O_BINARY
;
1025 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1030 /*********************************************************************
1031 * _fdopen (MSVCRT.@)
1033 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1035 int open_flags
, stream_flags
;
1038 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1041 if (!(file
= alloc_fp()))
1043 else if (init_fp(file
, fd
, stream_flags
) == -1)
1048 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
1054 /*********************************************************************
1055 * _wfdopen (MSVCRT.@)
1057 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1059 unsigned mlen
= strlenW(mode
);
1060 char *modea
= calloc(mlen
+ 1, 1);
1062 int open_flags
, stream_flags
;
1065 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
1067 if (get_flags(modea
, &open_flags
, &stream_flags
) == -1)
1073 if (!(file
= alloc_fp()))
1075 else if (init_fp(file
, fd
, stream_flags
) == -1)
1083 rewind(file
); /* FIXME: is this needed ??? */
1084 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
1092 /*********************************************************************
1093 * _filelength (MSVCRT.@)
1095 LONG CDECL
_filelength(int fd
)
1097 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1100 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1103 if (endPos
!= curPos
)
1104 _lseek(fd
, curPos
, SEEK_SET
);
1111 /*********************************************************************
1112 * _filelengthi64 (MSVCRT.@)
1114 __int64 CDECL
_filelengthi64(int fd
)
1116 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1119 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1122 if (endPos
!= curPos
)
1123 _lseeki64(fd
, curPos
, SEEK_SET
);
1130 /*********************************************************************
1131 * _fileno (MSVCRT.@)
1133 int CDECL
_fileno(FILE* file
)
1135 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1139 /*********************************************************************
1140 * _futime (MSVCRT.@)
1142 int CDECL
_futime(int fd
, struct _utimbuf
*t
)
1144 HANDLE hand
= fdtoh(fd
);
1147 if (hand
== INVALID_HANDLE_VALUE
)
1154 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
1159 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
1160 if (t
->actime
== t
->modtime
)
1163 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
1166 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1168 _dosmaperr(GetLastError());
1174 /*********************************************************************
1175 * _get_osfhandle (MSVCRT.@)
1177 intptr_t CDECL
_get_osfhandle(int fd
)
1179 HANDLE hand
= fdtoh(fd
);
1180 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1182 return (long)(LONG_PTR
)hand
;
1185 /*********************************************************************
1186 * _isatty (MSVCRT.@)
1188 int CDECL
_isatty(int fd
)
1190 HANDLE hand
= fdtoh(fd
);
1192 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1193 if (hand
== INVALID_HANDLE_VALUE
)
1196 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1199 /*********************************************************************
1200 * _mktemp (MSVCRT.@)
1202 char * CDECL
_mktemp(char *pattern
)
1205 char *retVal
= pattern
;
1210 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1214 id
= GetCurrentProcessId();
1218 int tempNum
= id
/ 10;
1219 *pattern
-- = id
- (tempNum
* 10) + '0';
1225 *pattern
= letter
++;
1226 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1227 GetLastError() == ERROR_FILE_NOT_FOUND
)
1229 } while(letter
<= 'z');
1233 /*********************************************************************
1234 * _wmktemp (MSVCRT.@)
1236 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1239 wchar_t *retVal
= pattern
;
1241 wchar_t letter
= 'a';
1244 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1248 id
= GetCurrentProcessId();
1252 int tempNum
= id
/ 10;
1253 *pattern
-- = id
- (tempNum
* 10) + '0';
1259 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1260 GetLastError() == ERROR_FILE_NOT_FOUND
)
1262 *pattern
= letter
++;
1263 } while(letter
!= '|');
1267 /*static */unsigned split_oflags(unsigned oflags
)
1270 unsigned unsupp
; /* until we support everything */
1272 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1273 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1274 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1275 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1276 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1277 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1279 if ((unsupp
= oflags
& ~(
1280 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1281 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1282 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1284 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1286 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1291 /*********************************************************************
1294 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1297 SECURITY_ATTRIBUTES sa
;
1298 HANDLE readHandle
, writeHandle
;
1306 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1307 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1308 sa
.lpSecurityDescriptor
= NULL
;
1309 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1311 unsigned int wxflags
= split_oflags(textmode
);
1315 fd
= alloc_fd(readHandle
, wxflags
);
1319 fd
= alloc_fd(writeHandle
, wxflags
);
1328 CloseHandle(writeHandle
);
1334 CloseHandle(readHandle
);
1335 CloseHandle(writeHandle
);
1341 _dosmaperr(GetLastError());
1346 /*********************************************************************
1349 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1353 DWORD access
= 0, creation
= 0, attrib
;
1357 SECURITY_ATTRIBUTES sa
;
1360 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1361 path
, oflags
, shflags
);
1363 wxflag
= split_oflags(oflags
);
1364 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1366 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1367 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1368 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1371 if (oflags
& _O_CREAT
)
1373 va_start(ap
, shflags
);
1374 pmode
= va_arg(ap
, int);
1377 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1378 FIXME(": pmode 0x%04x ignored\n", pmode
);
1380 WARN(": pmode 0x%04x ignored\n", pmode
);
1382 if (oflags
& _O_EXCL
)
1383 creation
= CREATE_NEW
;
1384 else if (oflags
& _O_TRUNC
)
1385 creation
= CREATE_ALWAYS
;
1387 creation
= OPEN_ALWAYS
;
1389 else /* no _O_CREAT */
1391 if (oflags
& _O_TRUNC
)
1392 creation
= TRUNCATE_EXISTING
;
1394 creation
= OPEN_EXISTING
;
1403 sharing
= FILE_SHARE_READ
;
1406 sharing
= FILE_SHARE_WRITE
;
1409 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1412 ERR( "Unhandled shflags 0x%x\n", shflags
);
1415 attrib
= FILE_ATTRIBUTE_NORMAL
;
1417 if (oflags
& _O_TEMPORARY
)
1419 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1421 sharing
|= FILE_SHARE_DELETE
;
1424 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1425 sa
.lpSecurityDescriptor
= NULL
;
1426 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1428 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1430 if (hand
== INVALID_HANDLE_VALUE
) {
1431 WARN(":failed-last error (%d)\n",GetLastError());
1432 _dosmaperr(GetLastError());
1436 fd
= alloc_fd(hand
, wxflag
);
1438 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1442 /*********************************************************************
1443 * _wsopen (MSVCRT.@)
1445 int CDECL
_wsopen( const wchar_t* path
, int oflags
, int shflags
, ... )
1447 const unsigned int len
= strlenW(path
);
1448 char *patha
= calloc(len
+ 1,1);
1452 va_start(ap
, shflags
);
1453 pmode
= va_arg(ap
, int);
1456 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1458 int retval
= _sopen(patha
,oflags
,shflags
,pmode
);
1462 _dosmaperr(GetLastError());
1467 /*********************************************************************
1470 int CDECL
_open( const char *path
, int flags
, ... )
1474 if (flags
& _O_CREAT
)
1477 va_start(ap
, flags
);
1478 pmode
= va_arg(ap
, int);
1480 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1483 return _sopen( path
, flags
, _SH_DENYNO
);
1486 /*********************************************************************
1489 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1491 const unsigned int len
= strlenW(path
);
1492 char *patha
= calloc(len
+ 1,1);
1496 va_start(ap
, flags
);
1497 pmode
= va_arg(ap
, int);
1500 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1502 int retval
= _open(patha
,flags
,pmode
);
1507 _dosmaperr(GetLastError());
1511 /*********************************************************************
1514 int CDECL
_creat(const char *path
, int flags
)
1516 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1517 return _open(path
, usedFlags
);
1520 /*********************************************************************
1521 * _wcreat (MSVCRT.@)
1523 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1525 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1526 return _wopen(path
, usedFlags
);
1529 /*********************************************************************
1530 * _open_osfhandle (MSVCRT.@)
1532 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1536 /* _O_RDONLY (0) always matches, so set the read flag
1537 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1538 * file, so set the write flag. It also only sets _O_TEXT if it wants
1539 * text - it never sets _O_BINARY.
1541 /* don't let split_oflags() decide the mode if no mode is passed */
1542 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1543 oflags
|= _O_BINARY
;
1545 fd
= alloc_fd((HANDLE
)(LONG_PTR
)handle
, split_oflags(oflags
));
1546 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1550 /*********************************************************************
1553 int CDECL
_rmtmp(void)
1555 int num_removed
= 0, i
;
1558 for (i
= 3; i
< stream_idx
; i
++)
1559 if (fstreams
[i
] && fstreams
[i
]->_tmpfname
)
1561 fclose(fstreams
[i
]);
1567 TRACE(":removed (%d) temp files\n",num_removed
);
1571 /*********************************************************************
1574 static int read_i(int fd
, void *buf
, unsigned int count
)
1577 char *bufstart
= buf
;
1578 HANDLE hand
= fdtoh(fd
);
1583 if (fdesc
[fd
].wxflag
& WX_READEOF
) {
1584 fdesc
[fd
].wxflag
|= WX_ATEOF
;
1585 TRACE("already at EOF, returning 0\n");
1588 /* Don't trace small reads, it gets *very* annoying */
1590 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1591 if (hand
== INVALID_HANDLE_VALUE
)
1594 /* Reading single bytes in O_TEXT mode makes things slow
1595 * So read big chunks
1597 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1599 if (count
!= 0 && num_read
== 0)
1601 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1602 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1604 else if (fdesc
[fd
].wxflag
& WX_TEXT
)
1607 if (bufstart
[num_read
-1] == '\r')
1611 fdesc
[fd
].wxflag
&= ~WX_READCR
;
1612 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1616 fdesc
[fd
].wxflag
|= WX_READCR
;
1621 fdesc
[fd
].wxflag
&= ~WX_READCR
;
1622 for (i
=0, j
=0; i
<num_read
; i
++)
1624 /* in text mode, a ctrl-z signals EOF */
1625 if (bufstart
[i
] == 0x1a)
1627 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1628 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1631 /* in text mode, strip \r if followed by \n.
1632 * BUG: should save state across calls somehow, so CR LF that
1633 * straddles buffer boundary gets recognized properly?
1635 if ((bufstart
[i
] != '\r')
1636 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1637 bufstart
[j
++] = bufstart
[i
];
1644 if (GetLastError() == ERROR_BROKEN_PIPE
)
1646 TRACE(":end-of-pipe\n");
1647 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1652 TRACE(":failed-last error (%d)\n",GetLastError());
1658 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1662 /*********************************************************************
1665 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
1668 num_read
= read_i(fd
, buf
, count
);
1672 /*********************************************************************
1673 * _setmode (MSVCRT.@)
1675 int CDECL
_setmode(int fd
,int mode
)
1677 int ret
= fdesc
[fd
].wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
1678 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1679 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1680 if ((mode
& _O_TEXT
) == _O_TEXT
)
1681 fdesc
[fd
].wxflag
|= WX_TEXT
;
1683 fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1687 /*********************************************************************
1690 long CDECL
_tell(int fd
)
1692 return _lseek(fd
, 0, SEEK_CUR
);
1695 /*********************************************************************
1696 * _telli64 (MSVCRT.@)
1698 __int64 CDECL
_telli64(int fd
)
1700 return _lseeki64(fd
, 0, SEEK_CUR
);
1703 /*********************************************************************
1704 * _tempnam (MSVCRT.@)
1706 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
1708 char tmpbuf
[MAX_PATH
];
1709 const char *tmp_dir
= getenv("TMP");
1711 if (tmp_dir
) dir
= tmp_dir
;
1713 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1714 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1716 TRACE("got name (%s)\n",tmpbuf
);
1717 DeleteFileA(tmpbuf
);
1718 return _strdup(tmpbuf
);
1720 TRACE("failed (%d)\n",GetLastError());
1724 /*********************************************************************
1725 * _wtempnam (MSVCRT.@)
1727 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
1729 wchar_t tmpbuf
[MAX_PATH
];
1731 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1732 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1734 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1735 DeleteFileW(tmpbuf
);
1736 return _wcsdup(tmpbuf
);
1738 TRACE("failed (%d)\n",GetLastError());
1742 /*********************************************************************
1745 int CDECL
_umask(int umask
)
1747 int old_umask
= umask
;
1748 TRACE("(%d)\n",umask
);
1749 MSVCRT_umask
= umask
;
1753 /*********************************************************************
1756 int CDECL
_utime(const char* path
, struct _utimbuf
*t
)
1758 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
1762 int retVal
= _futime(fd
, t
);
1769 /*********************************************************************
1770 * _wutime (MSVCRT.@)
1772 int CDECL
_wutime(const wchar_t* path
, struct _utimbuf
*t
)
1774 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
1778 int retVal
= _futime(fd
, t
);
1785 /*********************************************************************
1788 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
1791 HANDLE hand
= fdtoh(fd
);
1793 /* Don't trace small writes, it gets *very* annoying */
1796 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1798 if (hand
== INVALID_HANDLE_VALUE
)
1804 /* If appending, go to EOF */
1805 if (fdesc
[fd
].wxflag
& WX_APPEND
)
1806 _lseek(fd
, 0, FILE_END
);
1808 if (!(fdesc
[fd
].wxflag
& WX_TEXT
))
1810 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1811 && (num_written
== count
))
1813 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
1814 hand
, GetLastError());
1819 unsigned int i
, j
, nr_lf
;
1822 const char *s
= (const char *)buf
, *buf_start
= (const char *)buf
;
1823 /* find number of \n ( without preceding \r ) */
1824 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
1829 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1834 if ((q
= p
= malloc(count
+ nr_lf
)))
1836 for (s
= (const char *)buf
, i
= 0, j
= 0; i
< count
; i
++)
1841 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1848 FIXME("Malloc failed\n");
1856 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
1858 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
1859 fd
, hand
, GetLastError(), num_written
);
1863 return s
- buf_start
;
1875 /*********************************************************************
1878 int CDECL
_putw(int val
, FILE* file
)
1881 len
= _write(file
->_file
, &val
, sizeof(val
));
1882 if (len
== sizeof(val
)) return val
;
1883 file
->_flag
|= _IOERR
;
1887 /*********************************************************************
1890 int CDECL
fclose(FILE* file
)
1895 free(file
->_tmpfname
);
1896 file
->_tmpfname
= NULL
;
1897 /* flush stdio buffers */
1898 if(file
->_flag
& _IOWRT
)
1900 if(file
->_flag
& _IOMYBUF
)
1903 r
=_close(file
->_file
);
1907 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
1910 /*********************************************************************
1913 int CDECL
feof(FILE* file
)
1915 return file
->_flag
& _IOEOF
;
1918 /*********************************************************************
1921 int CDECL
ferror(FILE* file
)
1923 return file
->_flag
& _IOERR
;
1926 /*********************************************************************
1927 * _filbuf (MSVCRT.@)
1929 int CDECL
_filbuf(FILE* file
)
1931 /* Allocate buffer if needed */
1932 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
) ) {
1935 if(!(file
->_flag
& _IOREAD
)) {
1936 if(file
->_flag
& _IORW
) {
1937 file
->_flag
|= _IOREAD
;
1942 if(file
->_flag
& _IONBF
) {
1945 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
1946 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
1951 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
1953 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
1958 file
->_ptr
= file
->_base
+1;
1959 return *(unsigned char *)file
->_base
;
1963 /*********************************************************************
1966 int CDECL
fgetc(FILE* file
)
1972 i
= (unsigned char *)file
->_ptr
++;
1979 /*********************************************************************
1980 * _fgetchar (MSVCRT.@)
1982 int CDECL
_fgetchar(void)
1984 return fgetc(stdin
);
1987 /*********************************************************************
1990 char * CDECL
fgets(char *s
, int size
, FILE* file
)
1993 char * buf_start
= s
;
1995 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1996 file
,file
->_file
,s
,size
);
1998 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2003 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2005 TRACE(":nothing read\n");
2008 if ((cc
!= EOF
) && (size
> 1))
2011 TRACE(":got %s\n", debugstr_a(buf_start
));
2015 /*********************************************************************
2018 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2019 * the CR from CR/LF combinations
2021 wint_t CDECL
fgetwc(FILE* file
)
2025 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2031 for(i
=0; i
<sizeof(wc
); i
++)
2044 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2055 if ((*__p___mb_cur_max() > 1) && isleadbyte(c
))
2057 FIXME("Treat Multibyte characters\n");
2065 /*********************************************************************
2068 int CDECL
_getw(FILE* file
)
2073 for (j
=0; j
<sizeof(int); j
++) {
2076 file
->_flag
|= _IOEOF
;
2084 /*********************************************************************
2087 wint_t CDECL
getwc(FILE* file
)
2089 return fgetwc(file
);
2092 /*********************************************************************
2093 * _fgetwchar (MSVCRT.@)
2095 wint_t CDECL
_fgetwchar(void)
2097 return fgetwc(stdin
);
2100 /*********************************************************************
2101 * getwchar (MSVCRT.@)
2103 wint_t CDECL
getwchar(void)
2105 return _fgetwchar();
2108 /*********************************************************************
2111 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2114 wchar_t * buf_start
= s
;
2116 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2117 file
,file
->_file
,s
,size
);
2119 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2124 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2126 TRACE(":nothing read\n");
2129 if ((cc
!= WEOF
) && (size
> 1))
2132 TRACE(":got %s\n", debugstr_w(buf_start
));
2136 /*********************************************************************
2139 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2141 size_t wrcnt
=size
* nmemb
;
2146 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2147 memcpy(file
->_ptr
, ptr
, pcnt
);
2152 ptr
= (const char*)ptr
+ pcnt
;
2153 } else if(!(file
->_flag
& _IOWRT
)) {
2154 if(file
->_flag
& _IORW
) {
2155 file
->_flag
|= _IOWRT
;
2161 int res
=flush_buffer(file
);
2163 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2166 file
->_flag
|= _IOERR
;
2169 written
+= pwritten
;
2172 return written
/ size
;
2175 /*********************************************************************
2178 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2180 /* If this is a real file stream (and not some temporary one for
2181 sprintf-like functions), check whether it is opened in text mode.
2182 In this case, we have to perform an implicit conversion to ANSI. */
2183 if (!(stream
->_flag
& _IOSTRG
) && fdesc
[stream
->_file
].wxflag
& WX_TEXT
)
2185 /* Convert to multibyte in text mode */
2186 char mbc
[MB_LEN_MAX
];
2189 mb_return
= wctomb(mbc
, c
);
2194 /* Output all characters */
2195 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2200 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2207 /*********************************************************************
2208 * _fputwchar (MSVCRT.@)
2210 wint_t CDECL
_fputwchar(wint_t wc
)
2212 return fputwc(wc
, stdout
);
2215 /*********************************************************************
2216 * _fsopen (MSVCRT.@)
2218 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2221 int open_flags
, stream_flags
, fd
;
2223 TRACE("(%s,%s)\n",path
,mode
);
2225 /* map mode string to open() flags. "man fopen" for possibilities. */
2226 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2230 fd
= _sopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2233 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2235 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
2242 TRACE(":got (%p)\n",file
);
2243 if (fd
>= 0 && !file
)
2249 /*********************************************************************
2250 * _wfsopen (MSVCRT.@)
2252 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2254 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
2255 char *patha
= calloc(plen
+ 1, 1);
2256 char *modea
= calloc(mlen
+ 1, 1);
2258 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
2260 if (patha
&& modea
&&
2261 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
2262 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
2264 FILE *retval
= _fsopen(patha
,modea
,share
);
2271 _dosmaperr(GetLastError());
2275 /*********************************************************************
2278 FILE * CDECL
fopen(const char *path
, const char *mode
)
2280 return _fsopen( path
, mode
, _SH_DENYNO
);
2283 /*********************************************************************
2284 * _wfopen (MSVCRT.@)
2286 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2288 return _wfsopen( path
, mode
, _SH_DENYNO
);
2291 /* fputc calls _flsbuf which calls fputc */
2292 int CDECL
_flsbuf(int c
, FILE* file
);
2294 /*********************************************************************
2297 int CDECL
fputc(int c
, FILE* file
)
2304 int res
= flush_buffer(file
);
2305 return res
? res
: c
;
2310 return _flsbuf(c
, file
);
2314 /*********************************************************************
2315 * _fputchar (MSVCRT.@)
2317 int CDECL
_fputchar(int c
)
2319 return fputc(c
, stdout
);
2322 /*********************************************************************
2325 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2326 { size_t rcnt
=size
* nmemb
;
2333 /* first buffered data */
2335 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2336 memcpy(ptr
, file
->_ptr
, pcnt
);
2341 ptr
= (char*)ptr
+ pcnt
;
2342 } else if(!(file
->_flag
& _IOREAD
)) {
2343 if(file
->_flag
& _IORW
) {
2344 file
->_flag
|= _IOREAD
;
2351 /* Fill the buffer on small reads.
2352 * TODO: Use a better buffering strategy.
2354 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2355 if (file
->_bufsiz
== 0) {
2358 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2359 file
->_ptr
= file
->_base
;
2360 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2361 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2362 if (i
> 0 && i
< file
->_cnt
) {
2363 fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2364 file
->_flag
&= ~_IOEOF
;
2367 memcpy(ptr
, file
->_ptr
, i
);
2372 i
= _read(file
->_file
,ptr
, rcnt
);
2376 ptr
= (char *)ptr
+i
;
2377 /* expose feof condition in the flags
2378 * MFC tests file->_flag for feof, and doesn't call feof())
2380 if ( fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2381 file
->_flag
|= _IOEOF
;
2384 file
->_flag
|= _IOERR
;
2394 /*********************************************************************
2395 * freopen (MSVCRT.@)
2398 FILE* CDECL
freopen(const char *path
, const char *mode
,FILE* file
)
2400 int open_flags
, stream_flags
, fd
;
2402 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2405 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2410 /* map mode string to open() flags. "man fopen" for possibilities. */
2411 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2415 fd
= _open(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2418 else if (init_fp(file
, fd
, stream_flags
) == -1)
2421 WARN(":failed-last error (%d)\n",GetLastError());
2422 _dosmaperr(GetLastError());
2431 /*********************************************************************
2432 * wfreopen (MSVCRT.@)
2435 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
,FILE* file
)
2437 int open_flags
, stream_flags
, fd
;
2439 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2442 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2447 /* map mode string to open() flags. "man fopen" for possibilities. */
2448 if (get_flags((char*)mode
, &open_flags
, &stream_flags
) == -1)
2452 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2455 else if (init_fp(file
, fd
, stream_flags
) == -1)
2458 WARN(":failed-last error (%d)\n",GetLastError());
2459 _dosmaperr(GetLastError());
2468 /*********************************************************************
2469 * fsetpos (MSVCRT.@)
2471 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2473 /* Note that all this has been lifted 'as is' from fseek */
2474 if(file
->_flag
& _IOWRT
)
2477 /* Discard buffered input */
2479 file
->_ptr
= file
->_base
;
2481 /* Reset direction of i/o */
2482 if(file
->_flag
& _IORW
) {
2483 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2486 return (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2489 /*********************************************************************
2492 LONG CDECL
ftell(FILE* file
)
2494 /* TODO: just call fgetpos and return lower half of result */
2497 pos
= _tell(file
->_file
);
2498 if(pos
== -1) return -1;
2500 if( file
->_flag
& _IOWRT
) {
2501 off
= file
->_ptr
- file
->_base
;
2504 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2505 /* Black magic correction for CR removal */
2507 for (i
=0; i
<file
->_cnt
; i
++) {
2508 if (file
->_ptr
[i
] == '\n')
2517 /*********************************************************************
2518 * fgetpos (MSVCRT.@)
2520 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2523 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
2524 if(*pos
== -1) return -1;
2526 if( file
->_flag
& _IOWRT
) {
2527 off
= file
->_ptr
- file
->_base
;
2530 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2531 /* Black magic correction for CR removal */
2533 for (i
=0; i
<file
->_cnt
; i
++) {
2534 if (file
->_ptr
[i
] == '\n')
2544 /*********************************************************************
2547 int CDECL
fputs(const char *s
, FILE* file
)
2549 size_t i
, len
= strlen(s
);
2550 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2551 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2552 for (i
=0; i
<len
; i
++)
2553 if (fputc(s
[i
], file
) == EOF
)
2558 /*********************************************************************
2561 int CDECL
fputws(const wchar_t *s
, FILE* file
)
2563 size_t i
, len
= strlenW(s
);
2564 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2565 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2566 for (i
=0; i
<len
; i
++)
2568 if ((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
2570 if (fputwc(s
[i
], file
) == WEOF
)
2576 /*********************************************************************
2577 * getchar (MSVCRT.@)
2579 int CDECL
getchar(void)
2581 return fgetc(stdin
);
2584 /*********************************************************************
2587 int CDECL
getc(FILE* file
)
2592 /*********************************************************************
2595 char * CDECL
gets(char *buf
)
2598 char * buf_start
= buf
;
2600 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
2602 if(cc
!= '\r') *buf
++ = (char)cc
;
2606 TRACE("got '%s'\n", buf_start
);
2610 /*********************************************************************
2613 wchar_t* CDECL
_getws(wchar_t* buf
)
2618 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
2622 *buf
++ = (wchar_t)cc
;
2626 TRACE("got %s\n", debugstr_w(ws
));
2630 /*********************************************************************
2633 int CDECL
putc(int c
, FILE* file
)
2635 return fputc(c
, file
);
2638 /*********************************************************************
2639 * putchar (MSVCRT.@)
2641 int CDECL
putchar(int c
)
2643 return fputc(c
, stdout
);
2646 /*********************************************************************
2649 int CDECL
puts(const char *s
)
2651 size_t len
= strlen(s
);
2652 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2653 return fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
2656 /*********************************************************************
2659 int CDECL
_putws(const wchar_t *s
)
2661 static const wchar_t nl
= '\n';
2662 size_t len
= strlenW(s
);
2663 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2664 return fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
2667 /*********************************************************************
2670 int CDECL
remove(const char *path
)
2672 TRACE("(%s)\n",path
);
2673 if (DeleteFileA(path
))
2675 TRACE(":failed (%d)\n",GetLastError());
2676 _dosmaperr(GetLastError());
2680 /*********************************************************************
2681 * _wremove (MSVCRT.@)
2683 int CDECL
_wremove(const wchar_t *path
)
2685 TRACE("(%s)\n",debugstr_w(path
));
2686 if (DeleteFileW(path
))
2688 TRACE(":failed (%d)\n",GetLastError());
2689 _dosmaperr(GetLastError());
2693 /*********************************************************************
2696 int CDECL
rename(const char *oldpath
,const char *newpath
)
2698 TRACE(":from %s to %s\n",oldpath
,newpath
);
2699 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2701 TRACE(":failed (%d)\n",GetLastError());
2702 _dosmaperr(GetLastError());
2706 /*********************************************************************
2707 * _wrename (MSVCRT.@)
2709 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
2711 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2712 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2714 TRACE(":failed (%d)\n",GetLastError());
2715 _dosmaperr(GetLastError());
2719 /*********************************************************************
2720 * setvbuf (MSVCRT.@)
2722 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
2724 /* TODO: Check if file busy */
2730 if(mode
== _IOFBF
) {
2731 file
->_flag
&= ~_IONBF
;
2732 file
->_base
= file
->_ptr
= buf
;
2734 file
->_bufsiz
= size
;
2737 file
->_flag
|= _IONBF
;
2742 /*********************************************************************
2745 void CDECL
setbuf(FILE* file
, char *buf
)
2747 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
2750 /*********************************************************************
2753 char * CDECL
tmpnam(char *s
)
2761 int_to_base32(GetCurrentProcessId(), tmpstr
);
2762 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
2763 for (count
= 0; count
< TMP_MAX
; count
++)
2765 int_to_base32(unique
++, tmpstr
);
2767 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
2768 GetLastError() == ERROR_FILE_NOT_FOUND
)
2774 /*********************************************************************
2775 * wtmpnam (MSVCRT.@)
2777 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
2779 ERR("UNIMPLEMENTED!\n");
2783 /*********************************************************************
2784 * tmpfile (MSVCRT.@)
2786 FILE* CDECL
tmpfile(void)
2788 char *filename
= tmpnam(NULL
);
2793 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
2794 if (fd
!= -1 && (file
= alloc_fp()))
2796 if (init_fp(file
, fd
, _O_RDWR
) == -1)
2801 else file
->_tmpfname
= _strdup(filename
);
2807 /*********************************************************************
2810 int CDECL
ungetc(int c
, FILE * file
)
2814 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
)) {
2818 if(file
->_ptr
>file
->_base
) {
2828 /*********************************************************************
2829 * ungetwc (MSVCRT.@)
2831 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
2834 char * pp
= (char *)&mwc
;
2836 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
2837 if(pp
[i
] != ungetc(pp
[i
],file
))
2843 /*********************************************************************
2844 * _getmaxstdio (MSVCRT.@)
2846 int CDECL
_getmaxstdio(void)
2848 FIXME("stub, always returns 512\n");
2852 /*********************************************************************
2853 * _setmaxstdio_ (MSVCRT.@)
2855 int CDECL
_setmaxstdio(int newmax
)
2862 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
2866 /*********************************************************************
2867 * __pioinfo (MSVCRT.@)
2868 * FIXME: see MAX_FILES define.
2870 ioinfo
* __pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
2871 &fdesc
[0 * 64], &fdesc
[1 * 64], &fdesc
[2 * 64],
2872 &fdesc
[3 * 64], &fdesc
[4 * 64], &fdesc
[5 * 64],
2873 &fdesc
[6 * 64], &fdesc
[7 * 64], &fdesc
[8 * 64],
2874 &fdesc
[9 * 64], &fdesc
[10 * 64], &fdesc
[11 * 64],
2875 &fdesc
[12 * 64], &fdesc
[13 * 64], &fdesc
[14 * 64],
2876 &fdesc
[15 * 64], &fdesc
[16 * 64], &fdesc
[17 * 64],
2877 &fdesc
[18 * 64], &fdesc
[19 * 64], &fdesc
[20 * 64],
2878 &fdesc
[21 * 64], &fdesc
[22 * 64], &fdesc
[23 * 64],
2879 &fdesc
[24 * 64], &fdesc
[25 * 64], &fdesc
[26 * 64],
2880 &fdesc
[27 * 64], &fdesc
[28 * 64], &fdesc
[29 * 64],
2881 &fdesc
[30 * 64], &fdesc
[31 * 64]
2884 /*********************************************************************
2885 * __badioinfo (MSVCRT.@)
2887 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
, { 0, } };