2 * PROJECT: ReactOS CRT library
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: lib/sdk/crt/stdio/file.c
5 * PURPOSE: File CRT functions
6 * PROGRAMMERS: Wine team
7 * Ported to ReactOS by Aleksey Bragin (aleksey@reactos.org)
10 /*********************************************
11 * This file contains ReactOS changes!!
12 * Don't blindly sync it with Wine code!
14 * If you break Unicode output on the console again, please update this counter:
15 * int hours_wasted_on_this = 42;
16 *********************************************/
19 * msvcrt.dll file functions
21 * Copyright 1996,1998 Marcus Meissner
22 * Copyright 1996 Jukka Iivonen
23 * Copyright 1997,2000 Uwe Bonnes
24 * Copyright 2000 Jon Griffiths
25 * Copyright 2004 Eric Pouech
26 * Copyright 2004 Juan Lang
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Lesser General Public
30 * License as published by the Free Software Foundation; either
31 * version 2.1 of the License, or (at your option) any later version.
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Lesser General Public License for more details.
38 * You should have received a copy of the GNU Lesser General Public
39 * License along with this library; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
43 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
47 #include "wine/unicode.h"
48 #include "internal/wine/msvcrt.h"
50 #include <sys/utime.h>
53 int *__p__fmode(void);
54 int *__p___mb_cur_max(void);
59 #define _IOCOMMIT 0x4000
87 /* _access() bit flags FIXME: incomplete */
88 /* defined in crt/io.h */
90 /* values for wxflag in file descriptor */
93 #define WX_READNL 0x04 /* read started with \n */
94 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
96 #define WX_READCR 0x08 /* underlying file is at \r */
97 #define WX_DONTINHERIT 0x10
98 #define WX_APPEND 0x20
102 /* values for exflag - it's used differently in msvcr90.dll*/
104 #define EF_UTF16 0x02
105 #define EF_CRIT_INIT 0x04
106 #define EF_UNK_UNICODE 0x08
108 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
109 static char utf16_bom
[2] = { 0xff, 0xfe };
111 /* FIXME: this should be allocated dynamically */
112 #define MSVCRT_MAX_FILES 2048
113 #define MSVCRT_FD_BLOCK_SIZE 32
115 #define MSVCRT_INTERNAL_BUFSIZ 4096
117 /*********************************************************************
118 * __pioinfo (MSVCRT.@)
119 * array of pointers to ioinfo arrays [32]
121 ioinfo
* __pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
123 /*********************************************************************
124 * __badioinfo (MSVCRT.@)
126 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
128 static int fdstart
= 3; /* first unallocated fd */
129 static int fdend
= 3; /* highest allocated fd */
133 CRITICAL_SECTION crit
;
136 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
137 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { NULL
};
138 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
140 /* INTERNAL: process umask */
141 static int MSVCRT_umask
= 0;
143 /* INTERNAL: static data for tmpnam and _wtmpname functions */
144 static int tmpnam_unique
;
146 /* This critical section protects the tables __pioinfo and fstreams,
147 * and their related indexes, fdstart, fdend,
148 * and MSVCRT_stream_idx, from race conditions.
149 * It doesn't protect against race conditions manipulating the underlying files
150 * or flags; doing so would probably be better accomplished with per-file
151 * protection, rather than locking the whole table for every change.
153 static CRITICAL_SECTION MSVCRT_file_cs
;
154 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
156 0, 0, &MSVCRT_file_cs
,
157 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
158 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
160 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
161 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
162 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
164 static inline ioinfo
* get_ioinfo_nolock(int fd
)
167 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
168 ret
= __pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
172 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
175 /*static*/ inline ioinfo
* get_ioinfo(int fd
)
177 ioinfo
*ret
= get_ioinfo_nolock(fd
);
178 if(ret
->exflag
& EF_CRIT_INIT
)
179 EnterCriticalSection(&ret
->crit
);
183 /*static*/ inline void release_ioinfo(ioinfo
*info
)
185 if(info
->exflag
& EF_CRIT_INIT
)
186 LeaveCriticalSection(&info
->crit
);
189 static inline FILE* msvcrt_get_file(int i
)
193 if(i
>= MSVCRT_max_streams
)
199 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
201 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
202 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
203 ERR("out of memory\n");
208 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
210 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
215 static inline BOOL
is_valid_fd(int fd
)
217 return fd
>= 0 && fd
< fdend
&& (get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
);
220 /* INTERNAL: Get the HANDLE for a fd
221 * This doesn't lock the table, because a failure will result in
222 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
223 * it returns a valid handle which is about to be closed, a subsequent call
224 * will fail, most likely in a sane way.
226 /*static*/ HANDLE
fdtoh(int fd
)
228 if (!is_valid_fd(fd
))
230 WARN(":fd (%d) - no handle!\n",fd
);
233 return INVALID_HANDLE_VALUE
;
235 //if (get_ioinfo_nolock(fd)->handle == INVALID_HANDLE_VALUE)
236 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
237 return get_ioinfo_nolock(fd
)->handle
;
240 /* INTERNAL: free a file entry fd */
241 static void msvcrt_free_fd(int fd
)
246 fdinfo
= get_ioinfo_nolock(fd
);
247 if(fdinfo
!= &__badioinfo
)
249 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
252 TRACE(":fd (%d) freed\n",fd
);
259 SetStdHandle(STD_INPUT_HANDLE
, 0);
262 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
265 SetStdHandle(STD_ERROR_HANDLE
, 0);
277 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
278 /* caller must hold the files lock */
279 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
283 if (fd
>= MSVCRT_MAX_FILES
)
285 WARN(":files exhausted!\n");
290 fdinfo
= get_ioinfo_nolock(fd
);
291 if(fdinfo
== &__badioinfo
) {
294 __pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
295 if(!__pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
296 WARN(":out of memory!\n");
301 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
302 __pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
304 fdinfo
= get_ioinfo_nolock(fd
);
307 fdinfo
->handle
= hand
;
308 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
309 fdinfo
->lookahead
[0] = '\n';
310 fdinfo
->lookahead
[1] = '\n';
311 fdinfo
->lookahead
[2] = '\n';
312 if(!(fdinfo
->exflag
& EF_CRIT_INIT
))
313 InitializeCriticalSection(&fdinfo
->crit
);
314 fdinfo
->exflag
= EF_CRIT_INIT
;
316 /* locate next free slot */
317 if (fd
== fdstart
&& fd
== fdend
)
320 while (fdstart
< fdend
&&
321 get_ioinfo_nolock(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
323 /* update last fd in use */
326 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
330 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
331 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
332 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
338 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
339 /*static*/ int msvcrt_alloc_fd(HANDLE hand
, int flag
)
344 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
345 ret
= msvcrt_set_fd(hand
, flag
, fdstart
);
350 /* INTERNAL: Allocate a FILE* for an fd slot */
351 /* caller must hold the files lock */
352 static FILE* msvcrt_alloc_fp(void)
357 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
359 file
= msvcrt_get_file(i
);
363 if (file
->_flag
== 0)
365 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
373 /* INTERNAL: initialize a FILE* from an open fd */
374 static int msvcrt_init_fp(FILE* file
, int fd
, unsigned stream_flags
)
376 TRACE(":fd (%d) allocating FILE*\n",fd
);
377 if (!is_valid_fd(fd
))
379 WARN(":invalid fd %d\n",fd
);
384 file
->_ptr
= file
->_base
= NULL
;
387 file
->_flag
= stream_flags
;
388 file
->_tmpfname
= NULL
;
390 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
391 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
393 TRACE(":got FILE* (%p)\n",file
);
397 /* INTERNAL: Create an inheritance data block (for spawned process)
398 * The inheritance block is made of:
399 * 00 int nb of file descriptor (NBFD)
400 * 04 char file flags (wxflag): repeated for each fd
401 * 4+NBFD HANDLE file handle: repeated for each fd
403 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
410 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
411 *block
= calloc(1, *size
);
417 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
418 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
420 *(unsigned*)*block
= fdend
;
421 for (fd
= 0; fd
< fdend
; fd
++)
423 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
424 fdinfo
= get_ioinfo_nolock(fd
);
425 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
427 *wxflag_ptr
= fdinfo
->wxflag
;
428 *handle_ptr
= fdinfo
->handle
;
433 *handle_ptr
= INVALID_HANDLE_VALUE
;
435 wxflag_ptr
++; handle_ptr
++;
440 /* INTERNAL: Set up all file descriptors,
441 * as well as default streams (stdin, stderr and stdout)
443 void msvcrt_init_io(void)
449 GetStartupInfoA(&si
);
450 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
456 count
= *(unsigned*)si
.lpReserved2
;
457 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
458 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
460 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
461 count
= min(count
, MSVCRT_MAX_FILES
);
462 for (i
= 0; i
< count
; i
++)
464 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
465 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
467 wxflag_ptr
++; handle_ptr
++;
469 fdend
= max( 3, count
);
470 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
471 if (get_ioinfo_nolock(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
474 fdinfo
= get_ioinfo_nolock(STDIN_FILENO
);
475 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
476 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
477 DWORD type
= GetFileType(h
);
479 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
480 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDIN_FILENO
);
483 fdinfo
= get_ioinfo_nolock(STDOUT_FILENO
);
484 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
485 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
486 DWORD type
= GetFileType(h
);
488 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
489 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDOUT_FILENO
);
492 fdinfo
= get_ioinfo_nolock(STDERR_FILENO
);
493 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
494 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
495 DWORD type
= GetFileType(h
);
497 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
498 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDERR_FILENO
);
501 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO
)->handle
,
502 get_ioinfo_nolock(STDOUT_FILENO
)->handle
,
503 get_ioinfo_nolock(STDERR_FILENO
)->handle
);
505 memset(_iob
,0,3*sizeof(FILE));
506 for (i
= 0; i
< 3; i
++)
508 /* FILE structs for stdin/out/err are static and never deleted */
510 _iob
[i
]._tmpfname
= NULL
;
511 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
513 MSVCRT_stream_idx
= 3;
516 /* INTERNAL: Flush stdio file buffer */
517 static int msvcrt_flush_buffer(FILE* file
)
519 if((file
->_flag
& (_IOREAD
|_IOWRT
)) == _IOWRT
&&
520 file
->_flag
& (_IOMYBUF
|_USERBUF
)) {
521 int cnt
=file
->_ptr
-file
->_base
;
522 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
523 file
->_flag
|= _IOERR
;
527 if(file
->_flag
& _IORW
)
528 file
->_flag
&= ~_IOWRT
;
530 #ifdef __REACTOS__ /* CORE-11949 */
531 file
->_ptr
=file
->_base
;
536 #ifndef __REACTOS__ /* CORE-11949 */
537 file
->_ptr
=file
->_base
;
543 /*********************************************************************
546 int CDECL
_isatty(int fd
)
548 TRACE(":fd (%d)\n",fd
);
550 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
553 /* INTERNAL: Allocate stdio file buffer */
554 /*static*/ BOOL
msvcrt_alloc_buffer(FILE* file
)
556 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
557 && _isatty(file
->_file
))
560 file
->_base
= calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
562 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
563 file
->_flag
|= _IOMYBUF
;
565 file
->_base
= (char*)(&file
->_charbuf
);
567 file
->_flag
|= _IONBF
;
569 file
->_ptr
= file
->_base
;
574 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
575 static BOOL
add_std_buffer(FILE *file
)
577 static char buffers
[2][BUFSIZ
];
579 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
580 || (file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
581 || !_isatty(file
->_file
))
584 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
585 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
586 file
->_flag
|= _USERBUF
;
590 /* INTERNAL: Removes temporary buffer from stdout or stderr */
591 /* Only call this function when add_std_buffer returned TRUE */
592 static void remove_std_buffer(FILE *file
)
594 msvcrt_flush_buffer(file
);
595 file
->_ptr
= file
->_base
= NULL
;
596 file
->_bufsiz
= file
->_cnt
= 0;
597 file
->_flag
&= ~_USERBUF
;
600 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
601 static int msvcrt_int_to_base32(int num
, char *str
)
616 *p
= (num
& 31) + '0';
618 *p
+= ('a' - '0' - 10);
625 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
626 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
641 *p
= (num
& 31) + '0';
643 *p
+= ('a' - '0' - 10);
650 /* INTERNAL: Create a wide string from an ascii string */
651 wchar_t *msvcrt_wstrdupa(const char *str
)
653 const unsigned int len
= strlen(str
) + 1 ;
654 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
657 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
661 /*********************************************************************
662 * __iob_func(MSVCRT.@)
664 FILE * CDECL
__iob_func(void)
669 /*********************************************************************
672 int CDECL
_access(const char *filename
, int mode
)
674 DWORD attr
= GetFileAttributesA(filename
);
676 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
678 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
680 _dosmaperr(GetLastError());
683 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
685 _set_errno(ERROR_ACCESS_DENIED
);
691 /*********************************************************************
692 * _access_s (MSVCRT.@)
694 int CDECL
_access_s(const char *filename
, int mode
)
696 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
697 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
703 return _access(filename
, mode
);
706 /*********************************************************************
707 * _waccess (MSVCRT.@)
709 int CDECL
_waccess(const wchar_t *filename
, int mode
)
711 DWORD attr
= GetFileAttributesW(filename
);
713 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
715 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
717 _dosmaperr(GetLastError());
720 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
722 _set_errno(ERROR_ACCESS_DENIED
);
728 /*********************************************************************
729 * _waccess_s (MSVCRT.@)
731 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
733 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
734 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
740 return _waccess(filename
, mode
);
743 /*********************************************************************
746 int CDECL
_chmod(const char *path
, int flags
)
748 DWORD oldFlags
= GetFileAttributesA(path
);
750 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
752 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
753 oldFlags
| FILE_ATTRIBUTE_READONLY
;
755 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
758 _dosmaperr(GetLastError());
762 /*********************************************************************
765 int CDECL
_wchmod(const wchar_t *path
, int flags
)
767 DWORD oldFlags
= GetFileAttributesW(path
);
769 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
771 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
772 oldFlags
| FILE_ATTRIBUTE_READONLY
;
774 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
777 _dosmaperr(GetLastError());
781 /*********************************************************************
784 int CDECL
_unlink(const char *path
)
786 TRACE("%s\n",debugstr_a(path
));
787 if(DeleteFileA(path
))
789 TRACE("failed (%d)\n",GetLastError());
790 _dosmaperr(GetLastError());
794 /*********************************************************************
795 * _wunlink (MSVCRT.@)
797 int CDECL
_wunlink(const wchar_t *path
)
799 TRACE("(%s)\n",debugstr_w(path
));
800 if(DeleteFileW(path
))
802 TRACE("failed (%d)\n",GetLastError());
803 _dosmaperr(GetLastError());
807 /*********************************************************************
810 int CDECL
_commit(int fd
)
812 ioinfo
*info
= get_ioinfo(fd
);
815 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
816 if (info
->handle
== INVALID_HANDLE_VALUE
)
818 else if (!FlushFileBuffers(info
->handle
))
820 if (GetLastError() == ERROR_INVALID_HANDLE
)
822 /* FlushFileBuffers fails for console handles
823 * so we ignore this error.
829 TRACE(":failed-last error (%d)\n",GetLastError());
830 _dosmaperr(GetLastError());
840 release_ioinfo(info
);
844 /* _flushall calls fflush which calls _flushall */
845 int CDECL
fflush(FILE* file
);
847 /* INTERNAL: Flush all stream buffer */
848 static int msvcrt_flush_all_buffers(int mask
)
850 int i
, num_flushed
= 0;
854 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
855 file
= msvcrt_get_file(i
);
859 if(file
->_flag
& mask
) {
867 TRACE(":flushed (%d) handles\n",num_flushed
);
871 /*********************************************************************
872 * _flushall (MSVCRT.@)
874 int CDECL
_flushall(void)
876 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
879 /*********************************************************************
882 int CDECL
fflush(FILE* file
)
885 msvcrt_flush_all_buffers(_IOWRT
);
886 } else if(file
->_flag
& _IOWRT
) {
890 res
= msvcrt_flush_buffer(file
);
892 if(!res && (file->_flag & _IOCOMMIT))
893 res = _commit(file->_file) ? EOF : 0;
898 } else if(file
->_flag
& _IOREAD
) {
901 file
->_ptr
= file
->_base
;
909 /*********************************************************************
912 int CDECL
_close(int fd
)
914 ioinfo
*info
= get_ioinfo(fd
);
918 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
919 if (!(info
->wxflag
& WX_OPEN
)) {
922 ret
= CloseHandle(info
->handle
) ? 0 : -1;
925 WARN(":failed-last error (%d)\n",GetLastError());
926 _dosmaperr(GetLastError());
931 release_ioinfo(info
);
935 /*********************************************************************
938 * MSDN isn't clear on this point, but the remarks for _pipe
939 * indicate file descriptors duplicated with _dup and _dup2 are always
942 int CDECL
_dup2(int od
, int nd
)
946 TRACE("(od=%d, nd=%d)\n", od
, nd
);
948 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
952 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od
)->handle
,
953 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
955 int wxflag
= get_ioinfo_nolock(od
)->wxflag
& ~_O_NOINHERIT
;
959 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
967 /* _dup2 returns 0, not nd, on success */
974 _dosmaperr(GetLastError());
986 /*********************************************************************
989 int CDECL
_dup(int od
)
995 if (_dup2(od
, fd
) == 0)
1003 /*********************************************************************
1006 int CDECL
_eof(int fd
)
1008 ioinfo
*info
= get_ioinfo(fd
);
1009 DWORD curpos
,endpos
;
1010 LONG hcurpos
,hendpos
;
1012 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1014 if (info
->handle
== INVALID_HANDLE_VALUE
)
1016 release_ioinfo(info
);
1020 if (info
->wxflag
& WX_ATEOF
)
1022 release_ioinfo(info
);
1026 /* Otherwise we do it the hard way */
1027 hcurpos
= hendpos
= 0;
1028 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1029 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1031 if (curpos
== endpos
&& hcurpos
== hendpos
)
1033 /* FIXME: shouldn't WX_ATEOF be set here? */
1034 release_ioinfo(info
);
1038 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1039 release_ioinfo(info
);
1043 /*********************************************************************
1044 * _fcloseall (MSVCRT.@)
1046 int CDECL
_fcloseall(void)
1048 int num_closed
= 0, i
;
1052 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1053 file
= msvcrt_get_file(i
);
1055 if (file
->_flag
&& !fclose(file
))
1060 TRACE(":closed (%d) handles\n",num_closed
);
1064 /* free everything on process exit */
1065 void msvcrt_free_io(void)
1073 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1078 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1080 if(__pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1081 DeleteCriticalSection(&__pioinfo
[i
][j
].crit
);
1086 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1088 FILE *file
= msvcrt_get_file(j
);
1089 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1091 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1092 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1096 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1097 free(MSVCRT_fstream
[i
]);
1100 /*********************************************************************
1101 * _lseeki64 (MSVCRT.@)
1103 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1105 ioinfo
*info
= get_ioinfo(fd
);
1108 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1110 if (info
->handle
== INVALID_HANDLE_VALUE
)
1112 release_ioinfo(info
);
1116 if (whence
< 0 || whence
> 2)
1118 release_ioinfo(info
);
1123 TRACE(":fd (%d) to %s pos %s\n",
1124 fd
,wine_dbgstr_longlong(offset
),
1125 (whence
==SEEK_SET
)?"SEEK_SET":
1126 (whence
==SEEK_CUR
)?"SEEK_CUR":
1127 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1129 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1130 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1131 ofs
.QuadPart
= offset
;
1132 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1133 GetLastError() == ERROR_SUCCESS
)
1135 info
->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1136 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1138 release_ioinfo(info
);
1139 return ofs
.QuadPart
;
1141 release_ioinfo(info
);
1142 TRACE(":error-last error (%d)\n",GetLastError());
1143 _dosmaperr(GetLastError());
1147 /*********************************************************************
1150 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1152 return (LONG
)_lseeki64(fd
, offset
, whence
);
1155 /*********************************************************************
1156 * _lock_file (MSVCRT.@)
1158 void CDECL
_lock_file(FILE *file
)
1160 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1161 _lock(_STREAM_LOCKS
+(file
-_iob
));
1162 /* ReactOS: string streams dont need to be locked */
1163 else if(!(file
->_flag
& _IOSTRG
))
1164 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1167 /*********************************************************************
1168 * _unlock_file (MSVCRT.@)
1170 void CDECL
_unlock_file(FILE *file
)
1172 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1173 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1174 /* ReactOS: string streams dont need to be locked */
1175 else if(!(file
->_flag
& _IOSTRG
))
1176 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1180 /*********************************************************************
1181 * _locking (MSVCRT.@)
1183 * This is untested; the underlying LockFile doesn't work yet.
1185 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1187 ioinfo
*info
= get_ioinfo(fd
);
1191 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1192 if (info
->handle
== INVALID_HANDLE_VALUE
)
1194 release_ioinfo(info
);
1198 if (mode
< 0 || mode
> 4)
1200 release_ioinfo(info
);
1205 TRACE(":fd (%d) by 0x%08x mode %s\n",
1206 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1207 (mode
==_LK_LOCK
)?"_LK_LOCK":
1208 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1209 (mode
==_LK_RLCK
)?"_LK_RLCK":
1210 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1213 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1215 release_ioinfo(info
);
1216 FIXME ("Seek failed\n");
1217 *_errno() = EINVAL
; /* FIXME */
1220 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1223 ret
= 1; /* just to satisfy gcc */
1226 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1231 else if (mode
== _LK_UNLCK
)
1232 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1234 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1235 /* FIXME - what about error settings? */
1236 release_ioinfo(info
);
1237 return ret
? 0 : -1;
1240 /*********************************************************************
1241 * _fseeki64 (MSVCRT.@)
1243 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1248 /* Flush output if needed */
1249 if(file
->_flag
& _IOWRT
)
1250 msvcrt_flush_buffer(file
);
1252 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1254 offset
+= _ftelli64(file
);
1257 /* Discard buffered input */
1259 file
->_ptr
= file
->_base
;
1260 /* Reset direction of i/o */
1261 if(file
->_flag
& _IORW
) {
1262 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1264 /* Clear end of file flag */
1265 file
->_flag
&= ~_IOEOF
;
1266 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1272 /*********************************************************************
1275 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1277 return _fseeki64( file
, offset
, whence
);
1280 /*********************************************************************
1281 * _chsize_s (MSVCRT.@)
1283 int CDECL
_chsize_s(int fd
, __int64 size
)
1289 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1291 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1293 info
= get_ioinfo(fd
);
1294 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1296 /* save the current file pointer */
1297 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1300 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1303 ret
= SetEndOfFile(info
->handle
);
1304 if (!ret
) _dosmaperr(GetLastError());
1307 /* restore the file pointer */
1308 _lseeki64(fd
, cur
, SEEK_SET
);
1312 release_ioinfo(info
);
1313 return ret
? 0 : *_errno();
1316 /*********************************************************************
1317 * _chsize (MSVCRT.@)
1319 int CDECL
_chsize(int fd
, long size
)
1321 /* _chsize_s returns errno on failure but _chsize should return -1 */
1322 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1325 /*********************************************************************
1326 * clearerr (MSVCRT.@)
1328 void CDECL
clearerr(FILE* file
)
1330 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1333 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1337 /*********************************************************************
1340 void CDECL
rewind(FILE* file
)
1342 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1345 fseek(file
, 0L, SEEK_SET
);
1350 static int msvcrt_get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1352 int plus
= strchrW(mode
, '+') != NULL
;
1354 TRACE("%s\n", debugstr_w(mode
));
1356 while(*mode
== ' ') mode
++;
1361 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1362 *stream_flags
= plus
? _IORW
: _IOREAD
;
1365 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1366 *stream_flags
= plus
? _IORW
: _IOWRT
;
1369 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1370 *stream_flags
= plus
? _IORW
: _IOWRT
;
1373 MSVCRT_INVALID_PMT(0, EINVAL
);
1377 *stream_flags
|= _commode
;
1379 while (*mode
&& *mode
!=',')
1383 *open_flags
|= _O_BINARY
;
1384 *open_flags
&= ~_O_TEXT
;
1387 *open_flags
|= _O_TEXT
;
1388 *open_flags
&= ~_O_BINARY
;
1391 *open_flags
|= _O_TEMPORARY
;
1394 *open_flags
|= _O_SHORT_LIVED
;
1397 *stream_flags
|= _IOCOMMIT
;
1400 *stream_flags
&= ~_IOCOMMIT
;
1403 *open_flags
|= _O_NOINHERIT
;
1412 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1415 ERR("incorrect mode flag: %c\n", mode
[-1]);
1421 static const WCHAR ccs
[] = {'c','c','s'};
1422 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1423 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1424 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1427 while(*mode
== ' ') mode
++;
1428 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1430 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1431 while(*mode
== ' ') mode
++;
1432 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1435 while(*mode
== ' ') mode
++;
1437 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1439 *open_flags
|= _O_U8TEXT
;
1440 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1442 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1444 *open_flags
|= _O_U16TEXT
;
1445 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1447 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1449 *open_flags
|= _O_WTEXT
;
1450 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1454 MSVCRT_INVALID_PMT(0, EINVAL
);
1458 while(*mode
== ' ') mode
++;
1461 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1466 /*********************************************************************
1467 * _fdopen (MSVCRT.@)
1469 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1472 wchar_t *modeW
= NULL
;
1474 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1476 ret
= _wfdopen(fd
, modeW
);
1482 /*********************************************************************
1483 * _wfdopen (MSVCRT.@)
1485 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1487 int open_flags
, stream_flags
;
1490 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1493 if (!(file
= msvcrt_alloc_fp()))
1495 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1500 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1506 /*********************************************************************
1507 * _filelength (MSVCRT.@)
1509 LONG CDECL
_filelength(int fd
)
1511 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1514 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1517 if (endPos
!= curPos
)
1518 _lseek(fd
, curPos
, SEEK_SET
);
1525 /*********************************************************************
1526 * _filelengthi64 (MSVCRT.@)
1528 __int64 CDECL
_filelengthi64(int fd
)
1530 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1533 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1536 if (endPos
!= curPos
)
1537 _lseeki64(fd
, curPos
, SEEK_SET
);
1544 /*********************************************************************
1545 * _fileno (MSVCRT.@)
1547 int CDECL
_fileno(FILE* file
)
1549 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1553 /*********************************************************************
1554 * _get_osfhandle (MSVCRT.@)
1556 intptr_t CDECL
_get_osfhandle(int fd
)
1558 HANDLE hand
= fdtoh(fd
);
1559 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1561 return (intptr_t)hand
;
1564 /*********************************************************************
1565 * _mktemp (MSVCRT.@)
1567 char * CDECL
_mktemp(char *pattern
)
1570 char *retVal
= pattern
;
1578 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1582 id
= GetCurrentProcessId();
1586 int tempNum
= id
/ 10;
1587 *pattern
-- = id
- (tempNum
* 10) + '0';
1593 *pattern
= letter
++;
1594 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1596 } while(letter
<= 'z');
1600 /*********************************************************************
1601 * _wmktemp (MSVCRT.@)
1603 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1606 wchar_t *retVal
= pattern
;
1608 wchar_t letter
= 'a';
1611 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1615 id
= GetCurrentProcessId();
1619 int tempNum
= id
/ 10;
1620 *pattern
-- = id
- (tempNum
* 10) + '0';
1626 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1627 GetLastError() == ERROR_FILE_NOT_FOUND
)
1629 *pattern
= letter
++;
1630 } while(letter
!= '|');
1634 /*static*/ unsigned split_oflags(unsigned oflags
)
1637 unsigned unsupp
; /* until we support everything */
1639 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1640 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1641 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1642 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1643 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1644 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1645 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1646 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1647 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1649 if ((unsupp
= oflags
& ~(
1650 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1651 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1652 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1654 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1655 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1657 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1662 /*********************************************************************
1665 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1668 SECURITY_ATTRIBUTES sa
;
1669 HANDLE readHandle
, writeHandle
;
1677 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1678 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1679 sa
.lpSecurityDescriptor
= NULL
;
1680 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1682 unsigned int wxflags
= split_oflags(textmode
);
1686 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1690 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1699 CloseHandle(writeHandle
);
1705 CloseHandle(readHandle
);
1706 CloseHandle(writeHandle
);
1712 _dosmaperr(GetLastError());
1717 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1719 char bom
[sizeof(utf8_bom
)];
1722 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1724 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1727 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1728 oflags
|= _O_U8TEXT
;
1729 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1731 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1732 oflags
|= _O_U16TEXT
;
1734 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1740 /*********************************************************************
1741 * _wsopen_s (MSVCRT.@)
1743 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1745 DWORD access
= 0, creation
= 0, attrib
;
1746 SECURITY_ATTRIBUTES sa
;
1747 DWORD sharing
, type
;
1751 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1752 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1754 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1757 wxflag
= split_oflags(oflags
);
1758 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1760 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1761 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1762 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1765 if (oflags
& _O_CREAT
)
1767 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1768 FIXME(": pmode 0x%04x ignored\n", pmode
);
1770 WARN(": pmode 0x%04x ignored\n", pmode
);
1772 if (oflags
& _O_EXCL
)
1773 creation
= CREATE_NEW
;
1774 else if (oflags
& _O_TRUNC
)
1775 creation
= CREATE_ALWAYS
;
1777 creation
= OPEN_ALWAYS
;
1779 else /* no _O_CREAT */
1781 if (oflags
& _O_TRUNC
)
1782 creation
= TRUNCATE_EXISTING
;
1784 creation
= OPEN_EXISTING
;
1793 sharing
= FILE_SHARE_READ
;
1796 sharing
= FILE_SHARE_WRITE
;
1799 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1802 ERR( "Unhandled shflags 0x%x\n", shflags
);
1805 attrib
= FILE_ATTRIBUTE_NORMAL
;
1807 if (oflags
& _O_TEMPORARY
)
1809 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1811 sharing
|= FILE_SHARE_DELETE
;
1814 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1815 sa
.lpSecurityDescriptor
= NULL
;
1816 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1818 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1819 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1820 && !(access
&GENERIC_READ
))
1822 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1823 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1824 if (hand
!= INVALID_HANDLE_VALUE
)
1826 oflags
= check_bom(hand
, oflags
, FALSE
);
1830 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1833 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1834 if (hand
== INVALID_HANDLE_VALUE
) {
1835 WARN(":failed-last error (%d)\n",GetLastError());
1836 _dosmaperr(GetLastError());
1840 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1842 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1843 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1844 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1846 if (oflags
& _O_U8TEXT
)
1848 DWORD written
= 0, tmp
;
1850 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1851 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1853 if (written
!= sizeof(utf8_bom
)) {
1854 WARN("error writing BOM\n");
1856 _dosmaperr(GetLastError());
1862 DWORD written
= 0, tmp
;
1864 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1865 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1867 if (written
!= sizeof(utf16_bom
))
1869 WARN("error writing BOM\n");
1871 _dosmaperr(GetLastError());
1876 else if (access
& GENERIC_READ
)
1877 oflags
= check_bom(hand
, oflags
, TRUE
);
1880 type
= GetFileType(hand
);
1881 if (type
== FILE_TYPE_CHAR
)
1883 else if (type
== FILE_TYPE_PIPE
)
1886 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1890 if (oflags
& _O_WTEXT
)
1891 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1892 else if (oflags
& _O_U16TEXT
)
1893 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
1894 else if (oflags
& _O_U8TEXT
)
1895 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
1897 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1901 /*********************************************************************
1902 * _wsopen (MSVCRT.@)
1904 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1909 if (oflags
& _O_CREAT
)
1913 __ms_va_start(ap
, shflags
);
1914 pmode
= va_arg(ap
, int);
1920 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1924 /*********************************************************************
1925 * _sopen_s (MSVCRT.@)
1927 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1932 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1935 ret
= _wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1940 /*********************************************************************
1943 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1948 if (oflags
& _O_CREAT
)
1952 va_start(ap
, shflags
);
1953 pmode
= va_arg(ap
, int);
1959 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1963 /*********************************************************************
1966 int CDECL
_open( const char *path
, int flags
, ... )
1970 if (flags
& _O_CREAT
)
1973 va_start(ap
, flags
);
1974 pmode
= va_arg(ap
, int);
1976 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1979 return _sopen( path
, flags
, _SH_DENYNO
);
1982 /*********************************************************************
1985 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1989 if (flags
& _O_CREAT
)
1992 va_start(ap
, flags
);
1993 pmode
= va_arg(ap
, int);
1995 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
1998 return _wsopen( path
, flags
, _SH_DENYNO
);
2001 /*********************************************************************
2004 int CDECL
_creat(const char *path
, int flags
)
2006 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2007 return _open(path
, usedFlags
);
2010 /*********************************************************************
2011 * _wcreat (MSVCRT.@)
2013 int CDECL
_wcreat(const wchar_t *path
, int flags
)
2015 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2016 return _wopen(path
, usedFlags
);
2019 /*********************************************************************
2020 * _open_osfhandle (MSVCRT.@)
2022 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2027 /* _O_RDONLY (0) always matches, so set the read flag
2028 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2029 * file, so set the write flag. It also only sets _O_TEXT if it wants
2030 * text - it never sets _O_BINARY.
2032 /* don't let split_oflags() decide the mode if no mode is passed */
2033 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2034 oflags
|= _O_BINARY
;
2036 flags
= GetFileType((HANDLE
)handle
);
2037 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2039 _dosmaperr(GetLastError());
2043 if (flags
== FILE_TYPE_CHAR
)
2045 else if (flags
== FILE_TYPE_PIPE
)
2049 flags
|= split_oflags(oflags
);
2051 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2052 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2056 /*********************************************************************
2059 int CDECL
_rmtmp(void)
2061 int num_removed
= 0, i
;
2065 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2066 file
= msvcrt_get_file(i
);
2068 if (file
->_tmpfname
)
2077 TRACE(":removed (%d) temp files\n",num_removed
);
2081 static inline int get_utf8_char_len(char ch
)
2083 if((ch
&0xf8) == 0xf0)
2085 else if((ch
&0xf0) == 0xe0)
2087 else if((ch
&0xe0) == 0xc0)
2092 /*********************************************************************
2093 * (internal) read_utf8
2095 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2097 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2098 HANDLE hand
= fdinfo
->handle
;
2099 char min_buf
[4], *readbuf
, lookahead
;
2100 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2102 /* make the buffer big enough to hold at least one character */
2103 /* read bytes have to fit to output and lookahead buffers */
2105 readbuf_size
= count
< 4 ? 4 : count
;
2106 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2111 if(fdinfo
->lookahead
[0] != '\n') {
2112 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2113 fdinfo
->lookahead
[0] = '\n';
2115 if(fdinfo
->lookahead
[1] != '\n') {
2116 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2117 fdinfo
->lookahead
[1] = '\n';
2119 if(fdinfo
->lookahead
[2] != '\n') {
2120 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2121 fdinfo
->lookahead
[2] = '\n';
2126 /* NOTE: this case is broken in native dll, reading
2127 * sometimes fails when small buffer is passed
2130 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2131 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2132 fdinfo
->wxflag
|= WX_ATEOF
;
2135 _dosmaperr(GetLastError());
2138 }else if(!num_read
) {
2139 fdinfo
->wxflag
|= WX_ATEOF
;
2145 char_len
= get_utf8_char_len(readbuf
[0]);
2147 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2151 if(readbuf
[0] == '\n')
2152 fdinfo
->wxflag
|= WX_READNL
;
2154 fdinfo
->wxflag
&= ~WX_READNL
;
2156 if(readbuf
[0] == 0x1a) {
2157 fdinfo
->wxflag
|= WX_ATEOF
;
2161 if(readbuf
[0] == '\r') {
2162 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2164 else if(lookahead
== '\n')
2168 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2169 fdinfo
->lookahead
[0] = lookahead
;
2171 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2176 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2177 _dosmaperr(GetLastError());
2184 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2187 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2188 fdinfo
->wxflag
|= WX_ATEOF
;
2189 if (readbuf
!= min_buf
) free(readbuf
);
2192 _dosmaperr(GetLastError());
2193 if (readbuf
!= min_buf
) free(readbuf
);
2196 }else if(!pos
&& !num_read
) {
2197 fdinfo
->wxflag
|= WX_ATEOF
;
2198 if (readbuf
!= min_buf
) free(readbuf
);
2203 if(readbuf
[0] == '\n')
2204 fdinfo
->wxflag
|= WX_READNL
;
2206 fdinfo
->wxflag
&= ~WX_READNL
;
2208 /* Find first byte of last character (may be incomplete) */
2209 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2210 if((readbuf
[i
]&0xc0) != 0x80)
2212 char_len
= get_utf8_char_len(readbuf
[i
]);
2213 if(char_len
+i
<= pos
)
2216 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2218 fdinfo
->lookahead
[0] = readbuf
[i
];
2220 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2222 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2224 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2228 for(i
=0, j
=0; i
<pos
; i
++) {
2229 if(readbuf
[i
] == 0x1a) {
2230 fdinfo
->wxflag
|= WX_ATEOF
;
2234 /* strip '\r' if followed by '\n' */
2235 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2236 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2237 readbuf
[j
++] = '\r';
2238 }else if(lookahead
== '\n' && j
==0) {
2239 readbuf
[j
++] = '\n';
2241 if(lookahead
!= '\n')
2242 readbuf
[j
++] = '\r';
2244 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2245 fdinfo
->lookahead
[0] = lookahead
;
2247 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2249 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2250 readbuf
[j
++] = readbuf
[i
];
2255 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2256 _dosmaperr(GetLastError());
2257 if (readbuf
!= min_buf
) free(readbuf
);
2261 if (readbuf
!= min_buf
) free(readbuf
);
2265 /*********************************************************************
2268 * When reading \r as last character in text mode, read() positions
2269 * the file pointer on the \r character while getc() goes on to
2272 static int read_i(int fd
, void *buf
, unsigned int count
)
2274 DWORD num_read
, utf16
;
2275 char *bufstart
= buf
;
2276 HANDLE hand
= fdtoh(fd
);
2277 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2282 if (fdinfo
->wxflag
& WX_ATEOF
) {
2283 TRACE("already at EOF, returning 0\n");
2286 /* Don't trace small reads, it gets *very* annoying */
2288 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2289 if (hand
== INVALID_HANDLE_VALUE
)
2295 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2296 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2302 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2303 return read_utf8(fd
, buf
, count
);
2305 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2307 if (fdinfo
->lookahead
[0] != '\n')
2309 bufstart
[0] = fdinfo
->lookahead
[0];
2310 fdinfo
->lookahead
[0] = '\n';
2314 bufstart
[1] = fdinfo
->lookahead
[1];
2315 fdinfo
->lookahead
[1] = '\n';
2318 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2319 num_read
+= 1+utf16
;
2324 if(utf16
&& (num_read
&1))
2326 /* msvcr90 uses uninitialized value from the buffer in this case */
2327 /* msvcrt ignores additional data */
2328 ERR("got odd number of bytes in UTF16 mode\n");
2332 if (count
!= 0 && num_read
== 0)
2334 fdinfo
->wxflag
|= WX_ATEOF
;
2335 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2337 else if (fdinfo
->wxflag
& WX_TEXT
)
2341 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2342 fdinfo
->wxflag
|= WX_READNL
;
2344 fdinfo
->wxflag
&= ~WX_READNL
;
2346 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2348 /* in text mode, a ctrl-z signals EOF */
2349 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2351 fdinfo
->wxflag
|= WX_ATEOF
;
2352 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2356 /* in text mode, strip \r if followed by \n */
2357 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2362 lookahead
[1] = '\n';
2363 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2365 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2367 bufstart
[j
++] = '\n';
2368 if(utf16
) bufstart
[j
++] = 0;
2372 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2374 bufstart
[j
++] = '\r';
2375 if(utf16
) bufstart
[j
++] = 0;
2378 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2380 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2382 bufstart
[j
++] = '\n';
2383 if (utf16
) bufstart
[j
++] = 0;
2387 fdinfo
->lookahead
[0] = lookahead
[0];
2388 fdinfo
->lookahead
[1] = lookahead
[1];
2392 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2397 bufstart
[j
++] = '\r';
2398 if(utf16
) bufstart
[j
++] = 0;
2401 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2402 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2404 bufstart
[j
++] = bufstart
[i
];
2405 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2413 if (GetLastError() == ERROR_BROKEN_PIPE
)
2415 TRACE(":end-of-pipe\n");
2416 fdinfo
->wxflag
|= WX_ATEOF
;
2421 TRACE(":failed-last error (%d)\n",GetLastError());
2427 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2431 /*********************************************************************
2434 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2437 num_read
= read_i(fd
, buf
, count
);
2441 /*********************************************************************
2442 * _setmode (MSVCRT.@)
2444 int CDECL
_setmode(int fd
,int mode
)
2446 int ret
= get_ioinfo_nolock(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2447 if(ret
==_O_TEXT
&& (get_ioinfo_nolock(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2450 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2451 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2456 if(mode
== _O_BINARY
) {
2457 get_ioinfo_nolock(fd
)->wxflag
&= ~WX_TEXT
;
2458 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2462 get_ioinfo_nolock(fd
)->wxflag
|= WX_TEXT
;
2464 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2465 else if(mode
== _O_U8TEXT
)
2466 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2468 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2474 /*********************************************************************
2477 long CDECL
_tell(int fd
)
2479 return _lseek(fd
, 0, SEEK_CUR
);
2482 /*********************************************************************
2483 * _telli64 (MSVCRT.@)
2485 __int64 CDECL
_telli64(int fd
)
2487 return _lseeki64(fd
, 0, SEEK_CUR
);
2490 /*********************************************************************
2491 * _tempnam (MSVCRT.@)
2493 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2495 char tmpbuf
[MAX_PATH
];
2496 const char *tmp_dir
= getenv("TMP");
2498 if (tmp_dir
) dir
= tmp_dir
;
2500 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2501 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2503 TRACE("got name (%s)\n",tmpbuf
);
2504 DeleteFileA(tmpbuf
);
2505 return _strdup(tmpbuf
);
2507 TRACE("failed (%d)\n",GetLastError());
2511 /*********************************************************************
2512 * _wtempnam (MSVCRT.@)
2514 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2516 wchar_t tmpbuf
[MAX_PATH
];
2518 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2519 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2521 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2522 DeleteFileW(tmpbuf
);
2523 return _wcsdup(tmpbuf
);
2525 TRACE("failed (%d)\n",GetLastError());
2529 /*********************************************************************
2532 int CDECL
_umask(int umask
)
2534 int old_umask
= MSVCRT_umask
;
2535 TRACE("(%d)\n",umask
);
2536 MSVCRT_umask
= umask
;
2540 /*********************************************************************
2543 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2546 ioinfo
*info
= get_ioinfo_nolock(fd
);
2547 HANDLE hand
= info
->handle
;
2549 /* Don't trace small writes, it gets *very* annoying */
2552 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2554 if (hand
== INVALID_HANDLE_VALUE
)
2560 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2566 /* If appending, go to EOF */
2567 if (info
->wxflag
& WX_APPEND
)
2568 _lseek(fd
, 0, FILE_END
);
2570 if (!(info
->wxflag
& WX_TEXT
))
2572 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2573 && (num_written
== count
))
2575 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2576 hand
, GetLastError());
2581 unsigned int i
, j
, nr_lf
, size
;
2584 const char *s
= buf
, *buf_start
= buf
;
2586 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2588 /* find number of \n */
2589 for (nr_lf
=0, i
=0; i
<count
; i
++)
2595 if ((q
= p
= malloc(size
)))
2597 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2606 FIXME("Malloc failed\n");
2618 else if (info
->exflag
& EF_UTF16
)
2620 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2621 if (s
[i
]=='\n' && s
[i
+1]==0)
2626 if ((q
= p
= malloc(size
)))
2628 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2630 if (s
[i
]=='\n' && s
[i
+1]==0)
2641 FIXME("Malloc failed\n");
2657 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2658 if (s
[i
]=='\n' && s
[i
+1]==0)
2661 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2663 _dosmaperr(GetLastError());
2668 size
= conv_len
+nr_lf
;
2669 if((p
= malloc(count
+nr_lf
*2+size
)))
2671 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2673 if (s
[i
]=='\n' && s
[i
+1]==0)
2681 q
= p
+count
+nr_lf
*2;
2682 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2683 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2687 FIXME("Malloc failed\n");
2694 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2698 if (num_written
!= size
)
2700 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2701 fd
, hand
, GetLastError(), num_written
);
2703 return s
- buf_start
;
2711 /*********************************************************************
2714 int CDECL
_putw(int val
, FILE* file
)
2719 len
= _write(file
->_file
, &val
, sizeof(val
));
2720 if (len
== sizeof(val
)) {
2725 file
->_flag
|= _IOERR
;
2730 /*********************************************************************
2733 int CDECL
fclose(FILE* file
)
2739 free(file
->_tmpfname
);
2740 file
->_tmpfname
= NULL
;
2741 /* flush stdio buffers */
2742 if(file
->_flag
& _IOWRT
)
2744 if(file
->_flag
& _IOMYBUF
)
2747 r
=_close(file
->_file
);
2751 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2752 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2754 if(file
== msvcrt_get_file(MSVCRT_stream_idx
-1)) {
2755 while(MSVCRT_stream_idx
>3 && !file
->_flag
) {
2756 MSVCRT_stream_idx
--;
2757 file
= msvcrt_get_file(MSVCRT_stream_idx
-1);
2761 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2764 /*********************************************************************
2767 int CDECL
feof(FILE* file
)
2769 return file
->_flag
& _IOEOF
;
2772 /*********************************************************************
2775 int CDECL
ferror(FILE* file
)
2777 return file
->_flag
& _IOERR
;
2780 /*********************************************************************
2781 * _filbuf (MSVCRT.@)
2783 int CDECL
_filbuf(FILE* file
)
2788 if(file
->_flag
& _IOSTRG
) {
2793 /* Allocate buffer if needed */
2794 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
2795 msvcrt_alloc_buffer(file
);
2797 if(!(file
->_flag
& _IOREAD
)) {
2798 if(file
->_flag
& _IORW
)
2799 file
->_flag
|= _IOREAD
;
2806 if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
2808 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2809 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2817 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2819 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2826 file
->_ptr
= file
->_base
+1;
2827 c
= *(unsigned char *)file
->_base
;
2833 /*********************************************************************
2836 int CDECL
fgetc(FILE* file
)
2844 i
= (unsigned char *)file
->_ptr
++;
2853 /*********************************************************************
2854 * _fgetchar (MSVCRT.@)
2856 int CDECL
_fgetchar(void)
2858 return fgetc(stdin
);
2861 /*********************************************************************
2864 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2867 char * buf_start
= s
;
2869 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2870 file
,file
->_file
,s
,size
);
2874 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2879 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2881 TRACE(":nothing read\n");
2885 if ((cc
!= EOF
) && (size
> 1))
2888 TRACE(":got %s\n", debugstr_a(buf_start
));
2893 /*********************************************************************
2896 wint_t CDECL
fgetwc(FILE* file
)
2903 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2904 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
2907 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2916 char mbs
[MB_LEN_MAX
];
2922 if(isleadbyte((unsigned char)mbs
[0])) {
2933 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2941 /*********************************************************************
2944 int CDECL
_getw(FILE* file
)
2952 for (j
=0; j
<sizeof(int); j
++) {
2955 file
->_flag
|= _IOEOF
;
2966 /*********************************************************************
2969 wint_t CDECL
getwc(FILE* file
)
2971 return fgetwc(file
);
2974 /*********************************************************************
2975 * _fgetwchar (MSVCRT.@)
2977 wint_t CDECL
_fgetwchar(void)
2979 return fgetwc(stdin
);
2982 /*********************************************************************
2983 * getwchar (MSVCRT.@)
2985 wint_t CDECL
getwchar(void)
2987 return _fgetwchar();
2990 /*********************************************************************
2993 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2996 wchar_t * buf_start
= s
;
2998 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2999 file
,file
->_file
,s
,size
);
3003 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
3008 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3010 TRACE(":nothing read\n");
3014 if ((cc
!= WEOF
) && (size
> 1))
3017 TRACE(":got %s\n", debugstr_w(buf_start
));
3022 /*********************************************************************
3025 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3027 size_t wrcnt
=size
* nmemb
;
3036 if(file
->_cnt
< 0) {
3037 WARN("negative file->_cnt value in %p\n", file
);
3038 file
->_flag
|= MSVCRT__IOERR
;
3043 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3044 memcpy(file
->_ptr
, ptr
, pcnt
);
3049 ptr
= (const char*)ptr
+ pcnt
;
3050 } else if((file
->_flag
& _IONBF
)
3051 || ((file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3052 || (!(file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3056 if(file
->_flag
& _IONBF
)
3058 else if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
)))
3059 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3061 bufsiz
= file
->_bufsiz
;
3063 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3065 if(msvcrt_flush_buffer(file
) == EOF
)
3068 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
3069 file
->_flag
|= _IOERR
;
3074 ptr
= (const char*)ptr
+ pcnt
;
3076 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3080 ptr
= (const char*)ptr
+ 1;
3085 return written
/ size
;
3088 /*********************************************************************
3090 * FORKED for ReactOS, don't sync with Wine!
3092 * - http://jira.reactos.org/browse/CORE-6495
3093 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3095 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3097 /* If this is a real file stream (and not some temporary one for
3098 sprintf-like functions), check whether it is opened in text mode.
3099 In this case, we have to perform an implicit conversion to ANSI. */
3100 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo_nolock(stream
->_file
)->wxflag
& WX_TEXT
)
3102 /* Convert to multibyte in text mode */
3103 char mbc
[MB_LEN_MAX
];
3106 mb_return
= wctomb(mbc
, c
);
3111 /* Output all characters */
3112 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3117 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3124 /*********************************************************************
3125 * _fputwchar (MSVCRT.@)
3127 wint_t CDECL
_fputwchar(wint_t wc
)
3129 return fputwc(wc
, stdout
);
3132 /*********************************************************************
3133 * _wfsopen (MSVCRT.@)
3135 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3138 int open_flags
, stream_flags
, fd
;
3140 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3142 /* map mode string to open() flags. "man fopen" for possibilities. */
3143 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3147 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3150 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3152 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3159 TRACE(":got (%p)\n",file
);
3160 if (fd
>= 0 && !file
)
3166 /*********************************************************************
3167 * _fsopen (MSVCRT.@)
3169 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3172 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3174 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3175 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3179 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3182 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3187 ret
= _wfsopen(pathW
, modeW
, share
);
3194 /*********************************************************************
3197 FILE * CDECL
fopen(const char *path
, const char *mode
)
3199 return _fsopen( path
, mode
, _SH_DENYNO
);
3202 /*********************************************************************
3203 * fopen_s (MSVCRT.@)
3205 int CDECL
fopen_s(FILE** pFile
,
3206 const char *filename
, const char *mode
)
3208 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
3209 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
3210 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
3212 *pFile
= fopen(filename
, mode
);
3219 /*********************************************************************
3220 * _wfopen (MSVCRT.@)
3222 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3224 return _wfsopen( path
, mode
, _SH_DENYNO
);
3227 /*********************************************************************
3228 * _wfopen_s (MSVCRT.@)
3230 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3231 const wchar_t *mode
)
3233 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3234 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3239 *pFile
= _wfopen(filename
, mode
);
3246 /* fputc calls _flsbuf which calls fputc */
3247 int CDECL
_flsbuf(int c
, FILE* file
);
3249 /*********************************************************************
3252 int CDECL
fputc(int c
, FILE* file
)
3262 res
= msvcrt_flush_buffer(file
);
3264 return res
? res
: c
;
3271 res
= _flsbuf(c
, file
);
3277 /*********************************************************************
3278 * _fputchar (MSVCRT.@)
3280 int CDECL
_fputchar(int c
)
3282 return fputc(c
, stdout
);
3285 /*********************************************************************
3288 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3290 size_t rcnt
=size
* nmemb
;
3299 /* first buffered data */
3301 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3302 memcpy(ptr
, file
->_ptr
, pcnt
);
3307 ptr
= (char*)ptr
+ pcnt
;
3308 } else if(!(file
->_flag
& _IOREAD
)) {
3309 if(file
->_flag
& _IORW
) {
3310 file
->_flag
|= _IOREAD
;
3317 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
3318 msvcrt_alloc_buffer(file
);
3323 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& (file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
3324 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3325 file
->_ptr
= file
->_base
;
3326 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3327 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3328 if (i
> 0 && i
< file
->_cnt
) {
3329 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3330 file
->_flag
&= ~_IOEOF
;
3333 memcpy(ptr
, file
->_ptr
, i
);
3337 } else if (rcnt
> INT_MAX
) {
3338 i
= _read(file
->_file
, ptr
, INT_MAX
);
3339 } else if (rcnt
< BUFSIZ
) {
3340 i
= _read(file
->_file
, ptr
, rcnt
);
3342 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3346 ptr
= (char *)ptr
+i
;
3347 /* expose feof condition in the flags
3348 * MFC tests file->_flag for feof, and doesn't call feof())
3350 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
3351 file
->_flag
|= _IOEOF
;
3354 file
->_flag
|= _IOERR
;
3365 /*********************************************************************
3366 * _wfreopen (MSVCRT.@)
3369 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3371 int open_flags
, stream_flags
, fd
;
3373 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3376 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3381 /* map mode string to open() flags. "man fopen" for possibilities. */
3382 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3386 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3389 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
3392 WARN(":failed-last error (%d)\n",GetLastError());
3393 _dosmaperr(GetLastError());
3402 /*********************************************************************
3403 * freopen (MSVCRT.@)
3406 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3409 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3411 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3412 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3418 ret
= _wfreopen(pathW
, modeW
, file
);
3425 /*********************************************************************
3426 * fsetpos (MSVCRT.@)
3428 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3433 /* Note that all this has been lifted 'as is' from fseek */
3434 if(file
->_flag
& _IOWRT
)
3435 msvcrt_flush_buffer(file
);
3437 /* Discard buffered input */
3439 file
->_ptr
= file
->_base
;
3441 /* Reset direction of i/o */
3442 if(file
->_flag
& _IORW
) {
3443 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3446 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3451 /*********************************************************************
3452 * _ftelli64 (MSVCRT.@)
3454 __int64 CDECL
_ftelli64(FILE* file
)
3459 pos
= _telli64(file
->_file
);
3464 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
3465 if(file
->_flag
& _IOWRT
) {
3466 pos
+= file
->_ptr
- file
->_base
;
3468 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
3471 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3475 } else if(!file
->_cnt
) { /* nothing to do */
3476 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3480 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
3481 for(i
=0; i
<file
->_cnt
; i
++)
3482 if(file
->_ptr
[i
] == '\n')
3488 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3493 pos
-= file
->_bufsiz
;
3494 pos
+= file
->_ptr
- file
->_base
;
3496 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
3497 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
3500 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3511 /*********************************************************************
3514 LONG CDECL
ftell(FILE* file
)
3516 return (LONG
)_ftelli64(file
);
3519 /*********************************************************************
3520 * fgetpos (MSVCRT.@)
3522 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3524 *pos
= _ftelli64(file
);
3530 /*********************************************************************
3533 int CDECL
fputs(const char *s
, FILE* file
)
3535 size_t len
= strlen(s
);
3539 ret
= fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
3544 /*********************************************************************
3547 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3549 size_t i
, len
= strlenW(s
);
3554 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3555 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3560 tmp_buf
= add_std_buffer(file
);
3561 for (i
=0; i
<len
; i
++) {
3562 if(fputwc(s
[i
], file
) == WEOF
) {
3563 if(tmp_buf
) remove_std_buffer(file
);
3569 if(tmp_buf
) remove_std_buffer(file
);
3574 /*********************************************************************
3575 * getchar (MSVCRT.@)
3577 int CDECL
getchar(void)
3579 return fgetc(stdin
);
3582 /*********************************************************************
3585 int CDECL
getc(FILE* file
)
3590 /*********************************************************************
3593 char * CDECL
gets(char *buf
)
3596 char * buf_start
= buf
;
3599 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3601 if(cc
!= '\r') *buf
++ = (char)cc
;
3605 TRACE("got '%s'\n", buf_start
);
3606 _unlock_file(stdin
);
3610 /*********************************************************************
3613 wchar_t* CDECL
_getws(wchar_t* buf
)
3619 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3623 *buf
++ = (wchar_t)cc
;
3627 TRACE("got %s\n", debugstr_w(ws
));
3628 _unlock_file(stdin
);
3632 /*********************************************************************
3635 int CDECL
putc(int c
, FILE* file
)
3637 return fputc(c
, file
);
3640 /*********************************************************************
3641 * putchar (MSVCRT.@)
3643 int CDECL
putchar(int c
)
3645 return fputc(c
, stdout
);
3648 /*********************************************************************
3649 * _putwch (MSVCRT.@)
3651 wint_t CDECL
_putwch(wchar_t c
)
3653 return fputwc(c
, stdout
);
3656 /*********************************************************************
3659 int CDECL
puts(const char *s
)
3661 size_t len
= strlen(s
);
3665 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3666 _unlock_file(stdout
);
3670 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3671 _unlock_file(stdout
);
3675 /*********************************************************************
3678 int CDECL
_putws(const wchar_t *s
)
3680 static const wchar_t nl
= '\n';
3681 size_t len
= strlenW(s
);
3685 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3686 _unlock_file(stdout
);
3690 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3691 _unlock_file(stdout
);
3695 /*********************************************************************
3698 int CDECL
remove(const char *path
)
3700 TRACE("(%s)\n",path
);
3701 if (DeleteFileA(path
))
3703 TRACE(":failed (%d)\n",GetLastError());
3704 _dosmaperr(GetLastError());
3708 /*********************************************************************
3709 * _wremove (MSVCRT.@)
3711 int CDECL
_wremove(const wchar_t *path
)
3713 TRACE("(%s)\n",debugstr_w(path
));
3714 if (DeleteFileW(path
))
3716 TRACE(":failed (%d)\n",GetLastError());
3717 _dosmaperr(GetLastError());
3721 /*********************************************************************
3724 int CDECL
rename(const char *oldpath
,const char *newpath
)
3726 TRACE(":from %s to %s\n",oldpath
,newpath
);
3727 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3729 TRACE(":failed (%d)\n",GetLastError());
3730 _dosmaperr(GetLastError());
3734 /*********************************************************************
3735 * _wrename (MSVCRT.@)
3737 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3739 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3740 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3742 TRACE(":failed (%d)\n",GetLastError());
3743 _dosmaperr(GetLastError());
3747 /*********************************************************************
3748 * setvbuf (MSVCRT.@)
3750 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3752 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
3753 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
3754 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
3759 if(file
->_flag
& _IOMYBUF
)
3761 file
->_flag
&= ~(_IONBF
| _IOMYBUF
| _USERBUF
);
3764 if(mode
== _IONBF
) {
3765 file
->_flag
|= _IONBF
;
3766 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
3769 file
->_base
= file
->_ptr
= buf
;
3770 file
->_flag
|= _USERBUF
;
3771 file
->_bufsiz
= size
;
3773 file
->_base
= file
->_ptr
= malloc(size
);
3780 file
->_flag
|= _IOMYBUF
;
3781 file
->_bufsiz
= size
;
3787 /*********************************************************************
3790 void CDECL
setbuf(FILE* file
, char *buf
)
3792 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3795 /*********************************************************************
3798 char * CDECL
tmpnam(char *s
)
3805 thread_data_t
*data
= msvcrt_get_thread_data();
3807 if(!data
->tmpnam_buffer
)
3808 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3810 s
= data
->tmpnam_buffer
;
3813 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3814 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3815 for (count
= 0; count
< TMP_MAX
; count
++)
3817 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
3818 memcpy(p
, tmpstr
, size
);
3820 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3821 GetLastError() == ERROR_FILE_NOT_FOUND
)
3827 /*********************************************************************
3828 * _wtmpnam (MSVCRT.@)
3830 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3832 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3837 thread_data_t
*data
= msvcrt_get_thread_data();
3839 if(!data
->wtmpnam_buffer
)
3840 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3842 s
= data
->wtmpnam_buffer
;
3845 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3846 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3847 for (count
= 0; count
< TMP_MAX
; count
++)
3849 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
3850 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3852 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3853 GetLastError() == ERROR_FILE_NOT_FOUND
)
3859 /*********************************************************************
3860 * tmpfile (MSVCRT.@)
3862 FILE* CDECL
tmpfile(void)
3864 char *filename
= tmpnam(NULL
);
3869 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3870 _S_IREAD
| _S_IWRITE
);
3871 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3873 if (msvcrt_init_fp(file
, fd
, _IORW
) == -1)
3878 else file
->_tmpfname
= _strdup(filename
);
3881 if(fd
!= -1 && !file
)
3887 /*********************************************************************
3890 int CDECL
ungetc(int c
, FILE * file
)
3892 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3894 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
3895 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
3899 if((!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
3900 && msvcrt_alloc_buffer(file
))
3901 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
3904 if(file
->_ptr
>file
->_base
) {
3906 if(file
->_flag
& _IOSTRG
) {
3907 if(*file
->_ptr
!= c
) {
3917 file
->_flag
|= _IOREAD
;
3926 /*********************************************************************
3927 * ungetwc (MSVCRT.@)
3929 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3938 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3939 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3940 unsigned char * pp
= (unsigned char *)&mwc
;
3943 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3944 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3950 char mbs
[MB_LEN_MAX
];
3953 len
= wctomb(mbs
, mwc
);
3959 for(len
--; len
>=0; len
--) {
3960 if(mbs
[len
] != ungetc(mbs
[len
], file
)) {
3973 /*********************************************************************
3974 * _getmaxstdio (MSVCRT.@)
3976 int CDECL
_getmaxstdio(void)
3978 return MSVCRT_max_streams
;
3981 /*********************************************************************
3982 * _setmaxstdio (MSVCRT.@)
3984 int CDECL
_setmaxstdio(int newmax
)
3986 TRACE("%d\n", newmax
);
3988 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
3991 MSVCRT_max_streams
= newmax
;
3992 return MSVCRT_max_streams
;