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");
181 fdesc
[fd
].handle
= hand
;
182 fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
184 /* locate next free slot */
185 if (fd
== fdstart
&& fd
== fdend
)
188 while (fdstart
< fdend
&&
189 fdesc
[fdstart
].handle
!= INVALID_HANDLE_VALUE
)
191 /* update last fd in use */
194 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
198 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
199 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
200 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
206 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
207 /*static */int alloc_fd(HANDLE hand
, int flag
)
212 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
213 ret
= alloc_fd_from(hand
, flag
, fdstart
);
218 /* INTERNAL: Allocate a FILE* for an fd slot */
219 /* caller must hold the files lock */
220 static FILE* alloc_fp(void)
224 for (i
= 3; i
< sizeof(fstreams
) / sizeof(fstreams
[0]); i
++)
226 if (!fstreams
[i
] || fstreams
[i
]->_flag
== 0)
230 if (!(fstreams
[i
] = calloc(sizeof(FILE),1)))
232 if (i
== stream_idx
) stream_idx
++;
241 /* INTERNAL: initialize a FILE* from an open fd */
242 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
244 TRACE(":fd (%d) allocating FILE*\n",fd
);
245 if (!is_valid_fd(fd
))
247 WARN(":invalid fd %d\n",fd
);
252 memset(file
, 0, sizeof(*file
));
254 file
->_flag
= stream_flags
;
256 TRACE(":got FILE* (%p)\n",file
);
260 /* INTERNAL: Create an inheritance data block (for spawned process)
261 * The inheritance block is made of:
262 * 00 int nb of file descriptor (NBFD)
263 * 04 char file flags (wxflag): repeated for each fd
264 * 4+NBFD HANDLE file handle: repeated for each fd
266 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
272 *size
= (WORD
)(sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
);
273 *block
= calloc(*size
, 1);
279 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
280 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
282 *(unsigned*)*block
= fdend
;
283 for (fd
= 0; fd
< fdend
; fd
++)
285 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
286 if ((fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
288 *wxflag_ptr
= fdesc
[fd
].wxflag
;
289 *handle_ptr
= fdesc
[fd
].handle
;
294 *handle_ptr
= INVALID_HANDLE_VALUE
;
296 wxflag_ptr
++; handle_ptr
++;
301 /* INTERNAL: Set up all file descriptors,
302 * as well as default streams (stdin, stderr and stdout)
304 void msvcrt_init_io(void)
309 InitializeCriticalSection(&FILE_cs
);
310 FILE_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FILE_cs");
311 GetStartupInfoA(&si
);
312 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
318 count
= *(unsigned*)si
.lpReserved2
;
319 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
320 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
322 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
323 count
= min(count
, sizeof(fdesc
) / sizeof(fdesc
[0]));
324 for (i
= 0; i
< count
; i
++)
326 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
328 fdesc
[i
].wxflag
= *wxflag_ptr
;
329 fdesc
[i
].handle
= *handle_ptr
;
334 fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
336 wxflag_ptr
++; handle_ptr
++;
338 fdend
= max( 3, count
);
339 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
340 if (fdesc
[fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
343 if (!(fdesc
[0].wxflag
& WX_OPEN
) || fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
346 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
347 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
348 GetCurrentProcess(), &fdesc
[0].handle
,
349 0, TRUE
, DUPLICATE_SAME_ACCESS
))
351 fdesc
[0].handle
= GetStdHandle(STD_INPUT_HANDLE
);
352 if (fdesc
[0].handle
== NULL
)
353 fdesc
[0].handle
= INVALID_HANDLE_VALUE
;
355 fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
358 if (!(fdesc
[1].wxflag
& WX_OPEN
) || fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
361 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
362 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
363 GetCurrentProcess(), &fdesc
[1].handle
,
364 0, TRUE
, DUPLICATE_SAME_ACCESS
))
366 fdesc
[1].handle
= GetStdHandle(STD_OUTPUT_HANDLE
);
367 if (fdesc
[1].handle
== NULL
)
368 fdesc
[1].handle
= INVALID_HANDLE_VALUE
;
370 fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
373 if (!(fdesc
[2].wxflag
& WX_OPEN
) || fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
376 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
377 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
378 GetCurrentProcess(), &fdesc
[2].handle
,
379 0, TRUE
, DUPLICATE_SAME_ACCESS
))
381 fdesc
[2].handle
= GetStdHandle(STD_ERROR_HANDLE
);
382 if (fdesc
[2].handle
== NULL
)
383 fdesc
[2].handle
= INVALID_HANDLE_VALUE
;
385 fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
388 TRACE(":handles (%p)(%p)(%p)\n", fdesc
[0].handle
,
389 fdesc
[1].handle
, fdesc
[2].handle
);
391 memset(_iob
,0,3*sizeof(FILE));
392 for (i
= 0; i
< 3; i
++)
394 /* FILE structs for stdin/out/err are static and never deleted */
395 fstreams
[i
] = &_iob
[i
];
397 _iob
[i
]._tmpfname
= NULL
;
398 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
403 /* INTERNAL: Flush stdio file buffer */
404 static int flush_buffer(FILE* file
)
407 int cnt
= (int)(file
->_ptr
- file
->_base
);
408 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
409 file
->_flag
|= _IOERR
;
412 file
->_ptr
=file
->_base
;
413 file
->_cnt
=file
->_bufsiz
;
418 /* INTERNAL: Allocate stdio file buffer */
419 void alloc_buffer(FILE* file
)
421 file
->_base
= calloc(BUFSIZ
,1);
423 file
->_bufsiz
= BUFSIZ
;
424 file
->_flag
|= _IOMYBUF
;
426 file
->_base
= (char*)(&file
->_charbuf
);
428 file
->_bufsiz
= sizeof(file
->_charbuf
);
430 file
->_ptr
= file
->_base
;
434 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
435 static int int_to_base32(int num
, char *str
)
450 *p
= (num
& 31) + '0';
452 *p
+= ('a' - '0' - 10);
459 /*********************************************************************
462 FILE * CDECL
__p__iob(void)
467 /*********************************************************************
470 int CDECL
_access(const char *filename
, int mode
)
472 DWORD attr
= GetFileAttributesA(filename
);
474 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
476 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
478 _dosmaperr(GetLastError());
481 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
483 _dosmaperr(ERROR_ACCESS_DENIED
);
489 /*********************************************************************
490 * _waccess (MSVCRT.@)
492 int CDECL
_waccess(const wchar_t *filename
, int mode
)
494 DWORD attr
= GetFileAttributesW(filename
);
496 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
498 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
500 _dosmaperr(GetLastError());
503 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
505 _dosmaperr(ERROR_ACCESS_DENIED
);
511 /*********************************************************************
514 int CDECL
_chmod(const char *path
, int flags
)
516 DWORD oldFlags
= GetFileAttributesA(path
);
518 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
520 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
521 oldFlags
| FILE_ATTRIBUTE_READONLY
;
523 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
526 _dosmaperr(GetLastError());
530 /*********************************************************************
533 int CDECL
_wchmod(const wchar_t *path
, int flags
)
535 DWORD oldFlags
= GetFileAttributesW(path
);
537 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
539 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
540 oldFlags
| FILE_ATTRIBUTE_READONLY
;
542 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
545 _dosmaperr(GetLastError());
549 /*********************************************************************
552 int CDECL
_unlink(const char *path
)
554 TRACE("%s\n",debugstr_a(path
));
555 if(DeleteFileA(path
))
557 TRACE("failed (%d)\n",GetLastError());
558 _dosmaperr(GetLastError());
562 /*********************************************************************
563 * _wunlink (MSVCRT.@)
565 int CDECL
_wunlink(const wchar_t *path
)
567 TRACE("(%s)\n",debugstr_w(path
));
568 if(DeleteFileW(path
))
570 TRACE("failed (%d)\n",GetLastError());
571 _dosmaperr(GetLastError());
575 /* _flushall calls fflush which calls _flushall */
576 int CDECL
fflush(FILE* file
);
578 /*********************************************************************
579 * _flushall (MSVCRT.@)
581 int CDECL
_flushall(void)
583 int i
, num_flushed
= 0;
586 for (i
= 3; i
< stream_idx
; i
++)
587 if (fstreams
[i
] && fstreams
[i
]->_flag
)
590 /* FIXME: flush, do not commit */
591 if (_commit(i
) == -1)
593 fstreams
[i
]->_flag
|= _IOERR
;
595 if(fstreams
[i
]->_flag
& _IOWRT
) {
602 TRACE(":flushed (%d) handles\n",num_flushed
);
606 /*********************************************************************
609 int CDECL
fflush(FILE* file
)
613 } else if(file
->_flag
& _IOWRT
) {
614 int res
=flush_buffer(file
);
620 /*********************************************************************
623 int CDECL
_close(int fd
)
630 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
631 if (hand
== INVALID_HANDLE_VALUE
)
633 else if (!CloseHandle(hand
))
635 WARN(":failed-last error (%d)\n",GetLastError());
636 _dosmaperr(GetLastError());
649 /*********************************************************************
652 int CDECL
_commit(int fd
)
654 HANDLE hand
= fdtoh(fd
);
656 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
657 if (hand
== INVALID_HANDLE_VALUE
)
660 if (!FlushFileBuffers(hand
))
662 if (GetLastError() == ERROR_INVALID_HANDLE
)
664 /* FlushFileBuffers fails for console handles
665 * so we ignore this error.
669 TRACE(":failed-last error (%d)\n",GetLastError());
670 _dosmaperr(GetLastError());
677 /*********************************************************************
680 * MSDN isn't clear on this point, but the remarks for _pipe
681 * indicate file descriptors duplicated with _dup and _dup2 are always
684 int CDECL
_dup2(int od
, int nd
)
688 TRACE("(od=%d, nd=%d)\n", od
, nd
);
690 if (nd
< MAX_FILES
&& is_valid_fd(od
))
694 if (DuplicateHandle(GetCurrentProcess(), fdesc
[od
].handle
,
695 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
697 int wxflag
= fdesc
[od
].wxflag
& ~_O_NOINHERIT
;
701 ret
= alloc_fd_from(handle
, wxflag
, nd
);
709 /* _dup2 returns 0, not nd, on success */
716 _dosmaperr(GetLastError());
728 /*********************************************************************
731 int CDECL
_dup(int od
)
737 if (_dup2(od
, fd
) == 0)
745 /*********************************************************************
748 int CDECL
_eof(int fd
)
751 LONG hcurpos
,hendpos
;
752 HANDLE hand
= fdtoh(fd
);
754 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
756 if (hand
== INVALID_HANDLE_VALUE
)
759 if (fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
761 /* Otherwise we do it the hard way */
762 hcurpos
= hendpos
= 0;
763 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
764 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
766 if (curpos
== endpos
&& hcurpos
== hendpos
)
768 /* FIXME: shouldn't WX_ATEOF be set here? */
772 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
776 /*********************************************************************
777 * _fcloseall (MSVCRT.@)
779 int CDECL
_fcloseall(void)
781 int num_closed
= 0, i
;
784 for (i
= 3; i
< stream_idx
; i
++)
785 if (fstreams
[i
] && fstreams
[i
]->_flag
&&
786 !fclose(fstreams
[i
]))
790 TRACE(":closed (%d) handles\n",num_closed
);
794 /* free everything on process exit */
795 void msvcrt_free_io(void)
798 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
799 * stdout, and stderr (unlike GNU), so we need to fclose() them here
800 * or they won't get flushed.
805 FILE_cs
.DebugInfo
->Spare
[0] = 0;
806 DeleteCriticalSection(&FILE_cs
);
809 /*********************************************************************
810 * _lseeki64 (MSVCRT.@)
812 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
814 HANDLE hand
= fdtoh(fd
);
815 LARGE_INTEGER ofs
, ret
;
817 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
818 if (hand
== INVALID_HANDLE_VALUE
)
821 if (whence
< 0 || whence
> 2)
827 TRACE(":fd (%d) to %s pos %s\n",
828 fd
,wine_dbgstr_longlong(offset
),
829 (whence
==SEEK_SET
)?"SEEK_SET":
830 (whence
==SEEK_CUR
)?"SEEK_CUR":
831 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
833 ofs
.QuadPart
= offset
;
834 if (SetFilePointerEx(hand
, ofs
, &ret
, whence
))
836 fdesc
[fd
].wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
837 /* FIXME: What if we seek _to_ EOF - is EOF set? */
841 TRACE(":error-last error (%d)\n",GetLastError());
842 _dosmaperr(GetLastError());
846 /*********************************************************************
849 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
851 return (LONG
)_lseeki64(fd
, offset
, whence
);
854 /*********************************************************************
855 * _locking (MSVCRT.@)
857 * This is untested; the underlying LockFile doesn't work yet.
859 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
863 HANDLE hand
= fdtoh(fd
);
865 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
866 if (hand
== INVALID_HANDLE_VALUE
)
869 if (mode
< 0 || mode
> 4)
875 TRACE(":fd (%d) by 0x%08x mode %s\n",
876 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
877 (mode
==_LK_LOCK
)?"_LK_LOCK":
878 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
879 (mode
==_LK_RLCK
)?"_LK_RLCK":
880 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
883 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
885 FIXME ("Seek failed\n");
886 *_errno() = EINVAL
; /* FIXME */
889 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
892 ret
= 1; /* just to satisfy gcc */
895 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
900 else if (mode
== _LK_UNLCK
)
901 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
903 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
904 /* FIXME - what about error settings? */
908 /*********************************************************************
911 int CDECL
fseek(FILE* file
, long offset
, int whence
)
913 /* Flush output if needed */
914 if(file
->_flag
& _IOWRT
)
917 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
918 offset
-= file
->_cnt
;
919 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
920 /* Black magic correction for CR removal */
922 for (i
=0; i
<file
->_cnt
; i
++) {
923 if (file
->_ptr
[i
] == '\n')
926 /* Black magic when reading CR at buffer boundary*/
927 if(fdesc
[file
->_file
].wxflag
& WX_READCR
)
931 /* Discard buffered input */
933 file
->_ptr
= file
->_base
;
934 /* Reset direction of i/o */
935 if(file
->_flag
& _IORW
) {
936 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
938 /* Clear end of file flag */
939 file
->_flag
&= ~_IOEOF
;
940 return (_lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
943 /*********************************************************************
946 int CDECL
_chsize(int fd
, long size
)
952 TRACE("(fd=%d, size=%ld)\n", fd
, size
);
957 if (handle
!= INVALID_HANDLE_VALUE
)
959 /* save the current file pointer */
960 cur
= _lseek(fd
, 0, SEEK_CUR
);
963 pos
= _lseek(fd
, size
, SEEK_SET
);
966 ret
= SetEndOfFile(handle
);
967 if (!ret
) _dosmaperr(GetLastError());
970 /* restore the file pointer */
971 _lseek(fd
, cur
, SEEK_SET
);
979 /*********************************************************************
980 * clearerr (MSVCRT.@)
982 void CDECL
clearerr(FILE* file
)
984 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
985 file
->_flag
&= ~(_IOERR
| _IOEOF
);
988 /*********************************************************************
991 void CDECL
rewind(FILE* file
)
993 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
994 fseek(file
, 0L, SEEK_SET
);
998 static int get_flags(const char* mode
, int *open_flags
, int* stream_flags
)
1000 int plus
= strchr(mode
, '+') != NULL
;
1005 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1006 *stream_flags
= plus
? _IORW
: _IOREAD
;
1009 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1010 *stream_flags
= plus
? _IORW
: _IOWRT
;
1013 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1014 *stream_flags
= plus
? _IORW
: _IOWRT
;
1024 *open_flags
|= _O_BINARY
;
1025 *open_flags
&= ~_O_TEXT
;
1028 *open_flags
|= _O_TEXT
;
1029 *open_flags
&= ~_O_BINARY
;
1034 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1039 /*********************************************************************
1040 * _fdopen (MSVCRT.@)
1042 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1044 int open_flags
, stream_flags
;
1047 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1050 if (!(file
= alloc_fp()))
1052 else if (init_fp(file
, fd
, stream_flags
) == -1)
1057 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
1063 /*********************************************************************
1064 * _wfdopen (MSVCRT.@)
1066 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1068 unsigned mlen
= (unsigned)strlenW(mode
);
1069 char *modea
= calloc(mlen
+ 1, 1);
1071 int open_flags
, stream_flags
;
1074 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
1076 if (get_flags(modea
, &open_flags
, &stream_flags
) == -1)
1082 if (!(file
= alloc_fp()))
1084 else if (init_fp(file
, fd
, stream_flags
) == -1)
1092 rewind(file
); /* FIXME: is this needed ??? */
1093 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
1101 /*********************************************************************
1102 * _filelength (MSVCRT.@)
1104 LONG CDECL
_filelength(int fd
)
1106 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1109 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1112 if (endPos
!= curPos
)
1113 _lseek(fd
, curPos
, SEEK_SET
);
1120 /*********************************************************************
1121 * _filelengthi64 (MSVCRT.@)
1123 __int64 CDECL
_filelengthi64(int fd
)
1125 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1128 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1131 if (endPos
!= curPos
)
1132 _lseeki64(fd
, curPos
, SEEK_SET
);
1139 /*********************************************************************
1140 * _fileno (MSVCRT.@)
1142 int CDECL
_fileno(FILE* file
)
1144 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1148 /*********************************************************************
1149 * _get_osfhandle (MSVCRT.@)
1151 intptr_t CDECL
_get_osfhandle(int fd
)
1153 HANDLE hand
= fdtoh(fd
);
1154 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1156 return (long)(LONG_PTR
)hand
;
1159 /*********************************************************************
1160 * _isatty (MSVCRT.@)
1162 int CDECL
_isatty(int fd
)
1164 HANDLE hand
= fdtoh(fd
);
1166 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1167 if (hand
== INVALID_HANDLE_VALUE
)
1170 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1173 /*********************************************************************
1174 * _mktemp (MSVCRT.@)
1176 char * CDECL
_mktemp(char *pattern
)
1179 char *retVal
= pattern
;
1184 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1188 id
= GetCurrentProcessId();
1192 int tempNum
= id
/ 10;
1193 *pattern
-- = id
- (tempNum
* 10) + '0';
1199 *pattern
= letter
++;
1200 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1201 GetLastError() == ERROR_FILE_NOT_FOUND
)
1203 } while(letter
<= 'z');
1207 /*********************************************************************
1208 * _wmktemp (MSVCRT.@)
1210 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1213 wchar_t *retVal
= pattern
;
1215 wchar_t letter
= 'a';
1218 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1222 id
= GetCurrentProcessId();
1226 int tempNum
= id
/ 10;
1227 *pattern
-- = id
- (tempNum
* 10) + '0';
1233 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1234 GetLastError() == ERROR_FILE_NOT_FOUND
)
1236 *pattern
= letter
++;
1237 } while(letter
!= '|');
1241 /*static */unsigned split_oflags(unsigned oflags
)
1244 unsigned unsupp
; /* until we support everything */
1246 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1247 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1248 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1249 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1250 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1251 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1253 if ((unsupp
= oflags
& ~(
1254 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1255 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1256 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1258 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1260 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1265 /*********************************************************************
1268 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1271 SECURITY_ATTRIBUTES sa
;
1272 HANDLE readHandle
, writeHandle
;
1280 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1281 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1282 sa
.lpSecurityDescriptor
= NULL
;
1283 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1285 unsigned int wxflags
= split_oflags(textmode
);
1289 fd
= alloc_fd(readHandle
, wxflags
);
1293 fd
= alloc_fd(writeHandle
, wxflags
);
1302 CloseHandle(writeHandle
);
1308 CloseHandle(readHandle
);
1309 CloseHandle(writeHandle
);
1315 _dosmaperr(GetLastError());
1320 /*********************************************************************
1323 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1327 DWORD access
= 0, creation
= 0, attrib
;
1331 SECURITY_ATTRIBUTES sa
;
1334 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1335 path
, oflags
, shflags
);
1337 wxflag
= split_oflags(oflags
);
1338 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1340 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1341 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1342 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1345 if (oflags
& _O_CREAT
)
1347 va_start(ap
, shflags
);
1348 pmode
= va_arg(ap
, int);
1351 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1352 FIXME(": pmode 0x%04x ignored\n", pmode
);
1354 WARN(": pmode 0x%04x ignored\n", pmode
);
1356 if (oflags
& _O_EXCL
)
1357 creation
= CREATE_NEW
;
1358 else if (oflags
& _O_TRUNC
)
1359 creation
= CREATE_ALWAYS
;
1361 creation
= OPEN_ALWAYS
;
1363 else /* no _O_CREAT */
1365 if (oflags
& _O_TRUNC
)
1366 creation
= TRUNCATE_EXISTING
;
1368 creation
= OPEN_EXISTING
;
1377 sharing
= FILE_SHARE_READ
;
1380 sharing
= FILE_SHARE_WRITE
;
1383 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1386 ERR( "Unhandled shflags 0x%x\n", shflags
);
1389 attrib
= FILE_ATTRIBUTE_NORMAL
;
1391 if (oflags
& _O_TEMPORARY
)
1393 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1395 sharing
|= FILE_SHARE_DELETE
;
1398 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1399 sa
.lpSecurityDescriptor
= NULL
;
1400 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1402 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1404 if (hand
== INVALID_HANDLE_VALUE
) {
1405 WARN(":failed-last error (%d)\n",GetLastError());
1406 _dosmaperr(GetLastError());
1410 fd
= alloc_fd(hand
, wxflag
);
1412 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1416 /*********************************************************************
1417 * _wsopen (MSVCRT.@)
1419 int CDECL
_wsopen( const wchar_t* path
, int oflags
, int shflags
, ... )
1421 const unsigned int len
= (unsigned)strlenW(path
);
1422 char *patha
= calloc(len
+ 1,1);
1426 va_start(ap
, shflags
);
1427 pmode
= va_arg(ap
, int);
1430 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1432 int retval
= _sopen(patha
,oflags
,shflags
,pmode
);
1436 _dosmaperr(GetLastError());
1441 /*********************************************************************
1444 int CDECL
_open( const char *path
, int flags
, ... )
1448 if (flags
& _O_CREAT
)
1451 va_start(ap
, flags
);
1452 pmode
= va_arg(ap
, int);
1454 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1457 return _sopen( path
, flags
, _SH_DENYNO
);
1460 /*********************************************************************
1463 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1465 const unsigned int len
= (unsigned)strlenW(path
);
1466 char *patha
= calloc(len
+ 1,1);
1470 va_start(ap
, flags
);
1471 pmode
= va_arg(ap
, int);
1474 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1476 int retval
= _open(patha
,flags
,pmode
);
1481 _dosmaperr(GetLastError());
1485 /*********************************************************************
1488 int CDECL
_creat(const char *path
, int flags
)
1490 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1491 return _open(path
, usedFlags
);
1494 /*********************************************************************
1495 * _wcreat (MSVCRT.@)
1497 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1499 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1500 return _wopen(path
, usedFlags
);
1503 /*********************************************************************
1504 * _open_osfhandle (MSVCRT.@)
1506 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
1510 /* _O_RDONLY (0) always matches, so set the read flag
1511 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1512 * file, so set the write flag. It also only sets _O_TEXT if it wants
1513 * text - it never sets _O_BINARY.
1515 /* don't let split_oflags() decide the mode if no mode is passed */
1516 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1517 oflags
|= _O_BINARY
;
1519 fd
= alloc_fd((HANDLE
)(LONG_PTR
)handle
, split_oflags(oflags
));
1520 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1524 /*********************************************************************
1527 int CDECL
_rmtmp(void)
1529 int num_removed
= 0, i
;
1532 for (i
= 3; i
< stream_idx
; i
++)
1533 if (fstreams
[i
] && fstreams
[i
]->_tmpfname
)
1535 fclose(fstreams
[i
]);
1541 TRACE(":removed (%d) temp files\n",num_removed
);
1545 /*********************************************************************
1548 static int read_i(int fd
, void *buf
, unsigned int count
)
1551 char *bufstart
= buf
;
1552 HANDLE hand
= fdtoh(fd
);
1557 if (fdesc
[fd
].wxflag
& WX_READEOF
) {
1558 fdesc
[fd
].wxflag
|= WX_ATEOF
;
1559 TRACE("already at EOF, returning 0\n");
1562 /* Don't trace small reads, it gets *very* annoying */
1564 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1565 if (hand
== INVALID_HANDLE_VALUE
)
1568 /* Reading single bytes in O_TEXT mode makes things slow
1569 * So read big chunks
1571 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1573 if (count
!= 0 && num_read
== 0)
1575 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1576 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1578 else if (fdesc
[fd
].wxflag
& WX_TEXT
)
1581 if (bufstart
[num_read
-1] == '\r')
1585 fdesc
[fd
].wxflag
&= ~WX_READCR
;
1586 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1590 fdesc
[fd
].wxflag
|= WX_READCR
;
1595 fdesc
[fd
].wxflag
&= ~WX_READCR
;
1596 for (i
=0, j
=0; i
<num_read
; i
++)
1598 /* in text mode, a ctrl-z signals EOF */
1599 if (bufstart
[i
] == 0x1a)
1601 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1602 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1605 /* in text mode, strip \r if followed by \n.
1606 * BUG: should save state across calls somehow, so CR LF that
1607 * straddles buffer boundary gets recognized properly?
1609 if ((bufstart
[i
] != '\r')
1610 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1611 bufstart
[j
++] = bufstart
[i
];
1618 if (GetLastError() == ERROR_BROKEN_PIPE
)
1620 TRACE(":end-of-pipe\n");
1621 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1626 TRACE(":failed-last error (%d)\n",GetLastError());
1632 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1636 /*********************************************************************
1639 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
1642 num_read
= read_i(fd
, buf
, count
);
1646 /*********************************************************************
1647 * _setmode (MSVCRT.@)
1649 int CDECL
_setmode(int fd
,int mode
)
1651 int ret
= fdesc
[fd
].wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
1652 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1653 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1654 if ((mode
& _O_TEXT
) == _O_TEXT
)
1655 fdesc
[fd
].wxflag
|= WX_TEXT
;
1657 fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1661 /*********************************************************************
1664 long CDECL
_tell(int fd
)
1666 return _lseek(fd
, 0, SEEK_CUR
);
1669 /*********************************************************************
1670 * _telli64 (MSVCRT.@)
1672 __int64 CDECL
_telli64(int fd
)
1674 return _lseeki64(fd
, 0, SEEK_CUR
);
1677 /*********************************************************************
1678 * _tempnam (MSVCRT.@)
1680 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
1682 char tmpbuf
[MAX_PATH
];
1683 const char *tmp_dir
= getenv("TMP");
1685 if (tmp_dir
) dir
= tmp_dir
;
1687 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1688 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1690 TRACE("got name (%s)\n",tmpbuf
);
1691 DeleteFileA(tmpbuf
);
1692 return _strdup(tmpbuf
);
1694 TRACE("failed (%d)\n",GetLastError());
1698 /*********************************************************************
1699 * _wtempnam (MSVCRT.@)
1701 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
1703 wchar_t tmpbuf
[MAX_PATH
];
1705 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1706 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1708 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1709 DeleteFileW(tmpbuf
);
1710 return _wcsdup(tmpbuf
);
1712 TRACE("failed (%d)\n",GetLastError());
1716 /*********************************************************************
1719 int CDECL
_umask(int umask
)
1721 int old_umask
= umask
;
1722 TRACE("(%d)\n",umask
);
1723 MSVCRT_umask
= umask
;
1727 /*********************************************************************
1730 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
1733 HANDLE hand
= fdtoh(fd
);
1735 /* Don't trace small writes, it gets *very* annoying */
1738 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1740 if (hand
== INVALID_HANDLE_VALUE
)
1746 /* If appending, go to EOF */
1747 if (fdesc
[fd
].wxflag
& WX_APPEND
)
1748 _lseek(fd
, 0, FILE_END
);
1750 if (!(fdesc
[fd
].wxflag
& WX_TEXT
))
1752 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1753 && (num_written
== count
))
1755 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
1756 hand
, GetLastError());
1761 unsigned int i
, j
, nr_lf
;
1764 const char *s
= (const char *)buf
, *buf_start
= (const char *)buf
;
1765 /* find number of \n ( without preceding \r ) */
1766 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
1771 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1776 if ((q
= p
= malloc(count
+ nr_lf
)))
1778 for (s
= (const char *)buf
, i
= 0, j
= 0; i
< count
; i
++)
1783 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1790 FIXME("Malloc failed\n");
1798 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
1800 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
1801 fd
, hand
, GetLastError(), num_written
);
1805 return (int)(s
- buf_start
);
1817 /*********************************************************************
1820 int CDECL
_putw(int val
, FILE* file
)
1823 len
= _write(file
->_file
, &val
, sizeof(val
));
1824 if (len
== sizeof(val
)) return val
;
1825 file
->_flag
|= _IOERR
;
1829 /*********************************************************************
1832 int CDECL
fclose(FILE* file
)
1837 free(file
->_tmpfname
);
1838 file
->_tmpfname
= NULL
;
1839 /* flush stdio buffers */
1840 if(file
->_flag
& _IOWRT
)
1842 if(file
->_flag
& _IOMYBUF
)
1845 r
=_close(file
->_file
);
1849 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
1852 /*********************************************************************
1855 int CDECL
feof(FILE* file
)
1857 return file
->_flag
& _IOEOF
;
1860 /*********************************************************************
1863 int CDECL
ferror(FILE* file
)
1865 return file
->_flag
& _IOERR
;
1868 /*********************************************************************
1869 * _filbuf (MSVCRT.@)
1871 int CDECL
_filbuf(FILE* file
)
1873 /* Allocate buffer if needed */
1874 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
) ) {
1877 if(!(file
->_flag
& _IOREAD
)) {
1878 if(file
->_flag
& _IORW
) {
1879 file
->_flag
|= _IOREAD
;
1884 if(file
->_flag
& _IONBF
) {
1887 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
1888 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
1893 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
1895 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
1900 file
->_ptr
= file
->_base
+1;
1901 return *(unsigned char *)file
->_base
;
1905 /*********************************************************************
1908 int CDECL
fgetc(FILE* file
)
1914 i
= (unsigned char *)file
->_ptr
++;
1921 /*********************************************************************
1922 * _fgetchar (MSVCRT.@)
1924 int CDECL
_fgetchar(void)
1926 return fgetc(stdin
);
1929 /*********************************************************************
1932 char * CDECL
fgets(char *s
, int size
, FILE* file
)
1935 char * buf_start
= s
;
1937 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1938 file
,file
->_file
,s
,size
);
1940 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
1945 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1947 TRACE(":nothing read\n");
1950 if ((cc
!= EOF
) && (size
> 1))
1953 TRACE(":got %s\n", debugstr_a(buf_start
));
1957 /*********************************************************************
1960 * In _O_TEXT mode, multibyte characters are read from the file, dropping
1961 * the CR from CR/LF combinations
1963 wint_t CDECL
fgetwc(FILE* file
)
1967 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
1974 for(i
=0; i
<sizeof(wc
); i
++)
1987 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
1998 if ((*__p___mb_cur_max() > 1) && isleadbyte(c
))
2000 FIXME("Treat Multibyte characters\n");
2008 /*********************************************************************
2011 int CDECL
_getw(FILE* file
)
2016 for (j
=0; j
<sizeof(int); j
++) {
2019 file
->_flag
|= _IOEOF
;
2027 /*********************************************************************
2030 wint_t CDECL
getwc(FILE* file
)
2032 return fgetwc(file
);
2035 /*********************************************************************
2036 * _fgetwchar (MSVCRT.@)
2038 wint_t CDECL
_fgetwchar(void)
2040 return fgetwc(stdin
);
2043 /*********************************************************************
2044 * getwchar (MSVCRT.@)
2046 wint_t CDECL
getwchar(void)
2048 return _fgetwchar();
2051 /*********************************************************************
2054 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2057 wchar_t * buf_start
= s
;
2059 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2060 file
,file
->_file
,s
,size
);
2062 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2067 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2069 TRACE(":nothing read\n");
2072 if ((cc
!= WEOF
) && (size
> 1))
2075 TRACE(":got %s\n", debugstr_w(buf_start
));
2079 /*********************************************************************
2082 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2084 int wrcnt
=(int)(size
* nmemb
);
2089 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2090 memcpy(file
->_ptr
, ptr
, pcnt
);
2095 ptr
= (const char*)ptr
+ pcnt
;
2096 } else if(!(file
->_flag
& _IOWRT
)) {
2097 if(file
->_flag
& _IORW
) {
2098 file
->_flag
|= _IOWRT
;
2104 int res
=flush_buffer(file
);
2106 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2109 file
->_flag
|= _IOERR
;
2112 written
+= pwritten
;
2115 return written
/ size
;
2118 /*********************************************************************
2121 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
2123 /* If this is a real file stream (and not some temporary one for
2124 sprintf-like functions), check whether it is opened in text mode.
2125 In this case, we have to perform an implicit conversion to ANSI. */
2126 if (!(stream
->_flag
& _IOSTRG
) && fdesc
[stream
->_file
].wxflag
& WX_TEXT
)
2128 /* Convert to multibyte in text mode */
2129 char mbc
[MB_LEN_MAX
];
2132 mb_return
= wctomb(mbc
, c
);
2137 /* Output all characters */
2138 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
2143 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
2150 /*********************************************************************
2151 * _fputwchar (MSVCRT.@)
2153 wint_t CDECL
_fputwchar(wint_t wc
)
2155 return fputwc(wc
, stdout
);
2158 /*********************************************************************
2159 * _fsopen (MSVCRT.@)
2161 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2164 int open_flags
, stream_flags
, fd
;
2166 TRACE("(%s,%s)\n",path
,mode
);
2168 /* map mode string to open() flags. "man fopen" for possibilities. */
2169 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2173 fd
= _sopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2176 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2178 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
2185 TRACE(":got (%p)\n",file
);
2186 if (fd
>= 0 && !file
)
2192 /*********************************************************************
2193 * _wfsopen (MSVCRT.@)
2195 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2197 const unsigned int plen
= (unsigned)strlenW(path
), mlen
= (unsigned)strlenW(mode
);
2198 char *patha
= calloc(plen
+ 1, 1);
2199 char *modea
= calloc(mlen
+ 1, 1);
2201 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
2203 if (patha
&& modea
&&
2204 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
2205 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
2207 FILE *retval
= _fsopen(patha
,modea
,share
);
2214 _dosmaperr(GetLastError());
2218 /*********************************************************************
2221 FILE * CDECL
fopen(const char *path
, const char *mode
)
2223 return _fsopen( path
, mode
, _SH_DENYNO
);
2226 /*********************************************************************
2227 * _wfopen (MSVCRT.@)
2229 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2231 return _wfsopen( path
, mode
, _SH_DENYNO
);
2234 /* fputc calls _flsbuf which calls fputc */
2235 int CDECL
_flsbuf(int c
, FILE* file
);
2237 /*********************************************************************
2240 int CDECL
fputc(int c
, FILE* file
)
2247 int res
= flush_buffer(file
);
2248 return res
? res
: c
;
2253 return _flsbuf(c
, file
);
2257 /*********************************************************************
2258 * _fputchar (MSVCRT.@)
2260 int CDECL
_fputchar(int c
)
2262 return fputc(c
, stdout
);
2265 /*********************************************************************
2268 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2269 { int rcnt
=(int)(size
* nmemb
);
2276 /* first buffered data */
2278 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2279 memcpy(ptr
, file
->_ptr
, pcnt
);
2284 ptr
= (char*)ptr
+ pcnt
;
2285 } else if(!(file
->_flag
& _IOREAD
)) {
2286 if(file
->_flag
& _IORW
) {
2287 file
->_flag
|= _IOREAD
;
2294 /* Fill the buffer on small reads.
2295 * TODO: Use a better buffering strategy.
2297 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2298 if (file
->_bufsiz
== 0) {
2301 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2302 file
->_ptr
= file
->_base
;
2303 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2304 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2305 if (i
> 0 && i
< file
->_cnt
) {
2306 fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2307 file
->_flag
&= ~_IOEOF
;
2310 memcpy(ptr
, file
->_ptr
, i
);
2315 i
= _read(file
->_file
,ptr
, rcnt
);
2319 ptr
= (char *)ptr
+i
;
2320 /* expose feof condition in the flags
2321 * MFC tests file->_flag for feof, and doesn't call feof())
2323 if ( fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2324 file
->_flag
|= _IOEOF
;
2327 file
->_flag
|= _IOERR
;
2337 /*********************************************************************
2338 * freopen (MSVCRT.@)
2341 FILE* CDECL
freopen(const char *path
, const char *mode
,FILE* file
)
2343 int open_flags
, stream_flags
, fd
;
2345 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2348 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2353 /* map mode string to open() flags. "man fopen" for possibilities. */
2354 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2358 fd
= _open(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2361 else if (init_fp(file
, fd
, stream_flags
) == -1)
2364 WARN(":failed-last error (%d)\n",GetLastError());
2365 _dosmaperr(GetLastError());
2374 /*********************************************************************
2375 * wfreopen (MSVCRT.@)
2378 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
,FILE* file
)
2380 int open_flags
, stream_flags
, fd
;
2382 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2385 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2390 /* map mode string to open() flags. "man fopen" for possibilities. */
2391 if (get_flags((char*)mode
, &open_flags
, &stream_flags
) == -1)
2395 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2398 else if (init_fp(file
, fd
, stream_flags
) == -1)
2401 WARN(":failed-last error (%d)\n",GetLastError());
2402 _dosmaperr(GetLastError());
2411 /*********************************************************************
2412 * fsetpos (MSVCRT.@)
2414 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2416 /* Note that all this has been lifted 'as is' from fseek */
2417 if(file
->_flag
& _IOWRT
)
2420 /* Discard buffered input */
2422 file
->_ptr
= file
->_base
;
2424 /* Reset direction of i/o */
2425 if(file
->_flag
& _IORW
) {
2426 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2429 return (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2432 /*********************************************************************
2435 LONG CDECL
ftell(FILE* file
)
2437 /* TODO: just call fgetpos and return lower half of result */
2440 pos
= _tell(file
->_file
);
2441 if(pos
== -1) return -1;
2443 if( file
->_flag
& _IOWRT
) {
2444 off
= (int)(file
->_ptr
- file
->_base
);
2447 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2448 /* Black magic correction for CR removal */
2450 for (i
=0; i
<file
->_cnt
; i
++) {
2451 if (file
->_ptr
[i
] == '\n')
2460 /*********************************************************************
2461 * fgetpos (MSVCRT.@)
2463 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2466 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
2467 if(*pos
== -1) return -1;
2469 if( file
->_flag
& _IOWRT
) {
2470 off
= (int)(file
->_ptr
- file
->_base
);
2473 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2474 /* Black magic correction for CR removal */
2476 for (i
=0; i
<file
->_cnt
; i
++) {
2477 if (file
->_ptr
[i
] == '\n')
2487 /*********************************************************************
2490 int CDECL
fputs(const char *s
, FILE* file
)
2492 size_t i
, len
= strlen(s
);
2493 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2494 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2495 for (i
=0; i
<len
; i
++)
2496 if (fputc(s
[i
], file
) == EOF
)
2501 /*********************************************************************
2504 int CDECL
fputws(const wchar_t *s
, FILE* file
)
2506 size_t i
, len
= strlenW(s
);
2507 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2508 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2509 for (i
=0; i
<len
; i
++)
2511 if ((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
2513 if (fputwc(s
[i
], file
) == WEOF
)
2519 /*********************************************************************
2520 * getchar (MSVCRT.@)
2522 int CDECL
getchar(void)
2524 return fgetc(stdin
);
2527 /*********************************************************************
2530 int CDECL
getc(FILE* file
)
2535 /*********************************************************************
2538 char * CDECL
gets(char *buf
)
2541 char * buf_start
= buf
;
2543 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
2545 if(cc
!= '\r') *buf
++ = (char)cc
;
2549 TRACE("got '%s'\n", buf_start
);
2553 /*********************************************************************
2556 wchar_t* CDECL
_getws(wchar_t* buf
)
2561 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
2565 *buf
++ = (wchar_t)cc
;
2569 TRACE("got %s\n", debugstr_w(ws
));
2573 /*********************************************************************
2576 int CDECL
putc(int c
, FILE* file
)
2578 return fputc(c
, file
);
2581 /*********************************************************************
2582 * putchar (MSVCRT.@)
2584 int CDECL
putchar(int c
)
2586 return fputc(c
, stdout
);
2589 /*********************************************************************
2592 int CDECL
puts(const char *s
)
2594 size_t len
= strlen(s
);
2595 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2596 return fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
2599 /*********************************************************************
2602 int CDECL
_putws(const wchar_t *s
)
2604 static const wchar_t nl
= '\n';
2605 size_t len
= strlenW(s
);
2606 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2607 return fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
2610 /*********************************************************************
2613 int CDECL
remove(const char *path
)
2615 TRACE("(%s)\n",path
);
2616 if (DeleteFileA(path
))
2618 TRACE(":failed (%d)\n",GetLastError());
2619 _dosmaperr(GetLastError());
2623 /*********************************************************************
2624 * _wremove (MSVCRT.@)
2626 int CDECL
_wremove(const wchar_t *path
)
2628 TRACE("(%s)\n",debugstr_w(path
));
2629 if (DeleteFileW(path
))
2631 TRACE(":failed (%d)\n",GetLastError());
2632 _dosmaperr(GetLastError());
2636 /*********************************************************************
2639 int CDECL
rename(const char *oldpath
,const char *newpath
)
2641 TRACE(":from %s to %s\n",oldpath
,newpath
);
2642 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2644 TRACE(":failed (%d)\n",GetLastError());
2645 _dosmaperr(GetLastError());
2649 /*********************************************************************
2650 * _wrename (MSVCRT.@)
2652 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
2654 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2655 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2657 TRACE(":failed (%d)\n",GetLastError());
2658 _dosmaperr(GetLastError());
2662 /*********************************************************************
2663 * setvbuf (MSVCRT.@)
2665 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
2667 /* TODO: Check if file busy */
2673 if(mode
== _IOFBF
) {
2674 file
->_flag
&= ~_IONBF
;
2675 file
->_base
= file
->_ptr
= buf
;
2677 file
->_bufsiz
= (int)size
;
2680 file
->_flag
|= _IONBF
;
2685 /*********************************************************************
2688 void CDECL
setbuf(FILE* file
, char *buf
)
2690 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
2693 /*********************************************************************
2696 char * CDECL
tmpnam(char *s
)
2704 int_to_base32(GetCurrentProcessId(), tmpstr
);
2705 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
2706 for (count
= 0; count
< TMP_MAX
; count
++)
2708 int_to_base32(unique
++, tmpstr
);
2710 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
2711 GetLastError() == ERROR_FILE_NOT_FOUND
)
2717 /*********************************************************************
2718 * wtmpnam (MSVCRT.@)
2720 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
2722 ERR("UNIMPLEMENTED!\n");
2726 /*********************************************************************
2727 * tmpfile (MSVCRT.@)
2729 FILE* CDECL
tmpfile(void)
2731 char *filename
= tmpnam(NULL
);
2736 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
2737 if (fd
!= -1 && (file
= alloc_fp()))
2739 if (init_fp(file
, fd
, _O_RDWR
) == -1)
2744 else file
->_tmpfname
= _strdup(filename
);
2750 /*********************************************************************
2753 int CDECL
ungetc(int c
, FILE * file
)
2757 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
)) {
2761 if(file
->_ptr
>file
->_base
) {
2771 /*********************************************************************
2772 * ungetwc (MSVCRT.@)
2774 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
2777 char * pp
= (char *)&mwc
;
2779 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
2780 if(pp
[i
] != ungetc(pp
[i
],file
))
2786 /*********************************************************************
2787 * _getmaxstdio (MSVCRT.@)
2789 int CDECL
_getmaxstdio(void)
2791 FIXME("stub, always returns 512\n");
2795 /*********************************************************************
2796 * _setmaxstdio_ (MSVCRT.@)
2798 int CDECL
_setmaxstdio(int newmax
)
2805 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
2809 /*********************************************************************
2810 * __pioinfo (MSVCRT.@)
2811 * FIXME: see MAX_FILES define.
2813 ioinfo
* __pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
2814 &fdesc
[0 * 64], &fdesc
[1 * 64], &fdesc
[2 * 64],
2815 &fdesc
[3 * 64], &fdesc
[4 * 64], &fdesc
[5 * 64],
2816 &fdesc
[6 * 64], &fdesc
[7 * 64], &fdesc
[8 * 64],
2817 &fdesc
[9 * 64], &fdesc
[10 * 64], &fdesc
[11 * 64],
2818 &fdesc
[12 * 64], &fdesc
[13 * 64], &fdesc
[14 * 64],
2819 &fdesc
[15 * 64], &fdesc
[16 * 64], &fdesc
[17 * 64],
2820 &fdesc
[18 * 64], &fdesc
[19 * 64], &fdesc
[20 * 64],
2821 &fdesc
[21 * 64], &fdesc
[22 * 64], &fdesc
[23 * 64],
2822 &fdesc
[24 * 64], &fdesc
[25 * 64], &fdesc
[26 * 64],
2823 &fdesc
[27 * 64], &fdesc
[28 * 64], &fdesc
[29 * 64],
2824 &fdesc
[30 * 64], &fdesc
[31 * 64]
2827 /*********************************************************************
2828 * __badioinfo (MSVCRT.@)
2830 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
, { 0, } };