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
= (WORD
)(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
= (int)(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 (LONG
)_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
= (unsigned)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 * _get_osfhandle (MSVCRT.@)
1142 intptr_t CDECL
_get_osfhandle(int fd
)
1144 HANDLE hand
= fdtoh(fd
);
1145 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1147 return (long)(LONG_PTR
)hand
;
1150 /*********************************************************************
1151 * _isatty (MSVCRT.@)
1153 int CDECL
_isatty(int fd
)
1155 HANDLE hand
= fdtoh(fd
);
1157 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1158 if (hand
== INVALID_HANDLE_VALUE
)
1161 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1164 /*********************************************************************
1165 * _mktemp (MSVCRT.@)
1167 char * CDECL
_mktemp(char *pattern
)
1170 char *retVal
= pattern
;
1175 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1179 id
= GetCurrentProcessId();
1183 int tempNum
= id
/ 10;
1184 *pattern
-- = id
- (tempNum
* 10) + '0';
1190 *pattern
= letter
++;
1191 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1192 GetLastError() == ERROR_FILE_NOT_FOUND
)
1194 } while(letter
<= 'z');
1198 /*********************************************************************
1199 * _wmktemp (MSVCRT.@)
1201 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1204 wchar_t *retVal
= pattern
;
1206 wchar_t letter
= 'a';
1209 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1213 id
= GetCurrentProcessId();
1217 int tempNum
= id
/ 10;
1218 *pattern
-- = id
- (tempNum
* 10) + '0';
1224 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1225 GetLastError() == ERROR_FILE_NOT_FOUND
)
1227 *pattern
= letter
++;
1228 } while(letter
!= '|');
1232 /*static */unsigned split_oflags(unsigned oflags
)
1235 unsigned unsupp
; /* until we support everything */
1237 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1238 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1239 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1240 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1241 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1242 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1244 if ((unsupp
= oflags
& ~(
1245 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1246 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1247 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1249 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1251 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1256 /*********************************************************************
1259 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1262 SECURITY_ATTRIBUTES sa
;
1263 HANDLE readHandle
, writeHandle
;
1271 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1272 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1273 sa
.lpSecurityDescriptor
= NULL
;
1274 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1276 unsigned int wxflags
= split_oflags(textmode
);
1280 fd
= alloc_fd(readHandle
, wxflags
);
1284 fd
= alloc_fd(writeHandle
, wxflags
);
1293 CloseHandle(writeHandle
);
1299 CloseHandle(readHandle
);
1300 CloseHandle(writeHandle
);
1306 _dosmaperr(GetLastError());
1311 /*********************************************************************
1314 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1318 DWORD access
= 0, creation
= 0, attrib
;
1322 SECURITY_ATTRIBUTES sa
;
1325 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1326 path
, oflags
, shflags
);
1328 wxflag
= split_oflags(oflags
);
1329 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1331 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1332 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1333 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1336 if (oflags
& _O_CREAT
)
1338 va_start(ap
, shflags
);
1339 pmode
= va_arg(ap
, int);
1342 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1343 FIXME(": pmode 0x%04x ignored\n", pmode
);
1345 WARN(": pmode 0x%04x ignored\n", pmode
);
1347 if (oflags
& _O_EXCL
)
1348 creation
= CREATE_NEW
;
1349 else if (oflags
& _O_TRUNC
)
1350 creation
= CREATE_ALWAYS
;
1352 creation
= OPEN_ALWAYS
;
1354 else /* no _O_CREAT */
1356 if (oflags
& _O_TRUNC
)
1357 creation
= TRUNCATE_EXISTING
;
1359 creation
= OPEN_EXISTING
;
1368 sharing
= FILE_SHARE_READ
;
1371 sharing
= FILE_SHARE_WRITE
;
1374 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1377 ERR( "Unhandled shflags 0x%x\n", shflags
);
1380 attrib
= FILE_ATTRIBUTE_NORMAL
;
1382 if (oflags
& _O_TEMPORARY
)
1384 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1386 sharing
|= FILE_SHARE_DELETE
;
1389 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1390 sa
.lpSecurityDescriptor
= NULL
;
1391 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1393 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1395 if (hand
== INVALID_HANDLE_VALUE
) {
1396 WARN(":failed-last error (%d)\n",GetLastError());
1397 _dosmaperr(GetLastError());
1401 fd
= alloc_fd(hand
, wxflag
);
1403 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1407 /*********************************************************************
1408 * _wsopen (MSVCRT.@)
1410 int CDECL
_wsopen( const wchar_t* path
, int oflags
, int shflags
, ... )
1412 const unsigned int len
= (unsigned)strlenW(path
);
1413 char *patha
= calloc(len
+ 1,1);
1417 va_start(ap
, shflags
);
1418 pmode
= va_arg(ap
, int);
1421 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1423 int retval
= _sopen(patha
,oflags
,shflags
,pmode
);
1427 _dosmaperr(GetLastError());
1432 /*********************************************************************
1435 int CDECL
_open( const char *path
, int flags
, ... )
1439 if (flags
& _O_CREAT
)
1442 va_start(ap
, flags
);
1443 pmode
= va_arg(ap
, int);
1445 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1448 return _sopen( path
, flags
, _SH_DENYNO
);
1451 /*********************************************************************
1454 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1456 const unsigned int len
= (unsigned)strlenW(path
);
1457 char *patha
= calloc(len
+ 1,1);
1461 va_start(ap
, flags
);
1462 pmode
= va_arg(ap
, int);
1465 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1467 int retval
= _open(patha
,flags
,pmode
);
1472 _dosmaperr(GetLastError());
1476 /*********************************************************************
1479 int CDECL
_creat(const char *path
, int flags
)
1481 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1482 return _open(path
, usedFlags
);
1485 /*********************************************************************
1486 * _wcreat (MSVCRT.@)
1488 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1490 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1491 return _wopen(path
, usedFlags
);
1494 /*********************************************************************
1495 * _open_osfhandle (MSVCRT.@)
1497 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1501 /* _O_RDONLY (0) always matches, so set the read flag
1502 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1503 * file, so set the write flag. It also only sets _O_TEXT if it wants
1504 * text - it never sets _O_BINARY.
1506 /* don't let split_oflags() decide the mode if no mode is passed */
1507 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1508 oflags
|= _O_BINARY
;
1510 fd
= alloc_fd((HANDLE
)(LONG_PTR
)handle
, split_oflags(oflags
));
1511 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1515 /*********************************************************************
1518 int CDECL
_rmtmp(void)
1520 int num_removed
= 0, i
;
1523 for (i
= 3; i
< stream_idx
; i
++)
1524 if (fstreams
[i
] && fstreams
[i
]->_tmpfname
)
1526 fclose(fstreams
[i
]);
1532 TRACE(":removed (%d) temp files\n",num_removed
);
1536 /*********************************************************************
1539 static int read_i(int fd
, void *buf
, unsigned int count
)
1542 char *bufstart
= buf
;
1543 HANDLE hand
= fdtoh(fd
);
1548 if (fdesc
[fd
].wxflag
& WX_READEOF
) {
1549 fdesc
[fd
].wxflag
|= WX_ATEOF
;
1550 TRACE("already at EOF, returning 0\n");
1553 /* Don't trace small reads, it gets *very* annoying */
1555 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1556 if (hand
== INVALID_HANDLE_VALUE
)
1559 /* Reading single bytes in O_TEXT mode makes things slow
1560 * So read big chunks
1562 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1564 if (count
!= 0 && num_read
== 0)
1566 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1567 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1569 else if (fdesc
[fd
].wxflag
& WX_TEXT
)
1572 if (bufstart
[num_read
-1] == '\r')
1576 fdesc
[fd
].wxflag
&= ~WX_READCR
;
1577 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1581 fdesc
[fd
].wxflag
|= WX_READCR
;
1586 fdesc
[fd
].wxflag
&= ~WX_READCR
;
1587 for (i
=0, j
=0; i
<num_read
; i
++)
1589 /* in text mode, a ctrl-z signals EOF */
1590 if (bufstart
[i
] == 0x1a)
1592 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1593 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1596 /* in text mode, strip \r if followed by \n.
1597 * BUG: should save state across calls somehow, so CR LF that
1598 * straddles buffer boundary gets recognized properly?
1600 if ((bufstart
[i
] != '\r')
1601 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1602 bufstart
[j
++] = bufstart
[i
];
1609 if (GetLastError() == ERROR_BROKEN_PIPE
)
1611 TRACE(":end-of-pipe\n");
1612 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1617 TRACE(":failed-last error (%d)\n",GetLastError());
1623 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1627 /*********************************************************************
1630 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
1633 num_read
= read_i(fd
, buf
, count
);
1637 /*********************************************************************
1638 * _setmode (MSVCRT.@)
1640 int CDECL
_setmode(int fd
,int mode
)
1642 int ret
= fdesc
[fd
].wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
1643 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1644 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1645 if ((mode
& _O_TEXT
) == _O_TEXT
)
1646 fdesc
[fd
].wxflag
|= WX_TEXT
;
1648 fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1652 /*********************************************************************
1655 long CDECL
_tell(int fd
)
1657 return _lseek(fd
, 0, SEEK_CUR
);
1660 /*********************************************************************
1661 * _telli64 (MSVCRT.@)
1663 __int64 CDECL
_telli64(int fd
)
1665 return _lseeki64(fd
, 0, SEEK_CUR
);
1668 /*********************************************************************
1669 * _tempnam (MSVCRT.@)
1671 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
1673 char tmpbuf
[MAX_PATH
];
1674 const char *tmp_dir
= getenv("TMP");
1676 if (tmp_dir
) dir
= tmp_dir
;
1678 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1679 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1681 TRACE("got name (%s)\n",tmpbuf
);
1682 DeleteFileA(tmpbuf
);
1683 return _strdup(tmpbuf
);
1685 TRACE("failed (%d)\n",GetLastError());
1689 /*********************************************************************
1690 * _wtempnam (MSVCRT.@)
1692 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
1694 wchar_t tmpbuf
[MAX_PATH
];
1696 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1697 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1699 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1700 DeleteFileW(tmpbuf
);
1701 return _wcsdup(tmpbuf
);
1703 TRACE("failed (%d)\n",GetLastError());
1707 /*********************************************************************
1710 int CDECL
_umask(int umask
)
1712 int old_umask
= umask
;
1713 TRACE("(%d)\n",umask
);
1714 MSVCRT_umask
= umask
;
1718 /*********************************************************************
1721 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
1724 HANDLE hand
= fdtoh(fd
);
1726 /* Don't trace small writes, it gets *very* annoying */
1729 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1731 if (hand
== INVALID_HANDLE_VALUE
)
1737 /* If appending, go to EOF */
1738 if (fdesc
[fd
].wxflag
& WX_APPEND
)
1739 _lseek(fd
, 0, FILE_END
);
1741 if (!(fdesc
[fd
].wxflag
& WX_TEXT
))
1743 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1744 && (num_written
== count
))
1746 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
1747 hand
, GetLastError());
1752 unsigned int i
, j
, nr_lf
;
1755 const char *s
= (const char *)buf
, *buf_start
= (const char *)buf
;
1756 /* find number of \n ( without preceding \r ) */
1757 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
1762 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1767 if ((q
= p
= malloc(count
+ nr_lf
)))
1769 for (s
= (const char *)buf
, i
= 0, j
= 0; i
< count
; i
++)
1774 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1781 FIXME("Malloc failed\n");
1789 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
1791 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
1792 fd
, hand
, GetLastError(), num_written
);
1796 return (int)(s
- buf_start
);
1808 /*********************************************************************
1811 int CDECL
_putw(int val
, FILE* file
)
1814 len
= _write(file
->_file
, &val
, sizeof(val
));
1815 if (len
== sizeof(val
)) return val
;
1816 file
->_flag
|= _IOERR
;
1820 /*********************************************************************
1823 int CDECL
fclose(FILE* file
)
1828 free(file
->_tmpfname
);
1829 file
->_tmpfname
= NULL
;
1830 /* flush stdio buffers */
1831 if(file
->_flag
& _IOWRT
)
1833 if(file
->_flag
& _IOMYBUF
)
1836 r
=_close(file
->_file
);
1840 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
1843 /*********************************************************************
1846 int CDECL
feof(FILE* file
)
1848 return file
->_flag
& _IOEOF
;
1851 /*********************************************************************
1854 int CDECL
ferror(FILE* file
)
1856 return file
->_flag
& _IOERR
;
1859 /*********************************************************************
1860 * _filbuf (MSVCRT.@)
1862 int CDECL
_filbuf(FILE* file
)
1864 /* Allocate buffer if needed */
1865 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
) ) {
1868 if(!(file
->_flag
& _IOREAD
)) {
1869 if(file
->_flag
& _IORW
) {
1870 file
->_flag
|= _IOREAD
;
1875 if(file
->_flag
& _IONBF
) {
1878 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
1879 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
1884 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
1886 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
1891 file
->_ptr
= file
->_base
+1;
1892 return *(unsigned char *)file
->_base
;
1896 /*********************************************************************
1899 int CDECL
fgetc(FILE* file
)
1905 i
= (unsigned char *)file
->_ptr
++;
1912 /*********************************************************************
1913 * _fgetchar (MSVCRT.@)
1915 int CDECL
_fgetchar(void)
1917 return fgetc(stdin
);
1920 /*********************************************************************
1923 char * CDECL
fgets(char *s
, int size
, FILE* file
)
1926 char * buf_start
= s
;
1928 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1929 file
,file
->_file
,s
,size
);
1931 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
1936 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1938 TRACE(":nothing read\n");
1941 if ((cc
!= EOF
) && (size
> 1))
1944 TRACE(":got %s\n", debugstr_a(buf_start
));
1948 /*********************************************************************
1951 * In _O_TEXT mode, multibyte characters are read from the file, dropping
1952 * the CR from CR/LF combinations
1954 wint_t CDECL
fgetwc(FILE* file
)
1958 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
1964 for(i
=0; i
<sizeof(wc
); i
++)
1977 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
1988 if ((*__p___mb_cur_max() > 1) && isleadbyte(c
))
1990 FIXME("Treat Multibyte characters\n");
1998 /*********************************************************************
2001 int CDECL
_getw(FILE* file
)
2006 for (j
=0; j
<sizeof(int); j
++) {
2009 file
->_flag
|= _IOEOF
;
2017 /*********************************************************************
2020 wint_t CDECL
getwc(FILE* file
)
2022 return fgetwc(file
);
2025 /*********************************************************************
2026 * _fgetwchar (MSVCRT.@)
2028 wint_t CDECL
_fgetwchar(void)
2030 return fgetwc(stdin
);
2033 /*********************************************************************
2034 * getwchar (MSVCRT.@)
2036 wint_t CDECL
getwchar(void)
2038 return _fgetwchar();
2041 /*********************************************************************
2044 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2047 wchar_t * buf_start
= s
;
2049 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2050 file
,file
->_file
,s
,size
);
2052 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2057 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2059 TRACE(":nothing read\n");
2062 if ((cc
!= WEOF
) && (size
> 1))
2065 TRACE(":got %s\n", debugstr_w(buf_start
));
2069 /*********************************************************************
2072 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2074 int wrcnt
=(int)(size
* nmemb
);
2079 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2080 memcpy(file
->_ptr
, ptr
, pcnt
);
2085 ptr
= (const char*)ptr
+ pcnt
;
2086 } else if(!(file
->_flag
& _IOWRT
)) {
2087 if(file
->_flag
& _IORW
) {
2088 file
->_flag
|= _IOWRT
;
2094 int res
=flush_buffer(file
);
2096 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2099 file
->_flag
|= _IOERR
;
2102 written
+= pwritten
;
2105 return written
/ size
;
2108 /*********************************************************************
2111 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2113 /* If this is a real file stream (and not some temporary one for
2114 sprintf-like functions), check whether it is opened in text mode.
2115 In this case, we have to perform an implicit conversion to ANSI. */
2116 if (!(stream
->_flag
& _IOSTRG
) && fdesc
[stream
->_file
].wxflag
& WX_TEXT
)
2118 /* Convert to multibyte in text mode */
2119 char mbc
[MB_LEN_MAX
];
2122 mb_return
= wctomb(mbc
, c
);
2127 /* Output all characters */
2128 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2133 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2140 /*********************************************************************
2141 * _fputwchar (MSVCRT.@)
2143 wint_t CDECL
_fputwchar(wint_t wc
)
2145 return fputwc(wc
, stdout
);
2148 /*********************************************************************
2149 * _fsopen (MSVCRT.@)
2151 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2154 int open_flags
, stream_flags
, fd
;
2156 TRACE("(%s,%s)\n",path
,mode
);
2158 /* map mode string to open() flags. "man fopen" for possibilities. */
2159 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2163 fd
= _sopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2166 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2168 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
2175 TRACE(":got (%p)\n",file
);
2176 if (fd
>= 0 && !file
)
2182 /*********************************************************************
2183 * _wfsopen (MSVCRT.@)
2185 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2187 const unsigned int plen
= (unsigned)strlenW(path
), mlen
= (unsigned)strlenW(mode
);
2188 char *patha
= calloc(plen
+ 1, 1);
2189 char *modea
= calloc(mlen
+ 1, 1);
2191 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
2193 if (patha
&& modea
&&
2194 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
2195 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
2197 FILE *retval
= _fsopen(patha
,modea
,share
);
2204 _dosmaperr(GetLastError());
2208 /*********************************************************************
2211 FILE * CDECL
fopen(const char *path
, const char *mode
)
2213 return _fsopen( path
, mode
, _SH_DENYNO
);
2216 /*********************************************************************
2217 * _wfopen (MSVCRT.@)
2219 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2221 return _wfsopen( path
, mode
, _SH_DENYNO
);
2224 /* fputc calls _flsbuf which calls fputc */
2225 int CDECL
_flsbuf(int c
, FILE* file
);
2227 /*********************************************************************
2230 int CDECL
fputc(int c
, FILE* file
)
2237 int res
= flush_buffer(file
);
2238 return res
? res
: c
;
2243 return _flsbuf(c
, file
);
2247 /*********************************************************************
2248 * _fputchar (MSVCRT.@)
2250 int CDECL
_fputchar(int c
)
2252 return fputc(c
, stdout
);
2255 /*********************************************************************
2258 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2259 { int rcnt
=(int)(size
* nmemb
);
2266 /* first buffered data */
2268 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2269 memcpy(ptr
, file
->_ptr
, pcnt
);
2274 ptr
= (char*)ptr
+ pcnt
;
2275 } else if(!(file
->_flag
& _IOREAD
)) {
2276 if(file
->_flag
& _IORW
) {
2277 file
->_flag
|= _IOREAD
;
2284 /* Fill the buffer on small reads.
2285 * TODO: Use a better buffering strategy.
2287 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2288 if (file
->_bufsiz
== 0) {
2291 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2292 file
->_ptr
= file
->_base
;
2293 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2294 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2295 if (i
> 0 && i
< file
->_cnt
) {
2296 fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2297 file
->_flag
&= ~_IOEOF
;
2300 memcpy(ptr
, file
->_ptr
, i
);
2305 i
= _read(file
->_file
,ptr
, rcnt
);
2309 ptr
= (char *)ptr
+i
;
2310 /* expose feof condition in the flags
2311 * MFC tests file->_flag for feof, and doesn't call feof())
2313 if ( fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2314 file
->_flag
|= _IOEOF
;
2317 file
->_flag
|= _IOERR
;
2327 /*********************************************************************
2328 * freopen (MSVCRT.@)
2331 FILE* CDECL
freopen(const char *path
, const char *mode
,FILE* file
)
2333 int open_flags
, stream_flags
, fd
;
2335 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2338 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2343 /* map mode string to open() flags. "man fopen" for possibilities. */
2344 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2348 fd
= _open(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2351 else if (init_fp(file
, fd
, stream_flags
) == -1)
2354 WARN(":failed-last error (%d)\n",GetLastError());
2355 _dosmaperr(GetLastError());
2364 /*********************************************************************
2365 * wfreopen (MSVCRT.@)
2368 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
,FILE* file
)
2370 int open_flags
, stream_flags
, fd
;
2372 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2375 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2380 /* map mode string to open() flags. "man fopen" for possibilities. */
2381 if (get_flags((char*)mode
, &open_flags
, &stream_flags
) == -1)
2385 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2388 else if (init_fp(file
, fd
, stream_flags
) == -1)
2391 WARN(":failed-last error (%d)\n",GetLastError());
2392 _dosmaperr(GetLastError());
2401 /*********************************************************************
2402 * fsetpos (MSVCRT.@)
2404 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2406 /* Note that all this has been lifted 'as is' from fseek */
2407 if(file
->_flag
& _IOWRT
)
2410 /* Discard buffered input */
2412 file
->_ptr
= file
->_base
;
2414 /* Reset direction of i/o */
2415 if(file
->_flag
& _IORW
) {
2416 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2419 return (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2422 /*********************************************************************
2425 LONG CDECL
ftell(FILE* file
)
2427 /* TODO: just call fgetpos and return lower half of result */
2430 pos
= _tell(file
->_file
);
2431 if(pos
== -1) return -1;
2433 if( file
->_flag
& _IOWRT
) {
2434 off
= (int)(file
->_ptr
- file
->_base
);
2437 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2438 /* Black magic correction for CR removal */
2440 for (i
=0; i
<file
->_cnt
; i
++) {
2441 if (file
->_ptr
[i
] == '\n')
2450 /*********************************************************************
2451 * fgetpos (MSVCRT.@)
2453 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2456 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
2457 if(*pos
== -1) return -1;
2459 if( file
->_flag
& _IOWRT
) {
2460 off
= (int)(file
->_ptr
- file
->_base
);
2463 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2464 /* Black magic correction for CR removal */
2466 for (i
=0; i
<file
->_cnt
; i
++) {
2467 if (file
->_ptr
[i
] == '\n')
2477 /*********************************************************************
2480 int CDECL
fputs(const char *s
, FILE* file
)
2482 size_t i
, len
= strlen(s
);
2483 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2484 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2485 for (i
=0; i
<len
; i
++)
2486 if (fputc(s
[i
], file
) == EOF
)
2491 /*********************************************************************
2494 int CDECL
fputws(const wchar_t *s
, FILE* file
)
2496 size_t i
, len
= strlenW(s
);
2497 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2498 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2499 for (i
=0; i
<len
; i
++)
2501 if ((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
2503 if (fputwc(s
[i
], file
) == WEOF
)
2509 /*********************************************************************
2510 * getchar (MSVCRT.@)
2512 int CDECL
getchar(void)
2514 return fgetc(stdin
);
2517 /*********************************************************************
2520 int CDECL
getc(FILE* file
)
2525 /*********************************************************************
2528 char * CDECL
gets(char *buf
)
2531 char * buf_start
= buf
;
2533 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
2535 if(cc
!= '\r') *buf
++ = (char)cc
;
2539 TRACE("got '%s'\n", buf_start
);
2543 /*********************************************************************
2546 wchar_t* CDECL
_getws(wchar_t* buf
)
2551 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
2555 *buf
++ = (wchar_t)cc
;
2559 TRACE("got %s\n", debugstr_w(ws
));
2563 /*********************************************************************
2566 int CDECL
putc(int c
, FILE* file
)
2568 return fputc(c
, file
);
2571 /*********************************************************************
2572 * putchar (MSVCRT.@)
2574 int CDECL
putchar(int c
)
2576 return fputc(c
, stdout
);
2579 /*********************************************************************
2582 int CDECL
puts(const char *s
)
2584 size_t len
= strlen(s
);
2585 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2586 return fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
2589 /*********************************************************************
2592 int CDECL
_putws(const wchar_t *s
)
2594 static const wchar_t nl
= '\n';
2595 size_t len
= strlenW(s
);
2596 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2597 return fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
2600 /*********************************************************************
2603 int CDECL
remove(const char *path
)
2605 TRACE("(%s)\n",path
);
2606 if (DeleteFileA(path
))
2608 TRACE(":failed (%d)\n",GetLastError());
2609 _dosmaperr(GetLastError());
2613 /*********************************************************************
2614 * _wremove (MSVCRT.@)
2616 int CDECL
_wremove(const wchar_t *path
)
2618 TRACE("(%s)\n",debugstr_w(path
));
2619 if (DeleteFileW(path
))
2621 TRACE(":failed (%d)\n",GetLastError());
2622 _dosmaperr(GetLastError());
2626 /*********************************************************************
2629 int CDECL
rename(const char *oldpath
,const char *newpath
)
2631 TRACE(":from %s to %s\n",oldpath
,newpath
);
2632 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2634 TRACE(":failed (%d)\n",GetLastError());
2635 _dosmaperr(GetLastError());
2639 /*********************************************************************
2640 * _wrename (MSVCRT.@)
2642 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
2644 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2645 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2647 TRACE(":failed (%d)\n",GetLastError());
2648 _dosmaperr(GetLastError());
2652 /*********************************************************************
2653 * setvbuf (MSVCRT.@)
2655 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
2657 /* TODO: Check if file busy */
2663 if(mode
== _IOFBF
) {
2664 file
->_flag
&= ~_IONBF
;
2665 file
->_base
= file
->_ptr
= buf
;
2667 file
->_bufsiz
= (int)size
;
2670 file
->_flag
|= _IONBF
;
2675 /*********************************************************************
2678 void CDECL
setbuf(FILE* file
, char *buf
)
2680 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
2683 /*********************************************************************
2686 char * CDECL
tmpnam(char *s
)
2694 int_to_base32(GetCurrentProcessId(), tmpstr
);
2695 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
2696 for (count
= 0; count
< TMP_MAX
; count
++)
2698 int_to_base32(unique
++, tmpstr
);
2700 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
2701 GetLastError() == ERROR_FILE_NOT_FOUND
)
2707 /*********************************************************************
2708 * wtmpnam (MSVCRT.@)
2710 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
2712 ERR("UNIMPLEMENTED!\n");
2716 /*********************************************************************
2717 * tmpfile (MSVCRT.@)
2719 FILE* CDECL
tmpfile(void)
2721 char *filename
= tmpnam(NULL
);
2726 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
2727 if (fd
!= -1 && (file
= alloc_fp()))
2729 if (init_fp(file
, fd
, _O_RDWR
) == -1)
2734 else file
->_tmpfname
= _strdup(filename
);
2740 /*********************************************************************
2743 int CDECL
ungetc(int c
, FILE * file
)
2747 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
)) {
2751 if(file
->_ptr
>file
->_base
) {
2761 /*********************************************************************
2762 * ungetwc (MSVCRT.@)
2764 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
2767 char * pp
= (char *)&mwc
;
2769 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
2770 if(pp
[i
] != ungetc(pp
[i
],file
))
2776 /*********************************************************************
2777 * _getmaxstdio (MSVCRT.@)
2779 int CDECL
_getmaxstdio(void)
2781 FIXME("stub, always returns 512\n");
2785 /*********************************************************************
2786 * _setmaxstdio_ (MSVCRT.@)
2788 int CDECL
_setmaxstdio(int newmax
)
2795 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
2799 /*********************************************************************
2800 * __pioinfo (MSVCRT.@)
2801 * FIXME: see MAX_FILES define.
2803 ioinfo
* __pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
2804 &fdesc
[0 * 64], &fdesc
[1 * 64], &fdesc
[2 * 64],
2805 &fdesc
[3 * 64], &fdesc
[4 * 64], &fdesc
[5 * 64],
2806 &fdesc
[6 * 64], &fdesc
[7 * 64], &fdesc
[8 * 64],
2807 &fdesc
[9 * 64], &fdesc
[10 * 64], &fdesc
[11 * 64],
2808 &fdesc
[12 * 64], &fdesc
[13 * 64], &fdesc
[14 * 64],
2809 &fdesc
[15 * 64], &fdesc
[16 * 64], &fdesc
[17 * 64],
2810 &fdesc
[18 * 64], &fdesc
[19 * 64], &fdesc
[20 * 64],
2811 &fdesc
[21 * 64], &fdesc
[22 * 64], &fdesc
[23 * 64],
2812 &fdesc
[24 * 64], &fdesc
[25 * 64], &fdesc
[26 * 64],
2813 &fdesc
[27 * 64], &fdesc
[28 * 64], &fdesc
[29 * 64],
2814 &fdesc
[30 * 64], &fdesc
[31 * 64]
2817 /*********************************************************************
2818 * __badioinfo (MSVCRT.@)
2820 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
, { 0, } };